Chris Lattner | 210e45a | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 1 | //===- PHITransAddr.cpp - PHI Translation for Addresses -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the PHITransAddr class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Analysis/PHITransAddr.h" |
| 15 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/InstructionSimplify.h" |
David Greene | 2a0f3cc | 2009-12-23 21:06:14 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Debug.h" |
Chris Lattner | 7dedbf4 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 210e45a | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 21 | static bool CanPHITrans(Instruction *Inst) { |
| 22 | if (isa<PHINode>(Inst) || |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 23 | isa<GetElementPtrInst>(Inst)) |
| 24 | return true; |
Dan Gohman | ce56262 | 2010-11-18 17:05:13 +0000 | [diff] [blame^] | 25 | |
| 26 | if (isa<CastInst>(Inst) && |
| 27 | Inst->isSafeToSpeculativelyExecute()) |
| 28 | return true; |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 34f8490 | 2009-12-08 06:06:26 +0000 | [diff] [blame] | 30 | if (Inst->getOpcode() == Instruction::Add && |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 31 | isa<ConstantInt>(Inst->getOperand(1))) |
| 32 | return true; |
| 33 | |
| 34 | // cerr << "MEMDEP: Could not PHI translate: " << *Pointer; |
| 35 | // if (isa<BitCastInst>(PtrInst) || isa<GetElementPtrInst>(PtrInst)) |
| 36 | // cerr << "OP:\t\t\t\t" << *PtrInst->getOperand(0); |
| 37 | return false; |
| 38 | } |
| 39 | |
Chris Lattner | 7dedbf4 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 40 | void PHITransAddr::dump() const { |
| 41 | if (Addr == 0) { |
David Greene | 2a0f3cc | 2009-12-23 21:06:14 +0000 | [diff] [blame] | 42 | dbgs() << "PHITransAddr: null\n"; |
Chris Lattner | 7dedbf4 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 43 | return; |
| 44 | } |
David Greene | 2a0f3cc | 2009-12-23 21:06:14 +0000 | [diff] [blame] | 45 | dbgs() << "PHITransAddr: " << *Addr << "\n"; |
Chris Lattner | 7dedbf4 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 46 | for (unsigned i = 0, e = InstInputs.size(); i != e; ++i) |
David Greene | 2a0f3cc | 2009-12-23 21:06:14 +0000 | [diff] [blame] | 47 | dbgs() << " Input #" << i << " is " << *InstInputs[i] << "\n"; |
Chris Lattner | 7dedbf4 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | |
| 51 | static bool VerifySubExpr(Value *Expr, |
| 52 | SmallVectorImpl<Instruction*> &InstInputs) { |
| 53 | // If this is a non-instruction value, there is nothing to do. |
| 54 | Instruction *I = dyn_cast<Instruction>(Expr); |
| 55 | if (I == 0) return true; |
| 56 | |
| 57 | // If it's an instruction, it is either in Tmp or its operands recursively |
| 58 | // are. |
| 59 | SmallVectorImpl<Instruction*>::iterator Entry = |
| 60 | std::find(InstInputs.begin(), InstInputs.end(), I); |
| 61 | if (Entry != InstInputs.end()) { |
| 62 | InstInputs.erase(Entry); |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | // If it isn't in the InstInputs list it is a subexpr incorporated into the |
| 67 | // address. Sanity check that it is phi translatable. |
| 68 | if (!CanPHITrans(I)) { |
David Greene | a8e21d4 | 2009-12-23 23:27:15 +0000 | [diff] [blame] | 69 | errs() << "Non phi translatable instruction found in PHITransAddr, either " |
Chris Lattner | 7dedbf4 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 70 | "something is missing from InstInputs or CanPHITrans is wrong:\n"; |
David Greene | a8e21d4 | 2009-12-23 23:27:15 +0000 | [diff] [blame] | 71 | errs() << *I << '\n'; |
Chris Lattner | 7dedbf4 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | |
| 75 | // Validate the operands of the instruction. |
| 76 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 77 | if (!VerifySubExpr(I->getOperand(i), InstInputs)) |
| 78 | return false; |
| 79 | |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | /// Verify - Check internal consistency of this data structure. If the |
| 84 | /// structure is valid, it returns true. If invalid, it prints errors and |
| 85 | /// returns false. |
| 86 | bool PHITransAddr::Verify() const { |
| 87 | if (Addr == 0) return true; |
| 88 | |
| 89 | SmallVector<Instruction*, 8> Tmp(InstInputs.begin(), InstInputs.end()); |
| 90 | |
| 91 | if (!VerifySubExpr(Addr, Tmp)) |
| 92 | return false; |
| 93 | |
| 94 | if (!Tmp.empty()) { |
David Greene | a8e21d4 | 2009-12-23 23:27:15 +0000 | [diff] [blame] | 95 | errs() << "PHITransAddr inconsistent, contains extra instructions:\n"; |
Chris Lattner | 7dedbf4 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 96 | for (unsigned i = 0, e = InstInputs.size(); i != e; ++i) |
David Greene | a8e21d4 | 2009-12-23 23:27:15 +0000 | [diff] [blame] | 97 | errs() << " InstInput #" << i << " is " << *InstInputs[i] << "\n"; |
Chris Lattner | 7dedbf4 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 98 | return false; |
| 99 | } |
| 100 | |
| 101 | // a-ok. |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 106 | /// IsPotentiallyPHITranslatable - If this needs PHI translation, return true |
| 107 | /// if we have some hope of doing it. This should be used as a filter to |
| 108 | /// avoid calling PHITranslateValue in hopeless situations. |
| 109 | bool PHITransAddr::IsPotentiallyPHITranslatable() const { |
| 110 | // If the input value is not an instruction, or if it is not defined in CurBB, |
| 111 | // then we don't need to phi translate it. |
| 112 | Instruction *Inst = dyn_cast<Instruction>(Addr); |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 113 | return Inst == 0 || CanPHITrans(Inst); |
Chris Lattner | 210e45a | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 6200e53 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 117 | static void RemoveInstInputs(Value *V, |
Chris Lattner | 43678f4 | 2009-12-08 23:42:51 +0000 | [diff] [blame] | 118 | SmallVectorImpl<Instruction*> &InstInputs) { |
Chris Lattner | 6200e53 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 119 | Instruction *I = dyn_cast<Instruction>(V); |
| 120 | if (I == 0) return; |
| 121 | |
Chris Lattner | 43678f4 | 2009-12-08 23:42:51 +0000 | [diff] [blame] | 122 | // If the instruction is in the InstInputs list, remove it. |
| 123 | SmallVectorImpl<Instruction*>::iterator Entry = |
| 124 | std::find(InstInputs.begin(), InstInputs.end(), I); |
| 125 | if (Entry != InstInputs.end()) { |
| 126 | InstInputs.erase(Entry); |
| 127 | return; |
| 128 | } |
| 129 | |
Chris Lattner | 6200e53 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 130 | assert(!isa<PHINode>(I) && "Error, removing something that isn't an input"); |
Chris Lattner | 43678f4 | 2009-12-08 23:42:51 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 6200e53 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 132 | // Otherwise, it must have instruction inputs itself. Zap them recursively. |
| 133 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 134 | if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i))) |
| 135 | RemoveInstInputs(Op, InstInputs); |
| 136 | } |
Chris Lattner | 43678f4 | 2009-12-08 23:42:51 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 139 | Value *PHITransAddr::PHITranslateSubExpr(Value *V, BasicBlock *CurBB, |
Daniel Dunbar | 6d8f2ca | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 140 | BasicBlock *PredBB, |
| 141 | const DominatorTree *DT) { |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 142 | // If this is a non-instruction value, it can't require PHI translation. |
| 143 | Instruction *Inst = dyn_cast<Instruction>(V); |
| 144 | if (Inst == 0) return V; |
| 145 | |
Chris Lattner | af50315 | 2009-12-09 00:10:55 +0000 | [diff] [blame] | 146 | // Determine whether 'Inst' is an input to our PHI translatable expression. |
| 147 | bool isInput = std::count(InstInputs.begin(), InstInputs.end(), Inst); |
| 148 | |
| 149 | // Handle inputs instructions if needed. |
| 150 | if (isInput) { |
| 151 | if (Inst->getParent() != CurBB) { |
| 152 | // If it is an input defined in a different block, then it remains an |
| 153 | // input. |
| 154 | return Inst; |
| 155 | } |
Chris Lattner | e09e98c | 2009-12-09 00:18:13 +0000 | [diff] [blame] | 156 | |
| 157 | // If 'Inst' is defined in this block and is an input that needs to be phi |
| 158 | // translated, we need to incorporate the value into the expression or fail. |
| 159 | |
Chris Lattner | 6200e53 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 160 | // In either case, the instruction itself isn't an input any longer. |
| 161 | InstInputs.erase(std::find(InstInputs.begin(), InstInputs.end(), Inst)); |
| 162 | |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 163 | // If this is a PHI, go ahead and translate it. |
| 164 | if (PHINode *PN = dyn_cast<PHINode>(Inst)) |
Chris Lattner | 6200e53 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 165 | return AddAsInput(PN->getIncomingValueForBlock(PredBB)); |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 166 | |
| 167 | // If this is a non-phi value, and it is analyzable, we can incorporate it |
| 168 | // into the expression by making all instruction operands be inputs. |
| 169 | if (!CanPHITrans(Inst)) |
| 170 | return 0; |
Chris Lattner | e09e98c | 2009-12-09 00:18:13 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 172 | // All instruction operands are now inputs (and of course, they may also be |
| 173 | // defined in this block, so they may need to be phi translated themselves. |
| 174 | for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) |
| 175 | if (Instruction *Op = dyn_cast<Instruction>(Inst->getOperand(i))) |
| 176 | InstInputs.push_back(Op); |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 179 | // Ok, it must be an intermediate result (either because it started that way |
| 180 | // or because we just incorporated it into the expression). See if its |
| 181 | // operands need to be phi translated, and if so, reconstruct it. |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 182 | |
Dan Gohman | ce56262 | 2010-11-18 17:05:13 +0000 | [diff] [blame^] | 183 | if (CastInst *Cast = dyn_cast<CastInst>(Inst)) { |
| 184 | if (!Cast->isSafeToSpeculativelyExecute()) return 0; |
| 185 | Value *PHIIn = PHITranslateSubExpr(Cast->getOperand(0), CurBB, PredBB, DT); |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 186 | if (PHIIn == 0) return 0; |
Dan Gohman | ce56262 | 2010-11-18 17:05:13 +0000 | [diff] [blame^] | 187 | if (PHIIn == Cast->getOperand(0)) |
| 188 | return Cast; |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 189 | |
| 190 | // Find an available version of this cast. |
| 191 | |
| 192 | // Constants are trivial to find. |
| 193 | if (Constant *C = dyn_cast<Constant>(PHIIn)) |
Dan Gohman | ce56262 | 2010-11-18 17:05:13 +0000 | [diff] [blame^] | 194 | return AddAsInput(ConstantExpr::getCast(Cast->getOpcode(), |
| 195 | C, Cast->getType())); |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 196 | |
Dan Gohman | ce56262 | 2010-11-18 17:05:13 +0000 | [diff] [blame^] | 197 | // Otherwise we have to see if a casted version of the incoming pointer |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 198 | // is available. If so, we can use it, otherwise we have to fail. |
| 199 | for (Value::use_iterator UI = PHIIn->use_begin(), E = PHIIn->use_end(); |
| 200 | UI != E; ++UI) { |
Dan Gohman | ce56262 | 2010-11-18 17:05:13 +0000 | [diff] [blame^] | 201 | if (CastInst *CastI = dyn_cast<CastInst>(*UI)) |
| 202 | if (CastI->getOpcode() == Cast->getOpcode() && |
| 203 | CastI->getType() == Cast->getType() && |
| 204 | (!DT || DT->dominates(CastI->getParent(), PredBB))) |
| 205 | return CastI; |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 206 | } |
| 207 | return 0; |
| 208 | } |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 210 | // Handle getelementptr with at least one PHI translatable operand. |
| 211 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) { |
| 212 | SmallVector<Value*, 8> GEPOps; |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 213 | bool AnyChanged = false; |
| 214 | for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) { |
Daniel Dunbar | 6d8f2ca | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 215 | Value *GEPOp = PHITranslateSubExpr(GEP->getOperand(i), CurBB, PredBB, DT); |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 216 | if (GEPOp == 0) return 0; |
| 217 | |
Chris Lattner | 6045417 | 2009-12-07 19:52:57 +0000 | [diff] [blame] | 218 | AnyChanged |= GEPOp != GEP->getOperand(i); |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 219 | GEPOps.push_back(GEPOp); |
| 220 | } |
| 221 | |
| 222 | if (!AnyChanged) |
| 223 | return GEP; |
| 224 | |
| 225 | // Simplify the GEP to handle 'gep x, 0' -> x etc. |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 226 | if (Value *V = SimplifyGEPInst(&GEPOps[0], GEPOps.size(), TD, DT)) { |
Chris Lattner | 6200e53 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 227 | for (unsigned i = 0, e = GEPOps.size(); i != e; ++i) |
| 228 | RemoveInstInputs(GEPOps[i], InstInputs); |
| 229 | |
| 230 | return AddAsInput(V); |
| 231 | } |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 232 | |
| 233 | // Scan to see if we have this GEP available. |
| 234 | Value *APHIOp = GEPOps[0]; |
| 235 | for (Value::use_iterator UI = APHIOp->use_begin(), E = APHIOp->use_end(); |
| 236 | UI != E; ++UI) { |
| 237 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI)) |
| 238 | if (GEPI->getType() == GEP->getType() && |
| 239 | GEPI->getNumOperands() == GEPOps.size() && |
Daniel Dunbar | 6d8f2ca | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 240 | GEPI->getParent()->getParent() == CurBB->getParent() && |
| 241 | (!DT || DT->dominates(GEPI->getParent(), PredBB))) { |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 242 | bool Mismatch = false; |
| 243 | for (unsigned i = 0, e = GEPOps.size(); i != e; ++i) |
| 244 | if (GEPI->getOperand(i) != GEPOps[i]) { |
| 245 | Mismatch = true; |
| 246 | break; |
| 247 | } |
| 248 | if (!Mismatch) |
| 249 | return GEPI; |
| 250 | } |
| 251 | } |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | // 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(); |
| 262 | |
Daniel Dunbar | 6d8f2ca | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 263 | Value *LHS = PHITranslateSubExpr(Inst->getOperand(0), CurBB, PredBB, DT); |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 264 | if (LHS == 0) return 0; |
| 265 | |
| 266 | // 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; |
Chris Lattner | 6200e53 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 273 | |
| 274 | // If the old 'LHS' was an input, add the new 'LHS' as an input. |
| 275 | if (std::count(InstInputs.begin(), InstInputs.end(), BOp)) { |
| 276 | RemoveInstInputs(BOp, InstInputs); |
| 277 | AddAsInput(LHS); |
| 278 | } |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | // See if the add simplifies away. |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 282 | if (Value *Res = SimplifyAddInst(LHS, RHS, isNSW, isNUW, TD, DT)) { |
Chris Lattner | 6200e53 | 2009-12-09 00:56:14 +0000 | [diff] [blame] | 283 | // If we simplified the operands, the LHS is no longer an input, but Res |
| 284 | // is. |
| 285 | RemoveInstInputs(LHS, InstInputs); |
| 286 | return AddAsInput(Res); |
| 287 | } |
Chris Lattner | 4d3a16f | 2009-12-09 17:27:45 +0000 | [diff] [blame] | 288 | |
| 289 | // If we didn't modify the add, just return it. |
| 290 | if (LHS == Inst->getOperand(0) && RHS == Inst->getOperand(1)) |
| 291 | return Inst; |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 292 | |
| 293 | // Otherwise, see if we have this add available somewhere. |
| 294 | for (Value::use_iterator UI = LHS->use_begin(), E = LHS->use_end(); |
| 295 | UI != E; ++UI) { |
| 296 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*UI)) |
Chris Lattner | eddc65a | 2009-12-09 17:18:49 +0000 | [diff] [blame] | 297 | if (BO->getOpcode() == Instruction::Add && |
| 298 | BO->getOperand(0) == LHS && BO->getOperand(1) == RHS && |
Daniel Dunbar | 6d8f2ca | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 299 | BO->getParent()->getParent() == CurBB->getParent() && |
| 300 | (!DT || DT->dominates(BO->getParent(), PredBB))) |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 301 | return BO; |
| 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | // Otherwise, we failed. |
| 308 | return 0; |
Chris Lattner | 210e45a | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 311 | |
| 312 | /// PHITranslateValue - PHI translate the current address up the CFG from |
Daniel Dunbar | 6d8f2ca | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 313 | /// CurBB to Pred, updating our state to reflect any needed changes. If the |
| 314 | /// dominator tree DT is non-null, the translated value must dominate |
| 315 | /// PredBB. This returns true on failure and sets Addr to null. |
| 316 | bool PHITransAddr::PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB, |
| 317 | const DominatorTree *DT) { |
Chris Lattner | 7dedbf4 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 318 | assert(Verify() && "Invalid PHITransAddr!"); |
Daniel Dunbar | 6d8f2ca | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 319 | Addr = PHITranslateSubExpr(Addr, CurBB, PredBB, DT); |
Chris Lattner | 7dedbf4 | 2009-12-09 00:01:00 +0000 | [diff] [blame] | 320 | assert(Verify() && "Invalid PHITransAddr!"); |
Daniel Dunbar | 6d8f2ca | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 321 | |
| 322 | if (DT) { |
| 323 | // Make sure the value is live in the predecessor. |
| 324 | if (Instruction *Inst = dyn_cast_or_null<Instruction>(Addr)) |
| 325 | if (!DT->dominates(Inst->getParent(), PredBB)) |
| 326 | Addr = 0; |
| 327 | } |
| 328 | |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 329 | return Addr == 0; |
| 330 | } |
| 331 | |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 332 | /// PHITranslateWithInsertion - PHI translate this value into the specified |
| 333 | /// predecessor block, inserting a computation of the value if it is |
| 334 | /// unavailable. |
| 335 | /// |
| 336 | /// All newly created instructions are added to the NewInsts list. This |
| 337 | /// returns null on failure. |
| 338 | /// |
| 339 | Value *PHITransAddr:: |
| 340 | PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB, |
| 341 | const DominatorTree &DT, |
| 342 | SmallVectorImpl<Instruction*> &NewInsts) { |
| 343 | unsigned NISize = NewInsts.size(); |
| 344 | |
| 345 | // Attempt to PHI translate with insertion. |
| 346 | Addr = InsertPHITranslatedSubExpr(Addr, CurBB, PredBB, DT, NewInsts); |
| 347 | |
| 348 | // If successful, return the new value. |
| 349 | if (Addr) return Addr; |
| 350 | |
| 351 | // If not, destroy any intermediate instructions inserted. |
| 352 | while (NewInsts.size() != NISize) |
| 353 | NewInsts.pop_back_val()->eraseFromParent(); |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | |
Chris Lattner | 210e45a | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 358 | /// InsertPHITranslatedPointer - Insert a computation of the PHI translated |
| 359 | /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB |
| 360 | /// block. All newly created instructions are added to the NewInsts list. |
| 361 | /// This returns null on failure. |
| 362 | /// |
| 363 | Value *PHITransAddr:: |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 364 | InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB, |
| 365 | BasicBlock *PredBB, const DominatorTree &DT, |
| 366 | SmallVectorImpl<Instruction*> &NewInsts) { |
Chris Lattner | 210e45a | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 367 | // See if we have a version of this value already available and dominating |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 368 | // PredBB. If so, there is no need to insert a new instance of it. |
Daniel Dunbar | 6d8f2ca | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 369 | PHITransAddr Tmp(InVal, TD); |
| 370 | if (!Tmp.PHITranslateValue(CurBB, PredBB, &DT)) |
| 371 | return Tmp.getAddr(); |
Chris Lattner | 210e45a | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 372 | |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 373 | // If we don't have an available version of this value, it must be an |
| 374 | // instruction. |
| 375 | Instruction *Inst = cast<Instruction>(InVal); |
| 376 | |
Dan Gohman | ce56262 | 2010-11-18 17:05:13 +0000 | [diff] [blame^] | 377 | // Handle cast of PHI translatable value. |
| 378 | if (CastInst *Cast = dyn_cast<CastInst>(Inst)) { |
| 379 | if (!Cast->isSafeToSpeculativelyExecute()) return 0; |
| 380 | Value *OpVal = InsertPHITranslatedSubExpr(Cast->getOperand(0), |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 381 | CurBB, PredBB, DT, NewInsts); |
| 382 | if (OpVal == 0) return 0; |
| 383 | |
Dan Gohman | ce56262 | 2010-11-18 17:05:13 +0000 | [diff] [blame^] | 384 | // Otherwise insert a cast at the end of PredBB. |
| 385 | CastInst *New = CastInst::Create(Cast->getOpcode(), |
| 386 | OpVal, InVal->getType(), |
| 387 | InVal->getName()+".phi.trans.insert", |
| 388 | PredBB->getTerminator()); |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 389 | NewInsts.push_back(New); |
| 390 | return New; |
| 391 | } |
| 392 | |
| 393 | // Handle getelementptr with at least one PHI operand. |
| 394 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) { |
| 395 | SmallVector<Value*, 8> GEPOps; |
| 396 | BasicBlock *CurBB = GEP->getParent(); |
| 397 | for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) { |
| 398 | Value *OpVal = InsertPHITranslatedSubExpr(GEP->getOperand(i), |
| 399 | CurBB, PredBB, DT, NewInsts); |
| 400 | if (OpVal == 0) return 0; |
| 401 | GEPOps.push_back(OpVal); |
| 402 | } |
| 403 | |
| 404 | GetElementPtrInst *Result = |
| 405 | GetElementPtrInst::Create(GEPOps[0], GEPOps.begin()+1, GEPOps.end(), |
| 406 | InVal->getName()+".phi.trans.insert", |
| 407 | PredBB->getTerminator()); |
| 408 | Result->setIsInBounds(GEP->isInBounds()); |
| 409 | NewInsts.push_back(Result); |
| 410 | return Result; |
| 411 | } |
| 412 | |
| 413 | #if 0 |
| 414 | // FIXME: This code works, but it is unclear that we actually want to insert |
| 415 | // a big chain of computation in order to make a value available in a block. |
| 416 | // This needs to be evaluated carefully to consider its cost trade offs. |
| 417 | |
| 418 | // Handle add with a constant RHS. |
| 419 | if (Inst->getOpcode() == Instruction::Add && |
| 420 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 421 | // PHI translate the LHS. |
| 422 | Value *OpVal = InsertPHITranslatedSubExpr(Inst->getOperand(0), |
| 423 | CurBB, PredBB, DT, NewInsts); |
| 424 | if (OpVal == 0) return 0; |
| 425 | |
| 426 | BinaryOperator *Res = BinaryOperator::CreateAdd(OpVal, Inst->getOperand(1), |
| 427 | InVal->getName()+".phi.trans.insert", |
| 428 | PredBB->getTerminator()); |
| 429 | Res->setHasNoSignedWrap(cast<BinaryOperator>(Inst)->hasNoSignedWrap()); |
| 430 | Res->setHasNoUnsignedWrap(cast<BinaryOperator>(Inst)->hasNoUnsignedWrap()); |
| 431 | NewInsts.push_back(Res); |
| 432 | return Res; |
| 433 | } |
| 434 | #endif |
| 435 | |
Chris Lattner | 210e45a | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 436 | return 0; |
| 437 | } |