LLVM API Documentation

DeadStoreElimination.cpp
Go to the documentation of this file.
00001 //===- DeadStoreElimination.cpp - Fast Dead Store Elimination -------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements a trivial dead store elimination that only considers
00011 // basic-block local redundant stores.
00012 //
00013 // FIXME: This should eventually be extended to be a post-dominator tree
00014 // traversal.  Doing so would be pretty trivial.
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #define DEBUG_TYPE "dse"
00019 #include "llvm/Transforms/Scalar.h"
00020 #include "llvm/ADT/STLExtras.h"
00021 #include "llvm/ADT/SetVector.h"
00022 #include "llvm/ADT/Statistic.h"
00023 #include "llvm/Analysis/AliasAnalysis.h"
00024 #include "llvm/Analysis/CaptureTracking.h"
00025 #include "llvm/Analysis/Dominators.h"
00026 #include "llvm/Analysis/MemoryBuiltins.h"
00027 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
00028 #include "llvm/Analysis/ValueTracking.h"
00029 #include "llvm/IR/Constants.h"
00030 #include "llvm/IR/DataLayout.h"
00031 #include "llvm/IR/Function.h"
00032 #include "llvm/IR/GlobalVariable.h"
00033 #include "llvm/IR/Instructions.h"
00034 #include "llvm/IR/IntrinsicInst.h"
00035 #include "llvm/Pass.h"
00036 #include "llvm/Support/Debug.h"
00037 #include "llvm/Target/TargetLibraryInfo.h"
00038 #include "llvm/Transforms/Utils/Local.h"
00039 using namespace llvm;
00040 
00041 STATISTIC(NumFastStores, "Number of stores deleted");
00042 STATISTIC(NumFastOther , "Number of other instrs removed");
00043 
00044 namespace {
00045   struct DSE : public FunctionPass {
00046     AliasAnalysis *AA;
00047     MemoryDependenceAnalysis *MD;
00048     DominatorTree *DT;
00049     const TargetLibraryInfo *TLI;
00050 
00051     static char ID; // Pass identification, replacement for typeid
00052     DSE() : FunctionPass(ID), AA(0), MD(0), DT(0) {
00053       initializeDSEPass(*PassRegistry::getPassRegistry());
00054     }
00055 
00056     virtual bool runOnFunction(Function &F) {
00057       AA = &getAnalysis<AliasAnalysis>();
00058       MD = &getAnalysis<MemoryDependenceAnalysis>();
00059       DT = &getAnalysis<DominatorTree>();
00060       TLI = AA->getTargetLibraryInfo();
00061 
00062       bool Changed = false;
00063       for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
00064         // Only check non-dead blocks.  Dead blocks may have strange pointer
00065         // cycles that will confuse alias analysis.
00066         if (DT->isReachableFromEntry(I))
00067           Changed |= runOnBasicBlock(*I);
00068 
00069       AA = 0; MD = 0; DT = 0;
00070       return Changed;
00071     }
00072 
00073     bool runOnBasicBlock(BasicBlock &BB);
00074     bool HandleFree(CallInst *F);
00075     bool handleEndBlock(BasicBlock &BB);
00076     void RemoveAccessedObjects(const AliasAnalysis::Location &LoadedLoc,
00077                                SmallSetVector<Value*, 16> &DeadStackObjects);
00078 
00079     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00080       AU.setPreservesCFG();
00081       AU.addRequired<DominatorTree>();
00082       AU.addRequired<AliasAnalysis>();
00083       AU.addRequired<MemoryDependenceAnalysis>();
00084       AU.addPreserved<AliasAnalysis>();
00085       AU.addPreserved<DominatorTree>();
00086       AU.addPreserved<MemoryDependenceAnalysis>();
00087     }
00088   };
00089 }
00090 
00091 char DSE::ID = 0;
00092 INITIALIZE_PASS_BEGIN(DSE, "dse", "Dead Store Elimination", false, false)
00093 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
00094 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
00095 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
00096 INITIALIZE_PASS_END(DSE, "dse", "Dead Store Elimination", false, false)
00097 
00098 FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
00099 
00100 //===----------------------------------------------------------------------===//
00101 // Helper functions
00102 //===----------------------------------------------------------------------===//
00103 
00104 /// DeleteDeadInstruction - Delete this instruction.  Before we do, go through
00105 /// and zero out all the operands of this instruction.  If any of them become
00106 /// dead, delete them and the computation tree that feeds them.
00107 ///
00108 /// If ValueSet is non-null, remove any deleted instructions from it as well.
00109 ///
00110 static void DeleteDeadInstruction(Instruction *I,
00111                                   MemoryDependenceAnalysis &MD,
00112                                   const TargetLibraryInfo *TLI,
00113                                   SmallSetVector<Value*, 16> *ValueSet = 0) {
00114   SmallVector<Instruction*, 32> NowDeadInsts;
00115 
00116   NowDeadInsts.push_back(I);
00117   --NumFastOther;
00118 
00119   // Before we touch this instruction, remove it from memdep!
00120   do {
00121     Instruction *DeadInst = NowDeadInsts.pop_back_val();
00122     ++NumFastOther;
00123 
00124     // This instruction is dead, zap it, in stages.  Start by removing it from
00125     // MemDep, which needs to know the operands and needs it to be in the
00126     // function.
00127     MD.removeInstruction(DeadInst);
00128 
00129     for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
00130       Value *Op = DeadInst->getOperand(op);
00131       DeadInst->setOperand(op, 0);
00132 
00133       // If this operand just became dead, add it to the NowDeadInsts list.
00134       if (!Op->use_empty()) continue;
00135 
00136       if (Instruction *OpI = dyn_cast<Instruction>(Op))
00137         if (isInstructionTriviallyDead(OpI, TLI))
00138           NowDeadInsts.push_back(OpI);
00139     }
00140 
00141     DeadInst->eraseFromParent();
00142 
00143     if (ValueSet) ValueSet->remove(DeadInst);
00144   } while (!NowDeadInsts.empty());
00145 }
00146 
00147 
00148 /// hasMemoryWrite - Does this instruction write some memory?  This only returns
00149 /// true for things that we can analyze with other helpers below.
00150 static bool hasMemoryWrite(Instruction *I, const TargetLibraryInfo *TLI) {
00151   if (isa<StoreInst>(I))
00152     return true;
00153   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
00154     switch (II->getIntrinsicID()) {
00155     default:
00156       return false;
00157     case Intrinsic::memset:
00158     case Intrinsic::memmove:
00159     case Intrinsic::memcpy:
00160     case Intrinsic::init_trampoline:
00161     case Intrinsic::lifetime_end:
00162       return true;
00163     }
00164   }
00165   if (CallSite CS = I) {
00166     if (Function *F = CS.getCalledFunction()) {
00167       if (TLI && TLI->has(LibFunc::strcpy) &&
00168           F->getName() == TLI->getName(LibFunc::strcpy)) {
00169         return true;
00170       }
00171       if (TLI && TLI->has(LibFunc::strncpy) &&
00172           F->getName() == TLI->getName(LibFunc::strncpy)) {
00173         return true;
00174       }
00175       if (TLI && TLI->has(LibFunc::strcat) &&
00176           F->getName() == TLI->getName(LibFunc::strcat)) {
00177         return true;
00178       }
00179       if (TLI && TLI->has(LibFunc::strncat) &&
00180           F->getName() == TLI->getName(LibFunc::strncat)) {
00181         return true;
00182       }
00183     }
00184   }
00185   return false;
00186 }
00187 
00188 /// getLocForWrite - Return a Location stored to by the specified instruction.
00189 /// If isRemovable returns true, this function and getLocForRead completely
00190 /// describe the memory operations for this instruction.
00191 static AliasAnalysis::Location
00192 getLocForWrite(Instruction *Inst, AliasAnalysis &AA) {
00193   if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
00194     return AA.getLocation(SI);
00195 
00196   if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(Inst)) {
00197     // memcpy/memmove/memset.
00198     AliasAnalysis::Location Loc = AA.getLocationForDest(MI);
00199     // If we don't have target data around, an unknown size in Location means
00200     // that we should use the size of the pointee type.  This isn't valid for
00201     // memset/memcpy, which writes more than an i8.
00202     if (Loc.Size == AliasAnalysis::UnknownSize && AA.getDataLayout() == 0)
00203       return AliasAnalysis::Location();
00204     return Loc;
00205   }
00206 
00207   IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst);
00208   if (II == 0) return AliasAnalysis::Location();
00209 
00210   switch (II->getIntrinsicID()) {
00211   default: return AliasAnalysis::Location(); // Unhandled intrinsic.
00212   case Intrinsic::init_trampoline:
00213     // If we don't have target data around, an unknown size in Location means
00214     // that we should use the size of the pointee type.  This isn't valid for
00215     // init.trampoline, which writes more than an i8.
00216     if (AA.getDataLayout() == 0) return AliasAnalysis::Location();
00217 
00218     // FIXME: We don't know the size of the trampoline, so we can't really
00219     // handle it here.
00220     return AliasAnalysis::Location(II->getArgOperand(0));
00221   case Intrinsic::lifetime_end: {
00222     uint64_t Len = cast<ConstantInt>(II->getArgOperand(0))->getZExtValue();
00223     return AliasAnalysis::Location(II->getArgOperand(1), Len);
00224   }
00225   }
00226 }
00227 
00228 /// getLocForRead - Return the location read by the specified "hasMemoryWrite"
00229 /// instruction if any.
00230 static AliasAnalysis::Location
00231 getLocForRead(Instruction *Inst, AliasAnalysis &AA) {
00232   assert(hasMemoryWrite(Inst, AA.getTargetLibraryInfo()) &&
00233          "Unknown instruction case");
00234 
00235   // The only instructions that both read and write are the mem transfer
00236   // instructions (memcpy/memmove).
00237   if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(Inst))
00238     return AA.getLocationForSource(MTI);
00239   return AliasAnalysis::Location();
00240 }
00241 
00242 
00243 /// isRemovable - If the value of this instruction and the memory it writes to
00244 /// is unused, may we delete this instruction?
00245 static bool isRemovable(Instruction *I) {
00246   // Don't remove volatile/atomic stores.
00247   if (StoreInst *SI = dyn_cast<StoreInst>(I))
00248     return SI->isUnordered();
00249 
00250   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
00251     switch (II->getIntrinsicID()) {
00252     default: llvm_unreachable("doesn't pass 'hasMemoryWrite' predicate");
00253     case Intrinsic::lifetime_end:
00254       // Never remove dead lifetime_end's, e.g. because it is followed by a
00255       // free.
00256       return false;
00257     case Intrinsic::init_trampoline:
00258       // Always safe to remove init_trampoline.
00259       return true;
00260 
00261     case Intrinsic::memset:
00262     case Intrinsic::memmove:
00263     case Intrinsic::memcpy:
00264       // Don't remove volatile memory intrinsics.
00265       return !cast<MemIntrinsic>(II)->isVolatile();
00266     }
00267   }
00268 
00269   if (CallSite CS = I)
00270     return CS.getInstruction()->use_empty();
00271 
00272   return false;
00273 }
00274 
00275 
00276 /// isShortenable - Returns true if this instruction can be safely shortened in
00277 /// length.
00278 static bool isShortenable(Instruction *I) {
00279   // Don't shorten stores for now
00280   if (isa<StoreInst>(I))
00281     return false;
00282 
00283   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
00284     switch (II->getIntrinsicID()) {
00285       default: return false;
00286       case Intrinsic::memset:
00287       case Intrinsic::memcpy:
00288         // Do shorten memory intrinsics.
00289         return true;
00290     }
00291   }
00292 
00293   // Don't shorten libcalls calls for now.
00294 
00295   return false;
00296 }
00297 
00298 /// getStoredPointerOperand - Return the pointer that is being written to.
00299 static Value *getStoredPointerOperand(Instruction *I) {
00300   if (StoreInst *SI = dyn_cast<StoreInst>(I))
00301     return SI->getPointerOperand();
00302   if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I))
00303     return MI->getDest();
00304 
00305   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
00306     switch (II->getIntrinsicID()) {
00307     default: llvm_unreachable("Unexpected intrinsic!");
00308     case Intrinsic::init_trampoline:
00309       return II->getArgOperand(0);
00310     }
00311   }
00312 
00313   CallSite CS = I;
00314   // All the supported functions so far happen to have dest as their first
00315   // argument.
00316   return CS.getArgument(0);
00317 }
00318 
00319 static uint64_t getPointerSize(const Value *V, AliasAnalysis &AA) {
00320   uint64_t Size;
00321   if (getObjectSize(V, Size, AA.getDataLayout(), AA.getTargetLibraryInfo()))
00322     return Size;
00323   return AliasAnalysis::UnknownSize;
00324 }
00325 
00326 namespace {
00327   enum OverwriteResult
00328   {
00329     OverwriteComplete,
00330     OverwriteEnd,
00331     OverwriteUnknown
00332   };
00333 }
00334 
00335 /// isOverwrite - Return 'OverwriteComplete' if a store to the 'Later' location
00336 /// completely overwrites a store to the 'Earlier' location.
00337 /// 'OverwriteEnd' if the end of the 'Earlier' location is completely
00338 /// overwritten by 'Later', or 'OverwriteUnknown' if nothing can be determined
00339 static OverwriteResult isOverwrite(const AliasAnalysis::Location &Later,
00340                                    const AliasAnalysis::Location &Earlier,
00341                                    AliasAnalysis &AA,
00342                                    int64_t &EarlierOff,
00343                                    int64_t &LaterOff) {
00344   const Value *P1 = Earlier.Ptr->stripPointerCasts();
00345   const Value *P2 = Later.Ptr->stripPointerCasts();
00346 
00347   // If the start pointers are the same, we just have to compare sizes to see if
00348   // the later store was larger than the earlier store.
00349   if (P1 == P2) {
00350     // If we don't know the sizes of either access, then we can't do a
00351     // comparison.
00352     if (Later.Size == AliasAnalysis::UnknownSize ||
00353         Earlier.Size == AliasAnalysis::UnknownSize) {
00354       // If we have no DataLayout information around, then the size of the store
00355       // is inferrable from the pointee type.  If they are the same type, then
00356       // we know that the store is safe.
00357       if (AA.getDataLayout() == 0 &&
00358           Later.Ptr->getType() == Earlier.Ptr->getType())
00359         return OverwriteComplete;
00360 
00361       return OverwriteUnknown;
00362     }
00363 
00364     // Make sure that the Later size is >= the Earlier size.
00365     if (Later.Size >= Earlier.Size)
00366       return OverwriteComplete;
00367   }
00368 
00369   // Otherwise, we have to have size information, and the later store has to be
00370   // larger than the earlier one.
00371   if (Later.Size == AliasAnalysis::UnknownSize ||
00372       Earlier.Size == AliasAnalysis::UnknownSize ||
00373       AA.getDataLayout() == 0)
00374     return OverwriteUnknown;
00375 
00376   // Check to see if the later store is to the entire object (either a global,
00377   // an alloca, or a byval argument).  If so, then it clearly overwrites any
00378   // other store to the same object.
00379   const DataLayout *TD = AA.getDataLayout();
00380 
00381   const Value *UO1 = GetUnderlyingObject(P1, TD),
00382               *UO2 = GetUnderlyingObject(P2, TD);
00383 
00384   // If we can't resolve the same pointers to the same object, then we can't
00385   // analyze them at all.
00386   if (UO1 != UO2)
00387     return OverwriteUnknown;
00388 
00389   // If the "Later" store is to a recognizable object, get its size.
00390   uint64_t ObjectSize = getPointerSize(UO2, AA);
00391   if (ObjectSize != AliasAnalysis::UnknownSize)
00392     if (ObjectSize == Later.Size && ObjectSize >= Earlier.Size)
00393       return OverwriteComplete;
00394 
00395   // Okay, we have stores to two completely different pointers.  Try to
00396   // decompose the pointer into a "base + constant_offset" form.  If the base
00397   // pointers are equal, then we can reason about the two stores.
00398   EarlierOff = 0;
00399   LaterOff = 0;
00400   const Value *BP1 = GetPointerBaseWithConstantOffset(P1, EarlierOff, TD);
00401   const Value *BP2 = GetPointerBaseWithConstantOffset(P2, LaterOff, TD);
00402 
00403   // If the base pointers still differ, we have two completely different stores.
00404   if (BP1 != BP2)
00405     return OverwriteUnknown;
00406 
00407   // The later store completely overlaps the earlier store if:
00408   //
00409   // 1. Both start at the same offset and the later one's size is greater than
00410   //    or equal to the earlier one's, or
00411   //
00412   //      |--earlier--|
00413   //      |--   later   --|
00414   //
00415   // 2. The earlier store has an offset greater than the later offset, but which
00416   //    still lies completely within the later store.
00417   //
00418   //        |--earlier--|
00419   //    |-----  later  ------|
00420   //
00421   // We have to be careful here as *Off is signed while *.Size is unsigned.
00422   if (EarlierOff >= LaterOff &&
00423       Later.Size >= Earlier.Size &&
00424       uint64_t(EarlierOff - LaterOff) + Earlier.Size <= Later.Size)
00425     return OverwriteComplete;
00426 
00427   // The other interesting case is if the later store overwrites the end of
00428   // the earlier store
00429   //
00430   //      |--earlier--|
00431   //                |--   later   --|
00432   //
00433   // In this case we may want to trim the size of earlier to avoid generating
00434   // writes to addresses which will definitely be overwritten later
00435   if (LaterOff > EarlierOff &&
00436       LaterOff < int64_t(EarlierOff + Earlier.Size) &&
00437       int64_t(LaterOff + Later.Size) >= int64_t(EarlierOff + Earlier.Size))
00438     return OverwriteEnd;
00439 
00440   // Otherwise, they don't completely overlap.
00441   return OverwriteUnknown;
00442 }
00443 
00444 /// isPossibleSelfRead - If 'Inst' might be a self read (i.e. a noop copy of a
00445 /// memory region into an identical pointer) then it doesn't actually make its
00446 /// input dead in the traditional sense.  Consider this case:
00447 ///
00448 ///   memcpy(A <- B)
00449 ///   memcpy(A <- A)
00450 ///
00451 /// In this case, the second store to A does not make the first store to A dead.
00452 /// The usual situation isn't an explicit A<-A store like this (which can be
00453 /// trivially removed) but a case where two pointers may alias.
00454 ///
00455 /// This function detects when it is unsafe to remove a dependent instruction
00456 /// because the DSE inducing instruction may be a self-read.
00457 static bool isPossibleSelfRead(Instruction *Inst,
00458                                const AliasAnalysis::Location &InstStoreLoc,
00459                                Instruction *DepWrite, AliasAnalysis &AA) {
00460   // Self reads can only happen for instructions that read memory.  Get the
00461   // location read.
00462   AliasAnalysis::Location InstReadLoc = getLocForRead(Inst, AA);
00463   if (InstReadLoc.Ptr == 0) return false;  // Not a reading instruction.
00464 
00465   // If the read and written loc obviously don't alias, it isn't a read.
00466   if (AA.isNoAlias(InstReadLoc, InstStoreLoc)) return false;
00467 
00468   // Okay, 'Inst' may copy over itself.  However, we can still remove a the
00469   // DepWrite instruction if we can prove that it reads from the same location
00470   // as Inst.  This handles useful cases like:
00471   //   memcpy(A <- B)
00472   //   memcpy(A <- B)
00473   // Here we don't know if A/B may alias, but we do know that B/B are must
00474   // aliases, so removing the first memcpy is safe (assuming it writes <= #
00475   // bytes as the second one.
00476   AliasAnalysis::Location DepReadLoc = getLocForRead(DepWrite, AA);
00477 
00478   if (DepReadLoc.Ptr && AA.isMustAlias(InstReadLoc.Ptr, DepReadLoc.Ptr))
00479     return false;
00480 
00481   // If DepWrite doesn't read memory or if we can't prove it is a must alias,
00482   // then it can't be considered dead.
00483   return true;
00484 }
00485 
00486 
00487 //===----------------------------------------------------------------------===//
00488 // DSE Pass
00489 //===----------------------------------------------------------------------===//
00490 
00491 bool DSE::runOnBasicBlock(BasicBlock &BB) {
00492   bool MadeChange = false;
00493 
00494   // Do a top-down walk on the BB.
00495   for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end(); BBI != BBE; ) {
00496     Instruction *Inst = BBI++;
00497 
00498     // Handle 'free' calls specially.
00499     if (CallInst *F = isFreeCall(Inst, TLI)) {
00500       MadeChange |= HandleFree(F);
00501       continue;
00502     }
00503 
00504     // If we find something that writes memory, get its memory dependence.
00505     if (!hasMemoryWrite(Inst, TLI))
00506       continue;
00507 
00508     MemDepResult InstDep = MD->getDependency(Inst);
00509 
00510     // Ignore any store where we can't find a local dependence.
00511     // FIXME: cross-block DSE would be fun. :)
00512     if (!InstDep.isDef() && !InstDep.isClobber())
00513       continue;
00514 
00515     // If we're storing the same value back to a pointer that we just
00516     // loaded from, then the store can be removed.
00517     if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
00518       if (LoadInst *DepLoad = dyn_cast<LoadInst>(InstDep.getInst())) {
00519         if (SI->getPointerOperand() == DepLoad->getPointerOperand() &&
00520             SI->getOperand(0) == DepLoad && isRemovable(SI)) {
00521           DEBUG(dbgs() << "DSE: Remove Store Of Load from same pointer:\n  "
00522                        << "LOAD: " << *DepLoad << "\n  STORE: " << *SI << '\n');
00523 
00524           // DeleteDeadInstruction can delete the current instruction.  Save BBI
00525           // in case we need it.
00526           WeakVH NextInst(BBI);
00527 
00528           DeleteDeadInstruction(SI, *MD, TLI);
00529 
00530           if (NextInst == 0)  // Next instruction deleted.
00531             BBI = BB.begin();
00532           else if (BBI != BB.begin())  // Revisit this instruction if possible.
00533             --BBI;
00534           ++NumFastStores;
00535           MadeChange = true;
00536           continue;
00537         }
00538       }
00539     }
00540 
00541     // Figure out what location is being stored to.
00542     AliasAnalysis::Location Loc = getLocForWrite(Inst, *AA);
00543 
00544     // If we didn't get a useful location, fail.
00545     if (Loc.Ptr == 0)
00546       continue;
00547 
00548     while (InstDep.isDef() || InstDep.isClobber()) {
00549       // Get the memory clobbered by the instruction we depend on.  MemDep will
00550       // skip any instructions that 'Loc' clearly doesn't interact with.  If we
00551       // end up depending on a may- or must-aliased load, then we can't optimize
00552       // away the store and we bail out.  However, if we depend on on something
00553       // that overwrites the memory location we *can* potentially optimize it.
00554       //
00555       // Find out what memory location the dependent instruction stores.
00556       Instruction *DepWrite = InstDep.getInst();
00557       AliasAnalysis::Location DepLoc = getLocForWrite(DepWrite, *AA);
00558       // If we didn't get a useful location, or if it isn't a size, bail out.
00559       if (DepLoc.Ptr == 0)
00560         break;
00561 
00562       // If we find a write that is a) removable (i.e., non-volatile), b) is
00563       // completely obliterated by the store to 'Loc', and c) which we know that
00564       // 'Inst' doesn't load from, then we can remove it.
00565       if (isRemovable(DepWrite) &&
00566           !isPossibleSelfRead(Inst, Loc, DepWrite, *AA)) {
00567         int64_t InstWriteOffset, DepWriteOffset;
00568         OverwriteResult OR = isOverwrite(Loc, DepLoc, *AA,
00569                                          DepWriteOffset, InstWriteOffset);
00570         if (OR == OverwriteComplete) {
00571           DEBUG(dbgs() << "DSE: Remove Dead Store:\n  DEAD: "
00572                 << *DepWrite << "\n  KILLER: " << *Inst << '\n');
00573 
00574           // Delete the store and now-dead instructions that feed it.
00575           DeleteDeadInstruction(DepWrite, *MD, TLI);
00576           ++NumFastStores;
00577           MadeChange = true;
00578 
00579           // DeleteDeadInstruction can delete the current instruction in loop
00580           // cases, reset BBI.
00581           BBI = Inst;
00582           if (BBI != BB.begin())
00583             --BBI;
00584           break;
00585         } else if (OR == OverwriteEnd && isShortenable(DepWrite)) {
00586           // TODO: base this on the target vector size so that if the earlier
00587           // store was too small to get vector writes anyway then its likely
00588           // a good idea to shorten it
00589           // Power of 2 vector writes are probably always a bad idea to optimize
00590           // as any store/memset/memcpy is likely using vector instructions so
00591           // shortening it to not vector size is likely to be slower
00592           MemIntrinsic* DepIntrinsic = cast<MemIntrinsic>(DepWrite);
00593           unsigned DepWriteAlign = DepIntrinsic->getAlignment();
00594           if (llvm::isPowerOf2_64(InstWriteOffset) ||
00595               ((DepWriteAlign != 0) && InstWriteOffset % DepWriteAlign == 0)) {
00596 
00597             DEBUG(dbgs() << "DSE: Remove Dead Store:\n  OW END: "
00598                   << *DepWrite << "\n  KILLER (offset "
00599                   << InstWriteOffset << ", "
00600                   << DepLoc.Size << ")"
00601                   << *Inst << '\n');
00602 
00603             Value* DepWriteLength = DepIntrinsic->getLength();
00604             Value* TrimmedLength = ConstantInt::get(DepWriteLength->getType(),
00605                                                     InstWriteOffset -
00606                                                     DepWriteOffset);
00607             DepIntrinsic->setLength(TrimmedLength);
00608             MadeChange = true;
00609           }
00610         }
00611       }
00612 
00613       // If this is a may-aliased store that is clobbering the store value, we
00614       // can keep searching past it for another must-aliased pointer that stores
00615       // to the same location.  For example, in:
00616       //   store -> P
00617       //   store -> Q
00618       //   store -> P
00619       // we can remove the first store to P even though we don't know if P and Q
00620       // alias.
00621       if (DepWrite == &BB.front()) break;
00622 
00623       // Can't look past this instruction if it might read 'Loc'.
00624       if (AA->getModRefInfo(DepWrite, Loc) & AliasAnalysis::Ref)
00625         break;
00626 
00627       InstDep = MD->getPointerDependencyFrom(Loc, false, DepWrite, &BB);
00628     }
00629   }
00630 
00631   // If this block ends in a return, unwind, or unreachable, all allocas are
00632   // dead at its end, which means stores to them are also dead.
00633   if (BB.getTerminator()->getNumSuccessors() == 0)
00634     MadeChange |= handleEndBlock(BB);
00635 
00636   return MadeChange;
00637 }
00638 
00639 /// Find all blocks that will unconditionally lead to the block BB and append
00640 /// them to F.
00641 static void FindUnconditionalPreds(SmallVectorImpl<BasicBlock *> &Blocks,
00642                                    BasicBlock *BB, DominatorTree *DT) {
00643   for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
00644     BasicBlock *Pred = *I;
00645     if (Pred == BB) continue;
00646     TerminatorInst *PredTI = Pred->getTerminator();
00647     if (PredTI->getNumSuccessors() != 1)
00648       continue;
00649 
00650     if (DT->isReachableFromEntry(Pred))
00651       Blocks.push_back(Pred);
00652   }
00653 }
00654 
00655 /// HandleFree - Handle frees of entire structures whose dependency is a store
00656 /// to a field of that structure.
00657 bool DSE::HandleFree(CallInst *F) {
00658   bool MadeChange = false;
00659 
00660   AliasAnalysis::Location Loc = AliasAnalysis::Location(F->getOperand(0));
00661   SmallVector<BasicBlock *, 16> Blocks;
00662   Blocks.push_back(F->getParent());
00663 
00664   while (!Blocks.empty()) {
00665     BasicBlock *BB = Blocks.pop_back_val();
00666     Instruction *InstPt = BB->getTerminator();
00667     if (BB == F->getParent()) InstPt = F;
00668 
00669     MemDepResult Dep = MD->getPointerDependencyFrom(Loc, false, InstPt, BB);
00670     while (Dep.isDef() || Dep.isClobber()) {
00671       Instruction *Dependency = Dep.getInst();
00672       if (!hasMemoryWrite(Dependency, TLI) || !isRemovable(Dependency))
00673         break;
00674 
00675       Value *DepPointer =
00676         GetUnderlyingObject(getStoredPointerOperand(Dependency));
00677 
00678       // Check for aliasing.
00679       if (!AA->isMustAlias(F->getArgOperand(0), DepPointer))
00680         break;
00681 
00682       Instruction *Next = llvm::next(BasicBlock::iterator(Dependency));
00683 
00684       // DCE instructions only used to calculate that store
00685       DeleteDeadInstruction(Dependency, *MD, TLI);
00686       ++NumFastStores;
00687       MadeChange = true;
00688 
00689       // Inst's old Dependency is now deleted. Compute the next dependency,
00690       // which may also be dead, as in
00691       //    s[0] = 0;
00692       //    s[1] = 0; // This has just been deleted.
00693       //    free(s);
00694       Dep = MD->getPointerDependencyFrom(Loc, false, Next, BB);
00695     }
00696 
00697     if (Dep.isNonLocal())
00698       FindUnconditionalPreds(Blocks, BB, DT);
00699   }
00700 
00701   return MadeChange;
00702 }
00703 
00704 namespace {
00705   struct CouldRef {
00706     typedef Value *argument_type;
00707     const CallSite CS;
00708     AliasAnalysis *AA;
00709 
00710     bool operator()(Value *I) {
00711       // See if the call site touches the value.
00712       AliasAnalysis::ModRefResult A =
00713         AA->getModRefInfo(CS, I, getPointerSize(I, *AA));
00714 
00715       return A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref;
00716     }
00717   };
00718 }
00719 
00720 /// handleEndBlock - Remove dead stores to stack-allocated locations in the
00721 /// function end block.  Ex:
00722 /// %A = alloca i32
00723 /// ...
00724 /// store i32 1, i32* %A
00725 /// ret void
00726 bool DSE::handleEndBlock(BasicBlock &BB) {
00727   bool MadeChange = false;
00728 
00729   // Keep track of all of the stack objects that are dead at the end of the
00730   // function.
00731   SmallSetVector<Value*, 16> DeadStackObjects;
00732 
00733   // Find all of the alloca'd pointers in the entry block.
00734   BasicBlock *Entry = BB.getParent()->begin();
00735   for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I) {
00736     if (isa<AllocaInst>(I))
00737       DeadStackObjects.insert(I);
00738 
00739     // Okay, so these are dead heap objects, but if the pointer never escapes
00740     // then it's leaked by this function anyways.
00741     else if (isAllocLikeFn(I, TLI) && !PointerMayBeCaptured(I, true, true))
00742       DeadStackObjects.insert(I);
00743   }
00744 
00745   // Treat byval arguments the same, stores to them are dead at the end of the
00746   // function.
00747   for (Function::arg_iterator AI = BB.getParent()->arg_begin(),
00748        AE = BB.getParent()->arg_end(); AI != AE; ++AI)
00749     if (AI->hasByValAttr())
00750       DeadStackObjects.insert(AI);
00751 
00752   // Scan the basic block backwards
00753   for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
00754     --BBI;
00755 
00756     // If we find a store, check to see if it points into a dead stack value.
00757     if (hasMemoryWrite(BBI, TLI) && isRemovable(BBI)) {
00758       // See through pointer-to-pointer bitcasts
00759       SmallVector<Value *, 4> Pointers;
00760       GetUnderlyingObjects(getStoredPointerOperand(BBI), Pointers);
00761 
00762       // Stores to stack values are valid candidates for removal.
00763       bool AllDead = true;
00764       for (SmallVectorImpl<Value *>::iterator I = Pointers.begin(),
00765            E = Pointers.end(); I != E; ++I)
00766         if (!DeadStackObjects.count(*I)) {
00767           AllDead = false;
00768           break;
00769         }
00770 
00771       if (AllDead) {
00772         Instruction *Dead = BBI++;
00773 
00774         DEBUG(dbgs() << "DSE: Dead Store at End of Block:\n  DEAD: "
00775                      << *Dead << "\n  Objects: ";
00776               for (SmallVectorImpl<Value *>::iterator I = Pointers.begin(),
00777                    E = Pointers.end(); I != E; ++I) {
00778                 dbgs() << **I;
00779                 if (llvm::next(I) != E)
00780                   dbgs() << ", ";
00781               }
00782               dbgs() << '\n');
00783 
00784         // DCE instructions only used to calculate that store.
00785         DeleteDeadInstruction(Dead, *MD, TLI, &DeadStackObjects);
00786         ++NumFastStores;
00787         MadeChange = true;
00788         continue;
00789       }
00790     }
00791 
00792     // Remove any dead non-memory-mutating instructions.
00793     if (isInstructionTriviallyDead(BBI, TLI)) {
00794       Instruction *Inst = BBI++;
00795       DeleteDeadInstruction(Inst, *MD, TLI, &DeadStackObjects);
00796       ++NumFastOther;
00797       MadeChange = true;
00798       continue;
00799     }
00800 
00801     if (isa<AllocaInst>(BBI)) {
00802       // Remove allocas from the list of dead stack objects; there can't be
00803       // any references before the definition.
00804       DeadStackObjects.remove(BBI);
00805       continue;
00806     }
00807 
00808     if (CallSite CS = cast<Value>(BBI)) {
00809       // Remove allocation function calls from the list of dead stack objects; 
00810       // there can't be any references before the definition.
00811       if (isAllocLikeFn(BBI, TLI))
00812         DeadStackObjects.remove(BBI);
00813 
00814       // If this call does not access memory, it can't be loading any of our
00815       // pointers.
00816       if (AA->doesNotAccessMemory(CS))
00817         continue;
00818 
00819       // If the call might load from any of our allocas, then any store above
00820       // the call is live.
00821       CouldRef Pred = { CS, AA };
00822       DeadStackObjects.remove_if(Pred);
00823 
00824       // If all of the allocas were clobbered by the call then we're not going
00825       // to find anything else to process.
00826       if (DeadStackObjects.empty())
00827         break;
00828 
00829       continue;
00830     }
00831 
00832     AliasAnalysis::Location LoadedLoc;
00833 
00834     // If we encounter a use of the pointer, it is no longer considered dead
00835     if (LoadInst *L = dyn_cast<LoadInst>(BBI)) {
00836       if (!L->isUnordered()) // Be conservative with atomic/volatile load
00837         break;
00838       LoadedLoc = AA->getLocation(L);
00839     } else if (VAArgInst *V = dyn_cast<VAArgInst>(BBI)) {
00840       LoadedLoc = AA->getLocation(V);
00841     } else if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(BBI)) {
00842       LoadedLoc = AA->getLocationForSource(MTI);
00843     } else if (!BBI->mayReadFromMemory()) {
00844       // Instruction doesn't read memory.  Note that stores that weren't removed
00845       // above will hit this case.
00846       continue;
00847     } else {
00848       // Unknown inst; assume it clobbers everything.
00849       break;
00850     }
00851 
00852     // Remove any allocas from the DeadPointer set that are loaded, as this
00853     // makes any stores above the access live.
00854     RemoveAccessedObjects(LoadedLoc, DeadStackObjects);
00855 
00856     // If all of the allocas were clobbered by the access then we're not going
00857     // to find anything else to process.
00858     if (DeadStackObjects.empty())
00859       break;
00860   }
00861 
00862   return MadeChange;
00863 }
00864 
00865 namespace {
00866   struct CouldAlias {
00867     typedef Value *argument_type;
00868     const AliasAnalysis::Location &LoadedLoc;
00869     AliasAnalysis *AA;
00870 
00871     bool operator()(Value *I) {
00872       // See if the loaded location could alias the stack location.
00873       AliasAnalysis::Location StackLoc(I, getPointerSize(I, *AA));
00874       return !AA->isNoAlias(StackLoc, LoadedLoc);
00875     }
00876   };
00877 }
00878 
00879 /// RemoveAccessedObjects - Check to see if the specified location may alias any
00880 /// of the stack objects in the DeadStackObjects set.  If so, they become live
00881 /// because the location is being loaded.
00882 void DSE::RemoveAccessedObjects(const AliasAnalysis::Location &LoadedLoc,
00883                                 SmallSetVector<Value*, 16> &DeadStackObjects) {
00884   const Value *UnderlyingPointer = GetUnderlyingObject(LoadedLoc.Ptr);
00885 
00886   // A constant can't be in the dead pointer set.
00887   if (isa<Constant>(UnderlyingPointer))
00888     return;
00889 
00890   // If the kill pointer can be easily reduced to an alloca, don't bother doing
00891   // extraneous AA queries.
00892   if (isa<AllocaInst>(UnderlyingPointer) || isa<Argument>(UnderlyingPointer)) {
00893     DeadStackObjects.remove(const_cast<Value*>(UnderlyingPointer));
00894     return;
00895   }
00896 
00897   // Remove objects that could alias LoadedLoc.
00898   CouldAlias Pred = { LoadedLoc, AA };
00899   DeadStackObjects.remove_if(Pred);
00900 }