LLVM 19.0.0git
GISelKnownBits.h
Go to the documentation of this file.
1//===- llvm/CodeGen/GlobalISel/GISelKnownBits.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_GISELKNOWNBITS_H
15#define LLVM_CODEGEN_GLOBALISEL_GISELKNOWNBITS_H
16
17#include "llvm/ADT/DenseMap.h"
23
24namespace llvm {
25
26class TargetLowering;
27class DataLayout;
28
32 const TargetLowering &TL;
33 const DataLayout &DL;
34 unsigned MaxDepth;
35 /// Cache maintained during a computeKnownBits request.
36 SmallDenseMap<Register, KnownBits, 16> ComputeKnownBitsCache;
37
38 void computeKnownBitsMin(Register Src0, Register Src1, KnownBits &Known,
39 const APInt &DemandedElts,
40 unsigned Depth = 0);
41
42 unsigned computeNumSignBitsMin(Register Src0, Register Src1,
43 const APInt &DemandedElts, unsigned Depth = 0);
44
45public:
46 GISelKnownBits(MachineFunction &MF, unsigned MaxDepth = 6);
47 virtual ~GISelKnownBits() = default;
48
50 return MF;
51 }
52
53 const DataLayout &getDataLayout() const {
54 return DL;
55 }
56
57 virtual void computeKnownBitsImpl(Register R, KnownBits &Known,
58 const APInt &DemandedElts,
59 unsigned Depth = 0);
60
61 unsigned computeNumSignBits(Register R, const APInt &DemandedElts,
62 unsigned Depth = 0);
63 unsigned computeNumSignBits(Register R, unsigned Depth = 0);
64
65 // KnownBitsAPI
67 KnownBits getKnownBits(Register R, const APInt &DemandedElts,
68 unsigned Depth = 0);
69
70 // Calls getKnownBits for first operand def of MI.
74
75 /// \return true if 'V & Mask' is known to be zero in DemandedElts. We use
76 /// this predicate to simplify operations downstream.
77 /// Mask is known to be zero for bits that V cannot have.
78 bool maskedValueIsZero(Register Val, const APInt &Mask) {
79 return Mask.isSubsetOf(getKnownBits(Val).Zero);
80 }
81
82 /// \return true if the sign bit of Op is known to be zero. We use this
83 /// predicate to simplify operations downstream.
85
87 Align Alignment) {
88 // The low bits are known zero if the pointer is aligned.
89 Known.Zero.setLowBits(Log2(Alignment));
90 }
91
92 /// \return The known alignment for the pointer-like value \p R.
94
95 // Observer API. No-op for non-caching implementation.
96 void erasingInstr(MachineInstr &MI) override {}
97 void createdInstr(MachineInstr &MI) override {}
98 void changingInstr(MachineInstr &MI) override {}
99 void changedInstr(MachineInstr &MI) override {}
100
101protected:
102 unsigned getMaxDepth() const { return MaxDepth; }
103};
104
105/// To use KnownBitsInfo analysis in a pass,
106/// KnownBitsInfo &Info = getAnalysis<GISelKnownBitsInfoAnalysis>().get(MF);
107/// Add to observer if the Info is caching.
108/// WrapperObserver.addObserver(Info);
109
110/// Eventually add other features such as caching/ser/deserializing
111/// to MIR etc. Those implementations can derive from GISelKnownBits
112/// and override computeKnownBitsImpl.
114 std::unique_ptr<GISelKnownBits> Info;
115
116public:
117 static char ID;
120 }
122 void getAnalysisUsage(AnalysisUsage &AU) const override;
123 bool runOnMachineFunction(MachineFunction &MF) override;
124 void releaseMemory() override { Info.reset(); }
125};
126} // namespace llvm
127
128#endif // LLVM_CODEGEN_GLOBALISEL_GISELKNOWNBITS_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
This file defines the DenseMap class.
This contains common code to allow clients to notify changes to machine instr.
IRTranslator LLVM IR MI
Class for arbitrary precision integers.
Definition: APInt.h:76
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1367
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:110
Abstract class that contains various methods for clients to notify about changes.
To use KnownBitsInfo analysis in a pass, KnownBitsInfo &Info = getAnalysis<GISelKnownBitsInfoAnalysis...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
GISelKnownBits & get(MachineFunction &MF)
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
const DataLayout & getDataLayout() const
virtual void computeKnownBitsImpl(Register R, KnownBits &Known, const APInt &DemandedElts, unsigned Depth=0)
Align computeKnownAlignment(Register R, unsigned Depth=0)
static void computeKnownBitsForAlignment(KnownBits &Known, Align Alignment)
APInt getKnownOnes(Register R)
void erasingInstr(MachineInstr &MI) override
An instruction is about to be erased.
virtual ~GISelKnownBits()=default
void createdInstr(MachineInstr &MI) override
An instruction has been created and inserted into the function.
void changingInstr(MachineInstr &MI) override
This instruction is about to be mutated in some way.
unsigned computeNumSignBits(Register R, const APInt &DemandedElts, unsigned Depth=0)
const MachineFunction & getMachineFunction() const
KnownBits getKnownBits(Register R)
bool maskedValueIsZero(Register Val, const APInt &Mask)
unsigned getMaxDepth() const
void changedInstr(MachineInstr &MI) override
This instruction was mutated in some way.
bool signBitIsZero(Register Op)
APInt getKnownZeroes(Register R)
GISelKnownBits(MachineFunction &MF, unsigned MaxDepth=6)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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 is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeGISelKnownBitsAnalysisPass(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