1 //===- LoadCombine.cpp - Combine Adjacent Loads ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This transformation combines adjacent loads.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Transforms/Scalar.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/Analysis/AliasAnalysis.h"
18 #include "llvm/Analysis/AliasSetTracker.h"
19 #include "llvm/Analysis/GlobalsModRef.h"
20 #include "llvm/Analysis/TargetFolder.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/IRBuilder.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/Pass.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/MathExtras.h"
29 #include "llvm/Support/raw_ostream.h"
33 #define DEBUG_TYPE "load-combine"
35 STATISTIC(NumLoadsAnalyzed, "Number of loads analyzed for combining");
36 STATISTIC(NumLoadsCombined, "Number of loads combined");
38 #define LDCOMBINE_NAME "Combine Adjacent Loads"
41 struct PointerOffsetPair {
47 LoadPOPPair() = default;
48 LoadPOPPair(LoadInst *L, PointerOffsetPair P, unsigned O)
49 : Load(L), POP(P), InsertOrder(O) {}
51 PointerOffsetPair POP;
52 /// \brief The new load needs to be created before the first load in IR order.
56 class LoadCombine : public BasicBlockPass {
61 LoadCombine() : BasicBlockPass(ID), C(nullptr), AA(nullptr) {
62 initializeLoadCombinePass(*PassRegistry::getPassRegistry());
65 using llvm::Pass::doInitialization;
66 bool doInitialization(Function &) override;
67 bool runOnBasicBlock(BasicBlock &BB) override;
68 void getAnalysisUsage(AnalysisUsage &AU) const override {
70 AU.addRequired<AAResultsWrapperPass>();
71 AU.addPreserved<GlobalsAAWrapperPass>();
74 const char *getPassName() const override { return LDCOMBINE_NAME; }
77 typedef IRBuilder<TargetFolder> BuilderTy;
82 PointerOffsetPair getPointerOffsetPair(LoadInst &);
83 bool combineLoads(DenseMap<const Value *, SmallVector<LoadPOPPair, 8>> &);
84 bool aggregateLoads(SmallVectorImpl<LoadPOPPair> &);
85 bool combineLoads(SmallVectorImpl<LoadPOPPair> &);
89 bool LoadCombine::doInitialization(Function &F) {
90 DEBUG(dbgs() << "LoadCombine function: " << F.getName() << "\n");
95 PointerOffsetPair LoadCombine::getPointerOffsetPair(LoadInst &LI) {
96 auto &DL = LI.getModule()->getDataLayout();
98 PointerOffsetPair POP;
99 POP.Pointer = LI.getPointerOperand();
100 unsigned BitWidth = DL.getPointerSizeInBits(LI.getPointerAddressSpace());
101 POP.Offset = APInt(BitWidth, 0);
103 while (isa<BitCastInst>(POP.Pointer) || isa<GetElementPtrInst>(POP.Pointer)) {
104 if (auto *GEP = dyn_cast<GetElementPtrInst>(POP.Pointer)) {
105 APInt LastOffset = POP.Offset;
106 if (!GEP->accumulateConstantOffset(DL, POP.Offset)) {
107 // Can't handle GEPs with variable indices.
108 POP.Offset = LastOffset;
111 POP.Pointer = GEP->getPointerOperand();
112 } else if (auto *BC = dyn_cast<BitCastInst>(POP.Pointer)) {
113 POP.Pointer = BC->getOperand(0);
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)
125 std::sort(Loads.second.begin(), Loads.second.end(),
126 [](const LoadPOPPair &A, const LoadPOPPair &B) {
127 return A.POP.Offset.slt(B.POP.Offset);
129 if (aggregateLoads(Loads.second))
135 /// \brief Try to aggregate loads from a sorted list of loads to be combined.
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;
144 bool ValidPrevOffset = false;
146 uint64_t PrevSize = 0;
147 for (auto &L : Loads) {
148 if (ValidPrevOffset == false) {
150 PrevOffset = L.POP.Offset;
151 PrevSize = L.Load->getModule()->getDataLayout().getTypeStoreSize(
153 AggregateLoads.push_back(L);
154 ValidPrevOffset = true;
157 if (L.Load->getAlignment() > BaseLoad->getAlignment())
159 APInt PrevEnd = PrevOffset + PrevSize;
160 if (L.POP.Offset.sgt(PrevEnd)) {
161 // No other load will be combinable
162 if (combineLoads(AggregateLoads))
164 AggregateLoads.clear();
165 ValidPrevOffset = false;
168 if (L.POP.Offset != PrevEnd)
169 // This load is offset less than the size of the last load.
170 // FIXME: We may want to handle this case.
172 PrevOffset = L.POP.Offset;
173 PrevSize = L.Load->getModule()->getDataLayout().getTypeStoreSize(
175 AggregateLoads.push_back(L);
177 if (combineLoads(AggregateLoads))
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)
194 dbgs() << "***** Combining Loads ******\n";
195 for (const auto &L : Loads) {
196 dbgs() << L.POP.Offset << ": " << *L.Load << "\n";
200 // Find first load. This is where we put the new load.
202 FirstLP.InsertOrder = -1u;
203 for (const auto &L : Loads)
204 if (L.InsertOrder < FirstLP.InsertOrder)
207 unsigned AddressSpace =
208 FirstLP.POP.Pointer->getType()->getPointerAddressSpace();
210 Builder->SetInsertPoint(FirstLP.Load);
211 Value *Ptr = Builder->CreateConstGEP1_64(
212 Builder->CreatePointerCast(Loads[0].POP.Pointer,
213 Builder->getInt8PtrTy(AddressSpace)),
214 Loads[0].POP.Offset.getSExtValue());
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);
222 for (const auto &L : Loads) {
223 Builder->SetInsertPoint(L.Load);
224 Value *V = Builder->CreateExtractInteger(
225 L.Load->getModule()->getDataLayout(), NewLoad,
226 cast<IntegerType>(L.Load->getType()),
227 (L.POP.Offset - Loads[0].POP.Offset).getZExtValue(), "combine.extract");
228 L.Load->replaceAllUsesWith(V);
231 NumLoadsCombined = NumLoadsCombined + Loads.size();
235 bool LoadCombine::runOnBasicBlock(BasicBlock &BB) {
236 if (skipBasicBlock(BB))
239 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
241 IRBuilder<TargetFolder> TheBuilder(
242 BB.getContext(), TargetFolder(BB.getModule()->getDataLayout()));
243 Builder = &TheBuilder;
245 DenseMap<const Value *, SmallVector<LoadPOPPair, 8>> LoadMap;
246 AliasSetTracker AST(*AA);
248 bool Combined = false;
251 if (I.mayThrow() || (I.mayWriteToMemory() && AST.containsUnknown(&I))) {
252 if (combineLoads(LoadMap))
258 LoadInst *LI = dyn_cast<LoadInst>(&I);
262 if (!LI->isSimple() || !LI->getType()->isIntegerTy())
264 auto POP = getPointerOffsetPair(*LI);
267 LoadMap[POP.Pointer].push_back(LoadPOPPair(LI, POP, Index++));
270 if (combineLoads(LoadMap))
275 char LoadCombine::ID = 0;
277 BasicBlockPass *llvm::createLoadCombinePass() {
278 return new LoadCombine();
281 INITIALIZE_PASS_BEGIN(LoadCombine, "load-combine", LDCOMBINE_NAME, false, false)
282 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
283 INITIALIZE_PASS_END(LoadCombine, "load-combine", LDCOMBINE_NAME, false, false)