Bug Summary

File:lib/Transforms/Scalar/LoadCombine.cpp
Warning:line 208, column 7
Called C++ object pointer is uninitialized

Annotated Source Code

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"
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/Dominators.h"
23#include "llvm/IR/Function.h"
24#include "llvm/IR/IRBuilder.h"
25#include "llvm/IR/Instructions.h"
26#include "llvm/IR/Module.h"
27#include "llvm/Pass.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/MathExtras.h"
30#include "llvm/Support/raw_ostream.h"
31
32using namespace llvm;
33
34#define DEBUG_TYPE"load-combine" "load-combine"
35
36STATISTIC(NumLoadsAnalyzed, "Number of loads analyzed for combining")static llvm::Statistic NumLoadsAnalyzed = {"load-combine", "NumLoadsAnalyzed"
, "Number of loads analyzed for combining", {0}, false}
;
37STATISTIC(NumLoadsCombined, "Number of loads combined")static llvm::Statistic NumLoadsCombined = {"load-combine", "NumLoadsCombined"
, "Number of loads combined", {0}, false}
;
38
39#define LDCOMBINE_NAME"Combine Adjacent Loads" "Combine Adjacent Loads"
40
41namespace {
42struct PointerOffsetPair {
43 Value *Pointer;
44 APInt Offset;
45};
46
47struct LoadPOPPair {
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
54class LoadCombine : public BasicBlockPass {
55 LLVMContext *C;
56 AliasAnalysis *AA;
57 DominatorTree *DT;
58
59public:
60 LoadCombine() : BasicBlockPass(ID), C(nullptr), AA(nullptr) {
61 initializeLoadCombinePass(*PassRegistry::getPassRegistry());
62 }
63
64 using llvm::Pass::doInitialization;
65 bool doInitialization(Function &) override;
66 bool runOnBasicBlock(BasicBlock &BB) override;
67 void getAnalysisUsage(AnalysisUsage &AU) const override {
68 AU.setPreservesCFG();
69 AU.addRequired<AAResultsWrapperPass>();
70 AU.addRequired<DominatorTreeWrapperPass>();
71 AU.addPreserved<GlobalsAAWrapperPass>();
72 }
73
74 StringRef getPassName() const override { return LDCOMBINE_NAME"Combine Adjacent Loads"; }
75 static char ID;
76
77 typedef IRBuilder<TargetFolder> BuilderTy;
78
79private:
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};
87}
88
89bool LoadCombine::doInitialization(Function &F) {
90 DEBUG(dbgs() << "LoadCombine function: " << F.getName() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("load-combine")) { dbgs() << "LoadCombine function: " <<
F.getName() << "\n"; } } while (false)
;
91 C = &F.getContext();
92 return true;
93}
94
95PointerOffsetPair LoadCombine::getPointerOffsetPair(LoadInst &LI) {
96 auto &DL = LI.getModule()->getDataLayout();
97
98 PointerOffsetPair POP;
99 POP.Pointer = LI.getPointerOperand();
100 unsigned BitWidth = DL.getPointerSizeInBits(LI.getPointerAddressSpace());
101 POP.Offset = APInt(BitWidth, 0);
102
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;
109 return POP;
110 }
111 POP.Pointer = GEP->getPointerOperand();
112 } else if (auto *BC = dyn_cast<BitCastInst>(POP.Pointer)) {
113 POP.Pointer = BC->getOperand(0);
114 }
115 }
116 return POP;
117}
118
119bool 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)
6
Assuming the condition is false
7
Taking false branch
124 continue;
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);
128 });
129 if (aggregateLoads(Loads.second))
8
Calling 'LoadCombine::aggregateLoads'
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.
139bool LoadCombine::aggregateLoads(SmallVectorImpl<LoadPOPPair> &Loads) {
140 assert(Loads.size() >= 2 && "Insufficient loads!")((Loads.size() >= 2 && "Insufficient loads!") ? static_cast
<void> (0) : __assert_fail ("Loads.size() >= 2 && \"Insufficient loads!\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/lib/Transforms/Scalar/LoadCombine.cpp"
, 140, __PRETTY_FUNCTION__))
;
141 LoadInst *BaseLoad = nullptr;
142 SmallVector<LoadPOPPair, 8> AggregateLoads;
143 bool Combined = false;
144 bool ValidPrevOffset = false;
145 APInt PrevOffset;
146 uint64_t PrevSize = 0;
147 for (auto &L : Loads) {
9
Assuming '__begin' is equal to '__end'
148 if (ValidPrevOffset == false) {
149 BaseLoad = L.Load;
150 PrevOffset = L.POP.Offset;
151 PrevSize = L.Load->getModule()->getDataLayout().getTypeStoreSize(
152 L.Load->getType());
153 AggregateLoads.push_back(L);
154 ValidPrevOffset = true;
155 continue;
156 }
157 if (L.Load->getAlignment() > BaseLoad->getAlignment())
158 continue;
159 APInt PrevEnd = PrevOffset + PrevSize;
160 if (L.POP.Offset.sgt(PrevEnd)) {
161 // No other load will be combinable
162 if (combineLoads(AggregateLoads))
163 Combined = true;
164 AggregateLoads.clear();
165 ValidPrevOffset = false;
166 continue;
167 }
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.
171 continue;
172 PrevOffset = L.POP.Offset;
173 PrevSize = L.Load->getModule()->getDataLayout().getTypeStoreSize(
174 L.Load->getType());
175 AggregateLoads.push_back(L);
176 }
177 if (combineLoads(AggregateLoads))
10
Calling 'LoadCombine::combineLoads'
178 Combined = true;
179 return Combined;
180}
181
182/// \brief Given a list of combinable load. Combine the maximum number of them.
183bool 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)
11
Assuming '__begin' is equal to '__end'
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)
12
Assuming the condition is false
13
Taking false branch
191 return false;
192
193 DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("load-combine")) { { dbgs() << "***** Combining Loads ******\n"
; for (const auto &L : Loads) { dbgs() << L.POP.Offset
<< ": " << *L.Load << "\n"; } }; } } while
(false)
194 dbgs() << "***** Combining Loads ******\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("load-combine")) { { dbgs() << "***** Combining Loads ******\n"
; for (const auto &L : Loads) { dbgs() << L.POP.Offset
<< ": " << *L.Load << "\n"; } }; } } while
(false)
195 for (const auto &L : Loads) {do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("load-combine")) { { dbgs() << "***** Combining Loads ******\n"
; for (const auto &L : Loads) { dbgs() << L.POP.Offset
<< ": " << *L.Load << "\n"; } }; } } while
(false)
196 dbgs() << L.POP.Offset << ": " << *L.Load << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("load-combine")) { { dbgs() << "***** Combining Loads ******\n"
; for (const auto &L : Loads) { dbgs() << L.POP.Offset
<< ": " << *L.Load << "\n"; } }; } } while
(false)
197 }do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("load-combine")) { { dbgs() << "***** Combining Loads ******\n"
; for (const auto &L : Loads) { dbgs() << L.POP.Offset
<< ": " << *L.Load << "\n"; } }; } } while
(false)
198 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("load-combine")) { { dbgs() << "***** Combining Loads ******\n"
; for (const auto &L : Loads) { dbgs() << L.POP.Offset
<< ": " << *L.Load << "\n"; } }; } } while
(false)
;
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)
14
Assuming '__begin' is equal to '__end'
204 if (L.InsertOrder < FirstLP.InsertOrder)
205 FirstLP = L;
206
207 unsigned AddressSpace =
208 FirstLP.POP.Pointer->getType()->getPointerAddressSpace();
15
Called C++ object pointer is uninitialized
209
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);
221
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);
229 }
230
231 NumLoadsCombined += Loads.size();
232 return true;
233}
234
235bool LoadCombine::runOnBasicBlock(BasicBlock &BB) {
236 if (skipBasicBlock(BB))
1
Assuming the condition is false
2
Taking false branch
237 return false;
238
239 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
240 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
241
242 // Skip analysing dead blocks (not forward reachable from function entry).
243 if (!DT->isReachableFromEntry(&BB)) {
3
Assuming the condition is false
4
Taking false branch
244 DEBUG(dbgs() << "LC: skipping unreachable " << BB.getName() <<do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("load-combine")) { dbgs() << "LC: skipping unreachable "
<< BB.getName() << " in " << BB.getParent(
)->getName() << "\n"; } } while (false)
245 " in " << BB.getParent()->getName() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("load-combine")) { dbgs() << "LC: skipping unreachable "
<< BB.getName() << " in " << BB.getParent(
)->getName() << "\n"; } } while (false)
;
246 return false;
247 }
248
249 IRBuilder<TargetFolder> TheBuilder(
250 BB.getContext(), TargetFolder(BB.getModule()->getDataLayout()));
251 Builder = &TheBuilder;
252
253 DenseMap<const Value *, SmallVector<LoadPOPPair, 8>> LoadMap;
254 AliasSetTracker AST(*AA);
255
256 bool Combined = false;
257 unsigned Index = 0;
258 for (auto &I : BB) {
259 if (I.mayThrow() || AST.containsUnknown(&I)) {
260 if (combineLoads(LoadMap))
261 Combined = true;
262 LoadMap.clear();
263 AST.clear();
264 continue;
265 }
266 if (I.mayWriteToMemory()) {
267 AST.add(&I);
268 continue;
269 }
270 LoadInst *LI = dyn_cast<LoadInst>(&I);
271 if (!LI)
272 continue;
273 ++NumLoadsAnalyzed;
274 if (!LI->isSimple() || !LI->getType()->isIntegerTy())
275 continue;
276 auto POP = getPointerOffsetPair(*LI);
277 if (!POP.Pointer)
278 continue;
279 LoadMap[POP.Pointer].push_back({LI, std::move(POP), Index++});
280 AST.add(LI);
281 }
282 if (combineLoads(LoadMap))
5
Calling 'LoadCombine::combineLoads'
283 Combined = true;
284 return Combined;
285}
286
287char LoadCombine::ID = 0;
288
289BasicBlockPass *llvm::createLoadCombinePass() {
290 return new LoadCombine();
291}
292
293INITIALIZE_PASS_BEGIN(LoadCombine, "load-combine", LDCOMBINE_NAME, false, false)static void *initializeLoadCombinePassOnce(PassRegistry &
Registry) {
294INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)initializeAAResultsWrapperPassPass(Registry);
295INITIALIZE_PASS_END(LoadCombine, "load-combine", LDCOMBINE_NAME, false, false)PassInfo *PI = new PassInfo( "Combine Adjacent Loads", "load-combine"
, &LoadCombine::ID, PassInfo::NormalCtor_t(callDefaultCtor
<LoadCombine>), false, false); Registry.registerPass(*PI
, true); return PI; } static llvm::once_flag InitializeLoadCombinePassFlag
; void llvm::initializeLoadCombinePass(PassRegistry &Registry
) { llvm::call_once(InitializeLoadCombinePassFlag, initializeLoadCombinePassOnce
, std::ref(Registry)); }