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" |
Chris Lattner | 210e45a | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 19 | static bool CanPHITrans(Instruction *Inst) { |
| 20 | if (isa<PHINode>(Inst) || |
| 21 | isa<BitCastInst>(Inst) || |
| 22 | isa<GetElementPtrInst>(Inst)) |
| 23 | return true; |
| 24 | |
Chris Lattner | 34f8490 | 2009-12-08 06:06:26 +0000 | [diff] [blame] | 25 | if (Inst->getOpcode() == Instruction::Add && |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 26 | isa<ConstantInt>(Inst->getOperand(1))) |
| 27 | return true; |
| 28 | |
| 29 | // cerr << "MEMDEP: Could not PHI translate: " << *Pointer; |
| 30 | // if (isa<BitCastInst>(PtrInst) || isa<GetElementPtrInst>(PtrInst)) |
| 31 | // cerr << "OP:\t\t\t\t" << *PtrInst->getOperand(0); |
| 32 | return false; |
| 33 | } |
| 34 | |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 35 | /// IsPotentiallyPHITranslatable - If this needs PHI translation, return true |
| 36 | /// if we have some hope of doing it. This should be used as a filter to |
| 37 | /// avoid calling PHITranslateValue in hopeless situations. |
| 38 | bool PHITransAddr::IsPotentiallyPHITranslatable() const { |
| 39 | // If the input value is not an instruction, or if it is not defined in CurBB, |
| 40 | // then we don't need to phi translate it. |
| 41 | Instruction *Inst = dyn_cast<Instruction>(Addr); |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 42 | return Inst == 0 || CanPHITrans(Inst); |
Chris Lattner | 210e45a | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 43678f4 | 2009-12-08 23:42:51 +0000 | [diff] [blame^] | 46 | static void RemoveInstInputs(Instruction *I, |
| 47 | SmallVectorImpl<Instruction*> &InstInputs) { |
| 48 | // If the instruction is in the InstInputs list, remove it. |
| 49 | SmallVectorImpl<Instruction*>::iterator Entry = |
| 50 | std::find(InstInputs.begin(), InstInputs.end(), I); |
| 51 | if (Entry != InstInputs.end()) { |
| 52 | InstInputs.erase(Entry); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | // Otherwise, it must have instruction inputs itself. Zap them recursively. |
| 57 | bool HadInstInputs = false; |
| 58 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 59 | if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i))) { |
| 60 | RemoveInstInputs(Op, InstInputs); |
| 61 | HadInstInputs = true; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // This instruction had to have operands in the instinputs list or it should |
| 66 | // have been in the list itself. If not, the list is broken. |
| 67 | assert(HadInstInputs && "InstInputs list inconsistent!"); |
| 68 | } |
| 69 | |
| 70 | /// ReplaceInstWithValue - Remove any instruction inputs in the InstInputs |
| 71 | /// array that are due to the specified instruction that is about to be |
| 72 | /// removed from the address, and add any corresponding to V. This returns V. |
| 73 | Value *PHITransAddr::ReplaceInstWithValue(Instruction *I, Value *V) { |
| 74 | // Remove the old instruction from InstInputs. |
| 75 | RemoveInstInputs(I, InstInputs); |
| 76 | |
| 77 | // If V is an instruction, it is now an input. |
| 78 | if (Instruction *VI = dyn_cast<Instruction>(V)) |
| 79 | InstInputs.push_back(VI); |
| 80 | return V; |
| 81 | } |
| 82 | |
| 83 | |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 84 | Value *PHITransAddr::PHITranslateSubExpr(Value *V, BasicBlock *CurBB, |
| 85 | BasicBlock *PredBB) { |
| 86 | // If this is a non-instruction value, it can't require PHI translation. |
| 87 | Instruction *Inst = dyn_cast<Instruction>(V); |
| 88 | if (Inst == 0) return V; |
| 89 | |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 90 | // If 'Inst' is defined in this block, it must be an input that needs to be |
| 91 | // phi translated or an intermediate expression that needs to be incorporated |
| 92 | // into the expression. |
| 93 | if (Inst->getParent() == CurBB) { |
| 94 | assert(std::count(InstInputs.begin(), InstInputs.end(), Inst) && |
| 95 | "Not an input?"); |
| 96 | |
| 97 | // If this is a PHI, go ahead and translate it. |
| 98 | if (PHINode *PN = dyn_cast<PHINode>(Inst)) |
| 99 | return PN->getIncomingValueForBlock(PredBB); |
| 100 | |
| 101 | |
| 102 | // If this is a non-phi value, and it is analyzable, we can incorporate it |
| 103 | // into the expression by making all instruction operands be inputs. |
| 104 | if (!CanPHITrans(Inst)) |
| 105 | return 0; |
| 106 | |
| 107 | // Okay, we can incorporate it, this instruction is no longer an input. |
| 108 | InstInputs.erase(std::find(InstInputs.begin(), InstInputs.end(), Inst)); |
| 109 | |
| 110 | // All instruction operands are now inputs (and of course, they may also be |
| 111 | // defined in this block, so they may need to be phi translated themselves. |
| 112 | for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) |
| 113 | if (Instruction *Op = dyn_cast<Instruction>(Inst->getOperand(i))) |
| 114 | InstInputs.push_back(Op); |
| 115 | |
| 116 | } else { |
| 117 | // Determine whether 'Inst' is an input to our PHI translatable expression. |
| 118 | bool isInput = std::count(InstInputs.begin(), InstInputs.end(), Inst); |
| 119 | |
| 120 | // If it is an input defined in a different block, then it remains an input. |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 121 | if (isInput) |
| 122 | return Inst; |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 125 | // Ok, it must be an intermediate result (either because it started that way |
| 126 | // or because we just incorporated it into the expression). See if its |
| 127 | // operands need to be phi translated, and if so, reconstruct it. |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 128 | |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 129 | if (BitCastInst *BC = dyn_cast<BitCastInst>(Inst)) { |
| 130 | Value *PHIIn = PHITranslateSubExpr(BC->getOperand(0), CurBB, PredBB); |
| 131 | if (PHIIn == 0) return 0; |
| 132 | if (PHIIn == BC->getOperand(0)) |
| 133 | return BC; |
| 134 | |
| 135 | // Find an available version of this cast. |
| 136 | |
| 137 | // Constants are trivial to find. |
| 138 | if (Constant *C = dyn_cast<Constant>(PHIIn)) |
Chris Lattner | 43678f4 | 2009-12-08 23:42:51 +0000 | [diff] [blame^] | 139 | return ReplaceInstWithValue(BC, ConstantExpr::getBitCast(C, |
| 140 | BC->getType())); |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 141 | |
| 142 | // Otherwise we have to see if a bitcasted version of the incoming pointer |
| 143 | // is available. If so, we can use it, otherwise we have to fail. |
| 144 | for (Value::use_iterator UI = PHIIn->use_begin(), E = PHIIn->use_end(); |
| 145 | UI != E; ++UI) { |
| 146 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(*UI)) |
| 147 | if (BCI->getType() == BC->getType()) |
| 148 | return BCI; |
| 149 | } |
| 150 | return 0; |
| 151 | } |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 152 | |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 153 | // Handle getelementptr with at least one PHI translatable operand. |
| 154 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) { |
| 155 | SmallVector<Value*, 8> GEPOps; |
| 156 | BasicBlock *CurBB = GEP->getParent(); |
| 157 | bool AnyChanged = false; |
| 158 | for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) { |
| 159 | Value *GEPOp = PHITranslateSubExpr(GEP->getOperand(i), CurBB, PredBB); |
| 160 | if (GEPOp == 0) return 0; |
| 161 | |
Chris Lattner | 6045417 | 2009-12-07 19:52:57 +0000 | [diff] [blame] | 162 | AnyChanged |= GEPOp != GEP->getOperand(i); |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 163 | GEPOps.push_back(GEPOp); |
| 164 | } |
| 165 | |
| 166 | if (!AnyChanged) |
| 167 | return GEP; |
| 168 | |
| 169 | // Simplify the GEP to handle 'gep x, 0' -> x etc. |
| 170 | if (Value *V = SimplifyGEPInst(&GEPOps[0], GEPOps.size(), TD)) |
Chris Lattner | 43678f4 | 2009-12-08 23:42:51 +0000 | [diff] [blame^] | 171 | return ReplaceInstWithValue(GEP, V); |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 172 | |
| 173 | // Scan to see if we have this GEP available. |
| 174 | Value *APHIOp = GEPOps[0]; |
| 175 | for (Value::use_iterator UI = APHIOp->use_begin(), E = APHIOp->use_end(); |
| 176 | UI != E; ++UI) { |
| 177 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI)) |
| 178 | if (GEPI->getType() == GEP->getType() && |
| 179 | GEPI->getNumOperands() == GEPOps.size() && |
| 180 | GEPI->getParent()->getParent() == CurBB->getParent()) { |
| 181 | bool Mismatch = false; |
| 182 | for (unsigned i = 0, e = GEPOps.size(); i != e; ++i) |
| 183 | if (GEPI->getOperand(i) != GEPOps[i]) { |
| 184 | Mismatch = true; |
| 185 | break; |
| 186 | } |
| 187 | if (!Mismatch) |
| 188 | return GEPI; |
| 189 | } |
| 190 | } |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | // Handle add with a constant RHS. |
| 195 | if (Inst->getOpcode() == Instruction::Add && |
| 196 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 197 | // PHI translate the LHS. |
| 198 | Constant *RHS = cast<ConstantInt>(Inst->getOperand(1)); |
| 199 | bool isNSW = cast<BinaryOperator>(Inst)->hasNoSignedWrap(); |
| 200 | bool isNUW = cast<BinaryOperator>(Inst)->hasNoUnsignedWrap(); |
| 201 | |
| 202 | Value *LHS = PHITranslateSubExpr(Inst->getOperand(0), CurBB, PredBB); |
| 203 | if (LHS == 0) return 0; |
| 204 | |
| 205 | // If the PHI translated LHS is an add of a constant, fold the immediates. |
| 206 | if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(LHS)) |
| 207 | if (BOp->getOpcode() == Instruction::Add) |
| 208 | if (ConstantInt *CI = dyn_cast<ConstantInt>(BOp->getOperand(1))) { |
| 209 | LHS = BOp->getOperand(0); |
| 210 | RHS = ConstantExpr::getAdd(RHS, CI); |
| 211 | isNSW = isNUW = false; |
| 212 | } |
| 213 | |
| 214 | // See if the add simplifies away. |
| 215 | if (Value *Res = SimplifyAddInst(LHS, RHS, isNSW, isNUW, TD)) |
Chris Lattner | 43678f4 | 2009-12-08 23:42:51 +0000 | [diff] [blame^] | 216 | return ReplaceInstWithValue(Inst, Res); |
Chris Lattner | 6fcca1c | 2009-12-07 19:04:49 +0000 | [diff] [blame] | 217 | |
| 218 | // Otherwise, see if we have this add available somewhere. |
| 219 | for (Value::use_iterator UI = LHS->use_begin(), E = LHS->use_end(); |
| 220 | UI != E; ++UI) { |
| 221 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*UI)) |
| 222 | if (BO->getOperand(0) == LHS && BO->getOperand(1) == RHS && |
| 223 | BO->getParent()->getParent() == CurBB->getParent()) |
| 224 | return BO; |
| 225 | } |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | // Otherwise, we failed. |
| 231 | return 0; |
Chris Lattner | 210e45a | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 234 | |
| 235 | /// PHITranslateValue - PHI translate the current address up the CFG from |
| 236 | /// CurBB to Pred, updating our state the reflect any needed changes. This |
Chris Lattner | e05a188 | 2009-12-07 19:45:30 +0000 | [diff] [blame] | 237 | /// returns true on failure and sets Addr to null. |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 238 | bool PHITransAddr::PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB) { |
| 239 | Addr = PHITranslateSubExpr(Addr, CurBB, PredBB); |
| 240 | return Addr == 0; |
| 241 | } |
| 242 | |
| 243 | /// GetAvailablePHITranslatedSubExpr - Return the value computed by |
| 244 | /// PHITranslateSubExpr if it dominates PredBB, otherwise return null. |
Chris Lattner | 210e45a | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 245 | Value *PHITransAddr:: |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 246 | GetAvailablePHITranslatedSubExpr(Value *V, BasicBlock *CurBB,BasicBlock *PredBB, |
Chris Lattner | 34f8490 | 2009-12-08 06:06:26 +0000 | [diff] [blame] | 247 | const DominatorTree &DT) const { |
| 248 | PHITransAddr Tmp(V, TD); |
| 249 | Tmp.PHITranslateValue(CurBB, PredBB); |
| 250 | |
Chris Lattner | 210e45a | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 251 | // See if PHI translation succeeds. |
Chris Lattner | 34f8490 | 2009-12-08 06:06:26 +0000 | [diff] [blame] | 252 | V = Tmp.getAddr(); |
Chris Lattner | 210e45a | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 253 | |
| 254 | // Make sure the value is live in the predecessor. |
| 255 | if (Instruction *Inst = dyn_cast_or_null<Instruction>(V)) |
| 256 | if (!DT.dominates(Inst->getParent(), PredBB)) |
| 257 | return 0; |
| 258 | return V; |
| 259 | } |
| 260 | |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 261 | |
| 262 | /// PHITranslateWithInsertion - PHI translate this value into the specified |
| 263 | /// predecessor block, inserting a computation of the value if it is |
| 264 | /// unavailable. |
| 265 | /// |
| 266 | /// All newly created instructions are added to the NewInsts list. This |
| 267 | /// returns null on failure. |
| 268 | /// |
| 269 | Value *PHITransAddr:: |
| 270 | PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB, |
| 271 | const DominatorTree &DT, |
| 272 | SmallVectorImpl<Instruction*> &NewInsts) { |
| 273 | unsigned NISize = NewInsts.size(); |
| 274 | |
| 275 | // Attempt to PHI translate with insertion. |
| 276 | Addr = InsertPHITranslatedSubExpr(Addr, CurBB, PredBB, DT, NewInsts); |
| 277 | |
| 278 | // If successful, return the new value. |
| 279 | if (Addr) return Addr; |
| 280 | |
| 281 | // If not, destroy any intermediate instructions inserted. |
| 282 | while (NewInsts.size() != NISize) |
| 283 | NewInsts.pop_back_val()->eraseFromParent(); |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | |
Chris Lattner | 210e45a | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 288 | /// InsertPHITranslatedPointer - Insert a computation of the PHI translated |
| 289 | /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB |
| 290 | /// block. All newly created instructions are added to the NewInsts list. |
| 291 | /// This returns null on failure. |
| 292 | /// |
| 293 | Value *PHITransAddr:: |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 294 | InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB, |
| 295 | BasicBlock *PredBB, const DominatorTree &DT, |
| 296 | SmallVectorImpl<Instruction*> &NewInsts) { |
Chris Lattner | 210e45a | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 297 | // 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] | 298 | // PredBB. If so, there is no need to insert a new instance of it. |
| 299 | if (Value *Res = GetAvailablePHITranslatedSubExpr(InVal, CurBB, PredBB, DT)) |
Chris Lattner | 210e45a | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 300 | return Res; |
| 301 | |
Chris Lattner | 9a86412 | 2009-12-07 18:36:53 +0000 | [diff] [blame] | 302 | // If we don't have an available version of this value, it must be an |
| 303 | // instruction. |
| 304 | Instruction *Inst = cast<Instruction>(InVal); |
| 305 | |
| 306 | // Handle bitcast of PHI translatable value. |
| 307 | if (BitCastInst *BC = dyn_cast<BitCastInst>(Inst)) { |
| 308 | Value *OpVal = InsertPHITranslatedSubExpr(BC->getOperand(0), |
| 309 | CurBB, PredBB, DT, NewInsts); |
| 310 | if (OpVal == 0) return 0; |
| 311 | |
| 312 | // Otherwise insert a bitcast at the end of PredBB. |
| 313 | BitCastInst *New = new BitCastInst(OpVal, InVal->getType(), |
| 314 | InVal->getName()+".phi.trans.insert", |
| 315 | PredBB->getTerminator()); |
| 316 | NewInsts.push_back(New); |
| 317 | return New; |
| 318 | } |
| 319 | |
| 320 | // Handle getelementptr with at least one PHI operand. |
| 321 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) { |
| 322 | SmallVector<Value*, 8> GEPOps; |
| 323 | BasicBlock *CurBB = GEP->getParent(); |
| 324 | for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) { |
| 325 | Value *OpVal = InsertPHITranslatedSubExpr(GEP->getOperand(i), |
| 326 | CurBB, PredBB, DT, NewInsts); |
| 327 | if (OpVal == 0) return 0; |
| 328 | GEPOps.push_back(OpVal); |
| 329 | } |
| 330 | |
| 331 | GetElementPtrInst *Result = |
| 332 | GetElementPtrInst::Create(GEPOps[0], GEPOps.begin()+1, GEPOps.end(), |
| 333 | InVal->getName()+".phi.trans.insert", |
| 334 | PredBB->getTerminator()); |
| 335 | Result->setIsInBounds(GEP->isInBounds()); |
| 336 | NewInsts.push_back(Result); |
| 337 | return Result; |
| 338 | } |
| 339 | |
| 340 | #if 0 |
| 341 | // FIXME: This code works, but it is unclear that we actually want to insert |
| 342 | // a big chain of computation in order to make a value available in a block. |
| 343 | // This needs to be evaluated carefully to consider its cost trade offs. |
| 344 | |
| 345 | // Handle add with a constant RHS. |
| 346 | if (Inst->getOpcode() == Instruction::Add && |
| 347 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 348 | // PHI translate the LHS. |
| 349 | Value *OpVal = InsertPHITranslatedSubExpr(Inst->getOperand(0), |
| 350 | CurBB, PredBB, DT, NewInsts); |
| 351 | if (OpVal == 0) return 0; |
| 352 | |
| 353 | BinaryOperator *Res = BinaryOperator::CreateAdd(OpVal, Inst->getOperand(1), |
| 354 | InVal->getName()+".phi.trans.insert", |
| 355 | PredBB->getTerminator()); |
| 356 | Res->setHasNoSignedWrap(cast<BinaryOperator>(Inst)->hasNoSignedWrap()); |
| 357 | Res->setHasNoUnsignedWrap(cast<BinaryOperator>(Inst)->hasNoUnsignedWrap()); |
| 358 | NewInsts.push_back(Res); |
| 359 | return Res; |
| 360 | } |
| 361 | #endif |
| 362 | |
Chris Lattner | 210e45a | 2009-12-04 02:10:16 +0000 | [diff] [blame] | 363 | return 0; |
| 364 | } |