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