1 //===- MemCpyOptimizer.cpp - Optimize use of memcpy and friends -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass performs various transformations related to eliminating memcpy
11 // calls, or transforming sets of stores into memset's.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Transforms/Scalar.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Analysis/AliasAnalysis.h"
20 #include "llvm/Analysis/AssumptionCache.h"
21 #include "llvm/Analysis/GlobalsModRef.h"
22 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
23 #include "llvm/Analysis/TargetLibraryInfo.h"
24 #include "llvm/Analysis/ValueTracking.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/Dominators.h"
27 #include "llvm/IR/GetElementPtrTypeIterator.h"
28 #include "llvm/IR/GlobalVariable.h"
29 #include "llvm/IR/IRBuilder.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/IntrinsicInst.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Transforms/Utils/Local.h"
35 #include <algorithm>
36 using namespace llvm;
37 
38 #define DEBUG_TYPE "memcpyopt"
39 
40 STATISTIC(NumMemCpyInstr, "Number of memcpy instructions deleted");
41 STATISTIC(NumMemSetInfer, "Number of memsets inferred");
42 STATISTIC(NumMoveToCpy,   "Number of memmoves converted to memcpy");
43 STATISTIC(NumCpyToSet,    "Number of memcpys converted to memset");
44 
45 static int64_t GetOffsetFromIndex(const GEPOperator *GEP, unsigned Idx,
46                                   bool &VariableIdxFound,
47                                   const DataLayout &DL) {
48   // Skip over the first indices.
49   gep_type_iterator GTI = gep_type_begin(GEP);
50   for (unsigned i = 1; i != Idx; ++i, ++GTI)
51     /*skip along*/;
52 
53   // Compute the offset implied by the rest of the indices.
54   int64_t Offset = 0;
55   for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
56     ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i));
57     if (!OpC)
58       return VariableIdxFound = true;
59     if (OpC->isZero()) continue;  // No offset.
60 
61     // Handle struct indices, which add their field offset to the pointer.
62     if (StructType *STy = dyn_cast<StructType>(*GTI)) {
63       Offset += DL.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
64       continue;
65     }
66 
67     // Otherwise, we have a sequential type like an array or vector.  Multiply
68     // the index by the ElementSize.
69     uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType());
70     Offset += Size*OpC->getSExtValue();
71   }
72 
73   return Offset;
74 }
75 
76 /// Return true if Ptr1 is provably equal to Ptr2 plus a constant offset, and
77 /// return that constant offset. For example, Ptr1 might be &A[42], and Ptr2
78 /// might be &A[40]. In this case offset would be -8.
79 static bool IsPointerOffset(Value *Ptr1, Value *Ptr2, int64_t &Offset,
80                             const DataLayout &DL) {
81   Ptr1 = Ptr1->stripPointerCasts();
82   Ptr2 = Ptr2->stripPointerCasts();
83 
84   // Handle the trivial case first.
85   if (Ptr1 == Ptr2) {
86     Offset = 0;
87     return true;
88   }
89 
90   GEPOperator *GEP1 = dyn_cast<GEPOperator>(Ptr1);
91   GEPOperator *GEP2 = dyn_cast<GEPOperator>(Ptr2);
92 
93   bool VariableIdxFound = false;
94 
95   // If one pointer is a GEP and the other isn't, then see if the GEP is a
96   // constant offset from the base, as in "P" and "gep P, 1".
97   if (GEP1 && !GEP2 && GEP1->getOperand(0)->stripPointerCasts() == Ptr2) {
98     Offset = -GetOffsetFromIndex(GEP1, 1, VariableIdxFound, DL);
99     return !VariableIdxFound;
100   }
101 
102   if (GEP2 && !GEP1 && GEP2->getOperand(0)->stripPointerCasts() == Ptr1) {
103     Offset = GetOffsetFromIndex(GEP2, 1, VariableIdxFound, DL);
104     return !VariableIdxFound;
105   }
106 
107   // Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical
108   // base.  After that base, they may have some number of common (and
109   // potentially variable) indices.  After that they handle some constant
110   // offset, which determines their offset from each other.  At this point, we
111   // handle no other case.
112   if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0))
113     return false;
114 
115   // Skip any common indices and track the GEP types.
116   unsigned Idx = 1;
117   for (; Idx != GEP1->getNumOperands() && Idx != GEP2->getNumOperands(); ++Idx)
118     if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx))
119       break;
120 
121   int64_t Offset1 = GetOffsetFromIndex(GEP1, Idx, VariableIdxFound, DL);
122   int64_t Offset2 = GetOffsetFromIndex(GEP2, Idx, VariableIdxFound, DL);
123   if (VariableIdxFound) return false;
124 
125   Offset = Offset2-Offset1;
126   return true;
127 }
128 
129 
130 /// Represents a range of memset'd bytes with the ByteVal value.
131 /// This allows us to analyze stores like:
132 ///   store 0 -> P+1
133 ///   store 0 -> P+0
134 ///   store 0 -> P+3
135 ///   store 0 -> P+2
136 /// which sometimes happens with stores to arrays of structs etc.  When we see
137 /// the first store, we make a range [1, 2).  The second store extends the range
138 /// to [0, 2).  The third makes a new range [2, 3).  The fourth store joins the
139 /// two ranges into [0, 3) which is memset'able.
140 namespace {
141 struct MemsetRange {
142   // Start/End - A semi range that describes the span that this range covers.
143   // The range is closed at the start and open at the end: [Start, End).
144   int64_t Start, End;
145 
146   /// StartPtr - The getelementptr instruction that points to the start of the
147   /// range.
148   Value *StartPtr;
149 
150   /// Alignment - The known alignment of the first store.
151   unsigned Alignment;
152 
153   /// TheStores - The actual stores that make up this range.
154   SmallVector<Instruction*, 16> TheStores;
155 
156   bool isProfitableToUseMemset(const DataLayout &DL) const;
157 };
158 } // end anon namespace
159 
160 bool MemsetRange::isProfitableToUseMemset(const DataLayout &DL) const {
161   // If we found more than 4 stores to merge or 16 bytes, use memset.
162   if (TheStores.size() >= 4 || End-Start >= 16) return true;
163 
164   // If there is nothing to merge, don't do anything.
165   if (TheStores.size() < 2) return false;
166 
167   // If any of the stores are a memset, then it is always good to extend the
168   // memset.
169   for (Instruction *SI : TheStores)
170     if (!isa<StoreInst>(SI))
171       return true;
172 
173   // Assume that the code generator is capable of merging pairs of stores
174   // together if it wants to.
175   if (TheStores.size() == 2) return false;
176 
177   // If we have fewer than 8 stores, it can still be worthwhile to do this.
178   // For example, merging 4 i8 stores into an i32 store is useful almost always.
179   // However, merging 2 32-bit stores isn't useful on a 32-bit architecture (the
180   // memset will be split into 2 32-bit stores anyway) and doing so can
181   // pessimize the llvm optimizer.
182   //
183   // Since we don't have perfect knowledge here, make some assumptions: assume
184   // the maximum GPR width is the same size as the largest legal integer
185   // size. If so, check to see whether we will end up actually reducing the
186   // number of stores used.
187   unsigned Bytes = unsigned(End-Start);
188   unsigned MaxIntSize = DL.getLargestLegalIntTypeSizeInBits() / 8;
189   if (MaxIntSize == 0)
190     MaxIntSize = 1;
191   unsigned NumPointerStores = Bytes / MaxIntSize;
192 
193   // Assume the remaining bytes if any are done a byte at a time.
194   unsigned NumByteStores = Bytes % MaxIntSize;
195 
196   // If we will reduce the # stores (according to this heuristic), do the
197   // transformation.  This encourages merging 4 x i8 -> i32 and 2 x i16 -> i32
198   // etc.
199   return TheStores.size() > NumPointerStores+NumByteStores;
200 }
201 
202 
203 namespace {
204 class MemsetRanges {
205   /// A sorted list of the memset ranges.
206   SmallVector<MemsetRange, 8> Ranges;
207   typedef SmallVectorImpl<MemsetRange>::iterator range_iterator;
208   const DataLayout &DL;
209 public:
210   MemsetRanges(const DataLayout &DL) : DL(DL) {}
211 
212   typedef SmallVectorImpl<MemsetRange>::const_iterator const_iterator;
213   const_iterator begin() const { return Ranges.begin(); }
214   const_iterator end() const { return Ranges.end(); }
215   bool empty() const { return Ranges.empty(); }
216 
217   void addInst(int64_t OffsetFromFirst, Instruction *Inst) {
218     if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
219       addStore(OffsetFromFirst, SI);
220     else
221       addMemSet(OffsetFromFirst, cast<MemSetInst>(Inst));
222   }
223 
224   void addStore(int64_t OffsetFromFirst, StoreInst *SI) {
225     int64_t StoreSize = DL.getTypeStoreSize(SI->getOperand(0)->getType());
226 
227     addRange(OffsetFromFirst, StoreSize,
228              SI->getPointerOperand(), SI->getAlignment(), SI);
229   }
230 
231   void addMemSet(int64_t OffsetFromFirst, MemSetInst *MSI) {
232     int64_t Size = cast<ConstantInt>(MSI->getLength())->getZExtValue();
233     addRange(OffsetFromFirst, Size, MSI->getDest(), MSI->getAlignment(), MSI);
234   }
235 
236   void addRange(int64_t Start, int64_t Size, Value *Ptr,
237                 unsigned Alignment, Instruction *Inst);
238 
239 };
240 
241 } // end anon namespace
242 
243 
244 /// Add a new store to the MemsetRanges data structure.  This adds a
245 /// new range for the specified store at the specified offset, merging into
246 /// existing ranges as appropriate.
247 void MemsetRanges::addRange(int64_t Start, int64_t Size, Value *Ptr,
248                             unsigned Alignment, Instruction *Inst) {
249   int64_t End = Start+Size;
250 
251   range_iterator I = std::lower_bound(Ranges.begin(), Ranges.end(), Start,
252     [](const MemsetRange &LHS, int64_t RHS) { return LHS.End < RHS; });
253 
254   // We now know that I == E, in which case we didn't find anything to merge
255   // with, or that Start <= I->End.  If End < I->Start or I == E, then we need
256   // to insert a new range.  Handle this now.
257   if (I == Ranges.end() || End < I->Start) {
258     MemsetRange &R = *Ranges.insert(I, MemsetRange());
259     R.Start        = Start;
260     R.End          = End;
261     R.StartPtr     = Ptr;
262     R.Alignment    = Alignment;
263     R.TheStores.push_back(Inst);
264     return;
265   }
266 
267   // This store overlaps with I, add it.
268   I->TheStores.push_back(Inst);
269 
270   // At this point, we may have an interval that completely contains our store.
271   // If so, just add it to the interval and return.
272   if (I->Start <= Start && I->End >= End)
273     return;
274 
275   // Now we know that Start <= I->End and End >= I->Start so the range overlaps
276   // but is not entirely contained within the range.
277 
278   // See if the range extends the start of the range.  In this case, it couldn't
279   // possibly cause it to join the prior range, because otherwise we would have
280   // stopped on *it*.
281   if (Start < I->Start) {
282     I->Start = Start;
283     I->StartPtr = Ptr;
284     I->Alignment = Alignment;
285   }
286 
287   // Now we know that Start <= I->End and Start >= I->Start (so the startpoint
288   // is in or right at the end of I), and that End >= I->Start.  Extend I out to
289   // End.
290   if (End > I->End) {
291     I->End = End;
292     range_iterator NextI = I;
293     while (++NextI != Ranges.end() && End >= NextI->Start) {
294       // Merge the range in.
295       I->TheStores.append(NextI->TheStores.begin(), NextI->TheStores.end());
296       if (NextI->End > I->End)
297         I->End = NextI->End;
298       Ranges.erase(NextI);
299       NextI = I;
300     }
301   }
302 }
303 
304 //===----------------------------------------------------------------------===//
305 //                         MemCpyOpt Pass
306 //===----------------------------------------------------------------------===//
307 
308 namespace {
309   class MemCpyOpt : public FunctionPass {
310     MemoryDependenceResults *MD;
311     TargetLibraryInfo *TLI;
312   public:
313     static char ID; // Pass identification, replacement for typeid
314     MemCpyOpt() : FunctionPass(ID) {
315       initializeMemCpyOptPass(*PassRegistry::getPassRegistry());
316       MD = nullptr;
317       TLI = nullptr;
318     }
319 
320     bool runOnFunction(Function &F) override;
321 
322   private:
323     // This transformation requires dominator postdominator info
324     void getAnalysisUsage(AnalysisUsage &AU) const override {
325       AU.setPreservesCFG();
326       AU.addRequired<AssumptionCacheTracker>();
327       AU.addRequired<DominatorTreeWrapperPass>();
328       AU.addRequired<MemoryDependenceWrapperPass>();
329       AU.addRequired<AAResultsWrapperPass>();
330       AU.addRequired<TargetLibraryInfoWrapperPass>();
331       AU.addPreserved<GlobalsAAWrapperPass>();
332       AU.addPreserved<MemoryDependenceWrapperPass>();
333     }
334 
335     // Helper functions
336     bool processStore(StoreInst *SI, BasicBlock::iterator &BBI);
337     bool processMemSet(MemSetInst *SI, BasicBlock::iterator &BBI);
338     bool processMemCpy(MemCpyInst *M);
339     bool processMemMove(MemMoveInst *M);
340     bool performCallSlotOptzn(Instruction *cpy, Value *cpyDst, Value *cpySrc,
341                               uint64_t cpyLen, unsigned cpyAlign, CallInst *C);
342     bool processMemCpyMemCpyDependence(MemCpyInst *M, MemCpyInst *MDep);
343     bool processMemSetMemCpyDependence(MemCpyInst *M, MemSetInst *MDep);
344     bool performMemCpyToMemSetOptzn(MemCpyInst *M, MemSetInst *MDep);
345     bool processByValArgument(CallSite CS, unsigned ArgNo);
346     Instruction *tryMergingIntoMemset(Instruction *I, Value *StartPtr,
347                                       Value *ByteVal);
348 
349     bool iterateOnFunction(Function &F);
350   };
351 
352   char MemCpyOpt::ID = 0;
353 }
354 
355 /// The public interface to this file...
356 FunctionPass *llvm::createMemCpyOptPass() { return new MemCpyOpt(); }
357 
358 INITIALIZE_PASS_BEGIN(MemCpyOpt, "memcpyopt", "MemCpy Optimization",
359                       false, false)
360 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
361 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
362 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
363 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
364 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
365 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
366 INITIALIZE_PASS_END(MemCpyOpt, "memcpyopt", "MemCpy Optimization",
367                     false, false)
368 
369 /// When scanning forward over instructions, we look for some other patterns to
370 /// fold away. In particular, this looks for stores to neighboring locations of
371 /// memory. If it sees enough consecutive ones, it attempts to merge them
372 /// together into a memcpy/memset.
373 Instruction *MemCpyOpt::tryMergingIntoMemset(Instruction *StartInst,
374                                              Value *StartPtr, Value *ByteVal) {
375   const DataLayout &DL = StartInst->getModule()->getDataLayout();
376 
377   // Okay, so we now have a single store that can be splatable.  Scan to find
378   // all subsequent stores of the same value to offset from the same pointer.
379   // Join these together into ranges, so we can decide whether contiguous blocks
380   // are stored.
381   MemsetRanges Ranges(DL);
382 
383   BasicBlock::iterator BI(StartInst);
384   for (++BI; !isa<TerminatorInst>(BI); ++BI) {
385     if (!isa<StoreInst>(BI) && !isa<MemSetInst>(BI)) {
386       // If the instruction is readnone, ignore it, otherwise bail out.  We
387       // don't even allow readonly here because we don't want something like:
388       // A[1] = 2; strlen(A); A[2] = 2; -> memcpy(A, ...); strlen(A).
389       if (BI->mayWriteToMemory() || BI->mayReadFromMemory())
390         break;
391       continue;
392     }
393 
394     if (StoreInst *NextStore = dyn_cast<StoreInst>(BI)) {
395       // If this is a store, see if we can merge it in.
396       if (!NextStore->isSimple()) break;
397 
398       // Check to see if this stored value is of the same byte-splattable value.
399       if (ByteVal != isBytewiseValue(NextStore->getOperand(0)))
400         break;
401 
402       // Check to see if this store is to a constant offset from the start ptr.
403       int64_t Offset;
404       if (!IsPointerOffset(StartPtr, NextStore->getPointerOperand(), Offset,
405                            DL))
406         break;
407 
408       Ranges.addStore(Offset, NextStore);
409     } else {
410       MemSetInst *MSI = cast<MemSetInst>(BI);
411 
412       if (MSI->isVolatile() || ByteVal != MSI->getValue() ||
413           !isa<ConstantInt>(MSI->getLength()))
414         break;
415 
416       // Check to see if this store is to a constant offset from the start ptr.
417       int64_t Offset;
418       if (!IsPointerOffset(StartPtr, MSI->getDest(), Offset, DL))
419         break;
420 
421       Ranges.addMemSet(Offset, MSI);
422     }
423   }
424 
425   // If we have no ranges, then we just had a single store with nothing that
426   // could be merged in.  This is a very common case of course.
427   if (Ranges.empty())
428     return nullptr;
429 
430   // If we had at least one store that could be merged in, add the starting
431   // store as well.  We try to avoid this unless there is at least something
432   // interesting as a small compile-time optimization.
433   Ranges.addInst(0, StartInst);
434 
435   // If we create any memsets, we put it right before the first instruction that
436   // isn't part of the memset block.  This ensure that the memset is dominated
437   // by any addressing instruction needed by the start of the block.
438   IRBuilder<> Builder(&*BI);
439 
440   // Now that we have full information about ranges, loop over the ranges and
441   // emit memset's for anything big enough to be worthwhile.
442   Instruction *AMemSet = nullptr;
443   for (const MemsetRange &Range : Ranges) {
444 
445     if (Range.TheStores.size() == 1) continue;
446 
447     // If it is profitable to lower this range to memset, do so now.
448     if (!Range.isProfitableToUseMemset(DL))
449       continue;
450 
451     // Otherwise, we do want to transform this!  Create a new memset.
452     // Get the starting pointer of the block.
453     StartPtr = Range.StartPtr;
454 
455     // Determine alignment
456     unsigned Alignment = Range.Alignment;
457     if (Alignment == 0) {
458       Type *EltType =
459         cast<PointerType>(StartPtr->getType())->getElementType();
460       Alignment = DL.getABITypeAlignment(EltType);
461     }
462 
463     AMemSet =
464       Builder.CreateMemSet(StartPtr, ByteVal, Range.End-Range.Start, Alignment);
465 
466     DEBUG(dbgs() << "Replace stores:\n";
467           for (Instruction *SI : Range.TheStores)
468             dbgs() << *SI << '\n';
469           dbgs() << "With: " << *AMemSet << '\n');
470 
471     if (!Range.TheStores.empty())
472       AMemSet->setDebugLoc(Range.TheStores[0]->getDebugLoc());
473 
474     // Zap all the stores.
475     for (Instruction *SI : Range.TheStores) {
476       MD->removeInstruction(SI);
477       SI->eraseFromParent();
478     }
479     ++NumMemSetInfer;
480   }
481 
482   return AMemSet;
483 }
484 
485 static unsigned findCommonAlignment(const DataLayout &DL, const StoreInst *SI,
486                                      const LoadInst *LI) {
487   unsigned StoreAlign = SI->getAlignment();
488   if (!StoreAlign)
489     StoreAlign = DL.getABITypeAlignment(SI->getOperand(0)->getType());
490   unsigned LoadAlign = LI->getAlignment();
491   if (!LoadAlign)
492     LoadAlign = DL.getABITypeAlignment(LI->getType());
493 
494   return std::min(StoreAlign, LoadAlign);
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.
501 static bool moveUp(AliasAnalysis &AA, StoreInst *SI, Instruction *P) {
502   // If the store alias this position, early bail out.
503   MemoryLocation StoreLoc = MemoryLocation::get(SI);
504   if (AA.getModRefInfo(P, StoreLoc) != MRI_NoModRef)
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 apropriate.
509   DenseSet<Instruction*> Args;
510   if (auto *Ptr = dyn_cast<Instruction>(SI->getPointerOperand()))
511     if (Ptr->getParent() == SI->getParent())
512       Args.insert(Ptr);
513 
514   // Instruction to lift before P.
515   SmallVector<Instruction*, 8> ToLift;
516 
517   // Memory locations of lifted instructions.
518   SmallVector<MemoryLocation, 8> MemLocs;
519   MemLocs.push_back(StoreLoc);
520 
521   // Lifted callsites.
522   SmallVector<ImmutableCallSite, 8> CallSites;
523 
524   for (auto I = --SI->getIterator(), E = P->getIterator(); I != E; --I) {
525     auto *C = &*I;
526 
527     bool MayAlias = AA.getModRefInfo(C) != MRI_NoModRef;
528 
529     bool NeedLift = false;
530     if (Args.erase(C))
531       NeedLift = true;
532     else if (MayAlias) {
533       NeedLift = std::any_of(MemLocs.begin(), MemLocs.end(),
534         [C, &AA](const MemoryLocation &ML) {
535           return AA.getModRefInfo(C, ML);
536         });
537 
538       if (!NeedLift)
539         NeedLift = std::any_of(CallSites.begin(), CallSites.end(),
540           [C, &AA](const ImmutableCallSite &CS) {
541             return AA.getModRefInfo(C, CS);
542           });
543     }
544 
545     if (!NeedLift)
546       continue;
547 
548     if (MayAlias) {
549       if (auto CS = ImmutableCallSite(C)) {
550         // If we can't lift this before P, it's game over.
551         if (AA.getModRefInfo(P, CS) != MRI_NoModRef)
552           return false;
553 
554         CallSites.push_back(CS);
555       } else if (isa<LoadInst>(C) || isa<StoreInst>(C) || isa<VAArgInst>(C)) {
556         // If we can't lift this before P, it's game over.
557         auto ML = MemoryLocation::get(C);
558         if (AA.getModRefInfo(P, ML) != MRI_NoModRef)
559           return false;
560 
561         MemLocs.push_back(ML);
562       } else
563         // We don't know how to lift this instruction.
564         return false;
565     }
566 
567     ToLift.push_back(C);
568     for (unsigned k = 0, e = C->getNumOperands(); k != e; ++k)
569       if (auto *A = dyn_cast<Instruction>(C->getOperand(k)))
570         if (A->getParent() == SI->getParent())
571           Args.insert(A);
572   }
573 
574   // We made it, we need to lift
575   for (auto *I : reverse(ToLift)) {
576     DEBUG(dbgs() << "Lifting " << *I << " before " << *P << "\n");
577     I->moveBefore(P);
578   }
579 
580   return true;
581 }
582 
583 bool MemCpyOpt::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
584   if (!SI->isSimple()) return false;
585 
586   // Avoid merging nontemporal stores since the resulting
587   // memcpy/memset would not be able to preserve the nontemporal hint.
588   // In theory we could teach how to propagate the !nontemporal metadata to
589   // memset calls. However, that change would force the backend to
590   // conservatively expand !nontemporal memset calls back to sequences of
591   // store instructions (effectively undoing the merging).
592   if (SI->getMetadata(LLVMContext::MD_nontemporal))
593     return false;
594 
595   const DataLayout &DL = SI->getModule()->getDataLayout();
596 
597   // Load to store forwarding can be interpreted as memcpy.
598   if (LoadInst *LI = dyn_cast<LoadInst>(SI->getOperand(0))) {
599     if (LI->isSimple() && LI->hasOneUse() &&
600         LI->getParent() == SI->getParent()) {
601 
602       auto *T = LI->getType();
603       if (T->isAggregateType()) {
604         AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
605         MemoryLocation LoadLoc = MemoryLocation::get(LI);
606 
607         // We use alias analysis to check if an instruction may store to
608         // the memory we load from in between the load and the store. If
609         // such an instruction is found, we try to promote there instead
610         // of at the store position.
611         Instruction *P = SI;
612         for (auto &I : make_range(++LI->getIterator(), SI->getIterator())) {
613           if (AA.getModRefInfo(&I, LoadLoc) & MRI_Mod) {
614             P = &I;
615             break;
616           }
617         }
618 
619         // We found an instruction that may write to the loaded memory.
620         // We can try to promote at this position instead of the store
621         // position if nothing alias the store memory after this and the store
622         // destination is not in the range.
623         if (P && P != SI) {
624           if (!moveUp(AA, SI, P))
625             P = nullptr;
626         }
627 
628         // If a valid insertion position is found, then we can promote
629         // the load/store pair to a memcpy.
630         if (P) {
631           // If we load from memory that may alias the memory we store to,
632           // memmove must be used to preserve semantic. If not, memcpy can
633           // be used.
634           bool UseMemMove = false;
635           if (!AA.isNoAlias(MemoryLocation::get(SI), LoadLoc))
636             UseMemMove = true;
637 
638           unsigned Align = findCommonAlignment(DL, SI, LI);
639           uint64_t Size = DL.getTypeStoreSize(T);
640 
641           IRBuilder<> Builder(P);
642           Instruction *M;
643           if (UseMemMove)
644             M = Builder.CreateMemMove(SI->getPointerOperand(),
645                                       LI->getPointerOperand(), Size,
646                                       Align, SI->isVolatile());
647           else
648             M = Builder.CreateMemCpy(SI->getPointerOperand(),
649                                      LI->getPointerOperand(), Size,
650                                      Align, SI->isVolatile());
651 
652           DEBUG(dbgs() << "Promoting " << *LI << " to " << *SI
653                        << " => " << *M << "\n");
654 
655           MD->removeInstruction(SI);
656           SI->eraseFromParent();
657           MD->removeInstruction(LI);
658           LI->eraseFromParent();
659           ++NumMemCpyInstr;
660 
661           // Make sure we do not invalidate the iterator.
662           BBI = M->getIterator();
663           return true;
664         }
665       }
666 
667       // Detect cases where we're performing call slot forwarding, but
668       // happen to be using a load-store pair to implement it, rather than
669       // a memcpy.
670       MemDepResult ldep = MD->getDependency(LI);
671       CallInst *C = nullptr;
672       if (ldep.isClobber() && !isa<MemCpyInst>(ldep.getInst()))
673         C = dyn_cast<CallInst>(ldep.getInst());
674 
675       if (C) {
676         // Check that nothing touches the dest of the "copy" between
677         // the call and the store.
678         Value *CpyDest = SI->getPointerOperand()->stripPointerCasts();
679         bool CpyDestIsLocal = isa<AllocaInst>(CpyDest);
680         AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
681         MemoryLocation StoreLoc = MemoryLocation::get(SI);
682         for (BasicBlock::iterator I = --SI->getIterator(), E = C->getIterator();
683              I != E; --I) {
684           if (AA.getModRefInfo(&*I, StoreLoc) != MRI_NoModRef) {
685             C = nullptr;
686             break;
687           }
688           // The store to dest may never happen if an exception can be thrown
689           // between the load and the store.
690           if (I->mayThrow() && !CpyDestIsLocal) {
691             C = nullptr;
692             break;
693           }
694         }
695       }
696 
697       if (C) {
698         bool changed = performCallSlotOptzn(
699             LI, SI->getPointerOperand()->stripPointerCasts(),
700             LI->getPointerOperand()->stripPointerCasts(),
701             DL.getTypeStoreSize(SI->getOperand(0)->getType()),
702             findCommonAlignment(DL, SI, LI), C);
703         if (changed) {
704           MD->removeInstruction(SI);
705           SI->eraseFromParent();
706           MD->removeInstruction(LI);
707           LI->eraseFromParent();
708           ++NumMemCpyInstr;
709           return true;
710         }
711       }
712     }
713   }
714 
715   // There are two cases that are interesting for this code to handle: memcpy
716   // and memset.  Right now we only handle memset.
717 
718   // Ensure that the value being stored is something that can be memset'able a
719   // byte at a time like "0" or "-1" or any width, as well as things like
720   // 0xA0A0A0A0 and 0.0.
721   auto *V = SI->getOperand(0);
722   if (Value *ByteVal = isBytewiseValue(V)) {
723     if (Instruction *I = tryMergingIntoMemset(SI, SI->getPointerOperand(),
724                                               ByteVal)) {
725       BBI = I->getIterator(); // Don't invalidate iterator.
726       return true;
727     }
728 
729     // If we have an aggregate, we try to promote it to memset regardless
730     // of opportunity for merging as it can expose optimization opportunities
731     // in subsequent passes.
732     auto *T = V->getType();
733     if (T->isAggregateType()) {
734       uint64_t Size = DL.getTypeStoreSize(T);
735       unsigned Align = SI->getAlignment();
736       if (!Align)
737         Align = DL.getABITypeAlignment(T);
738       IRBuilder<> Builder(SI);
739       auto *M = Builder.CreateMemSet(SI->getPointerOperand(), ByteVal,
740                                      Size, Align, SI->isVolatile());
741 
742       DEBUG(dbgs() << "Promoting " << *SI << " to " << *M << "\n");
743 
744       MD->removeInstruction(SI);
745       SI->eraseFromParent();
746       NumMemSetInfer++;
747 
748       // Make sure we do not invalidate the iterator.
749       BBI = M->getIterator();
750       return true;
751     }
752   }
753 
754   return false;
755 }
756 
757 bool MemCpyOpt::processMemSet(MemSetInst *MSI, BasicBlock::iterator &BBI) {
758   // See if there is another memset or store neighboring this memset which
759   // allows us to widen out the memset to do a single larger store.
760   if (isa<ConstantInt>(MSI->getLength()) && !MSI->isVolatile())
761     if (Instruction *I = tryMergingIntoMemset(MSI, MSI->getDest(),
762                                               MSI->getValue())) {
763       BBI = I->getIterator(); // Don't invalidate iterator.
764       return true;
765     }
766   return false;
767 }
768 
769 
770 /// Takes a memcpy and a call that it depends on,
771 /// and checks for the possibility of a call slot optimization by having
772 /// the call write its result directly into the destination of the memcpy.
773 bool MemCpyOpt::performCallSlotOptzn(Instruction *cpy,
774                                      Value *cpyDest, Value *cpySrc,
775                                      uint64_t cpyLen, unsigned cpyAlign,
776                                      CallInst *C) {
777   // The general transformation to keep in mind is
778   //
779   //   call @func(..., src, ...)
780   //   memcpy(dest, src, ...)
781   //
782   // ->
783   //
784   //   memcpy(dest, src, ...)
785   //   call @func(..., dest, ...)
786   //
787   // Since moving the memcpy is technically awkward, we additionally check that
788   // src only holds uninitialized values at the moment of the call, meaning that
789   // the memcpy can be discarded rather than moved.
790 
791   // Lifetime marks shouldn't be operated on.
792   if (Function *F = C->getCalledFunction())
793     if (F->isIntrinsic() && F->getIntrinsicID() == Intrinsic::lifetime_start)
794       return false;
795 
796   // Deliberately get the source and destination with bitcasts stripped away,
797   // because we'll need to do type comparisons based on the underlying type.
798   CallSite CS(C);
799 
800   // Require that src be an alloca.  This simplifies the reasoning considerably.
801   AllocaInst *srcAlloca = dyn_cast<AllocaInst>(cpySrc);
802   if (!srcAlloca)
803     return false;
804 
805   ConstantInt *srcArraySize = dyn_cast<ConstantInt>(srcAlloca->getArraySize());
806   if (!srcArraySize)
807     return false;
808 
809   const DataLayout &DL = cpy->getModule()->getDataLayout();
810   uint64_t srcSize = DL.getTypeAllocSize(srcAlloca->getAllocatedType()) *
811                      srcArraySize->getZExtValue();
812 
813   if (cpyLen < srcSize)
814     return false;
815 
816   // Check that accessing the first srcSize bytes of dest will not cause a
817   // trap.  Otherwise the transform is invalid since it might cause a trap
818   // to occur earlier than it otherwise would.
819   if (AllocaInst *A = dyn_cast<AllocaInst>(cpyDest)) {
820     // The destination is an alloca.  Check it is larger than srcSize.
821     ConstantInt *destArraySize = dyn_cast<ConstantInt>(A->getArraySize());
822     if (!destArraySize)
823       return false;
824 
825     uint64_t destSize = DL.getTypeAllocSize(A->getAllocatedType()) *
826                         destArraySize->getZExtValue();
827 
828     if (destSize < srcSize)
829       return false;
830   } else if (Argument *A = dyn_cast<Argument>(cpyDest)) {
831     // The store to dest may never happen if the call can throw.
832     if (C->mayThrow())
833       return false;
834 
835     if (A->getDereferenceableBytes() < srcSize) {
836       // If the destination is an sret parameter then only accesses that are
837       // outside of the returned struct type can trap.
838       if (!A->hasStructRetAttr())
839         return false;
840 
841       Type *StructTy = cast<PointerType>(A->getType())->getElementType();
842       if (!StructTy->isSized()) {
843         // The call may never return and hence the copy-instruction may never
844         // be executed, and therefore it's not safe to say "the destination
845         // has at least <cpyLen> bytes, as implied by the copy-instruction",
846         return false;
847       }
848 
849       uint64_t destSize = DL.getTypeAllocSize(StructTy);
850       if (destSize < srcSize)
851         return false;
852     }
853   } else {
854     return false;
855   }
856 
857   // Check that dest points to memory that is at least as aligned as src.
858   unsigned srcAlign = srcAlloca->getAlignment();
859   if (!srcAlign)
860     srcAlign = DL.getABITypeAlignment(srcAlloca->getAllocatedType());
861   bool isDestSufficientlyAligned = srcAlign <= cpyAlign;
862   // If dest is not aligned enough and we can't increase its alignment then
863   // bail out.
864   if (!isDestSufficientlyAligned && !isa<AllocaInst>(cpyDest))
865     return false;
866 
867   // Check that src is not accessed except via the call and the memcpy.  This
868   // guarantees that it holds only undefined values when passed in (so the final
869   // memcpy can be dropped), that it is not read or written between the call and
870   // the memcpy, and that writing beyond the end of it is undefined.
871   SmallVector<User*, 8> srcUseList(srcAlloca->user_begin(),
872                                    srcAlloca->user_end());
873   while (!srcUseList.empty()) {
874     User *U = srcUseList.pop_back_val();
875 
876     if (isa<BitCastInst>(U) || isa<AddrSpaceCastInst>(U)) {
877       for (User *UU : U->users())
878         srcUseList.push_back(UU);
879       continue;
880     }
881     if (GetElementPtrInst *G = dyn_cast<GetElementPtrInst>(U)) {
882       if (!G->hasAllZeroIndices())
883         return false;
884 
885       for (User *UU : U->users())
886         srcUseList.push_back(UU);
887       continue;
888     }
889     if (const IntrinsicInst *IT = dyn_cast<IntrinsicInst>(U))
890       if (IT->getIntrinsicID() == Intrinsic::lifetime_start ||
891           IT->getIntrinsicID() == Intrinsic::lifetime_end)
892         continue;
893 
894     if (U != C && U != cpy)
895       return false;
896   }
897 
898   // Check that src isn't captured by the called function since the
899   // transformation can cause aliasing issues in that case.
900   for (unsigned i = 0, e = CS.arg_size(); i != e; ++i)
901     if (CS.getArgument(i) == cpySrc && !CS.doesNotCapture(i))
902       return false;
903 
904   // Since we're changing the parameter to the callsite, we need to make sure
905   // that what would be the new parameter dominates the callsite.
906   DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
907   if (Instruction *cpyDestInst = dyn_cast<Instruction>(cpyDest))
908     if (!DT.dominates(cpyDestInst, C))
909       return false;
910 
911   // In addition to knowing that the call does not access src in some
912   // unexpected manner, for example via a global, which we deduce from
913   // the use analysis, we also need to know that it does not sneakily
914   // access dest.  We rely on AA to figure this out for us.
915   AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
916   ModRefInfo MR = AA.getModRefInfo(C, cpyDest, srcSize);
917   // If necessary, perform additional analysis.
918   if (MR != MRI_NoModRef)
919     MR = AA.callCapturesBefore(C, cpyDest, srcSize, &DT);
920   if (MR != MRI_NoModRef)
921     return false;
922 
923   // All the checks have passed, so do the transformation.
924   bool changedArgument = false;
925   for (unsigned i = 0; i < CS.arg_size(); ++i)
926     if (CS.getArgument(i)->stripPointerCasts() == cpySrc) {
927       Value *Dest = cpySrc->getType() == cpyDest->getType() ?  cpyDest
928         : CastInst::CreatePointerCast(cpyDest, cpySrc->getType(),
929                                       cpyDest->getName(), C);
930       changedArgument = true;
931       if (CS.getArgument(i)->getType() == Dest->getType())
932         CS.setArgument(i, Dest);
933       else
934         CS.setArgument(i, CastInst::CreatePointerCast(Dest,
935                           CS.getArgument(i)->getType(), Dest->getName(), C));
936     }
937 
938   if (!changedArgument)
939     return false;
940 
941   // If the destination wasn't sufficiently aligned then increase its alignment.
942   if (!isDestSufficientlyAligned) {
943     assert(isa<AllocaInst>(cpyDest) && "Can only increase alloca alignment!");
944     cast<AllocaInst>(cpyDest)->setAlignment(srcAlign);
945   }
946 
947   // Drop any cached information about the call, because we may have changed
948   // its dependence information by changing its parameter.
949   MD->removeInstruction(C);
950 
951   // Update AA metadata
952   // FIXME: MD_tbaa_struct and MD_mem_parallel_loop_access should also be
953   // handled here, but combineMetadata doesn't support them yet
954   unsigned KnownIDs[] = {LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
955                          LLVMContext::MD_noalias,
956                          LLVMContext::MD_invariant_group};
957   combineMetadata(C, cpy, KnownIDs);
958 
959   // Remove the memcpy.
960   MD->removeInstruction(cpy);
961   ++NumMemCpyInstr;
962 
963   return true;
964 }
965 
966 /// We've found that the (upward scanning) memory dependence of memcpy 'M' is
967 /// the memcpy 'MDep'. Try to simplify M to copy from MDep's input if we can.
968 bool MemCpyOpt::processMemCpyMemCpyDependence(MemCpyInst *M, MemCpyInst *MDep) {
969   // We can only transforms memcpy's where the dest of one is the source of the
970   // other.
971   if (M->getSource() != MDep->getDest() || MDep->isVolatile())
972     return false;
973 
974   // If dep instruction is reading from our current input, then it is a noop
975   // transfer and substituting the input won't change this instruction.  Just
976   // ignore the input and let someone else zap MDep.  This handles cases like:
977   //    memcpy(a <- a)
978   //    memcpy(b <- a)
979   if (M->getSource() == MDep->getSource())
980     return false;
981 
982   // Second, the length of the memcpy's must be the same, or the preceding one
983   // must be larger than the following one.
984   ConstantInt *MDepLen = dyn_cast<ConstantInt>(MDep->getLength());
985   ConstantInt *MLen = dyn_cast<ConstantInt>(M->getLength());
986   if (!MDepLen || !MLen || MDepLen->getZExtValue() < MLen->getZExtValue())
987     return false;
988 
989   AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
990 
991   // Verify that the copied-from memory doesn't change in between the two
992   // transfers.  For example, in:
993   //    memcpy(a <- b)
994   //    *b = 42;
995   //    memcpy(c <- a)
996   // It would be invalid to transform the second memcpy into memcpy(c <- b).
997   //
998   // TODO: If the code between M and MDep is transparent to the destination "c",
999   // then we could still perform the xform by moving M up to the first memcpy.
1000   //
1001   // NOTE: This is conservative, it will stop on any read from the source loc,
1002   // not just the defining memcpy.
1003   MemDepResult SourceDep =
1004       MD->getPointerDependencyFrom(MemoryLocation::getForSource(MDep), false,
1005                                    M->getIterator(), M->getParent());
1006   if (!SourceDep.isClobber() || SourceDep.getInst() != MDep)
1007     return false;
1008 
1009   // If the dest of the second might alias the source of the first, then the
1010   // source and dest might overlap.  We still want to eliminate the intermediate
1011   // value, but we have to generate a memmove instead of memcpy.
1012   bool UseMemMove = false;
1013   if (!AA.isNoAlias(MemoryLocation::getForDest(M),
1014                     MemoryLocation::getForSource(MDep)))
1015     UseMemMove = true;
1016 
1017   // If all checks passed, then we can transform M.
1018 
1019   // Make sure to use the lesser of the alignment of the source and the dest
1020   // since we're changing where we're reading from, but don't want to increase
1021   // the alignment past what can be read from or written to.
1022   // TODO: Is this worth it if we're creating a less aligned memcpy? For
1023   // example we could be moving from movaps -> movq on x86.
1024   unsigned Align = std::min(MDep->getAlignment(), M->getAlignment());
1025 
1026   IRBuilder<> Builder(M);
1027   if (UseMemMove)
1028     Builder.CreateMemMove(M->getRawDest(), MDep->getRawSource(), M->getLength(),
1029                           Align, M->isVolatile());
1030   else
1031     Builder.CreateMemCpy(M->getRawDest(), MDep->getRawSource(), M->getLength(),
1032                          Align, M->isVolatile());
1033 
1034   // Remove the instruction we're replacing.
1035   MD->removeInstruction(M);
1036   M->eraseFromParent();
1037   ++NumMemCpyInstr;
1038   return true;
1039 }
1040 
1041 /// We've found that the (upward scanning) memory dependence of \p MemCpy is
1042 /// \p MemSet.  Try to simplify \p MemSet to only set the trailing bytes that
1043 /// weren't copied over by \p MemCpy.
1044 ///
1045 /// In other words, transform:
1046 /// \code
1047 ///   memset(dst, c, dst_size);
1048 ///   memcpy(dst, src, src_size);
1049 /// \endcode
1050 /// into:
1051 /// \code
1052 ///   memcpy(dst, src, src_size);
1053 ///   memset(dst + src_size, c, dst_size <= src_size ? 0 : dst_size - src_size);
1054 /// \endcode
1055 bool MemCpyOpt::processMemSetMemCpyDependence(MemCpyInst *MemCpy,
1056                                               MemSetInst *MemSet) {
1057   // We can only transform memset/memcpy with the same destination.
1058   if (MemSet->getDest() != MemCpy->getDest())
1059     return false;
1060 
1061   // Check that there are no other dependencies on the memset destination.
1062   MemDepResult DstDepInfo =
1063       MD->getPointerDependencyFrom(MemoryLocation::getForDest(MemSet), false,
1064                                    MemCpy->getIterator(), MemCpy->getParent());
1065   if (DstDepInfo.getInst() != MemSet)
1066     return false;
1067 
1068   // Use the same i8* dest as the memcpy, killing the memset dest if different.
1069   Value *Dest = MemCpy->getRawDest();
1070   Value *DestSize = MemSet->getLength();
1071   Value *SrcSize = MemCpy->getLength();
1072 
1073   // By default, create an unaligned memset.
1074   unsigned Align = 1;
1075   // If Dest is aligned, and SrcSize is constant, use the minimum alignment
1076   // of the sum.
1077   const unsigned DestAlign =
1078       std::max(MemSet->getAlignment(), MemCpy->getAlignment());
1079   if (DestAlign > 1)
1080     if (ConstantInt *SrcSizeC = dyn_cast<ConstantInt>(SrcSize))
1081       Align = MinAlign(SrcSizeC->getZExtValue(), DestAlign);
1082 
1083   IRBuilder<> Builder(MemCpy);
1084 
1085   // If the sizes have different types, zext the smaller one.
1086   if (DestSize->getType() != SrcSize->getType()) {
1087     if (DestSize->getType()->getIntegerBitWidth() >
1088         SrcSize->getType()->getIntegerBitWidth())
1089       SrcSize = Builder.CreateZExt(SrcSize, DestSize->getType());
1090     else
1091       DestSize = Builder.CreateZExt(DestSize, SrcSize->getType());
1092   }
1093 
1094   Value *MemsetLen =
1095       Builder.CreateSelect(Builder.CreateICmpULE(DestSize, SrcSize),
1096                            ConstantInt::getNullValue(DestSize->getType()),
1097                            Builder.CreateSub(DestSize, SrcSize));
1098   Builder.CreateMemSet(Builder.CreateGEP(Dest, SrcSize), MemSet->getOperand(1),
1099                        MemsetLen, Align);
1100 
1101   MD->removeInstruction(MemSet);
1102   MemSet->eraseFromParent();
1103   return true;
1104 }
1105 
1106 /// Transform memcpy to memset when its source was just memset.
1107 /// In other words, turn:
1108 /// \code
1109 ///   memset(dst1, c, dst1_size);
1110 ///   memcpy(dst2, dst1, dst2_size);
1111 /// \endcode
1112 /// into:
1113 /// \code
1114 ///   memset(dst1, c, dst1_size);
1115 ///   memset(dst2, c, dst2_size);
1116 /// \endcode
1117 /// When dst2_size <= dst1_size.
1118 ///
1119 /// The \p MemCpy must have a Constant length.
1120 bool MemCpyOpt::performMemCpyToMemSetOptzn(MemCpyInst *MemCpy,
1121                                            MemSetInst *MemSet) {
1122   // This only makes sense on memcpy(..., memset(...), ...).
1123   if (MemSet->getRawDest() != MemCpy->getRawSource())
1124     return false;
1125 
1126   ConstantInt *CopySize = cast<ConstantInt>(MemCpy->getLength());
1127   ConstantInt *MemSetSize = dyn_cast<ConstantInt>(MemSet->getLength());
1128   // Make sure the memcpy doesn't read any more than what the memset wrote.
1129   // Don't worry about sizes larger than i64.
1130   if (!MemSetSize || CopySize->getZExtValue() > MemSetSize->getZExtValue())
1131     return false;
1132 
1133   IRBuilder<> Builder(MemCpy);
1134   Builder.CreateMemSet(MemCpy->getRawDest(), MemSet->getOperand(1),
1135                        CopySize, MemCpy->getAlignment());
1136   return true;
1137 }
1138 
1139 /// Perform simplification of memcpy's.  If we have memcpy A
1140 /// which copies X to Y, and memcpy B which copies Y to Z, then we can rewrite
1141 /// B to be a memcpy from X to Z (or potentially a memmove, depending on
1142 /// circumstances). This allows later passes to remove the first memcpy
1143 /// altogether.
1144 bool MemCpyOpt::processMemCpy(MemCpyInst *M) {
1145   // We can only optimize non-volatile memcpy's.
1146   if (M->isVolatile()) return false;
1147 
1148   // If the source and destination of the memcpy are the same, then zap it.
1149   if (M->getSource() == M->getDest()) {
1150     MD->removeInstruction(M);
1151     M->eraseFromParent();
1152     return false;
1153   }
1154 
1155   // If copying from a constant, try to turn the memcpy into a memset.
1156   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(M->getSource()))
1157     if (GV->isConstant() && GV->hasDefinitiveInitializer())
1158       if (Value *ByteVal = isBytewiseValue(GV->getInitializer())) {
1159         IRBuilder<> Builder(M);
1160         Builder.CreateMemSet(M->getRawDest(), ByteVal, M->getLength(),
1161                              M->getAlignment(), false);
1162         MD->removeInstruction(M);
1163         M->eraseFromParent();
1164         ++NumCpyToSet;
1165         return true;
1166       }
1167 
1168   MemDepResult DepInfo = MD->getDependency(M);
1169 
1170   // Try to turn a partially redundant memset + memcpy into
1171   // memcpy + smaller memset.  We don't need the memcpy size for this.
1172   if (DepInfo.isClobber())
1173     if (MemSetInst *MDep = dyn_cast<MemSetInst>(DepInfo.getInst()))
1174       if (processMemSetMemCpyDependence(M, MDep))
1175         return true;
1176 
1177   // The optimizations after this point require the memcpy size.
1178   ConstantInt *CopySize = dyn_cast<ConstantInt>(M->getLength());
1179   if (!CopySize) return false;
1180 
1181   // There are four possible optimizations we can do for memcpy:
1182   //   a) memcpy-memcpy xform which exposes redundance for DSE.
1183   //   b) call-memcpy xform for return slot optimization.
1184   //   c) memcpy from freshly alloca'd space or space that has just started its
1185   //      lifetime copies undefined data, and we can therefore eliminate the
1186   //      memcpy in favor of the data that was already at the destination.
1187   //   d) memcpy from a just-memset'd source can be turned into memset.
1188   if (DepInfo.isClobber()) {
1189     if (CallInst *C = dyn_cast<CallInst>(DepInfo.getInst())) {
1190       if (performCallSlotOptzn(M, M->getDest(), M->getSource(),
1191                                CopySize->getZExtValue(), M->getAlignment(),
1192                                C)) {
1193         MD->removeInstruction(M);
1194         M->eraseFromParent();
1195         return true;
1196       }
1197     }
1198   }
1199 
1200   MemoryLocation SrcLoc = MemoryLocation::getForSource(M);
1201   MemDepResult SrcDepInfo = MD->getPointerDependencyFrom(
1202       SrcLoc, true, M->getIterator(), M->getParent());
1203 
1204   if (SrcDepInfo.isClobber()) {
1205     if (MemCpyInst *MDep = dyn_cast<MemCpyInst>(SrcDepInfo.getInst()))
1206       return processMemCpyMemCpyDependence(M, MDep);
1207   } else if (SrcDepInfo.isDef()) {
1208     Instruction *I = SrcDepInfo.getInst();
1209     bool hasUndefContents = false;
1210 
1211     if (isa<AllocaInst>(I)) {
1212       hasUndefContents = true;
1213     } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1214       if (II->getIntrinsicID() == Intrinsic::lifetime_start)
1215         if (ConstantInt *LTSize = dyn_cast<ConstantInt>(II->getArgOperand(0)))
1216           if (LTSize->getZExtValue() >= CopySize->getZExtValue())
1217             hasUndefContents = true;
1218     }
1219 
1220     if (hasUndefContents) {
1221       MD->removeInstruction(M);
1222       M->eraseFromParent();
1223       ++NumMemCpyInstr;
1224       return true;
1225     }
1226   }
1227 
1228   if (SrcDepInfo.isClobber())
1229     if (MemSetInst *MDep = dyn_cast<MemSetInst>(SrcDepInfo.getInst()))
1230       if (performMemCpyToMemSetOptzn(M, MDep)) {
1231         MD->removeInstruction(M);
1232         M->eraseFromParent();
1233         ++NumCpyToSet;
1234         return true;
1235       }
1236 
1237   return false;
1238 }
1239 
1240 /// Transforms memmove calls to memcpy calls when the src/dst are guaranteed
1241 /// not to alias.
1242 bool MemCpyOpt::processMemMove(MemMoveInst *M) {
1243   AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
1244 
1245   if (!TLI->has(LibFunc::memmove))
1246     return false;
1247 
1248   // See if the pointers alias.
1249   if (!AA.isNoAlias(MemoryLocation::getForDest(M),
1250                     MemoryLocation::getForSource(M)))
1251     return false;
1252 
1253   DEBUG(dbgs() << "MemCpyOpt: Optimizing memmove -> memcpy: " << *M << "\n");
1254 
1255   // If not, then we know we can transform this.
1256   Type *ArgTys[3] = { M->getRawDest()->getType(),
1257                       M->getRawSource()->getType(),
1258                       M->getLength()->getType() };
1259   M->setCalledFunction(Intrinsic::getDeclaration(M->getModule(),
1260                                                  Intrinsic::memcpy, ArgTys));
1261 
1262   // MemDep may have over conservative information about this instruction, just
1263   // conservatively flush it from the cache.
1264   MD->removeInstruction(M);
1265 
1266   ++NumMoveToCpy;
1267   return true;
1268 }
1269 
1270 /// This is called on every byval argument in call sites.
1271 bool MemCpyOpt::processByValArgument(CallSite CS, unsigned ArgNo) {
1272   const DataLayout &DL = CS.getCaller()->getParent()->getDataLayout();
1273   // Find out what feeds this byval argument.
1274   Value *ByValArg = CS.getArgument(ArgNo);
1275   Type *ByValTy = cast<PointerType>(ByValArg->getType())->getElementType();
1276   uint64_t ByValSize = DL.getTypeAllocSize(ByValTy);
1277   MemDepResult DepInfo = MD->getPointerDependencyFrom(
1278       MemoryLocation(ByValArg, ByValSize), true,
1279       CS.getInstruction()->getIterator(), CS.getInstruction()->getParent());
1280   if (!DepInfo.isClobber())
1281     return false;
1282 
1283   // If the byval argument isn't fed by a memcpy, ignore it.  If it is fed by
1284   // a memcpy, see if we can byval from the source of the memcpy instead of the
1285   // result.
1286   MemCpyInst *MDep = dyn_cast<MemCpyInst>(DepInfo.getInst());
1287   if (!MDep || MDep->isVolatile() ||
1288       ByValArg->stripPointerCasts() != MDep->getDest())
1289     return false;
1290 
1291   // The length of the memcpy must be larger or equal to the size of the byval.
1292   ConstantInt *C1 = dyn_cast<ConstantInt>(MDep->getLength());
1293   if (!C1 || C1->getValue().getZExtValue() < ByValSize)
1294     return false;
1295 
1296   // Get the alignment of the byval.  If the call doesn't specify the alignment,
1297   // then it is some target specific value that we can't know.
1298   unsigned ByValAlign = CS.getParamAlignment(ArgNo+1);
1299   if (ByValAlign == 0) return false;
1300 
1301   // If it is greater than the memcpy, then we check to see if we can force the
1302   // source of the memcpy to the alignment we need.  If we fail, we bail out.
1303   AssumptionCache &AC =
1304       getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
1305           *CS->getParent()->getParent());
1306   DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1307   if (MDep->getAlignment() < ByValAlign &&
1308       getOrEnforceKnownAlignment(MDep->getSource(), ByValAlign, DL,
1309                                  CS.getInstruction(), &AC, &DT) < ByValAlign)
1310     return false;
1311 
1312   // Verify that the copied-from memory doesn't change in between the memcpy and
1313   // the byval call.
1314   //    memcpy(a <- b)
1315   //    *b = 42;
1316   //    foo(*a)
1317   // It would be invalid to transform the second memcpy into foo(*b).
1318   //
1319   // NOTE: This is conservative, it will stop on any read from the source loc,
1320   // not just the defining memcpy.
1321   MemDepResult SourceDep = MD->getPointerDependencyFrom(
1322       MemoryLocation::getForSource(MDep), false,
1323       CS.getInstruction()->getIterator(), MDep->getParent());
1324   if (!SourceDep.isClobber() || SourceDep.getInst() != MDep)
1325     return false;
1326 
1327   Value *TmpCast = MDep->getSource();
1328   if (MDep->getSource()->getType() != ByValArg->getType())
1329     TmpCast = new BitCastInst(MDep->getSource(), ByValArg->getType(),
1330                               "tmpcast", CS.getInstruction());
1331 
1332   DEBUG(dbgs() << "MemCpyOpt: Forwarding memcpy to byval:\n"
1333                << "  " << *MDep << "\n"
1334                << "  " << *CS.getInstruction() << "\n");
1335 
1336   // Otherwise we're good!  Update the byval argument.
1337   CS.setArgument(ArgNo, TmpCast);
1338   ++NumMemCpyInstr;
1339   return true;
1340 }
1341 
1342 /// Executes one iteration of MemCpyOpt.
1343 bool MemCpyOpt::iterateOnFunction(Function &F) {
1344   bool MadeChange = false;
1345 
1346   // Walk all instruction in the function.
1347   for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) {
1348     for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
1349       // Avoid invalidating the iterator.
1350       Instruction *I = &*BI++;
1351 
1352       bool RepeatInstruction = false;
1353 
1354       if (StoreInst *SI = dyn_cast<StoreInst>(I))
1355         MadeChange |= processStore(SI, BI);
1356       else if (MemSetInst *M = dyn_cast<MemSetInst>(I))
1357         RepeatInstruction = processMemSet(M, BI);
1358       else if (MemCpyInst *M = dyn_cast<MemCpyInst>(I))
1359         RepeatInstruction = processMemCpy(M);
1360       else if (MemMoveInst *M = dyn_cast<MemMoveInst>(I))
1361         RepeatInstruction = processMemMove(M);
1362       else if (auto CS = CallSite(I)) {
1363         for (unsigned i = 0, e = CS.arg_size(); i != e; ++i)
1364           if (CS.isByValArgument(i))
1365             MadeChange |= processByValArgument(CS, i);
1366       }
1367 
1368       // Reprocess the instruction if desired.
1369       if (RepeatInstruction) {
1370         if (BI != BB->begin()) --BI;
1371         MadeChange = true;
1372       }
1373     }
1374   }
1375 
1376   return MadeChange;
1377 }
1378 
1379 /// This is the main transformation entry point for a function.
1380 bool MemCpyOpt::runOnFunction(Function &F) {
1381   if (skipFunction(F))
1382     return false;
1383 
1384   bool MadeChange = false;
1385   MD = &getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
1386   TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1387 
1388   // If we don't have at least memset and memcpy, there is little point of doing
1389   // anything here.  These are required by a freestanding implementation, so if
1390   // even they are disabled, there is no point in trying hard.
1391   if (!TLI->has(LibFunc::memset) || !TLI->has(LibFunc::memcpy))
1392     return false;
1393 
1394   while (1) {
1395     if (!iterateOnFunction(F))
1396       break;
1397     MadeChange = true;
1398   }
1399 
1400   MD = nullptr;
1401   return MadeChange;
1402 }
1403