LLVM 17.0.0git
RISCVExpandPseudoInsts.cpp
Go to the documentation of this file.
1//===-- RISCVExpandPseudoInsts.cpp - Expand pseudo instructions -----------===//
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 pseudo instructions into target
10// instructions. This pass should be run after register allocation but before
11// the post-regalloc scheduling pass.
12//
13//===----------------------------------------------------------------------===//
14
15#include "RISCV.h"
16#include "RISCVInstrInfo.h"
17#include "RISCVTargetMachine.h"
18
22#include "llvm/MC/MCContext.h"
23
24using namespace llvm;
25
26#define RISCV_EXPAND_PSEUDO_NAME "RISCV pseudo instruction expansion pass"
27#define RISCV_PRERA_EXPAND_PSEUDO_NAME "RISCV Pre-RA pseudo instruction expansion pass"
28
29namespace {
30
31class RISCVExpandPseudo : public MachineFunctionPass {
32public:
33 const RISCVInstrInfo *TII;
34 static char ID;
35
36 RISCVExpandPseudo() : MachineFunctionPass(ID) {
38 }
39
40 bool runOnMachineFunction(MachineFunction &MF) override;
41
42 StringRef getPassName() const override { return RISCV_EXPAND_PSEUDO_NAME; }
43
44private:
45 bool expandMBB(MachineBasicBlock &MBB);
51 bool expandVMSET_VMCLR(MachineBasicBlock &MBB,
52 MachineBasicBlock::iterator MBBI, unsigned Opcode);
53#ifndef NDEBUG
54 unsigned getInstSizeInBytes(const MachineFunction &MF) const {
55 unsigned Size = 0;
56 for (auto &MBB : MF)
57 for (auto &MI : MBB)
58 Size += TII->getInstSizeInBytes(MI);
59 return Size;
60 }
61#endif
62};
63
64char RISCVExpandPseudo::ID = 0;
65
66bool RISCVExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
67 TII = static_cast<const RISCVInstrInfo *>(MF.getSubtarget().getInstrInfo());
68
69#ifndef NDEBUG
70 const unsigned OldSize = getInstSizeInBytes(MF);
71#endif
72
73 bool Modified = false;
74 for (auto &MBB : MF)
75 Modified |= expandMBB(MBB);
76
77#ifndef NDEBUG
78 const unsigned NewSize = getInstSizeInBytes(MF);
79 assert(OldSize >= NewSize);
80#endif
81 return Modified;
82}
83
84bool RISCVExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
85 bool Modified = false;
86
88 while (MBBI != E) {
89 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
90 Modified |= expandMI(MBB, MBBI, NMBBI);
91 MBBI = NMBBI;
92 }
93
94 return Modified;
95}
96
97bool RISCVExpandPseudo::expandMI(MachineBasicBlock &MBB,
100 // RISCVInstrInfo::getInstSizeInBytes expects that the total size of the
101 // expanded instructions for each pseudo is correct in the Size field of the
102 // tablegen definition for the pseudo.
103 switch (MBBI->getOpcode()) {
104 case RISCV::PseudoCCMOVGPR:
105 case RISCV::PseudoCCADD:
106 case RISCV::PseudoCCSUB:
107 case RISCV::PseudoCCAND:
108 case RISCV::PseudoCCOR:
109 case RISCV::PseudoCCXOR:
110 case RISCV::PseudoCCADDW:
111 case RISCV::PseudoCCSUBW:
112 return expandCCOp(MBB, MBBI, NextMBBI);
113 case RISCV::PseudoVSETVLI:
114 case RISCV::PseudoVSETVLIX0:
115 case RISCV::PseudoVSETIVLI:
116 return expandVSetVL(MBB, MBBI);
117 case RISCV::PseudoVMCLR_M_B1:
118 case RISCV::PseudoVMCLR_M_B2:
119 case RISCV::PseudoVMCLR_M_B4:
120 case RISCV::PseudoVMCLR_M_B8:
121 case RISCV::PseudoVMCLR_M_B16:
122 case RISCV::PseudoVMCLR_M_B32:
123 case RISCV::PseudoVMCLR_M_B64:
124 // vmclr.m vd => vmxor.mm vd, vd, vd
125 return expandVMSET_VMCLR(MBB, MBBI, RISCV::VMXOR_MM);
126 case RISCV::PseudoVMSET_M_B1:
127 case RISCV::PseudoVMSET_M_B2:
128 case RISCV::PseudoVMSET_M_B4:
129 case RISCV::PseudoVMSET_M_B8:
130 case RISCV::PseudoVMSET_M_B16:
131 case RISCV::PseudoVMSET_M_B32:
132 case RISCV::PseudoVMSET_M_B64:
133 // vmset.m vd => vmxnor.mm vd, vd, vd
134 return expandVMSET_VMCLR(MBB, MBBI, RISCV::VMXNOR_MM);
135 }
136
137 return false;
138}
139
140bool RISCVExpandPseudo::expandCCOp(MachineBasicBlock &MBB,
142 MachineBasicBlock::iterator &NextMBBI) {
143
145 MachineInstr &MI = *MBBI;
146 DebugLoc DL = MI.getDebugLoc();
147
150
151 MF->insert(++MBB.getIterator(), TrueBB);
152 MF->insert(++TrueBB->getIterator(), MergeBB);
153
154 // We want to copy the "true" value when the condition is true which means
155 // we need to invert the branch condition to jump over TrueBB when the
156 // condition is false.
157 auto CC = static_cast<RISCVCC::CondCode>(MI.getOperand(3).getImm());
159
160 // Insert branch instruction.
161 BuildMI(MBB, MBBI, DL, TII->getBrCond(CC))
162 .addReg(MI.getOperand(1).getReg())
163 .addReg(MI.getOperand(2).getReg())
164 .addMBB(MergeBB);
165
166 Register DestReg = MI.getOperand(0).getReg();
167 assert(MI.getOperand(4).getReg() == DestReg);
168
169 if (MI.getOpcode() == RISCV::PseudoCCMOVGPR) {
170 // Add MV.
171 BuildMI(TrueBB, DL, TII->get(RISCV::ADDI), DestReg)
172 .add(MI.getOperand(5))
173 .addImm(0);
174 } else {
175 unsigned NewOpc;
176 switch (MI.getOpcode()) {
177 default:
178 llvm_unreachable("Unexpected opcode!");
179 case RISCV::PseudoCCADD: NewOpc = RISCV::ADD; break;
180 case RISCV::PseudoCCSUB: NewOpc = RISCV::SUB; break;
181 case RISCV::PseudoCCAND: NewOpc = RISCV::AND; break;
182 case RISCV::PseudoCCOR: NewOpc = RISCV::OR; break;
183 case RISCV::PseudoCCXOR: NewOpc = RISCV::XOR; break;
184 case RISCV::PseudoCCADDW: NewOpc = RISCV::ADDW; break;
185 case RISCV::PseudoCCSUBW: NewOpc = RISCV::SUBW; break;
186 }
187 BuildMI(TrueBB, DL, TII->get(NewOpc), DestReg)
188 .add(MI.getOperand(5))
189 .add(MI.getOperand(6));
190 }
191
192 TrueBB->addSuccessor(MergeBB);
193
194 MergeBB->splice(MergeBB->end(), &MBB, MI, MBB.end());
195 MergeBB->transferSuccessors(&MBB);
196
197 MBB.addSuccessor(TrueBB);
198 MBB.addSuccessor(MergeBB);
199
200 NextMBBI = MBB.end();
201 MI.eraseFromParent();
202
203 // Make sure live-ins are correctly attached to this new basic block.
204 LivePhysRegs LiveRegs;
205 computeAndAddLiveIns(LiveRegs, *TrueBB);
206 computeAndAddLiveIns(LiveRegs, *MergeBB);
207
208 return true;
209}
210
211bool RISCVExpandPseudo::expandVSetVL(MachineBasicBlock &MBB,
213 assert(MBBI->getNumExplicitOperands() == 3 && MBBI->getNumOperands() >= 5 &&
214 "Unexpected instruction format");
215
216 DebugLoc DL = MBBI->getDebugLoc();
217
218 assert((MBBI->getOpcode() == RISCV::PseudoVSETVLI ||
219 MBBI->getOpcode() == RISCV::PseudoVSETVLIX0 ||
220 MBBI->getOpcode() == RISCV::PseudoVSETIVLI) &&
221 "Unexpected pseudo instruction");
222 unsigned Opcode;
223 if (MBBI->getOpcode() == RISCV::PseudoVSETIVLI)
224 Opcode = RISCV::VSETIVLI;
225 else
226 Opcode = RISCV::VSETVLI;
227 const MCInstrDesc &Desc = TII->get(Opcode);
228 assert(Desc.getNumOperands() == 3 && "Unexpected instruction format");
229
230 Register DstReg = MBBI->getOperand(0).getReg();
231 bool DstIsDead = MBBI->getOperand(0).isDead();
232 BuildMI(MBB, MBBI, DL, Desc)
233 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
234 .add(MBBI->getOperand(1)) // VL
235 .add(MBBI->getOperand(2)); // VType
236
237 MBBI->eraseFromParent(); // The pseudo instruction is gone now.
238 return true;
239}
240
241bool RISCVExpandPseudo::expandVMSET_VMCLR(MachineBasicBlock &MBB,
243 unsigned Opcode) {
244 DebugLoc DL = MBBI->getDebugLoc();
245 Register DstReg = MBBI->getOperand(0).getReg();
246 const MCInstrDesc &Desc = TII->get(Opcode);
247 BuildMI(MBB, MBBI, DL, Desc, DstReg)
248 .addReg(DstReg, RegState::Undef)
249 .addReg(DstReg, RegState::Undef);
250 MBBI->eraseFromParent(); // The pseudo instruction is gone now.
251 return true;
252}
253
254class RISCVPreRAExpandPseudo : public MachineFunctionPass {
255public:
256 const RISCVInstrInfo *TII;
257 static char ID;
258
259 RISCVPreRAExpandPseudo() : MachineFunctionPass(ID) {
261 }
262
263 bool runOnMachineFunction(MachineFunction &MF) override;
264
265 void getAnalysisUsage(AnalysisUsage &AU) const override {
266 AU.setPreservesCFG();
268 }
269 StringRef getPassName() const override {
271 }
272
273private:
274 bool expandMBB(MachineBasicBlock &MBB);
277 bool expandAuipcInstPair(MachineBasicBlock &MBB,
280 unsigned FlagsHi, unsigned SecondOpcode);
281 bool expandLoadLocalAddress(MachineBasicBlock &MBB,
284 bool expandLoadAddress(MachineBasicBlock &MBB,
287 bool expandLoadTLSIEAddress(MachineBasicBlock &MBB,
290 bool expandLoadTLSGDAddress(MachineBasicBlock &MBB,
293#ifndef NDEBUG
294 unsigned getInstSizeInBytes(const MachineFunction &MF) const {
295 unsigned Size = 0;
296 for (auto &MBB : MF)
297 for (auto &MI : MBB)
298 Size += TII->getInstSizeInBytes(MI);
299 return Size;
300 }
301#endif
302};
303
304char RISCVPreRAExpandPseudo::ID = 0;
305
306bool RISCVPreRAExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
307 TII = static_cast<const RISCVInstrInfo *>(MF.getSubtarget().getInstrInfo());
308
309#ifndef NDEBUG
310 const unsigned OldSize = getInstSizeInBytes(MF);
311#endif
312
313 bool Modified = false;
314 for (auto &MBB : MF)
315 Modified |= expandMBB(MBB);
316
317#ifndef NDEBUG
318 const unsigned NewSize = getInstSizeInBytes(MF);
319 assert(OldSize >= NewSize);
320#endif
321 return Modified;
322}
323
324bool RISCVPreRAExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
325 bool Modified = false;
326
328 while (MBBI != E) {
329 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
330 Modified |= expandMI(MBB, MBBI, NMBBI);
331 MBBI = NMBBI;
332 }
333
334 return Modified;
335}
336
337bool RISCVPreRAExpandPseudo::expandMI(MachineBasicBlock &MBB,
339 MachineBasicBlock::iterator &NextMBBI) {
340
341 switch (MBBI->getOpcode()) {
342 case RISCV::PseudoLLA:
343 return expandLoadLocalAddress(MBB, MBBI, NextMBBI);
344 case RISCV::PseudoLA:
345 return expandLoadAddress(MBB, MBBI, NextMBBI);
346 case RISCV::PseudoLA_TLS_IE:
347 return expandLoadTLSIEAddress(MBB, MBBI, NextMBBI);
348 case RISCV::PseudoLA_TLS_GD:
349 return expandLoadTLSGDAddress(MBB, MBBI, NextMBBI);
350 }
351 return false;
352}
353
354bool RISCVPreRAExpandPseudo::expandAuipcInstPair(
356 MachineBasicBlock::iterator &NextMBBI, unsigned FlagsHi,
357 unsigned SecondOpcode) {
359 MachineInstr &MI = *MBBI;
360 DebugLoc DL = MI.getDebugLoc();
361
362 Register DestReg = MI.getOperand(0).getReg();
363 Register ScratchReg =
364 MF->getRegInfo().createVirtualRegister(&RISCV::GPRRegClass);
365
366 MachineOperand &Symbol = MI.getOperand(1);
367 Symbol.setTargetFlags(FlagsHi);
368 MCSymbol *AUIPCSymbol = MF->getContext().createNamedTempSymbol("pcrel_hi");
369
370 MachineInstr *MIAUIPC =
371 BuildMI(MBB, MBBI, DL, TII->get(RISCV::AUIPC), ScratchReg).add(Symbol);
372 MIAUIPC->setPreInstrSymbol(*MF, AUIPCSymbol);
373
374 MachineInstr *SecondMI =
375 BuildMI(MBB, MBBI, DL, TII->get(SecondOpcode), DestReg)
376 .addReg(ScratchReg)
377 .addSym(AUIPCSymbol, RISCVII::MO_PCREL_LO);
378
379 if (MI.hasOneMemOperand())
380 SecondMI->addMemOperand(*MF, *MI.memoperands_begin());
381
382 MI.eraseFromParent();
383 return true;
384}
385
386bool RISCVPreRAExpandPseudo::expandLoadLocalAddress(
388 MachineBasicBlock::iterator &NextMBBI) {
389 return expandAuipcInstPair(MBB, MBBI, NextMBBI, RISCVII::MO_PCREL_HI,
390 RISCV::ADDI);
391}
392
393bool RISCVPreRAExpandPseudo::expandLoadAddress(
395 MachineBasicBlock::iterator &NextMBBI) {
397
398 const auto &STI = MF->getSubtarget<RISCVSubtarget>();
399 // When HWASAN is used and tagging of global variables is enabled
400 // they should be accessed via the GOT, since the tagged address of a global
401 // is incompatible with existing code models. This also applies to non-pic
402 // mode.
403 assert(MF->getTarget().isPositionIndependent() || STI.allowTaggedGlobals());
404 unsigned SecondOpcode = STI.is64Bit() ? RISCV::LD : RISCV::LW;
405 return expandAuipcInstPair(MBB, MBBI, NextMBBI, RISCVII::MO_GOT_HI,
406 SecondOpcode);
407}
408
409bool RISCVPreRAExpandPseudo::expandLoadTLSIEAddress(
411 MachineBasicBlock::iterator &NextMBBI) {
413
414 const auto &STI = MF->getSubtarget<RISCVSubtarget>();
415 unsigned SecondOpcode = STI.is64Bit() ? RISCV::LD : RISCV::LW;
416 return expandAuipcInstPair(MBB, MBBI, NextMBBI, RISCVII::MO_TLS_GOT_HI,
417 SecondOpcode);
418}
419
420bool RISCVPreRAExpandPseudo::expandLoadTLSGDAddress(
422 MachineBasicBlock::iterator &NextMBBI) {
423 return expandAuipcInstPair(MBB, MBBI, NextMBBI, RISCVII::MO_TLS_GD_HI,
424 RISCV::ADDI);
425}
426
427} // end of anonymous namespace
428
429INITIALIZE_PASS(RISCVExpandPseudo, "riscv-expand-pseudo",
430 RISCV_EXPAND_PSEUDO_NAME, false, false)
431
432INITIALIZE_PASS(RISCVPreRAExpandPseudo, "riscv-prera-expand-pseudo",
434
435namespace llvm {
436
437FunctionPass *createRISCVExpandPseudoPass() { return new RISCVExpandPseudo(); }
438FunctionPass *createRISCVPreRAExpandPseudoPass() { return new RISCVPreRAExpandPseudo(); }
439
440} // end of namespace llvm
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
uint64_t Size
static Expected< BitVector > expand(StringRef S, StringRef Original)
Definition: GlobPattern.cpp:26
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
#define RISCV_PRERA_EXPAND_PSEUDO_NAME
#define RISCV_EXPAND_PSEUDO_NAME
riscv prera expand pseudo
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static unsigned getInstSizeInBytes(const MachineInstr &MI, const SystemZInstrInfo *TII)
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:265
A debug info location.
Definition: DebugLoc.h:33
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
A set of physical registers with utility functions to track liveness when walking backward/forward th...
Definition: LivePhysRegs.h:50
MCSymbol * createNamedTempSymbol()
Create a temporary symbol with a unique name whose name cannot be omitted in the symbol table.
Definition: MCContext.cpp:320
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:198
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
Definition: MCInstrDesc.h:237
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
void transferSuccessors(MachineBasicBlock *FromMBB)
Transfers all the successors from MBB to this machine basic block (i.e., copies all the successors Fr...
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MCContext & getContext() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
Definition: MachineInstr.h:68
void setPreInstrSymbol(MachineFunction &MF, MCSymbol *Symbol)
Set a symbol that will be emitted just prior to the instruction itself.
void addMemOperand(MachineFunction &MF, MachineMemOperand *MO)
Add a MachineMemOperand to the machine instruction.
MachineOperand class - Representation of each machine instruction operand.
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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:50
bool isPositionIndependent() const
virtual const TargetInstrInfo * getInstrInfo() const
self_iterator getIterator()
Definition: ilist_node.h:82
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
TargetPassConfig.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
CondCode getOppositeBranchCondition(CondCode)
@ Define
Register definition.
@ Undef
Value of the register doesn't matter.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeRISCVExpandPseudoPass(PassRegistry &)
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
unsigned getDeadRegState(bool B)
void computeAndAddLiveIns(LivePhysRegs &LiveRegs, MachineBasicBlock &MBB)
Convenience function combining computeLiveIns() and addLiveIns().
FunctionPass * createRISCVExpandPseudoPass()
FunctionPass * createRISCVPreRAExpandPseudoPass()
void initializeRISCVPreRAExpandPseudoPass(PassRegistry &)