LLVM API Documentation

MemCpyOptimizer.cpp

Go to the documentation of this file.
00001 //===- MemCpyOptimizer.cpp - Optimize use of memcpy and friends -----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This pass performs various transformations related to eliminating memcpy
00011 // calls, or transforming sets of stores into memset's.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #define DEBUG_TYPE "memcpyopt"
00016 #include "llvm/Transforms/Scalar.h"
00017 #include "llvm/IntrinsicInst.h"
00018 #include "llvm/Instructions.h"
00019 #include "llvm/ADT/SmallVector.h"
00020 #include "llvm/ADT/Statistic.h"
00021 #include "llvm/Analysis/Dominators.h"
00022 #include "llvm/Analysis/AliasAnalysis.h"
00023 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
00024 #include "llvm/Support/Debug.h"
00025 #include "llvm/Support/GetElementPtrTypeIterator.h"
00026 #include "llvm/Target/TargetData.h"
00027 #include <list>
00028 using namespace llvm;
00029 
00030 STATISTIC(NumMemCpyInstr, "Number of memcpy instructions deleted");
00031 STATISTIC(NumMemSetInfer, "Number of memsets inferred");
00032 
00033 /// isBytewiseValue - If the specified value can be set by repeating the same
00034 /// byte in memory, return the i8 value that it is represented with.  This is
00035 /// true for all i8 values obviously, but is also true for i32 0, i32 -1,
00036 /// i16 0xF0F0, double 0.0 etc.  If the value can't be handled with a repeated
00037 /// byte store (e.g. i16 0x1234), return null.
00038 static Value *isBytewiseValue(Value *V) {
00039   // All byte-wide stores are splatable, even of arbitrary variables.
00040   if (V->getType() == Type::Int8Ty) return V;
00041   
00042   // Constant float and double values can be handled as integer values if the
00043   // corresponding integer value is "byteable".  An important case is 0.0. 
00044   if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
00045     if (CFP->getType() == Type::FloatTy)
00046       V = ConstantExpr::getBitCast(CFP, Type::Int32Ty);
00047     if (CFP->getType() == Type::DoubleTy)
00048       V = ConstantExpr::getBitCast(CFP, Type::Int64Ty);
00049     // Don't handle long double formats, which have strange constraints.
00050   }
00051   
00052   // We can handle constant integers that are power of two in size and a 
00053   // multiple of 8 bits.
00054   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
00055     unsigned Width = CI->getBitWidth();
00056     if (isPowerOf2_32(Width) && Width > 8) {
00057       // We can handle this value if the recursive binary decomposition is the
00058       // same at all levels.
00059       APInt Val = CI->getValue();
00060       APInt Val2;
00061       while (Val.getBitWidth() != 8) {
00062         unsigned NextWidth = Val.getBitWidth()/2;
00063         Val2  = Val.lshr(NextWidth);
00064         Val2.trunc(Val.getBitWidth()/2);
00065         Val.trunc(Val.getBitWidth()/2);
00066 
00067         // If the top/bottom halves aren't the same, reject it.
00068         if (Val != Val2)
00069           return 0;
00070       }
00071       return ConstantInt::get(Val);
00072     }
00073   }
00074   
00075   // Conceptually, we could handle things like:
00076   //   %a = zext i8 %X to i16
00077   //   %b = shl i16 %a, 8
00078   //   %c = or i16 %a, %b
00079   // but until there is an example that actually needs this, it doesn't seem
00080   // worth worrying about.
00081   return 0;
00082 }
00083 
00084 static int64_t GetOffsetFromIndex(const GetElementPtrInst *GEP, unsigned Idx,
00085                                   bool &VariableIdxFound, TargetData &TD) {
00086   // Skip over the first indices.
00087   gep_type_iterator GTI = gep_type_begin(GEP);
00088   for (unsigned i = 1; i != Idx; ++i, ++GTI)
00089     /*skip along*/;
00090   
00091   // Compute the offset implied by the rest of the indices.
00092   int64_t Offset = 0;
00093   for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
00094     ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i));
00095     if (OpC == 0)
00096       return VariableIdxFound = true;
00097     if (OpC->isZero()) continue;  // No offset.
00098 
00099     // Handle struct indices, which add their field offset to the pointer.
00100     if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
00101       Offset += TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
00102       continue;
00103     }
00104     
00105     // Otherwise, we have a sequential type like an array or vector.  Multiply
00106     // the index by the ElementSize.
00107     uint64_t Size = TD.getTypePaddedSize(GTI.getIndexedType());
00108     Offset += Size*OpC->getSExtValue();
00109   }
00110 
00111   return Offset;
00112 }
00113 
00114 /// IsPointerOffset - Return true if Ptr1 is provably equal to Ptr2 plus a
00115 /// constant offset, and return that constant offset.  For example, Ptr1 might
00116 /// be &A[42], and Ptr2 might be &A[40].  In this case offset would be -8.
00117 static bool IsPointerOffset(Value *Ptr1, Value *Ptr2, int64_t &Offset,
00118                             TargetData &TD) {
00119   // Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical
00120   // base.  After that base, they may have some number of common (and
00121   // potentially variable) indices.  After that they handle some constant
00122   // offset, which determines their offset from each other.  At this point, we
00123   // handle no other case.
00124   GetElementPtrInst *GEP1 = dyn_cast<GetElementPtrInst>(Ptr1);
00125   GetElementPtrInst *GEP2 = dyn_cast<GetElementPtrInst>(Ptr2);
00126   if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0))
00127     return false;
00128   
00129   // Skip any common indices and track the GEP types.
00130   unsigned Idx = 1;
00131   for (; Idx != GEP1->getNumOperands() && Idx != GEP2->getNumOperands(); ++Idx)
00132     if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx))
00133       break;
00134 
00135   bool VariableIdxFound = false;
00136   int64_t Offset1 = GetOffsetFromIndex(GEP1, Idx, VariableIdxFound, TD);
00137   int64_t Offset2 = GetOffsetFromIndex(GEP2, Idx, VariableIdxFound, TD);
00138   if (VariableIdxFound) return false;
00139   
00140   Offset = Offset2-Offset1;
00141   return true;
00142 }
00143 
00144 
00145 /// MemsetRange - Represents a range of memset'd bytes with the ByteVal value.
00146 /// This allows us to analyze stores like:
00147 ///   store 0 -> P+1
00148 ///   store 0 -> P+0
00149 ///   store 0 -> P+3
00150 ///   store 0 -> P+2
00151 /// which sometimes happens with stores to arrays of structs etc.  When we see
00152 /// the first store, we make a range [1, 2).  The second store extends the range
00153 /// to [0, 2).  The third makes a new range [2, 3).  The fourth store joins the
00154 /// two ranges into [0, 3) which is memset'able.
00155 namespace {
00156 struct MemsetRange {
00157   // Start/End - A semi range that describes the span that this range covers.
00158   // The range is closed at the start and open at the end: [Start, End).  
00159   int64_t Start, End;
00160 
00161   /// StartPtr - The getelementptr instruction that points to the start of the
00162   /// range.
00163   Value *StartPtr;
00164   
00165   /// Alignment - The known alignment of the first store.
00166   unsigned Alignment;
00167   
00168   /// TheStores - The actual stores that make up this range.
00169   SmallVector<StoreInst*, 16> TheStores;
00170   
00171   bool isProfitableToUseMemset(const TargetData &TD) const;
00172 
00173 };
00174 } // end anon namespace
00175 
00176 bool MemsetRange::isProfitableToUseMemset(const TargetData &TD) const {
00177   // If we found more than 8 stores to merge or 64 bytes, use memset.
00178   if (TheStores.size() >= 8 || End-Start >= 64) return true;
00179   
00180   // Assume that the code generator is capable of merging pairs of stores
00181   // together if it wants to.
00182   if (TheStores.size() <= 2) return false;
00183   
00184   // If we have fewer than 8 stores, it can still be worthwhile to do this.
00185   // For example, merging 4 i8 stores into an i32 store is useful almost always.
00186   // However, merging 2 32-bit stores isn't useful on a 32-bit architecture (the
00187   // memset will be split into 2 32-bit stores anyway) and doing so can
00188   // pessimize the llvm optimizer.
00189   //
00190   // Since we don't have perfect knowledge here, make some assumptions: assume
00191   // the maximum GPR width is the same size as the pointer size and assume that
00192   // this width can be stored.  If so, check to see whether we will end up
00193   // actually reducing the number of stores used.
00194   unsigned Bytes = unsigned(End-Start);
00195   unsigned NumPointerStores = Bytes/TD.getPointerSize();
00196   
00197   // Assume the remaining bytes if any are done a byte at a time.
00198   unsigned NumByteStores = Bytes - NumPointerStores*TD.getPointerSize();
00199   
00200   // If we will reduce the # stores (according to this heuristic), do the
00201   // transformation.  This encourages merging 4 x i8 -> i32 and 2 x i16 -> i32
00202   // etc.
00203   return TheStores.size() > NumPointerStores+NumByteStores;
00204 }    
00205 
00206 
00207 namespace {
00208 class MemsetRanges {
00209   /// Ranges - A sorted list of the memset ranges.  We use std::list here
00210   /// because each element is relatively large and expensive to copy.
00211   std::list<MemsetRange> Ranges;
00212   typedef std::list<MemsetRange>::iterator range_iterator;
00213   TargetData &TD;
00214 public:
00215   MemsetRanges(TargetData &td) : TD(td) {}
00216   
00217   typedef std::list<MemsetRange>::const_iterator const_iterator;
00218   const_iterator begin() const { return Ranges.begin(); }
00219   const_iterator end() const { return Ranges.end(); }
00220   bool empty() const { return Ranges.empty(); }
00221   
00222   void addStore(int64_t OffsetFromFirst, StoreInst *SI);
00223 };
00224   
00225 } // end anon namespace
00226 
00227 
00228 /// addStore - Add a new store to the MemsetRanges data structure.  This adds a
00229 /// new range for the specified store at the specified offset, merging into
00230 /// existing ranges as appropriate.
00231 void MemsetRanges::addStore(int64_t Start, StoreInst *SI) {
00232   int64_t End = Start+TD.getTypeStoreSize(SI->getOperand(0)->getType());
00233   
00234   // Do a linear search of the ranges to see if this can be joined and/or to
00235   // find the insertion point in the list.  We keep the ranges sorted for
00236   // simplicity here.  This is a linear search of a linked list, which is ugly,
00237   // however the number of ranges is limited, so this won't get crazy slow.
00238   range_iterator I = Ranges.begin(), E = Ranges.end();
00239   
00240   while (I != E && Start > I->End)
00241     ++I;
00242   
00243   // We now know that I == E, in which case we didn't find anything to merge
00244   // with, or that Start <= I->End.  If End < I->Start or I == E, then we need
00245   // to insert a new range.  Handle this now.
00246   if (I == E || End < I->Start) {
00247     MemsetRange &R = *Ranges.insert(I, MemsetRange());
00248     R.Start        = Start;
00249     R.End          = End;
00250     R.StartPtr     = SI->getPointerOperand();
00251     R.Alignment    = SI->getAlignment();
00252     R.TheStores.push_back(SI);
00253     return;
00254   }
00255 
00256   // This store overlaps with I, add it.
00257   I->TheStores.push_back(SI);
00258   
00259   // At this point, we may have an interval that completely contains our store.
00260   // If so, just add it to the interval and return.
00261   if (I->Start <= Start && I->End >= End)
00262     return;
00263   
00264   // Now we know that Start <= I->End and End >= I->Start so the range overlaps
00265   // but is not entirely contained within the range.
00266   
00267   // See if the range extends the start of the range.  In this case, it couldn't
00268   // possibly cause it to join the prior range, because otherwise we would have
00269   // stopped on *it*.
00270   if (Start < I->Start) {
00271     I->Start = Start;
00272     I->StartPtr = SI->getPointerOperand();
00273   }
00274     
00275   // Now we know that Start <= I->End and Start >= I->Start (so the startpoint
00276   // is in or right at the end of I), and that End >= I->Start.  Extend I out to
00277   // End.
00278   if (End > I->End) {
00279     I->End = End;
00280     range_iterator NextI = I;;
00281     while (++NextI != E && End >= NextI->Start) {
00282       // Merge the range in.
00283       I->TheStores.append(NextI->TheStores.begin(), NextI->TheStores.end());
00284       if (NextI->End > I->End)
00285         I->End = NextI->End;
00286       Ranges.erase(NextI);
00287       NextI = I;
00288     }
00289   }
00290 }
00291 
00292 //===----------------------------------------------------------------------===//
00293 //                         MemCpyOpt Pass
00294 //===----------------------------------------------------------------------===//
00295 
00296 namespace {
00297 
00298   class VISIBILITY_HIDDEN MemCpyOpt : public FunctionPass {
00299     bool runOnFunction(Function &F);
00300   public:
00301     static char ID; // Pass identification, replacement for typeid
00302     MemCpyOpt() : FunctionPass(&ID) {}
00303 
00304   private:
00305     // This transformation requires dominator postdominator info
00306     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00307       AU.setPreservesCFG();
00308       AU.addRequired<DominatorTree>();
00309       AU.addRequired<MemoryDependenceAnalysis>();
00310       AU.addRequired<AliasAnalysis>();
00311       AU.addRequired<TargetData>();
00312       AU.addPreserved<AliasAnalysis>();
00313       AU.addPreserved<MemoryDependenceAnalysis>();
00314       AU.addPreserved<TargetData>();
00315     }
00316   
00317     // Helper fuctions
00318     bool processStore(StoreInst *SI, BasicBlock::iterator& BBI);
00319     bool processMemCpy(MemCpyInst* M);
00320     bool performCallSlotOptzn(MemCpyInst* cpy, CallInst* C);
00321     bool iterateOnFunction(Function &F);
00322   };
00323   
00324   char MemCpyOpt::ID = 0;
00325 }
00326 
00327 // createMemCpyOptPass - The public interface to this file...
00328 FunctionPass *llvm::createMemCpyOptPass() { return new MemCpyOpt(); }
00329 
00330 static RegisterPass<MemCpyOpt> X("memcpyopt",
00331                                  "MemCpy Optimization");
00332 
00333 
00334 
00335 /// processStore - When GVN is scanning forward over instructions, we look for
00336 /// some other patterns to fold away.  In particular, this looks for stores to
00337 /// neighboring locations of memory.  If it sees enough consequtive ones
00338 /// (currently 4) it attempts to merge them together into a memcpy/memset.
00339 bool MemCpyOpt::processStore(StoreInst *SI, BasicBlock::iterator& BBI) {
00340   if (SI->isVolatile()) return false;
00341   
00342   // There are two cases that are interesting for this code to handle: memcpy
00343   // and memset.  Right now we only handle memset.
00344   
00345   // Ensure that the value being stored is something that can be memset'able a
00346   // byte at a time like "0" or "-1" or any width, as well as things like
00347   // 0xA0A0A0A0 and 0.0.
00348   Value *ByteVal = isBytewiseValue(SI->getOperand(0));
00349   if (!ByteVal)
00350     return false;
00351 
00352   TargetData &TD = getAnalysis<TargetData>();
00353   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
00354 
00355   // Okay, so we now have a single store that can be splatable.  Scan to find
00356   // all subsequent stores of the same value to offset from the same pointer.
00357   // Join these together into ranges, so we can decide whether contiguous blocks
00358   // are stored.
00359   MemsetRanges Ranges(TD);
00360   
00361   Value *StartPtr = SI->getPointerOperand();
00362   
00363   BasicBlock::iterator BI = SI;
00364   for (++BI; !isa<TerminatorInst>(BI); ++BI) {
00365     if (isa<CallInst>(BI) || isa<InvokeInst>(BI)) { 
00366       // If the call is readnone, ignore it, otherwise bail out.  We don't even
00367       // allow readonly here because we don't want something like:
00368       // A[1] = 2; strlen(A); A[2] = 2; -> memcpy(A, ...); strlen(A).
00369       if (AA.getModRefBehavior(CallSite::get(BI)) ==
00370             AliasAnalysis::DoesNotAccessMemory)
00371         continue;
00372       
00373       // TODO: If this is a memset, try to join it in.
00374       
00375       break;
00376     } else if (isa<VAArgInst>(BI) || isa<LoadInst>(BI))
00377       break;
00378 
00379     // If this is a non-store instruction it is fine, ignore it.
00380     StoreInst *NextStore = dyn_cast<StoreInst>(BI);
00381     if (NextStore == 0) continue;
00382     
00383     // If this is a store, see if we can merge it in.
00384     if (NextStore->isVolatile()) break;
00385     
00386     // Check to see if this stored value is of the same byte-splattable value.
00387     if (ByteVal != isBytewiseValue(NextStore->getOperand(0)))
00388       break;
00389 
00390     // Check to see if this store is to a constant offset from the start ptr.
00391     int64_t Offset;
00392     if (!IsPointerOffset(StartPtr, NextStore->getPointerOperand(), Offset, TD))
00393       break;
00394 
00395     Ranges.addStore(Offset, NextStore);
00396   }
00397 
00398   // If we have no ranges, then we just had a single store with nothing that
00399   // could be merged in.  This is a very common case of course.
00400   if (Ranges.empty())
00401     return false;
00402   
00403   // If we had at least one store that could be merged in, add the starting
00404   // store as well.  We try to avoid this unless there is at least something
00405   // interesting as a small compile-time optimization.
00406   Ranges.addStore(0, SI);
00407 
00408   
00409   Function *MemSetF = 0;
00410   
00411   // Now that we have full information about ranges, loop over the ranges and
00412   // emit memset's for anything big enough to be worthwhile.
00413   bool MadeChange = false;
00414   for (MemsetRanges::const_iterator I = Ranges.begin(), E = Ranges.end();
00415        I != E; ++I) {
00416     const MemsetRange &Range = *I;
00417 
00418     if (Range.TheStores.size() == 1) continue;
00419     
00420     // If it is profitable to lower this range to memset, do so now.
00421     if (!Range.isProfitableToUseMemset(TD))
00422       continue;
00423     
00424     // Otherwise, we do want to transform this!  Create a new memset.  We put
00425     // the memset right before the first instruction that isn't part of this
00426     // memset block.  This ensure that the memset is dominated by any addressing
00427     // instruction needed by the start of the block.
00428     BasicBlock::iterator InsertPt = BI;
00429   
00430     if (MemSetF == 0) {
00431       const Type *Tys[] = {Type::Int64Ty};
00432       MemSetF = Intrinsic::getDeclaration(SI->getParent()->getParent()
00433                                           ->getParent(), Intrinsic::memset,
00434                                           Tys, 1);
00435    }
00436     
00437     // Get the starting pointer of the block.
00438     StartPtr = Range.StartPtr;
00439   
00440     // Cast the start ptr to be i8* as memset requires.
00441     const Type *i8Ptr = PointerType::getUnqual(Type::Int8Ty);
00442     if (StartPtr->getType() != i8Ptr)
00443       StartPtr = new BitCastInst(StartPtr, i8Ptr, StartPtr->getNameStart(),
00444                                  InsertPt);
00445   
00446     Value *Ops[] = {
00447       StartPtr, ByteVal,   // Start, value
00448       ConstantInt::get(Type::Int64Ty, Range.End-Range.Start),  // size
00449       ConstantInt::get(Type::Int32Ty, Range.Alignment)   // align
00450     };
00451     Value *C = CallInst::Create(MemSetF, Ops, Ops+4, "", InsertPt);
00452     DEBUG(cerr << "Replace stores:\n";
00453           for (unsigned i = 0, e = Range.TheStores.size(); i != e; ++i)
00454             cerr << *Range.TheStores[i];
00455           cerr << "With: " << *C); C=C;
00456   
00457     // Don't invalidate the iterator
00458     BBI = BI;
00459   
00460     // Zap all the stores.
00461     for (SmallVector<StoreInst*, 16>::const_iterator SI = Range.TheStores.begin(),
00462          SE = Range.TheStores.end(); SI != SE; ++SI)
00463       (*SI)->eraseFromParent();
00464     ++NumMemSetInfer;
00465     MadeChange = true;
00466   }
00467   
00468   return MadeChange;
00469 }
00470 
00471 
00472 /// performCallSlotOptzn - takes a memcpy and a call that it depends on,
00473 /// and checks for the possibility of a call slot optimization by having
00474 /// the call write its result directly into the destination of the memcpy.
00475 bool MemCpyOpt::performCallSlotOptzn(MemCpyInst *cpy, CallInst *C) {
00476   // The general transformation to keep in mind is
00477   //
00478   //   call @func(..., src, ...)
00479   //   memcpy(dest, src, ...)
00480   //
00481   // ->
00482   //
00483   //   memcpy(dest, src, ...)
00484   //   call @func(..., dest, ...)
00485   //
00486   // Since moving the memcpy is technically awkward, we additionally check that
00487   // src only holds uninitialized values at the moment of the call, meaning that
00488   // the memcpy can be discarded rather than moved.
00489 
00490   // Deliberately get the source and destination with bitcasts stripped away,
00491   // because we'll need to do type comparisons based on the underlying type.
00492   Value* cpyDest = cpy->getDest();
00493   Value* cpySrc = cpy->getSource();
00494   CallSite CS = CallSite::get(C);
00495 
00496   // We need to be able to reason about the size of the memcpy, so we require
00497   // that it be a constant.
00498   ConstantInt* cpyLength = dyn_cast<ConstantInt>(cpy->getLength());
00499   if (!cpyLength)
00500     return false;
00501 
00502   // Require that src be an alloca.  This simplifies the reasoning considerably.
00503   AllocaInst* srcAlloca = dyn_cast<AllocaInst>(cpySrc);
00504   if (!srcAlloca)
00505     return false;
00506 
00507   // Check that all of src is copied to dest.
00508   TargetData& TD = getAnalysis<TargetData>();
00509 
00510   ConstantInt* srcArraySize = dyn_cast<ConstantInt>(srcAlloca->getArraySize());
00511   if (!srcArraySize)
00512     return false;
00513 
00514   uint64_t srcSize = TD.getTypePaddedSize(srcAlloca->getAllocatedType()) *
00515     srcArraySize->getZExtValue();
00516 
00517   if (cpyLength->getZExtValue() < srcSize)
00518     return false;
00519 
00520   // Check that accessing the first srcSize bytes of dest will not cause a
00521   // trap.  Otherwise the transform is invalid since it might cause a trap
00522   // to occur earlier than it otherwise would.
00523   if (AllocaInst* A = dyn_cast<AllocaInst>(cpyDest)) {
00524     // The destination is an alloca.  Check it is larger than srcSize.
00525     ConstantInt* destArraySize = dyn_cast<ConstantInt>(A->getArraySize());
00526     if (!destArraySize)
00527       return false;
00528 
00529     uint64_t destSize = TD.getTypePaddedSize(A->getAllocatedType()) *
00530       destArraySize->getZExtValue();
00531 
00532     if (destSize < srcSize)
00533       return false;
00534   } else if (Argument* A = dyn_cast<Argument>(cpyDest)) {
00535     // If the destination is an sret parameter then only accesses that are
00536     // outside of the returned struct type can trap.
00537     if (!A->hasStructRetAttr())
00538       return false;
00539 
00540     const Type* StructTy = cast<PointerType>(A->getType())->getElementType();
00541     uint64_t destSize = TD.getTypePaddedSize(StructTy);
00542 
00543     if (destSize < srcSize)
00544       return false;
00545   } else {
00546     return false;
00547   }
00548 
00549   // Check that src is not accessed except via the call and the memcpy.  This
00550   // guarantees that it holds only undefined values when passed in (so the final
00551   // memcpy can be dropped), that it is not read or written between the call and
00552   // the memcpy, and that writing beyond the end of it is undefined.
00553   SmallVector<User*, 8> srcUseList(srcAlloca->use_begin(),
00554                                    srcAlloca->use_end());
00555   while (!srcUseList.empty()) {
00556     User* UI = srcUseList.back();
00557     srcUseList.pop_back();
00558 
00559     if (isa<BitCastInst>(UI)) {
00560       for (User::use_iterator I = UI->use_begin(), E = UI->use_end();
00561            I != E; ++I)
00562         srcUseList.push_back(*I);
00563     } else if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(UI)) {
00564       if (G->hasAllZeroIndices())
00565         for (User::use_iterator I = UI->use_begin(), E = UI->use_end();
00566              I != E; ++I)
00567           srcUseList.push_back(*I);
00568       else
00569         return false;
00570     } else if (UI != C && UI != cpy) {
00571       return false;
00572     }
00573   }
00574 
00575   // Since we're changing the parameter to the callsite, we need to make sure
00576   // that what would be the new parameter dominates the callsite.
00577   DominatorTree& DT = getAnalysis<DominatorTree>();
00578   if (Instruction* cpyDestInst = dyn_cast<Instruction>(cpyDest))
00579     if (!DT.dominates(cpyDestInst, C))
00580       return false;
00581 
00582   // In addition to knowing that the call does not access src in some
00583   // unexpected manner, for example via a global, which we deduce from
00584   // the use analysis, we also need to know that it does not sneakily
00585   // access dest.  We rely on AA to figure this out for us.
00586   AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
00587   if (AA.getModRefInfo(C, cpy->getRawDest(), srcSize) !=
00588       AliasAnalysis::NoModRef)
00589     return false;
00590 
00591   // All the checks have passed, so do the transformation.
00592   bool changedArgument = false;
00593   for (unsigned i = 0; i < CS.arg_size(); ++i)
00594     if (CS.getArgument(i)->stripPointerCasts() == cpySrc) {
00595       if (cpySrc->getType() != cpyDest->getType())
00596         cpyDest = CastInst::CreatePointerCast(cpyDest, cpySrc->getType(),
00597                                               cpyDest->getName(), C);
00598       changedArgument = true;
00599       if (CS.getArgument(i)->getType() != cpyDest->getType())
00600         CS.setArgument(i, CastInst::CreatePointerCast(cpyDest, 
00601                        CS.getArgument(i)->getType(), cpyDest->getName(), C));
00602       else
00603         CS.setArgument(i, cpyDest);
00604     }
00605 
00606   if (!changedArgument)
00607     return false;
00608 
00609   // Drop any cached information about the call, because we may have changed
00610   // its dependence information by changing its parameter.
00611   MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
00612   MD.removeInstruction(C);
00613 
00614   // Remove the memcpy
00615   MD.removeInstruction(cpy);
00616   cpy->eraseFromParent();
00617   NumMemCpyInstr++;
00618 
00619   return true;
00620 }
00621 
00622 /// processMemCpy - perform simplication of memcpy's.  If we have memcpy A which
00623 /// copies X to Y, and memcpy B which copies Y to Z, then we can rewrite B to be
00624 /// a memcpy from X to Z (or potentially a memmove, depending on circumstances).
00625 ///  This allows later passes to remove the first memcpy altogether.
00626 bool MemCpyOpt::processMemCpy(MemCpyInst* M) {
00627   MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
00628 
00629   // The are two possible optimizations we can do for memcpy:
00630   //   a) memcpy-memcpy xform which exposes redundance for DSE
00631   //   b) call-memcpy xform for return slot optimization
00632   MemDepResult dep = MD.getDependency(M);
00633   if (!dep.isClobber())
00634     return false;
00635   if (!isa<MemCpyInst>(dep.getInst())) {
00636     if (CallInst* C = dyn_cast<CallInst>(dep.getInst()))
00637       return performCallSlotOptzn(M, C);
00638     return false;
00639   }
00640   
00641   MemCpyInst* MDep = cast<MemCpyInst>(dep.getInst());
00642   
00643   // We can only transforms memcpy's where the dest of one is the source of the
00644   // other
00645   if (M->getSource() != MDep->getDest())
00646     return false;
00647   
00648   // Second, the length of the memcpy's must be the same, or the preceeding one
00649   // must be larger than the following one.
00650   ConstantInt* C1 = dyn_cast<ConstantInt>(MDep->getLength());
00651   ConstantInt* C2 = dyn_cast<ConstantInt>(M->getLength());
00652   if (!C1 || !C2)
00653     return false;
00654   
00655   uint64_t DepSize = C1->getValue().getZExtValue();
00656   uint64_t CpySize = C2->getValue().getZExtValue();
00657   
00658   if (DepSize < CpySize)
00659     return false;
00660   
00661   // Finally, we have to make sure that the dest of the second does not
00662   // alias the source of the first
00663   AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
00664   if (AA.alias(M->getRawDest(), CpySize, MDep->getRawSource(), DepSize) !=
00665       AliasAnalysis::NoAlias)
00666     return false;
00667   else if (AA.alias(M->getRawDest(), CpySize, M->getRawSource(), CpySize) !=
00668            AliasAnalysis::NoAlias)
00669     return false;
00670   else if (AA.alias(MDep->getRawDest(), DepSize, MDep->getRawSource(), DepSize)
00671            != AliasAnalysis::NoAlias)
00672     return false;
00673   
00674   // If all checks passed, then we can transform these memcpy's
00675   const Type *Tys[1];
00676   Tys[0] = M->getLength()->getType();
00677   Function* MemCpyFun = Intrinsic::getDeclaration(
00678                                  M->getParent()->getParent()->getParent(),
00679                                  M->getIntrinsicID(), Tys, 1);
00680     
00681   std::vector<Value*> args;
00682   args.push_back(M->getRawDest());
00683   args.push_back(MDep->getRawSource());
00684   args.push_back(M->getLength());
00685   args.push_back(M->getAlignment());
00686   
00687   CallInst* C = CallInst::Create(MemCpyFun, args.begin(), args.end(), "", M);
00688   
00689   
00690   // If C and M don't interfere, then this is a valid transformation.  If they
00691   // did, this would mean that the two sources overlap, which would be bad.
00692   if (MD.getDependency(C) == dep) {
00693     MD.removeInstruction(M);
00694     M->eraseFromParent();
00695     NumMemCpyInstr++;
00696     return true;
00697   }
00698   
00699   // Otherwise, there was no point in doing this, so we remove the call we
00700   // inserted and act like nothing happened.
00701   MD.removeInstruction(C);
00702   C->eraseFromParent();
00703   return false;
00704 }
00705 
00706 // MemCpyOpt::runOnFunction - This is the main transformation entry point for a
00707 // function.
00708 //
00709 bool MemCpyOpt::runOnFunction(Function& F) {
00710   
00711   bool changed = false;
00712   bool shouldContinue = true;
00713   
00714   while (shouldContinue) {
00715     shouldContinue = iterateOnFunction(F);
00716     changed |= shouldContinue;
00717   }
00718   
00719   return changed;
00720 }
00721 
00722 
00723 // MemCpyOpt::iterateOnFunction - Executes one iteration of GVN
00724 bool MemCpyOpt::iterateOnFunction(Function &F) {
00725   bool changed_function = false;
00726 
00727   // Walk all instruction in the function
00728   for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) {
00729     for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
00730          BI != BE;) {
00731       // Avoid invalidating the iterator
00732       Instruction* I = BI++;
00733       
00734       if (StoreInst *SI = dyn_cast<StoreInst>(I))
00735         changed_function |= processStore(SI, BI);
00736       else if (MemCpyInst* M = dyn_cast<MemCpyInst>(I)) {
00737         changed_function |= processMemCpy(M);
00738       }
00739     }
00740   }
00741   
00742   return changed_function;
00743 }