DeadStoreElimination.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. //===- DeadStoreElimination.cpp - Fast Dead Store Elimination -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements a trivial dead store elimination that only considers
  11. // basic-block local redundant stores.
  12. //
  13. // FIXME: This should eventually be extended to be a post-dominator tree
  14. // traversal. Doing so would be pretty trivial.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #include "llvm/Transforms/Scalar.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/ADT/SetVector.h"
  20. #include "llvm/ADT/Statistic.h"
  21. #include "llvm/Analysis/AliasAnalysis.h"
  22. #include "llvm/Analysis/CaptureTracking.h"
  23. #include "llvm/Analysis/MemoryBuiltins.h"
  24. #include "llvm/Analysis/MemoryDependenceAnalysis.h"
  25. #include "llvm/Analysis/TargetLibraryInfo.h"
  26. #include "llvm/Analysis/ValueTracking.h"
  27. #include "llvm/IR/Constants.h"
  28. #include "llvm/IR/DataLayout.h"
  29. #include "llvm/IR/Dominators.h"
  30. #include "llvm/IR/Function.h"
  31. #include "llvm/IR/GlobalVariable.h"
  32. #include "llvm/IR/Instructions.h"
  33. #include "llvm/IR/IntrinsicInst.h"
  34. #include "llvm/Pass.h"
  35. #include "llvm/Support/Debug.h"
  36. #include "llvm/Support/raw_ostream.h"
  37. #include "llvm/Transforms/Utils/Local.h"
  38. using namespace llvm;
  39. #define DEBUG_TYPE "dse"
  40. STATISTIC(NumFastStores, "Number of stores deleted");
  41. STATISTIC(NumFastOther , "Number of other instrs removed");
  42. namespace {
  43. struct DSE : public FunctionPass {
  44. AliasAnalysis *AA;
  45. MemoryDependenceAnalysis *MD;
  46. DominatorTree *DT;
  47. const TargetLibraryInfo *TLI;
  48. unsigned ScanLimit; // HLSL Change - Add ScanLimit
  49. static char ID; // Pass identification, replacement for typeid
  50. DSE(unsigned ScanLimit = 0) :
  51. FunctionPass(ID), AA(nullptr), MD(nullptr), DT(nullptr), ScanLimit(ScanLimit) {// HLSL Change - Add ScanLimit
  52. initializeDSEPass(*PassRegistry::getPassRegistry());
  53. }
  54. bool runOnFunction(Function &F) override {
  55. if (skipOptnoneFunction(F))
  56. return false;
  57. AA = &getAnalysis<AliasAnalysis>();
  58. MD = &getAnalysis<MemoryDependenceAnalysis>();
  59. DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  60. TLI = AA->getTargetLibraryInfo();
  61. bool Changed = false;
  62. for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
  63. // Only check non-dead blocks. Dead blocks may have strange pointer
  64. // cycles that will confuse alias analysis.
  65. if (DT->isReachableFromEntry(I))
  66. Changed |= runOnBasicBlock(*I);
  67. AA = nullptr; MD = nullptr; DT = nullptr;
  68. return Changed;
  69. }
  70. bool runOnBasicBlock(BasicBlock &BB);
  71. bool HandleFree(CallInst *F);
  72. bool handleEndBlock(BasicBlock &BB);
  73. void RemoveAccessedObjects(const MemoryLocation &LoadedLoc,
  74. SmallSetVector<Value *, 16> &DeadStackObjects,
  75. const DataLayout &DL);
  76. void getAnalysisUsage(AnalysisUsage &AU) const override {
  77. AU.setPreservesCFG();
  78. AU.addRequired<DominatorTreeWrapperPass>();
  79. AU.addRequired<AliasAnalysis>();
  80. AU.addRequired<MemoryDependenceAnalysis>();
  81. AU.addPreserved<AliasAnalysis>();
  82. AU.addPreserved<DominatorTreeWrapperPass>();
  83. AU.addPreserved<MemoryDependenceAnalysis>();
  84. }
  85. };
  86. }
  87. char DSE::ID = 0;
  88. INITIALIZE_PASS_BEGIN(DSE, "dse", "Dead Store Elimination", false, false)
  89. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  90. INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
  91. INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
  92. INITIALIZE_PASS_END(DSE, "dse", "Dead Store Elimination", false, false)
  93. FunctionPass *llvm::createDeadStoreEliminationPass(unsigned ScanLimit) { return new DSE(ScanLimit); } // HLSL Change - add ScanLimit
  94. //===----------------------------------------------------------------------===//
  95. // Helper functions
  96. //===----------------------------------------------------------------------===//
  97. /// DeleteDeadInstruction - Delete this instruction. Before we do, go through
  98. /// and zero out all the operands of this instruction. If any of them become
  99. /// dead, delete them and the computation tree that feeds them.
  100. ///
  101. /// If ValueSet is non-null, remove any deleted instructions from it as well.
  102. ///
  103. static void DeleteDeadInstruction(Instruction *I,
  104. MemoryDependenceAnalysis &MD,
  105. const TargetLibraryInfo *TLI,
  106. SmallSetVector<Value*, 16> *ValueSet = nullptr) {
  107. SmallVector<Instruction*, 32> NowDeadInsts;
  108. NowDeadInsts.push_back(I);
  109. --NumFastOther;
  110. // Before we touch this instruction, remove it from memdep!
  111. do {
  112. Instruction *DeadInst = NowDeadInsts.pop_back_val();
  113. ++NumFastOther;
  114. // This instruction is dead, zap it, in stages. Start by removing it from
  115. // MemDep, which needs to know the operands and needs it to be in the
  116. // function.
  117. MD.removeInstruction(DeadInst);
  118. for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
  119. Value *Op = DeadInst->getOperand(op);
  120. DeadInst->setOperand(op, nullptr);
  121. // If this operand just became dead, add it to the NowDeadInsts list.
  122. if (!Op->use_empty()) continue;
  123. if (Instruction *OpI = dyn_cast<Instruction>(Op))
  124. if (isInstructionTriviallyDead(OpI, TLI))
  125. NowDeadInsts.push_back(OpI);
  126. }
  127. DeadInst->eraseFromParent();
  128. if (ValueSet) ValueSet->remove(DeadInst);
  129. } while (!NowDeadInsts.empty());
  130. }
  131. /// hasMemoryWrite - Does this instruction write some memory? This only returns
  132. /// true for things that we can analyze with other helpers below.
  133. static bool hasMemoryWrite(Instruction *I, const TargetLibraryInfo *TLI) {
  134. if (isa<StoreInst>(I))
  135. return true;
  136. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
  137. switch (II->getIntrinsicID()) {
  138. default:
  139. return false;
  140. case Intrinsic::memset:
  141. case Intrinsic::memmove:
  142. case Intrinsic::memcpy:
  143. case Intrinsic::init_trampoline:
  144. case Intrinsic::lifetime_end:
  145. return true;
  146. }
  147. }
  148. if (auto CS = CallSite(I)) {
  149. if (Function *F = CS.getCalledFunction()) {
  150. if (TLI && TLI->has(LibFunc::strcpy) &&
  151. F->getName() == TLI->getName(LibFunc::strcpy)) {
  152. return true;
  153. }
  154. if (TLI && TLI->has(LibFunc::strncpy) &&
  155. F->getName() == TLI->getName(LibFunc::strncpy)) {
  156. return true;
  157. }
  158. if (TLI && TLI->has(LibFunc::strcat) &&
  159. F->getName() == TLI->getName(LibFunc::strcat)) {
  160. return true;
  161. }
  162. if (TLI && TLI->has(LibFunc::strncat) &&
  163. F->getName() == TLI->getName(LibFunc::strncat)) {
  164. return true;
  165. }
  166. }
  167. }
  168. return false;
  169. }
  170. /// getLocForWrite - Return a Location stored to by the specified instruction.
  171. /// If isRemovable returns true, this function and getLocForRead completely
  172. /// describe the memory operations for this instruction.
  173. static MemoryLocation getLocForWrite(Instruction *Inst, AliasAnalysis &AA) {
  174. if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
  175. return MemoryLocation::get(SI);
  176. if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(Inst)) {
  177. // memcpy/memmove/memset.
  178. MemoryLocation Loc = MemoryLocation::getForDest(MI);
  179. return Loc;
  180. }
  181. IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst);
  182. if (!II)
  183. return MemoryLocation();
  184. switch (II->getIntrinsicID()) {
  185. default:
  186. return MemoryLocation(); // Unhandled intrinsic.
  187. case Intrinsic::init_trampoline:
  188. // FIXME: We don't know the size of the trampoline, so we can't really
  189. // handle it here.
  190. return MemoryLocation(II->getArgOperand(0));
  191. case Intrinsic::lifetime_end: {
  192. uint64_t Len = cast<ConstantInt>(II->getArgOperand(0))->getZExtValue();
  193. return MemoryLocation(II->getArgOperand(1), Len);
  194. }
  195. }
  196. }
  197. /// getLocForRead - Return the location read by the specified "hasMemoryWrite"
  198. /// instruction if any.
  199. static MemoryLocation getLocForRead(Instruction *Inst, AliasAnalysis &AA) {
  200. assert(hasMemoryWrite(Inst, AA.getTargetLibraryInfo()) &&
  201. "Unknown instruction case");
  202. // The only instructions that both read and write are the mem transfer
  203. // instructions (memcpy/memmove).
  204. if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(Inst))
  205. return MemoryLocation::getForSource(MTI);
  206. return MemoryLocation();
  207. }
  208. /// isRemovable - If the value of this instruction and the memory it writes to
  209. /// is unused, may we delete this instruction?
  210. static bool isRemovable(Instruction *I) {
  211. // Don't remove volatile/atomic stores.
  212. if (StoreInst *SI = dyn_cast<StoreInst>(I))
  213. return SI->isUnordered();
  214. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
  215. switch (II->getIntrinsicID()) {
  216. default: llvm_unreachable("doesn't pass 'hasMemoryWrite' predicate");
  217. case Intrinsic::lifetime_end:
  218. // Never remove dead lifetime_end's, e.g. because it is followed by a
  219. // free.
  220. return false;
  221. case Intrinsic::init_trampoline:
  222. // Always safe to remove init_trampoline.
  223. return true;
  224. case Intrinsic::memset:
  225. case Intrinsic::memmove:
  226. case Intrinsic::memcpy:
  227. // Don't remove volatile memory intrinsics.
  228. return !cast<MemIntrinsic>(II)->isVolatile();
  229. }
  230. }
  231. if (auto CS = CallSite(I))
  232. return CS.getInstruction()->use_empty();
  233. return false;
  234. }
  235. /// isShortenable - Returns true if this instruction can be safely shortened in
  236. /// length.
  237. static bool isShortenable(Instruction *I) {
  238. // Don't shorten stores for now
  239. if (isa<StoreInst>(I))
  240. return false;
  241. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
  242. switch (II->getIntrinsicID()) {
  243. default: return false;
  244. case Intrinsic::memset:
  245. case Intrinsic::memcpy:
  246. // Do shorten memory intrinsics.
  247. return true;
  248. }
  249. }
  250. // Don't shorten libcalls calls for now.
  251. return false;
  252. }
  253. /// getStoredPointerOperand - Return the pointer that is being written to.
  254. static Value *getStoredPointerOperand(Instruction *I) {
  255. if (StoreInst *SI = dyn_cast<StoreInst>(I))
  256. return SI->getPointerOperand();
  257. if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I))
  258. return MI->getDest();
  259. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
  260. switch (II->getIntrinsicID()) {
  261. default: llvm_unreachable("Unexpected intrinsic!");
  262. case Intrinsic::init_trampoline:
  263. return II->getArgOperand(0);
  264. }
  265. }
  266. CallSite CS(I);
  267. // All the supported functions so far happen to have dest as their first
  268. // argument.
  269. return CS.getArgument(0);
  270. }
  271. static uint64_t getPointerSize(const Value *V, const DataLayout &DL,
  272. const TargetLibraryInfo *TLI) {
  273. uint64_t Size;
  274. if (getObjectSize(V, Size, DL, TLI))
  275. return Size;
  276. return MemoryLocation::UnknownSize;
  277. }
  278. namespace {
  279. enum OverwriteResult
  280. {
  281. OverwriteComplete,
  282. OverwriteEnd,
  283. OverwriteUnknown
  284. };
  285. }
  286. /// isOverwrite - Return 'OverwriteComplete' if a store to the 'Later' location
  287. /// completely overwrites a store to the 'Earlier' location.
  288. /// 'OverwriteEnd' if the end of the 'Earlier' location is completely
  289. /// overwritten by 'Later', or 'OverwriteUnknown' if nothing can be determined
  290. static OverwriteResult isOverwrite(const MemoryLocation &Later,
  291. const MemoryLocation &Earlier,
  292. const DataLayout &DL,
  293. const TargetLibraryInfo *TLI,
  294. int64_t &EarlierOff, int64_t &LaterOff) {
  295. const Value *P1 = Earlier.Ptr->stripPointerCasts();
  296. const Value *P2 = Later.Ptr->stripPointerCasts();
  297. // If the start pointers are the same, we just have to compare sizes to see if
  298. // the later store was larger than the earlier store.
  299. if (P1 == P2) {
  300. // If we don't know the sizes of either access, then we can't do a
  301. // comparison.
  302. if (Later.Size == MemoryLocation::UnknownSize ||
  303. Earlier.Size == MemoryLocation::UnknownSize)
  304. return OverwriteUnknown;
  305. // Make sure that the Later size is >= the Earlier size.
  306. if (Later.Size >= Earlier.Size)
  307. return OverwriteComplete;
  308. }
  309. // Otherwise, we have to have size information, and the later store has to be
  310. // larger than the earlier one.
  311. if (Later.Size == MemoryLocation::UnknownSize ||
  312. Earlier.Size == MemoryLocation::UnknownSize)
  313. return OverwriteUnknown;
  314. // Check to see if the later store is to the entire object (either a global,
  315. // an alloca, or a byval/inalloca argument). If so, then it clearly
  316. // overwrites any other store to the same object.
  317. const Value *UO1 = GetUnderlyingObject(P1, DL),
  318. *UO2 = GetUnderlyingObject(P2, DL);
  319. // If we can't resolve the same pointers to the same object, then we can't
  320. // analyze them at all.
  321. if (UO1 != UO2)
  322. return OverwriteUnknown;
  323. // If the "Later" store is to a recognizable object, get its size.
  324. uint64_t ObjectSize = getPointerSize(UO2, DL, TLI);
  325. if (ObjectSize != MemoryLocation::UnknownSize)
  326. if (ObjectSize == Later.Size && ObjectSize >= Earlier.Size)
  327. return OverwriteComplete;
  328. // Okay, we have stores to two completely different pointers. Try to
  329. // decompose the pointer into a "base + constant_offset" form. If the base
  330. // pointers are equal, then we can reason about the two stores.
  331. EarlierOff = 0;
  332. LaterOff = 0;
  333. const Value *BP1 = GetPointerBaseWithConstantOffset(P1, EarlierOff, DL);
  334. const Value *BP2 = GetPointerBaseWithConstantOffset(P2, LaterOff, DL);
  335. // If the base pointers still differ, we have two completely different stores.
  336. if (BP1 != BP2)
  337. return OverwriteUnknown;
  338. // The later store completely overlaps the earlier store if:
  339. //
  340. // 1. Both start at the same offset and the later one's size is greater than
  341. // or equal to the earlier one's, or
  342. //
  343. // |--earlier--|
  344. // |-- later --|
  345. //
  346. // 2. The earlier store has an offset greater than the later offset, but which
  347. // still lies completely within the later store.
  348. //
  349. // |--earlier--|
  350. // |----- later ------|
  351. //
  352. // We have to be careful here as *Off is signed while *.Size is unsigned.
  353. if (EarlierOff >= LaterOff &&
  354. Later.Size >= Earlier.Size &&
  355. uint64_t(EarlierOff - LaterOff) + Earlier.Size <= Later.Size)
  356. return OverwriteComplete;
  357. // The other interesting case is if the later store overwrites the end of
  358. // the earlier store
  359. //
  360. // |--earlier--|
  361. // |-- later --|
  362. //
  363. // In this case we may want to trim the size of earlier to avoid generating
  364. // writes to addresses which will definitely be overwritten later
  365. if (LaterOff > EarlierOff &&
  366. LaterOff < int64_t(EarlierOff + Earlier.Size) &&
  367. int64_t(LaterOff + Later.Size) >= int64_t(EarlierOff + Earlier.Size))
  368. return OverwriteEnd;
  369. // Otherwise, they don't completely overlap.
  370. return OverwriteUnknown;
  371. }
  372. /// isPossibleSelfRead - If 'Inst' might be a self read (i.e. a noop copy of a
  373. /// memory region into an identical pointer) then it doesn't actually make its
  374. /// input dead in the traditional sense. Consider this case:
  375. ///
  376. /// memcpy(A <- B)
  377. /// memcpy(A <- A)
  378. ///
  379. /// In this case, the second store to A does not make the first store to A dead.
  380. /// The usual situation isn't an explicit A<-A store like this (which can be
  381. /// trivially removed) but a case where two pointers may alias.
  382. ///
  383. /// This function detects when it is unsafe to remove a dependent instruction
  384. /// because the DSE inducing instruction may be a self-read.
  385. static bool isPossibleSelfRead(Instruction *Inst,
  386. const MemoryLocation &InstStoreLoc,
  387. Instruction *DepWrite, AliasAnalysis &AA) {
  388. // Self reads can only happen for instructions that read memory. Get the
  389. // location read.
  390. MemoryLocation InstReadLoc = getLocForRead(Inst, AA);
  391. if (!InstReadLoc.Ptr) return false; // Not a reading instruction.
  392. // If the read and written loc obviously don't alias, it isn't a read.
  393. if (AA.isNoAlias(InstReadLoc, InstStoreLoc)) return false;
  394. // Okay, 'Inst' may copy over itself. However, we can still remove a the
  395. // DepWrite instruction if we can prove that it reads from the same location
  396. // as Inst. This handles useful cases like:
  397. // memcpy(A <- B)
  398. // memcpy(A <- B)
  399. // Here we don't know if A/B may alias, but we do know that B/B are must
  400. // aliases, so removing the first memcpy is safe (assuming it writes <= #
  401. // bytes as the second one.
  402. MemoryLocation DepReadLoc = getLocForRead(DepWrite, AA);
  403. if (DepReadLoc.Ptr && AA.isMustAlias(InstReadLoc.Ptr, DepReadLoc.Ptr))
  404. return false;
  405. // If DepWrite doesn't read memory or if we can't prove it is a must alias,
  406. // then it can't be considered dead.
  407. return true;
  408. }
  409. //===----------------------------------------------------------------------===//
  410. // DSE Pass
  411. //===----------------------------------------------------------------------===//
  412. bool DSE::runOnBasicBlock(BasicBlock &BB) {
  413. bool MadeChange = false;
  414. // Do a top-down walk on the BB.
  415. for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end(); BBI != BBE; ) {
  416. Instruction *Inst = BBI++;
  417. // Handle 'free' calls specially.
  418. if (CallInst *F = isFreeCall(Inst, TLI)) {
  419. MadeChange |= HandleFree(F);
  420. continue;
  421. }
  422. // If we find something that writes memory, get its memory dependence.
  423. if (!hasMemoryWrite(Inst, TLI))
  424. continue;
  425. MemDepResult InstDep = MD->getDependency(Inst, ScanLimit);
  426. // Ignore any store where we can't find a local dependence.
  427. // FIXME: cross-block DSE would be fun. :)
  428. if (!InstDep.isDef() && !InstDep.isClobber())
  429. continue;
  430. // If we're storing the same value back to a pointer that we just
  431. // loaded from, then the store can be removed.
  432. if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
  433. if (LoadInst *DepLoad = dyn_cast<LoadInst>(InstDep.getInst())) {
  434. if (SI->getPointerOperand() == DepLoad->getPointerOperand() &&
  435. SI->getOperand(0) == DepLoad && isRemovable(SI)) {
  436. DEBUG(dbgs() << "DSE: Remove Store Of Load from same pointer:\n "
  437. << "LOAD: " << *DepLoad << "\n STORE: " << *SI << '\n');
  438. // DeleteDeadInstruction can delete the current instruction. Save BBI
  439. // in case we need it.
  440. WeakVH NextInst(BBI);
  441. DeleteDeadInstruction(SI, *MD, TLI);
  442. if (!NextInst) // Next instruction deleted.
  443. BBI = BB.begin();
  444. else if (BBI != BB.begin()) // Revisit this instruction if possible.
  445. --BBI;
  446. ++NumFastStores;
  447. MadeChange = true;
  448. continue;
  449. }
  450. }
  451. }
  452. // Figure out what location is being stored to.
  453. MemoryLocation Loc = getLocForWrite(Inst, *AA);
  454. // If we didn't get a useful location, fail.
  455. if (!Loc.Ptr)
  456. continue;
  457. while (InstDep.isDef() || InstDep.isClobber()) {
  458. // Get the memory clobbered by the instruction we depend on. MemDep will
  459. // skip any instructions that 'Loc' clearly doesn't interact with. If we
  460. // end up depending on a may- or must-aliased load, then we can't optimize
  461. // away the store and we bail out. However, if we depend on on something
  462. // that overwrites the memory location we *can* potentially optimize it.
  463. //
  464. // Find out what memory location the dependent instruction stores.
  465. Instruction *DepWrite = InstDep.getInst();
  466. MemoryLocation DepLoc = getLocForWrite(DepWrite, *AA);
  467. // If we didn't get a useful location, or if it isn't a size, bail out.
  468. if (!DepLoc.Ptr)
  469. break;
  470. // If we find a write that is a) removable (i.e., non-volatile), b) is
  471. // completely obliterated by the store to 'Loc', and c) which we know that
  472. // 'Inst' doesn't load from, then we can remove it.
  473. if (isRemovable(DepWrite) &&
  474. !isPossibleSelfRead(Inst, Loc, DepWrite, *AA)) {
  475. int64_t InstWriteOffset, DepWriteOffset;
  476. const DataLayout &DL = BB.getModule()->getDataLayout();
  477. OverwriteResult OR =
  478. isOverwrite(Loc, DepLoc, DL, AA->getTargetLibraryInfo(),
  479. DepWriteOffset, InstWriteOffset);
  480. if (OR == OverwriteComplete) {
  481. DEBUG(dbgs() << "DSE: Remove Dead Store:\n DEAD: "
  482. << *DepWrite << "\n KILLER: " << *Inst << '\n');
  483. // Delete the store and now-dead instructions that feed it.
  484. DeleteDeadInstruction(DepWrite, *MD, TLI);
  485. ++NumFastStores;
  486. MadeChange = true;
  487. // DeleteDeadInstruction can delete the current instruction in loop
  488. // cases, reset BBI.
  489. BBI = Inst;
  490. if (BBI != BB.begin())
  491. --BBI;
  492. break;
  493. } else if (OR == OverwriteEnd && isShortenable(DepWrite)) {
  494. // TODO: base this on the target vector size so that if the earlier
  495. // store was too small to get vector writes anyway then its likely
  496. // a good idea to shorten it
  497. // Power of 2 vector writes are probably always a bad idea to optimize
  498. // as any store/memset/memcpy is likely using vector instructions so
  499. // shortening it to not vector size is likely to be slower
  500. MemIntrinsic* DepIntrinsic = cast<MemIntrinsic>(DepWrite);
  501. unsigned DepWriteAlign = DepIntrinsic->getAlignment();
  502. if (llvm::isPowerOf2_64(InstWriteOffset) ||
  503. ((DepWriteAlign != 0) && InstWriteOffset % DepWriteAlign == 0)) {
  504. DEBUG(dbgs() << "DSE: Remove Dead Store:\n OW END: "
  505. << *DepWrite << "\n KILLER (offset "
  506. << InstWriteOffset << ", "
  507. << DepLoc.Size << ")"
  508. << *Inst << '\n');
  509. Value* DepWriteLength = DepIntrinsic->getLength();
  510. Value* TrimmedLength = ConstantInt::get(DepWriteLength->getType(),
  511. InstWriteOffset -
  512. DepWriteOffset);
  513. DepIntrinsic->setLength(TrimmedLength);
  514. MadeChange = true;
  515. }
  516. }
  517. }
  518. // If this is a may-aliased store that is clobbering the store value, we
  519. // can keep searching past it for another must-aliased pointer that stores
  520. // to the same location. For example, in:
  521. // store -> P
  522. // store -> Q
  523. // store -> P
  524. // we can remove the first store to P even though we don't know if P and Q
  525. // alias.
  526. if (DepWrite == &BB.front()) break;
  527. // Can't look past this instruction if it might read 'Loc'.
  528. if (AA->getModRefInfo(DepWrite, Loc) & AliasAnalysis::Ref)
  529. break;
  530. InstDep = MD->getPointerDependencyFrom(Loc, false, DepWrite, &BB);
  531. }
  532. }
  533. // If this block ends in a return, unwind, or unreachable, all allocas are
  534. // dead at its end, which means stores to them are also dead.
  535. if (BB.getTerminator()->getNumSuccessors() == 0)
  536. MadeChange |= handleEndBlock(BB);
  537. return MadeChange;
  538. }
  539. /// Find all blocks that will unconditionally lead to the block BB and append
  540. /// them to F.
  541. static void FindUnconditionalPreds(SmallVectorImpl<BasicBlock *> &Blocks,
  542. BasicBlock *BB, DominatorTree *DT) {
  543. for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
  544. BasicBlock *Pred = *I;
  545. if (Pred == BB) continue;
  546. TerminatorInst *PredTI = Pred->getTerminator();
  547. if (PredTI->getNumSuccessors() != 1)
  548. continue;
  549. if (DT->isReachableFromEntry(Pred))
  550. Blocks.push_back(Pred);
  551. }
  552. }
  553. /// HandleFree - Handle frees of entire structures whose dependency is a store
  554. /// to a field of that structure.
  555. bool DSE::HandleFree(CallInst *F) {
  556. bool MadeChange = false;
  557. MemoryLocation Loc = MemoryLocation(F->getOperand(0));
  558. SmallVector<BasicBlock *, 16> Blocks;
  559. Blocks.push_back(F->getParent());
  560. const DataLayout &DL = F->getModule()->getDataLayout();
  561. while (!Blocks.empty()) {
  562. BasicBlock *BB = Blocks.pop_back_val();
  563. Instruction *InstPt = BB->getTerminator();
  564. if (BB == F->getParent()) InstPt = F;
  565. MemDepResult Dep = MD->getPointerDependencyFrom(Loc, false, InstPt, BB);
  566. while (Dep.isDef() || Dep.isClobber()) {
  567. Instruction *Dependency = Dep.getInst();
  568. if (!hasMemoryWrite(Dependency, TLI) || !isRemovable(Dependency))
  569. break;
  570. Value *DepPointer =
  571. GetUnderlyingObject(getStoredPointerOperand(Dependency), DL);
  572. // Check for aliasing.
  573. if (!AA->isMustAlias(F->getArgOperand(0), DepPointer))
  574. break;
  575. Instruction *Next = std::next(BasicBlock::iterator(Dependency));
  576. // DCE instructions only used to calculate that store
  577. DeleteDeadInstruction(Dependency, *MD, TLI);
  578. ++NumFastStores;
  579. MadeChange = true;
  580. // Inst's old Dependency is now deleted. Compute the next dependency,
  581. // which may also be dead, as in
  582. // s[0] = 0;
  583. // s[1] = 0; // This has just been deleted.
  584. // free(s);
  585. Dep = MD->getPointerDependencyFrom(Loc, false, Next, BB);
  586. }
  587. if (Dep.isNonLocal())
  588. FindUnconditionalPreds(Blocks, BB, DT);
  589. }
  590. return MadeChange;
  591. }
  592. /// handleEndBlock - Remove dead stores to stack-allocated locations in the
  593. /// function end block. Ex:
  594. /// %A = alloca i32
  595. /// ...
  596. /// store i32 1, i32* %A
  597. /// ret void
  598. bool DSE::handleEndBlock(BasicBlock &BB) {
  599. bool MadeChange = false;
  600. // Keep track of all of the stack objects that are dead at the end of the
  601. // function.
  602. SmallSetVector<Value*, 16> DeadStackObjects;
  603. // Find all of the alloca'd pointers in the entry block.
  604. BasicBlock *Entry = BB.getParent()->begin();
  605. for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I) {
  606. if (isa<AllocaInst>(I))
  607. DeadStackObjects.insert(I);
  608. // Okay, so these are dead heap objects, but if the pointer never escapes
  609. // then it's leaked by this function anyways.
  610. else if (isAllocLikeFn(I, TLI) && !PointerMayBeCaptured(I, true, true))
  611. DeadStackObjects.insert(I);
  612. }
  613. // Treat byval or inalloca arguments the same, stores to them are dead at the
  614. // end of the function.
  615. for (Function::arg_iterator AI = BB.getParent()->arg_begin(),
  616. AE = BB.getParent()->arg_end(); AI != AE; ++AI)
  617. if (AI->hasByValOrInAllocaAttr())
  618. DeadStackObjects.insert(AI);
  619. const DataLayout &DL = BB.getModule()->getDataLayout();
  620. // Scan the basic block backwards
  621. for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
  622. --BBI;
  623. // If we find a store, check to see if it points into a dead stack value.
  624. if (hasMemoryWrite(BBI, TLI) && isRemovable(BBI)) {
  625. // See through pointer-to-pointer bitcasts
  626. SmallVector<Value *, 4> Pointers;
  627. GetUnderlyingObjects(getStoredPointerOperand(BBI), Pointers, DL);
  628. // Stores to stack values are valid candidates for removal.
  629. bool AllDead = true;
  630. for (SmallVectorImpl<Value *>::iterator I = Pointers.begin(),
  631. E = Pointers.end(); I != E; ++I)
  632. if (!DeadStackObjects.count(*I)) {
  633. AllDead = false;
  634. break;
  635. }
  636. if (AllDead) {
  637. Instruction *Dead = BBI++;
  638. DEBUG(dbgs() << "DSE: Dead Store at End of Block:\n DEAD: "
  639. << *Dead << "\n Objects: ";
  640. for (SmallVectorImpl<Value *>::iterator I = Pointers.begin(),
  641. E = Pointers.end(); I != E; ++I) {
  642. dbgs() << **I;
  643. if (std::next(I) != E)
  644. dbgs() << ", ";
  645. }
  646. dbgs() << '\n');
  647. // DCE instructions only used to calculate that store.
  648. DeleteDeadInstruction(Dead, *MD, TLI, &DeadStackObjects);
  649. ++NumFastStores;
  650. MadeChange = true;
  651. continue;
  652. }
  653. }
  654. // Remove any dead non-memory-mutating instructions.
  655. if (isInstructionTriviallyDead(BBI, TLI)) {
  656. Instruction *Inst = BBI++;
  657. DeleteDeadInstruction(Inst, *MD, TLI, &DeadStackObjects);
  658. ++NumFastOther;
  659. MadeChange = true;
  660. continue;
  661. }
  662. if (isa<AllocaInst>(BBI)) {
  663. // Remove allocas from the list of dead stack objects; there can't be
  664. // any references before the definition.
  665. DeadStackObjects.remove(BBI);
  666. continue;
  667. }
  668. if (auto CS = CallSite(BBI)) {
  669. // Remove allocation function calls from the list of dead stack objects;
  670. // there can't be any references before the definition.
  671. if (isAllocLikeFn(BBI, TLI))
  672. DeadStackObjects.remove(BBI);
  673. // If this call does not access memory, it can't be loading any of our
  674. // pointers.
  675. if (AA->doesNotAccessMemory(CS))
  676. continue;
  677. // If the call might load from any of our allocas, then any store above
  678. // the call is live.
  679. DeadStackObjects.remove_if([&](Value *I) {
  680. // See if the call site touches the value.
  681. AliasAnalysis::ModRefResult A = AA->getModRefInfo(
  682. CS, I, getPointerSize(I, DL, AA->getTargetLibraryInfo()));
  683. return A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref;
  684. });
  685. // If all of the allocas were clobbered by the call then we're not going
  686. // to find anything else to process.
  687. if (DeadStackObjects.empty())
  688. break;
  689. continue;
  690. }
  691. MemoryLocation LoadedLoc;
  692. // If we encounter a use of the pointer, it is no longer considered dead
  693. if (LoadInst *L = dyn_cast<LoadInst>(BBI)) {
  694. if (!L->isUnordered()) // Be conservative with atomic/volatile load
  695. break;
  696. LoadedLoc = MemoryLocation::get(L);
  697. } else if (VAArgInst *V = dyn_cast<VAArgInst>(BBI)) {
  698. LoadedLoc = MemoryLocation::get(V);
  699. } else if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(BBI)) {
  700. LoadedLoc = MemoryLocation::getForSource(MTI);
  701. } else if (!BBI->mayReadFromMemory()) {
  702. // Instruction doesn't read memory. Note that stores that weren't removed
  703. // above will hit this case.
  704. continue;
  705. } else {
  706. // Unknown inst; assume it clobbers everything.
  707. break;
  708. }
  709. // Remove any allocas from the DeadPointer set that are loaded, as this
  710. // makes any stores above the access live.
  711. RemoveAccessedObjects(LoadedLoc, DeadStackObjects, DL);
  712. // If all of the allocas were clobbered by the access then we're not going
  713. // to find anything else to process.
  714. if (DeadStackObjects.empty())
  715. break;
  716. }
  717. return MadeChange;
  718. }
  719. /// RemoveAccessedObjects - Check to see if the specified location may alias any
  720. /// of the stack objects in the DeadStackObjects set. If so, they become live
  721. /// because the location is being loaded.
  722. void DSE::RemoveAccessedObjects(const MemoryLocation &LoadedLoc,
  723. SmallSetVector<Value *, 16> &DeadStackObjects,
  724. const DataLayout &DL) {
  725. const Value *UnderlyingPointer = GetUnderlyingObject(LoadedLoc.Ptr, DL);
  726. // A constant can't be in the dead pointer set.
  727. if (isa<Constant>(UnderlyingPointer))
  728. return;
  729. // If the kill pointer can be easily reduced to an alloca, don't bother doing
  730. // extraneous AA queries.
  731. if (isa<AllocaInst>(UnderlyingPointer) || isa<Argument>(UnderlyingPointer)) {
  732. DeadStackObjects.remove(const_cast<Value*>(UnderlyingPointer));
  733. return;
  734. }
  735. // Remove objects that could alias LoadedLoc.
  736. DeadStackObjects.remove_if([&](Value *I) {
  737. // See if the loaded location could alias the stack location.
  738. MemoryLocation StackLoc(I,
  739. getPointerSize(I, DL, AA->getTargetLibraryInfo()));
  740. return !AA->isNoAlias(StackLoc, LoadedLoc);
  741. });
  742. }