LLVM API Documentation
00001 //===-- Local.cpp - Functions to perform local transformations ------------===// 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 family of functions perform various local transformations to the 00011 // program. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Transforms/Utils/Local.h" 00016 #include "llvm/ADT/DenseMap.h" 00017 #include "llvm/ADT/STLExtras.h" 00018 #include "llvm/ADT/SmallPtrSet.h" 00019 #include "llvm/ADT/Statistic.h" 00020 #include "llvm/Analysis/InstructionSimplify.h" 00021 #include "llvm/Analysis/MemoryBuiltins.h" 00022 #include "llvm/Analysis/ValueTracking.h" 00023 #include "llvm/IR/CFG.h" 00024 #include "llvm/IR/Constants.h" 00025 #include "llvm/IR/DIBuilder.h" 00026 #include "llvm/IR/DataLayout.h" 00027 #include "llvm/IR/DebugInfo.h" 00028 #include "llvm/IR/DerivedTypes.h" 00029 #include "llvm/IR/Dominators.h" 00030 #include "llvm/IR/GetElementPtrTypeIterator.h" 00031 #include "llvm/IR/GlobalAlias.h" 00032 #include "llvm/IR/GlobalVariable.h" 00033 #include "llvm/IR/IRBuilder.h" 00034 #include "llvm/IR/Instructions.h" 00035 #include "llvm/IR/IntrinsicInst.h" 00036 #include "llvm/IR/Intrinsics.h" 00037 #include "llvm/IR/MDBuilder.h" 00038 #include "llvm/IR/Metadata.h" 00039 #include "llvm/IR/Operator.h" 00040 #include "llvm/IR/ValueHandle.h" 00041 #include "llvm/Support/Debug.h" 00042 #include "llvm/Support/MathExtras.h" 00043 #include "llvm/Support/raw_ostream.h" 00044 using namespace llvm; 00045 00046 #define DEBUG_TYPE "local" 00047 00048 STATISTIC(NumRemoved, "Number of unreachable basic blocks removed"); 00049 00050 //===----------------------------------------------------------------------===// 00051 // Local constant propagation. 00052 // 00053 00054 /// ConstantFoldTerminator - If a terminator instruction is predicated on a 00055 /// constant value, convert it into an unconditional branch to the constant 00056 /// destination. This is a nontrivial operation because the successors of this 00057 /// basic block must have their PHI nodes updated. 00058 /// Also calls RecursivelyDeleteTriviallyDeadInstructions() on any branch/switch 00059 /// conditions and indirectbr addresses this might make dead if 00060 /// DeleteDeadConditions is true. 00061 bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions, 00062 const TargetLibraryInfo *TLI) { 00063 TerminatorInst *T = BB->getTerminator(); 00064 IRBuilder<> Builder(T); 00065 00066 // Branch - See if we are conditional jumping on constant 00067 if (BranchInst *BI = dyn_cast<BranchInst>(T)) { 00068 if (BI->isUnconditional()) return false; // Can't optimize uncond branch 00069 BasicBlock *Dest1 = BI->getSuccessor(0); 00070 BasicBlock *Dest2 = BI->getSuccessor(1); 00071 00072 if (ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition())) { 00073 // Are we branching on constant? 00074 // YES. Change to unconditional branch... 00075 BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2; 00076 BasicBlock *OldDest = Cond->getZExtValue() ? Dest2 : Dest1; 00077 00078 //cerr << "Function: " << T->getParent()->getParent() 00079 // << "\nRemoving branch from " << T->getParent() 00080 // << "\n\nTo: " << OldDest << endl; 00081 00082 // Let the basic block know that we are letting go of it. Based on this, 00083 // it will adjust it's PHI nodes. 00084 OldDest->removePredecessor(BB); 00085 00086 // Replace the conditional branch with an unconditional one. 00087 Builder.CreateBr(Destination); 00088 BI->eraseFromParent(); 00089 return true; 00090 } 00091 00092 if (Dest2 == Dest1) { // Conditional branch to same location? 00093 // This branch matches something like this: 00094 // br bool %cond, label %Dest, label %Dest 00095 // and changes it into: br label %Dest 00096 00097 // Let the basic block know that we are letting go of one copy of it. 00098 assert(BI->getParent() && "Terminator not inserted in block!"); 00099 Dest1->removePredecessor(BI->getParent()); 00100 00101 // Replace the conditional branch with an unconditional one. 00102 Builder.CreateBr(Dest1); 00103 Value *Cond = BI->getCondition(); 00104 BI->eraseFromParent(); 00105 if (DeleteDeadConditions) 00106 RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI); 00107 return true; 00108 } 00109 return false; 00110 } 00111 00112 if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) { 00113 // If we are switching on a constant, we can convert the switch into a 00114 // single branch instruction! 00115 ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition()); 00116 BasicBlock *TheOnlyDest = SI->getDefaultDest(); 00117 BasicBlock *DefaultDest = TheOnlyDest; 00118 00119 // Figure out which case it goes to. 00120 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); 00121 i != e; ++i) { 00122 // Found case matching a constant operand? 00123 if (i.getCaseValue() == CI) { 00124 TheOnlyDest = i.getCaseSuccessor(); 00125 break; 00126 } 00127 00128 // Check to see if this branch is going to the same place as the default 00129 // dest. If so, eliminate it as an explicit compare. 00130 if (i.getCaseSuccessor() == DefaultDest) { 00131 MDNode* MD = SI->getMetadata(LLVMContext::MD_prof); 00132 unsigned NCases = SI->getNumCases(); 00133 // Fold the case metadata into the default if there will be any branches 00134 // left, unless the metadata doesn't match the switch. 00135 if (NCases > 1 && MD && MD->getNumOperands() == 2 + NCases) { 00136 // Collect branch weights into a vector. 00137 SmallVector<uint32_t, 8> Weights; 00138 for (unsigned MD_i = 1, MD_e = MD->getNumOperands(); MD_i < MD_e; 00139 ++MD_i) { 00140 ConstantInt* CI = dyn_cast<ConstantInt>(MD->getOperand(MD_i)); 00141 assert(CI); 00142 Weights.push_back(CI->getValue().getZExtValue()); 00143 } 00144 // Merge weight of this case to the default weight. 00145 unsigned idx = i.getCaseIndex(); 00146 Weights[0] += Weights[idx+1]; 00147 // Remove weight for this case. 00148 std::swap(Weights[idx+1], Weights.back()); 00149 Weights.pop_back(); 00150 SI->setMetadata(LLVMContext::MD_prof, 00151 MDBuilder(BB->getContext()). 00152 createBranchWeights(Weights)); 00153 } 00154 // Remove this entry. 00155 DefaultDest->removePredecessor(SI->getParent()); 00156 SI->removeCase(i); 00157 --i; --e; 00158 continue; 00159 } 00160 00161 // Otherwise, check to see if the switch only branches to one destination. 00162 // We do this by reseting "TheOnlyDest" to null when we find two non-equal 00163 // destinations. 00164 if (i.getCaseSuccessor() != TheOnlyDest) TheOnlyDest = nullptr; 00165 } 00166 00167 if (CI && !TheOnlyDest) { 00168 // Branching on a constant, but not any of the cases, go to the default 00169 // successor. 00170 TheOnlyDest = SI->getDefaultDest(); 00171 } 00172 00173 // If we found a single destination that we can fold the switch into, do so 00174 // now. 00175 if (TheOnlyDest) { 00176 // Insert the new branch. 00177 Builder.CreateBr(TheOnlyDest); 00178 BasicBlock *BB = SI->getParent(); 00179 00180 // Remove entries from PHI nodes which we no longer branch to... 00181 for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) { 00182 // Found case matching a constant operand? 00183 BasicBlock *Succ = SI->getSuccessor(i); 00184 if (Succ == TheOnlyDest) 00185 TheOnlyDest = nullptr; // Don't modify the first branch to TheOnlyDest 00186 else 00187 Succ->removePredecessor(BB); 00188 } 00189 00190 // Delete the old switch. 00191 Value *Cond = SI->getCondition(); 00192 SI->eraseFromParent(); 00193 if (DeleteDeadConditions) 00194 RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI); 00195 return true; 00196 } 00197 00198 if (SI->getNumCases() == 1) { 00199 // Otherwise, we can fold this switch into a conditional branch 00200 // instruction if it has only one non-default destination. 00201 SwitchInst::CaseIt FirstCase = SI->case_begin(); 00202 Value *Cond = Builder.CreateICmpEQ(SI->getCondition(), 00203 FirstCase.getCaseValue(), "cond"); 00204 00205 // Insert the new branch. 00206 BranchInst *NewBr = Builder.CreateCondBr(Cond, 00207 FirstCase.getCaseSuccessor(), 00208 SI->getDefaultDest()); 00209 MDNode* MD = SI->getMetadata(LLVMContext::MD_prof); 00210 if (MD && MD->getNumOperands() == 3) { 00211 ConstantInt *SICase = dyn_cast<ConstantInt>(MD->getOperand(2)); 00212 ConstantInt *SIDef = dyn_cast<ConstantInt>(MD->getOperand(1)); 00213 assert(SICase && SIDef); 00214 // The TrueWeight should be the weight for the single case of SI. 00215 NewBr->setMetadata(LLVMContext::MD_prof, 00216 MDBuilder(BB->getContext()). 00217 createBranchWeights(SICase->getValue().getZExtValue(), 00218 SIDef->getValue().getZExtValue())); 00219 } 00220 00221 // Delete the old switch. 00222 SI->eraseFromParent(); 00223 return true; 00224 } 00225 return false; 00226 } 00227 00228 if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(T)) { 00229 // indirectbr blockaddress(@F, @BB) -> br label @BB 00230 if (BlockAddress *BA = 00231 dyn_cast<BlockAddress>(IBI->getAddress()->stripPointerCasts())) { 00232 BasicBlock *TheOnlyDest = BA->getBasicBlock(); 00233 // Insert the new branch. 00234 Builder.CreateBr(TheOnlyDest); 00235 00236 for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) { 00237 if (IBI->getDestination(i) == TheOnlyDest) 00238 TheOnlyDest = nullptr; 00239 else 00240 IBI->getDestination(i)->removePredecessor(IBI->getParent()); 00241 } 00242 Value *Address = IBI->getAddress(); 00243 IBI->eraseFromParent(); 00244 if (DeleteDeadConditions) 00245 RecursivelyDeleteTriviallyDeadInstructions(Address, TLI); 00246 00247 // If we didn't find our destination in the IBI successor list, then we 00248 // have undefined behavior. Replace the unconditional branch with an 00249 // 'unreachable' instruction. 00250 if (TheOnlyDest) { 00251 BB->getTerminator()->eraseFromParent(); 00252 new UnreachableInst(BB->getContext(), BB); 00253 } 00254 00255 return true; 00256 } 00257 } 00258 00259 return false; 00260 } 00261 00262 00263 //===----------------------------------------------------------------------===// 00264 // Local dead code elimination. 00265 // 00266 00267 /// isInstructionTriviallyDead - Return true if the result produced by the 00268 /// instruction is not used, and the instruction has no side effects. 00269 /// 00270 bool llvm::isInstructionTriviallyDead(Instruction *I, 00271 const TargetLibraryInfo *TLI) { 00272 if (!I->use_empty() || isa<TerminatorInst>(I)) return false; 00273 00274 // We don't want the landingpad instruction removed by anything this general. 00275 if (isa<LandingPadInst>(I)) 00276 return false; 00277 00278 // We don't want debug info removed by anything this general, unless 00279 // debug info is empty. 00280 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) { 00281 if (DDI->getAddress()) 00282 return false; 00283 return true; 00284 } 00285 if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) { 00286 if (DVI->getValue()) 00287 return false; 00288 return true; 00289 } 00290 00291 if (!I->mayHaveSideEffects()) return true; 00292 00293 // Special case intrinsics that "may have side effects" but can be deleted 00294 // when dead. 00295 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { 00296 // Safe to delete llvm.stacksave if dead. 00297 if (II->getIntrinsicID() == Intrinsic::stacksave) 00298 return true; 00299 00300 // Lifetime intrinsics are dead when their right-hand is undef. 00301 if (II->getIntrinsicID() == Intrinsic::lifetime_start || 00302 II->getIntrinsicID() == Intrinsic::lifetime_end) 00303 return isa<UndefValue>(II->getArgOperand(1)); 00304 00305 // Assumptions are dead if their condition is trivially true. 00306 if (II->getIntrinsicID() == Intrinsic::assume) { 00307 if (ConstantInt *Cond = dyn_cast<ConstantInt>(II->getArgOperand(0))) 00308 return !Cond->isZero(); 00309 00310 return false; 00311 } 00312 } 00313 00314 if (isAllocLikeFn(I, TLI)) return true; 00315 00316 if (CallInst *CI = isFreeCall(I, TLI)) 00317 if (Constant *C = dyn_cast<Constant>(CI->getArgOperand(0))) 00318 return C->isNullValue() || isa<UndefValue>(C); 00319 00320 return false; 00321 } 00322 00323 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a 00324 /// trivially dead instruction, delete it. If that makes any of its operands 00325 /// trivially dead, delete them too, recursively. Return true if any 00326 /// instructions were deleted. 00327 bool 00328 llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V, 00329 const TargetLibraryInfo *TLI) { 00330 Instruction *I = dyn_cast<Instruction>(V); 00331 if (!I || !I->use_empty() || !isInstructionTriviallyDead(I, TLI)) 00332 return false; 00333 00334 SmallVector<Instruction*, 16> DeadInsts; 00335 DeadInsts.push_back(I); 00336 00337 do { 00338 I = DeadInsts.pop_back_val(); 00339 00340 // Null out all of the instruction's operands to see if any operand becomes 00341 // dead as we go. 00342 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { 00343 Value *OpV = I->getOperand(i); 00344 I->setOperand(i, nullptr); 00345 00346 if (!OpV->use_empty()) continue; 00347 00348 // If the operand is an instruction that became dead as we nulled out the 00349 // operand, and if it is 'trivially' dead, delete it in a future loop 00350 // iteration. 00351 if (Instruction *OpI = dyn_cast<Instruction>(OpV)) 00352 if (isInstructionTriviallyDead(OpI, TLI)) 00353 DeadInsts.push_back(OpI); 00354 } 00355 00356 I->eraseFromParent(); 00357 } while (!DeadInsts.empty()); 00358 00359 return true; 00360 } 00361 00362 /// areAllUsesEqual - Check whether the uses of a value are all the same. 00363 /// This is similar to Instruction::hasOneUse() except this will also return 00364 /// true when there are no uses or multiple uses that all refer to the same 00365 /// value. 00366 static bool areAllUsesEqual(Instruction *I) { 00367 Value::user_iterator UI = I->user_begin(); 00368 Value::user_iterator UE = I->user_end(); 00369 if (UI == UE) 00370 return true; 00371 00372 User *TheUse = *UI; 00373 for (++UI; UI != UE; ++UI) { 00374 if (*UI != TheUse) 00375 return false; 00376 } 00377 return true; 00378 } 00379 00380 /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively 00381 /// dead PHI node, due to being a def-use chain of single-use nodes that 00382 /// either forms a cycle or is terminated by a trivially dead instruction, 00383 /// delete it. If that makes any of its operands trivially dead, delete them 00384 /// too, recursively. Return true if a change was made. 00385 bool llvm::RecursivelyDeleteDeadPHINode(PHINode *PN, 00386 const TargetLibraryInfo *TLI) { 00387 SmallPtrSet<Instruction*, 4> Visited; 00388 for (Instruction *I = PN; areAllUsesEqual(I) && !I->mayHaveSideEffects(); 00389 I = cast<Instruction>(*I->user_begin())) { 00390 if (I->use_empty()) 00391 return RecursivelyDeleteTriviallyDeadInstructions(I, TLI); 00392 00393 // If we find an instruction more than once, we're on a cycle that 00394 // won't prove fruitful. 00395 if (!Visited.insert(I)) { 00396 // Break the cycle and delete the instruction and its operands. 00397 I->replaceAllUsesWith(UndefValue::get(I->getType())); 00398 (void)RecursivelyDeleteTriviallyDeadInstructions(I, TLI); 00399 return true; 00400 } 00401 } 00402 return false; 00403 } 00404 00405 /// SimplifyInstructionsInBlock - Scan the specified basic block and try to 00406 /// simplify any instructions in it and recursively delete dead instructions. 00407 /// 00408 /// This returns true if it changed the code, note that it can delete 00409 /// instructions in other blocks as well in this block. 00410 bool llvm::SimplifyInstructionsInBlock(BasicBlock *BB, const DataLayout *TD, 00411 const TargetLibraryInfo *TLI) { 00412 bool MadeChange = false; 00413 00414 #ifndef NDEBUG 00415 // In debug builds, ensure that the terminator of the block is never replaced 00416 // or deleted by these simplifications. The idea of simplification is that it 00417 // cannot introduce new instructions, and there is no way to replace the 00418 // terminator of a block without introducing a new instruction. 00419 AssertingVH<Instruction> TerminatorVH(--BB->end()); 00420 #endif 00421 00422 for (BasicBlock::iterator BI = BB->begin(), E = --BB->end(); BI != E; ) { 00423 assert(!BI->isTerminator()); 00424 Instruction *Inst = BI++; 00425 00426 WeakVH BIHandle(BI); 00427 if (recursivelySimplifyInstruction(Inst, TD, TLI)) { 00428 MadeChange = true; 00429 if (BIHandle != BI) 00430 BI = BB->begin(); 00431 continue; 00432 } 00433 00434 MadeChange |= RecursivelyDeleteTriviallyDeadInstructions(Inst, TLI); 00435 if (BIHandle != BI) 00436 BI = BB->begin(); 00437 } 00438 return MadeChange; 00439 } 00440 00441 //===----------------------------------------------------------------------===// 00442 // Control Flow Graph Restructuring. 00443 // 00444 00445 00446 /// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this 00447 /// method is called when we're about to delete Pred as a predecessor of BB. If 00448 /// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred. 00449 /// 00450 /// Unlike the removePredecessor method, this attempts to simplify uses of PHI 00451 /// nodes that collapse into identity values. For example, if we have: 00452 /// x = phi(1, 0, 0, 0) 00453 /// y = and x, z 00454 /// 00455 /// .. and delete the predecessor corresponding to the '1', this will attempt to 00456 /// recursively fold the and to 0. 00457 void llvm::RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred, 00458 DataLayout *TD) { 00459 // This only adjusts blocks with PHI nodes. 00460 if (!isa<PHINode>(BB->begin())) 00461 return; 00462 00463 // Remove the entries for Pred from the PHI nodes in BB, but do not simplify 00464 // them down. This will leave us with single entry phi nodes and other phis 00465 // that can be removed. 00466 BB->removePredecessor(Pred, true); 00467 00468 WeakVH PhiIt = &BB->front(); 00469 while (PHINode *PN = dyn_cast<PHINode>(PhiIt)) { 00470 PhiIt = &*++BasicBlock::iterator(cast<Instruction>(PhiIt)); 00471 Value *OldPhiIt = PhiIt; 00472 00473 if (!recursivelySimplifyInstruction(PN, TD)) 00474 continue; 00475 00476 // If recursive simplification ended up deleting the next PHI node we would 00477 // iterate to, then our iterator is invalid, restart scanning from the top 00478 // of the block. 00479 if (PhiIt != OldPhiIt) PhiIt = &BB->front(); 00480 } 00481 } 00482 00483 00484 /// MergeBasicBlockIntoOnlyPred - DestBB is a block with one predecessor and its 00485 /// predecessor is known to have one successor (DestBB!). Eliminate the edge 00486 /// between them, moving the instructions in the predecessor into DestBB and 00487 /// deleting the predecessor block. 00488 /// 00489 void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, Pass *P) { 00490 // If BB has single-entry PHI nodes, fold them. 00491 while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) { 00492 Value *NewVal = PN->getIncomingValue(0); 00493 // Replace self referencing PHI with undef, it must be dead. 00494 if (NewVal == PN) NewVal = UndefValue::get(PN->getType()); 00495 PN->replaceAllUsesWith(NewVal); 00496 PN->eraseFromParent(); 00497 } 00498 00499 BasicBlock *PredBB = DestBB->getSinglePredecessor(); 00500 assert(PredBB && "Block doesn't have a single predecessor!"); 00501 00502 // Zap anything that took the address of DestBB. Not doing this will give the 00503 // address an invalid value. 00504 if (DestBB->hasAddressTaken()) { 00505 BlockAddress *BA = BlockAddress::get(DestBB); 00506 Constant *Replacement = 00507 ConstantInt::get(llvm::Type::getInt32Ty(BA->getContext()), 1); 00508 BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement, 00509 BA->getType())); 00510 BA->destroyConstant(); 00511 } 00512 00513 // Anything that branched to PredBB now branches to DestBB. 00514 PredBB->replaceAllUsesWith(DestBB); 00515 00516 // Splice all the instructions from PredBB to DestBB. 00517 PredBB->getTerminator()->eraseFromParent(); 00518 DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList()); 00519 00520 // If the PredBB is the entry block of the function, move DestBB up to 00521 // become the entry block after we erase PredBB. 00522 if (PredBB == &DestBB->getParent()->getEntryBlock()) 00523 DestBB->moveAfter(PredBB); 00524 00525 if (P) { 00526 if (DominatorTreeWrapperPass *DTWP = 00527 P->getAnalysisIfAvailable<DominatorTreeWrapperPass>()) { 00528 DominatorTree &DT = DTWP->getDomTree(); 00529 BasicBlock *PredBBIDom = DT.getNode(PredBB)->getIDom()->getBlock(); 00530 DT.changeImmediateDominator(DestBB, PredBBIDom); 00531 DT.eraseNode(PredBB); 00532 } 00533 } 00534 // Nuke BB. 00535 PredBB->eraseFromParent(); 00536 } 00537 00538 /// CanMergeValues - Return true if we can choose one of these values to use 00539 /// in place of the other. Note that we will always choose the non-undef 00540 /// value to keep. 00541 static bool CanMergeValues(Value *First, Value *Second) { 00542 return First == Second || isa<UndefValue>(First) || isa<UndefValue>(Second); 00543 } 00544 00545 /// CanPropagatePredecessorsForPHIs - Return true if we can fold BB, an 00546 /// almost-empty BB ending in an unconditional branch to Succ, into Succ. 00547 /// 00548 /// Assumption: Succ is the single successor for BB. 00549 /// 00550 static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) { 00551 assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!"); 00552 00553 DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into " 00554 << Succ->getName() << "\n"); 00555 // Shortcut, if there is only a single predecessor it must be BB and merging 00556 // is always safe 00557 if (Succ->getSinglePredecessor()) return true; 00558 00559 // Make a list of the predecessors of BB 00560 SmallPtrSet<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB)); 00561 00562 // Look at all the phi nodes in Succ, to see if they present a conflict when 00563 // merging these blocks 00564 for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { 00565 PHINode *PN = cast<PHINode>(I); 00566 00567 // If the incoming value from BB is again a PHINode in 00568 // BB which has the same incoming value for *PI as PN does, we can 00569 // merge the phi nodes and then the blocks can still be merged 00570 PHINode *BBPN = dyn_cast<PHINode>(PN->getIncomingValueForBlock(BB)); 00571 if (BBPN && BBPN->getParent() == BB) { 00572 for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) { 00573 BasicBlock *IBB = PN->getIncomingBlock(PI); 00574 if (BBPreds.count(IBB) && 00575 !CanMergeValues(BBPN->getIncomingValueForBlock(IBB), 00576 PN->getIncomingValue(PI))) { 00577 DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in " 00578 << Succ->getName() << " is conflicting with " 00579 << BBPN->getName() << " with regard to common predecessor " 00580 << IBB->getName() << "\n"); 00581 return false; 00582 } 00583 } 00584 } else { 00585 Value* Val = PN->getIncomingValueForBlock(BB); 00586 for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) { 00587 // See if the incoming value for the common predecessor is equal to the 00588 // one for BB, in which case this phi node will not prevent the merging 00589 // of the block. 00590 BasicBlock *IBB = PN->getIncomingBlock(PI); 00591 if (BBPreds.count(IBB) && 00592 !CanMergeValues(Val, PN->getIncomingValue(PI))) { 00593 DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in " 00594 << Succ->getName() << " is conflicting with regard to common " 00595 << "predecessor " << IBB->getName() << "\n"); 00596 return false; 00597 } 00598 } 00599 } 00600 } 00601 00602 return true; 00603 } 00604 00605 typedef SmallVector<BasicBlock *, 16> PredBlockVector; 00606 typedef DenseMap<BasicBlock *, Value *> IncomingValueMap; 00607 00608 /// \brief Determines the value to use as the phi node input for a block. 00609 /// 00610 /// Select between \p OldVal any value that we know flows from \p BB 00611 /// to a particular phi on the basis of which one (if either) is not 00612 /// undef. Update IncomingValues based on the selected value. 00613 /// 00614 /// \param OldVal The value we are considering selecting. 00615 /// \param BB The block that the value flows in from. 00616 /// \param IncomingValues A map from block-to-value for other phi inputs 00617 /// that we have examined. 00618 /// 00619 /// \returns the selected value. 00620 static Value *selectIncomingValueForBlock(Value *OldVal, BasicBlock *BB, 00621 IncomingValueMap &IncomingValues) { 00622 if (!isa<UndefValue>(OldVal)) { 00623 assert((!IncomingValues.count(BB) || 00624 IncomingValues.find(BB)->second == OldVal) && 00625 "Expected OldVal to match incoming value from BB!"); 00626 00627 IncomingValues.insert(std::make_pair(BB, OldVal)); 00628 return OldVal; 00629 } 00630 00631 IncomingValueMap::const_iterator It = IncomingValues.find(BB); 00632 if (It != IncomingValues.end()) return It->second; 00633 00634 return OldVal; 00635 } 00636 00637 /// \brief Create a map from block to value for the operands of a 00638 /// given phi. 00639 /// 00640 /// Create a map from block to value for each non-undef value flowing 00641 /// into \p PN. 00642 /// 00643 /// \param PN The phi we are collecting the map for. 00644 /// \param IncomingValues [out] The map from block to value for this phi. 00645 static void gatherIncomingValuesToPhi(PHINode *PN, 00646 IncomingValueMap &IncomingValues) { 00647 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 00648 BasicBlock *BB = PN->getIncomingBlock(i); 00649 Value *V = PN->getIncomingValue(i); 00650 00651 if (!isa<UndefValue>(V)) 00652 IncomingValues.insert(std::make_pair(BB, V)); 00653 } 00654 } 00655 00656 /// \brief Replace the incoming undef values to a phi with the values 00657 /// from a block-to-value map. 00658 /// 00659 /// \param PN The phi we are replacing the undefs in. 00660 /// \param IncomingValues A map from block to value. 00661 static void replaceUndefValuesInPhi(PHINode *PN, 00662 const IncomingValueMap &IncomingValues) { 00663 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 00664 Value *V = PN->getIncomingValue(i); 00665 00666 if (!isa<UndefValue>(V)) continue; 00667 00668 BasicBlock *BB = PN->getIncomingBlock(i); 00669 IncomingValueMap::const_iterator It = IncomingValues.find(BB); 00670 if (It == IncomingValues.end()) continue; 00671 00672 PN->setIncomingValue(i, It->second); 00673 } 00674 } 00675 00676 /// \brief Replace a value flowing from a block to a phi with 00677 /// potentially multiple instances of that value flowing from the 00678 /// block's predecessors to the phi. 00679 /// 00680 /// \param BB The block with the value flowing into the phi. 00681 /// \param BBPreds The predecessors of BB. 00682 /// \param PN The phi that we are updating. 00683 static void redirectValuesFromPredecessorsToPhi(BasicBlock *BB, 00684 const PredBlockVector &BBPreds, 00685 PHINode *PN) { 00686 Value *OldVal = PN->removeIncomingValue(BB, false); 00687 assert(OldVal && "No entry in PHI for Pred BB!"); 00688 00689 IncomingValueMap IncomingValues; 00690 00691 // We are merging two blocks - BB, and the block containing PN - and 00692 // as a result we need to redirect edges from the predecessors of BB 00693 // to go to the block containing PN, and update PN 00694 // accordingly. Since we allow merging blocks in the case where the 00695 // predecessor and successor blocks both share some predecessors, 00696 // and where some of those common predecessors might have undef 00697 // values flowing into PN, we want to rewrite those values to be 00698 // consistent with the non-undef values. 00699 00700 gatherIncomingValuesToPhi(PN, IncomingValues); 00701 00702 // If this incoming value is one of the PHI nodes in BB, the new entries 00703 // in the PHI node are the entries from the old PHI. 00704 if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) { 00705 PHINode *OldValPN = cast<PHINode>(OldVal); 00706 for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i) { 00707 // Note that, since we are merging phi nodes and BB and Succ might 00708 // have common predecessors, we could end up with a phi node with 00709 // identical incoming branches. This will be cleaned up later (and 00710 // will trigger asserts if we try to clean it up now, without also 00711 // simplifying the corresponding conditional branch). 00712 BasicBlock *PredBB = OldValPN->getIncomingBlock(i); 00713 Value *PredVal = OldValPN->getIncomingValue(i); 00714 Value *Selected = selectIncomingValueForBlock(PredVal, PredBB, 00715 IncomingValues); 00716 00717 // And add a new incoming value for this predecessor for the 00718 // newly retargeted branch. 00719 PN->addIncoming(Selected, PredBB); 00720 } 00721 } else { 00722 for (unsigned i = 0, e = BBPreds.size(); i != e; ++i) { 00723 // Update existing incoming values in PN for this 00724 // predecessor of BB. 00725 BasicBlock *PredBB = BBPreds[i]; 00726 Value *Selected = selectIncomingValueForBlock(OldVal, PredBB, 00727 IncomingValues); 00728 00729 // And add a new incoming value for this predecessor for the 00730 // newly retargeted branch. 00731 PN->addIncoming(Selected, PredBB); 00732 } 00733 } 00734 00735 replaceUndefValuesInPhi(PN, IncomingValues); 00736 } 00737 00738 /// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an 00739 /// unconditional branch, and contains no instructions other than PHI nodes, 00740 /// potential side-effect free intrinsics and the branch. If possible, 00741 /// eliminate BB by rewriting all the predecessors to branch to the successor 00742 /// block and return true. If we can't transform, return false. 00743 bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB) { 00744 assert(BB != &BB->getParent()->getEntryBlock() && 00745 "TryToSimplifyUncondBranchFromEmptyBlock called on entry block!"); 00746 00747 // We can't eliminate infinite loops. 00748 BasicBlock *Succ = cast<BranchInst>(BB->getTerminator())->getSuccessor(0); 00749 if (BB == Succ) return false; 00750 00751 // Check to see if merging these blocks would cause conflicts for any of the 00752 // phi nodes in BB or Succ. If not, we can safely merge. 00753 if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false; 00754 00755 // Check for cases where Succ has multiple predecessors and a PHI node in BB 00756 // has uses which will not disappear when the PHI nodes are merged. It is 00757 // possible to handle such cases, but difficult: it requires checking whether 00758 // BB dominates Succ, which is non-trivial to calculate in the case where 00759 // Succ has multiple predecessors. Also, it requires checking whether 00760 // constructing the necessary self-referential PHI node doesn't introduce any 00761 // conflicts; this isn't too difficult, but the previous code for doing this 00762 // was incorrect. 00763 // 00764 // Note that if this check finds a live use, BB dominates Succ, so BB is 00765 // something like a loop pre-header (or rarely, a part of an irreducible CFG); 00766 // folding the branch isn't profitable in that case anyway. 00767 if (!Succ->getSinglePredecessor()) { 00768 BasicBlock::iterator BBI = BB->begin(); 00769 while (isa<PHINode>(*BBI)) { 00770 for (Use &U : BBI->uses()) { 00771 if (PHINode* PN = dyn_cast<PHINode>(U.getUser())) { 00772 if (PN->getIncomingBlock(U) != BB) 00773 return false; 00774 } else { 00775 return false; 00776 } 00777 } 00778 ++BBI; 00779 } 00780 } 00781 00782 DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB); 00783 00784 if (isa<PHINode>(Succ->begin())) { 00785 // If there is more than one pred of succ, and there are PHI nodes in 00786 // the successor, then we need to add incoming edges for the PHI nodes 00787 // 00788 const PredBlockVector BBPreds(pred_begin(BB), pred_end(BB)); 00789 00790 // Loop over all of the PHI nodes in the successor of BB. 00791 for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { 00792 PHINode *PN = cast<PHINode>(I); 00793 00794 redirectValuesFromPredecessorsToPhi(BB, BBPreds, PN); 00795 } 00796 } 00797 00798 if (Succ->getSinglePredecessor()) { 00799 // BB is the only predecessor of Succ, so Succ will end up with exactly 00800 // the same predecessors BB had. 00801 00802 // Copy over any phi, debug or lifetime instruction. 00803 BB->getTerminator()->eraseFromParent(); 00804 Succ->getInstList().splice(Succ->getFirstNonPHI(), BB->getInstList()); 00805 } else { 00806 while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) { 00807 // We explicitly check for such uses in CanPropagatePredecessorsForPHIs. 00808 assert(PN->use_empty() && "There shouldn't be any uses here!"); 00809 PN->eraseFromParent(); 00810 } 00811 } 00812 00813 // Everything that jumped to BB now goes to Succ. 00814 BB->replaceAllUsesWith(Succ); 00815 if (!Succ->hasName()) Succ->takeName(BB); 00816 BB->eraseFromParent(); // Delete the old basic block. 00817 return true; 00818 } 00819 00820 /// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI 00821 /// nodes in this block. This doesn't try to be clever about PHI nodes 00822 /// which differ only in the order of the incoming values, but instcombine 00823 /// orders them so it usually won't matter. 00824 /// 00825 bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) { 00826 bool Changed = false; 00827 00828 // This implementation doesn't currently consider undef operands 00829 // specially. Theoretically, two phis which are identical except for 00830 // one having an undef where the other doesn't could be collapsed. 00831 00832 // Map from PHI hash values to PHI nodes. If multiple PHIs have 00833 // the same hash value, the element is the first PHI in the 00834 // linked list in CollisionMap. 00835 DenseMap<uintptr_t, PHINode *> HashMap; 00836 00837 // Maintain linked lists of PHI nodes with common hash values. 00838 DenseMap<PHINode *, PHINode *> CollisionMap; 00839 00840 // Examine each PHI. 00841 for (BasicBlock::iterator I = BB->begin(); 00842 PHINode *PN = dyn_cast<PHINode>(I++); ) { 00843 // Compute a hash value on the operands. Instcombine will likely have sorted 00844 // them, which helps expose duplicates, but we have to check all the 00845 // operands to be safe in case instcombine hasn't run. 00846 uintptr_t Hash = 0; 00847 // This hash algorithm is quite weak as hash functions go, but it seems 00848 // to do a good enough job for this particular purpose, and is very quick. 00849 for (User::op_iterator I = PN->op_begin(), E = PN->op_end(); I != E; ++I) { 00850 Hash ^= reinterpret_cast<uintptr_t>(static_cast<Value *>(*I)); 00851 Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7)); 00852 } 00853 for (PHINode::block_iterator I = PN->block_begin(), E = PN->block_end(); 00854 I != E; ++I) { 00855 Hash ^= reinterpret_cast<uintptr_t>(static_cast<BasicBlock *>(*I)); 00856 Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7)); 00857 } 00858 // Avoid colliding with the DenseMap sentinels ~0 and ~0-1. 00859 Hash >>= 1; 00860 // If we've never seen this hash value before, it's a unique PHI. 00861 std::pair<DenseMap<uintptr_t, PHINode *>::iterator, bool> Pair = 00862 HashMap.insert(std::make_pair(Hash, PN)); 00863 if (Pair.second) continue; 00864 // Otherwise it's either a duplicate or a hash collision. 00865 for (PHINode *OtherPN = Pair.first->second; ; ) { 00866 if (OtherPN->isIdenticalTo(PN)) { 00867 // A duplicate. Replace this PHI with its duplicate. 00868 PN->replaceAllUsesWith(OtherPN); 00869 PN->eraseFromParent(); 00870 Changed = true; 00871 break; 00872 } 00873 // A non-duplicate hash collision. 00874 DenseMap<PHINode *, PHINode *>::iterator I = CollisionMap.find(OtherPN); 00875 if (I == CollisionMap.end()) { 00876 // Set this PHI to be the head of the linked list of colliding PHIs. 00877 PHINode *Old = Pair.first->second; 00878 Pair.first->second = PN; 00879 CollisionMap[PN] = Old; 00880 break; 00881 } 00882 // Proceed to the next PHI in the list. 00883 OtherPN = I->second; 00884 } 00885 } 00886 00887 return Changed; 00888 } 00889 00890 /// enforceKnownAlignment - If the specified pointer points to an object that 00891 /// we control, modify the object's alignment to PrefAlign. This isn't 00892 /// often possible though. If alignment is important, a more reliable approach 00893 /// is to simply align all global variables and allocation instructions to 00894 /// their preferred alignment from the beginning. 00895 /// 00896 static unsigned enforceKnownAlignment(Value *V, unsigned Align, 00897 unsigned PrefAlign, const DataLayout *TD) { 00898 V = V->stripPointerCasts(); 00899 00900 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) { 00901 // If the preferred alignment is greater than the natural stack alignment 00902 // then don't round up. This avoids dynamic stack realignment. 00903 if (TD && TD->exceedsNaturalStackAlignment(PrefAlign)) 00904 return Align; 00905 // If there is a requested alignment and if this is an alloca, round up. 00906 if (AI->getAlignment() >= PrefAlign) 00907 return AI->getAlignment(); 00908 AI->setAlignment(PrefAlign); 00909 return PrefAlign; 00910 } 00911 00912 if (auto *GO = dyn_cast<GlobalObject>(V)) { 00913 // If there is a large requested alignment and we can, bump up the alignment 00914 // of the global. 00915 if (GO->isDeclaration()) 00916 return Align; 00917 // If the memory we set aside for the global may not be the memory used by 00918 // the final program then it is impossible for us to reliably enforce the 00919 // preferred alignment. 00920 if (GO->isWeakForLinker()) 00921 return Align; 00922 00923 if (GO->getAlignment() >= PrefAlign) 00924 return GO->getAlignment(); 00925 // We can only increase the alignment of the global if it has no alignment 00926 // specified or if it is not assigned a section. If it is assigned a 00927 // section, the global could be densely packed with other objects in the 00928 // section, increasing the alignment could cause padding issues. 00929 if (!GO->hasSection() || GO->getAlignment() == 0) 00930 GO->setAlignment(PrefAlign); 00931 return GO->getAlignment(); 00932 } 00933 00934 return Align; 00935 } 00936 00937 /// getOrEnforceKnownAlignment - If the specified pointer has an alignment that 00938 /// we can determine, return it, otherwise return 0. If PrefAlign is specified, 00939 /// and it is more than the alignment of the ultimate object, see if we can 00940 /// increase the alignment of the ultimate object, making this check succeed. 00941 unsigned llvm::getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign, 00942 const DataLayout *DL, 00943 AssumptionTracker *AT, 00944 const Instruction *CxtI, 00945 const DominatorTree *DT) { 00946 assert(V->getType()->isPointerTy() && 00947 "getOrEnforceKnownAlignment expects a pointer!"); 00948 unsigned BitWidth = DL ? DL->getPointerTypeSizeInBits(V->getType()) : 64; 00949 00950 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); 00951 computeKnownBits(V, KnownZero, KnownOne, DL, 0, AT, CxtI, DT); 00952 unsigned TrailZ = KnownZero.countTrailingOnes(); 00953 00954 // Avoid trouble with ridiculously large TrailZ values, such as 00955 // those computed from a null pointer. 00956 TrailZ = std::min(TrailZ, unsigned(sizeof(unsigned) * CHAR_BIT - 1)); 00957 00958 unsigned Align = 1u << std::min(BitWidth - 1, TrailZ); 00959 00960 // LLVM doesn't support alignments larger than this currently. 00961 Align = std::min(Align, +Value::MaximumAlignment); 00962 00963 if (PrefAlign > Align) 00964 Align = enforceKnownAlignment(V, Align, PrefAlign, DL); 00965 00966 // We don't need to make any adjustment. 00967 return Align; 00968 } 00969 00970 ///===---------------------------------------------------------------------===// 00971 /// Dbg Intrinsic utilities 00972 /// 00973 00974 /// See if there is a dbg.value intrinsic for DIVar before I. 00975 static bool LdStHasDebugValue(DIVariable &DIVar, Instruction *I) { 00976 // Since we can't guarantee that the original dbg.declare instrinsic 00977 // is removed by LowerDbgDeclare(), we need to make sure that we are 00978 // not inserting the same dbg.value intrinsic over and over. 00979 llvm::BasicBlock::InstListType::iterator PrevI(I); 00980 if (PrevI != I->getParent()->getInstList().begin()) { 00981 --PrevI; 00982 if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(PrevI)) 00983 if (DVI->getValue() == I->getOperand(0) && 00984 DVI->getOffset() == 0 && 00985 DVI->getVariable() == DIVar) 00986 return true; 00987 } 00988 return false; 00989 } 00990 00991 /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value 00992 /// that has an associated llvm.dbg.decl intrinsic. 00993 bool llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI, 00994 StoreInst *SI, DIBuilder &Builder) { 00995 DIVariable DIVar(DDI->getVariable()); 00996 assert((!DIVar || DIVar.isVariable()) && 00997 "Variable in DbgDeclareInst should be either null or a DIVariable."); 00998 if (!DIVar) 00999 return false; 01000 01001 if (LdStHasDebugValue(DIVar, SI)) 01002 return true; 01003 01004 Instruction *DbgVal = nullptr; 01005 // If an argument is zero extended then use argument directly. The ZExt 01006 // may be zapped by an optimization pass in future. 01007 Argument *ExtendedArg = nullptr; 01008 if (ZExtInst *ZExt = dyn_cast<ZExtInst>(SI->getOperand(0))) 01009 ExtendedArg = dyn_cast<Argument>(ZExt->getOperand(0)); 01010 if (SExtInst *SExt = dyn_cast<SExtInst>(SI->getOperand(0))) 01011 ExtendedArg = dyn_cast<Argument>(SExt->getOperand(0)); 01012 if (ExtendedArg) 01013 DbgVal = Builder.insertDbgValueIntrinsic(ExtendedArg, 0, DIVar, SI); 01014 else 01015 DbgVal = Builder.insertDbgValueIntrinsic(SI->getOperand(0), 0, DIVar, SI); 01016 DbgVal->setDebugLoc(DDI->getDebugLoc()); 01017 return true; 01018 } 01019 01020 /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value 01021 /// that has an associated llvm.dbg.decl intrinsic. 01022 bool llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI, 01023 LoadInst *LI, DIBuilder &Builder) { 01024 DIVariable DIVar(DDI->getVariable()); 01025 assert((!DIVar || DIVar.isVariable()) && 01026 "Variable in DbgDeclareInst should be either null or a DIVariable."); 01027 if (!DIVar) 01028 return false; 01029 01030 if (LdStHasDebugValue(DIVar, LI)) 01031 return true; 01032 01033 Instruction *DbgVal = 01034 Builder.insertDbgValueIntrinsic(LI->getOperand(0), 0, 01035 DIVar, LI); 01036 DbgVal->setDebugLoc(DDI->getDebugLoc()); 01037 return true; 01038 } 01039 01040 /// Determine whether this alloca is either a VLA or an array. 01041 static bool isArray(AllocaInst *AI) { 01042 return AI->isArrayAllocation() || 01043 AI->getType()->getElementType()->isArrayTy(); 01044 } 01045 01046 /// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set 01047 /// of llvm.dbg.value intrinsics. 01048 bool llvm::LowerDbgDeclare(Function &F) { 01049 DIBuilder DIB(*F.getParent()); 01050 SmallVector<DbgDeclareInst *, 4> Dbgs; 01051 for (auto &FI : F) 01052 for (BasicBlock::iterator BI : FI) 01053 if (auto DDI = dyn_cast<DbgDeclareInst>(BI)) 01054 Dbgs.push_back(DDI); 01055 01056 if (Dbgs.empty()) 01057 return false; 01058 01059 for (auto &I : Dbgs) { 01060 DbgDeclareInst *DDI = I; 01061 AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress()); 01062 // If this is an alloca for a scalar variable, insert a dbg.value 01063 // at each load and store to the alloca and erase the dbg.declare. 01064 // The dbg.values allow tracking a variable even if it is not 01065 // stored on the stack, while the dbg.declare can only describe 01066 // the stack slot (and at a lexical-scope granularity). Later 01067 // passes will attempt to elide the stack slot. 01068 if (AI && !isArray(AI)) { 01069 for (User *U : AI->users()) 01070 if (StoreInst *SI = dyn_cast<StoreInst>(U)) 01071 ConvertDebugDeclareToDebugValue(DDI, SI, DIB); 01072 else if (LoadInst *LI = dyn_cast<LoadInst>(U)) 01073 ConvertDebugDeclareToDebugValue(DDI, LI, DIB); 01074 else if (CallInst *CI = dyn_cast<CallInst>(U)) { 01075 // This is a call by-value or some other instruction that 01076 // takes a pointer to the variable. Insert a *value* 01077 // intrinsic that describes the alloca. 01078 auto DbgVal = 01079 DIB.insertDbgValueIntrinsic(AI, 0, 01080 DIVariable(DDI->getVariable()), CI); 01081 DbgVal->setDebugLoc(DDI->getDebugLoc()); 01082 } 01083 DDI->eraseFromParent(); 01084 } 01085 } 01086 return true; 01087 } 01088 01089 /// FindAllocaDbgDeclare - Finds the llvm.dbg.declare intrinsic describing the 01090 /// alloca 'V', if any. 01091 DbgDeclareInst *llvm::FindAllocaDbgDeclare(Value *V) { 01092 if (MDNode *DebugNode = MDNode::getIfExists(V->getContext(), V)) 01093 for (User *U : DebugNode->users()) 01094 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U)) 01095 return DDI; 01096 01097 return nullptr; 01098 } 01099 01100 bool llvm::replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress, 01101 DIBuilder &Builder) { 01102 DbgDeclareInst *DDI = FindAllocaDbgDeclare(AI); 01103 if (!DDI) 01104 return false; 01105 DIVariable DIVar(DDI->getVariable()); 01106 assert((!DIVar || DIVar.isVariable()) && 01107 "Variable in DbgDeclareInst should be either null or a DIVariable."); 01108 if (!DIVar) 01109 return false; 01110 01111 // Create a copy of the original DIDescriptor for user variable, appending 01112 // "deref" operation to a list of address elements, as new llvm.dbg.declare 01113 // will take a value storing address of the memory for variable, not 01114 // alloca itself. 01115 Type *Int64Ty = Type::getInt64Ty(AI->getContext()); 01116 SmallVector<Value*, 4> NewDIVarAddress; 01117 if (DIVar.hasComplexAddress()) { 01118 for (unsigned i = 0, n = DIVar.getNumAddrElements(); i < n; ++i) { 01119 NewDIVarAddress.push_back( 01120 ConstantInt::get(Int64Ty, DIVar.getAddrElement(i))); 01121 } 01122 } 01123 NewDIVarAddress.push_back(ConstantInt::get(Int64Ty, DIBuilder::OpDeref)); 01124 DIVariable NewDIVar = Builder.createComplexVariable( 01125 DIVar.getTag(), DIVar.getContext(), DIVar.getName(), 01126 DIVar.getFile(), DIVar.getLineNumber(), DIVar.getType(), 01127 NewDIVarAddress, DIVar.getArgNumber()); 01128 01129 // Insert llvm.dbg.declare in the same basic block as the original alloca, 01130 // and remove old llvm.dbg.declare. 01131 BasicBlock *BB = AI->getParent(); 01132 Builder.insertDeclare(NewAllocaAddress, NewDIVar, BB); 01133 DDI->eraseFromParent(); 01134 return true; 01135 } 01136 01137 /// changeToUnreachable - Insert an unreachable instruction before the specified 01138 /// instruction, making it and the rest of the code in the block dead. 01139 static void changeToUnreachable(Instruction *I, bool UseLLVMTrap) { 01140 BasicBlock *BB = I->getParent(); 01141 // Loop over all of the successors, removing BB's entry from any PHI 01142 // nodes. 01143 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) 01144 (*SI)->removePredecessor(BB); 01145 01146 // Insert a call to llvm.trap right before this. This turns the undefined 01147 // behavior into a hard fail instead of falling through into random code. 01148 if (UseLLVMTrap) { 01149 Function *TrapFn = 01150 Intrinsic::getDeclaration(BB->getParent()->getParent(), Intrinsic::trap); 01151 CallInst *CallTrap = CallInst::Create(TrapFn, "", I); 01152 CallTrap->setDebugLoc(I->getDebugLoc()); 01153 } 01154 new UnreachableInst(I->getContext(), I); 01155 01156 // All instructions after this are dead. 01157 BasicBlock::iterator BBI = I, BBE = BB->end(); 01158 while (BBI != BBE) { 01159 if (!BBI->use_empty()) 01160 BBI->replaceAllUsesWith(UndefValue::get(BBI->getType())); 01161 BB->getInstList().erase(BBI++); 01162 } 01163 } 01164 01165 /// changeToCall - Convert the specified invoke into a normal call. 01166 static void changeToCall(InvokeInst *II) { 01167 SmallVector<Value*, 8> Args(II->op_begin(), II->op_end() - 3); 01168 CallInst *NewCall = CallInst::Create(II->getCalledValue(), Args, "", II); 01169 NewCall->takeName(II); 01170 NewCall->setCallingConv(II->getCallingConv()); 01171 NewCall->setAttributes(II->getAttributes()); 01172 NewCall->setDebugLoc(II->getDebugLoc()); 01173 II->replaceAllUsesWith(NewCall); 01174 01175 // Follow the call by a branch to the normal destination. 01176 BranchInst::Create(II->getNormalDest(), II); 01177 01178 // Update PHI nodes in the unwind destination 01179 II->getUnwindDest()->removePredecessor(II->getParent()); 01180 II->eraseFromParent(); 01181 } 01182 01183 static bool markAliveBlocks(BasicBlock *BB, 01184 SmallPtrSetImpl<BasicBlock*> &Reachable) { 01185 01186 SmallVector<BasicBlock*, 128> Worklist; 01187 Worklist.push_back(BB); 01188 Reachable.insert(BB); 01189 bool Changed = false; 01190 do { 01191 BB = Worklist.pop_back_val(); 01192 01193 // Do a quick scan of the basic block, turning any obviously unreachable 01194 // instructions into LLVM unreachable insts. The instruction combining pass 01195 // canonicalizes unreachable insts into stores to null or undef. 01196 for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E;++BBI){ 01197 // Assumptions that are known to be false are equivalent to unreachable. 01198 // Also, if the condition is undefined, then we make the choice most 01199 // beneficial to the optimizer, and choose that to also be unreachable. 01200 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BBI)) 01201 if (II->getIntrinsicID() == Intrinsic::assume) { 01202 bool MakeUnreachable = false; 01203 if (isa<UndefValue>(II->getArgOperand(0))) 01204 MakeUnreachable = true; 01205 else if (ConstantInt *Cond = 01206 dyn_cast<ConstantInt>(II->getArgOperand(0))) 01207 MakeUnreachable = Cond->isZero(); 01208 01209 if (MakeUnreachable) { 01210 // Don't insert a call to llvm.trap right before the unreachable. 01211 changeToUnreachable(BBI, false); 01212 Changed = true; 01213 break; 01214 } 01215 } 01216 01217 if (CallInst *CI = dyn_cast<CallInst>(BBI)) { 01218 if (CI->doesNotReturn()) { 01219 // If we found a call to a no-return function, insert an unreachable 01220 // instruction after it. Make sure there isn't *already* one there 01221 // though. 01222 ++BBI; 01223 if (!isa<UnreachableInst>(BBI)) { 01224 // Don't insert a call to llvm.trap right before the unreachable. 01225 changeToUnreachable(BBI, false); 01226 Changed = true; 01227 } 01228 break; 01229 } 01230 } 01231 01232 // Store to undef and store to null are undefined and used to signal that 01233 // they should be changed to unreachable by passes that can't modify the 01234 // CFG. 01235 if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) { 01236 // Don't touch volatile stores. 01237 if (SI->isVolatile()) continue; 01238 01239 Value *Ptr = SI->getOperand(1); 01240 01241 if (isa<UndefValue>(Ptr) || 01242 (isa<ConstantPointerNull>(Ptr) && 01243 SI->getPointerAddressSpace() == 0)) { 01244 changeToUnreachable(SI, true); 01245 Changed = true; 01246 break; 01247 } 01248 } 01249 } 01250 01251 // Turn invokes that call 'nounwind' functions into ordinary calls. 01252 if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) { 01253 Value *Callee = II->getCalledValue(); 01254 if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { 01255 changeToUnreachable(II, true); 01256 Changed = true; 01257 } else if (II->doesNotThrow()) { 01258 if (II->use_empty() && II->onlyReadsMemory()) { 01259 // jump to the normal destination branch. 01260 BranchInst::Create(II->getNormalDest(), II); 01261 II->getUnwindDest()->removePredecessor(II->getParent()); 01262 II->eraseFromParent(); 01263 } else 01264 changeToCall(II); 01265 Changed = true; 01266 } 01267 } 01268 01269 Changed |= ConstantFoldTerminator(BB, true); 01270 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) 01271 if (Reachable.insert(*SI)) 01272 Worklist.push_back(*SI); 01273 } while (!Worklist.empty()); 01274 return Changed; 01275 } 01276 01277 /// removeUnreachableBlocksFromFn - Remove blocks that are not reachable, even 01278 /// if they are in a dead cycle. Return true if a change was made, false 01279 /// otherwise. 01280 bool llvm::removeUnreachableBlocks(Function &F) { 01281 SmallPtrSet<BasicBlock*, 128> Reachable; 01282 bool Changed = markAliveBlocks(F.begin(), Reachable); 01283 01284 // If there are unreachable blocks in the CFG... 01285 if (Reachable.size() == F.size()) 01286 return Changed; 01287 01288 assert(Reachable.size() < F.size()); 01289 NumRemoved += F.size()-Reachable.size(); 01290 01291 // Loop over all of the basic blocks that are not reachable, dropping all of 01292 // their internal references... 01293 for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB) { 01294 if (Reachable.count(BB)) 01295 continue; 01296 01297 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) 01298 if (Reachable.count(*SI)) 01299 (*SI)->removePredecessor(BB); 01300 BB->dropAllReferences(); 01301 } 01302 01303 for (Function::iterator I = ++F.begin(); I != F.end();) 01304 if (!Reachable.count(I)) 01305 I = F.getBasicBlockList().erase(I); 01306 else 01307 ++I; 01308 01309 return true; 01310 } 01311 01312 void llvm::combineMetadata(Instruction *K, const Instruction *J, ArrayRef<unsigned> KnownIDs) { 01313 SmallVector<std::pair<unsigned, MDNode*>, 4> Metadata; 01314 K->dropUnknownMetadata(KnownIDs); 01315 K->getAllMetadataOtherThanDebugLoc(Metadata); 01316 for (unsigned i = 0, n = Metadata.size(); i < n; ++i) { 01317 unsigned Kind = Metadata[i].first; 01318 MDNode *JMD = J->getMetadata(Kind); 01319 MDNode *KMD = Metadata[i].second; 01320 01321 switch (Kind) { 01322 default: 01323 K->setMetadata(Kind, nullptr); // Remove unknown metadata 01324 break; 01325 case LLVMContext::MD_dbg: 01326 llvm_unreachable("getAllMetadataOtherThanDebugLoc returned a MD_dbg"); 01327 case LLVMContext::MD_tbaa: 01328 K->setMetadata(Kind, MDNode::getMostGenericTBAA(JMD, KMD)); 01329 break; 01330 case LLVMContext::MD_alias_scope: 01331 case LLVMContext::MD_noalias: 01332 K->setMetadata(Kind, MDNode::intersect(JMD, KMD)); 01333 break; 01334 case LLVMContext::MD_range: 01335 K->setMetadata(Kind, MDNode::getMostGenericRange(JMD, KMD)); 01336 break; 01337 case LLVMContext::MD_fpmath: 01338 K->setMetadata(Kind, MDNode::getMostGenericFPMath(JMD, KMD)); 01339 break; 01340 case LLVMContext::MD_invariant_load: 01341 // Only set the !invariant.load if it is present in both instructions. 01342 K->setMetadata(Kind, JMD); 01343 break; 01344 } 01345 } 01346 }