1*9880d681SAndroid Build Coastguard Worker //===- InstCombinePHI.cpp -------------------------------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the visitPHINode function.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker #include "InstCombineInternal.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallPtrSet.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/InstructionSimplify.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ValueTracking.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/PatternMatch.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/Local.h"
21*9880d681SAndroid Build Coastguard Worker using namespace llvm;
22*9880d681SAndroid Build Coastguard Worker using namespace llvm::PatternMatch;
23*9880d681SAndroid Build Coastguard Worker
24*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "instcombine"
25*9880d681SAndroid Build Coastguard Worker
26*9880d681SAndroid Build Coastguard Worker /// If we have something like phi [add (a,b), add(a,c)] and if a/b/c and the
27*9880d681SAndroid Build Coastguard Worker /// adds all have a single use, turn this into a phi and a single binop.
FoldPHIArgBinOpIntoPHI(PHINode & PN)28*9880d681SAndroid Build Coastguard Worker Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) {
29*9880d681SAndroid Build Coastguard Worker Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0));
30*9880d681SAndroid Build Coastguard Worker assert(isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst));
31*9880d681SAndroid Build Coastguard Worker unsigned Opc = FirstInst->getOpcode();
32*9880d681SAndroid Build Coastguard Worker Value *LHSVal = FirstInst->getOperand(0);
33*9880d681SAndroid Build Coastguard Worker Value *RHSVal = FirstInst->getOperand(1);
34*9880d681SAndroid Build Coastguard Worker
35*9880d681SAndroid Build Coastguard Worker Type *LHSType = LHSVal->getType();
36*9880d681SAndroid Build Coastguard Worker Type *RHSType = RHSVal->getType();
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker // Scan to see if all operands are the same opcode, and all have one use.
39*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) {
40*9880d681SAndroid Build Coastguard Worker Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i));
41*9880d681SAndroid Build Coastguard Worker if (!I || I->getOpcode() != Opc || !I->hasOneUse() ||
42*9880d681SAndroid Build Coastguard Worker // Verify type of the LHS matches so we don't fold cmp's of different
43*9880d681SAndroid Build Coastguard Worker // types.
44*9880d681SAndroid Build Coastguard Worker I->getOperand(0)->getType() != LHSType ||
45*9880d681SAndroid Build Coastguard Worker I->getOperand(1)->getType() != RHSType)
46*9880d681SAndroid Build Coastguard Worker return nullptr;
47*9880d681SAndroid Build Coastguard Worker
48*9880d681SAndroid Build Coastguard Worker // If they are CmpInst instructions, check their predicates
49*9880d681SAndroid Build Coastguard Worker if (CmpInst *CI = dyn_cast<CmpInst>(I))
50*9880d681SAndroid Build Coastguard Worker if (CI->getPredicate() != cast<CmpInst>(FirstInst)->getPredicate())
51*9880d681SAndroid Build Coastguard Worker return nullptr;
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Worker // Keep track of which operand needs a phi node.
54*9880d681SAndroid Build Coastguard Worker if (I->getOperand(0) != LHSVal) LHSVal = nullptr;
55*9880d681SAndroid Build Coastguard Worker if (I->getOperand(1) != RHSVal) RHSVal = nullptr;
56*9880d681SAndroid Build Coastguard Worker }
57*9880d681SAndroid Build Coastguard Worker
58*9880d681SAndroid Build Coastguard Worker // If both LHS and RHS would need a PHI, don't do this transformation,
59*9880d681SAndroid Build Coastguard Worker // because it would increase the number of PHIs entering the block,
60*9880d681SAndroid Build Coastguard Worker // which leads to higher register pressure. This is especially
61*9880d681SAndroid Build Coastguard Worker // bad when the PHIs are in the header of a loop.
62*9880d681SAndroid Build Coastguard Worker if (!LHSVal && !RHSVal)
63*9880d681SAndroid Build Coastguard Worker return nullptr;
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard Worker // Otherwise, this is safe to transform!
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Worker Value *InLHS = FirstInst->getOperand(0);
68*9880d681SAndroid Build Coastguard Worker Value *InRHS = FirstInst->getOperand(1);
69*9880d681SAndroid Build Coastguard Worker PHINode *NewLHS = nullptr, *NewRHS = nullptr;
70*9880d681SAndroid Build Coastguard Worker if (!LHSVal) {
71*9880d681SAndroid Build Coastguard Worker NewLHS = PHINode::Create(LHSType, PN.getNumIncomingValues(),
72*9880d681SAndroid Build Coastguard Worker FirstInst->getOperand(0)->getName() + ".pn");
73*9880d681SAndroid Build Coastguard Worker NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0));
74*9880d681SAndroid Build Coastguard Worker InsertNewInstBefore(NewLHS, PN);
75*9880d681SAndroid Build Coastguard Worker LHSVal = NewLHS;
76*9880d681SAndroid Build Coastguard Worker }
77*9880d681SAndroid Build Coastguard Worker
78*9880d681SAndroid Build Coastguard Worker if (!RHSVal) {
79*9880d681SAndroid Build Coastguard Worker NewRHS = PHINode::Create(RHSType, PN.getNumIncomingValues(),
80*9880d681SAndroid Build Coastguard Worker FirstInst->getOperand(1)->getName() + ".pn");
81*9880d681SAndroid Build Coastguard Worker NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0));
82*9880d681SAndroid Build Coastguard Worker InsertNewInstBefore(NewRHS, PN);
83*9880d681SAndroid Build Coastguard Worker RHSVal = NewRHS;
84*9880d681SAndroid Build Coastguard Worker }
85*9880d681SAndroid Build Coastguard Worker
86*9880d681SAndroid Build Coastguard Worker // Add all operands to the new PHIs.
87*9880d681SAndroid Build Coastguard Worker if (NewLHS || NewRHS) {
88*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
89*9880d681SAndroid Build Coastguard Worker Instruction *InInst = cast<Instruction>(PN.getIncomingValue(i));
90*9880d681SAndroid Build Coastguard Worker if (NewLHS) {
91*9880d681SAndroid Build Coastguard Worker Value *NewInLHS = InInst->getOperand(0);
92*9880d681SAndroid Build Coastguard Worker NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i));
93*9880d681SAndroid Build Coastguard Worker }
94*9880d681SAndroid Build Coastguard Worker if (NewRHS) {
95*9880d681SAndroid Build Coastguard Worker Value *NewInRHS = InInst->getOperand(1);
96*9880d681SAndroid Build Coastguard Worker NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i));
97*9880d681SAndroid Build Coastguard Worker }
98*9880d681SAndroid Build Coastguard Worker }
99*9880d681SAndroid Build Coastguard Worker }
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) {
102*9880d681SAndroid Build Coastguard Worker CmpInst *NewCI = CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(),
103*9880d681SAndroid Build Coastguard Worker LHSVal, RHSVal);
104*9880d681SAndroid Build Coastguard Worker NewCI->setDebugLoc(FirstInst->getDebugLoc());
105*9880d681SAndroid Build Coastguard Worker return NewCI;
106*9880d681SAndroid Build Coastguard Worker }
107*9880d681SAndroid Build Coastguard Worker
108*9880d681SAndroid Build Coastguard Worker BinaryOperator *BinOp = cast<BinaryOperator>(FirstInst);
109*9880d681SAndroid Build Coastguard Worker BinaryOperator *NewBinOp =
110*9880d681SAndroid Build Coastguard Worker BinaryOperator::Create(BinOp->getOpcode(), LHSVal, RHSVal);
111*9880d681SAndroid Build Coastguard Worker
112*9880d681SAndroid Build Coastguard Worker NewBinOp->copyIRFlags(PN.getIncomingValue(0));
113*9880d681SAndroid Build Coastguard Worker
114*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i)
115*9880d681SAndroid Build Coastguard Worker NewBinOp->andIRFlags(PN.getIncomingValue(i));
116*9880d681SAndroid Build Coastguard Worker
117*9880d681SAndroid Build Coastguard Worker NewBinOp->setDebugLoc(FirstInst->getDebugLoc());
118*9880d681SAndroid Build Coastguard Worker return NewBinOp;
119*9880d681SAndroid Build Coastguard Worker }
120*9880d681SAndroid Build Coastguard Worker
FoldPHIArgGEPIntoPHI(PHINode & PN)121*9880d681SAndroid Build Coastguard Worker Instruction *InstCombiner::FoldPHIArgGEPIntoPHI(PHINode &PN) {
122*9880d681SAndroid Build Coastguard Worker GetElementPtrInst *FirstInst =cast<GetElementPtrInst>(PN.getIncomingValue(0));
123*9880d681SAndroid Build Coastguard Worker
124*9880d681SAndroid Build Coastguard Worker SmallVector<Value*, 16> FixedOperands(FirstInst->op_begin(),
125*9880d681SAndroid Build Coastguard Worker FirstInst->op_end());
126*9880d681SAndroid Build Coastguard Worker // This is true if all GEP bases are allocas and if all indices into them are
127*9880d681SAndroid Build Coastguard Worker // constants.
128*9880d681SAndroid Build Coastguard Worker bool AllBasePointersAreAllocas = true;
129*9880d681SAndroid Build Coastguard Worker
130*9880d681SAndroid Build Coastguard Worker // We don't want to replace this phi if the replacement would require
131*9880d681SAndroid Build Coastguard Worker // more than one phi, which leads to higher register pressure. This is
132*9880d681SAndroid Build Coastguard Worker // especially bad when the PHIs are in the header of a loop.
133*9880d681SAndroid Build Coastguard Worker bool NeededPhi = false;
134*9880d681SAndroid Build Coastguard Worker
135*9880d681SAndroid Build Coastguard Worker bool AllInBounds = true;
136*9880d681SAndroid Build Coastguard Worker
137*9880d681SAndroid Build Coastguard Worker // Scan to see if all operands are the same opcode, and all have one use.
138*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) {
139*9880d681SAndroid Build Coastguard Worker GetElementPtrInst *GEP= dyn_cast<GetElementPtrInst>(PN.getIncomingValue(i));
140*9880d681SAndroid Build Coastguard Worker if (!GEP || !GEP->hasOneUse() || GEP->getType() != FirstInst->getType() ||
141*9880d681SAndroid Build Coastguard Worker GEP->getNumOperands() != FirstInst->getNumOperands())
142*9880d681SAndroid Build Coastguard Worker return nullptr;
143*9880d681SAndroid Build Coastguard Worker
144*9880d681SAndroid Build Coastguard Worker AllInBounds &= GEP->isInBounds();
145*9880d681SAndroid Build Coastguard Worker
146*9880d681SAndroid Build Coastguard Worker // Keep track of whether or not all GEPs are of alloca pointers.
147*9880d681SAndroid Build Coastguard Worker if (AllBasePointersAreAllocas &&
148*9880d681SAndroid Build Coastguard Worker (!isa<AllocaInst>(GEP->getOperand(0)) ||
149*9880d681SAndroid Build Coastguard Worker !GEP->hasAllConstantIndices()))
150*9880d681SAndroid Build Coastguard Worker AllBasePointersAreAllocas = false;
151*9880d681SAndroid Build Coastguard Worker
152*9880d681SAndroid Build Coastguard Worker // Compare the operand lists.
153*9880d681SAndroid Build Coastguard Worker for (unsigned op = 0, e = FirstInst->getNumOperands(); op != e; ++op) {
154*9880d681SAndroid Build Coastguard Worker if (FirstInst->getOperand(op) == GEP->getOperand(op))
155*9880d681SAndroid Build Coastguard Worker continue;
156*9880d681SAndroid Build Coastguard Worker
157*9880d681SAndroid Build Coastguard Worker // Don't merge two GEPs when two operands differ (introducing phi nodes)
158*9880d681SAndroid Build Coastguard Worker // if one of the PHIs has a constant for the index. The index may be
159*9880d681SAndroid Build Coastguard Worker // substantially cheaper to compute for the constants, so making it a
160*9880d681SAndroid Build Coastguard Worker // variable index could pessimize the path. This also handles the case
161*9880d681SAndroid Build Coastguard Worker // for struct indices, which must always be constant.
162*9880d681SAndroid Build Coastguard Worker if (isa<ConstantInt>(FirstInst->getOperand(op)) ||
163*9880d681SAndroid Build Coastguard Worker isa<ConstantInt>(GEP->getOperand(op)))
164*9880d681SAndroid Build Coastguard Worker return nullptr;
165*9880d681SAndroid Build Coastguard Worker
166*9880d681SAndroid Build Coastguard Worker if (FirstInst->getOperand(op)->getType() !=GEP->getOperand(op)->getType())
167*9880d681SAndroid Build Coastguard Worker return nullptr;
168*9880d681SAndroid Build Coastguard Worker
169*9880d681SAndroid Build Coastguard Worker // If we already needed a PHI for an earlier operand, and another operand
170*9880d681SAndroid Build Coastguard Worker // also requires a PHI, we'd be introducing more PHIs than we're
171*9880d681SAndroid Build Coastguard Worker // eliminating, which increases register pressure on entry to the PHI's
172*9880d681SAndroid Build Coastguard Worker // block.
173*9880d681SAndroid Build Coastguard Worker if (NeededPhi)
174*9880d681SAndroid Build Coastguard Worker return nullptr;
175*9880d681SAndroid Build Coastguard Worker
176*9880d681SAndroid Build Coastguard Worker FixedOperands[op] = nullptr; // Needs a PHI.
177*9880d681SAndroid Build Coastguard Worker NeededPhi = true;
178*9880d681SAndroid Build Coastguard Worker }
179*9880d681SAndroid Build Coastguard Worker }
180*9880d681SAndroid Build Coastguard Worker
181*9880d681SAndroid Build Coastguard Worker // If all of the base pointers of the PHI'd GEPs are from allocas, don't
182*9880d681SAndroid Build Coastguard Worker // bother doing this transformation. At best, this will just save a bit of
183*9880d681SAndroid Build Coastguard Worker // offset calculation, but all the predecessors will have to materialize the
184*9880d681SAndroid Build Coastguard Worker // stack address into a register anyway. We'd actually rather *clone* the
185*9880d681SAndroid Build Coastguard Worker // load up into the predecessors so that we have a load of a gep of an alloca,
186*9880d681SAndroid Build Coastguard Worker // which can usually all be folded into the load.
187*9880d681SAndroid Build Coastguard Worker if (AllBasePointersAreAllocas)
188*9880d681SAndroid Build Coastguard Worker return nullptr;
189*9880d681SAndroid Build Coastguard Worker
190*9880d681SAndroid Build Coastguard Worker // Otherwise, this is safe to transform. Insert PHI nodes for each operand
191*9880d681SAndroid Build Coastguard Worker // that is variable.
192*9880d681SAndroid Build Coastguard Worker SmallVector<PHINode*, 16> OperandPhis(FixedOperands.size());
193*9880d681SAndroid Build Coastguard Worker
194*9880d681SAndroid Build Coastguard Worker bool HasAnyPHIs = false;
195*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = FixedOperands.size(); i != e; ++i) {
196*9880d681SAndroid Build Coastguard Worker if (FixedOperands[i]) continue; // operand doesn't need a phi.
197*9880d681SAndroid Build Coastguard Worker Value *FirstOp = FirstInst->getOperand(i);
198*9880d681SAndroid Build Coastguard Worker PHINode *NewPN = PHINode::Create(FirstOp->getType(), e,
199*9880d681SAndroid Build Coastguard Worker FirstOp->getName()+".pn");
200*9880d681SAndroid Build Coastguard Worker InsertNewInstBefore(NewPN, PN);
201*9880d681SAndroid Build Coastguard Worker
202*9880d681SAndroid Build Coastguard Worker NewPN->addIncoming(FirstOp, PN.getIncomingBlock(0));
203*9880d681SAndroid Build Coastguard Worker OperandPhis[i] = NewPN;
204*9880d681SAndroid Build Coastguard Worker FixedOperands[i] = NewPN;
205*9880d681SAndroid Build Coastguard Worker HasAnyPHIs = true;
206*9880d681SAndroid Build Coastguard Worker }
207*9880d681SAndroid Build Coastguard Worker
208*9880d681SAndroid Build Coastguard Worker
209*9880d681SAndroid Build Coastguard Worker // Add all operands to the new PHIs.
210*9880d681SAndroid Build Coastguard Worker if (HasAnyPHIs) {
211*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
212*9880d681SAndroid Build Coastguard Worker GetElementPtrInst *InGEP =cast<GetElementPtrInst>(PN.getIncomingValue(i));
213*9880d681SAndroid Build Coastguard Worker BasicBlock *InBB = PN.getIncomingBlock(i);
214*9880d681SAndroid Build Coastguard Worker
215*9880d681SAndroid Build Coastguard Worker for (unsigned op = 0, e = OperandPhis.size(); op != e; ++op)
216*9880d681SAndroid Build Coastguard Worker if (PHINode *OpPhi = OperandPhis[op])
217*9880d681SAndroid Build Coastguard Worker OpPhi->addIncoming(InGEP->getOperand(op), InBB);
218*9880d681SAndroid Build Coastguard Worker }
219*9880d681SAndroid Build Coastguard Worker }
220*9880d681SAndroid Build Coastguard Worker
221*9880d681SAndroid Build Coastguard Worker Value *Base = FixedOperands[0];
222*9880d681SAndroid Build Coastguard Worker GetElementPtrInst *NewGEP =
223*9880d681SAndroid Build Coastguard Worker GetElementPtrInst::Create(FirstInst->getSourceElementType(), Base,
224*9880d681SAndroid Build Coastguard Worker makeArrayRef(FixedOperands).slice(1));
225*9880d681SAndroid Build Coastguard Worker if (AllInBounds) NewGEP->setIsInBounds();
226*9880d681SAndroid Build Coastguard Worker NewGEP->setDebugLoc(FirstInst->getDebugLoc());
227*9880d681SAndroid Build Coastguard Worker return NewGEP;
228*9880d681SAndroid Build Coastguard Worker }
229*9880d681SAndroid Build Coastguard Worker
230*9880d681SAndroid Build Coastguard Worker
231*9880d681SAndroid Build Coastguard Worker /// Return true if we know that it is safe to sink the load out of the block
232*9880d681SAndroid Build Coastguard Worker /// that defines it. This means that it must be obvious the value of the load is
233*9880d681SAndroid Build Coastguard Worker /// not changed from the point of the load to the end of the block it is in.
234*9880d681SAndroid Build Coastguard Worker ///
235*9880d681SAndroid Build Coastguard Worker /// Finally, it is safe, but not profitable, to sink a load targeting a
236*9880d681SAndroid Build Coastguard Worker /// non-address-taken alloca. Doing so will cause us to not promote the alloca
237*9880d681SAndroid Build Coastguard Worker /// to a register.
isSafeAndProfitableToSinkLoad(LoadInst * L)238*9880d681SAndroid Build Coastguard Worker static bool isSafeAndProfitableToSinkLoad(LoadInst *L) {
239*9880d681SAndroid Build Coastguard Worker BasicBlock::iterator BBI = L->getIterator(), E = L->getParent()->end();
240*9880d681SAndroid Build Coastguard Worker
241*9880d681SAndroid Build Coastguard Worker for (++BBI; BBI != E; ++BBI)
242*9880d681SAndroid Build Coastguard Worker if (BBI->mayWriteToMemory())
243*9880d681SAndroid Build Coastguard Worker return false;
244*9880d681SAndroid Build Coastguard Worker
245*9880d681SAndroid Build Coastguard Worker // Check for non-address taken alloca. If not address-taken already, it isn't
246*9880d681SAndroid Build Coastguard Worker // profitable to do this xform.
247*9880d681SAndroid Build Coastguard Worker if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) {
248*9880d681SAndroid Build Coastguard Worker bool isAddressTaken = false;
249*9880d681SAndroid Build Coastguard Worker for (User *U : AI->users()) {
250*9880d681SAndroid Build Coastguard Worker if (isa<LoadInst>(U)) continue;
251*9880d681SAndroid Build Coastguard Worker if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
252*9880d681SAndroid Build Coastguard Worker // If storing TO the alloca, then the address isn't taken.
253*9880d681SAndroid Build Coastguard Worker if (SI->getOperand(1) == AI) continue;
254*9880d681SAndroid Build Coastguard Worker }
255*9880d681SAndroid Build Coastguard Worker isAddressTaken = true;
256*9880d681SAndroid Build Coastguard Worker break;
257*9880d681SAndroid Build Coastguard Worker }
258*9880d681SAndroid Build Coastguard Worker
259*9880d681SAndroid Build Coastguard Worker if (!isAddressTaken && AI->isStaticAlloca())
260*9880d681SAndroid Build Coastguard Worker return false;
261*9880d681SAndroid Build Coastguard Worker }
262*9880d681SAndroid Build Coastguard Worker
263*9880d681SAndroid Build Coastguard Worker // If this load is a load from a GEP with a constant offset from an alloca,
264*9880d681SAndroid Build Coastguard Worker // then we don't want to sink it. In its present form, it will be
265*9880d681SAndroid Build Coastguard Worker // load [constant stack offset]. Sinking it will cause us to have to
266*9880d681SAndroid Build Coastguard Worker // materialize the stack addresses in each predecessor in a register only to
267*9880d681SAndroid Build Coastguard Worker // do a shared load from register in the successor.
268*9880d681SAndroid Build Coastguard Worker if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(L->getOperand(0)))
269*9880d681SAndroid Build Coastguard Worker if (AllocaInst *AI = dyn_cast<AllocaInst>(GEP->getOperand(0)))
270*9880d681SAndroid Build Coastguard Worker if (AI->isStaticAlloca() && GEP->hasAllConstantIndices())
271*9880d681SAndroid Build Coastguard Worker return false;
272*9880d681SAndroid Build Coastguard Worker
273*9880d681SAndroid Build Coastguard Worker return true;
274*9880d681SAndroid Build Coastguard Worker }
275*9880d681SAndroid Build Coastguard Worker
FoldPHIArgLoadIntoPHI(PHINode & PN)276*9880d681SAndroid Build Coastguard Worker Instruction *InstCombiner::FoldPHIArgLoadIntoPHI(PHINode &PN) {
277*9880d681SAndroid Build Coastguard Worker LoadInst *FirstLI = cast<LoadInst>(PN.getIncomingValue(0));
278*9880d681SAndroid Build Coastguard Worker
279*9880d681SAndroid Build Coastguard Worker // FIXME: This is overconservative; this transform is allowed in some cases
280*9880d681SAndroid Build Coastguard Worker // for atomic operations.
281*9880d681SAndroid Build Coastguard Worker if (FirstLI->isAtomic())
282*9880d681SAndroid Build Coastguard Worker return nullptr;
283*9880d681SAndroid Build Coastguard Worker
284*9880d681SAndroid Build Coastguard Worker // When processing loads, we need to propagate two bits of information to the
285*9880d681SAndroid Build Coastguard Worker // sunk load: whether it is volatile, and what its alignment is. We currently
286*9880d681SAndroid Build Coastguard Worker // don't sink loads when some have their alignment specified and some don't.
287*9880d681SAndroid Build Coastguard Worker // visitLoadInst will propagate an alignment onto the load when TD is around,
288*9880d681SAndroid Build Coastguard Worker // and if TD isn't around, we can't handle the mixed case.
289*9880d681SAndroid Build Coastguard Worker bool isVolatile = FirstLI->isVolatile();
290*9880d681SAndroid Build Coastguard Worker unsigned LoadAlignment = FirstLI->getAlignment();
291*9880d681SAndroid Build Coastguard Worker unsigned LoadAddrSpace = FirstLI->getPointerAddressSpace();
292*9880d681SAndroid Build Coastguard Worker
293*9880d681SAndroid Build Coastguard Worker // We can't sink the load if the loaded value could be modified between the
294*9880d681SAndroid Build Coastguard Worker // load and the PHI.
295*9880d681SAndroid Build Coastguard Worker if (FirstLI->getParent() != PN.getIncomingBlock(0) ||
296*9880d681SAndroid Build Coastguard Worker !isSafeAndProfitableToSinkLoad(FirstLI))
297*9880d681SAndroid Build Coastguard Worker return nullptr;
298*9880d681SAndroid Build Coastguard Worker
299*9880d681SAndroid Build Coastguard Worker // If the PHI is of volatile loads and the load block has multiple
300*9880d681SAndroid Build Coastguard Worker // successors, sinking it would remove a load of the volatile value from
301*9880d681SAndroid Build Coastguard Worker // the path through the other successor.
302*9880d681SAndroid Build Coastguard Worker if (isVolatile &&
303*9880d681SAndroid Build Coastguard Worker FirstLI->getParent()->getTerminator()->getNumSuccessors() != 1)
304*9880d681SAndroid Build Coastguard Worker return nullptr;
305*9880d681SAndroid Build Coastguard Worker
306*9880d681SAndroid Build Coastguard Worker // Check to see if all arguments are the same operation.
307*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
308*9880d681SAndroid Build Coastguard Worker LoadInst *LI = dyn_cast<LoadInst>(PN.getIncomingValue(i));
309*9880d681SAndroid Build Coastguard Worker if (!LI || !LI->hasOneUse())
310*9880d681SAndroid Build Coastguard Worker return nullptr;
311*9880d681SAndroid Build Coastguard Worker
312*9880d681SAndroid Build Coastguard Worker // We can't sink the load if the loaded value could be modified between
313*9880d681SAndroid Build Coastguard Worker // the load and the PHI.
314*9880d681SAndroid Build Coastguard Worker if (LI->isVolatile() != isVolatile ||
315*9880d681SAndroid Build Coastguard Worker LI->getParent() != PN.getIncomingBlock(i) ||
316*9880d681SAndroid Build Coastguard Worker LI->getPointerAddressSpace() != LoadAddrSpace ||
317*9880d681SAndroid Build Coastguard Worker !isSafeAndProfitableToSinkLoad(LI))
318*9880d681SAndroid Build Coastguard Worker return nullptr;
319*9880d681SAndroid Build Coastguard Worker
320*9880d681SAndroid Build Coastguard Worker // If some of the loads have an alignment specified but not all of them,
321*9880d681SAndroid Build Coastguard Worker // we can't do the transformation.
322*9880d681SAndroid Build Coastguard Worker if ((LoadAlignment != 0) != (LI->getAlignment() != 0))
323*9880d681SAndroid Build Coastguard Worker return nullptr;
324*9880d681SAndroid Build Coastguard Worker
325*9880d681SAndroid Build Coastguard Worker LoadAlignment = std::min(LoadAlignment, LI->getAlignment());
326*9880d681SAndroid Build Coastguard Worker
327*9880d681SAndroid Build Coastguard Worker // If the PHI is of volatile loads and the load block has multiple
328*9880d681SAndroid Build Coastguard Worker // successors, sinking it would remove a load of the volatile value from
329*9880d681SAndroid Build Coastguard Worker // the path through the other successor.
330*9880d681SAndroid Build Coastguard Worker if (isVolatile &&
331*9880d681SAndroid Build Coastguard Worker LI->getParent()->getTerminator()->getNumSuccessors() != 1)
332*9880d681SAndroid Build Coastguard Worker return nullptr;
333*9880d681SAndroid Build Coastguard Worker }
334*9880d681SAndroid Build Coastguard Worker
335*9880d681SAndroid Build Coastguard Worker // Okay, they are all the same operation. Create a new PHI node of the
336*9880d681SAndroid Build Coastguard Worker // correct type, and PHI together all of the LHS's of the instructions.
337*9880d681SAndroid Build Coastguard Worker PHINode *NewPN = PHINode::Create(FirstLI->getOperand(0)->getType(),
338*9880d681SAndroid Build Coastguard Worker PN.getNumIncomingValues(),
339*9880d681SAndroid Build Coastguard Worker PN.getName()+".in");
340*9880d681SAndroid Build Coastguard Worker
341*9880d681SAndroid Build Coastguard Worker Value *InVal = FirstLI->getOperand(0);
342*9880d681SAndroid Build Coastguard Worker NewPN->addIncoming(InVal, PN.getIncomingBlock(0));
343*9880d681SAndroid Build Coastguard Worker LoadInst *NewLI = new LoadInst(NewPN, "", isVolatile, LoadAlignment);
344*9880d681SAndroid Build Coastguard Worker
345*9880d681SAndroid Build Coastguard Worker unsigned KnownIDs[] = {
346*9880d681SAndroid Build Coastguard Worker LLVMContext::MD_tbaa,
347*9880d681SAndroid Build Coastguard Worker LLVMContext::MD_range,
348*9880d681SAndroid Build Coastguard Worker LLVMContext::MD_invariant_load,
349*9880d681SAndroid Build Coastguard Worker LLVMContext::MD_alias_scope,
350*9880d681SAndroid Build Coastguard Worker LLVMContext::MD_noalias,
351*9880d681SAndroid Build Coastguard Worker LLVMContext::MD_nonnull,
352*9880d681SAndroid Build Coastguard Worker LLVMContext::MD_align,
353*9880d681SAndroid Build Coastguard Worker LLVMContext::MD_dereferenceable,
354*9880d681SAndroid Build Coastguard Worker LLVMContext::MD_dereferenceable_or_null,
355*9880d681SAndroid Build Coastguard Worker };
356*9880d681SAndroid Build Coastguard Worker
357*9880d681SAndroid Build Coastguard Worker for (unsigned ID : KnownIDs)
358*9880d681SAndroid Build Coastguard Worker NewLI->setMetadata(ID, FirstLI->getMetadata(ID));
359*9880d681SAndroid Build Coastguard Worker
360*9880d681SAndroid Build Coastguard Worker // Add all operands to the new PHI and combine TBAA metadata.
361*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
362*9880d681SAndroid Build Coastguard Worker LoadInst *LI = cast<LoadInst>(PN.getIncomingValue(i));
363*9880d681SAndroid Build Coastguard Worker combineMetadata(NewLI, LI, KnownIDs);
364*9880d681SAndroid Build Coastguard Worker Value *NewInVal = LI->getOperand(0);
365*9880d681SAndroid Build Coastguard Worker if (NewInVal != InVal)
366*9880d681SAndroid Build Coastguard Worker InVal = nullptr;
367*9880d681SAndroid Build Coastguard Worker NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i));
368*9880d681SAndroid Build Coastguard Worker }
369*9880d681SAndroid Build Coastguard Worker
370*9880d681SAndroid Build Coastguard Worker if (InVal) {
371*9880d681SAndroid Build Coastguard Worker // The new PHI unions all of the same values together. This is really
372*9880d681SAndroid Build Coastguard Worker // common, so we handle it intelligently here for compile-time speed.
373*9880d681SAndroid Build Coastguard Worker NewLI->setOperand(0, InVal);
374*9880d681SAndroid Build Coastguard Worker delete NewPN;
375*9880d681SAndroid Build Coastguard Worker } else {
376*9880d681SAndroid Build Coastguard Worker InsertNewInstBefore(NewPN, PN);
377*9880d681SAndroid Build Coastguard Worker }
378*9880d681SAndroid Build Coastguard Worker
379*9880d681SAndroid Build Coastguard Worker // If this was a volatile load that we are merging, make sure to loop through
380*9880d681SAndroid Build Coastguard Worker // and mark all the input loads as non-volatile. If we don't do this, we will
381*9880d681SAndroid Build Coastguard Worker // insert a new volatile load and the old ones will not be deletable.
382*9880d681SAndroid Build Coastguard Worker if (isVolatile)
383*9880d681SAndroid Build Coastguard Worker for (Value *IncValue : PN.incoming_values())
384*9880d681SAndroid Build Coastguard Worker cast<LoadInst>(IncValue)->setVolatile(false);
385*9880d681SAndroid Build Coastguard Worker
386*9880d681SAndroid Build Coastguard Worker NewLI->setDebugLoc(FirstLI->getDebugLoc());
387*9880d681SAndroid Build Coastguard Worker return NewLI;
388*9880d681SAndroid Build Coastguard Worker }
389*9880d681SAndroid Build Coastguard Worker
390*9880d681SAndroid Build Coastguard Worker /// TODO: This function could handle other cast types, but then it might
391*9880d681SAndroid Build Coastguard Worker /// require special-casing a cast from the 'i1' type. See the comment in
392*9880d681SAndroid Build Coastguard Worker /// FoldPHIArgOpIntoPHI() about pessimizing illegal integer types.
FoldPHIArgZextsIntoPHI(PHINode & Phi)393*9880d681SAndroid Build Coastguard Worker Instruction *InstCombiner::FoldPHIArgZextsIntoPHI(PHINode &Phi) {
394*9880d681SAndroid Build Coastguard Worker // We cannot create a new instruction after the PHI if the terminator is an
395*9880d681SAndroid Build Coastguard Worker // EHPad because there is no valid insertion point.
396*9880d681SAndroid Build Coastguard Worker if (TerminatorInst *TI = Phi.getParent()->getTerminator())
397*9880d681SAndroid Build Coastguard Worker if (TI->isEHPad())
398*9880d681SAndroid Build Coastguard Worker return nullptr;
399*9880d681SAndroid Build Coastguard Worker
400*9880d681SAndroid Build Coastguard Worker // Early exit for the common case of a phi with two operands. These are
401*9880d681SAndroid Build Coastguard Worker // handled elsewhere. See the comment below where we check the count of zexts
402*9880d681SAndroid Build Coastguard Worker // and constants for more details.
403*9880d681SAndroid Build Coastguard Worker unsigned NumIncomingValues = Phi.getNumIncomingValues();
404*9880d681SAndroid Build Coastguard Worker if (NumIncomingValues < 3)
405*9880d681SAndroid Build Coastguard Worker return nullptr;
406*9880d681SAndroid Build Coastguard Worker
407*9880d681SAndroid Build Coastguard Worker // Find the narrower type specified by the first zext.
408*9880d681SAndroid Build Coastguard Worker Type *NarrowType = nullptr;
409*9880d681SAndroid Build Coastguard Worker for (Value *V : Phi.incoming_values()) {
410*9880d681SAndroid Build Coastguard Worker if (auto *Zext = dyn_cast<ZExtInst>(V)) {
411*9880d681SAndroid Build Coastguard Worker NarrowType = Zext->getSrcTy();
412*9880d681SAndroid Build Coastguard Worker break;
413*9880d681SAndroid Build Coastguard Worker }
414*9880d681SAndroid Build Coastguard Worker }
415*9880d681SAndroid Build Coastguard Worker if (!NarrowType)
416*9880d681SAndroid Build Coastguard Worker return nullptr;
417*9880d681SAndroid Build Coastguard Worker
418*9880d681SAndroid Build Coastguard Worker // Walk the phi operands checking that we only have zexts or constants that
419*9880d681SAndroid Build Coastguard Worker // we can shrink for free. Store the new operands for the new phi.
420*9880d681SAndroid Build Coastguard Worker SmallVector<Value *, 4> NewIncoming;
421*9880d681SAndroid Build Coastguard Worker unsigned NumZexts = 0;
422*9880d681SAndroid Build Coastguard Worker unsigned NumConsts = 0;
423*9880d681SAndroid Build Coastguard Worker for (Value *V : Phi.incoming_values()) {
424*9880d681SAndroid Build Coastguard Worker if (auto *Zext = dyn_cast<ZExtInst>(V)) {
425*9880d681SAndroid Build Coastguard Worker // All zexts must be identical and have one use.
426*9880d681SAndroid Build Coastguard Worker if (Zext->getSrcTy() != NarrowType || !Zext->hasOneUse())
427*9880d681SAndroid Build Coastguard Worker return nullptr;
428*9880d681SAndroid Build Coastguard Worker NewIncoming.push_back(Zext->getOperand(0));
429*9880d681SAndroid Build Coastguard Worker NumZexts++;
430*9880d681SAndroid Build Coastguard Worker } else if (auto *C = dyn_cast<Constant>(V)) {
431*9880d681SAndroid Build Coastguard Worker // Make sure that constants can fit in the new type.
432*9880d681SAndroid Build Coastguard Worker Constant *Trunc = ConstantExpr::getTrunc(C, NarrowType);
433*9880d681SAndroid Build Coastguard Worker if (ConstantExpr::getZExt(Trunc, C->getType()) != C)
434*9880d681SAndroid Build Coastguard Worker return nullptr;
435*9880d681SAndroid Build Coastguard Worker NewIncoming.push_back(Trunc);
436*9880d681SAndroid Build Coastguard Worker NumConsts++;
437*9880d681SAndroid Build Coastguard Worker } else {
438*9880d681SAndroid Build Coastguard Worker // If it's not a cast or a constant, bail out.
439*9880d681SAndroid Build Coastguard Worker return nullptr;
440*9880d681SAndroid Build Coastguard Worker }
441*9880d681SAndroid Build Coastguard Worker }
442*9880d681SAndroid Build Coastguard Worker
443*9880d681SAndroid Build Coastguard Worker // The more common cases of a phi with no constant operands or just one
444*9880d681SAndroid Build Coastguard Worker // variable operand are handled by FoldPHIArgOpIntoPHI() and FoldOpIntoPhi()
445*9880d681SAndroid Build Coastguard Worker // respectively. FoldOpIntoPhi() wants to do the opposite transform that is
446*9880d681SAndroid Build Coastguard Worker // performed here. It tries to replicate a cast in the phi operand's basic
447*9880d681SAndroid Build Coastguard Worker // block to expose other folding opportunities. Thus, InstCombine will
448*9880d681SAndroid Build Coastguard Worker // infinite loop without this check.
449*9880d681SAndroid Build Coastguard Worker if (NumConsts == 0 || NumZexts < 2)
450*9880d681SAndroid Build Coastguard Worker return nullptr;
451*9880d681SAndroid Build Coastguard Worker
452*9880d681SAndroid Build Coastguard Worker // All incoming values are zexts or constants that are safe to truncate.
453*9880d681SAndroid Build Coastguard Worker // Create a new phi node of the narrow type, phi together all of the new
454*9880d681SAndroid Build Coastguard Worker // operands, and zext the result back to the original type.
455*9880d681SAndroid Build Coastguard Worker PHINode *NewPhi = PHINode::Create(NarrowType, NumIncomingValues,
456*9880d681SAndroid Build Coastguard Worker Phi.getName() + ".shrunk");
457*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumIncomingValues; ++i)
458*9880d681SAndroid Build Coastguard Worker NewPhi->addIncoming(NewIncoming[i], Phi.getIncomingBlock(i));
459*9880d681SAndroid Build Coastguard Worker
460*9880d681SAndroid Build Coastguard Worker InsertNewInstBefore(NewPhi, Phi);
461*9880d681SAndroid Build Coastguard Worker return CastInst::CreateZExtOrBitCast(NewPhi, Phi.getType());
462*9880d681SAndroid Build Coastguard Worker }
463*9880d681SAndroid Build Coastguard Worker
464*9880d681SAndroid Build Coastguard Worker /// If all operands to a PHI node are the same "unary" operator and they all are
465*9880d681SAndroid Build Coastguard Worker /// only used by the PHI, PHI together their inputs, and do the operation once,
466*9880d681SAndroid Build Coastguard Worker /// to the result of the PHI.
FoldPHIArgOpIntoPHI(PHINode & PN)467*9880d681SAndroid Build Coastguard Worker Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) {
468*9880d681SAndroid Build Coastguard Worker // We cannot create a new instruction after the PHI if the terminator is an
469*9880d681SAndroid Build Coastguard Worker // EHPad because there is no valid insertion point.
470*9880d681SAndroid Build Coastguard Worker if (TerminatorInst *TI = PN.getParent()->getTerminator())
471*9880d681SAndroid Build Coastguard Worker if (TI->isEHPad())
472*9880d681SAndroid Build Coastguard Worker return nullptr;
473*9880d681SAndroid Build Coastguard Worker
474*9880d681SAndroid Build Coastguard Worker Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0));
475*9880d681SAndroid Build Coastguard Worker
476*9880d681SAndroid Build Coastguard Worker if (isa<GetElementPtrInst>(FirstInst))
477*9880d681SAndroid Build Coastguard Worker return FoldPHIArgGEPIntoPHI(PN);
478*9880d681SAndroid Build Coastguard Worker if (isa<LoadInst>(FirstInst))
479*9880d681SAndroid Build Coastguard Worker return FoldPHIArgLoadIntoPHI(PN);
480*9880d681SAndroid Build Coastguard Worker
481*9880d681SAndroid Build Coastguard Worker // Scan the instruction, looking for input operations that can be folded away.
482*9880d681SAndroid Build Coastguard Worker // If all input operands to the phi are the same instruction (e.g. a cast from
483*9880d681SAndroid Build Coastguard Worker // the same type or "+42") we can pull the operation through the PHI, reducing
484*9880d681SAndroid Build Coastguard Worker // code size and simplifying code.
485*9880d681SAndroid Build Coastguard Worker Constant *ConstantOp = nullptr;
486*9880d681SAndroid Build Coastguard Worker Type *CastSrcTy = nullptr;
487*9880d681SAndroid Build Coastguard Worker
488*9880d681SAndroid Build Coastguard Worker if (isa<CastInst>(FirstInst)) {
489*9880d681SAndroid Build Coastguard Worker CastSrcTy = FirstInst->getOperand(0)->getType();
490*9880d681SAndroid Build Coastguard Worker
491*9880d681SAndroid Build Coastguard Worker // Be careful about transforming integer PHIs. We don't want to pessimize
492*9880d681SAndroid Build Coastguard Worker // the code by turning an i32 into an i1293.
493*9880d681SAndroid Build Coastguard Worker if (PN.getType()->isIntegerTy() && CastSrcTy->isIntegerTy()) {
494*9880d681SAndroid Build Coastguard Worker if (!ShouldChangeType(PN.getType(), CastSrcTy))
495*9880d681SAndroid Build Coastguard Worker return nullptr;
496*9880d681SAndroid Build Coastguard Worker }
497*9880d681SAndroid Build Coastguard Worker } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) {
498*9880d681SAndroid Build Coastguard Worker // Can fold binop, compare or shift here if the RHS is a constant,
499*9880d681SAndroid Build Coastguard Worker // otherwise call FoldPHIArgBinOpIntoPHI.
500*9880d681SAndroid Build Coastguard Worker ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1));
501*9880d681SAndroid Build Coastguard Worker if (!ConstantOp)
502*9880d681SAndroid Build Coastguard Worker return FoldPHIArgBinOpIntoPHI(PN);
503*9880d681SAndroid Build Coastguard Worker } else {
504*9880d681SAndroid Build Coastguard Worker return nullptr; // Cannot fold this operation.
505*9880d681SAndroid Build Coastguard Worker }
506*9880d681SAndroid Build Coastguard Worker
507*9880d681SAndroid Build Coastguard Worker // Check to see if all arguments are the same operation.
508*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
509*9880d681SAndroid Build Coastguard Worker Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i));
510*9880d681SAndroid Build Coastguard Worker if (!I || !I->hasOneUse() || !I->isSameOperationAs(FirstInst))
511*9880d681SAndroid Build Coastguard Worker return nullptr;
512*9880d681SAndroid Build Coastguard Worker if (CastSrcTy) {
513*9880d681SAndroid Build Coastguard Worker if (I->getOperand(0)->getType() != CastSrcTy)
514*9880d681SAndroid Build Coastguard Worker return nullptr; // Cast operation must match.
515*9880d681SAndroid Build Coastguard Worker } else if (I->getOperand(1) != ConstantOp) {
516*9880d681SAndroid Build Coastguard Worker return nullptr;
517*9880d681SAndroid Build Coastguard Worker }
518*9880d681SAndroid Build Coastguard Worker }
519*9880d681SAndroid Build Coastguard Worker
520*9880d681SAndroid Build Coastguard Worker // Okay, they are all the same operation. Create a new PHI node of the
521*9880d681SAndroid Build Coastguard Worker // correct type, and PHI together all of the LHS's of the instructions.
522*9880d681SAndroid Build Coastguard Worker PHINode *NewPN = PHINode::Create(FirstInst->getOperand(0)->getType(),
523*9880d681SAndroid Build Coastguard Worker PN.getNumIncomingValues(),
524*9880d681SAndroid Build Coastguard Worker PN.getName()+".in");
525*9880d681SAndroid Build Coastguard Worker
526*9880d681SAndroid Build Coastguard Worker Value *InVal = FirstInst->getOperand(0);
527*9880d681SAndroid Build Coastguard Worker NewPN->addIncoming(InVal, PN.getIncomingBlock(0));
528*9880d681SAndroid Build Coastguard Worker
529*9880d681SAndroid Build Coastguard Worker // Add all operands to the new PHI.
530*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
531*9880d681SAndroid Build Coastguard Worker Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0);
532*9880d681SAndroid Build Coastguard Worker if (NewInVal != InVal)
533*9880d681SAndroid Build Coastguard Worker InVal = nullptr;
534*9880d681SAndroid Build Coastguard Worker NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i));
535*9880d681SAndroid Build Coastguard Worker }
536*9880d681SAndroid Build Coastguard Worker
537*9880d681SAndroid Build Coastguard Worker Value *PhiVal;
538*9880d681SAndroid Build Coastguard Worker if (InVal) {
539*9880d681SAndroid Build Coastguard Worker // The new PHI unions all of the same values together. This is really
540*9880d681SAndroid Build Coastguard Worker // common, so we handle it intelligently here for compile-time speed.
541*9880d681SAndroid Build Coastguard Worker PhiVal = InVal;
542*9880d681SAndroid Build Coastguard Worker delete NewPN;
543*9880d681SAndroid Build Coastguard Worker } else {
544*9880d681SAndroid Build Coastguard Worker InsertNewInstBefore(NewPN, PN);
545*9880d681SAndroid Build Coastguard Worker PhiVal = NewPN;
546*9880d681SAndroid Build Coastguard Worker }
547*9880d681SAndroid Build Coastguard Worker
548*9880d681SAndroid Build Coastguard Worker // Insert and return the new operation.
549*9880d681SAndroid Build Coastguard Worker if (CastInst *FirstCI = dyn_cast<CastInst>(FirstInst)) {
550*9880d681SAndroid Build Coastguard Worker CastInst *NewCI = CastInst::Create(FirstCI->getOpcode(), PhiVal,
551*9880d681SAndroid Build Coastguard Worker PN.getType());
552*9880d681SAndroid Build Coastguard Worker NewCI->setDebugLoc(FirstInst->getDebugLoc());
553*9880d681SAndroid Build Coastguard Worker return NewCI;
554*9880d681SAndroid Build Coastguard Worker }
555*9880d681SAndroid Build Coastguard Worker
556*9880d681SAndroid Build Coastguard Worker if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) {
557*9880d681SAndroid Build Coastguard Worker BinOp = BinaryOperator::Create(BinOp->getOpcode(), PhiVal, ConstantOp);
558*9880d681SAndroid Build Coastguard Worker BinOp->copyIRFlags(PN.getIncomingValue(0));
559*9880d681SAndroid Build Coastguard Worker
560*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i)
561*9880d681SAndroid Build Coastguard Worker BinOp->andIRFlags(PN.getIncomingValue(i));
562*9880d681SAndroid Build Coastguard Worker
563*9880d681SAndroid Build Coastguard Worker BinOp->setDebugLoc(FirstInst->getDebugLoc());
564*9880d681SAndroid Build Coastguard Worker return BinOp;
565*9880d681SAndroid Build Coastguard Worker }
566*9880d681SAndroid Build Coastguard Worker
567*9880d681SAndroid Build Coastguard Worker CmpInst *CIOp = cast<CmpInst>(FirstInst);
568*9880d681SAndroid Build Coastguard Worker CmpInst *NewCI = CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(),
569*9880d681SAndroid Build Coastguard Worker PhiVal, ConstantOp);
570*9880d681SAndroid Build Coastguard Worker NewCI->setDebugLoc(FirstInst->getDebugLoc());
571*9880d681SAndroid Build Coastguard Worker return NewCI;
572*9880d681SAndroid Build Coastguard Worker }
573*9880d681SAndroid Build Coastguard Worker
574*9880d681SAndroid Build Coastguard Worker /// Return true if this PHI node is only used by a PHI node cycle that is dead.
DeadPHICycle(PHINode * PN,SmallPtrSetImpl<PHINode * > & PotentiallyDeadPHIs)575*9880d681SAndroid Build Coastguard Worker static bool DeadPHICycle(PHINode *PN,
576*9880d681SAndroid Build Coastguard Worker SmallPtrSetImpl<PHINode*> &PotentiallyDeadPHIs) {
577*9880d681SAndroid Build Coastguard Worker if (PN->use_empty()) return true;
578*9880d681SAndroid Build Coastguard Worker if (!PN->hasOneUse()) return false;
579*9880d681SAndroid Build Coastguard Worker
580*9880d681SAndroid Build Coastguard Worker // Remember this node, and if we find the cycle, return.
581*9880d681SAndroid Build Coastguard Worker if (!PotentiallyDeadPHIs.insert(PN).second)
582*9880d681SAndroid Build Coastguard Worker return true;
583*9880d681SAndroid Build Coastguard Worker
584*9880d681SAndroid Build Coastguard Worker // Don't scan crazily complex things.
585*9880d681SAndroid Build Coastguard Worker if (PotentiallyDeadPHIs.size() == 16)
586*9880d681SAndroid Build Coastguard Worker return false;
587*9880d681SAndroid Build Coastguard Worker
588*9880d681SAndroid Build Coastguard Worker if (PHINode *PU = dyn_cast<PHINode>(PN->user_back()))
589*9880d681SAndroid Build Coastguard Worker return DeadPHICycle(PU, PotentiallyDeadPHIs);
590*9880d681SAndroid Build Coastguard Worker
591*9880d681SAndroid Build Coastguard Worker return false;
592*9880d681SAndroid Build Coastguard Worker }
593*9880d681SAndroid Build Coastguard Worker
594*9880d681SAndroid Build Coastguard Worker /// Return true if this phi node is always equal to NonPhiInVal.
595*9880d681SAndroid Build Coastguard Worker /// This happens with mutually cyclic phi nodes like:
596*9880d681SAndroid Build Coastguard Worker /// z = some value; x = phi (y, z); y = phi (x, z)
PHIsEqualValue(PHINode * PN,Value * NonPhiInVal,SmallPtrSetImpl<PHINode * > & ValueEqualPHIs)597*9880d681SAndroid Build Coastguard Worker static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal,
598*9880d681SAndroid Build Coastguard Worker SmallPtrSetImpl<PHINode*> &ValueEqualPHIs) {
599*9880d681SAndroid Build Coastguard Worker // See if we already saw this PHI node.
600*9880d681SAndroid Build Coastguard Worker if (!ValueEqualPHIs.insert(PN).second)
601*9880d681SAndroid Build Coastguard Worker return true;
602*9880d681SAndroid Build Coastguard Worker
603*9880d681SAndroid Build Coastguard Worker // Don't scan crazily complex things.
604*9880d681SAndroid Build Coastguard Worker if (ValueEqualPHIs.size() == 16)
605*9880d681SAndroid Build Coastguard Worker return false;
606*9880d681SAndroid Build Coastguard Worker
607*9880d681SAndroid Build Coastguard Worker // Scan the operands to see if they are either phi nodes or are equal to
608*9880d681SAndroid Build Coastguard Worker // the value.
609*9880d681SAndroid Build Coastguard Worker for (Value *Op : PN->incoming_values()) {
610*9880d681SAndroid Build Coastguard Worker if (PHINode *OpPN = dyn_cast<PHINode>(Op)) {
611*9880d681SAndroid Build Coastguard Worker if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs))
612*9880d681SAndroid Build Coastguard Worker return false;
613*9880d681SAndroid Build Coastguard Worker } else if (Op != NonPhiInVal)
614*9880d681SAndroid Build Coastguard Worker return false;
615*9880d681SAndroid Build Coastguard Worker }
616*9880d681SAndroid Build Coastguard Worker
617*9880d681SAndroid Build Coastguard Worker return true;
618*9880d681SAndroid Build Coastguard Worker }
619*9880d681SAndroid Build Coastguard Worker
620*9880d681SAndroid Build Coastguard Worker /// Return an existing non-zero constant if this phi node has one, otherwise
621*9880d681SAndroid Build Coastguard Worker /// return constant 1.
GetAnyNonZeroConstInt(PHINode & PN)622*9880d681SAndroid Build Coastguard Worker static ConstantInt *GetAnyNonZeroConstInt(PHINode &PN) {
623*9880d681SAndroid Build Coastguard Worker assert(isa<IntegerType>(PN.getType()) && "Expect only intger type phi");
624*9880d681SAndroid Build Coastguard Worker for (Value *V : PN.operands())
625*9880d681SAndroid Build Coastguard Worker if (auto *ConstVA = dyn_cast<ConstantInt>(V))
626*9880d681SAndroid Build Coastguard Worker if (!ConstVA->isZeroValue())
627*9880d681SAndroid Build Coastguard Worker return ConstVA;
628*9880d681SAndroid Build Coastguard Worker return ConstantInt::get(cast<IntegerType>(PN.getType()), 1);
629*9880d681SAndroid Build Coastguard Worker }
630*9880d681SAndroid Build Coastguard Worker
631*9880d681SAndroid Build Coastguard Worker namespace {
632*9880d681SAndroid Build Coastguard Worker struct PHIUsageRecord {
633*9880d681SAndroid Build Coastguard Worker unsigned PHIId; // The ID # of the PHI (something determinstic to sort on)
634*9880d681SAndroid Build Coastguard Worker unsigned Shift; // The amount shifted.
635*9880d681SAndroid Build Coastguard Worker Instruction *Inst; // The trunc instruction.
636*9880d681SAndroid Build Coastguard Worker
PHIUsageRecord__anond529a3ac0111::PHIUsageRecord637*9880d681SAndroid Build Coastguard Worker PHIUsageRecord(unsigned pn, unsigned Sh, Instruction *User)
638*9880d681SAndroid Build Coastguard Worker : PHIId(pn), Shift(Sh), Inst(User) {}
639*9880d681SAndroid Build Coastguard Worker
operator <__anond529a3ac0111::PHIUsageRecord640*9880d681SAndroid Build Coastguard Worker bool operator<(const PHIUsageRecord &RHS) const {
641*9880d681SAndroid Build Coastguard Worker if (PHIId < RHS.PHIId) return true;
642*9880d681SAndroid Build Coastguard Worker if (PHIId > RHS.PHIId) return false;
643*9880d681SAndroid Build Coastguard Worker if (Shift < RHS.Shift) return true;
644*9880d681SAndroid Build Coastguard Worker if (Shift > RHS.Shift) return false;
645*9880d681SAndroid Build Coastguard Worker return Inst->getType()->getPrimitiveSizeInBits() <
646*9880d681SAndroid Build Coastguard Worker RHS.Inst->getType()->getPrimitiveSizeInBits();
647*9880d681SAndroid Build Coastguard Worker }
648*9880d681SAndroid Build Coastguard Worker };
649*9880d681SAndroid Build Coastguard Worker
650*9880d681SAndroid Build Coastguard Worker struct LoweredPHIRecord {
651*9880d681SAndroid Build Coastguard Worker PHINode *PN; // The PHI that was lowered.
652*9880d681SAndroid Build Coastguard Worker unsigned Shift; // The amount shifted.
653*9880d681SAndroid Build Coastguard Worker unsigned Width; // The width extracted.
654*9880d681SAndroid Build Coastguard Worker
LoweredPHIRecord__anond529a3ac0111::LoweredPHIRecord655*9880d681SAndroid Build Coastguard Worker LoweredPHIRecord(PHINode *pn, unsigned Sh, Type *Ty)
656*9880d681SAndroid Build Coastguard Worker : PN(pn), Shift(Sh), Width(Ty->getPrimitiveSizeInBits()) {}
657*9880d681SAndroid Build Coastguard Worker
658*9880d681SAndroid Build Coastguard Worker // Ctor form used by DenseMap.
LoweredPHIRecord__anond529a3ac0111::LoweredPHIRecord659*9880d681SAndroid Build Coastguard Worker LoweredPHIRecord(PHINode *pn, unsigned Sh)
660*9880d681SAndroid Build Coastguard Worker : PN(pn), Shift(Sh), Width(0) {}
661*9880d681SAndroid Build Coastguard Worker };
662*9880d681SAndroid Build Coastguard Worker }
663*9880d681SAndroid Build Coastguard Worker
664*9880d681SAndroid Build Coastguard Worker namespace llvm {
665*9880d681SAndroid Build Coastguard Worker template<>
666*9880d681SAndroid Build Coastguard Worker struct DenseMapInfo<LoweredPHIRecord> {
getEmptyKeyllvm::DenseMapInfo667*9880d681SAndroid Build Coastguard Worker static inline LoweredPHIRecord getEmptyKey() {
668*9880d681SAndroid Build Coastguard Worker return LoweredPHIRecord(nullptr, 0);
669*9880d681SAndroid Build Coastguard Worker }
getTombstoneKeyllvm::DenseMapInfo670*9880d681SAndroid Build Coastguard Worker static inline LoweredPHIRecord getTombstoneKey() {
671*9880d681SAndroid Build Coastguard Worker return LoweredPHIRecord(nullptr, 1);
672*9880d681SAndroid Build Coastguard Worker }
getHashValuellvm::DenseMapInfo673*9880d681SAndroid Build Coastguard Worker static unsigned getHashValue(const LoweredPHIRecord &Val) {
674*9880d681SAndroid Build Coastguard Worker return DenseMapInfo<PHINode*>::getHashValue(Val.PN) ^ (Val.Shift>>3) ^
675*9880d681SAndroid Build Coastguard Worker (Val.Width>>3);
676*9880d681SAndroid Build Coastguard Worker }
isEqualllvm::DenseMapInfo677*9880d681SAndroid Build Coastguard Worker static bool isEqual(const LoweredPHIRecord &LHS,
678*9880d681SAndroid Build Coastguard Worker const LoweredPHIRecord &RHS) {
679*9880d681SAndroid Build Coastguard Worker return LHS.PN == RHS.PN && LHS.Shift == RHS.Shift &&
680*9880d681SAndroid Build Coastguard Worker LHS.Width == RHS.Width;
681*9880d681SAndroid Build Coastguard Worker }
682*9880d681SAndroid Build Coastguard Worker };
683*9880d681SAndroid Build Coastguard Worker }
684*9880d681SAndroid Build Coastguard Worker
685*9880d681SAndroid Build Coastguard Worker
686*9880d681SAndroid Build Coastguard Worker /// This is an integer PHI and we know that it has an illegal type: see if it is
687*9880d681SAndroid Build Coastguard Worker /// only used by trunc or trunc(lshr) operations. If so, we split the PHI into
688*9880d681SAndroid Build Coastguard Worker /// the various pieces being extracted. This sort of thing is introduced when
689*9880d681SAndroid Build Coastguard Worker /// SROA promotes an aggregate to large integer values.
690*9880d681SAndroid Build Coastguard Worker ///
691*9880d681SAndroid Build Coastguard Worker /// TODO: The user of the trunc may be an bitcast to float/double/vector or an
692*9880d681SAndroid Build Coastguard Worker /// inttoptr. We should produce new PHIs in the right type.
693*9880d681SAndroid Build Coastguard Worker ///
SliceUpIllegalIntegerPHI(PHINode & FirstPhi)694*9880d681SAndroid Build Coastguard Worker Instruction *InstCombiner::SliceUpIllegalIntegerPHI(PHINode &FirstPhi) {
695*9880d681SAndroid Build Coastguard Worker // PHIUsers - Keep track of all of the truncated values extracted from a set
696*9880d681SAndroid Build Coastguard Worker // of PHIs, along with their offset. These are the things we want to rewrite.
697*9880d681SAndroid Build Coastguard Worker SmallVector<PHIUsageRecord, 16> PHIUsers;
698*9880d681SAndroid Build Coastguard Worker
699*9880d681SAndroid Build Coastguard Worker // PHIs are often mutually cyclic, so we keep track of a whole set of PHI
700*9880d681SAndroid Build Coastguard Worker // nodes which are extracted from. PHIsToSlice is a set we use to avoid
701*9880d681SAndroid Build Coastguard Worker // revisiting PHIs, PHIsInspected is a ordered list of PHIs that we need to
702*9880d681SAndroid Build Coastguard Worker // check the uses of (to ensure they are all extracts).
703*9880d681SAndroid Build Coastguard Worker SmallVector<PHINode*, 8> PHIsToSlice;
704*9880d681SAndroid Build Coastguard Worker SmallPtrSet<PHINode*, 8> PHIsInspected;
705*9880d681SAndroid Build Coastguard Worker
706*9880d681SAndroid Build Coastguard Worker PHIsToSlice.push_back(&FirstPhi);
707*9880d681SAndroid Build Coastguard Worker PHIsInspected.insert(&FirstPhi);
708*9880d681SAndroid Build Coastguard Worker
709*9880d681SAndroid Build Coastguard Worker for (unsigned PHIId = 0; PHIId != PHIsToSlice.size(); ++PHIId) {
710*9880d681SAndroid Build Coastguard Worker PHINode *PN = PHIsToSlice[PHIId];
711*9880d681SAndroid Build Coastguard Worker
712*9880d681SAndroid Build Coastguard Worker // Scan the input list of the PHI. If any input is an invoke, and if the
713*9880d681SAndroid Build Coastguard Worker // input is defined in the predecessor, then we won't be split the critical
714*9880d681SAndroid Build Coastguard Worker // edge which is required to insert a truncate. Because of this, we have to
715*9880d681SAndroid Build Coastguard Worker // bail out.
716*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
717*9880d681SAndroid Build Coastguard Worker InvokeInst *II = dyn_cast<InvokeInst>(PN->getIncomingValue(i));
718*9880d681SAndroid Build Coastguard Worker if (!II) continue;
719*9880d681SAndroid Build Coastguard Worker if (II->getParent() != PN->getIncomingBlock(i))
720*9880d681SAndroid Build Coastguard Worker continue;
721*9880d681SAndroid Build Coastguard Worker
722*9880d681SAndroid Build Coastguard Worker // If we have a phi, and if it's directly in the predecessor, then we have
723*9880d681SAndroid Build Coastguard Worker // a critical edge where we need to put the truncate. Since we can't
724*9880d681SAndroid Build Coastguard Worker // split the edge in instcombine, we have to bail out.
725*9880d681SAndroid Build Coastguard Worker return nullptr;
726*9880d681SAndroid Build Coastguard Worker }
727*9880d681SAndroid Build Coastguard Worker
728*9880d681SAndroid Build Coastguard Worker for (User *U : PN->users()) {
729*9880d681SAndroid Build Coastguard Worker Instruction *UserI = cast<Instruction>(U);
730*9880d681SAndroid Build Coastguard Worker
731*9880d681SAndroid Build Coastguard Worker // If the user is a PHI, inspect its uses recursively.
732*9880d681SAndroid Build Coastguard Worker if (PHINode *UserPN = dyn_cast<PHINode>(UserI)) {
733*9880d681SAndroid Build Coastguard Worker if (PHIsInspected.insert(UserPN).second)
734*9880d681SAndroid Build Coastguard Worker PHIsToSlice.push_back(UserPN);
735*9880d681SAndroid Build Coastguard Worker continue;
736*9880d681SAndroid Build Coastguard Worker }
737*9880d681SAndroid Build Coastguard Worker
738*9880d681SAndroid Build Coastguard Worker // Truncates are always ok.
739*9880d681SAndroid Build Coastguard Worker if (isa<TruncInst>(UserI)) {
740*9880d681SAndroid Build Coastguard Worker PHIUsers.push_back(PHIUsageRecord(PHIId, 0, UserI));
741*9880d681SAndroid Build Coastguard Worker continue;
742*9880d681SAndroid Build Coastguard Worker }
743*9880d681SAndroid Build Coastguard Worker
744*9880d681SAndroid Build Coastguard Worker // Otherwise it must be a lshr which can only be used by one trunc.
745*9880d681SAndroid Build Coastguard Worker if (UserI->getOpcode() != Instruction::LShr ||
746*9880d681SAndroid Build Coastguard Worker !UserI->hasOneUse() || !isa<TruncInst>(UserI->user_back()) ||
747*9880d681SAndroid Build Coastguard Worker !isa<ConstantInt>(UserI->getOperand(1)))
748*9880d681SAndroid Build Coastguard Worker return nullptr;
749*9880d681SAndroid Build Coastguard Worker
750*9880d681SAndroid Build Coastguard Worker unsigned Shift = cast<ConstantInt>(UserI->getOperand(1))->getZExtValue();
751*9880d681SAndroid Build Coastguard Worker PHIUsers.push_back(PHIUsageRecord(PHIId, Shift, UserI->user_back()));
752*9880d681SAndroid Build Coastguard Worker }
753*9880d681SAndroid Build Coastguard Worker }
754*9880d681SAndroid Build Coastguard Worker
755*9880d681SAndroid Build Coastguard Worker // If we have no users, they must be all self uses, just nuke the PHI.
756*9880d681SAndroid Build Coastguard Worker if (PHIUsers.empty())
757*9880d681SAndroid Build Coastguard Worker return replaceInstUsesWith(FirstPhi, UndefValue::get(FirstPhi.getType()));
758*9880d681SAndroid Build Coastguard Worker
759*9880d681SAndroid Build Coastguard Worker // If this phi node is transformable, create new PHIs for all the pieces
760*9880d681SAndroid Build Coastguard Worker // extracted out of it. First, sort the users by their offset and size.
761*9880d681SAndroid Build Coastguard Worker array_pod_sort(PHIUsers.begin(), PHIUsers.end());
762*9880d681SAndroid Build Coastguard Worker
763*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "SLICING UP PHI: " << FirstPhi << '\n';
764*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = PHIsToSlice.size(); i != e; ++i)
765*9880d681SAndroid Build Coastguard Worker dbgs() << "AND USER PHI #" << i << ": " << *PHIsToSlice[i] << '\n';
766*9880d681SAndroid Build Coastguard Worker );
767*9880d681SAndroid Build Coastguard Worker
768*9880d681SAndroid Build Coastguard Worker // PredValues - This is a temporary used when rewriting PHI nodes. It is
769*9880d681SAndroid Build Coastguard Worker // hoisted out here to avoid construction/destruction thrashing.
770*9880d681SAndroid Build Coastguard Worker DenseMap<BasicBlock*, Value*> PredValues;
771*9880d681SAndroid Build Coastguard Worker
772*9880d681SAndroid Build Coastguard Worker // ExtractedVals - Each new PHI we introduce is saved here so we don't
773*9880d681SAndroid Build Coastguard Worker // introduce redundant PHIs.
774*9880d681SAndroid Build Coastguard Worker DenseMap<LoweredPHIRecord, PHINode*> ExtractedVals;
775*9880d681SAndroid Build Coastguard Worker
776*9880d681SAndroid Build Coastguard Worker for (unsigned UserI = 0, UserE = PHIUsers.size(); UserI != UserE; ++UserI) {
777*9880d681SAndroid Build Coastguard Worker unsigned PHIId = PHIUsers[UserI].PHIId;
778*9880d681SAndroid Build Coastguard Worker PHINode *PN = PHIsToSlice[PHIId];
779*9880d681SAndroid Build Coastguard Worker unsigned Offset = PHIUsers[UserI].Shift;
780*9880d681SAndroid Build Coastguard Worker Type *Ty = PHIUsers[UserI].Inst->getType();
781*9880d681SAndroid Build Coastguard Worker
782*9880d681SAndroid Build Coastguard Worker PHINode *EltPHI;
783*9880d681SAndroid Build Coastguard Worker
784*9880d681SAndroid Build Coastguard Worker // If we've already lowered a user like this, reuse the previously lowered
785*9880d681SAndroid Build Coastguard Worker // value.
786*9880d681SAndroid Build Coastguard Worker if ((EltPHI = ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)]) == nullptr) {
787*9880d681SAndroid Build Coastguard Worker
788*9880d681SAndroid Build Coastguard Worker // Otherwise, Create the new PHI node for this user.
789*9880d681SAndroid Build Coastguard Worker EltPHI = PHINode::Create(Ty, PN->getNumIncomingValues(),
790*9880d681SAndroid Build Coastguard Worker PN->getName()+".off"+Twine(Offset), PN);
791*9880d681SAndroid Build Coastguard Worker assert(EltPHI->getType() != PN->getType() &&
792*9880d681SAndroid Build Coastguard Worker "Truncate didn't shrink phi?");
793*9880d681SAndroid Build Coastguard Worker
794*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
795*9880d681SAndroid Build Coastguard Worker BasicBlock *Pred = PN->getIncomingBlock(i);
796*9880d681SAndroid Build Coastguard Worker Value *&PredVal = PredValues[Pred];
797*9880d681SAndroid Build Coastguard Worker
798*9880d681SAndroid Build Coastguard Worker // If we already have a value for this predecessor, reuse it.
799*9880d681SAndroid Build Coastguard Worker if (PredVal) {
800*9880d681SAndroid Build Coastguard Worker EltPHI->addIncoming(PredVal, Pred);
801*9880d681SAndroid Build Coastguard Worker continue;
802*9880d681SAndroid Build Coastguard Worker }
803*9880d681SAndroid Build Coastguard Worker
804*9880d681SAndroid Build Coastguard Worker // Handle the PHI self-reuse case.
805*9880d681SAndroid Build Coastguard Worker Value *InVal = PN->getIncomingValue(i);
806*9880d681SAndroid Build Coastguard Worker if (InVal == PN) {
807*9880d681SAndroid Build Coastguard Worker PredVal = EltPHI;
808*9880d681SAndroid Build Coastguard Worker EltPHI->addIncoming(PredVal, Pred);
809*9880d681SAndroid Build Coastguard Worker continue;
810*9880d681SAndroid Build Coastguard Worker }
811*9880d681SAndroid Build Coastguard Worker
812*9880d681SAndroid Build Coastguard Worker if (PHINode *InPHI = dyn_cast<PHINode>(PN)) {
813*9880d681SAndroid Build Coastguard Worker // If the incoming value was a PHI, and if it was one of the PHIs we
814*9880d681SAndroid Build Coastguard Worker // already rewrote it, just use the lowered value.
815*9880d681SAndroid Build Coastguard Worker if (Value *Res = ExtractedVals[LoweredPHIRecord(InPHI, Offset, Ty)]) {
816*9880d681SAndroid Build Coastguard Worker PredVal = Res;
817*9880d681SAndroid Build Coastguard Worker EltPHI->addIncoming(PredVal, Pred);
818*9880d681SAndroid Build Coastguard Worker continue;
819*9880d681SAndroid Build Coastguard Worker }
820*9880d681SAndroid Build Coastguard Worker }
821*9880d681SAndroid Build Coastguard Worker
822*9880d681SAndroid Build Coastguard Worker // Otherwise, do an extract in the predecessor.
823*9880d681SAndroid Build Coastguard Worker Builder->SetInsertPoint(Pred->getTerminator());
824*9880d681SAndroid Build Coastguard Worker Value *Res = InVal;
825*9880d681SAndroid Build Coastguard Worker if (Offset)
826*9880d681SAndroid Build Coastguard Worker Res = Builder->CreateLShr(Res, ConstantInt::get(InVal->getType(),
827*9880d681SAndroid Build Coastguard Worker Offset), "extract");
828*9880d681SAndroid Build Coastguard Worker Res = Builder->CreateTrunc(Res, Ty, "extract.t");
829*9880d681SAndroid Build Coastguard Worker PredVal = Res;
830*9880d681SAndroid Build Coastguard Worker EltPHI->addIncoming(Res, Pred);
831*9880d681SAndroid Build Coastguard Worker
832*9880d681SAndroid Build Coastguard Worker // If the incoming value was a PHI, and if it was one of the PHIs we are
833*9880d681SAndroid Build Coastguard Worker // rewriting, we will ultimately delete the code we inserted. This
834*9880d681SAndroid Build Coastguard Worker // means we need to revisit that PHI to make sure we extract out the
835*9880d681SAndroid Build Coastguard Worker // needed piece.
836*9880d681SAndroid Build Coastguard Worker if (PHINode *OldInVal = dyn_cast<PHINode>(PN->getIncomingValue(i)))
837*9880d681SAndroid Build Coastguard Worker if (PHIsInspected.count(OldInVal)) {
838*9880d681SAndroid Build Coastguard Worker unsigned RefPHIId = std::find(PHIsToSlice.begin(),PHIsToSlice.end(),
839*9880d681SAndroid Build Coastguard Worker OldInVal)-PHIsToSlice.begin();
840*9880d681SAndroid Build Coastguard Worker PHIUsers.push_back(PHIUsageRecord(RefPHIId, Offset,
841*9880d681SAndroid Build Coastguard Worker cast<Instruction>(Res)));
842*9880d681SAndroid Build Coastguard Worker ++UserE;
843*9880d681SAndroid Build Coastguard Worker }
844*9880d681SAndroid Build Coastguard Worker }
845*9880d681SAndroid Build Coastguard Worker PredValues.clear();
846*9880d681SAndroid Build Coastguard Worker
847*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " Made element PHI for offset " << Offset << ": "
848*9880d681SAndroid Build Coastguard Worker << *EltPHI << '\n');
849*9880d681SAndroid Build Coastguard Worker ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)] = EltPHI;
850*9880d681SAndroid Build Coastguard Worker }
851*9880d681SAndroid Build Coastguard Worker
852*9880d681SAndroid Build Coastguard Worker // Replace the use of this piece with the PHI node.
853*9880d681SAndroid Build Coastguard Worker replaceInstUsesWith(*PHIUsers[UserI].Inst, EltPHI);
854*9880d681SAndroid Build Coastguard Worker }
855*9880d681SAndroid Build Coastguard Worker
856*9880d681SAndroid Build Coastguard Worker // Replace all the remaining uses of the PHI nodes (self uses and the lshrs)
857*9880d681SAndroid Build Coastguard Worker // with undefs.
858*9880d681SAndroid Build Coastguard Worker Value *Undef = UndefValue::get(FirstPhi.getType());
859*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = PHIsToSlice.size(); i != e; ++i)
860*9880d681SAndroid Build Coastguard Worker replaceInstUsesWith(*PHIsToSlice[i], Undef);
861*9880d681SAndroid Build Coastguard Worker return replaceInstUsesWith(FirstPhi, Undef);
862*9880d681SAndroid Build Coastguard Worker }
863*9880d681SAndroid Build Coastguard Worker
864*9880d681SAndroid Build Coastguard Worker // PHINode simplification
865*9880d681SAndroid Build Coastguard Worker //
visitPHINode(PHINode & PN)866*9880d681SAndroid Build Coastguard Worker Instruction *InstCombiner::visitPHINode(PHINode &PN) {
867*9880d681SAndroid Build Coastguard Worker if (Value *V = SimplifyInstruction(&PN, DL, TLI, DT, AC))
868*9880d681SAndroid Build Coastguard Worker return replaceInstUsesWith(PN, V);
869*9880d681SAndroid Build Coastguard Worker
870*9880d681SAndroid Build Coastguard Worker if (Instruction *Result = FoldPHIArgZextsIntoPHI(PN))
871*9880d681SAndroid Build Coastguard Worker return Result;
872*9880d681SAndroid Build Coastguard Worker
873*9880d681SAndroid Build Coastguard Worker // If all PHI operands are the same operation, pull them through the PHI,
874*9880d681SAndroid Build Coastguard Worker // reducing code size.
875*9880d681SAndroid Build Coastguard Worker if (isa<Instruction>(PN.getIncomingValue(0)) &&
876*9880d681SAndroid Build Coastguard Worker isa<Instruction>(PN.getIncomingValue(1)) &&
877*9880d681SAndroid Build Coastguard Worker cast<Instruction>(PN.getIncomingValue(0))->getOpcode() ==
878*9880d681SAndroid Build Coastguard Worker cast<Instruction>(PN.getIncomingValue(1))->getOpcode() &&
879*9880d681SAndroid Build Coastguard Worker // FIXME: The hasOneUse check will fail for PHIs that use the value more
880*9880d681SAndroid Build Coastguard Worker // than themselves more than once.
881*9880d681SAndroid Build Coastguard Worker PN.getIncomingValue(0)->hasOneUse())
882*9880d681SAndroid Build Coastguard Worker if (Instruction *Result = FoldPHIArgOpIntoPHI(PN))
883*9880d681SAndroid Build Coastguard Worker return Result;
884*9880d681SAndroid Build Coastguard Worker
885*9880d681SAndroid Build Coastguard Worker // If this is a trivial cycle in the PHI node graph, remove it. Basically, if
886*9880d681SAndroid Build Coastguard Worker // this PHI only has a single use (a PHI), and if that PHI only has one use (a
887*9880d681SAndroid Build Coastguard Worker // PHI)... break the cycle.
888*9880d681SAndroid Build Coastguard Worker if (PN.hasOneUse()) {
889*9880d681SAndroid Build Coastguard Worker Instruction *PHIUser = cast<Instruction>(PN.user_back());
890*9880d681SAndroid Build Coastguard Worker if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) {
891*9880d681SAndroid Build Coastguard Worker SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs;
892*9880d681SAndroid Build Coastguard Worker PotentiallyDeadPHIs.insert(&PN);
893*9880d681SAndroid Build Coastguard Worker if (DeadPHICycle(PU, PotentiallyDeadPHIs))
894*9880d681SAndroid Build Coastguard Worker return replaceInstUsesWith(PN, UndefValue::get(PN.getType()));
895*9880d681SAndroid Build Coastguard Worker }
896*9880d681SAndroid Build Coastguard Worker
897*9880d681SAndroid Build Coastguard Worker // If this phi has a single use, and if that use just computes a value for
898*9880d681SAndroid Build Coastguard Worker // the next iteration of a loop, delete the phi. This occurs with unused
899*9880d681SAndroid Build Coastguard Worker // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this
900*9880d681SAndroid Build Coastguard Worker // common case here is good because the only other things that catch this
901*9880d681SAndroid Build Coastguard Worker // are induction variable analysis (sometimes) and ADCE, which is only run
902*9880d681SAndroid Build Coastguard Worker // late.
903*9880d681SAndroid Build Coastguard Worker if (PHIUser->hasOneUse() &&
904*9880d681SAndroid Build Coastguard Worker (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) &&
905*9880d681SAndroid Build Coastguard Worker PHIUser->user_back() == &PN) {
906*9880d681SAndroid Build Coastguard Worker return replaceInstUsesWith(PN, UndefValue::get(PN.getType()));
907*9880d681SAndroid Build Coastguard Worker }
908*9880d681SAndroid Build Coastguard Worker // When a PHI is used only to be compared with zero, it is safe to replace
909*9880d681SAndroid Build Coastguard Worker // an incoming value proved as known nonzero with any non-zero constant.
910*9880d681SAndroid Build Coastguard Worker // For example, in the code below, the incoming value %v can be replaced
911*9880d681SAndroid Build Coastguard Worker // with any non-zero constant based on the fact that the PHI is only used to
912*9880d681SAndroid Build Coastguard Worker // be compared with zero and %v is a known non-zero value:
913*9880d681SAndroid Build Coastguard Worker // %v = select %cond, 1, 2
914*9880d681SAndroid Build Coastguard Worker // %p = phi [%v, BB] ...
915*9880d681SAndroid Build Coastguard Worker // icmp eq, %p, 0
916*9880d681SAndroid Build Coastguard Worker auto *CmpInst = dyn_cast<ICmpInst>(PHIUser);
917*9880d681SAndroid Build Coastguard Worker // FIXME: To be simple, handle only integer type for now.
918*9880d681SAndroid Build Coastguard Worker if (CmpInst && isa<IntegerType>(PN.getType()) && CmpInst->isEquality() &&
919*9880d681SAndroid Build Coastguard Worker match(CmpInst->getOperand(1), m_Zero())) {
920*9880d681SAndroid Build Coastguard Worker ConstantInt *NonZeroConst = nullptr;
921*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
922*9880d681SAndroid Build Coastguard Worker Instruction *CtxI = PN.getIncomingBlock(i)->getTerminator();
923*9880d681SAndroid Build Coastguard Worker Value *VA = PN.getIncomingValue(i);
924*9880d681SAndroid Build Coastguard Worker if (isKnownNonZero(VA, DL, 0, AC, CtxI, DT)) {
925*9880d681SAndroid Build Coastguard Worker if (!NonZeroConst)
926*9880d681SAndroid Build Coastguard Worker NonZeroConst = GetAnyNonZeroConstInt(PN);
927*9880d681SAndroid Build Coastguard Worker PN.setIncomingValue(i, NonZeroConst);
928*9880d681SAndroid Build Coastguard Worker }
929*9880d681SAndroid Build Coastguard Worker }
930*9880d681SAndroid Build Coastguard Worker }
931*9880d681SAndroid Build Coastguard Worker }
932*9880d681SAndroid Build Coastguard Worker
933*9880d681SAndroid Build Coastguard Worker // We sometimes end up with phi cycles that non-obviously end up being the
934*9880d681SAndroid Build Coastguard Worker // same value, for example:
935*9880d681SAndroid Build Coastguard Worker // z = some value; x = phi (y, z); y = phi (x, z)
936*9880d681SAndroid Build Coastguard Worker // where the phi nodes don't necessarily need to be in the same block. Do a
937*9880d681SAndroid Build Coastguard Worker // quick check to see if the PHI node only contains a single non-phi value, if
938*9880d681SAndroid Build Coastguard Worker // so, scan to see if the phi cycle is actually equal to that value.
939*9880d681SAndroid Build Coastguard Worker {
940*9880d681SAndroid Build Coastguard Worker unsigned InValNo = 0, NumIncomingVals = PN.getNumIncomingValues();
941*9880d681SAndroid Build Coastguard Worker // Scan for the first non-phi operand.
942*9880d681SAndroid Build Coastguard Worker while (InValNo != NumIncomingVals &&
943*9880d681SAndroid Build Coastguard Worker isa<PHINode>(PN.getIncomingValue(InValNo)))
944*9880d681SAndroid Build Coastguard Worker ++InValNo;
945*9880d681SAndroid Build Coastguard Worker
946*9880d681SAndroid Build Coastguard Worker if (InValNo != NumIncomingVals) {
947*9880d681SAndroid Build Coastguard Worker Value *NonPhiInVal = PN.getIncomingValue(InValNo);
948*9880d681SAndroid Build Coastguard Worker
949*9880d681SAndroid Build Coastguard Worker // Scan the rest of the operands to see if there are any conflicts, if so
950*9880d681SAndroid Build Coastguard Worker // there is no need to recursively scan other phis.
951*9880d681SAndroid Build Coastguard Worker for (++InValNo; InValNo != NumIncomingVals; ++InValNo) {
952*9880d681SAndroid Build Coastguard Worker Value *OpVal = PN.getIncomingValue(InValNo);
953*9880d681SAndroid Build Coastguard Worker if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal))
954*9880d681SAndroid Build Coastguard Worker break;
955*9880d681SAndroid Build Coastguard Worker }
956*9880d681SAndroid Build Coastguard Worker
957*9880d681SAndroid Build Coastguard Worker // If we scanned over all operands, then we have one unique value plus
958*9880d681SAndroid Build Coastguard Worker // phi values. Scan PHI nodes to see if they all merge in each other or
959*9880d681SAndroid Build Coastguard Worker // the value.
960*9880d681SAndroid Build Coastguard Worker if (InValNo == NumIncomingVals) {
961*9880d681SAndroid Build Coastguard Worker SmallPtrSet<PHINode*, 16> ValueEqualPHIs;
962*9880d681SAndroid Build Coastguard Worker if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs))
963*9880d681SAndroid Build Coastguard Worker return replaceInstUsesWith(PN, NonPhiInVal);
964*9880d681SAndroid Build Coastguard Worker }
965*9880d681SAndroid Build Coastguard Worker }
966*9880d681SAndroid Build Coastguard Worker }
967*9880d681SAndroid Build Coastguard Worker
968*9880d681SAndroid Build Coastguard Worker // If there are multiple PHIs, sort their operands so that they all list
969*9880d681SAndroid Build Coastguard Worker // the blocks in the same order. This will help identical PHIs be eliminated
970*9880d681SAndroid Build Coastguard Worker // by other passes. Other passes shouldn't depend on this for correctness
971*9880d681SAndroid Build Coastguard Worker // however.
972*9880d681SAndroid Build Coastguard Worker PHINode *FirstPN = cast<PHINode>(PN.getParent()->begin());
973*9880d681SAndroid Build Coastguard Worker if (&PN != FirstPN)
974*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = FirstPN->getNumIncomingValues(); i != e; ++i) {
975*9880d681SAndroid Build Coastguard Worker BasicBlock *BBA = PN.getIncomingBlock(i);
976*9880d681SAndroid Build Coastguard Worker BasicBlock *BBB = FirstPN->getIncomingBlock(i);
977*9880d681SAndroid Build Coastguard Worker if (BBA != BBB) {
978*9880d681SAndroid Build Coastguard Worker Value *VA = PN.getIncomingValue(i);
979*9880d681SAndroid Build Coastguard Worker unsigned j = PN.getBasicBlockIndex(BBB);
980*9880d681SAndroid Build Coastguard Worker Value *VB = PN.getIncomingValue(j);
981*9880d681SAndroid Build Coastguard Worker PN.setIncomingBlock(i, BBB);
982*9880d681SAndroid Build Coastguard Worker PN.setIncomingValue(i, VB);
983*9880d681SAndroid Build Coastguard Worker PN.setIncomingBlock(j, BBA);
984*9880d681SAndroid Build Coastguard Worker PN.setIncomingValue(j, VA);
985*9880d681SAndroid Build Coastguard Worker // NOTE: Instcombine normally would want us to "return &PN" if we
986*9880d681SAndroid Build Coastguard Worker // modified any of the operands of an instruction. However, since we
987*9880d681SAndroid Build Coastguard Worker // aren't adding or removing uses (just rearranging them) we don't do
988*9880d681SAndroid Build Coastguard Worker // this in this case.
989*9880d681SAndroid Build Coastguard Worker }
990*9880d681SAndroid Build Coastguard Worker }
991*9880d681SAndroid Build Coastguard Worker
992*9880d681SAndroid Build Coastguard Worker // If this is an integer PHI and we know that it has an illegal type, see if
993*9880d681SAndroid Build Coastguard Worker // it is only used by trunc or trunc(lshr) operations. If so, we split the
994*9880d681SAndroid Build Coastguard Worker // PHI into the various pieces being extracted. This sort of thing is
995*9880d681SAndroid Build Coastguard Worker // introduced when SROA promotes an aggregate to a single large integer type.
996*9880d681SAndroid Build Coastguard Worker if (PN.getType()->isIntegerTy() &&
997*9880d681SAndroid Build Coastguard Worker !DL.isLegalInteger(PN.getType()->getPrimitiveSizeInBits()))
998*9880d681SAndroid Build Coastguard Worker if (Instruction *Res = SliceUpIllegalIntegerPHI(PN))
999*9880d681SAndroid Build Coastguard Worker return Res;
1000*9880d681SAndroid Build Coastguard Worker
1001*9880d681SAndroid Build Coastguard Worker return nullptr;
1002*9880d681SAndroid Build Coastguard Worker }
1003