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 | |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Scalar/MemCpyOptimizer.h" |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Scalar.h" |
Amaury Sechet | bdb261b | 2016-03-14 22:52:27 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseSet.h" |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 22 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/GlobalVariable.h" |
| 24 | #include "llvm/IR/IRBuilder.h" |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Chris Lattner | b25de3f | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Utils/Local.h" |
Nick Lewycky | f836c89 | 2015-07-21 21:56:26 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 31 | #define DEBUG_TYPE "memcpyopt" |
| 32 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 33 | STATISTIC(NumMemCpyInstr, "Number of memcpy instructions deleted"); |
| 34 | STATISTIC(NumMemSetInfer, "Number of memsets inferred"); |
Duncan Sands | 0edc710 | 2009-09-03 13:37:16 +0000 | [diff] [blame] | 35 | STATISTIC(NumMoveToCpy, "Number of memmoves converted to memcpy"); |
Benjamin Kramer | ea9152e | 2010-12-24 21:17:12 +0000 | [diff] [blame] | 36 | STATISTIC(NumCpyToSet, "Number of memcpys converted to memset"); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 37 | |
Benjamin Kramer | 15a257d | 2012-09-13 16:29:49 +0000 | [diff] [blame] | 38 | static int64_t GetOffsetFromIndex(const GEPOperator *GEP, unsigned Idx, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 39 | bool &VariableIdxFound, |
| 40 | const DataLayout &DL) { |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 41 | // Skip over the first indices. |
| 42 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 43 | for (unsigned i = 1; i != Idx; ++i, ++GTI) |
| 44 | /*skip along*/; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 45 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 46 | // Compute the offset implied by the rest of the indices. |
| 47 | int64_t Offset = 0; |
| 48 | for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { |
| 49 | ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 50 | if (!OpC) |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 51 | return VariableIdxFound = true; |
| 52 | if (OpC->isZero()) continue; // No offset. |
| 53 | |
| 54 | // Handle struct indices, which add their field offset to the pointer. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 55 | if (StructType *STy = dyn_cast<StructType>(*GTI)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 56 | Offset += DL.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 57 | continue; |
| 58 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 59 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 60 | // Otherwise, we have a sequential type like an array or vector. Multiply |
| 61 | // the index by the ElementSize. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 62 | uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType()); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 63 | Offset += Size*OpC->getSExtValue(); |
| 64 | } |
| 65 | |
| 66 | return Offset; |
| 67 | } |
| 68 | |
Sanjay Patel | a75c41e | 2015-08-13 22:53:20 +0000 | [diff] [blame] | 69 | /// Return true if Ptr1 is provably equal to Ptr2 plus a constant offset, and |
| 70 | /// return that constant offset. For example, Ptr1 might be &A[42], and Ptr2 |
| 71 | /// might be &A[40]. In this case offset would be -8. |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 72 | static bool IsPointerOffset(Value *Ptr1, Value *Ptr2, int64_t &Offset, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 73 | const DataLayout &DL) { |
Chris Lattner | fa7c29d | 2011-01-12 01:43:46 +0000 | [diff] [blame] | 74 | Ptr1 = Ptr1->stripPointerCasts(); |
| 75 | Ptr2 = Ptr2->stripPointerCasts(); |
Benjamin Kramer | 3ef5e46 | 2014-03-10 21:05:13 +0000 | [diff] [blame] | 76 | |
| 77 | // Handle the trivial case first. |
| 78 | if (Ptr1 == Ptr2) { |
| 79 | Offset = 0; |
| 80 | return true; |
| 81 | } |
| 82 | |
Benjamin Kramer | 15a257d | 2012-09-13 16:29:49 +0000 | [diff] [blame] | 83 | GEPOperator *GEP1 = dyn_cast<GEPOperator>(Ptr1); |
| 84 | GEPOperator *GEP2 = dyn_cast<GEPOperator>(Ptr2); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 5120ebf | 2011-01-08 21:07:56 +0000 | [diff] [blame] | 86 | bool VariableIdxFound = false; |
| 87 | |
| 88 | // If one pointer is a GEP and the other isn't, then see if the GEP is a |
| 89 | // constant offset from the base, as in "P" and "gep P, 1". |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 90 | if (GEP1 && !GEP2 && GEP1->getOperand(0)->stripPointerCasts() == Ptr2) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 91 | Offset = -GetOffsetFromIndex(GEP1, 1, VariableIdxFound, DL); |
Chris Lattner | 5120ebf | 2011-01-08 21:07:56 +0000 | [diff] [blame] | 92 | return !VariableIdxFound; |
| 93 | } |
| 94 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 95 | if (GEP2 && !GEP1 && GEP2->getOperand(0)->stripPointerCasts() == Ptr1) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 96 | Offset = GetOffsetFromIndex(GEP2, 1, VariableIdxFound, DL); |
Chris Lattner | 5120ebf | 2011-01-08 21:07:56 +0000 | [diff] [blame] | 97 | return !VariableIdxFound; |
| 98 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 99 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 100 | // Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical |
| 101 | // base. After that base, they may have some number of common (and |
| 102 | // potentially variable) indices. After that they handle some constant |
| 103 | // offset, which determines their offset from each other. At this point, we |
| 104 | // handle no other case. |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 105 | if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0)) |
| 106 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 107 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 108 | // Skip any common indices and track the GEP types. |
| 109 | unsigned Idx = 1; |
| 110 | for (; Idx != GEP1->getNumOperands() && Idx != GEP2->getNumOperands(); ++Idx) |
| 111 | if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx)) |
| 112 | break; |
| 113 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 114 | int64_t Offset1 = GetOffsetFromIndex(GEP1, Idx, VariableIdxFound, DL); |
| 115 | int64_t Offset2 = GetOffsetFromIndex(GEP2, Idx, VariableIdxFound, DL); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 116 | if (VariableIdxFound) return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 117 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 118 | Offset = Offset2-Offset1; |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | |
Sanjay Patel | a75c41e | 2015-08-13 22:53:20 +0000 | [diff] [blame] | 123 | /// Represents a range of memset'd bytes with the ByteVal value. |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 124 | /// This allows us to analyze stores like: |
| 125 | /// store 0 -> P+1 |
| 126 | /// store 0 -> P+0 |
| 127 | /// store 0 -> P+3 |
| 128 | /// store 0 -> P+2 |
| 129 | /// which sometimes happens with stores to arrays of structs etc. When we see |
| 130 | /// the first store, we make a range [1, 2). The second store extends the range |
| 131 | /// to [0, 2). The third makes a new range [2, 3). The fourth store joins the |
| 132 | /// two ranges into [0, 3) which is memset'able. |
| 133 | namespace { |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 134 | struct MemsetRange { |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 135 | // 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] | 136 | // 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] | 137 | int64_t Start, End; |
| 138 | |
| 139 | /// StartPtr - The getelementptr instruction that points to the start of the |
| 140 | /// range. |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 141 | Value *StartPtr; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 142 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 143 | /// Alignment - The known alignment of the first store. |
| 144 | unsigned Alignment; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 145 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 146 | /// TheStores - The actual stores that make up this range. |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 147 | SmallVector<Instruction*, 16> TheStores; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 148 | |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 149 | bool isProfitableToUseMemset(const DataLayout &DL) const; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 150 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 151 | } // end anon namespace |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 152 | |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 153 | bool MemsetRange::isProfitableToUseMemset(const DataLayout &DL) const { |
| 154 | // 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] | 155 | if (TheStores.size() >= 4 || End-Start >= 16) return true; |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 156 | |
| 157 | // If there is nothing to merge, don't do anything. |
| 158 | if (TheStores.size() < 2) return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 159 | |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 160 | // If any of the stores are a memset, then it is always good to extend the |
| 161 | // memset. |
Craig Topper | e325e38 | 2015-11-20 07:18:48 +0000 | [diff] [blame] | 162 | for (Instruction *SI : TheStores) |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 163 | if (!isa<StoreInst>(SI)) |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 164 | return true; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 165 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 166 | // Assume that the code generator is capable of merging pairs of stores |
| 167 | // together if it wants to. |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 168 | if (TheStores.size() == 2) return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 169 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 170 | // If we have fewer than 8 stores, it can still be worthwhile to do this. |
| 171 | // For example, merging 4 i8 stores into an i32 store is useful almost always. |
| 172 | // However, merging 2 32-bit stores isn't useful on a 32-bit architecture (the |
| 173 | // memset will be split into 2 32-bit stores anyway) and doing so can |
| 174 | // pessimize the llvm optimizer. |
| 175 | // |
| 176 | // Since we don't have perfect knowledge here, make some assumptions: assume |
Matt Arsenault | 899f7d2 | 2013-09-16 22:43:16 +0000 | [diff] [blame] | 177 | // the maximum GPR width is the same size as the largest legal integer |
| 178 | // size. If so, check to see whether we will end up actually reducing the |
| 179 | // number of stores used. |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 180 | unsigned Bytes = unsigned(End-Start); |
Jun Bum Lim | be11bdc | 2016-05-13 18:38:35 +0000 | [diff] [blame] | 181 | unsigned MaxIntSize = DL.getLargestLegalIntTypeSizeInBits() / 8; |
Matt Arsenault | 899f7d2 | 2013-09-16 22:43:16 +0000 | [diff] [blame] | 182 | if (MaxIntSize == 0) |
| 183 | MaxIntSize = 1; |
| 184 | unsigned NumPointerStores = Bytes / MaxIntSize; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 185 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 186 | // Assume the remaining bytes if any are done a byte at a time. |
Craig Topper | a5ea528 | 2015-11-21 17:44:42 +0000 | [diff] [blame] | 187 | unsigned NumByteStores = Bytes % MaxIntSize; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 188 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 189 | // If we will reduce the # stores (according to this heuristic), do the |
| 190 | // transformation. This encourages merging 4 x i8 -> i32 and 2 x i16 -> i32 |
| 191 | // etc. |
| 192 | return TheStores.size() > NumPointerStores+NumByteStores; |
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 | |
| 195 | |
| 196 | namespace { |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 197 | class MemsetRanges { |
Sanjay Patel | a75c41e | 2015-08-13 22:53:20 +0000 | [diff] [blame] | 198 | /// A sorted list of the memset ranges. |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 199 | SmallVector<MemsetRange, 8> Ranges; |
| 200 | typedef SmallVectorImpl<MemsetRange>::iterator range_iterator; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 201 | const DataLayout &DL; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 202 | public: |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 203 | MemsetRanges(const DataLayout &DL) : DL(DL) {} |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 204 | |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 205 | typedef SmallVectorImpl<MemsetRange>::const_iterator const_iterator; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 206 | const_iterator begin() const { return Ranges.begin(); } |
| 207 | const_iterator end() const { return Ranges.end(); } |
| 208 | bool empty() const { return Ranges.empty(); } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 209 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 210 | void addInst(int64_t OffsetFromFirst, Instruction *Inst) { |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 211 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) |
| 212 | addStore(OffsetFromFirst, SI); |
| 213 | else |
| 214 | addMemSet(OffsetFromFirst, cast<MemSetInst>(Inst)); |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 215 | } |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 216 | |
| 217 | void addStore(int64_t OffsetFromFirst, StoreInst *SI) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 218 | int64_t StoreSize = DL.getTypeStoreSize(SI->getOperand(0)->getType()); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 220 | addRange(OffsetFromFirst, StoreSize, |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 221 | SI->getPointerOperand(), SI->getAlignment(), SI); |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 222 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 223 | |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 224 | void addMemSet(int64_t OffsetFromFirst, MemSetInst *MSI) { |
| 225 | int64_t Size = cast<ConstantInt>(MSI->getLength())->getZExtValue(); |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 226 | addRange(OffsetFromFirst, Size, MSI->getDest(), MSI->getAlignment(), MSI); |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 227 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 228 | |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 229 | void addRange(int64_t Start, int64_t Size, Value *Ptr, |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 230 | unsigned Alignment, Instruction *Inst); |
| 231 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 232 | }; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 233 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 234 | } // end anon namespace |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 235 | |
| 236 | |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 237 | /// Add a new store to the MemsetRanges data structure. This adds a |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 238 | /// new range for the specified store at the specified offset, merging into |
| 239 | /// existing ranges as appropriate. |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 240 | void MemsetRanges::addRange(int64_t Start, int64_t Size, Value *Ptr, |
| 241 | unsigned Alignment, Instruction *Inst) { |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 242 | int64_t End = Start+Size; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 243 | |
Nick Lewycky | f836c89 | 2015-07-21 21:56:26 +0000 | [diff] [blame] | 244 | range_iterator I = std::lower_bound(Ranges.begin(), Ranges.end(), Start, |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 245 | [](const MemsetRange &LHS, int64_t RHS) { return LHS.End < RHS; }); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 246 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 247 | // We now know that I == E, in which case we didn't find anything to merge |
| 248 | // with, or that Start <= I->End. If End < I->Start or I == E, then we need |
| 249 | // to insert a new range. Handle this now. |
Nick Lewycky | f836c89 | 2015-07-21 21:56:26 +0000 | [diff] [blame] | 250 | if (I == Ranges.end() || End < I->Start) { |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 251 | MemsetRange &R = *Ranges.insert(I, MemsetRange()); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 252 | R.Start = Start; |
| 253 | R.End = End; |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 254 | R.StartPtr = Ptr; |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 255 | R.Alignment = Alignment; |
| 256 | R.TheStores.push_back(Inst); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 257 | return; |
| 258 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 259 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 260 | // This store overlaps with I, add it. |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 261 | I->TheStores.push_back(Inst); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 262 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 263 | // At this point, we may have an interval that completely contains our store. |
| 264 | // If so, just add it to the interval and return. |
| 265 | if (I->Start <= Start && I->End >= End) |
| 266 | return; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 267 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 268 | // Now we know that Start <= I->End and End >= I->Start so the range overlaps |
| 269 | // but is not entirely contained within the range. |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 270 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 271 | // See if the range extends the start of the range. In this case, it couldn't |
| 272 | // possibly cause it to join the prior range, because otherwise we would have |
| 273 | // stopped on *it*. |
| 274 | if (Start < I->Start) { |
| 275 | I->Start = Start; |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 276 | I->StartPtr = Ptr; |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 277 | I->Alignment = Alignment; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 278 | } |
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 Start >= I->Start (so the startpoint |
| 281 | // is in or right at the end of I), and that End >= I->Start. Extend I out to |
| 282 | // End. |
| 283 | if (End > I->End) { |
| 284 | I->End = End; |
Nick Lewycky | bfd4ad6 | 2009-03-19 05:51:39 +0000 | [diff] [blame] | 285 | range_iterator NextI = I; |
Nick Lewycky | f836c89 | 2015-07-21 21:56:26 +0000 | [diff] [blame] | 286 | while (++NextI != Ranges.end() && End >= NextI->Start) { |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 287 | // Merge the range in. |
| 288 | I->TheStores.append(NextI->TheStores.begin(), NextI->TheStores.end()); |
| 289 | if (NextI->End > I->End) |
| 290 | I->End = NextI->End; |
| 291 | Ranges.erase(NextI); |
| 292 | NextI = I; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | //===----------------------------------------------------------------------===// |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 298 | // MemCpyOptLegacyPass Pass |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 299 | //===----------------------------------------------------------------------===// |
| 300 | |
| 301 | namespace { |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 302 | class MemCpyOptLegacyPass : public FunctionPass { |
| 303 | MemCpyOptPass Impl; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 304 | public: |
| 305 | static char ID; // Pass identification, replacement for typeid |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 306 | MemCpyOptLegacyPass() : FunctionPass(ID) { |
| 307 | initializeMemCpyOptLegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 308 | } |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 309 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 310 | bool runOnFunction(Function &F) override; |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 311 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 312 | private: |
| 313 | // This transformation requires dominator postdominator info |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 314 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 315 | AU.setPreservesCFG(); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 316 | AU.addRequired<AssumptionCacheTracker>(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 317 | AU.addRequired<DominatorTreeWrapperPass>(); |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 318 | AU.addRequired<MemoryDependenceWrapperPass>(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 319 | AU.addRequired<AAResultsWrapperPass>(); |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 320 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 321 | AU.addPreserved<GlobalsAAWrapperPass>(); |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 322 | AU.addPreserved<MemoryDependenceWrapperPass>(); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 323 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 324 | |
Matt Wala | a4afccd | 2015-06-12 18:16:51 +0000 | [diff] [blame] | 325 | // Helper functions |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 326 | bool processStore(StoreInst *SI, BasicBlock::iterator &BBI); |
Chris Lattner | 9a1d63b | 2011-01-08 21:19:19 +0000 | [diff] [blame] | 327 | bool processMemSet(MemSetInst *SI, BasicBlock::iterator &BBI); |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 328 | bool processMemCpy(MemCpyInst *M); |
Chris Lattner | 1145e33 | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 329 | bool processMemMove(MemMoveInst *M); |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 330 | bool performCallSlotOptzn(Instruction *cpy, Value *cpyDst, Value *cpySrc, |
Duncan Sands | c6ada69 | 2012-10-04 10:54:40 +0000 | [diff] [blame] | 331 | uint64_t cpyLen, unsigned cpyAlign, CallInst *C); |
Ahmed Bougacha | 15a31f6 | 2015-05-16 01:23:47 +0000 | [diff] [blame] | 332 | bool processMemCpyMemCpyDependence(MemCpyInst *M, MemCpyInst *MDep); |
Ahmed Bougacha | 83f78a4 | 2015-04-17 22:20:57 +0000 | [diff] [blame] | 333 | bool processMemSetMemCpyDependence(MemCpyInst *M, MemSetInst *MDep); |
Ahmed Bougacha | f8fa3b8 | 2015-05-16 01:32:26 +0000 | [diff] [blame] | 334 | bool performMemCpyToMemSetOptzn(MemCpyInst *M, MemSetInst *MDep); |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 335 | bool processByValArgument(CallSite CS, unsigned ArgNo); |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 336 | Instruction *tryMergingIntoMemset(Instruction *I, Value *StartPtr, |
| 337 | Value *ByteVal); |
| 338 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 339 | bool iterateOnFunction(Function &F); |
| 340 | }; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 341 | |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 342 | char MemCpyOptLegacyPass::ID = 0; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 343 | } |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 344 | |
Sanjay Patel | a75c41e | 2015-08-13 22:53:20 +0000 | [diff] [blame] | 345 | /// The public interface to this file... |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 346 | FunctionPass *llvm::createMemCpyOptPass() { return new MemCpyOptLegacyPass(); } |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 347 | |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 348 | INITIALIZE_PASS_BEGIN(MemCpyOptLegacyPass, "memcpyopt", "MemCpy Optimization", |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 349 | false, false) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 350 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 351 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 352 | INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass) |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 353 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 354 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
| 355 | INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass) |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 356 | INITIALIZE_PASS_END(MemCpyOptLegacyPass, "memcpyopt", "MemCpy Optimization", |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 357 | false, false) |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 358 | |
Sanjay Patel | a75c41e | 2015-08-13 22:53:20 +0000 | [diff] [blame] | 359 | /// When scanning forward over instructions, we look for some other patterns to |
| 360 | /// fold away. In particular, this looks for stores to neighboring locations of |
| 361 | /// memory. If it sees enough consecutive ones, it attempts to merge them |
| 362 | /// together into a memcpy/memset. |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 363 | Instruction *MemCpyOptPass::tryMergingIntoMemset(Instruction *StartInst, |
| 364 | Value *StartPtr, |
| 365 | Value *ByteVal) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 366 | const DataLayout &DL = StartInst->getModule()->getDataLayout(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 367 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 368 | // Okay, so we now have a single store that can be splatable. Scan to find |
| 369 | // all subsequent stores of the same value to offset from the same pointer. |
| 370 | // Join these together into ranges, so we can decide whether contiguous blocks |
| 371 | // are stored. |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 372 | MemsetRanges Ranges(DL); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 373 | |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 374 | BasicBlock::iterator BI(StartInst); |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 375 | for (++BI; !isa<TerminatorInst>(BI); ++BI) { |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 376 | if (!isa<StoreInst>(BI) && !isa<MemSetInst>(BI)) { |
| 377 | // If the instruction is readnone, ignore it, otherwise bail out. We |
| 378 | // 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] | 379 | // A[1] = 2; strlen(A); A[2] = 2; -> memcpy(A, ...); strlen(A). |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 380 | if (BI->mayWriteToMemory() || BI->mayReadFromMemory()) |
| 381 | break; |
| 382 | continue; |
| 383 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 384 | |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 385 | if (StoreInst *NextStore = dyn_cast<StoreInst>(BI)) { |
| 386 | // If this is a store, see if we can merge it in. |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 387 | if (!NextStore->isSimple()) break; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 388 | |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 389 | // Check to see if this stored value is of the same byte-splattable value. |
| 390 | if (ByteVal != isBytewiseValue(NextStore->getOperand(0))) |
| 391 | break; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 392 | |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 393 | // Check to see if this store is to a constant offset from the start ptr. |
| 394 | int64_t Offset; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 395 | if (!IsPointerOffset(StartPtr, NextStore->getPointerOperand(), Offset, |
| 396 | DL)) |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 397 | break; |
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 | Ranges.addStore(Offset, NextStore); |
| 400 | } else { |
| 401 | MemSetInst *MSI = cast<MemSetInst>(BI); |
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 | if (MSI->isVolatile() || ByteVal != MSI->getValue() || |
| 404 | !isa<ConstantInt>(MSI->getLength())) |
| 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; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 409 | if (!IsPointerOffset(StartPtr, MSI->getDest(), Offset, DL)) |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 410 | break; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 411 | |
Chris Lattner | 4dc1fd9 | 2011-01-08 20:54:51 +0000 | [diff] [blame] | 412 | Ranges.addMemSet(Offset, MSI); |
| 413 | } |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 414 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 415 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 416 | // If we have no ranges, then we just had a single store with nothing that |
| 417 | // could be merged in. This is a very common case of course. |
| 418 | if (Ranges.empty()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 419 | return nullptr; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 420 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 421 | // If we had at least one store that could be merged in, add the starting |
| 422 | // store as well. We try to avoid this unless there is at least something |
| 423 | // interesting as a small compile-time optimization. |
| 424 | Ranges.addInst(0, StartInst); |
| 425 | |
| 426 | // If we create any memsets, we put it right before the first instruction that |
| 427 | // isn't part of the memset block. This ensure that the memset is dominated |
| 428 | // by any addressing instruction needed by the start of the block. |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 429 | IRBuilder<> Builder(&*BI); |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 430 | |
| 431 | // Now that we have full information about ranges, loop over the ranges and |
| 432 | // emit memset's for anything big enough to be worthwhile. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 433 | Instruction *AMemSet = nullptr; |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 434 | for (const MemsetRange &Range : Ranges) { |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 435 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 436 | if (Range.TheStores.size() == 1) continue; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 437 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 438 | // If it is profitable to lower this range to memset, do so now. |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 439 | if (!Range.isProfitableToUseMemset(DL)) |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 440 | continue; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 441 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 442 | // Otherwise, we do want to transform this! Create a new memset. |
| 443 | // Get the starting pointer of the block. |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 444 | StartPtr = Range.StartPtr; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 445 | |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 446 | // Determine alignment |
| 447 | unsigned Alignment = Range.Alignment; |
| 448 | if (Alignment == 0) { |
| 449 | Type *EltType = |
| 450 | cast<PointerType>(StartPtr->getType())->getElementType(); |
| 451 | Alignment = DL.getABITypeAlignment(EltType); |
| 452 | } |
| 453 | |
| 454 | AMemSet = |
| 455 | Builder.CreateMemSet(StartPtr, ByteVal, Range.End-Range.Start, Alignment); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 456 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 457 | DEBUG(dbgs() << "Replace stores:\n"; |
Craig Topper | e325e38 | 2015-11-20 07:18:48 +0000 | [diff] [blame] | 458 | for (Instruction *SI : Range.TheStores) |
| 459 | dbgs() << *SI << '\n'; |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 460 | dbgs() << "With: " << *AMemSet << '\n'); |
Devang Patel | c7e4fa7 | 2011-05-04 21:58:58 +0000 | [diff] [blame] | 461 | |
| 462 | if (!Range.TheStores.empty()) |
| 463 | AMemSet->setDebugLoc(Range.TheStores[0]->getDebugLoc()); |
| 464 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 465 | // Zap all the stores. |
Craig Topper | e325e38 | 2015-11-20 07:18:48 +0000 | [diff] [blame] | 466 | for (Instruction *SI : Range.TheStores) { |
| 467 | MD->removeInstruction(SI); |
| 468 | SI->eraseFromParent(); |
Chris Lattner | 7d6433a | 2011-01-08 22:19:21 +0000 | [diff] [blame] | 469 | } |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 470 | ++NumMemSetInfer; |
| 471 | } |
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 | return AMemSet; |
| 474 | } |
| 475 | |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 476 | static unsigned findCommonAlignment(const DataLayout &DL, const StoreInst *SI, |
| 477 | const LoadInst *LI) { |
| 478 | unsigned StoreAlign = SI->getAlignment(); |
| 479 | if (!StoreAlign) |
| 480 | StoreAlign = DL.getABITypeAlignment(SI->getOperand(0)->getType()); |
| 481 | unsigned LoadAlign = LI->getAlignment(); |
| 482 | if (!LoadAlign) |
| 483 | LoadAlign = DL.getABITypeAlignment(LI->getType()); |
Amaury Sechet | a0c242c | 2016-01-05 20:17:48 +0000 | [diff] [blame] | 484 | |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 485 | return std::min(StoreAlign, LoadAlign); |
Amaury Sechet | a0c242c | 2016-01-05 20:17:48 +0000 | [diff] [blame] | 486 | } |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 487 | |
Amaury Sechet | bdb261b | 2016-03-14 22:52:27 +0000 | [diff] [blame] | 488 | // This method try to lift a store instruction before position P. |
| 489 | // It will lift the store and its argument + that anything that |
David Majnemer | d99068d | 2016-05-26 19:24:24 +0000 | [diff] [blame] | 490 | // may alias with these. |
Amaury Sechet | bdb261b | 2016-03-14 22:52:27 +0000 | [diff] [blame] | 491 | // The method returns true if it was successful. |
| 492 | static bool moveUp(AliasAnalysis &AA, StoreInst *SI, Instruction *P) { |
| 493 | // If the store alias this position, early bail out. |
| 494 | MemoryLocation StoreLoc = MemoryLocation::get(SI); |
| 495 | if (AA.getModRefInfo(P, StoreLoc) != MRI_NoModRef) |
| 496 | return false; |
| 497 | |
| 498 | // Keep track of the arguments of all instruction we plan to lift |
| 499 | // so we can make sure to lift them as well if apropriate. |
| 500 | DenseSet<Instruction*> Args; |
| 501 | if (auto *Ptr = dyn_cast<Instruction>(SI->getPointerOperand())) |
| 502 | if (Ptr->getParent() == SI->getParent()) |
| 503 | Args.insert(Ptr); |
| 504 | |
| 505 | // Instruction to lift before P. |
| 506 | SmallVector<Instruction*, 8> ToLift; |
| 507 | |
| 508 | // Memory locations of lifted instructions. |
| 509 | SmallVector<MemoryLocation, 8> MemLocs; |
| 510 | MemLocs.push_back(StoreLoc); |
| 511 | |
| 512 | // Lifted callsites. |
| 513 | SmallVector<ImmutableCallSite, 8> CallSites; |
| 514 | |
| 515 | for (auto I = --SI->getIterator(), E = P->getIterator(); I != E; --I) { |
| 516 | auto *C = &*I; |
| 517 | |
| 518 | bool MayAlias = AA.getModRefInfo(C) != MRI_NoModRef; |
| 519 | |
| 520 | bool NeedLift = false; |
| 521 | if (Args.erase(C)) |
| 522 | NeedLift = true; |
| 523 | else if (MayAlias) { |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 524 | NeedLift = any_of(MemLocs, [C, &AA](const MemoryLocation &ML) { |
| 525 | return AA.getModRefInfo(C, ML); |
| 526 | }); |
Amaury Sechet | bdb261b | 2016-03-14 22:52:27 +0000 | [diff] [blame] | 527 | |
| 528 | if (!NeedLift) |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 529 | NeedLift = any_of(CallSites, [C, &AA](const ImmutableCallSite &CS) { |
| 530 | return AA.getModRefInfo(C, CS); |
| 531 | }); |
Amaury Sechet | bdb261b | 2016-03-14 22:52:27 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | if (!NeedLift) |
| 535 | continue; |
| 536 | |
| 537 | if (MayAlias) { |
| 538 | if (auto CS = ImmutableCallSite(C)) { |
| 539 | // If we can't lift this before P, it's game over. |
| 540 | if (AA.getModRefInfo(P, CS) != MRI_NoModRef) |
| 541 | return false; |
| 542 | |
| 543 | CallSites.push_back(CS); |
| 544 | } else if (isa<LoadInst>(C) || isa<StoreInst>(C) || isa<VAArgInst>(C)) { |
| 545 | // If we can't lift this before P, it's game over. |
| 546 | auto ML = MemoryLocation::get(C); |
| 547 | if (AA.getModRefInfo(P, ML) != MRI_NoModRef) |
| 548 | return false; |
| 549 | |
| 550 | MemLocs.push_back(ML); |
| 551 | } else |
| 552 | // We don't know how to lift this instruction. |
| 553 | return false; |
| 554 | } |
| 555 | |
| 556 | ToLift.push_back(C); |
| 557 | for (unsigned k = 0, e = C->getNumOperands(); k != e; ++k) |
| 558 | if (auto *A = dyn_cast<Instruction>(C->getOperand(k))) |
| 559 | if (A->getParent() == SI->getParent()) |
| 560 | Args.insert(A); |
| 561 | } |
| 562 | |
| 563 | // We made it, we need to lift |
| 564 | for (auto *I : reverse(ToLift)) { |
| 565 | DEBUG(dbgs() << "Lifting " << *I << " before " << *P << "\n"); |
| 566 | I->moveBefore(P); |
| 567 | } |
| 568 | |
| 569 | return true; |
| 570 | } |
| 571 | |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 572 | bool MemCpyOptPass::processStore(StoreInst *SI, BasicBlock::iterator &BBI) { |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 573 | if (!SI->isSimple()) return false; |
Andrea Di Biagio | 99493df | 2015-10-09 10:53:41 +0000 | [diff] [blame] | 574 | |
| 575 | // Avoid merging nontemporal stores since the resulting |
| 576 | // memcpy/memset would not be able to preserve the nontemporal hint. |
| 577 | // In theory we could teach how to propagate the !nontemporal metadata to |
| 578 | // memset calls. However, that change would force the backend to |
| 579 | // conservatively expand !nontemporal memset calls back to sequences of |
| 580 | // store instructions (effectively undoing the merging). |
| 581 | if (SI->getMetadata(LLVMContext::MD_nontemporal)) |
| 582 | return false; |
| 583 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 584 | const DataLayout &DL = SI->getModule()->getDataLayout(); |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 585 | |
Amaury Sechet | a0c242c | 2016-01-05 20:17:48 +0000 | [diff] [blame] | 586 | // Load to store forwarding can be interpreted as memcpy. |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 587 | if (LoadInst *LI = dyn_cast<LoadInst>(SI->getOperand(0))) { |
Eli Friedman | 9a46815 | 2011-08-17 22:22:24 +0000 | [diff] [blame] | 588 | if (LI->isSimple() && LI->hasOneUse() && |
Eli Friedman | e8bbc10 | 2011-06-15 01:25:56 +0000 | [diff] [blame] | 589 | LI->getParent() == SI->getParent()) { |
Amaury Sechet | a0c242c | 2016-01-05 20:17:48 +0000 | [diff] [blame] | 590 | |
| 591 | auto *T = LI->getType(); |
| 592 | if (T->isAggregateType()) { |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 593 | AliasAnalysis &AA = LookupAliasAnalysis(); |
Amaury Sechet | a0c242c | 2016-01-05 20:17:48 +0000 | [diff] [blame] | 594 | MemoryLocation LoadLoc = MemoryLocation::get(LI); |
| 595 | |
| 596 | // We use alias analysis to check if an instruction may store to |
| 597 | // the memory we load from in between the load and the store. If |
Amaury Sechet | d3b2c0f | 2016-01-06 09:30:39 +0000 | [diff] [blame] | 598 | // such an instruction is found, we try to promote there instead |
| 599 | // of at the store position. |
| 600 | Instruction *P = SI; |
Amaury Sechet | bdb261b | 2016-03-14 22:52:27 +0000 | [diff] [blame] | 601 | for (auto &I : make_range(++LI->getIterator(), SI->getIterator())) { |
| 602 | if (AA.getModRefInfo(&I, LoadLoc) & MRI_Mod) { |
| 603 | P = &I; |
| 604 | break; |
Amaury Sechet | a0c242c | 2016-01-05 20:17:48 +0000 | [diff] [blame] | 605 | } |
Amaury Sechet | bdb261b | 2016-03-14 22:52:27 +0000 | [diff] [blame] | 606 | } |
Amaury Sechet | d3b2c0f | 2016-01-06 09:30:39 +0000 | [diff] [blame] | 607 | |
Amaury Sechet | bdb261b | 2016-03-14 22:52:27 +0000 | [diff] [blame] | 608 | // We found an instruction that may write to the loaded memory. |
| 609 | // We can try to promote at this position instead of the store |
| 610 | // position if nothing alias the store memory after this and the store |
| 611 | // destination is not in the range. |
| 612 | if (P && P != SI) { |
| 613 | if (!moveUp(AA, SI, P)) |
| 614 | P = nullptr; |
Amaury Sechet | a0c242c | 2016-01-05 20:17:48 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Amaury Sechet | d3b2c0f | 2016-01-06 09:30:39 +0000 | [diff] [blame] | 617 | // If a valid insertion position is found, then we can promote |
| 618 | // the load/store pair to a memcpy. |
| 619 | if (P) { |
Amaury Sechet | a0c242c | 2016-01-05 20:17:48 +0000 | [diff] [blame] | 620 | // If we load from memory that may alias the memory we store to, |
| 621 | // memmove must be used to preserve semantic. If not, memcpy can |
| 622 | // be used. |
| 623 | bool UseMemMove = false; |
| 624 | if (!AA.isNoAlias(MemoryLocation::get(SI), LoadLoc)) |
| 625 | UseMemMove = true; |
| 626 | |
| 627 | unsigned Align = findCommonAlignment(DL, SI, LI); |
| 628 | uint64_t Size = DL.getTypeStoreSize(T); |
| 629 | |
Amaury Sechet | d3b2c0f | 2016-01-06 09:30:39 +0000 | [diff] [blame] | 630 | IRBuilder<> Builder(P); |
Amaury Sechet | a0c242c | 2016-01-05 20:17:48 +0000 | [diff] [blame] | 631 | Instruction *M; |
| 632 | if (UseMemMove) |
| 633 | M = Builder.CreateMemMove(SI->getPointerOperand(), |
| 634 | LI->getPointerOperand(), Size, |
| 635 | Align, SI->isVolatile()); |
| 636 | else |
| 637 | M = Builder.CreateMemCpy(SI->getPointerOperand(), |
| 638 | LI->getPointerOperand(), Size, |
| 639 | Align, SI->isVolatile()); |
| 640 | |
| 641 | DEBUG(dbgs() << "Promoting " << *LI << " to " << *SI |
| 642 | << " => " << *M << "\n"); |
| 643 | |
| 644 | MD->removeInstruction(SI); |
| 645 | SI->eraseFromParent(); |
| 646 | MD->removeInstruction(LI); |
| 647 | LI->eraseFromParent(); |
| 648 | ++NumMemCpyInstr; |
| 649 | |
| 650 | // Make sure we do not invalidate the iterator. |
| 651 | BBI = M->getIterator(); |
| 652 | return true; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | // Detect cases where we're performing call slot forwarding, but |
| 657 | // happen to be using a load-store pair to implement it, rather than |
| 658 | // a memcpy. |
Eli Friedman | 5da0ff4 | 2011-06-02 21:24:42 +0000 | [diff] [blame] | 659 | MemDepResult ldep = MD->getDependency(LI); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 660 | CallInst *C = nullptr; |
Eli Friedman | 5da0ff4 | 2011-06-02 21:24:42 +0000 | [diff] [blame] | 661 | if (ldep.isClobber() && !isa<MemCpyInst>(ldep.getInst())) |
| 662 | C = dyn_cast<CallInst>(ldep.getInst()); |
| 663 | |
| 664 | if (C) { |
| 665 | // Check that nothing touches the dest of the "copy" between |
| 666 | // the call and the store. |
David Majnemer | d99068d | 2016-05-26 19:24:24 +0000 | [diff] [blame] | 667 | Value *CpyDest = SI->getPointerOperand()->stripPointerCasts(); |
| 668 | bool CpyDestIsLocal = isa<AllocaInst>(CpyDest); |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 669 | AliasAnalysis &AA = LookupAliasAnalysis(); |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 670 | MemoryLocation StoreLoc = MemoryLocation::get(SI); |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 671 | for (BasicBlock::iterator I = --SI->getIterator(), E = C->getIterator(); |
| 672 | I != E; --I) { |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 673 | if (AA.getModRefInfo(&*I, StoreLoc) != MRI_NoModRef) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 674 | C = nullptr; |
Eli Friedman | e8bbc10 | 2011-06-15 01:25:56 +0000 | [diff] [blame] | 675 | break; |
| 676 | } |
David Majnemer | d99068d | 2016-05-26 19:24:24 +0000 | [diff] [blame] | 677 | // The store to dest may never happen if an exception can be thrown |
| 678 | // between the load and the store. |
| 679 | if (I->mayThrow() && !CpyDestIsLocal) { |
| 680 | C = nullptr; |
| 681 | break; |
| 682 | } |
Eli Friedman | 5da0ff4 | 2011-06-02 21:24:42 +0000 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 686 | if (C) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 687 | bool changed = performCallSlotOptzn( |
| 688 | LI, SI->getPointerOperand()->stripPointerCasts(), |
| 689 | LI->getPointerOperand()->stripPointerCasts(), |
| 690 | DL.getTypeStoreSize(SI->getOperand(0)->getType()), |
Amaury Sechet | a0c242c | 2016-01-05 20:17:48 +0000 | [diff] [blame] | 691 | findCommonAlignment(DL, SI, LI), C); |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 692 | if (changed) { |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 693 | MD->removeInstruction(SI); |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 694 | SI->eraseFromParent(); |
Chris Lattner | caf5c0d | 2011-01-09 19:26:10 +0000 | [diff] [blame] | 695 | MD->removeInstruction(LI); |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 696 | LI->eraseFromParent(); |
| 697 | ++NumMemCpyInstr; |
| 698 | return true; |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 703 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 704 | // There are two cases that are interesting for this code to handle: memcpy |
| 705 | // and memset. Right now we only handle memset. |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 706 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 707 | // Ensure that the value being stored is something that can be memset'able a |
| 708 | // byte at a time like "0" or "-1" or any width, as well as things like |
| 709 | // 0xA0A0A0A0 and 0.0. |
Amaury Sechet | 3235c08 | 2016-01-06 19:47:24 +0000 | [diff] [blame] | 710 | auto *V = SI->getOperand(0); |
| 711 | if (Value *ByteVal = isBytewiseValue(V)) { |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 712 | if (Instruction *I = tryMergingIntoMemset(SI, SI->getPointerOperand(), |
| 713 | ByteVal)) { |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 714 | BBI = I->getIterator(); // Don't invalidate iterator. |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 715 | return true; |
Mon P Wang | c576ee9 | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 716 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 717 | |
Amaury Sechet | 3235c08 | 2016-01-06 19:47:24 +0000 | [diff] [blame] | 718 | // If we have an aggregate, we try to promote it to memset regardless |
| 719 | // of opportunity for merging as it can expose optimization opportunities |
| 720 | // in subsequent passes. |
| 721 | auto *T = V->getType(); |
| 722 | if (T->isAggregateType()) { |
| 723 | uint64_t Size = DL.getTypeStoreSize(T); |
| 724 | unsigned Align = SI->getAlignment(); |
| 725 | if (!Align) |
| 726 | Align = DL.getABITypeAlignment(T); |
| 727 | IRBuilder<> Builder(SI); |
| 728 | auto *M = Builder.CreateMemSet(SI->getPointerOperand(), ByteVal, |
| 729 | Size, Align, SI->isVolatile()); |
| 730 | |
| 731 | DEBUG(dbgs() << "Promoting " << *SI << " to " << *M << "\n"); |
| 732 | |
| 733 | MD->removeInstruction(SI); |
| 734 | SI->eraseFromParent(); |
| 735 | NumMemSetInfer++; |
| 736 | |
| 737 | // Make sure we do not invalidate the iterator. |
| 738 | BBI = M->getIterator(); |
| 739 | return true; |
| 740 | } |
| 741 | } |
| 742 | |
Chris Lattner | c638147 | 2011-01-08 20:24:01 +0000 | [diff] [blame] | 743 | return false; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 746 | bool MemCpyOptPass::processMemSet(MemSetInst *MSI, BasicBlock::iterator &BBI) { |
Chris Lattner | 9a1d63b | 2011-01-08 21:19:19 +0000 | [diff] [blame] | 747 | // See if there is another memset or store neighboring this memset which |
| 748 | // 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] | 749 | if (isa<ConstantInt>(MSI->getLength()) && !MSI->isVolatile()) |
| 750 | if (Instruction *I = tryMergingIntoMemset(MSI, MSI->getDest(), |
| 751 | MSI->getValue())) { |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 752 | BBI = I->getIterator(); // Don't invalidate iterator. |
Chris Lattner | ff6ed2a | 2011-01-08 22:11:56 +0000 | [diff] [blame] | 753 | return true; |
| 754 | } |
Chris Lattner | 9a1d63b | 2011-01-08 21:19:19 +0000 | [diff] [blame] | 755 | return false; |
| 756 | } |
| 757 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 758 | |
Sanjay Patel | a75c41e | 2015-08-13 22:53:20 +0000 | [diff] [blame] | 759 | /// Takes a memcpy and a call that it depends on, |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 760 | /// and checks for the possibility of a call slot optimization by having |
| 761 | /// the call write its result directly into the destination of the memcpy. |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 762 | bool MemCpyOptPass::performCallSlotOptzn(Instruction *cpy, Value *cpyDest, |
| 763 | Value *cpySrc, uint64_t cpyLen, |
| 764 | unsigned cpyAlign, CallInst *C) { |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 765 | // The general transformation to keep in mind is |
| 766 | // |
| 767 | // call @func(..., src, ...) |
| 768 | // memcpy(dest, src, ...) |
| 769 | // |
| 770 | // -> |
| 771 | // |
| 772 | // memcpy(dest, src, ...) |
| 773 | // call @func(..., dest, ...) |
| 774 | // |
| 775 | // Since moving the memcpy is technically awkward, we additionally check that |
| 776 | // src only holds uninitialized values at the moment of the call, meaning that |
| 777 | // the memcpy can be discarded rather than moved. |
| 778 | |
Tim Shen | 7aa0ad6 | 2016-06-08 19:42:32 +0000 | [diff] [blame] | 779 | // Lifetime marks shouldn't be operated on. |
| 780 | if (Function *F = C->getCalledFunction()) |
| 781 | if (F->isIntrinsic() && F->getIntrinsicID() == Intrinsic::lifetime_start) |
| 782 | return false; |
| 783 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 784 | // Deliberately get the source and destination with bitcasts stripped away, |
| 785 | // 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] | 786 | CallSite CS(C); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 787 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 788 | // Require that src be an alloca. This simplifies the reasoning considerably. |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 789 | AllocaInst *srcAlloca = dyn_cast<AllocaInst>(cpySrc); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 790 | if (!srcAlloca) |
| 791 | return false; |
| 792 | |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 793 | ConstantInt *srcArraySize = dyn_cast<ConstantInt>(srcAlloca->getArraySize()); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 794 | if (!srcArraySize) |
| 795 | return false; |
| 796 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 797 | const DataLayout &DL = cpy->getModule()->getDataLayout(); |
| 798 | uint64_t srcSize = DL.getTypeAllocSize(srcAlloca->getAllocatedType()) * |
| 799 | srcArraySize->getZExtValue(); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 800 | |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 801 | if (cpyLen < srcSize) |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 802 | return false; |
| 803 | |
| 804 | // Check that accessing the first srcSize bytes of dest will not cause a |
| 805 | // trap. Otherwise the transform is invalid since it might cause a trap |
| 806 | // to occur earlier than it otherwise would. |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 807 | if (AllocaInst *A = dyn_cast<AllocaInst>(cpyDest)) { |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 808 | // The destination is an alloca. Check it is larger than srcSize. |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 809 | ConstantInt *destArraySize = dyn_cast<ConstantInt>(A->getArraySize()); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 810 | if (!destArraySize) |
| 811 | return false; |
| 812 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 813 | uint64_t destSize = DL.getTypeAllocSize(A->getAllocatedType()) * |
| 814 | destArraySize->getZExtValue(); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 815 | |
| 816 | if (destSize < srcSize) |
| 817 | return false; |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 818 | } else if (Argument *A = dyn_cast<Argument>(cpyDest)) { |
David Majnemer | d99068d | 2016-05-26 19:24:24 +0000 | [diff] [blame] | 819 | // The store to dest may never happen if the call can throw. |
| 820 | if (C->mayThrow()) |
| 821 | return false; |
| 822 | |
Bjorn Steinbrink | d20816f | 2014-10-16 19:43:08 +0000 | [diff] [blame] | 823 | if (A->getDereferenceableBytes() < srcSize) { |
| 824 | // If the destination is an sret parameter then only accesses that are |
| 825 | // outside of the returned struct type can trap. |
| 826 | if (!A->hasStructRetAttr()) |
| 827 | return false; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 828 | |
Bjorn Steinbrink | d20816f | 2014-10-16 19:43:08 +0000 | [diff] [blame] | 829 | Type *StructTy = cast<PointerType>(A->getType())->getElementType(); |
| 830 | if (!StructTy->isSized()) { |
| 831 | // The call may never return and hence the copy-instruction may never |
| 832 | // be executed, and therefore it's not safe to say "the destination |
| 833 | // has at least <cpyLen> bytes, as implied by the copy-instruction", |
| 834 | return false; |
| 835 | } |
| 836 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 837 | uint64_t destSize = DL.getTypeAllocSize(StructTy); |
Bjorn Steinbrink | d20816f | 2014-10-16 19:43:08 +0000 | [diff] [blame] | 838 | if (destSize < srcSize) |
| 839 | return false; |
Shuxin Yang | 140d592 | 2013-06-08 04:56:05 +0000 | [diff] [blame] | 840 | } |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 841 | } else { |
| 842 | return false; |
| 843 | } |
| 844 | |
Duncan Sands | 933db77 | 2012-10-05 07:29:46 +0000 | [diff] [blame] | 845 | // Check that dest points to memory that is at least as aligned as src. |
| 846 | unsigned srcAlign = srcAlloca->getAlignment(); |
| 847 | if (!srcAlign) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 848 | srcAlign = DL.getABITypeAlignment(srcAlloca->getAllocatedType()); |
Duncan Sands | 933db77 | 2012-10-05 07:29:46 +0000 | [diff] [blame] | 849 | bool isDestSufficientlyAligned = srcAlign <= cpyAlign; |
| 850 | // If dest is not aligned enough and we can't increase its alignment then |
| 851 | // bail out. |
| 852 | if (!isDestSufficientlyAligned && !isa<AllocaInst>(cpyDest)) |
| 853 | return false; |
| 854 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 855 | // Check that src is not accessed except via the call and the memcpy. This |
| 856 | // guarantees that it holds only undefined values when passed in (so the final |
| 857 | // memcpy can be dropped), that it is not read or written between the call and |
| 858 | // the memcpy, and that writing beyond the end of it is undefined. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 859 | SmallVector<User*, 8> srcUseList(srcAlloca->user_begin(), |
| 860 | srcAlloca->user_end()); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 861 | while (!srcUseList.empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 862 | User *U = srcUseList.pop_back_val(); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 863 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 864 | if (isa<BitCastInst>(U) || isa<AddrSpaceCastInst>(U)) { |
| 865 | for (User *UU : U->users()) |
| 866 | srcUseList.push_back(UU); |
Chandler Carruth | 18cee1d | 2014-09-01 10:09:18 +0000 | [diff] [blame] | 867 | continue; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 868 | } |
Chandler Carruth | 18cee1d | 2014-09-01 10:09:18 +0000 | [diff] [blame] | 869 | if (GetElementPtrInst *G = dyn_cast<GetElementPtrInst>(U)) { |
| 870 | if (!G->hasAllZeroIndices()) |
| 871 | return false; |
| 872 | |
| 873 | for (User *UU : U->users()) |
| 874 | srcUseList.push_back(UU); |
| 875 | continue; |
| 876 | } |
| 877 | if (const IntrinsicInst *IT = dyn_cast<IntrinsicInst>(U)) |
| 878 | if (IT->getIntrinsicID() == Intrinsic::lifetime_start || |
| 879 | IT->getIntrinsicID() == Intrinsic::lifetime_end) |
| 880 | continue; |
| 881 | |
| 882 | if (U != C && U != cpy) |
| 883 | return false; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Nick Lewycky | 703e488 | 2014-07-14 18:52:02 +0000 | [diff] [blame] | 886 | // Check that src isn't captured by the called function since the |
| 887 | // transformation can cause aliasing issues in that case. |
| 888 | for (unsigned i = 0, e = CS.arg_size(); i != e; ++i) |
| 889 | if (CS.getArgument(i) == cpySrc && !CS.doesNotCapture(i)) |
| 890 | return false; |
| 891 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 892 | // Since we're changing the parameter to the callsite, we need to make sure |
| 893 | // that what would be the new parameter dominates the callsite. |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 894 | DominatorTree &DT = LookupDomTree(); |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 895 | if (Instruction *cpyDestInst = dyn_cast<Instruction>(cpyDest)) |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 896 | if (!DT.dominates(cpyDestInst, C)) |
| 897 | return false; |
| 898 | |
| 899 | // In addition to knowing that the call does not access src in some |
| 900 | // unexpected manner, for example via a global, which we deduce from |
| 901 | // the use analysis, we also need to know that it does not sneakily |
| 902 | // access dest. We rely on AA to figure this out for us. |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 903 | AliasAnalysis &AA = LookupAliasAnalysis(); |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 904 | ModRefInfo MR = AA.getModRefInfo(C, cpyDest, srcSize); |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 905 | // If necessary, perform additional analysis. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 906 | if (MR != MRI_NoModRef) |
Chad Rosier | a968caf | 2012-05-14 20:35:04 +0000 | [diff] [blame] | 907 | MR = AA.callCapturesBefore(C, cpyDest, srcSize, &DT); |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 908 | if (MR != MRI_NoModRef) |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 909 | return false; |
| 910 | |
| 911 | // All the checks have passed, so do the transformation. |
Owen Anderson | d071a87 | 2008-06-01 21:52:16 +0000 | [diff] [blame] | 912 | bool changedArgument = false; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 913 | for (unsigned i = 0; i < CS.arg_size(); ++i) |
Owen Anderson | 38099c1 | 2008-06-01 22:26:26 +0000 | [diff] [blame] | 914 | if (CS.getArgument(i)->stripPointerCasts() == cpySrc) { |
Duncan Sands | a6d2001 | 2012-10-04 13:53:21 +0000 | [diff] [blame] | 915 | Value *Dest = cpySrc->getType() == cpyDest->getType() ? cpyDest |
| 916 | : CastInst::CreatePointerCast(cpyDest, cpySrc->getType(), |
| 917 | cpyDest->getName(), C); |
Owen Anderson | d071a87 | 2008-06-01 21:52:16 +0000 | [diff] [blame] | 918 | changedArgument = true; |
Duncan Sands | a6d2001 | 2012-10-04 13:53:21 +0000 | [diff] [blame] | 919 | if (CS.getArgument(i)->getType() == Dest->getType()) |
| 920 | CS.setArgument(i, Dest); |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 921 | else |
Duncan Sands | a6d2001 | 2012-10-04 13:53:21 +0000 | [diff] [blame] | 922 | CS.setArgument(i, CastInst::CreatePointerCast(Dest, |
| 923 | CS.getArgument(i)->getType(), Dest->getName(), C)); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Owen Anderson | d071a87 | 2008-06-01 21:52:16 +0000 | [diff] [blame] | 926 | if (!changedArgument) |
| 927 | return false; |
| 928 | |
Duncan Sands | c6ada69 | 2012-10-04 10:54:40 +0000 | [diff] [blame] | 929 | // If the destination wasn't sufficiently aligned then increase its alignment. |
| 930 | if (!isDestSufficientlyAligned) { |
| 931 | assert(isa<AllocaInst>(cpyDest) && "Can only increase alloca alignment!"); |
| 932 | cast<AllocaInst>(cpyDest)->setAlignment(srcAlign); |
| 933 | } |
| 934 | |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 935 | // Drop any cached information about the call, because we may have changed |
| 936 | // its dependence information by changing its parameter. |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 937 | MD->removeInstruction(C); |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 938 | |
Bjorn Steinbrink | 71bf3b8 | 2015-02-07 17:54:36 +0000 | [diff] [blame] | 939 | // Update AA metadata |
| 940 | // FIXME: MD_tbaa_struct and MD_mem_parallel_loop_access should also be |
| 941 | // handled here, but combineMetadata doesn't support them yet |
Piotr Padlewski | dc9b2cf | 2015-10-02 22:12:22 +0000 | [diff] [blame] | 942 | unsigned KnownIDs[] = {LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope, |
| 943 | LLVMContext::MD_noalias, |
| 944 | LLVMContext::MD_invariant_group}; |
Bjorn Steinbrink | 71bf3b8 | 2015-02-07 17:54:36 +0000 | [diff] [blame] | 945 | combineMetadata(C, cpy, KnownIDs); |
| 946 | |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 947 | // Remove the memcpy. |
| 948 | MD->removeInstruction(cpy); |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 949 | ++NumMemCpyInstr; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 950 | |
| 951 | return true; |
| 952 | } |
| 953 | |
Sanjay Patel | a75c41e | 2015-08-13 22:53:20 +0000 | [diff] [blame] | 954 | /// We've found that the (upward scanning) memory dependence of memcpy 'M' is |
| 955 | /// the memcpy 'MDep'. Try to simplify M to copy from MDep's input if we can. |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 956 | bool MemCpyOptPass::processMemCpyMemCpyDependence(MemCpyInst *M, |
| 957 | MemCpyInst *MDep) { |
Chris Lattner | 7e9b2ea | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 958 | // We can only transforms memcpy's where the dest of one is the source of the |
| 959 | // other. |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 960 | if (M->getSource() != MDep->getDest() || MDep->isVolatile()) |
Chris Lattner | 7e9b2ea | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 961 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 962 | |
Chris Lattner | fd51c52 | 2010-12-09 07:39:50 +0000 | [diff] [blame] | 963 | // If dep instruction is reading from our current input, then it is a noop |
| 964 | // transfer and substituting the input won't change this instruction. Just |
| 965 | // ignore the input and let someone else zap MDep. This handles cases like: |
| 966 | // memcpy(a <- a) |
| 967 | // memcpy(b <- a) |
| 968 | if (M->getSource() == MDep->getSource()) |
| 969 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 970 | |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 971 | // 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] | 972 | // must be larger than the following one. |
Dan Gohman | 19e30d5 | 2011-01-21 22:07:57 +0000 | [diff] [blame] | 973 | ConstantInt *MDepLen = dyn_cast<ConstantInt>(MDep->getLength()); |
| 974 | ConstantInt *MLen = dyn_cast<ConstantInt>(M->getLength()); |
| 975 | if (!MDepLen || !MLen || MDepLen->getZExtValue() < MLen->getZExtValue()) |
| 976 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 977 | |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 978 | AliasAnalysis &AA = LookupAliasAnalysis(); |
Chris Lattner | 5957229 | 2010-11-21 08:06:10 +0000 | [diff] [blame] | 979 | |
| 980 | // Verify that the copied-from memory doesn't change in between the two |
| 981 | // transfers. For example, in: |
| 982 | // memcpy(a <- b) |
| 983 | // *b = 42; |
| 984 | // memcpy(c <- a) |
| 985 | // It would be invalid to transform the second memcpy into memcpy(c <- b). |
| 986 | // |
| 987 | // TODO: If the code between M and MDep is transparent to the destination "c", |
| 988 | // then we could still perform the xform by moving M up to the first memcpy. |
| 989 | // |
| 990 | // NOTE: This is conservative, it will stop on any read from the source loc, |
| 991 | // not just the defining memcpy. |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 992 | MemDepResult SourceDep = |
| 993 | MD->getPointerDependencyFrom(MemoryLocation::getForSource(MDep), false, |
| 994 | M->getIterator(), M->getParent()); |
Chris Lattner | 5957229 | 2010-11-21 08:06:10 +0000 | [diff] [blame] | 995 | if (!SourceDep.isClobber() || SourceDep.getInst() != MDep) |
| 996 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 997 | |
Chris Lattner | 731caac | 2010-11-18 08:00:57 +0000 | [diff] [blame] | 998 | // If the dest of the second might alias the source of the first, then the |
| 999 | // source and dest might overlap. We still want to eliminate the intermediate |
| 1000 | // value, but we have to generate a memmove instead of memcpy. |
Chris Lattner | 6cf8d6c | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 1001 | bool UseMemMove = false; |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 1002 | if (!AA.isNoAlias(MemoryLocation::getForDest(M), |
| 1003 | MemoryLocation::getForSource(MDep))) |
Chris Lattner | 6cf8d6c | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 1004 | UseMemMove = true; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1005 | |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1006 | // If all checks passed, then we can transform M. |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1007 | |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 1008 | // Make sure to use the lesser of the alignment of the source and the dest |
| 1009 | // since we're changing where we're reading from, but don't want to increase |
| 1010 | // the alignment past what can be read from or written to. |
Chris Lattner | 7e9b2ea | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 1011 | // TODO: Is this worth it if we're creating a less aligned memcpy? For |
| 1012 | // example we could be moving from movaps -> movq on x86. |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 1013 | unsigned Align = std::min(MDep->getAlignment(), M->getAlignment()); |
| 1014 | |
Chris Lattner | 6cf8d6c | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 1015 | IRBuilder<> Builder(M); |
| 1016 | if (UseMemMove) |
| 1017 | Builder.CreateMemMove(M->getRawDest(), MDep->getRawSource(), M->getLength(), |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 1018 | Align, M->isVolatile()); |
Chris Lattner | 6cf8d6c | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 1019 | else |
| 1020 | Builder.CreateMemCpy(M->getRawDest(), MDep->getRawSource(), M->getLength(), |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 1021 | Align, M->isVolatile()); |
Chris Lattner | 1385dff | 2010-11-18 08:07:09 +0000 | [diff] [blame] | 1022 | |
Chris Lattner | 5957229 | 2010-11-21 08:06:10 +0000 | [diff] [blame] | 1023 | // Remove the instruction we're replacing. |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1024 | MD->removeInstruction(M); |
Chris Lattner | 1385dff | 2010-11-18 08:07:09 +0000 | [diff] [blame] | 1025 | M->eraseFromParent(); |
| 1026 | ++NumMemCpyInstr; |
| 1027 | return true; |
Chris Lattner | 7e9b2ea | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Ahmed Bougacha | 83f78a4 | 2015-04-17 22:20:57 +0000 | [diff] [blame] | 1030 | /// We've found that the (upward scanning) memory dependence of \p MemCpy is |
| 1031 | /// \p MemSet. Try to simplify \p MemSet to only set the trailing bytes that |
| 1032 | /// weren't copied over by \p MemCpy. |
| 1033 | /// |
| 1034 | /// In other words, transform: |
| 1035 | /// \code |
| 1036 | /// memset(dst, c, dst_size); |
| 1037 | /// memcpy(dst, src, src_size); |
| 1038 | /// \endcode |
| 1039 | /// into: |
| 1040 | /// \code |
| 1041 | /// memcpy(dst, src, src_size); |
| 1042 | /// memset(dst + src_size, c, dst_size <= src_size ? 0 : dst_size - src_size); |
| 1043 | /// \endcode |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 1044 | bool MemCpyOptPass::processMemSetMemCpyDependence(MemCpyInst *MemCpy, |
| 1045 | MemSetInst *MemSet) { |
Ahmed Bougacha | 83f78a4 | 2015-04-17 22:20:57 +0000 | [diff] [blame] | 1046 | // We can only transform memset/memcpy with the same destination. |
| 1047 | if (MemSet->getDest() != MemCpy->getDest()) |
| 1048 | return false; |
| 1049 | |
Ahmed Bougacha | 97876fa | 2015-05-21 01:43:39 +0000 | [diff] [blame] | 1050 | // Check that there are no other dependencies on the memset destination. |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 1051 | MemDepResult DstDepInfo = |
| 1052 | MD->getPointerDependencyFrom(MemoryLocation::getForDest(MemSet), false, |
| 1053 | MemCpy->getIterator(), MemCpy->getParent()); |
Ahmed Bougacha | 97876fa | 2015-05-21 01:43:39 +0000 | [diff] [blame] | 1054 | if (DstDepInfo.getInst() != MemSet) |
| 1055 | return false; |
| 1056 | |
Ahmed Bougacha | 9692e30 | 2015-04-21 21:28:33 +0000 | [diff] [blame] | 1057 | // Use the same i8* dest as the memcpy, killing the memset dest if different. |
| 1058 | Value *Dest = MemCpy->getRawDest(); |
Ahmed Bougacha | 83f78a4 | 2015-04-17 22:20:57 +0000 | [diff] [blame] | 1059 | Value *DestSize = MemSet->getLength(); |
| 1060 | Value *SrcSize = MemCpy->getLength(); |
| 1061 | |
| 1062 | // By default, create an unaligned memset. |
| 1063 | unsigned Align = 1; |
| 1064 | // If Dest is aligned, and SrcSize is constant, use the minimum alignment |
| 1065 | // of the sum. |
| 1066 | const unsigned DestAlign = |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 1067 | std::max(MemSet->getAlignment(), MemCpy->getAlignment()); |
Ahmed Bougacha | 83f78a4 | 2015-04-17 22:20:57 +0000 | [diff] [blame] | 1068 | if (DestAlign > 1) |
| 1069 | if (ConstantInt *SrcSizeC = dyn_cast<ConstantInt>(SrcSize)) |
| 1070 | Align = MinAlign(SrcSizeC->getZExtValue(), DestAlign); |
| 1071 | |
Ahmed Bougacha | 97876fa | 2015-05-21 01:43:39 +0000 | [diff] [blame] | 1072 | IRBuilder<> Builder(MemCpy); |
Ahmed Bougacha | 83f78a4 | 2015-04-17 22:20:57 +0000 | [diff] [blame] | 1073 | |
Ahmed Bougacha | 05b72c1 | 2015-04-18 23:06:04 +0000 | [diff] [blame] | 1074 | // If the sizes have different types, zext the smaller one. |
Ahmed Bougacha | 7216ccc | 2015-04-18 17:57:41 +0000 | [diff] [blame] | 1075 | if (DestSize->getType() != SrcSize->getType()) { |
Ahmed Bougacha | 05b72c1 | 2015-04-18 23:06:04 +0000 | [diff] [blame] | 1076 | if (DestSize->getType()->getIntegerBitWidth() > |
| 1077 | SrcSize->getType()->getIntegerBitWidth()) |
| 1078 | SrcSize = Builder.CreateZExt(SrcSize, DestSize->getType()); |
| 1079 | else |
| 1080 | DestSize = Builder.CreateZExt(DestSize, SrcSize->getType()); |
Ahmed Bougacha | 7216ccc | 2015-04-18 17:57:41 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Benjamin Kramer | 1697d39 | 2016-11-07 17:47:28 +0000 | [diff] [blame^] | 1083 | Value *Ule = Builder.CreateICmpULE(DestSize, SrcSize); |
| 1084 | Value *SizeDiff = Builder.CreateSub(DestSize, SrcSize); |
| 1085 | Value *MemsetLen = Builder.CreateSelect( |
| 1086 | Ule, ConstantInt::getNullValue(DestSize->getType()), SizeDiff); |
Ahmed Bougacha | 83f78a4 | 2015-04-17 22:20:57 +0000 | [diff] [blame] | 1087 | Builder.CreateMemSet(Builder.CreateGEP(Dest, SrcSize), MemSet->getOperand(1), |
| 1088 | MemsetLen, Align); |
| 1089 | |
| 1090 | MD->removeInstruction(MemSet); |
| 1091 | MemSet->eraseFromParent(); |
| 1092 | return true; |
| 1093 | } |
Chris Lattner | 7e9b2ea | 2010-11-18 07:02:37 +0000 | [diff] [blame] | 1094 | |
Ahmed Bougacha | f8fa3b8 | 2015-05-16 01:32:26 +0000 | [diff] [blame] | 1095 | /// Transform memcpy to memset when its source was just memset. |
| 1096 | /// In other words, turn: |
| 1097 | /// \code |
| 1098 | /// memset(dst1, c, dst1_size); |
| 1099 | /// memcpy(dst2, dst1, dst2_size); |
| 1100 | /// \endcode |
| 1101 | /// into: |
| 1102 | /// \code |
| 1103 | /// memset(dst1, c, dst1_size); |
| 1104 | /// memset(dst2, c, dst2_size); |
| 1105 | /// \endcode |
| 1106 | /// When dst2_size <= dst1_size. |
| 1107 | /// |
| 1108 | /// The \p MemCpy must have a Constant length. |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 1109 | bool MemCpyOptPass::performMemCpyToMemSetOptzn(MemCpyInst *MemCpy, |
| 1110 | MemSetInst *MemSet) { |
Tim Shen | a3dbead | 2016-08-25 19:27:26 +0000 | [diff] [blame] | 1111 | AliasAnalysis &AA = LookupAliasAnalysis(); |
| 1112 | |
Tim Shen | 3ad8b43 | 2016-08-25 21:03:46 +0000 | [diff] [blame] | 1113 | // Make sure that memcpy(..., memset(...), ...), that is we are memsetting and |
| 1114 | // memcpying from the same address. Otherwise it is hard to reason about. |
Tim Shen | a3dbead | 2016-08-25 19:27:26 +0000 | [diff] [blame] | 1115 | if (!AA.isMustAlias(MemSet->getRawDest(), MemCpy->getRawSource())) |
Ahmed Bougacha | f8fa3b8 | 2015-05-16 01:32:26 +0000 | [diff] [blame] | 1116 | return false; |
| 1117 | |
| 1118 | ConstantInt *CopySize = cast<ConstantInt>(MemCpy->getLength()); |
| 1119 | ConstantInt *MemSetSize = dyn_cast<ConstantInt>(MemSet->getLength()); |
| 1120 | // Make sure the memcpy doesn't read any more than what the memset wrote. |
| 1121 | // Don't worry about sizes larger than i64. |
| 1122 | if (!MemSetSize || CopySize->getZExtValue() > MemSetSize->getZExtValue()) |
| 1123 | return false; |
| 1124 | |
Ahmed Bougacha | 0541c67 | 2015-05-21 00:08:35 +0000 | [diff] [blame] | 1125 | IRBuilder<> Builder(MemCpy); |
Ahmed Bougacha | f8fa3b8 | 2015-05-16 01:32:26 +0000 | [diff] [blame] | 1126 | Builder.CreateMemSet(MemCpy->getRawDest(), MemSet->getOperand(1), |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 1127 | CopySize, MemCpy->getAlignment()); |
Ahmed Bougacha | f8fa3b8 | 2015-05-16 01:32:26 +0000 | [diff] [blame] | 1128 | return true; |
| 1129 | } |
| 1130 | |
Sanjay Patel | a75c41e | 2015-08-13 22:53:20 +0000 | [diff] [blame] | 1131 | /// Perform simplification of memcpy's. If we have memcpy A |
Gabor Greif | 62f0aac | 2010-07-28 22:50:26 +0000 | [diff] [blame] | 1132 | /// which copies X to Y, and memcpy B which copies Y to Z, then we can rewrite |
| 1133 | /// B to be a memcpy from X to Z (or potentially a memmove, depending on |
| 1134 | /// circumstances). This allows later passes to remove the first memcpy |
| 1135 | /// altogether. |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 1136 | bool MemCpyOptPass::processMemCpy(MemCpyInst *M) { |
Nick Lewycky | 00703e7 | 2014-02-04 00:18:54 +0000 | [diff] [blame] | 1137 | // We can only optimize non-volatile memcpy's. |
| 1138 | if (M->isVolatile()) return false; |
Owen Anderson | 18e4fed | 2010-10-15 22:52:12 +0000 | [diff] [blame] | 1139 | |
Chris Lattner | bc4457e | 2010-12-09 07:45:45 +0000 | [diff] [blame] | 1140 | // If the source and destination of the memcpy are the same, then zap it. |
| 1141 | if (M->getSource() == M->getDest()) { |
| 1142 | MD->removeInstruction(M); |
| 1143 | M->eraseFromParent(); |
| 1144 | return false; |
| 1145 | } |
Benjamin Kramer | ea9152e | 2010-12-24 21:17:12 +0000 | [diff] [blame] | 1146 | |
| 1147 | // 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] | 1148 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(M->getSource())) |
Benjamin Kramer | 30342fb | 2010-12-26 15:23:45 +0000 | [diff] [blame] | 1149 | if (GV->isConstant() && GV->hasDefinitiveInitializer()) |
Benjamin Kramer | b90b2f0 | 2010-12-24 22:23:59 +0000 | [diff] [blame] | 1150 | if (Value *ByteVal = isBytewiseValue(GV->getInitializer())) { |
Chris Lattner | 6cf8d6c | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 1151 | IRBuilder<> Builder(M); |
Nick Lewycky | 00703e7 | 2014-02-04 00:18:54 +0000 | [diff] [blame] | 1152 | Builder.CreateMemSet(M->getRawDest(), ByteVal, M->getLength(), |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 1153 | M->getAlignment(), false); |
Benjamin Kramer | b90b2f0 | 2010-12-24 22:23:59 +0000 | [diff] [blame] | 1154 | MD->removeInstruction(M); |
| 1155 | M->eraseFromParent(); |
| 1156 | ++NumCpyToSet; |
| 1157 | return true; |
| 1158 | } |
Benjamin Kramer | ea9152e | 2010-12-24 21:17:12 +0000 | [diff] [blame] | 1159 | |
Ahmed Bougacha | b616966 | 2015-05-11 23:09:46 +0000 | [diff] [blame] | 1160 | MemDepResult DepInfo = MD->getDependency(M); |
Ahmed Bougacha | 83f78a4 | 2015-04-17 22:20:57 +0000 | [diff] [blame] | 1161 | |
| 1162 | // Try to turn a partially redundant memset + memcpy into |
| 1163 | // memcpy + smaller memset. We don't need the memcpy size for this. |
Ahmed Bougacha | b616966 | 2015-05-11 23:09:46 +0000 | [diff] [blame] | 1164 | if (DepInfo.isClobber()) |
| 1165 | if (MemSetInst *MDep = dyn_cast<MemSetInst>(DepInfo.getInst())) |
Ahmed Bougacha | 83f78a4 | 2015-04-17 22:20:57 +0000 | [diff] [blame] | 1166 | if (processMemSetMemCpyDependence(M, MDep)) |
| 1167 | return true; |
| 1168 | |
Nick Lewycky | 00703e7 | 2014-02-04 00:18:54 +0000 | [diff] [blame] | 1169 | // The optimizations after this point require the memcpy size. |
| 1170 | ConstantInt *CopySize = dyn_cast<ConstantInt>(M->getLength()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1171 | if (!CopySize) return false; |
Nick Lewycky | 00703e7 | 2014-02-04 00:18:54 +0000 | [diff] [blame] | 1172 | |
Ahmed Bougacha | f8fa3b8 | 2015-05-16 01:32:26 +0000 | [diff] [blame] | 1173 | // There are four possible optimizations we can do for memcpy: |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1174 | // a) memcpy-memcpy xform which exposes redundance for DSE. |
| 1175 | // b) call-memcpy xform for return slot optimization. |
Nick Lewycky | 77d5fb4 | 2014-03-26 23:45:15 +0000 | [diff] [blame] | 1176 | // c) memcpy from freshly alloca'd space or space that has just started its |
| 1177 | // lifetime copies undefined data, and we can therefore eliminate the |
| 1178 | // memcpy in favor of the data that was already at the destination. |
Ahmed Bougacha | f8fa3b8 | 2015-05-16 01:32:26 +0000 | [diff] [blame] | 1179 | // d) memcpy from a just-memset'd source can be turned into memset. |
Nick Lewycky | 0a7e9cc | 2011-10-16 20:13:32 +0000 | [diff] [blame] | 1180 | if (DepInfo.isClobber()) { |
| 1181 | if (CallInst *C = dyn_cast<CallInst>(DepInfo.getInst())) { |
| 1182 | if (performCallSlotOptzn(M, M->getDest(), M->getSource(), |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 1183 | CopySize->getZExtValue(), M->getAlignment(), |
Duncan Sands | c6ada69 | 2012-10-04 10:54:40 +0000 | [diff] [blame] | 1184 | C)) { |
Nick Lewycky | 0a7e9cc | 2011-10-16 20:13:32 +0000 | [diff] [blame] | 1185 | MD->removeInstruction(M); |
| 1186 | M->eraseFromParent(); |
| 1187 | return true; |
| 1188 | } |
Chris Lattner | bc4457e | 2010-12-09 07:45:45 +0000 | [diff] [blame] | 1189 | } |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 1190 | } |
Ahmed Charles | 32e983e | 2012-02-13 06:30:56 +0000 | [diff] [blame] | 1191 | |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 1192 | MemoryLocation SrcLoc = MemoryLocation::getForSource(M); |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 1193 | MemDepResult SrcDepInfo = MD->getPointerDependencyFrom( |
| 1194 | SrcLoc, true, M->getIterator(), M->getParent()); |
Ahmed Bougacha | b616966 | 2015-05-11 23:09:46 +0000 | [diff] [blame] | 1195 | |
Nick Lewycky | 0a7e9cc | 2011-10-16 20:13:32 +0000 | [diff] [blame] | 1196 | if (SrcDepInfo.isClobber()) { |
| 1197 | if (MemCpyInst *MDep = dyn_cast<MemCpyInst>(SrcDepInfo.getInst())) |
Ahmed Bougacha | 15a31f6 | 2015-05-16 01:23:47 +0000 | [diff] [blame] | 1198 | return processMemCpyMemCpyDependence(M, MDep); |
Nick Lewycky | 9938494 | 2014-02-06 06:29:19 +0000 | [diff] [blame] | 1199 | } else if (SrcDepInfo.isDef()) { |
Nick Lewycky | 77d5fb4 | 2014-03-26 23:45:15 +0000 | [diff] [blame] | 1200 | Instruction *I = SrcDepInfo.getInst(); |
| 1201 | bool hasUndefContents = false; |
| 1202 | |
| 1203 | if (isa<AllocaInst>(I)) { |
| 1204 | hasUndefContents = true; |
| 1205 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 1206 | if (II->getIntrinsicID() == Intrinsic::lifetime_start) |
| 1207 | if (ConstantInt *LTSize = dyn_cast<ConstantInt>(II->getArgOperand(0))) |
| 1208 | if (LTSize->getZExtValue() >= CopySize->getZExtValue()) |
| 1209 | hasUndefContents = true; |
| 1210 | } |
| 1211 | |
| 1212 | if (hasUndefContents) { |
Nick Lewycky | 9938494 | 2014-02-06 06:29:19 +0000 | [diff] [blame] | 1213 | MD->removeInstruction(M); |
| 1214 | M->eraseFromParent(); |
| 1215 | ++NumMemCpyInstr; |
| 1216 | return true; |
| 1217 | } |
Nick Lewycky | 0a7e9cc | 2011-10-16 20:13:32 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Ahmed Bougacha | f8fa3b8 | 2015-05-16 01:32:26 +0000 | [diff] [blame] | 1220 | if (SrcDepInfo.isClobber()) |
| 1221 | if (MemSetInst *MDep = dyn_cast<MemSetInst>(SrcDepInfo.getInst())) |
| 1222 | if (performMemCpyToMemSetOptzn(M, MDep)) { |
| 1223 | MD->removeInstruction(M); |
| 1224 | M->eraseFromParent(); |
| 1225 | ++NumCpyToSet; |
| 1226 | return true; |
| 1227 | } |
| 1228 | |
Owen Anderson | ad5367f | 2008-04-29 21:51:00 +0000 | [diff] [blame] | 1229 | return false; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Sanjay Patel | a75c41e | 2015-08-13 22:53:20 +0000 | [diff] [blame] | 1232 | /// Transforms memmove calls to memcpy calls when the src/dst are guaranteed |
| 1233 | /// not to alias. |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 1234 | bool MemCpyOptPass::processMemMove(MemMoveInst *M) { |
| 1235 | AliasAnalysis &AA = LookupAliasAnalysis(); |
Chris Lattner | 1145e33 | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 1236 | |
Chris Lattner | 23f61a0 | 2011-05-01 18:27:11 +0000 | [diff] [blame] | 1237 | if (!TLI->has(LibFunc::memmove)) |
| 1238 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1239 | |
Chris Lattner | 1145e33 | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 1240 | // See if the pointers alias. |
Chandler Carruth | 70c61c1 | 2015-06-04 02:03:15 +0000 | [diff] [blame] | 1241 | if (!AA.isNoAlias(MemoryLocation::getForDest(M), |
| 1242 | MemoryLocation::getForSource(M))) |
Chris Lattner | 1145e33 | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 1243 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1244 | |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 1245 | DEBUG(dbgs() << "MemCpyOptPass: Optimizing memmove -> memcpy: " << *M |
| 1246 | << "\n"); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1247 | |
Chris Lattner | 1145e33 | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 1248 | // If not, then we know we can transform this. |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 1249 | Type *ArgTys[3] = { M->getRawDest()->getType(), |
| 1250 | M->getRawSource()->getType(), |
| 1251 | M->getLength()->getType() }; |
Sanjay Patel | af674fb | 2015-12-14 17:24:23 +0000 | [diff] [blame] | 1252 | M->setCalledFunction(Intrinsic::getDeclaration(M->getModule(), |
| 1253 | Intrinsic::memcpy, ArgTys)); |
Duncan Sands | 0edc710 | 2009-09-03 13:37:16 +0000 | [diff] [blame] | 1254 | |
Chris Lattner | 1145e33 | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 1255 | // MemDep may have over conservative information about this instruction, just |
| 1256 | // conservatively flush it from the cache. |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1257 | MD->removeInstruction(M); |
Duncan Sands | 0edc710 | 2009-09-03 13:37:16 +0000 | [diff] [blame] | 1258 | |
| 1259 | ++NumMoveToCpy; |
Chris Lattner | 1145e33 | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 1260 | return true; |
| 1261 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1262 | |
Sanjay Patel | a75c41e | 2015-08-13 22:53:20 +0000 | [diff] [blame] | 1263 | /// This is called on every byval argument in call sites. |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 1264 | bool MemCpyOptPass::processByValArgument(CallSite CS, unsigned ArgNo) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1265 | const DataLayout &DL = CS.getCaller()->getParent()->getDataLayout(); |
Chris Lattner | 5957229 | 2010-11-21 08:06:10 +0000 | [diff] [blame] | 1266 | // Find out what feeds this byval argument. |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1267 | Value *ByValArg = CS.getArgument(ArgNo); |
Nick Lewycky | c585de6 | 2011-10-12 00:14:31 +0000 | [diff] [blame] | 1268 | Type *ByValTy = cast<PointerType>(ByValArg->getType())->getElementType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1269 | uint64_t ByValSize = DL.getTypeAllocSize(ByValTy); |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 1270 | MemDepResult DepInfo = MD->getPointerDependencyFrom( |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 1271 | MemoryLocation(ByValArg, ByValSize), true, |
| 1272 | CS.getInstruction()->getIterator(), CS.getInstruction()->getParent()); |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1273 | if (!DepInfo.isClobber()) |
| 1274 | return false; |
| 1275 | |
| 1276 | // If the byval argument isn't fed by a memcpy, ignore it. If it is fed by |
| 1277 | // a memcpy, see if we can byval from the source of the memcpy instead of the |
| 1278 | // result. |
| 1279 | MemCpyInst *MDep = dyn_cast<MemCpyInst>(DepInfo.getInst()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1280 | if (!MDep || MDep->isVolatile() || |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1281 | ByValArg->stripPointerCasts() != MDep->getDest()) |
| 1282 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1283 | |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1284 | // 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] | 1285 | ConstantInt *C1 = dyn_cast<ConstantInt>(MDep->getLength()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1286 | if (!C1 || C1->getValue().getZExtValue() < ByValSize) |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1287 | return false; |
| 1288 | |
Chris Lattner | 83791ce | 2011-05-23 00:03:39 +0000 | [diff] [blame] | 1289 | // Get the alignment of the byval. If the call doesn't specify the alignment, |
| 1290 | // then it is some target specific value that we can't know. |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1291 | unsigned ByValAlign = CS.getParamAlignment(ArgNo+1); |
Chris Lattner | 83791ce | 2011-05-23 00:03:39 +0000 | [diff] [blame] | 1292 | if (ByValAlign == 0) return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1293 | |
Chris Lattner | 83791ce | 2011-05-23 00:03:39 +0000 | [diff] [blame] | 1294 | // If it is greater than the memcpy, then we check to see if we can force the |
| 1295 | // source of the memcpy to the alignment we need. If we fail, we bail out. |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 1296 | AssumptionCache &AC = LookupAssumptionCache(); |
| 1297 | DominatorTree &DT = LookupDomTree(); |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 1298 | if (MDep->getAlignment() < ByValAlign && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1299 | getOrEnforceKnownAlignment(MDep->getSource(), ByValAlign, DL, |
| 1300 | CS.getInstruction(), &AC, &DT) < ByValAlign) |
Chris Lattner | 83791ce | 2011-05-23 00:03:39 +0000 | [diff] [blame] | 1301 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1302 | |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1303 | // Verify that the copied-from memory doesn't change in between the memcpy and |
| 1304 | // the byval call. |
| 1305 | // memcpy(a <- b) |
| 1306 | // *b = 42; |
| 1307 | // foo(*a) |
| 1308 | // It would be invalid to transform the second memcpy into foo(*b). |
Chris Lattner | 5957229 | 2010-11-21 08:06:10 +0000 | [diff] [blame] | 1309 | // |
| 1310 | // NOTE: This is conservative, it will stop on any read from the source loc, |
| 1311 | // not just the defining memcpy. |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 1312 | MemDepResult SourceDep = MD->getPointerDependencyFrom( |
| 1313 | MemoryLocation::getForSource(MDep), false, |
| 1314 | CS.getInstruction()->getIterator(), MDep->getParent()); |
Chris Lattner | 5957229 | 2010-11-21 08:06:10 +0000 | [diff] [blame] | 1315 | if (!SourceDep.isClobber() || SourceDep.getInst() != MDep) |
| 1316 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1317 | |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1318 | Value *TmpCast = MDep->getSource(); |
| 1319 | if (MDep->getSource()->getType() != ByValArg->getType()) |
| 1320 | TmpCast = new BitCastInst(MDep->getSource(), ByValArg->getType(), |
| 1321 | "tmpcast", CS.getInstruction()); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1322 | |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 1323 | DEBUG(dbgs() << "MemCpyOptPass: Forwarding memcpy to byval:\n" |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1324 | << " " << *MDep << "\n" |
| 1325 | << " " << *CS.getInstruction() << "\n"); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1326 | |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1327 | // Otherwise we're good! Update the byval argument. |
| 1328 | CS.setArgument(ArgNo, TmpCast); |
| 1329 | ++NumMemCpyInstr; |
| 1330 | return true; |
| 1331 | } |
| 1332 | |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 1333 | /// Executes one iteration of MemCpyOptPass. |
| 1334 | bool MemCpyOptPass::iterateOnFunction(Function &F) { |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1335 | bool MadeChange = false; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 1336 | |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1337 | // Walk all instruction in the function. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 1338 | for (BasicBlock &BB : F) { |
| 1339 | for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) { |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1340 | // Avoid invalidating the iterator. |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 1341 | Instruction *I = &*BI++; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1342 | |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1343 | bool RepeatInstruction = false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1344 | |
Owen Anderson | 6a7355c | 2008-04-21 07:45:10 +0000 | [diff] [blame] | 1345 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1346 | MadeChange |= processStore(SI, BI); |
Chris Lattner | 9a1d63b | 2011-01-08 21:19:19 +0000 | [diff] [blame] | 1347 | else if (MemSetInst *M = dyn_cast<MemSetInst>(I)) |
| 1348 | RepeatInstruction = processMemSet(M, BI); |
| 1349 | else if (MemCpyInst *M = dyn_cast<MemCpyInst>(I)) |
Tim Northover | 3961735 | 2016-05-10 21:49:40 +0000 | [diff] [blame] | 1350 | RepeatInstruction = processMemCpy(M); |
Chris Lattner | 9a1d63b | 2011-01-08 21:19:19 +0000 | [diff] [blame] | 1351 | else if (MemMoveInst *M = dyn_cast<MemMoveInst>(I)) |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1352 | RepeatInstruction = processMemMove(M); |
Benjamin Kramer | 3a09ef6 | 2015-04-10 14:50:08 +0000 | [diff] [blame] | 1353 | else if (auto CS = CallSite(I)) { |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1354 | for (unsigned i = 0, e = CS.arg_size(); i != e; ++i) |
Nick Lewycky | 612d70b | 2011-11-20 19:09:04 +0000 | [diff] [blame] | 1355 | if (CS.isByValArgument(i)) |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1356 | MadeChange |= processByValArgument(CS, i); |
| 1357 | } |
| 1358 | |
| 1359 | // Reprocess the instruction if desired. |
| 1360 | if (RepeatInstruction) { |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 1361 | if (BI != BB.begin()) |
| 1362 | --BI; |
Chris Lattner | 58f9f58 | 2010-11-21 00:28:59 +0000 | [diff] [blame] | 1363 | MadeChange = true; |
Chris Lattner | 1145e33 | 2009-09-01 17:56:32 +0000 | [diff] [blame] | 1364 | } |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 1365 | } |
| 1366 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1367 | |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1368 | return MadeChange; |
Owen Anderson | ef9a6fd | 2008-04-09 08:23:16 +0000 | [diff] [blame] | 1369 | } |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1370 | |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 1371 | PreservedAnalyses MemCpyOptPass::run(Function &F, FunctionAnalysisManager &AM) { |
Paul Robinson | af4e64d | 2014-02-06 00:07:05 +0000 | [diff] [blame] | 1372 | |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 1373 | auto &MD = AM.getResult<MemoryDependenceAnalysis>(F); |
| 1374 | auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); |
| 1375 | |
| 1376 | auto LookupAliasAnalysis = [&]() -> AliasAnalysis & { |
| 1377 | return AM.getResult<AAManager>(F); |
| 1378 | }; |
| 1379 | auto LookupAssumptionCache = [&]() -> AssumptionCache & { |
| 1380 | return AM.getResult<AssumptionAnalysis>(F); |
| 1381 | }; |
| 1382 | auto LookupDomTree = [&]() -> DominatorTree & { |
| 1383 | return AM.getResult<DominatorTreeAnalysis>(F); |
| 1384 | }; |
| 1385 | |
| 1386 | bool MadeChange = runImpl(F, &MD, &TLI, LookupAliasAnalysis, |
| 1387 | LookupAssumptionCache, LookupDomTree); |
| 1388 | if (!MadeChange) |
| 1389 | return PreservedAnalyses::all(); |
| 1390 | PreservedAnalyses PA; |
| 1391 | PA.preserve<GlobalsAA>(); |
| 1392 | PA.preserve<MemoryDependenceAnalysis>(); |
| 1393 | return PA; |
| 1394 | } |
| 1395 | |
| 1396 | bool MemCpyOptPass::runImpl( |
| 1397 | Function &F, MemoryDependenceResults *MD_, TargetLibraryInfo *TLI_, |
| 1398 | std::function<AliasAnalysis &()> LookupAliasAnalysis_, |
| 1399 | std::function<AssumptionCache &()> LookupAssumptionCache_, |
| 1400 | std::function<DominatorTree &()> LookupDomTree_) { |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1401 | bool MadeChange = false; |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 1402 | MD = MD_; |
| 1403 | TLI = TLI_; |
Benjamin Kramer | 1afc1de | 2016-06-17 20:41:14 +0000 | [diff] [blame] | 1404 | LookupAliasAnalysis = std::move(LookupAliasAnalysis_); |
| 1405 | LookupAssumptionCache = std::move(LookupAssumptionCache_); |
| 1406 | LookupDomTree = std::move(LookupDomTree_); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1407 | |
Chris Lattner | 23f61a0 | 2011-05-01 18:27:11 +0000 | [diff] [blame] | 1408 | // If we don't have at least memset and memcpy, there is little point of doing |
| 1409 | // anything here. These are required by a freestanding implementation, so if |
| 1410 | // even they are disabled, there is no point in trying hard. |
| 1411 | if (!TLI->has(LibFunc::memset) || !TLI->has(LibFunc::memcpy)) |
| 1412 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1413 | |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1414 | while (1) { |
| 1415 | if (!iterateOnFunction(F)) |
| 1416 | break; |
| 1417 | MadeChange = true; |
| 1418 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1419 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1420 | MD = nullptr; |
Chris Lattner | b5557a7 | 2009-09-01 17:09:55 +0000 | [diff] [blame] | 1421 | return MadeChange; |
| 1422 | } |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 1423 | |
| 1424 | /// This is the main transformation entry point for a function. |
| 1425 | bool MemCpyOptLegacyPass::runOnFunction(Function &F) { |
| 1426 | if (skipFunction(F)) |
| 1427 | return false; |
| 1428 | |
| 1429 | auto *MD = &getAnalysis<MemoryDependenceWrapperPass>().getMemDep(); |
| 1430 | auto *TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
| 1431 | |
| 1432 | auto LookupAliasAnalysis = [this]() -> AliasAnalysis & { |
| 1433 | return getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 1434 | }; |
| 1435 | auto LookupAssumptionCache = [this, &F]() -> AssumptionCache & { |
| 1436 | return getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
| 1437 | }; |
| 1438 | auto LookupDomTree = [this]() -> DominatorTree & { |
| 1439 | return getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 1440 | }; |
| 1441 | |
| 1442 | return Impl.runImpl(F, MD, TLI, LookupAliasAnalysis, LookupAssumptionCache, |
| 1443 | LookupDomTree); |
| 1444 | } |