Owen Anderson | ef9a6fd | 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 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Scalar.h" |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallVector.h" |
| 17 | #include "llvm/ADT/Statistic.h" |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/AliasAnalysis.h" |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame^] | 19 | #include "llvm/Analysis/AssumptionTracker.h" |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 24 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/GlobalVariable.h" |
| 26 | #include "llvm/IR/IRBuilder.h" |
| 27 | #include "llvm/IR/Instructions.h" |
| 28 | #include "llvm/IR/IntrinsicInst.h" |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
Chris Lattner | b25de3f | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 23f61a0 | 2011-05-01 18:27:11 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetLibraryInfo.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/Utils/Local.h" |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 33 | #include <list> |
| 34 | using namespace llvm; |
| 35 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 36 | #define DEBUG_TYPE "memcpyopt" |
| 37 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 38 | STATISTIC(NumMemCpyInstr, "Number of memcpy instructions deleted"); |
| 39 | STATISTIC(NumMemSetInfer, "Number of memsets inferred"); |
Duncan Sands | 0edc710 | 2009-09-03 13:37:16 +0000 | [diff] [blame] | 40 | STATISTIC(NumMoveToCpy, "Number of memmoves converted to memcpy"); |
Benjamin Kramer | ea9152e | 2010-12-24 21:17:12 +0000 | [diff] [blame] | 41 | STATISTIC(NumCpyToSet, "Number of memcpys converted to memset"); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 42 | |
Benjamin Kramer | 15a257d | 2012-09-13 16:29:49 +0000 | [diff] [blame] | 43 | static int64_t GetOffsetFromIndex(const GEPOperator *GEP, unsigned Idx, |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 44 | bool &VariableIdxFound, const DataLayout &TD){ |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 45 | // Skip over the first indices. |
| 46 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 47 | for (unsigned i = 1; i != Idx; ++i, ++GTI) |
| 48 | /*skip along*/; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 49 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 50 | // Compute the offset implied by the rest of the indices. |
| 51 | int64_t Offset = 0; |
| 52 | for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 53 | ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 54 | if (!OpC) |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 55 | return VariableIdxFound = true; |
| 56 | if (OpC->isZero()) continue; // No offset. |
| 57 | |
| 58 | // Handle struct indices, which add their field offset to the pointer. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 59 | if (StructType *STy = dyn_cast<StructType>(*GTI)) { |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 60 | Offset += TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); |
| 61 | continue; |
| 62 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 63 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 64 | // Otherwise, we have a sequential type like an array or vector. Multiply |
| 65 | // the index by the ElementSize. |
Duncan Sands | af9eaa8 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 66 | uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType()); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 67 | Offset += Size*OpC->getSExtValue(); |
| 68 | } |
| 69 | |
| 70 | return Offset; |
| 71 | } |
| 72 | |
| 73 | /// IsPointerOffset - Return true if Ptr1 is provably equal to Ptr2 plus a |
| 74 | /// constant offset, and return that constant offset. For example, Ptr1 might |
| 75 | /// be &A[42], and Ptr2 might be &A[40]. In this case offset would be -8. |
| 76 | static bool IsPointerOffset(Value *Ptr1, Value *Ptr2, int64_t &Offset, |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 77 | const DataLayout &TD) { |
Chris Lattner | fa7c29d | 2011-01-12 01:43:46 +0000 | [diff] [blame] | 78 | Ptr1 = Ptr1->stripPointerCasts(); |
| 79 | Ptr2 = Ptr2->stripPointerCasts(); |
Benjamin Kramer | 3ef5e46 | 2014-03-10 21:05:13 +0000 | [diff] [blame] | 80 | |
| 81 | // Handle the trivial case first. |
| 82 | if (Ptr1 == Ptr2) { |
| 83 | Offset = 0; |
| 84 | return true; |
| 85 | } |
| 86 | |
Benjamin Kramer | 15a257d | 2012-09-13 16:29:49 +0000 | [diff] [blame] | 87 | GEPOperator *GEP1 = dyn_cast<GEPOperator>(Ptr1); |
| 88 | GEPOperator *GEP2 = dyn_cast<GEPOperator>(Ptr2); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 5120ebf | 2011-01-08 21:07:56 +0000 | [diff] [blame] | 90 | bool VariableIdxFound = false; |
| 91 | |
| 92 | // If one pointer is a GEP and the other isn't, then see if the GEP is a |
| 93 | // constant offset from the base, as in "P" and "gep P, 1". |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 94 | if (GEP1 && !GEP2 && GEP1->getOperand(0)->stripPointerCasts() == Ptr2) { |
Chris Lattner | 5120ebf | 2011-01-08 21:07:56 +0000 | [diff] [blame] | 95 | Offset = -GetOffsetFromIndex(GEP1, 1, VariableIdxFound, TD); |
| 96 | return !VariableIdxFound; |
| 97 | } |
| 98 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 99 | if (GEP2 && !GEP1 && GEP2->getOperand(0)->stripPointerCasts() == Ptr1) { |
Chris Lattner | 5120ebf | 2011-01-08 21:07:56 +0000 | [diff] [blame] | 100 | Offset = GetOffsetFromIndex(GEP2, 1, VariableIdxFound, TD); |
| 101 | return !VariableIdxFound; |
| 102 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 103 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 104 | // Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical |
| 105 | // base. After that base, they may have some number of common (and |
| 106 | // potentially variable) indices. After that they handle some constant |
| 107 | // offset, which determines their offset from each other. At this point, we |
| 108 | // handle no other case. |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 109 | if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0)) |
| 110 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 111 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 112 | // Skip any common indices and track the GEP types. |
| 113 | unsigned Idx = 1; |
| 114 | for (; Idx != GEP1->getNumOperands() && Idx != GEP2->getNumOperands(); ++Idx) |
| 115 | if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx)) |
| 116 | break; |
| 117 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 118 | int64_t Offset1 = GetOffsetFromIndex(GEP1, Idx, VariableIdxFound, TD); |
| 119 | int64_t Offset2 = GetOffsetFromIndex(GEP2, Idx, VariableIdxFound, TD); |
| 120 | if (VariableIdxFound) return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 121 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 122 | Offset = Offset2-Offset1; |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /// MemsetRange - Represents a range of memset'd bytes with the ByteVal value. |
| 128 | /// This allows us to analyze stores like: |
| 129 | /// store 0 -> P+1 |
| 130 | /// store 0 -> P+0 |
| 131 | /// store 0 -> P+3 |
| 132 | /// store 0 -> P+2 |
| 133 | /// which sometimes happens with stores to arrays of structs etc. When we see |
| 134 | /// the first store, we make a range [1, 2). The second store extends the range |
| 135 | /// to [0, 2). The third makes a new range [2, 3). The fourth store joins the |
| 136 | /// two ranges into [0, 3) which is memset'able. |
| 137 | namespace { |
| 138 | struct MemsetRange { |
| 139 | // Start/End - A semi range that describes the span that this range covers. |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 140 | // The range is closed at the start and open at the end: [Start, End). |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 141 | int64_t Start, End; |
| 142 | |
| 143 | /// StartPtr - The getelementptr instruction that points to the start of the |
| 144 | /// range. |
| 145 | Value *StartPtr; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 146 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 147 | /// Alignment - The known alignment of the first store. |
| 148 | unsigned Alignment; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 149 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 150 | /// TheStores - The actual stores that make up this range. |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 151 | SmallVector<Instruction*, 16> TheStores; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 152 | |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 153 | bool isProfitableToUseMemset(const DataLayout &TD) const; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 154 | |
| 155 | }; |
| 156 | } // end anon namespace |
| 157 | |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 158 | bool MemsetRange::isProfitableToUseMemset(const DataLayout &TD) const { |
Chad Rosier | 3277557 | 2011-12-05 22:53:09 +0000 | [diff] [blame] | 159 | // If we found more than 4 stores to merge or 16 bytes, use memset. |
Chad Rosier | 19446a0 | 2011-12-05 22:37:00 +0000 | [diff] [blame] | 160 | if (TheStores.size() >= 4 || End-Start >= 16) return true; |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 161 | |
| 162 | // If there is nothing to merge, don't do anything. |
| 163 | if (TheStores.size() < 2) return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 165 | // If any of the stores are a memset, then it is always good to extend the |
| 166 | // memset. |
| 167 | for (unsigned i = 0, e = TheStores.size(); i != e; ++i) |
| 168 | if (!isa<StoreInst>(TheStores[i])) |
| 169 | return true; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 170 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 171 | // Assume that the code generator is capable of merging pairs of stores |
| 172 | // together if it wants to. |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 173 | if (TheStores.size() == 2) return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 174 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 175 | // If we have fewer than 8 stores, it can still be worthwhile to do this. |
| 176 | // For example, merging 4 i8 stores into an i32 store is useful almost always. |
| 177 | // However, merging 2 32-bit stores isn't useful on a 32-bit architecture (the |
| 178 | // memset will be split into 2 32-bit stores anyway) and doing so can |
| 179 | // pessimize the llvm optimizer. |
| 180 | // |
| 181 | // Since we don't have perfect knowledge here, make some assumptions: assume |
Matt Arsenault | 899f7d2 | 2013-09-16 22:43:16 +0000 | [diff] [blame] | 182 | // the maximum GPR width is the same size as the largest legal integer |
| 183 | // size. If so, check to see whether we will end up actually reducing the |
| 184 | // number of stores used. |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 185 | unsigned Bytes = unsigned(End-Start); |
Matt Arsenault | 899f7d2 | 2013-09-16 22:43:16 +0000 | [diff] [blame] | 186 | unsigned MaxIntSize = TD.getLargestLegalIntTypeSize(); |
| 187 | if (MaxIntSize == 0) |
| 188 | MaxIntSize = 1; |
| 189 | unsigned NumPointerStores = Bytes / MaxIntSize; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 190 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 191 | // Assume the remaining bytes if any are done a byte at a time. |
Matt Arsenault | 899f7d2 | 2013-09-16 22:43:16 +0000 | [diff] [blame] | 192 | unsigned NumByteStores = Bytes - NumPointerStores * MaxIntSize; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 193 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 194 | // If we will reduce the # stores (according to this heuristic), do the |
| 195 | // transformation. This encourages merging 4 x i8 -> i32 and 2 x i16 -> i32 |
| 196 | // etc. |
| 197 | return TheStores.size() > NumPointerStores+NumByteStores; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 198 | } |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 199 | |
| 200 | |
| 201 | namespace { |
| 202 | class MemsetRanges { |
| 203 | /// Ranges - A sorted list of the memset ranges. We use std::list here |
| 204 | /// because each element is relatively large and expensive to copy. |
| 205 | std::list<MemsetRange> Ranges; |
| 206 | typedef std::list<MemsetRange>::iterator range_iterator; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 207 | const DataLayout &DL; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 208 | public: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 209 | MemsetRanges(const DataLayout &DL) : DL(DL) {} |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 210 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 211 | typedef std::list<MemsetRange>::const_iterator const_iterator; |
| 212 | const_iterator begin() const { return Ranges.begin(); } |
| 213 | const_iterator end() const { return Ranges.end(); } |
| 214 | bool empty() const { return Ranges.empty(); } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 215 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 216 | void addInst(int64_t OffsetFromFirst, Instruction *Inst) { |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 217 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) |
| 218 | addStore(OffsetFromFirst, SI); |
| 219 | else |
| 220 | addMemSet(OffsetFromFirst, cast<MemSetInst>(Inst)); |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 221 | } |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 222 | |
| 223 | void addStore(int64_t OffsetFromFirst, StoreInst *SI) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 224 | int64_t StoreSize = DL.getTypeStoreSize(SI->getOperand(0)->getType()); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 226 | addRange(OffsetFromFirst, StoreSize, |
| 227 | SI->getPointerOperand(), SI->getAlignment(), SI); |
| 228 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 229 | |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 230 | void addMemSet(int64_t OffsetFromFirst, MemSetInst *MSI) { |
| 231 | int64_t Size = cast<ConstantInt>(MSI->getLength())->getZExtValue(); |
| 232 | addRange(OffsetFromFirst, Size, MSI->getDest(), MSI->getAlignment(), MSI); |
| 233 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 235 | void addRange(int64_t Start, int64_t Size, Value *Ptr, |
| 236 | unsigned Alignment, Instruction *Inst); |
| 237 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 238 | }; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 239 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 240 | } // end anon namespace |
| 241 | |
| 242 | |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 243 | /// addRange - Add a new store to the MemsetRanges data structure. This adds a |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 244 | /// new range for the specified store at the specified offset, merging into |
| 245 | /// existing ranges as appropriate. |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 246 | /// |
| 247 | /// Do a linear search of the ranges to see if this can be joined and/or to |
| 248 | /// find the insertion point in the list. We keep the ranges sorted for |
| 249 | /// simplicity here. This is a linear search of a linked list, which is ugly, |
| 250 | /// however the number of ranges is limited, so this won't get crazy slow. |
| 251 | void MemsetRanges::addRange(int64_t Start, int64_t Size, Value *Ptr, |
| 252 | unsigned Alignment, Instruction *Inst) { |
| 253 | int64_t End = Start+Size; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 254 | range_iterator I = Ranges.begin(), E = Ranges.end(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 255 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 256 | while (I != E && Start > I->End) |
| 257 | ++I; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 258 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 259 | // We now know that I == E, in which case we didn't find anything to merge |
| 260 | // with, or that Start <= I->End. If End < I->Start or I == E, then we need |
| 261 | // to insert a new range. Handle this now. |
| 262 | if (I == E || End < I->Start) { |
| 263 | MemsetRange &R = *Ranges.insert(I, MemsetRange()); |
| 264 | R.Start = Start; |
| 265 | R.End = End; |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 266 | R.StartPtr = Ptr; |
| 267 | R.Alignment = Alignment; |
| 268 | R.TheStores.push_back(Inst); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 269 | return; |
| 270 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 271 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 272 | // This store overlaps with I, add it. |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 273 | I->TheStores.push_back(Inst); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 274 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 275 | // At this point, we may have an interval that completely contains our store. |
| 276 | // If so, just add it to the interval and return. |
| 277 | if (I->Start <= Start && I->End >= End) |
| 278 | return; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 279 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 280 | // Now we know that Start <= I->End and End >= I->Start so the range overlaps |
| 281 | // but is not entirely contained within the range. |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 282 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 283 | // See if the range extends the start of the range. In this case, it couldn't |
| 284 | // possibly cause it to join the prior range, because otherwise we would have |
| 285 | // stopped on *it*. |
| 286 | if (Start < I->Start) { |
| 287 | I->Start = Start; |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 288 | I->StartPtr = Ptr; |
| 289 | I->Alignment = Alignment; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 290 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 291 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 292 | // Now we know that Start <= I->End and Start >= I->Start (so the startpoint |
| 293 | // is in or right at the end of I), and that End >= I->Start. Extend I out to |
| 294 | // End. |
| 295 | if (End > I->End) { |
| 296 | I->End = End; |
Nick Lewycky | bfd4ad6 | 2009-03-19 05:51:39 +0000 | [diff] [blame] | 297 | range_iterator NextI = I; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 298 | while (++NextI != E && End >= NextI->Start) { |
| 299 | // Merge the range in. |
| 300 | I->TheStores.append(NextI->TheStores.begin(), NextI->TheStores.end()); |
| 301 | if (NextI->End > I->End) |
| 302 | I->End = NextI->End; |
| 303 | Ranges.erase(NextI); |
| 304 | NextI = I; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | //===----------------------------------------------------------------------===// |
| 310 | // MemCpyOpt Pass |
| 311 | //===----------------------------------------------------------------------===// |
| 312 | |
| 313 | namespace { |
Chris Lattner | 2dd09db | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 314 | class MemCpyOpt : public FunctionPass { |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 315 | MemoryDependenceAnalysis *MD; |
Chris Lattner | 23f61a0 | 2011-05-01 18:27:11 +0000 | [diff] [blame] | 316 | TargetLibraryInfo *TLI; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 317 | const DataLayout *DL; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 318 | public: |
| 319 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 320 | MemCpyOpt() : FunctionPass(ID) { |
| 321 | initializeMemCpyOptPass(*PassRegistry::getPassRegistry()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 322 | MD = nullptr; |
| 323 | TLI = nullptr; |
| 324 | DL = nullptr; |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 325 | } |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 326 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 327 | bool runOnFunction(Function &F) override; |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 328 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 329 | private: |
| 330 | // This transformation requires dominator postdominator info |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 331 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 332 | AU.setPreservesCFG(); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame^] | 333 | AU.addRequired<AssumptionTracker>(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 334 | AU.addRequired<DominatorTreeWrapperPass>(); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 335 | AU.addRequired<MemoryDependenceAnalysis>(); |
| 336 | AU.addRequired<AliasAnalysis>(); |
Chris Lattner | 23f61a0 | 2011-05-01 18:27:11 +0000 | [diff] [blame] | 337 | AU.addRequired<TargetLibraryInfo>(); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 338 | AU.addPreserved<AliasAnalysis>(); |
| 339 | AU.addPreserved<MemoryDependenceAnalysis>(); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 340 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 341 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 342 | // Helper fuctions |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 343 | bool processStore(StoreInst *SI, BasicBlock::iterator &BBI); |
Chris Lattner | 9a1d63b | 2011-01-08 21:19:19 +0000 | [diff] [blame] | 344 | bool processMemSet(MemSetInst *SI, BasicBlock::iterator &BBI); |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 345 | bool processMemCpy(MemCpyInst *M); |
Chris Lattner | 1145e33 | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 346 | bool processMemMove(MemMoveInst *M); |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 347 | bool performCallSlotOptzn(Instruction *cpy, Value *cpyDst, Value *cpySrc, |
Duncan Sands | c6ada69 | 2012-10-04 10:54:40 +0000 | [diff] [blame] | 348 | uint64_t cpyLen, unsigned cpyAlign, CallInst *C); |
Chris Lattner | 7e9b2ea | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 349 | bool processMemCpyMemCpyDependence(MemCpyInst *M, MemCpyInst *MDep, |
| 350 | uint64_t MSize); |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 351 | bool processByValArgument(CallSite CS, unsigned ArgNo); |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 352 | Instruction *tryMergingIntoMemset(Instruction *I, Value *StartPtr, |
| 353 | Value *ByteVal); |
| 354 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 355 | bool iterateOnFunction(Function &F); |
| 356 | }; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 357 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 358 | char MemCpyOpt::ID = 0; |
| 359 | } |
| 360 | |
| 361 | // createMemCpyOptPass - The public interface to this file... |
| 362 | FunctionPass *llvm::createMemCpyOptPass() { return new MemCpyOpt(); } |
| 363 | |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 364 | INITIALIZE_PASS_BEGIN(MemCpyOpt, "memcpyopt", "MemCpy Optimization", |
| 365 | false, false) |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame^] | 366 | INITIALIZE_PASS_DEPENDENCY(AssumptionTracker) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 367 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 368 | INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis) |
Chris Lattner | 23f61a0 | 2011-05-01 18:27:11 +0000 | [diff] [blame] | 369 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 370 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 371 | INITIALIZE_PASS_END(MemCpyOpt, "memcpyopt", "MemCpy Optimization", |
| 372 | false, false) |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 373 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 374 | /// tryMergingIntoMemset - When scanning forward over instructions, we look for |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 375 | /// some other patterns to fold away. In particular, this looks for stores to |
Duncan Sands | 75b5d27 | 2011-02-15 09:23:02 +0000 | [diff] [blame] | 376 | /// neighboring locations of memory. If it sees enough consecutive ones, it |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 377 | /// attempts to merge them together into a memcpy/memset. |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 378 | Instruction *MemCpyOpt::tryMergingIntoMemset(Instruction *StartInst, |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 379 | Value *StartPtr, Value *ByteVal) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 380 | if (!DL) return nullptr; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 381 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 382 | // Okay, so we now have a single store that can be splatable. Scan to find |
| 383 | // all subsequent stores of the same value to offset from the same pointer. |
| 384 | // Join these together into ranges, so we can decide whether contiguous blocks |
| 385 | // are stored. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 386 | MemsetRanges Ranges(*DL); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 387 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 388 | BasicBlock::iterator BI = StartInst; |
| 389 | for (++BI; !isa<TerminatorInst>(BI); ++BI) { |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 390 | if (!isa<StoreInst>(BI) && !isa<MemSetInst>(BI)) { |
| 391 | // If the instruction is readnone, ignore it, otherwise bail out. We |
| 392 | // don't even allow readonly here because we don't want something like: |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 393 | // A[1] = 2; strlen(A); A[2] = 2; -> memcpy(A, ...); strlen(A). |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 394 | if (BI->mayWriteToMemory() || BI->mayReadFromMemory()) |
| 395 | break; |
| 396 | continue; |
| 397 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 398 | |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 399 | if (StoreInst *NextStore = dyn_cast<StoreInst>(BI)) { |
| 400 | // If this is a store, see if we can merge it in. |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 401 | if (!NextStore->isSimple()) break; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 402 | |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 403 | // Check to see if this stored value is of the same byte-splattable value. |
| 404 | if (ByteVal != isBytewiseValue(NextStore->getOperand(0))) |
| 405 | break; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 407 | // Check to see if this store is to a constant offset from the start ptr. |
| 408 | int64_t Offset; |
Chris Lattner | caf5c0d | 2011-01-09 19:26:10 +0000 | [diff] [blame] | 409 | if (!IsPointerOffset(StartPtr, NextStore->getPointerOperand(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 410 | Offset, *DL)) |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 411 | break; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 412 | |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 413 | Ranges.addStore(Offset, NextStore); |
| 414 | } else { |
| 415 | MemSetInst *MSI = cast<MemSetInst>(BI); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 416 | |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 417 | if (MSI->isVolatile() || ByteVal != MSI->getValue() || |
| 418 | !isa<ConstantInt>(MSI->getLength())) |
| 419 | break; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 420 | |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 421 | // Check to see if this store is to a constant offset from the start ptr. |
| 422 | int64_t Offset; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 423 | if (!IsPointerOffset(StartPtr, MSI->getDest(), Offset, *DL)) |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 424 | break; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 425 | |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 426 | Ranges.addMemSet(Offset, MSI); |
| 427 | } |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 428 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 429 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 430 | // If we have no ranges, then we just had a single store with nothing that |
| 431 | // could be merged in. This is a very common case of course. |
| 432 | if (Ranges.empty()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 433 | return nullptr; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 434 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 435 | // If we had at least one store that could be merged in, add the starting |
| 436 | // store as well. We try to avoid this unless there is at least something |
| 437 | // interesting as a small compile-time optimization. |
| 438 | Ranges.addInst(0, StartInst); |
| 439 | |
| 440 | // If we create any memsets, we put it right before the first instruction that |
| 441 | // isn't part of the memset block. This ensure that the memset is dominated |
| 442 | // by any addressing instruction needed by the start of the block. |
| 443 | IRBuilder<> Builder(BI); |
| 444 | |
| 445 | // Now that we have full information about ranges, loop over the ranges and |
| 446 | // emit memset's for anything big enough to be worthwhile. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 447 | Instruction *AMemSet = nullptr; |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 448 | for (MemsetRanges::const_iterator I = Ranges.begin(), E = Ranges.end(); |
| 449 | I != E; ++I) { |
| 450 | const MemsetRange &Range = *I; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 451 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 452 | if (Range.TheStores.size() == 1) continue; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 453 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 454 | // If it is profitable to lower this range to memset, do so now. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 455 | if (!Range.isProfitableToUseMemset(*DL)) |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 456 | continue; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 457 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 458 | // Otherwise, we do want to transform this! Create a new memset. |
| 459 | // Get the starting pointer of the block. |
| 460 | StartPtr = Range.StartPtr; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 461 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 462 | // Determine alignment |
| 463 | unsigned Alignment = Range.Alignment; |
| 464 | if (Alignment == 0) { |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 465 | Type *EltType = |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 466 | cast<PointerType>(StartPtr->getType())->getElementType(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 467 | Alignment = DL->getABITypeAlignment(EltType); |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 468 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 469 | |
| 470 | AMemSet = |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 471 | Builder.CreateMemSet(StartPtr, ByteVal, Range.End-Range.Start, Alignment); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 472 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 473 | DEBUG(dbgs() << "Replace stores:\n"; |
| 474 | for (unsigned i = 0, e = Range.TheStores.size(); i != e; ++i) |
| 475 | dbgs() << *Range.TheStores[i] << '\n'; |
| 476 | dbgs() << "With: " << *AMemSet << '\n'); |
Devang Patel | c7e4fa7 | 2011-05-04 21:58:58 +0000 | [diff] [blame] | 477 | |
| 478 | if (!Range.TheStores.empty()) |
| 479 | AMemSet->setDebugLoc(Range.TheStores[0]->getDebugLoc()); |
| 480 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 481 | // Zap all the stores. |
Craig Topper | 31ee586 | 2013-07-03 15:07:05 +0000 | [diff] [blame] | 482 | for (SmallVectorImpl<Instruction *>::const_iterator |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 483 | SI = Range.TheStores.begin(), |
Chris Lattner | 7d6433a | 2011-01-08 22:19:21 +0000 | [diff] [blame] | 484 | SE = Range.TheStores.end(); SI != SE; ++SI) { |
| 485 | MD->removeInstruction(*SI); |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 486 | (*SI)->eraseFromParent(); |
Chris Lattner | 7d6433a | 2011-01-08 22:19:21 +0000 | [diff] [blame] | 487 | } |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 488 | ++NumMemSetInfer; |
| 489 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 490 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 491 | return AMemSet; |
| 492 | } |
| 493 | |
| 494 | |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 495 | bool MemCpyOpt::processStore(StoreInst *SI, BasicBlock::iterator &BBI) { |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 496 | if (!SI->isSimple()) return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 497 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 498 | if (!DL) return false; |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 499 | |
| 500 | // Detect cases where we're performing call slot forwarding, but |
| 501 | // happen to be using a load-store pair to implement it, rather than |
| 502 | // a memcpy. |
| 503 | if (LoadInst *LI = dyn_cast<LoadInst>(SI->getOperand(0))) { |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 504 | if (LI->isSimple() && LI->hasOneUse() && |
Eli Friedman | e8bbc10 | 2011-06-15 01:25:56 +0000 | [diff] [blame] | 505 | LI->getParent() == SI->getParent()) { |
Eli Friedman | 5da0ff4 | 2011-06-02 21:24:42 +0000 | [diff] [blame] | 506 | MemDepResult ldep = MD->getDependency(LI); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 507 | CallInst *C = nullptr; |
Eli Friedman | 5da0ff4 | 2011-06-02 21:24:42 +0000 | [diff] [blame] | 508 | if (ldep.isClobber() && !isa<MemCpyInst>(ldep.getInst())) |
| 509 | C = dyn_cast<CallInst>(ldep.getInst()); |
| 510 | |
| 511 | if (C) { |
| 512 | // Check that nothing touches the dest of the "copy" between |
| 513 | // the call and the store. |
Eli Friedman | e8bbc10 | 2011-06-15 01:25:56 +0000 | [diff] [blame] | 514 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
| 515 | AliasAnalysis::Location StoreLoc = AA.getLocation(SI); |
| 516 | for (BasicBlock::iterator I = --BasicBlock::iterator(SI), |
| 517 | E = C; I != E; --I) { |
| 518 | if (AA.getModRefInfo(&*I, StoreLoc) != AliasAnalysis::NoModRef) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 519 | C = nullptr; |
Eli Friedman | e8bbc10 | 2011-06-15 01:25:56 +0000 | [diff] [blame] | 520 | break; |
| 521 | } |
Eli Friedman | 5da0ff4 | 2011-06-02 21:24:42 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 525 | if (C) { |
Duncan Sands | c6ada69 | 2012-10-04 10:54:40 +0000 | [diff] [blame] | 526 | unsigned storeAlign = SI->getAlignment(); |
| 527 | if (!storeAlign) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 528 | storeAlign = DL->getABITypeAlignment(SI->getOperand(0)->getType()); |
Duncan Sands | c6ada69 | 2012-10-04 10:54:40 +0000 | [diff] [blame] | 529 | unsigned loadAlign = LI->getAlignment(); |
| 530 | if (!loadAlign) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 531 | loadAlign = DL->getABITypeAlignment(LI->getType()); |
Duncan Sands | c6ada69 | 2012-10-04 10:54:40 +0000 | [diff] [blame] | 532 | |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 533 | bool changed = performCallSlotOptzn(LI, |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 534 | SI->getPointerOperand()->stripPointerCasts(), |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 535 | LI->getPointerOperand()->stripPointerCasts(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 536 | DL->getTypeStoreSize(SI->getOperand(0)->getType()), |
Duncan Sands | c6ada69 | 2012-10-04 10:54:40 +0000 | [diff] [blame] | 537 | std::min(storeAlign, loadAlign), C); |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 538 | if (changed) { |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 539 | MD->removeInstruction(SI); |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 540 | SI->eraseFromParent(); |
Chris Lattner | caf5c0d | 2011-01-09 19:26:10 +0000 | [diff] [blame] | 541 | MD->removeInstruction(LI); |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 542 | LI->eraseFromParent(); |
| 543 | ++NumMemCpyInstr; |
| 544 | return true; |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 549 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 550 | // There are two cases that are interesting for this code to handle: memcpy |
| 551 | // and memset. Right now we only handle memset. |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 552 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 553 | // Ensure that the value being stored is something that can be memset'able a |
| 554 | // byte at a time like "0" or "-1" or any width, as well as things like |
| 555 | // 0xA0A0A0A0 and 0.0. |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 556 | if (Value *ByteVal = isBytewiseValue(SI->getOperand(0))) |
| 557 | if (Instruction *I = tryMergingIntoMemset(SI, SI->getPointerOperand(), |
| 558 | ByteVal)) { |
| 559 | BBI = I; // Don't invalidate iterator. |
| 560 | return true; |
Mon P Wang | c576ee9 | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 561 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 562 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 563 | return false; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Chris Lattner | 9a1d63b | 2011-01-08 21:19:19 +0000 | [diff] [blame] | 566 | bool MemCpyOpt::processMemSet(MemSetInst *MSI, BasicBlock::iterator &BBI) { |
| 567 | // See if there is another memset or store neighboring this memset which |
| 568 | // allows us to widen out the memset to do a single larger store. |
Chris Lattner | ff6ed2a | 2011-01-08 22:11:56 +0000 | [diff] [blame] | 569 | if (isa<ConstantInt>(MSI->getLength()) && !MSI->isVolatile()) |
| 570 | if (Instruction *I = tryMergingIntoMemset(MSI, MSI->getDest(), |
| 571 | MSI->getValue())) { |
| 572 | BBI = I; // Don't invalidate iterator. |
| 573 | return true; |
| 574 | } |
Chris Lattner | 9a1d63b | 2011-01-08 21:19:19 +0000 | [diff] [blame] | 575 | return false; |
| 576 | } |
| 577 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 578 | |
| 579 | /// performCallSlotOptzn - takes a memcpy and a call that it depends on, |
| 580 | /// and checks for the possibility of a call slot optimization by having |
| 581 | /// the call write its result directly into the destination of the memcpy. |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 582 | bool MemCpyOpt::performCallSlotOptzn(Instruction *cpy, |
| 583 | Value *cpyDest, Value *cpySrc, |
Duncan Sands | c6ada69 | 2012-10-04 10:54:40 +0000 | [diff] [blame] | 584 | uint64_t cpyLen, unsigned cpyAlign, |
| 585 | CallInst *C) { |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 586 | // The general transformation to keep in mind is |
| 587 | // |
| 588 | // call @func(..., src, ...) |
| 589 | // memcpy(dest, src, ...) |
| 590 | // |
| 591 | // -> |
| 592 | // |
| 593 | // memcpy(dest, src, ...) |
| 594 | // call @func(..., dest, ...) |
| 595 | // |
| 596 | // Since moving the memcpy is technically awkward, we additionally check that |
| 597 | // src only holds uninitialized values at the moment of the call, meaning that |
| 598 | // the memcpy can be discarded rather than moved. |
| 599 | |
| 600 | // Deliberately get the source and destination with bitcasts stripped away, |
| 601 | // because we'll need to do type comparisons based on the underlying type. |
Gabor Greif | 62f0aac | 2010-07-28 22:50:26 +0000 | [diff] [blame] | 602 | CallSite CS(C); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 603 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 604 | // Require that src be an alloca. This simplifies the reasoning considerably. |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 605 | AllocaInst *srcAlloca = dyn_cast<AllocaInst>(cpySrc); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 606 | if (!srcAlloca) |
| 607 | return false; |
| 608 | |
| 609 | // Check that all of src is copied to dest. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 610 | if (!DL) return false; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 611 | |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 612 | ConstantInt *srcArraySize = dyn_cast<ConstantInt>(srcAlloca->getArraySize()); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 613 | if (!srcArraySize) |
| 614 | return false; |
| 615 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 616 | uint64_t srcSize = DL->getTypeAllocSize(srcAlloca->getAllocatedType()) * |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 617 | srcArraySize->getZExtValue(); |
| 618 | |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 619 | if (cpyLen < srcSize) |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 620 | return false; |
| 621 | |
| 622 | // Check that accessing the first srcSize bytes of dest will not cause a |
| 623 | // trap. Otherwise the transform is invalid since it might cause a trap |
| 624 | // to occur earlier than it otherwise would. |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 625 | if (AllocaInst *A = dyn_cast<AllocaInst>(cpyDest)) { |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 626 | // The destination is an alloca. Check it is larger than srcSize. |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 627 | ConstantInt *destArraySize = dyn_cast<ConstantInt>(A->getArraySize()); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 628 | if (!destArraySize) |
| 629 | return false; |
| 630 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 631 | uint64_t destSize = DL->getTypeAllocSize(A->getAllocatedType()) * |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 632 | destArraySize->getZExtValue(); |
| 633 | |
| 634 | if (destSize < srcSize) |
| 635 | return false; |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 636 | } else if (Argument *A = dyn_cast<Argument>(cpyDest)) { |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 637 | // If the destination is an sret parameter then only accesses that are |
| 638 | // outside of the returned struct type can trap. |
| 639 | if (!A->hasStructRetAttr()) |
| 640 | return false; |
| 641 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 642 | Type *StructTy = cast<PointerType>(A->getType())->getElementType(); |
Shuxin Yang | 140d592 | 2013-06-08 04:56:05 +0000 | [diff] [blame] | 643 | if (!StructTy->isSized()) { |
| 644 | // The call may never return and hence the copy-instruction may never |
| 645 | // be executed, and therefore it's not safe to say "the destination |
| 646 | // has at least <cpyLen> bytes, as implied by the copy-instruction", |
| 647 | return false; |
| 648 | } |
| 649 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 650 | uint64_t destSize = DL->getTypeAllocSize(StructTy); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 651 | if (destSize < srcSize) |
| 652 | return false; |
| 653 | } else { |
| 654 | return false; |
| 655 | } |
| 656 | |
Duncan Sands | 933db77 | 2012-10-05 07:29:46 +0000 | [diff] [blame] | 657 | // Check that dest points to memory that is at least as aligned as src. |
| 658 | unsigned srcAlign = srcAlloca->getAlignment(); |
| 659 | if (!srcAlign) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 660 | srcAlign = DL->getABITypeAlignment(srcAlloca->getAllocatedType()); |
Duncan Sands | 933db77 | 2012-10-05 07:29:46 +0000 | [diff] [blame] | 661 | bool isDestSufficientlyAligned = srcAlign <= cpyAlign; |
| 662 | // If dest is not aligned enough and we can't increase its alignment then |
| 663 | // bail out. |
| 664 | if (!isDestSufficientlyAligned && !isa<AllocaInst>(cpyDest)) |
| 665 | return false; |
| 666 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 667 | // Check that src is not accessed except via the call and the memcpy. This |
| 668 | // guarantees that it holds only undefined values when passed in (so the final |
| 669 | // memcpy can be dropped), that it is not read or written between the call and |
| 670 | // the memcpy, and that writing beyond the end of it is undefined. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 671 | SmallVector<User*, 8> srcUseList(srcAlloca->user_begin(), |
| 672 | srcAlloca->user_end()); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 673 | while (!srcUseList.empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 674 | User *U = srcUseList.pop_back_val(); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 675 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 676 | if (isa<BitCastInst>(U) || isa<AddrSpaceCastInst>(U)) { |
| 677 | for (User *UU : U->users()) |
| 678 | srcUseList.push_back(UU); |
Chandler Carruth | 18cee1d | 2014-09-01 10:09:18 +0000 | [diff] [blame] | 679 | continue; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 680 | } |
Chandler Carruth | 18cee1d | 2014-09-01 10:09:18 +0000 | [diff] [blame] | 681 | if (GetElementPtrInst *G = dyn_cast<GetElementPtrInst>(U)) { |
| 682 | if (!G->hasAllZeroIndices()) |
| 683 | return false; |
| 684 | |
| 685 | for (User *UU : U->users()) |
| 686 | srcUseList.push_back(UU); |
| 687 | continue; |
| 688 | } |
| 689 | if (const IntrinsicInst *IT = dyn_cast<IntrinsicInst>(U)) |
| 690 | if (IT->getIntrinsicID() == Intrinsic::lifetime_start || |
| 691 | IT->getIntrinsicID() == Intrinsic::lifetime_end) |
| 692 | continue; |
| 693 | |
| 694 | if (U != C && U != cpy) |
| 695 | return false; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Nick Lewycky | 703e488 | 2014-07-14 18:52:02 +0000 | [diff] [blame] | 698 | // Check that src isn't captured by the called function since the |
| 699 | // transformation can cause aliasing issues in that case. |
| 700 | for (unsigned i = 0, e = CS.arg_size(); i != e; ++i) |
| 701 | if (CS.getArgument(i) == cpySrc && !CS.doesNotCapture(i)) |
| 702 | return false; |
| 703 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 704 | // Since we're changing the parameter to the callsite, we need to make sure |
| 705 | // that what would be the new parameter dominates the callsite. |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 706 | DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 707 | if (Instruction *cpyDestInst = dyn_cast<Instruction>(cpyDest)) |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 708 | if (!DT.dominates(cpyDestInst, C)) |
| 709 | return false; |
| 710 | |
| 711 | // In addition to knowing that the call does not access src in some |
| 712 | // unexpected manner, for example via a global, which we deduce from |
| 713 | // the use analysis, we also need to know that it does not sneakily |
| 714 | // access dest. We rely on AA to figure this out for us. |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 715 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 716 | AliasAnalysis::ModRefResult MR = AA.getModRefInfo(C, cpyDest, srcSize); |
| 717 | // If necessary, perform additional analysis. |
| 718 | if (MR != AliasAnalysis::NoModRef) |
| 719 | MR = AA.callCapturesBefore(C, cpyDest, srcSize, &DT); |
| 720 | if (MR != AliasAnalysis::NoModRef) |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 721 | return false; |
| 722 | |
| 723 | // All the checks have passed, so do the transformation. |
Owen Anderson | d071a87 | 2008-06-01 21:52:16 +0000 | [diff] [blame] | 724 | bool changedArgument = false; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 725 | for (unsigned i = 0; i < CS.arg_size(); ++i) |
Owen Anderson | 38099c1 | 2008-06-01 22:26:26 +0000 | [diff] [blame] | 726 | if (CS.getArgument(i)->stripPointerCasts() == cpySrc) { |
Duncan Sands | a6d2001 | 2012-10-04 13:53:21 +0000 | [diff] [blame] | 727 | Value *Dest = cpySrc->getType() == cpyDest->getType() ? cpyDest |
| 728 | : CastInst::CreatePointerCast(cpyDest, cpySrc->getType(), |
| 729 | cpyDest->getName(), C); |
Owen Anderson | d071a87 | 2008-06-01 21:52:16 +0000 | [diff] [blame] | 730 | changedArgument = true; |
Duncan Sands | a6d2001 | 2012-10-04 13:53:21 +0000 | [diff] [blame] | 731 | if (CS.getArgument(i)->getType() == Dest->getType()) |
| 732 | CS.setArgument(i, Dest); |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 733 | else |
Duncan Sands | a6d2001 | 2012-10-04 13:53:21 +0000 | [diff] [blame] | 734 | CS.setArgument(i, CastInst::CreatePointerCast(Dest, |
| 735 | CS.getArgument(i)->getType(), Dest->getName(), C)); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Owen Anderson | d071a87 | 2008-06-01 21:52:16 +0000 | [diff] [blame] | 738 | if (!changedArgument) |
| 739 | return false; |
| 740 | |
Duncan Sands | c6ada69 | 2012-10-04 10:54:40 +0000 | [diff] [blame] | 741 | // If the destination wasn't sufficiently aligned then increase its alignment. |
| 742 | if (!isDestSufficientlyAligned) { |
| 743 | assert(isa<AllocaInst>(cpyDest) && "Can only increase alloca alignment!"); |
| 744 | cast<AllocaInst>(cpyDest)->setAlignment(srcAlign); |
| 745 | } |
| 746 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 747 | // Drop any cached information about the call, because we may have changed |
| 748 | // its dependence information by changing its parameter. |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 749 | MD->removeInstruction(C); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 750 | |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 751 | // Remove the memcpy. |
| 752 | MD->removeInstruction(cpy); |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 753 | ++NumMemCpyInstr; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 754 | |
| 755 | return true; |
| 756 | } |
| 757 | |
Chris Lattner | 7e9b2ea | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 758 | /// processMemCpyMemCpyDependence - We've found that the (upward scanning) |
| 759 | /// memory dependence of memcpy 'M' is the memcpy 'MDep'. Try to simplify M to |
| 760 | /// copy from MDep's input if we can. MSize is the size of M's copy. |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 761 | /// |
Chris Lattner | 7e9b2ea | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 762 | bool MemCpyOpt::processMemCpyMemCpyDependence(MemCpyInst *M, MemCpyInst *MDep, |
| 763 | uint64_t MSize) { |
| 764 | // We can only transforms memcpy's where the dest of one is the source of the |
| 765 | // other. |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 766 | if (M->getSource() != MDep->getDest() || MDep->isVolatile()) |
Chris Lattner | 7e9b2ea | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 767 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 768 | |
Chris Lattner | fd51c52 | 2010-12-09 07:39:50 +0000 | [diff] [blame] | 769 | // If dep instruction is reading from our current input, then it is a noop |
| 770 | // transfer and substituting the input won't change this instruction. Just |
| 771 | // ignore the input and let someone else zap MDep. This handles cases like: |
| 772 | // memcpy(a <- a) |
| 773 | // memcpy(b <- a) |
| 774 | if (M->getSource() == MDep->getSource()) |
| 775 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 776 | |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 777 | // Second, the length of the memcpy's must be the same, or the preceding one |
Chris Lattner | 7e9b2ea | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 778 | // must be larger than the following one. |
Dan Gohman | 19e30d5 | 2011-01-21 22:07:57 +0000 | [diff] [blame] | 779 | ConstantInt *MDepLen = dyn_cast<ConstantInt>(MDep->getLength()); |
| 780 | ConstantInt *MLen = dyn_cast<ConstantInt>(M->getLength()); |
| 781 | if (!MDepLen || !MLen || MDepLen->getZExtValue() < MLen->getZExtValue()) |
| 782 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 783 | |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 784 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
Chris Lattner | 5957229 | 2010-11-21 08:06:10 +0000 | [diff] [blame] | 785 | |
| 786 | // Verify that the copied-from memory doesn't change in between the two |
| 787 | // transfers. For example, in: |
| 788 | // memcpy(a <- b) |
| 789 | // *b = 42; |
| 790 | // memcpy(c <- a) |
| 791 | // It would be invalid to transform the second memcpy into memcpy(c <- b). |
| 792 | // |
| 793 | // TODO: If the code between M and MDep is transparent to the destination "c", |
| 794 | // then we could still perform the xform by moving M up to the first memcpy. |
| 795 | // |
| 796 | // NOTE: This is conservative, it will stop on any read from the source loc, |
| 797 | // not just the defining memcpy. |
| 798 | MemDepResult SourceDep = |
| 799 | MD->getPointerDependencyFrom(AA.getLocationForSource(MDep), |
| 800 | false, M, M->getParent()); |
| 801 | if (!SourceDep.isClobber() || SourceDep.getInst() != MDep) |
| 802 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 803 | |
Chris Lattner | 731caac | 2010-11-18 08:00:57 +0000 | [diff] [blame] | 804 | // If the dest of the second might alias the source of the first, then the |
| 805 | // source and dest might overlap. We still want to eliminate the intermediate |
| 806 | // value, but we have to generate a memmove instead of memcpy. |
Chris Lattner | 6cf8d6c | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 807 | bool UseMemMove = false; |
| 808 | if (!AA.isNoAlias(AA.getLocationForDest(M), AA.getLocationForSource(MDep))) |
| 809 | UseMemMove = true; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 810 | |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 811 | // If all checks passed, then we can transform M. |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 812 | |
Chris Lattner | 7e9b2ea | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 813 | // Make sure to use the lesser of the alignment of the source and the dest |
| 814 | // since we're changing where we're reading from, but don't want to increase |
| 815 | // the alignment past what can be read from or written to. |
| 816 | // TODO: Is this worth it if we're creating a less aligned memcpy? For |
| 817 | // example we could be moving from movaps -> movq on x86. |
Chris Lattner | 1385dff | 2010-11-18 08:07:09 +0000 | [diff] [blame] | 818 | unsigned Align = std::min(MDep->getAlignment(), M->getAlignment()); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 819 | |
Chris Lattner | 6cf8d6c | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 820 | IRBuilder<> Builder(M); |
| 821 | if (UseMemMove) |
| 822 | Builder.CreateMemMove(M->getRawDest(), MDep->getRawSource(), M->getLength(), |
| 823 | Align, M->isVolatile()); |
| 824 | else |
| 825 | Builder.CreateMemCpy(M->getRawDest(), MDep->getRawSource(), M->getLength(), |
| 826 | Align, M->isVolatile()); |
Chris Lattner | 1385dff | 2010-11-18 08:07:09 +0000 | [diff] [blame] | 827 | |
Chris Lattner | 5957229 | 2010-11-21 08:06:10 +0000 | [diff] [blame] | 828 | // Remove the instruction we're replacing. |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 829 | MD->removeInstruction(M); |
Chris Lattner | 1385dff | 2010-11-18 08:07:09 +0000 | [diff] [blame] | 830 | M->eraseFromParent(); |
| 831 | ++NumMemCpyInstr; |
| 832 | return true; |
Chris Lattner | 7e9b2ea | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | |
Gabor Greif | 62f0aac | 2010-07-28 22:50:26 +0000 | [diff] [blame] | 836 | /// processMemCpy - perform simplification of memcpy's. If we have memcpy A |
| 837 | /// which copies X to Y, and memcpy B which copies Y to Z, then we can rewrite |
| 838 | /// B to be a memcpy from X to Z (or potentially a memmove, depending on |
| 839 | /// circumstances). This allows later passes to remove the first memcpy |
| 840 | /// altogether. |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 841 | bool MemCpyOpt::processMemCpy(MemCpyInst *M) { |
Nick Lewycky | 00703e7 | 2014-02-04 00:18:54 +0000 | [diff] [blame] | 842 | // We can only optimize non-volatile memcpy's. |
| 843 | if (M->isVolatile()) return false; |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 844 | |
Chris Lattner | bc4457e | 2010-12-09 07:45:45 +0000 | [diff] [blame] | 845 | // If the source and destination of the memcpy are the same, then zap it. |
| 846 | if (M->getSource() == M->getDest()) { |
| 847 | MD->removeInstruction(M); |
| 848 | M->eraseFromParent(); |
| 849 | return false; |
| 850 | } |
Benjamin Kramer | ea9152e | 2010-12-24 21:17:12 +0000 | [diff] [blame] | 851 | |
| 852 | // If copying from a constant, try to turn the memcpy into a memset. |
Benjamin Kramer | b90b2f0 | 2010-12-24 22:23:59 +0000 | [diff] [blame] | 853 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(M->getSource())) |
Benjamin Kramer | 30342fb | 2010-12-26 15:23:45 +0000 | [diff] [blame] | 854 | if (GV->isConstant() && GV->hasDefinitiveInitializer()) |
Benjamin Kramer | b90b2f0 | 2010-12-24 22:23:59 +0000 | [diff] [blame] | 855 | if (Value *ByteVal = isBytewiseValue(GV->getInitializer())) { |
Chris Lattner | 6cf8d6c | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 856 | IRBuilder<> Builder(M); |
Nick Lewycky | 00703e7 | 2014-02-04 00:18:54 +0000 | [diff] [blame] | 857 | Builder.CreateMemSet(M->getRawDest(), ByteVal, M->getLength(), |
Chris Lattner | 6cf8d6c | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 858 | M->getAlignment(), false); |
Benjamin Kramer | b90b2f0 | 2010-12-24 22:23:59 +0000 | [diff] [blame] | 859 | MD->removeInstruction(M); |
| 860 | M->eraseFromParent(); |
| 861 | ++NumCpyToSet; |
| 862 | return true; |
| 863 | } |
Benjamin Kramer | ea9152e | 2010-12-24 21:17:12 +0000 | [diff] [blame] | 864 | |
Nick Lewycky | 00703e7 | 2014-02-04 00:18:54 +0000 | [diff] [blame] | 865 | // The optimizations after this point require the memcpy size. |
| 866 | ConstantInt *CopySize = dyn_cast<ConstantInt>(M->getLength()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 867 | if (!CopySize) return false; |
Nick Lewycky | 00703e7 | 2014-02-04 00:18:54 +0000 | [diff] [blame] | 868 | |
Nick Lewycky | 9938494 | 2014-02-06 06:29:19 +0000 | [diff] [blame] | 869 | // The are three possible optimizations we can do for memcpy: |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 870 | // a) memcpy-memcpy xform which exposes redundance for DSE. |
| 871 | // b) call-memcpy xform for return slot optimization. |
Nick Lewycky | 77d5fb4 | 2014-03-26 23:45:15 +0000 | [diff] [blame] | 872 | // c) memcpy from freshly alloca'd space or space that has just started its |
| 873 | // lifetime copies undefined data, and we can therefore eliminate the |
| 874 | // memcpy in favor of the data that was already at the destination. |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 875 | MemDepResult DepInfo = MD->getDependency(M); |
Nick Lewycky | 0a7e9cc | 2011-10-16 20:13:32 +0000 | [diff] [blame] | 876 | if (DepInfo.isClobber()) { |
| 877 | if (CallInst *C = dyn_cast<CallInst>(DepInfo.getInst())) { |
| 878 | if (performCallSlotOptzn(M, M->getDest(), M->getSource(), |
Duncan Sands | c6ada69 | 2012-10-04 10:54:40 +0000 | [diff] [blame] | 879 | CopySize->getZExtValue(), M->getAlignment(), |
| 880 | C)) { |
Nick Lewycky | 0a7e9cc | 2011-10-16 20:13:32 +0000 | [diff] [blame] | 881 | MD->removeInstruction(M); |
| 882 | M->eraseFromParent(); |
| 883 | return true; |
| 884 | } |
Chris Lattner | bc4457e | 2010-12-09 07:45:45 +0000 | [diff] [blame] | 885 | } |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 886 | } |
Ahmed Charles | 32e983e | 2012-02-13 06:30:56 +0000 | [diff] [blame] | 887 | |
| 888 | AliasAnalysis::Location SrcLoc = AliasAnalysis::getLocationForSource(M); |
Nick Lewycky | 0a7e9cc | 2011-10-16 20:13:32 +0000 | [diff] [blame] | 889 | MemDepResult SrcDepInfo = MD->getPointerDependencyFrom(SrcLoc, true, |
| 890 | M, M->getParent()); |
| 891 | if (SrcDepInfo.isClobber()) { |
| 892 | if (MemCpyInst *MDep = dyn_cast<MemCpyInst>(SrcDepInfo.getInst())) |
| 893 | return processMemCpyMemCpyDependence(M, MDep, CopySize->getZExtValue()); |
Nick Lewycky | 9938494 | 2014-02-06 06:29:19 +0000 | [diff] [blame] | 894 | } else if (SrcDepInfo.isDef()) { |
Nick Lewycky | 77d5fb4 | 2014-03-26 23:45:15 +0000 | [diff] [blame] | 895 | Instruction *I = SrcDepInfo.getInst(); |
| 896 | bool hasUndefContents = false; |
| 897 | |
| 898 | if (isa<AllocaInst>(I)) { |
| 899 | hasUndefContents = true; |
| 900 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 901 | if (II->getIntrinsicID() == Intrinsic::lifetime_start) |
| 902 | if (ConstantInt *LTSize = dyn_cast<ConstantInt>(II->getArgOperand(0))) |
| 903 | if (LTSize->getZExtValue() >= CopySize->getZExtValue()) |
| 904 | hasUndefContents = true; |
| 905 | } |
| 906 | |
| 907 | if (hasUndefContents) { |
Nick Lewycky | 9938494 | 2014-02-06 06:29:19 +0000 | [diff] [blame] | 908 | MD->removeInstruction(M); |
| 909 | M->eraseFromParent(); |
| 910 | ++NumMemCpyInstr; |
| 911 | return true; |
| 912 | } |
Nick Lewycky | 0a7e9cc | 2011-10-16 20:13:32 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Owen Anderson | ad5367f | 2008-04-29 21:51:00 +0000 | [diff] [blame] | 915 | return false; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Chris Lattner | 1145e33 | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 918 | /// processMemMove - Transforms memmove calls to memcpy calls when the src/dst |
| 919 | /// are guaranteed not to alias. |
| 920 | bool MemCpyOpt::processMemMove(MemMoveInst *M) { |
| 921 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
| 922 | |
Chris Lattner | 23f61a0 | 2011-05-01 18:27:11 +0000 | [diff] [blame] | 923 | if (!TLI->has(LibFunc::memmove)) |
| 924 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 925 | |
Chris Lattner | 1145e33 | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 926 | // See if the pointers alias. |
Chris Lattner | 6cf8d6c | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 927 | if (!AA.isNoAlias(AA.getLocationForDest(M), AA.getLocationForSource(M))) |
Chris Lattner | 1145e33 | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 928 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 929 | |
David Greene | 2419923 | 2010-01-05 01:27:47 +0000 | [diff] [blame] | 930 | DEBUG(dbgs() << "MemCpyOpt: Optimizing memmove -> memcpy: " << *M << "\n"); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 931 | |
Chris Lattner | 1145e33 | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 932 | // If not, then we know we can transform this. |
| 933 | Module *Mod = M->getParent()->getParent()->getParent(); |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 934 | Type *ArgTys[3] = { M->getRawDest()->getType(), |
| 935 | M->getRawSource()->getType(), |
| 936 | M->getLength()->getType() }; |
Gabor Greif | 3e44ea1 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 937 | M->setCalledFunction(Intrinsic::getDeclaration(Mod, Intrinsic::memcpy, |
Benjamin Kramer | e6e1933 | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 938 | ArgTys)); |
Duncan Sands | 0edc710 | 2009-09-03 13:37:16 +0000 | [diff] [blame] | 939 | |
Chris Lattner | 1145e33 | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 940 | // MemDep may have over conservative information about this instruction, just |
| 941 | // conservatively flush it from the cache. |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 942 | MD->removeInstruction(M); |
Duncan Sands | 0edc710 | 2009-09-03 13:37:16 +0000 | [diff] [blame] | 943 | |
| 944 | ++NumMoveToCpy; |
Chris Lattner | 1145e33 | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 945 | return true; |
| 946 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 947 | |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 948 | /// processByValArgument - This is called on every byval argument in call sites. |
| 949 | bool MemCpyOpt::processByValArgument(CallSite CS, unsigned ArgNo) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 950 | if (!DL) return false; |
Chris Lattner | 1145e33 | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 951 | |
Chris Lattner | 5957229 | 2010-11-21 08:06:10 +0000 | [diff] [blame] | 952 | // Find out what feeds this byval argument. |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 953 | Value *ByValArg = CS.getArgument(ArgNo); |
Nick Lewycky | c585de6 | 2011-10-12 00:14:31 +0000 | [diff] [blame] | 954 | Type *ByValTy = cast<PointerType>(ByValArg->getType())->getElementType(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 955 | uint64_t ByValSize = DL->getTypeAllocSize(ByValTy); |
Chris Lattner | 5957229 | 2010-11-21 08:06:10 +0000 | [diff] [blame] | 956 | MemDepResult DepInfo = |
| 957 | MD->getPointerDependencyFrom(AliasAnalysis::Location(ByValArg, ByValSize), |
| 958 | true, CS.getInstruction(), |
| 959 | CS.getInstruction()->getParent()); |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 960 | if (!DepInfo.isClobber()) |
| 961 | return false; |
| 962 | |
| 963 | // If the byval argument isn't fed by a memcpy, ignore it. If it is fed by |
| 964 | // a memcpy, see if we can byval from the source of the memcpy instead of the |
| 965 | // result. |
| 966 | MemCpyInst *MDep = dyn_cast<MemCpyInst>(DepInfo.getInst()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 967 | if (!MDep || MDep->isVolatile() || |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 968 | ByValArg->stripPointerCasts() != MDep->getDest()) |
| 969 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 970 | |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 971 | // The length of the memcpy must be larger or equal to the size of the byval. |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 972 | ConstantInt *C1 = dyn_cast<ConstantInt>(MDep->getLength()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 973 | if (!C1 || C1->getValue().getZExtValue() < ByValSize) |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 974 | return false; |
| 975 | |
Chris Lattner | 83791ce | 2011-05-23 00:03:39 +0000 | [diff] [blame] | 976 | // Get the alignment of the byval. If the call doesn't specify the alignment, |
| 977 | // then it is some target specific value that we can't know. |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 978 | unsigned ByValAlign = CS.getParamAlignment(ArgNo+1); |
Chris Lattner | 83791ce | 2011-05-23 00:03:39 +0000 | [diff] [blame] | 979 | if (ByValAlign == 0) return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 980 | |
Chris Lattner | 83791ce | 2011-05-23 00:03:39 +0000 | [diff] [blame] | 981 | // If it is greater than the memcpy, then we check to see if we can force the |
| 982 | // source of the memcpy to the alignment we need. If we fail, we bail out. |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame^] | 983 | AssumptionTracker *AT = &getAnalysis<AssumptionTracker>(); |
| 984 | DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Chris Lattner | 83791ce | 2011-05-23 00:03:39 +0000 | [diff] [blame] | 985 | if (MDep->getAlignment() < ByValAlign && |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame^] | 986 | getOrEnforceKnownAlignment(MDep->getSource(),ByValAlign, |
| 987 | DL, AT, CS.getInstruction(), &DT) < ByValAlign) |
Chris Lattner | 83791ce | 2011-05-23 00:03:39 +0000 | [diff] [blame] | 988 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 989 | |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 990 | // Verify that the copied-from memory doesn't change in between the memcpy and |
| 991 | // the byval call. |
| 992 | // memcpy(a <- b) |
| 993 | // *b = 42; |
| 994 | // foo(*a) |
| 995 | // It would be invalid to transform the second memcpy into foo(*b). |
Chris Lattner | 5957229 | 2010-11-21 08:06:10 +0000 | [diff] [blame] | 996 | // |
| 997 | // NOTE: This is conservative, it will stop on any read from the source loc, |
| 998 | // not just the defining memcpy. |
| 999 | MemDepResult SourceDep = |
| 1000 | MD->getPointerDependencyFrom(AliasAnalysis::getLocationForSource(MDep), |
| 1001 | false, CS.getInstruction(), MDep->getParent()); |
| 1002 | if (!SourceDep.isClobber() || SourceDep.getInst() != MDep) |
| 1003 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1004 | |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1005 | Value *TmpCast = MDep->getSource(); |
| 1006 | if (MDep->getSource()->getType() != ByValArg->getType()) |
| 1007 | TmpCast = new BitCastInst(MDep->getSource(), ByValArg->getType(), |
| 1008 | "tmpcast", CS.getInstruction()); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1009 | |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1010 | DEBUG(dbgs() << "MemCpyOpt: Forwarding memcpy to byval:\n" |
| 1011 | << " " << *MDep << "\n" |
| 1012 | << " " << *CS.getInstruction() << "\n"); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1013 | |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1014 | // Otherwise we're good! Update the byval argument. |
| 1015 | CS.setArgument(ArgNo, TmpCast); |
| 1016 | ++NumMemCpyInstr; |
| 1017 | return true; |
| 1018 | } |
| 1019 | |
| 1020 | /// iterateOnFunction - Executes one iteration of MemCpyOpt. |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 1021 | bool MemCpyOpt::iterateOnFunction(Function &F) { |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1022 | bool MadeChange = false; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 1023 | |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1024 | // Walk all instruction in the function. |
Owen Anderson | 6a7355c | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 1025 | for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) { |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1026 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1027 | // Avoid invalidating the iterator. |
| 1028 | Instruction *I = BI++; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1029 | |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1030 | bool RepeatInstruction = false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1031 | |
Owen Anderson | 6a7355c | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 1032 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1033 | MadeChange |= processStore(SI, BI); |
Chris Lattner | 9a1d63b | 2011-01-08 21:19:19 +0000 | [diff] [blame] | 1034 | else if (MemSetInst *M = dyn_cast<MemSetInst>(I)) |
| 1035 | RepeatInstruction = processMemSet(M, BI); |
| 1036 | else if (MemCpyInst *M = dyn_cast<MemCpyInst>(I)) |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1037 | RepeatInstruction = processMemCpy(M); |
Chris Lattner | 9a1d63b | 2011-01-08 21:19:19 +0000 | [diff] [blame] | 1038 | else if (MemMoveInst *M = dyn_cast<MemMoveInst>(I)) |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1039 | RepeatInstruction = processMemMove(M); |
Chris Lattner | 9a1d63b | 2011-01-08 21:19:19 +0000 | [diff] [blame] | 1040 | else if (CallSite CS = (Value*)I) { |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1041 | for (unsigned i = 0, e = CS.arg_size(); i != e; ++i) |
Nick Lewycky | 612d70b | 2011-11-20 19:09:04 +0000 | [diff] [blame] | 1042 | if (CS.isByValArgument(i)) |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1043 | MadeChange |= processByValArgument(CS, i); |
| 1044 | } |
| 1045 | |
| 1046 | // Reprocess the instruction if desired. |
| 1047 | if (RepeatInstruction) { |
Chris Lattner | 7d6433a | 2011-01-08 22:19:21 +0000 | [diff] [blame] | 1048 | if (BI != BB->begin()) --BI; |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1049 | MadeChange = true; |
Chris Lattner | 1145e33 | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 1050 | } |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 1051 | } |
| 1052 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1053 | |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1054 | return MadeChange; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 1055 | } |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1056 | |
| 1057 | // MemCpyOpt::runOnFunction - This is the main transformation entry point for a |
| 1058 | // function. |
| 1059 | // |
| 1060 | bool MemCpyOpt::runOnFunction(Function &F) { |
Paul Robinson | af4e64d | 2014-02-06 00:07:05 +0000 | [diff] [blame] | 1061 | if (skipOptnoneFunction(F)) |
| 1062 | return false; |
| 1063 | |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1064 | bool MadeChange = false; |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1065 | MD = &getAnalysis<MemoryDependenceAnalysis>(); |
Rafael Espindola | 9351251 | 2014-02-25 17:30:31 +0000 | [diff] [blame] | 1066 | DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1067 | DL = DLP ? &DLP->getDataLayout() : nullptr; |
Chris Lattner | 23f61a0 | 2011-05-01 18:27:11 +0000 | [diff] [blame] | 1068 | TLI = &getAnalysis<TargetLibraryInfo>(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1069 | |
Chris Lattner | 23f61a0 | 2011-05-01 18:27:11 +0000 | [diff] [blame] | 1070 | // If we don't have at least memset and memcpy, there is little point of doing |
| 1071 | // anything here. These are required by a freestanding implementation, so if |
| 1072 | // even they are disabled, there is no point in trying hard. |
| 1073 | if (!TLI->has(LibFunc::memset) || !TLI->has(LibFunc::memcpy)) |
| 1074 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1075 | |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1076 | while (1) { |
| 1077 | if (!iterateOnFunction(F)) |
| 1078 | break; |
| 1079 | MadeChange = true; |
| 1080 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1081 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1082 | MD = nullptr; |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1083 | return MadeChange; |
| 1084 | } |