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