blob: dd14a0cf87ee32fcd4647e145911ad41175965f4 [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"
18
19namespace llvm {
20
Andrew Scullcdfcccc2018-10-05 20:58:37 +010021class AnalysisUsage;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010022class MachineFunction;
23class MachineInstr;
24class MachineOperand;
25class MachineOptimizationRemarkEmitter;
26class MachineOptimizationRemarkMissed;
27class MachineRegisterInfo;
28class MCInstrDesc;
29class RegisterBankInfo;
30class TargetInstrInfo;
31class TargetPassConfig;
32class TargetRegisterInfo;
33class TargetRegisterClass;
34class Twine;
35class ConstantFP;
36class 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.
43unsigned 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.
56unsigned 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.
73bool 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.
79bool 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.
83void reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
84 MachineOptimizationRemarkEmitter &MORE,
85 MachineOptimizationRemarkMissed &R);
86
87void reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
88 MachineOptimizationRemarkEmitter &MORE,
89 const char *PassName, StringRef Msg,
90 const MachineInstr &MI);
91
92Optional<int64_t> getConstantVRegVal(unsigned VReg,
93 const MachineRegisterInfo &MRI);
94const ConstantFP* getConstantFPVRegVal(unsigned VReg,
95 const MachineRegisterInfo &MRI);
96
97/// See if Reg is defined by an single def instruction that is
98/// Opcode. Also try to do trivial folding if it's a COPY with
99/// same types. Returns null otherwise.
100MachineInstr *getOpcodeDef(unsigned Opcode, unsigned Reg,
101 const MachineRegisterInfo &MRI);
102
103/// Returns an APFloat from Val converted to the appropriate size.
104APFloat getAPFloatFromSize(double Val, unsigned Size);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100105
106/// Modify analysis usage so it preserves passes required for the SelectionDAG
107/// fallback.
108void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU);
109
Andrew Walbran16937d02019-10-22 13:54:20 +0100110Optional<APInt> ConstantFoldBinOp(unsigned Opcode, const unsigned Op1,
111 const unsigned Op2,
112 const MachineRegisterInfo &MRI);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100113} // End namespace llvm.
114#endif