blob: aca014f04cd84e6cca9efecc27ed9dcb3f732c90 [file] [log] [blame]
Chris Lattnera65e2f72010-01-05 05:57:49 +00001//===- InstCombineLoadStoreAlloca.cpp -------------------------------------===//
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 file implements the visit functions for load, store and alloca.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carrutha9174582015-01-22 05:25:13 +000014#include "InstCombineInternal.h"
NAKAMURA Takumiec6b1fc2015-12-15 09:37:31 +000015#include "llvm/ADT/SmallString.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/Statistic.h"
Dan Gohman826bdf82010-05-28 16:19:17 +000017#include "llvm/Analysis/Loads.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/DataLayout.h"
Chandler Carruthbc6378d2014-10-19 10:46:46 +000019#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/IntrinsicInst.h"
Charles Davis33d1dc02015-02-25 05:10:25 +000021#include "llvm/IR/MDBuilder.h"
Chris Lattnera65e2f72010-01-05 05:57:49 +000022#include "llvm/Transforms/Utils/BasicBlockUtils.h"
23#include "llvm/Transforms/Utils/Local.h"
Chris Lattnera65e2f72010-01-05 05:57:49 +000024using namespace llvm;
25
Chandler Carruth964daaa2014-04-22 02:55:47 +000026#define DEBUG_TYPE "instcombine"
27
Chandler Carruthc908ca12012-08-21 08:39:44 +000028STATISTIC(NumDeadStore, "Number of dead stores eliminated");
29STATISTIC(NumGlobalCopies, "Number of allocas copied from constant global");
30
31/// pointsToConstantGlobal - Return true if V (possibly indirectly) points to
32/// some part of a constant global variable. This intentionally only accepts
33/// constant expressions because we can't rewrite arbitrary instructions.
34static bool pointsToConstantGlobal(Value *V) {
35 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
36 return GV->isConstant();
Matt Arsenault607281772014-04-24 00:01:09 +000037
38 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Chandler Carruthc908ca12012-08-21 08:39:44 +000039 if (CE->getOpcode() == Instruction::BitCast ||
Matt Arsenault607281772014-04-24 00:01:09 +000040 CE->getOpcode() == Instruction::AddrSpaceCast ||
Chandler Carruthc908ca12012-08-21 08:39:44 +000041 CE->getOpcode() == Instruction::GetElementPtr)
42 return pointsToConstantGlobal(CE->getOperand(0));
Matt Arsenault607281772014-04-24 00:01:09 +000043 }
Chandler Carruthc908ca12012-08-21 08:39:44 +000044 return false;
45}
46
47/// isOnlyCopiedFromConstantGlobal - Recursively walk the uses of a (derived)
48/// pointer to an alloca. Ignore any reads of the pointer, return false if we
49/// see any stores or other unknown uses. If we see pointer arithmetic, keep
50/// track of whether it moves the pointer (with IsOffset) but otherwise traverse
51/// the uses. If we see a memcpy/memmove that targets an unoffseted pointer to
52/// the alloca, and if the source pointer is a pointer to a constant global, we
53/// can optimize this.
54static bool
55isOnlyCopiedFromConstantGlobal(Value *V, MemTransferInst *&TheCopy,
Reid Kleckner813dab22014-07-01 21:36:20 +000056 SmallVectorImpl<Instruction *> &ToDelete) {
Chandler Carruthc908ca12012-08-21 08:39:44 +000057 // We track lifetime intrinsics as we encounter them. If we decide to go
58 // ahead and replace the value with the global, this lets the caller quickly
59 // eliminate the markers.
60
Reid Kleckner813dab22014-07-01 21:36:20 +000061 SmallVector<std::pair<Value *, bool>, 35> ValuesToInspect;
62 ValuesToInspect.push_back(std::make_pair(V, false));
63 while (!ValuesToInspect.empty()) {
64 auto ValuePair = ValuesToInspect.pop_back_val();
65 const bool IsOffset = ValuePair.second;
66 for (auto &U : ValuePair.first->uses()) {
67 Instruction *I = cast<Instruction>(U.getUser());
Chandler Carruthc908ca12012-08-21 08:39:44 +000068
Reid Kleckner813dab22014-07-01 21:36:20 +000069 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
70 // Ignore non-volatile loads, they are always ok.
71 if (!LI->isSimple()) return false;
Chandler Carruthc908ca12012-08-21 08:39:44 +000072 continue;
73 }
Reid Kleckner813dab22014-07-01 21:36:20 +000074
75 if (isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I)) {
76 // If uses of the bitcast are ok, we are ok.
77 ValuesToInspect.push_back(std::make_pair(I, IsOffset));
78 continue;
79 }
80 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
81 // If the GEP has all zero indices, it doesn't offset the pointer. If it
82 // doesn't, it does.
83 ValuesToInspect.push_back(
84 std::make_pair(I, IsOffset || !GEP->hasAllZeroIndices()));
85 continue;
86 }
87
Benjamin Kramer3a09ef62015-04-10 14:50:08 +000088 if (auto CS = CallSite(I)) {
Reid Kleckner813dab22014-07-01 21:36:20 +000089 // If this is the function being called then we treat it like a load and
90 // ignore it.
91 if (CS.isCallee(&U))
92 continue;
93
94 // Inalloca arguments are clobbered by the call.
95 unsigned ArgNo = CS.getArgumentNo(&U);
96 if (CS.isInAllocaArgument(ArgNo))
97 return false;
98
99 // If this is a readonly/readnone call site, then we know it is just a
100 // load (but one that potentially returns the value itself), so we can
101 // ignore it if we know that the value isn't captured.
102 if (CS.onlyReadsMemory() &&
103 (CS.getInstruction()->use_empty() || CS.doesNotCapture(ArgNo)))
104 continue;
105
106 // If this is being passed as a byval argument, the caller is making a
107 // copy, so it is only a read of the alloca.
108 if (CS.isByValArgument(ArgNo))
109 continue;
110 }
111
112 // Lifetime intrinsics can be handled by the caller.
113 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
114 if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
115 II->getIntrinsicID() == Intrinsic::lifetime_end) {
116 assert(II->use_empty() && "Lifetime markers have no result to use!");
117 ToDelete.push_back(II);
118 continue;
119 }
120 }
121
122 // If this is isn't our memcpy/memmove, reject it as something we can't
123 // handle.
124 MemTransferInst *MI = dyn_cast<MemTransferInst>(I);
125 if (!MI)
126 return false;
127
128 // If the transfer is using the alloca as a source of the transfer, then
129 // ignore it since it is a load (unless the transfer is volatile).
130 if (U.getOperandNo() == 1) {
131 if (MI->isVolatile()) return false;
132 continue;
133 }
134
135 // If we already have seen a copy, reject the second one.
136 if (TheCopy) return false;
137
138 // If the pointer has been offset from the start of the alloca, we can't
139 // safely handle this.
140 if (IsOffset) return false;
141
142 // If the memintrinsic isn't using the alloca as the dest, reject it.
143 if (U.getOperandNo() != 0) return false;
144
145 // If the source of the memcpy/move is not a constant global, reject it.
146 if (!pointsToConstantGlobal(MI->getSource()))
147 return false;
148
149 // Otherwise, the transform is safe. Remember the copy instruction.
150 TheCopy = MI;
Chandler Carruthc908ca12012-08-21 08:39:44 +0000151 }
Chandler Carruthc908ca12012-08-21 08:39:44 +0000152 }
153 return true;
154}
155
156/// isOnlyCopiedFromConstantGlobal - Return true if the specified alloca is only
157/// modified by a copy from a constant global. If we can prove this, we can
158/// replace any uses of the alloca with uses of the global directly.
159static MemTransferInst *
160isOnlyCopiedFromConstantGlobal(AllocaInst *AI,
161 SmallVectorImpl<Instruction *> &ToDelete) {
Craig Topperf40110f2014-04-25 05:29:35 +0000162 MemTransferInst *TheCopy = nullptr;
Chandler Carruthc908ca12012-08-21 08:39:44 +0000163 if (isOnlyCopiedFromConstantGlobal(AI, TheCopy, ToDelete))
164 return TheCopy;
Craig Topperf40110f2014-04-25 05:29:35 +0000165 return nullptr;
Chandler Carruthc908ca12012-08-21 08:39:44 +0000166}
167
Duncan P. N. Exon Smithc6820ec2015-03-13 19:22:03 +0000168static Instruction *simplifyAllocaArraySize(InstCombiner &IC, AllocaInst &AI) {
Duncan P. N. Exon Smith720762e2015-03-13 19:30:44 +0000169 // Check for array size of 1 (scalar allocation).
Duncan P. N. Exon Smithbe95b4a2015-03-13 19:42:09 +0000170 if (!AI.isArrayAllocation()) {
171 // i32 1 is the canonical array size for scalar allocations.
172 if (AI.getArraySize()->getType()->isIntegerTy(32))
173 return nullptr;
174
175 // Canonicalize it.
176 Value *V = IC.Builder->getInt32(1);
177 AI.setOperand(0, V);
178 return &AI;
179 }
Duncan P. N. Exon Smith720762e2015-03-13 19:30:44 +0000180
Chris Lattnera65e2f72010-01-05 05:57:49 +0000181 // Convert: alloca Ty, C - where C is a constant != 1 into: alloca [C x Ty], 1
Duncan P. N. Exon Smithbb730132015-03-13 19:26:33 +0000182 if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) {
183 Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getZExtValue());
184 AllocaInst *New = IC.Builder->CreateAlloca(NewTy, nullptr, AI.getName());
185 New->setAlignment(AI.getAlignment());
Chris Lattnera65e2f72010-01-05 05:57:49 +0000186
Duncan P. N. Exon Smithbb730132015-03-13 19:26:33 +0000187 // Scan to the end of the allocation instructions, to skip over a block of
188 // allocas if possible...also skip interleaved debug info
189 //
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +0000190 BasicBlock::iterator It(New);
Duncan P. N. Exon Smithbb730132015-03-13 19:26:33 +0000191 while (isa<AllocaInst>(*It) || isa<DbgInfoIntrinsic>(*It))
192 ++It;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000193
Duncan P. N. Exon Smithbb730132015-03-13 19:26:33 +0000194 // Now that I is pointing to the first non-allocation-inst in the block,
195 // insert our getelementptr instruction...
196 //
197 Type *IdxTy = IC.getDataLayout().getIntPtrType(AI.getType());
198 Value *NullIdx = Constant::getNullValue(IdxTy);
199 Value *Idx[2] = {NullIdx, NullIdx};
200 Instruction *GEP =
Matt Arsenault640ff9d2013-08-14 00:24:05 +0000201 GetElementPtrInst::CreateInBounds(New, Idx, New->getName() + ".sub");
Duncan P. N. Exon Smithbb730132015-03-13 19:26:33 +0000202 IC.InsertNewInstBefore(GEP, *It);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000203
Duncan P. N. Exon Smithbb730132015-03-13 19:26:33 +0000204 // Now make everything use the getelementptr instead of the original
205 // allocation.
206 return IC.ReplaceInstUsesWith(AI, GEP);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000207 }
208
Duncan P. N. Exon Smithbb730132015-03-13 19:26:33 +0000209 if (isa<UndefValue>(AI.getArraySize()))
210 return IC.ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
211
Duncan P. N. Exon Smith07ff9b02015-03-13 19:34:55 +0000212 // Ensure that the alloca array size argument has type intptr_t, so that
213 // any casting is exposed early.
214 Type *IntPtrTy = IC.getDataLayout().getIntPtrType(AI.getType());
215 if (AI.getArraySize()->getType() != IntPtrTy) {
216 Value *V = IC.Builder->CreateIntCast(AI.getArraySize(), IntPtrTy, false);
217 AI.setOperand(0, V);
218 return &AI;
219 }
220
Duncan P. N. Exon Smithc6820ec2015-03-13 19:22:03 +0000221 return nullptr;
222}
223
224Instruction *InstCombiner::visitAllocaInst(AllocaInst &AI) {
225 if (auto *I = simplifyAllocaArraySize(*this, AI))
226 return I;
227
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000228 if (AI.getAllocatedType()->isSized()) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000229 // If the alignment is 0 (unspecified), assign it the preferred alignment.
230 if (AI.getAlignment() == 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000231 AI.setAlignment(DL.getPrefTypeAlignment(AI.getAllocatedType()));
Duncan Sands8bc764a2012-06-26 13:39:21 +0000232
233 // Move all alloca's of zero byte objects to the entry block and merge them
234 // together. Note that we only do this for alloca's, because malloc should
235 // allocate and return a unique pointer, even for a zero byte allocation.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000236 if (DL.getTypeAllocSize(AI.getAllocatedType()) == 0) {
Duncan Sands8bc764a2012-06-26 13:39:21 +0000237 // For a zero sized alloca there is no point in doing an array allocation.
238 // This is helpful if the array size is a complicated expression not used
239 // elsewhere.
240 if (AI.isArrayAllocation()) {
241 AI.setOperand(0, ConstantInt::get(AI.getArraySize()->getType(), 1));
242 return &AI;
243 }
244
245 // Get the first instruction in the entry block.
246 BasicBlock &EntryBlock = AI.getParent()->getParent()->getEntryBlock();
247 Instruction *FirstInst = EntryBlock.getFirstNonPHIOrDbg();
248 if (FirstInst != &AI) {
249 // If the entry block doesn't start with a zero-size alloca then move
250 // this one to the start of the entry block. There is no problem with
251 // dominance as the array size was forced to a constant earlier already.
252 AllocaInst *EntryAI = dyn_cast<AllocaInst>(FirstInst);
253 if (!EntryAI || !EntryAI->getAllocatedType()->isSized() ||
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000254 DL.getTypeAllocSize(EntryAI->getAllocatedType()) != 0) {
Duncan Sands8bc764a2012-06-26 13:39:21 +0000255 AI.moveBefore(FirstInst);
256 return &AI;
257 }
258
Richard Osborneb68053e2012-09-18 09:31:44 +0000259 // If the alignment of the entry block alloca is 0 (unspecified),
260 // assign it the preferred alignment.
261 if (EntryAI->getAlignment() == 0)
262 EntryAI->setAlignment(
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000263 DL.getPrefTypeAlignment(EntryAI->getAllocatedType()));
Duncan Sands8bc764a2012-06-26 13:39:21 +0000264 // Replace this zero-sized alloca with the one at the start of the entry
265 // block after ensuring that the address will be aligned enough for both
266 // types.
Richard Osborneb68053e2012-09-18 09:31:44 +0000267 unsigned MaxAlign = std::max(EntryAI->getAlignment(),
268 AI.getAlignment());
Duncan Sands8bc764a2012-06-26 13:39:21 +0000269 EntryAI->setAlignment(MaxAlign);
270 if (AI.getType() != EntryAI->getType())
271 return new BitCastInst(EntryAI, AI.getType());
272 return ReplaceInstUsesWith(AI, EntryAI);
273 }
274 }
Chris Lattnera65e2f72010-01-05 05:57:49 +0000275 }
276
Eli Friedmanb14873c2012-11-26 23:04:53 +0000277 if (AI.getAlignment()) {
Richard Osborne2fd29bf2012-09-24 17:10:03 +0000278 // Check to see if this allocation is only modified by a memcpy/memmove from
279 // a constant global whose alignment is equal to or exceeds that of the
280 // allocation. If this is the case, we can change all users to use
281 // the constant global instead. This is commonly produced by the CFE by
282 // constructs like "void foo() { int A[] = {1,2,3,4,5,6,7,8,9...}; }" if 'A'
283 // is only subsequently read.
284 SmallVector<Instruction *, 4> ToDelete;
285 if (MemTransferInst *Copy = isOnlyCopiedFromConstantGlobal(&AI, ToDelete)) {
Chandler Carruth66b31302015-01-04 12:03:27 +0000286 unsigned SourceAlign = getOrEnforceKnownAlignment(
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000287 Copy->getSource(), AI.getAlignment(), DL, &AI, AC, DT);
Eli Friedmanb14873c2012-11-26 23:04:53 +0000288 if (AI.getAlignment() <= SourceAlign) {
Richard Osborne2fd29bf2012-09-24 17:10:03 +0000289 DEBUG(dbgs() << "Found alloca equal to global: " << AI << '\n');
290 DEBUG(dbgs() << " memcpy = " << *Copy << '\n');
291 for (unsigned i = 0, e = ToDelete.size(); i != e; ++i)
292 EraseInstFromFunction(*ToDelete[i]);
293 Constant *TheSrc = cast<Constant>(Copy->getSource());
Matt Arsenaultbbf18c62013-12-07 02:58:45 +0000294 Constant *Cast
295 = ConstantExpr::getPointerBitCastOrAddrSpaceCast(TheSrc, AI.getType());
296 Instruction *NewI = ReplaceInstUsesWith(AI, Cast);
Richard Osborne2fd29bf2012-09-24 17:10:03 +0000297 EraseInstFromFunction(*Copy);
298 ++NumGlobalCopies;
299 return NewI;
300 }
Chandler Carruthc908ca12012-08-21 08:39:44 +0000301 }
302 }
303
Nuno Lopes95cc4f32012-07-09 18:38:20 +0000304 // At last, use the generic allocation site handler to aggressively remove
305 // unused allocas.
306 return visitAllocSite(AI);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000307}
308
Chandler Carruthbc6378d2014-10-19 10:46:46 +0000309/// \brief Helper to combine a load to a new type.
310///
311/// This just does the work of combining a load to a new type. It handles
312/// metadata, etc., and returns the new instruction. The \c NewTy should be the
313/// loaded *value* type. This will convert it to a pointer, cast the operand to
314/// that pointer type, load it, etc.
315///
316/// Note that this will create all of the instructions with whatever insert
317/// point the \c InstCombiner currently is using.
Mehdi Amini2668a482015-05-07 05:52:40 +0000318static LoadInst *combineLoadToNewType(InstCombiner &IC, LoadInst &LI, Type *NewTy,
319 const Twine &Suffix = "") {
Chandler Carruthbc6378d2014-10-19 10:46:46 +0000320 Value *Ptr = LI.getPointerOperand();
321 unsigned AS = LI.getPointerAddressSpace();
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000322 SmallVector<std::pair<unsigned, MDNode *>, 8> MD;
Chandler Carruthbc6378d2014-10-19 10:46:46 +0000323 LI.getAllMetadata(MD);
324
325 LoadInst *NewLoad = IC.Builder->CreateAlignedLoad(
326 IC.Builder->CreateBitCast(Ptr, NewTy->getPointerTo(AS)),
Mehdi Amini2668a482015-05-07 05:52:40 +0000327 LI.getAlignment(), LI.getName() + Suffix);
Charles Davis33d1dc02015-02-25 05:10:25 +0000328 MDBuilder MDB(NewLoad->getContext());
Chandler Carruthbc6378d2014-10-19 10:46:46 +0000329 for (const auto &MDPair : MD) {
330 unsigned ID = MDPair.first;
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000331 MDNode *N = MDPair.second;
Chandler Carruthbc6378d2014-10-19 10:46:46 +0000332 // Note, essentially every kind of metadata should be preserved here! This
333 // routine is supposed to clone a load instruction changing *only its type*.
334 // The only metadata it makes sense to drop is metadata which is invalidated
335 // when the pointer type changes. This should essentially never be the case
336 // in LLVM, but we explicitly switch over only known metadata to be
337 // conservatively correct. If you are adding metadata to LLVM which pertains
338 // to loads, you almost certainly want to add it here.
339 switch (ID) {
340 case LLVMContext::MD_dbg:
341 case LLVMContext::MD_tbaa:
342 case LLVMContext::MD_prof:
343 case LLVMContext::MD_fpmath:
344 case LLVMContext::MD_tbaa_struct:
345 case LLVMContext::MD_invariant_load:
346 case LLVMContext::MD_alias_scope:
347 case LLVMContext::MD_noalias:
Philip Reames5a3f5f72014-10-21 00:13:20 +0000348 case LLVMContext::MD_nontemporal:
349 case LLVMContext::MD_mem_parallel_loop_access:
Chandler Carruthbc6378d2014-10-19 10:46:46 +0000350 // All of these directly apply.
351 NewLoad->setMetadata(ID, N);
352 break;
353
Chandler Carruth87fdafc2015-02-13 02:30:01 +0000354 case LLVMContext::MD_nonnull:
Charles Davis33d1dc02015-02-25 05:10:25 +0000355 // This only directly applies if the new type is also a pointer.
356 if (NewTy->isPointerTy()) {
Chandler Carruth87fdafc2015-02-13 02:30:01 +0000357 NewLoad->setMetadata(ID, N);
Charles Davis33d1dc02015-02-25 05:10:25 +0000358 break;
359 }
360 // If it's integral now, translate it to !range metadata.
361 if (NewTy->isIntegerTy()) {
362 auto *ITy = cast<IntegerType>(NewTy);
363 auto *NullInt = ConstantExpr::getPtrToInt(
364 ConstantPointerNull::get(cast<PointerType>(Ptr->getType())), ITy);
365 auto *NonNullInt =
366 ConstantExpr::getAdd(NullInt, ConstantInt::get(ITy, 1));
367 NewLoad->setMetadata(LLVMContext::MD_range,
368 MDB.createRange(NonNullInt, NullInt));
369 }
Chandler Carruth87fdafc2015-02-13 02:30:01 +0000370 break;
Artur Pilipenko5c5011d2015-11-02 17:53:51 +0000371 case LLVMContext::MD_align:
372 case LLVMContext::MD_dereferenceable:
373 case LLVMContext::MD_dereferenceable_or_null:
374 // These only directly apply if the new type is also a pointer.
375 if (NewTy->isPointerTy())
376 NewLoad->setMetadata(ID, N);
377 break;
Chandler Carruthbc6378d2014-10-19 10:46:46 +0000378 case LLVMContext::MD_range:
379 // FIXME: It would be nice to propagate this in some way, but the type
Charles Davis33d1dc02015-02-25 05:10:25 +0000380 // conversions make it hard. If the new type is a pointer, we could
381 // translate it to !nonnull metadata.
Chandler Carruthbc6378d2014-10-19 10:46:46 +0000382 break;
383 }
384 }
Chandler Carruthbc6378d2014-10-19 10:46:46 +0000385 return NewLoad;
386}
387
Chandler Carruthfa11d832015-01-22 03:34:54 +0000388/// \brief Combine a store to a new type.
389///
390/// Returns the newly created store instruction.
391static StoreInst *combineStoreToNewValue(InstCombiner &IC, StoreInst &SI, Value *V) {
392 Value *Ptr = SI.getPointerOperand();
393 unsigned AS = SI.getPointerAddressSpace();
394 SmallVector<std::pair<unsigned, MDNode *>, 8> MD;
395 SI.getAllMetadata(MD);
396
397 StoreInst *NewStore = IC.Builder->CreateAlignedStore(
398 V, IC.Builder->CreateBitCast(Ptr, V->getType()->getPointerTo(AS)),
399 SI.getAlignment());
400 for (const auto &MDPair : MD) {
401 unsigned ID = MDPair.first;
402 MDNode *N = MDPair.second;
403 // Note, essentially every kind of metadata should be preserved here! This
404 // routine is supposed to clone a store instruction changing *only its
405 // type*. The only metadata it makes sense to drop is metadata which is
406 // invalidated when the pointer type changes. This should essentially
407 // never be the case in LLVM, but we explicitly switch over only known
408 // metadata to be conservatively correct. If you are adding metadata to
409 // LLVM which pertains to stores, you almost certainly want to add it
410 // here.
411 switch (ID) {
412 case LLVMContext::MD_dbg:
413 case LLVMContext::MD_tbaa:
414 case LLVMContext::MD_prof:
415 case LLVMContext::MD_fpmath:
416 case LLVMContext::MD_tbaa_struct:
417 case LLVMContext::MD_alias_scope:
418 case LLVMContext::MD_noalias:
419 case LLVMContext::MD_nontemporal:
420 case LLVMContext::MD_mem_parallel_loop_access:
Chandler Carruthfa11d832015-01-22 03:34:54 +0000421 // All of these directly apply.
422 NewStore->setMetadata(ID, N);
423 break;
424
425 case LLVMContext::MD_invariant_load:
Chandler Carruth87fdafc2015-02-13 02:30:01 +0000426 case LLVMContext::MD_nonnull:
Chandler Carruthfa11d832015-01-22 03:34:54 +0000427 case LLVMContext::MD_range:
Artur Pilipenko5c5011d2015-11-02 17:53:51 +0000428 case LLVMContext::MD_align:
429 case LLVMContext::MD_dereferenceable:
430 case LLVMContext::MD_dereferenceable_or_null:
Chandler Carruth87fdafc2015-02-13 02:30:01 +0000431 // These don't apply for stores.
Chandler Carruthfa11d832015-01-22 03:34:54 +0000432 break;
433 }
434 }
435
436 return NewStore;
437}
438
Chandler Carruth2f75fcf2014-10-18 06:36:22 +0000439/// \brief Combine loads to match the type of value their uses after looking
440/// through intervening bitcasts.
441///
442/// The core idea here is that if the result of a load is used in an operation,
443/// we should load the type most conducive to that operation. For example, when
444/// loading an integer and converting that immediately to a pointer, we should
445/// instead directly load a pointer.
446///
447/// However, this routine must never change the width of a load or the number of
448/// loads as that would introduce a semantic change. This combine is expected to
449/// be a semantic no-op which just allows loads to more closely model the types
450/// of their consuming operations.
451///
452/// Currently, we also refuse to change the precise type used for an atomic load
453/// or a volatile load. This is debatable, and might be reasonable to change
454/// later. However, it is risky in case some backend or other part of LLVM is
455/// relying on the exact type loaded to select appropriate atomic operations.
456static Instruction *combineLoadToOperationType(InstCombiner &IC, LoadInst &LI) {
457 // FIXME: We could probably with some care handle both volatile and atomic
458 // loads here but it isn't clear that this is important.
459 if (!LI.isSimple())
460 return nullptr;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000461
Chandler Carruth2f75fcf2014-10-18 06:36:22 +0000462 if (LI.use_empty())
463 return nullptr;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000464
Chandler Carruthcd8522e2015-01-22 05:08:12 +0000465 Type *Ty = LI.getType();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000466 const DataLayout &DL = IC.getDataLayout();
Chandler Carruthcd8522e2015-01-22 05:08:12 +0000467
468 // Try to canonicalize loads which are only ever stored to operate over
469 // integers instead of any other type. We only do this when the loaded type
470 // is sized and has a size exactly the same as its store size and the store
471 // size is a legal integer type.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000472 if (!Ty->isIntegerTy() && Ty->isSized() &&
473 DL.isLegalInteger(DL.getTypeStoreSizeInBits(Ty)) &&
474 DL.getTypeStoreSizeInBits(Ty) == DL.getTypeSizeInBits(Ty)) {
Chandler Carruthcd8522e2015-01-22 05:08:12 +0000475 if (std::all_of(LI.user_begin(), LI.user_end(), [&LI](User *U) {
476 auto *SI = dyn_cast<StoreInst>(U);
477 return SI && SI->getPointerOperand() != &LI;
478 })) {
479 LoadInst *NewLoad = combineLoadToNewType(
480 IC, LI,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000481 Type::getIntNTy(LI.getContext(), DL.getTypeStoreSizeInBits(Ty)));
Chandler Carruthcd8522e2015-01-22 05:08:12 +0000482 // Replace all the stores with stores of the newly loaded value.
483 for (auto UI = LI.user_begin(), UE = LI.user_end(); UI != UE;) {
484 auto *SI = cast<StoreInst>(*UI++);
485 IC.Builder->SetInsertPoint(SI);
486 combineStoreToNewValue(IC, *SI, NewLoad);
487 IC.EraseInstFromFunction(*SI);
488 }
489 assert(LI.use_empty() && "Failed to remove all users of the load!");
490 // Return the old load so the combiner can delete it safely.
491 return &LI;
492 }
493 }
Chris Lattnera65e2f72010-01-05 05:57:49 +0000494
Chandler Carruth2f75fcf2014-10-18 06:36:22 +0000495 // Fold away bit casts of the loaded value by loading the desired type.
David Majnemerdd043522015-05-28 18:39:17 +0000496 // We can do this for BitCastInsts as well as casts from and to pointer types,
497 // as long as those are noops (i.e., the source or dest type have the same
498 // bitwidth as the target's pointers).
Chandler Carruth2f75fcf2014-10-18 06:36:22 +0000499 if (LI.hasOneUse())
David Majnemerdd043522015-05-28 18:39:17 +0000500 if (auto* CI = dyn_cast<CastInst>(LI.user_back())) {
501 if (CI->isNoopCast(DL)) {
502 LoadInst *NewLoad = combineLoadToNewType(IC, LI, CI->getDestTy());
503 CI->replaceAllUsesWith(NewLoad);
504 IC.EraseInstFromFunction(*CI);
505 return &LI;
506 }
Chris Lattnera65e2f72010-01-05 05:57:49 +0000507 }
Chandler Carruth2f75fcf2014-10-18 06:36:22 +0000508
Chandler Carrutha7f247e2014-12-09 19:21:16 +0000509 // FIXME: We should also canonicalize loads of vectors when their elements are
510 // cast to other types.
Craig Topperf40110f2014-04-25 05:29:35 +0000511 return nullptr;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000512}
513
Mehdi Amini2668a482015-05-07 05:52:40 +0000514static Instruction *unpackLoadToAggregate(InstCombiner &IC, LoadInst &LI) {
515 // FIXME: We could probably with some care handle both volatile and atomic
516 // stores here but it isn't clear that this is important.
517 if (!LI.isSimple())
518 return nullptr;
519
520 Type *T = LI.getType();
521 if (!T->isAggregateType())
522 return nullptr;
523
Bruce Mitchenere9ffb452015-09-12 01:17:08 +0000524 assert(LI.getAlignment() && "Alignment must be set at this point");
Mehdi Amini2668a482015-05-07 05:52:40 +0000525
526 if (auto *ST = dyn_cast<StructType>(T)) {
527 // If the struct only have one element, we unpack.
Mehdi Amini1c131b32015-12-15 01:44:07 +0000528 unsigned Count = ST->getNumElements();
529 if (Count == 1) {
Mehdi Amini2668a482015-05-07 05:52:40 +0000530 LoadInst *NewLoad = combineLoadToNewType(IC, LI, ST->getTypeAtIndex(0U),
531 ".unpack");
532 return IC.ReplaceInstUsesWith(LI, IC.Builder->CreateInsertValue(
533 UndefValue::get(T), NewLoad, 0, LI.getName()));
534 }
Mehdi Amini1c131b32015-12-15 01:44:07 +0000535
536 // We don't want to break loads with padding here as we'd loose
537 // the knowledge that padding exists for the rest of the pipeline.
538 const DataLayout &DL = IC.getDataLayout();
539 auto *SL = DL.getStructLayout(ST);
540 if (SL->hasPadding())
541 return nullptr;
542
543 auto Name = LI.getName();
NAKAMURA Takumiec6b1fc2015-12-15 09:37:31 +0000544 SmallString<16> LoadName = Name;
545 LoadName += ".unpack";
546 SmallString<16> EltName = Name;
547 EltName += ".elt";
Mehdi Amini1c131b32015-12-15 01:44:07 +0000548 auto *Addr = LI.getPointerOperand();
549 Value *V = UndefValue::get(T);
550 auto *IdxType = Type::getInt32Ty(ST->getContext());
551 auto *Zero = ConstantInt::get(IdxType, 0);
552 for (unsigned i = 0; i < Count; i++) {
553 Value *Indices[2] = {
554 Zero,
555 ConstantInt::get(IdxType, i),
556 };
557 auto *Ptr = IC.Builder->CreateInBoundsGEP(ST, Addr, makeArrayRef(Indices), EltName);
558 auto *L = IC.Builder->CreateLoad(ST->getTypeAtIndex(i), Ptr, LoadName);
559 V = IC.Builder->CreateInsertValue(V, L, i);
560 }
561
562 V->setName(Name);
563 return IC.ReplaceInstUsesWith(LI, V);
Mehdi Amini2668a482015-05-07 05:52:40 +0000564 }
565
David Majnemer58fb0382015-05-11 05:04:22 +0000566 if (auto *AT = dyn_cast<ArrayType>(T)) {
567 // If the array only have one element, we unpack.
568 if (AT->getNumElements() == 1) {
569 LoadInst *NewLoad = combineLoadToNewType(IC, LI, AT->getElementType(),
570 ".unpack");
571 return IC.ReplaceInstUsesWith(LI, IC.Builder->CreateInsertValue(
572 UndefValue::get(T), NewLoad, 0, LI.getName()));
573 }
574 }
575
Mehdi Amini2668a482015-05-07 05:52:40 +0000576 return nullptr;
577}
578
Hal Finkel847e05f2015-02-20 03:05:53 +0000579// If we can determine that all possible objects pointed to by the provided
580// pointer value are, not only dereferenceable, but also definitively less than
581// or equal to the provided maximum size, then return true. Otherwise, return
582// false (constant global values and allocas fall into this category).
583//
584// FIXME: This should probably live in ValueTracking (or similar).
585static bool isObjectSizeLessThanOrEq(Value *V, uint64_t MaxSize,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000586 const DataLayout &DL) {
Hal Finkel847e05f2015-02-20 03:05:53 +0000587 SmallPtrSet<Value *, 4> Visited;
588 SmallVector<Value *, 4> Worklist(1, V);
589
590 do {
591 Value *P = Worklist.pop_back_val();
592 P = P->stripPointerCasts();
593
594 if (!Visited.insert(P).second)
595 continue;
596
597 if (SelectInst *SI = dyn_cast<SelectInst>(P)) {
598 Worklist.push_back(SI->getTrueValue());
599 Worklist.push_back(SI->getFalseValue());
600 continue;
601 }
602
603 if (PHINode *PN = dyn_cast<PHINode>(P)) {
Pete Cooper833f34d2015-05-12 20:05:31 +0000604 for (Value *IncValue : PN->incoming_values())
605 Worklist.push_back(IncValue);
Hal Finkel847e05f2015-02-20 03:05:53 +0000606 continue;
607 }
608
609 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(P)) {
610 if (GA->mayBeOverridden())
611 return false;
612 Worklist.push_back(GA->getAliasee());
613 continue;
614 }
615
616 // If we know how big this object is, and it is less than MaxSize, continue
617 // searching. Otherwise, return false.
618 if (AllocaInst *AI = dyn_cast<AllocaInst>(P)) {
619 if (!AI->getAllocatedType()->isSized())
620 return false;
621
622 ConstantInt *CS = dyn_cast<ConstantInt>(AI->getArraySize());
623 if (!CS)
624 return false;
625
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000626 uint64_t TypeSize = DL.getTypeAllocSize(AI->getAllocatedType());
Hal Finkel847e05f2015-02-20 03:05:53 +0000627 // Make sure that, even if the multiplication below would wrap as an
628 // uint64_t, we still do the right thing.
629 if ((CS->getValue().zextOrSelf(128)*APInt(128, TypeSize)).ugt(MaxSize))
630 return false;
631 continue;
632 }
633
634 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) {
635 if (!GV->hasDefinitiveInitializer() || !GV->isConstant())
636 return false;
637
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000638 uint64_t InitSize = DL.getTypeAllocSize(GV->getType()->getElementType());
Hal Finkel847e05f2015-02-20 03:05:53 +0000639 if (InitSize > MaxSize)
640 return false;
641 continue;
642 }
643
644 return false;
645 } while (!Worklist.empty());
646
647 return true;
648}
649
650// If we're indexing into an object of a known size, and the outer index is
651// not a constant, but having any value but zero would lead to undefined
652// behavior, replace it with zero.
653//
654// For example, if we have:
655// @f.a = private unnamed_addr constant [1 x i32] [i32 12], align 4
656// ...
657// %arrayidx = getelementptr inbounds [1 x i32]* @f.a, i64 0, i64 %x
658// ... = load i32* %arrayidx, align 4
659// Then we know that we can replace %x in the GEP with i64 0.
660//
661// FIXME: We could fold any GEP index to zero that would cause UB if it were
662// not zero. Currently, we only handle the first such index. Also, we could
663// also search through non-zero constant indices if we kept track of the
664// offsets those indices implied.
665static bool canReplaceGEPIdxWithZero(InstCombiner &IC, GetElementPtrInst *GEPI,
666 Instruction *MemI, unsigned &Idx) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000667 if (GEPI->getNumOperands() < 2)
Hal Finkel847e05f2015-02-20 03:05:53 +0000668 return false;
669
670 // Find the first non-zero index of a GEP. If all indices are zero, return
671 // one past the last index.
672 auto FirstNZIdx = [](const GetElementPtrInst *GEPI) {
673 unsigned I = 1;
674 for (unsigned IE = GEPI->getNumOperands(); I != IE; ++I) {
675 Value *V = GEPI->getOperand(I);
676 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
677 if (CI->isZero())
678 continue;
679
680 break;
681 }
682
683 return I;
684 };
685
686 // Skip through initial 'zero' indices, and find the corresponding pointer
687 // type. See if the next index is not a constant.
688 Idx = FirstNZIdx(GEPI);
689 if (Idx == GEPI->getNumOperands())
690 return false;
691 if (isa<Constant>(GEPI->getOperand(Idx)))
692 return false;
693
694 SmallVector<Value *, 4> Ops(GEPI->idx_begin(), GEPI->idx_begin() + Idx);
David Blaikied288fb82015-03-30 21:41:43 +0000695 Type *AllocTy = GetElementPtrInst::getIndexedType(
696 cast<PointerType>(GEPI->getOperand(0)->getType()->getScalarType())
697 ->getElementType(),
698 Ops);
Hal Finkel847e05f2015-02-20 03:05:53 +0000699 if (!AllocTy || !AllocTy->isSized())
700 return false;
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000701 const DataLayout &DL = IC.getDataLayout();
702 uint64_t TyAllocSize = DL.getTypeAllocSize(AllocTy);
Hal Finkel847e05f2015-02-20 03:05:53 +0000703
704 // If there are more indices after the one we might replace with a zero, make
705 // sure they're all non-negative. If any of them are negative, the overall
706 // address being computed might be before the base address determined by the
707 // first non-zero index.
708 auto IsAllNonNegative = [&]() {
709 for (unsigned i = Idx+1, e = GEPI->getNumOperands(); i != e; ++i) {
710 bool KnownNonNegative, KnownNegative;
711 IC.ComputeSignBit(GEPI->getOperand(i), KnownNonNegative,
712 KnownNegative, 0, MemI);
713 if (KnownNonNegative)
714 continue;
715 return false;
716 }
717
718 return true;
719 };
720
721 // FIXME: If the GEP is not inbounds, and there are extra indices after the
722 // one we'll replace, those could cause the address computation to wrap
723 // (rendering the IsAllNonNegative() check below insufficient). We can do
Bruce Mitchenere9ffb452015-09-12 01:17:08 +0000724 // better, ignoring zero indices (and other indices we can prove small
Hal Finkel847e05f2015-02-20 03:05:53 +0000725 // enough not to wrap).
726 if (Idx+1 != GEPI->getNumOperands() && !GEPI->isInBounds())
727 return false;
728
729 // Note that isObjectSizeLessThanOrEq will return true only if the pointer is
730 // also known to be dereferenceable.
731 return isObjectSizeLessThanOrEq(GEPI->getOperand(0), TyAllocSize, DL) &&
732 IsAllNonNegative();
733}
734
735// If we're indexing into an object with a variable index for the memory
736// access, but the object has only one element, we can assume that the index
737// will always be zero. If we replace the GEP, return it.
738template <typename T>
739static Instruction *replaceGEPIdxWithZero(InstCombiner &IC, Value *Ptr,
740 T &MemI) {
741 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Ptr)) {
742 unsigned Idx;
743 if (canReplaceGEPIdxWithZero(IC, GEPI, &MemI, Idx)) {
744 Instruction *NewGEPI = GEPI->clone();
745 NewGEPI->setOperand(Idx,
746 ConstantInt::get(GEPI->getOperand(Idx)->getType(), 0));
747 NewGEPI->insertBefore(GEPI);
748 MemI.setOperand(MemI.getPointerOperandIndex(), NewGEPI);
749 return NewGEPI;
750 }
751 }
752
753 return nullptr;
754}
755
Chris Lattnera65e2f72010-01-05 05:57:49 +0000756Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
757 Value *Op = LI.getOperand(0);
758
Chandler Carruth2f75fcf2014-10-18 06:36:22 +0000759 // Try to canonicalize the loaded type.
760 if (Instruction *Res = combineLoadToOperationType(*this, LI))
761 return Res;
762
Chris Lattnera65e2f72010-01-05 05:57:49 +0000763 // Attempt to improve the alignment.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000764 unsigned KnownAlign = getOrEnforceKnownAlignment(
765 Op, DL.getPrefTypeAlignment(LI.getType()), DL, &LI, AC, DT);
766 unsigned LoadAlign = LI.getAlignment();
767 unsigned EffectiveLoadAlign =
768 LoadAlign != 0 ? LoadAlign : DL.getABITypeAlignment(LI.getType());
Dan Gohman36196602010-08-03 18:20:32 +0000769
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000770 if (KnownAlign > EffectiveLoadAlign)
771 LI.setAlignment(KnownAlign);
772 else if (LoadAlign == 0)
773 LI.setAlignment(EffectiveLoadAlign);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000774
Hal Finkel847e05f2015-02-20 03:05:53 +0000775 // Replace GEP indices if possible.
776 if (Instruction *NewGEPI = replaceGEPIdxWithZero(*this, Op, LI)) {
777 Worklist.Add(NewGEPI);
778 return &LI;
779 }
780
Eli Friedman8bc586e2011-08-15 22:09:40 +0000781 // None of the following transforms are legal for volatile/atomic loads.
782 // FIXME: Some of it is okay for atomic loads; needs refactoring.
Craig Topperf40110f2014-04-25 05:29:35 +0000783 if (!LI.isSimple()) return nullptr;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000784
Mehdi Amini2668a482015-05-07 05:52:40 +0000785 if (Instruction *Res = unpackLoadToAggregate(*this, LI))
786 return Res;
787
Chris Lattnera65e2f72010-01-05 05:57:49 +0000788 // Do really simple store-to-load forwarding and load CSE, to catch cases
Duncan Sands75b5d272011-02-15 09:23:02 +0000789 // where there are several consecutive memory accesses to the same location,
Chris Lattnera65e2f72010-01-05 05:57:49 +0000790 // separated by a few arithmetic operations.
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +0000791 BasicBlock::iterator BBI(LI);
Bjorn Steinbrinka91fd092015-07-10 06:55:44 +0000792 AAMDNodes AATags;
Larisse Voufo532bf712015-09-18 19:14:35 +0000793 if (Value *AvailableVal =
794 FindAvailableLoadedValue(Op, LI.getParent(), BBI,
795 DefMaxInstsToScan, AA, &AATags)) {
Bjorn Steinbrinka91fd092015-07-10 06:55:44 +0000796 if (LoadInst *NLI = dyn_cast<LoadInst>(AvailableVal)) {
797 unsigned KnownIDs[] = {
Artur Pilipenko5c5011d2015-11-02 17:53:51 +0000798 LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
799 LLVMContext::MD_noalias, LLVMContext::MD_range,
800 LLVMContext::MD_invariant_load, LLVMContext::MD_nonnull,
801 LLVMContext::MD_invariant_group, LLVMContext::MD_align,
802 LLVMContext::MD_dereferenceable,
803 LLVMContext::MD_dereferenceable_or_null};
Bjorn Steinbrinka91fd092015-07-10 06:55:44 +0000804 combineMetadata(NLI, &LI, KnownIDs);
Bjorn Steinbrinka91fd092015-07-10 06:55:44 +0000805 };
806
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000807 return ReplaceInstUsesWith(
Chandler Carruth1a3c2c42014-11-25 08:20:27 +0000808 LI, Builder->CreateBitOrPointerCast(AvailableVal, LI.getType(),
809 LI.getName() + ".cast"));
Bjorn Steinbrinka91fd092015-07-10 06:55:44 +0000810 }
Chris Lattnera65e2f72010-01-05 05:57:49 +0000811
812 // load(gep null, ...) -> unreachable
813 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
814 const Value *GEPI0 = GEPI->getOperand(0);
815 // TODO: Consider a target hook for valid address spaces for this xform.
816 if (isa<ConstantPointerNull>(GEPI0) && GEPI->getPointerAddressSpace() == 0){
817 // Insert a new store to null instruction before the load to indicate
818 // that this code is not reachable. We do this instead of inserting
819 // an unreachable instruction directly because we cannot modify the
820 // CFG.
821 new StoreInst(UndefValue::get(LI.getType()),
822 Constant::getNullValue(Op->getType()), &LI);
823 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
824 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000825 }
Chris Lattnera65e2f72010-01-05 05:57:49 +0000826
827 // load null/undef -> unreachable
828 // TODO: Consider a target hook for valid address spaces for this xform.
829 if (isa<UndefValue>(Op) ||
830 (isa<ConstantPointerNull>(Op) && LI.getPointerAddressSpace() == 0)) {
831 // Insert a new store to null instruction before the load to indicate that
832 // this code is not reachable. We do this instead of inserting an
833 // unreachable instruction directly because we cannot modify the CFG.
834 new StoreInst(UndefValue::get(LI.getType()),
835 Constant::getNullValue(Op->getType()), &LI);
836 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
837 }
838
Chris Lattnera65e2f72010-01-05 05:57:49 +0000839 if (Op->hasOneUse()) {
840 // Change select and PHI nodes to select values instead of addresses: this
841 // helps alias analysis out a lot, allows many others simplifications, and
842 // exposes redundancy in the code.
843 //
844 // Note that we cannot do the transformation unless we know that the
845 // introduced loads cannot trap! Something like this is valid as long as
846 // the condition is always false: load (select bool %C, int* null, int* %G),
847 // but it would not be valid if we transformed it to load from null
848 // unconditionally.
849 //
850 if (SelectInst *SI = dyn_cast<SelectInst>(Op)) {
851 // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2).
Bob Wilson56600a12010-01-30 04:42:39 +0000852 unsigned Align = LI.getAlignment();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000853 if (isSafeToLoadUnconditionally(SI->getOperand(1), SI, Align) &&
854 isSafeToLoadUnconditionally(SI->getOperand(2), SI, Align)) {
Bob Wilson4b71b6c2010-01-30 00:41:10 +0000855 LoadInst *V1 = Builder->CreateLoad(SI->getOperand(1),
Bob Wilson56600a12010-01-30 04:42:39 +0000856 SI->getOperand(1)->getName()+".val");
Bob Wilson4b71b6c2010-01-30 00:41:10 +0000857 LoadInst *V2 = Builder->CreateLoad(SI->getOperand(2),
Bob Wilson56600a12010-01-30 04:42:39 +0000858 SI->getOperand(2)->getName()+".val");
859 V1->setAlignment(Align);
860 V2->setAlignment(Align);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000861 return SelectInst::Create(SI->getCondition(), V1, V2);
862 }
863
864 // load (select (cond, null, P)) -> load P
Larisse Voufo532bf712015-09-18 19:14:35 +0000865 if (isa<ConstantPointerNull>(SI->getOperand(1)) &&
Philip Reames5ad26c32014-12-29 22:46:21 +0000866 LI.getPointerAddressSpace() == 0) {
867 LI.setOperand(0, SI->getOperand(2));
868 return &LI;
869 }
Chris Lattnera65e2f72010-01-05 05:57:49 +0000870
871 // load (select (cond, P, null)) -> load P
Philip Reames5ad26c32014-12-29 22:46:21 +0000872 if (isa<ConstantPointerNull>(SI->getOperand(2)) &&
873 LI.getPointerAddressSpace() == 0) {
874 LI.setOperand(0, SI->getOperand(1));
875 return &LI;
876 }
Chris Lattnera65e2f72010-01-05 05:57:49 +0000877 }
878 }
Craig Topperf40110f2014-04-25 05:29:35 +0000879 return nullptr;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000880}
881
Chandler Carruth816d26f2014-11-25 10:09:51 +0000882/// \brief Combine stores to match the type of value being stored.
883///
884/// The core idea here is that the memory does not have any intrinsic type and
885/// where we can we should match the type of a store to the type of value being
886/// stored.
887///
888/// However, this routine must never change the width of a store or the number of
889/// stores as that would introduce a semantic change. This combine is expected to
890/// be a semantic no-op which just allows stores to more closely model the types
891/// of their incoming values.
892///
893/// Currently, we also refuse to change the precise type used for an atomic or
894/// volatile store. This is debatable, and might be reasonable to change later.
895/// However, it is risky in case some backend or other part of LLVM is relying
896/// on the exact type stored to select appropriate atomic operations.
897///
898/// \returns true if the store was successfully combined away. This indicates
899/// the caller must erase the store instruction. We have to let the caller erase
Bruce Mitchenere9ffb452015-09-12 01:17:08 +0000900/// the store instruction as otherwise there is no way to signal whether it was
Chandler Carruth816d26f2014-11-25 10:09:51 +0000901/// combined or not: IC.EraseInstFromFunction returns a null pointer.
902static bool combineStoreToValueType(InstCombiner &IC, StoreInst &SI) {
903 // FIXME: We could probably with some care handle both volatile and atomic
904 // stores here but it isn't clear that this is important.
905 if (!SI.isSimple())
906 return false;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000907
Chandler Carruth816d26f2014-11-25 10:09:51 +0000908 Value *V = SI.getValueOperand();
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000909
Chandler Carruth816d26f2014-11-25 10:09:51 +0000910 // Fold away bit casts of the stored value by storing the original type.
911 if (auto *BC = dyn_cast<BitCastInst>(V)) {
Chandler Carrutha7f247e2014-12-09 19:21:16 +0000912 V = BC->getOperand(0);
Chandler Carruth2135b972015-01-21 23:45:01 +0000913 combineStoreToNewValue(IC, SI, V);
Chandler Carruth816d26f2014-11-25 10:09:51 +0000914 return true;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000915 }
916
Chandler Carruth816d26f2014-11-25 10:09:51 +0000917 // FIXME: We should also canonicalize loads of vectors when their elements are
918 // cast to other types.
919 return false;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000920}
921
Mehdi Aminib344ac92015-03-14 22:19:33 +0000922static bool unpackStoreToAggregate(InstCombiner &IC, StoreInst &SI) {
923 // FIXME: We could probably with some care handle both volatile and atomic
924 // stores here but it isn't clear that this is important.
925 if (!SI.isSimple())
926 return false;
927
928 Value *V = SI.getValueOperand();
929 Type *T = V->getType();
930
931 if (!T->isAggregateType())
932 return false;
933
Mehdi Amini2668a482015-05-07 05:52:40 +0000934 if (auto *ST = dyn_cast<StructType>(T)) {
Mehdi Aminib344ac92015-03-14 22:19:33 +0000935 // If the struct only have one element, we unpack.
Mehdi Amini1c131b32015-12-15 01:44:07 +0000936 unsigned Count = ST->getNumElements();
937 if (Count == 1) {
Mehdi Aminib344ac92015-03-14 22:19:33 +0000938 V = IC.Builder->CreateExtractValue(V, 0);
939 combineStoreToNewValue(IC, SI, V);
940 return true;
941 }
Mehdi Amini1c131b32015-12-15 01:44:07 +0000942
943 // We don't want to break loads with padding here as we'd loose
944 // the knowledge that padding exists for the rest of the pipeline.
945 const DataLayout &DL = IC.getDataLayout();
946 auto *SL = DL.getStructLayout(ST);
947 if (SL->hasPadding())
948 return false;
949
NAKAMURA Takumiec6b1fc2015-12-15 09:37:31 +0000950 SmallString<16> EltName = V->getName();
951 EltName += ".elt";
Mehdi Amini1c131b32015-12-15 01:44:07 +0000952 auto *Addr = SI.getPointerOperand();
NAKAMURA Takumiec6b1fc2015-12-15 09:37:31 +0000953 SmallString<16> AddrName = Addr->getName();
954 AddrName += ".repack";
Mehdi Amini1c131b32015-12-15 01:44:07 +0000955 auto *IdxType = Type::getInt32Ty(ST->getContext());
956 auto *Zero = ConstantInt::get(IdxType, 0);
957 for (unsigned i = 0; i < Count; i++) {
958 Value *Indices[2] = {
959 Zero,
960 ConstantInt::get(IdxType, i),
961 };
962 auto *Ptr = IC.Builder->CreateInBoundsGEP(ST, Addr, makeArrayRef(Indices), AddrName);
963 auto *Val = IC.Builder->CreateExtractValue(V, i, EltName);
964 IC.Builder->CreateStore(Val, Ptr);
965 }
966
967 return true;
Mehdi Aminib344ac92015-03-14 22:19:33 +0000968 }
969
David Majnemer75364602015-05-11 05:04:27 +0000970 if (auto *AT = dyn_cast<ArrayType>(T)) {
971 // If the array only have one element, we unpack.
972 if (AT->getNumElements() == 1) {
973 V = IC.Builder->CreateExtractValue(V, 0);
974 combineStoreToNewValue(IC, SI, V);
975 return true;
976 }
977 }
978
Mehdi Aminib344ac92015-03-14 22:19:33 +0000979 return false;
980}
981
Chris Lattnera65e2f72010-01-05 05:57:49 +0000982/// equivalentAddressValues - Test if A and B will obviously have the same
983/// value. This includes recognizing that %t0 and %t1 will have the same
984/// value in code like this:
985/// %t0 = getelementptr \@a, 0, 3
986/// store i32 0, i32* %t0
987/// %t1 = getelementptr \@a, 0, 3
988/// %t2 = load i32* %t1
989///
990static bool equivalentAddressValues(Value *A, Value *B) {
991 // Test if the values are trivially equivalent.
992 if (A == B) return true;
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000993
Chris Lattnera65e2f72010-01-05 05:57:49 +0000994 // Test if the values come form identical arithmetic instructions.
995 // This uses isIdenticalToWhenDefined instead of isIdenticalTo because
996 // its only used to compare two uses within the same basic block, which
997 // means that they'll always either have the same value or one of them
998 // will have an undefined value.
999 if (isa<BinaryOperator>(A) ||
1000 isa<CastInst>(A) ||
1001 isa<PHINode>(A) ||
1002 isa<GetElementPtrInst>(A))
1003 if (Instruction *BI = dyn_cast<Instruction>(B))
1004 if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
1005 return true;
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001006
Chris Lattnera65e2f72010-01-05 05:57:49 +00001007 // Otherwise they may not be equivalent.
1008 return false;
1009}
1010
Chris Lattnera65e2f72010-01-05 05:57:49 +00001011Instruction *InstCombiner::visitStoreInst(StoreInst &SI) {
1012 Value *Val = SI.getOperand(0);
1013 Value *Ptr = SI.getOperand(1);
1014
Chandler Carruth816d26f2014-11-25 10:09:51 +00001015 // Try to canonicalize the stored type.
1016 if (combineStoreToValueType(*this, SI))
1017 return EraseInstFromFunction(SI);
1018
Chris Lattnera65e2f72010-01-05 05:57:49 +00001019 // Attempt to improve the alignment.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001020 unsigned KnownAlign = getOrEnforceKnownAlignment(
1021 Ptr, DL.getPrefTypeAlignment(Val->getType()), DL, &SI, AC, DT);
1022 unsigned StoreAlign = SI.getAlignment();
1023 unsigned EffectiveStoreAlign =
1024 StoreAlign != 0 ? StoreAlign : DL.getABITypeAlignment(Val->getType());
Dan Gohman36196602010-08-03 18:20:32 +00001025
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001026 if (KnownAlign > EffectiveStoreAlign)
1027 SI.setAlignment(KnownAlign);
1028 else if (StoreAlign == 0)
1029 SI.setAlignment(EffectiveStoreAlign);
Chris Lattnera65e2f72010-01-05 05:57:49 +00001030
Mehdi Aminib344ac92015-03-14 22:19:33 +00001031 // Try to canonicalize the stored type.
1032 if (unpackStoreToAggregate(*this, SI))
1033 return EraseInstFromFunction(SI);
1034
Hal Finkel847e05f2015-02-20 03:05:53 +00001035 // Replace GEP indices if possible.
1036 if (Instruction *NewGEPI = replaceGEPIdxWithZero(*this, Ptr, SI)) {
1037 Worklist.Add(NewGEPI);
1038 return &SI;
1039 }
1040
Philip Reamesd7a6cc82015-12-17 22:19:27 +00001041 // Don't hack volatile/ordered stores.
1042 // FIXME: Some bits are legal for ordered atomic stores; needs refactoring.
1043 if (!SI.isUnordered()) return nullptr;
Eli Friedman8bc586e2011-08-15 22:09:40 +00001044
1045 // If the RHS is an alloca with a single use, zapify the store, making the
1046 // alloca dead.
1047 if (Ptr->hasOneUse()) {
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001048 if (isa<AllocaInst>(Ptr))
Eli Friedman8bc586e2011-08-15 22:09:40 +00001049 return EraseInstFromFunction(SI);
1050 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
1051 if (isa<AllocaInst>(GEP->getOperand(0))) {
1052 if (GEP->getOperand(0)->hasOneUse())
1053 return EraseInstFromFunction(SI);
1054 }
1055 }
1056 }
1057
Chris Lattnera65e2f72010-01-05 05:57:49 +00001058 // Do really simple DSE, to catch cases where there are several consecutive
1059 // stores to the same location, separated by a few arithmetic operations. This
1060 // situation often occurs with bitfield accesses.
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +00001061 BasicBlock::iterator BBI(SI);
Chris Lattnera65e2f72010-01-05 05:57:49 +00001062 for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts;
1063 --ScanInsts) {
1064 --BBI;
Victor Hernandez5f8c8c02010-01-22 19:05:05 +00001065 // Don't count debug info directives, lest they affect codegen,
1066 // and we skip pointer-to-pointer bitcasts, which are NOPs.
1067 if (isa<DbgInfoIntrinsic>(BBI) ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001068 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) {
Chris Lattnera65e2f72010-01-05 05:57:49 +00001069 ScanInsts++;
1070 continue;
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001071 }
1072
Chris Lattnera65e2f72010-01-05 05:57:49 +00001073 if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) {
1074 // Prev store isn't volatile, and stores to the same location?
Philip Reamesd7a6cc82015-12-17 22:19:27 +00001075 if (PrevSI->isUnordered() && equivalentAddressValues(PrevSI->getOperand(1),
Eli Friedman8bc586e2011-08-15 22:09:40 +00001076 SI.getOperand(1))) {
Chris Lattnera65e2f72010-01-05 05:57:49 +00001077 ++NumDeadStore;
1078 ++BBI;
1079 EraseInstFromFunction(*PrevSI);
1080 continue;
1081 }
1082 break;
1083 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001084
Chris Lattnera65e2f72010-01-05 05:57:49 +00001085 // If this is a load, we have to stop. However, if the loaded value is from
1086 // the pointer we're loading and is producing the pointer we're storing,
1087 // then *this* store is dead (X = load P; store X -> P).
1088 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
Philip Reamesd7a6cc82015-12-17 22:19:27 +00001089 if (LI == Val && equivalentAddressValues(LI->getOperand(0), Ptr)) {
1090 assert(SI.isUnordered() && "can't eliminate ordering operation");
Jin-Gu Kangb452db02011-03-14 01:21:00 +00001091 return EraseInstFromFunction(SI);
Philip Reamesd7a6cc82015-12-17 22:19:27 +00001092 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001093
Chris Lattnera65e2f72010-01-05 05:57:49 +00001094 // Otherwise, this is a load from some other location. Stores before it
1095 // may not be dead.
1096 break;
1097 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001098
Chris Lattnera65e2f72010-01-05 05:57:49 +00001099 // Don't skip over loads or things that can modify memory.
1100 if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory())
1101 break;
1102 }
Chris Lattnera65e2f72010-01-05 05:57:49 +00001103
1104 // store X, null -> turns into 'unreachable' in SimplifyCFG
1105 if (isa<ConstantPointerNull>(Ptr) && SI.getPointerAddressSpace() == 0) {
1106 if (!isa<UndefValue>(Val)) {
1107 SI.setOperand(0, UndefValue::get(Val->getType()));
1108 if (Instruction *U = dyn_cast<Instruction>(Val))
1109 Worklist.Add(U); // Dropped a use.
1110 }
Craig Topperf40110f2014-04-25 05:29:35 +00001111 return nullptr; // Do not modify these!
Chris Lattnera65e2f72010-01-05 05:57:49 +00001112 }
1113
1114 // store undef, Ptr -> noop
1115 if (isa<UndefValue>(Val))
1116 return EraseInstFromFunction(SI);
1117
Philip Reamesd7a6cc82015-12-17 22:19:27 +00001118 // The code below needs to be audited and adjusted for unordered atomics
1119 if (!SI.isSimple())
1120 return nullptr;
1121
Chris Lattnera65e2f72010-01-05 05:57:49 +00001122 // If this store is the last instruction in the basic block (possibly
Victor Hernandez5f5abd52010-01-21 23:07:15 +00001123 // excepting debug info instructions), and if the block ends with an
1124 // unconditional branch, try to move it to the successor block.
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +00001125 BBI = SI.getIterator();
Chris Lattnera65e2f72010-01-05 05:57:49 +00001126 do {
1127 ++BBI;
Victor Hernandez5f8c8c02010-01-22 19:05:05 +00001128 } while (isa<DbgInfoIntrinsic>(BBI) ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001129 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy()));
Chris Lattnera65e2f72010-01-05 05:57:49 +00001130 if (BranchInst *BI = dyn_cast<BranchInst>(BBI))
1131 if (BI->isUnconditional())
1132 if (SimplifyStoreAtEndOfBlock(SI))
Craig Topperf40110f2014-04-25 05:29:35 +00001133 return nullptr; // xform done!
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001134
Craig Topperf40110f2014-04-25 05:29:35 +00001135 return nullptr;
Chris Lattnera65e2f72010-01-05 05:57:49 +00001136}
1137
1138/// SimplifyStoreAtEndOfBlock - Turn things like:
1139/// if () { *P = v1; } else { *P = v2 }
1140/// into a phi node with a store in the successor.
1141///
1142/// Simplify things like:
1143/// *P = v1; if () { *P = v2; }
1144/// into a phi node with a store in the successor.
1145///
1146bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) {
1147 BasicBlock *StoreBB = SI.getParent();
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001148
Chris Lattnera65e2f72010-01-05 05:57:49 +00001149 // Check to see if the successor block has exactly two incoming edges. If
1150 // so, see if the other predecessor contains a store to the same location.
1151 // if so, insert a PHI node (if needed) and move the stores down.
1152 BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0);
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001153
Chris Lattnera65e2f72010-01-05 05:57:49 +00001154 // Determine whether Dest has exactly two predecessors and, if so, compute
1155 // the other predecessor.
1156 pred_iterator PI = pred_begin(DestBB);
Gabor Greif1b787df2010-07-12 15:48:26 +00001157 BasicBlock *P = *PI;
Craig Topperf40110f2014-04-25 05:29:35 +00001158 BasicBlock *OtherBB = nullptr;
Gabor Greif1b787df2010-07-12 15:48:26 +00001159
1160 if (P != StoreBB)
1161 OtherBB = P;
1162
1163 if (++PI == pred_end(DestBB))
Chris Lattnera65e2f72010-01-05 05:57:49 +00001164 return false;
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001165
Gabor Greif1b787df2010-07-12 15:48:26 +00001166 P = *PI;
1167 if (P != StoreBB) {
Chris Lattnera65e2f72010-01-05 05:57:49 +00001168 if (OtherBB)
1169 return false;
Gabor Greif1b787df2010-07-12 15:48:26 +00001170 OtherBB = P;
Chris Lattnera65e2f72010-01-05 05:57:49 +00001171 }
1172 if (++PI != pred_end(DestBB))
1173 return false;
1174
1175 // Bail out if all the relevant blocks aren't distinct (this can happen,
1176 // for example, if SI is in an infinite loop)
1177 if (StoreBB == DestBB || OtherBB == DestBB)
1178 return false;
1179
1180 // Verify that the other block ends in a branch and is not otherwise empty.
Duncan P. N. Exon Smith9f8aaf22015-10-13 16:59:33 +00001181 BasicBlock::iterator BBI(OtherBB->getTerminator());
Chris Lattnera65e2f72010-01-05 05:57:49 +00001182 BranchInst *OtherBr = dyn_cast<BranchInst>(BBI);
1183 if (!OtherBr || BBI == OtherBB->begin())
1184 return false;
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001185
Chris Lattnera65e2f72010-01-05 05:57:49 +00001186 // If the other block ends in an unconditional branch, check for the 'if then
1187 // else' case. there is an instruction before the branch.
Craig Topperf40110f2014-04-25 05:29:35 +00001188 StoreInst *OtherStore = nullptr;
Chris Lattnera65e2f72010-01-05 05:57:49 +00001189 if (OtherBr->isUnconditional()) {
1190 --BBI;
1191 // Skip over debugging info.
Victor Hernandez5f8c8c02010-01-22 19:05:05 +00001192 while (isa<DbgInfoIntrinsic>(BBI) ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001193 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) {
Chris Lattnera65e2f72010-01-05 05:57:49 +00001194 if (BBI==OtherBB->begin())
1195 return false;
1196 --BBI;
1197 }
Eli Friedman8bc586e2011-08-15 22:09:40 +00001198 // If this isn't a store, isn't a store to the same location, or is not the
1199 // right kind of store, bail out.
Chris Lattnera65e2f72010-01-05 05:57:49 +00001200 OtherStore = dyn_cast<StoreInst>(BBI);
1201 if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1) ||
Eli Friedman8bc586e2011-08-15 22:09:40 +00001202 !SI.isSameOperationAs(OtherStore))
Chris Lattnera65e2f72010-01-05 05:57:49 +00001203 return false;
1204 } else {
1205 // Otherwise, the other block ended with a conditional branch. If one of the
1206 // destinations is StoreBB, then we have the if/then case.
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001207 if (OtherBr->getSuccessor(0) != StoreBB &&
Chris Lattnera65e2f72010-01-05 05:57:49 +00001208 OtherBr->getSuccessor(1) != StoreBB)
1209 return false;
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001210
Chris Lattnera65e2f72010-01-05 05:57:49 +00001211 // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an
1212 // if/then triangle. See if there is a store to the same ptr as SI that
1213 // lives in OtherBB.
1214 for (;; --BBI) {
1215 // Check to see if we find the matching store.
1216 if ((OtherStore = dyn_cast<StoreInst>(BBI))) {
1217 if (OtherStore->getOperand(1) != SI.getOperand(1) ||
Eli Friedman8bc586e2011-08-15 22:09:40 +00001218 !SI.isSameOperationAs(OtherStore))
Chris Lattnera65e2f72010-01-05 05:57:49 +00001219 return false;
1220 break;
1221 }
1222 // If we find something that may be using or overwriting the stored
1223 // value, or if we run out of instructions, we can't do the xform.
1224 if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() ||
1225 BBI == OtherBB->begin())
1226 return false;
1227 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001228
Chris Lattnera65e2f72010-01-05 05:57:49 +00001229 // In order to eliminate the store in OtherBr, we have to
1230 // make sure nothing reads or overwrites the stored value in
1231 // StoreBB.
1232 for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) {
1233 // FIXME: This should really be AA driven.
1234 if (I->mayReadFromMemory() || I->mayWriteToMemory())
1235 return false;
1236 }
1237 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001238
Chris Lattnera65e2f72010-01-05 05:57:49 +00001239 // Insert a PHI node now if we need it.
1240 Value *MergedVal = OtherStore->getOperand(0);
1241 if (MergedVal != SI.getOperand(0)) {
Jay Foad52131342011-03-30 11:28:46 +00001242 PHINode *PN = PHINode::Create(MergedVal->getType(), 2, "storemerge");
Chris Lattnera65e2f72010-01-05 05:57:49 +00001243 PN->addIncoming(SI.getOperand(0), SI.getParent());
1244 PN->addIncoming(OtherStore->getOperand(0), OtherBB);
1245 MergedVal = InsertNewInstBefore(PN, DestBB->front());
1246 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001247
Chris Lattnera65e2f72010-01-05 05:57:49 +00001248 // Advance to a place where it is safe to insert the new store and
1249 // insert it.
Bill Wendling8ddfc092011-08-16 20:45:24 +00001250 BBI = DestBB->getFirstInsertionPt();
Eli Friedman35211c62011-05-27 00:19:40 +00001251 StoreInst *NewSI = new StoreInst(MergedVal, SI.getOperand(1),
Eli Friedman8bc586e2011-08-15 22:09:40 +00001252 SI.isVolatile(),
1253 SI.getAlignment(),
1254 SI.getOrdering(),
1255 SI.getSynchScope());
Eli Friedman35211c62011-05-27 00:19:40 +00001256 InsertNewInstBefore(NewSI, *BBI);
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001257 NewSI->setDebugLoc(OtherStore->getDebugLoc());
Eli Friedman35211c62011-05-27 00:19:40 +00001258
Hal Finkelcc39b672014-07-24 12:16:19 +00001259 // If the two stores had AA tags, merge them.
1260 AAMDNodes AATags;
1261 SI.getAAMetadata(AATags);
1262 if (AATags) {
1263 OtherStore->getAAMetadata(AATags, /* Merge = */ true);
1264 NewSI->setAAMetadata(AATags);
1265 }
Jim Grosbachbdbd7342013-04-05 21:20:12 +00001266
Chris Lattnera65e2f72010-01-05 05:57:49 +00001267 // Nuke the old stores.
1268 EraseInstFromFunction(SI);
1269 EraseInstFromFunction(*OtherStore);
1270 return true;
1271}