blob: 7388e713e5b9992777b16dfb3bb83cc51bd4c50d [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
14#include "InstCombine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/Statistic.h"
Dan Gohman826bdf82010-05-28 16:19:17 +000016#include "llvm/Analysis/Loads.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/IntrinsicInst.h"
Chris Lattnera65e2f72010-01-05 05:57:49 +000019#include "llvm/Transforms/Utils/BasicBlockUtils.h"
20#include "llvm/Transforms/Utils/Local.h"
Chris Lattnera65e2f72010-01-05 05:57:49 +000021using namespace llvm;
22
Chandler Carruthc908ca12012-08-21 08:39:44 +000023STATISTIC(NumDeadStore, "Number of dead stores eliminated");
24STATISTIC(NumGlobalCopies, "Number of allocas copied from constant global");
25
26/// pointsToConstantGlobal - Return true if V (possibly indirectly) points to
27/// some part of a constant global variable. This intentionally only accepts
28/// constant expressions because we can't rewrite arbitrary instructions.
29static bool pointsToConstantGlobal(Value *V) {
30 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
31 return GV->isConstant();
32 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
33 if (CE->getOpcode() == Instruction::BitCast ||
34 CE->getOpcode() == Instruction::GetElementPtr)
35 return pointsToConstantGlobal(CE->getOperand(0));
36 return false;
37}
38
39/// isOnlyCopiedFromConstantGlobal - Recursively walk the uses of a (derived)
40/// pointer to an alloca. Ignore any reads of the pointer, return false if we
41/// see any stores or other unknown uses. If we see pointer arithmetic, keep
42/// track of whether it moves the pointer (with IsOffset) but otherwise traverse
43/// the uses. If we see a memcpy/memmove that targets an unoffseted pointer to
44/// the alloca, and if the source pointer is a pointer to a constant global, we
45/// can optimize this.
46static bool
47isOnlyCopiedFromConstantGlobal(Value *V, MemTransferInst *&TheCopy,
48 SmallVectorImpl<Instruction *> &ToDelete,
49 bool IsOffset = false) {
50 // We track lifetime intrinsics as we encounter them. If we decide to go
51 // ahead and replace the value with the global, this lets the caller quickly
52 // eliminate the markers.
53
54 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI!=E; ++UI) {
55 User *U = cast<Instruction>(*UI);
56
57 if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
58 // Ignore non-volatile loads, they are always ok.
59 if (!LI->isSimple()) return false;
60 continue;
61 }
62
63 if (BitCastInst *BCI = dyn_cast<BitCastInst>(U)) {
64 // If uses of the bitcast are ok, we are ok.
65 if (!isOnlyCopiedFromConstantGlobal(BCI, TheCopy, ToDelete, IsOffset))
66 return false;
67 continue;
68 }
69 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
70 // If the GEP has all zero indices, it doesn't offset the pointer. If it
71 // doesn't, it does.
72 if (!isOnlyCopiedFromConstantGlobal(GEP, TheCopy, ToDelete,
73 IsOffset || !GEP->hasAllZeroIndices()))
74 return false;
75 continue;
76 }
77
78 if (CallSite CS = U) {
79 // If this is the function being called then we treat it like a load and
80 // ignore it.
81 if (CS.isCallee(UI))
82 continue;
83
84 // If this is a readonly/readnone call site, then we know it is just a
85 // load (but one that potentially returns the value itself), so we can
86 // ignore it if we know that the value isn't captured.
87 unsigned ArgNo = CS.getArgumentNo(UI);
88 if (CS.onlyReadsMemory() &&
89 (CS.getInstruction()->use_empty() || CS.doesNotCapture(ArgNo)))
90 continue;
91
92 // If this is being passed as a byval argument, the caller is making a
93 // copy, so it is only a read of the alloca.
94 if (CS.isByValArgument(ArgNo))
95 continue;
96 }
97
98 // Lifetime intrinsics can be handled by the caller.
99 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(U)) {
100 if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
101 II->getIntrinsicID() == Intrinsic::lifetime_end) {
102 assert(II->use_empty() && "Lifetime markers have no result to use!");
103 ToDelete.push_back(II);
104 continue;
105 }
106 }
107
108 // If this is isn't our memcpy/memmove, reject it as something we can't
109 // handle.
110 MemTransferInst *MI = dyn_cast<MemTransferInst>(U);
111 if (MI == 0)
112 return false;
113
114 // If the transfer is using the alloca as a source of the transfer, then
115 // ignore it since it is a load (unless the transfer is volatile).
116 if (UI.getOperandNo() == 1) {
117 if (MI->isVolatile()) return false;
118 continue;
119 }
120
121 // If we already have seen a copy, reject the second one.
122 if (TheCopy) return false;
123
124 // If the pointer has been offset from the start of the alloca, we can't
125 // safely handle this.
126 if (IsOffset) return false;
127
128 // If the memintrinsic isn't using the alloca as the dest, reject it.
129 if (UI.getOperandNo() != 0) return false;
130
131 // If the source of the memcpy/move is not a constant global, reject it.
132 if (!pointsToConstantGlobal(MI->getSource()))
133 return false;
134
135 // Otherwise, the transform is safe. Remember the copy instruction.
136 TheCopy = MI;
137 }
138 return true;
139}
140
141/// isOnlyCopiedFromConstantGlobal - Return true if the specified alloca is only
142/// modified by a copy from a constant global. If we can prove this, we can
143/// replace any uses of the alloca with uses of the global directly.
144static MemTransferInst *
145isOnlyCopiedFromConstantGlobal(AllocaInst *AI,
146 SmallVectorImpl<Instruction *> &ToDelete) {
147 MemTransferInst *TheCopy = 0;
148 if (isOnlyCopiedFromConstantGlobal(AI, TheCopy, ToDelete))
149 return TheCopy;
150 return 0;
151}
152
Chris Lattnera65e2f72010-01-05 05:57:49 +0000153Instruction *InstCombiner::visitAllocaInst(AllocaInst &AI) {
Dan Gohmandf5d7dc2010-05-28 15:09:00 +0000154 // Ensure that the alloca array size argument has type intptr_t, so that
155 // any casting is exposed early.
156 if (TD) {
Chandler Carruth7ec50852012-11-01 08:07:29 +0000157 Type *IntPtrTy = TD->getIntPtrType(AI.getContext());
Dan Gohmandf5d7dc2010-05-28 15:09:00 +0000158 if (AI.getArraySize()->getType() != IntPtrTy) {
159 Value *V = Builder->CreateIntCast(AI.getArraySize(),
160 IntPtrTy, false);
161 AI.setOperand(0, V);
162 return &AI;
163 }
164 }
165
Chris Lattnera65e2f72010-01-05 05:57:49 +0000166 // Convert: alloca Ty, C - where C is a constant != 1 into: alloca [C x Ty], 1
167 if (AI.isArrayAllocation()) { // Check C != 1
168 if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) {
Chandler Carruth7ec50852012-11-01 08:07:29 +0000169 Type *NewTy =
Chris Lattnera65e2f72010-01-05 05:57:49 +0000170 ArrayType::get(AI.getAllocatedType(), C->getZExtValue());
Chris Lattnera65e2f72010-01-05 05:57:49 +0000171 AllocaInst *New = Builder->CreateAlloca(NewTy, 0, AI.getName());
172 New->setAlignment(AI.getAlignment());
173
174 // Scan to the end of the allocation instructions, to skip over a block of
175 // allocas if possible...also skip interleaved debug info
176 //
177 BasicBlock::iterator It = New;
178 while (isa<AllocaInst>(*It) || isa<DbgInfoIntrinsic>(*It)) ++It;
179
180 // Now that I is pointing to the first non-allocation-inst in the block,
181 // insert our getelementptr instruction...
182 //
183 Value *NullIdx =Constant::getNullValue(Type::getInt32Ty(AI.getContext()));
184 Value *Idx[2];
185 Idx[0] = NullIdx;
186 Idx[1] = NullIdx;
Eli Friedman41e509a2011-05-18 23:58:37 +0000187 Instruction *GEP =
Jay Foadd1b78492011-07-25 09:48:08 +0000188 GetElementPtrInst::CreateInBounds(New, Idx, New->getName()+".sub");
Eli Friedman41e509a2011-05-18 23:58:37 +0000189 InsertNewInstBefore(GEP, *It);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000190
191 // Now make everything use the getelementptr instead of the original
192 // allocation.
Eli Friedman41e509a2011-05-18 23:58:37 +0000193 return ReplaceInstUsesWith(AI, GEP);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000194 } else if (isa<UndefValue>(AI.getArraySize())) {
195 return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
196 }
197 }
198
Duncan Sands8bc764a2012-06-26 13:39:21 +0000199 if (TD && AI.getAllocatedType()->isSized()) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000200 // If the alignment is 0 (unspecified), assign it the preferred alignment.
201 if (AI.getAlignment() == 0)
202 AI.setAlignment(TD->getPrefTypeAlignment(AI.getAllocatedType()));
Duncan Sands8bc764a2012-06-26 13:39:21 +0000203
204 // Move all alloca's of zero byte objects to the entry block and merge them
205 // together. Note that we only do this for alloca's, because malloc should
206 // allocate and return a unique pointer, even for a zero byte allocation.
207 if (TD->getTypeAllocSize(AI.getAllocatedType()) == 0) {
208 // For a zero sized alloca there is no point in doing an array allocation.
209 // This is helpful if the array size is a complicated expression not used
210 // elsewhere.
211 if (AI.isArrayAllocation()) {
212 AI.setOperand(0, ConstantInt::get(AI.getArraySize()->getType(), 1));
213 return &AI;
214 }
215
216 // Get the first instruction in the entry block.
217 BasicBlock &EntryBlock = AI.getParent()->getParent()->getEntryBlock();
218 Instruction *FirstInst = EntryBlock.getFirstNonPHIOrDbg();
219 if (FirstInst != &AI) {
220 // If the entry block doesn't start with a zero-size alloca then move
221 // this one to the start of the entry block. There is no problem with
222 // dominance as the array size was forced to a constant earlier already.
223 AllocaInst *EntryAI = dyn_cast<AllocaInst>(FirstInst);
224 if (!EntryAI || !EntryAI->getAllocatedType()->isSized() ||
225 TD->getTypeAllocSize(EntryAI->getAllocatedType()) != 0) {
226 AI.moveBefore(FirstInst);
227 return &AI;
228 }
229
Richard Osborneb68053e2012-09-18 09:31:44 +0000230 // If the alignment of the entry block alloca is 0 (unspecified),
231 // assign it the preferred alignment.
232 if (EntryAI->getAlignment() == 0)
233 EntryAI->setAlignment(
234 TD->getPrefTypeAlignment(EntryAI->getAllocatedType()));
Duncan Sands8bc764a2012-06-26 13:39:21 +0000235 // Replace this zero-sized alloca with the one at the start of the entry
236 // block after ensuring that the address will be aligned enough for both
237 // types.
Richard Osborneb68053e2012-09-18 09:31:44 +0000238 unsigned MaxAlign = std::max(EntryAI->getAlignment(),
239 AI.getAlignment());
Duncan Sands8bc764a2012-06-26 13:39:21 +0000240 EntryAI->setAlignment(MaxAlign);
241 if (AI.getType() != EntryAI->getType())
242 return new BitCastInst(EntryAI, AI.getType());
243 return ReplaceInstUsesWith(AI, EntryAI);
244 }
245 }
Chris Lattnera65e2f72010-01-05 05:57:49 +0000246 }
247
Eli Friedmanb14873c2012-11-26 23:04:53 +0000248 if (AI.getAlignment()) {
Richard Osborne2fd29bf2012-09-24 17:10:03 +0000249 // Check to see if this allocation is only modified by a memcpy/memmove from
250 // a constant global whose alignment is equal to or exceeds that of the
251 // allocation. If this is the case, we can change all users to use
252 // the constant global instead. This is commonly produced by the CFE by
253 // constructs like "void foo() { int A[] = {1,2,3,4,5,6,7,8,9...}; }" if 'A'
254 // is only subsequently read.
255 SmallVector<Instruction *, 4> ToDelete;
256 if (MemTransferInst *Copy = isOnlyCopiedFromConstantGlobal(&AI, ToDelete)) {
Eli Friedmanb14873c2012-11-26 23:04:53 +0000257 unsigned SourceAlign = getOrEnforceKnownAlignment(Copy->getSource(),
258 AI.getAlignment(), TD);
259 if (AI.getAlignment() <= SourceAlign) {
Richard Osborne2fd29bf2012-09-24 17:10:03 +0000260 DEBUG(dbgs() << "Found alloca equal to global: " << AI << '\n');
261 DEBUG(dbgs() << " memcpy = " << *Copy << '\n');
262 for (unsigned i = 0, e = ToDelete.size(); i != e; ++i)
263 EraseInstFromFunction(*ToDelete[i]);
264 Constant *TheSrc = cast<Constant>(Copy->getSource());
265 Instruction *NewI
266 = ReplaceInstUsesWith(AI, ConstantExpr::getBitCast(TheSrc,
267 AI.getType()));
268 EraseInstFromFunction(*Copy);
269 ++NumGlobalCopies;
270 return NewI;
271 }
Chandler Carruthc908ca12012-08-21 08:39:44 +0000272 }
273 }
274
Nuno Lopes95cc4f32012-07-09 18:38:20 +0000275 // At last, use the generic allocation site handler to aggressively remove
276 // unused allocas.
277 return visitAllocSite(AI);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000278}
279
280
281/// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible.
282static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI,
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000283 const DataLayout *TD) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000284 User *CI = cast<User>(LI.getOperand(0));
285 Value *CastOp = CI->getOperand(0);
286
Chris Lattner229907c2011-07-18 04:54:35 +0000287 PointerType *DestTy = cast<PointerType>(CI->getType());
288 Type *DestPTy = DestTy->getElementType();
289 if (PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000290
291 // If the address spaces don't match, don't eliminate the cast.
292 if (DestTy->getAddressSpace() != SrcTy->getAddressSpace())
293 return 0;
294
Chris Lattner229907c2011-07-18 04:54:35 +0000295 Type *SrcPTy = SrcTy->getElementType();
Chris Lattnera65e2f72010-01-05 05:57:49 +0000296
Chandler Carruth7ec50852012-11-01 08:07:29 +0000297 if (DestPTy->isIntegerTy() || DestPTy->isPointerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +0000298 DestPTy->isVectorTy()) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000299 // If the source is an array, the code below will not succeed. Check to
300 // see if a trivial 'gep P, 0, 0' will help matters. Only do this for
301 // constants.
Chris Lattner229907c2011-07-18 04:54:35 +0000302 if (ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy))
Chris Lattnera65e2f72010-01-05 05:57:49 +0000303 if (Constant *CSrc = dyn_cast<Constant>(CastOp))
304 if (ASrcTy->getNumElements() != 0) {
305 Value *Idxs[2];
306 Idxs[0] = Constant::getNullValue(Type::getInt32Ty(LI.getContext()));
307 Idxs[1] = Idxs[0];
Jay Foad71f19ac2011-07-22 07:54:01 +0000308 CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000309 SrcTy = cast<PointerType>(CastOp->getType());
310 SrcPTy = SrcTy->getElementType();
311 }
312
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000313 if (IC.getDataLayout() &&
Chandler Carruth7ec50852012-11-01 08:07:29 +0000314 (SrcPTy->isIntegerTy() || SrcPTy->isPointerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +0000315 SrcPTy->isVectorTy()) &&
Chris Lattnera65e2f72010-01-05 05:57:49 +0000316 // Do not allow turning this into a load of an integer, which is then
317 // casted to a pointer, this pessimizes pointer analysis a lot.
Duncan Sands19d0b472010-02-16 11:11:14 +0000318 (SrcPTy->isPointerTy() == LI.getType()->isPointerTy()) &&
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000319 IC.getDataLayout()->getTypeSizeInBits(SrcPTy) ==
320 IC.getDataLayout()->getTypeSizeInBits(DestPTy)) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000321
322 // Okay, we are casting from one integer or pointer type to another of
323 // the same size. Instead of casting the pointer before the load, cast
324 // the result of the loaded value.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000325 LoadInst *NewLoad =
Chris Lattnera65e2f72010-01-05 05:57:49 +0000326 IC.Builder->CreateLoad(CastOp, LI.isVolatile(), CI->getName());
Bob Wilson4b71b6c2010-01-30 00:41:10 +0000327 NewLoad->setAlignment(LI.getAlignment());
Eli Friedman8bc586e2011-08-15 22:09:40 +0000328 NewLoad->setAtomic(LI.getOrdering(), LI.getSynchScope());
Chris Lattnera65e2f72010-01-05 05:57:49 +0000329 // Now cast the result of the load.
330 return new BitCastInst(NewLoad, LI.getType());
331 }
332 }
333 }
334 return 0;
335}
336
337Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
338 Value *Op = LI.getOperand(0);
339
340 // Attempt to improve the alignment.
341 if (TD) {
342 unsigned KnownAlign =
Chris Lattner6fcd32e2010-12-25 20:37:57 +0000343 getOrEnforceKnownAlignment(Op, TD->getPrefTypeAlignment(LI.getType()),TD);
Dan Gohman36196602010-08-03 18:20:32 +0000344 unsigned LoadAlign = LI.getAlignment();
345 unsigned EffectiveLoadAlign = LoadAlign != 0 ? LoadAlign :
346 TD->getABITypeAlignment(LI.getType());
347
348 if (KnownAlign > EffectiveLoadAlign)
Chris Lattnera65e2f72010-01-05 05:57:49 +0000349 LI.setAlignment(KnownAlign);
Dan Gohman36196602010-08-03 18:20:32 +0000350 else if (LoadAlign == 0)
351 LI.setAlignment(EffectiveLoadAlign);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000352 }
353
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000354 // load (cast X) --> cast (load X) iff safe.
Chris Lattnera65e2f72010-01-05 05:57:49 +0000355 if (isa<CastInst>(Op))
356 if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
357 return Res;
358
Eli Friedman8bc586e2011-08-15 22:09:40 +0000359 // None of the following transforms are legal for volatile/atomic loads.
360 // FIXME: Some of it is okay for atomic loads; needs refactoring.
361 if (!LI.isSimple()) return 0;
Chandler Carruth7ec50852012-11-01 08:07:29 +0000362
Chris Lattnera65e2f72010-01-05 05:57:49 +0000363 // Do really simple store-to-load forwarding and load CSE, to catch cases
Duncan Sands75b5d272011-02-15 09:23:02 +0000364 // where there are several consecutive memory accesses to the same location,
Chris Lattnera65e2f72010-01-05 05:57:49 +0000365 // separated by a few arithmetic operations.
366 BasicBlock::iterator BBI = &LI;
367 if (Value *AvailableVal = FindAvailableLoadedValue(Op, LI.getParent(), BBI,6))
368 return ReplaceInstUsesWith(LI, AvailableVal);
369
370 // load(gep null, ...) -> unreachable
371 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
372 const Value *GEPI0 = GEPI->getOperand(0);
373 // TODO: Consider a target hook for valid address spaces for this xform.
374 if (isa<ConstantPointerNull>(GEPI0) && GEPI->getPointerAddressSpace() == 0){
375 // Insert a new store to null instruction before the load to indicate
376 // that this code is not reachable. We do this instead of inserting
377 // an unreachable instruction directly because we cannot modify the
378 // CFG.
379 new StoreInst(UndefValue::get(LI.getType()),
380 Constant::getNullValue(Op->getType()), &LI);
381 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
382 }
Chandler Carruth7ec50852012-11-01 08:07:29 +0000383 }
Chris Lattnera65e2f72010-01-05 05:57:49 +0000384
385 // load null/undef -> unreachable
386 // TODO: Consider a target hook for valid address spaces for this xform.
387 if (isa<UndefValue>(Op) ||
388 (isa<ConstantPointerNull>(Op) && LI.getPointerAddressSpace() == 0)) {
389 // Insert a new store to null instruction before the load to indicate that
390 // this code is not reachable. We do this instead of inserting an
391 // unreachable instruction directly because we cannot modify the CFG.
392 new StoreInst(UndefValue::get(LI.getType()),
393 Constant::getNullValue(Op->getType()), &LI);
394 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
395 }
396
397 // Instcombine load (constantexpr_cast global) -> cast (load global)
398 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op))
399 if (CE->isCast())
400 if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
401 return Res;
Chandler Carruth7ec50852012-11-01 08:07:29 +0000402
Chris Lattnera65e2f72010-01-05 05:57:49 +0000403 if (Op->hasOneUse()) {
404 // Change select and PHI nodes to select values instead of addresses: this
405 // helps alias analysis out a lot, allows many others simplifications, and
406 // exposes redundancy in the code.
407 //
408 // Note that we cannot do the transformation unless we know that the
409 // introduced loads cannot trap! Something like this is valid as long as
410 // the condition is always false: load (select bool %C, int* null, int* %G),
411 // but it would not be valid if we transformed it to load from null
412 // unconditionally.
413 //
414 if (SelectInst *SI = dyn_cast<SelectInst>(Op)) {
415 // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2).
Bob Wilson56600a12010-01-30 04:42:39 +0000416 unsigned Align = LI.getAlignment();
417 if (isSafeToLoadUnconditionally(SI->getOperand(1), SI, Align, TD) &&
418 isSafeToLoadUnconditionally(SI->getOperand(2), SI, Align, TD)) {
Bob Wilson4b71b6c2010-01-30 00:41:10 +0000419 LoadInst *V1 = Builder->CreateLoad(SI->getOperand(1),
Bob Wilson56600a12010-01-30 04:42:39 +0000420 SI->getOperand(1)->getName()+".val");
Bob Wilson4b71b6c2010-01-30 00:41:10 +0000421 LoadInst *V2 = Builder->CreateLoad(SI->getOperand(2),
Bob Wilson56600a12010-01-30 04:42:39 +0000422 SI->getOperand(2)->getName()+".val");
423 V1->setAlignment(Align);
424 V2->setAlignment(Align);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000425 return SelectInst::Create(SI->getCondition(), V1, V2);
426 }
427
428 // load (select (cond, null, P)) -> load P
429 if (Constant *C = dyn_cast<Constant>(SI->getOperand(1)))
430 if (C->isNullValue()) {
431 LI.setOperand(0, SI->getOperand(2));
432 return &LI;
433 }
434
435 // load (select (cond, P, null)) -> load P
436 if (Constant *C = dyn_cast<Constant>(SI->getOperand(2)))
437 if (C->isNullValue()) {
438 LI.setOperand(0, SI->getOperand(1));
439 return &LI;
440 }
441 }
442 }
443 return 0;
444}
445
446/// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P
447/// when possible. This makes it generally easy to do alias analysis and/or
448/// SROA/mem2reg of the memory object.
449static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) {
450 User *CI = cast<User>(SI.getOperand(1));
451 Value *CastOp = CI->getOperand(0);
452
Chris Lattner229907c2011-07-18 04:54:35 +0000453 Type *DestPTy = cast<PointerType>(CI->getType())->getElementType();
454 PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType());
Chris Lattnera65e2f72010-01-05 05:57:49 +0000455 if (SrcTy == 0) return 0;
Chandler Carruth7ec50852012-11-01 08:07:29 +0000456
Chris Lattner229907c2011-07-18 04:54:35 +0000457 Type *SrcPTy = SrcTy->getElementType();
Chris Lattnera65e2f72010-01-05 05:57:49 +0000458
Duncan Sands19d0b472010-02-16 11:11:14 +0000459 if (!DestPTy->isIntegerTy() && !DestPTy->isPointerTy())
Chris Lattnera65e2f72010-01-05 05:57:49 +0000460 return 0;
Chandler Carruth7ec50852012-11-01 08:07:29 +0000461
Chris Lattnera65e2f72010-01-05 05:57:49 +0000462 /// NewGEPIndices - If SrcPTy is an aggregate type, we can emit a "noop gep"
463 /// to its first element. This allows us to handle things like:
464 /// store i32 xxx, (bitcast {foo*, float}* %P to i32*)
465 /// on 32-bit hosts.
466 SmallVector<Value*, 4> NewGEPIndices;
Chandler Carruth7ec50852012-11-01 08:07:29 +0000467
Chris Lattnera65e2f72010-01-05 05:57:49 +0000468 // If the source is an array, the code below will not succeed. Check to
469 // see if a trivial 'gep P, 0, 0' will help matters. Only do this for
470 // constants.
Duncan Sands19d0b472010-02-16 11:11:14 +0000471 if (SrcPTy->isArrayTy() || SrcPTy->isStructTy()) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000472 // Index through pointer.
473 Constant *Zero = Constant::getNullValue(Type::getInt32Ty(SI.getContext()));
474 NewGEPIndices.push_back(Zero);
Chandler Carruth7ec50852012-11-01 08:07:29 +0000475
Chris Lattnera65e2f72010-01-05 05:57:49 +0000476 while (1) {
Chris Lattner229907c2011-07-18 04:54:35 +0000477 if (StructType *STy = dyn_cast<StructType>(SrcPTy)) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000478 if (!STy->getNumElements()) /* Struct can be empty {} */
479 break;
480 NewGEPIndices.push_back(Zero);
481 SrcPTy = STy->getElementType(0);
Chris Lattner229907c2011-07-18 04:54:35 +0000482 } else if (ArrayType *ATy = dyn_cast<ArrayType>(SrcPTy)) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000483 NewGEPIndices.push_back(Zero);
484 SrcPTy = ATy->getElementType();
485 } else {
486 break;
487 }
488 }
Chandler Carruth7ec50852012-11-01 08:07:29 +0000489
Chris Lattnera65e2f72010-01-05 05:57:49 +0000490 SrcTy = PointerType::get(SrcPTy, SrcTy->getAddressSpace());
491 }
492
Duncan Sands19d0b472010-02-16 11:11:14 +0000493 if (!SrcPTy->isIntegerTy() && !SrcPTy->isPointerTy())
Chris Lattnera65e2f72010-01-05 05:57:49 +0000494 return 0;
Chandler Carruth7ec50852012-11-01 08:07:29 +0000495
Chris Lattnera65e2f72010-01-05 05:57:49 +0000496 // If the pointers point into different address spaces or if they point to
497 // values with different sizes, we can't do the transformation.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000498 if (!IC.getDataLayout() ||
Chandler Carruth7ec50852012-11-01 08:07:29 +0000499 SrcTy->getAddressSpace() !=
500 cast<PointerType>(CI->getType())->getAddressSpace() ||
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000501 IC.getDataLayout()->getTypeSizeInBits(SrcPTy) !=
502 IC.getDataLayout()->getTypeSizeInBits(DestPTy))
Chris Lattnera65e2f72010-01-05 05:57:49 +0000503 return 0;
504
505 // Okay, we are casting from one integer or pointer type to another of
Chandler Carruth7ec50852012-11-01 08:07:29 +0000506 // the same size. Instead of casting the pointer before
Chris Lattnera65e2f72010-01-05 05:57:49 +0000507 // the store, cast the value to be stored.
508 Value *NewCast;
509 Value *SIOp0 = SI.getOperand(0);
510 Instruction::CastOps opcode = Instruction::BitCast;
Chris Lattner229907c2011-07-18 04:54:35 +0000511 Type* CastSrcTy = SIOp0->getType();
512 Type* CastDstTy = SrcPTy;
Duncan Sands19d0b472010-02-16 11:11:14 +0000513 if (CastDstTy->isPointerTy()) {
Duncan Sands9dff9be2010-02-15 16:12:20 +0000514 if (CastSrcTy->isIntegerTy())
Chris Lattnera65e2f72010-01-05 05:57:49 +0000515 opcode = Instruction::IntToPtr;
Duncan Sands19d0b472010-02-16 11:11:14 +0000516 } else if (CastDstTy->isIntegerTy()) {
517 if (SIOp0->getType()->isPointerTy())
Chris Lattnera65e2f72010-01-05 05:57:49 +0000518 opcode = Instruction::PtrToInt;
519 }
Chandler Carruth7ec50852012-11-01 08:07:29 +0000520
Chris Lattnera65e2f72010-01-05 05:57:49 +0000521 // SIOp0 is a pointer to aggregate and this is a store to the first field,
522 // emit a GEP to index into its first field.
523 if (!NewGEPIndices.empty())
Jay Foad040dd822011-07-22 08:16:57 +0000524 CastOp = IC.Builder->CreateInBoundsGEP(CastOp, NewGEPIndices);
Chandler Carruth7ec50852012-11-01 08:07:29 +0000525
Chris Lattnera65e2f72010-01-05 05:57:49 +0000526 NewCast = IC.Builder->CreateCast(opcode, SIOp0, CastDstTy,
527 SIOp0->getName()+".c");
Dan Gohman2e20dfb2010-10-25 16:16:27 +0000528 SI.setOperand(0, NewCast);
529 SI.setOperand(1, CastOp);
530 return &SI;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000531}
532
533/// equivalentAddressValues - Test if A and B will obviously have the same
534/// value. This includes recognizing that %t0 and %t1 will have the same
535/// value in code like this:
536/// %t0 = getelementptr \@a, 0, 3
537/// store i32 0, i32* %t0
538/// %t1 = getelementptr \@a, 0, 3
539/// %t2 = load i32* %t1
540///
541static bool equivalentAddressValues(Value *A, Value *B) {
542 // Test if the values are trivially equivalent.
543 if (A == B) return true;
Chandler Carruth7ec50852012-11-01 08:07:29 +0000544
Chris Lattnera65e2f72010-01-05 05:57:49 +0000545 // Test if the values come form identical arithmetic instructions.
546 // This uses isIdenticalToWhenDefined instead of isIdenticalTo because
547 // its only used to compare two uses within the same basic block, which
548 // means that they'll always either have the same value or one of them
549 // will have an undefined value.
550 if (isa<BinaryOperator>(A) ||
551 isa<CastInst>(A) ||
552 isa<PHINode>(A) ||
553 isa<GetElementPtrInst>(A))
554 if (Instruction *BI = dyn_cast<Instruction>(B))
555 if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
556 return true;
Chandler Carruth7ec50852012-11-01 08:07:29 +0000557
Chris Lattnera65e2f72010-01-05 05:57:49 +0000558 // Otherwise they may not be equivalent.
559 return false;
560}
561
Chris Lattnera65e2f72010-01-05 05:57:49 +0000562Instruction *InstCombiner::visitStoreInst(StoreInst &SI) {
563 Value *Val = SI.getOperand(0);
564 Value *Ptr = SI.getOperand(1);
565
Chris Lattnera65e2f72010-01-05 05:57:49 +0000566 // Attempt to improve the alignment.
567 if (TD) {
568 unsigned KnownAlign =
Chris Lattner6fcd32e2010-12-25 20:37:57 +0000569 getOrEnforceKnownAlignment(Ptr, TD->getPrefTypeAlignment(Val->getType()),
570 TD);
Dan Gohman36196602010-08-03 18:20:32 +0000571 unsigned StoreAlign = SI.getAlignment();
572 unsigned EffectiveStoreAlign = StoreAlign != 0 ? StoreAlign :
573 TD->getABITypeAlignment(Val->getType());
574
Bill Wendling55b6b2b2012-03-16 18:20:54 +0000575 if (KnownAlign > EffectiveStoreAlign)
Chris Lattnera65e2f72010-01-05 05:57:49 +0000576 SI.setAlignment(KnownAlign);
Bill Wendling55b6b2b2012-03-16 18:20:54 +0000577 else if (StoreAlign == 0)
578 SI.setAlignment(EffectiveStoreAlign);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000579 }
580
Eli Friedman8bc586e2011-08-15 22:09:40 +0000581 // Don't hack volatile/atomic stores.
582 // FIXME: Some bits are legal for atomic stores; needs refactoring.
583 if (!SI.isSimple()) return 0;
584
585 // If the RHS is an alloca with a single use, zapify the store, making the
586 // alloca dead.
587 if (Ptr->hasOneUse()) {
Chandler Carruth7ec50852012-11-01 08:07:29 +0000588 if (isa<AllocaInst>(Ptr))
Eli Friedman8bc586e2011-08-15 22:09:40 +0000589 return EraseInstFromFunction(SI);
590 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
591 if (isa<AllocaInst>(GEP->getOperand(0))) {
592 if (GEP->getOperand(0)->hasOneUse())
593 return EraseInstFromFunction(SI);
594 }
595 }
596 }
597
Chris Lattnera65e2f72010-01-05 05:57:49 +0000598 // Do really simple DSE, to catch cases where there are several consecutive
599 // stores to the same location, separated by a few arithmetic operations. This
600 // situation often occurs with bitfield accesses.
601 BasicBlock::iterator BBI = &SI;
602 for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts;
603 --ScanInsts) {
604 --BBI;
Victor Hernandez5f8c8c02010-01-22 19:05:05 +0000605 // Don't count debug info directives, lest they affect codegen,
606 // and we skip pointer-to-pointer bitcasts, which are NOPs.
607 if (isa<DbgInfoIntrinsic>(BBI) ||
Duncan Sands19d0b472010-02-16 11:11:14 +0000608 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000609 ScanInsts++;
610 continue;
Chandler Carruth7ec50852012-11-01 08:07:29 +0000611 }
612
Chris Lattnera65e2f72010-01-05 05:57:49 +0000613 if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) {
614 // Prev store isn't volatile, and stores to the same location?
Eli Friedman8bc586e2011-08-15 22:09:40 +0000615 if (PrevSI->isSimple() && equivalentAddressValues(PrevSI->getOperand(1),
616 SI.getOperand(1))) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000617 ++NumDeadStore;
618 ++BBI;
619 EraseInstFromFunction(*PrevSI);
620 continue;
621 }
622 break;
623 }
Chandler Carruth7ec50852012-11-01 08:07:29 +0000624
Chris Lattnera65e2f72010-01-05 05:57:49 +0000625 // If this is a load, we have to stop. However, if the loaded value is from
626 // the pointer we're loading and is producing the pointer we're storing,
627 // then *this* store is dead (X = load P; store X -> P).
628 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
Jin-Gu Kangb452db02011-03-14 01:21:00 +0000629 if (LI == Val && equivalentAddressValues(LI->getOperand(0), Ptr) &&
Eli Friedman8bc586e2011-08-15 22:09:40 +0000630 LI->isSimple())
Jin-Gu Kangb452db02011-03-14 01:21:00 +0000631 return EraseInstFromFunction(SI);
Chandler Carruth7ec50852012-11-01 08:07:29 +0000632
Chris Lattnera65e2f72010-01-05 05:57:49 +0000633 // Otherwise, this is a load from some other location. Stores before it
634 // may not be dead.
635 break;
636 }
Chandler Carruth7ec50852012-11-01 08:07:29 +0000637
Chris Lattnera65e2f72010-01-05 05:57:49 +0000638 // Don't skip over loads or things that can modify memory.
639 if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory())
640 break;
641 }
Chris Lattnera65e2f72010-01-05 05:57:49 +0000642
643 // store X, null -> turns into 'unreachable' in SimplifyCFG
644 if (isa<ConstantPointerNull>(Ptr) && SI.getPointerAddressSpace() == 0) {
645 if (!isa<UndefValue>(Val)) {
646 SI.setOperand(0, UndefValue::get(Val->getType()));
647 if (Instruction *U = dyn_cast<Instruction>(Val))
648 Worklist.Add(U); // Dropped a use.
649 }
650 return 0; // Do not modify these!
651 }
652
653 // store undef, Ptr -> noop
654 if (isa<UndefValue>(Val))
655 return EraseInstFromFunction(SI);
656
657 // If the pointer destination is a cast, see if we can fold the cast into the
658 // source instead.
659 if (isa<CastInst>(Ptr))
660 if (Instruction *Res = InstCombineStoreToCast(*this, SI))
661 return Res;
662 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
663 if (CE->isCast())
664 if (Instruction *Res = InstCombineStoreToCast(*this, SI))
665 return Res;
666
Chandler Carruth7ec50852012-11-01 08:07:29 +0000667
Chris Lattnera65e2f72010-01-05 05:57:49 +0000668 // If this store is the last instruction in the basic block (possibly
Victor Hernandez5f5abd52010-01-21 23:07:15 +0000669 // excepting debug info instructions), and if the block ends with an
670 // unconditional branch, try to move it to the successor block.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000671 BBI = &SI;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000672 do {
673 ++BBI;
Victor Hernandez5f8c8c02010-01-22 19:05:05 +0000674 } while (isa<DbgInfoIntrinsic>(BBI) ||
Duncan Sands19d0b472010-02-16 11:11:14 +0000675 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy()));
Chris Lattnera65e2f72010-01-05 05:57:49 +0000676 if (BranchInst *BI = dyn_cast<BranchInst>(BBI))
677 if (BI->isUnconditional())
678 if (SimplifyStoreAtEndOfBlock(SI))
679 return 0; // xform done!
Chandler Carruth7ec50852012-11-01 08:07:29 +0000680
Chris Lattnera65e2f72010-01-05 05:57:49 +0000681 return 0;
682}
683
684/// SimplifyStoreAtEndOfBlock - Turn things like:
685/// if () { *P = v1; } else { *P = v2 }
686/// into a phi node with a store in the successor.
687///
688/// Simplify things like:
689/// *P = v1; if () { *P = v2; }
690/// into a phi node with a store in the successor.
691///
692bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) {
693 BasicBlock *StoreBB = SI.getParent();
Chandler Carruth7ec50852012-11-01 08:07:29 +0000694
Chris Lattnera65e2f72010-01-05 05:57:49 +0000695 // Check to see if the successor block has exactly two incoming edges. If
696 // so, see if the other predecessor contains a store to the same location.
697 // if so, insert a PHI node (if needed) and move the stores down.
698 BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0);
Chandler Carruth7ec50852012-11-01 08:07:29 +0000699
Chris Lattnera65e2f72010-01-05 05:57:49 +0000700 // Determine whether Dest has exactly two predecessors and, if so, compute
701 // the other predecessor.
702 pred_iterator PI = pred_begin(DestBB);
Gabor Greif1b787df2010-07-12 15:48:26 +0000703 BasicBlock *P = *PI;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000704 BasicBlock *OtherBB = 0;
Gabor Greif1b787df2010-07-12 15:48:26 +0000705
706 if (P != StoreBB)
707 OtherBB = P;
708
709 if (++PI == pred_end(DestBB))
Chris Lattnera65e2f72010-01-05 05:57:49 +0000710 return false;
Chandler Carruth7ec50852012-11-01 08:07:29 +0000711
Gabor Greif1b787df2010-07-12 15:48:26 +0000712 P = *PI;
713 if (P != StoreBB) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000714 if (OtherBB)
715 return false;
Gabor Greif1b787df2010-07-12 15:48:26 +0000716 OtherBB = P;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000717 }
718 if (++PI != pred_end(DestBB))
719 return false;
720
721 // Bail out if all the relevant blocks aren't distinct (this can happen,
722 // for example, if SI is in an infinite loop)
723 if (StoreBB == DestBB || OtherBB == DestBB)
724 return false;
725
726 // Verify that the other block ends in a branch and is not otherwise empty.
727 BasicBlock::iterator BBI = OtherBB->getTerminator();
728 BranchInst *OtherBr = dyn_cast<BranchInst>(BBI);
729 if (!OtherBr || BBI == OtherBB->begin())
730 return false;
Chandler Carruth7ec50852012-11-01 08:07:29 +0000731
Chris Lattnera65e2f72010-01-05 05:57:49 +0000732 // If the other block ends in an unconditional branch, check for the 'if then
733 // else' case. there is an instruction before the branch.
734 StoreInst *OtherStore = 0;
735 if (OtherBr->isUnconditional()) {
736 --BBI;
737 // Skip over debugging info.
Victor Hernandez5f8c8c02010-01-22 19:05:05 +0000738 while (isa<DbgInfoIntrinsic>(BBI) ||
Duncan Sands19d0b472010-02-16 11:11:14 +0000739 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000740 if (BBI==OtherBB->begin())
741 return false;
742 --BBI;
743 }
Eli Friedman8bc586e2011-08-15 22:09:40 +0000744 // If this isn't a store, isn't a store to the same location, or is not the
745 // right kind of store, bail out.
Chris Lattnera65e2f72010-01-05 05:57:49 +0000746 OtherStore = dyn_cast<StoreInst>(BBI);
747 if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1) ||
Eli Friedman8bc586e2011-08-15 22:09:40 +0000748 !SI.isSameOperationAs(OtherStore))
Chris Lattnera65e2f72010-01-05 05:57:49 +0000749 return false;
750 } else {
751 // Otherwise, the other block ended with a conditional branch. If one of the
752 // destinations is StoreBB, then we have the if/then case.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000753 if (OtherBr->getSuccessor(0) != StoreBB &&
Chris Lattnera65e2f72010-01-05 05:57:49 +0000754 OtherBr->getSuccessor(1) != StoreBB)
755 return false;
Chandler Carruth7ec50852012-11-01 08:07:29 +0000756
Chris Lattnera65e2f72010-01-05 05:57:49 +0000757 // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an
758 // if/then triangle. See if there is a store to the same ptr as SI that
759 // lives in OtherBB.
760 for (;; --BBI) {
761 // Check to see if we find the matching store.
762 if ((OtherStore = dyn_cast<StoreInst>(BBI))) {
763 if (OtherStore->getOperand(1) != SI.getOperand(1) ||
Eli Friedman8bc586e2011-08-15 22:09:40 +0000764 !SI.isSameOperationAs(OtherStore))
Chris Lattnera65e2f72010-01-05 05:57:49 +0000765 return false;
766 break;
767 }
768 // If we find something that may be using or overwriting the stored
769 // value, or if we run out of instructions, we can't do the xform.
770 if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() ||
771 BBI == OtherBB->begin())
772 return false;
773 }
Chandler Carruth7ec50852012-11-01 08:07:29 +0000774
Chris Lattnera65e2f72010-01-05 05:57:49 +0000775 // In order to eliminate the store in OtherBr, we have to
776 // make sure nothing reads or overwrites the stored value in
777 // StoreBB.
778 for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) {
779 // FIXME: This should really be AA driven.
780 if (I->mayReadFromMemory() || I->mayWriteToMemory())
781 return false;
782 }
783 }
Chandler Carruth7ec50852012-11-01 08:07:29 +0000784
Chris Lattnera65e2f72010-01-05 05:57:49 +0000785 // Insert a PHI node now if we need it.
786 Value *MergedVal = OtherStore->getOperand(0);
787 if (MergedVal != SI.getOperand(0)) {
Jay Foad52131342011-03-30 11:28:46 +0000788 PHINode *PN = PHINode::Create(MergedVal->getType(), 2, "storemerge");
Chris Lattnera65e2f72010-01-05 05:57:49 +0000789 PN->addIncoming(SI.getOperand(0), SI.getParent());
790 PN->addIncoming(OtherStore->getOperand(0), OtherBB);
791 MergedVal = InsertNewInstBefore(PN, DestBB->front());
792 }
Chandler Carruth7ec50852012-11-01 08:07:29 +0000793
Chris Lattnera65e2f72010-01-05 05:57:49 +0000794 // Advance to a place where it is safe to insert the new store and
795 // insert it.
Bill Wendling8ddfc092011-08-16 20:45:24 +0000796 BBI = DestBB->getFirstInsertionPt();
Eli Friedman35211c62011-05-27 00:19:40 +0000797 StoreInst *NewSI = new StoreInst(MergedVal, SI.getOperand(1),
Eli Friedman8bc586e2011-08-15 22:09:40 +0000798 SI.isVolatile(),
799 SI.getAlignment(),
800 SI.getOrdering(),
801 SI.getSynchScope());
Eli Friedman35211c62011-05-27 00:19:40 +0000802 InsertNewInstBefore(NewSI, *BBI);
Chandler Carruth7ec50852012-11-01 08:07:29 +0000803 NewSI->setDebugLoc(OtherStore->getDebugLoc());
Eli Friedman35211c62011-05-27 00:19:40 +0000804
Chris Lattnereeefe1b2012-12-31 08:10:58 +0000805 // If the two stores had the same TBAA tag, preserve it.
806 if (MDNode *TBAATag1 = SI.getMetadata(LLVMContext::MD_tbaa))
807 if (MDNode *TBAATag2 = OtherStore->getMetadata(LLVMContext::MD_tbaa))
808 if (TBAATag1 == TBAATag2)
809 NewSI->setMetadata(LLVMContext::MD_tbaa, TBAATag1);
810
811
Chris Lattnera65e2f72010-01-05 05:57:49 +0000812 // Nuke the old stores.
813 EraseInstFromFunction(SI);
814 EraseInstFromFunction(*OtherStore);
815 return true;
816}