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/Analysis/Dominators.h"
00020 #include "llvm/Analysis/InstructionSimplify.h"
00021 #include "llvm/Analysis/MemoryBuiltins.h"
00022 #include "llvm/Analysis/ProfileInfo.h"
00023 #include "llvm/Analysis/ValueTracking.h"
00024 #include "llvm/DIBuilder.h"
00025 #include "llvm/DebugInfo.h"
00026 #include "llvm/IR/Constants.h"
00027 #include "llvm/IR/DataLayout.h"
00028 #include "llvm/IR/DerivedTypes.h"
00029 #include "llvm/IR/GlobalAlias.h"
00030 #include "llvm/IR/GlobalVariable.h"
00031 #include "llvm/IR/IRBuilder.h"
00032 #include "llvm/IR/Instructions.h"
00033 #include "llvm/IR/IntrinsicInst.h"
00034 #include "llvm/IR/Intrinsics.h"
00035 #include "llvm/IR/MDBuilder.h"
00036 #include "llvm/IR/Metadata.h"
00037 #include "llvm/IR/Operator.h"
00038 #include "llvm/Support/CFG.h"
00039 #include "llvm/Support/Debug.h"
00040 #include "llvm/Support/GetElementPtrTypeIterator.h"
00041 #include "llvm/Support/MathExtras.h"
00042 #include "llvm/Support/ValueHandle.h"
00043 #include "llvm/Support/raw_ostream.h"
00044 using namespace llvm;
00045 
00046 //===----------------------------------------------------------------------===//
00047 //  Local constant propagation.
00048 //
00049 
00050 /// ConstantFoldTerminator - If a terminator instruction is predicated on a
00051 /// constant value, convert it into an unconditional branch to the constant
00052 /// destination.  This is a nontrivial operation because the successors of this
00053 /// basic block must have their PHI nodes updated.
00054 /// Also calls RecursivelyDeleteTriviallyDeadInstructions() on any branch/switch
00055 /// conditions and indirectbr addresses this might make dead if
00056 /// DeleteDeadConditions is true.
00057 bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions,
00058                                   const TargetLibraryInfo *TLI) {
00059   TerminatorInst *T = BB->getTerminator();
00060   IRBuilder<> Builder(T);
00061 
00062   // Branch - See if we are conditional jumping on constant
00063   if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
00064     if (BI->isUnconditional()) return false;  // Can't optimize uncond branch
00065     BasicBlock *Dest1 = BI->getSuccessor(0);
00066     BasicBlock *Dest2 = BI->getSuccessor(1);
00067 
00068     if (ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition())) {
00069       // Are we branching on constant?
00070       // YES.  Change to unconditional branch...
00071       BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2;
00072       BasicBlock *OldDest     = Cond->getZExtValue() ? Dest2 : Dest1;
00073 
00074       //cerr << "Function: " << T->getParent()->getParent()
00075       //     << "\nRemoving branch from " << T->getParent()
00076       //     << "\n\nTo: " << OldDest << endl;
00077 
00078       // Let the basic block know that we are letting go of it.  Based on this,
00079       // it will adjust it's PHI nodes.
00080       OldDest->removePredecessor(BB);
00081 
00082       // Replace the conditional branch with an unconditional one.
00083       Builder.CreateBr(Destination);
00084       BI->eraseFromParent();
00085       return true;
00086     }
00087     
00088     if (Dest2 == Dest1) {       // Conditional branch to same location?
00089       // This branch matches something like this:
00090       //     br bool %cond, label %Dest, label %Dest
00091       // and changes it into:  br label %Dest
00092 
00093       // Let the basic block know that we are letting go of one copy of it.
00094       assert(BI->getParent() && "Terminator not inserted in block!");
00095       Dest1->removePredecessor(BI->getParent());
00096 
00097       // Replace the conditional branch with an unconditional one.
00098       Builder.CreateBr(Dest1);
00099       Value *Cond = BI->getCondition();
00100       BI->eraseFromParent();
00101       if (DeleteDeadConditions)
00102         RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI);
00103       return true;
00104     }
00105     return false;
00106   }
00107   
00108   if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) {
00109     // If we are switching on a constant, we can convert the switch into a
00110     // single branch instruction!
00111     ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition());
00112     BasicBlock *TheOnlyDest = SI->getDefaultDest();
00113     BasicBlock *DefaultDest = TheOnlyDest;
00114 
00115     // Figure out which case it goes to.
00116     for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
00117          i != e; ++i) {
00118       // Found case matching a constant operand?
00119       if (i.getCaseValue() == CI) {
00120         TheOnlyDest = i.getCaseSuccessor();
00121         break;
00122       }
00123 
00124       // Check to see if this branch is going to the same place as the default
00125       // dest.  If so, eliminate it as an explicit compare.
00126       if (i.getCaseSuccessor() == DefaultDest) {
00127         MDNode* MD = SI->getMetadata(LLVMContext::MD_prof);
00128         // MD should have 2 + NumCases operands.
00129         if (MD && MD->getNumOperands() == 2 + SI->getNumCases()) {
00130           // Collect branch weights into a vector.
00131           SmallVector<uint32_t, 8> Weights;
00132           for (unsigned MD_i = 1, MD_e = MD->getNumOperands(); MD_i < MD_e;
00133                ++MD_i) {
00134             ConstantInt* CI = dyn_cast<ConstantInt>(MD->getOperand(MD_i));
00135             assert(CI);
00136             Weights.push_back(CI->getValue().getZExtValue());
00137           }
00138           // Merge weight of this case to the default weight.
00139           unsigned idx = i.getCaseIndex();
00140           Weights[0] += Weights[idx+1];
00141           // Remove weight for this case.
00142           std::swap(Weights[idx+1], Weights.back());
00143           Weights.pop_back();
00144           SI->setMetadata(LLVMContext::MD_prof,
00145                           MDBuilder(BB->getContext()).
00146                           createBranchWeights(Weights));
00147         }
00148         // Remove this entry.
00149         DefaultDest->removePredecessor(SI->getParent());
00150         SI->removeCase(i);
00151         --i; --e;
00152         continue;
00153       }
00154 
00155       // Otherwise, check to see if the switch only branches to one destination.
00156       // We do this by reseting "TheOnlyDest" to null when we find two non-equal
00157       // destinations.
00158       if (i.getCaseSuccessor() != TheOnlyDest) TheOnlyDest = 0;
00159     }
00160 
00161     if (CI && !TheOnlyDest) {
00162       // Branching on a constant, but not any of the cases, go to the default
00163       // successor.
00164       TheOnlyDest = SI->getDefaultDest();
00165     }
00166 
00167     // If we found a single destination that we can fold the switch into, do so
00168     // now.
00169     if (TheOnlyDest) {
00170       // Insert the new branch.
00171       Builder.CreateBr(TheOnlyDest);
00172       BasicBlock *BB = SI->getParent();
00173 
00174       // Remove entries from PHI nodes which we no longer branch to...
00175       for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) {
00176         // Found case matching a constant operand?
00177         BasicBlock *Succ = SI->getSuccessor(i);
00178         if (Succ == TheOnlyDest)
00179           TheOnlyDest = 0;  // Don't modify the first branch to TheOnlyDest
00180         else
00181           Succ->removePredecessor(BB);
00182       }
00183 
00184       // Delete the old switch.
00185       Value *Cond = SI->getCondition();
00186       SI->eraseFromParent();
00187       if (DeleteDeadConditions)
00188         RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI);
00189       return true;
00190     }
00191     
00192     if (SI->getNumCases() == 1) {
00193       // Otherwise, we can fold this switch into a conditional branch
00194       // instruction if it has only one non-default destination.
00195       SwitchInst::CaseIt FirstCase = SI->case_begin();
00196       IntegersSubset& Case = FirstCase.getCaseValueEx();
00197       if (Case.isSingleNumber()) {
00198         // FIXME: Currently work with ConstantInt based numbers.
00199         Value *Cond = Builder.CreateICmpEQ(SI->getCondition(),
00200              Case.getSingleNumber(0).toConstantInt(),
00201             "cond");
00202 
00203         // Insert the new branch.
00204         BranchInst *NewBr = Builder.CreateCondBr(Cond,
00205                                 FirstCase.getCaseSuccessor(),
00206                                 SI->getDefaultDest());
00207         MDNode* MD = SI->getMetadata(LLVMContext::MD_prof);
00208         if (MD && MD->getNumOperands() == 3) {
00209           ConstantInt *SICase = dyn_cast<ConstantInt>(MD->getOperand(2));
00210           ConstantInt *SIDef = dyn_cast<ConstantInt>(MD->getOperand(1));
00211           assert(SICase && SIDef);
00212           // The TrueWeight should be the weight for the single case of SI.
00213           NewBr->setMetadata(LLVMContext::MD_prof,
00214                  MDBuilder(BB->getContext()).
00215                  createBranchWeights(SICase->getValue().getZExtValue(),
00216                                      SIDef->getValue().getZExtValue()));
00217         }
00218 
00219         // Delete the old switch.
00220         SI->eraseFromParent();
00221         return true;
00222       }
00223     }
00224     return false;
00225   }
00226 
00227   if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(T)) {
00228     // indirectbr blockaddress(@F, @BB) -> br label @BB
00229     if (BlockAddress *BA =
00230           dyn_cast<BlockAddress>(IBI->getAddress()->stripPointerCasts())) {
00231       BasicBlock *TheOnlyDest = BA->getBasicBlock();
00232       // Insert the new branch.
00233       Builder.CreateBr(TheOnlyDest);
00234       
00235       for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
00236         if (IBI->getDestination(i) == TheOnlyDest)
00237           TheOnlyDest = 0;
00238         else
00239           IBI->getDestination(i)->removePredecessor(IBI->getParent());
00240       }
00241       Value *Address = IBI->getAddress();
00242       IBI->eraseFromParent();
00243       if (DeleteDeadConditions)
00244         RecursivelyDeleteTriviallyDeadInstructions(Address, TLI);
00245       
00246       // If we didn't find our destination in the IBI successor list, then we
00247       // have undefined behavior.  Replace the unconditional branch with an
00248       // 'unreachable' instruction.
00249       if (TheOnlyDest) {
00250         BB->getTerminator()->eraseFromParent();
00251         new UnreachableInst(BB->getContext(), BB);
00252       }
00253       
00254       return true;
00255     }
00256   }
00257   
00258   return false;
00259 }
00260 
00261 
00262 //===----------------------------------------------------------------------===//
00263 //  Local dead code elimination.
00264 //
00265 
00266 /// isInstructionTriviallyDead - Return true if the result produced by the
00267 /// instruction is not used, and the instruction has no side effects.
00268 ///
00269 bool llvm::isInstructionTriviallyDead(Instruction *I,
00270                                       const TargetLibraryInfo *TLI) {
00271   if (!I->use_empty() || isa<TerminatorInst>(I)) return false;
00272 
00273   // We don't want the landingpad instruction removed by anything this general.
00274   if (isa<LandingPadInst>(I))
00275     return false;
00276 
00277   // We don't want debug info removed by anything this general, unless
00278   // debug info is empty.
00279   if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
00280     if (DDI->getAddress())
00281       return false;
00282     return true;
00283   }
00284   if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
00285     if (DVI->getValue())
00286       return false;
00287     return true;
00288   }
00289 
00290   if (!I->mayHaveSideEffects()) return true;
00291 
00292   // Special case intrinsics that "may have side effects" but can be deleted
00293   // when dead.
00294   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
00295     // Safe to delete llvm.stacksave if dead.
00296     if (II->getIntrinsicID() == Intrinsic::stacksave)
00297       return true;
00298 
00299     // Lifetime intrinsics are dead when their right-hand is undef.
00300     if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
00301         II->getIntrinsicID() == Intrinsic::lifetime_end)
00302       return isa<UndefValue>(II->getArgOperand(1));
00303   }
00304 
00305   if (isAllocLikeFn(I, TLI)) return true;
00306 
00307   if (CallInst *CI = isFreeCall(I, TLI))
00308     if (Constant *C = dyn_cast<Constant>(CI->getArgOperand(0)))
00309       return C->isNullValue() || isa<UndefValue>(C);
00310 
00311   return false;
00312 }
00313 
00314 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
00315 /// trivially dead instruction, delete it.  If that makes any of its operands
00316 /// trivially dead, delete them too, recursively.  Return true if any
00317 /// instructions were deleted.
00318 bool
00319 llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V,
00320                                                  const TargetLibraryInfo *TLI) {
00321   Instruction *I = dyn_cast<Instruction>(V);
00322   if (!I || !I->use_empty() || !isInstructionTriviallyDead(I, TLI))
00323     return false;
00324   
00325   SmallVector<Instruction*, 16> DeadInsts;
00326   DeadInsts.push_back(I);
00327   
00328   do {
00329     I = DeadInsts.pop_back_val();
00330 
00331     // Null out all of the instruction's operands to see if any operand becomes
00332     // dead as we go.
00333     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
00334       Value *OpV = I->getOperand(i);
00335       I->setOperand(i, 0);
00336       
00337       if (!OpV->use_empty()) continue;
00338     
00339       // If the operand is an instruction that became dead as we nulled out the
00340       // operand, and if it is 'trivially' dead, delete it in a future loop
00341       // iteration.
00342       if (Instruction *OpI = dyn_cast<Instruction>(OpV))
00343         if (isInstructionTriviallyDead(OpI, TLI))
00344           DeadInsts.push_back(OpI);
00345     }
00346     
00347     I->eraseFromParent();
00348   } while (!DeadInsts.empty());
00349 
00350   return true;
00351 }
00352 
00353 /// areAllUsesEqual - Check whether the uses of a value are all the same.
00354 /// This is similar to Instruction::hasOneUse() except this will also return
00355 /// true when there are no uses or multiple uses that all refer to the same
00356 /// value.
00357 static bool areAllUsesEqual(Instruction *I) {
00358   Value::use_iterator UI = I->use_begin();
00359   Value::use_iterator UE = I->use_end();
00360   if (UI == UE)
00361     return true;
00362 
00363   User *TheUse = *UI;
00364   for (++UI; UI != UE; ++UI) {
00365     if (*UI != TheUse)
00366       return false;
00367   }
00368   return true;
00369 }
00370 
00371 /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
00372 /// dead PHI node, due to being a def-use chain of single-use nodes that
00373 /// either forms a cycle or is terminated by a trivially dead instruction,
00374 /// delete it.  If that makes any of its operands trivially dead, delete them
00375 /// too, recursively.  Return true if a change was made.
00376 bool llvm::RecursivelyDeleteDeadPHINode(PHINode *PN,
00377                                         const TargetLibraryInfo *TLI) {
00378   SmallPtrSet<Instruction*, 4> Visited;
00379   for (Instruction *I = PN; areAllUsesEqual(I) && !I->mayHaveSideEffects();
00380        I = cast<Instruction>(*I->use_begin())) {
00381     if (I->use_empty())
00382       return RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
00383 
00384     // If we find an instruction more than once, we're on a cycle that
00385     // won't prove fruitful.
00386     if (!Visited.insert(I)) {
00387       // Break the cycle and delete the instruction and its operands.
00388       I->replaceAllUsesWith(UndefValue::get(I->getType()));
00389       (void)RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
00390       return true;
00391     }
00392   }
00393   return false;
00394 }
00395 
00396 /// SimplifyInstructionsInBlock - Scan the specified basic block and try to
00397 /// simplify any instructions in it and recursively delete dead instructions.
00398 ///
00399 /// This returns true if it changed the code, note that it can delete
00400 /// instructions in other blocks as well in this block.
00401 bool llvm::SimplifyInstructionsInBlock(BasicBlock *BB, const DataLayout *TD,
00402                                        const TargetLibraryInfo *TLI) {
00403   bool MadeChange = false;
00404 
00405 #ifndef NDEBUG
00406   // In debug builds, ensure that the terminator of the block is never replaced
00407   // or deleted by these simplifications. The idea of simplification is that it
00408   // cannot introduce new instructions, and there is no way to replace the
00409   // terminator of a block without introducing a new instruction.
00410   AssertingVH<Instruction> TerminatorVH(--BB->end());
00411 #endif
00412 
00413   for (BasicBlock::iterator BI = BB->begin(), E = --BB->end(); BI != E; ) {
00414     assert(!BI->isTerminator());
00415     Instruction *Inst = BI++;
00416 
00417     WeakVH BIHandle(BI);
00418     if (recursivelySimplifyInstruction(Inst, TD)) {
00419       MadeChange = true;
00420       if (BIHandle != BI)
00421         BI = BB->begin();
00422       continue;
00423     }
00424 
00425     MadeChange |= RecursivelyDeleteTriviallyDeadInstructions(Inst, TLI);
00426     if (BIHandle != BI)
00427       BI = BB->begin();
00428   }
00429   return MadeChange;
00430 }
00431 
00432 //===----------------------------------------------------------------------===//
00433 //  Control Flow Graph Restructuring.
00434 //
00435 
00436 
00437 /// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this
00438 /// method is called when we're about to delete Pred as a predecessor of BB.  If
00439 /// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred.
00440 ///
00441 /// Unlike the removePredecessor method, this attempts to simplify uses of PHI
00442 /// nodes that collapse into identity values.  For example, if we have:
00443 ///   x = phi(1, 0, 0, 0)
00444 ///   y = and x, z
00445 ///
00446 /// .. and delete the predecessor corresponding to the '1', this will attempt to
00447 /// recursively fold the and to 0.
00448 void llvm::RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
00449                                         DataLayout *TD) {
00450   // This only adjusts blocks with PHI nodes.
00451   if (!isa<PHINode>(BB->begin()))
00452     return;
00453   
00454   // Remove the entries for Pred from the PHI nodes in BB, but do not simplify
00455   // them down.  This will leave us with single entry phi nodes and other phis
00456   // that can be removed.
00457   BB->removePredecessor(Pred, true);
00458   
00459   WeakVH PhiIt = &BB->front();
00460   while (PHINode *PN = dyn_cast<PHINode>(PhiIt)) {
00461     PhiIt = &*++BasicBlock::iterator(cast<Instruction>(PhiIt));
00462     Value *OldPhiIt = PhiIt;
00463 
00464     if (!recursivelySimplifyInstruction(PN, TD))
00465       continue;
00466 
00467     // If recursive simplification ended up deleting the next PHI node we would
00468     // iterate to, then our iterator is invalid, restart scanning from the top
00469     // of the block.
00470     if (PhiIt != OldPhiIt) PhiIt = &BB->front();
00471   }
00472 }
00473 
00474 
00475 /// MergeBasicBlockIntoOnlyPred - DestBB is a block with one predecessor and its
00476 /// predecessor is known to have one successor (DestBB!).  Eliminate the edge
00477 /// between them, moving the instructions in the predecessor into DestBB and
00478 /// deleting the predecessor block.
00479 ///
00480 void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, Pass *P) {
00481   // If BB has single-entry PHI nodes, fold them.
00482   while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
00483     Value *NewVal = PN->getIncomingValue(0);
00484     // Replace self referencing PHI with undef, it must be dead.
00485     if (NewVal == PN) NewVal = UndefValue::get(PN->getType());
00486     PN->replaceAllUsesWith(NewVal);
00487     PN->eraseFromParent();
00488   }
00489   
00490   BasicBlock *PredBB = DestBB->getSinglePredecessor();
00491   assert(PredBB && "Block doesn't have a single predecessor!");
00492   
00493   // Zap anything that took the address of DestBB.  Not doing this will give the
00494   // address an invalid value.
00495   if (DestBB->hasAddressTaken()) {
00496     BlockAddress *BA = BlockAddress::get(DestBB);
00497     Constant *Replacement =
00498       ConstantInt::get(llvm::Type::getInt32Ty(BA->getContext()), 1);
00499     BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement,
00500                                                      BA->getType()));
00501     BA->destroyConstant();
00502   }
00503   
00504   // Anything that branched to PredBB now branches to DestBB.
00505   PredBB->replaceAllUsesWith(DestBB);
00506   
00507   // Splice all the instructions from PredBB to DestBB.
00508   PredBB->getTerminator()->eraseFromParent();
00509   DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList());
00510 
00511   if (P) {
00512     DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>();
00513     if (DT) {
00514       BasicBlock *PredBBIDom = DT->getNode(PredBB)->getIDom()->getBlock();
00515       DT->changeImmediateDominator(DestBB, PredBBIDom);
00516       DT->eraseNode(PredBB);
00517     }
00518     ProfileInfo *PI = P->getAnalysisIfAvailable<ProfileInfo>();
00519     if (PI) {
00520       PI->replaceAllUses(PredBB, DestBB);
00521       PI->removeEdge(ProfileInfo::getEdge(PredBB, DestBB));
00522     }
00523   }
00524   // Nuke BB.
00525   PredBB->eraseFromParent();
00526 }
00527 
00528 /// CanPropagatePredecessorsForPHIs - Return true if we can fold BB, an
00529 /// almost-empty BB ending in an unconditional branch to Succ, into succ.
00530 ///
00531 /// Assumption: Succ is the single successor for BB.
00532 ///
00533 static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
00534   assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
00535 
00536   DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into " 
00537         << Succ->getName() << "\n");
00538   // Shortcut, if there is only a single predecessor it must be BB and merging
00539   // is always safe
00540   if (Succ->getSinglePredecessor()) return true;
00541 
00542   // Make a list of the predecessors of BB
00543   SmallPtrSet<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB));
00544 
00545   // Look at all the phi nodes in Succ, to see if they present a conflict when
00546   // merging these blocks
00547   for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
00548     PHINode *PN = cast<PHINode>(I);
00549 
00550     // If the incoming value from BB is again a PHINode in
00551     // BB which has the same incoming value for *PI as PN does, we can
00552     // merge the phi nodes and then the blocks can still be merged
00553     PHINode *BBPN = dyn_cast<PHINode>(PN->getIncomingValueForBlock(BB));
00554     if (BBPN && BBPN->getParent() == BB) {
00555       for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) {
00556         BasicBlock *IBB = PN->getIncomingBlock(PI);
00557         if (BBPreds.count(IBB) &&
00558             BBPN->getIncomingValueForBlock(IBB) != PN->getIncomingValue(PI)) {
00559           DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in " 
00560                 << Succ->getName() << " is conflicting with " 
00561                 << BBPN->getName() << " with regard to common predecessor "
00562                 << IBB->getName() << "\n");
00563           return false;
00564         }
00565       }
00566     } else {
00567       Value* Val = PN->getIncomingValueForBlock(BB);
00568       for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) {
00569         // See if the incoming value for the common predecessor is equal to the
00570         // one for BB, in which case this phi node will not prevent the merging
00571         // of the block.
00572         BasicBlock *IBB = PN->getIncomingBlock(PI);
00573         if (BBPreds.count(IBB) && Val != PN->getIncomingValue(PI)) {
00574           DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in " 
00575                 << Succ->getName() << " is conflicting with regard to common "
00576                 << "predecessor " << IBB->getName() << "\n");
00577           return false;
00578         }
00579       }
00580     }
00581   }
00582 
00583   return true;
00584 }
00585 
00586 /// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an
00587 /// unconditional branch, and contains no instructions other than PHI nodes,
00588 /// potential side-effect free intrinsics and the branch.  If possible,
00589 /// eliminate BB by rewriting all the predecessors to branch to the successor
00590 /// block and return true.  If we can't transform, return false.
00591 bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB) {
00592   assert(BB != &BB->getParent()->getEntryBlock() &&
00593          "TryToSimplifyUncondBranchFromEmptyBlock called on entry block!");
00594 
00595   // We can't eliminate infinite loops.
00596   BasicBlock *Succ = cast<BranchInst>(BB->getTerminator())->getSuccessor(0);
00597   if (BB == Succ) return false;
00598   
00599   // Check to see if merging these blocks would cause conflicts for any of the
00600   // phi nodes in BB or Succ. If not, we can safely merge.
00601   if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false;
00602 
00603   // Check for cases where Succ has multiple predecessors and a PHI node in BB
00604   // has uses which will not disappear when the PHI nodes are merged.  It is
00605   // possible to handle such cases, but difficult: it requires checking whether
00606   // BB dominates Succ, which is non-trivial to calculate in the case where
00607   // Succ has multiple predecessors.  Also, it requires checking whether
00608   // constructing the necessary self-referential PHI node doesn't introduce any
00609   // conflicts; this isn't too difficult, but the previous code for doing this
00610   // was incorrect.
00611   //
00612   // Note that if this check finds a live use, BB dominates Succ, so BB is
00613   // something like a loop pre-header (or rarely, a part of an irreducible CFG);
00614   // folding the branch isn't profitable in that case anyway.
00615   if (!Succ->getSinglePredecessor()) {
00616     BasicBlock::iterator BBI = BB->begin();
00617     while (isa<PHINode>(*BBI)) {
00618       for (Value::use_iterator UI = BBI->use_begin(), E = BBI->use_end();
00619            UI != E; ++UI) {
00620         if (PHINode* PN = dyn_cast<PHINode>(*UI)) {
00621           if (PN->getIncomingBlock(UI) != BB)
00622             return false;
00623         } else {
00624           return false;
00625         }
00626       }
00627       ++BBI;
00628     }
00629   }
00630 
00631   DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB);
00632   
00633   if (isa<PHINode>(Succ->begin())) {
00634     // If there is more than one pred of succ, and there are PHI nodes in
00635     // the successor, then we need to add incoming edges for the PHI nodes
00636     //
00637     const SmallVector<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB));
00638     
00639     // Loop over all of the PHI nodes in the successor of BB.
00640     for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
00641       PHINode *PN = cast<PHINode>(I);
00642       Value *OldVal = PN->removeIncomingValue(BB, false);
00643       assert(OldVal && "No entry in PHI for Pred BB!");
00644       
00645       // If this incoming value is one of the PHI nodes in BB, the new entries
00646       // in the PHI node are the entries from the old PHI.
00647       if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) {
00648         PHINode *OldValPN = cast<PHINode>(OldVal);
00649         for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i)
00650           // Note that, since we are merging phi nodes and BB and Succ might
00651           // have common predecessors, we could end up with a phi node with
00652           // identical incoming branches. This will be cleaned up later (and
00653           // will trigger asserts if we try to clean it up now, without also
00654           // simplifying the corresponding conditional branch).
00655           PN->addIncoming(OldValPN->getIncomingValue(i),
00656                           OldValPN->getIncomingBlock(i));
00657       } else {
00658         // Add an incoming value for each of the new incoming values.
00659         for (unsigned i = 0, e = BBPreds.size(); i != e; ++i)
00660           PN->addIncoming(OldVal, BBPreds[i]);
00661       }
00662     }
00663   }
00664   
00665   if (Succ->getSinglePredecessor()) {
00666     // BB is the only predecessor of Succ, so Succ will end up with exactly
00667     // the same predecessors BB had.
00668 
00669     // Copy over any phi, debug or lifetime instruction.
00670     BB->getTerminator()->eraseFromParent();
00671     Succ->getInstList().splice(Succ->getFirstNonPHI(), BB->getInstList());
00672   } else {
00673     while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
00674       // We explicitly check for such uses in CanPropagatePredecessorsForPHIs.
00675       assert(PN->use_empty() && "There shouldn't be any uses here!");
00676       PN->eraseFromParent();
00677     }
00678   }
00679     
00680   // Everything that jumped to BB now goes to Succ.
00681   BB->replaceAllUsesWith(Succ);
00682   if (!Succ->hasName()) Succ->takeName(BB);
00683   BB->eraseFromParent();              // Delete the old basic block.
00684   return true;
00685 }
00686 
00687 /// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI
00688 /// nodes in this block. This doesn't try to be clever about PHI nodes
00689 /// which differ only in the order of the incoming values, but instcombine
00690 /// orders them so it usually won't matter.
00691 ///
00692 bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) {
00693   bool Changed = false;
00694 
00695   // This implementation doesn't currently consider undef operands
00696   // specially. Theoretically, two phis which are identical except for
00697   // one having an undef where the other doesn't could be collapsed.
00698 
00699   // Map from PHI hash values to PHI nodes. If multiple PHIs have
00700   // the same hash value, the element is the first PHI in the
00701   // linked list in CollisionMap.
00702   DenseMap<uintptr_t, PHINode *> HashMap;
00703 
00704   // Maintain linked lists of PHI nodes with common hash values.
00705   DenseMap<PHINode *, PHINode *> CollisionMap;
00706 
00707   // Examine each PHI.
00708   for (BasicBlock::iterator I = BB->begin();
00709        PHINode *PN = dyn_cast<PHINode>(I++); ) {
00710     // Compute a hash value on the operands. Instcombine will likely have sorted
00711     // them, which helps expose duplicates, but we have to check all the
00712     // operands to be safe in case instcombine hasn't run.
00713     uintptr_t Hash = 0;
00714     // This hash algorithm is quite weak as hash functions go, but it seems
00715     // to do a good enough job for this particular purpose, and is very quick.
00716     for (User::op_iterator I = PN->op_begin(), E = PN->op_end(); I != E; ++I) {
00717       Hash ^= reinterpret_cast<uintptr_t>(static_cast<Value *>(*I));
00718       Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7));
00719     }
00720     for (PHINode::block_iterator I = PN->block_begin(), E = PN->block_end();
00721          I != E; ++I) {
00722       Hash ^= reinterpret_cast<uintptr_t>(static_cast<BasicBlock *>(*I));
00723       Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7));
00724     }
00725     // Avoid colliding with the DenseMap sentinels ~0 and ~0-1.
00726     Hash >>= 1;
00727     // If we've never seen this hash value before, it's a unique PHI.
00728     std::pair<DenseMap<uintptr_t, PHINode *>::iterator, bool> Pair =
00729       HashMap.insert(std::make_pair(Hash, PN));
00730     if (Pair.second) continue;
00731     // Otherwise it's either a duplicate or a hash collision.
00732     for (PHINode *OtherPN = Pair.first->second; ; ) {
00733       if (OtherPN->isIdenticalTo(PN)) {
00734         // A duplicate. Replace this PHI with its duplicate.
00735         PN->replaceAllUsesWith(OtherPN);
00736         PN->eraseFromParent();
00737         Changed = true;
00738         break;
00739       }
00740       // A non-duplicate hash collision.
00741       DenseMap<PHINode *, PHINode *>::iterator I = CollisionMap.find(OtherPN);
00742       if (I == CollisionMap.end()) {
00743         // Set this PHI to be the head of the linked list of colliding PHIs.
00744         PHINode *Old = Pair.first->second;
00745         Pair.first->second = PN;
00746         CollisionMap[PN] = Old;
00747         break;
00748       }
00749       // Proceed to the next PHI in the list.
00750       OtherPN = I->second;
00751     }
00752   }
00753 
00754   return Changed;
00755 }
00756 
00757 /// enforceKnownAlignment - If the specified pointer points to an object that
00758 /// we control, modify the object's alignment to PrefAlign. This isn't
00759 /// often possible though. If alignment is important, a more reliable approach
00760 /// is to simply align all global variables and allocation instructions to
00761 /// their preferred alignment from the beginning.
00762 ///
00763 static unsigned enforceKnownAlignment(Value *V, unsigned Align,
00764                                       unsigned PrefAlign, const DataLayout *TD) {
00765   V = V->stripPointerCasts();
00766 
00767   if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
00768     // If the preferred alignment is greater than the natural stack alignment
00769     // then don't round up. This avoids dynamic stack realignment.
00770     if (TD && TD->exceedsNaturalStackAlignment(PrefAlign))
00771       return Align;
00772     // If there is a requested alignment and if this is an alloca, round up.
00773     if (AI->getAlignment() >= PrefAlign)
00774       return AI->getAlignment();
00775     AI->setAlignment(PrefAlign);
00776     return PrefAlign;
00777   }
00778 
00779   if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
00780     // If there is a large requested alignment and we can, bump up the alignment
00781     // of the global.
00782     if (GV->isDeclaration()) return Align;
00783     // If the memory we set aside for the global may not be the memory used by
00784     // the final program then it is impossible for us to reliably enforce the
00785     // preferred alignment.
00786     if (GV->isWeakForLinker()) return Align;
00787     
00788     if (GV->getAlignment() >= PrefAlign)
00789       return GV->getAlignment();
00790     // We can only increase the alignment of the global if it has no alignment
00791     // specified or if it is not assigned a section.  If it is assigned a
00792     // section, the global could be densely packed with other objects in the
00793     // section, increasing the alignment could cause padding issues.
00794     if (!GV->hasSection() || GV->getAlignment() == 0)
00795       GV->setAlignment(PrefAlign);
00796     return GV->getAlignment();
00797   }
00798 
00799   return Align;
00800 }
00801 
00802 /// getOrEnforceKnownAlignment - If the specified pointer has an alignment that
00803 /// we can determine, return it, otherwise return 0.  If PrefAlign is specified,
00804 /// and it is more than the alignment of the ultimate object, see if we can
00805 /// increase the alignment of the ultimate object, making this check succeed.
00806 unsigned llvm::getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign,
00807                                           const DataLayout *TD) {
00808   assert(V->getType()->isPointerTy() &&
00809          "getOrEnforceKnownAlignment expects a pointer!");
00810   unsigned BitWidth = TD ? TD->getPointerSizeInBits() : 64;
00811   APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
00812   ComputeMaskedBits(V, KnownZero, KnownOne, TD);
00813   unsigned TrailZ = KnownZero.countTrailingOnes();
00814   
00815   // Avoid trouble with rediculously large TrailZ values, such as
00816   // those computed from a null pointer.
00817   TrailZ = std::min(TrailZ, unsigned(sizeof(unsigned) * CHAR_BIT - 1));
00818   
00819   unsigned Align = 1u << std::min(BitWidth - 1, TrailZ);
00820   
00821   // LLVM doesn't support alignments larger than this currently.
00822   Align = std::min(Align, +Value::MaximumAlignment);
00823   
00824   if (PrefAlign > Align)
00825     Align = enforceKnownAlignment(V, Align, PrefAlign, TD);
00826     
00827   // We don't need to make any adjustment.
00828   return Align;
00829 }
00830 
00831 ///===---------------------------------------------------------------------===//
00832 ///  Dbg Intrinsic utilities
00833 ///
00834 
00835 /// See if there is a dbg.value intrinsic for DIVar before I.
00836 static bool LdStHasDebugValue(DIVariable &DIVar, Instruction *I) {
00837   // Since we can't guarantee that the original dbg.declare instrinsic
00838   // is removed by LowerDbgDeclare(), we need to make sure that we are
00839   // not inserting the same dbg.value intrinsic over and over.
00840   llvm::BasicBlock::InstListType::iterator PrevI(I);
00841   if (PrevI != I->getParent()->getInstList().begin()) {
00842     --PrevI;
00843     if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(PrevI))
00844       if (DVI->getValue() == I->getOperand(0) &&
00845           DVI->getOffset() == 0 &&
00846           DVI->getVariable() == DIVar)
00847         return true;
00848   }
00849   return false;
00850 }
00851 
00852 /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
00853 /// that has an associated llvm.dbg.decl intrinsic.
00854 bool llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
00855                                            StoreInst *SI, DIBuilder &Builder) {
00856   DIVariable DIVar(DDI->getVariable());
00857   if (!DIVar.Verify())
00858     return false;
00859 
00860   if (LdStHasDebugValue(DIVar, SI))
00861     return true;
00862 
00863   Instruction *DbgVal = NULL;
00864   // If an argument is zero extended then use argument directly. The ZExt
00865   // may be zapped by an optimization pass in future.
00866   Argument *ExtendedArg = NULL;
00867   if (ZExtInst *ZExt = dyn_cast<ZExtInst>(SI->getOperand(0)))
00868     ExtendedArg = dyn_cast<Argument>(ZExt->getOperand(0));
00869   if (SExtInst *SExt = dyn_cast<SExtInst>(SI->getOperand(0)))
00870     ExtendedArg = dyn_cast<Argument>(SExt->getOperand(0));
00871   if (ExtendedArg)
00872     DbgVal = Builder.insertDbgValueIntrinsic(ExtendedArg, 0, DIVar, SI);
00873   else
00874     DbgVal = Builder.insertDbgValueIntrinsic(SI->getOperand(0), 0, DIVar, SI);
00875 
00876   // Propagate any debug metadata from the store onto the dbg.value.
00877   DebugLoc SIDL = SI->getDebugLoc();
00878   if (!SIDL.isUnknown())
00879     DbgVal->setDebugLoc(SIDL);
00880   // Otherwise propagate debug metadata from dbg.declare.
00881   else
00882     DbgVal->setDebugLoc(DDI->getDebugLoc());
00883   return true;
00884 }
00885 
00886 /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
00887 /// that has an associated llvm.dbg.decl intrinsic.
00888 bool llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
00889                                            LoadInst *LI, DIBuilder &Builder) {
00890   DIVariable DIVar(DDI->getVariable());
00891   if (!DIVar.Verify())
00892     return false;
00893 
00894   if (LdStHasDebugValue(DIVar, LI))
00895     return true;
00896 
00897   Instruction *DbgVal = 
00898     Builder.insertDbgValueIntrinsic(LI->getOperand(0), 0,
00899                                     DIVar, LI);
00900   
00901   // Propagate any debug metadata from the store onto the dbg.value.
00902   DebugLoc LIDL = LI->getDebugLoc();
00903   if (!LIDL.isUnknown())
00904     DbgVal->setDebugLoc(LIDL);
00905   // Otherwise propagate debug metadata from dbg.declare.
00906   else
00907     DbgVal->setDebugLoc(DDI->getDebugLoc());
00908   return true;
00909 }
00910 
00911 /// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set
00912 /// of llvm.dbg.value intrinsics.
00913 bool llvm::LowerDbgDeclare(Function &F) {
00914   DIBuilder DIB(*F.getParent());
00915   SmallVector<DbgDeclareInst *, 4> Dbgs;
00916   for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
00917     for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ++BI) {
00918       if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
00919         Dbgs.push_back(DDI);
00920     }
00921   if (Dbgs.empty())
00922     return false;
00923 
00924   for (SmallVector<DbgDeclareInst *, 4>::iterator I = Dbgs.begin(),
00925          E = Dbgs.end(); I != E; ++I) {
00926     DbgDeclareInst *DDI = *I;
00927     if (AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress())) {
00928       // We only remove the dbg.declare intrinsic if all uses are
00929       // converted to dbg.value intrinsics.
00930       bool RemoveDDI = true;
00931       for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end();
00932            UI != E; ++UI)
00933         if (StoreInst *SI = dyn_cast<StoreInst>(*UI))
00934           ConvertDebugDeclareToDebugValue(DDI, SI, DIB);
00935         else if (LoadInst *LI = dyn_cast<LoadInst>(*UI))
00936           ConvertDebugDeclareToDebugValue(DDI, LI, DIB);
00937         else
00938           RemoveDDI = false;
00939       if (RemoveDDI)
00940         DDI->eraseFromParent();
00941     }
00942   }
00943   return true;
00944 }
00945 
00946 /// FindAllocaDbgDeclare - Finds the llvm.dbg.declare intrinsic describing the
00947 /// alloca 'V', if any.
00948 DbgDeclareInst *llvm::FindAllocaDbgDeclare(Value *V) {
00949   if (MDNode *DebugNode = MDNode::getIfExists(V->getContext(), V))
00950     for (Value::use_iterator UI = DebugNode->use_begin(),
00951          E = DebugNode->use_end(); UI != E; ++UI)
00952       if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(*UI))
00953         return DDI;
00954 
00955   return 0;
00956 }
00957 
00958 bool llvm::replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
00959                                       DIBuilder &Builder) {
00960   DbgDeclareInst *DDI = FindAllocaDbgDeclare(AI);
00961   if (!DDI)
00962     return false;
00963   DIVariable DIVar(DDI->getVariable());
00964   if (!DIVar.Verify())
00965     return false;
00966 
00967   // Create a copy of the original DIDescriptor for user variable, appending
00968   // "deref" operation to a list of address elements, as new llvm.dbg.declare
00969   // will take a value storing address of the memory for variable, not
00970   // alloca itself.
00971   Type *Int64Ty = Type::getInt64Ty(AI->getContext());
00972   SmallVector<Value*, 4> NewDIVarAddress;
00973   if (DIVar.hasComplexAddress()) {
00974     for (unsigned i = 0, n = DIVar.getNumAddrElements(); i < n; ++i) {
00975       NewDIVarAddress.push_back(
00976           ConstantInt::get(Int64Ty, DIVar.getAddrElement(i)));
00977     }
00978   }
00979   NewDIVarAddress.push_back(ConstantInt::get(Int64Ty, DIBuilder::OpDeref));
00980   DIVariable NewDIVar = Builder.createComplexVariable(
00981       DIVar.getTag(), DIVar.getContext(), DIVar.getName(),
00982       DIVar.getFile(), DIVar.getLineNumber(), DIVar.getType(),
00983       NewDIVarAddress, DIVar.getArgNumber());
00984 
00985   // Insert llvm.dbg.declare in the same basic block as the original alloca,
00986   // and remove old llvm.dbg.declare.
00987   BasicBlock *BB = AI->getParent();
00988   Builder.insertDeclare(NewAllocaAddress, NewDIVar, BB);
00989   DDI->eraseFromParent();
00990   return true;
00991 }
00992 
00993 bool llvm::removeUnreachableBlocks(Function &F) {
00994   SmallPtrSet<BasicBlock*, 16> Reachable;
00995   SmallVector<BasicBlock*, 128> Worklist;
00996   Worklist.push_back(&F.getEntryBlock());
00997   Reachable.insert(&F.getEntryBlock());
00998   do {
00999     BasicBlock *BB = Worklist.pop_back_val();
01000     for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
01001       if (Reachable.insert(*SI))
01002         Worklist.push_back(*SI);
01003   } while (!Worklist.empty());
01004 
01005   if (Reachable.size() == F.size())
01006     return false;
01007 
01008   assert(Reachable.size() < F.size());
01009   for (Function::iterator I = llvm::next(F.begin()), E = F.end(); I != E; ++I) {
01010     if (Reachable.count(I))
01011       continue;
01012 
01013     for (succ_iterator SI = succ_begin(I), SE = succ_end(I); SI != SE; ++SI)
01014       if (Reachable.count(*SI))
01015         (*SI)->removePredecessor(I);
01016     I->dropAllReferences();
01017   }
01018 
01019   for (Function::iterator I = llvm::next(F.begin()), E=F.end(); I != E;)
01020     if (!Reachable.count(I))
01021       I = F.getBasicBlockList().erase(I);
01022     else
01023       ++I;
01024 
01025   return true;
01026 }