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).isImm() &&
80 (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
81 MI->getOperand(3).isImm() && "Invalid subreg_to_reg");
82
83 Register DstReg = MI->getOperand(0).getReg();
84 Register InsReg = MI->getOperand(2).getReg();
85 assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?");
86 unsigned SubIdx = MI->getOperand(3).getImm();
87
88 assert(SubIdx != 0 && "Invalid index for insert_subreg");
89 Register DstSubReg = TRI->getSubReg(DstReg, SubIdx);
90
91 assert(DstReg.isPhysical() &&
92 "Insert destination must be in a physical register");
93 assert(InsReg.isPhysical() &&
94 "Inserted value must be in a physical register");
95
96 LLVM_DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
97
98 if (MI->allDefsAreDead()) {
99 MI->setDesc(TII->get(TargetOpcode::KILL));
100 MI->removeOperand(3); // SubIdx
101 MI->removeOperand(1); // Imm
102 LLVM_DEBUG(dbgs() << "subreg: replaced by: " << *MI);
103 return true;
104 }
105
106 if (DstSubReg == InsReg) {
107 // No need to insert an identity copy instruction.
108 // Watch out for case like this:
109 // %rax = SUBREG_TO_REG 0, killed %eax, 3
110 // We must leave %rax live.
111 if (DstReg != InsReg) {
112 MI->setDesc(TII->get(TargetOpcode::KILL));
113 MI->removeOperand(3); // SubIdx
114 MI->removeOperand(1); // Imm
115 LLVM_DEBUG(dbgs() << "subreg: replace by: " << *MI);
116 return true;
117 }
118 LLVM_DEBUG(dbgs() << "subreg: eliminated!");
119 } else {
120 TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,
121 MI->getOperand(2).isKill());
122
123 // Implicitly define DstReg for subsequent uses.
125 --CopyMI;
126 CopyMI->addRegisterDefined(DstReg);
127 LLVM_DEBUG(dbgs() << "subreg: " << *CopyMI);
128 }
129
130 LLVM_DEBUG(dbgs() << '\n');
131 MBB->erase(MI);
132 return true;
133}
134
135bool ExpandPostRALegacy::runOnMachineFunction(MachineFunction &MF) {
136 return ExpandPostRA().run(MF);
137}
138
139/// runOnMachineFunction - Reduce subregister inserts and extracts to register
140/// copies.
141///
142bool ExpandPostRA::run(MachineFunction &MF) {
143 LLVM_DEBUG(dbgs() << "Machine Function\n"
144 << "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"
145 << "********** Function: " << MF.getName() << '\n');
148
149 bool MadeChange = false;
150
151 for (MachineBasicBlock &MBB : MF) {
153 // Only expand pseudos.
154 if (!MI.isPseudo())
155 continue;
156
157 // Give targets a chance to expand even standard pseudos.
158 if (TII->expandPostRAPseudo(MI)) {
159 MadeChange = true;
160 continue;
161 }
162
163 // Expand standard pseudos.
164 switch (MI.getOpcode()) {
165 case TargetOpcode::SUBREG_TO_REG:
166 MadeChange |= LowerSubregToReg(&MI);
167 break;
168 case TargetOpcode::COPY:
169 TII->lowerCopy(&MI, TRI);
170 MadeChange = true;
171 break;
172 case TargetOpcode::DBG_VALUE:
173 continue;
174 case TargetOpcode::INSERT_SUBREG:
175 case TargetOpcode::EXTRACT_SUBREG:
176 llvm_unreachable("Sub-register pseudos should have been eliminated.");
177 }
178 }
179 }
180
181 return MadeChange;
182}
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:632
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