LLVM  14.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"
20 #include "llvm/CodeGen/Register.h"
21 #include "llvm/InitializePasses.h"
22 #include "llvm/Support/KnownBits.h"
23 
24 namespace llvm {
25 
26 class TargetLowering;
27 class DataLayout;
28 
30  MachineFunction &MF;
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 
45 public:
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.
93  Align computeKnownAlignment(Register R, unsigned Depth = 0);
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 
101 protected:
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 
116 public:
117  static char ID;
120  }
122  if (!Info)
123  Info = std::make_unique<GISelKnownBits>(MF);
124  return *Info.get();
125  }
126  void getAnalysisUsage(AnalysisUsage &AU) const override;
127  bool runOnMachineFunction(MachineFunction &MF) override;
128  void releaseMemory() override { Info.reset(); }
129 };
130 } // namespace llvm
131 
132 #endif // LLVM_CODEGEN_GLOBALISEL_GISELKNOWNBITS_H
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:103
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::GISelKnownBits::maskedValueIsZero
bool maskedValueIsZero(Register Val, const APInt &Mask)
Definition: GISelKnownBits.h:78
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:112
llvm::GISelKnownBits
Definition: GISelKnownBits.h:29
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition: MachineRegisterInfo.h:52
llvm::GISelKnownBitsAnalysis::GISelKnownBitsAnalysis
GISelKnownBitsAnalysis()
Definition: GISelKnownBits.h:118
llvm::GISelKnownBits::getMachineFunction
const MachineFunction & getMachineFunction() const
Definition: GISelKnownBits.h:49
llvm::KnownBits::Zero
APInt Zero
Definition: KnownBits.h:24
llvm::SmallDenseMap
Definition: DenseMap.h:880
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
llvm::Depth
@ Depth
Definition: SIMachineScheduler.h:36
DenseMap.h
llvm::GISelKnownBits::signBitIsZero
bool signBitIsZero(Register Op)
Definition: GISelKnownBits.cpp:75
llvm::BitmaskEnumDetail::Mask
std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:80
llvm::GISelKnownBits::~GISelKnownBits
virtual ~GISelKnownBits()=default
llvm::GISelKnownBits::GISelKnownBits
GISelKnownBits(MachineFunction &MF, unsigned MaxDepth=6)
KnownBits.h
llvm::GISelKnownBitsAnalysis::ID
static char ID
Definition: GISelKnownBits.h:117
llvm::GISelKnownBitsAnalysis::releaseMemory
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Definition: GISelKnownBits.h:128
llvm::GISelKnownBitsAnalysis
To use KnownBitsInfo analysis in a pass, KnownBitsInfo &Info = getAnalysis<GISelKnownBitsInfoAnalysis...
Definition: GISelKnownBits.h:113
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::GISelKnownBits::getKnownBits
KnownBits getKnownBits(Register R)
Definition: GISelKnownBits.cpp:57
llvm::GISelKnownBitsAnalysis::get
GISelKnownBits & get(MachineFunction &MF)
Definition: GISelKnownBits.h:121
llvm::Log2
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:207
llvm::TargetLowering
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
Definition: TargetLowering.h:3189
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::APInt::setLowBits
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1331
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::GISelKnownBits::computeKnownBitsForAlignment
static void computeKnownBitsForAlignment(KnownBits &Known, Align Alignment)
Definition: GISelKnownBits.h:86
llvm::GISelKnownBits::computeNumSignBits
unsigned computeNumSignBits(Register R, const APInt &DemandedElts, unsigned Depth=0)
Definition: GISelKnownBits.cpp:573
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
llvm::GISelKnownBits::getMaxDepth
unsigned getMaxDepth() const
Definition: GISelKnownBits.h:102
llvm::GISelKnownBits::getKnownZeroes
APInt getKnownZeroes(Register R)
Definition: GISelKnownBits.cpp:81
MachineFunctionPass.h
llvm::GISelKnownBits::getKnownOnes
APInt getKnownOnes(Register R)
Definition: GISelKnownBits.cpp:85
llvm::GISelKnownBitsAnalysis::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: GISelKnownBits.cpp:696
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:75
llvm::MachineFunction
Definition: MachineFunction.h:230
llvm::initializeGISelKnownBitsAnalysisPass
void initializeGISelKnownBitsAnalysisPass(PassRegistry &)
llvm::GISelKnownBits::computeKnownAlignment
Align computeKnownAlignment(Register R, unsigned Depth=0)
Definition: GISelKnownBits.cpp:35
llvm::GISelKnownBits::erasingInstr
void erasingInstr(MachineInstr &MI) override
An instruction is about to be erased.
Definition: GISelKnownBits.h:96
llvm::GISelKnownBits::createdInstr
void createdInstr(MachineInstr &MI) override
An instruction has been created and inserted into the function.
Definition: GISelKnownBits.h:97
llvm::GISelKnownBits::changingInstr
void changingInstr(MachineInstr &MI) override
This instruction is about to be mutated in some way.
Definition: GISelKnownBits.h:98
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::GISelChangeObserver
Abstract class that contains various methods for clients to notify about changes.
Definition: GISelChangeObserver.h:29
llvm::Register
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
llvm::GISelKnownBits::getDataLayout
const DataLayout & getDataLayout() const
Definition: GISelKnownBits.h:53
llvm::KnownBits
Definition: KnownBits.h:23
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:321
llvm::GISelKnownBitsAnalysis::runOnMachineFunction
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: GISelKnownBits.cpp:701
llvm::GISelKnownBits::changedInstr
void changedInstr(MachineInstr &MI) override
This instruction was mutated in some way.
Definition: GISelKnownBits.h:99
llvm::GISelKnownBits::computeKnownBitsImpl
virtual void computeKnownBitsImpl(Register R, KnownBits &Known, const APInt &DemandedElts, unsigned Depth=0)
Definition: GISelKnownBits.cpp:131
Register.h
GISelChangeObserver.h
InitializePasses.h