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();
50 }
51
52 /// runOnMachineFunction - pass entry point
53 bool runOnMachineFunction(MachineFunction &) override;
54};
55} // end anonymous namespace
56
65
66char ExpandPostRALegacy::ID = 0;
67char &llvm::ExpandPostRAPseudosID = ExpandPostRALegacy::ID;
68
69INITIALIZE_PASS(ExpandPostRALegacy, DEBUG_TYPE,
70 "Post-RA pseudo instruction expansion pass", false, false)
71
72bool ExpandPostRA::LowerSubregToReg(MachineInstr *MI) {
73 MachineBasicBlock *MBB = MI->getParent();
74 assert(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
75 MI->getOperand(1).isReg() && MI->getOperand(1).isUse() &&
76 MI->getOperand(2).isImm() && "Invalid subreg_to_reg");
77
78 Register DstReg = MI->getOperand(0).getReg();
79 Register InsReg = MI->getOperand(1).getReg();
80 assert(!MI->getOperand(1).getSubReg() && "SubIdx on physreg?");
81 unsigned SubIdx = MI->getOperand(2).getImm();
82
83 assert(SubIdx != 0 && "Invalid index for insert_subreg");
84 Register DstSubReg = TRI->getSubReg(DstReg, SubIdx);
85
86 assert(DstReg.isPhysical() &&
87 "Insert destination must be in a physical register");
88 assert(InsReg.isPhysical() &&
89 "Inserted value must be in a physical register");
90
91 LLVM_DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
92
93 if (MI->allDefsAreDead() || DstSubReg == InsReg) {
94 // No need to insert an identity copy instruction.
95 // Watch out for case like this:
96 // %rax = SUBREG_TO_REG killed %eax, 3
97 // We must leave %rax live.
98 MI->setDesc(TII->get(TargetOpcode::KILL));
99 MI->removeOperand(2); // SubIdx
100 LLVM_DEBUG(dbgs() << "subreg: replaced by: " << *MI);
101 return true;
102 }
103
104 TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,
105 MI->getOperand(1).isKill());
106
107 // Implicitly define DstReg for subsequent uses.
109 --CopyMI;
110 CopyMI->addRegisterDefined(DstReg);
111 LLVM_DEBUG(dbgs() << "subreg: " << *CopyMI);
112
113 MBB->erase(MI);
114 return true;
115}
116
117bool ExpandPostRALegacy::runOnMachineFunction(MachineFunction &MF) {
118 return ExpandPostRA().run(MF);
119}
120
121/// runOnMachineFunction - Reduce subregister inserts and extracts to register
122/// copies.
123///
124bool ExpandPostRA::run(MachineFunction &MF) {
125 LLVM_DEBUG(dbgs() << "Machine Function\n"
126 << "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"
127 << "********** Function: " << MF.getName() << '\n');
130
131 bool MadeChange = false;
132
133 for (MachineBasicBlock &MBB : MF) {
135 // Only expand pseudos.
136 if (!MI.isPseudo())
137 continue;
138
139 // Give targets a chance to expand even standard pseudos.
140 if (TII->expandPostRAPseudo(MI)) {
141 MadeChange = true;
142 continue;
143 }
144
145 // Expand standard pseudos.
146 switch (MI.getOpcode()) {
147 case TargetOpcode::SUBREG_TO_REG:
148 MadeChange |= LowerSubregToReg(&MI);
149 break;
150 case TargetOpcode::COPY:
151 TII->lowerCopy(&MI, TRI);
152 MadeChange = true;
153 break;
154 case TargetOpcode::DBG_VALUE:
155 continue;
156 case TargetOpcode::INSERT_SUBREG:
157 case TargetOpcode::EXTRACT_SUBREG:
158 llvm_unreachable("Sub-register pseudos should have been eliminated.");
159 }
160 }
161 }
162
163 return MadeChange;
164}
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.
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
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.
This is an optimization pass for GlobalISel generic memory operations.
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 raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209