blob: bdaaac1eaa1f738e2bd4161638ed47b79502d675 [file] [log] [blame]
Chris Lattner210e45a2009-12-04 02:10:16 +00001//===- PHITransAddr.cpp - PHI Translation for Addresses -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the PHITransAddr class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/PHITransAddr.h"
15#include "llvm/Analysis/Dominators.h"
Chris Lattner9a864122009-12-07 18:36:53 +000016#include "llvm/Analysis/InstructionSimplify.h"
Chris Lattner210e45a2009-12-04 02:10:16 +000017using namespace llvm;
18
Chris Lattner9a864122009-12-07 18:36:53 +000019/// IsPotentiallyPHITranslatable - If this needs PHI translation, return true
20/// if we have some hope of doing it. This should be used as a filter to
21/// avoid calling PHITranslateValue in hopeless situations.
22bool PHITransAddr::IsPotentiallyPHITranslatable() const {
23 // If the input value is not an instruction, or if it is not defined in CurBB,
24 // then we don't need to phi translate it.
25 Instruction *Inst = dyn_cast<Instruction>(Addr);
26 if (isa<PHINode>(Inst) ||
27 isa<BitCastInst>(Inst) ||
28 isa<GetElementPtrInst>(Inst) ||
29 (Inst->getOpcode() == Instruction::And &&
30 isa<ConstantInt>(Inst->getOperand(1))))
31 return true;
32
33 // cerr << "MEMDEP: Could not PHI translate: " << *Pointer;
34 // if (isa<BitCastInst>(PtrInst) || isa<GetElementPtrInst>(PtrInst))
35 // cerr << "OP:\t\t\t\t" << *PtrInst->getOperand(0);
36
37 return false;
Chris Lattner210e45a2009-12-04 02:10:16 +000038}
39
Chris Lattner9a864122009-12-07 18:36:53 +000040
41Value *PHITransAddr::PHITranslateSubExpr(Value *V, BasicBlock *CurBB,
42 BasicBlock *PredBB) {
43 // If this is a non-instruction value, it can't require PHI translation.
44 Instruction *Inst = dyn_cast<Instruction>(V);
45 if (Inst == 0) return V;
46
47 // Determine whether 'Inst' is an input to our PHI translatable expression.
48 bool isInput = std::count(InstInputs.begin(), InstInputs.end(), Inst);
49
50 // If 'Inst' is not defined in this block, it is either an input, or an
51 // intermediate result.
52 if (Inst->getParent() != CurBB) {
53 // If it is an input, then it remains an input.
54 if (isInput)
55 return Inst;
56
57 // Otherwise, it must be an intermediate result. See if its operands need
58 // to be phi translated, and if so, reconstruct it.
59
60 if (BitCastInst *BC = dyn_cast<BitCastInst>(Inst)) {
61 Value *PHIIn = PHITranslateSubExpr(BC->getOperand(0), CurBB, PredBB);
62 if (PHIIn == 0) return 0;
63 if (PHIIn == BC->getOperand(0))
64 return BC;
65
66 // Find an available version of this cast.
67
68 // Constants are trivial to find.
69 if (Constant *C = dyn_cast<Constant>(PHIIn))
70 return ConstantExpr::getBitCast(C, BC->getType());
71
72 // Otherwise we have to see if a bitcasted version of the incoming pointer
73 // is available. If so, we can use it, otherwise we have to fail.
74 for (Value::use_iterator UI = PHIIn->use_begin(), E = PHIIn->use_end();
75 UI != E; ++UI) {
76 if (BitCastInst *BCI = dyn_cast<BitCastInst>(*UI))
77 if (BCI->getType() == BC->getType())
78 return BCI;
79 }
80 return 0;
81 }
82
83 // Handle getelementptr with at least one PHI translatable operand.
84 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
85 SmallVector<Value*, 8> GEPOps;
86 BasicBlock *CurBB = GEP->getParent();
87 bool AnyChanged = false;
88 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
89 Value *GEPOp = PHITranslateSubExpr(GEP->getOperand(i), CurBB, PredBB);
90 if (GEPOp == 0) return 0;
91
92 AnyChanged = GEPOp != GEP->getOperand(i);
93 GEPOps.push_back(GEPOp);
94 }
95
96 if (!AnyChanged)
97 return GEP;
98
99 // Simplify the GEP to handle 'gep x, 0' -> x etc.
100 if (Value *V = SimplifyGEPInst(&GEPOps[0], GEPOps.size(), TD))
101 return V;
102
103 // Scan to see if we have this GEP available.
104 Value *APHIOp = GEPOps[0];
105 for (Value::use_iterator UI = APHIOp->use_begin(), E = APHIOp->use_end();
106 UI != E; ++UI) {
107 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI))
108 if (GEPI->getType() == GEP->getType() &&
109 GEPI->getNumOperands() == GEPOps.size() &&
110 GEPI->getParent()->getParent() == CurBB->getParent()) {
111 bool Mismatch = false;
112 for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
113 if (GEPI->getOperand(i) != GEPOps[i]) {
114 Mismatch = true;
115 break;
116 }
117 if (!Mismatch)
118 return GEPI;
119 }
120 }
121 return 0;
122 }
123
124 // Handle add with a constant RHS.
125 if (Inst->getOpcode() == Instruction::Add &&
126 isa<ConstantInt>(Inst->getOperand(1))) {
127 // PHI translate the LHS.
128 Constant *RHS = cast<ConstantInt>(Inst->getOperand(1));
129 bool isNSW = cast<BinaryOperator>(Inst)->hasNoSignedWrap();
130 bool isNUW = cast<BinaryOperator>(Inst)->hasNoUnsignedWrap();
131
132 Value *LHS = PHITranslateSubExpr(Inst->getOperand(0), CurBB, PredBB);
133 if (LHS == 0) return 0;
134
135 // If the PHI translated LHS is an add of a constant, fold the immediates.
136 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(LHS))
137 if (BOp->getOpcode() == Instruction::Add)
138 if (ConstantInt *CI = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
139 LHS = BOp->getOperand(0);
140 RHS = ConstantExpr::getAdd(RHS, CI);
141 isNSW = isNUW = false;
142 }
143
144 // See if the add simplifies away.
145 if (Value *Res = SimplifyAddInst(LHS, RHS, isNSW, isNUW, TD))
146 return Res;
147
148 // Otherwise, see if we have this add available somewhere.
149 for (Value::use_iterator UI = LHS->use_begin(), E = LHS->use_end();
150 UI != E; ++UI) {
151 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*UI))
152 if (BO->getOperand(0) == LHS && BO->getOperand(1) == RHS &&
153 BO->getParent()->getParent() == CurBB->getParent())
154 return BO;
155 }
156
157 return 0;
158 }
159
160 // Otherwise, we failed.
161 return 0;
162 }
163
164 // Otherwise, it is defined in this block. It must be an input and must be
165 // phi translated.
166 assert(isInput && "Instruction defined in block must be an input");
167
168
169 abort(); // unimplemented so far.
Chris Lattner210e45a2009-12-04 02:10:16 +0000170}
171
Chris Lattner9a864122009-12-07 18:36:53 +0000172
173/// PHITranslateValue - PHI translate the current address up the CFG from
174/// CurBB to Pred, updating our state the reflect any needed changes. This
175/// returns true on failure.
176bool PHITransAddr::PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB) {
177 Addr = PHITranslateSubExpr(Addr, CurBB, PredBB);
178 return Addr == 0;
179}
180
181/// GetAvailablePHITranslatedSubExpr - Return the value computed by
182/// PHITranslateSubExpr if it dominates PredBB, otherwise return null.
Chris Lattner210e45a2009-12-04 02:10:16 +0000183Value *PHITransAddr::
Chris Lattner9a864122009-12-07 18:36:53 +0000184GetAvailablePHITranslatedSubExpr(Value *V, BasicBlock *CurBB,BasicBlock *PredBB,
185 const DominatorTree &DT) {
Chris Lattner210e45a2009-12-04 02:10:16 +0000186 // See if PHI translation succeeds.
Chris Lattner9a864122009-12-07 18:36:53 +0000187 V = PHITranslateSubExpr(V, CurBB, PredBB);
Chris Lattner210e45a2009-12-04 02:10:16 +0000188
189 // Make sure the value is live in the predecessor.
190 if (Instruction *Inst = dyn_cast_or_null<Instruction>(V))
191 if (!DT.dominates(Inst->getParent(), PredBB))
192 return 0;
193 return V;
194}
195
Chris Lattner9a864122009-12-07 18:36:53 +0000196
197/// PHITranslateWithInsertion - PHI translate this value into the specified
198/// predecessor block, inserting a computation of the value if it is
199/// unavailable.
200///
201/// All newly created instructions are added to the NewInsts list. This
202/// returns null on failure.
203///
204Value *PHITransAddr::
205PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
206 const DominatorTree &DT,
207 SmallVectorImpl<Instruction*> &NewInsts) {
208 unsigned NISize = NewInsts.size();
209
210 // Attempt to PHI translate with insertion.
211 Addr = InsertPHITranslatedSubExpr(Addr, CurBB, PredBB, DT, NewInsts);
212
213 // If successful, return the new value.
214 if (Addr) return Addr;
215
216 // If not, destroy any intermediate instructions inserted.
217 while (NewInsts.size() != NISize)
218 NewInsts.pop_back_val()->eraseFromParent();
219 return 0;
220}
221
222
Chris Lattner210e45a2009-12-04 02:10:16 +0000223/// InsertPHITranslatedPointer - Insert a computation of the PHI translated
224/// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
225/// block. All newly created instructions are added to the NewInsts list.
226/// This returns null on failure.
227///
228Value *PHITransAddr::
Chris Lattner9a864122009-12-07 18:36:53 +0000229InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
230 BasicBlock *PredBB, const DominatorTree &DT,
231 SmallVectorImpl<Instruction*> &NewInsts) {
Chris Lattner210e45a2009-12-04 02:10:16 +0000232 // See if we have a version of this value already available and dominating
Chris Lattner9a864122009-12-07 18:36:53 +0000233 // PredBB. If so, there is no need to insert a new instance of it.
234 if (Value *Res = GetAvailablePHITranslatedSubExpr(InVal, CurBB, PredBB, DT))
Chris Lattner210e45a2009-12-04 02:10:16 +0000235 return Res;
236
Chris Lattner9a864122009-12-07 18:36:53 +0000237 // If we don't have an available version of this value, it must be an
238 // instruction.
239 Instruction *Inst = cast<Instruction>(InVal);
240
241 // Handle bitcast of PHI translatable value.
242 if (BitCastInst *BC = dyn_cast<BitCastInst>(Inst)) {
243 Value *OpVal = InsertPHITranslatedSubExpr(BC->getOperand(0),
244 CurBB, PredBB, DT, NewInsts);
245 if (OpVal == 0) return 0;
246
247 // Otherwise insert a bitcast at the end of PredBB.
248 BitCastInst *New = new BitCastInst(OpVal, InVal->getType(),
249 InVal->getName()+".phi.trans.insert",
250 PredBB->getTerminator());
251 NewInsts.push_back(New);
252 return New;
253 }
254
255 // Handle getelementptr with at least one PHI operand.
256 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
257 SmallVector<Value*, 8> GEPOps;
258 BasicBlock *CurBB = GEP->getParent();
259 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
260 Value *OpVal = InsertPHITranslatedSubExpr(GEP->getOperand(i),
261 CurBB, PredBB, DT, NewInsts);
262 if (OpVal == 0) return 0;
263 GEPOps.push_back(OpVal);
264 }
265
266 GetElementPtrInst *Result =
267 GetElementPtrInst::Create(GEPOps[0], GEPOps.begin()+1, GEPOps.end(),
268 InVal->getName()+".phi.trans.insert",
269 PredBB->getTerminator());
270 Result->setIsInBounds(GEP->isInBounds());
271 NewInsts.push_back(Result);
272 return Result;
273 }
274
275#if 0
276 // FIXME: This code works, but it is unclear that we actually want to insert
277 // a big chain of computation in order to make a value available in a block.
278 // This needs to be evaluated carefully to consider its cost trade offs.
279
280 // Handle add with a constant RHS.
281 if (Inst->getOpcode() == Instruction::Add &&
282 isa<ConstantInt>(Inst->getOperand(1))) {
283 // PHI translate the LHS.
284 Value *OpVal = InsertPHITranslatedSubExpr(Inst->getOperand(0),
285 CurBB, PredBB, DT, NewInsts);
286 if (OpVal == 0) return 0;
287
288 BinaryOperator *Res = BinaryOperator::CreateAdd(OpVal, Inst->getOperand(1),
289 InVal->getName()+".phi.trans.insert",
290 PredBB->getTerminator());
291 Res->setHasNoSignedWrap(cast<BinaryOperator>(Inst)->hasNoSignedWrap());
292 Res->setHasNoUnsignedWrap(cast<BinaryOperator>(Inst)->hasNoUnsignedWrap());
293 NewInsts.push_back(Res);
294 return Res;
295 }
296#endif
297
Chris Lattner210e45a2009-12-04 02:10:16 +0000298 return 0;
299}