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 // FIXME: https://github.com/llvm/llvm-project/issues/121495
792 // Call combineMetadataForCSE instead, so that an explicit set of KnownIDs
793 // doesn't need to be maintained here.
794 combineMetadata(NewLI, LI, KnownIDs, true);
795 Value *NewInVal = LI->getOperand(0);
796 if (NewInVal != InVal)
797 InVal = nullptr;
798 NewPN->addIncoming(NewInVal, BB);
799 }
800
801 if (InVal) {
802 // The new PHI unions all of the same values together. This is really
803 // common, so we handle it intelligently here for compile-time speed.
804 NewLI->setOperand(0, InVal);
805 delete NewPN;
806 } else {
807 InsertNewInstBefore(NewPN, PN.getIterator());
808 }
809
810 // If this was a volatile load that we are merging, make sure to loop through
811 // and mark all the input loads as non-volatile. If we don't do this, we will
812 // insert a new volatile load and the old ones will not be deletable.
813 if (IsVolatile)
814 for (Value *IncValue : PN.incoming_values())
815 cast<LoadInst>(IncValue)->setVolatile(false);
816
817 PHIArgMergedDebugLoc(NewLI, PN);
818 return NewLI;
819}
820
821/// TODO: This function could handle other cast types, but then it might
822/// require special-casing a cast from the 'i1' type. See the comment in
823/// FoldPHIArgOpIntoPHI() about pessimizing illegal integer types.
825 // We cannot create a new instruction after the PHI if the terminator is an
826 // EHPad because there is no valid insertion point.
827 if (Instruction *TI = Phi.getParent()->getTerminator())
828 if (TI->isEHPad())
829 return nullptr;
830
831 // Early exit for the common case of a phi with two operands. These are
832 // handled elsewhere. See the comment below where we check the count of zexts
833 // and constants for more details.
834 unsigned NumIncomingValues = Phi.getNumIncomingValues();
835 if (NumIncomingValues < 3)
836 return nullptr;
837
838 // Find the narrower type specified by the first zext.
839 Type *NarrowType = nullptr;
840 for (Value *V : Phi.incoming_values()) {
841 if (auto *Zext = dyn_cast<ZExtInst>(V)) {
842 NarrowType = Zext->getSrcTy();
843 break;
844 }
845 }
846 if (!NarrowType)
847 return nullptr;
848
849 // Walk the phi operands checking that we only have zexts or constants that
850 // we can shrink for free. Store the new operands for the new phi.
851 SmallVector<Value *, 4> NewIncoming;
852 unsigned NumZexts = 0;
853 unsigned NumConsts = 0;
854 for (Value *V : Phi.incoming_values()) {
855 if (auto *Zext = dyn_cast<ZExtInst>(V)) {
856 // All zexts must be identical and have one user.
857 if (Zext->getSrcTy() != NarrowType || !Zext->hasOneUser())
858 return nullptr;
859 NewIncoming.push_back(Zext->getOperand(0));
860 NumZexts++;
861 } else if (auto *C = dyn_cast<Constant>(V)) {
862 // Make sure that constants can fit in the new type.
863 Constant *Trunc = getLosslessUnsignedTrunc(C, NarrowType);
864 if (!Trunc)
865 return nullptr;
866 NewIncoming.push_back(Trunc);
867 NumConsts++;
868 } else {
869 // If it's not a cast or a constant, bail out.
870 return nullptr;
871 }
872 }
873
874 // The more common cases of a phi with no constant operands or just one
875 // variable operand are handled by FoldPHIArgOpIntoPHI() and foldOpIntoPhi()
876 // respectively. foldOpIntoPhi() wants to do the opposite transform that is
877 // performed here. It tries to replicate a cast in the phi operand's basic
878 // block to expose other folding opportunities. Thus, InstCombine will
879 // infinite loop without this check.
880 if (NumConsts == 0 || NumZexts < 2)
881 return nullptr;
882
883 // All incoming values are zexts or constants that are safe to truncate.
884 // Create a new phi node of the narrow type, phi together all of the new
885 // operands, and zext the result back to the original type.
886 PHINode *NewPhi = PHINode::Create(NarrowType, NumIncomingValues,
887 Phi.getName() + ".shrunk");
888 for (unsigned I = 0; I != NumIncomingValues; ++I)
889 NewPhi->addIncoming(NewIncoming[I], Phi.getIncomingBlock(I));
890
891 InsertNewInstBefore(NewPhi, Phi.getIterator());
892 return CastInst::CreateZExtOrBitCast(NewPhi, Phi.getType());
893}
894
895/// If all operands to a PHI node are the same "unary" operator and they all are
896/// only used by the PHI, PHI together their inputs, and do the operation once,
897/// to the result of the PHI.
899 // We cannot create a new instruction after the PHI if the terminator is an
900 // EHPad because there is no valid insertion point.
901 if (Instruction *TI = PN.getParent()->getTerminator())
902 if (TI->isEHPad())
903 return nullptr;
904
905 Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0));
906
907 if (isa<GetElementPtrInst>(FirstInst))
908 return foldPHIArgGEPIntoPHI(PN);
909 if (isa<LoadInst>(FirstInst))
910 return foldPHIArgLoadIntoPHI(PN);
911 if (isa<InsertValueInst>(FirstInst))
913 if (isa<ExtractValueInst>(FirstInst))
915
916 // Scan the instruction, looking for input operations that can be folded away.
917 // If all input operands to the phi are the same instruction (e.g. a cast from
918 // the same type or "+42") we can pull the operation through the PHI, reducing
919 // code size and simplifying code.
920 Constant *ConstantOp = nullptr;
921 Type *CastSrcTy = nullptr;
922
923 if (isa<CastInst>(FirstInst)) {
924 CastSrcTy = FirstInst->getOperand(0)->getType();
925
926 // Be careful about transforming integer PHIs. We don't want to pessimize
927 // the code by turning an i32 into an i1293.
928 if (PN.getType()->isIntegerTy() && CastSrcTy->isIntegerTy()) {
929 if (!shouldChangeType(PN.getType(), CastSrcTy))
930 return nullptr;
931 }
932 } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) {
933 // Can fold binop, compare or shift here if the RHS is a constant,
934 // otherwise call FoldPHIArgBinOpIntoPHI.
935 ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1));
936 if (!ConstantOp)
937 return foldPHIArgBinOpIntoPHI(PN);
938 } else {
939 return nullptr; // Cannot fold this operation.
940 }
941
942 // Check to see if all arguments are the same operation.
943 for (Value *V : drop_begin(PN.incoming_values())) {
944 Instruction *I = dyn_cast<Instruction>(V);
945 if (!I || !I->hasOneUser() || !I->isSameOperationAs(FirstInst))
946 return nullptr;
947 if (CastSrcTy) {
948 if (I->getOperand(0)->getType() != CastSrcTy)
949 return nullptr; // Cast operation must match.
950 } else if (I->getOperand(1) != ConstantOp) {
951 return nullptr;
952 }
953 }
954
955 // Okay, they are all the same operation. Create a new PHI node of the
956 // correct type, and PHI together all of the LHS's of the instructions.
957 PHINode *NewPN = PHINode::Create(FirstInst->getOperand(0)->getType(),
959 PN.getName()+".in");
960
961 Value *InVal = FirstInst->getOperand(0);
962 NewPN->addIncoming(InVal, PN.getIncomingBlock(0));
963
964 // Add all operands to the new PHI.
965 for (auto Incoming : drop_begin(zip(PN.blocks(), PN.incoming_values()))) {
966 BasicBlock *BB = std::get<0>(Incoming);
967 Value *V = std::get<1>(Incoming);
968 Value *NewInVal = cast<Instruction>(V)->getOperand(0);
969 if (NewInVal != InVal)
970 InVal = nullptr;
971 NewPN->addIncoming(NewInVal, BB);
972 }
973
974 Value *PhiVal;
975 if (InVal) {
976 // The new PHI unions all of the same values together. This is really
977 // common, so we handle it intelligently here for compile-time speed.
978 PhiVal = InVal;
979 delete NewPN;
980 } else {
981 InsertNewInstBefore(NewPN, PN.getIterator());
982 PhiVal = NewPN;
983 }
984
985 // Insert and return the new operation.
986 if (CastInst *FirstCI = dyn_cast<CastInst>(FirstInst)) {
987 CastInst *NewCI = CastInst::Create(FirstCI->getOpcode(), PhiVal,
988 PN.getType());
989 PHIArgMergedDebugLoc(NewCI, PN);
990 return NewCI;
991 }
992
993 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) {
994 BinOp = BinaryOperator::Create(BinOp->getOpcode(), PhiVal, ConstantOp);
995 BinOp->copyIRFlags(PN.getIncomingValue(0));
996
997 for (Value *V : drop_begin(PN.incoming_values()))
998 BinOp->andIRFlags(V);
999
1000 PHIArgMergedDebugLoc(BinOp, PN);
1001 return BinOp;
1002 }
1003
1004 CmpInst *CIOp = cast<CmpInst>(FirstInst);
1005 CmpInst *NewCI = CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(),
1006 PhiVal, ConstantOp);
1007 PHIArgMergedDebugLoc(NewCI, PN);
1008 return NewCI;
1009}
1010
1011/// Return true if this phi node is always equal to NonPhiInVal.
1012/// This happens with mutually cyclic phi nodes like:
1013/// z = some value; x = phi (y, z); y = phi (x, z)
1014static bool PHIsEqualValue(PHINode *PN, Value *&NonPhiInVal,
1015 SmallPtrSetImpl<PHINode *> &ValueEqualPHIs) {
1016 // See if we already saw this PHI node.
1017 if (!ValueEqualPHIs.insert(PN).second)
1018 return true;
1019
1020 // Don't scan crazily complex things.
1021 if (ValueEqualPHIs.size() == 16)
1022 return false;
1023
1024 // Scan the operands to see if they are either phi nodes or are equal to
1025 // the value.
1026 for (Value *Op : PN->incoming_values()) {
1027 if (PHINode *OpPN = dyn_cast<PHINode>(Op)) {
1028 if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs)) {
1029 if (NonPhiInVal)
1030 return false;
1031 NonPhiInVal = OpPN;
1032 }
1033 } else if (Op != NonPhiInVal)
1034 return false;
1035 }
1036
1037 return true;
1038}
1039
1040/// Return an existing non-zero constant if this phi node has one, otherwise
1041/// return constant 1.
1043 assert(isa<IntegerType>(PN.getType()) && "Expect only integer type phi");
1044 for (Value *V : PN.operands())
1045 if (auto *ConstVA = dyn_cast<ConstantInt>(V))
1046 if (!ConstVA->isZero())
1047 return ConstVA;
1048 return ConstantInt::get(cast<IntegerType>(PN.getType()), 1);
1049}
1050
1051namespace {
1052struct PHIUsageRecord {
1053 unsigned PHIId; // The ID # of the PHI (something determinstic to sort on)
1054 unsigned Shift; // The amount shifted.
1055 Instruction *Inst; // The trunc instruction.
1056
1057 PHIUsageRecord(unsigned Pn, unsigned Sh, Instruction *User)
1058 : PHIId(Pn), Shift(Sh), Inst(User) {}
1059
1060 bool operator<(const PHIUsageRecord &RHS) const {
1061 if (PHIId < RHS.PHIId) return true;
1062 if (PHIId > RHS.PHIId) return false;
1063 if (Shift < RHS.Shift) return true;
1064 if (Shift > RHS.Shift) return false;
1065 return Inst->getType()->getPrimitiveSizeInBits() <
1067 }
1068};
1069
1070struct LoweredPHIRecord {
1071 PHINode *PN; // The PHI that was lowered.
1072 unsigned Shift; // The amount shifted.
1073 unsigned Width; // The width extracted.
1074
1075 LoweredPHIRecord(PHINode *Phi, unsigned Sh, Type *Ty)
1076 : PN(Phi), Shift(Sh), Width(Ty->getPrimitiveSizeInBits()) {}
1077
1078 // Ctor form used by DenseMap.
1079 LoweredPHIRecord(PHINode *Phi, unsigned Sh) : PN(Phi), Shift(Sh), Width(0) {}
1080};
1081} // namespace
1082
1083namespace llvm {
1084 template<>
1085 struct DenseMapInfo<LoweredPHIRecord> {
1086 static inline LoweredPHIRecord getEmptyKey() {
1087 return LoweredPHIRecord(nullptr, 0);
1088 }
1089 static inline LoweredPHIRecord getTombstoneKey() {
1090 return LoweredPHIRecord(nullptr, 1);
1091 }
1092 static unsigned getHashValue(const LoweredPHIRecord &Val) {
1093 return DenseMapInfo<PHINode*>::getHashValue(Val.PN) ^ (Val.Shift>>3) ^
1094 (Val.Width>>3);
1095 }
1096 static bool isEqual(const LoweredPHIRecord &LHS,
1097 const LoweredPHIRecord &RHS) {
1098 return LHS.PN == RHS.PN && LHS.Shift == RHS.Shift &&
1099 LHS.Width == RHS.Width;
1100 }
1101 };
1102} // namespace llvm
1103
1104
1105/// This is an integer PHI and we know that it has an illegal type: see if it is
1106/// only used by trunc or trunc(lshr) operations. If so, we split the PHI into
1107/// the various pieces being extracted. This sort of thing is introduced when
1108/// SROA promotes an aggregate to large integer values.
1109///
1110/// TODO: The user of the trunc may be an bitcast to float/double/vector or an
1111/// inttoptr. We should produce new PHIs in the right type.
1112///
1114 // PHIUsers - Keep track of all of the truncated values extracted from a set
1115 // of PHIs, along with their offset. These are the things we want to rewrite.
1117
1118 // PHIs are often mutually cyclic, so we keep track of a whole set of PHI
1119 // nodes which are extracted from. PHIsToSlice is a set we use to avoid
1120 // revisiting PHIs, PHIsInspected is a ordered list of PHIs that we need to
1121 // check the uses of (to ensure they are all extracts).
1122 SmallVector<PHINode*, 8> PHIsToSlice;
1123 SmallPtrSet<PHINode*, 8> PHIsInspected;
1124
1125 PHIsToSlice.push_back(&FirstPhi);
1126 PHIsInspected.insert(&FirstPhi);
1127
1128 for (unsigned PHIId = 0; PHIId != PHIsToSlice.size(); ++PHIId) {
1129 PHINode *PN = PHIsToSlice[PHIId];
1130
1131 // Scan the input list of the PHI. If any input is an invoke, and if the
1132 // input is defined in the predecessor, then we won't be split the critical
1133 // edge which is required to insert a truncate. Because of this, we have to
1134 // bail out.
1135 for (auto Incoming : zip(PN->blocks(), PN->incoming_values())) {
1136 BasicBlock *BB = std::get<0>(Incoming);
1137 Value *V = std::get<1>(Incoming);
1138 InvokeInst *II = dyn_cast<InvokeInst>(V);
1139 if (!II)
1140 continue;
1141 if (II->getParent() != BB)
1142 continue;
1143
1144 // If we have a phi, and if it's directly in the predecessor, then we have
1145 // a critical edge where we need to put the truncate. Since we can't
1146 // split the edge in instcombine, we have to bail out.
1147 return nullptr;
1148 }
1149
1150 // If the incoming value is a PHI node before a catchswitch, we cannot
1151 // extract the value within that BB because we cannot insert any non-PHI
1152 // instructions in the BB.
1153 for (auto *Pred : PN->blocks())
1154 if (Pred->getFirstInsertionPt() == Pred->end())
1155 return nullptr;
1156
1157 for (User *U : PN->users()) {
1158 Instruction *UserI = cast<Instruction>(U);
1159
1160 // If the user is a PHI, inspect its uses recursively.
1161 if (PHINode *UserPN = dyn_cast<PHINode>(UserI)) {
1162 if (PHIsInspected.insert(UserPN).second)
1163 PHIsToSlice.push_back(UserPN);
1164 continue;
1165 }
1166
1167 // Truncates are always ok.
1168 if (isa<TruncInst>(UserI)) {
1169 PHIUsers.push_back(PHIUsageRecord(PHIId, 0, UserI));
1170 continue;
1171 }
1172
1173 // Otherwise it must be a lshr which can only be used by one trunc.
1174 if (UserI->getOpcode() != Instruction::LShr ||
1175 !UserI->hasOneUse() || !isa<TruncInst>(UserI->user_back()) ||
1176 !isa<ConstantInt>(UserI->getOperand(1)))
1177 return nullptr;
1178
1179 // Bail on out of range shifts.
1180 unsigned SizeInBits = UserI->getType()->getScalarSizeInBits();
1181 if (cast<ConstantInt>(UserI->getOperand(1))->getValue().uge(SizeInBits))
1182 return nullptr;
1183
1184 unsigned Shift = cast<ConstantInt>(UserI->getOperand(1))->getZExtValue();
1185 PHIUsers.push_back(PHIUsageRecord(PHIId, Shift, UserI->user_back()));
1186 }
1187 }
1188
1189 // If we have no users, they must be all self uses, just nuke the PHI.
1190 if (PHIUsers.empty())
1191 return replaceInstUsesWith(FirstPhi, PoisonValue::get(FirstPhi.getType()));
1192
1193 // If this phi node is transformable, create new PHIs for all the pieces
1194 // extracted out of it. First, sort the users by their offset and size.
1195 array_pod_sort(PHIUsers.begin(), PHIUsers.end());
1196
1197 LLVM_DEBUG(dbgs() << "SLICING UP PHI: " << FirstPhi << '\n';
1198 for (unsigned I = 1; I != PHIsToSlice.size(); ++I) dbgs()
1199 << "AND USER PHI #" << I << ": " << *PHIsToSlice[I] << '\n');
1200
1201 // PredValues - This is a temporary used when rewriting PHI nodes. It is
1202 // hoisted out here to avoid construction/destruction thrashing.
1204
1205 // ExtractedVals - Each new PHI we introduce is saved here so we don't
1206 // introduce redundant PHIs.
1208
1209 for (unsigned UserI = 0, UserE = PHIUsers.size(); UserI != UserE; ++UserI) {
1210 unsigned PHIId = PHIUsers[UserI].PHIId;
1211 PHINode *PN = PHIsToSlice[PHIId];
1212 unsigned Offset = PHIUsers[UserI].Shift;
1213 Type *Ty = PHIUsers[UserI].Inst->getType();
1214
1215 PHINode *EltPHI;
1216
1217 // If we've already lowered a user like this, reuse the previously lowered
1218 // value.
1219 if ((EltPHI = ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)]) == nullptr) {
1220
1221 // Otherwise, Create the new PHI node for this user.
1222 EltPHI = PHINode::Create(Ty, PN->getNumIncomingValues(),
1223 PN->getName() + ".off" + Twine(Offset),
1224 PN->getIterator());
1225 assert(EltPHI->getType() != PN->getType() &&
1226 "Truncate didn't shrink phi?");
1227
1228 for (auto Incoming : zip(PN->blocks(), PN->incoming_values())) {
1229 BasicBlock *Pred = std::get<0>(Incoming);
1230 Value *InVal = std::get<1>(Incoming);
1231 Value *&PredVal = PredValues[Pred];
1232
1233 // If we already have a value for this predecessor, reuse it.
1234 if (PredVal) {
1235 EltPHI->addIncoming(PredVal, Pred);
1236 continue;
1237 }
1238
1239 // Handle the PHI self-reuse case.
1240 if (InVal == PN) {
1241 PredVal = EltPHI;
1242 EltPHI->addIncoming(PredVal, Pred);
1243 continue;
1244 }
1245
1246 if (PHINode *InPHI = dyn_cast<PHINode>(PN)) {
1247 // If the incoming value was a PHI, and if it was one of the PHIs we
1248 // already rewrote it, just use the lowered value.
1249 if (Value *Res = ExtractedVals[LoweredPHIRecord(InPHI, Offset, Ty)]) {
1250 PredVal = Res;
1251 EltPHI->addIncoming(PredVal, Pred);
1252 continue;
1253 }
1254 }
1255
1256 // Otherwise, do an extract in the predecessor.
1258 Value *Res = InVal;
1259 if (Offset)
1260 Res = Builder.CreateLShr(
1261 Res, ConstantInt::get(InVal->getType(), Offset), "extract");
1262 Res = Builder.CreateTrunc(Res, Ty, "extract.t");
1263 PredVal = Res;
1264 EltPHI->addIncoming(Res, Pred);
1265
1266 // If the incoming value was a PHI, and if it was one of the PHIs we are
1267 // rewriting, we will ultimately delete the code we inserted. This
1268 // means we need to revisit that PHI to make sure we extract out the
1269 // needed piece.
1270 if (PHINode *OldInVal = dyn_cast<PHINode>(InVal))
1271 if (PHIsInspected.count(OldInVal)) {
1272 unsigned RefPHIId =
1273 find(PHIsToSlice, OldInVal) - PHIsToSlice.begin();
1274 PHIUsers.push_back(
1275 PHIUsageRecord(RefPHIId, Offset, cast<Instruction>(Res)));
1276 ++UserE;
1277 }
1278 }
1279 PredValues.clear();
1280
1281 LLVM_DEBUG(dbgs() << " Made element PHI for offset " << Offset << ": "
1282 << *EltPHI << '\n');
1283 ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)] = EltPHI;
1284 }
1285
1286 // Replace the use of this piece with the PHI node.
1287 replaceInstUsesWith(*PHIUsers[UserI].Inst, EltPHI);
1288 }
1289
1290 // Replace all the remaining uses of the PHI nodes (self uses and the lshrs)
1291 // with poison.
1292 Value *Poison = PoisonValue::get(FirstPhi.getType());
1293 for (PHINode *PHI : drop_begin(PHIsToSlice))
1295 return replaceInstUsesWith(FirstPhi, Poison);
1296}
1297
1299 const DominatorTree &DT) {
1300 // Simplify the following patterns:
1301 // if (cond)
1302 // / \
1303 // ... ...
1304 // \ /
1305 // phi [true] [false]
1306 // and
1307 // switch (cond)
1308 // case v1: / \ case v2:
1309 // ... ...
1310 // \ /
1311 // phi [v1] [v2]
1312 // Make sure all inputs are constants.
1313 if (!all_of(PN.operands(), [](Value *V) { return isa<ConstantInt>(V); }))
1314 return nullptr;
1315
1316 BasicBlock *BB = PN.getParent();
1317 // Do not bother with unreachable instructions.
1318 if (!DT.isReachableFromEntry(BB))
1319 return nullptr;
1320
1321 // Determine which value the condition of the idom has for which successor.
1322 LLVMContext &Context = PN.getContext();
1323 auto *IDom = DT.getNode(BB)->getIDom()->getBlock();
1324 Value *Cond;
1327 auto AddSucc = [&](ConstantInt *C, BasicBlock *Succ) {
1328 SuccForValue[C] = Succ;
1329 ++SuccCount[Succ];
1330 };
1331 if (auto *BI = dyn_cast<BranchInst>(IDom->getTerminator())) {
1332 if (BI->isUnconditional())
1333 return nullptr;
1334
1335 Cond = BI->getCondition();
1336 AddSucc(ConstantInt::getTrue(Context), BI->getSuccessor(0));
1337 AddSucc(ConstantInt::getFalse(Context), BI->getSuccessor(1));
1338 } else if (auto *SI = dyn_cast<SwitchInst>(IDom->getTerminator())) {
1339 Cond = SI->getCondition();
1340 ++SuccCount[SI->getDefaultDest()];
1341 for (auto Case : SI->cases())
1342 AddSucc(Case.getCaseValue(), Case.getCaseSuccessor());
1343 } else {
1344 return nullptr;
1345 }
1346
1347 if (Cond->getType() != PN.getType())
1348 return nullptr;
1349
1350 // Check that edges outgoing from the idom's terminators dominate respective
1351 // inputs of the Phi.
1352 std::optional<bool> Invert;
1353 for (auto Pair : zip(PN.incoming_values(), PN.blocks())) {
1354 auto *Input = cast<ConstantInt>(std::get<0>(Pair));
1355 BasicBlock *Pred = std::get<1>(Pair);
1356 auto IsCorrectInput = [&](ConstantInt *Input) {
1357 // The input needs to be dominated by the corresponding edge of the idom.
1358 // This edge cannot be a multi-edge, as that would imply that multiple
1359 // different condition values follow the same edge.
1360 auto It = SuccForValue.find(Input);
1361 return It != SuccForValue.end() && SuccCount[It->second] == 1 &&
1362 DT.dominates(BasicBlockEdge(IDom, It->second),
1363 BasicBlockEdge(Pred, BB));
1364 };
1365
1366 // Depending on the constant, the condition may need to be inverted.
1367 bool NeedsInvert;
1368 if (IsCorrectInput(Input))
1369 NeedsInvert = false;
1370 else if (IsCorrectInput(cast<ConstantInt>(ConstantExpr::getNot(Input))))
1371 NeedsInvert = true;
1372 else
1373 return nullptr;
1374
1375 // Make sure the inversion requirement is always the same.
1376 if (Invert && *Invert != NeedsInvert)
1377 return nullptr;
1378
1379 Invert = NeedsInvert;
1380 }
1381
1382 if (!*Invert)
1383 return Cond;
1384
1385 // This Phi is actually opposite to branching condition of IDom. We invert
1386 // the condition that will potentially open up some opportunities for
1387 // sinking.
1388 auto InsertPt = BB->getFirstInsertionPt();
1389 if (InsertPt != BB->end()) {
1390 Self.Builder.SetInsertPoint(&*BB, InsertPt);
1391 return Self.Builder.CreateNot(Cond);
1392 }
1393
1394 return nullptr;
1395}
1396
1397// Fold iv = phi(start, iv.next = iv2.next op start)
1398// where iv2 = phi(iv2.start, iv2.next = iv2 + iv2.step)
1399// and iv2.start op start = start
1400// to iv = iv2 op start
1402 BasicBlock *BB = PN.getParent();
1403 if (PN.getNumIncomingValues() != 2)
1404 return nullptr;
1405
1406 Value *Start;
1407 Instruction *IvNext;
1408 BinaryOperator *Iv2Next;
1409 auto MatchOuterIV = [&](Value *V1, Value *V2) {
1410 if (match(V2, m_c_BinOp(m_Specific(V1), m_BinOp(Iv2Next))) ||
1411 match(V2, m_GEP(m_Specific(V1), m_BinOp(Iv2Next)))) {
1412 Start = V1;
1413 IvNext = cast<Instruction>(V2);
1414 return true;
1415 }
1416 return false;
1417 };
1418
1419 if (!MatchOuterIV(PN.getIncomingValue(0), PN.getIncomingValue(1)) &&
1420 !MatchOuterIV(PN.getIncomingValue(1), PN.getIncomingValue(0)))
1421 return nullptr;
1422
1423 PHINode *Iv2;
1424 Value *Iv2Start, *Iv2Step;
1425 if (!matchSimpleRecurrence(Iv2Next, Iv2, Iv2Start, Iv2Step) ||
1426 Iv2->getParent() != BB)
1427 return nullptr;
1428
1429 auto *BO = dyn_cast<BinaryOperator>(IvNext);
1430 Constant *Identity =
1431 BO ? ConstantExpr::getBinOpIdentity(BO->getOpcode(), Iv2Start->getType())
1432 : Constant::getNullValue(Iv2Start->getType());
1433 if (Iv2Start != Identity)
1434 return nullptr;
1435
1436 Builder.SetInsertPoint(&*BB, BB->getFirstInsertionPt());
1437 if (!BO) {
1438 auto *GEP = cast<GEPOperator>(IvNext);
1439 return Builder.CreateGEP(GEP->getSourceElementType(), Start, Iv2, "",
1440 cast<GEPOperator>(IvNext)->getNoWrapFlags());
1441 }
1442
1443 assert(BO->isCommutative() && "Must be commutative");
1444 Value *Res = Builder.CreateBinOp(BO->getOpcode(), Iv2, Start);
1445 cast<Instruction>(Res)->copyIRFlags(BO);
1446 return Res;
1447}
1448
1449// PHINode simplification
1450//
1452 if (Value *V = simplifyInstruction(&PN, SQ.getWithInstruction(&PN)))
1453 return replaceInstUsesWith(PN, V);
1454
1455 if (Instruction *Result = foldPHIArgZextsIntoPHI(PN))
1456 return Result;
1457
1458 if (Instruction *Result = foldPHIArgIntToPtrToPHI(PN))
1459 return Result;
1460
1461 // If all PHI operands are the same operation, pull them through the PHI,
1462 // reducing code size.
1463 auto *Inst0 = dyn_cast<Instruction>(PN.getIncomingValue(0));
1464 auto *Inst1 = dyn_cast<Instruction>(PN.getIncomingValue(1));
1465 if (Inst0 && Inst1 && Inst0->getOpcode() == Inst1->getOpcode() &&
1466 Inst0->hasOneUser())
1467 if (Instruction *Result = foldPHIArgOpIntoPHI(PN))
1468 return Result;
1469
1470 // If the incoming values are pointer casts of the same original value,
1471 // replace the phi with a single cast iff we can insert a non-PHI instruction.
1472 if (PN.getType()->isPointerTy() &&
1473 PN.getParent()->getFirstInsertionPt() != PN.getParent()->end()) {
1474 Value *IV0 = PN.getIncomingValue(0);
1475 Value *IV0Stripped = IV0->stripPointerCasts();
1476 // Set to keep track of values known to be equal to IV0Stripped after
1477 // stripping pointer casts.
1478 SmallPtrSet<Value *, 4> CheckedIVs;
1479 CheckedIVs.insert(IV0);
1480 if (IV0 != IV0Stripped &&
1481 all_of(PN.incoming_values(), [&CheckedIVs, IV0Stripped](Value *IV) {
1482 return !CheckedIVs.insert(IV).second ||
1483 IV0Stripped == IV->stripPointerCasts();
1484 })) {
1485 return CastInst::CreatePointerCast(IV0Stripped, PN.getType());
1486 }
1487 }
1488
1489 if (foldDeadPhiWeb(PN))
1490 return nullptr;
1491
1492 // Optimization when the phi only has one use
1493 if (PN.hasOneUse()) {
1494 if (foldIntegerTypedPHI(PN))
1495 return nullptr;
1496
1497 // If this phi has a single use, and if that use just computes a value for
1498 // the next iteration of a loop, delete the phi. This occurs with unused
1499 // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this
1500 // common case here is good because the only other things that catch this
1501 // are induction variable analysis (sometimes) and ADCE, which is only run
1502 // late.
1503 Instruction *PHIUser = cast<Instruction>(PN.user_back());
1504 if (PHIUser->hasOneUse() &&
1505 (isa<BinaryOperator>(PHIUser) || isa<UnaryOperator>(PHIUser) ||
1506 isa<GetElementPtrInst>(PHIUser)) &&
1507 PHIUser->user_back() == &PN) {
1509 }
1510 }
1511
1512 // When a PHI is used only to be compared with zero, it is safe to replace
1513 // an incoming value proved as known nonzero with any non-zero constant.
1514 // For example, in the code below, the incoming value %v can be replaced
1515 // with any non-zero constant based on the fact that the PHI is only used to
1516 // be compared with zero and %v is a known non-zero value:
1517 // %v = select %cond, 1, 2
1518 // %p = phi [%v, BB] ...
1519 // icmp eq, %p, 0
1520 // FIXME: To be simple, handle only integer type for now.
1521 // This handles a small number of uses to keep the complexity down, and an
1522 // icmp(or(phi)) can equally be replaced with any non-zero constant as the
1523 // "or" will only add bits.
1524 if (!PN.hasNUsesOrMore(3)) {
1525 SmallVector<Instruction *> DropPoisonFlags;
1526 bool AllUsesOfPhiEndsInCmp = all_of(PN.users(), [&](User *U) {
1527 auto *CmpInst = dyn_cast<ICmpInst>(U);
1528 if (!CmpInst) {
1529 // This is always correct as OR only add bits and we are checking
1530 // against 0.
1531 if (U->hasOneUse() && match(U, m_c_Or(m_Specific(&PN), m_Value()))) {
1532 DropPoisonFlags.push_back(cast<Instruction>(U));
1533 CmpInst = dyn_cast<ICmpInst>(U->user_back());
1534 }
1535 }
1536 if (!CmpInst || !isa<IntegerType>(PN.getType()) ||
1537 !CmpInst->isEquality() || !match(CmpInst->getOperand(1), m_Zero())) {
1538 return false;
1539 }
1540 return true;
1541 });
1542 // All uses of PHI results in a compare with zero.
1543 if (AllUsesOfPhiEndsInCmp) {
1544 ConstantInt *NonZeroConst = nullptr;
1545 bool MadeChange = false;
1546 for (unsigned I = 0, E = PN.getNumIncomingValues(); I != E; ++I) {
1548 Value *VA = PN.getIncomingValue(I);
1549 if (isKnownNonZero(VA, getSimplifyQuery().getWithInstruction(CtxI))) {
1550 if (!NonZeroConst)
1551 NonZeroConst = getAnyNonZeroConstInt(PN);
1552 if (NonZeroConst != VA) {
1553 replaceOperand(PN, I, NonZeroConst);
1554 // The "disjoint" flag may no longer hold after the transform.
1555 for (Instruction *I : DropPoisonFlags)
1556 I->dropPoisonGeneratingFlags();
1557 MadeChange = true;
1558 }
1559 }
1560 }
1561 if (MadeChange)
1562 return &PN;
1563 }
1564 }
1565
1566 // We sometimes end up with phi cycles that non-obviously end up being the
1567 // same value, for example:
1568 // z = some value; x = phi (y, z); y = phi (x, z)
1569 // where the phi nodes don't necessarily need to be in the same block. Do a
1570 // quick check to see if the PHI node only contains a single non-phi value, if
1571 // so, scan to see if the phi cycle is actually equal to that value. If the
1572 // phi has no non-phi values then allow the "NonPhiInVal" to be set later if
1573 // one of the phis itself does not have a single input.
1574 {
1575 unsigned InValNo = 0, NumIncomingVals = PN.getNumIncomingValues();
1576 // Scan for the first non-phi operand.
1577 while (InValNo != NumIncomingVals &&
1578 isa<PHINode>(PN.getIncomingValue(InValNo)))
1579 ++InValNo;
1580
1581 Value *NonPhiInVal =
1582 InValNo != NumIncomingVals ? PN.getIncomingValue(InValNo) : nullptr;
1583
1584 // Scan the rest of the operands to see if there are any conflicts, if so
1585 // there is no need to recursively scan other phis.
1586 if (NonPhiInVal)
1587 for (++InValNo; InValNo != NumIncomingVals; ++InValNo) {
1588 Value *OpVal = PN.getIncomingValue(InValNo);
1589 if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal))
1590 break;
1591 }
1592
1593 // If we scanned over all operands, then we have one unique value plus
1594 // phi values. Scan PHI nodes to see if they all merge in each other or
1595 // the value.
1596 if (InValNo == NumIncomingVals) {
1597 SmallPtrSet<PHINode *, 16> ValueEqualPHIs;
1598 if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs))
1599 return replaceInstUsesWith(PN, NonPhiInVal);
1600 }
1601 }
1602
1603 // If there are multiple PHIs, sort their operands so that they all list
1604 // the blocks in the same order. This will help identical PHIs be eliminated
1605 // by other passes. Other passes shouldn't depend on this for correctness
1606 // however.
1607 auto Res = PredOrder.try_emplace(PN.getParent());
1608 if (!Res.second) {
1609 const auto &Preds = Res.first->second;
1610 for (unsigned I = 0, E = PN.getNumIncomingValues(); I != E; ++I) {
1611 BasicBlock *BBA = PN.getIncomingBlock(I);
1612 BasicBlock *BBB = Preds[I];
1613 if (BBA != BBB) {
1614 Value *VA = PN.getIncomingValue(I);
1615 unsigned J = PN.getBasicBlockIndex(BBB);
1616 Value *VB = PN.getIncomingValue(J);
1617 PN.setIncomingBlock(I, BBB);
1618 PN.setIncomingValue(I, VB);
1619 PN.setIncomingBlock(J, BBA);
1620 PN.setIncomingValue(J, VA);
1621 // NOTE: Instcombine normally would want us to "return &PN" if we
1622 // modified any of the operands of an instruction. However, since we
1623 // aren't adding or removing uses (just rearranging them) we don't do
1624 // this in this case.
1625 }
1626 }
1627 } else {
1628 // Remember the block order of the first encountered phi node.
1629 append_range(Res.first->second, PN.blocks());
1630 }
1631
1632 // Is there an identical PHI node in this basic block?
1633 for (PHINode &IdenticalPN : PN.getParent()->phis()) {
1634 // Ignore the PHI node itself.
1635 if (&IdenticalPN == &PN)
1636 continue;
1637 // Note that even though we've just canonicalized this PHI, due to the
1638 // worklist visitation order, there are no guarantess that *every* PHI
1639 // has been canonicalized, so we can't just compare operands ranges.
1640 if (!PN.isIdenticalToWhenDefined(&IdenticalPN))
1641 continue;
1642 // Just use that PHI instead then.
1643 ++NumPHICSEs;
1644 return replaceInstUsesWith(PN, &IdenticalPN);
1645 }
1646
1647 // If this is an integer PHI and we know that it has an illegal type, see if
1648 // it is only used by trunc or trunc(lshr) operations. If so, we split the
1649 // PHI into the various pieces being extracted. This sort of thing is
1650 // introduced when SROA promotes an aggregate to a single large integer type.
1651 if (PN.getType()->isIntegerTy() &&
1652 !DL.isLegalInteger(PN.getType()->getPrimitiveSizeInBits()))
1653 if (Instruction *Res = SliceUpIllegalIntegerPHI(PN))
1654 return Res;
1655
1656 // Ultimately, try to replace this Phi with a dominating condition.
1657 if (auto *V = simplifyUsingControlFlow(*this, PN, DT))
1658 return replaceInstUsesWith(PN, V);
1659
1660 if (Value *Res = foldDependentIVs(PN, Builder))
1661 return replaceInstUsesWith(PN, Res);
1662
1663 return nullptr;
1664}
@ 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:475
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:390
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:472
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)
DO NOT CALL EXTERNALLY.
Definition: Local.cpp:3314
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