blob: 992a5d9f6b4fe2ebd98943e9b2279105bebb4df4 [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) {
29 const Type *IntPtrTy = TD->getIntPtrType(AI.getContext());
30 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())) {
41 const Type *NewTy =
42 ArrayType::get(AI.getAllocatedType(), C->getZExtValue());
43 assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!");
44 AllocaInst *New = Builder->CreateAlloca(NewTy, 0, AI.getName());
45 New->setAlignment(AI.getAlignment());
46
47 // Scan to the end of the allocation instructions, to skip over a block of
48 // allocas if possible...also skip interleaved debug info
49 //
50 BasicBlock::iterator It = New;
51 while (isa<AllocaInst>(*It) || isa<DbgInfoIntrinsic>(*It)) ++It;
52
53 // Now that I is pointing to the first non-allocation-inst in the block,
54 // insert our getelementptr instruction...
55 //
56 Value *NullIdx =Constant::getNullValue(Type::getInt32Ty(AI.getContext()));
57 Value *Idx[2];
58 Idx[0] = NullIdx;
59 Idx[1] = NullIdx;
60 Value *V = GetElementPtrInst::CreateInBounds(New, Idx, Idx + 2,
61 New->getName()+".sub", It);
62
63 // Now make everything use the getelementptr instead of the original
64 // allocation.
65 return ReplaceInstUsesWith(AI, V);
66 } else if (isa<UndefValue>(AI.getArraySize())) {
67 return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
68 }
69 }
70
71 if (TD && isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized()) {
72 // If alloca'ing a zero byte object, replace the alloca with a null pointer.
73 // Note that we only do this for alloca's, because malloc should allocate
74 // and return a unique pointer, even for a zero byte allocation.
75 if (TD->getTypeAllocSize(AI.getAllocatedType()) == 0)
76 return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
77
78 // If the alignment is 0 (unspecified), assign it the preferred alignment.
79 if (AI.getAlignment() == 0)
80 AI.setAlignment(TD->getPrefTypeAlignment(AI.getAllocatedType()));
81 }
82
83 return 0;
84}
85
86
87/// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible.
88static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI,
89 const TargetData *TD) {
90 User *CI = cast<User>(LI.getOperand(0));
91 Value *CastOp = CI->getOperand(0);
92
93 const PointerType *DestTy = cast<PointerType>(CI->getType());
94 const Type *DestPTy = DestTy->getElementType();
95 if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) {
96
97 // If the address spaces don't match, don't eliminate the cast.
98 if (DestTy->getAddressSpace() != SrcTy->getAddressSpace())
99 return 0;
100
101 const Type *SrcPTy = SrcTy->getElementType();
102
Duncan Sands19d0b472010-02-16 11:11:14 +0000103 if (DestPTy->isIntegerTy() || DestPTy->isPointerTy() ||
104 DestPTy->isVectorTy()) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000105 // If the source is an array, the code below will not succeed. Check to
106 // see if a trivial 'gep P, 0, 0' will help matters. Only do this for
107 // constants.
108 if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy))
109 if (Constant *CSrc = dyn_cast<Constant>(CastOp))
110 if (ASrcTy->getNumElements() != 0) {
111 Value *Idxs[2];
112 Idxs[0] = Constant::getNullValue(Type::getInt32Ty(LI.getContext()));
113 Idxs[1] = Idxs[0];
114 CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2);
115 SrcTy = cast<PointerType>(CastOp->getType());
116 SrcPTy = SrcTy->getElementType();
117 }
118
119 if (IC.getTargetData() &&
Duncan Sands19d0b472010-02-16 11:11:14 +0000120 (SrcPTy->isIntegerTy() || SrcPTy->isPointerTy() ||
121 SrcPTy->isVectorTy()) &&
Chris Lattnera65e2f72010-01-05 05:57:49 +0000122 // Do not allow turning this into a load of an integer, which is then
123 // casted to a pointer, this pessimizes pointer analysis a lot.
Duncan Sands19d0b472010-02-16 11:11:14 +0000124 (SrcPTy->isPointerTy() == LI.getType()->isPointerTy()) &&
Chris Lattnera65e2f72010-01-05 05:57:49 +0000125 IC.getTargetData()->getTypeSizeInBits(SrcPTy) ==
126 IC.getTargetData()->getTypeSizeInBits(DestPTy)) {
127
128 // Okay, we are casting from one integer or pointer type to another of
129 // the same size. Instead of casting the pointer before the load, cast
130 // the result of the loaded value.
Bob Wilson4b71b6c2010-01-30 00:41:10 +0000131 LoadInst *NewLoad =
Chris Lattnera65e2f72010-01-05 05:57:49 +0000132 IC.Builder->CreateLoad(CastOp, LI.isVolatile(), CI->getName());
Bob Wilson4b71b6c2010-01-30 00:41:10 +0000133 NewLoad->setAlignment(LI.getAlignment());
Chris Lattnera65e2f72010-01-05 05:57:49 +0000134 // Now cast the result of the load.
135 return new BitCastInst(NewLoad, LI.getType());
136 }
137 }
138 }
139 return 0;
140}
141
142Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
143 Value *Op = LI.getOperand(0);
144
145 // Attempt to improve the alignment.
146 if (TD) {
147 unsigned KnownAlign =
148 GetOrEnforceKnownAlignment(Op, TD->getPrefTypeAlignment(LI.getType()));
Dan Gohman36196602010-08-03 18:20:32 +0000149 unsigned LoadAlign = LI.getAlignment();
150 unsigned EffectiveLoadAlign = LoadAlign != 0 ? LoadAlign :
151 TD->getABITypeAlignment(LI.getType());
152
153 if (KnownAlign > EffectiveLoadAlign)
Chris Lattnera65e2f72010-01-05 05:57:49 +0000154 LI.setAlignment(KnownAlign);
Dan Gohman36196602010-08-03 18:20:32 +0000155 else if (LoadAlign == 0)
156 LI.setAlignment(EffectiveLoadAlign);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000157 }
158
159 // load (cast X) --> cast (load X) iff safe.
160 if (isa<CastInst>(Op))
161 if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
162 return Res;
163
164 // None of the following transforms are legal for volatile loads.
165 if (LI.isVolatile()) return 0;
166
167 // Do really simple store-to-load forwarding and load CSE, to catch cases
168 // where there are several consequtive memory accesses to the same location,
169 // separated by a few arithmetic operations.
170 BasicBlock::iterator BBI = &LI;
171 if (Value *AvailableVal = FindAvailableLoadedValue(Op, LI.getParent(), BBI,6))
172 return ReplaceInstUsesWith(LI, AvailableVal);
173
174 // load(gep null, ...) -> unreachable
175 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
176 const Value *GEPI0 = GEPI->getOperand(0);
177 // TODO: Consider a target hook for valid address spaces for this xform.
178 if (isa<ConstantPointerNull>(GEPI0) && GEPI->getPointerAddressSpace() == 0){
179 // Insert a new store to null instruction before the load to indicate
180 // that this code is not reachable. We do this instead of inserting
181 // an unreachable instruction directly because we cannot modify the
182 // CFG.
183 new StoreInst(UndefValue::get(LI.getType()),
184 Constant::getNullValue(Op->getType()), &LI);
185 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
186 }
187 }
188
189 // load null/undef -> unreachable
190 // TODO: Consider a target hook for valid address spaces for this xform.
191 if (isa<UndefValue>(Op) ||
192 (isa<ConstantPointerNull>(Op) && LI.getPointerAddressSpace() == 0)) {
193 // Insert a new store to null instruction before the load to indicate that
194 // this code is not reachable. We do this instead of inserting an
195 // unreachable instruction directly because we cannot modify the CFG.
196 new StoreInst(UndefValue::get(LI.getType()),
197 Constant::getNullValue(Op->getType()), &LI);
198 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
199 }
200
201 // Instcombine load (constantexpr_cast global) -> cast (load global)
202 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op))
203 if (CE->isCast())
204 if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
205 return Res;
206
207 if (Op->hasOneUse()) {
208 // Change select and PHI nodes to select values instead of addresses: this
209 // helps alias analysis out a lot, allows many others simplifications, and
210 // exposes redundancy in the code.
211 //
212 // Note that we cannot do the transformation unless we know that the
213 // introduced loads cannot trap! Something like this is valid as long as
214 // the condition is always false: load (select bool %C, int* null, int* %G),
215 // but it would not be valid if we transformed it to load from null
216 // unconditionally.
217 //
218 if (SelectInst *SI = dyn_cast<SelectInst>(Op)) {
219 // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2).
Bob Wilson56600a12010-01-30 04:42:39 +0000220 unsigned Align = LI.getAlignment();
221 if (isSafeToLoadUnconditionally(SI->getOperand(1), SI, Align, TD) &&
222 isSafeToLoadUnconditionally(SI->getOperand(2), SI, Align, TD)) {
Bob Wilson4b71b6c2010-01-30 00:41:10 +0000223 LoadInst *V1 = Builder->CreateLoad(SI->getOperand(1),
Bob Wilson56600a12010-01-30 04:42:39 +0000224 SI->getOperand(1)->getName()+".val");
Bob Wilson4b71b6c2010-01-30 00:41:10 +0000225 LoadInst *V2 = Builder->CreateLoad(SI->getOperand(2),
Bob Wilson56600a12010-01-30 04:42:39 +0000226 SI->getOperand(2)->getName()+".val");
227 V1->setAlignment(Align);
228 V2->setAlignment(Align);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000229 return SelectInst::Create(SI->getCondition(), V1, V2);
230 }
231
232 // load (select (cond, null, P)) -> load P
233 if (Constant *C = dyn_cast<Constant>(SI->getOperand(1)))
234 if (C->isNullValue()) {
235 LI.setOperand(0, SI->getOperand(2));
236 return &LI;
237 }
238
239 // load (select (cond, P, null)) -> load P
240 if (Constant *C = dyn_cast<Constant>(SI->getOperand(2)))
241 if (C->isNullValue()) {
242 LI.setOperand(0, SI->getOperand(1));
243 return &LI;
244 }
245 }
246 }
247 return 0;
248}
249
250/// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P
251/// when possible. This makes it generally easy to do alias analysis and/or
252/// SROA/mem2reg of the memory object.
253static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) {
254 User *CI = cast<User>(SI.getOperand(1));
255 Value *CastOp = CI->getOperand(0);
256
257 const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType();
258 const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType());
259 if (SrcTy == 0) return 0;
260
261 const Type *SrcPTy = SrcTy->getElementType();
262
Duncan Sands19d0b472010-02-16 11:11:14 +0000263 if (!DestPTy->isIntegerTy() && !DestPTy->isPointerTy())
Chris Lattnera65e2f72010-01-05 05:57:49 +0000264 return 0;
265
266 /// NewGEPIndices - If SrcPTy is an aggregate type, we can emit a "noop gep"
267 /// to its first element. This allows us to handle things like:
268 /// store i32 xxx, (bitcast {foo*, float}* %P to i32*)
269 /// on 32-bit hosts.
270 SmallVector<Value*, 4> NewGEPIndices;
271
272 // If the source is an array, the code below will not succeed. Check to
273 // see if a trivial 'gep P, 0, 0' will help matters. Only do this for
274 // constants.
Duncan Sands19d0b472010-02-16 11:11:14 +0000275 if (SrcPTy->isArrayTy() || SrcPTy->isStructTy()) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000276 // Index through pointer.
277 Constant *Zero = Constant::getNullValue(Type::getInt32Ty(SI.getContext()));
278 NewGEPIndices.push_back(Zero);
279
280 while (1) {
281 if (const StructType *STy = dyn_cast<StructType>(SrcPTy)) {
282 if (!STy->getNumElements()) /* Struct can be empty {} */
283 break;
284 NewGEPIndices.push_back(Zero);
285 SrcPTy = STy->getElementType(0);
286 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcPTy)) {
287 NewGEPIndices.push_back(Zero);
288 SrcPTy = ATy->getElementType();
289 } else {
290 break;
291 }
292 }
293
294 SrcTy = PointerType::get(SrcPTy, SrcTy->getAddressSpace());
295 }
296
Duncan Sands19d0b472010-02-16 11:11:14 +0000297 if (!SrcPTy->isIntegerTy() && !SrcPTy->isPointerTy())
Chris Lattnera65e2f72010-01-05 05:57:49 +0000298 return 0;
299
300 // If the pointers point into different address spaces or if they point to
301 // values with different sizes, we can't do the transformation.
302 if (!IC.getTargetData() ||
303 SrcTy->getAddressSpace() !=
304 cast<PointerType>(CI->getType())->getAddressSpace() ||
305 IC.getTargetData()->getTypeSizeInBits(SrcPTy) !=
306 IC.getTargetData()->getTypeSizeInBits(DestPTy))
307 return 0;
308
309 // Okay, we are casting from one integer or pointer type to another of
310 // the same size. Instead of casting the pointer before
311 // the store, cast the value to be stored.
312 Value *NewCast;
313 Value *SIOp0 = SI.getOperand(0);
314 Instruction::CastOps opcode = Instruction::BitCast;
315 const Type* CastSrcTy = SIOp0->getType();
316 const Type* CastDstTy = SrcPTy;
Duncan Sands19d0b472010-02-16 11:11:14 +0000317 if (CastDstTy->isPointerTy()) {
Duncan Sands9dff9be2010-02-15 16:12:20 +0000318 if (CastSrcTy->isIntegerTy())
Chris Lattnera65e2f72010-01-05 05:57:49 +0000319 opcode = Instruction::IntToPtr;
Duncan Sands19d0b472010-02-16 11:11:14 +0000320 } else if (CastDstTy->isIntegerTy()) {
321 if (SIOp0->getType()->isPointerTy())
Chris Lattnera65e2f72010-01-05 05:57:49 +0000322 opcode = Instruction::PtrToInt;
323 }
324
325 // SIOp0 is a pointer to aggregate and this is a store to the first field,
326 // emit a GEP to index into its first field.
327 if (!NewGEPIndices.empty())
328 CastOp = IC.Builder->CreateInBoundsGEP(CastOp, NewGEPIndices.begin(),
329 NewGEPIndices.end());
330
331 NewCast = IC.Builder->CreateCast(opcode, SIOp0, CastDstTy,
332 SIOp0->getName()+".c");
Dan Gohman2e20dfb2010-10-25 16:16:27 +0000333 SI.setOperand(0, NewCast);
334 SI.setOperand(1, CastOp);
335 return &SI;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000336}
337
338/// equivalentAddressValues - Test if A and B will obviously have the same
339/// value. This includes recognizing that %t0 and %t1 will have the same
340/// value in code like this:
341/// %t0 = getelementptr \@a, 0, 3
342/// store i32 0, i32* %t0
343/// %t1 = getelementptr \@a, 0, 3
344/// %t2 = load i32* %t1
345///
346static bool equivalentAddressValues(Value *A, Value *B) {
347 // Test if the values are trivially equivalent.
348 if (A == B) return true;
349
350 // Test if the values come form identical arithmetic instructions.
351 // This uses isIdenticalToWhenDefined instead of isIdenticalTo because
352 // its only used to compare two uses within the same basic block, which
353 // means that they'll always either have the same value or one of them
354 // will have an undefined value.
355 if (isa<BinaryOperator>(A) ||
356 isa<CastInst>(A) ||
357 isa<PHINode>(A) ||
358 isa<GetElementPtrInst>(A))
359 if (Instruction *BI = dyn_cast<Instruction>(B))
360 if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
361 return true;
362
363 // Otherwise they may not be equivalent.
364 return false;
365}
366
367// If this instruction has two uses, one of which is a llvm.dbg.declare,
368// return the llvm.dbg.declare.
369DbgDeclareInst *InstCombiner::hasOneUsePlusDeclare(Value *V) {
370 if (!V->hasNUses(2))
371 return 0;
372 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
373 UI != E; ++UI) {
Gabor Greif60a346d2010-07-09 12:23:50 +0000374 User *U = *UI;
375 if (DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(U))
Chris Lattnera65e2f72010-01-05 05:57:49 +0000376 return DI;
Gabor Greif60a346d2010-07-09 12:23:50 +0000377 if (isa<BitCastInst>(U) && U->hasOneUse()) {
Gabor Greifdde79d82010-07-22 13:36:47 +0000378 if (DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(*U->use_begin()))
Chris Lattnera65e2f72010-01-05 05:57:49 +0000379 return DI;
380 }
381 }
382 return 0;
383}
384
385Instruction *InstCombiner::visitStoreInst(StoreInst &SI) {
386 Value *Val = SI.getOperand(0);
387 Value *Ptr = SI.getOperand(1);
388
389 // If the RHS is an alloca with a single use, zapify the store, making the
390 // alloca dead.
391 // If the RHS is an alloca with a two uses, the other one being a
392 // llvm.dbg.declare, zapify the store and the declare, making the
Eric Christopher84bd3162010-01-19 01:20:15 +0000393 // alloca dead. We must do this to prevent declares from affecting
Chris Lattnera65e2f72010-01-05 05:57:49 +0000394 // codegen.
395 if (!SI.isVolatile()) {
396 if (Ptr->hasOneUse()) {
397 if (isa<AllocaInst>(Ptr))
398 return EraseInstFromFunction(SI);
399 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
400 if (isa<AllocaInst>(GEP->getOperand(0))) {
401 if (GEP->getOperand(0)->hasOneUse())
402 return EraseInstFromFunction(SI);
403 if (DbgDeclareInst *DI = hasOneUsePlusDeclare(GEP->getOperand(0))) {
404 EraseInstFromFunction(*DI);
405 return EraseInstFromFunction(SI);
406 }
407 }
408 }
409 }
410 if (DbgDeclareInst *DI = hasOneUsePlusDeclare(Ptr)) {
411 EraseInstFromFunction(*DI);
412 return EraseInstFromFunction(SI);
413 }
414 }
415
416 // Attempt to improve the alignment.
417 if (TD) {
418 unsigned KnownAlign =
419 GetOrEnforceKnownAlignment(Ptr, TD->getPrefTypeAlignment(Val->getType()));
Dan Gohman36196602010-08-03 18:20:32 +0000420 unsigned StoreAlign = SI.getAlignment();
421 unsigned EffectiveStoreAlign = StoreAlign != 0 ? StoreAlign :
422 TD->getABITypeAlignment(Val->getType());
423
424 if (KnownAlign > EffectiveStoreAlign)
Chris Lattnera65e2f72010-01-05 05:57:49 +0000425 SI.setAlignment(KnownAlign);
Dan Gohman36196602010-08-03 18:20:32 +0000426 else if (StoreAlign == 0)
427 SI.setAlignment(EffectiveStoreAlign);
Chris Lattnera65e2f72010-01-05 05:57:49 +0000428 }
429
430 // Do really simple DSE, to catch cases where there are several consecutive
431 // stores to the same location, separated by a few arithmetic operations. This
432 // situation often occurs with bitfield accesses.
433 BasicBlock::iterator BBI = &SI;
434 for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts;
435 --ScanInsts) {
436 --BBI;
Victor Hernandez5f8c8c02010-01-22 19:05:05 +0000437 // Don't count debug info directives, lest they affect codegen,
438 // and we skip pointer-to-pointer bitcasts, which are NOPs.
439 if (isa<DbgInfoIntrinsic>(BBI) ||
Duncan Sands19d0b472010-02-16 11:11:14 +0000440 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000441 ScanInsts++;
442 continue;
443 }
444
445 if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) {
446 // Prev store isn't volatile, and stores to the same location?
447 if (!PrevSI->isVolatile() &&equivalentAddressValues(PrevSI->getOperand(1),
448 SI.getOperand(1))) {
449 ++NumDeadStore;
450 ++BBI;
451 EraseInstFromFunction(*PrevSI);
452 continue;
453 }
454 break;
455 }
456
457 // If this is a load, we have to stop. However, if the loaded value is from
458 // the pointer we're loading and is producing the pointer we're storing,
459 // then *this* store is dead (X = load P; store X -> P).
460 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
461 if (LI == Val && equivalentAddressValues(LI->getOperand(0), Ptr) &&
462 !SI.isVolatile())
463 return EraseInstFromFunction(SI);
464
465 // Otherwise, this is a load from some other location. Stores before it
466 // may not be dead.
467 break;
468 }
469
470 // Don't skip over loads or things that can modify memory.
471 if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory())
472 break;
473 }
474
475
476 if (SI.isVolatile()) return 0; // Don't hack volatile stores.
477
478 // store X, null -> turns into 'unreachable' in SimplifyCFG
479 if (isa<ConstantPointerNull>(Ptr) && SI.getPointerAddressSpace() == 0) {
480 if (!isa<UndefValue>(Val)) {
481 SI.setOperand(0, UndefValue::get(Val->getType()));
482 if (Instruction *U = dyn_cast<Instruction>(Val))
483 Worklist.Add(U); // Dropped a use.
484 }
485 return 0; // Do not modify these!
486 }
487
488 // store undef, Ptr -> noop
489 if (isa<UndefValue>(Val))
490 return EraseInstFromFunction(SI);
491
492 // If the pointer destination is a cast, see if we can fold the cast into the
493 // source instead.
494 if (isa<CastInst>(Ptr))
495 if (Instruction *Res = InstCombineStoreToCast(*this, SI))
496 return Res;
497 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
498 if (CE->isCast())
499 if (Instruction *Res = InstCombineStoreToCast(*this, SI))
500 return Res;
501
502
503 // If this store is the last instruction in the basic block (possibly
Victor Hernandez5f5abd52010-01-21 23:07:15 +0000504 // excepting debug info instructions), and if the block ends with an
505 // unconditional branch, try to move it to the successor block.
Chris Lattnera65e2f72010-01-05 05:57:49 +0000506 BBI = &SI;
507 do {
508 ++BBI;
Victor Hernandez5f8c8c02010-01-22 19:05:05 +0000509 } while (isa<DbgInfoIntrinsic>(BBI) ||
Duncan Sands19d0b472010-02-16 11:11:14 +0000510 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy()));
Chris Lattnera65e2f72010-01-05 05:57:49 +0000511 if (BranchInst *BI = dyn_cast<BranchInst>(BBI))
512 if (BI->isUnconditional())
513 if (SimplifyStoreAtEndOfBlock(SI))
514 return 0; // xform done!
515
516 return 0;
517}
518
519/// SimplifyStoreAtEndOfBlock - Turn things like:
520/// if () { *P = v1; } else { *P = v2 }
521/// into a phi node with a store in the successor.
522///
523/// Simplify things like:
524/// *P = v1; if () { *P = v2; }
525/// into a phi node with a store in the successor.
526///
527bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) {
528 BasicBlock *StoreBB = SI.getParent();
529
530 // Check to see if the successor block has exactly two incoming edges. If
531 // so, see if the other predecessor contains a store to the same location.
532 // if so, insert a PHI node (if needed) and move the stores down.
533 BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0);
534
535 // Determine whether Dest has exactly two predecessors and, if so, compute
536 // the other predecessor.
537 pred_iterator PI = pred_begin(DestBB);
Gabor Greif1b787df2010-07-12 15:48:26 +0000538 BasicBlock *P = *PI;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000539 BasicBlock *OtherBB = 0;
Gabor Greif1b787df2010-07-12 15:48:26 +0000540
541 if (P != StoreBB)
542 OtherBB = P;
543
544 if (++PI == pred_end(DestBB))
Chris Lattnera65e2f72010-01-05 05:57:49 +0000545 return false;
546
Gabor Greif1b787df2010-07-12 15:48:26 +0000547 P = *PI;
548 if (P != StoreBB) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000549 if (OtherBB)
550 return false;
Gabor Greif1b787df2010-07-12 15:48:26 +0000551 OtherBB = P;
Chris Lattnera65e2f72010-01-05 05:57:49 +0000552 }
553 if (++PI != pred_end(DestBB))
554 return false;
555
556 // Bail out if all the relevant blocks aren't distinct (this can happen,
557 // for example, if SI is in an infinite loop)
558 if (StoreBB == DestBB || OtherBB == DestBB)
559 return false;
560
561 // Verify that the other block ends in a branch and is not otherwise empty.
562 BasicBlock::iterator BBI = OtherBB->getTerminator();
563 BranchInst *OtherBr = dyn_cast<BranchInst>(BBI);
564 if (!OtherBr || BBI == OtherBB->begin())
565 return false;
566
567 // If the other block ends in an unconditional branch, check for the 'if then
568 // else' case. there is an instruction before the branch.
569 StoreInst *OtherStore = 0;
570 if (OtherBr->isUnconditional()) {
571 --BBI;
572 // Skip over debugging info.
Victor Hernandez5f8c8c02010-01-22 19:05:05 +0000573 while (isa<DbgInfoIntrinsic>(BBI) ||
Duncan Sands19d0b472010-02-16 11:11:14 +0000574 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) {
Chris Lattnera65e2f72010-01-05 05:57:49 +0000575 if (BBI==OtherBB->begin())
576 return false;
577 --BBI;
578 }
579 // If this isn't a store, isn't a store to the same location, or if the
580 // alignments differ, bail out.
581 OtherStore = dyn_cast<StoreInst>(BBI);
582 if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1) ||
583 OtherStore->getAlignment() != SI.getAlignment())
584 return false;
585 } else {
586 // Otherwise, the other block ended with a conditional branch. If one of the
587 // destinations is StoreBB, then we have the if/then case.
588 if (OtherBr->getSuccessor(0) != StoreBB &&
589 OtherBr->getSuccessor(1) != StoreBB)
590 return false;
591
592 // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an
593 // if/then triangle. See if there is a store to the same ptr as SI that
594 // lives in OtherBB.
595 for (;; --BBI) {
596 // Check to see if we find the matching store.
597 if ((OtherStore = dyn_cast<StoreInst>(BBI))) {
598 if (OtherStore->getOperand(1) != SI.getOperand(1) ||
599 OtherStore->getAlignment() != SI.getAlignment())
600 return false;
601 break;
602 }
603 // If we find something that may be using or overwriting the stored
604 // value, or if we run out of instructions, we can't do the xform.
605 if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() ||
606 BBI == OtherBB->begin())
607 return false;
608 }
609
610 // In order to eliminate the store in OtherBr, we have to
611 // make sure nothing reads or overwrites the stored value in
612 // StoreBB.
613 for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) {
614 // FIXME: This should really be AA driven.
615 if (I->mayReadFromMemory() || I->mayWriteToMemory())
616 return false;
617 }
618 }
619
620 // Insert a PHI node now if we need it.
621 Value *MergedVal = OtherStore->getOperand(0);
622 if (MergedVal != SI.getOperand(0)) {
623 PHINode *PN = PHINode::Create(MergedVal->getType(), "storemerge");
624 PN->reserveOperandSpace(2);
625 PN->addIncoming(SI.getOperand(0), SI.getParent());
626 PN->addIncoming(OtherStore->getOperand(0), OtherBB);
627 MergedVal = InsertNewInstBefore(PN, DestBB->front());
628 }
629
630 // Advance to a place where it is safe to insert the new store and
631 // insert it.
632 BBI = DestBB->getFirstNonPHI();
633 InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1),
634 OtherStore->isVolatile(),
635 SI.getAlignment()), *BBI);
636
637 // Nuke the old stores.
638 EraseInstFromFunction(SI);
639 EraseInstFromFunction(*OtherStore);
640 return true;
641}