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