LLVM 20.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);
46};
47
48char RISCVPostRAExpandPseudo::ID = 0;
49
50bool RISCVPostRAExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
51 TII = static_cast<const RISCVInstrInfo *>(MF.getSubtarget().getInstrInfo());
52 bool Modified = false;
53 for (auto &MBB : MF)
54 Modified |= expandMBB(MBB);
55 return Modified;
56}
57
58bool RISCVPostRAExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
59 bool Modified = false;
60
62 while (MBBI != E) {
63 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
64 Modified |= expandMI(MBB, MBBI, NMBBI);
65 MBBI = NMBBI;
66 }
67
68 return Modified;
69}
70
71bool RISCVPostRAExpandPseudo::expandMI(MachineBasicBlock &MBB,
74 switch (MBBI->getOpcode()) {
75 case RISCV::PseudoMovImm:
76 return expandMovImm(MBB, MBBI);
77 case RISCV::PseudoMovAddr:
78 return expandMovAddr(MBB, MBBI);
79 default:
80 return false;
81 }
82}
83
84bool RISCVPostRAExpandPseudo::expandMovImm(MachineBasicBlock &MBB,
86 DebugLoc DL = MBBI->getDebugLoc();
87
88 int64_t Val = MBBI->getOperand(1).getImm();
89
90 Register DstReg = MBBI->getOperand(0).getReg();
91 bool DstIsDead = MBBI->getOperand(0).isDead();
92 bool Renamable = MBBI->getOperand(0).isRenamable();
93
94 TII->movImm(MBB, MBBI, DL, DstReg, Val, MachineInstr::NoFlags, Renamable,
95 DstIsDead);
96
98 return true;
99}
100
101bool RISCVPostRAExpandPseudo::expandMovAddr(MachineBasicBlock &MBB,
103 DebugLoc DL = MBBI->getDebugLoc();
104
105 Register DstReg = MBBI->getOperand(0).getReg();
106 bool DstIsDead = MBBI->getOperand(0).isDead();
107 bool Renamable = MBBI->getOperand(0).isRenamable();
108
109 BuildMI(MBB, MBBI, DL, TII->get(RISCV::LUI))
110 .addReg(DstReg, RegState::Define | getRenamableRegState(Renamable))
111 .add(MBBI->getOperand(1));
112 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI))
113 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead) |
114 getRenamableRegState(Renamable))
115 .addReg(DstReg, RegState::Kill | getRenamableRegState(Renamable))
116 .add(MBBI->getOperand(2));
118 return true;
119}
120
121} // end of anonymous namespace
122
123INITIALIZE_PASS(RISCVPostRAExpandPseudo, "riscv-post-ra-expand-pseudo",
125namespace llvm {
126
128 return new RISCVPostRAExpandPseudo();
129}
130
131} // end of namespace llvm
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
const HexagonInstrInfo * TII
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
#define RISCV_POST_RA_EXPAND_PSEUDO_NAME
A debug info location.
Definition: DebugLoc.h:33
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
virtual const TargetInstrInfo * getInstrInfo() const
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ Define
Register definition.
@ Kill
The last use of a register.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass * createRISCVPostRAExpandPseudoPass()
unsigned getDeadRegState(bool B)
unsigned getRenamableRegState(bool B)