Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 1 | //===- LoadCombine.cpp - Combine Adjacent Loads ---------------------------===// |
| 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 | /// \file |
| 10 | /// This transformation combines adjacent loads. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Transforms/Scalar.h" |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/DenseMap.h" |
| 16 | #include "llvm/ADT/Statistic.h" |
Hal Finkel | 840257a | 2014-11-03 23:19:16 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/AliasAnalysis.h" |
| 18 | #include "llvm/Analysis/AliasSetTracker.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/GlobalsModRef.h" |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/TargetFolder.h" |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DataLayout.h" |
| 22 | #include "llvm/IR/Function.h" |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 23 | #include "llvm/IR/IRBuilder.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Instructions.h" |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Module.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 26 | #include "llvm/Pass.h" |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/MathExtras.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | |
| 31 | using namespace llvm; |
| 32 | |
| 33 | #define DEBUG_TYPE "load-combine" |
| 34 | |
| 35 | STATISTIC(NumLoadsAnalyzed, "Number of loads analyzed for combining"); |
| 36 | STATISTIC(NumLoadsCombined, "Number of loads combined"); |
| 37 | |
Chad Rosier | 7ab9a7b | 2016-05-04 15:19:02 +0000 | [diff] [blame] | 38 | #define LDCOMBINE_NAME "Combine Adjacent Loads" |
| 39 | |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 40 | namespace { |
| 41 | struct PointerOffsetPair { |
| 42 | Value *Pointer; |
David Majnemer | 3ffe2dd | 2016-06-19 21:36:35 +0000 | [diff] [blame^] | 43 | APInt Offset; |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | struct LoadPOPPair { |
Benjamin Kramer | 79de6e6 | 2015-04-11 18:57:14 +0000 | [diff] [blame] | 47 | LoadPOPPair() = default; |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 48 | LoadPOPPair(LoadInst *L, PointerOffsetPair P, unsigned O) |
| 49 | : Load(L), POP(P), InsertOrder(O) {} |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 50 | LoadInst *Load; |
| 51 | PointerOffsetPair POP; |
| 52 | /// \brief The new load needs to be created before the first load in IR order. |
| 53 | unsigned InsertOrder; |
| 54 | }; |
| 55 | |
| 56 | class LoadCombine : public BasicBlockPass { |
| 57 | LLVMContext *C; |
Hal Finkel | 840257a | 2014-11-03 23:19:16 +0000 | [diff] [blame] | 58 | AliasAnalysis *AA; |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 59 | |
| 60 | public: |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 61 | LoadCombine() : BasicBlockPass(ID), C(nullptr), AA(nullptr) { |
Chandler Carruth | 1688a77 | 2015-09-09 09:46:16 +0000 | [diff] [blame] | 62 | initializeLoadCombinePass(*PassRegistry::getPassRegistry()); |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 63 | } |
Aaron Ballman | 573f3b5 | 2014-07-30 19:23:59 +0000 | [diff] [blame] | 64 | |
| 65 | using llvm::Pass::doInitialization; |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 66 | bool doInitialization(Function &) override; |
| 67 | bool runOnBasicBlock(BasicBlock &BB) override; |
Davide Italiano | 84e1414 | 2016-06-02 22:04:43 +0000 | [diff] [blame] | 68 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 69 | AU.setPreservesCFG(); |
| 70 | AU.addRequired<AAResultsWrapperPass>(); |
| 71 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 72 | } |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 73 | |
Chad Rosier | 7ab9a7b | 2016-05-04 15:19:02 +0000 | [diff] [blame] | 74 | const char *getPassName() const override { return LDCOMBINE_NAME; } |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 75 | static char ID; |
| 76 | |
Mehdi Amini | ba9fba8 | 2016-03-13 21:05:13 +0000 | [diff] [blame] | 77 | typedef IRBuilder<TargetFolder> BuilderTy; |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 78 | |
| 79 | private: |
| 80 | BuilderTy *Builder; |
| 81 | |
| 82 | PointerOffsetPair getPointerOffsetPair(LoadInst &); |
| 83 | bool combineLoads(DenseMap<const Value *, SmallVector<LoadPOPPair, 8>> &); |
| 84 | bool aggregateLoads(SmallVectorImpl<LoadPOPPair> &); |
| 85 | bool combineLoads(SmallVectorImpl<LoadPOPPair> &); |
| 86 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 87 | } |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 88 | |
| 89 | bool LoadCombine::doInitialization(Function &F) { |
| 90 | DEBUG(dbgs() << "LoadCombine function: " << F.getName() << "\n"); |
| 91 | C = &F.getContext(); |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | |
| 95 | PointerOffsetPair LoadCombine::getPointerOffsetPair(LoadInst &LI) { |
David Majnemer | 3ffe2dd | 2016-06-19 21:36:35 +0000 | [diff] [blame^] | 96 | auto &DL = LI.getModule()->getDataLayout(); |
| 97 | |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 98 | PointerOffsetPair POP; |
| 99 | POP.Pointer = LI.getPointerOperand(); |
David Majnemer | 3ffe2dd | 2016-06-19 21:36:35 +0000 | [diff] [blame^] | 100 | unsigned BitWidth = DL.getPointerSizeInBits(LI.getPointerAddressSpace()); |
| 101 | POP.Offset = APInt(BitWidth, 0); |
| 102 | |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 103 | while (isa<BitCastInst>(POP.Pointer) || isa<GetElementPtrInst>(POP.Pointer)) { |
| 104 | if (auto *GEP = dyn_cast<GetElementPtrInst>(POP.Pointer)) { |
David Majnemer | 3ffe2dd | 2016-06-19 21:36:35 +0000 | [diff] [blame^] | 105 | APInt LastOffset = POP.Offset; |
| 106 | if (!GEP->accumulateConstantOffset(DL, POP.Offset)) { |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 107 | // Can't handle GEPs with variable indices. |
David Majnemer | 3ffe2dd | 2016-06-19 21:36:35 +0000 | [diff] [blame^] | 108 | POP.Offset = LastOffset; |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 109 | return POP; |
David Majnemer | 3ffe2dd | 2016-06-19 21:36:35 +0000 | [diff] [blame^] | 110 | } |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 111 | POP.Pointer = GEP->getPointerOperand(); |
David Majnemer | 3ffe2dd | 2016-06-19 21:36:35 +0000 | [diff] [blame^] | 112 | } else if (auto *BC = dyn_cast<BitCastInst>(POP.Pointer)) { |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 113 | POP.Pointer = BC->getOperand(0); |
David Majnemer | 3ffe2dd | 2016-06-19 21:36:35 +0000 | [diff] [blame^] | 114 | } |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 115 | } |
| 116 | return POP; |
| 117 | } |
| 118 | |
| 119 | bool LoadCombine::combineLoads( |
| 120 | DenseMap<const Value *, SmallVector<LoadPOPPair, 8>> &LoadMap) { |
| 121 | bool Combined = false; |
| 122 | for (auto &Loads : LoadMap) { |
| 123 | if (Loads.second.size() < 2) |
| 124 | continue; |
| 125 | std::sort(Loads.second.begin(), Loads.second.end(), |
| 126 | [](const LoadPOPPair &A, const LoadPOPPair &B) { |
David Majnemer | 3ffe2dd | 2016-06-19 21:36:35 +0000 | [diff] [blame^] | 127 | return A.POP.Offset.slt(B.POP.Offset); |
| 128 | }); |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 129 | if (aggregateLoads(Loads.second)) |
| 130 | Combined = true; |
| 131 | } |
| 132 | return Combined; |
| 133 | } |
| 134 | |
| 135 | /// \brief Try to aggregate loads from a sorted list of loads to be combined. |
| 136 | /// |
| 137 | /// It is guaranteed that no writes occur between any of the loads. All loads |
| 138 | /// have the same base pointer. There are at least two loads. |
| 139 | bool LoadCombine::aggregateLoads(SmallVectorImpl<LoadPOPPair> &Loads) { |
| 140 | assert(Loads.size() >= 2 && "Insufficient loads!"); |
| 141 | LoadInst *BaseLoad = nullptr; |
| 142 | SmallVector<LoadPOPPair, 8> AggregateLoads; |
| 143 | bool Combined = false; |
David Majnemer | 3119599 | 2016-06-19 06:14:56 +0000 | [diff] [blame] | 144 | bool ValidPrevOffset = false; |
David Majnemer | 3ffe2dd | 2016-06-19 21:36:35 +0000 | [diff] [blame^] | 145 | APInt PrevOffset; |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 146 | uint64_t PrevSize = 0; |
| 147 | for (auto &L : Loads) { |
David Majnemer | 3119599 | 2016-06-19 06:14:56 +0000 | [diff] [blame] | 148 | if (ValidPrevOffset == false) { |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 149 | BaseLoad = L.Load; |
| 150 | PrevOffset = L.POP.Offset; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 151 | PrevSize = L.Load->getModule()->getDataLayout().getTypeStoreSize( |
| 152 | L.Load->getType()); |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 153 | AggregateLoads.push_back(L); |
David Majnemer | 3119599 | 2016-06-19 06:14:56 +0000 | [diff] [blame] | 154 | ValidPrevOffset = true; |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 155 | continue; |
| 156 | } |
| 157 | if (L.Load->getAlignment() > BaseLoad->getAlignment()) |
| 158 | continue; |
David Majnemer | 3ffe2dd | 2016-06-19 21:36:35 +0000 | [diff] [blame^] | 159 | APInt PrevEnd = PrevOffset + PrevSize; |
| 160 | if (L.POP.Offset.sgt(PrevEnd)) { |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 161 | // No other load will be combinable |
| 162 | if (combineLoads(AggregateLoads)) |
| 163 | Combined = true; |
| 164 | AggregateLoads.clear(); |
David Majnemer | 3119599 | 2016-06-19 06:14:56 +0000 | [diff] [blame] | 165 | ValidPrevOffset = false; |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 166 | continue; |
| 167 | } |
David Majnemer | 3119599 | 2016-06-19 06:14:56 +0000 | [diff] [blame] | 168 | if (L.POP.Offset != PrevEnd) |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 169 | // This load is offset less than the size of the last load. |
| 170 | // FIXME: We may want to handle this case. |
| 171 | continue; |
| 172 | PrevOffset = L.POP.Offset; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 173 | PrevSize = L.Load->getModule()->getDataLayout().getTypeStoreSize( |
| 174 | L.Load->getType()); |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 175 | AggregateLoads.push_back(L); |
| 176 | } |
| 177 | if (combineLoads(AggregateLoads)) |
| 178 | Combined = true; |
| 179 | return Combined; |
| 180 | } |
| 181 | |
| 182 | /// \brief Given a list of combinable load. Combine the maximum number of them. |
| 183 | bool LoadCombine::combineLoads(SmallVectorImpl<LoadPOPPair> &Loads) { |
| 184 | // Remove loads from the end while the size is not a power of 2. |
| 185 | unsigned TotalSize = 0; |
| 186 | for (const auto &L : Loads) |
| 187 | TotalSize += L.Load->getType()->getPrimitiveSizeInBits(); |
| 188 | while (TotalSize != 0 && !isPowerOf2_32(TotalSize)) |
| 189 | TotalSize -= Loads.pop_back_val().Load->getType()->getPrimitiveSizeInBits(); |
| 190 | if (Loads.size() < 2) |
| 191 | return false; |
| 192 | |
| 193 | DEBUG({ |
| 194 | dbgs() << "***** Combining Loads ******\n"; |
| 195 | for (const auto &L : Loads) { |
| 196 | dbgs() << L.POP.Offset << ": " << *L.Load << "\n"; |
| 197 | } |
| 198 | }); |
| 199 | |
| 200 | // Find first load. This is where we put the new load. |
| 201 | LoadPOPPair FirstLP; |
| 202 | FirstLP.InsertOrder = -1u; |
| 203 | for (const auto &L : Loads) |
| 204 | if (L.InsertOrder < FirstLP.InsertOrder) |
| 205 | FirstLP = L; |
| 206 | |
| 207 | unsigned AddressSpace = |
| 208 | FirstLP.POP.Pointer->getType()->getPointerAddressSpace(); |
| 209 | |
| 210 | Builder->SetInsertPoint(FirstLP.Load); |
| 211 | Value *Ptr = Builder->CreateConstGEP1_64( |
| 212 | Builder->CreatePointerCast(Loads[0].POP.Pointer, |
| 213 | Builder->getInt8PtrTy(AddressSpace)), |
David Majnemer | 3ffe2dd | 2016-06-19 21:36:35 +0000 | [diff] [blame^] | 214 | Loads[0].POP.Offset.getSExtValue()); |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 215 | LoadInst *NewLoad = new LoadInst( |
| 216 | Builder->CreatePointerCast( |
| 217 | Ptr, PointerType::get(IntegerType::get(Ptr->getContext(), TotalSize), |
| 218 | Ptr->getType()->getPointerAddressSpace())), |
| 219 | Twine(Loads[0].Load->getName()) + ".combined", false, |
| 220 | Loads[0].Load->getAlignment(), FirstLP.Load); |
| 221 | |
| 222 | for (const auto &L : Loads) { |
| 223 | Builder->SetInsertPoint(L.Load); |
| 224 | Value *V = Builder->CreateExtractInteger( |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 225 | L.Load->getModule()->getDataLayout(), NewLoad, |
| 226 | cast<IntegerType>(L.Load->getType()), |
David Majnemer | 3ffe2dd | 2016-06-19 21:36:35 +0000 | [diff] [blame^] | 227 | (L.POP.Offset - Loads[0].POP.Offset).getZExtValue(), "combine.extract"); |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 228 | L.Load->replaceAllUsesWith(V); |
| 229 | } |
| 230 | |
| 231 | NumLoadsCombined = NumLoadsCombined + Loads.size(); |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | bool LoadCombine::runOnBasicBlock(BasicBlock &BB) { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 236 | if (skipBasicBlock(BB)) |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 237 | return false; |
| 238 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 239 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
Hal Finkel | 840257a | 2014-11-03 23:19:16 +0000 | [diff] [blame] | 240 | |
Mehdi Amini | ba9fba8 | 2016-03-13 21:05:13 +0000 | [diff] [blame] | 241 | IRBuilder<TargetFolder> TheBuilder( |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 242 | BB.getContext(), TargetFolder(BB.getModule()->getDataLayout())); |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 243 | Builder = &TheBuilder; |
| 244 | |
| 245 | DenseMap<const Value *, SmallVector<LoadPOPPair, 8>> LoadMap; |
Hal Finkel | 840257a | 2014-11-03 23:19:16 +0000 | [diff] [blame] | 246 | AliasSetTracker AST(*AA); |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 247 | |
| 248 | bool Combined = false; |
| 249 | unsigned Index = 0; |
| 250 | for (auto &I : BB) { |
Hal Finkel | 840257a | 2014-11-03 23:19:16 +0000 | [diff] [blame] | 251 | if (I.mayThrow() || (I.mayWriteToMemory() && AST.containsUnknown(&I))) { |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 252 | if (combineLoads(LoadMap)) |
| 253 | Combined = true; |
| 254 | LoadMap.clear(); |
Hal Finkel | 840257a | 2014-11-03 23:19:16 +0000 | [diff] [blame] | 255 | AST.clear(); |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 256 | continue; |
| 257 | } |
| 258 | LoadInst *LI = dyn_cast<LoadInst>(&I); |
| 259 | if (!LI) |
| 260 | continue; |
| 261 | ++NumLoadsAnalyzed; |
| 262 | if (!LI->isSimple() || !LI->getType()->isIntegerTy()) |
| 263 | continue; |
| 264 | auto POP = getPointerOffsetPair(*LI); |
| 265 | if (!POP.Pointer) |
| 266 | continue; |
| 267 | LoadMap[POP.Pointer].push_back(LoadPOPPair(LI, POP, Index++)); |
Hal Finkel | 840257a | 2014-11-03 23:19:16 +0000 | [diff] [blame] | 268 | AST.add(LI); |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 269 | } |
| 270 | if (combineLoads(LoadMap)) |
| 271 | Combined = true; |
| 272 | return Combined; |
| 273 | } |
| 274 | |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 275 | char LoadCombine::ID = 0; |
| 276 | |
| 277 | BasicBlockPass *llvm::createLoadCombinePass() { |
| 278 | return new LoadCombine(); |
| 279 | } |
| 280 | |
Chad Rosier | 7ab9a7b | 2016-05-04 15:19:02 +0000 | [diff] [blame] | 281 | INITIALIZE_PASS_BEGIN(LoadCombine, "load-combine", LDCOMBINE_NAME, false, false) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 282 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
Chad Rosier | 7ab9a7b | 2016-05-04 15:19:02 +0000 | [diff] [blame] | 283 | INITIALIZE_PASS_END(LoadCombine, "load-combine", LDCOMBINE_NAME, false, false) |