LLVM 22.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 /// Cache maintained during a computeKnownBits request.
41 SmallDenseMap<Register, KnownBits, 16> ComputeKnownBitsCache;
42
43 void computeKnownBitsMin(Register Src0, Register Src1, KnownBits &Known,
44 const APInt &DemandedElts, unsigned Depth = 0);
45
46 unsigned computeNumSignBitsMin(Register Src0, Register Src1,
47 const APInt &DemandedElts, unsigned Depth = 0);
48
50 FPClassTest InterestedClasses, unsigned Depth);
51
53 const APInt &DemandedElts,
54 FPClassTest InterestedClasses,
55 KnownFPClass &Known, unsigned Depth);
56
57 void computeKnownFPClass(Register R, const APInt &DemandedElts,
58 FPClassTest InterestedClasses, KnownFPClass &Known,
59 unsigned Depth);
60
61public:
62 GISelValueTracking(MachineFunction &MF, unsigned MaxDepth = 6);
63 virtual ~GISelValueTracking() = default;
64
65 const MachineFunction &getMachineFunction() const { return MF; }
66
67 const DataLayout &getDataLayout() const { return DL; }
68
69 virtual void computeKnownBitsImpl(Register R, KnownBits &Known,
70 const APInt &DemandedElts,
71 unsigned Depth = 0);
72
73 unsigned computeNumSignBits(Register R, const APInt &DemandedElts,
74 unsigned Depth = 0);
75 unsigned computeNumSignBits(Register R, unsigned Depth = 0);
76
77 // KnownBitsAPI
78 KnownBits getKnownBits(Register R);
79 KnownBits getKnownBits(Register R, const APInt &DemandedElts,
80 unsigned Depth = 0);
81
82 // Calls getKnownBits for first operand def of MI.
83 KnownBits getKnownBits(MachineInstr &MI);
84 APInt getKnownZeroes(Register R);
85 APInt getKnownOnes(Register R);
86
87 /// \return true if 'V & Mask' is known to be zero in DemandedElts. We use
88 /// this predicate to simplify operations downstream.
89 /// Mask is known to be zero for bits that V cannot have.
90 bool maskedValueIsZero(Register Val, const APInt &Mask) {
91 return Mask.isSubsetOf(getKnownBits(Val).Zero);
92 }
93
94 /// \return true if the sign bit of Op is known to be zero. We use this
95 /// predicate to simplify operations downstream.
96 bool signBitIsZero(Register Op);
97
98 static void computeKnownBitsForAlignment(KnownBits &Known, Align Alignment) {
99 // The low bits are known zero if the pointer is aligned.
100 Known.Zero.setLowBits(Log2(Alignment));
101 }
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 // Observer API. No-op for non-caching implementation.
149 void erasingInstr(MachineInstr &MI) override {}
150 void createdInstr(MachineInstr &MI) override {}
151 void changingInstr(MachineInstr &MI) override {}
152 void changedInstr(MachineInstr &MI) override {}
153
154protected:
155 unsigned getMaxDepth() const { return MaxDepth; }
156};
157
158/// To use KnownBitsInfo analysis in a pass,
159/// KnownBitsInfo &Info = getAnalysis<GISelValueTrackingInfoAnalysis>().get(MF);
160/// Add to observer if the Info is caching.
161/// WrapperObserver.addObserver(Info);
162
163/// Eventually add other features such as caching/ser/deserializing
164/// to MIR etc. Those implementations can derive from GISelValueTracking
165/// and override computeKnownBitsImpl.
167 std::unique_ptr<GISelValueTracking> Info;
168
169public:
170 static char ID;
173 *PassRegistry::getPassRegistry());
174 }
176 void getAnalysisUsage(AnalysisUsage &AU) const override;
177 bool runOnMachineFunction(MachineFunction &MF) override;
178 void releaseMemory() override { Info.reset(); }
179};
180
182 : public AnalysisInfoMixin<GISelValueTrackingAnalysis> {
184 LLVM_ABI static AnalysisKey Key;
185
186public:
188
191};
192
194 : public PassInfoMixin<GISelValueTrackingPrinterPass> {
195 raw_ostream &OS;
196
197public:
199
202};
203} // namespace llvm
204
205#endif // LLVM_CODEGEN_GLOBALISEL_GISELVALUETRACKING_H
unsigned const MachineRegisterInfo * MRI
This file declares a class to represent arbitrary precision floating point values and provide a varie...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define LLVM_ABI
Definition: Compiler.h:213
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.
raw_pwrite_stream & OS
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
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1388
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
Represent the analysis usage information of a pass.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
Abstract class that contains various methods for clients to notify about changes.
To use KnownBitsInfo analysis in a pass, KnownBitsInfo &Info = getAnalysis<GISelValueTrackingInfoAnal...
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)
void createdInstr(MachineInstr &MI) override
An instruction has been created and inserted into the function.
static void computeKnownBitsForAlignment(KnownBits &Known, Align Alignment)
bool maskedValueIsZero(Register Val, const APInt &Mask)
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.
void changingInstr(MachineInstr &MI) override
This instruction is about to be mutated in some way.
GISelValueTracking(MachineFunction &MF, unsigned MaxDepth=6)
virtual ~GISelValueTracking()=default
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Representation of each machine instruction.
Definition: MachineInstr.h:72
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:19
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.
Definition: AddressRanges.h:18
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void initializeGISelValueTrackingAnalysisLegacyPass(PassRegistry &)
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:208
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.
Definition: PassManager.h:93
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:29
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:70