LLVM  9.0.0svn
Utils.h
Go to the documentation of this file.
1 //==-- llvm/CodeGen/GlobalISel/Utils.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 //
9 /// \file This file declares the API of helper functions used throughout the
10 /// GlobalISel pipeline.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CODEGEN_GLOBALISEL_UTILS_H
15 #define LLVM_CODEGEN_GLOBALISEL_UTILS_H
16 
17 #include "llvm/ADT/StringRef.h"
18 
19 namespace llvm {
20 
21 class AnalysisUsage;
22 class MachineFunction;
23 class MachineInstr;
24 class MachineOperand;
25 class MachineOptimizationRemarkEmitter;
26 class MachineOptimizationRemarkMissed;
27 class MachineRegisterInfo;
28 class MCInstrDesc;
29 class RegisterBankInfo;
30 class TargetInstrInfo;
31 class TargetPassConfig;
32 class TargetRegisterInfo;
33 class TargetRegisterClass;
34 class Twine;
35 class ConstantFP;
36 class APFloat;
37 
38 /// Try to constrain Reg to the specified register class. If this fails,
39 /// create a new virtual register in the correct class and insert a COPY before
40 /// \p InsertPt. The debug location of \p InsertPt is used for the new copy.
41 ///
42 /// \return The virtual register constrained to the right register class.
43 unsigned constrainRegToClass(MachineRegisterInfo &MRI,
44  const TargetInstrInfo &TII,
45  const RegisterBankInfo &RBI,
46  MachineInstr &InsertPt, unsigned Reg,
47  const TargetRegisterClass &RegClass);
48 
49 /// Try to constrain Reg so that it is usable by argument OpIdx of the
50 /// provided MCInstrDesc \p II. If this fails, create a new virtual
51 /// register in the correct class and insert a COPY before \p InsertPt.
52 /// This is equivalent to constrainRegToClass() with RegClass obtained from the
53 /// MCInstrDesc. The debug location of \p InsertPt is used for the new copy.
54 ///
55 /// \return The virtual register constrained to the right register class.
56 unsigned constrainOperandRegClass(const MachineFunction &MF,
57  const TargetRegisterInfo &TRI,
58  MachineRegisterInfo &MRI,
59  const TargetInstrInfo &TII,
60  const RegisterBankInfo &RBI,
61  MachineInstr &InsertPt, const MCInstrDesc &II,
62  const MachineOperand &RegMO, unsigned OpIdx);
63 
64 /// Mutate the newly-selected instruction \p I to constrain its (possibly
65 /// generic) virtual register operands to the instruction's register class.
66 /// This could involve inserting COPYs before (for uses) or after (for defs).
67 /// This requires the number of operands to match the instruction description.
68 /// \returns whether operand regclass constraining succeeded.
69 ///
70 // FIXME: Not all instructions have the same number of operands. We should
71 // probably expose a constrain helper per operand and let the target selector
72 // constrain individual registers, like fast-isel.
73 bool constrainSelectedInstRegOperands(MachineInstr &I,
74  const TargetInstrInfo &TII,
75  const TargetRegisterInfo &TRI,
76  const RegisterBankInfo &RBI);
77 /// Check whether an instruction \p MI is dead: it only defines dead virtual
78 /// registers, and doesn't have other side effects.
79 bool isTriviallyDead(const MachineInstr &MI, const MachineRegisterInfo &MRI);
80 
81 /// Report an ISel error as a missed optimization remark to the LLVMContext's
82 /// diagnostic stream. Set the FailedISel MachineFunction property.
83 void reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
84  MachineOptimizationRemarkEmitter &MORE,
85  MachineOptimizationRemarkMissed &R);
86 
87 void reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
88  MachineOptimizationRemarkEmitter &MORE,
89  const char *PassName, StringRef Msg,
90  const MachineInstr &MI);
91 
92 /// If \p VReg is defined by a G_CONSTANT fits in int64_t
93 /// returns it.
94 Optional<int64_t> getConstantVRegVal(unsigned VReg,
95  const MachineRegisterInfo &MRI);
96 /// Simple struct used to hold a constant integer value and a virtual
97 /// register.
98 struct ValueAndVReg {
99  int64_t Value;
100  unsigned VReg;
101 };
102 /// If \p VReg is defined by a statically evaluable chain of
103 /// instructions rooted on a G_CONSTANT (\p LookThroughInstrs == true)
104 /// and that constant fits in int64_t, returns its value as well as
105 /// the virtual register defined by this G_CONSTANT.
106 /// When \p LookThroughInstrs == false, this function behaves like
107 /// getConstantVRegVal.
110  bool LookThroughInstrs = true);
111 const ConstantFP* getConstantFPVRegVal(unsigned VReg,
112  const MachineRegisterInfo &MRI);
113 
114 /// See if Reg is defined by an single def instruction that is
115 /// Opcode. Also try to do trivial folding if it's a COPY with
116 /// same types. Returns null otherwise.
117 MachineInstr *getOpcodeDef(unsigned Opcode, unsigned Reg,
118  const MachineRegisterInfo &MRI);
119 
120 /// Returns an APFloat from Val converted to the appropriate size.
121 APFloat getAPFloatFromSize(double Val, unsigned Size);
122 
123 /// Modify analysis usage so it preserves passes required for the SelectionDAG
124 /// fallback.
126 
127 Optional<APInt> ConstantFoldBinOp(unsigned Opcode, const unsigned Op1,
128  const unsigned Op2,
129  const MachineRegisterInfo &MRI);
130 } // End namespace llvm.
131 #endif
Simple struct used to hold a constant integer value and a virtual register.
Definition: Utils.h:98
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU)
Modify analysis usage so it preserves passes required for the SelectionDAG fallback.
Definition: Utils.cpp:338
const ConstantFP * getConstantFPVRegVal(unsigned VReg, const MachineRegisterInfo &MRI)
Definition: Utils.cpp:250
unsigned Reg
unsigned const TargetRegisterInfo * TRI
int64_t Value
Definition: Utils.h:99
const HexagonInstrInfo * TII
Optional< APInt > ConstantFoldBinOp(unsigned Opcode, const unsigned Op1, const unsigned Op2, const MachineRegisterInfo &MRI)
Definition: Utils.cpp:287
APFloat getAPFloatFromSize(double Val, unsigned Size)
Returns an APFloat from Val converted to the appropriate size.
Definition: Utils.cpp:274
MachineInstr * getOpcodeDef(unsigned Opcode, unsigned Reg, const MachineRegisterInfo &MRI)
See if Reg is defined by an single def instruction that is Opcode.
Definition: Utils.cpp:258
unsigned const MachineRegisterInfo * MRI
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:263
Represent the analysis usage information of a pass.
unsigned VReg
Definition: Utils.h:100
unsigned constrainRegToClass(MachineRegisterInfo &MRI, const TargetInstrInfo &TII, const RegisterBankInfo &RBI, MachineInstr &InsertPt, unsigned Reg, const TargetRegisterClass &RegClass)
Try to constrain Reg to the specified register class.
Definition: Utils.cpp:30
Optional< ValueAndVReg > getConstantVRegValWithLookThrough(unsigned VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs=true)
If VReg is defined by a statically evaluable chain of instructions rooted on a G_CONSTANT (LookThroug...
Definition: Utils.cpp:195
#define MORE()
Definition: regcomp.c:251
bool constrainSelectedInstRegOperands(MachineInstr &I, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI)
Mutate the newly-selected instruction I to constrain its (possibly generic) virtual register operands...
Definition: Utils.cpp:87
RegisterBankInfo(RegisterBank **RegBanks, unsigned NumRegBanks)
Create a RegisterBankInfo that can accommodate up to NumRegBanks RegisterBank instances.
Optional< int64_t > getConstantVRegVal(unsigned VReg, const MachineRegisterInfo &MRI)
If VReg is defined by a G_CONSTANT fits in int64_t returns it.
Definition: Utils.cpp:184
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
bool isTriviallyDead(const MachineInstr &MI, const MachineRegisterInfo &MRI)
Check whether an instruction MI is dead: it only defines dead virtual registers, and doesn&#39;t have oth...
Definition: Utils.cpp:134
Representation of each machine instruction.
Definition: MachineInstr.h:63
#define I(x, y, z)
Definition: MD5.cpp:58
uint32_t Size
Definition: Profile.cpp:46
IRTranslator LLVM IR MI
unsigned constrainOperandRegClass(const MachineFunction &MF, const TargetRegisterInfo &TRI, MachineRegisterInfo &MRI, const TargetInstrInfo &TII, const RegisterBankInfo &RBI, MachineInstr &InsertPt, const MCInstrDesc &II, const MachineOperand &RegMO, unsigned OpIdx)
Try to constrain Reg so that it is usable by argument OpIdx of the provided MCInstrDesc II...
Definition: Utils.cpp:46
void reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC, MachineOptimizationRemarkEmitter &MORE, MachineOptimizationRemarkMissed &R)
Report an ISel error as a missed optimization remark to the LLVMContext&#39;s diagnostic stream...
Definition: Utils.cpp:155