Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 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 | #define DEBUG_TYPE "memcpyopt" |
| 16 | #include "llvm/Transforms/Scalar.h" |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 17 | #include "llvm/IntrinsicInst.h" |
| 18 | #include "llvm/Instructions.h" |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallVector.h" |
| 20 | #include "llvm/ADT/Statistic.h" |
| 21 | #include "llvm/Analysis/Dominators.h" |
| 22 | #include "llvm/Analysis/AliasAnalysis.h" |
| 23 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
| 28 | #include <list> |
| 29 | using namespace llvm; |
| 30 | |
| 31 | STATISTIC(NumMemCpyInstr, "Number of memcpy instructions deleted"); |
| 32 | STATISTIC(NumMemSetInfer, "Number of memsets inferred"); |
Duncan Sands | 05cd03b | 2009-09-03 13:37:16 +0000 | [diff] [blame] | 33 | STATISTIC(NumMoveToCpy, "Number of memmoves converted to memcpy"); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 34 | |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 35 | /// isBytewiseValue - If the specified value can be set by repeating the same |
| 36 | /// byte in memory, return the i8 value that it is represented with. This is |
| 37 | /// true for all i8 values obviously, but is also true for i32 0, i32 -1, |
| 38 | /// i16 0xF0F0, double 0.0 etc. If the value can't be handled with a repeated |
| 39 | /// byte store (e.g. i16 0x1234), return null. |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 40 | static Value *isBytewiseValue(Value *V) { |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 41 | // All byte-wide stores are splatable, even of arbitrary variables. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 42 | if (V->getType()->isIntegerTy(8)) return V; |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 43 | |
| 44 | // Constant float and double values can be handled as integer values if the |
| 45 | // corresponding integer value is "byteable". An important case is 0.0. |
| 46 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 47 | if (CFP->getType()->isFloatTy()) |
Chris Lattner | 7a0b4fd | 2010-11-29 23:35:33 +0000 | [diff] [blame] | 48 | V = ConstantExpr::getBitCast(CFP, Type::getInt32Ty(V->getContext())); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 49 | if (CFP->getType()->isDoubleTy()) |
Chris Lattner | 7a0b4fd | 2010-11-29 23:35:33 +0000 | [diff] [blame] | 50 | V = ConstantExpr::getBitCast(CFP, Type::getInt64Ty(V->getContext())); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 51 | // Don't handle long double formats, which have strange constraints. |
| 52 | } |
| 53 | |
| 54 | // We can handle constant integers that are power of two in size and a |
| 55 | // multiple of 8 bits. |
| 56 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 57 | unsigned Width = CI->getBitWidth(); |
| 58 | if (isPowerOf2_32(Width) && Width > 8) { |
| 59 | // We can handle this value if the recursive binary decomposition is the |
| 60 | // same at all levels. |
| 61 | APInt Val = CI->getValue(); |
| 62 | APInt Val2; |
| 63 | while (Val.getBitWidth() != 8) { |
| 64 | unsigned NextWidth = Val.getBitWidth()/2; |
| 65 | Val2 = Val.lshr(NextWidth); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 66 | Val2 = Val2.trunc(Val.getBitWidth()/2); |
| 67 | Val = Val.trunc(Val.getBitWidth()/2); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 68 | |
| 69 | // If the top/bottom halves aren't the same, reject it. |
| 70 | if (Val != Val2) |
| 71 | return 0; |
| 72 | } |
Chris Lattner | 7a0b4fd | 2010-11-29 23:35:33 +0000 | [diff] [blame] | 73 | return ConstantInt::get(V->getContext(), Val); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | // Conceptually, we could handle things like: |
| 78 | // %a = zext i8 %X to i16 |
| 79 | // %b = shl i16 %a, 8 |
| 80 | // %c = or i16 %a, %b |
| 81 | // but until there is an example that actually needs this, it doesn't seem |
| 82 | // worth worrying about. |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int64_t GetOffsetFromIndex(const GetElementPtrInst *GEP, unsigned Idx, |
| 87 | bool &VariableIdxFound, TargetData &TD) { |
| 88 | // Skip over the first indices. |
| 89 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 90 | for (unsigned i = 1; i != Idx; ++i, ++GTI) |
| 91 | /*skip along*/; |
| 92 | |
| 93 | // Compute the offset implied by the rest of the indices. |
| 94 | int64_t Offset = 0; |
| 95 | for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 96 | ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i)); |
| 97 | if (OpC == 0) |
| 98 | return VariableIdxFound = true; |
| 99 | if (OpC->isZero()) continue; // No offset. |
| 100 | |
| 101 | // Handle struct indices, which add their field offset to the pointer. |
| 102 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 103 | Offset += TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | // Otherwise, we have a sequential type like an array or vector. Multiply |
| 108 | // the index by the ElementSize. |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 109 | uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType()); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 110 | Offset += Size*OpC->getSExtValue(); |
| 111 | } |
| 112 | |
| 113 | return Offset; |
| 114 | } |
| 115 | |
| 116 | /// IsPointerOffset - Return true if Ptr1 is provably equal to Ptr2 plus a |
| 117 | /// constant offset, and return that constant offset. For example, Ptr1 might |
| 118 | /// be &A[42], and Ptr2 might be &A[40]. In this case offset would be -8. |
| 119 | static bool IsPointerOffset(Value *Ptr1, Value *Ptr2, int64_t &Offset, |
| 120 | TargetData &TD) { |
| 121 | // Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical |
| 122 | // base. After that base, they may have some number of common (and |
| 123 | // potentially variable) indices. After that they handle some constant |
| 124 | // offset, which determines their offset from each other. At this point, we |
| 125 | // handle no other case. |
| 126 | GetElementPtrInst *GEP1 = dyn_cast<GetElementPtrInst>(Ptr1); |
| 127 | GetElementPtrInst *GEP2 = dyn_cast<GetElementPtrInst>(Ptr2); |
| 128 | if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0)) |
| 129 | return false; |
| 130 | |
| 131 | // Skip any common indices and track the GEP types. |
| 132 | unsigned Idx = 1; |
| 133 | for (; Idx != GEP1->getNumOperands() && Idx != GEP2->getNumOperands(); ++Idx) |
| 134 | if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx)) |
| 135 | break; |
| 136 | |
| 137 | bool VariableIdxFound = false; |
| 138 | int64_t Offset1 = GetOffsetFromIndex(GEP1, Idx, VariableIdxFound, TD); |
| 139 | int64_t Offset2 = GetOffsetFromIndex(GEP2, Idx, VariableIdxFound, TD); |
| 140 | if (VariableIdxFound) return false; |
| 141 | |
| 142 | Offset = Offset2-Offset1; |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /// MemsetRange - Represents a range of memset'd bytes with the ByteVal value. |
| 148 | /// This allows us to analyze stores like: |
| 149 | /// store 0 -> P+1 |
| 150 | /// store 0 -> P+0 |
| 151 | /// store 0 -> P+3 |
| 152 | /// store 0 -> P+2 |
| 153 | /// which sometimes happens with stores to arrays of structs etc. When we see |
| 154 | /// the first store, we make a range [1, 2). The second store extends the range |
| 155 | /// to [0, 2). The third makes a new range [2, 3). The fourth store joins the |
| 156 | /// two ranges into [0, 3) which is memset'able. |
| 157 | namespace { |
| 158 | struct MemsetRange { |
| 159 | // Start/End - A semi range that describes the span that this range covers. |
| 160 | // The range is closed at the start and open at the end: [Start, End). |
| 161 | int64_t Start, End; |
| 162 | |
| 163 | /// StartPtr - The getelementptr instruction that points to the start of the |
| 164 | /// range. |
| 165 | Value *StartPtr; |
| 166 | |
| 167 | /// Alignment - The known alignment of the first store. |
| 168 | unsigned Alignment; |
| 169 | |
| 170 | /// TheStores - The actual stores that make up this range. |
| 171 | SmallVector<StoreInst*, 16> TheStores; |
| 172 | |
| 173 | bool isProfitableToUseMemset(const TargetData &TD) const; |
| 174 | |
| 175 | }; |
| 176 | } // end anon namespace |
| 177 | |
| 178 | bool MemsetRange::isProfitableToUseMemset(const TargetData &TD) const { |
| 179 | // If we found more than 8 stores to merge or 64 bytes, use memset. |
| 180 | if (TheStores.size() >= 8 || End-Start >= 64) return true; |
| 181 | |
| 182 | // Assume that the code generator is capable of merging pairs of stores |
| 183 | // together if it wants to. |
| 184 | if (TheStores.size() <= 2) return false; |
| 185 | |
| 186 | // If we have fewer than 8 stores, it can still be worthwhile to do this. |
| 187 | // For example, merging 4 i8 stores into an i32 store is useful almost always. |
| 188 | // However, merging 2 32-bit stores isn't useful on a 32-bit architecture (the |
| 189 | // memset will be split into 2 32-bit stores anyway) and doing so can |
| 190 | // pessimize the llvm optimizer. |
| 191 | // |
| 192 | // Since we don't have perfect knowledge here, make some assumptions: assume |
| 193 | // the maximum GPR width is the same size as the pointer size and assume that |
| 194 | // this width can be stored. If so, check to see whether we will end up |
| 195 | // actually reducing the number of stores used. |
| 196 | unsigned Bytes = unsigned(End-Start); |
| 197 | unsigned NumPointerStores = Bytes/TD.getPointerSize(); |
| 198 | |
| 199 | // Assume the remaining bytes if any are done a byte at a time. |
| 200 | unsigned NumByteStores = Bytes - NumPointerStores*TD.getPointerSize(); |
| 201 | |
| 202 | // If we will reduce the # stores (according to this heuristic), do the |
| 203 | // transformation. This encourages merging 4 x i8 -> i32 and 2 x i16 -> i32 |
| 204 | // etc. |
| 205 | return TheStores.size() > NumPointerStores+NumByteStores; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | namespace { |
| 210 | class MemsetRanges { |
| 211 | /// Ranges - A sorted list of the memset ranges. We use std::list here |
| 212 | /// because each element is relatively large and expensive to copy. |
| 213 | std::list<MemsetRange> Ranges; |
| 214 | typedef std::list<MemsetRange>::iterator range_iterator; |
| 215 | TargetData &TD; |
| 216 | public: |
| 217 | MemsetRanges(TargetData &td) : TD(td) {} |
| 218 | |
| 219 | typedef std::list<MemsetRange>::const_iterator const_iterator; |
| 220 | const_iterator begin() const { return Ranges.begin(); } |
| 221 | const_iterator end() const { return Ranges.end(); } |
| 222 | bool empty() const { return Ranges.empty(); } |
| 223 | |
| 224 | void addStore(int64_t OffsetFromFirst, StoreInst *SI); |
| 225 | }; |
| 226 | |
| 227 | } // end anon namespace |
| 228 | |
| 229 | |
| 230 | /// addStore - Add a new store to the MemsetRanges data structure. This adds a |
| 231 | /// new range for the specified store at the specified offset, merging into |
| 232 | /// existing ranges as appropriate. |
| 233 | void MemsetRanges::addStore(int64_t Start, StoreInst *SI) { |
| 234 | int64_t End = Start+TD.getTypeStoreSize(SI->getOperand(0)->getType()); |
| 235 | |
| 236 | // Do a linear search of the ranges to see if this can be joined and/or to |
| 237 | // find the insertion point in the list. We keep the ranges sorted for |
| 238 | // simplicity here. This is a linear search of a linked list, which is ugly, |
| 239 | // however the number of ranges is limited, so this won't get crazy slow. |
| 240 | range_iterator I = Ranges.begin(), E = Ranges.end(); |
| 241 | |
| 242 | while (I != E && Start > I->End) |
| 243 | ++I; |
| 244 | |
| 245 | // We now know that I == E, in which case we didn't find anything to merge |
| 246 | // with, or that Start <= I->End. If End < I->Start or I == E, then we need |
| 247 | // to insert a new range. Handle this now. |
| 248 | if (I == E || End < I->Start) { |
| 249 | MemsetRange &R = *Ranges.insert(I, MemsetRange()); |
| 250 | R.Start = Start; |
| 251 | R.End = End; |
| 252 | R.StartPtr = SI->getPointerOperand(); |
| 253 | R.Alignment = SI->getAlignment(); |
| 254 | R.TheStores.push_back(SI); |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | // This store overlaps with I, add it. |
| 259 | I->TheStores.push_back(SI); |
| 260 | |
| 261 | // At this point, we may have an interval that completely contains our store. |
| 262 | // If so, just add it to the interval and return. |
| 263 | if (I->Start <= Start && I->End >= End) |
| 264 | return; |
| 265 | |
| 266 | // Now we know that Start <= I->End and End >= I->Start so the range overlaps |
| 267 | // but is not entirely contained within the range. |
| 268 | |
| 269 | // See if the range extends the start of the range. In this case, it couldn't |
| 270 | // possibly cause it to join the prior range, because otherwise we would have |
| 271 | // stopped on *it*. |
| 272 | if (Start < I->Start) { |
| 273 | I->Start = Start; |
| 274 | I->StartPtr = SI->getPointerOperand(); |
Dan Gohman | 264d245 | 2009-09-14 23:39:10 +0000 | [diff] [blame] | 275 | I->Alignment = SI->getAlignment(); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 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 Lewycky | 9c0f146 | 2009-03-19 05:51:39 +0000 | [diff] [blame] | 283 | range_iterator NextI = I; |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 284 | 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 | |
| 299 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 300 | class MemCpyOpt : public FunctionPass { |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 301 | MemoryDependenceAnalysis *MD; |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 302 | bool runOnFunction(Function &F); |
| 303 | public: |
| 304 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 305 | MemCpyOpt() : FunctionPass(ID) { |
| 306 | initializeMemCpyOptPass(*PassRegistry::getPassRegistry()); |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 307 | MD = 0; |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 308 | } |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 309 | |
| 310 | private: |
| 311 | // This transformation requires dominator postdominator info |
| 312 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 313 | AU.setPreservesCFG(); |
| 314 | AU.addRequired<DominatorTree>(); |
| 315 | AU.addRequired<MemoryDependenceAnalysis>(); |
| 316 | AU.addRequired<AliasAnalysis>(); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 317 | AU.addPreserved<AliasAnalysis>(); |
| 318 | AU.addPreserved<MemoryDependenceAnalysis>(); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | // Helper fuctions |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 322 | bool processStore(StoreInst *SI, BasicBlock::iterator &BBI); |
| 323 | bool processMemCpy(MemCpyInst *M); |
Chris Lattner | f41eaac | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 324 | bool processMemMove(MemMoveInst *M); |
Owen Anderson | 6549121 | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 325 | bool performCallSlotOptzn(Instruction *cpy, Value *cpyDst, Value *cpySrc, |
| 326 | uint64_t cpyLen, CallInst *C); |
Chris Lattner | 43f8e43 | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 327 | bool processMemCpyMemCpyDependence(MemCpyInst *M, MemCpyInst *MDep, |
| 328 | uint64_t MSize); |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 329 | bool processByValArgument(CallSite CS, unsigned ArgNo); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 330 | bool iterateOnFunction(Function &F); |
| 331 | }; |
| 332 | |
| 333 | char MemCpyOpt::ID = 0; |
| 334 | } |
| 335 | |
| 336 | // createMemCpyOptPass - The public interface to this file... |
| 337 | FunctionPass *llvm::createMemCpyOptPass() { return new MemCpyOpt(); } |
| 338 | |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 339 | INITIALIZE_PASS_BEGIN(MemCpyOpt, "memcpyopt", "MemCpy Optimization", |
| 340 | false, false) |
| 341 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 342 | INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis) |
| 343 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 344 | INITIALIZE_PASS_END(MemCpyOpt, "memcpyopt", "MemCpy Optimization", |
| 345 | false, false) |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 346 | |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 347 | /// processStore - When GVN is scanning forward over instructions, we look for |
| 348 | /// some other patterns to fold away. In particular, this looks for stores to |
| 349 | /// neighboring locations of memory. If it sees enough consequtive ones |
| 350 | /// (currently 4) it attempts to merge them together into a memcpy/memset. |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 351 | bool MemCpyOpt::processStore(StoreInst *SI, BasicBlock::iterator &BBI) { |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 352 | if (SI->isVolatile()) return false; |
| 353 | |
Owen Anderson | 6549121 | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 354 | TargetData *TD = getAnalysisIfAvailable<TargetData>(); |
| 355 | if (!TD) return false; |
| 356 | |
| 357 | // Detect cases where we're performing call slot forwarding, but |
| 358 | // happen to be using a load-store pair to implement it, rather than |
| 359 | // a memcpy. |
| 360 | if (LoadInst *LI = dyn_cast<LoadInst>(SI->getOperand(0))) { |
| 361 | if (!LI->isVolatile() && LI->hasOneUse()) { |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 362 | MemDepResult dep = MD->getDependency(LI); |
Owen Anderson | 6549121 | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 363 | CallInst *C = 0; |
| 364 | if (dep.isClobber() && !isa<MemCpyInst>(dep.getInst())) |
| 365 | C = dyn_cast<CallInst>(dep.getInst()); |
| 366 | |
| 367 | if (C) { |
| 368 | bool changed = performCallSlotOptzn(LI, |
| 369 | SI->getPointerOperand()->stripPointerCasts(), |
| 370 | LI->getPointerOperand()->stripPointerCasts(), |
| 371 | TD->getTypeStoreSize(SI->getOperand(0)->getType()), C); |
| 372 | if (changed) { |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 373 | MD->removeInstruction(SI); |
Owen Anderson | 6549121 | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 374 | SI->eraseFromParent(); |
| 375 | LI->eraseFromParent(); |
| 376 | ++NumMemCpyInstr; |
| 377 | return true; |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
Chris Lattner | ff1e98c | 2009-09-08 00:27:14 +0000 | [diff] [blame] | 383 | LLVMContext &Context = SI->getContext(); |
| 384 | |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 385 | // There are two cases that are interesting for this code to handle: memcpy |
| 386 | // and memset. Right now we only handle memset. |
| 387 | |
| 388 | // Ensure that the value being stored is something that can be memset'able a |
| 389 | // byte at a time like "0" or "-1" or any width, as well as things like |
| 390 | // 0xA0A0A0A0 and 0.0. |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 391 | Value *ByteVal = isBytewiseValue(SI->getOperand(0)); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 392 | if (!ByteVal) |
| 393 | return false; |
| 394 | |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 395 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
Dan Gohman | a195b7f | 2009-07-28 00:37:06 +0000 | [diff] [blame] | 396 | Module *M = SI->getParent()->getParent()->getParent(); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 397 | |
| 398 | // Okay, so we now have a single store that can be splatable. Scan to find |
| 399 | // all subsequent stores of the same value to offset from the same pointer. |
| 400 | // Join these together into ranges, so we can decide whether contiguous blocks |
| 401 | // are stored. |
Dan Gohman | 8942f9bb | 2009-08-18 01:17:52 +0000 | [diff] [blame] | 402 | MemsetRanges Ranges(*TD); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 403 | |
| 404 | Value *StartPtr = SI->getPointerOperand(); |
| 405 | |
| 406 | BasicBlock::iterator BI = SI; |
| 407 | for (++BI; !isa<TerminatorInst>(BI); ++BI) { |
| 408 | if (isa<CallInst>(BI) || isa<InvokeInst>(BI)) { |
| 409 | // If the call is readnone, ignore it, otherwise bail out. We don't even |
| 410 | // allow readonly here because we don't want something like: |
| 411 | // A[1] = 2; strlen(A); A[2] = 2; -> memcpy(A, ...); strlen(A). |
Gabor Greif | a292b2f | 2010-07-27 16:44:23 +0000 | [diff] [blame] | 412 | if (AA.getModRefBehavior(CallSite(BI)) == |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 413 | AliasAnalysis::DoesNotAccessMemory) |
| 414 | continue; |
| 415 | |
| 416 | // TODO: If this is a memset, try to join it in. |
| 417 | |
| 418 | break; |
| 419 | } else if (isa<VAArgInst>(BI) || isa<LoadInst>(BI)) |
| 420 | break; |
| 421 | |
| 422 | // If this is a non-store instruction it is fine, ignore it. |
| 423 | StoreInst *NextStore = dyn_cast<StoreInst>(BI); |
| 424 | if (NextStore == 0) continue; |
| 425 | |
| 426 | // If this is a store, see if we can merge it in. |
| 427 | if (NextStore->isVolatile()) break; |
| 428 | |
| 429 | // Check to see if this stored value is of the same byte-splattable value. |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 430 | if (ByteVal != isBytewiseValue(NextStore->getOperand(0))) |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 431 | break; |
| 432 | |
| 433 | // Check to see if this store is to a constant offset from the start ptr. |
| 434 | int64_t Offset; |
Dan Gohman | 8942f9bb | 2009-08-18 01:17:52 +0000 | [diff] [blame] | 435 | if (!IsPointerOffset(StartPtr, NextStore->getPointerOperand(), Offset, *TD)) |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 436 | break; |
| 437 | |
| 438 | Ranges.addStore(Offset, NextStore); |
| 439 | } |
| 440 | |
| 441 | // If we have no ranges, then we just had a single store with nothing that |
| 442 | // could be merged in. This is a very common case of course. |
| 443 | if (Ranges.empty()) |
| 444 | return false; |
| 445 | |
| 446 | // If we had at least one store that could be merged in, add the starting |
| 447 | // store as well. We try to avoid this unless there is at least something |
| 448 | // interesting as a small compile-time optimization. |
| 449 | Ranges.addStore(0, SI); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 450 | |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 451 | |
| 452 | // Now that we have full information about ranges, loop over the ranges and |
| 453 | // emit memset's for anything big enough to be worthwhile. |
| 454 | bool MadeChange = false; |
| 455 | for (MemsetRanges::const_iterator I = Ranges.begin(), E = Ranges.end(); |
| 456 | I != E; ++I) { |
| 457 | const MemsetRange &Range = *I; |
| 458 | |
| 459 | if (Range.TheStores.size() == 1) continue; |
| 460 | |
| 461 | // If it is profitable to lower this range to memset, do so now. |
Dan Gohman | 8942f9bb | 2009-08-18 01:17:52 +0000 | [diff] [blame] | 462 | if (!Range.isProfitableToUseMemset(*TD)) |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 463 | continue; |
| 464 | |
| 465 | // Otherwise, we do want to transform this! Create a new memset. We put |
| 466 | // the memset right before the first instruction that isn't part of this |
| 467 | // memset block. This ensure that the memset is dominated by any addressing |
| 468 | // instruction needed by the start of the block. |
| 469 | BasicBlock::iterator InsertPt = BI; |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 470 | |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 471 | // Get the starting pointer of the block. |
| 472 | StartPtr = Range.StartPtr; |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 473 | |
| 474 | // Determine alignment |
| 475 | unsigned Alignment = Range.Alignment; |
| 476 | if (Alignment == 0) { |
| 477 | const Type *EltType = |
| 478 | cast<PointerType>(StartPtr->getType())->getElementType(); |
| 479 | Alignment = TD->getABITypeAlignment(EltType); |
| 480 | } |
| 481 | |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 482 | // Cast the start ptr to be i8* as memset requires. |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 483 | const PointerType* StartPTy = cast<PointerType>(StartPtr->getType()); |
| 484 | const PointerType *i8Ptr = Type::getInt8PtrTy(Context, |
| 485 | StartPTy->getAddressSpace()); |
| 486 | if (StartPTy!= i8Ptr) |
Daniel Dunbar | 460f656 | 2009-07-26 09:48:23 +0000 | [diff] [blame] | 487 | StartPtr = new BitCastInst(StartPtr, i8Ptr, StartPtr->getName(), |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 488 | InsertPt); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 489 | |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 490 | Value *Ops[] = { |
| 491 | StartPtr, ByteVal, // Start, value |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 492 | // size |
Chris Lattner | ff1e98c | 2009-09-08 00:27:14 +0000 | [diff] [blame] | 493 | ConstantInt::get(Type::getInt64Ty(Context), Range.End-Range.Start), |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 494 | // align |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 495 | ConstantInt::get(Type::getInt32Ty(Context), Alignment), |
| 496 | // volatile |
Benjamin Kramer | f601d6d | 2010-11-20 18:43:35 +0000 | [diff] [blame] | 497 | ConstantInt::getFalse(Context), |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 498 | }; |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 499 | const Type *Tys[] = { Ops[0]->getType(), Ops[2]->getType() }; |
| 500 | |
| 501 | Function *MemSetF = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys, 2); |
| 502 | |
| 503 | Value *C = CallInst::Create(MemSetF, Ops, Ops+5, "", InsertPt); |
David Greene | cb33fd1 | 2010-01-05 01:27:47 +0000 | [diff] [blame] | 504 | DEBUG(dbgs() << "Replace stores:\n"; |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 505 | for (unsigned i = 0, e = Range.TheStores.size(); i != e; ++i) |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 506 | dbgs() << *Range.TheStores[i] << '\n'; |
| 507 | dbgs() << "With: " << *C << '\n'); C=C; |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 508 | |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 509 | // Don't invalidate the iterator |
| 510 | BBI = BI; |
| 511 | |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 512 | // Zap all the stores. |
Chris Lattner | ff1e98c | 2009-09-08 00:27:14 +0000 | [diff] [blame] | 513 | for (SmallVector<StoreInst*, 16>::const_iterator |
| 514 | SI = Range.TheStores.begin(), |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 515 | SE = Range.TheStores.end(); SI != SE; ++SI) |
| 516 | (*SI)->eraseFromParent(); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 517 | ++NumMemSetInfer; |
| 518 | MadeChange = true; |
| 519 | } |
| 520 | |
| 521 | return MadeChange; |
| 522 | } |
| 523 | |
| 524 | |
| 525 | /// performCallSlotOptzn - takes a memcpy and a call that it depends on, |
| 526 | /// and checks for the possibility of a call slot optimization by having |
| 527 | /// the call write its result directly into the destination of the memcpy. |
Owen Anderson | 6549121 | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 528 | bool MemCpyOpt::performCallSlotOptzn(Instruction *cpy, |
| 529 | Value *cpyDest, Value *cpySrc, |
| 530 | uint64_t cpyLen, CallInst *C) { |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 531 | // The general transformation to keep in mind is |
| 532 | // |
| 533 | // call @func(..., src, ...) |
| 534 | // memcpy(dest, src, ...) |
| 535 | // |
| 536 | // -> |
| 537 | // |
| 538 | // memcpy(dest, src, ...) |
| 539 | // call @func(..., dest, ...) |
| 540 | // |
| 541 | // Since moving the memcpy is technically awkward, we additionally check that |
| 542 | // src only holds uninitialized values at the moment of the call, meaning that |
| 543 | // the memcpy can be discarded rather than moved. |
| 544 | |
| 545 | // Deliberately get the source and destination with bitcasts stripped away, |
| 546 | // because we'll need to do type comparisons based on the underlying type. |
Gabor Greif | 7d3056b | 2010-07-28 22:50:26 +0000 | [diff] [blame] | 547 | CallSite CS(C); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 548 | |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 549 | // Require that src be an alloca. This simplifies the reasoning considerably. |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 550 | AllocaInst *srcAlloca = dyn_cast<AllocaInst>(cpySrc); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 551 | if (!srcAlloca) |
| 552 | return false; |
| 553 | |
| 554 | // Check that all of src is copied to dest. |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 555 | TargetData *TD = getAnalysisIfAvailable<TargetData>(); |
Dan Gohman | 8942f9bb | 2009-08-18 01:17:52 +0000 | [diff] [blame] | 556 | if (!TD) return false; |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 557 | |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 558 | ConstantInt *srcArraySize = dyn_cast<ConstantInt>(srcAlloca->getArraySize()); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 559 | if (!srcArraySize) |
| 560 | return false; |
| 561 | |
Dan Gohman | 8942f9bb | 2009-08-18 01:17:52 +0000 | [diff] [blame] | 562 | uint64_t srcSize = TD->getTypeAllocSize(srcAlloca->getAllocatedType()) * |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 563 | srcArraySize->getZExtValue(); |
| 564 | |
Owen Anderson | 6549121 | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 565 | if (cpyLen < srcSize) |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 566 | return false; |
| 567 | |
| 568 | // Check that accessing the first srcSize bytes of dest will not cause a |
| 569 | // trap. Otherwise the transform is invalid since it might cause a trap |
| 570 | // to occur earlier than it otherwise would. |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 571 | if (AllocaInst *A = dyn_cast<AllocaInst>(cpyDest)) { |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 572 | // The destination is an alloca. Check it is larger than srcSize. |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 573 | ConstantInt *destArraySize = dyn_cast<ConstantInt>(A->getArraySize()); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 574 | if (!destArraySize) |
| 575 | return false; |
| 576 | |
Dan Gohman | 8942f9bb | 2009-08-18 01:17:52 +0000 | [diff] [blame] | 577 | uint64_t destSize = TD->getTypeAllocSize(A->getAllocatedType()) * |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 578 | destArraySize->getZExtValue(); |
| 579 | |
| 580 | if (destSize < srcSize) |
| 581 | return false; |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 582 | } else if (Argument *A = dyn_cast<Argument>(cpyDest)) { |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 583 | // If the destination is an sret parameter then only accesses that are |
| 584 | // outside of the returned struct type can trap. |
| 585 | if (!A->hasStructRetAttr()) |
| 586 | return false; |
| 587 | |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 588 | const Type *StructTy = cast<PointerType>(A->getType())->getElementType(); |
Dan Gohman | 8942f9bb | 2009-08-18 01:17:52 +0000 | [diff] [blame] | 589 | uint64_t destSize = TD->getTypeAllocSize(StructTy); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 590 | |
| 591 | if (destSize < srcSize) |
| 592 | return false; |
| 593 | } else { |
| 594 | return false; |
| 595 | } |
| 596 | |
| 597 | // Check that src is not accessed except via the call and the memcpy. This |
| 598 | // guarantees that it holds only undefined values when passed in (so the final |
| 599 | // memcpy can be dropped), that it is not read or written between the call and |
| 600 | // the memcpy, and that writing beyond the end of it is undefined. |
| 601 | SmallVector<User*, 8> srcUseList(srcAlloca->use_begin(), |
| 602 | srcAlloca->use_end()); |
| 603 | while (!srcUseList.empty()) { |
Dan Gohman | 321a813 | 2010-01-05 16:27:25 +0000 | [diff] [blame] | 604 | User *UI = srcUseList.pop_back_val(); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 605 | |
Owen Anderson | 009e4f7 | 2008-06-01 22:26:26 +0000 | [diff] [blame] | 606 | if (isa<BitCastInst>(UI)) { |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 607 | for (User::use_iterator I = UI->use_begin(), E = UI->use_end(); |
| 608 | I != E; ++I) |
| 609 | srcUseList.push_back(*I); |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 610 | } else if (GetElementPtrInst *G = dyn_cast<GetElementPtrInst>(UI)) { |
Owen Anderson | 009e4f7 | 2008-06-01 22:26:26 +0000 | [diff] [blame] | 611 | if (G->hasAllZeroIndices()) |
| 612 | for (User::use_iterator I = UI->use_begin(), E = UI->use_end(); |
| 613 | I != E; ++I) |
| 614 | srcUseList.push_back(*I); |
| 615 | else |
| 616 | return false; |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 617 | } else if (UI != C && UI != cpy) { |
| 618 | return false; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | // Since we're changing the parameter to the callsite, we need to make sure |
| 623 | // that what would be the new parameter dominates the callsite. |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 624 | DominatorTree &DT = getAnalysis<DominatorTree>(); |
| 625 | if (Instruction *cpyDestInst = dyn_cast<Instruction>(cpyDest)) |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 626 | if (!DT.dominates(cpyDestInst, C)) |
| 627 | return false; |
| 628 | |
| 629 | // In addition to knowing that the call does not access src in some |
| 630 | // unexpected manner, for example via a global, which we deduce from |
| 631 | // the use analysis, we also need to know that it does not sneakily |
| 632 | // access dest. We rely on AA to figure this out for us. |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 633 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
Owen Anderson | 6549121 | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 634 | if (AA.getModRefInfo(C, cpyDest, srcSize) != |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 635 | AliasAnalysis::NoModRef) |
| 636 | return false; |
| 637 | |
| 638 | // All the checks have passed, so do the transformation. |
Owen Anderson | 12cb36c | 2008-06-01 21:52:16 +0000 | [diff] [blame] | 639 | bool changedArgument = false; |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 640 | for (unsigned i = 0; i < CS.arg_size(); ++i) |
Owen Anderson | 009e4f7 | 2008-06-01 22:26:26 +0000 | [diff] [blame] | 641 | if (CS.getArgument(i)->stripPointerCasts() == cpySrc) { |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 642 | if (cpySrc->getType() != cpyDest->getType()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 643 | cpyDest = CastInst::CreatePointerCast(cpyDest, cpySrc->getType(), |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 644 | cpyDest->getName(), C); |
Owen Anderson | 12cb36c | 2008-06-01 21:52:16 +0000 | [diff] [blame] | 645 | changedArgument = true; |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 646 | if (CS.getArgument(i)->getType() == cpyDest->getType()) |
Owen Anderson | 009e4f7 | 2008-06-01 22:26:26 +0000 | [diff] [blame] | 647 | CS.setArgument(i, cpyDest); |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 648 | else |
| 649 | CS.setArgument(i, CastInst::CreatePointerCast(cpyDest, |
| 650 | CS.getArgument(i)->getType(), cpyDest->getName(), C)); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Owen Anderson | 12cb36c | 2008-06-01 21:52:16 +0000 | [diff] [blame] | 653 | if (!changedArgument) |
| 654 | return false; |
| 655 | |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 656 | // Drop any cached information about the call, because we may have changed |
| 657 | // its dependence information by changing its parameter. |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 658 | MD->removeInstruction(C); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 659 | |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 660 | // Remove the memcpy. |
| 661 | MD->removeInstruction(cpy); |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 662 | ++NumMemCpyInstr; |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 663 | |
| 664 | return true; |
| 665 | } |
| 666 | |
Chris Lattner | 43f8e43 | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 667 | /// processMemCpyMemCpyDependence - We've found that the (upward scanning) |
| 668 | /// memory dependence of memcpy 'M' is the memcpy 'MDep'. Try to simplify M to |
| 669 | /// copy from MDep's input if we can. MSize is the size of M's copy. |
| 670 | /// |
| 671 | bool MemCpyOpt::processMemCpyMemCpyDependence(MemCpyInst *M, MemCpyInst *MDep, |
| 672 | uint64_t MSize) { |
| 673 | // We can only transforms memcpy's where the dest of one is the source of the |
| 674 | // other. |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 675 | if (M->getSource() != MDep->getDest() || MDep->isVolatile()) |
Chris Lattner | 43f8e43 | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 676 | return false; |
| 677 | |
Chris Lattner | f7f3546 | 2010-12-09 07:39:50 +0000 | [diff] [blame] | 678 | // If dep instruction is reading from our current input, then it is a noop |
| 679 | // transfer and substituting the input won't change this instruction. Just |
| 680 | // ignore the input and let someone else zap MDep. This handles cases like: |
| 681 | // memcpy(a <- a) |
| 682 | // memcpy(b <- a) |
| 683 | if (M->getSource() == MDep->getSource()) |
| 684 | return false; |
| 685 | |
Chris Lattner | 43f8e43 | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 686 | // Second, the length of the memcpy's must be the same, or the preceeding one |
| 687 | // must be larger than the following one. |
| 688 | ConstantInt *C1 = dyn_cast<ConstantInt>(MDep->getLength()); |
| 689 | if (!C1) return false; |
| 690 | |
| 691 | uint64_t DepSize = C1->getValue().getZExtValue(); |
Chris Lattner | 43f8e43 | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 692 | if (DepSize < MSize) |
| 693 | return false; |
| 694 | |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 695 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
Chris Lattner | 604f6fe | 2010-11-21 08:06:10 +0000 | [diff] [blame] | 696 | |
| 697 | // Verify that the copied-from memory doesn't change in between the two |
| 698 | // transfers. For example, in: |
| 699 | // memcpy(a <- b) |
| 700 | // *b = 42; |
| 701 | // memcpy(c <- a) |
| 702 | // It would be invalid to transform the second memcpy into memcpy(c <- b). |
| 703 | // |
| 704 | // TODO: If the code between M and MDep is transparent to the destination "c", |
| 705 | // then we could still perform the xform by moving M up to the first memcpy. |
| 706 | // |
| 707 | // NOTE: This is conservative, it will stop on any read from the source loc, |
| 708 | // not just the defining memcpy. |
| 709 | MemDepResult SourceDep = |
| 710 | MD->getPointerDependencyFrom(AA.getLocationForSource(MDep), |
| 711 | false, M, M->getParent()); |
| 712 | if (!SourceDep.isClobber() || SourceDep.getInst() != MDep) |
| 713 | return false; |
Chris Lattner | 5a7aeaa | 2010-11-18 08:00:57 +0000 | [diff] [blame] | 714 | |
| 715 | // If the dest of the second might alias the source of the first, then the |
| 716 | // source and dest might overlap. We still want to eliminate the intermediate |
| 717 | // value, but we have to generate a memmove instead of memcpy. |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 718 | Intrinsic::ID ResultFn = Intrinsic::memcpy; |
Chris Lattner | 12f7085 | 2010-11-18 07:39:57 +0000 | [diff] [blame] | 719 | if (!AA.isNoAlias(M->getRawDest(), MSize, MDep->getRawSource(), DepSize)) |
Chris Lattner | 5a7aeaa | 2010-11-18 08:00:57 +0000 | [diff] [blame] | 720 | ResultFn = Intrinsic::memmove; |
Chris Lattner | 43f8e43 | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 721 | |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 722 | // If all checks passed, then we can transform M. |
Chris Lattner | 245b7f6 | 2010-11-18 07:38:43 +0000 | [diff] [blame] | 723 | const Type *ArgTys[3] = { |
| 724 | M->getRawDest()->getType(), |
Chris Lattner | 43f8e43 | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 725 | MDep->getRawSource()->getType(), |
Chris Lattner | 245b7f6 | 2010-11-18 07:38:43 +0000 | [diff] [blame] | 726 | M->getLength()->getType() |
| 727 | }; |
Chris Lattner | 43f8e43 | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 728 | Function *MemCpyFun = |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 729 | Intrinsic::getDeclaration(MDep->getParent()->getParent()->getParent(), |
Chris Lattner | 5a7aeaa | 2010-11-18 08:00:57 +0000 | [diff] [blame] | 730 | ResultFn, ArgTys, 3); |
Chris Lattner | 43f8e43 | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 731 | |
| 732 | // Make sure to use the lesser of the alignment of the source and the dest |
| 733 | // since we're changing where we're reading from, but don't want to increase |
| 734 | // the alignment past what can be read from or written to. |
| 735 | // TODO: Is this worth it if we're creating a less aligned memcpy? For |
| 736 | // example we could be moving from movaps -> movq on x86. |
Chris Lattner | d528be6 | 2010-11-18 08:07:09 +0000 | [diff] [blame] | 737 | unsigned Align = std::min(MDep->getAlignment(), M->getAlignment()); |
Chris Lattner | 43f8e43 | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 738 | Value *Args[5] = { |
Chris Lattner | d528be6 | 2010-11-18 08:07:09 +0000 | [diff] [blame] | 739 | M->getRawDest(), |
| 740 | MDep->getRawSource(), |
| 741 | M->getLength(), |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 742 | ConstantInt::get(Type::getInt32Ty(MemCpyFun->getContext()), Align), |
Chris Lattner | d528be6 | 2010-11-18 08:07:09 +0000 | [diff] [blame] | 743 | M->getVolatileCst() |
Chris Lattner | 43f8e43 | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 744 | }; |
Chris Lattner | 604f6fe | 2010-11-21 08:06:10 +0000 | [diff] [blame] | 745 | CallInst::Create(MemCpyFun, Args, Args+5, "", M); |
Chris Lattner | d528be6 | 2010-11-18 08:07:09 +0000 | [diff] [blame] | 746 | |
Chris Lattner | 604f6fe | 2010-11-21 08:06:10 +0000 | [diff] [blame] | 747 | // Remove the instruction we're replacing. |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 748 | MD->removeInstruction(M); |
Chris Lattner | d528be6 | 2010-11-18 08:07:09 +0000 | [diff] [blame] | 749 | M->eraseFromParent(); |
| 750 | ++NumMemCpyInstr; |
| 751 | return true; |
Chris Lattner | 43f8e43 | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | |
Gabor Greif | 7d3056b | 2010-07-28 22:50:26 +0000 | [diff] [blame] | 755 | /// processMemCpy - perform simplification of memcpy's. If we have memcpy A |
| 756 | /// which copies X to Y, and memcpy B which copies Y to Z, then we can rewrite |
| 757 | /// B to be a memcpy from X to Z (or potentially a memmove, depending on |
| 758 | /// circumstances). This allows later passes to remove the first memcpy |
| 759 | /// altogether. |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 760 | bool MemCpyOpt::processMemCpy(MemCpyInst *M) { |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 761 | // We can only optimize statically-sized memcpy's that are non-volatile. |
| 762 | ConstantInt *CopySize = dyn_cast<ConstantInt>(M->getLength()); |
| 763 | if (CopySize == 0 || M->isVolatile()) return false; |
Owen Anderson | 6549121 | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 764 | |
Chris Lattner | 8fdca6a | 2010-12-09 07:45:45 +0000 | [diff] [blame^] | 765 | // If the source and destination of the memcpy are the same, then zap it. |
| 766 | if (M->getSource() == M->getDest()) { |
| 767 | MD->removeInstruction(M); |
| 768 | M->eraseFromParent(); |
| 769 | return false; |
| 770 | } |
| 771 | |
| 772 | |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 773 | // The are two possible optimizations we can do for memcpy: |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 774 | // a) memcpy-memcpy xform which exposes redundance for DSE. |
| 775 | // b) call-memcpy xform for return slot optimization. |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 776 | MemDepResult DepInfo = MD->getDependency(M); |
| 777 | if (!DepInfo.isClobber()) |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 778 | return false; |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 779 | |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 780 | if (MemCpyInst *MDep = dyn_cast<MemCpyInst>(DepInfo.getInst())) |
| 781 | return processMemCpyMemCpyDependence(M, MDep, CopySize->getZExtValue()); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 782 | |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 783 | if (CallInst *C = dyn_cast<CallInst>(DepInfo.getInst())) { |
Chris Lattner | 8fdca6a | 2010-12-09 07:45:45 +0000 | [diff] [blame^] | 784 | if (performCallSlotOptzn(M, M->getDest(), M->getSource(), |
| 785 | CopySize->getZExtValue(), C)) { |
| 786 | M->eraseFromParent(); |
| 787 | return true; |
| 788 | } |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 789 | } |
Owen Anderson | 02e9988 | 2008-04-29 21:51:00 +0000 | [diff] [blame] | 790 | return false; |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Chris Lattner | f41eaac | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 793 | /// processMemMove - Transforms memmove calls to memcpy calls when the src/dst |
| 794 | /// are guaranteed not to alias. |
| 795 | bool MemCpyOpt::processMemMove(MemMoveInst *M) { |
| 796 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
| 797 | |
| 798 | // If the memmove is a constant size, use it for the alias query, this allows |
| 799 | // us to optimize things like: memmove(P, P+64, 64); |
Dan Gohman | 3da848b | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 800 | uint64_t MemMoveSize = AliasAnalysis::UnknownSize; |
Chris Lattner | f41eaac | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 801 | if (ConstantInt *Len = dyn_cast<ConstantInt>(M->getLength())) |
| 802 | MemMoveSize = Len->getZExtValue(); |
| 803 | |
| 804 | // See if the pointers alias. |
| 805 | if (AA.alias(M->getRawDest(), MemMoveSize, M->getRawSource(), MemMoveSize) != |
| 806 | AliasAnalysis::NoAlias) |
| 807 | return false; |
| 808 | |
David Greene | cb33fd1 | 2010-01-05 01:27:47 +0000 | [diff] [blame] | 809 | DEBUG(dbgs() << "MemCpyOpt: Optimizing memmove -> memcpy: " << *M << "\n"); |
Chris Lattner | f41eaac | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 810 | |
| 811 | // If not, then we know we can transform this. |
| 812 | Module *Mod = M->getParent()->getParent()->getParent(); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 813 | const Type *ArgTys[3] = { M->getRawDest()->getType(), |
| 814 | M->getRawSource()->getType(), |
| 815 | M->getLength()->getType() }; |
Gabor Greif | a399781 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 816 | M->setCalledFunction(Intrinsic::getDeclaration(Mod, Intrinsic::memcpy, |
| 817 | ArgTys, 3)); |
Duncan Sands | 05cd03b | 2009-09-03 13:37:16 +0000 | [diff] [blame] | 818 | |
Chris Lattner | f41eaac | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 819 | // MemDep may have over conservative information about this instruction, just |
| 820 | // conservatively flush it from the cache. |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 821 | MD->removeInstruction(M); |
Duncan Sands | 05cd03b | 2009-09-03 13:37:16 +0000 | [diff] [blame] | 822 | |
| 823 | ++NumMoveToCpy; |
Chris Lattner | f41eaac | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 824 | return true; |
| 825 | } |
| 826 | |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 827 | /// processByValArgument - This is called on every byval argument in call sites. |
| 828 | bool MemCpyOpt::processByValArgument(CallSite CS, unsigned ArgNo) { |
| 829 | TargetData *TD = getAnalysisIfAvailable<TargetData>(); |
| 830 | if (!TD) return false; |
Chris Lattner | f41eaac | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 831 | |
Chris Lattner | 604f6fe | 2010-11-21 08:06:10 +0000 | [diff] [blame] | 832 | // Find out what feeds this byval argument. |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 833 | Value *ByValArg = CS.getArgument(ArgNo); |
Chris Lattner | b5a3196 | 2010-12-01 01:24:55 +0000 | [diff] [blame] | 834 | const Type *ByValTy =cast<PointerType>(ByValArg->getType())->getElementType(); |
| 835 | uint64_t ByValSize = TD->getTypeAllocSize(ByValTy); |
Chris Lattner | 604f6fe | 2010-11-21 08:06:10 +0000 | [diff] [blame] | 836 | MemDepResult DepInfo = |
| 837 | MD->getPointerDependencyFrom(AliasAnalysis::Location(ByValArg, ByValSize), |
| 838 | true, CS.getInstruction(), |
| 839 | CS.getInstruction()->getParent()); |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 840 | if (!DepInfo.isClobber()) |
| 841 | return false; |
| 842 | |
| 843 | // If the byval argument isn't fed by a memcpy, ignore it. If it is fed by |
| 844 | // a memcpy, see if we can byval from the source of the memcpy instead of the |
| 845 | // result. |
| 846 | MemCpyInst *MDep = dyn_cast<MemCpyInst>(DepInfo.getInst()); |
| 847 | if (MDep == 0 || MDep->isVolatile() || |
| 848 | ByValArg->stripPointerCasts() != MDep->getDest()) |
| 849 | return false; |
| 850 | |
| 851 | // The length of the memcpy must be larger or equal to the size of the byval. |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 852 | ConstantInt *C1 = dyn_cast<ConstantInt>(MDep->getLength()); |
Chris Lattner | 604f6fe | 2010-11-21 08:06:10 +0000 | [diff] [blame] | 853 | if (C1 == 0 || C1->getValue().getZExtValue() < ByValSize) |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 854 | return false; |
| 855 | |
| 856 | // Get the alignment of the byval. If it is greater than the memcpy, then we |
| 857 | // can't do the substitution. If the call doesn't specify the alignment, then |
| 858 | // it is some target specific value that we can't know. |
| 859 | unsigned ByValAlign = CS.getParamAlignment(ArgNo+1); |
| 860 | if (ByValAlign == 0 || MDep->getAlignment() < ByValAlign) |
| 861 | return false; |
| 862 | |
| 863 | // Verify that the copied-from memory doesn't change in between the memcpy and |
| 864 | // the byval call. |
| 865 | // memcpy(a <- b) |
| 866 | // *b = 42; |
| 867 | // foo(*a) |
| 868 | // It would be invalid to transform the second memcpy into foo(*b). |
Chris Lattner | 604f6fe | 2010-11-21 08:06:10 +0000 | [diff] [blame] | 869 | // |
| 870 | // NOTE: This is conservative, it will stop on any read from the source loc, |
| 871 | // not just the defining memcpy. |
| 872 | MemDepResult SourceDep = |
| 873 | MD->getPointerDependencyFrom(AliasAnalysis::getLocationForSource(MDep), |
| 874 | false, CS.getInstruction(), MDep->getParent()); |
| 875 | if (!SourceDep.isClobber() || SourceDep.getInst() != MDep) |
| 876 | return false; |
| 877 | |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 878 | Value *TmpCast = MDep->getSource(); |
| 879 | if (MDep->getSource()->getType() != ByValArg->getType()) |
| 880 | TmpCast = new BitCastInst(MDep->getSource(), ByValArg->getType(), |
| 881 | "tmpcast", CS.getInstruction()); |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 882 | |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 883 | DEBUG(dbgs() << "MemCpyOpt: Forwarding memcpy to byval:\n" |
| 884 | << " " << *MDep << "\n" |
| 885 | << " " << *CS.getInstruction() << "\n"); |
| 886 | |
| 887 | // Otherwise we're good! Update the byval argument. |
| 888 | CS.setArgument(ArgNo, TmpCast); |
| 889 | ++NumMemCpyInstr; |
| 890 | return true; |
| 891 | } |
| 892 | |
| 893 | /// iterateOnFunction - Executes one iteration of MemCpyOpt. |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 894 | bool MemCpyOpt::iterateOnFunction(Function &F) { |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 895 | bool MadeChange = false; |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 896 | |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 897 | // Walk all instruction in the function. |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 898 | for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) { |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 899 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 900 | // Avoid invalidating the iterator. |
| 901 | Instruction *I = BI++; |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 902 | |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 903 | bool RepeatInstruction = false; |
| 904 | |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 905 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 906 | MadeChange |= processStore(SI, BI); |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 907 | else if (MemCpyInst *M = dyn_cast<MemCpyInst>(I)) { |
| 908 | RepeatInstruction = processMemCpy(M); |
| 909 | } else if (MemMoveInst *M = dyn_cast<MemMoveInst>(I)) { |
| 910 | RepeatInstruction = processMemMove(M); |
| 911 | } else if (CallSite CS = (Value*)I) { |
| 912 | for (unsigned i = 0, e = CS.arg_size(); i != e; ++i) |
| 913 | if (CS.paramHasAttr(i+1, Attribute::ByVal)) |
| 914 | MadeChange |= processByValArgument(CS, i); |
| 915 | } |
| 916 | |
| 917 | // Reprocess the instruction if desired. |
| 918 | if (RepeatInstruction) { |
| 919 | --BI; |
| 920 | MadeChange = true; |
Chris Lattner | f41eaac | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 921 | } |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 925 | return MadeChange; |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 926 | } |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 927 | |
| 928 | // MemCpyOpt::runOnFunction - This is the main transformation entry point for a |
| 929 | // function. |
| 930 | // |
| 931 | bool MemCpyOpt::runOnFunction(Function &F) { |
| 932 | bool MadeChange = false; |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 933 | MD = &getAnalysis<MemoryDependenceAnalysis>(); |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 934 | while (1) { |
| 935 | if (!iterateOnFunction(F)) |
| 936 | break; |
| 937 | MadeChange = true; |
| 938 | } |
| 939 | |
Chris Lattner | 2f5f90a | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 940 | MD = 0; |
Chris Lattner | 61c6ba8 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 941 | return MadeChange; |
| 942 | } |
| 943 | |
| 944 | |
| 945 | |