LLVM  10.0.0
DeadStoreElimination.cpp
Go to the documentation of this file.
1 //===- DeadStoreElimination.cpp - Fast Dead Store Elimination -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a trivial dead store elimination that only considers
10 // basic-block local redundant stores.
11 //
12 // FIXME: This should eventually be extended to be a post-dominator tree
13 // traversal. Doing so would be pretty trivial.
14 //
15 //===----------------------------------------------------------------------===//
16 
18 #include "llvm/ADT/APInt.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/MapVector.h"
21 #include "llvm/ADT/SetVector.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/ADT/StringRef.h"
35 #include "llvm/IR/Argument.h"
36 #include "llvm/IR/BasicBlock.h"
37 #include "llvm/IR/CallSite.h"
38 #include "llvm/IR/Constant.h"
39 #include "llvm/IR/Constants.h"
40 #include "llvm/IR/DataLayout.h"
41 #include "llvm/IR/Dominators.h"
42 #include "llvm/IR/Function.h"
43 #include "llvm/IR/InstrTypes.h"
44 #include "llvm/IR/Instruction.h"
45 #include "llvm/IR/Instructions.h"
46 #include "llvm/IR/IntrinsicInst.h"
47 #include "llvm/IR/Intrinsics.h"
48 #include "llvm/IR/LLVMContext.h"
49 #include "llvm/IR/Module.h"
50 #include "llvm/IR/PassManager.h"
51 #include "llvm/IR/Value.h"
52 #include "llvm/InitializePasses.h"
53 #include "llvm/Pass.h"
54 #include "llvm/Support/Casting.h"
56 #include "llvm/Support/Debug.h"
60 #include "llvm/Transforms/Scalar.h"
62 #include <algorithm>
63 #include <cassert>
64 #include <cstddef>
65 #include <cstdint>
66 #include <iterator>
67 #include <map>
68 #include <utility>
69 
70 using namespace llvm;
71 
72 #define DEBUG_TYPE "dse"
73 
74 STATISTIC(NumRedundantStores, "Number of redundant stores deleted");
75 STATISTIC(NumFastStores, "Number of stores deleted");
76 STATISTIC(NumFastOther, "Number of other instrs removed");
77 STATISTIC(NumCompletePartials, "Number of stores dead by later partials");
78 STATISTIC(NumModifiedStores, "Number of stores modified");
79 
80 static cl::opt<bool>
81 EnablePartialOverwriteTracking("enable-dse-partial-overwrite-tracking",
82  cl::init(true), cl::Hidden,
83  cl::desc("Enable partial-overwrite tracking in DSE"));
84 
85 static cl::opt<bool>
86 EnablePartialStoreMerging("enable-dse-partial-store-merging",
87  cl::init(true), cl::Hidden,
88  cl::desc("Enable partial store merging in DSE"));
89 
90 //===----------------------------------------------------------------------===//
91 // Helper functions
92 //===----------------------------------------------------------------------===//
93 using OverlapIntervalsTy = std::map<int64_t, int64_t>;
95 
96 /// Delete this instruction. Before we do, go through and zero out all the
97 /// operands of this instruction. If any of them become dead, delete them and
98 /// the computation tree that feeds them.
99 /// If ValueSet is non-null, remove any deleted instructions from it as well.
100 static void
104  MapVector<Instruction *, bool> &ThrowableInst,
105  SmallSetVector<const Value *, 16> *ValueSet = nullptr) {
106  SmallVector<Instruction*, 32> NowDeadInsts;
107 
108  NowDeadInsts.push_back(I);
109  --NumFastOther;
110 
111  // Keeping the iterator straight is a pain, so we let this routine tell the
112  // caller what the next instruction is after we're done mucking about.
113  BasicBlock::iterator NewIter = *BBI;
114 
115  // Before we touch this instruction, remove it from memdep!
116  do {
117  Instruction *DeadInst = NowDeadInsts.pop_back_val();
118  // Mark the DeadInst as dead in the list of throwable instructions.
119  auto It = ThrowableInst.find(DeadInst);
120  if (It != ThrowableInst.end())
121  ThrowableInst[It->first] = false;
122  ++NumFastOther;
123 
124  // Try to preserve debug information attached to the dead instruction.
125  salvageDebugInfo(*DeadInst);
126 
127  // This instruction is dead, zap it, in stages. Start by removing it from
128  // MemDep, which needs to know the operands and needs it to be in the
129  // function.
130  MD.removeInstruction(DeadInst);
131 
132  for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
133  Value *Op = DeadInst->getOperand(op);
134  DeadInst->setOperand(op, nullptr);
135 
136  // If this operand just became dead, add it to the NowDeadInsts list.
137  if (!Op->use_empty()) continue;
138 
139  if (Instruction *OpI = dyn_cast<Instruction>(Op))
140  if (isInstructionTriviallyDead(OpI, &TLI))
141  NowDeadInsts.push_back(OpI);
142  }
143 
144  if (ValueSet) ValueSet->remove(DeadInst);
145  IOL.erase(DeadInst);
146  OBB.eraseInstruction(DeadInst);
147 
148  if (NewIter == DeadInst->getIterator())
149  NewIter = DeadInst->eraseFromParent();
150  else
151  DeadInst->eraseFromParent();
152  } while (!NowDeadInsts.empty());
153  *BBI = NewIter;
154  // Pop dead entries from back of ThrowableInst till we find an alive entry.
155  while (!ThrowableInst.empty() && !ThrowableInst.back().second)
156  ThrowableInst.pop_back();
157 }
158 
159 /// Does this instruction write some memory? This only returns true for things
160 /// that we can analyze with other helpers below.
162  const TargetLibraryInfo &TLI) {
163  if (isa<StoreInst>(I))
164  return true;
165  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
166  switch (II->getIntrinsicID()) {
167  default:
168  return false;
169  case Intrinsic::memset:
170  case Intrinsic::memmove:
171  case Intrinsic::memcpy:
172  case Intrinsic::memcpy_element_unordered_atomic:
173  case Intrinsic::memmove_element_unordered_atomic:
174  case Intrinsic::memset_element_unordered_atomic:
175  case Intrinsic::init_trampoline:
176  case Intrinsic::lifetime_end:
177  return true;
178  }
179  }
180  if (auto CS = CallSite(I)) {
181  if (Function *F = CS.getCalledFunction()) {
182  LibFunc LF;
183  if (TLI.getLibFunc(*F, LF) && TLI.has(LF)) {
184  switch (LF) {
185  case LibFunc_strcpy:
186  case LibFunc_strncpy:
187  case LibFunc_strcat:
188  case LibFunc_strncat:
189  return true;
190  default:
191  return false;
192  }
193  }
194  }
195  }
196  return false;
197 }
198 
199 /// Return a Location stored to by the specified instruction. If isRemovable
200 /// returns true, this function and getLocForRead completely describe the memory
201 /// operations for this instruction.
203 
204  if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
205  return MemoryLocation::get(SI);
206 
207  if (auto *MI = dyn_cast<AnyMemIntrinsic>(Inst)) {
208  // memcpy/memmove/memset.
210  return Loc;
211  }
212 
213  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
214  switch (II->getIntrinsicID()) {
215  default:
216  return MemoryLocation(); // Unhandled intrinsic.
217  case Intrinsic::init_trampoline:
218  return MemoryLocation(II->getArgOperand(0));
219  case Intrinsic::lifetime_end: {
220  uint64_t Len = cast<ConstantInt>(II->getArgOperand(0))->getZExtValue();
221  return MemoryLocation(II->getArgOperand(1), Len);
222  }
223  }
224  }
225  if (auto CS = CallSite(Inst))
226  // All the supported TLI functions so far happen to have dest as their
227  // first argument.
228  return MemoryLocation(CS.getArgument(0));
229  return MemoryLocation();
230 }
231 
232 /// Return the location read by the specified "hasAnalyzableMemoryWrite"
233 /// instruction if any.
235  const TargetLibraryInfo &TLI) {
236  assert(hasAnalyzableMemoryWrite(Inst, TLI) && "Unknown instruction case");
237 
238  // The only instructions that both read and write are the mem transfer
239  // instructions (memcpy/memmove).
240  if (auto *MTI = dyn_cast<AnyMemTransferInst>(Inst))
241  return MemoryLocation::getForSource(MTI);
242  return MemoryLocation();
243 }
244 
245 /// If the value of this instruction and the memory it writes to is unused, may
246 /// we delete this instruction?
247 static bool isRemovable(Instruction *I) {
248  // Don't remove volatile/atomic stores.
249  if (StoreInst *SI = dyn_cast<StoreInst>(I))
250  return SI->isUnordered();
251 
252  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
253  switch (II->getIntrinsicID()) {
254  default: llvm_unreachable("doesn't pass 'hasAnalyzableMemoryWrite' predicate");
255  case Intrinsic::lifetime_end:
256  // Never remove dead lifetime_end's, e.g. because it is followed by a
257  // free.
258  return false;
259  case Intrinsic::init_trampoline:
260  // Always safe to remove init_trampoline.
261  return true;
262  case Intrinsic::memset:
263  case Intrinsic::memmove:
264  case Intrinsic::memcpy:
265  // Don't remove volatile memory intrinsics.
266  return !cast<MemIntrinsic>(II)->isVolatile();
267  case Intrinsic::memcpy_element_unordered_atomic:
268  case Intrinsic::memmove_element_unordered_atomic:
269  case Intrinsic::memset_element_unordered_atomic:
270  return true;
271  }
272  }
273 
274  // note: only get here for calls with analyzable writes - i.e. libcalls
275  if (auto CS = CallSite(I))
276  return CS.getInstruction()->use_empty();
277 
278  return false;
279 }
280 
281 /// Returns true if the end of this instruction can be safely shortened in
282 /// length.
284  // Don't shorten stores for now
285  if (isa<StoreInst>(I))
286  return false;
287 
288  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
289  switch (II->getIntrinsicID()) {
290  default: return false;
291  case Intrinsic::memset:
292  case Intrinsic::memcpy:
293  case Intrinsic::memcpy_element_unordered_atomic:
294  case Intrinsic::memset_element_unordered_atomic:
295  // Do shorten memory intrinsics.
296  // FIXME: Add memmove if it's also safe to transform.
297  return true;
298  }
299  }
300 
301  // Don't shorten libcalls calls for now.
302 
303  return false;
304 }
305 
306 /// Returns true if the beginning of this instruction can be safely shortened
307 /// in length.
309  // FIXME: Handle only memset for now. Supporting memcpy/memmove should be
310  // easily done by offsetting the source address.
311  return isa<AnyMemSetInst>(I);
312 }
313 
314 /// Return the pointer that is being written to.
316  //TODO: factor this to reuse getLocForWrite
318  assert(Loc.Ptr &&
319  "unable to find pointer written for analyzable instruction?");
320  // TODO: most APIs don't expect const Value *
321  return const_cast<Value*>(Loc.Ptr);
322 }
323 
324 static uint64_t getPointerSize(const Value *V, const DataLayout &DL,
325  const TargetLibraryInfo &TLI,
326  const Function *F) {
327  uint64_t Size;
328  ObjectSizeOpts Opts;
330 
331  if (getObjectSize(V, Size, DL, &TLI, Opts))
332  return Size;
334 }
335 
336 namespace {
337 
339  OW_Begin,
340  OW_Complete,
341  OW_End,
342  OW_PartialEarlierWithFullLater,
343  OW_Unknown
344 };
345 
346 } // end anonymous namespace
347 
348 /// Return 'OW_Complete' if a store to the 'Later' location completely
349 /// overwrites a store to the 'Earlier' location, 'OW_End' if the end of the
350 /// 'Earlier' location is completely overwritten by 'Later', 'OW_Begin' if the
351 /// beginning of the 'Earlier' location is overwritten by 'Later'.
352 /// 'OW_PartialEarlierWithFullLater' means that an earlier (big) store was
353 /// overwritten by a latter (smaller) store which doesn't write outside the big
354 /// store's memory locations. Returns 'OW_Unknown' if nothing can be determined.
356  const MemoryLocation &Earlier,
357  const DataLayout &DL,
358  const TargetLibraryInfo &TLI,
359  int64_t &EarlierOff, int64_t &LaterOff,
360  Instruction *DepWrite,
362  AliasAnalysis &AA,
363  const Function *F) {
364  // FIXME: Vet that this works for size upper-bounds. Seems unlikely that we'll
365  // get imprecise values here, though (except for unknown sizes).
366  if (!Later.Size.isPrecise() || !Earlier.Size.isPrecise())
367  return OW_Unknown;
368 
369  const uint64_t LaterSize = Later.Size.getValue();
370  const uint64_t EarlierSize = Earlier.Size.getValue();
371 
372  const Value *P1 = Earlier.Ptr->stripPointerCasts();
373  const Value *P2 = Later.Ptr->stripPointerCasts();
374 
375  // If the start pointers are the same, we just have to compare sizes to see if
376  // the later store was larger than the earlier store.
377  if (P1 == P2 || AA.isMustAlias(P1, P2)) {
378  // Make sure that the Later size is >= the Earlier size.
379  if (LaterSize >= EarlierSize)
380  return OW_Complete;
381  }
382 
383  // Check to see if the later store is to the entire object (either a global,
384  // an alloca, or a byval/inalloca argument). If so, then it clearly
385  // overwrites any other store to the same object.
386  const Value *UO1 = GetUnderlyingObject(P1, DL),
387  *UO2 = GetUnderlyingObject(P2, DL);
388 
389  // If we can't resolve the same pointers to the same object, then we can't
390  // analyze them at all.
391  if (UO1 != UO2)
392  return OW_Unknown;
393 
394  // If the "Later" store is to a recognizable object, get its size.
395  uint64_t ObjectSize = getPointerSize(UO2, DL, TLI, F);
396  if (ObjectSize != MemoryLocation::UnknownSize)
397  if (ObjectSize == LaterSize && ObjectSize >= EarlierSize)
398  return OW_Complete;
399 
400  // Okay, we have stores to two completely different pointers. Try to
401  // decompose the pointer into a "base + constant_offset" form. If the base
402  // pointers are equal, then we can reason about the two stores.
403  EarlierOff = 0;
404  LaterOff = 0;
405  const Value *BP1 = GetPointerBaseWithConstantOffset(P1, EarlierOff, DL);
406  const Value *BP2 = GetPointerBaseWithConstantOffset(P2, LaterOff, DL);
407 
408  // If the base pointers still differ, we have two completely different stores.
409  if (BP1 != BP2)
410  return OW_Unknown;
411 
412  // The later store completely overlaps the earlier store if:
413  //
414  // 1. Both start at the same offset and the later one's size is greater than
415  // or equal to the earlier one's, or
416  //
417  // |--earlier--|
418  // |-- later --|
419  //
420  // 2. The earlier store has an offset greater than the later offset, but which
421  // still lies completely within the later store.
422  //
423  // |--earlier--|
424  // |----- later ------|
425  //
426  // We have to be careful here as *Off is signed while *.Size is unsigned.
427  if (EarlierOff >= LaterOff &&
428  LaterSize >= EarlierSize &&
429  uint64_t(EarlierOff - LaterOff) + EarlierSize <= LaterSize)
430  return OW_Complete;
431 
432  // We may now overlap, although the overlap is not complete. There might also
433  // be other incomplete overlaps, and together, they might cover the complete
434  // earlier write.
435  // Note: The correctness of this logic depends on the fact that this function
436  // is not even called providing DepWrite when there are any intervening reads.
438  LaterOff < int64_t(EarlierOff + EarlierSize) &&
439  int64_t(LaterOff + LaterSize) >= EarlierOff) {
440 
441  // Insert our part of the overlap into the map.
442  auto &IM = IOL[DepWrite];
443  LLVM_DEBUG(dbgs() << "DSE: Partial overwrite: Earlier [" << EarlierOff
444  << ", " << int64_t(EarlierOff + EarlierSize)
445  << ") Later [" << LaterOff << ", "
446  << int64_t(LaterOff + LaterSize) << ")\n");
447 
448  // Make sure that we only insert non-overlapping intervals and combine
449  // adjacent intervals. The intervals are stored in the map with the ending
450  // offset as the key (in the half-open sense) and the starting offset as
451  // the value.
452  int64_t LaterIntStart = LaterOff, LaterIntEnd = LaterOff + LaterSize;
453 
454  // Find any intervals ending at, or after, LaterIntStart which start
455  // before LaterIntEnd.
456  auto ILI = IM.lower_bound(LaterIntStart);
457  if (ILI != IM.end() && ILI->second <= LaterIntEnd) {
458  // This existing interval is overlapped with the current store somewhere
459  // in [LaterIntStart, LaterIntEnd]. Merge them by erasing the existing
460  // intervals and adjusting our start and end.
461  LaterIntStart = std::min(LaterIntStart, ILI->second);
462  LaterIntEnd = std::max(LaterIntEnd, ILI->first);
463  ILI = IM.erase(ILI);
464 
465  // Continue erasing and adjusting our end in case other previous
466  // intervals are also overlapped with the current store.
467  //
468  // |--- ealier 1 ---| |--- ealier 2 ---|
469  // |------- later---------|
470  //
471  while (ILI != IM.end() && ILI->second <= LaterIntEnd) {
472  assert(ILI->second > LaterIntStart && "Unexpected interval");
473  LaterIntEnd = std::max(LaterIntEnd, ILI->first);
474  ILI = IM.erase(ILI);
475  }
476  }
477 
478  IM[LaterIntEnd] = LaterIntStart;
479 
480  ILI = IM.begin();
481  if (ILI->second <= EarlierOff &&
482  ILI->first >= int64_t(EarlierOff + EarlierSize)) {
483  LLVM_DEBUG(dbgs() << "DSE: Full overwrite from partials: Earlier ["
484  << EarlierOff << ", "
485  << int64_t(EarlierOff + EarlierSize)
486  << ") Composite Later [" << ILI->second << ", "
487  << ILI->first << ")\n");
488  ++NumCompletePartials;
489  return OW_Complete;
490  }
491  }
492 
493  // Check for an earlier store which writes to all the memory locations that
494  // the later store writes to.
495  if (EnablePartialStoreMerging && LaterOff >= EarlierOff &&
496  int64_t(EarlierOff + EarlierSize) > LaterOff &&
497  uint64_t(LaterOff - EarlierOff) + LaterSize <= EarlierSize) {
498  LLVM_DEBUG(dbgs() << "DSE: Partial overwrite an earlier load ["
499  << EarlierOff << ", "
500  << int64_t(EarlierOff + EarlierSize)
501  << ") by a later store [" << LaterOff << ", "
502  << int64_t(LaterOff + LaterSize) << ")\n");
503  // TODO: Maybe come up with a better name?
504  return OW_PartialEarlierWithFullLater;
505  }
506 
507  // Another interesting case is if the later store overwrites the end of the
508  // earlier store.
509  //
510  // |--earlier--|
511  // |-- later --|
512  //
513  // In this case we may want to trim the size of earlier to avoid generating
514  // writes to addresses which will definitely be overwritten later
516  (LaterOff > EarlierOff && LaterOff < int64_t(EarlierOff + EarlierSize) &&
517  int64_t(LaterOff + LaterSize) >= int64_t(EarlierOff + EarlierSize)))
518  return OW_End;
519 
520  // Finally, we also need to check if the later store overwrites the beginning
521  // of the earlier store.
522  //
523  // |--earlier--|
524  // |-- later --|
525  //
526  // In this case we may want to move the destination address and trim the size
527  // of earlier to avoid generating writes to addresses which will definitely
528  // be overwritten later.
530  (LaterOff <= EarlierOff && int64_t(LaterOff + LaterSize) > EarlierOff)) {
531  assert(int64_t(LaterOff + LaterSize) < int64_t(EarlierOff + EarlierSize) &&
532  "Expect to be handled as OW_Complete");
533  return OW_Begin;
534  }
535  // Otherwise, they don't completely overlap.
536  return OW_Unknown;
537 }
538 
539 /// If 'Inst' might be a self read (i.e. a noop copy of a
540 /// memory region into an identical pointer) then it doesn't actually make its
541 /// input dead in the traditional sense. Consider this case:
542 ///
543 /// memmove(A <- B)
544 /// memmove(A <- A)
545 ///
546 /// In this case, the second store to A does not make the first store to A dead.
547 /// The usual situation isn't an explicit A<-A store like this (which can be
548 /// trivially removed) but a case where two pointers may alias.
549 ///
550 /// This function detects when it is unsafe to remove a dependent instruction
551 /// because the DSE inducing instruction may be a self-read.
552 static bool isPossibleSelfRead(Instruction *Inst,
553  const MemoryLocation &InstStoreLoc,
554  Instruction *DepWrite,
555  const TargetLibraryInfo &TLI,
556  AliasAnalysis &AA) {
557  // Self reads can only happen for instructions that read memory. Get the
558  // location read.
559  MemoryLocation InstReadLoc = getLocForRead(Inst, TLI);
560  if (!InstReadLoc.Ptr)
561  return false; // Not a reading instruction.
562 
563  // If the read and written loc obviously don't alias, it isn't a read.
564  if (AA.isNoAlias(InstReadLoc, InstStoreLoc))
565  return false;
566 
567  if (isa<AnyMemCpyInst>(Inst)) {
568  // LLVM's memcpy overlap semantics are not fully fleshed out (see PR11763)
569  // but in practice memcpy(A <- B) either means that A and B are disjoint or
570  // are equal (i.e. there are not partial overlaps). Given that, if we have:
571  //
572  // memcpy/memmove(A <- B) // DepWrite
573  // memcpy(A <- B) // Inst
574  //
575  // with Inst reading/writing a >= size than DepWrite, we can reason as
576  // follows:
577  //
578  // - If A == B then both the copies are no-ops, so the DepWrite can be
579  // removed.
580  // - If A != B then A and B are disjoint locations in Inst. Since
581  // Inst.size >= DepWrite.size A and B are disjoint in DepWrite too.
582  // Therefore DepWrite can be removed.
583  MemoryLocation DepReadLoc = getLocForRead(DepWrite, TLI);
584 
585  if (DepReadLoc.Ptr && AA.isMustAlias(InstReadLoc.Ptr, DepReadLoc.Ptr))
586  return false;
587  }
588 
589  // If DepWrite doesn't read memory or if we can't prove it is a must alias,
590  // then it can't be considered dead.
591  return true;
592 }
593 
594 /// Returns true if the memory which is accessed by the second instruction is not
595 /// modified between the first and the second instruction.
596 /// Precondition: Second instruction must be dominated by the first
597 /// instruction.
599  Instruction *SecondI,
600  AliasAnalysis *AA) {
603  BasicBlock::iterator FirstBBI(FirstI);
604  ++FirstBBI;
605  BasicBlock::iterator SecondBBI(SecondI);
606  BasicBlock *FirstBB = FirstI->getParent();
607  BasicBlock *SecondBB = SecondI->getParent();
608  MemoryLocation MemLoc = MemoryLocation::get(SecondI);
609 
610  // Start checking the store-block.
611  WorkList.push_back(SecondBB);
612  bool isFirstBlock = true;
613 
614  // Check all blocks going backward until we reach the load-block.
615  while (!WorkList.empty()) {
616  BasicBlock *B = WorkList.pop_back_val();
617 
618  // Ignore instructions before LI if this is the FirstBB.
619  BasicBlock::iterator BI = (B == FirstBB ? FirstBBI : B->begin());
620 
622  if (isFirstBlock) {
623  // Ignore instructions after SI if this is the first visit of SecondBB.
624  assert(B == SecondBB && "first block is not the store block");
625  EI = SecondBBI;
626  isFirstBlock = false;
627  } else {
628  // It's not SecondBB or (in case of a loop) the second visit of SecondBB.
629  // In this case we also have to look at instructions after SI.
630  EI = B->end();
631  }
632  for (; BI != EI; ++BI) {
633  Instruction *I = &*BI;
634  if (I->mayWriteToMemory() && I != SecondI)
635  if (isModSet(AA->getModRefInfo(I, MemLoc)))
636  return false;
637  }
638  if (B != FirstBB) {
639  assert(B != &FirstBB->getParent()->getEntryBlock() &&
640  "Should not hit the entry block because SI must be dominated by LI");
641  for (auto PredI = pred_begin(B), PE = pred_end(B); PredI != PE; ++PredI) {
642  if (!Visited.insert(*PredI).second)
643  continue;
644  WorkList.push_back(*PredI);
645  }
646  }
647  }
648  return true;
649 }
650 
651 /// Find all blocks that will unconditionally lead to the block BB and append
652 /// them to F.
654  BasicBlock *BB, DominatorTree *DT) {
655  for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
656  BasicBlock *Pred = *I;
657  if (Pred == BB) continue;
658  Instruction *PredTI = Pred->getTerminator();
659  if (PredTI->getNumSuccessors() != 1)
660  continue;
661 
662  if (DT->isReachableFromEntry(Pred))
663  Blocks.push_back(Pred);
664  }
665 }
666 
667 /// Handle frees of entire structures whose dependency is a store
668 /// to a field of that structure.
669 static bool handleFree(CallInst *F, AliasAnalysis *AA,
671  const TargetLibraryInfo *TLI,
673  MapVector<Instruction *, bool> &ThrowableInst) {
674  bool MadeChange = false;
675 
678  Blocks.push_back(F->getParent());
679  const DataLayout &DL = F->getModule()->getDataLayout();
680 
681  while (!Blocks.empty()) {
682  BasicBlock *BB = Blocks.pop_back_val();
683  Instruction *InstPt = BB->getTerminator();
684  if (BB == F->getParent()) InstPt = F;
685 
686  MemDepResult Dep =
687  MD->getPointerDependencyFrom(Loc, false, InstPt->getIterator(), BB);
688  while (Dep.isDef() || Dep.isClobber()) {
689  Instruction *Dependency = Dep.getInst();
690  if (!hasAnalyzableMemoryWrite(Dependency, *TLI) ||
691  !isRemovable(Dependency))
692  break;
693 
694  Value *DepPointer =
696 
697  // Check for aliasing.
698  if (!AA->isMustAlias(F->getArgOperand(0), DepPointer))
699  break;
700 
701  LLVM_DEBUG(
702  dbgs() << "DSE: Dead Store to soon to be freed memory:\n DEAD: "
703  << *Dependency << '\n');
704 
705  // DCE instructions only used to calculate that store.
706  BasicBlock::iterator BBI(Dependency);
707  deleteDeadInstruction(Dependency, &BBI, *MD, *TLI, IOL, OBB,
708  ThrowableInst);
709  ++NumFastStores;
710  MadeChange = true;
711 
712  // Inst's old Dependency is now deleted. Compute the next dependency,
713  // which may also be dead, as in
714  // s[0] = 0;
715  // s[1] = 0; // This has just been deleted.
716  // free(s);
717  Dep = MD->getPointerDependencyFrom(Loc, false, BBI, BB);
718  }
719 
720  if (Dep.isNonLocal())
721  findUnconditionalPreds(Blocks, BB, DT);
722  }
723 
724  return MadeChange;
725 }
726 
727 /// Check to see if the specified location may alias any of the stack objects in
728 /// the DeadStackObjects set. If so, they become live because the location is
729 /// being loaded.
730 static void removeAccessedObjects(const MemoryLocation &LoadedLoc,
731  SmallSetVector<const Value *, 16> &DeadStackObjects,
732  const DataLayout &DL, AliasAnalysis *AA,
733  const TargetLibraryInfo *TLI,
734  const Function *F) {
735  const Value *UnderlyingPointer = GetUnderlyingObject(LoadedLoc.Ptr, DL);
736 
737  // A constant can't be in the dead pointer set.
738  if (isa<Constant>(UnderlyingPointer))
739  return;
740 
741  // If the kill pointer can be easily reduced to an alloca, don't bother doing
742  // extraneous AA queries.
743  if (isa<AllocaInst>(UnderlyingPointer) || isa<Argument>(UnderlyingPointer)) {
744  DeadStackObjects.remove(UnderlyingPointer);
745  return;
746  }
747 
748  // Remove objects that could alias LoadedLoc.
749  DeadStackObjects.remove_if([&](const Value *I) {
750  // See if the loaded location could alias the stack location.
751  MemoryLocation StackLoc(I, getPointerSize(I, DL, *TLI, F));
752  return !AA->isNoAlias(StackLoc, LoadedLoc);
753  });
754 }
755 
756 /// Remove dead stores to stack-allocated locations in the function end block.
757 /// Ex:
758 /// %A = alloca i32
759 /// ...
760 /// store i32 1, i32* %A
761 /// ret void
764  const TargetLibraryInfo *TLI,
766  MapVector<Instruction *, bool> &ThrowableInst) {
767  bool MadeChange = false;
768 
769  // Keep track of all of the stack objects that are dead at the end of the
770  // function.
771  SmallSetVector<const Value*, 16> DeadStackObjects;
772 
773  // Find all of the alloca'd pointers in the entry block.
774  BasicBlock &Entry = BB.getParent()->front();
775  for (Instruction &I : Entry) {
776  if (isa<AllocaInst>(&I))
777  DeadStackObjects.insert(&I);
778 
779  // Okay, so these are dead heap objects, but if the pointer never escapes
780  // then it's leaked by this function anyways.
781  else if (isAllocLikeFn(&I, TLI) && !PointerMayBeCaptured(&I, true, true))
782  DeadStackObjects.insert(&I);
783  }
784 
785  // Treat byval or inalloca arguments the same, stores to them are dead at the
786  // end of the function.
787  for (Argument &AI : BB.getParent()->args())
788  if (AI.hasByValOrInAllocaAttr())
789  DeadStackObjects.insert(&AI);
790 
791  const DataLayout &DL = BB.getModule()->getDataLayout();
792 
793  // Scan the basic block backwards
794  for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
795  --BBI;
796 
797  // If we find a store, check to see if it points into a dead stack value.
798  if (hasAnalyzableMemoryWrite(&*BBI, *TLI) && isRemovable(&*BBI)) {
799  // See through pointer-to-pointer bitcasts
801  GetUnderlyingObjects(getStoredPointerOperand(&*BBI), Pointers, DL);
802 
803  // Stores to stack values are valid candidates for removal.
804  bool AllDead = true;
805  for (const Value *Pointer : Pointers)
806  if (!DeadStackObjects.count(Pointer)) {
807  AllDead = false;
808  break;
809  }
810 
811  if (AllDead) {
812  Instruction *Dead = &*BBI;
813 
814  LLVM_DEBUG(dbgs() << "DSE: Dead Store at End of Block:\n DEAD: "
815  << *Dead << "\n Objects: ";
817  Pointers.begin(),
818  E = Pointers.end();
819  I != E; ++I) {
820  dbgs() << **I;
821  if (std::next(I) != E)
822  dbgs() << ", ";
823  } dbgs()
824  << '\n');
825 
826  // DCE instructions only used to calculate that store.
827  deleteDeadInstruction(Dead, &BBI, *MD, *TLI, IOL, OBB, ThrowableInst,
828  &DeadStackObjects);
829  ++NumFastStores;
830  MadeChange = true;
831  continue;
832  }
833  }
834 
835  // Remove any dead non-memory-mutating instructions.
836  if (isInstructionTriviallyDead(&*BBI, TLI)) {
837  LLVM_DEBUG(dbgs() << "DSE: Removing trivially dead instruction:\n DEAD: "
838  << *&*BBI << '\n');
839  deleteDeadInstruction(&*BBI, &BBI, *MD, *TLI, IOL, OBB, ThrowableInst,
840  &DeadStackObjects);
841  ++NumFastOther;
842  MadeChange = true;
843  continue;
844  }
845 
846  if (isa<AllocaInst>(BBI)) {
847  // Remove allocas from the list of dead stack objects; there can't be
848  // any references before the definition.
849  DeadStackObjects.remove(&*BBI);
850  continue;
851  }
852 
853  if (auto *Call = dyn_cast<CallBase>(&*BBI)) {
854  // Remove allocation function calls from the list of dead stack objects;
855  // there can't be any references before the definition.
856  if (isAllocLikeFn(&*BBI, TLI))
857  DeadStackObjects.remove(&*BBI);
858 
859  // If this call does not access memory, it can't be loading any of our
860  // pointers.
861  if (AA->doesNotAccessMemory(Call))
862  continue;
863 
864  // If the call might load from any of our allocas, then any store above
865  // the call is live.
866  DeadStackObjects.remove_if([&](const Value *I) {
867  // See if the call site touches the value.
868  return isRefSet(AA->getModRefInfo(
869  Call, I, getPointerSize(I, DL, *TLI, BB.getParent())));
870  });
871 
872  // If all of the allocas were clobbered by the call then we're not going
873  // to find anything else to process.
874  if (DeadStackObjects.empty())
875  break;
876 
877  continue;
878  }
879 
880  // We can remove the dead stores, irrespective of the fence and its ordering
881  // (release/acquire/seq_cst). Fences only constraints the ordering of
882  // already visible stores, it does not make a store visible to other
883  // threads. So, skipping over a fence does not change a store from being
884  // dead.
885  if (isa<FenceInst>(*BBI))
886  continue;
887 
888  MemoryLocation LoadedLoc;
889 
890  // If we encounter a use of the pointer, it is no longer considered dead
891  if (LoadInst *L = dyn_cast<LoadInst>(BBI)) {
892  if (!L->isUnordered()) // Be conservative with atomic/volatile load
893  break;
894  LoadedLoc = MemoryLocation::get(L);
895  } else if (VAArgInst *V = dyn_cast<VAArgInst>(BBI)) {
896  LoadedLoc = MemoryLocation::get(V);
897  } else if (!BBI->mayReadFromMemory()) {
898  // Instruction doesn't read memory. Note that stores that weren't removed
899  // above will hit this case.
900  continue;
901  } else {
902  // Unknown inst; assume it clobbers everything.
903  break;
904  }
905 
906  // Remove any allocas from the DeadPointer set that are loaded, as this
907  // makes any stores above the access live.
908  removeAccessedObjects(LoadedLoc, DeadStackObjects, DL, AA, TLI, BB.getParent());
909 
910  // If all of the allocas were clobbered by the access then we're not going
911  // to find anything else to process.
912  if (DeadStackObjects.empty())
913  break;
914  }
915 
916  return MadeChange;
917 }
918 
919 static bool tryToShorten(Instruction *EarlierWrite, int64_t &EarlierOffset,
920  int64_t &EarlierSize, int64_t LaterOffset,
921  int64_t LaterSize, bool IsOverwriteEnd) {
922  // TODO: base this on the target vector size so that if the earlier
923  // store was too small to get vector writes anyway then its likely
924  // a good idea to shorten it
925  // Power of 2 vector writes are probably always a bad idea to optimize
926  // as any store/memset/memcpy is likely using vector instructions so
927  // shortening it to not vector size is likely to be slower
928  auto *EarlierIntrinsic = cast<AnyMemIntrinsic>(EarlierWrite);
929  unsigned EarlierWriteAlign = EarlierIntrinsic->getDestAlignment();
930  if (!IsOverwriteEnd)
931  LaterOffset = int64_t(LaterOffset + LaterSize);
932 
933  if (!(isPowerOf2_64(LaterOffset) && EarlierWriteAlign <= LaterOffset) &&
934  !((EarlierWriteAlign != 0) && LaterOffset % EarlierWriteAlign == 0))
935  return false;
936 
937  int64_t NewLength = IsOverwriteEnd
938  ? LaterOffset - EarlierOffset
939  : EarlierSize - (LaterOffset - EarlierOffset);
940 
941  if (auto *AMI = dyn_cast<AtomicMemIntrinsic>(EarlierWrite)) {
942  // When shortening an atomic memory intrinsic, the newly shortened
943  // length must remain an integer multiple of the element size.
944  const uint32_t ElementSize = AMI->getElementSizeInBytes();
945  if (0 != NewLength % ElementSize)
946  return false;
947  }
948 
949  LLVM_DEBUG(dbgs() << "DSE: Remove Dead Store:\n OW "
950  << (IsOverwriteEnd ? "END" : "BEGIN") << ": "
951  << *EarlierWrite << "\n KILLER (offset " << LaterOffset
952  << ", " << EarlierSize << ")\n");
953 
954  Value *EarlierWriteLength = EarlierIntrinsic->getLength();
955  Value *TrimmedLength =
956  ConstantInt::get(EarlierWriteLength->getType(), NewLength);
957  EarlierIntrinsic->setLength(TrimmedLength);
958 
959  EarlierSize = NewLength;
960  if (!IsOverwriteEnd) {
961  int64_t OffsetMoved = (LaterOffset - EarlierOffset);
962  Value *Indices[1] = {
963  ConstantInt::get(EarlierWriteLength->getType(), OffsetMoved)};
965  EarlierIntrinsic->getRawDest()->getType()->getPointerElementType(),
966  EarlierIntrinsic->getRawDest(), Indices, "", EarlierWrite);
967  NewDestGEP->setDebugLoc(EarlierIntrinsic->getDebugLoc());
968  EarlierIntrinsic->setDest(NewDestGEP);
969  EarlierOffset = EarlierOffset + OffsetMoved;
970  }
971  return true;
972 }
973 
974 static bool tryToShortenEnd(Instruction *EarlierWrite,
976  int64_t &EarlierStart, int64_t &EarlierSize) {
977  if (IntervalMap.empty() || !isShortenableAtTheEnd(EarlierWrite))
978  return false;
979 
980  OverlapIntervalsTy::iterator OII = --IntervalMap.end();
981  int64_t LaterStart = OII->second;
982  int64_t LaterSize = OII->first - LaterStart;
983 
984  if (LaterStart > EarlierStart && LaterStart < EarlierStart + EarlierSize &&
985  LaterStart + LaterSize >= EarlierStart + EarlierSize) {
986  if (tryToShorten(EarlierWrite, EarlierStart, EarlierSize, LaterStart,
987  LaterSize, true)) {
988  IntervalMap.erase(OII);
989  return true;
990  }
991  }
992  return false;
993 }
994 
995 static bool tryToShortenBegin(Instruction *EarlierWrite,
997  int64_t &EarlierStart, int64_t &EarlierSize) {
998  if (IntervalMap.empty() || !isShortenableAtTheBeginning(EarlierWrite))
999  return false;
1000 
1001  OverlapIntervalsTy::iterator OII = IntervalMap.begin();
1002  int64_t LaterStart = OII->second;
1003  int64_t LaterSize = OII->first - LaterStart;
1004 
1005  if (LaterStart <= EarlierStart && LaterStart + LaterSize > EarlierStart) {
1006  assert(LaterStart + LaterSize < EarlierStart + EarlierSize &&
1007  "Should have been handled as OW_Complete");
1008  if (tryToShorten(EarlierWrite, EarlierStart, EarlierSize, LaterStart,
1009  LaterSize, false)) {
1010  IntervalMap.erase(OII);
1011  return true;
1012  }
1013  }
1014  return false;
1015 }
1016 
1018  const DataLayout &DL,
1019  InstOverlapIntervalsTy &IOL) {
1020  bool Changed = false;
1021  for (auto OI : IOL) {
1022  Instruction *EarlierWrite = OI.first;
1023  MemoryLocation Loc = getLocForWrite(EarlierWrite);
1024  assert(isRemovable(EarlierWrite) && "Expect only removable instruction");
1025 
1026  const Value *Ptr = Loc.Ptr->stripPointerCasts();
1027  int64_t EarlierStart = 0;
1028  int64_t EarlierSize = int64_t(Loc.Size.getValue());
1029  GetPointerBaseWithConstantOffset(Ptr, EarlierStart, DL);
1030  OverlapIntervalsTy &IntervalMap = OI.second;
1031  Changed |=
1032  tryToShortenEnd(EarlierWrite, IntervalMap, EarlierStart, EarlierSize);
1033  if (IntervalMap.empty())
1034  continue;
1035  Changed |=
1036  tryToShortenBegin(EarlierWrite, IntervalMap, EarlierStart, EarlierSize);
1037  }
1038  return Changed;
1039 }
1040 
1043  const DataLayout &DL,
1044  const TargetLibraryInfo *TLI,
1046  OrderedBasicBlock &OBB,
1047  MapVector<Instruction *, bool> &ThrowableInst) {
1048  // Must be a store instruction.
1049  StoreInst *SI = dyn_cast<StoreInst>(Inst);
1050  if (!SI)
1051  return false;
1052 
1053  // If we're storing the same value back to a pointer that we just loaded from,
1054  // then the store can be removed.
1055  if (LoadInst *DepLoad = dyn_cast<LoadInst>(SI->getValueOperand())) {
1056  if (SI->getPointerOperand() == DepLoad->getPointerOperand() &&
1057  isRemovable(SI) && memoryIsNotModifiedBetween(DepLoad, SI, AA)) {
1058 
1059  LLVM_DEBUG(
1060  dbgs() << "DSE: Remove Store Of Load from same pointer:\n LOAD: "
1061  << *DepLoad << "\n STORE: " << *SI << '\n');
1062 
1063  deleteDeadInstruction(SI, &BBI, *MD, *TLI, IOL, OBB, ThrowableInst);
1064  ++NumRedundantStores;
1065  return true;
1066  }
1067  }
1068 
1069  // Remove null stores into the calloc'ed objects
1070  Constant *StoredConstant = dyn_cast<Constant>(SI->getValueOperand());
1071  if (StoredConstant && StoredConstant->isNullValue() && isRemovable(SI)) {
1072  Instruction *UnderlyingPointer =
1074 
1075  if (UnderlyingPointer && isCallocLikeFn(UnderlyingPointer, TLI) &&
1076  memoryIsNotModifiedBetween(UnderlyingPointer, SI, AA)) {
1077  LLVM_DEBUG(
1078  dbgs() << "DSE: Remove null store to the calloc'ed object:\n DEAD: "
1079  << *Inst << "\n OBJECT: " << *UnderlyingPointer << '\n');
1080 
1081  deleteDeadInstruction(SI, &BBI, *MD, *TLI, IOL, OBB, ThrowableInst);
1082  ++NumRedundantStores;
1083  return true;
1084  }
1085  }
1086  return false;
1087 }
1088 
1091  const TargetLibraryInfo *TLI) {
1092  const DataLayout &DL = BB.getModule()->getDataLayout();
1093  bool MadeChange = false;
1094 
1095  OrderedBasicBlock OBB(&BB);
1096  MapVector<Instruction *, bool> ThrowableInst;
1097 
1098  // A map of interval maps representing partially-overwritten value parts.
1100 
1101  // Do a top-down walk on the BB.
1102  for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end(); BBI != BBE; ) {
1103  // Handle 'free' calls specially.
1104  if (CallInst *F = isFreeCall(&*BBI, TLI)) {
1105  MadeChange |= handleFree(F, AA, MD, DT, TLI, IOL, OBB, ThrowableInst);
1106  // Increment BBI after handleFree has potentially deleted instructions.
1107  // This ensures we maintain a valid iterator.
1108  ++BBI;
1109  continue;
1110  }
1111 
1112  Instruction *Inst = &*BBI++;
1113 
1114  if (Inst->mayThrow()) {
1115  ThrowableInst[Inst] = true;
1116  continue;
1117  }
1118 
1119  // Check to see if Inst writes to memory. If not, continue.
1120  if (!hasAnalyzableMemoryWrite(Inst, *TLI))
1121  continue;
1122 
1123  // eliminateNoopStore will update in iterator, if necessary.
1124  if (eliminateNoopStore(Inst, BBI, AA, MD, DL, TLI, IOL, OBB,
1125  ThrowableInst)) {
1126  MadeChange = true;
1127  continue;
1128  }
1129 
1130  // If we find something that writes memory, get its memory dependence.
1131  MemDepResult InstDep = MD->getDependency(Inst, &OBB);
1132 
1133  // Ignore any store where we can't find a local dependence.
1134  // FIXME: cross-block DSE would be fun. :)
1135  if (!InstDep.isDef() && !InstDep.isClobber())
1136  continue;
1137 
1138  // Figure out what location is being stored to.
1139  MemoryLocation Loc = getLocForWrite(Inst);
1140 
1141  // If we didn't get a useful location, fail.
1142  if (!Loc.Ptr)
1143  continue;
1144 
1145  // Loop until we find a store we can eliminate or a load that
1146  // invalidates the analysis. Without an upper bound on the number of
1147  // instructions examined, this analysis can become very time-consuming.
1148  // However, the potential gain diminishes as we process more instructions
1149  // without eliminating any of them. Therefore, we limit the number of
1150  // instructions we look at.
1151  auto Limit = MD->getDefaultBlockScanLimit();
1152  while (InstDep.isDef() || InstDep.isClobber()) {
1153  // Get the memory clobbered by the instruction we depend on. MemDep will
1154  // skip any instructions that 'Loc' clearly doesn't interact with. If we
1155  // end up depending on a may- or must-aliased load, then we can't optimize
1156  // away the store and we bail out. However, if we depend on something
1157  // that overwrites the memory location we *can* potentially optimize it.
1158  //
1159  // Find out what memory location the dependent instruction stores.
1160  Instruction *DepWrite = InstDep.getInst();
1161  if (!hasAnalyzableMemoryWrite(DepWrite, *TLI))
1162  break;
1163  MemoryLocation DepLoc = getLocForWrite(DepWrite);
1164  // If we didn't get a useful location, or if it isn't a size, bail out.
1165  if (!DepLoc.Ptr)
1166  break;
1167 
1168  // Find the last throwable instruction not removed by call to
1169  // deleteDeadInstruction.
1170  Instruction *LastThrowing = nullptr;
1171  if (!ThrowableInst.empty())
1172  LastThrowing = ThrowableInst.back().first;
1173 
1174  // Make sure we don't look past a call which might throw. This is an
1175  // issue because MemoryDependenceAnalysis works in the wrong direction:
1176  // it finds instructions which dominate the current instruction, rather than
1177  // instructions which are post-dominated by the current instruction.
1178  //
1179  // If the underlying object is a non-escaping memory allocation, any store
1180  // to it is dead along the unwind edge. Otherwise, we need to preserve
1181  // the store.
1182  if (LastThrowing && OBB.dominates(DepWrite, LastThrowing)) {
1183  const Value* Underlying = GetUnderlyingObject(DepLoc.Ptr, DL);
1184  bool IsStoreDeadOnUnwind = isa<AllocaInst>(Underlying);
1185  if (!IsStoreDeadOnUnwind) {
1186  // We're looking for a call to an allocation function
1187  // where the allocation doesn't escape before the last
1188  // throwing instruction; PointerMayBeCaptured
1189  // reasonably fast approximation.
1190  IsStoreDeadOnUnwind = isAllocLikeFn(Underlying, TLI) &&
1191  !PointerMayBeCaptured(Underlying, false, true);
1192  }
1193  if (!IsStoreDeadOnUnwind)
1194  break;
1195  }
1196 
1197  // If we find a write that is a) removable (i.e., non-volatile), b) is
1198  // completely obliterated by the store to 'Loc', and c) which we know that
1199  // 'Inst' doesn't load from, then we can remove it.
1200  // Also try to merge two stores if a later one only touches memory written
1201  // to by the earlier one.
1202  if (isRemovable(DepWrite) &&
1203  !isPossibleSelfRead(Inst, Loc, DepWrite, *TLI, *AA)) {
1204  int64_t InstWriteOffset, DepWriteOffset;
1205  OverwriteResult OR = isOverwrite(Loc, DepLoc, DL, *TLI, DepWriteOffset,
1206  InstWriteOffset, DepWrite, IOL, *AA,
1207  BB.getParent());
1208  if (OR == OW_Complete) {
1209  LLVM_DEBUG(dbgs() << "DSE: Remove Dead Store:\n DEAD: " << *DepWrite
1210  << "\n KILLER: " << *Inst << '\n');
1211 
1212  // Delete the store and now-dead instructions that feed it.
1213  deleteDeadInstruction(DepWrite, &BBI, *MD, *TLI, IOL, OBB,
1214  ThrowableInst);
1215  ++NumFastStores;
1216  MadeChange = true;
1217 
1218  // We erased DepWrite; start over.
1219  InstDep = MD->getDependency(Inst, &OBB);
1220  continue;
1221  } else if ((OR == OW_End && isShortenableAtTheEnd(DepWrite)) ||
1222  ((OR == OW_Begin &&
1223  isShortenableAtTheBeginning(DepWrite)))) {
1224  assert(!EnablePartialOverwriteTracking && "Do not expect to perform "
1225  "when partial-overwrite "
1226  "tracking is enabled");
1227  // The overwrite result is known, so these must be known, too.
1228  int64_t EarlierSize = DepLoc.Size.getValue();
1229  int64_t LaterSize = Loc.Size.getValue();
1230  bool IsOverwriteEnd = (OR == OW_End);
1231  MadeChange |= tryToShorten(DepWrite, DepWriteOffset, EarlierSize,
1232  InstWriteOffset, LaterSize, IsOverwriteEnd);
1233  } else if (EnablePartialStoreMerging &&
1234  OR == OW_PartialEarlierWithFullLater) {
1235  auto *Earlier = dyn_cast<StoreInst>(DepWrite);
1236  auto *Later = dyn_cast<StoreInst>(Inst);
1237  if (Earlier && isa<ConstantInt>(Earlier->getValueOperand()) &&
1239  Earlier->getValueOperand()->getType()) &&
1240  Later && isa<ConstantInt>(Later->getValueOperand()) &&
1242  Later->getValueOperand()->getType()) &&
1243  memoryIsNotModifiedBetween(Earlier, Later, AA)) {
1244  // If the store we find is:
1245  // a) partially overwritten by the store to 'Loc'
1246  // b) the later store is fully contained in the earlier one and
1247  // c) they both have a constant value
1248  // d) none of the two stores need padding
1249  // Merge the two stores, replacing the earlier store's value with a
1250  // merge of both values.
1251  // TODO: Deal with other constant types (vectors, etc), and probably
1252  // some mem intrinsics (if needed)
1253 
1254  APInt EarlierValue =
1255  cast<ConstantInt>(Earlier->getValueOperand())->getValue();
1256  APInt LaterValue =
1257  cast<ConstantInt>(Later->getValueOperand())->getValue();
1258  unsigned LaterBits = LaterValue.getBitWidth();
1259  assert(EarlierValue.getBitWidth() > LaterValue.getBitWidth());
1260  LaterValue = LaterValue.zext(EarlierValue.getBitWidth());
1261 
1262  // Offset of the smaller store inside the larger store
1263  unsigned BitOffsetDiff = (InstWriteOffset - DepWriteOffset) * 8;
1264  unsigned LShiftAmount =
1265  DL.isBigEndian()
1266  ? EarlierValue.getBitWidth() - BitOffsetDiff - LaterBits
1267  : BitOffsetDiff;
1268  APInt Mask =
1269  APInt::getBitsSet(EarlierValue.getBitWidth(), LShiftAmount,
1270  LShiftAmount + LaterBits);
1271  // Clear the bits we'll be replacing, then OR with the smaller
1272  // store, shifted appropriately.
1273  APInt Merged =
1274  (EarlierValue & ~Mask) | (LaterValue << LShiftAmount);
1275  LLVM_DEBUG(dbgs() << "DSE: Merge Stores:\n Earlier: " << *DepWrite
1276  << "\n Later: " << *Inst
1277  << "\n Merged Value: " << Merged << '\n');
1278 
1279  auto *SI = new StoreInst(
1280  ConstantInt::get(Earlier->getValueOperand()->getType(), Merged),
1281  Earlier->getPointerOperand(), false,
1282  MaybeAlign(Earlier->getAlignment()), Earlier->getOrdering(),
1283  Earlier->getSyncScopeID(), DepWrite);
1284 
1285  unsigned MDToKeep[] = {LLVMContext::MD_dbg, LLVMContext::MD_tbaa,
1286  LLVMContext::MD_alias_scope,
1287  LLVMContext::MD_noalias,
1288  LLVMContext::MD_nontemporal};
1289  SI->copyMetadata(*DepWrite, MDToKeep);
1290  ++NumModifiedStores;
1291 
1292  // Remove earlier, wider, store
1293  OBB.replaceInstruction(DepWrite, SI);
1294 
1295  // Delete the old stores and now-dead instructions that feed them.
1296  deleteDeadInstruction(Inst, &BBI, *MD, *TLI, IOL, OBB,
1297  ThrowableInst);
1298  deleteDeadInstruction(DepWrite, &BBI, *MD, *TLI, IOL, OBB,
1299  ThrowableInst);
1300  MadeChange = true;
1301 
1302  // We erased DepWrite and Inst (Loc); start over.
1303  break;
1304  }
1305  }
1306  }
1307 
1308  // If this is a may-aliased store that is clobbering the store value, we
1309  // can keep searching past it for another must-aliased pointer that stores
1310  // to the same location. For example, in:
1311  // store -> P
1312  // store -> Q
1313  // store -> P
1314  // we can remove the first store to P even though we don't know if P and Q
1315  // alias.
1316  if (DepWrite == &BB.front()) break;
1317 
1318  // Can't look past this instruction if it might read 'Loc'.
1319  if (isRefSet(AA->getModRefInfo(DepWrite, Loc)))
1320  break;
1321 
1322  InstDep = MD->getPointerDependencyFrom(Loc, /*isLoad=*/ false,
1323  DepWrite->getIterator(), &BB,
1324  /*QueryInst=*/ nullptr, &Limit);
1325  }
1326  }
1327 
1329  MadeChange |= removePartiallyOverlappedStores(AA, DL, IOL);
1330 
1331  // If this block ends in a return, unwind, or unreachable, all allocas are
1332  // dead at its end, which means stores to them are also dead.
1333  if (BB.getTerminator()->getNumSuccessors() == 0)
1334  MadeChange |= handleEndBlock(BB, AA, MD, TLI, IOL, OBB, ThrowableInst);
1335 
1336  return MadeChange;
1337 }
1338 
1341  const TargetLibraryInfo *TLI) {
1342  bool MadeChange = false;
1343  for (BasicBlock &BB : F)
1344  // Only check non-dead blocks. Dead blocks may have strange pointer
1345  // cycles that will confuse alias analysis.
1346  if (DT->isReachableFromEntry(&BB))
1347  MadeChange |= eliminateDeadStores(BB, AA, MD, DT, TLI);
1348 
1349  return MadeChange;
1350 }
1351 
1352 //===----------------------------------------------------------------------===//
1353 // DSE Pass
1354 //===----------------------------------------------------------------------===//
1356  AliasAnalysis *AA = &AM.getResult<AAManager>(F);
1360 
1361  if (!eliminateDeadStores(F, AA, MD, DT, TLI))
1362  return PreservedAnalyses::all();
1363 
1364  PreservedAnalyses PA;
1365  PA.preserveSet<CFGAnalyses>();
1366  PA.preserve<GlobalsAA>();
1368  return PA;
1369 }
1370 
1371 namespace {
1372 
1373 /// A legacy pass for the legacy pass manager that wraps \c DSEPass.
1374 class DSELegacyPass : public FunctionPass {
1375 public:
1376  static char ID; // Pass identification, replacement for typeid
1377 
1378  DSELegacyPass() : FunctionPass(ID) {
1380  }
1381 
1382  bool runOnFunction(Function &F) override {
1383  if (skipFunction(F))
1384  return false;
1385 
1386  DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1387  AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
1389  &getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
1390  const TargetLibraryInfo *TLI =
1391  &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
1392 
1393  return eliminateDeadStores(F, AA, MD, DT, TLI);
1394  }
1395 
1396  void getAnalysisUsage(AnalysisUsage &AU) const override {
1397  AU.setPreservesCFG();
1405  }
1406 };
1407 
1408 } // end anonymous namespace
1409 
1410 char DSELegacyPass::ID = 0;
1411 
1412 INITIALIZE_PASS_BEGIN(DSELegacyPass, "dse", "Dead Store Elimination", false,
1413  false)
1420  false)
1421 
1423  return new DSELegacyPass();
1424 }
Legacy wrapper pass to provide the GlobalsAAResult object.
bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL, const TargetLibraryInfo *TLI, ObjectSizeOpts Opts={})
Compute the size of the object pointed by Ptr.
Value * getValueOperand()
Definition: Instructions.h:426
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:67
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
void eraseInstruction(const Instruction *I)
Remove from the ordering, if it is present.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Provides a lazy, caching interface for making common memory aliasing information queries, backed by LLVM&#39;s alias analysis passes.
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL, bool AllowNonInbounds=true)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents an incoming formal argument to a Function.
Definition: Argument.h:29
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:777
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This is the interface for a simple mod/ref and alias analysis over globals.
void initializeDSELegacyPassPass(PassRegistry &)
bool isNoAlias(const MemoryLocation &LocA, const MemoryLocation &LocB)
A trivial helper function to check to see if the specified pointers are no-alias. ...
bool isPrecise() const
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:937
bool NullIsUnknownSize
If this is true, null pointers in address space 0 will be treated as though they can&#39;t be evaluated...
This class represents a function call, abstracting a target machine&#39;s calling convention.
bool isNonLocal() const
Tests if this MemDepResult represents a query that is transparent to the start of the block...
void pop_back()
Remove the last element from the vector.
Definition: MapVector.h:160
bool dominates(const Instruction *A, const Instruction *B)
Find out whether A dominates B, meaning whether A comes before B in BB.
bool mayWriteToMemory() const
Return true if this instruction may modify memory.
bool salvageDebugInfo(Instruction &I)
Assuming the instruction I is going to be deleted, attempt to salvage debug users of I by writing the...
Definition: Local.cpp:1615
MemDepResult getDependency(Instruction *QueryInst, OrderedBasicBlock *OBB=nullptr)
Returns the instruction on which a memory operation depends.
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:37
STATISTIC(NumFunctions, "Total number of functions")
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:230
F(f)
An instruction for reading from memory.
Definition: Instructions.h:169
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:144
static bool isPossibleSelfRead(Instruction *Inst, const MemoryLocation &InstStoreLoc, Instruction *DepWrite, const TargetLibraryInfo &TLI, AliasAnalysis &AA)
If &#39;Inst&#39; might be a self read (i.e.
static void removeAccessedObjects(const MemoryLocation &LoadedLoc, SmallSetVector< const Value *, 16 > &DeadStackObjects, const DataLayout &DL, AliasAnalysis *AA, const TargetLibraryInfo *TLI, const Function *F)
Check to see if the specified location may alias any of the stack objects in the DeadStackObjects set...
#define op(i)
bool typeSizeEqualsStoreSize(Type *Ty) const
Returns true if no extra padding bits are needed when storing the specified type. ...
Definition: DataLayout.h:474
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:300
static bool eliminateNoopStore(Instruction *Inst, BasicBlock::iterator &BBI, AliasAnalysis *AA, MemoryDependenceResults *MD, const DataLayout &DL, const TargetLibraryInfo *TLI, InstOverlapIntervalsTy &IOL, OrderedBasicBlock &OBB, MapVector< Instruction *, bool > &ThrowableInst)
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1566
static void findUnconditionalPreds(SmallVectorImpl< BasicBlock *> &Blocks, BasicBlock *BB, DominatorTree *DT)
Find all blocks that will unconditionally lead to the block BB and append them to F...
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
const CallInst * isFreeCall(const Value *I, const TargetLibraryInfo *TLI)
isFreeCall - Returns non-null if the value is a call to the builtin free()
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:273
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1246
AnalysisUsage & addRequired()
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
Definition: BasicBlock.cpp:140
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
bool isDef() const
Tests if this MemDepResult represents a query that is an instruction definition dependency.
std::pair< KeyT, ValueT > & back()
Definition: MapVector.h:85
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
bool isClobber() const
Tests if this MemDepResult represents a query that is an instruction clobber dependency.
unsigned getDefaultBlockScanLimit() const
Some methods limit the number of instructions they will examine.
An analysis that produces MemoryDependenceResults for a function.
bool empty() const
Definition: MapVector.h:79
static MemoryLocation getLocForWrite(Instruction *Inst)
Return a Location stored to by the specified instruction.
bool remove(const value_type &X)
Remove an item from the set vector.
Definition: SetVector.h:157
This file implements a class to represent arbitrary precision integral constant values and operations...
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:86
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:246
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:141
static Value * getStoredPointerOperand(Instruction *I)
Return the pointer that is being written to.
static MemoryLocation getForDest(const MemIntrinsic *MI)
Return a location representing the destination of a memory set or transfer.
bool has(LibFunc F) const
Tests whether a library function is available.
std::map< int64_t, int64_t > OverlapIntervalsTy
static uint64_t getPointerSize(const Value *V, const DataLayout &DL, const TargetLibraryInfo &TLI, const Function *F)
An instruction for storing to memory.
Definition: Instructions.h:331
iterator find(const KeyT &Key)
Definition: MapVector.h:147
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
unsigned getNumSuccessors() const
Return the number of successors that this instruction has.
Value * getOperand(unsigned i) const
Definition: User.h:169
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
Definition: SetVector.h:210
uint64_t getValue() const
const BasicBlock & getEntryBlock() const
Definition: Function.h:675
static bool handleFree(CallInst *F, AliasAnalysis *AA, MemoryDependenceResults *MD, DominatorTree *DT, const TargetLibraryInfo *TLI, InstOverlapIntervalsTy &IOL, OrderedBasicBlock &OBB, MapVector< Instruction *, bool > &ThrowableInst)
Handle frees of entire structures whose dependency is a store to a field of that structure.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
Definition: Instructions.h:892
static bool runOnFunction(Function &F, bool PostInlining)
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:435
bool erase(const KeyT &Val)
Definition: DenseMap.h:272
void GetUnderlyingObjects(const Value *V, SmallVectorImpl< const Value *> &Objects, const DataLayout &DL, LoopInfo *LI=nullptr, unsigned MaxLookup=6)
This method is similar to GetUnderlyingObject except that it can look through phi and select instruct...
static bool isShortenableAtTheBeginning(Instruction *I)
Returns true if the beginning of this instruction can be safely shortened in length.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:328
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
static bool removePartiallyOverlappedStores(AliasAnalysis *AA, const DataLayout &DL, InstOverlapIntervalsTy &IOL)
static OverwriteResult isOverwrite(const MemoryLocation &Later, const MemoryLocation &Earlier, const DataLayout &DL, const TargetLibraryInfo &TLI, int64_t &EarlierOff, int64_t &LaterOff, Instruction *DepWrite, InstOverlapIntervalsTy &IOL, AliasAnalysis &AA, const Function *F)
Return &#39;OW_Complete&#39; if a store to the &#39;Later&#39; location completely overwrites a store to the &#39;Earlier...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const Instruction & front() const
Definition: BasicBlock.h:285
A manager for alias analyses.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:370
static bool handleEndBlock(BasicBlock &BB, AliasAnalysis *AA, MemoryDependenceResults *MD, const TargetLibraryInfo *TLI, InstOverlapIntervalsTy &IOL, OrderedBasicBlock &OBB, MapVector< Instruction *, bool > &ThrowableInst)
Remove dead stores to stack-allocated locations in the function end block.
bool mayThrow() const
Return true if this instruction may throw an exception.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:112
static bool isShortenableAtTheEnd(Instruction *I)
Returns true if the end of this instruction can be safely shortened in length.
Represent the analysis usage information of a pass.
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:470
FunctionPass * createDeadStoreEliminationPass()
static bool memoryIsNotModifiedBetween(Instruction *FirstI, Instruction *SecondI, AliasAnalysis *AA)
Returns true if the memory which is accessed by the second instruction is not modified between the fi...
std::underlying_type< E >::type Underlying(E Val)
Check that Val is in range for E, and return Val cast to E&#39;s underlying type.
Definition: BitmaskEnum.h:90
Analysis pass providing a never-invalidated alias analysis result.
constexpr double e
Definition: MathExtras.h:57
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:282
LocationSize Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known...
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:115
self_iterator getIterator()
Definition: ilist_node.h:81
INITIALIZE_PASS_BEGIN(DSELegacyPass, "dse", "Dead Store Elimination", false, false) INITIALIZE_PASS_END(DSELegacyPass
MemDepResult getPointerDependencyFrom(const MemoryLocation &Loc, bool isLoad, BasicBlock::iterator ScanIt, BasicBlock *BB, Instruction *QueryInst=nullptr, unsigned *Limit=nullptr, OrderedBasicBlock *OBB=nullptr)
Returns the instruction on which a memory location depends.
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:530
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
Dead Store Elimination
A wrapper analysis pass for the legacy pass manager that exposes a MemoryDepnedenceResults instance...
static void deleteDeadInstruction(Instruction *I, BasicBlock::iterator *BBI, MemoryDependenceResults &MD, const TargetLibraryInfo &TLI, InstOverlapIntervalsTy &IOL, OrderedBasicBlock &OBB, MapVector< Instruction *, bool > &ThrowableInst, SmallSetVector< const Value *, 16 > *ValueSet=nullptr)
Delete this instruction.
Value * GetUnderlyingObject(Value *V, const DataLayout &DL, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A memory dependence query can return one of three different answers.
const Value * Ptr
The address of the start of the location.
Representation for a specific memory location.
static cl::opt< bool > EnablePartialOverwriteTracking("enable-dse-partial-overwrite-tracking", cl::init(true), cl::Hidden, cl::desc("Enable partial-overwrite tracking in DSE"))
static cl::opt< bool > EnablePartialStoreMerging("enable-dse-partial-store-merging", cl::init(true), cl::Hidden, cl::desc("Enable partial store merging in DSE"))
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:297
Iterator for intrusive lists based on ilist_node.
unsigned getNumOperands() const
Definition: User.h:191
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:443
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:390
iterator end()
Definition: BasicBlock.h:275
This struct is a compact representation of a valid (power of two) or undefined (0) alignment...
Definition: Alignment.h:117
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Module.h This file contains the declarations for the Module class.
Provides information about what library functions are available for the current target.
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:374
static bool hasAnalyzableMemoryWrite(Instruction *I, const TargetLibraryInfo &TLI)
Does this instruction write some memory? This only returns true for things that we can analyze with o...
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:704
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
Definition: Function.cpp:1628
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:55
Class for arbitrary precision integers.
Definition: APInt.h:69
bool isMustAlias(const MemoryLocation &LocA, const MemoryLocation &LocB)
A trivial helper function to check to see if the specified pointers are must-alias.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
Represents analyses that only rely on functions&#39; control flow.
Definition: PassManager.h:115
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition: APInt.h:611
LLVM_NODISCARD bool isModSet(const ModRefInfo MRI)
static bool tryToShortenBegin(Instruction *EarlierWrite, OverlapIntervalsTy &IntervalMap, int64_t &EarlierStart, int64_t &EarlierSize)
bool doesNotAccessMemory(const CallBase *Call)
Checks if the specified call is known to never read or write memory.
Instruction * getInst() const
If this is a normal dependency, returns the instruction that is depended on.
static MemoryLocation getLocForRead(Instruction *Inst, const TargetLibraryInfo &TLI)
Return the location read by the specified "hasAnalyzableMemoryWrite" instruction if any...
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
This file provides utility analysis objects describing memory locations.
void preserveSet()
Mark an analysis set as preserved.
Definition: PassManager.h:190
void replaceInstruction(const Instruction *Old, const Instruction *New)
Replace Old with New in the ordering.
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
bool isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates zero-filled memory (such as...
#define I(x, y, z)
Definition: MD5.cpp:58
bool empty() const
Determine if the SetVector is empty or not.
Definition: SetVector.h:72
bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates memory (either malloc...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
uint32_t Size
Definition: Profile.cpp:46
void preserve()
Mark an analysis as preserved.
Definition: PassManager.h:175
Analysis pass providing the TargetLibraryInfo.
static GetElementPtrInst * CreateInBounds(Value *Ptr, ArrayRef< Value *> IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Create an "inbounds" getelementptr.
Definition: Instructions.h:952
static bool eliminateDeadStores(BasicBlock &BB, AliasAnalysis *AA, MemoryDependenceResults *MD, DominatorTree *DT, const TargetLibraryInfo *TLI)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
iterator end()
Definition: MapVector.h:71
const BasicBlock & front() const
Definition: Function.h:698
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction has no side ef...
Definition: Local.cpp:359
LLVM Value Representation.
Definition: Value.h:74
void removeInstruction(Instruction *InstToRemove)
Removes an instruction from the dependence analysis, updating the dependence of instructions that pre...
bool remove_if(UnaryPredicate P)
Remove items from the set vector based on a predicate function.
Definition: SetVector.h:199
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
static bool isRemovable(Instruction *I)
If the value of this instruction and the memory it writes to is unused, may we delete this instructio...
IRTranslator LLVM IR MI
Various options to control the behavior of getObjectSize.
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:259
static bool isVolatile(Instruction *Inst)
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
This header defines various interfaces for pass management in LLVM.
bool isBigEndian() const
Definition: DataLayout.h:233
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc)
getModRefInfo (for call sites) - Return information about whether a particular call site modifies or ...
#define LLVM_DEBUG(X)
Definition: Debug.h:122
Value * getPointerOperand()
Definition: Instructions.h:429
bool use_empty() const
Definition: Value.h:343
static bool tryToShorten(Instruction *EarlierWrite, int64_t &EarlierOffset, int64_t &EarlierSize, int64_t LaterOffset, int64_t LaterSize, bool IsOverwriteEnd)
iterator_range< arg_iterator > args()
Definition: Function.h:730
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:44
const BasicBlock * getParent() const
Definition: Instruction.h:66
static bool tryToShortenEnd(Instruction *EarlierWrite, OverlapIntervalsTy &IntervalMap, int64_t &EarlierStart, int64_t &EarlierSize)
bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures, bool StoreCaptures, unsigned MaxUsesToExplore=DefaultMaxUsesToExplore)
PointerMayBeCaptured - Return true if this pointer value may be captured by the enclosing function (w...
static MemoryLocation getForSource(const MemTransferInst *MTI)
Return a location representing the source of a memory transfer.
LLVM_NODISCARD bool isRefSet(const ModRefInfo MRI)