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