1193323Sed//===- DeadStoreElimination.cpp - Fast Dead Store Elimination -------------===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file implements a trivial dead store elimination that only considers 11193323Sed// basic-block local redundant stores. 12193323Sed// 13193323Sed// FIXME: This should eventually be extended to be a post-dominator tree 14193323Sed// traversal. Doing so would be pretty trivial. 15193323Sed// 16193323Sed//===----------------------------------------------------------------------===// 17193323Sed 18193323Sed#define DEBUG_TYPE "dse" 19193323Sed#include "llvm/Transforms/Scalar.h" 20249423Sdim#include "llvm/ADT/STLExtras.h" 21249423Sdim#include "llvm/ADT/SetVector.h" 22249423Sdim#include "llvm/ADT/Statistic.h" 23193323Sed#include "llvm/Analysis/AliasAnalysis.h" 24234353Sdim#include "llvm/Analysis/CaptureTracking.h" 25193323Sed#include "llvm/Analysis/Dominators.h" 26198892Srdivacky#include "llvm/Analysis/MemoryBuiltins.h" 27193323Sed#include "llvm/Analysis/MemoryDependenceAnalysis.h" 28218893Sdim#include "llvm/Analysis/ValueTracking.h" 29249423Sdim#include "llvm/IR/Constants.h" 30249423Sdim#include "llvm/IR/DataLayout.h" 31249423Sdim#include "llvm/IR/Function.h" 32249423Sdim#include "llvm/IR/GlobalVariable.h" 33249423Sdim#include "llvm/IR/Instructions.h" 34249423Sdim#include "llvm/IR/IntrinsicInst.h" 35249423Sdim#include "llvm/Pass.h" 36249423Sdim#include "llvm/Support/Debug.h" 37243830Sdim#include "llvm/Target/TargetLibraryInfo.h" 38193323Sed#include "llvm/Transforms/Utils/Local.h" 39193323Sedusing namespace llvm; 40193323Sed 41193323SedSTATISTIC(NumFastStores, "Number of stores deleted"); 42193323SedSTATISTIC(NumFastOther , "Number of other instrs removed"); 43193323Sed 44193323Sednamespace { 45198090Srdivacky struct DSE : public FunctionPass { 46218893Sdim AliasAnalysis *AA; 47218893Sdim MemoryDependenceAnalysis *MD; 48234353Sdim DominatorTree *DT; 49243830Sdim const TargetLibraryInfo *TLI; 50198090Srdivacky 51193323Sed static char ID; // Pass identification, replacement for typeid 52234353Sdim DSE() : FunctionPass(ID), AA(0), MD(0), DT(0) { 53218893Sdim initializeDSEPass(*PassRegistry::getPassRegistry()); 54218893Sdim } 55193323Sed 56193323Sed virtual bool runOnFunction(Function &F) { 57218893Sdim AA = &getAnalysis<AliasAnalysis>(); 58218893Sdim MD = &getAnalysis<MemoryDependenceAnalysis>(); 59234353Sdim DT = &getAnalysis<DominatorTree>(); 60243830Sdim TLI = AA->getTargetLibraryInfo(); 61226633Sdim 62218893Sdim bool Changed = false; 63193323Sed for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) 64203954Srdivacky // Only check non-dead blocks. Dead blocks may have strange pointer 65203954Srdivacky // cycles that will confuse alias analysis. 66234353Sdim if (DT->isReachableFromEntry(I)) 67203954Srdivacky Changed |= runOnBasicBlock(*I); 68226633Sdim 69234353Sdim AA = 0; MD = 0; DT = 0; 70193323Sed return Changed; 71193323Sed } 72226633Sdim 73193323Sed bool runOnBasicBlock(BasicBlock &BB); 74218893Sdim bool HandleFree(CallInst *F); 75193323Sed bool handleEndBlock(BasicBlock &BB); 76218893Sdim void RemoveAccessedObjects(const AliasAnalysis::Location &LoadedLoc, 77239462Sdim SmallSetVector<Value*, 16> &DeadStackObjects); 78193323Sed 79193323Sed virtual void getAnalysisUsage(AnalysisUsage &AU) const { 80193323Sed AU.setPreservesCFG(); 81193323Sed AU.addRequired<DominatorTree>(); 82193323Sed AU.addRequired<AliasAnalysis>(); 83193323Sed AU.addRequired<MemoryDependenceAnalysis>(); 84218893Sdim AU.addPreserved<AliasAnalysis>(); 85193323Sed AU.addPreserved<DominatorTree>(); 86193323Sed AU.addPreserved<MemoryDependenceAnalysis>(); 87193323Sed } 88193323Sed }; 89193323Sed} 90193323Sed 91193323Sedchar DSE::ID = 0; 92218893SdimINITIALIZE_PASS_BEGIN(DSE, "dse", "Dead Store Elimination", false, false) 93218893SdimINITIALIZE_PASS_DEPENDENCY(DominatorTree) 94218893SdimINITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis) 95218893SdimINITIALIZE_AG_DEPENDENCY(AliasAnalysis) 96218893SdimINITIALIZE_PASS_END(DSE, "dse", "Dead Store Elimination", false, false) 97193323Sed 98193323SedFunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); } 99193323Sed 100218893Sdim//===----------------------------------------------------------------------===// 101218893Sdim// Helper functions 102218893Sdim//===----------------------------------------------------------------------===// 103218893Sdim 104218893Sdim/// DeleteDeadInstruction - Delete this instruction. Before we do, go through 105218893Sdim/// and zero out all the operands of this instruction. If any of them become 106218893Sdim/// dead, delete them and the computation tree that feeds them. 107218893Sdim/// 108218893Sdim/// If ValueSet is non-null, remove any deleted instructions from it as well. 109218893Sdim/// 110218893Sdimstatic void DeleteDeadInstruction(Instruction *I, 111218893Sdim MemoryDependenceAnalysis &MD, 112243830Sdim const TargetLibraryInfo *TLI, 113239462Sdim SmallSetVector<Value*, 16> *ValueSet = 0) { 114218893Sdim SmallVector<Instruction*, 32> NowDeadInsts; 115226633Sdim 116218893Sdim NowDeadInsts.push_back(I); 117218893Sdim --NumFastOther; 118226633Sdim 119218893Sdim // Before we touch this instruction, remove it from memdep! 120218893Sdim do { 121218893Sdim Instruction *DeadInst = NowDeadInsts.pop_back_val(); 122218893Sdim ++NumFastOther; 123226633Sdim 124218893Sdim // This instruction is dead, zap it, in stages. Start by removing it from 125218893Sdim // MemDep, which needs to know the operands and needs it to be in the 126218893Sdim // function. 127218893Sdim MD.removeInstruction(DeadInst); 128226633Sdim 129218893Sdim for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) { 130218893Sdim Value *Op = DeadInst->getOperand(op); 131218893Sdim DeadInst->setOperand(op, 0); 132226633Sdim 133218893Sdim // If this operand just became dead, add it to the NowDeadInsts list. 134218893Sdim if (!Op->use_empty()) continue; 135226633Sdim 136218893Sdim if (Instruction *OpI = dyn_cast<Instruction>(Op)) 137243830Sdim if (isInstructionTriviallyDead(OpI, TLI)) 138218893Sdim NowDeadInsts.push_back(OpI); 139218893Sdim } 140226633Sdim 141218893Sdim DeadInst->eraseFromParent(); 142226633Sdim 143239462Sdim if (ValueSet) ValueSet->remove(DeadInst); 144218893Sdim } while (!NowDeadInsts.empty()); 145218893Sdim} 146218893Sdim 147218893Sdim 148218893Sdim/// hasMemoryWrite - Does this instruction write some memory? This only returns 149218893Sdim/// true for things that we can analyze with other helpers below. 150243830Sdimstatic bool hasMemoryWrite(Instruction *I, const TargetLibraryInfo *TLI) { 151199481Srdivacky if (isa<StoreInst>(I)) 152199481Srdivacky return true; 153199481Srdivacky if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { 154199481Srdivacky switch (II->getIntrinsicID()) { 155200581Srdivacky default: 156200581Srdivacky return false; 157200581Srdivacky case Intrinsic::memset: 158200581Srdivacky case Intrinsic::memmove: 159200581Srdivacky case Intrinsic::memcpy: 160200581Srdivacky case Intrinsic::init_trampoline: 161200581Srdivacky case Intrinsic::lifetime_end: 162200581Srdivacky return true; 163199481Srdivacky } 164199481Srdivacky } 165243830Sdim if (CallSite CS = I) { 166243830Sdim if (Function *F = CS.getCalledFunction()) { 167243830Sdim if (TLI && TLI->has(LibFunc::strcpy) && 168243830Sdim F->getName() == TLI->getName(LibFunc::strcpy)) { 169243830Sdim return true; 170243830Sdim } 171243830Sdim if (TLI && TLI->has(LibFunc::strncpy) && 172243830Sdim F->getName() == TLI->getName(LibFunc::strncpy)) { 173243830Sdim return true; 174243830Sdim } 175243830Sdim if (TLI && TLI->has(LibFunc::strcat) && 176243830Sdim F->getName() == TLI->getName(LibFunc::strcat)) { 177243830Sdim return true; 178243830Sdim } 179243830Sdim if (TLI && TLI->has(LibFunc::strncat) && 180243830Sdim F->getName() == TLI->getName(LibFunc::strncat)) { 181243830Sdim return true; 182243830Sdim } 183243830Sdim } 184243830Sdim } 185199481Srdivacky return false; 186199481Srdivacky} 187199481Srdivacky 188218893Sdim/// getLocForWrite - Return a Location stored to by the specified instruction. 189226633Sdim/// If isRemovable returns true, this function and getLocForRead completely 190226633Sdim/// describe the memory operations for this instruction. 191218893Sdimstatic AliasAnalysis::Location 192218893SdimgetLocForWrite(Instruction *Inst, AliasAnalysis &AA) { 193218893Sdim if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) 194218893Sdim return AA.getLocation(SI); 195226633Sdim 196218893Sdim if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(Inst)) { 197218893Sdim // memcpy/memmove/memset. 198218893Sdim AliasAnalysis::Location Loc = AA.getLocationForDest(MI); 199218893Sdim // If we don't have target data around, an unknown size in Location means 200218893Sdim // that we should use the size of the pointee type. This isn't valid for 201218893Sdim // memset/memcpy, which writes more than an i8. 202243830Sdim if (Loc.Size == AliasAnalysis::UnknownSize && AA.getDataLayout() == 0) 203218893Sdim return AliasAnalysis::Location(); 204218893Sdim return Loc; 205218893Sdim } 206226633Sdim 207218893Sdim IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst); 208218893Sdim if (II == 0) return AliasAnalysis::Location(); 209226633Sdim 210218893Sdim switch (II->getIntrinsicID()) { 211218893Sdim default: return AliasAnalysis::Location(); // Unhandled intrinsic. 212218893Sdim case Intrinsic::init_trampoline: 213218893Sdim // If we don't have target data around, an unknown size in Location means 214218893Sdim // that we should use the size of the pointee type. This isn't valid for 215218893Sdim // init.trampoline, which writes more than an i8. 216243830Sdim if (AA.getDataLayout() == 0) return AliasAnalysis::Location(); 217226633Sdim 218218893Sdim // FIXME: We don't know the size of the trampoline, so we can't really 219218893Sdim // handle it here. 220218893Sdim return AliasAnalysis::Location(II->getArgOperand(0)); 221218893Sdim case Intrinsic::lifetime_end: { 222218893Sdim uint64_t Len = cast<ConstantInt>(II->getArgOperand(0))->getZExtValue(); 223218893Sdim return AliasAnalysis::Location(II->getArgOperand(1), Len); 224218893Sdim } 225218893Sdim } 226218893Sdim} 227218893Sdim 228218893Sdim/// getLocForRead - Return the location read by the specified "hasMemoryWrite" 229218893Sdim/// instruction if any. 230226633Sdimstatic AliasAnalysis::Location 231218893SdimgetLocForRead(Instruction *Inst, AliasAnalysis &AA) { 232243830Sdim assert(hasMemoryWrite(Inst, AA.getTargetLibraryInfo()) && 233243830Sdim "Unknown instruction case"); 234226633Sdim 235218893Sdim // The only instructions that both read and write are the mem transfer 236218893Sdim // instructions (memcpy/memmove). 237218893Sdim if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(Inst)) 238218893Sdim return AA.getLocationForSource(MTI); 239218893Sdim return AliasAnalysis::Location(); 240218893Sdim} 241218893Sdim 242218893Sdim 243218893Sdim/// isRemovable - If the value of this instruction and the memory it writes to 244218893Sdim/// is unused, may we delete this instruction? 245218893Sdimstatic bool isRemovable(Instruction *I) { 246226633Sdim // Don't remove volatile/atomic stores. 247199481Srdivacky if (StoreInst *SI = dyn_cast<StoreInst>(I)) 248226633Sdim return SI->isUnordered(); 249226633Sdim 250243830Sdim if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { 251243830Sdim switch (II->getIntrinsicID()) { 252243830Sdim default: llvm_unreachable("doesn't pass 'hasMemoryWrite' predicate"); 253243830Sdim case Intrinsic::lifetime_end: 254243830Sdim // Never remove dead lifetime_end's, e.g. because it is followed by a 255243830Sdim // free. 256243830Sdim return false; 257243830Sdim case Intrinsic::init_trampoline: 258243830Sdim // Always safe to remove init_trampoline. 259243830Sdim return true; 260226633Sdim 261243830Sdim case Intrinsic::memset: 262243830Sdim case Intrinsic::memmove: 263243830Sdim case Intrinsic::memcpy: 264243830Sdim // Don't remove volatile memory intrinsics. 265243830Sdim return !cast<MemIntrinsic>(II)->isVolatile(); 266243830Sdim } 267218893Sdim } 268243830Sdim 269243830Sdim if (CallSite CS = I) 270243830Sdim return CS.getInstruction()->use_empty(); 271243830Sdim 272243830Sdim return false; 273199481Srdivacky} 274199481Srdivacky 275234353Sdim 276234353Sdim/// isShortenable - Returns true if this instruction can be safely shortened in 277234353Sdim/// length. 278234353Sdimstatic bool isShortenable(Instruction *I) { 279234353Sdim // Don't shorten stores for now 280234353Sdim if (isa<StoreInst>(I)) 281234353Sdim return false; 282239462Sdim 283243830Sdim if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { 284243830Sdim switch (II->getIntrinsicID()) { 285243830Sdim default: return false; 286243830Sdim case Intrinsic::memset: 287243830Sdim case Intrinsic::memcpy: 288243830Sdim // Do shorten memory intrinsics. 289243830Sdim return true; 290243830Sdim } 291234353Sdim } 292243830Sdim 293243830Sdim // Don't shorten libcalls calls for now. 294243830Sdim 295243830Sdim return false; 296234353Sdim} 297234353Sdim 298218893Sdim/// getStoredPointerOperand - Return the pointer that is being written to. 299218893Sdimstatic Value *getStoredPointerOperand(Instruction *I) { 300199481Srdivacky if (StoreInst *SI = dyn_cast<StoreInst>(I)) 301199481Srdivacky return SI->getPointerOperand(); 302199481Srdivacky if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) 303218893Sdim return MI->getDest(); 304210299Sed 305243830Sdim if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { 306243830Sdim switch (II->getIntrinsicID()) { 307243830Sdim default: llvm_unreachable("Unexpected intrinsic!"); 308243830Sdim case Intrinsic::init_trampoline: 309243830Sdim return II->getArgOperand(0); 310243830Sdim } 311199481Srdivacky } 312243830Sdim 313243830Sdim CallSite CS = I; 314243830Sdim // All the supported functions so far happen to have dest as their first 315243830Sdim // argument. 316243830Sdim return CS.getArgument(0); 317199481Srdivacky} 318199481Srdivacky 319234353Sdimstatic uint64_t getPointerSize(const Value *V, AliasAnalysis &AA) { 320239462Sdim uint64_t Size; 321243830Sdim if (getObjectSize(V, Size, AA.getDataLayout(), AA.getTargetLibraryInfo())) 322239462Sdim return Size; 323234353Sdim return AliasAnalysis::UnknownSize; 324218893Sdim} 325199481Srdivacky 326234353Sdimnamespace { 327234353Sdim enum OverwriteResult 328234353Sdim { 329234353Sdim OverwriteComplete, 330234353Sdim OverwriteEnd, 331234353Sdim OverwriteUnknown 332234353Sdim }; 333218893Sdim} 334218893Sdim 335234353Sdim/// isOverwrite - Return 'OverwriteComplete' if a store to the 'Later' location 336218893Sdim/// completely overwrites a store to the 'Earlier' location. 337239462Sdim/// 'OverwriteEnd' if the end of the 'Earlier' location is completely 338234353Sdim/// overwritten by 'Later', or 'OverwriteUnknown' if nothing can be determined 339234353Sdimstatic OverwriteResult isOverwrite(const AliasAnalysis::Location &Later, 340234353Sdim const AliasAnalysis::Location &Earlier, 341234353Sdim AliasAnalysis &AA, 342234353Sdim int64_t &EarlierOff, 343234353Sdim int64_t &LaterOff) { 344218893Sdim const Value *P1 = Earlier.Ptr->stripPointerCasts(); 345218893Sdim const Value *P2 = Later.Ptr->stripPointerCasts(); 346226633Sdim 347218893Sdim // If the start pointers are the same, we just have to compare sizes to see if 348218893Sdim // the later store was larger than the earlier store. 349218893Sdim if (P1 == P2) { 350218893Sdim // If we don't know the sizes of either access, then we can't do a 351218893Sdim // comparison. 352218893Sdim if (Later.Size == AliasAnalysis::UnknownSize || 353218893Sdim Earlier.Size == AliasAnalysis::UnknownSize) { 354243830Sdim // If we have no DataLayout information around, then the size of the store 355218893Sdim // is inferrable from the pointee type. If they are the same type, then 356218893Sdim // we know that the store is safe. 357243830Sdim if (AA.getDataLayout() == 0 && 358234353Sdim Later.Ptr->getType() == Earlier.Ptr->getType()) 359234353Sdim return OverwriteComplete; 360239462Sdim 361234353Sdim return OverwriteUnknown; 362199481Srdivacky } 363226633Sdim 364218893Sdim // Make sure that the Later size is >= the Earlier size. 365234353Sdim if (Later.Size >= Earlier.Size) 366234353Sdim return OverwriteComplete; 367199481Srdivacky } 368226633Sdim 369218893Sdim // Otherwise, we have to have size information, and the later store has to be 370218893Sdim // larger than the earlier one. 371218893Sdim if (Later.Size == AliasAnalysis::UnknownSize || 372218893Sdim Earlier.Size == AliasAnalysis::UnknownSize || 373243830Sdim AA.getDataLayout() == 0) 374234353Sdim return OverwriteUnknown; 375226633Sdim 376218893Sdim // Check to see if the later store is to the entire object (either a global, 377218893Sdim // an alloca, or a byval argument). If so, then it clearly overwrites any 378218893Sdim // other store to the same object. 379249423Sdim const DataLayout *TD = AA.getDataLayout(); 380226633Sdim 381249423Sdim const Value *UO1 = GetUnderlyingObject(P1, TD), 382249423Sdim *UO2 = GetUnderlyingObject(P2, TD); 383226633Sdim 384218893Sdim // If we can't resolve the same pointers to the same object, then we can't 385218893Sdim // analyze them at all. 386218893Sdim if (UO1 != UO2) 387234353Sdim return OverwriteUnknown; 388226633Sdim 389218893Sdim // If the "Later" store is to a recognizable object, get its size. 390234353Sdim uint64_t ObjectSize = getPointerSize(UO2, AA); 391234353Sdim if (ObjectSize != AliasAnalysis::UnknownSize) 392234353Sdim if (ObjectSize == Later.Size && ObjectSize >= Earlier.Size) 393234353Sdim return OverwriteComplete; 394226633Sdim 395218893Sdim // Okay, we have stores to two completely different pointers. Try to 396218893Sdim // decompose the pointer into a "base + constant_offset" form. If the base 397218893Sdim // pointers are equal, then we can reason about the two stores. 398234353Sdim EarlierOff = 0; 399234353Sdim LaterOff = 0; 400221345Sdim const Value *BP1 = GetPointerBaseWithConstantOffset(P1, EarlierOff, TD); 401221345Sdim const Value *BP2 = GetPointerBaseWithConstantOffset(P2, LaterOff, TD); 402226633Sdim 403218893Sdim // If the base pointers still differ, we have two completely different stores. 404218893Sdim if (BP1 != BP2) 405234353Sdim return OverwriteUnknown; 406221345Sdim 407221345Sdim // The later store completely overlaps the earlier store if: 408226633Sdim // 409221345Sdim // 1. Both start at the same offset and the later one's size is greater than 410221345Sdim // or equal to the earlier one's, or 411221345Sdim // 412221345Sdim // |--earlier--| 413221345Sdim // |-- later --| 414226633Sdim // 415221345Sdim // 2. The earlier store has an offset greater than the later offset, but which 416221345Sdim // still lies completely within the later store. 417221345Sdim // 418221345Sdim // |--earlier--| 419221345Sdim // |----- later ------| 420221345Sdim // 421221345Sdim // We have to be careful here as *Off is signed while *.Size is unsigned. 422221345Sdim if (EarlierOff >= LaterOff && 423239462Sdim Later.Size >= Earlier.Size && 424221345Sdim uint64_t(EarlierOff - LaterOff) + Earlier.Size <= Later.Size) 425234353Sdim return OverwriteComplete; 426239462Sdim 427234353Sdim // The other interesting case is if the later store overwrites the end of 428234353Sdim // the earlier store 429234353Sdim // 430234353Sdim // |--earlier--| 431234353Sdim // |-- later --| 432234353Sdim // 433234353Sdim // In this case we may want to trim the size of earlier to avoid generating 434234353Sdim // writes to addresses which will definitely be overwritten later 435234353Sdim if (LaterOff > EarlierOff && 436234353Sdim LaterOff < int64_t(EarlierOff + Earlier.Size) && 437234353Sdim int64_t(LaterOff + Later.Size) >= int64_t(EarlierOff + Earlier.Size)) 438234353Sdim return OverwriteEnd; 439221345Sdim 440221345Sdim // Otherwise, they don't completely overlap. 441234353Sdim return OverwriteUnknown; 442199481Srdivacky} 443199481Srdivacky 444218893Sdim/// isPossibleSelfRead - If 'Inst' might be a self read (i.e. a noop copy of a 445218893Sdim/// memory region into an identical pointer) then it doesn't actually make its 446226633Sdim/// input dead in the traditional sense. Consider this case: 447198953Srdivacky/// 448218893Sdim/// memcpy(A <- B) 449218893Sdim/// memcpy(A <- A) 450218893Sdim/// 451218893Sdim/// In this case, the second store to A does not make the first store to A dead. 452218893Sdim/// The usual situation isn't an explicit A<-A store like this (which can be 453218893Sdim/// trivially removed) but a case where two pointers may alias. 454218893Sdim/// 455218893Sdim/// This function detects when it is unsafe to remove a dependent instruction 456218893Sdim/// because the DSE inducing instruction may be a self-read. 457218893Sdimstatic bool isPossibleSelfRead(Instruction *Inst, 458218893Sdim const AliasAnalysis::Location &InstStoreLoc, 459218893Sdim Instruction *DepWrite, AliasAnalysis &AA) { 460218893Sdim // Self reads can only happen for instructions that read memory. Get the 461218893Sdim // location read. 462218893Sdim AliasAnalysis::Location InstReadLoc = getLocForRead(Inst, AA); 463218893Sdim if (InstReadLoc.Ptr == 0) return false; // Not a reading instruction. 464226633Sdim 465218893Sdim // If the read and written loc obviously don't alias, it isn't a read. 466218893Sdim if (AA.isNoAlias(InstReadLoc, InstStoreLoc)) return false; 467226633Sdim 468218893Sdim // Okay, 'Inst' may copy over itself. However, we can still remove a the 469218893Sdim // DepWrite instruction if we can prove that it reads from the same location 470218893Sdim // as Inst. This handles useful cases like: 471218893Sdim // memcpy(A <- B) 472218893Sdim // memcpy(A <- B) 473218893Sdim // Here we don't know if A/B may alias, but we do know that B/B are must 474218893Sdim // aliases, so removing the first memcpy is safe (assuming it writes <= # 475218893Sdim // bytes as the second one. 476218893Sdim AliasAnalysis::Location DepReadLoc = getLocForRead(DepWrite, AA); 477226633Sdim 478218893Sdim if (DepReadLoc.Ptr && AA.isMustAlias(InstReadLoc.Ptr, DepReadLoc.Ptr)) 479218893Sdim return false; 480226633Sdim 481218893Sdim // If DepWrite doesn't read memory or if we can't prove it is a must alias, 482218893Sdim // then it can't be considered dead. 483218893Sdim return true; 484198953Srdivacky} 485198953Srdivacky 486218893Sdim 487218893Sdim//===----------------------------------------------------------------------===// 488218893Sdim// DSE Pass 489218893Sdim//===----------------------------------------------------------------------===// 490218893Sdim 491193323Sedbool DSE::runOnBasicBlock(BasicBlock &BB) { 492193323Sed bool MadeChange = false; 493226633Sdim 494198090Srdivacky // Do a top-down walk on the BB. 495193323Sed for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end(); BBI != BBE; ) { 496193323Sed Instruction *Inst = BBI++; 497226633Sdim 498218893Sdim // Handle 'free' calls specially. 499243830Sdim if (CallInst *F = isFreeCall(Inst, TLI)) { 500218893Sdim MadeChange |= HandleFree(F); 501193323Sed continue; 502193323Sed } 503226633Sdim 504218893Sdim // If we find something that writes memory, get its memory dependence. 505243830Sdim if (!hasMemoryWrite(Inst, TLI)) 506193323Sed continue; 507198090Srdivacky 508218893Sdim MemDepResult InstDep = MD->getDependency(Inst); 509226633Sdim 510224145Sdim // Ignore any store where we can't find a local dependence. 511218893Sdim // FIXME: cross-block DSE would be fun. :) 512226633Sdim if (!InstDep.isDef() && !InstDep.isClobber()) 513199481Srdivacky continue; 514226633Sdim 515193323Sed // If we're storing the same value back to a pointer that we just 516193323Sed // loaded from, then the store can be removed. 517199481Srdivacky if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { 518199481Srdivacky if (LoadInst *DepLoad = dyn_cast<LoadInst>(InstDep.getInst())) { 519199481Srdivacky if (SI->getPointerOperand() == DepLoad->getPointerOperand() && 520226633Sdim SI->getOperand(0) == DepLoad && isRemovable(SI)) { 521218893Sdim DEBUG(dbgs() << "DSE: Remove Store Of Load from same pointer:\n " 522218893Sdim << "LOAD: " << *DepLoad << "\n STORE: " << *SI << '\n'); 523226633Sdim 524199481Srdivacky // DeleteDeadInstruction can delete the current instruction. Save BBI 525199481Srdivacky // in case we need it. 526199481Srdivacky WeakVH NextInst(BBI); 527226633Sdim 528243830Sdim DeleteDeadInstruction(SI, *MD, TLI); 529226633Sdim 530199481Srdivacky if (NextInst == 0) // Next instruction deleted. 531199481Srdivacky BBI = BB.begin(); 532199481Srdivacky else if (BBI != BB.begin()) // Revisit this instruction if possible. 533199481Srdivacky --BBI; 534210299Sed ++NumFastStores; 535199481Srdivacky MadeChange = true; 536199481Srdivacky continue; 537199481Srdivacky } 538193323Sed } 539193323Sed } 540226633Sdim 541218893Sdim // Figure out what location is being stored to. 542218893Sdim AliasAnalysis::Location Loc = getLocForWrite(Inst, *AA); 543218893Sdim 544218893Sdim // If we didn't get a useful location, fail. 545218893Sdim if (Loc.Ptr == 0) 546218893Sdim continue; 547226633Sdim 548226633Sdim while (InstDep.isDef() || InstDep.isClobber()) { 549218893Sdim // Get the memory clobbered by the instruction we depend on. MemDep will 550218893Sdim // skip any instructions that 'Loc' clearly doesn't interact with. If we 551218893Sdim // end up depending on a may- or must-aliased load, then we can't optimize 552218893Sdim // away the store and we bail out. However, if we depend on on something 553218893Sdim // that overwrites the memory location we *can* potentially optimize it. 554218893Sdim // 555221345Sdim // Find out what memory location the dependent instruction stores. 556218893Sdim Instruction *DepWrite = InstDep.getInst(); 557218893Sdim AliasAnalysis::Location DepLoc = getLocForWrite(DepWrite, *AA); 558218893Sdim // If we didn't get a useful location, or if it isn't a size, bail out. 559218893Sdim if (DepLoc.Ptr == 0) 560218893Sdim break; 561218893Sdim 562218893Sdim // If we find a write that is a) removable (i.e., non-volatile), b) is 563218893Sdim // completely obliterated by the store to 'Loc', and c) which we know that 564218893Sdim // 'Inst' doesn't load from, then we can remove it. 565239462Sdim if (isRemovable(DepWrite) && 566218893Sdim !isPossibleSelfRead(Inst, Loc, DepWrite, *AA)) { 567239462Sdim int64_t InstWriteOffset, DepWriteOffset; 568239462Sdim OverwriteResult OR = isOverwrite(Loc, DepLoc, *AA, 569239462Sdim DepWriteOffset, InstWriteOffset); 570234353Sdim if (OR == OverwriteComplete) { 571234353Sdim DEBUG(dbgs() << "DSE: Remove Dead Store:\n DEAD: " 572234353Sdim << *DepWrite << "\n KILLER: " << *Inst << '\n'); 573226633Sdim 574234353Sdim // Delete the store and now-dead instructions that feed it. 575243830Sdim DeleteDeadInstruction(DepWrite, *MD, TLI); 576234353Sdim ++NumFastStores; 577234353Sdim MadeChange = true; 578239462Sdim 579234353Sdim // DeleteDeadInstruction can delete the current instruction in loop 580234353Sdim // cases, reset BBI. 581234353Sdim BBI = Inst; 582234353Sdim if (BBI != BB.begin()) 583234353Sdim --BBI; 584234353Sdim break; 585234353Sdim } else if (OR == OverwriteEnd && isShortenable(DepWrite)) { 586234353Sdim // TODO: base this on the target vector size so that if the earlier 587234353Sdim // store was too small to get vector writes anyway then its likely 588234353Sdim // a good idea to shorten it 589234353Sdim // Power of 2 vector writes are probably always a bad idea to optimize 590234353Sdim // as any store/memset/memcpy is likely using vector instructions so 591234353Sdim // shortening it to not vector size is likely to be slower 592234353Sdim MemIntrinsic* DepIntrinsic = cast<MemIntrinsic>(DepWrite); 593234353Sdim unsigned DepWriteAlign = DepIntrinsic->getAlignment(); 594234353Sdim if (llvm::isPowerOf2_64(InstWriteOffset) || 595234353Sdim ((DepWriteAlign != 0) && InstWriteOffset % DepWriteAlign == 0)) { 596239462Sdim 597234353Sdim DEBUG(dbgs() << "DSE: Remove Dead Store:\n OW END: " 598239462Sdim << *DepWrite << "\n KILLER (offset " 599239462Sdim << InstWriteOffset << ", " 600234353Sdim << DepLoc.Size << ")" 601234353Sdim << *Inst << '\n'); 602239462Sdim 603234353Sdim Value* DepWriteLength = DepIntrinsic->getLength(); 604234353Sdim Value* TrimmedLength = ConstantInt::get(DepWriteLength->getType(), 605239462Sdim InstWriteOffset - 606234353Sdim DepWriteOffset); 607234353Sdim DepIntrinsic->setLength(TrimmedLength); 608234353Sdim MadeChange = true; 609234353Sdim } 610234353Sdim } 611198892Srdivacky } 612226633Sdim 613218893Sdim // If this is a may-aliased store that is clobbering the store value, we 614218893Sdim // can keep searching past it for another must-aliased pointer that stores 615218893Sdim // to the same location. For example, in: 616218893Sdim // store -> P 617218893Sdim // store -> Q 618218893Sdim // store -> P 619218893Sdim // we can remove the first store to P even though we don't know if P and Q 620218893Sdim // alias. 621218893Sdim if (DepWrite == &BB.front()) break; 622226633Sdim 623218893Sdim // Can't look past this instruction if it might read 'Loc'. 624218893Sdim if (AA->getModRefInfo(DepWrite, Loc) & AliasAnalysis::Ref) 625218893Sdim break; 626226633Sdim 627218893Sdim InstDep = MD->getPointerDependencyFrom(Loc, false, DepWrite, &BB); 628198892Srdivacky } 629193323Sed } 630226633Sdim 631193323Sed // If this block ends in a return, unwind, or unreachable, all allocas are 632193323Sed // dead at its end, which means stores to them are also dead. 633193323Sed if (BB.getTerminator()->getNumSuccessors() == 0) 634193323Sed MadeChange |= handleEndBlock(BB); 635226633Sdim 636193323Sed return MadeChange; 637193323Sed} 638193323Sed 639234353Sdim/// Find all blocks that will unconditionally lead to the block BB and append 640234353Sdim/// them to F. 641234353Sdimstatic void FindUnconditionalPreds(SmallVectorImpl<BasicBlock *> &Blocks, 642234353Sdim BasicBlock *BB, DominatorTree *DT) { 643234353Sdim for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) { 644234353Sdim BasicBlock *Pred = *I; 645234353Sdim if (Pred == BB) continue; 646234353Sdim TerminatorInst *PredTI = Pred->getTerminator(); 647234353Sdim if (PredTI->getNumSuccessors() != 1) 648234353Sdim continue; 649234353Sdim 650234353Sdim if (DT->isReachableFromEntry(Pred)) 651234353Sdim Blocks.push_back(Pred); 652234353Sdim } 653234353Sdim} 654234353Sdim 655218893Sdim/// HandleFree - Handle frees of entire structures whose dependency is a store 656218893Sdim/// to a field of that structure. 657218893Sdimbool DSE::HandleFree(CallInst *F) { 658224145Sdim bool MadeChange = false; 659224145Sdim 660234353Sdim AliasAnalysis::Location Loc = AliasAnalysis::Location(F->getOperand(0)); 661234353Sdim SmallVector<BasicBlock *, 16> Blocks; 662234353Sdim Blocks.push_back(F->getParent()); 663224145Sdim 664234353Sdim while (!Blocks.empty()) { 665234353Sdim BasicBlock *BB = Blocks.pop_back_val(); 666234353Sdim Instruction *InstPt = BB->getTerminator(); 667234353Sdim if (BB == F->getParent()) InstPt = F; 668226633Sdim 669234353Sdim MemDepResult Dep = MD->getPointerDependencyFrom(Loc, false, InstPt, BB); 670234353Sdim while (Dep.isDef() || Dep.isClobber()) { 671234353Sdim Instruction *Dependency = Dep.getInst(); 672243830Sdim if (!hasMemoryWrite(Dependency, TLI) || !isRemovable(Dependency)) 673234353Sdim break; 674218893Sdim 675234353Sdim Value *DepPointer = 676234353Sdim GetUnderlyingObject(getStoredPointerOperand(Dependency)); 677226633Sdim 678234353Sdim // Check for aliasing. 679234353Sdim if (!AA->isMustAlias(F->getArgOperand(0), DepPointer)) 680234353Sdim break; 681193323Sed 682234353Sdim Instruction *Next = llvm::next(BasicBlock::iterator(Dependency)); 683226633Sdim 684234353Sdim // DCE instructions only used to calculate that store 685243830Sdim DeleteDeadInstruction(Dependency, *MD, TLI); 686234353Sdim ++NumFastStores; 687234353Sdim MadeChange = true; 688234353Sdim 689234353Sdim // Inst's old Dependency is now deleted. Compute the next dependency, 690234353Sdim // which may also be dead, as in 691234353Sdim // s[0] = 0; 692234353Sdim // s[1] = 0; // This has just been deleted. 693234353Sdim // free(s); 694234353Sdim Dep = MD->getPointerDependencyFrom(Loc, false, Next, BB); 695234353Sdim } 696234353Sdim 697234353Sdim if (Dep.isNonLocal()) 698234353Sdim FindUnconditionalPreds(Blocks, BB, DT); 699234353Sdim } 700234353Sdim 701224145Sdim return MadeChange; 702193323Sed} 703193323Sed 704243830Sdimnamespace { 705243830Sdim struct CouldRef { 706243830Sdim typedef Value *argument_type; 707243830Sdim const CallSite CS; 708243830Sdim AliasAnalysis *AA; 709243830Sdim 710243830Sdim bool operator()(Value *I) { 711243830Sdim // See if the call site touches the value. 712243830Sdim AliasAnalysis::ModRefResult A = 713243830Sdim AA->getModRefInfo(CS, I, getPointerSize(I, *AA)); 714243830Sdim 715243830Sdim return A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref; 716243830Sdim } 717243830Sdim }; 718243830Sdim} 719243830Sdim 720193323Sed/// handleEndBlock - Remove dead stores to stack-allocated locations in the 721193323Sed/// function end block. Ex: 722193323Sed/// %A = alloca i32 723193323Sed/// ... 724193323Sed/// store i32 1, i32* %A 725193323Sed/// ret void 726193323Sedbool DSE::handleEndBlock(BasicBlock &BB) { 727193323Sed bool MadeChange = false; 728226633Sdim 729218893Sdim // Keep track of all of the stack objects that are dead at the end of the 730218893Sdim // function. 731239462Sdim SmallSetVector<Value*, 16> DeadStackObjects; 732226633Sdim 733193323Sed // Find all of the alloca'd pointers in the entry block. 734193323Sed BasicBlock *Entry = BB.getParent()->begin(); 735234353Sdim for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I) { 736239462Sdim if (isa<AllocaInst>(I)) 737239462Sdim DeadStackObjects.insert(I); 738226633Sdim 739234353Sdim // Okay, so these are dead heap objects, but if the pointer never escapes 740234353Sdim // then it's leaked by this function anyways. 741243830Sdim else if (isAllocLikeFn(I, TLI) && !PointerMayBeCaptured(I, true, true)) 742239462Sdim DeadStackObjects.insert(I); 743234353Sdim } 744234353Sdim 745193323Sed // Treat byval arguments the same, stores to them are dead at the end of the 746193323Sed // function. 747193323Sed for (Function::arg_iterator AI = BB.getParent()->arg_begin(), 748193323Sed AE = BB.getParent()->arg_end(); AI != AE; ++AI) 749193323Sed if (AI->hasByValAttr()) 750218893Sdim DeadStackObjects.insert(AI); 751226633Sdim 752193323Sed // Scan the basic block backwards 753193323Sed for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){ 754193323Sed --BBI; 755226633Sdim 756218893Sdim // If we find a store, check to see if it points into a dead stack value. 757243830Sdim if (hasMemoryWrite(BBI, TLI) && isRemovable(BBI)) { 758218893Sdim // See through pointer-to-pointer bitcasts 759239462Sdim SmallVector<Value *, 4> Pointers; 760239462Sdim GetUnderlyingObjects(getStoredPointerOperand(BBI), Pointers); 761193323Sed 762218893Sdim // Stores to stack values are valid candidates for removal. 763239462Sdim bool AllDead = true; 764239462Sdim for (SmallVectorImpl<Value *>::iterator I = Pointers.begin(), 765239462Sdim E = Pointers.end(); I != E; ++I) 766239462Sdim if (!DeadStackObjects.count(*I)) { 767239462Sdim AllDead = false; 768239462Sdim break; 769239462Sdim } 770239462Sdim 771239462Sdim if (AllDead) { 772218893Sdim Instruction *Dead = BBI++; 773226633Sdim 774218893Sdim DEBUG(dbgs() << "DSE: Dead Store at End of Block:\n DEAD: " 775239462Sdim << *Dead << "\n Objects: "; 776239462Sdim for (SmallVectorImpl<Value *>::iterator I = Pointers.begin(), 777239462Sdim E = Pointers.end(); I != E; ++I) { 778239462Sdim dbgs() << **I; 779239462Sdim if (llvm::next(I) != E) 780239462Sdim dbgs() << ", "; 781239462Sdim } 782239462Sdim dbgs() << '\n'); 783226633Sdim 784218893Sdim // DCE instructions only used to calculate that store. 785243830Sdim DeleteDeadInstruction(Dead, *MD, TLI, &DeadStackObjects); 786218893Sdim ++NumFastStores; 787218893Sdim MadeChange = true; 788218893Sdim continue; 789193323Sed } 790193323Sed } 791226633Sdim 792218893Sdim // Remove any dead non-memory-mutating instructions. 793243830Sdim if (isInstructionTriviallyDead(BBI, TLI)) { 794218893Sdim Instruction *Inst = BBI++; 795243830Sdim DeleteDeadInstruction(Inst, *MD, TLI, &DeadStackObjects); 796218893Sdim ++NumFastOther; 797218893Sdim MadeChange = true; 798218893Sdim continue; 799218893Sdim } 800226633Sdim 801239462Sdim if (isa<AllocaInst>(BBI)) { 802239462Sdim // Remove allocas from the list of dead stack objects; there can't be 803239462Sdim // any references before the definition. 804239462Sdim DeadStackObjects.remove(BBI); 805193323Sed continue; 806218893Sdim } 807226633Sdim 808239462Sdim if (CallSite CS = cast<Value>(BBI)) { 809239462Sdim // Remove allocation function calls from the list of dead stack objects; 810239462Sdim // there can't be any references before the definition. 811243830Sdim if (isAllocLikeFn(BBI, TLI)) 812239462Sdim DeadStackObjects.remove(BBI); 813234353Sdim 814218893Sdim // If this call does not access memory, it can't be loading any of our 815218893Sdim // pointers. 816218893Sdim if (AA->doesNotAccessMemory(CS)) 817193323Sed continue; 818226633Sdim 819218893Sdim // If the call might load from any of our allocas, then any store above 820218893Sdim // the call is live. 821243830Sdim CouldRef Pred = { CS, AA }; 822243830Sdim DeadStackObjects.remove_if(Pred); 823226633Sdim 824218893Sdim // If all of the allocas were clobbered by the call then we're not going 825218893Sdim // to find anything else to process. 826218893Sdim if (DeadStackObjects.empty()) 827239462Sdim break; 828226633Sdim 829193323Sed continue; 830193323Sed } 831226633Sdim 832218893Sdim AliasAnalysis::Location LoadedLoc; 833226633Sdim 834218893Sdim // If we encounter a use of the pointer, it is no longer considered dead 835218893Sdim if (LoadInst *L = dyn_cast<LoadInst>(BBI)) { 836226633Sdim if (!L->isUnordered()) // Be conservative with atomic/volatile load 837226633Sdim break; 838218893Sdim LoadedLoc = AA->getLocation(L); 839218893Sdim } else if (VAArgInst *V = dyn_cast<VAArgInst>(BBI)) { 840218893Sdim LoadedLoc = AA->getLocation(V); 841218893Sdim } else if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(BBI)) { 842218893Sdim LoadedLoc = AA->getLocationForSource(MTI); 843226633Sdim } else if (!BBI->mayReadFromMemory()) { 844226633Sdim // Instruction doesn't read memory. Note that stores that weren't removed 845226633Sdim // above will hit this case. 846226633Sdim continue; 847218893Sdim } else { 848226633Sdim // Unknown inst; assume it clobbers everything. 849226633Sdim break; 850218893Sdim } 851193323Sed 852218893Sdim // Remove any allocas from the DeadPointer set that are loaded, as this 853218893Sdim // makes any stores above the access live. 854218893Sdim RemoveAccessedObjects(LoadedLoc, DeadStackObjects); 855193323Sed 856218893Sdim // If all of the allocas were clobbered by the access then we're not going 857218893Sdim // to find anything else to process. 858218893Sdim if (DeadStackObjects.empty()) 859218893Sdim break; 860193323Sed } 861226633Sdim 862193323Sed return MadeChange; 863193323Sed} 864193323Sed 865243830Sdimnamespace { 866243830Sdim struct CouldAlias { 867243830Sdim typedef Value *argument_type; 868243830Sdim const AliasAnalysis::Location &LoadedLoc; 869243830Sdim AliasAnalysis *AA; 870243830Sdim 871243830Sdim bool operator()(Value *I) { 872243830Sdim // See if the loaded location could alias the stack location. 873243830Sdim AliasAnalysis::Location StackLoc(I, getPointerSize(I, *AA)); 874243830Sdim return !AA->isNoAlias(StackLoc, LoadedLoc); 875243830Sdim } 876243830Sdim }; 877243830Sdim} 878243830Sdim 879218893Sdim/// RemoveAccessedObjects - Check to see if the specified location may alias any 880218893Sdim/// of the stack objects in the DeadStackObjects set. If so, they become live 881218893Sdim/// because the location is being loaded. 882218893Sdimvoid DSE::RemoveAccessedObjects(const AliasAnalysis::Location &LoadedLoc, 883239462Sdim SmallSetVector<Value*, 16> &DeadStackObjects) { 884218893Sdim const Value *UnderlyingPointer = GetUnderlyingObject(LoadedLoc.Ptr); 885202375Srdivacky 886218893Sdim // A constant can't be in the dead pointer set. 887218893Sdim if (isa<Constant>(UnderlyingPointer)) 888218893Sdim return; 889226633Sdim 890218893Sdim // If the kill pointer can be easily reduced to an alloca, don't bother doing 891218893Sdim // extraneous AA queries. 892218893Sdim if (isa<AllocaInst>(UnderlyingPointer) || isa<Argument>(UnderlyingPointer)) { 893239462Sdim DeadStackObjects.remove(const_cast<Value*>(UnderlyingPointer)); 894218893Sdim return; 895193323Sed } 896226633Sdim 897243830Sdim // Remove objects that could alias LoadedLoc. 898243830Sdim CouldAlias Pred = { LoadedLoc, AA }; 899243830Sdim DeadStackObjects.remove_if(Pred); 900193323Sed} 901