LLVM 23.0.0git
DeadStoreElimination.cpp
Go to the documentation of this file.
1//===- DeadStoreElimination.cpp - MemorySSA Backed 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// The code below implements dead store elimination using MemorySSA. It uses
10// the following general approach: given a MemoryDef, walk upwards to find
11// clobbering MemoryDefs that may be killed by the starting def. Then check
12// that there are no uses that may read the location of the original MemoryDef
13// in between both MemoryDefs. A bit more concretely:
14//
15// For all MemoryDefs StartDef:
16// 1. Get the next dominating clobbering MemoryDef (MaybeDeadAccess) by walking
17// upwards.
18// 2. Check that there are no reads between MaybeDeadAccess and the StartDef by
19// checking all uses starting at MaybeDeadAccess and walking until we see
20// StartDef.
21// 3. For each found CurrentDef, check that:
22// 1. There are no barrier instructions between CurrentDef and StartDef (like
23// throws or stores with ordering constraints).
24// 2. StartDef is executed whenever CurrentDef is executed.
25// 3. StartDef completely overwrites CurrentDef.
26// 4. Erase CurrentDef from the function and MemorySSA.
27//
28//===----------------------------------------------------------------------===//
29
31#include "llvm/ADT/APInt.h"
32#include "llvm/ADT/DenseMap.h"
33#include "llvm/ADT/MapVector.h"
35#include "llvm/ADT/SetVector.h"
38#include "llvm/ADT/Statistic.h"
39#include "llvm/ADT/StringRef.h"
53#include "llvm/IR/Argument.h"
55#include "llvm/IR/BasicBlock.h"
56#include "llvm/IR/Constant.h"
58#include "llvm/IR/Constants.h"
59#include "llvm/IR/DataLayout.h"
60#include "llvm/IR/DebugInfo.h"
61#include "llvm/IR/Dominators.h"
62#include "llvm/IR/Function.h"
63#include "llvm/IR/IRBuilder.h"
65#include "llvm/IR/InstrTypes.h"
66#include "llvm/IR/Instruction.h"
69#include "llvm/IR/Module.h"
70#include "llvm/IR/PassManager.h"
72#include "llvm/IR/Value.h"
76#include "llvm/Support/Debug.h"
84#include <algorithm>
85#include <cassert>
86#include <cstdint>
87#include <map>
88#include <optional>
89#include <utility>
90
91using namespace llvm;
92using namespace PatternMatch;
93
94#define DEBUG_TYPE "dse"
95
96STATISTIC(NumRemainingStores, "Number of stores remaining after DSE");
97STATISTIC(NumRedundantStores, "Number of redundant stores deleted");
98STATISTIC(NumFastStores, "Number of stores deleted");
99STATISTIC(NumFastOther, "Number of other instrs removed");
100STATISTIC(NumCompletePartials, "Number of stores dead by later partials");
101STATISTIC(NumModifiedStores, "Number of stores modified");
102STATISTIC(NumCFGChecks, "Number of stores modified");
103STATISTIC(NumCFGTries, "Number of stores modified");
104STATISTIC(NumCFGSuccess, "Number of stores modified");
105STATISTIC(NumGetDomMemoryDefPassed,
106 "Number of times a valid candidate is returned from getDomMemoryDef");
107STATISTIC(NumDomMemDefChecks,
108 "Number iterations check for reads in getDomMemoryDef");
109
110DEBUG_COUNTER(MemorySSACounter, "dse-memoryssa",
111 "Controls which MemoryDefs are eliminated.");
112
113static cl::opt<bool>
114EnablePartialOverwriteTracking("enable-dse-partial-overwrite-tracking",
115 cl::init(true), cl::Hidden,
116 cl::desc("Enable partial-overwrite tracking in DSE"));
117
118static cl::opt<bool>
119EnablePartialStoreMerging("enable-dse-partial-store-merging",
120 cl::init(true), cl::Hidden,
121 cl::desc("Enable partial store merging in DSE"));
122
124 MemorySSAScanLimit("dse-memoryssa-scanlimit", cl::init(150), cl::Hidden,
125 cl::desc("The number of memory instructions to scan for "
126 "dead store elimination (default = 150)"));
128 "dse-memoryssa-walklimit", cl::init(90), cl::Hidden,
129 cl::desc("The maximum number of steps while walking upwards to find "
130 "MemoryDefs that may be killed (default = 90)"));
131
133 "dse-memoryssa-partial-store-limit", cl::init(5), cl::Hidden,
134 cl::desc("The maximum number candidates that only partially overwrite the "
135 "killing MemoryDef to consider"
136 " (default = 5)"));
137
139 "dse-memoryssa-defs-per-block-limit", cl::init(5000), cl::Hidden,
140 cl::desc("The number of MemoryDefs we consider as candidates to eliminated "
141 "other stores per basic block (default = 5000)"));
142
144 "dse-memoryssa-samebb-cost", cl::init(1), cl::Hidden,
145 cl::desc(
146 "The cost of a step in the same basic block as the killing MemoryDef"
147 "(default = 1)"));
148
150 MemorySSAOtherBBStepCost("dse-memoryssa-otherbb-cost", cl::init(5),
152 cl::desc("The cost of a step in a different basic "
153 "block than the killing MemoryDef"
154 "(default = 5)"));
155
157 "dse-memoryssa-path-check-limit", cl::init(50), cl::Hidden,
158 cl::desc("The maximum number of blocks to check when trying to prove that "
159 "all paths to an exit go through a killing block (default = 50)"));
160
161// This flags allows or disallows DSE to optimize MemorySSA during its
162// traversal. Note that DSE optimizing MemorySSA may impact other passes
163// downstream of the DSE invocation and can lead to issues not being
164// reproducible in isolation (i.e. when MemorySSA is built from scratch). In
165// those cases, the flag can be used to check if DSE's MemorySSA optimizations
166// impact follow-up passes.
167static cl::opt<bool>
168 OptimizeMemorySSA("dse-optimize-memoryssa", cl::init(true), cl::Hidden,
169 cl::desc("Allow DSE to optimize memory accesses."));
170
171// TODO: remove this flag.
173 "enable-dse-initializes-attr-improvement", cl::init(true), cl::Hidden,
174 cl::desc("Enable the initializes attr improvement in DSE"));
175
176//===----------------------------------------------------------------------===//
177// Helper functions
178//===----------------------------------------------------------------------===//
179using OverlapIntervalsTy = std::map<int64_t, int64_t>;
181
182/// Returns true if the end of this instruction can be safely shortened in
183/// length.
185 // Don't shorten stores for now
186 if (isa<StoreInst>(I))
187 return false;
188
190 switch (II->getIntrinsicID()) {
191 default: return false;
192 case Intrinsic::memset:
193 case Intrinsic::memcpy:
194 case Intrinsic::memcpy_element_unordered_atomic:
195 case Intrinsic::memset_element_unordered_atomic:
196 // Do shorten memory intrinsics.
197 // FIXME: Add memmove if it's also safe to transform.
198 return true;
199 }
200 }
201
202 // Don't shorten libcalls calls for now.
203
204 return false;
205}
206
207/// Returns true if the beginning of this instruction can be safely shortened
208/// in length.
210 // FIXME: Handle only memset for now. Supporting memcpy/memmove should be
211 // easily done by offsetting the source address.
212 return isa<AnyMemSetInst>(I);
213}
214
215static std::optional<TypeSize> getPointerSize(const Value *V,
216 const DataLayout &DL,
217 const TargetLibraryInfo &TLI,
218 const Function *F) {
220 ObjectSizeOpts Opts;
222
223 if (getObjectSize(V, Size, DL, &TLI, Opts))
224 return TypeSize::getFixed(Size);
225 return std::nullopt;
226}
227
228namespace {
229
230enum OverwriteResult {
231 OW_Begin,
232 OW_Complete,
233 OW_End,
234 OW_PartialEarlierWithFullLater,
235 OW_MaybePartial,
236 OW_None,
237 OW_Unknown
238};
239
240} // end anonymous namespace
241
242/// Check if two instruction are masked stores that completely
243/// overwrite one another. More specifically, \p KillingI has to
244/// overwrite \p DeadI.
245static OverwriteResult isMaskedStoreOverwrite(const Instruction *KillingI,
246 const Instruction *DeadI,
248 const auto *KillingII = dyn_cast<IntrinsicInst>(KillingI);
249 const auto *DeadII = dyn_cast<IntrinsicInst>(DeadI);
250 if (KillingII == nullptr || DeadII == nullptr)
251 return OW_Unknown;
252 if (KillingII->getIntrinsicID() != DeadII->getIntrinsicID())
253 return OW_Unknown;
254
255 switch (KillingII->getIntrinsicID()) {
256 case Intrinsic::masked_store:
257 case Intrinsic::vp_store: {
258 const DataLayout &DL = KillingII->getDataLayout();
259 auto *KillingTy = KillingII->getArgOperand(0)->getType();
260 auto *DeadTy = DeadII->getArgOperand(0)->getType();
261 if (DL.getTypeSizeInBits(KillingTy) != DL.getTypeSizeInBits(DeadTy))
262 return OW_Unknown;
263 // Element count.
264 if (cast<VectorType>(KillingTy)->getElementCount() !=
265 cast<VectorType>(DeadTy)->getElementCount())
266 return OW_Unknown;
267 // Pointers.
268 Value *KillingPtr = KillingII->getArgOperand(1);
269 Value *DeadPtr = DeadII->getArgOperand(1);
270 if (KillingPtr != DeadPtr && !AA.isMustAlias(KillingPtr, DeadPtr))
271 return OW_Unknown;
272 if (KillingII->getIntrinsicID() == Intrinsic::masked_store) {
273 // Masks.
274 // TODO: check that KillingII's mask is a superset of the DeadII's mask.
275 if (KillingII->getArgOperand(2) != DeadII->getArgOperand(2))
276 return OW_Unknown;
277 } else if (KillingII->getIntrinsicID() == Intrinsic::vp_store) {
278 // Masks.
279 // TODO: check that KillingII's mask is a superset of the DeadII's mask.
280 if (KillingII->getArgOperand(2) != DeadII->getArgOperand(2))
281 return OW_Unknown;
282 // Lengths.
283 if (KillingII->getArgOperand(3) != DeadII->getArgOperand(3))
284 return OW_Unknown;
285 }
286 return OW_Complete;
287 }
288 default:
289 return OW_Unknown;
290 }
291}
292
293/// Return 'OW_Complete' if a store to the 'KillingLoc' location completely
294/// overwrites a store to the 'DeadLoc' location, 'OW_End' if the end of the
295/// 'DeadLoc' location is completely overwritten by 'KillingLoc', 'OW_Begin'
296/// if the beginning of the 'DeadLoc' location is overwritten by 'KillingLoc'.
297/// 'OW_PartialEarlierWithFullLater' means that a dead (big) store was
298/// overwritten by a killing (smaller) store which doesn't write outside the big
299/// store's memory locations. Returns 'OW_Unknown' if nothing can be determined.
300/// NOTE: This function must only be called if both \p KillingLoc and \p
301/// DeadLoc belong to the same underlying object with valid \p KillingOff and
302/// \p DeadOff.
303static OverwriteResult isPartialOverwrite(const MemoryLocation &KillingLoc,
304 const MemoryLocation &DeadLoc,
305 int64_t KillingOff, int64_t DeadOff,
306 Instruction *DeadI,
308 const uint64_t KillingSize = KillingLoc.Size.getValue();
309 const uint64_t DeadSize = DeadLoc.Size.getValue();
310 // We may now overlap, although the overlap is not complete. There might also
311 // be other incomplete overlaps, and together, they might cover the complete
312 // dead store.
313 // Note: The correctness of this logic depends on the fact that this function
314 // is not even called providing DepWrite when there are any intervening reads.
316 KillingOff < int64_t(DeadOff + DeadSize) &&
317 int64_t(KillingOff + KillingSize) >= DeadOff) {
318
319 // Insert our part of the overlap into the map.
320 auto &IM = IOL[DeadI];
321 LLVM_DEBUG(dbgs() << "DSE: Partial overwrite: DeadLoc [" << DeadOff << ", "
322 << int64_t(DeadOff + DeadSize) << ") KillingLoc ["
323 << KillingOff << ", " << int64_t(KillingOff + KillingSize)
324 << ")\n");
325
326 // Make sure that we only insert non-overlapping intervals and combine
327 // adjacent intervals. The intervals are stored in the map with the ending
328 // offset as the key (in the half-open sense) and the starting offset as
329 // the value.
330 int64_t KillingIntStart = KillingOff;
331 int64_t KillingIntEnd = KillingOff + KillingSize;
332
333 // Find any intervals ending at, or after, KillingIntStart which start
334 // before KillingIntEnd.
335 auto ILI = IM.lower_bound(KillingIntStart);
336 if (ILI != IM.end() && ILI->second <= KillingIntEnd) {
337 // This existing interval is overlapped with the current store somewhere
338 // in [KillingIntStart, KillingIntEnd]. Merge them by erasing the existing
339 // intervals and adjusting our start and end.
340 KillingIntStart = std::min(KillingIntStart, ILI->second);
341 KillingIntEnd = std::max(KillingIntEnd, ILI->first);
342 ILI = IM.erase(ILI);
343
344 // Continue erasing and adjusting our end in case other previous
345 // intervals are also overlapped with the current store.
346 //
347 // |--- dead 1 ---| |--- dead 2 ---|
348 // |------- killing---------|
349 //
350 while (ILI != IM.end() && ILI->second <= KillingIntEnd) {
351 assert(ILI->second > KillingIntStart && "Unexpected interval");
352 KillingIntEnd = std::max(KillingIntEnd, ILI->first);
353 ILI = IM.erase(ILI);
354 }
355 }
356
357 IM[KillingIntEnd] = KillingIntStart;
358
359 ILI = IM.begin();
360 if (ILI->second <= DeadOff && ILI->first >= int64_t(DeadOff + DeadSize)) {
361 LLVM_DEBUG(dbgs() << "DSE: Full overwrite from partials: DeadLoc ["
362 << DeadOff << ", " << int64_t(DeadOff + DeadSize)
363 << ") Composite KillingLoc [" << ILI->second << ", "
364 << ILI->first << ")\n");
365 ++NumCompletePartials;
366 return OW_Complete;
367 }
368 }
369
370 // Check for a dead store which writes to all the memory locations that
371 // the killing store writes to.
372 if (EnablePartialStoreMerging && KillingOff >= DeadOff &&
373 int64_t(DeadOff + DeadSize) > KillingOff &&
374 uint64_t(KillingOff - DeadOff) + KillingSize <= DeadSize) {
375 LLVM_DEBUG(dbgs() << "DSE: Partial overwrite a dead load [" << DeadOff
376 << ", " << int64_t(DeadOff + DeadSize)
377 << ") by a killing store [" << KillingOff << ", "
378 << int64_t(KillingOff + KillingSize) << ")\n");
379 // TODO: Maybe come up with a better name?
380 return OW_PartialEarlierWithFullLater;
381 }
382
383 // Another interesting case is if the killing store overwrites the end of the
384 // dead store.
385 //
386 // |--dead--|
387 // |-- killing --|
388 //
389 // In this case we may want to trim the size of dead store to avoid
390 // generating stores to addresses which will definitely be overwritten killing
391 // store.
393 (KillingOff > DeadOff && KillingOff < int64_t(DeadOff + DeadSize) &&
394 int64_t(KillingOff + KillingSize) >= int64_t(DeadOff + DeadSize)))
395 return OW_End;
396
397 // Finally, we also need to check if the killing store overwrites the
398 // beginning of the dead store.
399 //
400 // |--dead--|
401 // |-- killing --|
402 //
403 // In this case we may want to move the destination address and trim the size
404 // of dead store to avoid generating stores to addresses which will definitely
405 // be overwritten killing store.
407 (KillingOff <= DeadOff && int64_t(KillingOff + KillingSize) > DeadOff)) {
408 assert(int64_t(KillingOff + KillingSize) < int64_t(DeadOff + DeadSize) &&
409 "Expect to be handled as OW_Complete");
410 return OW_Begin;
411 }
412 // Otherwise, they don't completely overlap.
413 return OW_Unknown;
414}
415
416/// Returns true if the memory which is accessed by the second instruction is not
417/// modified between the first and the second instruction.
418/// Precondition: Second instruction must be dominated by the first
419/// instruction.
420static bool
423 DominatorTree *DT) {
424 // Do a backwards scan through the CFG from SecondI to FirstI. Look for
425 // instructions which can modify the memory location accessed by SecondI.
426 //
427 // While doing the walk keep track of the address to check. It might be
428 // different in different basic blocks due to PHI translation.
429 using BlockAddressPair = std::pair<BasicBlock *, PHITransAddr>;
431 // Keep track of the address we visited each block with. Bail out if we
432 // visit a block with different addresses.
434
435 BasicBlock::iterator FirstBBI(FirstI);
436 ++FirstBBI;
437 BasicBlock::iterator SecondBBI(SecondI);
438 BasicBlock *FirstBB = FirstI->getParent();
439 BasicBlock *SecondBB = SecondI->getParent();
440 MemoryLocation MemLoc;
441 if (auto *MemSet = dyn_cast<MemSetInst>(SecondI))
442 MemLoc = MemoryLocation::getForDest(MemSet);
443 else
444 MemLoc = MemoryLocation::get(SecondI);
445
446 auto *MemLocPtr = const_cast<Value *>(MemLoc.Ptr);
447
448 // Start checking the SecondBB.
449 WorkList.push_back(
450 std::make_pair(SecondBB, PHITransAddr(MemLocPtr, DL, nullptr)));
451 bool isFirstBlock = true;
452
453 // Check all blocks going backward until we reach the FirstBB.
454 while (!WorkList.empty()) {
455 BlockAddressPair Current = WorkList.pop_back_val();
456 BasicBlock *B = Current.first;
457 PHITransAddr &Addr = Current.second;
458 Value *Ptr = Addr.getAddr();
459
460 // Ignore instructions before FirstI if this is the FirstBB.
461 BasicBlock::iterator BI = (B == FirstBB ? FirstBBI : B->begin());
462
464 if (isFirstBlock) {
465 // Ignore instructions after SecondI if this is the first visit of SecondBB.
466 assert(B == SecondBB && "first block is not the store block");
467 EI = SecondBBI;
468 isFirstBlock = false;
469 } else {
470 // It's not SecondBB or (in case of a loop) the second visit of SecondBB.
471 // In this case we also have to look at instructions after SecondI.
472 EI = B->end();
473 }
474 for (; BI != EI; ++BI) {
475 Instruction *I = &*BI;
476 if (I->mayWriteToMemory() && I != SecondI)
477 if (isModSet(AA.getModRefInfo(I, MemLoc.getWithNewPtr(Ptr))))
478 return false;
479 }
480 if (B != FirstBB) {
481 assert(B != &FirstBB->getParent()->getEntryBlock() &&
482 "Should not hit the entry block because SI must be dominated by LI");
483 for (BasicBlock *Pred : predecessors(B)) {
484 PHITransAddr PredAddr = Addr;
485 if (PredAddr.needsPHITranslationFromBlock(B)) {
486 if (!PredAddr.isPotentiallyPHITranslatable())
487 return false;
488 if (!PredAddr.translateValue(B, Pred, DT, false))
489 return false;
490 }
491 Value *TranslatedPtr = PredAddr.getAddr();
492 auto Inserted = Visited.insert(std::make_pair(Pred, TranslatedPtr));
493 if (!Inserted.second) {
494 // We already visited this block before. If it was with a different
495 // address - bail out!
496 if (TranslatedPtr != Inserted.first->second)
497 return false;
498 // ... otherwise just skip it.
499 continue;
500 }
501 WorkList.push_back(std::make_pair(Pred, PredAddr));
502 }
503 }
504 }
505 return true;
506}
507
508static void shortenAssignment(Instruction *Inst, Value *OriginalDest,
509 uint64_t OldOffsetInBits, uint64_t OldSizeInBits,
510 uint64_t NewSizeInBits, bool IsOverwriteEnd) {
511 const DataLayout &DL = Inst->getDataLayout();
512 uint64_t DeadSliceSizeInBits = OldSizeInBits - NewSizeInBits;
513 uint64_t DeadSliceOffsetInBits =
514 OldOffsetInBits + (IsOverwriteEnd ? NewSizeInBits : 0);
515 auto SetDeadFragExpr = [](auto *Assign,
516 DIExpression::FragmentInfo DeadFragment) {
517 // createFragmentExpression expects an offset relative to the existing
518 // fragment offset if there is one.
519 uint64_t RelativeOffset = DeadFragment.OffsetInBits -
520 Assign->getExpression()
521 ->getFragmentInfo()
522 .value_or(DIExpression::FragmentInfo(0, 0))
523 .OffsetInBits;
525 Assign->getExpression(), RelativeOffset, DeadFragment.SizeInBits)) {
526 Assign->setExpression(*NewExpr);
527 return;
528 }
529 // Failed to create a fragment expression for this so discard the value,
530 // making this a kill location.
532 DIExpression::get(Assign->getContext(), {}), DeadFragment.OffsetInBits,
533 DeadFragment.SizeInBits);
534 Assign->setExpression(Expr);
535 Assign->setKillLocation();
536 };
537
538 // A DIAssignID to use so that the inserted dbg.assign intrinsics do not
539 // link to any instructions. Created in the loop below (once).
540 DIAssignID *LinkToNothing = nullptr;
541 LLVMContext &Ctx = Inst->getContext();
542 auto GetDeadLink = [&Ctx, &LinkToNothing]() {
543 if (!LinkToNothing)
544 LinkToNothing = DIAssignID::getDistinct(Ctx);
545 return LinkToNothing;
546 };
547
548 // Insert an unlinked dbg.assign intrinsic for the dead fragment after each
549 // overlapping dbg.assign intrinsic.
550 for (DbgVariableRecord *Assign : at::getDVRAssignmentMarkers(Inst)) {
551 std::optional<DIExpression::FragmentInfo> NewFragment;
552 if (!at::calculateFragmentIntersect(DL, OriginalDest, DeadSliceOffsetInBits,
553 DeadSliceSizeInBits, Assign,
554 NewFragment) ||
555 !NewFragment) {
556 // We couldn't calculate the intersecting fragment for some reason. Be
557 // cautious and unlink the whole assignment from the store.
558 Assign->setKillAddress();
559 Assign->setAssignId(GetDeadLink());
560 continue;
561 }
562 // No intersect.
563 if (NewFragment->SizeInBits == 0)
564 continue;
565
566 // Fragments overlap: insert a new dbg.assign for this dead part.
567 auto *NewAssign = static_cast<decltype(Assign)>(Assign->clone());
568 NewAssign->insertAfter(Assign->getIterator());
569 NewAssign->setAssignId(GetDeadLink());
570 if (NewFragment)
571 SetDeadFragExpr(NewAssign, *NewFragment);
572 NewAssign->setKillAddress();
573 }
574}
575
576/// Update the attributes given that a memory access is updated (the
577/// dereferenced pointer could be moved forward when shortening a
578/// mem intrinsic).
579static void adjustArgAttributes(AnyMemIntrinsic *Intrinsic, unsigned ArgNo,
580 uint64_t PtrOffset) {
581 // Remember old attributes.
582 AttributeSet OldAttrs = Intrinsic->getParamAttributes(ArgNo);
583
584 // Find attributes that should be kept, and remove the rest.
585 AttributeMask AttrsToRemove;
586 for (auto &Attr : OldAttrs) {
587 if (Attr.hasKindAsEnum()) {
588 switch (Attr.getKindAsEnum()) {
589 default:
590 break;
591 case Attribute::Alignment:
592 // Only keep alignment if PtrOffset satisfy the alignment.
593 if (isAligned(Attr.getAlignment().valueOrOne(), PtrOffset))
594 continue;
595 break;
596 case Attribute::Dereferenceable:
597 case Attribute::DereferenceableOrNull:
598 // We could reduce the size of these attributes according to
599 // PtrOffset. But we simply drop these for now.
600 break;
601 case Attribute::NonNull:
602 case Attribute::NoUndef:
603 continue;
604 }
605 }
606 AttrsToRemove.addAttribute(Attr);
607 }
608
609 // Remove the attributes that should be dropped.
610 Intrinsic->removeParamAttrs(ArgNo, AttrsToRemove);
611}
612
613static bool tryToShorten(Instruction *DeadI, int64_t &DeadStart,
614 uint64_t &DeadSize, int64_t KillingStart,
615 uint64_t KillingSize, bool IsOverwriteEnd) {
616 auto *DeadIntrinsic = cast<AnyMemIntrinsic>(DeadI);
617 Align PrefAlign = DeadIntrinsic->getDestAlign().valueOrOne();
618
619 // We assume that memet/memcpy operates in chunks of the "largest" native
620 // type size and aligned on the same value. That means optimal start and size
621 // of memset/memcpy should be modulo of preferred alignment of that type. That
622 // is it there is no any sense in trying to reduce store size any further
623 // since any "extra" stores comes for free anyway.
624 // On the other hand, maximum alignment we can achieve is limited by alignment
625 // of initial store.
626
627 // TODO: Limit maximum alignment by preferred (or abi?) alignment of the
628 // "largest" native type.
629 // Note: What is the proper way to get that value?
630 // Should TargetTransformInfo::getRegisterBitWidth be used or anything else?
631 // PrefAlign = std::min(DL.getPrefTypeAlign(LargestType), PrefAlign);
632
633 int64_t ToRemoveStart = 0;
634 uint64_t ToRemoveSize = 0;
635 // Compute start and size of the region to remove. Make sure 'PrefAlign' is
636 // maintained on the remaining store.
637 if (IsOverwriteEnd) {
638 // Calculate required adjustment for 'KillingStart' in order to keep
639 // remaining store size aligned on 'PerfAlign'.
640 uint64_t Off =
641 offsetToAlignment(uint64_t(KillingStart - DeadStart), PrefAlign);
642 ToRemoveStart = KillingStart + Off;
643 if (DeadSize <= uint64_t(ToRemoveStart - DeadStart))
644 return false;
645 ToRemoveSize = DeadSize - uint64_t(ToRemoveStart - DeadStart);
646 } else {
647 ToRemoveStart = DeadStart;
648 assert(KillingSize >= uint64_t(DeadStart - KillingStart) &&
649 "Not overlapping accesses?");
650 ToRemoveSize = KillingSize - uint64_t(DeadStart - KillingStart);
651 // Calculate required adjustment for 'ToRemoveSize'in order to keep
652 // start of the remaining store aligned on 'PerfAlign'.
653 uint64_t Off = offsetToAlignment(ToRemoveSize, PrefAlign);
654 if (Off != 0) {
655 if (ToRemoveSize <= (PrefAlign.value() - Off))
656 return false;
657 ToRemoveSize -= PrefAlign.value() - Off;
658 }
659 assert(isAligned(PrefAlign, ToRemoveSize) &&
660 "Should preserve selected alignment");
661 }
662
663 assert(ToRemoveSize > 0 && "Shouldn't reach here if nothing to remove");
664 assert(DeadSize > ToRemoveSize && "Can't remove more than original size");
665
666 uint64_t NewSize = DeadSize - ToRemoveSize;
667 if (DeadIntrinsic->isAtomic()) {
668 // When shortening an atomic memory intrinsic, the newly shortened
669 // length must remain an integer multiple of the element size.
670 const uint32_t ElementSize = DeadIntrinsic->getElementSizeInBytes();
671 if (0 != NewSize % ElementSize)
672 return false;
673 }
674
675 LLVM_DEBUG(dbgs() << "DSE: Remove Dead Store:\n OW "
676 << (IsOverwriteEnd ? "END" : "BEGIN") << ": " << *DeadI
677 << "\n KILLER [" << ToRemoveStart << ", "
678 << int64_t(ToRemoveStart + ToRemoveSize) << ")\n");
679
680 DeadIntrinsic->setLength(NewSize);
681 DeadIntrinsic->setDestAlignment(PrefAlign);
682
683 Value *OrigDest = DeadIntrinsic->getRawDest();
684 if (!IsOverwriteEnd) {
685 Value *Indices[1] = {
686 ConstantInt::get(DeadIntrinsic->getLength()->getType(), ToRemoveSize)};
688 Type::getInt8Ty(DeadIntrinsic->getContext()), OrigDest, Indices, "",
689 DeadI->getIterator());
690 NewDestGEP->setDebugLoc(DeadIntrinsic->getDebugLoc());
691 DeadIntrinsic->setDest(NewDestGEP);
692 adjustArgAttributes(DeadIntrinsic, 0, ToRemoveSize);
693 }
694
695 // Update attached dbg.assign intrinsics. Assume 8-bit byte.
696 shortenAssignment(DeadI, OrigDest, DeadStart * 8, DeadSize * 8, NewSize * 8,
697 IsOverwriteEnd);
698
699 // Finally update start and size of dead access.
700 if (!IsOverwriteEnd)
701 DeadStart += ToRemoveSize;
702 DeadSize = NewSize;
703
704 return true;
705}
706
708 int64_t &DeadStart, uint64_t &DeadSize) {
709 if (IntervalMap.empty() || !isShortenableAtTheEnd(DeadI))
710 return false;
711
712 OverlapIntervalsTy::iterator OII = --IntervalMap.end();
713 int64_t KillingStart = OII->second;
714 uint64_t KillingSize = OII->first - KillingStart;
715
716 assert(OII->first - KillingStart >= 0 && "Size expected to be positive");
717
718 if (KillingStart > DeadStart &&
719 // Note: "KillingStart - KillingStart" is known to be positive due to
720 // preceding check.
721 (uint64_t)(KillingStart - DeadStart) < DeadSize &&
722 // Note: "DeadSize - (uint64_t)(KillingStart - DeadStart)" is known to
723 // be non negative due to preceding checks.
724 KillingSize >= DeadSize - (uint64_t)(KillingStart - DeadStart)) {
725 if (tryToShorten(DeadI, DeadStart, DeadSize, KillingStart, KillingSize,
726 true)) {
727 IntervalMap.erase(OII);
728 return true;
729 }
730 }
731 return false;
732}
733
736 int64_t &DeadStart, uint64_t &DeadSize) {
738 return false;
739
740 OverlapIntervalsTy::iterator OII = IntervalMap.begin();
741 int64_t KillingStart = OII->second;
742 uint64_t KillingSize = OII->first - KillingStart;
743
744 assert(OII->first - KillingStart >= 0 && "Size expected to be positive");
745
746 if (KillingStart <= DeadStart &&
747 // Note: "DeadStart - KillingStart" is known to be non negative due to
748 // preceding check.
749 KillingSize > (uint64_t)(DeadStart - KillingStart)) {
750 // Note: "KillingSize - (uint64_t)(DeadStart - DeadStart)" is known to
751 // be positive due to preceding checks.
752 assert(KillingSize - (uint64_t)(DeadStart - KillingStart) < DeadSize &&
753 "Should have been handled as OW_Complete");
754 if (tryToShorten(DeadI, DeadStart, DeadSize, KillingStart, KillingSize,
755 false)) {
756 IntervalMap.erase(OII);
757 return true;
758 }
759 }
760 return false;
761}
762
763static Constant *
765 int64_t KillingOffset, int64_t DeadOffset,
767 DominatorTree *DT) {
768
769 if (DeadI && isa<ConstantInt>(DeadI->getValueOperand()) &&
770 DL.typeSizeEqualsStoreSize(DeadI->getValueOperand()->getType()) &&
771 KillingI && isa<ConstantInt>(KillingI->getValueOperand()) &&
772 DL.typeSizeEqualsStoreSize(KillingI->getValueOperand()->getType()) &&
773 memoryIsNotModifiedBetween(DeadI, KillingI, AA, DL, DT)) {
774 // If the store we find is:
775 // a) partially overwritten by the store to 'Loc'
776 // b) the killing store is fully contained in the dead one and
777 // c) they both have a constant value
778 // d) none of the two stores need padding
779 // Merge the two stores, replacing the dead store's value with a
780 // merge of both values.
781 // TODO: Deal with other constant types (vectors, etc), and probably
782 // some mem intrinsics (if needed)
783
784 APInt DeadValue = cast<ConstantInt>(DeadI->getValueOperand())->getValue();
785 APInt KillingValue =
786 cast<ConstantInt>(KillingI->getValueOperand())->getValue();
787 unsigned KillingBits = KillingValue.getBitWidth();
788 assert(DeadValue.getBitWidth() > KillingValue.getBitWidth());
789 KillingValue = KillingValue.zext(DeadValue.getBitWidth());
790
791 // Offset of the smaller store inside the larger store
792 unsigned BitOffsetDiff = (KillingOffset - DeadOffset) * 8;
793 unsigned LShiftAmount =
794 DL.isBigEndian() ? DeadValue.getBitWidth() - BitOffsetDiff - KillingBits
795 : BitOffsetDiff;
796 APInt Mask = APInt::getBitsSet(DeadValue.getBitWidth(), LShiftAmount,
797 LShiftAmount + KillingBits);
798 // Clear the bits we'll be replacing, then OR with the smaller
799 // store, shifted appropriately.
800 APInt Merged = (DeadValue & ~Mask) | (KillingValue << LShiftAmount);
801 LLVM_DEBUG(dbgs() << "DSE: Merge Stores:\n Dead: " << *DeadI
802 << "\n Killing: " << *KillingI
803 << "\n Merged Value: " << Merged << '\n');
804 return ConstantInt::get(DeadI->getValueOperand()->getType(), Merged);
805 }
806 return nullptr;
807}
808
809// Returns true if \p I is an intrinsic that does not read or write memory.
812 switch (II->getIntrinsicID()) {
813 case Intrinsic::lifetime_start:
814 case Intrinsic::lifetime_end:
815 case Intrinsic::invariant_end:
816 case Intrinsic::launder_invariant_group:
817 case Intrinsic::assume:
818 return true;
819 case Intrinsic::dbg_declare:
820 case Intrinsic::dbg_label:
821 case Intrinsic::dbg_value:
822 llvm_unreachable("Intrinsic should not be modeled in MemorySSA");
823 default:
824 return false;
825 }
826 }
827 return false;
828}
829
830// Check if we can ignore \p D for DSE.
831static bool canSkipDef(MemoryDef *D, bool DefVisibleToCaller) {
832 Instruction *DI = D->getMemoryInst();
833 // Calls that only access inaccessible memory cannot read or write any memory
834 // locations we consider for elimination.
835 if (auto *CB = dyn_cast<CallBase>(DI))
836 if (CB->onlyAccessesInaccessibleMemory())
837 return true;
838
839 // We can eliminate stores to locations not visible to the caller across
840 // throwing instructions.
841 if (DI->mayThrow() && !DefVisibleToCaller)
842 return true;
843
844 // We can remove the dead stores, irrespective of the fence and its ordering
845 // (release/acquire/seq_cst). Fences only constraints the ordering of
846 // already visible stores, it does not make a store visible to other
847 // threads. So, skipping over a fence does not change a store from being
848 // dead.
849 if (isa<FenceInst>(DI))
850 return true;
851
852 // Skip intrinsics that do not really read or modify memory.
853 if (isNoopIntrinsic(DI))
854 return true;
855
856 return false;
857}
858
859namespace {
860
861// A memory location wrapper that represents a MemoryLocation, `MemLoc`,
862// defined by `MemDef`.
863struct MemoryLocationWrapper {
864 MemoryLocationWrapper(MemoryLocation MemLoc, MemoryDef *MemDef,
865 bool DefByInitializesAttr)
866 : MemLoc(MemLoc), MemDef(MemDef),
867 DefByInitializesAttr(DefByInitializesAttr) {
868 assert(MemLoc.Ptr && "MemLoc should be not null");
869 UnderlyingObject = getUnderlyingObject(MemLoc.Ptr);
870 DefInst = MemDef->getMemoryInst();
871 }
872
873 MemoryLocation MemLoc;
874 const Value *UnderlyingObject;
875 MemoryDef *MemDef;
876 Instruction *DefInst;
877 bool DefByInitializesAttr = false;
878};
879
880// A memory def wrapper that represents a MemoryDef and the MemoryLocation(s)
881// defined by this MemoryDef.
882struct MemoryDefWrapper {
883 MemoryDefWrapper(MemoryDef *MemDef,
884 ArrayRef<std::pair<MemoryLocation, bool>> MemLocations) {
885 DefInst = MemDef->getMemoryInst();
886 for (auto &[MemLoc, DefByInitializesAttr] : MemLocations)
887 DefinedLocations.push_back(
888 MemoryLocationWrapper(MemLoc, MemDef, DefByInitializesAttr));
889 }
890 Instruction *DefInst;
892};
893
894struct ArgumentInitInfo {
895 unsigned Idx;
896 bool IsDeadOrInvisibleOnUnwind;
897 ConstantRangeList Inits;
898};
899} // namespace
900
903 return CB && CB->getArgOperandWithAttribute(Attribute::Initializes);
904}
905
906// Return the intersected range list of the initializes attributes of "Args".
907// "Args" are call arguments that alias to each other.
908// If any argument in "Args" doesn't have dead_on_unwind attr and
909// "CallHasNoUnwindAttr" is false, return empty.
912 bool CallHasNoUnwindAttr) {
913 if (Args.empty())
914 return {};
915
916 // To address unwind, the function should have nounwind attribute or the
917 // arguments have dead or invisible on unwind. Otherwise, return empty.
918 for (const auto &Arg : Args) {
919 if (!CallHasNoUnwindAttr && !Arg.IsDeadOrInvisibleOnUnwind)
920 return {};
921 if (Arg.Inits.empty())
922 return {};
923 }
924
925 ConstantRangeList IntersectedIntervals = Args.front().Inits;
926 for (auto &Arg : Args.drop_front())
927 IntersectedIntervals = IntersectedIntervals.intersectWith(Arg.Inits);
928
929 return IntersectedIntervals;
930}
931
932namespace {
933
934struct DSEState {
935 Function &F;
936 AliasAnalysis &AA;
937 EarliestEscapeAnalysis EA;
938
939 /// The single BatchAA instance that is used to cache AA queries. It will
940 /// not be invalidated over the whole run. This is safe, because:
941 /// 1. Only memory writes are removed, so the alias cache for memory
942 /// locations remains valid.
943 /// 2. No new instructions are added (only instructions removed), so cached
944 /// information for a deleted value cannot be accessed by a re-used new
945 /// value pointer.
946 BatchAAResults BatchAA;
947
948 MemorySSA &MSSA;
949 DominatorTree &DT;
950 PostDominatorTree &PDT;
951 const TargetLibraryInfo &TLI;
952 const DataLayout &DL;
953 const LoopInfo &LI;
954
955 // Whether the function contains any irreducible control flow, useful for
956 // being accurately able to detect loops.
957 bool ContainsIrreducibleLoops;
958
959 // All MemoryDefs that potentially could kill other MemDefs.
961 // Any that should be skipped as they are already deleted
962 SmallPtrSet<MemoryAccess *, 4> SkipStores;
963 // Keep track whether a given object is captured before return or not.
964 DenseMap<const Value *, bool> CapturedBeforeReturn;
965 // Keep track of all of the objects that are invisible to the caller after
966 // the function returns.
967 DenseMap<const Value *, bool> InvisibleToCallerAfterRet;
968 // Keep track of blocks with throwing instructions not modeled in MemorySSA.
969 SmallPtrSet<BasicBlock *, 16> ThrowingBlocks;
970 // Post-order numbers for each basic block. Used to figure out if memory
971 // accesses are executed before another access.
972 DenseMap<BasicBlock *, unsigned> PostOrderNumbers;
973
974 /// Keep track of instructions (partly) overlapping with killing MemoryDefs per
975 /// basic block.
976 MapVector<BasicBlock *, InstOverlapIntervalsTy> IOLs;
977 // Check if there are root nodes that are terminated by UnreachableInst.
978 // Those roots pessimize post-dominance queries. If there are such roots,
979 // fall back to CFG scan starting from all non-unreachable roots.
980 bool AnyUnreachableExit;
981
982 // Whether or not we should iterate on removing dead stores at the end of the
983 // function due to removing a store causing a previously captured pointer to
984 // no longer be captured.
985 bool ShouldIterateEndOfFunctionDSE;
986
987 /// Dead instructions to be removed at the end of DSE.
989
990 // Class contains self-reference, make sure it's not copied/moved.
991 DSEState(const DSEState &) = delete;
992 DSEState &operator=(const DSEState &) = delete;
993
994 DSEState(Function &F, AliasAnalysis &AA, MemorySSA &MSSA, DominatorTree &DT,
995 PostDominatorTree &PDT, const TargetLibraryInfo &TLI,
996 const LoopInfo &LI)
997 : F(F), AA(AA), EA(DT, &LI), BatchAA(AA, &EA), MSSA(MSSA), DT(DT),
998 PDT(PDT), TLI(TLI), DL(F.getDataLayout()), LI(LI) {
999 // Collect blocks with throwing instructions not modeled in MemorySSA and
1000 // alloc-like objects.
1001 unsigned PO = 0;
1002 for (BasicBlock *BB : post_order(&F)) {
1003 PostOrderNumbers[BB] = PO++;
1004 for (Instruction &I : *BB) {
1005 MemoryAccess *MA = MSSA.getMemoryAccess(&I);
1006 if (I.mayThrow() && !MA)
1007 ThrowingBlocks.insert(I.getParent());
1008
1009 auto *MD = dyn_cast_or_null<MemoryDef>(MA);
1010 if (MD && MemDefs.size() < MemorySSADefsPerBlockLimit &&
1011 (getLocForWrite(&I) || isMemTerminatorInst(&I) ||
1013 MemDefs.push_back(MD);
1014 }
1015 }
1016
1017 // Treat byval, inalloca or dead on return arguments the same as Allocas,
1018 // stores to them are dead at the end of the function.
1019 for (Argument &AI : F.args())
1020 if (AI.hasPassPointeeByValueCopyAttr() ||
1021 (AI.getType()->isPointerTy() &&
1022 AI.getDeadOnReturnInfo().coversAllReachableMemory()))
1023 InvisibleToCallerAfterRet.insert({&AI, true});
1024
1025 // Collect whether there is any irreducible control flow in the function.
1026 ContainsIrreducibleLoops = mayContainIrreducibleControl(F, &LI);
1027
1028 AnyUnreachableExit = any_of(PDT.roots(), [](const BasicBlock *E) {
1029 return isa<UnreachableInst>(E->getTerminator());
1030 });
1031 }
1032
1033 static void pushMemUses(MemoryAccess *Acc,
1034 SmallVectorImpl<MemoryAccess *> &WorkList,
1035 SmallPtrSetImpl<MemoryAccess *> &Visited) {
1036 for (Use &U : Acc->uses()) {
1037 auto *MA = cast<MemoryAccess>(U.getUser());
1038 if (Visited.insert(MA).second)
1039 WorkList.push_back(MA);
1040 }
1041 };
1042
1043 LocationSize strengthenLocationSize(const Instruction *I,
1044 LocationSize Size) const {
1045 if (auto *CB = dyn_cast<CallBase>(I)) {
1046 LibFunc F;
1047 if (TLI.getLibFunc(*CB, F) && TLI.has(F) &&
1048 (F == LibFunc_memset_chk || F == LibFunc_memcpy_chk)) {
1049 // Use the precise location size specified by the 3rd argument
1050 // for determining KillingI overwrites DeadLoc if it is a memset_chk
1051 // instruction. memset_chk will write either the amount specified as 3rd
1052 // argument or the function will immediately abort and exit the program.
1053 // NOTE: AA may determine NoAlias if it can prove that the access size
1054 // is larger than the allocation size due to that being UB. To avoid
1055 // returning potentially invalid NoAlias results by AA, limit the use of
1056 // the precise location size to isOverwrite.
1057 if (const auto *Len = dyn_cast<ConstantInt>(CB->getArgOperand(2)))
1058 return LocationSize::precise(Len->getZExtValue());
1059 }
1060 }
1061 return Size;
1062 }
1063
1064 /// Return 'OW_Complete' if a store to the 'KillingLoc' location (by \p
1065 /// KillingI instruction) completely overwrites a store to the 'DeadLoc'
1066 /// location (by \p DeadI instruction).
1067 /// Return OW_MaybePartial if \p KillingI does not completely overwrite
1068 /// \p DeadI, but they both write to the same underlying object. In that
1069 /// case, use isPartialOverwrite to check if \p KillingI partially overwrites
1070 /// \p DeadI. Returns 'OR_None' if \p KillingI is known to not overwrite the
1071 /// \p DeadI. Returns 'OW_Unknown' if nothing can be determined.
1072 OverwriteResult isOverwrite(const Instruction *KillingI,
1073 const Instruction *DeadI,
1074 const MemoryLocation &KillingLoc,
1075 const MemoryLocation &DeadLoc,
1076 int64_t &KillingOff, int64_t &DeadOff) {
1077 // AliasAnalysis does not always account for loops. Limit overwrite checks
1078 // to dependencies for which we can guarantee they are independent of any
1079 // loops they are in.
1080 if (!isGuaranteedLoopIndependent(DeadI, KillingI, DeadLoc))
1081 return OW_Unknown;
1082
1083 LocationSize KillingLocSize =
1084 strengthenLocationSize(KillingI, KillingLoc.Size);
1085 const Value *DeadPtr = DeadLoc.Ptr->stripPointerCasts();
1086 const Value *KillingPtr = KillingLoc.Ptr->stripPointerCasts();
1087 const Value *DeadUndObj = getUnderlyingObject(DeadPtr);
1088 const Value *KillingUndObj = getUnderlyingObject(KillingPtr);
1089
1090 // Check whether the killing store overwrites the whole object, in which
1091 // case the size/offset of the dead store does not matter.
1092 if (DeadUndObj == KillingUndObj && KillingLocSize.isPrecise() &&
1093 isIdentifiedObject(KillingUndObj)) {
1094 std::optional<TypeSize> KillingUndObjSize =
1095 getPointerSize(KillingUndObj, DL, TLI, &F);
1096 if (KillingUndObjSize && *KillingUndObjSize == KillingLocSize.getValue())
1097 return OW_Complete;
1098 }
1099
1100 // FIXME: Vet that this works for size upper-bounds. Seems unlikely that we'll
1101 // get imprecise values here, though (except for unknown sizes).
1102 if (!KillingLocSize.isPrecise() || !DeadLoc.Size.isPrecise()) {
1103 // In case no constant size is known, try to an IR values for the number
1104 // of bytes written and check if they match.
1105 const auto *KillingMemI = dyn_cast<MemIntrinsic>(KillingI);
1106 const auto *DeadMemI = dyn_cast<MemIntrinsic>(DeadI);
1107 if (KillingMemI && DeadMemI) {
1108 const Value *KillingV = KillingMemI->getLength();
1109 const Value *DeadV = DeadMemI->getLength();
1110 if (KillingV == DeadV && BatchAA.isMustAlias(DeadLoc, KillingLoc))
1111 return OW_Complete;
1112 }
1113
1114 // Masked stores have imprecise locations, but we can reason about them
1115 // to some extent.
1116 return isMaskedStoreOverwrite(KillingI, DeadI, BatchAA);
1117 }
1118
1119 const TypeSize KillingSize = KillingLocSize.getValue();
1120 const TypeSize DeadSize = DeadLoc.Size.getValue();
1121 // Bail on doing Size comparison which depends on AA for now
1122 // TODO: Remove AnyScalable once Alias Analysis deal with scalable vectors
1123 const bool AnyScalable =
1124 DeadSize.isScalable() || KillingLocSize.isScalable();
1125
1126 if (AnyScalable)
1127 return OW_Unknown;
1128 // Query the alias information
1129 AliasResult AAR = BatchAA.alias(KillingLoc, DeadLoc);
1130
1131 // If the start pointers are the same, we just have to compare sizes to see if
1132 // the killing store was larger than the dead store.
1133 if (AAR == AliasResult::MustAlias) {
1134 // Make sure that the KillingSize size is >= the DeadSize size.
1135 if (KillingSize >= DeadSize)
1136 return OW_Complete;
1137 }
1138
1139 // If we hit a partial alias we may have a full overwrite
1140 if (AAR == AliasResult::PartialAlias && AAR.hasOffset()) {
1141 int32_t Off = AAR.getOffset();
1142 if (Off >= 0 && (uint64_t)Off + DeadSize <= KillingSize)
1143 return OW_Complete;
1144 }
1145
1146 // If we can't resolve the same pointers to the same object, then we can't
1147 // analyze them at all.
1148 if (DeadUndObj != KillingUndObj) {
1149 // Non aliasing stores to different objects don't overlap. Note that
1150 // if the killing store is known to overwrite whole object (out of
1151 // bounds access overwrites whole object as well) then it is assumed to
1152 // completely overwrite any store to the same object even if they don't
1153 // actually alias (see next check).
1154 if (AAR == AliasResult::NoAlias)
1155 return OW_None;
1156 return OW_Unknown;
1157 }
1158
1159 // Okay, we have stores to two completely different pointers. Try to
1160 // decompose the pointer into a "base + constant_offset" form. If the base
1161 // pointers are equal, then we can reason about the two stores.
1162 DeadOff = 0;
1163 KillingOff = 0;
1164 const Value *DeadBasePtr =
1165 GetPointerBaseWithConstantOffset(DeadPtr, DeadOff, DL);
1166 const Value *KillingBasePtr =
1167 GetPointerBaseWithConstantOffset(KillingPtr, KillingOff, DL);
1168
1169 // If the base pointers still differ, we have two completely different
1170 // stores.
1171 if (DeadBasePtr != KillingBasePtr)
1172 return OW_Unknown;
1173
1174 // The killing access completely overlaps the dead store if and only if
1175 // both start and end of the dead one is "inside" the killing one:
1176 // |<->|--dead--|<->|
1177 // |-----killing------|
1178 // Accesses may overlap if and only if start of one of them is "inside"
1179 // another one:
1180 // |<->|--dead--|<-------->|
1181 // |-------killing--------|
1182 // OR
1183 // |-------dead-------|
1184 // |<->|---killing---|<----->|
1185 //
1186 // We have to be careful here as *Off is signed while *.Size is unsigned.
1187
1188 // Check if the dead access starts "not before" the killing one.
1189 if (DeadOff >= KillingOff) {
1190 // If the dead access ends "not after" the killing access then the
1191 // dead one is completely overwritten by the killing one.
1192 if (uint64_t(DeadOff - KillingOff) + DeadSize <= KillingSize)
1193 return OW_Complete;
1194 // If start of the dead access is "before" end of the killing access
1195 // then accesses overlap.
1196 else if ((uint64_t)(DeadOff - KillingOff) < KillingSize)
1197 return OW_MaybePartial;
1198 }
1199 // If start of the killing access is "before" end of the dead access then
1200 // accesses overlap.
1201 else if ((uint64_t)(KillingOff - DeadOff) < DeadSize) {
1202 return OW_MaybePartial;
1203 }
1204
1205 // Can reach here only if accesses are known not to overlap.
1206 return OW_None;
1207 }
1208
1209 bool isInvisibleToCallerAfterRet(const Value *V) {
1210 if (isa<AllocaInst>(V))
1211 return true;
1212
1213 auto I = InvisibleToCallerAfterRet.insert({V, false});
1214 if (I.second && isInvisibleToCallerOnUnwind(V) && isNoAliasCall(V))
1215 I.first->second = capturesNothing(PointerMayBeCaptured(
1216 V, /*ReturnCaptures=*/true, CaptureComponents::Provenance));
1217 return I.first->second;
1218 }
1219
1220 bool isInvisibleToCallerOnUnwind(const Value *V) {
1221 bool RequiresNoCaptureBeforeUnwind;
1222 if (!isNotVisibleOnUnwind(V, RequiresNoCaptureBeforeUnwind))
1223 return false;
1224 if (!RequiresNoCaptureBeforeUnwind)
1225 return true;
1226
1227 auto I = CapturedBeforeReturn.insert({V, true});
1228 if (I.second)
1229 // NOTE: This could be made more precise by PointerMayBeCapturedBefore
1230 // with the killing MemoryDef. But we refrain from doing so for now to
1231 // limit compile-time and this does not cause any changes to the number
1232 // of stores removed on a large test set in practice.
1233 I.first->second = capturesAnything(PointerMayBeCaptured(
1234 V, /*ReturnCaptures=*/false, CaptureComponents::Provenance));
1235 return !I.first->second;
1236 }
1237
1238 std::optional<MemoryLocation> getLocForWrite(Instruction *I) const {
1239 if (!I->mayWriteToMemory())
1240 return std::nullopt;
1241
1242 if (auto *CB = dyn_cast<CallBase>(I))
1243 return MemoryLocation::getForDest(CB, TLI);
1244
1246 }
1247
1248 // Returns a list of <MemoryLocation, bool> pairs written by I.
1249 // The bool means whether the write is from Initializes attr.
1251 getLocForInst(Instruction *I, bool ConsiderInitializesAttr) {
1253 if (isMemTerminatorInst(I)) {
1254 if (auto Loc = getLocForTerminator(I))
1255 Locations.push_back(std::make_pair(Loc->first, false));
1256 return Locations;
1257 }
1258
1259 if (auto Loc = getLocForWrite(I))
1260 Locations.push_back(std::make_pair(*Loc, false));
1261
1262 if (ConsiderInitializesAttr) {
1263 for (auto &MemLoc : getInitializesArgMemLoc(I)) {
1264 Locations.push_back(std::make_pair(MemLoc, true));
1265 }
1266 }
1267 return Locations;
1268 }
1269
1270 /// Assuming this instruction has a dead analyzable write, can we delete
1271 /// this instruction?
1272 bool isRemovable(Instruction *I) {
1273 assert(getLocForWrite(I) && "Must have analyzable write");
1274
1275 // Don't remove volatile/atomic stores.
1276 if (StoreInst *SI = dyn_cast<StoreInst>(I))
1277 return SI->isUnordered();
1278
1279 if (auto *CB = dyn_cast<CallBase>(I)) {
1280 // Don't remove volatile memory intrinsics.
1281 if (auto *MI = dyn_cast<MemIntrinsic>(CB))
1282 return !MI->isVolatile();
1283
1284 // Never remove dead lifetime intrinsics, e.g. because they are followed
1285 // by a free.
1286 if (CB->isLifetimeStartOrEnd())
1287 return false;
1288
1289 return CB->use_empty() && CB->willReturn() && CB->doesNotThrow() &&
1290 !CB->isTerminator();
1291 }
1292
1293 return false;
1294 }
1295
1296 /// Returns true if \p UseInst completely overwrites \p DefLoc
1297 /// (stored by \p DefInst).
1298 bool isCompleteOverwrite(const MemoryLocation &DefLoc, Instruction *DefInst,
1299 Instruction *UseInst) {
1300 // UseInst has a MemoryDef associated in MemorySSA. It's possible for a
1301 // MemoryDef to not write to memory, e.g. a volatile load is modeled as a
1302 // MemoryDef.
1303 if (!UseInst->mayWriteToMemory())
1304 return false;
1305
1306 if (auto *CB = dyn_cast<CallBase>(UseInst))
1307 if (CB->onlyAccessesInaccessibleMemory())
1308 return false;
1309
1310 int64_t InstWriteOffset, DepWriteOffset;
1311 if (auto CC = getLocForWrite(UseInst))
1312 return isOverwrite(UseInst, DefInst, *CC, DefLoc, InstWriteOffset,
1313 DepWriteOffset) == OW_Complete;
1314 return false;
1315 }
1316
1317 /// Returns true if \p Def is not read before returning from the function.
1318 bool isWriteAtEndOfFunction(MemoryDef *Def, const MemoryLocation &DefLoc) {
1319 LLVM_DEBUG(dbgs() << " Check if def " << *Def << " ("
1320 << *Def->getMemoryInst()
1321 << ") is at the end the function \n");
1323 SmallPtrSet<MemoryAccess *, 8> Visited;
1324
1325 pushMemUses(Def, WorkList, Visited);
1326 for (unsigned I = 0; I < WorkList.size(); I++) {
1327 if (WorkList.size() >= MemorySSAScanLimit) {
1328 LLVM_DEBUG(dbgs() << " ... hit exploration limit.\n");
1329 return false;
1330 }
1331
1332 MemoryAccess *UseAccess = WorkList[I];
1333 if (isa<MemoryPhi>(UseAccess)) {
1334 // AliasAnalysis does not account for loops. Limit elimination to
1335 // candidates for which we can guarantee they always store to the same
1336 // memory location.
1337 if (!isGuaranteedLoopInvariant(DefLoc.Ptr))
1338 return false;
1339
1340 pushMemUses(cast<MemoryPhi>(UseAccess), WorkList, Visited);
1341 continue;
1342 }
1343 // TODO: Checking for aliasing is expensive. Consider reducing the amount
1344 // of times this is called and/or caching it.
1345 Instruction *UseInst = cast<MemoryUseOrDef>(UseAccess)->getMemoryInst();
1346 if (isReadClobber(DefLoc, UseInst)) {
1347 LLVM_DEBUG(dbgs() << " ... hit read clobber " << *UseInst << ".\n");
1348 return false;
1349 }
1350
1351 if (MemoryDef *UseDef = dyn_cast<MemoryDef>(UseAccess))
1352 pushMemUses(UseDef, WorkList, Visited);
1353 }
1354 return true;
1355 }
1356
1357 /// If \p I is a memory terminator like llvm.lifetime.end or free, return a
1358 /// pair with the MemoryLocation terminated by \p I and a boolean flag
1359 /// indicating whether \p I is a free-like call.
1360 std::optional<std::pair<MemoryLocation, bool>>
1361 getLocForTerminator(Instruction *I) const {
1362 if (auto *CB = dyn_cast<CallBase>(I)) {
1363 if (CB->getIntrinsicID() == Intrinsic::lifetime_end)
1364 return {
1365 std::make_pair(MemoryLocation::getForArgument(CB, 0, &TLI), false)};
1366 if (Value *FreedOp = getFreedOperand(CB, &TLI))
1367 return {std::make_pair(MemoryLocation::getAfter(FreedOp), true)};
1368 }
1369
1370 return std::nullopt;
1371 }
1372
1373 /// Returns true if \p I is a memory terminator instruction like
1374 /// llvm.lifetime.end or free.
1375 bool isMemTerminatorInst(Instruction *I) const {
1376 auto *CB = dyn_cast<CallBase>(I);
1377 return CB && (CB->getIntrinsicID() == Intrinsic::lifetime_end ||
1378 getFreedOperand(CB, &TLI) != nullptr);
1379 }
1380
1381 /// Returns true if \p MaybeTerm is a memory terminator for \p Loc from
1382 /// instruction \p AccessI.
1383 bool isMemTerminator(const MemoryLocation &Loc, Instruction *AccessI,
1384 Instruction *MaybeTerm) {
1385 std::optional<std::pair<MemoryLocation, bool>> MaybeTermLoc =
1386 getLocForTerminator(MaybeTerm);
1387
1388 if (!MaybeTermLoc)
1389 return false;
1390
1391 // If the terminator is a free-like call, all accesses to the underlying
1392 // object can be considered terminated.
1393 if (getUnderlyingObject(Loc.Ptr) !=
1394 getUnderlyingObject(MaybeTermLoc->first.Ptr))
1395 return false;
1396
1397 auto TermLoc = MaybeTermLoc->first;
1398 if (MaybeTermLoc->second) {
1399 const Value *LocUO = getUnderlyingObject(Loc.Ptr);
1400 return BatchAA.isMustAlias(TermLoc.Ptr, LocUO);
1401 }
1402 int64_t InstWriteOffset = 0;
1403 int64_t DepWriteOffset = 0;
1404 return isOverwrite(MaybeTerm, AccessI, TermLoc, Loc, InstWriteOffset,
1405 DepWriteOffset) == OW_Complete;
1406 }
1407
1408 // Returns true if \p Use may read from \p DefLoc.
1409 bool isReadClobber(const MemoryLocation &DefLoc, Instruction *UseInst) {
1410 if (isNoopIntrinsic(UseInst))
1411 return false;
1412
1413 // Monotonic or weaker atomic stores can be re-ordered and do not need to be
1414 // treated as read clobber.
1415 if (auto SI = dyn_cast<StoreInst>(UseInst))
1416 return isStrongerThan(SI->getOrdering(), AtomicOrdering::Monotonic);
1417
1418 if (!UseInst->mayReadFromMemory())
1419 return false;
1420
1421 if (auto *CB = dyn_cast<CallBase>(UseInst))
1422 if (CB->onlyAccessesInaccessibleMemory())
1423 return false;
1424
1425 return isRefSet(BatchAA.getModRefInfo(UseInst, DefLoc));
1426 }
1427
1428 /// Returns true if a dependency between \p Current and \p KillingDef is
1429 /// guaranteed to be loop invariant for the loops that they are in. Either
1430 /// because they are known to be in the same block, in the same loop level or
1431 /// by guaranteeing that \p CurrentLoc only references a single MemoryLocation
1432 /// during execution of the containing function.
1433 bool isGuaranteedLoopIndependent(const Instruction *Current,
1434 const Instruction *KillingDef,
1435 const MemoryLocation &CurrentLoc) {
1436 // If the dependency is within the same block or loop level (being careful
1437 // of irreducible loops), we know that AA will return a valid result for the
1438 // memory dependency. (Both at the function level, outside of any loop,
1439 // would also be valid but we currently disable that to limit compile time).
1440 if (Current->getParent() == KillingDef->getParent())
1441 return true;
1442 const Loop *CurrentLI = LI.getLoopFor(Current->getParent());
1443 if (!ContainsIrreducibleLoops && CurrentLI &&
1444 CurrentLI == LI.getLoopFor(KillingDef->getParent()))
1445 return true;
1446 // Otherwise check the memory location is invariant to any loops.
1447 return isGuaranteedLoopInvariant(CurrentLoc.Ptr);
1448 }
1449
1450 /// Returns true if \p Ptr is guaranteed to be loop invariant for any possible
1451 /// loop. In particular, this guarantees that it only references a single
1452 /// MemoryLocation during execution of the containing function.
1453 bool isGuaranteedLoopInvariant(const Value *Ptr) {
1454 Ptr = Ptr->stripPointerCasts();
1455 if (auto *GEP = dyn_cast<GEPOperator>(Ptr))
1456 if (GEP->hasAllConstantIndices())
1457 Ptr = GEP->getPointerOperand()->stripPointerCasts();
1458
1459 if (auto *I = dyn_cast<Instruction>(Ptr)) {
1460 return I->getParent()->isEntryBlock() ||
1461 (!ContainsIrreducibleLoops && !LI.getLoopFor(I->getParent()));
1462 }
1463 return true;
1464 }
1465
1466 // Find a MemoryDef writing to \p KillingLoc and dominating \p StartAccess,
1467 // with no read access between them or on any other path to a function exit
1468 // block if \p KillingLoc is not accessible after the function returns. If
1469 // there is no such MemoryDef, return std::nullopt. The returned value may not
1470 // (completely) overwrite \p KillingLoc. Currently we bail out when we
1471 // encounter an aliasing MemoryUse (read).
1472 std::optional<MemoryAccess *>
1473 getDomMemoryDef(MemoryDef *KillingDef, MemoryAccess *StartAccess,
1474 const MemoryLocation &KillingLoc, const Value *KillingUndObj,
1475 unsigned &ScanLimit, unsigned &WalkerStepLimit,
1476 bool IsMemTerm, unsigned &PartialLimit,
1477 bool IsInitializesAttrMemLoc) {
1478 if (ScanLimit == 0 || WalkerStepLimit == 0) {
1479 LLVM_DEBUG(dbgs() << "\n ... hit scan limit\n");
1480 return std::nullopt;
1481 }
1482
1483 MemoryAccess *Current = StartAccess;
1484 Instruction *KillingI = KillingDef->getMemoryInst();
1485 LLVM_DEBUG(dbgs() << " trying to get dominating access\n");
1486
1487 // Only optimize defining access of KillingDef when directly starting at its
1488 // defining access. The defining access also must only access KillingLoc. At
1489 // the moment we only support instructions with a single write location, so
1490 // it should be sufficient to disable optimizations for instructions that
1491 // also read from memory.
1492 bool CanOptimize = OptimizeMemorySSA &&
1493 KillingDef->getDefiningAccess() == StartAccess &&
1494 !KillingI->mayReadFromMemory();
1495
1496 // Find the next clobbering Mod access for DefLoc, starting at StartAccess.
1497 std::optional<MemoryLocation> CurrentLoc;
1498 for (;; Current = cast<MemoryDef>(Current)->getDefiningAccess()) {
1499 LLVM_DEBUG({
1500 dbgs() << " visiting " << *Current;
1501 if (!MSSA.isLiveOnEntryDef(Current) && isa<MemoryUseOrDef>(Current))
1502 dbgs() << " (" << *cast<MemoryUseOrDef>(Current)->getMemoryInst()
1503 << ")";
1504 dbgs() << "\n";
1505 });
1506
1507 // Reached TOP.
1508 if (MSSA.isLiveOnEntryDef(Current)) {
1509 LLVM_DEBUG(dbgs() << " ... found LiveOnEntryDef\n");
1510 if (CanOptimize && Current != KillingDef->getDefiningAccess())
1511 // The first clobbering def is... none.
1512 KillingDef->setOptimized(Current);
1513 return std::nullopt;
1514 }
1515
1516 // Cost of a step. Accesses in the same block are more likely to be valid
1517 // candidates for elimination, hence consider them cheaper.
1518 unsigned StepCost = KillingDef->getBlock() == Current->getBlock()
1521 if (WalkerStepLimit <= StepCost) {
1522 LLVM_DEBUG(dbgs() << " ... hit walker step limit\n");
1523 return std::nullopt;
1524 }
1525 WalkerStepLimit -= StepCost;
1526
1527 // Return for MemoryPhis. They cannot be eliminated directly and the
1528 // caller is responsible for traversing them.
1529 if (isa<MemoryPhi>(Current)) {
1530 LLVM_DEBUG(dbgs() << " ... found MemoryPhi\n");
1531 return Current;
1532 }
1533
1534 // Below, check if CurrentDef is a valid candidate to be eliminated by
1535 // KillingDef. If it is not, check the next candidate.
1536 MemoryDef *CurrentDef = cast<MemoryDef>(Current);
1537 Instruction *CurrentI = CurrentDef->getMemoryInst();
1538
1539 if (canSkipDef(CurrentDef, !isInvisibleToCallerOnUnwind(KillingUndObj))) {
1540 CanOptimize = false;
1541 continue;
1542 }
1543
1544 // Before we try to remove anything, check for any extra throwing
1545 // instructions that block us from DSEing
1546 if (mayThrowBetween(KillingI, CurrentI, KillingUndObj)) {
1547 LLVM_DEBUG(dbgs() << " ... skip, may throw!\n");
1548 return std::nullopt;
1549 }
1550
1551 // Check for anything that looks like it will be a barrier to further
1552 // removal
1553 if (isDSEBarrier(KillingUndObj, CurrentI)) {
1554 LLVM_DEBUG(dbgs() << " ... skip, barrier\n");
1555 return std::nullopt;
1556 }
1557
1558 // If Current is known to be on path that reads DefLoc or is a read
1559 // clobber, bail out, as the path is not profitable. We skip this check
1560 // for intrinsic calls, because the code knows how to handle memcpy
1561 // intrinsics.
1562 if (!isa<IntrinsicInst>(CurrentI) && isReadClobber(KillingLoc, CurrentI))
1563 return std::nullopt;
1564
1565 // Quick check if there are direct uses that are read-clobbers.
1566 if (any_of(Current->uses(), [this, &KillingLoc, StartAccess](Use &U) {
1567 if (auto *UseOrDef = dyn_cast<MemoryUseOrDef>(U.getUser()))
1568 return !MSSA.dominates(StartAccess, UseOrDef) &&
1569 isReadClobber(KillingLoc, UseOrDef->getMemoryInst());
1570 return false;
1571 })) {
1572 LLVM_DEBUG(dbgs() << " ... found a read clobber\n");
1573 return std::nullopt;
1574 }
1575
1576 // If Current does not have an analyzable write location or is not
1577 // removable, skip it.
1578 CurrentLoc = getLocForWrite(CurrentI);
1579 if (!CurrentLoc || !isRemovable(CurrentI)) {
1580 CanOptimize = false;
1581 continue;
1582 }
1583
1584 // AliasAnalysis does not account for loops. Limit elimination to
1585 // candidates for which we can guarantee they always store to the same
1586 // memory location and not located in different loops.
1587 if (!isGuaranteedLoopIndependent(CurrentI, KillingI, *CurrentLoc)) {
1588 LLVM_DEBUG(dbgs() << " ... not guaranteed loop independent\n");
1589 CanOptimize = false;
1590 continue;
1591 }
1592
1593 if (IsMemTerm) {
1594 // If the killing def is a memory terminator (e.g. lifetime.end), check
1595 // the next candidate if the current Current does not write the same
1596 // underlying object as the terminator.
1597 if (!isMemTerminator(*CurrentLoc, CurrentI, KillingI)) {
1598 CanOptimize = false;
1599 continue;
1600 }
1601 } else {
1602 int64_t KillingOffset = 0;
1603 int64_t DeadOffset = 0;
1604 auto OR = isOverwrite(KillingI, CurrentI, KillingLoc, *CurrentLoc,
1605 KillingOffset, DeadOffset);
1606 if (CanOptimize) {
1607 // CurrentDef is the earliest write clobber of KillingDef. Use it as
1608 // optimized access. Do not optimize if CurrentDef is already the
1609 // defining access of KillingDef.
1610 if (CurrentDef != KillingDef->getDefiningAccess() &&
1611 (OR == OW_Complete || OR == OW_MaybePartial))
1612 KillingDef->setOptimized(CurrentDef);
1613
1614 // Once a may-aliasing def is encountered do not set an optimized
1615 // access.
1616 if (OR != OW_None)
1617 CanOptimize = false;
1618 }
1619
1620 // If Current does not write to the same object as KillingDef, check
1621 // the next candidate.
1622 if (OR == OW_Unknown || OR == OW_None)
1623 continue;
1624 else if (OR == OW_MaybePartial) {
1625 // If KillingDef only partially overwrites Current, check the next
1626 // candidate if the partial step limit is exceeded. This aggressively
1627 // limits the number of candidates for partial store elimination,
1628 // which are less likely to be removable in the end.
1629 if (PartialLimit <= 1) {
1630 WalkerStepLimit -= 1;
1631 LLVM_DEBUG(dbgs() << " ... reached partial limit ... continue with next access\n");
1632 continue;
1633 }
1634 PartialLimit -= 1;
1635 }
1636 }
1637 break;
1638 };
1639
1640 // Accesses to objects accessible after the function returns can only be
1641 // eliminated if the access is dead along all paths to the exit. Collect
1642 // the blocks with killing (=completely overwriting MemoryDefs) and check if
1643 // they cover all paths from MaybeDeadAccess to any function exit.
1644 SmallPtrSet<Instruction *, 16> KillingDefs;
1645 KillingDefs.insert(KillingDef->getMemoryInst());
1646 MemoryAccess *MaybeDeadAccess = Current;
1647 MemoryLocation MaybeDeadLoc = *CurrentLoc;
1648 Instruction *MaybeDeadI = cast<MemoryDef>(MaybeDeadAccess)->getMemoryInst();
1649 LLVM_DEBUG(dbgs() << " Checking for reads of " << *MaybeDeadAccess << " ("
1650 << *MaybeDeadI << ")\n");
1651
1653 SmallPtrSet<MemoryAccess *, 32> Visited;
1654 pushMemUses(MaybeDeadAccess, WorkList, Visited);
1655
1656 // Check if DeadDef may be read.
1657 for (unsigned I = 0; I < WorkList.size(); I++) {
1658 MemoryAccess *UseAccess = WorkList[I];
1659
1660 LLVM_DEBUG(dbgs() << " " << *UseAccess);
1661 // Bail out if the number of accesses to check exceeds the scan limit.
1662 if (ScanLimit < (WorkList.size() - I)) {
1663 LLVM_DEBUG(dbgs() << "\n ... hit scan limit\n");
1664 return std::nullopt;
1665 }
1666 --ScanLimit;
1667 NumDomMemDefChecks++;
1668
1669 if (isa<MemoryPhi>(UseAccess)) {
1670 if (any_of(KillingDefs, [this, UseAccess](Instruction *KI) {
1671 return DT.properlyDominates(KI->getParent(),
1672 UseAccess->getBlock());
1673 })) {
1674 LLVM_DEBUG(dbgs() << " ... skipping, dominated by killing block\n");
1675 continue;
1676 }
1677 LLVM_DEBUG(dbgs() << "\n ... adding PHI uses\n");
1678 pushMemUses(UseAccess, WorkList, Visited);
1679 continue;
1680 }
1681
1682 Instruction *UseInst = cast<MemoryUseOrDef>(UseAccess)->getMemoryInst();
1683 LLVM_DEBUG(dbgs() << " (" << *UseInst << ")\n");
1684
1685 if (any_of(KillingDefs, [this, UseInst](Instruction *KI) {
1686 return DT.dominates(KI, UseInst);
1687 })) {
1688 LLVM_DEBUG(dbgs() << " ... skipping, dominated by killing def\n");
1689 continue;
1690 }
1691
1692 // A memory terminator kills all preceeding MemoryDefs and all succeeding
1693 // MemoryAccesses. We do not have to check it's users.
1694 if (isMemTerminator(MaybeDeadLoc, MaybeDeadI, UseInst)) {
1695 LLVM_DEBUG(
1696 dbgs()
1697 << " ... skipping, memterminator invalidates following accesses\n");
1698 continue;
1699 }
1700
1701 if (isNoopIntrinsic(cast<MemoryUseOrDef>(UseAccess)->getMemoryInst())) {
1702 LLVM_DEBUG(dbgs() << " ... adding uses of intrinsic\n");
1703 pushMemUses(UseAccess, WorkList, Visited);
1704 continue;
1705 }
1706
1707 if (UseInst->mayThrow() && !isInvisibleToCallerOnUnwind(KillingUndObj)) {
1708 LLVM_DEBUG(dbgs() << " ... found throwing instruction\n");
1709 return std::nullopt;
1710 }
1711
1712 // Uses which may read the original MemoryDef mean we cannot eliminate the
1713 // original MD. Stop walk.
1714 // If KillingDef is a CallInst with "initializes" attribute, the reads in
1715 // the callee would be dominated by initializations, so it should be safe.
1716 bool IsKillingDefFromInitAttr = false;
1717 if (IsInitializesAttrMemLoc) {
1718 if (KillingI == UseInst &&
1719 KillingUndObj == getUnderlyingObject(MaybeDeadLoc.Ptr))
1720 IsKillingDefFromInitAttr = true;
1721 }
1722
1723 if (isReadClobber(MaybeDeadLoc, UseInst) && !IsKillingDefFromInitAttr) {
1724 LLVM_DEBUG(dbgs() << " ... found read clobber\n");
1725 return std::nullopt;
1726 }
1727
1728 // If this worklist walks back to the original memory access (and the
1729 // pointer is not guarenteed loop invariant) then we cannot assume that a
1730 // store kills itself.
1731 if (MaybeDeadAccess == UseAccess &&
1732 !isGuaranteedLoopInvariant(MaybeDeadLoc.Ptr)) {
1733 LLVM_DEBUG(dbgs() << " ... found not loop invariant self access\n");
1734 return std::nullopt;
1735 }
1736 // Otherwise, for the KillingDef and MaybeDeadAccess we only have to check
1737 // if it reads the memory location.
1738 // TODO: It would probably be better to check for self-reads before
1739 // calling the function.
1740 if (KillingDef == UseAccess || MaybeDeadAccess == UseAccess) {
1741 LLVM_DEBUG(dbgs() << " ... skipping killing def/dom access\n");
1742 continue;
1743 }
1744
1745 // Check all uses for MemoryDefs, except for defs completely overwriting
1746 // the original location. Otherwise we have to check uses of *all*
1747 // MemoryDefs we discover, including non-aliasing ones. Otherwise we might
1748 // miss cases like the following
1749 // 1 = Def(LoE) ; <----- DeadDef stores [0,1]
1750 // 2 = Def(1) ; (2, 1) = NoAlias, stores [2,3]
1751 // Use(2) ; MayAlias 2 *and* 1, loads [0, 3].
1752 // (The Use points to the *first* Def it may alias)
1753 // 3 = Def(1) ; <---- Current (3, 2) = NoAlias, (3,1) = MayAlias,
1754 // stores [0,1]
1755 if (MemoryDef *UseDef = dyn_cast<MemoryDef>(UseAccess)) {
1756 if (isCompleteOverwrite(MaybeDeadLoc, MaybeDeadI, UseInst)) {
1757 BasicBlock *MaybeKillingBlock = UseInst->getParent();
1758 if (PostOrderNumbers.find(MaybeKillingBlock)->second <
1759 PostOrderNumbers.find(MaybeDeadAccess->getBlock())->second) {
1760 if (!isInvisibleToCallerAfterRet(KillingUndObj)) {
1762 << " ... found killing def " << *UseInst << "\n");
1763 KillingDefs.insert(UseInst);
1764 }
1765 } else {
1767 << " ... found preceeding def " << *UseInst << "\n");
1768 return std::nullopt;
1769 }
1770 } else
1771 pushMemUses(UseDef, WorkList, Visited);
1772 }
1773 }
1774
1775 // For accesses to locations visible after the function returns, make sure
1776 // that the location is dead (=overwritten) along all paths from
1777 // MaybeDeadAccess to the exit.
1778 if (!isInvisibleToCallerAfterRet(KillingUndObj)) {
1779 SmallPtrSet<BasicBlock *, 16> KillingBlocks;
1780 for (Instruction *KD : KillingDefs)
1781 KillingBlocks.insert(KD->getParent());
1782 assert(!KillingBlocks.empty() &&
1783 "Expected at least a single killing block");
1784
1785 // Find the common post-dominator of all killing blocks.
1786 BasicBlock *CommonPred = *KillingBlocks.begin();
1787 for (BasicBlock *BB : llvm::drop_begin(KillingBlocks)) {
1788 if (!CommonPred)
1789 break;
1790 CommonPred = PDT.findNearestCommonDominator(CommonPred, BB);
1791 }
1792
1793 // If the common post-dominator does not post-dominate MaybeDeadAccess,
1794 // there is a path from MaybeDeadAccess to an exit not going through a
1795 // killing block.
1796 if (!PDT.dominates(CommonPred, MaybeDeadAccess->getBlock())) {
1797 if (!AnyUnreachableExit)
1798 return std::nullopt;
1799
1800 // Fall back to CFG scan starting at all non-unreachable roots if not
1801 // all paths to the exit go through CommonPred.
1802 CommonPred = nullptr;
1803 }
1804
1805 // If CommonPred itself is in the set of killing blocks, we're done.
1806 if (KillingBlocks.count(CommonPred))
1807 return {MaybeDeadAccess};
1808
1809 SetVector<BasicBlock *> WorkList;
1810 // If CommonPred is null, there are multiple exits from the function.
1811 // They all have to be added to the worklist.
1812 if (CommonPred)
1813 WorkList.insert(CommonPred);
1814 else
1815 for (BasicBlock *R : PDT.roots()) {
1816 if (!isa<UnreachableInst>(R->getTerminator()))
1817 WorkList.insert(R);
1818 }
1819
1820 NumCFGTries++;
1821 // Check if all paths starting from an exit node go through one of the
1822 // killing blocks before reaching MaybeDeadAccess.
1823 for (unsigned I = 0; I < WorkList.size(); I++) {
1824 NumCFGChecks++;
1825 BasicBlock *Current = WorkList[I];
1826 if (KillingBlocks.count(Current))
1827 continue;
1828 if (Current == MaybeDeadAccess->getBlock())
1829 return std::nullopt;
1830
1831 // MaybeDeadAccess is reachable from the entry, so we don't have to
1832 // explore unreachable blocks further.
1833 if (!DT.isReachableFromEntry(Current))
1834 continue;
1835
1836 WorkList.insert_range(predecessors(Current));
1837
1838 if (WorkList.size() >= MemorySSAPathCheckLimit)
1839 return std::nullopt;
1840 }
1841 NumCFGSuccess++;
1842 }
1843
1844 // No aliasing MemoryUses of MaybeDeadAccess found, MaybeDeadAccess is
1845 // potentially dead.
1846 return {MaybeDeadAccess};
1847 }
1848
1849 /// Delete dead memory defs and recursively add their operands to ToRemove if
1850 /// they became dead.
1851 void
1852 deleteDeadInstruction(Instruction *SI,
1853 SmallPtrSetImpl<MemoryAccess *> *Deleted = nullptr) {
1854 MemorySSAUpdater Updater(&MSSA);
1855 SmallVector<Instruction *, 32> NowDeadInsts;
1856 NowDeadInsts.push_back(SI);
1857 --NumFastOther;
1858
1859 while (!NowDeadInsts.empty()) {
1860 Instruction *DeadInst = NowDeadInsts.pop_back_val();
1861 ++NumFastOther;
1862
1863 // Try to preserve debug information attached to the dead instruction.
1864 salvageDebugInfo(*DeadInst);
1865 salvageKnowledge(DeadInst);
1866
1867 // Remove the Instruction from MSSA.
1868 MemoryAccess *MA = MSSA.getMemoryAccess(DeadInst);
1869 bool IsMemDef = MA && isa<MemoryDef>(MA);
1870 if (MA) {
1871 if (IsMemDef) {
1872 auto *MD = cast<MemoryDef>(MA);
1873 SkipStores.insert(MD);
1874 if (Deleted)
1875 Deleted->insert(MD);
1876 if (auto *SI = dyn_cast<StoreInst>(MD->getMemoryInst())) {
1877 if (SI->getValueOperand()->getType()->isPointerTy()) {
1878 const Value *UO = getUnderlyingObject(SI->getValueOperand());
1879 if (CapturedBeforeReturn.erase(UO))
1880 ShouldIterateEndOfFunctionDSE = true;
1881 InvisibleToCallerAfterRet.erase(UO);
1882 }
1883 }
1884 }
1885
1886 Updater.removeMemoryAccess(MA);
1887 }
1888
1889 auto I = IOLs.find(DeadInst->getParent());
1890 if (I != IOLs.end())
1891 I->second.erase(DeadInst);
1892 // Remove its operands
1893 for (Use &O : DeadInst->operands())
1894 if (Instruction *OpI = dyn_cast<Instruction>(O)) {
1895 O.set(PoisonValue::get(O->getType()));
1896 if (isInstructionTriviallyDead(OpI, &TLI))
1897 NowDeadInsts.push_back(OpI);
1898 }
1899
1900 EA.removeInstruction(DeadInst);
1901 // Remove memory defs directly if they don't produce results, but only
1902 // queue other dead instructions for later removal. They may have been
1903 // used as memory locations that have been cached by BatchAA. Removing
1904 // them here may lead to newly created instructions to be allocated at the
1905 // same address, yielding stale cache entries.
1906 if (IsMemDef && DeadInst->getType()->isVoidTy())
1907 DeadInst->eraseFromParent();
1908 else
1909 ToRemove.push_back(DeadInst);
1910 }
1911 }
1912
1913 // Check for any extra throws between \p KillingI and \p DeadI that block
1914 // DSE. This only checks extra maythrows (those that aren't MemoryDef's).
1915 // MemoryDef that may throw are handled during the walk from one def to the
1916 // next.
1917 bool mayThrowBetween(Instruction *KillingI, Instruction *DeadI,
1918 const Value *KillingUndObj) {
1919 // First see if we can ignore it by using the fact that KillingI is an
1920 // alloca/alloca like object that is not visible to the caller during
1921 // execution of the function.
1922 if (KillingUndObj && isInvisibleToCallerOnUnwind(KillingUndObj))
1923 return false;
1924
1925 if (KillingI->getParent() == DeadI->getParent())
1926 return ThrowingBlocks.count(KillingI->getParent());
1927 return !ThrowingBlocks.empty();
1928 }
1929
1930 // Check if \p DeadI acts as a DSE barrier for \p KillingI. The following
1931 // instructions act as barriers:
1932 // * A memory instruction that may throw and \p KillingI accesses a non-stack
1933 // object.
1934 // * Atomic stores stronger that monotonic.
1935 bool isDSEBarrier(const Value *KillingUndObj, Instruction *DeadI) {
1936 // If DeadI may throw it acts as a barrier, unless we are to an
1937 // alloca/alloca like object that does not escape.
1938 if (DeadI->mayThrow() && !isInvisibleToCallerOnUnwind(KillingUndObj))
1939 return true;
1940
1941 // If DeadI is an atomic load/store stronger than monotonic, do not try to
1942 // eliminate/reorder it.
1943 if (DeadI->isAtomic()) {
1944 if (auto *LI = dyn_cast<LoadInst>(DeadI))
1945 return isStrongerThanMonotonic(LI->getOrdering());
1946 if (auto *SI = dyn_cast<StoreInst>(DeadI))
1947 return isStrongerThanMonotonic(SI->getOrdering());
1948 if (auto *ARMW = dyn_cast<AtomicRMWInst>(DeadI))
1949 return isStrongerThanMonotonic(ARMW->getOrdering());
1950 if (auto *CmpXchg = dyn_cast<AtomicCmpXchgInst>(DeadI))
1951 return isStrongerThanMonotonic(CmpXchg->getSuccessOrdering()) ||
1952 isStrongerThanMonotonic(CmpXchg->getFailureOrdering());
1953 llvm_unreachable("other instructions should be skipped in MemorySSA");
1954 }
1955 return false;
1956 }
1957
1958 /// Eliminate writes to objects that are not visible in the caller and are not
1959 /// accessed before returning from the function.
1960 bool eliminateDeadWritesAtEndOfFunction() {
1961 bool MadeChange = false;
1962 LLVM_DEBUG(
1963 dbgs()
1964 << "Trying to eliminate MemoryDefs at the end of the function\n");
1965 do {
1966 ShouldIterateEndOfFunctionDSE = false;
1967 for (MemoryDef *Def : llvm::reverse(MemDefs)) {
1968 if (SkipStores.contains(Def))
1969 continue;
1970
1971 Instruction *DefI = Def->getMemoryInst();
1972 auto DefLoc = getLocForWrite(DefI);
1973 if (!DefLoc || !isRemovable(DefI)) {
1974 LLVM_DEBUG(dbgs() << " ... could not get location for write or "
1975 "instruction not removable.\n");
1976 continue;
1977 }
1978
1979 // NOTE: Currently eliminating writes at the end of a function is
1980 // limited to MemoryDefs with a single underlying object, to save
1981 // compile-time. In practice it appears the case with multiple
1982 // underlying objects is very uncommon. If it turns out to be important,
1983 // we can use getUnderlyingObjects here instead.
1984 const Value *UO = getUnderlyingObject(DefLoc->Ptr);
1985 if (!isInvisibleToCallerAfterRet(UO))
1986 continue;
1987
1988 if (isWriteAtEndOfFunction(Def, *DefLoc)) {
1989 // See through pointer-to-pointer bitcasts
1990 LLVM_DEBUG(dbgs() << " ... MemoryDef is not accessed until the end "
1991 "of the function\n");
1993 ++NumFastStores;
1994 MadeChange = true;
1995 }
1996 }
1997 } while (ShouldIterateEndOfFunctionDSE);
1998 return MadeChange;
1999 }
2000
2001 /// If we have a zero initializing memset following a call to malloc,
2002 /// try folding it into a call to calloc.
2003 bool tryFoldIntoCalloc(MemoryDef *Def, const Value *DefUO) {
2004 Instruction *DefI = Def->getMemoryInst();
2005 MemSetInst *MemSet = dyn_cast<MemSetInst>(DefI);
2006 if (!MemSet)
2007 // TODO: Could handle zero store to small allocation as well.
2008 return false;
2009 Constant *StoredConstant = dyn_cast<Constant>(MemSet->getValue());
2010 if (!StoredConstant || !StoredConstant->isNullValue())
2011 return false;
2012
2013 if (!isRemovable(DefI))
2014 // The memset might be volatile..
2015 return false;
2016
2017 if (F.hasFnAttribute(Attribute::SanitizeMemory) ||
2018 F.hasFnAttribute(Attribute::SanitizeAddress) ||
2019 F.hasFnAttribute(Attribute::SanitizeHWAddress) ||
2020 F.getName() == "calloc")
2021 return false;
2022 auto *Malloc = const_cast<CallInst *>(dyn_cast<CallInst>(DefUO));
2023 if (!Malloc)
2024 return false;
2025 auto *InnerCallee = Malloc->getCalledFunction();
2026 if (!InnerCallee)
2027 return false;
2028 LibFunc Func = NotLibFunc;
2029 StringRef ZeroedVariantName;
2030 if (!TLI.getLibFunc(*InnerCallee, Func) || !TLI.has(Func) ||
2031 Func != LibFunc_malloc) {
2032 Attribute Attr = Malloc->getFnAttr("alloc-variant-zeroed");
2033 if (!Attr.isValid())
2034 return false;
2035 ZeroedVariantName = Attr.getValueAsString();
2036 if (ZeroedVariantName.empty())
2037 return false;
2038 }
2039
2040 // Gracefully handle malloc with unexpected memory attributes.
2041 auto *MallocDef = dyn_cast_or_null<MemoryDef>(MSSA.getMemoryAccess(Malloc));
2042 if (!MallocDef)
2043 return false;
2044
2045 auto shouldCreateCalloc = [](CallInst *Malloc, CallInst *Memset) {
2046 // Check for br(icmp ptr, null), truebb, falsebb) pattern at the end
2047 // of malloc block
2048 auto *MallocBB = Malloc->getParent(),
2049 *MemsetBB = Memset->getParent();
2050 if (MallocBB == MemsetBB)
2051 return true;
2052 auto *Ptr = Memset->getArgOperand(0);
2053 auto *TI = MallocBB->getTerminator();
2054 BasicBlock *TrueBB, *FalseBB;
2055 if (!match(TI, m_Br(m_SpecificICmp(ICmpInst::ICMP_EQ, m_Specific(Ptr),
2056 m_Zero()),
2057 TrueBB, FalseBB)))
2058 return false;
2059 if (MemsetBB != FalseBB)
2060 return false;
2061 return true;
2062 };
2063
2064 if (Malloc->getOperand(0) != MemSet->getLength())
2065 return false;
2066 if (!shouldCreateCalloc(Malloc, MemSet) || !DT.dominates(Malloc, MemSet) ||
2067 !memoryIsNotModifiedBetween(Malloc, MemSet, BatchAA, DL, &DT))
2068 return false;
2069 IRBuilder<> IRB(Malloc);
2070 assert(Func == LibFunc_malloc || !ZeroedVariantName.empty());
2071 Value *Calloc = nullptr;
2072 if (!ZeroedVariantName.empty()) {
2073 LLVMContext &Ctx = Malloc->getContext();
2074 AttributeList Attrs = InnerCallee->getAttributes();
2075 AllocFnKind AllocKind =
2076 Attrs.getFnAttr(Attribute::AllocKind).getAllocKind() |
2077 AllocFnKind::Zeroed;
2078 AllocKind &= ~AllocFnKind::Uninitialized;
2079 Attrs =
2080 Attrs.addFnAttribute(Ctx, Attribute::getWithAllocKind(Ctx, AllocKind))
2081 .removeFnAttribute(Ctx, "alloc-variant-zeroed");
2082 FunctionCallee ZeroedVariant = Malloc->getModule()->getOrInsertFunction(
2083 ZeroedVariantName, InnerCallee->getFunctionType(), Attrs);
2084 cast<Function>(ZeroedVariant.getCallee())
2085 ->setCallingConv(Malloc->getCallingConv());
2087 Args.append(Malloc->arg_begin(), Malloc->arg_end());
2088 CallInst *CI = IRB.CreateCall(ZeroedVariant, Args, ZeroedVariantName);
2089 CI->setCallingConv(Malloc->getCallingConv());
2090 Calloc = CI;
2091 } else {
2092 Type *SizeTTy = Malloc->getArgOperand(0)->getType();
2093 Calloc =
2094 emitCalloc(ConstantInt::get(SizeTTy, 1), Malloc->getArgOperand(0),
2095 IRB, TLI, Malloc->getType()->getPointerAddressSpace());
2096 }
2097 if (!Calloc)
2098 return false;
2099
2100 MemorySSAUpdater Updater(&MSSA);
2101 auto *NewAccess =
2102 Updater.createMemoryAccessAfter(cast<Instruction>(Calloc), nullptr,
2103 MallocDef);
2104 auto *NewAccessMD = cast<MemoryDef>(NewAccess);
2105 Updater.insertDef(NewAccessMD, /*RenameUses=*/true);
2106 Malloc->replaceAllUsesWith(Calloc);
2108 return true;
2109 }
2110
2111 // Check if there is a dominating condition, that implies that the value
2112 // being stored in a ptr is already present in the ptr.
2113 bool dominatingConditionImpliesValue(MemoryDef *Def) {
2114 auto *StoreI = cast<StoreInst>(Def->getMemoryInst());
2115 BasicBlock *StoreBB = StoreI->getParent();
2116 Value *StorePtr = StoreI->getPointerOperand();
2117 Value *StoreVal = StoreI->getValueOperand();
2118
2119 DomTreeNode *IDom = DT.getNode(StoreBB)->getIDom();
2120 if (!IDom)
2121 return false;
2122
2123 auto *BI = dyn_cast<BranchInst>(IDom->getBlock()->getTerminator());
2124 if (!BI || !BI->isConditional())
2125 return false;
2126
2127 // In case both blocks are the same, it is not possible to determine
2128 // if optimization is possible. (We would not want to optimize a store
2129 // in the FalseBB if condition is true and vice versa.)
2130 if (BI->getSuccessor(0) == BI->getSuccessor(1))
2131 return false;
2132
2133 Instruction *ICmpL;
2134 CmpPredicate Pred;
2135 if (!match(BI->getCondition(),
2136 m_c_ICmp(Pred,
2137 m_CombineAnd(m_Load(m_Specific(StorePtr)),
2138 m_Instruction(ICmpL)),
2139 m_Specific(StoreVal))) ||
2140 !ICmpInst::isEquality(Pred))
2141 return false;
2142
2143 // In case the else blocks also branches to the if block or the other way
2144 // around it is not possible to determine if the optimization is possible.
2145 if (Pred == ICmpInst::ICMP_EQ &&
2146 !DT.dominates(BasicBlockEdge(BI->getParent(), BI->getSuccessor(0)),
2147 StoreBB))
2148 return false;
2149
2150 if (Pred == ICmpInst::ICMP_NE &&
2151 !DT.dominates(BasicBlockEdge(BI->getParent(), BI->getSuccessor(1)),
2152 StoreBB))
2153 return false;
2154
2155 MemoryAccess *LoadAcc = MSSA.getMemoryAccess(ICmpL);
2156 MemoryAccess *ClobAcc =
2157 MSSA.getSkipSelfWalker()->getClobberingMemoryAccess(Def, BatchAA);
2158
2159 return MSSA.dominates(ClobAcc, LoadAcc);
2160 }
2161
2162 /// \returns true if \p Def is a no-op store, either because it
2163 /// directly stores back a loaded value or stores zero to a calloced object.
2164 bool storeIsNoop(MemoryDef *Def, const Value *DefUO) {
2165 Instruction *DefI = Def->getMemoryInst();
2166 StoreInst *Store = dyn_cast<StoreInst>(DefI);
2167 MemSetInst *MemSet = dyn_cast<MemSetInst>(DefI);
2168 Constant *StoredConstant = nullptr;
2169 if (Store)
2170 StoredConstant = dyn_cast<Constant>(Store->getOperand(0));
2171 else if (MemSet)
2172 StoredConstant = dyn_cast<Constant>(MemSet->getValue());
2173 else
2174 return false;
2175
2176 if (!isRemovable(DefI))
2177 return false;
2178
2179 if (StoredConstant) {
2180 Constant *InitC =
2181 getInitialValueOfAllocation(DefUO, &TLI, StoredConstant->getType());
2182 // If the clobbering access is LiveOnEntry, no instructions between them
2183 // can modify the memory location.
2184 if (InitC && InitC == StoredConstant)
2185 return MSSA.isLiveOnEntryDef(
2186 MSSA.getSkipSelfWalker()->getClobberingMemoryAccess(Def, BatchAA));
2187 }
2188
2189 if (!Store)
2190 return false;
2191
2192 if (dominatingConditionImpliesValue(Def))
2193 return true;
2194
2195 if (auto *LoadI = dyn_cast<LoadInst>(Store->getOperand(0))) {
2196 if (LoadI->getPointerOperand() == Store->getOperand(1)) {
2197 // Get the defining access for the load.
2198 auto *LoadAccess = MSSA.getMemoryAccess(LoadI)->getDefiningAccess();
2199 // Fast path: the defining accesses are the same.
2200 if (LoadAccess == Def->getDefiningAccess())
2201 return true;
2202
2203 // Look through phi accesses. Recursively scan all phi accesses by
2204 // adding them to a worklist. Bail when we run into a memory def that
2205 // does not match LoadAccess.
2206 SetVector<MemoryAccess *> ToCheck;
2207 MemoryAccess *Current =
2208 MSSA.getWalker()->getClobberingMemoryAccess(Def, BatchAA);
2209 // We don't want to bail when we run into the store memory def. But,
2210 // the phi access may point to it. So, pretend like we've already
2211 // checked it.
2212 ToCheck.insert(Def);
2213 ToCheck.insert(Current);
2214 // Start at current (1) to simulate already having checked Def.
2215 for (unsigned I = 1; I < ToCheck.size(); ++I) {
2216 Current = ToCheck[I];
2217 if (auto PhiAccess = dyn_cast<MemoryPhi>(Current)) {
2218 // Check all the operands.
2219 for (auto &Use : PhiAccess->incoming_values())
2220 ToCheck.insert(cast<MemoryAccess>(&Use));
2221 continue;
2222 }
2223
2224 // If we found a memory def, bail. This happens when we have an
2225 // unrelated write in between an otherwise noop store.
2226 assert(isa<MemoryDef>(Current) &&
2227 "Only MemoryDefs should reach here.");
2228 // TODO: Skip no alias MemoryDefs that have no aliasing reads.
2229 // We are searching for the definition of the store's destination.
2230 // So, if that is the same definition as the load, then this is a
2231 // noop. Otherwise, fail.
2232 if (LoadAccess != Current)
2233 return false;
2234 }
2235 return true;
2236 }
2237 }
2238
2239 return false;
2240 }
2241
2242 bool removePartiallyOverlappedStores(InstOverlapIntervalsTy &IOL) {
2243 bool Changed = false;
2244 for (auto OI : IOL) {
2245 Instruction *DeadI = OI.first;
2246 MemoryLocation Loc = *getLocForWrite(DeadI);
2247 assert(isRemovable(DeadI) && "Expect only removable instruction");
2248
2249 const Value *Ptr = Loc.Ptr->stripPointerCasts();
2250 int64_t DeadStart = 0;
2251 uint64_t DeadSize = Loc.Size.getValue();
2252 GetPointerBaseWithConstantOffset(Ptr, DeadStart, DL);
2253 OverlapIntervalsTy &IntervalMap = OI.second;
2254 Changed |= tryToShortenEnd(DeadI, IntervalMap, DeadStart, DeadSize);
2255 if (IntervalMap.empty())
2256 continue;
2257 Changed |= tryToShortenBegin(DeadI, IntervalMap, DeadStart, DeadSize);
2258 }
2259 return Changed;
2260 }
2261
2262 /// Eliminates writes to locations where the value that is being written
2263 /// is already stored at the same location.
2264 bool eliminateRedundantStoresOfExistingValues() {
2265 bool MadeChange = false;
2266 LLVM_DEBUG(dbgs() << "Trying to eliminate MemoryDefs that write the "
2267 "already existing value\n");
2268 for (auto *Def : MemDefs) {
2269 if (SkipStores.contains(Def) || MSSA.isLiveOnEntryDef(Def))
2270 continue;
2271
2272 Instruction *DefInst = Def->getMemoryInst();
2273 auto MaybeDefLoc = getLocForWrite(DefInst);
2274 if (!MaybeDefLoc || !isRemovable(DefInst))
2275 continue;
2276
2277 MemoryDef *UpperDef;
2278 // To conserve compile-time, we avoid walking to the next clobbering def.
2279 // Instead, we just try to get the optimized access, if it exists. DSE
2280 // will try to optimize defs during the earlier traversal.
2281 if (Def->isOptimized())
2282 UpperDef = dyn_cast<MemoryDef>(Def->getOptimized());
2283 else
2284 UpperDef = dyn_cast<MemoryDef>(Def->getDefiningAccess());
2285 if (!UpperDef || MSSA.isLiveOnEntryDef(UpperDef))
2286 continue;
2287
2288 Instruction *UpperInst = UpperDef->getMemoryInst();
2289 auto IsRedundantStore = [&]() {
2290 // We don't care about differences in call attributes here.
2291 if (DefInst->isIdenticalToWhenDefined(UpperInst,
2292 /*IntersectAttrs=*/true))
2293 return true;
2294 if (auto *MemSetI = dyn_cast<MemSetInst>(UpperInst)) {
2295 if (auto *SI = dyn_cast<StoreInst>(DefInst)) {
2296 // MemSetInst must have a write location.
2297 auto UpperLoc = getLocForWrite(UpperInst);
2298 if (!UpperLoc)
2299 return false;
2300 int64_t InstWriteOffset = 0;
2301 int64_t DepWriteOffset = 0;
2302 auto OR = isOverwrite(UpperInst, DefInst, *UpperLoc, *MaybeDefLoc,
2303 InstWriteOffset, DepWriteOffset);
2304 Value *StoredByte = isBytewiseValue(SI->getValueOperand(), DL);
2305 return StoredByte && StoredByte == MemSetI->getOperand(1) &&
2306 OR == OW_Complete;
2307 }
2308 }
2309 return false;
2310 };
2311
2312 if (!IsRedundantStore() || isReadClobber(*MaybeDefLoc, DefInst))
2313 continue;
2314 LLVM_DEBUG(dbgs() << "DSE: Remove No-Op Store:\n DEAD: " << *DefInst
2315 << '\n');
2316 deleteDeadInstruction(DefInst);
2317 NumRedundantStores++;
2318 MadeChange = true;
2319 }
2320 return MadeChange;
2321 }
2322
2323 // Return the locations written by the initializes attribute.
2324 // Note that this function considers:
2325 // 1. Unwind edge: use "initializes" attribute only if the callee has
2326 // "nounwind" attribute, or the argument has "dead_on_unwind" attribute,
2327 // or the argument is invisible to caller on unwind. That is, we don't
2328 // perform incorrect DSE on unwind edges in the current function.
2329 // 2. Argument alias: for aliasing arguments, the "initializes" attribute is
2330 // the intersected range list of their "initializes" attributes.
2331 SmallVector<MemoryLocation, 1> getInitializesArgMemLoc(const Instruction *I);
2332
2333 // Try to eliminate dead defs that access `KillingLocWrapper.MemLoc` and are
2334 // killed by `KillingLocWrapper.MemDef`. Return whether
2335 // any changes were made, and whether `KillingLocWrapper.DefInst` was deleted.
2336 std::pair<bool, bool>
2337 eliminateDeadDefs(const MemoryLocationWrapper &KillingLocWrapper);
2338
2339 // Try to eliminate dead defs killed by `KillingDefWrapper` and return the
2340 // change state: whether make any change.
2341 bool eliminateDeadDefs(const MemoryDefWrapper &KillingDefWrapper);
2342};
2343} // namespace
2344
2345// Return true if "Arg" is function local and isn't captured before "CB".
2346static bool isFuncLocalAndNotCaptured(Value *Arg, const CallBase *CB,
2348 const Value *UnderlyingObj = getUnderlyingObject(Arg);
2349 return isIdentifiedFunctionLocal(UnderlyingObj) &&
2351 EA.getCapturesBefore(UnderlyingObj, CB, /*OrAt*/ true));
2352}
2353
2355DSEState::getInitializesArgMemLoc(const Instruction *I) {
2356 const CallBase *CB = dyn_cast<CallBase>(I);
2357 if (!CB)
2358 return {};
2359
2360 // Collect aliasing arguments and their initializes ranges.
2361 SmallMapVector<Value *, SmallVector<ArgumentInitInfo, 2>, 2> Arguments;
2362 for (unsigned Idx = 0, Count = CB->arg_size(); Idx < Count; ++Idx) {
2363 Value *CurArg = CB->getArgOperand(Idx);
2364 if (!CurArg->getType()->isPointerTy())
2365 continue;
2366
2367 ConstantRangeList Inits;
2368 Attribute InitializesAttr = CB->getParamAttr(Idx, Attribute::Initializes);
2369 // initializes on byval arguments refers to the callee copy, not the
2370 // original memory the caller passed in.
2371 if (InitializesAttr.isValid() && !CB->isByValArgument(Idx))
2372 Inits = InitializesAttr.getValueAsConstantRangeList();
2373
2374 // Check whether "CurArg" could alias with global variables. We require
2375 // either it's function local and isn't captured before or the "CB" only
2376 // accesses arg or inaccessible mem.
2377 if (!Inits.empty() && !CB->onlyAccessesInaccessibleMemOrArgMem() &&
2378 !isFuncLocalAndNotCaptured(CurArg, CB, EA))
2379 Inits = ConstantRangeList();
2380
2381 // We don't perform incorrect DSE on unwind edges in the current function,
2382 // and use the "initializes" attribute to kill dead stores if:
2383 // - The call does not throw exceptions, "CB->doesNotThrow()".
2384 // - Or the callee parameter has "dead_on_unwind" attribute.
2385 // - Or the argument is invisible to caller on unwind, and there are no
2386 // unwind edges from this call in the current function (e.g. `CallInst`).
2387 bool IsDeadOrInvisibleOnUnwind =
2388 CB->paramHasAttr(Idx, Attribute::DeadOnUnwind) ||
2389 (isa<CallInst>(CB) && isInvisibleToCallerOnUnwind(CurArg));
2390 ArgumentInitInfo InitInfo{Idx, IsDeadOrInvisibleOnUnwind, Inits};
2391 bool FoundAliasing = false;
2392 for (auto &[Arg, AliasList] : Arguments) {
2393 auto AAR = BatchAA.alias(MemoryLocation::getBeforeOrAfter(Arg),
2395 if (AAR == AliasResult::NoAlias) {
2396 continue;
2397 } else if (AAR == AliasResult::MustAlias) {
2398 FoundAliasing = true;
2399 AliasList.push_back(InitInfo);
2400 } else {
2401 // For PartialAlias and MayAlias, there is an offset or may be an
2402 // unknown offset between the arguments and we insert an empty init
2403 // range to discard the entire initializes info while intersecting.
2404 FoundAliasing = true;
2405 AliasList.push_back(ArgumentInitInfo{Idx, IsDeadOrInvisibleOnUnwind,
2406 ConstantRangeList()});
2407 }
2408 }
2409 if (!FoundAliasing)
2410 Arguments[CurArg] = {InitInfo};
2411 }
2412
2414 for (const auto &[_, Args] : Arguments) {
2415 auto IntersectedRanges =
2417 if (IntersectedRanges.empty())
2418 continue;
2419
2420 for (const auto &Arg : Args) {
2421 for (const auto &Range : IntersectedRanges) {
2422 int64_t Start = Range.getLower().getSExtValue();
2423 int64_t End = Range.getUpper().getSExtValue();
2424 // For now, we only handle locations starting at offset 0.
2425 if (Start == 0)
2426 Locations.push_back(MemoryLocation(CB->getArgOperand(Arg.Idx),
2427 LocationSize::precise(End - Start),
2428 CB->getAAMetadata()));
2429 }
2430 }
2431 }
2432 return Locations;
2433}
2434
2435std::pair<bool, bool>
2436DSEState::eliminateDeadDefs(const MemoryLocationWrapper &KillingLocWrapper) {
2437 bool Changed = false;
2438 bool DeletedKillingLoc = false;
2439 unsigned ScanLimit = MemorySSAScanLimit;
2440 unsigned WalkerStepLimit = MemorySSAUpwardsStepLimit;
2441 unsigned PartialLimit = MemorySSAPartialStoreLimit;
2442 // Worklist of MemoryAccesses that may be killed by
2443 // "KillingLocWrapper.MemDef".
2444 SmallSetVector<MemoryAccess *, 8> ToCheck;
2445 // Track MemoryAccesses that have been deleted in the loop below, so we can
2446 // skip them. Don't use SkipStores for this, which may contain reused
2447 // MemoryAccess addresses.
2448 SmallPtrSet<MemoryAccess *, 8> Deleted;
2449 [[maybe_unused]] unsigned OrigNumSkipStores = SkipStores.size();
2450 ToCheck.insert(KillingLocWrapper.MemDef->getDefiningAccess());
2451
2452 // Check if MemoryAccesses in the worklist are killed by
2453 // "KillingLocWrapper.MemDef".
2454 for (unsigned I = 0; I < ToCheck.size(); I++) {
2455 MemoryAccess *Current = ToCheck[I];
2456 if (Deleted.contains(Current))
2457 continue;
2458 std::optional<MemoryAccess *> MaybeDeadAccess = getDomMemoryDef(
2459 KillingLocWrapper.MemDef, Current, KillingLocWrapper.MemLoc,
2460 KillingLocWrapper.UnderlyingObject, ScanLimit, WalkerStepLimit,
2461 isMemTerminatorInst(KillingLocWrapper.DefInst), PartialLimit,
2462 KillingLocWrapper.DefByInitializesAttr);
2463
2464 if (!MaybeDeadAccess) {
2465 LLVM_DEBUG(dbgs() << " finished walk\n");
2466 continue;
2467 }
2468 MemoryAccess *DeadAccess = *MaybeDeadAccess;
2469 LLVM_DEBUG(dbgs() << " Checking if we can kill " << *DeadAccess);
2470 if (isa<MemoryPhi>(DeadAccess)) {
2471 LLVM_DEBUG(dbgs() << "\n ... adding incoming values to worklist\n");
2472 for (Value *V : cast<MemoryPhi>(DeadAccess)->incoming_values()) {
2473 MemoryAccess *IncomingAccess = cast<MemoryAccess>(V);
2474 BasicBlock *IncomingBlock = IncomingAccess->getBlock();
2475 BasicBlock *PhiBlock = DeadAccess->getBlock();
2476
2477 // We only consider incoming MemoryAccesses that come before the
2478 // MemoryPhi. Otherwise we could discover candidates that do not
2479 // strictly dominate our starting def.
2480 if (PostOrderNumbers[IncomingBlock] > PostOrderNumbers[PhiBlock])
2481 ToCheck.insert(IncomingAccess);
2482 }
2483 continue;
2484 }
2485 // We cannot apply the initializes attribute to DeadAccess/DeadDef.
2486 // It would incorrectly consider a call instruction as redundant store
2487 // and remove this call instruction.
2488 // TODO: this conflates the existence of a MemoryLocation with being able
2489 // to delete the instruction. Fix isRemovable() to consider calls with
2490 // side effects that cannot be removed, e.g. calls with the initializes
2491 // attribute, and remove getLocForInst(ConsiderInitializesAttr = false).
2492 MemoryDefWrapper DeadDefWrapper(
2493 cast<MemoryDef>(DeadAccess),
2494 getLocForInst(cast<MemoryDef>(DeadAccess)->getMemoryInst(),
2495 /*ConsiderInitializesAttr=*/false));
2496 assert(DeadDefWrapper.DefinedLocations.size() == 1);
2497 MemoryLocationWrapper &DeadLocWrapper =
2498 DeadDefWrapper.DefinedLocations.front();
2499 LLVM_DEBUG(dbgs() << " (" << *DeadLocWrapper.DefInst << ")\n");
2500 ToCheck.insert(DeadLocWrapper.MemDef->getDefiningAccess());
2501 NumGetDomMemoryDefPassed++;
2502
2503 if (!DebugCounter::shouldExecute(MemorySSACounter))
2504 continue;
2505 if (isMemTerminatorInst(KillingLocWrapper.DefInst)) {
2506 if (KillingLocWrapper.UnderlyingObject != DeadLocWrapper.UnderlyingObject)
2507 continue;
2508 LLVM_DEBUG(dbgs() << "DSE: Remove Dead Store:\n DEAD: "
2509 << *DeadLocWrapper.DefInst << "\n KILLER: "
2510 << *KillingLocWrapper.DefInst << '\n');
2511 deleteDeadInstruction(DeadLocWrapper.DefInst, &Deleted);
2512 ++NumFastStores;
2513 Changed = true;
2514 } else {
2515 // Check if DeadI overwrites KillingI.
2516 int64_t KillingOffset = 0;
2517 int64_t DeadOffset = 0;
2518 OverwriteResult OR =
2519 isOverwrite(KillingLocWrapper.DefInst, DeadLocWrapper.DefInst,
2520 KillingLocWrapper.MemLoc, DeadLocWrapper.MemLoc,
2521 KillingOffset, DeadOffset);
2522 if (OR == OW_MaybePartial) {
2523 auto &IOL = IOLs[DeadLocWrapper.DefInst->getParent()];
2524 OR = isPartialOverwrite(KillingLocWrapper.MemLoc, DeadLocWrapper.MemLoc,
2525 KillingOffset, DeadOffset,
2526 DeadLocWrapper.DefInst, IOL);
2527 }
2528 if (EnablePartialStoreMerging && OR == OW_PartialEarlierWithFullLater) {
2529 auto *DeadSI = dyn_cast<StoreInst>(DeadLocWrapper.DefInst);
2530 auto *KillingSI = dyn_cast<StoreInst>(KillingLocWrapper.DefInst);
2531 // We are re-using tryToMergePartialOverlappingStores, which requires
2532 // DeadSI to dominate KillingSI.
2533 // TODO: implement tryToMergeParialOverlappingStores using MemorySSA.
2534 if (DeadSI && KillingSI && DT.dominates(DeadSI, KillingSI)) {
2535 if (Constant *Merged = tryToMergePartialOverlappingStores(
2536 KillingSI, DeadSI, KillingOffset, DeadOffset, DL, BatchAA,
2537 &DT)) {
2538
2539 // Update stored value of earlier store to merged constant.
2540 DeadSI->setOperand(0, Merged);
2541 ++NumModifiedStores;
2542 Changed = true;
2543 DeletedKillingLoc = true;
2544
2545 // Remove killing store and remove any outstanding overlap
2546 // intervals for the updated store.
2547 deleteDeadInstruction(KillingSI, &Deleted);
2548 auto I = IOLs.find(DeadSI->getParent());
2549 if (I != IOLs.end())
2550 I->second.erase(DeadSI);
2551 break;
2552 }
2553 }
2554 }
2555 if (OR == OW_Complete) {
2556 LLVM_DEBUG(dbgs() << "DSE: Remove Dead Store:\n DEAD: "
2557 << *DeadLocWrapper.DefInst << "\n KILLER: "
2558 << *KillingLocWrapper.DefInst << '\n');
2559 deleteDeadInstruction(DeadLocWrapper.DefInst, &Deleted);
2560 ++NumFastStores;
2561 Changed = true;
2562 }
2563 }
2564 }
2565
2566 assert(SkipStores.size() - OrigNumSkipStores == Deleted.size() &&
2567 "SkipStores and Deleted out of sync?");
2568
2569 return {Changed, DeletedKillingLoc};
2570}
2571
2572bool DSEState::eliminateDeadDefs(const MemoryDefWrapper &KillingDefWrapper) {
2573 if (KillingDefWrapper.DefinedLocations.empty()) {
2574 LLVM_DEBUG(dbgs() << "Failed to find analyzable write location for "
2575 << *KillingDefWrapper.DefInst << "\n");
2576 return false;
2577 }
2578
2579 bool MadeChange = false;
2580 for (auto &KillingLocWrapper : KillingDefWrapper.DefinedLocations) {
2581 LLVM_DEBUG(dbgs() << "Trying to eliminate MemoryDefs killed by "
2582 << *KillingLocWrapper.MemDef << " ("
2583 << *KillingLocWrapper.DefInst << ")\n");
2584 auto [Changed, DeletedKillingLoc] = eliminateDeadDefs(KillingLocWrapper);
2585 MadeChange |= Changed;
2586
2587 // Check if the store is a no-op.
2588 if (!DeletedKillingLoc && storeIsNoop(KillingLocWrapper.MemDef,
2589 KillingLocWrapper.UnderlyingObject)) {
2590 LLVM_DEBUG(dbgs() << "DSE: Remove No-Op Store:\n DEAD: "
2591 << *KillingLocWrapper.DefInst << '\n');
2592 deleteDeadInstruction(KillingLocWrapper.DefInst);
2593 NumRedundantStores++;
2594 MadeChange = true;
2595 continue;
2596 }
2597 // Can we form a calloc from a memset/malloc pair?
2598 if (!DeletedKillingLoc &&
2599 tryFoldIntoCalloc(KillingLocWrapper.MemDef,
2600 KillingLocWrapper.UnderlyingObject)) {
2601 LLVM_DEBUG(dbgs() << "DSE: Remove memset after forming calloc:\n"
2602 << " DEAD: " << *KillingLocWrapper.DefInst << '\n');
2603 deleteDeadInstruction(KillingLocWrapper.DefInst);
2604 MadeChange = true;
2605 continue;
2606 }
2607 }
2608 return MadeChange;
2609}
2610
2613 const TargetLibraryInfo &TLI,
2614 const LoopInfo &LI) {
2615 bool MadeChange = false;
2616 DSEState State(F, AA, MSSA, DT, PDT, TLI, LI);
2617 // For each store:
2618 for (unsigned I = 0; I < State.MemDefs.size(); I++) {
2619 MemoryDef *KillingDef = State.MemDefs[I];
2620 if (State.SkipStores.count(KillingDef))
2621 continue;
2622
2623 MemoryDefWrapper KillingDefWrapper(
2624 KillingDef, State.getLocForInst(KillingDef->getMemoryInst(),
2626 MadeChange |= State.eliminateDeadDefs(KillingDefWrapper);
2627 }
2628
2630 for (auto &KV : State.IOLs)
2631 MadeChange |= State.removePartiallyOverlappedStores(KV.second);
2632
2633 MadeChange |= State.eliminateRedundantStoresOfExistingValues();
2634 MadeChange |= State.eliminateDeadWritesAtEndOfFunction();
2635
2636 while (!State.ToRemove.empty()) {
2637 Instruction *DeadInst = State.ToRemove.pop_back_val();
2638 DeadInst->eraseFromParent();
2639 }
2640
2641 return MadeChange;
2642}
2643
2644//===----------------------------------------------------------------------===//
2645// DSE Pass
2646//===----------------------------------------------------------------------===//
2651 MemorySSA &MSSA = AM.getResult<MemorySSAAnalysis>(F).getMSSA();
2653 LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
2654
2655 bool Changed = eliminateDeadStores(F, AA, MSSA, DT, PDT, TLI, LI);
2656
2657#ifdef LLVM_ENABLE_STATS
2659 for (auto &I : instructions(F))
2660 NumRemainingStores += isa<StoreInst>(&I);
2661#endif
2662
2663 if (!Changed)
2664 return PreservedAnalyses::all();
2665
2669 PA.preserve<LoopAnalysis>();
2670 return PA;
2671}
2672
2673namespace {
2674
2675/// A legacy pass for the legacy pass manager that wraps \c DSEPass.
2676class DSELegacyPass : public FunctionPass {
2677public:
2678 static char ID; // Pass identification, replacement for typeid
2679
2680 DSELegacyPass() : FunctionPass(ID) {
2682 }
2683
2684 bool runOnFunction(Function &F) override {
2685 if (skipFunction(F))
2686 return false;
2687
2688 AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
2689 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2690 const TargetLibraryInfo &TLI =
2691 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
2692 MemorySSA &MSSA = getAnalysis<MemorySSAWrapperPass>().getMSSA();
2693 PostDominatorTree &PDT =
2694 getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
2695 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
2696
2697 bool Changed = eliminateDeadStores(F, AA, MSSA, DT, PDT, TLI, LI);
2698
2699#ifdef LLVM_ENABLE_STATS
2701 for (auto &I : instructions(F))
2702 NumRemainingStores += isa<StoreInst>(&I);
2703#endif
2704
2705 return Changed;
2706 }
2707
2708 void getAnalysisUsage(AnalysisUsage &AU) const override {
2709 AU.setPreservesCFG();
2710 AU.addRequired<AAResultsWrapperPass>();
2711 AU.addRequired<TargetLibraryInfoWrapperPass>();
2712 AU.addPreserved<GlobalsAAWrapperPass>();
2713 AU.addRequired<DominatorTreeWrapperPass>();
2714 AU.addPreserved<DominatorTreeWrapperPass>();
2715 AU.addRequired<PostDominatorTreeWrapperPass>();
2716 AU.addRequired<MemorySSAWrapperPass>();
2717 AU.addPreserved<PostDominatorTreeWrapperPass>();
2718 AU.addPreserved<MemorySSAWrapperPass>();
2719 AU.addRequired<LoopInfoWrapperPass>();
2720 AU.addPreserved<LoopInfoWrapperPass>();
2721 AU.addRequired<AssumptionCacheTracker>();
2722 }
2723};
2724
2725} // end anonymous namespace
2726
2727char DSELegacyPass::ID = 0;
2728
2729INITIALIZE_PASS_BEGIN(DSELegacyPass, "dse", "Dead Store Elimination", false,
2730 false)
2740INITIALIZE_PASS_END(DSELegacyPass, "dse", "Dead Store Elimination", false,
2741 false)
2742
2744 return new DSELegacyPass();
2745}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Lower Kernel Arguments
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:213
This file contains the declarations for the subclasses of Constant, which represent the different fla...
MapVector< Instruction *, OverlapIntervalsTy > InstOverlapIntervalsTy
static bool canSkipDef(MemoryDef *D, bool DefVisibleToCaller)
static cl::opt< bool > EnableInitializesImprovement("enable-dse-initializes-attr-improvement", cl::init(true), cl::Hidden, cl::desc("Enable the initializes attr improvement in DSE"))
static void shortenAssignment(Instruction *Inst, Value *OriginalDest, uint64_t OldOffsetInBits, uint64_t OldSizeInBits, uint64_t NewSizeInBits, bool IsOverwriteEnd)
static bool isShortenableAtTheEnd(Instruction *I)
Returns true if the end of this instruction can be safely shortened in length.
static bool isNoopIntrinsic(Instruction *I)
static ConstantRangeList getIntersectedInitRangeList(ArrayRef< ArgumentInitInfo > Args, bool CallHasNoUnwindAttr)
static cl::opt< bool > EnablePartialStoreMerging("enable-dse-partial-store-merging", cl::init(true), cl::Hidden, cl::desc("Enable partial store merging in DSE"))
static bool tryToShortenBegin(Instruction *DeadI, OverlapIntervalsTy &IntervalMap, int64_t &DeadStart, uint64_t &DeadSize)
std::map< int64_t, int64_t > OverlapIntervalsTy
static bool isShortenableAtTheBeginning(Instruction *I)
Returns true if the beginning of this instruction can be safely shortened in length.
static cl::opt< unsigned > MemorySSADefsPerBlockLimit("dse-memoryssa-defs-per-block-limit", cl::init(5000), cl::Hidden, cl::desc("The number of MemoryDefs we consider as candidates to eliminated " "other stores per basic block (default = 5000)"))
static Constant * tryToMergePartialOverlappingStores(StoreInst *KillingI, StoreInst *DeadI, int64_t KillingOffset, int64_t DeadOffset, const DataLayout &DL, BatchAAResults &AA, DominatorTree *DT)
static bool memoryIsNotModifiedBetween(Instruction *FirstI, Instruction *SecondI, BatchAAResults &AA, const DataLayout &DL, DominatorTree *DT)
Returns true if the memory which is accessed by the second instruction is not modified between the fi...
static OverwriteResult isMaskedStoreOverwrite(const Instruction *KillingI, const Instruction *DeadI, BatchAAResults &AA)
Check if two instruction are masked stores that completely overwrite one another.
static cl::opt< unsigned > MemorySSAOtherBBStepCost("dse-memoryssa-otherbb-cost", cl::init(5), cl::Hidden, cl::desc("The cost of a step in a different basic " "block than the killing MemoryDef" "(default = 5)"))
static bool tryToShorten(Instruction *DeadI, int64_t &DeadStart, uint64_t &DeadSize, int64_t KillingStart, uint64_t KillingSize, bool IsOverwriteEnd)
static cl::opt< unsigned > MemorySSAScanLimit("dse-memoryssa-scanlimit", cl::init(150), cl::Hidden, cl::desc("The number of memory instructions to scan for " "dead store elimination (default = 150)"))
static bool isFuncLocalAndNotCaptured(Value *Arg, const CallBase *CB, EarliestEscapeAnalysis &EA)
static cl::opt< unsigned > MemorySSASameBBStepCost("dse-memoryssa-samebb-cost", cl::init(1), cl::Hidden, cl::desc("The cost of a step in the same basic block as the killing MemoryDef" "(default = 1)"))
static cl::opt< bool > EnablePartialOverwriteTracking("enable-dse-partial-overwrite-tracking", cl::init(true), cl::Hidden, cl::desc("Enable partial-overwrite tracking in DSE"))
static OverwriteResult isPartialOverwrite(const MemoryLocation &KillingLoc, const MemoryLocation &DeadLoc, int64_t KillingOff, int64_t DeadOff, Instruction *DeadI, InstOverlapIntervalsTy &IOL)
Return 'OW_Complete' if a store to the 'KillingLoc' location completely overwrites a store to the 'De...
static cl::opt< unsigned > MemorySSAPartialStoreLimit("dse-memoryssa-partial-store-limit", cl::init(5), cl::Hidden, cl::desc("The maximum number candidates that only partially overwrite the " "killing MemoryDef to consider" " (default = 5)"))
static std::optional< TypeSize > getPointerSize(const Value *V, const DataLayout &DL, const TargetLibraryInfo &TLI, const Function *F)
static bool tryToShortenEnd(Instruction *DeadI, OverlapIntervalsTy &IntervalMap, int64_t &DeadStart, uint64_t &DeadSize)
static void adjustArgAttributes(AnyMemIntrinsic *Intrinsic, unsigned ArgNo, uint64_t PtrOffset)
Update the attributes given that a memory access is updated (the dereferenced pointer could be moved ...
static cl::opt< unsigned > MemorySSAUpwardsStepLimit("dse-memoryssa-walklimit", cl::init(90), cl::Hidden, cl::desc("The maximum number of steps while walking upwards to find " "MemoryDefs that may be killed (default = 90)"))
static cl::opt< bool > OptimizeMemorySSA("dse-optimize-memoryssa", cl::init(true), cl::Hidden, cl::desc("Allow DSE to optimize memory accesses."))
static bool hasInitializesAttr(Instruction *I)
static cl::opt< unsigned > MemorySSAPathCheckLimit("dse-memoryssa-path-check-limit", cl::init(50), cl::Hidden, cl::desc("The maximum number of blocks to check when trying to prove that " "all paths to an exit go through a killing block (default = 50)"))
static bool eliminateDeadStores(Function &F, AliasAnalysis &AA, MemorySSA &MSSA, DominatorTree &DT, PostDominatorTree &PDT, const TargetLibraryInfo &TLI, const LoopInfo &LI)
This file provides an implementation of debug counters.
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC)
This file defines the DenseMap class.
early cse Early CSE w MemorySSA
static bool runOnFunction(Function &F, bool PostInlining)
This is the interface for a simple mod/ref and alias analysis over globals.
Hexagon Common GEP
#define _
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
static void deleteDeadInstruction(Instruction *I)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file implements a map that provides insertion order iteration.
This file provides utility analysis objects describing memory locations.
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
Contains a collection of routines for determining if a given instruction is guaranteed to execute if ...
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
if(PassOpts->AAPipeline)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
A manager for alias analyses.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1023
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition APInt.h:259
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1497
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1571
@ NoAlias
The two locations do not alias at all.
@ PartialAlias
The two locations alias, but only due to a partial overlap.
@ MustAlias
The two locations precisely alias each other.
constexpr int32_t getOffset() const
constexpr bool hasOffset() const
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:270
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
An immutable pass that tracks lazily created AssumptionCache objects.
This class stores enough information to efficiently remove some attributes from an existing AttrBuild...
AttributeMask & addAttribute(Attribute::AttrKind Val)
Add an attribute to the mask.
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:402
LLVM_ABI ArrayRef< ConstantRange > getValueAsConstantRangeList() const
Return the attribute's value as a ConstantRange array.
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:259
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
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.h:233
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
void setCallingConv(CallingConv::ID CC)
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Get the attribute of a given kind from a given arg.
bool isByValArgument(unsigned ArgNo) const
Determine whether this argument is passed by value.
LLVM_ABI bool onlyAccessesInaccessibleMemOrArgMem() const
Determine if the function may only access memory that is either inaccessible from the IR or pointed t...
bool doesNotThrow() const
Determine if the call cannot unwind.
Value * getArgOperand(unsigned i) const
LLVM_ABI Value * getArgOperandWithAttribute(Attribute::AttrKind Kind) const
If one of the arguments has the specified attribute, returns its operand value.
unsigned arg_size() const
This class represents a list of constant ranges.
bool empty() const
Return true if this list contains no members.
LLVM_ABI ConstantRangeList intersectWith(const ConstantRangeList &CRL) const
Return the range list that results from the intersection of this ConstantRangeList with another Const...
const APInt & getLower() const
Return the lower value for this range.
const APInt & getUpper() const
Return the upper value for this range.
This is an important base class in LLVM.
Definition Constant.h:43
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constants.cpp:90
static DIAssignID * getDistinct(LLVMContext &Context)
DbgVariableFragmentInfo FragmentInfo
static LLVM_ABI std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Record of a variable value-assignment, aka a non instruction representation of the dbg....
static bool shouldExecute(CounterInfo &Counter)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:241
DomTreeNodeBase * getIDom() const
NodeT * getBlock() const
Analysis pass which computes a DominatorTree.
Definition Dominators.h:283
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:321
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:164
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Context-sensitive CaptureAnalysis provider, which computes and caches the earliest common dominator c...
CaptureComponents getCapturesBefore(const Value *Object, const Instruction *I, bool OrAt) override
Return how Object may be captured before instruction I, considering only provenance captures.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
const BasicBlock & getEntryBlock() const
Definition Function.h:813
static GetElementPtrInst * CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Create an "inbounds" getelementptr.
Legacy wrapper pass to provide the GlobalsAAResult object.
bool isEquality() const
Return true if this predicate is either EQ or NE.
LLVM_ABI bool mayThrow(bool IncludePhaseOneUnwind=false) const LLVM_READONLY
Return true if this instruction may throw an exception.
LLVM_ABI bool mayWriteToMemory() const LLVM_READONLY
Return true if this instruction may modify memory.
LLVM_ABI bool isAtomic() const LLVM_READONLY
Return true if this instruction has an AtomicOrdering of unordered or higher.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI bool isIdenticalToWhenDefined(const Instruction *I, bool IntersectAttrs=false) const LLVM_READONLY
This is like isIdenticalTo, except that it ignores the SubclassOptionalData flags,...
LLVM_ABI bool mayReadFromMemory() const LLVM_READONLY
Return true if this instruction may read memory.
LLVM_ABI AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
const_iterator begin() const
bool empty() const
empty - Return true when no intervals are mapped.
const_iterator end() const
A wrapper class for inspecting calls to intrinsic functions.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
static LocationSize precise(uint64_t Value)
bool isScalable() const
TypeSize getValue() const
bool isPrecise() const
Analysis pass that exposes the LoopInfo for a function.
Definition LoopInfo.h:569
The legacy pass manager's analysis pass to compute loop information.
Definition LoopInfo.h:596
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1572
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:36
iterator end()
Definition MapVector.h:67
iterator find(const KeyT &Key)
Definition MapVector.h:154
Value * getLength() const
Value * getValue() const
BasicBlock * getBlock() const
Definition MemorySSA.h:162
Represents a read-write access to memory, whether it is a must-alias, or a may-alias.
Definition MemorySSA.h:371
void setOptimized(MemoryAccess *MA)
Definition MemorySSA.h:392
A wrapper analysis pass for the legacy pass manager that exposes a MemoryDepnedenceResults instance.
Representation for a specific memory location.
static LLVM_ABI MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
LocationSize Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known.
static MemoryLocation getBeforeOrAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location before or after Ptr, while remaining within the underl...
static MemoryLocation getAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location after Ptr, while remaining within the underlying objec...
MemoryLocation getWithNewPtr(const Value *NewPtr) const
const Value * Ptr
The address of the start of the location.
static LLVM_ABI MemoryLocation getForDest(const MemIntrinsic *MI)
Return a location representing the destination of a memory set or transfer.
static LLVM_ABI std::optional< MemoryLocation > getOrNone(const Instruction *Inst)
static LLVM_ABI MemoryLocation getForArgument(const CallBase *Call, unsigned ArgIdx, const TargetLibraryInfo *TLI)
Return a location representing a particular argument of a call.
An analysis that produces MemorySSA for a function.
Definition MemorySSA.h:936
Legacy analysis pass which computes MemorySSA.
Definition MemorySSA.h:993
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition MemorySSA.h:702
MemoryAccess * getDefiningAccess() const
Get the access that produces the memory state used by this Use.
Definition MemorySSA.h:260
Instruction * getMemoryInst() const
Get the instruction that this MemoryUse represents.
Definition MemorySSA.h:257
PHITransAddr - An address value which tracks and handles phi translation.
LLVM_ABI Value * translateValue(BasicBlock *CurBB, BasicBlock *PredBB, const DominatorTree *DT, bool MustDominate)
translateValue - PHI translate the current address up the CFG from CurBB to Pred, updating our state ...
LLVM_ABI bool isPotentiallyPHITranslatable() const
isPotentiallyPHITranslatable - If this needs PHI translation, return true if we have some hope of doi...
bool needsPHITranslationFromBlock(BasicBlock *BB) const
needsPHITranslationFromBlock - Return true if moving from the specified BasicBlock to its predecessor...
Value * getAddr() const
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Analysis pass which computes a PostDominatorTree.
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:103
void insert_range(Range &&R)
Definition SetVector.h:176
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:151
size_type size() const
Definition SmallPtrSet.h:99
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
iterator begin() const
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
Value * getValueOperand()
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:294
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
op_range operands()
Definition User.h:267
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:259
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:708
iterator_range< use_iterator > uses()
Definition Value.h:380
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract Attribute helper functions.
Definition Attributor.h:165
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
This namespace contains an enum with a value for every intrinsic/builtin function known by LLVM.
bool match(Val *V, const Pattern &P)
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
OneOps_match< OpTy, Instruction::Load > m_Load(const OpTy &Op)
Matches LoadInst.
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
SmallVector< DbgVariableRecord * > getDVRAssignmentMarkers(const Instruction *Inst)
Return a range of dbg_assign records for which Inst performs the assignment they encode.
Definition DebugInfo.h:195
LLVM_ABI bool calculateFragmentIntersect(const DataLayout &DL, const Value *Dest, uint64_t SliceOffsetInBits, uint64_t SliceSizeInBits, const DbgVariableRecord *DVRAssign, std::optional< DIExpression::FragmentInfo > &Result)
Calculate the fragment of the variable in DAI covered from (Dest + SliceOffsetInBits) to to (Dest + S...
initializer< Ty > init(const Ty &Val)
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
LLVM_ABI void initializeDSELegacyPassPass(PassRegistry &)
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
LLVM_ABI Constant * getInitialValueOfAllocation(const Value *V, const TargetLibraryInfo *TLI, Type *Ty)
If this is a call to an allocation function that initializes memory to a fixed value,...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
bool isStrongerThanMonotonic(AtomicOrdering AO)
@ Uninitialized
Definition Threading.h:60
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
Definition Alignment.h:134
AllocFnKind
Definition Attributes.h:51
LLVM_ABI void salvageDebugInfo(const MachineRegisterInfo &MRI, MachineInstr &MI)
Assuming the instruction MI is going to be deleted, attempt to salvage debug users of MI by writing t...
Definition Utils.cpp:1731
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.
iterator_range< po_iterator< T > > post_order(const T &G)
LLVM_ABI bool isNoAliasCall(const Value *V)
Return true if this pointer is returned by a noalias function.
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition Dominators.h:94
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1744
LLVM_ABI bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition Local.cpp:402
LLVM_ABI 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.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:406
bool isModSet(const ModRefInfo MRI)
Definition ModRef.h:49
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI bool AreStatisticsEnabled()
Check if statistics are enabled.
LLVM_ABI bool isNotVisibleOnUnwind(const Value *Object, bool &RequiresNoCaptureBeforeUnwind)
Return true if Object memory is not visible after an unwind, in the sense that program semantics cann...
LLVM_ABI Value * emitCalloc(Value *Num, Value *Size, IRBuilderBase &B, const TargetLibraryInfo &TLI, unsigned AddrSpace)
Emit a call to the calloc function.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Definition Alignment.h:186
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
LLVM_ABI bool salvageKnowledge(Instruction *I, AssumptionCache *AC=nullptr, DominatorTree *DT=nullptr)
Calls BuildAssumeFromInst and if the resulting llvm.assume is valid insert if before I.
LLVM_ABI bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures, unsigned MaxUsesToExplore=0)
PointerMayBeCaptured - Return true if this pointer value may be captured by the enclosing function (w...
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI Value * getFreedOperand(const CallBase *CB, const TargetLibraryInfo *TLI)
If this if a call to a free function, return the freed operand.
LLVM_ABI bool isIdentifiedFunctionLocal(const Value *V)
Return true if V is umabigously identified at the function-level.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI FunctionPass * createDeadStoreEliminationPass()
LLVM_ABI Value * isBytewiseValue(Value *V, const DataLayout &DL)
If the specified value can be set by repeating the same byte in memory, return the i8 value that it i...
auto predecessors(const MachineBasicBlock *BB)
bool capturesAnything(CaptureComponents CC)
Definition ModRef.h:324
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI bool mayContainIrreducibleControl(const Function &F, const LoopInfo *LI)
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
AAResults AliasAnalysis
Temporary typedef for legacy code that uses a generic AliasAnalysis pointer or reference.
bool capturesNothing(CaptureComponents CC)
Definition ModRef.h:320
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
bool isStrongerThan(AtomicOrdering AO, AtomicOrdering Other)
Returns true if ao is stronger than other as defined by the AtomicOrdering lattice,...
bool isRefSet(const ModRefInfo MRI)
Definition ModRef.h:52
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
Various options to control the behavior of getObjectSize.
bool NullIsUnknownSize
If this is true, null pointers in address space 0 will be treated as though they can't be evaluated.