blob: bfe86425119be24c0a87d9414c16e8ebe019dabe [file] [log] [blame]
Chris Lattnerb63051c2009-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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/Analysis/InstructionSimplify.h"
Dan Gohman75d7d5e2011-12-14 23:49:11 +000016#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/Constants.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000018#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Instructions.h"
David Greene04e7ae62009-12-23 21:06:14 +000020#include "llvm/Support/Debug.h"
Dan Gohman0ab28b62010-11-18 17:05:57 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattner0aa75682009-12-09 00:01:00 +000022#include "llvm/Support/raw_ostream.h"
Chris Lattnerb63051c2009-12-04 02:10:16 +000023using namespace llvm;
24
Chris Lattner77b0d3b2009-12-07 19:04:49 +000025static bool CanPHITrans(Instruction *Inst) {
26 if (isa<PHINode>(Inst) ||
Chris Lattner77b0d3b2009-12-07 19:04:49 +000027 isa<GetElementPtrInst>(Inst))
28 return true;
Dan Gohman2e1fc842010-11-18 17:05:13 +000029
30 if (isa<CastInst>(Inst) &&
Dan Gohman75d7d5e2011-12-14 23:49:11 +000031 isSafeToSpeculativelyExecute(Inst))
Dan Gohman2e1fc842010-11-18 17:05:13 +000032 return true;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +000033
Chris Lattner6425a232009-12-08 06:06:26 +000034 if (Inst->getOpcode() == Instruction::Add &&
Chris Lattner77b0d3b2009-12-07 19:04:49 +000035 isa<ConstantInt>(Inst->getOperand(1)))
36 return true;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +000037
Chris Lattner77b0d3b2009-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 Ren49d684e2012-09-12 05:06:18 +000044#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Chris Lattner0aa75682009-12-09 00:01:00 +000045void PHITransAddr::dump() const {
Craig Topper9f008862014-04-15 04:59:12 +000046 if (!Addr) {
David Greene04e7ae62009-12-23 21:06:14 +000047 dbgs() << "PHITransAddr: null\n";
Chris Lattner0aa75682009-12-09 00:01:00 +000048 return;
49 }
David Greene04e7ae62009-12-23 21:06:14 +000050 dbgs() << "PHITransAddr: " << *Addr << "\n";
Chris Lattner0aa75682009-12-09 00:01:00 +000051 for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
David Greene04e7ae62009-12-23 21:06:14 +000052 dbgs() << " Input #" << i << " is " << *InstInputs[i] << "\n";
Chris Lattner0aa75682009-12-09 00:01:00 +000053}
Manman Renc3366cc2012-09-06 19:55:56 +000054#endif
Chris Lattner0aa75682009-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);
Craig Topper9f008862014-04-15 04:59:12 +000061 if (!I) return true;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +000062
Chris Lattner0aa75682009-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 Gohmanf1ebfc12010-11-18 17:06:31 +000071
Chris Lattner0aa75682009-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)) {
Alp Tokerf907b892013-12-05 05:44:44 +000075 errs() << "Instruction in PHITransAddr is not phi-translatable:\n";
David Greenef7905932009-12-23 23:27:15 +000076 errs() << *I << '\n';
Dan Gohman0ab28b62010-11-18 17:05:57 +000077 llvm_unreachable("Either something is missing from InstInputs or "
78 "CanPHITrans is wrong.");
Chris Lattner0aa75682009-12-09 00:01:00 +000079 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +000080
Chris Lattner0aa75682009-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 {
Craig Topper9f008862014-04-15 04:59:12 +000093 if (!Addr) return true;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +000094
95 SmallVector<Instruction*, 8> Tmp(InstInputs.begin(), InstInputs.end());
96
Chris Lattner0aa75682009-12-09 00:01:00 +000097 if (!VerifySubExpr(Addr, Tmp))
98 return false;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +000099
Chris Lattner0aa75682009-12-09 00:01:00 +0000100 if (!Tmp.empty()) {
Dan Gohman0ab28b62010-11-18 17:05:57 +0000101 errs() << "PHITransAddr contains extra instructions:\n";
Chris Lattner0aa75682009-12-09 00:01:00 +0000102 for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
David Greenef7905932009-12-23 23:27:15 +0000103 errs() << " InstInput #" << i << " is " << *InstInputs[i] << "\n";
Dan Gohman0ab28b62010-11-18 17:05:57 +0000104 llvm_unreachable("This is unexpected.");
Chris Lattner0aa75682009-12-09 00:01:00 +0000105 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000106
Chris Lattner0aa75682009-12-09 00:01:00 +0000107 // a-ok.
108 return true;
109}
110
111
Chris Lattnere60244d2009-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);
Craig Topper9f008862014-04-15 04:59:12 +0000119 return !Inst || CanPHITrans(Inst);
Chris Lattnerb63051c2009-12-04 02:10:16 +0000120}
121
Chris Lattnere60244d2009-12-07 18:36:53 +0000122
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000123static void RemoveInstInputs(Value *V,
Chris Lattner5cf4b742009-12-08 23:42:51 +0000124 SmallVectorImpl<Instruction*> &InstInputs) {
Chris Lattner11da6b02009-12-09 00:56:14 +0000125 Instruction *I = dyn_cast<Instruction>(V);
Craig Topper9f008862014-04-15 04:59:12 +0000126 if (!I) return;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000127
Chris Lattner5cf4b742009-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 Gohmanf1ebfc12010-11-18 17:06:31 +0000135
Chris Lattner11da6b02009-12-09 00:56:14 +0000136 assert(!isa<PHINode>(I) && "Error, removing something that isn't an input");
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000137
Chris Lattner11da6b02009-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 Lattner5cf4b742009-12-08 23:42:51 +0000143}
144
Chris Lattnere60244d2009-12-07 18:36:53 +0000145Value *PHITransAddr::PHITranslateSubExpr(Value *V, BasicBlock *CurBB,
Daniel Dunbar693ea892010-02-24 08:48:04 +0000146 BasicBlock *PredBB,
147 const DominatorTree *DT) {
Chris Lattnere60244d2009-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);
Craig Topper9f008862014-04-15 04:59:12 +0000150 if (!Inst) return V;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000151
Chris Lattnercfd76372009-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 Lattner37251f82009-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 Lattner11da6b02009-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 Gohmanf1ebfc12010-11-18 17:06:31 +0000168
Chris Lattner77b0d3b2009-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 Lattner11da6b02009-12-09 00:56:14 +0000171 return AddAsInput(PN->getIncomingValueForBlock(PredBB));
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000172
Chris Lattner77b0d3b2009-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))
Craig Topper9f008862014-04-15 04:59:12 +0000176 return nullptr;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000177
Chris Lattner77b0d3b2009-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 Lattnere60244d2009-12-07 18:36:53 +0000183 }
184
Chris Lattner77b0d3b2009-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 Gohmanf1ebfc12010-11-18 17:06:31 +0000188
Dan Gohman2e1fc842010-11-18 17:05:13 +0000189 if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
Craig Topper9f008862014-04-15 04:59:12 +0000190 if (!isSafeToSpeculativelyExecute(Cast)) return nullptr;
Dan Gohman2e1fc842010-11-18 17:05:13 +0000191 Value *PHIIn = PHITranslateSubExpr(Cast->getOperand(0), CurBB, PredBB, DT);
Craig Topper9f008862014-04-15 04:59:12 +0000192 if (!PHIIn) return nullptr;
Dan Gohman2e1fc842010-11-18 17:05:13 +0000193 if (PHIIn == Cast->getOperand(0))
194 return Cast;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000195
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000196 // Find an available version of this cast.
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000197
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000198 // Constants are trivial to find.
199 if (Constant *C = dyn_cast<Constant>(PHIIn))
Dan Gohman2e1fc842010-11-18 17:05:13 +0000200 return AddAsInput(ConstantExpr::getCast(Cast->getOpcode(),
201 C, Cast->getType()));
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000202
Dan Gohman2e1fc842010-11-18 17:05:13 +0000203 // Otherwise we have to see if a casted version of the incoming pointer
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000204 // is available. If so, we can use it, otherwise we have to fail.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000205 for (User *U : PHIIn->users()) {
206 if (CastInst *CastI = dyn_cast<CastInst>(U))
Dan Gohman2e1fc842010-11-18 17:05:13 +0000207 if (CastI->getOpcode() == Cast->getOpcode() &&
208 CastI->getType() == Cast->getType() &&
209 (!DT || DT->dominates(CastI->getParent(), PredBB)))
210 return CastI;
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000211 }
Craig Topper9f008862014-04-15 04:59:12 +0000212 return nullptr;
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000213 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000214
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000215 // Handle getelementptr with at least one PHI translatable operand.
216 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
217 SmallVector<Value*, 8> GEPOps;
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000218 bool AnyChanged = false;
219 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
Daniel Dunbar693ea892010-02-24 08:48:04 +0000220 Value *GEPOp = PHITranslateSubExpr(GEP->getOperand(i), CurBB, PredBB, DT);
Craig Topper9f008862014-04-15 04:59:12 +0000221 if (!GEPOp) return nullptr;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000222
Chris Lattner9e34d152009-12-07 19:52:57 +0000223 AnyChanged |= GEPOp != GEP->getOperand(i);
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000224 GEPOps.push_back(GEPOp);
225 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000226
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000227 if (!AnyChanged)
228 return GEP;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000229
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000230 // Simplify the GEP to handle 'gep x, 0' -> x etc.
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000231 if (Value *V = SimplifyGEPInst(GEPOps, DL, TLI, DT)) {
Chris Lattner11da6b02009-12-09 00:56:14 +0000232 for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
233 RemoveInstInputs(GEPOps[i], InstInputs);
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000234
Chris Lattner11da6b02009-12-09 00:56:14 +0000235 return AddAsInput(V);
236 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000237
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000238 // Scan to see if we have this GEP available.
239 Value *APHIOp = GEPOps[0];
Chandler Carruthcdf47882014-03-09 03:16:01 +0000240 for (User *U : APHIOp->users()) {
241 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U))
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000242 if (GEPI->getType() == GEP->getType() &&
243 GEPI->getNumOperands() == GEPOps.size() &&
Daniel Dunbar693ea892010-02-24 08:48:04 +0000244 GEPI->getParent()->getParent() == CurBB->getParent() &&
245 (!DT || DT->dominates(GEPI->getParent(), PredBB))) {
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000246 bool Mismatch = false;
247 for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
248 if (GEPI->getOperand(i) != GEPOps[i]) {
249 Mismatch = true;
250 break;
251 }
252 if (!Mismatch)
253 return GEPI;
254 }
255 }
Craig Topper9f008862014-04-15 04:59:12 +0000256 return nullptr;
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000257 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000258
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000259 // Handle add with a constant RHS.
260 if (Inst->getOpcode() == Instruction::Add &&
261 isa<ConstantInt>(Inst->getOperand(1))) {
262 // PHI translate the LHS.
263 Constant *RHS = cast<ConstantInt>(Inst->getOperand(1));
264 bool isNSW = cast<BinaryOperator>(Inst)->hasNoSignedWrap();
265 bool isNUW = cast<BinaryOperator>(Inst)->hasNoUnsignedWrap();
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000266
Daniel Dunbar693ea892010-02-24 08:48:04 +0000267 Value *LHS = PHITranslateSubExpr(Inst->getOperand(0), CurBB, PredBB, DT);
Craig Topper9f008862014-04-15 04:59:12 +0000268 if (!LHS) return nullptr;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000269
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000270 // If the PHI translated LHS is an add of a constant, fold the immediates.
271 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(LHS))
272 if (BOp->getOpcode() == Instruction::Add)
273 if (ConstantInt *CI = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
274 LHS = BOp->getOperand(0);
275 RHS = ConstantExpr::getAdd(RHS, CI);
276 isNSW = isNUW = false;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000277
Chris Lattner11da6b02009-12-09 00:56:14 +0000278 // If the old 'LHS' was an input, add the new 'LHS' as an input.
279 if (std::count(InstInputs.begin(), InstInputs.end(), BOp)) {
280 RemoveInstInputs(BOp, InstInputs);
281 AddAsInput(LHS);
282 }
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000283 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000284
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000285 // See if the add simplifies away.
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000286 if (Value *Res = SimplifyAddInst(LHS, RHS, isNSW, isNUW, DL, TLI, DT)) {
Chris Lattner11da6b02009-12-09 00:56:14 +0000287 // If we simplified the operands, the LHS is no longer an input, but Res
288 // is.
289 RemoveInstInputs(LHS, InstInputs);
290 return AddAsInput(Res);
291 }
Chris Lattner9f9010e2009-12-09 17:27:45 +0000292
293 // If we didn't modify the add, just return it.
294 if (LHS == Inst->getOperand(0) && RHS == Inst->getOperand(1))
295 return Inst;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000296
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000297 // Otherwise, see if we have this add available somewhere.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000298 for (User *U : LHS->users()) {
299 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U))
Chris Lattnerfa2e5362009-12-09 17:18:49 +0000300 if (BO->getOpcode() == Instruction::Add &&
301 BO->getOperand(0) == LHS && BO->getOperand(1) == RHS &&
Daniel Dunbar693ea892010-02-24 08:48:04 +0000302 BO->getParent()->getParent() == CurBB->getParent() &&
303 (!DT || DT->dominates(BO->getParent(), PredBB)))
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000304 return BO;
305 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000306
Craig Topper9f008862014-04-15 04:59:12 +0000307 return nullptr;
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000308 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000309
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000310 // Otherwise, we failed.
Craig Topper9f008862014-04-15 04:59:12 +0000311 return nullptr;
Chris Lattnerb63051c2009-12-04 02:10:16 +0000312}
313
Chris Lattnere60244d2009-12-07 18:36:53 +0000314
315/// PHITranslateValue - PHI translate the current address up the CFG from
Daniel Dunbar693ea892010-02-24 08:48:04 +0000316/// CurBB to Pred, updating our state to reflect any needed changes. If the
317/// dominator tree DT is non-null, the translated value must dominate
318/// PredBB. This returns true on failure and sets Addr to null.
319bool PHITransAddr::PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB,
320 const DominatorTree *DT) {
Chris Lattner0aa75682009-12-09 00:01:00 +0000321 assert(Verify() && "Invalid PHITransAddr!");
Daniel Dunbar693ea892010-02-24 08:48:04 +0000322 Addr = PHITranslateSubExpr(Addr, CurBB, PredBB, DT);
Chris Lattner0aa75682009-12-09 00:01:00 +0000323 assert(Verify() && "Invalid PHITransAddr!");
Daniel Dunbar693ea892010-02-24 08:48:04 +0000324
325 if (DT) {
326 // Make sure the value is live in the predecessor.
327 if (Instruction *Inst = dyn_cast_or_null<Instruction>(Addr))
328 if (!DT->dominates(Inst->getParent(), PredBB))
Craig Topper9f008862014-04-15 04:59:12 +0000329 Addr = nullptr;
Daniel Dunbar693ea892010-02-24 08:48:04 +0000330 }
331
Craig Topper9f008862014-04-15 04:59:12 +0000332 return Addr == nullptr;
Chris Lattnere60244d2009-12-07 18:36:53 +0000333}
334
Chris Lattnere60244d2009-12-07 18:36:53 +0000335/// PHITranslateWithInsertion - PHI translate this value into the specified
336/// predecessor block, inserting a computation of the value if it is
337/// unavailable.
338///
339/// All newly created instructions are added to the NewInsts list. This
340/// returns null on failure.
341///
342Value *PHITransAddr::
343PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
344 const DominatorTree &DT,
345 SmallVectorImpl<Instruction*> &NewInsts) {
346 unsigned NISize = NewInsts.size();
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000347
Chris Lattnere60244d2009-12-07 18:36:53 +0000348 // Attempt to PHI translate with insertion.
349 Addr = InsertPHITranslatedSubExpr(Addr, CurBB, PredBB, DT, NewInsts);
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000350
Chris Lattnere60244d2009-12-07 18:36:53 +0000351 // If successful, return the new value.
352 if (Addr) return Addr;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000353
Chris Lattnere60244d2009-12-07 18:36:53 +0000354 // If not, destroy any intermediate instructions inserted.
355 while (NewInsts.size() != NISize)
356 NewInsts.pop_back_val()->eraseFromParent();
Craig Topper9f008862014-04-15 04:59:12 +0000357 return nullptr;
Chris Lattnere60244d2009-12-07 18:36:53 +0000358}
359
360
Chris Lattnerb63051c2009-12-04 02:10:16 +0000361/// InsertPHITranslatedPointer - Insert a computation of the PHI translated
362/// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
363/// block. All newly created instructions are added to the NewInsts list.
364/// This returns null on failure.
365///
366Value *PHITransAddr::
Chris Lattnere60244d2009-12-07 18:36:53 +0000367InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
368 BasicBlock *PredBB, const DominatorTree &DT,
369 SmallVectorImpl<Instruction*> &NewInsts) {
Chris Lattnerb63051c2009-12-04 02:10:16 +0000370 // See if we have a version of this value already available and dominating
Chris Lattnere60244d2009-12-07 18:36:53 +0000371 // PredBB. If so, there is no need to insert a new instance of it.
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000372 PHITransAddr Tmp(InVal, DL);
Daniel Dunbar693ea892010-02-24 08:48:04 +0000373 if (!Tmp.PHITranslateValue(CurBB, PredBB, &DT))
374 return Tmp.getAddr();
Chris Lattnerb63051c2009-12-04 02:10:16 +0000375
Chris Lattnere60244d2009-12-07 18:36:53 +0000376 // If we don't have an available version of this value, it must be an
377 // instruction.
378 Instruction *Inst = cast<Instruction>(InVal);
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000379
Dan Gohman2e1fc842010-11-18 17:05:13 +0000380 // Handle cast of PHI translatable value.
381 if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
Craig Topper9f008862014-04-15 04:59:12 +0000382 if (!isSafeToSpeculativelyExecute(Cast)) return nullptr;
Dan Gohman2e1fc842010-11-18 17:05:13 +0000383 Value *OpVal = InsertPHITranslatedSubExpr(Cast->getOperand(0),
Chris Lattnere60244d2009-12-07 18:36:53 +0000384 CurBB, PredBB, DT, NewInsts);
Craig Topper9f008862014-04-15 04:59:12 +0000385 if (!OpVal) return nullptr;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000386
Dan Gohman2e1fc842010-11-18 17:05:13 +0000387 // Otherwise insert a cast at the end of PredBB.
388 CastInst *New = CastInst::Create(Cast->getOpcode(),
389 OpVal, InVal->getType(),
390 InVal->getName()+".phi.trans.insert",
391 PredBB->getTerminator());
Chris Lattnere60244d2009-12-07 18:36:53 +0000392 NewInsts.push_back(New);
393 return New;
394 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000395
Chris Lattnere60244d2009-12-07 18:36:53 +0000396 // Handle getelementptr with at least one PHI operand.
397 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
398 SmallVector<Value*, 8> GEPOps;
399 BasicBlock *CurBB = GEP->getParent();
400 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
401 Value *OpVal = InsertPHITranslatedSubExpr(GEP->getOperand(i),
402 CurBB, PredBB, DT, NewInsts);
Craig Topper9f008862014-04-15 04:59:12 +0000403 if (!OpVal) return nullptr;
Chris Lattnere60244d2009-12-07 18:36:53 +0000404 GEPOps.push_back(OpVal);
405 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000406
407 GetElementPtrInst *Result =
Frits van Bommelede0dc62011-07-25 15:13:01 +0000408 GetElementPtrInst::Create(GEPOps[0], makeArrayRef(GEPOps).slice(1),
Jay Foadd1b78492011-07-25 09:48:08 +0000409 InVal->getName()+".phi.trans.insert",
410 PredBB->getTerminator());
Chris Lattnere60244d2009-12-07 18:36:53 +0000411 Result->setIsInBounds(GEP->isInBounds());
412 NewInsts.push_back(Result);
413 return Result;
414 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000415
Chris Lattnere60244d2009-12-07 18:36:53 +0000416#if 0
417 // FIXME: This code works, but it is unclear that we actually want to insert
418 // a big chain of computation in order to make a value available in a block.
419 // This needs to be evaluated carefully to consider its cost trade offs.
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000420
Chris Lattnere60244d2009-12-07 18:36:53 +0000421 // Handle add with a constant RHS.
422 if (Inst->getOpcode() == Instruction::Add &&
423 isa<ConstantInt>(Inst->getOperand(1))) {
424 // PHI translate the LHS.
425 Value *OpVal = InsertPHITranslatedSubExpr(Inst->getOperand(0),
426 CurBB, PredBB, DT, NewInsts);
427 if (OpVal == 0) return 0;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000428
Chris Lattnere60244d2009-12-07 18:36:53 +0000429 BinaryOperator *Res = BinaryOperator::CreateAdd(OpVal, Inst->getOperand(1),
430 InVal->getName()+".phi.trans.insert",
431 PredBB->getTerminator());
432 Res->setHasNoSignedWrap(cast<BinaryOperator>(Inst)->hasNoSignedWrap());
433 Res->setHasNoUnsignedWrap(cast<BinaryOperator>(Inst)->hasNoUnsignedWrap());
434 NewInsts.push_back(Res);
435 return Res;
436 }
437#endif
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000438
Craig Topper9f008862014-04-15 04:59:12 +0000439 return nullptr;
Chris Lattnerb63051c2009-12-04 02:10:16 +0000440}