1//===- InstCombinePHI.cpp -------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the visitPHINode function.
10//
11//===----------------------------------------------------------------------===//
12
13#include "InstCombineInternal.h"
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/SmallPtrSet.h"
16#include "llvm/ADT/Statistic.h"
17#include "llvm/Analysis/InstructionSimplify.h"
18#include "llvm/Analysis/ValueTracking.h"
19#include "llvm/IR/PatternMatch.h"
20#include "llvm/Support/CommandLine.h"
21#include "llvm/Transforms/InstCombine/InstCombiner.h"
22#include "llvm/Transforms/Utils/Local.h"
23#include <optional>
24
25using namespace llvm;
26using namespace llvm::PatternMatch;
27
28#define DEBUG_TYPE "instcombine"
29
30static cl::opt<unsigned>
31MaxNumPhis("instcombine-max-num-phis", cl::init(Val: 512),
32 cl::desc("Maximum number phis to handle in intptr/ptrint folding"));
33
34STATISTIC(NumPHIsOfInsertValues,
35 "Number of phi-of-insertvalue turned into insertvalue-of-phis");
36STATISTIC(NumPHIsOfExtractValues,
37 "Number of phi-of-extractvalue turned into extractvalue-of-phi");
38STATISTIC(NumPHICSEs, "Number of PHI's that got CSE'd");
39
40/// The PHI arguments will be folded into a single operation with a PHI node
41/// as input. The debug location of the single operation will be the merged
42/// locations of the original PHI node arguments.
43void InstCombinerImpl::PHIArgMergedDebugLoc(Instruction *Inst, PHINode &PN) {
44 auto *FirstInst = cast<Instruction>(Val: PN.getIncomingValue(i: 0));
45 Inst->setDebugLoc(FirstInst->getDebugLoc());
46 // We do not expect a CallInst here, otherwise, N-way merging of DebugLoc
47 // will be inefficient.
48 assert(!isa<CallInst>(Inst));
49
50 for (Value *V : drop_begin(RangeOrContainer: PN.incoming_values())) {
51 auto *I = cast<Instruction>(Val: V);
52 Inst->applyMergedLocation(LocA: Inst->getDebugLoc(), LocB: I->getDebugLoc());
53 }
54}
55
56/// If the phi is within a phi web, which is formed by the def-use chain
57/// of phis and all the phis in the web are only used in the other phis.
58/// In this case, these phis are dead and we will remove all of them.
59bool InstCombinerImpl::foldDeadPhiWeb(PHINode &PN) {
60 SmallVector<PHINode *, 16> Stack;
61 SmallPtrSet<PHINode *, 16> Visited;
62 Stack.push_back(Elt: &PN);
63 Visited.insert(Ptr: &PN);
64 while (!Stack.empty()) {
65 PHINode *Phi = Stack.pop_back_val();
66 for (User *Use : Phi->users()) {
67 if (PHINode *PhiUse = dyn_cast<PHINode>(Val: Use)) {
68 if (!Visited.insert(Ptr: PhiUse).second)
69 continue;
70 // Early stop if the set of PHIs is large
71 if (Visited.size() >= 16)
72 return false;
73 Stack.push_back(Elt: PhiUse);
74 } else
75 return false;
76 }
77 }
78 for (PHINode *Phi : Visited)
79 replaceInstUsesWith(I&: *Phi, V: PoisonValue::get(T: Phi->getType()));
80 for (PHINode *Phi : Visited)
81 eraseInstFromFunction(I&: *Phi);
82 return true;
83}
84
85// Replace Integer typed PHI PN if the PHI's value is used as a pointer value.
86// If there is an existing pointer typed PHI that produces the same value as PN,
87// replace PN and the IntToPtr operation with it. Otherwise, synthesize a new
88// PHI node:
89//
90// Case-1:
91// bb1:
92// int_init = PtrToInt(ptr_init)
93// br label %bb2
94// bb2:
95// int_val = PHI([int_init, %bb1], [int_val_inc, %bb2]
96// ptr_val = PHI([ptr_init, %bb1], [ptr_val_inc, %bb2]
97// ptr_val2 = IntToPtr(int_val)
98// ...
99// use(ptr_val2)
100// ptr_val_inc = ...
101// inc_val_inc = PtrToInt(ptr_val_inc)
102//
103// ==>
104// bb1:
105// br label %bb2
106// bb2:
107// ptr_val = PHI([ptr_init, %bb1], [ptr_val_inc, %bb2]
108// ...
109// use(ptr_val)
110// ptr_val_inc = ...
111//
112// Case-2:
113// bb1:
114// int_ptr = BitCast(ptr_ptr)
115// int_init = Load(int_ptr)
116// br label %bb2
117// bb2:
118// int_val = PHI([int_init, %bb1], [int_val_inc, %bb2]
119// ptr_val2 = IntToPtr(int_val)
120// ...
121// use(ptr_val2)
122// ptr_val_inc = ...
123// inc_val_inc = PtrToInt(ptr_val_inc)
124// ==>
125// bb1:
126// ptr_init = Load(ptr_ptr)
127// br label %bb2
128// bb2:
129// ptr_val = PHI([ptr_init, %bb1], [ptr_val_inc, %bb2]
130// ...
131// use(ptr_val)
132// ptr_val_inc = ...
133// ...
134//
135bool InstCombinerImpl::foldIntegerTypedPHI(PHINode &PN) {
136 if (!PN.getType()->isIntegerTy())
137 return false;
138 if (!PN.hasOneUse())
139 return false;
140
141 auto *IntToPtr = dyn_cast<IntToPtrInst>(Val: PN.user_back());
142 if (!IntToPtr)
143 return false;
144
145 // Check if the pointer is actually used as pointer:
146 auto HasPointerUse = [](Instruction *IIP) {
147 for (User *U : IIP->users()) {
148 Value *Ptr = nullptr;
149 if (LoadInst *LoadI = dyn_cast<LoadInst>(Val: U)) {
150 Ptr = LoadI->getPointerOperand();
151 } else if (StoreInst *SI = dyn_cast<StoreInst>(Val: U)) {
152 Ptr = SI->getPointerOperand();
153 } else if (GetElementPtrInst *GI = dyn_cast<GetElementPtrInst>(Val: U)) {
154 Ptr = GI->getPointerOperand();
155 }
156
157 if (Ptr && Ptr == IIP)
158 return true;
159 }
160 return false;
161 };
162
163 if (!HasPointerUse(IntToPtr))
164 return false;
165
166 if (DL.getPointerSizeInBits(AS: IntToPtr->getAddressSpace()) !=
167 DL.getTypeSizeInBits(Ty: IntToPtr->getOperand(i_nocapture: 0)->getType()))
168 return false;
169
170 SmallVector<Value *, 4> AvailablePtrVals;
171 for (auto Incoming : zip(t: PN.blocks(), u: PN.incoming_values())) {
172 BasicBlock *BB = std::get<0>(t&: Incoming);
173 Value *Arg = std::get<1>(t&: Incoming);
174
175 // Arg could be a constant, constant expr, etc., which we don't cover here.
176 if (!isa<Instruction>(Val: Arg) && !isa<Argument>(Val: Arg))
177 return false;
178
179 // First look backward:
180 if (auto *PI = dyn_cast<PtrToIntInst>(Val: Arg)) {
181 AvailablePtrVals.emplace_back(Args: PI->getOperand(i_nocapture: 0));
182 continue;
183 }
184
185 // Next look forward:
186 Value *ArgIntToPtr = nullptr;
187 for (User *U : Arg->users()) {
188 if (isa<IntToPtrInst>(Val: U) && U->getType() == IntToPtr->getType() &&
189 (DT.dominates(Def: cast<Instruction>(Val: U), BB) ||
190 cast<Instruction>(Val: U)->getParent() == BB)) {
191 ArgIntToPtr = U;
192 break;
193 }
194 }
195
196 if (ArgIntToPtr) {
197 AvailablePtrVals.emplace_back(Args&: ArgIntToPtr);
198 continue;
199 }
200
201 // If Arg is defined by a PHI, allow it. This will also create
202 // more opportunities iteratively.
203 if (isa<PHINode>(Val: Arg)) {
204 AvailablePtrVals.emplace_back(Args&: Arg);
205 continue;
206 }
207
208 // For a single use integer load:
209 auto *LoadI = dyn_cast<LoadInst>(Val: Arg);
210 if (!LoadI)
211 return false;
212
213 if (!LoadI->hasOneUse())
214 return false;
215
216 // Push the integer typed Load instruction into the available
217 // value set, and fix it up later when the pointer typed PHI
218 // is synthesized.
219 AvailablePtrVals.emplace_back(Args&: LoadI);
220 }
221
222 // Now search for a matching PHI
223 auto *BB = PN.getParent();
224 assert(AvailablePtrVals.size() == PN.getNumIncomingValues() &&
225 "Not enough available ptr typed incoming values");
226 PHINode *MatchingPtrPHI = nullptr;
227 unsigned NumPhis = 0;
228 for (PHINode &PtrPHI : BB->phis()) {
229 // FIXME: consider handling this in AggressiveInstCombine
230 if (NumPhis++ > MaxNumPhis)
231 return false;
232 if (&PtrPHI == &PN || PtrPHI.getType() != IntToPtr->getType())
233 continue;
234 if (any_of(Range: zip(t: PN.blocks(), u&: AvailablePtrVals),
235 P: [&](const auto &BlockAndValue) {
236 BasicBlock *BB = std::get<0>(BlockAndValue);
237 Value *V = std::get<1>(BlockAndValue);
238 return PtrPHI.getIncomingValueForBlock(BB) != V;
239 }))
240 continue;
241 MatchingPtrPHI = &PtrPHI;
242 break;
243 }
244
245 if (MatchingPtrPHI) {
246 assert(MatchingPtrPHI->getType() == IntToPtr->getType() &&
247 "Phi's Type does not match with IntToPtr");
248 // Explicitly replace the inttoptr (rather than inserting a ptrtoint) here,
249 // to make sure another transform can't undo it in the meantime.
250 replaceInstUsesWith(I&: *IntToPtr, V: MatchingPtrPHI);
251 eraseInstFromFunction(I&: *IntToPtr);
252 eraseInstFromFunction(I&: PN);
253 return true;
254 }
255
256 // If it requires a conversion for every PHI operand, do not do it.
257 if (all_of(Range&: AvailablePtrVals, P: [&](Value *V) {
258 return (V->getType() != IntToPtr->getType()) || isa<IntToPtrInst>(Val: V);
259 }))
260 return false;
261
262 // If any of the operand that requires casting is a terminator
263 // instruction, do not do it. Similarly, do not do the transform if the value
264 // is PHI in a block with no insertion point, for example, a catchswitch
265 // block, since we will not be able to insert a cast after the PHI.
266 if (any_of(Range&: AvailablePtrVals, P: [&](Value *V) {
267 if (V->getType() == IntToPtr->getType())
268 return false;
269 auto *Inst = dyn_cast<Instruction>(Val: V);
270 if (!Inst)
271 return false;
272 if (Inst->isTerminator())
273 return true;
274 auto *BB = Inst->getParent();
275 if (isa<PHINode>(Val: Inst) && !BB->hasInsertionPt())
276 return true;
277 return false;
278 }))
279 return false;
280
281 PHINode *NewPtrPHI = PHINode::Create(
282 Ty: IntToPtr->getType(), NumReservedValues: PN.getNumIncomingValues(), NameStr: PN.getName() + ".ptr");
283
284 InsertNewInstBefore(New: NewPtrPHI, Old: PN.getIterator());
285 SmallDenseMap<Value *, Instruction *> Casts;
286 for (auto Incoming : zip(t: PN.blocks(), u&: AvailablePtrVals)) {
287 auto *IncomingBB = std::get<0>(t&: Incoming);
288 auto *IncomingVal = std::get<1>(t&: Incoming);
289
290 if (IncomingVal->getType() == IntToPtr->getType()) {
291 NewPtrPHI->addIncoming(V: IncomingVal, BB: IncomingBB);
292 continue;
293 }
294
295#ifndef NDEBUG
296 LoadInst *LoadI = dyn_cast<LoadInst>(IncomingVal);
297 assert((isa<PHINode>(IncomingVal) ||
298 IncomingVal->getType()->isPointerTy() ||
299 (LoadI && LoadI->hasOneUse())) &&
300 "Can not replace LoadInst with multiple uses");
301#endif
302 // Need to insert a BitCast.
303 // For an integer Load instruction with a single use, the load + IntToPtr
304 // cast will be simplified into a pointer load:
305 // %v = load i64, i64* %a.ip, align 8
306 // %v.cast = inttoptr i64 %v to float **
307 // ==>
308 // %v.ptrp = bitcast i64 * %a.ip to float **
309 // %v.cast = load float *, float ** %v.ptrp, align 8
310 Instruction *&CI = Casts[IncomingVal];
311 if (!CI) {
312 CI = CastInst::CreateBitOrPointerCast(S: IncomingVal, Ty: IntToPtr->getType(),
313 Name: IncomingVal->getName() + ".ptr");
314 if (auto *IncomingI = dyn_cast<Instruction>(Val: IncomingVal)) {
315 BasicBlock::iterator InsertPos(IncomingI);
316 InsertPos++;
317 BasicBlock *BB = IncomingI->getParent();
318 if (isa<PHINode>(Val: IncomingI))
319 InsertPos = BB->getFirstInsertionPt();
320 assert(InsertPos != BB->end() && "should have checked above");
321 InsertNewInstBefore(New: CI, Old: InsertPos);
322 } else {
323 auto *InsertBB = &IncomingBB->getParent()->getEntryBlock();
324 InsertNewInstBefore(New: CI, Old: InsertBB->getFirstInsertionPt());
325 }
326 }
327 NewPtrPHI->addIncoming(V: CI, BB: IncomingBB);
328 }
329
330 // Explicitly replace the inttoptr (rather than inserting a ptrtoint) here,
331 // to make sure another transform can't undo it in the meantime.
332 replaceInstUsesWith(I&: *IntToPtr, V: NewPtrPHI);
333 eraseInstFromFunction(I&: *IntToPtr);
334 eraseInstFromFunction(I&: PN);
335 return true;
336}
337
338// Remove RoundTrip IntToPtr/PtrToInt Cast on PHI-Operand and
339// fold Phi-operand to bitcast.
340Instruction *InstCombinerImpl::foldPHIArgIntToPtrToPHI(PHINode &PN) {
341 // convert ptr2int ( phi[ int2ptr(ptr2int(x))] ) --> ptr2int ( phi [ x ] )
342 // Make sure all uses of phi are ptr2int.
343 if (!all_of(Range: PN.users(), P: IsaPred<PtrToIntInst>))
344 return nullptr;
345
346 // Iterating over all operands to check presence of target pointers for
347 // optimization.
348 bool OperandWithRoundTripCast = false;
349 for (unsigned OpNum = 0; OpNum != PN.getNumIncomingValues(); ++OpNum) {
350 if (auto *NewOp =
351 simplifyIntToPtrRoundTripCast(Val: PN.getIncomingValue(i: OpNum))) {
352 replaceOperand(I&: PN, OpNum, V: NewOp);
353 OperandWithRoundTripCast = true;
354 }
355 }
356 if (!OperandWithRoundTripCast)
357 return nullptr;
358 return &PN;
359}
360
361/// If we have something like phi [insertvalue(a,b,0), insertvalue(c,d,0)],
362/// turn this into a phi[a,c] and phi[b,d] and a single insertvalue.
363Instruction *
364InstCombinerImpl::foldPHIArgInsertValueInstructionIntoPHI(PHINode &PN) {
365 auto *FirstIVI = cast<InsertValueInst>(Val: PN.getIncomingValue(i: 0));
366
367 // Scan to see if all operands are `insertvalue`'s with the same indices,
368 // and all have a single use.
369 for (Value *V : drop_begin(RangeOrContainer: PN.incoming_values())) {
370 auto *I = dyn_cast<InsertValueInst>(Val: V);
371 if (!I || !I->hasOneUser() || I->getIndices() != FirstIVI->getIndices())
372 return nullptr;
373 }
374
375 // For each operand of an `insertvalue`
376 std::array<PHINode *, 2> NewOperands;
377 for (int OpIdx : {0, 1}) {
378 auto *&NewOperand = NewOperands[OpIdx];
379 // Create a new PHI node to receive the values the operand has in each
380 // incoming basic block.
381 NewOperand = PHINode::Create(
382 Ty: FirstIVI->getOperand(i_nocapture: OpIdx)->getType(), NumReservedValues: PN.getNumIncomingValues(),
383 NameStr: FirstIVI->getOperand(i_nocapture: OpIdx)->getName() + ".pn");
384 // And populate each operand's PHI with said values.
385 for (auto Incoming : zip(t: PN.blocks(), u: PN.incoming_values()))
386 NewOperand->addIncoming(
387 V: cast<InsertValueInst>(Val&: std::get<1>(t&: Incoming))->getOperand(i_nocapture: OpIdx),
388 BB: std::get<0>(t&: Incoming));
389 InsertNewInstBefore(New: NewOperand, Old: PN.getIterator());
390 }
391
392 // And finally, create `insertvalue` over the newly-formed PHI nodes.
393 auto *NewIVI = InsertValueInst::Create(Agg: NewOperands[0], Val: NewOperands[1],
394 Idxs: FirstIVI->getIndices(), NameStr: PN.getName());
395
396 PHIArgMergedDebugLoc(Inst: NewIVI, PN);
397 ++NumPHIsOfInsertValues;
398 return NewIVI;
399}
400
401/// If we have something like phi [extractvalue(a,0), extractvalue(b,0)],
402/// turn this into a phi[a,b] and a single extractvalue.
403Instruction *
404InstCombinerImpl::foldPHIArgExtractValueInstructionIntoPHI(PHINode &PN) {
405 auto *FirstEVI = cast<ExtractValueInst>(Val: PN.getIncomingValue(i: 0));
406
407 // Scan to see if all operands are `extractvalue`'s with the same indices,
408 // and all have a single use.
409 for (Value *V : drop_begin(RangeOrContainer: PN.incoming_values())) {
410 auto *I = dyn_cast<ExtractValueInst>(Val: V);
411 if (!I || !I->hasOneUser() || I->getIndices() != FirstEVI->getIndices() ||
412 I->getAggregateOperand()->getType() !=
413 FirstEVI->getAggregateOperand()->getType())
414 return nullptr;
415 }
416
417 // Create a new PHI node to receive the values the aggregate operand has
418 // in each incoming basic block.
419 auto *NewAggregateOperand = PHINode::Create(
420 Ty: FirstEVI->getAggregateOperand()->getType(), NumReservedValues: PN.getNumIncomingValues(),
421 NameStr: FirstEVI->getAggregateOperand()->getName() + ".pn");
422 // And populate the PHI with said values.
423 for (auto Incoming : zip(t: PN.blocks(), u: PN.incoming_values()))
424 NewAggregateOperand->addIncoming(
425 V: cast<ExtractValueInst>(Val&: std::get<1>(t&: Incoming))->getAggregateOperand(),
426 BB: std::get<0>(t&: Incoming));
427 InsertNewInstBefore(New: NewAggregateOperand, Old: PN.getIterator());
428
429 // And finally, create `extractvalue` over the newly-formed PHI nodes.
430 auto *NewEVI = ExtractValueInst::Create(Agg: NewAggregateOperand,
431 Idxs: FirstEVI->getIndices(), NameStr: PN.getName());
432
433 PHIArgMergedDebugLoc(Inst: NewEVI, PN);
434 ++NumPHIsOfExtractValues;
435 return NewEVI;
436}
437
438/// If we have something like phi [add (a,b), add(a,c)] and if a/b/c and the
439/// adds all have a single user, turn this into a phi and a single binop.
440Instruction *InstCombinerImpl::foldPHIArgBinOpIntoPHI(PHINode &PN) {
441 Instruction *FirstInst = cast<Instruction>(Val: PN.getIncomingValue(i: 0));
442 assert(isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst));
443 unsigned Opc = FirstInst->getOpcode();
444 Value *LHSVal = FirstInst->getOperand(i: 0);
445 Value *RHSVal = FirstInst->getOperand(i: 1);
446
447 Type *LHSType = LHSVal->getType();
448 Type *RHSType = RHSVal->getType();
449
450 // Scan to see if all operands are the same opcode, and all have one user.
451 for (Value *V : drop_begin(RangeOrContainer: PN.incoming_values())) {
452 Instruction *I = dyn_cast<Instruction>(Val: V);
453 if (!I || I->getOpcode() != Opc || !I->hasOneUser() ||
454 // Verify type of the LHS matches so we don't fold cmp's of different
455 // types.
456 I->getOperand(i: 0)->getType() != LHSType ||
457 I->getOperand(i: 1)->getType() != RHSType)
458 return nullptr;
459
460 // If they are CmpInst instructions, check their predicates
461 if (CmpInst *CI = dyn_cast<CmpInst>(Val: I))
462 if (CI->getPredicate() != cast<CmpInst>(Val: FirstInst)->getPredicate())
463 return nullptr;
464
465 // Keep track of which operand needs a phi node.
466 if (I->getOperand(i: 0) != LHSVal) LHSVal = nullptr;
467 if (I->getOperand(i: 1) != RHSVal) RHSVal = nullptr;
468 }
469
470 // If both LHS and RHS would need a PHI, don't do this transformation,
471 // because it would increase the number of PHIs entering the block,
472 // which leads to higher register pressure. This is especially
473 // bad when the PHIs are in the header of a loop.
474 if (!LHSVal && !RHSVal)
475 return nullptr;
476
477 // Otherwise, this is safe to transform!
478
479 Value *InLHS = FirstInst->getOperand(i: 0);
480 Value *InRHS = FirstInst->getOperand(i: 1);
481 PHINode *NewLHS = nullptr, *NewRHS = nullptr;
482 if (!LHSVal) {
483 NewLHS = PHINode::Create(Ty: LHSType, NumReservedValues: PN.getNumIncomingValues(),
484 NameStr: FirstInst->getOperand(i: 0)->getName() + ".pn");
485 NewLHS->addIncoming(V: InLHS, BB: PN.getIncomingBlock(i: 0));
486 InsertNewInstBefore(New: NewLHS, Old: PN.getIterator());
487 LHSVal = NewLHS;
488 }
489
490 if (!RHSVal) {
491 NewRHS = PHINode::Create(Ty: RHSType, NumReservedValues: PN.getNumIncomingValues(),
492 NameStr: FirstInst->getOperand(i: 1)->getName() + ".pn");
493 NewRHS->addIncoming(V: InRHS, BB: PN.getIncomingBlock(i: 0));
494 InsertNewInstBefore(New: NewRHS, Old: PN.getIterator());
495 RHSVal = NewRHS;
496 }
497
498 // Add all operands to the new PHIs.
499 if (NewLHS || NewRHS) {
500 for (auto Incoming : drop_begin(RangeOrContainer: zip(t: PN.blocks(), u: PN.incoming_values()))) {
501 BasicBlock *InBB = std::get<0>(t&: Incoming);
502 Value *InVal = std::get<1>(t&: Incoming);
503 Instruction *InInst = cast<Instruction>(Val: InVal);
504 if (NewLHS) {
505 Value *NewInLHS = InInst->getOperand(i: 0);
506 NewLHS->addIncoming(V: NewInLHS, BB: InBB);
507 }
508 if (NewRHS) {
509 Value *NewInRHS = InInst->getOperand(i: 1);
510 NewRHS->addIncoming(V: NewInRHS, BB: InBB);
511 }
512 }
513 }
514
515 if (CmpInst *CIOp = dyn_cast<CmpInst>(Val: FirstInst)) {
516 CmpInst *NewCI = CmpInst::Create(Op: CIOp->getOpcode(), Pred: CIOp->getPredicate(),
517 S1: LHSVal, S2: RHSVal);
518 PHIArgMergedDebugLoc(Inst: NewCI, PN);
519 return NewCI;
520 }
521
522 BinaryOperator *BinOp = cast<BinaryOperator>(Val: FirstInst);
523 BinaryOperator *NewBinOp =
524 BinaryOperator::Create(Op: BinOp->getOpcode(), S1: LHSVal, S2: RHSVal);
525
526 NewBinOp->copyIRFlags(V: PN.getIncomingValue(i: 0));
527
528 for (Value *V : drop_begin(RangeOrContainer: PN.incoming_values()))
529 NewBinOp->andIRFlags(V);
530
531 PHIArgMergedDebugLoc(Inst: NewBinOp, PN);
532 return NewBinOp;
533}
534
535Instruction *InstCombinerImpl::foldPHIArgGEPIntoPHI(PHINode &PN) {
536 GetElementPtrInst *FirstInst =cast<GetElementPtrInst>(Val: PN.getIncomingValue(i: 0));
537
538 SmallVector<Value*, 16> FixedOperands(FirstInst->op_begin(),
539 FirstInst->op_end());
540 // This is true if all GEP bases are allocas and if all indices into them are
541 // constants.
542 bool AllBasePointersAreAllocas = true;
543
544 // We don't want to replace this phi if the replacement would require
545 // more than one phi, which leads to higher register pressure. This is
546 // especially bad when the PHIs are in the header of a loop.
547 bool NeededPhi = false;
548
549 // Remember flags of the first phi-operand getelementptr.
550 GEPNoWrapFlags NW = FirstInst->getNoWrapFlags();
551
552 // Scan to see if all operands are the same opcode, and all have one user.
553 for (Value *V : drop_begin(RangeOrContainer: PN.incoming_values())) {
554 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Val: V);
555 if (!GEP || !GEP->hasOneUser() ||
556 GEP->getSourceElementType() != FirstInst->getSourceElementType() ||
557 GEP->getNumOperands() != FirstInst->getNumOperands())
558 return nullptr;
559
560 NW &= GEP->getNoWrapFlags();
561
562 // Keep track of whether or not all GEPs are of alloca pointers.
563 if (AllBasePointersAreAllocas &&
564 (!isa<AllocaInst>(Val: GEP->getOperand(i_nocapture: 0)) ||
565 !GEP->hasAllConstantIndices()))
566 AllBasePointersAreAllocas = false;
567
568 // Compare the operand lists.
569 for (unsigned Op = 0, E = FirstInst->getNumOperands(); Op != E; ++Op) {
570 if (FirstInst->getOperand(i_nocapture: Op) == GEP->getOperand(i_nocapture: Op))
571 continue;
572
573 // Don't merge two GEPs when two operands differ (introducing phi nodes)
574 // if one of the PHIs has a constant for the index. The index may be
575 // substantially cheaper to compute for the constants, so making it a
576 // variable index could pessimize the path. This also handles the case
577 // for struct indices, which must always be constant.
578 if (isa<Constant>(Val: FirstInst->getOperand(i_nocapture: Op)) ||
579 isa<Constant>(Val: GEP->getOperand(i_nocapture: Op)))
580 return nullptr;
581
582 if (FirstInst->getOperand(i_nocapture: Op)->getType() !=
583 GEP->getOperand(i_nocapture: Op)->getType())
584 return nullptr;
585
586 // If we already needed a PHI for an earlier operand, and another operand
587 // also requires a PHI, we'd be introducing more PHIs than we're
588 // eliminating, which increases register pressure on entry to the PHI's
589 // block.
590 if (NeededPhi)
591 return nullptr;
592
593 FixedOperands[Op] = nullptr; // Needs a PHI.
594 NeededPhi = true;
595 }
596 }
597
598 // If all of the base pointers of the PHI'd GEPs are from allocas, don't
599 // bother doing this transformation. At best, this will just save a bit of
600 // offset calculation, but all the predecessors will have to materialize the
601 // stack address into a register anyway. We'd actually rather *clone* the
602 // load up into the predecessors so that we have a load of a gep of an alloca,
603 // which can usually all be folded into the load.
604 if (AllBasePointersAreAllocas)
605 return nullptr;
606
607 // Otherwise, this is safe to transform. Insert PHI nodes for each operand
608 // that is variable.
609 SmallVector<PHINode*, 16> OperandPhis(FixedOperands.size());
610
611 bool HasAnyPHIs = false;
612 for (unsigned I = 0, E = FixedOperands.size(); I != E; ++I) {
613 if (FixedOperands[I])
614 continue; // operand doesn't need a phi.
615 Value *FirstOp = FirstInst->getOperand(i_nocapture: I);
616 PHINode *NewPN =
617 PHINode::Create(Ty: FirstOp->getType(), NumReservedValues: E, NameStr: FirstOp->getName() + ".pn");
618 InsertNewInstBefore(New: NewPN, Old: PN.getIterator());
619
620 NewPN->addIncoming(V: FirstOp, BB: PN.getIncomingBlock(i: 0));
621 OperandPhis[I] = NewPN;
622 FixedOperands[I] = NewPN;
623 HasAnyPHIs = true;
624 }
625
626 // Add all operands to the new PHIs.
627 if (HasAnyPHIs) {
628 for (auto Incoming : drop_begin(RangeOrContainer: zip(t: PN.blocks(), u: PN.incoming_values()))) {
629 BasicBlock *InBB = std::get<0>(t&: Incoming);
630 Value *InVal = std::get<1>(t&: Incoming);
631 GetElementPtrInst *InGEP = cast<GetElementPtrInst>(Val: InVal);
632
633 for (unsigned Op = 0, E = OperandPhis.size(); Op != E; ++Op)
634 if (PHINode *OpPhi = OperandPhis[Op])
635 OpPhi->addIncoming(V: InGEP->getOperand(i_nocapture: Op), BB: InBB);
636 }
637 }
638
639 Value *Base = FixedOperands[0];
640 GetElementPtrInst *NewGEP =
641 GetElementPtrInst::Create(PointeeType: FirstInst->getSourceElementType(), Ptr: Base,
642 IdxList: ArrayRef(FixedOperands).slice(N: 1), NW);
643 PHIArgMergedDebugLoc(Inst: NewGEP, PN);
644 return NewGEP;
645}
646
647/// Return true if we know that it is safe to sink the load out of the block
648/// that defines it. This means that it must be obvious the value of the load is
649/// not changed from the point of the load to the end of the block it is in.
650///
651/// Finally, it is safe, but not profitable, to sink a load targeting a
652/// non-address-taken alloca. Doing so will cause us to not promote the alloca
653/// to a register.
654static bool isSafeAndProfitableToSinkLoad(LoadInst *L) {
655 BasicBlock::iterator BBI = L->getIterator(), E = L->getParent()->end();
656
657 for (++BBI; BBI != E; ++BBI)
658 if (BBI->mayWriteToMemory()) {
659 // Calls that only access inaccessible memory do not block sinking the
660 // load.
661 if (auto *CB = dyn_cast<CallBase>(Val&: BBI))
662 if (CB->onlyAccessesInaccessibleMemory())
663 continue;
664 return false;
665 }
666
667 // Check for non-address taken alloca. If not address-taken already, it isn't
668 // profitable to do this xform.
669 if (AllocaInst *AI = dyn_cast<AllocaInst>(Val: L->getOperand(i_nocapture: 0))) {
670 bool IsAddressTaken = false;
671 for (User *U : AI->users()) {
672 if (isa<LoadInst>(Val: U)) continue;
673 if (StoreInst *SI = dyn_cast<StoreInst>(Val: U)) {
674 // If storing TO the alloca, then the address isn't taken.
675 if (SI->getOperand(i_nocapture: 1) == AI) continue;
676 }
677 IsAddressTaken = true;
678 break;
679 }
680
681 if (!IsAddressTaken && AI->isStaticAlloca())
682 return false;
683 }
684
685 // If this load is a load from a GEP with a constant offset from an alloca,
686 // then we don't want to sink it. In its present form, it will be
687 // load [constant stack offset]. Sinking it will cause us to have to
688 // materialize the stack addresses in each predecessor in a register only to
689 // do a shared load from register in the successor.
690 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Val: L->getOperand(i_nocapture: 0)))
691 if (AllocaInst *AI = dyn_cast<AllocaInst>(Val: GEP->getOperand(i_nocapture: 0)))
692 if (AI->isStaticAlloca() && GEP->hasAllConstantIndices())
693 return false;
694
695 return true;
696}
697
698Instruction *InstCombinerImpl::foldPHIArgLoadIntoPHI(PHINode &PN) {
699 LoadInst *FirstLI = cast<LoadInst>(Val: PN.getIncomingValue(i: 0));
700
701 if (!canReplaceOperandWithVariable(I: FirstLI, OpIdx: 0))
702 return nullptr;
703
704 // FIXME: This is overconservative; this transform is allowed in some cases
705 // for atomic operations.
706 if (FirstLI->isAtomic())
707 return nullptr;
708
709 // When processing loads, we need to propagate two bits of information to the
710 // sunk load: whether it is volatile, and what its alignment is.
711 bool IsVolatile = FirstLI->isVolatile();
712 Align LoadAlignment = FirstLI->getAlign();
713 const unsigned LoadAddrSpace = FirstLI->getPointerAddressSpace();
714
715 // We can't sink the load if the loaded value could be modified between the
716 // load and the PHI.
717 if (FirstLI->getParent() != PN.getIncomingBlock(i: 0) ||
718 !isSafeAndProfitableToSinkLoad(L: FirstLI))
719 return nullptr;
720
721 // If the PHI is of volatile loads and the load block has multiple
722 // successors, sinking it would remove a load of the volatile value from
723 // the path through the other successor.
724 if (IsVolatile &&
725 FirstLI->getParent()->getTerminator()->getNumSuccessors() != 1)
726 return nullptr;
727
728 for (auto Incoming : drop_begin(RangeOrContainer: zip(t: PN.blocks(), u: PN.incoming_values()))) {
729 BasicBlock *InBB = std::get<0>(t&: Incoming);
730 Value *InVal = std::get<1>(t&: Incoming);
731 LoadInst *LI = dyn_cast<LoadInst>(Val: InVal);
732 if (!LI || !LI->hasOneUser() || LI->isAtomic())
733 return nullptr;
734
735 // Make sure all arguments are the same type of operation.
736 if (LI->isVolatile() != IsVolatile ||
737 LI->getPointerAddressSpace() != LoadAddrSpace)
738 return nullptr;
739
740 if (!canReplaceOperandWithVariable(I: LI, OpIdx: 0))
741 return nullptr;
742
743 // We can't sink the load if the loaded value could be modified between
744 // the load and the PHI.
745 if (LI->getParent() != InBB || !isSafeAndProfitableToSinkLoad(L: LI))
746 return nullptr;
747
748 LoadAlignment = std::min(a: LoadAlignment, b: LI->getAlign());
749
750 // If the PHI is of volatile loads and the load block has multiple
751 // successors, sinking it would remove a load of the volatile value from
752 // the path through the other successor.
753 if (IsVolatile && LI->getParent()->getTerminator()->getNumSuccessors() != 1)
754 return nullptr;
755 }
756
757 // Okay, they are all the same operation. Create a new PHI node of the
758 // correct type, and PHI together all of the LHS's of the instructions.
759 PHINode *NewPN = PHINode::Create(Ty: FirstLI->getOperand(i_nocapture: 0)->getType(),
760 NumReservedValues: PN.getNumIncomingValues(),
761 NameStr: PN.getName()+".in");
762
763 Value *InVal = FirstLI->getOperand(i_nocapture: 0);
764 NewPN->addIncoming(V: InVal, BB: PN.getIncomingBlock(i: 0));
765 LoadInst *NewLI =
766 new LoadInst(FirstLI->getType(), NewPN, "", IsVolatile, LoadAlignment);
767 NewLI->copyMetadata(SrcInst: *FirstLI);
768
769 // Add all operands to the new PHI and combine TBAA metadata.
770 for (auto Incoming : drop_begin(RangeOrContainer: zip(t: PN.blocks(), u: PN.incoming_values()))) {
771 BasicBlock *BB = std::get<0>(t&: Incoming);
772 Value *V = std::get<1>(t&: Incoming);
773 LoadInst *LI = cast<LoadInst>(Val: V);
774 combineMetadataForCSE(K: NewLI, J: LI, DoesKMove: true);
775 Value *NewInVal = LI->getOperand(i_nocapture: 0);
776 if (NewInVal != InVal)
777 InVal = nullptr;
778 NewPN->addIncoming(V: NewInVal, BB);
779 }
780
781 if (InVal) {
782 // The new PHI unions all of the same values together. This is really
783 // common, so we handle it intelligently here for compile-time speed.
784 NewLI->setOperand(i_nocapture: 0, Val_nocapture: InVal);
785 delete NewPN;
786 } else {
787 InsertNewInstBefore(New: NewPN, Old: PN.getIterator());
788 }
789
790 // If this was a volatile load that we are merging, make sure to loop through
791 // and mark all the input loads as non-volatile. If we don't do this, we will
792 // insert a new volatile load and the old ones will not be deletable.
793 if (IsVolatile)
794 for (Value *IncValue : PN.incoming_values())
795 cast<LoadInst>(Val: IncValue)->setVolatile(false);
796
797 PHIArgMergedDebugLoc(Inst: NewLI, PN);
798 return NewLI;
799}
800
801/// TODO: This function could handle other cast types, but then it might
802/// require special-casing a cast from the 'i1' type. See the comment in
803/// FoldPHIArgOpIntoPHI() about pessimizing illegal integer types.
804Instruction *InstCombinerImpl::foldPHIArgZextsIntoPHI(PHINode &Phi) {
805 // We cannot create a new instruction after the PHI if the terminator is an
806 // EHPad because there is no valid insertion point.
807 if (Instruction *TI = Phi.getParent()->getTerminator())
808 if (TI->isEHPad())
809 return nullptr;
810
811 // Early exit for the common case of a phi with two operands. These are
812 // handled elsewhere. See the comment below where we check the count of zexts
813 // and constants for more details.
814 unsigned NumIncomingValues = Phi.getNumIncomingValues();
815 if (NumIncomingValues < 3)
816 return nullptr;
817
818 // Find the narrower type specified by the first zext.
819 Type *NarrowType = nullptr;
820 for (Value *V : Phi.incoming_values()) {
821 if (auto *Zext = dyn_cast<ZExtInst>(Val: V)) {
822 NarrowType = Zext->getSrcTy();
823 break;
824 }
825 }
826 if (!NarrowType)
827 return nullptr;
828
829 // Walk the phi operands checking that we only have zexts or constants that
830 // we can shrink for free. Store the new operands for the new phi.
831 SmallVector<Value *, 4> NewIncoming;
832 unsigned NumZexts = 0;
833 unsigned NumConsts = 0;
834 for (Value *V : Phi.incoming_values()) {
835 if (auto *Zext = dyn_cast<ZExtInst>(Val: V)) {
836 // All zexts must be identical and have one user.
837 if (Zext->getSrcTy() != NarrowType || !Zext->hasOneUser())
838 return nullptr;
839 NewIncoming.push_back(Elt: Zext->getOperand(i_nocapture: 0));
840 NumZexts++;
841 } else if (auto *C = dyn_cast<Constant>(Val: V)) {
842 // Make sure that constants can fit in the new type.
843 Constant *Trunc = getLosslessUnsignedTrunc(C, DestTy: NarrowType, DL);
844 if (!Trunc)
845 return nullptr;
846 NewIncoming.push_back(Elt: Trunc);
847 NumConsts++;
848 } else {
849 // If it's not a cast or a constant, bail out.
850 return nullptr;
851 }
852 }
853
854 // The more common cases of a phi with no constant operands or just one
855 // variable operand are handled by FoldPHIArgOpIntoPHI() and foldOpIntoPhi()
856 // respectively. foldOpIntoPhi() wants to do the opposite transform that is
857 // performed here. It tries to replicate a cast in the phi operand's basic
858 // block to expose other folding opportunities. Thus, InstCombine will
859 // infinite loop without this check.
860 if (NumConsts == 0 || NumZexts < 2)
861 return nullptr;
862
863 // All incoming values are zexts or constants that are safe to truncate.
864 // Create a new phi node of the narrow type, phi together all of the new
865 // operands, and zext the result back to the original type.
866 PHINode *NewPhi = PHINode::Create(Ty: NarrowType, NumReservedValues: NumIncomingValues,
867 NameStr: Phi.getName() + ".shrunk");
868 for (unsigned I = 0; I != NumIncomingValues; ++I)
869 NewPhi->addIncoming(V: NewIncoming[I], BB: Phi.getIncomingBlock(i: I));
870
871 InsertNewInstBefore(New: NewPhi, Old: Phi.getIterator());
872 auto *CI = CastInst::CreateZExtOrBitCast(S: NewPhi, Ty: Phi.getType());
873
874 // We use a dropped location here because the new ZExt is necessarily a merge
875 // of ZExtInsts and at least one constant from incoming branches; the presence
876 // of the constant means we have no viable DebugLoc from that branch, and
877 // therefore we must use a dropped location.
878 CI->setDebugLoc(DebugLoc::getDropped());
879 return CI;
880}
881
882/// If all operands to a PHI node are the same "unary" operator and they all are
883/// only used by the PHI, PHI together their inputs, and do the operation once,
884/// to the result of the PHI.
885Instruction *InstCombinerImpl::foldPHIArgOpIntoPHI(PHINode &PN) {
886 // We cannot create a new instruction after the PHI if the terminator is an
887 // EHPad because there is no valid insertion point.
888 if (Instruction *TI = PN.getParent()->getTerminator())
889 if (TI->isEHPad())
890 return nullptr;
891
892 Instruction *FirstInst = cast<Instruction>(Val: PN.getIncomingValue(i: 0));
893
894 if (isa<GetElementPtrInst>(Val: FirstInst))
895 return foldPHIArgGEPIntoPHI(PN);
896 if (isa<LoadInst>(Val: FirstInst))
897 return foldPHIArgLoadIntoPHI(PN);
898 if (isa<InsertValueInst>(Val: FirstInst))
899 return foldPHIArgInsertValueInstructionIntoPHI(PN);
900 if (isa<ExtractValueInst>(Val: FirstInst))
901 return foldPHIArgExtractValueInstructionIntoPHI(PN);
902
903 // Scan the instruction, looking for input operations that can be folded away.
904 // If all input operands to the phi are the same instruction (e.g. a cast from
905 // the same type or "+42") we can pull the operation through the PHI, reducing
906 // code size and simplifying code.
907 Constant *ConstantOp = nullptr;
908 Type *CastSrcTy = nullptr;
909
910 if (isa<CastInst>(Val: FirstInst)) {
911 CastSrcTy = FirstInst->getOperand(i: 0)->getType();
912
913 // Be careful about transforming integer PHIs. We don't want to pessimize
914 // the code by turning an i32 into an i1293.
915 if (PN.getType()->isIntegerTy() && CastSrcTy->isIntegerTy()) {
916 if (!shouldChangeType(From: PN.getType(), To: CastSrcTy))
917 return nullptr;
918 }
919 } else if (isa<BinaryOperator>(Val: FirstInst) || isa<CmpInst>(Val: FirstInst)) {
920 // Can fold binop, compare or shift here if the RHS is a constant,
921 // otherwise call FoldPHIArgBinOpIntoPHI.
922 ConstantOp = dyn_cast<Constant>(Val: FirstInst->getOperand(i: 1));
923 if (!ConstantOp)
924 return foldPHIArgBinOpIntoPHI(PN);
925 } else {
926 return nullptr; // Cannot fold this operation.
927 }
928
929 // Check to see if all arguments are the same operation.
930 for (Value *V : drop_begin(RangeOrContainer: PN.incoming_values())) {
931 Instruction *I = dyn_cast<Instruction>(Val: V);
932 if (!I || !I->hasOneUser() || !I->isSameOperationAs(I: FirstInst))
933 return nullptr;
934 if (CastSrcTy) {
935 if (I->getOperand(i: 0)->getType() != CastSrcTy)
936 return nullptr; // Cast operation must match.
937 } else if (I->getOperand(i: 1) != ConstantOp) {
938 return nullptr;
939 }
940 }
941
942 // Okay, they are all the same operation. Create a new PHI node of the
943 // correct type, and PHI together all of the LHS's of the instructions.
944 PHINode *NewPN = PHINode::Create(Ty: FirstInst->getOperand(i: 0)->getType(),
945 NumReservedValues: PN.getNumIncomingValues(),
946 NameStr: PN.getName()+".in");
947
948 Value *InVal = FirstInst->getOperand(i: 0);
949 NewPN->addIncoming(V: InVal, BB: PN.getIncomingBlock(i: 0));
950
951 // Add all operands to the new PHI.
952 for (auto Incoming : drop_begin(RangeOrContainer: zip(t: PN.blocks(), u: PN.incoming_values()))) {
953 BasicBlock *BB = std::get<0>(t&: Incoming);
954 Value *V = std::get<1>(t&: Incoming);
955 Value *NewInVal = cast<Instruction>(Val: V)->getOperand(i: 0);
956 if (NewInVal != InVal)
957 InVal = nullptr;
958 NewPN->addIncoming(V: NewInVal, BB);
959 }
960
961 Value *PhiVal;
962 if (InVal) {
963 // The new PHI unions all of the same values together. This is really
964 // common, so we handle it intelligently here for compile-time speed.
965 PhiVal = InVal;
966 delete NewPN;
967 } else {
968 InsertNewInstBefore(New: NewPN, Old: PN.getIterator());
969 PhiVal = NewPN;
970 }
971
972 // Insert and return the new operation.
973 if (CastInst *FirstCI = dyn_cast<CastInst>(Val: FirstInst)) {
974 CastInst *NewCI = CastInst::Create(FirstCI->getOpcode(), S: PhiVal,
975 Ty: PN.getType());
976 PHIArgMergedDebugLoc(Inst: NewCI, PN);
977 return NewCI;
978 }
979
980 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Val: FirstInst)) {
981 BinOp = BinaryOperator::Create(Op: BinOp->getOpcode(), S1: PhiVal, S2: ConstantOp);
982 BinOp->copyIRFlags(V: PN.getIncomingValue(i: 0));
983
984 for (Value *V : drop_begin(RangeOrContainer: PN.incoming_values()))
985 BinOp->andIRFlags(V);
986
987 PHIArgMergedDebugLoc(Inst: BinOp, PN);
988 return BinOp;
989 }
990
991 CmpInst *CIOp = cast<CmpInst>(Val: FirstInst);
992 CmpInst *NewCI = CmpInst::Create(Op: CIOp->getOpcode(), Pred: CIOp->getPredicate(),
993 S1: PhiVal, S2: ConstantOp);
994 PHIArgMergedDebugLoc(Inst: NewCI, PN);
995 return NewCI;
996}
997
998/// Return true if this phi node is always equal to NonPhiInVal.
999/// This happens with mutually cyclic phi nodes like:
1000/// z = some value; x = phi (y, z); y = phi (x, z)
1001static bool PHIsEqualValue(PHINode *PN, Value *&NonPhiInVal,
1002 SmallPtrSetImpl<PHINode *> &ValueEqualPHIs) {
1003 // See if we already saw this PHI node.
1004 if (!ValueEqualPHIs.insert(Ptr: PN).second)
1005 return true;
1006
1007 // Don't scan crazily complex things.
1008 if (ValueEqualPHIs.size() >= 16)
1009 return false;
1010
1011 // Scan the operands to see if they are either phi nodes or are equal to
1012 // the value.
1013 for (Value *Op : PN->incoming_values()) {
1014 if (PHINode *OpPN = dyn_cast<PHINode>(Val: Op)) {
1015 if (!PHIsEqualValue(PN: OpPN, NonPhiInVal, ValueEqualPHIs)) {
1016 if (NonPhiInVal)
1017 return false;
1018 NonPhiInVal = OpPN;
1019 }
1020 } else if (Op != NonPhiInVal)
1021 return false;
1022 }
1023
1024 return true;
1025}
1026
1027/// Return an existing non-zero constant if this phi node has one, otherwise
1028/// return constant 1.
1029static ConstantInt *getAnyNonZeroConstInt(PHINode &PN) {
1030 assert(isa<IntegerType>(PN.getType()) && "Expect only integer type phi");
1031 for (Value *V : PN.operands())
1032 if (auto *ConstVA = dyn_cast<ConstantInt>(Val: V))
1033 if (!ConstVA->isZero())
1034 return ConstVA;
1035 return ConstantInt::get(Ty: cast<IntegerType>(Val: PN.getType()), V: 1);
1036}
1037
1038namespace {
1039struct PHIUsageRecord {
1040 unsigned PHIId; // The ID # of the PHI (something determinstic to sort on)
1041 unsigned Shift; // The amount shifted.
1042 Instruction *Inst; // The trunc instruction.
1043
1044 PHIUsageRecord(unsigned Pn, unsigned Sh, Instruction *User)
1045 : PHIId(Pn), Shift(Sh), Inst(User) {}
1046
1047 bool operator<(const PHIUsageRecord &RHS) const {
1048 if (PHIId < RHS.PHIId) return true;
1049 if (PHIId > RHS.PHIId) return false;
1050 if (Shift < RHS.Shift) return true;
1051 if (Shift > RHS.Shift) return false;
1052 return Inst->getType()->getPrimitiveSizeInBits() <
1053 RHS.Inst->getType()->getPrimitiveSizeInBits();
1054 }
1055};
1056
1057struct LoweredPHIRecord {
1058 PHINode *PN; // The PHI that was lowered.
1059 unsigned Shift; // The amount shifted.
1060 unsigned Width; // The width extracted.
1061
1062 LoweredPHIRecord(PHINode *Phi, unsigned Sh, Type *Ty)
1063 : PN(Phi), Shift(Sh), Width(Ty->getPrimitiveSizeInBits()) {}
1064
1065 // Ctor form used by DenseMap.
1066 LoweredPHIRecord(PHINode *Phi, unsigned Sh) : PN(Phi), Shift(Sh), Width(0) {}
1067};
1068} // namespace
1069
1070template <> struct llvm::DenseMapInfo<LoweredPHIRecord> {
1071 static inline LoweredPHIRecord getEmptyKey() {
1072 return LoweredPHIRecord(nullptr, 0);
1073 }
1074 static inline LoweredPHIRecord getTombstoneKey() {
1075 return LoweredPHIRecord(nullptr, 1);
1076 }
1077 static unsigned getHashValue(const LoweredPHIRecord &Val) {
1078 return DenseMapInfo<PHINode *>::getHashValue(PtrVal: Val.PN) ^ (Val.Shift >> 3) ^
1079 (Val.Width >> 3);
1080 }
1081 static bool isEqual(const LoweredPHIRecord &LHS,
1082 const LoweredPHIRecord &RHS) {
1083 return LHS.PN == RHS.PN && LHS.Shift == RHS.Shift && LHS.Width == RHS.Width;
1084 }
1085};
1086
1087/// This is an integer PHI and we know that it has an illegal type: see if it is
1088/// only used by trunc or trunc(lshr) operations. If so, we split the PHI into
1089/// the various pieces being extracted. This sort of thing is introduced when
1090/// SROA promotes an aggregate to large integer values.
1091///
1092/// TODO: The user of the trunc may be an bitcast to float/double/vector or an
1093/// inttoptr. We should produce new PHIs in the right type.
1094///
1095Instruction *InstCombinerImpl::SliceUpIllegalIntegerPHI(PHINode &FirstPhi) {
1096 // PHIUsers - Keep track of all of the truncated values extracted from a set
1097 // of PHIs, along with their offset. These are the things we want to rewrite.
1098 SmallVector<PHIUsageRecord, 16> PHIUsers;
1099
1100 // PHIs are often mutually cyclic, so we keep track of a whole set of PHI
1101 // nodes which are extracted from. PHIsToSlice is a set we use to avoid
1102 // revisiting PHIs, PHIsInspected is a ordered list of PHIs that we need to
1103 // check the uses of (to ensure they are all extracts).
1104 SmallVector<PHINode*, 8> PHIsToSlice;
1105 SmallPtrSet<PHINode*, 8> PHIsInspected;
1106
1107 PHIsToSlice.push_back(Elt: &FirstPhi);
1108 PHIsInspected.insert(Ptr: &FirstPhi);
1109
1110 for (unsigned PHIId = 0; PHIId != PHIsToSlice.size(); ++PHIId) {
1111 PHINode *PN = PHIsToSlice[PHIId];
1112
1113 // Scan the input list of the PHI. If any input is an invoke, and if the
1114 // input is defined in the predecessor, then we won't be split the critical
1115 // edge which is required to insert a truncate. Because of this, we have to
1116 // bail out.
1117 for (auto Incoming : zip(t: PN->blocks(), u: PN->incoming_values())) {
1118 BasicBlock *BB = std::get<0>(t&: Incoming);
1119 Value *V = std::get<1>(t&: Incoming);
1120 InvokeInst *II = dyn_cast<InvokeInst>(Val: V);
1121 if (!II)
1122 continue;
1123 if (II->getParent() != BB)
1124 continue;
1125
1126 // If we have a phi, and if it's directly in the predecessor, then we have
1127 // a critical edge where we need to put the truncate. Since we can't
1128 // split the edge in instcombine, we have to bail out.
1129 return nullptr;
1130 }
1131
1132 // If the incoming value is a PHI node before a catchswitch, we cannot
1133 // extract the value within that BB because we cannot insert any non-PHI
1134 // instructions in the BB.
1135 for (auto *Pred : PN->blocks())
1136 if (!Pred->hasInsertionPt())
1137 return nullptr;
1138
1139 for (User *U : PN->users()) {
1140 Instruction *UserI = cast<Instruction>(Val: U);
1141
1142 // If the user is a PHI, inspect its uses recursively.
1143 if (PHINode *UserPN = dyn_cast<PHINode>(Val: UserI)) {
1144 if (PHIsInspected.insert(Ptr: UserPN).second)
1145 PHIsToSlice.push_back(Elt: UserPN);
1146 continue;
1147 }
1148
1149 // Truncates are always ok.
1150 if (isa<TruncInst>(Val: UserI)) {
1151 PHIUsers.push_back(Elt: PHIUsageRecord(PHIId, 0, UserI));
1152 continue;
1153 }
1154
1155 // Otherwise it must be a lshr which can only be used by one trunc.
1156 if (UserI->getOpcode() != Instruction::LShr ||
1157 !UserI->hasOneUse() || !isa<TruncInst>(Val: UserI->user_back()) ||
1158 !isa<ConstantInt>(Val: UserI->getOperand(i: 1)))
1159 return nullptr;
1160
1161 // Bail on out of range shifts.
1162 unsigned SizeInBits = UserI->getType()->getScalarSizeInBits();
1163 if (cast<ConstantInt>(Val: UserI->getOperand(i: 1))->getValue().uge(RHS: SizeInBits))
1164 return nullptr;
1165
1166 unsigned Shift = cast<ConstantInt>(Val: UserI->getOperand(i: 1))->getZExtValue();
1167 PHIUsers.push_back(Elt: PHIUsageRecord(PHIId, Shift, UserI->user_back()));
1168 }
1169 }
1170
1171 // If we have no users, they must be all self uses, just nuke the PHI.
1172 if (PHIUsers.empty())
1173 return replaceInstUsesWith(I&: FirstPhi, V: PoisonValue::get(T: FirstPhi.getType()));
1174
1175 // If this phi node is transformable, create new PHIs for all the pieces
1176 // extracted out of it. First, sort the users by their offset and size.
1177 array_pod_sort(Start: PHIUsers.begin(), End: PHIUsers.end());
1178
1179 LLVM_DEBUG(dbgs() << "SLICING UP PHI: " << FirstPhi << '\n';
1180 for (unsigned I = 1; I != PHIsToSlice.size(); ++I) dbgs()
1181 << "AND USER PHI #" << I << ": " << *PHIsToSlice[I] << '\n');
1182
1183 // PredValues - This is a temporary used when rewriting PHI nodes. It is
1184 // hoisted out here to avoid construction/destruction thrashing.
1185 DenseMap<BasicBlock*, Value*> PredValues;
1186
1187 // ExtractedVals - Each new PHI we introduce is saved here so we don't
1188 // introduce redundant PHIs.
1189 DenseMap<LoweredPHIRecord, PHINode*> ExtractedVals;
1190
1191 for (unsigned UserI = 0, UserE = PHIUsers.size(); UserI != UserE; ++UserI) {
1192 unsigned PHIId = PHIUsers[UserI].PHIId;
1193 PHINode *PN = PHIsToSlice[PHIId];
1194 unsigned Offset = PHIUsers[UserI].Shift;
1195 Type *Ty = PHIUsers[UserI].Inst->getType();
1196
1197 PHINode *EltPHI;
1198
1199 // If we've already lowered a user like this, reuse the previously lowered
1200 // value.
1201 if ((EltPHI = ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)]) == nullptr) {
1202
1203 // Otherwise, Create the new PHI node for this user.
1204 EltPHI = PHINode::Create(Ty, NumReservedValues: PN->getNumIncomingValues(),
1205 NameStr: PN->getName() + ".off" + Twine(Offset),
1206 InsertBefore: PN->getIterator());
1207 assert(EltPHI->getType() != PN->getType() &&
1208 "Truncate didn't shrink phi?");
1209
1210 for (auto Incoming : zip(t: PN->blocks(), u: PN->incoming_values())) {
1211 BasicBlock *Pred = std::get<0>(t&: Incoming);
1212 Value *InVal = std::get<1>(t&: Incoming);
1213 Value *&PredVal = PredValues[Pred];
1214
1215 // If we already have a value for this predecessor, reuse it.
1216 if (PredVal) {
1217 EltPHI->addIncoming(V: PredVal, BB: Pred);
1218 continue;
1219 }
1220
1221 // Handle the PHI self-reuse case.
1222 if (InVal == PN) {
1223 PredVal = EltPHI;
1224 EltPHI->addIncoming(V: PredVal, BB: Pred);
1225 continue;
1226 }
1227
1228 if (PHINode *InPHI = dyn_cast<PHINode>(Val: PN)) {
1229 // If the incoming value was a PHI, and if it was one of the PHIs we
1230 // already rewrote it, just use the lowered value.
1231 if (Value *Res = ExtractedVals[LoweredPHIRecord(InPHI, Offset, Ty)]) {
1232 PredVal = Res;
1233 EltPHI->addIncoming(V: PredVal, BB: Pred);
1234 continue;
1235 }
1236 }
1237
1238 // Otherwise, do an extract in the predecessor.
1239 Builder.SetInsertPoint(Pred->getTerminator());
1240 Value *Res = InVal;
1241 if (Offset)
1242 Res = Builder.CreateLShr(
1243 LHS: Res, RHS: ConstantInt::get(Ty: InVal->getType(), V: Offset), Name: "extract");
1244 Res = Builder.CreateTrunc(V: Res, DestTy: Ty, Name: "extract.t");
1245 PredVal = Res;
1246 EltPHI->addIncoming(V: Res, BB: Pred);
1247
1248 // If the incoming value was a PHI, and if it was one of the PHIs we are
1249 // rewriting, we will ultimately delete the code we inserted. This
1250 // means we need to revisit that PHI to make sure we extract out the
1251 // needed piece.
1252 if (PHINode *OldInVal = dyn_cast<PHINode>(Val: InVal))
1253 if (PHIsInspected.count(Ptr: OldInVal)) {
1254 unsigned RefPHIId =
1255 find(Range&: PHIsToSlice, Val: OldInVal) - PHIsToSlice.begin();
1256 PHIUsers.push_back(
1257 Elt: PHIUsageRecord(RefPHIId, Offset, cast<Instruction>(Val: Res)));
1258 ++UserE;
1259 }
1260 }
1261 PredValues.clear();
1262
1263 LLVM_DEBUG(dbgs() << " Made element PHI for offset " << Offset << ": "
1264 << *EltPHI << '\n');
1265 ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)] = EltPHI;
1266 }
1267
1268 // Replace the use of this piece with the PHI node.
1269 replaceInstUsesWith(I&: *PHIUsers[UserI].Inst, V: EltPHI);
1270 }
1271
1272 // Replace all the remaining uses of the PHI nodes (self uses and the lshrs)
1273 // with poison.
1274 Value *Poison = PoisonValue::get(T: FirstPhi.getType());
1275 for (PHINode *PHI : drop_begin(RangeOrContainer&: PHIsToSlice))
1276 replaceInstUsesWith(I&: *PHI, V: Poison);
1277 return replaceInstUsesWith(I&: FirstPhi, V: Poison);
1278}
1279
1280static Value *simplifyUsingControlFlow(InstCombiner &Self, PHINode &PN,
1281 const DominatorTree &DT) {
1282 // Simplify the following patterns:
1283 // if (cond)
1284 // / \
1285 // ... ...
1286 // \ /
1287 // phi [true] [false]
1288 // and
1289 // switch (cond)
1290 // case v1: / \ case v2:
1291 // ... ...
1292 // \ /
1293 // phi [v1] [v2]
1294 // Make sure all inputs are constants.
1295 if (!all_of(Range: PN.operands(), P: IsaPred<ConstantInt>))
1296 return nullptr;
1297
1298 BasicBlock *BB = PN.getParent();
1299 // Do not bother with unreachable instructions.
1300 if (!DT.isReachableFromEntry(A: BB))
1301 return nullptr;
1302
1303 // Determine which value the condition of the idom has for which successor.
1304 LLVMContext &Context = PN.getContext();
1305 auto *IDom = DT.getNode(BB)->getIDom()->getBlock();
1306 Value *Cond;
1307 SmallDenseMap<ConstantInt *, BasicBlock *, 8> SuccForValue;
1308 SmallDenseMap<BasicBlock *, unsigned, 8> SuccCount;
1309 auto AddSucc = [&](ConstantInt *C, BasicBlock *Succ) {
1310 SuccForValue[C] = Succ;
1311 ++SuccCount[Succ];
1312 };
1313 if (auto *BI = dyn_cast<BranchInst>(Val: IDom->getTerminator())) {
1314 if (BI->isUnconditional())
1315 return nullptr;
1316
1317 Cond = BI->getCondition();
1318 AddSucc(ConstantInt::getTrue(Context), BI->getSuccessor(i: 0));
1319 AddSucc(ConstantInt::getFalse(Context), BI->getSuccessor(i: 1));
1320 } else if (auto *SI = dyn_cast<SwitchInst>(Val: IDom->getTerminator())) {
1321 Cond = SI->getCondition();
1322 ++SuccCount[SI->getDefaultDest()];
1323 for (auto Case : SI->cases())
1324 AddSucc(Case.getCaseValue(), Case.getCaseSuccessor());
1325 } else {
1326 return nullptr;
1327 }
1328
1329 if (Cond->getType() != PN.getType())
1330 return nullptr;
1331
1332 // Check that edges outgoing from the idom's terminators dominate respective
1333 // inputs of the Phi.
1334 std::optional<bool> Invert;
1335 for (auto Pair : zip(t: PN.incoming_values(), u: PN.blocks())) {
1336 auto *Input = cast<ConstantInt>(Val&: std::get<0>(t&: Pair));
1337 BasicBlock *Pred = std::get<1>(t&: Pair);
1338 auto IsCorrectInput = [&](ConstantInt *Input) {
1339 // The input needs to be dominated by the corresponding edge of the idom.
1340 // This edge cannot be a multi-edge, as that would imply that multiple
1341 // different condition values follow the same edge.
1342 auto It = SuccForValue.find(Val: Input);
1343 return It != SuccForValue.end() && SuccCount[It->second] == 1 &&
1344 DT.dominates(BBE1: BasicBlockEdge(IDom, It->second),
1345 BBE2: BasicBlockEdge(Pred, BB));
1346 };
1347
1348 // Depending on the constant, the condition may need to be inverted.
1349 bool NeedsInvert;
1350 if (IsCorrectInput(Input))
1351 NeedsInvert = false;
1352 else if (IsCorrectInput(cast<ConstantInt>(Val: ConstantExpr::getNot(C: Input))))
1353 NeedsInvert = true;
1354 else
1355 return nullptr;
1356
1357 // Make sure the inversion requirement is always the same.
1358 if (Invert && *Invert != NeedsInvert)
1359 return nullptr;
1360
1361 Invert = NeedsInvert;
1362 }
1363
1364 if (!*Invert)
1365 return Cond;
1366
1367 // This Phi is actually opposite to branching condition of IDom. We invert
1368 // the condition that will potentially open up some opportunities for
1369 // sinking.
1370 auto InsertPt = BB->getFirstInsertionPt();
1371 if (InsertPt != BB->end()) {
1372 Self.Builder.SetInsertPoint(TheBB: &*BB, IP: InsertPt);
1373 return Self.Builder.CreateNot(V: Cond);
1374 }
1375
1376 return nullptr;
1377}
1378
1379// Fold iv = phi(start, iv.next = iv2.next op start)
1380// where iv2 = phi(iv2.start, iv2.next = iv2 + iv2.step)
1381// and iv2.start op start = start
1382// to iv = iv2 op start
1383static Value *foldDependentIVs(PHINode &PN, IRBuilderBase &Builder) {
1384 BasicBlock *BB = PN.getParent();
1385 if (PN.getNumIncomingValues() != 2)
1386 return nullptr;
1387
1388 Value *Start;
1389 Instruction *IvNext;
1390 BinaryOperator *Iv2Next;
1391 auto MatchOuterIV = [&](Value *V1, Value *V2) {
1392 if (match(V: V2, P: m_c_BinOp(L: m_Specific(V: V1), R: m_BinOp(I&: Iv2Next))) ||
1393 match(V: V2, P: m_GEP(Ops: m_Specific(V: V1), Ops: m_BinOp(I&: Iv2Next)))) {
1394 Start = V1;
1395 IvNext = cast<Instruction>(Val: V2);
1396 return true;
1397 }
1398 return false;
1399 };
1400
1401 if (!MatchOuterIV(PN.getIncomingValue(i: 0), PN.getIncomingValue(i: 1)) &&
1402 !MatchOuterIV(PN.getIncomingValue(i: 1), PN.getIncomingValue(i: 0)))
1403 return nullptr;
1404
1405 PHINode *Iv2;
1406 Value *Iv2Start, *Iv2Step;
1407 if (!matchSimpleRecurrence(I: Iv2Next, P&: Iv2, Start&: Iv2Start, Step&: Iv2Step) ||
1408 Iv2->getParent() != BB)
1409 return nullptr;
1410
1411 auto *BO = dyn_cast<BinaryOperator>(Val: IvNext);
1412 Constant *Identity =
1413 BO ? ConstantExpr::getBinOpIdentity(Opcode: BO->getOpcode(), Ty: Iv2Start->getType())
1414 : Constant::getNullValue(Ty: Iv2Start->getType());
1415 if (Iv2Start != Identity)
1416 return nullptr;
1417
1418 Builder.SetInsertPoint(TheBB: &*BB, IP: BB->getFirstInsertionPt());
1419 if (!BO) {
1420 auto *GEP = cast<GEPOperator>(Val: IvNext);
1421 return Builder.CreateGEP(Ty: GEP->getSourceElementType(), Ptr: Start, IdxList: Iv2, Name: "",
1422 NW: cast<GEPOperator>(Val: IvNext)->getNoWrapFlags());
1423 }
1424
1425 assert(BO->isCommutative() && "Must be commutative");
1426 Value *Res = Builder.CreateBinOp(Opc: BO->getOpcode(), LHS: Iv2, RHS: Start);
1427 cast<Instruction>(Val: Res)->copyIRFlags(V: BO);
1428 return Res;
1429}
1430
1431// PHINode simplification
1432//
1433Instruction *InstCombinerImpl::visitPHINode(PHINode &PN) {
1434 if (Value *V = simplifyInstruction(I: &PN, Q: SQ.getWithInstruction(I: &PN)))
1435 return replaceInstUsesWith(I&: PN, V);
1436
1437 if (Instruction *Result = foldPHIArgZextsIntoPHI(Phi&: PN))
1438 return Result;
1439
1440 if (Instruction *Result = foldPHIArgIntToPtrToPHI(PN))
1441 return Result;
1442
1443 // If all PHI operands are the same operation, pull them through the PHI,
1444 // reducing code size.
1445 auto *Inst0 = dyn_cast<Instruction>(Val: PN.getIncomingValue(i: 0));
1446 auto *Inst1 = dyn_cast<Instruction>(Val: PN.getIncomingValue(i: 1));
1447 if (Inst0 && Inst1 && Inst0->getOpcode() == Inst1->getOpcode() &&
1448 Inst0->hasOneUser())
1449 if (Instruction *Result = foldPHIArgOpIntoPHI(PN))
1450 return Result;
1451
1452 // If the incoming values are pointer casts of the same original value,
1453 // replace the phi with a single cast iff we can insert a non-PHI instruction.
1454 if (PN.getType()->isPointerTy() && PN.getParent()->hasInsertionPt()) {
1455 Value *IV0 = PN.getIncomingValue(i: 0);
1456 Value *IV0Stripped = IV0->stripPointerCasts();
1457 // Set to keep track of values known to be equal to IV0Stripped after
1458 // stripping pointer casts.
1459 SmallPtrSet<Value *, 4> CheckedIVs;
1460 CheckedIVs.insert(Ptr: IV0);
1461 if (IV0 != IV0Stripped &&
1462 all_of(Range: PN.incoming_values(), P: [&CheckedIVs, IV0Stripped](Value *IV) {
1463 return !CheckedIVs.insert(Ptr: IV).second ||
1464 IV0Stripped == IV->stripPointerCasts();
1465 })) {
1466 return CastInst::CreatePointerCast(S: IV0Stripped, Ty: PN.getType());
1467 }
1468 }
1469
1470 if (foldDeadPhiWeb(PN))
1471 return nullptr;
1472
1473 // Optimization when the phi only has one use
1474 if (PN.hasOneUse()) {
1475 if (foldIntegerTypedPHI(PN))
1476 return nullptr;
1477
1478 // If this phi has a single use, and if that use just computes a value for
1479 // the next iteration of a loop, delete the phi. This occurs with unused
1480 // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this
1481 // common case here is good because the only other things that catch this
1482 // are induction variable analysis (sometimes) and ADCE, which is only run
1483 // late.
1484 Instruction *PHIUser = cast<Instruction>(Val: PN.user_back());
1485 if (PHIUser->hasOneUse() &&
1486 (isa<BinaryOperator>(Val: PHIUser) || isa<UnaryOperator>(Val: PHIUser) ||
1487 isa<GetElementPtrInst>(Val: PHIUser)) &&
1488 PHIUser->user_back() == &PN) {
1489 return replaceInstUsesWith(I&: PN, V: PoisonValue::get(T: PN.getType()));
1490 }
1491 }
1492
1493 // When a PHI is used only to be compared with zero, it is safe to replace
1494 // an incoming value proved as known nonzero with any non-zero constant.
1495 // For example, in the code below, the incoming value %v can be replaced
1496 // with any non-zero constant based on the fact that the PHI is only used to
1497 // be compared with zero and %v is a known non-zero value:
1498 // %v = select %cond, 1, 2
1499 // %p = phi [%v, BB] ...
1500 // icmp eq, %p, 0
1501 // FIXME: To be simple, handle only integer type for now.
1502 // This handles a small number of uses to keep the complexity down, and an
1503 // icmp(or(phi)) can equally be replaced with any non-zero constant as the
1504 // "or" will only add bits.
1505 if (!PN.hasNUsesOrMore(N: 3)) {
1506 SmallVector<Instruction *> DropPoisonFlags;
1507 bool AllUsesOfPhiEndsInCmp = all_of(Range: PN.users(), P: [&](User *U) {
1508 auto *CmpInst = dyn_cast<ICmpInst>(Val: U);
1509 if (!CmpInst) {
1510 // This is always correct as OR only add bits and we are checking
1511 // against 0.
1512 if (U->hasOneUse() && match(V: U, P: m_c_Or(L: m_Specific(V: &PN), R: m_Value()))) {
1513 DropPoisonFlags.push_back(Elt: cast<Instruction>(Val: U));
1514 CmpInst = dyn_cast<ICmpInst>(Val: U->user_back());
1515 }
1516 }
1517 if (!CmpInst || !isa<IntegerType>(Val: PN.getType()) ||
1518 !CmpInst->isEquality() || !match(V: CmpInst->getOperand(i_nocapture: 1), P: m_Zero())) {
1519 return false;
1520 }
1521 return true;
1522 });
1523 // All uses of PHI results in a compare with zero.
1524 if (AllUsesOfPhiEndsInCmp) {
1525 ConstantInt *NonZeroConst = nullptr;
1526 bool MadeChange = false;
1527 for (unsigned I = 0, E = PN.getNumIncomingValues(); I != E; ++I) {
1528 Instruction *CtxI = PN.getIncomingBlock(i: I)->getTerminator();
1529 Value *VA = PN.getIncomingValue(i: I);
1530 if (isKnownNonZero(V: VA, Q: getSimplifyQuery().getWithInstruction(I: CtxI))) {
1531 if (!NonZeroConst)
1532 NonZeroConst = getAnyNonZeroConstInt(PN);
1533 if (NonZeroConst != VA) {
1534 replaceOperand(I&: PN, OpNum: I, V: NonZeroConst);
1535 // The "disjoint" flag may no longer hold after the transform.
1536 for (Instruction *I : DropPoisonFlags)
1537 I->dropPoisonGeneratingFlags();
1538 MadeChange = true;
1539 }
1540 }
1541 }
1542 if (MadeChange)
1543 return &PN;
1544 }
1545 }
1546
1547 // We sometimes end up with phi cycles that non-obviously end up being the
1548 // same value, for example:
1549 // z = some value; x = phi (y, z); y = phi (x, z)
1550 // where the phi nodes don't necessarily need to be in the same block. Do a
1551 // quick check to see if the PHI node only contains a single non-phi value, if
1552 // so, scan to see if the phi cycle is actually equal to that value. If the
1553 // phi has no non-phi values then allow the "NonPhiInVal" to be set later if
1554 // one of the phis itself does not have a single input.
1555 {
1556 unsigned InValNo = 0, NumIncomingVals = PN.getNumIncomingValues();
1557 // Scan for the first non-phi operand.
1558 while (InValNo != NumIncomingVals &&
1559 isa<PHINode>(Val: PN.getIncomingValue(i: InValNo)))
1560 ++InValNo;
1561
1562 Value *NonPhiInVal =
1563 InValNo != NumIncomingVals ? PN.getIncomingValue(i: InValNo) : nullptr;
1564
1565 // Scan the rest of the operands to see if there are any conflicts, if so
1566 // there is no need to recursively scan other phis.
1567 if (NonPhiInVal)
1568 for (++InValNo; InValNo != NumIncomingVals; ++InValNo) {
1569 Value *OpVal = PN.getIncomingValue(i: InValNo);
1570 if (OpVal != NonPhiInVal && !isa<PHINode>(Val: OpVal))
1571 break;
1572 }
1573
1574 // If we scanned over all operands, then we have one unique value plus
1575 // phi values. Scan PHI nodes to see if they all merge in each other or
1576 // the value.
1577 if (InValNo == NumIncomingVals) {
1578 SmallPtrSet<PHINode *, 16> ValueEqualPHIs;
1579 if (PHIsEqualValue(PN: &PN, NonPhiInVal, ValueEqualPHIs))
1580 return replaceInstUsesWith(I&: PN, V: NonPhiInVal);
1581 }
1582 }
1583
1584 // If there are multiple PHIs, sort their operands so that they all list
1585 // the blocks in the same order. This will help identical PHIs be eliminated
1586 // by other passes. Other passes shouldn't depend on this for correctness
1587 // however.
1588 auto Res = PredOrder.try_emplace(Key: PN.getParent());
1589 if (!Res.second) {
1590 const auto &Preds = Res.first->second;
1591 for (unsigned I = 0, E = PN.getNumIncomingValues(); I != E; ++I) {
1592 BasicBlock *BBA = PN.getIncomingBlock(i: I);
1593 BasicBlock *BBB = Preds[I];
1594 if (BBA != BBB) {
1595 Value *VA = PN.getIncomingValue(i: I);
1596 unsigned J = PN.getBasicBlockIndex(BB: BBB);
1597 Value *VB = PN.getIncomingValue(i: J);
1598 PN.setIncomingBlock(i: I, BB: BBB);
1599 PN.setIncomingValue(i: I, V: VB);
1600 PN.setIncomingBlock(i: J, BB: BBA);
1601 PN.setIncomingValue(i: J, V: VA);
1602 // NOTE: Instcombine normally would want us to "return &PN" if we
1603 // modified any of the operands of an instruction. However, since we
1604 // aren't adding or removing uses (just rearranging them) we don't do
1605 // this in this case.
1606 }
1607 }
1608 } else {
1609 // Remember the block order of the first encountered phi node.
1610 append_range(C&: Res.first->second, R: PN.blocks());
1611 }
1612
1613 // Is there an identical PHI node in this basic block?
1614 for (PHINode &IdenticalPN : PN.getParent()->phis()) {
1615 // Ignore the PHI node itself.
1616 if (&IdenticalPN == &PN)
1617 continue;
1618 // Note that even though we've just canonicalized this PHI, due to the
1619 // worklist visitation order, there are no guarantess that *every* PHI
1620 // has been canonicalized, so we can't just compare operands ranges.
1621 if (!PN.isIdenticalToWhenDefined(I: &IdenticalPN))
1622 continue;
1623 // Just use that PHI instead then.
1624 ++NumPHICSEs;
1625 return replaceInstUsesWith(I&: PN, V: &IdenticalPN);
1626 }
1627
1628 // If this is an integer PHI and we know that it has an illegal type, see if
1629 // it is only used by trunc or trunc(lshr) operations. If so, we split the
1630 // PHI into the various pieces being extracted. This sort of thing is
1631 // introduced when SROA promotes an aggregate to a single large integer type.
1632 if (PN.getType()->isIntegerTy() &&
1633 !DL.isLegalInteger(Width: PN.getType()->getPrimitiveSizeInBits()))
1634 if (Instruction *Res = SliceUpIllegalIntegerPHI(FirstPhi&: PN))
1635 return Res;
1636
1637 // Ultimately, try to replace this Phi with a dominating condition.
1638 if (auto *V = simplifyUsingControlFlow(Self&: *this, PN, DT))
1639 return replaceInstUsesWith(I&: PN, V);
1640
1641 if (Value *Res = foldDependentIVs(PN, Builder))
1642 return replaceInstUsesWith(I&: PN, V: Res);
1643
1644 return nullptr;
1645}
1646