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