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/TargetFolder.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/IRBuilder.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/raw_ostream.h"
32 #define DEBUG_TYPE "load-combine"
34 STATISTIC(NumLoadsAnalyzed, "Number of loads analyzed for combining");
35 STATISTIC(NumLoadsCombined, "Number of loads combined");
38 struct PointerOffsetPair {
44 LoadPOPPair() = default;
45 LoadPOPPair(LoadInst *L, PointerOffsetPair P, unsigned O)
46 : Load(L), POP(P), InsertOrder(O) {}
48 PointerOffsetPair POP;
49 /// \brief The new load needs to be created before the first load in IR order.
53 class LoadCombine : public BasicBlockPass {
58 LoadCombine() : BasicBlockPass(ID), C(nullptr), AA(nullptr) {
59 initializeSROAPass(*PassRegistry::getPassRegistry());
62 using llvm::Pass::doInitialization;
63 bool doInitialization(Function &) override;
64 bool runOnBasicBlock(BasicBlock &BB) override;
65 void getAnalysisUsage(AnalysisUsage &AU) const override;
67 const char *getPassName() const override { return "LoadCombine"; }
70 typedef IRBuilder<true, TargetFolder> BuilderTy;
75 PointerOffsetPair getPointerOffsetPair(LoadInst &);
76 bool combineLoads(DenseMap<const Value *, SmallVector<LoadPOPPair, 8>> &);
77 bool aggregateLoads(SmallVectorImpl<LoadPOPPair> &);
78 bool combineLoads(SmallVectorImpl<LoadPOPPair> &);
82 bool LoadCombine::doInitialization(Function &F) {
83 DEBUG(dbgs() << "LoadCombine function: " << F.getName() << "\n");
88 PointerOffsetPair LoadCombine::getPointerOffsetPair(LoadInst &LI) {
89 PointerOffsetPair POP;
90 POP.Pointer = LI.getPointerOperand();
92 while (isa<BitCastInst>(POP.Pointer) || isa<GetElementPtrInst>(POP.Pointer)) {
93 if (auto *GEP = dyn_cast<GetElementPtrInst>(POP.Pointer)) {
94 auto &DL = LI.getModule()->getDataLayout();
95 unsigned BitWidth = DL.getPointerTypeSizeInBits(GEP->getType());
96 APInt Offset(BitWidth, 0);
97 if (GEP->accumulateConstantOffset(DL, Offset))
98 POP.Offset += Offset.getZExtValue();
100 // Can't handle GEPs with variable indices.
102 POP.Pointer = GEP->getPointerOperand();
103 } else if (auto *BC = dyn_cast<BitCastInst>(POP.Pointer))
104 POP.Pointer = BC->getOperand(0);
109 bool LoadCombine::combineLoads(
110 DenseMap<const Value *, SmallVector<LoadPOPPair, 8>> &LoadMap) {
111 bool Combined = false;
112 for (auto &Loads : LoadMap) {
113 if (Loads.second.size() < 2)
115 std::sort(Loads.second.begin(), Loads.second.end(),
116 [](const LoadPOPPair &A, const LoadPOPPair &B) {
117 return A.POP.Offset < B.POP.Offset;
119 if (aggregateLoads(Loads.second))
125 /// \brief Try to aggregate loads from a sorted list of loads to be combined.
127 /// It is guaranteed that no writes occur between any of the loads. All loads
128 /// have the same base pointer. There are at least two loads.
129 bool LoadCombine::aggregateLoads(SmallVectorImpl<LoadPOPPair> &Loads) {
130 assert(Loads.size() >= 2 && "Insufficient loads!");
131 LoadInst *BaseLoad = nullptr;
132 SmallVector<LoadPOPPair, 8> AggregateLoads;
133 bool Combined = false;
134 uint64_t PrevOffset = -1ull;
135 uint64_t PrevSize = 0;
136 for (auto &L : Loads) {
137 if (PrevOffset == -1ull) {
139 PrevOffset = L.POP.Offset;
140 PrevSize = L.Load->getModule()->getDataLayout().getTypeStoreSize(
142 AggregateLoads.push_back(L);
145 if (L.Load->getAlignment() > BaseLoad->getAlignment())
147 if (L.POP.Offset > PrevOffset + PrevSize) {
148 // No other load will be combinable
149 if (combineLoads(AggregateLoads))
151 AggregateLoads.clear();
155 if (L.POP.Offset != PrevOffset + PrevSize)
156 // This load is offset less than the size of the last load.
157 // FIXME: We may want to handle this case.
159 PrevOffset = L.POP.Offset;
160 PrevSize = L.Load->getModule()->getDataLayout().getTypeStoreSize(
162 AggregateLoads.push_back(L);
164 if (combineLoads(AggregateLoads))
169 /// \brief Given a list of combinable load. Combine the maximum number of them.
170 bool LoadCombine::combineLoads(SmallVectorImpl<LoadPOPPair> &Loads) {
171 // Remove loads from the end while the size is not a power of 2.
172 unsigned TotalSize = 0;
173 for (const auto &L : Loads)
174 TotalSize += L.Load->getType()->getPrimitiveSizeInBits();
175 while (TotalSize != 0 && !isPowerOf2_32(TotalSize))
176 TotalSize -= Loads.pop_back_val().Load->getType()->getPrimitiveSizeInBits();
177 if (Loads.size() < 2)
181 dbgs() << "***** Combining Loads ******\n";
182 for (const auto &L : Loads) {
183 dbgs() << L.POP.Offset << ": " << *L.Load << "\n";
187 // Find first load. This is where we put the new load.
189 FirstLP.InsertOrder = -1u;
190 for (const auto &L : Loads)
191 if (L.InsertOrder < FirstLP.InsertOrder)
194 unsigned AddressSpace =
195 FirstLP.POP.Pointer->getType()->getPointerAddressSpace();
197 Builder->SetInsertPoint(FirstLP.Load);
198 Value *Ptr = Builder->CreateConstGEP1_64(
199 Builder->CreatePointerCast(Loads[0].POP.Pointer,
200 Builder->getInt8PtrTy(AddressSpace)),
201 Loads[0].POP.Offset);
202 LoadInst *NewLoad = new LoadInst(
203 Builder->CreatePointerCast(
204 Ptr, PointerType::get(IntegerType::get(Ptr->getContext(), TotalSize),
205 Ptr->getType()->getPointerAddressSpace())),
206 Twine(Loads[0].Load->getName()) + ".combined", false,
207 Loads[0].Load->getAlignment(), FirstLP.Load);
209 for (const auto &L : Loads) {
210 Builder->SetInsertPoint(L.Load);
211 Value *V = Builder->CreateExtractInteger(
212 L.Load->getModule()->getDataLayout(), NewLoad,
213 cast<IntegerType>(L.Load->getType()),
214 L.POP.Offset - Loads[0].POP.Offset, "combine.extract");
215 L.Load->replaceAllUsesWith(V);
218 NumLoadsCombined = NumLoadsCombined + Loads.size();
222 bool LoadCombine::runOnBasicBlock(BasicBlock &BB) {
223 if (skipOptnoneFunction(BB))
226 AA = &getAnalysis<AliasAnalysis>();
228 IRBuilder<true, TargetFolder> TheBuilder(
229 BB.getContext(), TargetFolder(BB.getModule()->getDataLayout()));
230 Builder = &TheBuilder;
232 DenseMap<const Value *, SmallVector<LoadPOPPair, 8>> LoadMap;
233 AliasSetTracker AST(*AA);
235 bool Combined = false;
238 if (I.mayThrow() || (I.mayWriteToMemory() && AST.containsUnknown(&I))) {
239 if (combineLoads(LoadMap))
245 LoadInst *LI = dyn_cast<LoadInst>(&I);
249 if (!LI->isSimple() || !LI->getType()->isIntegerTy())
251 auto POP = getPointerOffsetPair(*LI);
254 LoadMap[POP.Pointer].push_back(LoadPOPPair(LI, POP, Index++));
257 if (combineLoads(LoadMap))
262 void LoadCombine::getAnalysisUsage(AnalysisUsage &AU) const {
263 AU.setPreservesCFG();
265 AU.addRequired<AliasAnalysis>();
266 AU.addPreserved<AliasAnalysis>();
269 char LoadCombine::ID = 0;
271 BasicBlockPass *llvm::createLoadCombinePass() {
272 return new LoadCombine();
275 INITIALIZE_PASS_BEGIN(LoadCombine, "load-combine", "Combine Adjacent Loads",
277 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
278 INITIALIZE_PASS_END(LoadCombine, "load-combine", "Combine Adjacent Loads",