LLVM 22.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) {
46 }
47
48 void getAnalysisUsage(AnalysisUsage &AU) const override {
49 AU.setPreservesCFG();
53 }
54
55 /// runOnMachineFunction - pass entry point
57};
58} // end anonymous namespace
59
63 if (!ExpandPostRA().run(MF))
65
68 .preserve<MachineLoopAnalysis>()
70}
71
72char ExpandPostRALegacy::ID = 0;
73char &llvm::ExpandPostRAPseudosID = ExpandPostRALegacy::ID;
74
75INITIALIZE_PASS(ExpandPostRALegacy, DEBUG_TYPE,
76 "Post-RA pseudo instruction expansion pass", false, false)
77
78bool ExpandPostRA::LowerSubregToReg(MachineInstr *MI) {
79 MachineBasicBlock *MBB = MI->getParent();
80 assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
81 MI->getOperand(1).isImm() &&
82 (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
83 MI->getOperand(3).isImm() && "Invalid subreg_to_reg");
84
85 Register DstReg = MI->getOperand(0).getReg();
86 Register InsReg = MI->getOperand(2).getReg();
87 assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?");
88 unsigned SubIdx = MI->getOperand(3).getImm();
89
90 assert(SubIdx != 0 && "Invalid index for insert_subreg");
91 Register DstSubReg = TRI->getSubReg(DstReg, SubIdx);
92
93 assert(DstReg.isPhysical() &&
94 "Insert destination must be in a physical register");
95 assert(InsReg.isPhysical() &&
96 "Inserted value must be in a physical register");
97
98 LLVM_DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
99
100 if (MI->allDefsAreDead()) {
101 MI->setDesc(TII->get(TargetOpcode::KILL));
102 MI->removeOperand(3); // SubIdx
103 MI->removeOperand(1); // Imm
104 LLVM_DEBUG(dbgs() << "subreg: replaced by: " << *MI);
105 return true;
106 }
107
108 if (DstSubReg == InsReg) {
109 // No need to insert an identity copy instruction.
110 // Watch out for case like this:
111 // %rax = SUBREG_TO_REG 0, killed %eax, 3
112 // We must leave %rax live.
113 if (DstReg != InsReg) {
114 MI->setDesc(TII->get(TargetOpcode::KILL));
115 MI->removeOperand(3); // SubIdx
116 MI->removeOperand(1); // Imm
117 LLVM_DEBUG(dbgs() << "subreg: replace by: " << *MI);
118 return true;
119 }
120 LLVM_DEBUG(dbgs() << "subreg: eliminated!");
121 } else {
122 TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,
123 MI->getOperand(2).isKill());
124
125 // Implicitly define DstReg for subsequent uses.
127 --CopyMI;
128 CopyMI->addRegisterDefined(DstReg);
129 LLVM_DEBUG(dbgs() << "subreg: " << *CopyMI);
130 }
131
132 LLVM_DEBUG(dbgs() << '\n');
133 MBB->erase(MI);
134 return true;
135}
136
137bool ExpandPostRALegacy::runOnMachineFunction(MachineFunction &MF) {
138 return ExpandPostRA().run(MF);
139}
140
141/// runOnMachineFunction - Reduce subregister inserts and extracts to register
142/// copies.
143///
144bool ExpandPostRA::run(MachineFunction &MF) {
145 LLVM_DEBUG(dbgs() << "Machine Function\n"
146 << "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"
147 << "********** Function: " << MF.getName() << '\n');
150
151 bool MadeChange = false;
152
153 for (MachineBasicBlock &MBB : MF) {
155 // Only expand pseudos.
156 if (!MI.isPseudo())
157 continue;
158
159 // Give targets a chance to expand even standard pseudos.
160 if (TII->expandPostRAPseudo(MI)) {
161 MadeChange = true;
162 continue;
163 }
164
165 // Expand standard pseudos.
166 switch (MI.getOpcode()) {
167 case TargetOpcode::SUBREG_TO_REG:
168 MadeChange |= LowerSubregToReg(&MI);
169 break;
170 case TargetOpcode::COPY:
171 TII->lowerCopy(&MI, TRI);
172 MadeChange = true;
173 break;
174 case TargetOpcode::DBG_VALUE:
175 continue;
176 case TargetOpcode::INSERT_SUBREG:
177 case TargetOpcode::EXTRACT_SUBREG:
178 llvm_unreachable("Sub-register pseudos should have been eliminated.");
179 }
180 }
181 }
182
183 return MadeChange;
184}
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:119
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
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)
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, Register DestReg, Register SrcReg, bool KillSrc, bool RenamableDest=false, bool RenamableSrc=false) const override
Emit instructions to copy a pair of physical registers.
bool expandPostRAPseudo(MachineInstr &MI) const override
This function is called for all pseudo instructions that remain after register allocation.
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
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.
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.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Representation of each machine instruction.
Definition: MachineInstr.h:72
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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:19
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition: Register.h:78
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: AddressRanges.h:18
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:663
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
LLVM_ABI void initializeExpandPostRALegacyPass(PassRegistry &)