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>();
582}
583
584struct is_lowbit_mask {
585 bool isValue(const APInt &C) { return C.isMask(); }
586};
587/// Match an integer or vector with only the low bit(s) set.
588/// For vectors, this includes constants with undefined elements.
589inline cst_pred_ty<is_lowbit_mask> m_LowBitMask() {
590 return cst_pred_ty<is_lowbit_mask>();
591}
592
593struct icmp_pred_with_threshold {
594 ICmpInst::Predicate Pred;
595 const APInt *Thr;
596 bool isValue(const APInt &C) {
597 switch (Pred) {
598 case ICmpInst::Predicate::ICMP_EQ:
599 return C.eq(*Thr);
600 case ICmpInst::Predicate::ICMP_NE:
601 return C.ne(*Thr);
602 case ICmpInst::Predicate::ICMP_UGT:
603 return C.ugt(*Thr);
604 case ICmpInst::Predicate::ICMP_UGE:
605 return C.uge(*Thr);
606 case ICmpInst::Predicate::ICMP_ULT:
607 return C.ult(*Thr);
608 case ICmpInst::Predicate::ICMP_ULE:
609 return C.ule(*Thr);
610 case ICmpInst::Predicate::ICMP_SGT:
611 return C.sgt(*Thr);
612 case ICmpInst::Predicate::ICMP_SGE:
613 return C.sge(*Thr);
614 case ICmpInst::Predicate::ICMP_SLT:
615 return C.slt(*Thr);
616 case ICmpInst::Predicate::ICMP_SLE:
617 return C.sle(*Thr);
618 default:
619 llvm_unreachable("Unhandled ICmp predicate")::llvm::llvm_unreachable_internal("Unhandled ICmp predicate",
"/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/PatternMatch.h"
, 619)
;
620 }
621 }
622};
623/// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
624/// to Threshold. For vectors, this includes constants with undefined elements.
625inline cst_pred_ty<icmp_pred_with_threshold>
626m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold) {
627 cst_pred_ty<icmp_pred_with_threshold> P;
628 P.Pred = Predicate;
629 P.Thr = &Threshold;
630 return P;
631}
632
633struct is_nan {
634 bool isValue(const APFloat &C) { return C.isNaN(); }
635};
636/// Match an arbitrary NaN constant. This includes quiet and signalling nans.
637/// For vectors, this includes constants with undefined elements.
638inline cstfp_pred_ty<is_nan> m_NaN() {
639 return cstfp_pred_ty<is_nan>();
640}
641
642struct is_nonnan {
643 bool isValue(const APFloat &C) { return !C.isNaN(); }
644};
645/// Match a non-NaN FP constant.
646/// For vectors, this includes constants with undefined elements.
647inline cstfp_pred_ty<is_nonnan> m_NonNaN() {
648 return cstfp_pred_ty<is_nonnan>();
649}
650
651struct is_inf {
652 bool isValue(const APFloat &C) { return C.isInfinity(); }
653};
654/// Match a positive or negative infinity FP constant.
655/// For vectors, this includes constants with undefined elements.
656inline cstfp_pred_ty<is_inf> m_Inf() {
657 return cstfp_pred_ty<is_inf>();
658}
659
660struct is_noninf {
661 bool isValue(const APFloat &C) { return !C.isInfinity(); }
662};
663/// Match a non-infinity FP constant, i.e. finite or NaN.
664/// For vectors, this includes constants with undefined elements.
665inline cstfp_pred_ty<is_noninf> m_NonInf() {
666 return cstfp_pred_ty<is_noninf>();
667}
668
669struct is_finite {
670 bool isValue(const APFloat &C) { return C.isFinite(); }
671};
672/// Match a finite FP constant, i.e. not infinity or NaN.
673/// For vectors, this includes constants with undefined elements.
674inline cstfp_pred_ty<is_finite> m_Finite() {
675 return cstfp_pred_ty<is_finite>();
676}
677inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
678
679struct is_finitenonzero {
680 bool isValue(const APFloat &C) { return C.isFiniteNonZero(); }
681};
682/// Match a finite non-zero FP constant.
683/// For vectors, this includes constants with undefined elements.
684inline cstfp_pred_ty<is_finitenonzero> m_FiniteNonZero() {
685 return cstfp_pred_ty<is_finitenonzero>();
686}
687inline apf_pred_ty<is_finitenonzero> m_FiniteNonZero(const APFloat *&V) {
688 return V;
689}
690
691struct is_any_zero_fp {
692 bool isValue(const APFloat &C) { return C.isZero(); }
693};
694/// Match a floating-point negative zero or positive zero.
695/// For vectors, this includes constants with undefined elements.
696inline cstfp_pred_ty<is_any_zero_fp> m_AnyZeroFP() {
697 return cstfp_pred_ty<is_any_zero_fp>();
698}
699
700struct is_pos_zero_fp {
701 bool isValue(const APFloat &C) { return C.isPosZero(); }
702};
703/// Match a floating-point positive zero.
704/// For vectors, this includes constants with undefined elements.
705inline cstfp_pred_ty<is_pos_zero_fp> m_PosZeroFP() {
706 return cstfp_pred_ty<is_pos_zero_fp>();
707}
708
709struct is_neg_zero_fp {
710 bool isValue(const APFloat &C) { return C.isNegZero(); }
711};
712/// Match a floating-point negative zero.
713/// For vectors, this includes constants with undefined elements.
714inline cstfp_pred_ty<is_neg_zero_fp> m_NegZeroFP() {
715 return cstfp_pred_ty<is_neg_zero_fp>();
716}
717
718struct is_non_zero_fp {
719 bool isValue(const APFloat &C) { return C.isNonZero(); }
720};
721/// Match a floating-point non-zero.
722/// For vectors, this includes constants with undefined elements.
723inline cstfp_pred_ty<is_non_zero_fp> m_NonZeroFP() {
724 return cstfp_pred_ty<is_non_zero_fp>();
725}
726
727///////////////////////////////////////////////////////////////////////////////
728
729template <typename Class> struct bind_ty {
730 Class *&VR;
731
732 bind_ty(Class *&V) : VR(V) {}
733
734 template <typename ITy> bool match(ITy *V) {
735 if (auto *CV = dyn_cast<Class>(V)) {
736 VR = CV;
737 return true;
738 }
739 return false;
740 }
741};
742
743/// Match a value, capturing it if we match.
744inline bind_ty<Value> m_Value(Value *&V) { return V; }
745inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
746
747/// Match an instruction, capturing it if we match.
748inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; }
749/// Match a unary operator, capturing it if we match.
750inline bind_ty<UnaryOperator> m_UnOp(UnaryOperator *&I) { return I; }
751/// Match a binary operator, capturing it if we match.
752inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
753/// Match a with overflow intrinsic, capturing it if we match.
754inline bind_ty<WithOverflowInst> m_WithOverflowInst(WithOverflowInst *&I) { return I; }
755inline bind_ty<const WithOverflowInst>
756m_WithOverflowInst(const WithOverflowInst *&I) {
757 return I;
758}
759
760/// Match a Constant, capturing the value if we match.
761inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
762
763/// Match a ConstantInt, capturing the value if we match.
764inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
765
766/// Match a ConstantFP, capturing the value if we match.
767inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
768
769/// Match a ConstantExpr, capturing the value if we match.
770inline bind_ty<ConstantExpr> m_ConstantExpr(ConstantExpr *&C) { return C; }
771
772/// Match a basic block value, capturing it if we match.
773inline bind_ty<BasicBlock> m_BasicBlock(BasicBlock *&V) { return V; }
774inline bind_ty<const BasicBlock> m_BasicBlock(const BasicBlock *&V) {
775 return V;
776}
777
778/// Match an arbitrary immediate Constant and ignore it.
779inline match_combine_and<class_match<Constant>,
780 match_unless<class_match<ConstantExpr>>>
781m_ImmConstant() {
782 return m_CombineAnd(m_Constant(), m_Unless(m_ConstantExpr()));
783}
784
785/// Match an immediate Constant, capturing the value if we match.
786inline match_combine_and<bind_ty<Constant>,
787 match_unless<class_match<ConstantExpr>>>
788m_ImmConstant(Constant *&C) {
789 return m_CombineAnd(m_Constant(C), m_Unless(m_ConstantExpr()));
790}
791
792/// Match a specified Value*.
793struct specificval_ty {
794 const Value *Val;
795
796 specificval_ty(const Value *V) : Val(V) {}
797
798 template <typename ITy> bool match(ITy *V) { return V == Val; }
799};
800
801/// Match if we have a specific specified value.
802inline specificval_ty m_Specific(const Value *V) { return V; }
803
804/// Stores a reference to the Value *, not the Value * itself,
805/// thus can be used in commutative matchers.
806template <typename Class> struct deferredval_ty {
807 Class *const &Val;
808
809 deferredval_ty(Class *const &V) : Val(V) {}
810
811 template <typename ITy> bool match(ITy *const V) { return V == Val; }
812};
813
814/// Like m_Specific(), but works if the specific value to match is determined
815/// as part of the same match() expression. For example:
816/// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
817/// bind X before the pattern match starts.
818/// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
819/// whichever value m_Value(X) populated.
820inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
821inline deferredval_ty<const Value> m_Deferred(const Value *const &V) {
822 return V;
823}
824
825/// Match a specified floating point value or vector of all elements of
826/// that value.
827struct specific_fpval {
828 double Val;
829
830 specific_fpval(double V) : Val(V) {}
831
832 template <typename ITy> bool match(ITy *V) {
833 if (const auto *CFP = dyn_cast<ConstantFP>(V))
834 return CFP->isExactlyValue(Val);
835 if (V->getType()->isVectorTy())
836 if (const auto *C = dyn_cast<Constant>(V))
837 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
838 return CFP->isExactlyValue(Val);
839 return false;
840 }
841};
842
843/// Match a specific floating point value or vector with all elements
844/// equal to the value.
845inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
846
847/// Match a float 1.0 or vector with all elements equal to 1.0.
848inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
849
850struct bind_const_intval_ty {
851 uint64_t &VR;
852
853 bind_const_intval_ty(uint64_t &V) : VR(V) {}
854
855 template <typename ITy> bool match(ITy *V) {
856 if (const auto *CV = dyn_cast<ConstantInt>(V))
857 if (CV->getValue().ule(UINT64_MAX(18446744073709551615UL))) {
858 VR = CV->getZExtValue();
859 return true;
860 }
861 return false;
862 }
863};
864
865/// Match a specified integer value or vector of all elements of that
866/// value.
867template <bool AllowUndefs>
868struct specific_intval {
869 APInt Val;
870
871 specific_intval(APInt V) : Val(std::move(V)) {}
872
873 template <typename ITy> bool match(ITy *V) {
874 const auto *CI = dyn_cast<ConstantInt>(V);
875 if (!CI && V->getType()->isVectorTy())
876 if (const auto *C = dyn_cast<Constant>(V))
877 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowUndefs));
878
879 return CI && APInt::isSameValue(CI->getValue(), Val);
880 }
881};
882
883/// Match a specific integer value or vector with all elements equal to
884/// the value.
885inline specific_intval<false> m_SpecificInt(APInt V) {
886 return specific_intval<false>(std::move(V));
887}
888
889inline specific_intval<false> m_SpecificInt(uint64_t V) {
890 return m_SpecificInt(APInt(64, V));
891}
892
893inline specific_intval<true> m_SpecificIntAllowUndef(APInt V) {
894 return specific_intval<true>(std::move(V));
895}
896
897inline specific_intval<true> m_SpecificIntAllowUndef(uint64_t V) {
898 return m_SpecificIntAllowUndef(APInt(64, V));
899}
900
901/// Match a ConstantInt and bind to its value. This does not match
902/// ConstantInts wider than 64-bits.
903inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
904
905/// Match a specified basic block value.
906struct specific_bbval {
907 BasicBlock *Val;
908
909 specific_bbval(BasicBlock *Val) : Val(Val) {}
910
911 template <typename ITy> bool match(ITy *V) {
912 const auto *BB = dyn_cast<BasicBlock>(V);
913 return BB && BB == Val;
914 }
915};
916
917/// Match a specific basic block value.
918inline specific_bbval m_SpecificBB(BasicBlock *BB) {
919 return specific_bbval(BB);
920}
921
922/// A commutative-friendly version of m_Specific().
923inline deferredval_ty<BasicBlock> m_Deferred(BasicBlock *const &BB) {
924 return BB;
925}
926inline deferredval_ty<const BasicBlock>
927m_Deferred(const BasicBlock *const &BB) {
928 return BB;
929}
930
931//===----------------------------------------------------------------------===//
932// Matcher for any binary operator.
933//
934template <typename LHS_t, typename RHS_t, bool Commutable = false>
935struct AnyBinaryOp_match {
936 LHS_t L;
937 RHS_t R;
938
939 // The evaluation order is always stable, regardless of Commutability.
940 // The LHS is always matched first.
941 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
942
943 template <typename OpTy> bool match(OpTy *V) {
944 if (auto *I = dyn_cast<BinaryOperator>(V))
945 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
946 (Commutable && L.match(I->getOperand(1)) &&
947 R.match(I->getOperand(0)));
948 return false;
949 }
950};
951
952template <typename LHS, typename RHS>
953inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
954 return AnyBinaryOp_match<LHS, RHS>(L, R);
955}
956
957//===----------------------------------------------------------------------===//
958// Matcher for any unary operator.
959// TODO fuse unary, binary matcher into n-ary matcher
960//
961template <typename OP_t> struct AnyUnaryOp_match {
962 OP_t X;
963
964 AnyUnaryOp_match(const OP_t &X) : X(X) {}
965
966 template <typename OpTy> bool match(OpTy *V) {
967 if (auto *I = dyn_cast<UnaryOperator>(V))
968 return X.match(I->getOperand(0));
969 return false;
970 }
971};
972
973template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
974 return AnyUnaryOp_match<OP_t>(X);
975}
976
977//===----------------------------------------------------------------------===//
978// Matchers for specific binary operators.
979//
980
981template <typename LHS_t, typename RHS_t, unsigned Opcode,
982 bool Commutable = false>
983struct BinaryOp_match {
984 LHS_t L;
985 RHS_t R;
986
987 // The evaluation order is always stable, regardless of Commutability.
988 // The LHS is always matched first.
989 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
990
991 template <typename OpTy> bool match(OpTy *V) {
992 if (V->getValueID() == Value::InstructionVal + Opcode) {
993 auto *I = cast<BinaryOperator>(V);
994 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
995 (Commutable && L.match(I->getOperand(1)) &&
996 R.match(I->getOperand(0)));
997 }
998 if (auto *CE = dyn_cast<ConstantExpr>(V))
999 return CE->getOpcode() == Opcode &&
1000 ((L.match(CE->getOperand(0)) && R.match(CE->getOperand(1))) ||
1001 (Commutable && L.match(CE->getOperand(1)) &&
1002 R.match(CE->getOperand(0))));
1003 return false;
1004 }
1005};
1006
1007template <typename LHS, typename RHS>
1008inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
1009 const RHS &R) {
1010 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
1011}
1012
1013template <typename LHS, typename RHS>
1014inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L,
1015 const RHS &R) {
1016 return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
1017}
1018
1019template <typename LHS, typename RHS>
1020inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
1021 const RHS &R) {
1022 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
1023}
1024
1025template <typename LHS, typename RHS>
1026inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L,
1027 const RHS &R) {
1028 return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
1029}
1030
1031template <typename Op_t> struct FNeg_match {
1032 Op_t X;
1033
1034 FNeg_match(const Op_t &Op) : X(Op) {}
1035 template <typename OpTy> bool match(OpTy *V) {
1036 auto *FPMO = dyn_cast<FPMathOperator>(V);
1037 if (!FPMO) return false;
1038
1039 if (FPMO->getOpcode() == Instruction::FNeg)
1040 return X.match(FPMO->getOperand(0));
1041
1042 if (FPMO->getOpcode() == Instruction::FSub) {
1043 if (FPMO->hasNoSignedZeros()) {
1044 // With 'nsz', any zero goes.
1045 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
1046 return false;
1047 } else {
1048 // Without 'nsz', we need fsub -0.0, X exactly.
1049 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1050 return false;
1051 }
1052
1053 return X.match(FPMO->getOperand(1));
1054 }
1055
1056 return false;
1057 }
1058};
1059
1060/// Match 'fneg X' as 'fsub -0.0, X'.
1061template <typename OpTy>
1062inline FNeg_match<OpTy>
1063m_FNeg(const OpTy &X) {
1064 return FNeg_match<OpTy>(X);
1065}
1066
1067/// Match 'fneg X' as 'fsub +-0.0, X'.
1068template <typename RHS>
1069inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1070m_FNegNSZ(const RHS &X) {
1071 return m_FSub(m_AnyZeroFP(), X);
1072}
1073
1074template <typename LHS, typename RHS>
1075inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
1076 const RHS &R) {
1077 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
1078}
1079
1080template <typename LHS, typename RHS>
1081inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L,
1082 const RHS &R) {
1083 return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
1084}
1085
1086template <typename LHS, typename RHS>
1087inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L,
1088 const RHS &R) {
1089 return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
1090}
1091
1092template <typename LHS, typename RHS>
1093inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L,
1094 const RHS &R) {
1095 return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
1096}
1097
1098template <typename LHS, typename RHS>
1099inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L,
1100 const RHS &R) {
1101 return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
1102}
1103
1104template <typename LHS, typename RHS>
1105inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L,
1106 const RHS &R) {
1107 return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
1108}
1109
1110template <typename LHS, typename RHS>
1111inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L,
1112 const RHS &R) {
1113 return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
1114}
1115
1116template <typename LHS, typename RHS>
1117inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L,
1118 const RHS &R) {
1119 return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
1120}
1121
1122template <typename LHS, typename RHS>
1123inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
1124 const RHS &R) {
1125 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
1126}
1127
1128template <typename LHS, typename RHS>
1129inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L,
1130 const RHS &R) {
1131 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
1132}
1133
1134template <typename LHS, typename RHS>
1135inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
1136 const RHS &R) {
1137 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
1138}
1139
1140template <typename LHS, typename RHS>
1141inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L,
1142 const RHS &R) {
1143 return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
1144}
1145
1146template <typename LHS, typename RHS>
1147inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L,
1148 const RHS &R) {
1149 return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
1150}
1151
1152template <typename LHS, typename RHS>
1153inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L,
1154 const RHS &R) {
1155 return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
1156}
1157
1158template <typename LHS_t, typename RHS_t, unsigned Opcode,
1159 unsigned WrapFlags = 0>
1160struct OverflowingBinaryOp_match {
1161 LHS_t L;
1162 RHS_t R;
1163
1164 OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
1165 : L(LHS), R(RHS) {}
1166
1167 template <typename OpTy> bool match(OpTy *V) {
1168 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1169 if (Op->getOpcode() != Opcode)
1170 return false;
1171 if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap &&
1172 !Op->hasNoUnsignedWrap())
1173 return false;
1174 if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
1175 !Op->hasNoSignedWrap())
1176 return false;
1177 return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
1178 }
1179 return false;
1180 }
1181};
1182
1183template <typename LHS, typename RHS>
1184inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1185 OverflowingBinaryOperator::NoSignedWrap>
1186m_NSWAdd(const LHS &L, const RHS &R) {
1187 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1188 OverflowingBinaryOperator::NoSignedWrap>(
1189 L, R);
1190}
1191template <typename LHS, typename RHS>
1192inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1193 OverflowingBinaryOperator::NoSignedWrap>
1194m_NSWSub(const LHS &L, const RHS &R) {
1195 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1196 OverflowingBinaryOperator::NoSignedWrap>(
1197 L, R);
1198}
1199template <typename LHS, typename RHS>
1200inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1201 OverflowingBinaryOperator::NoSignedWrap>
1202m_NSWMul(const LHS &L, const RHS &R) {
1203 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1204 OverflowingBinaryOperator::NoSignedWrap>(
1205 L, R);
1206}
1207template <typename LHS, typename RHS>
1208inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1209 OverflowingBinaryOperator::NoSignedWrap>
1210m_NSWShl(const LHS &L, const RHS &R) {
1211 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1212 OverflowingBinaryOperator::NoSignedWrap>(
1213 L, R);
1214}
1215
1216template <typename LHS, typename RHS>
1217inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1218 OverflowingBinaryOperator::NoUnsignedWrap>
1219m_NUWAdd(const LHS &L, const RHS &R) {
1220 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1221 OverflowingBinaryOperator::NoUnsignedWrap>(
1222 L, R);
1223}
1224template <typename LHS, typename RHS>
1225inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1226 OverflowingBinaryOperator::NoUnsignedWrap>
1227m_NUWSub(const LHS &L, const RHS &R) {
1228 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1229 OverflowingBinaryOperator::NoUnsignedWrap>(
1230 L, R);
1231}
1232template <typename LHS, typename RHS>
1233inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1234 OverflowingBinaryOperator::NoUnsignedWrap>
1235m_NUWMul(const LHS &L, const RHS &R) {
1236 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1237 OverflowingBinaryOperator::NoUnsignedWrap>(
1238 L, R);
1239}
1240template <typename LHS, typename RHS>
1241inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1242 OverflowingBinaryOperator::NoUnsignedWrap>
1243m_NUWShl(const LHS &L, const RHS &R) {
1244 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1245 OverflowingBinaryOperator::NoUnsignedWrap>(
1246 L, R);
1247}
1248
1249//===----------------------------------------------------------------------===//
1250// Class that matches a group of binary opcodes.
1251//
1252template <typename LHS_t, typename RHS_t, typename Predicate>
1253struct BinOpPred_match : Predicate {
1254 LHS_t L;
1255 RHS_t R;
1256
1257 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1258
1259 template <typename OpTy> bool match(OpTy *V) {
1260 if (auto *I = dyn_cast<Instruction>(V))
1261 return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) &&
1262 R.match(I->getOperand(1));
1263 if (auto *CE = dyn_cast<ConstantExpr>(V))
1264 return this->isOpType(CE->getOpcode()) && L.match(CE->getOperand(0)) &&
1265 R.match(CE->getOperand(1));
1266 return false;
1267 }
1268};
1269
1270struct is_shift_op {
1271 bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); }
1272};
1273
1274struct is_right_shift_op {
1275 bool isOpType(unsigned Opcode) {
1276 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1277 }
1278};
1279
1280struct is_logical_shift_op {
1281 bool isOpType(unsigned Opcode) {
1282 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1283 }
1284};
1285
1286struct is_bitwiselogic_op {
1287 bool isOpType(unsigned Opcode) {
1288 return Instruction::isBitwiseLogicOp(Opcode);
1289 }
1290};
1291
1292struct is_idiv_op {
1293 bool isOpType(unsigned Opcode) {
1294 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1295 }
1296};
1297
1298struct is_irem_op {
1299 bool isOpType(unsigned Opcode) {
1300 return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1301 }
1302};
1303
1304/// Matches shift operations.
1305template <typename LHS, typename RHS>
1306inline BinOpPred_match<LHS, RHS, is_shift_op> m_Shift(const LHS &L,
1307 const RHS &R) {
1308 return BinOpPred_match<LHS, RHS, is_shift_op>(L, R);
1309}
1310
1311/// Matches logical shift operations.
1312template <typename LHS, typename RHS>
1313inline BinOpPred_match<LHS, RHS, is_right_shift_op> m_Shr(const LHS &L,
1314 const RHS &R) {
1315 return BinOpPred_match<LHS, RHS, is_right_shift_op>(L, R);
1316}
1317
1318/// Matches logical shift operations.
1319template <typename LHS, typename RHS>
1320inline BinOpPred_match<LHS, RHS, is_logical_shift_op>
1321m_LogicalShift(const LHS &L, const RHS &R) {
1322 return BinOpPred_match<LHS, RHS, is_logical_shift_op>(L, R);
1323}
1324
1325/// Matches bitwise logic operations.
1326template <typename LHS, typename RHS>
1327inline BinOpPred_match<LHS, RHS, is_bitwiselogic_op>
1328m_BitwiseLogic(const LHS &L, const RHS &R) {
1329 return BinOpPred_match<LHS, RHS, is_bitwiselogic_op>(L, R);
1330}
1331
1332/// Matches integer division operations.
1333template <typename LHS, typename RHS>
1334inline BinOpPred_match<LHS, RHS, is_idiv_op> m_IDiv(const LHS &L,
1335 const RHS &R) {
1336 return BinOpPred_match<LHS, RHS, is_idiv_op>(L, R);
1337}
1338
1339/// Matches integer remainder operations.
1340template <typename LHS, typename RHS>
1341inline BinOpPred_match<LHS, RHS, is_irem_op> m_IRem(const LHS &L,
1342 const RHS &R) {
1343 return BinOpPred_match<LHS, RHS, is_irem_op>(L, R);
1344}
1345
1346//===----------------------------------------------------------------------===//
1347// Class that matches exact binary ops.
1348//
1349template <typename SubPattern_t> struct Exact_match {
1350 SubPattern_t SubPattern;
1351
1352 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1353
1354 template <typename OpTy> bool match(OpTy *V) {
1355 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1356 return PEO->isExact() && SubPattern.match(V);
1357 return false;
1358 }
1359};
1360
1361template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1362 return SubPattern;
1363}
1364
1365//===----------------------------------------------------------------------===//
1366// Matchers for CmpInst classes
1367//
1368
1369template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy,
1370 bool Commutable = false>
1371struct CmpClass_match {
1372 PredicateTy &Predicate;
1373 LHS_t L;
1374 RHS_t R;
1375
1376 // The evaluation order is always stable, regardless of Commutability.
1377 // The LHS is always matched first.
1378 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
1379 : Predicate(Pred), L(LHS), R(RHS) {}
1380
1381 template <typename OpTy> bool match(OpTy *V) {
1382 if (auto *I = dyn_cast<Class>(V)) {
1383 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1384 Predicate = I->getPredicate();
1385 return true;
1386 } else if (Commutable && L.match(I->getOperand(1)) &&
1387 R.match(I->getOperand(0))) {
1388 Predicate = I->getSwappedPredicate();
1389 return true;
1390 }
1391 }
1392 return false;
1393 }
1394};
1395
1396template <typename LHS, typename RHS>
1397inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
1398m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1399 return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R);
1400}
1401
1402template <typename LHS, typename RHS>
1403inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
1404m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1405 return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R);
1406}
1407
1408template <typename LHS, typename RHS>
1409inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
1410m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1411 return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R);
1412}
1413
1414//===----------------------------------------------------------------------===//
1415// Matchers for instructions with a given opcode and number of operands.
1416//
1417
1418/// Matches instructions with Opcode and three operands.
1419template <typename T0, unsigned Opcode> struct OneOps_match {
1420 T0 Op1;
1421
1422 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1423
1424 template <typename OpTy> bool match(OpTy *V) {
1425 if (V->getValueID() == Value::InstructionVal + Opcode) {
1426 auto *I = cast<Instruction>(V);
1427 return Op1.match(I->getOperand(0));
1428 }
1429 return false;
1430 }
1431};
1432
1433/// Matches instructions with Opcode and three operands.
1434template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1435 T0 Op1;
1436 T1 Op2;
1437
1438 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1439
1440 template <typename OpTy> bool match(OpTy *V) {
1441 if (V->getValueID() == Value::InstructionVal + Opcode) {
1442 auto *I = cast<Instruction>(V);
1443 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1444 }
1445 return false;
1446 }
1447};
1448
1449/// Matches instructions with Opcode and three operands.
1450template <typename T0, typename T1, typename T2, unsigned Opcode>
1451struct ThreeOps_match {
1452 T0 Op1;
1453 T1 Op2;
1454 T2 Op3;
1455
1456 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1457 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1458
1459 template <typename OpTy> bool match(OpTy *V) {
1460 if (V->getValueID() == Value::InstructionVal + Opcode) {
1461 auto *I = cast<Instruction>(V);
1462 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1463 Op3.match(I->getOperand(2));
1464 }
1465 return false;
1466 }
1467};
1468
1469/// Matches SelectInst.
1470template <typename Cond, typename LHS, typename RHS>
1471inline ThreeOps_match<Cond, LHS, RHS, Instruction::Select>
1472m_Select(const Cond &C, const LHS &L, const RHS &R) {
1473 return ThreeOps_match<Cond, LHS, RHS, Instruction::Select>(C, L, R);
1474}
1475
1476/// This matches a select of two constants, e.g.:
1477/// m_SelectCst<-1, 0>(m_Value(V))
1478template <int64_t L, int64_t R, typename Cond>
1479inline ThreeOps_match<Cond, constantint_match<L>, constantint_match<R>,
1480 Instruction::Select>
1481m_SelectCst(const Cond &C) {
1482 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
1483}
1484
1485/// Matches FreezeInst.
1486template <typename OpTy>
1487inline OneOps_match<OpTy, Instruction::Freeze> m_Freeze(const OpTy &Op) {
1488 return OneOps_match<OpTy, Instruction::Freeze>(Op);
1489}
1490
1491/// Matches InsertElementInst.
1492template <typename Val_t, typename Elt_t, typename Idx_t>
1493inline ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>
1494m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1495 return ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>(
1496 Val, Elt, Idx);
1497}
1498
1499/// Matches ExtractElementInst.
1500template <typename Val_t, typename Idx_t>
1501inline TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>
1502m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
1503 return TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>(Val, Idx);
1504}
1505
1506/// Matches shuffle.
1507template <typename T0, typename T1, typename T2> struct Shuffle_match {
1508 T0 Op1;
1509 T1 Op2;
1510 T2 Mask;
1511
1512 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
1513 : Op1(Op1), Op2(Op2), Mask(Mask) {}
1514
1515 template <typename OpTy> bool match(OpTy *V) {
1516 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
1517 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1518 Mask.match(I->getShuffleMask());
1519 }
1520 return false;
1521 }
1522};
1523
1524struct m_Mask {
1525 ArrayRef<int> &MaskRef;
1526 m_Mask(ArrayRef<int> &MaskRef) : MaskRef(MaskRef) {}
1527 bool match(ArrayRef<int> Mask) {
1528 MaskRef = Mask;
1529 return true;
1530 }
1531};
1532
1533struct m_ZeroMask {
1534 bool match(ArrayRef<int> Mask) {
1535 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
1536 }
1537};
1538
1539struct m_SpecificMask {
1540 ArrayRef<int> &MaskRef;
1541 m_SpecificMask(ArrayRef<int> &MaskRef) : MaskRef(MaskRef) {}
1542 bool match(ArrayRef<int> Mask) { return MaskRef == Mask; }
1543};
1544
1545struct m_SplatOrUndefMask {
1546 int &SplatIndex;
1547 m_SplatOrUndefMask(int &SplatIndex) : SplatIndex(SplatIndex) {}
1548 bool match(ArrayRef<int> Mask) {
1549 auto First = find_if(Mask, [](int Elem) { return Elem != -1; });
1550 if (First == Mask.end())
1551 return false;
1552 SplatIndex = *First;
1553 return all_of(Mask,
1554 [First](int Elem) { return Elem == *First || Elem == -1; });
1555 }
1556};
1557
1558/// Matches ShuffleVectorInst independently of mask value.
1559template <typename V1_t, typename V2_t>
1560inline TwoOps_match<V1_t, V2_t, Instruction::ShuffleVector>
1561m_Shuffle(const V1_t &v1, const V2_t &v2) {
1562 return TwoOps_match<V1_t, V2_t, Instruction::ShuffleVector>(v1, v2);
1563}
1564
1565template <typename V1_t, typename V2_t, typename Mask_t>
1566inline Shuffle_match<V1_t, V2_t, Mask_t>
1567m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
1568 return Shuffle_match<V1_t, V2_t, Mask_t>(v1, v2, mask);
1569}
1570
1571/// Matches LoadInst.
1572template <typename OpTy>
1573inline OneOps_match<OpTy, Instruction::Load> m_Load(const OpTy &Op) {
1574 return OneOps_match<OpTy, Instruction::Load>(Op);
1575}
1576
1577/// Matches StoreInst.
1578template <typename ValueOpTy, typename PointerOpTy>
1579inline TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>
1580m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
1581 return TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>(ValueOp,
1582 PointerOp);
1583}
1584
1585//===----------------------------------------------------------------------===//
1586// Matchers for CastInst classes
1587//
1588
1589template <typename Op_t, unsigned Opcode> struct CastClass_match {
1590 Op_t Op;
1591
1592 CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
1593
1594 template <typename OpTy> bool match(OpTy *V) {
1595 if (auto *O = dyn_cast<Operator>(V))
1596 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
1597 return false;
1598 }
1599};
1600
1601/// Matches BitCast.
1602template <typename OpTy>
1603inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) {
1604 return CastClass_match<OpTy, Instruction::BitCast>(Op);
1605}
1606
1607/// Matches PtrToInt.
1608template <typename OpTy>
1609inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) {
1610 return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
1611}
1612
1613/// Matches IntToPtr.
1614template <typename OpTy>
1615inline CastClass_match<OpTy, Instruction::IntToPtr> m_IntToPtr(const OpTy &Op) {
1616 return CastClass_match<OpTy, Instruction::IntToPtr>(Op);
1617}
1618
1619/// Matches Trunc.
1620template <typename OpTy>
1621inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) {
1622 return CastClass_match<OpTy, Instruction::Trunc>(Op);
1623}
1624
1625template <typename OpTy>
1626inline match_combine_or<CastClass_match<OpTy, Instruction::Trunc>, OpTy>
1627m_TruncOrSelf(const OpTy &Op) {
1628 return m_CombineOr(m_Trunc(Op), Op);
1629}
1630
1631/// Matches SExt.
1632template <typename OpTy>
1633inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) {
1634 return CastClass_match<OpTy, Instruction::SExt>(Op);
1635}
1636
1637/// Matches ZExt.
1638template <typename OpTy>
1639inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) {
1640 return CastClass_match<OpTy, Instruction::ZExt>(Op);
1641}
1642
1643template <typename OpTy>
1644inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>, OpTy>
1645m_ZExtOrSelf(const OpTy &Op) {
1646 return m_CombineOr(m_ZExt(Op), Op);
1647}
1648
1649template <typename OpTy>
1650inline match_combine_or<CastClass_match<OpTy, Instruction::SExt>, OpTy>
1651m_SExtOrSelf(const OpTy &Op) {
1652 return m_CombineOr(m_SExt(Op), Op);
1653}
1654
1655template <typename OpTy>
1656inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>,
1657 CastClass_match<OpTy, Instruction::SExt>>
1658m_ZExtOrSExt(const OpTy &Op) {
1659 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
1660}
1661
1662template <typename OpTy>
1663inline match_combine_or<
1664 match_combine_or<CastClass_match<OpTy, Instruction::ZExt>,
1665 CastClass_match<OpTy, Instruction::SExt>>,
1666 OpTy>
1667m_ZExtOrSExtOrSelf(const OpTy &Op) {
1668 return m_CombineOr(m_ZExtOrSExt(Op), Op);
1669}
1670
1671template <typename OpTy>
1672inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) {
1673 return CastClass_match<OpTy, Instruction::UIToFP>(Op);
1674}
1675
1676template <typename OpTy>
1677inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) {
1678 return CastClass_match<OpTy, Instruction::SIToFP>(Op);
1679}
1680
1681template <typename OpTy>
1682inline CastClass_match<OpTy, Instruction::FPToUI> m_FPToUI(const OpTy &Op) {
1683 return CastClass_match<OpTy, Instruction::FPToUI>(Op);
1684}
1685
1686template <typename OpTy>
1687inline CastClass_match<OpTy, Instruction::FPToSI> m_FPToSI(const OpTy &Op) {
1688 return CastClass_match<OpTy, Instruction::FPToSI>(Op);
1689}
1690
1691template <typename OpTy>
1692inline CastClass_match<OpTy, Instruction::FPTrunc> m_FPTrunc(const OpTy &Op) {
1693 return CastClass_match<OpTy, Instruction::FPTrunc>(Op);
1694}
1695
1696template <typename OpTy>
1697inline CastClass_match<OpTy, Instruction::FPExt> m_FPExt(const OpTy &Op) {
1698 return CastClass_match<OpTy, Instruction::FPExt>(Op);
1699}
1700
1701//===----------------------------------------------------------------------===//
1702// Matchers for control flow.
1703//
1704
1705struct br_match {
1706 BasicBlock *&Succ;
1707
1708 br_match(BasicBlock *&Succ) : Succ(Succ) {}
1709
1710 template <typename OpTy> bool match(OpTy *V) {
1711 if (auto *BI = dyn_cast<BranchInst>(V))
1712 if (BI->isUnconditional()) {
1713 Succ = BI->getSuccessor(0);
1714 return true;
1715 }
1716 return false;
1717 }
1718};
1719
1720inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
1721
1722template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
1723struct brc_match {
1724 Cond_t Cond;
1725 TrueBlock_t T;
1726 FalseBlock_t F;
1727
1728 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
1729 : Cond(C), T(t), F(f) {}
1730
1731 template <typename OpTy> bool match(OpTy *V) {
1732 if (auto *BI = dyn_cast<BranchInst>(V))
1733 if (BI->isConditional() && Cond.match(BI->getCondition()))
1734 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
1735 return false;
1736 }
1737};
1738
1739template <typename Cond_t>
1740inline brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>>
1741m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
1742 return brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>>(
1743 C, m_BasicBlock(T), m_BasicBlock(F));
1744}
1745
1746template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
1747inline brc_match<Cond_t, TrueBlock_t, FalseBlock_t>
1748m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
1749 return brc_match<Cond_t, TrueBlock_t, FalseBlock_t>(C, T, F);
1750}
1751
1752//===----------------------------------------------------------------------===//
1753// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
1754//
1755
1756template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
1757 bool Commutable = false>
1758struct MaxMin_match {
1759 using PredType = Pred_t;
1760 LHS_t L;
1761 RHS_t R;
1762
1763 // The evaluation order is always stable, regardless of Commutability.
1764 // The LHS is always matched first.
1765 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1766
1767 template <typename OpTy> bool match(OpTy *V) {
1768 if (auto *II = dyn_cast<IntrinsicInst>(V)) {
1769 Intrinsic::ID IID = II->getIntrinsicID();
1770 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
1771 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
1772 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
1773 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
1774 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
1775 return (L.match(LHS) && R.match(RHS)) ||
1776 (Commutable && L.match(RHS) && R.match(LHS));
1777 }
1778 }
1779 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
1780 auto *SI = dyn_cast<SelectInst>(V);
1781 if (!SI)
1782 return false;
1783 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
1784 if (!Cmp)
1785 return false;
1786 // At this point we have a select conditioned on a comparison. Check that
1787 // it is the values returned by the select that are being compared.
1788 auto *TrueVal = SI->getTrueValue();
1789 auto *FalseVal = SI->getFalseValue();
1790 auto *LHS = Cmp->getOperand(0);
1791 auto *RHS = Cmp->getOperand(1);
1792 if ((TrueVal != LHS || FalseVal != RHS) &&
1793 (TrueVal != RHS || FalseVal != LHS))
1794 return false;
1795 typename CmpInst_t::Predicate Pred =
1796 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
1797 // Does "(x pred y) ? x : y" represent the desired max/min operation?
1798 if (!Pred_t::match(Pred))
1799 return false;
1800 // It does! Bind the operands.
1801 return (L.match(LHS) && R.match(RHS)) ||
1802 (Commutable && L.match(RHS) && R.match(LHS));
1803 }
1804};
1805
1806/// Helper class for identifying signed max predicates.
1807struct smax_pred_ty {
1808 static bool match(ICmpInst::Predicate Pred) {
1809 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
1810 }
1811};
1812
1813/// Helper class for identifying signed min predicates.
1814struct smin_pred_ty {
1815 static bool match(ICmpInst::Predicate Pred) {
1816 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
1817 }
1818};
1819
1820/// Helper class for identifying unsigned max predicates.
1821struct umax_pred_ty {
1822 static bool match(ICmpInst::Predicate Pred) {
1823 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
1824 }
1825};
1826
1827/// Helper class for identifying unsigned min predicates.
1828struct umin_pred_ty {
1829 static bool match(ICmpInst::Predicate Pred) {
1830 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
1831 }
1832};
1833
1834/// Helper class for identifying ordered max predicates.
1835struct ofmax_pred_ty {
1836 static bool match(FCmpInst::Predicate Pred) {
1837 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
1838 }
1839};
1840
1841/// Helper class for identifying ordered min predicates.
1842struct ofmin_pred_ty {
1843 static bool match(FCmpInst::Predicate Pred) {
1844 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
1845 }
1846};
1847
1848/// Helper class for identifying unordered max predicates.
1849struct ufmax_pred_ty {
1850 static bool match(FCmpInst::Predicate Pred) {
1851 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
1852 }
1853};
1854
1855/// Helper class for identifying unordered min predicates.
1856struct ufmin_pred_ty {
1857 static bool match(FCmpInst::Predicate Pred) {
1858 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
1859 }
1860};
1861
1862template <typename LHS, typename RHS>
1863inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L,
1864 const RHS &R) {
1865 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
1866}
1867
1868template <typename LHS, typename RHS>
1869inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L,
1870 const RHS &R) {
1871 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
1872}
1873
1874template <typename LHS, typename RHS>
1875inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L,
1876 const RHS &R) {
1877 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
1878}
1879
1880template <typename LHS, typename RHS>
1881inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L,
1882 const RHS &R) {
1883 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
1884}
1885
1886template <typename LHS, typename RHS>
1887inline match_combine_or<
1888 match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>,
1889 MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>>,
1890 match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>,
1891 MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>>>
1892m_MaxOrMin(const LHS &L, const RHS &R) {
1893 return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)),
1894 m_CombineOr(m_UMax(L, R), m_UMin(L, R)));
1895}
1896
1897/// Match an 'ordered' floating point maximum function.
1898/// Floating point has one special value 'NaN'. Therefore, there is no total
1899/// order. However, if we can ignore the 'NaN' value (for example, because of a
1900/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1901/// semantics. In the presence of 'NaN' we have to preserve the original
1902/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
1903///
1904/// max(L, R) iff L and R are not NaN
1905/// m_OrdFMax(L, R) = R iff L or R are NaN
1906template <typename LHS, typename RHS>
1907inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L,
1908 const RHS &R) {
1909 return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
1910}
1911
1912/// Match an 'ordered' floating point minimum function.
1913/// Floating point has one special value 'NaN'. Therefore, there is no total
1914/// order. However, if we can ignore the 'NaN' value (for example, because of a
1915/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1916/// semantics. In the presence of 'NaN' we have to preserve the original
1917/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
1918///
1919/// min(L, R) iff L and R are not NaN
1920/// m_OrdFMin(L, R) = R iff L or R are NaN
1921template <typename LHS, typename RHS>
1922inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L,
1923 const RHS &R) {
1924 return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
1925}
1926
1927/// Match an 'unordered' floating point maximum function.
1928/// Floating point has one special value 'NaN'. Therefore, there is no total
1929/// order. However, if we can ignore the 'NaN' value (for example, because of a
1930/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1931/// semantics. In the presence of 'NaN' we have to preserve the original
1932/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
1933///
1934/// max(L, R) iff L and R are not NaN
1935/// m_UnordFMax(L, R) = L iff L or R are NaN
1936template <typename LHS, typename RHS>
1937inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
1938m_UnordFMax(const LHS &L, const RHS &R) {
1939 return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R);
1940}
1941
1942/// Match an 'unordered' floating point minimum function.
1943/// Floating point has one special value 'NaN'. Therefore, there is no total
1944/// order. However, if we can ignore the 'NaN' value (for example, because of a
1945/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1946/// semantics. In the presence of 'NaN' we have to preserve the original
1947/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
1948///
1949/// min(L, R) iff L and R are not NaN
1950/// m_UnordFMin(L, R) = L iff L or R are NaN
1951template <typename LHS, typename RHS>
1952inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
1953m_UnordFMin(const LHS &L, const RHS &R) {
1954 return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
1955}
1956
1957//===----------------------------------------------------------------------===//
1958// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
1959// Note that S might be matched to other instructions than AddInst.
1960//
1961
1962template <typename LHS_t, typename RHS_t, typename Sum_t>
1963struct UAddWithOverflow_match {
1964 LHS_t L;
1965 RHS_t R;
1966 Sum_t S;
1967
1968 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
1969 : L(L), R(R), S(S) {}
1970
1971 template <typename OpTy> bool match(OpTy *V) {
1972 Value *ICmpLHS, *ICmpRHS;
1973 ICmpInst::Predicate Pred;
1974 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
1975 return false;
1976
1977 Value *AddLHS, *AddRHS;
1978 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
1979
1980 // (a + b) u< a, (a + b) u< b
1981 if (Pred == ICmpInst::ICMP_ULT)
1982 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
1983 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
1984
1985 // a >u (a + b), b >u (a + b)
1986 if (Pred == ICmpInst::ICMP_UGT)
1987 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
1988 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
1989
1990 Value *Op1;
1991 auto XorExpr = m_OneUse(m_Xor(m_Value(Op1), m_AllOnes()));
1992 // (a ^ -1) <u b
1993 if (Pred == ICmpInst::ICMP_ULT) {
1994 if (XorExpr.match(ICmpLHS))
1995 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
1996 }
1997 // b > u (a ^ -1)
1998 if (Pred == ICmpInst::ICMP_UGT) {
1999 if (XorExpr.match(ICmpRHS))
2000 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
2001 }
2002
2003 // Match special-case for increment-by-1.
2004 if (Pred == ICmpInst::ICMP_EQ) {
2005 // (a + 1) == 0
2006 // (1 + a) == 0
2007 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
2008 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2009 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
2010 // 0 == (a + 1)
2011 // 0 == (1 + a)
2012 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
2013 (m_One().match(AddLHS) || m_One().match(AddRHS)))
2014 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
2015 }
2016
2017 return false;
2018 }
2019};
2020
2021/// Match an icmp instruction checking for unsigned overflow on addition.
2022///
2023/// S is matched to the addition whose result is being checked for overflow, and
2024/// L and R are matched to the LHS and RHS of S.
2025template <typename LHS_t, typename RHS_t, typename Sum_t>
2026UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>
2027m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
2028 return UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>(L, R, S);
2029}
2030
2031template <typename Opnd_t> struct Argument_match {
2032 unsigned OpI;
2033 Opnd_t Val;
2034
2035 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
2036
2037 template <typename OpTy> bool match(OpTy *V) {
2038 // FIXME: Should likely be switched to use `CallBase`.
2039 if (const auto *CI = dyn_cast<CallInst>(V))
2040 return Val.match(CI->getArgOperand(OpI));
2041 return false;
2042 }
2043};
2044
2045/// Match an argument.
2046template <unsigned OpI, typename Opnd_t>
2047inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
2048 return Argument_match<Opnd_t>(OpI, Op);
2049}
2050
2051/// Intrinsic matchers.
2052struct IntrinsicID_match {
2053 unsigned ID;
2054
2055 IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
2056
2057 template <typename OpTy> bool match(OpTy *V) {
2058 if (const auto *CI
42.1
'CI' is null
51.1
'CI' is null
42.1
'CI' is null
51.1
'CI' is null
42.1
'CI' is null
51.1
'CI' is null
42.1
'CI' is null
51.1
'CI' is null
42.1
'CI' is null
51.1
'CI' is null
= dyn_cast<CallInst>(V))
42
Assuming 'V' is not a 'CallInst'
43
Taking false branch
51
'V' is not a 'CallInst'
52
Taking false branch
2059 if (const auto *F = CI->getCalledFunction())
2060 return F->getIntrinsicID() == ID;
2061 return false;
44
Returning zero, which participates in a condition later
53
Returning zero, which participates in a condition later
2062 }
2063};
2064
2065/// Intrinsic matches are combinations of ID matchers, and argument
2066/// matchers. Higher arity matcher are defined recursively in terms of and-ing
2067/// them with lower arity matchers. Here's some convenient typedefs for up to
2068/// several arguments, and more can be added as needed
2069template <typename T0 = void, typename T1 = void, typename T2 = void,
2070 typename T3 = void, typename T4 = void, typename T5 = void,
2071 typename T6 = void, typename T7 = void, typename T8 = void,
2072 typename T9 = void, typename T10 = void>
2073struct m_Intrinsic_Ty;
2074template <typename T0> struct m_Intrinsic_Ty<T0> {
2075 using Ty = match_combine_and<IntrinsicID_match, Argument_match<T0>>;
2076};
2077template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2078 using Ty =
2079 match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>;
2080};
2081template <typename T0, typename T1, typename T2>
2082struct m_Intrinsic_Ty<T0, T1, T2> {
2083 using Ty =
2084 match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
2085 Argument_match<T2>>;
2086};
2087template <typename T0, typename T1, typename T2, typename T3>
2088struct m_Intrinsic_Ty<T0, T1, T2, T3> {
2089 using Ty =
2090 match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
2091 Argument_match<T3>>;
2092};
2093
2094template <typename T0, typename T1, typename T2, typename T3, typename T4>
2095struct m_Intrinsic_Ty<T0, T1, T2, T3, T4> {
2096 using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty,
2097 Argument_match<T4>>;
2098};
2099
2100template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
2101struct m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5> {
2102 using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty,
2103 Argument_match<T5>>;
2104};
2105
2106/// Match intrinsic calls like this:
2107/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2108template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2109 return IntrinsicID_match(IntrID);
2110}
2111
2112template <Intrinsic::ID IntrID, typename T0>
2113inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2114 return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
2115}
2116
2117template <Intrinsic::ID IntrID, typename T0, typename T1>
2118inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2119 const T1 &Op1) {
2120 return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
2121}
2122
2123template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2124inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2125m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2126 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2127}
2128
2129template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2130 typename T3>
2131inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
2132m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2133 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2134}
2135
2136template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2137 typename T3, typename T4>
2138inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty
2139m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2140 const T4 &Op4) {
2141 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2142 m_Argument<4>(Op4));
2143}
2144
2145template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2146 typename T3, typename T4, typename T5>
2147inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5>::Ty
2148m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2149 const T4 &Op4, const T5 &Op5) {
2150 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2151 m_Argument<5>(Op5));
2152}
2153
2154// Helper intrinsic matching specializations.
2155template <typename Opnd0>
2156inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2157 return m_Intrinsic<Intrinsic::bitreverse>(Op0);
2158}
2159
2160template <typename Opnd0>
2161inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2162 return m_Intrinsic<Intrinsic::bswap>(Op0);
2163}
2164
2165template <typename Opnd0>
2166inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2167 return m_Intrinsic<Intrinsic::fabs>(Op0);
2168}
2169
2170template <typename Opnd0>
2171inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
2172 return m_Intrinsic<Intrinsic::canonicalize>(Op0);
2173}
2174
2175template <typename Opnd0, typename Opnd1>
2176inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
2177 const Opnd1 &Op1) {
2178 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2179}
2180
2181template <typename Opnd0, typename Opnd1>
2182inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
2183 const Opnd1 &Op1) {
2184 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
2185}
2186
2187template <typename Opnd0, typename Opnd1, typename Opnd2>
2188inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty
2189m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2190 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
2191}
2192
2193template <typename Opnd0, typename Opnd1, typename Opnd2>
2194inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty
2195m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2196 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
2197}
2198
2199//===----------------------------------------------------------------------===//
2200// Matchers for two-operands operators with the operators in either order
2201//
2202
2203/// Matches a BinaryOperator with LHS and RHS in either order.
2204template <typename LHS, typename RHS>
2205inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) {
2206 return AnyBinaryOp_match<LHS, RHS, true>(L, R);
2207}
2208
2209/// Matches an ICmp with a predicate over LHS and RHS in either order.
2210/// Swaps the predicate if operands are commuted.
2211template <typename LHS, typename RHS>
2212inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>
2213m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
2214 return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>(Pred, L,
2215 R);
2216}
2217
2218/// Matches a Add with LHS and RHS in either order.
2219template <typename LHS, typename RHS>
2220inline BinaryOp_match<LHS, RHS, Instruction::Add, true> m_c_Add(const LHS &L,
2221 const RHS &R) {
2222 return BinaryOp_match<LHS, RHS, Instruction::Add, true>(L, R);
2223}
2224
2225/// Matches a Mul with LHS and RHS in either order.
2226template <typename LHS, typename RHS>
2227inline BinaryOp_match<LHS, RHS, Instruction::Mul, true> m_c_Mul(const LHS &L,
2228 const RHS &R) {
2229 return BinaryOp_match<LHS, RHS, Instruction::Mul, true>(L, R);
2230}
2231
2232/// Matches an And with LHS and RHS in either order.
2233template <typename LHS, typename RHS>
2234inline BinaryOp_match<LHS, RHS, Instruction::And, true> m_c_And(const LHS &L,
2235 const RHS &R) {
2236 return BinaryOp_match<LHS, RHS, Instruction::And, true>(L, R);
2237}
2238
2239/// Matches an Or with LHS and RHS in either order.
2240template <typename LHS, typename RHS>
2241inline BinaryOp_match<LHS, RHS, Instruction::Or, true> m_c_Or(const LHS &L,
2242 const RHS &R) {
2243 return BinaryOp_match<LHS, RHS, Instruction::Or, true>(L, R);
2244}
2245
2246/// Matches an Xor with LHS and RHS in either order.
2247template <typename LHS, typename RHS>
2248inline BinaryOp_match<LHS, RHS, Instruction::Xor, true> m_c_Xor(const LHS &L,
2249 const RHS &R) {
2250 return BinaryOp_match<LHS, RHS, Instruction::Xor, true>(L, R);
2251}
2252
2253/// Matches a 'Neg' as 'sub 0, V'.
2254template <typename ValTy>
2255inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
2256m_Neg(const ValTy &V) {
2257 return m_Sub(m_ZeroInt(), V);
2258}
2259
2260/// Matches a 'Neg' as 'sub nsw 0, V'.
2261template <typename ValTy>
2262inline OverflowingBinaryOp_match<cst_pred_ty<is_zero_int>, ValTy,
2263 Instruction::Sub,
2264 OverflowingBinaryOperator::NoSignedWrap>
2265m_NSWNeg(const ValTy &V) {
2266 return m_NSWSub(m_ZeroInt(), V);
2267}
2268
2269/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2270template <typename ValTy>
2271inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true>
2272m_Not(const ValTy &V) {
2273 return m_c_Xor(V, m_AllOnes());
2274}
2275
2276/// Matches an SMin with LHS and RHS in either order.
2277template <typename LHS, typename RHS>
2278inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>
2279m_c_SMin(const LHS &L, const RHS &R) {
2280 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>(L, R);
2281}
2282/// Matches an SMax with LHS and RHS in either order.
2283template <typename LHS, typename RHS>
2284inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>
2285m_c_SMax(const LHS &L, const RHS &R) {
2286 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>(L, R);
2287}
2288/// Matches a UMin with LHS and RHS in either order.
2289template <typename LHS, typename RHS>
2290inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>
2291m_c_UMin(const LHS &L, const RHS &R) {
2292 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>(L, R);
2293}
2294/// Matches a UMax with LHS and RHS in either order.
2295template <typename LHS, typename RHS>
2296inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>
2297m_c_UMax(const LHS &L, const RHS &R) {
2298 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>(L, R);
2299}
2300
2301template <typename LHS, typename RHS>
2302inline match_combine_or<
2303 match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>,
2304 MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>>,
2305 match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>,
2306 MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>>>
2307m_c_MaxOrMin(const LHS &L, const RHS &R) {
2308 return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)),
2309 m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R)));
2310}
2311
2312/// Matches FAdd with LHS and RHS in either order.
2313template <typename LHS, typename RHS>
2314inline BinaryOp_match<LHS, RHS, Instruction::FAdd, true>
2315m_c_FAdd(const LHS &L, const RHS &R) {
2316 return BinaryOp_match<LHS, RHS, Instruction::FAdd, true>(L, R);
2317}
2318
2319/// Matches FMul with LHS and RHS in either order.
2320template <typename LHS, typename RHS>
2321inline BinaryOp_match<LHS, RHS, Instruction::FMul, true>
2322m_c_FMul(const LHS &L, const RHS &R) {
2323 return BinaryOp_match<LHS, RHS, Instruction::FMul, true>(L, R);
2324}
2325
2326template <typename Opnd_t> struct Signum_match {
2327 Opnd_t Val;
2328 Signum_match(const Opnd_t &V) : Val(V) {}
2329
2330 template <typename OpTy> bool match(OpTy *V) {
2331 unsigned TypeSize = V->getType()->getScalarSizeInBits();
2332 if (TypeSize == 0)
2333 return false;
2334
2335 unsigned ShiftWidth = TypeSize - 1;
2336 Value *OpL = nullptr, *OpR = nullptr;
2337
2338 // This is the representation of signum we match:
2339 //
2340 // signum(x) == (x >> 63) | (-x >>u 63)
2341 //
2342 // An i1 value is its own signum, so it's correct to match
2343 //
2344 // signum(x) == (x >> 0) | (-x >>u 0)
2345 //
2346 // for i1 values.
2347
2348 auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth));
2349 auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth));
2350 auto Signum = m_Or(LHS, RHS);
2351
2352 return Signum.match(V) && OpL == OpR && Val.match(OpL);
2353 }
2354};
2355
2356/// Matches a signum pattern.
2357///
2358/// signum(x) =
2359/// x > 0 -> 1
2360/// x == 0 -> 0
2361/// x < 0 -> -1
2362template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
2363 return Signum_match<Val_t>(V);
2364}
2365
2366template <int Ind, typename Opnd_t> struct ExtractValue_match {
2367 Opnd_t Val;
2368 ExtractValue_match(const Opnd_t &V) : Val(V) {}
2369
2370 template <typename OpTy> bool match(OpTy *V) {
2371 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
2372 // If Ind is -1, don't inspect indices
2373 if (Ind != -1 &&
2374 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
2375 return false;
2376 return Val.match(I->getAggregateOperand());
2377 }
2378 return false;
2379 }
2380};
2381
2382/// Match a single index ExtractValue instruction.
2383/// For example m_ExtractValue<1>(...)
2384template <int Ind, typename Val_t>
2385inline ExtractValue_match<Ind, Val_t> m_ExtractValue(const Val_t &V) {
2386 return ExtractValue_match<Ind, Val_t>(V);
2387}
2388
2389/// Match an ExtractValue instruction with any index.
2390/// For example m_ExtractValue(...)
2391template <typename Val_t>
2392inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
2393 return ExtractValue_match<-1, Val_t>(V);
2394}
2395
2396/// Matcher for a single index InsertValue instruction.
2397template <int Ind, typename T0, typename T1> struct InsertValue_match {
2398 T0 Op0;
2399 T1 Op1;
2400
2401 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
2402
2403 template <typename OpTy> bool match(OpTy *V) {
2404 if (auto *I = dyn_cast<InsertValueInst>(V)) {
2405 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
2406 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
2407 }
2408 return false;
2409 }
2410};
2411
2412/// Matches a single index InsertValue instruction.
2413template <int Ind, typename Val_t, typename Elt_t>
2414inline InsertValue_match<Ind, Val_t, Elt_t> m_InsertValue(const Val_t &Val,
2415 const Elt_t &Elt) {
2416 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
2417}
2418
2419/// Matches patterns for `vscale`. This can either be a call to `llvm.vscale` or
2420/// the constant expression
2421/// `ptrtoint(gep <vscale x 1 x i8>, <vscale x 1 x i8>* null, i32 1>`
2422/// under the right conditions determined by DataLayout.
2423struct VScaleVal_match {
2424private:
2425 template <typename Base, typename Offset>
2426 inline BinaryOp_match<Base, Offset, Instruction::GetElementPtr>
2427 m_OffsetGep(const Base &B, const Offset &O) {
2428 return BinaryOp_match<Base, Offset, Instruction::GetElementPtr>(B, O);
2429 }
2430
2431public:
2432 const DataLayout &DL;
2433 VScaleVal_match(const DataLayout &DL) : DL(DL) {}
2434
2435 template <typename ITy> bool match(ITy *V) {
2436 if (m_Intrinsic<Intrinsic::vscale>().match(V))
2437 return true;
2438
2439 if (m_PtrToInt(m_OffsetGep(m_Zero(), m_SpecificInt(1))).match(V)) {
2440 Type *PtrTy = cast<Operator>(V)->getOperand(0)->getType();
2441 auto *DerefTy = PtrTy->getPointerElementType();
2442 if (isa<ScalableVectorType>(DerefTy) &&
2443 DL.getTypeAllocSizeInBits(DerefTy).getKnownMinSize() == 8)
2444 return true;
2445 }
2446
2447 return false;
2448 }
2449};
2450
2451inline VScaleVal_match m_VScale(const DataLayout &DL) {
2452 return VScaleVal_match(DL);
2453}
2454
2455template <typename LHS, typename RHS, unsigned Opcode>
2456struct LogicalOp_match {
2457 LHS L;
2458 RHS R;
2459
2460 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
2461
2462 template <typename T> bool match(T *V) {
2463 if (auto *I = dyn_cast<Instruction>(V)) {
2464 if (!I->getType()->isIntOrIntVectorTy(1))
2465 return false;
2466
2467 if (I->getOpcode() == Opcode && L.match(I->getOperand(0)) &&
2468 R.match(I->getOperand(1)))
2469 return true;
2470
2471 if (auto *SI = dyn_cast<SelectInst>(I)) {
2472 if (Opcode == Instruction::And) {
2473 if (const auto *C = dyn_cast<Constant>(SI->getFalseValue()))
2474 if (C->isNullValue() && L.match(SI->getCondition()) &&
2475 R.match(SI->getTrueValue()))
2476 return true;
2477 } else {
2478 assert(Opcode == Instruction::Or)(static_cast <bool> (Opcode == Instruction::Or) ? void (
0) : __assert_fail ("Opcode == Instruction::Or", "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/PatternMatch.h"
, 2478, __extension__ __PRETTY_FUNCTION__))
;
2479 if (const auto *C = dyn_cast<Constant>(SI->getTrueValue()))
2480 if (C->isOneValue() && L.match(SI->getCondition()) &&
2481 R.match(SI->getFalseValue()))
2482 return true;
2483 }
2484 }
2485 }
2486
2487 return false;
2488 }
2489};
2490
2491/// Matches L && R either in the form of L & R or L ? R : false.
2492/// Note that the latter form is poison-blocking.
2493template <typename LHS, typename RHS>
2494inline LogicalOp_match<LHS, RHS, Instruction::And>
2495m_LogicalAnd(const LHS &L, const RHS &R) {
2496 return LogicalOp_match<LHS, RHS, Instruction::And>(L, R);
2497}
2498
2499/// Matches L && R where L and R are arbitrary values.
2500inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
2501
2502/// Matches L || R either in the form of L | R or L ? true : R.
2503/// Note that the latter form is poison-blocking.
2504template <typename LHS, typename RHS>
2505inline LogicalOp_match<LHS, RHS, Instruction::Or>
2506m_LogicalOr(const LHS &L, const RHS &R) {
2507 return LogicalOp_match<LHS, RHS, Instruction::Or>(L, R);
2508}
2509
2510/// Matches L || R where L and R are arbitrary values.
2511inline auto m_LogicalOr() {
2512 return m_LogicalOr(m_Value(), m_Value());
2513}
2514
2515} // end namespace PatternMatch
2516} // end namespace llvm
2517
2518#endif // LLVM_IR_PATTERNMATCH_H

