Bug Summary

File:src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT/SmallBitVector.h
Warning:line 376, column 51
The result of the left shift is undefined due to shifting by '4294967295', which is greater or equal to the width of type 'uintptr_t'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name DependenceAnalysis.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 -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Analysis -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ASMParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/BinaryFormat -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitstream -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /include/llvm/CodeGen -I /include/llvm/CodeGen/PBQP -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Coroutines -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData/Coverage -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/CodeView -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/DWARF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/MSF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/PDB -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Demangle -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/JITLink -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/Orc -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenACC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenMP -I /include/llvm/CodeGen/GlobalISel -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IRReader -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/LTO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Linker -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC/MCParser -I /include/llvm/CodeGen/MIRParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Object -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Option -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Passes -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Scalar -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/Symbolize -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Target -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Utils -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Vectorize -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/IPO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libLLVM/../include -I /usr/src/gnu/usr.bin/clang/libLLVM/obj -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Analysis/DependenceAnalysis.cpp

/usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Analysis/DependenceAnalysis.cpp

1//===-- DependenceAnalysis.cpp - DA Implementation --------------*- 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// DependenceAnalysis is an LLVM pass that analyses dependences between memory
10// accesses. Currently, it is an (incomplete) implementation of the approach
11// described in
12//
13// Practical Dependence Testing
14// Goff, Kennedy, Tseng
15// PLDI 1991
16//
17// There's a single entry point that analyzes the dependence between a pair
18// of memory references in a function, returning either NULL, for no dependence,
19// or a more-or-less detailed description of the dependence between them.
20//
21// Currently, the implementation cannot propagate constraints between
22// coupled RDIV subscripts and lacks a multi-subscript MIV test.
23// Both of these are conservative weaknesses;
24// that is, not a source of correctness problems.
25//
26// Since Clang linearizes some array subscripts, the dependence
27// analysis is using SCEV->delinearize to recover the representation of multiple
28// subscripts, and thus avoid the more expensive and less precise MIV tests. The
29// delinearization is controlled by the flag -da-delinearize.
30//
31// We should pay some careful attention to the possibility of integer overflow
32// in the implementation of the various tests. This could happen with Add,
33// Subtract, or Multiply, with both APInt's and SCEV's.
34//
35// Some non-linear subscript pairs can be handled by the GCD test
36// (and perhaps other tests).
37// Should explore how often these things occur.
38//
39// Finally, it seems like certain test cases expose weaknesses in the SCEV
40// simplification, especially in the handling of sign and zero extensions.
41// It could be useful to spend time exploring these.
42//
43// Please note that this is work in progress and the interface is subject to
44// change.
45//
46//===----------------------------------------------------------------------===//
47// //
48// In memory of Ken Kennedy, 1945 - 2007 //
49// //
50//===----------------------------------------------------------------------===//
51
52#include "llvm/Analysis/DependenceAnalysis.h"
53#include "llvm/ADT/STLExtras.h"
54#include "llvm/ADT/Statistic.h"
55#include "llvm/Analysis/AliasAnalysis.h"
56#include "llvm/Analysis/LoopInfo.h"
57#include "llvm/Analysis/ScalarEvolution.h"
58#include "llvm/Analysis/ScalarEvolutionExpressions.h"
59#include "llvm/Analysis/ValueTracking.h"
60#include "llvm/Config/llvm-config.h"
61#include "llvm/IR/InstIterator.h"
62#include "llvm/IR/Module.h"
63#include "llvm/IR/Operator.h"
64#include "llvm/InitializePasses.h"
65#include "llvm/Support/CommandLine.h"
66#include "llvm/Support/Debug.h"
67#include "llvm/Support/ErrorHandling.h"
68#include "llvm/Support/raw_ostream.h"
69
70using namespace llvm;
71
72#define DEBUG_TYPE"da" "da"
73
74//===----------------------------------------------------------------------===//
75// statistics
76
77STATISTIC(TotalArrayPairs, "Array pairs tested")static llvm::Statistic TotalArrayPairs = {"da", "TotalArrayPairs"
, "Array pairs tested"}
;
78STATISTIC(SeparableSubscriptPairs, "Separable subscript pairs")static llvm::Statistic SeparableSubscriptPairs = {"da", "SeparableSubscriptPairs"
, "Separable subscript pairs"}
;
79STATISTIC(CoupledSubscriptPairs, "Coupled subscript pairs")static llvm::Statistic CoupledSubscriptPairs = {"da", "CoupledSubscriptPairs"
, "Coupled subscript pairs"}
;
80STATISTIC(NonlinearSubscriptPairs, "Nonlinear subscript pairs")static llvm::Statistic NonlinearSubscriptPairs = {"da", "NonlinearSubscriptPairs"
, "Nonlinear subscript pairs"}
;
81STATISTIC(ZIVapplications, "ZIV applications")static llvm::Statistic ZIVapplications = {"da", "ZIVapplications"
, "ZIV applications"}
;
82STATISTIC(ZIVindependence, "ZIV independence")static llvm::Statistic ZIVindependence = {"da", "ZIVindependence"
, "ZIV independence"}
;
83STATISTIC(StrongSIVapplications, "Strong SIV applications")static llvm::Statistic StrongSIVapplications = {"da", "StrongSIVapplications"
, "Strong SIV applications"}
;
84STATISTIC(StrongSIVsuccesses, "Strong SIV successes")static llvm::Statistic StrongSIVsuccesses = {"da", "StrongSIVsuccesses"
, "Strong SIV successes"}
;
85STATISTIC(StrongSIVindependence, "Strong SIV independence")static llvm::Statistic StrongSIVindependence = {"da", "StrongSIVindependence"
, "Strong SIV independence"}
;
86STATISTIC(WeakCrossingSIVapplications, "Weak-Crossing SIV applications")static llvm::Statistic WeakCrossingSIVapplications = {"da", "WeakCrossingSIVapplications"
, "Weak-Crossing SIV applications"}
;
87STATISTIC(WeakCrossingSIVsuccesses, "Weak-Crossing SIV successes")static llvm::Statistic WeakCrossingSIVsuccesses = {"da", "WeakCrossingSIVsuccesses"
, "Weak-Crossing SIV successes"}
;
88STATISTIC(WeakCrossingSIVindependence, "Weak-Crossing SIV independence")static llvm::Statistic WeakCrossingSIVindependence = {"da", "WeakCrossingSIVindependence"
, "Weak-Crossing SIV independence"}
;
89STATISTIC(ExactSIVapplications, "Exact SIV applications")static llvm::Statistic ExactSIVapplications = {"da", "ExactSIVapplications"
, "Exact SIV applications"}
;
90STATISTIC(ExactSIVsuccesses, "Exact SIV successes")static llvm::Statistic ExactSIVsuccesses = {"da", "ExactSIVsuccesses"
, "Exact SIV successes"}
;
91STATISTIC(ExactSIVindependence, "Exact SIV independence")static llvm::Statistic ExactSIVindependence = {"da", "ExactSIVindependence"
, "Exact SIV independence"}
;
92STATISTIC(WeakZeroSIVapplications, "Weak-Zero SIV applications")static llvm::Statistic WeakZeroSIVapplications = {"da", "WeakZeroSIVapplications"
, "Weak-Zero SIV applications"}
;
93STATISTIC(WeakZeroSIVsuccesses, "Weak-Zero SIV successes")static llvm::Statistic WeakZeroSIVsuccesses = {"da", "WeakZeroSIVsuccesses"
, "Weak-Zero SIV successes"}
;
94STATISTIC(WeakZeroSIVindependence, "Weak-Zero SIV independence")static llvm::Statistic WeakZeroSIVindependence = {"da", "WeakZeroSIVindependence"
, "Weak-Zero SIV independence"}
;
95STATISTIC(ExactRDIVapplications, "Exact RDIV applications")static llvm::Statistic ExactRDIVapplications = {"da", "ExactRDIVapplications"
, "Exact RDIV applications"}
;
96STATISTIC(ExactRDIVindependence, "Exact RDIV independence")static llvm::Statistic ExactRDIVindependence = {"da", "ExactRDIVindependence"
, "Exact RDIV independence"}
;
97STATISTIC(SymbolicRDIVapplications, "Symbolic RDIV applications")static llvm::Statistic SymbolicRDIVapplications = {"da", "SymbolicRDIVapplications"
, "Symbolic RDIV applications"}
;
98STATISTIC(SymbolicRDIVindependence, "Symbolic RDIV independence")static llvm::Statistic SymbolicRDIVindependence = {"da", "SymbolicRDIVindependence"
, "Symbolic RDIV independence"}
;
99STATISTIC(DeltaApplications, "Delta applications")static llvm::Statistic DeltaApplications = {"da", "DeltaApplications"
, "Delta applications"}
;
100STATISTIC(DeltaSuccesses, "Delta successes")static llvm::Statistic DeltaSuccesses = {"da", "DeltaSuccesses"
, "Delta successes"}
;
101STATISTIC(DeltaIndependence, "Delta independence")static llvm::Statistic DeltaIndependence = {"da", "DeltaIndependence"
, "Delta independence"}
;
102STATISTIC(DeltaPropagations, "Delta propagations")static llvm::Statistic DeltaPropagations = {"da", "DeltaPropagations"
, "Delta propagations"}
;
103STATISTIC(GCDapplications, "GCD applications")static llvm::Statistic GCDapplications = {"da", "GCDapplications"
, "GCD applications"}
;
104STATISTIC(GCDsuccesses, "GCD successes")static llvm::Statistic GCDsuccesses = {"da", "GCDsuccesses", "GCD successes"
}
;
105STATISTIC(GCDindependence, "GCD independence")static llvm::Statistic GCDindependence = {"da", "GCDindependence"
, "GCD independence"}
;
106STATISTIC(BanerjeeApplications, "Banerjee applications")static llvm::Statistic BanerjeeApplications = {"da", "BanerjeeApplications"
, "Banerjee applications"}
;
107STATISTIC(BanerjeeIndependence, "Banerjee independence")static llvm::Statistic BanerjeeIndependence = {"da", "BanerjeeIndependence"
, "Banerjee independence"}
;
108STATISTIC(BanerjeeSuccesses, "Banerjee successes")static llvm::Statistic BanerjeeSuccesses = {"da", "BanerjeeSuccesses"
, "Banerjee successes"}
;
109
110static cl::opt<bool>
111 Delinearize("da-delinearize", cl::init(true), cl::Hidden, cl::ZeroOrMore,
112 cl::desc("Try to delinearize array references."));
113static cl::opt<bool> DisableDelinearizationChecks(
114 "da-disable-delinearization-checks", cl::init(false), cl::Hidden,
115 cl::ZeroOrMore,
116 cl::desc(
117 "Disable checks that try to statically verify validity of "
118 "delinearized subscripts. Enabling this option may result in incorrect "
119 "dependence vectors for languages that allow the subscript of one "
120 "dimension to underflow or overflow into another dimension."));
121
122//===----------------------------------------------------------------------===//
123// basics
124
125DependenceAnalysis::Result
126DependenceAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
127 auto &AA = FAM.getResult<AAManager>(F);
128 auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F);
129 auto &LI = FAM.getResult<LoopAnalysis>(F);
130 return DependenceInfo(&F, &AA, &SE, &LI);
131}
132
133AnalysisKey DependenceAnalysis::Key;
134
135INITIALIZE_PASS_BEGIN(DependenceAnalysisWrapperPass, "da",static void *initializeDependenceAnalysisWrapperPassPassOnce(
PassRegistry &Registry) {
136 "Dependence Analysis", true, true)static void *initializeDependenceAnalysisWrapperPassPassOnce(
PassRegistry &Registry) {
137INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)initializeLoopInfoWrapperPassPass(Registry);
138INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)initializeScalarEvolutionWrapperPassPass(Registry);
139INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)initializeAAResultsWrapperPassPass(Registry);
140INITIALIZE_PASS_END(DependenceAnalysisWrapperPass, "da", "Dependence Analysis",PassInfo *PI = new PassInfo( "Dependence Analysis", "da", &
DependenceAnalysisWrapperPass::ID, PassInfo::NormalCtor_t(callDefaultCtor
<DependenceAnalysisWrapperPass>), true, true); Registry
.registerPass(*PI, true); return PI; } static llvm::once_flag
InitializeDependenceAnalysisWrapperPassPassFlag; void llvm::
initializeDependenceAnalysisWrapperPassPass(PassRegistry &
Registry) { llvm::call_once(InitializeDependenceAnalysisWrapperPassPassFlag
, initializeDependenceAnalysisWrapperPassPassOnce, std::ref(Registry
)); }
141 true, true)PassInfo *PI = new PassInfo( "Dependence Analysis", "da", &
DependenceAnalysisWrapperPass::ID, PassInfo::NormalCtor_t(callDefaultCtor
<DependenceAnalysisWrapperPass>), true, true); Registry
.registerPass(*PI, true); return PI; } static llvm::once_flag
InitializeDependenceAnalysisWrapperPassPassFlag; void llvm::
initializeDependenceAnalysisWrapperPassPass(PassRegistry &
Registry) { llvm::call_once(InitializeDependenceAnalysisWrapperPassPassFlag
, initializeDependenceAnalysisWrapperPassPassOnce, std::ref(Registry
)); }
142
143char DependenceAnalysisWrapperPass::ID = 0;
144
145DependenceAnalysisWrapperPass::DependenceAnalysisWrapperPass()
146 : FunctionPass(ID) {
147 initializeDependenceAnalysisWrapperPassPass(*PassRegistry::getPassRegistry());
148}
149
150FunctionPass *llvm::createDependenceAnalysisWrapperPass() {
151 return new DependenceAnalysisWrapperPass();
152}
153
154bool DependenceAnalysisWrapperPass::runOnFunction(Function &F) {
155 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
156 auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
157 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
158 info.reset(new DependenceInfo(&F, &AA, &SE, &LI));
159 return false;
160}
161
162DependenceInfo &DependenceAnalysisWrapperPass::getDI() const { return *info; }
163
164void DependenceAnalysisWrapperPass::releaseMemory() { info.reset(); }
165
166void DependenceAnalysisWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
167 AU.setPreservesAll();
168 AU.addRequiredTransitive<AAResultsWrapperPass>();
169 AU.addRequiredTransitive<ScalarEvolutionWrapperPass>();
170 AU.addRequiredTransitive<LoopInfoWrapperPass>();
171}
172
173// Used to test the dependence analyzer.
174// Looks through the function, noting instructions that may access memory.
175// Calls depends() on every possible pair and prints out the result.
176// Ignores all other instructions.
177static void dumpExampleDependence(raw_ostream &OS, DependenceInfo *DA) {
178 auto *F = DA->getFunction();
179 for (inst_iterator SrcI = inst_begin(F), SrcE = inst_end(F); SrcI != SrcE;
180 ++SrcI) {
181 if (SrcI->mayReadOrWriteMemory()) {
182 for (inst_iterator DstI = SrcI, DstE = inst_end(F);
183 DstI != DstE; ++DstI) {
184 if (DstI->mayReadOrWriteMemory()) {
185 OS << "Src:" << *SrcI << " --> Dst:" << *DstI << "\n";
186 OS << " da analyze - ";
187 if (auto D = DA->depends(&*SrcI, &*DstI, true)) {
188 D->dump(OS);
189 for (unsigned Level = 1; Level <= D->getLevels(); Level++) {
190 if (D->isSplitable(Level)) {
191 OS << " da analyze - split level = " << Level;
192 OS << ", iteration = " << *DA->getSplitIteration(*D, Level);
193 OS << "!\n";
194 }
195 }
196 }
197 else
198 OS << "none!\n";
199 }
200 }
201 }
202 }
203}
204
205void DependenceAnalysisWrapperPass::print(raw_ostream &OS,
206 const Module *) const {
207 dumpExampleDependence(OS, info.get());
208}
209
210PreservedAnalyses
211DependenceAnalysisPrinterPass::run(Function &F, FunctionAnalysisManager &FAM) {
212 OS << "'Dependence Analysis' for function '" << F.getName() << "':\n";
213 dumpExampleDependence(OS, &FAM.getResult<DependenceAnalysis>(F));
214 return PreservedAnalyses::all();
215}
216
217//===----------------------------------------------------------------------===//
218// Dependence methods
219
220// Returns true if this is an input dependence.
221bool Dependence::isInput() const {
222 return Src->mayReadFromMemory() && Dst->mayReadFromMemory();
223}
224
225
226// Returns true if this is an output dependence.
227bool Dependence::isOutput() const {
228 return Src->mayWriteToMemory() && Dst->mayWriteToMemory();
229}
230
231
232// Returns true if this is an flow (aka true) dependence.
233bool Dependence::isFlow() const {
234 return Src->mayWriteToMemory() && Dst->mayReadFromMemory();
235}
236
237
238// Returns true if this is an anti dependence.
239bool Dependence::isAnti() const {
240 return Src->mayReadFromMemory() && Dst->mayWriteToMemory();
241}
242
243
244// Returns true if a particular level is scalar; that is,
245// if no subscript in the source or destination mention the induction
246// variable associated with the loop at this level.
247// Leave this out of line, so it will serve as a virtual method anchor
248bool Dependence::isScalar(unsigned level) const {
249 return false;
250}
251
252
253//===----------------------------------------------------------------------===//
254// FullDependence methods
255
256FullDependence::FullDependence(Instruction *Source, Instruction *Destination,
257 bool PossiblyLoopIndependent,
258 unsigned CommonLevels)
259 : Dependence(Source, Destination), Levels(CommonLevels),
260 LoopIndependent(PossiblyLoopIndependent) {
261 Consistent = true;
262 if (CommonLevels)
263 DV = std::make_unique<DVEntry[]>(CommonLevels);
264}
265
266// The rest are simple getters that hide the implementation.
267
268// getDirection - Returns the direction associated with a particular level.
269unsigned FullDependence::getDirection(unsigned Level) const {
270 assert(0 < Level && Level <= Levels && "Level out of range")((void)0);
271 return DV[Level - 1].Direction;
272}
273
274
275// Returns the distance (or NULL) associated with a particular level.
276const SCEV *FullDependence::getDistance(unsigned Level) const {
277 assert(0 < Level && Level <= Levels && "Level out of range")((void)0);
278 return DV[Level - 1].Distance;
279}
280
281
282// Returns true if a particular level is scalar; that is,
283// if no subscript in the source or destination mention the induction
284// variable associated with the loop at this level.
285bool FullDependence::isScalar(unsigned Level) const {
286 assert(0 < Level && Level <= Levels && "Level out of range")((void)0);
287 return DV[Level - 1].Scalar;
288}
289
290
291// Returns true if peeling the first iteration from this loop
292// will break this dependence.
293bool FullDependence::isPeelFirst(unsigned Level) const {
294 assert(0 < Level && Level <= Levels && "Level out of range")((void)0);
295 return DV[Level - 1].PeelFirst;
296}
297
298
299// Returns true if peeling the last iteration from this loop
300// will break this dependence.
301bool FullDependence::isPeelLast(unsigned Level) const {
302 assert(0 < Level && Level <= Levels && "Level out of range")((void)0);
303 return DV[Level - 1].PeelLast;
304}
305
306
307// Returns true if splitting this loop will break the dependence.
308bool FullDependence::isSplitable(unsigned Level) const {
309 assert(0 < Level && Level <= Levels && "Level out of range")((void)0);
310 return DV[Level - 1].Splitable;
311}
312
313
314//===----------------------------------------------------------------------===//
315// DependenceInfo::Constraint methods
316
317// If constraint is a point <X, Y>, returns X.
318// Otherwise assert.
319const SCEV *DependenceInfo::Constraint::getX() const {
320 assert(Kind == Point && "Kind should be Point")((void)0);
321 return A;
322}
323
324
325// If constraint is a point <X, Y>, returns Y.
326// Otherwise assert.
327const SCEV *DependenceInfo::Constraint::getY() const {
328 assert(Kind == Point && "Kind should be Point")((void)0);
329 return B;
330}
331
332
333// If constraint is a line AX + BY = C, returns A.
334// Otherwise assert.
335const SCEV *DependenceInfo::Constraint::getA() const {
336 assert((Kind == Line || Kind == Distance) &&((void)0)
337 "Kind should be Line (or Distance)")((void)0);
338 return A;
339}
340
341
342// If constraint is a line AX + BY = C, returns B.
343// Otherwise assert.
344const SCEV *DependenceInfo::Constraint::getB() const {
345 assert((Kind == Line || Kind == Distance) &&((void)0)
346 "Kind should be Line (or Distance)")((void)0);
347 return B;
348}
349
350
351// If constraint is a line AX + BY = C, returns C.
352// Otherwise assert.
353const SCEV *DependenceInfo::Constraint::getC() const {
354 assert((Kind == Line || Kind == Distance) &&((void)0)
355 "Kind should be Line (or Distance)")((void)0);
356 return C;
357}
358
359
360// If constraint is a distance, returns D.
361// Otherwise assert.
362const SCEV *DependenceInfo::Constraint::getD() const {
363 assert(Kind == Distance && "Kind should be Distance")((void)0);
364 return SE->getNegativeSCEV(C);
365}
366
367
368// Returns the loop associated with this constraint.
369const Loop *DependenceInfo::Constraint::getAssociatedLoop() const {
370 assert((Kind == Distance || Kind == Line || Kind == Point) &&((void)0)
371 "Kind should be Distance, Line, or Point")((void)0);
372 return AssociatedLoop;
373}
374
375void DependenceInfo::Constraint::setPoint(const SCEV *X, const SCEV *Y,
376 const Loop *CurLoop) {
377 Kind = Point;
378 A = X;
379 B = Y;
380 AssociatedLoop = CurLoop;
381}
382
383void DependenceInfo::Constraint::setLine(const SCEV *AA, const SCEV *BB,
384 const SCEV *CC, const Loop *CurLoop) {
385 Kind = Line;
386 A = AA;
387 B = BB;
388 C = CC;
389 AssociatedLoop = CurLoop;
390}
391
392void DependenceInfo::Constraint::setDistance(const SCEV *D,
393 const Loop *CurLoop) {
394 Kind = Distance;
395 A = SE->getOne(D->getType());
396 B = SE->getNegativeSCEV(A);
397 C = SE->getNegativeSCEV(D);
398 AssociatedLoop = CurLoop;
399}
400
401void DependenceInfo::Constraint::setEmpty() { Kind = Empty; }
402
403void DependenceInfo::Constraint::setAny(ScalarEvolution *NewSE) {
404 SE = NewSE;
405 Kind = Any;
406}
407
408#if !defined(NDEBUG1) || defined(LLVM_ENABLE_DUMP)
409// For debugging purposes. Dumps the constraint out to OS.
410LLVM_DUMP_METHOD__attribute__((noinline)) void DependenceInfo::Constraint::dump(raw_ostream &OS) const {
411 if (isEmpty())
412 OS << " Empty\n";
413 else if (isAny())
414 OS << " Any\n";
415 else if (isPoint())
416 OS << " Point is <" << *getX() << ", " << *getY() << ">\n";
417 else if (isDistance())
418 OS << " Distance is " << *getD() <<
419 " (" << *getA() << "*X + " << *getB() << "*Y = " << *getC() << ")\n";
420 else if (isLine())
421 OS << " Line is " << *getA() << "*X + " <<
422 *getB() << "*Y = " << *getC() << "\n";
423 else
424 llvm_unreachable("unknown constraint type in Constraint::dump")__builtin_unreachable();
425}
426#endif
427
428
429// Updates X with the intersection
430// of the Constraints X and Y. Returns true if X has changed.
431// Corresponds to Figure 4 from the paper
432//
433// Practical Dependence Testing
434// Goff, Kennedy, Tseng
435// PLDI 1991
436bool DependenceInfo::intersectConstraints(Constraint *X, const Constraint *Y) {
437 ++DeltaApplications;
438 LLVM_DEBUG(dbgs() << "\tintersect constraints\n")do { } while (false);
439 LLVM_DEBUG(dbgs() << "\t X ="; X->dump(dbgs()))do { } while (false);
440 LLVM_DEBUG(dbgs() << "\t Y ="; Y->dump(dbgs()))do { } while (false);
441 assert(!Y->isPoint() && "Y must not be a Point")((void)0);
442 if (X->isAny()) {
443 if (Y->isAny())
444 return false;
445 *X = *Y;
446 return true;
447 }
448 if (X->isEmpty())
449 return false;
450 if (Y->isEmpty()) {
451 X->setEmpty();
452 return true;
453 }
454
455 if (X->isDistance() && Y->isDistance()) {
456 LLVM_DEBUG(dbgs() << "\t intersect 2 distances\n")do { } while (false);
457 if (isKnownPredicate(CmpInst::ICMP_EQ, X->getD(), Y->getD()))
458 return false;
459 if (isKnownPredicate(CmpInst::ICMP_NE, X->getD(), Y->getD())) {
460 X->setEmpty();
461 ++DeltaSuccesses;
462 return true;
463 }
464 // Hmmm, interesting situation.
465 // I guess if either is constant, keep it and ignore the other.
466 if (isa<SCEVConstant>(Y->getD())) {
467 *X = *Y;
468 return true;
469 }
470 return false;
471 }
472
473 // At this point, the pseudo-code in Figure 4 of the paper
474 // checks if (X->isPoint() && Y->isPoint()).
475 // This case can't occur in our implementation,
476 // since a Point can only arise as the result of intersecting
477 // two Line constraints, and the right-hand value, Y, is never
478 // the result of an intersection.
479 assert(!(X->isPoint() && Y->isPoint()) &&((void)0)
480 "We shouldn't ever see X->isPoint() && Y->isPoint()")((void)0);
481
482 if (X->isLine() && Y->isLine()) {
483 LLVM_DEBUG(dbgs() << "\t intersect 2 lines\n")do { } while (false);
484 const SCEV *Prod1 = SE->getMulExpr(X->getA(), Y->getB());
485 const SCEV *Prod2 = SE->getMulExpr(X->getB(), Y->getA());
486 if (isKnownPredicate(CmpInst::ICMP_EQ, Prod1, Prod2)) {
487 // slopes are equal, so lines are parallel
488 LLVM_DEBUG(dbgs() << "\t\tsame slope\n")do { } while (false);
489 Prod1 = SE->getMulExpr(X->getC(), Y->getB());
490 Prod2 = SE->getMulExpr(X->getB(), Y->getC());
491 if (isKnownPredicate(CmpInst::ICMP_EQ, Prod1, Prod2))
492 return false;
493 if (isKnownPredicate(CmpInst::ICMP_NE, Prod1, Prod2)) {
494 X->setEmpty();
495 ++DeltaSuccesses;
496 return true;
497 }
498 return false;
499 }
500 if (isKnownPredicate(CmpInst::ICMP_NE, Prod1, Prod2)) {
501 // slopes differ, so lines intersect
502 LLVM_DEBUG(dbgs() << "\t\tdifferent slopes\n")do { } while (false);
503 const SCEV *C1B2 = SE->getMulExpr(X->getC(), Y->getB());
504 const SCEV *C1A2 = SE->getMulExpr(X->getC(), Y->getA());
505 const SCEV *C2B1 = SE->getMulExpr(Y->getC(), X->getB());
506 const SCEV *C2A1 = SE->getMulExpr(Y->getC(), X->getA());
507 const SCEV *A1B2 = SE->getMulExpr(X->getA(), Y->getB());
508 const SCEV *A2B1 = SE->getMulExpr(Y->getA(), X->getB());
509 const SCEVConstant *C1A2_C2A1 =
510 dyn_cast<SCEVConstant>(SE->getMinusSCEV(C1A2, C2A1));
511 const SCEVConstant *C1B2_C2B1 =
512 dyn_cast<SCEVConstant>(SE->getMinusSCEV(C1B2, C2B1));
513 const SCEVConstant *A1B2_A2B1 =
514 dyn_cast<SCEVConstant>(SE->getMinusSCEV(A1B2, A2B1));
515 const SCEVConstant *A2B1_A1B2 =
516 dyn_cast<SCEVConstant>(SE->getMinusSCEV(A2B1, A1B2));
517 if (!C1B2_C2B1 || !C1A2_C2A1 ||
518 !A1B2_A2B1 || !A2B1_A1B2)
519 return false;
520 APInt Xtop = C1B2_C2B1->getAPInt();
521 APInt Xbot = A1B2_A2B1->getAPInt();
522 APInt Ytop = C1A2_C2A1->getAPInt();
523 APInt Ybot = A2B1_A1B2->getAPInt();
524 LLVM_DEBUG(dbgs() << "\t\tXtop = " << Xtop << "\n")do { } while (false);
525 LLVM_DEBUG(dbgs() << "\t\tXbot = " << Xbot << "\n")do { } while (false);
526 LLVM_DEBUG(dbgs() << "\t\tYtop = " << Ytop << "\n")do { } while (false);
527 LLVM_DEBUG(dbgs() << "\t\tYbot = " << Ybot << "\n")do { } while (false);
528 APInt Xq = Xtop; // these need to be initialized, even
529 APInt Xr = Xtop; // though they're just going to be overwritten
530 APInt::sdivrem(Xtop, Xbot, Xq, Xr);
531 APInt Yq = Ytop;
532 APInt Yr = Ytop;
533 APInt::sdivrem(Ytop, Ybot, Yq, Yr);
534 if (Xr != 0 || Yr != 0) {
535 X->setEmpty();
536 ++DeltaSuccesses;
537 return true;
538 }
539 LLVM_DEBUG(dbgs() << "\t\tX = " << Xq << ", Y = " << Yq << "\n")do { } while (false);
540 if (Xq.slt(0) || Yq.slt(0)) {
541 X->setEmpty();
542 ++DeltaSuccesses;
543 return true;
544 }
545 if (const SCEVConstant *CUB =
546 collectConstantUpperBound(X->getAssociatedLoop(), Prod1->getType())) {
547 const APInt &UpperBound = CUB->getAPInt();
548 LLVM_DEBUG(dbgs() << "\t\tupper bound = " << UpperBound << "\n")do { } while (false);
549 if (Xq.sgt(UpperBound) || Yq.sgt(UpperBound)) {
550 X->setEmpty();
551 ++DeltaSuccesses;
552 return true;
553 }
554 }
555 X->setPoint(SE->getConstant(Xq),
556 SE->getConstant(Yq),
557 X->getAssociatedLoop());
558 ++DeltaSuccesses;
559 return true;
560 }
561 return false;
562 }
563
564 // if (X->isLine() && Y->isPoint()) This case can't occur.
565 assert(!(X->isLine() && Y->isPoint()) && "This case should never occur")((void)0);
566
567 if (X->isPoint() && Y->isLine()) {
568 LLVM_DEBUG(dbgs() << "\t intersect Point and Line\n")do { } while (false);
569 const SCEV *A1X1 = SE->getMulExpr(Y->getA(), X->getX());
570 const SCEV *B1Y1 = SE->getMulExpr(Y->getB(), X->getY());
571 const SCEV *Sum = SE->getAddExpr(A1X1, B1Y1);
572 if (isKnownPredicate(CmpInst::ICMP_EQ, Sum, Y->getC()))
573 return false;
574 if (isKnownPredicate(CmpInst::ICMP_NE, Sum, Y->getC())) {
575 X->setEmpty();
576 ++DeltaSuccesses;
577 return true;
578 }
579 return false;
580 }
581
582 llvm_unreachable("shouldn't reach the end of Constraint intersection")__builtin_unreachable();
583 return false;
584}
585
586
587//===----------------------------------------------------------------------===//
588// DependenceInfo methods
589
590// For debugging purposes. Dumps a dependence to OS.
591void Dependence::dump(raw_ostream &OS) const {
592 bool Splitable = false;
593 if (isConfused())
594 OS << "confused";
595 else {
596 if (isConsistent())
597 OS << "consistent ";
598 if (isFlow())
599 OS << "flow";
600 else if (isOutput())
601 OS << "output";
602 else if (isAnti())
603 OS << "anti";
604 else if (isInput())
605 OS << "input";
606 unsigned Levels = getLevels();
607 OS << " [";
608 for (unsigned II = 1; II <= Levels; ++II) {
609 if (isSplitable(II))
610 Splitable = true;
611 if (isPeelFirst(II))
612 OS << 'p';
613 const SCEV *Distance = getDistance(II);
614 if (Distance)
615 OS << *Distance;
616 else if (isScalar(II))
617 OS << "S";
618 else {
619 unsigned Direction = getDirection(II);
620 if (Direction == DVEntry::ALL)
621 OS << "*";
622 else {
623 if (Direction & DVEntry::LT)
624 OS << "<";
625 if (Direction & DVEntry::EQ)
626 OS << "=";
627 if (Direction & DVEntry::GT)
628 OS << ">";
629 }
630 }
631 if (isPeelLast(II))
632 OS << 'p';
633 if (II < Levels)
634 OS << " ";
635 }
636 if (isLoopIndependent())
637 OS << "|<";
638 OS << "]";
639 if (Splitable)
640 OS << " splitable";
641 }
642 OS << "!\n";
643}
644
645// Returns NoAlias/MayAliass/MustAlias for two memory locations based upon their
646// underlaying objects. If LocA and LocB are known to not alias (for any reason:
647// tbaa, non-overlapping regions etc), then it is known there is no dependecy.
648// Otherwise the underlying objects are checked to see if they point to
649// different identifiable objects.
650static AliasResult underlyingObjectsAlias(AAResults *AA,
651 const DataLayout &DL,
652 const MemoryLocation &LocA,
653 const MemoryLocation &LocB) {
654 // Check the original locations (minus size) for noalias, which can happen for
655 // tbaa, incompatible underlying object locations, etc.
656 MemoryLocation LocAS =
657 MemoryLocation::getBeforeOrAfter(LocA.Ptr, LocA.AATags);
658 MemoryLocation LocBS =
659 MemoryLocation::getBeforeOrAfter(LocB.Ptr, LocB.AATags);
660 if (AA->isNoAlias(LocAS, LocBS))
661 return AliasResult::NoAlias;
662
663 // Check the underlying objects are the same
664 const Value *AObj = getUnderlyingObject(LocA.Ptr);
665 const Value *BObj = getUnderlyingObject(LocB.Ptr);
666
667 // If the underlying objects are the same, they must alias
668 if (AObj == BObj)
669 return AliasResult::MustAlias;
670
671 // We may have hit the recursion limit for underlying objects, or have
672 // underlying objects where we don't know they will alias.
673 if (!isIdentifiedObject(AObj) || !isIdentifiedObject(BObj))
674 return AliasResult::MayAlias;
675
676 // Otherwise we know the objects are different and both identified objects so
677 // must not alias.
678 return AliasResult::NoAlias;
679}
680
681
682// Returns true if the load or store can be analyzed. Atomic and volatile
683// operations have properties which this analysis does not understand.
684static
685bool isLoadOrStore(const Instruction *I) {
686 if (const LoadInst *LI
7.1
'LI' is non-null
18.1
'LI' is non-null
7.1
'LI' is non-null
18.1
'LI' is non-null
7.1
'LI' is non-null
18.1
'LI' is non-null
7.1
'LI' is non-null
18.1
'LI' is non-null
7.1
'LI' is non-null
18.1
'LI' is non-null
= dyn_cast<LoadInst>(I))
7
Assuming 'I' is a 'LoadInst'
8
Taking true branch
18
Assuming 'I' is a 'LoadInst'
19
Taking true branch
687 return LI->isUnordered();
9
Calling 'LoadInst::isUnordered'
14
Returning from 'LoadInst::isUnordered'
15
Returning the value 1, which participates in a condition later
20
Calling 'LoadInst::isUnordered'
25
Returning from 'LoadInst::isUnordered'
26
Returning the value 1, which participates in a condition later
688 else if (const StoreInst *SI = dyn_cast<StoreInst>(I))
689 return SI->isUnordered();
690 return false;
691}
692
693
694// Examines the loop nesting of the Src and Dst
695// instructions and establishes their shared loops. Sets the variables
696// CommonLevels, SrcLevels, and MaxLevels.
697// The source and destination instructions needn't be contained in the same
698// loop. The routine establishNestingLevels finds the level of most deeply
699// nested loop that contains them both, CommonLevels. An instruction that's
700// not contained in a loop is at level = 0. MaxLevels is equal to the level
701// of the source plus the level of the destination, minus CommonLevels.
702// This lets us allocate vectors MaxLevels in length, with room for every
703// distinct loop referenced in both the source and destination subscripts.
704// The variable SrcLevels is the nesting depth of the source instruction.
705// It's used to help calculate distinct loops referenced by the destination.
706// Here's the map from loops to levels:
707// 0 - unused
708// 1 - outermost common loop
709// ... - other common loops
710// CommonLevels - innermost common loop
711// ... - loops containing Src but not Dst
712// SrcLevels - innermost loop containing Src but not Dst
713// ... - loops containing Dst but not Src
714// MaxLevels - innermost loops containing Dst but not Src
715// Consider the follow code fragment:
716// for (a = ...) {
717// for (b = ...) {
718// for (c = ...) {
719// for (d = ...) {
720// A[] = ...;
721// }
722// }
723// for (e = ...) {
724// for (f = ...) {
725// for (g = ...) {
726// ... = A[];
727// }
728// }
729// }
730// }
731// }
732// If we're looking at the possibility of a dependence between the store
733// to A (the Src) and the load from A (the Dst), we'll note that they
734// have 2 loops in common, so CommonLevels will equal 2 and the direction
735// vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7.
736// A map from loop names to loop numbers would look like
737// a - 1
738// b - 2 = CommonLevels
739// c - 3
740// d - 4 = SrcLevels
741// e - 5
742// f - 6
743// g - 7 = MaxLevels
744void DependenceInfo::establishNestingLevels(const Instruction *Src,
745 const Instruction *Dst) {
746 const BasicBlock *SrcBlock = Src->getParent();
747 const BasicBlock *DstBlock = Dst->getParent();
748 unsigned SrcLevel = LI->getLoopDepth(SrcBlock);
749 unsigned DstLevel = LI->getLoopDepth(DstBlock);
750 const Loop *SrcLoop = LI->getLoopFor(SrcBlock);
751 const Loop *DstLoop = LI->getLoopFor(DstBlock);
752 SrcLevels = SrcLevel;
753 MaxLevels = SrcLevel + DstLevel;
754 while (SrcLevel > DstLevel) {
755 SrcLoop = SrcLoop->getParentLoop();
756 SrcLevel--;
757 }
758 while (DstLevel > SrcLevel) {
759 DstLoop = DstLoop->getParentLoop();
760 DstLevel--;
761 }
762 while (SrcLoop != DstLoop) {
763 SrcLoop = SrcLoop->getParentLoop();
764 DstLoop = DstLoop->getParentLoop();
765 SrcLevel--;
766 }
767 CommonLevels = SrcLevel;
768 MaxLevels -= CommonLevels;
769}
770
771
772// Given one of the loops containing the source, return
773// its level index in our numbering scheme.
774unsigned DependenceInfo::mapSrcLoop(const Loop *SrcLoop) const {
775 return SrcLoop->getLoopDepth();
776}
777
778
779// Given one of the loops containing the destination,
780// return its level index in our numbering scheme.
781unsigned DependenceInfo::mapDstLoop(const Loop *DstLoop) const {
782 unsigned D = DstLoop->getLoopDepth();
783 if (D > CommonLevels)
784 return D - CommonLevels + SrcLevels;
785 else
786 return D;
787}
788
789
790// Returns true if Expression is loop invariant in LoopNest.
791bool DependenceInfo::isLoopInvariant(const SCEV *Expression,
792 const Loop *LoopNest) const {
793 if (!LoopNest)
794 return true;
795 return SE->isLoopInvariant(Expression, LoopNest) &&
796 isLoopInvariant(Expression, LoopNest->getParentLoop());
797}
798
799
800
801// Finds the set of loops from the LoopNest that
802// have a level <= CommonLevels and are referred to by the SCEV Expression.
803void DependenceInfo::collectCommonLoops(const SCEV *Expression,
804 const Loop *LoopNest,
805 SmallBitVector &Loops) const {
806 while (LoopNest) {
807 unsigned Level = LoopNest->getLoopDepth();
808 if (Level <= CommonLevels && !SE->isLoopInvariant(Expression, LoopNest))
809 Loops.set(Level);
810 LoopNest = LoopNest->getParentLoop();
811 }
812}
813
814void DependenceInfo::unifySubscriptType(ArrayRef<Subscript *> Pairs) {
815
816 unsigned widestWidthSeen = 0;
817 Type *widestType;
818
819 // Go through each pair and find the widest bit to which we need
820 // to extend all of them.
821 for (Subscript *Pair : Pairs) {
822 const SCEV *Src = Pair->Src;
823 const SCEV *Dst = Pair->Dst;
824 IntegerType *SrcTy = dyn_cast<IntegerType>(Src->getType());
825 IntegerType *DstTy = dyn_cast<IntegerType>(Dst->getType());
826 if (SrcTy == nullptr || DstTy == nullptr) {
827 assert(SrcTy == DstTy && "This function only unify integer types and "((void)0)
828 "expect Src and Dst share the same type "((void)0)
829 "otherwise.")((void)0);
830 continue;
831 }
832 if (SrcTy->getBitWidth() > widestWidthSeen) {
833 widestWidthSeen = SrcTy->getBitWidth();
834 widestType = SrcTy;
835 }
836 if (DstTy->getBitWidth() > widestWidthSeen) {
837 widestWidthSeen = DstTy->getBitWidth();
838 widestType = DstTy;
839 }
840 }
841
842
843 assert(widestWidthSeen > 0)((void)0);
844
845 // Now extend each pair to the widest seen.
846 for (Subscript *Pair : Pairs) {
847 const SCEV *Src = Pair->Src;
848 const SCEV *Dst = Pair->Dst;
849 IntegerType *SrcTy = dyn_cast<IntegerType>(Src->getType());
850 IntegerType *DstTy = dyn_cast<IntegerType>(Dst->getType());
851 if (SrcTy == nullptr || DstTy == nullptr) {
852 assert(SrcTy == DstTy && "This function only unify integer types and "((void)0)
853 "expect Src and Dst share the same type "((void)0)
854 "otherwise.")((void)0);
855 continue;
856 }
857 if (SrcTy->getBitWidth() < widestWidthSeen)
858 // Sign-extend Src to widestType
859 Pair->Src = SE->getSignExtendExpr(Src, widestType);
860 if (DstTy->getBitWidth() < widestWidthSeen) {
861 // Sign-extend Dst to widestType
862 Pair->Dst = SE->getSignExtendExpr(Dst, widestType);
863 }
864 }
865}
866
867// removeMatchingExtensions - Examines a subscript pair.
868// If the source and destination are identically sign (or zero)
869// extended, it strips off the extension in an effect to simplify
870// the actual analysis.
871void DependenceInfo::removeMatchingExtensions(Subscript *Pair) {
872 const SCEV *Src = Pair->Src;
873 const SCEV *Dst = Pair->Dst;
874 if ((isa<SCEVZeroExtendExpr>(Src) && isa<SCEVZeroExtendExpr>(Dst)) ||
875 (isa<SCEVSignExtendExpr>(Src) && isa<SCEVSignExtendExpr>(Dst))) {
876 const SCEVIntegralCastExpr *SrcCast = cast<SCEVIntegralCastExpr>(Src);
877 const SCEVIntegralCastExpr *DstCast = cast<SCEVIntegralCastExpr>(Dst);
878 const SCEV *SrcCastOp = SrcCast->getOperand();
879 const SCEV *DstCastOp = DstCast->getOperand();
880 if (SrcCastOp->getType() == DstCastOp->getType()) {
881 Pair->Src = SrcCastOp;
882 Pair->Dst = DstCastOp;
883 }
884 }
885}
886
887// Examine the scev and return true iff it's linear.
888// Collect any loops mentioned in the set of "Loops".
889bool DependenceInfo::checkSubscript(const SCEV *Expr, const Loop *LoopNest,
890 SmallBitVector &Loops, bool IsSrc) {
891 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
892 if (!AddRec)
893 return isLoopInvariant(Expr, LoopNest);
894 const SCEV *Start = AddRec->getStart();
895 const SCEV *Step = AddRec->getStepRecurrence(*SE);
896 const SCEV *UB = SE->getBackedgeTakenCount(AddRec->getLoop());
897 if (!isa<SCEVCouldNotCompute>(UB)) {
898 if (SE->getTypeSizeInBits(Start->getType()) <
899 SE->getTypeSizeInBits(UB->getType())) {
900 if (!AddRec->getNoWrapFlags())
901 return false;
902 }
903 }
904 if (!isLoopInvariant(Step, LoopNest))
905 return false;
906 if (IsSrc)
907 Loops.set(mapSrcLoop(AddRec->getLoop()));
908 else
909 Loops.set(mapDstLoop(AddRec->getLoop()));
910 return checkSubscript(Start, LoopNest, Loops, IsSrc);
911}
912
913// Examine the scev and return true iff it's linear.
914// Collect any loops mentioned in the set of "Loops".
915bool DependenceInfo::checkSrcSubscript(const SCEV *Src, const Loop *LoopNest,
916 SmallBitVector &Loops) {
917 return checkSubscript(Src, LoopNest, Loops, true);
918}
919
920// Examine the scev and return true iff it's linear.
921// Collect any loops mentioned in the set of "Loops".
922bool DependenceInfo::checkDstSubscript(const SCEV *Dst, const Loop *LoopNest,
923 SmallBitVector &Loops) {
924 return checkSubscript(Dst, LoopNest, Loops, false);
925}
926
927
928// Examines the subscript pair (the Src and Dst SCEVs)
929// and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
930// Collects the associated loops in a set.
931DependenceInfo::Subscript::ClassificationKind
932DependenceInfo::classifyPair(const SCEV *Src, const Loop *SrcLoopNest,
933 const SCEV *Dst, const Loop *DstLoopNest,
934 SmallBitVector &Loops) {
935 SmallBitVector SrcLoops(MaxLevels + 1);
936 SmallBitVector DstLoops(MaxLevels + 1);
937 if (!checkSrcSubscript(Src, SrcLoopNest, SrcLoops))
938 return Subscript::NonLinear;
939 if (!checkDstSubscript(Dst, DstLoopNest, DstLoops))
940 return Subscript::NonLinear;
941 Loops = SrcLoops;
942 Loops |= DstLoops;
943 unsigned N = Loops.count();
944 if (N == 0)
945 return Subscript::ZIV;
946 if (N == 1)
947 return Subscript::SIV;
948 if (N == 2 && (SrcLoops.count() == 0 ||
949 DstLoops.count() == 0 ||
950 (SrcLoops.count() == 1 && DstLoops.count() == 1)))
951 return Subscript::RDIV;
952 return Subscript::MIV;
953}
954
955
956// A wrapper around SCEV::isKnownPredicate.
957// Looks for cases where we're interested in comparing for equality.
958// If both X and Y have been identically sign or zero extended,
959// it strips off the (confusing) extensions before invoking
960// SCEV::isKnownPredicate. Perhaps, someday, the ScalarEvolution package
961// will be similarly updated.
962//
963// If SCEV::isKnownPredicate can't prove the predicate,
964// we try simple subtraction, which seems to help in some cases
965// involving symbolics.
966bool DependenceInfo::isKnownPredicate(ICmpInst::Predicate Pred, const SCEV *X,
967 const SCEV *Y) const {
968 if (Pred == CmpInst::ICMP_EQ ||
969 Pred == CmpInst::ICMP_NE) {
970 if ((isa<SCEVSignExtendExpr>(X) &&
971 isa<SCEVSignExtendExpr>(Y)) ||
972 (isa<SCEVZeroExtendExpr>(X) &&
973 isa<SCEVZeroExtendExpr>(Y))) {
974 const SCEVIntegralCastExpr *CX = cast<SCEVIntegralCastExpr>(X);
975 const SCEVIntegralCastExpr *CY = cast<SCEVIntegralCastExpr>(Y);
976 const SCEV *Xop = CX->getOperand();
977 const SCEV *Yop = CY->getOperand();
978 if (Xop->getType() == Yop->getType()) {
979 X = Xop;
980 Y = Yop;
981 }
982 }
983 }
984 if (SE->isKnownPredicate(Pred, X, Y))
985 return true;
986 // If SE->isKnownPredicate can't prove the condition,
987 // we try the brute-force approach of subtracting
988 // and testing the difference.
989 // By testing with SE->isKnownPredicate first, we avoid
990 // the possibility of overflow when the arguments are constants.
991 const SCEV *Delta = SE->getMinusSCEV(X, Y);
992 switch (Pred) {
993 case CmpInst::ICMP_EQ:
994 return Delta->isZero();
995 case CmpInst::ICMP_NE:
996 return SE->isKnownNonZero(Delta);
997 case CmpInst::ICMP_SGE:
998 return SE->isKnownNonNegative(Delta);
999 case CmpInst::ICMP_SLE:
1000 return SE->isKnownNonPositive(Delta);
1001 case CmpInst::ICMP_SGT:
1002 return SE->isKnownPositive(Delta);
1003 case CmpInst::ICMP_SLT:
1004 return SE->isKnownNegative(Delta);
1005 default:
1006 llvm_unreachable("unexpected predicate in isKnownPredicate")__builtin_unreachable();
1007 }
1008}
1009
1010/// Compare to see if S is less than Size, using isKnownNegative(S - max(Size, 1))
1011/// with some extra checking if S is an AddRec and we can prove less-than using
1012/// the loop bounds.
1013bool DependenceInfo::isKnownLessThan(const SCEV *S, const SCEV *Size) const {
1014 // First unify to the same type
1015 auto *SType = dyn_cast<IntegerType>(S->getType());
1016 auto *SizeType = dyn_cast<IntegerType>(Size->getType());
1017 if (!SType || !SizeType)
1018 return false;
1019 Type *MaxType =
1020 (SType->getBitWidth() >= SizeType->getBitWidth()) ? SType : SizeType;
1021 S = SE->getTruncateOrZeroExtend(S, MaxType);
1022 Size = SE->getTruncateOrZeroExtend(Size, MaxType);
1023
1024 // Special check for addrecs using BE taken count
1025 const SCEV *Bound = SE->getMinusSCEV(S, Size);
1026 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Bound)) {
1027 if (AddRec->isAffine()) {
1028 const SCEV *BECount = SE->getBackedgeTakenCount(AddRec->getLoop());
1029 if (!isa<SCEVCouldNotCompute>(BECount)) {
1030 const SCEV *Limit = AddRec->evaluateAtIteration(BECount, *SE);
1031 if (SE->isKnownNegative(Limit))
1032 return true;
1033 }
1034 }
1035 }
1036
1037 // Check using normal isKnownNegative
1038 const SCEV *LimitedBound =
1039 SE->getMinusSCEV(S, SE->getSMaxExpr(Size, SE->getOne(Size->getType())));
1040 return SE->isKnownNegative(LimitedBound);
1041}
1042
1043bool DependenceInfo::isKnownNonNegative(const SCEV *S, const Value *Ptr) const {
1044 bool Inbounds = false;
1045 if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(Ptr))
1046 Inbounds = SrcGEP->isInBounds();
1047 if (Inbounds) {
1048 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
1049 if (AddRec->isAffine()) {
1050 // We know S is for Ptr, the operand on a load/store, so doesn't wrap.
1051 // If both parts are NonNegative, the end result will be NonNegative
1052 if (SE->isKnownNonNegative(AddRec->getStart()) &&
1053 SE->isKnownNonNegative(AddRec->getOperand(1)))
1054 return true;
1055 }
1056 }
1057 }
1058
1059 return SE->isKnownNonNegative(S);
1060}
1061
1062// All subscripts are all the same type.
1063// Loop bound may be smaller (e.g., a char).
1064// Should zero extend loop bound, since it's always >= 0.
1065// This routine collects upper bound and extends or truncates if needed.
1066// Truncating is safe when subscripts are known not to wrap. Cases without
1067// nowrap flags should have been rejected earlier.
1068// Return null if no bound available.
1069const SCEV *DependenceInfo::collectUpperBound(const Loop *L, Type *T) const {
1070 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
1071 const SCEV *UB = SE->getBackedgeTakenCount(L);
1072 return SE->getTruncateOrZeroExtend(UB, T);
1073 }
1074 return nullptr;
1075}
1076
1077
1078// Calls collectUpperBound(), then attempts to cast it to SCEVConstant.
1079// If the cast fails, returns NULL.
1080const SCEVConstant *DependenceInfo::collectConstantUpperBound(const Loop *L,
1081 Type *T) const {
1082 if (const SCEV *UB = collectUpperBound(L, T))
1083 return dyn_cast<SCEVConstant>(UB);
1084 return nullptr;
1085}
1086
1087
1088// testZIV -
1089// When we have a pair of subscripts of the form [c1] and [c2],
1090// where c1 and c2 are both loop invariant, we attack it using
1091// the ZIV test. Basically, we test by comparing the two values,
1092// but there are actually three possible results:
1093// 1) the values are equal, so there's a dependence
1094// 2) the values are different, so there's no dependence
1095// 3) the values might be equal, so we have to assume a dependence.
1096//
1097// Return true if dependence disproved.
1098bool DependenceInfo::testZIV(const SCEV *Src, const SCEV *Dst,
1099 FullDependence &Result) const {
1100 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n")do { } while (false);
1101 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n")do { } while (false);
1102 ++ZIVapplications;
1103 if (isKnownPredicate(CmpInst::ICMP_EQ, Src, Dst)) {
1104 LLVM_DEBUG(dbgs() << " provably dependent\n")do { } while (false);
1105 return false; // provably dependent
1106 }
1107 if (isKnownPredicate(CmpInst::ICMP_NE, Src, Dst)) {
1108 LLVM_DEBUG(dbgs() << " provably independent\n")do { } while (false);
1109 ++ZIVindependence;
1110 return true; // provably independent
1111 }
1112 LLVM_DEBUG(dbgs() << " possibly dependent\n")do { } while (false);
1113 Result.Consistent = false;
1114 return false; // possibly dependent
1115}
1116
1117
1118// strongSIVtest -
1119// From the paper, Practical Dependence Testing, Section 4.2.1
1120//
1121// When we have a pair of subscripts of the form [c1 + a*i] and [c2 + a*i],
1122// where i is an induction variable, c1 and c2 are loop invariant,
1123// and a is a constant, we can solve it exactly using the Strong SIV test.
1124//
1125// Can prove independence. Failing that, can compute distance (and direction).
1126// In the presence of symbolic terms, we can sometimes make progress.
1127//
1128// If there's a dependence,
1129//
1130// c1 + a*i = c2 + a*i'
1131//
1132// The dependence distance is
1133//
1134// d = i' - i = (c1 - c2)/a
1135//
1136// A dependence only exists if d is an integer and abs(d) <= U, where U is the
1137// loop's upper bound. If a dependence exists, the dependence direction is
1138// defined as
1139//
1140// { < if d > 0
1141// direction = { = if d = 0
1142// { > if d < 0
1143//
1144// Return true if dependence disproved.
1145bool DependenceInfo::strongSIVtest(const SCEV *Coeff, const SCEV *SrcConst,
1146 const SCEV *DstConst, const Loop *CurLoop,
1147 unsigned Level, FullDependence &Result,
1148 Constraint &NewConstraint) const {
1149 LLVM_DEBUG(dbgs() << "\tStrong SIV test\n")do { } while (false);
1150 LLVM_DEBUG(dbgs() << "\t Coeff = " << *Coeff)do { } while (false);
1151 LLVM_DEBUG(dbgs() << ", " << *Coeff->getType() << "\n")do { } while (false);
1152 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst)do { } while (false);
1153 LLVM_DEBUG(dbgs() << ", " << *SrcConst->getType() << "\n")do { } while (false);
1154 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst)do { } while (false);
1155 LLVM_DEBUG(dbgs() << ", " << *DstConst->getType() << "\n")do { } while (false);
1156 ++StrongSIVapplications;
1157 assert(0 < Level && Level <= CommonLevels && "level out of range")((void)0);
1158 Level--;
1159
1160 const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst);
1161 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta)do { } while (false);
1162 LLVM_DEBUG(dbgs() << ", " << *Delta->getType() << "\n")do { } while (false);
1163
1164 // check that |Delta| < iteration count
1165 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1166 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound)do { } while (false);
1167 LLVM_DEBUG(dbgs() << ", " << *UpperBound->getType() << "\n")do { } while (false);
1168 const SCEV *AbsDelta =
1169 SE->isKnownNonNegative(Delta) ? Delta : SE->getNegativeSCEV(Delta);
1170 const SCEV *AbsCoeff =
1171 SE->isKnownNonNegative(Coeff) ? Coeff : SE->getNegativeSCEV(Coeff);
1172 const SCEV *Product = SE->getMulExpr(UpperBound, AbsCoeff);
1173 if (isKnownPredicate(CmpInst::ICMP_SGT, AbsDelta, Product)) {
1174 // Distance greater than trip count - no dependence
1175 ++StrongSIVindependence;
1176 ++StrongSIVsuccesses;
1177 return true;
1178 }
1179 }
1180
1181 // Can we compute distance?
1182 if (isa<SCEVConstant>(Delta) && isa<SCEVConstant>(Coeff)) {
1183 APInt ConstDelta = cast<SCEVConstant>(Delta)->getAPInt();
1184 APInt ConstCoeff = cast<SCEVConstant>(Coeff)->getAPInt();
1185 APInt Distance = ConstDelta; // these need to be initialized
1186 APInt Remainder = ConstDelta;
1187 APInt::sdivrem(ConstDelta, ConstCoeff, Distance, Remainder);
1188 LLVM_DEBUG(dbgs() << "\t Distance = " << Distance << "\n")do { } while (false);
1189 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n")do { } while (false);
1190 // Make sure Coeff divides Delta exactly
1191 if (Remainder != 0) {
1192 // Coeff doesn't divide Distance, no dependence
1193 ++StrongSIVindependence;
1194 ++StrongSIVsuccesses;
1195 return true;
1196 }
1197 Result.DV[Level].Distance = SE->getConstant(Distance);
1198 NewConstraint.setDistance(SE->getConstant(Distance), CurLoop);
1199 if (Distance.sgt(0))
1200 Result.DV[Level].Direction &= Dependence::DVEntry::LT;
1201 else if (Distance.slt(0))
1202 Result.DV[Level].Direction &= Dependence::DVEntry::GT;
1203 else
1204 Result.DV[Level].Direction &= Dependence::DVEntry::EQ;
1205 ++StrongSIVsuccesses;
1206 }
1207 else if (Delta->isZero()) {
1208 // since 0/X == 0
1209 Result.DV[Level].Distance = Delta;
1210 NewConstraint.setDistance(Delta, CurLoop);
1211 Result.DV[Level].Direction &= Dependence::DVEntry::EQ;
1212 ++StrongSIVsuccesses;
1213 }
1214 else {
1215 if (Coeff->isOne()) {
1216 LLVM_DEBUG(dbgs() << "\t Distance = " << *Delta << "\n")do { } while (false);
1217 Result.DV[Level].Distance = Delta; // since X/1 == X
1218 NewConstraint.setDistance(Delta, CurLoop);
1219 }
1220 else {
1221 Result.Consistent = false;
1222 NewConstraint.setLine(Coeff,
1223 SE->getNegativeSCEV(Coeff),
1224 SE->getNegativeSCEV(Delta), CurLoop);
1225 }
1226
1227 // maybe we can get a useful direction
1228 bool DeltaMaybeZero = !SE->isKnownNonZero(Delta);
1229 bool DeltaMaybePositive = !SE->isKnownNonPositive(Delta);
1230 bool DeltaMaybeNegative = !SE->isKnownNonNegative(Delta);
1231 bool CoeffMaybePositive = !SE->isKnownNonPositive(Coeff);
1232 bool CoeffMaybeNegative = !SE->isKnownNonNegative(Coeff);
1233 // The double negatives above are confusing.
1234 // It helps to read !SE->isKnownNonZero(Delta)
1235 // as "Delta might be Zero"
1236 unsigned NewDirection = Dependence::DVEntry::NONE;
1237 if ((DeltaMaybePositive && CoeffMaybePositive) ||
1238 (DeltaMaybeNegative && CoeffMaybeNegative))
1239 NewDirection = Dependence::DVEntry::LT;
1240 if (DeltaMaybeZero)
1241 NewDirection |= Dependence::DVEntry::EQ;
1242 if ((DeltaMaybeNegative && CoeffMaybePositive) ||
1243 (DeltaMaybePositive && CoeffMaybeNegative))
1244 NewDirection |= Dependence::DVEntry::GT;
1245 if (NewDirection < Result.DV[Level].Direction)
1246 ++StrongSIVsuccesses;
1247 Result.DV[Level].Direction &= NewDirection;
1248 }
1249 return false;
1250}
1251
1252
1253// weakCrossingSIVtest -
1254// From the paper, Practical Dependence Testing, Section 4.2.2
1255//
1256// When we have a pair of subscripts of the form [c1 + a*i] and [c2 - a*i],
1257// where i is an induction variable, c1 and c2 are loop invariant,
1258// and a is a constant, we can solve it exactly using the
1259// Weak-Crossing SIV test.
1260//
1261// Given c1 + a*i = c2 - a*i', we can look for the intersection of
1262// the two lines, where i = i', yielding
1263//
1264// c1 + a*i = c2 - a*i
1265// 2a*i = c2 - c1
1266// i = (c2 - c1)/2a
1267//
1268// If i < 0, there is no dependence.
1269// If i > upperbound, there is no dependence.
1270// If i = 0 (i.e., if c1 = c2), there's a dependence with distance = 0.
1271// If i = upperbound, there's a dependence with distance = 0.
1272// If i is integral, there's a dependence (all directions).
1273// If the non-integer part = 1/2, there's a dependence (<> directions).
1274// Otherwise, there's no dependence.
1275//
1276// Can prove independence. Failing that,
1277// can sometimes refine the directions.
1278// Can determine iteration for splitting.
1279//
1280// Return true if dependence disproved.
1281bool DependenceInfo::weakCrossingSIVtest(
1282 const SCEV *Coeff, const SCEV *SrcConst, const SCEV *DstConst,
1283 const Loop *CurLoop, unsigned Level, FullDependence &Result,
1284 Constraint &NewConstraint, const SCEV *&SplitIter) const {
1285 LLVM_DEBUG(dbgs() << "\tWeak-Crossing SIV test\n")do { } while (false);
1286 LLVM_DEBUG(dbgs() << "\t Coeff = " << *Coeff << "\n")do { } while (false);
1287 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n")do { } while (false);
1288 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n")do { } while (false);
1289 ++WeakCrossingSIVapplications;
1290 assert(0 < Level && Level <= CommonLevels && "Level out of range")((void)0);
1291 Level--;
1292 Result.Consistent = false;
1293 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1294 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n")do { } while (false);
1295 NewConstraint.setLine(Coeff, Coeff, Delta, CurLoop);
1296 if (Delta->isZero()) {
1297 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::LT);
1298 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::GT);
1299 ++WeakCrossingSIVsuccesses;
1300 if (!Result.DV[Level].Direction) {
1301 ++WeakCrossingSIVindependence;
1302 return true;
1303 }
1304 Result.DV[Level].Distance = Delta; // = 0
1305 return false;
1306 }
1307 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(Coeff);
1308 if (!ConstCoeff)
1309 return false;
1310
1311 Result.DV[Level].Splitable = true;
1312 if (SE->isKnownNegative(ConstCoeff)) {
1313 ConstCoeff = dyn_cast<SCEVConstant>(SE->getNegativeSCEV(ConstCoeff));
1314 assert(ConstCoeff &&((void)0)
1315 "dynamic cast of negative of ConstCoeff should yield constant")((void)0);
1316 Delta = SE->getNegativeSCEV(Delta);
1317 }
1318 assert(SE->isKnownPositive(ConstCoeff) && "ConstCoeff should be positive")((void)0);
1319
1320 // compute SplitIter for use by DependenceInfo::getSplitIteration()
1321 SplitIter = SE->getUDivExpr(
1322 SE->getSMaxExpr(SE->getZero(Delta->getType()), Delta),
1323 SE->getMulExpr(SE->getConstant(Delta->getType(), 2), ConstCoeff));
1324 LLVM_DEBUG(dbgs() << "\t Split iter = " << *SplitIter << "\n")do { } while (false);
1325
1326 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1327 if (!ConstDelta)
1328 return false;
1329
1330 // We're certain that ConstCoeff > 0; therefore,
1331 // if Delta < 0, then no dependence.
1332 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n")do { } while (false);
1333 LLVM_DEBUG(dbgs() << "\t ConstCoeff = " << *ConstCoeff << "\n")do { } while (false);
1334 if (SE->isKnownNegative(Delta)) {
1335 // No dependence, Delta < 0
1336 ++WeakCrossingSIVindependence;
1337 ++WeakCrossingSIVsuccesses;
1338 return true;
1339 }
1340
1341 // We're certain that Delta > 0 and ConstCoeff > 0.
1342 // Check Delta/(2*ConstCoeff) against upper loop bound
1343 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1344 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n")do { } while (false);
1345 const SCEV *ConstantTwo = SE->getConstant(UpperBound->getType(), 2);
1346 const SCEV *ML = SE->getMulExpr(SE->getMulExpr(ConstCoeff, UpperBound),
1347 ConstantTwo);
1348 LLVM_DEBUG(dbgs() << "\t ML = " << *ML << "\n")do { } while (false);
1349 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, ML)) {
1350 // Delta too big, no dependence
1351 ++WeakCrossingSIVindependence;
1352 ++WeakCrossingSIVsuccesses;
1353 return true;
1354 }
1355 if (isKnownPredicate(CmpInst::ICMP_EQ, Delta, ML)) {
1356 // i = i' = UB
1357 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::LT);
1358 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::GT);
1359 ++WeakCrossingSIVsuccesses;
1360 if (!Result.DV[Level].Direction) {
1361 ++WeakCrossingSIVindependence;
1362 return true;
1363 }
1364 Result.DV[Level].Splitable = false;
1365 Result.DV[Level].Distance = SE->getZero(Delta->getType());
1366 return false;
1367 }
1368 }
1369
1370 // check that Coeff divides Delta
1371 APInt APDelta = ConstDelta->getAPInt();
1372 APInt APCoeff = ConstCoeff->getAPInt();
1373 APInt Distance = APDelta; // these need to be initialzed
1374 APInt Remainder = APDelta;
1375 APInt::sdivrem(APDelta, APCoeff, Distance, Remainder);
1376 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n")do { } while (false);
1377 if (Remainder != 0) {
1378 // Coeff doesn't divide Delta, no dependence
1379 ++WeakCrossingSIVindependence;
1380 ++WeakCrossingSIVsuccesses;
1381 return true;
1382 }
1383 LLVM_DEBUG(dbgs() << "\t Distance = " << Distance << "\n")do { } while (false);
1384
1385 // if 2*Coeff doesn't divide Delta, then the equal direction isn't possible
1386 APInt Two = APInt(Distance.getBitWidth(), 2, true);
1387 Remainder = Distance.srem(Two);
1388 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n")do { } while (false);
1389 if (Remainder != 0) {
1390 // Equal direction isn't possible
1391 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::EQ);
1392 ++WeakCrossingSIVsuccesses;
1393 }
1394 return false;
1395}
1396
1397
1398// Kirch's algorithm, from
1399//
1400// Optimizing Supercompilers for Supercomputers
1401// Michael Wolfe
1402// MIT Press, 1989
1403//
1404// Program 2.1, page 29.
1405// Computes the GCD of AM and BM.
1406// Also finds a solution to the equation ax - by = gcd(a, b).
1407// Returns true if dependence disproved; i.e., gcd does not divide Delta.
1408static bool findGCD(unsigned Bits, const APInt &AM, const APInt &BM,
1409 const APInt &Delta, APInt &G, APInt &X, APInt &Y) {
1410 APInt A0(Bits, 1, true), A1(Bits, 0, true);
1411 APInt B0(Bits, 0, true), B1(Bits, 1, true);
1412 APInt G0 = AM.abs();
1413 APInt G1 = BM.abs();
1414 APInt Q = G0; // these need to be initialized
1415 APInt R = G0;
1416 APInt::sdivrem(G0, G1, Q, R);
1417 while (R != 0) {
1418 APInt A2 = A0 - Q*A1; A0 = A1; A1 = A2;
1419 APInt B2 = B0 - Q*B1; B0 = B1; B1 = B2;
1420 G0 = G1; G1 = R;
1421 APInt::sdivrem(G0, G1, Q, R);
1422 }
1423 G = G1;
1424 LLVM_DEBUG(dbgs() << "\t GCD = " << G << "\n")do { } while (false);
1425 X = AM.slt(0) ? -A1 : A1;
1426 Y = BM.slt(0) ? B1 : -B1;
1427
1428 // make sure gcd divides Delta
1429 R = Delta.srem(G);
1430 if (R != 0)
1431 return true; // gcd doesn't divide Delta, no dependence
1432 Q = Delta.sdiv(G);
1433 return false;
1434}
1435
1436static APInt floorOfQuotient(const APInt &A, const APInt &B) {
1437 APInt Q = A; // these need to be initialized
1438 APInt R = A;
1439 APInt::sdivrem(A, B, Q, R);
1440 if (R == 0)
1441 return Q;
1442 if ((A.sgt(0) && B.sgt(0)) ||
1443 (A.slt(0) && B.slt(0)))
1444 return Q;
1445 else
1446 return Q - 1;
1447}
1448
1449static APInt ceilingOfQuotient(const APInt &A, const APInt &B) {
1450 APInt Q = A; // these need to be initialized
1451 APInt R = A;
1452 APInt::sdivrem(A, B, Q, R);
1453 if (R == 0)
1454 return Q;
1455 if ((A.sgt(0) && B.sgt(0)) ||
1456 (A.slt(0) && B.slt(0)))
1457 return Q + 1;
1458 else
1459 return Q;
1460}
1461
1462// exactSIVtest -
1463// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*i],
1464// where i is an induction variable, c1 and c2 are loop invariant, and a1
1465// and a2 are constant, we can solve it exactly using an algorithm developed
1466// by Banerjee and Wolfe. See Algorithm 6.2.1 (case 2.5) in:
1467//
1468// Dependence Analysis for Supercomputing
1469// Utpal Banerjee
1470// Kluwer Academic Publishers, 1988
1471//
1472// It's slower than the specialized tests (strong SIV, weak-zero SIV, etc),
1473// so use them if possible. They're also a bit better with symbolics and,
1474// in the case of the strong SIV test, can compute Distances.
1475//
1476// Return true if dependence disproved.
1477//
1478// This is a modified version of the original Banerjee algorithm. The original
1479// only tested whether Dst depends on Src. This algorithm extends that and
1480// returns all the dependencies that exist between Dst and Src.
1481bool DependenceInfo::exactSIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
1482 const SCEV *SrcConst, const SCEV *DstConst,
1483 const Loop *CurLoop, unsigned Level,
1484 FullDependence &Result,
1485 Constraint &NewConstraint) const {
1486 LLVM_DEBUG(dbgs() << "\tExact SIV test\n")do { } while (false);
1487 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n")do { } while (false);
1488 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n")do { } while (false);
1489 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n")do { } while (false);
1490 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n")do { } while (false);
1491 ++ExactSIVapplications;
1492 assert(0 < Level && Level <= CommonLevels && "Level out of range")((void)0);
1493 Level--;
1494 Result.Consistent = false;
1495 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1496 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n")do { } while (false);
1497 NewConstraint.setLine(SrcCoeff, SE->getNegativeSCEV(DstCoeff), Delta,
1498 CurLoop);
1499 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1500 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1501 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1502 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
1503 return false;
1504
1505 // find gcd
1506 APInt G, X, Y;
1507 APInt AM = ConstSrcCoeff->getAPInt();
1508 APInt BM = ConstDstCoeff->getAPInt();
1509 APInt CM = ConstDelta->getAPInt();
1510 unsigned Bits = AM.getBitWidth();
1511 if (findGCD(Bits, AM, BM, CM, G, X, Y)) {
1512 // gcd doesn't divide Delta, no dependence
1513 ++ExactSIVindependence;
1514 ++ExactSIVsuccesses;
1515 return true;
1516 }
1517
1518 LLVM_DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n")do { } while (false);
1519
1520 // since SCEV construction normalizes, LM = 0
1521 APInt UM(Bits, 1, true);
1522 bool UMValid = false;
1523 // UM is perhaps unavailable, let's check
1524 if (const SCEVConstant *CUB =
1525 collectConstantUpperBound(CurLoop, Delta->getType())) {
1526 UM = CUB->getAPInt();
1527 LLVM_DEBUG(dbgs() << "\t UM = " << UM << "\n")do { } while (false);
1528 UMValid = true;
1529 }
1530
1531 APInt TU(APInt::getSignedMaxValue(Bits));
1532 APInt TL(APInt::getSignedMinValue(Bits));
1533 APInt TC = CM.sdiv(G);
1534 APInt TX = X * TC;
1535 APInt TY = Y * TC;
1536 LLVM_DEBUG(dbgs() << "\t TC = " << TC << "\n")do { } while (false);
1537 LLVM_DEBUG(dbgs() << "\t TX = " << TX << "\n")do { } while (false);
1538 LLVM_DEBUG(dbgs() << "\t TY = " << TY << "\n")do { } while (false);
1539
1540 SmallVector<APInt, 2> TLVec, TUVec;
1541 APInt TB = BM.sdiv(G);
1542 if (TB.sgt(0)) {
1543 TLVec.push_back(ceilingOfQuotient(-TX, TB));
1544 LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n")do { } while (false);
1545 // New bound check - modification to Banerjee's e3 check
1546 if (UMValid) {
1547 TUVec.push_back(floorOfQuotient(UM - TX, TB));
1548 LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n")do { } while (false);
1549 }
1550 } else {
1551 TUVec.push_back(floorOfQuotient(-TX, TB));
1552 LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n")do { } while (false);
1553 // New bound check - modification to Banerjee's e3 check
1554 if (UMValid) {
1555 TLVec.push_back(ceilingOfQuotient(UM - TX, TB));
1556 LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n")do { } while (false);
1557 }
1558 }
1559
1560 APInt TA = AM.sdiv(G);
1561 if (TA.sgt(0)) {
1562 if (UMValid) {
1563 TUVec.push_back(floorOfQuotient(UM - TY, TA));
1564 LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n")do { } while (false);
1565 }
1566 // New bound check - modification to Banerjee's e3 check
1567 TLVec.push_back(ceilingOfQuotient(-TY, TA));
1568 LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n")do { } while (false);
1569 } else {
1570 if (UMValid) {
1571 TLVec.push_back(ceilingOfQuotient(UM - TY, TA));
1572 LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n")do { } while (false);
1573 }
1574 // New bound check - modification to Banerjee's e3 check
1575 TUVec.push_back(floorOfQuotient(-TY, TA));
1576 LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n")do { } while (false);
1577 }
1578
1579 LLVM_DEBUG(dbgs() << "\t TA = " << TA << "\n")do { } while (false);
1580 LLVM_DEBUG(dbgs() << "\t TB = " << TB << "\n")do { } while (false);
1581
1582 if (TLVec.empty() || TUVec.empty())
1583 return false;
1584 TL = APIntOps::smax(TLVec.front(), TLVec.back());
1585 TU = APIntOps::smin(TUVec.front(), TUVec.back());
1586 LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n")do { } while (false);
1587 LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n")do { } while (false);
1588
1589 if (TL.sgt(TU)) {
1590 ++ExactSIVindependence;
1591 ++ExactSIVsuccesses;
1592 return true;
1593 }
1594
1595 // explore directions
1596 unsigned NewDirection = Dependence::DVEntry::NONE;
1597 APInt LowerDistance, UpperDistance;
1598 if (TA.sgt(TB)) {
1599 LowerDistance = (TY - TX) + (TA - TB) * TL;
1600 UpperDistance = (TY - TX) + (TA - TB) * TU;
1601 } else {
1602 LowerDistance = (TY - TX) + (TA - TB) * TU;
1603 UpperDistance = (TY - TX) + (TA - TB) * TL;
1604 }
1605
1606 LLVM_DEBUG(dbgs() << "\t LowerDistance = " << LowerDistance << "\n")do { } while (false);
1607 LLVM_DEBUG(dbgs() << "\t UpperDistance = " << UpperDistance << "\n")do { } while (false);
1608
1609 APInt Zero(Bits, 0, true);
1610 if (LowerDistance.sle(Zero) && UpperDistance.sge(Zero)) {
1611 NewDirection |= Dependence::DVEntry::EQ;
1612 ++ExactSIVsuccesses;
1613 }
1614 if (LowerDistance.slt(0)) {
1615 NewDirection |= Dependence::DVEntry::GT;
1616 ++ExactSIVsuccesses;
1617 }
1618 if (UpperDistance.sgt(0)) {
1619 NewDirection |= Dependence::DVEntry::LT;
1620 ++ExactSIVsuccesses;
1621 }
1622
1623 // finished
1624 Result.DV[Level].Direction &= NewDirection;
1625 if (Result.DV[Level].Direction == Dependence::DVEntry::NONE)
1626 ++ExactSIVindependence;
1627 LLVM_DEBUG(dbgs() << "\t Result = ")do { } while (false);
1628 LLVM_DEBUG(Result.dump(dbgs()))do { } while (false);
1629 return Result.DV[Level].Direction == Dependence::DVEntry::NONE;
1630}
1631
1632
1633// Return true if the divisor evenly divides the dividend.
1634static
1635bool isRemainderZero(const SCEVConstant *Dividend,
1636 const SCEVConstant *Divisor) {
1637 const APInt &ConstDividend = Dividend->getAPInt();
1638 const APInt &ConstDivisor = Divisor->getAPInt();
1639 return ConstDividend.srem(ConstDivisor) == 0;
1640}
1641
1642
1643// weakZeroSrcSIVtest -
1644// From the paper, Practical Dependence Testing, Section 4.2.2
1645//
1646// When we have a pair of subscripts of the form [c1] and [c2 + a*i],
1647// where i is an induction variable, c1 and c2 are loop invariant,
1648// and a is a constant, we can solve it exactly using the
1649// Weak-Zero SIV test.
1650//
1651// Given
1652//
1653// c1 = c2 + a*i
1654//
1655// we get
1656//
1657// (c1 - c2)/a = i
1658//
1659// If i is not an integer, there's no dependence.
1660// If i < 0 or > UB, there's no dependence.
1661// If i = 0, the direction is >= and peeling the
1662// 1st iteration will break the dependence.
1663// If i = UB, the direction is <= and peeling the
1664// last iteration will break the dependence.
1665// Otherwise, the direction is *.
1666//
1667// Can prove independence. Failing that, we can sometimes refine
1668// the directions. Can sometimes show that first or last
1669// iteration carries all the dependences (so worth peeling).
1670//
1671// (see also weakZeroDstSIVtest)
1672//
1673// Return true if dependence disproved.
1674bool DependenceInfo::weakZeroSrcSIVtest(const SCEV *DstCoeff,
1675 const SCEV *SrcConst,
1676 const SCEV *DstConst,
1677 const Loop *CurLoop, unsigned Level,
1678 FullDependence &Result,
1679 Constraint &NewConstraint) const {
1680 // For the WeakSIV test, it's possible the loop isn't common to
1681 // the Src and Dst loops. If it isn't, then there's no need to
1682 // record a direction.
1683 LLVM_DEBUG(dbgs() << "\tWeak-Zero (src) SIV test\n")do { } while (false);
1684 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << "\n")do { } while (false);
1685 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n")do { } while (false);
1686 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n")do { } while (false);
1687 ++WeakZeroSIVapplications;
1688 assert(0 < Level && Level <= MaxLevels && "Level out of range")((void)0);
1689 Level--;
1690 Result.Consistent = false;
1691 const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst);
1692 NewConstraint.setLine(SE->getZero(Delta->getType()), DstCoeff, Delta,
1693 CurLoop);
1694 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n")do { } while (false);
1695 if (isKnownPredicate(CmpInst::ICMP_EQ, SrcConst, DstConst)) {
1696 if (Level < CommonLevels) {
1697 Result.DV[Level].Direction &= Dependence::DVEntry::GE;
1698 Result.DV[Level].PeelFirst = true;
1699 ++WeakZeroSIVsuccesses;
1700 }
1701 return false; // dependences caused by first iteration
1702 }
1703 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1704 if (!ConstCoeff)
1705 return false;
1706 const SCEV *AbsCoeff =
1707 SE->isKnownNegative(ConstCoeff) ?
1708 SE->getNegativeSCEV(ConstCoeff) : ConstCoeff;
1709 const SCEV *NewDelta =
1710 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
1711
1712 // check that Delta/SrcCoeff < iteration count
1713 // really check NewDelta < count*AbsCoeff
1714 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1715 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n")do { } while (false);
1716 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
1717 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) {
1718 ++WeakZeroSIVindependence;
1719 ++WeakZeroSIVsuccesses;
1720 return true;
1721 }
1722 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
1723 // dependences caused by last iteration
1724 if (Level < CommonLevels) {
1725 Result.DV[Level].Direction &= Dependence::DVEntry::LE;
1726 Result.DV[Level].PeelLast = true;
1727 ++WeakZeroSIVsuccesses;
1728 }
1729 return false;
1730 }
1731 }
1732
1733 // check that Delta/SrcCoeff >= 0
1734 // really check that NewDelta >= 0
1735 if (SE->isKnownNegative(NewDelta)) {
1736 // No dependence, newDelta < 0
1737 ++WeakZeroSIVindependence;
1738 ++WeakZeroSIVsuccesses;
1739 return true;
1740 }
1741
1742 // if SrcCoeff doesn't divide Delta, then no dependence
1743 if (isa<SCEVConstant>(Delta) &&
1744 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
1745 ++WeakZeroSIVindependence;
1746 ++WeakZeroSIVsuccesses;
1747 return true;
1748 }
1749 return false;
1750}
1751
1752
1753// weakZeroDstSIVtest -
1754// From the paper, Practical Dependence Testing, Section 4.2.2
1755//
1756// When we have a pair of subscripts of the form [c1 + a*i] and [c2],
1757// where i is an induction variable, c1 and c2 are loop invariant,
1758// and a is a constant, we can solve it exactly using the
1759// Weak-Zero SIV test.
1760//
1761// Given
1762//
1763// c1 + a*i = c2
1764//
1765// we get
1766//
1767// i = (c2 - c1)/a
1768//
1769// If i is not an integer, there's no dependence.
1770// If i < 0 or > UB, there's no dependence.
1771// If i = 0, the direction is <= and peeling the
1772// 1st iteration will break the dependence.
1773// If i = UB, the direction is >= and peeling the
1774// last iteration will break the dependence.
1775// Otherwise, the direction is *.
1776//
1777// Can prove independence. Failing that, we can sometimes refine
1778// the directions. Can sometimes show that first or last
1779// iteration carries all the dependences (so worth peeling).
1780//
1781// (see also weakZeroSrcSIVtest)
1782//
1783// Return true if dependence disproved.
1784bool DependenceInfo::weakZeroDstSIVtest(const SCEV *SrcCoeff,
1785 const SCEV *SrcConst,
1786 const SCEV *DstConst,
1787 const Loop *CurLoop, unsigned Level,
1788 FullDependence &Result,
1789 Constraint &NewConstraint) const {
1790 // For the WeakSIV test, it's possible the loop isn't common to the
1791 // Src and Dst loops. If it isn't, then there's no need to record a direction.
1792 LLVM_DEBUG(dbgs() << "\tWeak-Zero (dst) SIV test\n")do { } while (false);
1793 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << "\n")do { } while (false);
1794 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n")do { } while (false);
1795 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n")do { } while (false);
1796 ++WeakZeroSIVapplications;
1797 assert(0 < Level && Level <= SrcLevels && "Level out of range")((void)0);
1798 Level--;
1799 Result.Consistent = false;
1800 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1801 NewConstraint.setLine(SrcCoeff, SE->getZero(Delta->getType()), Delta,
1802 CurLoop);
1803 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n")do { } while (false);
1804 if (isKnownPredicate(CmpInst::ICMP_EQ, DstConst, SrcConst)) {
1805 if (Level < CommonLevels) {
1806 Result.DV[Level].Direction &= Dependence::DVEntry::LE;
1807 Result.DV[Level].PeelFirst = true;
1808 ++WeakZeroSIVsuccesses;
1809 }
1810 return false; // dependences caused by first iteration
1811 }
1812 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1813 if (!ConstCoeff)
1814 return false;
1815 const SCEV *AbsCoeff =
1816 SE->isKnownNegative(ConstCoeff) ?
1817 SE->getNegativeSCEV(ConstCoeff) : ConstCoeff;
1818 const SCEV *NewDelta =
1819 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
1820
1821 // check that Delta/SrcCoeff < iteration count
1822 // really check NewDelta < count*AbsCoeff
1823 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1824 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n")do { } while (false);
1825 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
1826 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) {
1827 ++WeakZeroSIVindependence;
1828 ++WeakZeroSIVsuccesses;
1829 return true;
1830 }
1831 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
1832 // dependences caused by last iteration
1833 if (Level < CommonLevels) {
1834 Result.DV[Level].Direction &= Dependence::DVEntry::GE;
1835 Result.DV[Level].PeelLast = true;
1836 ++WeakZeroSIVsuccesses;
1837 }
1838 return false;
1839 }
1840 }
1841
1842 // check that Delta/SrcCoeff >= 0
1843 // really check that NewDelta >= 0
1844 if (SE->isKnownNegative(NewDelta)) {
1845 // No dependence, newDelta < 0
1846 ++WeakZeroSIVindependence;
1847 ++WeakZeroSIVsuccesses;
1848 return true;
1849 }
1850
1851 // if SrcCoeff doesn't divide Delta, then no dependence
1852 if (isa<SCEVConstant>(Delta) &&
1853 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
1854 ++WeakZeroSIVindependence;
1855 ++WeakZeroSIVsuccesses;
1856 return true;
1857 }
1858 return false;
1859}
1860
1861
1862// exactRDIVtest - Tests the RDIV subscript pair for dependence.
1863// Things of the form [c1 + a*i] and [c2 + b*j],
1864// where i and j are induction variable, c1 and c2 are loop invariant,
1865// and a and b are constants.
1866// Returns true if any possible dependence is disproved.
1867// Marks the result as inconsistent.
1868// Works in some cases that symbolicRDIVtest doesn't, and vice versa.
1869bool DependenceInfo::exactRDIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
1870 const SCEV *SrcConst, const SCEV *DstConst,
1871 const Loop *SrcLoop, const Loop *DstLoop,
1872 FullDependence &Result) const {
1873 LLVM_DEBUG(dbgs() << "\tExact RDIV test\n")do { } while (false);
1874 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n")do { } while (false);
1875 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n")do { } while (false);
1876 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n")do { } while (false);
1877 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n")do { } while (false);
1878 ++ExactRDIVapplications;
1879 Result.Consistent = false;
1880 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1881 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n")do { } while (false);
1882 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1883 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1884 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1885 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
1886 return false;
1887
1888 // find gcd
1889 APInt G, X, Y;
1890 APInt AM = ConstSrcCoeff->getAPInt();
1891 APInt BM = ConstDstCoeff->getAPInt();
1892 APInt CM = ConstDelta->getAPInt();
1893 unsigned Bits = AM.getBitWidth();
1894 if (findGCD(Bits, AM, BM, CM, G, X, Y)) {
1895 // gcd doesn't divide Delta, no dependence
1896 ++ExactRDIVindependence;
1897 return true;
1898 }
1899
1900 LLVM_DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n")do { } while (false);
1901
1902 // since SCEV construction seems to normalize, LM = 0
1903 APInt SrcUM(Bits, 1, true);
1904 bool SrcUMvalid = false;
1905 // SrcUM is perhaps unavailable, let's check
1906 if (const SCEVConstant *UpperBound =
1907 collectConstantUpperBound(SrcLoop, Delta->getType())) {
1908 SrcUM = UpperBound->getAPInt();
1909 LLVM_DEBUG(dbgs() << "\t SrcUM = " << SrcUM << "\n")do { } while (false);
1910 SrcUMvalid = true;
1911 }
1912
1913 APInt DstUM(Bits, 1, true);
1914 bool DstUMvalid = false;
1915 // UM is perhaps unavailable, let's check
1916 if (const SCEVConstant *UpperBound =
1917 collectConstantUpperBound(DstLoop, Delta->getType())) {
1918 DstUM = UpperBound->getAPInt();
1919 LLVM_DEBUG(dbgs() << "\t DstUM = " << DstUM << "\n")do { } while (false);
1920 DstUMvalid = true;
1921 }
1922
1923 APInt TU(APInt::getSignedMaxValue(Bits));
1924 APInt TL(APInt::getSignedMinValue(Bits));
1925 APInt TC = CM.sdiv(G);
1926 APInt TX = X * TC;
1927 APInt TY = Y * TC;
1928 LLVM_DEBUG(dbgs() << "\t TC = " << TC << "\n")do { } while (false);
1929 LLVM_DEBUG(dbgs() << "\t TX = " << TX << "\n")do { } while (false);
1930 LLVM_DEBUG(dbgs() << "\t TY = " << TY << "\n")do { } while (false);
1931
1932 SmallVector<APInt, 2> TLVec, TUVec;
1933 APInt TB = BM.sdiv(G);
1934 if (TB.sgt(0)) {
1935 TLVec.push_back(ceilingOfQuotient(-TX, TB));
1936 LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n")do { } while (false);
1937 if (SrcUMvalid) {
1938 TUVec.push_back(floorOfQuotient(SrcUM - TX, TB));
1939 LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n")do { } while (false);
1940 }
1941 } else {
1942 TUVec.push_back(floorOfQuotient(-TX, TB));
1943 LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n")do { } while (false);
1944 if (SrcUMvalid) {
1945 TLVec.push_back(ceilingOfQuotient(SrcUM - TX, TB));
1946 LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n")do { } while (false);
1947 }
1948 }
1949
1950 APInt TA = AM.sdiv(G);
1951 if (TA.sgt(0)) {
1952 TLVec.push_back(ceilingOfQuotient(-TY, TA));
1953 LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n")do { } while (false);
1954 if (DstUMvalid) {
1955 TUVec.push_back(floorOfQuotient(DstUM - TY, TA));
1956 LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n")do { } while (false);
1957 }
1958 } else {
1959 TUVec.push_back(floorOfQuotient(-TY, TA));
1960 LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n")do { } while (false);
1961 if (DstUMvalid) {
1962 TLVec.push_back(ceilingOfQuotient(DstUM - TY, TA));
1963 LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n")do { } while (false);
1964 }
1965 }
1966
1967 if (TLVec.empty() || TUVec.empty())
1968 return false;
1969
1970 LLVM_DEBUG(dbgs() << "\t TA = " << TA << "\n")do { } while (false);
1971 LLVM_DEBUG(dbgs() << "\t TB = " << TB << "\n")do { } while (false);
1972
1973 TL = APIntOps::smax(TLVec.front(), TLVec.back());
1974 TU = APIntOps::smin(TUVec.front(), TUVec.back());
1975 LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n")do { } while (false);
1976 LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n")do { } while (false);
1977
1978 if (TL.sgt(TU))
1979 ++ExactRDIVindependence;
1980 return TL.sgt(TU);
1981}
1982
1983
1984// symbolicRDIVtest -
1985// In Section 4.5 of the Practical Dependence Testing paper,the authors
1986// introduce a special case of Banerjee's Inequalities (also called the
1987// Extreme-Value Test) that can handle some of the SIV and RDIV cases,
1988// particularly cases with symbolics. Since it's only able to disprove
1989// dependence (not compute distances or directions), we'll use it as a
1990// fall back for the other tests.
1991//
1992// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
1993// where i and j are induction variables and c1 and c2 are loop invariants,
1994// we can use the symbolic tests to disprove some dependences, serving as a
1995// backup for the RDIV test. Note that i and j can be the same variable,
1996// letting this test serve as a backup for the various SIV tests.
1997//
1998// For a dependence to exist, c1 + a1*i must equal c2 + a2*j for some
1999// 0 <= i <= N1 and some 0 <= j <= N2, where N1 and N2 are the (normalized)
2000// loop bounds for the i and j loops, respectively. So, ...
2001//
2002// c1 + a1*i = c2 + a2*j
2003// a1*i - a2*j = c2 - c1
2004//
2005// To test for a dependence, we compute c2 - c1 and make sure it's in the
2006// range of the maximum and minimum possible values of a1*i - a2*j.
2007// Considering the signs of a1 and a2, we have 4 possible cases:
2008//
2009// 1) If a1 >= 0 and a2 >= 0, then
2010// a1*0 - a2*N2 <= c2 - c1 <= a1*N1 - a2*0
2011// -a2*N2 <= c2 - c1 <= a1*N1
2012//
2013// 2) If a1 >= 0 and a2 <= 0, then
2014// a1*0 - a2*0 <= c2 - c1 <= a1*N1 - a2*N2
2015// 0 <= c2 - c1 <= a1*N1 - a2*N2
2016//
2017// 3) If a1 <= 0 and a2 >= 0, then
2018// a1*N1 - a2*N2 <= c2 - c1 <= a1*0 - a2*0
2019// a1*N1 - a2*N2 <= c2 - c1 <= 0
2020//
2021// 4) If a1 <= 0 and a2 <= 0, then
2022// a1*N1 - a2*0 <= c2 - c1 <= a1*0 - a2*N2
2023// a1*N1 <= c2 - c1 <= -a2*N2
2024//
2025// return true if dependence disproved
2026bool DependenceInfo::symbolicRDIVtest(const SCEV *A1, const SCEV *A2,
2027 const SCEV *C1, const SCEV *C2,
2028 const Loop *Loop1,
2029 const Loop *Loop2) const {
2030 ++SymbolicRDIVapplications;
2031 LLVM_DEBUG(dbgs() << "\ttry symbolic RDIV test\n")do { } while (false);
2032 LLVM_DEBUG(dbgs() << "\t A1 = " << *A1)do { } while (false);
2033 LLVM_DEBUG(dbgs() << ", type = " << *A1->getType() << "\n")do { } while (false);
2034 LLVM_DEBUG(dbgs() << "\t A2 = " << *A2 << "\n")do { } while (false);
2035 LLVM_DEBUG(dbgs() << "\t C1 = " << *C1 << "\n")do { } while (false);
2036 LLVM_DEBUG(dbgs() << "\t C2 = " << *C2 << "\n")do { } while (false);
2037 const SCEV *N1 = collectUpperBound(Loop1, A1->getType());
2038 const SCEV *N2 = collectUpperBound(Loop2, A1->getType());
2039 LLVM_DEBUG(if (N1) dbgs() << "\t N1 = " << *N1 << "\n")do { } while (false);
2040 LLVM_DEBUG(if (N2) dbgs() << "\t N2 = " << *N2 << "\n")do { } while (false);
2041 const SCEV *C2_C1 = SE->getMinusSCEV(C2, C1);
2042 const SCEV *C1_C2 = SE->getMinusSCEV(C1, C2);
2043 LLVM_DEBUG(dbgs() << "\t C2 - C1 = " << *C2_C1 << "\n")do { } while (false);
2044 LLVM_DEBUG(dbgs() << "\t C1 - C2 = " << *C1_C2 << "\n")do { } while (false);
2045 if (SE->isKnownNonNegative(A1)) {
2046 if (SE->isKnownNonNegative(A2)) {
2047 // A1 >= 0 && A2 >= 0
2048 if (N1) {
2049 // make sure that c2 - c1 <= a1*N1
2050 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2051 LLVM_DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n")do { } while (false);
2052 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1)) {
2053 ++SymbolicRDIVindependence;
2054 return true;
2055 }
2056 }
2057 if (N2) {
2058 // make sure that -a2*N2 <= c2 - c1, or a2*N2 >= c1 - c2
2059 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2060 LLVM_DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n")do { } while (false);
2061 if (isKnownPredicate(CmpInst::ICMP_SLT, A2N2, C1_C2)) {
2062 ++SymbolicRDIVindependence;
2063 return true;
2064 }
2065 }
2066 }
2067 else if (SE->isKnownNonPositive(A2)) {
2068 // a1 >= 0 && a2 <= 0
2069 if (N1 && N2) {
2070 // make sure that c2 - c1 <= a1*N1 - a2*N2
2071 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2072 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2073 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2);
2074 LLVM_DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n")do { } while (false);
2075 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1_A2N2)) {
2076 ++SymbolicRDIVindependence;
2077 return true;
2078 }
2079 }
2080 // make sure that 0 <= c2 - c1
2081 if (SE->isKnownNegative(C2_C1)) {
2082 ++SymbolicRDIVindependence;
2083 return true;
2084 }
2085 }
2086 }
2087 else if (SE->isKnownNonPositive(A1)) {
2088 if (SE->isKnownNonNegative(A2)) {
2089 // a1 <= 0 && a2 >= 0
2090 if (N1 && N2) {
2091 // make sure that a1*N1 - a2*N2 <= c2 - c1
2092 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2093 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2094 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2);
2095 LLVM_DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n")do { } while (false);
2096 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1_A2N2, C2_C1)) {
2097 ++SymbolicRDIVindependence;
2098 return true;
2099 }
2100 }
2101 // make sure that c2 - c1 <= 0
2102 if (SE->isKnownPositive(C2_C1)) {
2103 ++SymbolicRDIVindependence;
2104 return true;
2105 }
2106 }
2107 else if (SE->isKnownNonPositive(A2)) {
2108 // a1 <= 0 && a2 <= 0
2109 if (N1) {
2110 // make sure that a1*N1 <= c2 - c1
2111 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2112 LLVM_DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n")do { } while (false);
2113 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1, C2_C1)) {
2114 ++SymbolicRDIVindependence;
2115 return true;
2116 }
2117 }
2118 if (N2) {
2119 // make sure that c2 - c1 <= -a2*N2, or c1 - c2 >= a2*N2
2120 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2121 LLVM_DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n")do { } while (false);
2122 if (isKnownPredicate(CmpInst::ICMP_SLT, C1_C2, A2N2)) {
2123 ++SymbolicRDIVindependence;
2124 return true;
2125 }
2126 }
2127 }
2128 }
2129 return false;
2130}
2131
2132
2133// testSIV -
2134// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 - a2*i]
2135// where i is an induction variable, c1 and c2 are loop invariant, and a1 and
2136// a2 are constant, we attack it with an SIV test. While they can all be
2137// solved with the Exact SIV test, it's worthwhile to use simpler tests when
2138// they apply; they're cheaper and sometimes more precise.
2139//
2140// Return true if dependence disproved.
2141bool DependenceInfo::testSIV(const SCEV *Src, const SCEV *Dst, unsigned &Level,
2142 FullDependence &Result, Constraint &NewConstraint,
2143 const SCEV *&SplitIter) const {
2144 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n")do { } while (false);
2145 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n")do { } while (false);
2146 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
2147 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
2148 if (SrcAddRec && DstAddRec) {
2149 const SCEV *SrcConst = SrcAddRec->getStart();
2150 const SCEV *DstConst = DstAddRec->getStart();
2151 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2152 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
2153 const Loop *CurLoop = SrcAddRec->getLoop();
2154 assert(CurLoop == DstAddRec->getLoop() &&((void)0)
2155 "both loops in SIV should be same")((void)0);
2156 Level = mapSrcLoop(CurLoop);
2157 bool disproven;
2158 if (SrcCoeff == DstCoeff)
2159 disproven = strongSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
2160 Level, Result, NewConstraint);
2161 else if (SrcCoeff == SE->getNegativeSCEV(DstCoeff))
2162 disproven = weakCrossingSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
2163 Level, Result, NewConstraint, SplitIter);
2164 else
2165 disproven = exactSIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurLoop,
2166 Level, Result, NewConstraint);
2167 return disproven ||
2168 gcdMIVtest(Src, Dst, Result) ||
2169 symbolicRDIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurLoop, CurLoop);
2170 }
2171 if (SrcAddRec) {
2172 const SCEV *SrcConst = SrcAddRec->getStart();
2173 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2174 const SCEV *DstConst = Dst;
2175 const Loop *CurLoop = SrcAddRec->getLoop();
2176 Level = mapSrcLoop(CurLoop);
2177 return weakZeroDstSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
2178 Level, Result, NewConstraint) ||
2179 gcdMIVtest(Src, Dst, Result);
2180 }
2181 if (DstAddRec) {
2182 const SCEV *DstConst = DstAddRec->getStart();
2183 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
2184 const SCEV *SrcConst = Src;
2185 const Loop *CurLoop = DstAddRec->getLoop();
2186 Level = mapDstLoop(CurLoop);
2187 return weakZeroSrcSIVtest(DstCoeff, SrcConst, DstConst,
2188 CurLoop, Level, Result, NewConstraint) ||
2189 gcdMIVtest(Src, Dst, Result);
2190 }
2191 llvm_unreachable("SIV test expected at least one AddRec")__builtin_unreachable();
2192 return false;
2193}
2194
2195
2196// testRDIV -
2197// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
2198// where i and j are induction variables, c1 and c2 are loop invariant,
2199// and a1 and a2 are constant, we can solve it exactly with an easy adaptation
2200// of the Exact SIV test, the Restricted Double Index Variable (RDIV) test.
2201// It doesn't make sense to talk about distance or direction in this case,
2202// so there's no point in making special versions of the Strong SIV test or
2203// the Weak-crossing SIV test.
2204//
2205// With minor algebra, this test can also be used for things like
2206// [c1 + a1*i + a2*j][c2].
2207//
2208// Return true if dependence disproved.
2209bool DependenceInfo::testRDIV(const SCEV *Src, const SCEV *Dst,
2210 FullDependence &Result) const {
2211 // we have 3 possible situations here:
2212 // 1) [a*i + b] and [c*j + d]
2213 // 2) [a*i + c*j + b] and [d]
2214 // 3) [b] and [a*i + c*j + d]
2215 // We need to find what we've got and get organized
2216
2217 const SCEV *SrcConst, *DstConst;
2218 const SCEV *SrcCoeff, *DstCoeff;
2219 const Loop *SrcLoop, *DstLoop;
2220
2221 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n")do { } while (false);
2222 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n")do { } while (false);
2223 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
2224 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
2225 if (SrcAddRec && DstAddRec) {
2226 SrcConst = SrcAddRec->getStart();
2227 SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2228 SrcLoop = SrcAddRec->getLoop();
2229 DstConst = DstAddRec->getStart();
2230 DstCoeff = DstAddRec->getStepRecurrence(*SE);
2231 DstLoop = DstAddRec->getLoop();
2232 }
2233 else if (SrcAddRec) {
2234 if (const SCEVAddRecExpr *tmpAddRec =
2235 dyn_cast<SCEVAddRecExpr>(SrcAddRec->getStart())) {
2236 SrcConst = tmpAddRec->getStart();
2237 SrcCoeff = tmpAddRec->getStepRecurrence(*SE);
2238 SrcLoop = tmpAddRec->getLoop();
2239 DstConst = Dst;
2240 DstCoeff = SE->getNegativeSCEV(SrcAddRec->getStepRecurrence(*SE));
2241 DstLoop = SrcAddRec->getLoop();
2242 }
2243 else
2244 llvm_unreachable("RDIV reached by surprising SCEVs")__builtin_unreachable();
2245 }
2246 else if (DstAddRec) {
2247 if (const SCEVAddRecExpr *tmpAddRec =
2248 dyn_cast<SCEVAddRecExpr>(DstAddRec->getStart())) {
2249 DstConst = tmpAddRec->getStart();
2250 DstCoeff = tmpAddRec->getStepRecurrence(*SE);
2251 DstLoop = tmpAddRec->getLoop();
2252 SrcConst = Src;
2253 SrcCoeff = SE->getNegativeSCEV(DstAddRec->getStepRecurrence(*SE));
2254 SrcLoop = DstAddRec->getLoop();
2255 }
2256 else
2257 llvm_unreachable("RDIV reached by surprising SCEVs")__builtin_unreachable();
2258 }
2259 else
2260 llvm_unreachable("RDIV expected at least one AddRec")__builtin_unreachable();
2261 return exactRDIVtest(SrcCoeff, DstCoeff,
2262 SrcConst, DstConst,
2263 SrcLoop, DstLoop,
2264 Result) ||
2265 gcdMIVtest(Src, Dst, Result) ||
2266 symbolicRDIVtest(SrcCoeff, DstCoeff,
2267 SrcConst, DstConst,
2268 SrcLoop, DstLoop);
2269}
2270
2271
2272// Tests the single-subscript MIV pair (Src and Dst) for dependence.
2273// Return true if dependence disproved.
2274// Can sometimes refine direction vectors.
2275bool DependenceInfo::testMIV(const SCEV *Src, const SCEV *Dst,
2276 const SmallBitVector &Loops,
2277 FullDependence &Result) const {
2278 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n")do { } while (false);
2279 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n")do { } while (false);
2280 Result.Consistent = false;
2281 return gcdMIVtest(Src, Dst, Result) ||
2282 banerjeeMIVtest(Src, Dst, Loops, Result);
2283}
2284
2285
2286// Given a product, e.g., 10*X*Y, returns the first constant operand,
2287// in this case 10. If there is no constant part, returns NULL.
2288static
2289const SCEVConstant *getConstantPart(const SCEV *Expr) {
2290 if (const auto *Constant = dyn_cast<SCEVConstant>(Expr))
2291 return Constant;
2292 else if (const auto *Product = dyn_cast<SCEVMulExpr>(Expr))
2293 if (const auto *Constant = dyn_cast<SCEVConstant>(Product->getOperand(0)))
2294 return Constant;
2295 return nullptr;
2296}
2297
2298
2299//===----------------------------------------------------------------------===//
2300// gcdMIVtest -
2301// Tests an MIV subscript pair for dependence.
2302// Returns true if any possible dependence is disproved.
2303// Marks the result as inconsistent.
2304// Can sometimes disprove the equal direction for 1 or more loops,
2305// as discussed in Michael Wolfe's book,
2306// High Performance Compilers for Parallel Computing, page 235.
2307//
2308// We spend some effort (code!) to handle cases like
2309// [10*i + 5*N*j + 15*M + 6], where i and j are induction variables,
2310// but M and N are just loop-invariant variables.
2311// This should help us handle linearized subscripts;
2312// also makes this test a useful backup to the various SIV tests.
2313//
2314// It occurs to me that the presence of loop-invariant variables
2315// changes the nature of the test from "greatest common divisor"
2316// to "a common divisor".
2317bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,
2318 FullDependence &Result) const {
2319 LLVM_DEBUG(dbgs() << "starting gcd\n")do { } while (false);
2320 ++GCDapplications;
2321 unsigned BitWidth = SE->getTypeSizeInBits(Src->getType());
2322 APInt RunningGCD = APInt::getNullValue(BitWidth);
2323
2324 // Examine Src coefficients.
2325 // Compute running GCD and record source constant.
2326 // Because we're looking for the constant at the end of the chain,
2327 // we can't quit the loop just because the GCD == 1.
2328 const SCEV *Coefficients = Src;
2329 while (const SCEVAddRecExpr *AddRec =
2330 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2331 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2332 // If the coefficient is the product of a constant and other stuff,
2333 // we can use the constant in the GCD computation.
2334 const auto *Constant = getConstantPart(Coeff);
2335 if (!Constant)
2336 return false;
2337 APInt ConstCoeff = Constant->getAPInt();
2338 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2339 Coefficients = AddRec->getStart();
2340 }
2341 const SCEV *SrcConst = Coefficients;
2342
2343 // Examine Dst coefficients.
2344 // Compute running GCD and record destination constant.
2345 // Because we're looking for the constant at the end of the chain,
2346 // we can't quit the loop just because the GCD == 1.
2347 Coefficients = Dst;
2348 while (const SCEVAddRecExpr *AddRec =
2349 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2350 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2351 // If the coefficient is the product of a constant and other stuff,
2352 // we can use the constant in the GCD computation.
2353 const auto *Constant = getConstantPart(Coeff);
2354 if (!Constant)
2355 return false;
2356 APInt ConstCoeff = Constant->getAPInt();
2357 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2358 Coefficients = AddRec->getStart();
2359 }
2360 const SCEV *DstConst = Coefficients;
2361
2362 APInt ExtraGCD = APInt::getNullValue(BitWidth);
2363 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
2364 LLVM_DEBUG(dbgs() << " Delta = " << *Delta << "\n")do { } while (false);
2365 const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Delta);
2366 if (const SCEVAddExpr *Sum = dyn_cast<SCEVAddExpr>(Delta)) {
2367 // If Delta is a sum of products, we may be able to make further progress.
2368 for (unsigned Op = 0, Ops = Sum->getNumOperands(); Op < Ops; Op++) {
2369 const SCEV *Operand = Sum->getOperand(Op);
2370 if (isa<SCEVConstant>(Operand)) {
2371 assert(!Constant && "Surprised to find multiple constants")((void)0);
2372 Constant = cast<SCEVConstant>(Operand);
2373 }
2374 else if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Operand)) {
2375 // Search for constant operand to participate in GCD;
2376 // If none found; return false.
2377 const SCEVConstant *ConstOp = getConstantPart(Product);
2378 if (!ConstOp)
2379 return false;
2380 APInt ConstOpValue = ConstOp->getAPInt();
2381 ExtraGCD = APIntOps::GreatestCommonDivisor(ExtraGCD,
2382 ConstOpValue.abs());
2383 }
2384 else
2385 return false;
2386 }
2387 }
2388 if (!Constant)
2389 return false;
2390 APInt ConstDelta = cast<SCEVConstant>(Constant)->getAPInt();
2391 LLVM_DEBUG(dbgs() << " ConstDelta = " << ConstDelta << "\n")do { } while (false);
2392 if (ConstDelta == 0)
2393 return false;
2394 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ExtraGCD);
2395 LLVM_DEBUG(dbgs() << " RunningGCD = " << RunningGCD << "\n")do { } while (false);
2396 APInt Remainder = ConstDelta.srem(RunningGCD);
2397 if (Remainder != 0) {
2398 ++GCDindependence;
2399 return true;
2400 }
2401
2402 // Try to disprove equal directions.
2403 // For example, given a subscript pair [3*i + 2*j] and [i' + 2*j' - 1],
2404 // the code above can't disprove the dependence because the GCD = 1.
2405 // So we consider what happen if i = i' and what happens if j = j'.
2406 // If i = i', we can simplify the subscript to [2*i + 2*j] and [2*j' - 1],
2407 // which is infeasible, so we can disallow the = direction for the i level.
2408 // Setting j = j' doesn't help matters, so we end up with a direction vector
2409 // of [<>, *]
2410 //
2411 // Given A[5*i + 10*j*M + 9*M*N] and A[15*i + 20*j*M - 21*N*M + 5],
2412 // we need to remember that the constant part is 5 and the RunningGCD should
2413 // be initialized to ExtraGCD = 30.
2414 LLVM_DEBUG(dbgs() << " ExtraGCD = " << ExtraGCD << '\n')do { } while (false);
2415
2416 bool Improved = false;
2417 Coefficients = Src;
2418 while (const SCEVAddRecExpr *AddRec =
2419 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2420 Coefficients = AddRec->getStart();
2421 const Loop *CurLoop = AddRec->getLoop();
2422 RunningGCD = ExtraGCD;
2423 const SCEV *SrcCoeff = AddRec->getStepRecurrence(*SE);
2424 const SCEV *DstCoeff = SE->getMinusSCEV(SrcCoeff, SrcCoeff);
2425 const SCEV *Inner = Src;
2426 while (RunningGCD != 1 && isa<SCEVAddRecExpr>(Inner)) {
2427 AddRec = cast<SCEVAddRecExpr>(Inner);
2428 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2429 if (CurLoop == AddRec->getLoop())
2430 ; // SrcCoeff == Coeff
2431 else {
2432 // If the coefficient is the product of a constant and other stuff,
2433 // we can use the constant in the GCD computation.
2434 Constant = getConstantPart(Coeff);
2435 if (!Constant)
2436 return false;
2437 APInt ConstCoeff = Constant->getAPInt();
2438 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2439 }
2440 Inner = AddRec->getStart();
2441 }
2442 Inner = Dst;
2443 while (RunningGCD != 1 && isa<SCEVAddRecExpr>(Inner)) {
2444 AddRec = cast<SCEVAddRecExpr>(Inner);
2445 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2446 if (CurLoop == AddRec->getLoop())
2447 DstCoeff = Coeff;
2448 else {
2449 // If the coefficient is the product of a constant and other stuff,
2450 // we can use the constant in the GCD computation.
2451 Constant = getConstantPart(Coeff);
2452 if (!Constant)
2453 return false;
2454 APInt ConstCoeff = Constant->getAPInt();
2455 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2456 }
2457 Inner = AddRec->getStart();
2458 }
2459 Delta = SE->getMinusSCEV(SrcCoeff, DstCoeff);
2460 // If the coefficient is the product of a constant and other stuff,
2461 // we can use the constant in the GCD computation.
2462 Constant = getConstantPart(Delta);
2463 if (!Constant)
2464 // The difference of the two coefficients might not be a product
2465 // or constant, in which case we give up on this direction.
2466 continue;
2467 APInt ConstCoeff = Constant->getAPInt();
2468 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2469 LLVM_DEBUG(dbgs() << "\tRunningGCD = " << RunningGCD << "\n")do { } while (false);
2470 if (RunningGCD != 0) {
2471 Remainder = ConstDelta.srem(RunningGCD);
2472 LLVM_DEBUG(dbgs() << "\tRemainder = " << Remainder << "\n")do { } while (false);
2473 if (Remainder != 0) {
2474 unsigned Level = mapSrcLoop(CurLoop);
2475 Result.DV[Level - 1].Direction &= unsigned(~Dependence::DVEntry::EQ);
2476 Improved = true;
2477 }
2478 }
2479 }
2480 if (Improved)
2481 ++GCDsuccesses;
2482 LLVM_DEBUG(dbgs() << "all done\n")do { } while (false);
2483 return false;
2484}
2485
2486
2487//===----------------------------------------------------------------------===//
2488// banerjeeMIVtest -
2489// Use Banerjee's Inequalities to test an MIV subscript pair.
2490// (Wolfe, in the race-car book, calls this the Extreme Value Test.)
2491// Generally follows the discussion in Section 2.5.2 of
2492//
2493// Optimizing Supercompilers for Supercomputers
2494// Michael Wolfe
2495//
2496// The inequalities given on page 25 are simplified in that loops are
2497// normalized so that the lower bound is always 0 and the stride is always 1.
2498// For example, Wolfe gives
2499//
2500// LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2501//
2502// where A_k is the coefficient of the kth index in the source subscript,
2503// B_k is the coefficient of the kth index in the destination subscript,
2504// U_k is the upper bound of the kth index, L_k is the lower bound of the Kth
2505// index, and N_k is the stride of the kth index. Since all loops are normalized
2506// by the SCEV package, N_k = 1 and L_k = 0, allowing us to simplify the
2507// equation to
2508//
2509// LB^<_k = (A^-_k - B_k)^- (U_k - 0 - 1) + (A_k - B_k)0 - B_k 1
2510// = (A^-_k - B_k)^- (U_k - 1) - B_k
2511//
2512// Similar simplifications are possible for the other equations.
2513//
2514// When we can't determine the number of iterations for a loop,
2515// we use NULL as an indicator for the worst case, infinity.
2516// When computing the upper bound, NULL denotes +inf;
2517// for the lower bound, NULL denotes -inf.
2518//
2519// Return true if dependence disproved.
2520bool DependenceInfo::banerjeeMIVtest(const SCEV *Src, const SCEV *Dst,
2521 const SmallBitVector &Loops,
2522 FullDependence &Result) const {
2523 LLVM_DEBUG(dbgs() << "starting Banerjee\n")do { } while (false);
2524 ++BanerjeeApplications;
2525 LLVM_DEBUG(dbgs() << " Src = " << *Src << '\n')do { } while (false);
2526 const SCEV *A0;
2527 CoefficientInfo *A = collectCoeffInfo(Src, true, A0);
2528 LLVM_DEBUG(dbgs() << " Dst = " << *Dst << '\n')do { } while (false);
2529 const SCEV *B0;
2530 CoefficientInfo *B = collectCoeffInfo(Dst, false, B0);
2531 BoundInfo *Bound = new BoundInfo[MaxLevels + 1];
2532 const SCEV *Delta = SE->getMinusSCEV(B0, A0);
2533 LLVM_DEBUG(dbgs() << "\tDelta = " << *Delta << '\n')do { } while (false);
2534
2535 // Compute bounds for all the * directions.
2536 LLVM_DEBUG(dbgs() << "\tBounds[*]\n")do { } while (false);
2537 for (unsigned K = 1; K <= MaxLevels; ++K) {
2538 Bound[K].Iterations = A[K].Iterations ? A[K].Iterations : B[K].Iterations;
2539 Bound[K].Direction = Dependence::DVEntry::ALL;
2540 Bound[K].DirSet = Dependence::DVEntry::NONE;
2541 findBoundsALL(A, B, Bound, K);
2542#ifndef NDEBUG1
2543 LLVM_DEBUG(dbgs() << "\t " << K << '\t')do { } while (false);
2544 if (Bound[K].Lower[Dependence::DVEntry::ALL])
2545 LLVM_DEBUG(dbgs() << *Bound[K].Lower[Dependence::DVEntry::ALL] << '\t')do { } while (false);
2546 else
2547 LLVM_DEBUG(dbgs() << "-inf\t")do { } while (false);
2548 if (Bound[K].Upper[Dependence::DVEntry::ALL])
2549 LLVM_DEBUG(dbgs() << *Bound[K].Upper[Dependence::DVEntry::ALL] << '\n')do { } while (false);
2550 else
2551 LLVM_DEBUG(dbgs() << "+inf\n")do { } while (false);
2552#endif
2553 }
2554
2555 // Test the *, *, *, ... case.
2556 bool Disproved = false;
2557 if (testBounds(Dependence::DVEntry::ALL, 0, Bound, Delta)) {
2558 // Explore the direction vector hierarchy.
2559 unsigned DepthExpanded = 0;
2560 unsigned NewDeps = exploreDirections(1, A, B, Bound,
2561 Loops, DepthExpanded, Delta);
2562 if (NewDeps > 0) {
2563 bool Improved = false;
2564 for (unsigned K = 1; K <= CommonLevels; ++K) {
2565 if (Loops[K]) {
2566 unsigned Old = Result.DV[K - 1].Direction;
2567 Result.DV[K - 1].Direction = Old & Bound[K].DirSet;
2568 Improved |= Old != Result.DV[K - 1].Direction;
2569 if (!Result.DV[K - 1].Direction) {
2570 Improved = false;
2571 Disproved = true;
2572 break;
2573 }
2574 }
2575 }
2576 if (Improved)
2577 ++BanerjeeSuccesses;
2578 }
2579 else {
2580 ++BanerjeeIndependence;
2581 Disproved = true;
2582 }
2583 }
2584 else {
2585 ++BanerjeeIndependence;
2586 Disproved = true;
2587 }
2588 delete [] Bound;
2589 delete [] A;
2590 delete [] B;
2591 return Disproved;
2592}
2593
2594
2595// Hierarchically expands the direction vector
2596// search space, combining the directions of discovered dependences
2597// in the DirSet field of Bound. Returns the number of distinct
2598// dependences discovered. If the dependence is disproved,
2599// it will return 0.
2600unsigned DependenceInfo::exploreDirections(unsigned Level, CoefficientInfo *A,
2601 CoefficientInfo *B, BoundInfo *Bound,
2602 const SmallBitVector &Loops,
2603 unsigned &DepthExpanded,
2604 const SCEV *Delta) const {
2605 if (Level > CommonLevels) {
2606 // record result
2607 LLVM_DEBUG(dbgs() << "\t[")do { } while (false);
2608 for (unsigned K = 1; K <= CommonLevels; ++K) {
2609 if (Loops[K]) {
2610 Bound[K].DirSet |= Bound[K].Direction;
2611#ifndef NDEBUG1
2612 switch (Bound[K].Direction) {
2613 case Dependence::DVEntry::LT:
2614 LLVM_DEBUG(dbgs() << " <")do { } while (false);
2615 break;
2616 case Dependence::DVEntry::EQ:
2617 LLVM_DEBUG(dbgs() << " =")do { } while (false);
2618 break;
2619 case Dependence::DVEntry::GT:
2620 LLVM_DEBUG(dbgs() << " >")do { } while (false);
2621 break;
2622 case Dependence::DVEntry::ALL:
2623 LLVM_DEBUG(dbgs() << " *")do { } while (false);
2624 break;
2625 default:
2626 llvm_unreachable("unexpected Bound[K].Direction")__builtin_unreachable();
2627 }
2628#endif
2629 }
2630 }
2631 LLVM_DEBUG(dbgs() << " ]\n")do { } while (false);
2632 return 1;
2633 }
2634 if (Loops[Level]) {
2635 if (Level > DepthExpanded) {
2636 DepthExpanded = Level;
2637 // compute bounds for <, =, > at current level
2638 findBoundsLT(A, B, Bound, Level);
2639 findBoundsGT(A, B, Bound, Level);
2640 findBoundsEQ(A, B, Bound, Level);
2641#ifndef NDEBUG1
2642 LLVM_DEBUG(dbgs() << "\tBound for level = " << Level << '\n')do { } while (false);
2643 LLVM_DEBUG(dbgs() << "\t <\t")do { } while (false);
2644 if (Bound[Level].Lower[Dependence::DVEntry::LT])
2645 LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::LT]do { } while (false)
2646 << '\t')do { } while (false);
2647 else
2648 LLVM_DEBUG(dbgs() << "-inf\t")do { } while (false);
2649 if (Bound[Level].Upper[Dependence::DVEntry::LT])
2650 LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::LT]do { } while (false)
2651 << '\n')do { } while (false);
2652 else
2653 LLVM_DEBUG(dbgs() << "+inf\n")do { } while (false);
2654 LLVM_DEBUG(dbgs() << "\t =\t")do { } while (false);
2655 if (Bound[Level].Lower[Dependence::DVEntry::EQ])
2656 LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::EQ]do { } while (false)
2657 << '\t')do { } while (false);
2658 else
2659 LLVM_DEBUG(dbgs() << "-inf\t")do { } while (false);
2660 if (Bound[Level].Upper[Dependence::DVEntry::EQ])
2661 LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::EQ]do { } while (false)
2662 << '\n')do { } while (false);
2663 else
2664 LLVM_DEBUG(dbgs() << "+inf\n")do { } while (false);
2665 LLVM_DEBUG(dbgs() << "\t >\t")do { } while (false);
2666 if (Bound[Level].Lower[Dependence::DVEntry::GT])
2667 LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::GT]do { } while (false)
2668 << '\t')do { } while (false);
2669 else
2670 LLVM_DEBUG(dbgs() << "-inf\t")do { } while (false);
2671 if (Bound[Level].Upper[Dependence::DVEntry::GT])
2672 LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::GT]do { } while (false)
2673 << '\n')do { } while (false);
2674 else
2675 LLVM_DEBUG(dbgs() << "+inf\n")do { } while (false);
2676#endif
2677 }
2678
2679 unsigned NewDeps = 0;
2680
2681 // test bounds for <, *, *, ...
2682 if (testBounds(Dependence::DVEntry::LT, Level, Bound, Delta))
2683 NewDeps += exploreDirections(Level + 1, A, B, Bound,
2684 Loops, DepthExpanded, Delta);
2685
2686 // Test bounds for =, *, *, ...
2687 if (testBounds(Dependence::DVEntry::EQ, Level, Bound, Delta))
2688 NewDeps += exploreDirections(Level + 1, A, B, Bound,
2689 Loops, DepthExpanded, Delta);
2690
2691 // test bounds for >, *, *, ...
2692 if (testBounds(Dependence::DVEntry::GT, Level, Bound, Delta))
2693 NewDeps += exploreDirections(Level + 1, A, B, Bound,
2694 Loops, DepthExpanded, Delta);
2695
2696 Bound[Level].Direction = Dependence::DVEntry::ALL;
2697 return NewDeps;
2698 }
2699 else
2700 return exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded, Delta);
2701}
2702
2703
2704// Returns true iff the current bounds are plausible.
2705bool DependenceInfo::testBounds(unsigned char DirKind, unsigned Level,
2706 BoundInfo *Bound, const SCEV *Delta) const {
2707 Bound[Level].Direction = DirKind;
2708 if (const SCEV *LowerBound = getLowerBound(Bound))
2709 if (isKnownPredicate(CmpInst::ICMP_SGT, LowerBound, Delta))
2710 return false;
2711 if (const SCEV *UpperBound = getUpperBound(Bound))
2712 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, UpperBound))
2713 return false;
2714 return true;
2715}
2716
2717
2718// Computes the upper and lower bounds for level K
2719// using the * direction. Records them in Bound.
2720// Wolfe gives the equations
2721//
2722// LB^*_k = (A^-_k - B^+_k)(U_k - L_k) + (A_k - B_k)L_k
2723// UB^*_k = (A^+_k - B^-_k)(U_k - L_k) + (A_k - B_k)L_k
2724//
2725// Since we normalize loops, we can simplify these equations to
2726//
2727// LB^*_k = (A^-_k - B^+_k)U_k
2728// UB^*_k = (A^+_k - B^-_k)U_k
2729//
2730// We must be careful to handle the case where the upper bound is unknown.
2731// Note that the lower bound is always <= 0
2732// and the upper bound is always >= 0.
2733void DependenceInfo::findBoundsALL(CoefficientInfo *A, CoefficientInfo *B,
2734 BoundInfo *Bound, unsigned K) const {
2735 Bound[K].Lower[Dependence::DVEntry::ALL] = nullptr; // Default value = -infinity.
2736 Bound[K].Upper[Dependence::DVEntry::ALL] = nullptr; // Default value = +infinity.
2737 if (Bound[K].Iterations) {
2738 Bound[K].Lower[Dependence::DVEntry::ALL] =
2739 SE->getMulExpr(SE->getMinusSCEV(A[K].NegPart, B[K].PosPart),
2740 Bound[K].Iterations);
2741 Bound[K].Upper[Dependence::DVEntry::ALL] =
2742 SE->getMulExpr(SE->getMinusSCEV(A[K].PosPart, B[K].NegPart),
2743 Bound[K].Iterations);
2744 }
2745 else {
2746 // If the difference is 0, we won't need to know the number of iterations.
2747 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].NegPart, B[K].PosPart))
2748 Bound[K].Lower[Dependence::DVEntry::ALL] =
2749 SE->getZero(A[K].Coeff->getType());
2750 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].PosPart, B[K].NegPart))
2751 Bound[K].Upper[Dependence::DVEntry::ALL] =
2752 SE->getZero(A[K].Coeff->getType());
2753 }
2754}
2755
2756
2757// Computes the upper and lower bounds for level K
2758// using the = direction. Records them in Bound.
2759// Wolfe gives the equations
2760//
2761// LB^=_k = (A_k - B_k)^- (U_k - L_k) + (A_k - B_k)L_k
2762// UB^=_k = (A_k - B_k)^+ (U_k - L_k) + (A_k - B_k)L_k
2763//
2764// Since we normalize loops, we can simplify these equations to
2765//
2766// LB^=_k = (A_k - B_k)^- U_k
2767// UB^=_k = (A_k - B_k)^+ U_k
2768//
2769// We must be careful to handle the case where the upper bound is unknown.
2770// Note that the lower bound is always <= 0
2771// and the upper bound is always >= 0.
2772void DependenceInfo::findBoundsEQ(CoefficientInfo *A, CoefficientInfo *B,
2773 BoundInfo *Bound, unsigned K) const {
2774 Bound[K].Lower[Dependence::DVEntry::EQ] = nullptr; // Default value = -infinity.
2775 Bound[K].Upper[Dependence::DVEntry::EQ] = nullptr; // Default value = +infinity.
2776 if (Bound[K].Iterations) {
2777 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
2778 const SCEV *NegativePart = getNegativePart(Delta);
2779 Bound[K].Lower[Dependence::DVEntry::EQ] =
2780 SE->getMulExpr(NegativePart, Bound[K].Iterations);
2781 const SCEV *PositivePart = getPositivePart(Delta);
2782 Bound[K].Upper[Dependence::DVEntry::EQ] =
2783 SE->getMulExpr(PositivePart, Bound[K].Iterations);
2784 }
2785 else {
2786 // If the positive/negative part of the difference is 0,
2787 // we won't need to know the number of iterations.
2788 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
2789 const SCEV *NegativePart = getNegativePart(Delta);
2790 if (NegativePart->isZero())
2791 Bound[K].Lower[Dependence::DVEntry::EQ] = NegativePart; // Zero
2792 const SCEV *PositivePart = getPositivePart(Delta);
2793 if (PositivePart->isZero())
2794 Bound[K].Upper[Dependence::DVEntry::EQ] = PositivePart; // Zero
2795 }
2796}
2797
2798
2799// Computes the upper and lower bounds for level K
2800// using the < direction. Records them in Bound.
2801// Wolfe gives the equations
2802//
2803// LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2804// UB^<_k = (A^+_k - B_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2805//
2806// Since we normalize loops, we can simplify these equations to
2807//
2808// LB^<_k = (A^-_k - B_k)^- (U_k - 1) - B_k
2809// UB^<_k = (A^+_k - B_k)^+ (U_k - 1) - B_k
2810//
2811// We must be careful to handle the case where the upper bound is unknown.
2812void DependenceInfo::findBoundsLT(CoefficientInfo *A, CoefficientInfo *B,
2813 BoundInfo *Bound, unsigned K) const {
2814 Bound[K].Lower[Dependence::DVEntry::LT] = nullptr; // Default value = -infinity.
2815 Bound[K].Upper[Dependence::DVEntry::LT] = nullptr; // Default value = +infinity.
2816 if (Bound[K].Iterations) {
2817 const SCEV *Iter_1 = SE->getMinusSCEV(
2818 Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
2819 const SCEV *NegPart =
2820 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
2821 Bound[K].Lower[Dependence::DVEntry::LT] =
2822 SE->getMinusSCEV(SE->getMulExpr(NegPart, Iter_1), B[K].Coeff);
2823 const SCEV *PosPart =
2824 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
2825 Bound[K].Upper[Dependence::DVEntry::LT] =
2826 SE->getMinusSCEV(SE->getMulExpr(PosPart, Iter_1), B[K].Coeff);
2827 }
2828 else {
2829 // If the positive/negative part of the difference is 0,
2830 // we won't need to know the number of iterations.
2831 const SCEV *NegPart =
2832 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
2833 if (NegPart->isZero())
2834 Bound[K].Lower[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
2835 const SCEV *PosPart =
2836 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
2837 if (PosPart->isZero())
2838 Bound[K].Upper[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
2839 }
2840}
2841
2842
2843// Computes the upper and lower bounds for level K
2844// using the > direction. Records them in Bound.
2845// Wolfe gives the equations
2846//
2847// LB^>_k = (A_k - B^+_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
2848// UB^>_k = (A_k - B^-_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
2849//
2850// Since we normalize loops, we can simplify these equations to
2851//
2852// LB^>_k = (A_k - B^+_k)^- (U_k - 1) + A_k
2853// UB^>_k = (A_k - B^-_k)^+ (U_k - 1) + A_k
2854//
2855// We must be careful to handle the case where the upper bound is unknown.
2856void DependenceInfo::findBoundsGT(CoefficientInfo *A, CoefficientInfo *B,
2857 BoundInfo *Bound, unsigned K) const {
2858 Bound[K].Lower[Dependence::DVEntry::GT] = nullptr; // Default value = -infinity.
2859 Bound[K].Upper[Dependence::DVEntry::GT] = nullptr; // Default value = +infinity.
2860 if (Bound[K].Iterations) {
2861 const SCEV *Iter_1 = SE->getMinusSCEV(
2862 Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
2863 const SCEV *NegPart =
2864 getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
2865 Bound[K].Lower[Dependence::DVEntry::GT] =
2866 SE->getAddExpr(SE->getMulExpr(NegPart, Iter_1), A[K].Coeff);
2867 const SCEV *PosPart =
2868 getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
2869 Bound[K].Upper[Dependence::DVEntry::GT] =
2870 SE->getAddExpr(SE->getMulExpr(PosPart, Iter_1), A[K].Coeff);
2871 }
2872 else {
2873 // If the positive/negative part of the difference is 0,
2874 // we won't need to know the number of iterations.
2875 const SCEV *NegPart = getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
2876 if (NegPart->isZero())
2877 Bound[K].Lower[Dependence::DVEntry::GT] = A[K].Coeff;
2878 const SCEV *PosPart = getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
2879 if (PosPart->isZero())
2880 Bound[K].Upper[Dependence::DVEntry::GT] = A[K].Coeff;
2881 }
2882}
2883
2884
2885// X^+ = max(X, 0)
2886const SCEV *DependenceInfo::getPositivePart(const SCEV *X) const {
2887 return SE->getSMaxExpr(X, SE->getZero(X->getType()));
2888}
2889
2890
2891// X^- = min(X, 0)
2892const SCEV *DependenceInfo::getNegativePart(const SCEV *X) const {
2893 return SE->getSMinExpr(X, SE->getZero(X->getType()));
2894}
2895
2896
2897// Walks through the subscript,
2898// collecting each coefficient, the associated loop bounds,
2899// and recording its positive and negative parts for later use.
2900DependenceInfo::CoefficientInfo *
2901DependenceInfo::collectCoeffInfo(const SCEV *Subscript, bool SrcFlag,
2902 const SCEV *&Constant) const {
2903 const SCEV *Zero = SE->getZero(Subscript->getType());
2904 CoefficientInfo *CI = new CoefficientInfo[MaxLevels + 1];
2905 for (unsigned K = 1; K <= MaxLevels; ++K) {
2906 CI[K].Coeff = Zero;
2907 CI[K].PosPart = Zero;
2908 CI[K].NegPart = Zero;
2909 CI[K].Iterations = nullptr;
2910 }
2911 while (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Subscript)) {
2912 const Loop *L = AddRec->getLoop();
2913 unsigned K = SrcFlag ? mapSrcLoop(L) : mapDstLoop(L);
2914 CI[K].Coeff = AddRec->getStepRecurrence(*SE);
2915 CI[K].PosPart = getPositivePart(CI[K].Coeff);
2916 CI[K].NegPart = getNegativePart(CI[K].Coeff);
2917 CI[K].Iterations = collectUpperBound(L, Subscript->getType());
2918 Subscript = AddRec->getStart();
2919 }
2920 Constant = Subscript;
2921#ifndef NDEBUG1
2922 LLVM_DEBUG(dbgs() << "\tCoefficient Info\n")do { } while (false);
2923 for (unsigned K = 1; K <= MaxLevels; ++K) {
2924 LLVM_DEBUG(dbgs() << "\t " << K << "\t" << *CI[K].Coeff)do { } while (false);
2925 LLVM_DEBUG(dbgs() << "\tPos Part = ")do { } while (false);
2926 LLVM_DEBUG(dbgs() << *CI[K].PosPart)do { } while (false);
2927 LLVM_DEBUG(dbgs() << "\tNeg Part = ")do { } while (false);
2928 LLVM_DEBUG(dbgs() << *CI[K].NegPart)do { } while (false);
2929 LLVM_DEBUG(dbgs() << "\tUpper Bound = ")do { } while (false);
2930 if (CI[K].Iterations)
2931 LLVM_DEBUG(dbgs() << *CI[K].Iterations)do { } while (false);
2932 else
2933 LLVM_DEBUG(dbgs() << "+inf")do { } while (false);
2934 LLVM_DEBUG(dbgs() << '\n')do { } while (false);
2935 }
2936 LLVM_DEBUG(dbgs() << "\t Constant = " << *Subscript << '\n')do { } while (false);
2937#endif
2938 return CI;
2939}
2940
2941
2942// Looks through all the bounds info and
2943// computes the lower bound given the current direction settings
2944// at each level. If the lower bound for any level is -inf,
2945// the result is -inf.
2946const SCEV *DependenceInfo::getLowerBound(BoundInfo *Bound) const {
2947 const SCEV *Sum = Bound[1].Lower[Bound[1].Direction];
2948 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
2949 if (Bound[K].Lower[Bound[K].Direction])
2950 Sum = SE->getAddExpr(Sum, Bound[K].Lower[Bound[K].Direction]);
2951 else
2952 Sum = nullptr;
2953 }
2954 return Sum;
2955}
2956
2957
2958// Looks through all the bounds info and
2959// computes the upper bound given the current direction settings
2960// at each level. If the upper bound at any level is +inf,
2961// the result is +inf.
2962const SCEV *DependenceInfo::getUpperBound(BoundInfo *Bound) const {
2963 const SCEV *Sum = Bound[1].Upper[Bound[1].Direction];
2964 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
2965 if (Bound[K].Upper[Bound[K].Direction])
2966 Sum = SE->getAddExpr(Sum, Bound[K].Upper[Bound[K].Direction]);
2967 else
2968 Sum = nullptr;
2969 }
2970 return Sum;
2971}
2972
2973
2974//===----------------------------------------------------------------------===//
2975// Constraint manipulation for Delta test.
2976
2977// Given a linear SCEV,
2978// return the coefficient (the step)
2979// corresponding to the specified loop.
2980// If there isn't one, return 0.
2981// For example, given a*i + b*j + c*k, finding the coefficient
2982// corresponding to the j loop would yield b.
2983const SCEV *DependenceInfo::findCoefficient(const SCEV *Expr,
2984 const Loop *TargetLoop) const {
2985 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
2986 if (!AddRec)
2987 return SE->getZero(Expr->getType());
2988 if (AddRec->getLoop() == TargetLoop)
2989 return AddRec->getStepRecurrence(*SE);
2990 return findCoefficient(AddRec->getStart(), TargetLoop);
2991}
2992
2993
2994// Given a linear SCEV,
2995// return the SCEV given by zeroing out the coefficient
2996// corresponding to the specified loop.
2997// For example, given a*i + b*j + c*k, zeroing the coefficient
2998// corresponding to the j loop would yield a*i + c*k.
2999const SCEV *DependenceInfo::zeroCoefficient(const SCEV *Expr,
3000 const Loop *TargetLoop) const {
3001 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
3002 if (!AddRec)
3003 return Expr; // ignore
3004 if (AddRec->getLoop() == TargetLoop)
3005 return AddRec->getStart();
3006 return SE->getAddRecExpr(zeroCoefficient(AddRec->getStart(), TargetLoop),
3007 AddRec->getStepRecurrence(*SE),
3008 AddRec->getLoop(),
3009 AddRec->getNoWrapFlags());
3010}
3011
3012
3013// Given a linear SCEV Expr,
3014// return the SCEV given by adding some Value to the
3015// coefficient corresponding to the specified TargetLoop.
3016// For example, given a*i + b*j + c*k, adding 1 to the coefficient
3017// corresponding to the j loop would yield a*i + (b+1)*j + c*k.
3018const SCEV *DependenceInfo::addToCoefficient(const SCEV *Expr,
3019 const Loop *TargetLoop,
3020 const SCEV *Value) const {
3021 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
3022 if (!AddRec) // create a new addRec
3023 return SE->getAddRecExpr(Expr,
3024 Value,
3025 TargetLoop,
3026 SCEV::FlagAnyWrap); // Worst case, with no info.
3027 if (AddRec->getLoop() == TargetLoop) {
3028 const SCEV *Sum = SE->getAddExpr(AddRec->getStepRecurrence(*SE), Value);
3029 if (Sum->isZero())
3030 return AddRec->getStart();
3031 return SE->getAddRecExpr(AddRec->getStart(),
3032 Sum,
3033 AddRec->getLoop(),
3034 AddRec->getNoWrapFlags());
3035 }
3036 if (SE->isLoopInvariant(AddRec, TargetLoop))
3037 return SE->getAddRecExpr(AddRec, Value, TargetLoop, SCEV::FlagAnyWrap);
3038 return SE->getAddRecExpr(
3039 addToCoefficient(AddRec->getStart(), TargetLoop, Value),
3040 AddRec->getStepRecurrence(*SE), AddRec->getLoop(),
3041 AddRec->getNoWrapFlags());
3042}
3043
3044
3045// Review the constraints, looking for opportunities
3046// to simplify a subscript pair (Src and Dst).
3047// Return true if some simplification occurs.
3048// If the simplification isn't exact (that is, if it is conservative
3049// in terms of dependence), set consistent to false.
3050// Corresponds to Figure 5 from the paper
3051//
3052// Practical Dependence Testing
3053// Goff, Kennedy, Tseng
3054// PLDI 1991
3055bool DependenceInfo::propagate(const SCEV *&Src, const SCEV *&Dst,
3056 SmallBitVector &Loops,
3057 SmallVectorImpl<Constraint> &Constraints,
3058 bool &Consistent) {
3059 bool Result = false;
3060 for (unsigned LI : Loops.set_bits()) {
3061 LLVM_DEBUG(dbgs() << "\t Constraint[" << LI << "] is")do { } while (false);
3062 LLVM_DEBUG(Constraints[LI].dump(dbgs()))do { } while (false);
3063 if (Constraints[LI].isDistance())
3064 Result |= propagateDistance(Src, Dst, Constraints[LI], Consistent);
3065 else if (Constraints[LI].isLine())
3066 Result |= propagateLine(Src, Dst, Constraints[LI], Consistent);
3067 else if (Constraints[LI].isPoint())
3068 Result |= propagatePoint(Src, Dst, Constraints[LI]);
3069 }
3070 return Result;
3071}
3072
3073
3074// Attempt to propagate a distance
3075// constraint into a subscript pair (Src and Dst).
3076// Return true if some simplification occurs.
3077// If the simplification isn't exact (that is, if it is conservative
3078// in terms of dependence), set consistent to false.
3079bool DependenceInfo::propagateDistance(const SCEV *&Src, const SCEV *&Dst,
3080 Constraint &CurConstraint,
3081 bool &Consistent) {
3082 const Loop *CurLoop = CurConstraint.getAssociatedLoop();
3083 LLVM_DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n")do { } while (false);
3084 const SCEV *A_K = findCoefficient(Src, CurLoop);
3085 if (A_K->isZero())
3086 return false;
3087 const SCEV *DA_K = SE->getMulExpr(A_K, CurConstraint.getD());
3088 Src = SE->getMinusSCEV(Src, DA_K);
3089 Src = zeroCoefficient(Src, CurLoop);
3090 LLVM_DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n")do { } while (false);
3091 LLVM_DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n")do { } while (false);
3092 Dst = addToCoefficient(Dst, CurLoop, SE->getNegativeSCEV(A_K));
3093 LLVM_DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n")do { } while (false);
3094 if (!findCoefficient(Dst, CurLoop)->isZero())
3095 Consistent = false;
3096 return true;
3097}
3098
3099
3100// Attempt to propagate a line
3101// constraint into a subscript pair (Src and Dst).
3102// Return true if some simplification occurs.
3103// If the simplification isn't exact (that is, if it is conservative
3104// in terms of dependence), set consistent to false.
3105bool DependenceInfo::propagateLine(const SCEV *&Src, const SCEV *&Dst,
3106 Constraint &CurConstraint,
3107 bool &Consistent) {
3108 const Loop *CurLoop = CurConstraint.getAssociatedLoop();
3109 const SCEV *A = CurConstraint.getA();
3110 const SCEV *B = CurConstraint.getB();
3111 const SCEV *C = CurConstraint.getC();
3112 LLVM_DEBUG(dbgs() << "\t\tA = " << *A << ", B = " << *B << ", C = " << *Cdo { } while (false)
3113 << "\n")do { } while (false);
3114 LLVM_DEBUG(dbgs() << "\t\tSrc = " << *Src << "\n")do { } while (false);
3115 LLVM_DEBUG(dbgs() << "\t\tDst = " << *Dst << "\n")do { } while (false);
3116 if (A->isZero()) {
3117 const SCEVConstant *Bconst = dyn_cast<SCEVConstant>(B);
3118 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3119 if (!Bconst || !Cconst) return false;
3120 APInt Beta = Bconst->getAPInt();
3121 APInt Charlie = Cconst->getAPInt();
3122 APInt CdivB = Charlie.sdiv(Beta);
3123 assert(Charlie.srem(Beta) == 0 && "C should be evenly divisible by B")((void)0);
3124 const SCEV *AP_K = findCoefficient(Dst, CurLoop);
3125 // Src = SE->getAddExpr(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB)));
3126 Src = SE->getMinusSCEV(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB)));
3127 Dst = zeroCoefficient(Dst, CurLoop);
3128 if (!findCoefficient(Src, CurLoop)->isZero())
3129 Consistent = false;
3130 }
3131 else if (B->isZero()) {
3132 const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A);
3133 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3134 if (!Aconst || !Cconst) return false;
3135 APInt Alpha = Aconst->getAPInt();
3136 APInt Charlie = Cconst->getAPInt();
3137 APInt CdivA = Charlie.sdiv(Alpha);
3138 assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A")((void)0);
3139 const SCEV *A_K = findCoefficient(Src, CurLoop);
3140 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA)));
3141 Src = zeroCoefficient(Src, CurLoop);
3142 if (!findCoefficient(Dst, CurLoop)->isZero())
3143 Consistent = false;
3144 }
3145 else if (isKnownPredicate(CmpInst::ICMP_EQ, A, B)) {
3146 const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A);
3147 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3148 if (!Aconst || !Cconst) return false;
3149 APInt Alpha = Aconst->getAPInt();
3150 APInt Charlie = Cconst->getAPInt();
3151 APInt CdivA = Charlie.sdiv(Alpha);
3152 assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A")((void)0);
3153 const SCEV *A_K = findCoefficient(Src, CurLoop);
3154 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA)));
3155 Src = zeroCoefficient(Src, CurLoop);
3156 Dst = addToCoefficient(Dst, CurLoop, A_K);
3157 if (!findCoefficient(Dst, CurLoop)->isZero())
3158 Consistent = false;
3159 }
3160 else {
3161 // paper is incorrect here, or perhaps just misleading
3162 const SCEV *A_K = findCoefficient(Src, CurLoop);
3163 Src = SE->getMulExpr(Src, A);
3164 Dst = SE->getMulExpr(Dst, A);
3165 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, C));
3166 Src = zeroCoefficient(Src, CurLoop);
3167 Dst = addToCoefficient(Dst, CurLoop, SE->getMulExpr(A_K, B));
3168 if (!findCoefficient(Dst, CurLoop)->isZero())
3169 Consistent = false;
3170 }
3171 LLVM_DEBUG(dbgs() << "\t\tnew Src = " << *Src << "\n")do { } while (false);
3172 LLVM_DEBUG(dbgs() << "\t\tnew Dst = " << *Dst << "\n")do { } while (false);
3173 return true;
3174}
3175
3176
3177// Attempt to propagate a point
3178// constraint into a subscript pair (Src and Dst).
3179// Return true if some simplification occurs.
3180bool DependenceInfo::propagatePoint(const SCEV *&Src, const SCEV *&Dst,
3181 Constraint &CurConstraint) {
3182 const Loop *CurLoop = CurConstraint.getAssociatedLoop();
3183 const SCEV *A_K = findCoefficient(Src, CurLoop);
3184 const SCEV *AP_K = findCoefficient(Dst, CurLoop);
3185 const SCEV *XA_K = SE->getMulExpr(A_K, CurConstraint.getX());
3186 const SCEV *YAP_K = SE->getMulExpr(AP_K, CurConstraint.getY());
3187 LLVM_DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n")do { } while (false);
3188 Src = SE->getAddExpr(Src, SE->getMinusSCEV(XA_K, YAP_K));
3189 Src = zeroCoefficient(Src, CurLoop);
3190 LLVM_DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n")do { } while (false);
3191 LLVM_DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n")do { } while (false);
3192 Dst = zeroCoefficient(Dst, CurLoop);
3193 LLVM_DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n")do { } while (false);
3194 return true;
3195}
3196
3197
3198// Update direction vector entry based on the current constraint.
3199void DependenceInfo::updateDirection(Dependence::DVEntry &Level,
3200 const Constraint &CurConstraint) const {
3201 LLVM_DEBUG(dbgs() << "\tUpdate direction, constraint =")do { } while (false);
3202 LLVM_DEBUG(CurConstraint.dump(dbgs()))do { } while (false);
3203 if (CurConstraint.isAny())
3204 ; // use defaults
3205 else if (CurConstraint.isDistance()) {
3206 // this one is consistent, the others aren't
3207 Level.Scalar = false;
3208 Level.Distance = CurConstraint.getD();
3209 unsigned NewDirection = Dependence::DVEntry::NONE;
3210 if (!SE->isKnownNonZero(Level.Distance)) // if may be zero
3211 NewDirection = Dependence::DVEntry::EQ;
3212 if (!SE->isKnownNonPositive(Level.Distance)) // if may be positive
3213 NewDirection |= Dependence::DVEntry::LT;
3214 if (!SE->isKnownNonNegative(Level.Distance)) // if may be negative
3215 NewDirection |= Dependence::DVEntry::GT;
3216 Level.Direction &= NewDirection;
3217 }
3218 else if (CurConstraint.isLine()) {
3219 Level.Scalar = false;
3220 Level.Distance = nullptr;
3221 // direction should be accurate
3222 }
3223 else if (CurConstraint.isPoint()) {
3224 Level.Scalar = false;
3225 Level.Distance = nullptr;
3226 unsigned NewDirection = Dependence::DVEntry::NONE;
3227 if (!isKnownPredicate(CmpInst::ICMP_NE,
3228 CurConstraint.getY(),
3229 CurConstraint.getX()))
3230 // if X may be = Y
3231 NewDirection |= Dependence::DVEntry::EQ;
3232 if (!isKnownPredicate(CmpInst::ICMP_SLE,
3233 CurConstraint.getY(),
3234 CurConstraint.getX()))
3235 // if Y may be > X
3236 NewDirection |= Dependence::DVEntry::LT;
3237 if (!isKnownPredicate(CmpInst::ICMP_SGE,
3238 CurConstraint.getY(),
3239 CurConstraint.getX()))
3240 // if Y may be < X
3241 NewDirection |= Dependence::DVEntry::GT;
3242 Level.Direction &= NewDirection;
3243 }
3244 else
3245 llvm_unreachable("constraint has unexpected kind")__builtin_unreachable();
3246}
3247
3248/// Check if we can delinearize the subscripts. If the SCEVs representing the
3249/// source and destination array references are recurrences on a nested loop,
3250/// this function flattens the nested recurrences into separate recurrences
3251/// for each loop level.
3252bool DependenceInfo::tryDelinearize(Instruction *Src, Instruction *Dst,
3253 SmallVectorImpl<Subscript> &Pair) {
3254 assert(isLoadOrStore(Src) && "instruction is not load or store")((void)0);
3255 assert(isLoadOrStore(Dst) && "instruction is not load or store")((void)0);
3256 Value *SrcPtr = getLoadStorePointerOperand(Src);
3257 Value *DstPtr = getLoadStorePointerOperand(Dst);
3258 Loop *SrcLoop = LI->getLoopFor(Src->getParent());
3259 Loop *DstLoop = LI->getLoopFor(Dst->getParent());
3260 const SCEV *SrcAccessFn = SE->getSCEVAtScope(SrcPtr, SrcLoop);
3261 const SCEV *DstAccessFn = SE->getSCEVAtScope(DstPtr, DstLoop);
3262 const SCEVUnknown *SrcBase =
3263 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
3264 const SCEVUnknown *DstBase =
3265 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
3266
3267 if (!SrcBase || !DstBase || SrcBase != DstBase)
3268 return false;
3269
3270 SmallVector<const SCEV *, 4> SrcSubscripts, DstSubscripts;
3271
3272 if (!tryDelinearizeFixedSize(Src, Dst, SrcAccessFn, DstAccessFn,
3273 SrcSubscripts, DstSubscripts) &&
3274 !tryDelinearizeParametricSize(Src, Dst, SrcAccessFn, DstAccessFn,
3275 SrcSubscripts, DstSubscripts))
3276 return false;
3277
3278 int Size = SrcSubscripts.size();
3279 LLVM_DEBUG({do { } while (false)
3280 dbgs() << "\nSrcSubscripts: ";do { } while (false)
3281 for (int I = 0; I < Size; I++)do { } while (false)
3282 dbgs() << *SrcSubscripts[I];do { } while (false)
3283 dbgs() << "\nDstSubscripts: ";do { } while (false)
3284 for (int I = 0; I < Size; I++)do { } while (false)
3285 dbgs() << *DstSubscripts[I];do { } while (false)
3286 })do { } while (false);
3287
3288 // The delinearization transforms a single-subscript MIV dependence test into
3289 // a multi-subscript SIV dependence test that is easier to compute. So we
3290 // resize Pair to contain as many pairs of subscripts as the delinearization
3291 // has found, and then initialize the pairs following the delinearization.
3292 Pair.resize(Size);
3293 for (int I = 0; I < Size; ++I) {
3294 Pair[I].Src = SrcSubscripts[I];
3295 Pair[I].Dst = DstSubscripts[I];
3296 unifySubscriptType(&Pair[I]);
3297 }
3298
3299 return true;
3300}
3301
3302bool DependenceInfo::tryDelinearizeFixedSize(
3303 Instruction *Src, Instruction *Dst, const SCEV *SrcAccessFn,
3304 const SCEV *DstAccessFn, SmallVectorImpl<const SCEV *> &SrcSubscripts,
3305 SmallVectorImpl<const SCEV *> &DstSubscripts) {
3306
3307 Value *SrcPtr = getLoadStorePointerOperand(Src);
3308 Value *DstPtr = getLoadStorePointerOperand(Dst);
3309 const SCEVUnknown *SrcBase =
3310 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
3311 const SCEVUnknown *DstBase =
3312 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
3313 assert(SrcBase && DstBase && SrcBase == DstBase &&((void)0)
3314 "expected src and dst scev unknowns to be equal")((void)0);
3315
3316 // Check the simple case where the array dimensions are fixed size.
3317 auto *SrcGEP = dyn_cast<GetElementPtrInst>(SrcPtr);
3318 auto *DstGEP = dyn_cast<GetElementPtrInst>(DstPtr);
3319 if (!SrcGEP || !DstGEP)
3320 return false;
3321
3322 SmallVector<int, 4> SrcSizes, DstSizes;
3323 SE->getIndexExpressionsFromGEP(SrcGEP, SrcSubscripts, SrcSizes);
3324 SE->getIndexExpressionsFromGEP(DstGEP, DstSubscripts, DstSizes);
3325
3326 // Check that the two size arrays are non-empty and equal in length and
3327 // value.
3328 if (SrcSizes.empty() || SrcSubscripts.size() <= 1 ||
3329 SrcSizes.size() != DstSizes.size() ||
3330 !std::equal(SrcSizes.begin(), SrcSizes.end(), DstSizes.begin())) {
3331 SrcSubscripts.clear();
3332 DstSubscripts.clear();
3333 return false;
3334 }
3335
3336 Value *SrcBasePtr = SrcGEP->getOperand(0);
3337 Value *DstBasePtr = DstGEP->getOperand(0);
3338 while (auto *PCast = dyn_cast<BitCastInst>(SrcBasePtr))
3339 SrcBasePtr = PCast->getOperand(0);
3340 while (auto *PCast = dyn_cast<BitCastInst>(DstBasePtr))
3341 DstBasePtr = PCast->getOperand(0);
3342
3343 // Check that for identical base pointers we do not miss index offsets
3344 // that have been added before this GEP is applied.
3345 if (SrcBasePtr != SrcBase->getValue() || DstBasePtr != DstBase->getValue()) {
3346 SrcSubscripts.clear();
3347 DstSubscripts.clear();
3348 return false;
3349 }
3350
3351 assert(SrcSubscripts.size() == DstSubscripts.size() &&((void)0)
3352 SrcSubscripts.size() == SrcSizes.size() + 1 &&((void)0)
3353 "Expected equal number of entries in the list of sizes and "((void)0)
3354 "subscripts.")((void)0);
3355
3356 // In general we cannot safely assume that the subscripts recovered from GEPs
3357 // are in the range of values defined for their corresponding array
3358 // dimensions. For example some C language usage/interpretation make it
3359 // impossible to verify this at compile-time. As such we can only delinearize
3360 // iff the subscripts are positive and are less than the range of the
3361 // dimension.
3362 if (!DisableDelinearizationChecks) {
3363 auto AllIndiciesInRange = [&](SmallVector<int, 4> &DimensionSizes,
3364 SmallVectorImpl<const SCEV *> &Subscripts,
3365 Value *Ptr) {
3366 size_t SSize = Subscripts.size();
3367 for (size_t I = 1; I < SSize; ++I) {
3368 const SCEV *S = Subscripts[I];
3369 if (!isKnownNonNegative(S, Ptr))
3370 return false;
3371 if (auto *SType = dyn_cast<IntegerType>(S->getType())) {
3372 const SCEV *Range = SE->getConstant(
3373 ConstantInt::get(SType, DimensionSizes[I - 1], false));
3374 if (!isKnownLessThan(S, Range))
3375 return false;
3376 }
3377 }
3378 return true;
3379 };
3380
3381 if (!AllIndiciesInRange(SrcSizes, SrcSubscripts, SrcPtr) ||
3382 !AllIndiciesInRange(DstSizes, DstSubscripts, DstPtr)) {
3383 SrcSubscripts.clear();
3384 DstSubscripts.clear();
3385 return false;
3386 }
3387 }
3388 LLVM_DEBUG({do { } while (false)
3389 dbgs() << "Delinearized subscripts of fixed-size array\n"do { } while (false)
3390 << "SrcGEP:" << *SrcGEP << "\n"do { } while (false)
3391 << "DstGEP:" << *DstGEP << "\n";do { } while (false)
3392 })do { } while (false);
3393 return true;
3394}
3395
3396bool DependenceInfo::tryDelinearizeParametricSize(
3397 Instruction *Src, Instruction *Dst, const SCEV *SrcAccessFn,
3398 const SCEV *DstAccessFn, SmallVectorImpl<const SCEV *> &SrcSubscripts,
3399 SmallVectorImpl<const SCEV *> &DstSubscripts) {
3400
3401 Value *SrcPtr = getLoadStorePointerOperand(Src);
3402 Value *DstPtr = getLoadStorePointerOperand(Dst);
3403 const SCEVUnknown *SrcBase =
3404 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
3405 const SCEVUnknown *DstBase =
3406 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
3407 assert(SrcBase && DstBase && SrcBase == DstBase &&((void)0)
3408 "expected src and dst scev unknowns to be equal")((void)0);
3409
3410 const SCEV *ElementSize = SE->getElementSize(Src);
3411 if (ElementSize != SE->getElementSize(Dst))
3412 return false;
3413
3414 const SCEV *SrcSCEV = SE->getMinusSCEV(SrcAccessFn, SrcBase);
3415 const SCEV *DstSCEV = SE->getMinusSCEV(DstAccessFn, DstBase);
3416
3417 const SCEVAddRecExpr *SrcAR = dyn_cast<SCEVAddRecExpr>(SrcSCEV);
3418 const SCEVAddRecExpr *DstAR = dyn_cast<SCEVAddRecExpr>(DstSCEV);
3419 if (!SrcAR || !DstAR || !SrcAR->isAffine() || !DstAR->isAffine())
3420 return false;
3421
3422 // First step: collect parametric terms in both array references.
3423 SmallVector<const SCEV *, 4> Terms;
3424 SE->collectParametricTerms(SrcAR, Terms);
3425 SE->collectParametricTerms(DstAR, Terms);
3426
3427 // Second step: find subscript sizes.
3428 SmallVector<const SCEV *, 4> Sizes;
3429 SE->findArrayDimensions(Terms, Sizes, ElementSize);
3430
3431 // Third step: compute the access functions for each subscript.
3432 SE->computeAccessFunctions(SrcAR, SrcSubscripts, Sizes);
3433 SE->computeAccessFunctions(DstAR, DstSubscripts, Sizes);
3434
3435 // Fail when there is only a subscript: that's a linearized access function.
3436 if (SrcSubscripts.size() < 2 || DstSubscripts.size() < 2 ||
3437 SrcSubscripts.size() != DstSubscripts.size())
3438 return false;
3439
3440 size_t Size = SrcSubscripts.size();
3441
3442 // Statically check that the array bounds are in-range. The first subscript we
3443 // don't have a size for and it cannot overflow into another subscript, so is
3444 // always safe. The others need to be 0 <= subscript[i] < bound, for both src
3445 // and dst.
3446 // FIXME: It may be better to record these sizes and add them as constraints
3447 // to the dependency checks.
3448 if (!DisableDelinearizationChecks)
3449 for (size_t I = 1; I < Size; ++I) {
3450 if (!isKnownNonNegative(SrcSubscripts[I], SrcPtr))
3451 return false;
3452
3453 if (!isKnownLessThan(SrcSubscripts[I], Sizes[I - 1]))
3454 return false;
3455
3456 if (!isKnownNonNegative(DstSubscripts[I], DstPtr))
3457 return false;
3458
3459 if (!isKnownLessThan(DstSubscripts[I], Sizes[I - 1]))
3460 return false;
3461 }
3462
3463 return true;
3464}
3465
3466//===----------------------------------------------------------------------===//
3467
3468#ifndef NDEBUG1
3469// For debugging purposes, dump a small bit vector to dbgs().
3470static void dumpSmallBitVector(SmallBitVector &BV) {
3471 dbgs() << "{";
3472 for (unsigned VI : BV.set_bits()) {
3473 dbgs() << VI;
3474 if (BV.find_next(VI) >= 0)
3475 dbgs() << ' ';
3476 }
3477 dbgs() << "}\n";
3478}
3479#endif
3480
3481bool DependenceInfo::invalidate(Function &F, const PreservedAnalyses &PA,
3482 FunctionAnalysisManager::Invalidator &Inv) {
3483 // Check if the analysis itself has been invalidated.
3484 auto PAC = PA.getChecker<DependenceAnalysis>();
3485 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>())
3486 return true;
3487
3488 // Check transitive dependencies.
3489 return Inv.invalidate<AAManager>(F, PA) ||
3490 Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) ||
3491 Inv.invalidate<LoopAnalysis>(F, PA);
3492}
3493
3494// depends -
3495// Returns NULL if there is no dependence.
3496// Otherwise, return a Dependence with as many details as possible.
3497// Corresponds to Section 3.1 in the paper
3498//
3499// Practical Dependence Testing
3500// Goff, Kennedy, Tseng
3501// PLDI 1991
3502//
3503// Care is required to keep the routine below, getSplitIteration(),
3504// up to date with respect to this routine.
3505std::unique_ptr<Dependence>
3506DependenceInfo::depends(Instruction *Src, Instruction *Dst,
3507 bool PossiblyLoopIndependent) {
3508 if (Src == Dst)
1
Assuming 'Src' is not equal to 'Dst'
2
Taking false branch
3509 PossiblyLoopIndependent = false;
3510
3511 if (!(Src->mayReadOrWriteMemory() && Dst->mayReadOrWriteMemory()))
3
Assuming the condition is true
4
Assuming the condition is false
5
Taking false branch
3512 // if both instructions don't reference memory, there's no dependence
3513 return nullptr;
3514
3515 if (!isLoadOrStore(Src) || !isLoadOrStore(Dst)) {
6
Calling 'isLoadOrStore'
16
Returning from 'isLoadOrStore'
17
Calling 'isLoadOrStore'
27
Returning from 'isLoadOrStore'
28
Taking false branch
3516 // can only analyze simple loads and stores, i.e., no calls, invokes, etc.
3517 LLVM_DEBUG(dbgs() << "can only handle simple loads and stores\n")do { } while (false);
3518 return std::make_unique<Dependence>(Src, Dst);
3519 }
3520
3521 assert(isLoadOrStore(Src) && "instruction is not load or store")((void)0);
3522 assert(isLoadOrStore(Dst) && "instruction is not load or store")((void)0);
3523 Value *SrcPtr = getLoadStorePointerOperand(Src);
3524 Value *DstPtr = getLoadStorePointerOperand(Dst);
3525
3526 switch (underlyingObjectsAlias(AA, F->getParent()->getDataLayout(),
29
Calling 'AliasResult::operator llvm::AliasResult::Kind'
31
Returning from 'AliasResult::operator llvm::AliasResult::Kind'
32
Control jumps to 'case MustAlias:' at line 3538
3527 MemoryLocation::get(Dst),
3528 MemoryLocation::get(Src))) {
3529 case AliasResult::MayAlias:
3530 case AliasResult::PartialAlias:
3531 // cannot analyse objects if we don't understand their aliasing.
3532 LLVM_DEBUG(dbgs() << "can't analyze may or partial alias\n")do { } while (false);
3533 return std::make_unique<Dependence>(Src, Dst);
3534 case AliasResult::NoAlias:
3535 // If the objects noalias, they are distinct, accesses are independent.
3536 LLVM_DEBUG(dbgs() << "no alias\n")do { } while (false);
3537 return nullptr;
3538 case AliasResult::MustAlias:
3539 break; // The underlying objects alias; test accesses for dependence.
33
Execution continues on line 3543
3540 }
3541
3542 // establish loop nesting levels
3543 establishNestingLevels(Src, Dst);
3544 LLVM_DEBUG(dbgs() << " common nesting levels = " << CommonLevels << "\n")do { } while (false);
34
Loop condition is false. Exiting loop
3545 LLVM_DEBUG(dbgs() << " maximum nesting levels = " << MaxLevels << "\n")do { } while (false);
35
Loop condition is false. Exiting loop
3546
3547 FullDependence Result(Src, Dst, PossiblyLoopIndependent, CommonLevels);
3548 ++TotalArrayPairs;
3549
3550 unsigned Pairs = 1;
3551 SmallVector<Subscript, 2> Pair(Pairs);
3552 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
3553 const SCEV *DstSCEV = SE->getSCEV(DstPtr);
3554 LLVM_DEBUG(dbgs() << " SrcSCEV = " << *SrcSCEV << "\n")do { } while (false);
36
Loop condition is false. Exiting loop
3555 LLVM_DEBUG(dbgs() << " DstSCEV = " << *DstSCEV << "\n")do { } while (false);
37
Loop condition is false. Exiting loop
3556 if (SE->getPointerBase(SrcSCEV) != SE->getPointerBase(DstSCEV)) {
38
Assuming the condition is false
39
Taking false branch
3557 // If two pointers have different bases, trying to analyze indexes won't
3558 // work; we can't compare them to each other. This can happen, for example,
3559 // if one is produced by an LCSSA PHI node.
3560 //
3561 // We check this upfront so we don't crash in cases where getMinusSCEV()
3562 // returns a SCEVCouldNotCompute.
3563 LLVM_DEBUG(dbgs() << "can't analyze SCEV with different pointer base\n")do { } while (false);
3564 return std::make_unique<Dependence>(Src, Dst);
3565 }
3566 Pair[0].Src = SrcSCEV;
3567 Pair[0].Dst = DstSCEV;
3568
3569 if (Delinearize) {
40
Assuming the condition is false
41
Taking false branch
3570 if (tryDelinearize(Src, Dst, Pair)) {
3571 LLVM_DEBUG(dbgs() << " delinearized\n")do { } while (false);
3572 Pairs = Pair.size();
3573 }
3574 }
3575
3576 for (unsigned P = 0; P < Pairs; ++P) {
42
Loop condition is true. Entering loop body
49
Loop condition is false. Execution continues on line 3595
3577 Pair[P].Loops.resize(MaxLevels + 1);
3578 Pair[P].GroupLoops.resize(MaxLevels + 1);
3579 Pair[P].Group.resize(Pairs);
3580 removeMatchingExtensions(&Pair[P]);
3581 Pair[P].Classification =
3582 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()),
3583 Pair[P].Dst, LI->getLoopFor(Dst->getParent()),
3584 Pair[P].Loops);
3585 Pair[P].GroupLoops = Pair[P].Loops;
3586 Pair[P].Group.set(P);
3587 LLVM_DEBUG(dbgs() << " subscript " << P << "\n")do { } while (false);
43
Loop condition is false. Exiting loop
3588 LLVM_DEBUG(dbgs() << "\tsrc = " << *Pair[P].Src << "\n")do { } while (false);
44
Loop condition is false. Exiting loop
3589 LLVM_DEBUG(dbgs() << "\tdst = " << *Pair[P].Dst << "\n")do { } while (false);
45
Loop condition is false. Exiting loop
3590 LLVM_DEBUG(dbgs() << "\tclass = " << Pair[P].Classification << "\n")do { } while (false);
46
Loop condition is false. Exiting loop
3591 LLVM_DEBUG(dbgs() << "\tloops = ")do { } while (false);
47
Loop condition is false. Exiting loop
3592 LLVM_DEBUG(dumpSmallBitVector(Pair[P].Loops))do { } while (false);
48
Loop condition is false. Exiting loop
3593 }
3594
3595 SmallBitVector Separable(Pairs);
3596 SmallBitVector Coupled(Pairs);
3597
3598 // Partition subscripts into separable and minimally-coupled groups
3599 // Algorithm in paper is algorithmically better;
3600 // this may be faster in practice. Check someday.
3601 //
3602 // Here's an example of how it works. Consider this code:
3603 //
3604 // for (i = ...) {
3605 // for (j = ...) {
3606 // for (k = ...) {
3607 // for (l = ...) {
3608 // for (m = ...) {
3609 // A[i][j][k][m] = ...;
3610 // ... = A[0][j][l][i + j];
3611 // }
3612 // }
3613 // }
3614 // }
3615 // }
3616 //
3617 // There are 4 subscripts here:
3618 // 0 [i] and [0]
3619 // 1 [j] and [j]
3620 // 2 [k] and [l]
3621 // 3 [m] and [i + j]
3622 //
3623 // We've already classified each subscript pair as ZIV, SIV, etc.,
3624 // and collected all the loops mentioned by pair P in Pair[P].Loops.
3625 // In addition, we've initialized Pair[P].GroupLoops to Pair[P].Loops
3626 // and set Pair[P].Group = {P}.
3627 //
3628 // Src Dst Classification Loops GroupLoops Group
3629 // 0 [i] [0] SIV {1} {1} {0}
3630 // 1 [j] [j] SIV {2} {2} {1}
3631 // 2 [k] [l] RDIV {3,4} {3,4} {2}
3632 // 3 [m] [i + j] MIV {1,2,5} {1,2,5} {3}
3633 //
3634 // For each subscript SI 0 .. 3, we consider each remaining subscript, SJ.
3635 // So, 0 is compared against 1, 2, and 3; 1 is compared against 2 and 3, etc.
3636 //
3637 // We begin by comparing 0 and 1. The intersection of the GroupLoops is empty.
3638 // Next, 0 and 2. Again, the intersection of their GroupLoops is empty.
3639 // Next 0 and 3. The intersection of their GroupLoop = {1}, not empty,
3640 // so Pair[3].Group = {0,3} and Done = false (that is, 0 will not be added
3641 // to either Separable or Coupled).
3642 //
3643 // Next, we consider 1 and 2. The intersection of the GroupLoops is empty.
3644 // Next, 1 and 3. The intersection of their GroupLoops = {2}, not empty,
3645 // so Pair[3].Group = {0, 1, 3} and Done = false.
3646 //
3647 // Next, we compare 2 against 3. The intersection of the GroupLoops is empty.
3648 // Since Done remains true, we add 2 to the set of Separable pairs.
3649 //
3650 // Finally, we consider 3. There's nothing to compare it with,
3651 // so Done remains true and we add it to the Coupled set.
3652 // Pair[3].Group = {0, 1, 3} and GroupLoops = {1, 2, 5}.
3653 //
3654 // In the end, we've got 1 separable subscript and 1 coupled group.
3655 for (unsigned SI = 0; SI < Pairs; ++SI) {
50
Loop condition is true. Entering loop body
59
Loop condition is false. Execution continues on line 3697
3656 if (Pair[SI].Classification == Subscript::NonLinear) {
51
Assuming field 'Classification' is not equal to NonLinear
52
Taking false branch
3657 // ignore these, but collect loops for later
3658 ++NonlinearSubscriptPairs;
3659 collectCommonLoops(Pair[SI].Src,
3660 LI->getLoopFor(Src->getParent()),
3661 Pair[SI].Loops);
3662 collectCommonLoops(Pair[SI].Dst,
3663 LI->getLoopFor(Dst->getParent()),
3664 Pair[SI].Loops);
3665 Result.Consistent = false;
3666 } else if (Pair[SI].Classification == Subscript::ZIV) {
53
Assuming field 'Classification' is not equal to ZIV
54
Taking false branch
3667 // always separable
3668 Separable.set(SI);
3669 }
3670 else {
3671 // SIV, RDIV, or MIV, so check for coupled group
3672 bool Done = true;
3673 for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) {
55
Loop condition is false. Execution continues on line 3684
3674 SmallBitVector Intersection = Pair[SI].GroupLoops;
3675 Intersection &= Pair[SJ].GroupLoops;
3676 if (Intersection.any()) {
3677 // accumulate set of all the loops in group
3678 Pair[SJ].GroupLoops |= Pair[SI].GroupLoops;
3679 // accumulate set of all subscripts in group
3680 Pair[SJ].Group |= Pair[SI].Group;
3681 Done = false;
3682 }
3683 }
3684 if (Done
55.1
'Done' is true
55.1
'Done' is true
55.1
'Done' is true
55.1
'Done' is true
55.1
'Done' is true
) {
56
Taking true branch
3685 if (Pair[SI].Group.count() == 1) {
57
Assuming the condition is false
58
Taking false branch
3686 Separable.set(SI);
3687 ++SeparableSubscriptPairs;
3688 }
3689 else {
3690 Coupled.set(SI);
3691 ++CoupledSubscriptPairs;
3692 }
3693 }
3694 }
3695 }
3696
3697 LLVM_DEBUG(dbgs() << " Separable = ")do { } while (false);
60
Loop condition is false. Exiting loop
3698 LLVM_DEBUG(dumpSmallBitVector(Separable))do { } while (false);
61
Loop condition is false. Exiting loop
3699 LLVM_DEBUG(dbgs() << " Coupled = ")do { } while (false);
62
Loop condition is false. Exiting loop
3700 LLVM_DEBUG(dumpSmallBitVector(Coupled))do { } while (false);
63
Loop condition is false. Exiting loop
3701
3702 Constraint NewConstraint;
3703 NewConstraint.setAny(SE);
3704
3705 // test separable subscripts
3706 for (unsigned SI : Separable.set_bits()) {
3707 LLVM_DEBUG(dbgs() << "testing subscript " << SI)do { } while (false);
3708 switch (Pair[SI].Classification) {
3709 case Subscript::ZIV:
3710 LLVM_DEBUG(dbgs() << ", ZIV\n")do { } while (false);
3711 if (testZIV(Pair[SI].Src, Pair[SI].Dst, Result))
3712 return nullptr;
3713 break;
3714 case Subscript::SIV: {
3715 LLVM_DEBUG(dbgs() << ", SIV\n")do { } while (false);
3716 unsigned Level;
3717 const SCEV *SplitIter = nullptr;
3718 if (testSIV(Pair[SI].Src, Pair[SI].Dst, Level, Result, NewConstraint,
3719 SplitIter))
3720 return nullptr;
3721 break;
3722 }
3723 case Subscript::RDIV:
3724 LLVM_DEBUG(dbgs() << ", RDIV\n")do { } while (false);
3725 if (testRDIV(Pair[SI].Src, Pair[SI].Dst, Result))
3726 return nullptr;
3727 break;
3728 case Subscript::MIV:
3729 LLVM_DEBUG(dbgs() << ", MIV\n")do { } while (false);
3730 if (testMIV(Pair[SI].Src, Pair[SI].Dst, Pair[SI].Loops, Result))
3731 return nullptr;
3732 break;
3733 default:
3734 llvm_unreachable("subscript has unexpected classification")__builtin_unreachable();
3735 }
3736 }
3737
3738 if (Coupled.count()) {
64
Calling 'SmallBitVector::count'
70
Returning from 'SmallBitVector::count'
71
Assuming the condition is true
72
Taking true branch
3739 // test coupled subscript groups
3740 LLVM_DEBUG(dbgs() << "starting on coupled subscripts\n")do { } while (false);
73
Loop condition is false. Exiting loop
3741 LLVM_DEBUG(dbgs() << "MaxLevels + 1 = " << MaxLevels + 1 << "\n")do { } while (false);
74
Loop condition is false. Exiting loop
3742 SmallVector<Constraint, 4> Constraints(MaxLevels + 1);
3743 for (unsigned II = 0; II
74.1
'II' is <= field 'MaxLevels'
74.1
'II' is <= field 'MaxLevels'
74.1
'II' is <= field 'MaxLevels'
74.1
'II' is <= field 'MaxLevels'
74.1
'II' is <= field 'MaxLevels'
<= MaxLevels
; ++II)
75
Loop condition is true. Entering loop body
76
Assuming 'II' is > field 'MaxLevels'
77
Loop condition is false. Execution continues on line 3745
3744 Constraints[II].setAny(SE);
3745 for (unsigned SI : Coupled.set_bits()) {
3746 LLVM_DEBUG(dbgs() << "testing subscript group " << SI << " { ")do { } while (false);
78
Loop condition is false. Exiting loop
3747 SmallBitVector Group(Pair[SI].Group);
3748 SmallBitVector Sivs(Pairs);
3749 SmallBitVector Mivs(Pairs);
3750 SmallBitVector ConstrainedLevels(MaxLevels + 1);
3751 SmallVector<Subscript *, 4> PairsInGroup;
3752 for (unsigned SJ : Group.set_bits()) {
82
Calling 'const_set_bits_iterator_impl::operator++'
101
Returning from 'const_set_bits_iterator_impl::operator++'
102
Calling 'const_set_bits_iterator_impl::operator*'
104
Returning from 'const_set_bits_iterator_impl::operator*'
105
'SJ' initialized to 4294967295
3753 LLVM_DEBUG(dbgs() << SJ << " ")do { } while (false);
79
Loop condition is false. Exiting loop
106
Loop condition is false. Exiting loop
3754 if (Pair[SJ].Classification == Subscript::SIV)
80
Assuming field 'Classification' is not equal to SIV
81
Taking false branch
107
Assuming field 'Classification' is not equal to SIV
108
Taking false branch
3755 Sivs.set(SJ);
3756 else
3757 Mivs.set(SJ);
109
Passing the value 4294967295 via 1st parameter 'Idx'
110
Calling 'SmallBitVector::set'
3758 PairsInGroup.push_back(&Pair[SJ]);
3759 }
3760 unifySubscriptType(PairsInGroup);
3761 LLVM_DEBUG(dbgs() << "}\n")do { } while (false);
3762 while (Sivs.any()) {
3763 bool Changed = false;
3764 for (unsigned SJ : Sivs.set_bits()) {
3765 LLVM_DEBUG(dbgs() << "testing subscript " << SJ << ", SIV\n")do { } while (false);
3766 // SJ is an SIV subscript that's part of the current coupled group
3767 unsigned Level;
3768 const SCEV *SplitIter = nullptr;
3769 LLVM_DEBUG(dbgs() << "SIV\n")do { } while (false);
3770 if (testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level, Result, NewConstraint,
3771 SplitIter))
3772 return nullptr;
3773 ConstrainedLevels.set(Level);
3774 if (intersectConstraints(&Constraints[Level], &NewConstraint)) {
3775 if (Constraints[Level].isEmpty()) {
3776 ++DeltaIndependence;
3777 return nullptr;
3778 }
3779 Changed = true;
3780 }
3781 Sivs.reset(SJ);
3782 }
3783 if (Changed) {
3784 // propagate, possibly creating new SIVs and ZIVs
3785 LLVM_DEBUG(dbgs() << " propagating\n")do { } while (false);
3786 LLVM_DEBUG(dbgs() << "\tMivs = ")do { } while (false);
3787 LLVM_DEBUG(dumpSmallBitVector(Mivs))do { } while (false);
3788 for (unsigned SJ : Mivs.set_bits()) {
3789 // SJ is an MIV subscript that's part of the current coupled group
3790 LLVM_DEBUG(dbgs() << "\tSJ = " << SJ << "\n")do { } while (false);
3791 if (propagate(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops,
3792 Constraints, Result.Consistent)) {
3793 LLVM_DEBUG(dbgs() << "\t Changed\n")do { } while (false);
3794 ++DeltaPropagations;
3795 Pair[SJ].Classification =
3796 classifyPair(Pair[SJ].Src, LI->getLoopFor(Src->getParent()),
3797 Pair[SJ].Dst, LI->getLoopFor(Dst->getParent()),
3798 Pair[SJ].Loops);
3799 switch (Pair[SJ].Classification) {
3800 case Subscript::ZIV:
3801 LLVM_DEBUG(dbgs() << "ZIV\n")do { } while (false);
3802 if (testZIV(Pair[SJ].Src, Pair[SJ].Dst, Result))
3803 return nullptr;
3804 Mivs.reset(SJ);
3805 break;
3806 case Subscript::SIV:
3807 Sivs.set(SJ);
3808 Mivs.reset(SJ);
3809 break;
3810 case Subscript::RDIV:
3811 case Subscript::MIV:
3812 break;
3813 default:
3814 llvm_unreachable("bad subscript classification")__builtin_unreachable();
3815 }
3816 }
3817 }
3818 }
3819 }
3820
3821 // test & propagate remaining RDIVs
3822 for (unsigned SJ : Mivs.set_bits()) {
3823 if (Pair[SJ].Classification == Subscript::RDIV) {
3824 LLVM_DEBUG(dbgs() << "RDIV test\n")do { } while (false);
3825 if (testRDIV(Pair[SJ].Src, Pair[SJ].Dst, Result))
3826 return nullptr;
3827 // I don't yet understand how to propagate RDIV results
3828 Mivs.reset(SJ);
3829 }
3830 }
3831
3832 // test remaining MIVs
3833 // This code is temporary.
3834 // Better to somehow test all remaining subscripts simultaneously.
3835 for (unsigned SJ : Mivs.set_bits()) {
3836 if (Pair[SJ].Classification == Subscript::MIV) {
3837 LLVM_DEBUG(dbgs() << "MIV test\n")do { } while (false);
3838 if (testMIV(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops, Result))
3839 return nullptr;
3840 }
3841 else
3842 llvm_unreachable("expected only MIV subscripts at this point")__builtin_unreachable();
3843 }
3844
3845 // update Result.DV from constraint vector
3846 LLVM_DEBUG(dbgs() << " updating\n")do { } while (false);
3847 for (unsigned SJ : ConstrainedLevels.set_bits()) {
3848 if (SJ > CommonLevels)
3849 break;
3850 updateDirection(Result.DV[SJ - 1], Constraints[SJ]);
3851 if (Result.DV[SJ - 1].Direction == Dependence::DVEntry::NONE)
3852 return nullptr;
3853 }
3854 }
3855 }
3856
3857 // Make sure the Scalar flags are set correctly.
3858 SmallBitVector CompleteLoops(MaxLevels + 1);
3859 for (unsigned SI = 0; SI < Pairs; ++SI)
3860 CompleteLoops |= Pair[SI].Loops;
3861 for (unsigned II = 1; II <= CommonLevels; ++II)
3862 if (CompleteLoops[II])
3863 Result.DV[II - 1].Scalar = false;
3864
3865 if (PossiblyLoopIndependent) {
3866 // Make sure the LoopIndependent flag is set correctly.
3867 // All directions must include equal, otherwise no
3868 // loop-independent dependence is possible.
3869 for (unsigned II = 1; II <= CommonLevels; ++II) {
3870 if (!(Result.getDirection(II) & Dependence::DVEntry::EQ)) {
3871 Result.LoopIndependent = false;
3872 break;
3873 }
3874 }
3875 }
3876 else {
3877 // On the other hand, if all directions are equal and there's no
3878 // loop-independent dependence possible, then no dependence exists.
3879 bool AllEqual = true;
3880 for (unsigned II = 1; II <= CommonLevels; ++II) {
3881 if (Result.getDirection(II) != Dependence::DVEntry::EQ) {
3882 AllEqual = false;
3883 break;
3884 }
3885 }
3886 if (AllEqual)
3887 return nullptr;
3888 }
3889
3890 return std::make_unique<FullDependence>(std::move(Result));
3891}
3892
3893//===----------------------------------------------------------------------===//
3894// getSplitIteration -
3895// Rather than spend rarely-used space recording the splitting iteration
3896// during the Weak-Crossing SIV test, we re-compute it on demand.
3897// The re-computation is basically a repeat of the entire dependence test,
3898// though simplified since we know that the dependence exists.
3899// It's tedious, since we must go through all propagations, etc.
3900//
3901// Care is required to keep this code up to date with respect to the routine
3902// above, depends().
3903//
3904// Generally, the dependence analyzer will be used to build
3905// a dependence graph for a function (basically a map from instructions
3906// to dependences). Looking for cycles in the graph shows us loops
3907// that cannot be trivially vectorized/parallelized.
3908//
3909// We can try to improve the situation by examining all the dependences
3910// that make up the cycle, looking for ones we can break.
3911// Sometimes, peeling the first or last iteration of a loop will break
3912// dependences, and we've got flags for those possibilities.
3913// Sometimes, splitting a loop at some other iteration will do the trick,
3914// and we've got a flag for that case. Rather than waste the space to
3915// record the exact iteration (since we rarely know), we provide
3916// a method that calculates the iteration. It's a drag that it must work
3917// from scratch, but wonderful in that it's possible.
3918//
3919// Here's an example:
3920//
3921// for (i = 0; i < 10; i++)
3922// A[i] = ...
3923// ... = A[11 - i]
3924//
3925// There's a loop-carried flow dependence from the store to the load,
3926// found by the weak-crossing SIV test. The dependence will have a flag,
3927// indicating that the dependence can be broken by splitting the loop.
3928// Calling getSplitIteration will return 5.
3929// Splitting the loop breaks the dependence, like so:
3930//
3931// for (i = 0; i <= 5; i++)
3932// A[i] = ...
3933// ... = A[11 - i]
3934// for (i = 6; i < 10; i++)
3935// A[i] = ...
3936// ... = A[11 - i]
3937//
3938// breaks the dependence and allows us to vectorize/parallelize
3939// both loops.
3940const SCEV *DependenceInfo::getSplitIteration(const Dependence &Dep,
3941 unsigned SplitLevel) {
3942 assert(Dep.isSplitable(SplitLevel) &&((void)0)
3943 "Dep should be splitable at SplitLevel")((void)0);
3944 Instruction *Src = Dep.getSrc();
3945 Instruction *Dst = Dep.getDst();
3946 assert(Src->mayReadFromMemory() || Src->mayWriteToMemory())((void)0);
3947 assert(Dst->mayReadFromMemory() || Dst->mayWriteToMemory())((void)0);
3948 assert(isLoadOrStore(Src))((void)0);
3949 assert(isLoadOrStore(Dst))((void)0);
3950 Value *SrcPtr = getLoadStorePointerOperand(Src);
3951 Value *DstPtr = getLoadStorePointerOperand(Dst);
3952 assert(underlyingObjectsAlias(((void)0)
3953 AA, F->getParent()->getDataLayout(), MemoryLocation::get(Dst),((void)0)
3954 MemoryLocation::get(Src)) == AliasResult::MustAlias)((void)0);
3955
3956 // establish loop nesting levels
3957 establishNestingLevels(Src, Dst);
3958
3959 FullDependence Result(Src, Dst, false, CommonLevels);
3960
3961 unsigned Pairs = 1;
3962 SmallVector<Subscript, 2> Pair(Pairs);
3963 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
3964 const SCEV *DstSCEV = SE->getSCEV(DstPtr);
3965 Pair[0].Src = SrcSCEV;
3966 Pair[0].Dst = DstSCEV;
3967
3968 if (Delinearize) {
3969 if (tryDelinearize(Src, Dst, Pair)) {
3970 LLVM_DEBUG(dbgs() << " delinearized\n")do { } while (false);
3971 Pairs = Pair.size();
3972 }
3973 }
3974
3975 for (unsigned P = 0; P < Pairs; ++P) {
3976 Pair[P].Loops.resize(MaxLevels + 1);
3977 Pair[P].GroupLoops.resize(MaxLevels + 1);
3978 Pair[P].Group.resize(Pairs);
3979 removeMatchingExtensions(&Pair[P]);
3980 Pair[P].Classification =
3981 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()),
3982 Pair[P].Dst, LI->getLoopFor(Dst->getParent()),
3983 Pair[P].Loops);
3984 Pair[P].GroupLoops = Pair[P].Loops;
3985 Pair[P].Group.set(P);
3986 }
3987
3988 SmallBitVector Separable(Pairs);
3989 SmallBitVector Coupled(Pairs);
3990
3991 // partition subscripts into separable and minimally-coupled groups
3992 for (unsigned SI = 0; SI < Pairs; ++SI) {
3993 if (Pair[SI].Classification == Subscript::NonLinear) {
3994 // ignore these, but collect loops for later
3995 collectCommonLoops(Pair[SI].Src,
3996 LI->getLoopFor(Src->getParent()),
3997 Pair[SI].Loops);
3998 collectCommonLoops(Pair[SI].Dst,
3999 LI->getLoopFor(Dst->getParent()),
4000 Pair[SI].Loops);
4001 Result.Consistent = false;
4002 }
4003 else if (Pair[SI].Classification == Subscript::ZIV)
4004 Separable.set(SI);
4005 else {
4006 // SIV, RDIV, or MIV, so check for coupled group
4007 bool Done = true;
4008 for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) {
4009 SmallBitVector Intersection = Pair[SI].GroupLoops;
4010 Intersection &= Pair[SJ].GroupLoops;
4011 if (Intersection.any()) {
4012 // accumulate set of all the loops in group
4013 Pair[SJ].GroupLoops |= Pair[SI].GroupLoops;
4014 // accumulate set of all subscripts in group
4015 Pair[SJ].Group |= Pair[SI].Group;
4016 Done = false;
4017 }
4018 }
4019 if (Done) {
4020 if (Pair[SI].Group.count() == 1)
4021 Separable.set(SI);
4022 else
4023 Coupled.set(SI);
4024 }
4025 }
4026 }
4027
4028 Constraint NewConstraint;
4029 NewConstraint.setAny(SE);
4030
4031 // test separable subscripts
4032 for (unsigned SI : Separable.set_bits()) {
4033 switch (Pair[SI].Classification) {
4034 case Subscript::SIV: {
4035 unsigned Level;
4036 const SCEV *SplitIter = nullptr;
4037 (void) testSIV(Pair[SI].Src, Pair[SI].Dst, Level,
4038 Result, NewConstraint, SplitIter);
4039 if (Level == SplitLevel) {
4040 assert(SplitIter != nullptr)((void)0);
4041 return SplitIter;
4042 }
4043 break;
4044 }
4045 case Subscript::ZIV:
4046 case Subscript::RDIV:
4047 case Subscript::MIV:
4048 break;
4049 default:
4050 llvm_unreachable("subscript has unexpected classification")__builtin_unreachable();
4051 }
4052 }
4053
4054 if (Coupled.count()) {
4055 // test coupled subscript groups
4056 SmallVector<Constraint, 4> Constraints(MaxLevels + 1);
4057 for (unsigned II = 0; II <= MaxLevels; ++II)
4058 Constraints[II].setAny(SE);
4059 for (unsigned SI : Coupled.set_bits()) {
4060 SmallBitVector Group(Pair[SI].Group);
4061 SmallBitVector Sivs(Pairs);
4062 SmallBitVector Mivs(Pairs);
4063 SmallBitVector ConstrainedLevels(MaxLevels + 1);
4064 for (unsigned SJ : Group.set_bits()) {
4065 if (Pair[SJ].Classification == Subscript::SIV)
4066 Sivs.set(SJ);
4067 else
4068 Mivs.set(SJ);
4069 }
4070 while (Sivs.any()) {
4071 bool Changed = false;
4072 for (unsigned SJ : Sivs.set_bits()) {
4073 // SJ is an SIV subscript that's part of the current coupled group
4074 unsigned Level;
4075 const SCEV *SplitIter = nullptr;
4076 (void) testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level,
4077 Result, NewConstraint, SplitIter);
4078 if (Level == SplitLevel && SplitIter)
4079 return SplitIter;
4080 ConstrainedLevels.set(Level);
4081 if (intersectConstraints(&Constraints[Level], &NewConstraint))
4082 Changed = true;
4083 Sivs.reset(SJ);
4084 }
4085 if (Changed) {
4086 // propagate, possibly creating new SIVs and ZIVs
4087 for (unsigned SJ : Mivs.set_bits()) {
4088 // SJ is an MIV subscript that's part of the current coupled group
4089 if (propagate(Pair[SJ].Src, Pair[SJ].Dst,
4090 Pair[SJ].Loops, Constraints, Result.Consistent)) {
4091 Pair[SJ].Classification =
4092 classifyPair(Pair[SJ].Src, LI->getLoopFor(Src->getParent()),
4093 Pair[SJ].Dst, LI->getLoopFor(Dst->getParent()),
4094 Pair[SJ].Loops);
4095 switch (Pair[SJ].Classification) {
4096 case Subscript::ZIV:
4097 Mivs.reset(SJ);
4098 break;
4099 case Subscript::SIV:
4100 Sivs.set(SJ);
4101 Mivs.reset(SJ);
4102 break;
4103 case Subscript::RDIV:
4104 case Subscript::MIV:
4105 break;
4106 default:
4107 llvm_unreachable("bad subscript classification")__builtin_unreachable();
4108 }
4109 }
4110 }
4111 }
4112 }
4113 }
4114 }
4115 llvm_unreachable("somehow reached end of routine")__builtin_unreachable();
4116 return nullptr;
4117}

/usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IR/Instructions.h

1//===- llvm/Instructions.h - Instruction subclass definitions ---*- 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 exposes the class definitions of all of the subclasses of the
10// Instruction class. This is meant to be an easy way to get access to all
11// instruction subclasses.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_IR_INSTRUCTIONS_H
16#define LLVM_IR_INSTRUCTIONS_H
17
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/Bitfields.h"
20#include "llvm/ADT/MapVector.h"
21#include "llvm/ADT/None.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/ADT/Twine.h"
26#include "llvm/ADT/iterator.h"
27#include "llvm/ADT/iterator_range.h"
28#include "llvm/IR/Attributes.h"
29#include "llvm/IR/BasicBlock.h"
30#include "llvm/IR/CallingConv.h"
31#include "llvm/IR/CFG.h"
32#include "llvm/IR/Constant.h"
33#include "llvm/IR/DerivedTypes.h"
34#include "llvm/IR/Function.h"
35#include "llvm/IR/InstrTypes.h"
36#include "llvm/IR/Instruction.h"
37#include "llvm/IR/OperandTraits.h"
38#include "llvm/IR/Type.h"
39#include "llvm/IR/Use.h"
40#include "llvm/IR/User.h"
41#include "llvm/IR/Value.h"
42#include "llvm/Support/AtomicOrdering.h"
43#include "llvm/Support/Casting.h"
44#include "llvm/Support/ErrorHandling.h"
45#include <cassert>
46#include <cstddef>
47#include <cstdint>
48#include <iterator>
49
50namespace llvm {
51
52class APInt;
53class ConstantInt;
54class DataLayout;
55class LLVMContext;
56
57//===----------------------------------------------------------------------===//
58// AllocaInst Class
59//===----------------------------------------------------------------------===//
60
61/// an instruction to allocate memory on the stack
62class AllocaInst : public UnaryInstruction {
63 Type *AllocatedType;
64
65 using AlignmentField = AlignmentBitfieldElementT<0>;
66 using UsedWithInAllocaField = BoolBitfieldElementT<AlignmentField::NextBit>;
67 using SwiftErrorField = BoolBitfieldElementT<UsedWithInAllocaField::NextBit>;
68 static_assert(Bitfield::areContiguous<AlignmentField, UsedWithInAllocaField,
69 SwiftErrorField>(),
70 "Bitfields must be contiguous");
71
72protected:
73 // Note: Instruction needs to be a friend here to call cloneImpl.
74 friend class Instruction;
75
76 AllocaInst *cloneImpl() const;
77
78public:
79 explicit AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
80 const Twine &Name, Instruction *InsertBefore);
81 AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
82 const Twine &Name, BasicBlock *InsertAtEnd);
83
84 AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
85 Instruction *InsertBefore);
86 AllocaInst(Type *Ty, unsigned AddrSpace,
87 const Twine &Name, BasicBlock *InsertAtEnd);
88
89 AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, Align Align,
90 const Twine &Name = "", Instruction *InsertBefore = nullptr);
91 AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, Align Align,
92 const Twine &Name, BasicBlock *InsertAtEnd);
93
94 /// Return true if there is an allocation size parameter to the allocation
95 /// instruction that is not 1.
96 bool isArrayAllocation() const;
97
98 /// Get the number of elements allocated. For a simple allocation of a single
99 /// element, this will return a constant 1 value.
100 const Value *getArraySize() const { return getOperand(0); }
101 Value *getArraySize() { return getOperand(0); }
102
103 /// Overload to return most specific pointer type.
104 PointerType *getType() const {
105 return cast<PointerType>(Instruction::getType());
106 }
107
108 /// Get allocation size in bits. Returns None if size can't be determined,
109 /// e.g. in case of a VLA.
110 Optional<TypeSize> getAllocationSizeInBits(const DataLayout &DL) const;
111
112 /// Return the type that is being allocated by the instruction.
113 Type *getAllocatedType() const { return AllocatedType; }
114 /// for use only in special circumstances that need to generically
115 /// transform a whole instruction (eg: IR linking and vectorization).
116 void setAllocatedType(Type *Ty) { AllocatedType = Ty; }
117
118 /// Return the alignment of the memory that is being allocated by the
119 /// instruction.
120 Align getAlign() const {
121 return Align(1ULL << getSubclassData<AlignmentField>());
122 }
123
124 void setAlignment(Align Align) {
125 setSubclassData<AlignmentField>(Log2(Align));
126 }
127
128 // FIXME: Remove this one transition to Align is over.
129 unsigned getAlignment() const { return getAlign().value(); }
130
131 /// Return true if this alloca is in the entry block of the function and is a
132 /// constant size. If so, the code generator will fold it into the
133 /// prolog/epilog code, so it is basically free.
134 bool isStaticAlloca() const;
135
136 /// Return true if this alloca is used as an inalloca argument to a call. Such
137 /// allocas are never considered static even if they are in the entry block.
138 bool isUsedWithInAlloca() const {
139 return getSubclassData<UsedWithInAllocaField>();
140 }
141
142 /// Specify whether this alloca is used to represent the arguments to a call.
143 void setUsedWithInAlloca(bool V) {
144 setSubclassData<UsedWithInAllocaField>(V);
145 }
146
147 /// Return true if this alloca is used as a swifterror argument to a call.
148 bool isSwiftError() const { return getSubclassData<SwiftErrorField>(); }
149 /// Specify whether this alloca is used to represent a swifterror.
150 void setSwiftError(bool V) { setSubclassData<SwiftErrorField>(V); }
151
152 // Methods for support type inquiry through isa, cast, and dyn_cast:
153 static bool classof(const Instruction *I) {
154 return (I->getOpcode() == Instruction::Alloca);
155 }
156 static bool classof(const Value *V) {
157 return isa<Instruction>(V) && classof(cast<Instruction>(V));
158 }
159
160private:
161 // Shadow Instruction::setInstructionSubclassData with a private forwarding
162 // method so that subclasses cannot accidentally use it.
163 template <typename Bitfield>
164 void setSubclassData(typename Bitfield::Type Value) {
165 Instruction::setSubclassData<Bitfield>(Value);
166 }
167};
168
169//===----------------------------------------------------------------------===//
170// LoadInst Class
171//===----------------------------------------------------------------------===//
172
173/// An instruction for reading from memory. This uses the SubclassData field in
174/// Value to store whether or not the load is volatile.
175class LoadInst : public UnaryInstruction {
176 using VolatileField = BoolBitfieldElementT<0>;
177 using AlignmentField = AlignmentBitfieldElementT<VolatileField::NextBit>;
178 using OrderingField = AtomicOrderingBitfieldElementT<AlignmentField::NextBit>;
179 static_assert(
180 Bitfield::areContiguous<VolatileField, AlignmentField, OrderingField>(),
181 "Bitfields must be contiguous");
182
183 void AssertOK();
184
185protected:
186 // Note: Instruction needs to be a friend here to call cloneImpl.
187 friend class Instruction;
188
189 LoadInst *cloneImpl() const;
190
191public:
192 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr,
193 Instruction *InsertBefore);
194 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
195 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
196 Instruction *InsertBefore);
197 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
198 BasicBlock *InsertAtEnd);
199 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
200 Align Align, Instruction *InsertBefore = nullptr);
201 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
202 Align Align, BasicBlock *InsertAtEnd);
203 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
204 Align Align, AtomicOrdering Order,
205 SyncScope::ID SSID = SyncScope::System,
206 Instruction *InsertBefore = nullptr);
207 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
208 Align Align, AtomicOrdering Order, SyncScope::ID SSID,
209 BasicBlock *InsertAtEnd);
210
211 /// Return true if this is a load from a volatile memory location.
212 bool isVolatile() const { return getSubclassData<VolatileField>(); }
213
214 /// Specify whether this is a volatile load or not.
215 void setVolatile(bool V) { setSubclassData<VolatileField>(V); }
216
217 /// Return the alignment of the access that is being performed.
218 /// FIXME: Remove this function once transition to Align is over.
219 /// Use getAlign() instead.
220 unsigned getAlignment() const { return getAlign().value(); }
221
222 /// Return the alignment of the access that is being performed.
223 Align getAlign() const {
224 return Align(1ULL << (getSubclassData<AlignmentField>()));
225 }
226
227 void setAlignment(Align Align) {
228 setSubclassData<AlignmentField>(Log2(Align));
229 }
230
231 /// Returns the ordering constraint of this load instruction.
232 AtomicOrdering getOrdering() const {
233 return getSubclassData<OrderingField>();
234 }
235 /// Sets the ordering constraint of this load instruction. May not be Release
236 /// or AcquireRelease.
237 void setOrdering(AtomicOrdering Ordering) {
238 setSubclassData<OrderingField>(Ordering);
239 }
240
241 /// Returns the synchronization scope ID of this load instruction.
242 SyncScope::ID getSyncScopeID() const {
243 return SSID;
244 }
245
246 /// Sets the synchronization scope ID of this load instruction.
247 void setSyncScopeID(SyncScope::ID SSID) {
248 this->SSID = SSID;
249 }
250
251 /// Sets the ordering constraint and the synchronization scope ID of this load
252 /// instruction.
253 void setAtomic(AtomicOrdering Ordering,
254 SyncScope::ID SSID = SyncScope::System) {
255 setOrdering(Ordering);
256 setSyncScopeID(SSID);
257 }
258
259 bool isSimple() const { return !isAtomic() && !isVolatile(); }
260
261 bool isUnordered() const {
262 return (getOrdering() == AtomicOrdering::NotAtomic ||
10
Assuming the condition is false
13
Returning the value 1, which participates in a condition later
21
Assuming the condition is false
24
Returning the value 1, which participates in a condition later
263 getOrdering() == AtomicOrdering::Unordered) &&
11
Assuming the condition is true
22
Assuming the condition is true
264 !isVolatile();
12
Assuming the condition is true
23
Assuming the condition is true
265 }
266
267 Value *getPointerOperand() { return getOperand(0); }
268 const Value *getPointerOperand() const { return getOperand(0); }
269 static unsigned getPointerOperandIndex() { return 0U; }
270 Type *getPointerOperandType() const { return getPointerOperand()->getType(); }
271
272 /// Returns the address space of the pointer operand.
273 unsigned getPointerAddressSpace() const {
274 return getPointerOperandType()->getPointerAddressSpace();
275 }
276
277 // Methods for support type inquiry through isa, cast, and dyn_cast:
278 static bool classof(const Instruction *I) {
279 return I->getOpcode() == Instruction::Load;
280 }
281 static bool classof(const Value *V) {
282 return isa<Instruction>(V) && classof(cast<Instruction>(V));
283 }
284
285private:
286 // Shadow Instruction::setInstructionSubclassData with a private forwarding
287 // method so that subclasses cannot accidentally use it.
288 template <typename Bitfield>
289 void setSubclassData(typename Bitfield::Type Value) {
290 Instruction::setSubclassData<Bitfield>(Value);
291 }
292
293 /// The synchronization scope ID of this load instruction. Not quite enough
294 /// room in SubClassData for everything, so synchronization scope ID gets its
295 /// own field.
296 SyncScope::ID SSID;
297};
298
299//===----------------------------------------------------------------------===//
300// StoreInst Class
301//===----------------------------------------------------------------------===//
302
303/// An instruction for storing to memory.
304class StoreInst : public Instruction {
305 using VolatileField = BoolBitfieldElementT<0>;
306 using AlignmentField = AlignmentBitfieldElementT<VolatileField::NextBit>;
307 using OrderingField = AtomicOrderingBitfieldElementT<AlignmentField::NextBit>;
308 static_assert(
309 Bitfield::areContiguous<VolatileField, AlignmentField, OrderingField>(),
310 "Bitfields must be contiguous");
311
312 void AssertOK();
313
314protected:
315 // Note: Instruction needs to be a friend here to call cloneImpl.
316 friend class Instruction;
317
318 StoreInst *cloneImpl() const;
319
320public:
321 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
322 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
323 StoreInst(Value *Val, Value *Ptr, bool isVolatile, Instruction *InsertBefore);
324 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
325 StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align,
326 Instruction *InsertBefore = nullptr);
327 StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align,
328 BasicBlock *InsertAtEnd);
329 StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align,
330 AtomicOrdering Order, SyncScope::ID SSID = SyncScope::System,
331 Instruction *InsertBefore = nullptr);
332 StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align,
333 AtomicOrdering Order, SyncScope::ID SSID, BasicBlock *InsertAtEnd);
334
335 // allocate space for exactly two operands
336 void *operator new(size_t S) { return User::operator new(S, 2); }
337 void operator delete(void *Ptr) { User::operator delete(Ptr); }
338
339 /// Return true if this is a store to a volatile memory location.
340 bool isVolatile() const { return getSubclassData<VolatileField>(); }
341
342 /// Specify whether this is a volatile store or not.
343 void setVolatile(bool V) { setSubclassData<VolatileField>(V); }
344
345 /// Transparently provide more efficient getOperand methods.
346 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
;
347
348 /// Return the alignment of the access that is being performed
349 /// FIXME: Remove this function once transition to Align is over.
350 /// Use getAlign() instead.
351 unsigned getAlignment() const { return getAlign().value(); }
352
353 Align getAlign() const {
354 return Align(1ULL << (getSubclassData<AlignmentField>()));
355 }
356
357 void setAlignment(Align Align) {
358 setSubclassData<AlignmentField>(Log2(Align));
359 }
360
361 /// Returns the ordering constraint of this store instruction.
362 AtomicOrdering getOrdering() const {
363 return getSubclassData<OrderingField>();
364 }
365
366 /// Sets the ordering constraint of this store instruction. May not be
367 /// Acquire or AcquireRelease.
368 void setOrdering(AtomicOrdering Ordering) {
369 setSubclassData<OrderingField>(Ordering);
370 }
371
372 /// Returns the synchronization scope ID of this store instruction.
373 SyncScope::ID getSyncScopeID() const {
374 return SSID;
375 }
376
377 /// Sets the synchronization scope ID of this store instruction.
378 void setSyncScopeID(SyncScope::ID SSID) {
379 this->SSID = SSID;
380 }
381
382 /// Sets the ordering constraint and the synchronization scope ID of this
383 /// store instruction.
384 void setAtomic(AtomicOrdering Ordering,
385 SyncScope::ID SSID = SyncScope::System) {
386 setOrdering(Ordering);
387 setSyncScopeID(SSID);
388 }
389
390 bool isSimple() const { return !isAtomic() && !isVolatile(); }
391
392 bool isUnordered() const {
393 return (getOrdering() == AtomicOrdering::NotAtomic ||
394 getOrdering() == AtomicOrdering::Unordered) &&
395 !isVolatile();
396 }
397
398 Value *getValueOperand() { return getOperand(0); }
399 const Value *getValueOperand() const { return getOperand(0); }
400
401 Value *getPointerOperand() { return getOperand(1); }
402 const Value *getPointerOperand() const { return getOperand(1); }
403 static unsigned getPointerOperandIndex() { return 1U; }
404 Type *getPointerOperandType() const { return getPointerOperand()->getType(); }
405
406 /// Returns the address space of the pointer operand.
407 unsigned getPointerAddressSpace() const {
408 return getPointerOperandType()->getPointerAddressSpace();
409 }
410
411 // Methods for support type inquiry through isa, cast, and dyn_cast:
412 static bool classof(const Instruction *I) {
413 return I->getOpcode() == Instruction::Store;
414 }
415 static bool classof(const Value *V) {
416 return isa<Instruction>(V) && classof(cast<Instruction>(V));
417 }
418
419private:
420 // Shadow Instruction::setInstructionSubclassData with a private forwarding
421 // method so that subclasses cannot accidentally use it.
422 template <typename Bitfield>
423 void setSubclassData(typename Bitfield::Type Value) {
424 Instruction::setSubclassData<Bitfield>(Value);
425 }
426
427 /// The synchronization scope ID of this store instruction. Not quite enough
428 /// room in SubClassData for everything, so synchronization scope ID gets its
429 /// own field.
430 SyncScope::ID SSID;
431};
432
433template <>
434struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
435};
436
437DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)StoreInst::op_iterator StoreInst::op_begin() { return OperandTraits
<StoreInst>::op_begin(this); } StoreInst::const_op_iterator
StoreInst::op_begin() const { return OperandTraits<StoreInst
>::op_begin(const_cast<StoreInst*>(this)); } StoreInst
::op_iterator StoreInst::op_end() { return OperandTraits<StoreInst
>::op_end(this); } StoreInst::const_op_iterator StoreInst::
op_end() const { return OperandTraits<StoreInst>::op_end
(const_cast<StoreInst*>(this)); } Value *StoreInst::getOperand
(unsigned i_nocapture) const { ((void)0); return cast_or_null
<Value>( OperandTraits<StoreInst>::op_begin(const_cast
<StoreInst*>(this))[i_nocapture].get()); } void StoreInst
::setOperand(unsigned i_nocapture, Value *Val_nocapture) { ((
void)0); OperandTraits<StoreInst>::op_begin(this)[i_nocapture
] = Val_nocapture; } unsigned StoreInst::getNumOperands() const
{ return OperandTraits<StoreInst>::operands(this); } template
<int Idx_nocapture> Use &StoreInst::Op() { return this
->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture
> const Use &StoreInst::Op() const { return this->OpFrom
<Idx_nocapture>(this); }
438
439//===----------------------------------------------------------------------===//
440// FenceInst Class
441//===----------------------------------------------------------------------===//
442
443/// An instruction for ordering other memory operations.
444class FenceInst : public Instruction {
445 using OrderingField = AtomicOrderingBitfieldElementT<0>;
446
447 void Init(AtomicOrdering Ordering, SyncScope::ID SSID);
448
449protected:
450 // Note: Instruction needs to be a friend here to call cloneImpl.
451 friend class Instruction;
452
453 FenceInst *cloneImpl() const;
454
455public:
456 // Ordering may only be Acquire, Release, AcquireRelease, or
457 // SequentiallyConsistent.
458 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
459 SyncScope::ID SSID = SyncScope::System,
460 Instruction *InsertBefore = nullptr);
461 FenceInst(LLVMContext &C, AtomicOrdering Ordering, SyncScope::ID SSID,
462 BasicBlock *InsertAtEnd);
463
464 // allocate space for exactly zero operands
465 void *operator new(size_t S) { return User::operator new(S, 0); }
466 void operator delete(void *Ptr) { User::operator delete(Ptr); }
467
468 /// Returns the ordering constraint of this fence instruction.
469 AtomicOrdering getOrdering() const {
470 return getSubclassData<OrderingField>();
471 }
472
473 /// Sets the ordering constraint of this fence instruction. May only be
474 /// Acquire, Release, AcquireRelease, or SequentiallyConsistent.
475 void setOrdering(AtomicOrdering Ordering) {
476 setSubclassData<OrderingField>(Ordering);
477 }
478
479 /// Returns the synchronization scope ID of this fence instruction.
480 SyncScope::ID getSyncScopeID() const {
481 return SSID;
482 }
483
484 /// Sets the synchronization scope ID of this fence instruction.
485 void setSyncScopeID(SyncScope::ID SSID) {
486 this->SSID = SSID;
487 }
488
489 // Methods for support type inquiry through isa, cast, and dyn_cast:
490 static bool classof(const Instruction *I) {
491 return I->getOpcode() == Instruction::Fence;
492 }
493 static bool classof(const Value *V) {
494 return isa<Instruction>(V) && classof(cast<Instruction>(V));
495 }
496
497private:
498 // Shadow Instruction::setInstructionSubclassData with a private forwarding
499 // method so that subclasses cannot accidentally use it.
500 template <typename Bitfield>
501 void setSubclassData(typename Bitfield::Type Value) {
502 Instruction::setSubclassData<Bitfield>(Value);
503 }
504
505 /// The synchronization scope ID of this fence instruction. Not quite enough
506 /// room in SubClassData for everything, so synchronization scope ID gets its
507 /// own field.
508 SyncScope::ID SSID;
509};
510
511//===----------------------------------------------------------------------===//
512// AtomicCmpXchgInst Class
513//===----------------------------------------------------------------------===//
514
515/// An instruction that atomically checks whether a
516/// specified value is in a memory location, and, if it is, stores a new value
517/// there. The value returned by this instruction is a pair containing the
518/// original value as first element, and an i1 indicating success (true) or
519/// failure (false) as second element.
520///
521class AtomicCmpXchgInst : public Instruction {
522 void Init(Value *Ptr, Value *Cmp, Value *NewVal, Align Align,
523 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
524 SyncScope::ID SSID);
525
526 template <unsigned Offset>
527 using AtomicOrderingBitfieldElement =
528 typename Bitfield::Element<AtomicOrdering, Offset, 3,
529 AtomicOrdering::LAST>;
530
531protected:
532 // Note: Instruction needs to be a friend here to call cloneImpl.
533 friend class Instruction;
534
535 AtomicCmpXchgInst *cloneImpl() const;
536
537public:
538 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, Align Alignment,
539 AtomicOrdering SuccessOrdering,
540 AtomicOrdering FailureOrdering, SyncScope::ID SSID,
541 Instruction *InsertBefore = nullptr);
542 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, Align Alignment,
543 AtomicOrdering SuccessOrdering,
544 AtomicOrdering FailureOrdering, SyncScope::ID SSID,
545 BasicBlock *InsertAtEnd);
546
547 // allocate space for exactly three operands
548 void *operator new(size_t S) { return User::operator new(S, 3); }
549 void operator delete(void *Ptr) { User::operator delete(Ptr); }
550
551 using VolatileField = BoolBitfieldElementT<0>;
552 using WeakField = BoolBitfieldElementT<VolatileField::NextBit>;
553 using SuccessOrderingField =
554 AtomicOrderingBitfieldElementT<WeakField::NextBit>;
555 using FailureOrderingField =
556 AtomicOrderingBitfieldElementT<SuccessOrderingField::NextBit>;
557 using AlignmentField =
558 AlignmentBitfieldElementT<FailureOrderingField::NextBit>;
559 static_assert(
560 Bitfield::areContiguous<VolatileField, WeakField, SuccessOrderingField,
561 FailureOrderingField, AlignmentField>(),
562 "Bitfields must be contiguous");
563
564 /// Return the alignment of the memory that is being allocated by the
565 /// instruction.
566 Align getAlign() const {
567 return Align(1ULL << getSubclassData<AlignmentField>());
568 }
569
570 void setAlignment(Align Align) {
571 setSubclassData<AlignmentField>(Log2(Align));
572 }
573
574 /// Return true if this is a cmpxchg from a volatile memory
575 /// location.
576 ///
577 bool isVolatile() const { return getSubclassData<VolatileField>(); }
578
579 /// Specify whether this is a volatile cmpxchg.
580 ///
581 void setVolatile(bool V) { setSubclassData<VolatileField>(V); }
582
583 /// Return true if this cmpxchg may spuriously fail.
584 bool isWeak() const { return getSubclassData<WeakField>(); }
585
586 void setWeak(bool IsWeak) { setSubclassData<WeakField>(IsWeak); }
587
588 /// Transparently provide more efficient getOperand methods.
589 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
;
590
591 static bool isValidSuccessOrdering(AtomicOrdering Ordering) {
592 return Ordering != AtomicOrdering::NotAtomic &&
593 Ordering != AtomicOrdering::Unordered;
594 }
595
596 static bool isValidFailureOrdering(AtomicOrdering Ordering) {
597 return Ordering != AtomicOrdering::NotAtomic &&
598 Ordering != AtomicOrdering::Unordered &&
599 Ordering != AtomicOrdering::AcquireRelease &&
600 Ordering != AtomicOrdering::Release;
601 }
602
603 /// Returns the success ordering constraint of this cmpxchg instruction.
604 AtomicOrdering getSuccessOrdering() const {
605 return getSubclassData<SuccessOrderingField>();
606 }
607
608 /// Sets the success ordering constraint of this cmpxchg instruction.
609 void setSuccessOrdering(AtomicOrdering Ordering) {
610 assert(isValidSuccessOrdering(Ordering) &&((void)0)
611 "invalid CmpXchg success ordering")((void)0);
612 setSubclassData<SuccessOrderingField>(Ordering);
613 }
614
615 /// Returns the failure ordering constraint of this cmpxchg instruction.
616 AtomicOrdering getFailureOrdering() const {
617 return getSubclassData<FailureOrderingField>();
618 }
619
620 /// Sets the failure ordering constraint of this cmpxchg instruction.
621 void setFailureOrdering(AtomicOrdering Ordering) {
622 assert(isValidFailureOrdering(Ordering) &&((void)0)
623 "invalid CmpXchg failure ordering")((void)0);
624 setSubclassData<FailureOrderingField>(Ordering);
625 }
626
627 /// Returns a single ordering which is at least as strong as both the
628 /// success and failure orderings for this cmpxchg.
629 AtomicOrdering getMergedOrdering() const {
630 if (getFailureOrdering() == AtomicOrdering::SequentiallyConsistent)
631 return AtomicOrdering::SequentiallyConsistent;
632 if (getFailureOrdering() == AtomicOrdering::Acquire) {
633 if (getSuccessOrdering() == AtomicOrdering::Monotonic)
634 return AtomicOrdering::Acquire;
635 if (getSuccessOrdering() == AtomicOrdering::Release)
636 return AtomicOrdering::AcquireRelease;
637 }
638 return getSuccessOrdering();
639 }
640
641 /// Returns the synchronization scope ID of this cmpxchg instruction.
642 SyncScope::ID getSyncScopeID() const {
643 return SSID;
644 }
645
646 /// Sets the synchronization scope ID of this cmpxchg instruction.
647 void setSyncScopeID(SyncScope::ID SSID) {
648 this->SSID = SSID;
649 }
650
651 Value *getPointerOperand() { return getOperand(0); }
652 const Value *getPointerOperand() const { return getOperand(0); }
653 static unsigned getPointerOperandIndex() { return 0U; }
654
655 Value *getCompareOperand() { return getOperand(1); }
656 const Value *getCompareOperand() const { return getOperand(1); }
657
658 Value *getNewValOperand() { return getOperand(2); }
659 const Value *getNewValOperand() const { return getOperand(2); }
660
661 /// Returns the address space of the pointer operand.
662 unsigned getPointerAddressSpace() const {
663 return getPointerOperand()->getType()->getPointerAddressSpace();
664 }
665
666 /// Returns the strongest permitted ordering on failure, given the
667 /// desired ordering on success.
668 ///
669 /// If the comparison in a cmpxchg operation fails, there is no atomic store
670 /// so release semantics cannot be provided. So this function drops explicit
671 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
672 /// operation would remain SequentiallyConsistent.
673 static AtomicOrdering
674 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
675 switch (SuccessOrdering) {
676 default:
677 llvm_unreachable("invalid cmpxchg success ordering")__builtin_unreachable();
678 case AtomicOrdering::Release:
679 case AtomicOrdering::Monotonic:
680 return AtomicOrdering::Monotonic;
681 case AtomicOrdering::AcquireRelease:
682 case AtomicOrdering::Acquire:
683 return AtomicOrdering::Acquire;
684 case AtomicOrdering::SequentiallyConsistent:
685 return AtomicOrdering::SequentiallyConsistent;
686 }
687 }
688
689 // Methods for support type inquiry through isa, cast, and dyn_cast:
690 static bool classof(const Instruction *I) {
691 return I->getOpcode() == Instruction::AtomicCmpXchg;
692 }
693 static bool classof(const Value *V) {
694 return isa<Instruction>(V) && classof(cast<Instruction>(V));
695 }
696
697private:
698 // Shadow Instruction::setInstructionSubclassData with a private forwarding
699 // method so that subclasses cannot accidentally use it.
700 template <typename Bitfield>
701 void setSubclassData(typename Bitfield::Type Value) {
702 Instruction::setSubclassData<Bitfield>(Value);
703 }
704
705 /// The synchronization scope ID of this cmpxchg instruction. Not quite
706 /// enough room in SubClassData for everything, so synchronization scope ID
707 /// gets its own field.
708 SyncScope::ID SSID;
709};
710
711template <>
712struct OperandTraits<AtomicCmpXchgInst> :
713 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
714};
715
716DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)AtomicCmpXchgInst::op_iterator AtomicCmpXchgInst::op_begin() {
return OperandTraits<AtomicCmpXchgInst>::op_begin(this
); } AtomicCmpXchgInst::const_op_iterator AtomicCmpXchgInst::
op_begin() const { return OperandTraits<AtomicCmpXchgInst>
::op_begin(const_cast<AtomicCmpXchgInst*>(this)); } AtomicCmpXchgInst
::op_iterator AtomicCmpXchgInst::op_end() { return OperandTraits
<AtomicCmpXchgInst>::op_end(this); } AtomicCmpXchgInst::
const_op_iterator AtomicCmpXchgInst::op_end() const { return OperandTraits
<AtomicCmpXchgInst>::op_end(const_cast<AtomicCmpXchgInst
*>(this)); } Value *AtomicCmpXchgInst::getOperand(unsigned
i_nocapture) const { ((void)0); return cast_or_null<Value
>( OperandTraits<AtomicCmpXchgInst>::op_begin(const_cast
<AtomicCmpXchgInst*>(this))[i_nocapture].get()); } void
AtomicCmpXchgInst::setOperand(unsigned i_nocapture, Value *Val_nocapture
) { ((void)0); OperandTraits<AtomicCmpXchgInst>::op_begin
(this)[i_nocapture] = Val_nocapture; } unsigned AtomicCmpXchgInst
::getNumOperands() const { return OperandTraits<AtomicCmpXchgInst
>::operands(this); } template <int Idx_nocapture> Use
&AtomicCmpXchgInst::Op() { return this->OpFrom<Idx_nocapture
>(this); } template <int Idx_nocapture> const Use &
AtomicCmpXchgInst::Op() const { return this->OpFrom<Idx_nocapture
>(this); }
717
718//===----------------------------------------------------------------------===//
719// AtomicRMWInst Class
720//===----------------------------------------------------------------------===//
721
722/// an instruction that atomically reads a memory location,
723/// combines it with another value, and then stores the result back. Returns
724/// the old value.
725///
726class AtomicRMWInst : public Instruction {
727protected:
728 // Note: Instruction needs to be a friend here to call cloneImpl.
729 friend class Instruction;
730
731 AtomicRMWInst *cloneImpl() const;
732
733public:
734 /// This enumeration lists the possible modifications atomicrmw can make. In
735 /// the descriptions, 'p' is the pointer to the instruction's memory location,
736 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
737 /// instruction. These instructions always return 'old'.
738 enum BinOp : unsigned {
739 /// *p = v
740 Xchg,
741 /// *p = old + v
742 Add,
743 /// *p = old - v
744 Sub,
745 /// *p = old & v
746 And,
747 /// *p = ~(old & v)
748 Nand,
749 /// *p = old | v
750 Or,
751 /// *p = old ^ v
752 Xor,
753 /// *p = old >signed v ? old : v
754 Max,
755 /// *p = old <signed v ? old : v
756 Min,
757 /// *p = old >unsigned v ? old : v
758 UMax,
759 /// *p = old <unsigned v ? old : v
760 UMin,
761
762 /// *p = old + v
763 FAdd,
764
765 /// *p = old - v
766 FSub,
767
768 FIRST_BINOP = Xchg,
769 LAST_BINOP = FSub,
770 BAD_BINOP
771 };
772
773private:
774 template <unsigned Offset>
775 using AtomicOrderingBitfieldElement =
776 typename Bitfield::Element<AtomicOrdering, Offset, 3,
777 AtomicOrdering::LAST>;
778
779 template <unsigned Offset>
780 using BinOpBitfieldElement =
781 typename Bitfield::Element<BinOp, Offset, 4, BinOp::LAST_BINOP>;
782
783public:
784 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, Align Alignment,
785 AtomicOrdering Ordering, SyncScope::ID SSID,
786 Instruction *InsertBefore = nullptr);
787 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, Align Alignment,
788 AtomicOrdering Ordering, SyncScope::ID SSID,
789 BasicBlock *InsertAtEnd);
790
791 // allocate space for exactly two operands
792 void *operator new(size_t S) { return User::operator new(S, 2); }
793 void operator delete(void *Ptr) { User::operator delete(Ptr); }
794
795 using VolatileField = BoolBitfieldElementT<0>;
796 using AtomicOrderingField =
797 AtomicOrderingBitfieldElementT<VolatileField::NextBit>;
798 using OperationField = BinOpBitfieldElement<AtomicOrderingField::NextBit>;
799 using AlignmentField = AlignmentBitfieldElementT<OperationField::NextBit>;
800 static_assert(Bitfield::areContiguous<VolatileField, AtomicOrderingField,
801 OperationField, AlignmentField>(),
802 "Bitfields must be contiguous");
803
804 BinOp getOperation() const { return getSubclassData<OperationField>(); }
805
806 static StringRef getOperationName(BinOp Op);
807
808 static bool isFPOperation(BinOp Op) {
809 switch (Op) {
810 case AtomicRMWInst::FAdd:
811 case AtomicRMWInst::FSub:
812 return true;
813 default:
814 return false;
815 }
816 }
817
818 void setOperation(BinOp Operation) {
819 setSubclassData<OperationField>(Operation);
820 }
821
822 /// Return the alignment of the memory that is being allocated by the
823 /// instruction.
824 Align getAlign() const {
825 return Align(1ULL << getSubclassData<AlignmentField>());
826 }
827
828 void setAlignment(Align Align) {
829 setSubclassData<AlignmentField>(Log2(Align));
830 }
831
832 /// Return true if this is a RMW on a volatile memory location.
833 ///
834 bool isVolatile() const { return getSubclassData<VolatileField>(); }
835
836 /// Specify whether this is a volatile RMW or not.
837 ///
838 void setVolatile(bool V) { setSubclassData<VolatileField>(V); }
839
840 /// Transparently provide more efficient getOperand methods.
841 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
;
842
843 /// Returns the ordering constraint of this rmw instruction.
844 AtomicOrdering getOrdering() const {
845 return getSubclassData<AtomicOrderingField>();
846 }
847
848 /// Sets the ordering constraint of this rmw instruction.
849 void setOrdering(AtomicOrdering Ordering) {
850 assert(Ordering != AtomicOrdering::NotAtomic &&((void)0)
851 "atomicrmw instructions can only be atomic.")((void)0);
852 setSubclassData<AtomicOrderingField>(Ordering);
853 }
854
855 /// Returns the synchronization scope ID of this rmw instruction.
856 SyncScope::ID getSyncScopeID() const {
857 return SSID;
858 }
859
860 /// Sets the synchronization scope ID of this rmw instruction.
861 void setSyncScopeID(SyncScope::ID SSID) {
862 this->SSID = SSID;
863 }
864
865 Value *getPointerOperand() { return getOperand(0); }
866 const Value *getPointerOperand() const { return getOperand(0); }
867 static unsigned getPointerOperandIndex() { return 0U; }
868
869 Value *getValOperand() { return getOperand(1); }
870 const Value *getValOperand() const { return getOperand(1); }
871
872 /// Returns the address space of the pointer operand.
873 unsigned getPointerAddressSpace() const {
874 return getPointerOperand()->getType()->getPointerAddressSpace();
875 }
876
877 bool isFloatingPointOperation() const {
878 return isFPOperation(getOperation());
879 }
880
881 // Methods for support type inquiry through isa, cast, and dyn_cast:
882 static bool classof(const Instruction *I) {
883 return I->getOpcode() == Instruction::AtomicRMW;
884 }
885 static bool classof(const Value *V) {
886 return isa<Instruction>(V) && classof(cast<Instruction>(V));
887 }
888
889private:
890 void Init(BinOp Operation, Value *Ptr, Value *Val, Align Align,
891 AtomicOrdering Ordering, SyncScope::ID SSID);
892
893 // Shadow Instruction::setInstructionSubclassData with a private forwarding
894 // method so that subclasses cannot accidentally use it.
895 template <typename Bitfield>
896 void setSubclassData(typename Bitfield::Type Value) {
897 Instruction::setSubclassData<Bitfield>(Value);
898 }
899
900 /// The synchronization scope ID of this rmw instruction. Not quite enough
901 /// room in SubClassData for everything, so synchronization scope ID gets its
902 /// own field.
903 SyncScope::ID SSID;
904};
905
906template <>
907struct OperandTraits<AtomicRMWInst>
908 : public FixedNumOperandTraits<AtomicRMWInst,2> {
909};
910
911DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)AtomicRMWInst::op_iterator AtomicRMWInst::op_begin() { return
OperandTraits<AtomicRMWInst>::op_begin(this); } AtomicRMWInst
::const_op_iterator AtomicRMWInst::op_begin() const { return OperandTraits
<AtomicRMWInst>::op_begin(const_cast<AtomicRMWInst*>
(this)); } AtomicRMWInst::op_iterator AtomicRMWInst::op_end()
{ return OperandTraits<AtomicRMWInst>::op_end(this); }
AtomicRMWInst::const_op_iterator AtomicRMWInst::op_end() const
{ return OperandTraits<AtomicRMWInst>::op_end(const_cast
<AtomicRMWInst*>(this)); } Value *AtomicRMWInst::getOperand
(unsigned i_nocapture) const { ((void)0); return cast_or_null
<Value>( OperandTraits<AtomicRMWInst>::op_begin(const_cast
<AtomicRMWInst*>(this))[i_nocapture].get()); } void AtomicRMWInst
::setOperand(unsigned i_nocapture, Value *Val_nocapture) { ((
void)0); OperandTraits<AtomicRMWInst>::op_begin(this)[i_nocapture
] = Val_nocapture; } unsigned AtomicRMWInst::getNumOperands()
const { return OperandTraits<AtomicRMWInst>::operands(
this); } template <int Idx_nocapture> Use &AtomicRMWInst
::Op() { return this->OpFrom<Idx_nocapture>(this); }
template <int Idx_nocapture> const Use &AtomicRMWInst
::Op() const { return this->OpFrom<Idx_nocapture>(this
); }
912
913//===----------------------------------------------------------------------===//
914// GetElementPtrInst Class
915//===----------------------------------------------------------------------===//
916
917// checkGEPType - Simple wrapper function to give a better assertion failure
918// message on bad indexes for a gep instruction.
919//
920inline Type *checkGEPType(Type *Ty) {
921 assert(Ty && "Invalid GetElementPtrInst indices for type!")((void)0);
922 return Ty;
923}
924
925/// an instruction for type-safe pointer arithmetic to
926/// access elements of arrays and structs
927///
928class GetElementPtrInst : public Instruction {
929 Type *SourceElementType;
930 Type *ResultElementType;
931
932 GetElementPtrInst(const GetElementPtrInst &GEPI);
933
934 /// Constructors - Create a getelementptr instruction with a base pointer an
935 /// list of indices. The first ctor can optionally insert before an existing
936 /// instruction, the second appends the new instruction to the specified
937 /// BasicBlock.
938 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
939 ArrayRef<Value *> IdxList, unsigned Values,
940 const Twine &NameStr, Instruction *InsertBefore);
941 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
942 ArrayRef<Value *> IdxList, unsigned Values,
943 const Twine &NameStr, BasicBlock *InsertAtEnd);
944
945 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
946
947protected:
948 // Note: Instruction needs to be a friend here to call cloneImpl.
949 friend class Instruction;
950
951 GetElementPtrInst *cloneImpl() const;
952
953public:
954 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
955 ArrayRef<Value *> IdxList,
956 const Twine &NameStr = "",
957 Instruction *InsertBefore = nullptr) {
958 unsigned Values = 1 + unsigned(IdxList.size());
959 assert(PointeeType && "Must specify element type")((void)0);
960 assert(cast<PointerType>(Ptr->getType()->getScalarType())((void)0)
961 ->isOpaqueOrPointeeTypeMatches(PointeeType))((void)0);
962 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
963 NameStr, InsertBefore);
964 }
965
966 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
967 ArrayRef<Value *> IdxList,
968 const Twine &NameStr,
969 BasicBlock *InsertAtEnd) {
970 unsigned Values = 1 + unsigned(IdxList.size());
971 assert(PointeeType && "Must specify element type")((void)0);
972 assert(cast<PointerType>(Ptr->getType()->getScalarType())((void)0)
973 ->isOpaqueOrPointeeTypeMatches(PointeeType))((void)0);
974 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
975 NameStr, InsertAtEnd);
976 }
977
978 LLVM_ATTRIBUTE_DEPRECATED(static GetElementPtrInst *CreateInBounds([[deprecated("Use the version with explicit element type instead"
)]] static GetElementPtrInst *CreateInBounds( Value *Ptr, ArrayRef
<Value *> IdxList, const Twine &NameStr = "", Instruction
*InsertBefore = nullptr)
979 Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr = "",[[deprecated("Use the version with explicit element type instead"
)]] static GetElementPtrInst *CreateInBounds( Value *Ptr, ArrayRef
<Value *> IdxList, const Twine &NameStr = "", Instruction
*InsertBefore = nullptr)
980 Instruction *InsertBefore = nullptr),[[deprecated("Use the version with explicit element type instead"
)]] static GetElementPtrInst *CreateInBounds( Value *Ptr, ArrayRef
<Value *> IdxList, const Twine &NameStr = "", Instruction
*InsertBefore = nullptr)
981 "Use the version with explicit element type instead")[[deprecated("Use the version with explicit element type instead"
)]] static GetElementPtrInst *CreateInBounds( Value *Ptr, ArrayRef
<Value *> IdxList, const Twine &NameStr = "", Instruction
*InsertBefore = nullptr)
{
982 return CreateInBounds(
983 Ptr->getType()->getScalarType()->getPointerElementType(), Ptr, IdxList,
984 NameStr, InsertBefore);
985 }
986
987 /// Create an "inbounds" getelementptr. See the documentation for the
988 /// "inbounds" flag in LangRef.html for details.
989 static GetElementPtrInst *
990 CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList,
991 const Twine &NameStr = "",
992 Instruction *InsertBefore = nullptr) {
993 GetElementPtrInst *GEP =
994 Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore);
995 GEP->setIsInBounds(true);
996 return GEP;
997 }
998
999 LLVM_ATTRIBUTE_DEPRECATED(static GetElementPtrInst *CreateInBounds([[deprecated("Use the version with explicit element type instead"
)]] static GetElementPtrInst *CreateInBounds( Value *Ptr, ArrayRef
<Value *> IdxList, const Twine &NameStr, BasicBlock
*InsertAtEnd)
1000 Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr,[[deprecated("Use the version with explicit element type instead"
)]] static GetElementPtrInst *CreateInBounds( Value *Ptr, ArrayRef
<Value *> IdxList, const Twine &NameStr, BasicBlock
*InsertAtEnd)
1001 BasicBlock *InsertAtEnd),[[deprecated("Use the version with explicit element type instead"
)]] static GetElementPtrInst *CreateInBounds( Value *Ptr, ArrayRef
<Value *> IdxList, const Twine &NameStr, BasicBlock
*InsertAtEnd)
1002 "Use the version with explicit element type instead")[[deprecated("Use the version with explicit element type instead"
)]] static GetElementPtrInst *CreateInBounds( Value *Ptr, ArrayRef
<Value *> IdxList, const Twine &NameStr, BasicBlock
*InsertAtEnd)
{
1003 return CreateInBounds(
1004 Ptr->getType()->getScalarType()->getPointerElementType(), Ptr, IdxList,
1005 NameStr, InsertAtEnd);
1006 }
1007
1008 static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr,
1009 ArrayRef<Value *> IdxList,
1010 const Twine &NameStr,
1011 BasicBlock *InsertAtEnd) {
1012 GetElementPtrInst *GEP =
1013 Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd);
1014 GEP->setIsInBounds(true);
1015 return GEP;
1016 }
1017
1018 /// Transparently provide more efficient getOperand methods.
1019 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
;
1020
1021 Type *getSourceElementType() const { return SourceElementType; }
1022
1023 void setSourceElementType(Type *Ty) { SourceElementType = Ty; }
1024 void setResultElementType(Type *Ty) { ResultElementType = Ty; }
1025
1026 Type *getResultElementType() const {
1027 assert(cast<PointerType>(getType()->getScalarType())((void)0)
1028 ->isOpaqueOrPointeeTypeMatches(ResultElementType))((void)0);
1029 return ResultElementType;
1030 }
1031
1032 /// Returns the address space of this instruction's pointer type.
1033 unsigned getAddressSpace() const {
1034 // Note that this is always the same as the pointer operand's address space
1035 // and that is cheaper to compute, so cheat here.
1036 return getPointerAddressSpace();
1037 }
1038
1039 /// Returns the result type of a getelementptr with the given source
1040 /// element type and indexes.
1041 ///
1042 /// Null is returned if the indices are invalid for the specified
1043 /// source element type.
1044 static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList);
1045 static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList);
1046 static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList);
1047
1048 /// Return the type of the element at the given index of an indexable
1049 /// type. This is equivalent to "getIndexedType(Agg, {Zero, Idx})".
1050 ///
1051 /// Returns null if the type can't be indexed, or the given index is not
1052 /// legal for the given type.
1053 static Type *getTypeAtIndex(Type *Ty, Value *Idx);
1054 static Type *getTypeAtIndex(Type *Ty, uint64_t Idx);
1055
1056 inline op_iterator idx_begin() { return op_begin()+1; }
1057 inline const_op_iterator idx_begin() const { return op_begin()+1; }
1058 inline op_iterator idx_end() { return op_end(); }
1059 inline const_op_iterator idx_end() const { return op_end(); }
1060
1061 inline iterator_range<op_iterator> indices() {
1062 return make_range(idx_begin(), idx_end());
1063 }
1064
1065 inline iterator_range<const_op_iterator> indices() const {
1066 return make_range(idx_begin(), idx_end());
1067 }
1068
1069 Value *getPointerOperand() {
1070 return getOperand(0);
1071 }
1072 const Value *getPointerOperand() const {
1073 return getOperand(0);
1074 }
1075 static unsigned getPointerOperandIndex() {
1076 return 0U; // get index for modifying correct operand.
1077 }
1078
1079 /// Method to return the pointer operand as a
1080 /// PointerType.
1081 Type *getPointerOperandType() const {
1082 return getPointerOperand()->getType();
1083 }
1084
1085 /// Returns the address space of the pointer operand.
1086 unsigned getPointerAddressSpace() const {
1087 return getPointerOperandType()->getPointerAddressSpace();
1088 }
1089
1090 /// Returns the pointer type returned by the GEP
1091 /// instruction, which may be a vector of pointers.
1092 static Type *getGEPReturnType(Type *ElTy, Value *Ptr,
1093 ArrayRef<Value *> IdxList) {
1094 PointerType *OrigPtrTy = cast<PointerType>(Ptr->getType()->getScalarType());
1095 unsigned AddrSpace = OrigPtrTy->getAddressSpace();
1096 Type *ResultElemTy = checkGEPType(getIndexedType(ElTy, IdxList));
1097 Type *PtrTy = OrigPtrTy->isOpaque()
1098 ? PointerType::get(OrigPtrTy->getContext(), AddrSpace)
1099 : PointerType::get(ResultElemTy, AddrSpace);
1100 // Vector GEP
1101 if (auto *PtrVTy = dyn_cast<VectorType>(Ptr->getType())) {
1102 ElementCount EltCount = PtrVTy->getElementCount();
1103 return VectorType::get(PtrTy, EltCount);
1104 }
1105 for (Value *Index : IdxList)
1106 if (auto *IndexVTy = dyn_cast<VectorType>(Index->getType())) {
1107 ElementCount EltCount = IndexVTy->getElementCount();
1108 return VectorType::get(PtrTy, EltCount);
1109 }
1110 // Scalar GEP
1111 return PtrTy;
1112 }
1113
1114 unsigned getNumIndices() const { // Note: always non-negative
1115 return getNumOperands() - 1;
1116 }
1117
1118 bool hasIndices() const {
1119 return getNumOperands() > 1;
1120 }
1121
1122 /// Return true if all of the indices of this GEP are
1123 /// zeros. If so, the result pointer and the first operand have the same
1124 /// value, just potentially different types.
1125 bool hasAllZeroIndices() const;
1126
1127 /// Return true if all of the indices of this GEP are
1128 /// constant integers. If so, the result pointer and the first operand have
1129 /// a constant offset between them.
1130 bool hasAllConstantIndices() const;
1131
1132 /// Set or clear the inbounds flag on this GEP instruction.
1133 /// See LangRef.html for the meaning of inbounds on a getelementptr.
1134 void setIsInBounds(bool b = true);
1135
1136 /// Determine whether the GEP has the inbounds flag.
1137 bool isInBounds() const;
1138
1139 /// Accumulate the constant address offset of this GEP if possible.
1140 ///
1141 /// This routine accepts an APInt into which it will accumulate the constant
1142 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
1143 /// all-constant, it returns false and the value of the offset APInt is
1144 /// undefined (it is *not* preserved!). The APInt passed into this routine
1145 /// must be at least as wide as the IntPtr type for the address space of
1146 /// the base GEP pointer.
1147 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
1148 bool collectOffset(const DataLayout &DL, unsigned BitWidth,
1149 MapVector<Value *, APInt> &VariableOffsets,
1150 APInt &ConstantOffset) const;
1151 // Methods for support type inquiry through isa, cast, and dyn_cast:
1152 static bool classof(const Instruction *I) {
1153 return (I->getOpcode() == Instruction::GetElementPtr);
1154 }
1155 static bool classof(const Value *V) {
1156 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1157 }
1158};
1159
1160template <>
1161struct OperandTraits<GetElementPtrInst> :
1162 public VariadicOperandTraits<GetElementPtrInst, 1> {
1163};
1164
1165GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1166 ArrayRef<Value *> IdxList, unsigned Values,
1167 const Twine &NameStr,
1168 Instruction *InsertBefore)
1169 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1170 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1171 Values, InsertBefore),
1172 SourceElementType(PointeeType),
1173 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1174 assert(cast<PointerType>(getType()->getScalarType())((void)0)
1175 ->isOpaqueOrPointeeTypeMatches(ResultElementType))((void)0);
1176 init(Ptr, IdxList, NameStr);
1177}
1178
1179GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1180 ArrayRef<Value *> IdxList, unsigned Values,
1181 const Twine &NameStr,
1182 BasicBlock *InsertAtEnd)
1183 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1184 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1185 Values, InsertAtEnd),
1186 SourceElementType(PointeeType),
1187 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1188 assert(cast<PointerType>(getType()->getScalarType())((void)0)
1189 ->isOpaqueOrPointeeTypeMatches(ResultElementType))((void)0);
1190 init(Ptr, IdxList, NameStr);
1191}
1192
1193DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)GetElementPtrInst::op_iterator GetElementPtrInst::op_begin() {
return OperandTraits<GetElementPtrInst>::op_begin(this
); } GetElementPtrInst::const_op_iterator GetElementPtrInst::
op_begin() const { return OperandTraits<GetElementPtrInst>
::op_begin(const_cast<GetElementPtrInst*>(this)); } GetElementPtrInst
::op_iterator GetElementPtrInst::op_end() { return OperandTraits
<GetElementPtrInst>::op_end(this); } GetElementPtrInst::
const_op_iterator GetElementPtrInst::op_end() const { return OperandTraits
<GetElementPtrInst>::op_end(const_cast<GetElementPtrInst
*>(this)); } Value *GetElementPtrInst::getOperand(unsigned
i_nocapture) const { ((void)0); return cast_or_null<Value
>( OperandTraits<GetElementPtrInst>::op_begin(const_cast
<GetElementPtrInst*>(this))[i_nocapture].get()); } void
GetElementPtrInst::setOperand(unsigned i_nocapture, Value *Val_nocapture
) { ((void)0); OperandTraits<GetElementPtrInst>::op_begin
(this)[i_nocapture] = Val_nocapture; } unsigned GetElementPtrInst
::getNumOperands() const { return OperandTraits<GetElementPtrInst
>::operands(this); } template <int Idx_nocapture> Use
&GetElementPtrInst::Op() { return this->OpFrom<Idx_nocapture
>(this); } template <int Idx_nocapture> const Use &
GetElementPtrInst::Op() const { return this->OpFrom<Idx_nocapture
>(this); }
1194
1195//===----------------------------------------------------------------------===//
1196// ICmpInst Class
1197//===----------------------------------------------------------------------===//
1198
1199/// This instruction compares its operands according to the predicate given
1200/// to the constructor. It only operates on integers or pointers. The operands
1201/// must be identical types.
1202/// Represent an integer comparison operator.
1203class ICmpInst: public CmpInst {
1204 void AssertOK() {
1205 assert(isIntPredicate() &&((void)0)
1206 "Invalid ICmp predicate value")((void)0);
1207 assert(getOperand(0)->getType() == getOperand(1)->getType() &&((void)0)
1208 "Both operands to ICmp instruction are not of the same type!")((void)0);
1209 // Check that the operands are the right type
1210 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||((void)0)
1211 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&((void)0)
1212 "Invalid operand types for ICmp instruction")((void)0);
1213 }
1214
1215protected:
1216 // Note: Instruction needs to be a friend here to call cloneImpl.
1217 friend class Instruction;
1218
1219 /// Clone an identical ICmpInst
1220 ICmpInst *cloneImpl() const;
1221
1222public:
1223 /// Constructor with insert-before-instruction semantics.
1224 ICmpInst(
1225 Instruction *InsertBefore, ///< Where to insert
1226 Predicate pred, ///< The predicate to use for the comparison
1227 Value *LHS, ///< The left-hand-side of the expression
1228 Value *RHS, ///< The right-hand-side of the expression
1229 const Twine &NameStr = "" ///< Name of the instruction
1230 ) : CmpInst(makeCmpResultType(LHS->getType()),
1231 Instruction::ICmp, pred, LHS, RHS, NameStr,
1232 InsertBefore) {
1233#ifndef NDEBUG1
1234 AssertOK();
1235#endif
1236 }
1237
1238 /// Constructor with insert-at-end semantics.
1239 ICmpInst(
1240 BasicBlock &InsertAtEnd, ///< Block to insert into.
1241 Predicate pred, ///< The predicate to use for the comparison
1242 Value *LHS, ///< The left-hand-side of the expression
1243 Value *RHS, ///< The right-hand-side of the expression
1244 const Twine &NameStr = "" ///< Name of the instruction
1245 ) : CmpInst(makeCmpResultType(LHS->getType()),
1246 Instruction::ICmp, pred, LHS, RHS, NameStr,
1247 &InsertAtEnd) {
1248#ifndef NDEBUG1
1249 AssertOK();
1250#endif
1251 }
1252
1253 /// Constructor with no-insertion semantics
1254 ICmpInst(
1255 Predicate pred, ///< The predicate to use for the comparison
1256 Value *LHS, ///< The left-hand-side of the expression
1257 Value *RHS, ///< The right-hand-side of the expression
1258 const Twine &NameStr = "" ///< Name of the instruction
1259 ) : CmpInst(makeCmpResultType(LHS->getType()),
1260 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1261#ifndef NDEBUG1
1262 AssertOK();
1263#endif
1264 }
1265
1266 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1267 /// @returns the predicate that would be the result if the operand were
1268 /// regarded as signed.
1269 /// Return the signed version of the predicate
1270 Predicate getSignedPredicate() const {
1271 return getSignedPredicate(getPredicate());
1272 }
1273
1274 /// This is a static version that you can use without an instruction.
1275 /// Return the signed version of the predicate.
1276 static Predicate getSignedPredicate(Predicate pred);
1277
1278 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1279 /// @returns the predicate that would be the result if the operand were
1280 /// regarded as unsigned.
1281 /// Return the unsigned version of the predicate
1282 Predicate getUnsignedPredicate() const {
1283 return getUnsignedPredicate(getPredicate());
1284 }
1285
1286 /// This is a static version that you can use without an instruction.
1287 /// Return the unsigned version of the predicate.
1288 static Predicate getUnsignedPredicate(Predicate pred);
1289
1290 /// Return true if this predicate is either EQ or NE. This also
1291 /// tests for commutativity.
1292 static bool isEquality(Predicate P) {
1293 return P == ICMP_EQ || P == ICMP_NE;
1294 }
1295
1296 /// Return true if this predicate is either EQ or NE. This also
1297 /// tests for commutativity.
1298 bool isEquality() const {
1299 return isEquality(getPredicate());
1300 }
1301
1302 /// @returns true if the predicate of this ICmpInst is commutative
1303 /// Determine if this relation is commutative.
1304 bool isCommutative() const { return isEquality(); }
1305
1306 /// Return true if the predicate is relational (not EQ or NE).
1307 ///
1308 bool isRelational() const {
1309 return !isEquality();
1310 }
1311
1312 /// Return true if the predicate is relational (not EQ or NE).
1313 ///
1314 static bool isRelational(Predicate P) {
1315 return !isEquality(P);
1316 }
1317
1318 /// Return true if the predicate is SGT or UGT.
1319 ///
1320 static bool isGT(Predicate P) {
1321 return P == ICMP_SGT || P == ICMP_UGT;
1322 }
1323
1324 /// Return true if the predicate is SLT or ULT.
1325 ///
1326 static bool isLT(Predicate P) {
1327 return P == ICMP_SLT || P == ICMP_ULT;
1328 }
1329
1330 /// Return true if the predicate is SGE or UGE.
1331 ///
1332 static bool isGE(Predicate P) {
1333 return P == ICMP_SGE || P == ICMP_UGE;
1334 }
1335
1336 /// Return true if the predicate is SLE or ULE.
1337 ///
1338 static bool isLE(Predicate P) {
1339 return P == ICMP_SLE || P == ICMP_ULE;
1340 }
1341
1342 /// Exchange the two operands to this instruction in such a way that it does
1343 /// not modify the semantics of the instruction. The predicate value may be
1344 /// changed to retain the same result if the predicate is order dependent
1345 /// (e.g. ult).
1346 /// Swap operands and adjust predicate.
1347 void swapOperands() {
1348 setPredicate(getSwappedPredicate());
1349 Op<0>().swap(Op<1>());
1350 }
1351
1352 // Methods for support type inquiry through isa, cast, and dyn_cast:
1353 static bool classof(const Instruction *I) {
1354 return I->getOpcode() == Instruction::ICmp;
1355 }
1356 static bool classof(const Value *V) {
1357 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1358 }
1359};
1360
1361//===----------------------------------------------------------------------===//
1362// FCmpInst Class
1363//===----------------------------------------------------------------------===//
1364
1365/// This instruction compares its operands according to the predicate given
1366/// to the constructor. It only operates on floating point values or packed
1367/// vectors of floating point values. The operands must be identical types.
1368/// Represents a floating point comparison operator.
1369class FCmpInst: public CmpInst {
1370 void AssertOK() {
1371 assert(isFPPredicate() && "Invalid FCmp predicate value")((void)0);
1372 assert(getOperand(0)->getType() == getOperand(1)->getType() &&((void)0)
1373 "Both operands to FCmp instruction are not of the same type!")((void)0);
1374 // Check that the operands are the right type
1375 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&((void)0)
1376 "Invalid operand types for FCmp instruction")((void)0);
1377 }
1378
1379protected:
1380 // Note: Instruction needs to be a friend here to call cloneImpl.
1381 friend class Instruction;
1382
1383 /// Clone an identical FCmpInst
1384 FCmpInst *cloneImpl() const;
1385
1386public:
1387 /// Constructor with insert-before-instruction semantics.
1388 FCmpInst(
1389 Instruction *InsertBefore, ///< Where to insert
1390 Predicate pred, ///< The predicate to use for the comparison
1391 Value *LHS, ///< The left-hand-side of the expression
1392 Value *RHS, ///< The right-hand-side of the expression
1393 const Twine &NameStr = "" ///< Name of the instruction
1394 ) : CmpInst(makeCmpResultType(LHS->getType()),
1395 Instruction::FCmp, pred, LHS, RHS, NameStr,
1396 InsertBefore) {
1397 AssertOK();
1398 }
1399
1400 /// Constructor with insert-at-end semantics.
1401 FCmpInst(
1402 BasicBlock &InsertAtEnd, ///< Block to insert into.
1403 Predicate pred, ///< The predicate to use for the comparison
1404 Value *LHS, ///< The left-hand-side of the expression
1405 Value *RHS, ///< The right-hand-side of the expression
1406 const Twine &NameStr = "" ///< Name of the instruction
1407 ) : CmpInst(makeCmpResultType(LHS->getType()),
1408 Instruction::FCmp, pred, LHS, RHS, NameStr,
1409 &InsertAtEnd) {
1410 AssertOK();
1411 }
1412
1413 /// Constructor with no-insertion semantics
1414 FCmpInst(
1415 Predicate Pred, ///< The predicate to use for the comparison
1416 Value *LHS, ///< The left-hand-side of the expression
1417 Value *RHS, ///< The right-hand-side of the expression
1418 const Twine &NameStr = "", ///< Name of the instruction
1419 Instruction *FlagsSource = nullptr
1420 ) : CmpInst(makeCmpResultType(LHS->getType()), Instruction::FCmp, Pred, LHS,
1421 RHS, NameStr, nullptr, FlagsSource) {
1422 AssertOK();
1423 }
1424
1425 /// @returns true if the predicate of this instruction is EQ or NE.
1426 /// Determine if this is an equality predicate.
1427 static bool isEquality(Predicate Pred) {
1428 return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ ||
1429 Pred == FCMP_UNE;
1430 }
1431
1432 /// @returns true if the predicate of this instruction is EQ or NE.
1433 /// Determine if this is an equality predicate.
1434 bool isEquality() const { return isEquality(getPredicate()); }
1435
1436 /// @returns true if the predicate of this instruction is commutative.
1437 /// Determine if this is a commutative predicate.
1438 bool isCommutative() const {
1439 return isEquality() ||
1440 getPredicate() == FCMP_FALSE ||
1441 getPredicate() == FCMP_TRUE ||
1442 getPredicate() == FCMP_ORD ||
1443 getPredicate() == FCMP_UNO;
1444 }
1445
1446 /// @returns true if the predicate is relational (not EQ or NE).
1447 /// Determine if this a relational predicate.
1448 bool isRelational() const { return !isEquality(); }
1449
1450 /// Exchange the two operands to this instruction in such a way that it does
1451 /// not modify the semantics of the instruction. The predicate value may be
1452 /// changed to retain the same result if the predicate is order dependent
1453 /// (e.g. ult).
1454 /// Swap operands and adjust predicate.
1455 void swapOperands() {
1456 setPredicate(getSwappedPredicate());
1457 Op<0>().swap(Op<1>());
1458 }
1459
1460 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1461 static bool classof(const Instruction *I) {
1462 return I->getOpcode() == Instruction::FCmp;
1463 }
1464 static bool classof(const Value *V) {
1465 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1466 }
1467};
1468
1469//===----------------------------------------------------------------------===//
1470/// This class represents a function call, abstracting a target
1471/// machine's calling convention. This class uses low bit of the SubClassData
1472/// field to indicate whether or not this is a tail call. The rest of the bits
1473/// hold the calling convention of the call.
1474///
1475class CallInst : public CallBase {
1476 CallInst(const CallInst &CI);
1477
1478 /// Construct a CallInst given a range of arguments.
1479 /// Construct a CallInst from a range of arguments
1480 inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1481 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1482 Instruction *InsertBefore);
1483
1484 inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1485 const Twine &NameStr, Instruction *InsertBefore)
1486 : CallInst(Ty, Func, Args, None, NameStr, InsertBefore) {}
1487
1488 /// Construct a CallInst given a range of arguments.
1489 /// Construct a CallInst from a range of arguments
1490 inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1491 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1492 BasicBlock *InsertAtEnd);
1493
1494 explicit CallInst(FunctionType *Ty, Value *F, const Twine &NameStr,
1495 Instruction *InsertBefore);
1496
1497 CallInst(FunctionType *ty, Value *F, const Twine &NameStr,
1498 BasicBlock *InsertAtEnd);
1499
1500 void init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
1501 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
1502 void init(FunctionType *FTy, Value *Func, const Twine &NameStr);
1503
1504 /// Compute the number of operands to allocate.
1505 static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) {
1506 // We need one operand for the called function, plus the input operand
1507 // counts provided.
1508 return 1 + NumArgs + NumBundleInputs;
1509 }
1510
1511protected:
1512 // Note: Instruction needs to be a friend here to call cloneImpl.
1513 friend class Instruction;
1514
1515 CallInst *cloneImpl() const;
1516
1517public:
1518 static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr = "",
1519 Instruction *InsertBefore = nullptr) {
1520 return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertBefore);
1521 }
1522
1523 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1524 const Twine &NameStr,
1525 Instruction *InsertBefore = nullptr) {
1526 return new (ComputeNumOperands(Args.size()))
1527 CallInst(Ty, Func, Args, None, NameStr, InsertBefore);
1528 }
1529
1530 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1531 ArrayRef<OperandBundleDef> Bundles = None,
1532 const Twine &NameStr = "",
1533 Instruction *InsertBefore = nullptr) {
1534 const int NumOperands =
1535 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles));
1536 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
1537
1538 return new (NumOperands, DescriptorBytes)
1539 CallInst(Ty, Func, Args, Bundles, NameStr, InsertBefore);
1540 }
1541
1542 static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr,
1543 BasicBlock *InsertAtEnd) {
1544 return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertAtEnd);
1545 }
1546
1547 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1548 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1549 return new (ComputeNumOperands(Args.size()))
1550 CallInst(Ty, Func, Args, None, NameStr, InsertAtEnd);
1551 }
1552
1553 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1554 ArrayRef<OperandBundleDef> Bundles,
1555 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1556 const int NumOperands =
1557 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles));
1558 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
1559
1560 return new (NumOperands, DescriptorBytes)
1561 CallInst(Ty, Func, Args, Bundles, NameStr, InsertAtEnd);
1562 }
1563
1564 static CallInst *Create(FunctionCallee Func, const Twine &NameStr = "",
1565 Instruction *InsertBefore = nullptr) {
1566 return Create(Func.getFunctionType(), Func.getCallee(), NameStr,
1567 InsertBefore);
1568 }
1569
1570 static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args,
1571 ArrayRef<OperandBundleDef> Bundles = None,
1572 const Twine &NameStr = "",
1573 Instruction *InsertBefore = nullptr) {
1574 return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles,
1575 NameStr, InsertBefore);
1576 }
1577
1578 static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args,
1579 const Twine &NameStr,
1580 Instruction *InsertBefore = nullptr) {
1581 return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr,
1582 InsertBefore);
1583 }
1584
1585 static CallInst *Create(FunctionCallee Func, const Twine &NameStr,
1586 BasicBlock *InsertAtEnd) {
1587 return Create(Func.getFunctionType(), Func.getCallee(), NameStr,
1588 InsertAtEnd);
1589 }
1590
1591 static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args,
1592 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1593 return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr,
1594 InsertAtEnd);
1595 }
1596
1597 static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args,
1598 ArrayRef<OperandBundleDef> Bundles,
1599 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1600 return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles,
1601 NameStr, InsertAtEnd);
1602 }
1603
1604 /// Create a clone of \p CI with a different set of operand bundles and
1605 /// insert it before \p InsertPt.
1606 ///
1607 /// The returned call instruction is identical \p CI in every way except that
1608 /// the operand bundles for the new instruction are set to the operand bundles
1609 /// in \p Bundles.
1610 static CallInst *Create(CallInst *CI, ArrayRef<OperandBundleDef> Bundles,
1611 Instruction *InsertPt = nullptr);
1612
1613 /// Generate the IR for a call to malloc:
1614 /// 1. Compute the malloc call's argument as the specified type's size,
1615 /// possibly multiplied by the array size if the array size is not
1616 /// constant 1.
1617 /// 2. Call malloc with that argument.
1618 /// 3. Bitcast the result of the malloc call to the specified type.
1619 static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy,
1620 Type *AllocTy, Value *AllocSize,
1621 Value *ArraySize = nullptr,
1622 Function *MallocF = nullptr,
1623 const Twine &Name = "");
1624 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy,
1625 Type *AllocTy, Value *AllocSize,
1626 Value *ArraySize = nullptr,
1627 Function *MallocF = nullptr,
1628 const Twine &Name = "");
1629 static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy,
1630 Type *AllocTy, Value *AllocSize,
1631 Value *ArraySize = nullptr,
1632 ArrayRef<OperandBundleDef> Bundles = None,
1633 Function *MallocF = nullptr,
1634 const Twine &Name = "");
1635 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy,
1636 Type *AllocTy, Value *AllocSize,
1637 Value *ArraySize = nullptr,
1638 ArrayRef<OperandBundleDef> Bundles = None,
1639 Function *MallocF = nullptr,
1640 const Twine &Name = "");
1641 /// Generate the IR for a call to the builtin free function.
1642 static Instruction *CreateFree(Value *Source, Instruction *InsertBefore);
1643 static Instruction *CreateFree(Value *Source, BasicBlock *InsertAtEnd);
1644 static Instruction *CreateFree(Value *Source,
1645 ArrayRef<OperandBundleDef> Bundles,
1646 Instruction *InsertBefore);
1647 static Instruction *CreateFree(Value *Source,
1648 ArrayRef<OperandBundleDef> Bundles,
1649 BasicBlock *InsertAtEnd);
1650
1651 // Note that 'musttail' implies 'tail'.
1652 enum TailCallKind : unsigned {
1653 TCK_None = 0,
1654 TCK_Tail = 1,
1655 TCK_MustTail = 2,
1656 TCK_NoTail = 3,
1657 TCK_LAST = TCK_NoTail
1658 };
1659
1660 using TailCallKindField = Bitfield::Element<TailCallKind, 0, 2, TCK_LAST>;
1661 static_assert(
1662 Bitfield::areContiguous<TailCallKindField, CallBase::CallingConvField>(),
1663 "Bitfields must be contiguous");
1664
1665 TailCallKind getTailCallKind() const {
1666 return getSubclassData<TailCallKindField>();
1667 }
1668
1669 bool isTailCall() const {
1670 TailCallKind Kind = getTailCallKind();
1671 return Kind == TCK_Tail || Kind == TCK_MustTail;
1672 }
1673
1674 bool isMustTailCall() const { return getTailCallKind() == TCK_MustTail; }
1675
1676 bool isNoTailCall() const { return getTailCallKind() == TCK_NoTail; }
1677
1678 void setTailCallKind(TailCallKind TCK) {
1679 setSubclassData<TailCallKindField>(TCK);
1680 }
1681
1682 void setTailCall(bool IsTc = true) {
1683 setTailCallKind(IsTc ? TCK_Tail : TCK_None);
1684 }
1685
1686 /// Return true if the call can return twice
1687 bool canReturnTwice() const { return hasFnAttr(Attribute::ReturnsTwice); }
1688 void setCanReturnTwice() {
1689 addAttribute(AttributeList::FunctionIndex, Attribute::ReturnsTwice);
1690 }
1691
1692 // Methods for support type inquiry through isa, cast, and dyn_cast:
1693 static bool classof(const Instruction *I) {
1694 return I->getOpcode() == Instruction::Call;
1695 }
1696 static bool classof(const Value *V) {
1697 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1698 }
1699
1700 /// Updates profile metadata by scaling it by \p S / \p T.
1701 void updateProfWeight(uint64_t S, uint64_t T);
1702
1703private:
1704 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1705 // method so that subclasses cannot accidentally use it.
1706 template <typename Bitfield>
1707 void setSubclassData(typename Bitfield::Type Value) {
1708 Instruction::setSubclassData<Bitfield>(Value);
1709 }
1710};
1711
1712CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1713 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1714 BasicBlock *InsertAtEnd)
1715 : CallBase(Ty->getReturnType(), Instruction::Call,
1716 OperandTraits<CallBase>::op_end(this) -
1717 (Args.size() + CountBundleInputs(Bundles) + 1),
1718 unsigned(Args.size() + CountBundleInputs(Bundles) + 1),
1719 InsertAtEnd) {
1720 init(Ty, Func, Args, Bundles, NameStr);
1721}
1722
1723CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1724 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1725 Instruction *InsertBefore)
1726 : CallBase(Ty->getReturnType(), Instruction::Call,
1727 OperandTraits<CallBase>::op_end(this) -
1728 (Args.size() + CountBundleInputs(Bundles) + 1),
1729 unsigned(Args.size() + CountBundleInputs(Bundles) + 1),
1730 InsertBefore) {
1731 init(Ty, Func, Args, Bundles, NameStr);
1732}
1733
1734//===----------------------------------------------------------------------===//
1735// SelectInst Class
1736//===----------------------------------------------------------------------===//
1737
1738/// This class represents the LLVM 'select' instruction.
1739///
1740class SelectInst : public Instruction {
1741 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1742 Instruction *InsertBefore)
1743 : Instruction(S1->getType(), Instruction::Select,
1744 &Op<0>(), 3, InsertBefore) {
1745 init(C, S1, S2);
1746 setName(NameStr);
1747 }
1748
1749 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1750 BasicBlock *InsertAtEnd)
1751 : Instruction(S1->getType(), Instruction::Select,
1752 &Op<0>(), 3, InsertAtEnd) {
1753 init(C, S1, S2);
1754 setName(NameStr);
1755 }
1756
1757 void init(Value *C, Value *S1, Value *S2) {
1758 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select")((void)0);
1759 Op<0>() = C;
1760 Op<1>() = S1;
1761 Op<2>() = S2;
1762 }
1763
1764protected:
1765 // Note: Instruction needs to be a friend here to call cloneImpl.
1766 friend class Instruction;
1767
1768 SelectInst *cloneImpl() const;
1769
1770public:
1771 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1772 const Twine &NameStr = "",
1773 Instruction *InsertBefore = nullptr,
1774 Instruction *MDFrom = nullptr) {
1775 SelectInst *Sel = new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1776 if (MDFrom)
1777 Sel->copyMetadata(*MDFrom);
1778 return Sel;
1779 }
1780
1781 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1782 const Twine &NameStr,
1783 BasicBlock *InsertAtEnd) {
1784 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1785 }
1786
1787 const Value *getCondition() const { return Op<0>(); }
1788 const Value *getTrueValue() const { return Op<1>(); }
1789 const Value *getFalseValue() const { return Op<2>(); }
1790 Value *getCondition() { return Op<0>(); }
1791 Value *getTrueValue() { return Op<1>(); }
1792 Value *getFalseValue() { return Op<2>(); }
1793
1794 void setCondition(Value *V) { Op<0>() = V; }
1795 void setTrueValue(Value *V) { Op<1>() = V; }
1796 void setFalseValue(Value *V) { Op<2>() = V; }
1797
1798 /// Swap the true and false values of the select instruction.
1799 /// This doesn't swap prof metadata.
1800 void swapValues() { Op<1>().swap(Op<2>()); }
1801
1802 /// Return a string if the specified operands are invalid
1803 /// for a select operation, otherwise return null.
1804 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1805
1806 /// Transparently provide more efficient getOperand methods.
1807 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
;
1808
1809 OtherOps getOpcode() const {
1810 return static_cast<OtherOps>(Instruction::getOpcode());
1811 }
1812
1813 // Methods for support type inquiry through isa, cast, and dyn_cast:
1814 static bool classof(const Instruction *I) {
1815 return I->getOpcode() == Instruction::Select;
1816 }
1817 static bool classof(const Value *V) {
1818 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1819 }
1820};
1821
1822template <>
1823struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1824};
1825
1826DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)SelectInst::op_iterator SelectInst::op_begin() { return OperandTraits
<SelectInst>::op_begin(this); } SelectInst::const_op_iterator
SelectInst::op_begin() const { return OperandTraits<SelectInst
>::op_begin(const_cast<SelectInst*>(this)); } SelectInst
::op_iterator SelectInst::op_end() { return OperandTraits<
SelectInst>::op_end(this); } SelectInst::const_op_iterator
SelectInst::op_end() const { return OperandTraits<SelectInst
>::op_end(const_cast<SelectInst*>(this)); } Value *SelectInst
::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null
<Value>( OperandTraits<SelectInst>::op_begin(const_cast
<SelectInst*>(this))[i_nocapture].get()); } void SelectInst
::setOperand(unsigned i_nocapture, Value *Val_nocapture) { ((
void)0); OperandTraits<SelectInst>::op_begin(this)[i_nocapture
] = Val_nocapture; } unsigned SelectInst::getNumOperands() const
{ return OperandTraits<SelectInst>::operands(this); } template
<int Idx_nocapture> Use &SelectInst::Op() { return
this->OpFrom<Idx_nocapture>(this); } template <int
Idx_nocapture> const Use &SelectInst::Op() const { return
this->OpFrom<Idx_nocapture>(this); }
1827
1828//===----------------------------------------------------------------------===//
1829// VAArgInst Class
1830//===----------------------------------------------------------------------===//
1831
1832/// This class represents the va_arg llvm instruction, which returns
1833/// an argument of the specified type given a va_list and increments that list
1834///
1835class VAArgInst : public UnaryInstruction {
1836protected:
1837 // Note: Instruction needs to be a friend here to call cloneImpl.
1838 friend class Instruction;
1839
1840 VAArgInst *cloneImpl() const;
1841
1842public:
1843 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1844 Instruction *InsertBefore = nullptr)
1845 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1846 setName(NameStr);
1847 }
1848
1849 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1850 BasicBlock *InsertAtEnd)
1851 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1852 setName(NameStr);
1853 }
1854
1855 Value *getPointerOperand() { return getOperand(0); }
1856 const Value *getPointerOperand() const { return getOperand(0); }
1857 static unsigned getPointerOperandIndex() { return 0U; }
1858
1859 // Methods for support type inquiry through isa, cast, and dyn_cast:
1860 static bool classof(const Instruction *I) {
1861 return I->getOpcode() == VAArg;
1862 }
1863 static bool classof(const Value *V) {
1864 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1865 }
1866};
1867
1868//===----------------------------------------------------------------------===//
1869// ExtractElementInst Class
1870//===----------------------------------------------------------------------===//
1871
1872/// This instruction extracts a single (scalar)
1873/// element from a VectorType value
1874///
1875class ExtractElementInst : public Instruction {
1876 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1877 Instruction *InsertBefore = nullptr);
1878 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1879 BasicBlock *InsertAtEnd);
1880
1881protected:
1882 // Note: Instruction needs to be a friend here to call cloneImpl.
1883 friend class Instruction;
1884
1885 ExtractElementInst *cloneImpl() const;
1886
1887public:
1888 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1889 const Twine &NameStr = "",
1890 Instruction *InsertBefore = nullptr) {
1891 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1892 }
1893
1894 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1895 const Twine &NameStr,
1896 BasicBlock *InsertAtEnd) {
1897 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1898 }
1899
1900 /// Return true if an extractelement instruction can be
1901 /// formed with the specified operands.
1902 static bool isValidOperands(const Value *Vec, const Value *Idx);
1903
1904 Value *getVectorOperand() { return Op<0>(); }
1905 Value *getIndexOperand() { return Op<1>(); }
1906 const Value *getVectorOperand() const { return Op<0>(); }
1907 const Value *getIndexOperand() const { return Op<1>(); }
1908
1909 VectorType *getVectorOperandType() const {
1910 return cast<VectorType>(getVectorOperand()->getType());
1911 }
1912
1913 /// Transparently provide more efficient getOperand methods.
1914 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
;
1915
1916 // Methods for support type inquiry through isa, cast, and dyn_cast:
1917 static bool classof(const Instruction *I) {
1918 return I->getOpcode() == Instruction::ExtractElement;
1919 }
1920 static bool classof(const Value *V) {
1921 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1922 }
1923};
1924
1925template <>
1926struct OperandTraits<ExtractElementInst> :
1927 public FixedNumOperandTraits<ExtractElementInst, 2> {
1928};
1929
1930DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)ExtractElementInst::op_iterator ExtractElementInst::op_begin(
) { return OperandTraits<ExtractElementInst>::op_begin(
this); } ExtractElementInst::const_op_iterator ExtractElementInst
::op_begin() const { return OperandTraits<ExtractElementInst
>::op_begin(const_cast<ExtractElementInst*>(this)); }
ExtractElementInst::op_iterator ExtractElementInst::op_end()
{ return OperandTraits<ExtractElementInst>::op_end(this
); } ExtractElementInst::const_op_iterator ExtractElementInst
::op_end() const { return OperandTraits<ExtractElementInst
>::op_end(const_cast<ExtractElementInst*>(this)); } Value
*ExtractElementInst::getOperand(unsigned i_nocapture) const {
((void)0); return cast_or_null<Value>( OperandTraits<
ExtractElementInst>::op_begin(const_cast<ExtractElementInst
*>(this))[i_nocapture].get()); } void ExtractElementInst::
setOperand(unsigned i_nocapture, Value *Val_nocapture) { ((void
)0); OperandTraits<ExtractElementInst>::op_begin(this)[
i_nocapture] = Val_nocapture; } unsigned ExtractElementInst::
getNumOperands() const { return OperandTraits<ExtractElementInst
>::operands(this); } template <int Idx_nocapture> Use
&ExtractElementInst::Op() { return this->OpFrom<Idx_nocapture
>(this); } template <int Idx_nocapture> const Use &
ExtractElementInst::Op() const { return this->OpFrom<Idx_nocapture
>(this); }
1931
1932//===----------------------------------------------------------------------===//
1933// InsertElementInst Class
1934//===----------------------------------------------------------------------===//
1935
1936/// This instruction inserts a single (scalar)
1937/// element into a VectorType value
1938///
1939class InsertElementInst : public Instruction {
1940 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1941 const Twine &NameStr = "",
1942 Instruction *InsertBefore = nullptr);
1943 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr,
1944 BasicBlock *InsertAtEnd);
1945
1946protected:
1947 // Note: Instruction needs to be a friend here to call cloneImpl.
1948 friend class Instruction;
1949
1950 InsertElementInst *cloneImpl() const;
1951
1952public:
1953 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1954 const Twine &NameStr = "",
1955 Instruction *InsertBefore = nullptr) {
1956 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1957 }
1958
1959 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1960 const Twine &NameStr,
1961 BasicBlock *InsertAtEnd) {
1962 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1963 }
1964
1965 /// Return true if an insertelement instruction can be
1966 /// formed with the specified operands.
1967 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1968 const Value *Idx);
1969
1970 /// Overload to return most specific vector type.
1971 ///
1972 VectorType *getType() const {
1973 return cast<VectorType>(Instruction::getType());
1974 }
1975
1976 /// Transparently provide more efficient getOperand methods.
1977 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
;
1978
1979 // Methods for support type inquiry through isa, cast, and dyn_cast:
1980 static bool classof(const Instruction *I) {
1981 return I->getOpcode() == Instruction::InsertElement;
1982 }
1983 static bool classof(const Value *V) {
1984 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1985 }
1986};
1987
1988template <>
1989struct OperandTraits<InsertElementInst> :
1990 public FixedNumOperandTraits<InsertElementInst, 3> {
1991};
1992
1993DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)InsertElementInst::op_iterator InsertElementInst::op_begin() {
return OperandTraits<InsertElementInst>::op_begin(this
); } InsertElementInst::const_op_iterator InsertElementInst::
op_begin() const { return OperandTraits<InsertElementInst>
::op_begin(const_cast<InsertElementInst*>(this)); } InsertElementInst
::op_iterator InsertElementInst::op_end() { return OperandTraits
<InsertElementInst>::op_end(this); } InsertElementInst::
const_op_iterator InsertElementInst::op_end() const { return OperandTraits
<InsertElementInst>::op_end(const_cast<InsertElementInst
*>(this)); } Value *InsertElementInst::getOperand(unsigned
i_nocapture) const { ((void)0); return cast_or_null<Value
>( OperandTraits<InsertElementInst>::op_begin(const_cast
<InsertElementInst*>(this))[i_nocapture].get()); } void
InsertElementInst::setOperand(unsigned i_nocapture, Value *Val_nocapture
) { ((void)0); OperandTraits<InsertElementInst>::op_begin
(this)[i_nocapture] = Val_nocapture; } unsigned InsertElementInst
::getNumOperands() const { return OperandTraits<InsertElementInst
>::operands(this); } template <int Idx_nocapture> Use
&InsertElementInst::Op() { return this->OpFrom<Idx_nocapture
>(this); } template <int Idx_nocapture> const Use &
InsertElementInst::Op() const { return this->OpFrom<Idx_nocapture
>(this); }
1994
1995//===----------------------------------------------------------------------===//
1996// ShuffleVectorInst Class
1997//===----------------------------------------------------------------------===//
1998
1999constexpr int UndefMaskElem = -1;
2000
2001/// This instruction constructs a fixed permutation of two
2002/// input vectors.
2003///
2004/// For each element of the result vector, the shuffle mask selects an element
2005/// from one of the input vectors to copy to the result. Non-negative elements
2006/// in the mask represent an index into the concatenated pair of input vectors.
2007/// UndefMaskElem (-1) specifies that the result element is undefined.
2008///
2009/// For scalable vectors, all the elements of the mask must be 0 or -1. This
2010/// requirement may be relaxed in the future.
2011class ShuffleVectorInst : public Instruction {
2012 SmallVector<int, 4> ShuffleMask;
2013 Constant *ShuffleMaskForBitcode;
2014
2015protected:
2016 // Note: Instruction needs to be a friend here to call cloneImpl.
2017 friend class Instruction;
2018
2019 ShuffleVectorInst *cloneImpl() const;
2020
2021public:
2022 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
2023 const Twine &NameStr = "",
2024 Instruction *InsertBefor = nullptr);
2025 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
2026 const Twine &NameStr, BasicBlock *InsertAtEnd);
2027 ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask,
2028 const Twine &NameStr = "",
2029 Instruction *InsertBefor = nullptr);
2030 ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask,
2031 const Twine &NameStr, BasicBlock *InsertAtEnd);
2032
2033 void *operator new(size_t S) { return User::operator new(S, 2); }
2034 void operator delete(void *Ptr) { return User::operator delete(Ptr); }
2035
2036 /// Swap the operands and adjust the mask to preserve the semantics
2037 /// of the instruction.
2038 void commute();
2039
2040 /// Return true if a shufflevector instruction can be
2041 /// formed with the specified operands.
2042 static bool isValidOperands(const Value *V1, const Value *V2,
2043 const Value *Mask);
2044 static bool isValidOperands(const Value *V1, const Value *V2,
2045 ArrayRef<int> Mask);
2046
2047 /// Overload to return most specific vector type.
2048 ///
2049 VectorType *getType() const {
2050 return cast<VectorType>(Instruction::getType());
2051 }
2052
2053 /// Transparently provide more efficient getOperand methods.
2054 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
;
2055
2056 /// Return the shuffle mask value of this instruction for the given element
2057 /// index. Return UndefMaskElem if the element is undef.
2058 int getMaskValue(unsigned Elt) const { return ShuffleMask[Elt]; }
2059
2060 /// Convert the input shuffle mask operand to a vector of integers. Undefined
2061 /// elements of the mask are returned as UndefMaskElem.
2062 static void getShuffleMask(const Constant *Mask,
2063 SmallVectorImpl<int> &Result);
2064
2065 /// Return the mask for this instruction as a vector of integers. Undefined
2066 /// elements of the mask are returned as UndefMaskElem.
2067 void getShuffleMask(SmallVectorImpl<int> &Result) const {
2068 Result.assign(ShuffleMask.begin(), ShuffleMask.end());
2069 }
2070
2071 /// Return the mask for this instruction, for use in bitcode.
2072 ///
2073 /// TODO: This is temporary until we decide a new bitcode encoding for
2074 /// shufflevector.
2075 Constant *getShuffleMaskForBitcode() const { return ShuffleMaskForBitcode; }
2076
2077 static Constant *convertShuffleMaskForBitcode(ArrayRef<int> Mask,
2078 Type *ResultTy);
2079
2080 void setShuffleMask(ArrayRef<int> Mask);
2081
2082 ArrayRef<int> getShuffleMask() const { return ShuffleMask; }
2083
2084 /// Return true if this shuffle returns a vector with a different number of
2085 /// elements than its source vectors.
2086 /// Examples: shufflevector <4 x n> A, <4 x n> B, <1,2,3>
2087 /// shufflevector <4 x n> A, <4 x n> B, <1,2,3,4,5>
2088 bool changesLength() const {
2089 unsigned NumSourceElts = cast<VectorType>(Op<0>()->getType())
2090 ->getElementCount()
2091 .getKnownMinValue();
2092 unsigned NumMaskElts = ShuffleMask.size();
2093 return NumSourceElts != NumMaskElts;
2094 }
2095
2096 /// Return true if this shuffle returns a vector with a greater number of
2097 /// elements than its source vectors.
2098 /// Example: shufflevector <2 x n> A, <2 x n> B, <1,2,3>
2099 bool increasesLength() const {
2100 unsigned NumSourceElts = cast<VectorType>(Op<0>()->getType())
2101 ->getElementCount()
2102 .getKnownMinValue();
2103 unsigned NumMaskElts = ShuffleMask.size();
2104 return NumSourceElts < NumMaskElts;
2105 }
2106
2107 /// Return true if this shuffle mask chooses elements from exactly one source
2108 /// vector.
2109 /// Example: <7,5,undef,7>
2110 /// This assumes that vector operands are the same length as the mask.
2111 static bool isSingleSourceMask(ArrayRef<int> Mask);
2112 static bool isSingleSourceMask(const Constant *Mask) {
2113 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((void)0);
2114 SmallVector<int, 16> MaskAsInts;
2115 getShuffleMask(Mask, MaskAsInts);
2116 return isSingleSourceMask(MaskAsInts);
2117 }
2118
2119 /// Return true if this shuffle chooses elements from exactly one source
2120 /// vector without changing the length of that vector.
2121 /// Example: shufflevector <4 x n> A, <4 x n> B, <3,0,undef,3>
2122 /// TODO: Optionally allow length-changing shuffles.
2123 bool isSingleSource() const {
2124 return !changesLength() && isSingleSourceMask(ShuffleMask);
2125 }
2126
2127 /// Return true if this shuffle mask chooses elements from exactly one source
2128 /// vector without lane crossings. A shuffle using this mask is not
2129 /// necessarily a no-op because it may change the number of elements from its
2130 /// input vectors or it may provide demanded bits knowledge via undef lanes.
2131 /// Example: <undef,undef,2,3>
2132 static bool isIdentityMask(ArrayRef<int> Mask);
2133 static bool isIdentityMask(const Constant *Mask) {
2134 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((void)0);
2135 SmallVector<int, 16> MaskAsInts;
2136 getShuffleMask(Mask, MaskAsInts);
2137 return isIdentityMask(MaskAsInts);
2138 }
2139
2140 /// Return true if this shuffle chooses elements from exactly one source
2141 /// vector without lane crossings and does not change the number of elements
2142 /// from its input vectors.
2143 /// Example: shufflevector <4 x n> A, <4 x n> B, <4,undef,6,undef>
2144 bool isIdentity() const {
2145 return !changesLength() && isIdentityMask(ShuffleMask);
2146 }
2147
2148 /// Return true if this shuffle lengthens exactly one source vector with
2149 /// undefs in the high elements.
2150 bool isIdentityWithPadding() const;
2151
2152 /// Return true if this shuffle extracts the first N elements of exactly one
2153 /// source vector.
2154 bool isIdentityWithExtract() const;
2155
2156 /// Return true if this shuffle concatenates its 2 source vectors. This
2157 /// returns false if either input is undefined. In that case, the shuffle is
2158 /// is better classified as an identity with padding operation.
2159 bool isConcat() const;
2160
2161 /// Return true if this shuffle mask chooses elements from its source vectors
2162 /// without lane crossings. A shuffle using this mask would be
2163 /// equivalent to a vector select with a constant condition operand.
2164 /// Example: <4,1,6,undef>
2165 /// This returns false if the mask does not choose from both input vectors.
2166 /// In that case, the shuffle is better classified as an identity shuffle.
2167 /// This assumes that vector operands are the same length as the mask
2168 /// (a length-changing shuffle can never be equivalent to a vector select).
2169 static bool isSelectMask(ArrayRef<int> Mask);
2170 static bool isSelectMask(const Constant *Mask) {
2171 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((void)0);
2172 SmallVector<int, 16> MaskAsInts;
2173 getShuffleMask(Mask, MaskAsInts);
2174 return isSelectMask(MaskAsInts);
2175 }
2176
2177 /// Return true if this shuffle chooses elements from its source vectors
2178 /// without lane crossings and all operands have the same number of elements.
2179 /// In other words, this shuffle is equivalent to a vector select with a
2180 /// constant condition operand.
2181 /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,1,6,3>
2182 /// This returns false if the mask does not choose from both input vectors.
2183 /// In that case, the shuffle is better classified as an identity shuffle.
2184 /// TODO: Optionally allow length-changing shuffles.
2185 bool isSelect() const {
2186 return !changesLength() && isSelectMask(ShuffleMask);
2187 }
2188
2189 /// Return true if this shuffle mask swaps the order of elements from exactly
2190 /// one source vector.
2191 /// Example: <7,6,undef,4>
2192 /// This assumes that vector operands are the same length as the mask.
2193 static bool isReverseMask(ArrayRef<int> Mask);
2194 static bool isReverseMask(const Constant *Mask) {
2195 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((void)0);
2196 SmallVector<int, 16> MaskAsInts;
2197 getShuffleMask(Mask, MaskAsInts);
2198 return isReverseMask(MaskAsInts);
2199 }
2200
2201 /// Return true if this shuffle swaps the order of elements from exactly
2202 /// one source vector.
2203 /// Example: shufflevector <4 x n> A, <4 x n> B, <3,undef,1,undef>
2204 /// TODO: Optionally allow length-changing shuffles.
2205 bool isReverse() const {
2206 return !changesLength() && isReverseMask(ShuffleMask);
2207 }
2208
2209 /// Return true if this shuffle mask chooses all elements with the same value
2210 /// as the first element of exactly one source vector.
2211 /// Example: <4,undef,undef,4>
2212 /// This assumes that vector operands are the same length as the mask.
2213 static bool isZeroEltSplatMask(ArrayRef<int> Mask);
2214 static bool isZeroEltSplatMask(const Constant *Mask) {
2215 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((void)0);
2216 SmallVector<int, 16> MaskAsInts;
2217 getShuffleMask(Mask, MaskAsInts);
2218 return isZeroEltSplatMask(MaskAsInts);
2219 }
2220
2221 /// Return true if all elements of this shuffle are the same value as the
2222 /// first element of exactly one source vector without changing the length
2223 /// of that vector.
2224 /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,0,undef,0>
2225 /// TODO: Optionally allow length-changing shuffles.
2226 /// TODO: Optionally allow splats from other elements.
2227 bool isZeroEltSplat() const {
2228 return !changesLength() && isZeroEltSplatMask(ShuffleMask);
2229 }
2230
2231 /// Return true if this shuffle mask is a transpose mask.
2232 /// Transpose vector masks transpose a 2xn matrix. They read corresponding
2233 /// even- or odd-numbered vector elements from two n-dimensional source
2234 /// vectors and write each result into consecutive elements of an
2235 /// n-dimensional destination vector. Two shuffles are necessary to complete
2236 /// the transpose, one for the even elements and another for the odd elements.
2237 /// This description closely follows how the TRN1 and TRN2 AArch64
2238 /// instructions operate.
2239 ///
2240 /// For example, a simple 2x2 matrix can be transposed with:
2241 ///
2242 /// ; Original matrix
2243 /// m0 = < a, b >
2244 /// m1 = < c, d >
2245 ///
2246 /// ; Transposed matrix
2247 /// t0 = < a, c > = shufflevector m0, m1, < 0, 2 >
2248 /// t1 = < b, d > = shufflevector m0, m1, < 1, 3 >
2249 ///
2250 /// For matrices having greater than n columns, the resulting nx2 transposed
2251 /// matrix is stored in two result vectors such that one vector contains
2252 /// interleaved elements from all the even-numbered rows and the other vector
2253 /// contains interleaved elements from all the odd-numbered rows. For example,
2254 /// a 2x4 matrix can be transposed with:
2255 ///
2256 /// ; Original matrix
2257 /// m0 = < a, b, c, d >
2258 /// m1 = < e, f, g, h >
2259 ///
2260 /// ; Transposed matrix
2261 /// t0 = < a, e, c, g > = shufflevector m0, m1 < 0, 4, 2, 6 >
2262 /// t1 = < b, f, d, h > = shufflevector m0, m1 < 1, 5, 3, 7 >
2263 static bool isTransposeMask(ArrayRef<int> Mask);
2264 static bool isTransposeMask(const Constant *Mask) {
2265 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((void)0);
2266 SmallVector<int, 16> MaskAsInts;
2267 getShuffleMask(Mask, MaskAsInts);
2268 return isTransposeMask(MaskAsInts);
2269 }
2270
2271 /// Return true if this shuffle transposes the elements of its inputs without
2272 /// changing the length of the vectors. This operation may also be known as a
2273 /// merge or interleave. See the description for isTransposeMask() for the
2274 /// exact specification.
2275 /// Example: shufflevector <4 x n> A, <4 x n> B, <0,4,2,6>
2276 bool isTranspose() const {
2277 return !changesLength() && isTransposeMask(ShuffleMask);
2278 }
2279
2280 /// Return true if this shuffle mask is an extract subvector mask.
2281 /// A valid extract subvector mask returns a smaller vector from a single
2282 /// source operand. The base extraction index is returned as well.
2283 static bool isExtractSubvectorMask(ArrayRef<int> Mask, int NumSrcElts,
2284 int &Index);
2285 static bool isExtractSubvectorMask(const Constant *Mask, int NumSrcElts,
2286 int &Index) {
2287 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((void)0);
2288 // Not possible to express a shuffle mask for a scalable vector for this
2289 // case.
2290 if (isa<ScalableVectorType>(Mask->getType()))
2291 return false;
2292 SmallVector<int, 16> MaskAsInts;
2293 getShuffleMask(Mask, MaskAsInts);
2294 return isExtractSubvectorMask(MaskAsInts, NumSrcElts, Index);
2295 }
2296
2297 /// Return true if this shuffle mask is an extract subvector mask.
2298 bool isExtractSubvectorMask(int &Index) const {
2299 // Not possible to express a shuffle mask for a scalable vector for this
2300 // case.
2301 if (isa<ScalableVectorType>(getType()))
2302 return false;
2303
2304 int NumSrcElts =
2305 cast<FixedVectorType>(Op<0>()->getType())->getNumElements();
2306 return isExtractSubvectorMask(ShuffleMask, NumSrcElts, Index);
2307 }
2308
2309 /// Change values in a shuffle permute mask assuming the two vector operands
2310 /// of length InVecNumElts have swapped position.
2311 static void commuteShuffleMask(MutableArrayRef<int> Mask,
2312 unsigned InVecNumElts) {
2313 for (int &Idx : Mask) {
2314 if (Idx == -1)
2315 continue;
2316 Idx = Idx < (int)InVecNumElts ? Idx + InVecNumElts : Idx - InVecNumElts;
2317 assert(Idx >= 0 && Idx < (int)InVecNumElts * 2 &&((void)0)
2318 "shufflevector mask index out of range")((void)0);
2319 }
2320 }
2321
2322 // Methods for support type inquiry through isa, cast, and dyn_cast:
2323 static bool classof(const Instruction *I) {
2324 return I->getOpcode() == Instruction::ShuffleVector;
2325 }
2326 static bool classof(const Value *V) {
2327 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2328 }
2329};
2330
2331template <>
2332struct OperandTraits<ShuffleVectorInst>
2333 : public FixedNumOperandTraits<ShuffleVectorInst, 2> {};
2334
2335DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)ShuffleVectorInst::op_iterator ShuffleVectorInst::op_begin() {
return OperandTraits<ShuffleVectorInst>::op_begin(this
); } ShuffleVectorInst::const_op_iterator ShuffleVectorInst::
op_begin() const { return OperandTraits<ShuffleVectorInst>
::op_begin(const_cast<ShuffleVectorInst*>(this)); } ShuffleVectorInst
::op_iterator ShuffleVectorInst::op_end() { return OperandTraits
<ShuffleVectorInst>::op_end(this); } ShuffleVectorInst::
const_op_iterator ShuffleVectorInst::op_end() const { return OperandTraits
<ShuffleVectorInst>::op_end(const_cast<ShuffleVectorInst
*>(this)); } Value *ShuffleVectorInst::getOperand(unsigned
i_nocapture) const { ((void)0); return cast_or_null<Value
>( OperandTraits<ShuffleVectorInst>::op_begin(const_cast
<ShuffleVectorInst*>(this))[i_nocapture].get()); } void
ShuffleVectorInst::setOperand(unsigned i_nocapture, Value *Val_nocapture
) { ((void)0); OperandTraits<ShuffleVectorInst>::op_begin
(this)[i_nocapture] = Val_nocapture; } unsigned ShuffleVectorInst
::getNumOperands() const { return OperandTraits<ShuffleVectorInst
>::operands(this); } template <int Idx_nocapture> Use
&ShuffleVectorInst::Op() { return this->OpFrom<Idx_nocapture
>(this); } template <int Idx_nocapture> const Use &
ShuffleVectorInst::Op() const { return this->OpFrom<Idx_nocapture
>(this); }
2336
2337//===----------------------------------------------------------------------===//
2338// ExtractValueInst Class
2339//===----------------------------------------------------------------------===//
2340
2341/// This instruction extracts a struct member or array
2342/// element value from an aggregate value.
2343///
2344class ExtractValueInst : public UnaryInstruction {
2345 SmallVector<unsigned, 4> Indices;
2346
2347 ExtractValueInst(const ExtractValueInst &EVI);
2348
2349 /// Constructors - Create a extractvalue instruction with a base aggregate
2350 /// value and a list of indices. The first ctor can optionally insert before
2351 /// an existing instruction, the second appends the new instruction to the
2352 /// specified BasicBlock.
2353 inline ExtractValueInst(Value *Agg,
2354 ArrayRef<unsigned> Idxs,
2355 const Twine &NameStr,
2356 Instruction *InsertBefore);
2357 inline ExtractValueInst(Value *Agg,
2358 ArrayRef<unsigned> Idxs,
2359 const Twine &NameStr, BasicBlock *InsertAtEnd);
2360
2361 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
2362
2363protected:
2364 // Note: Instruction needs to be a friend here to call cloneImpl.
2365 friend class Instruction;
2366
2367 ExtractValueInst *cloneImpl() const;
2368
2369public:
2370 static ExtractValueInst *Create(Value *Agg,
2371 ArrayRef<unsigned> Idxs,
2372 const Twine &NameStr = "",
2373 Instruction *InsertBefore = nullptr) {
2374 return new
2375 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
2376 }
2377
2378 static ExtractValueInst *Create(Value *Agg,
2379 ArrayRef<unsigned> Idxs,
2380 const Twine &NameStr,
2381 BasicBlock *InsertAtEnd) {
2382 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
2383 }
2384
2385 /// Returns the type of the element that would be extracted
2386 /// with an extractvalue instruction with the specified parameters.
2387 ///
2388 /// Null is returned if the indices are invalid for the specified type.
2389 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
2390
2391 using idx_iterator = const unsigned*;
2392
2393 inline idx_iterator idx_begin() const { return Indices.begin(); }
2394 inline idx_iterator idx_end() const { return Indices.end(); }
2395 inline iterator_range<idx_iterator> indices() const {
2396 return make_range(idx_begin(), idx_end());
2397 }
2398
2399 Value *getAggregateOperand() {
2400 return getOperand(0);
2401 }
2402 const Value *getAggregateOperand() const {
2403 return getOperand(0);
2404 }
2405 static unsigned getAggregateOperandIndex() {
2406 return 0U; // get index for modifying correct operand
2407 }
2408
2409 ArrayRef<unsigned> getIndices() const {
2410 return Indices;
2411 }
2412
2413 unsigned getNumIndices() const {
2414 return (unsigned)Indices.size();
2415 }
2416
2417 bool hasIndices() const {
2418 return true;
2419 }
2420
2421 // Methods for support type inquiry through isa, cast, and dyn_cast:
2422 static bool classof(const Instruction *I) {
2423 return I->getOpcode() == Instruction::ExtractValue;
2424 }
2425 static bool classof(const Value *V) {
2426 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2427 }
2428};
2429
2430ExtractValueInst::ExtractValueInst(Value *Agg,
2431 ArrayRef<unsigned> Idxs,
2432 const Twine &NameStr,
2433 Instruction *InsertBefore)
2434 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2435 ExtractValue, Agg, InsertBefore) {
2436 init(Idxs, NameStr);
2437}
2438
2439ExtractValueInst::ExtractValueInst(Value *Agg,
2440 ArrayRef<unsigned> Idxs,
2441 const Twine &NameStr,
2442 BasicBlock *InsertAtEnd)
2443 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2444 ExtractValue, Agg, InsertAtEnd) {
2445 init(Idxs, NameStr);
2446}
2447
2448//===----------------------------------------------------------------------===//
2449// InsertValueInst Class
2450//===----------------------------------------------------------------------===//
2451
2452/// This instruction inserts a struct field of array element
2453/// value into an aggregate value.
2454///
2455class InsertValueInst : public Instruction {
2456 SmallVector<unsigned, 4> Indices;
2457
2458 InsertValueInst(const InsertValueInst &IVI);
2459
2460 /// Constructors - Create a insertvalue instruction with a base aggregate
2461 /// value, a value to insert, and a list of indices. The first ctor can
2462 /// optionally insert before an existing instruction, the second appends
2463 /// the new instruction to the specified BasicBlock.
2464 inline InsertValueInst(Value *Agg, Value *Val,
2465 ArrayRef<unsigned> Idxs,
2466 const Twine &NameStr,
2467 Instruction *InsertBefore);
2468 inline InsertValueInst(Value *Agg, Value *Val,
2469 ArrayRef<unsigned> Idxs,
2470 const Twine &NameStr, BasicBlock *InsertAtEnd);
2471
2472 /// Constructors - These two constructors are convenience methods because one
2473 /// and two index insertvalue instructions are so common.
2474 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
2475 const Twine &NameStr = "",
2476 Instruction *InsertBefore = nullptr);
2477 InsertValueInst(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr,
2478 BasicBlock *InsertAtEnd);
2479
2480 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2481 const Twine &NameStr);
2482
2483protected:
2484 // Note: Instruction needs to be a friend here to call cloneImpl.
2485 friend class Instruction;
2486
2487 InsertValueInst *cloneImpl() const;
2488
2489public:
2490 // allocate space for exactly two operands
2491 void *operator new(size_t S) { return User::operator new(S, 2); }
2492 void operator delete(void *Ptr) { User::operator delete(Ptr); }
2493
2494 static InsertValueInst *Create(Value *Agg, Value *Val,
2495 ArrayRef<unsigned> Idxs,
2496 const Twine &NameStr = "",
2497 Instruction *InsertBefore = nullptr) {
2498 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
2499 }
2500
2501 static InsertValueInst *Create(Value *Agg, Value *Val,
2502 ArrayRef<unsigned> Idxs,
2503 const Twine &NameStr,
2504 BasicBlock *InsertAtEnd) {
2505 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
2506 }
2507
2508 /// Transparently provide more efficient getOperand methods.
2509 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
;
2510
2511 using idx_iterator = const unsigned*;
2512
2513 inline idx_iterator idx_begin() const { return Indices.begin(); }
2514 inline idx_iterator idx_end() const { return Indices.end(); }
2515 inline iterator_range<idx_iterator> indices() const {
2516 return make_range(idx_begin(), idx_end());
2517 }
2518
2519 Value *getAggregateOperand() {
2520 return getOperand(0);
2521 }
2522 const Value *getAggregateOperand() const {
2523 return getOperand(0);
2524 }
2525 static unsigned getAggregateOperandIndex() {
2526 return 0U; // get index for modifying correct operand
2527 }
2528
2529 Value *getInsertedValueOperand() {
2530 return getOperand(1);
2531 }
2532 const Value *getInsertedValueOperand() const {
2533 return getOperand(1);
2534 }
2535 static unsigned getInsertedValueOperandIndex() {
2536 return 1U; // get index for modifying correct operand
2537 }
2538
2539 ArrayRef<unsigned> getIndices() const {
2540 return Indices;
2541 }
2542
2543 unsigned getNumIndices() const {
2544 return (unsigned)Indices.size();
2545 }
2546
2547 bool hasIndices() const {
2548 return true;
2549 }
2550
2551 // Methods for support type inquiry through isa, cast, and dyn_cast:
2552 static bool classof(const Instruction *I) {
2553 return I->getOpcode() == Instruction::InsertValue;
2554 }
2555 static bool classof(const Value *V) {
2556 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2557 }
2558};
2559
2560template <>
2561struct OperandTraits<InsertValueInst> :
2562 public FixedNumOperandTraits<InsertValueInst, 2> {
2563};
2564
2565InsertValueInst::InsertValueInst(Value *Agg,
2566 Value *Val,
2567 ArrayRef<unsigned> Idxs,
2568 const Twine &NameStr,
2569 Instruction *InsertBefore)
2570 : Instruction(Agg->getType(), InsertValue,
2571 OperandTraits<InsertValueInst>::op_begin(this),
2572 2, InsertBefore) {
2573 init(Agg, Val, Idxs, NameStr);
2574}
2575
2576InsertValueInst::InsertValueInst(Value *Agg,
2577 Value *Val,
2578 ArrayRef<unsigned> Idxs,
2579 const Twine &NameStr,
2580 BasicBlock *InsertAtEnd)
2581 : Instruction(Agg->getType(), InsertValue,
2582 OperandTraits<InsertValueInst>::op_begin(this),
2583 2, InsertAtEnd) {
2584 init(Agg, Val, Idxs, NameStr);
2585}
2586
2587DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)InsertValueInst::op_iterator InsertValueInst::op_begin() { return
OperandTraits<InsertValueInst>::op_begin(this); } InsertValueInst
::const_op_iterator InsertValueInst::op_begin() const { return
OperandTraits<InsertValueInst>::op_begin(const_cast<
InsertValueInst*>(this)); } InsertValueInst::op_iterator InsertValueInst
::op_end() { return OperandTraits<InsertValueInst>::op_end
(this); } InsertValueInst::const_op_iterator InsertValueInst::
op_end() const { return OperandTraits<InsertValueInst>::
op_end(const_cast<InsertValueInst*>(this)); } Value *InsertValueInst
::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null
<Value>( OperandTraits<InsertValueInst>::op_begin
(const_cast<InsertValueInst*>(this))[i_nocapture].get()
); } void InsertValueInst::setOperand(unsigned i_nocapture, Value
*Val_nocapture) { ((void)0); OperandTraits<InsertValueInst
>::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned
InsertValueInst::getNumOperands() const { return OperandTraits
<InsertValueInst>::operands(this); } template <int Idx_nocapture
> Use &InsertValueInst::Op() { return this->OpFrom<
Idx_nocapture>(this); } template <int Idx_nocapture>
const Use &InsertValueInst::Op() const { return this->
OpFrom<Idx_nocapture>(this); }
2588
2589//===----------------------------------------------------------------------===//
2590// PHINode Class
2591//===----------------------------------------------------------------------===//
2592
2593// PHINode - The PHINode class is used to represent the magical mystical PHI
2594// node, that can not exist in nature, but can be synthesized in a computer
2595// scientist's overactive imagination.
2596//
2597class PHINode : public Instruction {
2598 /// The number of operands actually allocated. NumOperands is
2599 /// the number actually in use.
2600 unsigned ReservedSpace;
2601
2602 PHINode(const PHINode &PN);
2603
2604 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2605 const Twine &NameStr = "",
2606 Instruction *InsertBefore = nullptr)
2607 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2608 ReservedSpace(NumReservedValues) {
2609 assert(!Ty->isTokenTy() && "PHI nodes cannot have token type!")((void)0);
2610 setName(NameStr);
2611 allocHungoffUses(ReservedSpace);
2612 }
2613
2614 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2615 BasicBlock *InsertAtEnd)
2616 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2617 ReservedSpace(NumReservedValues) {
2618 assert(!Ty->isTokenTy() && "PHI nodes cannot have token type!")((void)0);
2619 setName(NameStr);
2620 allocHungoffUses(ReservedSpace);
2621 }
2622
2623protected:
2624 // Note: Instruction needs to be a friend here to call cloneImpl.
2625 friend class Instruction;
2626
2627 PHINode *cloneImpl() const;
2628
2629 // allocHungoffUses - this is more complicated than the generic
2630 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2631 // values and pointers to the incoming blocks, all in one allocation.
2632 void allocHungoffUses(unsigned N) {
2633 User::allocHungoffUses(N, /* IsPhi */ true);
2634 }
2635
2636public:
2637 /// Constructors - NumReservedValues is a hint for the number of incoming
2638 /// edges that this phi node will have (use 0 if you really have no idea).
2639 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2640 const Twine &NameStr = "",
2641 Instruction *InsertBefore = nullptr) {
2642 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2643 }
2644
2645 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2646 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2647 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2648 }
2649
2650 /// Provide fast operand accessors
2651 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
;
2652
2653 // Block iterator interface. This provides access to the list of incoming
2654 // basic blocks, which parallels the list of incoming values.
2655
2656 using block_iterator = BasicBlock **;
2657 using const_block_iterator = BasicBlock * const *;
2658
2659 block_iterator block_begin() {
2660 return reinterpret_cast<block_iterator>(op_begin() + ReservedSpace);
2661 }
2662
2663 const_block_iterator block_begin() const {
2664 return reinterpret_cast<const_block_iterator>(op_begin() + ReservedSpace);
2665 }
2666
2667 block_iterator block_end() {
2668 return block_begin() + getNumOperands();
2669 }
2670
2671 const_block_iterator block_end() const {
2672 return block_begin() + getNumOperands();
2673 }
2674
2675 iterator_range<block_iterator> blocks() {
2676 return make_range(block_begin(), block_end());
2677 }
2678
2679 iterator_range<const_block_iterator> blocks() const {
2680 return make_range(block_begin(), block_end());
2681 }
2682
2683 op_range incoming_values() { return operands(); }
2684
2685 const_op_range incoming_values() const { return operands(); }
2686
2687 /// Return the number of incoming edges
2688 ///
2689 unsigned getNumIncomingValues() const { return getNumOperands(); }
2690
2691 /// Return incoming value number x
2692 ///
2693 Value *getIncomingValue(unsigned i) const {
2694 return getOperand(i);
2695 }
2696 void setIncomingValue(unsigned i, Value *V) {
2697 assert(V && "PHI node got a null value!")((void)0);
2698 assert(getType() == V->getType() &&((void)0)
2699 "All operands to PHI node must be the same type as the PHI node!")((void)0);
2700 setOperand(i, V);
2701 }
2702
2703 static unsigned getOperandNumForIncomingValue(unsigned i) {
2704 return i;
2705 }
2706
2707 static unsigned getIncomingValueNumForOperand(unsigned i) {
2708 return i;
2709 }
2710
2711 /// Return incoming basic block number @p i.
2712 ///
2713 BasicBlock *getIncomingBlock(unsigned i) const {
2714 return block_begin()[i];
2715 }
2716
2717 /// Return incoming basic block corresponding
2718 /// to an operand of the PHI.
2719 ///
2720 BasicBlock *getIncomingBlock(const Use &U) const {
2721 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?")((void)0);
2722 return getIncomingBlock(unsigned(&U - op_begin()));
2723 }
2724
2725 /// Return incoming basic block corresponding
2726 /// to value use iterator.
2727 ///
2728 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2729 return getIncomingBlock(I.getUse());
2730 }
2731
2732 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2733 assert(BB && "PHI node got a null basic block!")((void)0);
2734 block_begin()[i] = BB;
2735 }
2736
2737 /// Replace every incoming basic block \p Old to basic block \p New.
2738 void replaceIncomingBlockWith(const BasicBlock *Old, BasicBlock *New) {
2739 assert(New && Old && "PHI node got a null basic block!")((void)0);
2740 for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op)
2741 if (getIncomingBlock(Op) == Old)
2742 setIncomingBlock(Op, New);
2743 }
2744
2745 /// Add an incoming value to the end of the PHI list
2746 ///
2747 void addIncoming(Value *V, BasicBlock *BB) {
2748 if (getNumOperands() == ReservedSpace)
2749 growOperands(); // Get more space!
2750 // Initialize some new operands.
2751 setNumHungOffUseOperands(getNumOperands() + 1);
2752 setIncomingValue(getNumOperands() - 1, V);
2753 setIncomingBlock(getNumOperands() - 1, BB);
2754 }
2755
2756 /// Remove an incoming value. This is useful if a
2757 /// predecessor basic block is deleted. The value removed is returned.
2758 ///
2759 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2760 /// is true), the PHI node is destroyed and any uses of it are replaced with
2761 /// dummy values. The only time there should be zero incoming values to a PHI
2762 /// node is when the block is dead, so this strategy is sound.
2763 ///
2764 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2765
2766 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2767 int Idx = getBasicBlockIndex(BB);
2768 assert(Idx >= 0 && "Invalid basic block argument to remove!")((void)0);
2769 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2770 }
2771
2772 /// Return the first index of the specified basic
2773 /// block in the value list for this PHI. Returns -1 if no instance.
2774 ///
2775 int getBasicBlockIndex(const BasicBlock *BB) const {
2776 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2777 if (block_begin()[i] == BB)
2778 return i;
2779 return -1;
2780 }
2781
2782 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2783 int Idx = getBasicBlockIndex(BB);
2784 assert(Idx >= 0 && "Invalid basic block argument!")((void)0);
2785 return getIncomingValue(Idx);
2786 }
2787
2788 /// Set every incoming value(s) for block \p BB to \p V.
2789 void setIncomingValueForBlock(const BasicBlock *BB, Value *V) {
2790 assert(BB && "PHI node got a null basic block!")((void)0);
2791 bool Found = false;
2792 for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op)
2793 if (getIncomingBlock(Op) == BB) {
2794 Found = true;
2795 setIncomingValue(Op, V);
2796 }
2797 (void)Found;
2798 assert(Found && "Invalid basic block argument to set!")((void)0);
2799 }
2800
2801 /// If the specified PHI node always merges together the
2802 /// same value, return the value, otherwise return null.
2803 Value *hasConstantValue() const;
2804
2805 /// Whether the specified PHI node always merges
2806 /// together the same value, assuming undefs are equal to a unique
2807 /// non-undef value.
2808 bool hasConstantOrUndefValue() const;
2809
2810 /// If the PHI node is complete which means all of its parent's predecessors
2811 /// have incoming value in this PHI, return true, otherwise return false.
2812 bool isComplete() const {
2813 return llvm::all_of(predecessors(getParent()),
2814 [this](const BasicBlock *Pred) {
2815 return getBasicBlockIndex(Pred) >= 0;
2816 });
2817 }
2818
2819 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2820 static bool classof(const Instruction *I) {
2821 return I->getOpcode() == Instruction::PHI;
2822 }
2823 static bool classof(const Value *V) {
2824 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2825 }
2826
2827private:
2828 void growOperands();
2829};
2830
2831template <>
2832struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2833};
2834
2835DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)PHINode::op_iterator PHINode::op_begin() { return OperandTraits
<PHINode>::op_begin(this); } PHINode::const_op_iterator
PHINode::op_begin() const { return OperandTraits<PHINode>
::op_begin(const_cast<PHINode*>(this)); } PHINode::op_iterator
PHINode::op_end() { return OperandTraits<PHINode>::op_end
(this); } PHINode::const_op_iterator PHINode::op_end() const {
return OperandTraits<PHINode>::op_end(const_cast<PHINode
*>(this)); } Value *PHINode::getOperand(unsigned i_nocapture
) const { ((void)0); return cast_or_null<Value>( OperandTraits
<PHINode>::op_begin(const_cast<PHINode*>(this))[i_nocapture
].get()); } void PHINode::setOperand(unsigned i_nocapture, Value
*Val_nocapture) { ((void)0); OperandTraits<PHINode>::op_begin
(this)[i_nocapture] = Val_nocapture; } unsigned PHINode::getNumOperands
() const { return OperandTraits<PHINode>::operands(this
); } template <int Idx_nocapture> Use &PHINode::Op(
) { return this->OpFrom<Idx_nocapture>(this); } template
<int Idx_nocapture> const Use &PHINode::Op() const
{ return this->OpFrom<Idx_nocapture>(this); }
2836
2837//===----------------------------------------------------------------------===//
2838// LandingPadInst Class
2839//===----------------------------------------------------------------------===//
2840
2841//===---------------------------------------------------------------------------
2842/// The landingpad instruction holds all of the information
2843/// necessary to generate correct exception handling. The landingpad instruction
2844/// cannot be moved from the top of a landing pad block, which itself is
2845/// accessible only from the 'unwind' edge of an invoke. This uses the
2846/// SubclassData field in Value to store whether or not the landingpad is a
2847/// cleanup.
2848///
2849class LandingPadInst : public Instruction {
2850 using CleanupField = BoolBitfieldElementT<0>;
2851
2852 /// The number of operands actually allocated. NumOperands is
2853 /// the number actually in use.
2854 unsigned ReservedSpace;
2855
2856 LandingPadInst(const LandingPadInst &LP);
2857
2858public:
2859 enum ClauseType { Catch, Filter };
2860
2861private:
2862 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
2863 const Twine &NameStr, Instruction *InsertBefore);
2864 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
2865 const Twine &NameStr, BasicBlock *InsertAtEnd);
2866
2867 // Allocate space for exactly zero operands.
2868 void *operator new(size_t S) { return User::operator new(S); }
2869
2870 void growOperands(unsigned Size);
2871 void init(unsigned NumReservedValues, const Twine &NameStr);
2872
2873protected:
2874 // Note: Instruction needs to be a friend here to call cloneImpl.
2875 friend class Instruction;
2876
2877 LandingPadInst *cloneImpl() const;
2878
2879public:
2880 void operator delete(void *Ptr) { User::operator delete(Ptr); }
2881
2882 /// Constructors - NumReservedClauses is a hint for the number of incoming
2883 /// clauses that this landingpad will have (use 0 if you really have no idea).
2884 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
2885 const Twine &NameStr = "",
2886 Instruction *InsertBefore = nullptr);
2887 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
2888 const Twine &NameStr, BasicBlock *InsertAtEnd);
2889
2890 /// Provide fast operand accessors
2891 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
;
2892
2893 /// Return 'true' if this landingpad instruction is a
2894 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2895 /// doesn't catch the exception.
2896 bool isCleanup() const { return getSubclassData<CleanupField>(); }
2897
2898 /// Indicate that this landingpad instruction is a cleanup.
2899 void setCleanup(bool V) { setSubclassData<CleanupField>(V); }
2900
2901 /// Add a catch or filter clause to the landing pad.
2902 void addClause(Constant *ClauseVal);
2903
2904 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2905 /// determine what type of clause this is.
2906 Constant *getClause(unsigned Idx) const {
2907 return cast<Constant>(getOperandList()[Idx]);
2908 }
2909
2910 /// Return 'true' if the clause and index Idx is a catch clause.
2911 bool isCatch(unsigned Idx) const {
2912 return !isa<ArrayType>(getOperandList()[Idx]->getType());
2913 }
2914
2915 /// Return 'true' if the clause and index Idx is a filter clause.
2916 bool isFilter(unsigned Idx) const {
2917 return isa<ArrayType>(getOperandList()[Idx]->getType());
2918 }
2919
2920 /// Get the number of clauses for this landing pad.
2921 unsigned getNumClauses() const { return getNumOperands(); }
2922
2923 /// Grow the size of the operand list to accommodate the new
2924 /// number of clauses.
2925 void reserveClauses(unsigned Size) { growOperands(Size); }
2926
2927 // Methods for support type inquiry through isa, cast, and dyn_cast:
2928 static bool classof(const Instruction *I) {
2929 return I->getOpcode() == Instruction::LandingPad;
2930 }
2931 static bool classof(const Value *V) {
2932 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2933 }
2934};
2935
2936template <>
2937struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<1> {
2938};
2939
2940DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)LandingPadInst::op_iterator LandingPadInst::op_begin() { return
OperandTraits<LandingPadInst>::op_begin(this); } LandingPadInst
::const_op_iterator LandingPadInst::op_begin() const { return
OperandTraits<LandingPadInst>::op_begin(const_cast<
LandingPadInst*>(this)); } LandingPadInst::op_iterator LandingPadInst
::op_end() { return OperandTraits<LandingPadInst>::op_end
(this); } LandingPadInst::const_op_iterator LandingPadInst::op_end
() const { return OperandTraits<LandingPadInst>::op_end
(const_cast<LandingPadInst*>(this)); } Value *LandingPadInst
::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null
<Value>( OperandTraits<LandingPadInst>::op_begin(
const_cast<LandingPadInst*>(this))[i_nocapture].get());
} void LandingPadInst::setOperand(unsigned i_nocapture, Value
*Val_nocapture) { ((void)0); OperandTraits<LandingPadInst
>::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned
LandingPadInst::getNumOperands() const { return OperandTraits
<LandingPadInst>::operands(this); } template <int Idx_nocapture
> Use &LandingPadInst::Op() { return this->OpFrom<
Idx_nocapture>(this); } template <int Idx_nocapture>
const Use &LandingPadInst::Op() const { return this->
OpFrom<Idx_nocapture>(this); }
2941
2942//===----------------------------------------------------------------------===//
2943// ReturnInst Class
2944//===----------------------------------------------------------------------===//
2945
2946//===---------------------------------------------------------------------------
2947/// Return a value (possibly void), from a function. Execution
2948/// does not continue in this function any longer.
2949///
2950class ReturnInst : public Instruction {
2951 ReturnInst(const ReturnInst &RI);
2952
2953private:
2954 // ReturnInst constructors:
2955 // ReturnInst() - 'ret void' instruction
2956 // ReturnInst( null) - 'ret void' instruction
2957 // ReturnInst(Value* X) - 'ret X' instruction
2958 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2959 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2960 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2961 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2962 //
2963 // NOTE: If the Value* passed is of type void then the constructor behaves as
2964 // if it was passed NULL.
2965 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
2966 Instruction *InsertBefore = nullptr);
2967 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2968 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2969
2970protected:
2971 // Note: Instruction needs to be a friend here to call cloneImpl.
2972 friend class Instruction;
2973
2974 ReturnInst *cloneImpl() const;
2975
2976public:
2977 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
2978 Instruction *InsertBefore = nullptr) {
2979 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2980 }
2981
2982 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2983 BasicBlock *InsertAtEnd) {
2984 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2985 }
2986
2987 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2988 return new(0) ReturnInst(C, InsertAtEnd);
2989 }
2990
2991 /// Provide fast operand accessors
2992 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
;
2993
2994 /// Convenience accessor. Returns null if there is no return value.
2995 Value *getReturnValue() const {
2996 return getNumOperands() != 0 ? getOperand(0) : nullptr;
2997 }
2998
2999 unsigned getNumSuccessors() const { return 0; }
3000
3001 // Methods for support type inquiry through isa, cast, and dyn_cast:
3002 static bool classof(const Instruction *I) {
3003 return (I->getOpcode() == Instruction::Ret);
3004 }
3005 static bool classof(const Value *V) {
3006 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3007 }
3008
3009private:
3010 BasicBlock *getSuccessor(unsigned idx) const {
3011 llvm_unreachable("ReturnInst has no successors!")__builtin_unreachable();
3012 }
3013
3014 void setSuccessor(unsigned idx, BasicBlock *B) {
3015 llvm_unreachable("ReturnInst has no successors!")__builtin_unreachable();
3016 }
3017};
3018
3019template <>
3020struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
3021};
3022
3023DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)ReturnInst::op_iterator ReturnInst::op_begin() { return OperandTraits
<ReturnInst>::op_begin(this); } ReturnInst::const_op_iterator
ReturnInst::op_begin() const { return OperandTraits<ReturnInst
>::op_begin(const_cast<ReturnInst*>(this)); } ReturnInst
::op_iterator ReturnInst::op_end() { return OperandTraits<
ReturnInst>::op_end(this); } ReturnInst::const_op_iterator
ReturnInst::op_end() const { return OperandTraits<ReturnInst
>::op_end(const_cast<ReturnInst*>(this)); } Value *ReturnInst
::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null
<Value>( OperandTraits<ReturnInst>::op_begin(const_cast
<ReturnInst*>(this))[i_nocapture].get()); } void ReturnInst
::setOperand(unsigned i_nocapture, Value *Val_nocapture) { ((
void)0); OperandTraits<ReturnInst>::op_begin(this)[i_nocapture
] = Val_nocapture; } unsigned ReturnInst::getNumOperands() const
{ return OperandTraits<ReturnInst>::operands(this); } template
<int Idx_nocapture> Use &ReturnInst::Op() { return
this->OpFrom<Idx_nocapture>(this); } template <int
Idx_nocapture> const Use &ReturnInst::Op() const { return
this->OpFrom<Idx_nocapture>(this); }
3024
3025//===----------------------------------------------------------------------===//
3026// BranchInst Class
3027//===----------------------------------------------------------------------===//
3028
3029//===---------------------------------------------------------------------------
3030/// Conditional or Unconditional Branch instruction.
3031///
3032class BranchInst : public Instruction {
3033 /// Ops list - Branches are strange. The operands are ordered:
3034 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
3035 /// they don't have to check for cond/uncond branchness. These are mostly
3036 /// accessed relative from op_end().
3037 BranchInst(const BranchInst &BI);
3038 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
3039 // BranchInst(BB *B) - 'br B'
3040 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
3041 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
3042 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
3043 // BranchInst(BB* B, BB *I) - 'br B' insert at end
3044 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
3045 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
3046 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
3047 Instruction *InsertBefore = nullptr);
3048 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
3049 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
3050 BasicBlock *InsertAtEnd);
3051
3052 void AssertOK();
3053
3054protected:
3055 // Note: Instruction needs to be a friend here to call cloneImpl.
3056 friend class Instruction;
3057
3058 BranchInst *cloneImpl() const;
3059
3060public:
3061 /// Iterator type that casts an operand to a basic block.
3062 ///
3063 /// This only makes sense because the successors are stored as adjacent
3064 /// operands for branch instructions.
3065 struct succ_op_iterator
3066 : iterator_adaptor_base<succ_op_iterator, value_op_iterator,
3067 std::random_access_iterator_tag, BasicBlock *,
3068 ptrdiff_t, BasicBlock *, BasicBlock *> {
3069 explicit succ_op_iterator(value_op_iterator I) : iterator_adaptor_base(I) {}
3070
3071 BasicBlock *operator*() const { return cast<BasicBlock>(*I); }
3072 BasicBlock *operator->() const { return operator*(); }
3073 };
3074
3075 /// The const version of `succ_op_iterator`.
3076 struct const_succ_op_iterator
3077 : iterator_adaptor_base<const_succ_op_iterator, const_value_op_iterator,
3078 std::random_access_iterator_tag,
3079 const BasicBlock *, ptrdiff_t, const BasicBlock *,
3080 const BasicBlock *> {
3081 explicit const_succ_op_iterator(const_value_op_iterator I)
3082 : iterator_adaptor_base(I) {}
3083
3084 const BasicBlock *operator*() const { return cast<BasicBlock>(*I); }
3085 const BasicBlock *operator->() const { return operator*(); }
3086 };
3087
3088 static BranchInst *Create(BasicBlock *IfTrue,
3089 Instruction *InsertBefore = nullptr) {
3090 return new(1) BranchInst(IfTrue, InsertBefore);
3091 }
3092
3093 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
3094 Value *Cond, Instruction *InsertBefore = nullptr) {
3095 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
3096 }
3097
3098 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
3099 return new(1) BranchInst(IfTrue, InsertAtEnd);
3100 }
3101
3102 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
3103 Value *Cond, BasicBlock *InsertAtEnd) {
3104 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
3105 }
3106
3107 /// Transparently provide more efficient getOperand methods.
3108 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
;
3109
3110 bool isUnconditional() const { return getNumOperands() == 1; }
3111 bool isConditional() const { return getNumOperands() == 3; }
3112
3113 Value *getCondition() const {
3114 assert(isConditional() && "Cannot get condition of an uncond branch!")((void)0);
3115 return Op<-3>();
3116 }
3117
3118 void setCondition(Value *V) {
3119 assert(isConditional() && "Cannot set condition of unconditional branch!")((void)0);
3120 Op<-3>() = V;
3121 }
3122
3123 unsigned getNumSuccessors() const { return 1+isConditional(); }
3124
3125 BasicBlock *getSuccessor(unsigned i) const {
3126 assert(i < getNumSuccessors() && "Successor # out of range for Branch!")((void)0);
3127 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
3128 }
3129
3130 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3131 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!")((void)0);
3132 *(&Op<-1>() - idx) = NewSucc;
3133 }
3134
3135 /// Swap the successors of this branch instruction.
3136 ///
3137 /// Swaps the successors of the branch instruction. This also swaps any
3138 /// branch weight metadata associated with the instruction so that it
3139 /// continues to map correctly to each operand.
3140 void swapSuccessors();
3141
3142 iterator_range<succ_op_iterator> successors() {
3143 return make_range(
3144 succ_op_iterator(std::next(value_op_begin(), isConditional() ? 1 : 0)),
3145 succ_op_iterator(value_op_end()));
3146 }
3147
3148 iterator_range<const_succ_op_iterator> successors() const {
3149 return make_range(const_succ_op_iterator(
3150 std::next(value_op_begin(), isConditional() ? 1 : 0)),
3151 const_succ_op_iterator(value_op_end()));
3152 }
3153
3154 // Methods for support type inquiry through isa, cast, and dyn_cast:
3155 static bool classof(const Instruction *I) {
3156 return (I->getOpcode() == Instruction::Br);
3157 }
3158 static bool classof(const Value *V) {
3159 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3160 }
3161};
3162
3163template <>
3164struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
3165};
3166
3167DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)BranchInst::op_iterator BranchInst::op_begin() { return OperandTraits
<BranchInst>::op_begin(this); } BranchInst::const_op_iterator
BranchInst::op_begin() const { return OperandTraits<BranchInst
>::op_begin(const_cast<BranchInst*>(this)); } BranchInst
::op_iterator BranchInst::op_end() { return OperandTraits<
BranchInst>::op_end(this); } BranchInst::const_op_iterator
BranchInst::op_end() const { return OperandTraits<BranchInst
>::op_end(const_cast<BranchInst*>(this)); } Value *BranchInst
::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null
<Value>( OperandTraits<BranchInst>::op_begin(const_cast
<BranchInst*>(this))[i_nocapture].get()); } void BranchInst
::setOperand(unsigned i_nocapture, Value *Val_nocapture) { ((
void)0); OperandTraits<BranchInst>::op_begin(this)[i_nocapture
] = Val_nocapture; } unsigned BranchInst::getNumOperands() const
{ return OperandTraits<BranchInst>::operands(this); } template
<int Idx_nocapture> Use &BranchInst::Op() { return
this->OpFrom<Idx_nocapture>(this); } template <int
Idx_nocapture> const Use &BranchInst::Op() const { return
this->OpFrom<Idx_nocapture>(this); }
3168
3169//===----------------------------------------------------------------------===//
3170// SwitchInst Class
3171//===----------------------------------------------------------------------===//
3172
3173//===---------------------------------------------------------------------------
3174/// Multiway switch
3175///
3176class SwitchInst : public Instruction {
3177 unsigned ReservedSpace;
3178
3179 // Operand[0] = Value to switch on
3180 // Operand[1] = Default basic block destination
3181 // Operand[2n ] = Value to match
3182 // Operand[2n+1] = BasicBlock to go to on match
3183 SwitchInst(const SwitchInst &SI);
3184
3185 /// Create a new switch instruction, specifying a value to switch on and a
3186 /// default destination. The number of additional cases can be specified here
3187 /// to make memory allocation more efficient. This constructor can also
3188 /// auto-insert before another instruction.
3189 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3190 Instruction *InsertBefore);
3191
3192 /// Create a new switch instruction, specifying a value to switch on and a
3193 /// default destination. The number of additional cases can be specified here
3194 /// to make memory allocation more efficient. This constructor also
3195 /// auto-inserts at the end of the specified BasicBlock.
3196 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3197 BasicBlock *InsertAtEnd);
3198
3199 // allocate space for exactly zero operands
3200 void *operator new(size_t S) { return User::operator new(S); }
3201
3202 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
3203 void growOperands();
3204
3205protected:
3206 // Note: Instruction needs to be a friend here to call cloneImpl.
3207 friend class Instruction;
3208
3209 SwitchInst *cloneImpl() const;
3210
3211public:
3212 void operator delete(void *Ptr) { User::operator delete(Ptr); }
3213
3214 // -2
3215 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
3216
3217 template <typename CaseHandleT> class CaseIteratorImpl;
3218
3219 /// A handle to a particular switch case. It exposes a convenient interface
3220 /// to both the case value and the successor block.
3221 ///
3222 /// We define this as a template and instantiate it to form both a const and
3223 /// non-const handle.
3224 template <typename SwitchInstT, typename ConstantIntT, typename BasicBlockT>
3225 class CaseHandleImpl {
3226 // Directly befriend both const and non-const iterators.
3227 friend class SwitchInst::CaseIteratorImpl<
3228 CaseHandleImpl<SwitchInstT, ConstantIntT, BasicBlockT>>;
3229
3230 protected:
3231 // Expose the switch type we're parameterized with to the iterator.
3232 using SwitchInstType = SwitchInstT;
3233
3234 SwitchInstT *SI;
3235 ptrdiff_t Index;
3236
3237 CaseHandleImpl() = default;
3238 CaseHandleImpl(SwitchInstT *SI, ptrdiff_t Index) : SI(SI), Index(Index) {}
3239
3240 public:
3241 /// Resolves case value for current case.
3242 ConstantIntT *getCaseValue() const {
3243 assert((unsigned)Index < SI->getNumCases() &&((void)0)
3244 "Index out the number of cases.")((void)0);
3245 return reinterpret_cast<ConstantIntT *>(SI->getOperand(2 + Index * 2));
3246 }
3247
3248 /// Resolves successor for current case.
3249 BasicBlockT *getCaseSuccessor() const {
3250 assert(((unsigned)Index < SI->getNumCases() ||((void)0)
3251 (unsigned)Index == DefaultPseudoIndex) &&((void)0)
3252 "Index out the number of cases.")((void)0);
3253 return SI->getSuccessor(getSuccessorIndex());
3254 }
3255
3256 /// Returns number of current case.
3257 unsigned getCaseIndex() const { return Index; }
3258
3259 /// Returns successor index for current case successor.
3260 unsigned getSuccessorIndex() const {
3261 assert(((unsigned)Index == DefaultPseudoIndex ||((void)0)
3262 (unsigned)Index < SI->getNumCases()) &&((void)0)
3263 "Index out the number of cases.")((void)0);
3264 return (unsigned)Index != DefaultPseudoIndex ? Index + 1 : 0;
3265 }
3266
3267 bool operator==(const CaseHandleImpl &RHS) const {
3268 assert(SI == RHS.SI && "Incompatible operators.")((void)0);
3269 return Index == RHS.Index;
3270 }
3271 };
3272
3273 using ConstCaseHandle =
3274 CaseHandleImpl<const SwitchInst, const ConstantInt, const BasicBlock>;
3275
3276 class CaseHandle
3277 : public CaseHandleImpl<SwitchInst, ConstantInt, BasicBlock> {
3278 friend class SwitchInst::CaseIteratorImpl<CaseHandle>;
3279
3280 public:
3281 CaseHandle(SwitchInst *SI, ptrdiff_t Index) : CaseHandleImpl(SI, Index) {}
3282
3283 /// Sets the new value for current case.
3284 void setValue(ConstantInt *V) {
3285 assert((unsigned)Index < SI->getNumCases() &&((void)0)
3286 "Index out the number of cases.")((void)0);
3287 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
3288 }
3289
3290 /// Sets the new successor for current case.
3291 void setSuccessor(BasicBlock *S) {
3292 SI->setSuccessor(getSuccessorIndex(), S);
3293 }
3294 };
3295
3296 template <typename CaseHandleT>
3297 class CaseIteratorImpl
3298 : public iterator_facade_base<CaseIteratorImpl<CaseHandleT>,
3299 std::random_access_iterator_tag,
3300 CaseHandleT> {
3301 using SwitchInstT = typename CaseHandleT::SwitchInstType;
3302
3303 CaseHandleT Case;
3304
3305 public:
3306 /// Default constructed iterator is in an invalid state until assigned to
3307 /// a case for a particular switch.
3308 CaseIteratorImpl() = default;
3309
3310 /// Initializes case iterator for given SwitchInst and for given
3311 /// case number.
3312 CaseIteratorImpl(SwitchInstT *SI, unsigned CaseNum) : Case(SI, CaseNum) {}
3313
3314 /// Initializes case iterator for given SwitchInst and for given
3315 /// successor index.
3316 static CaseIteratorImpl fromSuccessorIndex(SwitchInstT *SI,
3317 unsigned SuccessorIndex) {
3318 assert(SuccessorIndex < SI->getNumSuccessors() &&((void)0)
3319 "Successor index # out of range!")((void)0);
3320 return SuccessorIndex != 0 ? CaseIteratorImpl(SI, SuccessorIndex - 1)
3321 : CaseIteratorImpl(SI, DefaultPseudoIndex);
3322 }
3323
3324 /// Support converting to the const variant. This will be a no-op for const
3325 /// variant.
3326 operator CaseIteratorImpl<ConstCaseHandle>() const {
3327 return CaseIteratorImpl<ConstCaseHandle>(Case.SI, Case.Index);
3328 }
3329
3330 CaseIteratorImpl &operator+=(ptrdiff_t N) {
3331 // Check index correctness after addition.
3332 // Note: Index == getNumCases() means end().
3333 assert(Case.Index + N >= 0 &&((void)0)
3334 (unsigned)(Case.Index + N) <= Case.SI->getNumCases() &&((void)0)
3335 "Case.Index out the number of cases.")((void)0);
3336 Case.Index += N;
3337 return *this;
3338 }
3339 CaseIteratorImpl &operator-=(ptrdiff_t N) {
3340 // Check index correctness after subtraction.
3341 // Note: Case.Index == getNumCases() means end().
3342 assert(Case.Index - N >= 0 &&((void)0)
3343 (unsigned)(Case.Index - N) <= Case.SI->getNumCases() &&((void)0)
3344 "Case.Index out the number of cases.")((void)0);
3345 Case.Index -= N;
3346 return *this;
3347 }
3348 ptrdiff_t operator-(const CaseIteratorImpl &RHS) const {
3349 assert(Case.SI == RHS.Case.SI && "Incompatible operators.")((void)0);
3350 return Case.Index - RHS.Case.Index;
3351 }
3352 bool operator==(const CaseIteratorImpl &RHS) const {
3353 return Case == RHS.Case;
3354 }
3355 bool operator<(const CaseIteratorImpl &RHS) const {
3356 assert(Case.SI == RHS.Case.SI && "Incompatible operators.")((void)0);
3357 return Case.Index < RHS.Case.Index;
3358 }
3359 CaseHandleT &operator*() { return Case; }
3360 const CaseHandleT &operator*() const { return Case; }
3361 };
3362
3363 using CaseIt = CaseIteratorImpl<CaseHandle>;
3364 using ConstCaseIt = CaseIteratorImpl<ConstCaseHandle>;
3365
3366 static SwitchInst *Create(Value *Value, BasicBlock *Default,
3367 unsigned NumCases,
3368 Instruction *InsertBefore = nullptr) {
3369 return new SwitchInst(Value, Default, NumCases, InsertBefore);
3370 }
3371
3372 static SwitchInst *Create(Value *Value, BasicBlock *Default,
3373 unsigned NumCases, BasicBlock *InsertAtEnd) {
3374 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
3375 }
3376
3377 /// Provide fast operand accessors
3378 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
;
3379
3380 // Accessor Methods for Switch stmt
3381 Value *getCondition() const { return getOperand(0); }
3382 void setCondition(Value *V) { setOperand(0, V); }
3383
3384 BasicBlock *getDefaultDest() const {
3385 return cast<BasicBlock>(getOperand(1));
3386 }
3387
3388 void setDefaultDest(BasicBlock *DefaultCase) {
3389 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
3390 }
3391
3392 /// Return the number of 'cases' in this switch instruction, excluding the
3393 /// default case.
3394 unsigned getNumCases() const {
3395 return getNumOperands()/2 - 1;
3396 }
3397
3398 /// Returns a read/write iterator that points to the first case in the
3399 /// SwitchInst.
3400 CaseIt case_begin() {
3401 return CaseIt(this, 0);
3402 }
3403
3404 /// Returns a read-only iterator that points to the first case in the
3405 /// SwitchInst.
3406 ConstCaseIt case_begin() const {
3407 return ConstCaseIt(this, 0);
3408 }
3409
3410 /// Returns a read/write iterator that points one past the last in the
3411 /// SwitchInst.
3412 CaseIt case_end() {
3413 return CaseIt(this, getNumCases());
3414 }
3415
3416 /// Returns a read-only iterator that points one past the last in the
3417 /// SwitchInst.
3418 ConstCaseIt case_end() const {
3419 return ConstCaseIt(this, getNumCases());
3420 }
3421
3422 /// Iteration adapter for range-for loops.
3423 iterator_range<CaseIt> cases() {
3424 return make_range(case_begin(), case_end());
3425 }
3426
3427 /// Constant iteration adapter for range-for loops.
3428 iterator_range<ConstCaseIt> cases() const {
3429 return make_range(case_begin(), case_end());
3430 }
3431
3432 /// Returns an iterator that points to the default case.
3433 /// Note: this iterator allows to resolve successor only. Attempt
3434 /// to resolve case value causes an assertion.
3435 /// Also note, that increment and decrement also causes an assertion and
3436 /// makes iterator invalid.
3437 CaseIt case_default() {
3438 return CaseIt(this, DefaultPseudoIndex);
3439 }
3440 ConstCaseIt case_default() const {
3441 return ConstCaseIt(this, DefaultPseudoIndex);
3442 }
3443
3444 /// Search all of the case values for the specified constant. If it is
3445 /// explicitly handled, return the case iterator of it, otherwise return
3446 /// default case iterator to indicate that it is handled by the default
3447 /// handler.
3448 CaseIt findCaseValue(const ConstantInt *C) {
3449 CaseIt I = llvm::find_if(
3450 cases(), [C](CaseHandle &Case) { return Case.getCaseValue() == C; });
3451 if (I != case_end())
3452 return I;
3453
3454 return case_default();
3455 }
3456 ConstCaseIt findCaseValue(const ConstantInt *C) const {
3457 ConstCaseIt I = llvm::find_if(cases(), [C](ConstCaseHandle &Case) {
3458 return Case.getCaseValue() == C;
3459 });
3460 if (I != case_end())
3461 return I;
3462
3463 return case_default();
3464 }
3465
3466 /// Finds the unique case value for a given successor. Returns null if the
3467 /// successor is not found, not unique, or is the default case.
3468 ConstantInt *findCaseDest(BasicBlock *BB) {
3469 if (BB == getDefaultDest())
3470 return nullptr;
3471
3472 ConstantInt *CI = nullptr;
3473 for (auto Case : cases()) {
3474 if (Case.getCaseSuccessor() != BB)
3475 continue;
3476
3477 if (CI)
3478 return nullptr; // Multiple cases lead to BB.
3479
3480 CI = Case.getCaseValue();
3481 }
3482
3483 return CI;
3484 }
3485
3486 /// Add an entry to the switch instruction.
3487 /// Note:
3488 /// This action invalidates case_end(). Old case_end() iterator will
3489 /// point to the added case.
3490 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
3491
3492 /// This method removes the specified case and its successor from the switch
3493 /// instruction. Note that this operation may reorder the remaining cases at
3494 /// index idx and above.
3495 /// Note:
3496 /// This action invalidates iterators for all cases following the one removed,
3497 /// including the case_end() iterator. It returns an iterator for the next
3498 /// case.
3499 CaseIt removeCase(CaseIt I);
3500
3501 unsigned getNumSuccessors() const { return getNumOperands()/2; }
3502 BasicBlock *getSuccessor(unsigned idx) const {
3503 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!")((void)0);
3504 return cast<BasicBlock>(getOperand(idx*2+1));
3505 }
3506 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3507 assert(idx < getNumSuccessors() && "Successor # out of range for switch!")((void)0);
3508 setOperand(idx * 2 + 1, NewSucc);
3509 }
3510
3511 // Methods for support type inquiry through isa, cast, and dyn_cast:
3512 static bool classof(const Instruction *I) {
3513 return I->getOpcode() == Instruction::Switch;
3514 }
3515 static bool classof(const Value *V) {
3516 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3517 }
3518};
3519
3520/// A wrapper class to simplify modification of SwitchInst cases along with
3521/// their prof branch_weights metadata.
3522class SwitchInstProfUpdateWrapper {
3523 SwitchInst &SI;
3524 Optional<SmallVector<uint32_t, 8> > Weights = None;
3525 bool Changed = false;
3526
3527protected:
3528 static MDNode *getProfBranchWeightsMD(const SwitchInst &SI);
3529
3530 MDNode *buildProfBranchWeightsMD();
3531
3532 void init();
3533
3534public:
3535 using CaseWeightOpt = Optional<uint32_t>;
3536 SwitchInst *operator->() { return &SI; }
3537 SwitchInst &operator*() { return SI; }
3538 operator SwitchInst *() { return &SI; }
3539
3540 SwitchInstProfUpdateWrapper(SwitchInst &SI) : SI(SI) { init(); }
3541
3542 ~SwitchInstProfUpdateWrapper() {
3543 if (Changed)
3544 SI.setMetadata(LLVMContext::MD_prof, buildProfBranchWeightsMD());
3545 }
3546
3547 /// Delegate the call to the underlying SwitchInst::removeCase() and remove
3548 /// correspondent branch weight.
3549 SwitchInst::CaseIt removeCase(SwitchInst::CaseIt I);
3550
3551 /// Delegate the call to the underlying SwitchInst::addCase() and set the
3552 /// specified branch weight for the added case.
3553 void addCase(ConstantInt *OnVal, BasicBlock *Dest, CaseWeightOpt W);
3554
3555 /// Delegate the call to the underlying SwitchInst::eraseFromParent() and mark
3556 /// this object to not touch the underlying SwitchInst in destructor.
3557 SymbolTableList<Instruction>::iterator eraseFromParent();
3558
3559 void setSuccessorWeight(unsigned idx, CaseWeightOpt W);
3560 CaseWeightOpt getSuccessorWeight(unsigned idx);
3561
3562 static CaseWeightOpt getSuccessorWeight(const SwitchInst &SI, unsigned idx);
3563};
3564
3565template <>
3566struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
3567};
3568
3569DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)SwitchInst::op_iterator SwitchInst::op_begin() { return OperandTraits
<SwitchInst>::op_begin(this); } SwitchInst::const_op_iterator
SwitchInst::op_begin() const { return OperandTraits<SwitchInst
>::op_begin(const_cast<SwitchInst*>(this)); } SwitchInst
::op_iterator SwitchInst::op_end() { return OperandTraits<
SwitchInst>::op_end(this); } SwitchInst::const_op_iterator
SwitchInst::op_end() const { return OperandTraits<SwitchInst
>::op_end(const_cast<SwitchInst*>(this)); } Value *SwitchInst
::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null
<Value>( OperandTraits<SwitchInst>::op_begin(const_cast
<SwitchInst*>(this))[i_nocapture].get()); } void SwitchInst
::setOperand(unsigned i_nocapture, Value *Val_nocapture) { ((
void)0); OperandTraits<SwitchInst>::op_begin(this)[i_nocapture
] = Val_nocapture; } unsigned SwitchInst::getNumOperands() const
{ return OperandTraits<SwitchInst>::operands(this); } template
<int Idx_nocapture> Use &SwitchInst::Op() { return
this->OpFrom<Idx_nocapture>(this); } template <int
Idx_nocapture> const Use &SwitchInst::Op() const { return
this->OpFrom<Idx_nocapture>(this); }
3570
3571//===----------------------------------------------------------------------===//
3572// IndirectBrInst Class
3573//===----------------------------------------------------------------------===//
3574
3575//===---------------------------------------------------------------------------
3576/// Indirect Branch Instruction.
3577///
3578class IndirectBrInst : public Instruction {
3579 unsigned ReservedSpace;
3580
3581 // Operand[0] = Address to jump to
3582 // Operand[n+1] = n-th destination
3583 IndirectBrInst(const IndirectBrInst &IBI);
3584
3585 /// Create a new indirectbr instruction, specifying an
3586 /// Address to jump to. The number of expected destinations can be specified
3587 /// here to make memory allocation more efficient. This constructor can also
3588 /// autoinsert before another instruction.
3589 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
3590
3591 /// Create a new indirectbr instruction, specifying an
3592 /// Address to jump to. The number of expected destinations can be specified
3593 /// here to make memory allocation more efficient. This constructor also
3594 /// autoinserts at the end of the specified BasicBlock.
3595 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
3596
3597 // allocate space for exactly zero operands
3598 void *operator new(size_t S) { return User::operator new(S); }
3599
3600 void init(Value *Address, unsigned NumDests);
3601 void growOperands();
3602
3603protected:
3604 // Note: Instruction needs to be a friend here to call cloneImpl.
3605 friend class Instruction;
3606
3607 IndirectBrInst *cloneImpl() const;
3608
3609public:
3610 void operator delete(void *Ptr) { User::operator delete(Ptr); }
3611
3612 /// Iterator type that casts an operand to a basic block.
3613 ///
3614 /// This only makes sense because the successors are stored as adjacent
3615 /// operands for indirectbr instructions.
3616 struct succ_op_iterator
3617 : iterator_adaptor_base<succ_op_iterator, value_op_iterator,
3618 std::random_access_iterator_tag, BasicBlock *,
3619 ptrdiff_t, BasicBlock *, BasicBlock *> {
3620 explicit succ_op_iterator(value_op_iterator I) : iterator_adaptor_base(I) {}
3621
3622 BasicBlock *operator*() const { return cast<BasicBlock>(*I); }
3623 BasicBlock *operator->() const { return operator*(); }
3624 };
3625
3626 /// The const version of `succ_op_iterator`.
3627 struct const_succ_op_iterator
3628 : iterator_adaptor_base<const_succ_op_iterator, const_value_op_iterator,
3629 std::random_access_iterator_tag,
3630 const BasicBlock *, ptrdiff_t, const BasicBlock *,
3631 const BasicBlock *> {
3632 explicit const_succ_op_iterator(const_value_op_iterator I)
3633 : iterator_adaptor_base(I) {}
3634
3635 const BasicBlock *operator*() const { return cast<BasicBlock>(*I); }
3636 const BasicBlock *operator->() const { return operator*(); }
3637 };
3638
3639 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3640 Instruction *InsertBefore = nullptr) {
3641 return new IndirectBrInst(Address, NumDests, InsertBefore);
3642 }
3643
3644 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3645 BasicBlock *InsertAtEnd) {
3646 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
3647 }
3648
3649 /// Provide fast operand accessors.
3650 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
;
3651
3652 // Accessor Methods for IndirectBrInst instruction.
3653 Value *getAddress() { return getOperand(0); }
3654 const Value *getAddress() const { return getOperand(0); }
3655 void setAddress(Value *V) { setOperand(0, V); }
3656
3657 /// return the number of possible destinations in this
3658 /// indirectbr instruction.
3659 unsigned getNumDestinations() const { return getNumOperands()-1; }
3660
3661 /// Return the specified destination.
3662 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
3663 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
3664
3665 /// Add a destination.
3666 ///
3667 void addDestination(BasicBlock *Dest);
3668
3669 /// This method removes the specified successor from the
3670 /// indirectbr instruction.
3671 void removeDestination(unsigned i);
3672
3673 unsigned getNumSuccessors() const { return getNumOperands()-1; }
3674 BasicBlock *getSuccessor(unsigned i) const {
3675 return cast<BasicBlock>(getOperand(i+1));
3676 }
3677 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
3678 setOperand(i + 1, NewSucc);
3679 }
3680
3681 iterator_range<succ_op_iterator> successors() {
3682 return make_range(succ_op_iterator(std::next(value_op_begin())),
3683 succ_op_iterator(value_op_end()));
3684 }
3685
3686 iterator_range<const_succ_op_iterator> successors() const {
3687 return make_range(const_succ_op_iterator(std::next(value_op_begin())),
3688 const_succ_op_iterator(value_op_end()));
3689 }
3690
3691 // Methods for support type inquiry through isa, cast, and dyn_cast:
3692 static bool classof(const Instruction *I) {
3693 return I->getOpcode() == Instruction::IndirectBr;
3694 }
3695 static bool classof(const Value *V) {
3696 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3697 }
3698};
3699
3700template <>
3701struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
3702};
3703
3704DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)IndirectBrInst::op_iterator IndirectBrInst::op_begin() { return
OperandTraits<IndirectBrInst>::op_begin(this); } IndirectBrInst
::const_op_iterator IndirectBrInst::op_begin() const { return
OperandTraits<IndirectBrInst>::op_begin(const_cast<
IndirectBrInst*>(this)); } IndirectBrInst::op_iterator IndirectBrInst
::op_end() { return OperandTraits<IndirectBrInst>::op_end
(this); } IndirectBrInst::const_op_iterator IndirectBrInst::op_end
() const { return OperandTraits<IndirectBrInst>::op_end
(const_cast<IndirectBrInst*>(this)); } Value *IndirectBrInst
::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null
<Value>( OperandTraits<IndirectBrInst>::op_begin(
const_cast<IndirectBrInst*>(this))[i_nocapture].get());
} void IndirectBrInst::setOperand(unsigned i_nocapture, Value
*Val_nocapture) { ((void)0); OperandTraits<IndirectBrInst
>::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned
IndirectBrInst::getNumOperands() const { return OperandTraits
<IndirectBrInst>::operands(this); } template <int Idx_nocapture
> Use &IndirectBrInst::Op() { return this->OpFrom<
Idx_nocapture>(this); } template <int Idx_nocapture>
const Use &IndirectBrInst::Op() const { return this->
OpFrom<Idx_nocapture>(this); }
3705
3706//===----------------------------------------------------------------------===//
3707// InvokeInst Class
3708//===----------------------------------------------------------------------===//
3709
3710/// Invoke instruction. The SubclassData field is used to hold the
3711/// calling convention of the call.
3712///
3713class InvokeInst : public CallBase {
3714 /// The number of operands for this call beyond the called function,
3715 /// arguments, and operand bundles.
3716 static constexpr int NumExtraOperands = 2;
3717
3718 /// The index from the end of the operand array to the normal destination.
3719 static constexpr int NormalDestOpEndIdx = -3;
3720
3721 /// The index from the end of the operand array to the unwind destination.
3722 static constexpr int UnwindDestOpEndIdx = -2;
3723
3724 InvokeInst(const InvokeInst &BI);
3725
3726 /// Construct an InvokeInst given a range of arguments.
3727 ///
3728 /// Construct an InvokeInst from a range of arguments
3729 inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3730 BasicBlock *IfException, ArrayRef<Value *> Args,
3731 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3732 const Twine &NameStr, Instruction *InsertBefore);
3733
3734 inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3735 BasicBlock *IfException, ArrayRef<Value *> Args,
3736 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3737 const Twine &NameStr, BasicBlock *InsertAtEnd);
3738
3739 void init(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3740 BasicBlock *IfException, ArrayRef<Value *> Args,
3741 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
3742
3743 /// Compute the number of operands to allocate.
3744 static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) {
3745 // We need one operand for the called function, plus our extra operands and
3746 // the input operand counts provided.
3747 return 1 + NumExtraOperands + NumArgs + NumBundleInputs;
3748 }
3749
3750protected:
3751 // Note: Instruction needs to be a friend here to call cloneImpl.
3752 friend class Instruction;
3753
3754 InvokeInst *cloneImpl() const;
3755
3756public:
3757 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3758 BasicBlock *IfException, ArrayRef<Value *> Args,
3759 const Twine &NameStr,
3760 Instruction *InsertBefore = nullptr) {
3761 int NumOperands = ComputeNumOperands(Args.size());
3762 return new (NumOperands)
3763 InvokeInst(Ty, Func, IfNormal, IfException, Args, None, NumOperands,
3764 NameStr, InsertBefore);
3765 }
3766
3767 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3768 BasicBlock *IfException, ArrayRef<Value *> Args,
3769 ArrayRef<OperandBundleDef> Bundles = None,
3770 const Twine &NameStr = "",
3771 Instruction *InsertBefore = nullptr) {
3772 int NumOperands =
3773 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles));
3774 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3775
3776 return new (NumOperands, DescriptorBytes)
3777 InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, NumOperands,
3778 NameStr, InsertBefore);
3779 }
3780
3781 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3782 BasicBlock *IfException, ArrayRef<Value *> Args,
3783 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3784 int NumOperands = ComputeNumOperands(Args.size());
3785 return new (NumOperands)
3786 InvokeInst(Ty, Func, IfNormal, IfException, Args, None, NumOperands,
3787 NameStr, InsertAtEnd);
3788 }
3789
3790 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3791 BasicBlock *IfException, ArrayRef<Value *> Args,
3792 ArrayRef<OperandBundleDef> Bundles,
3793 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3794 int NumOperands =
3795 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles));
3796 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3797
3798 return new (NumOperands, DescriptorBytes)
3799 InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, NumOperands,
3800 NameStr, InsertAtEnd);
3801 }
3802
3803 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
3804 BasicBlock *IfException, ArrayRef<Value *> Args,
3805 const Twine &NameStr,
3806 Instruction *InsertBefore = nullptr) {
3807 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3808 IfException, Args, None, NameStr, InsertBefore);
3809 }
3810
3811 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
3812 BasicBlock *IfException, ArrayRef<Value *> Args,
3813 ArrayRef<OperandBundleDef> Bundles = None,
3814 const Twine &NameStr = "",
3815 Instruction *InsertBefore = nullptr) {
3816 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3817 IfException, Args, Bundles, NameStr, InsertBefore);
3818 }
3819
3820 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
3821 BasicBlock *IfException, ArrayRef<Value *> Args,
3822 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3823 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3824 IfException, Args, NameStr, InsertAtEnd);
3825 }
3826
3827 static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal,
3828 BasicBlock *IfException, ArrayRef<Value *> Args,
3829 ArrayRef<OperandBundleDef> Bundles,
3830 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3831 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3832 IfException, Args, Bundles, NameStr, InsertAtEnd);
3833 }
3834
3835 /// Create a clone of \p II with a different set of operand bundles and
3836 /// insert it before \p InsertPt.
3837 ///
3838 /// The returned invoke instruction is identical to \p II in every way except
3839 /// that the operand bundles for the new instruction are set to the operand
3840 /// bundles in \p Bundles.
3841 static InvokeInst *Create(InvokeInst *II, ArrayRef<OperandBundleDef> Bundles,
3842 Instruction *InsertPt = nullptr);
3843
3844 // get*Dest - Return the destination basic blocks...
3845 BasicBlock *getNormalDest() const {
3846 return cast<BasicBlock>(Op<NormalDestOpEndIdx>());
3847 }
3848 BasicBlock *getUnwindDest() const {
3849 return cast<BasicBlock>(Op<UnwindDestOpEndIdx>());
3850 }
3851 void setNormalDest(BasicBlock *B) {
3852 Op<NormalDestOpEndIdx>() = reinterpret_cast<Value *>(B);
3853 }
3854 void setUnwindDest(BasicBlock *B) {
3855 Op<UnwindDestOpEndIdx>() = reinterpret_cast<Value *>(B);
3856 }
3857
3858 /// Get the landingpad instruction from the landing pad
3859 /// block (the unwind destination).
3860 LandingPadInst *getLandingPadInst() const;
3861
3862 BasicBlock *getSuccessor(unsigned i) const {
3863 assert(i < 2 && "Successor # out of range for invoke!")((void)0);
3864 return i == 0 ? getNormalDest() : getUnwindDest();
3865 }
3866
3867 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
3868 assert(i < 2 && "Successor # out of range for invoke!")((void)0);
3869 if (i == 0)
3870 setNormalDest(NewSucc);
3871 else
3872 setUnwindDest(NewSucc);
3873 }
3874
3875 unsigned getNumSuccessors() const { return 2; }
3876
3877 // Methods for support type inquiry through isa, cast, and dyn_cast:
3878 static bool classof(const Instruction *I) {
3879 return (I->getOpcode() == Instruction::Invoke);
3880 }
3881 static bool classof(const Value *V) {
3882 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3883 }
3884
3885private:
3886 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3887 // method so that subclasses cannot accidentally use it.
3888 template <typename Bitfield>
3889 void setSubclassData(typename Bitfield::Type Value) {
3890 Instruction::setSubclassData<Bitfield>(Value);
3891 }
3892};
3893
3894InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3895 BasicBlock *IfException, ArrayRef<Value *> Args,
3896 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3897 const Twine &NameStr, Instruction *InsertBefore)
3898 : CallBase(Ty->getReturnType(), Instruction::Invoke,
3899 OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands,
3900 InsertBefore) {
3901 init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr);
3902}
3903
3904InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3905 BasicBlock *IfException, ArrayRef<Value *> Args,
3906 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3907 const Twine &NameStr, BasicBlock *InsertAtEnd)
3908 : CallBase(Ty->getReturnType(), Instruction::Invoke,
3909 OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands,
3910 InsertAtEnd) {
3911 init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr);
3912}
3913
3914//===----------------------------------------------------------------------===//
3915// CallBrInst Class
3916//===----------------------------------------------------------------------===//
3917
3918/// CallBr instruction, tracking function calls that may not return control but
3919/// instead transfer it to a third location. The SubclassData field is used to
3920/// hold the calling convention of the call.
3921///
3922class CallBrInst : public CallBase {
3923
3924 unsigned NumIndirectDests;
3925
3926 CallBrInst(const CallBrInst &BI);
3927
3928 /// Construct a CallBrInst given a range of arguments.
3929 ///
3930 /// Construct a CallBrInst from a range of arguments
3931 inline CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest,
3932 ArrayRef<BasicBlock *> IndirectDests,
3933 ArrayRef<Value *> Args,
3934 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3935 const Twine &NameStr, Instruction *InsertBefore);
3936
3937 inline CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest,
3938 ArrayRef<BasicBlock *> IndirectDests,
3939 ArrayRef<Value *> Args,
3940 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3941 const Twine &NameStr, BasicBlock *InsertAtEnd);
3942
3943 void init(FunctionType *FTy, Value *Func, BasicBlock *DefaultDest,
3944 ArrayRef<BasicBlock *> IndirectDests, ArrayRef<Value *> Args,
3945 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
3946
3947 /// Should the Indirect Destinations change, scan + update the Arg list.
3948 void updateArgBlockAddresses(unsigned i, BasicBlock *B);
3949
3950 /// Compute the number of operands to allocate.
3951 static int ComputeNumOperands(int NumArgs, int NumIndirectDests,
3952 int NumBundleInputs = 0) {
3953 // We need one operand for the called function, plus our extra operands and
3954 // the input operand counts provided.
3955 return 2 + NumIndirectDests + NumArgs + NumBundleInputs;
3956 }
3957
3958protected:
3959 // Note: Instruction needs to be a friend here to call cloneImpl.
3960 friend class Instruction;
3961
3962 CallBrInst *cloneImpl() const;
3963
3964public:
3965 static CallBrInst *Create(FunctionType *Ty, Value *Func,
3966 BasicBlock *DefaultDest,
3967 ArrayRef<BasicBlock *> IndirectDests,
3968 ArrayRef<Value *> Args, const Twine &NameStr,
3969 Instruction *InsertBefore = nullptr) {
3970 int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size());
3971 return new (NumOperands)
3972 CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, None,
3973 NumOperands, NameStr, InsertBefore);
3974 }
3975
3976 static CallBrInst *Create(FunctionType *Ty, Value *Func,
3977 BasicBlock *DefaultDest,
3978 ArrayRef<BasicBlock *> IndirectDests,
3979 ArrayRef<Value *> Args,
3980 ArrayRef<OperandBundleDef> Bundles = None,
3981 const Twine &NameStr = "",
3982 Instruction *InsertBefore = nullptr) {
3983 int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size(),
3984 CountBundleInputs(Bundles));
3985 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3986
3987 return new (NumOperands, DescriptorBytes)
3988 CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, Bundles,
3989 NumOperands, NameStr, InsertBefore);
3990 }
3991
3992 static CallBrInst *Create(FunctionType *Ty, Value *Func,
3993 BasicBlock *DefaultDest,
3994 ArrayRef<BasicBlock *> IndirectDests,
3995 ArrayRef<Value *> Args, const Twine &NameStr,
3996 BasicBlock *InsertAtEnd) {
3997 int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size());
3998 return new (NumOperands)
3999 CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, None,
4000 NumOperands, NameStr, InsertAtEnd);
4001 }
4002
4003 static CallBrInst *Create(FunctionType *Ty, Value *Func,
4004 BasicBlock *DefaultDest,
4005 ArrayRef<BasicBlock *> IndirectDests,
4006 ArrayRef<Value *> Args,
4007 ArrayRef<OperandBundleDef> Bundles,
4008 const Twine &NameStr, BasicBlock *InsertAtEnd) {
4009 int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size(),
4010 CountBundleInputs(Bundles));
4011 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
4012
4013 return new (NumOperands, DescriptorBytes)
4014 CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, Bundles,
4015 NumOperands, NameStr, InsertAtEnd);
4016 }
4017
4018 static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest,
4019 ArrayRef<BasicBlock *> IndirectDests,
4020 ArrayRef<Value *> Args, const Twine &NameStr,
4021 Instruction *InsertBefore = nullptr) {
4022 return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest,
4023 IndirectDests, Args, NameStr, InsertBefore);
4024 }
4025
4026 static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest,
4027 ArrayRef<BasicBlock *> IndirectDests,
4028 ArrayRef<Value *> Args,
4029 ArrayRef<OperandBundleDef> Bundles = None,
4030 const Twine &NameStr = "",
4031 Instruction *InsertBefore = nullptr) {
4032 return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest,
4033 IndirectDests, Args, Bundles, NameStr, InsertBefore);
4034 }
4035
4036 static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest,
4037 ArrayRef<BasicBlock *> IndirectDests,
4038 ArrayRef<Value *> Args, const Twine &NameStr,
4039 BasicBlock *InsertAtEnd) {
4040 return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest,
4041 IndirectDests, Args, NameStr, InsertAtEnd);
4042 }
4043
4044 static CallBrInst *Create(FunctionCallee Func,
4045 BasicBlock *DefaultDest,
4046 ArrayRef<BasicBlock *> IndirectDests,
4047 ArrayRef<Value *> Args,
4048 ArrayRef<OperandBundleDef> Bundles,
4049 const Twine &NameStr, BasicBlock *InsertAtEnd) {
4050 return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest,
4051 IndirectDests, Args, Bundles, NameStr, InsertAtEnd);
4052 }
4053
4054 /// Create a clone of \p CBI with a different set of operand bundles and
4055 /// insert it before \p InsertPt.
4056 ///
4057 /// The returned callbr instruction is identical to \p CBI in every way
4058 /// except that the operand bundles for the new instruction are set to the
4059 /// operand bundles in \p Bundles.
4060 static CallBrInst *Create(CallBrInst *CBI,
4061 ArrayRef<OperandBundleDef> Bundles,
4062 Instruction *InsertPt = nullptr);
4063
4064 /// Return the number of callbr indirect dest labels.
4065 ///
4066 unsigned getNumIndirectDests() const { return NumIndirectDests; }
4067
4068 /// getIndirectDestLabel - Return the i-th indirect dest label.
4069 ///
4070 Value *getIndirectDestLabel(unsigned i) const {
4071 assert(i < getNumIndirectDests() && "Out of bounds!")((void)0);
4072 return getOperand(i + getNumArgOperands() + getNumTotalBundleOperands() +
4073 1);
4074 }
4075
4076 Value *getIndirectDestLabelUse(unsigned i) const {
4077 assert(i < getNumIndirectDests() && "Out of bounds!")((void)0);
4078 return getOperandUse(i + getNumArgOperands() + getNumTotalBundleOperands() +
4079 1);
4080 }
4081
4082 // Return the destination basic blocks...
4083 BasicBlock *getDefaultDest() const {
4084 return cast<BasicBlock>(*(&Op<-1>() - getNumIndirectDests() - 1));
4085 }
4086 BasicBlock *getIndirectDest(unsigned i) const {
4087 return cast_or_null<BasicBlock>(*(&Op<-1>() - getNumIndirectDests() + i));
4088 }
4089 SmallVector<BasicBlock *, 16> getIndirectDests() const {
4090 SmallVector<BasicBlock *, 16> IndirectDests;
4091 for (unsigned i = 0, e = getNumIndirectDests(); i < e; ++i)
4092 IndirectDests.push_back(getIndirectDest(i));
4093 return IndirectDests;
4094 }
4095 void setDefaultDest(BasicBlock *B) {
4096 *(&Op<-1>() - getNumIndirectDests() - 1) = reinterpret_cast<Value *>(B);
4097 }
4098 void setIndirectDest(unsigned i, BasicBlock *B) {
4099 updateArgBlockAddresses(i, B);
4100 *(&Op<-1>() - getNumIndirectDests() + i) = reinterpret_cast<Value *>(B);
4101 }
4102
4103 BasicBlock *getSuccessor(unsigned i) const {
4104 assert(i < getNumSuccessors() + 1 &&((void)0)
4105 "Successor # out of range for callbr!")((void)0);
4106 return i == 0 ? getDefaultDest() : getIndirectDest(i - 1);
4107 }
4108
4109 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
4110 assert(i < getNumIndirectDests() + 1 &&((void)0)
4111 "Successor # out of range for callbr!")((void)0);
4112 return i == 0 ? setDefaultDest(NewSucc) : setIndirectDest(i - 1, NewSucc);
4113 }
4114
4115 unsigned getNumSuccessors() const { return getNumIndirectDests() + 1; }
4116
4117 // Methods for support type inquiry through isa, cast, and dyn_cast:
4118 static bool classof(const Instruction *I) {
4119 return (I->getOpcode() == Instruction::CallBr);
4120 }
4121 static bool classof(const Value *V) {
4122 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4123 }
4124
4125private:
4126 // Shadow Instruction::setInstructionSubclassData with a private forwarding
4127 // method so that subclasses cannot accidentally use it.
4128 template <typename Bitfield>
4129 void setSubclassData(typename Bitfield::Type Value) {
4130 Instruction::setSubclassData<Bitfield>(Value);
4131 }
4132};
4133
4134CallBrInst::CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest,
4135 ArrayRef<BasicBlock *> IndirectDests,
4136 ArrayRef<Value *> Args,
4137 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
4138 const Twine &NameStr, Instruction *InsertBefore)
4139 : CallBase(Ty->getReturnType(), Instruction::CallBr,
4140 OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands,
4141 InsertBefore) {
4142 init(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, NameStr);
4143}
4144
4145CallBrInst::CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest,
4146 ArrayRef<BasicBlock *> IndirectDests,
4147 ArrayRef<Value *> Args,
4148 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
4149 const Twine &NameStr, BasicBlock *InsertAtEnd)
4150 : CallBase(Ty->getReturnType(), Instruction::CallBr,
4151 OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands,
4152 InsertAtEnd) {
4153 init(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, NameStr);
4154}
4155
4156//===----------------------------------------------------------------------===//
4157// ResumeInst Class
4158//===----------------------------------------------------------------------===//
4159
4160//===---------------------------------------------------------------------------
4161/// Resume the propagation of an exception.
4162///
4163class ResumeInst : public Instruction {
4164 ResumeInst(const ResumeInst &RI);
4165
4166 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
4167 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
4168
4169protected:
4170 // Note: Instruction needs to be a friend here to call cloneImpl.
4171 friend class Instruction;
4172
4173 ResumeInst *cloneImpl() const;
4174
4175public:
4176 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
4177 return new(1) ResumeInst(Exn, InsertBefore);
4178 }
4179
4180 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
4181 return new(1) ResumeInst(Exn, InsertAtEnd);
4182 }
4183
4184 /// Provide fast operand accessors
4185 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
;
4186
4187 /// Convenience accessor.
4188 Value *getValue() const { return Op<0>(); }
4189
4190 unsigned getNumSuccessors() const { return 0; }
4191
4192 // Methods for support type inquiry through isa, cast, and dyn_cast:
4193 static bool classof(const Instruction *I) {
4194 return I->getOpcode() == Instruction::Resume;
4195 }
4196 static bool classof(const Value *V) {
4197 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4198 }
4199
4200private:
4201 BasicBlock *getSuccessor(unsigned idx) const {
4202 llvm_unreachable("ResumeInst has no successors!")__builtin_unreachable();
4203 }
4204
4205 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
4206 llvm_unreachable("ResumeInst has no successors!")__builtin_unreachable();
4207 }
4208};
4209
4210template <>
4211struct OperandTraits<ResumeInst> :
4212 public FixedNumOperandTraits<ResumeInst, 1> {
4213};
4214
4215DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)ResumeInst::op_iterator ResumeInst::op_begin() { return OperandTraits
<ResumeInst>::op_begin(this); } ResumeInst::const_op_iterator
ResumeInst::op_begin() const { return OperandTraits<ResumeInst
>::op_begin(const_cast<ResumeInst*>(this)); } ResumeInst
::op_iterator ResumeInst::op_end() { return OperandTraits<
ResumeInst>::op_end(this); } ResumeInst::const_op_iterator
ResumeInst::op_end() const { return OperandTraits<ResumeInst
>::op_end(const_cast<ResumeInst*>(this)); } Value *ResumeInst
::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null
<Value>( OperandTraits<ResumeInst>::op_begin(const_cast
<ResumeInst*>(this))[i_nocapture].get()); } void ResumeInst
::setOperand(unsigned i_nocapture, Value *Val_nocapture) { ((
void)0); OperandTraits<ResumeInst>::op_begin(this)[i_nocapture
] = Val_nocapture; } unsigned ResumeInst::getNumOperands() const
{ return OperandTraits<ResumeInst>::operands(this); } template
<int Idx_nocapture> Use &ResumeInst::Op() { return
this->OpFrom<Idx_nocapture>(this); } template <int
Idx_nocapture> const Use &ResumeInst::Op() const { return
this->OpFrom<Idx_nocapture>(this); }
4216
4217//===----------------------------------------------------------------------===//
4218// CatchSwitchInst Class
4219//===----------------------------------------------------------------------===//
4220class CatchSwitchInst : public Instruction {
4221 using UnwindDestField = BoolBitfieldElementT<0>;
4222
4223 /// The number of operands actually allocated. NumOperands is
4224 /// the number actually in use.
4225 unsigned ReservedSpace;
4226
4227 // Operand[0] = Outer scope
4228 // Operand[1] = Unwind block destination
4229 // Operand[n] = BasicBlock to go to on match
4230 CatchSwitchInst(const CatchSwitchInst &CSI);
4231
4232 /// Create a new switch instruction, specifying a
4233 /// default destination. The number of additional handlers can be specified
4234 /// here to make memory allocation more efficient.
4235 /// This constructor can also autoinsert before another instruction.
4236 CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
4237 unsigned NumHandlers, const Twine &NameStr,
4238 Instruction *InsertBefore);
4239
4240 /// Create a new switch instruction, specifying a
4241 /// default destination. The number of additional handlers can be specified
4242 /// here to make memory allocation more efficient.
4243 /// This constructor also autoinserts at the end of the specified BasicBlock.
4244 CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
4245 unsigned NumHandlers, const Twine &NameStr,
4246 BasicBlock *InsertAtEnd);
4247
4248 // allocate space for exactly zero operands
4249 void *operator new(size_t S) { return User::operator new(S); }
4250
4251 void init(Value *ParentPad, BasicBlock *UnwindDest, unsigned NumReserved);
4252 void growOperands(unsigned Size);
4253
4254protected:
4255 // Note: Instruction needs to be a friend here to call cloneImpl.
4256 friend class Instruction;
4257
4258 CatchSwitchInst *cloneImpl() const;
4259
4260public:
4261 void operator delete(void *Ptr) { return User::operator delete(Ptr); }
4262
4263 static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest,
4264 unsigned NumHandlers,
4265 const Twine &NameStr = "",
4266 Instruction *InsertBefore = nullptr) {
4267 return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr,
4268 InsertBefore);
4269 }
4270
4271 static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest,
4272 unsigned NumHandlers, const Twine &NameStr,
4273 BasicBlock *InsertAtEnd) {
4274 return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr,
4275 InsertAtEnd);
4276 }
4277
4278 /// Provide fast operand accessors
4279 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
;
4280
4281 // Accessor Methods for CatchSwitch stmt
4282 Value *getParentPad() const { return getOperand(0); }
4283 void setParentPad(Value *ParentPad) { setOperand(0, ParentPad); }
4284
4285 // Accessor Methods for CatchSwitch stmt
4286 bool hasUnwindDest() const { return getSubclassData<UnwindDestField>(); }
4287 bool unwindsToCaller() const { return !hasUnwindDest(); }
4288 BasicBlock *getUnwindDest() const {
4289 if (hasUnwindDest())
4290 return cast<BasicBlock>(getOperand(1));
4291 return nullptr;
4292 }
4293 void setUnwindDest(BasicBlock *UnwindDest) {
4294 assert(UnwindDest)((void)0);
4295 assert(hasUnwindDest())((void)0);
4296 setOperand(1, UnwindDest);
4297 }
4298
4299 /// return the number of 'handlers' in this catchswitch
4300 /// instruction, except the default handler
4301 unsigned getNumHandlers() const {
4302 if (hasUnwindDest())
4303 return getNumOperands() - 2;
4304 return getNumOperands() - 1;
4305 }
4306
4307private:
4308 static BasicBlock *handler_helper(Value *V) { return cast<BasicBlock>(V); }
4309 static const BasicBlock *handler_helper(const Value *V) {
4310 return cast<BasicBlock>(V);
4311 }
4312
4313public:
4314 using DerefFnTy = BasicBlock *(*)(Value *);
4315 using handler_iterator = mapped_iterator<op_iterator, DerefFnTy>;
4316 using handler_range = iterator_range<handler_iterator>;
4317 using ConstDerefFnTy = const BasicBlock *(*)(const Value *);
4318 using const_handler_iterator =
4319 mapped_iterator<const_op_iterator, ConstDerefFnTy>;
4320 using const_handler_range = iterator_range<const_handler_iterator>;
4321
4322 /// Returns an iterator that points to the first handler in CatchSwitchInst.
4323 handler_iterator handler_begin() {
4324 op_iterator It = op_begin() + 1;
4325 if (hasUnwindDest())
4326 ++It;
4327 return handler_iterator(It, DerefFnTy(handler_helper));
4328 }
4329
4330 /// Returns an iterator that points to the first handler in the
4331 /// CatchSwitchInst.
4332 const_handler_iterator handler_begin() const {
4333 const_op_iterator It = op_begin() + 1;
4334 if (hasUnwindDest())
4335 ++It;
4336 return const_handler_iterator(It, ConstDerefFnTy(handler_helper));
4337 }
4338
4339 /// Returns a read-only iterator that points one past the last
4340 /// handler in the CatchSwitchInst.
4341 handler_iterator handler_end() {
4342 return handler_iterator(op_end(), DerefFnTy(handler_helper));
4343 }
4344
4345 /// Returns an iterator that points one past the last handler in the
4346 /// CatchSwitchInst.
4347 const_handler_iterator handler_end() const {
4348 return const_handler_iterator(op_end(), ConstDerefFnTy(handler_helper));
4349 }
4350
4351 /// iteration adapter for range-for loops.
4352 handler_range handlers() {
4353 return make_range(handler_begin(), handler_end());
4354 }
4355
4356 /// iteration adapter for range-for loops.
4357 const_handler_range handlers() const {
4358 return make_range(handler_begin(), handler_end());
4359 }
4360
4361 /// Add an entry to the switch instruction...
4362 /// Note:
4363 /// This action invalidates handler_end(). Old handler_end() iterator will
4364 /// point to the added handler.
4365 void addHandler(BasicBlock *Dest);
4366
4367 void removeHandler(handler_iterator HI);
4368
4369 unsigned getNumSuccessors() const { return getNumOperands() - 1; }
4370 BasicBlock *getSuccessor(unsigned Idx) const {
4371 assert(Idx < getNumSuccessors() &&((void)0)
4372 "Successor # out of range for catchswitch!")((void)0);
4373 return cast<BasicBlock>(getOperand(Idx + 1));
4374 }
4375 void setSuccessor(unsigned Idx, BasicBlock *NewSucc) {
4376 assert(Idx < getNumSuccessors() &&((void)0)
4377 "Successor # out of range for catchswitch!")((void)0);
4378 setOperand(Idx + 1, NewSucc);
4379 }
4380
4381 // Methods for support type inquiry through isa, cast, and dyn_cast:
4382 static bool classof(const Instruction *I) {
4383 return I->getOpcode() == Instruction::CatchSwitch;
4384 }
4385 static bool classof(const Value *V) {
4386 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4387 }
4388};
4389
4390template <>
4391struct OperandTraits<CatchSwitchInst> : public HungoffOperandTraits<2> {};
4392
4393DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchSwitchInst, Value)CatchSwitchInst::op_iterator CatchSwitchInst::op_begin() { return
OperandTraits<CatchSwitchInst>::op_begin(this); } CatchSwitchInst
::const_op_iterator CatchSwitchInst::op_begin() const { return
OperandTraits<CatchSwitchInst>::op_begin(const_cast<
CatchSwitchInst*>(this)); } CatchSwitchInst::op_iterator CatchSwitchInst
::op_end() { return OperandTraits<CatchSwitchInst>::op_end
(this); } CatchSwitchInst::const_op_iterator CatchSwitchInst::
op_end() const { return OperandTraits<CatchSwitchInst>::
op_end(const_cast<CatchSwitchInst*>(this)); } Value *CatchSwitchInst
::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null
<Value>( OperandTraits<CatchSwitchInst>::op_begin
(const_cast<CatchSwitchInst*>(this))[i_nocapture].get()
); } void CatchSwitchInst::setOperand(unsigned i_nocapture, Value
*Val_nocapture) { ((void)0); OperandTraits<CatchSwitchInst
>::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned
CatchSwitchInst::getNumOperands() const { return OperandTraits
<CatchSwitchInst>::operands(this); } template <int Idx_nocapture
> Use &CatchSwitchInst::Op() { return this->OpFrom<
Idx_nocapture>(this); } template <int Idx_nocapture>
const Use &CatchSwitchInst::Op() const { return this->
OpFrom<Idx_nocapture>(this); }
4394
4395//===----------------------------------------------------------------------===//
4396// CleanupPadInst Class
4397//===----------------------------------------------------------------------===//
4398class CleanupPadInst : public FuncletPadInst {
4399private:
4400 explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args,
4401 unsigned Values, const Twine &NameStr,
4402 Instruction *InsertBefore)
4403 : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values,
4404 NameStr, InsertBefore) {}
4405 explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args,
4406 unsigned Values, const Twine &NameStr,
4407 BasicBlock *InsertAtEnd)
4408 : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values,
4409 NameStr, InsertAtEnd) {}
4410
4411public:
4412 static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args = None,
4413 const Twine &NameStr = "",
4414 Instruction *InsertBefore = nullptr) {
4415 unsigned Values = 1 + Args.size();
4416 return new (Values)
4417 CleanupPadInst(ParentPad, Args, Values, NameStr, InsertBefore);
4418 }
4419
4420 static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args,
4421 const Twine &NameStr, BasicBlock *InsertAtEnd) {
4422 unsigned Values = 1 + Args.size();
4423 return new (Values)
4424 CleanupPadInst(ParentPad, Args, Values, NameStr, InsertAtEnd);
4425 }
4426
4427 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4428 static bool classof(const Instruction *I) {
4429 return I->getOpcode() == Instruction::CleanupPad;
4430 }
4431 static bool classof(const Value *V) {
4432 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4433 }
4434};
4435
4436//===----------------------------------------------------------------------===//
4437// CatchPadInst Class
4438//===----------------------------------------------------------------------===//
4439class CatchPadInst : public FuncletPadInst {
4440private:
4441 explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args,
4442 unsigned Values, const Twine &NameStr,
4443 Instruction *InsertBefore)
4444 : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values,
4445 NameStr, InsertBefore) {}
4446 explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args,
4447 unsigned Values, const Twine &NameStr,
4448 BasicBlock *InsertAtEnd)
4449 : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values,
4450 NameStr, InsertAtEnd) {}
4451
4452public:
4453 static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args,
4454 const Twine &NameStr = "",
4455 Instruction *InsertBefore = nullptr) {
4456 unsigned Values = 1 + Args.size();
4457 return new (Values)
4458 CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertBefore);
4459 }
4460
4461 static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args,
4462 const Twine &NameStr, BasicBlock *InsertAtEnd) {
4463 unsigned Values = 1 + Args.size();
4464 return new (Values)
4465 CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertAtEnd);
4466 }
4467
4468 /// Convenience accessors
4469 CatchSwitchInst *getCatchSwitch() const {
4470 return cast<CatchSwitchInst>(Op<-1>());
4471 }
4472 void setCatchSwitch(Value *CatchSwitch) {
4473 assert(CatchSwitch)((void)0);
4474 Op<-1>() = CatchSwitch;
4475 }
4476
4477 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4478 static bool classof(const Instruction *I) {
4479 return I->getOpcode() == Instruction::CatchPad;
4480 }
4481 static bool classof(const Value *V) {
4482 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4483 }
4484};
4485
4486//===----------------------------------------------------------------------===//
4487// CatchReturnInst Class
4488//===----------------------------------------------------------------------===//
4489
4490class CatchReturnInst : public Instruction {
4491 CatchReturnInst(const CatchReturnInst &RI);
4492 CatchReturnInst(Value *CatchPad, BasicBlock *BB, Instruction *InsertBefore);
4493 CatchReturnInst(Value *CatchPad, BasicBlock *BB, BasicBlock *InsertAtEnd);
4494
4495 void init(Value *CatchPad, BasicBlock *BB);
4496
4497protected:
4498 // Note: Instruction needs to be a friend here to call cloneImpl.
4499 friend class Instruction;
4500
4501 CatchReturnInst *cloneImpl() const;
4502
4503public:
4504 static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB,
4505 Instruction *InsertBefore = nullptr) {
4506 assert(CatchPad)((void)0);
4507 assert(BB)((void)0);
4508 return new (2) CatchReturnInst(CatchPad, BB, InsertBefore);
4509 }
4510
4511 static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB,
4512 BasicBlock *InsertAtEnd) {
4513 assert(CatchPad)((void)0);
4514 assert(BB)((void)0);
4515 return new (2) CatchReturnInst(CatchPad, BB, InsertAtEnd);
4516 }
4517
4518 /// Provide fast operand accessors
4519 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
;
4520
4521 /// Convenience accessors.
4522 CatchPadInst *getCatchPad() const { return cast<CatchPadInst>(Op<0>()); }
4523 void setCatchPad(CatchPadInst *CatchPad) {
4524 assert(CatchPad)((void)0);
4525 Op<0>() = CatchPad;
4526 }
4527
4528 BasicBlock *getSuccessor() const { return cast<BasicBlock>(Op<1>()); }
4529 void setSuccessor(BasicBlock *NewSucc) {
4530 assert(NewSucc)((void)0);
4531 Op<1>() = NewSucc;
4532 }
4533 unsigned getNumSuccessors() const { return 1; }
4534
4535 /// Get the parentPad of this catchret's catchpad's catchswitch.
4536 /// The successor block is implicitly a member of this funclet.
4537 Value *getCatchSwitchParentPad() const {
4538 return getCatchPad()->getCatchSwitch()->getParentPad();
4539 }
4540
4541 // Methods for support type inquiry through isa, cast, and dyn_cast:
4542 static bool classof(const Instruction *I) {
4543 return (I->getOpcode() == Instruction::CatchRet);
4544 }
4545 static bool classof(const Value *V) {
4546 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4547 }
4548
4549private:
4550 BasicBlock *getSuccessor(unsigned Idx) const {
4551 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!")((void)0);
4552 return getSuccessor();
4553 }
4554
4555 void setSuccessor(unsigned Idx, BasicBlock *B) {
4556 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!")((void)0);
4557 setSuccessor(B);
4558 }
4559};
4560
4561template <>
4562struct OperandTraits<CatchReturnInst>
4563 : public FixedNumOperandTraits<CatchReturnInst, 2> {};
4564
4565DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchReturnInst, Value)CatchReturnInst::op_iterator CatchReturnInst::op_begin() { return
OperandTraits<CatchReturnInst>::op_begin(this); } CatchReturnInst
::const_op_iterator CatchReturnInst::op_begin() const { return
OperandTraits<CatchReturnInst>::op_begin(const_cast<
CatchReturnInst*>(this)); } CatchReturnInst::op_iterator CatchReturnInst
::op_end() { return OperandTraits<CatchReturnInst>::op_end
(this); } CatchReturnInst::const_op_iterator CatchReturnInst::
op_end() const { return OperandTraits<CatchReturnInst>::
op_end(const_cast<CatchReturnInst*>(this)); } Value *CatchReturnInst
::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null
<Value>( OperandTraits<CatchReturnInst>::op_begin
(const_cast<CatchReturnInst*>(this))[i_nocapture].get()
); } void CatchReturnInst::setOperand(unsigned i_nocapture, Value
*Val_nocapture) { ((void)0); OperandTraits<CatchReturnInst
>::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned
CatchReturnInst::getNumOperands() const { return OperandTraits
<CatchReturnInst>::operands(this); } template <int Idx_nocapture
> Use &CatchReturnInst::Op() { return this->OpFrom<
Idx_nocapture>(this); } template <int Idx_nocapture>
const Use &CatchReturnInst::Op() const { return this->
OpFrom<Idx_nocapture>(this); }
4566
4567//===----------------------------------------------------------------------===//
4568// CleanupReturnInst Class
4569//===----------------------------------------------------------------------===//
4570
4571class CleanupReturnInst : public Instruction {
4572 using UnwindDestField = BoolBitfieldElementT<0>;
4573
4574private:
4575 CleanupReturnInst(const CleanupReturnInst &RI);
4576 CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values,
4577 Instruction *InsertBefore = nullptr);
4578 CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values,
4579 BasicBlock *InsertAtEnd);
4580
4581 void init(Value *CleanupPad, BasicBlock *UnwindBB);
4582
4583protected:
4584 // Note: Instruction needs to be a friend here to call cloneImpl.
4585 friend class Instruction;
4586
4587 CleanupReturnInst *cloneImpl() const;
4588
4589public:
4590 static CleanupReturnInst *Create(Value *CleanupPad,
4591 BasicBlock *UnwindBB = nullptr,
4592 Instruction *InsertBefore = nullptr) {
4593 assert(CleanupPad)((void)0);
4594 unsigned Values = 1;
4595 if (UnwindBB)
4596 ++Values;
4597 return new (Values)
4598 CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertBefore);
4599 }
4600
4601 static CleanupReturnInst *Create(Value *CleanupPad, BasicBlock *UnwindBB,
4602 BasicBlock *InsertAtEnd) {
4603 assert(CleanupPad)((void)0);
4604 unsigned Values = 1;
4605 if (UnwindBB)
4606 ++Values;
4607 return new (Values)
4608 CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertAtEnd);
4609 }
4610
4611 /// Provide fast operand accessors
4612 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
;
4613
4614 bool hasUnwindDest() const { return getSubclassData<UnwindDestField>(); }
4615 bool unwindsToCaller() const { return !hasUnwindDest(); }
4616
4617 /// Convenience accessor.
4618 CleanupPadInst *getCleanupPad() const {
4619 return cast<CleanupPadInst>(Op<0>());
4620 }
4621 void setCleanupPad(CleanupPadInst *CleanupPad) {
4622 assert(CleanupPad)((void)0);
4623 Op<0>() = CleanupPad;
4624 }
4625
4626 unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; }
4627
4628 BasicBlock *getUnwindDest() const {
4629 return hasUnwindDest() ? cast<BasicBlock>(Op<1>()) : nullptr;
4630 }
4631 void setUnwindDest(BasicBlock *NewDest) {
4632 assert(NewDest)((void)0);
4633 assert(hasUnwindDest())((void)0);
4634 Op<1>() = NewDest;
4635 }
4636
4637 // Methods for support type inquiry through isa, cast, and dyn_cast:
4638 static bool classof(const Instruction *I) {
4639 return (I->getOpcode() == Instruction::CleanupRet);
4640 }
4641 static bool classof(const Value *V) {
4642 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4643 }
4644
4645private:
4646 BasicBlock *getSuccessor(unsigned Idx) const {
4647 assert(Idx == 0)((void)0);
4648 return getUnwindDest();
4649 }
4650
4651 void setSuccessor(unsigned Idx, BasicBlock *B) {
4652 assert(Idx == 0)((void)0);
4653 setUnwindDest(B);
4654 }
4655
4656 // Shadow Instruction::setInstructionSubclassData with a private forwarding
4657 // method so that subclasses cannot accidentally use it.
4658 template <typename Bitfield>
4659 void setSubclassData(typename Bitfield::Type Value) {
4660 Instruction::setSubclassData<Bitfield>(Value);
4661 }
4662};
4663
4664template <>
4665struct OperandTraits<CleanupReturnInst>
4666 : public VariadicOperandTraits<CleanupReturnInst, /*MINARITY=*/1> {};
4667
4668DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CleanupReturnInst, Value)CleanupReturnInst::op_iterator CleanupReturnInst::op_begin() {
return OperandTraits<CleanupReturnInst>::op_begin(this
); } CleanupReturnInst::const_op_iterator CleanupReturnInst::
op_begin() const { return OperandTraits<CleanupReturnInst>
::op_begin(const_cast<CleanupReturnInst*>(this)); } CleanupReturnInst
::op_iterator CleanupReturnInst::op_end() { return OperandTraits
<CleanupReturnInst>::op_end(this); } CleanupReturnInst::
const_op_iterator CleanupReturnInst::op_end() const { return OperandTraits
<CleanupReturnInst>::op_end(const_cast<CleanupReturnInst
*>(this)); } Value *CleanupReturnInst::getOperand(unsigned
i_nocapture) const { ((void)0); return cast_or_null<Value
>( OperandTraits<CleanupReturnInst>::op_begin(const_cast
<CleanupReturnInst*>(this))[i_nocapture].get()); } void
CleanupReturnInst::setOperand(unsigned i_nocapture, Value *Val_nocapture
) { ((void)0); OperandTraits<CleanupReturnInst>::op_begin
(this)[i_nocapture] = Val_nocapture; } unsigned CleanupReturnInst
::getNumOperands() const { return OperandTraits<CleanupReturnInst
>::operands(this); } template <int Idx_nocapture> Use
&CleanupReturnInst::Op() { return this->OpFrom<Idx_nocapture
>(this); } template <int Idx_nocapture> const Use &
CleanupReturnInst::Op() const { return this->OpFrom<Idx_nocapture
>(this); }
4669
4670//===----------------------------------------------------------------------===//
4671// UnreachableInst Class
4672//===----------------------------------------------------------------------===//
4673
4674//===---------------------------------------------------------------------------
4675/// This function has undefined behavior. In particular, the
4676/// presence of this instruction indicates some higher level knowledge that the
4677/// end of the block cannot be reached.
4678///
4679class UnreachableInst : public Instruction {
4680protected:
4681 // Note: Instruction needs to be a friend here to call cloneImpl.
4682 friend class Instruction;
4683
4684 UnreachableInst *cloneImpl() const;
4685
4686public:
4687 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
4688 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
4689
4690 // allocate space for exactly zero operands
4691 void *operator new(size_t S) { return User::operator new(S, 0); }
4692 void operator delete(void *Ptr) { User::operator delete(Ptr); }
4693
4694 unsigned getNumSuccessors() const { return 0; }
4695
4696 // Methods for support type inquiry through isa, cast, and dyn_cast:
4697 static bool classof(const Instruction *I) {
4698 return I->getOpcode() == Instruction::Unreachable;
4699 }
4700 static bool classof(const Value *V) {
4701 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4702 }
4703
4704private:
4705 BasicBlock *getSuccessor(unsigned idx) const {
4706 llvm_unreachable("UnreachableInst has no successors!")__builtin_unreachable();
4707 }
4708
4709 void setSuccessor(unsigned idx, BasicBlock *B) {
4710 llvm_unreachable("UnreachableInst has no successors!")__builtin_unreachable();
4711 }
4712};
4713
4714//===----------------------------------------------------------------------===//
4715// TruncInst Class
4716//===----------------------------------------------------------------------===//
4717
4718/// This class represents a truncation of integer types.
4719class TruncInst : public CastInst {
4720protected:
4721 // Note: Instruction needs to be a friend here to call cloneImpl.
4722 friend class Instruction;
4723
4724 /// Clone an identical TruncInst
4725 TruncInst *cloneImpl() const;
4726
4727public:
4728 /// Constructor with insert-before-instruction semantics
4729 TruncInst(
4730 Value *S, ///< The value to be truncated
4731 Type *Ty, ///< The (smaller) type to truncate to
4732 const Twine &NameStr = "", ///< A name for the new instruction
4733 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4734 );
4735
4736 /// Constructor with insert-at-end-of-block semantics
4737 TruncInst(
4738 Value *S, ///< The value to be truncated
4739 Type *Ty, ///< The (smaller) type to truncate to
4740 const Twine &NameStr, ///< A name for the new instruction
4741 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4742 );
4743
4744 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4745 static bool classof(const Instruction *I) {
4746 return I->getOpcode() == Trunc;
4747 }
4748 static bool classof(const Value *V) {
4749 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4750 }
4751};
4752
4753//===----------------------------------------------------------------------===//
4754// ZExtInst Class
4755//===----------------------------------------------------------------------===//
4756
4757/// This class represents zero extension of integer types.
4758class ZExtInst : public CastInst {
4759protected:
4760 // Note: Instruction needs to be a friend here to call cloneImpl.
4761 friend class Instruction;
4762
4763 /// Clone an identical ZExtInst
4764 ZExtInst *cloneImpl() const;
4765
4766public:
4767 /// Constructor with insert-before-instruction semantics
4768 ZExtInst(
4769 Value *S, ///< The value to be zero extended
4770 Type *Ty, ///< The type to zero extend to
4771 const Twine &NameStr = "", ///< A name for the new instruction
4772 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4773 );
4774
4775 /// Constructor with insert-at-end semantics.
4776 ZExtInst(
4777 Value *S, ///< The value to be zero extended
4778 Type *Ty, ///< The type to zero extend to
4779 const Twine &NameStr, ///< A name for the new instruction
4780 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4781 );
4782
4783 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4784 static bool classof(const Instruction *I) {
4785 return I->getOpcode() == ZExt;
4786 }
4787 static bool classof(const Value *V) {
4788 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4789 }
4790};
4791
4792//===----------------------------------------------------------------------===//
4793// SExtInst Class
4794//===----------------------------------------------------------------------===//
4795
4796/// This class represents a sign extension of integer types.
4797class SExtInst : public CastInst {
4798protected:
4799 // Note: Instruction needs to be a friend here to call cloneImpl.
4800 friend class Instruction;
4801
4802 /// Clone an identical SExtInst
4803 SExtInst *cloneImpl() const;
4804
4805public:
4806 /// Constructor with insert-before-instruction semantics
4807 SExtInst(
4808 Value *S, ///< The value to be sign extended
4809 Type *Ty, ///< The type to sign extend to
4810 const Twine &NameStr = "", ///< A name for the new instruction
4811 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4812 );
4813
4814 /// Constructor with insert-at-end-of-block semantics
4815 SExtInst(
4816 Value *S, ///< The value to be sign extended
4817 Type *Ty, ///< The type to sign extend to
4818 const Twine &NameStr, ///< A name for the new instruction
4819 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4820 );
4821
4822 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4823 static bool classof(const Instruction *I) {
4824 return I->getOpcode() == SExt;
4825 }
4826 static bool classof(const Value *V) {
4827 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4828 }
4829};
4830
4831//===----------------------------------------------------------------------===//
4832// FPTruncInst Class
4833//===----------------------------------------------------------------------===//
4834
4835/// This class represents a truncation of floating point types.
4836class FPTruncInst : public CastInst {
4837protected:
4838 // Note: Instruction needs to be a friend here to call cloneImpl.
4839 friend class Instruction;
4840
4841 /// Clone an identical FPTruncInst
4842 FPTruncInst *cloneImpl() const;
4843
4844public:
4845 /// Constructor with insert-before-instruction semantics
4846 FPTruncInst(
4847 Value *S, ///< The value to be truncated
4848 Type *Ty, ///< The type to truncate to
4849 const Twine &NameStr = "", ///< A name for the new instruction
4850 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4851 );
4852
4853 /// Constructor with insert-before-instruction semantics
4854 FPTruncInst(
4855 Value *S, ///< The value to be truncated
4856 Type *Ty, ///< The type to truncate to
4857 const Twine &NameStr, ///< A name for the new instruction
4858 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4859 );
4860
4861 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4862 static bool classof(const Instruction *I) {
4863 return I->getOpcode() == FPTrunc;
4864 }
4865 static bool classof(const Value *V) {
4866 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4867 }
4868};
4869
4870//===----------------------------------------------------------------------===//
4871// FPExtInst Class
4872//===----------------------------------------------------------------------===//
4873
4874/// This class represents an extension of floating point types.
4875class FPExtInst : public CastInst {
4876protected:
4877 // Note: Instruction needs to be a friend here to call cloneImpl.
4878 friend class Instruction;
4879
4880 /// Clone an identical FPExtInst
4881 FPExtInst *cloneImpl() const;
4882
4883public:
4884 /// Constructor with insert-before-instruction semantics
4885 FPExtInst(
4886 Value *S, ///< The value to be extended
4887 Type *Ty, ///< The type to extend to
4888 const Twine &NameStr = "", ///< A name for the new instruction
4889 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4890 );
4891
4892 /// Constructor with insert-at-end-of-block semantics
4893 FPExtInst(
4894 Value *S, ///< The value to be extended
4895 Type *Ty, ///< The type to extend to
4896 const Twine &NameStr, ///< A name for the new instruction
4897 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4898 );
4899
4900 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4901 static bool classof(const Instruction *I) {
4902 return I->getOpcode() == FPExt;
4903 }
4904 static bool classof(const Value *V) {
4905 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4906 }
4907};
4908
4909//===----------------------------------------------------------------------===//
4910// UIToFPInst Class
4911//===----------------------------------------------------------------------===//
4912
4913/// This class represents a cast unsigned integer to floating point.
4914class UIToFPInst : public CastInst {
4915protected:
4916 // Note: Instruction needs to be a friend here to call cloneImpl.
4917 friend class Instruction;
4918
4919 /// Clone an identical UIToFPInst
4920 UIToFPInst *cloneImpl() const;
4921
4922public:
4923 /// Constructor with insert-before-instruction semantics
4924 UIToFPInst(
4925 Value *S, ///< The value to be converted
4926 Type *Ty, ///< The type to convert to
4927 const Twine &NameStr = "", ///< A name for the new instruction
4928 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4929 );
4930
4931 /// Constructor with insert-at-end-of-block semantics
4932 UIToFPInst(
4933 Value *S, ///< The value to be converted
4934 Type *Ty, ///< The type to convert to
4935 const Twine &NameStr, ///< A name for the new instruction
4936 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4937 );
4938
4939 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4940 static bool classof(const Instruction *I) {
4941 return I->getOpcode() == UIToFP;
4942 }
4943 static bool classof(const Value *V) {
4944 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4945 }
4946};
4947
4948//===----------------------------------------------------------------------===//
4949// SIToFPInst Class
4950//===----------------------------------------------------------------------===//
4951
4952/// This class represents a cast from signed integer to floating point.
4953class SIToFPInst : public CastInst {
4954protected:
4955 // Note: Instruction needs to be a friend here to call cloneImpl.
4956 friend class Instruction;
4957
4958 /// Clone an identical SIToFPInst
4959 SIToFPInst *cloneImpl() const;
4960
4961public:
4962 /// Constructor with insert-before-instruction semantics
4963 SIToFPInst(
4964 Value *S, ///< The value to be converted
4965 Type *Ty, ///< The type to convert to
4966 const Twine &NameStr = "", ///< A name for the new instruction
4967 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4968 );
4969
4970 /// Constructor with insert-at-end-of-block semantics
4971 SIToFPInst(
4972 Value *S, ///< The value to be converted
4973 Type *Ty, ///< The type to convert to
4974 const Twine &NameStr, ///< A name for the new instruction
4975 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4976 );
4977
4978 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4979 static bool classof(const Instruction *I) {
4980 return I->getOpcode() == SIToFP;
4981 }
4982 static bool classof(const Value *V) {
4983 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4984 }
4985};
4986
4987//===----------------------------------------------------------------------===//
4988// FPToUIInst Class
4989//===----------------------------------------------------------------------===//
4990
4991/// This class represents a cast from floating point to unsigned integer
4992class FPToUIInst : public CastInst {
4993protected:
4994 // Note: Instruction needs to be a friend here to call cloneImpl.
4995 friend class Instruction;
4996
4997 /// Clone an identical FPToUIInst
4998 FPToUIInst *cloneImpl() const;
4999
5000public:
5001 /// Constructor with insert-before-instruction semantics
5002 FPToUIInst(
5003 Value *S, ///< The value to be converted
5004 Type *Ty, ///< The type to convert to
5005 const Twine &NameStr = "", ///< A name for the new instruction
5006 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5007 );
5008
5009 /// Constructor with insert-at-end-of-block semantics
5010 FPToUIInst(
5011 Value *S, ///< The value to be converted
5012 Type *Ty, ///< The type to convert to
5013 const Twine &NameStr, ///< A name for the new instruction
5014 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
5015 );
5016
5017 /// Methods for support type inquiry through isa, cast, and dyn_cast:
5018 static bool classof(const Instruction *I) {
5019 return I->getOpcode() == FPToUI;
5020 }
5021 static bool classof(const Value *V) {
5022 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5023 }
5024};
5025
5026//===----------------------------------------------------------------------===//
5027// FPToSIInst Class
5028//===----------------------------------------------------------------------===//
5029
5030/// This class represents a cast from floating point to signed integer.
5031class FPToSIInst : public CastInst {
5032protected:
5033 // Note: Instruction needs to be a friend here to call cloneImpl.
5034 friend class Instruction;
5035
5036 /// Clone an identical FPToSIInst
5037 FPToSIInst *cloneImpl() const;
5038
5039public:
5040 /// Constructor with insert-before-instruction semantics
5041 FPToSIInst(
5042 Value *S, ///< The value to be converted
5043 Type *Ty, ///< The type to convert to
5044 const Twine &NameStr = "", ///< A name for the new instruction
5045 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5046 );
5047
5048 /// Constructor with insert-at-end-of-block semantics
5049 FPToSIInst(
5050 Value *S, ///< The value to be converted
5051 Type *Ty, ///< The type to convert to
5052 const Twine &NameStr, ///< A name for the new instruction
5053 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5054 );
5055
5056 /// Methods for support type inquiry through isa, cast, and dyn_cast:
5057 static bool classof(const Instruction *I) {
5058 return I->getOpcode() == FPToSI;
5059 }
5060 static bool classof(const Value *V) {
5061 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5062 }
5063};
5064
5065//===----------------------------------------------------------------------===//
5066// IntToPtrInst Class
5067//===----------------------------------------------------------------------===//
5068
5069/// This class represents a cast from an integer to a pointer.
5070class IntToPtrInst : public CastInst {
5071public:
5072 // Note: Instruction needs to be a friend here to call cloneImpl.
5073 friend class Instruction;
5074
5075 /// Constructor with insert-before-instruction semantics
5076 IntToPtrInst(
5077 Value *S, ///< The value to be converted
5078 Type *Ty, ///< The type to convert to
5079 const Twine &NameStr = "", ///< A name for the new instruction
5080 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5081 );
5082
5083 /// Constructor with insert-at-end-of-block semantics
5084 IntToPtrInst(
5085 Value *S, ///< The value to be converted
5086 Type *Ty, ///< The type to convert to
5087 const Twine &NameStr, ///< A name for the new instruction
5088 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5089 );
5090
5091 /// Clone an identical IntToPtrInst.
5092 IntToPtrInst *cloneImpl() const;
5093
5094 /// Returns the address space of this instruction's pointer type.
5095 unsigned getAddressSpace() const {
5096 return getType()->getPointerAddressSpace();
5097 }
5098
5099 // Methods for support type inquiry through isa, cast, and dyn_cast:
5100 static bool classof(const Instruction *I) {
5101 return I->getOpcode() == IntToPtr;
5102 }
5103 static bool classof(const Value *V) {
5104 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5105 }
5106};
5107
5108//===----------------------------------------------------------------------===//
5109// PtrToIntInst Class
5110//===----------------------------------------------------------------------===//
5111
5112/// This class represents a cast from a pointer to an integer.
5113class PtrToIntInst : public CastInst {
5114protected:
5115 // Note: Instruction needs to be a friend here to call cloneImpl.
5116 friend class Instruction;
5117
5118 /// Clone an identical PtrToIntInst.
5119 PtrToIntInst *cloneImpl() const;
5120
5121public:
5122 /// Constructor with insert-before-instruction semantics
5123 PtrToIntInst(
5124 Value *S, ///< The value to be converted
5125 Type *Ty, ///< The type to convert to
5126 const Twine &NameStr = "", ///< A name for the new instruction
5127 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5128 );
5129
5130 /// Constructor with insert-at-end-of-block semantics
5131 PtrToIntInst(
5132 Value *S, ///< The value to be converted
5133 Type *Ty, ///< The type to convert to
5134 const Twine &NameStr, ///< A name for the new instruction
5135 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5136 );
5137
5138 /// Gets the pointer operand.
5139 Value *getPointerOperand() { return getOperand(0); }
5140 /// Gets the pointer operand.
5141 const Value *getPointerOperand() const { return getOperand(0); }
5142 /// Gets the operand index of the pointer operand.
5143 static unsigned getPointerOperandIndex() { return 0U; }
5144
5145 /// Returns the address space of the pointer operand.
5146 unsigned getPointerAddressSpace() const {
5147 return getPointerOperand()->getType()->getPointerAddressSpace();
5148 }
5149
5150 // Methods for support type inquiry through isa, cast, and dyn_cast:
5151 static bool classof(const Instruction *I) {
5152 return I->getOpcode() == PtrToInt;
5153 }
5154 static bool classof(const Value *V) {
5155 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5156 }
5157};
5158
5159//===----------------------------------------------------------------------===//
5160// BitCastInst Class
5161//===----------------------------------------------------------------------===//
5162
5163/// This class represents a no-op cast from one type to another.
5164class BitCastInst : public CastInst {
5165protected:
5166 // Note: Instruction needs to be a friend here to call cloneImpl.
5167 friend class Instruction;
5168
5169 /// Clone an identical BitCastInst.
5170 BitCastInst *cloneImpl() const;
5171
5172public:
5173 /// Constructor with insert-before-instruction semantics
5174 BitCastInst(
5175 Value *S, ///< The value to be casted
5176 Type *Ty, ///< The type to casted to
5177 const Twine &NameStr = "", ///< A name for the new instruction
5178 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5179 );
5180
5181 /// Constructor with insert-at-end-of-block semantics
5182 BitCastInst(
5183 Value *S, ///< The value to be casted
5184 Type *Ty, ///< The type to casted to
5185 const Twine &NameStr, ///< A name for the new instruction
5186 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5187 );
5188
5189 // Methods for support type inquiry through isa, cast, and dyn_cast:
5190 static bool classof(const Instruction *I) {
5191 return I->getOpcode() == BitCast;
5192 }
5193 static bool classof(const Value *V) {
5194 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5195 }
5196};
5197
5198//===----------------------------------------------------------------------===//
5199// AddrSpaceCastInst Class
5200//===----------------------------------------------------------------------===//
5201
5202/// This class represents a conversion between pointers from one address space
5203/// to another.
5204class AddrSpaceCastInst : public CastInst {
5205protected:
5206 // Note: Instruction needs to be a friend here to call cloneImpl.
5207 friend class Instruction;
5208
5209 /// Clone an identical AddrSpaceCastInst.
5210 AddrSpaceCastInst *cloneImpl() const;
5211
5212public:
5213 /// Constructor with insert-before-instruction semantics
5214 AddrSpaceCastInst(
5215 Value *S, ///< The value to be casted
5216 Type *Ty, ///< The type to casted to
5217 const Twine &NameStr = "", ///< A name for the new instruction
5218 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
5219 );
5220
5221 /// Constructor with insert-at-end-of-block semantics
5222 AddrSpaceCastInst(
5223 Value *S, ///< The value to be casted
5224 Type *Ty, ///< The type to casted to
5225 const Twine &NameStr, ///< A name for the new instruction
5226 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
5227 );
5228
5229 // Methods for support type inquiry through isa, cast, and dyn_cast:
5230 static bool classof(const Instruction *I) {
5231 return I->getOpcode() == AddrSpaceCast;
5232 }
5233 static bool classof(const Value *V) {
5234 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5235 }
5236
5237 /// Gets the pointer operand.
5238 Value *getPointerOperand() {
5239 return getOperand(0);
5240 }
5241
5242 /// Gets the pointer operand.
5243 const Value *getPointerOperand() const {
5244 return getOperand(0);
5245 }
5246
5247 /// Gets the operand index of the pointer operand.
5248 static unsigned getPointerOperandIndex() {
5249 return 0U;
5250 }
5251
5252 /// Returns the address space of the pointer operand.
5253 unsigned getSrcAddressSpace() const {
5254 return getPointerOperand()->getType()->getPointerAddressSpace();
5255 }
5256
5257 /// Returns the address space of the result.
5258 unsigned getDestAddressSpace() const {
5259 return getType()->getPointerAddressSpace();
5260 }
5261};
5262
5263/// A helper function that returns the pointer operand of a load or store
5264/// instruction. Returns nullptr if not load or store.
5265inline const Value *getLoadStorePointerOperand(const Value *V) {
5266 if (auto *Load = dyn_cast<LoadInst>(V))
5267 return Load->getPointerOperand();
5268 if (auto *Store = dyn_cast<StoreInst>(V))
5269 return Store->getPointerOperand();
5270 return nullptr;
5271}
5272inline Value *getLoadStorePointerOperand(Value *V) {
5273 return const_cast<Value *>(
5274 getLoadStorePointerOperand(static_cast<const Value *>(V)));
5275}
5276
5277/// A helper function that returns the pointer operand of a load, store
5278/// or GEP instruction. Returns nullptr if not load, store, or GEP.
5279inline const Value *getPointerOperand(const Value *V) {
5280 if (auto *Ptr = getLoadStorePointerOperand(V))
5281 return Ptr;
5282 if (auto *Gep = dyn_cast<GetElementPtrInst>(V))
5283 return Gep->getPointerOperand();
5284 return nullptr;
5285}
5286inline Value *getPointerOperand(Value *V) {
5287 return const_cast<Value *>(getPointerOperand(static_cast<const Value *>(V)));
5288}
5289
5290/// A helper function that returns the alignment of load or store instruction.
5291inline Align getLoadStoreAlignment(Value *I) {
5292 assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&((void)0)
5293 "Expected Load or Store instruction")((void)0);
5294 if (auto *LI = dyn_cast<LoadInst>(I))
5295 return LI->getAlign();
5296 return cast<StoreInst>(I)->getAlign();
5297}
5298
5299/// A helper function that returns the address space of the pointer operand of
5300/// load or store instruction.
5301inline unsigned getLoadStoreAddressSpace(Value *I) {
5302 assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&((void)0)
5303 "Expected Load or Store instruction")((void)0);
5304 if (auto *LI = dyn_cast<LoadInst>(I))
5305 return LI->getPointerAddressSpace();
5306 return cast<StoreInst>(I)->getPointerAddressSpace();
5307}
5308
5309/// A helper function that returns the type of a load or store instruction.
5310inline Type *getLoadStoreType(Value *I) {
5311 assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&((void)0)
5312 "Expected Load or Store instruction")((void)0);
5313 if (auto *LI = dyn_cast<LoadInst>(I))
5314 return LI->getType();
5315 return cast<StoreInst>(I)->getValueOperand()->getType();
5316}
5317
5318//===----------------------------------------------------------------------===//
5319// FreezeInst Class
5320//===----------------------------------------------------------------------===//
5321
5322/// This class represents a freeze function that returns random concrete
5323/// value if an operand is either a poison value or an undef value
5324class FreezeInst : public UnaryInstruction {
5325protected:
5326 // Note: Instruction needs to be a friend here to call cloneImpl.
5327 friend class Instruction;
5328
5329 /// Clone an identical FreezeInst
5330 FreezeInst *cloneImpl() const;
5331
5332public:
5333 explicit FreezeInst(Value *S,
5334 const Twine &NameStr = "",
5335 Instruction *InsertBefore = nullptr);
5336 FreezeInst(Value *S, const Twine &NameStr, BasicBlock *InsertAtEnd);
5337
5338 // Methods for support type inquiry through isa, cast, and dyn_cast:
5339 static inline bool classof(const Instruction *I) {
5340 return I->getOpcode() == Freeze;
5341 }
5342 static inline bool classof(const Value *V) {
5343 return isa<Instruction>(V) && classof(cast<Instruction>(V));
5344 }
5345};
5346
5347} // end namespace llvm
5348
5349#endif // LLVM_IR_INSTRUCTIONS_H

/usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/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); }
30
Returning the value 3, which participates in a condition later
116
117 constexpr bool hasOffset() const { return HasOffset; }
118 constexpr int32_t getOffset() const {
119 assert(HasOffset && "No offset!")((void)0);
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));
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);
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,
799 DominatorTree *DT) {
800 AAQueryInfo AAQIP;
801 return callCapturesBefore(I, MemLoc, DT, AAQIP);
802 }
803
804 /// A convenience wrapper to synthesize a memory location.
805 ModRefInfo callCapturesBefore(const Instruction *I, const Value *P,
806 LocationSize Size, DominatorTree *DT) {
807 return callCapturesBefore(I, MemoryLocation(P, Size), DT);
808 }
809
810 /// @}
811 //===--------------------------------------------------------------------===//
812 /// \name Higher level methods for querying mod/ref information.
813 /// @{
814
815 /// Check if it is possible for execution of the specified basic block to
816 /// modify the location Loc.
817 bool canBasicBlockModify(const BasicBlock &BB, const MemoryLocation &Loc);
818
819 /// A convenience wrapper synthesizing a memory location.
820 bool canBasicBlockModify(const BasicBlock &BB, const Value *P,
821 LocationSize Size) {
822 return canBasicBlockModify(BB, MemoryLocation(P, Size));
823 }
824
825 /// Check if it is possible for the execution of the specified instructions
826 /// to mod\ref (according to the mode) the location Loc.
827 ///
828 /// The instructions to consider are all of the instructions in the range of
829 /// [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block.
830 bool canInstructionRangeModRef(const Instruction &I1, const Instruction &I2,
831 const MemoryLocation &Loc,
832 const ModRefInfo Mode);
833
834 /// A convenience wrapper synthesizing a memory location.
835 bool canInstructionRangeModRef(const Instruction &I1, const Instruction &I2,
836 const Value *Ptr, LocationSize Size,
837 const ModRefInfo Mode) {
838 return canInstructionRangeModRef(I1, I2, MemoryLocation(Ptr, Size), Mode);
839 }
840
841private:
842 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
843 AAQueryInfo &AAQI);
844 bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI,
845 bool OrLocal = false);
846 ModRefInfo getModRefInfo(Instruction *I, const CallBase *Call2,
847 AAQueryInfo &AAQIP);
848 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc,
849 AAQueryInfo &AAQI);
850 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2,
851 AAQueryInfo &AAQI);
852 ModRefInfo getModRefInfo(const VAArgInst *V, const MemoryLocation &Loc,
853 AAQueryInfo &AAQI);
854 ModRefInfo getModRefInfo(const LoadInst *L, const MemoryLocation &Loc,
855 AAQueryInfo &AAQI);
856 ModRefInfo getModRefInfo(const StoreInst *S, const MemoryLocation &Loc,
857 AAQueryInfo &AAQI);
858 ModRefInfo getModRefInfo(const FenceInst *S, const MemoryLocation &Loc,
859 AAQueryInfo &AAQI);
860 ModRefInfo getModRefInfo(const AtomicCmpXchgInst *CX,
861 const MemoryLocation &Loc, AAQueryInfo &AAQI);
862 ModRefInfo getModRefInfo(const AtomicRMWInst *RMW, const MemoryLocation &Loc,
863 AAQueryInfo &AAQI);
864 ModRefInfo getModRefInfo(const CatchPadInst *I, const MemoryLocation &Loc,
865 AAQueryInfo &AAQI);
866 ModRefInfo getModRefInfo(const CatchReturnInst *I, const MemoryLocation &Loc,
867 AAQueryInfo &AAQI);
868 ModRefInfo getModRefInfo(const Instruction *I,
869 const Optional<MemoryLocation> &OptLoc,
870 AAQueryInfo &AAQIP);
871 ModRefInfo callCapturesBefore(const Instruction *I,
872 const MemoryLocation &MemLoc, DominatorTree *DT,
873 AAQueryInfo &AAQIP);
874
875 class Concept;
876
877 template <typename T> class Model;
878
879 template <typename T> friend class AAResultBase;
880
881 const TargetLibraryInfo &TLI;
882
883 std::vector<std::unique_ptr<Concept>> AAs;
884
885 std::vector<AnalysisKey *> AADeps;
886
887 friend class BatchAAResults;
888};
889
890/// This class is a wrapper over an AAResults, and it is intended to be used
891/// only when there are no IR changes inbetween queries. BatchAAResults is
892/// reusing the same `AAQueryInfo` to preserve the state across queries,
893/// esentially making AA work in "batch mode". The internal state cannot be
894/// cleared, so to go "out-of-batch-mode", the user must either use AAResults,
895/// or create a new BatchAAResults.
896class BatchAAResults {
897 AAResults &AA;
898 AAQueryInfo AAQI;
899
900public:
901 BatchAAResults(AAResults &AAR) : AA(AAR), AAQI() {}
902 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB) {
903 return AA.alias(LocA, LocB, AAQI);
904 }
905 bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal = false) {
906 return AA.pointsToConstantMemory(Loc, AAQI, OrLocal);
907 }
908 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc) {
909 return AA.getModRefInfo(Call, Loc, AAQI);
910 }
911 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2) {
912 return AA.getModRefInfo(Call1, Call2, AAQI);
913 }
914 ModRefInfo getModRefInfo(const Instruction *I,
915 const Optional<MemoryLocation> &OptLoc) {
916 return AA.getModRefInfo(I, OptLoc, AAQI);
917 }
918 ModRefInfo getModRefInfo(Instruction *I, const CallBase *Call2) {
919 return AA.getModRefInfo(I, Call2, AAQI);
920 }
921 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) {
922 return AA.getArgModRefInfo(Call, ArgIdx);
923 }
924 FunctionModRefBehavior getModRefBehavior(const CallBase *Call) {
925 return AA.getModRefBehavior(Call);
926 }
927 bool isMustAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) {
928 return alias(LocA, LocB) == AliasResult::MustAlias;
929 }
930 bool isMustAlias(const Value *V1, const Value *V2) {
931 return alias(MemoryLocation(V1, LocationSize::precise(1)),
932 MemoryLocation(V2, LocationSize::precise(1))) ==
933 AliasResult::MustAlias;
934 }
935 ModRefInfo callCapturesBefore(const Instruction *I,
936 const MemoryLocation &MemLoc,
937 DominatorTree *DT) {
938 return AA.callCapturesBefore(I, MemLoc, DT, AAQI);
939 }
940};
941
942/// Temporary typedef for legacy code that uses a generic \c AliasAnalysis
943/// pointer or reference.
944using AliasAnalysis = AAResults;
945
946/// A private abstract base class describing the concept of an individual alias
947/// analysis implementation.
948///
949/// This interface is implemented by any \c Model instantiation. It is also the
950/// interface which a type used to instantiate the model must provide.
951///
952/// All of these methods model methods by the same name in the \c
953/// AAResults class. Only differences and specifics to how the
954/// implementations are called are documented here.
955class AAResults::Concept {
956public:
957 virtual ~Concept() = 0;
958
959 /// An update API used internally by the AAResults to provide
960 /// a handle back to the top level aggregation.
961 virtual void setAAResults(AAResults *NewAAR) = 0;
962
963 //===--------------------------------------------------------------------===//
964 /// \name Alias Queries
965 /// @{
966
967 /// The main low level interface to the alias analysis implementation.
968 /// Returns an AliasResult indicating whether the two pointers are aliased to
969 /// each other. This is the interface that must be implemented by specific
970 /// alias analysis implementations.
971 virtual AliasResult alias(const MemoryLocation &LocA,
972 const MemoryLocation &LocB, AAQueryInfo &AAQI) = 0;
973
974 /// Checks whether the given location points to constant memory, or if
975 /// \p OrLocal is true whether it points to a local alloca.
976 virtual bool pointsToConstantMemory(const MemoryLocation &Loc,
977 AAQueryInfo &AAQI, bool OrLocal) = 0;
978
979 /// @}
980 //===--------------------------------------------------------------------===//
981 /// \name Simple mod/ref information
982 /// @{
983
984 /// Get the ModRef info associated with a pointer argument of a callsite. The
985 /// result's bits are set to indicate the allowed aliasing ModRef kinds. Note
986 /// that these bits do not necessarily account for the overall behavior of
987 /// the function, but rather only provide additional per-argument
988 /// information.
989 virtual ModRefInfo getArgModRefInfo(const CallBase *Call,
990 unsigned ArgIdx) = 0;
991
992 /// Return the behavior of the given call site.
993 virtual FunctionModRefBehavior getModRefBehavior(const CallBase *Call) = 0;
994
995 /// Return the behavior when calling the given function.
996 virtual FunctionModRefBehavior getModRefBehavior(const Function *F) = 0;
997
998 /// getModRefInfo (for call sites) - Return information about whether
999 /// a particular call site modifies or reads the specified memory location.
1000 virtual ModRefInfo getModRefInfo(const CallBase *Call,
1001 const MemoryLocation &Loc,
1002 AAQueryInfo &AAQI) = 0;
1003
1004 /// Return information about whether two call sites may refer to the same set
1005 /// of memory locations. See the AA documentation for details:
1006 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
1007 virtual ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2,
1008 AAQueryInfo &AAQI) = 0;
1009
1010 /// @}
1011};
1012
1013/// A private class template which derives from \c Concept and wraps some other
1014/// type.
1015///
1016/// This models the concept by directly forwarding each interface point to the
1017/// wrapped type which must implement a compatible interface. This provides
1018/// a type erased binding.
1019template <typename AAResultT> class AAResults::Model final : public Concept {
1020 AAResultT &Result;
1021
1022public:
1023 explicit Model(AAResultT &Result, AAResults &AAR) : Result(Result) {
1024 Result.setAAResults(&AAR);
1025 }
1026 ~Model() override = default;
1027
1028 void setAAResults(AAResults *NewAAR) override { Result.setAAResults(NewAAR); }
1029
1030 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
1031 AAQueryInfo &AAQI) override {
1032 return Result.alias(LocA, LocB, AAQI);
1033 }
1034
1035 bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI,
1036 bool OrLocal) override {
1037 return Result.pointsToConstantMemory(Loc, AAQI, OrLocal);
1038 }
1039
1040 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) override {
1041 return Result.getArgModRefInfo(Call, ArgIdx);
1042 }
1043
1044 FunctionModRefBehavior getModRefBehavior(const CallBase *Call) override {
1045 return Result.getModRefBehavior(Call);
1046 }
1047
1048 FunctionModRefBehavior getModRefBehavior(const Function *F) override {
1049 return Result.getModRefBehavior(F);
1050 }
1051
1052 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc,
1053 AAQueryInfo &AAQI) override {
1054 return Result.getModRefInfo(Call, Loc, AAQI);
1055 }
1056
1057 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2,
1058 AAQueryInfo &AAQI) override {
1059 return Result.getModRefInfo(Call1, Call2, AAQI);
1060 }
1061};
1062
1063/// A CRTP-driven "mixin" base class to help implement the function alias
1064/// analysis results concept.
1065///
1066/// Because of the nature of many alias analysis implementations, they often
1067/// only implement a subset of the interface. This base class will attempt to
1068/// implement the remaining portions of the interface in terms of simpler forms
1069/// of the interface where possible, and otherwise provide conservatively
1070/// correct fallback implementations.
1071///
1072/// Implementors of an alias analysis should derive from this CRTP, and then
1073/// override specific methods that they wish to customize. There is no need to
1074/// use virtual anywhere, the CRTP base class does static dispatch to the
1075/// derived type passed into it.
1076template <typename DerivedT> class AAResultBase {
1077 // Expose some parts of the interface only to the AAResults::Model
1078 // for wrapping. Specifically, this allows the model to call our
1079 // setAAResults method without exposing it as a fully public API.
1080 friend class AAResults::Model<DerivedT>;
1081
1082 /// A pointer to the AAResults object that this AAResult is
1083 /// aggregated within. May be null if not aggregated.
1084 AAResults *AAR = nullptr;
1085
1086 /// Helper to dispatch calls back through the derived type.
1087 DerivedT &derived() { return static_cast<DerivedT &>(*this); }
1088
1089 /// A setter for the AAResults pointer, which is used to satisfy the
1090 /// AAResults::Model contract.
1091 void setAAResults(AAResults *NewAAR) { AAR = NewAAR; }
1092
1093protected:
1094 /// This proxy class models a common pattern where we delegate to either the
1095 /// top-level \c AAResults aggregation if one is registered, or to the
1096 /// current result if none are registered.
1097 class AAResultsProxy {
1098 AAResults *AAR;
1099 DerivedT &CurrentResult;
1100
1101 public:
1102 AAResultsProxy(AAResults *AAR, DerivedT &CurrentResult)
1103 : AAR(AAR), CurrentResult(CurrentResult) {}
1104
1105 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
1106 AAQueryInfo &AAQI) {
1107 return AAR ? AAR->alias(LocA, LocB, AAQI)
1108 : CurrentResult.alias(LocA, LocB, AAQI);
1109 }
1110
1111 bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI,
1112 bool OrLocal) {
1113 return AAR ? AAR->pointsToConstantMemory(Loc, AAQI, OrLocal)
1114 : CurrentResult.pointsToConstantMemory(Loc, AAQI, OrLocal);
1115 }
1116
1117 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) {
1118 return AAR ? AAR->getArgModRefInfo(Call, ArgIdx)
1119 : CurrentResult.getArgModRefInfo(Call, ArgIdx);
1120 }
1121
1122 FunctionModRefBehavior getModRefBehavior(const CallBase *Call) {
1123 return AAR ? AAR->getModRefBehavior(Call)
1124 : CurrentResult.getModRefBehavior(Call);
1125 }
1126
1127 FunctionModRefBehavior getModRefBehavior(const Function *F) {
1128 return AAR ? AAR->getModRefBehavior(F) : CurrentResult.getModRefBehavior(F);
1129 }
1130
1131 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc,
1132 AAQueryInfo &AAQI) {
1133 return AAR ? AAR->getModRefInfo(Call, Loc, AAQI)
1134 : CurrentResult.getModRefInfo(Call, Loc, AAQI);
1135 }
1136
1137 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2,
1138 AAQueryInfo &AAQI) {
1139 return AAR ? AAR->getModRefInfo(Call1, Call2, AAQI)
1140 : CurrentResult.getModRefInfo(Call1, Call2, AAQI);
1141 }
1142 };
1143
1144 explicit AAResultBase() = default;
1145
1146 // Provide all the copy and move constructors so that derived types aren't
1147 // constrained.
1148 AAResultBase(const AAResultBase &Arg) {}
1149 AAResultBase(AAResultBase &&Arg) {}
1150
1151 /// Get a proxy for the best AA result set to query at this time.
1152 ///
1153 /// When this result is part of a larger aggregation, this will proxy to that
1154 /// aggregation. When this result is used in isolation, it will just delegate
1155 /// back to the derived class's implementation.
1156 ///
1157 /// Note that callers of this need to take considerable care to not cause
1158 /// performance problems when they use this routine, in the case of a large
1159 /// number of alias analyses being aggregated, it can be expensive to walk
1160 /// back across the chain.
1161 AAResultsProxy getBestAAResults() { return AAResultsProxy(AAR, derived()); }
1162
1163public:
1164 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
1165 AAQueryInfo &AAQI) {
1166 return AliasResult::MayAlias;
1167 }
1168
1169 bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI,
1170 bool OrLocal) {
1171 return false;
1172 }
1173
1174 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) {
1175 return ModRefInfo::ModRef;
1176 }
1177
1178 FunctionModRefBehavior getModRefBehavior(const CallBase *Call) {
1179 return FMRB_UnknownModRefBehavior;
1180 }
1181
1182 FunctionModRefBehavior getModRefBehavior(const Function *F) {
1183 return FMRB_UnknownModRefBehavior;
1184 }
1185
1186 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc,
1187 AAQueryInfo &AAQI) {
1188 return ModRefInfo::ModRef;
1189 }
1190
1191 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2,
1192 AAQueryInfo &AAQI) {
1193 return ModRefInfo::ModRef;
1194 }
1195};
1196
1197/// Return true if this pointer is returned by a noalias function.
1198bool isNoAliasCall(const Value *V);
1199
1200/// Return true if this pointer refers to a distinct and identifiable object.
1201/// This returns true for:
1202/// Global Variables and Functions (but not Global Aliases)
1203/// Allocas
1204/// ByVal and NoAlias Arguments
1205/// NoAlias returns (e.g. calls to malloc)
1206///
1207bool isIdentifiedObject(const Value *V);
1208
1209/// Return true if V is umabigously identified at the function-level.
1210/// Different IdentifiedFunctionLocals can't alias.
1211/// Further, an IdentifiedFunctionLocal can not alias with any function
1212/// arguments other than itself, which is not necessarily true for
1213/// IdentifiedObjects.
1214bool isIdentifiedFunctionLocal(const Value *V);
1215
1216/// A manager for alias analyses.
1217///
1218/// This class can have analyses registered with it and when run, it will run
1219/// all of them and aggregate their results into single AA results interface
1220/// that dispatches across all of the alias analysis results available.
1221///
1222/// Note that the order in which analyses are registered is very significant.
1223/// That is the order in which the results will be aggregated and queried.
1224///
1225/// This manager effectively wraps the AnalysisManager for registering alias
1226/// analyses. When you register your alias analysis with this manager, it will
1227/// ensure the analysis itself is registered with its AnalysisManager.
1228///
1229/// The result of this analysis is only invalidated if one of the particular
1230/// aggregated AA results end up being invalidated. This removes the need to
1231/// explicitly preserve the results of `AAManager`. Note that analyses should no
1232/// longer be registered once the `AAManager` is run.
1233class AAManager : public AnalysisInfoMixin<AAManager> {
1234public:
1235 using Result = AAResults;
1236
1237 /// Register a specific AA result.
1238 template <typename AnalysisT> void registerFunctionAnalysis() {
1239 ResultGetters.push_back(&getFunctionAAResultImpl<AnalysisT>);
1240 }
1241
1242 /// Register a specific AA result.
1243 template <typename AnalysisT> void registerModuleAnalysis() {
1244 ResultGetters.push_back(&getModuleAAResultImpl<AnalysisT>);
1245 }
1246
1247 Result run(Function &F, FunctionAnalysisManager &AM);
1248
1249private:
1250 friend AnalysisInfoMixin<AAManager>;
1251
1252 static AnalysisKey Key;
1253
1254 SmallVector<void (*)(Function &F, FunctionAnalysisManager &AM,
1255 AAResults &AAResults),
1256 4> ResultGetters;
1257
1258 template <typename AnalysisT>
1259 static void getFunctionAAResultImpl(Function &F,
1260 FunctionAnalysisManager &AM,
1261 AAResults &AAResults) {
1262 AAResults.addAAResult(AM.template getResult<AnalysisT>(F));
1263 AAResults.addAADependencyID(AnalysisT::ID());
1264 }
1265
1266 template <typename AnalysisT>
1267 static void getModuleAAResultImpl(Function &F, FunctionAnalysisManager &AM,
1268 AAResults &AAResults) {
1269 auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
1270 if (auto *R =
1271 MAMProxy.template getCachedResult<AnalysisT>(*F.getParent())) {
1272 AAResults.addAAResult(*R);
1273 MAMProxy
1274 .template registerOuterAnalysisInvalidation<AnalysisT, AAManager>();
1275 }
1276 }
1277};
1278
1279/// A wrapper pass to provide the legacy pass manager access to a suitably
1280/// prepared AAResults object.
1281class AAResultsWrapperPass : public FunctionPass {
1282 std::unique_ptr<AAResults> AAR;
1283
1284public:
1285 static char ID;
1286
1287 AAResultsWrapperPass();
1288
1289 AAResults &getAAResults() { return *AAR; }
1290 const AAResults &getAAResults() const { return *AAR; }
1291
1292 bool runOnFunction(Function &F) override;
1293
1294 void getAnalysisUsage(AnalysisUsage &AU) const override;
1295};
1296
1297/// A wrapper pass for external alias analyses. This just squirrels away the
1298/// callback used to run any analyses and register their results.
1299struct ExternalAAWrapperPass : ImmutablePass {
1300 using CallbackT = std::function<void(Pass &, Function &, AAResults &)>;
1301
1302 CallbackT CB;
1303
1304 static char ID;
1305
1306 ExternalAAWrapperPass();
1307
1308 explicit ExternalAAWrapperPass(CallbackT CB);
1309
1310 void getAnalysisUsage(AnalysisUsage &AU) const override {
1311 AU.setPreservesAll();
1312 }
1313};
1314
1315FunctionPass *createAAResultsWrapperPass();
1316
1317/// A wrapper pass around a callback which can be used to populate the
1318/// AAResults in the AAResultsWrapperPass from an external AA.
1319///
1320/// The callback provided here will be used each time we prepare an AAResults
1321/// object, and will receive a reference to the function wrapper pass, the
1322/// function, and the AAResults object to populate. This should be used when
1323/// setting up a custom pass pipeline to inject a hook into the AA results.
1324ImmutablePass *createExternalAAWrapperPass(
1325 std::function<void(Pass &, Function &, AAResults &)> Callback);
1326
1327/// A helper for the legacy pass manager to create a \c AAResults
1328/// object populated to the best of our ability for a particular function when
1329/// inside of a \c ModulePass or a \c CallGraphSCCPass.
1330///
1331/// If a \c ModulePass or a \c CallGraphSCCPass calls \p
1332/// createLegacyPMAAResults, it also needs to call \p addUsedAAAnalyses in \p
1333/// getAnalysisUsage.
1334AAResults createLegacyPMAAResults(Pass &P, Function &F, BasicAAResult &BAR);
1335
1336/// A helper for the legacy pass manager to populate \p AU to add uses to make
1337/// sure the analyses required by \p createLegacyPMAAResults are available.
1338void getAAResultsAnalysisUsage(AnalysisUsage &AU);
1339
1340} // end namespace llvm
1341
1342#endif // LLVM_ANALYSIS_ALIASANALYSIS_H

/usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT/SmallBitVector.h

1//===- llvm/ADT/SmallBitVector.h - 'Normally small' bit vectors -*- 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 implements the SmallBitVector class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_ADT_SMALLBITVECTOR_H
14#define LLVM_ADT_SMALLBITVECTOR_H
15
16#include "llvm/ADT/BitVector.h"
17#include "llvm/ADT/iterator_range.h"
18#include "llvm/Support/MathExtras.h"
19#include <algorithm>
20#include <cassert>
21#include <climits>
22#include <cstddef>
23#include <cstdint>
24#include <limits>
25#include <utility>
26
27namespace llvm {
28
29/// This is a 'bitvector' (really, a variable-sized bit array), optimized for
30/// the case when the array is small. It contains one pointer-sized field, which
31/// is directly used as a plain collection of bits when possible, or as a
32/// pointer to a larger heap-allocated array when necessary. This allows normal
33/// "small" cases to be fast without losing generality for large inputs.
34class SmallBitVector {
35 // TODO: In "large" mode, a pointer to a BitVector is used, leading to an
36 // unnecessary level of indirection. It would be more efficient to use a
37 // pointer to memory containing size, allocation size, and the array of bits.
38 uintptr_t X = 1;
39
40 enum {
41 // The number of bits in this class.
42 NumBaseBits = sizeof(uintptr_t) * CHAR_BIT8,
43
44 // One bit is used to discriminate between small and large mode. The
45 // remaining bits are used for the small-mode representation.
46 SmallNumRawBits = NumBaseBits - 1,
47
48 // A few more bits are used to store the size of the bit set in small mode.
49 // Theoretically this is a ceil-log2. These bits are encoded in the most
50 // significant bits of the raw bits.
51 SmallNumSizeBits = (NumBaseBits == 32 ? 5 :
52 NumBaseBits == 64 ? 6 :
53 SmallNumRawBits),
54
55 // The remaining bits are used to store the actual set in small mode.
56 SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits
57 };
58
59 static_assert(NumBaseBits == 64 || NumBaseBits == 32,
60 "Unsupported word size");
61
62public:
63 using size_type = unsigned;
64
65 // Encapsulation of a single bit.
66 class reference {
67 SmallBitVector &TheVector;
68 unsigned BitPos;
69
70 public:
71 reference(SmallBitVector &b, unsigned Idx) : TheVector(b), BitPos(Idx) {}
72
73 reference(const reference&) = default;
74
75 reference& operator=(reference t) {
76 *this = bool(t);
77 return *this;
78 }
79
80 reference& operator=(bool t) {
81 if (t)
82 TheVector.set(BitPos);
83 else
84 TheVector.reset(BitPos);
85 return *this;
86 }
87
88 operator bool() const {
89 return const_cast<const SmallBitVector &>(TheVector).operator[](BitPos);
90 }
91 };
92
93private:
94 BitVector *getPointer() const {
95 assert(!isSmall())((void)0);
96 return reinterpret_cast<BitVector *>(X);
97 }
98
99 void switchToSmall(uintptr_t NewSmallBits, size_t NewSize) {
100 X = 1;
101 setSmallSize(NewSize);
102 setSmallBits(NewSmallBits);
103 }
104
105 void switchToLarge(BitVector *BV) {
106 X = reinterpret_cast<uintptr_t>(BV);
107 assert(!isSmall() && "Tried to use an unaligned pointer")((void)0);
108 }
109
110 // Return all the bits used for the "small" representation; this includes
111 // bits for the size as well as the element bits.
112 uintptr_t getSmallRawBits() const {
113 assert(isSmall())((void)0);
114 return X >> 1;
115 }
116
117 void setSmallRawBits(uintptr_t NewRawBits) {
118 assert(isSmall())((void)0);
119 X = (NewRawBits << 1) | uintptr_t(1);
120 }
121
122 // Return the size.
123 size_t getSmallSize() const { return getSmallRawBits() >> SmallNumDataBits; }
124
125 void setSmallSize(size_t Size) {
126 setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
127 }
128
129 // Return the element bits.
130 uintptr_t getSmallBits() const {
131 return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
132 }
133
134 void setSmallBits(uintptr_t NewBits) {
135 setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
136 (getSmallSize() << SmallNumDataBits));
137 }
138
139public:
140 /// Creates an empty bitvector.
141 SmallBitVector() = default;
142
143 /// Creates a bitvector of specified number of bits. All bits are initialized
144 /// to the specified value.
145 explicit SmallBitVector(unsigned s, bool t = false) {
146 if (s <= SmallNumDataBits)
147 switchToSmall(t ? ~uintptr_t(0) : 0, s);
148 else
149 switchToLarge(new BitVector(s, t));
150 }
151
152 /// SmallBitVector copy ctor.
153 SmallBitVector(const SmallBitVector &RHS) {
154 if (RHS.isSmall())
155 X = RHS.X;
156 else
157 switchToLarge(new BitVector(*RHS.getPointer()));
158 }
159
160 SmallBitVector(SmallBitVector &&RHS) : X(RHS.X) {
161 RHS.X = 1;
162 }
163
164 ~SmallBitVector() {
165 if (!isSmall())
166 delete getPointer();
167 }
168
169 using const_set_bits_iterator = const_set_bits_iterator_impl<SmallBitVector>;
170 using set_iterator = const_set_bits_iterator;
171
172 const_set_bits_iterator set_bits_begin() const {
173 return const_set_bits_iterator(*this);
174 }
175
176 const_set_bits_iterator set_bits_end() const {
177 return const_set_bits_iterator(*this, -1);
178 }
179
180 iterator_range<const_set_bits_iterator> set_bits() const {
181 return make_range(set_bits_begin(), set_bits_end());
182 }
183
184 bool isSmall() const { return X & uintptr_t(1); }
66
Returning the value 1, which participates in a condition later
185
186 /// Tests whether there are no bits in this bitvector.
187 bool empty() const {
188 return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
189 }
190
191 /// Returns the number of bits in this bitvector.
192 size_t size() const {
193 return isSmall() ? getSmallSize() : getPointer()->size();
194 }
195
196 /// Returns the number of bits which are set.
197 size_type count() const {
198 if (isSmall()) {
65
Calling 'SmallBitVector::isSmall'
67
Returning from 'SmallBitVector::isSmall'
68
Taking true branch
199 uintptr_t Bits = getSmallBits();
200 return countPopulation(Bits);
69
Returning value, which participates in a condition later
201 }
202 return getPointer()->count();
203 }
204
205 /// Returns true if any bit is set.
206 bool any() const {
207 if (isSmall())
208 return getSmallBits() != 0;
209 return getPointer()->any();
210 }
211
212 /// Returns true if all bits are set.
213 bool all() const {
214 if (isSmall())
215 return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
216 return getPointer()->all();
217 }
218
219 /// Returns true if none of the bits are set.
220 bool none() const {
221 if (isSmall())
222 return getSmallBits() == 0;
223 return getPointer()->none();
224 }
225
226 /// Returns the index of the first set bit, -1 if none of the bits are set.
227 int find_first() const {
228 if (isSmall()) {
229 uintptr_t Bits = getSmallBits();
230 if (Bits == 0)
231 return -1;
232 return countTrailingZeros(Bits);
233 }
234 return getPointer()->find_first();
235 }
236
237 int find_last() const {
238 if (isSmall()) {
239 uintptr_t Bits = getSmallBits();
240 if (Bits == 0)
241 return -1;
242 return NumBaseBits - countLeadingZeros(Bits) - 1;
243 }
244 return getPointer()->find_last();
245 }
246
247 /// Returns the index of the first unset bit, -1 if all of the bits are set.
248 int find_first_unset() const {
249 if (isSmall()) {
250 if (count() == getSmallSize())
251 return -1;
252
253 uintptr_t Bits = getSmallBits();
254 return countTrailingOnes(Bits);
255 }
256 return getPointer()->find_first_unset();
257 }
258
259 int find_last_unset() const {
260 if (isSmall()) {
261 if (count() == getSmallSize())
262 return -1;
263
264 uintptr_t Bits = getSmallBits();
265 // Set unused bits.
266 Bits |= ~uintptr_t(0) << getSmallSize();
267 return NumBaseBits - countLeadingOnes(Bits) - 1;
268 }
269 return getPointer()->find_last_unset();
270 }
271
272 /// Returns the index of the next set bit following the "Prev" bit.
273 /// Returns -1 if the next set bit is not found.
274 int find_next(unsigned Prev) const {
275 if (isSmall()) {
85
Assuming the condition is false
86
Taking false branch
276 uintptr_t Bits = getSmallBits();
277 // Mask off previous bits.
278 Bits &= ~uintptr_t(0) << (Prev + 1);
279 if (Bits == 0 || Prev + 1 >= getSmallSize())
280 return -1;
281 return countTrailingZeros(Bits);
282 }
283 return getPointer()->find_next(Prev);
87
Calling 'BitVector::find_next'
96
Returning from 'BitVector::find_next'
97
Returning the value -1
284 }
285
286 /// Returns the index of the next unset bit following the "Prev" bit.
287 /// Returns -1 if the next unset bit is not found.
288 int find_next_unset(unsigned Prev) const {
289 if (isSmall()) {
290 uintptr_t Bits = getSmallBits();
291 // Mask in previous bits.
292 Bits |= (uintptr_t(1) << (Prev + 1)) - 1;
293 // Mask in unused bits.
294 Bits |= ~uintptr_t(0) << getSmallSize();
295
296 if (Bits == ~uintptr_t(0) || Prev + 1 >= getSmallSize())
297 return -1;
298 return countTrailingOnes(Bits);
299 }
300 return getPointer()->find_next_unset(Prev);
301 }
302
303 /// find_prev - Returns the index of the first set bit that precedes the
304 /// the bit at \p PriorTo. Returns -1 if all previous bits are unset.
305 int find_prev(unsigned PriorTo) const {
306 if (isSmall()) {
307 if (PriorTo == 0)
308 return -1;
309
310 --PriorTo;
311 uintptr_t Bits = getSmallBits();
312 Bits &= maskTrailingOnes<uintptr_t>(PriorTo + 1);
313 if (Bits == 0)
314 return -1;
315
316 return NumBaseBits - countLeadingZeros(Bits) - 1;
317 }
318 return getPointer()->find_prev(PriorTo);
319 }
320
321 /// Clear all bits.
322 void clear() {
323 if (!isSmall())
324 delete getPointer();
325 switchToSmall(0, 0);
326 }
327
328 /// Grow or shrink the bitvector.
329 void resize(unsigned N, bool t = false) {
330 if (!isSmall()) {
331 getPointer()->resize(N, t);
332 } else if (SmallNumDataBits >= N) {
333 uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
334 setSmallSize(N);
335 setSmallBits(NewBits | getSmallBits());
336 } else {
337 BitVector *BV = new BitVector(N, t);
338 uintptr_t OldBits = getSmallBits();
339 for (size_t i = 0, e = getSmallSize(); i != e; ++i)
340 (*BV)[i] = (OldBits >> i) & 1;
341 switchToLarge(BV);
342 }
343 }
344
345 void reserve(unsigned N) {
346 if (isSmall()) {
347 if (N > SmallNumDataBits) {
348 uintptr_t OldBits = getSmallRawBits();
349 size_t SmallSize = getSmallSize();
350 BitVector *BV = new BitVector(SmallSize);
351 for (size_t i = 0; i < SmallSize; ++i)
352 if ((OldBits >> i) & 1)
353 BV->set(i);
354 BV->reserve(N);
355 switchToLarge(BV);
356 }
357 } else {
358 getPointer()->reserve(N);
359 }
360 }
361
362 // Set, reset, flip
363 SmallBitVector &set() {
364 if (isSmall())
365 setSmallBits(~uintptr_t(0));
366 else
367 getPointer()->set();
368 return *this;
369 }
370
371 SmallBitVector &set(unsigned Idx) {
372 if (isSmall()) {
111
Assuming the condition is true
112
Taking true branch
373 assert(Idx <= static_cast<unsigned>(((void)0)
374 std::numeric_limits<uintptr_t>::digits) &&((void)0)
375 "undefined behavior")((void)0);
376 setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
113
The result of the left shift is undefined due to shifting by '4294967295', which is greater or equal to the width of type 'uintptr_t'
377 }
378 else
379 getPointer()->set(Idx);
380 return *this;
381 }
382
383 /// Efficiently set a range of bits in [I, E)
384 SmallBitVector &set(unsigned I, unsigned E) {
385 assert(I <= E && "Attempted to set backwards range!")((void)0);
386 assert(E <= size() && "Attempted to set out-of-bounds range!")((void)0);
387 if (I == E) return *this;
388 if (isSmall()) {
389 uintptr_t EMask = ((uintptr_t)1) << E;
390 uintptr_t IMask = ((uintptr_t)1) << I;
391 uintptr_t Mask = EMask - IMask;
392 setSmallBits(getSmallBits() | Mask);
393 } else
394 getPointer()->set(I, E);
395 return *this;
396 }
397
398 SmallBitVector &reset() {
399 if (isSmall())
400 setSmallBits(0);
401 else
402 getPointer()->reset();
403 return *this;
404 }
405
406 SmallBitVector &reset(unsigned Idx) {
407 if (isSmall())
408 setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
409 else
410 getPointer()->reset(Idx);
411 return *this;
412 }
413
414 /// Efficiently reset a range of bits in [I, E)
415 SmallBitVector &reset(unsigned I, unsigned E) {
416 assert(I <= E && "Attempted to reset backwards range!")((void)0);
417 assert(E <= size() && "Attempted to reset out-of-bounds range!")((void)0);
418 if (I == E) return *this;
419 if (isSmall()) {
420 uintptr_t EMask = ((uintptr_t)1) << E;
421 uintptr_t IMask = ((uintptr_t)1) << I;
422 uintptr_t Mask = EMask - IMask;
423 setSmallBits(getSmallBits() & ~Mask);
424 } else
425 getPointer()->reset(I, E);
426 return *this;
427 }
428
429 SmallBitVector &flip() {
430 if (isSmall())
431 setSmallBits(~getSmallBits());
432 else
433 getPointer()->flip();
434 return *this;
435 }
436
437 SmallBitVector &flip(unsigned Idx) {
438 if (isSmall())
439 setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
440 else
441 getPointer()->flip(Idx);
442 return *this;
443 }
444
445 // No argument flip.
446 SmallBitVector operator~() const {
447 return SmallBitVector(*this).flip();
448 }
449
450 // Indexing.
451 reference operator[](unsigned Idx) {
452 assert(Idx < size() && "Out-of-bounds Bit access.")((void)0);
453 return reference(*this, Idx);
454 }
455
456 bool operator[](unsigned Idx) const {
457 assert(Idx < size() && "Out-of-bounds Bit access.")((void)0);
458 if (isSmall())
459 return ((getSmallBits() >> Idx) & 1) != 0;
460 return getPointer()->operator[](Idx);
461 }
462
463 bool test(unsigned Idx) const {
464 return (*this)[Idx];
465 }
466
467 // Push single bit to end of vector.
468 void push_back(bool Val) {
469 resize(size() + 1, Val);
470 }
471
472 /// Test if any common bits are set.
473 bool anyCommon(const SmallBitVector &RHS) const {
474 if (isSmall() && RHS.isSmall())
475 return (getSmallBits() & RHS.getSmallBits()) != 0;
476 if (!isSmall() && !RHS.isSmall())
477 return getPointer()->anyCommon(*RHS.getPointer());
478
479 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
480 if (test(i) && RHS.test(i))
481 return true;
482 return false;
483 }
484
485 // Comparison operators.
486 bool operator==(const SmallBitVector &RHS) const {
487 if (size() != RHS.size())
488 return false;
489 if (isSmall() && RHS.isSmall())
490 return getSmallBits() == RHS.getSmallBits();
491 else if (!isSmall() && !RHS.isSmall())
492 return *getPointer() == *RHS.getPointer();
493 else {
494 for (size_t i = 0, e = size(); i != e; ++i) {
495 if ((*this)[i] != RHS[i])
496 return false;
497 }
498 return true;
499 }
500 }
501
502 bool operator!=(const SmallBitVector &RHS) const {
503 return !(*this == RHS);
504 }
505
506 // Intersection, union, disjoint union.
507 // FIXME BitVector::operator&= does not resize the LHS but this does
508 SmallBitVector &operator&=(const SmallBitVector &RHS) {
509 resize(std::max(size(), RHS.size()));
510 if (isSmall() && RHS.isSmall())
511 setSmallBits(getSmallBits() & RHS.getSmallBits());
512 else if (!isSmall() && !RHS.isSmall())
513 getPointer()->operator&=(*RHS.getPointer());
514 else {
515 size_t i, e;
516 for (i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
517 (*this)[i] = test(i) && RHS.test(i);
518 for (e = size(); i != e; ++i)
519 reset(i);
520 }
521 return *this;
522 }
523
524 /// Reset bits that are set in RHS. Same as *this &= ~RHS.
525 SmallBitVector &reset(const SmallBitVector &RHS) {
526 if (isSmall() && RHS.isSmall())
527 setSmallBits(getSmallBits() & ~RHS.getSmallBits());
528 else if (!isSmall() && !RHS.isSmall())
529 getPointer()->reset(*RHS.getPointer());
530 else
531 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
532 if (RHS.test(i))
533 reset(i);
534
535 return *this;
536 }
537
538 /// Check if (This - RHS) is zero. This is the same as reset(RHS) and any().
539 bool test(const SmallBitVector &RHS) const {
540 if (isSmall() && RHS.isSmall())
541 return (getSmallBits() & ~RHS.getSmallBits()) != 0;
542 if (!isSmall() && !RHS.isSmall())
543 return getPointer()->test(*RHS.getPointer());
544
545 unsigned i, e;
546 for (i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
547 if (test(i) && !RHS.test(i))
548 return true;
549
550 for (e = size(); i != e; ++i)
551 if (test(i))
552 return true;
553
554 return false;
555 }
556
557 SmallBitVector &operator|=(const SmallBitVector &RHS) {
558 resize(std::max(size(), RHS.size()));
559 if (isSmall() && RHS.isSmall())
560 setSmallBits(getSmallBits() | RHS.getSmallBits());
561 else if (!isSmall() && !RHS.isSmall())
562 getPointer()->operator|=(*RHS.getPointer());
563 else {
564 for (size_t i = 0, e = RHS.size(); i != e; ++i)
565 (*this)[i] = test(i) || RHS.test(i);
566 }
567 return *this;
568 }
569
570 SmallBitVector &operator^=(const SmallBitVector &RHS) {
571 resize(std::max(size(), RHS.size()));
572 if (isSmall() && RHS.isSmall())
573 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
574 else if (!isSmall() && !RHS.isSmall())
575 getPointer()->operator^=(*RHS.getPointer());
576 else {
577 for (size_t i = 0, e = RHS.size(); i != e; ++i)
578 (*this)[i] = test(i) != RHS.test(i);
579 }
580 return *this;
581 }
582
583 SmallBitVector &operator<<=(unsigned N) {
584 if (isSmall())
585 setSmallBits(getSmallBits() << N);
586 else
587 getPointer()->operator<<=(N);
588 return *this;
589 }
590
591 SmallBitVector &operator>>=(unsigned N) {
592 if (isSmall())
593 setSmallBits(getSmallBits() >> N);
594 else
595 getPointer()->operator>>=(N);
596 return *this;
597 }
598
599 // Assignment operator.
600 const SmallBitVector &operator=(const SmallBitVector &RHS) {
601 if (isSmall()) {
602 if (RHS.isSmall())
603 X = RHS.X;
604 else
605 switchToLarge(new BitVector(*RHS.getPointer()));
606 } else {
607 if (!RHS.isSmall())
608 *getPointer() = *RHS.getPointer();
609 else {
610 delete getPointer();
611 X = RHS.X;
612 }
613 }
614 return *this;
615 }
616
617 const SmallBitVector &operator=(SmallBitVector &&RHS) {
618 if (this != &RHS) {
619 clear();
620 swap(RHS);
621 }
622 return *this;
623 }
624
625 void swap(SmallBitVector &RHS) {
626 std::swap(X, RHS.X);
627 }
628
629 /// Add '1' bits from Mask to this vector. Don't resize.
630 /// This computes "*this |= Mask".
631 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
632 if (isSmall())
633 applyMask<true, false>(Mask, MaskWords);
634 else
635 getPointer()->setBitsInMask(Mask, MaskWords);
636 }
637
638 /// Clear any bits in this vector that are set in Mask. Don't resize.
639 /// This computes "*this &= ~Mask".
640 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
641 if (isSmall())
642 applyMask<false, false>(Mask, MaskWords);
643 else
644 getPointer()->clearBitsInMask(Mask, MaskWords);
645 }
646
647 /// Add a bit to this vector for every '0' bit in Mask. Don't resize.
648 /// This computes "*this |= ~Mask".
649 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
650 if (isSmall())
651 applyMask<true, true>(Mask, MaskWords);
652 else
653 getPointer()->setBitsNotInMask(Mask, MaskWords);
654 }
655
656 /// Clear a bit in this vector for every '0' bit in Mask. Don't resize.
657 /// This computes "*this &= Mask".
658 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
659 if (isSmall())
660 applyMask<false, true>(Mask, MaskWords);
661 else
662 getPointer()->clearBitsNotInMask(Mask, MaskWords);
663 }
664
665 void invalid() {
666 assert(empty())((void)0);
667 X = (uintptr_t)-1;
668 }
669 bool isInvalid() const { return X == (uintptr_t)-1; }
670
671 ArrayRef<uintptr_t> getData(uintptr_t &Store) const {
672 if (!isSmall())
673 return getPointer()->getData();
674 Store = getSmallBits();
675 return makeArrayRef(Store);
676 }
677
678private:
679 template <bool AddBits, bool InvertMask>
680 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
681 assert(MaskWords <= sizeof(uintptr_t) && "Mask is larger than base!")((void)0);
682 uintptr_t M = Mask[0];
683 if (NumBaseBits == 64)
684 M |= uint64_t(Mask[1]) << 32;
685 if (InvertMask)
686 M = ~M;
687 if (AddBits)
688 setSmallBits(getSmallBits() | M);
689 else
690 setSmallBits(getSmallBits() & ~M);
691 }
692};
693
694inline SmallBitVector
695operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
696 SmallBitVector Result(LHS);
697 Result &= RHS;
698 return Result;
699}
700
701inline SmallBitVector
702operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
703 SmallBitVector Result(LHS);
704 Result |= RHS;
705 return Result;
706}
707
708inline SmallBitVector
709operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
710 SmallBitVector Result(LHS);
711 Result ^= RHS;
712 return Result;
713}
714
715template <> struct DenseMapInfo<SmallBitVector> {
716 static inline SmallBitVector getEmptyKey() { return SmallBitVector(); }
717 static inline SmallBitVector getTombstoneKey() {
718 SmallBitVector V;
719 V.invalid();
720 return V;
721 }
722 static unsigned getHashValue(const SmallBitVector &V) {
723 uintptr_t Store;
724 return DenseMapInfo<std::pair<unsigned, ArrayRef<uintptr_t>>>::getHashValue(
725 std::make_pair(V.size(), V.getData(Store)));
726 }
727 static bool isEqual(const SmallBitVector &LHS, const SmallBitVector &RHS) {
728 if (LHS.isInvalid() || RHS.isInvalid())
729 return LHS.isInvalid() == RHS.isInvalid();
730 return LHS == RHS;
731 }
732};
733} // end namespace llvm
734
735namespace std {
736
737/// Implement std::swap in terms of BitVector swap.
738inline void
739swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {
740 LHS.swap(RHS);
741}
742
743} // end namespace std
744
745#endif // LLVM_ADT_SMALLBITVECTOR_H

/usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT/BitVector.h

1//===- llvm/ADT/BitVector.h - Bit vectors -----------------------*- 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 implements the BitVector class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_ADT_BITVECTOR_H
14#define LLVM_ADT_BITVECTOR_H
15
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/DenseMapInfo.h"
18#include "llvm/ADT/iterator_range.h"
19#include "llvm/Support/MathExtras.h"
20#include <algorithm>
21#include <cassert>
22#include <climits>
23#include <cstdint>
24#include <cstdlib>
25#include <cstring>
26#include <utility>
27
28namespace llvm {
29
30/// ForwardIterator for the bits that are set.
31/// Iterators get invalidated when resize / reserve is called.
32template <typename BitVectorT> class const_set_bits_iterator_impl {
33 const BitVectorT &Parent;
34 int Current = 0;
35
36 void advance() {
37 assert(Current != -1 && "Trying to advance past end.")((void)0);
38 Current = Parent.find_next(Current);
84
Calling 'SmallBitVector::find_next'
98
Returning from 'SmallBitVector::find_next'
99
The value -1 is assigned to '__begin3.Current'
39 }
40
41public:
42 const_set_bits_iterator_impl(const BitVectorT &Parent, int Current)
43 : Parent(Parent), Current(Current) {}
44 explicit const_set_bits_iterator_impl(const BitVectorT &Parent)
45 : const_set_bits_iterator_impl(Parent, Parent.find_first()) {}
46 const_set_bits_iterator_impl(const const_set_bits_iterator_impl &) = default;
47
48 const_set_bits_iterator_impl operator++(int) {
49 auto Prev = *this;
50 advance();
51 return Prev;
52 }
53
54 const_set_bits_iterator_impl &operator++() {
55 advance();
83
Calling 'const_set_bits_iterator_impl::advance'
100
Returning from 'const_set_bits_iterator_impl::advance'
56 return *this;
57 }
58
59 unsigned operator*() const { return Current; }
103
Returning the value 4294967295
60
61 bool operator==(const const_set_bits_iterator_impl &Other) const {
62 assert(&Parent == &Other.Parent &&((void)0)
63 "Comparing iterators from different BitVectors")((void)0);
64 return Current == Other.Current;
65 }
66
67 bool operator!=(const const_set_bits_iterator_impl &Other) const {
68 assert(&Parent == &Other.Parent &&((void)0)
69 "Comparing iterators from different BitVectors")((void)0);
70 return Current != Other.Current;
71 }
72};
73
74class BitVector {
75 typedef uintptr_t BitWord;
76
77 enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * CHAR_BIT8 };
78
79 static_assert(BITWORD_SIZE == 64 || BITWORD_SIZE == 32,
80 "Unsupported word size");
81
82 using Storage = SmallVector<BitWord>;
83
84 Storage Bits; // Actual bits.
85 unsigned Size; // Size of bitvector in bits.
86
87public:
88 typedef unsigned size_type;
89
90 // Encapsulation of a single bit.
91 class reference {
92
93 BitWord *WordRef;
94 unsigned BitPos;
95
96 public:
97 reference(BitVector &b, unsigned Idx) {
98 WordRef = &b.Bits[Idx / BITWORD_SIZE];
99 BitPos = Idx % BITWORD_SIZE;
100 }
101
102 reference() = delete;
103 reference(const reference&) = default;
104
105 reference &operator=(reference t) {
106 *this = bool(t);
107 return *this;
108 }
109
110 reference& operator=(bool t) {
111 if (t)
112 *WordRef |= BitWord(1) << BitPos;
113 else
114 *WordRef &= ~(BitWord(1) << BitPos);
115 return *this;
116 }
117
118 operator bool() const {
119 return ((*WordRef) & (BitWord(1) << BitPos)) != 0;
120 }
121 };
122
123 typedef const_set_bits_iterator_impl<BitVector> const_set_bits_iterator;
124 typedef const_set_bits_iterator set_iterator;
125
126 const_set_bits_iterator set_bits_begin() const {
127 return const_set_bits_iterator(*this);
128 }
129 const_set_bits_iterator set_bits_end() const {
130 return const_set_bits_iterator(*this, -1);
131 }
132 iterator_range<const_set_bits_iterator> set_bits() const {
133 return make_range(set_bits_begin(), set_bits_end());
134 }
135
136 /// BitVector default ctor - Creates an empty bitvector.
137 BitVector() : Size(0) {}
138
139 /// BitVector ctor - Creates a bitvector of specified number of bits. All
140 /// bits are initialized to the specified value.
141 explicit BitVector(unsigned s, bool t = false)
142 : Bits(NumBitWords(s), 0 - (BitWord)t), Size(s) {
143 if (t)
144 clear_unused_bits();
145 }
146
147 /// empty - Tests whether there are no bits in this bitvector.
148 bool empty() const { return Size == 0; }
149
150 /// size - Returns the number of bits in this bitvector.
151 size_type size() const { return Size; }
152
153 /// count - Returns the number of bits which are set.
154 size_type count() const {
155 unsigned NumBits = 0;
156 for (auto Bit : Bits)
157 NumBits += countPopulation(Bit);
158 return NumBits;
159 }
160
161 /// any - Returns true if any bit is set.
162 bool any() const {
163 return any_of(Bits, [](BitWord Bit) { return Bit != 0; });
164 }
165
166 /// all - Returns true if all bits are set.
167 bool all() const {
168 for (unsigned i = 0; i < Size / BITWORD_SIZE; ++i)
169 if (Bits[i] != ~BitWord(0))
170 return false;
171
172 // If bits remain check that they are ones. The unused bits are always zero.
173 if (unsigned Remainder = Size % BITWORD_SIZE)
174 return Bits[Size / BITWORD_SIZE] == (BitWord(1) << Remainder) - 1;
175
176 return true;
177 }
178
179 /// none - Returns true if none of the bits are set.
180 bool none() const {
181 return !any();
182 }
183
184 /// find_first_in - Returns the index of the first set / unset bit,
185 /// depending on \p Set, in the range [Begin, End).
186 /// Returns -1 if all bits in the range are unset / set.
187 int find_first_in(unsigned Begin, unsigned End, bool Set = true) const {
188 assert(Begin <= End && End <= Size)((void)0);
189 if (Begin == End)
89
Assuming 'Begin' is not equal to 'End'
90
Taking false branch
190 return -1;
191
192 unsigned FirstWord = Begin / BITWORD_SIZE;
193 unsigned LastWord = (End - 1) / BITWORD_SIZE;
194
195 // Check subsequent words.
196 // The code below is based on search for the first _set_ bit. If
197 // we're searching for the first _unset_, we just take the
198 // complement of each word before we use it and apply
199 // the same method.
200 for (unsigned i = FirstWord; i <= LastWord; ++i) {
91
Assuming 'i' is > 'LastWord'
92
Loop condition is false. Execution continues on line 217
201 BitWord Copy = Bits[i];
202 if (!Set)
203 Copy = ~Copy;
204
205 if (i == FirstWord) {
206 unsigned FirstBit = Begin % BITWORD_SIZE;
207 Copy &= maskTrailingZeros<BitWord>(FirstBit);
208 }
209
210 if (i == LastWord) {
211 unsigned LastBit = (End - 1) % BITWORD_SIZE;
212 Copy &= maskTrailingOnes<BitWord>(LastBit + 1);
213 }
214 if (Copy != 0)
215 return i * BITWORD_SIZE + countTrailingZeros(Copy);
216 }
217 return -1;
93
Returning the value -1
218 }
219
220 /// find_last_in - Returns the index of the last set bit in the range
221 /// [Begin, End). Returns -1 if all bits in the range are unset.
222 int find_last_in(unsigned Begin, unsigned End) const {
223 assert(Begin <= End && End <= Size)((void)0);
224 if (Begin == End)
225 return -1;
226
227 unsigned LastWord = (End - 1) / BITWORD_SIZE;
228 unsigned FirstWord = Begin / BITWORD_SIZE;
229
230 for (unsigned i = LastWord + 1; i >= FirstWord + 1; --i) {
231 unsigned CurrentWord = i - 1;
232
233 BitWord Copy = Bits[CurrentWord];
234 if (CurrentWord == LastWord) {
235 unsigned LastBit = (End - 1) % BITWORD_SIZE;
236 Copy &= maskTrailingOnes<BitWord>(LastBit + 1);
237 }
238
239 if (CurrentWord == FirstWord) {
240 unsigned FirstBit = Begin % BITWORD_SIZE;
241 Copy &= maskTrailingZeros<BitWord>(FirstBit);
242 }
243
244 if (Copy != 0)
245 return (CurrentWord + 1) * BITWORD_SIZE - countLeadingZeros(Copy) - 1;
246 }
247
248 return -1;
249 }
250
251 /// find_first_unset_in - Returns the index of the first unset bit in the
252 /// range [Begin, End). Returns -1 if all bits in the range are set.
253 int find_first_unset_in(unsigned Begin, unsigned End) const {
254 return find_first_in(Begin, End, /* Set = */ false);
255 }
256
257 /// find_last_unset_in - Returns the index of the last unset bit in the
258 /// range [Begin, End). Returns -1 if all bits in the range are set.
259 int find_last_unset_in(unsigned Begin, unsigned End) const {
260 assert(Begin <= End && End <= Size)((void)0);
261 if (Begin == End)
262 return -1;
263
264 unsigned LastWord = (End - 1) / BITWORD_SIZE;
265 unsigned FirstWord = Begin / BITWORD_SIZE;
266
267 for (unsigned i = LastWord + 1; i >= FirstWord + 1; --i) {
268 unsigned CurrentWord = i - 1;
269
270 BitWord Copy = Bits[CurrentWord];
271 if (CurrentWord == LastWord) {
272 unsigned LastBit = (End - 1) % BITWORD_SIZE;
273 Copy |= maskTrailingZeros<BitWord>(LastBit + 1);
274 }
275
276 if (CurrentWord == FirstWord) {
277 unsigned FirstBit = Begin % BITWORD_SIZE;
278 Copy |= maskTrailingOnes<BitWord>(FirstBit);
279 }
280
281 if (Copy != ~BitWord(0)) {
282 unsigned Result =
283 (CurrentWord + 1) * BITWORD_SIZE - countLeadingOnes(Copy) - 1;
284 return Result < Size ? Result : -1;
285 }
286 }
287 return -1;
288 }
289
290 /// find_first - Returns the index of the first set bit, -1 if none
291 /// of the bits are set.
292 int find_first() const { return find_first_in(0, Size); }
293
294 /// find_last - Returns the index of the last set bit, -1 if none of the bits
295 /// are set.
296 int find_last() const { return find_last_in(0, Size); }
297
298 /// find_next - Returns the index of the next set bit following the
299 /// "Prev" bit. Returns -1 if the next set bit is not found.
300 int find_next(unsigned Prev) const { return find_first_in(Prev + 1, Size); }
88
Calling 'BitVector::find_first_in'
94
Returning from 'BitVector::find_first_in'
95
Returning the value -1
301
302 /// find_prev - Returns the index of the first set bit that precedes the
303 /// the bit at \p PriorTo. Returns -1 if all previous bits are unset.
304 int find_prev(unsigned PriorTo) const { return find_last_in(0, PriorTo); }
305
306 /// find_first_unset - Returns the index of the first unset bit, -1 if all
307 /// of the bits are set.
308 int find_first_unset() const { return find_first_unset_in(0, Size); }
309
310 /// find_next_unset - Returns the index of the next unset bit following the
311 /// "Prev" bit. Returns -1 if all remaining bits are set.
312 int find_next_unset(unsigned Prev) const {
313 return find_first_unset_in(Prev + 1, Size);
314 }
315
316 /// find_last_unset - Returns the index of the last unset bit, -1 if all of
317 /// the bits are set.
318 int find_last_unset() const { return find_last_unset_in(0, Size); }
319
320 /// find_prev_unset - Returns the index of the first unset bit that precedes
321 /// the bit at \p PriorTo. Returns -1 if all previous bits are set.
322 int find_prev_unset(unsigned PriorTo) {
323 return find_last_unset_in(0, PriorTo);
324 }
325
326 /// clear - Removes all bits from the bitvector.
327 void clear() {
328 Size = 0;
329 Bits.clear();
330 }
331
332 /// resize - Grow or shrink the bitvector.
333 void resize(unsigned N, bool t = false) {
334 set_unused_bits(t);
335 Size = N;
336 Bits.resize(NumBitWords(N), 0 - BitWord(t));
337 clear_unused_bits();
338 }
339
340 void reserve(unsigned N) { Bits.reserve(NumBitWords(N)); }
341
342 // Set, reset, flip
343 BitVector &set() {
344 init_words(true);
345 clear_unused_bits();
346 return *this;
347 }
348
349 BitVector &set(unsigned Idx) {
350 assert(Idx < Size && "access in bound")((void)0);
351 Bits[Idx / BITWORD_SIZE] |= BitWord(1) << (Idx % BITWORD_SIZE);
352 return *this;
353 }
354
355 /// set - Efficiently set a range of bits in [I, E)
356 BitVector &set(unsigned I, unsigned E) {
357 assert(I <= E && "Attempted to set backwards range!")((void)0);
358 assert(E <= size() && "Attempted to set out-of-bounds range!")((void)0);
359
360 if (I == E) return *this;
361
362 if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
363 BitWord EMask = BitWord(1) << (E % BITWORD_SIZE);
364 BitWord IMask = BitWord(1) << (I % BITWORD_SIZE);
365 BitWord Mask = EMask - IMask;
366 Bits[I / BITWORD_SIZE] |= Mask;
367 return *this;
368 }
369
370 BitWord PrefixMask = ~BitWord(0) << (I % BITWORD_SIZE);
371 Bits[I / BITWORD_SIZE] |= PrefixMask;
372 I = alignTo(I, BITWORD_SIZE);
373
374 for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
375 Bits[I / BITWORD_SIZE] = ~BitWord(0);
376
377 BitWord PostfixMask = (BitWord(1) << (E % BITWORD_SIZE)) - 1;
378 if (I < E)
379 Bits[I / BITWORD_SIZE] |= PostfixMask;
380
381 return *this;
382 }
383
384 BitVector &reset() {
385 init_words(false);
386 return *this;
387 }
388
389 BitVector &reset(unsigned Idx) {
390 Bits[Idx / BITWORD_SIZE] &= ~(BitWord(1) << (Idx % BITWORD_SIZE));
391 return *this;
392 }
393
394 /// reset - Efficiently reset a range of bits in [I, E)
395 BitVector &reset(unsigned I, unsigned E) {
396 assert(I <= E && "Attempted to reset backwards range!")((void)0);
397 assert(E <= size() && "Attempted to reset out-of-bounds range!")((void)0);
398
399 if (I == E) return *this;
400
401 if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
402 BitWord EMask = BitWord(1) << (E % BITWORD_SIZE);
403 BitWord IMask = BitWord(1) << (I % BITWORD_SIZE);
404 BitWord Mask = EMask - IMask;
405 Bits[I / BITWORD_SIZE] &= ~Mask;
406 return *this;
407 }
408
409 BitWord PrefixMask = ~BitWord(0) << (I % BITWORD_SIZE);
410 Bits[I / BITWORD_SIZE] &= ~PrefixMask;
411 I = alignTo(I, BITWORD_SIZE);
412
413 for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
414 Bits[I / BITWORD_SIZE] = BitWord(0);
415
416 BitWord PostfixMask = (BitWord(1) << (E % BITWORD_SIZE)) - 1;
417 if (I < E)
418 Bits[I / BITWORD_SIZE] &= ~PostfixMask;
419
420 return *this;
421 }
422
423 BitVector &flip() {
424 for (auto &Bit : Bits)
425 Bit = ~Bit;
426 clear_unused_bits();
427 return *this;
428 }
429
430 BitVector &flip(unsigned Idx) {
431 Bits[Idx / BITWORD_SIZE] ^= BitWord(1) << (Idx % BITWORD_SIZE);
432 return *this;
433 }
434
435 // Indexing.
436 reference operator[](unsigned Idx) {
437 assert (Idx < Size && "Out-of-bounds Bit access.")((void)0);
438 return reference(*this, Idx);
439 }
440
441 bool operator[](unsigned Idx) const {
442 assert (Idx < Size && "Out-of-bounds Bit access.")((void)0);
443 BitWord Mask = BitWord(1) << (Idx % BITWORD_SIZE);
444 return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
445 }
446
447 bool test(unsigned Idx) const {
448 return (*this)[Idx];
449 }
450
451 // Push single bit to end of vector.
452 void push_back(bool Val) {
453 unsigned OldSize = Size;
454 unsigned NewSize = Size + 1;
455
456 // Resize, which will insert zeros.
457 // If we already fit then the unused bits will be already zero.
458 if (NewSize > getBitCapacity())
459 resize(NewSize, false);
460 else
461 Size = NewSize;
462
463 // If true, set single bit.
464 if (Val)
465 set(OldSize);
466 }
467
468 /// Test if any common bits are set.
469 bool anyCommon(const BitVector &RHS) const {
470 unsigned ThisWords = Bits.size();
471 unsigned RHSWords = RHS.Bits.size();
472 for (unsigned i = 0, e = std::min(ThisWords, RHSWords); i != e; ++i)
473 if (Bits[i] & RHS.Bits[i])
474 return true;
475 return false;
476 }
477
478 // Comparison operators.
479 bool operator==(const BitVector &RHS) const {
480 if (size() != RHS.size())
481 return false;
482 unsigned NumWords = Bits.size();
483 return std::equal(Bits.begin(), Bits.begin() + NumWords, RHS.Bits.begin());
484 }
485
486 bool operator!=(const BitVector &RHS) const { return !(*this == RHS); }
487
488 /// Intersection, union, disjoint union.
489 BitVector &operator&=(const BitVector &RHS) {
490 unsigned ThisWords = Bits.size();
491 unsigned RHSWords = RHS.Bits.size();
492 unsigned i;
493 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
494 Bits[i] &= RHS.Bits[i];
495
496 // Any bits that are just in this bitvector become zero, because they aren't
497 // in the RHS bit vector. Any words only in RHS are ignored because they
498 // are already zero in the LHS.
499 for (; i != ThisWords; ++i)
500 Bits[i] = 0;
501
502 return *this;
503 }
504
505 /// reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
506 BitVector &reset(const BitVector &RHS) {
507 unsigned ThisWords = Bits.size();
508 unsigned RHSWords = RHS.Bits.size();
509 for (unsigned i = 0; i != std::min(ThisWords, RHSWords); ++i)
510 Bits[i] &= ~RHS.Bits[i];
511 return *this;
512 }
513
514 /// test - Check if (This - RHS) is zero.
515 /// This is the same as reset(RHS) and any().
516 bool test(const BitVector &RHS) const {
517 unsigned ThisWords = Bits.size();
518 unsigned RHSWords = RHS.Bits.size();
519 unsigned i;
520 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
521 if ((Bits[i] & ~RHS.Bits[i]) != 0)
522 return true;
523
524 for (; i != ThisWords ; ++i)
525 if (Bits[i] != 0)
526 return true;
527
528 return false;
529 }
530
531 template <class F, class... ArgTys>
532 static BitVector &apply(F &&f, BitVector &Out, BitVector const &Arg,
533 ArgTys const &...Args) {
534 assert(llvm::all_of(((void)0)
535 std::initializer_list<unsigned>{Args.size()...},((void)0)
536 [&Arg](auto const &BV) { return Arg.size() == BV; }) &&((void)0)
537 "consistent sizes")((void)0);
538 Out.resize(Arg.size());
539 for (size_t i = 0, e = Arg.Bits.size(); i != e; ++i)
540 Out.Bits[i] = f(Arg.Bits[i], Args.Bits[i]...);
541 Out.clear_unused_bits();
542 return Out;
543 }
544
545 BitVector &operator|=(const BitVector &RHS) {
546 if (size() < RHS.size())
547 resize(RHS.size());
548 for (size_t i = 0, e = RHS.Bits.size(); i != e; ++i)
549 Bits[i] |= RHS.Bits[i];
550 return *this;
551 }
552
553 BitVector &operator^=(const BitVector &RHS) {
554 if (size() < RHS.size())
555 resize(RHS.size());
556 for (size_t i = 0, e = RHS.Bits.size(); i != e; ++i)
557 Bits[i] ^= RHS.Bits[i];
558 return *this;
559 }
560
561 BitVector &operator>>=(unsigned N) {
562 assert(N <= Size)((void)0);
563 if (LLVM_UNLIKELY(empty() || N == 0)__builtin_expect((bool)(empty() || N == 0), false))
564 return *this;
565
566 unsigned NumWords = Bits.size();
567 assert(NumWords >= 1)((void)0);
568
569 wordShr(N / BITWORD_SIZE);
570
571 unsigned BitDistance = N % BITWORD_SIZE;
572 if (BitDistance == 0)
573 return *this;
574
575 // When the shift size is not a multiple of the word size, then we have
576 // a tricky situation where each word in succession needs to extract some
577 // of the bits from the next word and or them into this word while
578 // shifting this word to make room for the new bits. This has to be done
579 // for every word in the array.
580
581 // Since we're shifting each word right, some bits will fall off the end
582 // of each word to the right, and empty space will be created on the left.
583 // The final word in the array will lose bits permanently, so starting at
584 // the beginning, work forwards shifting each word to the right, and
585 // OR'ing in the bits from the end of the next word to the beginning of
586 // the current word.
587
588 // Example:
589 // Starting with {0xAABBCCDD, 0xEEFF0011, 0x22334455} and shifting right
590 // by 4 bits.
591 // Step 1: Word[0] >>= 4 ; 0x0ABBCCDD
592 // Step 2: Word[0] |= 0x10000000 ; 0x1ABBCCDD
593 // Step 3: Word[1] >>= 4 ; 0x0EEFF001
594 // Step 4: Word[1] |= 0x50000000 ; 0x5EEFF001
595 // Step 5: Word[2] >>= 4 ; 0x02334455
596 // Result: { 0x1ABBCCDD, 0x5EEFF001, 0x02334455 }
597 const BitWord Mask = maskTrailingOnes<BitWord>(BitDistance);
598 const unsigned LSH = BITWORD_SIZE - BitDistance;
599
600 for (unsigned I = 0; I < NumWords - 1; ++I) {
601 Bits[I] >>= BitDistance;
602 Bits[I] |= (Bits[I + 1] & Mask) << LSH;
603 }
604
605 Bits[NumWords - 1] >>= BitDistance;
606
607 return *this;
608 }
609
610 BitVector &operator<<=(unsigned N) {
611 assert(N <= Size)((void)0);
612 if (LLVM_UNLIKELY(empty() || N == 0)__builtin_expect((bool)(empty() || N == 0), false))
613 return *this;
614
615 unsigned NumWords = Bits.size();
616 assert(NumWords >= 1)((void)0);
617
618 wordShl(N / BITWORD_SIZE);
619
620 unsigned BitDistance = N % BITWORD_SIZE;
621 if (BitDistance == 0)
622 return *this;
623
624 // When the shift size is not a multiple of the word size, then we have
625 // a tricky situation where each word in succession needs to extract some
626 // of the bits from the previous word and or them into this word while
627 // shifting this word to make room for the new bits. This has to be done
628 // for every word in the array. This is similar to the algorithm outlined
629 // in operator>>=, but backwards.
630
631 // Since we're shifting each word left, some bits will fall off the end
632 // of each word to the left, and empty space will be created on the right.
633 // The first word in the array will lose bits permanently, so starting at
634 // the end, work backwards shifting each word to the left, and OR'ing
635 // in the bits from the end of the next word to the beginning of the
636 // current word.
637
638 // Example:
639 // Starting with {0xAABBCCDD, 0xEEFF0011, 0x22334455} and shifting left
640 // by 4 bits.
641 // Step 1: Word[2] <<= 4 ; 0x23344550
642 // Step 2: Word[2] |= 0x0000000E ; 0x2334455E
643 // Step 3: Word[1] <<= 4 ; 0xEFF00110
644 // Step 4: Word[1] |= 0x0000000A ; 0xEFF0011A
645 // Step 5: Word[0] <<= 4 ; 0xABBCCDD0
646 // Result: { 0xABBCCDD0, 0xEFF0011A, 0x2334455E }
647 const BitWord Mask = maskLeadingOnes<BitWord>(BitDistance);
648 const unsigned RSH = BITWORD_SIZE - BitDistance;
649
650 for (int I = NumWords - 1; I > 0; --I) {
651 Bits[I] <<= BitDistance;
652 Bits[I] |= (Bits[I - 1] & Mask) >> RSH;
653 }
654 Bits[0] <<= BitDistance;
655 clear_unused_bits();
656
657 return *this;
658 }
659
660 void swap(BitVector &RHS) {
661 std::swap(Bits, RHS.Bits);
662 std::swap(Size, RHS.Size);
663 }
664
665 void invalid() {
666 assert(!Size && Bits.empty())((void)0);
667 Size = (unsigned)-1;
668 }
669 bool isInvalid() const { return Size == (unsigned)-1; }
670
671 ArrayRef<BitWord> getData() const { return {&Bits[0], Bits.size()}; }
672
673 //===--------------------------------------------------------------------===//
674 // Portable bit mask operations.
675 //===--------------------------------------------------------------------===//
676 //
677 // These methods all operate on arrays of uint32_t, each holding 32 bits. The
678 // fixed word size makes it easier to work with literal bit vector constants
679 // in portable code.
680 //
681 // The LSB in each word is the lowest numbered bit. The size of a portable
682 // bit mask is always a whole multiple of 32 bits. If no bit mask size is
683 // given, the bit mask is assumed to cover the entire BitVector.
684
685 /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
686 /// This computes "*this |= Mask".
687 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
688 applyMask<true, false>(Mask, MaskWords);
689 }
690
691 /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
692 /// Don't resize. This computes "*this &= ~Mask".
693 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
694 applyMask<false, false>(Mask, MaskWords);
695 }
696
697 /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
698 /// Don't resize. This computes "*this |= ~Mask".
699 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
700 applyMask<true, true>(Mask, MaskWords);
701 }
702
703 /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
704 /// Don't resize. This computes "*this &= Mask".
705 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
706 applyMask<false, true>(Mask, MaskWords);
707 }
708
709private:
710 /// Perform a logical left shift of \p Count words by moving everything
711 /// \p Count words to the right in memory.
712 ///
713 /// While confusing, words are stored from least significant at Bits[0] to
714 /// most significant at Bits[NumWords-1]. A logical shift left, however,
715 /// moves the current least significant bit to a higher logical index, and
716 /// fills the previous least significant bits with 0. Thus, we actually
717 /// need to move the bytes of the memory to the right, not to the left.
718 /// Example:
719 /// Words = [0xBBBBAAAA, 0xDDDDFFFF, 0x00000000, 0xDDDD0000]
720 /// represents a BitVector where 0xBBBBAAAA contain the least significant
721 /// bits. So if we want to shift the BitVector left by 2 words, we need
722 /// to turn this into 0x00000000 0x00000000 0xBBBBAAAA 0xDDDDFFFF by using a
723 /// memmove which moves right, not left.
724 void wordShl(uint32_t Count) {
725 if (Count == 0)
726 return;
727
728 uint32_t NumWords = Bits.size();
729
730 // Since we always move Word-sized chunks of data with src and dest both
731 // aligned to a word-boundary, we don't need to worry about endianness
732 // here.
733 std::copy(Bits.begin(), Bits.begin() + NumWords - Count,
734 Bits.begin() + Count);
735 std::fill(Bits.begin(), Bits.begin() + Count, 0);
736 clear_unused_bits();
737 }
738
739 /// Perform a logical right shift of \p Count words by moving those
740 /// words to the left in memory. See wordShl for more information.
741 ///
742 void wordShr(uint32_t Count) {
743 if (Count == 0)
744 return;
745
746 uint32_t NumWords = Bits.size();
747
748 std::copy(Bits.begin() + Count, Bits.begin() + NumWords, Bits.begin());
749 std::fill(Bits.begin() + NumWords - Count, Bits.begin() + NumWords, 0);
750 }
751
752 int next_unset_in_word(int WordIndex, BitWord Word) const {
753 unsigned Result = WordIndex * BITWORD_SIZE + countTrailingOnes(Word);
754 return Result < size() ? Result : -1;
755 }
756
757 unsigned NumBitWords(unsigned S) const {
758 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
759 }
760
761 // Set the unused bits in the high words.
762 void set_unused_bits(bool t = true) {
763 // Then set any stray high bits of the last used word.
764 if (unsigned ExtraBits = Size % BITWORD_SIZE) {
765 BitWord ExtraBitMask = ~BitWord(0) << ExtraBits;
766 if (t)
767 Bits.back() |= ExtraBitMask;
768 else
769 Bits.back() &= ~ExtraBitMask;
770 }
771 }
772
773 // Clear the unused bits in the high words.
774 void clear_unused_bits() {
775 set_unused_bits(false);
776 }
777
778 void init_words(bool t) {
779 std::fill(Bits.begin(), Bits.end(), 0 - (BitWord)t);
780 }
781
782 template<bool AddBits, bool InvertMask>
783 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
784 static_assert(BITWORD_SIZE % 32 == 0, "Unsupported BitWord size.");
785 MaskWords = std::min(MaskWords, (size() + 31) / 32);
786 const unsigned Scale = BITWORD_SIZE / 32;
787 unsigned i;
788 for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) {
789 BitWord BW = Bits[i];
790 // This inner loop should unroll completely when BITWORD_SIZE > 32.
791 for (unsigned b = 0; b != BITWORD_SIZE; b += 32) {
792 uint32_t M = *Mask++;
793 if (InvertMask) M = ~M;
794 if (AddBits) BW |= BitWord(M) << b;
795 else BW &= ~(BitWord(M) << b);
796 }
797 Bits[i] = BW;
798 }
799 for (unsigned b = 0; MaskWords; b += 32, --MaskWords) {
800 uint32_t M = *Mask++;
801 if (InvertMask) M = ~M;
802 if (AddBits) Bits[i] |= BitWord(M) << b;
803 else Bits[i] &= ~(BitWord(M) << b);
804 }
805 if (AddBits)
806 clear_unused_bits();
807 }
808
809public:
810 /// Return the size (in bytes) of the bit vector.
811 size_t getMemorySize() const { return Bits.size() * sizeof(BitWord); }
812 size_t getBitCapacity() const { return Bits.size() * BITWORD_SIZE; }
813};
814
815inline size_t capacity_in_bytes(const BitVector &X) {
816 return X.getMemorySize();
817}
818
819template <> struct DenseMapInfo<BitVector> {
820 static inline BitVector getEmptyKey() { return {}; }
821 static inline BitVector getTombstoneKey() {
822 BitVector V;
823 V.invalid();
824 return V;
825 }
826 static unsigned getHashValue(const BitVector &V) {
827 return DenseMapInfo<std::pair<unsigned, ArrayRef<uintptr_t>>>::getHashValue(
828 std::make_pair(V.size(), V.getData()));
829 }
830 static bool isEqual(const BitVector &LHS, const BitVector &RHS) {
831 if (LHS.isInvalid() || RHS.isInvalid())
832 return LHS.isInvalid() == RHS.isInvalid();
833 return LHS == RHS;
834 }
835};
836} // end namespace llvm
837
838namespace std {
839 /// Implement std::swap in terms of BitVector swap.
840inline void swap(llvm::BitVector &LHS, llvm::BitVector &RHS) { LHS.swap(RHS); }
841} // end namespace std
842
843#endif // LLVM_ADT_BITVECTOR_H