LLVM 24.0.0git
GISelValueTracking.h
Go to the documentation of this file.
1//===- llvm/CodeGen/GlobalISel/GISelValueTracking.h -------------*- 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/// \file
9/// Provides analysis for querying information about KnownBits during GISel
10/// passes.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CODEGEN_GLOBALISEL_GISELVALUETRACKING_H
15#define LLVM_CODEGEN_GLOBALISEL_GISELVALUETRACKING_H
16
17#include "llvm/ADT/APFloat.h"
18#include "llvm/ADT/DenseMap.h"
22#include "llvm/IR/InstrTypes.h"
23#include "llvm/IR/PassManager.h"
28
29namespace llvm {
30
31class TargetLowering;
32class DataLayout;
33
37 const TargetLowering &TL;
38 const DataLayout &DL;
39 unsigned MaxDepth;
40
41 void computeKnownBitsMin(Register Src0, Register Src1, KnownBits &Known,
42 const APInt &DemandedElts, unsigned Depth = 0);
43
44 unsigned computeNumSignBitsMin(Register Src0, Register Src1,
45 const APInt &DemandedElts, unsigned Depth = 0);
46
48 FPClassTest InterestedClasses, unsigned Depth);
49
51 const APInt &DemandedElts,
52 FPClassTest InterestedClasses,
53 KnownFPClass &Known, unsigned Depth);
54
55 void computeKnownFPClass(Register R, const APInt &DemandedElts,
56 FPClassTest InterestedClasses, KnownFPClass &Known,
57 unsigned Depth);
58
59public:
60 GISelValueTracking(MachineFunction &MF, unsigned MaxDepth = 6);
61 ~GISelValueTracking() override = default;
62
63 const MachineFunction &getMachineFunction() const { return MF; }
64
65 const DataLayout &getDataLayout() const { return DL; }
66
67 void computeKnownBitsImpl(Register R, KnownBits &Known,
68 const APInt &DemandedElts, unsigned Depth = 0);
69
70 unsigned computeNumSignBits(Register R, const APInt &DemandedElts,
71 unsigned Depth = 0);
72 unsigned computeNumSignBits(Register R, unsigned Depth = 0);
73
74 // KnownBitsAPI
75 KnownBits getKnownBits(Register R);
76 KnownBits getKnownBits(Register R, const APInt &DemandedElts,
77 unsigned Depth = 0);
78
79 APInt getKnownZeroes(Register R);
80 APInt getKnownOnes(Register R);
81
82 /// \return true if 'V & Mask' is known to be zero in DemandedElts. We use
83 /// this predicate to simplify operations downstream.
84 /// Mask is known to be zero for bits that V cannot have.
85 bool maskedValueIsZero(Register Val, const APInt &Mask) {
86 return Mask.isSubsetOf(getKnownBits(Val).Zero);
87 }
88
89 /// \return true if the sign bit of Op is known to be zero. We use this
90 /// predicate to simplify operations downstream.
91 bool signBitIsZero(Register Op);
92
93 /// Return true if the value defined by \p R is provably never zero.
94 ///
95 /// \p DemandedElts selects the vector elements that must be proven nonzero.
96 /// For scalar values this is a one-bit mask. The overload without
97 /// \p DemandedElts demands every fixed-vector element, or the scalar value to
98 /// be non-zero.
99 bool isKnownNeverZero(Register R, unsigned Depth = 0);
100 bool isKnownNeverZero(Register R, const APInt &DemandedElts,
101 unsigned Depth = 0);
102
103 /// \return The known alignment for the pointer-like value \p R.
104 Align computeKnownAlignment(Register R, unsigned Depth = 0);
105
106 /// If a G_SHL/G_ASHR/G_LSHR node with shift operand \p R has shift amounts
107 /// that are all less than the element bit-width of the shift node, return the
108 /// valid constant range.
109 std::optional<ConstantRange>
110 getValidShiftAmountRange(Register R, const APInt &DemandedElts,
111 unsigned Depth);
112
113 /// If a G_SHL/G_ASHR/G_LSHR node with shift operand \p R has shift amounts
114 /// that are all less than the element bit-width of the shift node, return the
115 /// minimum possible value.
116 std::optional<uint64_t> getValidMinimumShiftAmount(Register R,
117 const APInt &DemandedElts,
118 unsigned Depth = 0);
119
120 /// Determine which floating-point classes are valid for \p V, and return them
121 /// in KnownFPClass bit sets.
122 ///
123 /// This function is defined on values with floating-point type, values
124 /// vectors of floating-point type, and arrays of floating-point type.
125
126 /// \p InterestedClasses is a compile time optimization hint for which
127 /// floating point classes should be queried. Queries not specified in \p
128 /// InterestedClasses should be reliable if they are determined during the
129 /// query.
130 KnownFPClass computeKnownFPClass(Register R, const APInt &DemandedElts,
131 FPClassTest InterestedClasses,
132 unsigned Depth);
133
135 FPClassTest InterestedClasses = fcAllFlags,
136 unsigned Depth = 0);
137
138 /// Wrapper to account for known fast math flags at the use instruction.
139 KnownFPClass computeKnownFPClass(Register R, const APInt &DemandedElts,
140 uint32_t Flags,
141 FPClassTest InterestedClasses,
142 unsigned Depth);
143
145 FPClassTest InterestedClasses,
146 unsigned Depth);
147
148 /// Returns true if \p Val can be assumed to never be a NaN. If \p SNaN is
149 /// true, this returns whether \p Val can be assumed to never be a signaling
150 /// NaN.
151 bool isKnownNeverNaN(Register Val, bool SNaN = false);
152
153 /// Returns true if \p Val can be assumed to never be a signaling NaN.
154 bool isKnownNeverSNaN(Register Val) { return isKnownNeverNaN(Val, true); }
155
156 // Observer API. No-op for non-caching implementation.
157 void erasingInstr(MachineInstr &MI) override {}
158 void createdInstr(MachineInstr &MI) override {}
159 void changingInstr(MachineInstr &MI) override {}
160 void changedInstr(MachineInstr &MI) override {}
161
162protected:
163 unsigned getMaxDepth() const { return MaxDepth; }
164};
165
166/// To use KnownBitsInfo analysis in a pass,
167/// KnownBitsInfo &Info = getAnalysis<GISelValueTrackingInfoAnalysis>().get(MF);
168/// Add to observer if the Info is caching.
169/// WrapperObserver.addObserver(Info);
170
171/// Eventually add other features such as caching/ser/deserializing
172/// to MIR etc. Those implementations can derive from GISelValueTracking
173/// and override computeKnownBitsImpl.
175 std::unique_ptr<GISelValueTracking> Info;
176
177public:
178 static char ID;
181 void getAnalysisUsage(AnalysisUsage &AU) const override;
182 bool runOnMachineFunction(MachineFunction &MF) override;
183 void releaseMemory() override { Info.reset(); }
184};
185
187 : public AnalysisInfoMixin<GISelValueTrackingAnalysis> {
189 LLVM_ABI static AnalysisKey Key;
190
191public:
193
196};
197
199 : public RequiredPassInfoMixin<GISelValueTrackingPrinterPass> {
200 raw_ostream &OS;
201
202public:
204
207};
208} // namespace llvm
209
210#endif // LLVM_CODEGEN_GLOBALISEL_GISELVALUETRACKING_H
This file declares a class to represent arbitrary precision floating point values and provide a varie...
#define LLVM_ABI
Definition Compiler.h:215
This file defines the DenseMap class.
This contains common code to allow clients to notify changes to machine instr.
IRTranslator LLVM IR MI
This header defines various interfaces for pass management in LLVM.
void computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth)
static void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth)
Class for arbitrary precision integers.
Definition APInt.h:78
Represent the analysis usage information of a pass.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Abstract class that contains various methods for clients to notify about changes.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
KnownBits getKnownBits(Register R)
void createdInstr(MachineInstr &MI) override
An instruction has been created and inserted into the function.
bool maskedValueIsZero(Register Val, const APInt &Mask)
bool isKnownNeverSNaN(Register Val)
Returns true if Val can be assumed to never be a signaling NaN.
const DataLayout & getDataLayout() const
const MachineFunction & getMachineFunction() const
void changedInstr(MachineInstr &MI) override
This instruction was mutated in some way.
void erasingInstr(MachineInstr &MI) override
An instruction is about to be erased.
bool isKnownNeverNaN(Register Val, bool SNaN=false)
Returns true if Val can be assumed to never be a NaN.
~GISelValueTracking() override=default
void changingInstr(MachineInstr &MI) override
This instruction is about to be mutated in some way.
GISelValueTracking(MachineFunction &MF, unsigned MaxDepth=6)
Representation of each machine instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
Wrapper class representing virtual and physical registers.
Definition Register.h:20
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
@ Known
Known to have no common set bits.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
DWARFExpression::Operation Op
LLVM_ABI bool isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
A CRTP mix-in that provides informational APIs needed for analysis passes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
A CRTP mix-in for passes that should not be skipped.