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
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);
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 // Calls getKnownBits for first operand def of MI.
80 KnownBits getKnownBits(MachineInstr &MI);
81 APInt getKnownZeroes(Register R);
82 APInt getKnownOnes(Register R);
83
84 /// \return true if 'V & Mask' is known to be zero in DemandedElts. We use
85 /// this predicate to simplify operations downstream.
86 /// Mask is known to be zero for bits that V cannot have.
87 bool maskedValueIsZero(Register Val, const APInt &Mask) {
88 return Mask.isSubsetOf(getKnownBits(Val).Zero);
89 }
90
91 /// \return true if the sign bit of Op is known to be zero. We use this
92 /// predicate to simplify operations downstream.
93 bool signBitIsZero(Register Op);
94
95 static void computeKnownBitsForAlignment(KnownBits &Known, Align Alignment) {
96 // The low bits are known zero if the pointer is aligned.
97 Known.Zero.setLowBits(Log2(Alignment));
98 }
99
100 /// \return The known alignment for the pointer-like value \p R.
101 Align computeKnownAlignment(Register R, unsigned Depth = 0);
102
103 /// If a G_SHL/G_ASHR/G_LSHR node with shift operand \p R has shift amounts
104 /// that are all less than the element bit-width of the shift node, return the
105 /// valid constant range.
106 std::optional<ConstantRange>
107 getValidShiftAmountRange(Register R, const APInt &DemandedElts,
108 unsigned Depth);
109
110 /// If a G_SHL/G_ASHR/G_LSHR node with shift operand \p R has shift amounts
111 /// that are all less than the element bit-width of the shift node, return the
112 /// minimum possible value.
113 std::optional<uint64_t> getValidMinimumShiftAmount(Register R,
114 const APInt &DemandedElts,
115 unsigned Depth = 0);
116
117 /// Determine which floating-point classes are valid for \p V, and return them
118 /// in KnownFPClass bit sets.
119 ///
120 /// This function is defined on values with floating-point type, values
121 /// vectors of floating-point type, and arrays of floating-point type.
122
123 /// \p InterestedClasses is a compile time optimization hint for which
124 /// floating point classes should be queried. Queries not specified in \p
125 /// InterestedClasses should be reliable if they are determined during the
126 /// query.
127 KnownFPClass computeKnownFPClass(Register R, const APInt &DemandedElts,
128 FPClassTest InterestedClasses,
129 unsigned Depth);
130
132 FPClassTest InterestedClasses = fcAllFlags,
133 unsigned Depth = 0);
134
135 /// Wrapper to account for known fast math flags at the use instruction.
136 KnownFPClass computeKnownFPClass(Register R, const APInt &DemandedElts,
137 uint32_t Flags,
138 FPClassTest InterestedClasses,
139 unsigned Depth);
140
142 FPClassTest InterestedClasses,
143 unsigned Depth);
144
145 // Observer API. No-op for non-caching implementation.
146 void erasingInstr(MachineInstr &MI) override {}
147 void createdInstr(MachineInstr &MI) override {}
148 void changingInstr(MachineInstr &MI) override {}
149 void changedInstr(MachineInstr &MI) override {}
150
151protected:
152 unsigned getMaxDepth() const { return MaxDepth; }
153};
154
155/// To use KnownBitsInfo analysis in a pass,
156/// KnownBitsInfo &Info = getAnalysis<GISelValueTrackingInfoAnalysis>().get(MF);
157/// Add to observer if the Info is caching.
158/// WrapperObserver.addObserver(Info);
159
160/// Eventually add other features such as caching/ser/deserializing
161/// to MIR etc. Those implementations can derive from GISelValueTracking
162/// and override computeKnownBitsImpl.
164 std::unique_ptr<GISelValueTracking> Info;
165
166public:
167 static char ID;
173 void getAnalysisUsage(AnalysisUsage &AU) const override;
174 bool runOnMachineFunction(MachineFunction &MF) override;
175 void releaseMemory() override { Info.reset(); }
176};
177
179 : public AnalysisInfoMixin<GISelValueTrackingAnalysis> {
181 LLVM_ABI static AnalysisKey Key;
182
183public:
185
188};
189
191 : public PassInfoMixin<GISelValueTrackingPrinterPass> {
192 raw_ostream &OS;
193
194public:
196
199};
200} // namespace llvm
201
202#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: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.
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
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:63
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.
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)
Representation of each machine instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
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 &)
DWARFExpression::Operation Op
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