blob: 9c60a0463bc94713a5886fa152df5e3fbf7a2f9d [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.
David Majnemerfc41f632015-06-01 00:15:04 +0000153 bool isInput =
154 std::find(InstInputs.begin(), InstInputs.end(), Inst) != InstInputs.end();
Chris Lattnercfd76372009-12-09 00:10:55 +0000155
156 // Handle inputs instructions if needed.
157 if (isInput) {
158 if (Inst->getParent() != CurBB) {
159 // If it is an input defined in a different block, then it remains an
160 // input.
161 return Inst;
162 }
Chris Lattner37251f82009-12-09 00:18:13 +0000163
164 // If 'Inst' is defined in this block and is an input that needs to be phi
165 // translated, we need to incorporate the value into the expression or fail.
166
Chris Lattner11da6b02009-12-09 00:56:14 +0000167 // In either case, the instruction itself isn't an input any longer.
168 InstInputs.erase(std::find(InstInputs.begin(), InstInputs.end(), Inst));
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000169
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000170 // If this is a PHI, go ahead and translate it.
171 if (PHINode *PN = dyn_cast<PHINode>(Inst))
Chris Lattner11da6b02009-12-09 00:56:14 +0000172 return AddAsInput(PN->getIncomingValueForBlock(PredBB));
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000173
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000174 // If this is a non-phi value, and it is analyzable, we can incorporate it
175 // into the expression by making all instruction operands be inputs.
176 if (!CanPHITrans(Inst))
Craig Topper9f008862014-04-15 04:59:12 +0000177 return nullptr;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000178
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000179 // All instruction operands are now inputs (and of course, they may also be
180 // defined in this block, so they may need to be phi translated themselves.
181 for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i)
182 if (Instruction *Op = dyn_cast<Instruction>(Inst->getOperand(i)))
183 InstInputs.push_back(Op);
Chris Lattnere60244d2009-12-07 18:36:53 +0000184 }
185
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000186 // Ok, it must be an intermediate result (either because it started that way
187 // or because we just incorporated it into the expression). See if its
188 // operands need to be phi translated, and if so, reconstruct it.
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000189
Dan Gohman2e1fc842010-11-18 17:05:13 +0000190 if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
Craig Topper9f008862014-04-15 04:59:12 +0000191 if (!isSafeToSpeculativelyExecute(Cast)) return nullptr;
Dan Gohman2e1fc842010-11-18 17:05:13 +0000192 Value *PHIIn = PHITranslateSubExpr(Cast->getOperand(0), CurBB, PredBB, DT);
Craig Topper9f008862014-04-15 04:59:12 +0000193 if (!PHIIn) return nullptr;
Dan Gohman2e1fc842010-11-18 17:05:13 +0000194 if (PHIIn == Cast->getOperand(0))
195 return Cast;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000196
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000197 // Find an available version of this cast.
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000198
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000199 // Constants are trivial to find.
200 if (Constant *C = dyn_cast<Constant>(PHIIn))
Dan Gohman2e1fc842010-11-18 17:05:13 +0000201 return AddAsInput(ConstantExpr::getCast(Cast->getOpcode(),
202 C, Cast->getType()));
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000203
Dan Gohman2e1fc842010-11-18 17:05:13 +0000204 // Otherwise we have to see if a casted version of the incoming pointer
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000205 // is available. If so, we can use it, otherwise we have to fail.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000206 for (User *U : PHIIn->users()) {
207 if (CastInst *CastI = dyn_cast<CastInst>(U))
Dan Gohman2e1fc842010-11-18 17:05:13 +0000208 if (CastI->getOpcode() == Cast->getOpcode() &&
209 CastI->getType() == Cast->getType() &&
210 (!DT || DT->dominates(CastI->getParent(), PredBB)))
211 return CastI;
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000212 }
Craig Topper9f008862014-04-15 04:59:12 +0000213 return nullptr;
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000214 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000215
Chris Lattner77b0d3b2009-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 Lattner77b0d3b2009-12-07 19:04:49 +0000219 bool AnyChanged = false;
220 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
Daniel Dunbar693ea892010-02-24 08:48:04 +0000221 Value *GEPOp = PHITranslateSubExpr(GEP->getOperand(i), CurBB, PredBB, DT);
Craig Topper9f008862014-04-15 04:59:12 +0000222 if (!GEPOp) return nullptr;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000223
Chris Lattner9e34d152009-12-07 19:52:57 +0000224 AnyChanged |= GEPOp != GEP->getOperand(i);
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000225 GEPOps.push_back(GEPOp);
226 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000227
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000228 if (!AnyChanged)
229 return GEP;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000230
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000231 // Simplify the GEP to handle 'gep x, 0' -> x etc.
Manuel Jacob20c6d5b2016-01-17 22:46:43 +0000232 if (Value *V = SimplifyGEPInst(GEP->getSourceElementType(),
233 GEPOps, DL, TLI, DT, AC)) {
Chris Lattner11da6b02009-12-09 00:56:14 +0000234 for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
235 RemoveInstInputs(GEPOps[i], InstInputs);
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000236
Chris Lattner11da6b02009-12-09 00:56:14 +0000237 return AddAsInput(V);
238 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000239
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000240 // Scan to see if we have this GEP available.
241 Value *APHIOp = GEPOps[0];
Chandler Carruthcdf47882014-03-09 03:16:01 +0000242 for (User *U : APHIOp->users()) {
243 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U))
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000244 if (GEPI->getType() == GEP->getType() &&
245 GEPI->getNumOperands() == GEPOps.size() &&
Daniel Dunbar693ea892010-02-24 08:48:04 +0000246 GEPI->getParent()->getParent() == CurBB->getParent() &&
247 (!DT || DT->dominates(GEPI->getParent(), PredBB))) {
Benjamin Kramerc1c3c842015-06-09 20:41:21 +0000248 if (std::equal(GEPOps.begin(), GEPOps.end(), GEPI->op_begin()))
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000249 return GEPI;
250 }
251 }
Craig Topper9f008862014-04-15 04:59:12 +0000252 return nullptr;
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000253 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000254
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000255 // Handle add with a constant RHS.
256 if (Inst->getOpcode() == Instruction::Add &&
257 isa<ConstantInt>(Inst->getOperand(1))) {
258 // PHI translate the LHS.
259 Constant *RHS = cast<ConstantInt>(Inst->getOperand(1));
260 bool isNSW = cast<BinaryOperator>(Inst)->hasNoSignedWrap();
261 bool isNUW = cast<BinaryOperator>(Inst)->hasNoUnsignedWrap();
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000262
Daniel Dunbar693ea892010-02-24 08:48:04 +0000263 Value *LHS = PHITranslateSubExpr(Inst->getOperand(0), CurBB, PredBB, DT);
Craig Topper9f008862014-04-15 04:59:12 +0000264 if (!LHS) return nullptr;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000265
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000266 // If the PHI translated LHS is an add of a constant, fold the immediates.
267 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(LHS))
268 if (BOp->getOpcode() == Instruction::Add)
269 if (ConstantInt *CI = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
270 LHS = BOp->getOperand(0);
271 RHS = ConstantExpr::getAdd(RHS, CI);
272 isNSW = isNUW = false;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000273
Chris Lattner11da6b02009-12-09 00:56:14 +0000274 // If the old 'LHS' was an input, add the new 'LHS' as an input.
David Majnemerfc41f632015-06-01 00:15:04 +0000275 if (std::find(InstInputs.begin(), InstInputs.end(), BOp) !=
276 InstInputs.end()) {
Chris Lattner11da6b02009-12-09 00:56:14 +0000277 RemoveInstInputs(BOp, InstInputs);
278 AddAsInput(LHS);
279 }
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000280 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000281
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000282 // See if the add simplifies away.
Chandler Carruth66b31302015-01-04 12:03:27 +0000283 if (Value *Res = SimplifyAddInst(LHS, RHS, isNSW, isNUW, DL, TLI, DT, AC)) {
Chris Lattner11da6b02009-12-09 00:56:14 +0000284 // If we simplified the operands, the LHS is no longer an input, but Res
285 // is.
286 RemoveInstInputs(LHS, InstInputs);
287 return AddAsInput(Res);
288 }
Chris Lattner9f9010e2009-12-09 17:27:45 +0000289
290 // If we didn't modify the add, just return it.
291 if (LHS == Inst->getOperand(0) && RHS == Inst->getOperand(1))
292 return Inst;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000293
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000294 // Otherwise, see if we have this add available somewhere.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000295 for (User *U : LHS->users()) {
296 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U))
Chris Lattnerfa2e5362009-12-09 17:18:49 +0000297 if (BO->getOpcode() == Instruction::Add &&
298 BO->getOperand(0) == LHS && BO->getOperand(1) == RHS &&
Daniel Dunbar693ea892010-02-24 08:48:04 +0000299 BO->getParent()->getParent() == CurBB->getParent() &&
300 (!DT || DT->dominates(BO->getParent(), PredBB)))
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000301 return BO;
302 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000303
Craig Topper9f008862014-04-15 04:59:12 +0000304 return nullptr;
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000305 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000306
Chris Lattner77b0d3b2009-12-07 19:04:49 +0000307 // Otherwise, we failed.
Craig Topper9f008862014-04-15 04:59:12 +0000308 return nullptr;
Chris Lattnerb63051c2009-12-04 02:10:16 +0000309}
310
Chris Lattnere60244d2009-12-07 18:36:53 +0000311
312/// PHITranslateValue - PHI translate the current address up the CFG from
David Majnemer7666be72015-06-01 00:15:08 +0000313/// CurBB to Pred, updating our state to reflect any needed changes. If
314/// 'MustDominate' is true, the translated value must dominate
Daniel Dunbar693ea892010-02-24 08:48:04 +0000315/// PredBB. This returns true on failure and sets Addr to null.
316bool PHITransAddr::PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB,
David Majnemer7666be72015-06-01 00:15:08 +0000317 const DominatorTree *DT,
318 bool MustDominate) {
319 assert(DT || !MustDominate);
Chris Lattner0aa75682009-12-09 00:01:00 +0000320 assert(Verify() && "Invalid PHITransAddr!");
David Majnemer7666be72015-06-01 00:15:08 +0000321 if (DT && DT->isReachableFromEntry(PredBB))
322 Addr =
323 PHITranslateSubExpr(Addr, CurBB, PredBB, MustDominate ? DT : nullptr);
324 else
325 Addr = nullptr;
Chris Lattner0aa75682009-12-09 00:01:00 +0000326 assert(Verify() && "Invalid PHITransAddr!");
Daniel Dunbar693ea892010-02-24 08:48:04 +0000327
David Majnemer7666be72015-06-01 00:15:08 +0000328 if (MustDominate)
Daniel Dunbar693ea892010-02-24 08:48:04 +0000329 // 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))
Craig Topper9f008862014-04-15 04:59:12 +0000332 Addr = nullptr;
Daniel Dunbar693ea892010-02-24 08:48:04 +0000333
Craig Topper9f008862014-04-15 04:59:12 +0000334 return Addr == nullptr;
Chris Lattnere60244d2009-12-07 18:36:53 +0000335}
336
Chris Lattnere60244d2009-12-07 18:36:53 +0000337/// PHITranslateWithInsertion - PHI translate this value into the specified
338/// predecessor block, inserting a computation of the value if it is
339/// unavailable.
340///
341/// All newly created instructions are added to the NewInsts list. This
342/// returns null on failure.
343///
344Value *PHITransAddr::
345PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
346 const DominatorTree &DT,
347 SmallVectorImpl<Instruction*> &NewInsts) {
348 unsigned NISize = NewInsts.size();
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000349
Chris Lattnere60244d2009-12-07 18:36:53 +0000350 // Attempt to PHI translate with insertion.
351 Addr = InsertPHITranslatedSubExpr(Addr, CurBB, PredBB, DT, NewInsts);
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000352
Chris Lattnere60244d2009-12-07 18:36:53 +0000353 // If successful, return the new value.
354 if (Addr) return Addr;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000355
Chris Lattnere60244d2009-12-07 18:36:53 +0000356 // If not, destroy any intermediate instructions inserted.
357 while (NewInsts.size() != NISize)
358 NewInsts.pop_back_val()->eraseFromParent();
Craig Topper9f008862014-04-15 04:59:12 +0000359 return nullptr;
Chris Lattnere60244d2009-12-07 18:36:53 +0000360}
361
362
Chris Lattnerb63051c2009-12-04 02:10:16 +0000363/// InsertPHITranslatedPointer - Insert a computation of the PHI translated
364/// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
365/// block. All newly created instructions are added to the NewInsts list.
366/// This returns null on failure.
367///
368Value *PHITransAddr::
Chris Lattnere60244d2009-12-07 18:36:53 +0000369InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
370 BasicBlock *PredBB, const DominatorTree &DT,
371 SmallVectorImpl<Instruction*> &NewInsts) {
Chris Lattnerb63051c2009-12-04 02:10:16 +0000372 // See if we have a version of this value already available and dominating
Chris Lattnere60244d2009-12-07 18:36:53 +0000373 // PredBB. If so, there is no need to insert a new instance of it.
Chandler Carruth66b31302015-01-04 12:03:27 +0000374 PHITransAddr Tmp(InVal, DL, AC);
David Majnemer7666be72015-06-01 00:15:08 +0000375 if (!Tmp.PHITranslateValue(CurBB, PredBB, &DT, /*MustDominate=*/true))
Daniel Dunbar693ea892010-02-24 08:48:04 +0000376 return Tmp.getAddr();
Chris Lattnerb63051c2009-12-04 02:10:16 +0000377
David Majnemer4232fb32015-08-09 15:43:02 +0000378 // We don't need to PHI translate values which aren't instructions.
379 auto *Inst = dyn_cast<Instruction>(InVal);
380 if (!Inst)
381 return nullptr;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000382
Dan Gohman2e1fc842010-11-18 17:05:13 +0000383 // Handle cast of PHI translatable value.
384 if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
Craig Topper9f008862014-04-15 04:59:12 +0000385 if (!isSafeToSpeculativelyExecute(Cast)) return nullptr;
Dan Gohman2e1fc842010-11-18 17:05:13 +0000386 Value *OpVal = InsertPHITranslatedSubExpr(Cast->getOperand(0),
Chris Lattnere60244d2009-12-07 18:36:53 +0000387 CurBB, PredBB, DT, NewInsts);
Craig Topper9f008862014-04-15 04:59:12 +0000388 if (!OpVal) return nullptr;
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000389
Dan Gohman2e1fc842010-11-18 17:05:13 +0000390 // Otherwise insert a cast at the end of PredBB.
Alexey Samsonov89645df2015-06-10 17:37:38 +0000391 CastInst *New = CastInst::Create(Cast->getOpcode(), OpVal, InVal->getType(),
392 InVal->getName() + ".phi.trans.insert",
Dan Gohman2e1fc842010-11-18 17:05:13 +0000393 PredBB->getTerminator());
Alexey Samsonov89645df2015-06-10 17:37:38 +0000394 New->setDebugLoc(Inst->getDebugLoc());
Chris Lattnere60244d2009-12-07 18:36:53 +0000395 NewInsts.push_back(New);
396 return New;
397 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000398
Chris Lattnere60244d2009-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);
Craig Topper9f008862014-04-15 04:59:12 +0000406 if (!OpVal) return nullptr;
Chris Lattnere60244d2009-12-07 18:36:53 +0000407 GEPOps.push_back(OpVal);
408 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000409
David Blaikie096b1da2015-03-14 19:53:33 +0000410 GetElementPtrInst *Result = GetElementPtrInst::Create(
411 GEP->getSourceElementType(), GEPOps[0], makeArrayRef(GEPOps).slice(1),
412 InVal->getName() + ".phi.trans.insert", PredBB->getTerminator());
Alexey Samsonov89645df2015-06-10 17:37:38 +0000413 Result->setDebugLoc(Inst->getDebugLoc());
Chris Lattnere60244d2009-12-07 18:36:53 +0000414 Result->setIsInBounds(GEP->isInBounds());
415 NewInsts.push_back(Result);
416 return Result;
417 }
Dan Gohmanf1ebfc12010-11-18 17:06:31 +0000418
Chris Lattnere60244d2009-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 Gohmanf1ebfc12010-11-18 17:06:31 +0000423
Chris Lattnere60244d2009-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 Gohmanf1ebfc12010-11-18 17:06:31 +0000431
Chris Lattnere60244d2009-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 Gohmanf1ebfc12010-11-18 17:06:31 +0000441
Craig Topper9f008862014-04-15 04:59:12 +0000442 return nullptr;
Chris Lattnerb63051c2009-12-04 02:10:16 +0000443}