Revert r220349 to re-instate r220277 with a fix for PR21330 -- quite
[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/ADT/Statistic.h"
16 #include "llvm/Analysis/Loads.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/IntrinsicInst.h"
20 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
21 #include "llvm/Transforms/Utils/Local.h"
22 using namespace llvm;
23
24 #define DEBUG_TYPE "instcombine"
25
26 STATISTIC(NumDeadStore,    "Number of dead stores eliminated");
27 STATISTIC(NumGlobalCopies, "Number of allocas copied from constant global");
28
29 /// pointsToConstantGlobal - Return true if V (possibly indirectly) points to
30 /// some part of a constant global variable.  This intentionally only accepts
31 /// constant expressions because we can't rewrite arbitrary instructions.
32 static bool pointsToConstantGlobal(Value *V) {
33   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
34     return GV->isConstant();
35
36   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
37     if (CE->getOpcode() == Instruction::BitCast ||
38         CE->getOpcode() == Instruction::AddrSpaceCast ||
39         CE->getOpcode() == Instruction::GetElementPtr)
40       return pointsToConstantGlobal(CE->getOperand(0));
41   }
42   return false;
43 }
44
45 /// isOnlyCopiedFromConstantGlobal - Recursively walk the uses of a (derived)
46 /// pointer to an alloca.  Ignore any reads of the pointer, return false if we
47 /// see any stores or other unknown uses.  If we see pointer arithmetic, keep
48 /// track of whether it moves the pointer (with IsOffset) but otherwise traverse
49 /// the uses.  If we see a memcpy/memmove that targets an unoffseted pointer to
50 /// the alloca, and if the source pointer is a pointer to a constant global, we
51 /// can optimize this.
52 static bool
53 isOnlyCopiedFromConstantGlobal(Value *V, MemTransferInst *&TheCopy,
54                                SmallVectorImpl<Instruction *> &ToDelete) {
55   // We track lifetime intrinsics as we encounter them.  If we decide to go
56   // ahead and replace the value with the global, this lets the caller quickly
57   // eliminate the markers.
58
59   SmallVector<std::pair<Value *, bool>, 35> ValuesToInspect;
60   ValuesToInspect.push_back(std::make_pair(V, false));
61   while (!ValuesToInspect.empty()) {
62     auto ValuePair = ValuesToInspect.pop_back_val();
63     const bool IsOffset = ValuePair.second;
64     for (auto &U : ValuePair.first->uses()) {
65       Instruction *I = cast<Instruction>(U.getUser());
66
67       if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
68         // Ignore non-volatile loads, they are always ok.
69         if (!LI->isSimple()) return false;
70         continue;
71       }
72
73       if (isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I)) {
74         // If uses of the bitcast are ok, we are ok.
75         ValuesToInspect.push_back(std::make_pair(I, IsOffset));
76         continue;
77       }
78       if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
79         // If the GEP has all zero indices, it doesn't offset the pointer. If it
80         // doesn't, it does.
81         ValuesToInspect.push_back(
82             std::make_pair(I, IsOffset || !GEP->hasAllZeroIndices()));
83         continue;
84       }
85
86       if (CallSite CS = I) {
87         // If this is the function being called then we treat it like a load and
88         // ignore it.
89         if (CS.isCallee(&U))
90           continue;
91
92         // Inalloca arguments are clobbered by the call.
93         unsigned ArgNo = CS.getArgumentNo(&U);
94         if (CS.isInAllocaArgument(ArgNo))
95           return false;
96
97         // If this is a readonly/readnone call site, then we know it is just a
98         // load (but one that potentially returns the value itself), so we can
99         // ignore it if we know that the value isn't captured.
100         if (CS.onlyReadsMemory() &&
101             (CS.getInstruction()->use_empty() || CS.doesNotCapture(ArgNo)))
102           continue;
103
104         // If this is being passed as a byval argument, the caller is making a
105         // copy, so it is only a read of the alloca.
106         if (CS.isByValArgument(ArgNo))
107           continue;
108       }
109
110       // Lifetime intrinsics can be handled by the caller.
111       if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
112         if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
113             II->getIntrinsicID() == Intrinsic::lifetime_end) {
114           assert(II->use_empty() && "Lifetime markers have no result to use!");
115           ToDelete.push_back(II);
116           continue;
117         }
118       }
119
120       // If this is isn't our memcpy/memmove, reject it as something we can't
121       // handle.
122       MemTransferInst *MI = dyn_cast<MemTransferInst>(I);
123       if (!MI)
124         return false;
125
126       // If the transfer is using the alloca as a source of the transfer, then
127       // ignore it since it is a load (unless the transfer is volatile).
128       if (U.getOperandNo() == 1) {
129         if (MI->isVolatile()) return false;
130         continue;
131       }
132
133       // If we already have seen a copy, reject the second one.
134       if (TheCopy) return false;
135
136       // If the pointer has been offset from the start of the alloca, we can't
137       // safely handle this.
138       if (IsOffset) return false;
139
140       // If the memintrinsic isn't using the alloca as the dest, reject it.
141       if (U.getOperandNo() != 0) return false;
142
143       // If the source of the memcpy/move is not a constant global, reject it.
144       if (!pointsToConstantGlobal(MI->getSource()))
145         return false;
146
147       // Otherwise, the transform is safe.  Remember the copy instruction.
148       TheCopy = MI;
149     }
150   }
151   return true;
152 }
153
154 /// isOnlyCopiedFromConstantGlobal - Return true if the specified alloca is only
155 /// modified by a copy from a constant global.  If we can prove this, we can
156 /// replace any uses of the alloca with uses of the global directly.
157 static MemTransferInst *
158 isOnlyCopiedFromConstantGlobal(AllocaInst *AI,
159                                SmallVectorImpl<Instruction *> &ToDelete) {
160   MemTransferInst *TheCopy = nullptr;
161   if (isOnlyCopiedFromConstantGlobal(AI, TheCopy, ToDelete))
162     return TheCopy;
163   return nullptr;
164 }
165
166 Instruction *InstCombiner::visitAllocaInst(AllocaInst &AI) {
167   // Ensure that the alloca array size argument has type intptr_t, so that
168   // any casting is exposed early.
169   if (DL) {
170     Type *IntPtrTy = DL->getIntPtrType(AI.getType());
171     if (AI.getArraySize()->getType() != IntPtrTy) {
172       Value *V = Builder->CreateIntCast(AI.getArraySize(),
173                                         IntPtrTy, false);
174       AI.setOperand(0, V);
175       return &AI;
176     }
177   }
178
179   // Convert: alloca Ty, C - where C is a constant != 1 into: alloca [C x Ty], 1
180   if (AI.isArrayAllocation()) {  // Check C != 1
181     if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) {
182       Type *NewTy =
183         ArrayType::get(AI.getAllocatedType(), C->getZExtValue());
184       AllocaInst *New = Builder->CreateAlloca(NewTy, nullptr, AI.getName());
185       New->setAlignment(AI.getAlignment());
186
187       // Scan to the end of the allocation instructions, to skip over a block of
188       // allocas if possible...also skip interleaved debug info
189       //
190       BasicBlock::iterator It = New;
191       while (isa<AllocaInst>(*It) || isa<DbgInfoIntrinsic>(*It)) ++It;
192
193       // Now that I is pointing to the first non-allocation-inst in the block,
194       // insert our getelementptr instruction...
195       //
196       Type *IdxTy = DL
197                   ? DL->getIntPtrType(AI.getType())
198                   : Type::getInt64Ty(AI.getContext());
199       Value *NullIdx = Constant::getNullValue(IdxTy);
200       Value *Idx[2] = { NullIdx, NullIdx };
201       Instruction *GEP =
202         GetElementPtrInst::CreateInBounds(New, Idx, New->getName() + ".sub");
203       InsertNewInstBefore(GEP, *It);
204
205       // Now make everything use the getelementptr instead of the original
206       // allocation.
207       return ReplaceInstUsesWith(AI, GEP);
208     } else if (isa<UndefValue>(AI.getArraySize())) {
209       return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
210     }
211   }
212
213   if (DL && AI.getAllocatedType()->isSized()) {
214     // If the alignment is 0 (unspecified), assign it the preferred alignment.
215     if (AI.getAlignment() == 0)
216       AI.setAlignment(DL->getPrefTypeAlignment(AI.getAllocatedType()));
217
218     // Move all alloca's of zero byte objects to the entry block and merge them
219     // together.  Note that we only do this for alloca's, because malloc should
220     // allocate and return a unique pointer, even for a zero byte allocation.
221     if (DL->getTypeAllocSize(AI.getAllocatedType()) == 0) {
222       // For a zero sized alloca there is no point in doing an array allocation.
223       // This is helpful if the array size is a complicated expression not used
224       // elsewhere.
225       if (AI.isArrayAllocation()) {
226         AI.setOperand(0, ConstantInt::get(AI.getArraySize()->getType(), 1));
227         return &AI;
228       }
229
230       // Get the first instruction in the entry block.
231       BasicBlock &EntryBlock = AI.getParent()->getParent()->getEntryBlock();
232       Instruction *FirstInst = EntryBlock.getFirstNonPHIOrDbg();
233       if (FirstInst != &AI) {
234         // If the entry block doesn't start with a zero-size alloca then move
235         // this one to the start of the entry block.  There is no problem with
236         // dominance as the array size was forced to a constant earlier already.
237         AllocaInst *EntryAI = dyn_cast<AllocaInst>(FirstInst);
238         if (!EntryAI || !EntryAI->getAllocatedType()->isSized() ||
239             DL->getTypeAllocSize(EntryAI->getAllocatedType()) != 0) {
240           AI.moveBefore(FirstInst);
241           return &AI;
242         }
243
244         // If the alignment of the entry block alloca is 0 (unspecified),
245         // assign it the preferred alignment.
246         if (EntryAI->getAlignment() == 0)
247           EntryAI->setAlignment(
248             DL->getPrefTypeAlignment(EntryAI->getAllocatedType()));
249         // Replace this zero-sized alloca with the one at the start of the entry
250         // block after ensuring that the address will be aligned enough for both
251         // types.
252         unsigned MaxAlign = std::max(EntryAI->getAlignment(),
253                                      AI.getAlignment());
254         EntryAI->setAlignment(MaxAlign);
255         if (AI.getType() != EntryAI->getType())
256           return new BitCastInst(EntryAI, AI.getType());
257         return ReplaceInstUsesWith(AI, EntryAI);
258       }
259     }
260   }
261
262   if (AI.getAlignment()) {
263     // Check to see if this allocation is only modified by a memcpy/memmove from
264     // a constant global whose alignment is equal to or exceeds that of the
265     // allocation.  If this is the case, we can change all users to use
266     // the constant global instead.  This is commonly produced by the CFE by
267     // constructs like "void foo() { int A[] = {1,2,3,4,5,6,7,8,9...}; }" if 'A'
268     // is only subsequently read.
269     SmallVector<Instruction *, 4> ToDelete;
270     if (MemTransferInst *Copy = isOnlyCopiedFromConstantGlobal(&AI, ToDelete)) {
271       unsigned SourceAlign = getOrEnforceKnownAlignment(Copy->getSource(),
272                                                         AI.getAlignment(),
273                                                         DL, AT, &AI, DT);
274       if (AI.getAlignment() <= SourceAlign) {
275         DEBUG(dbgs() << "Found alloca equal to global: " << AI << '\n');
276         DEBUG(dbgs() << "  memcpy = " << *Copy << '\n');
277         for (unsigned i = 0, e = ToDelete.size(); i != e; ++i)
278           EraseInstFromFunction(*ToDelete[i]);
279         Constant *TheSrc = cast<Constant>(Copy->getSource());
280         Constant *Cast
281           = ConstantExpr::getPointerBitCastOrAddrSpaceCast(TheSrc, AI.getType());
282         Instruction *NewI = ReplaceInstUsesWith(AI, Cast);
283         EraseInstFromFunction(*Copy);
284         ++NumGlobalCopies;
285         return NewI;
286       }
287     }
288   }
289
290   // At last, use the generic allocation site handler to aggressively remove
291   // unused allocas.
292   return visitAllocSite(AI);
293 }
294
295 /// \brief Helper to combine a load to a new type.
296 ///
297 /// This just does the work of combining a load to a new type. It handles
298 /// metadata, etc., and returns the new instruction. The \c NewTy should be the
299 /// loaded *value* type. This will convert it to a pointer, cast the operand to
300 /// that pointer type, load it, etc.
301 ///
302 /// Note that this will create all of the instructions with whatever insert
303 /// point the \c InstCombiner currently is using.
304 static LoadInst *combineLoadToNewType(InstCombiner &IC, LoadInst &LI, Type *NewTy) {
305   Value *Ptr = LI.getPointerOperand();
306   unsigned AS = LI.getPointerAddressSpace();
307   SmallVector<std::pair<unsigned, MDNode *>, 8> MD;
308   LI.getAllMetadata(MD);
309
310   LoadInst *NewLoad = IC.Builder->CreateAlignedLoad(
311       IC.Builder->CreateBitCast(Ptr, NewTy->getPointerTo(AS)),
312       LI.getAlignment(), LI.getName());
313   for (const auto &MDPair : MD) {
314     unsigned ID = MDPair.first;
315     MDNode *N = MDPair.second;
316     // Note, essentially every kind of metadata should be preserved here! This
317     // routine is supposed to clone a load instruction changing *only its type*.
318     // The only metadata it makes sense to drop is metadata which is invalidated
319     // when the pointer type changes. This should essentially never be the case
320     // in LLVM, but we explicitly switch over only known metadata to be
321     // conservatively correct. If you are adding metadata to LLVM which pertains
322     // to loads, you almost certainly want to add it here.
323     switch (ID) {
324     case LLVMContext::MD_dbg:
325     case LLVMContext::MD_tbaa:
326     case LLVMContext::MD_prof:
327     case LLVMContext::MD_fpmath:
328     case LLVMContext::MD_tbaa_struct:
329     case LLVMContext::MD_invariant_load:
330     case LLVMContext::MD_alias_scope:
331     case LLVMContext::MD_noalias:
332     case LLVMContext::MD_nontemporal:
333     case LLVMContext::MD_mem_parallel_loop_access:
334     case LLVMContext::MD_nonnull:
335       // All of these directly apply.
336       NewLoad->setMetadata(ID, N);
337       break;
338
339     case LLVMContext::MD_range:
340       // FIXME: It would be nice to propagate this in some way, but the type
341       // conversions make it hard.
342       break;
343     }
344   }
345   return NewLoad;
346 }
347
348 /// \brief Combine loads to match the type of value their uses after looking
349 /// through intervening bitcasts.
350 ///
351 /// The core idea here is that if the result of a load is used in an operation,
352 /// we should load the type most conducive to that operation. For example, when
353 /// loading an integer and converting that immediately to a pointer, we should
354 /// instead directly load a pointer.
355 ///
356 /// However, this routine must never change the width of a load or the number of
357 /// loads as that would introduce a semantic change. This combine is expected to
358 /// be a semantic no-op which just allows loads to more closely model the types
359 /// of their consuming operations.
360 ///
361 /// Currently, we also refuse to change the precise type used for an atomic load
362 /// or a volatile load. This is debatable, and might be reasonable to change
363 /// later. However, it is risky in case some backend or other part of LLVM is
364 /// relying on the exact type loaded to select appropriate atomic operations.
365 static Instruction *combineLoadToOperationType(InstCombiner &IC, LoadInst &LI) {
366   // FIXME: We could probably with some care handle both volatile and atomic
367   // loads here but it isn't clear that this is important.
368   if (!LI.isSimple())
369     return nullptr;
370
371   if (LI.use_empty())
372     return nullptr;
373
374
375   // Fold away bit casts of the loaded value by loading the desired type.
376   if (LI.hasOneUse())
377     if (auto *BC = dyn_cast<BitCastInst>(LI.user_back())) {
378       LoadInst *NewLoad = combineLoadToNewType(IC, LI, BC->getDestTy());
379       BC->replaceAllUsesWith(NewLoad);
380       IC.EraseInstFromFunction(*BC);
381       return &LI;
382     }
383
384   // FIXME: We should also canonicalize loads of vectors when their elements are
385   // cast to other types.
386   return nullptr;
387 }
388
389 Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
390   Value *Op = LI.getOperand(0);
391
392   // Try to canonicalize the loaded type.
393   if (Instruction *Res = combineLoadToOperationType(*this, LI))
394     return Res;
395
396   // Attempt to improve the alignment.
397   if (DL) {
398     unsigned KnownAlign =
399       getOrEnforceKnownAlignment(Op, DL->getPrefTypeAlignment(LI.getType()),
400                                  DL, AT, &LI, DT);
401     unsigned LoadAlign = LI.getAlignment();
402     unsigned EffectiveLoadAlign = LoadAlign != 0 ? LoadAlign :
403       DL->getABITypeAlignment(LI.getType());
404
405     if (KnownAlign > EffectiveLoadAlign)
406       LI.setAlignment(KnownAlign);
407     else if (LoadAlign == 0)
408       LI.setAlignment(EffectiveLoadAlign);
409   }
410
411   // None of the following transforms are legal for volatile/atomic loads.
412   // FIXME: Some of it is okay for atomic loads; needs refactoring.
413   if (!LI.isSimple()) return nullptr;
414
415   // Do really simple store-to-load forwarding and load CSE, to catch cases
416   // where there are several consecutive memory accesses to the same location,
417   // separated by a few arithmetic operations.
418   BasicBlock::iterator BBI = &LI;
419   if (Value *AvailableVal = FindAvailableLoadedValue(Op, LI.getParent(), BBI,6))
420     return ReplaceInstUsesWith(
421         LI, Builder->CreateBitOrPointerCast(AvailableVal, LI.getType(),
422                                             LI.getName() + ".cast"));
423
424   // load(gep null, ...) -> unreachable
425   if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
426     const Value *GEPI0 = GEPI->getOperand(0);
427     // TODO: Consider a target hook for valid address spaces for this xform.
428     if (isa<ConstantPointerNull>(GEPI0) && GEPI->getPointerAddressSpace() == 0){
429       // Insert a new store to null instruction before the load to indicate
430       // that this code is not reachable.  We do this instead of inserting
431       // an unreachable instruction directly because we cannot modify the
432       // CFG.
433       new StoreInst(UndefValue::get(LI.getType()),
434                     Constant::getNullValue(Op->getType()), &LI);
435       return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
436     }
437   }
438
439   // load null/undef -> unreachable
440   // TODO: Consider a target hook for valid address spaces for this xform.
441   if (isa<UndefValue>(Op) ||
442       (isa<ConstantPointerNull>(Op) && LI.getPointerAddressSpace() == 0)) {
443     // Insert a new store to null instruction before the load to indicate that
444     // this code is not reachable.  We do this instead of inserting an
445     // unreachable instruction directly because we cannot modify the CFG.
446     new StoreInst(UndefValue::get(LI.getType()),
447                   Constant::getNullValue(Op->getType()), &LI);
448     return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
449   }
450
451   if (Op->hasOneUse()) {
452     // Change select and PHI nodes to select values instead of addresses: this
453     // helps alias analysis out a lot, allows many others simplifications, and
454     // exposes redundancy in the code.
455     //
456     // Note that we cannot do the transformation unless we know that the
457     // introduced loads cannot trap!  Something like this is valid as long as
458     // the condition is always false: load (select bool %C, int* null, int* %G),
459     // but it would not be valid if we transformed it to load from null
460     // unconditionally.
461     //
462     if (SelectInst *SI = dyn_cast<SelectInst>(Op)) {
463       // load (select (Cond, &V1, &V2))  --> select(Cond, load &V1, load &V2).
464       unsigned Align = LI.getAlignment();
465       if (isSafeToLoadUnconditionally(SI->getOperand(1), SI, Align, DL) &&
466           isSafeToLoadUnconditionally(SI->getOperand(2), SI, Align, DL)) {
467         LoadInst *V1 = Builder->CreateLoad(SI->getOperand(1),
468                                            SI->getOperand(1)->getName()+".val");
469         LoadInst *V2 = Builder->CreateLoad(SI->getOperand(2),
470                                            SI->getOperand(2)->getName()+".val");
471         V1->setAlignment(Align);
472         V2->setAlignment(Align);
473         return SelectInst::Create(SI->getCondition(), V1, V2);
474       }
475
476       // load (select (cond, null, P)) -> load P
477       if (Constant *C = dyn_cast<Constant>(SI->getOperand(1)))
478         if (C->isNullValue()) {
479           LI.setOperand(0, SI->getOperand(2));
480           return &LI;
481         }
482
483       // load (select (cond, P, null)) -> load P
484       if (Constant *C = dyn_cast<Constant>(SI->getOperand(2)))
485         if (C->isNullValue()) {
486           LI.setOperand(0, SI->getOperand(1));
487           return &LI;
488         }
489     }
490   }
491   return nullptr;
492 }
493
494 /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P
495 /// when possible.  This makes it generally easy to do alias analysis and/or
496 /// SROA/mem2reg of the memory object.
497 static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) {
498   User *CI = cast<User>(SI.getOperand(1));
499   Value *CastOp = CI->getOperand(0);
500
501   Type *DestPTy = CI->getType()->getPointerElementType();
502   PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType());
503   if (!SrcTy) return nullptr;
504
505   Type *SrcPTy = SrcTy->getElementType();
506
507   if (!DestPTy->isIntegerTy() && !DestPTy->isPointerTy())
508     return nullptr;
509
510   /// NewGEPIndices - If SrcPTy is an aggregate type, we can emit a "noop gep"
511   /// to its first element.  This allows us to handle things like:
512   ///   store i32 xxx, (bitcast {foo*, float}* %P to i32*)
513   /// on 32-bit hosts.
514   SmallVector<Value*, 4> NewGEPIndices;
515
516   // If the source is an array, the code below will not succeed.  Check to
517   // see if a trivial 'gep P, 0, 0' will help matters.  Only do this for
518   // constants.
519   if (SrcPTy->isArrayTy() || SrcPTy->isStructTy()) {
520     // Index through pointer.
521     Constant *Zero = Constant::getNullValue(Type::getInt32Ty(SI.getContext()));
522     NewGEPIndices.push_back(Zero);
523
524     while (1) {
525       if (StructType *STy = dyn_cast<StructType>(SrcPTy)) {
526         if (!STy->getNumElements()) /* Struct can be empty {} */
527           break;
528         NewGEPIndices.push_back(Zero);
529         SrcPTy = STy->getElementType(0);
530       } else if (ArrayType *ATy = dyn_cast<ArrayType>(SrcPTy)) {
531         NewGEPIndices.push_back(Zero);
532         SrcPTy = ATy->getElementType();
533       } else {
534         break;
535       }
536     }
537
538     SrcTy = PointerType::get(SrcPTy, SrcTy->getAddressSpace());
539   }
540
541   if (!SrcPTy->isIntegerTy() && !SrcPTy->isPointerTy())
542     return nullptr;
543
544   // If the pointers point into different address spaces don't do the
545   // transformation.
546   if (SrcTy->getAddressSpace() != CI->getType()->getPointerAddressSpace())
547     return nullptr;
548
549   // If the pointers point to values of different sizes don't do the
550   // transformation.
551   if (!IC.getDataLayout() ||
552       IC.getDataLayout()->getTypeSizeInBits(SrcPTy) !=
553       IC.getDataLayout()->getTypeSizeInBits(DestPTy))
554     return nullptr;
555
556   // If the pointers point to pointers to different address spaces don't do the
557   // transformation. It is not safe to introduce an addrspacecast instruction in
558   // this case since, depending on the target, addrspacecast may not be a no-op
559   // cast.
560   if (SrcPTy->isPointerTy() && DestPTy->isPointerTy() &&
561       SrcPTy->getPointerAddressSpace() != DestPTy->getPointerAddressSpace())
562     return nullptr;
563
564   // Okay, we are casting from one integer or pointer type to another of
565   // the same size.  Instead of casting the pointer before
566   // the store, cast the value to be stored.
567   Value *NewCast;
568   Instruction::CastOps opcode = Instruction::BitCast;
569   Type* CastSrcTy = DestPTy;
570   Type* CastDstTy = SrcPTy;
571   if (CastDstTy->isPointerTy()) {
572     if (CastSrcTy->isIntegerTy())
573       opcode = Instruction::IntToPtr;
574   } else if (CastDstTy->isIntegerTy()) {
575     if (CastSrcTy->isPointerTy())
576       opcode = Instruction::PtrToInt;
577   }
578
579   // SIOp0 is a pointer to aggregate and this is a store to the first field,
580   // emit a GEP to index into its first field.
581   if (!NewGEPIndices.empty())
582     CastOp = IC.Builder->CreateInBoundsGEP(CastOp, NewGEPIndices);
583
584   Value *SIOp0 = SI.getOperand(0);
585   NewCast = IC.Builder->CreateCast(opcode, SIOp0, CastDstTy,
586                                    SIOp0->getName()+".c");
587   SI.setOperand(0, NewCast);
588   SI.setOperand(1, CastOp);
589   return &SI;
590 }
591
592 /// equivalentAddressValues - Test if A and B will obviously have the same
593 /// value. This includes recognizing that %t0 and %t1 will have the same
594 /// value in code like this:
595 ///   %t0 = getelementptr \@a, 0, 3
596 ///   store i32 0, i32* %t0
597 ///   %t1 = getelementptr \@a, 0, 3
598 ///   %t2 = load i32* %t1
599 ///
600 static bool equivalentAddressValues(Value *A, Value *B) {
601   // Test if the values are trivially equivalent.
602   if (A == B) return true;
603
604   // Test if the values come form identical arithmetic instructions.
605   // This uses isIdenticalToWhenDefined instead of isIdenticalTo because
606   // its only used to compare two uses within the same basic block, which
607   // means that they'll always either have the same value or one of them
608   // will have an undefined value.
609   if (isa<BinaryOperator>(A) ||
610       isa<CastInst>(A) ||
611       isa<PHINode>(A) ||
612       isa<GetElementPtrInst>(A))
613     if (Instruction *BI = dyn_cast<Instruction>(B))
614       if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
615         return true;
616
617   // Otherwise they may not be equivalent.
618   return false;
619 }
620
621 Instruction *InstCombiner::visitStoreInst(StoreInst &SI) {
622   Value *Val = SI.getOperand(0);
623   Value *Ptr = SI.getOperand(1);
624
625   // Attempt to improve the alignment.
626   if (DL) {
627     unsigned KnownAlign =
628       getOrEnforceKnownAlignment(Ptr, DL->getPrefTypeAlignment(Val->getType()),
629                                  DL, AT, &SI, DT);
630     unsigned StoreAlign = SI.getAlignment();
631     unsigned EffectiveStoreAlign = StoreAlign != 0 ? StoreAlign :
632       DL->getABITypeAlignment(Val->getType());
633
634     if (KnownAlign > EffectiveStoreAlign)
635       SI.setAlignment(KnownAlign);
636     else if (StoreAlign == 0)
637       SI.setAlignment(EffectiveStoreAlign);
638   }
639
640   // Don't hack volatile/atomic stores.
641   // FIXME: Some bits are legal for atomic stores; needs refactoring.
642   if (!SI.isSimple()) return nullptr;
643
644   // If the RHS is an alloca with a single use, zapify the store, making the
645   // alloca dead.
646   if (Ptr->hasOneUse()) {
647     if (isa<AllocaInst>(Ptr))
648       return EraseInstFromFunction(SI);
649     if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
650       if (isa<AllocaInst>(GEP->getOperand(0))) {
651         if (GEP->getOperand(0)->hasOneUse())
652           return EraseInstFromFunction(SI);
653       }
654     }
655   }
656
657   // Do really simple DSE, to catch cases where there are several consecutive
658   // stores to the same location, separated by a few arithmetic operations. This
659   // situation often occurs with bitfield accesses.
660   BasicBlock::iterator BBI = &SI;
661   for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts;
662        --ScanInsts) {
663     --BBI;
664     // Don't count debug info directives, lest they affect codegen,
665     // and we skip pointer-to-pointer bitcasts, which are NOPs.
666     if (isa<DbgInfoIntrinsic>(BBI) ||
667         (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) {
668       ScanInsts++;
669       continue;
670     }
671
672     if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) {
673       // Prev store isn't volatile, and stores to the same location?
674       if (PrevSI->isSimple() && equivalentAddressValues(PrevSI->getOperand(1),
675                                                         SI.getOperand(1))) {
676         ++NumDeadStore;
677         ++BBI;
678         EraseInstFromFunction(*PrevSI);
679         continue;
680       }
681       break;
682     }
683
684     // If this is a load, we have to stop.  However, if the loaded value is from
685     // the pointer we're loading and is producing the pointer we're storing,
686     // then *this* store is dead (X = load P; store X -> P).
687     if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
688       if (LI == Val && equivalentAddressValues(LI->getOperand(0), Ptr) &&
689           LI->isSimple())
690         return EraseInstFromFunction(SI);
691
692       // Otherwise, this is a load from some other location.  Stores before it
693       // may not be dead.
694       break;
695     }
696
697     // Don't skip over loads or things that can modify memory.
698     if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory())
699       break;
700   }
701
702   // store X, null    -> turns into 'unreachable' in SimplifyCFG
703   if (isa<ConstantPointerNull>(Ptr) && SI.getPointerAddressSpace() == 0) {
704     if (!isa<UndefValue>(Val)) {
705       SI.setOperand(0, UndefValue::get(Val->getType()));
706       if (Instruction *U = dyn_cast<Instruction>(Val))
707         Worklist.Add(U);  // Dropped a use.
708     }
709     return nullptr;  // Do not modify these!
710   }
711
712   // store undef, Ptr -> noop
713   if (isa<UndefValue>(Val))
714     return EraseInstFromFunction(SI);
715
716   // If the pointer destination is a cast, see if we can fold the cast into the
717   // source instead.
718   if (isa<CastInst>(Ptr))
719     if (Instruction *Res = InstCombineStoreToCast(*this, SI))
720       return Res;
721   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
722     if (CE->isCast())
723       if (Instruction *Res = InstCombineStoreToCast(*this, SI))
724         return Res;
725
726
727   // If this store is the last instruction in the basic block (possibly
728   // excepting debug info instructions), and if the block ends with an
729   // unconditional branch, try to move it to the successor block.
730   BBI = &SI;
731   do {
732     ++BBI;
733   } while (isa<DbgInfoIntrinsic>(BBI) ||
734            (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy()));
735   if (BranchInst *BI = dyn_cast<BranchInst>(BBI))
736     if (BI->isUnconditional())
737       if (SimplifyStoreAtEndOfBlock(SI))
738         return nullptr;  // xform done!
739
740   return nullptr;
741 }
742
743 /// SimplifyStoreAtEndOfBlock - Turn things like:
744 ///   if () { *P = v1; } else { *P = v2 }
745 /// into a phi node with a store in the successor.
746 ///
747 /// Simplify things like:
748 ///   *P = v1; if () { *P = v2; }
749 /// into a phi node with a store in the successor.
750 ///
751 bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) {
752   BasicBlock *StoreBB = SI.getParent();
753
754   // Check to see if the successor block has exactly two incoming edges.  If
755   // so, see if the other predecessor contains a store to the same location.
756   // if so, insert a PHI node (if needed) and move the stores down.
757   BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0);
758
759   // Determine whether Dest has exactly two predecessors and, if so, compute
760   // the other predecessor.
761   pred_iterator PI = pred_begin(DestBB);
762   BasicBlock *P = *PI;
763   BasicBlock *OtherBB = nullptr;
764
765   if (P != StoreBB)
766     OtherBB = P;
767
768   if (++PI == pred_end(DestBB))
769     return false;
770
771   P = *PI;
772   if (P != StoreBB) {
773     if (OtherBB)
774       return false;
775     OtherBB = P;
776   }
777   if (++PI != pred_end(DestBB))
778     return false;
779
780   // Bail out if all the relevant blocks aren't distinct (this can happen,
781   // for example, if SI is in an infinite loop)
782   if (StoreBB == DestBB || OtherBB == DestBB)
783     return false;
784
785   // Verify that the other block ends in a branch and is not otherwise empty.
786   BasicBlock::iterator BBI = OtherBB->getTerminator();
787   BranchInst *OtherBr = dyn_cast<BranchInst>(BBI);
788   if (!OtherBr || BBI == OtherBB->begin())
789     return false;
790
791   // If the other block ends in an unconditional branch, check for the 'if then
792   // else' case.  there is an instruction before the branch.
793   StoreInst *OtherStore = nullptr;
794   if (OtherBr->isUnconditional()) {
795     --BBI;
796     // Skip over debugging info.
797     while (isa<DbgInfoIntrinsic>(BBI) ||
798            (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) {
799       if (BBI==OtherBB->begin())
800         return false;
801       --BBI;
802     }
803     // If this isn't a store, isn't a store to the same location, or is not the
804     // right kind of store, bail out.
805     OtherStore = dyn_cast<StoreInst>(BBI);
806     if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1) ||
807         !SI.isSameOperationAs(OtherStore))
808       return false;
809   } else {
810     // Otherwise, the other block ended with a conditional branch. If one of the
811     // destinations is StoreBB, then we have the if/then case.
812     if (OtherBr->getSuccessor(0) != StoreBB &&
813         OtherBr->getSuccessor(1) != StoreBB)
814       return false;
815
816     // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an
817     // if/then triangle.  See if there is a store to the same ptr as SI that
818     // lives in OtherBB.
819     for (;; --BBI) {
820       // Check to see if we find the matching store.
821       if ((OtherStore = dyn_cast<StoreInst>(BBI))) {
822         if (OtherStore->getOperand(1) != SI.getOperand(1) ||
823             !SI.isSameOperationAs(OtherStore))
824           return false;
825         break;
826       }
827       // If we find something that may be using or overwriting the stored
828       // value, or if we run out of instructions, we can't do the xform.
829       if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() ||
830           BBI == OtherBB->begin())
831         return false;
832     }
833
834     // In order to eliminate the store in OtherBr, we have to
835     // make sure nothing reads or overwrites the stored value in
836     // StoreBB.
837     for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) {
838       // FIXME: This should really be AA driven.
839       if (I->mayReadFromMemory() || I->mayWriteToMemory())
840         return false;
841     }
842   }
843
844   // Insert a PHI node now if we need it.
845   Value *MergedVal = OtherStore->getOperand(0);
846   if (MergedVal != SI.getOperand(0)) {
847     PHINode *PN = PHINode::Create(MergedVal->getType(), 2, "storemerge");
848     PN->addIncoming(SI.getOperand(0), SI.getParent());
849     PN->addIncoming(OtherStore->getOperand(0), OtherBB);
850     MergedVal = InsertNewInstBefore(PN, DestBB->front());
851   }
852
853   // Advance to a place where it is safe to insert the new store and
854   // insert it.
855   BBI = DestBB->getFirstInsertionPt();
856   StoreInst *NewSI = new StoreInst(MergedVal, SI.getOperand(1),
857                                    SI.isVolatile(),
858                                    SI.getAlignment(),
859                                    SI.getOrdering(),
860                                    SI.getSynchScope());
861   InsertNewInstBefore(NewSI, *BBI);
862   NewSI->setDebugLoc(OtherStore->getDebugLoc());
863
864   // If the two stores had AA tags, merge them.
865   AAMDNodes AATags;
866   SI.getAAMetadata(AATags);
867   if (AATags) {
868     OtherStore->getAAMetadata(AATags, /* Merge = */ true);
869     NewSI->setAAMetadata(AATags);
870   }
871
872   // Nuke the old stores.
873   EraseInstFromFunction(SI);
874   EraseInstFromFunction(*OtherStore);
875   return true;
876 }