blob: 6946aadb01a03af9b4c0c7b88152425f27a42f65 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//==-- llvm/CodeGen/GlobalISel/Utils.h ---------------------------*- C++ -*-==//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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"
Andrew Walbran3d2c1972020-04-07 12:24:26 +010018#include "llvm/CodeGen/Register.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010019
20namespace llvm {
21
Andrew Scullcdfcccc2018-10-05 20:58:37 +010022class AnalysisUsage;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010023class MachineFunction;
24class MachineInstr;
25class MachineOperand;
26class MachineOptimizationRemarkEmitter;
27class MachineOptimizationRemarkMissed;
28class MachineRegisterInfo;
29class MCInstrDesc;
30class RegisterBankInfo;
31class TargetInstrInfo;
32class TargetPassConfig;
33class TargetRegisterInfo;
34class TargetRegisterClass;
35class Twine;
36class ConstantFP;
37class APFloat;
38
39/// Try to constrain Reg to the specified register class. If this fails,
Andrew Walbran3d2c1972020-04-07 12:24:26 +010040/// create a new virtual register in the correct class.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010041///
42/// \return The virtual register constrained to the right register class.
43unsigned constrainRegToClass(MachineRegisterInfo &MRI,
44 const TargetInstrInfo &TII,
Andrew Walbran3d2c1972020-04-07 12:24:26 +010045 const RegisterBankInfo &RBI, unsigned Reg,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010046 const TargetRegisterClass &RegClass);
47
Andrew Walbran3d2c1972020-04-07 12:24:26 +010048/// Constrain the Register operand OpIdx, so that it is now constrained to the
49/// TargetRegisterClass passed as an argument (RegClass).
50/// If this fails, create a new virtual register in the correct class and
51/// insert a COPY before \p InsertPt if it is a use or after if it is a
52/// definition. The debug location of \p InsertPt is used for the new copy.
53///
54/// \return The virtual register constrained to the right register class.
55unsigned constrainOperandRegClass(const MachineFunction &MF,
56 const TargetRegisterInfo &TRI,
57 MachineRegisterInfo &MRI,
58 const TargetInstrInfo &TII,
59 const RegisterBankInfo &RBI,
60 MachineInstr &InsertPt,
61 const TargetRegisterClass &RegClass,
62 const MachineOperand &RegMO, unsigned OpIdx);
63
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010064/// Try to constrain Reg so that it is usable by argument OpIdx of the
65/// provided MCInstrDesc \p II. If this fails, create a new virtual
Andrew Walbran3d2c1972020-04-07 12:24:26 +010066/// register in the correct class and insert a COPY before \p InsertPt
67/// if it is a use or after if it is a definition.
68/// This is equivalent to constrainOperandRegClass(..., RegClass, ...)
69/// with RegClass obtained from the MCInstrDesc. The debug location of \p
70/// InsertPt is used for the new copy.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010071///
72/// \return The virtual register constrained to the right register class.
73unsigned constrainOperandRegClass(const MachineFunction &MF,
74 const TargetRegisterInfo &TRI,
75 MachineRegisterInfo &MRI,
76 const TargetInstrInfo &TII,
77 const RegisterBankInfo &RBI,
78 MachineInstr &InsertPt, const MCInstrDesc &II,
79 const MachineOperand &RegMO, unsigned OpIdx);
80
81/// Mutate the newly-selected instruction \p I to constrain its (possibly
82/// generic) virtual register operands to the instruction's register class.
83/// This could involve inserting COPYs before (for uses) or after (for defs).
84/// This requires the number of operands to match the instruction description.
85/// \returns whether operand regclass constraining succeeded.
86///
87// FIXME: Not all instructions have the same number of operands. We should
88// probably expose a constrain helper per operand and let the target selector
89// constrain individual registers, like fast-isel.
90bool constrainSelectedInstRegOperands(MachineInstr &I,
91 const TargetInstrInfo &TII,
92 const TargetRegisterInfo &TRI,
93 const RegisterBankInfo &RBI);
94/// Check whether an instruction \p MI is dead: it only defines dead virtual
95/// registers, and doesn't have other side effects.
96bool isTriviallyDead(const MachineInstr &MI, const MachineRegisterInfo &MRI);
97
98/// Report an ISel error as a missed optimization remark to the LLVMContext's
99/// diagnostic stream. Set the FailedISel MachineFunction property.
100void reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
101 MachineOptimizationRemarkEmitter &MORE,
102 MachineOptimizationRemarkMissed &R);
103
104void reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
105 MachineOptimizationRemarkEmitter &MORE,
106 const char *PassName, StringRef Msg,
107 const MachineInstr &MI);
108
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100109/// If \p VReg is defined by a G_CONSTANT fits in int64_t
110/// returns it.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100111Optional<int64_t> getConstantVRegVal(unsigned VReg,
112 const MachineRegisterInfo &MRI);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100113/// Simple struct used to hold a constant integer value and a virtual
114/// register.
115struct ValueAndVReg {
116 int64_t Value;
117 unsigned VReg;
118};
119/// If \p VReg is defined by a statically evaluable chain of
120/// instructions rooted on a G_CONSTANT (\p LookThroughInstrs == true)
121/// and that constant fits in int64_t, returns its value as well as
122/// the virtual register defined by this G_CONSTANT.
123/// When \p LookThroughInstrs == false, this function behaves like
124/// getConstantVRegVal.
125Optional<ValueAndVReg>
126getConstantVRegValWithLookThrough(unsigned VReg, const MachineRegisterInfo &MRI,
127 bool LookThroughInstrs = true);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100128const ConstantFP* getConstantFPVRegVal(unsigned VReg,
129 const MachineRegisterInfo &MRI);
130
131/// See if Reg is defined by an single def instruction that is
132/// Opcode. Also try to do trivial folding if it's a COPY with
133/// same types. Returns null otherwise.
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100134MachineInstr *getOpcodeDef(unsigned Opcode, Register Reg,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100135 const MachineRegisterInfo &MRI);
136
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100137/// Find the def instruction for \p Reg, folding away any trivial copies. Note
138/// it may still return a COPY, if it changes the type. May return nullptr if \p
139/// Reg is not a generic virtual register.
140MachineInstr *getDefIgnoringCopies(Register Reg,
141 const MachineRegisterInfo &MRI);
142
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100143/// Returns an APFloat from Val converted to the appropriate size.
144APFloat getAPFloatFromSize(double Val, unsigned Size);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100145
146/// Modify analysis usage so it preserves passes required for the SelectionDAG
147/// fallback.
148void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU);
149
Andrew Walbran16937d02019-10-22 13:54:20 +0100150Optional<APInt> ConstantFoldBinOp(unsigned Opcode, const unsigned Op1,
151 const unsigned Op2,
152 const MachineRegisterInfo &MRI);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100153} // End namespace llvm.
154#endif