blob: ced923d6973d8fadd1f169a163fea4e0f8303d68 [file] [log] [blame]
Owen Andersonef9a6fd2008-04-09 08:23:16 +00001//===- MemCpyOptimizer.cpp - Optimize use of memcpy and friends -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass performs various transformations related to eliminating memcpy
11// calls, or transforming sets of stores into memset's.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Amaury Sechetbdb261b2016-03-14 22:52:27 +000016#include "llvm/ADT/DenseSet.h"
Eugene Zelenko306d2992017-10-18 21:46:47 +000017#include "llvm/ADT/None.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000018#include "llvm/ADT/STLExtras.h"
Owen Andersonef9a6fd2008-04-09 08:23:16 +000019#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/Statistic.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000021#include "llvm/ADT/iterator_range.h"
Eugene Zelenko306d2992017-10-18 21:46:47 +000022#include "llvm/Analysis/AliasAnalysis.h"
Eugene Zelenko34c23272017-01-18 00:57:48 +000023#include "llvm/Analysis/AssumptionCache.h"
24#include "llvm/Analysis/GlobalsModRef.h"
25#include "llvm/Analysis/MemoryDependenceAnalysis.h"
26#include "llvm/Analysis/MemoryLocation.h"
27#include "llvm/Analysis/TargetLibraryInfo.h"
David Blaikie31b98d22018-06-04 21:23:21 +000028#include "llvm/Transforms/Utils/Local.h"
Chris Lattner9cb10352010-12-26 20:15:01 +000029#include "llvm/Analysis/ValueTracking.h"
Eugene Zelenko34c23272017-01-18 00:57:48 +000030#include "llvm/IR/Argument.h"
Eugene Zelenko306d2992017-10-18 21:46:47 +000031#include "llvm/IR/BasicBlock.h"
32#include "llvm/IR/CallSite.h"
Eugene Zelenko34c23272017-01-18 00:57:48 +000033#include "llvm/IR/Constants.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/DataLayout.h"
Eugene Zelenko34c23272017-01-18 00:57:48 +000035#include "llvm/IR/DerivedTypes.h"
36#include "llvm/IR/Dominators.h"
37#include "llvm/IR/Function.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000038#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000039#include "llvm/IR/GlobalVariable.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000040#include "llvm/IR/IRBuilder.h"
Eugene Zelenko34c23272017-01-18 00:57:48 +000041#include "llvm/IR/InstrTypes.h"
42#include "llvm/IR/Instruction.h"
43#include "llvm/IR/Instructions.h"
44#include "llvm/IR/IntrinsicInst.h"
45#include "llvm/IR/Intrinsics.h"
Eugene Zelenko34c23272017-01-18 00:57:48 +000046#include "llvm/IR/LLVMContext.h"
47#include "llvm/IR/Module.h"
48#include "llvm/IR/Operator.h"
Eugene Zelenko306d2992017-10-18 21:46:47 +000049#include "llvm/IR/PassManager.h"
Eugene Zelenko34c23272017-01-18 00:57:48 +000050#include "llvm/IR/Type.h"
51#include "llvm/IR/User.h"
52#include "llvm/IR/Value.h"
53#include "llvm/Pass.h"
54#include "llvm/Support/Casting.h"
Owen Andersonef9a6fd2008-04-09 08:23:16 +000055#include "llvm/Support/Debug.h"
Eugene Zelenko34c23272017-01-18 00:57:48 +000056#include "llvm/Support/MathExtras.h"
Chris Lattnerb25de3f2009-08-23 04:37:46 +000057#include "llvm/Support/raw_ostream.h"
Eugene Zelenko34c23272017-01-18 00:57:48 +000058#include "llvm/Transforms/Scalar.h"
Nick Lewyckyf836c892015-07-21 21:56:26 +000059#include <algorithm>
Eugene Zelenko34c23272017-01-18 00:57:48 +000060#include <cassert>
61#include <cstdint>
Eugene Zelenko306d2992017-10-18 21:46:47 +000062#include <utility>
Eugene Zelenko34c23272017-01-18 00:57:48 +000063
Owen Andersonef9a6fd2008-04-09 08:23:16 +000064using namespace llvm;
65
Chandler Carruth964daaa2014-04-22 02:55:47 +000066#define DEBUG_TYPE "memcpyopt"
67
Owen Andersonef9a6fd2008-04-09 08:23:16 +000068STATISTIC(NumMemCpyInstr, "Number of memcpy instructions deleted");
69STATISTIC(NumMemSetInfer, "Number of memsets inferred");
Duncan Sands0edc7102009-09-03 13:37:16 +000070STATISTIC(NumMoveToCpy, "Number of memmoves converted to memcpy");
Benjamin Kramerea9152e2010-12-24 21:17:12 +000071STATISTIC(NumCpyToSet, "Number of memcpys converted to memset");
Owen Andersonef9a6fd2008-04-09 08:23:16 +000072
Benjamin Kramer15a257d2012-09-13 16:29:49 +000073static int64_t GetOffsetFromIndex(const GEPOperator *GEP, unsigned Idx,
Mehdi Aminia28d91d2015-03-10 02:37:25 +000074 bool &VariableIdxFound,
75 const DataLayout &DL) {
Owen Andersonef9a6fd2008-04-09 08:23:16 +000076 // Skip over the first indices.
77 gep_type_iterator GTI = gep_type_begin(GEP);
78 for (unsigned i = 1; i != Idx; ++i, ++GTI)
79 /*skip along*/;
Nadav Rotem465834c2012-07-24 10:51:42 +000080
Owen Andersonef9a6fd2008-04-09 08:23:16 +000081 // Compute the offset implied by the rest of the indices.
82 int64_t Offset = 0;
83 for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
84 ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i));
Craig Topperf40110f2014-04-25 05:29:35 +000085 if (!OpC)
Owen Andersonef9a6fd2008-04-09 08:23:16 +000086 return VariableIdxFound = true;
87 if (OpC->isZero()) continue; // No offset.
88
89 // Handle struct indices, which add their field offset to the pointer.
Peter Collingbourneab85225b2016-12-02 02:24:42 +000090 if (StructType *STy = GTI.getStructTypeOrNull()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +000091 Offset += DL.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
Owen Andersonef9a6fd2008-04-09 08:23:16 +000092 continue;
93 }
Nadav Rotem465834c2012-07-24 10:51:42 +000094
Owen Andersonef9a6fd2008-04-09 08:23:16 +000095 // Otherwise, we have a sequential type like an array or vector. Multiply
96 // the index by the ElementSize.
Mehdi Aminia28d91d2015-03-10 02:37:25 +000097 uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType());
Owen Andersonef9a6fd2008-04-09 08:23:16 +000098 Offset += Size*OpC->getSExtValue();
99 }
100
101 return Offset;
102}
103
Sanjay Patela75c41e2015-08-13 22:53:20 +0000104/// Return true if Ptr1 is provably equal to Ptr2 plus a constant offset, and
105/// return that constant offset. For example, Ptr1 might be &A[42], and Ptr2
106/// might be &A[40]. In this case offset would be -8.
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000107static bool IsPointerOffset(Value *Ptr1, Value *Ptr2, int64_t &Offset,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000108 const DataLayout &DL) {
Chris Lattnerfa7c29d2011-01-12 01:43:46 +0000109 Ptr1 = Ptr1->stripPointerCasts();
110 Ptr2 = Ptr2->stripPointerCasts();
Benjamin Kramer3ef5e462014-03-10 21:05:13 +0000111
112 // Handle the trivial case first.
113 if (Ptr1 == Ptr2) {
114 Offset = 0;
115 return true;
116 }
117
Benjamin Kramer15a257d2012-09-13 16:29:49 +0000118 GEPOperator *GEP1 = dyn_cast<GEPOperator>(Ptr1);
119 GEPOperator *GEP2 = dyn_cast<GEPOperator>(Ptr2);
Nadav Rotem465834c2012-07-24 10:51:42 +0000120
Chris Lattner5120ebf2011-01-08 21:07:56 +0000121 bool VariableIdxFound = false;
122
123 // If one pointer is a GEP and the other isn't, then see if the GEP is a
124 // constant offset from the base, as in "P" and "gep P, 1".
Craig Topperf40110f2014-04-25 05:29:35 +0000125 if (GEP1 && !GEP2 && GEP1->getOperand(0)->stripPointerCasts() == Ptr2) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000126 Offset = -GetOffsetFromIndex(GEP1, 1, VariableIdxFound, DL);
Chris Lattner5120ebf2011-01-08 21:07:56 +0000127 return !VariableIdxFound;
128 }
129
Craig Topperf40110f2014-04-25 05:29:35 +0000130 if (GEP2 && !GEP1 && GEP2->getOperand(0)->stripPointerCasts() == Ptr1) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000131 Offset = GetOffsetFromIndex(GEP2, 1, VariableIdxFound, DL);
Chris Lattner5120ebf2011-01-08 21:07:56 +0000132 return !VariableIdxFound;
133 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000134
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000135 // Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical
136 // base. After that base, they may have some number of common (and
137 // potentially variable) indices. After that they handle some constant
138 // offset, which determines their offset from each other. At this point, we
139 // handle no other case.
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000140 if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0))
141 return false;
Nadav Rotem465834c2012-07-24 10:51:42 +0000142
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000143 // Skip any common indices and track the GEP types.
144 unsigned Idx = 1;
145 for (; Idx != GEP1->getNumOperands() && Idx != GEP2->getNumOperands(); ++Idx)
146 if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx))
147 break;
148
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000149 int64_t Offset1 = GetOffsetFromIndex(GEP1, Idx, VariableIdxFound, DL);
150 int64_t Offset2 = GetOffsetFromIndex(GEP2, Idx, VariableIdxFound, DL);
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000151 if (VariableIdxFound) return false;
Nadav Rotem465834c2012-07-24 10:51:42 +0000152
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000153 Offset = Offset2-Offset1;
154 return true;
155}
156
Eugene Zelenko34c23272017-01-18 00:57:48 +0000157namespace {
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000158
Sanjay Patela75c41e2015-08-13 22:53:20 +0000159/// Represents a range of memset'd bytes with the ByteVal value.
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000160/// This allows us to analyze stores like:
161/// store 0 -> P+1
162/// store 0 -> P+0
163/// store 0 -> P+3
164/// store 0 -> P+2
165/// which sometimes happens with stores to arrays of structs etc. When we see
166/// the first store, we make a range [1, 2). The second store extends the range
167/// to [0, 2). The third makes a new range [2, 3). The fourth store joins the
168/// two ranges into [0, 3) which is memset'able.
Tim Northover39617352016-05-10 21:49:40 +0000169struct MemsetRange {
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000170 // Start/End - A semi range that describes the span that this range covers.
Nadav Rotem465834c2012-07-24 10:51:42 +0000171 // The range is closed at the start and open at the end: [Start, End).
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000172 int64_t Start, End;
173
174 /// StartPtr - The getelementptr instruction that points to the start of the
175 /// range.
Tim Northover39617352016-05-10 21:49:40 +0000176 Value *StartPtr;
Nadav Rotem465834c2012-07-24 10:51:42 +0000177
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000178 /// Alignment - The known alignment of the first store.
179 unsigned Alignment;
Nadav Rotem465834c2012-07-24 10:51:42 +0000180
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000181 /// TheStores - The actual stores that make up this range.
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000182 SmallVector<Instruction*, 16> TheStores;
Nadav Rotem465834c2012-07-24 10:51:42 +0000183
Tim Northover39617352016-05-10 21:49:40 +0000184 bool isProfitableToUseMemset(const DataLayout &DL) const;
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000185};
Eugene Zelenko34c23272017-01-18 00:57:48 +0000186
187} // end anonymous namespace
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000188
Tim Northover39617352016-05-10 21:49:40 +0000189bool MemsetRange::isProfitableToUseMemset(const DataLayout &DL) const {
190 // If we found more than 4 stores to merge or 16 bytes, use memset.
Chad Rosier19446a02011-12-05 22:37:00 +0000191 if (TheStores.size() >= 4 || End-Start >= 16) return true;
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000192
193 // If there is nothing to merge, don't do anything.
194 if (TheStores.size() < 2) return false;
Nadav Rotem465834c2012-07-24 10:51:42 +0000195
Tim Northover39617352016-05-10 21:49:40 +0000196 // If any of the stores are a memset, then it is always good to extend the
197 // memset.
Craig Toppere325e382015-11-20 07:18:48 +0000198 for (Instruction *SI : TheStores)
Tim Northover39617352016-05-10 21:49:40 +0000199 if (!isa<StoreInst>(SI))
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000200 return true;
Nadav Rotem465834c2012-07-24 10:51:42 +0000201
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000202 // Assume that the code generator is capable of merging pairs of stores
203 // together if it wants to.
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000204 if (TheStores.size() == 2) return false;
Nadav Rotem465834c2012-07-24 10:51:42 +0000205
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000206 // If we have fewer than 8 stores, it can still be worthwhile to do this.
207 // For example, merging 4 i8 stores into an i32 store is useful almost always.
208 // However, merging 2 32-bit stores isn't useful on a 32-bit architecture (the
209 // memset will be split into 2 32-bit stores anyway) and doing so can
210 // pessimize the llvm optimizer.
211 //
212 // Since we don't have perfect knowledge here, make some assumptions: assume
Matt Arsenault899f7d22013-09-16 22:43:16 +0000213 // the maximum GPR width is the same size as the largest legal integer
214 // size. If so, check to see whether we will end up actually reducing the
215 // number of stores used.
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000216 unsigned Bytes = unsigned(End-Start);
Jun Bum Limbe11bdc2016-05-13 18:38:35 +0000217 unsigned MaxIntSize = DL.getLargestLegalIntTypeSizeInBits() / 8;
Matt Arsenault899f7d22013-09-16 22:43:16 +0000218 if (MaxIntSize == 0)
219 MaxIntSize = 1;
220 unsigned NumPointerStores = Bytes / MaxIntSize;
Nadav Rotem465834c2012-07-24 10:51:42 +0000221
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000222 // Assume the remaining bytes if any are done a byte at a time.
Craig Toppera5ea5282015-11-21 17:44:42 +0000223 unsigned NumByteStores = Bytes % MaxIntSize;
Nadav Rotem465834c2012-07-24 10:51:42 +0000224
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000225 // If we will reduce the # stores (according to this heuristic), do the
226 // transformation. This encourages merging 4 x i8 -> i32 and 2 x i16 -> i32
227 // etc.
228 return TheStores.size() > NumPointerStores+NumByteStores;
Nadav Rotem465834c2012-07-24 10:51:42 +0000229}
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000230
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000231namespace {
Eugene Zelenko34c23272017-01-18 00:57:48 +0000232
Tim Northover39617352016-05-10 21:49:40 +0000233class MemsetRanges {
Eugene Zelenko306d2992017-10-18 21:46:47 +0000234 using range_iterator = SmallVectorImpl<MemsetRange>::iterator;
235
Sanjay Patela75c41e2015-08-13 22:53:20 +0000236 /// A sorted list of the memset ranges.
Tim Northover39617352016-05-10 21:49:40 +0000237 SmallVector<MemsetRange, 8> Ranges;
Eugene Zelenko306d2992017-10-18 21:46:47 +0000238
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000239 const DataLayout &DL;
Eugene Zelenko34c23272017-01-18 00:57:48 +0000240
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000241public:
Tim Northover39617352016-05-10 21:49:40 +0000242 MemsetRanges(const DataLayout &DL) : DL(DL) {}
Nadav Rotem465834c2012-07-24 10:51:42 +0000243
Eugene Zelenko306d2992017-10-18 21:46:47 +0000244 using const_iterator = SmallVectorImpl<MemsetRange>::const_iterator;
245
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000246 const_iterator begin() const { return Ranges.begin(); }
247 const_iterator end() const { return Ranges.end(); }
248 bool empty() const { return Ranges.empty(); }
Nadav Rotem465834c2012-07-24 10:51:42 +0000249
Chris Lattnerc6381472011-01-08 20:24:01 +0000250 void addInst(int64_t OffsetFromFirst, Instruction *Inst) {
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000251 if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
252 addStore(OffsetFromFirst, SI);
253 else
254 addMemSet(OffsetFromFirst, cast<MemSetInst>(Inst));
Chris Lattnerc6381472011-01-08 20:24:01 +0000255 }
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000256
257 void addStore(int64_t OffsetFromFirst, StoreInst *SI) {
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000258 int64_t StoreSize = DL.getTypeStoreSize(SI->getOperand(0)->getType());
Nadav Rotem465834c2012-07-24 10:51:42 +0000259
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000260 addRange(OffsetFromFirst, StoreSize,
Tim Northover39617352016-05-10 21:49:40 +0000261 SI->getPointerOperand(), SI->getAlignment(), SI);
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000262 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000263
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000264 void addMemSet(int64_t OffsetFromFirst, MemSetInst *MSI) {
265 int64_t Size = cast<ConstantInt>(MSI->getLength())->getZExtValue();
Daniel Neilson6f1eb582018-03-21 14:14:55 +0000266 addRange(OffsetFromFirst, Size, MSI->getDest(), MSI->getDestAlignment(), MSI);
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000267 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000268
Tim Northover39617352016-05-10 21:49:40 +0000269 void addRange(int64_t Start, int64_t Size, Value *Ptr,
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000270 unsigned Alignment, Instruction *Inst);
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000271};
Nadav Rotem465834c2012-07-24 10:51:42 +0000272
Eugene Zelenko34c23272017-01-18 00:57:48 +0000273} // end anonymous namespace
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000274
Tim Northover39617352016-05-10 21:49:40 +0000275/// Add a new store to the MemsetRanges data structure. This adds a
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000276/// new range for the specified store at the specified offset, merging into
277/// existing ranges as appropriate.
Tim Northover39617352016-05-10 21:49:40 +0000278void MemsetRanges::addRange(int64_t Start, int64_t Size, Value *Ptr,
279 unsigned Alignment, Instruction *Inst) {
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000280 int64_t End = Start+Size;
Nadav Rotem465834c2012-07-24 10:51:42 +0000281
Nick Lewyckyf836c892015-07-21 21:56:26 +0000282 range_iterator I = std::lower_bound(Ranges.begin(), Ranges.end(), Start,
Tim Northover39617352016-05-10 21:49:40 +0000283 [](const MemsetRange &LHS, int64_t RHS) { return LHS.End < RHS; });
Nadav Rotem465834c2012-07-24 10:51:42 +0000284
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000285 // We now know that I == E, in which case we didn't find anything to merge
286 // with, or that Start <= I->End. If End < I->Start or I == E, then we need
287 // to insert a new range. Handle this now.
Nick Lewyckyf836c892015-07-21 21:56:26 +0000288 if (I == Ranges.end() || End < I->Start) {
Tim Northover39617352016-05-10 21:49:40 +0000289 MemsetRange &R = *Ranges.insert(I, MemsetRange());
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000290 R.Start = Start;
291 R.End = End;
Tim Northover39617352016-05-10 21:49:40 +0000292 R.StartPtr = Ptr;
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000293 R.Alignment = Alignment;
294 R.TheStores.push_back(Inst);
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000295 return;
296 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000297
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000298 // This store overlaps with I, add it.
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000299 I->TheStores.push_back(Inst);
Nadav Rotem465834c2012-07-24 10:51:42 +0000300
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000301 // At this point, we may have an interval that completely contains our store.
302 // If so, just add it to the interval and return.
303 if (I->Start <= Start && I->End >= End)
304 return;
Nadav Rotem465834c2012-07-24 10:51:42 +0000305
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000306 // Now we know that Start <= I->End and End >= I->Start so the range overlaps
307 // but is not entirely contained within the range.
Nadav Rotem465834c2012-07-24 10:51:42 +0000308
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000309 // See if the range extends the start of the range. In this case, it couldn't
310 // possibly cause it to join the prior range, because otherwise we would have
311 // stopped on *it*.
312 if (Start < I->Start) {
313 I->Start = Start;
Tim Northover39617352016-05-10 21:49:40 +0000314 I->StartPtr = Ptr;
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000315 I->Alignment = Alignment;
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000316 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000317
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000318 // Now we know that Start <= I->End and Start >= I->Start (so the startpoint
319 // is in or right at the end of I), and that End >= I->Start. Extend I out to
320 // End.
321 if (End > I->End) {
322 I->End = End;
Nick Lewyckybfd4ad62009-03-19 05:51:39 +0000323 range_iterator NextI = I;
Nick Lewyckyf836c892015-07-21 21:56:26 +0000324 while (++NextI != Ranges.end() && End >= NextI->Start) {
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000325 // Merge the range in.
326 I->TheStores.append(NextI->TheStores.begin(), NextI->TheStores.end());
327 if (NextI->End > I->End)
328 I->End = NextI->End;
329 Ranges.erase(NextI);
330 NextI = I;
331 }
332 }
333}
334
335//===----------------------------------------------------------------------===//
Sean Silva6347df02016-06-14 02:44:55 +0000336// MemCpyOptLegacyPass Pass
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000337//===----------------------------------------------------------------------===//
338
339namespace {
Eugene Zelenko34c23272017-01-18 00:57:48 +0000340
George Burgess IVecb95f52017-03-08 21:28:19 +0000341class MemCpyOptLegacyPass : public FunctionPass {
342 MemCpyOptPass Impl;
Eugene Zelenko34c23272017-01-18 00:57:48 +0000343
George Burgess IVecb95f52017-03-08 21:28:19 +0000344public:
345 static char ID; // Pass identification, replacement for typeid
Eugene Zelenko34c23272017-01-18 00:57:48 +0000346
George Burgess IVecb95f52017-03-08 21:28:19 +0000347 MemCpyOptLegacyPass() : FunctionPass(ID) {
348 initializeMemCpyOptLegacyPassPass(*PassRegistry::getPassRegistry());
349 }
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000350
George Burgess IVecb95f52017-03-08 21:28:19 +0000351 bool runOnFunction(Function &F) override;
Chris Lattnerc6381472011-01-08 20:24:01 +0000352
George Burgess IVecb95f52017-03-08 21:28:19 +0000353private:
354 // This transformation requires dominator postdominator info
355 void getAnalysisUsage(AnalysisUsage &AU) const override {
356 AU.setPreservesCFG();
357 AU.addRequired<AssumptionCacheTracker>();
358 AU.addRequired<DominatorTreeWrapperPass>();
359 AU.addRequired<MemoryDependenceWrapperPass>();
360 AU.addRequired<AAResultsWrapperPass>();
361 AU.addRequired<TargetLibraryInfoWrapperPass>();
362 AU.addPreserved<GlobalsAAWrapperPass>();
363 AU.addPreserved<MemoryDependenceWrapperPass>();
364 }
365};
Nadav Rotem465834c2012-07-24 10:51:42 +0000366
Eugene Zelenko34c23272017-01-18 00:57:48 +0000367} // end anonymous namespace
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000368
Eugene Zelenko306d2992017-10-18 21:46:47 +0000369char MemCpyOptLegacyPass::ID = 0;
370
Sanjay Patela75c41e2015-08-13 22:53:20 +0000371/// The public interface to this file...
Sean Silva6347df02016-06-14 02:44:55 +0000372FunctionPass *llvm::createMemCpyOptPass() { return new MemCpyOptLegacyPass(); }
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000373
Sean Silva6347df02016-06-14 02:44:55 +0000374INITIALIZE_PASS_BEGIN(MemCpyOptLegacyPass, "memcpyopt", "MemCpy Optimization",
Owen Anderson8ac477f2010-10-12 19:48:12 +0000375 false, false)
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000376INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruth73523022014-01-13 13:07:17 +0000377INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Chandler Carruth61440d22016-03-10 00:55:30 +0000378INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000379INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000380INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
381INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
Sean Silva6347df02016-06-14 02:44:55 +0000382INITIALIZE_PASS_END(MemCpyOptLegacyPass, "memcpyopt", "MemCpy Optimization",
Owen Anderson8ac477f2010-10-12 19:48:12 +0000383 false, false)
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000384
Sanjay Patela75c41e2015-08-13 22:53:20 +0000385/// When scanning forward over instructions, we look for some other patterns to
386/// fold away. In particular, this looks for stores to neighboring locations of
387/// memory. If it sees enough consecutive ones, it attempts to merge them
388/// together into a memcpy/memset.
Sean Silva6347df02016-06-14 02:44:55 +0000389Instruction *MemCpyOptPass::tryMergingIntoMemset(Instruction *StartInst,
390 Value *StartPtr,
391 Value *ByteVal) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000392 const DataLayout &DL = StartInst->getModule()->getDataLayout();
Nadav Rotem465834c2012-07-24 10:51:42 +0000393
Chris Lattnerc6381472011-01-08 20:24:01 +0000394 // Okay, so we now have a single store that can be splatable. Scan to find
395 // all subsequent stores of the same value to offset from the same pointer.
396 // Join these together into ranges, so we can decide whether contiguous blocks
397 // are stored.
Tim Northover39617352016-05-10 21:49:40 +0000398 MemsetRanges Ranges(DL);
Nadav Rotem465834c2012-07-24 10:51:42 +0000399
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000400 BasicBlock::iterator BI(StartInst);
Chandler Carruth9ae926b2018-08-26 09:51:22 +0000401 for (++BI; !BI->isTerminator(); ++BI) {
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000402 if (!isa<StoreInst>(BI) && !isa<MemSetInst>(BI)) {
403 // If the instruction is readnone, ignore it, otherwise bail out. We
404 // don't even allow readonly here because we don't want something like:
Chris Lattnerc6381472011-01-08 20:24:01 +0000405 // A[1] = 2; strlen(A); A[2] = 2; -> memcpy(A, ...); strlen(A).
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000406 if (BI->mayWriteToMemory() || BI->mayReadFromMemory())
407 break;
408 continue;
409 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000410
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000411 if (StoreInst *NextStore = dyn_cast<StoreInst>(BI)) {
412 // If this is a store, see if we can merge it in.
Eli Friedman9a468152011-08-17 22:22:24 +0000413 if (!NextStore->isSimple()) break;
Nadav Rotem465834c2012-07-24 10:51:42 +0000414
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000415 // Check to see if this stored value is of the same byte-splattable value.
JF Bastien73d8e4e2018-09-21 05:17:42 +0000416 Value *StoredByte = isBytewiseValue(NextStore->getOperand(0));
417 if (isa<UndefValue>(ByteVal) && StoredByte)
418 ByteVal = StoredByte;
419 if (ByteVal != StoredByte)
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000420 break;
Nadav Rotem465834c2012-07-24 10:51:42 +0000421
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000422 // Check to see if this store is to a constant offset from the start ptr.
423 int64_t Offset;
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000424 if (!IsPointerOffset(StartPtr, NextStore->getPointerOperand(), Offset,
425 DL))
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000426 break;
Nadav Rotem465834c2012-07-24 10:51:42 +0000427
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000428 Ranges.addStore(Offset, NextStore);
429 } else {
430 MemSetInst *MSI = cast<MemSetInst>(BI);
Nadav Rotem465834c2012-07-24 10:51:42 +0000431
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000432 if (MSI->isVolatile() || ByteVal != MSI->getValue() ||
433 !isa<ConstantInt>(MSI->getLength()))
434 break;
Nadav Rotem465834c2012-07-24 10:51:42 +0000435
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000436 // Check to see if this store is to a constant offset from the start ptr.
437 int64_t Offset;
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000438 if (!IsPointerOffset(StartPtr, MSI->getDest(), Offset, DL))
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000439 break;
Nadav Rotem465834c2012-07-24 10:51:42 +0000440
Chris Lattner4dc1fd92011-01-08 20:54:51 +0000441 Ranges.addMemSet(Offset, MSI);
442 }
Chris Lattnerc6381472011-01-08 20:24:01 +0000443 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000444
Chris Lattnerc6381472011-01-08 20:24:01 +0000445 // If we have no ranges, then we just had a single store with nothing that
446 // could be merged in. This is a very common case of course.
447 if (Ranges.empty())
Craig Topperf40110f2014-04-25 05:29:35 +0000448 return nullptr;
Nadav Rotem465834c2012-07-24 10:51:42 +0000449
Chris Lattnerc6381472011-01-08 20:24:01 +0000450 // If we had at least one store that could be merged in, add the starting
451 // store as well. We try to avoid this unless there is at least something
452 // interesting as a small compile-time optimization.
453 Ranges.addInst(0, StartInst);
454
455 // If we create any memsets, we put it right before the first instruction that
456 // isn't part of the memset block. This ensure that the memset is dominated
457 // by any addressing instruction needed by the start of the block.
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000458 IRBuilder<> Builder(&*BI);
Chris Lattnerc6381472011-01-08 20:24:01 +0000459
460 // Now that we have full information about ranges, loop over the ranges and
461 // emit memset's for anything big enough to be worthwhile.
Craig Topperf40110f2014-04-25 05:29:35 +0000462 Instruction *AMemSet = nullptr;
Tim Northover39617352016-05-10 21:49:40 +0000463 for (const MemsetRange &Range : Ranges) {
Chris Lattnerc6381472011-01-08 20:24:01 +0000464 if (Range.TheStores.size() == 1) continue;
Nadav Rotem465834c2012-07-24 10:51:42 +0000465
Chris Lattnerc6381472011-01-08 20:24:01 +0000466 // If it is profitable to lower this range to memset, do so now.
Tim Northover39617352016-05-10 21:49:40 +0000467 if (!Range.isProfitableToUseMemset(DL))
Chris Lattnerc6381472011-01-08 20:24:01 +0000468 continue;
Nadav Rotem465834c2012-07-24 10:51:42 +0000469
Chris Lattnerc6381472011-01-08 20:24:01 +0000470 // Otherwise, we do want to transform this! Create a new memset.
471 // Get the starting pointer of the block.
Tim Northover39617352016-05-10 21:49:40 +0000472 StartPtr = Range.StartPtr;
Nadav Rotem465834c2012-07-24 10:51:42 +0000473
Tim Northover39617352016-05-10 21:49:40 +0000474 // Determine alignment
475 unsigned Alignment = Range.Alignment;
476 if (Alignment == 0) {
477 Type *EltType =
478 cast<PointerType>(StartPtr->getType())->getElementType();
479 Alignment = DL.getABITypeAlignment(EltType);
480 }
481
Reid Kleckner6d310012017-12-28 05:10:33 +0000482 AMemSet =
483 Builder.CreateMemSet(StartPtr, ByteVal, Range.End-Range.Start, Alignment);
Nadav Rotem465834c2012-07-24 10:51:42 +0000484
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000485 LLVM_DEBUG(dbgs() << "Replace stores:\n"; for (Instruction *SI
486 : Range.TheStores) dbgs()
487 << *SI << '\n';
488 dbgs() << "With: " << *AMemSet << '\n');
Reid Kleckner6d310012017-12-28 05:10:33 +0000489
490 if (!Range.TheStores.empty())
491 AMemSet->setDebugLoc(Range.TheStores[0]->getDebugLoc());
Devang Patelc7e4fa72011-05-04 21:58:58 +0000492
Chris Lattnerc6381472011-01-08 20:24:01 +0000493 // Zap all the stores.
Craig Toppere325e382015-11-20 07:18:48 +0000494 for (Instruction *SI : Range.TheStores) {
495 MD->removeInstruction(SI);
496 SI->eraseFromParent();
Chris Lattner7d6433a2011-01-08 22:19:21 +0000497 }
Chris Lattnerc6381472011-01-08 20:24:01 +0000498 ++NumMemSetInfer;
499 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000500
Chris Lattnerc6381472011-01-08 20:24:01 +0000501 return AMemSet;
502}
503
Daniel Neilson6f1eb582018-03-21 14:14:55 +0000504static unsigned findStoreAlignment(const DataLayout &DL, const StoreInst *SI) {
Tim Northover39617352016-05-10 21:49:40 +0000505 unsigned StoreAlign = SI->getAlignment();
506 if (!StoreAlign)
507 StoreAlign = DL.getABITypeAlignment(SI->getOperand(0)->getType());
Daniel Neilson6f1eb582018-03-21 14:14:55 +0000508 return StoreAlign;
509}
510
511static unsigned findLoadAlignment(const DataLayout &DL, const LoadInst *LI) {
Tim Northover39617352016-05-10 21:49:40 +0000512 unsigned LoadAlign = LI->getAlignment();
513 if (!LoadAlign)
514 LoadAlign = DL.getABITypeAlignment(LI->getType());
Daniel Neilson6f1eb582018-03-21 14:14:55 +0000515 return LoadAlign;
516}
Amaury Secheta0c242c2016-01-05 20:17:48 +0000517
Daniel Neilson6f1eb582018-03-21 14:14:55 +0000518static unsigned findCommonAlignment(const DataLayout &DL, const StoreInst *SI,
519 const LoadInst *LI) {
520 unsigned StoreAlign = findStoreAlignment(DL, SI);
521 unsigned LoadAlign = findLoadAlignment(DL, LI);
522 return MinAlign(StoreAlign, LoadAlign);
Amaury Secheta0c242c2016-01-05 20:17:48 +0000523}
Chris Lattnerc6381472011-01-08 20:24:01 +0000524
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000525// This method try to lift a store instruction before position P.
526// It will lift the store and its argument + that anything that
David Majnemerd99068d2016-05-26 19:24:24 +0000527// may alias with these.
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000528// The method returns true if it was successful.
Bryant Wong7cb74462016-12-27 17:58:12 +0000529static bool moveUp(AliasAnalysis &AA, StoreInst *SI, Instruction *P,
530 const LoadInst *LI) {
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000531 // If the store alias this position, early bail out.
532 MemoryLocation StoreLoc = MemoryLocation::get(SI);
Alina Sbirlea63d22502017-12-05 20:12:23 +0000533 if (isModOrRefSet(AA.getModRefInfo(P, StoreLoc)))
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000534 return false;
535
536 // Keep track of the arguments of all instruction we plan to lift
Hiroshi Inouef2096492018-06-14 05:41:49 +0000537 // so we can make sure to lift them as well if appropriate.
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000538 DenseSet<Instruction*> Args;
539 if (auto *Ptr = dyn_cast<Instruction>(SI->getPointerOperand()))
540 if (Ptr->getParent() == SI->getParent())
541 Args.insert(Ptr);
542
543 // Instruction to lift before P.
544 SmallVector<Instruction*, 8> ToLift;
545
546 // Memory locations of lifted instructions.
Bryant Wong7cb74462016-12-27 17:58:12 +0000547 SmallVector<MemoryLocation, 8> MemLocs{StoreLoc};
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000548
Chandler Carruth363ac682019-01-07 05:42:51 +0000549 // Lifted calls.
550 SmallVector<const CallBase *, 8> Calls;
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000551
Bryant Wong7cb74462016-12-27 17:58:12 +0000552 const MemoryLocation LoadLoc = MemoryLocation::get(LI);
553
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000554 for (auto I = --SI->getIterator(), E = P->getIterator(); I != E; --I) {
555 auto *C = &*I;
556
Alina Sbirlea63d22502017-12-05 20:12:23 +0000557 bool MayAlias = isModOrRefSet(AA.getModRefInfo(C, None));
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000558
559 bool NeedLift = false;
560 if (Args.erase(C))
561 NeedLift = true;
562 else if (MayAlias) {
Eugene Zelenko34c23272017-01-18 00:57:48 +0000563 NeedLift = llvm::any_of(MemLocs, [C, &AA](const MemoryLocation &ML) {
Alina Sbirlea63d22502017-12-05 20:12:23 +0000564 return isModOrRefSet(AA.getModRefInfo(C, ML));
David Majnemer0a16c222016-08-11 21:15:00 +0000565 });
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000566
567 if (!NeedLift)
Chandler Carruth363ac682019-01-07 05:42:51 +0000568 NeedLift = llvm::any_of(Calls, [C, &AA](const CallBase *Call) {
569 return isModOrRefSet(AA.getModRefInfo(C, Call));
570 });
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000571 }
572
573 if (!NeedLift)
574 continue;
575
576 if (MayAlias) {
Bryant Wong7cb74462016-12-27 17:58:12 +0000577 // Since LI is implicitly moved downwards past the lifted instructions,
578 // none of them may modify its source.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000579 if (isModSet(AA.getModRefInfo(C, LoadLoc)))
Bryant Wong7cb74462016-12-27 17:58:12 +0000580 return false;
Chandler Carruth363ac682019-01-07 05:42:51 +0000581 else if (const auto *Call = dyn_cast<CallBase>(C)) {
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000582 // If we can't lift this before P, it's game over.
Chandler Carruth363ac682019-01-07 05:42:51 +0000583 if (isModOrRefSet(AA.getModRefInfo(P, Call)))
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000584 return false;
585
Chandler Carruth363ac682019-01-07 05:42:51 +0000586 Calls.push_back(Call);
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000587 } else if (isa<LoadInst>(C) || isa<StoreInst>(C) || isa<VAArgInst>(C)) {
588 // If we can't lift this before P, it's game over.
589 auto ML = MemoryLocation::get(C);
Alina Sbirlea63d22502017-12-05 20:12:23 +0000590 if (isModOrRefSet(AA.getModRefInfo(P, ML)))
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000591 return false;
592
593 MemLocs.push_back(ML);
594 } else
595 // We don't know how to lift this instruction.
596 return false;
597 }
598
599 ToLift.push_back(C);
600 for (unsigned k = 0, e = C->getNumOperands(); k != e; ++k)
601 if (auto *A = dyn_cast<Instruction>(C->getOperand(k)))
602 if (A->getParent() == SI->getParent())
603 Args.insert(A);
604 }
605
606 // We made it, we need to lift
Eugene Zelenko34c23272017-01-18 00:57:48 +0000607 for (auto *I : llvm::reverse(ToLift)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000608 LLVM_DEBUG(dbgs() << "Lifting " << *I << " before " << *P << "\n");
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000609 I->moveBefore(P);
610 }
611
612 return true;
613}
614
Sean Silva6347df02016-06-14 02:44:55 +0000615bool MemCpyOptPass::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
Eli Friedman9a468152011-08-17 22:22:24 +0000616 if (!SI->isSimple()) return false;
Andrea Di Biagio99493df2015-10-09 10:53:41 +0000617
618 // Avoid merging nontemporal stores since the resulting
619 // memcpy/memset would not be able to preserve the nontemporal hint.
620 // In theory we could teach how to propagate the !nontemporal metadata to
621 // memset calls. However, that change would force the backend to
622 // conservatively expand !nontemporal memset calls back to sequences of
623 // store instructions (effectively undoing the merging).
624 if (SI->getMetadata(LLVMContext::MD_nontemporal))
625 return false;
626
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000627 const DataLayout &DL = SI->getModule()->getDataLayout();
Owen Anderson18e4fed2010-10-15 22:52:12 +0000628
Amaury Secheta0c242c2016-01-05 20:17:48 +0000629 // Load to store forwarding can be interpreted as memcpy.
Owen Anderson18e4fed2010-10-15 22:52:12 +0000630 if (LoadInst *LI = dyn_cast<LoadInst>(SI->getOperand(0))) {
Eli Friedman9a468152011-08-17 22:22:24 +0000631 if (LI->isSimple() && LI->hasOneUse() &&
Eli Friedmane8bbc102011-06-15 01:25:56 +0000632 LI->getParent() == SI->getParent()) {
Amaury Secheta0c242c2016-01-05 20:17:48 +0000633
634 auto *T = LI->getType();
635 if (T->isAggregateType()) {
Sean Silva6347df02016-06-14 02:44:55 +0000636 AliasAnalysis &AA = LookupAliasAnalysis();
Amaury Secheta0c242c2016-01-05 20:17:48 +0000637 MemoryLocation LoadLoc = MemoryLocation::get(LI);
638
639 // We use alias analysis to check if an instruction may store to
640 // the memory we load from in between the load and the store. If
Amaury Sechetd3b2c0f2016-01-06 09:30:39 +0000641 // such an instruction is found, we try to promote there instead
642 // of at the store position.
643 Instruction *P = SI;
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000644 for (auto &I : make_range(++LI->getIterator(), SI->getIterator())) {
Alina Sbirlea63d22502017-12-05 20:12:23 +0000645 if (isModSet(AA.getModRefInfo(&I, LoadLoc))) {
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000646 P = &I;
647 break;
Amaury Secheta0c242c2016-01-05 20:17:48 +0000648 }
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000649 }
Amaury Sechetd3b2c0f2016-01-06 09:30:39 +0000650
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000651 // We found an instruction that may write to the loaded memory.
652 // We can try to promote at this position instead of the store
653 // position if nothing alias the store memory after this and the store
654 // destination is not in the range.
655 if (P && P != SI) {
Bryant Wong7cb74462016-12-27 17:58:12 +0000656 if (!moveUp(AA, SI, P, LI))
Amaury Sechetbdb261b2016-03-14 22:52:27 +0000657 P = nullptr;
Amaury Secheta0c242c2016-01-05 20:17:48 +0000658 }
659
Amaury Sechetd3b2c0f2016-01-06 09:30:39 +0000660 // If a valid insertion position is found, then we can promote
661 // the load/store pair to a memcpy.
662 if (P) {
Amaury Secheta0c242c2016-01-05 20:17:48 +0000663 // If we load from memory that may alias the memory we store to,
664 // memmove must be used to preserve semantic. If not, memcpy can
665 // be used.
666 bool UseMemMove = false;
667 if (!AA.isNoAlias(MemoryLocation::get(SI), LoadLoc))
668 UseMemMove = true;
669
Amaury Secheta0c242c2016-01-05 20:17:48 +0000670 uint64_t Size = DL.getTypeStoreSize(T);
671
Amaury Sechetd3b2c0f2016-01-06 09:30:39 +0000672 IRBuilder<> Builder(P);
Amaury Secheta0c242c2016-01-05 20:17:48 +0000673 Instruction *M;
674 if (UseMemMove)
Daniel Neilson6f1eb582018-03-21 14:14:55 +0000675 M = Builder.CreateMemMove(
676 SI->getPointerOperand(), findStoreAlignment(DL, SI),
Xin Tong47beee22019-01-04 02:13:22 +0000677 LI->getPointerOperand(), findLoadAlignment(DL, LI), Size);
Amaury Secheta0c242c2016-01-05 20:17:48 +0000678 else
Daniel Neilson6f1eb582018-03-21 14:14:55 +0000679 M = Builder.CreateMemCpy(
680 SI->getPointerOperand(), findStoreAlignment(DL, SI),
Xin Tong47beee22019-01-04 02:13:22 +0000681 LI->getPointerOperand(), findLoadAlignment(DL, LI), Size);
Amaury Secheta0c242c2016-01-05 20:17:48 +0000682
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000683 LLVM_DEBUG(dbgs() << "Promoting " << *LI << " to " << *SI << " => "
684 << *M << "\n");
Amaury Secheta0c242c2016-01-05 20:17:48 +0000685
686 MD->removeInstruction(SI);
687 SI->eraseFromParent();
688 MD->removeInstruction(LI);
689 LI->eraseFromParent();
690 ++NumMemCpyInstr;
691
692 // Make sure we do not invalidate the iterator.
693 BBI = M->getIterator();
694 return true;
695 }
696 }
697
698 // Detect cases where we're performing call slot forwarding, but
699 // happen to be using a load-store pair to implement it, rather than
700 // a memcpy.
Eli Friedman5da0ff42011-06-02 21:24:42 +0000701 MemDepResult ldep = MD->getDependency(LI);
Craig Topperf40110f2014-04-25 05:29:35 +0000702 CallInst *C = nullptr;
Eli Friedman5da0ff42011-06-02 21:24:42 +0000703 if (ldep.isClobber() && !isa<MemCpyInst>(ldep.getInst()))
704 C = dyn_cast<CallInst>(ldep.getInst());
705
706 if (C) {
707 // Check that nothing touches the dest of the "copy" between
708 // the call and the store.
David Majnemerd99068d2016-05-26 19:24:24 +0000709 Value *CpyDest = SI->getPointerOperand()->stripPointerCasts();
710 bool CpyDestIsLocal = isa<AllocaInst>(CpyDest);
Sean Silva6347df02016-06-14 02:44:55 +0000711 AliasAnalysis &AA = LookupAliasAnalysis();
Chandler Carruthac80dc72015-06-17 07:18:54 +0000712 MemoryLocation StoreLoc = MemoryLocation::get(SI);
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000713 for (BasicBlock::iterator I = --SI->getIterator(), E = C->getIterator();
714 I != E; --I) {
Alina Sbirlea63d22502017-12-05 20:12:23 +0000715 if (isModOrRefSet(AA.getModRefInfo(&*I, StoreLoc))) {
Craig Topperf40110f2014-04-25 05:29:35 +0000716 C = nullptr;
Eli Friedmane8bbc102011-06-15 01:25:56 +0000717 break;
718 }
David Majnemerd99068d2016-05-26 19:24:24 +0000719 // The store to dest may never happen if an exception can be thrown
720 // between the load and the store.
721 if (I->mayThrow() && !CpyDestIsLocal) {
722 C = nullptr;
723 break;
724 }
Eli Friedman5da0ff42011-06-02 21:24:42 +0000725 }
726 }
727
Owen Anderson18e4fed2010-10-15 22:52:12 +0000728 if (C) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000729 bool changed = performCallSlotOptzn(
730 LI, SI->getPointerOperand()->stripPointerCasts(),
731 LI->getPointerOperand()->stripPointerCasts(),
732 DL.getTypeStoreSize(SI->getOperand(0)->getType()),
Amaury Secheta0c242c2016-01-05 20:17:48 +0000733 findCommonAlignment(DL, SI, LI), C);
Owen Anderson18e4fed2010-10-15 22:52:12 +0000734 if (changed) {
Chris Lattner58f9f582010-11-21 00:28:59 +0000735 MD->removeInstruction(SI);
Owen Anderson18e4fed2010-10-15 22:52:12 +0000736 SI->eraseFromParent();
Chris Lattnercaf5c0d2011-01-09 19:26:10 +0000737 MD->removeInstruction(LI);
Owen Anderson18e4fed2010-10-15 22:52:12 +0000738 LI->eraseFromParent();
739 ++NumMemCpyInstr;
740 return true;
741 }
742 }
743 }
744 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000745
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000746 // There are two cases that are interesting for this code to handle: memcpy
747 // and memset. Right now we only handle memset.
Nadav Rotem465834c2012-07-24 10:51:42 +0000748
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000749 // Ensure that the value being stored is something that can be memset'able a
750 // byte at a time like "0" or "-1" or any width, as well as things like
751 // 0xA0A0A0A0 and 0.0.
Amaury Sechet3235c082016-01-06 19:47:24 +0000752 auto *V = SI->getOperand(0);
753 if (Value *ByteVal = isBytewiseValue(V)) {
Chris Lattnerc6381472011-01-08 20:24:01 +0000754 if (Instruction *I = tryMergingIntoMemset(SI, SI->getPointerOperand(),
755 ByteVal)) {
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000756 BBI = I->getIterator(); // Don't invalidate iterator.
Chris Lattnerc6381472011-01-08 20:24:01 +0000757 return true;
Mon P Wangc576ee92010-04-04 03:10:48 +0000758 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000759
Amaury Sechet3235c082016-01-06 19:47:24 +0000760 // If we have an aggregate, we try to promote it to memset regardless
761 // of opportunity for merging as it can expose optimization opportunities
762 // in subsequent passes.
763 auto *T = V->getType();
764 if (T->isAggregateType()) {
765 uint64_t Size = DL.getTypeStoreSize(T);
766 unsigned Align = SI->getAlignment();
767 if (!Align)
768 Align = DL.getABITypeAlignment(T);
769 IRBuilder<> Builder(SI);
Xin Tong47beee22019-01-04 02:13:22 +0000770 auto *M =
771 Builder.CreateMemSet(SI->getPointerOperand(), ByteVal, Size, Align);
Amaury Sechet3235c082016-01-06 19:47:24 +0000772
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000773 LLVM_DEBUG(dbgs() << "Promoting " << *SI << " to " << *M << "\n");
Amaury Sechet3235c082016-01-06 19:47:24 +0000774
775 MD->removeInstruction(SI);
776 SI->eraseFromParent();
777 NumMemSetInfer++;
778
779 // Make sure we do not invalidate the iterator.
780 BBI = M->getIterator();
781 return true;
782 }
783 }
784
Chris Lattnerc6381472011-01-08 20:24:01 +0000785 return false;
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000786}
787
Sean Silva6347df02016-06-14 02:44:55 +0000788bool MemCpyOptPass::processMemSet(MemSetInst *MSI, BasicBlock::iterator &BBI) {
Chris Lattner9a1d63b2011-01-08 21:19:19 +0000789 // See if there is another memset or store neighboring this memset which
790 // allows us to widen out the memset to do a single larger store.
Chris Lattnerff6ed2a2011-01-08 22:11:56 +0000791 if (isa<ConstantInt>(MSI->getLength()) && !MSI->isVolatile())
792 if (Instruction *I = tryMergingIntoMemset(MSI, MSI->getDest(),
793 MSI->getValue())) {
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000794 BBI = I->getIterator(); // Don't invalidate iterator.
Chris Lattnerff6ed2a2011-01-08 22:11:56 +0000795 return true;
796 }
Chris Lattner9a1d63b2011-01-08 21:19:19 +0000797 return false;
798}
799
Sanjay Patela75c41e2015-08-13 22:53:20 +0000800/// Takes a memcpy and a call that it depends on,
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000801/// and checks for the possibility of a call slot optimization by having
802/// the call write its result directly into the destination of the memcpy.
Sean Silva6347df02016-06-14 02:44:55 +0000803bool MemCpyOptPass::performCallSlotOptzn(Instruction *cpy, Value *cpyDest,
804 Value *cpySrc, uint64_t cpyLen,
805 unsigned cpyAlign, CallInst *C) {
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000806 // The general transformation to keep in mind is
807 //
808 // call @func(..., src, ...)
809 // memcpy(dest, src, ...)
810 //
811 // ->
812 //
813 // memcpy(dest, src, ...)
814 // call @func(..., dest, ...)
815 //
816 // Since moving the memcpy is technically awkward, we additionally check that
817 // src only holds uninitialized values at the moment of the call, meaning that
818 // the memcpy can be discarded rather than moved.
819
Tim Shen7aa0ad62016-06-08 19:42:32 +0000820 // Lifetime marks shouldn't be operated on.
821 if (Function *F = C->getCalledFunction())
822 if (F->isIntrinsic() && F->getIntrinsicID() == Intrinsic::lifetime_start)
823 return false;
824
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000825 // Deliberately get the source and destination with bitcasts stripped away,
826 // because we'll need to do type comparisons based on the underlying type.
Gabor Greif62f0aac2010-07-28 22:50:26 +0000827 CallSite CS(C);
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000828
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000829 // Require that src be an alloca. This simplifies the reasoning considerably.
Chris Lattnerb5557a72009-09-01 17:09:55 +0000830 AllocaInst *srcAlloca = dyn_cast<AllocaInst>(cpySrc);
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000831 if (!srcAlloca)
832 return false;
833
Chris Lattnerb5557a72009-09-01 17:09:55 +0000834 ConstantInt *srcArraySize = dyn_cast<ConstantInt>(srcAlloca->getArraySize());
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000835 if (!srcArraySize)
836 return false;
837
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000838 const DataLayout &DL = cpy->getModule()->getDataLayout();
839 uint64_t srcSize = DL.getTypeAllocSize(srcAlloca->getAllocatedType()) *
840 srcArraySize->getZExtValue();
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000841
Owen Anderson18e4fed2010-10-15 22:52:12 +0000842 if (cpyLen < srcSize)
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000843 return false;
844
845 // Check that accessing the first srcSize bytes of dest will not cause a
846 // trap. Otherwise the transform is invalid since it might cause a trap
847 // to occur earlier than it otherwise would.
Chris Lattnerb5557a72009-09-01 17:09:55 +0000848 if (AllocaInst *A = dyn_cast<AllocaInst>(cpyDest)) {
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000849 // The destination is an alloca. Check it is larger than srcSize.
Chris Lattnerb5557a72009-09-01 17:09:55 +0000850 ConstantInt *destArraySize = dyn_cast<ConstantInt>(A->getArraySize());
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000851 if (!destArraySize)
852 return false;
853
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000854 uint64_t destSize = DL.getTypeAllocSize(A->getAllocatedType()) *
855 destArraySize->getZExtValue();
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000856
857 if (destSize < srcSize)
858 return false;
Chris Lattnerb5557a72009-09-01 17:09:55 +0000859 } else if (Argument *A = dyn_cast<Argument>(cpyDest)) {
David Majnemerd99068d2016-05-26 19:24:24 +0000860 // The store to dest may never happen if the call can throw.
861 if (C->mayThrow())
862 return false;
863
Bjorn Steinbrinkd20816f2014-10-16 19:43:08 +0000864 if (A->getDereferenceableBytes() < srcSize) {
865 // If the destination is an sret parameter then only accesses that are
866 // outside of the returned struct type can trap.
867 if (!A->hasStructRetAttr())
868 return false;
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000869
Bjorn Steinbrinkd20816f2014-10-16 19:43:08 +0000870 Type *StructTy = cast<PointerType>(A->getType())->getElementType();
871 if (!StructTy->isSized()) {
872 // The call may never return and hence the copy-instruction may never
873 // be executed, and therefore it's not safe to say "the destination
874 // has at least <cpyLen> bytes, as implied by the copy-instruction",
875 return false;
876 }
877
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000878 uint64_t destSize = DL.getTypeAllocSize(StructTy);
Bjorn Steinbrinkd20816f2014-10-16 19:43:08 +0000879 if (destSize < srcSize)
880 return false;
Shuxin Yang140d5922013-06-08 04:56:05 +0000881 }
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000882 } else {
883 return false;
884 }
885
Duncan Sands933db772012-10-05 07:29:46 +0000886 // Check that dest points to memory that is at least as aligned as src.
887 unsigned srcAlign = srcAlloca->getAlignment();
888 if (!srcAlign)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000889 srcAlign = DL.getABITypeAlignment(srcAlloca->getAllocatedType());
Duncan Sands933db772012-10-05 07:29:46 +0000890 bool isDestSufficientlyAligned = srcAlign <= cpyAlign;
891 // If dest is not aligned enough and we can't increase its alignment then
892 // bail out.
893 if (!isDestSufficientlyAligned && !isa<AllocaInst>(cpyDest))
894 return false;
895
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000896 // Check that src is not accessed except via the call and the memcpy. This
897 // guarantees that it holds only undefined values when passed in (so the final
898 // memcpy can be dropped), that it is not read or written between the call and
899 // the memcpy, and that writing beyond the end of it is undefined.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000900 SmallVector<User*, 8> srcUseList(srcAlloca->user_begin(),
901 srcAlloca->user_end());
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000902 while (!srcUseList.empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000903 User *U = srcUseList.pop_back_val();
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000904
Chandler Carruthcdf47882014-03-09 03:16:01 +0000905 if (isa<BitCastInst>(U) || isa<AddrSpaceCastInst>(U)) {
906 for (User *UU : U->users())
907 srcUseList.push_back(UU);
Chandler Carruth18cee1d2014-09-01 10:09:18 +0000908 continue;
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000909 }
Chandler Carruth18cee1d2014-09-01 10:09:18 +0000910 if (GetElementPtrInst *G = dyn_cast<GetElementPtrInst>(U)) {
911 if (!G->hasAllZeroIndices())
912 return false;
913
914 for (User *UU : U->users())
915 srcUseList.push_back(UU);
916 continue;
917 }
918 if (const IntrinsicInst *IT = dyn_cast<IntrinsicInst>(U))
Vedant Kumarb264d692018-12-21 21:49:40 +0000919 if (IT->isLifetimeStartOrEnd())
Chandler Carruth18cee1d2014-09-01 10:09:18 +0000920 continue;
921
922 if (U != C && U != cpy)
923 return false;
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000924 }
925
Nick Lewycky703e4882014-07-14 18:52:02 +0000926 // Check that src isn't captured by the called function since the
927 // transformation can cause aliasing issues in that case.
928 for (unsigned i = 0, e = CS.arg_size(); i != e; ++i)
929 if (CS.getArgument(i) == cpySrc && !CS.doesNotCapture(i))
930 return false;
931
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000932 // Since we're changing the parameter to the callsite, we need to make sure
933 // that what would be the new parameter dominates the callsite.
Sean Silva6347df02016-06-14 02:44:55 +0000934 DominatorTree &DT = LookupDomTree();
Chris Lattnerb5557a72009-09-01 17:09:55 +0000935 if (Instruction *cpyDestInst = dyn_cast<Instruction>(cpyDest))
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000936 if (!DT.dominates(cpyDestInst, C))
937 return false;
938
939 // In addition to knowing that the call does not access src in some
940 // unexpected manner, for example via a global, which we deduce from
941 // the use analysis, we also need to know that it does not sneakily
942 // access dest. We rely on AA to figure this out for us.
Sean Silva6347df02016-06-14 02:44:55 +0000943 AliasAnalysis &AA = LookupAliasAnalysis();
George Burgess IV5e4a03a2018-12-23 06:40:39 +0000944 ModRefInfo MR = AA.getModRefInfo(C, cpyDest, LocationSize::precise(srcSize));
Chad Rosiera968caf2012-05-14 20:35:04 +0000945 // If necessary, perform additional analysis.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000946 if (isModOrRefSet(MR))
George Burgess IV5e4a03a2018-12-23 06:40:39 +0000947 MR = AA.callCapturesBefore(C, cpyDest, LocationSize::precise(srcSize), &DT);
Alina Sbirlea63d22502017-12-05 20:12:23 +0000948 if (isModOrRefSet(MR))
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000949 return false;
950
Fiona Glasera9bd5722017-03-14 22:37:38 +0000951 // We can't create address space casts here because we don't know if they're
952 // safe for the target.
953 if (cpySrc->getType()->getPointerAddressSpace() !=
954 cpyDest->getType()->getPointerAddressSpace())
955 return false;
956 for (unsigned i = 0; i < CS.arg_size(); ++i)
957 if (CS.getArgument(i)->stripPointerCasts() == cpySrc &&
958 cpySrc->getType()->getPointerAddressSpace() !=
959 CS.getArgument(i)->getType()->getPointerAddressSpace())
960 return false;
961
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000962 // All the checks have passed, so do the transformation.
Owen Andersond071a872008-06-01 21:52:16 +0000963 bool changedArgument = false;
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000964 for (unsigned i = 0; i < CS.arg_size(); ++i)
Owen Anderson38099c12008-06-01 22:26:26 +0000965 if (CS.getArgument(i)->stripPointerCasts() == cpySrc) {
Duncan Sandsa6d20012012-10-04 13:53:21 +0000966 Value *Dest = cpySrc->getType() == cpyDest->getType() ? cpyDest
967 : CastInst::CreatePointerCast(cpyDest, cpySrc->getType(),
968 cpyDest->getName(), C);
Owen Andersond071a872008-06-01 21:52:16 +0000969 changedArgument = true;
Duncan Sandsa6d20012012-10-04 13:53:21 +0000970 if (CS.getArgument(i)->getType() == Dest->getType())
971 CS.setArgument(i, Dest);
Chris Lattnerb5557a72009-09-01 17:09:55 +0000972 else
Duncan Sandsa6d20012012-10-04 13:53:21 +0000973 CS.setArgument(i, CastInst::CreatePointerCast(Dest,
974 CS.getArgument(i)->getType(), Dest->getName(), C));
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000975 }
976
Owen Andersond071a872008-06-01 21:52:16 +0000977 if (!changedArgument)
978 return false;
979
Duncan Sandsc6ada692012-10-04 10:54:40 +0000980 // If the destination wasn't sufficiently aligned then increase its alignment.
981 if (!isDestSufficientlyAligned) {
982 assert(isa<AllocaInst>(cpyDest) && "Can only increase alloca alignment!");
983 cast<AllocaInst>(cpyDest)->setAlignment(srcAlign);
984 }
985
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000986 // Drop any cached information about the call, because we may have changed
987 // its dependence information by changing its parameter.
Chris Lattner58f9f582010-11-21 00:28:59 +0000988 MD->removeInstruction(C);
Owen Andersonef9a6fd2008-04-09 08:23:16 +0000989
Bjorn Steinbrink71bf3b82015-02-07 17:54:36 +0000990 // Update AA metadata
991 // FIXME: MD_tbaa_struct and MD_mem_parallel_loop_access should also be
992 // handled here, but combineMetadata doesn't support them yet
Piotr Padlewskidc9b2cf2015-10-02 22:12:22 +0000993 unsigned KnownIDs[] = {LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
994 LLVMContext::MD_noalias,
Michael Kruse978ba612018-12-20 04:58:07 +0000995 LLVMContext::MD_invariant_group,
996 LLVMContext::MD_access_group};
Florian Hahn406f1ff2018-08-24 11:40:04 +0000997 combineMetadata(C, cpy, KnownIDs, true);
Bjorn Steinbrink71bf3b82015-02-07 17:54:36 +0000998
Chris Lattner58f9f582010-11-21 00:28:59 +0000999 // Remove the memcpy.
1000 MD->removeInstruction(cpy);
Dan Gohmand2d1ae12010-06-22 15:08:57 +00001001 ++NumMemCpyInstr;
Owen Andersonef9a6fd2008-04-09 08:23:16 +00001002
1003 return true;
1004}
1005
Sanjay Patela75c41e2015-08-13 22:53:20 +00001006/// We've found that the (upward scanning) memory dependence of memcpy 'M' is
1007/// the memcpy 'MDep'. Try to simplify M to copy from MDep's input if we can.
Sean Silva6347df02016-06-14 02:44:55 +00001008bool MemCpyOptPass::processMemCpyMemCpyDependence(MemCpyInst *M,
1009 MemCpyInst *MDep) {
Chris Lattner7e9b2ea2010-11-18 07:02:37 +00001010 // We can only transforms memcpy's where the dest of one is the source of the
1011 // other.
Chris Lattner58f9f582010-11-21 00:28:59 +00001012 if (M->getSource() != MDep->getDest() || MDep->isVolatile())
Chris Lattner7e9b2ea2010-11-18 07:02:37 +00001013 return false;
Nadav Rotem465834c2012-07-24 10:51:42 +00001014
Chris Lattnerfd51c522010-12-09 07:39:50 +00001015 // If dep instruction is reading from our current input, then it is a noop
1016 // transfer and substituting the input won't change this instruction. Just
1017 // ignore the input and let someone else zap MDep. This handles cases like:
1018 // memcpy(a <- a)
1019 // memcpy(b <- a)
1020 if (M->getSource() == MDep->getSource())
1021 return false;
Nadav Rotem465834c2012-07-24 10:51:42 +00001022
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001023 // Second, the length of the memcpy's must be the same, or the preceding one
Chris Lattner7e9b2ea2010-11-18 07:02:37 +00001024 // must be larger than the following one.
Dan Gohman19e30d52011-01-21 22:07:57 +00001025 ConstantInt *MDepLen = dyn_cast<ConstantInt>(MDep->getLength());
1026 ConstantInt *MLen = dyn_cast<ConstantInt>(M->getLength());
1027 if (!MDepLen || !MLen || MDepLen->getZExtValue() < MLen->getZExtValue())
1028 return false;
Nadav Rotem465834c2012-07-24 10:51:42 +00001029
Sean Silva6347df02016-06-14 02:44:55 +00001030 AliasAnalysis &AA = LookupAliasAnalysis();
Chris Lattner59572292010-11-21 08:06:10 +00001031
1032 // Verify that the copied-from memory doesn't change in between the two
1033 // transfers. For example, in:
1034 // memcpy(a <- b)
1035 // *b = 42;
1036 // memcpy(c <- a)
1037 // It would be invalid to transform the second memcpy into memcpy(c <- b).
1038 //
1039 // TODO: If the code between M and MDep is transparent to the destination "c",
1040 // then we could still perform the xform by moving M up to the first memcpy.
1041 //
1042 // NOTE: This is conservative, it will stop on any read from the source loc,
1043 // not just the defining memcpy.
Reid Kleckner6d310012017-12-28 05:10:33 +00001044 MemDepResult SourceDep =
1045 MD->getPointerDependencyFrom(MemoryLocation::getForSource(MDep), false,
1046 M->getIterator(), M->getParent());
Chris Lattner59572292010-11-21 08:06:10 +00001047 if (!SourceDep.isClobber() || SourceDep.getInst() != MDep)
1048 return false;
Nadav Rotem465834c2012-07-24 10:51:42 +00001049
Chris Lattner731caac2010-11-18 08:00:57 +00001050 // If the dest of the second might alias the source of the first, then the
1051 // source and dest might overlap. We still want to eliminate the intermediate
1052 // value, but we have to generate a memmove instead of memcpy.
Chris Lattner6cf8d6c2010-12-26 22:57:41 +00001053 bool UseMemMove = false;
Chandler Carruth70c61c12015-06-04 02:03:15 +00001054 if (!AA.isNoAlias(MemoryLocation::getForDest(M),
1055 MemoryLocation::getForSource(MDep)))
Chris Lattner6cf8d6c2010-12-26 22:57:41 +00001056 UseMemMove = true;
Nadav Rotem465834c2012-07-24 10:51:42 +00001057
Chris Lattner58f9f582010-11-21 00:28:59 +00001058 // If all checks passed, then we can transform M.
Reid Klecknerb894ecf2018-12-21 01:41:20 +00001059 LLVM_DEBUG(dbgs() << "MemCpyOptPass: Forwarding memcpy->memcpy src:\n"
1060 << *MDep << '\n' << *M << '\n');
Nadav Rotem465834c2012-07-24 10:51:42 +00001061
Chris Lattner7e9b2ea2010-11-18 07:02:37 +00001062 // TODO: Is this worth it if we're creating a less aligned memcpy? For
1063 // example we could be moving from movaps -> movq on x86.
Chris Lattner6cf8d6c2010-12-26 22:57:41 +00001064 IRBuilder<> Builder(M);
1065 if (UseMemMove)
Daniel Neilson6f1eb582018-03-21 14:14:55 +00001066 Builder.CreateMemMove(M->getRawDest(), M->getDestAlignment(),
1067 MDep->getRawSource(), MDep->getSourceAlignment(),
1068 M->getLength(), M->isVolatile());
Chris Lattner6cf8d6c2010-12-26 22:57:41 +00001069 else
Daniel Neilson6f1eb582018-03-21 14:14:55 +00001070 Builder.CreateMemCpy(M->getRawDest(), M->getDestAlignment(),
1071 MDep->getRawSource(), MDep->getSourceAlignment(),
1072 M->getLength(), M->isVolatile());
Chris Lattner1385dff2010-11-18 08:07:09 +00001073
Chris Lattner59572292010-11-21 08:06:10 +00001074 // Remove the instruction we're replacing.
Chris Lattner58f9f582010-11-21 00:28:59 +00001075 MD->removeInstruction(M);
Chris Lattner1385dff2010-11-18 08:07:09 +00001076 M->eraseFromParent();
1077 ++NumMemCpyInstr;
1078 return true;
Chris Lattner7e9b2ea2010-11-18 07:02:37 +00001079}
1080
Ahmed Bougacha83f78a42015-04-17 22:20:57 +00001081/// We've found that the (upward scanning) memory dependence of \p MemCpy is
1082/// \p MemSet. Try to simplify \p MemSet to only set the trailing bytes that
1083/// weren't copied over by \p MemCpy.
1084///
1085/// In other words, transform:
1086/// \code
1087/// memset(dst, c, dst_size);
1088/// memcpy(dst, src, src_size);
1089/// \endcode
1090/// into:
1091/// \code
1092/// memcpy(dst, src, src_size);
1093/// memset(dst + src_size, c, dst_size <= src_size ? 0 : dst_size - src_size);
1094/// \endcode
Sean Silva6347df02016-06-14 02:44:55 +00001095bool MemCpyOptPass::processMemSetMemCpyDependence(MemCpyInst *MemCpy,
1096 MemSetInst *MemSet) {
Ahmed Bougacha83f78a42015-04-17 22:20:57 +00001097 // We can only transform memset/memcpy with the same destination.
1098 if (MemSet->getDest() != MemCpy->getDest())
1099 return false;
1100
Ahmed Bougacha97876fa2015-05-21 01:43:39 +00001101 // Check that there are no other dependencies on the memset destination.
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +00001102 MemDepResult DstDepInfo =
1103 MD->getPointerDependencyFrom(MemoryLocation::getForDest(MemSet), false,
1104 MemCpy->getIterator(), MemCpy->getParent());
Ahmed Bougacha97876fa2015-05-21 01:43:39 +00001105 if (DstDepInfo.getInst() != MemSet)
1106 return false;
1107
Ahmed Bougacha9692e302015-04-21 21:28:33 +00001108 // Use the same i8* dest as the memcpy, killing the memset dest if different.
1109 Value *Dest = MemCpy->getRawDest();
Ahmed Bougacha83f78a42015-04-17 22:20:57 +00001110 Value *DestSize = MemSet->getLength();
1111 Value *SrcSize = MemCpy->getLength();
1112
1113 // By default, create an unaligned memset.
1114 unsigned Align = 1;
1115 // If Dest is aligned, and SrcSize is constant, use the minimum alignment
1116 // of the sum.
1117 const unsigned DestAlign =
Daniel Neilson6f1eb582018-03-21 14:14:55 +00001118 std::max(MemSet->getDestAlignment(), MemCpy->getDestAlignment());
Ahmed Bougacha83f78a42015-04-17 22:20:57 +00001119 if (DestAlign > 1)
1120 if (ConstantInt *SrcSizeC = dyn_cast<ConstantInt>(SrcSize))
1121 Align = MinAlign(SrcSizeC->getZExtValue(), DestAlign);
1122
Ahmed Bougacha97876fa2015-05-21 01:43:39 +00001123 IRBuilder<> Builder(MemCpy);
Ahmed Bougacha83f78a42015-04-17 22:20:57 +00001124
Ahmed Bougacha05b72c12015-04-18 23:06:04 +00001125 // If the sizes have different types, zext the smaller one.
Ahmed Bougacha7216ccc2015-04-18 17:57:41 +00001126 if (DestSize->getType() != SrcSize->getType()) {
Ahmed Bougacha05b72c12015-04-18 23:06:04 +00001127 if (DestSize->getType()->getIntegerBitWidth() >
1128 SrcSize->getType()->getIntegerBitWidth())
1129 SrcSize = Builder.CreateZExt(SrcSize, DestSize->getType());
1130 else
1131 DestSize = Builder.CreateZExt(DestSize, SrcSize->getType());
Ahmed Bougacha7216ccc2015-04-18 17:57:41 +00001132 }
1133
Benjamin Kramer1697d392016-11-07 17:47:28 +00001134 Value *Ule = Builder.CreateICmpULE(DestSize, SrcSize);
1135 Value *SizeDiff = Builder.CreateSub(DestSize, SrcSize);
1136 Value *MemsetLen = Builder.CreateSelect(
1137 Ule, ConstantInt::getNullValue(DestSize->getType()), SizeDiff);
Ahmed Bougacha83f78a42015-04-17 22:20:57 +00001138 Builder.CreateMemSet(Builder.CreateGEP(Dest, SrcSize), MemSet->getOperand(1),
1139 MemsetLen, Align);
1140
1141 MD->removeInstruction(MemSet);
1142 MemSet->eraseFromParent();
1143 return true;
1144}
Chris Lattner7e9b2ea2010-11-18 07:02:37 +00001145
Nikita Popovdc73a6e2018-12-13 20:04:27 +00001146/// Determine whether the instruction has undefined content for the given Size,
1147/// either because it was freshly alloca'd or started its lifetime.
1148static bool hasUndefContents(Instruction *I, ConstantInt *Size) {
1149 if (isa<AllocaInst>(I))
1150 return true;
1151
1152 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
1153 if (II->getIntrinsicID() == Intrinsic::lifetime_start)
1154 if (ConstantInt *LTSize = dyn_cast<ConstantInt>(II->getArgOperand(0)))
1155 if (LTSize->getZExtValue() >= Size->getZExtValue())
1156 return true;
1157
1158 return false;
1159}
1160
Ahmed Bougachaf8fa3b82015-05-16 01:32:26 +00001161/// Transform memcpy to memset when its source was just memset.
1162/// In other words, turn:
1163/// \code
1164/// memset(dst1, c, dst1_size);
1165/// memcpy(dst2, dst1, dst2_size);
1166/// \endcode
1167/// into:
1168/// \code
1169/// memset(dst1, c, dst1_size);
1170/// memset(dst2, c, dst2_size);
1171/// \endcode
1172/// When dst2_size <= dst1_size.
1173///
1174/// The \p MemCpy must have a Constant length.
Sean Silva6347df02016-06-14 02:44:55 +00001175bool MemCpyOptPass::performMemCpyToMemSetOptzn(MemCpyInst *MemCpy,
1176 MemSetInst *MemSet) {
Tim Shena3dbead2016-08-25 19:27:26 +00001177 AliasAnalysis &AA = LookupAliasAnalysis();
1178
Tim Shen3ad8b432016-08-25 21:03:46 +00001179 // Make sure that memcpy(..., memset(...), ...), that is we are memsetting and
1180 // memcpying from the same address. Otherwise it is hard to reason about.
Tim Shena3dbead2016-08-25 19:27:26 +00001181 if (!AA.isMustAlias(MemSet->getRawDest(), MemCpy->getRawSource()))
Ahmed Bougachaf8fa3b82015-05-16 01:32:26 +00001182 return false;
1183
Nikita Popovdc73a6e2018-12-13 20:04:27 +00001184 // A known memset size is required.
Ahmed Bougachaf8fa3b82015-05-16 01:32:26 +00001185 ConstantInt *MemSetSize = dyn_cast<ConstantInt>(MemSet->getLength());
Nikita Popovdc73a6e2018-12-13 20:04:27 +00001186 if (!MemSetSize)
1187 return false;
1188
Ahmed Bougachaf8fa3b82015-05-16 01:32:26 +00001189 // Make sure the memcpy doesn't read any more than what the memset wrote.
1190 // Don't worry about sizes larger than i64.
Nikita Popovdc73a6e2018-12-13 20:04:27 +00001191 ConstantInt *CopySize = cast<ConstantInt>(MemCpy->getLength());
1192 if (CopySize->getZExtValue() > MemSetSize->getZExtValue()) {
1193 // If the memcpy is larger than the memset, but the memory was undef prior
1194 // to the memset, we can just ignore the tail. Technically we're only
1195 // interested in the bytes from MemSetSize..CopySize here, but as we can't
1196 // easily represent this location, we use the full 0..CopySize range.
1197 MemoryLocation MemCpyLoc = MemoryLocation::getForSource(MemCpy);
1198 MemDepResult DepInfo = MD->getPointerDependencyFrom(
1199 MemCpyLoc, true, MemSet->getIterator(), MemSet->getParent());
1200 if (DepInfo.isDef() && hasUndefContents(DepInfo.getInst(), CopySize))
1201 CopySize = MemSetSize;
1202 else
1203 return false;
1204 }
Ahmed Bougachaf8fa3b82015-05-16 01:32:26 +00001205
Ahmed Bougacha0541c672015-05-21 00:08:35 +00001206 IRBuilder<> Builder(MemCpy);
Ahmed Bougachaf8fa3b82015-05-16 01:32:26 +00001207 Builder.CreateMemSet(MemCpy->getRawDest(), MemSet->getOperand(1),
Daniel Neilson6f1eb582018-03-21 14:14:55 +00001208 CopySize, MemCpy->getDestAlignment());
Ahmed Bougachaf8fa3b82015-05-16 01:32:26 +00001209 return true;
1210}
1211
Sanjay Patela75c41e2015-08-13 22:53:20 +00001212/// Perform simplification of memcpy's. If we have memcpy A
Gabor Greif62f0aac2010-07-28 22:50:26 +00001213/// which copies X to Y, and memcpy B which copies Y to Z, then we can rewrite
1214/// B to be a memcpy from X to Z (or potentially a memmove, depending on
1215/// circumstances). This allows later passes to remove the first memcpy
1216/// altogether.
Sean Silva6347df02016-06-14 02:44:55 +00001217bool MemCpyOptPass::processMemCpy(MemCpyInst *M) {
Nick Lewycky00703e72014-02-04 00:18:54 +00001218 // We can only optimize non-volatile memcpy's.
1219 if (M->isVolatile()) return false;
Owen Anderson18e4fed2010-10-15 22:52:12 +00001220
Chris Lattnerbc4457e2010-12-09 07:45:45 +00001221 // If the source and destination of the memcpy are the same, then zap it.
1222 if (M->getSource() == M->getDest()) {
1223 MD->removeInstruction(M);
1224 M->eraseFromParent();
1225 return false;
1226 }
Benjamin Kramerea9152e2010-12-24 21:17:12 +00001227
1228 // If copying from a constant, try to turn the memcpy into a memset.
Benjamin Kramerb90b2f02010-12-24 22:23:59 +00001229 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(M->getSource()))
Benjamin Kramer30342fb2010-12-26 15:23:45 +00001230 if (GV->isConstant() && GV->hasDefinitiveInitializer())
Benjamin Kramerb90b2f02010-12-24 22:23:59 +00001231 if (Value *ByteVal = isBytewiseValue(GV->getInitializer())) {
Chris Lattner6cf8d6c2010-12-26 22:57:41 +00001232 IRBuilder<> Builder(M);
Nick Lewycky00703e72014-02-04 00:18:54 +00001233 Builder.CreateMemSet(M->getRawDest(), ByteVal, M->getLength(),
Daniel Neilson6f1eb582018-03-21 14:14:55 +00001234 M->getDestAlignment(), false);
Benjamin Kramerb90b2f02010-12-24 22:23:59 +00001235 MD->removeInstruction(M);
1236 M->eraseFromParent();
1237 ++NumCpyToSet;
1238 return true;
1239 }
Benjamin Kramerea9152e2010-12-24 21:17:12 +00001240
Ahmed Bougachab6169662015-05-11 23:09:46 +00001241 MemDepResult DepInfo = MD->getDependency(M);
Ahmed Bougacha83f78a42015-04-17 22:20:57 +00001242
1243 // Try to turn a partially redundant memset + memcpy into
1244 // memcpy + smaller memset. We don't need the memcpy size for this.
Ahmed Bougachab6169662015-05-11 23:09:46 +00001245 if (DepInfo.isClobber())
1246 if (MemSetInst *MDep = dyn_cast<MemSetInst>(DepInfo.getInst()))
Ahmed Bougacha83f78a42015-04-17 22:20:57 +00001247 if (processMemSetMemCpyDependence(M, MDep))
1248 return true;
1249
Nick Lewycky00703e72014-02-04 00:18:54 +00001250 // The optimizations after this point require the memcpy size.
1251 ConstantInt *CopySize = dyn_cast<ConstantInt>(M->getLength());
Craig Topperf40110f2014-04-25 05:29:35 +00001252 if (!CopySize) return false;
Nick Lewycky00703e72014-02-04 00:18:54 +00001253
Ahmed Bougachaf8fa3b82015-05-16 01:32:26 +00001254 // There are four possible optimizations we can do for memcpy:
Chris Lattnerb5557a72009-09-01 17:09:55 +00001255 // a) memcpy-memcpy xform which exposes redundance for DSE.
1256 // b) call-memcpy xform for return slot optimization.
Nick Lewycky77d5fb42014-03-26 23:45:15 +00001257 // c) memcpy from freshly alloca'd space or space that has just started its
1258 // lifetime copies undefined data, and we can therefore eliminate the
1259 // memcpy in favor of the data that was already at the destination.
Ahmed Bougachaf8fa3b82015-05-16 01:32:26 +00001260 // d) memcpy from a just-memset'd source can be turned into memset.
Nick Lewycky0a7e9cc2011-10-16 20:13:32 +00001261 if (DepInfo.isClobber()) {
1262 if (CallInst *C = dyn_cast<CallInst>(DepInfo.getInst())) {
Daniel Neilson6f1eb582018-03-21 14:14:55 +00001263 // FIXME: Can we pass in either of dest/src alignment here instead
1264 // of conservatively taking the minimum?
1265 unsigned Align = MinAlign(M->getDestAlignment(), M->getSourceAlignment());
Nick Lewycky0a7e9cc2011-10-16 20:13:32 +00001266 if (performCallSlotOptzn(M, M->getDest(), M->getSource(),
Daniel Neilson6f1eb582018-03-21 14:14:55 +00001267 CopySize->getZExtValue(), Align,
Duncan Sandsc6ada692012-10-04 10:54:40 +00001268 C)) {
Nick Lewycky0a7e9cc2011-10-16 20:13:32 +00001269 MD->removeInstruction(M);
1270 M->eraseFromParent();
1271 return true;
1272 }
Chris Lattnerbc4457e2010-12-09 07:45:45 +00001273 }
Owen Andersonef9a6fd2008-04-09 08:23:16 +00001274 }
Ahmed Charles32e983e2012-02-13 06:30:56 +00001275
Chandler Carruthac80dc72015-06-17 07:18:54 +00001276 MemoryLocation SrcLoc = MemoryLocation::getForSource(M);
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +00001277 MemDepResult SrcDepInfo = MD->getPointerDependencyFrom(
1278 SrcLoc, true, M->getIterator(), M->getParent());
Ahmed Bougachab6169662015-05-11 23:09:46 +00001279
Nick Lewycky0a7e9cc2011-10-16 20:13:32 +00001280 if (SrcDepInfo.isClobber()) {
1281 if (MemCpyInst *MDep = dyn_cast<MemCpyInst>(SrcDepInfo.getInst()))
Ahmed Bougacha15a31f62015-05-16 01:23:47 +00001282 return processMemCpyMemCpyDependence(M, MDep);
Nick Lewycky99384942014-02-06 06:29:19 +00001283 } else if (SrcDepInfo.isDef()) {
Nikita Popovdc73a6e2018-12-13 20:04:27 +00001284 if (hasUndefContents(SrcDepInfo.getInst(), CopySize)) {
Nick Lewycky99384942014-02-06 06:29:19 +00001285 MD->removeInstruction(M);
1286 M->eraseFromParent();
1287 ++NumMemCpyInstr;
1288 return true;
1289 }
Nick Lewycky0a7e9cc2011-10-16 20:13:32 +00001290 }
1291
Ahmed Bougachaf8fa3b82015-05-16 01:32:26 +00001292 if (SrcDepInfo.isClobber())
1293 if (MemSetInst *MDep = dyn_cast<MemSetInst>(SrcDepInfo.getInst()))
1294 if (performMemCpyToMemSetOptzn(M, MDep)) {
1295 MD->removeInstruction(M);
1296 M->eraseFromParent();
1297 ++NumCpyToSet;
1298 return true;
1299 }
1300
Owen Andersonad5367f2008-04-29 21:51:00 +00001301 return false;
Owen Andersonef9a6fd2008-04-09 08:23:16 +00001302}
1303
Sanjay Patela75c41e2015-08-13 22:53:20 +00001304/// Transforms memmove calls to memcpy calls when the src/dst are guaranteed
1305/// not to alias.
Sean Silva6347df02016-06-14 02:44:55 +00001306bool MemCpyOptPass::processMemMove(MemMoveInst *M) {
1307 AliasAnalysis &AA = LookupAliasAnalysis();
Chris Lattner1145e332009-09-01 17:56:32 +00001308
David L. Jonesd21529f2017-01-23 23:16:46 +00001309 if (!TLI->has(LibFunc_memmove))
Chris Lattner23f61a02011-05-01 18:27:11 +00001310 return false;
Nadav Rotem465834c2012-07-24 10:51:42 +00001311
Chris Lattner1145e332009-09-01 17:56:32 +00001312 // See if the pointers alias.
Chandler Carruth70c61c12015-06-04 02:03:15 +00001313 if (!AA.isNoAlias(MemoryLocation::getForDest(M),
1314 MemoryLocation::getForSource(M)))
Chris Lattner1145e332009-09-01 17:56:32 +00001315 return false;
Nadav Rotem465834c2012-07-24 10:51:42 +00001316
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001317 LLVM_DEBUG(dbgs() << "MemCpyOptPass: Optimizing memmove -> memcpy: " << *M
1318 << "\n");
Nadav Rotem465834c2012-07-24 10:51:42 +00001319
Chris Lattner1145e332009-09-01 17:56:32 +00001320 // If not, then we know we can transform this.
Jay Foadb804a2b2011-07-12 14:06:48 +00001321 Type *ArgTys[3] = { M->getRawDest()->getType(),
1322 M->getRawSource()->getType(),
1323 M->getLength()->getType() };
Sanjay Patelaf674fb2015-12-14 17:24:23 +00001324 M->setCalledFunction(Intrinsic::getDeclaration(M->getModule(),
1325 Intrinsic::memcpy, ArgTys));
Duncan Sands0edc7102009-09-03 13:37:16 +00001326
Chris Lattner1145e332009-09-01 17:56:32 +00001327 // MemDep may have over conservative information about this instruction, just
1328 // conservatively flush it from the cache.
Chris Lattner58f9f582010-11-21 00:28:59 +00001329 MD->removeInstruction(M);
Duncan Sands0edc7102009-09-03 13:37:16 +00001330
1331 ++NumMoveToCpy;
Chris Lattner1145e332009-09-01 17:56:32 +00001332 return true;
1333}
Nadav Rotem465834c2012-07-24 10:51:42 +00001334
Sanjay Patela75c41e2015-08-13 22:53:20 +00001335/// This is called on every byval argument in call sites.
Sean Silva6347df02016-06-14 02:44:55 +00001336bool MemCpyOptPass::processByValArgument(CallSite CS, unsigned ArgNo) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001337 const DataLayout &DL = CS.getCaller()->getParent()->getDataLayout();
Chris Lattner59572292010-11-21 08:06:10 +00001338 // Find out what feeds this byval argument.
Chris Lattner58f9f582010-11-21 00:28:59 +00001339 Value *ByValArg = CS.getArgument(ArgNo);
Nick Lewyckyc585de62011-10-12 00:14:31 +00001340 Type *ByValTy = cast<PointerType>(ByValArg->getType())->getElementType();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001341 uint64_t ByValSize = DL.getTypeAllocSize(ByValTy);
Chandler Carruthac80dc72015-06-17 07:18:54 +00001342 MemDepResult DepInfo = MD->getPointerDependencyFrom(
George Burgess IV5e4a03a2018-12-23 06:40:39 +00001343 MemoryLocation(ByValArg, LocationSize::precise(ByValSize)), true,
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +00001344 CS.getInstruction()->getIterator(), CS.getInstruction()->getParent());
Chris Lattner58f9f582010-11-21 00:28:59 +00001345 if (!DepInfo.isClobber())
1346 return false;
1347
1348 // If the byval argument isn't fed by a memcpy, ignore it. If it is fed by
1349 // a memcpy, see if we can byval from the source of the memcpy instead of the
1350 // result.
1351 MemCpyInst *MDep = dyn_cast<MemCpyInst>(DepInfo.getInst());
Craig Topperf40110f2014-04-25 05:29:35 +00001352 if (!MDep || MDep->isVolatile() ||
Chris Lattner58f9f582010-11-21 00:28:59 +00001353 ByValArg->stripPointerCasts() != MDep->getDest())
1354 return false;
Nadav Rotem465834c2012-07-24 10:51:42 +00001355
Chris Lattner58f9f582010-11-21 00:28:59 +00001356 // The length of the memcpy must be larger or equal to the size of the byval.
Chris Lattner58f9f582010-11-21 00:28:59 +00001357 ConstantInt *C1 = dyn_cast<ConstantInt>(MDep->getLength());
Craig Topperf40110f2014-04-25 05:29:35 +00001358 if (!C1 || C1->getValue().getZExtValue() < ByValSize)
Chris Lattner58f9f582010-11-21 00:28:59 +00001359 return false;
1360
Chris Lattner83791ce2011-05-23 00:03:39 +00001361 // Get the alignment of the byval. If the call doesn't specify the alignment,
1362 // then it is some target specific value that we can't know.
Reid Kleckner859f8b52017-04-28 20:34:27 +00001363 unsigned ByValAlign = CS.getParamAlignment(ArgNo);
Chris Lattner83791ce2011-05-23 00:03:39 +00001364 if (ByValAlign == 0) return false;
Nadav Rotem465834c2012-07-24 10:51:42 +00001365
Chris Lattner83791ce2011-05-23 00:03:39 +00001366 // If it is greater than the memcpy, then we check to see if we can force the
1367 // source of the memcpy to the alignment we need. If we fail, we bail out.
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001368 AssumptionCache &AC = LookupAssumptionCache();
Sean Silva6347df02016-06-14 02:44:55 +00001369 DominatorTree &DT = LookupDomTree();
Daniel Neilson6f1eb582018-03-21 14:14:55 +00001370 if (MDep->getSourceAlignment() < ByValAlign &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001371 getOrEnforceKnownAlignment(MDep->getSource(), ByValAlign, DL,
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001372 CS.getInstruction(), &AC, &DT) < ByValAlign)
Chris Lattner83791ce2011-05-23 00:03:39 +00001373 return false;
Nadav Rotem465834c2012-07-24 10:51:42 +00001374
Matt Arsenaultdaa08872017-04-10 19:00:25 +00001375 // The address space of the memcpy source must match the byval argument
1376 if (MDep->getSource()->getType()->getPointerAddressSpace() !=
1377 ByValArg->getType()->getPointerAddressSpace())
1378 return false;
1379
Chris Lattner58f9f582010-11-21 00:28:59 +00001380 // Verify that the copied-from memory doesn't change in between the memcpy and
1381 // the byval call.
1382 // memcpy(a <- b)
1383 // *b = 42;
1384 // foo(*a)
1385 // It would be invalid to transform the second memcpy into foo(*b).
Chris Lattner59572292010-11-21 08:06:10 +00001386 //
1387 // NOTE: This is conservative, it will stop on any read from the source loc,
1388 // not just the defining memcpy.
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +00001389 MemDepResult SourceDep = MD->getPointerDependencyFrom(
1390 MemoryLocation::getForSource(MDep), false,
1391 CS.getInstruction()->getIterator(), MDep->getParent());
Chris Lattner59572292010-11-21 08:06:10 +00001392 if (!SourceDep.isClobber() || SourceDep.getInst() != MDep)
1393 return false;
Nadav Rotem465834c2012-07-24 10:51:42 +00001394
Chris Lattner58f9f582010-11-21 00:28:59 +00001395 Value *TmpCast = MDep->getSource();
1396 if (MDep->getSource()->getType() != ByValArg->getType())
1397 TmpCast = new BitCastInst(MDep->getSource(), ByValArg->getType(),
1398 "tmpcast", CS.getInstruction());
Nadav Rotem465834c2012-07-24 10:51:42 +00001399
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001400 LLVM_DEBUG(dbgs() << "MemCpyOptPass: Forwarding memcpy to byval:\n"
1401 << " " << *MDep << "\n"
1402 << " " << *CS.getInstruction() << "\n");
Nadav Rotem465834c2012-07-24 10:51:42 +00001403
Chris Lattner58f9f582010-11-21 00:28:59 +00001404 // Otherwise we're good! Update the byval argument.
1405 CS.setArgument(ArgNo, TmpCast);
1406 ++NumMemCpyInstr;
1407 return true;
1408}
1409
Sean Silva6347df02016-06-14 02:44:55 +00001410/// Executes one iteration of MemCpyOptPass.
1411bool MemCpyOptPass::iterateOnFunction(Function &F) {
Chris Lattnerb5557a72009-09-01 17:09:55 +00001412 bool MadeChange = false;
Owen Andersonef9a6fd2008-04-09 08:23:16 +00001413
Bjorn Pettersson8e484dc2018-04-23 19:55:04 +00001414 DominatorTree &DT = LookupDomTree();
1415
Chris Lattnerb5557a72009-09-01 17:09:55 +00001416 // Walk all instruction in the function.
Benjamin Kramer135f7352016-06-26 12:28:59 +00001417 for (BasicBlock &BB : F) {
Bjorn Pettersson8e484dc2018-04-23 19:55:04 +00001418 // Skip unreachable blocks. For example processStore assumes that an
1419 // instruction in a BB can't be dominated by a later instruction in the
1420 // same BB (which is a scenario that can happen for an unreachable BB that
1421 // has itself as a predecessor).
1422 if (!DT.isReachableFromEntry(&BB))
1423 continue;
1424
Benjamin Kramer135f7352016-06-26 12:28:59 +00001425 for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) {
Bjorn Pettersson8e484dc2018-04-23 19:55:04 +00001426 // Avoid invalidating the iterator.
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +00001427 Instruction *I = &*BI++;
Nadav Rotem465834c2012-07-24 10:51:42 +00001428
Chris Lattner58f9f582010-11-21 00:28:59 +00001429 bool RepeatInstruction = false;
Nadav Rotem465834c2012-07-24 10:51:42 +00001430
Owen Anderson6a7355c2008-04-21 07:45:10 +00001431 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattnerb5557a72009-09-01 17:09:55 +00001432 MadeChange |= processStore(SI, BI);
Chris Lattner9a1d63b2011-01-08 21:19:19 +00001433 else if (MemSetInst *M = dyn_cast<MemSetInst>(I))
1434 RepeatInstruction = processMemSet(M, BI);
1435 else if (MemCpyInst *M = dyn_cast<MemCpyInst>(I))
Tim Northover39617352016-05-10 21:49:40 +00001436 RepeatInstruction = processMemCpy(M);
Chris Lattner9a1d63b2011-01-08 21:19:19 +00001437 else if (MemMoveInst *M = dyn_cast<MemMoveInst>(I))
Chris Lattner58f9f582010-11-21 00:28:59 +00001438 RepeatInstruction = processMemMove(M);
Benjamin Kramer3a09ef62015-04-10 14:50:08 +00001439 else if (auto CS = CallSite(I)) {
Chris Lattner58f9f582010-11-21 00:28:59 +00001440 for (unsigned i = 0, e = CS.arg_size(); i != e; ++i)
Nick Lewycky612d70b2011-11-20 19:09:04 +00001441 if (CS.isByValArgument(i))
Chris Lattner58f9f582010-11-21 00:28:59 +00001442 MadeChange |= processByValArgument(CS, i);
1443 }
1444
1445 // Reprocess the instruction if desired.
1446 if (RepeatInstruction) {
Benjamin Kramer135f7352016-06-26 12:28:59 +00001447 if (BI != BB.begin())
1448 --BI;
Chris Lattner58f9f582010-11-21 00:28:59 +00001449 MadeChange = true;
Chris Lattner1145e332009-09-01 17:56:32 +00001450 }
Owen Andersonef9a6fd2008-04-09 08:23:16 +00001451 }
1452 }
Nadav Rotem465834c2012-07-24 10:51:42 +00001453
Chris Lattnerb5557a72009-09-01 17:09:55 +00001454 return MadeChange;
Owen Andersonef9a6fd2008-04-09 08:23:16 +00001455}
Chris Lattnerb5557a72009-09-01 17:09:55 +00001456
Sean Silva6347df02016-06-14 02:44:55 +00001457PreservedAnalyses MemCpyOptPass::run(Function &F, FunctionAnalysisManager &AM) {
Sean Silva6347df02016-06-14 02:44:55 +00001458 auto &MD = AM.getResult<MemoryDependenceAnalysis>(F);
1459 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
1460
1461 auto LookupAliasAnalysis = [&]() -> AliasAnalysis & {
1462 return AM.getResult<AAManager>(F);
1463 };
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001464 auto LookupAssumptionCache = [&]() -> AssumptionCache & {
1465 return AM.getResult<AssumptionAnalysis>(F);
1466 };
Sean Silva6347df02016-06-14 02:44:55 +00001467 auto LookupDomTree = [&]() -> DominatorTree & {
1468 return AM.getResult<DominatorTreeAnalysis>(F);
1469 };
1470
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001471 bool MadeChange = runImpl(F, &MD, &TLI, LookupAliasAnalysis,
1472 LookupAssumptionCache, LookupDomTree);
Sean Silva6347df02016-06-14 02:44:55 +00001473 if (!MadeChange)
1474 return PreservedAnalyses::all();
Chandler Carruthca68a3e2017-01-15 06:32:49 +00001475
Sean Silva6347df02016-06-14 02:44:55 +00001476 PreservedAnalyses PA;
Chandler Carruthca68a3e2017-01-15 06:32:49 +00001477 PA.preserveSet<CFGAnalyses>();
Sean Silva6347df02016-06-14 02:44:55 +00001478 PA.preserve<GlobalsAA>();
1479 PA.preserve<MemoryDependenceAnalysis>();
1480 return PA;
1481}
1482
1483bool MemCpyOptPass::runImpl(
1484 Function &F, MemoryDependenceResults *MD_, TargetLibraryInfo *TLI_,
1485 std::function<AliasAnalysis &()> LookupAliasAnalysis_,
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001486 std::function<AssumptionCache &()> LookupAssumptionCache_,
Sean Silva6347df02016-06-14 02:44:55 +00001487 std::function<DominatorTree &()> LookupDomTree_) {
Chris Lattnerb5557a72009-09-01 17:09:55 +00001488 bool MadeChange = false;
Sean Silva6347df02016-06-14 02:44:55 +00001489 MD = MD_;
1490 TLI = TLI_;
Benjamin Kramer1afc1de2016-06-17 20:41:14 +00001491 LookupAliasAnalysis = std::move(LookupAliasAnalysis_);
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001492 LookupAssumptionCache = std::move(LookupAssumptionCache_);
Benjamin Kramer1afc1de2016-06-17 20:41:14 +00001493 LookupDomTree = std::move(LookupDomTree_);
Nadav Rotem465834c2012-07-24 10:51:42 +00001494
Chris Lattner23f61a02011-05-01 18:27:11 +00001495 // If we don't have at least memset and memcpy, there is little point of doing
1496 // anything here. These are required by a freestanding implementation, so if
1497 // even they are disabled, there is no point in trying hard.
David L. Jonesd21529f2017-01-23 23:16:46 +00001498 if (!TLI->has(LibFunc_memset) || !TLI->has(LibFunc_memcpy))
Chris Lattner23f61a02011-05-01 18:27:11 +00001499 return false;
Nadav Rotem465834c2012-07-24 10:51:42 +00001500
Eugene Zelenko34c23272017-01-18 00:57:48 +00001501 while (true) {
Chris Lattnerb5557a72009-09-01 17:09:55 +00001502 if (!iterateOnFunction(F))
1503 break;
1504 MadeChange = true;
1505 }
Nadav Rotem465834c2012-07-24 10:51:42 +00001506
Craig Topperf40110f2014-04-25 05:29:35 +00001507 MD = nullptr;
Chris Lattnerb5557a72009-09-01 17:09:55 +00001508 return MadeChange;
1509}
Sean Silva6347df02016-06-14 02:44:55 +00001510
1511/// This is the main transformation entry point for a function.
1512bool MemCpyOptLegacyPass::runOnFunction(Function &F) {
1513 if (skipFunction(F))
1514 return false;
1515
1516 auto *MD = &getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
1517 auto *TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1518
1519 auto LookupAliasAnalysis = [this]() -> AliasAnalysis & {
1520 return getAnalysis<AAResultsWrapperPass>().getAAResults();
1521 };
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001522 auto LookupAssumptionCache = [this, &F]() -> AssumptionCache & {
1523 return getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
1524 };
Sean Silva6347df02016-06-14 02:44:55 +00001525 auto LookupDomTree = [this]() -> DominatorTree & {
1526 return getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1527 };
1528
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001529 return Impl.runImpl(F, MD, TLI, LookupAliasAnalysis, LookupAssumptionCache,
1530 LookupDomTree);
Sean Silva6347df02016-06-14 02:44:55 +00001531}