Bug Summary

File:llvm/lib/Transforms/Scalar/LICM.cpp
Warning:line 1266, column 41
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name LICM.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -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 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/build-llvm/lib/Transforms/Scalar -resource-dir /usr/lib/llvm-13/lib/clang/13.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/build-llvm/lib/Transforms/Scalar -I /build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar -I /build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/build-llvm/include -I /build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include -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-13/lib/clang/13.0.0/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 -O2 -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 -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/build-llvm/lib/Transforms/Scalar -fdebug-prefix-map=/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -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-2021-05-07-005843-9350-1 -x c++ /build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp

/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp

1//===-- LICM.cpp - Loop Invariant Code Motion Pass ------------------------===//
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 loop invariant code motion, attempting to remove as much
10// code from the body of a loop as possible. It does this by either hoisting
11// code into the preheader block, or by sinking code to the exit blocks if it is
12// safe. This pass also promotes must-aliased memory locations in the loop to
13// live in registers, thus hoisting and sinking "invariant" loads and stores.
14//
15// Hoisting operations out of loops is a canonicalization transform. It
16// enables and simplifies subsequent optimizations in the middle-end.
17// Rematerialization of hoisted instructions to reduce register pressure is the
18// responsibility of the back-end, which has more accurate information about
19// register pressure and also handles other optimizations than LICM that
20// increase live-ranges.
21//
22// This pass uses alias analysis for two purposes:
23//
24// 1. Moving loop invariant loads and calls out of loops. If we can determine
25// that a load or call inside of a loop never aliases anything stored to,
26// we can hoist it or sink it like any other instruction.
27// 2. Scalar Promotion of Memory - If there is a store instruction inside of
28// the loop, we try to move the store to happen AFTER the loop instead of
29// inside of the loop. This can only happen if a few conditions are true:
30// A. The pointer stored through is loop invariant
31// B. There are no stores or loads in the loop which _may_ alias the
32// pointer. There are no calls in the loop which mod/ref the pointer.
33// If these conditions are true, we can promote the loads and stores in the
34// loop of the pointer to use a temporary alloca'd variable. We then use
35// the SSAUpdater to construct the appropriate SSA form for the value.
36//
37//===----------------------------------------------------------------------===//
38
39#include "llvm/Transforms/Scalar/LICM.h"
40#include "llvm/ADT/SetOperations.h"
41#include "llvm/ADT/Statistic.h"
42#include "llvm/Analysis/AliasAnalysis.h"
43#include "llvm/Analysis/AliasSetTracker.h"
44#include "llvm/Analysis/BasicAliasAnalysis.h"
45#include "llvm/Analysis/BlockFrequencyInfo.h"
46#include "llvm/Analysis/CaptureTracking.h"
47#include "llvm/Analysis/ConstantFolding.h"
48#include "llvm/Analysis/GlobalsModRef.h"
49#include "llvm/Analysis/GuardUtils.h"
50#include "llvm/Analysis/LazyBlockFrequencyInfo.h"
51#include "llvm/Analysis/Loads.h"
52#include "llvm/Analysis/LoopInfo.h"
53#include "llvm/Analysis/LoopIterator.h"
54#include "llvm/Analysis/LoopPass.h"
55#include "llvm/Analysis/MemoryBuiltins.h"
56#include "llvm/Analysis/MemorySSA.h"
57#include "llvm/Analysis/MemorySSAUpdater.h"
58#include "llvm/Analysis/MustExecute.h"
59#include "llvm/Analysis/OptimizationRemarkEmitter.h"
60#include "llvm/Analysis/ScalarEvolution.h"
61#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
62#include "llvm/Analysis/TargetLibraryInfo.h"
63#include "llvm/Analysis/ValueTracking.h"
64#include "llvm/IR/CFG.h"
65#include "llvm/IR/Constants.h"
66#include "llvm/IR/DataLayout.h"
67#include "llvm/IR/DebugInfoMetadata.h"
68#include "llvm/IR/DerivedTypes.h"
69#include "llvm/IR/Dominators.h"
70#include "llvm/IR/Instructions.h"
71#include "llvm/IR/IntrinsicInst.h"
72#include "llvm/IR/LLVMContext.h"
73#include "llvm/IR/Metadata.h"
74#include "llvm/IR/PatternMatch.h"
75#include "llvm/IR/PredIteratorCache.h"
76#include "llvm/InitializePasses.h"
77#include "llvm/Support/CommandLine.h"
78#include "llvm/Support/Debug.h"
79#include "llvm/Support/raw_ostream.h"
80#include "llvm/Transforms/Scalar.h"
81#include "llvm/Transforms/Scalar/LoopPassManager.h"
82#include "llvm/Transforms/Utils/AssumeBundleBuilder.h"
83#include "llvm/Transforms/Utils/BasicBlockUtils.h"
84#include "llvm/Transforms/Utils/Local.h"
85#include "llvm/Transforms/Utils/LoopUtils.h"
86#include "llvm/Transforms/Utils/SSAUpdater.h"
87#include <algorithm>
88#include <utility>
89using namespace llvm;
90
91#define DEBUG_TYPE"licm" "licm"
92
93STATISTIC(NumCreatedBlocks, "Number of blocks created")static llvm::Statistic NumCreatedBlocks = {"licm", "NumCreatedBlocks"
, "Number of blocks created"}
;
94STATISTIC(NumClonedBranches, "Number of branches cloned")static llvm::Statistic NumClonedBranches = {"licm", "NumClonedBranches"
, "Number of branches cloned"}
;
95STATISTIC(NumSunk, "Number of instructions sunk out of loop")static llvm::Statistic NumSunk = {"licm", "NumSunk", "Number of instructions sunk out of loop"
}
;
96STATISTIC(NumHoisted, "Number of instructions hoisted out of loop")static llvm::Statistic NumHoisted = {"licm", "NumHoisted", "Number of instructions hoisted out of loop"
}
;
97STATISTIC(NumMovedLoads, "Number of load insts hoisted or sunk")static llvm::Statistic NumMovedLoads = {"licm", "NumMovedLoads"
, "Number of load insts hoisted or sunk"}
;
98STATISTIC(NumMovedCalls, "Number of call insts hoisted or sunk")static llvm::Statistic NumMovedCalls = {"licm", "NumMovedCalls"
, "Number of call insts hoisted or sunk"}
;
99STATISTIC(NumPromoted, "Number of memory locations promoted to registers")static llvm::Statistic NumPromoted = {"licm", "NumPromoted", "Number of memory locations promoted to registers"
}
;
100
101/// Memory promotion is enabled by default.
102static cl::opt<bool>
103 DisablePromotion("disable-licm-promotion", cl::Hidden, cl::init(false),
104 cl::desc("Disable memory promotion in LICM pass"));
105
106static cl::opt<bool> ControlFlowHoisting(
107 "licm-control-flow-hoisting", cl::Hidden, cl::init(false),
108 cl::desc("Enable control flow (and PHI) hoisting in LICM"));
109
110static cl::opt<unsigned> HoistSinkColdnessThreshold(
111 "licm-coldness-threshold", cl::Hidden, cl::init(4),
112 cl::desc("Relative coldness Threshold of hoisting/sinking destination "
113 "block for LICM to be considered beneficial"));
114
115static cl::opt<uint32_t> MaxNumUsesTraversed(
116 "licm-max-num-uses-traversed", cl::Hidden, cl::init(8),
117 cl::desc("Max num uses visited for identifying load "
118 "invariance in loop using invariant start (default = 8)"));
119
120// Default value of zero implies we use the regular alias set tracker mechanism
121// instead of the cross product using AA to identify aliasing of the memory
122// location we are interested in.
123static cl::opt<int>
124LICMN2Theshold("licm-n2-threshold", cl::Hidden, cl::init(0),
125 cl::desc("How many instruction to cross product using AA"));
126
127// Experimental option to allow imprecision in LICM in pathological cases, in
128// exchange for faster compile. This is to be removed if MemorySSA starts to
129// address the same issue. This flag applies only when LICM uses MemorySSA
130// instead on AliasSetTracker. LICM calls MemorySSAWalker's
131// getClobberingMemoryAccess, up to the value of the Cap, getting perfect
132// accuracy. Afterwards, LICM will call into MemorySSA's getDefiningAccess,
133// which may not be precise, since optimizeUses is capped. The result is
134// correct, but we may not get as "far up" as possible to get which access is
135// clobbering the one queried.
136cl::opt<unsigned> llvm::SetLicmMssaOptCap(
137 "licm-mssa-optimization-cap", cl::init(100), cl::Hidden,
138 cl::desc("Enable imprecision in LICM in pathological cases, in exchange "
139 "for faster compile. Caps the MemorySSA clobbering calls."));
140
141// Experimentally, memory promotion carries less importance than sinking and
142// hoisting. Limit when we do promotion when using MemorySSA, in order to save
143// compile time.
144cl::opt<unsigned> llvm::SetLicmMssaNoAccForPromotionCap(
145 "licm-mssa-max-acc-promotion", cl::init(250), cl::Hidden,
146 cl::desc("[LICM & MemorySSA] When MSSA in LICM is disabled, this has no "
147 "effect. When MSSA in LICM is enabled, then this is the maximum "
148 "number of accesses allowed to be present in a loop in order to "
149 "enable memory promotion."));
150
151static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI);
152static bool isNotUsedOrFreeInLoop(const Instruction &I, const Loop *CurLoop,
153 const LoopSafetyInfo *SafetyInfo,
154 TargetTransformInfo *TTI, bool &FreeInLoop);
155static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop,
156 BasicBlock *Dest, ICFLoopSafetyInfo *SafetyInfo,
157 MemorySSAUpdater *MSSAU, ScalarEvolution *SE,
158 OptimizationRemarkEmitter *ORE);
159static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT,
160 BlockFrequencyInfo *BFI, const Loop *CurLoop,
161 ICFLoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU,
162 OptimizationRemarkEmitter *ORE);
163static bool isSafeToExecuteUnconditionally(Instruction &Inst,
164 const DominatorTree *DT,
165 const TargetLibraryInfo *TLI,
166 const Loop *CurLoop,
167 const LoopSafetyInfo *SafetyInfo,
168 OptimizationRemarkEmitter *ORE,
169 const Instruction *CtxI = nullptr);
170static bool pointerInvalidatedByLoop(MemoryLocation MemLoc,
171 AliasSetTracker *CurAST, Loop *CurLoop,
172 AAResults *AA);
173static bool pointerInvalidatedByLoopWithMSSA(MemorySSA *MSSA, MemoryUse *MU,
174 Loop *CurLoop, Instruction &I,
175 SinkAndHoistLICMFlags &Flags);
176static bool pointerInvalidatedByBlockWithMSSA(BasicBlock &BB, MemorySSA &MSSA,
177 MemoryUse &MU);
178static Instruction *cloneInstructionInExitBlock(
179 Instruction &I, BasicBlock &ExitBlock, PHINode &PN, const LoopInfo *LI,
180 const LoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU);
181
182static void eraseInstruction(Instruction &I, ICFLoopSafetyInfo &SafetyInfo,
183 AliasSetTracker *AST, MemorySSAUpdater *MSSAU);
184
185static void moveInstructionBefore(Instruction &I, Instruction &Dest,
186 ICFLoopSafetyInfo &SafetyInfo,
187 MemorySSAUpdater *MSSAU, ScalarEvolution *SE);
188
189static void foreachMemoryAccess(MemorySSA *MSSA, Loop *L,
190 function_ref<void(Instruction *)> Fn);
191static SmallVector<SmallSetVector<Value *, 8>, 0>
192collectPromotionCandidates(MemorySSA *MSSA, AliasAnalysis *AA, Loop *L,
193 SmallVectorImpl<Instruction *> &MaybePromotable);
194
195namespace {
196struct LoopInvariantCodeMotion {
197 bool runOnLoop(Loop *L, AAResults *AA, LoopInfo *LI, DominatorTree *DT,
198 BlockFrequencyInfo *BFI, TargetLibraryInfo *TLI,
199 TargetTransformInfo *TTI, ScalarEvolution *SE, MemorySSA *MSSA,
200 OptimizationRemarkEmitter *ORE);
201
202 LoopInvariantCodeMotion(unsigned LicmMssaOptCap,
203 unsigned LicmMssaNoAccForPromotionCap)
204 : LicmMssaOptCap(LicmMssaOptCap),
205 LicmMssaNoAccForPromotionCap(LicmMssaNoAccForPromotionCap) {}
206
207private:
208 unsigned LicmMssaOptCap;
209 unsigned LicmMssaNoAccForPromotionCap;
210
211 std::unique_ptr<AliasSetTracker>
212 collectAliasInfoForLoop(Loop *L, LoopInfo *LI, AAResults *AA);
213};
214
215struct LegacyLICMPass : public LoopPass {
216 static char ID; // Pass identification, replacement for typeid
217 LegacyLICMPass(
218 unsigned LicmMssaOptCap = SetLicmMssaOptCap,
219 unsigned LicmMssaNoAccForPromotionCap = SetLicmMssaNoAccForPromotionCap)
220 : LoopPass(ID), LICM(LicmMssaOptCap, LicmMssaNoAccForPromotionCap) {
221 initializeLegacyLICMPassPass(*PassRegistry::getPassRegistry());
222 }
223
224 bool runOnLoop(Loop *L, LPPassManager &LPM) override {
225 if (skipLoop(L))
226 return false;
227
228 LLVM_DEBUG(dbgs() << "Perform LICM on Loop with header at block "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "Perform LICM on Loop with header at block "
<< L->getHeader()->getNameOrAsOperand() <<
"\n"; } } while (false)
229 << L->getHeader()->getNameOrAsOperand() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "Perform LICM on Loop with header at block "
<< L->getHeader()->getNameOrAsOperand() <<
"\n"; } } while (false)
;
230
231 auto *SE = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
232 MemorySSA *MSSA = EnableMSSALoopDependency
233 ? (&getAnalysis<MemorySSAWrapperPass>().getMSSA())
234 : nullptr;
235 bool hasProfileData = L->getHeader()->getParent()->hasProfileData();
236 BlockFrequencyInfo *BFI =
237 hasProfileData ? &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI()
238 : nullptr;
239 // For the old PM, we can't use OptimizationRemarkEmitter as an analysis
240 // pass. Function analyses need to be preserved across loop transformations
241 // but ORE cannot be preserved (see comment before the pass definition).
242 OptimizationRemarkEmitter ORE(L->getHeader()->getParent());
243 return LICM.runOnLoop(
244 L, &getAnalysis<AAResultsWrapperPass>().getAAResults(),
245 &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(),
246 &getAnalysis<DominatorTreeWrapperPass>().getDomTree(), BFI,
247 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(
248 *L->getHeader()->getParent()),
249 &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
250 *L->getHeader()->getParent()),
251 SE ? &SE->getSE() : nullptr, MSSA, &ORE);
252 }
253
254 /// This transformation requires natural loop information & requires that
255 /// loop preheaders be inserted into the CFG...
256 ///
257 void getAnalysisUsage(AnalysisUsage &AU) const override {
258 AU.addPreserved<DominatorTreeWrapperPass>();
259 AU.addPreserved<LoopInfoWrapperPass>();
260 AU.addRequired<TargetLibraryInfoWrapperPass>();
261 if (EnableMSSALoopDependency) {
262 AU.addRequired<MemorySSAWrapperPass>();
263 AU.addPreserved<MemorySSAWrapperPass>();
264 }
265 AU.addRequired<TargetTransformInfoWrapperPass>();
266 getLoopAnalysisUsage(AU);
267 LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU);
268 AU.addPreserved<LazyBlockFrequencyInfoPass>();
269 AU.addPreserved<LazyBranchProbabilityInfoPass>();
270 }
271
272private:
273 LoopInvariantCodeMotion LICM;
274};
275} // namespace
276
277PreservedAnalyses LICMPass::run(Loop &L, LoopAnalysisManager &AM,
278 LoopStandardAnalysisResults &AR, LPMUpdater &) {
279 // For the new PM, we also can't use OptimizationRemarkEmitter as an analysis
280 // pass. Function analyses need to be preserved across loop transformations
281 // but ORE cannot be preserved (see comment before the pass definition).
282 OptimizationRemarkEmitter ORE(L.getHeader()->getParent());
283
284 LoopInvariantCodeMotion LICM(LicmMssaOptCap, LicmMssaNoAccForPromotionCap);
285 if (!LICM.runOnLoop(&L, &AR.AA, &AR.LI, &AR.DT, AR.BFI, &AR.TLI, &AR.TTI,
286 &AR.SE, AR.MSSA, &ORE))
287 return PreservedAnalyses::all();
288
289 auto PA = getLoopPassPreservedAnalyses();
290
291 PA.preserve<DominatorTreeAnalysis>();
292 PA.preserve<LoopAnalysis>();
293 if (AR.MSSA)
294 PA.preserve<MemorySSAAnalysis>();
295
296 return PA;
297}
298
299char LegacyLICMPass::ID = 0;
300INITIALIZE_PASS_BEGIN(LegacyLICMPass, "licm", "Loop Invariant Code Motion",static void *initializeLegacyLICMPassPassOnce(PassRegistry &
Registry) {
301 false, false)static void *initializeLegacyLICMPassPassOnce(PassRegistry &
Registry) {
302INITIALIZE_PASS_DEPENDENCY(LoopPass)initializeLoopPassPass(Registry);
303INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)initializeTargetLibraryInfoWrapperPassPass(Registry);
304INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)initializeTargetTransformInfoWrapperPassPass(Registry);
305INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)initializeMemorySSAWrapperPassPass(Registry);
306INITIALIZE_PASS_DEPENDENCY(LazyBFIPass)initializeLazyBFIPassPass(Registry);
307INITIALIZE_PASS_END(LegacyLICMPass, "licm", "Loop Invariant Code Motion", false,PassInfo *PI = new PassInfo( "Loop Invariant Code Motion", "licm"
, &LegacyLICMPass::ID, PassInfo::NormalCtor_t(callDefaultCtor
<LegacyLICMPass>), false, false); Registry.registerPass
(*PI, true); return PI; } static llvm::once_flag InitializeLegacyLICMPassPassFlag
; void llvm::initializeLegacyLICMPassPass(PassRegistry &Registry
) { llvm::call_once(InitializeLegacyLICMPassPassFlag, initializeLegacyLICMPassPassOnce
, std::ref(Registry)); }
308 false)PassInfo *PI = new PassInfo( "Loop Invariant Code Motion", "licm"
, &LegacyLICMPass::ID, PassInfo::NormalCtor_t(callDefaultCtor
<LegacyLICMPass>), false, false); Registry.registerPass
(*PI, true); return PI; } static llvm::once_flag InitializeLegacyLICMPassPassFlag
; void llvm::initializeLegacyLICMPassPass(PassRegistry &Registry
) { llvm::call_once(InitializeLegacyLICMPassPassFlag, initializeLegacyLICMPassPassOnce
, std::ref(Registry)); }
309
310Pass *llvm::createLICMPass() { return new LegacyLICMPass(); }
311Pass *llvm::createLICMPass(unsigned LicmMssaOptCap,
312 unsigned LicmMssaNoAccForPromotionCap) {
313 return new LegacyLICMPass(LicmMssaOptCap, LicmMssaNoAccForPromotionCap);
314}
315
316llvm::SinkAndHoistLICMFlags::SinkAndHoistLICMFlags(bool IsSink, Loop *L,
317 MemorySSA *MSSA)
318 : SinkAndHoistLICMFlags(SetLicmMssaOptCap, SetLicmMssaNoAccForPromotionCap,
319 IsSink, L, MSSA) {}
320
321llvm::SinkAndHoistLICMFlags::SinkAndHoistLICMFlags(
322 unsigned LicmMssaOptCap, unsigned LicmMssaNoAccForPromotionCap, bool IsSink,
323 Loop *L, MemorySSA *MSSA)
324 : LicmMssaOptCap(LicmMssaOptCap),
325 LicmMssaNoAccForPromotionCap(LicmMssaNoAccForPromotionCap),
326 IsSink(IsSink) {
327 assert(((L != nullptr) == (MSSA != nullptr)) &&(static_cast <bool> (((L != nullptr) == (MSSA != nullptr
)) && "Unexpected values for SinkAndHoistLICMFlags") ?
void (0) : __assert_fail ("((L != nullptr) == (MSSA != nullptr)) && \"Unexpected values for SinkAndHoistLICMFlags\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 328, __extension__ __PRETTY_FUNCTION__))
328 "Unexpected values for SinkAndHoistLICMFlags")(static_cast <bool> (((L != nullptr) == (MSSA != nullptr
)) && "Unexpected values for SinkAndHoistLICMFlags") ?
void (0) : __assert_fail ("((L != nullptr) == (MSSA != nullptr)) && \"Unexpected values for SinkAndHoistLICMFlags\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 328, __extension__ __PRETTY_FUNCTION__))
;
329 if (!MSSA)
330 return;
331
332 unsigned AccessCapCount = 0;
333 for (auto *BB : L->getBlocks())
334 if (const auto *Accesses = MSSA->getBlockAccesses(BB))
335 for (const auto &MA : *Accesses) {
336 (void)MA;
337 ++AccessCapCount;
338 if (AccessCapCount > LicmMssaNoAccForPromotionCap) {
339 NoOfMemAccTooLarge = true;
340 return;
341 }
342 }
343}
344
345/// Hoist expressions out of the specified loop. Note, alias info for inner
346/// loop is not preserved so it is not a good idea to run LICM multiple
347/// times on one loop.
348bool LoopInvariantCodeMotion::runOnLoop(
349 Loop *L, AAResults *AA, LoopInfo *LI, DominatorTree *DT,
350 BlockFrequencyInfo *BFI, TargetLibraryInfo *TLI, TargetTransformInfo *TTI,
351 ScalarEvolution *SE, MemorySSA *MSSA, OptimizationRemarkEmitter *ORE) {
352 bool Changed = false;
353
354 assert(L->isLCSSAForm(*DT) && "Loop is not in LCSSA form.")(static_cast <bool> (L->isLCSSAForm(*DT) && "Loop is not in LCSSA form."
) ? void (0) : __assert_fail ("L->isLCSSAForm(*DT) && \"Loop is not in LCSSA form.\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 354, __extension__ __PRETTY_FUNCTION__))
;
355
356 // If this loop has metadata indicating that LICM is not to be performed then
357 // just exit.
358 if (hasDisableLICMTransformsHint(L)) {
359 return false;
360 }
361
362 std::unique_ptr<AliasSetTracker> CurAST;
363 std::unique_ptr<MemorySSAUpdater> MSSAU;
364 std::unique_ptr<SinkAndHoistLICMFlags> Flags;
365
366 // Don't sink stores from loops with coroutine suspend instructions.
367 // LICM would sink instructions into the default destination of
368 // the coroutine switch. The default destination of the switch is to
369 // handle the case where the coroutine is suspended, by which point the
370 // coroutine frame may have been destroyed. No instruction can be sunk there.
371 // FIXME: This would unfortunately hurt the performance of coroutines, however
372 // there is currently no general solution for this. Similar issues could also
373 // potentially happen in other passes where instructions are being moved
374 // across that edge.
375 bool HasCoroSuspendInst = llvm::any_of(L->getBlocks(), [](BasicBlock *BB) {
376 return llvm::any_of(*BB, [](Instruction &I) {
377 IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
378 return II && II->getIntrinsicID() == Intrinsic::coro_suspend;
379 });
380 });
381
382 if (!MSSA) {
383 LLVM_DEBUG(dbgs() << "LICM: Using Alias Set Tracker.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM: Using Alias Set Tracker.\n"
; } } while (false)
;
384 CurAST = collectAliasInfoForLoop(L, LI, AA);
385 Flags = std::make_unique<SinkAndHoistLICMFlags>(
386 LicmMssaOptCap, LicmMssaNoAccForPromotionCap, /*IsSink=*/true);
387 } else {
388 LLVM_DEBUG(dbgs() << "LICM: Using MemorySSA.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM: Using MemorySSA.\n"; } } while
(false)
;
389 MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
390 Flags = std::make_unique<SinkAndHoistLICMFlags>(
391 LicmMssaOptCap, LicmMssaNoAccForPromotionCap, /*IsSink=*/true, L, MSSA);
392 }
393
394 // Get the preheader block to move instructions into...
395 BasicBlock *Preheader = L->getLoopPreheader();
396
397 // Compute loop safety information.
398 ICFLoopSafetyInfo SafetyInfo;
399 SafetyInfo.computeLoopSafetyInfo(L);
400
401 // We want to visit all of the instructions in this loop... that are not parts
402 // of our subloops (they have already had their invariants hoisted out of
403 // their loop, into this loop, so there is no need to process the BODIES of
404 // the subloops).
405 //
406 // Traverse the body of the loop in depth first order on the dominator tree so
407 // that we are guaranteed to see definitions before we see uses. This allows
408 // us to sink instructions in one pass, without iteration. After sinking
409 // instructions, we perform another pass to hoist them out of the loop.
410 if (L->hasDedicatedExits())
411 Changed |=
412 sinkRegion(DT->getNode(L->getHeader()), AA, LI, DT, BFI, TLI, TTI, L,
413 CurAST.get(), MSSAU.get(), &SafetyInfo, *Flags.get(), ORE);
414 Flags->setIsSink(false);
415 if (Preheader)
416 Changed |= hoistRegion(DT->getNode(L->getHeader()), AA, LI, DT, BFI, TLI, L,
417 CurAST.get(), MSSAU.get(), SE, &SafetyInfo,
418 *Flags.get(), ORE);
419
420 // Now that all loop invariants have been removed from the loop, promote any
421 // memory references to scalars that we can.
422 // Don't sink stores from loops without dedicated block exits. Exits
423 // containing indirect branches are not transformed by loop simplify,
424 // make sure we catch that. An additional load may be generated in the
425 // preheader for SSA updater, so also avoid sinking when no preheader
426 // is available.
427 if (!DisablePromotion && Preheader && L->hasDedicatedExits() &&
428 !Flags->tooManyMemoryAccesses() && !HasCoroSuspendInst) {
429 // Figure out the loop exits and their insertion points
430 SmallVector<BasicBlock *, 8> ExitBlocks;
431 L->getUniqueExitBlocks(ExitBlocks);
432
433 // We can't insert into a catchswitch.
434 bool HasCatchSwitch = llvm::any_of(ExitBlocks, [](BasicBlock *Exit) {
435 return isa<CatchSwitchInst>(Exit->getTerminator());
436 });
437
438 if (!HasCatchSwitch) {
439 SmallVector<Instruction *, 8> InsertPts;
440 SmallVector<MemoryAccess *, 8> MSSAInsertPts;
441 InsertPts.reserve(ExitBlocks.size());
442 if (MSSAU)
443 MSSAInsertPts.reserve(ExitBlocks.size());
444 for (BasicBlock *ExitBlock : ExitBlocks) {
445 InsertPts.push_back(&*ExitBlock->getFirstInsertionPt());
446 if (MSSAU)
447 MSSAInsertPts.push_back(nullptr);
448 }
449
450 PredIteratorCache PIC;
451
452 bool Promoted = false;
453 if (CurAST.get()) {
454 // Loop over all of the alias sets in the tracker object.
455 for (AliasSet &AS : *CurAST) {
456 // We can promote this alias set if it has a store, if it is a "Must"
457 // alias set, if the pointer is loop invariant, and if we are not
458 // eliminating any volatile loads or stores.
459 if (AS.isForwardingAliasSet() || !AS.isMod() || !AS.isMustAlias() ||
460 !L->isLoopInvariant(AS.begin()->getValue()))
461 continue;
462
463 assert((static_cast <bool> (!AS.empty() && "Must alias set should have at least one pointer element in it!"
) ? void (0) : __assert_fail ("!AS.empty() && \"Must alias set should have at least one pointer element in it!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 465, __extension__ __PRETTY_FUNCTION__))
464 !AS.empty() &&(static_cast <bool> (!AS.empty() && "Must alias set should have at least one pointer element in it!"
) ? void (0) : __assert_fail ("!AS.empty() && \"Must alias set should have at least one pointer element in it!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 465, __extension__ __PRETTY_FUNCTION__))
465 "Must alias set should have at least one pointer element in it!")(static_cast <bool> (!AS.empty() && "Must alias set should have at least one pointer element in it!"
) ? void (0) : __assert_fail ("!AS.empty() && \"Must alias set should have at least one pointer element in it!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 465, __extension__ __PRETTY_FUNCTION__))
;
466
467 SmallSetVector<Value *, 8> PointerMustAliases;
468 for (const auto &ASI : AS)
469 PointerMustAliases.insert(ASI.getValue());
470
471 Promoted |= promoteLoopAccessesToScalars(
472 PointerMustAliases, ExitBlocks, InsertPts, MSSAInsertPts, PIC, LI,
473 DT, TLI, L, CurAST.get(), MSSAU.get(), &SafetyInfo, ORE);
474 }
475 } else {
476 SmallVector<Instruction *, 16> MaybePromotable;
477 foreachMemoryAccess(MSSA, L, [&](Instruction *I) {
478 MaybePromotable.push_back(I);
479 });
480
481 // Promoting one set of accesses may make the pointers for another set
482 // loop invariant, so run this in a loop (with the MaybePromotable set
483 // decreasing in size over time).
484 bool LocalPromoted;
485 do {
486 LocalPromoted = false;
487 for (const SmallSetVector<Value *, 8> &PointerMustAliases :
488 collectPromotionCandidates(MSSA, AA, L, MaybePromotable)) {
489 LocalPromoted |= promoteLoopAccessesToScalars(
490 PointerMustAliases, ExitBlocks, InsertPts, MSSAInsertPts, PIC,
491 LI, DT, TLI, L, /*AST*/nullptr, MSSAU.get(), &SafetyInfo, ORE);
492 }
493 Promoted |= LocalPromoted;
494 } while (LocalPromoted);
495 }
496
497 // Once we have promoted values across the loop body we have to
498 // recursively reform LCSSA as any nested loop may now have values defined
499 // within the loop used in the outer loop.
500 // FIXME: This is really heavy handed. It would be a bit better to use an
501 // SSAUpdater strategy during promotion that was LCSSA aware and reformed
502 // it as it went.
503 if (Promoted)
504 formLCSSARecursively(*L, *DT, LI, SE);
505
506 Changed |= Promoted;
507 }
508 }
509
510 // Check that neither this loop nor its parent have had LCSSA broken. LICM is
511 // specifically moving instructions across the loop boundary and so it is
512 // especially in need of sanity checking here.
513 assert(L->isLCSSAForm(*DT) && "Loop not left in LCSSA form after LICM!")(static_cast <bool> (L->isLCSSAForm(*DT) && "Loop not left in LCSSA form after LICM!"
) ? void (0) : __assert_fail ("L->isLCSSAForm(*DT) && \"Loop not left in LCSSA form after LICM!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 513, __extension__ __PRETTY_FUNCTION__))
;
514 assert((L->isOutermost() || L->getParentLoop()->isLCSSAForm(*DT)) &&(static_cast <bool> ((L->isOutermost() || L->getParentLoop
()->isLCSSAForm(*DT)) && "Parent loop not left in LCSSA form after LICM!"
) ? void (0) : __assert_fail ("(L->isOutermost() || L->getParentLoop()->isLCSSAForm(*DT)) && \"Parent loop not left in LCSSA form after LICM!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 515, __extension__ __PRETTY_FUNCTION__))
515 "Parent loop not left in LCSSA form after LICM!")(static_cast <bool> ((L->isOutermost() || L->getParentLoop
()->isLCSSAForm(*DT)) && "Parent loop not left in LCSSA form after LICM!"
) ? void (0) : __assert_fail ("(L->isOutermost() || L->getParentLoop()->isLCSSAForm(*DT)) && \"Parent loop not left in LCSSA form after LICM!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 515, __extension__ __PRETTY_FUNCTION__))
;
516
517 if (MSSAU.get() && VerifyMemorySSA)
518 MSSAU->getMemorySSA()->verifyMemorySSA();
519
520 if (Changed && SE)
521 SE->forgetLoopDispositions(L);
522 return Changed;
523}
524
525/// Walk the specified region of the CFG (defined by all blocks dominated by
526/// the specified block, and that are in the current loop) in reverse depth
527/// first order w.r.t the DominatorTree. This allows us to visit uses before
528/// definitions, allowing us to sink a loop body in one pass without iteration.
529///
530bool llvm::sinkRegion(DomTreeNode *N, AAResults *AA, LoopInfo *LI,
531 DominatorTree *DT, BlockFrequencyInfo *BFI,
532 TargetLibraryInfo *TLI, TargetTransformInfo *TTI,
533 Loop *CurLoop, AliasSetTracker *CurAST,
534 MemorySSAUpdater *MSSAU, ICFLoopSafetyInfo *SafetyInfo,
535 SinkAndHoistLICMFlags &Flags,
536 OptimizationRemarkEmitter *ORE) {
537
538 // Verify inputs.
539 assert(N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr &&(static_cast <bool> (N != nullptr && AA != nullptr
&& LI != nullptr && DT != nullptr &&
CurLoop != nullptr && SafetyInfo != nullptr &&
"Unexpected input to sinkRegion.") ? void (0) : __assert_fail
("N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected input to sinkRegion.\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 541, __extension__ __PRETTY_FUNCTION__))
540 CurLoop != nullptr && SafetyInfo != nullptr &&(static_cast <bool> (N != nullptr && AA != nullptr
&& LI != nullptr && DT != nullptr &&
CurLoop != nullptr && SafetyInfo != nullptr &&
"Unexpected input to sinkRegion.") ? void (0) : __assert_fail
("N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected input to sinkRegion.\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 541, __extension__ __PRETTY_FUNCTION__))
541 "Unexpected input to sinkRegion.")(static_cast <bool> (N != nullptr && AA != nullptr
&& LI != nullptr && DT != nullptr &&
CurLoop != nullptr && SafetyInfo != nullptr &&
"Unexpected input to sinkRegion.") ? void (0) : __assert_fail
("N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected input to sinkRegion.\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 541, __extension__ __PRETTY_FUNCTION__))
;
542 assert(((CurAST != nullptr) ^ (MSSAU != nullptr)) &&(static_cast <bool> (((CurAST != nullptr) ^ (MSSAU != nullptr
)) && "Either AliasSetTracker or MemorySSA should be initialized."
) ? void (0) : __assert_fail ("((CurAST != nullptr) ^ (MSSAU != nullptr)) && \"Either AliasSetTracker or MemorySSA should be initialized.\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 543, __extension__ __PRETTY_FUNCTION__))
543 "Either AliasSetTracker or MemorySSA should be initialized.")(static_cast <bool> (((CurAST != nullptr) ^ (MSSAU != nullptr
)) && "Either AliasSetTracker or MemorySSA should be initialized."
) ? void (0) : __assert_fail ("((CurAST != nullptr) ^ (MSSAU != nullptr)) && \"Either AliasSetTracker or MemorySSA should be initialized.\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 543, __extension__ __PRETTY_FUNCTION__))
;
544
545 // We want to visit children before parents. We will enque all the parents
546 // before their children in the worklist and process the worklist in reverse
547 // order.
548 SmallVector<DomTreeNode *, 16> Worklist = collectChildrenInLoop(N, CurLoop);
549
550 bool Changed = false;
551 for (DomTreeNode *DTN : reverse(Worklist)) {
552 BasicBlock *BB = DTN->getBlock();
553 // Only need to process the contents of this block if it is not part of a
554 // subloop (which would already have been processed).
555 if (inSubLoop(BB, CurLoop, LI))
556 continue;
557
558 for (BasicBlock::iterator II = BB->end(); II != BB->begin();) {
559 Instruction &I = *--II;
560
561 // The instruction is not used in the loop if it is dead. In this case,
562 // we just delete it instead of sinking it.
563 if (isInstructionTriviallyDead(&I, TLI)) {
564 LLVM_DEBUG(dbgs() << "LICM deleting dead inst: " << I << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM deleting dead inst: " <<
I << '\n'; } } while (false)
;
565 salvageKnowledge(&I);
566 salvageDebugInfo(I);
567 ++II;
568 eraseInstruction(I, *SafetyInfo, CurAST, MSSAU);
569 Changed = true;
570 continue;
571 }
572
573 // Check to see if we can sink this instruction to the exit blocks
574 // of the loop. We can do this if the all users of the instruction are
575 // outside of the loop. In this case, it doesn't even matter if the
576 // operands of the instruction are loop invariant.
577 //
578 bool FreeInLoop = false;
579 if (!I.mayHaveSideEffects() &&
580 isNotUsedOrFreeInLoop(I, CurLoop, SafetyInfo, TTI, FreeInLoop) &&
581 canSinkOrHoistInst(I, AA, DT, CurLoop, CurAST, MSSAU, true, &Flags,
582 ORE)) {
583 if (sink(I, LI, DT, BFI, CurLoop, SafetyInfo, MSSAU, ORE)) {
584 if (!FreeInLoop) {
585 ++II;
586 salvageDebugInfo(I);
587 eraseInstruction(I, *SafetyInfo, CurAST, MSSAU);
588 }
589 Changed = true;
590 }
591 }
592 }
593 }
594 if (MSSAU && VerifyMemorySSA)
595 MSSAU->getMemorySSA()->verifyMemorySSA();
596 return Changed;
597}
598
599namespace {
600// This is a helper class for hoistRegion to make it able to hoist control flow
601// in order to be able to hoist phis. The way this works is that we initially
602// start hoisting to the loop preheader, and when we see a loop invariant branch
603// we make note of this. When we then come to hoist an instruction that's
604// conditional on such a branch we duplicate the branch and the relevant control
605// flow, then hoist the instruction into the block corresponding to its original
606// block in the duplicated control flow.
607class ControlFlowHoister {
608private:
609 // Information about the loop we are hoisting from
610 LoopInfo *LI;
611 DominatorTree *DT;
612 Loop *CurLoop;
613 MemorySSAUpdater *MSSAU;
614
615 // A map of blocks in the loop to the block their instructions will be hoisted
616 // to.
617 DenseMap<BasicBlock *, BasicBlock *> HoistDestinationMap;
618
619 // The branches that we can hoist, mapped to the block that marks a
620 // convergence point of their control flow.
621 DenseMap<BranchInst *, BasicBlock *> HoistableBranches;
622
623public:
624 ControlFlowHoister(LoopInfo *LI, DominatorTree *DT, Loop *CurLoop,
625 MemorySSAUpdater *MSSAU)
626 : LI(LI), DT(DT), CurLoop(CurLoop), MSSAU(MSSAU) {}
627
628 void registerPossiblyHoistableBranch(BranchInst *BI) {
629 // We can only hoist conditional branches with loop invariant operands.
630 if (!ControlFlowHoisting || !BI->isConditional() ||
631 !CurLoop->hasLoopInvariantOperands(BI))
632 return;
633
634 // The branch destinations need to be in the loop, and we don't gain
635 // anything by duplicating conditional branches with duplicate successors,
636 // as it's essentially the same as an unconditional branch.
637 BasicBlock *TrueDest = BI->getSuccessor(0);
638 BasicBlock *FalseDest = BI->getSuccessor(1);
639 if (!CurLoop->contains(TrueDest) || !CurLoop->contains(FalseDest) ||
640 TrueDest == FalseDest)
641 return;
642
643 // We can hoist BI if one branch destination is the successor of the other,
644 // or both have common successor which we check by seeing if the
645 // intersection of their successors is non-empty.
646 // TODO: This could be expanded to allowing branches where both ends
647 // eventually converge to a single block.
648 SmallPtrSet<BasicBlock *, 4> TrueDestSucc, FalseDestSucc;
649 TrueDestSucc.insert(succ_begin(TrueDest), succ_end(TrueDest));
650 FalseDestSucc.insert(succ_begin(FalseDest), succ_end(FalseDest));
651 BasicBlock *CommonSucc = nullptr;
652 if (TrueDestSucc.count(FalseDest)) {
653 CommonSucc = FalseDest;
654 } else if (FalseDestSucc.count(TrueDest)) {
655 CommonSucc = TrueDest;
656 } else {
657 set_intersect(TrueDestSucc, FalseDestSucc);
658 // If there's one common successor use that.
659 if (TrueDestSucc.size() == 1)
660 CommonSucc = *TrueDestSucc.begin();
661 // If there's more than one pick whichever appears first in the block list
662 // (we can't use the value returned by TrueDestSucc.begin() as it's
663 // unpredicatable which element gets returned).
664 else if (!TrueDestSucc.empty()) {
665 Function *F = TrueDest->getParent();
666 auto IsSucc = [&](BasicBlock &BB) { return TrueDestSucc.count(&BB); };
667 auto It = llvm::find_if(*F, IsSucc);
668 assert(It != F->end() && "Could not find successor in function")(static_cast <bool> (It != F->end() && "Could not find successor in function"
) ? void (0) : __assert_fail ("It != F->end() && \"Could not find successor in function\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 668, __extension__ __PRETTY_FUNCTION__))
;
669 CommonSucc = &*It;
670 }
671 }
672 // The common successor has to be dominated by the branch, as otherwise
673 // there will be some other path to the successor that will not be
674 // controlled by this branch so any phi we hoist would be controlled by the
675 // wrong condition. This also takes care of avoiding hoisting of loop back
676 // edges.
677 // TODO: In some cases this could be relaxed if the successor is dominated
678 // by another block that's been hoisted and we can guarantee that the
679 // control flow has been replicated exactly.
680 if (CommonSucc && DT->dominates(BI, CommonSucc))
681 HoistableBranches[BI] = CommonSucc;
682 }
683
684 bool canHoistPHI(PHINode *PN) {
685 // The phi must have loop invariant operands.
686 if (!ControlFlowHoisting || !CurLoop->hasLoopInvariantOperands(PN))
687 return false;
688 // We can hoist phis if the block they are in is the target of hoistable
689 // branches which cover all of the predecessors of the block.
690 SmallPtrSet<BasicBlock *, 8> PredecessorBlocks;
691 BasicBlock *BB = PN->getParent();
692 for (BasicBlock *PredBB : predecessors(BB))
693 PredecessorBlocks.insert(PredBB);
694 // If we have less predecessor blocks than predecessors then the phi will
695 // have more than one incoming value for the same block which we can't
696 // handle.
697 // TODO: This could be handled be erasing some of the duplicate incoming
698 // values.
699 if (PredecessorBlocks.size() != pred_size(BB))
700 return false;
701 for (auto &Pair : HoistableBranches) {
702 if (Pair.second == BB) {
703 // Which blocks are predecessors via this branch depends on if the
704 // branch is triangle-like or diamond-like.
705 if (Pair.first->getSuccessor(0) == BB) {
706 PredecessorBlocks.erase(Pair.first->getParent());
707 PredecessorBlocks.erase(Pair.first->getSuccessor(1));
708 } else if (Pair.first->getSuccessor(1) == BB) {
709 PredecessorBlocks.erase(Pair.first->getParent());
710 PredecessorBlocks.erase(Pair.first->getSuccessor(0));
711 } else {
712 PredecessorBlocks.erase(Pair.first->getSuccessor(0));
713 PredecessorBlocks.erase(Pair.first->getSuccessor(1));
714 }
715 }
716 }
717 // PredecessorBlocks will now be empty if for every predecessor of BB we
718 // found a hoistable branch source.
719 return PredecessorBlocks.empty();
720 }
721
722 BasicBlock *getOrCreateHoistedBlock(BasicBlock *BB) {
723 if (!ControlFlowHoisting)
724 return CurLoop->getLoopPreheader();
725 // If BB has already been hoisted, return that
726 if (HoistDestinationMap.count(BB))
727 return HoistDestinationMap[BB];
728
729 // Check if this block is conditional based on a pending branch
730 auto HasBBAsSuccessor =
731 [&](DenseMap<BranchInst *, BasicBlock *>::value_type &Pair) {
732 return BB != Pair.second && (Pair.first->getSuccessor(0) == BB ||
733 Pair.first->getSuccessor(1) == BB);
734 };
735 auto It = llvm::find_if(HoistableBranches, HasBBAsSuccessor);
736
737 // If not involved in a pending branch, hoist to preheader
738 BasicBlock *InitialPreheader = CurLoop->getLoopPreheader();
739 if (It == HoistableBranches.end()) {
740 LLVM_DEBUG(dbgs() << "LICM using "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM using " << InitialPreheader
->getNameOrAsOperand() << " as hoist destination for "
<< BB->getNameOrAsOperand() << "\n"; } } while
(false)
741 << InitialPreheader->getNameOrAsOperand()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM using " << InitialPreheader
->getNameOrAsOperand() << " as hoist destination for "
<< BB->getNameOrAsOperand() << "\n"; } } while
(false)
742 << " as hoist destination for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM using " << InitialPreheader
->getNameOrAsOperand() << " as hoist destination for "
<< BB->getNameOrAsOperand() << "\n"; } } while
(false)
743 << BB->getNameOrAsOperand() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM using " << InitialPreheader
->getNameOrAsOperand() << " as hoist destination for "
<< BB->getNameOrAsOperand() << "\n"; } } while
(false)
;
744 HoistDestinationMap[BB] = InitialPreheader;
745 return InitialPreheader;
746 }
747 BranchInst *BI = It->first;
748 assert(std::find_if(++It, HoistableBranches.end(), HasBBAsSuccessor) ==(static_cast <bool> (std::find_if(++It, HoistableBranches
.end(), HasBBAsSuccessor) == HoistableBranches.end() &&
"BB is expected to be the target of at most one branch") ? void
(0) : __assert_fail ("std::find_if(++It, HoistableBranches.end(), HasBBAsSuccessor) == HoistableBranches.end() && \"BB is expected to be the target of at most one branch\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 750, __extension__ __PRETTY_FUNCTION__))
749 HoistableBranches.end() &&(static_cast <bool> (std::find_if(++It, HoistableBranches
.end(), HasBBAsSuccessor) == HoistableBranches.end() &&
"BB is expected to be the target of at most one branch") ? void
(0) : __assert_fail ("std::find_if(++It, HoistableBranches.end(), HasBBAsSuccessor) == HoistableBranches.end() && \"BB is expected to be the target of at most one branch\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 750, __extension__ __PRETTY_FUNCTION__))
750 "BB is expected to be the target of at most one branch")(static_cast <bool> (std::find_if(++It, HoistableBranches
.end(), HasBBAsSuccessor) == HoistableBranches.end() &&
"BB is expected to be the target of at most one branch") ? void
(0) : __assert_fail ("std::find_if(++It, HoistableBranches.end(), HasBBAsSuccessor) == HoistableBranches.end() && \"BB is expected to be the target of at most one branch\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 750, __extension__ __PRETTY_FUNCTION__))
;
751
752 LLVMContext &C = BB->getContext();
753 BasicBlock *TrueDest = BI->getSuccessor(0);
754 BasicBlock *FalseDest = BI->getSuccessor(1);
755 BasicBlock *CommonSucc = HoistableBranches[BI];
756 BasicBlock *HoistTarget = getOrCreateHoistedBlock(BI->getParent());
757
758 // Create hoisted versions of blocks that currently don't have them
759 auto CreateHoistedBlock = [&](BasicBlock *Orig) {
760 if (HoistDestinationMap.count(Orig))
761 return HoistDestinationMap[Orig];
762 BasicBlock *New =
763 BasicBlock::Create(C, Orig->getName() + ".licm", Orig->getParent());
764 HoistDestinationMap[Orig] = New;
765 DT->addNewBlock(New, HoistTarget);
766 if (CurLoop->getParentLoop())
767 CurLoop->getParentLoop()->addBasicBlockToLoop(New, *LI);
768 ++NumCreatedBlocks;
769 LLVM_DEBUG(dbgs() << "LICM created " << New->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM created " << New->
getName() << " as hoist destination for " << Orig
->getName() << "\n"; } } while (false)
770 << " as hoist destination for " << Orig->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM created " << New->
getName() << " as hoist destination for " << Orig
->getName() << "\n"; } } while (false)
771 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM created " << New->
getName() << " as hoist destination for " << Orig
->getName() << "\n"; } } while (false)
;
772 return New;
773 };
774 BasicBlock *HoistTrueDest = CreateHoistedBlock(TrueDest);
775 BasicBlock *HoistFalseDest = CreateHoistedBlock(FalseDest);
776 BasicBlock *HoistCommonSucc = CreateHoistedBlock(CommonSucc);
777
778 // Link up these blocks with branches.
779 if (!HoistCommonSucc->getTerminator()) {
780 // The new common successor we've generated will branch to whatever that
781 // hoist target branched to.
782 BasicBlock *TargetSucc = HoistTarget->getSingleSuccessor();
783 assert(TargetSucc && "Expected hoist target to have a single successor")(static_cast <bool> (TargetSucc && "Expected hoist target to have a single successor"
) ? void (0) : __assert_fail ("TargetSucc && \"Expected hoist target to have a single successor\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 783, __extension__ __PRETTY_FUNCTION__))
;
784 HoistCommonSucc->moveBefore(TargetSucc);
785 BranchInst::Create(TargetSucc, HoistCommonSucc);
786 }
787 if (!HoistTrueDest->getTerminator()) {
788 HoistTrueDest->moveBefore(HoistCommonSucc);
789 BranchInst::Create(HoistCommonSucc, HoistTrueDest);
790 }
791 if (!HoistFalseDest->getTerminator()) {
792 HoistFalseDest->moveBefore(HoistCommonSucc);
793 BranchInst::Create(HoistCommonSucc, HoistFalseDest);
794 }
795
796 // If BI is being cloned to what was originally the preheader then
797 // HoistCommonSucc will now be the new preheader.
798 if (HoistTarget == InitialPreheader) {
799 // Phis in the loop header now need to use the new preheader.
800 InitialPreheader->replaceSuccessorsPhiUsesWith(HoistCommonSucc);
801 if (MSSAU)
802 MSSAU->wireOldPredecessorsToNewImmediatePredecessor(
803 HoistTarget->getSingleSuccessor(), HoistCommonSucc, {HoistTarget});
804 // The new preheader dominates the loop header.
805 DomTreeNode *PreheaderNode = DT->getNode(HoistCommonSucc);
806 DomTreeNode *HeaderNode = DT->getNode(CurLoop->getHeader());
807 DT->changeImmediateDominator(HeaderNode, PreheaderNode);
808 // The preheader hoist destination is now the new preheader, with the
809 // exception of the hoist destination of this branch.
810 for (auto &Pair : HoistDestinationMap)
811 if (Pair.second == InitialPreheader && Pair.first != BI->getParent())
812 Pair.second = HoistCommonSucc;
813 }
814
815 // Now finally clone BI.
816 ReplaceInstWithInst(
817 HoistTarget->getTerminator(),
818 BranchInst::Create(HoistTrueDest, HoistFalseDest, BI->getCondition()));
819 ++NumClonedBranches;
820
821 assert(CurLoop->getLoopPreheader() &&(static_cast <bool> (CurLoop->getLoopPreheader() &&
"Hoisting blocks should not have destroyed preheader") ? void
(0) : __assert_fail ("CurLoop->getLoopPreheader() && \"Hoisting blocks should not have destroyed preheader\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 822, __extension__ __PRETTY_FUNCTION__))
822 "Hoisting blocks should not have destroyed preheader")(static_cast <bool> (CurLoop->getLoopPreheader() &&
"Hoisting blocks should not have destroyed preheader") ? void
(0) : __assert_fail ("CurLoop->getLoopPreheader() && \"Hoisting blocks should not have destroyed preheader\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 822, __extension__ __PRETTY_FUNCTION__))
;
823 return HoistDestinationMap[BB];
824 }
825};
826} // namespace
827
828// Hoisting/sinking instruction out of a loop isn't always beneficial. It's only
829// only worthwhile if the destination block is actually colder than current
830// block.
831static bool worthSinkOrHoistInst(Instruction &I, BasicBlock *DstBlock,
832 OptimizationRemarkEmitter *ORE,
833 BlockFrequencyInfo *BFI) {
834 // Check block frequency only when runtime profile is available
835 // to avoid pathological cases. With static profile, lean towards
836 // hosting because it helps canonicalize the loop for vectorizer.
837 if (!DstBlock->getParent()->hasProfileData())
838 return true;
839
840 if (!HoistSinkColdnessThreshold || !BFI)
841 return true;
842
843 BasicBlock *SrcBlock = I.getParent();
844 if (BFI->getBlockFreq(DstBlock).getFrequency() / HoistSinkColdnessThreshold >
845 BFI->getBlockFreq(SrcBlock).getFrequency()) {
846 ORE->emit([&]() {
847 return OptimizationRemarkMissed(DEBUG_TYPE"licm", "SinkHoistInst", &I)
848 << "failed to sink or hoist instruction because containing block "
849 "has lower frequency than destination block";
850 });
851 return false;
852 }
853
854 return true;
855}
856
857/// Walk the specified region of the CFG (defined by all blocks dominated by
858/// the specified block, and that are in the current loop) in depth first
859/// order w.r.t the DominatorTree. This allows us to visit definitions before
860/// uses, allowing us to hoist a loop body in one pass without iteration.
861///
862bool llvm::hoistRegion(DomTreeNode *N, AAResults *AA, LoopInfo *LI,
863 DominatorTree *DT, BlockFrequencyInfo *BFI,
864 TargetLibraryInfo *TLI, Loop *CurLoop,
865 AliasSetTracker *CurAST, MemorySSAUpdater *MSSAU,
866 ScalarEvolution *SE, ICFLoopSafetyInfo *SafetyInfo,
867 SinkAndHoistLICMFlags &Flags,
868 OptimizationRemarkEmitter *ORE) {
869 // Verify inputs.
870 assert(N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr &&(static_cast <bool> (N != nullptr && AA != nullptr
&& LI != nullptr && DT != nullptr &&
CurLoop != nullptr && SafetyInfo != nullptr &&
"Unexpected input to hoistRegion.") ? void (0) : __assert_fail
("N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected input to hoistRegion.\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 872, __extension__ __PRETTY_FUNCTION__))
871 CurLoop != nullptr && SafetyInfo != nullptr &&(static_cast <bool> (N != nullptr && AA != nullptr
&& LI != nullptr && DT != nullptr &&
CurLoop != nullptr && SafetyInfo != nullptr &&
"Unexpected input to hoistRegion.") ? void (0) : __assert_fail
("N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected input to hoistRegion.\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 872, __extension__ __PRETTY_FUNCTION__))
872 "Unexpected input to hoistRegion.")(static_cast <bool> (N != nullptr && AA != nullptr
&& LI != nullptr && DT != nullptr &&
CurLoop != nullptr && SafetyInfo != nullptr &&
"Unexpected input to hoistRegion.") ? void (0) : __assert_fail
("N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected input to hoistRegion.\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 872, __extension__ __PRETTY_FUNCTION__))
;
873 assert(((CurAST != nullptr) ^ (MSSAU != nullptr)) &&(static_cast <bool> (((CurAST != nullptr) ^ (MSSAU != nullptr
)) && "Either AliasSetTracker or MemorySSA should be initialized."
) ? void (0) : __assert_fail ("((CurAST != nullptr) ^ (MSSAU != nullptr)) && \"Either AliasSetTracker or MemorySSA should be initialized.\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 874, __extension__ __PRETTY_FUNCTION__))
874 "Either AliasSetTracker or MemorySSA should be initialized.")(static_cast <bool> (((CurAST != nullptr) ^ (MSSAU != nullptr
)) && "Either AliasSetTracker or MemorySSA should be initialized."
) ? void (0) : __assert_fail ("((CurAST != nullptr) ^ (MSSAU != nullptr)) && \"Either AliasSetTracker or MemorySSA should be initialized.\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 874, __extension__ __PRETTY_FUNCTION__))
;
875
876 ControlFlowHoister CFH(LI, DT, CurLoop, MSSAU);
877
878 // Keep track of instructions that have been hoisted, as they may need to be
879 // re-hoisted if they end up not dominating all of their uses.
880 SmallVector<Instruction *, 16> HoistedInstructions;
881
882 // For PHI hoisting to work we need to hoist blocks before their successors.
883 // We can do this by iterating through the blocks in the loop in reverse
884 // post-order.
885 LoopBlocksRPO Worklist(CurLoop);
886 Worklist.perform(LI);
887 bool Changed = false;
888 for (BasicBlock *BB : Worklist) {
889 // Only need to process the contents of this block if it is not part of a
890 // subloop (which would already have been processed).
891 if (inSubLoop(BB, CurLoop, LI))
892 continue;
893
894 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) {
895 Instruction &I = *II++;
896 // Try constant folding this instruction. If all the operands are
897 // constants, it is technically hoistable, but it would be better to
898 // just fold it.
899 if (Constant *C = ConstantFoldInstruction(
900 &I, I.getModule()->getDataLayout(), TLI)) {
901 LLVM_DEBUG(dbgs() << "LICM folding inst: " << I << " --> " << *Cdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM folding inst: " << I <<
" --> " << *C << '\n'; } } while (false)
902 << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM folding inst: " << I <<
" --> " << *C << '\n'; } } while (false)
;
903 if (CurAST)
904 CurAST->copyValue(&I, C);
905 // FIXME MSSA: Such replacements may make accesses unoptimized (D51960).
906 I.replaceAllUsesWith(C);
907 if (isInstructionTriviallyDead(&I, TLI))
908 eraseInstruction(I, *SafetyInfo, CurAST, MSSAU);
909 Changed = true;
910 continue;
911 }
912
913 // Try hoisting the instruction out to the preheader. We can only do
914 // this if all of the operands of the instruction are loop invariant and
915 // if it is safe to hoist the instruction. We also check block frequency
916 // to make sure instruction only gets hoisted into colder blocks.
917 // TODO: It may be safe to hoist if we are hoisting to a conditional block
918 // and we have accurately duplicated the control flow from the loop header
919 // to that block.
920 if (CurLoop->hasLoopInvariantOperands(&I) &&
921 canSinkOrHoistInst(I, AA, DT, CurLoop, CurAST, MSSAU, true, &Flags,
922 ORE) &&
923 worthSinkOrHoistInst(I, CurLoop->getLoopPreheader(), ORE, BFI) &&
924 isSafeToExecuteUnconditionally(
925 I, DT, TLI, CurLoop, SafetyInfo, ORE,
926 CurLoop->getLoopPreheader()->getTerminator())) {
927 hoist(I, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo,
928 MSSAU, SE, ORE);
929 HoistedInstructions.push_back(&I);
930 Changed = true;
931 continue;
932 }
933
934 // Attempt to remove floating point division out of the loop by
935 // converting it to a reciprocal multiplication.
936 if (I.getOpcode() == Instruction::FDiv && I.hasAllowReciprocal() &&
937 CurLoop->isLoopInvariant(I.getOperand(1))) {
938 auto Divisor = I.getOperand(1);
939 auto One = llvm::ConstantFP::get(Divisor->getType(), 1.0);
940 auto ReciprocalDivisor = BinaryOperator::CreateFDiv(One, Divisor);
941 ReciprocalDivisor->setFastMathFlags(I.getFastMathFlags());
942 SafetyInfo->insertInstructionTo(ReciprocalDivisor, I.getParent());
943 ReciprocalDivisor->insertBefore(&I);
944
945 auto Product =
946 BinaryOperator::CreateFMul(I.getOperand(0), ReciprocalDivisor);
947 Product->setFastMathFlags(I.getFastMathFlags());
948 SafetyInfo->insertInstructionTo(Product, I.getParent());
949 Product->insertAfter(&I);
950 I.replaceAllUsesWith(Product);
951 eraseInstruction(I, *SafetyInfo, CurAST, MSSAU);
952
953 hoist(*ReciprocalDivisor, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB),
954 SafetyInfo, MSSAU, SE, ORE);
955 HoistedInstructions.push_back(ReciprocalDivisor);
956 Changed = true;
957 continue;
958 }
959
960 auto IsInvariantStart = [&](Instruction &I) {
961 using namespace PatternMatch;
962 return I.use_empty() &&
963 match(&I, m_Intrinsic<Intrinsic::invariant_start>());
964 };
965 auto MustExecuteWithoutWritesBefore = [&](Instruction &I) {
966 return SafetyInfo->isGuaranteedToExecute(I, DT, CurLoop) &&
967 SafetyInfo->doesNotWriteMemoryBefore(I, CurLoop);
968 };
969 if ((IsInvariantStart(I) || isGuard(&I)) &&
970 CurLoop->hasLoopInvariantOperands(&I) &&
971 MustExecuteWithoutWritesBefore(I)) {
972 hoist(I, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo,
973 MSSAU, SE, ORE);
974 HoistedInstructions.push_back(&I);
975 Changed = true;
976 continue;
977 }
978
979 if (PHINode *PN = dyn_cast<PHINode>(&I)) {
980 if (CFH.canHoistPHI(PN)) {
981 // Redirect incoming blocks first to ensure that we create hoisted
982 // versions of those blocks before we hoist the phi.
983 for (unsigned int i = 0; i < PN->getNumIncomingValues(); ++i)
984 PN->setIncomingBlock(
985 i, CFH.getOrCreateHoistedBlock(PN->getIncomingBlock(i)));
986 hoist(*PN, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo,
987 MSSAU, SE, ORE);
988 assert(DT->dominates(PN, BB) && "Conditional PHIs not expected")(static_cast <bool> (DT->dominates(PN, BB) &&
"Conditional PHIs not expected") ? void (0) : __assert_fail (
"DT->dominates(PN, BB) && \"Conditional PHIs not expected\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 988, __extension__ __PRETTY_FUNCTION__))
;
989 Changed = true;
990 continue;
991 }
992 }
993
994 // Remember possibly hoistable branches so we can actually hoist them
995 // later if needed.
996 if (BranchInst *BI = dyn_cast<BranchInst>(&I))
997 CFH.registerPossiblyHoistableBranch(BI);
998 }
999 }
1000
1001 // If we hoisted instructions to a conditional block they may not dominate
1002 // their uses that weren't hoisted (such as phis where some operands are not
1003 // loop invariant). If so make them unconditional by moving them to their
1004 // immediate dominator. We iterate through the instructions in reverse order
1005 // which ensures that when we rehoist an instruction we rehoist its operands,
1006 // and also keep track of where in the block we are rehoisting to to make sure
1007 // that we rehoist instructions before the instructions that use them.
1008 Instruction *HoistPoint = nullptr;
1009 if (ControlFlowHoisting) {
1010 for (Instruction *I : reverse(HoistedInstructions)) {
1011 if (!llvm::all_of(I->uses(),
1012 [&](Use &U) { return DT->dominates(I, U); })) {
1013 BasicBlock *Dominator =
1014 DT->getNode(I->getParent())->getIDom()->getBlock();
1015 if (!HoistPoint || !DT->dominates(HoistPoint->getParent(), Dominator)) {
1016 if (HoistPoint)
1017 assert(DT->dominates(Dominator, HoistPoint->getParent()) &&(static_cast <bool> (DT->dominates(Dominator, HoistPoint
->getParent()) && "New hoist point expected to dominate old hoist point"
) ? void (0) : __assert_fail ("DT->dominates(Dominator, HoistPoint->getParent()) && \"New hoist point expected to dominate old hoist point\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1018, __extension__ __PRETTY_FUNCTION__))
1018 "New hoist point expected to dominate old hoist point")(static_cast <bool> (DT->dominates(Dominator, HoistPoint
->getParent()) && "New hoist point expected to dominate old hoist point"
) ? void (0) : __assert_fail ("DT->dominates(Dominator, HoistPoint->getParent()) && \"New hoist point expected to dominate old hoist point\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1018, __extension__ __PRETTY_FUNCTION__))
;
1019 HoistPoint = Dominator->getTerminator();
1020 }
1021 LLVM_DEBUG(dbgs() << "LICM rehoisting to "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM rehoisting to " << HoistPoint
->getParent()->getNameOrAsOperand() << ": " <<
*I << "\n"; } } while (false)
1022 << HoistPoint->getParent()->getNameOrAsOperand()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM rehoisting to " << HoistPoint
->getParent()->getNameOrAsOperand() << ": " <<
*I << "\n"; } } while (false)
1023 << ": " << *I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM rehoisting to " << HoistPoint
->getParent()->getNameOrAsOperand() << ": " <<
*I << "\n"; } } while (false)
;
1024 moveInstructionBefore(*I, *HoistPoint, *SafetyInfo, MSSAU, SE);
1025 HoistPoint = I;
1026 Changed = true;
1027 }
1028 }
1029 }
1030 if (MSSAU && VerifyMemorySSA)
1031 MSSAU->getMemorySSA()->verifyMemorySSA();
1032
1033 // Now that we've finished hoisting make sure that LI and DT are still
1034 // valid.
1035#ifdef EXPENSIVE_CHECKS
1036 if (Changed) {
1037 assert(DT->verify(DominatorTree::VerificationLevel::Fast) &&(static_cast <bool> (DT->verify(DominatorTree::VerificationLevel
::Fast) && "Dominator tree verification failed") ? void
(0) : __assert_fail ("DT->verify(DominatorTree::VerificationLevel::Fast) && \"Dominator tree verification failed\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1038, __extension__ __PRETTY_FUNCTION__))
1038 "Dominator tree verification failed")(static_cast <bool> (DT->verify(DominatorTree::VerificationLevel
::Fast) && "Dominator tree verification failed") ? void
(0) : __assert_fail ("DT->verify(DominatorTree::VerificationLevel::Fast) && \"Dominator tree verification failed\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1038, __extension__ __PRETTY_FUNCTION__))
;
1039 LI->verify(*DT);
1040 }
1041#endif
1042
1043 return Changed;
1044}
1045
1046// Return true if LI is invariant within scope of the loop. LI is invariant if
1047// CurLoop is dominated by an invariant.start representing the same memory
1048// location and size as the memory location LI loads from, and also the
1049// invariant.start has no uses.
1050static bool isLoadInvariantInLoop(LoadInst *LI, DominatorTree *DT,
1051 Loop *CurLoop) {
1052 Value *Addr = LI->getOperand(0);
1053 const DataLayout &DL = LI->getModule()->getDataLayout();
1054 const TypeSize LocSizeInBits = DL.getTypeSizeInBits(LI->getType());
1055
1056 // It is not currently possible for clang to generate an invariant.start
1057 // intrinsic with scalable vector types because we don't support thread local
1058 // sizeless types and we don't permit sizeless types in structs or classes.
1059 // Furthermore, even if support is added for this in future the intrinsic
1060 // itself is defined to have a size of -1 for variable sized objects. This
1061 // makes it impossible to verify if the intrinsic envelops our region of
1062 // interest. For example, both <vscale x 32 x i8> and <vscale x 16 x i8>
1063 // types would have a -1 parameter, but the former is clearly double the size
1064 // of the latter.
1065 if (LocSizeInBits.isScalable())
1066 return false;
1067
1068 // if the type is i8 addrspace(x)*, we know this is the type of
1069 // llvm.invariant.start operand
1070 auto *PtrInt8Ty = PointerType::get(Type::getInt8Ty(LI->getContext()),
1071 LI->getPointerAddressSpace());
1072 unsigned BitcastsVisited = 0;
1073 // Look through bitcasts until we reach the i8* type (this is invariant.start
1074 // operand type).
1075 while (Addr->getType() != PtrInt8Ty) {
1076 auto *BC = dyn_cast<BitCastInst>(Addr);
1077 // Avoid traversing high number of bitcast uses.
1078 if (++BitcastsVisited > MaxNumUsesTraversed || !BC)
1079 return false;
1080 Addr = BC->getOperand(0);
1081 }
1082
1083 unsigned UsesVisited = 0;
1084 // Traverse all uses of the load operand value, to see if invariant.start is
1085 // one of the uses, and whether it dominates the load instruction.
1086 for (auto *U : Addr->users()) {
1087 // Avoid traversing for Load operand with high number of users.
1088 if (++UsesVisited > MaxNumUsesTraversed)
1089 return false;
1090 IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
1091 // If there are escaping uses of invariant.start instruction, the load maybe
1092 // non-invariant.
1093 if (!II || II->getIntrinsicID() != Intrinsic::invariant_start ||
1094 !II->use_empty())
1095 continue;
1096 ConstantInt *InvariantSize = cast<ConstantInt>(II->getArgOperand(0));
1097 // The intrinsic supports having a -1 argument for variable sized objects
1098 // so we should check for that here.
1099 if (InvariantSize->isNegative())
1100 continue;
1101 uint64_t InvariantSizeInBits = InvariantSize->getSExtValue() * 8;
1102 // Confirm the invariant.start location size contains the load operand size
1103 // in bits. Also, the invariant.start should dominate the load, and we
1104 // should not hoist the load out of a loop that contains this dominating
1105 // invariant.start.
1106 if (LocSizeInBits.getFixedSize() <= InvariantSizeInBits &&
1107 DT->properlyDominates(II->getParent(), CurLoop->getHeader()))
1108 return true;
1109 }
1110
1111 return false;
1112}
1113
1114namespace {
1115/// Return true if-and-only-if we know how to (mechanically) both hoist and
1116/// sink a given instruction out of a loop. Does not address legality
1117/// concerns such as aliasing or speculation safety.
1118bool isHoistableAndSinkableInst(Instruction &I) {
1119 // Only these instructions are hoistable/sinkable.
1120 return (isa<LoadInst>(I) || isa<StoreInst>(I) || isa<CallInst>(I) ||
5
Assuming 'I' is a 'LoadInst'
6
Returning the value 1, which participates in a condition later
1121 isa<FenceInst>(I) || isa<CastInst>(I) || isa<UnaryOperator>(I) ||
1122 isa<BinaryOperator>(I) || isa<SelectInst>(I) ||
1123 isa<GetElementPtrInst>(I) || isa<CmpInst>(I) ||
1124 isa<InsertElementInst>(I) || isa<ExtractElementInst>(I) ||
1125 isa<ShuffleVectorInst>(I) || isa<ExtractValueInst>(I) ||
1126 isa<InsertValueInst>(I) || isa<FreezeInst>(I));
1127}
1128/// Return true if all of the alias sets within this AST are known not to
1129/// contain a Mod, or if MSSA knows there are no MemoryDefs in the loop.
1130bool isReadOnly(AliasSetTracker *CurAST, const MemorySSAUpdater *MSSAU,
1131 const Loop *L) {
1132 if (CurAST) {
1133 for (AliasSet &AS : *CurAST) {
1134 if (!AS.isForwardingAliasSet() && AS.isMod()) {
1135 return false;
1136 }
1137 }
1138 return true;
1139 } else { /*MSSAU*/
1140 for (auto *BB : L->getBlocks())
1141 if (MSSAU->getMemorySSA()->getBlockDefs(BB))
1142 return false;
1143 return true;
1144 }
1145}
1146
1147/// Return true if I is the only Instruction with a MemoryAccess in L.
1148bool isOnlyMemoryAccess(const Instruction *I, const Loop *L,
1149 const MemorySSAUpdater *MSSAU) {
1150 for (auto *BB : L->getBlocks())
1151 if (auto *Accs = MSSAU->getMemorySSA()->getBlockAccesses(BB)) {
1152 int NotAPhi = 0;
1153 for (const auto &Acc : *Accs) {
1154 if (isa<MemoryPhi>(&Acc))
1155 continue;
1156 const auto *MUD = cast<MemoryUseOrDef>(&Acc);
1157 if (MUD->getMemoryInst() != I || NotAPhi++ == 1)
1158 return false;
1159 }
1160 }
1161 return true;
1162}
1163}
1164
1165bool llvm::canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT,
1166 Loop *CurLoop, AliasSetTracker *CurAST,
1167 MemorySSAUpdater *MSSAU,
1168 bool TargetExecutesOncePerLoop,
1169 SinkAndHoistLICMFlags *Flags,
1170 OptimizationRemarkEmitter *ORE) {
1171 assert(((CurAST != nullptr) ^ (MSSAU != nullptr)) &&(static_cast <bool> (((CurAST != nullptr) ^ (MSSAU != nullptr
)) && "Either AliasSetTracker or MemorySSA should be initialized."
) ? void (0) : __assert_fail ("((CurAST != nullptr) ^ (MSSAU != nullptr)) && \"Either AliasSetTracker or MemorySSA should be initialized.\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1172, __extension__ __PRETTY_FUNCTION__))
1
Assuming pointer value is null
2
Assuming the condition is true
3
'?' condition is true
1172 "Either AliasSetTracker or MemorySSA should be initialized.")(static_cast <bool> (((CurAST != nullptr) ^ (MSSAU != nullptr
)) && "Either AliasSetTracker or MemorySSA should be initialized."
) ? void (0) : __assert_fail ("((CurAST != nullptr) ^ (MSSAU != nullptr)) && \"Either AliasSetTracker or MemorySSA should be initialized.\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1172, __extension__ __PRETTY_FUNCTION__))
;
1173
1174 // If we don't understand the instruction, bail early.
1175 if (!isHoistableAndSinkableInst(I))
4
Calling 'isHoistableAndSinkableInst'
7
Returning from 'isHoistableAndSinkableInst'
8
Taking false branch
1176 return false;
1177
1178 MemorySSA *MSSA = MSSAU
8.1
'MSSAU' is non-null
8.1
'MSSAU' is non-null
8.1
'MSSAU' is non-null
8.1
'MSSAU' is non-null
8.1
'MSSAU' is non-null
? MSSAU->getMemorySSA() : nullptr;
9
'?' condition is true
10
'MSSA' initialized here
1179 if (MSSA)
11
Assuming 'MSSA' is null
12
Taking false branch
1180 assert(Flags != nullptr && "Flags cannot be null.")(static_cast <bool> (Flags != nullptr && "Flags cannot be null."
) ? void (0) : __assert_fail ("Flags != nullptr && \"Flags cannot be null.\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1180, __extension__ __PRETTY_FUNCTION__))
;
1181
1182 // Loads have extra constraints we have to verify before we can hoist them.
1183 if (LoadInst *LI
13.1
'LI' is null
13.1
'LI' is null
13.1
'LI' is null
13.1
'LI' is null
13.1
'LI' is null
= dyn_cast<LoadInst>(&I)) {
13
Assuming the object is not a 'LoadInst'
14
Taking false branch
1184 if (!LI->isUnordered())
1185 return false; // Don't sink/hoist volatile or ordered atomic loads!
1186
1187 // Loads from constant memory are always safe to move, even if they end up
1188 // in the same alias set as something that ends up being modified.
1189 if (AA->pointsToConstantMemory(LI->getOperand(0)))
1190 return true;
1191 if (LI->hasMetadata(LLVMContext::MD_invariant_load))
1192 return true;
1193
1194 if (LI->isAtomic() && !TargetExecutesOncePerLoop)
1195 return false; // Don't risk duplicating unordered loads
1196
1197 // This checks for an invariant.start dominating the load.
1198 if (isLoadInvariantInLoop(LI, DT, CurLoop))
1199 return true;
1200
1201 // Stores with an invariant.group metadata are ok to sink/hoist.
1202 if (LI->hasMetadata(LLVMContext::MD_invariant_group))
1203 return true;
1204
1205 bool Invalidated;
1206 if (CurAST)
1207 Invalidated = pointerInvalidatedByLoop(MemoryLocation::get(LI), CurAST,
1208 CurLoop, AA);
1209 else
1210 Invalidated = pointerInvalidatedByLoopWithMSSA(
1211 MSSA, cast<MemoryUse>(MSSA->getMemoryAccess(LI)), CurLoop, I, *Flags);
1212 // Check loop-invariant address because this may also be a sinkable load
1213 // whose address is not necessarily loop-invariant.
1214 if (ORE && Invalidated && CurLoop->isLoopInvariant(LI->getPointerOperand()))
1215 ORE->emit([&]() {
1216 return OptimizationRemarkMissed(
1217 DEBUG_TYPE"licm", "LoadWithLoopInvariantAddressInvalidated", LI)
1218 << "failed to move load with loop-invariant address "
1219 "because the loop may invalidate its value";
1220 });
1221
1222 return !Invalidated;
1223 } else if (CallInst *CI
15.1
'CI' is non-null
15.1
'CI' is non-null
15.1
'CI' is non-null
15.1
'CI' is non-null
15.1
'CI' is non-null
= dyn_cast<CallInst>(&I)) {
15
Assuming the object is a 'CallInst'
16
Taking true branch
1224 // Don't sink or hoist dbg info; it's legal, but not useful.
1225 if (isa<DbgInfoIntrinsic>(I))
17
Assuming 'I' is not a 'DbgInfoIntrinsic'
18
Taking false branch
1226 return false;
1227
1228 // Don't sink calls which can throw.
1229 if (CI->mayThrow())
19
Assuming the condition is false
20
Taking false branch
1230 return false;
1231
1232 // Convergent attribute has been used on operations that involve
1233 // inter-thread communication which results are implicitly affected by the
1234 // enclosing control flows. It is not safe to hoist or sink such operations
1235 // across control flow.
1236 if (CI->isConvergent())
21
Calling 'CallBase::isConvergent'
37
Returning from 'CallBase::isConvergent'
38
Assuming the condition is false
39
Taking false branch
1237 return false;
1238
1239 using namespace PatternMatch;
1240 if (match(CI, m_Intrinsic<Intrinsic::assume>()))
40
Calling 'match<llvm::CallInst, llvm::PatternMatch::IntrinsicID_match>'
47
Returning from 'match<llvm::CallInst, llvm::PatternMatch::IntrinsicID_match>'
48
Taking false branch
1241 // Assumes don't actually alias anything or throw
1242 return true;
1243
1244 if (match(CI, m_Intrinsic<Intrinsic::experimental_widenable_condition>()))
49
Calling 'match<llvm::CallInst, llvm::PatternMatch::IntrinsicID_match>'
56
Returning from 'match<llvm::CallInst, llvm::PatternMatch::IntrinsicID_match>'
57
Taking false branch
1245 // Widenable conditions don't actually alias anything or throw
1246 return true;
1247
1248 // Handle simple cases by querying alias analysis.
1249 FunctionModRefBehavior Behavior = AA->getModRefBehavior(CI);
1250 if (Behavior == FMRB_DoesNotAccessMemory)
58
Assuming 'Behavior' is not equal to FMRB_DoesNotAccessMemory
59
Taking false branch
1251 return true;
1252 if (AAResults::onlyReadsMemory(Behavior)) {
60
Calling 'AAResults::onlyReadsMemory'
63
Returning from 'AAResults::onlyReadsMemory'
64
Taking true branch
1253 // A readonly argmemonly function only reads from memory pointed to by
1254 // it's arguments with arbitrary offsets. If we can prove there are no
1255 // writes to this memory in the loop, we can hoist or sink.
1256 if (AAResults::onlyAccessesArgPointees(Behavior)) {
65
Calling 'AAResults::onlyAccessesArgPointees'
68
Returning from 'AAResults::onlyAccessesArgPointees'
69
Taking true branch
1257 // TODO: expand to writeable arguments
1258 for (Value *Op : CI->arg_operands())
70
Assuming '__begin5' is not equal to '__end5'
1259 if (Op->getType()->isPointerTy()) {
71
Calling 'Type::isPointerTy'
74
Returning from 'Type::isPointerTy'
75
Taking true branch
1260 bool Invalidated;
1261 if (CurAST
75.1
'CurAST' is null
75.1
'CurAST' is null
75.1
'CurAST' is null
75.1
'CurAST' is null
75.1
'CurAST' is null
)
76
Taking false branch
1262 Invalidated = pointerInvalidatedByLoop(
1263 MemoryLocation::getBeforeOrAfter(Op), CurAST, CurLoop, AA);
1264 else
1265 Invalidated = pointerInvalidatedByLoopWithMSSA(
1266 MSSA, cast<MemoryUse>(MSSA->getMemoryAccess(CI)), CurLoop, I,
77
Called C++ object pointer is null
1267 *Flags);
1268 if (Invalidated)
1269 return false;
1270 }
1271 return true;
1272 }
1273
1274 // If this call only reads from memory and there are no writes to memory
1275 // in the loop, we can hoist or sink the call as appropriate.
1276 if (isReadOnly(CurAST, MSSAU, CurLoop))
1277 return true;
1278 }
1279
1280 // FIXME: This should use mod/ref information to see if we can hoist or
1281 // sink the call.
1282
1283 return false;
1284 } else if (auto *FI = dyn_cast<FenceInst>(&I)) {
1285 // Fences alias (most) everything to provide ordering. For the moment,
1286 // just give up if there are any other memory operations in the loop.
1287 if (CurAST) {
1288 auto Begin = CurAST->begin();
1289 assert(Begin != CurAST->end() && "must contain FI")(static_cast <bool> (Begin != CurAST->end() &&
"must contain FI") ? void (0) : __assert_fail ("Begin != CurAST->end() && \"must contain FI\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1289, __extension__ __PRETTY_FUNCTION__))
;
1290 if (std::next(Begin) != CurAST->end())
1291 // constant memory for instance, TODO: handle better
1292 return false;
1293 auto *UniqueI = Begin->getUniqueInstruction();
1294 if (!UniqueI)
1295 // other memory op, give up
1296 return false;
1297 (void)FI; // suppress unused variable warning
1298 assert(UniqueI == FI && "AS must contain FI")(static_cast <bool> (UniqueI == FI && "AS must contain FI"
) ? void (0) : __assert_fail ("UniqueI == FI && \"AS must contain FI\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1298, __extension__ __PRETTY_FUNCTION__))
;
1299 return true;
1300 } else // MSSAU
1301 return isOnlyMemoryAccess(FI, CurLoop, MSSAU);
1302 } else if (auto *SI = dyn_cast<StoreInst>(&I)) {
1303 if (!SI->isUnordered())
1304 return false; // Don't sink/hoist volatile or ordered atomic store!
1305
1306 // We can only hoist a store that we can prove writes a value which is not
1307 // read or overwritten within the loop. For those cases, we fallback to
1308 // load store promotion instead. TODO: We can extend this to cases where
1309 // there is exactly one write to the location and that write dominates an
1310 // arbitrary number of reads in the loop.
1311 if (CurAST) {
1312 auto &AS = CurAST->getAliasSetFor(MemoryLocation::get(SI));
1313
1314 if (AS.isRef() || !AS.isMustAlias())
1315 // Quick exit test, handled by the full path below as well.
1316 return false;
1317 auto *UniqueI = AS.getUniqueInstruction();
1318 if (!UniqueI)
1319 // other memory op, give up
1320 return false;
1321 assert(UniqueI == SI && "AS must contain SI")(static_cast <bool> (UniqueI == SI && "AS must contain SI"
) ? void (0) : __assert_fail ("UniqueI == SI && \"AS must contain SI\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1321, __extension__ __PRETTY_FUNCTION__))
;
1322 return true;
1323 } else { // MSSAU
1324 if (isOnlyMemoryAccess(SI, CurLoop, MSSAU))
1325 return true;
1326 // If there are more accesses than the Promotion cap or no "quota" to
1327 // check clobber, then give up as we're not walking a list that long.
1328 if (Flags->tooManyMemoryAccesses() || Flags->tooManyClobberingCalls())
1329 return false;
1330 // If there are interfering Uses (i.e. their defining access is in the
1331 // loop), or ordered loads (stored as Defs!), don't move this store.
1332 // Could do better here, but this is conservatively correct.
1333 // TODO: Cache set of Uses on the first walk in runOnLoop, update when
1334 // moving accesses. Can also extend to dominating uses.
1335 auto *SIMD = MSSA->getMemoryAccess(SI);
1336 for (auto *BB : CurLoop->getBlocks())
1337 if (auto *Accesses = MSSA->getBlockAccesses(BB)) {
1338 for (const auto &MA : *Accesses)
1339 if (const auto *MU = dyn_cast<MemoryUse>(&MA)) {
1340 auto *MD = MU->getDefiningAccess();
1341 if (!MSSA->isLiveOnEntryDef(MD) &&
1342 CurLoop->contains(MD->getBlock()))
1343 return false;
1344 // Disable hoisting past potentially interfering loads. Optimized
1345 // Uses may point to an access outside the loop, as getClobbering
1346 // checks the previous iteration when walking the backedge.
1347 // FIXME: More precise: no Uses that alias SI.
1348 if (!Flags->getIsSink() && !MSSA->dominates(SIMD, MU))
1349 return false;
1350 } else if (const auto *MD = dyn_cast<MemoryDef>(&MA)) {
1351 if (auto *LI = dyn_cast<LoadInst>(MD->getMemoryInst())) {
1352 (void)LI; // Silence warning.
1353 assert(!LI->isUnordered() && "Expected unordered load")(static_cast <bool> (!LI->isUnordered() && "Expected unordered load"
) ? void (0) : __assert_fail ("!LI->isUnordered() && \"Expected unordered load\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1353, __extension__ __PRETTY_FUNCTION__))
;
1354 return false;
1355 }
1356 // Any call, while it may not be clobbering SI, it may be a use.
1357 if (auto *CI = dyn_cast<CallInst>(MD->getMemoryInst())) {
1358 // Check if the call may read from the memory location written
1359 // to by SI. Check CI's attributes and arguments; the number of
1360 // such checks performed is limited above by NoOfMemAccTooLarge.
1361 ModRefInfo MRI = AA->getModRefInfo(CI, MemoryLocation::get(SI));
1362 if (isModOrRefSet(MRI))
1363 return false;
1364 }
1365 }
1366 }
1367 auto *Source = MSSA->getSkipSelfWalker()->getClobberingMemoryAccess(SI);
1368 Flags->incrementClobberingCalls();
1369 // If there are no clobbering Defs in the loop, store is safe to hoist.
1370 return MSSA->isLiveOnEntryDef(Source) ||
1371 !CurLoop->contains(Source->getBlock());
1372 }
1373 }
1374
1375 assert(!I.mayReadOrWriteMemory() && "unhandled aliasing")(static_cast <bool> (!I.mayReadOrWriteMemory() &&
"unhandled aliasing") ? void (0) : __assert_fail ("!I.mayReadOrWriteMemory() && \"unhandled aliasing\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1375, __extension__ __PRETTY_FUNCTION__))
;
1376
1377 // We've established mechanical ability and aliasing, it's up to the caller
1378 // to check fault safety
1379 return true;
1380}
1381
1382/// Returns true if a PHINode is a trivially replaceable with an
1383/// Instruction.
1384/// This is true when all incoming values are that instruction.
1385/// This pattern occurs most often with LCSSA PHI nodes.
1386///
1387static bool isTriviallyReplaceablePHI(const PHINode &PN, const Instruction &I) {
1388 for (const Value *IncValue : PN.incoming_values())
1389 if (IncValue != &I)
1390 return false;
1391
1392 return true;
1393}
1394
1395/// Return true if the instruction is free in the loop.
1396static bool isFreeInLoop(const Instruction &I, const Loop *CurLoop,
1397 const TargetTransformInfo *TTI) {
1398
1399 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) {
1400 if (TTI->getUserCost(GEP, TargetTransformInfo::TCK_SizeAndLatency) !=
1401 TargetTransformInfo::TCC_Free)
1402 return false;
1403 // For a GEP, we cannot simply use getUserCost because currently it
1404 // optimistically assume that a GEP will fold into addressing mode
1405 // regardless of its users.
1406 const BasicBlock *BB = GEP->getParent();
1407 for (const User *U : GEP->users()) {
1408 const Instruction *UI = cast<Instruction>(U);
1409 if (CurLoop->contains(UI) &&
1410 (BB != UI->getParent() ||
1411 (!isa<StoreInst>(UI) && !isa<LoadInst>(UI))))
1412 return false;
1413 }
1414 return true;
1415 } else
1416 return TTI->getUserCost(&I, TargetTransformInfo::TCK_SizeAndLatency) ==
1417 TargetTransformInfo::TCC_Free;
1418}
1419
1420/// Return true if the only users of this instruction are outside of
1421/// the loop. If this is true, we can sink the instruction to the exit
1422/// blocks of the loop.
1423///
1424/// We also return true if the instruction could be folded away in lowering.
1425/// (e.g., a GEP can be folded into a load as an addressing mode in the loop).
1426static bool isNotUsedOrFreeInLoop(const Instruction &I, const Loop *CurLoop,
1427 const LoopSafetyInfo *SafetyInfo,
1428 TargetTransformInfo *TTI, bool &FreeInLoop) {
1429 const auto &BlockColors = SafetyInfo->getBlockColors();
1430 bool IsFree = isFreeInLoop(I, CurLoop, TTI);
1431 for (const User *U : I.users()) {
1432 const Instruction *UI = cast<Instruction>(U);
1433 if (const PHINode *PN = dyn_cast<PHINode>(UI)) {
1434 const BasicBlock *BB = PN->getParent();
1435 // We cannot sink uses in catchswitches.
1436 if (isa<CatchSwitchInst>(BB->getTerminator()))
1437 return false;
1438
1439 // We need to sink a callsite to a unique funclet. Avoid sinking if the
1440 // phi use is too muddled.
1441 if (isa<CallInst>(I))
1442 if (!BlockColors.empty() &&
1443 BlockColors.find(const_cast<BasicBlock *>(BB))->second.size() != 1)
1444 return false;
1445 }
1446
1447 if (CurLoop->contains(UI)) {
1448 if (IsFree) {
1449 FreeInLoop = true;
1450 continue;
1451 }
1452 return false;
1453 }
1454 }
1455 return true;
1456}
1457
1458static Instruction *cloneInstructionInExitBlock(
1459 Instruction &I, BasicBlock &ExitBlock, PHINode &PN, const LoopInfo *LI,
1460 const LoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU) {
1461 Instruction *New;
1462 if (auto *CI = dyn_cast<CallInst>(&I)) {
1463 const auto &BlockColors = SafetyInfo->getBlockColors();
1464
1465 // Sinking call-sites need to be handled differently from other
1466 // instructions. The cloned call-site needs a funclet bundle operand
1467 // appropriate for its location in the CFG.
1468 SmallVector<OperandBundleDef, 1> OpBundles;
1469 for (unsigned BundleIdx = 0, BundleEnd = CI->getNumOperandBundles();
1470 BundleIdx != BundleEnd; ++BundleIdx) {
1471 OperandBundleUse Bundle = CI->getOperandBundleAt(BundleIdx);
1472 if (Bundle.getTagID() == LLVMContext::OB_funclet)
1473 continue;
1474
1475 OpBundles.emplace_back(Bundle);
1476 }
1477
1478 if (!BlockColors.empty()) {
1479 const ColorVector &CV = BlockColors.find(&ExitBlock)->second;
1480 assert(CV.size() == 1 && "non-unique color for exit block!")(static_cast <bool> (CV.size() == 1 && "non-unique color for exit block!"
) ? void (0) : __assert_fail ("CV.size() == 1 && \"non-unique color for exit block!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1480, __extension__ __PRETTY_FUNCTION__))
;
1481 BasicBlock *BBColor = CV.front();
1482 Instruction *EHPad = BBColor->getFirstNonPHI();
1483 if (EHPad->isEHPad())
1484 OpBundles.emplace_back("funclet", EHPad);
1485 }
1486
1487 New = CallInst::Create(CI, OpBundles);
1488 } else {
1489 New = I.clone();
1490 }
1491
1492 ExitBlock.getInstList().insert(ExitBlock.getFirstInsertionPt(), New);
1493 if (!I.getName().empty())
1494 New->setName(I.getName() + ".le");
1495
1496 if (MSSAU && MSSAU->getMemorySSA()->getMemoryAccess(&I)) {
1497 // Create a new MemoryAccess and let MemorySSA set its defining access.
1498 MemoryAccess *NewMemAcc = MSSAU->createMemoryAccessInBB(
1499 New, nullptr, New->getParent(), MemorySSA::Beginning);
1500 if (NewMemAcc) {
1501 if (auto *MemDef = dyn_cast<MemoryDef>(NewMemAcc))
1502 MSSAU->insertDef(MemDef, /*RenameUses=*/true);
1503 else {
1504 auto *MemUse = cast<MemoryUse>(NewMemAcc);
1505 MSSAU->insertUse(MemUse, /*RenameUses=*/true);
1506 }
1507 }
1508 }
1509
1510 // Build LCSSA PHI nodes for any in-loop operands (if legal). Note that
1511 // this is particularly cheap because we can rip off the PHI node that we're
1512 // replacing for the number and blocks of the predecessors.
1513 // OPT: If this shows up in a profile, we can instead finish sinking all
1514 // invariant instructions, and then walk their operands to re-establish
1515 // LCSSA. That will eliminate creating PHI nodes just to nuke them when
1516 // sinking bottom-up.
1517 for (Use &Op : New->operands())
1518 if (LI->wouldBeOutOfLoopUseRequiringLCSSA(Op.get(), PN.getParent())) {
1519 auto *OInst = cast<Instruction>(Op.get());
1520 PHINode *OpPN =
1521 PHINode::Create(OInst->getType(), PN.getNumIncomingValues(),
1522 OInst->getName() + ".lcssa", &ExitBlock.front());
1523 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
1524 OpPN->addIncoming(OInst, PN.getIncomingBlock(i));
1525 Op = OpPN;
1526 }
1527 return New;
1528}
1529
1530static void eraseInstruction(Instruction &I, ICFLoopSafetyInfo &SafetyInfo,
1531 AliasSetTracker *AST, MemorySSAUpdater *MSSAU) {
1532 if (AST)
1533 AST->deleteValue(&I);
1534 if (MSSAU)
1535 MSSAU->removeMemoryAccess(&I);
1536 SafetyInfo.removeInstruction(&I);
1537 I.eraseFromParent();
1538}
1539
1540static void moveInstructionBefore(Instruction &I, Instruction &Dest,
1541 ICFLoopSafetyInfo &SafetyInfo,
1542 MemorySSAUpdater *MSSAU,
1543 ScalarEvolution *SE) {
1544 SafetyInfo.removeInstruction(&I);
1545 SafetyInfo.insertInstructionTo(&I, Dest.getParent());
1546 I.moveBefore(&Dest);
1547 if (MSSAU)
1548 if (MemoryUseOrDef *OldMemAcc = cast_or_null<MemoryUseOrDef>(
1549 MSSAU->getMemorySSA()->getMemoryAccess(&I)))
1550 MSSAU->moveToPlace(OldMemAcc, Dest.getParent(),
1551 MemorySSA::BeforeTerminator);
1552 if (SE)
1553 SE->forgetValue(&I);
1554}
1555
1556static Instruction *sinkThroughTriviallyReplaceablePHI(
1557 PHINode *TPN, Instruction *I, LoopInfo *LI,
1558 SmallDenseMap<BasicBlock *, Instruction *, 32> &SunkCopies,
1559 const LoopSafetyInfo *SafetyInfo, const Loop *CurLoop,
1560 MemorySSAUpdater *MSSAU) {
1561 assert(isTriviallyReplaceablePHI(*TPN, *I) &&(static_cast <bool> (isTriviallyReplaceablePHI(*TPN, *I
) && "Expect only trivially replaceable PHI") ? void (
0) : __assert_fail ("isTriviallyReplaceablePHI(*TPN, *I) && \"Expect only trivially replaceable PHI\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1562, __extension__ __PRETTY_FUNCTION__))
1562 "Expect only trivially replaceable PHI")(static_cast <bool> (isTriviallyReplaceablePHI(*TPN, *I
) && "Expect only trivially replaceable PHI") ? void (
0) : __assert_fail ("isTriviallyReplaceablePHI(*TPN, *I) && \"Expect only trivially replaceable PHI\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1562, __extension__ __PRETTY_FUNCTION__))
;
1563 BasicBlock *ExitBlock = TPN->getParent();
1564 Instruction *New;
1565 auto It = SunkCopies.find(ExitBlock);
1566 if (It != SunkCopies.end())
1567 New = It->second;
1568 else
1569 New = SunkCopies[ExitBlock] = cloneInstructionInExitBlock(
1570 *I, *ExitBlock, *TPN, LI, SafetyInfo, MSSAU);
1571 return New;
1572}
1573
1574static bool canSplitPredecessors(PHINode *PN, LoopSafetyInfo *SafetyInfo) {
1575 BasicBlock *BB = PN->getParent();
1576 if (!BB->canSplitPredecessors())
1577 return false;
1578 // It's not impossible to split EHPad blocks, but if BlockColors already exist
1579 // it require updating BlockColors for all offspring blocks accordingly. By
1580 // skipping such corner case, we can make updating BlockColors after splitting
1581 // predecessor fairly simple.
1582 if (!SafetyInfo->getBlockColors().empty() && BB->getFirstNonPHI()->isEHPad())
1583 return false;
1584 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
1585 BasicBlock *BBPred = *PI;
1586 if (isa<IndirectBrInst>(BBPred->getTerminator()) ||
1587 isa<CallBrInst>(BBPred->getTerminator()))
1588 return false;
1589 }
1590 return true;
1591}
1592
1593static void splitPredecessorsOfLoopExit(PHINode *PN, DominatorTree *DT,
1594 LoopInfo *LI, const Loop *CurLoop,
1595 LoopSafetyInfo *SafetyInfo,
1596 MemorySSAUpdater *MSSAU) {
1597#ifndef NDEBUG
1598 SmallVector<BasicBlock *, 32> ExitBlocks;
1599 CurLoop->getUniqueExitBlocks(ExitBlocks);
1600 SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(),
1601 ExitBlocks.end());
1602#endif
1603 BasicBlock *ExitBB = PN->getParent();
1604 assert(ExitBlockSet.count(ExitBB) && "Expect the PHI is in an exit block.")(static_cast <bool> (ExitBlockSet.count(ExitBB) &&
"Expect the PHI is in an exit block.") ? void (0) : __assert_fail
("ExitBlockSet.count(ExitBB) && \"Expect the PHI is in an exit block.\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1604, __extension__ __PRETTY_FUNCTION__))
;
1605
1606 // Split predecessors of the loop exit to make instructions in the loop are
1607 // exposed to exit blocks through trivially replaceable PHIs while keeping the
1608 // loop in the canonical form where each predecessor of each exit block should
1609 // be contained within the loop. For example, this will convert the loop below
1610 // from
1611 //
1612 // LB1:
1613 // %v1 =
1614 // br %LE, %LB2
1615 // LB2:
1616 // %v2 =
1617 // br %LE, %LB1
1618 // LE:
1619 // %p = phi [%v1, %LB1], [%v2, %LB2] <-- non-trivially replaceable
1620 //
1621 // to
1622 //
1623 // LB1:
1624 // %v1 =
1625 // br %LE.split, %LB2
1626 // LB2:
1627 // %v2 =
1628 // br %LE.split2, %LB1
1629 // LE.split:
1630 // %p1 = phi [%v1, %LB1] <-- trivially replaceable
1631 // br %LE
1632 // LE.split2:
1633 // %p2 = phi [%v2, %LB2] <-- trivially replaceable
1634 // br %LE
1635 // LE:
1636 // %p = phi [%p1, %LE.split], [%p2, %LE.split2]
1637 //
1638 const auto &BlockColors = SafetyInfo->getBlockColors();
1639 SmallSetVector<BasicBlock *, 8> PredBBs(pred_begin(ExitBB), pred_end(ExitBB));
1640 while (!PredBBs.empty()) {
1641 BasicBlock *PredBB = *PredBBs.begin();
1642 assert(CurLoop->contains(PredBB) &&(static_cast <bool> (CurLoop->contains(PredBB) &&
"Expect all predecessors are in the loop") ? void (0) : __assert_fail
("CurLoop->contains(PredBB) && \"Expect all predecessors are in the loop\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1643, __extension__ __PRETTY_FUNCTION__))
1643 "Expect all predecessors are in the loop")(static_cast <bool> (CurLoop->contains(PredBB) &&
"Expect all predecessors are in the loop") ? void (0) : __assert_fail
("CurLoop->contains(PredBB) && \"Expect all predecessors are in the loop\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1643, __extension__ __PRETTY_FUNCTION__))
;
1644 if (PN->getBasicBlockIndex(PredBB) >= 0) {
1645 BasicBlock *NewPred = SplitBlockPredecessors(
1646 ExitBB, PredBB, ".split.loop.exit", DT, LI, MSSAU, true);
1647 // Since we do not allow splitting EH-block with BlockColors in
1648 // canSplitPredecessors(), we can simply assign predecessor's color to
1649 // the new block.
1650 if (!BlockColors.empty())
1651 // Grab a reference to the ColorVector to be inserted before getting the
1652 // reference to the vector we are copying because inserting the new
1653 // element in BlockColors might cause the map to be reallocated.
1654 SafetyInfo->copyColors(NewPred, PredBB);
1655 }
1656 PredBBs.remove(PredBB);
1657 }
1658}
1659
1660/// When an instruction is found to only be used outside of the loop, this
1661/// function moves it to the exit blocks and patches up SSA form as needed.
1662/// This method is guaranteed to remove the original instruction from its
1663/// position, and may either delete it or move it to outside of the loop.
1664///
1665static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT,
1666 BlockFrequencyInfo *BFI, const Loop *CurLoop,
1667 ICFLoopSafetyInfo *SafetyInfo, MemorySSAUpdater *MSSAU,
1668 OptimizationRemarkEmitter *ORE) {
1669 bool Changed = false;
1670 LLVM_DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM sinking instruction: " <<
I << "\n"; } } while (false)
;
1671
1672 // Iterate over users to be ready for actual sinking. Replace users via
1673 // unreachable blocks with undef and make all user PHIs trivially replaceable.
1674 SmallPtrSet<Instruction *, 8> VisitedUsers;
1675 for (Value::user_iterator UI = I.user_begin(), UE = I.user_end(); UI != UE;) {
1676 auto *User = cast<Instruction>(*UI);
1677 Use &U = UI.getUse();
1678 ++UI;
1679
1680 if (VisitedUsers.count(User) || CurLoop->contains(User))
1681 continue;
1682
1683 if (!DT->isReachableFromEntry(User->getParent())) {
1684 U = UndefValue::get(I.getType());
1685 Changed = true;
1686 continue;
1687 }
1688
1689 // The user must be a PHI node.
1690 PHINode *PN = cast<PHINode>(User);
1691
1692 // Surprisingly, instructions can be used outside of loops without any
1693 // exits. This can only happen in PHI nodes if the incoming block is
1694 // unreachable.
1695 BasicBlock *BB = PN->getIncomingBlock(U);
1696 if (!DT->isReachableFromEntry(BB)) {
1697 U = UndefValue::get(I.getType());
1698 Changed = true;
1699 continue;
1700 }
1701
1702 VisitedUsers.insert(PN);
1703 if (isTriviallyReplaceablePHI(*PN, I))
1704 continue;
1705
1706 if (!canSplitPredecessors(PN, SafetyInfo))
1707 return Changed;
1708
1709 // Split predecessors of the PHI so that we can make users trivially
1710 // replaceable.
1711 splitPredecessorsOfLoopExit(PN, DT, LI, CurLoop, SafetyInfo, MSSAU);
1712
1713 // Should rebuild the iterators, as they may be invalidated by
1714 // splitPredecessorsOfLoopExit().
1715 UI = I.user_begin();
1716 UE = I.user_end();
1717 }
1718
1719 if (VisitedUsers.empty())
1720 return Changed;
1721
1722 ORE->emit([&]() {
1723 return OptimizationRemark(DEBUG_TYPE"licm", "InstSunk", &I)
1724 << "sinking " << ore::NV("Inst", &I);
1725 });
1726 if (isa<LoadInst>(I))
1727 ++NumMovedLoads;
1728 else if (isa<CallInst>(I))
1729 ++NumMovedCalls;
1730 ++NumSunk;
1731
1732#ifndef NDEBUG
1733 SmallVector<BasicBlock *, 32> ExitBlocks;
1734 CurLoop->getUniqueExitBlocks(ExitBlocks);
1735 SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(),
1736 ExitBlocks.end());
1737#endif
1738
1739 // Clones of this instruction. Don't create more than one per exit block!
1740 SmallDenseMap<BasicBlock *, Instruction *, 32> SunkCopies;
1741
1742 // If this instruction is only used outside of the loop, then all users are
1743 // PHI nodes in exit blocks due to LCSSA form. Just RAUW them with clones of
1744 // the instruction.
1745 // First check if I is worth sinking for all uses. Sink only when it is worth
1746 // across all uses.
1747 SmallSetVector<User*, 8> Users(I.user_begin(), I.user_end());
1748 SmallVector<PHINode *, 8> ExitPNs;
1749 for (auto *UI : Users) {
1750 auto *User = cast<Instruction>(UI);
1751
1752 if (CurLoop->contains(User))
1753 continue;
1754
1755 PHINode *PN = cast<PHINode>(User);
1756 assert(ExitBlockSet.count(PN->getParent()) &&(static_cast <bool> (ExitBlockSet.count(PN->getParent
()) && "The LCSSA PHI is not in an exit block!") ? void
(0) : __assert_fail ("ExitBlockSet.count(PN->getParent()) && \"The LCSSA PHI is not in an exit block!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1757, __extension__ __PRETTY_FUNCTION__))
1757 "The LCSSA PHI is not in an exit block!")(static_cast <bool> (ExitBlockSet.count(PN->getParent
()) && "The LCSSA PHI is not in an exit block!") ? void
(0) : __assert_fail ("ExitBlockSet.count(PN->getParent()) && \"The LCSSA PHI is not in an exit block!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 1757, __extension__ __PRETTY_FUNCTION__))
;
1758 if (!worthSinkOrHoistInst(I, PN->getParent(), ORE, BFI)) {
1759 return Changed;
1760 }
1761
1762 ExitPNs.push_back(PN);
1763 }
1764
1765 for (auto *PN : ExitPNs) {
1766
1767 // The PHI must be trivially replaceable.
1768 Instruction *New = sinkThroughTriviallyReplaceablePHI(
1769 PN, &I, LI, SunkCopies, SafetyInfo, CurLoop, MSSAU);
1770 PN->replaceAllUsesWith(New);
1771 eraseInstruction(*PN, *SafetyInfo, nullptr, nullptr);
1772 Changed = true;
1773 }
1774 return Changed;
1775}
1776
1777/// When an instruction is found to only use loop invariant operands that
1778/// is safe to hoist, this instruction is called to do the dirty work.
1779///
1780static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop,
1781 BasicBlock *Dest, ICFLoopSafetyInfo *SafetyInfo,
1782 MemorySSAUpdater *MSSAU, ScalarEvolution *SE,
1783 OptimizationRemarkEmitter *ORE) {
1784 LLVM_DEBUG(dbgs() << "LICM hoisting to " << Dest->getNameOrAsOperand() << ": "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM hoisting to " << Dest
->getNameOrAsOperand() << ": " << I << "\n"
; } } while (false)
1785 << I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM hoisting to " << Dest
->getNameOrAsOperand() << ": " << I << "\n"
; } } while (false)
;
1786 ORE->emit([&]() {
1787 return OptimizationRemark(DEBUG_TYPE"licm", "Hoisted", &I) << "hoisting "
1788 << ore::NV("Inst", &I);
1789 });
1790
1791 // Metadata can be dependent on conditions we are hoisting above.
1792 // Conservatively strip all metadata on the instruction unless we were
1793 // guaranteed to execute I if we entered the loop, in which case the metadata
1794 // is valid in the loop preheader.
1795 if (I.hasMetadataOtherThanDebugLoc() &&
1796 // The check on hasMetadataOtherThanDebugLoc is to prevent us from burning
1797 // time in isGuaranteedToExecute if we don't actually have anything to
1798 // drop. It is a compile time optimization, not required for correctness.
1799 !SafetyInfo->isGuaranteedToExecute(I, DT, CurLoop))
1800 I.dropUnknownNonDebugMetadata();
1801
1802 if (isa<PHINode>(I))
1803 // Move the new node to the end of the phi list in the destination block.
1804 moveInstructionBefore(I, *Dest->getFirstNonPHI(), *SafetyInfo, MSSAU, SE);
1805 else
1806 // Move the new node to the destination block, before its terminator.
1807 moveInstructionBefore(I, *Dest->getTerminator(), *SafetyInfo, MSSAU, SE);
1808
1809 I.updateLocationAfterHoist();
1810
1811 if (isa<LoadInst>(I))
1812 ++NumMovedLoads;
1813 else if (isa<CallInst>(I))
1814 ++NumMovedCalls;
1815 ++NumHoisted;
1816}
1817
1818/// Only sink or hoist an instruction if it is not a trapping instruction,
1819/// or if the instruction is known not to trap when moved to the preheader.
1820/// or if it is a trapping instruction and is guaranteed to execute.
1821static bool isSafeToExecuteUnconditionally(Instruction &Inst,
1822 const DominatorTree *DT,
1823 const TargetLibraryInfo *TLI,
1824 const Loop *CurLoop,
1825 const LoopSafetyInfo *SafetyInfo,
1826 OptimizationRemarkEmitter *ORE,
1827 const Instruction *CtxI) {
1828 if (isSafeToSpeculativelyExecute(&Inst, CtxI, DT, TLI))
1829 return true;
1830
1831 bool GuaranteedToExecute =
1832 SafetyInfo->isGuaranteedToExecute(Inst, DT, CurLoop);
1833
1834 if (!GuaranteedToExecute) {
1835 auto *LI = dyn_cast<LoadInst>(&Inst);
1836 if (LI && CurLoop->isLoopInvariant(LI->getPointerOperand()))
1837 ORE->emit([&]() {
1838 return OptimizationRemarkMissed(
1839 DEBUG_TYPE"licm", "LoadWithLoopInvariantAddressCondExecuted", LI)
1840 << "failed to hoist load with loop-invariant address "
1841 "because load is conditionally executed";
1842 });
1843 }
1844
1845 return GuaranteedToExecute;
1846}
1847
1848namespace {
1849class LoopPromoter : public LoadAndStorePromoter {
1850 Value *SomePtr; // Designated pointer to store to.
1851 const SmallSetVector<Value *, 8> &PointerMustAliases;
1852 SmallVectorImpl<BasicBlock *> &LoopExitBlocks;
1853 SmallVectorImpl<Instruction *> &LoopInsertPts;
1854 SmallVectorImpl<MemoryAccess *> &MSSAInsertPts;
1855 PredIteratorCache &PredCache;
1856 AliasSetTracker *AST;
1857 MemorySSAUpdater *MSSAU;
1858 LoopInfo &LI;
1859 DebugLoc DL;
1860 int Alignment;
1861 bool UnorderedAtomic;
1862 AAMDNodes AATags;
1863 ICFLoopSafetyInfo &SafetyInfo;
1864
1865 // We're about to add a use of V in a loop exit block. Insert an LCSSA phi
1866 // (if legal) if doing so would add an out-of-loop use to an instruction
1867 // defined in-loop.
1868 Value *maybeInsertLCSSAPHI(Value *V, BasicBlock *BB) const {
1869 if (!LI.wouldBeOutOfLoopUseRequiringLCSSA(V, BB))
1870 return V;
1871
1872 Instruction *I = cast<Instruction>(V);
1873 // We need to create an LCSSA PHI node for the incoming value and
1874 // store that.
1875 PHINode *PN = PHINode::Create(I->getType(), PredCache.size(BB),
1876 I->getName() + ".lcssa", &BB->front());
1877 for (BasicBlock *Pred : PredCache.get(BB))
1878 PN->addIncoming(I, Pred);
1879 return PN;
1880 }
1881
1882public:
1883 LoopPromoter(Value *SP, ArrayRef<const Instruction *> Insts, SSAUpdater &S,
1884 const SmallSetVector<Value *, 8> &PMA,
1885 SmallVectorImpl<BasicBlock *> &LEB,
1886 SmallVectorImpl<Instruction *> &LIP,
1887 SmallVectorImpl<MemoryAccess *> &MSSAIP, PredIteratorCache &PIC,
1888 AliasSetTracker *ast, MemorySSAUpdater *MSSAU, LoopInfo &li,
1889 DebugLoc dl, int alignment, bool UnorderedAtomic,
1890 const AAMDNodes &AATags, ICFLoopSafetyInfo &SafetyInfo)
1891 : LoadAndStorePromoter(Insts, S), SomePtr(SP), PointerMustAliases(PMA),
1892 LoopExitBlocks(LEB), LoopInsertPts(LIP), MSSAInsertPts(MSSAIP),
1893 PredCache(PIC), AST(ast), MSSAU(MSSAU), LI(li), DL(std::move(dl)),
1894 Alignment(alignment), UnorderedAtomic(UnorderedAtomic), AATags(AATags),
1895 SafetyInfo(SafetyInfo) {}
1896
1897 bool isInstInList(Instruction *I,
1898 const SmallVectorImpl<Instruction *> &) const override {
1899 Value *Ptr;
1900 if (LoadInst *LI = dyn_cast<LoadInst>(I))
1901 Ptr = LI->getOperand(0);
1902 else
1903 Ptr = cast<StoreInst>(I)->getPointerOperand();
1904 return PointerMustAliases.count(Ptr);
1905 }
1906
1907 void doExtraRewritesBeforeFinalDeletion() override {
1908 // Insert stores after in the loop exit blocks. Each exit block gets a
1909 // store of the live-out values that feed them. Since we've already told
1910 // the SSA updater about the defs in the loop and the preheader
1911 // definition, it is all set and we can start using it.
1912 for (unsigned i = 0, e = LoopExitBlocks.size(); i != e; ++i) {
1913 BasicBlock *ExitBlock = LoopExitBlocks[i];
1914 Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock);
1915 LiveInValue = maybeInsertLCSSAPHI(LiveInValue, ExitBlock);
1916 Value *Ptr = maybeInsertLCSSAPHI(SomePtr, ExitBlock);
1917 Instruction *InsertPos = LoopInsertPts[i];
1918 StoreInst *NewSI = new StoreInst(LiveInValue, Ptr, InsertPos);
1919 if (UnorderedAtomic)
1920 NewSI->setOrdering(AtomicOrdering::Unordered);
1921 NewSI->setAlignment(Align(Alignment));
1922 NewSI->setDebugLoc(DL);
1923 if (AATags)
1924 NewSI->setAAMetadata(AATags);
1925
1926 if (MSSAU) {
1927 MemoryAccess *MSSAInsertPoint = MSSAInsertPts[i];
1928 MemoryAccess *NewMemAcc;
1929 if (!MSSAInsertPoint) {
1930 NewMemAcc = MSSAU->createMemoryAccessInBB(
1931 NewSI, nullptr, NewSI->getParent(), MemorySSA::Beginning);
1932 } else {
1933 NewMemAcc =
1934 MSSAU->createMemoryAccessAfter(NewSI, nullptr, MSSAInsertPoint);
1935 }
1936 MSSAInsertPts[i] = NewMemAcc;
1937 MSSAU->insertDef(cast<MemoryDef>(NewMemAcc), true);
1938 // FIXME: true for safety, false may still be correct.
1939 }
1940 }
1941 }
1942
1943 void replaceLoadWithValue(LoadInst *LI, Value *V) const override {
1944 // Update alias analysis.
1945 if (AST)
1946 AST->copyValue(LI, V);
1947 }
1948 void instructionDeleted(Instruction *I) const override {
1949 SafetyInfo.removeInstruction(I);
1950 if (AST)
1951 AST->deleteValue(I);
1952 if (MSSAU)
1953 MSSAU->removeMemoryAccess(I);
1954 }
1955};
1956
1957bool isNotCapturedBeforeOrInLoop(const Value *V, const Loop *L,
1958 DominatorTree *DT) {
1959 // We can perform the captured-before check against any instruction in the
1960 // loop header, as the loop header is reachable from any instruction inside
1961 // the loop.
1962 // TODO: ReturnCaptures=true shouldn't be necessary here.
1963 return !PointerMayBeCapturedBefore(V, /* ReturnCaptures */ true,
1964 /* StoreCaptures */ true,
1965 L->getHeader()->getTerminator(), DT);
1966}
1967
1968/// Return true iff we can prove that a caller of this function can not inspect
1969/// the contents of the provided object in a well defined program.
1970bool isKnownNonEscaping(Value *Object, const Loop *L,
1971 const TargetLibraryInfo *TLI, DominatorTree *DT) {
1972 if (isa<AllocaInst>(Object))
1973 // Since the alloca goes out of scope, we know the caller can't retain a
1974 // reference to it and be well defined. Thus, we don't need to check for
1975 // capture.
1976 return true;
1977
1978 // For all other objects we need to know that the caller can't possibly
1979 // have gotten a reference to the object. There are two components of
1980 // that:
1981 // 1) Object can't be escaped by this function. This is what
1982 // PointerMayBeCaptured checks.
1983 // 2) Object can't have been captured at definition site. For this, we
1984 // need to know the return value is noalias. At the moment, we use a
1985 // weaker condition and handle only AllocLikeFunctions (which are
1986 // known to be noalias). TODO
1987 return isAllocLikeFn(Object, TLI) &&
1988 isNotCapturedBeforeOrInLoop(Object, L, DT);
1989}
1990
1991} // namespace
1992
1993/// Try to promote memory values to scalars by sinking stores out of the
1994/// loop and moving loads to before the loop. We do this by looping over
1995/// the stores in the loop, looking for stores to Must pointers which are
1996/// loop invariant.
1997///
1998bool llvm::promoteLoopAccessesToScalars(
1999 const SmallSetVector<Value *, 8> &PointerMustAliases,
2000 SmallVectorImpl<BasicBlock *> &ExitBlocks,
2001 SmallVectorImpl<Instruction *> &InsertPts,
2002 SmallVectorImpl<MemoryAccess *> &MSSAInsertPts, PredIteratorCache &PIC,
2003 LoopInfo *LI, DominatorTree *DT, const TargetLibraryInfo *TLI,
2004 Loop *CurLoop, AliasSetTracker *CurAST, MemorySSAUpdater *MSSAU,
2005 ICFLoopSafetyInfo *SafetyInfo, OptimizationRemarkEmitter *ORE) {
2006 // Verify inputs.
2007 assert(LI != nullptr && DT != nullptr && CurLoop != nullptr &&(static_cast <bool> (LI != nullptr && DT != nullptr
&& CurLoop != nullptr && SafetyInfo != nullptr
&& "Unexpected Input to promoteLoopAccessesToScalars"
) ? void (0) : __assert_fail ("LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected Input to promoteLoopAccessesToScalars\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 2009, __extension__ __PRETTY_FUNCTION__))
2008 SafetyInfo != nullptr &&(static_cast <bool> (LI != nullptr && DT != nullptr
&& CurLoop != nullptr && SafetyInfo != nullptr
&& "Unexpected Input to promoteLoopAccessesToScalars"
) ? void (0) : __assert_fail ("LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected Input to promoteLoopAccessesToScalars\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 2009, __extension__ __PRETTY_FUNCTION__))
2009 "Unexpected Input to promoteLoopAccessesToScalars")(static_cast <bool> (LI != nullptr && DT != nullptr
&& CurLoop != nullptr && SafetyInfo != nullptr
&& "Unexpected Input to promoteLoopAccessesToScalars"
) ? void (0) : __assert_fail ("LI != nullptr && DT != nullptr && CurLoop != nullptr && SafetyInfo != nullptr && \"Unexpected Input to promoteLoopAccessesToScalars\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 2009, __extension__ __PRETTY_FUNCTION__))
;
2010
2011 Value *SomePtr = *PointerMustAliases.begin();
2012 BasicBlock *Preheader = CurLoop->getLoopPreheader();
2013
2014 // It is not safe to promote a load/store from the loop if the load/store is
2015 // conditional. For example, turning:
2016 //
2017 // for () { if (c) *P += 1; }
2018 //
2019 // into:
2020 //
2021 // tmp = *P; for () { if (c) tmp +=1; } *P = tmp;
2022 //
2023 // is not safe, because *P may only be valid to access if 'c' is true.
2024 //
2025 // The safety property divides into two parts:
2026 // p1) The memory may not be dereferenceable on entry to the loop. In this
2027 // case, we can't insert the required load in the preheader.
2028 // p2) The memory model does not allow us to insert a store along any dynamic
2029 // path which did not originally have one.
2030 //
2031 // If at least one store is guaranteed to execute, both properties are
2032 // satisfied, and promotion is legal.
2033 //
2034 // This, however, is not a necessary condition. Even if no store/load is
2035 // guaranteed to execute, we can still establish these properties.
2036 // We can establish (p1) by proving that hoisting the load into the preheader
2037 // is safe (i.e. proving dereferenceability on all paths through the loop). We
2038 // can use any access within the alias set to prove dereferenceability,
2039 // since they're all must alias.
2040 //
2041 // There are two ways establish (p2):
2042 // a) Prove the location is thread-local. In this case the memory model
2043 // requirement does not apply, and stores are safe to insert.
2044 // b) Prove a store dominates every exit block. In this case, if an exit
2045 // blocks is reached, the original dynamic path would have taken us through
2046 // the store, so inserting a store into the exit block is safe. Note that this
2047 // is different from the store being guaranteed to execute. For instance,
2048 // if an exception is thrown on the first iteration of the loop, the original
2049 // store is never executed, but the exit blocks are not executed either.
2050
2051 bool DereferenceableInPH = false;
2052 bool SafeToInsertStore = false;
2053
2054 SmallVector<Instruction *, 64> LoopUses;
2055
2056 // We start with an alignment of one and try to find instructions that allow
2057 // us to prove better alignment.
2058 Align Alignment;
2059 // Keep track of which types of access we see
2060 bool SawUnorderedAtomic = false;
2061 bool SawNotAtomic = false;
2062 AAMDNodes AATags;
2063
2064 const DataLayout &MDL = Preheader->getModule()->getDataLayout();
2065
2066 bool IsKnownThreadLocalObject = false;
2067 if (SafetyInfo->anyBlockMayThrow()) {
2068 // If a loop can throw, we have to insert a store along each unwind edge.
2069 // That said, we can't actually make the unwind edge explicit. Therefore,
2070 // we have to prove that the store is dead along the unwind edge. We do
2071 // this by proving that the caller can't have a reference to the object
2072 // after return and thus can't possibly load from the object.
2073 Value *Object = getUnderlyingObject(SomePtr);
2074 if (!isKnownNonEscaping(Object, CurLoop, TLI, DT))
2075 return false;
2076 // Subtlety: Alloca's aren't visible to callers, but *are* potentially
2077 // visible to other threads if captured and used during their lifetimes.
2078 IsKnownThreadLocalObject = !isa<AllocaInst>(Object);
2079 }
2080
2081 // Check that all of the pointers in the alias set have the same type. We
2082 // cannot (yet) promote a memory location that is loaded and stored in
2083 // different sizes. While we are at it, collect alignment and AA info.
2084 for (Value *ASIV : PointerMustAliases) {
2085 // Check that all of the pointers in the alias set have the same type. We
2086 // cannot (yet) promote a memory location that is loaded and stored in
2087 // different sizes.
2088 if (SomePtr->getType() != ASIV->getType())
2089 return false;
2090
2091 for (User *U : ASIV->users()) {
2092 // Ignore instructions that are outside the loop.
2093 Instruction *UI = dyn_cast<Instruction>(U);
2094 if (!UI || !CurLoop->contains(UI))
2095 continue;
2096
2097 // If there is an non-load/store instruction in the loop, we can't promote
2098 // it.
2099 if (LoadInst *Load = dyn_cast<LoadInst>(UI)) {
2100 if (!Load->isUnordered())
2101 return false;
2102
2103 SawUnorderedAtomic |= Load->isAtomic();
2104 SawNotAtomic |= !Load->isAtomic();
2105
2106 Align InstAlignment = Load->getAlign();
2107
2108 // Note that proving a load safe to speculate requires proving
2109 // sufficient alignment at the target location. Proving it guaranteed
2110 // to execute does as well. Thus we can increase our guaranteed
2111 // alignment as well.
2112 if (!DereferenceableInPH || (InstAlignment > Alignment))
2113 if (isSafeToExecuteUnconditionally(*Load, DT, TLI, CurLoop,
2114 SafetyInfo, ORE,
2115 Preheader->getTerminator())) {
2116 DereferenceableInPH = true;
2117 Alignment = std::max(Alignment, InstAlignment);
2118 }
2119 } else if (const StoreInst *Store = dyn_cast<StoreInst>(UI)) {
2120 // Stores *of* the pointer are not interesting, only stores *to* the
2121 // pointer.
2122 if (UI->getOperand(1) != ASIV)
2123 continue;
2124 if (!Store->isUnordered())
2125 return false;
2126
2127 SawUnorderedAtomic |= Store->isAtomic();
2128 SawNotAtomic |= !Store->isAtomic();
2129
2130 // If the store is guaranteed to execute, both properties are satisfied.
2131 // We may want to check if a store is guaranteed to execute even if we
2132 // already know that promotion is safe, since it may have higher
2133 // alignment than any other guaranteed stores, in which case we can
2134 // raise the alignment on the promoted store.
2135 Align InstAlignment = Store->getAlign();
2136
2137 if (!DereferenceableInPH || !SafeToInsertStore ||
2138 (InstAlignment > Alignment)) {
2139 if (SafetyInfo->isGuaranteedToExecute(*UI, DT, CurLoop)) {
2140 DereferenceableInPH = true;
2141 SafeToInsertStore = true;
2142 Alignment = std::max(Alignment, InstAlignment);
2143 }
2144 }
2145
2146 // If a store dominates all exit blocks, it is safe to sink.
2147 // As explained above, if an exit block was executed, a dominating
2148 // store must have been executed at least once, so we are not
2149 // introducing stores on paths that did not have them.
2150 // Note that this only looks at explicit exit blocks. If we ever
2151 // start sinking stores into unwind edges (see above), this will break.
2152 if (!SafeToInsertStore)
2153 SafeToInsertStore = llvm::all_of(ExitBlocks, [&](BasicBlock *Exit) {
2154 return DT->dominates(Store->getParent(), Exit);
2155 });
2156
2157 // If the store is not guaranteed to execute, we may still get
2158 // deref info through it.
2159 if (!DereferenceableInPH) {
2160 DereferenceableInPH = isDereferenceableAndAlignedPointer(
2161 Store->getPointerOperand(), Store->getValueOperand()->getType(),
2162 Store->getAlign(), MDL, Preheader->getTerminator(), DT, TLI);
2163 }
2164 } else
2165 return false; // Not a load or store.
2166
2167 // Merge the AA tags.
2168 if (LoopUses.empty()) {
2169 // On the first load/store, just take its AA tags.
2170 UI->getAAMetadata(AATags);
2171 } else if (AATags) {
2172 UI->getAAMetadata(AATags, /* Merge = */ true);
2173 }
2174
2175 LoopUses.push_back(UI);
2176 }
2177 }
2178
2179 // If we found both an unordered atomic instruction and a non-atomic memory
2180 // access, bail. We can't blindly promote non-atomic to atomic since we
2181 // might not be able to lower the result. We can't downgrade since that
2182 // would violate memory model. Also, align 0 is an error for atomics.
2183 if (SawUnorderedAtomic && SawNotAtomic)
2184 return false;
2185
2186 // If we're inserting an atomic load in the preheader, we must be able to
2187 // lower it. We're only guaranteed to be able to lower naturally aligned
2188 // atomics.
2189 auto *SomePtrElemType = SomePtr->getType()->getPointerElementType();
2190 if (SawUnorderedAtomic &&
2191 Alignment < MDL.getTypeStoreSize(SomePtrElemType))
2192 return false;
2193
2194 // If we couldn't prove we can hoist the load, bail.
2195 if (!DereferenceableInPH)
2196 return false;
2197
2198 // We know we can hoist the load, but don't have a guaranteed store.
2199 // Check whether the location is thread-local. If it is, then we can insert
2200 // stores along paths which originally didn't have them without violating the
2201 // memory model.
2202 if (!SafeToInsertStore) {
2203 if (IsKnownThreadLocalObject)
2204 SafeToInsertStore = true;
2205 else {
2206 Value *Object = getUnderlyingObject(SomePtr);
2207 SafeToInsertStore =
2208 (isAllocLikeFn(Object, TLI) || isa<AllocaInst>(Object)) &&
2209 isNotCapturedBeforeOrInLoop(Object, CurLoop, DT);
2210 }
2211 }
2212
2213 // If we've still failed to prove we can sink the store, give up.
2214 if (!SafeToInsertStore)
2215 return false;
2216
2217 // Otherwise, this is safe to promote, lets do it!
2218 LLVM_DEBUG(dbgs() << "LICM: Promoting value stored to in loop: " << *SomePtrdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM: Promoting value stored to in loop: "
<< *SomePtr << '\n'; } } while (false)
2219 << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "LICM: Promoting value stored to in loop: "
<< *SomePtr << '\n'; } } while (false)
;
2220 ORE->emit([&]() {
2221 return OptimizationRemark(DEBUG_TYPE"licm", "PromoteLoopAccessesToScalar",
2222 LoopUses[0])
2223 << "Moving accesses to memory location out of the loop";
2224 });
2225 ++NumPromoted;
2226
2227 // Look at all the loop uses, and try to merge their locations.
2228 std::vector<const DILocation *> LoopUsesLocs;
2229 for (auto U : LoopUses)
2230 LoopUsesLocs.push_back(U->getDebugLoc().get());
2231 auto DL = DebugLoc(DILocation::getMergedLocations(LoopUsesLocs));
2232
2233 // We use the SSAUpdater interface to insert phi nodes as required.
2234 SmallVector<PHINode *, 16> NewPHIs;
2235 SSAUpdater SSA(&NewPHIs);
2236 LoopPromoter Promoter(SomePtr, LoopUses, SSA, PointerMustAliases, ExitBlocks,
2237 InsertPts, MSSAInsertPts, PIC, CurAST, MSSAU, *LI, DL,
2238 Alignment.value(), SawUnorderedAtomic, AATags,
2239 *SafetyInfo);
2240
2241 // Set up the preheader to have a definition of the value. It is the live-out
2242 // value from the preheader that uses in the loop will use.
2243 LoadInst *PreheaderLoad = new LoadInst(
2244 SomePtr->getType()->getPointerElementType(), SomePtr,
2245 SomePtr->getName() + ".promoted", Preheader->getTerminator());
2246 if (SawUnorderedAtomic)
2247 PreheaderLoad->setOrdering(AtomicOrdering::Unordered);
2248 PreheaderLoad->setAlignment(Alignment);
2249 PreheaderLoad->setDebugLoc(DebugLoc());
2250 if (AATags)
2251 PreheaderLoad->setAAMetadata(AATags);
2252 SSA.AddAvailableValue(Preheader, PreheaderLoad);
2253
2254 if (MSSAU) {
2255 MemoryAccess *PreheaderLoadMemoryAccess = MSSAU->createMemoryAccessInBB(
2256 PreheaderLoad, nullptr, PreheaderLoad->getParent(), MemorySSA::End);
2257 MemoryUse *NewMemUse = cast<MemoryUse>(PreheaderLoadMemoryAccess);
2258 MSSAU->insertUse(NewMemUse, /*RenameUses=*/true);
2259 }
2260
2261 if (MSSAU && VerifyMemorySSA)
2262 MSSAU->getMemorySSA()->verifyMemorySSA();
2263 // Rewrite all the loads in the loop and remember all the definitions from
2264 // stores in the loop.
2265 Promoter.run(LoopUses);
2266
2267 if (MSSAU && VerifyMemorySSA)
2268 MSSAU->getMemorySSA()->verifyMemorySSA();
2269 // If the SSAUpdater didn't use the load in the preheader, just zap it now.
2270 if (PreheaderLoad->use_empty())
2271 eraseInstruction(*PreheaderLoad, *SafetyInfo, CurAST, MSSAU);
2272
2273 return true;
2274}
2275
2276static void foreachMemoryAccess(MemorySSA *MSSA, Loop *L,
2277 function_ref<void(Instruction *)> Fn) {
2278 for (const BasicBlock *BB : L->blocks())
2279 if (const auto *Accesses = MSSA->getBlockAccesses(BB))
2280 for (const auto &Access : *Accesses)
2281 if (const auto *MUD = dyn_cast<MemoryUseOrDef>(&Access))
2282 Fn(MUD->getMemoryInst());
2283}
2284
2285static SmallVector<SmallSetVector<Value *, 8>, 0>
2286collectPromotionCandidates(MemorySSA *MSSA, AliasAnalysis *AA, Loop *L,
2287 SmallVectorImpl<Instruction *> &MaybePromotable) {
2288 AliasSetTracker AST(*AA);
2289
2290 auto IsPotentiallyPromotable = [L](const Instruction *I) {
2291 if (const auto *SI = dyn_cast<StoreInst>(I))
2292 return L->isLoopInvariant(SI->getPointerOperand());
2293 if (const auto *LI = dyn_cast<LoadInst>(I))
2294 return L->isLoopInvariant(LI->getPointerOperand());
2295 return false;
2296 };
2297
2298 // Populate AST with potentially promotable accesses and remove them from
2299 // MaybePromotable, so they will not be checked again on the next iteration.
2300 SmallPtrSet<Value *, 16> AttemptingPromotion;
2301 llvm::erase_if(MaybePromotable, [&](Instruction *I) {
2302 if (IsPotentiallyPromotable(I)) {
2303 AttemptingPromotion.insert(I);
2304 AST.add(I);
2305 return true;
2306 }
2307 return false;
2308 });
2309
2310 // We're only interested in must-alias sets that contain a mod.
2311 SmallVector<const AliasSet *, 8> Sets;
2312 for (AliasSet &AS : AST)
2313 if (!AS.isForwardingAliasSet() && AS.isMod() && AS.isMustAlias())
2314 Sets.push_back(&AS);
2315
2316 if (Sets.empty())
2317 return {}; // Nothing to promote...
2318
2319 // Discard any sets for which there is an aliasing non-promotable access.
2320 foreachMemoryAccess(MSSA, L, [&](Instruction *I) {
2321 if (AttemptingPromotion.contains(I))
2322 return;
2323
2324 llvm::erase_if(Sets, [&](const AliasSet *AS) {
2325 return AS->aliasesUnknownInst(I, *AA);
2326 });
2327 });
2328
2329 SmallVector<SmallSetVector<Value *, 8>, 0> Result;
2330 for (const AliasSet *Set : Sets) {
2331 SmallSetVector<Value *, 8> PointerMustAliases;
2332 for (const auto &ASI : *Set)
2333 PointerMustAliases.insert(ASI.getValue());
2334 Result.push_back(std::move(PointerMustAliases));
2335 }
2336
2337 return Result;
2338}
2339
2340/// Returns an owning pointer to an alias set which incorporates aliasing info
2341/// from L and all subloops of L.
2342std::unique_ptr<AliasSetTracker>
2343LoopInvariantCodeMotion::collectAliasInfoForLoop(Loop *L, LoopInfo *LI,
2344 AAResults *AA) {
2345 auto CurAST = std::make_unique<AliasSetTracker>(*AA);
2346
2347 // Add everything from all the sub loops.
2348 for (Loop *InnerL : L->getSubLoops())
2349 for (BasicBlock *BB : InnerL->blocks())
2350 CurAST->add(*BB);
2351
2352 // And merge in this loop (without anything from inner loops).
2353 for (BasicBlock *BB : L->blocks())
2354 if (LI->getLoopFor(BB) == L)
2355 CurAST->add(*BB);
2356
2357 return CurAST;
2358}
2359
2360static bool pointerInvalidatedByLoop(MemoryLocation MemLoc,
2361 AliasSetTracker *CurAST, Loop *CurLoop,
2362 AAResults *AA) {
2363 // First check to see if any of the basic blocks in CurLoop invalidate *V.
2364 bool isInvalidatedAccordingToAST = CurAST->getAliasSetFor(MemLoc).isMod();
2365
2366 if (!isInvalidatedAccordingToAST || !LICMN2Theshold)
2367 return isInvalidatedAccordingToAST;
2368
2369 // Check with a diagnostic analysis if we can refine the information above.
2370 // This is to identify the limitations of using the AST.
2371 // The alias set mechanism used by LICM has a major weakness in that it
2372 // combines all things which may alias into a single set *before* asking
2373 // modref questions. As a result, a single readonly call within a loop will
2374 // collapse all loads and stores into a single alias set and report
2375 // invalidation if the loop contains any store. For example, readonly calls
2376 // with deopt states have this form and create a general alias set with all
2377 // loads and stores. In order to get any LICM in loops containing possible
2378 // deopt states we need a more precise invalidation of checking the mod ref
2379 // info of each instruction within the loop and LI. This has a complexity of
2380 // O(N^2), so currently, it is used only as a diagnostic tool since the
2381 // default value of LICMN2Threshold is zero.
2382
2383 // Don't look at nested loops.
2384 if (CurLoop->begin() != CurLoop->end())
2385 return true;
2386
2387 int N = 0;
2388 for (BasicBlock *BB : CurLoop->getBlocks())
2389 for (Instruction &I : *BB) {
2390 if (N >= LICMN2Theshold) {
2391 LLVM_DEBUG(dbgs() << "Alasing N2 threshold exhausted for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "Alasing N2 threshold exhausted for "
<< *(MemLoc.Ptr) << "\n"; } } while (false)
2392 << *(MemLoc.Ptr) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "Alasing N2 threshold exhausted for "
<< *(MemLoc.Ptr) << "\n"; } } while (false)
;
2393 return true;
2394 }
2395 N++;
2396 auto Res = AA->getModRefInfo(&I, MemLoc);
2397 if (isModSet(Res)) {
2398 LLVM_DEBUG(dbgs() << "Aliasing failed on " << I << " for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "Aliasing failed on " << I <<
" for " << *(MemLoc.Ptr) << "\n"; } } while (false
)
2399 << *(MemLoc.Ptr) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "Aliasing failed on " << I <<
" for " << *(MemLoc.Ptr) << "\n"; } } while (false
)
;
2400 return true;
2401 }
2402 }
2403 LLVM_DEBUG(dbgs() << "Aliasing okay for " << *(MemLoc.Ptr) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("licm")) { dbgs() << "Aliasing okay for " << *(MemLoc
.Ptr) << "\n"; } } while (false)
;
2404 return false;
2405}
2406
2407bool pointerInvalidatedByLoopWithMSSA(MemorySSA *MSSA, MemoryUse *MU,
2408 Loop *CurLoop, Instruction &I,
2409 SinkAndHoistLICMFlags &Flags) {
2410 // For hoisting, use the walker to determine safety
2411 if (!Flags.getIsSink()) {
2412 MemoryAccess *Source;
2413 // See declaration of SetLicmMssaOptCap for usage details.
2414 if (Flags.tooManyClobberingCalls())
2415 Source = MU->getDefiningAccess();
2416 else {
2417 Source = MSSA->getSkipSelfWalker()->getClobberingMemoryAccess(MU);
2418 Flags.incrementClobberingCalls();
2419 }
2420 return !MSSA->isLiveOnEntryDef(Source) &&
2421 CurLoop->contains(Source->getBlock());
2422 }
2423
2424 // For sinking, we'd need to check all Defs below this use. The getClobbering
2425 // call will look on the backedge of the loop, but will check aliasing with
2426 // the instructions on the previous iteration.
2427 // For example:
2428 // for (i ... )
2429 // load a[i] ( Use (LoE)
2430 // store a[i] ( 1 = Def (2), with 2 = Phi for the loop.
2431 // i++;
2432 // The load sees no clobbering inside the loop, as the backedge alias check
2433 // does phi translation, and will check aliasing against store a[i-1].
2434 // However sinking the load outside the loop, below the store is incorrect.
2435
2436 // For now, only sink if there are no Defs in the loop, and the existing ones
2437 // precede the use and are in the same block.
2438 // FIXME: Increase precision: Safe to sink if Use post dominates the Def;
2439 // needs PostDominatorTreeAnalysis.
2440 // FIXME: More precise: no Defs that alias this Use.
2441 if (Flags.tooManyMemoryAccesses())
2442 return true;
2443 for (auto *BB : CurLoop->getBlocks())
2444 if (pointerInvalidatedByBlockWithMSSA(*BB, *MSSA, *MU))
2445 return true;
2446 // When sinking, the source block may not be part of the loop so check it.
2447 if (!CurLoop->contains(&I))
2448 return pointerInvalidatedByBlockWithMSSA(*I.getParent(), *MSSA, *MU);
2449
2450 return false;
2451}
2452
2453bool pointerInvalidatedByBlockWithMSSA(BasicBlock &BB, MemorySSA &MSSA,
2454 MemoryUse &MU) {
2455 if (const auto *Accesses = MSSA.getBlockDefs(&BB))
2456 for (const auto &MA : *Accesses)
2457 if (const auto *MD = dyn_cast<MemoryDef>(&MA))
2458 if (MU.getBlock() != MD->getBlock() || !MSSA.locallyDominates(MD, &MU))
2459 return true;
2460 return false;
2461}
2462
2463/// Little predicate that returns true if the specified basic block is in
2464/// a subloop of the current one, not the current one itself.
2465///
2466static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI) {
2467 assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop")(static_cast <bool> (CurLoop->contains(BB) &&
"Only valid if BB is IN the loop") ? void (0) : __assert_fail
("CurLoop->contains(BB) && \"Only valid if BB is IN the loop\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/lib/Transforms/Scalar/LICM.cpp"
, 2467, __extension__ __PRETTY_FUNCTION__))
;
2468 return LI->getLoopFor(BB) != CurLoop;
2469}

/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h

1//===- llvm/InstrTypes.h - Important Instruction subclasses -----*- C++ -*-===//
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 file defines various meta classes of instructions that exist in the VM
10// representation. Specific concrete subclasses of these may be found in the
11// i*.h files...
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_IR_INSTRTYPES_H
16#define LLVM_IR_INSTRTYPES_H
17
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/None.h"
20#include "llvm/ADT/Optional.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/StringMap.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/ADT/Twine.h"
25#include "llvm/ADT/iterator_range.h"
26#include "llvm/IR/Attributes.h"
27#include "llvm/IR/CallingConv.h"
28#include "llvm/IR/Constants.h"
29#include "llvm/IR/DerivedTypes.h"
30#include "llvm/IR/Function.h"
31#include "llvm/IR/Instruction.h"
32#include "llvm/IR/LLVMContext.h"
33#include "llvm/IR/OperandTraits.h"
34#include "llvm/IR/Type.h"
35#include "llvm/IR/User.h"
36#include "llvm/IR/Value.h"
37#include "llvm/Support/Casting.h"
38#include "llvm/Support/ErrorHandling.h"
39#include <algorithm>
40#include <cassert>
41#include <cstddef>
42#include <cstdint>
43#include <iterator>
44#include <string>
45#include <vector>
46
47namespace llvm {
48
49namespace Intrinsic {
50typedef unsigned ID;
51}
52
53//===----------------------------------------------------------------------===//
54// UnaryInstruction Class
55//===----------------------------------------------------------------------===//
56
57class UnaryInstruction : public Instruction {
58protected:
59 UnaryInstruction(Type *Ty, unsigned iType, Value *V,
60 Instruction *IB = nullptr)
61 : Instruction(Ty, iType, &Op<0>(), 1, IB) {
62 Op<0>() = V;
63 }
64 UnaryInstruction(Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
65 : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
66 Op<0>() = V;
67 }
68
69public:
70 // allocate space for exactly one operand
71 void *operator new(size_t s) {
72 return User::operator new(s, 1);
73 }
74
75 /// Transparently provide more efficient getOperand methods.
76 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void
setOperand(unsigned, Value*); inline op_iterator op_begin();
inline const_op_iterator op_begin() const; inline op_iterator
op_end(); inline const_op_iterator op_end() const; protected
: template <int> inline Use &Op(); template <int
> inline const Use &Op() const; public: inline unsigned
getNumOperands() const
;
77
78 // Methods for support type inquiry through isa, cast, and dyn_cast:
79 static bool classof(const Instruction *I) {
80 return I->isUnaryOp() ||
81 I->getOpcode() == Instruction::Alloca ||
82 I->getOpcode() == Instruction::Load ||
83 I->getOpcode() == Instruction::VAArg ||
84 I->getOpcode() == Instruction::ExtractValue ||
85 (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
86 }
87 static bool classof(const Value *V) {
88 return isa<Instruction>(V) && classof(cast<Instruction>(V));
89 }
90};
91
92template <>
93struct OperandTraits<UnaryInstruction> :
94 public FixedNumOperandTraits<UnaryInstruction, 1> {
95};
96
97DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)UnaryInstruction::op_iterator UnaryInstruction::op_begin() { return
OperandTraits<UnaryInstruction>::op_begin(this); } UnaryInstruction
::const_op_iterator UnaryInstruction::op_begin() const { return
OperandTraits<UnaryInstruction>::op_begin(const_cast<
UnaryInstruction*>(this)); } UnaryInstruction::op_iterator
UnaryInstruction::op_end() { return OperandTraits<UnaryInstruction
>::op_end(this); } UnaryInstruction::const_op_iterator UnaryInstruction
::op_end() const { return OperandTraits<UnaryInstruction>
::op_end(const_cast<UnaryInstruction*>(this)); } Value *
UnaryInstruction::getOperand(unsigned i_nocapture) const { (static_cast
<bool> (i_nocapture < OperandTraits<UnaryInstruction
>::operands(this) && "getOperand() out of range!")
? void (0) : __assert_fail ("i_nocapture < OperandTraits<UnaryInstruction>::operands(this) && \"getOperand() out of range!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 97, __extension__ __PRETTY_FUNCTION__)); return cast_or_null
<Value>( OperandTraits<UnaryInstruction>::op_begin
(const_cast<UnaryInstruction*>(this))[i_nocapture].get(
)); } void UnaryInstruction::setOperand(unsigned i_nocapture,
Value *Val_nocapture) { (static_cast <bool> (i_nocapture
< OperandTraits<UnaryInstruction>::operands(this) &&
"setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<UnaryInstruction>::operands(this) && \"setOperand() out of range!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 97, __extension__ __PRETTY_FUNCTION__)); OperandTraits<UnaryInstruction
>::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned
UnaryInstruction::getNumOperands() const { return OperandTraits
<UnaryInstruction>::operands(this); } template <int Idx_nocapture
> Use &UnaryInstruction::Op() { return this->OpFrom
<Idx_nocapture>(this); } template <int Idx_nocapture
> const Use &UnaryInstruction::Op() const { return this
->OpFrom<Idx_nocapture>(this); }
98
99//===----------------------------------------------------------------------===//
100// UnaryOperator Class
101//===----------------------------------------------------------------------===//
102
103class UnaryOperator : public UnaryInstruction {
104 void AssertOK();
105
106protected:
107 UnaryOperator(UnaryOps iType, Value *S, Type *Ty,
108 const Twine &Name, Instruction *InsertBefore);
109 UnaryOperator(UnaryOps iType, Value *S, Type *Ty,
110 const Twine &Name, BasicBlock *InsertAtEnd);
111
112 // Note: Instruction needs to be a friend here to call cloneImpl.
113 friend class Instruction;
114
115 UnaryOperator *cloneImpl() const;
116
117public:
118
119 /// Construct a unary instruction, given the opcode and an operand.
120 /// Optionally (if InstBefore is specified) insert the instruction
121 /// into a BasicBlock right before the specified instruction. The specified
122 /// Instruction is allowed to be a dereferenced end iterator.
123 ///
124 static UnaryOperator *Create(UnaryOps Op, Value *S,
125 const Twine &Name = Twine(),
126 Instruction *InsertBefore = nullptr);
127
128 /// Construct a unary instruction, given the opcode and an operand.
129 /// Also automatically insert this instruction to the end of the
130 /// BasicBlock specified.
131 ///
132 static UnaryOperator *Create(UnaryOps Op, Value *S,
133 const Twine &Name,
134 BasicBlock *InsertAtEnd);
135
136 /// These methods just forward to Create, and are useful when you
137 /// statically know what type of instruction you're going to create. These
138 /// helpers just save some typing.
139#define HANDLE_UNARY_INST(N, OPC, CLASS) \
140 static UnaryOperator *Create##OPC(Value *V, const Twine &Name = "") {\
141 return Create(Instruction::OPC, V, Name);\
142 }
143#include "llvm/IR/Instruction.def"
144#define HANDLE_UNARY_INST(N, OPC, CLASS) \
145 static UnaryOperator *Create##OPC(Value *V, const Twine &Name, \
146 BasicBlock *BB) {\
147 return Create(Instruction::OPC, V, Name, BB);\
148 }
149#include "llvm/IR/Instruction.def"
150#define HANDLE_UNARY_INST(N, OPC, CLASS) \
151 static UnaryOperator *Create##OPC(Value *V, const Twine &Name, \
152 Instruction *I) {\
153 return Create(Instruction::OPC, V, Name, I);\
154 }
155#include "llvm/IR/Instruction.def"
156
157 static UnaryOperator *
158 CreateWithCopiedFlags(UnaryOps Opc, Value *V, Instruction *CopyO,
159 const Twine &Name = "",
160 Instruction *InsertBefore = nullptr) {
161 UnaryOperator *UO = Create(Opc, V, Name, InsertBefore);
162 UO->copyIRFlags(CopyO);
163 return UO;
164 }
165
166 static UnaryOperator *CreateFNegFMF(Value *Op, Instruction *FMFSource,
167 const Twine &Name = "",
168 Instruction *InsertBefore = nullptr) {
169 return CreateWithCopiedFlags(Instruction::FNeg, Op, FMFSource, Name,
170 InsertBefore);
171 }
172
173 UnaryOps getOpcode() const {
174 return static_cast<UnaryOps>(Instruction::getOpcode());
175 }
176
177 // Methods for support type inquiry through isa, cast, and dyn_cast:
178 static bool classof(const Instruction *I) {
179 return I->isUnaryOp();
180 }
181 static bool classof(const Value *V) {
182 return isa<Instruction>(V) && classof(cast<Instruction>(V));
183 }
184};
185
186//===----------------------------------------------------------------------===//
187// BinaryOperator Class
188//===----------------------------------------------------------------------===//
189
190class BinaryOperator : public Instruction {
191 void AssertOK();
192
193protected:
194 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty,
195 const Twine &Name, Instruction *InsertBefore);
196 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty,
197 const Twine &Name, BasicBlock *InsertAtEnd);
198
199 // Note: Instruction needs to be a friend here to call cloneImpl.
200 friend class Instruction;
201
202 BinaryOperator *cloneImpl() const;
203
204public:
205 // allocate space for exactly two operands
206 void *operator new(size_t s) {
207 return User::operator new(s, 2);
208 }
209
210 /// Transparently provide more efficient getOperand methods.
211 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void
setOperand(unsigned, Value*); inline op_iterator op_begin();
inline const_op_iterator op_begin() const; inline op_iterator
op_end(); inline const_op_iterator op_end() const; protected
: template <int> inline Use &Op(); template <int
> inline const Use &Op() const; public: inline unsigned
getNumOperands() const
;
212
213 /// Construct a binary instruction, given the opcode and the two
214 /// operands. Optionally (if InstBefore is specified) insert the instruction
215 /// into a BasicBlock right before the specified instruction. The specified
216 /// Instruction is allowed to be a dereferenced end iterator.
217 ///
218 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
219 const Twine &Name = Twine(),
220 Instruction *InsertBefore = nullptr);
221
222 /// Construct a binary instruction, given the opcode and the two
223 /// operands. Also automatically insert this instruction to the end of the
224 /// BasicBlock specified.
225 ///
226 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
227 const Twine &Name, BasicBlock *InsertAtEnd);
228
229 /// These methods just forward to Create, and are useful when you
230 /// statically know what type of instruction you're going to create. These
231 /// helpers just save some typing.
232#define HANDLE_BINARY_INST(N, OPC, CLASS) \
233 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
234 const Twine &Name = "") {\
235 return Create(Instruction::OPC, V1, V2, Name);\
236 }
237#include "llvm/IR/Instruction.def"
238#define HANDLE_BINARY_INST(N, OPC, CLASS) \
239 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
240 const Twine &Name, BasicBlock *BB) {\
241 return Create(Instruction::OPC, V1, V2, Name, BB);\
242 }
243#include "llvm/IR/Instruction.def"
244#define HANDLE_BINARY_INST(N, OPC, CLASS) \
245 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
246 const Twine &Name, Instruction *I) {\
247 return Create(Instruction::OPC, V1, V2, Name, I);\
248 }
249#include "llvm/IR/Instruction.def"
250
251 static BinaryOperator *CreateWithCopiedFlags(BinaryOps Opc,
252 Value *V1, Value *V2,
253 Instruction *CopyO,
254 const Twine &Name = "") {
255 BinaryOperator *BO = Create(Opc, V1, V2, Name);
256 BO->copyIRFlags(CopyO);
257 return BO;
258 }
259
260 static BinaryOperator *CreateFAddFMF(Value *V1, Value *V2,
261 Instruction *FMFSource,
262 const Twine &Name = "") {
263 return CreateWithCopiedFlags(Instruction::FAdd, V1, V2, FMFSource, Name);
264 }
265 static BinaryOperator *CreateFSubFMF(Value *V1, Value *V2,
266 Instruction *FMFSource,
267 const Twine &Name = "") {
268 return CreateWithCopiedFlags(Instruction::FSub, V1, V2, FMFSource, Name);
269 }
270 static BinaryOperator *CreateFMulFMF(Value *V1, Value *V2,
271 Instruction *FMFSource,
272 const Twine &Name = "") {
273 return CreateWithCopiedFlags(Instruction::FMul, V1, V2, FMFSource, Name);
274 }
275 static BinaryOperator *CreateFDivFMF(Value *V1, Value *V2,
276 Instruction *FMFSource,
277 const Twine &Name = "") {
278 return CreateWithCopiedFlags(Instruction::FDiv, V1, V2, FMFSource, Name);
279 }
280 static BinaryOperator *CreateFRemFMF(Value *V1, Value *V2,
281 Instruction *FMFSource,
282 const Twine &Name = "") {
283 return CreateWithCopiedFlags(Instruction::FRem, V1, V2, FMFSource, Name);
284 }
285
286 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
287 const Twine &Name = "") {
288 BinaryOperator *BO = Create(Opc, V1, V2, Name);
289 BO->setHasNoSignedWrap(true);
290 return BO;
291 }
292 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
293 const Twine &Name, BasicBlock *BB) {
294 BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
295 BO->setHasNoSignedWrap(true);
296 return BO;
297 }
298 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
299 const Twine &Name, Instruction *I) {
300 BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
301 BO->setHasNoSignedWrap(true);
302 return BO;
303 }
304
305 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
306 const Twine &Name = "") {
307 BinaryOperator *BO = Create(Opc, V1, V2, Name);
308 BO->setHasNoUnsignedWrap(true);
309 return BO;
310 }
311 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
312 const Twine &Name, BasicBlock *BB) {
313 BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
314 BO->setHasNoUnsignedWrap(true);
315 return BO;
316 }
317 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
318 const Twine &Name, Instruction *I) {
319 BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
320 BO->setHasNoUnsignedWrap(true);
321 return BO;
322 }
323
324 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
325 const Twine &Name = "") {
326 BinaryOperator *BO = Create(Opc, V1, V2, Name);
327 BO->setIsExact(true);
328 return BO;
329 }
330 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
331 const Twine &Name, BasicBlock *BB) {
332 BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
333 BO->setIsExact(true);
334 return BO;
335 }
336 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
337 const Twine &Name, Instruction *I) {
338 BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
339 BO->setIsExact(true);
340 return BO;
341 }
342
343#define DEFINE_HELPERS(OPC, NUWNSWEXACT) \
344 static BinaryOperator *Create##NUWNSWEXACT##OPC(Value *V1, Value *V2, \
345 const Twine &Name = "") { \
346 return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name); \
347 } \
348 static BinaryOperator *Create##NUWNSWEXACT##OPC( \
349 Value *V1, Value *V2, const Twine &Name, BasicBlock *BB) { \
350 return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name, BB); \
351 } \
352 static BinaryOperator *Create##NUWNSWEXACT##OPC( \
353 Value *V1, Value *V2, const Twine &Name, Instruction *I) { \
354 return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name, I); \
355 }
356
357 DEFINE_HELPERS(Add, NSW) // CreateNSWAdd
358 DEFINE_HELPERS(Add, NUW) // CreateNUWAdd
359 DEFINE_HELPERS(Sub, NSW) // CreateNSWSub
360 DEFINE_HELPERS(Sub, NUW) // CreateNUWSub
361 DEFINE_HELPERS(Mul, NSW) // CreateNSWMul
362 DEFINE_HELPERS(Mul, NUW) // CreateNUWMul
363 DEFINE_HELPERS(Shl, NSW) // CreateNSWShl
364 DEFINE_HELPERS(Shl, NUW) // CreateNUWShl
365
366 DEFINE_HELPERS(SDiv, Exact) // CreateExactSDiv
367 DEFINE_HELPERS(UDiv, Exact) // CreateExactUDiv
368 DEFINE_HELPERS(AShr, Exact) // CreateExactAShr
369 DEFINE_HELPERS(LShr, Exact) // CreateExactLShr
370
371#undef DEFINE_HELPERS
372
373 /// Helper functions to construct and inspect unary operations (NEG and NOT)
374 /// via binary operators SUB and XOR:
375 ///
376 /// Create the NEG and NOT instructions out of SUB and XOR instructions.
377 ///
378 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
379 Instruction *InsertBefore = nullptr);
380 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
381 BasicBlock *InsertAtEnd);
382 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
383 Instruction *InsertBefore = nullptr);
384 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
385 BasicBlock *InsertAtEnd);
386 static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "",
387 Instruction *InsertBefore = nullptr);
388 static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name,
389 BasicBlock *InsertAtEnd);
390 static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
391 Instruction *InsertBefore = nullptr);
392 static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
393 BasicBlock *InsertAtEnd);
394
395 BinaryOps getOpcode() const {
396 return static_cast<BinaryOps>(Instruction::getOpcode());
397 }
398
399 /// Exchange the two operands to this instruction.
400 /// This instruction is safe to use on any binary instruction and
401 /// does not modify the semantics of the instruction. If the instruction
402 /// cannot be reversed (ie, it's a Div), then return true.
403 ///
404 bool swapOperands();
405
406 // Methods for support type inquiry through isa, cast, and dyn_cast:
407 static bool classof(const Instruction *I) {
408 return I->isBinaryOp();
409 }
410 static bool classof(const Value *V) {
411 return isa<Instruction>(V) && classof(cast<Instruction>(V));
412 }
413};
414
415template <>
416struct OperandTraits<BinaryOperator> :
417 public FixedNumOperandTraits<BinaryOperator, 2> {
418};
419
420DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)BinaryOperator::op_iterator BinaryOperator::op_begin() { return
OperandTraits<BinaryOperator>::op_begin(this); } BinaryOperator
::const_op_iterator BinaryOperator::op_begin() const { return
OperandTraits<BinaryOperator>::op_begin(const_cast<
BinaryOperator*>(this)); } BinaryOperator::op_iterator BinaryOperator
::op_end() { return OperandTraits<BinaryOperator>::op_end
(this); } BinaryOperator::const_op_iterator BinaryOperator::op_end
() const { return OperandTraits<BinaryOperator>::op_end
(const_cast<BinaryOperator*>(this)); } Value *BinaryOperator
::getOperand(unsigned i_nocapture) const { (static_cast <bool
> (i_nocapture < OperandTraits<BinaryOperator>::operands
(this) && "getOperand() out of range!") ? void (0) : __assert_fail
("i_nocapture < OperandTraits<BinaryOperator>::operands(this) && \"getOperand() out of range!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 420, __extension__ __PRETTY_FUNCTION__)); return cast_or_null
<Value>( OperandTraits<BinaryOperator>::op_begin(
const_cast<BinaryOperator*>(this))[i_nocapture].get());
} void BinaryOperator::setOperand(unsigned i_nocapture, Value
*Val_nocapture) { (static_cast <bool> (i_nocapture <
OperandTraits<BinaryOperator>::operands(this) &&
"setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<BinaryOperator>::operands(this) && \"setOperand() out of range!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 420, __extension__ __PRETTY_FUNCTION__)); OperandTraits<
BinaryOperator>::op_begin(this)[i_nocapture] = Val_nocapture
; } unsigned BinaryOperator::getNumOperands() const { return OperandTraits
<BinaryOperator>::operands(this); } template <int Idx_nocapture
> Use &BinaryOperator::Op() { return this->OpFrom<
Idx_nocapture>(this); } template <int Idx_nocapture>
const Use &BinaryOperator::Op() const { return this->
OpFrom<Idx_nocapture>(this); }
421
422//===----------------------------------------------------------------------===//
423// CastInst Class
424//===----------------------------------------------------------------------===//
425
426/// This is the base class for all instructions that perform data
427/// casts. It is simply provided so that instruction category testing
428/// can be performed with code like:
429///
430/// if (isa<CastInst>(Instr)) { ... }
431/// Base class of casting instructions.
432class CastInst : public UnaryInstruction {
433protected:
434 /// Constructor with insert-before-instruction semantics for subclasses
435 CastInst(Type *Ty, unsigned iType, Value *S,
436 const Twine &NameStr = "", Instruction *InsertBefore = nullptr)
437 : UnaryInstruction(Ty, iType, S, InsertBefore) {
438 setName(NameStr);
439 }
440 /// Constructor with insert-at-end-of-block semantics for subclasses
441 CastInst(Type *Ty, unsigned iType, Value *S,
442 const Twine &NameStr, BasicBlock *InsertAtEnd)
443 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
444 setName(NameStr);
445 }
446
447public:
448 /// Provides a way to construct any of the CastInst subclasses using an
449 /// opcode instead of the subclass's constructor. The opcode must be in the
450 /// CastOps category (Instruction::isCast(opcode) returns true). This
451 /// constructor has insert-before-instruction semantics to automatically
452 /// insert the new CastInst before InsertBefore (if it is non-null).
453 /// Construct any of the CastInst subclasses
454 static CastInst *Create(
455 Instruction::CastOps, ///< The opcode of the cast instruction
456 Value *S, ///< The value to be casted (operand 0)
457 Type *Ty, ///< The type to which cast should be made
458 const Twine &Name = "", ///< Name for the instruction
459 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
460 );
461 /// Provides a way to construct any of the CastInst subclasses using an
462 /// opcode instead of the subclass's constructor. The opcode must be in the
463 /// CastOps category. This constructor has insert-at-end-of-block semantics
464 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
465 /// its non-null).
466 /// Construct any of the CastInst subclasses
467 static CastInst *Create(
468 Instruction::CastOps, ///< The opcode for the cast instruction
469 Value *S, ///< The value to be casted (operand 0)
470 Type *Ty, ///< The type to which operand is casted
471 const Twine &Name, ///< The name for the instruction
472 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
473 );
474
475 /// Create a ZExt or BitCast cast instruction
476 static CastInst *CreateZExtOrBitCast(
477 Value *S, ///< The value to be casted (operand 0)
478 Type *Ty, ///< The type to which cast should be made
479 const Twine &Name = "", ///< Name for the instruction
480 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
481 );
482
483 /// Create a ZExt or BitCast cast instruction
484 static CastInst *CreateZExtOrBitCast(
485 Value *S, ///< The value to be casted (operand 0)
486 Type *Ty, ///< The type to which operand is casted
487 const Twine &Name, ///< The name for the instruction
488 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
489 );
490
491 /// Create a SExt or BitCast cast instruction
492 static CastInst *CreateSExtOrBitCast(
493 Value *S, ///< The value to be casted (operand 0)
494 Type *Ty, ///< The type to which cast should be made
495 const Twine &Name = "", ///< Name for the instruction
496 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
497 );
498
499 /// Create a SExt or BitCast cast instruction
500 static CastInst *CreateSExtOrBitCast(
501 Value *S, ///< The value to be casted (operand 0)
502 Type *Ty, ///< The type to which operand is casted
503 const Twine &Name, ///< The name for the instruction
504 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
505 );
506
507 /// Create a BitCast AddrSpaceCast, or a PtrToInt cast instruction.
508 static CastInst *CreatePointerCast(
509 Value *S, ///< The pointer value to be casted (operand 0)
510 Type *Ty, ///< The type to which operand is casted
511 const Twine &Name, ///< The name for the instruction
512 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
513 );
514
515 /// Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction.
516 static CastInst *CreatePointerCast(
517 Value *S, ///< The pointer value to be casted (operand 0)
518 Type *Ty, ///< The type to which cast should be made
519 const Twine &Name = "", ///< Name for the instruction
520 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
521 );
522
523 /// Create a BitCast or an AddrSpaceCast cast instruction.
524 static CastInst *CreatePointerBitCastOrAddrSpaceCast(
525 Value *S, ///< The pointer value to be casted (operand 0)
526 Type *Ty, ///< The type to which operand is casted
527 const Twine &Name, ///< The name for the instruction
528 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
529 );
530
531 /// Create a BitCast or an AddrSpaceCast cast instruction.
532 static CastInst *CreatePointerBitCastOrAddrSpaceCast(
533 Value *S, ///< The pointer value to be casted (operand 0)
534 Type *Ty, ///< The type to which cast should be made
535 const Twine &Name = "", ///< Name for the instruction
536 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
537 );
538
539 /// Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
540 ///
541 /// If the value is a pointer type and the destination an integer type,
542 /// creates a PtrToInt cast. If the value is an integer type and the
543 /// destination a pointer type, creates an IntToPtr cast. Otherwise, creates
544 /// a bitcast.
545 static CastInst *CreateBitOrPointerCast(
546 Value *S, ///< The pointer value to be casted (operand 0)
547 Type *Ty, ///< The type to which cast should be made
548 const Twine &Name = "", ///< Name for the instruction
549 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
550 );
551
552 /// Create a ZExt, BitCast, or Trunc for int -> int casts.
553 static CastInst *CreateIntegerCast(
554 Value *S, ///< The pointer value to be casted (operand 0)
555 Type *Ty, ///< The type to which cast should be made
556 bool isSigned, ///< Whether to regard S as signed or not
557 const Twine &Name = "", ///< Name for the instruction
558 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
559 );
560
561 /// Create a ZExt, BitCast, or Trunc for int -> int casts.
562 static CastInst *CreateIntegerCast(
563 Value *S, ///< The integer value to be casted (operand 0)
564 Type *Ty, ///< The integer type to which operand is casted
565 bool isSigned, ///< Whether to regard S as signed or not
566 const Twine &Name, ///< The name for the instruction
567 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
568 );
569
570 /// Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
571 static CastInst *CreateFPCast(
572 Value *S, ///< The floating point value to be casted
573 Type *Ty, ///< The floating point type to cast to
574 const Twine &Name = "", ///< Name for the instruction
575 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
576 );
577
578 /// Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
579 static CastInst *CreateFPCast(
580 Value *S, ///< The floating point value to be casted
581 Type *Ty, ///< The floating point type to cast to
582 const Twine &Name, ///< The name for the instruction
583 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
584 );
585
586 /// Create a Trunc or BitCast cast instruction
587 static CastInst *CreateTruncOrBitCast(
588 Value *S, ///< The value to be casted (operand 0)
589 Type *Ty, ///< The type to which cast should be made
590 const Twine &Name = "", ///< Name for the instruction
591 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
592 );
593
594 /// Create a Trunc or BitCast cast instruction
595 static CastInst *CreateTruncOrBitCast(
596 Value *S, ///< The value to be casted (operand 0)
597 Type *Ty, ///< The type to which operand is casted
598 const Twine &Name, ///< The name for the instruction
599 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
600 );
601
602 /// Check whether a bitcast between these types is valid
603 static bool isBitCastable(
604 Type *SrcTy, ///< The Type from which the value should be cast.
605 Type *DestTy ///< The Type to which the value should be cast.
606 );
607
608 /// Check whether a bitcast, inttoptr, or ptrtoint cast between these
609 /// types is valid and a no-op.
610 ///
611 /// This ensures that any pointer<->integer cast has enough bits in the
612 /// integer and any other cast is a bitcast.
613 static bool isBitOrNoopPointerCastable(
614 Type *SrcTy, ///< The Type from which the value should be cast.
615 Type *DestTy, ///< The Type to which the value should be cast.
616 const DataLayout &DL);
617
618 /// Returns the opcode necessary to cast Val into Ty using usual casting
619 /// rules.
620 /// Infer the opcode for cast operand and type
621 static Instruction::CastOps getCastOpcode(
622 const Value *Val, ///< The value to cast
623 bool SrcIsSigned, ///< Whether to treat the source as signed
624 Type *Ty, ///< The Type to which the value should be casted
625 bool DstIsSigned ///< Whether to treate the dest. as signed
626 );
627
628 /// There are several places where we need to know if a cast instruction
629 /// only deals with integer source and destination types. To simplify that
630 /// logic, this method is provided.
631 /// @returns true iff the cast has only integral typed operand and dest type.
632 /// Determine if this is an integer-only cast.
633 bool isIntegerCast() const;
634
635 /// A lossless cast is one that does not alter the basic value. It implies
636 /// a no-op cast but is more stringent, preventing things like int->float,
637 /// long->double, or int->ptr.
638 /// @returns true iff the cast is lossless.
639 /// Determine if this is a lossless cast.
640 bool isLosslessCast() const;
641
642 /// A no-op cast is one that can be effected without changing any bits.
643 /// It implies that the source and destination types are the same size. The
644 /// DataLayout argument is to determine the pointer size when examining casts
645 /// involving Integer and Pointer types. They are no-op casts if the integer
646 /// is the same size as the pointer. However, pointer size varies with
647 /// platform. Note that a precondition of this method is that the cast is
648 /// legal - i.e. the instruction formed with these operands would verify.
649 static bool isNoopCast(
650 Instruction::CastOps Opcode, ///< Opcode of cast
651 Type *SrcTy, ///< SrcTy of cast
652 Type *DstTy, ///< DstTy of cast
653 const DataLayout &DL ///< DataLayout to get the Int Ptr type from.
654 );
655
656 /// Determine if this cast is a no-op cast.
657 ///
658 /// \param DL is the DataLayout to determine pointer size.
659 bool isNoopCast(const DataLayout &DL) const;
660
661 /// Determine how a pair of casts can be eliminated, if they can be at all.
662 /// This is a helper function for both CastInst and ConstantExpr.
663 /// @returns 0 if the CastInst pair can't be eliminated, otherwise
664 /// returns Instruction::CastOps value for a cast that can replace
665 /// the pair, casting SrcTy to DstTy.
666 /// Determine if a cast pair is eliminable
667 static unsigned isEliminableCastPair(
668 Instruction::CastOps firstOpcode, ///< Opcode of first cast
669 Instruction::CastOps secondOpcode, ///< Opcode of second cast
670 Type *SrcTy, ///< SrcTy of 1st cast
671 Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
672 Type *DstTy, ///< DstTy of 2nd cast
673 Type *SrcIntPtrTy, ///< Integer type corresponding to Ptr SrcTy, or null
674 Type *MidIntPtrTy, ///< Integer type corresponding to Ptr MidTy, or null
675 Type *DstIntPtrTy ///< Integer type corresponding to Ptr DstTy, or null
676 );
677
678 /// Return the opcode of this CastInst
679 Instruction::CastOps getOpcode() const {
680 return Instruction::CastOps(Instruction::getOpcode());
681 }
682
683 /// Return the source type, as a convenience
684 Type* getSrcTy() const { return getOperand(0)->getType(); }
685 /// Return the destination type, as a convenience
686 Type* getDestTy() const { return getType(); }
687
688 /// This method can be used to determine if a cast from SrcTy to DstTy using
689 /// Opcode op is valid or not.
690 /// @returns true iff the proposed cast is valid.
691 /// Determine if a cast is valid without creating one.
692 static bool castIsValid(Instruction::CastOps op, Type *SrcTy, Type *DstTy);
693 static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
694 return castIsValid(op, S->getType(), DstTy);
695 }
696
697 /// Methods for support type inquiry through isa, cast, and dyn_cast:
698 static bool classof(const Instruction *I) {
699 return I->isCast();
700 }
701 static bool classof(const Value *V) {
702 return isa<Instruction>(V) && classof(cast<Instruction>(V));
703 }
704};
705
706//===----------------------------------------------------------------------===//
707// CmpInst Class
708//===----------------------------------------------------------------------===//
709
710/// This class is the base class for the comparison instructions.
711/// Abstract base class of comparison instructions.
712class CmpInst : public Instruction {
713public:
714 /// This enumeration lists the possible predicates for CmpInst subclasses.
715 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
716 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
717 /// predicate values are not overlapping between the classes.
718 ///
719 /// Some passes (e.g. InstCombine) depend on the bit-wise characteristics of
720 /// FCMP_* values. Changing the bit patterns requires a potential change to
721 /// those passes.
722 enum Predicate : unsigned {
723 // Opcode U L G E Intuitive operation
724 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
725 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
726 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
727 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
728 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
729 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
730 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
731 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
732 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
733 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
734 FCMP_UGT = 10, ///< 1 0 1 0 True if unordered or greater than
735 FCMP_UGE = 11, ///< 1 0 1 1 True if unordered, greater than, or equal
736 FCMP_ULT = 12, ///< 1 1 0 0 True if unordered or less than
737 FCMP_ULE = 13, ///< 1 1 0 1 True if unordered, less than, or equal
738 FCMP_UNE = 14, ///< 1 1 1 0 True if unordered or not equal
739 FCMP_TRUE = 15, ///< 1 1 1 1 Always true (always folded)
740 FIRST_FCMP_PREDICATE = FCMP_FALSE,
741 LAST_FCMP_PREDICATE = FCMP_TRUE,
742 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
743 ICMP_EQ = 32, ///< equal
744 ICMP_NE = 33, ///< not equal
745 ICMP_UGT = 34, ///< unsigned greater than
746 ICMP_UGE = 35, ///< unsigned greater or equal
747 ICMP_ULT = 36, ///< unsigned less than
748 ICMP_ULE = 37, ///< unsigned less or equal
749 ICMP_SGT = 38, ///< signed greater than
750 ICMP_SGE = 39, ///< signed greater or equal
751 ICMP_SLT = 40, ///< signed less than
752 ICMP_SLE = 41, ///< signed less or equal
753 FIRST_ICMP_PREDICATE = ICMP_EQ,
754 LAST_ICMP_PREDICATE = ICMP_SLE,
755 BAD_ICMP_PREDICATE = ICMP_SLE + 1
756 };
757 using PredicateField =
758 Bitfield::Element<Predicate, 0, 6, LAST_ICMP_PREDICATE>;
759
760protected:
761 CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred,
762 Value *LHS, Value *RHS, const Twine &Name = "",
763 Instruction *InsertBefore = nullptr,
764 Instruction *FlagsSource = nullptr);
765
766 CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred,
767 Value *LHS, Value *RHS, const Twine &Name,
768 BasicBlock *InsertAtEnd);
769
770public:
771 // allocate space for exactly two operands
772 void *operator new(size_t s) {
773 return User::operator new(s, 2);
774 }
775
776 /// Construct a compare instruction, given the opcode, the predicate and
777 /// the two operands. Optionally (if InstBefore is specified) insert the
778 /// instruction into a BasicBlock right before the specified instruction.
779 /// The specified Instruction is allowed to be a dereferenced end iterator.
780 /// Create a CmpInst
781 static CmpInst *Create(OtherOps Op,
782 Predicate predicate, Value *S1,
783 Value *S2, const Twine &Name = "",
784 Instruction *InsertBefore = nullptr);
785
786 /// Construct a compare instruction, given the opcode, the predicate and the
787 /// two operands. Also automatically insert this instruction to the end of
788 /// the BasicBlock specified.
789 /// Create a CmpInst
790 static CmpInst *Create(OtherOps Op, Predicate predicate, Value *S1,
791 Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
792
793 /// Get the opcode casted to the right type
794 OtherOps getOpcode() const {
795 return static_cast<OtherOps>(Instruction::getOpcode());
796 }
797
798 /// Return the predicate for this instruction.
799 Predicate getPredicate() const { return getSubclassData<PredicateField>(); }
800
801 /// Set the predicate for this instruction to the specified value.
802 void setPredicate(Predicate P) { setSubclassData<PredicateField>(P); }
803
804 static bool isFPPredicate(Predicate P) {
805 static_assert(FIRST_FCMP_PREDICATE == 0,
806 "FIRST_FCMP_PREDICATE is required to be 0");
807 return P <= LAST_FCMP_PREDICATE;
808 }
809
810 static bool isIntPredicate(Predicate P) {
811 return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
812 }
813
814 static StringRef getPredicateName(Predicate P);
815
816 bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
817 bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
818
819 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
820 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
821 /// @returns the inverse predicate for the instruction's current predicate.
822 /// Return the inverse of the instruction's predicate.
823 Predicate getInversePredicate() const {
824 return getInversePredicate(getPredicate());
825 }
826
827 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
828 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
829 /// @returns the inverse predicate for predicate provided in \p pred.
830 /// Return the inverse of a given predicate
831 static Predicate getInversePredicate(Predicate pred);
832
833 /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
834 /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
835 /// @returns the predicate that would be the result of exchanging the two
836 /// operands of the CmpInst instruction without changing the result
837 /// produced.
838 /// Return the predicate as if the operands were swapped
839 Predicate getSwappedPredicate() const {
840 return getSwappedPredicate(getPredicate());
841 }
842
843 /// This is a static version that you can use without an instruction
844 /// available.
845 /// Return the predicate as if the operands were swapped.
846 static Predicate getSwappedPredicate(Predicate pred);
847
848 /// This is a static version that you can use without an instruction
849 /// available.
850 /// @returns true if the comparison predicate is strict, false otherwise.
851 static bool isStrictPredicate(Predicate predicate);
852
853 /// @returns true if the comparison predicate is strict, false otherwise.
854 /// Determine if this instruction is using an strict comparison predicate.
855 bool isStrictPredicate() const { return isStrictPredicate(getPredicate()); }
856
857 /// This is a static version that you can use without an instruction
858 /// available.
859 /// @returns true if the comparison predicate is non-strict, false otherwise.
860 static bool isNonStrictPredicate(Predicate predicate);
861
862 /// @returns true if the comparison predicate is non-strict, false otherwise.
863 /// Determine if this instruction is using an non-strict comparison predicate.
864 bool isNonStrictPredicate() const {
865 return isNonStrictPredicate(getPredicate());
866 }
867
868 /// For example, SGE -> SGT, SLE -> SLT, ULE -> ULT, UGE -> UGT.
869 /// Returns the strict version of non-strict comparisons.
870 Predicate getStrictPredicate() const {
871 return getStrictPredicate(getPredicate());
872 }
873
874 /// This is a static version that you can use without an instruction
875 /// available.
876 /// @returns the strict version of comparison provided in \p pred.
877 /// If \p pred is not a strict comparison predicate, returns \p pred.
878 /// Returns the strict version of non-strict comparisons.
879 static Predicate getStrictPredicate(Predicate pred);
880
881 /// For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE.
882 /// Returns the non-strict version of strict comparisons.
883 Predicate getNonStrictPredicate() const {
884 return getNonStrictPredicate(getPredicate());
885 }
886
887 /// This is a static version that you can use without an instruction
888 /// available.
889 /// @returns the non-strict version of comparison provided in \p pred.
890 /// If \p pred is not a strict comparison predicate, returns \p pred.
891 /// Returns the non-strict version of strict comparisons.
892 static Predicate getNonStrictPredicate(Predicate pred);
893
894 /// This is a static version that you can use without an instruction
895 /// available.
896 /// Return the flipped strictness of predicate
897 static Predicate getFlippedStrictnessPredicate(Predicate pred);
898
899 /// For predicate of kind "is X or equal to 0" returns the predicate "is X".
900 /// For predicate of kind "is X" returns the predicate "is X or equal to 0".
901 /// does not support other kind of predicates.
902 /// @returns the predicate that does not contains is equal to zero if
903 /// it had and vice versa.
904 /// Return the flipped strictness of predicate
905 Predicate getFlippedStrictnessPredicate() const {
906 return getFlippedStrictnessPredicate(getPredicate());
907 }
908
909 /// Provide more efficient getOperand methods.
910 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void
setOperand(unsigned, Value*); inline op_iterator op_begin();
inline const_op_iterator op_begin() const; inline op_iterator
op_end(); inline const_op_iterator op_end() const; protected
: template <int> inline Use &Op(); template <int
> inline const Use &Op() const; public: inline unsigned
getNumOperands() const
;
911
912 /// This is just a convenience that dispatches to the subclasses.
913 /// Swap the operands and adjust predicate accordingly to retain
914 /// the same comparison.
915 void swapOperands();
916
917 /// This is just a convenience that dispatches to the subclasses.
918 /// Determine if this CmpInst is commutative.
919 bool isCommutative() const;
920
921 /// Determine if this is an equals/not equals predicate.
922 /// This is a static version that you can use without an instruction
923 /// available.
924 static bool isEquality(Predicate pred);
925
926 /// Determine if this is an equals/not equals predicate.
927 bool isEquality() const { return isEquality(getPredicate()); }
928
929 /// Return true if the predicate is relational (not EQ or NE).
930 static bool isRelational(Predicate P) { return !isEquality(P); }
931
932 /// Return true if the predicate is relational (not EQ or NE).
933 bool isRelational() const { return !isEquality(); }
934
935 /// @returns true if the comparison is signed, false otherwise.
936 /// Determine if this instruction is using a signed comparison.
937 bool isSigned() const {
938 return isSigned(getPredicate());
939 }
940
941 /// @returns true if the comparison is unsigned, false otherwise.
942 /// Determine if this instruction is using an unsigned comparison.
943 bool isUnsigned() const {
944 return isUnsigned(getPredicate());
945 }
946
947 /// For example, ULT->SLT, ULE->SLE, UGT->SGT, UGE->SGE, SLT->Failed assert
948 /// @returns the signed version of the unsigned predicate pred.
949 /// return the signed version of a predicate
950 static Predicate getSignedPredicate(Predicate pred);
951
952 /// For example, ULT->SLT, ULE->SLE, UGT->SGT, UGE->SGE, SLT->Failed assert
953 /// @returns the signed version of the predicate for this instruction (which
954 /// has to be an unsigned predicate).
955 /// return the signed version of a predicate
956 Predicate getSignedPredicate() {
957 return getSignedPredicate(getPredicate());
958 }
959
960 /// For example, SLT->ULT, SLE->ULE, SGT->UGT, SGE->UGE, ULT->Failed assert
961 /// @returns the unsigned version of the signed predicate pred.
962 static Predicate getUnsignedPredicate(Predicate pred);
963
964 /// For example, SLT->ULT, SLE->ULE, SGT->UGT, SGE->UGE, ULT->Failed assert
965 /// @returns the unsigned version of the predicate for this instruction (which
966 /// has to be an signed predicate).
967 /// return the unsigned version of a predicate
968 Predicate getUnsignedPredicate() {
969 return getUnsignedPredicate(getPredicate());
970 }
971
972 /// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert
973 /// @returns the unsigned version of the signed predicate pred or
974 /// the signed version of the signed predicate pred.
975 static Predicate getFlippedSignednessPredicate(Predicate pred);
976
977 /// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert
978 /// @returns the unsigned version of the signed predicate pred or
979 /// the signed version of the signed predicate pred.
980 Predicate getFlippedSignednessPredicate() {
981 return getFlippedSignednessPredicate(getPredicate());
982 }
983
984 /// This is just a convenience.
985 /// Determine if this is true when both operands are the same.
986 bool isTrueWhenEqual() const {
987 return isTrueWhenEqual(getPredicate());
988 }
989
990 /// This is just a convenience.
991 /// Determine if this is false when both operands are the same.
992 bool isFalseWhenEqual() const {
993 return isFalseWhenEqual(getPredicate());
994 }
995
996 /// @returns true if the predicate is unsigned, false otherwise.
997 /// Determine if the predicate is an unsigned operation.
998 static bool isUnsigned(Predicate predicate);
999
1000 /// @returns true if the predicate is signed, false otherwise.
1001 /// Determine if the predicate is an signed operation.
1002 static bool isSigned(Predicate predicate);
1003
1004 /// Determine if the predicate is an ordered operation.
1005 static bool isOrdered(Predicate predicate);
1006
1007 /// Determine if the predicate is an unordered operation.
1008 static bool isUnordered(Predicate predicate);
1009
1010 /// Determine if the predicate is true when comparing a value with itself.
1011 static bool isTrueWhenEqual(Predicate predicate);
1012
1013 /// Determine if the predicate is false when comparing a value with itself.
1014 static bool isFalseWhenEqual(Predicate predicate);
1015
1016 /// Determine if Pred1 implies Pred2 is true when two compares have matching
1017 /// operands.
1018 static bool isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2);
1019
1020 /// Determine if Pred1 implies Pred2 is false when two compares have matching
1021 /// operands.
1022 static bool isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2);
1023
1024 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1025 static bool classof(const Instruction *I) {
1026 return I->getOpcode() == Instruction::ICmp ||
1027 I->getOpcode() == Instruction::FCmp;
1028 }
1029 static bool classof(const Value *V) {
1030 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1031 }
1032
1033 /// Create a result type for fcmp/icmp
1034 static Type* makeCmpResultType(Type* opnd_type) {
1035 if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) {
1036 return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
1037 vt->getElementCount());
1038 }
1039 return Type::getInt1Ty(opnd_type->getContext());
1040 }
1041
1042private:
1043 // Shadow Value::setValueSubclassData with a private forwarding method so that
1044 // subclasses cannot accidentally use it.
1045 void setValueSubclassData(unsigned short D) {
1046 Value::setValueSubclassData(D);
1047 }
1048};
1049
1050// FIXME: these are redundant if CmpInst < BinaryOperator
1051template <>
1052struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> {
1053};
1054
1055DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)CmpInst::op_iterator CmpInst::op_begin() { return OperandTraits
<CmpInst>::op_begin(this); } CmpInst::const_op_iterator
CmpInst::op_begin() const { return OperandTraits<CmpInst>
::op_begin(const_cast<CmpInst*>(this)); } CmpInst::op_iterator
CmpInst::op_end() { return OperandTraits<CmpInst>::op_end
(this); } CmpInst::const_op_iterator CmpInst::op_end() const {
return OperandTraits<CmpInst>::op_end(const_cast<CmpInst
*>(this)); } Value *CmpInst::getOperand(unsigned i_nocapture
) const { (static_cast <bool> (i_nocapture < OperandTraits
<CmpInst>::operands(this) && "getOperand() out of range!"
) ? void (0) : __assert_fail ("i_nocapture < OperandTraits<CmpInst>::operands(this) && \"getOperand() out of range!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1055, __extension__ __PRETTY_FUNCTION__)); return cast_or_null
<Value>( OperandTraits<CmpInst>::op_begin(const_cast
<CmpInst*>(this))[i_nocapture].get()); } void CmpInst::
setOperand(unsigned i_nocapture, Value *Val_nocapture) { (static_cast
<bool> (i_nocapture < OperandTraits<CmpInst>::
operands(this) && "setOperand() out of range!") ? void
(0) : __assert_fail ("i_nocapture < OperandTraits<CmpInst>::operands(this) && \"setOperand() out of range!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1055, __extension__ __PRETTY_FUNCTION__)); OperandTraits<
CmpInst>::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned
CmpInst::getNumOperands() const { return OperandTraits<CmpInst
>::operands(this); } template <int Idx_nocapture> Use
&CmpInst::Op() { return this->OpFrom<Idx_nocapture
>(this); } template <int Idx_nocapture> const Use &
CmpInst::Op() const { return this->OpFrom<Idx_nocapture
>(this); }
1056
1057/// A lightweight accessor for an operand bundle meant to be passed
1058/// around by value.
1059struct OperandBundleUse {
1060 ArrayRef<Use> Inputs;
1061
1062 OperandBundleUse() = default;
1063 explicit OperandBundleUse(StringMapEntry<uint32_t> *Tag, ArrayRef<Use> Inputs)
1064 : Inputs(Inputs), Tag(Tag) {}
1065
1066 /// Return true if the operand at index \p Idx in this operand bundle
1067 /// has the attribute A.
1068 bool operandHasAttr(unsigned Idx, Attribute::AttrKind A) const {
1069 if (isDeoptOperandBundle())
1070 if (A == Attribute::ReadOnly || A == Attribute::NoCapture)
1071 return Inputs[Idx]->getType()->isPointerTy();
1072
1073 // Conservative answer: no operands have any attributes.
1074 return false;
1075 }
1076
1077 /// Return the tag of this operand bundle as a string.
1078 StringRef getTagName() const {
1079 return Tag->getKey();
1080 }
1081
1082 /// Return the tag of this operand bundle as an integer.
1083 ///
1084 /// Operand bundle tags are interned by LLVMContextImpl::getOrInsertBundleTag,
1085 /// and this function returns the unique integer getOrInsertBundleTag
1086 /// associated the tag of this operand bundle to.
1087 uint32_t getTagID() const {
1088 return Tag->getValue();
1089 }
1090
1091 /// Return true if this is a "deopt" operand bundle.
1092 bool isDeoptOperandBundle() const {
1093 return getTagID() == LLVMContext::OB_deopt;
1094 }
1095
1096 /// Return true if this is a "funclet" operand bundle.
1097 bool isFuncletOperandBundle() const {
1098 return getTagID() == LLVMContext::OB_funclet;
1099 }
1100
1101 /// Return true if this is a "cfguardtarget" operand bundle.
1102 bool isCFGuardTargetOperandBundle() const {
1103 return getTagID() == LLVMContext::OB_cfguardtarget;
1104 }
1105
1106private:
1107 /// Pointer to an entry in LLVMContextImpl::getOrInsertBundleTag.
1108 StringMapEntry<uint32_t> *Tag;
1109};
1110
1111/// A container for an operand bundle being viewed as a set of values
1112/// rather than a set of uses.
1113///
1114/// Unlike OperandBundleUse, OperandBundleDefT owns the memory it carries, and
1115/// so it is possible to create and pass around "self-contained" instances of
1116/// OperandBundleDef and ConstOperandBundleDef.
1117template <typename InputTy> class OperandBundleDefT {
1118 std::string Tag;
1119 std::vector<InputTy> Inputs;
1120
1121public:
1122 explicit OperandBundleDefT(std::string Tag, std::vector<InputTy> Inputs)
1123 : Tag(std::move(Tag)), Inputs(std::move(Inputs)) {}
1124 explicit OperandBundleDefT(std::string Tag, ArrayRef<InputTy> Inputs)
1125 : Tag(std::move(Tag)), Inputs(Inputs) {}
1126
1127 explicit OperandBundleDefT(const OperandBundleUse &OBU) {
1128 Tag = std::string(OBU.getTagName());
1129 llvm::append_range(Inputs, OBU.Inputs);
1130 }
1131
1132 ArrayRef<InputTy> inputs() const { return Inputs; }
1133
1134 using input_iterator = typename std::vector<InputTy>::const_iterator;
1135
1136 size_t input_size() const { return Inputs.size(); }
1137 input_iterator input_begin() const { return Inputs.begin(); }
1138 input_iterator input_end() const { return Inputs.end(); }
1139
1140 StringRef getTag() const { return Tag; }
1141};
1142
1143using OperandBundleDef = OperandBundleDefT<Value *>;
1144using ConstOperandBundleDef = OperandBundleDefT<const Value *>;
1145
1146//===----------------------------------------------------------------------===//
1147// CallBase Class
1148//===----------------------------------------------------------------------===//
1149
1150/// Base class for all callable instructions (InvokeInst and CallInst)
1151/// Holds everything related to calling a function.
1152///
1153/// All call-like instructions are required to use a common operand layout:
1154/// - Zero or more arguments to the call,
1155/// - Zero or more operand bundles with zero or more operand inputs each
1156/// bundle,
1157/// - Zero or more subclass controlled operands
1158/// - The called function.
1159///
1160/// This allows this base class to easily access the called function and the
1161/// start of the arguments without knowing how many other operands a particular
1162/// subclass requires. Note that accessing the end of the argument list isn't
1163/// as cheap as most other operations on the base class.
1164class CallBase : public Instruction {
1165protected:
1166 // The first two bits are reserved by CallInst for fast retrieval,
1167 using CallInstReservedField = Bitfield::Element<unsigned, 0, 2>;
1168 using CallingConvField =
1169 Bitfield::Element<CallingConv::ID, CallInstReservedField::NextBit, 10,
1170 CallingConv::MaxID>;
1171 static_assert(
1172 Bitfield::areContiguous<CallInstReservedField, CallingConvField>(),
1173 "Bitfields must be contiguous");
1174
1175 /// The last operand is the called operand.
1176 static constexpr int CalledOperandOpEndIdx = -1;
1177
1178 AttributeList Attrs; ///< parameter attributes for callable
1179 FunctionType *FTy;
1180
1181 template <class... ArgsTy>
1182 CallBase(AttributeList const &A, FunctionType *FT, ArgsTy &&... Args)
1183 : Instruction(std::forward<ArgsTy>(Args)...), Attrs(A), FTy(FT) {}
1184
1185 using Instruction::Instruction;
1186
1187 bool hasDescriptor() const { return Value::HasDescriptor; }
1188
1189 unsigned getNumSubclassExtraOperands() const {
1190 switch (getOpcode()) {
1191 case Instruction::Call:
1192 return 0;
1193 case Instruction::Invoke:
1194 return 2;
1195 case Instruction::CallBr:
1196 return getNumSubclassExtraOperandsDynamic();
1197 }
1198 llvm_unreachable("Invalid opcode!")::llvm::llvm_unreachable_internal("Invalid opcode!", "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1198)
;
1199 }
1200
1201 /// Get the number of extra operands for instructions that don't have a fixed
1202 /// number of extra operands.
1203 unsigned getNumSubclassExtraOperandsDynamic() const;
1204
1205public:
1206 using Instruction::getContext;
1207
1208 /// Create a clone of \p CB with a different set of operand bundles and
1209 /// insert it before \p InsertPt.
1210 ///
1211 /// The returned call instruction is identical \p CB in every way except that
1212 /// the operand bundles for the new instruction are set to the operand bundles
1213 /// in \p Bundles.
1214 static CallBase *Create(CallBase *CB, ArrayRef<OperandBundleDef> Bundles,
1215 Instruction *InsertPt = nullptr);
1216
1217 /// Create a clone of \p CB with the operand bundle with the tag matching
1218 /// \p Bundle's tag replaced with Bundle, and insert it before \p InsertPt.
1219 ///
1220 /// The returned call instruction is identical \p CI in every way except that
1221 /// the specified operand bundle has been replaced.
1222 static CallBase *Create(CallBase *CB,
1223 OperandBundleDef Bundle,
1224 Instruction *InsertPt = nullptr);
1225
1226 /// Create a clone of \p CB with operand bundle \p OB added.
1227 static CallBase *addOperandBundle(CallBase *CB, uint32_t ID,
1228 OperandBundleDef OB,
1229 Instruction *InsertPt = nullptr);
1230
1231 /// Create a clone of \p CB with operand bundle \p ID removed.
1232 static CallBase *removeOperandBundle(CallBase *CB, uint32_t ID,
1233 Instruction *InsertPt = nullptr);
1234
1235 static bool classof(const Instruction *I) {
1236 return I->getOpcode() == Instruction::Call ||
1237 I->getOpcode() == Instruction::Invoke ||
1238 I->getOpcode() == Instruction::CallBr;
1239 }
1240 static bool classof(const Value *V) {
1241 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1242 }
1243
1244 FunctionType *getFunctionType() const { return FTy; }
1245
1246 void mutateFunctionType(FunctionType *FTy) {
1247 Value::mutateType(FTy->getReturnType());
1248 this->FTy = FTy;
1249 }
1250
1251 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void
setOperand(unsigned, Value*); inline op_iterator op_begin();
inline const_op_iterator op_begin() const; inline op_iterator
op_end(); inline const_op_iterator op_end() const; protected
: template <int> inline Use &Op(); template <int
> inline const Use &Op() const; public: inline unsigned
getNumOperands() const
;
1252
1253 /// data_operands_begin/data_operands_end - Return iterators iterating over
1254 /// the call / invoke argument list and bundle operands. For invokes, this is
1255 /// the set of instruction operands except the invoke target and the two
1256 /// successor blocks; and for calls this is the set of instruction operands
1257 /// except the call target.
1258 User::op_iterator data_operands_begin() { return op_begin(); }
1259 User::const_op_iterator data_operands_begin() const {
1260 return const_cast<CallBase *>(this)->data_operands_begin();
1261 }
1262 User::op_iterator data_operands_end() {
1263 // Walk from the end of the operands over the called operand and any
1264 // subclass operands.
1265 return op_end() - getNumSubclassExtraOperands() - 1;
1266 }
1267 User::const_op_iterator data_operands_end() const {
1268 return const_cast<CallBase *>(this)->data_operands_end();
1269 }
1270 iterator_range<User::op_iterator> data_ops() {
1271 return make_range(data_operands_begin(), data_operands_end());
1272 }
1273 iterator_range<User::const_op_iterator> data_ops() const {
1274 return make_range(data_operands_begin(), data_operands_end());
1275 }
1276 bool data_operands_empty() const {
1277 return data_operands_end() == data_operands_begin();
1278 }
1279 unsigned data_operands_size() const {
1280 return std::distance(data_operands_begin(), data_operands_end());
1281 }
1282
1283 bool isDataOperand(const Use *U) const {
1284 assert(this == U->getUser() &&(static_cast <bool> (this == U->getUser() &&
"Only valid to query with a use of this instruction!") ? void
(0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1285, __extension__ __PRETTY_FUNCTION__))
1285 "Only valid to query with a use of this instruction!")(static_cast <bool> (this == U->getUser() &&
"Only valid to query with a use of this instruction!") ? void
(0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1285, __extension__ __PRETTY_FUNCTION__))
;
1286 return data_operands_begin() <= U && U < data_operands_end();
1287 }
1288 bool isDataOperand(Value::const_user_iterator UI) const {
1289 return isDataOperand(&UI.getUse());
1290 }
1291
1292 /// Given a value use iterator, return the data operand corresponding to it.
1293 /// Iterator must actually correspond to a data operand.
1294 unsigned getDataOperandNo(Value::const_user_iterator UI) const {
1295 return getDataOperandNo(&UI.getUse());
1296 }
1297
1298 /// Given a use for a data operand, get the data operand number that
1299 /// corresponds to it.
1300 unsigned getDataOperandNo(const Use *U) const {
1301 assert(isDataOperand(U) && "Data operand # out of range!")(static_cast <bool> (isDataOperand(U) && "Data operand # out of range!"
) ? void (0) : __assert_fail ("isDataOperand(U) && \"Data operand # out of range!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1301, __extension__ __PRETTY_FUNCTION__))
;
1302 return U - data_operands_begin();
1303 }
1304
1305 /// Return the iterator pointing to the beginning of the argument list.
1306 User::op_iterator arg_begin() { return op_begin(); }
1307 User::const_op_iterator arg_begin() const {
1308 return const_cast<CallBase *>(this)->arg_begin();
1309 }
1310
1311 /// Return the iterator pointing to the end of the argument list.
1312 User::op_iterator arg_end() {
1313 // From the end of the data operands, walk backwards past the bundle
1314 // operands.
1315 return data_operands_end() - getNumTotalBundleOperands();
1316 }
1317 User::const_op_iterator arg_end() const {
1318 return const_cast<CallBase *>(this)->arg_end();
1319 }
1320
1321 /// Iteration adapter for range-for loops.
1322 iterator_range<User::op_iterator> args() {
1323 return make_range(arg_begin(), arg_end());
1324 }
1325 iterator_range<User::const_op_iterator> args() const {
1326 return make_range(arg_begin(), arg_end());
1327 }
1328 bool arg_empty() const { return arg_end() == arg_begin(); }
1329 unsigned arg_size() const { return arg_end() - arg_begin(); }
1330
1331 // Legacy API names that duplicate the above and will be removed once users
1332 // are migrated.
1333 iterator_range<User::op_iterator> arg_operands() {
1334 return make_range(arg_begin(), arg_end());
1335 }
1336 iterator_range<User::const_op_iterator> arg_operands() const {
1337 return make_range(arg_begin(), arg_end());
1338 }
1339 unsigned getNumArgOperands() const { return arg_size(); }
1340
1341 Value *getArgOperand(unsigned i) const {
1342 assert(i < getNumArgOperands() && "Out of bounds!")(static_cast <bool> (i < getNumArgOperands() &&
"Out of bounds!") ? void (0) : __assert_fail ("i < getNumArgOperands() && \"Out of bounds!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1342, __extension__ __PRETTY_FUNCTION__))
;
1343 return getOperand(i);
1344 }
1345
1346 void setArgOperand(unsigned i, Value *v) {
1347 assert(i < getNumArgOperands() && "Out of bounds!")(static_cast <bool> (i < getNumArgOperands() &&
"Out of bounds!") ? void (0) : __assert_fail ("i < getNumArgOperands() && \"Out of bounds!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1347, __extension__ __PRETTY_FUNCTION__))
;
1348 setOperand(i, v);
1349 }
1350
1351 /// Wrappers for getting the \c Use of a call argument.
1352 const Use &getArgOperandUse(unsigned i) const {
1353 assert(i < getNumArgOperands() && "Out of bounds!")(static_cast <bool> (i < getNumArgOperands() &&
"Out of bounds!") ? void (0) : __assert_fail ("i < getNumArgOperands() && \"Out of bounds!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1353, __extension__ __PRETTY_FUNCTION__))
;
1354 return User::getOperandUse(i);
1355 }
1356 Use &getArgOperandUse(unsigned i) {
1357 assert(i < getNumArgOperands() && "Out of bounds!")(static_cast <bool> (i < getNumArgOperands() &&
"Out of bounds!") ? void (0) : __assert_fail ("i < getNumArgOperands() && \"Out of bounds!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1357, __extension__ __PRETTY_FUNCTION__))
;
1358 return User::getOperandUse(i);
1359 }
1360
1361 bool isArgOperand(const Use *U) const {
1362 assert(this == U->getUser() &&(static_cast <bool> (this == U->getUser() &&
"Only valid to query with a use of this instruction!") ? void
(0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1363, __extension__ __PRETTY_FUNCTION__))
1363 "Only valid to query with a use of this instruction!")(static_cast <bool> (this == U->getUser() &&
"Only valid to query with a use of this instruction!") ? void
(0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1363, __extension__ __PRETTY_FUNCTION__))
;
1364 return arg_begin() <= U && U < arg_end();
1365 }
1366 bool isArgOperand(Value::const_user_iterator UI) const {
1367 return isArgOperand(&UI.getUse());
1368 }
1369
1370 /// Given a use for a arg operand, get the arg operand number that
1371 /// corresponds to it.
1372 unsigned getArgOperandNo(const Use *U) const {
1373 assert(isArgOperand(U) && "Arg operand # out of range!")(static_cast <bool> (isArgOperand(U) && "Arg operand # out of range!"
) ? void (0) : __assert_fail ("isArgOperand(U) && \"Arg operand # out of range!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1373, __extension__ __PRETTY_FUNCTION__))
;
1374 return U - arg_begin();
1375 }
1376
1377 /// Given a value use iterator, return the arg operand number corresponding to
1378 /// it. Iterator must actually correspond to a data operand.
1379 unsigned getArgOperandNo(Value::const_user_iterator UI) const {
1380 return getArgOperandNo(&UI.getUse());
1381 }
1382
1383 /// Returns true if this CallSite passes the given Value* as an argument to
1384 /// the called function.
1385 bool hasArgument(const Value *V) const {
1386 return llvm::is_contained(args(), V);
1387 }
1388
1389 Value *getCalledOperand() const { return Op<CalledOperandOpEndIdx>(); }
1390
1391 const Use &getCalledOperandUse() const { return Op<CalledOperandOpEndIdx>(); }
1392 Use &getCalledOperandUse() { return Op<CalledOperandOpEndIdx>(); }
1393
1394 /// Returns the function called, or null if this is an
1395 /// indirect function invocation.
1396 Function *getCalledFunction() const {
1397 return dyn_cast_or_null<Function>(getCalledOperand());
1398 }
1399
1400 /// Return true if the callsite is an indirect call.
1401 bool isIndirectCall() const;
1402
1403 /// Determine whether the passed iterator points to the callee operand's Use.
1404 bool isCallee(Value::const_user_iterator UI) const {
1405 return isCallee(&UI.getUse());
1406 }
1407
1408 /// Determine whether this Use is the callee operand's Use.
1409 bool isCallee(const Use *U) const { return &getCalledOperandUse() == U; }
1410
1411 /// Helper to get the caller (the parent function).
1412 Function *getCaller();
1413 const Function *getCaller() const {
1414 return const_cast<CallBase *>(this)->getCaller();
1415 }
1416
1417 /// Tests if this call site must be tail call optimized. Only a CallInst can
1418 /// be tail call optimized.
1419 bool isMustTailCall() const;
1420
1421 /// Tests if this call site is marked as a tail call.
1422 bool isTailCall() const;
1423
1424 /// Returns the intrinsic ID of the intrinsic called or
1425 /// Intrinsic::not_intrinsic if the called function is not an intrinsic, or if
1426 /// this is an indirect call.
1427 Intrinsic::ID getIntrinsicID() const;
1428
1429 void setCalledOperand(Value *V) { Op<CalledOperandOpEndIdx>() = V; }
1430
1431 /// Sets the function called, including updating the function type.
1432 void setCalledFunction(Function *Fn) {
1433 setCalledFunction(Fn->getFunctionType(), Fn);
1434 }
1435
1436 /// Sets the function called, including updating the function type.
1437 void setCalledFunction(FunctionCallee Fn) {
1438 setCalledFunction(Fn.getFunctionType(), Fn.getCallee());
1439 }
1440
1441 /// Sets the function called, including updating to the specified function
1442 /// type.
1443 void setCalledFunction(FunctionType *FTy, Value *Fn) {
1444 this->FTy = FTy;
1445 assert(FTy == cast<FunctionType>((static_cast <bool> (FTy == cast<FunctionType>( cast
<PointerType>(Fn->getType())->getElementType())) ?
void (0) : __assert_fail ("FTy == cast<FunctionType>( cast<PointerType>(Fn->getType())->getElementType())"
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1446, __extension__ __PRETTY_FUNCTION__))
1446 cast<PointerType>(Fn->getType())->getElementType()))(static_cast <bool> (FTy == cast<FunctionType>( cast
<PointerType>(Fn->getType())->getElementType())) ?
void (0) : __assert_fail ("FTy == cast<FunctionType>( cast<PointerType>(Fn->getType())->getElementType())"
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1446, __extension__ __PRETTY_FUNCTION__))
;
1447 // This function doesn't mutate the return type, only the function
1448 // type. Seems broken, but I'm just gonna stick an assert in for now.
1449 assert(getType() == FTy->getReturnType())(static_cast <bool> (getType() == FTy->getReturnType
()) ? void (0) : __assert_fail ("getType() == FTy->getReturnType()"
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1449, __extension__ __PRETTY_FUNCTION__))
;
1450 setCalledOperand(Fn);
1451 }
1452
1453 CallingConv::ID getCallingConv() const {
1454 return getSubclassData<CallingConvField>();
1455 }
1456
1457 void setCallingConv(CallingConv::ID CC) {
1458 setSubclassData<CallingConvField>(CC);
1459 }
1460
1461 /// Check if this call is an inline asm statement.
1462 bool isInlineAsm() const { return isa<InlineAsm>(getCalledOperand()); }
1463
1464 /// \name Attribute API
1465 ///
1466 /// These methods access and modify attributes on this call (including
1467 /// looking through to the attributes on the called function when necessary).
1468 ///@{
1469
1470 /// Return the parameter attributes for this call.
1471 ///
1472 AttributeList getAttributes() const { return Attrs; }
1473
1474 /// Set the parameter attributes for this call.
1475 ///
1476 void setAttributes(AttributeList A) { Attrs = A; }
1477
1478 /// Determine whether this call has the given attribute. If it does not
1479 /// then determine if the called function has the attribute, but only if
1480 /// the attribute is allowed for the call.
1481 bool hasFnAttr(Attribute::AttrKind Kind) const {
1482 assert(Kind != Attribute::NoBuiltin &&(static_cast <bool> (Kind != Attribute::NoBuiltin &&
"Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin"
) ? void (0) : __assert_fail ("Kind != Attribute::NoBuiltin && \"Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1483, __extension__ __PRETTY_FUNCTION__))
23
'?' condition is true
1483 "Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin")(static_cast <bool> (Kind != Attribute::NoBuiltin &&
"Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin"
) ? void (0) : __assert_fail ("Kind != Attribute::NoBuiltin && \"Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1483, __extension__ __PRETTY_FUNCTION__))
;
1484 return hasFnAttrImpl(Kind);
24
Calling 'CallBase::hasFnAttrImpl'
33
Returning from 'CallBase::hasFnAttrImpl'
34
Returning value, which participates in a condition later
1485 }
1486
1487 /// Determine whether this call has the given attribute. If it does not
1488 /// then determine if the called function has the attribute, but only if
1489 /// the attribute is allowed for the call.
1490 bool hasFnAttr(StringRef Kind) const { return hasFnAttrImpl(Kind); }
1491
1492 /// adds the attribute to the list of attributes.
1493 void addAttribute(unsigned i, Attribute::AttrKind Kind) {
1494 AttributeList PAL = getAttributes();
1495 PAL = PAL.addAttribute(getContext(), i, Kind);
1496 setAttributes(PAL);
1497 }
1498
1499 /// adds the attribute to the list of attributes.
1500 void addAttribute(unsigned i, Attribute Attr) {
1501 AttributeList PAL = getAttributes();
1502 PAL = PAL.addAttribute(getContext(), i, Attr);
1503 setAttributes(PAL);
1504 }
1505
1506 /// Adds the attribute to the indicated argument
1507 void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
1508 assert(ArgNo < getNumArgOperands() && "Out of bounds")(static_cast <bool> (ArgNo < getNumArgOperands() &&
"Out of bounds") ? void (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1508, __extension__ __PRETTY_FUNCTION__))
;
1509 AttributeList PAL = getAttributes();
1510 PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind);
1511 setAttributes(PAL);
1512 }
1513
1514 /// Adds the attribute to the indicated argument
1515 void addParamAttr(unsigned ArgNo, Attribute Attr) {
1516 assert(ArgNo < getNumArgOperands() && "Out of bounds")(static_cast <bool> (ArgNo < getNumArgOperands() &&
"Out of bounds") ? void (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1516, __extension__ __PRETTY_FUNCTION__))
;
1517 AttributeList PAL = getAttributes();
1518 PAL = PAL.addParamAttribute(getContext(), ArgNo, Attr);
1519 setAttributes(PAL);
1520 }
1521
1522 /// removes the attribute from the list of attributes.
1523 void removeAttribute(unsigned i, Attribute::AttrKind Kind) {
1524 AttributeList PAL = getAttributes();
1525 PAL = PAL.removeAttribute(getContext(), i, Kind);
1526 setAttributes(PAL);
1527 }
1528
1529 /// removes the attribute from the list of attributes.
1530 void removeAttribute(unsigned i, StringRef Kind) {
1531 AttributeList PAL = getAttributes();
1532 PAL = PAL.removeAttribute(getContext(), i, Kind);
1533 setAttributes(PAL);
1534 }
1535
1536 void removeAttributes(unsigned i, const AttrBuilder &Attrs) {
1537 AttributeList PAL = getAttributes();
1538 PAL = PAL.removeAttributes(getContext(), i, Attrs);
1539 setAttributes(PAL);
1540 }
1541
1542 /// Removes the attribute from the given argument
1543 void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
1544 assert(ArgNo < getNumArgOperands() && "Out of bounds")(static_cast <bool> (ArgNo < getNumArgOperands() &&
"Out of bounds") ? void (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1544, __extension__ __PRETTY_FUNCTION__))
;
1545 AttributeList PAL = getAttributes();
1546 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
1547 setAttributes(PAL);
1548 }
1549
1550 /// Removes the attribute from the given argument
1551 void removeParamAttr(unsigned ArgNo, StringRef Kind) {
1552 assert(ArgNo < getNumArgOperands() && "Out of bounds")(static_cast <bool> (ArgNo < getNumArgOperands() &&
"Out of bounds") ? void (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1552, __extension__ __PRETTY_FUNCTION__))
;
1553 AttributeList PAL = getAttributes();
1554 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
1555 setAttributes(PAL);
1556 }
1557
1558 /// Removes noundef and other attributes that imply undefined behavior if a
1559 /// `undef` or `poison` value is passed from the given argument.
1560 void removeParamUndefImplyingAttrs(unsigned ArgNo) {
1561 assert(ArgNo < getNumArgOperands() && "Out of bounds")(static_cast <bool> (ArgNo < getNumArgOperands() &&
"Out of bounds") ? void (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1561, __extension__ __PRETTY_FUNCTION__))
;
1562 AttributeList PAL = getAttributes();
1563 PAL = PAL.removeParamUndefImplyingAttributes(getContext(), ArgNo);
1564 setAttributes(PAL);
1565 }
1566
1567 /// adds the dereferenceable attribute to the list of attributes.
1568 void addDereferenceableAttr(unsigned i, uint64_t Bytes) {
1569 AttributeList PAL = getAttributes();
1570 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
1571 setAttributes(PAL);
1572 }
1573
1574 /// adds the dereferenceable_or_null attribute to the list of
1575 /// attributes.
1576 void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
1577 AttributeList PAL = getAttributes();
1578 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
1579 setAttributes(PAL);
1580 }
1581
1582 /// Determine whether the return value has the given attribute.
1583 bool hasRetAttr(Attribute::AttrKind Kind) const {
1584 return hasRetAttrImpl(Kind);
1585 }
1586 /// Determine whether the return value has the given attribute.
1587 bool hasRetAttr(StringRef Kind) const { return hasRetAttrImpl(Kind); }
1588
1589 /// Determine whether the argument or parameter has the given attribute.
1590 bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const;
1591
1592 /// Get the attribute of a given kind at a position.
1593 Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const {
1594 return getAttributes().getAttribute(i, Kind);
1595 }
1596
1597 /// Get the attribute of a given kind at a position.
1598 Attribute getAttribute(unsigned i, StringRef Kind) const {
1599 return getAttributes().getAttribute(i, Kind);
1600 }
1601
1602 /// Get the attribute of a given kind from a given arg
1603 Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
1604 assert(ArgNo < getNumArgOperands() && "Out of bounds")(static_cast <bool> (ArgNo < getNumArgOperands() &&
"Out of bounds") ? void (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1604, __extension__ __PRETTY_FUNCTION__))
;
1605 return getAttributes().getParamAttr(ArgNo, Kind);
1606 }
1607
1608 /// Get the attribute of a given kind from a given arg
1609 Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const {
1610 assert(ArgNo < getNumArgOperands() && "Out of bounds")(static_cast <bool> (ArgNo < getNumArgOperands() &&
"Out of bounds") ? void (0) : __assert_fail ("ArgNo < getNumArgOperands() && \"Out of bounds\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1610, __extension__ __PRETTY_FUNCTION__))
;
1611 return getAttributes().getParamAttr(ArgNo, Kind);
1612 }
1613
1614 /// Return true if the data operand at index \p i has the attribute \p
1615 /// A.
1616 ///
1617 /// Data operands include call arguments and values used in operand bundles,
1618 /// but does not include the callee operand. This routine dispatches to the
1619 /// underlying AttributeList or the OperandBundleUser as appropriate.
1620 ///
1621 /// The index \p i is interpreted as
1622 ///
1623 /// \p i == Attribute::ReturnIndex -> the return value
1624 /// \p i in [1, arg_size + 1) -> argument number (\p i - 1)
1625 /// \p i in [arg_size + 1, data_operand_size + 1) -> bundle operand at index
1626 /// (\p i - 1) in the operand list.
1627 bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const {
1628 // Note that we have to add one because `i` isn't zero-indexed.
1629 assert(i < (getNumArgOperands() + getNumTotalBundleOperands() + 1) &&(static_cast <bool> (i < (getNumArgOperands() + getNumTotalBundleOperands
() + 1) && "Data operand index out of bounds!") ? void
(0) : __assert_fail ("i < (getNumArgOperands() + getNumTotalBundleOperands() + 1) && \"Data operand index out of bounds!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1630, __extension__ __PRETTY_FUNCTION__))
1630 "Data operand index out of bounds!")(static_cast <bool> (i < (getNumArgOperands() + getNumTotalBundleOperands
() + 1) && "Data operand index out of bounds!") ? void
(0) : __assert_fail ("i < (getNumArgOperands() + getNumTotalBundleOperands() + 1) && \"Data operand index out of bounds!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1630, __extension__ __PRETTY_FUNCTION__))
;
1631
1632 // The attribute A can either be directly specified, if the operand in
1633 // question is a call argument; or be indirectly implied by the kind of its
1634 // containing operand bundle, if the operand is a bundle operand.
1635
1636 if (i == AttributeList::ReturnIndex)
1637 return hasRetAttr(Kind);
1638
1639 // FIXME: Avoid these i - 1 calculations and update the API to use
1640 // zero-based indices.
1641 if (i < (getNumArgOperands() + 1))
1642 return paramHasAttr(i - 1, Kind);
1643
1644 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&(static_cast <bool> (hasOperandBundles() && i >=
(getBundleOperandsStartIndex() + 1) && "Must be either a call argument or an operand bundle!"
) ? void (0) : __assert_fail ("hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) && \"Must be either a call argument or an operand bundle!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1645, __extension__ __PRETTY_FUNCTION__))
1645 "Must be either a call argument or an operand bundle!")(static_cast <bool> (hasOperandBundles() && i >=
(getBundleOperandsStartIndex() + 1) && "Must be either a call argument or an operand bundle!"
) ? void (0) : __assert_fail ("hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) && \"Must be either a call argument or an operand bundle!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1645, __extension__ __PRETTY_FUNCTION__))
;
1646 return bundleOperandHasAttr(i - 1, Kind);
1647 }
1648
1649 /// Determine whether this data operand is not captured.
1650 // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to
1651 // better indicate that this may return a conservative answer.
1652 bool doesNotCapture(unsigned OpNo) const {
1653 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::NoCapture);
1654 }
1655
1656 /// Determine whether this argument is passed by value.
1657 bool isByValArgument(unsigned ArgNo) const {
1658 return paramHasAttr(ArgNo, Attribute::ByVal);
1659 }
1660
1661 /// Determine whether this argument is passed in an alloca.
1662 bool isInAllocaArgument(unsigned ArgNo) const {
1663 return paramHasAttr(ArgNo, Attribute::InAlloca);
1664 }
1665
1666 /// Determine whether this argument is passed by value, in an alloca, or is
1667 /// preallocated.
1668 bool isPassPointeeByValueArgument(unsigned ArgNo) const {
1669 return paramHasAttr(ArgNo, Attribute::ByVal) ||
1670 paramHasAttr(ArgNo, Attribute::InAlloca) ||
1671 paramHasAttr(ArgNo, Attribute::Preallocated);
1672 }
1673
1674 /// Determine whether passing undef to this argument is undefined behavior.
1675 /// If passing undef to this argument is UB, passing poison is UB as well
1676 /// because poison is more undefined than undef.
1677 bool isPassingUndefUB(unsigned ArgNo) const {
1678 return paramHasAttr(ArgNo, Attribute::NoUndef) ||
1679 // dereferenceable implies noundef.
1680 paramHasAttr(ArgNo, Attribute::Dereferenceable) ||
1681 // dereferenceable implies noundef, and null is a well-defined value.
1682 paramHasAttr(ArgNo, Attribute::DereferenceableOrNull);
1683 }
1684
1685 /// Determine if there are is an inalloca argument. Only the last argument can
1686 /// have the inalloca attribute.
1687 bool hasInAllocaArgument() const {
1688 return !arg_empty() && paramHasAttr(arg_size() - 1, Attribute::InAlloca);
1689 }
1690
1691 // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to
1692 // better indicate that this may return a conservative answer.
1693 bool doesNotAccessMemory(unsigned OpNo) const {
1694 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone);
1695 }
1696
1697 // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to
1698 // better indicate that this may return a conservative answer.
1699 bool onlyReadsMemory(unsigned OpNo) const {
1700 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadOnly) ||
1701 dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone);
1702 }
1703
1704 // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to
1705 // better indicate that this may return a conservative answer.
1706 bool doesNotReadMemory(unsigned OpNo) const {
1707 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::WriteOnly) ||
1708 dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone);
1709 }
1710
1711 LLVM_ATTRIBUTE_DEPRECATED(unsigned getRetAlignment() const,[[deprecated("Use getRetAlign() instead")]] unsigned getRetAlignment
() const
1712 "Use getRetAlign() instead")[[deprecated("Use getRetAlign() instead")]] unsigned getRetAlignment
() const
{
1713 if (const auto MA = Attrs.getRetAlignment())
1714 return MA->value();
1715 return 0;
1716 }
1717
1718 /// Extract the alignment of the return value.
1719 MaybeAlign getRetAlign() const { return Attrs.getRetAlignment(); }
1720
1721 /// Extract the alignment for a call or parameter (0=unknown).
1722 LLVM_ATTRIBUTE_DEPRECATED(unsigned getParamAlignment(unsigned ArgNo) const,[[deprecated("Use getParamAlign() instead")]] unsigned getParamAlignment
(unsigned ArgNo) const
1723 "Use getParamAlign() instead")[[deprecated("Use getParamAlign() instead")]] unsigned getParamAlignment
(unsigned ArgNo) const
{
1724 if (const auto MA = Attrs.getParamAlignment(ArgNo))
1725 return MA->value();
1726 return 0;
1727 }
1728
1729 /// Extract the alignment for a call or parameter (0=unknown).
1730 MaybeAlign getParamAlign(unsigned ArgNo) const {
1731 return Attrs.getParamAlignment(ArgNo);
1732 }
1733
1734 MaybeAlign getParamStackAlign(unsigned ArgNo) const {
1735 return Attrs.getParamStackAlignment(ArgNo);
1736 }
1737
1738 /// Extract the byval type for a call or parameter.
1739 Type *getParamByValType(unsigned ArgNo) const {
1740 Type *Ty = Attrs.getParamByValType(ArgNo);
1741 return Ty ? Ty : getArgOperand(ArgNo)->getType()->getPointerElementType();
1742 }
1743
1744 /// Extract the preallocated type for a call or parameter.
1745 Type *getParamPreallocatedType(unsigned ArgNo) const {
1746 Type *Ty = Attrs.getParamPreallocatedType(ArgNo);
1747 return Ty ? Ty : getArgOperand(ArgNo)->getType()->getPointerElementType();
1748 }
1749
1750 /// Extract the number of dereferenceable bytes for a call or
1751 /// parameter (0=unknown).
1752 uint64_t getDereferenceableBytes(unsigned i) const {
1753 return Attrs.getDereferenceableBytes(i);
1754 }
1755
1756 /// Extract the number of dereferenceable_or_null bytes for a call or
1757 /// parameter (0=unknown).
1758 uint64_t getDereferenceableOrNullBytes(unsigned i) const {
1759 return Attrs.getDereferenceableOrNullBytes(i);
1760 }
1761
1762 /// Return true if the return value is known to be not null.
1763 /// This may be because it has the nonnull attribute, or because at least
1764 /// one byte is dereferenceable and the pointer is in addrspace(0).
1765 bool isReturnNonNull() const;
1766
1767 /// Determine if the return value is marked with NoAlias attribute.
1768 bool returnDoesNotAlias() const {
1769 return Attrs.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
1770 }
1771
1772 /// If one of the arguments has the 'returned' attribute, returns its
1773 /// operand value. Otherwise, return nullptr.
1774 Value *getReturnedArgOperand() const;
1775
1776 /// Return true if the call should not be treated as a call to a
1777 /// builtin.
1778 bool isNoBuiltin() const {
1779 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1780 !hasFnAttrImpl(Attribute::Builtin);
1781 }
1782
1783 /// Determine if the call requires strict floating point semantics.
1784 bool isStrictFP() const { return hasFnAttr(Attribute::StrictFP); }
1785
1786 /// Return true if the call should not be inlined.
1787 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1788 void setIsNoInline() {
1789 addAttribute(AttributeList::FunctionIndex, Attribute::NoInline);
1790 }
1791 /// Determine if the call does not access memory.
1792 bool doesNotAccessMemory() const { return hasFnAttr(Attribute::ReadNone); }
1793 void setDoesNotAccessMemory() {
1794 addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone);
1795 }
1796
1797 /// Determine if the call does not access or only reads memory.
1798 bool onlyReadsMemory() const {
1799 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1800 }
1801
1802 void setOnlyReadsMemory() {
1803 addAttribute(AttributeList::FunctionIndex, Attribute::ReadOnly);
1804 }
1805
1806 /// Determine if the call does not access or only writes memory.
1807 bool doesNotReadMemory() const {
1808 return doesNotAccessMemory() || hasFnAttr(Attribute::WriteOnly);
1809 }
1810 void setDoesNotReadMemory() {
1811 addAttribute(AttributeList::FunctionIndex, Attribute::WriteOnly);
1812 }
1813
1814 /// Determine if the call can access memmory only using pointers based
1815 /// on its arguments.
1816 bool onlyAccessesArgMemory() const {
1817 return hasFnAttr(Attribute::ArgMemOnly);
1818 }
1819 void setOnlyAccessesArgMemory() {
1820 addAttribute(AttributeList::FunctionIndex, Attribute::ArgMemOnly);
1821 }
1822
1823 /// Determine if the function may only access memory that is
1824 /// inaccessible from the IR.
1825 bool onlyAccessesInaccessibleMemory() const {
1826 return hasFnAttr(Attribute::InaccessibleMemOnly);
1827 }
1828 void setOnlyAccessesInaccessibleMemory() {
1829 addAttribute(AttributeList::FunctionIndex, Attribute::InaccessibleMemOnly);
1830 }
1831
1832 /// Determine if the function may only access memory that is
1833 /// either inaccessible from the IR or pointed to by its arguments.
1834 bool onlyAccessesInaccessibleMemOrArgMem() const {
1835 return hasFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
1836 }
1837 void setOnlyAccessesInaccessibleMemOrArgMem() {
1838 addAttribute(AttributeList::FunctionIndex,
1839 Attribute::InaccessibleMemOrArgMemOnly);
1840 }
1841 /// Determine if the call cannot return.
1842 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1843 void setDoesNotReturn() {
1844 addAttribute(AttributeList::FunctionIndex, Attribute::NoReturn);
1845 }
1846
1847 /// Determine if the call should not perform indirect branch tracking.
1848 bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck); }
1849
1850 /// Determine if the call cannot unwind.
1851 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1852 void setDoesNotThrow() {
1853 addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
1854 }
1855
1856 /// Determine if the invoke cannot be duplicated.
1857 bool cannotDuplicate() const { return hasFnAttr(Attribute::NoDuplicate); }
1858 void setCannotDuplicate() {
1859 addAttribute(AttributeList::FunctionIndex, Attribute::NoDuplicate);
1860 }
1861
1862 /// Determine if the call cannot be tail merged.
1863 bool cannotMerge() const { return hasFnAttr(Attribute::NoMerge); }
1864 void setCannotMerge() {
1865 addAttribute(AttributeList::FunctionIndex, Attribute::NoMerge);
1866 }
1867
1868 /// Determine if the invoke is convergent
1869 bool isConvergent() const { return hasFnAttr(Attribute::Convergent); }
22
Calling 'CallBase::hasFnAttr'
35
Returning from 'CallBase::hasFnAttr'
36
Returning value, which participates in a condition later
1870 void setConvergent() {
1871 addAttribute(AttributeList::FunctionIndex, Attribute::Convergent);
1872 }
1873 void setNotConvergent() {
1874 removeAttribute(AttributeList::FunctionIndex, Attribute::Convergent);
1875 }
1876
1877 /// Determine if the call returns a structure through first
1878 /// pointer argument.
1879 bool hasStructRetAttr() const {
1880 if (getNumArgOperands() == 0)
1881 return false;
1882
1883 // Be friendly and also check the callee.
1884 return paramHasAttr(0, Attribute::StructRet);
1885 }
1886
1887 /// Determine if any call argument is an aggregate passed by value.
1888 bool hasByValArgument() const {
1889 return Attrs.hasAttrSomewhere(Attribute::ByVal);
1890 }
1891
1892 ///@{
1893 // End of attribute API.
1894
1895 /// \name Operand Bundle API
1896 ///
1897 /// This group of methods provides the API to access and manipulate operand
1898 /// bundles on this call.
1899 /// @{
1900
1901 /// Return the number of operand bundles associated with this User.
1902 unsigned getNumOperandBundles() const {
1903 return std::distance(bundle_op_info_begin(), bundle_op_info_end());
1904 }
1905
1906 /// Return true if this User has any operand bundles.
1907 bool hasOperandBundles() const { return getNumOperandBundles() != 0; }
1908
1909 /// Return the index of the first bundle operand in the Use array.
1910 unsigned getBundleOperandsStartIndex() const {
1911 assert(hasOperandBundles() && "Don't call otherwise!")(static_cast <bool> (hasOperandBundles() && "Don't call otherwise!"
) ? void (0) : __assert_fail ("hasOperandBundles() && \"Don't call otherwise!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1911, __extension__ __PRETTY_FUNCTION__))
;
1912 return bundle_op_info_begin()->Begin;
1913 }
1914
1915 /// Return the index of the last bundle operand in the Use array.
1916 unsigned getBundleOperandsEndIndex() const {
1917 assert(hasOperandBundles() && "Don't call otherwise!")(static_cast <bool> (hasOperandBundles() && "Don't call otherwise!"
) ? void (0) : __assert_fail ("hasOperandBundles() && \"Don't call otherwise!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1917, __extension__ __PRETTY_FUNCTION__))
;
1918 return bundle_op_info_end()[-1].End;
1919 }
1920
1921 /// Return true if the operand at index \p Idx is a bundle operand.
1922 bool isBundleOperand(unsigned Idx) const {
1923 return hasOperandBundles() && Idx >= getBundleOperandsStartIndex() &&
1924 Idx < getBundleOperandsEndIndex();
1925 }
1926
1927 /// Returns true if the use is a bundle operand.
1928 bool isBundleOperand(const Use *U) const {
1929 assert(this == U->getUser() &&(static_cast <bool> (this == U->getUser() &&
"Only valid to query with a use of this instruction!") ? void
(0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1930, __extension__ __PRETTY_FUNCTION__))
1930 "Only valid to query with a use of this instruction!")(static_cast <bool> (this == U->getUser() &&
"Only valid to query with a use of this instruction!") ? void
(0) : __assert_fail ("this == U->getUser() && \"Only valid to query with a use of this instruction!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1930, __extension__ __PRETTY_FUNCTION__))
;
1931 return hasOperandBundles() && isBundleOperand(U - op_begin());
1932 }
1933 bool isBundleOperand(Value::const_user_iterator UI) const {
1934 return isBundleOperand(&UI.getUse());
1935 }
1936
1937 /// Return the total number operands (not operand bundles) used by
1938 /// every operand bundle in this OperandBundleUser.
1939 unsigned getNumTotalBundleOperands() const {
1940 if (!hasOperandBundles())
1941 return 0;
1942
1943 unsigned Begin = getBundleOperandsStartIndex();
1944 unsigned End = getBundleOperandsEndIndex();
1945
1946 assert(Begin <= End && "Should be!")(static_cast <bool> (Begin <= End && "Should be!"
) ? void (0) : __assert_fail ("Begin <= End && \"Should be!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1946, __extension__ __PRETTY_FUNCTION__))
;
1947 return End - Begin;
1948 }
1949
1950 /// Return the operand bundle at a specific index.
1951 OperandBundleUse getOperandBundleAt(unsigned Index) const {
1952 assert(Index < getNumOperandBundles() && "Index out of bounds!")(static_cast <bool> (Index < getNumOperandBundles() &&
"Index out of bounds!") ? void (0) : __assert_fail ("Index < getNumOperandBundles() && \"Index out of bounds!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1952, __extension__ __PRETTY_FUNCTION__))
;
1953 return operandBundleFromBundleOpInfo(*(bundle_op_info_begin() + Index));
1954 }
1955
1956 /// Return the number of operand bundles with the tag Name attached to
1957 /// this instruction.
1958 unsigned countOperandBundlesOfType(StringRef Name) const {
1959 unsigned Count = 0;
1960 for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i)
1961 if (getOperandBundleAt(i).getTagName() == Name)
1962 Count++;
1963
1964 return Count;
1965 }
1966
1967 /// Return the number of operand bundles with the tag ID attached to
1968 /// this instruction.
1969 unsigned countOperandBundlesOfType(uint32_t ID) const {
1970 unsigned Count = 0;
1971 for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i)
1972 if (getOperandBundleAt(i).getTagID() == ID)
1973 Count++;
1974
1975 return Count;
1976 }
1977
1978 /// Return an operand bundle by name, if present.
1979 ///
1980 /// It is an error to call this for operand bundle types that may have
1981 /// multiple instances of them on the same instruction.
1982 Optional<OperandBundleUse> getOperandBundle(StringRef Name) const {
1983 assert(countOperandBundlesOfType(Name) < 2 && "Precondition violated!")(static_cast <bool> (countOperandBundlesOfType(Name) <
2 && "Precondition violated!") ? void (0) : __assert_fail
("countOperandBundlesOfType(Name) < 2 && \"Precondition violated!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1983, __extension__ __PRETTY_FUNCTION__))
;
1984
1985 for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) {
1986 OperandBundleUse U = getOperandBundleAt(i);
1987 if (U.getTagName() == Name)
1988 return U;
1989 }
1990
1991 return None;
1992 }
1993
1994 /// Return an operand bundle by tag ID, if present.
1995 ///
1996 /// It is an error to call this for operand bundle types that may have
1997 /// multiple instances of them on the same instruction.
1998 Optional<OperandBundleUse> getOperandBundle(uint32_t ID) const {
1999 assert(countOperandBundlesOfType(ID) < 2 && "Precondition violated!")(static_cast <bool> (countOperandBundlesOfType(ID) <
2 && "Precondition violated!") ? void (0) : __assert_fail
("countOperandBundlesOfType(ID) < 2 && \"Precondition violated!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 1999, __extension__ __PRETTY_FUNCTION__))
;
2000
2001 for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) {
2002 OperandBundleUse U = getOperandBundleAt(i);
2003 if (U.getTagID() == ID)
2004 return U;
2005 }
2006
2007 return None;
2008 }
2009
2010 /// Return the list of operand bundles attached to this instruction as
2011 /// a vector of OperandBundleDefs.
2012 ///
2013 /// This function copies the OperandBundeUse instances associated with this
2014 /// OperandBundleUser to a vector of OperandBundleDefs. Note:
2015 /// OperandBundeUses and OperandBundleDefs are non-trivially *different*
2016 /// representations of operand bundles (see documentation above).
2017 void getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> &Defs) const;
2018
2019 /// Return the operand bundle for the operand at index OpIdx.
2020 ///
2021 /// It is an error to call this with an OpIdx that does not correspond to an
2022 /// bundle operand.
2023 OperandBundleUse getOperandBundleForOperand(unsigned OpIdx) const {
2024 return operandBundleFromBundleOpInfo(getBundleOpInfoForOperand(OpIdx));
2025 }
2026
2027 /// Return true if this operand bundle user has operand bundles that
2028 /// may read from the heap.
2029 bool hasReadingOperandBundles() const;
2030
2031 /// Return true if this operand bundle user has operand bundles that
2032 /// may write to the heap.
2033 bool hasClobberingOperandBundles() const {
2034 for (auto &BOI : bundle_op_infos()) {
2035 if (BOI.Tag->second == LLVMContext::OB_deopt ||
2036 BOI.Tag->second == LLVMContext::OB_funclet)
2037 continue;
2038
2039 // This instruction has an operand bundle that is not known to us.
2040 // Assume the worst.
2041 return true;
2042 }
2043
2044 return false;
2045 }
2046
2047 /// Return true if the bundle operand at index \p OpIdx has the
2048 /// attribute \p A.
2049 bool bundleOperandHasAttr(unsigned OpIdx, Attribute::AttrKind A) const {
2050 auto &BOI = getBundleOpInfoForOperand(OpIdx);
2051 auto OBU = operandBundleFromBundleOpInfo(BOI);
2052 return OBU.operandHasAttr(OpIdx - BOI.Begin, A);
2053 }
2054
2055 /// Return true if \p Other has the same sequence of operand bundle
2056 /// tags with the same number of operands on each one of them as this
2057 /// OperandBundleUser.
2058 bool hasIdenticalOperandBundleSchema(const CallBase &Other) const {
2059 if (getNumOperandBundles() != Other.getNumOperandBundles())
2060 return false;
2061
2062 return std::equal(bundle_op_info_begin(), bundle_op_info_end(),
2063 Other.bundle_op_info_begin());
2064 }
2065
2066 /// Return true if this operand bundle user contains operand bundles
2067 /// with tags other than those specified in \p IDs.
2068 bool hasOperandBundlesOtherThan(ArrayRef<uint32_t> IDs) const {
2069 for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) {
2070 uint32_t ID = getOperandBundleAt(i).getTagID();
2071 if (!is_contained(IDs, ID))
2072 return true;
2073 }
2074 return false;
2075 }
2076
2077 /// Is the function attribute S disallowed by some operand bundle on
2078 /// this operand bundle user?
2079 bool isFnAttrDisallowedByOpBundle(StringRef S) const {
2080 // Operand bundles only possibly disallow readnone, readonly and argmemonly
2081 // attributes. All String attributes are fine.
2082 return false;
2083 }
2084
2085 /// Is the function attribute A disallowed by some operand bundle on
2086 /// this operand bundle user?
2087 bool isFnAttrDisallowedByOpBundle(Attribute::AttrKind A) const {
2088 switch (A) {
28
Control jumps to the 'default' case at line 2089
2089 default:
2090 return false;
29
Returning zero, which participates in a condition later
2091
2092 case Attribute::InaccessibleMemOrArgMemOnly:
2093 return hasReadingOperandBundles();
2094
2095 case Attribute::InaccessibleMemOnly:
2096 return hasReadingOperandBundles();
2097
2098 case Attribute::ArgMemOnly:
2099 return hasReadingOperandBundles();
2100
2101 case Attribute::ReadNone:
2102 return hasReadingOperandBundles();
2103
2104 case Attribute::ReadOnly:
2105 return hasClobberingOperandBundles();
2106 }
2107
2108 llvm_unreachable("switch has a default case!")::llvm::llvm_unreachable_internal("switch has a default case!"
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 2108)
;
2109 }
2110
2111 /// Used to keep track of an operand bundle. See the main comment on
2112 /// OperandBundleUser above.
2113 struct BundleOpInfo {
2114 /// The operand bundle tag, interned by
2115 /// LLVMContextImpl::getOrInsertBundleTag.
2116 StringMapEntry<uint32_t> *Tag;
2117
2118 /// The index in the Use& vector where operands for this operand
2119 /// bundle starts.
2120 uint32_t Begin;
2121
2122 /// The index in the Use& vector where operands for this operand
2123 /// bundle ends.
2124 uint32_t End;
2125
2126 bool operator==(const BundleOpInfo &Other) const {
2127 return Tag == Other.Tag && Begin == Other.Begin && End == Other.End;
2128 }
2129 };
2130
2131 /// Simple helper function to map a BundleOpInfo to an
2132 /// OperandBundleUse.
2133 OperandBundleUse
2134 operandBundleFromBundleOpInfo(const BundleOpInfo &BOI) const {
2135 auto begin = op_begin();
2136 ArrayRef<Use> Inputs(begin + BOI.Begin, begin + BOI.End);
2137 return OperandBundleUse(BOI.Tag, Inputs);
2138 }
2139
2140 using bundle_op_iterator = BundleOpInfo *;
2141 using const_bundle_op_iterator = const BundleOpInfo *;
2142
2143 /// Return the start of the list of BundleOpInfo instances associated
2144 /// with this OperandBundleUser.
2145 ///
2146 /// OperandBundleUser uses the descriptor area co-allocated with the host User
2147 /// to store some meta information about which operands are "normal" operands,
2148 /// and which ones belong to some operand bundle.
2149 ///
2150 /// The layout of an operand bundle user is
2151 ///
2152 /// +-----------uint32_t End-------------------------------------+
2153 /// | |
2154 /// | +--------uint32_t Begin--------------------+ |
2155 /// | | | |
2156 /// ^ ^ v v
2157 /// |------|------|----|----|----|----|----|---------|----|---------|----|-----
2158 /// | BOI0 | BOI1 | .. | DU | U0 | U1 | .. | BOI0_U0 | .. | BOI1_U0 | .. | Un
2159 /// |------|------|----|----|----|----|----|---------|----|---------|----|-----
2160 /// v v ^ ^
2161 /// | | | |
2162 /// | +--------uint32_t Begin------------+ |
2163 /// | |
2164 /// +-----------uint32_t End-----------------------------+
2165 ///
2166 ///
2167 /// BOI0, BOI1 ... are descriptions of operand bundles in this User's use
2168 /// list. These descriptions are installed and managed by this class, and
2169 /// they're all instances of OperandBundleUser<T>::BundleOpInfo.
2170 ///
2171 /// DU is an additional descriptor installed by User's 'operator new' to keep
2172 /// track of the 'BOI0 ... BOIN' co-allocation. OperandBundleUser does not
2173 /// access or modify DU in any way, it's an implementation detail private to
2174 /// User.
2175 ///
2176 /// The regular Use& vector for the User starts at U0. The operand bundle
2177 /// uses are part of the Use& vector, just like normal uses. In the diagram
2178 /// above, the operand bundle uses start at BOI0_U0. Each instance of
2179 /// BundleOpInfo has information about a contiguous set of uses constituting
2180 /// an operand bundle, and the total set of operand bundle uses themselves
2181 /// form a contiguous set of uses (i.e. there are no gaps between uses
2182 /// corresponding to individual operand bundles).
2183 ///
2184 /// This class does not know the location of the set of operand bundle uses
2185 /// within the use list -- that is decided by the User using this class via
2186 /// the BeginIdx argument in populateBundleOperandInfos.
2187 ///
2188 /// Currently operand bundle users with hung-off operands are not supported.
2189 bundle_op_iterator bundle_op_info_begin() {
2190 if (!hasDescriptor())
2191 return nullptr;
2192
2193 uint8_t *BytesBegin = getDescriptor().begin();
2194 return reinterpret_cast<bundle_op_iterator>(BytesBegin);
2195 }
2196
2197 /// Return the start of the list of BundleOpInfo instances associated
2198 /// with this OperandBundleUser.
2199 const_bundle_op_iterator bundle_op_info_begin() const {
2200 auto *NonConstThis = const_cast<CallBase *>(this);
2201 return NonConstThis->bundle_op_info_begin();
2202 }
2203
2204 /// Return the end of the list of BundleOpInfo instances associated
2205 /// with this OperandBundleUser.
2206 bundle_op_iterator bundle_op_info_end() {
2207 if (!hasDescriptor())
2208 return nullptr;
2209
2210 uint8_t *BytesEnd = getDescriptor().end();
2211 return reinterpret_cast<bundle_op_iterator>(BytesEnd);
2212 }
2213
2214 /// Return the end of the list of BundleOpInfo instances associated
2215 /// with this OperandBundleUser.
2216 const_bundle_op_iterator bundle_op_info_end() const {
2217 auto *NonConstThis = const_cast<CallBase *>(this);
2218 return NonConstThis->bundle_op_info_end();
2219 }
2220
2221 /// Return the range [\p bundle_op_info_begin, \p bundle_op_info_end).
2222 iterator_range<bundle_op_iterator> bundle_op_infos() {
2223 return make_range(bundle_op_info_begin(), bundle_op_info_end());
2224 }
2225
2226 /// Return the range [\p bundle_op_info_begin, \p bundle_op_info_end).
2227 iterator_range<const_bundle_op_iterator> bundle_op_infos() const {
2228 return make_range(bundle_op_info_begin(), bundle_op_info_end());
2229 }
2230
2231 /// Populate the BundleOpInfo instances and the Use& vector from \p
2232 /// Bundles. Return the op_iterator pointing to the Use& one past the last
2233 /// last bundle operand use.
2234 ///
2235 /// Each \p OperandBundleDef instance is tracked by a OperandBundleInfo
2236 /// instance allocated in this User's descriptor.
2237 op_iterator populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles,
2238 const unsigned BeginIndex);
2239
2240public:
2241 /// Return the BundleOpInfo for the operand at index OpIdx.
2242 ///
2243 /// It is an error to call this with an OpIdx that does not correspond to an
2244 /// bundle operand.
2245 BundleOpInfo &getBundleOpInfoForOperand(unsigned OpIdx);
2246 const BundleOpInfo &getBundleOpInfoForOperand(unsigned OpIdx) const {
2247 return const_cast<CallBase *>(this)->getBundleOpInfoForOperand(OpIdx);
2248 }
2249
2250protected:
2251 /// Return the total number of values used in \p Bundles.
2252 static unsigned CountBundleInputs(ArrayRef<OperandBundleDef> Bundles) {
2253 unsigned Total = 0;
2254 for (auto &B : Bundles)
2255 Total += B.input_size();
2256 return Total;
2257 }
2258
2259 /// @}
2260 // End of operand bundle API.
2261
2262private:
2263 bool hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const;
2264 bool hasFnAttrOnCalledFunction(StringRef Kind) const;
2265
2266 template <typename AttrKind> bool hasFnAttrImpl(AttrKind Kind) const {
2267 if (Attrs.hasFnAttribute(Kind))
25
Assuming the condition is false
26
Taking false branch
2268 return true;
2269
2270 // Operand bundles override attributes on the called function, but don't
2271 // override attributes directly present on the call instruction.
2272 if (isFnAttrDisallowedByOpBundle(Kind))
27
Calling 'CallBase::isFnAttrDisallowedByOpBundle'
30
Returning from 'CallBase::isFnAttrDisallowedByOpBundle'
31
Taking false branch
2273 return false;
2274
2275 return hasFnAttrOnCalledFunction(Kind);
32
Returning value, which participates in a condition later
2276 }
2277
2278 /// Determine whether the return value has the given attribute. Supports
2279 /// Attribute::AttrKind and StringRef as \p AttrKind types.
2280 template <typename AttrKind> bool hasRetAttrImpl(AttrKind Kind) const {
2281 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
2282 return true;
2283
2284 // Look at the callee, if available.
2285 if (const Function *F = getCalledFunction())
2286 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
2287 return false;
2288 }
2289};
2290
2291template <>
2292struct OperandTraits<CallBase> : public VariadicOperandTraits<CallBase, 1> {};
2293
2294DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallBase, Value)CallBase::op_iterator CallBase::op_begin() { return OperandTraits
<CallBase>::op_begin(this); } CallBase::const_op_iterator
CallBase::op_begin() const { return OperandTraits<CallBase
>::op_begin(const_cast<CallBase*>(this)); } CallBase
::op_iterator CallBase::op_end() { return OperandTraits<CallBase
>::op_end(this); } CallBase::const_op_iterator CallBase::op_end
() const { return OperandTraits<CallBase>::op_end(const_cast
<CallBase*>(this)); } Value *CallBase::getOperand(unsigned
i_nocapture) const { (static_cast <bool> (i_nocapture <
OperandTraits<CallBase>::operands(this) && "getOperand() out of range!"
) ? void (0) : __assert_fail ("i_nocapture < OperandTraits<CallBase>::operands(this) && \"getOperand() out of range!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 2294, __extension__ __PRETTY_FUNCTION__)); return cast_or_null
<Value>( OperandTraits<CallBase>::op_begin(const_cast
<CallBase*>(this))[i_nocapture].get()); } void CallBase
::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (static_cast
<bool> (i_nocapture < OperandTraits<CallBase>
::operands(this) && "setOperand() out of range!") ? void
(0) : __assert_fail ("i_nocapture < OperandTraits<CallBase>::operands(this) && \"setOperand() out of range!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 2294, __extension__ __PRETTY_FUNCTION__)); OperandTraits<
CallBase>::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned
CallBase::getNumOperands() const { return OperandTraits<CallBase
>::operands(this); } template <int Idx_nocapture> Use
&CallBase::Op() { return this->OpFrom<Idx_nocapture
>(this); } template <int Idx_nocapture> const Use &
CallBase::Op() const { return this->OpFrom<Idx_nocapture
>(this); }
2295
2296//===----------------------------------------------------------------------===//
2297// FuncletPadInst Class
2298//===----------------------------------------------------------------------===//
2299class FuncletPadInst : public Instruction {
2300private:
2301 FuncletPadInst(const FuncletPadInst &CPI);
2302
2303 explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
2304 ArrayRef<Value *> Args, unsigned Values,
2305 const Twine &NameStr, Instruction *InsertBefore);
2306 explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
2307 ArrayRef<Value *> Args, unsigned Values,
2308 const Twine &NameStr, BasicBlock *InsertAtEnd);
2309
2310 void init(Value *ParentPad, ArrayRef<Value *> Args, const Twine &NameStr);
2311
2312protected:
2313 // Note: Instruction needs to be a friend here to call cloneImpl.
2314 friend class Instruction;
2315 friend class CatchPadInst;
2316 friend class CleanupPadInst;
2317
2318 FuncletPadInst *cloneImpl() const;
2319
2320public:
2321 /// Provide fast operand accessors
2322 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void
setOperand(unsigned, Value*); inline op_iterator op_begin();
inline const_op_iterator op_begin() const; inline op_iterator
op_end(); inline const_op_iterator op_end() const; protected
: template <int> inline Use &Op(); template <int
> inline const Use &Op() const; public: inline unsigned
getNumOperands() const
;
2323
2324 /// getNumArgOperands - Return the number of funcletpad arguments.
2325 ///
2326 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
2327
2328 /// Convenience accessors
2329
2330 /// Return the outer EH-pad this funclet is nested within.
2331 ///
2332 /// Note: This returns the associated CatchSwitchInst if this FuncletPadInst
2333 /// is a CatchPadInst.
2334 Value *getParentPad() const { return Op<-1>(); }
2335 void setParentPad(Value *ParentPad) {
2336 assert(ParentPad)(static_cast <bool> (ParentPad) ? void (0) : __assert_fail
("ParentPad", "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 2336, __extension__ __PRETTY_FUNCTION__))
;
2337 Op<-1>() = ParentPad;
2338 }
2339
2340 /// getArgOperand/setArgOperand - Return/set the i-th funcletpad argument.
2341 ///
2342 Value *getArgOperand(unsigned i) const { return getOperand(i); }
2343 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
2344
2345 /// arg_operands - iteration adapter for range-for loops.
2346 op_range arg_operands() { return op_range(op_begin(), op_end() - 1); }
2347
2348 /// arg_operands - iteration adapter for range-for loops.
2349 const_op_range arg_operands() const {
2350 return const_op_range(op_begin(), op_end() - 1);
2351 }
2352
2353 // Methods for support type inquiry through isa, cast, and dyn_cast:
2354 static bool classof(const Instruction *I) { return I->isFuncletPad(); }
2355 static bool classof(const Value *V) {
2356 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2357 }
2358};
2359
2360template <>
2361struct OperandTraits<FuncletPadInst>
2362 : public VariadicOperandTraits<FuncletPadInst, /*MINARITY=*/1> {};
2363
2364DEFINE_TRANSPARENT_OPERAND_ACCESSORS(FuncletPadInst, Value)FuncletPadInst::op_iterator FuncletPadInst::op_begin() { return
OperandTraits<FuncletPadInst>::op_begin(this); } FuncletPadInst
::const_op_iterator FuncletPadInst::op_begin() const { return
OperandTraits<FuncletPadInst>::op_begin(const_cast<
FuncletPadInst*>(this)); } FuncletPadInst::op_iterator FuncletPadInst
::op_end() { return OperandTraits<FuncletPadInst>::op_end
(this); } FuncletPadInst::const_op_iterator FuncletPadInst::op_end
() const { return OperandTraits<FuncletPadInst>::op_end
(const_cast<FuncletPadInst*>(this)); } Value *FuncletPadInst
::getOperand(unsigned i_nocapture) const { (static_cast <bool
> (i_nocapture < OperandTraits<FuncletPadInst>::operands
(this) && "getOperand() out of range!") ? void (0) : __assert_fail
("i_nocapture < OperandTraits<FuncletPadInst>::operands(this) && \"getOperand() out of range!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 2364, __extension__ __PRETTY_FUNCTION__)); return cast_or_null
<Value>( OperandTraits<FuncletPadInst>::op_begin(
const_cast<FuncletPadInst*>(this))[i_nocapture].get());
} void FuncletPadInst::setOperand(unsigned i_nocapture, Value
*Val_nocapture) { (static_cast <bool> (i_nocapture <
OperandTraits<FuncletPadInst>::operands(this) &&
"setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<FuncletPadInst>::operands(this) && \"setOperand() out of range!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/InstrTypes.h"
, 2364, __extension__ __PRETTY_FUNCTION__)); OperandTraits<
FuncletPadInst>::op_begin(this)[i_nocapture] = Val_nocapture
; } unsigned FuncletPadInst::getNumOperands() const { return OperandTraits
<FuncletPadInst>::operands(this); } template <int Idx_nocapture
> Use &FuncletPadInst::Op() { return this->OpFrom<
Idx_nocapture>(this); } template <int Idx_nocapture>
const Use &FuncletPadInst::Op() const { return this->
OpFrom<Idx_nocapture>(this); }
2365
2366} // end namespace llvm
2367
2368#endif // LLVM_IR_INSTRTYPES_H

/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/PatternMatch.h

1//===- PatternMatch.h - Match on the LLVM IR --------------------*- C++ -*-===//
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 file provides a simple and efficient mechanism for performing general
10// tree-based pattern matches on the LLVM IR. The power of these routines is
11// that it allows you to write concise patterns that are expressive and easy to
12// understand. The other major advantage of this is that it allows you to
13// trivially capture/bind elements in the pattern to variables. For example,
14// you can do something like this:
15//
16// Value *Exp = ...
17// Value *X, *Y; ConstantInt *C1, *C2; // (X & C1) | (Y & C2)
18// if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)),
19// m_And(m_Value(Y), m_ConstantInt(C2))))) {
20// ... Pattern is matched and variables are bound ...
21// }
22//
23// This is primarily useful to things like the instruction combiner, but can
24// also be useful for static analysis tools or code generators.
25//
26//===----------------------------------------------------------------------===//
27
28#ifndef LLVM_IR_PATTERNMATCH_H
29#define LLVM_IR_PATTERNMATCH_H
30
31#include "llvm/ADT/APFloat.h"
32#include "llvm/ADT/APInt.h"
33#include "llvm/IR/Constant.h"
34#include "llvm/IR/Constants.h"
35#include "llvm/IR/DataLayout.h"
36#include "llvm/IR/InstrTypes.h"
37#include "llvm/IR/Instruction.h"
38#include "llvm/IR/Instructions.h"
39#include "llvm/IR/IntrinsicInst.h"
40#include "llvm/IR/Intrinsics.h"
41#include "llvm/IR/Operator.h"
42#include "llvm/IR/Value.h"
43#include "llvm/Support/Casting.h"
44#include <cstdint>
45
46namespace llvm {
47namespace PatternMatch {
48
49template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
50 return const_cast<Pattern &>(P).match(V);
41
Calling 'IntrinsicID_match::match'
45
Returning from 'IntrinsicID_match::match'
46
Returning zero, which participates in a condition later
50
Calling 'IntrinsicID_match::match'
54
Returning from 'IntrinsicID_match::match'
55
Returning zero, which participates in a condition later
51}
52
53template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) {
54 return const_cast<Pattern &>(P).match(Mask);
55}
56
57template <typename SubPattern_t> struct OneUse_match {
58 SubPattern_t SubPattern;
59
60 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
61
62 template <typename OpTy> bool match(OpTy *V) {
63 return V->hasOneUse() && SubPattern.match(V);
64 }
65};
66
67template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
68 return SubPattern;
69}
70
71template <typename Class> struct class_match {
72 template <typename ITy> bool match(ITy *V) { return isa<Class>(V); }
73};
74
75/// Match an arbitrary value and ignore it.
76inline class_match<Value> m_Value() { return class_match<Value>(); }
77
78/// Match an arbitrary unary operation and ignore it.
79inline class_match<UnaryOperator> m_UnOp() {
80 return class_match<UnaryOperator>();
81}
82
83/// Match an arbitrary binary operation and ignore it.
84inline class_match<BinaryOperator> m_BinOp() {
85 return class_match<BinaryOperator>();
86}
87
88/// Matches any compare instruction and ignore it.
89inline class_match<CmpInst> m_Cmp() { return class_match<CmpInst>(); }
90
91struct undef_match {
92 static bool check(const Value *V) {
93 if (isa<UndefValue>(V))
94 return true;
95
96 const auto *CA = dyn_cast<ConstantAggregate>(V);
97 if (!CA)
98 return false;
99
100 SmallPtrSet<const ConstantAggregate *, 8> Seen;
101 SmallVector<const ConstantAggregate *, 8> Worklist;
102
103 // Either UndefValue, PoisonValue, or an aggregate that only contains
104 // these is accepted by matcher.
105 // CheckValue returns false if CA cannot satisfy this constraint.
106 auto CheckValue = [&](const ConstantAggregate *CA) {
107 for (const Value *Op : CA->operand_values()) {
108 if (isa<UndefValue>(Op))
109 continue;
110
111 const auto *CA = dyn_cast<ConstantAggregate>(Op);
112 if (!CA)
113 return false;
114 if (Seen.insert(CA).second)
115 Worklist.emplace_back(CA);
116 }
117
118 return true;
119 };
120
121 if (!CheckValue(CA))
122 return false;
123
124 while (!Worklist.empty()) {
125 if (!CheckValue(Worklist.pop_back_val()))
126 return false;
127 }
128 return true;
129 }
130 template <typename ITy> bool match(ITy *V) { return check(V); }
131};
132
133/// Match an arbitrary undef constant. This matches poison as well.
134/// If this is an aggregate and contains a non-aggregate element that is
135/// neither undef nor poison, the aggregate is not matched.
136inline auto m_Undef() { return undef_match(); }
137
138/// Match an arbitrary poison constant.
139inline class_match<PoisonValue> m_Poison() { return class_match<PoisonValue>(); }
140
141/// Match an arbitrary Constant and ignore it.
142inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
143
144/// Match an arbitrary ConstantInt and ignore it.
145inline class_match<ConstantInt> m_ConstantInt() {
146 return class_match<ConstantInt>();
147}
148
149/// Match an arbitrary ConstantFP and ignore it.
150inline class_match<ConstantFP> m_ConstantFP() {
151 return class_match<ConstantFP>();
152}
153
154/// Match an arbitrary ConstantExpr and ignore it.
155inline class_match<ConstantExpr> m_ConstantExpr() {
156 return class_match<ConstantExpr>();
157}
158
159/// Match an arbitrary basic block value and ignore it.
160inline class_match<BasicBlock> m_BasicBlock() {
161 return class_match<BasicBlock>();
162}
163
164/// Inverting matcher
165template <typename Ty> struct match_unless {
166 Ty M;
167
168 match_unless(const Ty &Matcher) : M(Matcher) {}
169
170 template <typename ITy> bool match(ITy *V) { return !M.match(V); }
171};
172
173/// Match if the inner matcher does *NOT* match.
174template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) {
175 return match_unless<Ty>(M);
176}
177
178/// Matching combinators
179template <typename LTy, typename RTy> struct match_combine_or {
180 LTy L;
181 RTy R;
182
183 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
184
185 template <typename ITy> bool match(ITy *V) {
186 if (L.match(V))
187 return true;
188 if (R.match(V))
189 return true;
190 return false;
191 }
192};
193
194template <typename LTy, typename RTy> struct match_combine_and {
195 LTy L;
196 RTy R;
197
198 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
199
200 template <typename ITy> bool match(ITy *V) {
201 if (L.match(V))
202 if (R.match(V))
203 return true;
204 return false;
205 }
206};
207
208/// Combine two pattern matchers matching L || R
209template <typename LTy, typename RTy>
210inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
211 return match_combine_or<LTy, RTy>(L, R);
212}
213
214/// Combine two pattern matchers matching L && R
215template <typename LTy, typename RTy>
216inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
217 return match_combine_and<LTy, RTy>(L, R);
218}
219
220struct apint_match {
221 const APInt *&Res;
222 bool AllowUndef;
223
224 apint_match(const APInt *&Res, bool AllowUndef)
225 : Res(Res), AllowUndef(AllowUndef) {}
226
227 template <typename ITy> bool match(ITy *V) {
228 if (auto *CI = dyn_cast<ConstantInt>(V)) {
229 Res = &CI->getValue();
230 return true;
231 }
232 if (V->getType()->isVectorTy())
233 if (const auto *C = dyn_cast<Constant>(V))
234 if (auto *CI = dyn_cast_or_null<ConstantInt>(
235 C->getSplatValue(AllowUndef))) {
236 Res = &CI->getValue();
237 return true;
238 }
239 return false;
240 }
241};
242// Either constexpr if or renaming ConstantFP::getValueAPF to
243// ConstantFP::getValue is needed to do it via single template
244// function for both apint/apfloat.
245struct apfloat_match {
246 const APFloat *&Res;
247 bool AllowUndef;
248
249 apfloat_match(const APFloat *&Res, bool AllowUndef)
250 : Res(Res), AllowUndef(AllowUndef) {}
251
252 template <typename ITy> bool match(ITy *V) {
253 if (auto *CI = dyn_cast<ConstantFP>(V)) {
254 Res = &CI->getValueAPF();
255 return true;
256 }
257 if (V->getType()->isVectorTy())
258 if (const auto *C = dyn_cast<Constant>(V))
259 if (auto *CI = dyn_cast_or_null<ConstantFP>(
260 C->getSplatValue(AllowUndef))) {
261 Res = &CI->getValueAPF();
262 return true;
263 }
264 return false;
265 }
266};
267
268/// Match a ConstantInt or splatted ConstantVector, binding the
269/// specified pointer to the contained APInt.
270inline apint_match m_APInt(const APInt *&Res) {
271 // Forbid undefs by default to maintain previous behavior.
272 return apint_match(Res, /* AllowUndef */ false);
273}
274
275/// Match APInt while allowing undefs in splat vector constants.
276inline apint_match m_APIntAllowUndef(const APInt *&Res) {
277 return apint_match(Res, /* AllowUndef */ true);
278}
279
280/// Match APInt while forbidding undefs in splat vector constants.
281inline apint_match m_APIntForbidUndef(const APInt *&Res) {
282 return apint_match(Res, /* AllowUndef */ false);
283}
284
285/// Match a ConstantFP or splatted ConstantVector, binding the
286/// specified pointer to the contained APFloat.
287inline apfloat_match m_APFloat(const APFloat *&Res) {
288 // Forbid undefs by default to maintain previous behavior.
289 return apfloat_match(Res, /* AllowUndef */ false);
290}
291
292/// Match APFloat while allowing undefs in splat vector constants.
293inline apfloat_match m_APFloatAllowUndef(const APFloat *&Res) {
294 return apfloat_match(Res, /* AllowUndef */ true);
295}
296
297/// Match APFloat while forbidding undefs in splat vector constants.
298inline apfloat_match m_APFloatForbidUndef(const APFloat *&Res) {
299 return apfloat_match(Res, /* AllowUndef */ false);
300}
301
302template <int64_t Val> struct constantint_match {
303 template <typename ITy> bool match(ITy *V) {
304 if (const auto *CI = dyn_cast<ConstantInt>(V)) {
305 const APInt &CIV = CI->getValue();
306 if (Val >= 0)
307 return CIV == static_cast<uint64_t>(Val);
308 // If Val is negative, and CI is shorter than it, truncate to the right
309 // number of bits. If it is larger, then we have to sign extend. Just
310 // compare their negated values.
311 return -CIV == -Val;
312 }
313 return false;
314 }
315};
316
317/// Match a ConstantInt with a specific value.
318template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
319 return constantint_match<Val>();
320}
321
322/// This helper class is used to match constant scalars, vector splats,
323/// and fixed width vectors that satisfy a specified predicate.
324/// For fixed width vector constants, undefined elements are ignored.
325template <typename Predicate, typename ConstantVal>
326struct cstval_pred_ty : public Predicate {
327 template <typename ITy> bool match(ITy *V) {
328 if (const auto *CV = dyn_cast<ConstantVal>(V))
329 return this->isValue(CV->getValue());
330 if (const auto *VTy = dyn_cast<VectorType>(V->getType())) {
331 if (const auto *C = dyn_cast<Constant>(V)) {
332 if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue()))
333 return this->isValue(CV->getValue());
334
335 // Number of elements of a scalable vector unknown at compile time
336 auto *FVTy = dyn_cast<FixedVectorType>(VTy);
337 if (!FVTy)
338 return false;
339
340 // Non-splat vector constant: check each element for a match.
341 unsigned NumElts = FVTy->getNumElements();
342 assert(NumElts != 0 && "Constant vector with no elements?")(static_cast <bool> (NumElts != 0 && "Constant vector with no elements?"
) ? void (0) : __assert_fail ("NumElts != 0 && \"Constant vector with no elements?\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/PatternMatch.h"
, 342, __extension__ __PRETTY_FUNCTION__))
;
343 bool HasNonUndefElements = false;
344 for (unsigned i = 0; i != NumElts; ++i) {
345 Constant *Elt = C->getAggregateElement(i);
346 if (!Elt)
347 return false;
348 if (isa<UndefValue>(Elt))
349 continue;
350 auto *CV = dyn_cast<ConstantVal>(Elt);
351 if (!CV || !this->isValue(CV->getValue()))
352 return false;
353 HasNonUndefElements = true;
354 }
355 return HasNonUndefElements;
356 }
357 }
358 return false;
359 }
360};
361
362/// specialization of cstval_pred_ty for ConstantInt
363template <typename Predicate>
364using cst_pred_ty = cstval_pred_ty<Predicate, ConstantInt>;
365
366/// specialization of cstval_pred_ty for ConstantFP
367template <typename Predicate>
368using cstfp_pred_ty = cstval_pred_ty<Predicate, ConstantFP>;
369
370/// This helper class is used to match scalar and vector constants that
371/// satisfy a specified predicate, and bind them to an APInt.
372template <typename Predicate> struct api_pred_ty : public Predicate {
373 const APInt *&Res;
374
375 api_pred_ty(const APInt *&R) : Res(R) {}
376
377 template <typename ITy> bool match(ITy *V) {
378 if (const auto *CI = dyn_cast<ConstantInt>(V))
379 if (this->isValue(CI->getValue())) {
380 Res = &CI->getValue();
381 return true;
382 }
383 if (V->getType()->isVectorTy())
384 if (const auto *C = dyn_cast<Constant>(V))
385 if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
386 if (this->isValue(CI->getValue())) {
387 Res = &CI->getValue();
388 return true;
389 }
390
391 return false;
392 }
393};
394
395/// This helper class is used to match scalar and vector constants that
396/// satisfy a specified predicate, and bind them to an APFloat.
397/// Undefs are allowed in splat vector constants.
398template <typename Predicate> struct apf_pred_ty : public Predicate {
399 const APFloat *&Res;
400
401 apf_pred_ty(const APFloat *&R) : Res(R) {}
402
403 template <typename ITy> bool match(ITy *V) {
404 if (const auto *CI = dyn_cast<ConstantFP>(V))
405 if (this->isValue(CI->getValue())) {
406 Res = &CI->getValue();
407 return true;
408 }
409 if (V->getType()->isVectorTy())
410 if (const auto *C = dyn_cast<Constant>(V))
411 if (auto *CI = dyn_cast_or_null<ConstantFP>(
412 C->getSplatValue(/* AllowUndef */ true)))
413 if (this->isValue(CI->getValue())) {
414 Res = &CI->getValue();
415 return true;
416 }
417
418 return false;
419 }
420};
421
422///////////////////////////////////////////////////////////////////////////////
423//
424// Encapsulate constant value queries for use in templated predicate matchers.
425// This allows checking if constants match using compound predicates and works
426// with vector constants, possibly with relaxed constraints. For example, ignore
427// undef values.
428//
429///////////////////////////////////////////////////////////////////////////////
430
431struct is_any_apint {
432 bool isValue(const APInt &C) { return true; }
433};
434/// Match an integer or vector with any integral constant.
435/// For vectors, this includes constants with undefined elements.
436inline cst_pred_ty<is_any_apint> m_AnyIntegralConstant() {
437 return cst_pred_ty<is_any_apint>();
438}
439
440struct is_all_ones {
441 bool isValue(const APInt &C) { return C.isAllOnesValue(); }
442};
443/// Match an integer or vector with all bits set.
444/// For vectors, this includes constants with undefined elements.
445inline cst_pred_ty<is_all_ones> m_AllOnes() {
446 return cst_pred_ty<is_all_ones>();
447}
448
449struct is_maxsignedvalue {
450 bool isValue(const APInt &C) { return C.isMaxSignedValue(); }
451};
452/// Match an integer or vector with values having all bits except for the high
453/// bit set (0x7f...).
454/// For vectors, this includes constants with undefined elements.
455inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() {
456 return cst_pred_ty<is_maxsignedvalue>();
457}
458inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) {
459 return V;
460}
461
462struct is_negative {
463 bool isValue(const APInt &C) { return C.isNegative(); }
464};
465/// Match an integer or vector of negative values.
466/// For vectors, this includes constants with undefined elements.
467inline cst_pred_ty<is_negative> m_Negative() {
468 return cst_pred_ty<is_negative>();
469}
470inline api_pred_ty<is_negative> m_Negative(const APInt *&V) {
471 return V;
472}
473
474struct is_nonnegative {
475 bool isValue(const APInt &C) { return C.isNonNegative(); }
476};
477/// Match an integer or vector of non-negative values.
478/// For vectors, this includes constants with undefined elements.
479inline cst_pred_ty<is_nonnegative> m_NonNegative() {
480 return cst_pred_ty<is_nonnegative>();
481}
482inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) {
483 return V;
484}
485
486struct is_strictlypositive {
487 bool isValue(const APInt &C) { return C.isStrictlyPositive(); }
488};
489/// Match an integer or vector of strictly positive values.
490/// For vectors, this includes constants with undefined elements.
491inline cst_pred_ty<is_strictlypositive> m_StrictlyPositive() {
492 return cst_pred_ty<is_strictlypositive>();
493}
494inline api_pred_ty<is_strictlypositive> m_StrictlyPositive(const APInt *&V) {
495 return V;
496}
497
498struct is_nonpositive {
499 bool isValue(const APInt &C) { return C.isNonPositive(); }
500};
501/// Match an integer or vector of non-positive values.
502/// For vectors, this includes constants with undefined elements.
503inline cst_pred_ty<is_nonpositive> m_NonPositive() {
504 return cst_pred_ty<is_nonpositive>();
505}
506inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
507
508struct is_one {
509 bool isValue(const APInt &C) { return C.isOneValue(); }
510};
511/// Match an integer 1 or a vector with all elements equal to 1.
512/// For vectors, this includes constants with undefined elements.
513inline cst_pred_ty<is_one> m_One() {
514 return cst_pred_ty<is_one>();
515}
516
517struct is_zero_int {
518 bool isValue(const APInt &C) { return C.isNullValue(); }
519};
520/// Match an integer 0 or a vector with all elements equal to 0.
521/// For vectors, this includes constants with undefined elements.
522inline cst_pred_ty<is_zero_int> m_ZeroInt() {
523 return cst_pred_ty<is_zero_int>();
524}
525
526struct is_zero {
527 template <typename ITy> bool match(ITy *V) {
528 auto *C = dyn_cast<Constant>(V);
529 // FIXME: this should be able to do something for scalable vectors
530 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
531 }
532};
533/// Match any null constant or a vector with all elements equal to 0.
534/// For vectors, this includes constants with undefined elements.
535inline is_zero m_Zero() {
536 return is_zero();
537}
538
539struct is_power2 {
540 bool isValue(const APInt &C) { return C.isPowerOf2(); }
541};
542/// Match an integer or vector power-of-2.
543/// For vectors, this includes constants with undefined elements.
544inline cst_pred_ty<is_power2> m_Power2() {
545 return cst_pred_ty<is_power2>();
546}
547inline api_pred_ty<is_power2> m_Power2(const APInt *&V) {
548 return V;
549}
550
551struct is_negated_power2 {
552 bool isValue(const APInt &C) { return (-C).isPowerOf2(); }
553};
554/// Match a integer or vector negated power-of-2.
555/// For vectors, this includes constants with undefined elements.
556inline cst_pred_ty<is_negated_power2> m_NegatedPower2() {
557 return cst_pred_ty<is_negated_power2>();
558}
559inline api_pred_ty<is_negated_power2> m_NegatedPower2(const APInt *&V) {
560 return V;
561}
562
563struct is_power2_or_zero {
564 bool isValue(const APInt &C) { return !C || C.isPowerOf2(); }
565};
566/// Match an integer or vector of 0 or power-of-2 values.
567/// For vectors, this includes constants with undefined elements.
568inline cst_pred_ty<is_power2_or_zero> m_Power2OrZero() {
569 return cst_pred_ty<is_power2_or_zero>();
570}
571inline api_pred_ty<is_power2_or_zero> m_Power2OrZero(const APInt *&V) {
572 return V;
573}
574
575struct is_sign_mask {
576 bool isValue(const APInt &C) { return C.isSignMask(); }
577};
578/// Match an integer or vector with only the sign bit(s) set.
579/// For vectors, this includes constants with undefined elements.
580inline cst_pred_ty<is_sign_mask> m_SignMask() {
581 return cst_pred_ty<is_sign_mask>();