blob: 858f08f6537a88db1b1572988787c42d359e469a [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/Analysis/InstructionSimplify.h"
Dan Gohmanf0426602011-12-14 23:49:11 +000016#include "llvm/Analysis/ValueTracking.h"
Nico Weber0f38c602018-04-30 14:59:11 +000017#include "llvm/Config/llvm-config.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000018#include "llvm/IR/Constants.h"
Chandler Carruth56e13942014-01-13 09:26:24 +000019#include "llvm/IR/Dominators.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Instructions.h"
David Greene2a0f3cc2009-12-23 21:06:14 +000021#include "llvm/Support/Debug.h"
Dan Gohman88fc03c2010-11-18 17:05:57 +000022#include "llvm/Support/ErrorHandling.h"
Chris Lattner7dedbf42009-12-09 00:01:00 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattner210e45a2009-12-04 02:10:16 +000024using namespace llvm;
25
Chris Lattner6fcca1c2009-12-07 19:04:49 +000026static bool CanPHITrans(Instruction *Inst) {
27 if (isa<PHINode>(Inst) ||
Chris Lattner6fcca1c2009-12-07 19:04:49 +000028 isa<GetElementPtrInst>(Inst))
29 return true;
Dan Gohmance562622010-11-18 17:05:13 +000030
31 if (isa<CastInst>(Inst) &&
Dan Gohmanf0426602011-12-14 23:49:11 +000032 isSafeToSpeculativelyExecute(Inst))
Dan Gohmance562622010-11-18 17:05:13 +000033 return true;
Dan Gohman7feccd22010-11-18 17:06:31 +000034
Chris Lattner34f84902009-12-08 06:06:26 +000035 if (Inst->getOpcode() == Instruction::Add &&
Chris Lattner6fcca1c2009-12-07 19:04:49 +000036 isa<ConstantInt>(Inst->getOperand(1)))
37 return true;
Dan Gohman7feccd22010-11-18 17:06:31 +000038
Chris Lattner6fcca1c2009-12-07 19:04:49 +000039 // cerr << "MEMDEP: Could not PHI translate: " << *Pointer;
40 // if (isa<BitCastInst>(PtrInst) || isa<GetElementPtrInst>(PtrInst))
41 // cerr << "OP:\t\t\t\t" << *PtrInst->getOperand(0);
42 return false;
43}
44
Aaron Ballman1d03d382017-10-15 14:32:27 +000045#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Keren55307982016-01-29 20:50:44 +000046LLVM_DUMP_METHOD void PHITransAddr::dump() const {
Craig Topper570e52c2014-04-15 04:59:12 +000047 if (!Addr) {
David Greene2a0f3cc2009-12-23 21:06:14 +000048 dbgs() << "PHITransAddr: null\n";
Chris Lattner7dedbf42009-12-09 00:01:00 +000049 return;
50 }
David Greene2a0f3cc2009-12-23 21:06:14 +000051 dbgs() << "PHITransAddr: " << *Addr << "\n";
Chris Lattner7dedbf42009-12-09 00:01:00 +000052 for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
David Greene2a0f3cc2009-12-23 21:06:14 +000053 dbgs() << " Input #" << i << " is " << *InstInputs[i] << "\n";
Chris Lattner7dedbf42009-12-09 00:01:00 +000054}
Manman Rencc77eec2012-09-06 19:55:56 +000055#endif
Chris Lattner7dedbf42009-12-09 00:01:00 +000056
57
58static bool VerifySubExpr(Value *Expr,
59 SmallVectorImpl<Instruction*> &InstInputs) {
60 // If this is a non-instruction value, there is nothing to do.
61 Instruction *I = dyn_cast<Instruction>(Expr);
Craig Topper570e52c2014-04-15 04:59:12 +000062 if (!I) return true;
Dan Gohman7feccd22010-11-18 17:06:31 +000063
Chris Lattner7dedbf42009-12-09 00:01:00 +000064 // If it's an instruction, it is either in Tmp or its operands recursively
65 // are.
David Majnemer975248e2016-08-11 22:21:41 +000066 SmallVectorImpl<Instruction *>::iterator Entry = find(InstInputs, I);
Chris Lattner7dedbf42009-12-09 00:01:00 +000067 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)) {
Alp Toker087ab612013-12-05 05:44:44 +000075 errs() << "Instruction in PHITransAddr is not phi-translatable:\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 {
Craig Topper570e52c2014-04-15 04:59:12 +000093 if (!Addr) 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);
Craig Topper570e52c2014-04-15 04:59:12 +0000119 return !Inst || 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);
Craig Topper570e52c2014-04-15 04:59:12 +0000126 if (!I) 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.
David Majnemer975248e2016-08-11 22:21:41 +0000129 SmallVectorImpl<Instruction *>::iterator Entry = find(InstInputs, I);
Chris Lattner43678f42009-12-08 23:42:51 +0000130 if (Entry != InstInputs.end()) {
131 InstInputs.erase(Entry);
132 return;
133 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000134
Chris Lattner6200e532009-12-09 00:56:14 +0000135 assert(!isa<PHINode>(I) && "Error, removing something that isn't an input");
Dan Gohman7feccd22010-11-18 17:06:31 +0000136
Chris Lattner6200e532009-12-09 00:56:14 +0000137 // Otherwise, it must have instruction inputs itself. Zap them recursively.
138 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
139 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
140 RemoveInstInputs(Op, InstInputs);
141 }
Chris Lattner43678f42009-12-08 23:42:51 +0000142}
143
Chris Lattner9a864122009-12-07 18:36:53 +0000144Value *PHITransAddr::PHITranslateSubExpr(Value *V, BasicBlock *CurBB,
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000145 BasicBlock *PredBB,
146 const DominatorTree *DT) {
Chris Lattner9a864122009-12-07 18:36:53 +0000147 // If this is a non-instruction value, it can't require PHI translation.
148 Instruction *Inst = dyn_cast<Instruction>(V);
Craig Topper570e52c2014-04-15 04:59:12 +0000149 if (!Inst) return V;
Dan Gohman7feccd22010-11-18 17:06:31 +0000150
Chris Lattneraf503152009-12-09 00:10:55 +0000151 // Determine whether 'Inst' is an input to our PHI translatable expression.
David Majnemer975248e2016-08-11 22:21:41 +0000152 bool isInput = is_contained(InstInputs, Inst);
Chris Lattneraf503152009-12-09 00:10:55 +0000153
154 // Handle inputs instructions if needed.
155 if (isInput) {
156 if (Inst->getParent() != CurBB) {
157 // If it is an input defined in a different block, then it remains an
158 // input.
159 return Inst;
160 }
Chris Lattnere09e98c2009-12-09 00:18:13 +0000161
162 // If 'Inst' is defined in this block and is an input that needs to be phi
163 // translated, we need to incorporate the value into the expression or fail.
164
Chris Lattner6200e532009-12-09 00:56:14 +0000165 // In either case, the instruction itself isn't an input any longer.
David Majnemer975248e2016-08-11 22:21:41 +0000166 InstInputs.erase(find(InstInputs, Inst));
Dan Gohman7feccd22010-11-18 17:06:31 +0000167
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000168 // If this is a PHI, go ahead and translate it.
169 if (PHINode *PN = dyn_cast<PHINode>(Inst))
Chris Lattner6200e532009-12-09 00:56:14 +0000170 return AddAsInput(PN->getIncomingValueForBlock(PredBB));
Dan Gohman7feccd22010-11-18 17:06:31 +0000171
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000172 // If this is a non-phi value, and it is analyzable, we can incorporate it
173 // into the expression by making all instruction operands be inputs.
174 if (!CanPHITrans(Inst))
Craig Topper570e52c2014-04-15 04:59:12 +0000175 return nullptr;
Dan Gohman7feccd22010-11-18 17:06:31 +0000176
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000177 // All instruction operands are now inputs (and of course, they may also be
178 // defined in this block, so they may need to be phi translated themselves.
179 for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i)
180 if (Instruction *Op = dyn_cast<Instruction>(Inst->getOperand(i)))
181 InstInputs.push_back(Op);
Chris Lattner9a864122009-12-07 18:36:53 +0000182 }
183
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000184 // Ok, it must be an intermediate result (either because it started that way
185 // or because we just incorporated it into the expression). See if its
186 // operands need to be phi translated, and if so, reconstruct it.
Dan Gohman7feccd22010-11-18 17:06:31 +0000187
Dan Gohmance562622010-11-18 17:05:13 +0000188 if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
Craig Topper570e52c2014-04-15 04:59:12 +0000189 if (!isSafeToSpeculativelyExecute(Cast)) return nullptr;
Dan Gohmance562622010-11-18 17:05:13 +0000190 Value *PHIIn = PHITranslateSubExpr(Cast->getOperand(0), CurBB, PredBB, DT);
Craig Topper570e52c2014-04-15 04:59:12 +0000191 if (!PHIIn) return nullptr;
Dan Gohmance562622010-11-18 17:05:13 +0000192 if (PHIIn == Cast->getOperand(0))
193 return Cast;
Dan Gohman7feccd22010-11-18 17:06:31 +0000194
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000195 // Find an available version of this cast.
Dan Gohman7feccd22010-11-18 17:06:31 +0000196
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000197 // Constants are trivial to find.
198 if (Constant *C = dyn_cast<Constant>(PHIIn))
Dan Gohmance562622010-11-18 17:05:13 +0000199 return AddAsInput(ConstantExpr::getCast(Cast->getOpcode(),
200 C, Cast->getType()));
Dan Gohman7feccd22010-11-18 17:06:31 +0000201
Dan Gohmance562622010-11-18 17:05:13 +0000202 // Otherwise we have to see if a casted version of the incoming pointer
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000203 // is available. If so, we can use it, otherwise we have to fail.
Chandler Carruth36b699f2014-03-09 03:16:01 +0000204 for (User *U : PHIIn->users()) {
205 if (CastInst *CastI = dyn_cast<CastInst>(U))
Dan Gohmance562622010-11-18 17:05:13 +0000206 if (CastI->getOpcode() == Cast->getOpcode() &&
207 CastI->getType() == Cast->getType() &&
208 (!DT || DT->dominates(CastI->getParent(), PredBB)))
209 return CastI;
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000210 }
Craig Topper570e52c2014-04-15 04:59:12 +0000211 return nullptr;
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000212 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000213
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000214 // Handle getelementptr with at least one PHI translatable operand.
215 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
216 SmallVector<Value*, 8> GEPOps;
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000217 bool AnyChanged = false;
218 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000219 Value *GEPOp = PHITranslateSubExpr(GEP->getOperand(i), CurBB, PredBB, DT);
Craig Topper570e52c2014-04-15 04:59:12 +0000220 if (!GEPOp) return nullptr;
Dan Gohman7feccd22010-11-18 17:06:31 +0000221
Chris Lattner60454172009-12-07 19:52:57 +0000222 AnyChanged |= GEPOp != GEP->getOperand(i);
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000223 GEPOps.push_back(GEPOp);
224 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000225
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000226 if (!AnyChanged)
227 return GEP;
Dan Gohman7feccd22010-11-18 17:06:31 +0000228
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000229 // Simplify the GEP to handle 'gep x, 0' -> x etc.
Manuel Jacobb7d824d2016-01-17 22:46:43 +0000230 if (Value *V = SimplifyGEPInst(GEP->getSourceElementType(),
Daniel Berlin5c3bf012017-04-26 20:56:13 +0000231 GEPOps, {DL, TLI, DT, AC})) {
Chris Lattner6200e532009-12-09 00:56:14 +0000232 for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
233 RemoveInstInputs(GEPOps[i], InstInputs);
Dan Gohman7feccd22010-11-18 17:06:31 +0000234
Chris Lattner6200e532009-12-09 00:56:14 +0000235 return AddAsInput(V);
236 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000237
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000238 // Scan to see if we have this GEP available.
239 Value *APHIOp = GEPOps[0];
Chandler Carruth36b699f2014-03-09 03:16:01 +0000240 for (User *U : APHIOp->users()) {
241 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U))
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000242 if (GEPI->getType() == GEP->getType() &&
243 GEPI->getNumOperands() == GEPOps.size() &&
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000244 GEPI->getParent()->getParent() == CurBB->getParent() &&
245 (!DT || DT->dominates(GEPI->getParent(), PredBB))) {
Benjamin Kramerea8e85d2015-06-09 20:41:21 +0000246 if (std::equal(GEPOps.begin(), GEPOps.end(), GEPI->op_begin()))
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000247 return GEPI;
248 }
249 }
Craig Topper570e52c2014-04-15 04:59:12 +0000250 return nullptr;
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000251 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000252
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000253 // Handle add with a constant RHS.
254 if (Inst->getOpcode() == Instruction::Add &&
255 isa<ConstantInt>(Inst->getOperand(1))) {
256 // PHI translate the LHS.
257 Constant *RHS = cast<ConstantInt>(Inst->getOperand(1));
258 bool isNSW = cast<BinaryOperator>(Inst)->hasNoSignedWrap();
259 bool isNUW = cast<BinaryOperator>(Inst)->hasNoUnsignedWrap();
Dan Gohman7feccd22010-11-18 17:06:31 +0000260
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000261 Value *LHS = PHITranslateSubExpr(Inst->getOperand(0), CurBB, PredBB, DT);
Craig Topper570e52c2014-04-15 04:59:12 +0000262 if (!LHS) return nullptr;
Dan Gohman7feccd22010-11-18 17:06:31 +0000263
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000264 // If the PHI translated LHS is an add of a constant, fold the immediates.
265 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(LHS))
266 if (BOp->getOpcode() == Instruction::Add)
267 if (ConstantInt *CI = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
268 LHS = BOp->getOperand(0);
269 RHS = ConstantExpr::getAdd(RHS, CI);
270 isNSW = isNUW = false;
Dan Gohman7feccd22010-11-18 17:06:31 +0000271
Chris Lattner6200e532009-12-09 00:56:14 +0000272 // If the old 'LHS' was an input, add the new 'LHS' as an input.
David Majnemer975248e2016-08-11 22:21:41 +0000273 if (is_contained(InstInputs, BOp)) {
Chris Lattner6200e532009-12-09 00:56:14 +0000274 RemoveInstInputs(BOp, InstInputs);
275 AddAsInput(LHS);
276 }
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000277 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000278
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000279 // See if the add simplifies away.
Daniel Berlin5c3bf012017-04-26 20:56:13 +0000280 if (Value *Res = SimplifyAddInst(LHS, RHS, isNSW, isNUW, {DL, TLI, DT, AC})) {
Chris Lattner6200e532009-12-09 00:56:14 +0000281 // If we simplified the operands, the LHS is no longer an input, but Res
282 // is.
283 RemoveInstInputs(LHS, InstInputs);
284 return AddAsInput(Res);
285 }
Chris Lattner4d3a16f2009-12-09 17:27:45 +0000286
287 // If we didn't modify the add, just return it.
288 if (LHS == Inst->getOperand(0) && RHS == Inst->getOperand(1))
289 return Inst;
Dan Gohman7feccd22010-11-18 17:06:31 +0000290
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000291 // Otherwise, see if we have this add available somewhere.
Chandler Carruth36b699f2014-03-09 03:16:01 +0000292 for (User *U : LHS->users()) {
293 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U))
Chris Lattnereddc65a2009-12-09 17:18:49 +0000294 if (BO->getOpcode() == Instruction::Add &&
295 BO->getOperand(0) == LHS && BO->getOperand(1) == RHS &&
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000296 BO->getParent()->getParent() == CurBB->getParent() &&
297 (!DT || DT->dominates(BO->getParent(), PredBB)))
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000298 return BO;
299 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000300
Craig Topper570e52c2014-04-15 04:59:12 +0000301 return nullptr;
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000302 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000303
Chris Lattner6fcca1c2009-12-07 19:04:49 +0000304 // Otherwise, we failed.
Craig Topper570e52c2014-04-15 04:59:12 +0000305 return nullptr;
Chris Lattner210e45a2009-12-04 02:10:16 +0000306}
307
Chris Lattner9a864122009-12-07 18:36:53 +0000308
309/// PHITranslateValue - PHI translate the current address up the CFG from
David Majnemer75ce6bb2015-06-01 00:15:08 +0000310/// CurBB to Pred, updating our state to reflect any needed changes. If
311/// 'MustDominate' is true, the translated value must dominate
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000312/// PredBB. This returns true on failure and sets Addr to null.
313bool PHITransAddr::PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB,
David Majnemer75ce6bb2015-06-01 00:15:08 +0000314 const DominatorTree *DT,
315 bool MustDominate) {
316 assert(DT || !MustDominate);
Chris Lattner7dedbf42009-12-09 00:01:00 +0000317 assert(Verify() && "Invalid PHITransAddr!");
David Majnemer75ce6bb2015-06-01 00:15:08 +0000318 if (DT && DT->isReachableFromEntry(PredBB))
319 Addr =
320 PHITranslateSubExpr(Addr, CurBB, PredBB, MustDominate ? DT : nullptr);
321 else
322 Addr = nullptr;
Chris Lattner7dedbf42009-12-09 00:01:00 +0000323 assert(Verify() && "Invalid PHITransAddr!");
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000324
David Majnemer75ce6bb2015-06-01 00:15:08 +0000325 if (MustDominate)
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000326 // 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 Topper570e52c2014-04-15 04:59:12 +0000329 Addr = nullptr;
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000330
Craig Topper570e52c2014-04-15 04:59:12 +0000331 return Addr == nullptr;
Chris Lattner9a864122009-12-07 18:36:53 +0000332}
333
Chris Lattner9a864122009-12-07 18:36:53 +0000334/// PHITranslateWithInsertion - PHI translate this value into the specified
335/// predecessor block, inserting a computation of the value if it is
336/// unavailable.
337///
338/// All newly created instructions are added to the NewInsts list. This
339/// returns null on failure.
340///
341Value *PHITransAddr::
342PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
343 const DominatorTree &DT,
344 SmallVectorImpl<Instruction*> &NewInsts) {
345 unsigned NISize = NewInsts.size();
Dan Gohman7feccd22010-11-18 17:06:31 +0000346
Chris Lattner9a864122009-12-07 18:36:53 +0000347 // Attempt to PHI translate with insertion.
348 Addr = InsertPHITranslatedSubExpr(Addr, CurBB, PredBB, DT, NewInsts);
Dan Gohman7feccd22010-11-18 17:06:31 +0000349
Chris Lattner9a864122009-12-07 18:36:53 +0000350 // If successful, return the new value.
351 if (Addr) return Addr;
Dan Gohman7feccd22010-11-18 17:06:31 +0000352
Chris Lattner9a864122009-12-07 18:36:53 +0000353 // If not, destroy any intermediate instructions inserted.
354 while (NewInsts.size() != NISize)
355 NewInsts.pop_back_val()->eraseFromParent();
Craig Topper570e52c2014-04-15 04:59:12 +0000356 return nullptr;
Chris Lattner9a864122009-12-07 18:36:53 +0000357}
358
359
Chris Lattner210e45a2009-12-04 02:10:16 +0000360/// InsertPHITranslatedPointer - Insert a computation of the PHI translated
361/// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
362/// block. All newly created instructions are added to the NewInsts list.
363/// This returns null on failure.
364///
365Value *PHITransAddr::
Chris Lattner9a864122009-12-07 18:36:53 +0000366InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
367 BasicBlock *PredBB, const DominatorTree &DT,
368 SmallVectorImpl<Instruction*> &NewInsts) {
Chris Lattner210e45a2009-12-04 02:10:16 +0000369 // See if we have a version of this value already available and dominating
Chris Lattner9a864122009-12-07 18:36:53 +0000370 // PredBB. If so, there is no need to insert a new instance of it.
Daniel Jasper8de3a542016-12-19 08:22:17 +0000371 PHITransAddr Tmp(InVal, DL, AC);
David Majnemer75ce6bb2015-06-01 00:15:08 +0000372 if (!Tmp.PHITranslateValue(CurBB, PredBB, &DT, /*MustDominate=*/true))
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000373 return Tmp.getAddr();
Chris Lattner210e45a2009-12-04 02:10:16 +0000374
David Majnemer9585c542015-08-09 15:43:02 +0000375 // We don't need to PHI translate values which aren't instructions.
376 auto *Inst = dyn_cast<Instruction>(InVal);
377 if (!Inst)
378 return nullptr;
Dan Gohman7feccd22010-11-18 17:06:31 +0000379
Dan Gohmance562622010-11-18 17:05:13 +0000380 // Handle cast of PHI translatable value.
381 if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
Craig Topper570e52c2014-04-15 04:59:12 +0000382 if (!isSafeToSpeculativelyExecute(Cast)) return nullptr;
Dan Gohmance562622010-11-18 17:05:13 +0000383 Value *OpVal = InsertPHITranslatedSubExpr(Cast->getOperand(0),
Chris Lattner9a864122009-12-07 18:36:53 +0000384 CurBB, PredBB, DT, NewInsts);
Craig Topper570e52c2014-04-15 04:59:12 +0000385 if (!OpVal) return nullptr;
Dan Gohman7feccd22010-11-18 17:06:31 +0000386
Dan Gohmance562622010-11-18 17:05:13 +0000387 // Otherwise insert a cast at the end of PredBB.
Alexey Samsonov881c8e02015-06-10 17:37:38 +0000388 CastInst *New = CastInst::Create(Cast->getOpcode(), OpVal, InVal->getType(),
389 InVal->getName() + ".phi.trans.insert",
Dan Gohmance562622010-11-18 17:05:13 +0000390 PredBB->getTerminator());
Alexey Samsonov881c8e02015-06-10 17:37:38 +0000391 New->setDebugLoc(Inst->getDebugLoc());
Chris Lattner9a864122009-12-07 18:36:53 +0000392 NewInsts.push_back(New);
393 return New;
394 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000395
Chris Lattner9a864122009-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 Topper570e52c2014-04-15 04:59:12 +0000403 if (!OpVal) return nullptr;
Chris Lattner9a864122009-12-07 18:36:53 +0000404 GEPOps.push_back(OpVal);
405 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000406
David Blaikief508add2015-03-14 19:53:33 +0000407 GetElementPtrInst *Result = GetElementPtrInst::Create(
408 GEP->getSourceElementType(), GEPOps[0], makeArrayRef(GEPOps).slice(1),
409 InVal->getName() + ".phi.trans.insert", PredBB->getTerminator());
Alexey Samsonov881c8e02015-06-10 17:37:38 +0000410 Result->setDebugLoc(Inst->getDebugLoc());
Chris Lattner9a864122009-12-07 18:36:53 +0000411 Result->setIsInBounds(GEP->isInBounds());
412 NewInsts.push_back(Result);
413 return Result;
414 }
Dan Gohman7feccd22010-11-18 17:06:31 +0000415
Chris Lattner9a864122009-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 Gohman7feccd22010-11-18 17:06:31 +0000420
Chris Lattner9a864122009-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 Gohman7feccd22010-11-18 17:06:31 +0000428
Chris Lattner9a864122009-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 Gohman7feccd22010-11-18 17:06:31 +0000438
Craig Topper570e52c2014-04-15 04:59:12 +0000439 return nullptr;
Chris Lattner210e45a2009-12-04 02:10:16 +0000440}