LLVM 24.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"
25#include "llvm/Support/Debug.h"
27
28using namespace llvm;
29
30#define DEBUG_TYPE "postrapseudos"
31
32namespace {
33struct ExpandPostRA {
34 bool run(MachineFunction &);
35
36private:
37 const TargetRegisterInfo *TRI = nullptr;
38 const TargetInstrInfo *TII = nullptr;
39
40 bool LowerSubregToReg(MachineInstr *MI);
41};
42
43struct ExpandPostRALegacy : public MachineFunctionPass {
44 static char ID;
45 ExpandPostRALegacy() : MachineFunctionPass(ID) {}
46
47 void getAnalysisUsage(AnalysisUsage &AU) const override {
48 AU.setPreservesCFG();
53 }
54
55 /// runOnMachineFunction - pass entry point
56 bool runOnMachineFunction(MachineFunction &) override;
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).isReg() && MI->getOperand(1).isUse() &&
82 MI->getOperand(2).isImm() && "Invalid subreg_to_reg");
83
84 Register DstReg = MI->getOperand(0).getReg();
85 Register InsReg = MI->getOperand(1).getReg();
86 assert(!MI->getOperand(1).getSubReg() && "SubIdx on physreg?");
87 unsigned SubIdx = MI->getOperand(2).getImm();
88
89 assert(SubIdx != 0 && "Invalid index for insert_subreg");
90 Register DstSubReg = TRI->getSubReg(DstReg, SubIdx);
91
92 assert(DstReg.isPhysical() &&
93 "Insert destination must be in a physical register");
94 assert(InsReg.isPhysical() &&
95 "Inserted value must be in a physical register");
96
97 LLVM_DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
98
99 if (MI->allDefsAreDead() || DstSubReg == InsReg) {
100 // No need to insert an identity copy instruction.
101 // Watch out for case like this:
102 // %rax = SUBREG_TO_REG killed %eax, 3
103 // We must leave %rax live.
104 MI->setDesc(TII->get(TargetOpcode::KILL));
105 MI->removeOperand(2); // SubIdx
106 LLVM_DEBUG(dbgs() << "subreg: replaced by: " << *MI);
107 return true;
108 }
109
110 TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,
111 MI->getOperand(1).isKill());
112
113 // Implicitly define DstReg for subsequent uses.
115 --CopyMI;
116 CopyMI->addRegisterDefined(DstReg);
117 LLVM_DEBUG(dbgs() << "subreg: " << *CopyMI);
118
119 MBB->erase(MI);
120 return true;
121}
122
123bool ExpandPostRALegacy::runOnMachineFunction(MachineFunction &MF) {
124 return ExpandPostRA().run(MF);
125}
126
127/// runOnMachineFunction - Reduce subregister inserts and extracts to register
128/// copies.
129///
130bool ExpandPostRA::run(MachineFunction &MF) {
131 LLVM_DEBUG(dbgs() << "Machine Function\n"
132 << "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"
133 << "********** Function: " << MF.getName() << '\n');
136
137 bool MadeChange = false;
138
139 for (MachineBasicBlock &MBB : MF) {
141 // Only expand pseudos.
142 if (!MI.isPseudo())
143 continue;
144
145 // Give targets a chance to expand even standard pseudos.
146 if (TII->expandPostRAPseudo(MI)) {
147 MadeChange = true;
148 continue;
149 }
150
151 // Expand standard pseudos.
152 switch (MI.getOpcode()) {
153 case TargetOpcode::SUBREG_TO_REG:
154 MadeChange |= LowerSubregToReg(&MI);
155 break;
156 case TargetOpcode::COPY:
157 TII->lowerCopy(&MI, TRI);
158 MadeChange = true;
159 break;
160 case TargetOpcode::DBG_VALUE:
161 continue;
162 case TargetOpcode::INSERT_SUBREG:
163 case TargetOpcode::EXTRACT_SUBREG:
164 llvm_unreachable("Sub-register pseudos should have been eliminated.");
165 }
166 }
167 }
168
169 return MadeChange;
170}
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
Represent the analysis usage information of a pass.
AnalysisUsage & addPreservedID(const void *ID)
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
LLVM_ABI 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.
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:633
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:209