LLVM 21.0.0git
MemCpyOptimizer.cpp
Go to the documentation of this file.
1//===- MemCpyOptimizer.cpp - Optimize use of memcpy and friends -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass performs various transformations related to eliminating memcpy
10// calls, or transforming sets of stores into memset's.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/DenseSet.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/ScopeExit.h"
19#include "llvm/ADT/Statistic.h"
23#include "llvm/Analysis/CFG.h"
27#include "llvm/Analysis/Loads.h"
34#include "llvm/IR/BasicBlock.h"
35#include "llvm/IR/Constants.h"
36#include "llvm/IR/DataLayout.h"
38#include "llvm/IR/Dominators.h"
39#include "llvm/IR/Function.h"
41#include "llvm/IR/IRBuilder.h"
42#include "llvm/IR/InstrTypes.h"
43#include "llvm/IR/Instruction.h"
46#include "llvm/IR/Intrinsics.h"
47#include "llvm/IR/LLVMContext.h"
48#include "llvm/IR/Module.h"
49#include "llvm/IR/PassManager.h"
50#include "llvm/IR/Type.h"
51#include "llvm/IR/User.h"
52#include "llvm/IR/Value.h"
54#include "llvm/Support/Debug.h"
57#include <algorithm>
58#include <cassert>
59#include <cstdint>
60#include <optional>
61
62using namespace llvm;
63
64#define DEBUG_TYPE "memcpyopt"
65
67 "enable-memcpyopt-without-libcalls", cl::Hidden,
68 cl::desc("Enable memcpyopt even when libcalls are disabled"));
69
70STATISTIC(NumMemCpyInstr, "Number of memcpy instructions deleted");
71STATISTIC(NumMemMoveInstr, "Number of memmove instructions deleted");
72STATISTIC(NumMemSetInfer, "Number of memsets inferred");
73STATISTIC(NumMoveToCpy, "Number of memmoves converted to memcpy");
74STATISTIC(NumCpyToSet, "Number of memcpys converted to memset");
75STATISTIC(NumCallSlot, "Number of call slot optimizations performed");
76STATISTIC(NumStackMove, "Number of stack-move optimizations performed");
77
78namespace {
79
80/// Represents a range of memset'd bytes with the ByteVal value.
81/// This allows us to analyze stores like:
82/// store 0 -> P+1
83/// store 0 -> P+0
84/// store 0 -> P+3
85/// store 0 -> P+2
86/// which sometimes happens with stores to arrays of structs etc. When we see
87/// the first store, we make a range [1, 2). The second store extends the range
88/// to [0, 2). The third makes a new range [2, 3). The fourth store joins the
89/// two ranges into [0, 3) which is memset'able.
90struct MemsetRange {
91 // Start/End - A semi range that describes the span that this range covers.
92 // The range is closed at the start and open at the end: [Start, End).
93 int64_t Start, End;
94
95 /// StartPtr - The getelementptr instruction that points to the start of the
96 /// range.
97 Value *StartPtr;
98
99 /// Alignment - The known alignment of the first store.
100 MaybeAlign Alignment;
101
102 /// TheStores - The actual stores that make up this range.
104
105 bool isProfitableToUseMemset(const DataLayout &DL) const;
106};
107
108} // end anonymous namespace
109
110bool MemsetRange::isProfitableToUseMemset(const DataLayout &DL) const {
111 // If we found more than 4 stores to merge or 16 bytes, use memset.
112 if (TheStores.size() >= 4 || End - Start >= 16)
113 return true;
114
115 // If there is nothing to merge, don't do anything.
116 if (TheStores.size() < 2)
117 return false;
118
119 // If any of the stores are a memset, then it is always good to extend the
120 // memset.
121 for (Instruction *SI : TheStores)
122 if (!isa<StoreInst>(SI))
123 return true;
124
125 // Assume that the code generator is capable of merging pairs of stores
126 // together if it wants to.
127 if (TheStores.size() == 2)
128 return false;
129
130 // If we have fewer than 8 stores, it can still be worthwhile to do this.
131 // For example, merging 4 i8 stores into an i32 store is useful almost always.
132 // However, merging 2 32-bit stores isn't useful on a 32-bit architecture (the
133 // memset will be split into 2 32-bit stores anyway) and doing so can
134 // pessimize the llvm optimizer.
135 //
136 // Since we don't have perfect knowledge here, make some assumptions: assume
137 // the maximum GPR width is the same size as the largest legal integer
138 // size. If so, check to see whether we will end up actually reducing the
139 // number of stores used.
140 unsigned Bytes = unsigned(End - Start);
141 unsigned MaxIntSize = DL.getLargestLegalIntTypeSizeInBits() / 8;
142 if (MaxIntSize == 0)
143 MaxIntSize = 1;
144 unsigned NumPointerStores = Bytes / MaxIntSize;
145
146 // Assume the remaining bytes if any are done a byte at a time.
147 unsigned NumByteStores = Bytes % MaxIntSize;
148
149 // If we will reduce the # stores (according to this heuristic), do the
150 // transformation. This encourages merging 4 x i8 -> i32 and 2 x i16 -> i32
151 // etc.
152 return TheStores.size() > NumPointerStores + NumByteStores;
153}
154
155namespace {
156
157class MemsetRanges {
158 using range_iterator = SmallVectorImpl<MemsetRange>::iterator;
159
160 /// A sorted list of the memset ranges.
162
163 const DataLayout &DL;
164
165public:
166 MemsetRanges(const DataLayout &DL) : DL(DL) {}
167
169
170 const_iterator begin() const { return Ranges.begin(); }
171 const_iterator end() const { return Ranges.end(); }
172 bool empty() const { return Ranges.empty(); }
173
174 void addInst(int64_t OffsetFromFirst, Instruction *Inst) {
175 if (auto *SI = dyn_cast<StoreInst>(Inst))
176 addStore(OffsetFromFirst, SI);
177 else
178 addMemSet(OffsetFromFirst, cast<MemSetInst>(Inst));
179 }
180
181 void addStore(int64_t OffsetFromFirst, StoreInst *SI) {
182 TypeSize StoreSize = DL.getTypeStoreSize(SI->getOperand(0)->getType());
183 assert(!StoreSize.isScalable() && "Can't track scalable-typed stores");
184 addRange(OffsetFromFirst, StoreSize.getFixedValue(),
185 SI->getPointerOperand(), SI->getAlign(), SI);
186 }
187
188 void addMemSet(int64_t OffsetFromFirst, MemSetInst *MSI) {
189 int64_t Size = cast<ConstantInt>(MSI->getLength())->getZExtValue();
190 addRange(OffsetFromFirst, Size, MSI->getDest(), MSI->getDestAlign(), MSI);
191 }
192
193 void addRange(int64_t Start, int64_t Size, Value *Ptr, MaybeAlign Alignment,
194 Instruction *Inst);
195};
196
197} // end anonymous namespace
198
199/// Add a new store to the MemsetRanges data structure. This adds a
200/// new range for the specified store at the specified offset, merging into
201/// existing ranges as appropriate.
202void MemsetRanges::addRange(int64_t Start, int64_t Size, Value *Ptr,
203 MaybeAlign Alignment, Instruction *Inst) {
204 int64_t End = Start + Size;
205
206 range_iterator I = partition_point(
207 Ranges, [=](const MemsetRange &O) { return O.End < Start; });
208
209 // We now know that I == E, in which case we didn't find anything to merge
210 // with, or that Start <= I->End. If End < I->Start or I == E, then we need
211 // to insert a new range. Handle this now.
212 if (I == Ranges.end() || End < I->Start) {
213 MemsetRange &R = *Ranges.insert(I, MemsetRange());
214 R.Start = Start;
215 R.End = End;
216 R.StartPtr = Ptr;
217 R.Alignment = Alignment;
218 R.TheStores.push_back(Inst);
219 return;
220 }
221
222 // This store overlaps with I, add it.
223 I->TheStores.push_back(Inst);
224
225 // At this point, we may have an interval that completely contains our store.
226 // If so, just add it to the interval and return.
227 if (I->Start <= Start && I->End >= End)
228 return;
229
230 // Now we know that Start <= I->End and End >= I->Start so the range overlaps
231 // but is not entirely contained within the range.
232
233 // See if the range extends the start of the range. In this case, it couldn't
234 // possibly cause it to join the prior range, because otherwise we would have
235 // stopped on *it*.
236 if (Start < I->Start) {
237 I->Start = Start;
238 I->StartPtr = Ptr;
239 I->Alignment = Alignment;
240 }
241
242 // Now we know that Start <= I->End and Start >= I->Start (so the startpoint
243 // is in or right at the end of I), and that End >= I->Start. Extend I out to
244 // End.
245 if (End > I->End) {
246 I->End = End;
247 range_iterator NextI = I;
248 while (++NextI != Ranges.end() && End >= NextI->Start) {
249 // Merge the range in.
250 I->TheStores.append(NextI->TheStores.begin(), NextI->TheStores.end());
251 if (NextI->End > I->End)
252 I->End = NextI->End;
253 Ranges.erase(NextI);
254 NextI = I;
255 }
256 }
257}
258
259//===----------------------------------------------------------------------===//
260// MemCpyOptLegacyPass Pass
261//===----------------------------------------------------------------------===//
262
263// Check that V is either not accessible by the caller, or unwinding cannot
264// occur between Start and End.
266 Instruction *End) {
267 assert(Start->getParent() == End->getParent() && "Must be in same block");
268 // Function can't unwind, so it also can't be visible through unwinding.
269 if (Start->getFunction()->doesNotThrow())
270 return false;
271
272 // Object is not visible on unwind.
273 // TODO: Support RequiresNoCaptureBeforeUnwind case.
274 bool RequiresNoCaptureBeforeUnwind;
276 RequiresNoCaptureBeforeUnwind) &&
277 !RequiresNoCaptureBeforeUnwind)
278 return false;
279
280 // Check whether there are any unwinding instructions in the range.
281 return any_of(make_range(Start->getIterator(), End->getIterator()),
282 [](const Instruction &I) { return I.mayThrow(); });
283}
284
285void MemCpyOptPass::eraseInstruction(Instruction *I) {
286 MSSAU->removeMemoryAccess(I);
287 EEA->removeInstruction(I);
288 I->eraseFromParent();
289}
290
291// Check for mod or ref of Loc between Start and End, excluding both boundaries.
292// Start and End must be in the same block.
293// If SkippedLifetimeStart is provided, skip over one clobbering lifetime.start
294// intrinsic and store it inside SkippedLifetimeStart.
296 const MemoryUseOrDef *Start,
297 const MemoryUseOrDef *End,
298 Instruction **SkippedLifetimeStart = nullptr) {
299 assert(Start->getBlock() == End->getBlock() && "Only local supported");
300 for (const MemoryAccess &MA :
301 make_range(++Start->getIterator(), End->getIterator())) {
302 Instruction *I = cast<MemoryUseOrDef>(MA).getMemoryInst();
303 if (isModOrRefSet(AA.getModRefInfo(I, Loc))) {
304 auto *II = dyn_cast<IntrinsicInst>(I);
305 if (II && II->getIntrinsicID() == Intrinsic::lifetime_start &&
306 SkippedLifetimeStart && !*SkippedLifetimeStart) {
307 *SkippedLifetimeStart = I;
308 continue;
309 }
310
311 return true;
312 }
313 }
314 return false;
315}
316
317// Check for mod of Loc between Start and End, excluding both boundaries.
318// Start and End can be in different blocks.
320 MemoryLocation Loc, const MemoryUseOrDef *Start,
321 const MemoryUseOrDef *End) {
322 if (isa<MemoryUse>(End)) {
323 // For MemoryUses, getClobberingMemoryAccess may skip non-clobbering writes.
324 // Manually check read accesses between Start and End, if they are in the
325 // same block, for clobbers. Otherwise assume Loc is clobbered.
326 return Start->getBlock() != End->getBlock() ||
327 any_of(
328 make_range(std::next(Start->getIterator()), End->getIterator()),
329 [&AA, Loc](const MemoryAccess &Acc) {
330 if (isa<MemoryUse>(&Acc))
331 return false;
332 Instruction *AccInst =
333 cast<MemoryUseOrDef>(&Acc)->getMemoryInst();
334 return isModSet(AA.getModRefInfo(AccInst, Loc));
335 });
336 }
337
338 // TODO: Only walk until we hit Start.
340 End->getDefiningAccess(), Loc, AA);
341 return !MSSA->dominates(Clobber, Start);
342}
343
344/// When scanning forward over instructions, we look for some other patterns to
345/// fold away. In particular, this looks for stores to neighboring locations of
346/// memory. If it sees enough consecutive ones, it attempts to merge them
347/// together into a memcpy/memset.
348Instruction *MemCpyOptPass::tryMergingIntoMemset(Instruction *StartInst,
349 Value *StartPtr,
350 Value *ByteVal) {
351 const DataLayout &DL = StartInst->getDataLayout();
352
353 // We can't track scalable types
354 if (auto *SI = dyn_cast<StoreInst>(StartInst))
355 if (DL.getTypeStoreSize(SI->getOperand(0)->getType()).isScalable())
356 return nullptr;
357
358 // Okay, so we now have a single store that can be splatable. Scan to find
359 // all subsequent stores of the same value to offset from the same pointer.
360 // Join these together into ranges, so we can decide whether contiguous blocks
361 // are stored.
362 MemsetRanges Ranges(DL);
363
364 BasicBlock::iterator BI(StartInst);
365
366 // Keeps track of the last memory use or def before the insertion point for
367 // the new memset. The new MemoryDef for the inserted memsets will be inserted
368 // after MemInsertPoint.
369 MemoryUseOrDef *MemInsertPoint = nullptr;
370 for (++BI; !BI->isTerminator(); ++BI) {
371 auto *CurrentAcc =
372 cast_or_null<MemoryUseOrDef>(MSSA->getMemoryAccess(&*BI));
373 if (CurrentAcc)
374 MemInsertPoint = CurrentAcc;
375
376 // Calls that only access inaccessible memory do not block merging
377 // accessible stores.
378 if (auto *CB = dyn_cast<CallBase>(BI)) {
379 if (CB->onlyAccessesInaccessibleMemory())
380 continue;
381 }
382
383 if (!isa<StoreInst>(BI) && !isa<MemSetInst>(BI)) {
384 // If the instruction is readnone, ignore it, otherwise bail out. We
385 // don't even allow readonly here because we don't want something like:
386 // A[1] = 2; strlen(A); A[2] = 2; -> memcpy(A, ...); strlen(A).
387 if (BI->mayWriteToMemory() || BI->mayReadFromMemory())
388 break;
389 continue;
390 }
391
392 if (auto *NextStore = dyn_cast<StoreInst>(BI)) {
393 // If this is a store, see if we can merge it in.
394 if (!NextStore->isSimple())
395 break;
396
397 Value *StoredVal = NextStore->getValueOperand();
398
399 // Don't convert stores of non-integral pointer types to memsets (which
400 // stores integers).
401 if (DL.isNonIntegralPointerType(StoredVal->getType()->getScalarType()))
402 break;
403
404 // We can't track ranges involving scalable types.
405 if (DL.getTypeStoreSize(StoredVal->getType()).isScalable())
406 break;
407
408 // Check to see if this stored value is of the same byte-splattable value.
409 Value *StoredByte = isBytewiseValue(StoredVal, DL);
410 if (isa<UndefValue>(ByteVal) && StoredByte)
411 ByteVal = StoredByte;
412 if (ByteVal != StoredByte)
413 break;
414
415 // Check to see if this store is to a constant offset from the start ptr.
416 std::optional<int64_t> Offset =
417 NextStore->getPointerOperand()->getPointerOffsetFrom(StartPtr, DL);
418 if (!Offset)
419 break;
420
421 Ranges.addStore(*Offset, NextStore);
422 } else {
423 auto *MSI = cast<MemSetInst>(BI);
424
425 if (MSI->isVolatile() || ByteVal != MSI->getValue() ||
426 !isa<ConstantInt>(MSI->getLength()))
427 break;
428
429 // Check to see if this store is to a constant offset from the start ptr.
430 std::optional<int64_t> Offset =
431 MSI->getDest()->getPointerOffsetFrom(StartPtr, DL);
432 if (!Offset)
433 break;
434
435 Ranges.addMemSet(*Offset, MSI);
436 }
437 }
438
439 // If we have no ranges, then we just had a single store with nothing that
440 // could be merged in. This is a very common case of course.
441 if (Ranges.empty())
442 return nullptr;
443
444 // If we had at least one store that could be merged in, add the starting
445 // store as well. We try to avoid this unless there is at least something
446 // interesting as a small compile-time optimization.
447 Ranges.addInst(0, StartInst);
448
449 // If we create any memsets, we put it right before the first instruction that
450 // isn't part of the memset block. This ensure that the memset is dominated
451 // by any addressing instruction needed by the start of the block.
452 IRBuilder<> Builder(&*BI);
453
454 // Now that we have full information about ranges, loop over the ranges and
455 // emit memset's for anything big enough to be worthwhile.
456 Instruction *AMemSet = nullptr;
457 for (const MemsetRange &Range : Ranges) {
458 if (Range.TheStores.size() == 1)
459 continue;
460
461 // If it is profitable to lower this range to memset, do so now.
462 if (!Range.isProfitableToUseMemset(DL))
463 continue;
464
465 // Otherwise, we do want to transform this! Create a new memset.
466 // Get the starting pointer of the block.
467 StartPtr = Range.StartPtr;
468
469 AMemSet = Builder.CreateMemSet(StartPtr, ByteVal, Range.End - Range.Start,
470 Range.Alignment);
471 AMemSet->mergeDIAssignID(Range.TheStores);
472
473 LLVM_DEBUG(dbgs() << "Replace stores:\n"; for (Instruction *SI
474 : Range.TheStores) dbgs()
475 << *SI << '\n';
476 dbgs() << "With: " << *AMemSet << '\n');
477 if (!Range.TheStores.empty())
478 AMemSet->setDebugLoc(Range.TheStores[0]->getDebugLoc());
479
480 auto *NewDef = cast<MemoryDef>(
481 MemInsertPoint->getMemoryInst() == &*BI
482 ? MSSAU->createMemoryAccessBefore(AMemSet, nullptr, MemInsertPoint)
483 : MSSAU->createMemoryAccessAfter(AMemSet, nullptr, MemInsertPoint));
484 MSSAU->insertDef(NewDef, /*RenameUses=*/true);
485 MemInsertPoint = NewDef;
486
487 // Zap all the stores.
488 for (Instruction *SI : Range.TheStores)
489 eraseInstruction(SI);
490
491 ++NumMemSetInfer;
492 }
493
494 return AMemSet;
495}
496
497// This method try to lift a store instruction before position P.
498// It will lift the store and its argument + that anything that
499// may alias with these.
500// The method returns true if it was successful.
501bool MemCpyOptPass::moveUp(StoreInst *SI, Instruction *P, const LoadInst *LI) {
502 // If the store alias this position, early bail out.
503 MemoryLocation StoreLoc = MemoryLocation::get(SI);
504 if (isModOrRefSet(AA->getModRefInfo(P, StoreLoc)))
505 return false;
506
507 // Keep track of the arguments of all instruction we plan to lift
508 // so we can make sure to lift them as well if appropriate.
510 auto AddArg = [&](Value *Arg) {
511 auto *I = dyn_cast<Instruction>(Arg);
512 if (I && I->getParent() == SI->getParent()) {
513 // Cannot hoist user of P above P
514 if (I == P)
515 return false;
516 Args.insert(I);
517 }
518 return true;
519 };
520 if (!AddArg(SI->getPointerOperand()))
521 return false;
522
523 // Instruction to lift before P.
525
526 // Memory locations of lifted instructions.
527 SmallVector<MemoryLocation, 8> MemLocs{StoreLoc};
528
529 // Lifted calls.
531
532 const MemoryLocation LoadLoc = MemoryLocation::get(LI);
533
534 for (auto I = --SI->getIterator(), E = P->getIterator(); I != E; --I) {
535 auto *C = &*I;
536
537 // Make sure hoisting does not perform a store that was not guaranteed to
538 // happen.
540 return false;
541
542 bool MayAlias = isModOrRefSet(AA->getModRefInfo(C, std::nullopt));
543
544 bool NeedLift = false;
545 if (Args.erase(C))
546 NeedLift = true;
547 else if (MayAlias) {
548 NeedLift = llvm::any_of(MemLocs, [C, this](const MemoryLocation &ML) {
549 return isModOrRefSet(AA->getModRefInfo(C, ML));
550 });
551
552 if (!NeedLift)
553 NeedLift = llvm::any_of(Calls, [C, this](const CallBase *Call) {
554 return isModOrRefSet(AA->getModRefInfo(C, Call));
555 });
556 }
557
558 if (!NeedLift)
559 continue;
560
561 if (MayAlias) {
562 // Since LI is implicitly moved downwards past the lifted instructions,
563 // none of them may modify its source.
564 if (isModSet(AA->getModRefInfo(C, LoadLoc)))
565 return false;
566 else if (const auto *Call = dyn_cast<CallBase>(C)) {
567 // If we can't lift this before P, it's game over.
568 if (isModOrRefSet(AA->getModRefInfo(P, Call)))
569 return false;
570
571 Calls.push_back(Call);
572 } else if (isa<LoadInst>(C) || isa<StoreInst>(C) || isa<VAArgInst>(C)) {
573 // If we can't lift this before P, it's game over.
574 auto ML = MemoryLocation::get(C);
575 if (isModOrRefSet(AA->getModRefInfo(P, ML)))
576 return false;
577
578 MemLocs.push_back(ML);
579 } else
580 // We don't know how to lift this instruction.
581 return false;
582 }
583
584 ToLift.push_back(C);
585 for (Value *Op : C->operands())
586 if (!AddArg(Op))
587 return false;
588 }
589
590 // Find MSSA insertion point. Normally P will always have a corresponding
591 // memory access before which we can insert. However, with non-standard AA
592 // pipelines, there may be a mismatch between AA and MSSA, in which case we
593 // will scan for a memory access before P. In either case, we know for sure
594 // that at least the load will have a memory access.
595 // TODO: Simplify this once P will be determined by MSSA, in which case the
596 // discrepancy can no longer occur.
597 MemoryUseOrDef *MemInsertPoint = nullptr;
598 if (MemoryUseOrDef *MA = MSSA->getMemoryAccess(P)) {
599 MemInsertPoint = cast<MemoryUseOrDef>(--MA->getIterator());
600 } else {
601 const Instruction *ConstP = P;
602 for (const Instruction &I : make_range(++ConstP->getReverseIterator(),
603 ++LI->getReverseIterator())) {
604 if (MemoryUseOrDef *MA = MSSA->getMemoryAccess(&I)) {
605 MemInsertPoint = MA;
606 break;
607 }
608 }
609 }
610
611 // We made it, we need to lift.
612 for (auto *I : llvm::reverse(ToLift)) {
613 LLVM_DEBUG(dbgs() << "Lifting " << *I << " before " << *P << "\n");
614 I->moveBefore(P->getIterator());
615 assert(MemInsertPoint && "Must have found insert point");
616 if (MemoryUseOrDef *MA = MSSA->getMemoryAccess(I)) {
617 MSSAU->moveAfter(MA, MemInsertPoint);
618 MemInsertPoint = MA;
619 }
620 }
621
622 return true;
623}
624
625bool MemCpyOptPass::processStoreOfLoad(StoreInst *SI, LoadInst *LI,
626 const DataLayout &DL,
628 if (!LI->isSimple() || !LI->hasOneUse() || LI->getParent() != SI->getParent())
629 return false;
630
631 BatchAAResults BAA(*AA, EEA);
632 auto *T = LI->getType();
633 // Don't introduce calls to memcpy/memmove intrinsics out of thin air if
634 // the corresponding libcalls are not available.
635 // TODO: We should really distinguish between libcall availability and
636 // our ability to introduce intrinsics.
637 if (T->isAggregateType() &&
639 (TLI->has(LibFunc_memcpy) && TLI->has(LibFunc_memmove)))) {
641 MemoryUseOrDef *LoadAccess = MSSA->getMemoryAccess(LI),
642 *StoreAccess = MSSA->getMemoryAccess(SI);
643
644 // We use MSSA to check if an instruction may store to the memory we load
645 // from in between the load and the store. If such an instruction is found,
646 // we try to promote there instead of at the store position.
647 auto *Clobber = MSSA->getWalker()->getClobberingMemoryAccess(
648 StoreAccess->getDefiningAccess(), LoadLoc, BAA);
649 Instruction *P = MSSA->dominates(LoadAccess, Clobber)
650 ? cast<MemoryUseOrDef>(Clobber)->getMemoryInst()
651 : SI;
652
653 // If we found an instruction that may write to the loaded memory,
654 // we can try to promote at this position instead of the store
655 // position if nothing aliases the store memory after this and the store
656 // destination is not in the range.
657 if (P == SI || moveUp(SI, P, LI)) {
658 // If we load from memory that may alias the memory we store to,
659 // memmove must be used to preserve semantic. If not, memcpy can
660 // be used. Also, if we load from constant memory, memcpy can be used
661 // as the constant memory won't be modified.
662 bool UseMemMove = false;
663 if (isModSet(AA->getModRefInfo(SI, LoadLoc)))
664 UseMemMove = true;
665
666 IRBuilder<> Builder(P);
667 Value *Size =
668 Builder.CreateTypeSize(Builder.getInt64Ty(), DL.getTypeStoreSize(T));
669 Instruction *M;
670 if (UseMemMove)
671 M = Builder.CreateMemMove(SI->getPointerOperand(), SI->getAlign(),
672 LI->getPointerOperand(), LI->getAlign(),
673 Size);
674 else
675 M = Builder.CreateMemCpy(SI->getPointerOperand(), SI->getAlign(),
676 LI->getPointerOperand(), LI->getAlign(), Size);
677 M->copyMetadata(*SI, LLVMContext::MD_DIAssignID);
678
679 LLVM_DEBUG(dbgs() << "Promoting " << *LI << " to " << *SI << " => " << *M
680 << "\n");
681
682 auto *LastDef = cast<MemoryDef>(MSSA->getMemoryAccess(SI));
683 auto *NewAccess = MSSAU->createMemoryAccessAfter(M, nullptr, LastDef);
684 MSSAU->insertDef(cast<MemoryDef>(NewAccess), /*RenameUses=*/true);
685
686 eraseInstruction(SI);
687 eraseInstruction(LI);
688 ++NumMemCpyInstr;
689
690 // Make sure we do not invalidate the iterator.
691 BBI = M->getIterator();
692 return true;
693 }
694 }
695
696 // Detect cases where we're performing call slot forwarding, but
697 // happen to be using a load-store pair to implement it, rather than
698 // a memcpy.
699 auto GetCall = [&]() -> CallInst * {
700 // We defer this expensive clobber walk until the cheap checks
701 // have been done on the source inside performCallSlotOptzn.
702 if (auto *LoadClobber = dyn_cast<MemoryUseOrDef>(
703 MSSA->getWalker()->getClobberingMemoryAccess(LI, BAA)))
704 return dyn_cast_or_null<CallInst>(LoadClobber->getMemoryInst());
705 return nullptr;
706 };
707
708 bool Changed = performCallSlotOptzn(
709 LI, SI, SI->getPointerOperand()->stripPointerCasts(),
711 DL.getTypeStoreSize(SI->getOperand(0)->getType()),
712 std::min(SI->getAlign(), LI->getAlign()), BAA, GetCall);
713 if (Changed) {
714 eraseInstruction(SI);
715 eraseInstruction(LI);
716 ++NumMemCpyInstr;
717 return true;
718 }
719
720 // If this is a load-store pair from a stack slot to a stack slot, we
721 // might be able to perform the stack-move optimization just as we do for
722 // memcpys from an alloca to an alloca.
723 if (auto *DestAlloca = dyn_cast<AllocaInst>(SI->getPointerOperand())) {
724 if (auto *SrcAlloca = dyn_cast<AllocaInst>(LI->getPointerOperand())) {
725 if (performStackMoveOptzn(LI, SI, DestAlloca, SrcAlloca,
726 DL.getTypeStoreSize(T), BAA)) {
727 // Avoid invalidating the iterator.
728 BBI = SI->getNextNonDebugInstruction()->getIterator();
729 eraseInstruction(SI);
730 eraseInstruction(LI);
731 ++NumMemCpyInstr;
732 return true;
733 }
734 }
735 }
736
737 return false;
738}
739
740bool MemCpyOptPass::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
741 if (!SI->isSimple())
742 return false;
743
744 // Avoid merging nontemporal stores since the resulting
745 // memcpy/memset would not be able to preserve the nontemporal hint.
746 // In theory we could teach how to propagate the !nontemporal metadata to
747 // memset calls. However, that change would force the backend to
748 // conservatively expand !nontemporal memset calls back to sequences of
749 // store instructions (effectively undoing the merging).
750 if (SI->getMetadata(LLVMContext::MD_nontemporal))
751 return false;
752
753 const DataLayout &DL = SI->getDataLayout();
754
755 Value *StoredVal = SI->getValueOperand();
756
757 // Not all the transforms below are correct for non-integral pointers, bail
758 // until we've audited the individual pieces.
759 if (DL.isNonIntegralPointerType(StoredVal->getType()->getScalarType()))
760 return false;
761
762 // Load to store forwarding can be interpreted as memcpy.
763 if (auto *LI = dyn_cast<LoadInst>(StoredVal))
764 return processStoreOfLoad(SI, LI, DL, BBI);
765
766 // The following code creates memset intrinsics out of thin air. Don't do
767 // this if the corresponding libfunc is not available.
768 // TODO: We should really distinguish between libcall availability and
769 // our ability to introduce intrinsics.
770 if (!(TLI->has(LibFunc_memset) || EnableMemCpyOptWithoutLibcalls))
771 return false;
772
773 // There are two cases that are interesting for this code to handle: memcpy
774 // and memset. Right now we only handle memset.
775
776 // Ensure that the value being stored is something that can be memset'able a
777 // byte at a time like "0" or "-1" or any width, as well as things like
778 // 0xA0A0A0A0 and 0.0.
779 Value *V = SI->getOperand(0);
780 Value *ByteVal = isBytewiseValue(V, DL);
781 if (!ByteVal)
782 return false;
783
784 if (Instruction *I =
785 tryMergingIntoMemset(SI, SI->getPointerOperand(), ByteVal)) {
786 BBI = I->getIterator(); // Don't invalidate iterator.
787 return true;
788 }
789
790 // If we have an aggregate, we try to promote it to memset regardless
791 // of opportunity for merging as it can expose optimization opportunities
792 // in subsequent passes.
793 auto *T = V->getType();
794 if (!T->isAggregateType())
795 return false;
796
797 TypeSize Size = DL.getTypeStoreSize(T);
798 if (Size.isScalable())
799 return false;
800
801 IRBuilder<> Builder(SI);
802 auto *M = Builder.CreateMemSet(SI->getPointerOperand(), ByteVal, Size,
803 SI->getAlign());
804 M->copyMetadata(*SI, LLVMContext::MD_DIAssignID);
805
806 LLVM_DEBUG(dbgs() << "Promoting " << *SI << " to " << *M << "\n");
807
808 // The newly inserted memset is immediately overwritten by the original
809 // store, so we do not need to rename uses.
810 auto *StoreDef = cast<MemoryDef>(MSSA->getMemoryAccess(SI));
811 auto *NewAccess = MSSAU->createMemoryAccessBefore(M, nullptr, StoreDef);
812 MSSAU->insertDef(cast<MemoryDef>(NewAccess), /*RenameUses=*/false);
813
814 eraseInstruction(SI);
815 NumMemSetInfer++;
816
817 // Make sure we do not invalidate the iterator.
818 BBI = M->getIterator();
819 return true;
820}
821
822bool MemCpyOptPass::processMemSet(MemSetInst *MSI, BasicBlock::iterator &BBI) {
823 // See if there is another memset or store neighboring this memset which
824 // allows us to widen out the memset to do a single larger store.
825 if (isa<ConstantInt>(MSI->getLength()) && !MSI->isVolatile())
826 if (Instruction *I =
827 tryMergingIntoMemset(MSI, MSI->getDest(), MSI->getValue())) {
828 BBI = I->getIterator(); // Don't invalidate iterator.
829 return true;
830 }
831 return false;
832}
833
834/// Takes a memcpy and a call that it depends on,
835/// and checks for the possibility of a call slot optimization by having
836/// the call write its result directly into the destination of the memcpy.
837bool MemCpyOptPass::performCallSlotOptzn(Instruction *cpyLoad,
838 Instruction *cpyStore, Value *cpyDest,
839 Value *cpySrc, TypeSize cpySize,
840 Align cpyDestAlign,
841 BatchAAResults &BAA,
842 std::function<CallInst *()> GetC) {
843 // The general transformation to keep in mind is
844 //
845 // call @func(..., src, ...)
846 // memcpy(dest, src, ...)
847 //
848 // ->
849 //
850 // memcpy(dest, src, ...)
851 // call @func(..., dest, ...)
852 //
853 // Since moving the memcpy is technically awkward, we additionally check that
854 // src only holds uninitialized values at the moment of the call, meaning that
855 // the memcpy can be discarded rather than moved.
856
857 // We can't optimize scalable types.
858 if (cpySize.isScalable())
859 return false;
860
861 // Require that src be an alloca. This simplifies the reasoning considerably.
862 auto *srcAlloca = dyn_cast<AllocaInst>(cpySrc);
863 if (!srcAlloca)
864 return false;
865
866 ConstantInt *srcArraySize = dyn_cast<ConstantInt>(srcAlloca->getArraySize());
867 if (!srcArraySize)
868 return false;
869
870 const DataLayout &DL = cpyLoad->getDataLayout();
871 TypeSize SrcAllocaSize = DL.getTypeAllocSize(srcAlloca->getAllocatedType());
872 // We can't optimize scalable types.
873 if (SrcAllocaSize.isScalable())
874 return false;
875 uint64_t srcSize = SrcAllocaSize * srcArraySize->getZExtValue();
876
877 if (cpySize < srcSize)
878 return false;
879
880 CallInst *C = GetC();
881 if (!C)
882 return false;
883
884 // Lifetime marks shouldn't be operated on.
885 if (Function *F = C->getCalledFunction())
886 if (F->isIntrinsic() && F->getIntrinsicID() == Intrinsic::lifetime_start)
887 return false;
888
889 if (C->getParent() != cpyStore->getParent()) {
890 LLVM_DEBUG(dbgs() << "Call Slot: block local restriction\n");
891 return false;
892 }
893
894 MemoryLocation DestLoc =
895 isa<StoreInst>(cpyStore)
896 ? MemoryLocation::get(cpyStore)
897 : MemoryLocation::getForDest(cast<MemCpyInst>(cpyStore));
898
899 // Check that nothing touches the dest of the copy between
900 // the call and the store/memcpy.
901 Instruction *SkippedLifetimeStart = nullptr;
902 if (accessedBetween(BAA, DestLoc, MSSA->getMemoryAccess(C),
903 MSSA->getMemoryAccess(cpyStore), &SkippedLifetimeStart)) {
904 LLVM_DEBUG(dbgs() << "Call Slot: Dest pointer modified after call\n");
905 return false;
906 }
907
908 // If we need to move a lifetime.start above the call, make sure that we can
909 // actually do so. If the argument is bitcasted for example, we would have to
910 // move the bitcast as well, which we don't handle.
911 if (SkippedLifetimeStart) {
912 auto *LifetimeArg =
913 dyn_cast<Instruction>(SkippedLifetimeStart->getOperand(1));
914 if (LifetimeArg && LifetimeArg->getParent() == C->getParent() &&
915 C->comesBefore(LifetimeArg))
916 return false;
917 }
918
919 // Check that storing to the first srcSize bytes of dest will not cause a
920 // trap or data race.
921 bool ExplicitlyDereferenceableOnly;
923 ExplicitlyDereferenceableOnly) ||
924 !isDereferenceableAndAlignedPointer(cpyDest, Align(1), APInt(64, cpySize),
925 DL, C, AC, DT)) {
926 LLVM_DEBUG(dbgs() << "Call Slot: Dest pointer not dereferenceable\n");
927 return false;
928 }
929
930 // Make sure that nothing can observe cpyDest being written early. There are
931 // a number of cases to consider:
932 // 1. cpyDest cannot be accessed between C and cpyStore as a precondition of
933 // the transform.
934 // 2. C itself may not access cpyDest (prior to the transform). This is
935 // checked further below.
936 // 3. If cpyDest is accessible to the caller of this function (potentially
937 // captured and not based on an alloca), we need to ensure that we cannot
938 // unwind between C and cpyStore. This is checked here.
939 // 4. If cpyDest is potentially captured, there may be accesses to it from
940 // another thread. In this case, we need to check that cpyStore is
941 // guaranteed to be executed if C is. As it is a non-atomic access, it
942 // renders accesses from other threads undefined.
943 // TODO: This is currently not checked.
944 if (mayBeVisibleThroughUnwinding(cpyDest, C, cpyStore)) {
945 LLVM_DEBUG(dbgs() << "Call Slot: Dest may be visible through unwinding\n");
946 return false;
947 }
948
949 // Check that dest points to memory that is at least as aligned as src.
950 Align srcAlign = srcAlloca->getAlign();
951 bool isDestSufficientlyAligned = srcAlign <= cpyDestAlign;
952 // If dest is not aligned enough and we can't increase its alignment then
953 // bail out.
954 if (!isDestSufficientlyAligned && !isa<AllocaInst>(cpyDest)) {
955 LLVM_DEBUG(dbgs() << "Call Slot: Dest not sufficiently aligned\n");
956 return false;
957 }
958
959 // Check that src is not accessed except via the call and the memcpy. This
960 // guarantees that it holds only undefined values when passed in (so the final
961 // memcpy can be dropped), that it is not read or written between the call and
962 // the memcpy, and that writing beyond the end of it is undefined.
963 SmallVector<User *, 8> srcUseList(srcAlloca->users());
964 while (!srcUseList.empty()) {
965 User *U = srcUseList.pop_back_val();
966
967 if (isa<AddrSpaceCastInst>(U)) {
968 append_range(srcUseList, U->users());
969 continue;
970 }
971 if (isa<LifetimeIntrinsic>(U))
972 continue;
973
974 if (U != C && U != cpyLoad) {
975 LLVM_DEBUG(dbgs() << "Call slot: Source accessed by " << *U << "\n");
976 return false;
977 }
978 }
979
980 // Check whether src is captured by the called function, in which case there
981 // may be further indirect uses of src.
982 bool SrcIsCaptured = any_of(C->args(), [&](Use &U) {
983 return U->stripPointerCasts() == cpySrc &&
984 !C->doesNotCapture(C->getArgOperandNo(&U));
985 });
986
987 // If src is captured, then check whether there are any potential uses of
988 // src through the captured pointer before the lifetime of src ends, either
989 // due to a lifetime.end or a return from the function.
990 if (SrcIsCaptured) {
991 // Check that dest is not captured before/at the call. We have already
992 // checked that src is not captured before it. If either had been captured,
993 // then the call might be comparing the argument against the captured dest
994 // or src pointer.
995 Value *DestObj = getUnderlyingObject(cpyDest);
996 if (!isIdentifiedFunctionLocal(DestObj) ||
997 PointerMayBeCapturedBefore(DestObj, /* ReturnCaptures */ true,
998 /* StoreCaptures */ true, C, DT,
999 /* IncludeI */ true))
1000 return false;
1001
1002 MemoryLocation SrcLoc =
1003 MemoryLocation(srcAlloca, LocationSize::precise(srcSize));
1004 for (Instruction &I :
1005 make_range(++C->getIterator(), C->getParent()->end())) {
1006 // Lifetime of srcAlloca ends at lifetime.end.
1007 if (auto *II = dyn_cast<IntrinsicInst>(&I)) {
1008 if (II->getIntrinsicID() == Intrinsic::lifetime_end &&
1009 II->getArgOperand(1)->stripPointerCasts() == srcAlloca &&
1010 cast<ConstantInt>(II->getArgOperand(0))->uge(srcSize))
1011 break;
1012 }
1013
1014 // Lifetime of srcAlloca ends at return.
1015 if (isa<ReturnInst>(&I))
1016 break;
1017
1018 // Ignore the direct read of src in the load.
1019 if (&I == cpyLoad)
1020 continue;
1021
1022 // Check whether this instruction may mod/ref src through the captured
1023 // pointer (we have already any direct mod/refs in the loop above).
1024 // Also bail if we hit a terminator, as we don't want to scan into other
1025 // blocks.
1026 if (isModOrRefSet(BAA.getModRefInfo(&I, SrcLoc)) || I.isTerminator())
1027 return false;
1028 }
1029 }
1030
1031 // Since we're changing the parameter to the callsite, we need to make sure
1032 // that what would be the new parameter dominates the callsite.
1033 bool NeedMoveGEP = false;
1034 if (!DT->dominates(cpyDest, C)) {
1035 // Support moving a constant index GEP before the call.
1036 auto *GEP = dyn_cast<GetElementPtrInst>(cpyDest);
1037 if (GEP && GEP->hasAllConstantIndices() &&
1038 DT->dominates(GEP->getPointerOperand(), C))
1039 NeedMoveGEP = true;
1040 else
1041 return false;
1042 }
1043
1044 // In addition to knowing that the call does not access src in some
1045 // unexpected manner, for example via a global, which we deduce from
1046 // the use analysis, we also need to know that it does not sneakily
1047 // access dest. We rely on AA to figure this out for us.
1048 MemoryLocation DestWithSrcSize(cpyDest, LocationSize::precise(srcSize));
1049 ModRefInfo MR = BAA.getModRefInfo(C, DestWithSrcSize);
1050 // If necessary, perform additional analysis.
1051 if (isModOrRefSet(MR))
1052 MR = BAA.callCapturesBefore(C, DestWithSrcSize, DT);
1053 if (isModOrRefSet(MR))
1054 return false;
1055
1056 // We can't create address space casts here because we don't know if they're
1057 // safe for the target.
1058 if (cpySrc->getType() != cpyDest->getType())
1059 return false;
1060 for (unsigned ArgI = 0; ArgI < C->arg_size(); ++ArgI)
1061 if (C->getArgOperand(ArgI)->stripPointerCasts() == cpySrc &&
1062 cpySrc->getType() != C->getArgOperand(ArgI)->getType())
1063 return false;
1064
1065 // All the checks have passed, so do the transformation.
1066 bool changedArgument = false;
1067 for (unsigned ArgI = 0; ArgI < C->arg_size(); ++ArgI)
1068 if (C->getArgOperand(ArgI)->stripPointerCasts() == cpySrc) {
1069 changedArgument = true;
1070 C->setArgOperand(ArgI, cpyDest);
1071 }
1072
1073 if (!changedArgument)
1074 return false;
1075
1076 // If the destination wasn't sufficiently aligned then increase its alignment.
1077 if (!isDestSufficientlyAligned) {
1078 assert(isa<AllocaInst>(cpyDest) && "Can only increase alloca alignment!");
1079 cast<AllocaInst>(cpyDest)->setAlignment(srcAlign);
1080 }
1081
1082 if (NeedMoveGEP) {
1083 auto *GEP = dyn_cast<GetElementPtrInst>(cpyDest);
1084 GEP->moveBefore(C->getIterator());
1085 }
1086
1087 if (SkippedLifetimeStart) {
1088 SkippedLifetimeStart->moveBefore(C->getIterator());
1089 MSSAU->moveBefore(MSSA->getMemoryAccess(SkippedLifetimeStart),
1090 MSSA->getMemoryAccess(C));
1091 }
1092
1093 combineAAMetadata(C, cpyLoad);
1094 if (cpyLoad != cpyStore)
1095 combineAAMetadata(C, cpyStore);
1096
1097 ++NumCallSlot;
1098 return true;
1099}
1100
1101/// We've found that the (upward scanning) memory dependence of memcpy 'M' is
1102/// the memcpy 'MDep'. Try to simplify M to copy from MDep's input if we can.
1103bool MemCpyOptPass::processMemCpyMemCpyDependence(MemCpyInst *M,
1104 MemCpyInst *MDep,
1105 BatchAAResults &BAA) {
1106 // If dep instruction is reading from our current input, then it is a noop
1107 // transfer and substituting the input won't change this instruction. Just
1108 // ignore the input and let someone else zap MDep. This handles cases like:
1109 // memcpy(a <- a)
1110 // memcpy(b <- a)
1111 if (M->getSource() == MDep->getSource())
1112 return false;
1113
1114 // We can only optimize non-volatile memcpy's.
1115 if (MDep->isVolatile())
1116 return false;
1117
1118 int64_t MForwardOffset = 0;
1119 const DataLayout &DL = M->getModule()->getDataLayout();
1120 // We can only transforms memcpy's where the dest of one is the source of the
1121 // other, or they have an offset in a range.
1122 if (M->getSource() != MDep->getDest()) {
1123 std::optional<int64_t> Offset =
1124 M->getSource()->getPointerOffsetFrom(MDep->getDest(), DL);
1125 if (!Offset || *Offset < 0)
1126 return false;
1127 MForwardOffset = *Offset;
1128 }
1129
1130 // The length of the memcpy's must be the same, or the preceding one
1131 // must be larger than the following one.
1132 if (MForwardOffset != 0 || MDep->getLength() != M->getLength()) {
1133 auto *MDepLen = dyn_cast<ConstantInt>(MDep->getLength());
1134 auto *MLen = dyn_cast<ConstantInt>(M->getLength());
1135 if (!MDepLen || !MLen ||
1136 MDepLen->getZExtValue() < MLen->getZExtValue() + MForwardOffset)
1137 return false;
1138 }
1139
1140 IRBuilder<> Builder(M);
1141 auto *CopySource = MDep->getSource();
1142 Instruction *NewCopySource = nullptr;
1143 auto CleanupOnRet = llvm::make_scope_exit([&] {
1144 if (NewCopySource && NewCopySource->use_empty())
1145 // Safety: It's safe here because we will only allocate more instructions
1146 // after finishing all BatchAA queries, but we have to be careful if we
1147 // want to do something like this in another place. Then we'd probably
1148 // have to delay instruction removal until all transforms on an
1149 // instruction finished.
1150 eraseInstruction(NewCopySource);
1151 });
1152 MaybeAlign CopySourceAlign = MDep->getSourceAlign();
1153 // We just need to calculate the actual size of the copy.
1154 auto MCopyLoc = MemoryLocation::getForSource(MDep).getWithNewSize(
1156
1157 // When the forwarding offset is greater than 0, we transform
1158 // memcpy(d1 <- s1)
1159 // memcpy(d2 <- d1+o)
1160 // to
1161 // memcpy(d2 <- s1+o)
1162 if (MForwardOffset > 0) {
1163 // The copy destination of `M` maybe can serve as the source of copying.
1164 std::optional<int64_t> MDestOffset =
1165 M->getRawDest()->getPointerOffsetFrom(MDep->getRawSource(), DL);
1166 if (MDestOffset == MForwardOffset)
1167 CopySource = M->getDest();
1168 else {
1169 CopySource = Builder.CreateInBoundsPtrAdd(
1170 CopySource, Builder.getInt64(MForwardOffset));
1171 NewCopySource = dyn_cast<Instruction>(CopySource);
1172 }
1173 // We need to update `MCopyLoc` if an offset exists.
1174 MCopyLoc = MCopyLoc.getWithNewPtr(CopySource);
1175 if (CopySourceAlign)
1176 CopySourceAlign = commonAlignment(*CopySourceAlign, MForwardOffset);
1177 }
1178
1179 // Avoid infinite loops
1180 if (BAA.isMustAlias(M->getSource(), CopySource))
1181 return false;
1182
1183 // Verify that the copied-from memory doesn't change in between the two
1184 // transfers. For example, in:
1185 // memcpy(a <- b)
1186 // *b = 42;
1187 // memcpy(c <- a)
1188 // It would be invalid to transform the second memcpy into memcpy(c <- b).
1189 //
1190 // TODO: If the code between M and MDep is transparent to the destination "c",
1191 // then we could still perform the xform by moving M up to the first memcpy.
1192 if (writtenBetween(MSSA, BAA, MCopyLoc, MSSA->getMemoryAccess(MDep),
1193 MSSA->getMemoryAccess(M)))
1194 return false;
1195
1196 // No need to create `memcpy(a <- a)`.
1197 if (BAA.isMustAlias(M->getDest(), CopySource)) {
1198 // Remove the instruction we're replacing.
1199 eraseInstruction(M);
1200 ++NumMemCpyInstr;
1201 return true;
1202 }
1203
1204 // If the dest of the second might alias the source of the first, then the
1205 // source and dest might overlap. In addition, if the source of the first
1206 // points to constant memory, they won't overlap by definition. Otherwise, we
1207 // still want to eliminate the intermediate value, but we have to generate a
1208 // memmove instead of memcpy.
1209 bool UseMemMove = false;
1211 // Don't convert llvm.memcpy.inline into memmove because memmove can be
1212 // lowered as a call, and that is not allowed for llvm.memcpy.inline (and
1213 // there is no inline version of llvm.memmove)
1214 if (isa<MemCpyInlineInst>(M))
1215 return false;
1216 UseMemMove = true;
1217 }
1218
1219 // If all checks passed, then we can transform M.
1220 LLVM_DEBUG(dbgs() << "MemCpyOptPass: Forwarding memcpy->memcpy src:\n"
1221 << *MDep << '\n'
1222 << *M << '\n');
1223
1224 // TODO: Is this worth it if we're creating a less aligned memcpy? For
1225 // example we could be moving from movaps -> movq on x86.
1226 Instruction *NewM;
1227 if (UseMemMove)
1228 NewM =
1229 Builder.CreateMemMove(M->getDest(), M->getDestAlign(), CopySource,
1230 CopySourceAlign, M->getLength(), M->isVolatile());
1231 else if (isa<MemCpyInlineInst>(M)) {
1232 // llvm.memcpy may be promoted to llvm.memcpy.inline, but the converse is
1233 // never allowed since that would allow the latter to be lowered as a call
1234 // to an external function.
1235 NewM = Builder.CreateMemCpyInline(M->getDest(), M->getDestAlign(),
1236 CopySource, CopySourceAlign,
1237 M->getLength(), M->isVolatile());
1238 } else
1239 NewM =
1240 Builder.CreateMemCpy(M->getDest(), M->getDestAlign(), CopySource,
1241 CopySourceAlign, M->getLength(), M->isVolatile());
1242 NewM->copyMetadata(*M, LLVMContext::MD_DIAssignID);
1243
1244 assert(isa<MemoryDef>(MSSA->getMemoryAccess(M)));
1245 auto *LastDef = cast<MemoryDef>(MSSA->getMemoryAccess(M));
1246 auto *NewAccess = MSSAU->createMemoryAccessAfter(NewM, nullptr, LastDef);
1247 MSSAU->insertDef(cast<MemoryDef>(NewAccess), /*RenameUses=*/true);
1248
1249 // Remove the instruction we're replacing.
1250 eraseInstruction(M);
1251 ++NumMemCpyInstr;
1252 return true;
1253}
1254
1255/// We've found that the (upward scanning) memory dependence of \p MemCpy is
1256/// \p MemSet. Try to simplify \p MemSet to only set the trailing bytes that
1257/// weren't copied over by \p MemCpy.
1258///
1259/// In other words, transform:
1260/// \code
1261/// memset(dst, c, dst_size);
1262/// ...
1263/// memcpy(dst, src, src_size);
1264/// \endcode
1265/// into:
1266/// \code
1267/// ...
1268/// memset(dst + src_size, c, dst_size <= src_size ? 0 : dst_size - src_size);
1269/// memcpy(dst, src, src_size);
1270/// \endcode
1271///
1272/// The memset is sunk to just before the memcpy to ensure that src_size is
1273/// present when emitting the simplified memset.
1274bool MemCpyOptPass::processMemSetMemCpyDependence(MemCpyInst *MemCpy,
1275 MemSetInst *MemSet,
1276 BatchAAResults &BAA) {
1277 // We can only transform memset/memcpy with the same destination.
1278 if (!BAA.isMustAlias(MemSet->getDest(), MemCpy->getDest()))
1279 return false;
1280
1281 // Don't perform the transform if src_size may be zero. In that case, the
1282 // transform is essentially a complex no-op and may lead to an infinite
1283 // loop if BasicAA is smart enough to understand that dst and dst + src_size
1284 // are still MustAlias after the transform.
1285 Value *SrcSize = MemCpy->getLength();
1286 if (!isKnownNonZero(SrcSize,
1287 SimplifyQuery(MemCpy->getDataLayout(), DT, AC, MemCpy)))
1288 return false;
1289
1290 // Check that src and dst of the memcpy aren't the same. While memcpy
1291 // operands cannot partially overlap, exact equality is allowed.
1292 if (isModSet(BAA.getModRefInfo(MemCpy, MemoryLocation::getForSource(MemCpy))))
1293 return false;
1294
1295 // We know that dst up to src_size is not written. We now need to make sure
1296 // that dst up to dst_size is not accessed. (If we did not move the memset,
1297 // checking for reads would be sufficient.)
1299 MSSA->getMemoryAccess(MemSet),
1300 MSSA->getMemoryAccess(MemCpy)))
1301 return false;
1302
1303 // Use the same i8* dest as the memcpy, killing the memset dest if different.
1304 Value *Dest = MemCpy->getRawDest();
1305 Value *DestSize = MemSet->getLength();
1306
1307 if (mayBeVisibleThroughUnwinding(Dest, MemSet, MemCpy))
1308 return false;
1309
1310 // If the sizes are the same, simply drop the memset instead of generating
1311 // a replacement with zero size.
1312 if (DestSize == SrcSize) {
1313 eraseInstruction(MemSet);
1314 return true;
1315 }
1316
1317 // By default, create an unaligned memset.
1318 Align Alignment = Align(1);
1319 // If Dest is aligned, and SrcSize is constant, use the minimum alignment
1320 // of the sum.
1321 const Align DestAlign = std::max(MemSet->getDestAlign().valueOrOne(),
1322 MemCpy->getDestAlign().valueOrOne());
1323 if (DestAlign > 1)
1324 if (auto *SrcSizeC = dyn_cast<ConstantInt>(SrcSize))
1325 Alignment = commonAlignment(DestAlign, SrcSizeC->getZExtValue());
1326
1327 IRBuilder<> Builder(MemCpy);
1328
1329 // Preserve the debug location of the old memset for the code emitted here
1330 // related to the new memset. This is correct according to the rules in
1331 // https://llvm.org/docs/HowToUpdateDebugInfo.html about "when to preserve an
1332 // instruction location", given that we move the memset within the basic
1333 // block.
1334 assert(MemSet->getParent() == MemCpy->getParent() &&
1335 "Preserving debug location based on moving memset within BB.");
1336 Builder.SetCurrentDebugLocation(MemSet->getDebugLoc());
1337
1338 // If the sizes have different types, zext the smaller one.
1339 if (DestSize->getType() != SrcSize->getType()) {
1340 if (DestSize->getType()->getIntegerBitWidth() >
1341 SrcSize->getType()->getIntegerBitWidth())
1342 SrcSize = Builder.CreateZExt(SrcSize, DestSize->getType());
1343 else
1344 DestSize = Builder.CreateZExt(DestSize, SrcSize->getType());
1345 }
1346
1347 Value *Ule = Builder.CreateICmpULE(DestSize, SrcSize);
1348 Value *SizeDiff = Builder.CreateSub(DestSize, SrcSize);
1349 Value *MemsetLen = Builder.CreateSelect(
1350 Ule, ConstantInt::getNullValue(DestSize->getType()), SizeDiff);
1351 Instruction *NewMemSet =
1352 Builder.CreateMemSet(Builder.CreatePtrAdd(Dest, SrcSize),
1353 MemSet->getOperand(1), MemsetLen, Alignment);
1354
1355 assert(isa<MemoryDef>(MSSA->getMemoryAccess(MemCpy)) &&
1356 "MemCpy must be a MemoryDef");
1357 // The new memset is inserted before the memcpy, and it is known that the
1358 // memcpy's defining access is the memset about to be removed.
1359 auto *LastDef = cast<MemoryDef>(MSSA->getMemoryAccess(MemCpy));
1360 auto *NewAccess =
1361 MSSAU->createMemoryAccessBefore(NewMemSet, nullptr, LastDef);
1362 MSSAU->insertDef(cast<MemoryDef>(NewAccess), /*RenameUses=*/true);
1363
1364 eraseInstruction(MemSet);
1365 return true;
1366}
1367
1368/// Determine whether the instruction has undefined content for the given Size,
1369/// either because it was freshly alloca'd or started its lifetime.
1371 MemoryDef *Def, Value *Size) {
1372 if (MSSA->isLiveOnEntryDef(Def))
1373 return isa<AllocaInst>(getUnderlyingObject(V));
1374
1375 if (auto *II = dyn_cast_or_null<IntrinsicInst>(Def->getMemoryInst())) {
1376 if (II->getIntrinsicID() == Intrinsic::lifetime_start) {
1377 auto *LTSize = cast<ConstantInt>(II->getArgOperand(0));
1378
1379 if (auto *CSize = dyn_cast<ConstantInt>(Size)) {
1380 if (AA.isMustAlias(V, II->getArgOperand(1)) &&
1381 LTSize->getZExtValue() >= CSize->getZExtValue())
1382 return true;
1383 }
1384
1385 // If the lifetime.start covers a whole alloca (as it almost always
1386 // does) and we're querying a pointer based on that alloca, then we know
1387 // the memory is definitely undef, regardless of how exactly we alias.
1388 // The size also doesn't matter, as an out-of-bounds access would be UB.
1389 if (auto *Alloca = dyn_cast<AllocaInst>(getUnderlyingObject(V))) {
1390 if (getUnderlyingObject(II->getArgOperand(1)) == Alloca) {
1391 const DataLayout &DL = Alloca->getDataLayout();
1392 if (std::optional<TypeSize> AllocaSize =
1393 Alloca->getAllocationSize(DL))
1394 if (*AllocaSize == LTSize->getValue())
1395 return true;
1396 }
1397 }
1398 }
1399 }
1400
1401 return false;
1402}
1403
1404/// Transform memcpy to memset when its source was just memset.
1405/// In other words, turn:
1406/// \code
1407/// memset(dst1, c, dst1_size);
1408/// memcpy(dst2, dst1, dst2_size);
1409/// \endcode
1410/// into:
1411/// \code
1412/// memset(dst1, c, dst1_size);
1413/// memset(dst2, c, dst2_size);
1414/// \endcode
1415/// When dst2_size <= dst1_size.
1416bool MemCpyOptPass::performMemCpyToMemSetOptzn(MemCpyInst *MemCpy,
1417 MemSetInst *MemSet,
1418 BatchAAResults &BAA) {
1419 // Make sure that memcpy(..., memset(...), ...), that is we are memsetting and
1420 // memcpying from the same address. Otherwise it is hard to reason about.
1421 if (!BAA.isMustAlias(MemSet->getRawDest(), MemCpy->getRawSource()))
1422 return false;
1423
1424 Value *MemSetSize = MemSet->getLength();
1425 Value *CopySize = MemCpy->getLength();
1426
1427 if (MemSetSize != CopySize) {
1428 // Make sure the memcpy doesn't read any more than what the memset wrote.
1429 // Don't worry about sizes larger than i64.
1430
1431 // A known memset size is required.
1432 auto *CMemSetSize = dyn_cast<ConstantInt>(MemSetSize);
1433 if (!CMemSetSize)
1434 return false;
1435
1436 // A known memcpy size is also required.
1437 auto *CCopySize = dyn_cast<ConstantInt>(CopySize);
1438 if (!CCopySize)
1439 return false;
1440 if (CCopySize->getZExtValue() > CMemSetSize->getZExtValue()) {
1441 // If the memcpy is larger than the memset, but the memory was undef prior
1442 // to the memset, we can just ignore the tail. Technically we're only
1443 // interested in the bytes from MemSetSize..CopySize here, but as we can't
1444 // easily represent this location, we use the full 0..CopySize range.
1445 MemoryLocation MemCpyLoc = MemoryLocation::getForSource(MemCpy);
1446 bool CanReduceSize = false;
1447 MemoryUseOrDef *MemSetAccess = MSSA->getMemoryAccess(MemSet);
1449 MemSetAccess->getDefiningAccess(), MemCpyLoc, BAA);
1450 if (auto *MD = dyn_cast<MemoryDef>(Clobber))
1451 if (hasUndefContents(MSSA, BAA, MemCpy->getSource(), MD, CopySize))
1452 CanReduceSize = true;
1453
1454 if (!CanReduceSize)
1455 return false;
1456 CopySize = MemSetSize;
1457 }
1458 }
1459
1460 IRBuilder<> Builder(MemCpy);
1461 Instruction *NewM =
1462 Builder.CreateMemSet(MemCpy->getRawDest(), MemSet->getOperand(1),
1463 CopySize, MemCpy->getDestAlign());
1464 auto *LastDef = cast<MemoryDef>(MSSA->getMemoryAccess(MemCpy));
1465 auto *NewAccess = MSSAU->createMemoryAccessAfter(NewM, nullptr, LastDef);
1466 MSSAU->insertDef(cast<MemoryDef>(NewAccess), /*RenameUses=*/true);
1467
1468 return true;
1469}
1470
1471// Attempts to optimize the pattern whereby memory is copied from an alloca to
1472// another alloca, where the two allocas don't have conflicting mod/ref. If
1473// successful, the two allocas can be merged into one and the transfer can be
1474// deleted. This pattern is generated frequently in Rust, due to the ubiquity of
1475// move operations in that language.
1476//
1477// Once we determine that the optimization is safe to perform, we replace all
1478// uses of the destination alloca with the source alloca. We also "shrink wrap"
1479// the lifetime markers of the single merged alloca to before the first use
1480// and after the last use. Note that the "shrink wrapping" procedure is a safe
1481// transformation only because we restrict the scope of this optimization to
1482// allocas that aren't captured.
1483bool MemCpyOptPass::performStackMoveOptzn(Instruction *Load, Instruction *Store,
1484 AllocaInst *DestAlloca,
1485 AllocaInst *SrcAlloca, TypeSize Size,
1486 BatchAAResults &BAA) {
1487 LLVM_DEBUG(dbgs() << "Stack Move: Attempting to optimize:\n"
1488 << *Store << "\n");
1489
1490 // Make sure the two allocas are in the same address space.
1491 if (SrcAlloca->getAddressSpace() != DestAlloca->getAddressSpace()) {
1492 LLVM_DEBUG(dbgs() << "Stack Move: Address space mismatch\n");
1493 return false;
1494 }
1495
1496 // Check that copy is full with static size.
1497 const DataLayout &DL = DestAlloca->getDataLayout();
1498 std::optional<TypeSize> SrcSize = SrcAlloca->getAllocationSize(DL);
1499 if (!SrcSize || Size != *SrcSize) {
1500 LLVM_DEBUG(dbgs() << "Stack Move: Source alloca size mismatch\n");
1501 return false;
1502 }
1503 std::optional<TypeSize> DestSize = DestAlloca->getAllocationSize(DL);
1504 if (!DestSize || Size != *DestSize) {
1505 LLVM_DEBUG(dbgs() << "Stack Move: Destination alloca size mismatch\n");
1506 return false;
1507 }
1508
1509 if (!SrcAlloca->isStaticAlloca() || !DestAlloca->isStaticAlloca())
1510 return false;
1511
1512 // Check that src and dest are never captured, unescaped allocas. Also
1513 // find the nearest common dominator and postdominator for all users in
1514 // order to shrink wrap the lifetimes, and instructions with noalias metadata
1515 // to remove them.
1516
1517 SmallVector<Instruction *, 4> LifetimeMarkers;
1518 SmallSet<Instruction *, 4> NoAliasInstrs;
1519 bool SrcNotDom = false;
1520
1521 // Recursively track the user and check whether modified alias exist.
1522 auto IsDereferenceableOrNull = [](Value *V, const DataLayout &DL) -> bool {
1523 bool CanBeNull, CanBeFreed;
1524 return V->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed);
1525 };
1526
1527 auto CaptureTrackingWithModRef =
1528 [&](Instruction *AI,
1529 function_ref<bool(Instruction *)> ModRefCallback) -> bool {
1531 Worklist.push_back(AI);
1532 unsigned MaxUsesToExplore = getDefaultMaxUsesToExploreForCaptureTracking();
1533 Worklist.reserve(MaxUsesToExplore);
1535 while (!Worklist.empty()) {
1536 Instruction *I = Worklist.back();
1537 Worklist.pop_back();
1538 for (const Use &U : I->uses()) {
1539 auto *UI = cast<Instruction>(U.getUser());
1540 // If any use that isn't dominated by SrcAlloca exists, we move src
1541 // alloca to the entry before the transformation.
1542 if (!DT->dominates(SrcAlloca, UI))
1543 SrcNotDom = true;
1544
1545 if (Visited.size() >= MaxUsesToExplore) {
1546 LLVM_DEBUG(
1547 dbgs()
1548 << "Stack Move: Exceeded max uses to see ModRef, bailing\n");
1549 return false;
1550 }
1551 if (!Visited.insert(&U).second)
1552 continue;
1553 switch (DetermineUseCaptureKind(U, IsDereferenceableOrNull)) {
1555 return false;
1557 // Instructions cannot have non-instruction users.
1558 Worklist.push_back(UI);
1559 continue;
1561 if (UI->isLifetimeStartOrEnd()) {
1562 // We note the locations of these intrinsic calls so that we can
1563 // delete them later if the optimization succeeds, this is safe
1564 // since both llvm.lifetime.start and llvm.lifetime.end intrinsics
1565 // practically fill all the bytes of the alloca with an undefined
1566 // value, although conceptually marked as alive/dead.
1567 int64_t Size = cast<ConstantInt>(UI->getOperand(0))->getSExtValue();
1568 if (Size < 0 || Size == DestSize) {
1569 LifetimeMarkers.push_back(UI);
1570 continue;
1571 }
1572 }
1573 if (UI->hasMetadata(LLVMContext::MD_noalias))
1574 NoAliasInstrs.insert(UI);
1575 if (!ModRefCallback(UI))
1576 return false;
1577 }
1578 }
1579 }
1580 }
1581 return true;
1582 };
1583
1584 // Check that dest has no Mod/Ref, from the alloca to the Store, except full
1585 // size lifetime intrinsics. And collect modref inst for the reachability
1586 // check.
1587 ModRefInfo DestModRef = ModRefInfo::NoModRef;
1588 MemoryLocation DestLoc(DestAlloca, LocationSize::precise(Size));
1589 SmallVector<BasicBlock *, 8> ReachabilityWorklist;
1590 auto DestModRefCallback = [&](Instruction *UI) -> bool {
1591 // We don't care about the store itself.
1592 if (UI == Store)
1593 return true;
1594 ModRefInfo Res = BAA.getModRefInfo(UI, DestLoc);
1595 DestModRef |= Res;
1596 if (isModOrRefSet(Res)) {
1597 // Instructions reachability checks.
1598 // FIXME: adding the Instruction version isPotentiallyReachableFromMany on
1599 // lib/Analysis/CFG.cpp (currently only for BasicBlocks) might be helpful.
1600 if (UI->getParent() == Store->getParent()) {
1601 // The same block case is special because it's the only time we're
1602 // looking within a single block to see which instruction comes first.
1603 // Once we start looking at multiple blocks, the first instruction of
1604 // the block is reachable, so we only need to determine reachability
1605 // between whole blocks.
1606 BasicBlock *BB = UI->getParent();
1607
1608 // If A comes before B, then B is definitively reachable from A.
1609 if (UI->comesBefore(Store))
1610 return false;
1611
1612 // If the user's parent block is entry, no predecessor exists.
1613 if (BB->isEntryBlock())
1614 return true;
1615
1616 // Otherwise, continue doing the normal per-BB CFG walk.
1617 ReachabilityWorklist.append(succ_begin(BB), succ_end(BB));
1618 } else {
1619 ReachabilityWorklist.push_back(UI->getParent());
1620 }
1621 }
1622 return true;
1623 };
1624
1625 if (!CaptureTrackingWithModRef(DestAlloca, DestModRefCallback))
1626 return false;
1627 // Bailout if Dest may have any ModRef before Store.
1628 if (!ReachabilityWorklist.empty() &&
1629 isPotentiallyReachableFromMany(ReachabilityWorklist, Store->getParent(),
1630 nullptr, DT, nullptr))
1631 return false;
1632
1633 // Check that, from after the Load to the end of the BB,
1634 // - if the dest has any Mod, src has no Ref, and
1635 // - if the dest has any Ref, src has no Mod except full-sized lifetimes.
1636 MemoryLocation SrcLoc(SrcAlloca, LocationSize::precise(Size));
1637
1638 auto SrcModRefCallback = [&](Instruction *UI) -> bool {
1639 // Any ModRef post-dominated by Load doesn't matter, also Load and Store
1640 // themselves can be ignored.
1641 if (PDT->dominates(Load, UI) || UI == Load || UI == Store)
1642 return true;
1643 ModRefInfo Res = BAA.getModRefInfo(UI, SrcLoc);
1644 if ((isModSet(DestModRef) && isRefSet(Res)) ||
1645 (isRefSet(DestModRef) && isModSet(Res)))
1646 return false;
1647
1648 return true;
1649 };
1650
1651 if (!CaptureTrackingWithModRef(SrcAlloca, SrcModRefCallback))
1652 return false;
1653
1654 // We can do the transformation. First, move the SrcAlloca to the start of the
1655 // BB.
1656 if (SrcNotDom)
1657 SrcAlloca->moveBefore(*SrcAlloca->getParent(),
1658 SrcAlloca->getParent()->getFirstInsertionPt());
1659 // Align the allocas appropriately.
1660 SrcAlloca->setAlignment(
1661 std::max(SrcAlloca->getAlign(), DestAlloca->getAlign()));
1662
1663 // Merge the two allocas.
1664 DestAlloca->replaceAllUsesWith(SrcAlloca);
1665 eraseInstruction(DestAlloca);
1666
1667 // Drop metadata on the source alloca.
1668 SrcAlloca->dropUnknownNonDebugMetadata();
1669
1670 // TODO: Reconstruct merged lifetime markers.
1671 // Remove all other lifetime markers. if the original lifetime intrinsics
1672 // exists.
1673 if (!LifetimeMarkers.empty()) {
1674 for (Instruction *I : LifetimeMarkers)
1675 eraseInstruction(I);
1676 }
1677
1678 // As this transformation can cause memory accesses that didn't previously
1679 // alias to begin to alias one another, we remove !noalias metadata from any
1680 // uses of either alloca. This is conservative, but more precision doesn't
1681 // seem worthwhile right now.
1682 for (Instruction *I : NoAliasInstrs)
1683 I->setMetadata(LLVMContext::MD_noalias, nullptr);
1684
1685 LLVM_DEBUG(dbgs() << "Stack Move: Performed staack-move optimization\n");
1686 NumStackMove++;
1687 return true;
1688}
1689
1690static bool isZeroSize(Value *Size) {
1691 if (auto *I = dyn_cast<Instruction>(Size))
1692 if (auto *Res = simplifyInstruction(I, I->getDataLayout()))
1693 Size = Res;
1694 // Treat undef/poison size like zero.
1695 if (auto *C = dyn_cast<Constant>(Size))
1696 return isa<UndefValue>(C) || C->isNullValue();
1697 return false;
1698}
1699
1700/// Perform simplification of memcpy's. If we have memcpy A
1701/// which copies X to Y, and memcpy B which copies Y to Z, then we can rewrite
1702/// B to be a memcpy from X to Z (or potentially a memmove, depending on
1703/// circumstances). This allows later passes to remove the first memcpy
1704/// altogether.
1705bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
1706 // We can only optimize non-volatile memcpy's.
1707 if (M->isVolatile())
1708 return false;
1709
1710 // If the source and destination of the memcpy are the same, then zap it.
1711 if (M->getSource() == M->getDest()) {
1712 ++BBI;
1713 eraseInstruction(M);
1714 return true;
1715 }
1716
1717 // If the size is zero, remove the memcpy.
1718 if (isZeroSize(M->getLength())) {
1719 ++BBI;
1720 eraseInstruction(M);
1721 return true;
1722 }
1723
1724 MemoryUseOrDef *MA = MSSA->getMemoryAccess(M);
1725 if (!MA)
1726 // Degenerate case: memcpy marked as not accessing memory.
1727 return false;
1728
1729 // If copying from a constant, try to turn the memcpy into a memset.
1730 if (auto *GV = dyn_cast<GlobalVariable>(M->getSource()))
1731 if (GV->isConstant() && GV->hasDefinitiveInitializer())
1732 if (Value *ByteVal = isBytewiseValue(GV->getInitializer(),
1733 M->getDataLayout())) {
1734 IRBuilder<> Builder(M);
1735 Instruction *NewM = Builder.CreateMemSet(
1736 M->getRawDest(), ByteVal, M->getLength(), M->getDestAlign(), false);
1737 auto *LastDef = cast<MemoryDef>(MA);
1738 auto *NewAccess =
1739 MSSAU->createMemoryAccessAfter(NewM, nullptr, LastDef);
1740 MSSAU->insertDef(cast<MemoryDef>(NewAccess), /*RenameUses=*/true);
1741
1742 eraseInstruction(M);
1743 ++NumCpyToSet;
1744 return true;
1745 }
1746
1747 BatchAAResults BAA(*AA, EEA);
1748 // FIXME: Not using getClobberingMemoryAccess() here due to PR54682.
1749 MemoryAccess *AnyClobber = MA->getDefiningAccess();
1751 const MemoryAccess *DestClobber =
1752 MSSA->getWalker()->getClobberingMemoryAccess(AnyClobber, DestLoc, BAA);
1753
1754 // Try to turn a partially redundant memset + memcpy into
1755 // smaller memset + memcpy. We don't need the memcpy size for this.
1756 // The memcpy must post-dom the memset, so limit this to the same basic
1757 // block. A non-local generalization is likely not worthwhile.
1758 if (auto *MD = dyn_cast<MemoryDef>(DestClobber))
1759 if (auto *MDep = dyn_cast_or_null<MemSetInst>(MD->getMemoryInst()))
1760 if (DestClobber->getBlock() == M->getParent())
1761 if (processMemSetMemCpyDependence(M, MDep, BAA))
1762 return true;
1763
1764 MemoryAccess *SrcClobber = MSSA->getWalker()->getClobberingMemoryAccess(
1765 AnyClobber, MemoryLocation::getForSource(M), BAA);
1766
1767 // There are five possible optimizations we can do for memcpy:
1768 // a) memcpy-memcpy xform which exposes redundance for DSE.
1769 // b) call-memcpy xform for return slot optimization.
1770 // c) memcpy from freshly alloca'd space or space that has just started
1771 // its lifetime copies undefined data, and we can therefore eliminate
1772 // the memcpy in favor of the data that was already at the destination.
1773 // d) memcpy from a just-memset'd source can be turned into memset.
1774 // e) elimination of memcpy via stack-move optimization.
1775 if (auto *MD = dyn_cast<MemoryDef>(SrcClobber)) {
1776 if (Instruction *MI = MD->getMemoryInst()) {
1777 if (auto *CopySize = dyn_cast<ConstantInt>(M->getLength())) {
1778 if (auto *C = dyn_cast<CallInst>(MI)) {
1779 if (performCallSlotOptzn(M, M, M->getDest(), M->getSource(),
1780 TypeSize::getFixed(CopySize->getZExtValue()),
1781 M->getDestAlign().valueOrOne(), BAA,
1782 [C]() -> CallInst * { return C; })) {
1783 LLVM_DEBUG(dbgs() << "Performed call slot optimization:\n"
1784 << " call: " << *C << "\n"
1785 << " memcpy: " << *M << "\n");
1786 eraseInstruction(M);
1787 ++NumMemCpyInstr;
1788 return true;
1789 }
1790 }
1791 }
1792 if (auto *MDep = dyn_cast<MemCpyInst>(MI))
1793 if (processMemCpyMemCpyDependence(M, MDep, BAA))
1794 return true;
1795 if (auto *MDep = dyn_cast<MemSetInst>(MI)) {
1796 if (performMemCpyToMemSetOptzn(M, MDep, BAA)) {
1797 LLVM_DEBUG(dbgs() << "Converted memcpy to memset\n");
1798 eraseInstruction(M);
1799 ++NumCpyToSet;
1800 return true;
1801 }
1802 }
1803 }
1804
1805 if (hasUndefContents(MSSA, BAA, M->getSource(), MD, M->getLength())) {
1806 LLVM_DEBUG(dbgs() << "Removed memcpy from undef\n");
1807 eraseInstruction(M);
1808 ++NumMemCpyInstr;
1809 return true;
1810 }
1811 }
1812
1813 // If the transfer is from a stack slot to a stack slot, then we may be able
1814 // to perform the stack-move optimization. See the comments in
1815 // performStackMoveOptzn() for more details.
1816 auto *DestAlloca = dyn_cast<AllocaInst>(M->getDest());
1817 if (!DestAlloca)
1818 return false;
1819 auto *SrcAlloca = dyn_cast<AllocaInst>(M->getSource());
1820 if (!SrcAlloca)
1821 return false;
1822 ConstantInt *Len = dyn_cast<ConstantInt>(M->getLength());
1823 if (Len == nullptr)
1824 return false;
1825 if (performStackMoveOptzn(M, M, DestAlloca, SrcAlloca,
1826 TypeSize::getFixed(Len->getZExtValue()), BAA)) {
1827 // Avoid invalidating the iterator.
1828 BBI = M->getNextNonDebugInstruction()->getIterator();
1829 eraseInstruction(M);
1830 ++NumMemCpyInstr;
1831 return true;
1832 }
1833
1834 return false;
1835}
1836
1837/// Memmove calls with overlapping src/dest buffers that come after a memset may
1838/// be removed.
1839bool MemCpyOptPass::isMemMoveMemSetDependency(MemMoveInst *M) {
1840 const auto &DL = M->getDataLayout();
1841 MemoryUseOrDef *MemMoveAccess = MSSA->getMemoryAccess(M);
1842 if (!MemMoveAccess)
1843 return false;
1844
1845 // The memmove is of form memmove(x, x + A, B).
1847 auto *MemMoveSourceOp = M->getSource();
1848 auto *Source = dyn_cast<GEPOperator>(MemMoveSourceOp);
1849 if (!Source)
1850 return false;
1851
1852 APInt Offset(DL.getIndexTypeSizeInBits(Source->getType()), 0);
1853 LocationSize MemMoveLocSize = SourceLoc.Size;
1854 if (Source->getPointerOperand() != M->getDest() ||
1855 !MemMoveLocSize.hasValue() ||
1856 !Source->accumulateConstantOffset(DL, Offset) || Offset.isNegative()) {
1857 return false;
1858 }
1859
1860 uint64_t MemMoveSize = MemMoveLocSize.getValue();
1861 LocationSize TotalSize =
1862 LocationSize::precise(Offset.getZExtValue() + MemMoveSize);
1863 MemoryLocation CombinedLoc(M->getDest(), TotalSize);
1864
1865 // The first dominating clobbering MemoryAccess for the combined location
1866 // needs to be a memset.
1867 BatchAAResults BAA(*AA);
1868 MemoryAccess *FirstDef = MemMoveAccess->getDefiningAccess();
1869 auto *DestClobber = dyn_cast<MemoryDef>(
1870 MSSA->getWalker()->getClobberingMemoryAccess(FirstDef, CombinedLoc, BAA));
1871 if (!DestClobber)
1872 return false;
1873
1874 auto *MS = dyn_cast_or_null<MemSetInst>(DestClobber->getMemoryInst());
1875 if (!MS)
1876 return false;
1877
1878 // Memset length must be sufficiently large.
1879 auto *MemSetLength = dyn_cast<ConstantInt>(MS->getLength());
1880 if (!MemSetLength || MemSetLength->getZExtValue() < MemMoveSize)
1881 return false;
1882
1883 // The destination buffer must have been memset'd.
1884 if (!BAA.isMustAlias(MS->getDest(), M->getDest()))
1885 return false;
1886
1887 return true;
1888}
1889
1890/// Transforms memmove calls to memcpy calls when the src/dst are guaranteed
1891/// not to alias.
1892bool MemCpyOptPass::processMemMove(MemMoveInst *M, BasicBlock::iterator &BBI) {
1893 // See if the source could be modified by this memmove potentially.
1895 // On the off-chance the memmove clobbers src with previously memset'd
1896 // bytes, the memmove may be redundant.
1897 if (!M->isVolatile() && isMemMoveMemSetDependency(M)) {
1898 LLVM_DEBUG(dbgs() << "Removed redundant memmove.\n");
1899 ++BBI;
1900 eraseInstruction(M);
1901 ++NumMemMoveInstr;
1902 return true;
1903 }
1904 return false;
1905 }
1906
1907 LLVM_DEBUG(dbgs() << "MemCpyOptPass: Optimizing memmove -> memcpy: " << *M
1908 << "\n");
1909
1910 // If not, then we know we can transform this.
1911 Type *ArgTys[3] = {M->getRawDest()->getType(), M->getRawSource()->getType(),
1912 M->getLength()->getType()};
1913 M->setCalledFunction(Intrinsic::getOrInsertDeclaration(
1914 M->getModule(), Intrinsic::memcpy, ArgTys));
1915
1916 // For MemorySSA nothing really changes (except that memcpy may imply stricter
1917 // aliasing guarantees).
1918
1919 ++NumMoveToCpy;
1920 return true;
1921}
1922
1923/// This is called on every byval argument in call sites.
1924bool MemCpyOptPass::processByValArgument(CallBase &CB, unsigned ArgNo) {
1925 const DataLayout &DL = CB.getDataLayout();
1926 // Find out what feeds this byval argument.
1927 Value *ByValArg = CB.getArgOperand(ArgNo);
1928 Type *ByValTy = CB.getParamByValType(ArgNo);
1929 TypeSize ByValSize = DL.getTypeAllocSize(ByValTy);
1930 MemoryLocation Loc(ByValArg, LocationSize::precise(ByValSize));
1931 MemoryUseOrDef *CallAccess = MSSA->getMemoryAccess(&CB);
1932 if (!CallAccess)
1933 return false;
1934 MemCpyInst *MDep = nullptr;
1935 BatchAAResults BAA(*AA, EEA);
1937 CallAccess->getDefiningAccess(), Loc, BAA);
1938 if (auto *MD = dyn_cast<MemoryDef>(Clobber))
1939 MDep = dyn_cast_or_null<MemCpyInst>(MD->getMemoryInst());
1940
1941 // If the byval argument isn't fed by a memcpy, ignore it. If it is fed by
1942 // a memcpy, see if we can byval from the source of the memcpy instead of the
1943 // result.
1944 if (!MDep || MDep->isVolatile() ||
1945 ByValArg->stripPointerCasts() != MDep->getDest())
1946 return false;
1947
1948 // The length of the memcpy must be larger or equal to the size of the byval.
1949 auto *C1 = dyn_cast<ConstantInt>(MDep->getLength());
1950 if (!C1 || !TypeSize::isKnownGE(
1951 TypeSize::getFixed(C1->getValue().getZExtValue()), ByValSize))
1952 return false;
1953
1954 // Get the alignment of the byval. If the call doesn't specify the alignment,
1955 // then it is some target specific value that we can't know.
1956 MaybeAlign ByValAlign = CB.getParamAlign(ArgNo);
1957 if (!ByValAlign)
1958 return false;
1959
1960 // If it is greater than the memcpy, then we check to see if we can force the
1961 // source of the memcpy to the alignment we need. If we fail, we bail out.
1962 MaybeAlign MemDepAlign = MDep->getSourceAlign();
1963 if ((!MemDepAlign || *MemDepAlign < *ByValAlign) &&
1964 getOrEnforceKnownAlignment(MDep->getSource(), ByValAlign, DL, &CB, AC,
1965 DT) < *ByValAlign)
1966 return false;
1967
1968 // The type of the memcpy source must match the byval argument
1969 if (MDep->getSource()->getType() != ByValArg->getType())
1970 return false;
1971
1972 // Verify that the copied-from memory doesn't change in between the memcpy and
1973 // the byval call.
1974 // memcpy(a <- b)
1975 // *b = 42;
1976 // foo(*a)
1977 // It would be invalid to transform the second memcpy into foo(*b).
1978 if (writtenBetween(MSSA, BAA, MemoryLocation::getForSource(MDep),
1979 MSSA->getMemoryAccess(MDep), CallAccess))
1980 return false;
1981
1982 LLVM_DEBUG(dbgs() << "MemCpyOptPass: Forwarding memcpy to byval:\n"
1983 << " " << *MDep << "\n"
1984 << " " << CB << "\n");
1985
1986 // Otherwise we're good! Update the byval argument.
1987 combineAAMetadata(&CB, MDep);
1988 CB.setArgOperand(ArgNo, MDep->getSource());
1989 ++NumMemCpyInstr;
1990 return true;
1991}
1992
1993/// This is called on memcpy dest pointer arguments attributed as immutable
1994/// during call. Try to use memcpy source directly if all of the following
1995/// conditions are satisfied.
1996/// 1. The memcpy dst is neither modified during the call nor captured by the
1997/// call.
1998/// 2. The memcpy dst is an alloca with known alignment & size.
1999/// 2-1. The memcpy length == the alloca size which ensures that the new
2000/// pointer is dereferenceable for the required range
2001/// 2-2. The src pointer has alignment >= the alloca alignment or can be
2002/// enforced so.
2003/// 3. The memcpy dst and src is not modified between the memcpy and the call.
2004/// (if MSSA clobber check is safe.)
2005/// 4. The memcpy src is not modified during the call. (ModRef check shows no
2006/// Mod.)
2007bool MemCpyOptPass::processImmutArgument(CallBase &CB, unsigned ArgNo) {
2008 BatchAAResults BAA(*AA, EEA);
2009 Value *ImmutArg = CB.getArgOperand(ArgNo);
2010
2011 // 1. Ensure passed argument is immutable during call.
2012 if (!CB.doesNotCapture(ArgNo))
2013 return false;
2014
2015 // We know that the argument is readonly at this point, but the function
2016 // might still modify the same memory through a different pointer. Exclude
2017 // this either via noalias, or alias analysis.
2018 if (!CB.paramHasAttr(ArgNo, Attribute::NoAlias) &&
2019 isModSet(
2021 return false;
2022
2023 const DataLayout &DL = CB.getDataLayout();
2024
2025 // 2. Check that arg is alloca
2026 // TODO: Even if the arg gets back to branches, we can remove memcpy if all
2027 // the alloca alignments can be enforced to source alignment.
2028 auto *AI = dyn_cast<AllocaInst>(ImmutArg->stripPointerCasts());
2029 if (!AI)
2030 return false;
2031
2032 std::optional<TypeSize> AllocaSize = AI->getAllocationSize(DL);
2033 // Can't handle unknown size alloca.
2034 // (e.g. Variable Length Array, Scalable Vector)
2035 if (!AllocaSize || AllocaSize->isScalable())
2036 return false;
2037 MemoryLocation Loc(ImmutArg, LocationSize::precise(*AllocaSize));
2038 MemoryUseOrDef *CallAccess = MSSA->getMemoryAccess(&CB);
2039 if (!CallAccess)
2040 return false;
2041
2042 MemCpyInst *MDep = nullptr;
2044 CallAccess->getDefiningAccess(), Loc, BAA);
2045 if (auto *MD = dyn_cast<MemoryDef>(Clobber))
2046 MDep = dyn_cast_or_null<MemCpyInst>(MD->getMemoryInst());
2047
2048 // If the immut argument isn't fed by a memcpy, ignore it. If it is fed by
2049 // a memcpy, check that the arg equals the memcpy dest.
2050 if (!MDep || MDep->isVolatile() || AI != MDep->getDest())
2051 return false;
2052
2053 // The type of the memcpy source must match the immut argument
2054 if (MDep->getSource()->getType() != ImmutArg->getType())
2055 return false;
2056
2057 // 2-1. The length of the memcpy must be equal to the size of the alloca.
2058 auto *MDepLen = dyn_cast<ConstantInt>(MDep->getLength());
2059 if (!MDepLen || AllocaSize != MDepLen->getValue())
2060 return false;
2061
2062 // 2-2. the memcpy source align must be larger than or equal the alloca's
2063 // align. If not so, we check to see if we can force the source of the memcpy
2064 // to the alignment we need. If we fail, we bail out.
2065 Align MemDepAlign = MDep->getSourceAlign().valueOrOne();
2066 Align AllocaAlign = AI->getAlign();
2067 if (MemDepAlign < AllocaAlign &&
2068 getOrEnforceKnownAlignment(MDep->getSource(), AllocaAlign, DL, &CB, AC,
2069 DT) < AllocaAlign)
2070 return false;
2071
2072 // 3. Verify that the source doesn't change in between the memcpy and
2073 // the call.
2074 // memcpy(a <- b)
2075 // *b = 42;
2076 // foo(*a)
2077 // It would be invalid to transform the second memcpy into foo(*b).
2078 if (writtenBetween(MSSA, BAA, MemoryLocation::getForSource(MDep),
2079 MSSA->getMemoryAccess(MDep), CallAccess))
2080 return false;
2081
2082 // 4. The memcpy src must not be modified during the call.
2084 return false;
2085
2086 LLVM_DEBUG(dbgs() << "MemCpyOptPass: Forwarding memcpy to Immut src:\n"
2087 << " " << *MDep << "\n"
2088 << " " << CB << "\n");
2089
2090 // Otherwise we're good! Update the immut argument.
2091 combineAAMetadata(&CB, MDep);
2092 CB.setArgOperand(ArgNo, MDep->getSource());
2093 ++NumMemCpyInstr;
2094 return true;
2095}
2096
2097/// Executes one iteration of MemCpyOptPass.
2098bool MemCpyOptPass::iterateOnFunction(Function &F) {
2099 bool MadeChange = false;
2100
2101 // Walk all instruction in the function.
2102 for (BasicBlock &BB : F) {
2103 // Skip unreachable blocks. For example processStore assumes that an
2104 // instruction in a BB can't be dominated by a later instruction in the
2105 // same BB (which is a scenario that can happen for an unreachable BB that
2106 // has itself as a predecessor).
2107 if (!DT->isReachableFromEntry(&BB))
2108 continue;
2109
2110 for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) {
2111 // Avoid invalidating the iterator.
2112 Instruction *I = &*BI++;
2113
2114 bool RepeatInstruction = false;
2115
2116 if (auto *SI = dyn_cast<StoreInst>(I))
2117 MadeChange |= processStore(SI, BI);
2118 else if (auto *M = dyn_cast<MemSetInst>(I))
2119 RepeatInstruction = processMemSet(M, BI);
2120 else if (auto *M = dyn_cast<MemCpyInst>(I))
2121 RepeatInstruction = processMemCpy(M, BI);
2122 else if (auto *M = dyn_cast<MemMoveInst>(I))
2123 RepeatInstruction = processMemMove(M, BI);
2124 else if (auto *CB = dyn_cast<CallBase>(I)) {
2125 for (unsigned i = 0, e = CB->arg_size(); i != e; ++i) {
2126 if (CB->isByValArgument(i))
2127 MadeChange |= processByValArgument(*CB, i);
2128 else if (CB->onlyReadsMemory(i))
2129 MadeChange |= processImmutArgument(*CB, i);
2130 }
2131 }
2132
2133 // Reprocess the instruction if desired.
2134 if (RepeatInstruction) {
2135 if (BI != BB.begin())
2136 --BI;
2137 MadeChange = true;
2138 }
2139 }
2140 }
2141
2142 return MadeChange;
2143}
2144
2146 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
2147 auto *AA = &AM.getResult<AAManager>(F);
2148 auto *AC = &AM.getResult<AssumptionAnalysis>(F);
2149 auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
2150 auto *PDT = &AM.getResult<PostDominatorTreeAnalysis>(F);
2151 auto *MSSA = &AM.getResult<MemorySSAAnalysis>(F);
2152
2153 bool MadeChange = runImpl(F, &TLI, AA, AC, DT, PDT, &MSSA->getMSSA());
2154 if (!MadeChange)
2155 return PreservedAnalyses::all();
2156
2160 return PA;
2161}
2162
2164 AliasAnalysis *AA_, AssumptionCache *AC_,
2165 DominatorTree *DT_, PostDominatorTree *PDT_,
2166 MemorySSA *MSSA_) {
2167 bool MadeChange = false;
2168 TLI = TLI_;
2169 AA = AA_;
2170 AC = AC_;
2171 DT = DT_;
2172 PDT = PDT_;
2173 MSSA = MSSA_;
2174 MemorySSAUpdater MSSAU_(MSSA_);
2175 MSSAU = &MSSAU_;
2176 EarliestEscapeAnalysis EEA_(*DT);
2177 EEA = &EEA_;
2178
2179 while (true) {
2180 if (!iterateOnFunction(F))
2181 break;
2182 MadeChange = true;
2183 }
2184
2185 if (VerifyMemorySSA)
2186 MSSA_->verifyMemorySSA();
2187
2188 return MadeChange;
2189}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define LLVM_DEBUG(...)
Definition: Debug.h:106
This file defines the DenseSet and SmallDenseSet classes.
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
This is the interface for a simple mod/ref and alias analysis over globals.
Hexagon Common GEP
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.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
static bool mayBeVisibleThroughUnwinding(Value *V, Instruction *Start, Instruction *End)
static bool isZeroSize(Value *Size)
static bool accessedBetween(BatchAAResults &AA, MemoryLocation Loc, const MemoryUseOrDef *Start, const MemoryUseOrDef *End, Instruction **SkippedLifetimeStart=nullptr)
static bool hasUndefContents(MemorySSA *MSSA, BatchAAResults &AA, Value *V, MemoryDef *Def, Value *Size)
Determine whether the instruction has undefined content for the given Size, either because it was fre...
static cl::opt< bool > EnableMemCpyOptWithoutLibcalls("enable-memcpyopt-without-libcalls", cl::Hidden, cl::desc("Enable memcpyopt even when libcalls are disabled"))
static bool writtenBetween(MemorySSA *MSSA, BatchAAResults &AA, MemoryLocation Loc, const MemoryUseOrDef *Start, const MemoryUseOrDef *End)
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...
static void addRange(SmallVectorImpl< ConstantInt * > &EndPoints, ConstantInt *Low, ConstantInt *High)
Definition: Metadata.cpp:1274
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
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:166
A manager for alias analyses.
ModRefInfo getModRefInfo(const Instruction *I, const std::optional< MemoryLocation > &OptLoc)
Check whether or not an instruction may read or write the optionally specified memory location.
Class for arbitrary precision integers.
Definition: APInt.h:78
an instruction to allocate memory on the stack
Definition: Instructions.h:63
bool isStaticAlloca() const
Return true if this alloca is in the entry block of the function and is a constant size.
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Definition: Instructions.h:124
unsigned getAddressSpace() const
Return the address space for the allocation.
Definition: Instructions.h:104
std::optional< TypeSize > getAllocationSize(const DataLayout &DL) const
Get allocation size in bytes.
void setAlignment(Align Align)
Definition: Instructions.h:128
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:410
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
iterator end()
Definition: BasicBlock.h:474
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:461
bool isEntryBlock() const
Return true if this is the entry block of the containing function.
Definition: BasicBlock.cpp:593
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:220
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:177
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
bool isMustAlias(const MemoryLocation &LocA, const MemoryLocation &LocB)
ModRefInfo getModRefInfo(const Instruction *I, const std::optional< MemoryLocation > &OptLoc)
ModRefInfo callCapturesBefore(const Instruction *I, const MemoryLocation &MemLoc, DominatorTree *DT)
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:72
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1112
bool doesNotCapture(unsigned OpNo) const
Determine whether this data operand is not captured.
Definition: InstrTypes.h:1691
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
bool isByValArgument(unsigned ArgNo) const
Determine whether this argument is passed by value.
Definition: InstrTypes.h:1696
MaybeAlign getParamAlign(unsigned ArgNo) const
Extract the alignment for a call or parameter (0=unknown).
Definition: InstrTypes.h:1765
bool onlyReadsMemory(unsigned OpNo) const
Definition: InstrTypes.h:1738
Type * getParamByValType(unsigned ArgNo) const
Extract the byval type for a call or parameter.
Definition: InstrTypes.h:1783
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1286
void setArgOperand(unsigned i, Value *v)
Definition: InstrTypes.h:1291
unsigned arg_size() const
Definition: InstrTypes.h:1284
This class represents a function call, abstracting a target machine's calling convention.
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:157
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:373
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
Implements a dense probed hash-table based set.
Definition: DenseSet.h:278
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:279
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:321
bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Definition: Dominators.cpp:122
Context-sensitive CaptureAnalysis provider, which computes and caches the earliest common dominator c...
void removeInstruction(Instruction *I)
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2705
void mergeDIAssignID(ArrayRef< const Instruction * > SourceInstructions)
Merge the DIAssignID metadata from this instruction and those attached to instructions in SourceInstr...
Definition: DebugInfo.cpp:957
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:511
void dropUnknownNonDebugMetadata(ArrayRef< unsigned > KnownIDs={})
Drop all unknown metadata except for debug locations.
Definition: Metadata.cpp:1633
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:508
void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
Definition: Instruction.cpp:76
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
An instruction for reading from memory.
Definition: Instructions.h:176
Value * getPointerOperand()
Definition: Instructions.h:255
bool isSimple() const
Definition: Instructions.h:247
Align getAlign() const
Return the alignment of the access that is being performed.
Definition: Instructions.h:211
bool hasValue() const
static LocationSize precise(uint64_t Value)
TypeSize getValue() const
This class wraps the llvm.memcpy intrinsic.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
bool runImpl(Function &F, TargetLibraryInfo *TLI, AAResults *AA, AssumptionCache *AC, DominatorTree *DT, PostDominatorTree *PDT, MemorySSA *MSSA)
Value * getLength() const
Value * getRawDest() const
Value * getDest() const
This is just like getRawDest, but it strips off any cast instructions (including addrspacecast) that ...
MaybeAlign getDestAlign() const
bool isVolatile() const
This class wraps the llvm.memmove intrinsic.
Value * getValue() const
This class wraps the llvm.memset and llvm.memset.inline intrinsics.
Value * getRawSource() const
Return the arguments to the instruction.
MaybeAlign getSourceAlign() const
Value * getSource() const
This is just like getRawSource, but it strips off any cast instructions that feed it,...
BasicBlock * getBlock() const
Definition: MemorySSA.h:161
Represents a read-write access to memory, whether it is a must-alias, or a may-alias.
Definition: MemorySSA.h:370
Representation for a specific memory location.
MemoryLocation getWithNewSize(LocationSize NewSize) const
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
static MemoryLocation getForSource(const MemTransferInst *MTI)
Return a location representing the source of a memory transfer.
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 getForDest(const MemIntrinsic *MI)
Return a location representing the destination of a memory set or transfer.
An analysis that produces MemorySSA for a function.
Definition: MemorySSA.h:928
MemoryUseOrDef * createMemoryAccessBefore(Instruction *I, MemoryAccess *Definition, MemoryUseOrDef *InsertPt)
Create a MemoryAccess in MemorySSA before an existing MemoryAccess.
void insertDef(MemoryDef *Def, bool RenameUses=false)
Insert a definition into the MemorySSA IR.
void moveAfter(MemoryUseOrDef *What, MemoryUseOrDef *Where)
void removeMemoryAccess(MemoryAccess *, bool OptimizePhis=false)
Remove a MemoryAccess from MemorySSA, including updating all definitions and uses.
MemoryUseOrDef * createMemoryAccessAfter(Instruction *I, MemoryAccess *Definition, MemoryAccess *InsertPt)
Create a MemoryAccess in MemorySSA after an existing MemoryAccess.
void moveBefore(MemoryUseOrDef *What, MemoryUseOrDef *Where)
MemoryAccess * getClobberingMemoryAccess(const Instruction *I, BatchAAResults &AA)
Given a memory Mod/Ref/ModRef'ing instruction, calling this will give you the nearest dominating Memo...
Definition: MemorySSA.h:1045
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition: MemorySSA.h:701
bool dominates(const MemoryAccess *A, const MemoryAccess *B) const
Given two memory accesses in potentially different blocks, determine whether MemoryAccess A dominates...
Definition: MemorySSA.cpp:2173
void verifyMemorySSA(VerificationLevel=VerificationLevel::Fast) const
Verify that MemorySSA is self consistent (IE definitions dominate all uses, uses appear in the right ...
Definition: MemorySSA.cpp:1905
MemorySSAWalker * getWalker()
Definition: MemorySSA.cpp:1590
MemoryUseOrDef * getMemoryAccess(const Instruction *I) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
Definition: MemorySSA.h:719
bool isLiveOnEntryDef(const MemoryAccess *MA) const
Return true if MA represents the live on entry value.
Definition: MemorySSA.h:739
Class that has the common methods + fields of memory uses/defs.
Definition: MemorySSA.h:249
MemoryAccess * getDefiningAccess() const
Get the access that produces the memory state used by this Use.
Definition: MemorySSA.h:259
Instruction * getMemoryInst() const
Get the instruction that this MemoryUse represents.
Definition: MemorySSA.h:256
Analysis pass which computes a PostDominatorTree.
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
bool dominates(const Instruction *I1, const Instruction *I2) const
Return true if I1 dominates I2.
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
void preserveSet()
Mark an analysis set as preserved.
Definition: Analysis.h:146
void preserve()
Mark an analysis as preserved.
Definition: Analysis.h:131
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:132
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:181
size_type size() const
Definition: SmallSet.h:170
bool empty() const
Definition: SmallVector.h:81
void reserve(size_type N)
Definition: SmallVector.h:663
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:578
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:683
typename SuperClass::iterator iterator
Definition: SmallVector.h:577
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
An instruction for storing to memory.
Definition: Instructions.h:292
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
bool has(LibFunc F) const
Tests whether a library function is available.
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition: TypeSize.h:345
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
unsigned getIntegerBitWidth() const
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:355
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
Value * getOperand(unsigned i) const
Definition: User.h:228
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:434
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:694
bool use_empty() const
Definition: Value.h:344
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:202
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:171
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:239
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
Definition: ilist_node.h:32
reverse_self_iterator getReverseIterator()
Definition: ilist_node.h:135
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
Definition: Intrinsics.cpp:756
const_iterator begin(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:226
const_iterator end(StringRef path LLVM_LIFETIME_BOUND)
Get end iterator over path.
Definition: Path.cpp:235
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
bool isPotentiallyReachableFromMany(SmallVectorImpl< BasicBlock * > &Worklist, const BasicBlock *StopBB, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether there is at least one path from a block in 'Worklist' to 'StopBB' without passing t...
Definition: CFG.cpp:239
bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition: Loads.cpp:217
UseCaptureKind DetermineUseCaptureKind(const Use &U, llvm::function_ref< bool(Value *, const DataLayout &)> IsDereferenceableOrNull)
Determine what kind of capture behaviour U may exhibit.
auto partition_point(R &&Range, Predicate P)
Binary search for the first iterator in a range where a predicate is false.
Definition: STLExtras.h:2050
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
bool PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures, bool StoreCaptures, const Instruction *I, const DominatorTree *DT, bool IncludeI=false, unsigned MaxUsesToExplore=0, const LoopInfo *LI=nullptr)
PointerMayBeCapturedBefore - Return true if this pointer value may be captured by the enclosing funct...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2115
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
unsigned getDefaultMaxUsesToExploreForCaptureTracking()
getDefaultMaxUsesToExploreForCaptureTracking - Return default value of the maximal number of uses to ...
Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
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:1746
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:420
Align getOrEnforceKnownAlignment(Value *V, MaybeAlign PrefAlign, const DataLayout &DL, const Instruction *CxtI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr)
Try to ensure that the alignment of V is at least PrefAlign bytes.
Definition: Local.cpp:1578
bool isModSet(const ModRefInfo MRI)
Definition: ModRef.h:48
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
bool isModOrRefSet(const ModRefInfo MRI)
Definition: ModRef.h:42
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...
bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition: ModRef.h:27
@ NoModRef
The access neither references nor modifies the value stored in memory.
bool VerifyMemorySSA
Enables verification of MemorySSA.
Definition: MemorySSA.cpp:84
RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)
bool isIdentifiedFunctionLocal(const Value *V)
Return true if V is umabigously identified at the function-level.
bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition: Casting.h:565
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...
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition: Alignment.h:212
void combineAAMetadata(Instruction *K, const Instruction *J)
Combine metadata of two instructions, where instruction J is a memory access that has been merged int...
Definition: Local.cpp:3441
bool isRefSet(const ModRefInfo MRI)
Definition: ModRef.h:51
bool isWritableObject(const Value *Object, bool &ExplicitlyDereferenceableOnly)
Return true if the Object is writable, in the sense that any location based on this pointer that can ...
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition: Alignment.h:141