/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/Analysis/AliasAnalysis.h

1//===- llvm/Analysis/AliasAnalysis.h - Alias Analysis Interface -*- 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 the generic AliasAnalysis interface, which is used as the
10// common interface used by all clients of alias analysis information, and
11// implemented by all alias analysis implementations. Mod/Ref information is
12// also captured by this interface.
13//
14// Implementations of this interface must implement the various virtual methods,
15// which automatically provides functionality for the entire suite of client
16// APIs.
17//
18// This API identifies memory regions with the MemoryLocation class. The pointer
19// component specifies the base memory address of the region. The Size specifies
20// the maximum size (in address units) of the memory region, or
21// MemoryLocation::UnknownSize if the size is not known. The TBAA tag
22// identifies the "type" of the memory reference; see the
23// TypeBasedAliasAnalysis class for details.
24//
25// Some non-obvious details include:
26// - Pointers that point to two completely different objects in memory never
27// alias, regardless of the value of the Size component.
28// - NoAlias doesn't imply inequal pointers. The most obvious example of this
29// is two pointers to constant memory. Even if they are equal, constant
30// memory is never stored to, so there will never be any dependencies.
31// In this and other situations, the pointers may be both NoAlias and
32// MustAlias at the same time. The current API can only return one result,
33// though this is rarely a problem in practice.
34//
35//===----------------------------------------------------------------------===//
36
37#ifndef LLVM_ANALYSIS_ALIASANALYSIS_H
38#define LLVM_ANALYSIS_ALIASANALYSIS_H
39
40#include "llvm/ADT/DenseMap.h"
41#include "llvm/ADT/None.h"
42#include "llvm/ADT/Optional.h"
43#include "llvm/ADT/SmallVector.h"
44#include "llvm/Analysis/MemoryLocation.h"
45#include "llvm/IR/PassManager.h"
46#include "llvm/Pass.h"
47#include <cstdint>
48#include <functional>
49#include <memory>
50#include <vector>
51
52namespace llvm {
53
54class AnalysisUsage;
55class AtomicCmpXchgInst;
56class BasicAAResult;
57class BasicBlock;
58class CatchPadInst;
59class CatchReturnInst;
60class DominatorTree;
61class FenceInst;
62class Function;
63class InvokeInst;
64class PreservedAnalyses;
65class TargetLibraryInfo;
66class Value;
67
68/// The possible results of an alias query.
69///
70/// These results are always computed between two MemoryLocation objects as
71/// a query to some alias analysis.
72///
73/// Note that these are unscoped enumerations because we would like to support
74/// implicitly testing a result for the existence of any possible aliasing with
75/// a conversion to bool, but an "enum class" doesn't support this. The
76/// canonical names from the literature are suffixed and unique anyways, and so
77/// they serve as global constants in LLVM for these results.
78///
79/// See docs/AliasAnalysis.html for more information on the specific meanings
80/// of these values.
81class AliasResult {
82private:
83 static const int OffsetBits = 23;
84 static const int AliasBits = 8;
85 static_assert(AliasBits + 1 + OffsetBits <= 32,
86 "AliasResult size is intended to be 4 bytes!");
87
88 unsigned int Alias : AliasBits;
89 unsigned int HasOffset : 1;
90 signed int Offset : OffsetBits;
91
92public:
93 enum Kind : uint8_t {
94 /// The two locations do not alias at all.
95 ///
96 /// This value is arranged to convert to false, while all other values
97 /// convert to true. This allows a boolean context to convert the result to
98 /// a binary flag indicating whether there is the possibility of aliasing.
99 NoAlias = 0,
100 /// The two locations may or may not alias. This is the least precise
101 /// result.
102 MayAlias,
103 /// The two locations alias, but only due to a partial overlap.
104 PartialAlias,
105 /// The two locations precisely alias each other.
106 MustAlias,
107 };
108 static_assert(MustAlias < (1 << AliasBits),
109 "Not enough bit field size for the enum!");
110
111 explicit AliasResult() = delete;
112 constexpr AliasResult(const Kind &Alias)
113 : Alias(Alias), HasOffset(false), Offset(0) {}
114
115 operator Kind() const { return static_cast<Kind>(Alias); }
116
117 constexpr bool hasOffset() const { return HasOffset; }
118 constexpr int32_t getOffset() const {
119 assert(HasOffset && "No offset!")(static_cast <bool> (HasOffset && "No offset!")
? void (0) : __assert_fail ("HasOffset && \"No offset!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/Analysis/AliasAnalysis.h"
, 119, __extension__ __PRETTY_FUNCTION__))
;
120 return Offset;
121 }
122 void setOffset(int32_t NewOffset) {
123 if (isInt<OffsetBits>(NewOffset)) {
124 HasOffset = true;
125 Offset = NewOffset;
126 }
127 }
128
129 /// Helper for processing AliasResult for swapped memory location pairs.
130 void swap(bool DoSwap = true) {
131 if (DoSwap && hasOffset())
132 setOffset(-getOffset());
133 }
134};
135
136static_assert(sizeof(AliasResult) == 4,
137 "AliasResult size is intended to be 4 bytes!");
138
139/// << operator for AliasResult.
140raw_ostream &operator<<(raw_ostream &OS, AliasResult AR);
141
142/// Flags indicating whether a memory access modifies or references memory.
143///
144/// This is no access at all, a modification, a reference, or both
145/// a modification and a reference. These are specifically structured such that
146/// they form a three bit matrix and bit-tests for 'mod' or 'ref' or 'must'
147/// work with any of the possible values.
148enum class ModRefInfo : uint8_t {
149 /// Must is provided for completeness, but no routines will return only
150 /// Must today. See definition of Must below.
151 Must = 0,
152 /// The access may reference the value stored in memory,
153 /// a mustAlias relation was found, and no mayAlias or partialAlias found.
154 MustRef = 1,
155 /// The access may modify the value stored in memory,
156 /// a mustAlias relation was found, and no mayAlias or partialAlias found.
157 MustMod = 2,
158 /// The access may reference, modify or both the value stored in memory,
159 /// a mustAlias relation was found, and no mayAlias or partialAlias found.
160 MustModRef = MustRef | MustMod,
161 /// The access neither references nor modifies the value stored in memory.
162 NoModRef = 4,
163 /// The access may reference the value stored in memory.
164 Ref = NoModRef | MustRef,
165 /// The access may modify the value stored in memory.
166 Mod = NoModRef | MustMod,
167 /// The access may reference and may modify the value stored in memory.
168 ModRef = Ref | Mod,
169
170 /// About Must:
171 /// Must is set in a best effort manner.
172 /// We usually do not try our best to infer Must, instead it is merely
173 /// another piece of "free" information that is presented when available.
174 /// Must set means there was certainly a MustAlias found. For calls,
175 /// where multiple arguments are checked (argmemonly), this translates to
176 /// only MustAlias or NoAlias was found.
177 /// Must is not set for RAR accesses, even if the two locations must
178 /// alias. The reason is that two read accesses translate to an early return
179 /// of NoModRef. An additional alias check to set Must may be
180 /// expensive. Other cases may also not set Must(e.g. callCapturesBefore).
181 /// We refer to Must being *set* when the most significant bit is *cleared*.
182 /// Conversely we *clear* Must information by *setting* the Must bit to 1.
183};
184
185LLVM_NODISCARD[[clang::warn_unused_result]] inline bool isNoModRef(const ModRefInfo MRI) {
186 return (static_cast<int>(MRI) & static_cast<int>(ModRefInfo::MustModRef)) ==
187 static_cast<int>(ModRefInfo::Must);
188}
189LLVM_NODISCARD[[clang::warn_unused_result]] inline bool isModOrRefSet(const ModRefInfo MRI) {
190 return static_cast<int>(MRI) & static_cast<int>(ModRefInfo::MustModRef);
191}
192LLVM_NODISCARD[[clang::warn_unused_result]] inline bool isModAndRefSet(const ModRefInfo MRI) {
193 return (static_cast<int>(MRI) & static_cast<int>(ModRefInfo::MustModRef)) ==
194 static_cast<int>(ModRefInfo::MustModRef);
195}
196LLVM_NODISCARD[[clang::warn_unused_result]] inline bool isModSet(const ModRefInfo MRI) {
197 return static_cast<int>(MRI) & static_cast<int>(ModRefInfo::MustMod);
198}
199LLVM_NODISCARD[[clang::warn_unused_result]] inline bool isRefSet(const ModRefInfo MRI) {
200 return static_cast<int>(MRI) & static_cast<int>(ModRefInfo::MustRef);
201}
202LLVM_NODISCARD[[clang::warn_unused_result]] inline bool isMustSet(const ModRefInfo MRI) {
203 return !(static_cast<int>(MRI) & static_cast<int>(ModRefInfo::NoModRef));
204}
205
206LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo setMod(const ModRefInfo MRI) {
207 return ModRefInfo(static_cast<int>(MRI) |
208 static_cast<int>(ModRefInfo::MustMod));
209}
210LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo setRef(const ModRefInfo MRI) {
211 return ModRefInfo(static_cast<int>(MRI) |
212 static_cast<int>(ModRefInfo::MustRef));
213}
214LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo setMust(const ModRefInfo MRI) {
215 return ModRefInfo(static_cast<int>(MRI) &
216 static_cast<int>(ModRefInfo::MustModRef));
217}
218LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo setModAndRef(const ModRefInfo MRI) {
219 return ModRefInfo(static_cast<int>(MRI) |
220 static_cast<int>(ModRefInfo::MustModRef));
221}
222LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo clearMod(const ModRefInfo MRI) {
223 return ModRefInfo(static_cast<int>(MRI) & static_cast<int>(ModRefInfo::Ref));
224}
225LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo clearRef(const ModRefInfo MRI) {
226 return ModRefInfo(static_cast<int>(MRI) & static_cast<int>(ModRefInfo::Mod));
227}
228LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo clearMust(const ModRefInfo MRI) {
229 return ModRefInfo(static_cast<int>(MRI) |
230 static_cast<int>(ModRefInfo::NoModRef));
231}
232LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo unionModRef(const ModRefInfo MRI1,
233 const ModRefInfo MRI2) {
234 return ModRefInfo(static_cast<int>(MRI1) | static_cast<int>(MRI2));
235}
236LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo intersectModRef(const ModRefInfo MRI1,
237 const ModRefInfo MRI2) {
238 return ModRefInfo(static_cast<int>(MRI1) & static_cast<int>(MRI2));
239}
240
241/// The locations at which a function might access memory.
242///
243/// These are primarily used in conjunction with the \c AccessKind bits to
244/// describe both the nature of access and the locations of access for a
245/// function call.
246enum FunctionModRefLocation {
247 /// Base case is no access to memory.
248 FMRL_Nowhere = 0,
249 /// Access to memory via argument pointers.
250 FMRL_ArgumentPointees = 8,
251 /// Memory that is inaccessible via LLVM IR.
252 FMRL_InaccessibleMem = 16,
253 /// Access to any memory.
254 FMRL_Anywhere = 32 | FMRL_InaccessibleMem | FMRL_ArgumentPointees
255};
256
257/// Summary of how a function affects memory in the program.
258///
259/// Loads from constant globals are not considered memory accesses for this
260/// interface. Also, functions may freely modify stack space local to their
261/// invocation without having to report it through these interfaces.
262enum FunctionModRefBehavior {
263 /// This function does not perform any non-local loads or stores to memory.
264 ///
265 /// This property corresponds to the GCC 'const' attribute.
266 /// This property corresponds to the LLVM IR 'readnone' attribute.
267 /// This property corresponds to the IntrNoMem LLVM intrinsic flag.
268 FMRB_DoesNotAccessMemory =
269 FMRL_Nowhere | static_cast<int>(ModRefInfo::NoModRef),
270
271 /// The only memory references in this function (if it has any) are
272 /// non-volatile loads from objects pointed to by its pointer-typed
273 /// arguments, with arbitrary offsets.
274 ///
275 /// This property corresponds to the combination of the IntrReadMem
276 /// and IntrArgMemOnly LLVM intrinsic flags.
277 FMRB_OnlyReadsArgumentPointees =
278 FMRL_ArgumentPointees | static_cast<int>(ModRefInfo::Ref),
279
280 /// The only memory references in this function (if it has any) are
281 /// non-volatile stores from objects pointed to by its pointer-typed
282 /// arguments, with arbitrary offsets.
283 ///
284 /// This property corresponds to the combination of the IntrWriteMem
285 /// and IntrArgMemOnly LLVM intrinsic flags.
286 FMRB_OnlyWritesArgumentPointees =
287 FMRL_ArgumentPointees | static_cast<int>(ModRefInfo::Mod),
288
289 /// The only memory references in this function (if it has any) are
290 /// non-volatile loads and stores from objects pointed to by its
291 /// pointer-typed arguments, with arbitrary offsets.
292 ///
293 /// This property corresponds to the IntrArgMemOnly LLVM intrinsic flag.
294 FMRB_OnlyAccessesArgumentPointees =
295 FMRL_ArgumentPointees | static_cast<int>(ModRefInfo::ModRef),
296
297 /// The only memory references in this function (if it has any) are
298 /// reads of memory that is otherwise inaccessible via LLVM IR.
299 ///
300 /// This property corresponds to the LLVM IR inaccessiblememonly attribute.
301 FMRB_OnlyReadsInaccessibleMem =
302 FMRL_InaccessibleMem | static_cast<int>(ModRefInfo::Ref),
303
304 /// The only memory references in this function (if it has any) are
305 /// writes to memory that is otherwise inaccessible via LLVM IR.
306 ///
307 /// This property corresponds to the LLVM IR inaccessiblememonly attribute.
308 FMRB_OnlyWritesInaccessibleMem =
309 FMRL_InaccessibleMem | static_cast<int>(ModRefInfo::Mod),
310
311 /// The only memory references in this function (if it has any) are
312 /// references of memory that is otherwise inaccessible via LLVM IR.
313 ///
314 /// This property corresponds to the LLVM IR inaccessiblememonly attribute.
315 FMRB_OnlyAccessesInaccessibleMem =
316 FMRL_InaccessibleMem | static_cast<int>(ModRefInfo::ModRef),
317
318 /// The function may perform non-volatile loads from objects pointed
319 /// to by its pointer-typed arguments, with arbitrary offsets, and
320 /// it may also perform loads of memory that is otherwise
321 /// inaccessible via LLVM IR.
322 ///
323 /// This property corresponds to the LLVM IR
324 /// inaccessiblemem_or_argmemonly attribute.
325 FMRB_OnlyReadsInaccessibleOrArgMem = FMRL_InaccessibleMem |
326 FMRL_ArgumentPointees |
327 static_cast<int>(ModRefInfo::Ref),
328
329 /// The function may perform non-volatile stores to objects pointed
330 /// to by its pointer-typed arguments, with arbitrary offsets, and
331 /// it may also perform stores of memory that is otherwise
332 /// inaccessible via LLVM IR.
333 ///
334 /// This property corresponds to the LLVM IR
335 /// inaccessiblemem_or_argmemonly attribute.
336 FMRB_OnlyWritesInaccessibleOrArgMem = FMRL_InaccessibleMem |
337 FMRL_ArgumentPointees |
338 static_cast<int>(ModRefInfo::Mod),
339
340 /// The function may perform non-volatile loads and stores of objects
341 /// pointed to by its pointer-typed arguments, with arbitrary offsets, and
342 /// it may also perform loads and stores of memory that is otherwise
343 /// inaccessible via LLVM IR.
344 ///
345 /// This property corresponds to the LLVM IR
346 /// inaccessiblemem_or_argmemonly attribute.
347 FMRB_OnlyAccessesInaccessibleOrArgMem = FMRL_InaccessibleMem |
348 FMRL_ArgumentPointees |
349 static_cast<int>(ModRefInfo::ModRef),
350
351 /// This function does not perform any non-local stores or volatile loads,
352 /// but may read from any memory location.
353 ///
354 /// This property corresponds to the GCC 'pure' attribute.
355 /// This property corresponds to the LLVM IR 'readonly' attribute.
356 /// This property corresponds to the IntrReadMem LLVM intrinsic flag.
357 FMRB_OnlyReadsMemory = FMRL_Anywhere | static_cast<int>(ModRefInfo::Ref),
358
359 // This function does not read from memory anywhere, but may write to any
360 // memory location.
361 //
362 // This property corresponds to the LLVM IR 'writeonly' attribute.
363 // This property corresponds to the IntrWriteMem LLVM intrinsic flag.
364 FMRB_OnlyWritesMemory = FMRL_Anywhere | static_cast<int>(ModRefInfo::Mod),
365
366 /// This indicates that the function could not be classified into one of the
367 /// behaviors above.
368 FMRB_UnknownModRefBehavior =
369 FMRL_Anywhere | static_cast<int>(ModRefInfo::ModRef)
370};
371
372// Wrapper method strips bits significant only in FunctionModRefBehavior,
373// to obtain a valid ModRefInfo. The benefit of using the wrapper is that if
374// ModRefInfo enum changes, the wrapper can be updated to & with the new enum
375// entry with all bits set to 1.
376LLVM_NODISCARD[[clang::warn_unused_result]] inline ModRefInfo
377createModRefInfo(const FunctionModRefBehavior FMRB) {
378 return ModRefInfo(FMRB & static_cast<int>(ModRefInfo::ModRef));
379}
380
381/// Reduced version of MemoryLocation that only stores a pointer and size.
382/// Used for caching AATags independent BasicAA results.
383struct AACacheLoc {
384 const Value *Ptr;
385 LocationSize Size;
386};
387
388template <> struct DenseMapInfo<AACacheLoc> {
389 static inline AACacheLoc getEmptyKey() {
390 return {DenseMapInfo<const Value *>::getEmptyKey(),
391 DenseMapInfo<LocationSize>::getEmptyKey()};
392 }
393 static inline AACacheLoc getTombstoneKey() {
394 return {DenseMapInfo<const Value *>::getTombstoneKey(),
395 DenseMapInfo<LocationSize>::getTombstoneKey()};
396 }
397 static unsigned getHashValue(const AACacheLoc &Val) {
398 return DenseMapInfo<const Value *>::getHashValue(Val.Ptr) ^
399 DenseMapInfo<LocationSize>::getHashValue(Val.Size);
400 }
401 static bool isEqual(const AACacheLoc &LHS, const AACacheLoc &RHS) {
402 return LHS.Ptr == RHS.Ptr && LHS.Size == RHS.Size;
403 }
404};
405
406/// This class stores info we want to provide to or retain within an alias
407/// query. By default, the root query is stateless and starts with a freshly
408/// constructed info object. Specific alias analyses can use this query info to
409/// store per-query state that is important for recursive or nested queries to
410/// avoid recomputing. To enable preserving this state across multiple queries
411/// where safe (due to the IR not changing), use a `BatchAAResults` wrapper.
412/// The information stored in an `AAQueryInfo` is currently limitted to the
413/// caches used by BasicAA, but can further be extended to fit other AA needs.
414class AAQueryInfo {
415public:
416 using LocPair = std::pair<AACacheLoc, AACacheLoc>;
417 struct CacheEntry {
418 AliasResult Result;
419 /// Number of times a NoAlias assumption has been used.
420 /// 0 for assumptions that have not been used, -1 for definitive results.
421 int NumAssumptionUses;
422 /// Whether this is a definitive (non-assumption) result.
423 bool isDefinitive() const { return NumAssumptionUses < 0; }
424 };
425 using AliasCacheT = SmallDenseMap<LocPair, CacheEntry, 8>;
426 AliasCacheT AliasCache;
427
428 using IsCapturedCacheT = SmallDenseMap<const Value *, bool, 8>;
429 IsCapturedCacheT IsCapturedCache;
430
431 /// Query depth used to distinguish recursive queries.
432 unsigned Depth = 0;
433
434 /// How many active NoAlias assumption uses there are.
435 int NumAssumptionUses = 0;
436
437 /// Location pairs for which an assumption based result is currently stored.
438 /// Used to remove all potentially incorrect results from the cache if an
439 /// assumption is disproven.
440 SmallVector<AAQueryInfo::LocPair, 4> AssumptionBasedResults;
441
442 AAQueryInfo() : AliasCache(), IsCapturedCache() {}
443
444 /// Create a new AAQueryInfo based on this one, but with the cache cleared.
445 /// This is used for recursive queries across phis, where cache results may
446 /// not be valid.
447 AAQueryInfo withEmptyCache() {
448 AAQueryInfo NewAAQI;
449 NewAAQI.Depth = Depth;
450 return NewAAQI;
451 }
452};
453
454class BatchAAResults;
455
456class AAResults {
457public:
458 // Make these results default constructable and movable. We have to spell
459 // these out because MSVC won't synthesize them.
460 AAResults(const TargetLibraryInfo &TLI) : TLI(TLI) {}
461 AAResults(AAResults &&Arg);
462 ~AAResults();
463
464 /// Register a specific AA result.
465 template <typename AAResultT> void addAAResult(AAResultT &AAResult) {
466 // FIXME: We should use a much lighter weight system than the usual
467 // polymorphic pattern because we don't own AAResult. It should
468 // ideally involve two pointers and no separate allocation.
469 AAs.emplace_back(new Model<AAResultT>(AAResult, *this));
470 }
471
472 /// Register a function analysis ID that the results aggregation depends on.
473 ///
474 /// This is used in the new pass manager to implement the invalidation logic
475 /// where we must invalidate the results aggregation if any of our component
476 /// analyses become invalid.
477 void addAADependencyID(AnalysisKey *ID) { AADeps.push_back(ID); }
478
479 /// Handle invalidation events in the new pass manager.
480 ///
481 /// The aggregation is invalidated if any of the underlying analyses is
482 /// invalidated.
483 bool invalidate(Function &F, const PreservedAnalyses &PA,
484 FunctionAnalysisManager::Invalidator &Inv);
485
486 //===--------------------------------------------------------------------===//
487 /// \name Alias Queries
488 /// @{
489
490 /// The main low level interface to the alias analysis implementation.
491 /// Returns an AliasResult indicating whether the two pointers are aliased to
492 /// each other. This is the interface that must be implemented by specific
493 /// alias analysis implementations.
494 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB);
495
496 /// A convenience wrapper around the primary \c alias interface.
497 AliasResult alias(const Value *V1, LocationSize V1Size, const Value *V2,
498 LocationSize V2Size) {
499 return alias(MemoryLocation(V1, V1Size), MemoryLocation(V2, V2Size));
500 }
501
502 /// A convenience wrapper around the primary \c alias interface.
503 AliasResult alias(const Value *V1, const Value *V2) {
504 return alias(MemoryLocation::getBeforeOrAfter(V1),
505 MemoryLocation::getBeforeOrAfter(V2));
506 }
507
508 /// A trivial helper function to check to see if the specified pointers are
509 /// no-alias.
510 bool isNoAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) {
511 return alias(LocA, LocB) == AliasResult::NoAlias;
512 }
513
514 /// A convenience wrapper around the \c isNoAlias helper interface.
515 bool isNoAlias(const Value *V1, LocationSize V1Size, const Value *V2,
516 LocationSize V2Size) {
517 return isNoAlias(MemoryLocation(V1, V1Size), MemoryLocation(V2, V2Size));
518 }
519
520 /// A convenience wrapper around the \c isNoAlias helper interface.
521 bool isNoAlias(const Value *V1, const Value *V2) {
522 return isNoAlias(MemoryLocation::getBeforeOrAfter(V1),
523 MemoryLocation::getBeforeOrAfter(V2));
524 }
525
526 /// A trivial helper function to check to see if the specified pointers are
527 /// must-alias.
528 bool isMustAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) {
529 return alias(LocA, LocB) == AliasResult::MustAlias;
530 }
531
532 /// A convenience wrapper around the \c isMustAlias helper interface.
533 bool isMustAlias(const Value *V1, const Value *V2) {
534 return alias(V1, LocationSize::precise(1), V2, LocationSize::precise(1)) ==
535 AliasResult::MustAlias;
536 }
537
538 /// Checks whether the given location points to constant memory, or if
539 /// \p OrLocal is true whether it points to a local alloca.
540 bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal = false);
541
542 /// A convenience wrapper around the primary \c pointsToConstantMemory
543 /// interface.
544 bool pointsToConstantMemory(const Value *P, bool OrLocal = false) {
545 return pointsToConstantMemory(MemoryLocation::getBeforeOrAfter(P), OrLocal);
546 }
547
548 /// @}
549 //===--------------------------------------------------------------------===//
550 /// \name Simple mod/ref information
551 /// @{
552
553 /// Get the ModRef info associated with a pointer argument of a call. The
554 /// result's bits are set to indicate the allowed aliasing ModRef kinds. Note
555 /// that these bits do not necessarily account for the overall behavior of
556 /// the function, but rather only provide additional per-argument
557 /// information. This never sets ModRefInfo::Must.
558 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx);
559
560 /// Return the behavior of the given call site.
561 FunctionModRefBehavior getModRefBehavior(const CallBase *Call);
562
563 /// Return the behavior when calling the given function.
564 FunctionModRefBehavior getModRefBehavior(const Function *F);
565
566 /// Checks if the specified call is known to never read or write memory.
567 ///
568 /// Note that if the call only reads from known-constant memory, it is also
569 /// legal to return true. Also, calls that unwind the stack are legal for
570 /// this predicate.
571 ///
572 /// Many optimizations (such as CSE and LICM) can be performed on such calls
573 /// without worrying about aliasing properties, and many calls have this
574 /// property (e.g. calls to 'sin' and 'cos').
575 ///
576 /// This property corresponds to the GCC 'const' attribute.
577 bool doesNotAccessMemory(const CallBase *Call) {
578 return getModRefBehavior(Call) == FMRB_DoesNotAccessMemory;
579 }
580
581 /// Checks if the specified function is known to never read or write memory.
582 ///
583 /// Note that if the function only reads from known-constant memory, it is
584 /// also legal to return true. Also, function that unwind the stack are legal
585 /// for this predicate.
586 ///
587 /// Many optimizations (such as CSE and LICM) can be performed on such calls
588 /// to such functions without worrying about aliasing properties, and many
589 /// functions have this property (e.g. 'sin' and 'cos').
590 ///
591 /// This property corresponds to the GCC 'const' attribute.
592 bool doesNotAccessMemory(const Function *F) {
593 return getModRefBehavior(F) == FMRB_DoesNotAccessMemory;
594 }
595
596 /// Checks if the specified call is known to only read from non-volatile
597 /// memory (or not access memory at all).
598 ///
599 /// Calls that unwind the stack are legal for this predicate.
600 ///
601 /// This property allows many common optimizations to be performed in the
602 /// absence of interfering store instructions, such as CSE of strlen calls.
603 ///
604 /// This property corresponds to the GCC 'pure' attribute.
605 bool onlyReadsMemory(const CallBase *Call) {
606 return onlyReadsMemory(getModRefBehavior(Call));
607 }
608
609 /// Checks if the specified function is known to only read from non-volatile
610 /// memory (or not access memory at all).
611 ///
612 /// Functions that unwind the stack are legal for this predicate.
613 ///
614 /// This property allows many common optimizations to be performed in the
615 /// absence of interfering store instructions, such as CSE of strlen calls.
616 ///
617 /// This property corresponds to the GCC 'pure' attribute.
618 bool onlyReadsMemory(const Function *F) {
619 return onlyReadsMemory(getModRefBehavior(F));
620 }
621
622 /// Checks if functions with the specified behavior are known to only read
623 /// from non-volatile memory (or not access memory at all).
624 static bool onlyReadsMemory(FunctionModRefBehavior MRB) {
625 return !isModSet(createModRefInfo(MRB));
61
Assuming the condition is true
62
Returning the value 1, which participates in a condition later
626 }
627
628 /// Checks if functions with the specified behavior are known to only write
629 /// memory (or not access memory at all).
630 static bool doesNotReadMemory(FunctionModRefBehavior MRB) {
631 return !isRefSet(createModRefInfo(MRB));
632 }
633
634 /// Checks if functions with the specified behavior are known to read and
635 /// write at most from objects pointed to by their pointer-typed arguments
636 /// (with arbitrary offsets).
637 static bool onlyAccessesArgPointees(FunctionModRefBehavior MRB) {
638 return !((unsigned)MRB & FMRL_Anywhere & ~FMRL_ArgumentPointees);
66
Assuming the condition is true
67
Returning the value 1, which participates in a condition later
639 }
640
641 /// Checks if functions with the specified behavior are known to potentially
642 /// read or write from objects pointed to be their pointer-typed arguments
643 /// (with arbitrary offsets).
644 static bool doesAccessArgPointees(FunctionModRefBehavior MRB) {
645 return isModOrRefSet(createModRefInfo(MRB)) &&
646 ((unsigned)MRB & FMRL_ArgumentPointees);
647 }
648
649 /// Checks if functions with the specified behavior are known to read and
650 /// write at most from memory that is inaccessible from LLVM IR.
651 static bool onlyAccessesInaccessibleMem(FunctionModRefBehavior MRB) {
652 return !((unsigned)MRB & FMRL_Anywhere & ~FMRL_InaccessibleMem);
653 }
654
655 /// Checks if functions with the specified behavior are known to potentially
656 /// read or write from memory that is inaccessible from LLVM IR.
657 static bool doesAccessInaccessibleMem(FunctionModRefBehavior MRB) {
658 return isModOrRefSet(createModRefInfo(MRB)) &&
659 ((unsigned)MRB & FMRL_InaccessibleMem);
660 }
661
662 /// Checks if functions with the specified behavior are known to read and
663 /// write at most from memory that is inaccessible from LLVM IR or objects
664 /// pointed to by their pointer-typed arguments (with arbitrary offsets).
665 static bool onlyAccessesInaccessibleOrArgMem(FunctionModRefBehavior MRB) {
666 return !((unsigned)MRB & FMRL_Anywhere &
667 ~(FMRL_InaccessibleMem | FMRL_ArgumentPointees));
668 }
669
670 /// getModRefInfo (for call sites) - Return information about whether
671 /// a particular call site modifies or reads the specified memory location.
672 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc);
673
674 /// getModRefInfo (for call sites) - A convenience wrapper.
675 ModRefInfo getModRefInfo(const CallBase *Call, const Value *P,
676 LocationSize Size) {
677 return getModRefInfo(Call, MemoryLocation(P, Size));
678 }
679
680 /// getModRefInfo (for loads) - Return information about whether
681 /// a particular load modifies or reads the specified memory location.
682 ModRefInfo getModRefInfo(const LoadInst *L, const MemoryLocation &Loc);
683
684 /// getModRefInfo (for loads) - A convenience wrapper.
685 ModRefInfo getModRefInfo(const LoadInst *L, const Value *P,
686 LocationSize Size) {
687 return getModRefInfo(L, MemoryLocation(P, Size));
688 }
689
690 /// getModRefInfo (for stores) - Return information about whether
691 /// a particular store modifies or reads the specified memory location.
692 ModRefInfo getModRefInfo(const StoreInst *S, const MemoryLocation &Loc);
693
694 /// getModRefInfo (for stores) - A convenience wrapper.
695 ModRefInfo getModRefInfo(const StoreInst *S, const Value *P,
696 LocationSize Size) {
697 return getModRefInfo(S, MemoryLocation(P, Size));
698 }
699
700 /// getModRefInfo (for fences) - Return information about whether
701 /// a particular store modifies or reads the specified memory location.
702 ModRefInfo getModRefInfo(const FenceInst *S, const MemoryLocation &Loc);
703
704 /// getModRefInfo (for fences) - A convenience wrapper.
705 ModRefInfo getModRefInfo(const FenceInst *S, const Value *P,
706 LocationSize Size) {
707 return getModRefInfo(S, MemoryLocation(P, Size));
708 }
709
710 /// getModRefInfo (for cmpxchges) - Return information about whether
711 /// a particular cmpxchg modifies or reads the specified memory location.
712 ModRefInfo getModRefInfo(const AtomicCmpXchgInst *CX,
713 const MemoryLocation &Loc);
714
715 /// getModRefInfo (for cmpxchges) - A convenience wrapper.
716 ModRefInfo getModRefInfo(const AtomicCmpXchgInst *CX, const Value *P,
717 LocationSize Size) {
718 return getModRefInfo(CX, MemoryLocation(P, Size));
719 }
720
721 /// getModRefInfo (for atomicrmws) - Return information about whether
722 /// a particular atomicrmw modifies or reads the specified memory location.
723 ModRefInfo getModRefInfo(const AtomicRMWInst *RMW, const MemoryLocation &Loc);
724
725 /// getModRefInfo (for atomicrmws) - A convenience wrapper.
726 ModRefInfo getModRefInfo(const AtomicRMWInst *RMW, const Value *P,
727 LocationSize Size) {
728 return getModRefInfo(RMW, MemoryLocation(P, Size));
729 }
730
731 /// getModRefInfo (for va_args) - Return information about whether
732 /// a particular va_arg modifies or reads the specified memory location.
733 ModRefInfo getModRefInfo(const VAArgInst *I, const MemoryLocation &Loc);
734
735 /// getModRefInfo (for va_args) - A convenience wrapper.
736 ModRefInfo getModRefInfo(const VAArgInst *I, const Value *P,
737 LocationSize Size) {
738 return getModRefInfo(I, MemoryLocation(P, Size));
739 }
740
741 /// getModRefInfo (for catchpads) - Return information about whether
742 /// a particular catchpad modifies or reads the specified memory location.
743 ModRefInfo getModRefInfo(const CatchPadInst *I, const MemoryLocation &Loc);
744
745 /// getModRefInfo (for catchpads) - A convenience wrapper.
746 ModRefInfo getModRefInfo(const CatchPadInst *I, const Value *P,
747 LocationSize Size) {
748 return getModRefInfo(I, MemoryLocation(P, Size));
749 }
750
751 /// getModRefInfo (for catchrets) - Return information about whether
752 /// a particular catchret modifies or reads the specified memory location.
753 ModRefInfo getModRefInfo(const CatchReturnInst *I, const MemoryLocation &Loc);
754
755 /// getModRefInfo (for catchrets) - A convenience wrapper.
756 ModRefInfo getModRefInfo(const CatchReturnInst *I, const Value *P,
757 LocationSize Size) {
758 return getModRefInfo(I, MemoryLocation(P, Size));
759 }
760
761 /// Check whether or not an instruction may read or write the optionally
762 /// specified memory location.
763 ///
764 ///
765 /// An instruction that doesn't read or write memory may be trivially LICM'd
766 /// for example.
767 ///
768 /// For function calls, this delegates to the alias-analysis specific
769 /// call-site mod-ref behavior queries. Otherwise it delegates to the specific
770 /// helpers above.
771 ModRefInfo getModRefInfo(const Instruction *I,
772 const Optional<MemoryLocation> &OptLoc) {
773 AAQueryInfo AAQIP;
774 return getModRefInfo(I, OptLoc, AAQIP);
775 }
776
777 /// A convenience wrapper for constructing the memory location.
778 ModRefInfo getModRefInfo(const Instruction *I, const Value *P,
779 LocationSize Size) {
780 return getModRefInfo(I, MemoryLocation(P, Size));
781 }
782
783 /// Return information about whether a call and an instruction may refer to
784 /// the same memory locations.
785 ModRefInfo getModRefInfo(Instruction *I, const CallBase *Call);
786
787 /// Return information about whether two call sites may refer to the same set
788 /// of memory locations. See the AA documentation for details:
789 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
790 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2);
791
792 /// Return information about whether a particular call site modifies
793 /// or reads the specified memory location \p MemLoc before instruction \p I
794 /// in a BasicBlock.
795 /// Early exits in callCapturesBefore may lead to ModRefInfo::Must not being
796 /// set.
797 ModRefInfo callCapturesBefore(const Instruction *I,
798 const MemoryLocation &MemLoc, DominatorTree *DT);
799
800 /// A convenience wrapper to synthesize a memory location.
801 ModRefInfo callCapturesBefore(const Instruction *I, const Value *P,
802 LocationSize Size, DominatorTree *DT) {
803 return callCapturesBefore(I, MemoryLocation(P, Size), DT);
804 }
805
806 /// @}
807 //===--------------------------------------------------------------------===//
808 /// \name Higher level methods for querying mod/ref information.
809 /// @{
810
811 /// Check if it is possible for execution of the specified basic block to
812 /// modify the location Loc.
813 bool canBasicBlockModify(const BasicBlock &BB, const MemoryLocation &Loc);
814
815 /// A convenience wrapper synthesizing a memory location.
816 bool canBasicBlockModify(const BasicBlock &BB, const Value *P,
817 LocationSize Size) {
818 return canBasicBlockModify(BB, MemoryLocation(P, Size));
819 }
820
821 /// Check if it is possible for the execution of the specified instructions
822 /// to mod\ref (according to the mode) the location Loc.
823 ///
824 /// The instructions to consider are all of the instructions in the range of
825 /// [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block.
826 bool canInstructionRangeModRef(const Instruction &I1, const Instruction &I2,
827 const MemoryLocation &Loc,
828 const ModRefInfo Mode);
829
830 /// A convenience wrapper synthesizing a memory location.
831 bool canInstructionRangeModRef(const Instruction &I1, const Instruction &I2,
832 const Value *Ptr, LocationSize Size,
833 const ModRefInfo Mode) {
834 return canInstructionRangeModRef(I1, I2, MemoryLocation(Ptr, Size), Mode);
835 }
836
837private:
838 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
839 AAQueryInfo &AAQI);
840 bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI,
841 bool OrLocal = false);
842 ModRefInfo getModRefInfo(Instruction *I, const CallBase *Call2,
843 AAQueryInfo &AAQIP);
844 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc,
845 AAQueryInfo &AAQI);
846 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2,
847 AAQueryInfo &AAQI);
848 ModRefInfo getModRefInfo(const VAArgInst *V, const MemoryLocation &Loc,
849 AAQueryInfo &AAQI);
850 ModRefInfo getModRefInfo(const LoadInst *L, const MemoryLocation &Loc,
851 AAQueryInfo &AAQI);
852 ModRefInfo getModRefInfo(const StoreInst *S, const MemoryLocation &Loc,
853 AAQueryInfo &AAQI);
854 ModRefInfo getModRefInfo(const FenceInst *S, const MemoryLocation &Loc,
855 AAQueryInfo &AAQI);
856 ModRefInfo getModRefInfo(const AtomicCmpXchgInst *CX,
857 const MemoryLocation &Loc, AAQueryInfo &AAQI);
858 ModRefInfo getModRefInfo(const AtomicRMWInst *RMW, const MemoryLocation &Loc,
859 AAQueryInfo &AAQI);
860 ModRefInfo getModRefInfo(const CatchPadInst *I, const MemoryLocation &Loc,
861 AAQueryInfo &AAQI);
862 ModRefInfo getModRefInfo(const CatchReturnInst *I, const MemoryLocation &Loc,
863 AAQueryInfo &AAQI);
864 ModRefInfo getModRefInfo(const Instruction *I,
865 const Optional<MemoryLocation> &OptLoc,
866 AAQueryInfo &AAQIP);
867
868 class Concept;
869
870 template <typename T> class Model;
871
872 template <typename T> friend class AAResultBase;
873
874 const TargetLibraryInfo &TLI;
875
876 std::vector<std::unique_ptr<Concept>> AAs;
877
878 std::vector<AnalysisKey *> AADeps;
879
880 friend class BatchAAResults;
881};
882
883/// This class is a wrapper over an AAResults, and it is intended to be used
884/// only when there are no IR changes inbetween queries. BatchAAResults is
885/// reusing the same `AAQueryInfo` to preserve the state across queries,
886/// esentially making AA work in "batch mode". The internal state cannot be
887/// cleared, so to go "out-of-batch-mode", the user must either use AAResults,
888/// or create a new BatchAAResults.
889class BatchAAResults {
890 AAResults &AA;
891 AAQueryInfo AAQI;
892
893public:
894 BatchAAResults(AAResults &AAR) : AA(AAR), AAQI() {}
895 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB) {
896 return AA.alias(LocA, LocB, AAQI);
897 }
898 bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal = false) {
899 return AA.pointsToConstantMemory(Loc, AAQI, OrLocal);
900 }
901 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc) {
902 return AA.getModRefInfo(Call, Loc, AAQI);
903 }
904 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2) {
905 return AA.getModRefInfo(Call1, Call2, AAQI);
906 }
907 ModRefInfo getModRefInfo(const Instruction *I,
908 const Optional<MemoryLocation> &OptLoc) {
909 return AA.getModRefInfo(I, OptLoc, AAQI);
910 }
911 ModRefInfo getModRefInfo(Instruction *I, const CallBase *Call2) {
912 return AA.getModRefInfo(I, Call2, AAQI);
913 }
914 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) {
915 return AA.getArgModRefInfo(Call, ArgIdx);
916 }
917 FunctionModRefBehavior getModRefBehavior(const CallBase *Call) {
918 return AA.getModRefBehavior(Call);
919 }
920 bool isMustAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) {
921 return alias(LocA, LocB) == AliasResult::MustAlias;
922 }
923 bool isMustAlias(const Value *V1, const Value *V2) {
924 return alias(MemoryLocation(V1, LocationSize::precise(1)),
925 MemoryLocation(V2, LocationSize::precise(1))) ==
926 AliasResult::MustAlias;
927 }
928};
929
930/// Temporary typedef for legacy code that uses a generic \c AliasAnalysis
931/// pointer or reference.
932using AliasAnalysis = AAResults;
933
934/// A private abstract base class describing the concept of an individual alias
935/// analysis implementation.
936///
937/// This interface is implemented by any \c Model instantiation. It is also the
938/// interface which a type used to instantiate the model must provide.
939///
940/// All of these methods model methods by the same name in the \c
941/// AAResults class. Only differences and specifics to how the
942/// implementations are called are documented here.
943class AAResults::Concept {
944public:
945 virtual ~Concept() = 0;
946
947 /// An update API used internally by the AAResults to provide
948 /// a handle back to the top level aggregation.
949 virtual void setAAResults(AAResults *NewAAR) = 0;
950
951 //===--------------------------------------------------------------------===//
952 /// \name Alias Queries
953 /// @{
954
955 /// The main low level interface to the alias analysis implementation.
956 /// Returns an AliasResult indicating whether the two pointers are aliased to
957 /// each other. This is the interface that must be implemented by specific
958 /// alias analysis implementations.
959 virtual AliasResult alias(const MemoryLocation &LocA,
960 const MemoryLocation &LocB, AAQueryInfo &AAQI) = 0;
961
962 /// Checks whether the given location points to constant memory, or if
963 /// \p OrLocal is true whether it points to a local alloca.
964 virtual bool pointsToConstantMemory(const MemoryLocation &Loc,
965 AAQueryInfo &AAQI, bool OrLocal) = 0;
966
967 /// @}
968 //===--------------------------------------------------------------------===//
969 /// \name Simple mod/ref information
970 /// @{
971
972 /// Get the ModRef info associated with a pointer argument of a callsite. The
973 /// result's bits are set to indicate the allowed aliasing ModRef kinds. Note
974 /// that these bits do not necessarily account for the overall behavior of
975 /// the function, but rather only provide additional per-argument
976 /// information.
977 virtual ModRefInfo getArgModRefInfo(const CallBase *Call,
978 unsigned ArgIdx) = 0;
979
980 /// Return the behavior of the given call site.
981 virtual FunctionModRefBehavior getModRefBehavior(const CallBase *Call) = 0;
982
983 /// Return the behavior when calling the given function.
984 virtual FunctionModRefBehavior getModRefBehavior(const Function *F) = 0;
985
986 /// getModRefInfo (for call sites) - Return information about whether
987 /// a particular call site modifies or reads the specified memory location.
988 virtual ModRefInfo getModRefInfo(const CallBase *Call,
989 const MemoryLocation &Loc,
990 AAQueryInfo &AAQI) = 0;
991
992 /// Return information about whether two call sites may refer to the same set
993 /// of memory locations. See the AA documentation for details:
994 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
995 virtual ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2,
996 AAQueryInfo &AAQI) = 0;
997
998 /// @}
999};
1000
1001/// A private class template which derives from \c Concept and wraps some other
1002/// type.
1003///
1004/// This models the concept by directly forwarding each interface point to the
1005/// wrapped type which must implement a compatible interface. This provides
1006/// a type erased binding.
1007template <typename AAResultT> class AAResults::Model final : public Concept {
1008 AAResultT &Result;
1009
1010public:
1011 explicit Model(AAResultT &Result, AAResults &AAR) : Result(Result) {
1012 Result.setAAResults(&AAR);
1013 }
1014 ~Model() override = default;
1015
1016 void setAAResults(AAResults *NewAAR) override { Result.setAAResults(NewAAR); }
1017
1018 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
1019 AAQueryInfo &AAQI) override {
1020 return Result.alias(LocA, LocB, AAQI);
1021 }
1022
1023 bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI,
1024 bool OrLocal) override {
1025 return Result.pointsToConstantMemory(Loc, AAQI, OrLocal);
1026 }
1027
1028 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) override {
1029 return Result.getArgModRefInfo(Call, ArgIdx);
1030 }
1031
1032 FunctionModRefBehavior getModRefBehavior(const CallBase *Call) override {
1033 return Result.getModRefBehavior(Call);
1034 }
1035
1036 FunctionModRefBehavior getModRefBehavior(const Function *F) override {
1037 return Result.getModRefBehavior(F);
1038 }
1039
1040 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc,
1041 AAQueryInfo &AAQI) override {
1042 return Result.getModRefInfo(Call, Loc, AAQI);
1043 }
1044
1045 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2,
1046 AAQueryInfo &AAQI) override {
1047 return Result.getModRefInfo(Call1, Call2, AAQI);
1048 }
1049};
1050
1051/// A CRTP-driven "mixin" base class to help implement the function alias
1052/// analysis results concept.
1053///
1054/// Because of the nature of many alias analysis implementations, they often
1055/// only implement a subset of the interface. This base class will attempt to
1056/// implement the remaining portions of the interface in terms of simpler forms
1057/// of the interface where possible, and otherwise provide conservatively
1058/// correct fallback implementations.
1059///
1060/// Implementors of an alias analysis should derive from this CRTP, and then
1061/// override specific methods that they wish to customize. There is no need to
1062/// use virtual anywhere, the CRTP base class does static dispatch to the
1063/// derived type passed into it.
1064template <typename DerivedT> class AAResultBase {
1065 // Expose some parts of the interface only to the AAResults::Model
1066 // for wrapping. Specifically, this allows the model to call our
1067 // setAAResults method without exposing it as a fully public API.
1068 friend class AAResults::Model<DerivedT>;
1069
1070 /// A pointer to the AAResults object that this AAResult is
1071 /// aggregated within. May be null if not aggregated.
1072 AAResults *AAR = nullptr;
1073
1074 /// Helper to dispatch calls back through the derived type.
1075 DerivedT &derived() { return static_cast<DerivedT &>(*this); }
1076
1077 /// A setter for the AAResults pointer, which is used to satisfy the
1078 /// AAResults::Model contract.
1079 void setAAResults(AAResults *NewAAR) { AAR = NewAAR; }
1080
1081protected:
1082 /// This proxy class models a common pattern where we delegate to either the
1083 /// top-level \c AAResults aggregation if one is registered, or to the
1084 /// current result if none are registered.
1085 class AAResultsProxy {
1086 AAResults *AAR;
1087 DerivedT &CurrentResult;
1088
1089 public:
1090 AAResultsProxy(AAResults *AAR, DerivedT &CurrentResult)
1091 : AAR(AAR), CurrentResult(CurrentResult) {}
1092
1093 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
1094 AAQueryInfo &AAQI) {
1095 return AAR ? AAR->alias(LocA, LocB, AAQI)
1096 : CurrentResult.alias(LocA, LocB, AAQI);
1097 }
1098
1099 bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI,
1100 bool OrLocal) {
1101 return AAR ? AAR->pointsToConstantMemory(Loc, AAQI, OrLocal)
1102 : CurrentResult.pointsToConstantMemory(Loc, AAQI, OrLocal);
1103 }
1104
1105 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) {
1106 return AAR ? AAR->getArgModRefInfo(Call, ArgIdx)
1107 : CurrentResult.getArgModRefInfo(Call, ArgIdx);
1108 }
1109
1110 FunctionModRefBehavior getModRefBehavior(const CallBase *Call) {
1111 return AAR ? AAR->getModRefBehavior(Call)
1112 : CurrentResult.getModRefBehavior(Call);
1113 }
1114
1115 FunctionModRefBehavior getModRefBehavior(const Function *F) {
1116 return AAR ? AAR->getModRefBehavior(F) : CurrentResult.getModRefBehavior(F);
1117 }
1118
1119 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc,
1120 AAQueryInfo &AAQI) {
1121 return AAR ? AAR->getModRefInfo(Call, Loc, AAQI)
1122 : CurrentResult.getModRefInfo(Call, Loc, AAQI);
1123 }
1124
1125 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2,
1126 AAQueryInfo &AAQI) {
1127 return AAR ? AAR->getModRefInfo(Call1, Call2, AAQI)
1128 : CurrentResult.getModRefInfo(Call1, Call2, AAQI);
1129 }
1130 };
1131
1132 explicit AAResultBase() = default;
1133
1134 // Provide all the copy and move constructors so that derived types aren't
1135 // constrained.
1136 AAResultBase(const AAResultBase &Arg) {}
1137 AAResultBase(AAResultBase &&Arg) {}
1138
1139 /// Get a proxy for the best AA result set to query at this time.
1140 ///
1141 /// When this result is part of a larger aggregation, this will proxy to that
1142 /// aggregation. When this result is used in isolation, it will just delegate
1143 /// back to the derived class's implementation.
1144 ///
1145 /// Note that callers of this need to take considerable care to not cause
1146 /// performance problems when they use this routine, in the case of a large
1147 /// number of alias analyses being aggregated, it can be expensive to walk
1148 /// back across the chain.
1149 AAResultsProxy getBestAAResults() { return AAResultsProxy(AAR, derived()); }
1150
1151public:
1152 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
1153 AAQueryInfo &AAQI) {
1154 return AliasResult::MayAlias;
1155 }
1156
1157 bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI,
1158 bool OrLocal) {
1159 return false;
1160 }
1161
1162 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) {
1163 return ModRefInfo::ModRef;
1164 }
1165
1166 FunctionModRefBehavior getModRefBehavior(const CallBase *Call) {
1167 return FMRB_UnknownModRefBehavior;
1168 }
1169
1170 FunctionModRefBehavior getModRefBehavior(const Function *F) {
1171 return FMRB_UnknownModRefBehavior;
1172 }
1173
1174 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc,
1175 AAQueryInfo &AAQI) {
1176 return ModRefInfo::ModRef;
1177 }
1178
1179 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2,
1180 AAQueryInfo &AAQI) {
1181 return ModRefInfo::ModRef;
1182 }
1183};
1184
1185/// Return true if this pointer is returned by a noalias function.
1186bool isNoAliasCall(const Value *V);
1187
1188/// Return true if this pointer refers to a distinct and identifiable object.
1189/// This returns true for:
1190/// Global Variables and Functions (but not Global Aliases)
1191/// Allocas
1192/// ByVal and NoAlias Arguments
1193/// NoAlias returns (e.g. calls to malloc)
1194///
1195bool isIdentifiedObject(const Value *V);
1196
1197/// Return true if V is umabigously identified at the function-level.
1198/// Different IdentifiedFunctionLocals can't alias.
1199/// Further, an IdentifiedFunctionLocal can not alias with any function
1200/// arguments other than itself, which is not necessarily true for
1201/// IdentifiedObjects.
1202bool isIdentifiedFunctionLocal(const Value *V);
1203
1204/// A manager for alias analyses.
1205///
1206/// This class can have analyses registered with it and when run, it will run
1207/// all of them and aggregate their results into single AA results interface
1208/// that dispatches across all of the alias analysis results available.
1209///
1210/// Note that the order in which analyses are registered is very significant.
1211/// That is the order in which the results will be aggregated and queried.
1212///
1213/// This manager effectively wraps the AnalysisManager for registering alias
1214/// analyses. When you register your alias analysis with this manager, it will
1215/// ensure the analysis itself is registered with its AnalysisManager.
1216///
1217/// The result of this analysis is only invalidated if one of the particular
1218/// aggregated AA results end up being invalidated. This removes the need to
1219/// explicitly preserve the results of `AAManager`. Note that analyses should no
1220/// longer be registered once the `AAManager` is run.
1221class AAManager : public AnalysisInfoMixin<AAManager> {
1222public:
1223 using Result = AAResults;
1224
1225 /// Register a specific AA result.
1226 template <typename AnalysisT> void registerFunctionAnalysis() {
1227 ResultGetters.push_back(&getFunctionAAResultImpl<AnalysisT>);
1228 }
1229
1230 /// Register a specific AA result.
1231 template <typename AnalysisT> void registerModuleAnalysis() {
1232 ResultGetters.push_back(&getModuleAAResultImpl<AnalysisT>);
1233 }
1234
1235 Result run(Function &F, FunctionAnalysisManager &AM);
1236
1237private:
1238 friend AnalysisInfoMixin<AAManager>;
1239
1240 static AnalysisKey Key;
1241
1242 SmallVector<void (*)(Function &F, FunctionAnalysisManager &AM,
1243 AAResults &AAResults),
1244 4> ResultGetters;
1245
1246 template <typename AnalysisT>
1247 static void getFunctionAAResultImpl(Function &F,
1248 FunctionAnalysisManager &AM,
1249 AAResults &AAResults) {
1250 AAResults.addAAResult(AM.template getResult<AnalysisT>(F));
1251 AAResults.addAADependencyID(AnalysisT::ID());
1252 }
1253
1254 template <typename AnalysisT>
1255 static void getModuleAAResultImpl(Function &F, FunctionAnalysisManager &AM,
1256 AAResults &AAResults) {
1257 auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
1258 if (auto *R =
1259 MAMProxy.template getCachedResult<AnalysisT>(*F.getParent())) {
1260 AAResults.addAAResult(*R);
1261 MAMProxy
1262 .template registerOuterAnalysisInvalidation<AnalysisT, AAManager>();
1263 }
1264 }
1265};
1266
1267/// A wrapper pass to provide the legacy pass manager access to a suitably
1268/// prepared AAResults object.
1269class AAResultsWrapperPass : public FunctionPass {
1270 std::unique_ptr<AAResults> AAR;
1271
1272public:
1273 static char ID;
1274
1275 AAResultsWrapperPass();
1276
1277 AAResults &getAAResults() { return *AAR; }
1278 const AAResults &getAAResults() const { return *AAR; }
1279
1280 bool runOnFunction(Function &F) override;
1281
1282 void getAnalysisUsage(AnalysisUsage &AU) const override;
1283};
1284
1285/// A wrapper pass for external alias analyses. This just squirrels away the
1286/// callback used to run any analyses and register their results.
1287struct ExternalAAWrapperPass : ImmutablePass {
1288 using CallbackT = std::function<void(Pass &, Function &, AAResults &)>;
1289
1290 CallbackT CB;
1291
1292 static char ID;
1293
1294 ExternalAAWrapperPass();
1295
1296 explicit ExternalAAWrapperPass(CallbackT CB);
1297
1298 void getAnalysisUsage(AnalysisUsage &AU) const override {
1299 AU.setPreservesAll();
1300 }
1301};
1302
1303FunctionPass *createAAResultsWrapperPass();
1304
1305/// A wrapper pass around a callback which can be used to populate the
1306/// AAResults in the AAResultsWrapperPass from an external AA.
1307///
1308/// The callback provided here will be used each time we prepare an AAResults
1309/// object, and will receive a reference to the function wrapper pass, the
1310/// function, and the AAResults object to populate. This should be used when
1311/// setting up a custom pass pipeline to inject a hook into the AA results.
1312ImmutablePass *createExternalAAWrapperPass(
1313 std::function<void(Pass &, Function &, AAResults &)> Callback);
1314
1315/// A helper for the legacy pass manager to create a \c AAResults
1316/// object populated to the best of our ability for a particular function when
1317/// inside of a \c ModulePass or a \c CallGraphSCCPass.
1318///
1319/// If a \c ModulePass or a \c CallGraphSCCPass calls \p
1320/// createLegacyPMAAResults, it also needs to call \p addUsedAAAnalyses in \p
1321/// getAnalysisUsage.
1322AAResults createLegacyPMAAResults(Pass &P, Function &F, BasicAAResult &BAR);
1323
1324/// A helper for the legacy pass manager to populate \p AU to add uses to make
1325/// sure the analyses required by \p createLegacyPMAAResults are available.
1326void getAAResultsAnalysisUsage(AnalysisUsage &AU);
1327
1328} // end namespace llvm
1329
1330#endif // LLVM_ANALYSIS_ALIASANALYSIS_H

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

