Bug Summary

File:build/source/llvm/lib/Transforms/Scalar/GVN.cpp
Warning:line 3095, column 29
Although the value stored to 'P' is used in the enclosing expression, the value is never actually read from 'P'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name GVN.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins -resource-dir /usr/lib/llvm-17/lib/clang/17 -D _DEBUG -D _GLIBCXX_ASSERTIONS -D _GNU_SOURCE -D _LIBCPP_ENABLE_ASSERTIONS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/Transforms/Scalar -I /build/source/llvm/lib/Transforms/Scalar -I include -I /build/source/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-17/lib/clang/17/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fmacro-prefix-map=/build/source/= -fcoverage-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fcoverage-prefix-map=/build/source/= -source-date-epoch 1683717183 -O2 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -Wno-misleading-indentation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/source/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2023-05-10-133810-16478-1 -x c++ /build/source/llvm/lib/Transforms/Scalar/GVN.cpp
1//===- GVN.cpp - Eliminate redundant values and loads ---------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass performs global value numbering to eliminate fully redundant
10// instructions. It also performs simple dead load elimination.
11//
12// Note that this pass does the value numbering itself; it does not use the
13// ValueNumbering analysis passes.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/Transforms/Scalar/GVN.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/DepthFirstIterator.h"
20#include "llvm/ADT/Hashing.h"
21#include "llvm/ADT/MapVector.h"
22#include "llvm/ADT/PostOrderIterator.h"
23#include "llvm/ADT/STLExtras.h"
24#include "llvm/ADT/SetVector.h"
25#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/SmallVector.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/Analysis/AliasAnalysis.h"
29#include "llvm/Analysis/AssumeBundleQueries.h"
30#include "llvm/Analysis/AssumptionCache.h"
31#include "llvm/Analysis/CFG.h"
32#include "llvm/Analysis/DomTreeUpdater.h"
33#include "llvm/Analysis/GlobalsModRef.h"
34#include "llvm/Analysis/InstructionPrecedenceTracking.h"
35#include "llvm/Analysis/InstructionSimplify.h"
36#include "llvm/Analysis/LoopInfo.h"
37#include "llvm/Analysis/MemoryBuiltins.h"
38#include "llvm/Analysis/MemoryDependenceAnalysis.h"
39#include "llvm/Analysis/MemorySSA.h"
40#include "llvm/Analysis/MemorySSAUpdater.h"
41#include "llvm/Analysis/OptimizationRemarkEmitter.h"
42#include "llvm/Analysis/PHITransAddr.h"
43#include "llvm/Analysis/TargetLibraryInfo.h"
44#include "llvm/Analysis/ValueTracking.h"
45#include "llvm/IR/Attributes.h"
46#include "llvm/IR/BasicBlock.h"
47#include "llvm/IR/Constant.h"
48#include "llvm/IR/Constants.h"
49#include "llvm/IR/DebugLoc.h"
50#include "llvm/IR/Dominators.h"
51#include "llvm/IR/Function.h"
52#include "llvm/IR/InstrTypes.h"
53#include "llvm/IR/Instruction.h"
54#include "llvm/IR/Instructions.h"
55#include "llvm/IR/IntrinsicInst.h"
56#include "llvm/IR/LLVMContext.h"
57#include "llvm/IR/Metadata.h"
58#include "llvm/IR/Module.h"
59#include "llvm/IR/PassManager.h"
60#include "llvm/IR/PatternMatch.h"
61#include "llvm/IR/Type.h"
62#include "llvm/IR/Use.h"
63#include "llvm/IR/Value.h"
64#include "llvm/InitializePasses.h"
65#include "llvm/Pass.h"
66#include "llvm/Support/Casting.h"
67#include "llvm/Support/CommandLine.h"
68#include "llvm/Support/Compiler.h"
69#include "llvm/Support/Debug.h"
70#include "llvm/Support/raw_ostream.h"
71#include "llvm/Transforms/Utils/AssumeBundleBuilder.h"
72#include "llvm/Transforms/Utils/BasicBlockUtils.h"
73#include "llvm/Transforms/Utils/Local.h"
74#include "llvm/Transforms/Utils/SSAUpdater.h"
75#include "llvm/Transforms/Utils/VNCoercion.h"
76#include <algorithm>
77#include <cassert>
78#include <cstdint>
79#include <optional>
80#include <utility>
81
82using namespace llvm;
83using namespace llvm::gvn;
84using namespace llvm::VNCoercion;
85using namespace PatternMatch;
86
87#define DEBUG_TYPE"gvn" "gvn"
88
89STATISTIC(NumGVNInstr, "Number of instructions deleted")static llvm::Statistic NumGVNInstr = {"gvn", "NumGVNInstr", "Number of instructions deleted"
}
;
90STATISTIC(NumGVNLoad, "Number of loads deleted")static llvm::Statistic NumGVNLoad = {"gvn", "NumGVNLoad", "Number of loads deleted"
}
;
91STATISTIC(NumGVNPRE, "Number of instructions PRE'd")static llvm::Statistic NumGVNPRE = {"gvn", "NumGVNPRE", "Number of instructions PRE'd"
}
;
92STATISTIC(NumGVNBlocks, "Number of blocks merged")static llvm::Statistic NumGVNBlocks = {"gvn", "NumGVNBlocks",
"Number of blocks merged"}
;
93STATISTIC(NumGVNSimpl, "Number of instructions simplified")static llvm::Statistic NumGVNSimpl = {"gvn", "NumGVNSimpl", "Number of instructions simplified"
}
;
94STATISTIC(NumGVNEqProp, "Number of equalities propagated")static llvm::Statistic NumGVNEqProp = {"gvn", "NumGVNEqProp",
"Number of equalities propagated"}
;
95STATISTIC(NumPRELoad, "Number of loads PRE'd")static llvm::Statistic NumPRELoad = {"gvn", "NumPRELoad", "Number of loads PRE'd"
}
;
96STATISTIC(NumPRELoopLoad, "Number of loop loads PRE'd")static llvm::Statistic NumPRELoopLoad = {"gvn", "NumPRELoopLoad"
, "Number of loop loads PRE'd"}
;
97
98STATISTIC(IsValueFullyAvailableInBlockNumSpeculationsMax,static llvm::Statistic IsValueFullyAvailableInBlockNumSpeculationsMax
= {"gvn", "IsValueFullyAvailableInBlockNumSpeculationsMax", "Number of blocks speculated as available in "
"IsValueFullyAvailableInBlock(), max"}
99 "Number of blocks speculated as available in "static llvm::Statistic IsValueFullyAvailableInBlockNumSpeculationsMax
= {"gvn", "IsValueFullyAvailableInBlockNumSpeculationsMax", "Number of blocks speculated as available in "
"IsValueFullyAvailableInBlock(), max"}
100 "IsValueFullyAvailableInBlock(), max")static llvm::Statistic IsValueFullyAvailableInBlockNumSpeculationsMax
= {"gvn", "IsValueFullyAvailableInBlockNumSpeculationsMax", "Number of blocks speculated as available in "
"IsValueFullyAvailableInBlock(), max"}
;
101STATISTIC(MaxBBSpeculationCutoffReachedTimes,static llvm::Statistic MaxBBSpeculationCutoffReachedTimes = {
"gvn", "MaxBBSpeculationCutoffReachedTimes", "Number of times we we reached gvn-max-block-speculations cut-off "
"preventing further exploration"}
102 "Number of times we we reached gvn-max-block-speculations cut-off "static llvm::Statistic MaxBBSpeculationCutoffReachedTimes = {
"gvn", "MaxBBSpeculationCutoffReachedTimes", "Number of times we we reached gvn-max-block-speculations cut-off "
"preventing further exploration"}
103 "preventing further exploration")static llvm::Statistic MaxBBSpeculationCutoffReachedTimes = {
"gvn", "MaxBBSpeculationCutoffReachedTimes", "Number of times we we reached gvn-max-block-speculations cut-off "
"preventing further exploration"}
;
104
105static cl::opt<bool> GVNEnablePRE("enable-pre", cl::init(true), cl::Hidden);
106static cl::opt<bool> GVNEnableLoadPRE("enable-load-pre", cl::init(true));
107static cl::opt<bool> GVNEnableLoadInLoopPRE("enable-load-in-loop-pre",
108 cl::init(true));
109static cl::opt<bool>
110GVNEnableSplitBackedgeInLoadPRE("enable-split-backedge-in-load-pre",
111 cl::init(false));
112static cl::opt<bool> GVNEnableMemDep("enable-gvn-memdep", cl::init(true));
113
114static cl::opt<uint32_t> MaxNumDeps(
115 "gvn-max-num-deps", cl::Hidden, cl::init(100),
116 cl::desc("Max number of dependences to attempt Load PRE (default = 100)"));
117
118// This is based on IsValueFullyAvailableInBlockNumSpeculationsMax stat.
119static cl::opt<uint32_t> MaxBBSpeculations(
120 "gvn-max-block-speculations", cl::Hidden, cl::init(600),
121 cl::desc("Max number of blocks we're willing to speculate on (and recurse "
122 "into) when deducing if a value is fully available or not in GVN "
123 "(default = 600)"));
124
125static cl::opt<uint32_t> MaxNumVisitedInsts(
126 "gvn-max-num-visited-insts", cl::Hidden, cl::init(100),
127 cl::desc("Max number of visited instructions when trying to find "
128 "dominating value of select dependency (default = 100)"));
129
130struct llvm::GVNPass::Expression {
131 uint32_t opcode;
132 bool commutative = false;
133 // The type is not necessarily the result type of the expression, it may be
134 // any additional type needed to disambiguate the expression.
135 Type *type = nullptr;
136 SmallVector<uint32_t, 4> varargs;
137
138 Expression(uint32_t o = ~2U) : opcode(o) {}
139
140 bool operator==(const Expression &other) const {
141 if (opcode != other.opcode)
142 return false;
143 if (opcode == ~0U || opcode == ~1U)
144 return true;
145 if (type != other.type)
146 return false;
147 if (varargs != other.varargs)
148 return false;
149 return true;
150 }
151
152 friend hash_code hash_value(const Expression &Value) {
153 return hash_combine(
154 Value.opcode, Value.type,
155 hash_combine_range(Value.varargs.begin(), Value.varargs.end()));
156 }
157};
158
159namespace llvm {
160
161template <> struct DenseMapInfo<GVNPass::Expression> {
162 static inline GVNPass::Expression getEmptyKey() { return ~0U; }
163 static inline GVNPass::Expression getTombstoneKey() { return ~1U; }
164
165 static unsigned getHashValue(const GVNPass::Expression &e) {
166 using llvm::hash_value;
167
168 return static_cast<unsigned>(hash_value(e));
169 }
170
171 static bool isEqual(const GVNPass::Expression &LHS,
172 const GVNPass::Expression &RHS) {
173 return LHS == RHS;
174 }
175};
176
177} // end namespace llvm
178
179/// Represents a particular available value that we know how to materialize.
180/// Materialization of an AvailableValue never fails. An AvailableValue is
181/// implicitly associated with a rematerialization point which is the
182/// location of the instruction from which it was formed.
183struct llvm::gvn::AvailableValue {
184 enum class ValType {
185 SimpleVal, // A simple offsetted value that is accessed.
186 LoadVal, // A value produced by a load.
187 MemIntrin, // A memory intrinsic which is loaded from.
188 UndefVal, // A UndefValue representing a value from dead block (which
189 // is not yet physically removed from the CFG).
190 SelectVal, // A pointer select which is loaded from and for which the load
191 // can be replace by a value select.
192 };
193
194 /// Val - The value that is live out of the block.
195 Value *Val;
196 /// Kind of the live-out value.
197 ValType Kind;
198
199 /// Offset - The byte offset in Val that is interesting for the load query.
200 unsigned Offset = 0;
201 /// V1, V2 - The dominating non-clobbered values of SelectVal.
202 Value *V1 = nullptr, *V2 = nullptr;
203
204 static AvailableValue get(Value *V, unsigned Offset = 0) {
205 AvailableValue Res;
206 Res.Val = V;
207 Res.Kind = ValType::SimpleVal;
208 Res.Offset = Offset;
209 return Res;
210 }
211
212 static AvailableValue getMI(MemIntrinsic *MI, unsigned Offset = 0) {
213 AvailableValue Res;
214 Res.Val = MI;
215 Res.Kind = ValType::MemIntrin;
216 Res.Offset = Offset;
217 return Res;
218 }
219
220 static AvailableValue getLoad(LoadInst *Load, unsigned Offset = 0) {
221 AvailableValue Res;
222 Res.Val = Load;
223 Res.Kind = ValType::LoadVal;
224 Res.Offset = Offset;
225 return Res;
226 }
227
228 static AvailableValue getUndef() {
229 AvailableValue Res;
230 Res.Val = nullptr;
231 Res.Kind = ValType::UndefVal;
232 Res.Offset = 0;
233 return Res;
234 }
235
236 static AvailableValue getSelect(SelectInst *Sel, Value *V1, Value *V2) {
237 AvailableValue Res;
238 Res.Val = Sel;
239 Res.Kind = ValType::SelectVal;
240 Res.Offset = 0;
241 Res.V1 = V1;
242 Res.V2 = V2;
243 return Res;
244 }
245
246 bool isSimpleValue() const { return Kind == ValType::SimpleVal; }
247 bool isCoercedLoadValue() const { return Kind == ValType::LoadVal; }
248 bool isMemIntrinValue() const { return Kind == ValType::MemIntrin; }
249 bool isUndefValue() const { return Kind == ValType::UndefVal; }
250 bool isSelectValue() const { return Kind == ValType::SelectVal; }
251
252 Value *getSimpleValue() const {
253 assert(isSimpleValue() && "Wrong accessor")(static_cast <bool> (isSimpleValue() && "Wrong accessor"
) ? void (0) : __assert_fail ("isSimpleValue() && \"Wrong accessor\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 253, __extension__ __PRETTY_FUNCTION__
))
;
254 return Val;
255 }
256
257 LoadInst *getCoercedLoadValue() const {
258 assert(isCoercedLoadValue() && "Wrong accessor")(static_cast <bool> (isCoercedLoadValue() && "Wrong accessor"
) ? void (0) : __assert_fail ("isCoercedLoadValue() && \"Wrong accessor\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 258, __extension__ __PRETTY_FUNCTION__
))
;
259 return cast<LoadInst>(Val);
260 }
261
262 MemIntrinsic *getMemIntrinValue() const {
263 assert(isMemIntrinValue() && "Wrong accessor")(static_cast <bool> (isMemIntrinValue() && "Wrong accessor"
) ? void (0) : __assert_fail ("isMemIntrinValue() && \"Wrong accessor\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 263, __extension__ __PRETTY_FUNCTION__
))
;
264 return cast<MemIntrinsic>(Val);
265 }
266
267 SelectInst *getSelectValue() const {
268 assert(isSelectValue() && "Wrong accessor")(static_cast <bool> (isSelectValue() && "Wrong accessor"
) ? void (0) : __assert_fail ("isSelectValue() && \"Wrong accessor\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 268, __extension__ __PRETTY_FUNCTION__
))
;
269 return cast<SelectInst>(Val);
270 }
271
272 /// Emit code at the specified insertion point to adjust the value defined
273 /// here to the specified type. This handles various coercion cases.
274 Value *MaterializeAdjustedValue(LoadInst *Load, Instruction *InsertPt,
275 GVNPass &gvn) const;
276};
277
278/// Represents an AvailableValue which can be rematerialized at the end of
279/// the associated BasicBlock.
280struct llvm::gvn::AvailableValueInBlock {
281 /// BB - The basic block in question.
282 BasicBlock *BB = nullptr;
283
284 /// AV - The actual available value
285 AvailableValue AV;
286
287 static AvailableValueInBlock get(BasicBlock *BB, AvailableValue &&AV) {
288 AvailableValueInBlock Res;
289 Res.BB = BB;
290 Res.AV = std::move(AV);
291 return Res;
292 }
293
294 static AvailableValueInBlock get(BasicBlock *BB, Value *V,
295 unsigned Offset = 0) {
296 return get(BB, AvailableValue::get(V, Offset));
297 }
298
299 static AvailableValueInBlock getUndef(BasicBlock *BB) {
300 return get(BB, AvailableValue::getUndef());
301 }
302
303 static AvailableValueInBlock getSelect(BasicBlock *BB, SelectInst *Sel,
304 Value *V1, Value *V2) {
305 return get(BB, AvailableValue::getSelect(Sel, V1, V2));
306 }
307
308 /// Emit code at the end of this block to adjust the value defined here to
309 /// the specified type. This handles various coercion cases.
310 Value *MaterializeAdjustedValue(LoadInst *Load, GVNPass &gvn) const {
311 return AV.MaterializeAdjustedValue(Load, BB->getTerminator(), gvn);
312 }
313};
314
315//===----------------------------------------------------------------------===//
316// ValueTable Internal Functions
317//===----------------------------------------------------------------------===//
318
319GVNPass::Expression GVNPass::ValueTable::createExpr(Instruction *I) {
320 Expression e;
321 e.type = I->getType();
322 e.opcode = I->getOpcode();
323 if (const GCRelocateInst *GCR = dyn_cast<GCRelocateInst>(I)) {
324 // gc.relocate is 'special' call: its second and third operands are
325 // not real values, but indices into statepoint's argument list.
326 // Use the refered to values for purposes of identity.
327 e.varargs.push_back(lookupOrAdd(GCR->getOperand(0)));
328 e.varargs.push_back(lookupOrAdd(GCR->getBasePtr()));
329 e.varargs.push_back(lookupOrAdd(GCR->getDerivedPtr()));
330 } else {
331 for (Use &Op : I->operands())
332 e.varargs.push_back(lookupOrAdd(Op));
333 }
334 if (I->isCommutative()) {
335 // Ensure that commutative instructions that only differ by a permutation
336 // of their operands get the same value number by sorting the operand value
337 // numbers. Since commutative operands are the 1st two operands it is more
338 // efficient to sort by hand rather than using, say, std::sort.
339 assert(I->getNumOperands() >= 2 && "Unsupported commutative instruction!")(static_cast <bool> (I->getNumOperands() >= 2 &&
"Unsupported commutative instruction!") ? void (0) : __assert_fail
("I->getNumOperands() >= 2 && \"Unsupported commutative instruction!\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 339, __extension__ __PRETTY_FUNCTION__
))
;
340 if (e.varargs[0] > e.varargs[1])
341 std::swap(e.varargs[0], e.varargs[1]);
342 e.commutative = true;
343 }
344
345 if (auto *C = dyn_cast<CmpInst>(I)) {
346 // Sort the operand value numbers so x<y and y>x get the same value number.
347 CmpInst::Predicate Predicate = C->getPredicate();
348 if (e.varargs[0] > e.varargs[1]) {
349 std::swap(e.varargs[0], e.varargs[1]);
350 Predicate = CmpInst::getSwappedPredicate(Predicate);
351 }
352 e.opcode = (C->getOpcode() << 8) | Predicate;
353 e.commutative = true;
354 } else if (auto *E = dyn_cast<InsertValueInst>(I)) {
355 e.varargs.append(E->idx_begin(), E->idx_end());
356 } else if (auto *SVI = dyn_cast<ShuffleVectorInst>(I)) {
357 ArrayRef<int> ShuffleMask = SVI->getShuffleMask();
358 e.varargs.append(ShuffleMask.begin(), ShuffleMask.end());
359 }
360
361 return e;
362}
363
364GVNPass::Expression GVNPass::ValueTable::createCmpExpr(
365 unsigned Opcode, CmpInst::Predicate Predicate, Value *LHS, Value *RHS) {
366 assert((Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) &&(static_cast <bool> ((Opcode == Instruction::ICmp || Opcode
== Instruction::FCmp) && "Not a comparison!") ? void
(0) : __assert_fail ("(Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) && \"Not a comparison!\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 367, __extension__ __PRETTY_FUNCTION__
))
367 "Not a comparison!")(static_cast <bool> ((Opcode == Instruction::ICmp || Opcode
== Instruction::FCmp) && "Not a comparison!") ? void
(0) : __assert_fail ("(Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) && \"Not a comparison!\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 367, __extension__ __PRETTY_FUNCTION__
))
;
368 Expression e;
369 e.type = CmpInst::makeCmpResultType(LHS->getType());
370 e.varargs.push_back(lookupOrAdd(LHS));
371 e.varargs.push_back(lookupOrAdd(RHS));
372
373 // Sort the operand value numbers so x<y and y>x get the same value number.
374 if (e.varargs[0] > e.varargs[1]) {
375 std::swap(e.varargs[0], e.varargs[1]);
376 Predicate = CmpInst::getSwappedPredicate(Predicate);
377 }
378 e.opcode = (Opcode << 8) | Predicate;
379 e.commutative = true;
380 return e;
381}
382
383GVNPass::Expression
384GVNPass::ValueTable::createExtractvalueExpr(ExtractValueInst *EI) {
385 assert(EI && "Not an ExtractValueInst?")(static_cast <bool> (EI && "Not an ExtractValueInst?"
) ? void (0) : __assert_fail ("EI && \"Not an ExtractValueInst?\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 385, __extension__ __PRETTY_FUNCTION__
))
;
386 Expression e;
387 e.type = EI->getType();
388 e.opcode = 0;
389
390 WithOverflowInst *WO = dyn_cast<WithOverflowInst>(EI->getAggregateOperand());
391 if (WO != nullptr && EI->getNumIndices() == 1 && *EI->idx_begin() == 0) {
392 // EI is an extract from one of our with.overflow intrinsics. Synthesize
393 // a semantically equivalent expression instead of an extract value
394 // expression.
395 e.opcode = WO->getBinaryOp();
396 e.varargs.push_back(lookupOrAdd(WO->getLHS()));
397 e.varargs.push_back(lookupOrAdd(WO->getRHS()));
398 return e;
399 }
400
401 // Not a recognised intrinsic. Fall back to producing an extract value
402 // expression.
403 e.opcode = EI->getOpcode();
404 for (Use &Op : EI->operands())
405 e.varargs.push_back(lookupOrAdd(Op));
406
407 append_range(e.varargs, EI->indices());
408
409 return e;
410}
411
412GVNPass::Expression GVNPass::ValueTable::createGEPExpr(GetElementPtrInst *GEP) {
413 Expression E;
414 Type *PtrTy = GEP->getType()->getScalarType();
415 const DataLayout &DL = GEP->getModule()->getDataLayout();
416 unsigned BitWidth = DL.getIndexTypeSizeInBits(PtrTy);
417 MapVector<Value *, APInt> VariableOffsets;
418 APInt ConstantOffset(BitWidth, 0);
419 if (PtrTy->isOpaquePointerTy() &&
420 GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset)) {
421 // For opaque pointers, convert into offset representation, to recognize
422 // equivalent address calculations that use different type encoding.
423 LLVMContext &Context = GEP->getContext();
424 E.opcode = GEP->getOpcode();
425 E.type = nullptr;
426 E.varargs.push_back(lookupOrAdd(GEP->getPointerOperand()));
427 for (const auto &Pair : VariableOffsets) {
428 E.varargs.push_back(lookupOrAdd(Pair.first));
429 E.varargs.push_back(lookupOrAdd(ConstantInt::get(Context, Pair.second)));
430 }
431 if (!ConstantOffset.isZero())
432 E.varargs.push_back(
433 lookupOrAdd(ConstantInt::get(Context, ConstantOffset)));
434 } else {
435 // If converting to offset representation fails (for typed pointers and
436 // scalable vectors), fall back to type-based implementation:
437 E.opcode = GEP->getOpcode();
438 E.type = GEP->getSourceElementType();
439 for (Use &Op : GEP->operands())
440 E.varargs.push_back(lookupOrAdd(Op));
441 }
442 return E;
443}
444
445//===----------------------------------------------------------------------===//
446// ValueTable External Functions
447//===----------------------------------------------------------------------===//
448
449GVNPass::ValueTable::ValueTable() = default;
450GVNPass::ValueTable::ValueTable(const ValueTable &) = default;
451GVNPass::ValueTable::ValueTable(ValueTable &&) = default;
452GVNPass::ValueTable::~ValueTable() = default;
453GVNPass::ValueTable &
454GVNPass::ValueTable::operator=(const GVNPass::ValueTable &Arg) = default;
455
456/// add - Insert a value into the table with a specified value number.
457void GVNPass::ValueTable::add(Value *V, uint32_t num) {
458 valueNumbering.insert(std::make_pair(V, num));
459 if (PHINode *PN = dyn_cast<PHINode>(V))
460 NumberingPhi[num] = PN;
461}
462
463uint32_t GVNPass::ValueTable::lookupOrAddCall(CallInst *C) {
464 if (AA->doesNotAccessMemory(C) &&
465 // FIXME: Currently the calls which may access the thread id may
466 // be considered as not accessing the memory. But this is
467 // problematic for coroutines, since coroutines may resume in a
468 // different thread. So we disable the optimization here for the
469 // correctness. However, it may block many other correct
470 // optimizations. Revert this one when we detect the memory
471 // accessing kind more precisely.
472 !C->getFunction()->isPresplitCoroutine()) {
473 Expression exp = createExpr(C);
474 uint32_t e = assignExpNewValueNum(exp).first;
475 valueNumbering[C] = e;
476 return e;
477 } else if (MD && AA->onlyReadsMemory(C) &&
478 // FIXME: Currently the calls which may access the thread id may
479 // be considered as not accessing the memory. But this is
480 // problematic for coroutines, since coroutines may resume in a
481 // different thread. So we disable the optimization here for the
482 // correctness. However, it may block many other correct
483 // optimizations. Revert this one when we detect the memory
484 // accessing kind more precisely.
485 !C->getFunction()->isPresplitCoroutine()) {
486 Expression exp = createExpr(C);
487 auto ValNum = assignExpNewValueNum(exp);
488 if (ValNum.second) {
489 valueNumbering[C] = ValNum.first;
490 return ValNum.first;
491 }
492
493 MemDepResult local_dep = MD->getDependency(C);
494
495 if (!local_dep.isDef() && !local_dep.isNonLocal()) {
496 valueNumbering[C] = nextValueNumber;
497 return nextValueNumber++;
498 }
499
500 if (local_dep.isDef()) {
501 // For masked load/store intrinsics, the local_dep may actually be
502 // a normal load or store instruction.
503 CallInst *local_cdep = dyn_cast<CallInst>(local_dep.getInst());
504
505 if (!local_cdep || local_cdep->arg_size() != C->arg_size()) {
506 valueNumbering[C] = nextValueNumber;
507 return nextValueNumber++;
508 }
509
510 for (unsigned i = 0, e = C->arg_size(); i < e; ++i) {
511 uint32_t c_vn = lookupOrAdd(C->getArgOperand(i));
512 uint32_t cd_vn = lookupOrAdd(local_cdep->getArgOperand(i));
513 if (c_vn != cd_vn) {
514 valueNumbering[C] = nextValueNumber;
515 return nextValueNumber++;
516 }
517 }
518
519 uint32_t v = lookupOrAdd(local_cdep);
520 valueNumbering[C] = v;
521 return v;
522 }
523
524 // Non-local case.
525 const MemoryDependenceResults::NonLocalDepInfo &deps =
526 MD->getNonLocalCallDependency(C);
527 // FIXME: Move the checking logic to MemDep!
528 CallInst* cdep = nullptr;
529
530 // Check to see if we have a single dominating call instruction that is
531 // identical to C.
532 for (const NonLocalDepEntry &I : deps) {
533 if (I.getResult().isNonLocal())
534 continue;
535
536 // We don't handle non-definitions. If we already have a call, reject
537 // instruction dependencies.
538 if (!I.getResult().isDef() || cdep != nullptr) {
539 cdep = nullptr;
540 break;
541 }
542
543 CallInst *NonLocalDepCall = dyn_cast<CallInst>(I.getResult().getInst());
544 // FIXME: All duplicated with non-local case.
545 if (NonLocalDepCall && DT->properlyDominates(I.getBB(), C->getParent())) {
546 cdep = NonLocalDepCall;
547 continue;
548 }
549
550 cdep = nullptr;
551 break;
552 }
553
554 if (!cdep) {
555 valueNumbering[C] = nextValueNumber;
556 return nextValueNumber++;
557 }
558
559 if (cdep->arg_size() != C->arg_size()) {
560 valueNumbering[C] = nextValueNumber;
561 return nextValueNumber++;
562 }
563 for (unsigned i = 0, e = C->arg_size(); i < e; ++i) {
564 uint32_t c_vn = lookupOrAdd(C->getArgOperand(i));
565 uint32_t cd_vn = lookupOrAdd(cdep->getArgOperand(i));
566 if (c_vn != cd_vn) {
567 valueNumbering[C] = nextValueNumber;
568 return nextValueNumber++;
569 }
570 }
571
572 uint32_t v = lookupOrAdd(cdep);
573 valueNumbering[C] = v;
574 return v;
575 } else {
576 valueNumbering[C] = nextValueNumber;
577 return nextValueNumber++;
578 }
579}
580
581/// Returns true if a value number exists for the specified value.
582bool GVNPass::ValueTable::exists(Value *V) const {
583 return valueNumbering.count(V) != 0;
584}
585
586/// lookup_or_add - Returns the value number for the specified value, assigning
587/// it a new number if it did not have one before.
588uint32_t GVNPass::ValueTable::lookupOrAdd(Value *V) {
589 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
590 if (VI != valueNumbering.end())
591 return VI->second;
592
593 auto *I = dyn_cast<Instruction>(V);
594 if (!I) {
595 valueNumbering[V] = nextValueNumber;
596 return nextValueNumber++;
597 }
598
599 Expression exp;
600 switch (I->getOpcode()) {
601 case Instruction::Call:
602 return lookupOrAddCall(cast<CallInst>(I));
603 case Instruction::FNeg:
604 case Instruction::Add:
605 case Instruction::FAdd:
606 case Instruction::Sub:
607 case Instruction::FSub:
608 case Instruction::Mul:
609 case Instruction::FMul:
610 case Instruction::UDiv:
611 case Instruction::SDiv:
612 case Instruction::FDiv:
613 case Instruction::URem:
614 case Instruction::SRem:
615 case Instruction::FRem:
616 case Instruction::Shl:
617 case Instruction::LShr:
618 case Instruction::AShr:
619 case Instruction::And:
620 case Instruction::Or:
621 case Instruction::Xor:
622 case Instruction::ICmp:
623 case Instruction::FCmp:
624 case Instruction::Trunc:
625 case Instruction::ZExt:
626 case Instruction::SExt:
627 case Instruction::FPToUI:
628 case Instruction::FPToSI:
629 case Instruction::UIToFP:
630 case Instruction::SIToFP:
631 case Instruction::FPTrunc:
632 case Instruction::FPExt:
633 case Instruction::PtrToInt:
634 case Instruction::IntToPtr:
635 case Instruction::AddrSpaceCast:
636 case Instruction::BitCast:
637 case Instruction::Select:
638 case Instruction::Freeze:
639 case Instruction::ExtractElement:
640 case Instruction::InsertElement:
641 case Instruction::ShuffleVector:
642 case Instruction::InsertValue:
643 exp = createExpr(I);
644 break;
645 case Instruction::GetElementPtr:
646 exp = createGEPExpr(cast<GetElementPtrInst>(I));
647 break;
648 case Instruction::ExtractValue:
649 exp = createExtractvalueExpr(cast<ExtractValueInst>(I));
650 break;
651 case Instruction::PHI:
652 valueNumbering[V] = nextValueNumber;
653 NumberingPhi[nextValueNumber] = cast<PHINode>(V);
654 return nextValueNumber++;
655 default:
656 valueNumbering[V] = nextValueNumber;
657 return nextValueNumber++;
658 }
659
660 uint32_t e = assignExpNewValueNum(exp).first;
661 valueNumbering[V] = e;
662 return e;
663}
664
665/// Returns the value number of the specified value. Fails if
666/// the value has not yet been numbered.
667uint32_t GVNPass::ValueTable::lookup(Value *V, bool Verify) const {
668 DenseMap<Value*, uint32_t>::const_iterator VI = valueNumbering.find(V);
669 if (Verify) {
670 assert(VI != valueNumbering.end() && "Value not numbered?")(static_cast <bool> (VI != valueNumbering.end() &&
"Value not numbered?") ? void (0) : __assert_fail ("VI != valueNumbering.end() && \"Value not numbered?\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 670, __extension__ __PRETTY_FUNCTION__
))
;
671 return VI->second;
672 }
673 return (VI != valueNumbering.end()) ? VI->second : 0;
674}
675
676/// Returns the value number of the given comparison,
677/// assigning it a new number if it did not have one before. Useful when
678/// we deduced the result of a comparison, but don't immediately have an
679/// instruction realizing that comparison to hand.
680uint32_t GVNPass::ValueTable::lookupOrAddCmp(unsigned Opcode,
681 CmpInst::Predicate Predicate,
682 Value *LHS, Value *RHS) {
683 Expression exp = createCmpExpr(Opcode, Predicate, LHS, RHS);
684 return assignExpNewValueNum(exp).first;
685}
686
687/// Remove all entries from the ValueTable.
688void GVNPass::ValueTable::clear() {
689 valueNumbering.clear();
690 expressionNumbering.clear();
691 NumberingPhi.clear();
692 PhiTranslateTable.clear();
693 nextValueNumber = 1;
694 Expressions.clear();
695 ExprIdx.clear();
696 nextExprNumber = 0;
697}
698
699/// Remove a value from the value numbering.
700void GVNPass::ValueTable::erase(Value *V) {
701 uint32_t Num = valueNumbering.lookup(V);
702 valueNumbering.erase(V);
703 // If V is PHINode, V <--> value number is an one-to-one mapping.
704 if (isa<PHINode>(V))
705 NumberingPhi.erase(Num);
706}
707
708/// verifyRemoved - Verify that the value is removed from all internal data
709/// structures.
710void GVNPass::ValueTable::verifyRemoved(const Value *V) const {
711 for (DenseMap<Value*, uint32_t>::const_iterator
712 I = valueNumbering.begin(), E = valueNumbering.end(); I != E; ++I) {
713 assert(I->first != V && "Inst still occurs in value numbering map!")(static_cast <bool> (I->first != V && "Inst still occurs in value numbering map!"
) ? void (0) : __assert_fail ("I->first != V && \"Inst still occurs in value numbering map!\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 713, __extension__ __PRETTY_FUNCTION__
))
;
714 }
715}
716
717//===----------------------------------------------------------------------===//
718// GVN Pass
719//===----------------------------------------------------------------------===//
720
721bool GVNPass::isPREEnabled() const {
722 return Options.AllowPRE.value_or(GVNEnablePRE);
723}
724
725bool GVNPass::isLoadPREEnabled() const {
726 return Options.AllowLoadPRE.value_or(GVNEnableLoadPRE);
727}
728
729bool GVNPass::isLoadInLoopPREEnabled() const {
730 return Options.AllowLoadInLoopPRE.value_or(GVNEnableLoadInLoopPRE);
731}
732
733bool GVNPass::isLoadPRESplitBackedgeEnabled() const {
734 return Options.AllowLoadPRESplitBackedge.value_or(
735 GVNEnableSplitBackedgeInLoadPRE);
736}
737
738bool GVNPass::isMemDepEnabled() const {
739 return Options.AllowMemDep.value_or(GVNEnableMemDep);
740}
741
742PreservedAnalyses GVNPass::run(Function &F, FunctionAnalysisManager &AM) {
743 // FIXME: The order of evaluation of these 'getResult' calls is very
744 // significant! Re-ordering these variables will cause GVN when run alone to
745 // be less effective! We should fix memdep and basic-aa to not exhibit this
746 // behavior, but until then don't change the order here.
747 auto &AC = AM.getResult<AssumptionAnalysis>(F);
748 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
749 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
750 auto &AA = AM.getResult<AAManager>(F);
751 auto *MemDep =
752 isMemDepEnabled() ? &AM.getResult<MemoryDependenceAnalysis>(F) : nullptr;
753 auto *LI = AM.getCachedResult<LoopAnalysis>(F);
754 auto *MSSA = AM.getCachedResult<MemorySSAAnalysis>(F);
755 auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
756 bool Changed = runImpl(F, AC, DT, TLI, AA, MemDep, LI, &ORE,
757 MSSA ? &MSSA->getMSSA() : nullptr);
758 if (!Changed)
759 return PreservedAnalyses::all();
760 PreservedAnalyses PA;
761 PA.preserve<DominatorTreeAnalysis>();
762 PA.preserve<TargetLibraryAnalysis>();
763 if (MSSA)
764 PA.preserve<MemorySSAAnalysis>();
765 if (LI)
766 PA.preserve<LoopAnalysis>();
767 return PA;
768}
769
770void GVNPass::printPipeline(
771 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
772 static_cast<PassInfoMixin<GVNPass> *>(this)->printPipeline(
773 OS, MapClassName2PassName);
774
775 OS << '<';
776 if (Options.AllowPRE != std::nullopt)
777 OS << (*Options.AllowPRE ? "" : "no-") << "pre;";
778 if (Options.AllowLoadPRE != std::nullopt)
779 OS << (*Options.AllowLoadPRE ? "" : "no-") << "load-pre;";
780 if (Options.AllowLoadPRESplitBackedge != std::nullopt)
781 OS << (*Options.AllowLoadPRESplitBackedge ? "" : "no-")
782 << "split-backedge-load-pre;";
783 if (Options.AllowMemDep != std::nullopt)
784 OS << (*Options.AllowMemDep ? "" : "no-") << "memdep";
785 OS << '>';
786}
787
788#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
789LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void GVNPass::dump(DenseMap<uint32_t, Value *> &d) const {
790 errs() << "{\n";
791 for (auto &I : d) {
792 errs() << I.first << "\n";
793 I.second->dump();
794 }
795 errs() << "}\n";
796}
797#endif
798
799enum class AvailabilityState : char {
800 /// We know the block *is not* fully available. This is a fixpoint.
801 Unavailable = 0,
802 /// We know the block *is* fully available. This is a fixpoint.
803 Available = 1,
804 /// We do not know whether the block is fully available or not,
805 /// but we are currently speculating that it will be.
806 /// If it would have turned out that the block was, in fact, not fully
807 /// available, this would have been cleaned up into an Unavailable.
808 SpeculativelyAvailable = 2,
809};
810
811/// Return true if we can prove that the value
812/// we're analyzing is fully available in the specified block. As we go, keep
813/// track of which blocks we know are fully alive in FullyAvailableBlocks. This
814/// map is actually a tri-state map with the following values:
815/// 0) we know the block *is not* fully available.
816/// 1) we know the block *is* fully available.
817/// 2) we do not know whether the block is fully available or not, but we are
818/// currently speculating that it will be.
819static bool IsValueFullyAvailableInBlock(
820 BasicBlock *BB,
821 DenseMap<BasicBlock *, AvailabilityState> &FullyAvailableBlocks) {
822 SmallVector<BasicBlock *, 32> Worklist;
823 std::optional<BasicBlock *> UnavailableBB;
824
825 // The number of times we didn't find an entry for a block in a map and
826 // optimistically inserted an entry marking block as speculatively available.
827 unsigned NumNewNewSpeculativelyAvailableBBs = 0;
828
829#ifndef NDEBUG
830 SmallSet<BasicBlock *, 32> NewSpeculativelyAvailableBBs;
831 SmallVector<BasicBlock *, 32> AvailableBBs;
832#endif
833
834 Worklist.emplace_back(BB);
835 while (!Worklist.empty()) {
836 BasicBlock *CurrBB = Worklist.pop_back_val(); // LoadFO - depth-first!
837 // Optimistically assume that the block is Speculatively Available and check
838 // to see if we already know about this block in one lookup.
839 std::pair<DenseMap<BasicBlock *, AvailabilityState>::iterator, bool> IV =
840 FullyAvailableBlocks.try_emplace(
841 CurrBB, AvailabilityState::SpeculativelyAvailable);
842 AvailabilityState &State = IV.first->second;
843
844 // Did the entry already exist for this block?
845 if (!IV.second) {
846 if (State == AvailabilityState::Unavailable) {
847 UnavailableBB = CurrBB;
848 break; // Backpropagate unavailability info.
849 }
850
851#ifndef NDEBUG
852 AvailableBBs.emplace_back(CurrBB);
853#endif
854 continue; // Don't recurse further, but continue processing worklist.
855 }
856
857 // No entry found for block.
858 ++NumNewNewSpeculativelyAvailableBBs;
859 bool OutOfBudget = NumNewNewSpeculativelyAvailableBBs > MaxBBSpeculations;
860
861 // If we have exhausted our budget, mark this block as unavailable.
862 // Also, if this block has no predecessors, the value isn't live-in here.
863 if (OutOfBudget || pred_empty(CurrBB)) {
864 MaxBBSpeculationCutoffReachedTimes += (int)OutOfBudget;
865 State = AvailabilityState::Unavailable;
866 UnavailableBB = CurrBB;
867 break; // Backpropagate unavailability info.
868 }
869
870 // Tentatively consider this block as speculatively available.
871#ifndef NDEBUG
872 NewSpeculativelyAvailableBBs.insert(CurrBB);
873#endif
874 // And further recurse into block's predecessors, in depth-first order!
875 Worklist.append(pred_begin(CurrBB), pred_end(CurrBB));
876 }
877
878#if LLVM_ENABLE_STATS1
879 IsValueFullyAvailableInBlockNumSpeculationsMax.updateMax(
880 NumNewNewSpeculativelyAvailableBBs);
881#endif
882
883 // If the block isn't marked as fixpoint yet
884 // (the Unavailable and Available states are fixpoints)
885 auto MarkAsFixpointAndEnqueueSuccessors =
886 [&](BasicBlock *BB, AvailabilityState FixpointState) {
887 auto It = FullyAvailableBlocks.find(BB);
888 if (It == FullyAvailableBlocks.end())
889 return; // Never queried this block, leave as-is.
890 switch (AvailabilityState &State = It->second) {
891 case AvailabilityState::Unavailable:
892 case AvailabilityState::Available:
893 return; // Don't backpropagate further, continue processing worklist.
894 case AvailabilityState::SpeculativelyAvailable: // Fix it!
895 State = FixpointState;
896#ifndef NDEBUG
897 assert(NewSpeculativelyAvailableBBs.erase(BB) &&(static_cast <bool> (NewSpeculativelyAvailableBBs.erase
(BB) && "Found a speculatively available successor leftover?"
) ? void (0) : __assert_fail ("NewSpeculativelyAvailableBBs.erase(BB) && \"Found a speculatively available successor leftover?\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 898, __extension__ __PRETTY_FUNCTION__
))
898 "Found a speculatively available successor leftover?")(static_cast <bool> (NewSpeculativelyAvailableBBs.erase
(BB) && "Found a speculatively available successor leftover?"
) ? void (0) : __assert_fail ("NewSpeculativelyAvailableBBs.erase(BB) && \"Found a speculatively available successor leftover?\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 898, __extension__ __PRETTY_FUNCTION__
))
;
899#endif
900 // Queue successors for further processing.
901 Worklist.append(succ_begin(BB), succ_end(BB));
902 return;
903 }
904 };
905
906 if (UnavailableBB) {
907 // Okay, we have encountered an unavailable block.
908 // Mark speculatively available blocks reachable from UnavailableBB as
909 // unavailable as well. Paths are terminated when they reach blocks not in
910 // FullyAvailableBlocks or they are not marked as speculatively available.
911 Worklist.clear();
912 Worklist.append(succ_begin(*UnavailableBB), succ_end(*UnavailableBB));
913 while (!Worklist.empty())
914 MarkAsFixpointAndEnqueueSuccessors(Worklist.pop_back_val(),
915 AvailabilityState::Unavailable);
916 }
917
918#ifndef NDEBUG
919 Worklist.clear();
920 for (BasicBlock *AvailableBB : AvailableBBs)
921 Worklist.append(succ_begin(AvailableBB), succ_end(AvailableBB));
922 while (!Worklist.empty())
923 MarkAsFixpointAndEnqueueSuccessors(Worklist.pop_back_val(),
924 AvailabilityState::Available);
925
926 assert(NewSpeculativelyAvailableBBs.empty() &&(static_cast <bool> (NewSpeculativelyAvailableBBs.empty
() && "Must have fixed all the new speculatively available blocks."
) ? void (0) : __assert_fail ("NewSpeculativelyAvailableBBs.empty() && \"Must have fixed all the new speculatively available blocks.\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 927, __extension__ __PRETTY_FUNCTION__
))
927 "Must have fixed all the new speculatively available blocks.")(static_cast <bool> (NewSpeculativelyAvailableBBs.empty
() && "Must have fixed all the new speculatively available blocks."
) ? void (0) : __assert_fail ("NewSpeculativelyAvailableBBs.empty() && \"Must have fixed all the new speculatively available blocks.\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 927, __extension__ __PRETTY_FUNCTION__
))
;
928#endif
929
930 return !UnavailableBB;
931}
932
933/// Given a set of loads specified by ValuesPerBlock,
934/// construct SSA form, allowing us to eliminate Load. This returns the value
935/// that should be used at Load's definition site.
936static Value *
937ConstructSSAForLoadSet(LoadInst *Load,
938 SmallVectorImpl<AvailableValueInBlock> &ValuesPerBlock,
939 GVNPass &gvn) {
940 // Check for the fully redundant, dominating load case. In this case, we can
941 // just use the dominating value directly.
942 if (ValuesPerBlock.size() == 1 &&
943 gvn.getDominatorTree().properlyDominates(ValuesPerBlock[0].BB,
944 Load->getParent())) {
945 assert(!ValuesPerBlock[0].AV.isUndefValue() &&(static_cast <bool> (!ValuesPerBlock[0].AV.isUndefValue
() && "Dead BB dominate this block") ? void (0) : __assert_fail
("!ValuesPerBlock[0].AV.isUndefValue() && \"Dead BB dominate this block\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 946, __extension__ __PRETTY_FUNCTION__
))
946 "Dead BB dominate this block")(static_cast <bool> (!ValuesPerBlock[0].AV.isUndefValue
() && "Dead BB dominate this block") ? void (0) : __assert_fail
("!ValuesPerBlock[0].AV.isUndefValue() && \"Dead BB dominate this block\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 946, __extension__ __PRETTY_FUNCTION__
))
;
947 return ValuesPerBlock[0].MaterializeAdjustedValue(Load, gvn);
948 }
949
950 // Otherwise, we have to construct SSA form.
951 SmallVector<PHINode*, 8> NewPHIs;
952 SSAUpdater SSAUpdate(&NewPHIs);
953 SSAUpdate.Initialize(Load->getType(), Load->getName());
954
955 for (const AvailableValueInBlock &AV : ValuesPerBlock) {
956 BasicBlock *BB = AV.BB;
957
958 if (AV.AV.isUndefValue())
959 continue;
960
961 if (SSAUpdate.HasValueForBlock(BB))
962 continue;
963
964 // If the value is the load that we will be eliminating, and the block it's
965 // available in is the block that the load is in, then don't add it as
966 // SSAUpdater will resolve the value to the relevant phi which may let it
967 // avoid phi construction entirely if there's actually only one value.
968 if (BB == Load->getParent() &&
969 ((AV.AV.isSimpleValue() && AV.AV.getSimpleValue() == Load) ||
970 (AV.AV.isCoercedLoadValue() && AV.AV.getCoercedLoadValue() == Load)))
971 continue;
972
973 SSAUpdate.AddAvailableValue(BB, AV.MaterializeAdjustedValue(Load, gvn));
974 }
975
976 // Perform PHI construction.
977 return SSAUpdate.GetValueInMiddleOfBlock(Load->getParent());
978}
979
980Value *AvailableValue::MaterializeAdjustedValue(LoadInst *Load,
981 Instruction *InsertPt,
982 GVNPass &gvn) const {
983 Value *Res;
984 Type *LoadTy = Load->getType();
985 const DataLayout &DL = Load->getModule()->getDataLayout();
986 if (isSimpleValue()) {
987 Res = getSimpleValue();
988 if (Res->getType() != LoadTy) {
989 Res = getValueForLoad(Res, Offset, LoadTy, InsertPt, DL);
990
991 LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL VAL:\nOffset: " << Offsetdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN COERCED NONLOCAL VAL:\nOffset: "
<< Offset << " " << *getSimpleValue() <<
'\n' << *Res << '\n' << "\n\n\n"; } } while
(false)
992 << " " << *getSimpleValue() << '\n'do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN COERCED NONLOCAL VAL:\nOffset: "
<< Offset << " " << *getSimpleValue() <<
'\n' << *Res << '\n' << "\n\n\n"; } } while
(false)
993 << *Res << '\n'do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN COERCED NONLOCAL VAL:\nOffset: "
<< Offset << " " << *getSimpleValue() <<
'\n' << *Res << '\n' << "\n\n\n"; } } while
(false)
994 << "\n\n\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN COERCED NONLOCAL VAL:\nOffset: "
<< Offset << " " << *getSimpleValue() <<
'\n' << *Res << '\n' << "\n\n\n"; } } while
(false)
;
995 }
996 } else if (isCoercedLoadValue()) {
997 LoadInst *CoercedLoad = getCoercedLoadValue();
998 if (CoercedLoad->getType() == LoadTy && Offset == 0) {
999 Res = CoercedLoad;
1000 combineMetadataForCSE(CoercedLoad, Load, false);
1001 } else {
1002 Res = getValueForLoad(CoercedLoad, Offset, LoadTy, InsertPt, DL);
1003 // We are adding a new user for this load, for which the original
1004 // metadata may not hold. Additionally, the new load may have a different
1005 // size and type, so their metadata cannot be combined in any
1006 // straightforward way.
1007 // Drop all metadata that is not known to cause immediate UB on violation,
1008 // unless the load has !noundef, in which case all metadata violations
1009 // will be promoted to UB.
1010 // TODO: We can combine noalias/alias.scope metadata here, because it is
1011 // independent of the load type.
1012 if (!CoercedLoad->hasMetadata(LLVMContext::MD_noundef))
1013 CoercedLoad->dropUnknownNonDebugMetadata(
1014 {LLVMContext::MD_dereferenceable,
1015 LLVMContext::MD_dereferenceable_or_null,
1016 LLVMContext::MD_invariant_load, LLVMContext::MD_invariant_group});
1017 LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL LOAD:\nOffset: " << Offsetdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN COERCED NONLOCAL LOAD:\nOffset: "
<< Offset << " " << *getCoercedLoadValue(
) << '\n' << *Res << '\n' << "\n\n\n"
; } } while (false)
1018 << " " << *getCoercedLoadValue() << '\n'do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN COERCED NONLOCAL LOAD:\nOffset: "
<< Offset << " " << *getCoercedLoadValue(
) << '\n' << *Res << '\n' << "\n\n\n"
; } } while (false)
1019 << *Res << '\n'do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN COERCED NONLOCAL LOAD:\nOffset: "
<< Offset << " " << *getCoercedLoadValue(
) << '\n' << *Res << '\n' << "\n\n\n"
; } } while (false)
1020 << "\n\n\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN COERCED NONLOCAL LOAD:\nOffset: "
<< Offset << " " << *getCoercedLoadValue(
) << '\n' << *Res << '\n' << "\n\n\n"
; } } while (false)
;
1021 }
1022 } else if (isMemIntrinValue()) {
1023 Res = getMemInstValueForLoad(getMemIntrinValue(), Offset, LoadTy,
1024 InsertPt, DL);
1025 LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL MEM INTRIN:\nOffset: " << Offsetdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN COERCED NONLOCAL MEM INTRIN:\nOffset: "
<< Offset << " " << *getMemIntrinValue() <<
'\n' << *Res << '\n' << "\n\n\n"; } } while
(false)
1026 << " " << *getMemIntrinValue() << '\n'do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN COERCED NONLOCAL MEM INTRIN:\nOffset: "
<< Offset << " " << *getMemIntrinValue() <<
'\n' << *Res << '\n' << "\n\n\n"; } } while
(false)
1027 << *Res << '\n'do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN COERCED NONLOCAL MEM INTRIN:\nOffset: "
<< Offset << " " << *getMemIntrinValue() <<
'\n' << *Res << '\n' << "\n\n\n"; } } while
(false)
1028 << "\n\n\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN COERCED NONLOCAL MEM INTRIN:\nOffset: "
<< Offset << " " << *getMemIntrinValue() <<
'\n' << *Res << '\n' << "\n\n\n"; } } while
(false)
;
1029 } else if (isSelectValue()) {
1030 // Introduce a new value select for a load from an eligible pointer select.
1031 SelectInst *Sel = getSelectValue();
1032 assert(V1 && V2 && "both value operands of the select must be present")(static_cast <bool> (V1 && V2 && "both value operands of the select must be present"
) ? void (0) : __assert_fail ("V1 && V2 && \"both value operands of the select must be present\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 1032, __extension__ __PRETTY_FUNCTION__
))
;
1033 Res = SelectInst::Create(Sel->getCondition(), V1, V2, "", Sel);
1034 } else {
1035 llvm_unreachable("Should not materialize value from dead block")::llvm::llvm_unreachable_internal("Should not materialize value from dead block"
, "llvm/lib/Transforms/Scalar/GVN.cpp", 1035)
;
1036 }
1037 assert(Res && "failed to materialize?")(static_cast <bool> (Res && "failed to materialize?"
) ? void (0) : __assert_fail ("Res && \"failed to materialize?\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 1037, __extension__ __PRETTY_FUNCTION__
))
;
1038 return Res;
1039}
1040
1041static bool isLifetimeStart(const Instruction *Inst) {
1042 if (const IntrinsicInst* II = dyn_cast<IntrinsicInst>(Inst))
1043 return II->getIntrinsicID() == Intrinsic::lifetime_start;
1044 return false;
1045}
1046
1047/// Assuming To can be reached from both From and Between, does Between lie on
1048/// every path from From to To?
1049static bool liesBetween(const Instruction *From, Instruction *Between,
1050 const Instruction *To, DominatorTree *DT) {
1051 if (From->getParent() == Between->getParent())
1052 return DT->dominates(From, Between);
1053 SmallSet<BasicBlock *, 1> Exclusion;
1054 Exclusion.insert(Between->getParent());
1055 return !isPotentiallyReachable(From, To, &Exclusion, DT);
1056}
1057
1058/// Try to locate the three instruction involved in a missed
1059/// load-elimination case that is due to an intervening store.
1060static void reportMayClobberedLoad(LoadInst *Load, MemDepResult DepInfo,
1061 DominatorTree *DT,
1062 OptimizationRemarkEmitter *ORE) {
1063 using namespace ore;
1064
1065 Instruction *OtherAccess = nullptr;
1066
1067 OptimizationRemarkMissed R(DEBUG_TYPE"gvn", "LoadClobbered", Load);
1068 R << "load of type " << NV("Type", Load->getType()) << " not eliminated"
1069 << setExtraArgs();
1070
1071 for (auto *U : Load->getPointerOperand()->users()) {
1072 if (U != Load && (isa<LoadInst>(U) || isa<StoreInst>(U))) {
1073 auto *I = cast<Instruction>(U);
1074 if (I->getFunction() == Load->getFunction() && DT->dominates(I, Load)) {
1075 // Use the most immediately dominating value
1076 if (OtherAccess) {
1077 if (DT->dominates(OtherAccess, I))
1078 OtherAccess = I;
1079 else
1080 assert(U == OtherAccess || DT->dominates(I, OtherAccess))(static_cast <bool> (U == OtherAccess || DT->dominates
(I, OtherAccess)) ? void (0) : __assert_fail ("U == OtherAccess || DT->dominates(I, OtherAccess)"
, "llvm/lib/Transforms/Scalar/GVN.cpp", 1080, __extension__ __PRETTY_FUNCTION__
))
;
1081 } else
1082 OtherAccess = I;
1083 }
1084 }
1085 }
1086
1087 if (!OtherAccess) {
1088 // There is no dominating use, check if we can find a closest non-dominating
1089 // use that lies between any other potentially available use and Load.
1090 for (auto *U : Load->getPointerOperand()->users()) {
1091 if (U != Load && (isa<LoadInst>(U) || isa<StoreInst>(U))) {
1092 auto *I = cast<Instruction>(U);
1093 if (I->getFunction() == Load->getFunction() &&
1094 isPotentiallyReachable(I, Load, nullptr, DT)) {
1095 if (OtherAccess) {
1096 if (liesBetween(OtherAccess, I, Load, DT)) {
1097 OtherAccess = I;
1098 } else if (!liesBetween(I, OtherAccess, Load, DT)) {
1099 // These uses are both partially available at Load were it not for
1100 // the clobber, but neither lies strictly after the other.
1101 OtherAccess = nullptr;
1102 break;
1103 } // else: keep current OtherAccess since it lies between U and Load
1104 } else {
1105 OtherAccess = I;
1106 }
1107 }
1108 }
1109 }
1110 }
1111
1112 if (OtherAccess)
1113 R << " in favor of " << NV("OtherAccess", OtherAccess);
1114
1115 R << " because it is clobbered by " << NV("ClobberedBy", DepInfo.getInst());
1116
1117 ORE->emit(R);
1118}
1119
1120// Find non-clobbered value for Loc memory location in extended basic block
1121// (chain of basic blocks with single predecessors) starting From instruction.
1122static Value *findDominatingValue(const MemoryLocation &Loc, Type *LoadTy,
1123 Instruction *From, AAResults *AA) {
1124 uint32_t NumVisitedInsts = 0;
1125 BasicBlock *FromBB = From->getParent();
1126 BatchAAResults BatchAA(*AA);
1127 for (BasicBlock *BB = FromBB; BB; BB = BB->getSinglePredecessor())
1128 for (auto I = BB == FromBB ? From->getReverseIterator() : BB->rbegin(),
1129 E = BB->rend();
1130 I != E; ++I) {
1131 // Stop the search if limit is reached.
1132 if (++NumVisitedInsts > MaxNumVisitedInsts)
1133 return nullptr;
1134 Instruction *Inst = &*I;
1135 if (isModSet(BatchAA.getModRefInfo(Inst, Loc)))
1136 return nullptr;
1137 if (auto *LI = dyn_cast<LoadInst>(Inst))
1138 if (LI->getPointerOperand() == Loc.Ptr && LI->getType() == LoadTy)
1139 return LI;
1140 }
1141 return nullptr;
1142}
1143
1144std::optional<AvailableValue>
1145GVNPass::AnalyzeLoadAvailability(LoadInst *Load, MemDepResult DepInfo,
1146 Value *Address) {
1147 assert(Load->isUnordered() && "rules below are incorrect for ordered access")(static_cast <bool> (Load->isUnordered() && "rules below are incorrect for ordered access"
) ? void (0) : __assert_fail ("Load->isUnordered() && \"rules below are incorrect for ordered access\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 1147, __extension__ __PRETTY_FUNCTION__
))
;
1148 assert(DepInfo.isLocal() && "expected a local dependence")(static_cast <bool> (DepInfo.isLocal() && "expected a local dependence"
) ? void (0) : __assert_fail ("DepInfo.isLocal() && \"expected a local dependence\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 1148, __extension__ __PRETTY_FUNCTION__
))
;
1149
1150 Instruction *DepInst = DepInfo.getInst();
1151
1152 const DataLayout &DL = Load->getModule()->getDataLayout();
1153 if (DepInfo.isClobber()) {
1154 // If the dependence is to a store that writes to a superset of the bits
1155 // read by the load, we can extract the bits we need for the load from the
1156 // stored value.
1157 if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInst)) {
1158 // Can't forward from non-atomic to atomic without violating memory model.
1159 if (Address && Load->isAtomic() <= DepSI->isAtomic()) {
1160 int Offset =
1161 analyzeLoadFromClobberingStore(Load->getType(), Address, DepSI, DL);
1162 if (Offset != -1)
1163 return AvailableValue::get(DepSI->getValueOperand(), Offset);
1164 }
1165 }
1166
1167 // Check to see if we have something like this:
1168 // load i32* P
1169 // load i8* (P+1)
1170 // if we have this, replace the later with an extraction from the former.
1171 if (LoadInst *DepLoad = dyn_cast<LoadInst>(DepInst)) {
1172 // If this is a clobber and L is the first instruction in its block, then
1173 // we have the first instruction in the entry block.
1174 // Can't forward from non-atomic to atomic without violating memory model.
1175 if (DepLoad != Load && Address &&
1176 Load->isAtomic() <= DepLoad->isAtomic()) {
1177 Type *LoadType = Load->getType();
1178 int Offset = -1;
1179
1180 // If MD reported clobber, check it was nested.
1181 if (DepInfo.isClobber() &&
1182 canCoerceMustAliasedValueToLoad(DepLoad, LoadType, DL)) {
1183 const auto ClobberOff = MD->getClobberOffset(DepLoad);
1184 // GVN has no deal with a negative offset.
1185 Offset = (ClobberOff == std::nullopt || *ClobberOff < 0)
1186 ? -1
1187 : *ClobberOff;
1188 }
1189 if (Offset == -1)
1190 Offset =
1191 analyzeLoadFromClobberingLoad(LoadType, Address, DepLoad, DL);
1192 if (Offset != -1)
1193 return AvailableValue::getLoad(DepLoad, Offset);
1194 }
1195 }
1196
1197 // If the clobbering value is a memset/memcpy/memmove, see if we can
1198 // forward a value on from it.
1199 if (MemIntrinsic *DepMI = dyn_cast<MemIntrinsic>(DepInst)) {
1200 if (Address && !Load->isAtomic()) {
1201 int Offset = analyzeLoadFromClobberingMemInst(Load->getType(), Address,
1202 DepMI, DL);
1203 if (Offset != -1)
1204 return AvailableValue::getMI(DepMI, Offset);
1205 }
1206 }
1207
1208 // Nothing known about this clobber, have to be conservative
1209 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN: load "; Load->printAsOperand
(dbgs()); dbgs() << " is clobbered by " << *DepInst
<< '\n';; } } while (false)
1210 // fast print dep, using operator<< on instruction is too slow.do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN: load "; Load->printAsOperand
(dbgs()); dbgs() << " is clobbered by " << *DepInst
<< '\n';; } } while (false)
1211 dbgs() << "GVN: load "; Load->printAsOperand(dbgs());do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN: load "; Load->printAsOperand
(dbgs()); dbgs() << " is clobbered by " << *DepInst
<< '\n';; } } while (false)
1212 dbgs() << " is clobbered by " << *DepInst << '\n';)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN: load "; Load->printAsOperand
(dbgs()); dbgs() << " is clobbered by " << *DepInst
<< '\n';; } } while (false)
;
1213 if (ORE->allowExtraAnalysis(DEBUG_TYPE"gvn"))
1214 reportMayClobberedLoad(Load, DepInfo, DT, ORE);
1215
1216 return std::nullopt;
1217 }
1218 assert(DepInfo.isDef() && "follows from above")(static_cast <bool> (DepInfo.isDef() && "follows from above"
) ? void (0) : __assert_fail ("DepInfo.isDef() && \"follows from above\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 1218, __extension__ __PRETTY_FUNCTION__
))
;
1219
1220 // Loading the alloca -> undef.
1221 // Loading immediately after lifetime begin -> undef.
1222 if (isa<AllocaInst>(DepInst) || isLifetimeStart(DepInst))
1223 return AvailableValue::get(UndefValue::get(Load->getType()));
1224
1225 if (Constant *InitVal =
1226 getInitialValueOfAllocation(DepInst, TLI, Load->getType()))
1227 return AvailableValue::get(InitVal);
1228
1229 if (StoreInst *S = dyn_cast<StoreInst>(DepInst)) {
1230 // Reject loads and stores that are to the same address but are of
1231 // different types if we have to. If the stored value is convertable to
1232 // the loaded value, we can reuse it.
1233 if (!canCoerceMustAliasedValueToLoad(S->getValueOperand(), Load->getType(),
1234 DL))
1235 return std::nullopt;
1236
1237 // Can't forward from non-atomic to atomic without violating memory model.
1238 if (S->isAtomic() < Load->isAtomic())
1239 return std::nullopt;
1240
1241 return AvailableValue::get(S->getValueOperand());
1242 }
1243
1244 if (LoadInst *LD = dyn_cast<LoadInst>(DepInst)) {
1245 // If the types mismatch and we can't handle it, reject reuse of the load.
1246 // If the stored value is larger or equal to the loaded value, we can reuse
1247 // it.
1248 if (!canCoerceMustAliasedValueToLoad(LD, Load->getType(), DL))
1249 return std::nullopt;
1250
1251 // Can't forward from non-atomic to atomic without violating memory model.
1252 if (LD->isAtomic() < Load->isAtomic())
1253 return std::nullopt;
1254
1255 return AvailableValue::getLoad(LD);
1256 }
1257
1258 // Check if load with Addr dependent from select can be converted to select
1259 // between load values. There must be no instructions between the found
1260 // loads and DepInst that may clobber the loads.
1261 if (auto *Sel = dyn_cast<SelectInst>(DepInst)) {
1262 assert(Sel->getType() == Load->getPointerOperandType())(static_cast <bool> (Sel->getType() == Load->getPointerOperandType
()) ? void (0) : __assert_fail ("Sel->getType() == Load->getPointerOperandType()"
, "llvm/lib/Transforms/Scalar/GVN.cpp", 1262, __extension__ __PRETTY_FUNCTION__
))
;
1263 auto Loc = MemoryLocation::get(Load);
1264 Value *V1 =
1265 findDominatingValue(Loc.getWithNewPtr(Sel->getTrueValue()),
1266 Load->getType(), DepInst, getAliasAnalysis());
1267 if (!V1)
1268 return std::nullopt;
1269 Value *V2 =
1270 findDominatingValue(Loc.getWithNewPtr(Sel->getFalseValue()),
1271 Load->getType(), DepInst, getAliasAnalysis());
1272 if (!V2)
1273 return std::nullopt;
1274 return AvailableValue::getSelect(Sel, V1, V2);
1275 }
1276
1277 // Unknown def - must be conservative
1278 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN: load "; Load->printAsOperand
(dbgs()); dbgs() << " has unknown def " << *DepInst
<< '\n';; } } while (false)
1279 // fast print dep, using operator<< on instruction is too slow.do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN: load "; Load->printAsOperand
(dbgs()); dbgs() << " has unknown def " << *DepInst
<< '\n';; } } while (false)
1280 dbgs() << "GVN: load "; Load->printAsOperand(dbgs());do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN: load "; Load->printAsOperand
(dbgs()); dbgs() << " has unknown def " << *DepInst
<< '\n';; } } while (false)
1281 dbgs() << " has unknown def " << *DepInst << '\n';)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN: load "; Load->printAsOperand
(dbgs()); dbgs() << " has unknown def " << *DepInst
<< '\n';; } } while (false)
;
1282 return std::nullopt;
1283}
1284
1285void GVNPass::AnalyzeLoadAvailability(LoadInst *Load, LoadDepVect &Deps,
1286 AvailValInBlkVect &ValuesPerBlock,
1287 UnavailBlkVect &UnavailableBlocks) {
1288 // Filter out useless results (non-locals, etc). Keep track of the blocks
1289 // where we have a value available in repl, also keep track of whether we see
1290 // dependencies that produce an unknown value for the load (such as a call
1291 // that could potentially clobber the load).
1292 for (const auto &Dep : Deps) {
1293 BasicBlock *DepBB = Dep.getBB();
1294 MemDepResult DepInfo = Dep.getResult();
1295
1296 if (DeadBlocks.count(DepBB)) {
1297 // Dead dependent mem-op disguise as a load evaluating the same value
1298 // as the load in question.
1299 ValuesPerBlock.push_back(AvailableValueInBlock::getUndef(DepBB));
1300 continue;
1301 }
1302
1303 if (!DepInfo.isLocal()) {
1304 UnavailableBlocks.push_back(DepBB);
1305 continue;
1306 }
1307
1308 // The address being loaded in this non-local block may not be the same as
1309 // the pointer operand of the load if PHI translation occurs. Make sure
1310 // to consider the right address.
1311 if (auto AV = AnalyzeLoadAvailability(Load, DepInfo, Dep.getAddress())) {
1312 // subtlety: because we know this was a non-local dependency, we know
1313 // it's safe to materialize anywhere between the instruction within
1314 // DepInfo and the end of it's block.
1315 ValuesPerBlock.push_back(
1316 AvailableValueInBlock::get(DepBB, std::move(*AV)));
1317 } else {
1318 UnavailableBlocks.push_back(DepBB);
1319 }
1320 }
1321
1322 assert(Deps.size() == ValuesPerBlock.size() + UnavailableBlocks.size() &&(static_cast <bool> (Deps.size() == ValuesPerBlock.size
() + UnavailableBlocks.size() && "post condition violation"
) ? void (0) : __assert_fail ("Deps.size() == ValuesPerBlock.size() + UnavailableBlocks.size() && \"post condition violation\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 1323, __extension__ __PRETTY_FUNCTION__
))
1323 "post condition violation")(static_cast <bool> (Deps.size() == ValuesPerBlock.size
() + UnavailableBlocks.size() && "post condition violation"
) ? void (0) : __assert_fail ("Deps.size() == ValuesPerBlock.size() + UnavailableBlocks.size() && \"post condition violation\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 1323, __extension__ __PRETTY_FUNCTION__
))
;
1324}
1325
1326void GVNPass::eliminatePartiallyRedundantLoad(
1327 LoadInst *Load, AvailValInBlkVect &ValuesPerBlock,
1328 MapVector<BasicBlock *, Value *> &AvailableLoads) {
1329 for (const auto &AvailableLoad : AvailableLoads) {
1330 BasicBlock *UnavailableBlock = AvailableLoad.first;
1331 Value *LoadPtr = AvailableLoad.second;
1332
1333 auto *NewLoad =
1334 new LoadInst(Load->getType(), LoadPtr, Load->getName() + ".pre",
1335 Load->isVolatile(), Load->getAlign(), Load->getOrdering(),
1336 Load->getSyncScopeID(), UnavailableBlock->getTerminator());
1337 NewLoad->setDebugLoc(Load->getDebugLoc());
1338 if (MSSAU) {
1339 auto *MSSA = MSSAU->getMemorySSA();
1340 // Get the defining access of the original load or use the load if it is a
1341 // MemoryDef (e.g. because it is volatile). The inserted loads are
1342 // guaranteed to load from the same definition.
1343 auto *LoadAcc = MSSA->getMemoryAccess(Load);
1344 auto *DefiningAcc =
1345 isa<MemoryDef>(LoadAcc) ? LoadAcc : LoadAcc->getDefiningAccess();
1346 auto *NewAccess = MSSAU->createMemoryAccessInBB(
1347 NewLoad, DefiningAcc, NewLoad->getParent(),
1348 MemorySSA::BeforeTerminator);
1349 if (auto *NewDef = dyn_cast<MemoryDef>(NewAccess))
1350 MSSAU->insertDef(NewDef, /*RenameUses=*/true);
1351 else
1352 MSSAU->insertUse(cast<MemoryUse>(NewAccess), /*RenameUses=*/true);
1353 }
1354
1355 // Transfer the old load's AA tags to the new load.
1356 AAMDNodes Tags = Load->getAAMetadata();
1357 if (Tags)
1358 NewLoad->setAAMetadata(Tags);
1359
1360 if (auto *MD = Load->getMetadata(LLVMContext::MD_invariant_load))
1361 NewLoad->setMetadata(LLVMContext::MD_invariant_load, MD);
1362 if (auto *InvGroupMD = Load->getMetadata(LLVMContext::MD_invariant_group))
1363 NewLoad->setMetadata(LLVMContext::MD_invariant_group, InvGroupMD);
1364 if (auto *RangeMD = Load->getMetadata(LLVMContext::MD_range))
1365 NewLoad->setMetadata(LLVMContext::MD_range, RangeMD);
1366 if (auto *AccessMD = Load->getMetadata(LLVMContext::MD_access_group))
1367 if (LI &&
1368 LI->getLoopFor(Load->getParent()) == LI->getLoopFor(UnavailableBlock))
1369 NewLoad->setMetadata(LLVMContext::MD_access_group, AccessMD);
1370
1371 // We do not propagate the old load's debug location, because the new
1372 // load now lives in a different BB, and we want to avoid a jumpy line
1373 // table.
1374 // FIXME: How do we retain source locations without causing poor debugging
1375 // behavior?
1376
1377 // Add the newly created load.
1378 ValuesPerBlock.push_back(
1379 AvailableValueInBlock::get(UnavailableBlock, NewLoad));
1380 MD->invalidateCachedPointerInfo(LoadPtr);
1381 LLVM_DEBUG(dbgs() << "GVN INSERTED " << *NewLoad << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN INSERTED " << *NewLoad <<
'\n'; } } while (false)
;
1382 }
1383
1384 // Perform PHI construction.
1385 Value *V = ConstructSSAForLoadSet(Load, ValuesPerBlock, *this);
1386 // ConstructSSAForLoadSet is responsible for combining metadata.
1387 Load->replaceAllUsesWith(V);
1388 if (isa<PHINode>(V))
1389 V->takeName(Load);
1390 if (Instruction *I = dyn_cast<Instruction>(V))
1391 I->setDebugLoc(Load->getDebugLoc());
1392 if (V->getType()->isPtrOrPtrVectorTy())
1393 MD->invalidateCachedPointerInfo(V);
1394 markInstructionForDeletion(Load);
1395 ORE->emit([&]() {
1396 return OptimizationRemark(DEBUG_TYPE"gvn", "LoadPRE", Load)
1397 << "load eliminated by PRE";
1398 });
1399}
1400
1401bool GVNPass::PerformLoadPRE(LoadInst *Load, AvailValInBlkVect &ValuesPerBlock,
1402 UnavailBlkVect &UnavailableBlocks) {
1403 // Okay, we have *some* definitions of the value. This means that the value
1404 // is available in some of our (transitive) predecessors. Lets think about
1405 // doing PRE of this load. This will involve inserting a new load into the
1406 // predecessor when it's not available. We could do this in general, but
1407 // prefer to not increase code size. As such, we only do this when we know
1408 // that we only have to insert *one* load (which means we're basically moving
1409 // the load, not inserting a new one).
1410
1411 SmallPtrSet<BasicBlock *, 4> Blockers(UnavailableBlocks.begin(),
1412 UnavailableBlocks.end());
1413
1414 // Let's find the first basic block with more than one predecessor. Walk
1415 // backwards through predecessors if needed.
1416 BasicBlock *LoadBB = Load->getParent();
1417 BasicBlock *TmpBB = LoadBB;
1418
1419 // Check that there is no implicit control flow instructions above our load in
1420 // its block. If there is an instruction that doesn't always pass the
1421 // execution to the following instruction, then moving through it may become
1422 // invalid. For example:
1423 //
1424 // int arr[LEN];
1425 // int index = ???;
1426 // ...
1427 // guard(0 <= index && index < LEN);
1428 // use(arr[index]);
1429 //
1430 // It is illegal to move the array access to any point above the guard,
1431 // because if the index is out of bounds we should deoptimize rather than
1432 // access the array.
1433 // Check that there is no guard in this block above our instruction.
1434 bool MustEnsureSafetyOfSpeculativeExecution =
1435 ICF->isDominatedByICFIFromSameBlock(Load);
1436
1437 while (TmpBB->getSinglePredecessor()) {
1438 TmpBB = TmpBB->getSinglePredecessor();
1439 if (TmpBB == LoadBB) // Infinite (unreachable) loop.
1440 return false;
1441 if (Blockers.count(TmpBB))
1442 return false;
1443
1444 // If any of these blocks has more than one successor (i.e. if the edge we
1445 // just traversed was critical), then there are other paths through this
1446 // block along which the load may not be anticipated. Hoisting the load
1447 // above this block would be adding the load to execution paths along
1448 // which it was not previously executed.
1449 if (TmpBB->getTerminator()->getNumSuccessors() != 1)
1450 return false;
1451
1452 // Check that there is no implicit control flow in a block above.
1453 MustEnsureSafetyOfSpeculativeExecution =
1454 MustEnsureSafetyOfSpeculativeExecution || ICF->hasICF(TmpBB);
1455 }
1456
1457 assert(TmpBB)(static_cast <bool> (TmpBB) ? void (0) : __assert_fail (
"TmpBB", "llvm/lib/Transforms/Scalar/GVN.cpp", 1457, __extension__
__PRETTY_FUNCTION__))
;
1458 LoadBB = TmpBB;
1459
1460 // Check to see how many predecessors have the loaded value fully
1461 // available.
1462 MapVector<BasicBlock *, Value *> PredLoads;
1463 DenseMap<BasicBlock *, AvailabilityState> FullyAvailableBlocks;
1464 for (const AvailableValueInBlock &AV : ValuesPerBlock)
1465 FullyAvailableBlocks[AV.BB] = AvailabilityState::Available;
1466 for (BasicBlock *UnavailableBB : UnavailableBlocks)
1467 FullyAvailableBlocks[UnavailableBB] = AvailabilityState::Unavailable;
1468
1469 SmallVector<BasicBlock *, 4> CriticalEdgePred;
1470 for (BasicBlock *Pred : predecessors(LoadBB)) {
1471 // If any predecessor block is an EH pad that does not allow non-PHI
1472 // instructions before the terminator, we can't PRE the load.
1473 if (Pred->getTerminator()->isEHPad()) {
1474 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD PREDECESSOR '"
<< Pred->getName() << "': " << *Load <<
'\n'; } } while (false)
1475 dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD PREDECESSOR '"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD PREDECESSOR '"
<< Pred->getName() << "': " << *Load <<
'\n'; } } while (false)
1476 << Pred->getName() << "': " << *Load << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD PREDECESSOR '"
<< Pred->getName() << "': " << *Load <<
'\n'; } } while (false)
;
1477 return false;
1478 }
1479
1480 if (IsValueFullyAvailableInBlock(Pred, FullyAvailableBlocks)) {
1481 continue;
1482 }
1483
1484 if (Pred->getTerminator()->getNumSuccessors() != 1) {
1485 if (isa<IndirectBrInst>(Pred->getTerminator())) {
1486 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "COULD NOT PRE LOAD BECAUSE OF INDBR CRITICAL EDGE '"
<< Pred->getName() << "': " << *Load <<
'\n'; } } while (false)
1487 dbgs() << "COULD NOT PRE LOAD BECAUSE OF INDBR CRITICAL EDGE '"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "COULD NOT PRE LOAD BECAUSE OF INDBR CRITICAL EDGE '"
<< Pred->getName() << "': " << *Load <<
'\n'; } } while (false)
1488 << Pred->getName() << "': " << *Load << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "COULD NOT PRE LOAD BECAUSE OF INDBR CRITICAL EDGE '"
<< Pred->getName() << "': " << *Load <<
'\n'; } } while (false)
;
1489 return false;
1490 }
1491
1492 if (LoadBB->isEHPad()) {
1493 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD CRITICAL EDGE '"
<< Pred->getName() << "': " << *Load <<
'\n'; } } while (false)
1494 dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD CRITICAL EDGE '"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD CRITICAL EDGE '"
<< Pred->getName() << "': " << *Load <<
'\n'; } } while (false)
1495 << Pred->getName() << "': " << *Load << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD CRITICAL EDGE '"
<< Pred->getName() << "': " << *Load <<
'\n'; } } while (false)
;
1496 return false;
1497 }
1498
1499 // Do not split backedge as it will break the canonical loop form.
1500 if (!isLoadPRESplitBackedgeEnabled())
1501 if (DT->dominates(LoadBB, Pred)) {
1502 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "COULD NOT PRE LOAD BECAUSE OF A BACKEDGE CRITICAL EDGE '"
<< Pred->getName() << "': " << *Load <<
'\n'; } } while (false)
1503 dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "COULD NOT PRE LOAD BECAUSE OF A BACKEDGE CRITICAL EDGE '"
<< Pred->getName() << "': " << *Load <<
'\n'; } } while (false)
1504 << "COULD NOT PRE LOAD BECAUSE OF A BACKEDGE CRITICAL EDGE '"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "COULD NOT PRE LOAD BECAUSE OF A BACKEDGE CRITICAL EDGE '"
<< Pred->getName() << "': " << *Load <<
'\n'; } } while (false)
1505 << Pred->getName() << "': " << *Load << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "COULD NOT PRE LOAD BECAUSE OF A BACKEDGE CRITICAL EDGE '"
<< Pred->getName() << "': " << *Load <<
'\n'; } } while (false)
;
1506 return false;
1507 }
1508
1509 CriticalEdgePred.push_back(Pred);
1510 } else {
1511 // Only add the predecessors that will not be split for now.
1512 PredLoads[Pred] = nullptr;
1513 }
1514 }
1515
1516 // Decide whether PRE is profitable for this load.
1517 unsigned NumUnavailablePreds = PredLoads.size() + CriticalEdgePred.size();
1518 assert(NumUnavailablePreds != 0 &&(static_cast <bool> (NumUnavailablePreds != 0 &&
"Fully available value should already be eliminated!") ? void
(0) : __assert_fail ("NumUnavailablePreds != 0 && \"Fully available value should already be eliminated!\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 1519, __extension__ __PRETTY_FUNCTION__
))
1519 "Fully available value should already be eliminated!")(static_cast <bool> (NumUnavailablePreds != 0 &&
"Fully available value should already be eliminated!") ? void
(0) : __assert_fail ("NumUnavailablePreds != 0 && \"Fully available value should already be eliminated!\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 1519, __extension__ __PRETTY_FUNCTION__
))
;
1520 (void)NumUnavailablePreds;
1521
1522 // If this load is unavailable in multiple predecessors, reject it.
1523 // FIXME: If we could restructure the CFG, we could make a common pred with
1524 // all the preds that don't have an available Load and insert a new load into
1525 // that one block.
1526 if (NumUnavailablePreds != 1)
1527 return false;
1528
1529 // Now we know where we will insert load. We must ensure that it is safe
1530 // to speculatively execute the load at that points.
1531 if (MustEnsureSafetyOfSpeculativeExecution) {
1532 if (CriticalEdgePred.size())
1533 if (!isSafeToSpeculativelyExecute(Load, LoadBB->getFirstNonPHI(), AC, DT))
1534 return false;
1535 for (auto &PL : PredLoads)
1536 if (!isSafeToSpeculativelyExecute(Load, PL.first->getTerminator(), AC,
1537 DT))
1538 return false;
1539 }
1540
1541 // Split critical edges, and update the unavailable predecessors accordingly.
1542 for (BasicBlock *OrigPred : CriticalEdgePred) {
1543 BasicBlock *NewPred = splitCriticalEdges(OrigPred, LoadBB);
1544 assert(!PredLoads.count(OrigPred) && "Split edges shouldn't be in map!")(static_cast <bool> (!PredLoads.count(OrigPred) &&
"Split edges shouldn't be in map!") ? void (0) : __assert_fail
("!PredLoads.count(OrigPred) && \"Split edges shouldn't be in map!\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 1544, __extension__ __PRETTY_FUNCTION__
))
;
1545 PredLoads[NewPred] = nullptr;
1546 LLVM_DEBUG(dbgs() << "Split critical edge " << OrigPred->getName() << "->"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "Split critical edge " << OrigPred
->getName() << "->" << LoadBB->getName()
<< '\n'; } } while (false)
1547 << LoadBB->getName() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "Split critical edge " << OrigPred
->getName() << "->" << LoadBB->getName()
<< '\n'; } } while (false)
;
1548 }
1549
1550 // Check if the load can safely be moved to all the unavailable predecessors.
1551 bool CanDoPRE = true;
1552 const DataLayout &DL = Load->getModule()->getDataLayout();
1553 SmallVector<Instruction*, 8> NewInsts;
1554 for (auto &PredLoad : PredLoads) {
1555 BasicBlock *UnavailablePred = PredLoad.first;
1556
1557 // Do PHI translation to get its value in the predecessor if necessary. The
1558 // returned pointer (if non-null) is guaranteed to dominate UnavailablePred.
1559 // We do the translation for each edge we skipped by going from Load's block
1560 // to LoadBB, otherwise we might miss pieces needing translation.
1561
1562 // If all preds have a single successor, then we know it is safe to insert
1563 // the load on the pred (?!?), so we can insert code to materialize the
1564 // pointer if it is not available.
1565 Value *LoadPtr = Load->getPointerOperand();
1566 BasicBlock *Cur = Load->getParent();
1567 while (Cur != LoadBB) {
1568 PHITransAddr Address(LoadPtr, DL, AC);
1569 LoadPtr = Address.translateWithInsertion(Cur, Cur->getSinglePredecessor(),
1570 *DT, NewInsts);
1571 if (!LoadPtr) {
1572 CanDoPRE = false;
1573 break;
1574 }
1575 Cur = Cur->getSinglePredecessor();
1576 }
1577
1578 if (LoadPtr) {
1579 PHITransAddr Address(LoadPtr, DL, AC);
1580 LoadPtr = Address.translateWithInsertion(LoadBB, UnavailablePred, *DT,
1581 NewInsts);
1582 }
1583 // If we couldn't find or insert a computation of this phi translated value,
1584 // we fail PRE.
1585 if (!LoadPtr) {
1586 LLVM_DEBUG(dbgs() << "COULDN'T INSERT PHI TRANSLATED VALUE OF: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "COULDN'T INSERT PHI TRANSLATED VALUE OF: "
<< *Load->getPointerOperand() << "\n"; } } while
(false)
1587 << *Load->getPointerOperand() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "COULDN'T INSERT PHI TRANSLATED VALUE OF: "
<< *Load->getPointerOperand() << "\n"; } } while
(false)
;
1588 CanDoPRE = false;
1589 break;
1590 }
1591
1592 PredLoad.second = LoadPtr;
1593 }
1594
1595 if (!CanDoPRE) {
1596 while (!NewInsts.empty()) {
1597 // Erase instructions generated by the failed PHI translation before
1598 // trying to number them. PHI translation might insert instructions
1599 // in basic blocks other than the current one, and we delete them
1600 // directly, as markInstructionForDeletion only allows removing from the
1601 // current basic block.
1602 NewInsts.pop_back_val()->eraseFromParent();
1603 }
1604 // HINT: Don't revert the edge-splitting as following transformation may
1605 // also need to split these critical edges.
1606 return !CriticalEdgePred.empty();
1607 }
1608
1609 // Okay, we can eliminate this load by inserting a reload in the predecessor
1610 // and using PHI construction to get the value in the other predecessors, do
1611 // it.
1612 LLVM_DEBUG(dbgs() << "GVN REMOVING PRE LOAD: " << *Load << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN REMOVING PRE LOAD: " <<
*Load << '\n'; } } while (false)
;
1613 LLVM_DEBUG(if (!NewInsts.empty()) dbgs() << "INSERTED " << NewInsts.size()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { if (!NewInsts.empty()) dbgs() << "INSERTED "
<< NewInsts.size() << " INSTS: " << *NewInsts
.back() << '\n'; } } while (false)
1614 << " INSTS: " << *NewInsts.back()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { if (!NewInsts.empty()) dbgs() << "INSERTED "
<< NewInsts.size() << " INSTS: " << *NewInsts
.back() << '\n'; } } while (false)
1615 << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { if (!NewInsts.empty()) dbgs() << "INSERTED "
<< NewInsts.size() << " INSTS: " << *NewInsts
.back() << '\n'; } } while (false)
;
1616
1617 // Assign value numbers to the new instructions.
1618 for (Instruction *I : NewInsts) {
1619 // Instructions that have been inserted in predecessor(s) to materialize
1620 // the load address do not retain their original debug locations. Doing
1621 // so could lead to confusing (but correct) source attributions.
1622 I->updateLocationAfterHoist();
1623
1624 // FIXME: We really _ought_ to insert these value numbers into their
1625 // parent's availability map. However, in doing so, we risk getting into
1626 // ordering issues. If a block hasn't been processed yet, we would be
1627 // marking a value as AVAIL-IN, which isn't what we intend.
1628 VN.lookupOrAdd(I);
1629 }
1630
1631 eliminatePartiallyRedundantLoad(Load, ValuesPerBlock, PredLoads);
1632 ++NumPRELoad;
1633 return true;
1634}
1635
1636bool GVNPass::performLoopLoadPRE(LoadInst *Load,
1637 AvailValInBlkVect &ValuesPerBlock,
1638 UnavailBlkVect &UnavailableBlocks) {
1639 if (!LI)
1640 return false;
1641
1642 const Loop *L = LI->getLoopFor(Load->getParent());
1643 // TODO: Generalize to other loop blocks that dominate the latch.
1644 if (!L || L->getHeader() != Load->getParent())
1645 return false;
1646
1647 BasicBlock *Preheader = L->getLoopPreheader();
1648 BasicBlock *Latch = L->getLoopLatch();
1649 if (!Preheader || !Latch)
1650 return false;
1651
1652 Value *LoadPtr = Load->getPointerOperand();
1653 // Must be available in preheader.
1654 if (!L->isLoopInvariant(LoadPtr))
1655 return false;
1656
1657 // We plan to hoist the load to preheader without introducing a new fault.
1658 // In order to do it, we need to prove that we cannot side-exit the loop
1659 // once loop header is first entered before execution of the load.
1660 if (ICF->isDominatedByICFIFromSameBlock(Load))
1661 return false;
1662
1663 BasicBlock *LoopBlock = nullptr;
1664 for (auto *Blocker : UnavailableBlocks) {
1665 // Blockers from outside the loop are handled in preheader.
1666 if (!L->contains(Blocker))
1667 continue;
1668
1669 // Only allow one loop block. Loop header is not less frequently executed
1670 // than each loop block, and likely it is much more frequently executed. But
1671 // in case of multiple loop blocks, we need extra information (such as block
1672 // frequency info) to understand whether it is profitable to PRE into
1673 // multiple loop blocks.
1674 if (LoopBlock)
1675 return false;
1676
1677 // Do not sink into inner loops. This may be non-profitable.
1678 if (L != LI->getLoopFor(Blocker))
1679 return false;
1680
1681 // Blocks that dominate the latch execute on every single iteration, maybe
1682 // except the last one. So PREing into these blocks doesn't make much sense
1683 // in most cases. But the blocks that do not necessarily execute on each
1684 // iteration are sometimes much colder than the header, and this is when
1685 // PRE is potentially profitable.
1686 if (DT->dominates(Blocker, Latch))
1687 return false;
1688
1689 // Make sure that the terminator itself doesn't clobber.
1690 if (Blocker->getTerminator()->mayWriteToMemory())
1691 return false;
1692
1693 LoopBlock = Blocker;
1694 }
1695
1696 if (!LoopBlock)
1697 return false;
1698
1699 // Make sure the memory at this pointer cannot be freed, therefore we can
1700 // safely reload from it after clobber.
1701 if (LoadPtr->canBeFreed())
1702 return false;
1703
1704 // TODO: Support critical edge splitting if blocker has more than 1 successor.
1705 MapVector<BasicBlock *, Value *> AvailableLoads;
1706 AvailableLoads[LoopBlock] = LoadPtr;
1707 AvailableLoads[Preheader] = LoadPtr;
1708
1709 LLVM_DEBUG(dbgs() << "GVN REMOVING PRE LOOP LOAD: " << *Load << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN REMOVING PRE LOOP LOAD: " <<
*Load << '\n'; } } while (false)
;
1710 eliminatePartiallyRedundantLoad(Load, ValuesPerBlock, AvailableLoads);
1711 ++NumPRELoopLoad;
1712 return true;
1713}
1714
1715static void reportLoadElim(LoadInst *Load, Value *AvailableValue,
1716 OptimizationRemarkEmitter *ORE) {
1717 using namespace ore;
1718
1719 ORE->emit([&]() {
1720 return OptimizationRemark(DEBUG_TYPE"gvn", "LoadElim", Load)
1721 << "load of type " << NV("Type", Load->getType()) << " eliminated"
1722 << setExtraArgs() << " in favor of "
1723 << NV("InfavorOfValue", AvailableValue);
1724 });
1725}
1726
1727/// Attempt to eliminate a load whose dependencies are
1728/// non-local by performing PHI construction.
1729bool GVNPass::processNonLocalLoad(LoadInst *Load) {
1730 // non-local speculations are not allowed under asan.
1731 if (Load->getParent()->getParent()->hasFnAttribute(
1732 Attribute::SanitizeAddress) ||
1733 Load->getParent()->getParent()->hasFnAttribute(
1734 Attribute::SanitizeHWAddress))
1735 return false;
1736
1737 // Step 1: Find the non-local dependencies of the load.
1738 LoadDepVect Deps;
1739 MD->getNonLocalPointerDependency(Load, Deps);
1740
1741 // If we had to process more than one hundred blocks to find the
1742 // dependencies, this load isn't worth worrying about. Optimizing
1743 // it will be too expensive.
1744 unsigned NumDeps = Deps.size();
1745 if (NumDeps > MaxNumDeps)
1746 return false;
1747
1748 // If we had a phi translation failure, we'll have a single entry which is a
1749 // clobber in the current block. Reject this early.
1750 if (NumDeps == 1 &&
1751 !Deps[0].getResult().isDef() && !Deps[0].getResult().isClobber()) {
1752 LLVM_DEBUG(dbgs() << "GVN: non-local load "; Load->printAsOperand(dbgs());do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN: non-local load "; Load->printAsOperand
(dbgs()); dbgs() << " has unknown dependencies\n";; } }
while (false)
1753 dbgs() << " has unknown dependencies\n";)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN: non-local load "; Load->printAsOperand
(dbgs()); dbgs() << " has unknown dependencies\n";; } }
while (false)
;
1754 return false;
1755 }
1756
1757 bool Changed = false;
1758 // If this load follows a GEP, see if we can PRE the indices before analyzing.
1759 if (GetElementPtrInst *GEP =
1760 dyn_cast<GetElementPtrInst>(Load->getOperand(0))) {
1761 for (Use &U : GEP->indices())
1762 if (Instruction *I = dyn_cast<Instruction>(U.get()))
1763 Changed |= performScalarPRE(I);
1764 }
1765
1766 // Step 2: Analyze the availability of the load
1767 AvailValInBlkVect ValuesPerBlock;
1768 UnavailBlkVect UnavailableBlocks;
1769 AnalyzeLoadAvailability(Load, Deps, ValuesPerBlock, UnavailableBlocks);
1770
1771 // If we have no predecessors that produce a known value for this load, exit
1772 // early.
1773 if (ValuesPerBlock.empty())
1774 return Changed;
1775
1776 // Step 3: Eliminate fully redundancy.
1777 //
1778 // If all of the instructions we depend on produce a known value for this
1779 // load, then it is fully redundant and we can use PHI insertion to compute
1780 // its value. Insert PHIs and remove the fully redundant value now.
1781 if (UnavailableBlocks.empty()) {
1782 LLVM_DEBUG(dbgs() << "GVN REMOVING NONLOCAL LOAD: " << *Load << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN REMOVING NONLOCAL LOAD: " <<
*Load << '\n'; } } while (false)
;
1783
1784 // Perform PHI construction.
1785 Value *V = ConstructSSAForLoadSet(Load, ValuesPerBlock, *this);
1786 // ConstructSSAForLoadSet is responsible for combining metadata.
1787 Load->replaceAllUsesWith(V);
1788
1789 if (isa<PHINode>(V))
1790 V->takeName(Load);
1791 if (Instruction *I = dyn_cast<Instruction>(V))
1792 // If instruction I has debug info, then we should not update it.
1793 // Also, if I has a null DebugLoc, then it is still potentially incorrect
1794 // to propagate Load's DebugLoc because Load may not post-dominate I.
1795 if (Load->getDebugLoc() && Load->getParent() == I->getParent())
1796 I->setDebugLoc(Load->getDebugLoc());
1797 if (V->getType()->isPtrOrPtrVectorTy())
1798 MD->invalidateCachedPointerInfo(V);
1799 markInstructionForDeletion(Load);
1800 ++NumGVNLoad;
1801 reportLoadElim(Load, V, ORE);
1802 return true;
1803 }
1804
1805 // Step 4: Eliminate partial redundancy.
1806 if (!isPREEnabled() || !isLoadPREEnabled())
1807 return Changed;
1808 if (!isLoadInLoopPREEnabled() && LI && LI->getLoopFor(Load->getParent()))
1809 return Changed;
1810
1811 if (performLoopLoadPRE(Load, ValuesPerBlock, UnavailableBlocks) ||
1812 PerformLoadPRE(Load, ValuesPerBlock, UnavailableBlocks))
1813 return true;
1814
1815 return Changed;
1816}
1817
1818static bool impliesEquivalanceIfTrue(CmpInst* Cmp) {
1819 if (Cmp->getPredicate() == CmpInst::Predicate::ICMP_EQ)
1820 return true;
1821
1822 // Floating point comparisons can be equal, but not equivalent. Cases:
1823 // NaNs for unordered operators
1824 // +0.0 vs 0.0 for all operators
1825 if (Cmp->getPredicate() == CmpInst::Predicate::FCMP_OEQ ||
1826 (Cmp->getPredicate() == CmpInst::Predicate::FCMP_UEQ &&
1827 Cmp->getFastMathFlags().noNaNs())) {
1828 Value *LHS = Cmp->getOperand(0);
1829 Value *RHS = Cmp->getOperand(1);
1830 // If we can prove either side non-zero, then equality must imply
1831 // equivalence.
1832 // FIXME: We should do this optimization if 'no signed zeros' is
1833 // applicable via an instruction-level fast-math-flag or some other
1834 // indicator that relaxed FP semantics are being used.
1835 if (isa<ConstantFP>(LHS) && !cast<ConstantFP>(LHS)->isZero())
1836 return true;
1837 if (isa<ConstantFP>(RHS) && !cast<ConstantFP>(RHS)->isZero())
1838 return true;;
1839 // TODO: Handle vector floating point constants
1840 }
1841 return false;
1842}
1843
1844static bool impliesEquivalanceIfFalse(CmpInst* Cmp) {
1845 if (Cmp->getPredicate() == CmpInst::Predicate::ICMP_NE)
1846 return true;
1847
1848 // Floating point comparisons can be equal, but not equivelent. Cases:
1849 // NaNs for unordered operators
1850 // +0.0 vs 0.0 for all operators
1851 if ((Cmp->getPredicate() == CmpInst::Predicate::FCMP_ONE &&
1852 Cmp->getFastMathFlags().noNaNs()) ||
1853 Cmp->getPredicate() == CmpInst::Predicate::FCMP_UNE) {
1854 Value *LHS = Cmp->getOperand(0);
1855 Value *RHS = Cmp->getOperand(1);
1856 // If we can prove either side non-zero, then equality must imply
1857 // equivalence.
1858 // FIXME: We should do this optimization if 'no signed zeros' is
1859 // applicable via an instruction-level fast-math-flag or some other
1860 // indicator that relaxed FP semantics are being used.
1861 if (isa<ConstantFP>(LHS) && !cast<ConstantFP>(LHS)->isZero())
1862 return true;
1863 if (isa<ConstantFP>(RHS) && !cast<ConstantFP>(RHS)->isZero())
1864 return true;;
1865 // TODO: Handle vector floating point constants
1866 }
1867 return false;
1868}
1869
1870
1871static bool hasUsersIn(Value *V, BasicBlock *BB) {
1872 return llvm::any_of(V->users(), [BB](User *U) {
1873 auto *I = dyn_cast<Instruction>(U);
1874 return I && I->getParent() == BB;
1875 });
1876}
1877
1878bool GVNPass::processAssumeIntrinsic(AssumeInst *IntrinsicI) {
1879 Value *V = IntrinsicI->getArgOperand(0);
1880
1881 if (ConstantInt *Cond = dyn_cast<ConstantInt>(V)) {
1882 if (Cond->isZero()) {
1883 Type *Int8Ty = Type::getInt8Ty(V->getContext());
1884 // Insert a new store to null instruction before the load to indicate that
1885 // this code is not reachable. FIXME: We could insert unreachable
1886 // instruction directly because we can modify the CFG.
1887 auto *NewS = new StoreInst(PoisonValue::get(Int8Ty),
1888 Constant::getNullValue(Int8Ty->getPointerTo()),
1889 IntrinsicI);
1890 if (MSSAU) {
1891 const MemoryUseOrDef *FirstNonDom = nullptr;
1892 const auto *AL =
1893 MSSAU->getMemorySSA()->getBlockAccesses(IntrinsicI->getParent());
1894
1895 // If there are accesses in the current basic block, find the first one
1896 // that does not come before NewS. The new memory access is inserted
1897 // after the found access or before the terminator if no such access is
1898 // found.
1899 if (AL) {
1900 for (const auto &Acc : *AL) {
1901 if (auto *Current = dyn_cast<MemoryUseOrDef>(&Acc))
1902 if (!Current->getMemoryInst()->comesBefore(NewS)) {
1903 FirstNonDom = Current;
1904 break;
1905 }
1906 }
1907 }
1908
1909 // This added store is to null, so it will never executed and we can
1910 // just use the LiveOnEntry def as defining access.
1911 auto *NewDef =
1912 FirstNonDom ? MSSAU->createMemoryAccessBefore(
1913 NewS, MSSAU->getMemorySSA()->getLiveOnEntryDef(),
1914 const_cast<MemoryUseOrDef *>(FirstNonDom))
1915 : MSSAU->createMemoryAccessInBB(
1916 NewS, MSSAU->getMemorySSA()->getLiveOnEntryDef(),
1917 NewS->getParent(), MemorySSA::BeforeTerminator);
1918
1919 MSSAU->insertDef(cast<MemoryDef>(NewDef), /*RenameUses=*/false);
1920 }
1921 }
1922 if (isAssumeWithEmptyBundle(*IntrinsicI)) {
1923 markInstructionForDeletion(IntrinsicI);
1924 return true;
1925 }
1926 return false;
1927 } else if (isa<Constant>(V)) {
1928 // If it's not false, and constant, it must evaluate to true. This means our
1929 // assume is assume(true), and thus, pointless, and we don't want to do
1930 // anything more here.
1931 return false;
1932 }
1933
1934 Constant *True = ConstantInt::getTrue(V->getContext());
1935 bool Changed = false;
1936
1937 for (BasicBlock *Successor : successors(IntrinsicI->getParent())) {
1938 BasicBlockEdge Edge(IntrinsicI->getParent(), Successor);
1939
1940 // This property is only true in dominated successors, propagateEquality
1941 // will check dominance for us.
1942 Changed |= propagateEquality(V, True, Edge, false);
1943 }
1944
1945 // We can replace assume value with true, which covers cases like this:
1946 // call void @llvm.assume(i1 %cmp)
1947 // br i1 %cmp, label %bb1, label %bb2 ; will change %cmp to true
1948 ReplaceOperandsWithMap[V] = True;
1949
1950 // Similarly, after assume(!NotV) we know that NotV == false.
1951 Value *NotV;
1952 if (match(V, m_Not(m_Value(NotV))))
1953 ReplaceOperandsWithMap[NotV] = ConstantInt::getFalse(V->getContext());
1954
1955 // If we find an equality fact, canonicalize all dominated uses in this block
1956 // to one of the two values. We heuristically choice the "oldest" of the
1957 // two where age is determined by value number. (Note that propagateEquality
1958 // above handles the cross block case.)
1959 //
1960 // Key case to cover are:
1961 // 1)
1962 // %cmp = fcmp oeq float 3.000000e+00, %0 ; const on lhs could happen
1963 // call void @llvm.assume(i1 %cmp)
1964 // ret float %0 ; will change it to ret float 3.000000e+00
1965 // 2)
1966 // %load = load float, float* %addr
1967 // %cmp = fcmp oeq float %load, %0
1968 // call void @llvm.assume(i1 %cmp)
1969 // ret float %load ; will change it to ret float %0
1970 if (auto *CmpI = dyn_cast<CmpInst>(V)) {
1971 if (impliesEquivalanceIfTrue(CmpI)) {
1972 Value *CmpLHS = CmpI->getOperand(0);
1973 Value *CmpRHS = CmpI->getOperand(1);
1974 // Heuristically pick the better replacement -- the choice of heuristic
1975 // isn't terribly important here, but the fact we canonicalize on some
1976 // replacement is for exposing other simplifications.
1977 // TODO: pull this out as a helper function and reuse w/existing
1978 // (slightly different) logic.
1979 if (isa<Constant>(CmpLHS) && !isa<Constant>(CmpRHS))
1980 std::swap(CmpLHS, CmpRHS);
1981 if (!isa<Instruction>(CmpLHS) && isa<Instruction>(CmpRHS))
1982 std::swap(CmpLHS, CmpRHS);
1983 if ((isa<Argument>(CmpLHS) && isa<Argument>(CmpRHS)) ||
1984 (isa<Instruction>(CmpLHS) && isa<Instruction>(CmpRHS))) {
1985 // Move the 'oldest' value to the right-hand side, using the value
1986 // number as a proxy for age.
1987 uint32_t LVN = VN.lookupOrAdd(CmpLHS);
1988 uint32_t RVN = VN.lookupOrAdd(CmpRHS);
1989 if (LVN < RVN)
1990 std::swap(CmpLHS, CmpRHS);
1991 }
1992
1993 // Handle degenerate case where we either haven't pruned a dead path or a
1994 // removed a trivial assume yet.
1995 if (isa<Constant>(CmpLHS) && isa<Constant>(CmpRHS))
1996 return Changed;
1997
1998 LLVM_DEBUG(dbgs() << "Replacing dominated uses of "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "Replacing dominated uses of " <<
*CmpLHS << " with " << *CmpRHS << " in block "
<< IntrinsicI->getParent()->getName() << "\n"
; } } while (false)
1999 << *CmpLHS << " with "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "Replacing dominated uses of " <<
*CmpLHS << " with " << *CmpRHS << " in block "
<< IntrinsicI->getParent()->getName() << "\n"
; } } while (false)
2000 << *CmpRHS << " in block "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "Replacing dominated uses of " <<
*CmpLHS << " with " << *CmpRHS << " in block "
<< IntrinsicI->getParent()->getName() << "\n"
; } } while (false)
2001 << IntrinsicI->getParent()->getName() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "Replacing dominated uses of " <<
*CmpLHS << " with " << *CmpRHS << " in block "
<< IntrinsicI->getParent()->getName() << "\n"
; } } while (false)
;
2002
2003
2004 // Setup the replacement map - this handles uses within the same block
2005 if (hasUsersIn(CmpLHS, IntrinsicI->getParent()))
2006 ReplaceOperandsWithMap[CmpLHS] = CmpRHS;
2007
2008 // NOTE: The non-block local cases are handled by the call to
2009 // propagateEquality above; this block is just about handling the block
2010 // local cases. TODO: There's a bunch of logic in propagateEqualiy which
2011 // isn't duplicated for the block local case, can we share it somehow?
2012 }
2013 }
2014 return Changed;
2015}
2016
2017static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl) {
2018 patchReplacementInstruction(I, Repl);
2019 I->replaceAllUsesWith(Repl);
2020}
2021
2022/// Attempt to eliminate a load, first by eliminating it
2023/// locally, and then attempting non-local elimination if that fails.
2024bool GVNPass::processLoad(LoadInst *L) {
2025 if (!MD)
2026 return false;
2027
2028 // This code hasn't been audited for ordered or volatile memory access
2029 if (!L->isUnordered())
2030 return false;
2031
2032 if (L->use_empty()) {
2033 markInstructionForDeletion(L);
2034 return true;
2035 }
2036
2037 // ... to a pointer that has been loaded from before...
2038 MemDepResult Dep = MD->getDependency(L);
2039
2040 // If it is defined in another block, try harder.
2041 if (Dep.isNonLocal())
2042 return processNonLocalLoad(L);
2043
2044 // Only handle the local case below
2045 if (!Dep.isLocal()) {
2046 // This might be a NonFuncLocal or an Unknown
2047 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN: load "; L->printAsOperand
(dbgs()); dbgs() << " has unknown dependence\n";; } } while
(false)
2048 // fast print dep, using operator<< on instruction is too slow.do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN: load "; L->printAsOperand
(dbgs()); dbgs() << " has unknown dependence\n";; } } while
(false)
2049 dbgs() << "GVN: load "; L->printAsOperand(dbgs());do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN: load "; L->printAsOperand
(dbgs()); dbgs() << " has unknown dependence\n";; } } while
(false)
2050 dbgs() << " has unknown dependence\n";)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN: load "; L->printAsOperand
(dbgs()); dbgs() << " has unknown dependence\n";; } } while
(false)
;
2051 return false;
2052 }
2053
2054 auto AV = AnalyzeLoadAvailability(L, Dep, L->getPointerOperand());
2055 if (!AV)
2056 return false;
2057
2058 Value *AvailableValue = AV->MaterializeAdjustedValue(L, L, *this);
2059
2060 // MaterializeAdjustedValue is responsible for combining metadata.
2061 L->replaceAllUsesWith(AvailableValue);
2062 markInstructionForDeletion(L);
2063 if (MSSAU)
2064 MSSAU->removeMemoryAccess(L);
2065 ++NumGVNLoad;
2066 reportLoadElim(L, AvailableValue, ORE);
2067 // Tell MDA to reexamine the reused pointer since we might have more
2068 // information after forwarding it.
2069 if (MD && AvailableValue->getType()->isPtrOrPtrVectorTy())
2070 MD->invalidateCachedPointerInfo(AvailableValue);
2071 return true;
2072}
2073
2074/// Return a pair the first field showing the value number of \p Exp and the
2075/// second field showing whether it is a value number newly created.
2076std::pair<uint32_t, bool>
2077GVNPass::ValueTable::assignExpNewValueNum(Expression &Exp) {
2078 uint32_t &e = expressionNumbering[Exp];
2079 bool CreateNewValNum = !e;
2080 if (CreateNewValNum) {
2081 Expressions.push_back(Exp);
2082 if (ExprIdx.size() < nextValueNumber + 1)
2083 ExprIdx.resize(nextValueNumber * 2);
2084 e = nextValueNumber;
2085 ExprIdx[nextValueNumber++] = nextExprNumber++;
2086 }
2087 return {e, CreateNewValNum};
2088}
2089
2090/// Return whether all the values related with the same \p num are
2091/// defined in \p BB.
2092bool GVNPass::ValueTable::areAllValsInBB(uint32_t Num, const BasicBlock *BB,
2093 GVNPass &Gvn) {
2094 LeaderTableEntry *Vals = &Gvn.LeaderTable[Num];
2095 while (Vals && Vals->BB == BB)
2096 Vals = Vals->Next;
2097 return !Vals;
2098}
2099
2100/// Wrap phiTranslateImpl to provide caching functionality.
2101uint32_t GVNPass::ValueTable::phiTranslate(const BasicBlock *Pred,
2102 const BasicBlock *PhiBlock,
2103 uint32_t Num, GVNPass &Gvn) {
2104 auto FindRes = PhiTranslateTable.find({Num, Pred});
2105 if (FindRes != PhiTranslateTable.end())
2106 return FindRes->second;
2107 uint32_t NewNum = phiTranslateImpl(Pred, PhiBlock, Num, Gvn);
2108 PhiTranslateTable.insert({{Num, Pred}, NewNum});
2109 return NewNum;
2110}
2111
2112// Return true if the value number \p Num and NewNum have equal value.
2113// Return false if the result is unknown.
2114bool GVNPass::ValueTable::areCallValsEqual(uint32_t Num, uint32_t NewNum,
2115 const BasicBlock *Pred,
2116 const BasicBlock *PhiBlock,
2117 GVNPass &Gvn) {
2118 CallInst *Call = nullptr;
2119 LeaderTableEntry *Vals = &Gvn.LeaderTable[Num];
2120 while (Vals) {
2121 Call = dyn_cast<CallInst>(Vals->Val);
2122 if (Call && Call->getParent() == PhiBlock)
2123 break;
2124 Vals = Vals->Next;
2125 }
2126
2127 if (AA->doesNotAccessMemory(Call))
2128 return true;
2129
2130 if (!MD || !AA->onlyReadsMemory(Call))
2131 return false;
2132
2133 MemDepResult local_dep = MD->getDependency(Call);
2134 if (!local_dep.isNonLocal())
2135 return false;
2136
2137 const MemoryDependenceResults::NonLocalDepInfo &deps =
2138 MD->getNonLocalCallDependency(Call);
2139
2140 // Check to see if the Call has no function local clobber.
2141 for (const NonLocalDepEntry &D : deps) {
2142 if (D.getResult().isNonFuncLocal())
2143 return true;
2144 }
2145 return false;
2146}
2147
2148/// Translate value number \p Num using phis, so that it has the values of
2149/// the phis in BB.
2150uint32_t GVNPass::ValueTable::phiTranslateImpl(const BasicBlock *Pred,
2151 const BasicBlock *PhiBlock,
2152 uint32_t Num, GVNPass &Gvn) {
2153 if (PHINode *PN = NumberingPhi[Num]) {
2154 for (unsigned i = 0; i != PN->getNumIncomingValues(); ++i) {
2155 if (PN->getParent() == PhiBlock && PN->getIncomingBlock(i) == Pred)
2156 if (uint32_t TransVal = lookup(PN->getIncomingValue(i), false))
2157 return TransVal;
2158 }
2159 return Num;
2160 }
2161
2162 // If there is any value related with Num is defined in a BB other than
2163 // PhiBlock, it cannot depend on a phi in PhiBlock without going through
2164 // a backedge. We can do an early exit in that case to save compile time.
2165 if (!areAllValsInBB(Num, PhiBlock, Gvn))
2166 return Num;
2167
2168 if (Num >= ExprIdx.size() || ExprIdx[Num] == 0)
2169 return Num;
2170 Expression Exp = Expressions[ExprIdx[Num]];
2171
2172 for (unsigned i = 0; i < Exp.varargs.size(); i++) {
2173 // For InsertValue and ExtractValue, some varargs are index numbers
2174 // instead of value numbers. Those index numbers should not be
2175 // translated.
2176 if ((i > 1 && Exp.opcode == Instruction::InsertValue) ||
2177 (i > 0 && Exp.opcode == Instruction::ExtractValue) ||
2178 (i > 1 && Exp.opcode == Instruction::ShuffleVector))
2179 continue;
2180 Exp.varargs[i] = phiTranslate(Pred, PhiBlock, Exp.varargs[i], Gvn);
2181 }
2182
2183 if (Exp.commutative) {
2184 assert(Exp.varargs.size() >= 2 && "Unsupported commutative instruction!")(static_cast <bool> (Exp.varargs.size() >= 2 &&
"Unsupported commutative instruction!") ? void (0) : __assert_fail
("Exp.varargs.size() >= 2 && \"Unsupported commutative instruction!\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 2184, __extension__ __PRETTY_FUNCTION__
))
;
2185 if (Exp.varargs[0] > Exp.varargs[1]) {
2186 std::swap(Exp.varargs[0], Exp.varargs[1]);
2187 uint32_t Opcode = Exp.opcode >> 8;
2188 if (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp)
2189 Exp.opcode = (Opcode << 8) |
2190 CmpInst::getSwappedPredicate(
2191 static_cast<CmpInst::Predicate>(Exp.opcode & 255));
2192 }
2193 }
2194
2195 if (uint32_t NewNum = expressionNumbering[Exp]) {
2196 if (Exp.opcode == Instruction::Call && NewNum != Num)
2197 return areCallValsEqual(Num, NewNum, Pred, PhiBlock, Gvn) ? NewNum : Num;
2198 return NewNum;
2199 }
2200 return Num;
2201}
2202
2203/// Erase stale entry from phiTranslate cache so phiTranslate can be computed
2204/// again.
2205void GVNPass::ValueTable::eraseTranslateCacheEntry(
2206 uint32_t Num, const BasicBlock &CurrBlock) {
2207 for (const BasicBlock *Pred : predecessors(&CurrBlock))
2208 PhiTranslateTable.erase({Num, Pred});
2209}
2210
2211// In order to find a leader for a given value number at a
2212// specific basic block, we first obtain the list of all Values for that number,
2213// and then scan the list to find one whose block dominates the block in
2214// question. This is fast because dominator tree queries consist of only
2215// a few comparisons of DFS numbers.
2216Value *GVNPass::findLeader(const BasicBlock *BB, uint32_t num) {
2217 LeaderTableEntry Vals = LeaderTable[num];
2218 if (!Vals.Val) return nullptr;
2219
2220 Value *Val = nullptr;
2221 if (DT->dominates(Vals.BB, BB)) {
2222 Val = Vals.Val;
2223 if (isa<Constant>(Val)) return Val;
2224 }
2225
2226 LeaderTableEntry* Next = Vals.Next;
2227 while (Next) {
2228 if (DT->dominates(Next->BB, BB)) {
2229 if (isa<Constant>(Next->Val)) return Next->Val;
2230 if (!Val) Val = Next->Val;
2231 }
2232
2233 Next = Next->Next;
2234 }
2235
2236 return Val;
2237}
2238
2239/// There is an edge from 'Src' to 'Dst'. Return
2240/// true if every path from the entry block to 'Dst' passes via this edge. In
2241/// particular 'Dst' must not be reachable via another edge from 'Src'.
2242static bool isOnlyReachableViaThisEdge(const BasicBlockEdge &E,
2243 DominatorTree *DT) {
2244 // While in theory it is interesting to consider the case in which Dst has
2245 // more than one predecessor, because Dst might be part of a loop which is
2246 // only reachable from Src, in practice it is pointless since at the time
2247 // GVN runs all such loops have preheaders, which means that Dst will have
2248 // been changed to have only one predecessor, namely Src.
2249 const BasicBlock *Pred = E.getEnd()->getSinglePredecessor();
2250 assert((!Pred || Pred == E.getStart()) &&(static_cast <bool> ((!Pred || Pred == E.getStart()) &&
"No edge between these basic blocks!") ? void (0) : __assert_fail
("(!Pred || Pred == E.getStart()) && \"No edge between these basic blocks!\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 2251, __extension__ __PRETTY_FUNCTION__
))
2251 "No edge between these basic blocks!")(static_cast <bool> ((!Pred || Pred == E.getStart()) &&
"No edge between these basic blocks!") ? void (0) : __assert_fail
("(!Pred || Pred == E.getStart()) && \"No edge between these basic blocks!\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 2251, __extension__ __PRETTY_FUNCTION__
))
;
2252 return Pred != nullptr;
2253}
2254
2255void GVNPass::assignBlockRPONumber(Function &F) {
2256 BlockRPONumber.clear();
2257 uint32_t NextBlockNumber = 1;
2258 ReversePostOrderTraversal<Function *> RPOT(&F);
2259 for (BasicBlock *BB : RPOT)
2260 BlockRPONumber[BB] = NextBlockNumber++;
2261 InvalidBlockRPONumbers = false;
2262}
2263
2264bool GVNPass::replaceOperandsForInBlockEquality(Instruction *Instr) const {
2265 bool Changed = false;
2266 for (unsigned OpNum = 0; OpNum < Instr->getNumOperands(); ++OpNum) {
2267 Value *Operand = Instr->getOperand(OpNum);
2268 auto it = ReplaceOperandsWithMap.find(Operand);
2269 if (it != ReplaceOperandsWithMap.end()) {
2270 LLVM_DEBUG(dbgs() << "GVN replacing: " << *Operand << " with "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN replacing: " << *Operand
<< " with " << *it->second << " in instruction "
<< *Instr << '\n'; } } while (false)
2271 << *it->second << " in instruction " << *Instr << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN replacing: " << *Operand
<< " with " << *it->second << " in instruction "
<< *Instr << '\n'; } } while (false)
;
2272 Instr->setOperand(OpNum, it->second);
2273 Changed = true;
2274 }
2275 }
2276 return Changed;
2277}
2278
2279/// The given values are known to be equal in every block
2280/// dominated by 'Root'. Exploit this, for example by replacing 'LHS' with
2281/// 'RHS' everywhere in the scope. Returns whether a change was made.
2282/// If DominatesByEdge is false, then it means that we will propagate the RHS
2283/// value starting from the end of Root.Start.
2284bool GVNPass::propagateEquality(Value *LHS, Value *RHS,
2285 const BasicBlockEdge &Root,
2286 bool DominatesByEdge) {
2287 SmallVector<std::pair<Value*, Value*>, 4> Worklist;
2288 Worklist.push_back(std::make_pair(LHS, RHS));
2289 bool Changed = false;
2290 // For speed, compute a conservative fast approximation to
2291 // DT->dominates(Root, Root.getEnd());
2292 const bool RootDominatesEnd = isOnlyReachableViaThisEdge(Root, DT);
2293
2294 while (!Worklist.empty()) {
2295 std::pair<Value*, Value*> Item = Worklist.pop_back_val();
2296 LHS = Item.first; RHS = Item.second;
2297
2298 if (LHS == RHS)
2299 continue;
2300 assert(LHS->getType() == RHS->getType() && "Equality but unequal types!")(static_cast <bool> (LHS->getType() == RHS->getType
() && "Equality but unequal types!") ? void (0) : __assert_fail
("LHS->getType() == RHS->getType() && \"Equality but unequal types!\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 2300, __extension__ __PRETTY_FUNCTION__
))
;
2301
2302 // Don't try to propagate equalities between constants.
2303 if (isa<Constant>(LHS) && isa<Constant>(RHS))
2304 continue;
2305
2306 // Prefer a constant on the right-hand side, or an Argument if no constants.
2307 if (isa<Constant>(LHS) || (isa<Argument>(LHS) && !isa<Constant>(RHS)))
2308 std::swap(LHS, RHS);
2309 assert((isa<Argument>(LHS) || isa<Instruction>(LHS)) && "Unexpected value!")(static_cast <bool> ((isa<Argument>(LHS) || isa<
Instruction>(LHS)) && "Unexpected value!") ? void (
0) : __assert_fail ("(isa<Argument>(LHS) || isa<Instruction>(LHS)) && \"Unexpected value!\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 2309, __extension__ __PRETTY_FUNCTION__
))
;
2310
2311 // If there is no obvious reason to prefer the left-hand side over the
2312 // right-hand side, ensure the longest lived term is on the right-hand side,
2313 // so the shortest lived term will be replaced by the longest lived.
2314 // This tends to expose more simplifications.
2315 uint32_t LVN = VN.lookupOrAdd(LHS);
2316 if ((isa<Argument>(LHS) && isa<Argument>(RHS)) ||
2317 (isa<Instruction>(LHS) && isa<Instruction>(RHS))) {
2318 // Move the 'oldest' value to the right-hand side, using the value number
2319 // as a proxy for age.
2320 uint32_t RVN = VN.lookupOrAdd(RHS);
2321 if (LVN < RVN) {
2322 std::swap(LHS, RHS);
2323 LVN = RVN;
2324 }
2325 }
2326
2327 // If value numbering later sees that an instruction in the scope is equal
2328 // to 'LHS' then ensure it will be turned into 'RHS'. In order to preserve
2329 // the invariant that instructions only occur in the leader table for their
2330 // own value number (this is used by removeFromLeaderTable), do not do this
2331 // if RHS is an instruction (if an instruction in the scope is morphed into
2332 // LHS then it will be turned into RHS by the next GVN iteration anyway, so
2333 // using the leader table is about compiling faster, not optimizing better).
2334 // The leader table only tracks basic blocks, not edges. Only add to if we
2335 // have the simple case where the edge dominates the end.
2336 if (RootDominatesEnd && !isa<Instruction>(RHS))
2337 addToLeaderTable(LVN, RHS, Root.getEnd());
2338
2339 // Replace all occurrences of 'LHS' with 'RHS' everywhere in the scope. As
2340 // LHS always has at least one use that is not dominated by Root, this will
2341 // never do anything if LHS has only one use.
2342 if (!LHS->hasOneUse()) {
2343 unsigned NumReplacements =
2344 DominatesByEdge
2345 ? replaceDominatedUsesWith(LHS, RHS, *DT, Root)
2346 : replaceDominatedUsesWith(LHS, RHS, *DT, Root.getStart());
2347
2348 Changed |= NumReplacements > 0;
2349 NumGVNEqProp += NumReplacements;
2350 // Cached information for anything that uses LHS will be invalid.
2351 if (MD)
2352 MD->invalidateCachedPointerInfo(LHS);
2353 }
2354
2355 // Now try to deduce additional equalities from this one. For example, if
2356 // the known equality was "(A != B)" == "false" then it follows that A and B
2357 // are equal in the scope. Only boolean equalities with an explicit true or
2358 // false RHS are currently supported.
2359 if (!RHS->getType()->isIntegerTy(1))
2360 // Not a boolean equality - bail out.
2361 continue;
2362 ConstantInt *CI = dyn_cast<ConstantInt>(RHS);
2363 if (!CI)
2364 // RHS neither 'true' nor 'false' - bail out.
2365 continue;
2366 // Whether RHS equals 'true'. Otherwise it equals 'false'.
2367 bool isKnownTrue = CI->isMinusOne();
2368 bool isKnownFalse = !isKnownTrue;
2369
2370 // If "A && B" is known true then both A and B are known true. If "A || B"
2371 // is known false then both A and B are known false.
2372 Value *A, *B;
2373 if ((isKnownTrue && match(LHS, m_LogicalAnd(m_Value(A), m_Value(B)))) ||
2374 (isKnownFalse && match(LHS, m_LogicalOr(m_Value(A), m_Value(B))))) {
2375 Worklist.push_back(std::make_pair(A, RHS));
2376 Worklist.push_back(std::make_pair(B, RHS));
2377 continue;
2378 }
2379
2380 // If we are propagating an equality like "(A == B)" == "true" then also
2381 // propagate the equality A == B. When propagating a comparison such as
2382 // "(A >= B)" == "true", replace all instances of "A < B" with "false".
2383 if (CmpInst *Cmp = dyn_cast<CmpInst>(LHS)) {
2384 Value *Op0 = Cmp->getOperand(0), *Op1 = Cmp->getOperand(1);
2385
2386 // If "A == B" is known true, or "A != B" is known false, then replace
2387 // A with B everywhere in the scope. For floating point operations, we
2388 // have to be careful since equality does not always imply equivalance.
2389 if ((isKnownTrue && impliesEquivalanceIfTrue(Cmp)) ||
2390 (isKnownFalse && impliesEquivalanceIfFalse(Cmp)))
2391 Worklist.push_back(std::make_pair(Op0, Op1));
2392
2393 // If "A >= B" is known true, replace "A < B" with false everywhere.
2394 CmpInst::Predicate NotPred = Cmp->getInversePredicate();
2395 Constant *NotVal = ConstantInt::get(Cmp->getType(), isKnownFalse);
2396 // Since we don't have the instruction "A < B" immediately to hand, work
2397 // out the value number that it would have and use that to find an
2398 // appropriate instruction (if any).
2399 uint32_t NextNum = VN.getNextUnusedValueNumber();
2400 uint32_t Num = VN.lookupOrAddCmp(Cmp->getOpcode(), NotPred, Op0, Op1);
2401 // If the number we were assigned was brand new then there is no point in
2402 // looking for an instruction realizing it: there cannot be one!
2403 if (Num < NextNum) {
2404 Value *NotCmp = findLeader(Root.getEnd(), Num);
2405 if (NotCmp && isa<Instruction>(NotCmp)) {
2406 unsigned NumReplacements =
2407 DominatesByEdge
2408 ? replaceDominatedUsesWith(NotCmp, NotVal, *DT, Root)
2409 : replaceDominatedUsesWith(NotCmp, NotVal, *DT,
2410 Root.getStart());
2411 Changed |= NumReplacements > 0;
2412 NumGVNEqProp += NumReplacements;
2413 // Cached information for anything that uses NotCmp will be invalid.
2414 if (MD)
2415 MD->invalidateCachedPointerInfo(NotCmp);
2416 }
2417 }
2418 // Ensure that any instruction in scope that gets the "A < B" value number
2419 // is replaced with false.
2420 // The leader table only tracks basic blocks, not edges. Only add to if we
2421 // have the simple case where the edge dominates the end.
2422 if (RootDominatesEnd)
2423 addToLeaderTable(Num, NotVal, Root.getEnd());
2424
2425 continue;
2426 }
2427 }
2428
2429 return Changed;
2430}
2431
2432/// When calculating availability, handle an instruction
2433/// by inserting it into the appropriate sets
2434bool GVNPass::processInstruction(Instruction *I) {
2435 // Ignore dbg info intrinsics.
2436 if (isa<DbgInfoIntrinsic>(I))
2437 return false;
2438
2439 // If the instruction can be easily simplified then do so now in preference
2440 // to value numbering it. Value numbering often exposes redundancies, for
2441 // example if it determines that %y is equal to %x then the instruction
2442 // "%z = and i32 %x, %y" becomes "%z = and i32 %x, %x" which we now simplify.
2443 const DataLayout &DL = I->getModule()->getDataLayout();
2444 if (Value *V = simplifyInstruction(I, {DL, TLI, DT, AC})) {
2445 bool Changed = false;
2446 if (!I->use_empty()) {
2447 // Simplification can cause a special instruction to become not special.
2448 // For example, devirtualization to a willreturn function.
2449 ICF->removeUsersOf(I);
2450 I->replaceAllUsesWith(V);
2451 Changed = true;
2452 }
2453 if (isInstructionTriviallyDead(I, TLI)) {
2454 markInstructionForDeletion(I);
2455 Changed = true;
2456 }
2457 if (Changed) {
2458 if (MD && V->getType()->isPtrOrPtrVectorTy())
2459 MD->invalidateCachedPointerInfo(V);
2460 ++NumGVNSimpl;
2461 return true;
2462 }
2463 }
2464
2465 if (auto *Assume = dyn_cast<AssumeInst>(I))
2466 return processAssumeIntrinsic(Assume);
2467
2468 if (LoadInst *Load = dyn_cast<LoadInst>(I)) {
2469 if (processLoad(Load))
2470 return true;
2471
2472 unsigned Num = VN.lookupOrAdd(Load);
2473 addToLeaderTable(Num, Load, Load->getParent());
2474 return false;
2475 }
2476
2477 // For conditional branches, we can perform simple conditional propagation on
2478 // the condition value itself.
2479 if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
2480 if (!BI->isConditional())
2481 return false;
2482
2483 if (isa<Constant>(BI->getCondition()))
2484 return processFoldableCondBr(BI);
2485
2486 Value *BranchCond = BI->getCondition();
2487 BasicBlock *TrueSucc = BI->getSuccessor(0);
2488 BasicBlock *FalseSucc = BI->getSuccessor(1);
2489 // Avoid multiple edges early.
2490 if (TrueSucc == FalseSucc)
2491 return false;
2492
2493 BasicBlock *Parent = BI->getParent();
2494 bool Changed = false;
2495
2496 Value *TrueVal = ConstantInt::getTrue(TrueSucc->getContext());
2497 BasicBlockEdge TrueE(Parent, TrueSucc);
2498 Changed |= propagateEquality(BranchCond, TrueVal, TrueE, true);
2499
2500 Value *FalseVal = ConstantInt::getFalse(FalseSucc->getContext());
2501 BasicBlockEdge FalseE(Parent, FalseSucc);
2502 Changed |= propagateEquality(BranchCond, FalseVal, FalseE, true);
2503
2504 return Changed;
2505 }
2506
2507 // For switches, propagate the case values into the case destinations.
2508 if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
2509 Value *SwitchCond = SI->getCondition();
2510 BasicBlock *Parent = SI->getParent();
2511 bool Changed = false;
2512
2513 // Remember how many outgoing edges there are to every successor.
2514 SmallDenseMap<BasicBlock *, unsigned, 16> SwitchEdges;
2515 for (unsigned i = 0, n = SI->getNumSuccessors(); i != n; ++i)
2516 ++SwitchEdges[SI->getSuccessor(i)];
2517
2518 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
2519 i != e; ++i) {
2520 BasicBlock *Dst = i->getCaseSuccessor();
2521 // If there is only a single edge, propagate the case value into it.
2522 if (SwitchEdges.lookup(Dst) == 1) {
2523 BasicBlockEdge E(Parent, Dst);
2524 Changed |= propagateEquality(SwitchCond, i->getCaseValue(), E, true);
2525 }
2526 }
2527 return Changed;
2528 }
2529
2530 // Instructions with void type don't return a value, so there's
2531 // no point in trying to find redundancies in them.
2532 if (I->getType()->isVoidTy())
2533 return false;
2534
2535 uint32_t NextNum = VN.getNextUnusedValueNumber();
2536 unsigned Num = VN.lookupOrAdd(I);
2537
2538 // Allocations are always uniquely numbered, so we can save time and memory
2539 // by fast failing them.
2540 if (isa<AllocaInst>(I) || I->isTerminator() || isa<PHINode>(I)) {
2541 addToLeaderTable(Num, I, I->getParent());
2542 return false;
2543 }
2544
2545 // If the number we were assigned was a brand new VN, then we don't
2546 // need to do a lookup to see if the number already exists
2547 // somewhere in the domtree: it can't!
2548 if (Num >= NextNum) {
2549 addToLeaderTable(Num, I, I->getParent());
2550 return false;
2551 }
2552
2553 // Perform fast-path value-number based elimination of values inherited from
2554 // dominators.
2555 Value *Repl = findLeader(I->getParent(), Num);
2556 if (!Repl) {
2557 // Failure, just remember this instance for future use.
2558 addToLeaderTable(Num, I, I->getParent());
2559 return false;
2560 } else if (Repl == I) {
2561 // If I was the result of a shortcut PRE, it might already be in the table
2562 // and the best replacement for itself. Nothing to do.
2563 return false;
2564 }
2565
2566 // Remove it!
2567 patchAndReplaceAllUsesWith(I, Repl);
2568 if (MD && Repl->getType()->isPtrOrPtrVectorTy())
2569 MD->invalidateCachedPointerInfo(Repl);
2570 markInstructionForDeletion(I);
2571 return true;
2572}
2573
2574/// runOnFunction - This is the main transformation entry point for a function.
2575bool GVNPass::runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
2576 const TargetLibraryInfo &RunTLI, AAResults &RunAA,
2577 MemoryDependenceResults *RunMD, LoopInfo *LI,
2578 OptimizationRemarkEmitter *RunORE, MemorySSA *MSSA) {
2579 AC = &RunAC;
2580 DT = &RunDT;
2581 VN.setDomTree(DT);
2582 TLI = &RunTLI;
2583 VN.setAliasAnalysis(&RunAA);
2584 MD = RunMD;
2585 ImplicitControlFlowTracking ImplicitCFT;
2586 ICF = &ImplicitCFT;
2587 this->LI = LI;
2588 VN.setMemDep(MD);
2589 ORE = RunORE;
2590 InvalidBlockRPONumbers = true;
2591 MemorySSAUpdater Updater(MSSA);
2592 MSSAU = MSSA ? &Updater : nullptr;
2593
2594 bool Changed = false;
2595 bool ShouldContinue = true;
2596
2597 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
2598 // Merge unconditional branches, allowing PRE to catch more
2599 // optimization opportunities.
2600 for (BasicBlock &BB : llvm::make_early_inc_range(F)) {
2601 bool removedBlock = MergeBlockIntoPredecessor(&BB, &DTU, LI, MSSAU, MD);
2602 if (removedBlock)
2603 ++NumGVNBlocks;
2604
2605 Changed |= removedBlock;
2606 }
2607
2608 unsigned Iteration = 0;
2609 while (ShouldContinue) {
2610 LLVM_DEBUG(dbgs() << "GVN iteration: " << Iteration << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN iteration: " << Iteration
<< "\n"; } } while (false)
;
2611 (void) Iteration;
2612 ShouldContinue = iterateOnFunction(F);
2613 Changed |= ShouldContinue;
2614 ++Iteration;
2615 }
2616
2617 if (isPREEnabled()) {
2618 // Fabricate val-num for dead-code in order to suppress assertion in
2619 // performPRE().
2620 assignValNumForDeadCode();
2621 bool PREChanged = true;
2622 while (PREChanged) {
2623 PREChanged = performPRE(F);
2624 Changed |= PREChanged;
2625 }
2626 }
2627
2628 // FIXME: Should perform GVN again after PRE does something. PRE can move
2629 // computations into blocks where they become fully redundant. Note that
2630 // we can't do this until PRE's critical edge splitting updates memdep.
2631 // Actually, when this happens, we should just fully integrate PRE into GVN.
2632
2633 cleanupGlobalSets();
2634 // Do not cleanup DeadBlocks in cleanupGlobalSets() as it's called for each
2635 // iteration.
2636 DeadBlocks.clear();
2637
2638 if (MSSA && VerifyMemorySSA)
2639 MSSA->verifyMemorySSA();
2640
2641 return Changed;
2642}
2643
2644bool GVNPass::processBlock(BasicBlock *BB) {
2645 // FIXME: Kill off InstrsToErase by doing erasing eagerly in a helper function
2646 // (and incrementing BI before processing an instruction).
2647 assert(InstrsToErase.empty() &&(static_cast <bool> (InstrsToErase.empty() && "We expect InstrsToErase to be empty across iterations"
) ? void (0) : __assert_fail ("InstrsToErase.empty() && \"We expect InstrsToErase to be empty across iterations\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 2648, __extension__ __PRETTY_FUNCTION__
))
2648 "We expect InstrsToErase to be empty across iterations")(static_cast <bool> (InstrsToErase.empty() && "We expect InstrsToErase to be empty across iterations"
) ? void (0) : __assert_fail ("InstrsToErase.empty() && \"We expect InstrsToErase to be empty across iterations\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 2648, __extension__ __PRETTY_FUNCTION__
))
;
2649 if (DeadBlocks.count(BB))
2650 return false;
2651
2652 // Clearing map before every BB because it can be used only for single BB.
2653 ReplaceOperandsWithMap.clear();
2654 bool ChangedFunction = false;
2655
2656 // Since we may not have visited the input blocks of the phis, we can't
2657 // use our normal hash approach for phis. Instead, simply look for
2658 // obvious duplicates. The first pass of GVN will tend to create
2659 // identical phis, and the second or later passes can eliminate them.
2660 ChangedFunction |= EliminateDuplicatePHINodes(BB);
2661
2662 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
2663 BI != BE;) {
2664 if (!ReplaceOperandsWithMap.empty())
2665 ChangedFunction |= replaceOperandsForInBlockEquality(&*BI);
2666 ChangedFunction |= processInstruction(&*BI);
2667
2668 if (InstrsToErase.empty()) {
2669 ++BI;
2670 continue;
2671 }
2672
2673 // If we need some instructions deleted, do it now.
2674 NumGVNInstr += InstrsToErase.size();
2675
2676 // Avoid iterator invalidation.
2677 bool AtStart = BI == BB->begin();
2678 if (!AtStart)
2679 --BI;
2680
2681 for (auto *I : InstrsToErase) {
2682 assert(I->getParent() == BB && "Removing instruction from wrong block?")(static_cast <bool> (I->getParent() == BB &&
"Removing instruction from wrong block?") ? void (0) : __assert_fail
("I->getParent() == BB && \"Removing instruction from wrong block?\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 2682, __extension__ __PRETTY_FUNCTION__
))
;
2683 LLVM_DEBUG(dbgs() << "GVN removed: " << *I << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN removed: " << *I <<
'\n'; } } while (false)
;
2684 salvageKnowledge(I, AC);
2685 salvageDebugInfo(*I);
2686 if (MD) MD->removeInstruction(I);
2687 if (MSSAU)
2688 MSSAU->removeMemoryAccess(I);
2689 LLVM_DEBUG(verifyRemoved(I))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { verifyRemoved(I); } } while (false)
;
2690 ICF->removeInstruction(I);
2691 I->eraseFromParent();
2692 }
2693 InstrsToErase.clear();
2694
2695 if (AtStart)
2696 BI = BB->begin();
2697 else
2698 ++BI;
2699 }
2700
2701 return ChangedFunction;
2702}
2703
2704// Instantiate an expression in a predecessor that lacked it.
2705bool GVNPass::performScalarPREInsertion(Instruction *Instr, BasicBlock *Pred,
2706 BasicBlock *Curr, unsigned int ValNo) {
2707 // Because we are going top-down through the block, all value numbers
2708 // will be available in the predecessor by the time we need them. Any
2709 // that weren't originally present will have been instantiated earlier
2710 // in this loop.
2711 bool success = true;
2712 for (unsigned i = 0, e = Instr->getNumOperands(); i != e; ++i) {
2713 Value *Op = Instr->getOperand(i);
2714 if (isa<Argument>(Op) || isa<Constant>(Op) || isa<GlobalValue>(Op))
2715 continue;
2716 // This could be a newly inserted instruction, in which case, we won't
2717 // find a value number, and should give up before we hurt ourselves.
2718 // FIXME: Rewrite the infrastructure to let it easier to value number
2719 // and process newly inserted instructions.
2720 if (!VN.exists(Op)) {
2721 success = false;
2722 break;
2723 }
2724 uint32_t TValNo =
2725 VN.phiTranslate(Pred, Curr, VN.lookup(Op), *this);
2726 if (Value *V = findLeader(Pred, TValNo)) {
2727 Instr->setOperand(i, V);
2728 } else {
2729 success = false;
2730 break;
2731 }
2732 }
2733
2734 // Fail out if we encounter an operand that is not available in
2735 // the PRE predecessor. This is typically because of loads which
2736 // are not value numbered precisely.
2737 if (!success)
2738 return false;
2739
2740 Instr->insertBefore(Pred->getTerminator());
2741 Instr->setName(Instr->getName() + ".pre");
2742 Instr->setDebugLoc(Instr->getDebugLoc());
2743
2744 ICF->insertInstructionTo(Instr, Pred);
2745
2746 unsigned Num = VN.lookupOrAdd(Instr);
2747 VN.add(Instr, Num);
2748
2749 // Update the availability map to include the new instruction.
2750 addToLeaderTable(Num, Instr, Pred);
2751 return true;
2752}
2753
2754bool GVNPass::performScalarPRE(Instruction *CurInst) {
2755 if (isa<AllocaInst>(CurInst) || CurInst->isTerminator() ||
2756 isa<PHINode>(CurInst) || CurInst->getType()->isVoidTy() ||
2757 CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects() ||
2758 isa<DbgInfoIntrinsic>(CurInst))
2759 return false;
2760
2761 // Don't do PRE on compares. The PHI would prevent CodeGenPrepare from
2762 // sinking the compare again, and it would force the code generator to
2763 // move the i1 from processor flags or predicate registers into a general
2764 // purpose register.
2765 if (isa<CmpInst>(CurInst))
2766 return false;
2767
2768 // Don't do PRE on GEPs. The inserted PHI would prevent CodeGenPrepare from
2769 // sinking the addressing mode computation back to its uses. Extending the
2770 // GEP's live range increases the register pressure, and therefore it can
2771 // introduce unnecessary spills.
2772 //
2773 // This doesn't prevent Load PRE. PHI translation will make the GEP available
2774 // to the load by moving it to the predecessor block if necessary.
2775 if (isa<GetElementPtrInst>(CurInst))
2776 return false;
2777
2778 if (auto *CallB = dyn_cast<CallBase>(CurInst)) {
2779 // We don't currently value number ANY inline asm calls.
2780 if (CallB->isInlineAsm())
2781 return false;
2782 // Don't do PRE on convergent calls.
2783 if (CallB->isConvergent())
2784 return false;
2785 }
2786
2787 uint32_t ValNo = VN.lookup(CurInst);
2788
2789 // Look for the predecessors for PRE opportunities. We're
2790 // only trying to solve the basic diamond case, where
2791 // a value is computed in the successor and one predecessor,
2792 // but not the other. We also explicitly disallow cases
2793 // where the successor is its own predecessor, because they're
2794 // more complicated to get right.
2795 unsigned NumWith = 0;
2796 unsigned NumWithout = 0;
2797 BasicBlock *PREPred = nullptr;
2798 BasicBlock *CurrentBlock = CurInst->getParent();
2799
2800 // Update the RPO numbers for this function.
2801 if (InvalidBlockRPONumbers)
2802 assignBlockRPONumber(*CurrentBlock->getParent());
2803
2804 SmallVector<std::pair<Value *, BasicBlock *>, 8> predMap;
2805 for (BasicBlock *P : predecessors(CurrentBlock)) {
2806 // We're not interested in PRE where blocks with predecessors that are
2807 // not reachable.
2808 if (!DT->isReachableFromEntry(P)) {
2809 NumWithout = 2;
2810 break;
2811 }
2812 // It is not safe to do PRE when P->CurrentBlock is a loop backedge.
2813 assert(BlockRPONumber.count(P) && BlockRPONumber.count(CurrentBlock) &&(static_cast <bool> (BlockRPONumber.count(P) &&
BlockRPONumber.count(CurrentBlock) && "Invalid BlockRPONumber map."
) ? void (0) : __assert_fail ("BlockRPONumber.count(P) && BlockRPONumber.count(CurrentBlock) && \"Invalid BlockRPONumber map.\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 2814, __extension__ __PRETTY_FUNCTION__
))
2814 "Invalid BlockRPONumber map.")(static_cast <bool> (BlockRPONumber.count(P) &&
BlockRPONumber.count(CurrentBlock) && "Invalid BlockRPONumber map."
) ? void (0) : __assert_fail ("BlockRPONumber.count(P) && BlockRPONumber.count(CurrentBlock) && \"Invalid BlockRPONumber map.\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 2814, __extension__ __PRETTY_FUNCTION__
))
;
2815 if (BlockRPONumber[P] >= BlockRPONumber[CurrentBlock]) {
2816 NumWithout = 2;
2817 break;
2818 }
2819
2820 uint32_t TValNo = VN.phiTranslate(P, CurrentBlock, ValNo, *this);
2821 Value *predV = findLeader(P, TValNo);
2822 if (!predV) {
2823 predMap.push_back(std::make_pair(static_cast<Value *>(nullptr), P));
2824 PREPred = P;
2825 ++NumWithout;
2826 } else if (predV == CurInst) {
2827 /* CurInst dominates this predecessor. */
2828 NumWithout = 2;
2829 break;
2830 } else {
2831 predMap.push_back(std::make_pair(predV, P));
2832 ++NumWith;
2833 }
2834 }
2835
2836 // Don't do PRE when it might increase code size, i.e. when
2837 // we would need to insert instructions in more than one pred.
2838 if (NumWithout > 1 || NumWith == 0)
2839 return false;
2840
2841 // We may have a case where all predecessors have the instruction,
2842 // and we just need to insert a phi node. Otherwise, perform
2843 // insertion.
2844 Instruction *PREInstr = nullptr;
2845
2846 if (NumWithout != 0) {
2847 if (!isSafeToSpeculativelyExecute(CurInst)) {
2848 // It is only valid to insert a new instruction if the current instruction
2849 // is always executed. An instruction with implicit control flow could
2850 // prevent us from doing it. If we cannot speculate the execution, then
2851 // PRE should be prohibited.
2852 if (ICF->isDominatedByICFIFromSameBlock(CurInst))
2853 return false;
2854 }
2855
2856 // Don't do PRE across indirect branch.
2857 if (isa<IndirectBrInst>(PREPred->getTerminator()))
2858 return false;
2859
2860 // We can't do PRE safely on a critical edge, so instead we schedule
2861 // the edge to be split and perform the PRE the next time we iterate
2862 // on the function.
2863 unsigned SuccNum = GetSuccessorNumber(PREPred, CurrentBlock);
2864 if (isCriticalEdge(PREPred->getTerminator(), SuccNum)) {
2865 toSplit.push_back(std::make_pair(PREPred->getTerminator(), SuccNum));
2866 return false;
2867 }
2868 // We need to insert somewhere, so let's give it a shot
2869 PREInstr = CurInst->clone();
2870 if (!performScalarPREInsertion(PREInstr, PREPred, CurrentBlock, ValNo)) {
2871 // If we failed insertion, make sure we remove the instruction.
2872 LLVM_DEBUG(verifyRemoved(PREInstr))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { verifyRemoved(PREInstr); } } while (false)
;
2873 PREInstr->deleteValue();
2874 return false;
2875 }
2876 }
2877
2878 // Either we should have filled in the PRE instruction, or we should
2879 // not have needed insertions.
2880 assert(PREInstr != nullptr || NumWithout == 0)(static_cast <bool> (PREInstr != nullptr || NumWithout ==
0) ? void (0) : __assert_fail ("PREInstr != nullptr || NumWithout == 0"
, "llvm/lib/Transforms/Scalar/GVN.cpp", 2880, __extension__ __PRETTY_FUNCTION__
))
;
2881
2882 ++NumGVNPRE;
2883
2884 // Create a PHI to make the value available in this block.
2885 PHINode *Phi =
2886 PHINode::Create(CurInst->getType(), predMap.size(),
2887 CurInst->getName() + ".pre-phi", &CurrentBlock->front());
2888 for (unsigned i = 0, e = predMap.size(); i != e; ++i) {
2889 if (Value *V = predMap[i].first) {
2890 // If we use an existing value in this phi, we have to patch the original
2891 // value because the phi will be used to replace a later value.
2892 patchReplacementInstruction(CurInst, V);
2893 Phi->addIncoming(V, predMap[i].second);
2894 } else
2895 Phi->addIncoming(PREInstr, PREPred);
2896 }
2897
2898 VN.add(Phi, ValNo);
2899 // After creating a new PHI for ValNo, the phi translate result for ValNo will
2900 // be changed, so erase the related stale entries in phi translate cache.
2901 VN.eraseTranslateCacheEntry(ValNo, *CurrentBlock);
2902 addToLeaderTable(ValNo, Phi, CurrentBlock);
2903 Phi->setDebugLoc(CurInst->getDebugLoc());
2904 CurInst->replaceAllUsesWith(Phi);
2905 if (MD && Phi->getType()->isPtrOrPtrVectorTy())
2906 MD->invalidateCachedPointerInfo(Phi);
2907 VN.erase(CurInst);
2908 removeFromLeaderTable(ValNo, CurInst, CurrentBlock);
2909
2910 LLVM_DEBUG(dbgs() << "GVN PRE removed: " << *CurInst << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { dbgs() << "GVN PRE removed: " << *CurInst
<< '\n'; } } while (false)
;
2911 if (MD)
2912 MD->removeInstruction(CurInst);
2913 if (MSSAU)
2914 MSSAU->removeMemoryAccess(CurInst);
2915 LLVM_DEBUG(verifyRemoved(CurInst))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("gvn")) { verifyRemoved(CurInst); } } while (false)
;
2916 // FIXME: Intended to be markInstructionForDeletion(CurInst), but it causes
2917 // some assertion failures.
2918 ICF->removeInstruction(CurInst);
2919 CurInst->eraseFromParent();
2920 ++NumGVNInstr;
2921
2922 return true;
2923}
2924
2925/// Perform a purely local form of PRE that looks for diamond
2926/// control flow patterns and attempts to perform simple PRE at the join point.
2927bool GVNPass::performPRE(Function &F) {
2928 bool Changed = false;
2929 for (BasicBlock *CurrentBlock : depth_first(&F.getEntryBlock())) {
2930 // Nothing to PRE in the entry block.
2931 if (CurrentBlock == &F.getEntryBlock())
2932 continue;
2933
2934 // Don't perform PRE on an EH pad.
2935 if (CurrentBlock->isEHPad())
2936 continue;
2937
2938 for (BasicBlock::iterator BI = CurrentBlock->begin(),
2939 BE = CurrentBlock->end();
2940 BI != BE;) {
2941 Instruction *CurInst = &*BI++;
2942 Changed |= performScalarPRE(CurInst);
2943 }
2944 }
2945
2946 if (splitCriticalEdges())
2947 Changed = true;
2948
2949 return Changed;
2950}
2951
2952/// Split the critical edge connecting the given two blocks, and return
2953/// the block inserted to the critical edge.
2954BasicBlock *GVNPass::splitCriticalEdges(BasicBlock *Pred, BasicBlock *Succ) {
2955 // GVN does not require loop-simplify, do not try to preserve it if it is not
2956 // possible.
2957 BasicBlock *BB = SplitCriticalEdge(
2958 Pred, Succ,
2959 CriticalEdgeSplittingOptions(DT, LI, MSSAU).unsetPreserveLoopSimplify());
2960 if (BB) {
2961 if (MD)
2962 MD->invalidateCachedPredecessors();
2963 InvalidBlockRPONumbers = true;
2964 }
2965 return BB;
2966}
2967
2968/// Split critical edges found during the previous
2969/// iteration that may enable further optimization.
2970bool GVNPass::splitCriticalEdges() {
2971 if (toSplit.empty())
2972 return false;
2973
2974 bool Changed = false;
2975 do {
2976 std::pair<Instruction *, unsigned> Edge = toSplit.pop_back_val();
2977 Changed |= SplitCriticalEdge(Edge.first, Edge.second,
2978 CriticalEdgeSplittingOptions(DT, LI, MSSAU)) !=
2979 nullptr;
2980 } while (!toSplit.empty());
2981 if (Changed) {
2982 if (MD)
2983 MD->invalidateCachedPredecessors();
2984 InvalidBlockRPONumbers = true;
2985 }
2986 return Changed;
2987}
2988
2989/// Executes one iteration of GVN
2990bool GVNPass::iterateOnFunction(Function &F) {
2991 cleanupGlobalSets();
2992
2993 // Top-down walk of the dominator tree
2994 bool Changed = false;
2995 // Needed for value numbering with phi construction to work.
2996 // RPOT walks the graph in its constructor and will not be invalidated during
2997 // processBlock.
2998 ReversePostOrderTraversal<Function *> RPOT(&F);
2999
3000 for (BasicBlock *BB : RPOT)
3001 Changed |= processBlock(BB);
3002
3003 return Changed;
3004}
3005
3006void GVNPass::cleanupGlobalSets() {
3007 VN.clear();
3008 LeaderTable.clear();
3009 BlockRPONumber.clear();
3010 TableAllocator.Reset();
3011 ICF->clear();
3012 InvalidBlockRPONumbers = true;
3013}
3014
3015/// Verify that the specified instruction does not occur in our
3016/// internal data structures.
3017void GVNPass::verifyRemoved(const Instruction *Inst) const {
3018 VN.verifyRemoved(Inst);
3019
3020 // Walk through the value number scope to make sure the instruction isn't
3021 // ferreted away in it.
3022 for (const auto &I : LeaderTable) {
3023 const LeaderTableEntry *Node = &I.second;
3024 assert(Node->Val != Inst && "Inst still in value numbering scope!")(static_cast <bool> (Node->Val != Inst && "Inst still in value numbering scope!"
) ? void (0) : __assert_fail ("Node->Val != Inst && \"Inst still in value numbering scope!\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 3024, __extension__ __PRETTY_FUNCTION__
))
;
3025
3026 while (Node->Next) {
3027 Node = Node->Next;
3028 assert(Node->Val != Inst && "Inst still in value numbering scope!")(static_cast <bool> (Node->Val != Inst && "Inst still in value numbering scope!"
) ? void (0) : __assert_fail ("Node->Val != Inst && \"Inst still in value numbering scope!\""
, "llvm/lib/Transforms/Scalar/GVN.cpp", 3028, __extension__ __PRETTY_FUNCTION__
))
;
3029 }
3030 }
3031}
3032
3033/// BB is declared dead, which implied other blocks become dead as well. This
3034/// function is to add all these blocks to "DeadBlocks". For the dead blocks'
3035/// live successors, update their phi nodes by replacing the operands
3036/// corresponding to dead blocks with UndefVal.
3037void GVNPass::addDeadBlock(BasicBlock *BB) {
3038 SmallVector<BasicBlock *, 4> NewDead;
3039 SmallSetVector<BasicBlock *, 4> DF;
3040
3041 NewDead.push_back(BB);
3042 while (!NewDead.empty()) {
3043 BasicBlock *D = NewDead.pop_back_val();
3044 if (DeadBlocks.count(D))
3045 continue;
3046
3047 // All blocks dominated by D are dead.
3048 SmallVector<BasicBlock *, 8> Dom;
3049 DT->getDescendants(D, Dom);
3050 DeadBlocks.insert(Dom.begin(), Dom.end());
3051
3052 // Figure out the dominance-frontier(D).
3053 for (BasicBlock *B : Dom) {
3054 for (BasicBlock *S : successors(B)) {
3055 if (DeadBlocks.count(S))
3056 continue;
3057
3058 bool AllPredDead = true;
3059 for (BasicBlock *P : predecessors(S))
3060 if (!DeadBlocks.count(P)) {
3061 AllPredDead = false;
3062 break;
3063 }
3064
3065 if (!AllPredDead) {
3066 // S could be proved dead later on. That is why we don't update phi
3067 // operands at this moment.
3068 DF.insert(S);
3069 } else {
3070 // While S is not dominated by D, it is dead by now. This could take
3071 // place if S already have a dead predecessor before D is declared
3072 // dead.
3073 NewDead.push_back(S);
3074 }
3075 }
3076 }
3077 }
3078
3079 // For the dead blocks' live successors, update their phi nodes by replacing
3080 // the operands corresponding to dead blocks with UndefVal.
3081 for (BasicBlock *B : DF) {
3082 if (DeadBlocks.count(B))
3083 continue;
3084
3085 // First, split the critical edges. This might also create additional blocks
3086 // to preserve LoopSimplify form and adjust edges accordingly.
3087 SmallVector<BasicBlock *, 4> Preds(predecessors(B));
3088 for (BasicBlock *P : Preds) {
3089 if (!DeadBlocks.count(P))
3090 continue;
3091
3092 if (llvm::is_contained(successors(P), B) &&
3093 isCriticalEdge(P->getTerminator(), B)) {
3094 if (BasicBlock *S = splitCriticalEdges(P, B))
3095 DeadBlocks.insert(P = S);
Although the value stored to 'P' is used in the enclosing expression, the value is never actually read from 'P'
3096 }
3097 }
3098
3099 // Now poison the incoming values from the dead predecessors.
3100 for (BasicBlock *P : predecessors(B)) {
3101 if (!DeadBlocks.count(P))
3102 continue;
3103 for (PHINode &Phi : B->phis()) {
3104 Phi.setIncomingValueForBlock(P, PoisonValue::get(Phi.getType()));
3105 if (MD)
3106 MD->invalidateCachedPointerInfo(&Phi);
3107 }
3108 }
3109 }
3110}
3111
3112// If the given branch is recognized as a foldable branch (i.e. conditional
3113// branch with constant condition), it will perform following analyses and
3114// transformation.
3115// 1) If the dead out-coming edge is a critical-edge, split it. Let
3116// R be the target of the dead out-coming edge.
3117// 1) Identify the set of dead blocks implied by the branch's dead outcoming
3118// edge. The result of this step will be {X| X is dominated by R}
3119// 2) Identify those blocks which haves at least one dead predecessor. The
3120// result of this step will be dominance-frontier(R).
3121// 3) Update the PHIs in DF(R) by replacing the operands corresponding to
3122// dead blocks with "UndefVal" in an hope these PHIs will optimized away.
3123//
3124// Return true iff *NEW* dead code are found.
3125bool GVNPass::processFoldableCondBr(BranchInst *BI) {
3126 if (!BI || BI->isUnconditional())
3127 return false;
3128
3129 // If a branch has two identical successors, we cannot declare either dead.
3130 if (BI->getSuccessor(0) == BI->getSuccessor(1))
3131 return false;
3132
3133 ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
3134 if (!Cond)
3135 return false;
3136
3137 BasicBlock *DeadRoot =
3138 Cond->getZExtValue() ? BI->getSuccessor(1) : BI->getSuccessor(0);
3139 if (DeadBlocks.count(DeadRoot))
3140 return false;
3141
3142 if (!DeadRoot->getSinglePredecessor())
3143 DeadRoot = splitCriticalEdges(BI->getParent(), DeadRoot);
3144
3145 addDeadBlock(DeadRoot);
3146 return true;
3147}
3148
3149// performPRE() will trigger assert if it comes across an instruction without
3150// associated val-num. As it normally has far more live instructions than dead
3151// instructions, it makes more sense just to "fabricate" a val-number for the
3152// dead code than checking if instruction involved is dead or not.
3153void GVNPass::assignValNumForDeadCode() {
3154 for (BasicBlock *BB : DeadBlocks) {
3155 for (Instruction &Inst : *BB) {
3156 unsigned ValNum = VN.lookupOrAdd(&Inst);
3157 addToLeaderTable(ValNum, &Inst, BB);
3158 }
3159 }
3160}
3161
3162class llvm::gvn::GVNLegacyPass : public FunctionPass {
3163public:
3164 static char ID; // Pass identification, replacement for typeid
3165
3166 explicit GVNLegacyPass(bool NoMemDepAnalysis = !GVNEnableMemDep)
3167 : FunctionPass(ID), Impl(GVNOptions().setMemDep(!NoMemDepAnalysis)) {
3168 initializeGVNLegacyPassPass(*PassRegistry::getPassRegistry());
3169 }
3170
3171 bool runOnFunction(Function &F) override {
3172 if (skipFunction(F))
3173 return false;
3174
3175 auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
3176
3177 auto *MSSAWP = getAnalysisIfAvailable<MemorySSAWrapperPass>();
3178 return Impl.runImpl(
3179 F, getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
3180 getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
3181 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F),
3182 getAnalysis<AAResultsWrapperPass>().getAAResults(),
3183 Impl.isMemDepEnabled()
3184 ? &getAnalysis<MemoryDependenceWrapperPass>().getMemDep()
3185 : nullptr,
3186 LIWP ? &LIWP->getLoopInfo() : nullptr,
3187 &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(),
3188 MSSAWP ? &MSSAWP->getMSSA() : nullptr);
3189 }
3190
3191 void getAnalysisUsage(AnalysisUsage &AU) const override {
3192 AU.addRequired<AssumptionCacheTracker>();
3193 AU.addRequired<DominatorTreeWrapperPass>();
3194 AU.addRequired<TargetLibraryInfoWrapperPass>();
3195 AU.addRequired<LoopInfoWrapperPass>();
3196 if (Impl.isMemDepEnabled())
3197 AU.addRequired<MemoryDependenceWrapperPass>();
3198 AU.addRequired<AAResultsWrapperPass>();
3199 AU.addPreserved<DominatorTreeWrapperPass>();
3200 AU.addPreserved<GlobalsAAWrapperPass>();
3201 AU.addPreserved<TargetLibraryInfoWrapperPass>();
3202 AU.addPreserved<LoopInfoWrapperPass>();
3203 AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
3204 AU.addPreserved<MemorySSAWrapperPass>();
3205 }
3206
3207private:
3208 GVNPass Impl;
3209};
3210
3211char GVNLegacyPass::ID = 0;
3212
3213INITIALIZE_PASS_BEGIN(GVNLegacyPass, "gvn", "Global Value Numbering", false, false)static void *initializeGVNLegacyPassPassOnce(PassRegistry &
Registry) {
3214INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)initializeAssumptionCacheTrackerPass(Registry);
3215INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)initializeMemoryDependenceWrapperPassPass(Registry);
3216INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)initializeDominatorTreeWrapperPassPass(Registry);
3217INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)initializeTargetLibraryInfoWrapperPassPass(Registry);
3218INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)initializeAAResultsWrapperPassPass(Registry);
3219INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)initializeGlobalsAAWrapperPassPass(Registry);
3220INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)initializeOptimizationRemarkEmitterWrapperPassPass(Registry);
3221INITIALIZE_PASS_END(GVNLegacyPass, "gvn", "Global Value Numbering", false, false)PassInfo *PI = new PassInfo( "Global Value Numbering", "gvn",
&GVNLegacyPass::ID, PassInfo::NormalCtor_t(callDefaultCtor
<GVNLegacyPass>), false, false); Registry.registerPass(
*PI, true); return PI; } static llvm::once_flag InitializeGVNLegacyPassPassFlag
; void llvm::initializeGVNLegacyPassPass(PassRegistry &Registry
) { llvm::call_once(InitializeGVNLegacyPassPassFlag, initializeGVNLegacyPassPassOnce
, std::ref(Registry)); }
3222
3223// The public interface to this file...
3224FunctionPass *llvm::createGVNPass(bool NoMemDepAnalysis) {
3225 return new GVNLegacyPass(NoMemDepAnalysis);
3226}