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 | |
Chandler Carruth | a917458 | 2015-01-22 05:25:13 +0000 | [diff] [blame] | 14 | #include "InstCombineInternal.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" |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 18 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/IntrinsicInst.h" |
Charles Davis | 33d1dc0 | 2015-02-25 05:10:25 +0000 | [diff] [blame] | 20 | #include "llvm/IR/MDBuilder.h" |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 22 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 25 | #define DEBUG_TYPE "instcombine" |
| 26 | |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 27 | STATISTIC(NumDeadStore, "Number of dead stores eliminated"); |
| 28 | STATISTIC(NumGlobalCopies, "Number of allocas copied from constant global"); |
| 29 | |
| 30 | /// pointsToConstantGlobal - Return true if V (possibly indirectly) points to |
| 31 | /// some part of a constant global variable. This intentionally only accepts |
| 32 | /// constant expressions because we can't rewrite arbitrary instructions. |
| 33 | static bool pointsToConstantGlobal(Value *V) { |
| 34 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 35 | return GV->isConstant(); |
Matt Arsenault | 60728177 | 2014-04-24 00:01:09 +0000 | [diff] [blame] | 36 | |
| 37 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 38 | if (CE->getOpcode() == Instruction::BitCast || |
Matt Arsenault | 60728177 | 2014-04-24 00:01:09 +0000 | [diff] [blame] | 39 | CE->getOpcode() == Instruction::AddrSpaceCast || |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 40 | CE->getOpcode() == Instruction::GetElementPtr) |
| 41 | return pointsToConstantGlobal(CE->getOperand(0)); |
Matt Arsenault | 60728177 | 2014-04-24 00:01:09 +0000 | [diff] [blame] | 42 | } |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 43 | return false; |
| 44 | } |
| 45 | |
| 46 | /// isOnlyCopiedFromConstantGlobal - Recursively walk the uses of a (derived) |
| 47 | /// pointer to an alloca. Ignore any reads of the pointer, return false if we |
| 48 | /// see any stores or other unknown uses. If we see pointer arithmetic, keep |
| 49 | /// track of whether it moves the pointer (with IsOffset) but otherwise traverse |
| 50 | /// the uses. If we see a memcpy/memmove that targets an unoffseted pointer to |
| 51 | /// the alloca, and if the source pointer is a pointer to a constant global, we |
| 52 | /// can optimize this. |
| 53 | static bool |
| 54 | isOnlyCopiedFromConstantGlobal(Value *V, MemTransferInst *&TheCopy, |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 55 | SmallVectorImpl<Instruction *> &ToDelete) { |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 56 | // We track lifetime intrinsics as we encounter them. If we decide to go |
| 57 | // ahead and replace the value with the global, this lets the caller quickly |
| 58 | // eliminate the markers. |
| 59 | |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 60 | SmallVector<std::pair<Value *, bool>, 35> ValuesToInspect; |
| 61 | ValuesToInspect.push_back(std::make_pair(V, false)); |
| 62 | while (!ValuesToInspect.empty()) { |
| 63 | auto ValuePair = ValuesToInspect.pop_back_val(); |
| 64 | const bool IsOffset = ValuePair.second; |
| 65 | for (auto &U : ValuePair.first->uses()) { |
| 66 | Instruction *I = cast<Instruction>(U.getUser()); |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 67 | |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 68 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 69 | // Ignore non-volatile loads, they are always ok. |
| 70 | if (!LI->isSimple()) return false; |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 71 | continue; |
| 72 | } |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 73 | |
| 74 | if (isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I)) { |
| 75 | // If uses of the bitcast are ok, we are ok. |
| 76 | ValuesToInspect.push_back(std::make_pair(I, IsOffset)); |
| 77 | continue; |
| 78 | } |
| 79 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) { |
| 80 | // If the GEP has all zero indices, it doesn't offset the pointer. If it |
| 81 | // doesn't, it does. |
| 82 | ValuesToInspect.push_back( |
| 83 | std::make_pair(I, IsOffset || !GEP->hasAllZeroIndices())); |
| 84 | continue; |
| 85 | } |
| 86 | |
Benjamin Kramer | 3a09ef6 | 2015-04-10 14:50:08 +0000 | [diff] [blame] | 87 | if (auto CS = CallSite(I)) { |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 88 | // If this is the function being called then we treat it like a load and |
| 89 | // ignore it. |
| 90 | if (CS.isCallee(&U)) |
| 91 | continue; |
| 92 | |
| 93 | // Inalloca arguments are clobbered by the call. |
| 94 | unsigned ArgNo = CS.getArgumentNo(&U); |
| 95 | if (CS.isInAllocaArgument(ArgNo)) |
| 96 | return false; |
| 97 | |
| 98 | // If this is a readonly/readnone call site, then we know it is just a |
| 99 | // load (but one that potentially returns the value itself), so we can |
| 100 | // ignore it if we know that the value isn't captured. |
| 101 | if (CS.onlyReadsMemory() && |
| 102 | (CS.getInstruction()->use_empty() || CS.doesNotCapture(ArgNo))) |
| 103 | continue; |
| 104 | |
| 105 | // If this is being passed as a byval argument, the caller is making a |
| 106 | // copy, so it is only a read of the alloca. |
| 107 | if (CS.isByValArgument(ArgNo)) |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | // Lifetime intrinsics can be handled by the caller. |
| 112 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 113 | if (II->getIntrinsicID() == Intrinsic::lifetime_start || |
| 114 | II->getIntrinsicID() == Intrinsic::lifetime_end) { |
| 115 | assert(II->use_empty() && "Lifetime markers have no result to use!"); |
| 116 | ToDelete.push_back(II); |
| 117 | continue; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // If this is isn't our memcpy/memmove, reject it as something we can't |
| 122 | // handle. |
| 123 | MemTransferInst *MI = dyn_cast<MemTransferInst>(I); |
| 124 | if (!MI) |
| 125 | return false; |
| 126 | |
| 127 | // If the transfer is using the alloca as a source of the transfer, then |
| 128 | // ignore it since it is a load (unless the transfer is volatile). |
| 129 | if (U.getOperandNo() == 1) { |
| 130 | if (MI->isVolatile()) return false; |
| 131 | continue; |
| 132 | } |
| 133 | |
| 134 | // If we already have seen a copy, reject the second one. |
| 135 | if (TheCopy) return false; |
| 136 | |
| 137 | // If the pointer has been offset from the start of the alloca, we can't |
| 138 | // safely handle this. |
| 139 | if (IsOffset) return false; |
| 140 | |
| 141 | // If the memintrinsic isn't using the alloca as the dest, reject it. |
| 142 | if (U.getOperandNo() != 0) return false; |
| 143 | |
| 144 | // If the source of the memcpy/move is not a constant global, reject it. |
| 145 | if (!pointsToConstantGlobal(MI->getSource())) |
| 146 | return false; |
| 147 | |
| 148 | // Otherwise, the transform is safe. Remember the copy instruction. |
| 149 | TheCopy = MI; |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 150 | } |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 151 | } |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | /// isOnlyCopiedFromConstantGlobal - Return true if the specified alloca is only |
| 156 | /// modified by a copy from a constant global. If we can prove this, we can |
| 157 | /// replace any uses of the alloca with uses of the global directly. |
| 158 | static MemTransferInst * |
| 159 | isOnlyCopiedFromConstantGlobal(AllocaInst *AI, |
| 160 | SmallVectorImpl<Instruction *> &ToDelete) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 161 | MemTransferInst *TheCopy = nullptr; |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 162 | if (isOnlyCopiedFromConstantGlobal(AI, TheCopy, ToDelete)) |
| 163 | return TheCopy; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 164 | return nullptr; |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Duncan P. N. Exon Smith | c6820ec | 2015-03-13 19:22:03 +0000 | [diff] [blame] | 167 | static Instruction *simplifyAllocaArraySize(InstCombiner &IC, AllocaInst &AI) { |
Duncan P. N. Exon Smith | 720762e | 2015-03-13 19:30:44 +0000 | [diff] [blame] | 168 | // Check for array size of 1 (scalar allocation). |
Duncan P. N. Exon Smith | be95b4a | 2015-03-13 19:42:09 +0000 | [diff] [blame] | 169 | if (!AI.isArrayAllocation()) { |
| 170 | // i32 1 is the canonical array size for scalar allocations. |
| 171 | if (AI.getArraySize()->getType()->isIntegerTy(32)) |
| 172 | return nullptr; |
| 173 | |
| 174 | // Canonicalize it. |
| 175 | Value *V = IC.Builder->getInt32(1); |
| 176 | AI.setOperand(0, V); |
| 177 | return &AI; |
| 178 | } |
Duncan P. N. Exon Smith | 720762e | 2015-03-13 19:30:44 +0000 | [diff] [blame] | 179 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 180 | // Convert: alloca Ty, C - where C is a constant != 1 into: alloca [C x Ty], 1 |
Duncan P. N. Exon Smith | bb73013 | 2015-03-13 19:26:33 +0000 | [diff] [blame] | 181 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
| 182 | Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); |
| 183 | AllocaInst *New = IC.Builder->CreateAlloca(NewTy, nullptr, AI.getName()); |
| 184 | New->setAlignment(AI.getAlignment()); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 185 | |
Duncan P. N. Exon Smith | bb73013 | 2015-03-13 19:26:33 +0000 | [diff] [blame] | 186 | // Scan to the end of the allocation instructions, to skip over a block of |
| 187 | // allocas if possible...also skip interleaved debug info |
| 188 | // |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 189 | BasicBlock::iterator It(New); |
Duncan P. N. Exon Smith | bb73013 | 2015-03-13 19:26:33 +0000 | [diff] [blame] | 190 | while (isa<AllocaInst>(*It) || isa<DbgInfoIntrinsic>(*It)) |
| 191 | ++It; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 192 | |
Duncan P. N. Exon Smith | bb73013 | 2015-03-13 19:26:33 +0000 | [diff] [blame] | 193 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 194 | // insert our getelementptr instruction... |
| 195 | // |
| 196 | Type *IdxTy = IC.getDataLayout().getIntPtrType(AI.getType()); |
| 197 | Value *NullIdx = Constant::getNullValue(IdxTy); |
| 198 | Value *Idx[2] = {NullIdx, NullIdx}; |
| 199 | Instruction *GEP = |
Matt Arsenault | 640ff9d | 2013-08-14 00:24:05 +0000 | [diff] [blame] | 200 | GetElementPtrInst::CreateInBounds(New, Idx, New->getName() + ".sub"); |
Duncan P. N. Exon Smith | bb73013 | 2015-03-13 19:26:33 +0000 | [diff] [blame] | 201 | IC.InsertNewInstBefore(GEP, *It); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 202 | |
Duncan P. N. Exon Smith | bb73013 | 2015-03-13 19:26:33 +0000 | [diff] [blame] | 203 | // Now make everything use the getelementptr instead of the original |
| 204 | // allocation. |
| 205 | return IC.ReplaceInstUsesWith(AI, GEP); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Duncan P. N. Exon Smith | bb73013 | 2015-03-13 19:26:33 +0000 | [diff] [blame] | 208 | if (isa<UndefValue>(AI.getArraySize())) |
| 209 | return IC.ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 210 | |
Duncan P. N. Exon Smith | 07ff9b0 | 2015-03-13 19:34:55 +0000 | [diff] [blame] | 211 | // Ensure that the alloca array size argument has type intptr_t, so that |
| 212 | // any casting is exposed early. |
| 213 | Type *IntPtrTy = IC.getDataLayout().getIntPtrType(AI.getType()); |
| 214 | if (AI.getArraySize()->getType() != IntPtrTy) { |
| 215 | Value *V = IC.Builder->CreateIntCast(AI.getArraySize(), IntPtrTy, false); |
| 216 | AI.setOperand(0, V); |
| 217 | return &AI; |
| 218 | } |
| 219 | |
Duncan P. N. Exon Smith | c6820ec | 2015-03-13 19:22:03 +0000 | [diff] [blame] | 220 | return nullptr; |
| 221 | } |
| 222 | |
| 223 | Instruction *InstCombiner::visitAllocaInst(AllocaInst &AI) { |
| 224 | if (auto *I = simplifyAllocaArraySize(*this, AI)) |
| 225 | return I; |
| 226 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 227 | if (AI.getAllocatedType()->isSized()) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 228 | // If the alignment is 0 (unspecified), assign it the preferred alignment. |
| 229 | if (AI.getAlignment() == 0) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 230 | AI.setAlignment(DL.getPrefTypeAlignment(AI.getAllocatedType())); |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 231 | |
| 232 | // Move all alloca's of zero byte objects to the entry block and merge them |
| 233 | // together. Note that we only do this for alloca's, because malloc should |
| 234 | // allocate and return a unique pointer, even for a zero byte allocation. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 235 | if (DL.getTypeAllocSize(AI.getAllocatedType()) == 0) { |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 236 | // For a zero sized alloca there is no point in doing an array allocation. |
| 237 | // This is helpful if the array size is a complicated expression not used |
| 238 | // elsewhere. |
| 239 | if (AI.isArrayAllocation()) { |
| 240 | AI.setOperand(0, ConstantInt::get(AI.getArraySize()->getType(), 1)); |
| 241 | return &AI; |
| 242 | } |
| 243 | |
| 244 | // Get the first instruction in the entry block. |
| 245 | BasicBlock &EntryBlock = AI.getParent()->getParent()->getEntryBlock(); |
| 246 | Instruction *FirstInst = EntryBlock.getFirstNonPHIOrDbg(); |
| 247 | if (FirstInst != &AI) { |
| 248 | // If the entry block doesn't start with a zero-size alloca then move |
| 249 | // this one to the start of the entry block. There is no problem with |
| 250 | // dominance as the array size was forced to a constant earlier already. |
| 251 | AllocaInst *EntryAI = dyn_cast<AllocaInst>(FirstInst); |
| 252 | if (!EntryAI || !EntryAI->getAllocatedType()->isSized() || |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 253 | DL.getTypeAllocSize(EntryAI->getAllocatedType()) != 0) { |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 254 | AI.moveBefore(FirstInst); |
| 255 | return &AI; |
| 256 | } |
| 257 | |
Richard Osborne | b68053e | 2012-09-18 09:31:44 +0000 | [diff] [blame] | 258 | // If the alignment of the entry block alloca is 0 (unspecified), |
| 259 | // assign it the preferred alignment. |
| 260 | if (EntryAI->getAlignment() == 0) |
| 261 | EntryAI->setAlignment( |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 262 | DL.getPrefTypeAlignment(EntryAI->getAllocatedType())); |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 263 | // Replace this zero-sized alloca with the one at the start of the entry |
| 264 | // block after ensuring that the address will be aligned enough for both |
| 265 | // types. |
Richard Osborne | b68053e | 2012-09-18 09:31:44 +0000 | [diff] [blame] | 266 | unsigned MaxAlign = std::max(EntryAI->getAlignment(), |
| 267 | AI.getAlignment()); |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 268 | EntryAI->setAlignment(MaxAlign); |
| 269 | if (AI.getType() != EntryAI->getType()) |
| 270 | return new BitCastInst(EntryAI, AI.getType()); |
| 271 | return ReplaceInstUsesWith(AI, EntryAI); |
| 272 | } |
| 273 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Eli Friedman | b14873c | 2012-11-26 23:04:53 +0000 | [diff] [blame] | 276 | if (AI.getAlignment()) { |
Richard Osborne | 2fd29bf | 2012-09-24 17:10:03 +0000 | [diff] [blame] | 277 | // Check to see if this allocation is only modified by a memcpy/memmove from |
| 278 | // a constant global whose alignment is equal to or exceeds that of the |
| 279 | // allocation. If this is the case, we can change all users to use |
| 280 | // the constant global instead. This is commonly produced by the CFE by |
| 281 | // constructs like "void foo() { int A[] = {1,2,3,4,5,6,7,8,9...}; }" if 'A' |
| 282 | // is only subsequently read. |
| 283 | SmallVector<Instruction *, 4> ToDelete; |
| 284 | if (MemTransferInst *Copy = isOnlyCopiedFromConstantGlobal(&AI, ToDelete)) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 285 | unsigned SourceAlign = getOrEnforceKnownAlignment( |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 286 | Copy->getSource(), AI.getAlignment(), DL, &AI, AC, DT); |
Eli Friedman | b14873c | 2012-11-26 23:04:53 +0000 | [diff] [blame] | 287 | if (AI.getAlignment() <= SourceAlign) { |
Richard Osborne | 2fd29bf | 2012-09-24 17:10:03 +0000 | [diff] [blame] | 288 | DEBUG(dbgs() << "Found alloca equal to global: " << AI << '\n'); |
| 289 | DEBUG(dbgs() << " memcpy = " << *Copy << '\n'); |
| 290 | for (unsigned i = 0, e = ToDelete.size(); i != e; ++i) |
| 291 | EraseInstFromFunction(*ToDelete[i]); |
| 292 | Constant *TheSrc = cast<Constant>(Copy->getSource()); |
Matt Arsenault | bbf18c6 | 2013-12-07 02:58:45 +0000 | [diff] [blame] | 293 | Constant *Cast |
| 294 | = ConstantExpr::getPointerBitCastOrAddrSpaceCast(TheSrc, AI.getType()); |
| 295 | Instruction *NewI = ReplaceInstUsesWith(AI, Cast); |
Richard Osborne | 2fd29bf | 2012-09-24 17:10:03 +0000 | [diff] [blame] | 296 | EraseInstFromFunction(*Copy); |
| 297 | ++NumGlobalCopies; |
| 298 | return NewI; |
| 299 | } |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
Nuno Lopes | 95cc4f3 | 2012-07-09 18:38:20 +0000 | [diff] [blame] | 303 | // At last, use the generic allocation site handler to aggressively remove |
| 304 | // unused allocas. |
| 305 | return visitAllocSite(AI); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 308 | /// \brief Helper to combine a load to a new type. |
| 309 | /// |
| 310 | /// This just does the work of combining a load to a new type. It handles |
| 311 | /// metadata, etc., and returns the new instruction. The \c NewTy should be the |
| 312 | /// loaded *value* type. This will convert it to a pointer, cast the operand to |
| 313 | /// that pointer type, load it, etc. |
| 314 | /// |
| 315 | /// Note that this will create all of the instructions with whatever insert |
| 316 | /// point the \c InstCombiner currently is using. |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 317 | static LoadInst *combineLoadToNewType(InstCombiner &IC, LoadInst &LI, Type *NewTy, |
| 318 | const Twine &Suffix = "") { |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 319 | Value *Ptr = LI.getPointerOperand(); |
| 320 | unsigned AS = LI.getPointerAddressSpace(); |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 321 | SmallVector<std::pair<unsigned, MDNode *>, 8> MD; |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 322 | LI.getAllMetadata(MD); |
| 323 | |
| 324 | LoadInst *NewLoad = IC.Builder->CreateAlignedLoad( |
| 325 | IC.Builder->CreateBitCast(Ptr, NewTy->getPointerTo(AS)), |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 326 | LI.getAlignment(), LI.getName() + Suffix); |
Charles Davis | 33d1dc0 | 2015-02-25 05:10:25 +0000 | [diff] [blame] | 327 | MDBuilder MDB(NewLoad->getContext()); |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 328 | for (const auto &MDPair : MD) { |
| 329 | unsigned ID = MDPair.first; |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 330 | MDNode *N = MDPair.second; |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 331 | // Note, essentially every kind of metadata should be preserved here! This |
| 332 | // routine is supposed to clone a load instruction changing *only its type*. |
| 333 | // The only metadata it makes sense to drop is metadata which is invalidated |
| 334 | // when the pointer type changes. This should essentially never be the case |
| 335 | // in LLVM, but we explicitly switch over only known metadata to be |
| 336 | // conservatively correct. If you are adding metadata to LLVM which pertains |
| 337 | // to loads, you almost certainly want to add it here. |
| 338 | switch (ID) { |
| 339 | case LLVMContext::MD_dbg: |
| 340 | case LLVMContext::MD_tbaa: |
| 341 | case LLVMContext::MD_prof: |
| 342 | case LLVMContext::MD_fpmath: |
| 343 | case LLVMContext::MD_tbaa_struct: |
| 344 | case LLVMContext::MD_invariant_load: |
| 345 | case LLVMContext::MD_alias_scope: |
| 346 | case LLVMContext::MD_noalias: |
Philip Reames | 5a3f5f7 | 2014-10-21 00:13:20 +0000 | [diff] [blame] | 347 | case LLVMContext::MD_nontemporal: |
| 348 | case LLVMContext::MD_mem_parallel_loop_access: |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 349 | // All of these directly apply. |
| 350 | NewLoad->setMetadata(ID, N); |
| 351 | break; |
| 352 | |
Chandler Carruth | 87fdafc | 2015-02-13 02:30:01 +0000 | [diff] [blame] | 353 | case LLVMContext::MD_nonnull: |
Charles Davis | 33d1dc0 | 2015-02-25 05:10:25 +0000 | [diff] [blame] | 354 | // This only directly applies if the new type is also a pointer. |
| 355 | if (NewTy->isPointerTy()) { |
Chandler Carruth | 87fdafc | 2015-02-13 02:30:01 +0000 | [diff] [blame] | 356 | NewLoad->setMetadata(ID, N); |
Charles Davis | 33d1dc0 | 2015-02-25 05:10:25 +0000 | [diff] [blame] | 357 | break; |
| 358 | } |
| 359 | // If it's integral now, translate it to !range metadata. |
| 360 | if (NewTy->isIntegerTy()) { |
| 361 | auto *ITy = cast<IntegerType>(NewTy); |
| 362 | auto *NullInt = ConstantExpr::getPtrToInt( |
| 363 | ConstantPointerNull::get(cast<PointerType>(Ptr->getType())), ITy); |
| 364 | auto *NonNullInt = |
| 365 | ConstantExpr::getAdd(NullInt, ConstantInt::get(ITy, 1)); |
| 366 | NewLoad->setMetadata(LLVMContext::MD_range, |
| 367 | MDB.createRange(NonNullInt, NullInt)); |
| 368 | } |
Chandler Carruth | 87fdafc | 2015-02-13 02:30:01 +0000 | [diff] [blame] | 369 | break; |
Artur Pilipenko | 5c5011d | 2015-11-02 17:53:51 +0000 | [diff] [blame] | 370 | case LLVMContext::MD_align: |
| 371 | case LLVMContext::MD_dereferenceable: |
| 372 | case LLVMContext::MD_dereferenceable_or_null: |
| 373 | // These only directly apply if the new type is also a pointer. |
| 374 | if (NewTy->isPointerTy()) |
| 375 | NewLoad->setMetadata(ID, N); |
| 376 | break; |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 377 | case LLVMContext::MD_range: |
| 378 | // FIXME: It would be nice to propagate this in some way, but the type |
Charles Davis | 33d1dc0 | 2015-02-25 05:10:25 +0000 | [diff] [blame] | 379 | // conversions make it hard. If the new type is a pointer, we could |
| 380 | // translate it to !nonnull metadata. |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 381 | break; |
| 382 | } |
| 383 | } |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 384 | return NewLoad; |
| 385 | } |
| 386 | |
Chandler Carruth | fa11d83 | 2015-01-22 03:34:54 +0000 | [diff] [blame] | 387 | /// \brief Combine a store to a new type. |
| 388 | /// |
| 389 | /// Returns the newly created store instruction. |
| 390 | static StoreInst *combineStoreToNewValue(InstCombiner &IC, StoreInst &SI, Value *V) { |
| 391 | Value *Ptr = SI.getPointerOperand(); |
| 392 | unsigned AS = SI.getPointerAddressSpace(); |
| 393 | SmallVector<std::pair<unsigned, MDNode *>, 8> MD; |
| 394 | SI.getAllMetadata(MD); |
| 395 | |
| 396 | StoreInst *NewStore = IC.Builder->CreateAlignedStore( |
| 397 | V, IC.Builder->CreateBitCast(Ptr, V->getType()->getPointerTo(AS)), |
| 398 | SI.getAlignment()); |
| 399 | for (const auto &MDPair : MD) { |
| 400 | unsigned ID = MDPair.first; |
| 401 | MDNode *N = MDPair.second; |
| 402 | // Note, essentially every kind of metadata should be preserved here! This |
| 403 | // routine is supposed to clone a store instruction changing *only its |
| 404 | // type*. The only metadata it makes sense to drop is metadata which is |
| 405 | // invalidated when the pointer type changes. This should essentially |
| 406 | // never be the case in LLVM, but we explicitly switch over only known |
| 407 | // metadata to be conservatively correct. If you are adding metadata to |
| 408 | // LLVM which pertains to stores, you almost certainly want to add it |
| 409 | // here. |
| 410 | switch (ID) { |
| 411 | case LLVMContext::MD_dbg: |
| 412 | case LLVMContext::MD_tbaa: |
| 413 | case LLVMContext::MD_prof: |
| 414 | case LLVMContext::MD_fpmath: |
| 415 | case LLVMContext::MD_tbaa_struct: |
| 416 | case LLVMContext::MD_alias_scope: |
| 417 | case LLVMContext::MD_noalias: |
| 418 | case LLVMContext::MD_nontemporal: |
| 419 | case LLVMContext::MD_mem_parallel_loop_access: |
Chandler Carruth | fa11d83 | 2015-01-22 03:34:54 +0000 | [diff] [blame] | 420 | // All of these directly apply. |
| 421 | NewStore->setMetadata(ID, N); |
| 422 | break; |
| 423 | |
| 424 | case LLVMContext::MD_invariant_load: |
Chandler Carruth | 87fdafc | 2015-02-13 02:30:01 +0000 | [diff] [blame] | 425 | case LLVMContext::MD_nonnull: |
Chandler Carruth | fa11d83 | 2015-01-22 03:34:54 +0000 | [diff] [blame] | 426 | case LLVMContext::MD_range: |
Artur Pilipenko | 5c5011d | 2015-11-02 17:53:51 +0000 | [diff] [blame] | 427 | case LLVMContext::MD_align: |
| 428 | case LLVMContext::MD_dereferenceable: |
| 429 | case LLVMContext::MD_dereferenceable_or_null: |
Chandler Carruth | 87fdafc | 2015-02-13 02:30:01 +0000 | [diff] [blame] | 430 | // These don't apply for stores. |
Chandler Carruth | fa11d83 | 2015-01-22 03:34:54 +0000 | [diff] [blame] | 431 | break; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | return NewStore; |
| 436 | } |
| 437 | |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 438 | /// \brief Combine loads to match the type of value their uses after looking |
| 439 | /// through intervening bitcasts. |
| 440 | /// |
| 441 | /// The core idea here is that if the result of a load is used in an operation, |
| 442 | /// we should load the type most conducive to that operation. For example, when |
| 443 | /// loading an integer and converting that immediately to a pointer, we should |
| 444 | /// instead directly load a pointer. |
| 445 | /// |
| 446 | /// However, this routine must never change the width of a load or the number of |
| 447 | /// loads as that would introduce a semantic change. This combine is expected to |
| 448 | /// be a semantic no-op which just allows loads to more closely model the types |
| 449 | /// of their consuming operations. |
| 450 | /// |
| 451 | /// Currently, we also refuse to change the precise type used for an atomic load |
| 452 | /// or a volatile load. This is debatable, and might be reasonable to change |
| 453 | /// later. However, it is risky in case some backend or other part of LLVM is |
| 454 | /// relying on the exact type loaded to select appropriate atomic operations. |
| 455 | static Instruction *combineLoadToOperationType(InstCombiner &IC, LoadInst &LI) { |
| 456 | // FIXME: We could probably with some care handle both volatile and atomic |
| 457 | // loads here but it isn't clear that this is important. |
| 458 | if (!LI.isSimple()) |
| 459 | return nullptr; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 460 | |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 461 | if (LI.use_empty()) |
| 462 | return nullptr; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 463 | |
Chandler Carruth | cd8522e | 2015-01-22 05:08:12 +0000 | [diff] [blame] | 464 | Type *Ty = LI.getType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 465 | const DataLayout &DL = IC.getDataLayout(); |
Chandler Carruth | cd8522e | 2015-01-22 05:08:12 +0000 | [diff] [blame] | 466 | |
| 467 | // Try to canonicalize loads which are only ever stored to operate over |
| 468 | // integers instead of any other type. We only do this when the loaded type |
| 469 | // is sized and has a size exactly the same as its store size and the store |
| 470 | // size is a legal integer type. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 471 | if (!Ty->isIntegerTy() && Ty->isSized() && |
| 472 | DL.isLegalInteger(DL.getTypeStoreSizeInBits(Ty)) && |
| 473 | DL.getTypeStoreSizeInBits(Ty) == DL.getTypeSizeInBits(Ty)) { |
Chandler Carruth | cd8522e | 2015-01-22 05:08:12 +0000 | [diff] [blame] | 474 | if (std::all_of(LI.user_begin(), LI.user_end(), [&LI](User *U) { |
| 475 | auto *SI = dyn_cast<StoreInst>(U); |
| 476 | return SI && SI->getPointerOperand() != &LI; |
| 477 | })) { |
| 478 | LoadInst *NewLoad = combineLoadToNewType( |
| 479 | IC, LI, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 480 | Type::getIntNTy(LI.getContext(), DL.getTypeStoreSizeInBits(Ty))); |
Chandler Carruth | cd8522e | 2015-01-22 05:08:12 +0000 | [diff] [blame] | 481 | // Replace all the stores with stores of the newly loaded value. |
| 482 | for (auto UI = LI.user_begin(), UE = LI.user_end(); UI != UE;) { |
| 483 | auto *SI = cast<StoreInst>(*UI++); |
| 484 | IC.Builder->SetInsertPoint(SI); |
| 485 | combineStoreToNewValue(IC, *SI, NewLoad); |
| 486 | IC.EraseInstFromFunction(*SI); |
| 487 | } |
| 488 | assert(LI.use_empty() && "Failed to remove all users of the load!"); |
| 489 | // Return the old load so the combiner can delete it safely. |
| 490 | return &LI; |
| 491 | } |
| 492 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 493 | |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 494 | // Fold away bit casts of the loaded value by loading the desired type. |
David Majnemer | dd04352 | 2015-05-28 18:39:17 +0000 | [diff] [blame] | 495 | // We can do this for BitCastInsts as well as casts from and to pointer types, |
| 496 | // as long as those are noops (i.e., the source or dest type have the same |
| 497 | // bitwidth as the target's pointers). |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 498 | if (LI.hasOneUse()) |
David Majnemer | dd04352 | 2015-05-28 18:39:17 +0000 | [diff] [blame] | 499 | if (auto* CI = dyn_cast<CastInst>(LI.user_back())) { |
| 500 | if (CI->isNoopCast(DL)) { |
| 501 | LoadInst *NewLoad = combineLoadToNewType(IC, LI, CI->getDestTy()); |
| 502 | CI->replaceAllUsesWith(NewLoad); |
| 503 | IC.EraseInstFromFunction(*CI); |
| 504 | return &LI; |
| 505 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 506 | } |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 507 | |
Chandler Carruth | a7f247e | 2014-12-09 19:21:16 +0000 | [diff] [blame] | 508 | // FIXME: We should also canonicalize loads of vectors when their elements are |
| 509 | // cast to other types. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 510 | return nullptr; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 513 | static Instruction *unpackLoadToAggregate(InstCombiner &IC, LoadInst &LI) { |
| 514 | // FIXME: We could probably with some care handle both volatile and atomic |
| 515 | // stores here but it isn't clear that this is important. |
| 516 | if (!LI.isSimple()) |
| 517 | return nullptr; |
| 518 | |
| 519 | Type *T = LI.getType(); |
| 520 | if (!T->isAggregateType()) |
| 521 | return nullptr; |
| 522 | |
Bruce Mitchener | e9ffb45 | 2015-09-12 01:17:08 +0000 | [diff] [blame] | 523 | assert(LI.getAlignment() && "Alignment must be set at this point"); |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 524 | |
| 525 | if (auto *ST = dyn_cast<StructType>(T)) { |
| 526 | // If the struct only have one element, we unpack. |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame^] | 527 | unsigned Count = ST->getNumElements(); |
| 528 | if (Count == 1) { |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 529 | LoadInst *NewLoad = combineLoadToNewType(IC, LI, ST->getTypeAtIndex(0U), |
| 530 | ".unpack"); |
| 531 | return IC.ReplaceInstUsesWith(LI, IC.Builder->CreateInsertValue( |
| 532 | UndefValue::get(T), NewLoad, 0, LI.getName())); |
| 533 | } |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame^] | 534 | |
| 535 | // We don't want to break loads with padding here as we'd loose |
| 536 | // the knowledge that padding exists for the rest of the pipeline. |
| 537 | const DataLayout &DL = IC.getDataLayout(); |
| 538 | auto *SL = DL.getStructLayout(ST); |
| 539 | if (SL->hasPadding()) |
| 540 | return nullptr; |
| 541 | |
| 542 | auto Name = LI.getName(); |
| 543 | auto LoadName = LI.getName() + ".unpack"; |
| 544 | auto EltName = Name + ".elt"; |
| 545 | auto *Addr = LI.getPointerOperand(); |
| 546 | Value *V = UndefValue::get(T); |
| 547 | auto *IdxType = Type::getInt32Ty(ST->getContext()); |
| 548 | auto *Zero = ConstantInt::get(IdxType, 0); |
| 549 | for (unsigned i = 0; i < Count; i++) { |
| 550 | Value *Indices[2] = { |
| 551 | Zero, |
| 552 | ConstantInt::get(IdxType, i), |
| 553 | }; |
| 554 | auto *Ptr = IC.Builder->CreateInBoundsGEP(ST, Addr, makeArrayRef(Indices), EltName); |
| 555 | auto *L = IC.Builder->CreateLoad(ST->getTypeAtIndex(i), Ptr, LoadName); |
| 556 | V = IC.Builder->CreateInsertValue(V, L, i); |
| 557 | } |
| 558 | |
| 559 | V->setName(Name); |
| 560 | return IC.ReplaceInstUsesWith(LI, V); |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 561 | } |
| 562 | |
David Majnemer | 58fb038 | 2015-05-11 05:04:22 +0000 | [diff] [blame] | 563 | if (auto *AT = dyn_cast<ArrayType>(T)) { |
| 564 | // If the array only have one element, we unpack. |
| 565 | if (AT->getNumElements() == 1) { |
| 566 | LoadInst *NewLoad = combineLoadToNewType(IC, LI, AT->getElementType(), |
| 567 | ".unpack"); |
| 568 | return IC.ReplaceInstUsesWith(LI, IC.Builder->CreateInsertValue( |
| 569 | UndefValue::get(T), NewLoad, 0, LI.getName())); |
| 570 | } |
| 571 | } |
| 572 | |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 573 | return nullptr; |
| 574 | } |
| 575 | |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 576 | // If we can determine that all possible objects pointed to by the provided |
| 577 | // pointer value are, not only dereferenceable, but also definitively less than |
| 578 | // or equal to the provided maximum size, then return true. Otherwise, return |
| 579 | // false (constant global values and allocas fall into this category). |
| 580 | // |
| 581 | // FIXME: This should probably live in ValueTracking (or similar). |
| 582 | static bool isObjectSizeLessThanOrEq(Value *V, uint64_t MaxSize, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 583 | const DataLayout &DL) { |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 584 | SmallPtrSet<Value *, 4> Visited; |
| 585 | SmallVector<Value *, 4> Worklist(1, V); |
| 586 | |
| 587 | do { |
| 588 | Value *P = Worklist.pop_back_val(); |
| 589 | P = P->stripPointerCasts(); |
| 590 | |
| 591 | if (!Visited.insert(P).second) |
| 592 | continue; |
| 593 | |
| 594 | if (SelectInst *SI = dyn_cast<SelectInst>(P)) { |
| 595 | Worklist.push_back(SI->getTrueValue()); |
| 596 | Worklist.push_back(SI->getFalseValue()); |
| 597 | continue; |
| 598 | } |
| 599 | |
| 600 | if (PHINode *PN = dyn_cast<PHINode>(P)) { |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 601 | for (Value *IncValue : PN->incoming_values()) |
| 602 | Worklist.push_back(IncValue); |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 603 | continue; |
| 604 | } |
| 605 | |
| 606 | if (GlobalAlias *GA = dyn_cast<GlobalAlias>(P)) { |
| 607 | if (GA->mayBeOverridden()) |
| 608 | return false; |
| 609 | Worklist.push_back(GA->getAliasee()); |
| 610 | continue; |
| 611 | } |
| 612 | |
| 613 | // If we know how big this object is, and it is less than MaxSize, continue |
| 614 | // searching. Otherwise, return false. |
| 615 | if (AllocaInst *AI = dyn_cast<AllocaInst>(P)) { |
| 616 | if (!AI->getAllocatedType()->isSized()) |
| 617 | return false; |
| 618 | |
| 619 | ConstantInt *CS = dyn_cast<ConstantInt>(AI->getArraySize()); |
| 620 | if (!CS) |
| 621 | return false; |
| 622 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 623 | uint64_t TypeSize = DL.getTypeAllocSize(AI->getAllocatedType()); |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 624 | // Make sure that, even if the multiplication below would wrap as an |
| 625 | // uint64_t, we still do the right thing. |
| 626 | if ((CS->getValue().zextOrSelf(128)*APInt(128, TypeSize)).ugt(MaxSize)) |
| 627 | return false; |
| 628 | continue; |
| 629 | } |
| 630 | |
| 631 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) { |
| 632 | if (!GV->hasDefinitiveInitializer() || !GV->isConstant()) |
| 633 | return false; |
| 634 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 635 | uint64_t InitSize = DL.getTypeAllocSize(GV->getType()->getElementType()); |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 636 | if (InitSize > MaxSize) |
| 637 | return false; |
| 638 | continue; |
| 639 | } |
| 640 | |
| 641 | return false; |
| 642 | } while (!Worklist.empty()); |
| 643 | |
| 644 | return true; |
| 645 | } |
| 646 | |
| 647 | // If we're indexing into an object of a known size, and the outer index is |
| 648 | // not a constant, but having any value but zero would lead to undefined |
| 649 | // behavior, replace it with zero. |
| 650 | // |
| 651 | // For example, if we have: |
| 652 | // @f.a = private unnamed_addr constant [1 x i32] [i32 12], align 4 |
| 653 | // ... |
| 654 | // %arrayidx = getelementptr inbounds [1 x i32]* @f.a, i64 0, i64 %x |
| 655 | // ... = load i32* %arrayidx, align 4 |
| 656 | // Then we know that we can replace %x in the GEP with i64 0. |
| 657 | // |
| 658 | // FIXME: We could fold any GEP index to zero that would cause UB if it were |
| 659 | // not zero. Currently, we only handle the first such index. Also, we could |
| 660 | // also search through non-zero constant indices if we kept track of the |
| 661 | // offsets those indices implied. |
| 662 | static bool canReplaceGEPIdxWithZero(InstCombiner &IC, GetElementPtrInst *GEPI, |
| 663 | Instruction *MemI, unsigned &Idx) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 664 | if (GEPI->getNumOperands() < 2) |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 665 | return false; |
| 666 | |
| 667 | // Find the first non-zero index of a GEP. If all indices are zero, return |
| 668 | // one past the last index. |
| 669 | auto FirstNZIdx = [](const GetElementPtrInst *GEPI) { |
| 670 | unsigned I = 1; |
| 671 | for (unsigned IE = GEPI->getNumOperands(); I != IE; ++I) { |
| 672 | Value *V = GEPI->getOperand(I); |
| 673 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 674 | if (CI->isZero()) |
| 675 | continue; |
| 676 | |
| 677 | break; |
| 678 | } |
| 679 | |
| 680 | return I; |
| 681 | }; |
| 682 | |
| 683 | // Skip through initial 'zero' indices, and find the corresponding pointer |
| 684 | // type. See if the next index is not a constant. |
| 685 | Idx = FirstNZIdx(GEPI); |
| 686 | if (Idx == GEPI->getNumOperands()) |
| 687 | return false; |
| 688 | if (isa<Constant>(GEPI->getOperand(Idx))) |
| 689 | return false; |
| 690 | |
| 691 | SmallVector<Value *, 4> Ops(GEPI->idx_begin(), GEPI->idx_begin() + Idx); |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 692 | Type *AllocTy = GetElementPtrInst::getIndexedType( |
| 693 | cast<PointerType>(GEPI->getOperand(0)->getType()->getScalarType()) |
| 694 | ->getElementType(), |
| 695 | Ops); |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 696 | if (!AllocTy || !AllocTy->isSized()) |
| 697 | return false; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 698 | const DataLayout &DL = IC.getDataLayout(); |
| 699 | uint64_t TyAllocSize = DL.getTypeAllocSize(AllocTy); |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 700 | |
| 701 | // If there are more indices after the one we might replace with a zero, make |
| 702 | // sure they're all non-negative. If any of them are negative, the overall |
| 703 | // address being computed might be before the base address determined by the |
| 704 | // first non-zero index. |
| 705 | auto IsAllNonNegative = [&]() { |
| 706 | for (unsigned i = Idx+1, e = GEPI->getNumOperands(); i != e; ++i) { |
| 707 | bool KnownNonNegative, KnownNegative; |
| 708 | IC.ComputeSignBit(GEPI->getOperand(i), KnownNonNegative, |
| 709 | KnownNegative, 0, MemI); |
| 710 | if (KnownNonNegative) |
| 711 | continue; |
| 712 | return false; |
| 713 | } |
| 714 | |
| 715 | return true; |
| 716 | }; |
| 717 | |
| 718 | // FIXME: If the GEP is not inbounds, and there are extra indices after the |
| 719 | // one we'll replace, those could cause the address computation to wrap |
| 720 | // (rendering the IsAllNonNegative() check below insufficient). We can do |
Bruce Mitchener | e9ffb45 | 2015-09-12 01:17:08 +0000 | [diff] [blame] | 721 | // better, ignoring zero indices (and other indices we can prove small |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 722 | // enough not to wrap). |
| 723 | if (Idx+1 != GEPI->getNumOperands() && !GEPI->isInBounds()) |
| 724 | return false; |
| 725 | |
| 726 | // Note that isObjectSizeLessThanOrEq will return true only if the pointer is |
| 727 | // also known to be dereferenceable. |
| 728 | return isObjectSizeLessThanOrEq(GEPI->getOperand(0), TyAllocSize, DL) && |
| 729 | IsAllNonNegative(); |
| 730 | } |
| 731 | |
| 732 | // If we're indexing into an object with a variable index for the memory |
| 733 | // access, but the object has only one element, we can assume that the index |
| 734 | // will always be zero. If we replace the GEP, return it. |
| 735 | template <typename T> |
| 736 | static Instruction *replaceGEPIdxWithZero(InstCombiner &IC, Value *Ptr, |
| 737 | T &MemI) { |
| 738 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Ptr)) { |
| 739 | unsigned Idx; |
| 740 | if (canReplaceGEPIdxWithZero(IC, GEPI, &MemI, Idx)) { |
| 741 | Instruction *NewGEPI = GEPI->clone(); |
| 742 | NewGEPI->setOperand(Idx, |
| 743 | ConstantInt::get(GEPI->getOperand(Idx)->getType(), 0)); |
| 744 | NewGEPI->insertBefore(GEPI); |
| 745 | MemI.setOperand(MemI.getPointerOperandIndex(), NewGEPI); |
| 746 | return NewGEPI; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | return nullptr; |
| 751 | } |
| 752 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 753 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 754 | Value *Op = LI.getOperand(0); |
| 755 | |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 756 | // Try to canonicalize the loaded type. |
| 757 | if (Instruction *Res = combineLoadToOperationType(*this, LI)) |
| 758 | return Res; |
| 759 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 760 | // Attempt to improve the alignment. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 761 | unsigned KnownAlign = getOrEnforceKnownAlignment( |
| 762 | Op, DL.getPrefTypeAlignment(LI.getType()), DL, &LI, AC, DT); |
| 763 | unsigned LoadAlign = LI.getAlignment(); |
| 764 | unsigned EffectiveLoadAlign = |
| 765 | LoadAlign != 0 ? LoadAlign : DL.getABITypeAlignment(LI.getType()); |
Dan Gohman | 3619660 | 2010-08-03 18:20:32 +0000 | [diff] [blame] | 766 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 767 | if (KnownAlign > EffectiveLoadAlign) |
| 768 | LI.setAlignment(KnownAlign); |
| 769 | else if (LoadAlign == 0) |
| 770 | LI.setAlignment(EffectiveLoadAlign); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 771 | |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 772 | // Replace GEP indices if possible. |
| 773 | if (Instruction *NewGEPI = replaceGEPIdxWithZero(*this, Op, LI)) { |
| 774 | Worklist.Add(NewGEPI); |
| 775 | return &LI; |
| 776 | } |
| 777 | |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 778 | // None of the following transforms are legal for volatile/atomic loads. |
| 779 | // FIXME: Some of it is okay for atomic loads; needs refactoring. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 780 | if (!LI.isSimple()) return nullptr; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 781 | |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 782 | if (Instruction *Res = unpackLoadToAggregate(*this, LI)) |
| 783 | return Res; |
| 784 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 785 | // 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] | 786 | // where there are several consecutive memory accesses to the same location, |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 787 | // separated by a few arithmetic operations. |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 788 | BasicBlock::iterator BBI(LI); |
Bjorn Steinbrink | a91fd09 | 2015-07-10 06:55:44 +0000 | [diff] [blame] | 789 | AAMDNodes AATags; |
Larisse Voufo | 532bf71 | 2015-09-18 19:14:35 +0000 | [diff] [blame] | 790 | if (Value *AvailableVal = |
| 791 | FindAvailableLoadedValue(Op, LI.getParent(), BBI, |
| 792 | DefMaxInstsToScan, AA, &AATags)) { |
Bjorn Steinbrink | a91fd09 | 2015-07-10 06:55:44 +0000 | [diff] [blame] | 793 | if (LoadInst *NLI = dyn_cast<LoadInst>(AvailableVal)) { |
| 794 | unsigned KnownIDs[] = { |
Artur Pilipenko | 5c5011d | 2015-11-02 17:53:51 +0000 | [diff] [blame] | 795 | LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope, |
| 796 | LLVMContext::MD_noalias, LLVMContext::MD_range, |
| 797 | LLVMContext::MD_invariant_load, LLVMContext::MD_nonnull, |
| 798 | LLVMContext::MD_invariant_group, LLVMContext::MD_align, |
| 799 | LLVMContext::MD_dereferenceable, |
| 800 | LLVMContext::MD_dereferenceable_or_null}; |
Bjorn Steinbrink | a91fd09 | 2015-07-10 06:55:44 +0000 | [diff] [blame] | 801 | combineMetadata(NLI, &LI, KnownIDs); |
Bjorn Steinbrink | a91fd09 | 2015-07-10 06:55:44 +0000 | [diff] [blame] | 802 | }; |
| 803 | |
Chandler Carruth | eeec35a | 2014-10-20 00:24:14 +0000 | [diff] [blame] | 804 | return ReplaceInstUsesWith( |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 805 | LI, Builder->CreateBitOrPointerCast(AvailableVal, LI.getType(), |
| 806 | LI.getName() + ".cast")); |
Bjorn Steinbrink | a91fd09 | 2015-07-10 06:55:44 +0000 | [diff] [blame] | 807 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 808 | |
| 809 | // load(gep null, ...) -> unreachable |
| 810 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 811 | const Value *GEPI0 = GEPI->getOperand(0); |
| 812 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 813 | if (isa<ConstantPointerNull>(GEPI0) && GEPI->getPointerAddressSpace() == 0){ |
| 814 | // Insert a new store to null instruction before the load to indicate |
| 815 | // that this code is not reachable. We do this instead of inserting |
| 816 | // an unreachable instruction directly because we cannot modify the |
| 817 | // CFG. |
| 818 | new StoreInst(UndefValue::get(LI.getType()), |
| 819 | Constant::getNullValue(Op->getType()), &LI); |
| 820 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 821 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 822 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 823 | |
| 824 | // load null/undef -> unreachable |
| 825 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 826 | if (isa<UndefValue>(Op) || |
| 827 | (isa<ConstantPointerNull>(Op) && LI.getPointerAddressSpace() == 0)) { |
| 828 | // Insert a new store to null instruction before the load to indicate that |
| 829 | // this code is not reachable. We do this instead of inserting an |
| 830 | // unreachable instruction directly because we cannot modify the CFG. |
| 831 | new StoreInst(UndefValue::get(LI.getType()), |
| 832 | Constant::getNullValue(Op->getType()), &LI); |
| 833 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 834 | } |
| 835 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 836 | if (Op->hasOneUse()) { |
| 837 | // Change select and PHI nodes to select values instead of addresses: this |
| 838 | // helps alias analysis out a lot, allows many others simplifications, and |
| 839 | // exposes redundancy in the code. |
| 840 | // |
| 841 | // Note that we cannot do the transformation unless we know that the |
| 842 | // introduced loads cannot trap! Something like this is valid as long as |
| 843 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 844 | // but it would not be valid if we transformed it to load from null |
| 845 | // unconditionally. |
| 846 | // |
| 847 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 848 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Bob Wilson | 56600a1 | 2010-01-30 04:42:39 +0000 | [diff] [blame] | 849 | unsigned Align = LI.getAlignment(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 850 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI, Align) && |
| 851 | isSafeToLoadUnconditionally(SI->getOperand(2), SI, Align)) { |
Bob Wilson | 4b71b6c | 2010-01-30 00:41:10 +0000 | [diff] [blame] | 852 | LoadInst *V1 = Builder->CreateLoad(SI->getOperand(1), |
Bob Wilson | 56600a1 | 2010-01-30 04:42:39 +0000 | [diff] [blame] | 853 | SI->getOperand(1)->getName()+".val"); |
Bob Wilson | 4b71b6c | 2010-01-30 00:41:10 +0000 | [diff] [blame] | 854 | LoadInst *V2 = Builder->CreateLoad(SI->getOperand(2), |
Bob Wilson | 56600a1 | 2010-01-30 04:42:39 +0000 | [diff] [blame] | 855 | SI->getOperand(2)->getName()+".val"); |
| 856 | V1->setAlignment(Align); |
| 857 | V2->setAlignment(Align); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 858 | return SelectInst::Create(SI->getCondition(), V1, V2); |
| 859 | } |
| 860 | |
| 861 | // load (select (cond, null, P)) -> load P |
Larisse Voufo | 532bf71 | 2015-09-18 19:14:35 +0000 | [diff] [blame] | 862 | if (isa<ConstantPointerNull>(SI->getOperand(1)) && |
Philip Reames | 5ad26c3 | 2014-12-29 22:46:21 +0000 | [diff] [blame] | 863 | LI.getPointerAddressSpace() == 0) { |
| 864 | LI.setOperand(0, SI->getOperand(2)); |
| 865 | return &LI; |
| 866 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 867 | |
| 868 | // load (select (cond, P, null)) -> load P |
Philip Reames | 5ad26c3 | 2014-12-29 22:46:21 +0000 | [diff] [blame] | 869 | if (isa<ConstantPointerNull>(SI->getOperand(2)) && |
| 870 | LI.getPointerAddressSpace() == 0) { |
| 871 | LI.setOperand(0, SI->getOperand(1)); |
| 872 | return &LI; |
| 873 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 874 | } |
| 875 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 876 | return nullptr; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 879 | /// \brief Combine stores to match the type of value being stored. |
| 880 | /// |
| 881 | /// The core idea here is that the memory does not have any intrinsic type and |
| 882 | /// where we can we should match the type of a store to the type of value being |
| 883 | /// stored. |
| 884 | /// |
| 885 | /// However, this routine must never change the width of a store or the number of |
| 886 | /// stores as that would introduce a semantic change. This combine is expected to |
| 887 | /// be a semantic no-op which just allows stores to more closely model the types |
| 888 | /// of their incoming values. |
| 889 | /// |
| 890 | /// Currently, we also refuse to change the precise type used for an atomic or |
| 891 | /// volatile store. This is debatable, and might be reasonable to change later. |
| 892 | /// However, it is risky in case some backend or other part of LLVM is relying |
| 893 | /// on the exact type stored to select appropriate atomic operations. |
| 894 | /// |
| 895 | /// \returns true if the store was successfully combined away. This indicates |
| 896 | /// the caller must erase the store instruction. We have to let the caller erase |
Bruce Mitchener | e9ffb45 | 2015-09-12 01:17:08 +0000 | [diff] [blame] | 897 | /// the store instruction as otherwise there is no way to signal whether it was |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 898 | /// combined or not: IC.EraseInstFromFunction returns a null pointer. |
| 899 | static bool combineStoreToValueType(InstCombiner &IC, StoreInst &SI) { |
| 900 | // FIXME: We could probably with some care handle both volatile and atomic |
| 901 | // stores here but it isn't clear that this is important. |
| 902 | if (!SI.isSimple()) |
| 903 | return false; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 904 | |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 905 | Value *V = SI.getValueOperand(); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 906 | |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 907 | // Fold away bit casts of the stored value by storing the original type. |
| 908 | if (auto *BC = dyn_cast<BitCastInst>(V)) { |
Chandler Carruth | a7f247e | 2014-12-09 19:21:16 +0000 | [diff] [blame] | 909 | V = BC->getOperand(0); |
Chandler Carruth | 2135b97 | 2015-01-21 23:45:01 +0000 | [diff] [blame] | 910 | combineStoreToNewValue(IC, SI, V); |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 911 | return true; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 914 | // FIXME: We should also canonicalize loads of vectors when their elements are |
| 915 | // cast to other types. |
| 916 | return false; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 919 | static bool unpackStoreToAggregate(InstCombiner &IC, StoreInst &SI) { |
| 920 | // FIXME: We could probably with some care handle both volatile and atomic |
| 921 | // stores here but it isn't clear that this is important. |
| 922 | if (!SI.isSimple()) |
| 923 | return false; |
| 924 | |
| 925 | Value *V = SI.getValueOperand(); |
| 926 | Type *T = V->getType(); |
| 927 | |
| 928 | if (!T->isAggregateType()) |
| 929 | return false; |
| 930 | |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 931 | if (auto *ST = dyn_cast<StructType>(T)) { |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 932 | // If the struct only have one element, we unpack. |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame^] | 933 | unsigned Count = ST->getNumElements(); |
| 934 | if (Count == 1) { |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 935 | V = IC.Builder->CreateExtractValue(V, 0); |
| 936 | combineStoreToNewValue(IC, SI, V); |
| 937 | return true; |
| 938 | } |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame^] | 939 | |
| 940 | // We don't want to break loads with padding here as we'd loose |
| 941 | // the knowledge that padding exists for the rest of the pipeline. |
| 942 | const DataLayout &DL = IC.getDataLayout(); |
| 943 | auto *SL = DL.getStructLayout(ST); |
| 944 | if (SL->hasPadding()) |
| 945 | return false; |
| 946 | |
| 947 | auto EltName = V->getName() + ".elt"; |
| 948 | auto *Addr = SI.getPointerOperand(); |
| 949 | auto AddrName = Addr->getName() + ".repack"; |
| 950 | auto *IdxType = Type::getInt32Ty(ST->getContext()); |
| 951 | auto *Zero = ConstantInt::get(IdxType, 0); |
| 952 | for (unsigned i = 0; i < Count; i++) { |
| 953 | Value *Indices[2] = { |
| 954 | Zero, |
| 955 | ConstantInt::get(IdxType, i), |
| 956 | }; |
| 957 | auto *Ptr = IC.Builder->CreateInBoundsGEP(ST, Addr, makeArrayRef(Indices), AddrName); |
| 958 | auto *Val = IC.Builder->CreateExtractValue(V, i, EltName); |
| 959 | IC.Builder->CreateStore(Val, Ptr); |
| 960 | } |
| 961 | |
| 962 | return true; |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 963 | } |
| 964 | |
David Majnemer | 7536460 | 2015-05-11 05:04:27 +0000 | [diff] [blame] | 965 | if (auto *AT = dyn_cast<ArrayType>(T)) { |
| 966 | // If the array only have one element, we unpack. |
| 967 | if (AT->getNumElements() == 1) { |
| 968 | V = IC.Builder->CreateExtractValue(V, 0); |
| 969 | combineStoreToNewValue(IC, SI, V); |
| 970 | return true; |
| 971 | } |
| 972 | } |
| 973 | |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 974 | return false; |
| 975 | } |
| 976 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 977 | /// equivalentAddressValues - Test if A and B will obviously have the same |
| 978 | /// value. This includes recognizing that %t0 and %t1 will have the same |
| 979 | /// value in code like this: |
| 980 | /// %t0 = getelementptr \@a, 0, 3 |
| 981 | /// store i32 0, i32* %t0 |
| 982 | /// %t1 = getelementptr \@a, 0, 3 |
| 983 | /// %t2 = load i32* %t1 |
| 984 | /// |
| 985 | static bool equivalentAddressValues(Value *A, Value *B) { |
| 986 | // Test if the values are trivially equivalent. |
| 987 | if (A == B) return true; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 988 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 989 | // Test if the values come form identical arithmetic instructions. |
| 990 | // This uses isIdenticalToWhenDefined instead of isIdenticalTo because |
| 991 | // its only used to compare two uses within the same basic block, which |
| 992 | // means that they'll always either have the same value or one of them |
| 993 | // will have an undefined value. |
| 994 | if (isa<BinaryOperator>(A) || |
| 995 | isa<CastInst>(A) || |
| 996 | isa<PHINode>(A) || |
| 997 | isa<GetElementPtrInst>(A)) |
| 998 | if (Instruction *BI = dyn_cast<Instruction>(B)) |
| 999 | if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI)) |
| 1000 | return true; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1001 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1002 | // Otherwise they may not be equivalent. |
| 1003 | return false; |
| 1004 | } |
| 1005 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1006 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 1007 | Value *Val = SI.getOperand(0); |
| 1008 | Value *Ptr = SI.getOperand(1); |
| 1009 | |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 1010 | // Try to canonicalize the stored type. |
| 1011 | if (combineStoreToValueType(*this, SI)) |
| 1012 | return EraseInstFromFunction(SI); |
| 1013 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1014 | // Attempt to improve the alignment. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1015 | unsigned KnownAlign = getOrEnforceKnownAlignment( |
| 1016 | Ptr, DL.getPrefTypeAlignment(Val->getType()), DL, &SI, AC, DT); |
| 1017 | unsigned StoreAlign = SI.getAlignment(); |
| 1018 | unsigned EffectiveStoreAlign = |
| 1019 | StoreAlign != 0 ? StoreAlign : DL.getABITypeAlignment(Val->getType()); |
Dan Gohman | 3619660 | 2010-08-03 18:20:32 +0000 | [diff] [blame] | 1020 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1021 | if (KnownAlign > EffectiveStoreAlign) |
| 1022 | SI.setAlignment(KnownAlign); |
| 1023 | else if (StoreAlign == 0) |
| 1024 | SI.setAlignment(EffectiveStoreAlign); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1025 | |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 1026 | // Try to canonicalize the stored type. |
| 1027 | if (unpackStoreToAggregate(*this, SI)) |
| 1028 | return EraseInstFromFunction(SI); |
| 1029 | |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 1030 | // Replace GEP indices if possible. |
| 1031 | if (Instruction *NewGEPI = replaceGEPIdxWithZero(*this, Ptr, SI)) { |
| 1032 | Worklist.Add(NewGEPI); |
| 1033 | return &SI; |
| 1034 | } |
| 1035 | |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1036 | // Don't hack volatile/atomic stores. |
| 1037 | // FIXME: Some bits are legal for atomic stores; needs refactoring. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1038 | if (!SI.isSimple()) return nullptr; |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1039 | |
| 1040 | // If the RHS is an alloca with a single use, zapify the store, making the |
| 1041 | // alloca dead. |
| 1042 | if (Ptr->hasOneUse()) { |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1043 | if (isa<AllocaInst>(Ptr)) |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1044 | return EraseInstFromFunction(SI); |
| 1045 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { |
| 1046 | if (isa<AllocaInst>(GEP->getOperand(0))) { |
| 1047 | if (GEP->getOperand(0)->hasOneUse()) |
| 1048 | return EraseInstFromFunction(SI); |
| 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1053 | // Do really simple DSE, to catch cases where there are several consecutive |
| 1054 | // stores to the same location, separated by a few arithmetic operations. This |
| 1055 | // situation often occurs with bitfield accesses. |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 1056 | BasicBlock::iterator BBI(SI); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1057 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 1058 | --ScanInsts) { |
| 1059 | --BBI; |
Victor Hernandez | 5f8c8c0 | 2010-01-22 19:05:05 +0000 | [diff] [blame] | 1060 | // Don't count debug info directives, lest they affect codegen, |
| 1061 | // and we skip pointer-to-pointer bitcasts, which are NOPs. |
| 1062 | if (isa<DbgInfoIntrinsic>(BBI) || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1063 | (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1064 | ScanInsts++; |
| 1065 | continue; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1068 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 1069 | // Prev store isn't volatile, and stores to the same location? |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1070 | if (PrevSI->isSimple() && equivalentAddressValues(PrevSI->getOperand(1), |
| 1071 | SI.getOperand(1))) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1072 | ++NumDeadStore; |
| 1073 | ++BBI; |
| 1074 | EraseInstFromFunction(*PrevSI); |
| 1075 | continue; |
| 1076 | } |
| 1077 | break; |
| 1078 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1079 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1080 | // If this is a load, we have to stop. However, if the loaded value is from |
| 1081 | // the pointer we're loading and is producing the pointer we're storing, |
| 1082 | // then *this* store is dead (X = load P; store X -> P). |
| 1083 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
Jin-Gu Kang | b452db0 | 2011-03-14 01:21:00 +0000 | [diff] [blame] | 1084 | if (LI == Val && equivalentAddressValues(LI->getOperand(0), Ptr) && |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1085 | LI->isSimple()) |
Jin-Gu Kang | b452db0 | 2011-03-14 01:21:00 +0000 | [diff] [blame] | 1086 | return EraseInstFromFunction(SI); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1087 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1088 | // Otherwise, this is a load from some other location. Stores before it |
| 1089 | // may not be dead. |
| 1090 | break; |
| 1091 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1092 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1093 | // Don't skip over loads or things that can modify memory. |
| 1094 | if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory()) |
| 1095 | break; |
| 1096 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1097 | |
| 1098 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 1099 | if (isa<ConstantPointerNull>(Ptr) && SI.getPointerAddressSpace() == 0) { |
| 1100 | if (!isa<UndefValue>(Val)) { |
| 1101 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 1102 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
| 1103 | Worklist.Add(U); // Dropped a use. |
| 1104 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1105 | return nullptr; // Do not modify these! |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | // store undef, Ptr -> noop |
| 1109 | if (isa<UndefValue>(Val)) |
| 1110 | return EraseInstFromFunction(SI); |
| 1111 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1112 | // If this store is the last instruction in the basic block (possibly |
Victor Hernandez | 5f5abd5 | 2010-01-21 23:07:15 +0000 | [diff] [blame] | 1113 | // excepting debug info instructions), and if the block ends with an |
| 1114 | // unconditional branch, try to move it to the successor block. |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 1115 | BBI = SI.getIterator(); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1116 | do { |
| 1117 | ++BBI; |
Victor Hernandez | 5f8c8c0 | 2010-01-22 19:05:05 +0000 | [diff] [blame] | 1118 | } while (isa<DbgInfoIntrinsic>(BBI) || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1119 | (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1120 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
| 1121 | if (BI->isUnconditional()) |
| 1122 | if (SimplifyStoreAtEndOfBlock(SI)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1123 | return nullptr; // xform done! |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1124 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1125 | return nullptr; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | /// SimplifyStoreAtEndOfBlock - Turn things like: |
| 1129 | /// if () { *P = v1; } else { *P = v2 } |
| 1130 | /// into a phi node with a store in the successor. |
| 1131 | /// |
| 1132 | /// Simplify things like: |
| 1133 | /// *P = v1; if () { *P = v2; } |
| 1134 | /// into a phi node with a store in the successor. |
| 1135 | /// |
| 1136 | bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) { |
| 1137 | BasicBlock *StoreBB = SI.getParent(); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1138 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1139 | // Check to see if the successor block has exactly two incoming edges. If |
| 1140 | // so, see if the other predecessor contains a store to the same location. |
| 1141 | // if so, insert a PHI node (if needed) and move the stores down. |
| 1142 | BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1143 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1144 | // Determine whether Dest has exactly two predecessors and, if so, compute |
| 1145 | // the other predecessor. |
| 1146 | pred_iterator PI = pred_begin(DestBB); |
Gabor Greif | 1b787df | 2010-07-12 15:48:26 +0000 | [diff] [blame] | 1147 | BasicBlock *P = *PI; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1148 | BasicBlock *OtherBB = nullptr; |
Gabor Greif | 1b787df | 2010-07-12 15:48:26 +0000 | [diff] [blame] | 1149 | |
| 1150 | if (P != StoreBB) |
| 1151 | OtherBB = P; |
| 1152 | |
| 1153 | if (++PI == pred_end(DestBB)) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1154 | return false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1155 | |
Gabor Greif | 1b787df | 2010-07-12 15:48:26 +0000 | [diff] [blame] | 1156 | P = *PI; |
| 1157 | if (P != StoreBB) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1158 | if (OtherBB) |
| 1159 | return false; |
Gabor Greif | 1b787df | 2010-07-12 15:48:26 +0000 | [diff] [blame] | 1160 | OtherBB = P; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1161 | } |
| 1162 | if (++PI != pred_end(DestBB)) |
| 1163 | return false; |
| 1164 | |
| 1165 | // Bail out if all the relevant blocks aren't distinct (this can happen, |
| 1166 | // for example, if SI is in an infinite loop) |
| 1167 | if (StoreBB == DestBB || OtherBB == DestBB) |
| 1168 | return false; |
| 1169 | |
| 1170 | // Verify that the other block ends in a branch and is not otherwise empty. |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 1171 | BasicBlock::iterator BBI(OtherBB->getTerminator()); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1172 | BranchInst *OtherBr = dyn_cast<BranchInst>(BBI); |
| 1173 | if (!OtherBr || BBI == OtherBB->begin()) |
| 1174 | return false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1175 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1176 | // If the other block ends in an unconditional branch, check for the 'if then |
| 1177 | // else' case. there is an instruction before the branch. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1178 | StoreInst *OtherStore = nullptr; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1179 | if (OtherBr->isUnconditional()) { |
| 1180 | --BBI; |
| 1181 | // Skip over debugging info. |
Victor Hernandez | 5f8c8c0 | 2010-01-22 19:05:05 +0000 | [diff] [blame] | 1182 | while (isa<DbgInfoIntrinsic>(BBI) || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1183 | (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1184 | if (BBI==OtherBB->begin()) |
| 1185 | return false; |
| 1186 | --BBI; |
| 1187 | } |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1188 | // If this isn't a store, isn't a store to the same location, or is not the |
| 1189 | // right kind of store, bail out. |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1190 | OtherStore = dyn_cast<StoreInst>(BBI); |
| 1191 | if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1) || |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1192 | !SI.isSameOperationAs(OtherStore)) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1193 | return false; |
| 1194 | } else { |
| 1195 | // Otherwise, the other block ended with a conditional branch. If one of the |
| 1196 | // destinations is StoreBB, then we have the if/then case. |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1197 | if (OtherBr->getSuccessor(0) != StoreBB && |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1198 | OtherBr->getSuccessor(1) != StoreBB) |
| 1199 | return false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1200 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1201 | // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an |
| 1202 | // if/then triangle. See if there is a store to the same ptr as SI that |
| 1203 | // lives in OtherBB. |
| 1204 | for (;; --BBI) { |
| 1205 | // Check to see if we find the matching store. |
| 1206 | if ((OtherStore = dyn_cast<StoreInst>(BBI))) { |
| 1207 | if (OtherStore->getOperand(1) != SI.getOperand(1) || |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1208 | !SI.isSameOperationAs(OtherStore)) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1209 | return false; |
| 1210 | break; |
| 1211 | } |
| 1212 | // If we find something that may be using or overwriting the stored |
| 1213 | // value, or if we run out of instructions, we can't do the xform. |
| 1214 | if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() || |
| 1215 | BBI == OtherBB->begin()) |
| 1216 | return false; |
| 1217 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1218 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1219 | // In order to eliminate the store in OtherBr, we have to |
| 1220 | // make sure nothing reads or overwrites the stored value in |
| 1221 | // StoreBB. |
| 1222 | for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) { |
| 1223 | // FIXME: This should really be AA driven. |
| 1224 | if (I->mayReadFromMemory() || I->mayWriteToMemory()) |
| 1225 | return false; |
| 1226 | } |
| 1227 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1228 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1229 | // Insert a PHI node now if we need it. |
| 1230 | Value *MergedVal = OtherStore->getOperand(0); |
| 1231 | if (MergedVal != SI.getOperand(0)) { |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 1232 | PHINode *PN = PHINode::Create(MergedVal->getType(), 2, "storemerge"); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1233 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
| 1234 | PN->addIncoming(OtherStore->getOperand(0), OtherBB); |
| 1235 | MergedVal = InsertNewInstBefore(PN, DestBB->front()); |
| 1236 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1237 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1238 | // Advance to a place where it is safe to insert the new store and |
| 1239 | // insert it. |
Bill Wendling | 8ddfc09 | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 1240 | BBI = DestBB->getFirstInsertionPt(); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 1241 | StoreInst *NewSI = new StoreInst(MergedVal, SI.getOperand(1), |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1242 | SI.isVolatile(), |
| 1243 | SI.getAlignment(), |
| 1244 | SI.getOrdering(), |
| 1245 | SI.getSynchScope()); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 1246 | InsertNewInstBefore(NewSI, *BBI); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1247 | NewSI->setDebugLoc(OtherStore->getDebugLoc()); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 1248 | |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1249 | // If the two stores had AA tags, merge them. |
| 1250 | AAMDNodes AATags; |
| 1251 | SI.getAAMetadata(AATags); |
| 1252 | if (AATags) { |
| 1253 | OtherStore->getAAMetadata(AATags, /* Merge = */ true); |
| 1254 | NewSI->setAAMetadata(AATags); |
| 1255 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1256 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1257 | // Nuke the old stores. |
| 1258 | EraseInstFromFunction(SI); |
| 1259 | EraseInstFromFunction(*OtherStore); |
| 1260 | return true; |
| 1261 | } |