blob: c35737e4724c84a884e8818f0278548486c5bf24 [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"
Dan Gohmanf0426602011-12-14 23:49:11 +000015#include "llvm/Analysis/ValueTracking.h"
Jay Foad562b84b2011-04-11 09:35:34 +000016#include "llvm/Constants.h"
Chris Lattner9fc5cdf2011-01-02 22:09:33 +000017#include "llvm/Instructions.h"
Chris Lattner210e45a2009-12-04 02:10:16 +000018#include "llvm/Analysis/Dominators.h"
Chris Lattner9a864122009-12-07 18:36:53 +000019#include "llvm/Analysis/InstructionSimplify.h"
David Greene2a0f3cc2009-12-23 21:06:14 +000020#include "llvm/Support/Debug.h"
Dan Gohman88fc03c2010-11-18 17:05:57 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattner7dedbf42009-12-09 00:01:00 +000022#include "llvm/Support/raw_ostream.h"
Chris Lattner210e45a2009-12-04 02:10:16 +000023using namespace llvm;
24
Chris Lattner6fcca1c2009-12-07 19:04:49 +000025static bool CanPHITrans(Instruction *Inst) {
26 if (isa<PHINode>(Inst) ||
Chris Lattner6fcca1c2009-12-07 19:04:49 +000027 isa<GetElementPtrInst>(Inst))
28 return true;
Dan Gohmance562622010-11-18 17:05:13 +000029
30 if (isa<CastInst>(Inst) &&
Dan Gohmanf0426602011-12-14 23:49:11 +000031 isSafeToSpeculativelyExecute(Inst))
Dan Gohmance562622010-11-18 17:05:13 +000032 return true;
Dan Gohman7feccd22010-11-18 17:06:31 +000033
Chris Lattner34f84902009-12-08 06:06:26 +000034 if (Inst->getOpcode() == Instruction::Add &&
Chris Lattner6fcca1c2009-12-07 19:04:49 +000035 isa<ConstantInt>(Inst->getOperand(1)))
36 return true;
Dan Gohman7feccd22010-11-18 17:06:31 +000037
Chris Lattner6fcca1c2009-12-07 19:04:49 +000038 // cerr << "MEMDEP: Could not PHI translate: " << *Pointer;
39 // if (isa<BitCastInst>(PtrInst) || isa<GetElementPtrInst>(PtrInst))
40 // cerr << "OP:\t\t\t\t" << *PtrInst->getOperand(0);
41 return false;
42}
43
Manman Ren286c4dc2012-09-12 05:06:18 +000044#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Chris Lattner7dedbf42009-12-09 00:01:00 +000045void PHITransAddr::dump() const {
46 if (Addr == 0) {
David Greene2a0f3cc2009-12-23 21:06:14 +000047 dbgs() << "PHITransAddr: null\n";
Chris Lattner7dedbf42009-12-09 00:01:00 +000048 return;
49 }
David Greene2a0f3cc2009-12-23 21:06:14 +000050 dbgs() << "PHITransAddr: " << *Addr << "\n";
Chris Lattner7dedbf42009-12-09 00:01:00 +000051 for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
David Greene2a0f3cc2009-12-23 21:06:14 +000052 dbgs() << " Input #" << i << " is " << *InstInputs[i] << "\n";
Chris Lattner7dedbf42009-12-09 00:01:00 +000053}
Manman Rencc77eec2012-09-06 19:55:56 +000054#endif
Chris Lattner7dedbf42009-12-09 00:01:00 +000055
56
57static bool VerifySubExpr(Value *Expr,
58 SmallVectorImpl<Instruction*> &InstInputs) {
59 // If this is a non-instruction value, there is nothing to do.
60 Instruction *I = dyn_cast<Instruction>(Expr);
61 if (I == 0) return true;
Dan Gohman7feccd22010-11-18 17:06:31 +000062
Chris Lattner7dedbf42009-12-09 00:01:00 +000063 // If it's an instruction, it is either in Tmp or its operands recursively
64 // are.
65 SmallVectorImpl<Instruction*>::iterator Entry =
66 std::find(InstInputs.begin(), InstInputs.end(), I);
67 if (Entry != InstInputs.end()) {
68 InstInputs.erase(Entry);
69 return true;
70 }
Dan Gohman7feccd22010-11-18 17:06:31 +000071
Chris Lattner7dedbf42009-12-09 00:01:00 +000072 // If it isn't in the InstInputs list it is a subexpr incorporated into the
73 // address. Sanity check that it is phi translatable.
74 if (!CanPHITrans(I)) {
Dan Gohman88fc03c2010-11-18 17:05:57 +000075 errs() << "Non phi translatable instruction found in PHITransAddr:\n";
David Greenea8e21d42009-12-23 23:27:15 +000076 errs() << *I << '\n';
Dan Gohman88fc03c2010-11-18 17:05:57 +000077 llvm_unreachable("Either something is missing from InstInputs or "
78 "CanPHITrans is wrong.");
Chris Lattner7dedbf42009-12-09 00:01:00 +000079 }
Dan Gohman7feccd22010-11-18 17:06:31 +000080
Chris Lattner7dedbf42009-12-09 00:01:00 +000081 // Validate the operands of the instruction.
82 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
83 if (!VerifySubExpr(I->getOperand(i), InstInputs))
84 return false;
85
86 return true;
87}
88
89/// Verify - Check internal consistency of this data structure. If the
90/// structure is valid, it returns true. If invalid, it prints errors and
91/// returns false.
92bool PHITransAddr::Verify() const {
93 if (Addr == 0) return true;
Dan Gohman7feccd22010-11-18 17:06:31 +000094
95 SmallVector<Instruction*, 8> Tmp(InstInputs.begin(), InstInputs.end());
96
Chris Lattner7dedbf42009-12-09 00:01:00 +000097 if (!VerifySubExpr(Addr, Tmp))
98 return false;
Dan Gohman7feccd22010-11-18 17:06:31 +000099
Chris Lattner7dedbf42009-12-09 00:01:00 +0000100 if (!Tmp.empty()) {
Dan Gohman88fc03c2010-11-18 17:05:57 +0000101 errs() << "PHITransAddr contains extra instructions:\n";
Chris Lattner7dedbf42009-12-09 00:01:00 +0000102 for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
David Greenea8e21d42009-12-23 23:27:15 +0000103 errs() << " InstInput #" << i << " is " << *InstInputs[i] << "\n";
Dan Gohman88fc03c2010-11-18 17:05:57 +0000104 llvm_unreachable("This is unexpected.");
Chris Lattner7dedbf42009-12-09 00:01:00 +0000105 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000106
Chris Lattner7dedbf42009-12-09 00:01:00 +0000107 // a-ok.
108 return true;
109}
110
111
Chris Lattner9a864122009-12-07 18:36:53 +0000112/// IsPotentiallyPHITranslatable - If this needs PHI translation, return true
113/// if we have some hope of doing it. This should be used as a filter to
114/// avoid calling PHITranslateValue in hopeless situations.
115bool PHITransAddr::IsPotentiallyPHITranslatable() const {
116 // If the input value is not an instruction, or if it is not defined in CurBB,
117 // then we don't need to phi translate it.
118 Instruction *Inst = dyn_cast<Instruction>(Addr);
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000119 return Inst == 0 || CanPHITrans(Inst);
Chris Lattner210e45a2009-12-04 02:10:16 +0000120}
121
Chris Lattner9a864122009-12-07 18:36:53 +0000122
Dan Gohman7feccd22010-11-18 17:06:31 +0000123static void RemoveInstInputs(Value *V,
Chris Lattner43678f42009-12-08 23:42:51 +0000124 SmallVectorImpl<Instruction*> &InstInputs) {
Chris Lattner6200e532009-12-09 00:56:14 +0000125 Instruction *I = dyn_cast<Instruction>(V);
126 if (I == 0) return;
Dan Gohman7feccd22010-11-18 17:06:31 +0000127
Chris Lattner43678f42009-12-08 23:42:51 +0000128 // If the instruction is in the InstInputs list, remove it.
129 SmallVectorImpl<Instruction*>::iterator Entry =
130 std::find(InstInputs.begin(), InstInputs.end(), I);
131 if (Entry != InstInputs.end()) {
132 InstInputs.erase(Entry);
133 return;
134 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000135
Chris Lattner6200e532009-12-09 00:56:14 +0000136 assert(!isa<PHINode>(I) && "Error, removing something that isn't an input");
Dan Gohman7feccd22010-11-18 17:06:31 +0000137
Chris Lattner6200e532009-12-09 00:56:14 +0000138 // Otherwise, it must have instruction inputs itself. Zap them recursively.
139 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
140 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
141 RemoveInstInputs(Op, InstInputs);
142 }
Chris Lattner43678f42009-12-08 23:42:51 +0000143}
144
Chris Lattner9a864122009-12-07 18:36:53 +0000145Value *PHITransAddr::PHITranslateSubExpr(Value *V, BasicBlock *CurBB,
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000146 BasicBlock *PredBB,
147 const DominatorTree *DT) {
Chris Lattner9a864122009-12-07 18:36:53 +0000148 // If this is a non-instruction value, it can't require PHI translation.
149 Instruction *Inst = dyn_cast<Instruction>(V);
150 if (Inst == 0) return V;
Dan Gohman7feccd22010-11-18 17:06:31 +0000151
Chris Lattneraf503152009-12-09 00:10:55 +0000152 // Determine whether 'Inst' is an input to our PHI translatable expression.
153 bool isInput = std::count(InstInputs.begin(), InstInputs.end(), Inst);
154
155 // Handle inputs instructions if needed.
156 if (isInput) {
157 if (Inst->getParent() != CurBB) {
158 // If it is an input defined in a different block, then it remains an
159 // input.
160 return Inst;
161 }
Chris Lattnere09e98c2009-12-09 00:18:13 +0000162
163 // If 'Inst' is defined in this block and is an input that needs to be phi
164 // translated, we need to incorporate the value into the expression or fail.
165
Chris Lattner6200e532009-12-09 00:56:14 +0000166 // In either case, the instruction itself isn't an input any longer.
167 InstInputs.erase(std::find(InstInputs.begin(), InstInputs.end(), Inst));
Dan Gohman7feccd22010-11-18 17:06:31 +0000168
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000169 // If this is a PHI, go ahead and translate it.
170 if (PHINode *PN = dyn_cast<PHINode>(Inst))
Chris Lattner6200e532009-12-09 00:56:14 +0000171 return AddAsInput(PN->getIncomingValueForBlock(PredBB));
Dan Gohman7feccd22010-11-18 17:06:31 +0000172
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000173 // If this is a non-phi value, and it is analyzable, we can incorporate it
174 // into the expression by making all instruction operands be inputs.
175 if (!CanPHITrans(Inst))
176 return 0;
Dan Gohman7feccd22010-11-18 17:06:31 +0000177
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000178 // All instruction operands are now inputs (and of course, they may also be
179 // defined in this block, so they may need to be phi translated themselves.
180 for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i)
181 if (Instruction *Op = dyn_cast<Instruction>(Inst->getOperand(i)))
182 InstInputs.push_back(Op);
Chris Lattner9a864122009-12-07 18:36:53 +0000183 }
184
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000185 // Ok, it must be an intermediate result (either because it started that way
186 // or because we just incorporated it into the expression). See if its
187 // operands need to be phi translated, and if so, reconstruct it.
Dan Gohman7feccd22010-11-18 17:06:31 +0000188
Dan Gohmance562622010-11-18 17:05:13 +0000189 if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
Dan Gohmanf0426602011-12-14 23:49:11 +0000190 if (!isSafeToSpeculativelyExecute(Cast)) return 0;
Dan Gohmance562622010-11-18 17:05:13 +0000191 Value *PHIIn = PHITranslateSubExpr(Cast->getOperand(0), CurBB, PredBB, DT);
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000192 if (PHIIn == 0) return 0;
Dan Gohmance562622010-11-18 17:05:13 +0000193 if (PHIIn == Cast->getOperand(0))
194 return Cast;
Dan Gohman7feccd22010-11-18 17:06:31 +0000195
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000196 // Find an available version of this cast.
Dan Gohman7feccd22010-11-18 17:06:31 +0000197
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000198 // Constants are trivial to find.
199 if (Constant *C = dyn_cast<Constant>(PHIIn))
Dan Gohmance562622010-11-18 17:05:13 +0000200 return AddAsInput(ConstantExpr::getCast(Cast->getOpcode(),
201 C, Cast->getType()));
Dan Gohman7feccd22010-11-18 17:06:31 +0000202
Dan Gohmance562622010-11-18 17:05:13 +0000203 // Otherwise we have to see if a casted version of the incoming pointer
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000204 // is available. If so, we can use it, otherwise we have to fail.
205 for (Value::use_iterator UI = PHIIn->use_begin(), E = PHIIn->use_end();
206 UI != E; ++UI) {
Dan Gohmance562622010-11-18 17:05:13 +0000207 if (CastInst *CastI = dyn_cast<CastInst>(*UI))
208 if (CastI->getOpcode() == Cast->getOpcode() &&
209 CastI->getType() == Cast->getType() &&
210 (!DT || DT->dominates(CastI->getParent(), PredBB)))
211 return CastI;
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000212 }
213 return 0;
214 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000215
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000216 // Handle getelementptr with at least one PHI translatable operand.
217 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
218 SmallVector<Value*, 8> GEPOps;
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000219 bool AnyChanged = false;
220 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000221 Value *GEPOp = PHITranslateSubExpr(GEP->getOperand(i), CurBB, PredBB, DT);
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000222 if (GEPOp == 0) return 0;
Dan Gohman7feccd22010-11-18 17:06:31 +0000223
Chris Lattner60454172009-12-07 19:52:57 +0000224 AnyChanged |= GEPOp != GEP->getOperand(i);
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000225 GEPOps.push_back(GEPOp);
226 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000227
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000228 if (!AnyChanged)
229 return GEP;
Dan Gohman7feccd22010-11-18 17:06:31 +0000230
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000231 // Simplify the GEP to handle 'gep x, 0' -> x etc.
Duncan Sands0aa85eb2012-03-13 11:42:19 +0000232 if (Value *V = SimplifyGEPInst(GEPOps, TD, TLI, DT)) {
Chris Lattner6200e532009-12-09 00:56:14 +0000233 for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
234 RemoveInstInputs(GEPOps[i], InstInputs);
Dan Gohman7feccd22010-11-18 17:06:31 +0000235
Chris Lattner6200e532009-12-09 00:56:14 +0000236 return AddAsInput(V);
237 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000238
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000239 // Scan to see if we have this GEP available.
240 Value *APHIOp = GEPOps[0];
241 for (Value::use_iterator UI = APHIOp->use_begin(), E = APHIOp->use_end();
242 UI != E; ++UI) {
243 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI))
244 if (GEPI->getType() == GEP->getType() &&
245 GEPI->getNumOperands() == GEPOps.size() &&
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000246 GEPI->getParent()->getParent() == CurBB->getParent() &&
247 (!DT || DT->dominates(GEPI->getParent(), PredBB))) {
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000248 bool Mismatch = false;
249 for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
250 if (GEPI->getOperand(i) != GEPOps[i]) {
251 Mismatch = true;
252 break;
253 }
254 if (!Mismatch)
255 return GEPI;
256 }
257 }
258 return 0;
259 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000260
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000261 // Handle add with a constant RHS.
262 if (Inst->getOpcode() == Instruction::Add &&
263 isa<ConstantInt>(Inst->getOperand(1))) {
264 // PHI translate the LHS.
265 Constant *RHS = cast<ConstantInt>(Inst->getOperand(1));
266 bool isNSW = cast<BinaryOperator>(Inst)->hasNoSignedWrap();
267 bool isNUW = cast<BinaryOperator>(Inst)->hasNoUnsignedWrap();
Dan Gohman7feccd22010-11-18 17:06:31 +0000268
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000269 Value *LHS = PHITranslateSubExpr(Inst->getOperand(0), CurBB, PredBB, DT);
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000270 if (LHS == 0) return 0;
Dan Gohman7feccd22010-11-18 17:06:31 +0000271
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000272 // If the PHI translated LHS is an add of a constant, fold the immediates.
273 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(LHS))
274 if (BOp->getOpcode() == Instruction::Add)
275 if (ConstantInt *CI = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
276 LHS = BOp->getOperand(0);
277 RHS = ConstantExpr::getAdd(RHS, CI);
278 isNSW = isNUW = false;
Dan Gohman7feccd22010-11-18 17:06:31 +0000279
Chris Lattner6200e532009-12-09 00:56:14 +0000280 // If the old 'LHS' was an input, add the new 'LHS' as an input.
281 if (std::count(InstInputs.begin(), InstInputs.end(), BOp)) {
282 RemoveInstInputs(BOp, InstInputs);
283 AddAsInput(LHS);
284 }
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000285 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000286
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000287 // See if the add simplifies away.
Chad Rosier618c1db2011-12-01 03:08:23 +0000288 if (Value *Res = SimplifyAddInst(LHS, RHS, isNSW, isNUW, TD, TLI, DT)) {
Chris Lattner6200e532009-12-09 00:56:14 +0000289 // If we simplified the operands, the LHS is no longer an input, but Res
290 // is.
291 RemoveInstInputs(LHS, InstInputs);
292 return AddAsInput(Res);
293 }
Chris Lattner4d3a16f2009-12-09 17:27:45 +0000294
295 // If we didn't modify the add, just return it.
296 if (LHS == Inst->getOperand(0) && RHS == Inst->getOperand(1))
297 return Inst;
Dan Gohman7feccd22010-11-18 17:06:31 +0000298
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000299 // Otherwise, see if we have this add available somewhere.
300 for (Value::use_iterator UI = LHS->use_begin(), E = LHS->use_end();
301 UI != E; ++UI) {
302 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*UI))
Chris Lattnereddc65a2009-12-09 17:18:49 +0000303 if (BO->getOpcode() == Instruction::Add &&
304 BO->getOperand(0) == LHS && BO->getOperand(1) == RHS &&
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000305 BO->getParent()->getParent() == CurBB->getParent() &&
306 (!DT || DT->dominates(BO->getParent(), PredBB)))
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000307 return BO;
308 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000309
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000310 return 0;
311 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000312
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000313 // Otherwise, we failed.
314 return 0;
Chris Lattner210e45a2009-12-04 02:10:16 +0000315}
316
Chris Lattner9a864122009-12-07 18:36:53 +0000317
318/// PHITranslateValue - PHI translate the current address up the CFG from
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000319/// CurBB to Pred, updating our state to reflect any needed changes. If the
320/// dominator tree DT is non-null, the translated value must dominate
321/// PredBB. This returns true on failure and sets Addr to null.
322bool PHITransAddr::PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB,
323 const DominatorTree *DT) {
Chris Lattner7dedbf42009-12-09 00:01:00 +0000324 assert(Verify() && "Invalid PHITransAddr!");
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000325 Addr = PHITranslateSubExpr(Addr, CurBB, PredBB, DT);
Chris Lattner7dedbf42009-12-09 00:01:00 +0000326 assert(Verify() && "Invalid PHITransAddr!");
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000327
328 if (DT) {
329 // Make sure the value is live in the predecessor.
330 if (Instruction *Inst = dyn_cast_or_null<Instruction>(Addr))
331 if (!DT->dominates(Inst->getParent(), PredBB))
332 Addr = 0;
333 }
334
Chris Lattner9a864122009-12-07 18:36:53 +0000335 return Addr == 0;
336}
337
Chris Lattner9a864122009-12-07 18:36:53 +0000338/// PHITranslateWithInsertion - PHI translate this value into the specified
339/// predecessor block, inserting a computation of the value if it is
340/// unavailable.
341///
342/// All newly created instructions are added to the NewInsts list. This
343/// returns null on failure.
344///
345Value *PHITransAddr::
346PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
347 const DominatorTree &DT,
348 SmallVectorImpl<Instruction*> &NewInsts) {
349 unsigned NISize = NewInsts.size();
Dan Gohman7feccd22010-11-18 17:06:31 +0000350
Chris Lattner9a864122009-12-07 18:36:53 +0000351 // Attempt to PHI translate with insertion.
352 Addr = InsertPHITranslatedSubExpr(Addr, CurBB, PredBB, DT, NewInsts);
Dan Gohman7feccd22010-11-18 17:06:31 +0000353
Chris Lattner9a864122009-12-07 18:36:53 +0000354 // If successful, return the new value.
355 if (Addr) return Addr;
Dan Gohman7feccd22010-11-18 17:06:31 +0000356
Chris Lattner9a864122009-12-07 18:36:53 +0000357 // If not, destroy any intermediate instructions inserted.
358 while (NewInsts.size() != NISize)
359 NewInsts.pop_back_val()->eraseFromParent();
360 return 0;
361}
362
363
Chris Lattner210e45a2009-12-04 02:10:16 +0000364/// InsertPHITranslatedPointer - Insert a computation of the PHI translated
365/// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
366/// block. All newly created instructions are added to the NewInsts list.
367/// This returns null on failure.
368///
369Value *PHITransAddr::
Chris Lattner9a864122009-12-07 18:36:53 +0000370InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
371 BasicBlock *PredBB, const DominatorTree &DT,
372 SmallVectorImpl<Instruction*> &NewInsts) {
Chris Lattner210e45a2009-12-04 02:10:16 +0000373 // See if we have a version of this value already available and dominating
Chris Lattner9a864122009-12-07 18:36:53 +0000374 // PredBB. If so, there is no need to insert a new instance of it.
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000375 PHITransAddr Tmp(InVal, TD);
376 if (!Tmp.PHITranslateValue(CurBB, PredBB, &DT))
377 return Tmp.getAddr();
Chris Lattner210e45a2009-12-04 02:10:16 +0000378
Chris Lattner9a864122009-12-07 18:36:53 +0000379 // If we don't have an available version of this value, it must be an
380 // instruction.
381 Instruction *Inst = cast<Instruction>(InVal);
Dan Gohman7feccd22010-11-18 17:06:31 +0000382
Dan Gohmance562622010-11-18 17:05:13 +0000383 // Handle cast of PHI translatable value.
384 if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
Dan Gohmanf0426602011-12-14 23:49:11 +0000385 if (!isSafeToSpeculativelyExecute(Cast)) return 0;
Dan Gohmance562622010-11-18 17:05:13 +0000386 Value *OpVal = InsertPHITranslatedSubExpr(Cast->getOperand(0),
Chris Lattner9a864122009-12-07 18:36:53 +0000387 CurBB, PredBB, DT, NewInsts);
388 if (OpVal == 0) return 0;
Dan Gohman7feccd22010-11-18 17:06:31 +0000389
Dan Gohmance562622010-11-18 17:05:13 +0000390 // Otherwise insert a cast at the end of PredBB.
391 CastInst *New = CastInst::Create(Cast->getOpcode(),
392 OpVal, InVal->getType(),
393 InVal->getName()+".phi.trans.insert",
394 PredBB->getTerminator());
Chris Lattner9a864122009-12-07 18:36:53 +0000395 NewInsts.push_back(New);
396 return New;
397 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000398
Chris Lattner9a864122009-12-07 18:36:53 +0000399 // Handle getelementptr with at least one PHI operand.
400 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
401 SmallVector<Value*, 8> GEPOps;
402 BasicBlock *CurBB = GEP->getParent();
403 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
404 Value *OpVal = InsertPHITranslatedSubExpr(GEP->getOperand(i),
405 CurBB, PredBB, DT, NewInsts);
406 if (OpVal == 0) return 0;
407 GEPOps.push_back(OpVal);
408 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000409
410 GetElementPtrInst *Result =
Frits van Bommel2eb40f62011-07-25 15:13:01 +0000411 GetElementPtrInst::Create(GEPOps[0], makeArrayRef(GEPOps).slice(1),
Jay Foada9203102011-07-25 09:48:08 +0000412 InVal->getName()+".phi.trans.insert",
413 PredBB->getTerminator());
Chris Lattner9a864122009-12-07 18:36:53 +0000414 Result->setIsInBounds(GEP->isInBounds());
415 NewInsts.push_back(Result);
416 return Result;
417 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000418
Chris Lattner9a864122009-12-07 18:36:53 +0000419#if 0
420 // FIXME: This code works, but it is unclear that we actually want to insert
421 // a big chain of computation in order to make a value available in a block.
422 // This needs to be evaluated carefully to consider its cost trade offs.
Dan Gohman7feccd22010-11-18 17:06:31 +0000423
Chris Lattner9a864122009-12-07 18:36:53 +0000424 // Handle add with a constant RHS.
425 if (Inst->getOpcode() == Instruction::Add &&
426 isa<ConstantInt>(Inst->getOperand(1))) {
427 // PHI translate the LHS.
428 Value *OpVal = InsertPHITranslatedSubExpr(Inst->getOperand(0),
429 CurBB, PredBB, DT, NewInsts);
430 if (OpVal == 0) return 0;
Dan Gohman7feccd22010-11-18 17:06:31 +0000431
Chris Lattner9a864122009-12-07 18:36:53 +0000432 BinaryOperator *Res = BinaryOperator::CreateAdd(OpVal, Inst->getOperand(1),
433 InVal->getName()+".phi.trans.insert",
434 PredBB->getTerminator());
435 Res->setHasNoSignedWrap(cast<BinaryOperator>(Inst)->hasNoSignedWrap());
436 Res->setHasNoUnsignedWrap(cast<BinaryOperator>(Inst)->hasNoUnsignedWrap());
437 NewInsts.push_back(Res);
438 return Res;
439 }
440#endif
Dan Gohman7feccd22010-11-18 17:06:31 +0000441
Chris Lattner210e45a2009-12-04 02:10:16 +0000442 return 0;
443}