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" |
| 19 | #include "llvm/ParameterAttributes.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" |
| 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"); |
| 33 | |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 34 | /// isBytewiseValue - If the specified value can be set by repeating the same |
| 35 | /// byte in memory, return the i8 value that it is represented with. This is |
| 36 | /// true for all i8 values obviously, but is also true for i32 0, i32 -1, |
| 37 | /// i16 0xF0F0, double 0.0 etc. If the value can't be handled with a repeated |
| 38 | /// byte store (e.g. i16 0x1234), return null. |
| 39 | static Value *isBytewiseValue(Value *V) { |
| 40 | // All byte-wide stores are splatable, even of arbitrary variables. |
| 41 | if (V->getType() == Type::Int8Ty) return V; |
| 42 | |
| 43 | // Constant float and double values can be handled as integer values if the |
| 44 | // corresponding integer value is "byteable". An important case is 0.0. |
| 45 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 46 | if (CFP->getType() == Type::FloatTy) |
| 47 | V = ConstantExpr::getBitCast(CFP, Type::Int32Ty); |
| 48 | if (CFP->getType() == Type::DoubleTy) |
| 49 | V = ConstantExpr::getBitCast(CFP, Type::Int64Ty); |
| 50 | // Don't handle long double formats, which have strange constraints. |
| 51 | } |
| 52 | |
| 53 | // We can handle constant integers that are power of two in size and a |
| 54 | // multiple of 8 bits. |
| 55 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 56 | unsigned Width = CI->getBitWidth(); |
| 57 | if (isPowerOf2_32(Width) && Width > 8) { |
| 58 | // We can handle this value if the recursive binary decomposition is the |
| 59 | // same at all levels. |
| 60 | APInt Val = CI->getValue(); |
| 61 | APInt Val2; |
| 62 | while (Val.getBitWidth() != 8) { |
| 63 | unsigned NextWidth = Val.getBitWidth()/2; |
| 64 | Val2 = Val.lshr(NextWidth); |
| 65 | Val2.trunc(Val.getBitWidth()/2); |
| 66 | Val.trunc(Val.getBitWidth()/2); |
| 67 | |
| 68 | // If the top/bottom halves aren't the same, reject it. |
| 69 | if (Val != Val2) |
| 70 | return 0; |
| 71 | } |
| 72 | return ConstantInt::get(Val); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Conceptually, we could handle things like: |
| 77 | // %a = zext i8 %X to i16 |
| 78 | // %b = shl i16 %a, 8 |
| 79 | // %c = or i16 %a, %b |
| 80 | // but until there is an example that actually needs this, it doesn't seem |
| 81 | // worth worrying about. |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int64_t GetOffsetFromIndex(const GetElementPtrInst *GEP, unsigned Idx, |
| 86 | bool &VariableIdxFound, TargetData &TD) { |
| 87 | // Skip over the first indices. |
| 88 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 89 | for (unsigned i = 1; i != Idx; ++i, ++GTI) |
| 90 | /*skip along*/; |
| 91 | |
| 92 | // Compute the offset implied by the rest of the indices. |
| 93 | int64_t Offset = 0; |
| 94 | for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 95 | ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i)); |
| 96 | if (OpC == 0) |
| 97 | return VariableIdxFound = true; |
| 98 | if (OpC->isZero()) continue; // No offset. |
| 99 | |
| 100 | // Handle struct indices, which add their field offset to the pointer. |
| 101 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 102 | Offset += TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); |
| 103 | continue; |
| 104 | } |
| 105 | |
| 106 | // Otherwise, we have a sequential type like an array or vector. Multiply |
| 107 | // the index by the ElementSize. |
| 108 | uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()); |
| 109 | Offset += Size*OpC->getSExtValue(); |
| 110 | } |
| 111 | |
| 112 | return Offset; |
| 113 | } |
| 114 | |
| 115 | /// IsPointerOffset - Return true if Ptr1 is provably equal to Ptr2 plus a |
| 116 | /// constant offset, and return that constant offset. For example, Ptr1 might |
| 117 | /// be &A[42], and Ptr2 might be &A[40]. In this case offset would be -8. |
| 118 | static bool IsPointerOffset(Value *Ptr1, Value *Ptr2, int64_t &Offset, |
| 119 | TargetData &TD) { |
| 120 | // Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical |
| 121 | // base. After that base, they may have some number of common (and |
| 122 | // potentially variable) indices. After that they handle some constant |
| 123 | // offset, which determines their offset from each other. At this point, we |
| 124 | // handle no other case. |
| 125 | GetElementPtrInst *GEP1 = dyn_cast<GetElementPtrInst>(Ptr1); |
| 126 | GetElementPtrInst *GEP2 = dyn_cast<GetElementPtrInst>(Ptr2); |
| 127 | if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0)) |
| 128 | return false; |
| 129 | |
| 130 | // Skip any common indices and track the GEP types. |
| 131 | unsigned Idx = 1; |
| 132 | for (; Idx != GEP1->getNumOperands() && Idx != GEP2->getNumOperands(); ++Idx) |
| 133 | if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx)) |
| 134 | break; |
| 135 | |
| 136 | bool VariableIdxFound = false; |
| 137 | int64_t Offset1 = GetOffsetFromIndex(GEP1, Idx, VariableIdxFound, TD); |
| 138 | int64_t Offset2 = GetOffsetFromIndex(GEP2, Idx, VariableIdxFound, TD); |
| 139 | if (VariableIdxFound) return false; |
| 140 | |
| 141 | Offset = Offset2-Offset1; |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | /// MemsetRange - Represents a range of memset'd bytes with the ByteVal value. |
| 147 | /// This allows us to analyze stores like: |
| 148 | /// store 0 -> P+1 |
| 149 | /// store 0 -> P+0 |
| 150 | /// store 0 -> P+3 |
| 151 | /// store 0 -> P+2 |
| 152 | /// which sometimes happens with stores to arrays of structs etc. When we see |
| 153 | /// the first store, we make a range [1, 2). The second store extends the range |
| 154 | /// to [0, 2). The third makes a new range [2, 3). The fourth store joins the |
| 155 | /// two ranges into [0, 3) which is memset'able. |
| 156 | namespace { |
| 157 | struct MemsetRange { |
| 158 | // Start/End - A semi range that describes the span that this range covers. |
| 159 | // The range is closed at the start and open at the end: [Start, End). |
| 160 | int64_t Start, End; |
| 161 | |
| 162 | /// StartPtr - The getelementptr instruction that points to the start of the |
| 163 | /// range. |
| 164 | Value *StartPtr; |
| 165 | |
| 166 | /// Alignment - The known alignment of the first store. |
| 167 | unsigned Alignment; |
| 168 | |
| 169 | /// TheStores - The actual stores that make up this range. |
| 170 | SmallVector<StoreInst*, 16> TheStores; |
| 171 | |
| 172 | bool isProfitableToUseMemset(const TargetData &TD) const; |
| 173 | |
| 174 | }; |
| 175 | } // end anon namespace |
| 176 | |
| 177 | bool MemsetRange::isProfitableToUseMemset(const TargetData &TD) const { |
| 178 | // If we found more than 8 stores to merge or 64 bytes, use memset. |
| 179 | if (TheStores.size() >= 8 || End-Start >= 64) return true; |
| 180 | |
| 181 | // Assume that the code generator is capable of merging pairs of stores |
| 182 | // together if it wants to. |
| 183 | if (TheStores.size() <= 2) return false; |
| 184 | |
| 185 | // If we have fewer than 8 stores, it can still be worthwhile to do this. |
| 186 | // For example, merging 4 i8 stores into an i32 store is useful almost always. |
| 187 | // However, merging 2 32-bit stores isn't useful on a 32-bit architecture (the |
| 188 | // memset will be split into 2 32-bit stores anyway) and doing so can |
| 189 | // pessimize the llvm optimizer. |
| 190 | // |
| 191 | // Since we don't have perfect knowledge here, make some assumptions: assume |
| 192 | // the maximum GPR width is the same size as the pointer size and assume that |
| 193 | // this width can be stored. If so, check to see whether we will end up |
| 194 | // actually reducing the number of stores used. |
| 195 | unsigned Bytes = unsigned(End-Start); |
| 196 | unsigned NumPointerStores = Bytes/TD.getPointerSize(); |
| 197 | |
| 198 | // Assume the remaining bytes if any are done a byte at a time. |
| 199 | unsigned NumByteStores = Bytes - NumPointerStores*TD.getPointerSize(); |
| 200 | |
| 201 | // If we will reduce the # stores (according to this heuristic), do the |
| 202 | // transformation. This encourages merging 4 x i8 -> i32 and 2 x i16 -> i32 |
| 203 | // etc. |
| 204 | return TheStores.size() > NumPointerStores+NumByteStores; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | namespace { |
| 209 | class MemsetRanges { |
| 210 | /// Ranges - A sorted list of the memset ranges. We use std::list here |
| 211 | /// because each element is relatively large and expensive to copy. |
| 212 | std::list<MemsetRange> Ranges; |
| 213 | typedef std::list<MemsetRange>::iterator range_iterator; |
| 214 | TargetData &TD; |
| 215 | public: |
| 216 | MemsetRanges(TargetData &td) : TD(td) {} |
| 217 | |
| 218 | typedef std::list<MemsetRange>::const_iterator const_iterator; |
| 219 | const_iterator begin() const { return Ranges.begin(); } |
| 220 | const_iterator end() const { return Ranges.end(); } |
| 221 | bool empty() const { return Ranges.empty(); } |
| 222 | |
| 223 | void addStore(int64_t OffsetFromFirst, StoreInst *SI); |
| 224 | }; |
| 225 | |
| 226 | } // end anon namespace |
| 227 | |
| 228 | |
| 229 | /// addStore - Add a new store to the MemsetRanges data structure. This adds a |
| 230 | /// new range for the specified store at the specified offset, merging into |
| 231 | /// existing ranges as appropriate. |
| 232 | void MemsetRanges::addStore(int64_t Start, StoreInst *SI) { |
| 233 | int64_t End = Start+TD.getTypeStoreSize(SI->getOperand(0)->getType()); |
| 234 | |
| 235 | // Do a linear search of the ranges to see if this can be joined and/or to |
| 236 | // find the insertion point in the list. We keep the ranges sorted for |
| 237 | // simplicity here. This is a linear search of a linked list, which is ugly, |
| 238 | // however the number of ranges is limited, so this won't get crazy slow. |
| 239 | range_iterator I = Ranges.begin(), E = Ranges.end(); |
| 240 | |
| 241 | while (I != E && Start > I->End) |
| 242 | ++I; |
| 243 | |
| 244 | // We now know that I == E, in which case we didn't find anything to merge |
| 245 | // with, or that Start <= I->End. If End < I->Start or I == E, then we need |
| 246 | // to insert a new range. Handle this now. |
| 247 | if (I == E || End < I->Start) { |
| 248 | MemsetRange &R = *Ranges.insert(I, MemsetRange()); |
| 249 | R.Start = Start; |
| 250 | R.End = End; |
| 251 | R.StartPtr = SI->getPointerOperand(); |
| 252 | R.Alignment = SI->getAlignment(); |
| 253 | R.TheStores.push_back(SI); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | // This store overlaps with I, add it. |
| 258 | I->TheStores.push_back(SI); |
| 259 | |
| 260 | // At this point, we may have an interval that completely contains our store. |
| 261 | // If so, just add it to the interval and return. |
| 262 | if (I->Start <= Start && I->End >= End) |
| 263 | return; |
| 264 | |
| 265 | // Now we know that Start <= I->End and End >= I->Start so the range overlaps |
| 266 | // but is not entirely contained within the range. |
| 267 | |
| 268 | // See if the range extends the start of the range. In this case, it couldn't |
| 269 | // possibly cause it to join the prior range, because otherwise we would have |
| 270 | // stopped on *it*. |
| 271 | if (Start < I->Start) { |
| 272 | I->Start = Start; |
| 273 | I->StartPtr = SI->getPointerOperand(); |
| 274 | } |
| 275 | |
| 276 | // Now we know that Start <= I->End and Start >= I->Start (so the startpoint |
| 277 | // is in or right at the end of I), and that End >= I->Start. Extend I out to |
| 278 | // End. |
| 279 | if (End > I->End) { |
| 280 | I->End = End; |
| 281 | range_iterator NextI = I;; |
| 282 | while (++NextI != E && End >= NextI->Start) { |
| 283 | // Merge the range in. |
| 284 | I->TheStores.append(NextI->TheStores.begin(), NextI->TheStores.end()); |
| 285 | if (NextI->End > I->End) |
| 286 | I->End = NextI->End; |
| 287 | Ranges.erase(NextI); |
| 288 | NextI = I; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | //===----------------------------------------------------------------------===// |
| 294 | // MemCpyOpt Pass |
| 295 | //===----------------------------------------------------------------------===// |
| 296 | |
| 297 | namespace { |
| 298 | |
| 299 | class VISIBILITY_HIDDEN MemCpyOpt : public FunctionPass { |
| 300 | bool runOnFunction(Function &F); |
| 301 | public: |
| 302 | static char ID; // Pass identification, replacement for typeid |
| 303 | MemCpyOpt() : FunctionPass((intptr_t)&ID) { } |
| 304 | |
| 305 | private: |
| 306 | // This transformation requires dominator postdominator info |
| 307 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 308 | AU.setPreservesCFG(); |
| 309 | AU.addRequired<DominatorTree>(); |
| 310 | AU.addRequired<MemoryDependenceAnalysis>(); |
| 311 | AU.addRequired<AliasAnalysis>(); |
| 312 | AU.addRequired<TargetData>(); |
| 313 | AU.addPreserved<AliasAnalysis>(); |
| 314 | AU.addPreserved<MemoryDependenceAnalysis>(); |
| 315 | AU.addPreserved<TargetData>(); |
| 316 | } |
| 317 | |
| 318 | // Helper fuctions |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 319 | bool processStore(StoreInst *SI, BasicBlock::iterator& BBI); |
| 320 | bool processMemCpy(MemCpyInst* M); |
| 321 | bool performCallSlotOptzn(MemCpyInst* cpy, CallInst* C); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 322 | bool iterateOnFunction(Function &F); |
| 323 | }; |
| 324 | |
| 325 | char MemCpyOpt::ID = 0; |
| 326 | } |
| 327 | |
| 328 | // createMemCpyOptPass - The public interface to this file... |
| 329 | FunctionPass *llvm::createMemCpyOptPass() { return new MemCpyOpt(); } |
| 330 | |
| 331 | static RegisterPass<MemCpyOpt> X("memcpyopt", |
| 332 | "MemCpy Optimization"); |
| 333 | |
| 334 | |
| 335 | |
| 336 | /// processStore - When GVN is scanning forward over instructions, we look for |
| 337 | /// some other patterns to fold away. In particular, this looks for stores to |
| 338 | /// neighboring locations of memory. If it sees enough consequtive ones |
| 339 | /// (currently 4) it attempts to merge them together into a memcpy/memset. |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 340 | bool MemCpyOpt::processStore(StoreInst *SI, BasicBlock::iterator& BBI) { |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 341 | if (SI->isVolatile()) return false; |
| 342 | |
| 343 | // There are two cases that are interesting for this code to handle: memcpy |
| 344 | // and memset. Right now we only handle memset. |
| 345 | |
| 346 | // Ensure that the value being stored is something that can be memset'able a |
| 347 | // byte at a time like "0" or "-1" or any width, as well as things like |
| 348 | // 0xA0A0A0A0 and 0.0. |
| 349 | Value *ByteVal = isBytewiseValue(SI->getOperand(0)); |
| 350 | if (!ByteVal) |
| 351 | return false; |
| 352 | |
| 353 | TargetData &TD = getAnalysis<TargetData>(); |
| 354 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
| 355 | |
| 356 | // Okay, so we now have a single store that can be splatable. Scan to find |
| 357 | // all subsequent stores of the same value to offset from the same pointer. |
| 358 | // Join these together into ranges, so we can decide whether contiguous blocks |
| 359 | // are stored. |
| 360 | MemsetRanges Ranges(TD); |
| 361 | |
| 362 | Value *StartPtr = SI->getPointerOperand(); |
| 363 | |
| 364 | BasicBlock::iterator BI = SI; |
| 365 | for (++BI; !isa<TerminatorInst>(BI); ++BI) { |
| 366 | if (isa<CallInst>(BI) || isa<InvokeInst>(BI)) { |
| 367 | // If the call is readnone, ignore it, otherwise bail out. We don't even |
| 368 | // allow readonly here because we don't want something like: |
| 369 | // A[1] = 2; strlen(A); A[2] = 2; -> memcpy(A, ...); strlen(A). |
| 370 | if (AA.getModRefBehavior(CallSite::get(BI)) == |
| 371 | AliasAnalysis::DoesNotAccessMemory) |
| 372 | continue; |
| 373 | |
| 374 | // TODO: If this is a memset, try to join it in. |
| 375 | |
| 376 | break; |
| 377 | } else if (isa<VAArgInst>(BI) || isa<LoadInst>(BI)) |
| 378 | break; |
| 379 | |
| 380 | // If this is a non-store instruction it is fine, ignore it. |
| 381 | StoreInst *NextStore = dyn_cast<StoreInst>(BI); |
| 382 | if (NextStore == 0) continue; |
| 383 | |
| 384 | // If this is a store, see if we can merge it in. |
| 385 | if (NextStore->isVolatile()) break; |
| 386 | |
| 387 | // Check to see if this stored value is of the same byte-splattable value. |
| 388 | if (ByteVal != isBytewiseValue(NextStore->getOperand(0))) |
| 389 | break; |
| 390 | |
| 391 | // Check to see if this store is to a constant offset from the start ptr. |
| 392 | int64_t Offset; |
| 393 | if (!IsPointerOffset(StartPtr, NextStore->getPointerOperand(), Offset, TD)) |
| 394 | break; |
| 395 | |
| 396 | Ranges.addStore(Offset, NextStore); |
| 397 | } |
| 398 | |
| 399 | // If we have no ranges, then we just had a single store with nothing that |
| 400 | // could be merged in. This is a very common case of course. |
| 401 | if (Ranges.empty()) |
| 402 | return false; |
| 403 | |
| 404 | // If we had at least one store that could be merged in, add the starting |
| 405 | // store as well. We try to avoid this unless there is at least something |
| 406 | // interesting as a small compile-time optimization. |
| 407 | Ranges.addStore(0, SI); |
| 408 | |
| 409 | |
| 410 | Function *MemSetF = 0; |
| 411 | |
| 412 | // Now that we have full information about ranges, loop over the ranges and |
| 413 | // emit memset's for anything big enough to be worthwhile. |
| 414 | bool MadeChange = false; |
| 415 | for (MemsetRanges::const_iterator I = Ranges.begin(), E = Ranges.end(); |
| 416 | I != E; ++I) { |
| 417 | const MemsetRange &Range = *I; |
| 418 | |
| 419 | if (Range.TheStores.size() == 1) continue; |
| 420 | |
| 421 | // If it is profitable to lower this range to memset, do so now. |
| 422 | if (!Range.isProfitableToUseMemset(TD)) |
| 423 | continue; |
| 424 | |
| 425 | // Otherwise, we do want to transform this! Create a new memset. We put |
| 426 | // the memset right before the first instruction that isn't part of this |
| 427 | // memset block. This ensure that the memset is dominated by any addressing |
| 428 | // instruction needed by the start of the block. |
| 429 | BasicBlock::iterator InsertPt = BI; |
| 430 | |
| 431 | if (MemSetF == 0) |
| 432 | MemSetF = Intrinsic::getDeclaration(SI->getParent()->getParent() |
| 433 | ->getParent(), Intrinsic::memset_i64); |
| 434 | |
| 435 | // Get the starting pointer of the block. |
| 436 | StartPtr = Range.StartPtr; |
| 437 | |
| 438 | // Cast the start ptr to be i8* as memset requires. |
| 439 | const Type *i8Ptr = PointerType::getUnqual(Type::Int8Ty); |
| 440 | if (StartPtr->getType() != i8Ptr) |
| 441 | StartPtr = new BitCastInst(StartPtr, i8Ptr, StartPtr->getNameStart(), |
| 442 | InsertPt); |
| 443 | |
| 444 | Value *Ops[] = { |
| 445 | StartPtr, ByteVal, // Start, value |
| 446 | ConstantInt::get(Type::Int64Ty, Range.End-Range.Start), // size |
| 447 | ConstantInt::get(Type::Int32Ty, Range.Alignment) // align |
| 448 | }; |
| 449 | Value *C = CallInst::Create(MemSetF, Ops, Ops+4, "", InsertPt); |
| 450 | DEBUG(cerr << "Replace stores:\n"; |
| 451 | for (unsigned i = 0, e = Range.TheStores.size(); i != e; ++i) |
| 452 | cerr << *Range.TheStores[i]; |
| 453 | cerr << "With: " << *C); C=C; |
| 454 | |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 455 | // Don't invalidate the iterator |
| 456 | BBI = BI; |
| 457 | |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 458 | // Zap all the stores. |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 459 | for (SmallVector<StoreInst*, 16>::const_iterator SI = Range.TheStores.begin(), |
| 460 | SE = Range.TheStores.end(); SI != SE; ++SI) |
| 461 | (*SI)->eraseFromParent(); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 462 | ++NumMemSetInfer; |
| 463 | MadeChange = true; |
| 464 | } |
| 465 | |
| 466 | return MadeChange; |
| 467 | } |
| 468 | |
| 469 | |
| 470 | /// performCallSlotOptzn - takes a memcpy and a call that it depends on, |
| 471 | /// and checks for the possibility of a call slot optimization by having |
| 472 | /// the call write its result directly into the destination of the memcpy. |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 473 | bool MemCpyOpt::performCallSlotOptzn(MemCpyInst *cpy, CallInst *C) { |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 474 | // The general transformation to keep in mind is |
| 475 | // |
| 476 | // call @func(..., src, ...) |
| 477 | // memcpy(dest, src, ...) |
| 478 | // |
| 479 | // -> |
| 480 | // |
| 481 | // memcpy(dest, src, ...) |
| 482 | // call @func(..., dest, ...) |
| 483 | // |
| 484 | // Since moving the memcpy is technically awkward, we additionally check that |
| 485 | // src only holds uninitialized values at the moment of the call, meaning that |
| 486 | // the memcpy can be discarded rather than moved. |
| 487 | |
| 488 | // Deliberately get the source and destination with bitcasts stripped away, |
| 489 | // because we'll need to do type comparisons based on the underlying type. |
| 490 | Value* cpyDest = cpy->getDest(); |
| 491 | Value* cpySrc = cpy->getSource(); |
| 492 | CallSite CS = CallSite::get(C); |
| 493 | |
| 494 | // We need to be able to reason about the size of the memcpy, so we require |
| 495 | // that it be a constant. |
| 496 | ConstantInt* cpyLength = dyn_cast<ConstantInt>(cpy->getLength()); |
| 497 | if (!cpyLength) |
| 498 | return false; |
| 499 | |
| 500 | // Require that src be an alloca. This simplifies the reasoning considerably. |
| 501 | AllocaInst* srcAlloca = dyn_cast<AllocaInst>(cpySrc); |
| 502 | if (!srcAlloca) |
| 503 | return false; |
| 504 | |
| 505 | // Check that all of src is copied to dest. |
| 506 | TargetData& TD = getAnalysis<TargetData>(); |
| 507 | |
| 508 | ConstantInt* srcArraySize = dyn_cast<ConstantInt>(srcAlloca->getArraySize()); |
| 509 | if (!srcArraySize) |
| 510 | return false; |
| 511 | |
| 512 | uint64_t srcSize = TD.getABITypeSize(srcAlloca->getAllocatedType()) * |
| 513 | srcArraySize->getZExtValue(); |
| 514 | |
| 515 | if (cpyLength->getZExtValue() < srcSize) |
| 516 | return false; |
| 517 | |
| 518 | // Check that accessing the first srcSize bytes of dest will not cause a |
| 519 | // trap. Otherwise the transform is invalid since it might cause a trap |
| 520 | // to occur earlier than it otherwise would. |
| 521 | if (AllocaInst* A = dyn_cast<AllocaInst>(cpyDest)) { |
| 522 | // The destination is an alloca. Check it is larger than srcSize. |
| 523 | ConstantInt* destArraySize = dyn_cast<ConstantInt>(A->getArraySize()); |
| 524 | if (!destArraySize) |
| 525 | return false; |
| 526 | |
| 527 | uint64_t destSize = TD.getABITypeSize(A->getAllocatedType()) * |
| 528 | destArraySize->getZExtValue(); |
| 529 | |
| 530 | if (destSize < srcSize) |
| 531 | return false; |
| 532 | } else if (Argument* A = dyn_cast<Argument>(cpyDest)) { |
| 533 | // If the destination is an sret parameter then only accesses that are |
| 534 | // outside of the returned struct type can trap. |
| 535 | if (!A->hasStructRetAttr()) |
| 536 | return false; |
| 537 | |
| 538 | const Type* StructTy = cast<PointerType>(A->getType())->getElementType(); |
| 539 | uint64_t destSize = TD.getABITypeSize(StructTy); |
| 540 | |
| 541 | if (destSize < srcSize) |
| 542 | return false; |
| 543 | } else { |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | // Check that src is not accessed except via the call and the memcpy. This |
| 548 | // guarantees that it holds only undefined values when passed in (so the final |
| 549 | // memcpy can be dropped), that it is not read or written between the call and |
| 550 | // the memcpy, and that writing beyond the end of it is undefined. |
| 551 | SmallVector<User*, 8> srcUseList(srcAlloca->use_begin(), |
| 552 | srcAlloca->use_end()); |
| 553 | while (!srcUseList.empty()) { |
| 554 | User* UI = srcUseList.back(); |
| 555 | srcUseList.pop_back(); |
| 556 | |
| 557 | if (isa<GetElementPtrInst>(UI) || isa<BitCastInst>(UI)) { |
| 558 | for (User::use_iterator I = UI->use_begin(), E = UI->use_end(); |
| 559 | I != E; ++I) |
| 560 | srcUseList.push_back(*I); |
| 561 | } else if (UI != C && UI != cpy) { |
| 562 | return false; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | // Since we're changing the parameter to the callsite, we need to make sure |
| 567 | // that what would be the new parameter dominates the callsite. |
| 568 | DominatorTree& DT = getAnalysis<DominatorTree>(); |
| 569 | if (Instruction* cpyDestInst = dyn_cast<Instruction>(cpyDest)) |
| 570 | if (!DT.dominates(cpyDestInst, C)) |
| 571 | return false; |
| 572 | |
| 573 | // In addition to knowing that the call does not access src in some |
| 574 | // unexpected manner, for example via a global, which we deduce from |
| 575 | // the use analysis, we also need to know that it does not sneakily |
| 576 | // access dest. We rely on AA to figure this out for us. |
| 577 | AliasAnalysis& AA = getAnalysis<AliasAnalysis>(); |
| 578 | if (AA.getModRefInfo(C, cpy->getRawDest(), srcSize) != |
| 579 | AliasAnalysis::NoModRef) |
| 580 | return false; |
| 581 | |
| 582 | // All the checks have passed, so do the transformation. |
| 583 | for (unsigned i = 0; i < CS.arg_size(); ++i) |
| 584 | if (CS.getArgument(i) == cpySrc) { |
| 585 | if (cpySrc->getType() != cpyDest->getType()) |
| 586 | cpyDest = CastInst::createPointerCast(cpyDest, cpySrc->getType(), |
| 587 | cpyDest->getName(), C); |
| 588 | CS.setArgument(i, cpyDest); |
| 589 | } |
| 590 | |
| 591 | // Drop any cached information about the call, because we may have changed |
| 592 | // its dependence information by changing its parameter. |
| 593 | MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>(); |
| 594 | MD.dropInstruction(C); |
| 595 | |
| 596 | // Remove the memcpy |
| 597 | MD.removeInstruction(cpy); |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 598 | cpy->eraseFromParent(); |
| 599 | NumMemCpyInstr++; |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 600 | |
| 601 | return true; |
| 602 | } |
| 603 | |
| 604 | /// processMemCpy - perform simplication of memcpy's. If we have memcpy A which |
| 605 | /// copies X to Y, and memcpy B which copies Y to Z, then we can rewrite B to be |
| 606 | /// a memcpy from X to Z (or potentially a memmove, depending on circumstances). |
| 607 | /// This allows later passes to remove the first memcpy altogether. |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 608 | bool MemCpyOpt::processMemCpy(MemCpyInst* M) { |
| 609 | MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>(); |
| 610 | |
| 611 | // The are two possible optimizations we can do for memcpy: |
| 612 | // a) memcpy-memcpy xform which exposes redundance for DSE |
| 613 | // b) call-memcpy xform for return slot optimization |
| 614 | Instruction* dep = MD.getDependency(M); |
| 615 | if (dep == MemoryDependenceAnalysis::None || |
| 616 | dep == MemoryDependenceAnalysis::NonLocal) |
| 617 | return false; |
Owen Anderson | 9dcace3 | 2008-04-29 21:26:06 +0000 | [diff] [blame] | 618 | else if (!isa<MemCpyInst>(dep)) { |
| 619 | if (CallInst* C = dyn_cast<CallInst>(dep)) |
| 620 | return performCallSlotOptzn(M, C); |
| 621 | else |
| 622 | return false; |
| 623 | } |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 624 | |
| 625 | MemCpyInst* MDep = cast<MemCpyInst>(dep); |
| 626 | |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 627 | // We can only transforms memcpy's where the dest of one is the source of the |
| 628 | // other |
| 629 | if (M->getSource() != MDep->getDest()) |
| 630 | return false; |
| 631 | |
| 632 | // Second, the length of the memcpy's must be the same, or the preceeding one |
| 633 | // must be larger than the following one. |
| 634 | ConstantInt* C1 = dyn_cast<ConstantInt>(MDep->getLength()); |
| 635 | ConstantInt* C2 = dyn_cast<ConstantInt>(M->getLength()); |
| 636 | if (!C1 || !C2) |
| 637 | return false; |
| 638 | |
| 639 | uint64_t DepSize = C1->getValue().getZExtValue(); |
| 640 | uint64_t CpySize = C2->getValue().getZExtValue(); |
| 641 | |
| 642 | if (DepSize < CpySize) |
| 643 | return false; |
| 644 | |
| 645 | // Finally, we have to make sure that the dest of the second does not |
| 646 | // alias the source of the first |
| 647 | AliasAnalysis& AA = getAnalysis<AliasAnalysis>(); |
| 648 | if (AA.alias(M->getRawDest(), CpySize, MDep->getRawSource(), DepSize) != |
| 649 | AliasAnalysis::NoAlias) |
| 650 | return false; |
| 651 | else if (AA.alias(M->getRawDest(), CpySize, M->getRawSource(), CpySize) != |
| 652 | AliasAnalysis::NoAlias) |
| 653 | return false; |
| 654 | else if (AA.alias(MDep->getRawDest(), DepSize, MDep->getRawSource(), DepSize) |
| 655 | != AliasAnalysis::NoAlias) |
| 656 | return false; |
| 657 | |
| 658 | // If all checks passed, then we can transform these memcpy's |
| 659 | Function* MemCpyFun = Intrinsic::getDeclaration( |
| 660 | M->getParent()->getParent()->getParent(), |
| 661 | M->getIntrinsicID()); |
| 662 | |
| 663 | std::vector<Value*> args; |
| 664 | args.push_back(M->getRawDest()); |
| 665 | args.push_back(MDep->getRawSource()); |
| 666 | args.push_back(M->getLength()); |
| 667 | args.push_back(M->getAlignment()); |
| 668 | |
| 669 | CallInst* C = CallInst::Create(MemCpyFun, args.begin(), args.end(), "", M); |
| 670 | |
Owen Anderson | 02e9988 | 2008-04-29 21:51:00 +0000 | [diff] [blame^] | 671 | |
| 672 | // If C and M don't interfere, then this is a valid transformation. If they |
| 673 | // did, this would mean that the two sources overlap, which would be bad. |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 674 | if (MD.getDependency(C) == MDep) { |
| 675 | MD.dropInstruction(M); |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 676 | M->eraseFromParent(); |
Owen Anderson | 02e9988 | 2008-04-29 21:51:00 +0000 | [diff] [blame^] | 677 | |
| 678 | NumMemCpyInstr++; |
| 679 | |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 680 | return true; |
| 681 | } |
| 682 | |
Owen Anderson | 02e9988 | 2008-04-29 21:51:00 +0000 | [diff] [blame^] | 683 | // Otherwise, there was no point in doing this, so we remove the call we |
| 684 | // inserted and act like nothing happened. |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 685 | MD.removeInstruction(C); |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 686 | C->eraseFromParent(); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 687 | |
Owen Anderson | 02e9988 | 2008-04-29 21:51:00 +0000 | [diff] [blame^] | 688 | return false; |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | // MemCpyOpt::runOnFunction - This is the main transformation entry point for a |
| 692 | // function. |
| 693 | // |
| 694 | bool MemCpyOpt::runOnFunction(Function& F) { |
| 695 | |
| 696 | bool changed = false; |
| 697 | bool shouldContinue = true; |
| 698 | |
| 699 | while (shouldContinue) { |
| 700 | shouldContinue = iterateOnFunction(F); |
| 701 | changed |= shouldContinue; |
| 702 | } |
| 703 | |
| 704 | return changed; |
| 705 | } |
| 706 | |
| 707 | |
| 708 | // MemCpyOpt::iterateOnFunction - Executes one iteration of GVN |
| 709 | bool MemCpyOpt::iterateOnFunction(Function &F) { |
| 710 | bool changed_function = false; |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 711 | |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 712 | // Walk all instruction in the function |
| 713 | for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) { |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 714 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); |
| 715 | BI != BE;) { |
Owen Anderson | a8bd658 | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 716 | // Avoid invalidating the iterator |
| 717 | Instruction* I = BI++; |
| 718 | |
| 719 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) |
| 720 | changed_function |= processStore(SI, BI); |
| 721 | |
| 722 | if (MemCpyInst* M = dyn_cast<MemCpyInst>(I)) { |
| 723 | changed_function |= processMemCpy(M); |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 724 | } |
Owen Anderson | a723d1e | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
| 728 | return changed_function; |
| 729 | } |