1//===- llvm/Type.h - Classes for handling data types ------------*- 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 contains the declaration of the Type class. For more "Type"
10// stuff, look in DerivedTypes.h.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_IR_TYPE_H
15#define LLVM_IR_TYPE_H
16
17#include "llvm/ADT/APFloat.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/Support/CBindingWrapping.h"
21#include "llvm/Support/Casting.h"
22#include "llvm/Support/Compiler.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/TypeSize.h"
25#include <cassert>
26#include <cstdint>
27#include <iterator>
28
29namespace llvm {
30
31template<class GraphType> struct GraphTraits;
32class IntegerType;
33class LLVMContext;
34class PointerType;
35class raw_ostream;
36class StringRef;
37
38/// The instances of the Type class are immutable: once they are created,
39/// they are never changed. Also note that only one instance of a particular
40/// type is ever created. Thus seeing if two types are equal is a matter of
41/// doing a trivial pointer comparison. To enforce that no two equal instances
42/// are created, Type instances can only be created via static factory methods
43/// in class Type and in derived classes. Once allocated, Types are never
44/// free'd.
45///
46class Type {
47public:
48 //===--------------------------------------------------------------------===//
49 /// Definitions of all of the base types for the Type system. Based on this
50 /// value, you can cast to a class defined in DerivedTypes.h.
51 /// Note: If you add an element to this, you need to add an element to the
52 /// Type::getPrimitiveType function, or else things will break!
53 /// Also update LLVMTypeKind and LLVMGetTypeKind () in the C binding.
54 ///
55 enum TypeID {
56 // PrimitiveTypes
57 HalfTyID = 0, ///< 16-bit floating point type
58 BFloatTyID, ///< 16-bit floating point type (7-bit significand)
59 FloatTyID, ///< 32-bit floating point type
60 DoubleTyID, ///< 64-bit floating point type
61 X86_FP80TyID, ///< 80-bit floating point type (X87)
62 FP128TyID, ///< 128-bit floating point type (112-bit significand)
63 PPC_FP128TyID, ///< 128-bit floating point type (two 64-bits, PowerPC)
64 VoidTyID, ///< type with no size
65 LabelTyID, ///< Labels
66 MetadataTyID, ///< Metadata
67 X86_MMXTyID, ///< MMX vectors (64 bits, X86 specific)
68 X86_AMXTyID, ///< AMX vectors (8192 bits, X86 specific)
69 TokenTyID, ///< Tokens
70
71 // Derived types... see DerivedTypes.h file.
72 IntegerTyID, ///< Arbitrary bit width integers
73 FunctionTyID, ///< Functions
74 PointerTyID, ///< Pointers
75 StructTyID, ///< Structures
76 ArrayTyID, ///< Arrays
77 FixedVectorTyID, ///< Fixed width SIMD vector type
78 ScalableVectorTyID ///< Scalable SIMD vector type
79 };
80
81private:
82 /// This refers to the LLVMContext in which this type was uniqued.
83 LLVMContext &Context;
84
85 TypeID ID : 8; // The current base type of this type.
86 unsigned SubclassData : 24; // Space for subclasses to store data.
87 // Note that this should be synchronized with
88 // MAX_INT_BITS value in IntegerType class.
89
90protected:
91 friend class LLVMContextImpl;
92
93 explicit Type(LLVMContext &C, TypeID tid)
94 : Context(C), ID(tid), SubclassData(0) {}
95 ~Type() = default;
96
97 unsigned getSubclassData() const { return SubclassData; }
98
99 void setSubclassData(unsigned val) {
100 SubclassData = val;
101 // Ensure we don't have any accidental truncation.
102 assert(getSubclassData() == val && "Subclass data too large for field")(static_cast <bool> (getSubclassData() == val &&
"Subclass data too large for field") ? void (0) : __assert_fail
("getSubclassData() == val && \"Subclass data too large for field\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/Type.h"
, 102, __extension__ __PRETTY_FUNCTION__))
;
103 }
104
105 /// Keeps track of how many Type*'s there are in the ContainedTys list.
106 unsigned NumContainedTys = 0;
107
108 /// A pointer to the array of Types contained by this Type. For example, this
109 /// includes the arguments of a function type, the elements of a structure,
110 /// the pointee of a pointer, the element type of an array, etc. This pointer
111 /// may be 0 for types that don't contain other types (Integer, Double,
112 /// Float).
113 Type * const *ContainedTys = nullptr;
114
115public:
116 /// Print the current type.
117 /// Omit the type details if \p NoDetails == true.
118 /// E.g., let %st = type { i32, i16 }
119 /// When \p NoDetails is true, we only print %st.
120 /// Put differently, \p NoDetails prints the type as if
121 /// inlined with the operands when printing an instruction.
122 void print(raw_ostream &O, bool IsForDebug = false,
123 bool NoDetails = false) const;
124
125 void dump() const;
126
127 /// Return the LLVMContext in which this type was uniqued.
128 LLVMContext &getContext() const { return Context; }
129
130 //===--------------------------------------------------------------------===//
131 // Accessors for working with types.
132 //
133
134 /// Return the type id for the type. This will return one of the TypeID enum
135 /// elements defined above.
136 TypeID getTypeID() const { return ID; }
137
138 /// Return true if this is 'void'.
139 bool isVoidTy() const { return getTypeID() == VoidTyID; }
140
141 /// Return true if this is 'half', a 16-bit IEEE fp type.
142 bool isHalfTy() const { return getTypeID() == HalfTyID; }
143
144 /// Return true if this is 'bfloat', a 16-bit bfloat type.
145 bool isBFloatTy() const { return getTypeID() == BFloatTyID; }
146
147 /// Return true if this is 'float', a 32-bit IEEE fp type.
148 bool isFloatTy() const { return getTypeID() == FloatTyID; }
149
150 /// Return true if this is 'double', a 64-bit IEEE fp type.
151 bool isDoubleTy() const { return getTypeID() == DoubleTyID; }
152
153 /// Return true if this is x86 long double.
154 bool isX86_FP80Ty() const { return getTypeID() == X86_FP80TyID; }
155
156 /// Return true if this is 'fp128'.
157 bool isFP128Ty() const { return getTypeID() == FP128TyID; }
158
159 /// Return true if this is powerpc long double.
160 bool isPPC_FP128Ty() const { return getTypeID() == PPC_FP128TyID; }
161
162 /// Return true if this is one of the six floating-point types
163 bool isFloatingPointTy() const {
164 return getTypeID() == HalfTyID || getTypeID() == BFloatTyID ||
165 getTypeID() == FloatTyID || getTypeID() == DoubleTyID ||
166 getTypeID() == X86_FP80TyID || getTypeID() == FP128TyID ||
167 getTypeID() == PPC_FP128TyID;
168 }
169
170 const fltSemantics &getFltSemantics() const {
171 switch (getTypeID()) {
172 case HalfTyID: return APFloat::IEEEhalf();
173 case BFloatTyID: return APFloat::BFloat();
174 case FloatTyID: return APFloat::IEEEsingle();
175 case DoubleTyID: return APFloat::IEEEdouble();
176 case X86_FP80TyID: return APFloat::x87DoubleExtended();
177 case FP128TyID: return APFloat::IEEEquad();
178 case PPC_FP128TyID: return APFloat::PPCDoubleDouble();
179 default: llvm_unreachable("Invalid floating type")::llvm::llvm_unreachable_internal("Invalid floating type", "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/Type.h"
, 179)
;
180 }
181 }
182
183 /// Return true if this is X86 MMX.
184 bool isX86_MMXTy() const { return getTypeID() == X86_MMXTyID; }
185
186 /// Return true if this is X86 AMX.
187 bool isX86_AMXTy() const { return getTypeID() == X86_AMXTyID; }
188
189 /// Return true if this is a FP type or a vector of FP.
190 bool isFPOrFPVectorTy() const { return getScalarType()->isFloatingPointTy(); }
191
192 /// Return true if this is 'label'.
193 bool isLabelTy() const { return getTypeID() == LabelTyID; }
194
195 /// Return true if this is 'metadata'.
196 bool isMetadataTy() const { return getTypeID() == MetadataTyID; }
197
198 /// Return true if this is 'token'.
199 bool isTokenTy() const { return getTypeID() == TokenTyID; }
200
201 /// True if this is an instance of IntegerType.
202 bool isIntegerTy() const { return getTypeID() == IntegerTyID; }
203
204 /// Return true if this is an IntegerType of the given width.
205 bool isIntegerTy(unsigned Bitwidth) const;
206
207 /// Return true if this is an integer type or a vector of integer types.
208 bool isIntOrIntVectorTy() const { return getScalarType()->isIntegerTy(); }
209
210 /// Return true if this is an integer type or a vector of integer types of
211 /// the given width.
212 bool isIntOrIntVectorTy(unsigned BitWidth) const {
213 return getScalarType()->isIntegerTy(BitWidth);
214 }
215
216 /// Return true if this is an integer type or a pointer type.
217 bool isIntOrPtrTy() const { return isIntegerTy() || isPointerTy(); }
218
219 /// True if this is an instance of FunctionType.
220 bool isFunctionTy() const { return getTypeID() == FunctionTyID; }
221
222 /// True if this is an instance of StructType.
223 bool isStructTy() const { return getTypeID() == StructTyID; }
224
225 /// True if this is an instance of ArrayType.
226 bool isArrayTy() const { return getTypeID() == ArrayTyID; }
227
228 /// True if this is an instance of PointerType.
229 bool isPointerTy() const { return getTypeID() == PointerTyID; }
72
Assuming the condition is true
73
Returning the value 1, which participates in a condition later
230
231 /// Return true if this is a pointer type or a vector of pointer types.
232 bool isPtrOrPtrVectorTy() const { return getScalarType()->isPointerTy(); }
233
234 /// True if this is an instance of VectorType.
235 inline bool isVectorTy() const {
236 return getTypeID() == ScalableVectorTyID || getTypeID() == FixedVectorTyID;
237 }
238
239 /// Return true if this type could be converted with a lossless BitCast to
240 /// type 'Ty'. For example, i8* to i32*. BitCasts are valid for types of the
241 /// same size only where no re-interpretation of the bits is done.
242 /// Determine if this type could be losslessly bitcast to Ty
243 bool canLosslesslyBitCastTo(Type *Ty) const;
244
245 /// Return true if this type is empty, that is, it has no elements or all of
246 /// its elements are empty.
247 bool isEmptyTy() const;
248
249 /// Return true if the type is "first class", meaning it is a valid type for a
250 /// Value.
251 bool isFirstClassType() const {
252 return getTypeID() != FunctionTyID && getTypeID() != VoidTyID;
253 }
254
255 /// Return true if the type is a valid type for a register in codegen. This
256 /// includes all first-class types except struct and array types.
257 bool isSingleValueType() const {
258 return isFloatingPointTy() || isX86_MMXTy() || isIntegerTy() ||
259 isPointerTy() || isVectorTy() || isX86_AMXTy();
260 }
261
262 /// Return true if the type is an aggregate type. This means it is valid as
263 /// the first operand of an insertvalue or extractvalue instruction. This
264 /// includes struct and array types, but does not include vector types.
265 bool isAggregateType() const {
266 return getTypeID() == StructTyID || getTypeID() == ArrayTyID;
267 }
268
269 /// Return true if it makes sense to take the size of this type. To get the
270 /// actual size for a particular target, it is reasonable to use the
271 /// DataLayout subsystem to do this.
272 bool isSized(SmallPtrSetImpl<Type*> *Visited = nullptr) const {
273 // If it's a primitive, it is always sized.
274 if (getTypeID() == IntegerTyID || isFloatingPointTy() ||
275 getTypeID() == PointerTyID || getTypeID() == X86_MMXTyID ||
276 getTypeID() == X86_AMXTyID)
277 return true;
278 // If it is not something that can have a size (e.g. a function or label),
279 // it doesn't have a size.
280 if (getTypeID() != StructTyID && getTypeID() != ArrayTyID && !isVectorTy())
281 return false;
282 // Otherwise we have to try harder to decide.
283 return isSizedDerivedType(Visited);
284 }
285
286 /// Return the basic size of this type if it is a primitive type. These are
287 /// fixed by LLVM and are not target-dependent.
288 /// This will return zero if the type does not have a size or is not a
289 /// primitive type.
290 ///
291 /// If this is a scalable vector type, the scalable property will be set and
292 /// the runtime size will be a positive integer multiple of the base size.
293 ///
294 /// Note that this may not reflect the size of memory allocated for an
295 /// instance of the type or the number of bytes that are written when an
296 /// instance of the type is stored to memory. The DataLayout class provides
297 /// additional query functions to provide this information.
298 ///
299 TypeSize getPrimitiveSizeInBits() const LLVM_READONLY__attribute__((__pure__));
300
301 /// If this is a vector type, return the getPrimitiveSizeInBits value for the
302 /// element type. Otherwise return the getPrimitiveSizeInBits value for this
303 /// type.
304 unsigned getScalarSizeInBits() const LLVM_READONLY__attribute__((__pure__));
305
306 /// Return the width of the mantissa of this type. This is only valid on
307 /// floating-point types. If the FP type does not have a stable mantissa (e.g.
308 /// ppc long double), this method returns -1.
309 int getFPMantissaWidth() const;
310
311 /// Return whether the type is IEEE compatible, as defined by the eponymous
312 /// method in APFloat.
313 bool isIEEE() const { return APFloat::getZero(getFltSemantics()).isIEEE(); }
314
315 /// If this is a vector type, return the element type, otherwise return
316 /// 'this'.
317 inline Type *getScalarType() const {
318 if (isVectorTy())
319 return getContainedType(0);
320 return const_cast<Type *>(this);
321 }
322
323 //===--------------------------------------------------------------------===//
324 // Type Iteration support.
325 //
326 using subtype_iterator = Type * const *;
327
328 subtype_iterator subtype_begin() const { return ContainedTys; }
329 subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];}
330 ArrayRef<Type*> subtypes() const {
331 return makeArrayRef(subtype_begin(), subtype_end());
332 }
333
334 using subtype_reverse_iterator = std::reverse_iterator<subtype_iterator>;
335
336 subtype_reverse_iterator subtype_rbegin() const {
337 return subtype_reverse_iterator(subtype_end());
338 }
339 subtype_reverse_iterator subtype_rend() const {
340 return subtype_reverse_iterator(subtype_begin());
341 }
342
343 /// This method is used to implement the type iterator (defined at the end of
344 /// the file). For derived types, this returns the types 'contained' in the
345 /// derived type.
346 Type *getContainedType(unsigned i) const {
347 assert(i < NumContainedTys && "Index out of range!")(static_cast <bool> (i < NumContainedTys && "Index out of range!"
) ? void (0) : __assert_fail ("i < NumContainedTys && \"Index out of range!\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/Type.h"
, 347, __extension__ __PRETTY_FUNCTION__))
;
348 return ContainedTys[i];
349 }
350
351 /// Return the number of types in the derived type.
352 unsigned getNumContainedTypes() const { return NumContainedTys; }
353
354 //===--------------------------------------------------------------------===//
355 // Helper methods corresponding to subclass methods. This forces a cast to
356 // the specified subclass and calls its accessor. "getArrayNumElements" (for
357 // example) is shorthand for cast<ArrayType>(Ty)->getNumElements(). This is
358 // only intended to cover the core methods that are frequently used, helper
359 // methods should not be added here.
360
361 inline unsigned getIntegerBitWidth() const;
362
363 inline Type *getFunctionParamType(unsigned i) const;
364 inline unsigned getFunctionNumParams() const;
365 inline bool isFunctionVarArg() const;
366
367 inline StringRef getStructName() const;
368 inline unsigned getStructNumElements() const;
369 inline Type *getStructElementType(unsigned N) const;
370
371 inline uint64_t getArrayNumElements() const;
372
373 Type *getArrayElementType() const {
374 assert(getTypeID() == ArrayTyID)(static_cast <bool> (getTypeID() == ArrayTyID) ? void (
0) : __assert_fail ("getTypeID() == ArrayTyID", "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/Type.h"
, 374, __extension__ __PRETTY_FUNCTION__))
;
375 return ContainedTys[0];
376 }
377
378 Type *getPointerElementType() const {
379 assert(getTypeID() == PointerTyID)(static_cast <bool> (getTypeID() == PointerTyID) ? void
(0) : __assert_fail ("getTypeID() == PointerTyID", "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/Type.h"
, 379, __extension__ __PRETTY_FUNCTION__))
;
380 return ContainedTys[0];
381 }
382
383 /// Given vector type, change the element type,
384 /// whilst keeping the old number of elements.
385 /// For non-vectors simply returns \p EltTy.
386 inline Type *getWithNewType(Type *EltTy) const;
387
388 /// Given an integer or vector type, change the lane bitwidth to NewBitwidth,
389 /// whilst keeping the old number of lanes.
390 inline Type *getWithNewBitWidth(unsigned NewBitWidth) const;
391
392 /// Given scalar/vector integer type, returns a type with elements twice as
393 /// wide as in the original type. For vectors, preserves element count.
394 inline Type *getExtendedType() const;
395
396 /// Get the address space of this pointer or pointer vector type.
397 inline unsigned getPointerAddressSpace() const;
398
399 //===--------------------------------------------------------------------===//
400 // Static members exported by the Type class itself. Useful for getting
401 // instances of Type.
402 //
403
404 /// Return a type based on an identifier.
405 static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber);
406
407 //===--------------------------------------------------------------------===//
408 // These are the builtin types that are always available.
409 //
410 static Type *getVoidTy(LLVMContext &C);
411 static Type *getLabelTy(LLVMContext &C);
412 static Type *getHalfTy(LLVMContext &C);
413 static Type *getBFloatTy(LLVMContext &C);
414 static Type *getFloatTy(LLVMContext &C);
415 static Type *getDoubleTy(LLVMContext &C);
416 static Type *getMetadataTy(LLVMContext &C);
417 static Type *getX86_FP80Ty(LLVMContext &C);
418 static Type *getFP128Ty(LLVMContext &C);
419 static Type *getPPC_FP128Ty(LLVMContext &C);
420 static Type *getX86_MMXTy(LLVMContext &C);
421 static Type *getX86_AMXTy(LLVMContext &C);
422 static Type *getTokenTy(LLVMContext &C);
423 static IntegerType *getIntNTy(LLVMContext &C, unsigned N);
424 static IntegerType *getInt1Ty(LLVMContext &C);
425 static IntegerType *getInt8Ty(LLVMContext &C);
426 static IntegerType *getInt16Ty(LLVMContext &C);
427 static IntegerType *getInt32Ty(LLVMContext &C);
428 static IntegerType *getInt64Ty(LLVMContext &C);
429 static IntegerType *getInt128Ty(LLVMContext &C);
430 template <typename ScalarTy> static Type *getScalarTy(LLVMContext &C) {
431 int noOfBits = sizeof(ScalarTy) * CHAR_BIT8;
432 if (std::is_integral<ScalarTy>::value) {
433 return (Type*) Type::getIntNTy(C, noOfBits);
434 } else if (std::is_floating_point<ScalarTy>::value) {
435 switch (noOfBits) {
436 case 32:
437 return Type::getFloatTy(C);
438 case 64:
439 return Type::getDoubleTy(C);
440 }
441 }
442 llvm_unreachable("Unsupported type in Type::getScalarTy")::llvm::llvm_unreachable_internal("Unsupported type in Type::getScalarTy"
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/Type.h"
, 442)
;
443 }
444 static Type *getFloatingPointTy(LLVMContext &C, const fltSemantics &S) {
445 Type *Ty;
446 if (&S == &APFloat::IEEEhalf())
447 Ty = Type::getHalfTy(C);
448 else if (&S == &APFloat::BFloat())
449 Ty = Type::getBFloatTy(C);
450 else if (&S == &APFloat::IEEEsingle())
451 Ty = Type::getFloatTy(C);
452 else if (&S == &APFloat::IEEEdouble())
453 Ty = Type::getDoubleTy(C);
454 else if (&S == &APFloat::x87DoubleExtended())
455 Ty = Type::getX86_FP80Ty(C);
456 else if (&S == &APFloat::IEEEquad())
457 Ty = Type::getFP128Ty(C);
458 else {
459 assert(&S == &APFloat::PPCDoubleDouble() && "Unknown FP format")(static_cast <bool> (&S == &APFloat::PPCDoubleDouble
() && "Unknown FP format") ? void (0) : __assert_fail
("&S == &APFloat::PPCDoubleDouble() && \"Unknown FP format\""
, "/build/llvm-toolchain-snapshot-13~++20210506100649+6304c0836a4d/llvm/include/llvm/IR/Type.h"
, 459, __extension__ __PRETTY_FUNCTION__))
;
460 Ty = Type::getPPC_FP128Ty(C);
461 }
462 return Ty;
463 }
464
465 //===--------------------------------------------------------------------===//
466 // Convenience methods for getting pointer types with one of the above builtin
467 // types as pointee.
468 //
469 static PointerType *getHalfPtrTy(LLVMContext &C, unsigned AS = 0);
470 static PointerType *getBFloatPtrTy(LLVMContext &C, unsigned AS = 0);
471 static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0);
472 static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0);
473 static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0);
474 static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0);
475 static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0);
476 static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0);
477 static PointerType *getX86_AMXPtrTy(LLVMContext &C, unsigned AS = 0);
478 static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0);
479 static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0);
480 static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0);
481 static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0);
482 static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0);
483 static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0);
484
485 /// Return a pointer to the current type. This is equivalent to
486 /// PointerType::get(Foo, AddrSpace).
487 PointerType *getPointerTo(unsigned AddrSpace = 0) const;
488
489private:
490 /// Derived types like structures and arrays are sized iff all of the members
491 /// of the type are sized as well. Since asking for their size is relatively
492 /// uncommon, move this operation out-of-line.
493 bool isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited = nullptr) const;
494};
495
496// Printing of types.
497inline raw_ostream &operator<<(raw_ostream &OS, const Type &T) {
498 T.print(OS);
499 return OS;
500}
501
502// allow isa<PointerType>(x) to work without DerivedTypes.h included.
503template <> struct isa_impl<PointerType, Type> {
504 static inline bool doit(const Type &Ty) {
505 return Ty.getTypeID() == Type::PointerTyID;
506 }
507};
508
509// Create wrappers for C Binding types (see CBindingWrapping.h).
510DEFINE_ISA_CONVERSION_FUNCTIONS(Type, LLVMTypeRef)inline Type *unwrap(LLVMTypeRef P) { return reinterpret_cast<
Type*>(P); } inline LLVMTypeRef wrap(const Type *P) { return
reinterpret_cast<LLVMTypeRef>(const_cast<Type*>(
P)); } template<typename T> inline T *unwrap(LLVMTypeRef
P) { return cast<T>(unwrap(P)); }
511
512/* Specialized opaque type conversions.
513 */
514inline Type **unwrap(LLVMTypeRef* Tys) {
515 return reinterpret_cast<Type**>(Tys);
516}
517
518inline LLVMTypeRef *wrap(Type **Tys) {
519 return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys));
520}
521
522} // end namespace llvm
523
524#endif // LLVM_IR_TYPE_H