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