DeadStoreElimination.cpp 31 KB

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