blob: c485844aaeb457b194f02987fd21243e3be53911 [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"
15#include "llvm/IntrinsicInst.h"
Dan Gohman826bdf82010-05-28 16:19:17 +000016#include "llvm/Analysis/Loads.h"
Chris Lattnera65e2f72010-01-05 05:57:49 +000017#include "llvm/Target/TargetData.h"
18#include "llvm/Transforms/Utils/BasicBlockUtils.h"
19#include "llvm/Transforms/Utils/Local.h"
20#include "llvm/ADT/Statistic.h"
21using namespace llvm;
22
23STATISTIC(NumDeadStore, "Number of dead stores eliminated");
24
25Instruction *InstCombiner::visitAllocaInst(AllocaInst &AI) {
Dan Gohmandf5d7dc2010-05-28 15:09:00 +000026 // Ensure that the alloca array size argument has type intptr_t, so that
27 // any casting is exposed early.
28 if (TD) {
Chris Lattner229907c2011-07-18 04:54:35 +000029 Type *IntPtrTy = TD->getIntPtrType(AI.getContext());
Dan Gohmandf5d7dc2010-05-28 15:09:00 +000030 if (AI.getArraySize()->getType() != IntPtrTy) {
31 Value *V = Builder->CreateIntCast(AI.getArraySize(),
32 IntPtrTy, false);
33 AI.setOperand(0, V);
34 return &AI;
35 }
36 }
37
Chris Lattnera65e2f72010-01-05 05:57:49 +000038 // Convert: alloca Ty, C - where C is a constant != 1 into: alloca [C x Ty], 1
39 if (AI.isArrayAllocation()) { // Check C != 1
40 if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) {
Chris Lattner229907c2011-07-18 04:54:35 +000041 Type *NewTy =
Chris Lattnera65e2f72010-01-05 05:57:49 +000042 ArrayType::get(AI.getAllocatedType(), C->getZExtValue());
Chris Lattnera65e2f72010-01-05 05:57:49 +000043 AllocaInst *New = Builder->CreateAlloca(NewTy, 0, AI.getName());
44 New->setAlignment(AI.getAlignment());
45
46 // Scan to the end of the allocation instructions, to skip over a block of
47 // allocas if possible...also skip interleaved debug info
48 //
49 BasicBlock::iterator It = New;
50 while (isa<AllocaInst>(*It) || isa<DbgInfoIntrinsic>(*It)) ++It;
51
52 // Now that I is pointing to the first non-allocation-inst in the block,
53 // insert our getelementptr instruction...
54 //
55 Value *NullIdx =Constant::getNullValue(Type::getInt32Ty(AI.getContext()));
56 Value *Idx[2];
57 Idx[0] = NullIdx;
58 Idx[1] = NullIdx;
Eli Friedman41e509a2011-05-18 23:58:37 +000059 Instruction *GEP =
Jay Foadd1b78492011-07-25 09:48:08 +000060 GetElementPtrInst::CreateInBounds(New, Idx, New->getName()+".sub");
Eli Friedman41e509a2011-05-18 23:58:37 +000061 InsertNewInstBefore(GEP, *It);
Chris Lattnera65e2f72010-01-05 05:57:49 +000062
63 // Now make everything use the getelementptr instead of the original
64 // allocation.
Eli Friedman41e509a2011-05-18 23:58:37 +000065 return ReplaceInstUsesWith(AI, GEP);
Chris Lattnera65e2f72010-01-05 05:57:49 +000066 } else if (isa<UndefValue>(AI.getArraySize())) {
67 return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
68 }
69 }
70
Duncan Sands8bc764a2012-06-26 13:39:21 +000071 if (TD && AI.getAllocatedType()->isSized()) {
Chris Lattnera65e2f72010-01-05 05:57:49 +000072 // If the alignment is 0 (unspecified), assign it the preferred alignment.
73 if (AI.getAlignment() == 0)
74 AI.setAlignment(TD->getPrefTypeAlignment(AI.getAllocatedType()));
Duncan Sands8bc764a2012-06-26 13:39:21 +000075
76 // Move all alloca's of zero byte objects to the entry block and merge them
77 // together. Note that we only do this for alloca's, because malloc should
78 // allocate and return a unique pointer, even for a zero byte allocation.
79 if (TD->getTypeAllocSize(AI.getAllocatedType()) == 0) {
80 // For a zero sized alloca there is no point in doing an array allocation.
81 // This is helpful if the array size is a complicated expression not used
82 // elsewhere.
83 if (AI.isArrayAllocation()) {
84 AI.setOperand(0, ConstantInt::get(AI.getArraySize()->getType(), 1));
85 return &AI;
86 }
87
88 // Get the first instruction in the entry block.
89 BasicBlock &EntryBlock = AI.getParent()->getParent()->getEntryBlock();
90 Instruction *FirstInst = EntryBlock.getFirstNonPHIOrDbg();
91 if (FirstInst != &AI) {
92 // If the entry block doesn't start with a zero-size alloca then move
93 // this one to the start of the entry block. There is no problem with
94 // dominance as the array size was forced to a constant earlier already.
95 AllocaInst *EntryAI = dyn_cast<AllocaInst>(FirstInst);
96 if (!EntryAI || !EntryAI->getAllocatedType()->isSized() ||
97 TD->getTypeAllocSize(EntryAI->getAllocatedType()) != 0) {
98 AI.moveBefore(FirstInst);
99 return &AI;
100 }
101
102 // Replace this zero-sized alloca with the one at the start of the entry
103 // block after ensuring that the address will be aligned enough for both
104 // types.
105 unsigned MaxAlign =
106 std::max(TD->getPrefTypeAlignment(EntryAI->getAllocatedType()),
107 TD->getPrefTypeAlignment(AI.getAllocatedType()));
108 EntryAI->setAlignment(MaxAlign);
109 if (AI.getType() != EntryAI->getType())
110 return new BitCastInst(EntryAI, AI.getType());
111 return ReplaceInstUsesWith(AI, EntryAI);
112 }
113 }
Chris Lattnera65e2f72010-01-05 05:57:49 +0000114 }
115
Nuno Lopes95cc4f32012-07-09 18:38:20 +0000116 // At last, use the generic allocation site handler to aggressively remove
117 // unused allocas.
118 return visitAllocSite(AI);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000119}
120
121
122/// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible.
123static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI,
124 const TargetData *TD) {
125 User *CI = cast<User>(LI.getOperand(0));
126 Value *CastOp = CI->getOperand(0);
127
Chris Lattner229907c2011-07-18 04:54:35 +0000128 PointerType *DestTy = cast<PointerType>(CI->getType());
129 Type *DestPTy = DestTy->getElementType();
130 if (PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000131
132 // If the address spaces don't match, don't eliminate the cast.
133 if (DestTy->getAddressSpace() != SrcTy->getAddressSpace())
134 return 0;
135
Chris Lattner229907c2011-07-18 04:54:35 +0000136 Type *SrcPTy = SrcTy->getElementType();
Chris Lattnera65e2f72010-01-05 05:57:49 +0000137
Duncan Sands19d0b472010-02-16 11:11:14 +0000138 if (DestPTy->isIntegerTy() || DestPTy->isPointerTy() ||
139 DestPTy->isVectorTy()) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000140 // If the source is an array, the code below will not succeed. Check to
141 // see if a trivial 'gep P, 0, 0' will help matters. Only do this for
142 // constants.
Chris Lattner229907c2011-07-18 04:54:35 +0000143 if (ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy))
Chris Lattnera65e2f72010-01-05 05:57:49 +0000144 if (Constant *CSrc = dyn_cast<Constant>(CastOp))
145 if (ASrcTy->getNumElements() != 0) {
146 Value *Idxs[2];
147 Idxs[0] = Constant::getNullValue(Type::getInt32Ty(LI.getContext()));
148 Idxs[1] = Idxs[0];
Jay Foad71f19ac2011-07-22 07:54:01 +0000149 CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000150 SrcTy = cast<PointerType>(CastOp->getType());
151 SrcPTy = SrcTy->getElementType();
152 }
153
154 if (IC.getTargetData() &&
Duncan Sands19d0b472010-02-16 11:11:14 +0000155 (SrcPTy->isIntegerTy() || SrcPTy->isPointerTy() ||
156 SrcPTy->isVectorTy()) &&
Chris Lattnera65e2f72010-01-05 05:57:49 +0000157 // Do not allow turning this into a load of an integer, which is then
158 // casted to a pointer, this pessimizes pointer analysis a lot.
Duncan Sands19d0b472010-02-16 11:11:14 +0000159 (SrcPTy->isPointerTy() == LI.getType()->isPointerTy()) &&
Chris Lattnera65e2f72010-01-05 05:57:49 +0000160 IC.getTargetData()->getTypeSizeInBits(SrcPTy) ==
161 IC.getTargetData()->getTypeSizeInBits(DestPTy)) {
162
163 // Okay, we are casting from one integer or pointer type to another of
164 // the same size. Instead of casting the pointer before the load, cast
165 // the result of the loaded value.
Bob Wilson4b71b6c2010-01-30 00:41:10 +0000166 LoadInst *NewLoad =
Chris Lattnera65e2f72010-01-05 05:57:49 +0000167 IC.Builder->CreateLoad(CastOp, LI.isVolatile(), CI->getName());
Bob Wilson4b71b6c2010-01-30 00:41:10 +0000168 NewLoad->setAlignment(LI.getAlignment());
Eli Friedman8bc586e2011-08-15 22:09:40 +0000169 NewLoad->setAtomic(LI.getOrdering(), LI.getSynchScope());
Chris Lattnera65e2f72010-01-05 05:57:49 +0000170 // Now cast the result of the load.
171 return new BitCastInst(NewLoad, LI.getType());
172 }
173 }
174 }
175 return 0;
176}
177
178Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
179 Value *Op = LI.getOperand(0);
180
181 // Attempt to improve the alignment.
182 if (TD) {
183 unsigned KnownAlign =
Chris Lattner6fcd32e2010-12-25 20:37:57 +0000184 getOrEnforceKnownAlignment(Op, TD->getPrefTypeAlignment(LI.getType()),TD);
Dan Gohman36196602010-08-03 18:20:32 +0000185 unsigned LoadAlign = LI.getAlignment();
186 unsigned EffectiveLoadAlign = LoadAlign != 0 ? LoadAlign :
187 TD->getABITypeAlignment(LI.getType());
188
189 if (KnownAlign > EffectiveLoadAlign)
Chris Lattnera65e2f72010-01-05 05:57:49 +0000190 LI.setAlignment(KnownAlign);
Dan Gohman36196602010-08-03 18:20:32 +0000191 else if (LoadAlign == 0)
192 LI.setAlignment(EffectiveLoadAlign);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000193 }
194
195 // load (cast X) --> cast (load X) iff safe.
196 if (isa<CastInst>(Op))
197 if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
198 return Res;
199
Eli Friedman8bc586e2011-08-15 22:09:40 +0000200 // None of the following transforms are legal for volatile/atomic loads.
201 // FIXME: Some of it is okay for atomic loads; needs refactoring.
202 if (!LI.isSimple()) return 0;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000203
204 // Do really simple store-to-load forwarding and load CSE, to catch cases
Duncan Sands75b5d272011-02-15 09:23:02 +0000205 // where there are several consecutive memory accesses to the same location,
Chris Lattnera65e2f72010-01-05 05:57:49 +0000206 // separated by a few arithmetic operations.
207 BasicBlock::iterator BBI = &LI;
208 if (Value *AvailableVal = FindAvailableLoadedValue(Op, LI.getParent(), BBI,6))
209 return ReplaceInstUsesWith(LI, AvailableVal);
210
211 // load(gep null, ...) -> unreachable
212 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
213 const Value *GEPI0 = GEPI->getOperand(0);
214 // TODO: Consider a target hook for valid address spaces for this xform.
215 if (isa<ConstantPointerNull>(GEPI0) && GEPI->getPointerAddressSpace() == 0){
216 // Insert a new store to null instruction before the load to indicate
217 // that this code is not reachable. We do this instead of inserting
218 // an unreachable instruction directly because we cannot modify the
219 // CFG.
220 new StoreInst(UndefValue::get(LI.getType()),
221 Constant::getNullValue(Op->getType()), &LI);
222 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
223 }
224 }
225
226 // load null/undef -> unreachable
227 // TODO: Consider a target hook for valid address spaces for this xform.
228 if (isa<UndefValue>(Op) ||
229 (isa<ConstantPointerNull>(Op) && LI.getPointerAddressSpace() == 0)) {
230 // Insert a new store to null instruction before the load to indicate that
231 // this code is not reachable. We do this instead of inserting an
232 // unreachable instruction directly because we cannot modify the CFG.
233 new StoreInst(UndefValue::get(LI.getType()),
234 Constant::getNullValue(Op->getType()), &LI);
235 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
236 }
237
238 // Instcombine load (constantexpr_cast global) -> cast (load global)
239 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op))
240 if (CE->isCast())
241 if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
242 return Res;
243
244 if (Op->hasOneUse()) {
245 // Change select and PHI nodes to select values instead of addresses: this
246 // helps alias analysis out a lot, allows many others simplifications, and
247 // exposes redundancy in the code.
248 //
249 // Note that we cannot do the transformation unless we know that the
250 // introduced loads cannot trap! Something like this is valid as long as
251 // the condition is always false: load (select bool %C, int* null, int* %G),
252 // but it would not be valid if we transformed it to load from null
253 // unconditionally.
254 //
255 if (SelectInst *SI = dyn_cast<SelectInst>(Op)) {
256 // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2).
Bob Wilson56600a12010-01-30 04:42:39 +0000257 unsigned Align = LI.getAlignment();
258 if (isSafeToLoadUnconditionally(SI->getOperand(1), SI, Align, TD) &&
259 isSafeToLoadUnconditionally(SI->getOperand(2), SI, Align, TD)) {
Bob Wilson4b71b6c2010-01-30 00:41:10 +0000260 LoadInst *V1 = Builder->CreateLoad(SI->getOperand(1),
Bob Wilson56600a12010-01-30 04:42:39 +0000261 SI->getOperand(1)->getName()+".val");
Bob Wilson4b71b6c2010-01-30 00:41:10 +0000262 LoadInst *V2 = Builder->CreateLoad(SI->getOperand(2),
Bob Wilson56600a12010-01-30 04:42:39 +0000263 SI->getOperand(2)->getName()+".val");
264 V1->setAlignment(Align);
265 V2->setAlignment(Align);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000266 return SelectInst::Create(SI->getCondition(), V1, V2);
267 }
268
269 // load (select (cond, null, P)) -> load P
270 if (Constant *C = dyn_cast<Constant>(SI->getOperand(1)))
271 if (C->isNullValue()) {
272 LI.setOperand(0, SI->getOperand(2));
273 return &LI;
274 }
275
276 // load (select (cond, P, null)) -> load P
277 if (Constant *C = dyn_cast<Constant>(SI->getOperand(2)))
278 if (C->isNullValue()) {
279 LI.setOperand(0, SI->getOperand(1));
280 return &LI;
281 }
282 }
283 }
284 return 0;
285}
286
287/// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P
288/// when possible. This makes it generally easy to do alias analysis and/or
289/// SROA/mem2reg of the memory object.
290static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) {
291 User *CI = cast<User>(SI.getOperand(1));
292 Value *CastOp = CI->getOperand(0);
293
Chris Lattner229907c2011-07-18 04:54:35 +0000294 Type *DestPTy = cast<PointerType>(CI->getType())->getElementType();
295 PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType());
Chris Lattnera65e2f72010-01-05 05:57:49 +0000296 if (SrcTy == 0) return 0;
297
Chris Lattner229907c2011-07-18 04:54:35 +0000298 Type *SrcPTy = SrcTy->getElementType();
Chris Lattnera65e2f72010-01-05 05:57:49 +0000299
Duncan Sands19d0b472010-02-16 11:11:14 +0000300 if (!DestPTy->isIntegerTy() && !DestPTy->isPointerTy())
Chris Lattnera65e2f72010-01-05 05:57:49 +0000301 return 0;
302
303 /// NewGEPIndices - If SrcPTy is an aggregate type, we can emit a "noop gep"
304 /// to its first element. This allows us to handle things like:
305 /// store i32 xxx, (bitcast {foo*, float}* %P to i32*)
306 /// on 32-bit hosts.
307 SmallVector<Value*, 4> NewGEPIndices;
308
309 // If the source is an array, the code below will not succeed. Check to
310 // see if a trivial 'gep P, 0, 0' will help matters. Only do this for
311 // constants.
Duncan Sands19d0b472010-02-16 11:11:14 +0000312 if (SrcPTy->isArrayTy() || SrcPTy->isStructTy()) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000313 // Index through pointer.
314 Constant *Zero = Constant::getNullValue(Type::getInt32Ty(SI.getContext()));
315 NewGEPIndices.push_back(Zero);
316
317 while (1) {
Chris Lattner229907c2011-07-18 04:54:35 +0000318 if (StructType *STy = dyn_cast<StructType>(SrcPTy)) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000319 if (!STy->getNumElements()) /* Struct can be empty {} */
320 break;
321 NewGEPIndices.push_back(Zero);
322 SrcPTy = STy->getElementType(0);
Chris Lattner229907c2011-07-18 04:54:35 +0000323 } else if (ArrayType *ATy = dyn_cast<ArrayType>(SrcPTy)) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000324 NewGEPIndices.push_back(Zero);
325 SrcPTy = ATy->getElementType();
326 } else {
327 break;
328 }
329 }
330
331 SrcTy = PointerType::get(SrcPTy, SrcTy->getAddressSpace());
332 }
333
Duncan Sands19d0b472010-02-16 11:11:14 +0000334 if (!SrcPTy->isIntegerTy() && !SrcPTy->isPointerTy())
Chris Lattnera65e2f72010-01-05 05:57:49 +0000335 return 0;
336
337 // If the pointers point into different address spaces or if they point to
338 // values with different sizes, we can't do the transformation.
339 if (!IC.getTargetData() ||
340 SrcTy->getAddressSpace() !=
341 cast<PointerType>(CI->getType())->getAddressSpace() ||
342 IC.getTargetData()->getTypeSizeInBits(SrcPTy) !=
343 IC.getTargetData()->getTypeSizeInBits(DestPTy))
344 return 0;
345
346 // Okay, we are casting from one integer or pointer type to another of
347 // the same size. Instead of casting the pointer before
348 // the store, cast the value to be stored.
349 Value *NewCast;
350 Value *SIOp0 = SI.getOperand(0);
351 Instruction::CastOps opcode = Instruction::BitCast;
Chris Lattner229907c2011-07-18 04:54:35 +0000352 Type* CastSrcTy = SIOp0->getType();
353 Type* CastDstTy = SrcPTy;
Duncan Sands19d0b472010-02-16 11:11:14 +0000354 if (CastDstTy->isPointerTy()) {
Duncan Sands9dff9be2010-02-15 16:12:20 +0000355 if (CastSrcTy->isIntegerTy())
Chris Lattnera65e2f72010-01-05 05:57:49 +0000356 opcode = Instruction::IntToPtr;
Duncan Sands19d0b472010-02-16 11:11:14 +0000357 } else if (CastDstTy->isIntegerTy()) {
358 if (SIOp0->getType()->isPointerTy())
Chris Lattnera65e2f72010-01-05 05:57:49 +0000359 opcode = Instruction::PtrToInt;
360 }
361
362 // SIOp0 is a pointer to aggregate and this is a store to the first field,
363 // emit a GEP to index into its first field.
364 if (!NewGEPIndices.empty())
Jay Foad040dd822011-07-22 08:16:57 +0000365 CastOp = IC.Builder->CreateInBoundsGEP(CastOp, NewGEPIndices);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000366
367 NewCast = IC.Builder->CreateCast(opcode, SIOp0, CastDstTy,
368 SIOp0->getName()+".c");
Dan Gohman2e20dfb2010-10-25 16:16:27 +0000369 SI.setOperand(0, NewCast);
370 SI.setOperand(1, CastOp);
371 return &SI;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000372}
373
374/// equivalentAddressValues - Test if A and B will obviously have the same
375/// value. This includes recognizing that %t0 and %t1 will have the same
376/// value in code like this:
377/// %t0 = getelementptr \@a, 0, 3
378/// store i32 0, i32* %t0
379/// %t1 = getelementptr \@a, 0, 3
380/// %t2 = load i32* %t1
381///
382static bool equivalentAddressValues(Value *A, Value *B) {
383 // Test if the values are trivially equivalent.
384 if (A == B) return true;
385
386 // Test if the values come form identical arithmetic instructions.
387 // This uses isIdenticalToWhenDefined instead of isIdenticalTo because
388 // its only used to compare two uses within the same basic block, which
389 // means that they'll always either have the same value or one of them
390 // will have an undefined value.
391 if (isa<BinaryOperator>(A) ||
392 isa<CastInst>(A) ||
393 isa<PHINode>(A) ||
394 isa<GetElementPtrInst>(A))
395 if (Instruction *BI = dyn_cast<Instruction>(B))
396 if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
397 return true;
398
399 // Otherwise they may not be equivalent.
400 return false;
401}
402
Chris Lattnera65e2f72010-01-05 05:57:49 +0000403Instruction *InstCombiner::visitStoreInst(StoreInst &SI) {
404 Value *Val = SI.getOperand(0);
405 Value *Ptr = SI.getOperand(1);
406
Chris Lattnera65e2f72010-01-05 05:57:49 +0000407 // Attempt to improve the alignment.
408 if (TD) {
409 unsigned KnownAlign =
Chris Lattner6fcd32e2010-12-25 20:37:57 +0000410 getOrEnforceKnownAlignment(Ptr, TD->getPrefTypeAlignment(Val->getType()),
411 TD);
Dan Gohman36196602010-08-03 18:20:32 +0000412 unsigned StoreAlign = SI.getAlignment();
413 unsigned EffectiveStoreAlign = StoreAlign != 0 ? StoreAlign :
414 TD->getABITypeAlignment(Val->getType());
415
Bill Wendling55b6b2b2012-03-16 18:20:54 +0000416 if (KnownAlign > EffectiveStoreAlign)
Chris Lattnera65e2f72010-01-05 05:57:49 +0000417 SI.setAlignment(KnownAlign);
Bill Wendling55b6b2b2012-03-16 18:20:54 +0000418 else if (StoreAlign == 0)
419 SI.setAlignment(EffectiveStoreAlign);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000420 }
421
Eli Friedman8bc586e2011-08-15 22:09:40 +0000422 // Don't hack volatile/atomic stores.
423 // FIXME: Some bits are legal for atomic stores; needs refactoring.
424 if (!SI.isSimple()) return 0;
425
426 // If the RHS is an alloca with a single use, zapify the store, making the
427 // alloca dead.
428 if (Ptr->hasOneUse()) {
429 if (isa<AllocaInst>(Ptr))
430 return EraseInstFromFunction(SI);
431 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
432 if (isa<AllocaInst>(GEP->getOperand(0))) {
433 if (GEP->getOperand(0)->hasOneUse())
434 return EraseInstFromFunction(SI);
435 }
436 }
437 }
438
Chris Lattnera65e2f72010-01-05 05:57:49 +0000439 // Do really simple DSE, to catch cases where there are several consecutive
440 // stores to the same location, separated by a few arithmetic operations. This
441 // situation often occurs with bitfield accesses.
442 BasicBlock::iterator BBI = &SI;
443 for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts;
444 --ScanInsts) {
445 --BBI;
Victor Hernandez5f8c8c02010-01-22 19:05:05 +0000446 // Don't count debug info directives, lest they affect codegen,
447 // and we skip pointer-to-pointer bitcasts, which are NOPs.
448 if (isa<DbgInfoIntrinsic>(BBI) ||
Duncan Sands19d0b472010-02-16 11:11:14 +0000449 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000450 ScanInsts++;
451 continue;
452 }
453
454 if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) {
455 // Prev store isn't volatile, and stores to the same location?
Eli Friedman8bc586e2011-08-15 22:09:40 +0000456 if (PrevSI->isSimple() && equivalentAddressValues(PrevSI->getOperand(1),
457 SI.getOperand(1))) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000458 ++NumDeadStore;
459 ++BBI;
460 EraseInstFromFunction(*PrevSI);
461 continue;
462 }
463 break;
464 }
465
466 // If this is a load, we have to stop. However, if the loaded value is from
467 // the pointer we're loading and is producing the pointer we're storing,
468 // then *this* store is dead (X = load P; store X -> P).
469 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
Jin-Gu Kangb452db02011-03-14 01:21:00 +0000470 if (LI == Val && equivalentAddressValues(LI->getOperand(0), Ptr) &&
Eli Friedman8bc586e2011-08-15 22:09:40 +0000471 LI->isSimple())
Jin-Gu Kangb452db02011-03-14 01:21:00 +0000472 return EraseInstFromFunction(SI);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000473
474 // Otherwise, this is a load from some other location. Stores before it
475 // may not be dead.
476 break;
477 }
478
479 // Don't skip over loads or things that can modify memory.
480 if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory())
481 break;
482 }
Chris Lattnera65e2f72010-01-05 05:57:49 +0000483
484 // store X, null -> turns into 'unreachable' in SimplifyCFG
485 if (isa<ConstantPointerNull>(Ptr) && SI.getPointerAddressSpace() == 0) {
486 if (!isa<UndefValue>(Val)) {
487 SI.setOperand(0, UndefValue::get(Val->getType()));
488 if (Instruction *U = dyn_cast<Instruction>(Val))
489 Worklist.Add(U); // Dropped a use.
490 }
491 return 0; // Do not modify these!
492 }
493
494 // store undef, Ptr -> noop
495 if (isa<UndefValue>(Val))
496 return EraseInstFromFunction(SI);
497
498 // If the pointer destination is a cast, see if we can fold the cast into the
499 // source instead.
500 if (isa<CastInst>(Ptr))
501 if (Instruction *Res = InstCombineStoreToCast(*this, SI))
502 return Res;
503 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
504 if (CE->isCast())
505 if (Instruction *Res = InstCombineStoreToCast(*this, SI))
506 return Res;
507
508
509 // If this store is the last instruction in the basic block (possibly
Victor Hernandez5f5abd52010-01-21 23:07:15 +0000510 // excepting debug info instructions), and if the block ends with an
511 // unconditional branch, try to move it to the successor block.
Chris Lattnera65e2f72010-01-05 05:57:49 +0000512 BBI = &SI;
513 do {
514 ++BBI;
Victor Hernandez5f8c8c02010-01-22 19:05:05 +0000515 } while (isa<DbgInfoIntrinsic>(BBI) ||
Duncan Sands19d0b472010-02-16 11:11:14 +0000516 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy()));
Chris Lattnera65e2f72010-01-05 05:57:49 +0000517 if (BranchInst *BI = dyn_cast<BranchInst>(BBI))
518 if (BI->isUnconditional())
519 if (SimplifyStoreAtEndOfBlock(SI))
520 return 0; // xform done!
521
522 return 0;
523}
524
525/// SimplifyStoreAtEndOfBlock - Turn things like:
526/// if () { *P = v1; } else { *P = v2 }
527/// into a phi node with a store in the successor.
528///
529/// Simplify things like:
530/// *P = v1; if () { *P = v2; }
531/// into a phi node with a store in the successor.
532///
533bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) {
534 BasicBlock *StoreBB = SI.getParent();
535
536 // Check to see if the successor block has exactly two incoming edges. If
537 // so, see if the other predecessor contains a store to the same location.
538 // if so, insert a PHI node (if needed) and move the stores down.
539 BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0);
540
541 // Determine whether Dest has exactly two predecessors and, if so, compute
542 // the other predecessor.
543 pred_iterator PI = pred_begin(DestBB);
Gabor Greif1b787df2010-07-12 15:48:26 +0000544 BasicBlock *P = *PI;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000545 BasicBlock *OtherBB = 0;
Gabor Greif1b787df2010-07-12 15:48:26 +0000546
547 if (P != StoreBB)
548 OtherBB = P;
549
550 if (++PI == pred_end(DestBB))
Chris Lattnera65e2f72010-01-05 05:57:49 +0000551 return false;
552
Gabor Greif1b787df2010-07-12 15:48:26 +0000553 P = *PI;
554 if (P != StoreBB) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000555 if (OtherBB)
556 return false;
Gabor Greif1b787df2010-07-12 15:48:26 +0000557 OtherBB = P;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000558 }
559 if (++PI != pred_end(DestBB))
560 return false;
561
562 // Bail out if all the relevant blocks aren't distinct (this can happen,
563 // for example, if SI is in an infinite loop)
564 if (StoreBB == DestBB || OtherBB == DestBB)
565 return false;
566
567 // Verify that the other block ends in a branch and is not otherwise empty.
568 BasicBlock::iterator BBI = OtherBB->getTerminator();
569 BranchInst *OtherBr = dyn_cast<BranchInst>(BBI);
570 if (!OtherBr || BBI == OtherBB->begin())
571 return false;
572
573 // If the other block ends in an unconditional branch, check for the 'if then
574 // else' case. there is an instruction before the branch.
575 StoreInst *OtherStore = 0;
576 if (OtherBr->isUnconditional()) {
577 --BBI;
578 // Skip over debugging info.
Victor Hernandez5f8c8c02010-01-22 19:05:05 +0000579 while (isa<DbgInfoIntrinsic>(BBI) ||
Duncan Sands19d0b472010-02-16 11:11:14 +0000580 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000581 if (BBI==OtherBB->begin())
582 return false;
583 --BBI;
584 }
Eli Friedman8bc586e2011-08-15 22:09:40 +0000585 // If this isn't a store, isn't a store to the same location, or is not the
586 // right kind of store, bail out.
Chris Lattnera65e2f72010-01-05 05:57:49 +0000587 OtherStore = dyn_cast<StoreInst>(BBI);
588 if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1) ||
Eli Friedman8bc586e2011-08-15 22:09:40 +0000589 !SI.isSameOperationAs(OtherStore))
Chris Lattnera65e2f72010-01-05 05:57:49 +0000590 return false;
591 } else {
592 // Otherwise, the other block ended with a conditional branch. If one of the
593 // destinations is StoreBB, then we have the if/then case.
594 if (OtherBr->getSuccessor(0) != StoreBB &&
595 OtherBr->getSuccessor(1) != StoreBB)
596 return false;
597
598 // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an
599 // if/then triangle. See if there is a store to the same ptr as SI that
600 // lives in OtherBB.
601 for (;; --BBI) {
602 // Check to see if we find the matching store.
603 if ((OtherStore = dyn_cast<StoreInst>(BBI))) {
604 if (OtherStore->getOperand(1) != SI.getOperand(1) ||
Eli Friedman8bc586e2011-08-15 22:09:40 +0000605 !SI.isSameOperationAs(OtherStore))
Chris Lattnera65e2f72010-01-05 05:57:49 +0000606 return false;
607 break;
608 }
609 // If we find something that may be using or overwriting the stored
610 // value, or if we run out of instructions, we can't do the xform.
611 if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() ||
612 BBI == OtherBB->begin())
613 return false;
614 }
615
616 // In order to eliminate the store in OtherBr, we have to
617 // make sure nothing reads or overwrites the stored value in
618 // StoreBB.
619 for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) {
620 // FIXME: This should really be AA driven.
621 if (I->mayReadFromMemory() || I->mayWriteToMemory())
622 return false;
623 }
624 }
625
626 // Insert a PHI node now if we need it.
627 Value *MergedVal = OtherStore->getOperand(0);
628 if (MergedVal != SI.getOperand(0)) {
Jay Foad52131342011-03-30 11:28:46 +0000629 PHINode *PN = PHINode::Create(MergedVal->getType(), 2, "storemerge");
Chris Lattnera65e2f72010-01-05 05:57:49 +0000630 PN->addIncoming(SI.getOperand(0), SI.getParent());
631 PN->addIncoming(OtherStore->getOperand(0), OtherBB);
632 MergedVal = InsertNewInstBefore(PN, DestBB->front());
633 }
634
635 // Advance to a place where it is safe to insert the new store and
636 // insert it.
Bill Wendling8ddfc092011-08-16 20:45:24 +0000637 BBI = DestBB->getFirstInsertionPt();
Eli Friedman35211c62011-05-27 00:19:40 +0000638 StoreInst *NewSI = new StoreInst(MergedVal, SI.getOperand(1),
Eli Friedman8bc586e2011-08-15 22:09:40 +0000639 SI.isVolatile(),
640 SI.getAlignment(),
641 SI.getOrdering(),
642 SI.getSynchScope());
Eli Friedman35211c62011-05-27 00:19:40 +0000643 InsertNewInstBefore(NewSI, *BBI);
644 NewSI->setDebugLoc(OtherStore->getDebugLoc());
645
Chris Lattnera65e2f72010-01-05 05:57:49 +0000646 // Nuke the old stores.
647 EraseInstFromFunction(SI);
648 EraseInstFromFunction(*OtherStore);
649 return true;
650}