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