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