blob: 389b3b740c0f52451b366875b4dd2b35cd9ae380 [file] [log] [blame]
Owen Andersona723d1e2008-04-09 08:23:16 +00001//===- 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#define DEBUG_TYPE "memcpyopt"
16#include "llvm/Transforms/Scalar.h"
Owen Andersona723d1e2008-04-09 08:23:16 +000017#include "llvm/IntrinsicInst.h"
18#include "llvm/Instructions.h"
Owen Andersonfa5cbd62009-07-03 19:42:02 +000019#include "llvm/LLVMContext.h"
Owen Andersona723d1e2008-04-09 08:23:16 +000020#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/Statistic.h"
22#include "llvm/Analysis/Dominators.h"
23#include "llvm/Analysis/AliasAnalysis.h"
24#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Owen Andersona723d1e2008-04-09 08:23:16 +000025#include "llvm/Support/Debug.h"
26#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnerbdff5482009-08-23 04:37:46 +000027#include "llvm/Support/raw_ostream.h"
Owen Andersona723d1e2008-04-09 08:23:16 +000028#include "llvm/Target/TargetData.h"
29#include <list>
30using namespace llvm;
31
32STATISTIC(NumMemCpyInstr, "Number of memcpy instructions deleted");
33STATISTIC(NumMemSetInfer, "Number of memsets inferred");
Duncan Sands05cd03b2009-09-03 13:37:16 +000034STATISTIC(NumMoveToCpy, "Number of memmoves converted to memcpy");
Owen Andersona723d1e2008-04-09 08:23:16 +000035
Owen Andersona723d1e2008-04-09 08:23:16 +000036/// isBytewiseValue - If the specified value can be set by repeating the same
37/// byte in memory, return the i8 value that it is represented with. This is
38/// true for all i8 values obviously, but is also true for i32 0, i32 -1,
39/// i16 0xF0F0, double 0.0 etc. If the value can't be handled with a repeated
40/// byte store (e.g. i16 0x1234), return null.
Chris Lattner61c6ba82009-09-01 17:09:55 +000041static Value *isBytewiseValue(Value *V, LLVMContext &Context) {
Owen Andersona723d1e2008-04-09 08:23:16 +000042 // All byte-wide stores are splatable, even of arbitrary variables.
Owen Anderson1d0be152009-08-13 21:58:54 +000043 if (V->getType() == Type::getInt8Ty(Context)) return V;
Owen Andersona723d1e2008-04-09 08:23:16 +000044
45 // Constant float and double values can be handled as integer values if the
46 // corresponding integer value is "byteable". An important case is 0.0.
47 if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
Owen Anderson1d0be152009-08-13 21:58:54 +000048 if (CFP->getType() == Type::getFloatTy(Context))
49 V = ConstantExpr::getBitCast(CFP, Type::getInt32Ty(Context));
50 if (CFP->getType() == Type::getDoubleTy(Context))
51 V = ConstantExpr::getBitCast(CFP, Type::getInt64Ty(Context));
Owen Andersona723d1e2008-04-09 08:23:16 +000052 // Don't handle long double formats, which have strange constraints.
53 }
54
55 // We can handle constant integers that are power of two in size and a
56 // multiple of 8 bits.
57 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
58 unsigned Width = CI->getBitWidth();
59 if (isPowerOf2_32(Width) && Width > 8) {
60 // We can handle this value if the recursive binary decomposition is the
61 // same at all levels.
62 APInt Val = CI->getValue();
63 APInt Val2;
64 while (Val.getBitWidth() != 8) {
65 unsigned NextWidth = Val.getBitWidth()/2;
66 Val2 = Val.lshr(NextWidth);
67 Val2.trunc(Val.getBitWidth()/2);
68 Val.trunc(Val.getBitWidth()/2);
69
70 // If the top/bottom halves aren't the same, reject it.
71 if (Val != Val2)
72 return 0;
73 }
Owen Andersoneed707b2009-07-24 23:12:02 +000074 return ConstantInt::get(Context, Val);
Owen Andersona723d1e2008-04-09 08:23:16 +000075 }
76 }
77
78 // Conceptually, we could handle things like:
79 // %a = zext i8 %X to i16
80 // %b = shl i16 %a, 8
81 // %c = or i16 %a, %b
82 // but until there is an example that actually needs this, it doesn't seem
83 // worth worrying about.
84 return 0;
85}
86
87static int64_t GetOffsetFromIndex(const GetElementPtrInst *GEP, unsigned Idx,
88 bool &VariableIdxFound, TargetData &TD) {
89 // Skip over the first indices.
90 gep_type_iterator GTI = gep_type_begin(GEP);
91 for (unsigned i = 1; i != Idx; ++i, ++GTI)
92 /*skip along*/;
93
94 // Compute the offset implied by the rest of the indices.
95 int64_t Offset = 0;
96 for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
97 ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i));
98 if (OpC == 0)
99 return VariableIdxFound = true;
100 if (OpC->isZero()) continue; // No offset.
101
102 // Handle struct indices, which add their field offset to the pointer.
103 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
104 Offset += TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
105 continue;
106 }
107
108 // Otherwise, we have a sequential type like an array or vector. Multiply
109 // the index by the ElementSize.
Duncan Sands777d2302009-05-09 07:06:46 +0000110 uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType());
Owen Andersona723d1e2008-04-09 08:23:16 +0000111 Offset += Size*OpC->getSExtValue();
112 }
113
114 return Offset;
115}
116
117/// IsPointerOffset - Return true if Ptr1 is provably equal to Ptr2 plus a
118/// constant offset, and return that constant offset. For example, Ptr1 might
119/// be &A[42], and Ptr2 might be &A[40]. In this case offset would be -8.
120static bool IsPointerOffset(Value *Ptr1, Value *Ptr2, int64_t &Offset,
121 TargetData &TD) {
122 // Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical
123 // base. After that base, they may have some number of common (and
124 // potentially variable) indices. After that they handle some constant
125 // offset, which determines their offset from each other. At this point, we
126 // handle no other case.
127 GetElementPtrInst *GEP1 = dyn_cast<GetElementPtrInst>(Ptr1);
128 GetElementPtrInst *GEP2 = dyn_cast<GetElementPtrInst>(Ptr2);
129 if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0))
130 return false;
131
132 // Skip any common indices and track the GEP types.
133 unsigned Idx = 1;
134 for (; Idx != GEP1->getNumOperands() && Idx != GEP2->getNumOperands(); ++Idx)
135 if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx))
136 break;
137
138 bool VariableIdxFound = false;
139 int64_t Offset1 = GetOffsetFromIndex(GEP1, Idx, VariableIdxFound, TD);
140 int64_t Offset2 = GetOffsetFromIndex(GEP2, Idx, VariableIdxFound, TD);
141 if (VariableIdxFound) return false;
142
143 Offset = Offset2-Offset1;
144 return true;
145}
146
147
148/// MemsetRange - Represents a range of memset'd bytes with the ByteVal value.
149/// This allows us to analyze stores like:
150/// store 0 -> P+1
151/// store 0 -> P+0
152/// store 0 -> P+3
153/// store 0 -> P+2
154/// which sometimes happens with stores to arrays of structs etc. When we see
155/// the first store, we make a range [1, 2). The second store extends the range
156/// to [0, 2). The third makes a new range [2, 3). The fourth store joins the
157/// two ranges into [0, 3) which is memset'able.
158namespace {
159struct MemsetRange {
160 // Start/End - A semi range that describes the span that this range covers.
161 // The range is closed at the start and open at the end: [Start, End).
162 int64_t Start, End;
163
164 /// StartPtr - The getelementptr instruction that points to the start of the
165 /// range.
166 Value *StartPtr;
167
168 /// Alignment - The known alignment of the first store.
169 unsigned Alignment;
170
171 /// TheStores - The actual stores that make up this range.
172 SmallVector<StoreInst*, 16> TheStores;
173
174 bool isProfitableToUseMemset(const TargetData &TD) const;
175
176};
177} // end anon namespace
178
179bool MemsetRange::isProfitableToUseMemset(const TargetData &TD) const {
180 // If we found more than 8 stores to merge or 64 bytes, use memset.
181 if (TheStores.size() >= 8 || End-Start >= 64) return true;
182
183 // Assume that the code generator is capable of merging pairs of stores
184 // together if it wants to.
185 if (TheStores.size() <= 2) return false;
186
187 // If we have fewer than 8 stores, it can still be worthwhile to do this.
188 // For example, merging 4 i8 stores into an i32 store is useful almost always.
189 // However, merging 2 32-bit stores isn't useful on a 32-bit architecture (the
190 // memset will be split into 2 32-bit stores anyway) and doing so can
191 // pessimize the llvm optimizer.
192 //
193 // Since we don't have perfect knowledge here, make some assumptions: assume
194 // the maximum GPR width is the same size as the pointer size and assume that
195 // this width can be stored. If so, check to see whether we will end up
196 // actually reducing the number of stores used.
197 unsigned Bytes = unsigned(End-Start);
198 unsigned NumPointerStores = Bytes/TD.getPointerSize();
199
200 // Assume the remaining bytes if any are done a byte at a time.
201 unsigned NumByteStores = Bytes - NumPointerStores*TD.getPointerSize();
202
203 // If we will reduce the # stores (according to this heuristic), do the
204 // transformation. This encourages merging 4 x i8 -> i32 and 2 x i16 -> i32
205 // etc.
206 return TheStores.size() > NumPointerStores+NumByteStores;
207}
208
209
210namespace {
211class MemsetRanges {
212 /// Ranges - A sorted list of the memset ranges. We use std::list here
213 /// because each element is relatively large and expensive to copy.
214 std::list<MemsetRange> Ranges;
215 typedef std::list<MemsetRange>::iterator range_iterator;
216 TargetData &TD;
217public:
218 MemsetRanges(TargetData &td) : TD(td) {}
219
220 typedef std::list<MemsetRange>::const_iterator const_iterator;
221 const_iterator begin() const { return Ranges.begin(); }
222 const_iterator end() const { return Ranges.end(); }
223 bool empty() const { return Ranges.empty(); }
224
225 void addStore(int64_t OffsetFromFirst, StoreInst *SI);
226};
227
228} // end anon namespace
229
230
231/// addStore - Add a new store to the MemsetRanges data structure. This adds a
232/// new range for the specified store at the specified offset, merging into
233/// existing ranges as appropriate.
234void MemsetRanges::addStore(int64_t Start, StoreInst *SI) {
235 int64_t End = Start+TD.getTypeStoreSize(SI->getOperand(0)->getType());
236
237 // Do a linear search of the ranges to see if this can be joined and/or to
238 // find the insertion point in the list. We keep the ranges sorted for
239 // simplicity here. This is a linear search of a linked list, which is ugly,
240 // however the number of ranges is limited, so this won't get crazy slow.
241 range_iterator I = Ranges.begin(), E = Ranges.end();
242
243 while (I != E && Start > I->End)
244 ++I;
245
246 // We now know that I == E, in which case we didn't find anything to merge
247 // with, or that Start <= I->End. If End < I->Start or I == E, then we need
248 // to insert a new range. Handle this now.
249 if (I == E || End < I->Start) {
250 MemsetRange &R = *Ranges.insert(I, MemsetRange());
251 R.Start = Start;
252 R.End = End;
253 R.StartPtr = SI->getPointerOperand();
254 R.Alignment = SI->getAlignment();
255 R.TheStores.push_back(SI);
256 return;
257 }
258
259 // This store overlaps with I, add it.
260 I->TheStores.push_back(SI);
261
262 // At this point, we may have an interval that completely contains our store.
263 // If so, just add it to the interval and return.
264 if (I->Start <= Start && I->End >= End)
265 return;
266
267 // Now we know that Start <= I->End and End >= I->Start so the range overlaps
268 // but is not entirely contained within the range.
269
270 // See if the range extends the start of the range. In this case, it couldn't
271 // possibly cause it to join the prior range, because otherwise we would have
272 // stopped on *it*.
273 if (Start < I->Start) {
274 I->Start = Start;
275 I->StartPtr = SI->getPointerOperand();
276 }
277
278 // Now we know that Start <= I->End and Start >= I->Start (so the startpoint
279 // is in or right at the end of I), and that End >= I->Start. Extend I out to
280 // End.
281 if (End > I->End) {
282 I->End = End;
Nick Lewycky9c0f1462009-03-19 05:51:39 +0000283 range_iterator NextI = I;
Owen Andersona723d1e2008-04-09 08:23:16 +0000284 while (++NextI != E && End >= NextI->Start) {
285 // Merge the range in.
286 I->TheStores.append(NextI->TheStores.begin(), NextI->TheStores.end());
287 if (NextI->End > I->End)
288 I->End = NextI->End;
289 Ranges.erase(NextI);
290 NextI = I;
291 }
292 }
293}
294
295//===----------------------------------------------------------------------===//
296// MemCpyOpt Pass
297//===----------------------------------------------------------------------===//
298
299namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +0000300 class MemCpyOpt : public FunctionPass {
Owen Andersona723d1e2008-04-09 08:23:16 +0000301 bool runOnFunction(Function &F);
302 public:
303 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +0000304 MemCpyOpt() : FunctionPass(&ID) {}
Owen Andersona723d1e2008-04-09 08:23:16 +0000305
306 private:
307 // This transformation requires dominator postdominator info
308 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
309 AU.setPreservesCFG();
310 AU.addRequired<DominatorTree>();
311 AU.addRequired<MemoryDependenceAnalysis>();
312 AU.addRequired<AliasAnalysis>();
Owen Andersona723d1e2008-04-09 08:23:16 +0000313 AU.addPreserved<AliasAnalysis>();
314 AU.addPreserved<MemoryDependenceAnalysis>();
Owen Andersona723d1e2008-04-09 08:23:16 +0000315 }
316
317 // Helper fuctions
Chris Lattner61c6ba82009-09-01 17:09:55 +0000318 bool processStore(StoreInst *SI, BasicBlock::iterator &BBI);
319 bool processMemCpy(MemCpyInst *M);
Chris Lattnerf41eaac2009-09-01 17:56:32 +0000320 bool processMemMove(MemMoveInst *M);
Chris Lattner61c6ba82009-09-01 17:09:55 +0000321 bool performCallSlotOptzn(MemCpyInst *cpy, CallInst *C);
Owen Andersona723d1e2008-04-09 08:23:16 +0000322 bool iterateOnFunction(Function &F);
323 };
324
325 char MemCpyOpt::ID = 0;
326}
327
328// createMemCpyOptPass - The public interface to this file...
329FunctionPass *llvm::createMemCpyOptPass() { return new MemCpyOpt(); }
330
331static RegisterPass<MemCpyOpt> X("memcpyopt",
332 "MemCpy Optimization");
333
334
335
336/// processStore - When GVN is scanning forward over instructions, we look for
337/// some other patterns to fold away. In particular, this looks for stores to
338/// neighboring locations of memory. If it sees enough consequtive ones
339/// (currently 4) it attempts to merge them together into a memcpy/memset.
Chris Lattner61c6ba82009-09-01 17:09:55 +0000340bool MemCpyOpt::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
Owen Andersona723d1e2008-04-09 08:23:16 +0000341 if (SI->isVolatile()) return false;
342
Chris Lattnerff1e98c2009-09-08 00:27:14 +0000343 LLVMContext &Context = SI->getContext();
344
Owen Andersona723d1e2008-04-09 08:23:16 +0000345 // There are two cases that are interesting for this code to handle: memcpy
346 // and memset. Right now we only handle memset.
347
348 // Ensure that the value being stored is something that can be memset'able a
349 // byte at a time like "0" or "-1" or any width, as well as things like
350 // 0xA0A0A0A0 and 0.0.
Chris Lattnerff1e98c2009-09-08 00:27:14 +0000351 Value *ByteVal = isBytewiseValue(SI->getOperand(0), Context);
Owen Andersona723d1e2008-04-09 08:23:16 +0000352 if (!ByteVal)
353 return false;
354
Dan Gohman8942f9bb2009-08-18 01:17:52 +0000355 TargetData *TD = getAnalysisIfAvailable<TargetData>();
356 if (!TD) return false;
Owen Andersona723d1e2008-04-09 08:23:16 +0000357 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Dan Gohmana195b7f2009-07-28 00:37:06 +0000358 Module *M = SI->getParent()->getParent()->getParent();
Owen Andersona723d1e2008-04-09 08:23:16 +0000359
360 // Okay, so we now have a single store that can be splatable. Scan to find
361 // all subsequent stores of the same value to offset from the same pointer.
362 // Join these together into ranges, so we can decide whether contiguous blocks
363 // are stored.
Dan Gohman8942f9bb2009-08-18 01:17:52 +0000364 MemsetRanges Ranges(*TD);
Owen Andersona723d1e2008-04-09 08:23:16 +0000365
366 Value *StartPtr = SI->getPointerOperand();
367
368 BasicBlock::iterator BI = SI;
369 for (++BI; !isa<TerminatorInst>(BI); ++BI) {
370 if (isa<CallInst>(BI) || isa<InvokeInst>(BI)) {
371 // If the call is readnone, ignore it, otherwise bail out. We don't even
372 // allow readonly here because we don't want something like:
373 // A[1] = 2; strlen(A); A[2] = 2; -> memcpy(A, ...); strlen(A).
374 if (AA.getModRefBehavior(CallSite::get(BI)) ==
375 AliasAnalysis::DoesNotAccessMemory)
376 continue;
377
378 // TODO: If this is a memset, try to join it in.
379
380 break;
381 } else if (isa<VAArgInst>(BI) || isa<LoadInst>(BI))
382 break;
383
384 // If this is a non-store instruction it is fine, ignore it.
385 StoreInst *NextStore = dyn_cast<StoreInst>(BI);
386 if (NextStore == 0) continue;
387
388 // If this is a store, see if we can merge it in.
389 if (NextStore->isVolatile()) break;
390
391 // Check to see if this stored value is of the same byte-splattable value.
Chris Lattnerff1e98c2009-09-08 00:27:14 +0000392 if (ByteVal != isBytewiseValue(NextStore->getOperand(0), Context))
Owen Andersona723d1e2008-04-09 08:23:16 +0000393 break;
394
395 // Check to see if this store is to a constant offset from the start ptr.
396 int64_t Offset;
Dan Gohman8942f9bb2009-08-18 01:17:52 +0000397 if (!IsPointerOffset(StartPtr, NextStore->getPointerOperand(), Offset, *TD))
Owen Andersona723d1e2008-04-09 08:23:16 +0000398 break;
399
400 Ranges.addStore(Offset, NextStore);
401 }
402
403 // If we have no ranges, then we just had a single store with nothing that
404 // could be merged in. This is a very common case of course.
405 if (Ranges.empty())
406 return false;
407
408 // If we had at least one store that could be merged in, add the starting
409 // store as well. We try to avoid this unless there is at least something
410 // interesting as a small compile-time optimization.
411 Ranges.addStore(0, SI);
Owen Andersona723d1e2008-04-09 08:23:16 +0000412
413 Function *MemSetF = 0;
414
415 // Now that we have full information about ranges, loop over the ranges and
416 // emit memset's for anything big enough to be worthwhile.
417 bool MadeChange = false;
418 for (MemsetRanges::const_iterator I = Ranges.begin(), E = Ranges.end();
419 I != E; ++I) {
420 const MemsetRange &Range = *I;
421
422 if (Range.TheStores.size() == 1) continue;
423
424 // If it is profitable to lower this range to memset, do so now.
Dan Gohman8942f9bb2009-08-18 01:17:52 +0000425 if (!Range.isProfitableToUseMemset(*TD))
Owen Andersona723d1e2008-04-09 08:23:16 +0000426 continue;
427
428 // Otherwise, we do want to transform this! Create a new memset. We put
429 // the memset right before the first instruction that isn't part of this
430 // memset block. This ensure that the memset is dominated by any addressing
431 // instruction needed by the start of the block.
432 BasicBlock::iterator InsertPt = BI;
433
Chris Lattner824b9582008-11-21 16:42:48 +0000434 if (MemSetF == 0) {
Chris Lattnerff1e98c2009-09-08 00:27:14 +0000435 const Type *Ty = Type::getInt64Ty(Context);
Chris Lattnerf41eaac2009-09-01 17:56:32 +0000436 MemSetF = Intrinsic::getDeclaration(M, Intrinsic::memset, &Ty, 1);
Chris Lattnerff1e98c2009-09-08 00:27:14 +0000437 }
Owen Andersona723d1e2008-04-09 08:23:16 +0000438
439 // Get the starting pointer of the block.
440 StartPtr = Range.StartPtr;
441
442 // Cast the start ptr to be i8* as memset requires.
Chris Lattnerff1e98c2009-09-08 00:27:14 +0000443 const Type *i8Ptr = PointerType::getUnqual(Type::getInt8Ty(Context));
Owen Andersona723d1e2008-04-09 08:23:16 +0000444 if (StartPtr->getType() != i8Ptr)
Daniel Dunbar460f6562009-07-26 09:48:23 +0000445 StartPtr = new BitCastInst(StartPtr, i8Ptr, StartPtr->getName(),
Owen Andersona723d1e2008-04-09 08:23:16 +0000446 InsertPt);
447
448 Value *Ops[] = {
449 StartPtr, ByteVal, // Start, value
Owen Andersone922c022009-07-22 00:24:57 +0000450 // size
Chris Lattnerff1e98c2009-09-08 00:27:14 +0000451 ConstantInt::get(Type::getInt64Ty(Context), Range.End-Range.Start),
Owen Andersone922c022009-07-22 00:24:57 +0000452 // align
Chris Lattnerff1e98c2009-09-08 00:27:14 +0000453 ConstantInt::get(Type::getInt32Ty(Context), Range.Alignment)
Owen Andersona723d1e2008-04-09 08:23:16 +0000454 };
455 Value *C = CallInst::Create(MemSetF, Ops, Ops+4, "", InsertPt);
Chris Lattnerbdff5482009-08-23 04:37:46 +0000456 DEBUG(errs() << "Replace stores:\n";
Owen Andersona723d1e2008-04-09 08:23:16 +0000457 for (unsigned i = 0, e = Range.TheStores.size(); i != e; ++i)
Chris Lattnerbdff5482009-08-23 04:37:46 +0000458 errs() << *Range.TheStores[i];
459 errs() << "With: " << *C); C=C;
Owen Andersona723d1e2008-04-09 08:23:16 +0000460
Owen Andersona8bd6582008-04-21 07:45:10 +0000461 // Don't invalidate the iterator
462 BBI = BI;
463
Owen Andersona723d1e2008-04-09 08:23:16 +0000464 // Zap all the stores.
Chris Lattnerff1e98c2009-09-08 00:27:14 +0000465 for (SmallVector<StoreInst*, 16>::const_iterator
466 SI = Range.TheStores.begin(),
Owen Andersona8bd6582008-04-21 07:45:10 +0000467 SE = Range.TheStores.end(); SI != SE; ++SI)
468 (*SI)->eraseFromParent();
Owen Andersona723d1e2008-04-09 08:23:16 +0000469 ++NumMemSetInfer;
470 MadeChange = true;
471 }
472
473 return MadeChange;
474}
475
476
477/// performCallSlotOptzn - takes a memcpy and a call that it depends on,
478/// and checks for the possibility of a call slot optimization by having
479/// the call write its result directly into the destination of the memcpy.
Owen Andersona8bd6582008-04-21 07:45:10 +0000480bool MemCpyOpt::performCallSlotOptzn(MemCpyInst *cpy, CallInst *C) {
Owen Andersona723d1e2008-04-09 08:23:16 +0000481 // The general transformation to keep in mind is
482 //
483 // call @func(..., src, ...)
484 // memcpy(dest, src, ...)
485 //
486 // ->
487 //
488 // memcpy(dest, src, ...)
489 // call @func(..., dest, ...)
490 //
491 // Since moving the memcpy is technically awkward, we additionally check that
492 // src only holds uninitialized values at the moment of the call, meaning that
493 // the memcpy can be discarded rather than moved.
494
495 // Deliberately get the source and destination with bitcasts stripped away,
496 // because we'll need to do type comparisons based on the underlying type.
Chris Lattner61c6ba82009-09-01 17:09:55 +0000497 Value *cpyDest = cpy->getDest();
498 Value *cpySrc = cpy->getSource();
Owen Andersona723d1e2008-04-09 08:23:16 +0000499 CallSite CS = CallSite::get(C);
500
501 // We need to be able to reason about the size of the memcpy, so we require
502 // that it be a constant.
Chris Lattner61c6ba82009-09-01 17:09:55 +0000503 ConstantInt *cpyLength = dyn_cast<ConstantInt>(cpy->getLength());
Owen Andersona723d1e2008-04-09 08:23:16 +0000504 if (!cpyLength)
505 return false;
506
507 // Require that src be an alloca. This simplifies the reasoning considerably.
Chris Lattner61c6ba82009-09-01 17:09:55 +0000508 AllocaInst *srcAlloca = dyn_cast<AllocaInst>(cpySrc);
Owen Andersona723d1e2008-04-09 08:23:16 +0000509 if (!srcAlloca)
510 return false;
511
512 // Check that all of src is copied to dest.
Chris Lattner61c6ba82009-09-01 17:09:55 +0000513 TargetData *TD = getAnalysisIfAvailable<TargetData>();
Dan Gohman8942f9bb2009-08-18 01:17:52 +0000514 if (!TD) return false;
Owen Andersona723d1e2008-04-09 08:23:16 +0000515
Chris Lattner61c6ba82009-09-01 17:09:55 +0000516 ConstantInt *srcArraySize = dyn_cast<ConstantInt>(srcAlloca->getArraySize());
Owen Andersona723d1e2008-04-09 08:23:16 +0000517 if (!srcArraySize)
518 return false;
519
Dan Gohman8942f9bb2009-08-18 01:17:52 +0000520 uint64_t srcSize = TD->getTypeAllocSize(srcAlloca->getAllocatedType()) *
Owen Andersona723d1e2008-04-09 08:23:16 +0000521 srcArraySize->getZExtValue();
522
523 if (cpyLength->getZExtValue() < srcSize)
524 return false;
525
526 // Check that accessing the first srcSize bytes of dest will not cause a
527 // trap. Otherwise the transform is invalid since it might cause a trap
528 // to occur earlier than it otherwise would.
Chris Lattner61c6ba82009-09-01 17:09:55 +0000529 if (AllocaInst *A = dyn_cast<AllocaInst>(cpyDest)) {
Owen Andersona723d1e2008-04-09 08:23:16 +0000530 // The destination is an alloca. Check it is larger than srcSize.
Chris Lattner61c6ba82009-09-01 17:09:55 +0000531 ConstantInt *destArraySize = dyn_cast<ConstantInt>(A->getArraySize());
Owen Andersona723d1e2008-04-09 08:23:16 +0000532 if (!destArraySize)
533 return false;
534
Dan Gohman8942f9bb2009-08-18 01:17:52 +0000535 uint64_t destSize = TD->getTypeAllocSize(A->getAllocatedType()) *
Owen Andersona723d1e2008-04-09 08:23:16 +0000536 destArraySize->getZExtValue();
537
538 if (destSize < srcSize)
539 return false;
Chris Lattner61c6ba82009-09-01 17:09:55 +0000540 } else if (Argument *A = dyn_cast<Argument>(cpyDest)) {
Owen Andersona723d1e2008-04-09 08:23:16 +0000541 // If the destination is an sret parameter then only accesses that are
542 // outside of the returned struct type can trap.
543 if (!A->hasStructRetAttr())
544 return false;
545
Chris Lattner61c6ba82009-09-01 17:09:55 +0000546 const Type *StructTy = cast<PointerType>(A->getType())->getElementType();
Dan Gohman8942f9bb2009-08-18 01:17:52 +0000547 uint64_t destSize = TD->getTypeAllocSize(StructTy);
Owen Andersona723d1e2008-04-09 08:23:16 +0000548
549 if (destSize < srcSize)
550 return false;
551 } else {
552 return false;
553 }
554
555 // Check that src is not accessed except via the call and the memcpy. This
556 // guarantees that it holds only undefined values when passed in (so the final
557 // memcpy can be dropped), that it is not read or written between the call and
558 // the memcpy, and that writing beyond the end of it is undefined.
559 SmallVector<User*, 8> srcUseList(srcAlloca->use_begin(),
560 srcAlloca->use_end());
561 while (!srcUseList.empty()) {
Chris Lattner61c6ba82009-09-01 17:09:55 +0000562 User *UI = srcUseList.back();
Owen Andersona723d1e2008-04-09 08:23:16 +0000563 srcUseList.pop_back();
564
Owen Anderson009e4f72008-06-01 22:26:26 +0000565 if (isa<BitCastInst>(UI)) {
Owen Andersona723d1e2008-04-09 08:23:16 +0000566 for (User::use_iterator I = UI->use_begin(), E = UI->use_end();
567 I != E; ++I)
568 srcUseList.push_back(*I);
Chris Lattner61c6ba82009-09-01 17:09:55 +0000569 } else if (GetElementPtrInst *G = dyn_cast<GetElementPtrInst>(UI)) {
Owen Anderson009e4f72008-06-01 22:26:26 +0000570 if (G->hasAllZeroIndices())
571 for (User::use_iterator I = UI->use_begin(), E = UI->use_end();
572 I != E; ++I)
573 srcUseList.push_back(*I);
574 else
575 return false;
Owen Andersona723d1e2008-04-09 08:23:16 +0000576 } else if (UI != C && UI != cpy) {
577 return false;
578 }
579 }
580
581 // Since we're changing the parameter to the callsite, we need to make sure
582 // that what would be the new parameter dominates the callsite.
Chris Lattner61c6ba82009-09-01 17:09:55 +0000583 DominatorTree &DT = getAnalysis<DominatorTree>();
584 if (Instruction *cpyDestInst = dyn_cast<Instruction>(cpyDest))
Owen Andersona723d1e2008-04-09 08:23:16 +0000585 if (!DT.dominates(cpyDestInst, C))
586 return false;
587
588 // In addition to knowing that the call does not access src in some
589 // unexpected manner, for example via a global, which we deduce from
590 // the use analysis, we also need to know that it does not sneakily
591 // access dest. We rely on AA to figure this out for us.
Chris Lattner61c6ba82009-09-01 17:09:55 +0000592 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Owen Andersona723d1e2008-04-09 08:23:16 +0000593 if (AA.getModRefInfo(C, cpy->getRawDest(), srcSize) !=
594 AliasAnalysis::NoModRef)
595 return false;
596
597 // All the checks have passed, so do the transformation.
Owen Anderson12cb36c2008-06-01 21:52:16 +0000598 bool changedArgument = false;
Owen Andersona723d1e2008-04-09 08:23:16 +0000599 for (unsigned i = 0; i < CS.arg_size(); ++i)
Owen Anderson009e4f72008-06-01 22:26:26 +0000600 if (CS.getArgument(i)->stripPointerCasts() == cpySrc) {
Owen Andersona723d1e2008-04-09 08:23:16 +0000601 if (cpySrc->getType() != cpyDest->getType())
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000602 cpyDest = CastInst::CreatePointerCast(cpyDest, cpySrc->getType(),
Owen Andersona723d1e2008-04-09 08:23:16 +0000603 cpyDest->getName(), C);
Owen Anderson12cb36c2008-06-01 21:52:16 +0000604 changedArgument = true;
Chris Lattner61c6ba82009-09-01 17:09:55 +0000605 if (CS.getArgument(i)->getType() == cpyDest->getType())
Owen Anderson009e4f72008-06-01 22:26:26 +0000606 CS.setArgument(i, cpyDest);
Chris Lattner61c6ba82009-09-01 17:09:55 +0000607 else
608 CS.setArgument(i, CastInst::CreatePointerCast(cpyDest,
609 CS.getArgument(i)->getType(), cpyDest->getName(), C));
Owen Andersona723d1e2008-04-09 08:23:16 +0000610 }
611
Owen Anderson12cb36c2008-06-01 21:52:16 +0000612 if (!changedArgument)
613 return false;
614
Owen Andersona723d1e2008-04-09 08:23:16 +0000615 // Drop any cached information about the call, because we may have changed
616 // its dependence information by changing its parameter.
Chris Lattner61c6ba82009-09-01 17:09:55 +0000617 MemoryDependenceAnalysis &MD = getAnalysis<MemoryDependenceAnalysis>();
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000618 MD.removeInstruction(C);
Owen Andersona723d1e2008-04-09 08:23:16 +0000619
620 // Remove the memcpy
621 MD.removeInstruction(cpy);
Owen Andersona8bd6582008-04-21 07:45:10 +0000622 cpy->eraseFromParent();
623 NumMemCpyInstr++;
Owen Andersona723d1e2008-04-09 08:23:16 +0000624
625 return true;
626}
627
628/// processMemCpy - perform simplication of memcpy's. If we have memcpy A which
629/// copies X to Y, and memcpy B which copies Y to Z, then we can rewrite B to be
630/// a memcpy from X to Z (or potentially a memmove, depending on circumstances).
631/// This allows later passes to remove the first memcpy altogether.
Chris Lattner61c6ba82009-09-01 17:09:55 +0000632bool MemCpyOpt::processMemCpy(MemCpyInst *M) {
633 MemoryDependenceAnalysis &MD = getAnalysis<MemoryDependenceAnalysis>();
Owen Andersona8bd6582008-04-21 07:45:10 +0000634
635 // The are two possible optimizations we can do for memcpy:
Chris Lattner61c6ba82009-09-01 17:09:55 +0000636 // a) memcpy-memcpy xform which exposes redundance for DSE.
637 // b) call-memcpy xform for return slot optimization.
Chris Lattner4c724002008-11-29 02:29:27 +0000638 MemDepResult dep = MD.getDependency(M);
Chris Lattnerb51deb92008-12-05 21:04:20 +0000639 if (!dep.isClobber())
Owen Andersona8bd6582008-04-21 07:45:10 +0000640 return false;
Chris Lattnerb51deb92008-12-05 21:04:20 +0000641 if (!isa<MemCpyInst>(dep.getInst())) {
Chris Lattner61c6ba82009-09-01 17:09:55 +0000642 if (CallInst *C = dyn_cast<CallInst>(dep.getInst()))
Owen Anderson9dcace32008-04-29 21:26:06 +0000643 return performCallSlotOptzn(M, C);
Chris Lattnerb51deb92008-12-05 21:04:20 +0000644 return false;
Owen Anderson9dcace32008-04-29 21:26:06 +0000645 }
Owen Andersona8bd6582008-04-21 07:45:10 +0000646
Chris Lattner61c6ba82009-09-01 17:09:55 +0000647 MemCpyInst *MDep = cast<MemCpyInst>(dep.getInst());
Owen Andersona8bd6582008-04-21 07:45:10 +0000648
Owen Andersona723d1e2008-04-09 08:23:16 +0000649 // We can only transforms memcpy's where the dest of one is the source of the
650 // other
651 if (M->getSource() != MDep->getDest())
652 return false;
653
654 // Second, the length of the memcpy's must be the same, or the preceeding one
655 // must be larger than the following one.
Chris Lattner61c6ba82009-09-01 17:09:55 +0000656 ConstantInt *C1 = dyn_cast<ConstantInt>(MDep->getLength());
657 ConstantInt *C2 = dyn_cast<ConstantInt>(M->getLength());
Owen Andersona723d1e2008-04-09 08:23:16 +0000658 if (!C1 || !C2)
659 return false;
660
661 uint64_t DepSize = C1->getValue().getZExtValue();
662 uint64_t CpySize = C2->getValue().getZExtValue();
663
664 if (DepSize < CpySize)
665 return false;
666
667 // Finally, we have to make sure that the dest of the second does not
668 // alias the source of the first
Chris Lattner61c6ba82009-09-01 17:09:55 +0000669 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Owen Andersona723d1e2008-04-09 08:23:16 +0000670 if (AA.alias(M->getRawDest(), CpySize, MDep->getRawSource(), DepSize) !=
671 AliasAnalysis::NoAlias)
672 return false;
673 else if (AA.alias(M->getRawDest(), CpySize, M->getRawSource(), CpySize) !=
674 AliasAnalysis::NoAlias)
675 return false;
676 else if (AA.alias(MDep->getRawDest(), DepSize, MDep->getRawSource(), DepSize)
677 != AliasAnalysis::NoAlias)
678 return false;
679
680 // If all checks passed, then we can transform these memcpy's
Chris Lattnerf41eaac2009-09-01 17:56:32 +0000681 const Type *Ty = M->getLength()->getType();
Chris Lattner61c6ba82009-09-01 17:09:55 +0000682 Function *MemCpyFun = Intrinsic::getDeclaration(
Owen Andersona723d1e2008-04-09 08:23:16 +0000683 M->getParent()->getParent()->getParent(),
Chris Lattnerf41eaac2009-09-01 17:56:32 +0000684 M->getIntrinsicID(), &Ty, 1);
Owen Andersona723d1e2008-04-09 08:23:16 +0000685
Chris Lattnerdfe964c2009-03-08 03:59:00 +0000686 Value *Args[4] = {
687 M->getRawDest(), MDep->getRawSource(), M->getLength(), M->getAlignmentCst()
688 };
Owen Andersona723d1e2008-04-09 08:23:16 +0000689
Chris Lattner61c6ba82009-09-01 17:09:55 +0000690 CallInst *C = CallInst::Create(MemCpyFun, Args, Args+4, "", M);
Owen Andersona723d1e2008-04-09 08:23:16 +0000691
Owen Anderson02e99882008-04-29 21:51:00 +0000692
693 // If C and M don't interfere, then this is a valid transformation. If they
694 // did, this would mean that the two sources overlap, which would be bad.
Chris Lattner39f372e2008-11-29 01:43:36 +0000695 if (MD.getDependency(C) == dep) {
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000696 MD.removeInstruction(M);
Owen Andersona8bd6582008-04-21 07:45:10 +0000697 M->eraseFromParent();
Owen Anderson02e99882008-04-29 21:51:00 +0000698 NumMemCpyInstr++;
Owen Andersona723d1e2008-04-09 08:23:16 +0000699 return true;
700 }
701
Owen Anderson02e99882008-04-29 21:51:00 +0000702 // Otherwise, there was no point in doing this, so we remove the call we
703 // inserted and act like nothing happened.
Owen Andersona723d1e2008-04-09 08:23:16 +0000704 MD.removeInstruction(C);
Owen Andersona8bd6582008-04-21 07:45:10 +0000705 C->eraseFromParent();
Owen Anderson02e99882008-04-29 21:51:00 +0000706 return false;
Owen Andersona723d1e2008-04-09 08:23:16 +0000707}
708
Chris Lattnerf41eaac2009-09-01 17:56:32 +0000709/// processMemMove - Transforms memmove calls to memcpy calls when the src/dst
710/// are guaranteed not to alias.
711bool MemCpyOpt::processMemMove(MemMoveInst *M) {
712 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
713
714 // If the memmove is a constant size, use it for the alias query, this allows
715 // us to optimize things like: memmove(P, P+64, 64);
716 uint64_t MemMoveSize = ~0ULL;
717 if (ConstantInt *Len = dyn_cast<ConstantInt>(M->getLength()))
718 MemMoveSize = Len->getZExtValue();
719
720 // See if the pointers alias.
721 if (AA.alias(M->getRawDest(), MemMoveSize, M->getRawSource(), MemMoveSize) !=
722 AliasAnalysis::NoAlias)
723 return false;
724
725 DEBUG(errs() << "MemCpyOpt: Optimizing memmove -> memcpy: " << *M << "\n");
726
727 // If not, then we know we can transform this.
728 Module *Mod = M->getParent()->getParent()->getParent();
729 const Type *Ty = M->getLength()->getType();
730 M->setOperand(0, Intrinsic::getDeclaration(Mod, Intrinsic::memcpy, &Ty, 1));
Duncan Sands05cd03b2009-09-03 13:37:16 +0000731
Chris Lattnerf41eaac2009-09-01 17:56:32 +0000732 // MemDep may have over conservative information about this instruction, just
733 // conservatively flush it from the cache.
734 getAnalysis<MemoryDependenceAnalysis>().removeInstruction(M);
Duncan Sands05cd03b2009-09-03 13:37:16 +0000735
736 ++NumMoveToCpy;
Chris Lattnerf41eaac2009-09-01 17:56:32 +0000737 return true;
738}
739
740
Chris Lattner61c6ba82009-09-01 17:09:55 +0000741// MemCpyOpt::iterateOnFunction - Executes one iteration of GVN.
Owen Andersona723d1e2008-04-09 08:23:16 +0000742bool MemCpyOpt::iterateOnFunction(Function &F) {
Chris Lattner61c6ba82009-09-01 17:09:55 +0000743 bool MadeChange = false;
Owen Andersona723d1e2008-04-09 08:23:16 +0000744
Chris Lattner61c6ba82009-09-01 17:09:55 +0000745 // Walk all instruction in the function.
Owen Andersona8bd6582008-04-21 07:45:10 +0000746 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) {
Owen Andersona723d1e2008-04-09 08:23:16 +0000747 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
748 BI != BE;) {
Chris Lattner61c6ba82009-09-01 17:09:55 +0000749 // Avoid invalidating the iterator.
750 Instruction *I = BI++;
Owen Andersona8bd6582008-04-21 07:45:10 +0000751
752 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner61c6ba82009-09-01 17:09:55 +0000753 MadeChange |= processStore(SI, BI);
754 else if (MemCpyInst *M = dyn_cast<MemCpyInst>(I))
755 MadeChange |= processMemCpy(M);
Chris Lattnerf41eaac2009-09-01 17:56:32 +0000756 else if (MemMoveInst *M = dyn_cast<MemMoveInst>(I)) {
757 if (processMemMove(M)) {
758 --BI; // Reprocess the new memcpy.
759 MadeChange = true;
760 }
761 }
Owen Andersona723d1e2008-04-09 08:23:16 +0000762 }
763 }
764
Chris Lattner61c6ba82009-09-01 17:09:55 +0000765 return MadeChange;
Owen Andersona723d1e2008-04-09 08:23:16 +0000766}
Chris Lattner61c6ba82009-09-01 17:09:55 +0000767
768// MemCpyOpt::runOnFunction - This is the main transformation entry point for a
769// function.
770//
771bool MemCpyOpt::runOnFunction(Function &F) {
772 bool MadeChange = false;
773 while (1) {
774 if (!iterateOnFunction(F))
775 break;
776 MadeChange = true;
777 }
778
779 return MadeChange;
780}
781
782
783