Bug Summary

File:lib/Transforms/Scalar/LoadCombine.cpp
Warning:line 205, 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/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"
30
31using namespace llvm;
32
33#define DEBUG_TYPE"load-combine" "load-combine"
34
35STATISTIC(NumLoadsAnalyzed, "Number of loads analyzed for combining")static llvm::Statistic NumLoadsAnalyzed = {"load-combine", "NumLoadsAnalyzed"
, "Number of loads analyzed for combining", {0}, false}
;
36STATISTIC(NumLoadsCombined, "Number of loads combined")static llvm::Statistic NumLoadsCombined = {"load-combine", "NumLoadsCombined"
, "Number of loads combined", {0}, false}
;
37
38#define LDCOMBINE_NAME"Combine Adjacent Loads" "Combine Adjacent Loads"
39
40namespace {
41struct PointerOffsetPair {
42 Value *Pointer;
43 APInt Offset;
44};
45
46struct LoadPOPPair {
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
53class LoadCombine : public BasicBlockPass {
54 LLVMContext *C;
55 AliasAnalysis *AA;
56
57public:
58 LoadCombine() : BasicBlockPass(ID), C(nullptr), AA(nullptr) {
59 initializeLoadCombinePass(*PassRegistry::getPassRegistry());
60 }
61
62 using llvm::Pass::doInitialization;
63 bool doInitialization(Function &) override;
64 bool runOnBasicBlock(BasicBlock &BB) override;
65 void getAnalysisUsage(AnalysisUsage &AU) const override {
66 AU.setPreservesCFG();
67 AU.addRequired<AAResultsWrapperPass>();
68 AU.addPreserved<GlobalsAAWrapperPass>();
69 }
70
71 StringRef getPassName() const override { return LDCOMBINE_NAME"Combine Adjacent Loads"; }
72 static char ID;
73
74 typedef IRBuilder<TargetFolder> BuilderTy;
75
76private:
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};
84}
85
86bool LoadCombine::doInitialization(Function &F) {
87 DEBUG(dbgs() << "LoadCombine function: " << F.getName() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("load-combine")) { dbgs() << "LoadCombine function: " <<
F.getName() << "\n"; } } while (false)
;
88 C = &F.getContext();
89 return true;
90}
91
92PointerOffsetPair LoadCombine::getPointerOffsetPair(LoadInst &LI) {
93 auto &DL = LI.getModule()->getDataLayout();
94
95 PointerOffsetPair POP;
96 POP.Pointer = LI.getPointerOperand();
97 unsigned BitWidth = DL.getPointerSizeInBits(LI.getPointerAddressSpace());
98 POP.Offset = APInt(BitWidth, 0);
99
100 while (isa<BitCastInst>(POP.Pointer) || isa<GetElementPtrInst>(POP.Pointer)) {
101 if (auto *GEP = dyn_cast<GetElementPtrInst>(POP.Pointer)) {
102 APInt LastOffset = POP.Offset;
103 if (!GEP->accumulateConstantOffset(DL, POP.Offset)) {
104 // Can't handle GEPs with variable indices.
105 POP.Offset = LastOffset;
106 return POP;
107 }
108 POP.Pointer = GEP->getPointerOperand();
109 } else if (auto *BC = dyn_cast<BitCastInst>(POP.Pointer)) {
110 POP.Pointer = BC->getOperand(0);
111 }
112 }
113 return POP;
114}
115
116bool 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)
4
Assuming the condition is false
5
Taking false branch
121 continue;
122 std::sort(Loads.second.begin(), Loads.second.end(),
123 [](const LoadPOPPair &A, const LoadPOPPair &B) {
124 return A.POP.Offset.slt(B.POP.Offset);
125 });
126 if (aggregateLoads(Loads.second))
6
Calling 'LoadCombine::aggregateLoads'
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.
136bool LoadCombine::aggregateLoads(SmallVectorImpl<LoadPOPPair> &Loads) {
137 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~svn298304/lib/Transforms/Scalar/LoadCombine.cpp"
, 137, __PRETTY_FUNCTION__))
;
138 LoadInst *BaseLoad = nullptr;
139 SmallVector<LoadPOPPair, 8> AggregateLoads;
140 bool Combined = false;
141 bool ValidPrevOffset = false;
142 APInt PrevOffset;
143 uint64_t PrevSize = 0;
144 for (auto &L : Loads) {
7
Assuming '__begin' is equal to '__end'
145 if (ValidPrevOffset == false) {
146 BaseLoad = L.Load;
147 PrevOffset = L.POP.Offset;
148 PrevSize = L.Load->getModule()->getDataLayout().getTypeStoreSize(
149 L.Load->getType());
150 AggregateLoads.push_back(L);
151 ValidPrevOffset = true;
152 continue;
153 }
154 if (L.Load->getAlignment() > BaseLoad->getAlignment())
155 continue;
156 APInt PrevEnd = PrevOffset + PrevSize;
157 if (L.POP.Offset.sgt(PrevEnd)) {
158 // No other load will be combinable
159 if (combineLoads(AggregateLoads))
160 Combined = true;
161 AggregateLoads.clear();
162 ValidPrevOffset = false;
163 continue;
164 }
165 if (L.POP.Offset != PrevEnd)
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;
170 PrevSize = L.Load->getModule()->getDataLayout().getTypeStoreSize(
171 L.Load->getType());
172 AggregateLoads.push_back(L);
173 }
174 if (combineLoads(AggregateLoads))
8
Calling 'LoadCombine::combineLoads'
175 Combined = true;
176 return Combined;
177}
178
179/// \brief Given a list of combinable load. Combine the maximum number of them.
180bool 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)
9
Assuming '__begin' is equal to '__end'
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)
10
Assuming the condition is false
11
Taking false branch
188 return false;
189
190 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)
191 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)
192 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)
193 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)
194 }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 })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
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)
12
Assuming '__begin' is equal to '__end'
201 if (L.InsertOrder < FirstLP.InsertOrder)
202 FirstLP = L;
203
204 unsigned AddressSpace =
205 FirstLP.POP.Pointer->getType()->getPointerAddressSpace();
13
Called C++ object pointer is uninitialized
206
207 Builder->SetInsertPoint(FirstLP.Load);
208 Value *Ptr = Builder->CreateConstGEP1_64(
209 Builder->CreatePointerCast(Loads[0].POP.Pointer,
210 Builder->getInt8PtrTy(AddressSpace)),
211 Loads[0].POP.Offset.getSExtValue());
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(
222 L.Load->getModule()->getDataLayout(), NewLoad,
223 cast<IntegerType>(L.Load->getType()),
224 (L.POP.Offset - Loads[0].POP.Offset).getZExtValue(), "combine.extract");
225 L.Load->replaceAllUsesWith(V);
226 }
227
228 NumLoadsCombined = NumLoadsCombined + Loads.size();
229 return true;
230}
231
232bool LoadCombine::runOnBasicBlock(BasicBlock &BB) {
233 if (skipBasicBlock(BB))
1
Assuming the condition is false
2
Taking false branch
234 return false;
235
236 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
237
238 IRBuilder<TargetFolder> TheBuilder(
239 BB.getContext(), TargetFolder(BB.getModule()->getDataLayout()));
240 Builder = &TheBuilder;
241
242 DenseMap<const Value *, SmallVector<LoadPOPPair, 8>> LoadMap;
243 AliasSetTracker AST(*AA);
244
245 bool Combined = false;
246 unsigned Index = 0;
247 for (auto &I : BB) {
248 if (I.mayThrow() || AST.containsUnknown(&I)) {
249 if (combineLoads(LoadMap))
250 Combined = true;
251 LoadMap.clear();
252 AST.clear();
253 continue;
254 }
255 if (I.mayWriteToMemory()) {
256 AST.add(&I);
257 continue;
258 }
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;
268 LoadMap[POP.Pointer].push_back({LI, std::move(POP), Index++});
269 AST.add(LI);
270 }
271 if (combineLoads(LoadMap))
3
Calling 'LoadCombine::combineLoads'
272 Combined = true;
273 return Combined;
274}
275
276char LoadCombine::ID = 0;
277
278BasicBlockPass *llvm::createLoadCombinePass() {
279 return new LoadCombine();
280}
281
282INITIALIZE_PASS_BEGIN(LoadCombine, "load-combine", LDCOMBINE_NAME, false, false)static void *initializeLoadCombinePassOnce(PassRegistry &
Registry) {
283INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)initializeAAResultsWrapperPassPass(Registry);
284INITIALIZE_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)); }