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