LLVM API Documentation
00001 //===- PHITransAddr.cpp - PHI Translation for Addresses -------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the PHITransAddr class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Analysis/PHITransAddr.h" 00015 #include "llvm/Analysis/Dominators.h" 00016 #include "llvm/Analysis/InstructionSimplify.h" 00017 #include "llvm/Analysis/ValueTracking.h" 00018 #include "llvm/IR/Constants.h" 00019 #include "llvm/IR/Instructions.h" 00020 #include "llvm/Support/Debug.h" 00021 #include "llvm/Support/ErrorHandling.h" 00022 #include "llvm/Support/raw_ostream.h" 00023 using namespace llvm; 00024 00025 static bool CanPHITrans(Instruction *Inst) { 00026 if (isa<PHINode>(Inst) || 00027 isa<GetElementPtrInst>(Inst)) 00028 return true; 00029 00030 if (isa<CastInst>(Inst) && 00031 isSafeToSpeculativelyExecute(Inst)) 00032 return true; 00033 00034 if (Inst->getOpcode() == Instruction::Add && 00035 isa<ConstantInt>(Inst->getOperand(1))) 00036 return true; 00037 00038 // cerr << "MEMDEP: Could not PHI translate: " << *Pointer; 00039 // if (isa<BitCastInst>(PtrInst) || isa<GetElementPtrInst>(PtrInst)) 00040 // cerr << "OP:\t\t\t\t" << *PtrInst->getOperand(0); 00041 return false; 00042 } 00043 00044 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 00045 void PHITransAddr::dump() const { 00046 if (Addr == 0) { 00047 dbgs() << "PHITransAddr: null\n"; 00048 return; 00049 } 00050 dbgs() << "PHITransAddr: " << *Addr << "\n"; 00051 for (unsigned i = 0, e = InstInputs.size(); i != e; ++i) 00052 dbgs() << " Input #" << i << " is " << *InstInputs[i] << "\n"; 00053 } 00054 #endif 00055 00056 00057 static bool VerifySubExpr(Value *Expr, 00058 SmallVectorImpl<Instruction*> &InstInputs) { 00059 // If this is a non-instruction value, there is nothing to do. 00060 Instruction *I = dyn_cast<Instruction>(Expr); 00061 if (I == 0) return true; 00062 00063 // If it's an instruction, it is either in Tmp or its operands recursively 00064 // are. 00065 SmallVectorImpl<Instruction*>::iterator Entry = 00066 std::find(InstInputs.begin(), InstInputs.end(), I); 00067 if (Entry != InstInputs.end()) { 00068 InstInputs.erase(Entry); 00069 return true; 00070 } 00071 00072 // If it isn't in the InstInputs list it is a subexpr incorporated into the 00073 // address. Sanity check that it is phi translatable. 00074 if (!CanPHITrans(I)) { 00075 errs() << "Non phi translatable instruction found in PHITransAddr:\n"; 00076 errs() << *I << '\n'; 00077 llvm_unreachable("Either something is missing from InstInputs or " 00078 "CanPHITrans is wrong."); 00079 } 00080 00081 // Validate the operands of the instruction. 00082 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 00083 if (!VerifySubExpr(I->getOperand(i), InstInputs)) 00084 return false; 00085 00086 return true; 00087 } 00088 00089 /// Verify - Check internal consistency of this data structure. If the 00090 /// structure is valid, it returns true. If invalid, it prints errors and 00091 /// returns false. 00092 bool PHITransAddr::Verify() const { 00093 if (Addr == 0) return true; 00094 00095 SmallVector<Instruction*, 8> Tmp(InstInputs.begin(), InstInputs.end()); 00096 00097 if (!VerifySubExpr(Addr, Tmp)) 00098 return false; 00099 00100 if (!Tmp.empty()) { 00101 errs() << "PHITransAddr contains extra instructions:\n"; 00102 for (unsigned i = 0, e = InstInputs.size(); i != e; ++i) 00103 errs() << " InstInput #" << i << " is " << *InstInputs[i] << "\n"; 00104 llvm_unreachable("This is unexpected."); 00105 } 00106 00107 // a-ok. 00108 return true; 00109 } 00110 00111 00112 /// IsPotentiallyPHITranslatable - If this needs PHI translation, return true 00113 /// if we have some hope of doing it. This should be used as a filter to 00114 /// avoid calling PHITranslateValue in hopeless situations. 00115 bool PHITransAddr::IsPotentiallyPHITranslatable() const { 00116 // If the input value is not an instruction, or if it is not defined in CurBB, 00117 // then we don't need to phi translate it. 00118 Instruction *Inst = dyn_cast<Instruction>(Addr); 00119 return Inst == 0 || CanPHITrans(Inst); 00120 } 00121 00122 00123 static void RemoveInstInputs(Value *V, 00124 SmallVectorImpl<Instruction*> &InstInputs) { 00125 Instruction *I = dyn_cast<Instruction>(V); 00126 if (I == 0) return; 00127 00128 // If the instruction is in the InstInputs list, remove it. 00129 SmallVectorImpl<Instruction*>::iterator Entry = 00130 std::find(InstInputs.begin(), InstInputs.end(), I); 00131 if (Entry != InstInputs.end()) { 00132 InstInputs.erase(Entry); 00133 return; 00134 } 00135 00136 assert(!isa<PHINode>(I) && "Error, removing something that isn't an input"); 00137 00138 // Otherwise, it must have instruction inputs itself. Zap them recursively. 00139 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { 00140 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i))) 00141 RemoveInstInputs(Op, InstInputs); 00142 } 00143 } 00144 00145 Value *PHITransAddr::PHITranslateSubExpr(Value *V, BasicBlock *CurBB, 00146 BasicBlock *PredBB, 00147 const DominatorTree *DT) { 00148 // If this is a non-instruction value, it can't require PHI translation. 00149 Instruction *Inst = dyn_cast<Instruction>(V); 00150 if (Inst == 0) return V; 00151 00152 // Determine whether 'Inst' is an input to our PHI translatable expression. 00153 bool isInput = std::count(InstInputs.begin(), InstInputs.end(), Inst); 00154 00155 // Handle inputs instructions if needed. 00156 if (isInput) { 00157 if (Inst->getParent() != CurBB) { 00158 // If it is an input defined in a different block, then it remains an 00159 // input. 00160 return Inst; 00161 } 00162 00163 // If 'Inst' is defined in this block and is an input that needs to be phi 00164 // translated, we need to incorporate the value into the expression or fail. 00165 00166 // In either case, the instruction itself isn't an input any longer. 00167 InstInputs.erase(std::find(InstInputs.begin(), InstInputs.end(), Inst)); 00168 00169 // If this is a PHI, go ahead and translate it. 00170 if (PHINode *PN = dyn_cast<PHINode>(Inst)) 00171 return AddAsInput(PN->getIncomingValueForBlock(PredBB)); 00172 00173 // If this is a non-phi value, and it is analyzable, we can incorporate it 00174 // into the expression by making all instruction operands be inputs. 00175 if (!CanPHITrans(Inst)) 00176 return 0; 00177 00178 // All instruction operands are now inputs (and of course, they may also be 00179 // defined in this block, so they may need to be phi translated themselves. 00180 for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) 00181 if (Instruction *Op = dyn_cast<Instruction>(Inst->getOperand(i))) 00182 InstInputs.push_back(Op); 00183 } 00184 00185 // Ok, it must be an intermediate result (either because it started that way 00186 // or because we just incorporated it into the expression). See if its 00187 // operands need to be phi translated, and if so, reconstruct it. 00188 00189 if (CastInst *Cast = dyn_cast<CastInst>(Inst)) { 00190 if (!isSafeToSpeculativelyExecute(Cast)) return 0; 00191 Value *PHIIn = PHITranslateSubExpr(Cast->getOperand(0), CurBB, PredBB, DT); 00192 if (PHIIn == 0) return 0; 00193 if (PHIIn == Cast->getOperand(0)) 00194 return Cast; 00195 00196 // Find an available version of this cast. 00197 00198 // Constants are trivial to find. 00199 if (Constant *C = dyn_cast<Constant>(PHIIn)) 00200 return AddAsInput(ConstantExpr::getCast(Cast->getOpcode(), 00201 C, Cast->getType())); 00202 00203 // Otherwise we have to see if a casted version of the incoming pointer 00204 // is available. If so, we can use it, otherwise we have to fail. 00205 for (Value::use_iterator UI = PHIIn->use_begin(), E = PHIIn->use_end(); 00206 UI != E; ++UI) { 00207 if (CastInst *CastI = dyn_cast<CastInst>(*UI)) 00208 if (CastI->getOpcode() == Cast->getOpcode() && 00209 CastI->getType() == Cast->getType() && 00210 (!DT || DT->dominates(CastI->getParent(), PredBB))) 00211 return CastI; 00212 } 00213 return 0; 00214 } 00215 00216 // Handle getelementptr with at least one PHI translatable operand. 00217 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) { 00218 SmallVector<Value*, 8> GEPOps; 00219 bool AnyChanged = false; 00220 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) { 00221 Value *GEPOp = PHITranslateSubExpr(GEP->getOperand(i), CurBB, PredBB, DT); 00222 if (GEPOp == 0) return 0; 00223 00224 AnyChanged |= GEPOp != GEP->getOperand(i); 00225 GEPOps.push_back(GEPOp); 00226 } 00227 00228 if (!AnyChanged) 00229 return GEP; 00230 00231 // Simplify the GEP to handle 'gep x, 0' -> x etc. 00232 if (Value *V = SimplifyGEPInst(GEPOps, TD, TLI, DT)) { 00233 for (unsigned i = 0, e = GEPOps.size(); i != e; ++i) 00234 RemoveInstInputs(GEPOps[i], InstInputs); 00235 00236 return AddAsInput(V); 00237 } 00238 00239 // Scan to see if we have this GEP available. 00240 Value *APHIOp = GEPOps[0]; 00241 for (Value::use_iterator UI = APHIOp->use_begin(), E = APHIOp->use_end(); 00242 UI != E; ++UI) { 00243 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI)) 00244 if (GEPI->getType() == GEP->getType() && 00245 GEPI->getNumOperands() == GEPOps.size() && 00246 GEPI->getParent()->getParent() == CurBB->getParent() && 00247 (!DT || DT->dominates(GEPI->getParent(), PredBB))) { 00248 bool Mismatch = false; 00249 for (unsigned i = 0, e = GEPOps.size(); i != e; ++i) 00250 if (GEPI->getOperand(i) != GEPOps[i]) { 00251 Mismatch = true; 00252 break; 00253 } 00254 if (!Mismatch) 00255 return GEPI; 00256 } 00257 } 00258 return 0; 00259 } 00260 00261 // Handle add with a constant RHS. 00262 if (Inst->getOpcode() == Instruction::Add && 00263 isa<ConstantInt>(Inst->getOperand(1))) { 00264 // PHI translate the LHS. 00265 Constant *RHS = cast<ConstantInt>(Inst->getOperand(1)); 00266 bool isNSW = cast<BinaryOperator>(Inst)->hasNoSignedWrap(); 00267 bool isNUW = cast<BinaryOperator>(Inst)->hasNoUnsignedWrap(); 00268 00269 Value *LHS = PHITranslateSubExpr(Inst->getOperand(0), CurBB, PredBB, DT); 00270 if (LHS == 0) return 0; 00271 00272 // If the PHI translated LHS is an add of a constant, fold the immediates. 00273 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(LHS)) 00274 if (BOp->getOpcode() == Instruction::Add) 00275 if (ConstantInt *CI = dyn_cast<ConstantInt>(BOp->getOperand(1))) { 00276 LHS = BOp->getOperand(0); 00277 RHS = ConstantExpr::getAdd(RHS, CI); 00278 isNSW = isNUW = false; 00279 00280 // If the old 'LHS' was an input, add the new 'LHS' as an input. 00281 if (std::count(InstInputs.begin(), InstInputs.end(), BOp)) { 00282 RemoveInstInputs(BOp, InstInputs); 00283 AddAsInput(LHS); 00284 } 00285 } 00286 00287 // See if the add simplifies away. 00288 if (Value *Res = SimplifyAddInst(LHS, RHS, isNSW, isNUW, TD, TLI, DT)) { 00289 // If we simplified the operands, the LHS is no longer an input, but Res 00290 // is. 00291 RemoveInstInputs(LHS, InstInputs); 00292 return AddAsInput(Res); 00293 } 00294 00295 // If we didn't modify the add, just return it. 00296 if (LHS == Inst->getOperand(0) && RHS == Inst->getOperand(1)) 00297 return Inst; 00298 00299 // Otherwise, see if we have this add available somewhere. 00300 for (Value::use_iterator UI = LHS->use_begin(), E = LHS->use_end(); 00301 UI != E; ++UI) { 00302 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*UI)) 00303 if (BO->getOpcode() == Instruction::Add && 00304 BO->getOperand(0) == LHS && BO->getOperand(1) == RHS && 00305 BO->getParent()->getParent() == CurBB->getParent() && 00306 (!DT || DT->dominates(BO->getParent(), PredBB))) 00307 return BO; 00308 } 00309 00310 return 0; 00311 } 00312 00313 // Otherwise, we failed. 00314 return 0; 00315 } 00316 00317 00318 /// PHITranslateValue - PHI translate the current address up the CFG from 00319 /// CurBB to Pred, updating our state to reflect any needed changes. If the 00320 /// dominator tree DT is non-null, the translated value must dominate 00321 /// PredBB. This returns true on failure and sets Addr to null. 00322 bool PHITransAddr::PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB, 00323 const DominatorTree *DT) { 00324 assert(Verify() && "Invalid PHITransAddr!"); 00325 Addr = PHITranslateSubExpr(Addr, CurBB, PredBB, DT); 00326 assert(Verify() && "Invalid PHITransAddr!"); 00327 00328 if (DT) { 00329 // Make sure the value is live in the predecessor. 00330 if (Instruction *Inst = dyn_cast_or_null<Instruction>(Addr)) 00331 if (!DT->dominates(Inst->getParent(), PredBB)) 00332 Addr = 0; 00333 } 00334 00335 return Addr == 0; 00336 } 00337 00338 /// PHITranslateWithInsertion - PHI translate this value into the specified 00339 /// predecessor block, inserting a computation of the value if it is 00340 /// unavailable. 00341 /// 00342 /// All newly created instructions are added to the NewInsts list. This 00343 /// returns null on failure. 00344 /// 00345 Value *PHITransAddr:: 00346 PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB, 00347 const DominatorTree &DT, 00348 SmallVectorImpl<Instruction*> &NewInsts) { 00349 unsigned NISize = NewInsts.size(); 00350 00351 // Attempt to PHI translate with insertion. 00352 Addr = InsertPHITranslatedSubExpr(Addr, CurBB, PredBB, DT, NewInsts); 00353 00354 // If successful, return the new value. 00355 if (Addr) return Addr; 00356 00357 // If not, destroy any intermediate instructions inserted. 00358 while (NewInsts.size() != NISize) 00359 NewInsts.pop_back_val()->eraseFromParent(); 00360 return 0; 00361 } 00362 00363 00364 /// InsertPHITranslatedPointer - Insert a computation of the PHI translated 00365 /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB 00366 /// block. All newly created instructions are added to the NewInsts list. 00367 /// This returns null on failure. 00368 /// 00369 Value *PHITransAddr:: 00370 InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB, 00371 BasicBlock *PredBB, const DominatorTree &DT, 00372 SmallVectorImpl<Instruction*> &NewInsts) { 00373 // See if we have a version of this value already available and dominating 00374 // PredBB. If so, there is no need to insert a new instance of it. 00375 PHITransAddr Tmp(InVal, TD); 00376 if (!Tmp.PHITranslateValue(CurBB, PredBB, &DT)) 00377 return Tmp.getAddr(); 00378 00379 // If we don't have an available version of this value, it must be an 00380 // instruction. 00381 Instruction *Inst = cast<Instruction>(InVal); 00382 00383 // Handle cast of PHI translatable value. 00384 if (CastInst *Cast = dyn_cast<CastInst>(Inst)) { 00385 if (!isSafeToSpeculativelyExecute(Cast)) return 0; 00386 Value *OpVal = InsertPHITranslatedSubExpr(Cast->getOperand(0), 00387 CurBB, PredBB, DT, NewInsts); 00388 if (OpVal == 0) return 0; 00389 00390 // Otherwise insert a cast at the end of PredBB. 00391 CastInst *New = CastInst::Create(Cast->getOpcode(), 00392 OpVal, InVal->getType(), 00393 InVal->getName()+".phi.trans.insert", 00394 PredBB->getTerminator()); 00395 NewInsts.push_back(New); 00396 return New; 00397 } 00398 00399 // Handle getelementptr with at least one PHI operand. 00400 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) { 00401 SmallVector<Value*, 8> GEPOps; 00402 BasicBlock *CurBB = GEP->getParent(); 00403 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) { 00404 Value *OpVal = InsertPHITranslatedSubExpr(GEP->getOperand(i), 00405 CurBB, PredBB, DT, NewInsts); 00406 if (OpVal == 0) return 0; 00407 GEPOps.push_back(OpVal); 00408 } 00409 00410 GetElementPtrInst *Result = 00411 GetElementPtrInst::Create(GEPOps[0], makeArrayRef(GEPOps).slice(1), 00412 InVal->getName()+".phi.trans.insert", 00413 PredBB->getTerminator()); 00414 Result->setIsInBounds(GEP->isInBounds()); 00415 NewInsts.push_back(Result); 00416 return Result; 00417 } 00418 00419 #if 0 00420 // FIXME: This code works, but it is unclear that we actually want to insert 00421 // a big chain of computation in order to make a value available in a block. 00422 // This needs to be evaluated carefully to consider its cost trade offs. 00423 00424 // Handle add with a constant RHS. 00425 if (Inst->getOpcode() == Instruction::Add && 00426 isa<ConstantInt>(Inst->getOperand(1))) { 00427 // PHI translate the LHS. 00428 Value *OpVal = InsertPHITranslatedSubExpr(Inst->getOperand(0), 00429 CurBB, PredBB, DT, NewInsts); 00430 if (OpVal == 0) return 0; 00431 00432 BinaryOperator *Res = BinaryOperator::CreateAdd(OpVal, Inst->getOperand(1), 00433 InVal->getName()+".phi.trans.insert", 00434 PredBB->getTerminator()); 00435 Res->setHasNoSignedWrap(cast<BinaryOperator>(Inst)->hasNoSignedWrap()); 00436 Res->setHasNoUnsignedWrap(cast<BinaryOperator>(Inst)->hasNoUnsignedWrap()); 00437 NewInsts.push_back(Res); 00438 return Res; 00439 } 00440 #endif 00441 00442 return 0; 00443 }