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" |
Yaxun Liu | ba01ed0 | 2017-02-10 21:46:07 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/MapVector.h" |
NAKAMURA Takumi | ec6b1fc | 2015-12-15 09:37:31 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/Loads.h" |
David Blaikie | 31b98d2 | 2018-06-04 21:23:21 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Utils/Local.h" |
Peter Collingbourne | ecdd58f | 2016-10-21 19:59:26 +0000 | [diff] [blame] | 20 | #include "llvm/IR/ConstantRange.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DataLayout.h" |
Vedant Kumar | 238533e | 2018-11-19 19:55:02 +0000 | [diff] [blame^] | 22 | #include "llvm/IR/DebugInfoMetadata.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/IntrinsicInst.h" |
Yaxun Liu | ba01ed0 | 2017-02-10 21:46:07 +0000 | [diff] [blame] | 24 | #include "llvm/IR/LLVMContext.h" |
Charles Davis | 33d1dc0 | 2015-02-25 05:10:25 +0000 | [diff] [blame] | 25 | #include "llvm/IR/MDBuilder.h" |
Alexey Bataev | ec95c6c | 2017-12-08 15:32:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/PatternMatch.h" |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Alexey Bataev | ec95c6c | 2017-12-08 15:32:10 +0000 | [diff] [blame] | 29 | using namespace PatternMatch; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 30 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 31 | #define DEBUG_TYPE "instcombine" |
| 32 | |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 33 | STATISTIC(NumDeadStore, "Number of dead stores eliminated"); |
| 34 | STATISTIC(NumGlobalCopies, "Number of allocas copied from constant global"); |
| 35 | |
| 36 | /// pointsToConstantGlobal - Return true if V (possibly indirectly) points to |
| 37 | /// some part of a constant global variable. This intentionally only accepts |
| 38 | /// constant expressions because we can't rewrite arbitrary instructions. |
| 39 | static bool pointsToConstantGlobal(Value *V) { |
| 40 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 41 | return GV->isConstant(); |
Matt Arsenault | 60728177 | 2014-04-24 00:01:09 +0000 | [diff] [blame] | 42 | |
| 43 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 44 | if (CE->getOpcode() == Instruction::BitCast || |
Matt Arsenault | 60728177 | 2014-04-24 00:01:09 +0000 | [diff] [blame] | 45 | CE->getOpcode() == Instruction::AddrSpaceCast || |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 46 | CE->getOpcode() == Instruction::GetElementPtr) |
| 47 | return pointsToConstantGlobal(CE->getOperand(0)); |
Matt Arsenault | 60728177 | 2014-04-24 00:01:09 +0000 | [diff] [blame] | 48 | } |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 49 | return false; |
| 50 | } |
| 51 | |
| 52 | /// isOnlyCopiedFromConstantGlobal - Recursively walk the uses of a (derived) |
| 53 | /// pointer to an alloca. Ignore any reads of the pointer, return false if we |
| 54 | /// see any stores or other unknown uses. If we see pointer arithmetic, keep |
| 55 | /// track of whether it moves the pointer (with IsOffset) but otherwise traverse |
| 56 | /// the uses. If we see a memcpy/memmove that targets an unoffseted pointer to |
| 57 | /// the alloca, and if the source pointer is a pointer to a constant global, we |
| 58 | /// can optimize this. |
| 59 | static bool |
| 60 | isOnlyCopiedFromConstantGlobal(Value *V, MemTransferInst *&TheCopy, |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 61 | SmallVectorImpl<Instruction *> &ToDelete) { |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 62 | // We track lifetime intrinsics as we encounter them. If we decide to go |
| 63 | // ahead and replace the value with the global, this lets the caller quickly |
| 64 | // eliminate the markers. |
| 65 | |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 66 | SmallVector<std::pair<Value *, bool>, 35> ValuesToInspect; |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 67 | ValuesToInspect.emplace_back(V, false); |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 68 | while (!ValuesToInspect.empty()) { |
| 69 | auto ValuePair = ValuesToInspect.pop_back_val(); |
| 70 | const bool IsOffset = ValuePair.second; |
| 71 | for (auto &U : ValuePair.first->uses()) { |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 72 | auto *I = cast<Instruction>(U.getUser()); |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 73 | |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 74 | if (auto *LI = dyn_cast<LoadInst>(I)) { |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 75 | // Ignore non-volatile loads, they are always ok. |
| 76 | if (!LI->isSimple()) return false; |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 77 | continue; |
| 78 | } |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 79 | |
| 80 | if (isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I)) { |
| 81 | // If uses of the bitcast are ok, we are ok. |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 82 | ValuesToInspect.emplace_back(I, IsOffset); |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 83 | continue; |
| 84 | } |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 85 | if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) { |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 86 | // If the GEP has all zero indices, it doesn't offset the pointer. If it |
| 87 | // doesn't, it does. |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 88 | ValuesToInspect.emplace_back(I, IsOffset || !GEP->hasAllZeroIndices()); |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 89 | continue; |
| 90 | } |
| 91 | |
Benjamin Kramer | 3a09ef6 | 2015-04-10 14:50:08 +0000 | [diff] [blame] | 92 | if (auto CS = CallSite(I)) { |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 93 | // If this is the function being called then we treat it like a load and |
| 94 | // ignore it. |
| 95 | if (CS.isCallee(&U)) |
| 96 | continue; |
| 97 | |
David Majnemer | 02f4787 | 2015-12-23 09:58:41 +0000 | [diff] [blame] | 98 | unsigned DataOpNo = CS.getDataOperandNo(&U); |
| 99 | bool IsArgOperand = CS.isArgOperand(&U); |
| 100 | |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 101 | // Inalloca arguments are clobbered by the call. |
David Majnemer | 02f4787 | 2015-12-23 09:58:41 +0000 | [diff] [blame] | 102 | if (IsArgOperand && CS.isInAllocaArgument(DataOpNo)) |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 103 | return false; |
| 104 | |
| 105 | // If this is a readonly/readnone call site, then we know it is just a |
| 106 | // load (but one that potentially returns the value itself), so we can |
| 107 | // ignore it if we know that the value isn't captured. |
| 108 | if (CS.onlyReadsMemory() && |
David Majnemer | 02f4787 | 2015-12-23 09:58:41 +0000 | [diff] [blame] | 109 | (CS.getInstruction()->use_empty() || CS.doesNotCapture(DataOpNo))) |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 110 | continue; |
| 111 | |
| 112 | // If this is being passed as a byval argument, the caller is making a |
| 113 | // copy, so it is only a read of the alloca. |
David Majnemer | 02f4787 | 2015-12-23 09:58:41 +0000 | [diff] [blame] | 114 | if (IsArgOperand && CS.isByValArgument(DataOpNo)) |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 115 | continue; |
| 116 | } |
| 117 | |
| 118 | // Lifetime intrinsics can be handled by the caller. |
| 119 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 120 | if (II->getIntrinsicID() == Intrinsic::lifetime_start || |
| 121 | II->getIntrinsicID() == Intrinsic::lifetime_end) { |
| 122 | assert(II->use_empty() && "Lifetime markers have no result to use!"); |
| 123 | ToDelete.push_back(II); |
| 124 | continue; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // If this is isn't our memcpy/memmove, reject it as something we can't |
| 129 | // handle. |
| 130 | MemTransferInst *MI = dyn_cast<MemTransferInst>(I); |
| 131 | if (!MI) |
| 132 | return false; |
| 133 | |
| 134 | // If the transfer is using the alloca as a source of the transfer, then |
| 135 | // ignore it since it is a load (unless the transfer is volatile). |
| 136 | if (U.getOperandNo() == 1) { |
| 137 | if (MI->isVolatile()) return false; |
| 138 | continue; |
| 139 | } |
| 140 | |
| 141 | // If we already have seen a copy, reject the second one. |
| 142 | if (TheCopy) return false; |
| 143 | |
| 144 | // If the pointer has been offset from the start of the alloca, we can't |
| 145 | // safely handle this. |
| 146 | if (IsOffset) return false; |
| 147 | |
| 148 | // If the memintrinsic isn't using the alloca as the dest, reject it. |
| 149 | if (U.getOperandNo() != 0) return false; |
| 150 | |
| 151 | // If the source of the memcpy/move is not a constant global, reject it. |
| 152 | if (!pointsToConstantGlobal(MI->getSource())) |
| 153 | return false; |
| 154 | |
| 155 | // Otherwise, the transform is safe. Remember the copy instruction. |
| 156 | TheCopy = MI; |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 157 | } |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 158 | } |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | /// isOnlyCopiedFromConstantGlobal - Return true if the specified alloca is only |
| 163 | /// modified by a copy from a constant global. If we can prove this, we can |
| 164 | /// replace any uses of the alloca with uses of the global directly. |
| 165 | static MemTransferInst * |
| 166 | isOnlyCopiedFromConstantGlobal(AllocaInst *AI, |
| 167 | SmallVectorImpl<Instruction *> &ToDelete) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 168 | MemTransferInst *TheCopy = nullptr; |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 169 | if (isOnlyCopiedFromConstantGlobal(AI, TheCopy, ToDelete)) |
| 170 | return TheCopy; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 171 | return nullptr; |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Vitaly Buka | df19ad4 | 2017-06-24 01:35:19 +0000 | [diff] [blame] | 174 | /// Returns true if V is dereferenceable for size of alloca. |
| 175 | static bool isDereferenceableForAllocaSize(const Value *V, const AllocaInst *AI, |
| 176 | const DataLayout &DL) { |
| 177 | if (AI->isArrayAllocation()) |
| 178 | return false; |
| 179 | uint64_t AllocaSize = DL.getTypeStoreSize(AI->getAllocatedType()); |
| 180 | if (!AllocaSize) |
| 181 | return false; |
| 182 | return isDereferenceableAndAlignedPointer(V, AI->getAlignment(), |
| 183 | APInt(64, AllocaSize), DL); |
| 184 | } |
| 185 | |
Duncan P. N. Exon Smith | c6820ec | 2015-03-13 19:22:03 +0000 | [diff] [blame] | 186 | static Instruction *simplifyAllocaArraySize(InstCombiner &IC, AllocaInst &AI) { |
Duncan P. N. Exon Smith | 720762e | 2015-03-13 19:30:44 +0000 | [diff] [blame] | 187 | // Check for array size of 1 (scalar allocation). |
Duncan P. N. Exon Smith | be95b4a | 2015-03-13 19:42:09 +0000 | [diff] [blame] | 188 | if (!AI.isArrayAllocation()) { |
| 189 | // i32 1 is the canonical array size for scalar allocations. |
| 190 | if (AI.getArraySize()->getType()->isIntegerTy(32)) |
| 191 | return nullptr; |
| 192 | |
| 193 | // Canonicalize it. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 194 | Value *V = IC.Builder.getInt32(1); |
Duncan P. N. Exon Smith | be95b4a | 2015-03-13 19:42:09 +0000 | [diff] [blame] | 195 | AI.setOperand(0, V); |
| 196 | return &AI; |
| 197 | } |
Duncan P. N. Exon Smith | 720762e | 2015-03-13 19:30:44 +0000 | [diff] [blame] | 198 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 199 | // 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] | 200 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
Simon Pilgrim | 82edf8d | 2018-08-13 16:50:20 +0000 | [diff] [blame] | 201 | if (C->getValue().getActiveBits() <= 64) { |
| 202 | Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); |
| 203 | AllocaInst *New = IC.Builder.CreateAlloca(NewTy, nullptr, AI.getName()); |
| 204 | New->setAlignment(AI.getAlignment()); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 205 | |
Simon Pilgrim | 82edf8d | 2018-08-13 16:50:20 +0000 | [diff] [blame] | 206 | // Scan to the end of the allocation instructions, to skip over a block of |
| 207 | // allocas if possible...also skip interleaved debug info |
| 208 | // |
| 209 | BasicBlock::iterator It(New); |
| 210 | while (isa<AllocaInst>(*It) || isa<DbgInfoIntrinsic>(*It)) |
| 211 | ++It; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 212 | |
Simon Pilgrim | 82edf8d | 2018-08-13 16:50:20 +0000 | [diff] [blame] | 213 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 214 | // insert our getelementptr instruction... |
| 215 | // |
| 216 | Type *IdxTy = IC.getDataLayout().getIntPtrType(AI.getType()); |
| 217 | Value *NullIdx = Constant::getNullValue(IdxTy); |
| 218 | Value *Idx[2] = {NullIdx, NullIdx}; |
| 219 | Instruction *GEP = |
| 220 | GetElementPtrInst::CreateInBounds(New, Idx, New->getName() + ".sub"); |
| 221 | IC.InsertNewInstBefore(GEP, *It); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 222 | |
Simon Pilgrim | 82edf8d | 2018-08-13 16:50:20 +0000 | [diff] [blame] | 223 | // Now make everything use the getelementptr instead of the original |
| 224 | // allocation. |
| 225 | return IC.replaceInstUsesWith(AI, GEP); |
| 226 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Duncan P. N. Exon Smith | bb73013 | 2015-03-13 19:26:33 +0000 | [diff] [blame] | 229 | if (isa<UndefValue>(AI.getArraySize())) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 230 | return IC.replaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Duncan P. N. Exon Smith | bb73013 | 2015-03-13 19:26:33 +0000 | [diff] [blame] | 231 | |
Duncan P. N. Exon Smith | 07ff9b0 | 2015-03-13 19:34:55 +0000 | [diff] [blame] | 232 | // Ensure that the alloca array size argument has type intptr_t, so that |
| 233 | // any casting is exposed early. |
| 234 | Type *IntPtrTy = IC.getDataLayout().getIntPtrType(AI.getType()); |
| 235 | if (AI.getArraySize()->getType() != IntPtrTy) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 236 | Value *V = IC.Builder.CreateIntCast(AI.getArraySize(), IntPtrTy, false); |
Duncan P. N. Exon Smith | 07ff9b0 | 2015-03-13 19:34:55 +0000 | [diff] [blame] | 237 | AI.setOperand(0, V); |
| 238 | return &AI; |
| 239 | } |
| 240 | |
Duncan P. N. Exon Smith | c6820ec | 2015-03-13 19:22:03 +0000 | [diff] [blame] | 241 | return nullptr; |
| 242 | } |
| 243 | |
Benjamin Kramer | 03ab8a3 | 2017-02-10 22:26:35 +0000 | [diff] [blame] | 244 | namespace { |
Yaxun Liu | ba01ed0 | 2017-02-10 21:46:07 +0000 | [diff] [blame] | 245 | // If I and V are pointers in different address space, it is not allowed to |
| 246 | // use replaceAllUsesWith since I and V have different types. A |
| 247 | // non-target-specific transformation should not use addrspacecast on V since |
| 248 | // the two address space may be disjoint depending on target. |
| 249 | // |
| 250 | // This class chases down uses of the old pointer until reaching the load |
| 251 | // instructions, then replaces the old pointer in the load instructions with |
| 252 | // the new pointer. If during the chasing it sees bitcast or GEP, it will |
| 253 | // create new bitcast or GEP with the new pointer and use them in the load |
| 254 | // instruction. |
| 255 | class PointerReplacer { |
| 256 | public: |
| 257 | PointerReplacer(InstCombiner &IC) : IC(IC) {} |
| 258 | void replacePointer(Instruction &I, Value *V); |
| 259 | |
| 260 | private: |
| 261 | void findLoadAndReplace(Instruction &I); |
| 262 | void replace(Instruction *I); |
| 263 | Value *getReplacement(Value *I); |
| 264 | |
| 265 | SmallVector<Instruction *, 4> Path; |
| 266 | MapVector<Value *, Value *> WorkMap; |
| 267 | InstCombiner &IC; |
| 268 | }; |
Benjamin Kramer | 03ab8a3 | 2017-02-10 22:26:35 +0000 | [diff] [blame] | 269 | } // end anonymous namespace |
Yaxun Liu | ba01ed0 | 2017-02-10 21:46:07 +0000 | [diff] [blame] | 270 | |
| 271 | void PointerReplacer::findLoadAndReplace(Instruction &I) { |
| 272 | for (auto U : I.users()) { |
| 273 | auto *Inst = dyn_cast<Instruction>(&*U); |
| 274 | if (!Inst) |
| 275 | return; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 276 | LLVM_DEBUG(dbgs() << "Found pointer user: " << *U << '\n'); |
Yaxun Liu | ba01ed0 | 2017-02-10 21:46:07 +0000 | [diff] [blame] | 277 | if (isa<LoadInst>(Inst)) { |
| 278 | for (auto P : Path) |
| 279 | replace(P); |
| 280 | replace(Inst); |
| 281 | } else if (isa<GetElementPtrInst>(Inst) || isa<BitCastInst>(Inst)) { |
| 282 | Path.push_back(Inst); |
| 283 | findLoadAndReplace(*Inst); |
| 284 | Path.pop_back(); |
| 285 | } else { |
| 286 | return; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | Value *PointerReplacer::getReplacement(Value *V) { |
| 292 | auto Loc = WorkMap.find(V); |
| 293 | if (Loc != WorkMap.end()) |
| 294 | return Loc->second; |
| 295 | return nullptr; |
| 296 | } |
| 297 | |
| 298 | void PointerReplacer::replace(Instruction *I) { |
| 299 | if (getReplacement(I)) |
| 300 | return; |
| 301 | |
| 302 | if (auto *LT = dyn_cast<LoadInst>(I)) { |
| 303 | auto *V = getReplacement(LT->getPointerOperand()); |
| 304 | assert(V && "Operand not replaced"); |
| 305 | auto *NewI = new LoadInst(V); |
| 306 | NewI->takeName(LT); |
| 307 | IC.InsertNewInstWith(NewI, *LT); |
| 308 | IC.replaceInstUsesWith(*LT, NewI); |
| 309 | WorkMap[LT] = NewI; |
| 310 | } else if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) { |
| 311 | auto *V = getReplacement(GEP->getPointerOperand()); |
| 312 | assert(V && "Operand not replaced"); |
| 313 | SmallVector<Value *, 8> Indices; |
| 314 | Indices.append(GEP->idx_begin(), GEP->idx_end()); |
| 315 | auto *NewI = GetElementPtrInst::Create( |
| 316 | V->getType()->getPointerElementType(), V, Indices); |
| 317 | IC.InsertNewInstWith(NewI, *GEP); |
| 318 | NewI->takeName(GEP); |
| 319 | WorkMap[GEP] = NewI; |
| 320 | } else if (auto *BC = dyn_cast<BitCastInst>(I)) { |
| 321 | auto *V = getReplacement(BC->getOperand(0)); |
| 322 | assert(V && "Operand not replaced"); |
| 323 | auto *NewT = PointerType::get(BC->getType()->getPointerElementType(), |
| 324 | V->getType()->getPointerAddressSpace()); |
| 325 | auto *NewI = new BitCastInst(V, NewT); |
| 326 | IC.InsertNewInstWith(NewI, *BC); |
| 327 | NewI->takeName(BC); |
Yaxun Liu | e6d1ce5 | 2017-02-24 20:27:25 +0000 | [diff] [blame] | 328 | WorkMap[BC] = NewI; |
Yaxun Liu | ba01ed0 | 2017-02-10 21:46:07 +0000 | [diff] [blame] | 329 | } else { |
| 330 | llvm_unreachable("should never reach here"); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | void PointerReplacer::replacePointer(Instruction &I, Value *V) { |
Benjamin Kramer | 684c87b | 2017-02-10 22:04:17 +0000 | [diff] [blame] | 335 | #ifndef NDEBUG |
Yaxun Liu | ba01ed0 | 2017-02-10 21:46:07 +0000 | [diff] [blame] | 336 | auto *PT = cast<PointerType>(I.getType()); |
| 337 | auto *NT = cast<PointerType>(V->getType()); |
| 338 | assert(PT != NT && PT->getElementType() == NT->getElementType() && |
| 339 | "Invalid usage"); |
Benjamin Kramer | 684c87b | 2017-02-10 22:04:17 +0000 | [diff] [blame] | 340 | #endif |
Yaxun Liu | ba01ed0 | 2017-02-10 21:46:07 +0000 | [diff] [blame] | 341 | WorkMap[&I] = V; |
| 342 | findLoadAndReplace(I); |
| 343 | } |
| 344 | |
Duncan P. N. Exon Smith | c6820ec | 2015-03-13 19:22:03 +0000 | [diff] [blame] | 345 | Instruction *InstCombiner::visitAllocaInst(AllocaInst &AI) { |
| 346 | if (auto *I = simplifyAllocaArraySize(*this, AI)) |
| 347 | return I; |
| 348 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 349 | if (AI.getAllocatedType()->isSized()) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 350 | // If the alignment is 0 (unspecified), assign it the preferred alignment. |
| 351 | if (AI.getAlignment() == 0) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 352 | AI.setAlignment(DL.getPrefTypeAlignment(AI.getAllocatedType())); |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 353 | |
| 354 | // Move all alloca's of zero byte objects to the entry block and merge them |
| 355 | // together. Note that we only do this for alloca's, because malloc should |
| 356 | // allocate and return a unique pointer, even for a zero byte allocation. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 357 | if (DL.getTypeAllocSize(AI.getAllocatedType()) == 0) { |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 358 | // For a zero sized alloca there is no point in doing an array allocation. |
| 359 | // This is helpful if the array size is a complicated expression not used |
| 360 | // elsewhere. |
| 361 | if (AI.isArrayAllocation()) { |
| 362 | AI.setOperand(0, ConstantInt::get(AI.getArraySize()->getType(), 1)); |
| 363 | return &AI; |
| 364 | } |
| 365 | |
| 366 | // Get the first instruction in the entry block. |
| 367 | BasicBlock &EntryBlock = AI.getParent()->getParent()->getEntryBlock(); |
| 368 | Instruction *FirstInst = EntryBlock.getFirstNonPHIOrDbg(); |
| 369 | if (FirstInst != &AI) { |
| 370 | // If the entry block doesn't start with a zero-size alloca then move |
| 371 | // this one to the start of the entry block. There is no problem with |
| 372 | // dominance as the array size was forced to a constant earlier already. |
| 373 | AllocaInst *EntryAI = dyn_cast<AllocaInst>(FirstInst); |
| 374 | if (!EntryAI || !EntryAI->getAllocatedType()->isSized() || |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 375 | DL.getTypeAllocSize(EntryAI->getAllocatedType()) != 0) { |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 376 | AI.moveBefore(FirstInst); |
| 377 | return &AI; |
| 378 | } |
| 379 | |
Richard Osborne | b68053e | 2012-09-18 09:31:44 +0000 | [diff] [blame] | 380 | // If the alignment of the entry block alloca is 0 (unspecified), |
| 381 | // assign it the preferred alignment. |
| 382 | if (EntryAI->getAlignment() == 0) |
| 383 | EntryAI->setAlignment( |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 384 | DL.getPrefTypeAlignment(EntryAI->getAllocatedType())); |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 385 | // Replace this zero-sized alloca with the one at the start of the entry |
| 386 | // block after ensuring that the address will be aligned enough for both |
| 387 | // types. |
Richard Osborne | b68053e | 2012-09-18 09:31:44 +0000 | [diff] [blame] | 388 | unsigned MaxAlign = std::max(EntryAI->getAlignment(), |
| 389 | AI.getAlignment()); |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 390 | EntryAI->setAlignment(MaxAlign); |
| 391 | if (AI.getType() != EntryAI->getType()) |
| 392 | return new BitCastInst(EntryAI, AI.getType()); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 393 | return replaceInstUsesWith(AI, EntryAI); |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 394 | } |
| 395 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Eli Friedman | b14873c | 2012-11-26 23:04:53 +0000 | [diff] [blame] | 398 | if (AI.getAlignment()) { |
Richard Osborne | 2fd29bf | 2012-09-24 17:10:03 +0000 | [diff] [blame] | 399 | // Check to see if this allocation is only modified by a memcpy/memmove from |
| 400 | // a constant global whose alignment is equal to or exceeds that of the |
| 401 | // allocation. If this is the case, we can change all users to use |
| 402 | // the constant global instead. This is commonly produced by the CFE by |
| 403 | // constructs like "void foo() { int A[] = {1,2,3,4,5,6,7,8,9...}; }" if 'A' |
| 404 | // is only subsequently read. |
| 405 | SmallVector<Instruction *, 4> ToDelete; |
| 406 | if (MemTransferInst *Copy = isOnlyCopiedFromConstantGlobal(&AI, ToDelete)) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 407 | unsigned SourceAlign = getOrEnforceKnownAlignment( |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 408 | Copy->getSource(), AI.getAlignment(), DL, &AI, &AC, &DT); |
Vitaly Buka | df19ad4 | 2017-06-24 01:35:19 +0000 | [diff] [blame] | 409 | if (AI.getAlignment() <= SourceAlign && |
| 410 | isDereferenceableForAllocaSize(Copy->getSource(), &AI, DL)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 411 | LLVM_DEBUG(dbgs() << "Found alloca equal to global: " << AI << '\n'); |
| 412 | LLVM_DEBUG(dbgs() << " memcpy = " << *Copy << '\n'); |
Richard Osborne | 2fd29bf | 2012-09-24 17:10:03 +0000 | [diff] [blame] | 413 | for (unsigned i = 0, e = ToDelete.size(); i != e; ++i) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 414 | eraseInstFromFunction(*ToDelete[i]); |
Richard Osborne | 2fd29bf | 2012-09-24 17:10:03 +0000 | [diff] [blame] | 415 | Constant *TheSrc = cast<Constant>(Copy->getSource()); |
Yaxun Liu | ba01ed0 | 2017-02-10 21:46:07 +0000 | [diff] [blame] | 416 | auto *SrcTy = TheSrc->getType(); |
| 417 | auto *DestTy = PointerType::get(AI.getType()->getPointerElementType(), |
| 418 | SrcTy->getPointerAddressSpace()); |
| 419 | Constant *Cast = |
| 420 | ConstantExpr::getPointerBitCastOrAddrSpaceCast(TheSrc, DestTy); |
| 421 | if (AI.getType()->getPointerAddressSpace() == |
| 422 | SrcTy->getPointerAddressSpace()) { |
| 423 | Instruction *NewI = replaceInstUsesWith(AI, Cast); |
| 424 | eraseInstFromFunction(*Copy); |
| 425 | ++NumGlobalCopies; |
| 426 | return NewI; |
| 427 | } else { |
| 428 | PointerReplacer PtrReplacer(*this); |
| 429 | PtrReplacer.replacePointer(AI, Cast); |
| 430 | ++NumGlobalCopies; |
| 431 | } |
Richard Osborne | 2fd29bf | 2012-09-24 17:10:03 +0000 | [diff] [blame] | 432 | } |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Nuno Lopes | 95cc4f3 | 2012-07-09 18:38:20 +0000 | [diff] [blame] | 436 | // At last, use the generic allocation site handler to aggressively remove |
| 437 | // unused allocas. |
| 438 | return visitAllocSite(AI); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Philip Reames | 89e92d2 | 2016-12-01 20:17:06 +0000 | [diff] [blame] | 441 | // Are we allowed to form a atomic load or store of this type? |
| 442 | static bool isSupportedAtomicType(Type *Ty) { |
Vedant Kumar | b3091da | 2018-07-06 20:17:42 +0000 | [diff] [blame] | 443 | return Ty->isIntOrPtrTy() || Ty->isFloatingPointTy(); |
Philip Reames | 89e92d2 | 2016-12-01 20:17:06 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 446 | /// Helper to combine a load to a new type. |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 447 | /// |
| 448 | /// This just does the work of combining a load to a new type. It handles |
| 449 | /// metadata, etc., and returns the new instruction. The \c NewTy should be the |
| 450 | /// loaded *value* type. This will convert it to a pointer, cast the operand to |
| 451 | /// that pointer type, load it, etc. |
| 452 | /// |
| 453 | /// Note that this will create all of the instructions with whatever insert |
| 454 | /// point the \c InstCombiner currently is using. |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 455 | static LoadInst *combineLoadToNewType(InstCombiner &IC, LoadInst &LI, Type *NewTy, |
| 456 | const Twine &Suffix = "") { |
Philip Reames | 89e92d2 | 2016-12-01 20:17:06 +0000 | [diff] [blame] | 457 | assert((!LI.isAtomic() || isSupportedAtomicType(NewTy)) && |
| 458 | "can't fold an atomic load to requested type"); |
Alexey Bataev | 7c9ad0d | 2018-05-21 17:46:34 +0000 | [diff] [blame] | 459 | |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 460 | Value *Ptr = LI.getPointerOperand(); |
| 461 | unsigned AS = LI.getPointerAddressSpace(); |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 462 | SmallVector<std::pair<unsigned, MDNode *>, 8> MD; |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 463 | LI.getAllMetadata(MD); |
| 464 | |
Alexey Bataev | 7c9ad0d | 2018-05-21 17:46:34 +0000 | [diff] [blame] | 465 | Value *NewPtr = nullptr; |
| 466 | if (!(match(Ptr, m_BitCast(m_Value(NewPtr))) && |
| 467 | NewPtr->getType()->getPointerElementType() == NewTy && |
| 468 | NewPtr->getType()->getPointerAddressSpace() == AS)) |
| 469 | NewPtr = IC.Builder.CreateBitCast(Ptr, NewTy->getPointerTo(AS)); |
| 470 | |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 471 | LoadInst *NewLoad = IC.Builder.CreateAlignedLoad( |
Alexey Bataev | 7c9ad0d | 2018-05-21 17:46:34 +0000 | [diff] [blame] | 472 | NewPtr, LI.getAlignment(), LI.isVolatile(), LI.getName() + Suffix); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 473 | NewLoad->setAtomic(LI.getOrdering(), LI.getSyncScopeID()); |
Charles Davis | 33d1dc0 | 2015-02-25 05:10:25 +0000 | [diff] [blame] | 474 | MDBuilder MDB(NewLoad->getContext()); |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 475 | for (const auto &MDPair : MD) { |
| 476 | unsigned ID = MDPair.first; |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 477 | MDNode *N = MDPair.second; |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 478 | // Note, essentially every kind of metadata should be preserved here! This |
| 479 | // routine is supposed to clone a load instruction changing *only its type*. |
| 480 | // The only metadata it makes sense to drop is metadata which is invalidated |
| 481 | // when the pointer type changes. This should essentially never be the case |
| 482 | // in LLVM, but we explicitly switch over only known metadata to be |
| 483 | // conservatively correct. If you are adding metadata to LLVM which pertains |
| 484 | // to loads, you almost certainly want to add it here. |
| 485 | switch (ID) { |
| 486 | case LLVMContext::MD_dbg: |
| 487 | case LLVMContext::MD_tbaa: |
| 488 | case LLVMContext::MD_prof: |
| 489 | case LLVMContext::MD_fpmath: |
| 490 | case LLVMContext::MD_tbaa_struct: |
| 491 | case LLVMContext::MD_invariant_load: |
| 492 | case LLVMContext::MD_alias_scope: |
| 493 | case LLVMContext::MD_noalias: |
Philip Reames | 5a3f5f7 | 2014-10-21 00:13:20 +0000 | [diff] [blame] | 494 | case LLVMContext::MD_nontemporal: |
| 495 | case LLVMContext::MD_mem_parallel_loop_access: |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 496 | // All of these directly apply. |
| 497 | NewLoad->setMetadata(ID, N); |
| 498 | break; |
| 499 | |
Chandler Carruth | 87fdafc | 2015-02-13 02:30:01 +0000 | [diff] [blame] | 500 | case LLVMContext::MD_nonnull: |
Chandler Carruth | 2abb65a | 2017-06-26 03:31:31 +0000 | [diff] [blame] | 501 | copyNonnullMetadata(LI, N, *NewLoad); |
Chandler Carruth | 87fdafc | 2015-02-13 02:30:01 +0000 | [diff] [blame] | 502 | break; |
Artur Pilipenko | 5c5011d | 2015-11-02 17:53:51 +0000 | [diff] [blame] | 503 | case LLVMContext::MD_align: |
| 504 | case LLVMContext::MD_dereferenceable: |
| 505 | case LLVMContext::MD_dereferenceable_or_null: |
| 506 | // These only directly apply if the new type is also a pointer. |
| 507 | if (NewTy->isPointerTy()) |
| 508 | NewLoad->setMetadata(ID, N); |
| 509 | break; |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 510 | case LLVMContext::MD_range: |
Chandler Carruth | 2abb65a | 2017-06-26 03:31:31 +0000 | [diff] [blame] | 511 | copyRangeMetadata(IC.getDataLayout(), LI, N, *NewLoad); |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 512 | break; |
| 513 | } |
| 514 | } |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 515 | return NewLoad; |
| 516 | } |
| 517 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 518 | /// Combine a store to a new type. |
Chandler Carruth | fa11d83 | 2015-01-22 03:34:54 +0000 | [diff] [blame] | 519 | /// |
| 520 | /// Returns the newly created store instruction. |
| 521 | static StoreInst *combineStoreToNewValue(InstCombiner &IC, StoreInst &SI, Value *V) { |
Philip Reames | 89e92d2 | 2016-12-01 20:17:06 +0000 | [diff] [blame] | 522 | assert((!SI.isAtomic() || isSupportedAtomicType(V->getType())) && |
| 523 | "can't fold an atomic store of requested type"); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 524 | |
Chandler Carruth | fa11d83 | 2015-01-22 03:34:54 +0000 | [diff] [blame] | 525 | Value *Ptr = SI.getPointerOperand(); |
| 526 | unsigned AS = SI.getPointerAddressSpace(); |
| 527 | SmallVector<std::pair<unsigned, MDNode *>, 8> MD; |
| 528 | SI.getAllMetadata(MD); |
| 529 | |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 530 | StoreInst *NewStore = IC.Builder.CreateAlignedStore( |
| 531 | V, IC.Builder.CreateBitCast(Ptr, V->getType()->getPointerTo(AS)), |
Philip Reames | 6f4d008 | 2016-05-06 22:17:01 +0000 | [diff] [blame] | 532 | SI.getAlignment(), SI.isVolatile()); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 533 | NewStore->setAtomic(SI.getOrdering(), SI.getSyncScopeID()); |
Chandler Carruth | fa11d83 | 2015-01-22 03:34:54 +0000 | [diff] [blame] | 534 | for (const auto &MDPair : MD) { |
| 535 | unsigned ID = MDPair.first; |
| 536 | MDNode *N = MDPair.second; |
| 537 | // Note, essentially every kind of metadata should be preserved here! This |
| 538 | // routine is supposed to clone a store instruction changing *only its |
| 539 | // type*. The only metadata it makes sense to drop is metadata which is |
| 540 | // invalidated when the pointer type changes. This should essentially |
| 541 | // never be the case in LLVM, but we explicitly switch over only known |
| 542 | // metadata to be conservatively correct. If you are adding metadata to |
| 543 | // LLVM which pertains to stores, you almost certainly want to add it |
| 544 | // here. |
| 545 | switch (ID) { |
| 546 | case LLVMContext::MD_dbg: |
| 547 | case LLVMContext::MD_tbaa: |
| 548 | case LLVMContext::MD_prof: |
| 549 | case LLVMContext::MD_fpmath: |
| 550 | case LLVMContext::MD_tbaa_struct: |
| 551 | case LLVMContext::MD_alias_scope: |
| 552 | case LLVMContext::MD_noalias: |
| 553 | case LLVMContext::MD_nontemporal: |
| 554 | case LLVMContext::MD_mem_parallel_loop_access: |
Chandler Carruth | fa11d83 | 2015-01-22 03:34:54 +0000 | [diff] [blame] | 555 | // All of these directly apply. |
| 556 | NewStore->setMetadata(ID, N); |
| 557 | break; |
| 558 | |
| 559 | case LLVMContext::MD_invariant_load: |
Chandler Carruth | 87fdafc | 2015-02-13 02:30:01 +0000 | [diff] [blame] | 560 | case LLVMContext::MD_nonnull: |
Chandler Carruth | fa11d83 | 2015-01-22 03:34:54 +0000 | [diff] [blame] | 561 | case LLVMContext::MD_range: |
Artur Pilipenko | 5c5011d | 2015-11-02 17:53:51 +0000 | [diff] [blame] | 562 | case LLVMContext::MD_align: |
| 563 | case LLVMContext::MD_dereferenceable: |
| 564 | case LLVMContext::MD_dereferenceable_or_null: |
Chandler Carruth | 87fdafc | 2015-02-13 02:30:01 +0000 | [diff] [blame] | 565 | // These don't apply for stores. |
Chandler Carruth | fa11d83 | 2015-01-22 03:34:54 +0000 | [diff] [blame] | 566 | break; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | return NewStore; |
| 571 | } |
| 572 | |
Alexey Bataev | ec95c6c | 2017-12-08 15:32:10 +0000 | [diff] [blame] | 573 | /// Returns true if instruction represent minmax pattern like: |
| 574 | /// select ((cmp load V1, load V2), V1, V2). |
| 575 | static bool isMinMaxWithLoads(Value *V) { |
| 576 | assert(V->getType()->isPointerTy() && "Expected pointer type."); |
| 577 | // Ignore possible ty* to ixx* bitcast. |
| 578 | V = peekThroughBitcast(V); |
| 579 | // Check that select is select ((cmp load V1, load V2), V1, V2) - minmax |
| 580 | // pattern. |
| 581 | CmpInst::Predicate Pred; |
| 582 | Instruction *L1; |
| 583 | Instruction *L2; |
| 584 | Value *LHS; |
| 585 | Value *RHS; |
| 586 | if (!match(V, m_Select(m_Cmp(Pred, m_Instruction(L1), m_Instruction(L2)), |
| 587 | m_Value(LHS), m_Value(RHS)))) |
| 588 | return false; |
| 589 | return (match(L1, m_Load(m_Specific(LHS))) && |
| 590 | match(L2, m_Load(m_Specific(RHS)))) || |
| 591 | (match(L1, m_Load(m_Specific(RHS))) && |
| 592 | match(L2, m_Load(m_Specific(LHS)))); |
| 593 | } |
| 594 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 595 | /// Combine loads to match the type of their uses' value after looking |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 596 | /// through intervening bitcasts. |
| 597 | /// |
| 598 | /// The core idea here is that if the result of a load is used in an operation, |
| 599 | /// we should load the type most conducive to that operation. For example, when |
| 600 | /// loading an integer and converting that immediately to a pointer, we should |
| 601 | /// instead directly load a pointer. |
| 602 | /// |
| 603 | /// However, this routine must never change the width of a load or the number of |
| 604 | /// loads as that would introduce a semantic change. This combine is expected to |
| 605 | /// be a semantic no-op which just allows loads to more closely model the types |
| 606 | /// of their consuming operations. |
| 607 | /// |
| 608 | /// Currently, we also refuse to change the precise type used for an atomic load |
| 609 | /// or a volatile load. This is debatable, and might be reasonable to change |
| 610 | /// later. However, it is risky in case some backend or other part of LLVM is |
| 611 | /// relying on the exact type loaded to select appropriate atomic operations. |
| 612 | static Instruction *combineLoadToOperationType(InstCombiner &IC, LoadInst &LI) { |
Philip Reames | 6f4d008 | 2016-05-06 22:17:01 +0000 | [diff] [blame] | 613 | // FIXME: We could probably with some care handle both volatile and ordered |
| 614 | // atomic loads here but it isn't clear that this is important. |
| 615 | if (!LI.isUnordered()) |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 616 | return nullptr; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 617 | |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 618 | if (LI.use_empty()) |
| 619 | return nullptr; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 620 | |
Arnold Schwaighofer | 5d33555 | 2016-09-10 18:14:57 +0000 | [diff] [blame] | 621 | // swifterror values can't be bitcasted. |
| 622 | if (LI.getPointerOperand()->isSwiftError()) |
| 623 | return nullptr; |
| 624 | |
Chandler Carruth | cd8522e | 2015-01-22 05:08:12 +0000 | [diff] [blame] | 625 | Type *Ty = LI.getType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 626 | const DataLayout &DL = IC.getDataLayout(); |
Chandler Carruth | cd8522e | 2015-01-22 05:08:12 +0000 | [diff] [blame] | 627 | |
| 628 | // Try to canonicalize loads which are only ever stored to operate over |
| 629 | // integers instead of any other type. We only do this when the loaded type |
| 630 | // is sized and has a size exactly the same as its store size and the store |
| 631 | // size is a legal integer type. |
Alexey Bataev | ec95c6c | 2017-12-08 15:32:10 +0000 | [diff] [blame] | 632 | // Do not perform canonicalization if minmax pattern is found (to avoid |
| 633 | // infinite loop). |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 634 | if (!Ty->isIntegerTy() && Ty->isSized() && |
| 635 | DL.isLegalInteger(DL.getTypeStoreSizeInBits(Ty)) && |
Sanjoy Das | ba04d3a | 2016-08-06 02:58:48 +0000 | [diff] [blame] | 636 | DL.getTypeStoreSizeInBits(Ty) == DL.getTypeSizeInBits(Ty) && |
Alexey Bataev | ec95c6c | 2017-12-08 15:32:10 +0000 | [diff] [blame] | 637 | !DL.isNonIntegralPointerType(Ty) && |
| 638 | !isMinMaxWithLoads( |
| 639 | peekThroughBitcast(LI.getPointerOperand(), /*OneUseOnly=*/true))) { |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 640 | if (all_of(LI.users(), [&LI](User *U) { |
Chandler Carruth | cd8522e | 2015-01-22 05:08:12 +0000 | [diff] [blame] | 641 | auto *SI = dyn_cast<StoreInst>(U); |
Arnold Schwaighofer | c368563 | 2017-01-31 17:53:49 +0000 | [diff] [blame] | 642 | return SI && SI->getPointerOperand() != &LI && |
| 643 | !SI->getPointerOperand()->isSwiftError(); |
Chandler Carruth | cd8522e | 2015-01-22 05:08:12 +0000 | [diff] [blame] | 644 | })) { |
| 645 | LoadInst *NewLoad = combineLoadToNewType( |
| 646 | IC, LI, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 647 | Type::getIntNTy(LI.getContext(), DL.getTypeStoreSizeInBits(Ty))); |
Chandler Carruth | cd8522e | 2015-01-22 05:08:12 +0000 | [diff] [blame] | 648 | // Replace all the stores with stores of the newly loaded value. |
| 649 | for (auto UI = LI.user_begin(), UE = LI.user_end(); UI != UE;) { |
| 650 | auto *SI = cast<StoreInst>(*UI++); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 651 | IC.Builder.SetInsertPoint(SI); |
Chandler Carruth | cd8522e | 2015-01-22 05:08:12 +0000 | [diff] [blame] | 652 | combineStoreToNewValue(IC, *SI, NewLoad); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 653 | IC.eraseInstFromFunction(*SI); |
Chandler Carruth | cd8522e | 2015-01-22 05:08:12 +0000 | [diff] [blame] | 654 | } |
| 655 | assert(LI.use_empty() && "Failed to remove all users of the load!"); |
| 656 | // Return the old load so the combiner can delete it safely. |
| 657 | return &LI; |
| 658 | } |
| 659 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 660 | |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 661 | // Fold away bit casts of the loaded value by loading the desired type. |
Quentin Colombet | 490cfbe | 2016-02-11 22:30:41 +0000 | [diff] [blame] | 662 | // We can do this for BitCastInsts as well as casts from and to pointer types, |
| 663 | // as long as those are noops (i.e., the source or dest type have the same |
| 664 | // bitwidth as the target's pointers). |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 665 | if (LI.hasOneUse()) |
Philip Reames | 89e92d2 | 2016-12-01 20:17:06 +0000 | [diff] [blame] | 666 | if (auto* CI = dyn_cast<CastInst>(LI.user_back())) |
| 667 | if (CI->isNoopCast(DL)) |
| 668 | if (!LI.isAtomic() || isSupportedAtomicType(CI->getDestTy())) { |
| 669 | LoadInst *NewLoad = combineLoadToNewType(IC, LI, CI->getDestTy()); |
| 670 | CI->replaceAllUsesWith(NewLoad); |
| 671 | IC.eraseInstFromFunction(*CI); |
| 672 | return &LI; |
| 673 | } |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 674 | |
Chandler Carruth | a7f247e | 2014-12-09 19:21:16 +0000 | [diff] [blame] | 675 | // FIXME: We should also canonicalize loads of vectors when their elements are |
| 676 | // cast to other types. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 677 | return nullptr; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 680 | static Instruction *unpackLoadToAggregate(InstCombiner &IC, LoadInst &LI) { |
| 681 | // FIXME: We could probably with some care handle both volatile and atomic |
| 682 | // stores here but it isn't clear that this is important. |
| 683 | if (!LI.isSimple()) |
| 684 | return nullptr; |
| 685 | |
| 686 | Type *T = LI.getType(); |
| 687 | if (!T->isAggregateType()) |
| 688 | return nullptr; |
| 689 | |
Benjamin Kramer | c126353 | 2016-03-11 10:20:56 +0000 | [diff] [blame] | 690 | StringRef Name = LI.getName(); |
Bruce Mitchener | e9ffb45 | 2015-09-12 01:17:08 +0000 | [diff] [blame] | 691 | assert(LI.getAlignment() && "Alignment must be set at this point"); |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 692 | |
| 693 | if (auto *ST = dyn_cast<StructType>(T)) { |
| 694 | // If the struct only have one element, we unpack. |
Amaury Sechet | 61a7d62 | 2016-02-17 19:21:28 +0000 | [diff] [blame] | 695 | auto NumElements = ST->getNumElements(); |
| 696 | if (NumElements == 1) { |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 697 | LoadInst *NewLoad = combineLoadToNewType(IC, LI, ST->getTypeAtIndex(0U), |
| 698 | ".unpack"); |
Keno Fischer | a236dae | 2017-06-28 23:36:40 +0000 | [diff] [blame] | 699 | AAMDNodes AAMD; |
| 700 | LI.getAAMetadata(AAMD); |
| 701 | NewLoad->setAAMetadata(AAMD); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 702 | return IC.replaceInstUsesWith(LI, IC.Builder.CreateInsertValue( |
Amaury Sechet | 61a7d62 | 2016-02-17 19:21:28 +0000 | [diff] [blame] | 703 | UndefValue::get(T), NewLoad, 0, Name)); |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 704 | } |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 705 | |
| 706 | // We don't want to break loads with padding here as we'd loose |
| 707 | // the knowledge that padding exists for the rest of the pipeline. |
| 708 | const DataLayout &DL = IC.getDataLayout(); |
| 709 | auto *SL = DL.getStructLayout(ST); |
| 710 | if (SL->hasPadding()) |
| 711 | return nullptr; |
| 712 | |
Amaury Sechet | 61a7d62 | 2016-02-17 19:21:28 +0000 | [diff] [blame] | 713 | auto Align = LI.getAlignment(); |
| 714 | if (!Align) |
| 715 | Align = DL.getABITypeAlignment(ST); |
| 716 | |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 717 | auto *Addr = LI.getPointerOperand(); |
Amaury Sechet | 61a7d62 | 2016-02-17 19:21:28 +0000 | [diff] [blame] | 718 | auto *IdxType = Type::getInt32Ty(T->getContext()); |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 719 | auto *Zero = ConstantInt::get(IdxType, 0); |
Amaury Sechet | 61a7d62 | 2016-02-17 19:21:28 +0000 | [diff] [blame] | 720 | |
| 721 | Value *V = UndefValue::get(T); |
| 722 | for (unsigned i = 0; i < NumElements; i++) { |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 723 | Value *Indices[2] = { |
| 724 | Zero, |
| 725 | ConstantInt::get(IdxType, i), |
| 726 | }; |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 727 | auto *Ptr = IC.Builder.CreateInBoundsGEP(ST, Addr, makeArrayRef(Indices), |
| 728 | Name + ".elt"); |
Amaury Sechet | 61a7d62 | 2016-02-17 19:21:28 +0000 | [diff] [blame] | 729 | auto EltAlign = MinAlign(Align, SL->getElementOffset(i)); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 730 | auto *L = IC.Builder.CreateAlignedLoad(Ptr, EltAlign, Name + ".unpack"); |
Keno Fischer | a236dae | 2017-06-28 23:36:40 +0000 | [diff] [blame] | 731 | // Propagate AA metadata. It'll still be valid on the narrowed load. |
| 732 | AAMDNodes AAMD; |
| 733 | LI.getAAMetadata(AAMD); |
| 734 | L->setAAMetadata(AAMD); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 735 | V = IC.Builder.CreateInsertValue(V, L, i); |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | V->setName(Name); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 739 | return IC.replaceInstUsesWith(LI, V); |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 740 | } |
| 741 | |
David Majnemer | 58fb038 | 2015-05-11 05:04:22 +0000 | [diff] [blame] | 742 | if (auto *AT = dyn_cast<ArrayType>(T)) { |
Amaury Sechet | 7cd3fe7 | 2016-03-02 21:28:30 +0000 | [diff] [blame] | 743 | auto *ET = AT->getElementType(); |
| 744 | auto NumElements = AT->getNumElements(); |
| 745 | if (NumElements == 1) { |
| 746 | LoadInst *NewLoad = combineLoadToNewType(IC, LI, ET, ".unpack"); |
Keno Fischer | a236dae | 2017-06-28 23:36:40 +0000 | [diff] [blame] | 747 | AAMDNodes AAMD; |
| 748 | LI.getAAMetadata(AAMD); |
| 749 | NewLoad->setAAMetadata(AAMD); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 750 | return IC.replaceInstUsesWith(LI, IC.Builder.CreateInsertValue( |
Amaury Sechet | 7cd3fe7 | 2016-03-02 21:28:30 +0000 | [diff] [blame] | 751 | UndefValue::get(T), NewLoad, 0, Name)); |
David Majnemer | 58fb038 | 2015-05-11 05:04:22 +0000 | [diff] [blame] | 752 | } |
Amaury Sechet | 7cd3fe7 | 2016-03-02 21:28:30 +0000 | [diff] [blame] | 753 | |
Davide Italiano | da11412 | 2016-10-07 20:57:42 +0000 | [diff] [blame] | 754 | // Bail out if the array is too large. Ideally we would like to optimize |
| 755 | // arrays of arbitrary size but this has a terrible impact on compile time. |
| 756 | // The threshold here is chosen arbitrarily, maybe needs a little bit of |
| 757 | // tuning. |
Davide Italiano | 2133bf5 | 2017-02-07 17:56:50 +0000 | [diff] [blame] | 758 | if (NumElements > IC.MaxArraySizeForCombine) |
Davide Italiano | da11412 | 2016-10-07 20:57:42 +0000 | [diff] [blame] | 759 | return nullptr; |
| 760 | |
Amaury Sechet | 7cd3fe7 | 2016-03-02 21:28:30 +0000 | [diff] [blame] | 761 | const DataLayout &DL = IC.getDataLayout(); |
| 762 | auto EltSize = DL.getTypeAllocSize(ET); |
| 763 | auto Align = LI.getAlignment(); |
| 764 | if (!Align) |
| 765 | Align = DL.getABITypeAlignment(T); |
| 766 | |
Amaury Sechet | 7cd3fe7 | 2016-03-02 21:28:30 +0000 | [diff] [blame] | 767 | auto *Addr = LI.getPointerOperand(); |
| 768 | auto *IdxType = Type::getInt64Ty(T->getContext()); |
| 769 | auto *Zero = ConstantInt::get(IdxType, 0); |
| 770 | |
| 771 | Value *V = UndefValue::get(T); |
| 772 | uint64_t Offset = 0; |
| 773 | for (uint64_t i = 0; i < NumElements; i++) { |
| 774 | Value *Indices[2] = { |
| 775 | Zero, |
| 776 | ConstantInt::get(IdxType, i), |
| 777 | }; |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 778 | auto *Ptr = IC.Builder.CreateInBoundsGEP(AT, Addr, makeArrayRef(Indices), |
| 779 | Name + ".elt"); |
| 780 | auto *L = IC.Builder.CreateAlignedLoad(Ptr, MinAlign(Align, Offset), |
| 781 | Name + ".unpack"); |
Keno Fischer | a236dae | 2017-06-28 23:36:40 +0000 | [diff] [blame] | 782 | AAMDNodes AAMD; |
| 783 | LI.getAAMetadata(AAMD); |
| 784 | L->setAAMetadata(AAMD); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 785 | V = IC.Builder.CreateInsertValue(V, L, i); |
Amaury Sechet | 7cd3fe7 | 2016-03-02 21:28:30 +0000 | [diff] [blame] | 786 | Offset += EltSize; |
| 787 | } |
| 788 | |
| 789 | V->setName(Name); |
| 790 | return IC.replaceInstUsesWith(LI, V); |
David Majnemer | 58fb038 | 2015-05-11 05:04:22 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 793 | return nullptr; |
| 794 | } |
| 795 | |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 796 | // If we can determine that all possible objects pointed to by the provided |
| 797 | // pointer value are, not only dereferenceable, but also definitively less than |
| 798 | // or equal to the provided maximum size, then return true. Otherwise, return |
| 799 | // false (constant global values and allocas fall into this category). |
| 800 | // |
| 801 | // FIXME: This should probably live in ValueTracking (or similar). |
| 802 | static bool isObjectSizeLessThanOrEq(Value *V, uint64_t MaxSize, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 803 | const DataLayout &DL) { |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 804 | SmallPtrSet<Value *, 4> Visited; |
| 805 | SmallVector<Value *, 4> Worklist(1, V); |
| 806 | |
| 807 | do { |
| 808 | Value *P = Worklist.pop_back_val(); |
| 809 | P = P->stripPointerCasts(); |
| 810 | |
| 811 | if (!Visited.insert(P).second) |
| 812 | continue; |
| 813 | |
| 814 | if (SelectInst *SI = dyn_cast<SelectInst>(P)) { |
| 815 | Worklist.push_back(SI->getTrueValue()); |
| 816 | Worklist.push_back(SI->getFalseValue()); |
| 817 | continue; |
| 818 | } |
| 819 | |
| 820 | if (PHINode *PN = dyn_cast<PHINode>(P)) { |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 821 | for (Value *IncValue : PN->incoming_values()) |
| 822 | Worklist.push_back(IncValue); |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 823 | continue; |
| 824 | } |
| 825 | |
| 826 | if (GlobalAlias *GA = dyn_cast<GlobalAlias>(P)) { |
Sanjoy Das | 9904247 | 2016-04-17 04:30:43 +0000 | [diff] [blame] | 827 | if (GA->isInterposable()) |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 828 | return false; |
| 829 | Worklist.push_back(GA->getAliasee()); |
| 830 | continue; |
| 831 | } |
| 832 | |
| 833 | // If we know how big this object is, and it is less than MaxSize, continue |
| 834 | // searching. Otherwise, return false. |
| 835 | if (AllocaInst *AI = dyn_cast<AllocaInst>(P)) { |
| 836 | if (!AI->getAllocatedType()->isSized()) |
| 837 | return false; |
| 838 | |
| 839 | ConstantInt *CS = dyn_cast<ConstantInt>(AI->getArraySize()); |
| 840 | if (!CS) |
| 841 | return false; |
| 842 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 843 | uint64_t TypeSize = DL.getTypeAllocSize(AI->getAllocatedType()); |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 844 | // Make sure that, even if the multiplication below would wrap as an |
| 845 | // uint64_t, we still do the right thing. |
| 846 | if ((CS->getValue().zextOrSelf(128)*APInt(128, TypeSize)).ugt(MaxSize)) |
| 847 | return false; |
| 848 | continue; |
| 849 | } |
| 850 | |
| 851 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) { |
| 852 | if (!GV->hasDefinitiveInitializer() || !GV->isConstant()) |
| 853 | return false; |
| 854 | |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 855 | uint64_t InitSize = DL.getTypeAllocSize(GV->getValueType()); |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 856 | if (InitSize > MaxSize) |
| 857 | return false; |
| 858 | continue; |
| 859 | } |
| 860 | |
| 861 | return false; |
| 862 | } while (!Worklist.empty()); |
| 863 | |
| 864 | return true; |
| 865 | } |
| 866 | |
| 867 | // If we're indexing into an object of a known size, and the outer index is |
| 868 | // not a constant, but having any value but zero would lead to undefined |
| 869 | // behavior, replace it with zero. |
| 870 | // |
| 871 | // For example, if we have: |
| 872 | // @f.a = private unnamed_addr constant [1 x i32] [i32 12], align 4 |
| 873 | // ... |
| 874 | // %arrayidx = getelementptr inbounds [1 x i32]* @f.a, i64 0, i64 %x |
| 875 | // ... = load i32* %arrayidx, align 4 |
| 876 | // Then we know that we can replace %x in the GEP with i64 0. |
| 877 | // |
| 878 | // FIXME: We could fold any GEP index to zero that would cause UB if it were |
| 879 | // not zero. Currently, we only handle the first such index. Also, we could |
| 880 | // also search through non-zero constant indices if we kept track of the |
| 881 | // offsets those indices implied. |
| 882 | static bool canReplaceGEPIdxWithZero(InstCombiner &IC, GetElementPtrInst *GEPI, |
| 883 | Instruction *MemI, unsigned &Idx) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 884 | if (GEPI->getNumOperands() < 2) |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 885 | return false; |
| 886 | |
| 887 | // Find the first non-zero index of a GEP. If all indices are zero, return |
| 888 | // one past the last index. |
| 889 | auto FirstNZIdx = [](const GetElementPtrInst *GEPI) { |
| 890 | unsigned I = 1; |
| 891 | for (unsigned IE = GEPI->getNumOperands(); I != IE; ++I) { |
| 892 | Value *V = GEPI->getOperand(I); |
| 893 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 894 | if (CI->isZero()) |
| 895 | continue; |
| 896 | |
| 897 | break; |
| 898 | } |
| 899 | |
| 900 | return I; |
| 901 | }; |
| 902 | |
| 903 | // Skip through initial 'zero' indices, and find the corresponding pointer |
| 904 | // type. See if the next index is not a constant. |
| 905 | Idx = FirstNZIdx(GEPI); |
| 906 | if (Idx == GEPI->getNumOperands()) |
| 907 | return false; |
| 908 | if (isa<Constant>(GEPI->getOperand(Idx))) |
| 909 | return false; |
| 910 | |
| 911 | SmallVector<Value *, 4> Ops(GEPI->idx_begin(), GEPI->idx_begin() + Idx); |
Eduard Burtescu | 19eb031 | 2016-01-19 17:28:00 +0000 | [diff] [blame] | 912 | Type *AllocTy = |
| 913 | GetElementPtrInst::getIndexedType(GEPI->getSourceElementType(), Ops); |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 914 | if (!AllocTy || !AllocTy->isSized()) |
| 915 | return false; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 916 | const DataLayout &DL = IC.getDataLayout(); |
| 917 | uint64_t TyAllocSize = DL.getTypeAllocSize(AllocTy); |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 918 | |
| 919 | // If there are more indices after the one we might replace with a zero, make |
| 920 | // sure they're all non-negative. If any of them are negative, the overall |
| 921 | // address being computed might be before the base address determined by the |
| 922 | // first non-zero index. |
| 923 | auto IsAllNonNegative = [&]() { |
| 924 | for (unsigned i = Idx+1, e = GEPI->getNumOperands(); i != e; ++i) { |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 925 | KnownBits Known = IC.computeKnownBits(GEPI->getOperand(i), 0, MemI); |
| 926 | if (Known.isNonNegative()) |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 927 | continue; |
| 928 | return false; |
| 929 | } |
| 930 | |
| 931 | return true; |
| 932 | }; |
| 933 | |
| 934 | // FIXME: If the GEP is not inbounds, and there are extra indices after the |
| 935 | // one we'll replace, those could cause the address computation to wrap |
| 936 | // (rendering the IsAllNonNegative() check below insufficient). We can do |
Bruce Mitchener | e9ffb45 | 2015-09-12 01:17:08 +0000 | [diff] [blame] | 937 | // better, ignoring zero indices (and other indices we can prove small |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 938 | // enough not to wrap). |
| 939 | if (Idx+1 != GEPI->getNumOperands() && !GEPI->isInBounds()) |
| 940 | return false; |
| 941 | |
| 942 | // Note that isObjectSizeLessThanOrEq will return true only if the pointer is |
| 943 | // also known to be dereferenceable. |
| 944 | return isObjectSizeLessThanOrEq(GEPI->getOperand(0), TyAllocSize, DL) && |
| 945 | IsAllNonNegative(); |
| 946 | } |
| 947 | |
| 948 | // If we're indexing into an object with a variable index for the memory |
| 949 | // access, but the object has only one element, we can assume that the index |
| 950 | // will always be zero. If we replace the GEP, return it. |
| 951 | template <typename T> |
| 952 | static Instruction *replaceGEPIdxWithZero(InstCombiner &IC, Value *Ptr, |
| 953 | T &MemI) { |
| 954 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Ptr)) { |
| 955 | unsigned Idx; |
| 956 | if (canReplaceGEPIdxWithZero(IC, GEPI, &MemI, Idx)) { |
| 957 | Instruction *NewGEPI = GEPI->clone(); |
| 958 | NewGEPI->setOperand(Idx, |
| 959 | ConstantInt::get(GEPI->getOperand(Idx)->getType(), 0)); |
| 960 | NewGEPI->insertBefore(GEPI); |
| 961 | MemI.setOperand(MemI.getPointerOperandIndex(), NewGEPI); |
| 962 | return NewGEPI; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | return nullptr; |
| 967 | } |
| 968 | |
Anna Thomas | 2dd9835 | 2017-12-12 14:12:33 +0000 | [diff] [blame] | 969 | static bool canSimplifyNullStoreOrGEP(StoreInst &SI) { |
Manoj Gupta | 77eeac3 | 2018-07-09 22:27:23 +0000 | [diff] [blame] | 970 | if (NullPointerIsDefined(SI.getFunction(), SI.getPointerAddressSpace())) |
Anna Thomas | 2dd9835 | 2017-12-12 14:12:33 +0000 | [diff] [blame] | 971 | return false; |
| 972 | |
| 973 | auto *Ptr = SI.getPointerOperand(); |
| 974 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Ptr)) |
| 975 | Ptr = GEPI->getOperand(0); |
Manoj Gupta | 77eeac3 | 2018-07-09 22:27:23 +0000 | [diff] [blame] | 976 | return (isa<ConstantPointerNull>(Ptr) && |
| 977 | !NullPointerIsDefined(SI.getFunction(), SI.getPointerAddressSpace())); |
Anna Thomas | 2dd9835 | 2017-12-12 14:12:33 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Davide Italiano | ffcb4df | 2017-04-19 17:26:57 +0000 | [diff] [blame] | 980 | static bool canSimplifyNullLoadOrGEP(LoadInst &LI, Value *Op) { |
| 981 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 982 | const Value *GEPI0 = GEPI->getOperand(0); |
Manoj Gupta | 77eeac3 | 2018-07-09 22:27:23 +0000 | [diff] [blame] | 983 | if (isa<ConstantPointerNull>(GEPI0) && |
| 984 | !NullPointerIsDefined(LI.getFunction(), GEPI->getPointerAddressSpace())) |
Davide Italiano | ffcb4df | 2017-04-19 17:26:57 +0000 | [diff] [blame] | 985 | return true; |
| 986 | } |
| 987 | if (isa<UndefValue>(Op) || |
Manoj Gupta | 77eeac3 | 2018-07-09 22:27:23 +0000 | [diff] [blame] | 988 | (isa<ConstantPointerNull>(Op) && |
| 989 | !NullPointerIsDefined(LI.getFunction(), LI.getPointerAddressSpace()))) |
Davide Italiano | ffcb4df | 2017-04-19 17:26:57 +0000 | [diff] [blame] | 990 | return true; |
| 991 | return false; |
| 992 | } |
| 993 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 994 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 995 | Value *Op = LI.getOperand(0); |
| 996 | |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 997 | // Try to canonicalize the loaded type. |
| 998 | if (Instruction *Res = combineLoadToOperationType(*this, LI)) |
| 999 | return Res; |
| 1000 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1001 | // Attempt to improve the alignment. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1002 | unsigned KnownAlign = getOrEnforceKnownAlignment( |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1003 | Op, DL.getPrefTypeAlignment(LI.getType()), DL, &LI, &AC, &DT); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1004 | unsigned LoadAlign = LI.getAlignment(); |
| 1005 | unsigned EffectiveLoadAlign = |
| 1006 | LoadAlign != 0 ? LoadAlign : DL.getABITypeAlignment(LI.getType()); |
Dan Gohman | 3619660 | 2010-08-03 18:20:32 +0000 | [diff] [blame] | 1007 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1008 | if (KnownAlign > EffectiveLoadAlign) |
| 1009 | LI.setAlignment(KnownAlign); |
| 1010 | else if (LoadAlign == 0) |
| 1011 | LI.setAlignment(EffectiveLoadAlign); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1012 | |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 1013 | // Replace GEP indices if possible. |
| 1014 | if (Instruction *NewGEPI = replaceGEPIdxWithZero(*this, Op, LI)) { |
| 1015 | Worklist.Add(NewGEPI); |
| 1016 | return &LI; |
| 1017 | } |
| 1018 | |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 1019 | if (Instruction *Res = unpackLoadToAggregate(*this, LI)) |
| 1020 | return Res; |
| 1021 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1022 | // 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] | 1023 | // where there are several consecutive memory accesses to the same location, |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1024 | // separated by a few arithmetic operations. |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 1025 | BasicBlock::iterator BBI(LI); |
Eli Friedman | bd254a6 | 2016-06-16 02:33:42 +0000 | [diff] [blame] | 1026 | bool IsLoadCSE = false; |
Sanjay Patel | b38ad88e | 2017-01-02 23:25:28 +0000 | [diff] [blame] | 1027 | if (Value *AvailableVal = FindAvailableLoadedValue( |
| 1028 | &LI, LI.getParent(), BBI, DefMaxInstsToScan, AA, &IsLoadCSE)) { |
| 1029 | if (IsLoadCSE) |
Florian Hahn | 406f1ff | 2018-08-24 11:40:04 +0000 | [diff] [blame] | 1030 | combineMetadataForCSE(cast<LoadInst>(AvailableVal), &LI, false); |
Bjorn Steinbrink | a91fd09 | 2015-07-10 06:55:44 +0000 | [diff] [blame] | 1031 | |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1032 | return replaceInstUsesWith( |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1033 | LI, Builder.CreateBitOrPointerCast(AvailableVal, LI.getType(), |
| 1034 | LI.getName() + ".cast")); |
Bjorn Steinbrink | a91fd09 | 2015-07-10 06:55:44 +0000 | [diff] [blame] | 1035 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1036 | |
Philip Reames | 3ac0718 | 2016-04-21 17:45:05 +0000 | [diff] [blame] | 1037 | // None of the following transforms are legal for volatile/ordered atomic |
| 1038 | // loads. Most of them do apply for unordered atomics. |
| 1039 | if (!LI.isUnordered()) return nullptr; |
Philip Reames | ac55090 | 2016-04-21 17:03:33 +0000 | [diff] [blame] | 1040 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1041 | // load(gep null, ...) -> unreachable |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1042 | // load null/undef -> unreachable |
Davide Italiano | ffcb4df | 2017-04-19 17:26:57 +0000 | [diff] [blame] | 1043 | // TODO: Consider a target hook for valid address spaces for this xforms. |
| 1044 | if (canSimplifyNullLoadOrGEP(LI, Op)) { |
| 1045 | // Insert a new store to null instruction before the load to indicate |
| 1046 | // that this code is not reachable. We do this instead of inserting |
| 1047 | // an unreachable instruction directly because we cannot modify the |
| 1048 | // CFG. |
Weiming Zhao | 984f1dc | 2017-07-19 01:27:24 +0000 | [diff] [blame] | 1049 | StoreInst *SI = new StoreInst(UndefValue::get(LI.getType()), |
| 1050 | Constant::getNullValue(Op->getType()), &LI); |
| 1051 | SI->setDebugLoc(LI.getDebugLoc()); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1052 | return replaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1055 | if (Op->hasOneUse()) { |
| 1056 | // Change select and PHI nodes to select values instead of addresses: this |
| 1057 | // helps alias analysis out a lot, allows many others simplifications, and |
| 1058 | // exposes redundancy in the code. |
| 1059 | // |
| 1060 | // Note that we cannot do the transformation unless we know that the |
| 1061 | // introduced loads cannot trap! Something like this is valid as long as |
| 1062 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 1063 | // but it would not be valid if we transformed it to load from null |
| 1064 | // unconditionally. |
| 1065 | // |
| 1066 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 1067 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Bob Wilson | 56600a1 | 2010-01-30 04:42:39 +0000 | [diff] [blame] | 1068 | unsigned Align = LI.getAlignment(); |
Artur Pilipenko | 9bb6bea | 2016-04-27 11:00:48 +0000 | [diff] [blame] | 1069 | if (isSafeToLoadUnconditionally(SI->getOperand(1), Align, DL, SI) && |
| 1070 | isSafeToLoadUnconditionally(SI->getOperand(2), Align, DL, SI)) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1071 | LoadInst *V1 = Builder.CreateLoad(SI->getOperand(1), |
| 1072 | SI->getOperand(1)->getName()+".val"); |
| 1073 | LoadInst *V2 = Builder.CreateLoad(SI->getOperand(2), |
| 1074 | SI->getOperand(2)->getName()+".val"); |
Philip Reames | a98c7ea | 2016-04-21 17:59:40 +0000 | [diff] [blame] | 1075 | assert(LI.isUnordered() && "implied by above"); |
Bob Wilson | 56600a1 | 2010-01-30 04:42:39 +0000 | [diff] [blame] | 1076 | V1->setAlignment(Align); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1077 | V1->setAtomic(LI.getOrdering(), LI.getSyncScopeID()); |
Bob Wilson | 56600a1 | 2010-01-30 04:42:39 +0000 | [diff] [blame] | 1078 | V2->setAlignment(Align); |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 1079 | V2->setAtomic(LI.getOrdering(), LI.getSyncScopeID()); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1080 | return SelectInst::Create(SI->getCondition(), V1, V2); |
| 1081 | } |
| 1082 | |
| 1083 | // load (select (cond, null, P)) -> load P |
Larisse Voufo | 532bf71 | 2015-09-18 19:14:35 +0000 | [diff] [blame] | 1084 | if (isa<ConstantPointerNull>(SI->getOperand(1)) && |
Manoj Gupta | 77eeac3 | 2018-07-09 22:27:23 +0000 | [diff] [blame] | 1085 | !NullPointerIsDefined(SI->getFunction(), |
| 1086 | LI.getPointerAddressSpace())) { |
Philip Reames | 5ad26c3 | 2014-12-29 22:46:21 +0000 | [diff] [blame] | 1087 | LI.setOperand(0, SI->getOperand(2)); |
| 1088 | return &LI; |
| 1089 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1090 | |
| 1091 | // load (select (cond, P, null)) -> load P |
Philip Reames | 5ad26c3 | 2014-12-29 22:46:21 +0000 | [diff] [blame] | 1092 | if (isa<ConstantPointerNull>(SI->getOperand(2)) && |
Manoj Gupta | 77eeac3 | 2018-07-09 22:27:23 +0000 | [diff] [blame] | 1093 | !NullPointerIsDefined(SI->getFunction(), |
| 1094 | LI.getPointerAddressSpace())) { |
Philip Reames | 5ad26c3 | 2014-12-29 22:46:21 +0000 | [diff] [blame] | 1095 | LI.setOperand(0, SI->getOperand(1)); |
| 1096 | return &LI; |
| 1097 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1098 | } |
| 1099 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1100 | return nullptr; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1103 | /// Look for extractelement/insertvalue sequence that acts like a bitcast. |
Arch D. Robison | be0490a | 2016-04-25 22:22:39 +0000 | [diff] [blame] | 1104 | /// |
| 1105 | /// \returns underlying value that was "cast", or nullptr otherwise. |
| 1106 | /// |
| 1107 | /// For example, if we have: |
| 1108 | /// |
| 1109 | /// %E0 = extractelement <2 x double> %U, i32 0 |
| 1110 | /// %V0 = insertvalue [2 x double] undef, double %E0, 0 |
| 1111 | /// %E1 = extractelement <2 x double> %U, i32 1 |
| 1112 | /// %V1 = insertvalue [2 x double] %V0, double %E1, 1 |
| 1113 | /// |
| 1114 | /// and the layout of a <2 x double> is isomorphic to a [2 x double], |
| 1115 | /// then %V1 can be safely approximated by a conceptual "bitcast" of %U. |
| 1116 | /// Note that %U may contain non-undef values where %V1 has undef. |
| 1117 | static Value *likeBitCastFromVector(InstCombiner &IC, Value *V) { |
| 1118 | Value *U = nullptr; |
| 1119 | while (auto *IV = dyn_cast<InsertValueInst>(V)) { |
| 1120 | auto *E = dyn_cast<ExtractElementInst>(IV->getInsertedValueOperand()); |
| 1121 | if (!E) |
| 1122 | return nullptr; |
| 1123 | auto *W = E->getVectorOperand(); |
| 1124 | if (!U) |
| 1125 | U = W; |
| 1126 | else if (U != W) |
| 1127 | return nullptr; |
| 1128 | auto *CI = dyn_cast<ConstantInt>(E->getIndexOperand()); |
| 1129 | if (!CI || IV->getNumIndices() != 1 || CI->getZExtValue() != *IV->idx_begin()) |
| 1130 | return nullptr; |
| 1131 | V = IV->getAggregateOperand(); |
| 1132 | } |
| 1133 | if (!isa<UndefValue>(V) ||!U) |
| 1134 | return nullptr; |
| 1135 | |
| 1136 | auto *UT = cast<VectorType>(U->getType()); |
| 1137 | auto *VT = V->getType(); |
| 1138 | // Check that types UT and VT are bitwise isomorphic. |
| 1139 | const auto &DL = IC.getDataLayout(); |
| 1140 | if (DL.getTypeStoreSizeInBits(UT) != DL.getTypeStoreSizeInBits(VT)) { |
| 1141 | return nullptr; |
| 1142 | } |
| 1143 | if (auto *AT = dyn_cast<ArrayType>(VT)) { |
| 1144 | if (AT->getNumElements() != UT->getNumElements()) |
| 1145 | return nullptr; |
| 1146 | } else { |
| 1147 | auto *ST = cast<StructType>(VT); |
| 1148 | if (ST->getNumElements() != UT->getNumElements()) |
| 1149 | return nullptr; |
| 1150 | for (const auto *EltT : ST->elements()) { |
| 1151 | if (EltT != UT->getElementType()) |
| 1152 | return nullptr; |
| 1153 | } |
| 1154 | } |
| 1155 | return U; |
| 1156 | } |
| 1157 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1158 | /// Combine stores to match the type of value being stored. |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 1159 | /// |
| 1160 | /// The core idea here is that the memory does not have any intrinsic type and |
| 1161 | /// where we can we should match the type of a store to the type of value being |
| 1162 | /// stored. |
| 1163 | /// |
| 1164 | /// However, this routine must never change the width of a store or the number of |
| 1165 | /// stores as that would introduce a semantic change. This combine is expected to |
| 1166 | /// be a semantic no-op which just allows stores to more closely model the types |
| 1167 | /// of their incoming values. |
| 1168 | /// |
| 1169 | /// Currently, we also refuse to change the precise type used for an atomic or |
| 1170 | /// volatile store. This is debatable, and might be reasonable to change later. |
| 1171 | /// However, it is risky in case some backend or other part of LLVM is relying |
| 1172 | /// on the exact type stored to select appropriate atomic operations. |
| 1173 | /// |
| 1174 | /// \returns true if the store was successfully combined away. This indicates |
| 1175 | /// 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] | 1176 | /// 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] | 1177 | /// combined or not: IC.EraseInstFromFunction returns a null pointer. |
| 1178 | static bool combineStoreToValueType(InstCombiner &IC, StoreInst &SI) { |
Philip Reames | 6f4d008 | 2016-05-06 22:17:01 +0000 | [diff] [blame] | 1179 | // FIXME: We could probably with some care handle both volatile and ordered |
| 1180 | // atomic stores here but it isn't clear that this is important. |
| 1181 | if (!SI.isUnordered()) |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 1182 | return false; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1183 | |
Arnold Schwaighofer | 5d33555 | 2016-09-10 18:14:57 +0000 | [diff] [blame] | 1184 | // swifterror values can't be bitcasted. |
| 1185 | if (SI.getPointerOperand()->isSwiftError()) |
| 1186 | return false; |
| 1187 | |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 1188 | Value *V = SI.getValueOperand(); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1189 | |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 1190 | // Fold away bit casts of the stored value by storing the original type. |
| 1191 | if (auto *BC = dyn_cast<BitCastInst>(V)) { |
Chandler Carruth | a7f247e | 2014-12-09 19:21:16 +0000 | [diff] [blame] | 1192 | V = BC->getOperand(0); |
Philip Reames | 89e92d2 | 2016-12-01 20:17:06 +0000 | [diff] [blame] | 1193 | if (!SI.isAtomic() || isSupportedAtomicType(V->getType())) { |
| 1194 | combineStoreToNewValue(IC, SI, V); |
| 1195 | return true; |
| 1196 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Philip Reames | 89e92d2 | 2016-12-01 20:17:06 +0000 | [diff] [blame] | 1199 | if (Value *U = likeBitCastFromVector(IC, V)) |
| 1200 | if (!SI.isAtomic() || isSupportedAtomicType(U->getType())) { |
| 1201 | combineStoreToNewValue(IC, SI, U); |
| 1202 | return true; |
| 1203 | } |
Arch D. Robison | be0490a | 2016-04-25 22:22:39 +0000 | [diff] [blame] | 1204 | |
JF Bastien | c22d299 | 2016-04-21 19:53:39 +0000 | [diff] [blame] | 1205 | // FIXME: We should also canonicalize stores of vectors when their elements |
| 1206 | // are cast to other types. |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 1207 | return false; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 1210 | static bool unpackStoreToAggregate(InstCombiner &IC, StoreInst &SI) { |
| 1211 | // FIXME: We could probably with some care handle both volatile and atomic |
| 1212 | // stores here but it isn't clear that this is important. |
| 1213 | if (!SI.isSimple()) |
| 1214 | return false; |
| 1215 | |
| 1216 | Value *V = SI.getValueOperand(); |
| 1217 | Type *T = V->getType(); |
| 1218 | |
| 1219 | if (!T->isAggregateType()) |
| 1220 | return false; |
| 1221 | |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 1222 | if (auto *ST = dyn_cast<StructType>(T)) { |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 1223 | // If the struct only have one element, we unpack. |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 1224 | unsigned Count = ST->getNumElements(); |
| 1225 | if (Count == 1) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1226 | V = IC.Builder.CreateExtractValue(V, 0); |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 1227 | combineStoreToNewValue(IC, SI, V); |
| 1228 | return true; |
| 1229 | } |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 1230 | |
| 1231 | // We don't want to break loads with padding here as we'd loose |
| 1232 | // the knowledge that padding exists for the rest of the pipeline. |
| 1233 | const DataLayout &DL = IC.getDataLayout(); |
| 1234 | auto *SL = DL.getStructLayout(ST); |
| 1235 | if (SL->hasPadding()) |
| 1236 | return false; |
| 1237 | |
Amaury Sechet | 61a7d62 | 2016-02-17 19:21:28 +0000 | [diff] [blame] | 1238 | auto Align = SI.getAlignment(); |
| 1239 | if (!Align) |
| 1240 | Align = DL.getABITypeAlignment(ST); |
| 1241 | |
NAKAMURA Takumi | ec6b1fc | 2015-12-15 09:37:31 +0000 | [diff] [blame] | 1242 | SmallString<16> EltName = V->getName(); |
| 1243 | EltName += ".elt"; |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 1244 | auto *Addr = SI.getPointerOperand(); |
NAKAMURA Takumi | ec6b1fc | 2015-12-15 09:37:31 +0000 | [diff] [blame] | 1245 | SmallString<16> AddrName = Addr->getName(); |
| 1246 | AddrName += ".repack"; |
Amaury Sechet | 61a7d62 | 2016-02-17 19:21:28 +0000 | [diff] [blame] | 1247 | |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 1248 | auto *IdxType = Type::getInt32Ty(ST->getContext()); |
| 1249 | auto *Zero = ConstantInt::get(IdxType, 0); |
| 1250 | for (unsigned i = 0; i < Count; i++) { |
| 1251 | Value *Indices[2] = { |
| 1252 | Zero, |
| 1253 | ConstantInt::get(IdxType, i), |
| 1254 | }; |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1255 | auto *Ptr = IC.Builder.CreateInBoundsGEP(ST, Addr, makeArrayRef(Indices), |
| 1256 | AddrName); |
| 1257 | auto *Val = IC.Builder.CreateExtractValue(V, i, EltName); |
Amaury Sechet | 61a7d62 | 2016-02-17 19:21:28 +0000 | [diff] [blame] | 1258 | auto EltAlign = MinAlign(Align, SL->getElementOffset(i)); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1259 | llvm::Instruction *NS = IC.Builder.CreateAlignedStore(Val, Ptr, EltAlign); |
Keno Fischer | a236dae | 2017-06-28 23:36:40 +0000 | [diff] [blame] | 1260 | AAMDNodes AAMD; |
| 1261 | SI.getAAMetadata(AAMD); |
| 1262 | NS->setAAMetadata(AAMD); |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | return true; |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
David Majnemer | 7536460 | 2015-05-11 05:04:27 +0000 | [diff] [blame] | 1268 | if (auto *AT = dyn_cast<ArrayType>(T)) { |
| 1269 | // If the array only have one element, we unpack. |
Amaury Sechet | 3b8b2ea | 2016-03-02 22:36:45 +0000 | [diff] [blame] | 1270 | auto NumElements = AT->getNumElements(); |
| 1271 | if (NumElements == 1) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1272 | V = IC.Builder.CreateExtractValue(V, 0); |
David Majnemer | 7536460 | 2015-05-11 05:04:27 +0000 | [diff] [blame] | 1273 | combineStoreToNewValue(IC, SI, V); |
| 1274 | return true; |
| 1275 | } |
Amaury Sechet | 3b8b2ea | 2016-03-02 22:36:45 +0000 | [diff] [blame] | 1276 | |
Davide Italiano | f6988d2 | 2016-10-07 21:53:09 +0000 | [diff] [blame] | 1277 | // Bail out if the array is too large. Ideally we would like to optimize |
| 1278 | // arrays of arbitrary size but this has a terrible impact on compile time. |
| 1279 | // The threshold here is chosen arbitrarily, maybe needs a little bit of |
| 1280 | // tuning. |
Davide Italiano | 2133bf5 | 2017-02-07 17:56:50 +0000 | [diff] [blame] | 1281 | if (NumElements > IC.MaxArraySizeForCombine) |
Davide Italiano | f6988d2 | 2016-10-07 21:53:09 +0000 | [diff] [blame] | 1282 | return false; |
| 1283 | |
Amaury Sechet | 3b8b2ea | 2016-03-02 22:36:45 +0000 | [diff] [blame] | 1284 | const DataLayout &DL = IC.getDataLayout(); |
| 1285 | auto EltSize = DL.getTypeAllocSize(AT->getElementType()); |
| 1286 | auto Align = SI.getAlignment(); |
| 1287 | if (!Align) |
| 1288 | Align = DL.getABITypeAlignment(T); |
| 1289 | |
| 1290 | SmallString<16> EltName = V->getName(); |
| 1291 | EltName += ".elt"; |
| 1292 | auto *Addr = SI.getPointerOperand(); |
| 1293 | SmallString<16> AddrName = Addr->getName(); |
| 1294 | AddrName += ".repack"; |
| 1295 | |
| 1296 | auto *IdxType = Type::getInt64Ty(T->getContext()); |
| 1297 | auto *Zero = ConstantInt::get(IdxType, 0); |
| 1298 | |
| 1299 | uint64_t Offset = 0; |
| 1300 | for (uint64_t i = 0; i < NumElements; i++) { |
| 1301 | Value *Indices[2] = { |
| 1302 | Zero, |
| 1303 | ConstantInt::get(IdxType, i), |
| 1304 | }; |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1305 | auto *Ptr = IC.Builder.CreateInBoundsGEP(AT, Addr, makeArrayRef(Indices), |
| 1306 | AddrName); |
| 1307 | auto *Val = IC.Builder.CreateExtractValue(V, i, EltName); |
Amaury Sechet | 3b8b2ea | 2016-03-02 22:36:45 +0000 | [diff] [blame] | 1308 | auto EltAlign = MinAlign(Align, Offset); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1309 | Instruction *NS = IC.Builder.CreateAlignedStore(Val, Ptr, EltAlign); |
Keno Fischer | a236dae | 2017-06-28 23:36:40 +0000 | [diff] [blame] | 1310 | AAMDNodes AAMD; |
| 1311 | SI.getAAMetadata(AAMD); |
| 1312 | NS->setAAMetadata(AAMD); |
Amaury Sechet | 3b8b2ea | 2016-03-02 22:36:45 +0000 | [diff] [blame] | 1313 | Offset += EltSize; |
| 1314 | } |
| 1315 | |
| 1316 | return true; |
David Majnemer | 7536460 | 2015-05-11 05:04:27 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 1319 | return false; |
| 1320 | } |
| 1321 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1322 | /// equivalentAddressValues - Test if A and B will obviously have the same |
| 1323 | /// value. This includes recognizing that %t0 and %t1 will have the same |
| 1324 | /// value in code like this: |
| 1325 | /// %t0 = getelementptr \@a, 0, 3 |
| 1326 | /// store i32 0, i32* %t0 |
| 1327 | /// %t1 = getelementptr \@a, 0, 3 |
| 1328 | /// %t2 = load i32* %t1 |
| 1329 | /// |
| 1330 | static bool equivalentAddressValues(Value *A, Value *B) { |
| 1331 | // Test if the values are trivially equivalent. |
| 1332 | if (A == B) return true; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1333 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1334 | // Test if the values come form identical arithmetic instructions. |
| 1335 | // This uses isIdenticalToWhenDefined instead of isIdenticalTo because |
| 1336 | // its only used to compare two uses within the same basic block, which |
| 1337 | // means that they'll always either have the same value or one of them |
| 1338 | // will have an undefined value. |
| 1339 | if (isa<BinaryOperator>(A) || |
| 1340 | isa<CastInst>(A) || |
| 1341 | isa<PHINode>(A) || |
| 1342 | isa<GetElementPtrInst>(A)) |
| 1343 | if (Instruction *BI = dyn_cast<Instruction>(B)) |
| 1344 | if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI)) |
| 1345 | return true; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1346 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1347 | // Otherwise they may not be equivalent. |
| 1348 | return false; |
| 1349 | } |
| 1350 | |
Alexey Bataev | ec95c6c | 2017-12-08 15:32:10 +0000 | [diff] [blame] | 1351 | /// Converts store (bitcast (load (bitcast (select ...)))) to |
| 1352 | /// store (load (select ...)), where select is minmax: |
| 1353 | /// select ((cmp load V1, load V2), V1, V2). |
Alexey Bataev | 83c15b1 | 2017-12-12 20:28:46 +0000 | [diff] [blame] | 1354 | static bool removeBitcastsFromLoadStoreOnMinMax(InstCombiner &IC, |
| 1355 | StoreInst &SI) { |
Alexey Bataev | ec95c6c | 2017-12-08 15:32:10 +0000 | [diff] [blame] | 1356 | // bitcast? |
Alexey Bataev | 83c15b1 | 2017-12-12 20:28:46 +0000 | [diff] [blame] | 1357 | if (!match(SI.getPointerOperand(), m_BitCast(m_Value()))) |
Alexey Bataev | fa0a76d | 2017-12-12 19:12:34 +0000 | [diff] [blame] | 1358 | return false; |
Alexey Bataev | ec95c6c | 2017-12-08 15:32:10 +0000 | [diff] [blame] | 1359 | // load? integer? |
| 1360 | Value *LoadAddr; |
| 1361 | if (!match(SI.getValueOperand(), m_Load(m_BitCast(m_Value(LoadAddr))))) |
Alexey Bataev | fa0a76d | 2017-12-12 19:12:34 +0000 | [diff] [blame] | 1362 | return false; |
Alexey Bataev | ec95c6c | 2017-12-08 15:32:10 +0000 | [diff] [blame] | 1363 | auto *LI = cast<LoadInst>(SI.getValueOperand()); |
| 1364 | if (!LI->getType()->isIntegerTy()) |
Alexey Bataev | fa0a76d | 2017-12-12 19:12:34 +0000 | [diff] [blame] | 1365 | return false; |
Alexey Bataev | ec95c6c | 2017-12-08 15:32:10 +0000 | [diff] [blame] | 1366 | if (!isMinMaxWithLoads(LoadAddr)) |
Alexey Bataev | fa0a76d | 2017-12-12 19:12:34 +0000 | [diff] [blame] | 1367 | return false; |
Alexey Bataev | ec95c6c | 2017-12-08 15:32:10 +0000 | [diff] [blame] | 1368 | |
Alexey Bataev | 83c15b1 | 2017-12-12 20:28:46 +0000 | [diff] [blame] | 1369 | if (!all_of(LI->users(), [LI, LoadAddr](User *U) { |
| 1370 | auto *SI = dyn_cast<StoreInst>(U); |
| 1371 | return SI && SI->getPointerOperand() != LI && |
| 1372 | peekThroughBitcast(SI->getPointerOperand()) != LoadAddr && |
| 1373 | !SI->getPointerOperand()->isSwiftError(); |
| 1374 | })) |
| 1375 | return false; |
| 1376 | |
| 1377 | IC.Builder.SetInsertPoint(LI); |
Alexey Bataev | ec95c6c | 2017-12-08 15:32:10 +0000 | [diff] [blame] | 1378 | LoadInst *NewLI = combineLoadToNewType( |
| 1379 | IC, *LI, LoadAddr->getType()->getPointerElementType()); |
Alexey Bataev | 83c15b1 | 2017-12-12 20:28:46 +0000 | [diff] [blame] | 1380 | // Replace all the stores with stores of the newly loaded value. |
| 1381 | for (auto *UI : LI->users()) { |
| 1382 | auto *USI = cast<StoreInst>(UI); |
| 1383 | IC.Builder.SetInsertPoint(USI); |
| 1384 | combineStoreToNewValue(IC, *USI, NewLI); |
| 1385 | } |
| 1386 | IC.replaceInstUsesWith(*LI, UndefValue::get(LI->getType())); |
| 1387 | IC.eraseInstFromFunction(*LI); |
Alexey Bataev | fa0a76d | 2017-12-12 19:12:34 +0000 | [diff] [blame] | 1388 | return true; |
Alexey Bataev | ec95c6c | 2017-12-08 15:32:10 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1391 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 1392 | Value *Val = SI.getOperand(0); |
| 1393 | Value *Ptr = SI.getOperand(1); |
| 1394 | |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 1395 | // Try to canonicalize the stored type. |
| 1396 | if (combineStoreToValueType(*this, SI)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1397 | return eraseInstFromFunction(SI); |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 1398 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1399 | // Attempt to improve the alignment. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1400 | unsigned KnownAlign = getOrEnforceKnownAlignment( |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1401 | Ptr, DL.getPrefTypeAlignment(Val->getType()), DL, &SI, &AC, &DT); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1402 | unsigned StoreAlign = SI.getAlignment(); |
| 1403 | unsigned EffectiveStoreAlign = |
| 1404 | StoreAlign != 0 ? StoreAlign : DL.getABITypeAlignment(Val->getType()); |
Dan Gohman | 3619660 | 2010-08-03 18:20:32 +0000 | [diff] [blame] | 1405 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1406 | if (KnownAlign > EffectiveStoreAlign) |
| 1407 | SI.setAlignment(KnownAlign); |
| 1408 | else if (StoreAlign == 0) |
| 1409 | SI.setAlignment(EffectiveStoreAlign); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1410 | |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 1411 | // Try to canonicalize the stored type. |
| 1412 | if (unpackStoreToAggregate(*this, SI)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1413 | return eraseInstFromFunction(SI); |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 1414 | |
Alexey Bataev | fa0a76d | 2017-12-12 19:12:34 +0000 | [diff] [blame] | 1415 | if (removeBitcastsFromLoadStoreOnMinMax(*this, SI)) |
| 1416 | return eraseInstFromFunction(SI); |
Alexey Bataev | ec95c6c | 2017-12-08 15:32:10 +0000 | [diff] [blame] | 1417 | |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 1418 | // Replace GEP indices if possible. |
| 1419 | if (Instruction *NewGEPI = replaceGEPIdxWithZero(*this, Ptr, SI)) { |
| 1420 | Worklist.Add(NewGEPI); |
| 1421 | return &SI; |
| 1422 | } |
| 1423 | |
Philip Reames | d7a6cc8 | 2015-12-17 22:19:27 +0000 | [diff] [blame] | 1424 | // Don't hack volatile/ordered stores. |
| 1425 | // FIXME: Some bits are legal for ordered atomic stores; needs refactoring. |
| 1426 | if (!SI.isUnordered()) return nullptr; |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1427 | |
| 1428 | // If the RHS is an alloca with a single use, zapify the store, making the |
| 1429 | // alloca dead. |
| 1430 | if (Ptr->hasOneUse()) { |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1431 | if (isa<AllocaInst>(Ptr)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1432 | return eraseInstFromFunction(SI); |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1433 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { |
| 1434 | if (isa<AllocaInst>(GEP->getOperand(0))) { |
| 1435 | if (GEP->getOperand(0)->hasOneUse()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1436 | return eraseInstFromFunction(SI); |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1437 | } |
| 1438 | } |
| 1439 | } |
| 1440 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1441 | // Do really simple DSE, to catch cases where there are several consecutive |
| 1442 | // stores to the same location, separated by a few arithmetic operations. This |
| 1443 | // situation often occurs with bitfield accesses. |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 1444 | BasicBlock::iterator BBI(SI); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1445 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 1446 | --ScanInsts) { |
| 1447 | --BBI; |
Victor Hernandez | 5f8c8c0 | 2010-01-22 19:05:05 +0000 | [diff] [blame] | 1448 | // Don't count debug info directives, lest they affect codegen, |
| 1449 | // and we skip pointer-to-pointer bitcasts, which are NOPs. |
| 1450 | if (isa<DbgInfoIntrinsic>(BBI) || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1451 | (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1452 | ScanInsts++; |
| 1453 | continue; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1456 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 1457 | // Prev store isn't volatile, and stores to the same location? |
Philip Reames | d7a6cc8 | 2015-12-17 22:19:27 +0000 | [diff] [blame] | 1458 | if (PrevSI->isUnordered() && equivalentAddressValues(PrevSI->getOperand(1), |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1459 | SI.getOperand(1))) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1460 | ++NumDeadStore; |
| 1461 | ++BBI; |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1462 | eraseInstFromFunction(*PrevSI); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1463 | continue; |
| 1464 | } |
| 1465 | break; |
| 1466 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1467 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1468 | // If this is a load, we have to stop. However, if the loaded value is from |
| 1469 | // the pointer we're loading and is producing the pointer we're storing, |
| 1470 | // then *this* store is dead (X = load P; store X -> P). |
| 1471 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
Philip Reames | d7a6cc8 | 2015-12-17 22:19:27 +0000 | [diff] [blame] | 1472 | if (LI == Val && equivalentAddressValues(LI->getOperand(0), Ptr)) { |
| 1473 | assert(SI.isUnordered() && "can't eliminate ordering operation"); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1474 | return eraseInstFromFunction(SI); |
Philip Reames | d7a6cc8 | 2015-12-17 22:19:27 +0000 | [diff] [blame] | 1475 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1476 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1477 | // Otherwise, this is a load from some other location. Stores before it |
| 1478 | // may not be dead. |
| 1479 | break; |
| 1480 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1481 | |
Sanjoy Das | 679bc32 | 2017-01-17 05:45:09 +0000 | [diff] [blame] | 1482 | // Don't skip over loads, throws or things that can modify memory. |
| 1483 | if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory() || BBI->mayThrow()) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1484 | break; |
| 1485 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1486 | |
| 1487 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
Anna Thomas | 2dd9835 | 2017-12-12 14:12:33 +0000 | [diff] [blame] | 1488 | // store X, GEP(null, Y) -> turns into 'unreachable' in SimplifyCFG |
| 1489 | if (canSimplifyNullStoreOrGEP(SI)) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1490 | if (!isa<UndefValue>(Val)) { |
| 1491 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 1492 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
| 1493 | Worklist.Add(U); // Dropped a use. |
| 1494 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1495 | return nullptr; // Do not modify these! |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | // store undef, Ptr -> noop |
| 1499 | if (isa<UndefValue>(Val)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1500 | return eraseInstFromFunction(SI); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1501 | |
Sanjay Patel | 4a12aa9 | 2018-11-10 20:29:25 +0000 | [diff] [blame] | 1502 | // If this store is the second-to-last instruction in the basic block |
| 1503 | // (excluding debug info and bitcasts of pointers) and if the block ends with |
| 1504 | // an unconditional branch, try to move the store to the successor block. |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 1505 | BBI = SI.getIterator(); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1506 | do { |
| 1507 | ++BBI; |
Victor Hernandez | 5f8c8c0 | 2010-01-22 19:05:05 +0000 | [diff] [blame] | 1508 | } while (isa<DbgInfoIntrinsic>(BBI) || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1509 | (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())); |
Sanjay Patel | 4a12aa9 | 2018-11-10 20:29:25 +0000 | [diff] [blame] | 1510 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1511 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
| 1512 | if (BI->isUnconditional()) |
Sanjay Patel | 4a12aa9 | 2018-11-10 20:29:25 +0000 | [diff] [blame] | 1513 | mergeStoreIntoSuccessor(SI); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1514 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1515 | return nullptr; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Sanjay Patel | 4a12aa9 | 2018-11-10 20:29:25 +0000 | [diff] [blame] | 1518 | /// Try to transform: |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1519 | /// if () { *P = v1; } else { *P = v2 } |
Sanjay Patel | 4a12aa9 | 2018-11-10 20:29:25 +0000 | [diff] [blame] | 1520 | /// or: |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1521 | /// *P = v1; if () { *P = v2; } |
| 1522 | /// into a phi node with a store in the successor. |
Sanjay Patel | 4a12aa9 | 2018-11-10 20:29:25 +0000 | [diff] [blame] | 1523 | bool InstCombiner::mergeStoreIntoSuccessor(StoreInst &SI) { |
Philip Reames | 5f0e369 | 2016-04-22 20:53:32 +0000 | [diff] [blame] | 1524 | assert(SI.isUnordered() && |
Sanjay Patel | 4a12aa9 | 2018-11-10 20:29:25 +0000 | [diff] [blame] | 1525 | "This code has not been audited for volatile or ordered store case."); |
Justin Bogner | c7e4fbe | 2016-08-05 01:09:48 +0000 | [diff] [blame] | 1526 | |
Sanjay Patel | 4a12aa9 | 2018-11-10 20:29:25 +0000 | [diff] [blame] | 1527 | // Check if the successor block has exactly 2 incoming edges. |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1528 | BasicBlock *StoreBB = SI.getParent(); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1529 | BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0); |
Vedant Kumar | 4de31bb | 2018-11-19 19:54:27 +0000 | [diff] [blame] | 1530 | if (!DestBB->hasNPredecessors(2)) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1531 | return false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1532 | |
Sanjay Patel | 4a12aa9 | 2018-11-10 20:29:25 +0000 | [diff] [blame] | 1533 | // Capture the other block (the block that doesn't contain our store). |
| 1534 | pred_iterator PredIter = pred_begin(DestBB); |
| 1535 | if (*PredIter == StoreBB) |
| 1536 | ++PredIter; |
| 1537 | BasicBlock *OtherBB = *PredIter; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1538 | |
Sanjay Patel | 4a12aa9 | 2018-11-10 20:29:25 +0000 | [diff] [blame] | 1539 | // Bail out if all of the relevant blocks aren't distinct. This can happen, |
| 1540 | // for example, if SI is in an infinite loop. |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1541 | if (StoreBB == DestBB || OtherBB == DestBB) |
| 1542 | return false; |
| 1543 | |
| 1544 | // 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] | 1545 | BasicBlock::iterator BBI(OtherBB->getTerminator()); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1546 | BranchInst *OtherBr = dyn_cast<BranchInst>(BBI); |
| 1547 | if (!OtherBr || BBI == OtherBB->begin()) |
| 1548 | return false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1549 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1550 | // If the other block ends in an unconditional branch, check for the 'if then |
Sanjay Patel | 4a12aa9 | 2018-11-10 20:29:25 +0000 | [diff] [blame] | 1551 | // else' case. There is an instruction before the branch. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1552 | StoreInst *OtherStore = nullptr; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1553 | if (OtherBr->isUnconditional()) { |
| 1554 | --BBI; |
| 1555 | // Skip over debugging info. |
Victor Hernandez | 5f8c8c0 | 2010-01-22 19:05:05 +0000 | [diff] [blame] | 1556 | while (isa<DbgInfoIntrinsic>(BBI) || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1557 | (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1558 | if (BBI==OtherBB->begin()) |
| 1559 | return false; |
| 1560 | --BBI; |
| 1561 | } |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1562 | // If this isn't a store, isn't a store to the same location, or is not the |
| 1563 | // right kind of store, bail out. |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1564 | OtherStore = dyn_cast<StoreInst>(BBI); |
| 1565 | if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1) || |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1566 | !SI.isSameOperationAs(OtherStore)) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1567 | return false; |
| 1568 | } else { |
| 1569 | // Otherwise, the other block ended with a conditional branch. If one of the |
| 1570 | // destinations is StoreBB, then we have the if/then case. |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1571 | if (OtherBr->getSuccessor(0) != StoreBB && |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1572 | OtherBr->getSuccessor(1) != StoreBB) |
| 1573 | return false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1574 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1575 | // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an |
Sanjay Patel | 4a12aa9 | 2018-11-10 20:29:25 +0000 | [diff] [blame] | 1576 | // if/then triangle. See if there is a store to the same ptr as SI that |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1577 | // lives in OtherBB. |
| 1578 | for (;; --BBI) { |
| 1579 | // Check to see if we find the matching store. |
| 1580 | if ((OtherStore = dyn_cast<StoreInst>(BBI))) { |
| 1581 | if (OtherStore->getOperand(1) != SI.getOperand(1) || |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1582 | !SI.isSameOperationAs(OtherStore)) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1583 | return false; |
| 1584 | break; |
| 1585 | } |
| 1586 | // If we find something that may be using or overwriting the stored |
Sanjay Patel | 4a12aa9 | 2018-11-10 20:29:25 +0000 | [diff] [blame] | 1587 | // value, or if we run out of instructions, we can't do the transform. |
Sanjoy Das | 679bc32 | 2017-01-17 05:45:09 +0000 | [diff] [blame] | 1588 | if (BBI->mayReadFromMemory() || BBI->mayThrow() || |
| 1589 | BBI->mayWriteToMemory() || BBI == OtherBB->begin()) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1590 | return false; |
| 1591 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1592 | |
Sanjay Patel | 4a12aa9 | 2018-11-10 20:29:25 +0000 | [diff] [blame] | 1593 | // In order to eliminate the store in OtherBr, we have to make sure nothing |
| 1594 | // reads or overwrites the stored value in StoreBB. |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1595 | for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) { |
| 1596 | // FIXME: This should really be AA driven. |
Sanjoy Das | 679bc32 | 2017-01-17 05:45:09 +0000 | [diff] [blame] | 1597 | if (I->mayReadFromMemory() || I->mayThrow() || I->mayWriteToMemory()) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1598 | return false; |
| 1599 | } |
| 1600 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1601 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1602 | // Insert a PHI node now if we need it. |
| 1603 | Value *MergedVal = OtherStore->getOperand(0); |
Vedant Kumar | 238533e | 2018-11-19 19:55:02 +0000 | [diff] [blame^] | 1604 | // The debug locations of the original instructions might differ. Merge them. |
| 1605 | DebugLoc MergedLoc = DILocation::getMergedLocation(SI.getDebugLoc(), |
| 1606 | OtherStore->getDebugLoc()); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1607 | if (MergedVal != SI.getOperand(0)) { |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 1608 | PHINode *PN = PHINode::Create(MergedVal->getType(), 2, "storemerge"); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1609 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
| 1610 | PN->addIncoming(OtherStore->getOperand(0), OtherBB); |
| 1611 | MergedVal = InsertNewInstBefore(PN, DestBB->front()); |
Vedant Kumar | 238533e | 2018-11-19 19:55:02 +0000 | [diff] [blame^] | 1612 | PN->setDebugLoc(MergedLoc); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1613 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1614 | |
Sanjay Patel | 4a12aa9 | 2018-11-10 20:29:25 +0000 | [diff] [blame] | 1615 | // Advance to a place where it is safe to insert the new store and insert it. |
Bill Wendling | 8ddfc09 | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 1616 | BBI = DestBB->getFirstInsertionPt(); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 1617 | StoreInst *NewSI = new StoreInst(MergedVal, SI.getOperand(1), |
Sanjay Patel | 4a12aa9 | 2018-11-10 20:29:25 +0000 | [diff] [blame] | 1618 | SI.isVolatile(), SI.getAlignment(), |
| 1619 | SI.getOrdering(), SI.getSyncScopeID()); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 1620 | InsertNewInstBefore(NewSI, *BBI); |
Vedant Kumar | 238533e | 2018-11-19 19:55:02 +0000 | [diff] [blame^] | 1621 | NewSI->setDebugLoc(MergedLoc); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 1622 | |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1623 | // If the two stores had AA tags, merge them. |
| 1624 | AAMDNodes AATags; |
| 1625 | SI.getAAMetadata(AATags); |
| 1626 | if (AATags) { |
| 1627 | OtherStore->getAAMetadata(AATags, /* Merge = */ true); |
| 1628 | NewSI->setAAMetadata(AATags); |
| 1629 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1630 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1631 | // Nuke the old stores. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1632 | eraseInstFromFunction(SI); |
| 1633 | eraseInstFromFunction(*OtherStore); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1634 | return true; |
| 1635 | } |