LLVM 23.0.0git
ExpandPostRAPseudos.cpp
Go to the documentation of this file.
1//===-- ExpandPostRAPseudos.cpp - Pseudo instruction expansion pass -------===//
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 defines a pass that expands COPY and SUBREG_TO_REG pseudo
10// instructions after register allocation.
11//
12//===----------------------------------------------------------------------===//
13
19#include "llvm/CodeGen/Passes.h"
24#include "llvm/Support/Debug.h"
26
27using namespace llvm;
28
29#define DEBUG_TYPE "postrapseudos"
30
31namespace {
32struct ExpandPostRA {
33 bool run(MachineFunction &);
34
35private:
36 const TargetRegisterInfo *TRI = nullptr;
37 const TargetInstrInfo *TII = nullptr;
38
39 bool LowerSubregToReg(MachineInstr *MI);
40};
41
42struct ExpandPostRALegacy : public MachineFunctionPass {
43 static char ID;
44 ExpandPostRALegacy() : MachineFunctionPass(ID) {}
45
46 void getAnalysisUsage(AnalysisUsage &AU) const override {
47 AU.setPreservesCFG();
51 }
52
53 /// runOnMachineFunction - pass entry point
54 bool runOnMachineFunction(MachineFunction &) override;
55};
56} // end anonymous namespace
57
61 if (!ExpandPostRA().run(MF))
63
66 .preserve<MachineLoopAnalysis>()
68}
69
70char ExpandPostRALegacy::ID = 0;
71char &llvm::ExpandPostRAPseudosID = ExpandPostRALegacy::ID;
72
73INITIALIZE_PASS(ExpandPostRALegacy, DEBUG_TYPE,
74 "Post-RA pseudo instruction expansion pass", false, false)
75
76bool ExpandPostRA::LowerSubregToReg(MachineInstr *MI) {
77 MachineBasicBlock *MBB = MI->getParent();
78 assert(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
79 MI->getOperand(1).isReg() && MI->getOperand(1).isUse() &&
80 MI->getOperand(2).isImm() && "Invalid subreg_to_reg");
81
82 Register DstReg = MI->getOperand(0).getReg();
83 Register InsReg = MI->getOperand(1).getReg();
84 assert(!MI->getOperand(1).getSubReg() && "SubIdx on physreg?");
85 unsigned SubIdx = MI->getOperand(2).getImm();
86
87 assert(SubIdx != 0 && "Invalid index for insert_subreg");
88 Register DstSubReg = TRI->getSubReg(DstReg, SubIdx);
89
90 assert(DstReg.isPhysical() &&
91 "Insert destination must be in a physical register");
92 assert(InsReg.isPhysical() &&
93 "Inserted value must be in a physical register");
94
95 LLVM_DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
96
97 if (MI->allDefsAreDead() || DstSubReg == InsReg) {
98 // No need to insert an identity copy instruction.
99 // Watch out for case like this:
100 // %rax = SUBREG_TO_REG killed %eax, 3
101 // We must leave %rax live.
102 MI->setDesc(TII->get(TargetOpcode::KILL));
103 MI->removeOperand(2); // SubIdx
104 LLVM_DEBUG(dbgs() << "subreg: replaced by: " << *MI);
105 return true;
106 }
107
108 TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,
109 MI->getOperand(1).isKill());
110
111 // Implicitly define DstReg for subsequent uses.
113 --CopyMI;
114 CopyMI->addRegisterDefined(DstReg);
115 LLVM_DEBUG(dbgs() << "subreg: " << *CopyMI);
116
117 MBB->erase(MI);
118 return true;
119}
120
121bool ExpandPostRALegacy::runOnMachineFunction(MachineFunction &MF) {
122 return ExpandPostRA().run(MF);
123}
124
125/// runOnMachineFunction - Reduce subregister inserts and extracts to register
126/// copies.
127///
128bool ExpandPostRA::run(MachineFunction &MF) {
129 LLVM_DEBUG(dbgs() << "Machine Function\n"
130 << "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"
131 << "********** Function: " << MF.getName() << '\n');
134
135 bool MadeChange = false;
136
137 for (MachineBasicBlock &MBB : MF) {
139 // Only expand pseudos.
140 if (!MI.isPseudo())
141 continue;
142
143 // Give targets a chance to expand even standard pseudos.
144 if (TII->expandPostRAPseudo(MI)) {
145 MadeChange = true;
146 continue;
147 }
148
149 // Expand standard pseudos.
150 switch (MI.getOpcode()) {
151 case TargetOpcode::SUBREG_TO_REG:
152 MadeChange |= LowerSubregToReg(&MI);
153 break;
154 case TargetOpcode::COPY:
155 TII->lowerCopy(&MI, TRI);
156 MadeChange = true;
157 break;
158 case TargetOpcode::DBG_VALUE:
159 continue;
160 case TargetOpcode::INSERT_SUBREG:
161 case TargetOpcode::EXTRACT_SUBREG:
162 llvm_unreachable("Sub-register pseudos should have been eliminated.");
163 }
164 }
165 }
166
167 return MadeChange;
168}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
#define LLVM_DEBUG(...)
Definition Debug.h:114
Represent the analysis usage information of a pass.
AnalysisUsage & addPreservedID(const void *ID)
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:270
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
bool expandPostRAPseudo(MachineInstr &MI) const override
This function is called for all pseudo instructions that remain after register allocation.
MachineInstrBundleIterator< MachineInstr > iterator
Analysis pass which computes a MachineDominatorTree.
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.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Representation of each machine instruction.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
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
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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
LLVM_ABI char & MachineDominatorsID
MachineDominators - This pass is a machine dominators analysis pass.
LLVM_ABI char & ExpandPostRAPseudosID
ExpandPostRAPseudos - This pass expands pseudo instructions after register allocation.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:634
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI char & MachineLoopInfoID
MachineLoopInfo - This pass is a loop analysis pass.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207