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