LLVM 24.0.0git
RISCVInsertReadWriteCSR.cpp
Go to the documentation of this file.
1//===-- RISCVInsertReadWriteCSR.cpp - Insert Read/Write of RISC-V CSR -----===//
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// This file implements the machine function pass to insert read/write of CSR-s
9// of the RISC-V instructions.
10//
11// Currently the pass implements:
12// -Writing and saving frm before an RVV floating-point instruction with a
13// static rounding mode and restores the value after.
14//
15//===----------------------------------------------------------------------===//
16
18#include "RISCV.h"
19#include "RISCVSubtarget.h"
23#include "llvm/IR/Analysis.h"
24using namespace llvm;
25
26#define DEBUG_TYPE "riscv-insert-read-write-csr"
27#define RISCV_INSERT_READ_WRITE_CSR_NAME "RISC-V Insert Read/Write CSR Pass"
28
29namespace {
30
31class RISCVInsertReadWriteCSRLegacy : public MachineFunctionPass {
32public:
33 static char ID;
34
35 RISCVInsertReadWriteCSRLegacy() : MachineFunctionPass(ID) {}
36
37 bool runOnMachineFunction(MachineFunction &MF) override;
38
39 void getAnalysisUsage(AnalysisUsage &AU) const override {
40 AU.setPreservesCFG();
42 }
43
44 StringRef getPassName() const override {
46 }
47};
48
49} // end anonymous namespace
50
51char RISCVInsertReadWriteCSRLegacy::ID = 0;
52
53INITIALIZE_PASS(RISCVInsertReadWriteCSRLegacy, DEBUG_TYPE,
55
56// TODO: Use more accurate rounding mode at the start of MBB.
57static bool emitWriteRoundingModeOpt(MachineBasicBlock &MBB,
59 bool Changed = false;
63
64 for (MachineInstr &MI : MBB) {
65 if (MI.getOpcode() == RISCV::SwapFRMImm ||
66 MI.getOpcode() == RISCV::WriteFRMImm) {
67 CurrentRM = MI.getOperand(0).getImm();
69 continue;
70 }
71
72 if (MI.getOpcode() == RISCV::WriteFRM) {
75 continue;
76 }
77
78 if (MI.isCall() || MI.isInlineAsm() ||
79 MI.readsRegister(RISCV::FRM, /*TRI=*/nullptr)) {
80 // Restore FRM before unknown operations.
81 if (SavedFRM.isValid())
82 BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(RISCV::WriteFRM))
86 continue;
87 }
88
89 assert(!MI.modifiesRegister(RISCV::FRM, /*TRI=*/nullptr) &&
90 "Expected that MI could not modify FRM.");
91
92 int FRMIdx = RISCVII::getFRMOpNum(MI.getDesc());
93 if (FRMIdx < 0)
94 continue;
95 unsigned InstrRM = MI.getOperand(FRMIdx).getImm();
96
98
99 // Make MI implicit use FRM.
100 MI.addOperand(MachineOperand::CreateReg(RISCV::FRM, /*IsDef*/ false,
101 /*IsImp*/ true));
102 Changed = true;
103
104 // Skip if MI uses same rounding mode as FRM.
105 if (InstrRM == CurrentRM)
106 continue;
107
108 if (!SavedFRM.isValid()) {
109 // Save current FRM value to SavedFRM.
110 MachineRegisterInfo *MRI = &MBB.getParent()->getRegInfo();
111 SavedFRM = MRI->createVirtualRegister(&RISCV::GPRRegClass);
112 BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(RISCV::SwapFRMImm), SavedFRM)
113 .addImm(InstrRM);
114 } else {
115 // Don't need to save current FRM when SavedFRM having value.
116 BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(RISCV::WriteFRMImm))
117 .addImm(InstrRM);
118 }
119 CurrentRM = InstrRM;
120 }
121
122 // Restore FRM if needed.
123 if (SavedFRM.isValid()) {
124 assert(LastFRMChanger && "Expected valid pointer.");
125 MachineInstrBuilder MIB =
126 BuildMI(*MBB.getParent(), {}, TII->get(RISCV::WriteFRM))
127 .addReg(SavedFRM);
128 MBB.insertAfter(LastFRMChanger, MIB);
129 }
130
131 return Changed;
132}
133
134// This function also swaps frm and restores it when encountering an RVV
135// floating point instruction with a static rounding mode.
137 const TargetInstrInfo *TII) {
138 bool Changed = false;
139 for (MachineInstr &MI : MBB) {
140 int FRMIdx = RISCVII::getFRMOpNum(MI.getDesc());
141 if (FRMIdx < 0)
142 continue;
143
144 unsigned FRMImm = MI.getOperand(FRMIdx).getImm();
145
146 // The value is a hint to this pass to not alter the frm value.
147 if (FRMImm == RISCVFPRndMode::DYN)
148 continue;
149
150 Changed = true;
151
152 // Save
153 MachineRegisterInfo *MRI = &MBB.getParent()->getRegInfo();
154 Register SavedFRM = MRI->createVirtualRegister(&RISCV::GPRRegClass);
155 BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(RISCV::SwapFRMImm),
156 SavedFRM)
157 .addImm(FRMImm);
158 MI.addOperand(MachineOperand::CreateReg(RISCV::FRM, /*IsDef*/ false,
159 /*IsImp*/ true));
160 // Restore
162 BuildMI(*MBB.getParent(), {}, TII->get(RISCV::WriteFRM))
164 MBB.insertAfter(MI, MIB);
165 }
166 return Changed;
167}
168
170 // Skip if the vector extension is not enabled.
172 if (!ST.hasVInstructions())
173 return false;
174
175 const TargetInstrInfo *TII = ST.getInstrInfo();
176
177 bool Changed = false;
178
179 for (MachineBasicBlock &MBB : MF) {
180 if (!ST.getCLOpts().frm_insert_opt)
182 else
183 Changed |= emitWriteRoundingModeOpt(MBB, TII);
184 }
185
186 return Changed;
187}
188
190 return new RISCVInsertReadWriteCSRLegacy();
191}
192
193bool RISCVInsertReadWriteCSRLegacy::runOnMachineFunction(MachineFunction &MF) {
195}
196
197PreservedAnalyses
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
MachineBasicBlock & MBB
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
#define RISCV_INSERT_READ_WRITE_CSR_NAME
unsigned CurrentRM
static bool emitWriteRoundingMode(MachineBasicBlock &MBB, const TargetInstrInfo *TII)
static bool insertReadWriteCSROnMachineFunction(MachineFunction &MF)
Register SavedFRM
MachineInstr * LastFRMChanger
MachineRegisterInfo & MRI
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:280
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
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.
Representation of each machine instruction.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
TargetInstrInfo - Interface to description of machine instruction set.
Pass manager infrastructure for declaring and invalidating analyses.
static int getFRMOpNum(const MCInstrDesc &Desc)
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
FunctionPass * createRISCVInsertReadWriteCSRLegacyPass()