When a set of bitmask operations, typically from a bitfield initialization, only...
[oota-llvm.git] / lib / Transforms / InstCombine / InstCombineLoadStoreAlloca.cpp
1 //===- 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"
16 #include "llvm/Analysis/Loads.h"
17 #include "llvm/Support/PatternMatch.h"
18 #include "llvm/Target/TargetData.h"
19 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
20 #include "llvm/Transforms/Utils/Local.h"
21 #include "llvm/ADT/Statistic.h"
22 using namespace llvm;
23 using namespace PatternMatch;
24
25 STATISTIC(NumDeadStore, "Number of dead stores eliminated");
26
27 Instruction *InstCombiner::visitAllocaInst(AllocaInst &AI) {
28   // Ensure that the alloca array size argument has type intptr_t, so that
29   // any casting is exposed early.
30   if (TD) {
31     const Type *IntPtrTy = TD->getIntPtrType(AI.getContext());
32     if (AI.getArraySize()->getType() != IntPtrTy) {
33       Value *V = Builder->CreateIntCast(AI.getArraySize(),
34                                         IntPtrTy, false);
35       AI.setOperand(0, V);
36       return &AI;
37     }
38   }
39
40   // Convert: alloca Ty, C - where C is a constant != 1 into: alloca [C x Ty], 1
41   if (AI.isArrayAllocation()) {  // Check C != 1
42     if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) {
43       const Type *NewTy = 
44         ArrayType::get(AI.getAllocatedType(), C->getZExtValue());
45       assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!");
46       AllocaInst *New = Builder->CreateAlloca(NewTy, 0, AI.getName());
47       New->setAlignment(AI.getAlignment());
48
49       // Scan to the end of the allocation instructions, to skip over a block of
50       // allocas if possible...also skip interleaved debug info
51       //
52       BasicBlock::iterator It = New;
53       while (isa<AllocaInst>(*It) || isa<DbgInfoIntrinsic>(*It)) ++It;
54
55       // Now that I is pointing to the first non-allocation-inst in the block,
56       // insert our getelementptr instruction...
57       //
58       Value *NullIdx =Constant::getNullValue(Type::getInt32Ty(AI.getContext()));
59       Value *Idx[2];
60       Idx[0] = NullIdx;
61       Idx[1] = NullIdx;
62       Value *V = GetElementPtrInst::CreateInBounds(New, Idx, Idx + 2,
63                                                    New->getName()+".sub", It);
64
65       // Now make everything use the getelementptr instead of the original
66       // allocation.
67       return ReplaceInstUsesWith(AI, V);
68     } else if (isa<UndefValue>(AI.getArraySize())) {
69       return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
70     }
71   }
72
73   if (TD && isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized()) {
74     // If alloca'ing a zero byte object, replace the alloca with a null pointer.
75     // Note that we only do this for alloca's, because malloc should allocate
76     // and return a unique pointer, even for a zero byte allocation.
77     if (TD->getTypeAllocSize(AI.getAllocatedType()) == 0)
78       return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
79
80     // If the alignment is 0 (unspecified), assign it the preferred alignment.
81     if (AI.getAlignment() == 0)
82       AI.setAlignment(TD->getPrefTypeAlignment(AI.getAllocatedType()));
83   }
84
85   return 0;
86 }
87
88
89 /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible.
90 static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI,
91                                         const TargetData *TD) {
92   User *CI = cast<User>(LI.getOperand(0));
93   Value *CastOp = CI->getOperand(0);
94
95   const PointerType *DestTy = cast<PointerType>(CI->getType());
96   const Type *DestPTy = DestTy->getElementType();
97   if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) {
98
99     // If the address spaces don't match, don't eliminate the cast.
100     if (DestTy->getAddressSpace() != SrcTy->getAddressSpace())
101       return 0;
102
103     const Type *SrcPTy = SrcTy->getElementType();
104
105     if (DestPTy->isIntegerTy() || DestPTy->isPointerTy() || 
106          DestPTy->isVectorTy()) {
107       // If the source is an array, the code below will not succeed.  Check to
108       // see if a trivial 'gep P, 0, 0' will help matters.  Only do this for
109       // constants.
110       if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy))
111         if (Constant *CSrc = dyn_cast<Constant>(CastOp))
112           if (ASrcTy->getNumElements() != 0) {
113             Value *Idxs[2];
114             Idxs[0] = Constant::getNullValue(Type::getInt32Ty(LI.getContext()));
115             Idxs[1] = Idxs[0];
116             CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2);
117             SrcTy = cast<PointerType>(CastOp->getType());
118             SrcPTy = SrcTy->getElementType();
119           }
120
121       if (IC.getTargetData() &&
122           (SrcPTy->isIntegerTy() || SrcPTy->isPointerTy() || 
123             SrcPTy->isVectorTy()) &&
124           // Do not allow turning this into a load of an integer, which is then
125           // casted to a pointer, this pessimizes pointer analysis a lot.
126           (SrcPTy->isPointerTy() == LI.getType()->isPointerTy()) &&
127           IC.getTargetData()->getTypeSizeInBits(SrcPTy) ==
128                IC.getTargetData()->getTypeSizeInBits(DestPTy)) {
129
130         // Okay, we are casting from one integer or pointer type to another of
131         // the same size.  Instead of casting the pointer before the load, cast
132         // the result of the loaded value.
133         LoadInst *NewLoad = 
134           IC.Builder->CreateLoad(CastOp, LI.isVolatile(), CI->getName());
135         NewLoad->setAlignment(LI.getAlignment());
136         // Now cast the result of the load.
137         return new BitCastInst(NewLoad, LI.getType());
138       }
139     }
140   }
141   return 0;
142 }
143
144 Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
145   Value *Op = LI.getOperand(0);
146
147   // Attempt to improve the alignment.
148   if (TD) {
149     unsigned KnownAlign =
150       GetOrEnforceKnownAlignment(Op, TD->getPrefTypeAlignment(LI.getType()));
151     unsigned LoadAlign = LI.getAlignment();
152     unsigned EffectiveLoadAlign = LoadAlign != 0 ? LoadAlign :
153       TD->getABITypeAlignment(LI.getType());
154
155     if (KnownAlign > EffectiveLoadAlign)
156       LI.setAlignment(KnownAlign);
157     else if (LoadAlign == 0)
158       LI.setAlignment(EffectiveLoadAlign);
159   }
160
161   // load (cast X) --> cast (load X) iff safe.
162   if (isa<CastInst>(Op))
163     if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
164       return Res;
165
166   // None of the following transforms are legal for volatile loads.
167   if (LI.isVolatile()) return 0;
168   
169   // Do really simple store-to-load forwarding and load CSE, to catch cases
170   // where there are several consequtive memory accesses to the same location,
171   // separated by a few arithmetic operations.
172   BasicBlock::iterator BBI = &LI;
173   if (Value *AvailableVal = FindAvailableLoadedValue(Op, LI.getParent(), BBI,6))
174     return ReplaceInstUsesWith(LI, AvailableVal);
175
176   // load(gep null, ...) -> unreachable
177   if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
178     const Value *GEPI0 = GEPI->getOperand(0);
179     // TODO: Consider a target hook for valid address spaces for this xform.
180     if (isa<ConstantPointerNull>(GEPI0) && GEPI->getPointerAddressSpace() == 0){
181       // Insert a new store to null instruction before the load to indicate
182       // that this code is not reachable.  We do this instead of inserting
183       // an unreachable instruction directly because we cannot modify the
184       // CFG.
185       new StoreInst(UndefValue::get(LI.getType()),
186                     Constant::getNullValue(Op->getType()), &LI);
187       return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
188     }
189   } 
190
191   // load null/undef -> unreachable
192   // TODO: Consider a target hook for valid address spaces for this xform.
193   if (isa<UndefValue>(Op) ||
194       (isa<ConstantPointerNull>(Op) && LI.getPointerAddressSpace() == 0)) {
195     // Insert a new store to null instruction before the load to indicate that
196     // this code is not reachable.  We do this instead of inserting an
197     // unreachable instruction directly because we cannot modify the CFG.
198     new StoreInst(UndefValue::get(LI.getType()),
199                   Constant::getNullValue(Op->getType()), &LI);
200     return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
201   }
202
203   // Instcombine load (constantexpr_cast global) -> cast (load global)
204   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op))
205     if (CE->isCast())
206       if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
207         return Res;
208   
209   if (Op->hasOneUse()) {
210     // Change select and PHI nodes to select values instead of addresses: this
211     // helps alias analysis out a lot, allows many others simplifications, and
212     // exposes redundancy in the code.
213     //
214     // Note that we cannot do the transformation unless we know that the
215     // introduced loads cannot trap!  Something like this is valid as long as
216     // the condition is always false: load (select bool %C, int* null, int* %G),
217     // but it would not be valid if we transformed it to load from null
218     // unconditionally.
219     //
220     if (SelectInst *SI = dyn_cast<SelectInst>(Op)) {
221       // load (select (Cond, &V1, &V2))  --> select(Cond, load &V1, load &V2).
222       unsigned Align = LI.getAlignment();
223       if (isSafeToLoadUnconditionally(SI->getOperand(1), SI, Align, TD) &&
224           isSafeToLoadUnconditionally(SI->getOperand(2), SI, Align, TD)) {
225         LoadInst *V1 = Builder->CreateLoad(SI->getOperand(1),
226                                            SI->getOperand(1)->getName()+".val");
227         LoadInst *V2 = Builder->CreateLoad(SI->getOperand(2),
228                                            SI->getOperand(2)->getName()+".val");
229         V1->setAlignment(Align);
230         V2->setAlignment(Align);
231         return SelectInst::Create(SI->getCondition(), V1, V2);
232       }
233
234       // load (select (cond, null, P)) -> load P
235       if (Constant *C = dyn_cast<Constant>(SI->getOperand(1)))
236         if (C->isNullValue()) {
237           LI.setOperand(0, SI->getOperand(2));
238           return &LI;
239         }
240
241       // load (select (cond, P, null)) -> load P
242       if (Constant *C = dyn_cast<Constant>(SI->getOperand(2)))
243         if (C->isNullValue()) {
244           LI.setOperand(0, SI->getOperand(1));
245           return &LI;
246         }
247     }
248   }
249   return 0;
250 }
251
252 /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P
253 /// when possible.  This makes it generally easy to do alias analysis and/or
254 /// SROA/mem2reg of the memory object.
255 static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) {
256   User *CI = cast<User>(SI.getOperand(1));
257   Value *CastOp = CI->getOperand(0);
258
259   const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType();
260   const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType());
261   if (SrcTy == 0) return 0;
262   
263   const Type *SrcPTy = SrcTy->getElementType();
264
265   if (!DestPTy->isIntegerTy() && !DestPTy->isPointerTy())
266     return 0;
267   
268   /// NewGEPIndices - If SrcPTy is an aggregate type, we can emit a "noop gep"
269   /// to its first element.  This allows us to handle things like:
270   ///   store i32 xxx, (bitcast {foo*, float}* %P to i32*)
271   /// on 32-bit hosts.
272   SmallVector<Value*, 4> NewGEPIndices;
273   
274   // If the source is an array, the code below will not succeed.  Check to
275   // see if a trivial 'gep P, 0, 0' will help matters.  Only do this for
276   // constants.
277   if (SrcPTy->isArrayTy() || SrcPTy->isStructTy()) {
278     // Index through pointer.
279     Constant *Zero = Constant::getNullValue(Type::getInt32Ty(SI.getContext()));
280     NewGEPIndices.push_back(Zero);
281     
282     while (1) {
283       if (const StructType *STy = dyn_cast<StructType>(SrcPTy)) {
284         if (!STy->getNumElements()) /* Struct can be empty {} */
285           break;
286         NewGEPIndices.push_back(Zero);
287         SrcPTy = STy->getElementType(0);
288       } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcPTy)) {
289         NewGEPIndices.push_back(Zero);
290         SrcPTy = ATy->getElementType();
291       } else {
292         break;
293       }
294     }
295     
296     SrcTy = PointerType::get(SrcPTy, SrcTy->getAddressSpace());
297   }
298
299   if (!SrcPTy->isIntegerTy() && !SrcPTy->isPointerTy())
300     return 0;
301   
302   // If the pointers point into different address spaces or if they point to
303   // values with different sizes, we can't do the transformation.
304   if (!IC.getTargetData() ||
305       SrcTy->getAddressSpace() != 
306         cast<PointerType>(CI->getType())->getAddressSpace() ||
307       IC.getTargetData()->getTypeSizeInBits(SrcPTy) !=
308       IC.getTargetData()->getTypeSizeInBits(DestPTy))
309     return 0;
310
311   // Okay, we are casting from one integer or pointer type to another of
312   // the same size.  Instead of casting the pointer before 
313   // the store, cast the value to be stored.
314   Value *NewCast;
315   Value *SIOp0 = SI.getOperand(0);
316   Instruction::CastOps opcode = Instruction::BitCast;
317   const Type* CastSrcTy = SIOp0->getType();
318   const Type* CastDstTy = SrcPTy;
319   if (CastDstTy->isPointerTy()) {
320     if (CastSrcTy->isIntegerTy())
321       opcode = Instruction::IntToPtr;
322   } else if (CastDstTy->isIntegerTy()) {
323     if (SIOp0->getType()->isPointerTy())
324       opcode = Instruction::PtrToInt;
325   }
326   
327   // SIOp0 is a pointer to aggregate and this is a store to the first field,
328   // emit a GEP to index into its first field.
329   if (!NewGEPIndices.empty())
330     CastOp = IC.Builder->CreateInBoundsGEP(CastOp, NewGEPIndices.begin(),
331                                            NewGEPIndices.end());
332   
333   NewCast = IC.Builder->CreateCast(opcode, SIOp0, CastDstTy,
334                                    SIOp0->getName()+".c");
335   return new StoreInst(NewCast, CastOp);
336 }
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 ///
346 static 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.
369 DbgDeclareInst *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) {
374     User *U = *UI;
375     if (DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(U))
376       return DI;
377     if (isa<BitCastInst>(U) && U->hasOneUse()) {
378       if (DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(*U->use_begin()))
379         return DI;
380       }
381   }
382   return 0;
383 }
384
385 Instruction *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
393   // alloca dead.  We must do this to prevent declares from affecting
394   // 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()));
420     unsigned StoreAlign = SI.getAlignment();
421     unsigned EffectiveStoreAlign = StoreAlign != 0 ? StoreAlign :
422       TD->getABITypeAlignment(Val->getType());
423
424     if (KnownAlign > EffectiveStoreAlign)
425       SI.setAlignment(KnownAlign);
426     else if (StoreAlign == 0)
427       SI.setAlignment(EffectiveStoreAlign);
428   }
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;
437     // 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) ||
440         (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) {
441       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   // Attempt to narrow sequences where we load a wide value, perform bitmasks
479   // that only affect the low bits of it, and then store it back.  This 
480   // typically arises from bitfield initializers in C++.
481   ConstantInt *CI1 =0, *CI2 = 0;
482   Value *Ld = 0;
483   if (getTargetData() &&
484       match(SI.getValueOperand(),
485             m_And(m_Or(m_Value(Ld), m_ConstantInt(CI1)), m_ConstantInt(CI2))) &&
486       isa<LoadInst>(Ld) &&
487       equivalentAddressValues(cast<LoadInst>(Ld)->getPointerOperand(), Ptr)) {
488     APInt OrMask = CI1->getValue();
489     APInt AndMask = CI2->getValue();
490     
491     // Compute the prefix of the value that is unmodified by the bitmasking.
492     unsigned LeadingAndOnes = AndMask.countLeadingOnes();
493     unsigned LeadingOrZeros = OrMask.countLeadingZeros();
494     unsigned Prefix = std::min(LeadingAndOnes, LeadingOrZeros);
495     uint64_t NewWidth = AndMask.getBitWidth() - Prefix;
496     if (!isPowerOf2_64(NewWidth)) NewWidth = NextPowerOf2(NewWidth);
497     
498     // If we can find a power-of-2 prefix (and if the values we're working with
499     // are themselves POT widths), then we can narrow the store.  We rely on
500     // later iterations of instcombine to propagate the demanded bits to narrow
501     // the other computations in the chain.
502     if (NewWidth < AndMask.getBitWidth() && 
503         isPowerOf2_64(AndMask.getBitWidth())) {
504       const Type *NewType = IntegerType::get(Ptr->getContext(), NewWidth);
505       const Type *NewPtrType = PointerType::getUnqual(NewType);
506       
507       Value *NewVal = Builder->CreateTrunc(SI.getValueOperand(), NewType);
508       Value *NewPtr = Builder->CreateBitCast(Ptr, NewPtrType);
509       
510       // On big endian targets, we need to offset from the original pointer
511       // in order to store to the low-bit suffix.
512       if (getTargetData()->isBigEndian()) {
513         uint64_t GEPOffset = (AndMask.getBitWidth() - NewWidth) / 8;
514         NewPtr = Builder->CreateConstGEP1_64(NewPtr, GEPOffset);
515       }
516       
517       return new StoreInst(NewVal, NewPtr);
518     }
519   }
520
521   // store X, null    -> turns into 'unreachable' in SimplifyCFG
522   if (isa<ConstantPointerNull>(Ptr) && SI.getPointerAddressSpace() == 0) {
523     if (!isa<UndefValue>(Val)) {
524       SI.setOperand(0, UndefValue::get(Val->getType()));
525       if (Instruction *U = dyn_cast<Instruction>(Val))
526         Worklist.Add(U);  // Dropped a use.
527     }
528     return 0;  // Do not modify these!
529   }
530
531   // store undef, Ptr -> noop
532   if (isa<UndefValue>(Val))
533     return EraseInstFromFunction(SI);
534
535   // If the pointer destination is a cast, see if we can fold the cast into the
536   // source instead.
537   if (isa<CastInst>(Ptr))
538     if (Instruction *Res = InstCombineStoreToCast(*this, SI))
539       return Res;
540   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
541     if (CE->isCast())
542       if (Instruction *Res = InstCombineStoreToCast(*this, SI))
543         return Res;
544
545   
546   // If this store is the last instruction in the basic block (possibly
547   // excepting debug info instructions), and if the block ends with an
548   // unconditional branch, try to move it to the successor block.
549   BBI = &SI; 
550   do {
551     ++BBI;
552   } while (isa<DbgInfoIntrinsic>(BBI) ||
553            (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy()));
554   if (BranchInst *BI = dyn_cast<BranchInst>(BBI))
555     if (BI->isUnconditional())
556       if (SimplifyStoreAtEndOfBlock(SI))
557         return 0;  // xform done!
558   
559   return 0;
560 }
561
562 /// SimplifyStoreAtEndOfBlock - Turn things like:
563 ///   if () { *P = v1; } else { *P = v2 }
564 /// into a phi node with a store in the successor.
565 ///
566 /// Simplify things like:
567 ///   *P = v1; if () { *P = v2; }
568 /// into a phi node with a store in the successor.
569 ///
570 bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) {
571   BasicBlock *StoreBB = SI.getParent();
572   
573   // Check to see if the successor block has exactly two incoming edges.  If
574   // so, see if the other predecessor contains a store to the same location.
575   // if so, insert a PHI node (if needed) and move the stores down.
576   BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0);
577   
578   // Determine whether Dest has exactly two predecessors and, if so, compute
579   // the other predecessor.
580   pred_iterator PI = pred_begin(DestBB);
581   BasicBlock *P = *PI;
582   BasicBlock *OtherBB = 0;
583
584   if (P != StoreBB)
585     OtherBB = P;
586
587   if (++PI == pred_end(DestBB))
588     return false;
589   
590   P = *PI;
591   if (P != StoreBB) {
592     if (OtherBB)
593       return false;
594     OtherBB = P;
595   }
596   if (++PI != pred_end(DestBB))
597     return false;
598
599   // Bail out if all the relevant blocks aren't distinct (this can happen,
600   // for example, if SI is in an infinite loop)
601   if (StoreBB == DestBB || OtherBB == DestBB)
602     return false;
603
604   // Verify that the other block ends in a branch and is not otherwise empty.
605   BasicBlock::iterator BBI = OtherBB->getTerminator();
606   BranchInst *OtherBr = dyn_cast<BranchInst>(BBI);
607   if (!OtherBr || BBI == OtherBB->begin())
608     return false;
609   
610   // If the other block ends in an unconditional branch, check for the 'if then
611   // else' case.  there is an instruction before the branch.
612   StoreInst *OtherStore = 0;
613   if (OtherBr->isUnconditional()) {
614     --BBI;
615     // Skip over debugging info.
616     while (isa<DbgInfoIntrinsic>(BBI) ||
617            (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) {
618       if (BBI==OtherBB->begin())
619         return false;
620       --BBI;
621     }
622     // If this isn't a store, isn't a store to the same location, or if the
623     // alignments differ, bail out.
624     OtherStore = dyn_cast<StoreInst>(BBI);
625     if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1) ||
626         OtherStore->getAlignment() != SI.getAlignment())
627       return false;
628   } else {
629     // Otherwise, the other block ended with a conditional branch. If one of the
630     // destinations is StoreBB, then we have the if/then case.
631     if (OtherBr->getSuccessor(0) != StoreBB && 
632         OtherBr->getSuccessor(1) != StoreBB)
633       return false;
634     
635     // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an
636     // if/then triangle.  See if there is a store to the same ptr as SI that
637     // lives in OtherBB.
638     for (;; --BBI) {
639       // Check to see if we find the matching store.
640       if ((OtherStore = dyn_cast<StoreInst>(BBI))) {
641         if (OtherStore->getOperand(1) != SI.getOperand(1) ||
642             OtherStore->getAlignment() != SI.getAlignment())
643           return false;
644         break;
645       }
646       // If we find something that may be using or overwriting the stored
647       // value, or if we run out of instructions, we can't do the xform.
648       if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() ||
649           BBI == OtherBB->begin())
650         return false;
651     }
652     
653     // In order to eliminate the store in OtherBr, we have to
654     // make sure nothing reads or overwrites the stored value in
655     // StoreBB.
656     for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) {
657       // FIXME: This should really be AA driven.
658       if (I->mayReadFromMemory() || I->mayWriteToMemory())
659         return false;
660     }
661   }
662   
663   // Insert a PHI node now if we need it.
664   Value *MergedVal = OtherStore->getOperand(0);
665   if (MergedVal != SI.getOperand(0)) {
666     PHINode *PN = PHINode::Create(MergedVal->getType(), "storemerge");
667     PN->reserveOperandSpace(2);
668     PN->addIncoming(SI.getOperand(0), SI.getParent());
669     PN->addIncoming(OtherStore->getOperand(0), OtherBB);
670     MergedVal = InsertNewInstBefore(PN, DestBB->front());
671   }
672   
673   // Advance to a place where it is safe to insert the new store and
674   // insert it.
675   BBI = DestBB->getFirstNonPHI();
676   InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1),
677                                     OtherStore->isVolatile(),
678                                     SI.getAlignment()), *BBI);
679   
680   // Nuke the old stores.
681   EraseInstFromFunction(SI);
682   EraseInstFromFunction(*OtherStore);
683   return true;
684 }