LLVM 23.0.0git
RISCVPostRAExpandPseudoInsts.cpp
Go to the documentation of this file.
1//===-- RISCVPostRAExpandPseudoInsts.cpp - Expand pseudo instrs ----===//
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// This file contains a pass that expands the pseudo instruction pseudolisimm32
10// into target instructions. This pass should be run during the post-regalloc
11// passes, before post RA scheduling.
12//
13//===----------------------------------------------------------------------===//
14
15#include "RISCV.h"
16#include "RISCVInstrInfo.h"
19
20using namespace llvm;
21
22#define RISCV_POST_RA_EXPAND_PSEUDO_NAME \
23 "RISC-V post-regalloc pseudo instruction expansion pass"
24
25namespace {
26
27class RISCVPostRAExpandPseudo : public MachineFunctionPass {
28public:
29 const RISCVInstrInfo *TII;
30 static char ID;
31
32 RISCVPostRAExpandPseudo() : MachineFunctionPass(ID) {}
33
34 bool runOnMachineFunction(MachineFunction &MF) override;
35
36 StringRef getPassName() const override {
38 }
39
40private:
41 bool expandMBB(MachineBasicBlock &MBB);
47};
48
49char RISCVPostRAExpandPseudo::ID = 0;
50
51bool RISCVPostRAExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
52 TII = static_cast<const RISCVInstrInfo *>(MF.getSubtarget().getInstrInfo());
53 bool Modified = false;
54 for (auto &MBB : MF)
55 Modified |= expandMBB(MBB);
56 return Modified;
57}
58
59bool RISCVPostRAExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
60 bool Modified = false;
61
62 MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
63 while (MBBI != E) {
64 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
65 Modified |= expandMI(MBB, MBBI, NMBBI);
66 MBBI = NMBBI;
67 }
68
69 return Modified;
70}
71
72bool RISCVPostRAExpandPseudo::expandMI(MachineBasicBlock &MBB,
75 switch (MBBI->getOpcode()) {
76 case RISCV::PseudoMovImm:
77 return expandMovImm(MBB, MBBI);
78 case RISCV::PseudoMovAddr:
79 return expandMovAddr(MBB, MBBI);
80 case RISCV::PseudoMERGE:
81 return expandMERGE(MBB, MBBI);
82 default:
83 return false;
84 }
85}
86
87bool RISCVPostRAExpandPseudo::expandMovImm(MachineBasicBlock &MBB,
89 DebugLoc DL = MBBI->getDebugLoc();
90
91 int64_t Val = MBBI->getOperand(1).getImm();
92
93 Register DstReg = MBBI->getOperand(0).getReg();
94 bool DstIsDead = MBBI->getOperand(0).isDead();
95 bool Renamable = MBBI->getOperand(0).isRenamable();
96
97 TII->movImm(MBB, MBBI, DL, DstReg, Val, MachineInstr::NoFlags, Renamable,
98 DstIsDead);
99
100 MBBI->eraseFromParent();
101 return true;
102}
103
104bool RISCVPostRAExpandPseudo::expandMovAddr(MachineBasicBlock &MBB,
106 DebugLoc DL = MBBI->getDebugLoc();
107
108 Register DstReg = MBBI->getOperand(0).getReg();
109 bool DstIsDead = MBBI->getOperand(0).isDead();
110 bool Renamable = MBBI->getOperand(0).isRenamable();
111
112 BuildMI(MBB, MBBI, DL, TII->get(RISCV::LUI))
114 .add(MBBI->getOperand(1));
115 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI))
116 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead) |
119 .add(MBBI->getOperand(2));
120 MBBI->eraseFromParent();
121 return true;
122}
123
124/// Transfer implicit operands on the pseudo instruction to the
125/// instructions created from the expansion.
126static void transferImpOps(MachineInstr &OldMI, MachineInstrBuilder &MI) {
127 const MCInstrDesc &Desc = OldMI.getDesc();
128 for (const MachineOperand &MO :
129 llvm::drop_begin(OldMI.operands(), Desc.getNumOperands())) {
130 assert(MO.isReg() && MO.getReg());
131 MI.add(MO);
132 }
133}
134
135// Expand PseudoMERGE to MERGE, MVM, or MVMN.
136bool RISCVPostRAExpandPseudo::expandMERGE(MachineBasicBlock &MBB,
138 MachineInstr &MI = *MBBI;
139 DebugLoc DL = MI.getDebugLoc();
140
141 Register DstReg = MI.getOperand(0).getReg();
142 if (DstReg == MI.getOperand(3).getReg()) {
143 // Expand to MVMN
144 auto I = BuildMI(MBB, MBBI, DL, TII->get(RISCV::MVMN))
145 .add(MI.getOperand(0))
146 .add(MI.getOperand(3))
147 .add(MI.getOperand(2))
148 .add(MI.getOperand(1));
149 transferImpOps(*MBBI, I);
150 } else if (DstReg == MBBI->getOperand(2).getReg()) {
151 // Expand to MVM
152 auto I = BuildMI(MBB, MBBI, DL, TII->get(RISCV::MVM))
153 .add(MI.getOperand(0))
154 .add(MI.getOperand(2))
155 .add(MI.getOperand(3))
156 .add(MI.getOperand(1));
157 transferImpOps(*MBBI, I);
158 } else if (DstReg == MI.getOperand(1).getReg()) {
159 // Expand to MERGE
160 auto I = BuildMI(MBB, MBBI, DL, TII->get(RISCV::MERGE))
161 .add(MI.getOperand(0))
162 .add(MI.getOperand(1))
163 .add(MI.getOperand(2))
164 .add(MI.getOperand(3));
165 transferImpOps(*MBBI, I);
166 } else {
167 // Use an additional move.
169 getRenamableRegState(MI.getOperand(1).isRenamable()) |
170 getKillRegState(MI.getOperand(1).isKill() &&
171 MI.getOperand(1).getReg() !=
172 MI.getOperand(2).getReg() &&
173 MI.getOperand(1).getReg() != MI.getOperand(3).getReg());
174 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(RISCV::ADDI))
175 .addDef(DstReg, getRenamableRegState(MI.getOperand(0).isRenamable()))
176 .addReg(MI.getOperand(1).getReg(), RegState)
177 .addImm(0);
178 auto I = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(RISCV::MERGE))
179 .add(MI.getOperand(0))
180 .addReg(DstReg,
182 MI.getOperand(0).isRenamable()))
183 .add(MI.getOperand(2))
184 .add(MI.getOperand(3));
185 transferImpOps(*MBBI, I);
186 }
187 MI.eraseFromParent();
188 return true;
189}
190
191} // end of anonymous namespace
192
193INITIALIZE_PASS(RISCVPostRAExpandPseudo, "riscv-post-ra-expand-pseudo",
195namespace llvm {
196
198 return new RISCVPostRAExpandPseudo();
199}
200
201} // end of namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
#define RISCV_POST_RA_EXPAND_PSEUDO_NAME
A debug info location.
Definition DebugLoc.h:123
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
Describe properties that are true of each instruction in the target description file.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
mop_range operands()
MachineOperand class - Representation of each machine instruction operand.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
RegState
Flags to represent properties of register accesses.
@ Kill
The last use of a register.
@ Define
Register definition.
@ Renamable
Register that may be renamed.
constexpr RegState getKillRegState(bool B)
FunctionPass * createRISCVPostRAExpandPseudoPass()
constexpr RegState getDeadRegState(bool B)
Op::Description Desc
constexpr RegState getRenamableRegState(bool B)