1*9880d681SAndroid Build Coastguard Worker //===- PHITransAddr.cpp - PHI Translation for Addresses -------------------===//
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 PHITransAddr class.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/PHITransAddr.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/InstructionSimplify.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ValueTracking.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Dominators.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
23*9880d681SAndroid Build Coastguard Worker using namespace llvm;
24*9880d681SAndroid Build Coastguard Worker
CanPHITrans(Instruction * Inst)25*9880d681SAndroid Build Coastguard Worker static bool CanPHITrans(Instruction *Inst) {
26*9880d681SAndroid Build Coastguard Worker if (isa<PHINode>(Inst) ||
27*9880d681SAndroid Build Coastguard Worker isa<GetElementPtrInst>(Inst))
28*9880d681SAndroid Build Coastguard Worker return true;
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard Worker if (isa<CastInst>(Inst) &&
31*9880d681SAndroid Build Coastguard Worker isSafeToSpeculativelyExecute(Inst))
32*9880d681SAndroid Build Coastguard Worker return true;
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Worker if (Inst->getOpcode() == Instruction::Add &&
35*9880d681SAndroid Build Coastguard Worker isa<ConstantInt>(Inst->getOperand(1)))
36*9880d681SAndroid Build Coastguard Worker return true;
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker // cerr << "MEMDEP: Could not PHI translate: " << *Pointer;
39*9880d681SAndroid Build Coastguard Worker // if (isa<BitCastInst>(PtrInst) || isa<GetElementPtrInst>(PtrInst))
40*9880d681SAndroid Build Coastguard Worker // cerr << "OP:\t\t\t\t" << *PtrInst->getOperand(0);
41*9880d681SAndroid Build Coastguard Worker return false;
42*9880d681SAndroid Build Coastguard Worker }
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const45*9880d681SAndroid Build Coastguard Worker LLVM_DUMP_METHOD void PHITransAddr::dump() const {
46*9880d681SAndroid Build Coastguard Worker if (!Addr) {
47*9880d681SAndroid Build Coastguard Worker dbgs() << "PHITransAddr: null\n";
48*9880d681SAndroid Build Coastguard Worker return;
49*9880d681SAndroid Build Coastguard Worker }
50*9880d681SAndroid Build Coastguard Worker dbgs() << "PHITransAddr: " << *Addr << "\n";
51*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
52*9880d681SAndroid Build Coastguard Worker dbgs() << " Input #" << i << " is " << *InstInputs[i] << "\n";
53*9880d681SAndroid Build Coastguard Worker }
54*9880d681SAndroid Build Coastguard Worker #endif
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Worker
VerifySubExpr(Value * Expr,SmallVectorImpl<Instruction * > & InstInputs)57*9880d681SAndroid Build Coastguard Worker static bool VerifySubExpr(Value *Expr,
58*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<Instruction*> &InstInputs) {
59*9880d681SAndroid Build Coastguard Worker // If this is a non-instruction value, there is nothing to do.
60*9880d681SAndroid Build Coastguard Worker Instruction *I = dyn_cast<Instruction>(Expr);
61*9880d681SAndroid Build Coastguard Worker if (!I) return true;
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard Worker // If it's an instruction, it is either in Tmp or its operands recursively
64*9880d681SAndroid Build Coastguard Worker // are.
65*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<Instruction*>::iterator Entry =
66*9880d681SAndroid Build Coastguard Worker std::find(InstInputs.begin(), InstInputs.end(), I);
67*9880d681SAndroid Build Coastguard Worker if (Entry != InstInputs.end()) {
68*9880d681SAndroid Build Coastguard Worker InstInputs.erase(Entry);
69*9880d681SAndroid Build Coastguard Worker return true;
70*9880d681SAndroid Build Coastguard Worker }
71*9880d681SAndroid Build Coastguard Worker
72*9880d681SAndroid Build Coastguard Worker // If it isn't in the InstInputs list it is a subexpr incorporated into the
73*9880d681SAndroid Build Coastguard Worker // address. Sanity check that it is phi translatable.
74*9880d681SAndroid Build Coastguard Worker if (!CanPHITrans(I)) {
75*9880d681SAndroid Build Coastguard Worker errs() << "Instruction in PHITransAddr is not phi-translatable:\n";
76*9880d681SAndroid Build Coastguard Worker errs() << *I << '\n';
77*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Either something is missing from InstInputs or "
78*9880d681SAndroid Build Coastguard Worker "CanPHITrans is wrong.");
79*9880d681SAndroid Build Coastguard Worker }
80*9880d681SAndroid Build Coastguard Worker
81*9880d681SAndroid Build Coastguard Worker // Validate the operands of the instruction.
82*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
83*9880d681SAndroid Build Coastguard Worker if (!VerifySubExpr(I->getOperand(i), InstInputs))
84*9880d681SAndroid Build Coastguard Worker return false;
85*9880d681SAndroid Build Coastguard Worker
86*9880d681SAndroid Build Coastguard Worker return true;
87*9880d681SAndroid Build Coastguard Worker }
88*9880d681SAndroid Build Coastguard Worker
89*9880d681SAndroid Build Coastguard Worker /// Verify - Check internal consistency of this data structure. If the
90*9880d681SAndroid Build Coastguard Worker /// structure is valid, it returns true. If invalid, it prints errors and
91*9880d681SAndroid Build Coastguard Worker /// returns false.
Verify() const92*9880d681SAndroid Build Coastguard Worker bool PHITransAddr::Verify() const {
93*9880d681SAndroid Build Coastguard Worker if (!Addr) return true;
94*9880d681SAndroid Build Coastguard Worker
95*9880d681SAndroid Build Coastguard Worker SmallVector<Instruction*, 8> Tmp(InstInputs.begin(), InstInputs.end());
96*9880d681SAndroid Build Coastguard Worker
97*9880d681SAndroid Build Coastguard Worker if (!VerifySubExpr(Addr, Tmp))
98*9880d681SAndroid Build Coastguard Worker return false;
99*9880d681SAndroid Build Coastguard Worker
100*9880d681SAndroid Build Coastguard Worker if (!Tmp.empty()) {
101*9880d681SAndroid Build Coastguard Worker errs() << "PHITransAddr contains extra instructions:\n";
102*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
103*9880d681SAndroid Build Coastguard Worker errs() << " InstInput #" << i << " is " << *InstInputs[i] << "\n";
104*9880d681SAndroid Build Coastguard Worker llvm_unreachable("This is unexpected.");
105*9880d681SAndroid Build Coastguard Worker }
106*9880d681SAndroid Build Coastguard Worker
107*9880d681SAndroid Build Coastguard Worker // a-ok.
108*9880d681SAndroid Build Coastguard Worker return true;
109*9880d681SAndroid Build Coastguard Worker }
110*9880d681SAndroid Build Coastguard Worker
111*9880d681SAndroid Build Coastguard Worker
112*9880d681SAndroid Build Coastguard Worker /// IsPotentiallyPHITranslatable - If this needs PHI translation, return true
113*9880d681SAndroid Build Coastguard Worker /// if we have some hope of doing it. This should be used as a filter to
114*9880d681SAndroid Build Coastguard Worker /// avoid calling PHITranslateValue in hopeless situations.
IsPotentiallyPHITranslatable() const115*9880d681SAndroid Build Coastguard Worker bool PHITransAddr::IsPotentiallyPHITranslatable() const {
116*9880d681SAndroid Build Coastguard Worker // If the input value is not an instruction, or if it is not defined in CurBB,
117*9880d681SAndroid Build Coastguard Worker // then we don't need to phi translate it.
118*9880d681SAndroid Build Coastguard Worker Instruction *Inst = dyn_cast<Instruction>(Addr);
119*9880d681SAndroid Build Coastguard Worker return !Inst || CanPHITrans(Inst);
120*9880d681SAndroid Build Coastguard Worker }
121*9880d681SAndroid Build Coastguard Worker
122*9880d681SAndroid Build Coastguard Worker
RemoveInstInputs(Value * V,SmallVectorImpl<Instruction * > & InstInputs)123*9880d681SAndroid Build Coastguard Worker static void RemoveInstInputs(Value *V,
124*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<Instruction*> &InstInputs) {
125*9880d681SAndroid Build Coastguard Worker Instruction *I = dyn_cast<Instruction>(V);
126*9880d681SAndroid Build Coastguard Worker if (!I) return;
127*9880d681SAndroid Build Coastguard Worker
128*9880d681SAndroid Build Coastguard Worker // If the instruction is in the InstInputs list, remove it.
129*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<Instruction*>::iterator Entry =
130*9880d681SAndroid Build Coastguard Worker std::find(InstInputs.begin(), InstInputs.end(), I);
131*9880d681SAndroid Build Coastguard Worker if (Entry != InstInputs.end()) {
132*9880d681SAndroid Build Coastguard Worker InstInputs.erase(Entry);
133*9880d681SAndroid Build Coastguard Worker return;
134*9880d681SAndroid Build Coastguard Worker }
135*9880d681SAndroid Build Coastguard Worker
136*9880d681SAndroid Build Coastguard Worker assert(!isa<PHINode>(I) && "Error, removing something that isn't an input");
137*9880d681SAndroid Build Coastguard Worker
138*9880d681SAndroid Build Coastguard Worker // Otherwise, it must have instruction inputs itself. Zap them recursively.
139*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
140*9880d681SAndroid Build Coastguard Worker if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
141*9880d681SAndroid Build Coastguard Worker RemoveInstInputs(Op, InstInputs);
142*9880d681SAndroid Build Coastguard Worker }
143*9880d681SAndroid Build Coastguard Worker }
144*9880d681SAndroid Build Coastguard Worker
PHITranslateSubExpr(Value * V,BasicBlock * CurBB,BasicBlock * PredBB,const DominatorTree * DT)145*9880d681SAndroid Build Coastguard Worker Value *PHITransAddr::PHITranslateSubExpr(Value *V, BasicBlock *CurBB,
146*9880d681SAndroid Build Coastguard Worker BasicBlock *PredBB,
147*9880d681SAndroid Build Coastguard Worker const DominatorTree *DT) {
148*9880d681SAndroid Build Coastguard Worker // If this is a non-instruction value, it can't require PHI translation.
149*9880d681SAndroid Build Coastguard Worker Instruction *Inst = dyn_cast<Instruction>(V);
150*9880d681SAndroid Build Coastguard Worker if (!Inst) return V;
151*9880d681SAndroid Build Coastguard Worker
152*9880d681SAndroid Build Coastguard Worker // Determine whether 'Inst' is an input to our PHI translatable expression.
153*9880d681SAndroid Build Coastguard Worker bool isInput =
154*9880d681SAndroid Build Coastguard Worker std::find(InstInputs.begin(), InstInputs.end(), Inst) != InstInputs.end();
155*9880d681SAndroid Build Coastguard Worker
156*9880d681SAndroid Build Coastguard Worker // Handle inputs instructions if needed.
157*9880d681SAndroid Build Coastguard Worker if (isInput) {
158*9880d681SAndroid Build Coastguard Worker if (Inst->getParent() != CurBB) {
159*9880d681SAndroid Build Coastguard Worker // If it is an input defined in a different block, then it remains an
160*9880d681SAndroid Build Coastguard Worker // input.
161*9880d681SAndroid Build Coastguard Worker return Inst;
162*9880d681SAndroid Build Coastguard Worker }
163*9880d681SAndroid Build Coastguard Worker
164*9880d681SAndroid Build Coastguard Worker // If 'Inst' is defined in this block and is an input that needs to be phi
165*9880d681SAndroid Build Coastguard Worker // translated, we need to incorporate the value into the expression or fail.
166*9880d681SAndroid Build Coastguard Worker
167*9880d681SAndroid Build Coastguard Worker // In either case, the instruction itself isn't an input any longer.
168*9880d681SAndroid Build Coastguard Worker InstInputs.erase(std::find(InstInputs.begin(), InstInputs.end(), Inst));
169*9880d681SAndroid Build Coastguard Worker
170*9880d681SAndroid Build Coastguard Worker // If this is a PHI, go ahead and translate it.
171*9880d681SAndroid Build Coastguard Worker if (PHINode *PN = dyn_cast<PHINode>(Inst))
172*9880d681SAndroid Build Coastguard Worker return AddAsInput(PN->getIncomingValueForBlock(PredBB));
173*9880d681SAndroid Build Coastguard Worker
174*9880d681SAndroid Build Coastguard Worker // If this is a non-phi value, and it is analyzable, we can incorporate it
175*9880d681SAndroid Build Coastguard Worker // into the expression by making all instruction operands be inputs.
176*9880d681SAndroid Build Coastguard Worker if (!CanPHITrans(Inst))
177*9880d681SAndroid Build Coastguard Worker return nullptr;
178*9880d681SAndroid Build Coastguard Worker
179*9880d681SAndroid Build Coastguard Worker // All instruction operands are now inputs (and of course, they may also be
180*9880d681SAndroid Build Coastguard Worker // defined in this block, so they may need to be phi translated themselves.
181*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i)
182*9880d681SAndroid Build Coastguard Worker if (Instruction *Op = dyn_cast<Instruction>(Inst->getOperand(i)))
183*9880d681SAndroid Build Coastguard Worker InstInputs.push_back(Op);
184*9880d681SAndroid Build Coastguard Worker }
185*9880d681SAndroid Build Coastguard Worker
186*9880d681SAndroid Build Coastguard Worker // Ok, it must be an intermediate result (either because it started that way
187*9880d681SAndroid Build Coastguard Worker // or because we just incorporated it into the expression). See if its
188*9880d681SAndroid Build Coastguard Worker // operands need to be phi translated, and if so, reconstruct it.
189*9880d681SAndroid Build Coastguard Worker
190*9880d681SAndroid Build Coastguard Worker if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
191*9880d681SAndroid Build Coastguard Worker if (!isSafeToSpeculativelyExecute(Cast)) return nullptr;
192*9880d681SAndroid Build Coastguard Worker Value *PHIIn = PHITranslateSubExpr(Cast->getOperand(0), CurBB, PredBB, DT);
193*9880d681SAndroid Build Coastguard Worker if (!PHIIn) return nullptr;
194*9880d681SAndroid Build Coastguard Worker if (PHIIn == Cast->getOperand(0))
195*9880d681SAndroid Build Coastguard Worker return Cast;
196*9880d681SAndroid Build Coastguard Worker
197*9880d681SAndroid Build Coastguard Worker // Find an available version of this cast.
198*9880d681SAndroid Build Coastguard Worker
199*9880d681SAndroid Build Coastguard Worker // Constants are trivial to find.
200*9880d681SAndroid Build Coastguard Worker if (Constant *C = dyn_cast<Constant>(PHIIn))
201*9880d681SAndroid Build Coastguard Worker return AddAsInput(ConstantExpr::getCast(Cast->getOpcode(),
202*9880d681SAndroid Build Coastguard Worker C, Cast->getType()));
203*9880d681SAndroid Build Coastguard Worker
204*9880d681SAndroid Build Coastguard Worker // Otherwise we have to see if a casted version of the incoming pointer
205*9880d681SAndroid Build Coastguard Worker // is available. If so, we can use it, otherwise we have to fail.
206*9880d681SAndroid Build Coastguard Worker for (User *U : PHIIn->users()) {
207*9880d681SAndroid Build Coastguard Worker if (CastInst *CastI = dyn_cast<CastInst>(U))
208*9880d681SAndroid Build Coastguard Worker if (CastI->getOpcode() == Cast->getOpcode() &&
209*9880d681SAndroid Build Coastguard Worker CastI->getType() == Cast->getType() &&
210*9880d681SAndroid Build Coastguard Worker (!DT || DT->dominates(CastI->getParent(), PredBB)))
211*9880d681SAndroid Build Coastguard Worker return CastI;
212*9880d681SAndroid Build Coastguard Worker }
213*9880d681SAndroid Build Coastguard Worker return nullptr;
214*9880d681SAndroid Build Coastguard Worker }
215*9880d681SAndroid Build Coastguard Worker
216*9880d681SAndroid Build Coastguard Worker // Handle getelementptr with at least one PHI translatable operand.
217*9880d681SAndroid Build Coastguard Worker if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
218*9880d681SAndroid Build Coastguard Worker SmallVector<Value*, 8> GEPOps;
219*9880d681SAndroid Build Coastguard Worker bool AnyChanged = false;
220*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
221*9880d681SAndroid Build Coastguard Worker Value *GEPOp = PHITranslateSubExpr(GEP->getOperand(i), CurBB, PredBB, DT);
222*9880d681SAndroid Build Coastguard Worker if (!GEPOp) return nullptr;
223*9880d681SAndroid Build Coastguard Worker
224*9880d681SAndroid Build Coastguard Worker AnyChanged |= GEPOp != GEP->getOperand(i);
225*9880d681SAndroid Build Coastguard Worker GEPOps.push_back(GEPOp);
226*9880d681SAndroid Build Coastguard Worker }
227*9880d681SAndroid Build Coastguard Worker
228*9880d681SAndroid Build Coastguard Worker if (!AnyChanged)
229*9880d681SAndroid Build Coastguard Worker return GEP;
230*9880d681SAndroid Build Coastguard Worker
231*9880d681SAndroid Build Coastguard Worker // Simplify the GEP to handle 'gep x, 0' -> x etc.
232*9880d681SAndroid Build Coastguard Worker if (Value *V = SimplifyGEPInst(GEP->getSourceElementType(),
233*9880d681SAndroid Build Coastguard Worker GEPOps, DL, TLI, DT, AC)) {
234*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
235*9880d681SAndroid Build Coastguard Worker RemoveInstInputs(GEPOps[i], InstInputs);
236*9880d681SAndroid Build Coastguard Worker
237*9880d681SAndroid Build Coastguard Worker return AddAsInput(V);
238*9880d681SAndroid Build Coastguard Worker }
239*9880d681SAndroid Build Coastguard Worker
240*9880d681SAndroid Build Coastguard Worker // Scan to see if we have this GEP available.
241*9880d681SAndroid Build Coastguard Worker Value *APHIOp = GEPOps[0];
242*9880d681SAndroid Build Coastguard Worker for (User *U : APHIOp->users()) {
243*9880d681SAndroid Build Coastguard Worker if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U))
244*9880d681SAndroid Build Coastguard Worker if (GEPI->getType() == GEP->getType() &&
245*9880d681SAndroid Build Coastguard Worker GEPI->getNumOperands() == GEPOps.size() &&
246*9880d681SAndroid Build Coastguard Worker GEPI->getParent()->getParent() == CurBB->getParent() &&
247*9880d681SAndroid Build Coastguard Worker (!DT || DT->dominates(GEPI->getParent(), PredBB))) {
248*9880d681SAndroid Build Coastguard Worker if (std::equal(GEPOps.begin(), GEPOps.end(), GEPI->op_begin()))
249*9880d681SAndroid Build Coastguard Worker return GEPI;
250*9880d681SAndroid Build Coastguard Worker }
251*9880d681SAndroid Build Coastguard Worker }
252*9880d681SAndroid Build Coastguard Worker return nullptr;
253*9880d681SAndroid Build Coastguard Worker }
254*9880d681SAndroid Build Coastguard Worker
255*9880d681SAndroid Build Coastguard Worker // Handle add with a constant RHS.
256*9880d681SAndroid Build Coastguard Worker if (Inst->getOpcode() == Instruction::Add &&
257*9880d681SAndroid Build Coastguard Worker isa<ConstantInt>(Inst->getOperand(1))) {
258*9880d681SAndroid Build Coastguard Worker // PHI translate the LHS.
259*9880d681SAndroid Build Coastguard Worker Constant *RHS = cast<ConstantInt>(Inst->getOperand(1));
260*9880d681SAndroid Build Coastguard Worker bool isNSW = cast<BinaryOperator>(Inst)->hasNoSignedWrap();
261*9880d681SAndroid Build Coastguard Worker bool isNUW = cast<BinaryOperator>(Inst)->hasNoUnsignedWrap();
262*9880d681SAndroid Build Coastguard Worker
263*9880d681SAndroid Build Coastguard Worker Value *LHS = PHITranslateSubExpr(Inst->getOperand(0), CurBB, PredBB, DT);
264*9880d681SAndroid Build Coastguard Worker if (!LHS) return nullptr;
265*9880d681SAndroid Build Coastguard Worker
266*9880d681SAndroid Build Coastguard Worker // If the PHI translated LHS is an add of a constant, fold the immediates.
267*9880d681SAndroid Build Coastguard Worker if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(LHS))
268*9880d681SAndroid Build Coastguard Worker if (BOp->getOpcode() == Instruction::Add)
269*9880d681SAndroid Build Coastguard Worker if (ConstantInt *CI = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
270*9880d681SAndroid Build Coastguard Worker LHS = BOp->getOperand(0);
271*9880d681SAndroid Build Coastguard Worker RHS = ConstantExpr::getAdd(RHS, CI);
272*9880d681SAndroid Build Coastguard Worker isNSW = isNUW = false;
273*9880d681SAndroid Build Coastguard Worker
274*9880d681SAndroid Build Coastguard Worker // If the old 'LHS' was an input, add the new 'LHS' as an input.
275*9880d681SAndroid Build Coastguard Worker if (std::find(InstInputs.begin(), InstInputs.end(), BOp) !=
276*9880d681SAndroid Build Coastguard Worker InstInputs.end()) {
277*9880d681SAndroid Build Coastguard Worker RemoveInstInputs(BOp, InstInputs);
278*9880d681SAndroid Build Coastguard Worker AddAsInput(LHS);
279*9880d681SAndroid Build Coastguard Worker }
280*9880d681SAndroid Build Coastguard Worker }
281*9880d681SAndroid Build Coastguard Worker
282*9880d681SAndroid Build Coastguard Worker // See if the add simplifies away.
283*9880d681SAndroid Build Coastguard Worker if (Value *Res = SimplifyAddInst(LHS, RHS, isNSW, isNUW, DL, TLI, DT, AC)) {
284*9880d681SAndroid Build Coastguard Worker // If we simplified the operands, the LHS is no longer an input, but Res
285*9880d681SAndroid Build Coastguard Worker // is.
286*9880d681SAndroid Build Coastguard Worker RemoveInstInputs(LHS, InstInputs);
287*9880d681SAndroid Build Coastguard Worker return AddAsInput(Res);
288*9880d681SAndroid Build Coastguard Worker }
289*9880d681SAndroid Build Coastguard Worker
290*9880d681SAndroid Build Coastguard Worker // If we didn't modify the add, just return it.
291*9880d681SAndroid Build Coastguard Worker if (LHS == Inst->getOperand(0) && RHS == Inst->getOperand(1))
292*9880d681SAndroid Build Coastguard Worker return Inst;
293*9880d681SAndroid Build Coastguard Worker
294*9880d681SAndroid Build Coastguard Worker // Otherwise, see if we have this add available somewhere.
295*9880d681SAndroid Build Coastguard Worker for (User *U : LHS->users()) {
296*9880d681SAndroid Build Coastguard Worker if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U))
297*9880d681SAndroid Build Coastguard Worker if (BO->getOpcode() == Instruction::Add &&
298*9880d681SAndroid Build Coastguard Worker BO->getOperand(0) == LHS && BO->getOperand(1) == RHS &&
299*9880d681SAndroid Build Coastguard Worker BO->getParent()->getParent() == CurBB->getParent() &&
300*9880d681SAndroid Build Coastguard Worker (!DT || DT->dominates(BO->getParent(), PredBB)))
301*9880d681SAndroid Build Coastguard Worker return BO;
302*9880d681SAndroid Build Coastguard Worker }
303*9880d681SAndroid Build Coastguard Worker
304*9880d681SAndroid Build Coastguard Worker return nullptr;
305*9880d681SAndroid Build Coastguard Worker }
306*9880d681SAndroid Build Coastguard Worker
307*9880d681SAndroid Build Coastguard Worker // Otherwise, we failed.
308*9880d681SAndroid Build Coastguard Worker return nullptr;
309*9880d681SAndroid Build Coastguard Worker }
310*9880d681SAndroid Build Coastguard Worker
311*9880d681SAndroid Build Coastguard Worker
312*9880d681SAndroid Build Coastguard Worker /// PHITranslateValue - PHI translate the current address up the CFG from
313*9880d681SAndroid Build Coastguard Worker /// CurBB to Pred, updating our state to reflect any needed changes. If
314*9880d681SAndroid Build Coastguard Worker /// 'MustDominate' is true, the translated value must dominate
315*9880d681SAndroid Build Coastguard Worker /// PredBB. This returns true on failure and sets Addr to null.
PHITranslateValue(BasicBlock * CurBB,BasicBlock * PredBB,const DominatorTree * DT,bool MustDominate)316*9880d681SAndroid Build Coastguard Worker bool PHITransAddr::PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB,
317*9880d681SAndroid Build Coastguard Worker const DominatorTree *DT,
318*9880d681SAndroid Build Coastguard Worker bool MustDominate) {
319*9880d681SAndroid Build Coastguard Worker assert(DT || !MustDominate);
320*9880d681SAndroid Build Coastguard Worker assert(Verify() && "Invalid PHITransAddr!");
321*9880d681SAndroid Build Coastguard Worker if (DT && DT->isReachableFromEntry(PredBB))
322*9880d681SAndroid Build Coastguard Worker Addr =
323*9880d681SAndroid Build Coastguard Worker PHITranslateSubExpr(Addr, CurBB, PredBB, MustDominate ? DT : nullptr);
324*9880d681SAndroid Build Coastguard Worker else
325*9880d681SAndroid Build Coastguard Worker Addr = nullptr;
326*9880d681SAndroid Build Coastguard Worker assert(Verify() && "Invalid PHITransAddr!");
327*9880d681SAndroid Build Coastguard Worker
328*9880d681SAndroid Build Coastguard Worker if (MustDominate)
329*9880d681SAndroid Build Coastguard Worker // Make sure the value is live in the predecessor.
330*9880d681SAndroid Build Coastguard Worker if (Instruction *Inst = dyn_cast_or_null<Instruction>(Addr))
331*9880d681SAndroid Build Coastguard Worker if (!DT->dominates(Inst->getParent(), PredBB))
332*9880d681SAndroid Build Coastguard Worker Addr = nullptr;
333*9880d681SAndroid Build Coastguard Worker
334*9880d681SAndroid Build Coastguard Worker return Addr == nullptr;
335*9880d681SAndroid Build Coastguard Worker }
336*9880d681SAndroid Build Coastguard Worker
337*9880d681SAndroid Build Coastguard Worker /// PHITranslateWithInsertion - PHI translate this value into the specified
338*9880d681SAndroid Build Coastguard Worker /// predecessor block, inserting a computation of the value if it is
339*9880d681SAndroid Build Coastguard Worker /// unavailable.
340*9880d681SAndroid Build Coastguard Worker ///
341*9880d681SAndroid Build Coastguard Worker /// All newly created instructions are added to the NewInsts list. This
342*9880d681SAndroid Build Coastguard Worker /// returns null on failure.
343*9880d681SAndroid Build Coastguard Worker ///
344*9880d681SAndroid Build Coastguard Worker Value *PHITransAddr::
PHITranslateWithInsertion(BasicBlock * CurBB,BasicBlock * PredBB,const DominatorTree & DT,SmallVectorImpl<Instruction * > & NewInsts)345*9880d681SAndroid Build Coastguard Worker PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
346*9880d681SAndroid Build Coastguard Worker const DominatorTree &DT,
347*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<Instruction*> &NewInsts) {
348*9880d681SAndroid Build Coastguard Worker unsigned NISize = NewInsts.size();
349*9880d681SAndroid Build Coastguard Worker
350*9880d681SAndroid Build Coastguard Worker // Attempt to PHI translate with insertion.
351*9880d681SAndroid Build Coastguard Worker Addr = InsertPHITranslatedSubExpr(Addr, CurBB, PredBB, DT, NewInsts);
352*9880d681SAndroid Build Coastguard Worker
353*9880d681SAndroid Build Coastguard Worker // If successful, return the new value.
354*9880d681SAndroid Build Coastguard Worker if (Addr) return Addr;
355*9880d681SAndroid Build Coastguard Worker
356*9880d681SAndroid Build Coastguard Worker // If not, destroy any intermediate instructions inserted.
357*9880d681SAndroid Build Coastguard Worker while (NewInsts.size() != NISize)
358*9880d681SAndroid Build Coastguard Worker NewInsts.pop_back_val()->eraseFromParent();
359*9880d681SAndroid Build Coastguard Worker return nullptr;
360*9880d681SAndroid Build Coastguard Worker }
361*9880d681SAndroid Build Coastguard Worker
362*9880d681SAndroid Build Coastguard Worker
363*9880d681SAndroid Build Coastguard Worker /// InsertPHITranslatedPointer - Insert a computation of the PHI translated
364*9880d681SAndroid Build Coastguard Worker /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
365*9880d681SAndroid Build Coastguard Worker /// block. All newly created instructions are added to the NewInsts list.
366*9880d681SAndroid Build Coastguard Worker /// This returns null on failure.
367*9880d681SAndroid Build Coastguard Worker ///
368*9880d681SAndroid Build Coastguard Worker Value *PHITransAddr::
InsertPHITranslatedSubExpr(Value * InVal,BasicBlock * CurBB,BasicBlock * PredBB,const DominatorTree & DT,SmallVectorImpl<Instruction * > & NewInsts)369*9880d681SAndroid Build Coastguard Worker InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
370*9880d681SAndroid Build Coastguard Worker BasicBlock *PredBB, const DominatorTree &DT,
371*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<Instruction*> &NewInsts) {
372*9880d681SAndroid Build Coastguard Worker // See if we have a version of this value already available and dominating
373*9880d681SAndroid Build Coastguard Worker // PredBB. If so, there is no need to insert a new instance of it.
374*9880d681SAndroid Build Coastguard Worker PHITransAddr Tmp(InVal, DL, AC);
375*9880d681SAndroid Build Coastguard Worker if (!Tmp.PHITranslateValue(CurBB, PredBB, &DT, /*MustDominate=*/true))
376*9880d681SAndroid Build Coastguard Worker return Tmp.getAddr();
377*9880d681SAndroid Build Coastguard Worker
378*9880d681SAndroid Build Coastguard Worker // We don't need to PHI translate values which aren't instructions.
379*9880d681SAndroid Build Coastguard Worker auto *Inst = dyn_cast<Instruction>(InVal);
380*9880d681SAndroid Build Coastguard Worker if (!Inst)
381*9880d681SAndroid Build Coastguard Worker return nullptr;
382*9880d681SAndroid Build Coastguard Worker
383*9880d681SAndroid Build Coastguard Worker // Handle cast of PHI translatable value.
384*9880d681SAndroid Build Coastguard Worker if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
385*9880d681SAndroid Build Coastguard Worker if (!isSafeToSpeculativelyExecute(Cast)) return nullptr;
386*9880d681SAndroid Build Coastguard Worker Value *OpVal = InsertPHITranslatedSubExpr(Cast->getOperand(0),
387*9880d681SAndroid Build Coastguard Worker CurBB, PredBB, DT, NewInsts);
388*9880d681SAndroid Build Coastguard Worker if (!OpVal) return nullptr;
389*9880d681SAndroid Build Coastguard Worker
390*9880d681SAndroid Build Coastguard Worker // Otherwise insert a cast at the end of PredBB.
391*9880d681SAndroid Build Coastguard Worker CastInst *New = CastInst::Create(Cast->getOpcode(), OpVal, InVal->getType(),
392*9880d681SAndroid Build Coastguard Worker InVal->getName() + ".phi.trans.insert",
393*9880d681SAndroid Build Coastguard Worker PredBB->getTerminator());
394*9880d681SAndroid Build Coastguard Worker New->setDebugLoc(Inst->getDebugLoc());
395*9880d681SAndroid Build Coastguard Worker NewInsts.push_back(New);
396*9880d681SAndroid Build Coastguard Worker return New;
397*9880d681SAndroid Build Coastguard Worker }
398*9880d681SAndroid Build Coastguard Worker
399*9880d681SAndroid Build Coastguard Worker // Handle getelementptr with at least one PHI operand.
400*9880d681SAndroid Build Coastguard Worker if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
401*9880d681SAndroid Build Coastguard Worker SmallVector<Value*, 8> GEPOps;
402*9880d681SAndroid Build Coastguard Worker BasicBlock *CurBB = GEP->getParent();
403*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
404*9880d681SAndroid Build Coastguard Worker Value *OpVal = InsertPHITranslatedSubExpr(GEP->getOperand(i),
405*9880d681SAndroid Build Coastguard Worker CurBB, PredBB, DT, NewInsts);
406*9880d681SAndroid Build Coastguard Worker if (!OpVal) return nullptr;
407*9880d681SAndroid Build Coastguard Worker GEPOps.push_back(OpVal);
408*9880d681SAndroid Build Coastguard Worker }
409*9880d681SAndroid Build Coastguard Worker
410*9880d681SAndroid Build Coastguard Worker GetElementPtrInst *Result = GetElementPtrInst::Create(
411*9880d681SAndroid Build Coastguard Worker GEP->getSourceElementType(), GEPOps[0], makeArrayRef(GEPOps).slice(1),
412*9880d681SAndroid Build Coastguard Worker InVal->getName() + ".phi.trans.insert", PredBB->getTerminator());
413*9880d681SAndroid Build Coastguard Worker Result->setDebugLoc(Inst->getDebugLoc());
414*9880d681SAndroid Build Coastguard Worker Result->setIsInBounds(GEP->isInBounds());
415*9880d681SAndroid Build Coastguard Worker NewInsts.push_back(Result);
416*9880d681SAndroid Build Coastguard Worker return Result;
417*9880d681SAndroid Build Coastguard Worker }
418*9880d681SAndroid Build Coastguard Worker
419*9880d681SAndroid Build Coastguard Worker #if 0
420*9880d681SAndroid Build Coastguard Worker // FIXME: This code works, but it is unclear that we actually want to insert
421*9880d681SAndroid Build Coastguard Worker // a big chain of computation in order to make a value available in a block.
422*9880d681SAndroid Build Coastguard Worker // This needs to be evaluated carefully to consider its cost trade offs.
423*9880d681SAndroid Build Coastguard Worker
424*9880d681SAndroid Build Coastguard Worker // Handle add with a constant RHS.
425*9880d681SAndroid Build Coastguard Worker if (Inst->getOpcode() == Instruction::Add &&
426*9880d681SAndroid Build Coastguard Worker isa<ConstantInt>(Inst->getOperand(1))) {
427*9880d681SAndroid Build Coastguard Worker // PHI translate the LHS.
428*9880d681SAndroid Build Coastguard Worker Value *OpVal = InsertPHITranslatedSubExpr(Inst->getOperand(0),
429*9880d681SAndroid Build Coastguard Worker CurBB, PredBB, DT, NewInsts);
430*9880d681SAndroid Build Coastguard Worker if (OpVal == 0) return 0;
431*9880d681SAndroid Build Coastguard Worker
432*9880d681SAndroid Build Coastguard Worker BinaryOperator *Res = BinaryOperator::CreateAdd(OpVal, Inst->getOperand(1),
433*9880d681SAndroid Build Coastguard Worker InVal->getName()+".phi.trans.insert",
434*9880d681SAndroid Build Coastguard Worker PredBB->getTerminator());
435*9880d681SAndroid Build Coastguard Worker Res->setHasNoSignedWrap(cast<BinaryOperator>(Inst)->hasNoSignedWrap());
436*9880d681SAndroid Build Coastguard Worker Res->setHasNoUnsignedWrap(cast<BinaryOperator>(Inst)->hasNoUnsignedWrap());
437*9880d681SAndroid Build Coastguard Worker NewInsts.push_back(Res);
438*9880d681SAndroid Build Coastguard Worker return Res;
439*9880d681SAndroid Build Coastguard Worker }
440*9880d681SAndroid Build Coastguard Worker #endif
441*9880d681SAndroid Build Coastguard Worker
442*9880d681SAndroid Build Coastguard Worker return nullptr;
443*9880d681SAndroid Build Coastguard Worker }
444