LLVM API Documentation

Local.cpp
Go to the documentation of this file.
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 =
00141                 mdconst::dyn_extract<ConstantInt>(MD->getOperand(MD_i));
00142             assert(CI);
00143             Weights.push_back(CI->getValue().getZExtValue());
00144           }
00145           // Merge weight of this case to the default weight.
00146           unsigned idx = i.getCaseIndex();
00147           Weights[0] += Weights[idx+1];
00148           // Remove weight for this case.
00149           std::swap(Weights[idx+1], Weights.back());
00150           Weights.pop_back();
00151           SI->setMetadata(LLVMContext::MD_prof,
00152                           MDBuilder(BB->getContext()).
00153                           createBranchWeights(Weights));
00154         }
00155         // Remove this entry.
00156         DefaultDest->removePredecessor(SI->getParent());
00157         SI->removeCase(i);
00158         --i; --e;
00159         continue;
00160       }
00161 
00162       // Otherwise, check to see if the switch only branches to one destination.
00163       // We do this by reseting "TheOnlyDest" to null when we find two non-equal
00164       // destinations.
00165       if (i.getCaseSuccessor() != TheOnlyDest) TheOnlyDest = nullptr;
00166     }
00167 
00168     if (CI && !TheOnlyDest) {
00169       // Branching on a constant, but not any of the cases, go to the default
00170       // successor.
00171       TheOnlyDest = SI->getDefaultDest();
00172     }
00173 
00174     // If we found a single destination that we can fold the switch into, do so
00175     // now.
00176     if (TheOnlyDest) {
00177       // Insert the new branch.
00178       Builder.CreateBr(TheOnlyDest);
00179       BasicBlock *BB = SI->getParent();
00180 
00181       // Remove entries from PHI nodes which we no longer branch to...
00182       for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) {
00183         // Found case matching a constant operand?
00184         BasicBlock *Succ = SI->getSuccessor(i);
00185         if (Succ == TheOnlyDest)
00186           TheOnlyDest = nullptr; // Don't modify the first branch to TheOnlyDest
00187         else
00188           Succ->removePredecessor(BB);
00189       }
00190 
00191       // Delete the old switch.
00192       Value *Cond = SI->getCondition();
00193       SI->eraseFromParent();
00194       if (DeleteDeadConditions)
00195         RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI);
00196       return true;
00197     }
00198 
00199     if (SI->getNumCases() == 1) {
00200       // Otherwise, we can fold this switch into a conditional branch
00201       // instruction if it has only one non-default destination.
00202       SwitchInst::CaseIt FirstCase = SI->case_begin();
00203       Value *Cond = Builder.CreateICmpEQ(SI->getCondition(),
00204           FirstCase.getCaseValue(), "cond");
00205 
00206       // Insert the new branch.
00207       BranchInst *NewBr = Builder.CreateCondBr(Cond,
00208                                                FirstCase.getCaseSuccessor(),
00209                                                SI->getDefaultDest());
00210       MDNode *MD = SI->getMetadata(LLVMContext::MD_prof);
00211       if (MD && MD->getNumOperands() == 3) {
00212         ConstantInt *SICase =
00213             mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
00214         ConstantInt *SIDef =
00215             mdconst::dyn_extract<ConstantInt>(MD->getOperand(1));
00216         assert(SICase && SIDef);
00217         // The TrueWeight should be the weight for the single case of SI.
00218         NewBr->setMetadata(LLVMContext::MD_prof,
00219                         MDBuilder(BB->getContext()).
00220                         createBranchWeights(SICase->getValue().getZExtValue(),
00221                                             SIDef->getValue().getZExtValue()));
00222       }
00223 
00224       // Delete the old switch.
00225       SI->eraseFromParent();
00226       return true;
00227     }
00228     return false;
00229   }
00230 
00231   if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(T)) {
00232     // indirectbr blockaddress(@F, @BB) -> br label @BB
00233     if (BlockAddress *BA =
00234           dyn_cast<BlockAddress>(IBI->getAddress()->stripPointerCasts())) {
00235       BasicBlock *TheOnlyDest = BA->getBasicBlock();
00236       // Insert the new branch.
00237       Builder.CreateBr(TheOnlyDest);
00238 
00239       for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
00240         if (IBI->getDestination(i) == TheOnlyDest)
00241           TheOnlyDest = nullptr;
00242         else
00243           IBI->getDestination(i)->removePredecessor(IBI->getParent());
00244       }
00245       Value *Address = IBI->getAddress();
00246       IBI->eraseFromParent();
00247       if (DeleteDeadConditions)
00248         RecursivelyDeleteTriviallyDeadInstructions(Address, TLI);
00249 
00250       // If we didn't find our destination in the IBI successor list, then we
00251       // have undefined behavior.  Replace the unconditional branch with an
00252       // 'unreachable' instruction.
00253       if (TheOnlyDest) {
00254         BB->getTerminator()->eraseFromParent();
00255         new UnreachableInst(BB->getContext(), BB);
00256       }
00257 
00258       return true;
00259     }
00260   }
00261 
00262   return false;
00263 }
00264 
00265 
00266 //===----------------------------------------------------------------------===//
00267 //  Local dead code elimination.
00268 //
00269 
00270 /// isInstructionTriviallyDead - Return true if the result produced by the
00271 /// instruction is not used, and the instruction has no side effects.
00272 ///
00273 bool llvm::isInstructionTriviallyDead(Instruction *I,
00274                                       const TargetLibraryInfo *TLI) {
00275   if (!I->use_empty() || isa<TerminatorInst>(I)) return false;
00276 
00277   // We don't want the landingpad instruction removed by anything this general.
00278   if (isa<LandingPadInst>(I))
00279     return false;
00280 
00281   // We don't want debug info removed by anything this general, unless
00282   // debug info is empty.
00283   if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
00284     if (DDI->getAddress())
00285       return false;
00286     return true;
00287   }
00288   if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
00289     if (DVI->getValue())
00290       return false;
00291     return true;
00292   }
00293 
00294   if (!I->mayHaveSideEffects()) return true;
00295 
00296   // Special case intrinsics that "may have side effects" but can be deleted
00297   // when dead.
00298   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
00299     // Safe to delete llvm.stacksave if dead.
00300     if (II->getIntrinsicID() == Intrinsic::stacksave)
00301       return true;
00302 
00303     // Lifetime intrinsics are dead when their right-hand is undef.
00304     if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
00305         II->getIntrinsicID() == Intrinsic::lifetime_end)
00306       return isa<UndefValue>(II->getArgOperand(1));
00307 
00308     // Assumptions are dead if their condition is trivially true.
00309     if (II->getIntrinsicID() == Intrinsic::assume) {
00310       if (ConstantInt *Cond = dyn_cast<ConstantInt>(II->getArgOperand(0)))
00311         return !Cond->isZero();
00312 
00313       return false;
00314     }
00315   }
00316 
00317   if (isAllocLikeFn(I, TLI)) return true;
00318 
00319   if (CallInst *CI = isFreeCall(I, TLI))
00320     if (Constant *C = dyn_cast<Constant>(CI->getArgOperand(0)))
00321       return C->isNullValue() || isa<UndefValue>(C);
00322 
00323   return false;
00324 }
00325 
00326 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
00327 /// trivially dead instruction, delete it.  If that makes any of its operands
00328 /// trivially dead, delete them too, recursively.  Return true if any
00329 /// instructions were deleted.
00330 bool
00331 llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V,
00332                                                  const TargetLibraryInfo *TLI) {
00333   Instruction *I = dyn_cast<Instruction>(V);
00334   if (!I || !I->use_empty() || !isInstructionTriviallyDead(I, TLI))
00335     return false;
00336 
00337   SmallVector<Instruction*, 16> DeadInsts;
00338   DeadInsts.push_back(I);
00339 
00340   do {
00341     I = DeadInsts.pop_back_val();
00342 
00343     // Null out all of the instruction's operands to see if any operand becomes
00344     // dead as we go.
00345     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
00346       Value *OpV = I->getOperand(i);
00347       I->setOperand(i, nullptr);
00348 
00349       if (!OpV->use_empty()) continue;
00350 
00351       // If the operand is an instruction that became dead as we nulled out the
00352       // operand, and if it is 'trivially' dead, delete it in a future loop
00353       // iteration.
00354       if (Instruction *OpI = dyn_cast<Instruction>(OpV))
00355         if (isInstructionTriviallyDead(OpI, TLI))
00356           DeadInsts.push_back(OpI);
00357     }
00358 
00359     I->eraseFromParent();
00360   } while (!DeadInsts.empty());
00361 
00362   return true;
00363 }
00364 
00365 /// areAllUsesEqual - Check whether the uses of a value are all the same.
00366 /// This is similar to Instruction::hasOneUse() except this will also return
00367 /// true when there are no uses or multiple uses that all refer to the same
00368 /// value.
00369 static bool areAllUsesEqual(Instruction *I) {
00370   Value::user_iterator UI = I->user_begin();
00371   Value::user_iterator UE = I->user_end();
00372   if (UI == UE)
00373     return true;
00374 
00375   User *TheUse = *UI;
00376   for (++UI; UI != UE; ++UI) {
00377     if (*UI != TheUse)
00378       return false;
00379   }
00380   return true;
00381 }
00382 
00383 /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
00384 /// dead PHI node, due to being a def-use chain of single-use nodes that
00385 /// either forms a cycle or is terminated by a trivially dead instruction,
00386 /// delete it.  If that makes any of its operands trivially dead, delete them
00387 /// too, recursively.  Return true if a change was made.
00388 bool llvm::RecursivelyDeleteDeadPHINode(PHINode *PN,
00389                                         const TargetLibraryInfo *TLI) {
00390   SmallPtrSet<Instruction*, 4> Visited;
00391   for (Instruction *I = PN; areAllUsesEqual(I) && !I->mayHaveSideEffects();
00392        I = cast<Instruction>(*I->user_begin())) {
00393     if (I->use_empty())
00394       return RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
00395 
00396     // If we find an instruction more than once, we're on a cycle that
00397     // won't prove fruitful.
00398     if (!Visited.insert(I).second) {
00399       // Break the cycle and delete the instruction and its operands.
00400       I->replaceAllUsesWith(UndefValue::get(I->getType()));
00401       (void)RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
00402       return true;
00403     }
00404   }
00405   return false;
00406 }
00407 
00408 /// SimplifyInstructionsInBlock - Scan the specified basic block and try to
00409 /// simplify any instructions in it and recursively delete dead instructions.
00410 ///
00411 /// This returns true if it changed the code, note that it can delete
00412 /// instructions in other blocks as well in this block.
00413 bool llvm::SimplifyInstructionsInBlock(BasicBlock *BB, const DataLayout *TD,
00414                                        const TargetLibraryInfo *TLI) {
00415   bool MadeChange = false;
00416 
00417 #ifndef NDEBUG
00418   // In debug builds, ensure that the terminator of the block is never replaced
00419   // or deleted by these simplifications. The idea of simplification is that it
00420   // cannot introduce new instructions, and there is no way to replace the
00421   // terminator of a block without introducing a new instruction.
00422   AssertingVH<Instruction> TerminatorVH(--BB->end());
00423 #endif
00424 
00425   for (BasicBlock::iterator BI = BB->begin(), E = --BB->end(); BI != E; ) {
00426     assert(!BI->isTerminator());
00427     Instruction *Inst = BI++;
00428 
00429     WeakVH BIHandle(BI);
00430     if (recursivelySimplifyInstruction(Inst, TD, TLI)) {
00431       MadeChange = true;
00432       if (BIHandle != BI)
00433         BI = BB->begin();
00434       continue;
00435     }
00436 
00437     MadeChange |= RecursivelyDeleteTriviallyDeadInstructions(Inst, TLI);
00438     if (BIHandle != BI)
00439       BI = BB->begin();
00440   }
00441   return MadeChange;
00442 }
00443 
00444 //===----------------------------------------------------------------------===//
00445 //  Control Flow Graph Restructuring.
00446 //
00447 
00448 
00449 /// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this
00450 /// method is called when we're about to delete Pred as a predecessor of BB.  If
00451 /// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred.
00452 ///
00453 /// Unlike the removePredecessor method, this attempts to simplify uses of PHI
00454 /// nodes that collapse into identity values.  For example, if we have:
00455 ///   x = phi(1, 0, 0, 0)
00456 ///   y = and x, z
00457 ///
00458 /// .. and delete the predecessor corresponding to the '1', this will attempt to
00459 /// recursively fold the and to 0.
00460 void llvm::RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
00461                                         DataLayout *TD) {
00462   // This only adjusts blocks with PHI nodes.
00463   if (!isa<PHINode>(BB->begin()))
00464     return;
00465 
00466   // Remove the entries for Pred from the PHI nodes in BB, but do not simplify
00467   // them down.  This will leave us with single entry phi nodes and other phis
00468   // that can be removed.
00469   BB->removePredecessor(Pred, true);
00470 
00471   WeakVH PhiIt = &BB->front();
00472   while (PHINode *PN = dyn_cast<PHINode>(PhiIt)) {
00473     PhiIt = &*++BasicBlock::iterator(cast<Instruction>(PhiIt));
00474     Value *OldPhiIt = PhiIt;
00475 
00476     if (!recursivelySimplifyInstruction(PN, TD))
00477       continue;
00478 
00479     // If recursive simplification ended up deleting the next PHI node we would
00480     // iterate to, then our iterator is invalid, restart scanning from the top
00481     // of the block.
00482     if (PhiIt != OldPhiIt) PhiIt = &BB->front();
00483   }
00484 }
00485 
00486 
00487 /// MergeBasicBlockIntoOnlyPred - DestBB is a block with one predecessor and its
00488 /// predecessor is known to have one successor (DestBB!).  Eliminate the edge
00489 /// between them, moving the instructions in the predecessor into DestBB and
00490 /// deleting the predecessor block.
00491 ///
00492 void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, Pass *P) {
00493   // If BB has single-entry PHI nodes, fold them.
00494   while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
00495     Value *NewVal = PN->getIncomingValue(0);
00496     // Replace self referencing PHI with undef, it must be dead.
00497     if (NewVal == PN) NewVal = UndefValue::get(PN->getType());
00498     PN->replaceAllUsesWith(NewVal);
00499     PN->eraseFromParent();
00500   }
00501 
00502   BasicBlock *PredBB = DestBB->getSinglePredecessor();
00503   assert(PredBB && "Block doesn't have a single predecessor!");
00504 
00505   // Zap anything that took the address of DestBB.  Not doing this will give the
00506   // address an invalid value.
00507   if (DestBB->hasAddressTaken()) {
00508     BlockAddress *BA = BlockAddress::get(DestBB);
00509     Constant *Replacement =
00510       ConstantInt::get(llvm::Type::getInt32Ty(BA->getContext()), 1);
00511     BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement,
00512                                                      BA->getType()));
00513     BA->destroyConstant();
00514   }
00515 
00516   // Anything that branched to PredBB now branches to DestBB.
00517   PredBB->replaceAllUsesWith(DestBB);
00518 
00519   // Splice all the instructions from PredBB to DestBB.
00520   PredBB->getTerminator()->eraseFromParent();
00521   DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList());
00522 
00523   // If the PredBB is the entry block of the function, move DestBB up to
00524   // become the entry block after we erase PredBB.
00525   if (PredBB == &DestBB->getParent()->getEntryBlock())
00526     DestBB->moveAfter(PredBB);
00527 
00528   if (P) {
00529     if (DominatorTreeWrapperPass *DTWP =
00530             P->getAnalysisIfAvailable<DominatorTreeWrapperPass>()) {
00531       DominatorTree &DT = DTWP->getDomTree();
00532       BasicBlock *PredBBIDom = DT.getNode(PredBB)->getIDom()->getBlock();
00533       DT.changeImmediateDominator(DestBB, PredBBIDom);
00534       DT.eraseNode(PredBB);
00535     }
00536   }
00537   // Nuke BB.
00538   PredBB->eraseFromParent();
00539 }
00540 
00541 /// CanMergeValues - Return true if we can choose one of these values to use
00542 /// in place of the other. Note that we will always choose the non-undef
00543 /// value to keep.
00544 static bool CanMergeValues(Value *First, Value *Second) {
00545   return First == Second || isa<UndefValue>(First) || isa<UndefValue>(Second);
00546 }
00547 
00548 /// CanPropagatePredecessorsForPHIs - Return true if we can fold BB, an
00549 /// almost-empty BB ending in an unconditional branch to Succ, into Succ.
00550 ///
00551 /// Assumption: Succ is the single successor for BB.
00552 ///
00553 static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
00554   assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
00555 
00556   DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into "
00557         << Succ->getName() << "\n");
00558   // Shortcut, if there is only a single predecessor it must be BB and merging
00559   // is always safe
00560   if (Succ->getSinglePredecessor()) return true;
00561 
00562   // Make a list of the predecessors of BB
00563   SmallPtrSet<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB));
00564 
00565   // Look at all the phi nodes in Succ, to see if they present a conflict when
00566   // merging these blocks
00567   for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
00568     PHINode *PN = cast<PHINode>(I);
00569 
00570     // If the incoming value from BB is again a PHINode in
00571     // BB which has the same incoming value for *PI as PN does, we can
00572     // merge the phi nodes and then the blocks can still be merged
00573     PHINode *BBPN = dyn_cast<PHINode>(PN->getIncomingValueForBlock(BB));
00574     if (BBPN && BBPN->getParent() == BB) {
00575       for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) {
00576         BasicBlock *IBB = PN->getIncomingBlock(PI);
00577         if (BBPreds.count(IBB) &&
00578             !CanMergeValues(BBPN->getIncomingValueForBlock(IBB),
00579                             PN->getIncomingValue(PI))) {
00580           DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in "
00581                 << Succ->getName() << " is conflicting with "
00582                 << BBPN->getName() << " with regard to common predecessor "
00583                 << IBB->getName() << "\n");
00584           return false;
00585         }
00586       }
00587     } else {
00588       Value* Val = PN->getIncomingValueForBlock(BB);
00589       for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) {
00590         // See if the incoming value for the common predecessor is equal to the
00591         // one for BB, in which case this phi node will not prevent the merging
00592         // of the block.
00593         BasicBlock *IBB = PN->getIncomingBlock(PI);
00594         if (BBPreds.count(IBB) &&
00595             !CanMergeValues(Val, PN->getIncomingValue(PI))) {
00596           DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in "
00597                 << Succ->getName() << " is conflicting with regard to common "
00598                 << "predecessor " << IBB->getName() << "\n");
00599           return false;
00600         }
00601       }
00602     }
00603   }
00604 
00605   return true;
00606 }
00607 
00608 typedef SmallVector<BasicBlock *, 16> PredBlockVector;
00609 typedef DenseMap<BasicBlock *, Value *> IncomingValueMap;
00610 
00611 /// \brief Determines the value to use as the phi node input for a block.
00612 ///
00613 /// Select between \p OldVal any value that we know flows from \p BB
00614 /// to a particular phi on the basis of which one (if either) is not
00615 /// undef. Update IncomingValues based on the selected value.
00616 ///
00617 /// \param OldVal The value we are considering selecting.
00618 /// \param BB The block that the value flows in from.
00619 /// \param IncomingValues A map from block-to-value for other phi inputs
00620 /// that we have examined.
00621 ///
00622 /// \returns the selected value.
00623 static Value *selectIncomingValueForBlock(Value *OldVal, BasicBlock *BB,
00624                                           IncomingValueMap &IncomingValues) {
00625   if (!isa<UndefValue>(OldVal)) {
00626     assert((!IncomingValues.count(BB) ||
00627             IncomingValues.find(BB)->second == OldVal) &&
00628            "Expected OldVal to match incoming value from BB!");
00629 
00630     IncomingValues.insert(std::make_pair(BB, OldVal));
00631     return OldVal;
00632   }
00633 
00634   IncomingValueMap::const_iterator It = IncomingValues.find(BB);
00635   if (It != IncomingValues.end()) return It->second;
00636 
00637   return OldVal;
00638 }
00639 
00640 /// \brief Create a map from block to value for the operands of a
00641 /// given phi.
00642 ///
00643 /// Create a map from block to value for each non-undef value flowing
00644 /// into \p PN.
00645 ///
00646 /// \param PN The phi we are collecting the map for.
00647 /// \param IncomingValues [out] The map from block to value for this phi.
00648 static void gatherIncomingValuesToPhi(PHINode *PN,
00649                                       IncomingValueMap &IncomingValues) {
00650   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
00651     BasicBlock *BB = PN->getIncomingBlock(i);
00652     Value *V = PN->getIncomingValue(i);
00653 
00654     if (!isa<UndefValue>(V))
00655       IncomingValues.insert(std::make_pair(BB, V));
00656   }
00657 }
00658 
00659 /// \brief Replace the incoming undef values to a phi with the values
00660 /// from a block-to-value map.
00661 ///
00662 /// \param PN The phi we are replacing the undefs in.
00663 /// \param IncomingValues A map from block to value.
00664 static void replaceUndefValuesInPhi(PHINode *PN,
00665                                     const IncomingValueMap &IncomingValues) {
00666   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
00667     Value *V = PN->getIncomingValue(i);
00668 
00669     if (!isa<UndefValue>(V)) continue;
00670 
00671     BasicBlock *BB = PN->getIncomingBlock(i);
00672     IncomingValueMap::const_iterator It = IncomingValues.find(BB);
00673     if (It == IncomingValues.end()) continue;
00674 
00675     PN->setIncomingValue(i, It->second);
00676   }
00677 }
00678 
00679 /// \brief Replace a value flowing from a block to a phi with
00680 /// potentially multiple instances of that value flowing from the
00681 /// block's predecessors to the phi.
00682 ///
00683 /// \param BB The block with the value flowing into the phi.
00684 /// \param BBPreds The predecessors of BB.
00685 /// \param PN The phi that we are updating.
00686 static void redirectValuesFromPredecessorsToPhi(BasicBlock *BB,
00687                                                 const PredBlockVector &BBPreds,
00688                                                 PHINode *PN) {
00689   Value *OldVal = PN->removeIncomingValue(BB, false);
00690   assert(OldVal && "No entry in PHI for Pred BB!");
00691 
00692   IncomingValueMap IncomingValues;
00693 
00694   // We are merging two blocks - BB, and the block containing PN - and
00695   // as a result we need to redirect edges from the predecessors of BB
00696   // to go to the block containing PN, and update PN
00697   // accordingly. Since we allow merging blocks in the case where the
00698   // predecessor and successor blocks both share some predecessors,
00699   // and where some of those common predecessors might have undef
00700   // values flowing into PN, we want to rewrite those values to be
00701   // consistent with the non-undef values.
00702 
00703   gatherIncomingValuesToPhi(PN, IncomingValues);
00704 
00705   // If this incoming value is one of the PHI nodes in BB, the new entries
00706   // in the PHI node are the entries from the old PHI.
00707   if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) {
00708     PHINode *OldValPN = cast<PHINode>(OldVal);
00709     for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i) {
00710       // Note that, since we are merging phi nodes and BB and Succ might
00711       // have common predecessors, we could end up with a phi node with
00712       // identical incoming branches. This will be cleaned up later (and
00713       // will trigger asserts if we try to clean it up now, without also
00714       // simplifying the corresponding conditional branch).
00715       BasicBlock *PredBB = OldValPN->getIncomingBlock(i);
00716       Value *PredVal = OldValPN->getIncomingValue(i);
00717       Value *Selected = selectIncomingValueForBlock(PredVal, PredBB,
00718                                                     IncomingValues);
00719 
00720       // And add a new incoming value for this predecessor for the
00721       // newly retargeted branch.
00722       PN->addIncoming(Selected, PredBB);
00723     }
00724   } else {
00725     for (unsigned i = 0, e = BBPreds.size(); i != e; ++i) {
00726       // Update existing incoming values in PN for this
00727       // predecessor of BB.
00728       BasicBlock *PredBB = BBPreds[i];
00729       Value *Selected = selectIncomingValueForBlock(OldVal, PredBB,
00730                                                     IncomingValues);
00731 
00732       // And add a new incoming value for this predecessor for the
00733       // newly retargeted branch.
00734       PN->addIncoming(Selected, PredBB);
00735     }
00736   }
00737 
00738   replaceUndefValuesInPhi(PN, IncomingValues);
00739 }
00740 
00741 /// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an
00742 /// unconditional branch, and contains no instructions other than PHI nodes,
00743 /// potential side-effect free intrinsics and the branch.  If possible,
00744 /// eliminate BB by rewriting all the predecessors to branch to the successor
00745 /// block and return true.  If we can't transform, return false.
00746 bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB) {
00747   assert(BB != &BB->getParent()->getEntryBlock() &&
00748          "TryToSimplifyUncondBranchFromEmptyBlock called on entry block!");
00749 
00750   // We can't eliminate infinite loops.
00751   BasicBlock *Succ = cast<BranchInst>(BB->getTerminator())->getSuccessor(0);
00752   if (BB == Succ) return false;
00753 
00754   // Check to see if merging these blocks would cause conflicts for any of the
00755   // phi nodes in BB or Succ. If not, we can safely merge.
00756   if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false;
00757 
00758   // Check for cases where Succ has multiple predecessors and a PHI node in BB
00759   // has uses which will not disappear when the PHI nodes are merged.  It is
00760   // possible to handle such cases, but difficult: it requires checking whether
00761   // BB dominates Succ, which is non-trivial to calculate in the case where
00762   // Succ has multiple predecessors.  Also, it requires checking whether
00763   // constructing the necessary self-referential PHI node doesn't introduce any
00764   // conflicts; this isn't too difficult, but the previous code for doing this
00765   // was incorrect.
00766   //
00767   // Note that if this check finds a live use, BB dominates Succ, so BB is
00768   // something like a loop pre-header (or rarely, a part of an irreducible CFG);
00769   // folding the branch isn't profitable in that case anyway.
00770   if (!Succ->getSinglePredecessor()) {
00771     BasicBlock::iterator BBI = BB->begin();
00772     while (isa<PHINode>(*BBI)) {
00773       for (Use &U : BBI->uses()) {
00774         if (PHINode* PN = dyn_cast<PHINode>(U.getUser())) {
00775           if (PN->getIncomingBlock(U) != BB)
00776             return false;
00777         } else {
00778           return false;
00779         }
00780       }
00781       ++BBI;
00782     }
00783   }
00784 
00785   DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB);
00786 
00787   if (isa<PHINode>(Succ->begin())) {
00788     // If there is more than one pred of succ, and there are PHI nodes in
00789     // the successor, then we need to add incoming edges for the PHI nodes
00790     //
00791     const PredBlockVector BBPreds(pred_begin(BB), pred_end(BB));
00792 
00793     // Loop over all of the PHI nodes in the successor of BB.
00794     for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
00795       PHINode *PN = cast<PHINode>(I);
00796 
00797       redirectValuesFromPredecessorsToPhi(BB, BBPreds, PN);
00798     }
00799   }
00800 
00801   if (Succ->getSinglePredecessor()) {
00802     // BB is the only predecessor of Succ, so Succ will end up with exactly
00803     // the same predecessors BB had.
00804 
00805     // Copy over any phi, debug or lifetime instruction.
00806     BB->getTerminator()->eraseFromParent();
00807     Succ->getInstList().splice(Succ->getFirstNonPHI(), BB->getInstList());
00808   } else {
00809     while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
00810       // We explicitly check for such uses in CanPropagatePredecessorsForPHIs.
00811       assert(PN->use_empty() && "There shouldn't be any uses here!");
00812       PN->eraseFromParent();
00813     }
00814   }
00815 
00816   // Everything that jumped to BB now goes to Succ.
00817   BB->replaceAllUsesWith(Succ);
00818   if (!Succ->hasName()) Succ->takeName(BB);
00819   BB->eraseFromParent();              // Delete the old basic block.
00820   return true;
00821 }
00822 
00823 /// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI
00824 /// nodes in this block. This doesn't try to be clever about PHI nodes
00825 /// which differ only in the order of the incoming values, but instcombine
00826 /// orders them so it usually won't matter.
00827 ///
00828 bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) {
00829   bool Changed = false;
00830 
00831   // This implementation doesn't currently consider undef operands
00832   // specially. Theoretically, two phis which are identical except for
00833   // one having an undef where the other doesn't could be collapsed.
00834 
00835   // Map from PHI hash values to PHI nodes. If multiple PHIs have
00836   // the same hash value, the element is the first PHI in the
00837   // linked list in CollisionMap.
00838   DenseMap<uintptr_t, PHINode *> HashMap;
00839 
00840   // Maintain linked lists of PHI nodes with common hash values.
00841   DenseMap<PHINode *, PHINode *> CollisionMap;
00842 
00843   // Examine each PHI.
00844   for (BasicBlock::iterator I = BB->begin();
00845        PHINode *PN = dyn_cast<PHINode>(I++); ) {
00846     // Compute a hash value on the operands. Instcombine will likely have sorted
00847     // them, which helps expose duplicates, but we have to check all the
00848     // operands to be safe in case instcombine hasn't run.
00849     uintptr_t Hash = 0;
00850     // This hash algorithm is quite weak as hash functions go, but it seems
00851     // to do a good enough job for this particular purpose, and is very quick.
00852     for (User::op_iterator I = PN->op_begin(), E = PN->op_end(); I != E; ++I) {
00853       Hash ^= reinterpret_cast<uintptr_t>(static_cast<Value *>(*I));
00854       Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7));
00855     }
00856     for (PHINode::block_iterator I = PN->block_begin(), E = PN->block_end();
00857          I != E; ++I) {
00858       Hash ^= reinterpret_cast<uintptr_t>(static_cast<BasicBlock *>(*I));
00859       Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7));
00860     }
00861     // Avoid colliding with the DenseMap sentinels ~0 and ~0-1.
00862     Hash >>= 1;
00863     // If we've never seen this hash value before, it's a unique PHI.
00864     std::pair<DenseMap<uintptr_t, PHINode *>::iterator, bool> Pair =
00865       HashMap.insert(std::make_pair(Hash, PN));
00866     if (Pair.second) continue;
00867     // Otherwise it's either a duplicate or a hash collision.
00868     for (PHINode *OtherPN = Pair.first->second; ; ) {
00869       if (OtherPN->isIdenticalTo(PN)) {
00870         // A duplicate. Replace this PHI with its duplicate.
00871         PN->replaceAllUsesWith(OtherPN);
00872         PN->eraseFromParent();
00873         Changed = true;
00874         break;
00875       }
00876       // A non-duplicate hash collision.
00877       DenseMap<PHINode *, PHINode *>::iterator I = CollisionMap.find(OtherPN);
00878       if (I == CollisionMap.end()) {
00879         // Set this PHI to be the head of the linked list of colliding PHIs.
00880         PHINode *Old = Pair.first->second;
00881         Pair.first->second = PN;
00882         CollisionMap[PN] = Old;
00883         break;
00884       }
00885       // Proceed to the next PHI in the list.
00886       OtherPN = I->second;
00887     }
00888   }
00889 
00890   return Changed;
00891 }
00892 
00893 /// enforceKnownAlignment - If the specified pointer points to an object that
00894 /// we control, modify the object's alignment to PrefAlign. This isn't
00895 /// often possible though. If alignment is important, a more reliable approach
00896 /// is to simply align all global variables and allocation instructions to
00897 /// their preferred alignment from the beginning.
00898 ///
00899 static unsigned enforceKnownAlignment(Value *V, unsigned Align,
00900                                       unsigned PrefAlign, const DataLayout *TD) {
00901   V = V->stripPointerCasts();
00902 
00903   if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
00904     // If the preferred alignment is greater than the natural stack alignment
00905     // then don't round up. This avoids dynamic stack realignment.
00906     if (TD && TD->exceedsNaturalStackAlignment(PrefAlign))
00907       return Align;
00908     // If there is a requested alignment and if this is an alloca, round up.
00909     if (AI->getAlignment() >= PrefAlign)
00910       return AI->getAlignment();
00911     AI->setAlignment(PrefAlign);
00912     return PrefAlign;
00913   }
00914 
00915   if (auto *GO = dyn_cast<GlobalObject>(V)) {
00916     // If there is a large requested alignment and we can, bump up the alignment
00917     // of the global.
00918     if (GO->isDeclaration())
00919       return Align;
00920     // If the memory we set aside for the global may not be the memory used by
00921     // the final program then it is impossible for us to reliably enforce the
00922     // preferred alignment.
00923     if (GO->isWeakForLinker())
00924       return Align;
00925 
00926     if (GO->getAlignment() >= PrefAlign)
00927       return GO->getAlignment();
00928     // We can only increase the alignment of the global if it has no alignment
00929     // specified or if it is not assigned a section.  If it is assigned a
00930     // section, the global could be densely packed with other objects in the
00931     // section, increasing the alignment could cause padding issues.
00932     if (!GO->hasSection() || GO->getAlignment() == 0)
00933       GO->setAlignment(PrefAlign);
00934     return GO->getAlignment();
00935   }
00936 
00937   return Align;
00938 }
00939 
00940 /// getOrEnforceKnownAlignment - If the specified pointer has an alignment that
00941 /// we can determine, return it, otherwise return 0.  If PrefAlign is specified,
00942 /// and it is more than the alignment of the ultimate object, see if we can
00943 /// increase the alignment of the ultimate object, making this check succeed.
00944 unsigned llvm::getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign,
00945                                           const DataLayout *DL,
00946                                           AssumptionTracker *AT,
00947                                           const Instruction *CxtI,
00948                                           const DominatorTree *DT) {
00949   assert(V->getType()->isPointerTy() &&
00950          "getOrEnforceKnownAlignment expects a pointer!");
00951   unsigned BitWidth = DL ? DL->getPointerTypeSizeInBits(V->getType()) : 64;
00952 
00953   APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
00954   computeKnownBits(V, KnownZero, KnownOne, DL, 0, AT, CxtI, DT);
00955   unsigned TrailZ = KnownZero.countTrailingOnes();
00956 
00957   // Avoid trouble with ridiculously large TrailZ values, such as
00958   // those computed from a null pointer.
00959   TrailZ = std::min(TrailZ, unsigned(sizeof(unsigned) * CHAR_BIT - 1));
00960 
00961   unsigned Align = 1u << std::min(BitWidth - 1, TrailZ);
00962 
00963   // LLVM doesn't support alignments larger than this currently.
00964   Align = std::min(Align, +Value::MaximumAlignment);
00965 
00966   if (PrefAlign > Align)
00967     Align = enforceKnownAlignment(V, Align, PrefAlign, DL);
00968 
00969   // We don't need to make any adjustment.
00970   return Align;
00971 }
00972 
00973 ///===---------------------------------------------------------------------===//
00974 ///  Dbg Intrinsic utilities
00975 ///
00976 
00977 /// See if there is a dbg.value intrinsic for DIVar before I.
00978 static bool LdStHasDebugValue(DIVariable &DIVar, Instruction *I) {
00979   // Since we can't guarantee that the original dbg.declare instrinsic
00980   // is removed by LowerDbgDeclare(), we need to make sure that we are
00981   // not inserting the same dbg.value intrinsic over and over.
00982   llvm::BasicBlock::InstListType::iterator PrevI(I);
00983   if (PrevI != I->getParent()->getInstList().begin()) {
00984     --PrevI;
00985     if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(PrevI))
00986       if (DVI->getValue() == I->getOperand(0) &&
00987           DVI->getOffset() == 0 &&
00988           DVI->getVariable() == DIVar)
00989         return true;
00990   }
00991   return false;
00992 }
00993 
00994 /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
00995 /// that has an associated llvm.dbg.decl intrinsic.
00996 bool llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
00997                                            StoreInst *SI, DIBuilder &Builder) {
00998   DIVariable DIVar(DDI->getVariable());
00999   DIExpression DIExpr(DDI->getExpression());
01000   assert((!DIVar || DIVar.isVariable()) &&
01001          "Variable in DbgDeclareInst should be either null or a DIVariable.");
01002   if (!DIVar)
01003     return false;
01004 
01005   if (LdStHasDebugValue(DIVar, SI))
01006     return true;
01007 
01008   Instruction *DbgVal = nullptr;
01009   // If an argument is zero extended then use argument directly. The ZExt
01010   // may be zapped by an optimization pass in future.
01011   Argument *ExtendedArg = nullptr;
01012   if (ZExtInst *ZExt = dyn_cast<ZExtInst>(SI->getOperand(0)))
01013     ExtendedArg = dyn_cast<Argument>(ZExt->getOperand(0));
01014   if (SExtInst *SExt = dyn_cast<SExtInst>(SI->getOperand(0)))
01015     ExtendedArg = dyn_cast<Argument>(SExt->getOperand(0));
01016   if (ExtendedArg)
01017     DbgVal = Builder.insertDbgValueIntrinsic(ExtendedArg, 0, DIVar, DIExpr, SI);
01018   else
01019     DbgVal = Builder.insertDbgValueIntrinsic(SI->getOperand(0), 0, DIVar,
01020                                              DIExpr, SI);
01021   DbgVal->setDebugLoc(DDI->getDebugLoc());
01022   return true;
01023 }
01024 
01025 /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
01026 /// that has an associated llvm.dbg.decl intrinsic.
01027 bool llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
01028                                            LoadInst *LI, DIBuilder &Builder) {
01029   DIVariable DIVar(DDI->getVariable());
01030   DIExpression DIExpr(DDI->getExpression());
01031   assert((!DIVar || DIVar.isVariable()) &&
01032          "Variable in DbgDeclareInst should be either null or a DIVariable.");
01033   if (!DIVar)
01034     return false;
01035 
01036   if (LdStHasDebugValue(DIVar, LI))
01037     return true;
01038 
01039   Instruction *DbgVal =
01040       Builder.insertDbgValueIntrinsic(LI->getOperand(0), 0, DIVar, DIExpr, LI);
01041   DbgVal->setDebugLoc(DDI->getDebugLoc());
01042   return true;
01043 }
01044 
01045 /// Determine whether this alloca is either a VLA or an array.
01046 static bool isArray(AllocaInst *AI) {
01047   return AI->isArrayAllocation() ||
01048     AI->getType()->getElementType()->isArrayTy();
01049 }
01050 
01051 /// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set
01052 /// of llvm.dbg.value intrinsics.
01053 bool llvm::LowerDbgDeclare(Function &F) {
01054   DIBuilder DIB(*F.getParent(), /*AllowUnresolved*/ false);
01055   SmallVector<DbgDeclareInst *, 4> Dbgs;
01056   for (auto &FI : F)
01057     for (BasicBlock::iterator BI : FI)
01058       if (auto DDI = dyn_cast<DbgDeclareInst>(BI))
01059         Dbgs.push_back(DDI);
01060 
01061   if (Dbgs.empty())
01062     return false;
01063 
01064   for (auto &I : Dbgs) {
01065     DbgDeclareInst *DDI = I;
01066     AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress());
01067     // If this is an alloca for a scalar variable, insert a dbg.value
01068     // at each load and store to the alloca and erase the dbg.declare.
01069     // The dbg.values allow tracking a variable even if it is not
01070     // stored on the stack, while the dbg.declare can only describe
01071     // the stack slot (and at a lexical-scope granularity). Later
01072     // passes will attempt to elide the stack slot.
01073     if (AI && !isArray(AI)) {
01074       for (User *U : AI->users())
01075         if (StoreInst *SI = dyn_cast<StoreInst>(U))
01076           ConvertDebugDeclareToDebugValue(DDI, SI, DIB);
01077         else if (LoadInst *LI = dyn_cast<LoadInst>(U))
01078           ConvertDebugDeclareToDebugValue(DDI, LI, DIB);
01079         else if (CallInst *CI = dyn_cast<CallInst>(U)) {
01080           // This is a call by-value or some other instruction that
01081           // takes a pointer to the variable. Insert a *value*
01082           // intrinsic that describes the alloca.
01083           auto DbgVal = DIB.insertDbgValueIntrinsic(
01084               AI, 0, DIVariable(DDI->getVariable()),
01085               DIExpression(DDI->getExpression()), CI);
01086           DbgVal->setDebugLoc(DDI->getDebugLoc());
01087         }
01088       DDI->eraseFromParent();
01089     }
01090   }
01091   return true;
01092 }
01093 
01094 /// FindAllocaDbgDeclare - Finds the llvm.dbg.declare intrinsic describing the
01095 /// alloca 'V', if any.
01096 DbgDeclareInst *llvm::FindAllocaDbgDeclare(Value *V) {
01097   if (auto *L = LocalAsMetadata::getIfExists(V))
01098     if (auto *MDV = MetadataAsValue::getIfExists(V->getContext(), L))
01099       for (User *U : MDV->users())
01100         if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U))
01101           return DDI;
01102 
01103   return nullptr;
01104 }
01105 
01106 bool llvm::replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
01107                                       DIBuilder &Builder) {
01108   DbgDeclareInst *DDI = FindAllocaDbgDeclare(AI);
01109   if (!DDI)
01110     return false;
01111   DIVariable DIVar(DDI->getVariable());
01112   DIExpression DIExpr(DDI->getExpression());
01113   assert((!DIVar || DIVar.isVariable()) &&
01114          "Variable in DbgDeclareInst should be either null or a DIVariable.");
01115   if (!DIVar)
01116     return false;
01117 
01118   // Create a copy of the original DIDescriptor for user variable, prepending
01119   // "deref" operation to a list of address elements, as new llvm.dbg.declare
01120   // will take a value storing address of the memory for variable, not
01121   // alloca itself.
01122   SmallVector<int64_t, 4> NewDIExpr;
01123   NewDIExpr.push_back(dwarf::DW_OP_deref);
01124   if (DIExpr)
01125     for (unsigned i = 0, n = DIExpr.getNumElements(); i < n; ++i)
01126       NewDIExpr.push_back(DIExpr.getElement(i));
01127 
01128   // Insert llvm.dbg.declare in the same basic block as the original alloca,
01129   // and remove old llvm.dbg.declare.
01130   BasicBlock *BB = AI->getParent();
01131   Builder.insertDeclare(NewAllocaAddress, DIVar,
01132                         Builder.createExpression(NewDIExpr), 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).second)
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       case LLVMContext::MD_nonnull:
01345         // Only set the !nonnull if it is present in both instructions.
01346         K->setMetadata(Kind, JMD);
01347         break;
01348     }
01349   }
01350 }