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, DominatorTree *DT) {
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 (DT) {
00529     BasicBlock *PredBBIDom = DT->getNode(PredBB)->getIDom()->getBlock();
00530     DT->changeImmediateDominator(DestBB, PredBBIDom);
00531     DT->eraseNode(PredBB);
00532   }
00533   // Nuke BB.
00534   PredBB->eraseFromParent();
00535 }
00536 
00537 /// CanMergeValues - Return true if we can choose one of these values to use
00538 /// in place of the other. Note that we will always choose the non-undef
00539 /// value to keep.
00540 static bool CanMergeValues(Value *First, Value *Second) {
00541   return First == Second || isa<UndefValue>(First) || isa<UndefValue>(Second);
00542 }
00543 
00544 /// CanPropagatePredecessorsForPHIs - Return true if we can fold BB, an
00545 /// almost-empty BB ending in an unconditional branch to Succ, into Succ.
00546 ///
00547 /// Assumption: Succ is the single successor for BB.
00548 ///
00549 static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
00550   assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
00551 
00552   DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into "
00553         << Succ->getName() << "\n");
00554   // Shortcut, if there is only a single predecessor it must be BB and merging
00555   // is always safe
00556   if (Succ->getSinglePredecessor()) return true;
00557 
00558   // Make a list of the predecessors of BB
00559   SmallPtrSet<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB));
00560 
00561   // Look at all the phi nodes in Succ, to see if they present a conflict when
00562   // merging these blocks
00563   for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
00564     PHINode *PN = cast<PHINode>(I);
00565 
00566     // If the incoming value from BB is again a PHINode in
00567     // BB which has the same incoming value for *PI as PN does, we can
00568     // merge the phi nodes and then the blocks can still be merged
00569     PHINode *BBPN = dyn_cast<PHINode>(PN->getIncomingValueForBlock(BB));
00570     if (BBPN && BBPN->getParent() == BB) {
00571       for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) {
00572         BasicBlock *IBB = PN->getIncomingBlock(PI);
00573         if (BBPreds.count(IBB) &&
00574             !CanMergeValues(BBPN->getIncomingValueForBlock(IBB),
00575                             PN->getIncomingValue(PI))) {
00576           DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in "
00577                 << Succ->getName() << " is conflicting with "
00578                 << BBPN->getName() << " with regard to common predecessor "
00579                 << IBB->getName() << "\n");
00580           return false;
00581         }
00582       }
00583     } else {
00584       Value* Val = PN->getIncomingValueForBlock(BB);
00585       for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) {
00586         // See if the incoming value for the common predecessor is equal to the
00587         // one for BB, in which case this phi node will not prevent the merging
00588         // of the block.
00589         BasicBlock *IBB = PN->getIncomingBlock(PI);
00590         if (BBPreds.count(IBB) &&
00591             !CanMergeValues(Val, PN->getIncomingValue(PI))) {
00592           DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in "
00593                 << Succ->getName() << " is conflicting with regard to common "
00594                 << "predecessor " << IBB->getName() << "\n");
00595           return false;
00596         }
00597       }
00598     }
00599   }
00600 
00601   return true;
00602 }
00603 
00604 typedef SmallVector<BasicBlock *, 16> PredBlockVector;
00605 typedef DenseMap<BasicBlock *, Value *> IncomingValueMap;
00606 
00607 /// \brief Determines the value to use as the phi node input for a block.
00608 ///
00609 /// Select between \p OldVal any value that we know flows from \p BB
00610 /// to a particular phi on the basis of which one (if either) is not
00611 /// undef. Update IncomingValues based on the selected value.
00612 ///
00613 /// \param OldVal The value we are considering selecting.
00614 /// \param BB The block that the value flows in from.
00615 /// \param IncomingValues A map from block-to-value for other phi inputs
00616 /// that we have examined.
00617 ///
00618 /// \returns the selected value.
00619 static Value *selectIncomingValueForBlock(Value *OldVal, BasicBlock *BB,
00620                                           IncomingValueMap &IncomingValues) {
00621   if (!isa<UndefValue>(OldVal)) {
00622     assert((!IncomingValues.count(BB) ||
00623             IncomingValues.find(BB)->second == OldVal) &&
00624            "Expected OldVal to match incoming value from BB!");
00625 
00626     IncomingValues.insert(std::make_pair(BB, OldVal));
00627     return OldVal;
00628   }
00629 
00630   IncomingValueMap::const_iterator It = IncomingValues.find(BB);
00631   if (It != IncomingValues.end()) return It->second;
00632 
00633   return OldVal;
00634 }
00635 
00636 /// \brief Create a map from block to value for the operands of a
00637 /// given phi.
00638 ///
00639 /// Create a map from block to value for each non-undef value flowing
00640 /// into \p PN.
00641 ///
00642 /// \param PN The phi we are collecting the map for.
00643 /// \param IncomingValues [out] The map from block to value for this phi.
00644 static void gatherIncomingValuesToPhi(PHINode *PN,
00645                                       IncomingValueMap &IncomingValues) {
00646   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
00647     BasicBlock *BB = PN->getIncomingBlock(i);
00648     Value *V = PN->getIncomingValue(i);
00649 
00650     if (!isa<UndefValue>(V))
00651       IncomingValues.insert(std::make_pair(BB, V));
00652   }
00653 }
00654 
00655 /// \brief Replace the incoming undef values to a phi with the values
00656 /// from a block-to-value map.
00657 ///
00658 /// \param PN The phi we are replacing the undefs in.
00659 /// \param IncomingValues A map from block to value.
00660 static void replaceUndefValuesInPhi(PHINode *PN,
00661                                     const IncomingValueMap &IncomingValues) {
00662   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
00663     Value *V = PN->getIncomingValue(i);
00664 
00665     if (!isa<UndefValue>(V)) continue;
00666 
00667     BasicBlock *BB = PN->getIncomingBlock(i);
00668     IncomingValueMap::const_iterator It = IncomingValues.find(BB);
00669     if (It == IncomingValues.end()) continue;
00670 
00671     PN->setIncomingValue(i, It->second);
00672   }
00673 }
00674 
00675 /// \brief Replace a value flowing from a block to a phi with
00676 /// potentially multiple instances of that value flowing from the
00677 /// block's predecessors to the phi.
00678 ///
00679 /// \param BB The block with the value flowing into the phi.
00680 /// \param BBPreds The predecessors of BB.
00681 /// \param PN The phi that we are updating.
00682 static void redirectValuesFromPredecessorsToPhi(BasicBlock *BB,
00683                                                 const PredBlockVector &BBPreds,
00684                                                 PHINode *PN) {
00685   Value *OldVal = PN->removeIncomingValue(BB, false);
00686   assert(OldVal && "No entry in PHI for Pred BB!");
00687 
00688   IncomingValueMap IncomingValues;
00689 
00690   // We are merging two blocks - BB, and the block containing PN - and
00691   // as a result we need to redirect edges from the predecessors of BB
00692   // to go to the block containing PN, and update PN
00693   // accordingly. Since we allow merging blocks in the case where the
00694   // predecessor and successor blocks both share some predecessors,
00695   // and where some of those common predecessors might have undef
00696   // values flowing into PN, we want to rewrite those values to be
00697   // consistent with the non-undef values.
00698 
00699   gatherIncomingValuesToPhi(PN, IncomingValues);
00700 
00701   // If this incoming value is one of the PHI nodes in BB, the new entries
00702   // in the PHI node are the entries from the old PHI.
00703   if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) {
00704     PHINode *OldValPN = cast<PHINode>(OldVal);
00705     for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i) {
00706       // Note that, since we are merging phi nodes and BB and Succ might
00707       // have common predecessors, we could end up with a phi node with
00708       // identical incoming branches. This will be cleaned up later (and
00709       // will trigger asserts if we try to clean it up now, without also
00710       // simplifying the corresponding conditional branch).
00711       BasicBlock *PredBB = OldValPN->getIncomingBlock(i);
00712       Value *PredVal = OldValPN->getIncomingValue(i);
00713       Value *Selected = selectIncomingValueForBlock(PredVal, PredBB,
00714                                                     IncomingValues);
00715 
00716       // And add a new incoming value for this predecessor for the
00717       // newly retargeted branch.
00718       PN->addIncoming(Selected, PredBB);
00719     }
00720   } else {
00721     for (unsigned i = 0, e = BBPreds.size(); i != e; ++i) {
00722       // Update existing incoming values in PN for this
00723       // predecessor of BB.
00724       BasicBlock *PredBB = BBPreds[i];
00725       Value *Selected = selectIncomingValueForBlock(OldVal, PredBB,
00726                                                     IncomingValues);
00727 
00728       // And add a new incoming value for this predecessor for the
00729       // newly retargeted branch.
00730       PN->addIncoming(Selected, PredBB);
00731     }
00732   }
00733 
00734   replaceUndefValuesInPhi(PN, IncomingValues);
00735 }
00736 
00737 /// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an
00738 /// unconditional branch, and contains no instructions other than PHI nodes,
00739 /// potential side-effect free intrinsics and the branch.  If possible,
00740 /// eliminate BB by rewriting all the predecessors to branch to the successor
00741 /// block and return true.  If we can't transform, return false.
00742 bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB) {
00743   assert(BB != &BB->getParent()->getEntryBlock() &&
00744          "TryToSimplifyUncondBranchFromEmptyBlock called on entry block!");
00745 
00746   // We can't eliminate infinite loops.
00747   BasicBlock *Succ = cast<BranchInst>(BB->getTerminator())->getSuccessor(0);
00748   if (BB == Succ) return false;
00749 
00750   // Check to see if merging these blocks would cause conflicts for any of the
00751   // phi nodes in BB or Succ. If not, we can safely merge.
00752   if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false;
00753 
00754   // Check for cases where Succ has multiple predecessors and a PHI node in BB
00755   // has uses which will not disappear when the PHI nodes are merged.  It is
00756   // possible to handle such cases, but difficult: it requires checking whether
00757   // BB dominates Succ, which is non-trivial to calculate in the case where
00758   // Succ has multiple predecessors.  Also, it requires checking whether
00759   // constructing the necessary self-referential PHI node doesn't introduce any
00760   // conflicts; this isn't too difficult, but the previous code for doing this
00761   // was incorrect.
00762   //
00763   // Note that if this check finds a live use, BB dominates Succ, so BB is
00764   // something like a loop pre-header (or rarely, a part of an irreducible CFG);
00765   // folding the branch isn't profitable in that case anyway.
00766   if (!Succ->getSinglePredecessor()) {
00767     BasicBlock::iterator BBI = BB->begin();
00768     while (isa<PHINode>(*BBI)) {
00769       for (Use &U : BBI->uses()) {
00770         if (PHINode* PN = dyn_cast<PHINode>(U.getUser())) {
00771           if (PN->getIncomingBlock(U) != BB)
00772             return false;
00773         } else {
00774           return false;
00775         }
00776       }
00777       ++BBI;
00778     }
00779   }
00780 
00781   DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB);
00782 
00783   if (isa<PHINode>(Succ->begin())) {
00784     // If there is more than one pred of succ, and there are PHI nodes in
00785     // the successor, then we need to add incoming edges for the PHI nodes
00786     //
00787     const PredBlockVector BBPreds(pred_begin(BB), pred_end(BB));
00788 
00789     // Loop over all of the PHI nodes in the successor of BB.
00790     for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
00791       PHINode *PN = cast<PHINode>(I);
00792 
00793       redirectValuesFromPredecessorsToPhi(BB, BBPreds, PN);
00794     }
00795   }
00796 
00797   if (Succ->getSinglePredecessor()) {
00798     // BB is the only predecessor of Succ, so Succ will end up with exactly
00799     // the same predecessors BB had.
00800 
00801     // Copy over any phi, debug or lifetime instruction.
00802     BB->getTerminator()->eraseFromParent();
00803     Succ->getInstList().splice(Succ->getFirstNonPHI(), BB->getInstList());
00804   } else {
00805     while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
00806       // We explicitly check for such uses in CanPropagatePredecessorsForPHIs.
00807       assert(PN->use_empty() && "There shouldn't be any uses here!");
00808       PN->eraseFromParent();
00809     }
00810   }
00811 
00812   // Everything that jumped to BB now goes to Succ.
00813   BB->replaceAllUsesWith(Succ);
00814   if (!Succ->hasName()) Succ->takeName(BB);
00815   BB->eraseFromParent();              // Delete the old basic block.
00816   return true;
00817 }
00818 
00819 /// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI
00820 /// nodes in this block. This doesn't try to be clever about PHI nodes
00821 /// which differ only in the order of the incoming values, but instcombine
00822 /// orders them so it usually won't matter.
00823 ///
00824 bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) {
00825   bool Changed = false;
00826 
00827   // This implementation doesn't currently consider undef operands
00828   // specially. Theoretically, two phis which are identical except for
00829   // one having an undef where the other doesn't could be collapsed.
00830 
00831   // Map from PHI hash values to PHI nodes. If multiple PHIs have
00832   // the same hash value, the element is the first PHI in the
00833   // linked list in CollisionMap.
00834   DenseMap<uintptr_t, PHINode *> HashMap;
00835 
00836   // Maintain linked lists of PHI nodes with common hash values.
00837   DenseMap<PHINode *, PHINode *> CollisionMap;
00838 
00839   // Examine each PHI.
00840   for (BasicBlock::iterator I = BB->begin();
00841        PHINode *PN = dyn_cast<PHINode>(I++); ) {
00842     // Compute a hash value on the operands. Instcombine will likely have sorted
00843     // them, which helps expose duplicates, but we have to check all the
00844     // operands to be safe in case instcombine hasn't run.
00845     uintptr_t Hash = 0;
00846     // This hash algorithm is quite weak as hash functions go, but it seems
00847     // to do a good enough job for this particular purpose, and is very quick.
00848     for (User::op_iterator I = PN->op_begin(), E = PN->op_end(); I != E; ++I) {
00849       Hash ^= reinterpret_cast<uintptr_t>(static_cast<Value *>(*I));
00850       Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7));
00851     }
00852     for (PHINode::block_iterator I = PN->block_begin(), E = PN->block_end();
00853          I != E; ++I) {
00854       Hash ^= reinterpret_cast<uintptr_t>(static_cast<BasicBlock *>(*I));
00855       Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7));
00856     }
00857     // Avoid colliding with the DenseMap sentinels ~0 and ~0-1.
00858     Hash >>= 1;
00859     // If we've never seen this hash value before, it's a unique PHI.
00860     std::pair<DenseMap<uintptr_t, PHINode *>::iterator, bool> Pair =
00861       HashMap.insert(std::make_pair(Hash, PN));
00862     if (Pair.second) continue;
00863     // Otherwise it's either a duplicate or a hash collision.
00864     for (PHINode *OtherPN = Pair.first->second; ; ) {
00865       if (OtherPN->isIdenticalTo(PN)) {
00866         // A duplicate. Replace this PHI with its duplicate.
00867         PN->replaceAllUsesWith(OtherPN);
00868         PN->eraseFromParent();
00869         Changed = true;
00870         break;
00871       }
00872       // A non-duplicate hash collision.
00873       DenseMap<PHINode *, PHINode *>::iterator I = CollisionMap.find(OtherPN);
00874       if (I == CollisionMap.end()) {
00875         // Set this PHI to be the head of the linked list of colliding PHIs.
00876         PHINode *Old = Pair.first->second;
00877         Pair.first->second = PN;
00878         CollisionMap[PN] = Old;
00879         break;
00880       }
00881       // Proceed to the next PHI in the list.
00882       OtherPN = I->second;
00883     }
00884   }
00885 
00886   return Changed;
00887 }
00888 
00889 /// enforceKnownAlignment - If the specified pointer points to an object that
00890 /// we control, modify the object's alignment to PrefAlign. This isn't
00891 /// often possible though. If alignment is important, a more reliable approach
00892 /// is to simply align all global variables and allocation instructions to
00893 /// their preferred alignment from the beginning.
00894 ///
00895 static unsigned enforceKnownAlignment(Value *V, unsigned Align,
00896                                       unsigned PrefAlign, const DataLayout *TD) {
00897   V = V->stripPointerCasts();
00898 
00899   if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
00900     // If the preferred alignment is greater than the natural stack alignment
00901     // then don't round up. This avoids dynamic stack realignment.
00902     if (TD && TD->exceedsNaturalStackAlignment(PrefAlign))
00903       return Align;
00904     // If there is a requested alignment and if this is an alloca, round up.
00905     if (AI->getAlignment() >= PrefAlign)
00906       return AI->getAlignment();
00907     AI->setAlignment(PrefAlign);
00908     return PrefAlign;
00909   }
00910 
00911   if (auto *GO = dyn_cast<GlobalObject>(V)) {
00912     // If there is a large requested alignment and we can, bump up the alignment
00913     // of the global.
00914     if (GO->isDeclaration())
00915       return Align;
00916     // If the memory we set aside for the global may not be the memory used by
00917     // the final program then it is impossible for us to reliably enforce the
00918     // preferred alignment.
00919     if (GO->isWeakForLinker())
00920       return Align;
00921 
00922     if (GO->getAlignment() >= PrefAlign)
00923       return GO->getAlignment();
00924     // We can only increase the alignment of the global if it has no alignment
00925     // specified or if it is not assigned a section.  If it is assigned a
00926     // section, the global could be densely packed with other objects in the
00927     // section, increasing the alignment could cause padding issues.
00928     if (!GO->hasSection() || GO->getAlignment() == 0)
00929       GO->setAlignment(PrefAlign);
00930     return GO->getAlignment();
00931   }
00932 
00933   return Align;
00934 }
00935 
00936 /// getOrEnforceKnownAlignment - If the specified pointer has an alignment that
00937 /// we can determine, return it, otherwise return 0.  If PrefAlign is specified,
00938 /// and it is more than the alignment of the ultimate object, see if we can
00939 /// increase the alignment of the ultimate object, making this check succeed.
00940 unsigned llvm::getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign,
00941                                           const DataLayout *DL,
00942                                           AssumptionCache *AC,
00943                                           const Instruction *CxtI,
00944                                           const DominatorTree *DT) {
00945   assert(V->getType()->isPointerTy() &&
00946          "getOrEnforceKnownAlignment expects a pointer!");
00947   unsigned BitWidth = DL ? DL->getPointerTypeSizeInBits(V->getType()) : 64;
00948 
00949   APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
00950   computeKnownBits(V, KnownZero, KnownOne, DL, 0, AC, CxtI, DT);
00951   unsigned TrailZ = KnownZero.countTrailingOnes();
00952 
00953   // Avoid trouble with ridiculously large TrailZ values, such as
00954   // those computed from a null pointer.
00955   TrailZ = std::min(TrailZ, unsigned(sizeof(unsigned) * CHAR_BIT - 1));
00956 
00957   unsigned Align = 1u << std::min(BitWidth - 1, TrailZ);
00958 
00959   // LLVM doesn't support alignments larger than this currently.
00960   Align = std::min(Align, +Value::MaximumAlignment);
00961 
00962   if (PrefAlign > Align)
00963     Align = enforceKnownAlignment(V, Align, PrefAlign, DL);
00964 
00965   // We don't need to make any adjustment.
00966   return Align;
00967 }
00968 
00969 ///===---------------------------------------------------------------------===//
00970 ///  Dbg Intrinsic utilities
00971 ///
00972 
00973 /// See if there is a dbg.value intrinsic for DIVar before I.
00974 static bool LdStHasDebugValue(DIVariable &DIVar, Instruction *I) {
00975   // Since we can't guarantee that the original dbg.declare instrinsic
00976   // is removed by LowerDbgDeclare(), we need to make sure that we are
00977   // not inserting the same dbg.value intrinsic over and over.
00978   llvm::BasicBlock::InstListType::iterator PrevI(I);
00979   if (PrevI != I->getParent()->getInstList().begin()) {
00980     --PrevI;
00981     if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(PrevI))
00982       if (DVI->getValue() == I->getOperand(0) &&
00983           DVI->getOffset() == 0 &&
00984           DVI->getVariable() == DIVar)
00985         return true;
00986   }
00987   return false;
00988 }
00989 
00990 /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
00991 /// that has an associated llvm.dbg.decl intrinsic.
00992 bool llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
00993                                            StoreInst *SI, DIBuilder &Builder) {
00994   DIVariable DIVar(DDI->getVariable());
00995   DIExpression DIExpr(DDI->getExpression());
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, DIExpr, SI);
01014   else
01015     DbgVal = Builder.insertDbgValueIntrinsic(SI->getOperand(0), 0, DIVar,
01016                                              DIExpr, SI);
01017   DbgVal->setDebugLoc(DDI->getDebugLoc());
01018   return true;
01019 }
01020 
01021 /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
01022 /// that has an associated llvm.dbg.decl intrinsic.
01023 bool llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
01024                                            LoadInst *LI, DIBuilder &Builder) {
01025   DIVariable DIVar(DDI->getVariable());
01026   DIExpression DIExpr(DDI->getExpression());
01027   assert((!DIVar || DIVar.isVariable()) &&
01028          "Variable in DbgDeclareInst should be either null or a DIVariable.");
01029   if (!DIVar)
01030     return false;
01031 
01032   if (LdStHasDebugValue(DIVar, LI))
01033     return true;
01034 
01035   Instruction *DbgVal =
01036       Builder.insertDbgValueIntrinsic(LI->getOperand(0), 0, DIVar, DIExpr, LI);
01037   DbgVal->setDebugLoc(DDI->getDebugLoc());
01038   return true;
01039 }
01040 
01041 /// Determine whether this alloca is either a VLA or an array.
01042 static bool isArray(AllocaInst *AI) {
01043   return AI->isArrayAllocation() ||
01044     AI->getType()->getElementType()->isArrayTy();
01045 }
01046 
01047 /// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set
01048 /// of llvm.dbg.value intrinsics.
01049 bool llvm::LowerDbgDeclare(Function &F) {
01050   DIBuilder DIB(*F.getParent(), /*AllowUnresolved*/ false);
01051   SmallVector<DbgDeclareInst *, 4> Dbgs;
01052   for (auto &FI : F)
01053     for (BasicBlock::iterator BI : FI)
01054       if (auto DDI = dyn_cast<DbgDeclareInst>(BI))
01055         Dbgs.push_back(DDI);
01056 
01057   if (Dbgs.empty())
01058     return false;
01059 
01060   for (auto &I : Dbgs) {
01061     DbgDeclareInst *DDI = I;
01062     AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress());
01063     // If this is an alloca for a scalar variable, insert a dbg.value
01064     // at each load and store to the alloca and erase the dbg.declare.
01065     // The dbg.values allow tracking a variable even if it is not
01066     // stored on the stack, while the dbg.declare can only describe
01067     // the stack slot (and at a lexical-scope granularity). Later
01068     // passes will attempt to elide the stack slot.
01069     if (AI && !isArray(AI)) {
01070       for (User *U : AI->users())
01071         if (StoreInst *SI = dyn_cast<StoreInst>(U))
01072           ConvertDebugDeclareToDebugValue(DDI, SI, DIB);
01073         else if (LoadInst *LI = dyn_cast<LoadInst>(U))
01074           ConvertDebugDeclareToDebugValue(DDI, LI, DIB);
01075         else if (CallInst *CI = dyn_cast<CallInst>(U)) {
01076           // This is a call by-value or some other instruction that
01077           // takes a pointer to the variable. Insert a *value*
01078           // intrinsic that describes the alloca.
01079           auto DbgVal = DIB.insertDbgValueIntrinsic(
01080               AI, 0, DIVariable(DDI->getVariable()),
01081               DIExpression(DDI->getExpression()), CI);
01082           DbgVal->setDebugLoc(DDI->getDebugLoc());
01083         }
01084       DDI->eraseFromParent();
01085     }
01086   }
01087   return true;
01088 }
01089 
01090 /// FindAllocaDbgDeclare - Finds the llvm.dbg.declare intrinsic describing the
01091 /// alloca 'V', if any.
01092 DbgDeclareInst *llvm::FindAllocaDbgDeclare(Value *V) {
01093   if (auto *L = LocalAsMetadata::getIfExists(V))
01094     if (auto *MDV = MetadataAsValue::getIfExists(V->getContext(), L))
01095       for (User *U : MDV->users())
01096         if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U))
01097           return DDI;
01098 
01099   return nullptr;
01100 }
01101 
01102 bool llvm::replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
01103                                       DIBuilder &Builder) {
01104   DbgDeclareInst *DDI = FindAllocaDbgDeclare(AI);
01105   if (!DDI)
01106     return false;
01107   DIVariable DIVar(DDI->getVariable());
01108   DIExpression DIExpr(DDI->getExpression());
01109   assert((!DIVar || DIVar.isVariable()) &&
01110          "Variable in DbgDeclareInst should be either null or a DIVariable.");
01111   if (!DIVar)
01112     return false;
01113 
01114   // Create a copy of the original DIDescriptor for user variable, prepending
01115   // "deref" operation to a list of address elements, as new llvm.dbg.declare
01116   // will take a value storing address of the memory for variable, not
01117   // alloca itself.
01118   SmallVector<int64_t, 4> NewDIExpr;
01119   NewDIExpr.push_back(dwarf::DW_OP_deref);
01120   if (DIExpr)
01121     for (unsigned i = 0, n = DIExpr.getNumElements(); i < n; ++i)
01122       NewDIExpr.push_back(DIExpr.getElement(i));
01123 
01124   // Insert llvm.dbg.declare in the same basic block as the original alloca,
01125   // and remove old llvm.dbg.declare.
01126   BasicBlock *BB = AI->getParent();
01127   Builder.insertDeclare(NewAllocaAddress, DIVar,
01128                         Builder.createExpression(NewDIExpr), BB);
01129   DDI->eraseFromParent();
01130   return true;
01131 }
01132 
01133 /// changeToUnreachable - Insert an unreachable instruction before the specified
01134 /// instruction, making it and the rest of the code in the block dead.
01135 static void changeToUnreachable(Instruction *I, bool UseLLVMTrap) {
01136   BasicBlock *BB = I->getParent();
01137   // Loop over all of the successors, removing BB's entry from any PHI
01138   // nodes.
01139   for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
01140     (*SI)->removePredecessor(BB);
01141 
01142   // Insert a call to llvm.trap right before this.  This turns the undefined
01143   // behavior into a hard fail instead of falling through into random code.
01144   if (UseLLVMTrap) {
01145     Function *TrapFn =
01146       Intrinsic::getDeclaration(BB->getParent()->getParent(), Intrinsic::trap);
01147     CallInst *CallTrap = CallInst::Create(TrapFn, "", I);
01148     CallTrap->setDebugLoc(I->getDebugLoc());
01149   }
01150   new UnreachableInst(I->getContext(), I);
01151 
01152   // All instructions after this are dead.
01153   BasicBlock::iterator BBI = I, BBE = BB->end();
01154   while (BBI != BBE) {
01155     if (!BBI->use_empty())
01156       BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
01157     BB->getInstList().erase(BBI++);
01158   }
01159 }
01160 
01161 /// changeToCall - Convert the specified invoke into a normal call.
01162 static void changeToCall(InvokeInst *II) {
01163   SmallVector<Value*, 8> Args(II->op_begin(), II->op_end() - 3);
01164   CallInst *NewCall = CallInst::Create(II->getCalledValue(), Args, "", II);
01165   NewCall->takeName(II);
01166   NewCall->setCallingConv(II->getCallingConv());
01167   NewCall->setAttributes(II->getAttributes());
01168   NewCall->setDebugLoc(II->getDebugLoc());
01169   II->replaceAllUsesWith(NewCall);
01170 
01171   // Follow the call by a branch to the normal destination.
01172   BranchInst::Create(II->getNormalDest(), II);
01173 
01174   // Update PHI nodes in the unwind destination
01175   II->getUnwindDest()->removePredecessor(II->getParent());
01176   II->eraseFromParent();
01177 }
01178 
01179 static bool markAliveBlocks(BasicBlock *BB,
01180                             SmallPtrSetImpl<BasicBlock*> &Reachable) {
01181 
01182   SmallVector<BasicBlock*, 128> Worklist;
01183   Worklist.push_back(BB);
01184   Reachable.insert(BB);
01185   bool Changed = false;
01186   do {
01187     BB = Worklist.pop_back_val();
01188 
01189     // Do a quick scan of the basic block, turning any obviously unreachable
01190     // instructions into LLVM unreachable insts.  The instruction combining pass
01191     // canonicalizes unreachable insts into stores to null or undef.
01192     for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E;++BBI){
01193       // Assumptions that are known to be false are equivalent to unreachable.
01194       // Also, if the condition is undefined, then we make the choice most
01195       // beneficial to the optimizer, and choose that to also be unreachable.
01196       if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BBI))
01197         if (II->getIntrinsicID() == Intrinsic::assume) {
01198           bool MakeUnreachable = false;
01199           if (isa<UndefValue>(II->getArgOperand(0)))
01200             MakeUnreachable = true;
01201           else if (ConstantInt *Cond =
01202                    dyn_cast<ConstantInt>(II->getArgOperand(0)))
01203             MakeUnreachable = Cond->isZero();
01204 
01205           if (MakeUnreachable) {
01206             // Don't insert a call to llvm.trap right before the unreachable.
01207             changeToUnreachable(BBI, false);
01208             Changed = true;
01209             break;
01210           }
01211         }
01212 
01213       if (CallInst *CI = dyn_cast<CallInst>(BBI)) {
01214         if (CI->doesNotReturn()) {
01215           // If we found a call to a no-return function, insert an unreachable
01216           // instruction after it.  Make sure there isn't *already* one there
01217           // though.
01218           ++BBI;
01219           if (!isa<UnreachableInst>(BBI)) {
01220             // Don't insert a call to llvm.trap right before the unreachable.
01221             changeToUnreachable(BBI, false);
01222             Changed = true;
01223           }
01224           break;
01225         }
01226       }
01227 
01228       // Store to undef and store to null are undefined and used to signal that
01229       // they should be changed to unreachable by passes that can't modify the
01230       // CFG.
01231       if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
01232         // Don't touch volatile stores.
01233         if (SI->isVolatile()) continue;
01234 
01235         Value *Ptr = SI->getOperand(1);
01236 
01237         if (isa<UndefValue>(Ptr) ||
01238             (isa<ConstantPointerNull>(Ptr) &&
01239              SI->getPointerAddressSpace() == 0)) {
01240           changeToUnreachable(SI, true);
01241           Changed = true;
01242           break;
01243         }
01244       }
01245     }
01246 
01247     // Turn invokes that call 'nounwind' functions into ordinary calls.
01248     if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
01249       Value *Callee = II->getCalledValue();
01250       if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) {
01251         changeToUnreachable(II, true);
01252         Changed = true;
01253       } else if (II->doesNotThrow()) {
01254         if (II->use_empty() && II->onlyReadsMemory()) {
01255           // jump to the normal destination branch.
01256           BranchInst::Create(II->getNormalDest(), II);
01257           II->getUnwindDest()->removePredecessor(II->getParent());
01258           II->eraseFromParent();
01259         } else
01260           changeToCall(II);
01261         Changed = true;
01262       }
01263     }
01264 
01265     Changed |= ConstantFoldTerminator(BB, true);
01266     for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
01267       if (Reachable.insert(*SI).second)
01268         Worklist.push_back(*SI);
01269   } while (!Worklist.empty());
01270   return Changed;
01271 }
01272 
01273 /// removeUnreachableBlocksFromFn - Remove blocks that are not reachable, even
01274 /// if they are in a dead cycle.  Return true if a change was made, false
01275 /// otherwise.
01276 bool llvm::removeUnreachableBlocks(Function &F) {
01277   SmallPtrSet<BasicBlock*, 128> Reachable;
01278   bool Changed = markAliveBlocks(F.begin(), Reachable);
01279 
01280   // If there are unreachable blocks in the CFG...
01281   if (Reachable.size() == F.size())
01282     return Changed;
01283 
01284   assert(Reachable.size() < F.size());
01285   NumRemoved += F.size()-Reachable.size();
01286 
01287   // Loop over all of the basic blocks that are not reachable, dropping all of
01288   // their internal references...
01289   for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB) {
01290     if (Reachable.count(BB))
01291       continue;
01292 
01293     for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
01294       if (Reachable.count(*SI))
01295         (*SI)->removePredecessor(BB);
01296     BB->dropAllReferences();
01297   }
01298 
01299   for (Function::iterator I = ++F.begin(); I != F.end();)
01300     if (!Reachable.count(I))
01301       I = F.getBasicBlockList().erase(I);
01302     else
01303       ++I;
01304 
01305   return true;
01306 }
01307 
01308 void llvm::combineMetadata(Instruction *K, const Instruction *J, ArrayRef<unsigned> KnownIDs) {
01309   SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata;
01310   K->dropUnknownMetadata(KnownIDs);
01311   K->getAllMetadataOtherThanDebugLoc(Metadata);
01312   for (unsigned i = 0, n = Metadata.size(); i < n; ++i) {
01313     unsigned Kind = Metadata[i].first;
01314     MDNode *JMD = J->getMetadata(Kind);
01315     MDNode *KMD = Metadata[i].second;
01316 
01317     switch (Kind) {
01318       default:
01319         K->setMetadata(Kind, nullptr); // Remove unknown metadata
01320         break;
01321       case LLVMContext::MD_dbg:
01322         llvm_unreachable("getAllMetadataOtherThanDebugLoc returned a MD_dbg");
01323       case LLVMContext::MD_tbaa:
01324         K->setMetadata(Kind, MDNode::getMostGenericTBAA(JMD, KMD));
01325         break;
01326       case LLVMContext::MD_alias_scope:
01327       case LLVMContext::MD_noalias:
01328         K->setMetadata(Kind, MDNode::intersect(JMD, KMD));
01329         break;
01330       case LLVMContext::MD_range:
01331         K->setMetadata(Kind, MDNode::getMostGenericRange(JMD, KMD));
01332         break;
01333       case LLVMContext::MD_fpmath:
01334         K->setMetadata(Kind, MDNode::getMostGenericFPMath(JMD, KMD));
01335         break;
01336       case LLVMContext::MD_invariant_load:
01337         // Only set the !invariant.load if it is present in both instructions.
01338         K->setMetadata(Kind, JMD);
01339         break;
01340       case LLVMContext::MD_nonnull:
01341         // Only set the !nonnull if it is present in both instructions.
01342         K->setMetadata(Kind, JMD);
01343         break;
01344     }
01345   }
01346 }