LLVM 24.0.0git
RegBankSelect.h
Go to the documentation of this file.
1//=- llvm/CodeGen/GlobalISel/RegBankSelect.h - Reg Bank Selector --*- 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
10/// This file describes the interface of the MachineFunctionPass
11/// responsible for assigning the generic virtual registers to register bank.
12///
13/// By default, the reg bank selector relies on local decisions to
14/// assign the register bank. In other words, it looks at one instruction
15/// at a time to decide where the operand of that instruction should live.
16///
17/// At higher optimization level, we could imagine that the reg bank selector
18/// would use more global analysis and do crazier thing like duplicating
19/// instructions and so on. This is future work.
20///
21/// For now, the pass uses a greedy algorithm to decide where the operand
22/// of an instruction should live. It asks the target which banks may be
23/// used for each operand of the instruction and what is the cost. Then,
24/// it chooses the solution which minimize the cost of the instruction plus
25/// the cost of any move that may be needed to the values into the right
26/// register bank.
27/// In other words, the cost for an instruction on a register bank RegBank
28/// is: Cost of I on RegBank plus the sum of the cost for bringing the
29/// input operands from their current register bank to RegBank.
30/// Thus, the following formula:
31/// cost(I, RegBank) = cost(I.Opcode, RegBank) +
32/// sum(for each arg in I.arguments: costCrossCopy(arg.RegBank, RegBank))
33///
34/// E.g., Let say we are assigning the register bank for the instruction
35/// defining v2.
36/// v0(A_REGBANK) = ...
37/// v1(A_REGBANK) = ...
38/// v2 = G_ADD i32 v0, v1 <-- MI
39///
40/// The target may say it can generate G_ADD i32 on register bank A and B
41/// with a cost of respectively 5 and 1.
42/// Then, let say the cost of a cross register bank copies from A to B is 1.
43/// The reg bank selector would compare the following two costs:
44/// cost(MI, A_REGBANK) = cost(G_ADD, A_REGBANK) + cost(v0.RegBank, A_REGBANK) +
45/// cost(v1.RegBank, A_REGBANK)
46/// = 5 + cost(A_REGBANK, A_REGBANK) + cost(A_REGBANK,
47/// A_REGBANK)
48/// = 5 + 0 + 0 = 5
49/// cost(MI, B_REGBANK) = cost(G_ADD, B_REGBANK) + cost(v0.RegBank, B_REGBANK) +
50/// cost(v1.RegBank, B_REGBANK)
51/// = 1 + cost(A_REGBANK, B_REGBANK) + cost(A_REGBANK,
52/// B_REGBANK)
53/// = 1 + 1 + 1 = 3
54/// Therefore, in this specific example, the reg bank selector would choose
55/// bank B for MI.
56/// v0(A_REGBANK) = ...
57/// v1(A_REGBANK) = ...
58/// tmp0(B_REGBANK) = COPY v0
59/// tmp1(B_REGBANK) = COPY v1
60/// v2(B_REGBANK) = G_ADD i32 tmp0, tmp1
61//
62//===----------------------------------------------------------------------===//
63
64#ifndef LLVM_CODEGEN_GLOBALISEL_REGBANKSELECT_H
65#define LLVM_CODEGEN_GLOBALISEL_REGBANKSELECT_H
66
67#include "llvm/ADT/StringRef.h"
71#include "llvm/IR/Analysis.h"
72#include "llvm/IR/PassManager.h"
73
74namespace llvm {
75
76/// List of the modes supported by the RegBankSelect pass.
78 /// Assign the register banks as fast as possible (default).
80 /// Greedily minimize the cost of assigning register banks.
81 /// This should produce code of greater quality, but will
82 /// require more compile time.
84};
85
86/// This pass implements the reg bank selector pass used in the GlobalISel
87/// pipeline. At the end of this pass, all register operands have been assigned
89 RegBankSelectMode OptMode;
90
91public:
92 static char ID;
93
95
96 StringRef getPassName() const override { return "RegBankSelect"; }
97
98 void getAnalysisUsage(AnalysisUsage &AU) const override;
99
101 return MachineFunctionProperties().setIsSSA().setLegalized();
102 }
103
105 return MachineFunctionProperties().setRegBankSelected();
106 }
107
109 return MachineFunctionProperties().setNoPHIs();
110 }
111
112 bool runOnMachineFunction(MachineFunction &MF) override;
113};
114
115class RegBankSelectPass : public RequiredPassInfoMixin<RegBankSelectPass> {
116 RegBankSelectMode OptMode;
117
118public:
122
124 return MachineFunctionProperties().setIsSSA().setLegalized();
125 }
126
128 return MachineFunctionProperties().setRegBankSelected();
129 }
130
134};
135
136} // end namespace llvm
137
138#endif // LLVM_CODEGEN_GLOBALISEL_REGBANKSELECT_H
#define LLVM_ABI
Definition Compiler.h:215
This header defines various interfaces for pass management in LLVM.
Represent the analysis usage information of a pass.
Properties which a MachineFunction may have at a given point in time.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
MachineFunctionProperties getRequiredProperties() const override
MachineFunctionProperties getClearedProperties() const override
MachineFunctionProperties getSetProperties() const override
RegBankSelectLegacy(RegBankSelectMode RunningMode=RegBankSelectMode::Fast)
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
MachineFunctionProperties getRequiredProperties() const
MachineFunctionProperties getClearedProperties() const
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
RegBankSelectPass(RegBankSelectMode RunningMode=RegBankSelectMode::Fast)
MachineFunctionProperties getSetProperties() const
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Pass manager infrastructure for declaring and invalidating analyses.
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
RegBankSelectMode
List of the modes supported by the RegBankSelect pass.
@ Greedy
Greedily minimize the cost of assigning register banks.
@ Fast
Assign the register banks as fast as possible (default).
A CRTP mix-in for passes that should not be skipped.