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