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
18#include "llvm/CodeGen/Passes.h"
23#include "llvm/Support/Debug.h"
25
26using namespace llvm;
27
28#define DEBUG_TYPE "postrapseudos"
29
30namespace {
31struct ExpandPostRA {
32 bool run(MachineFunction &);
33
34private:
35 const TargetRegisterInfo *TRI = nullptr;
36 const TargetInstrInfo *TII = nullptr;
37
38 bool LowerSubregToReg(MachineInstr *MI);
39};
40
41struct ExpandPostRALegacy : public MachineFunctionPass {
42 static char ID;
43 ExpandPostRALegacy() : MachineFunctionPass(ID) {}
44
45 void getAnalysisUsage(AnalysisUsage &AU) const override {
46 AU.setPreservesCFG();
48 }
49
50 /// runOnMachineFunction - pass entry point
51 bool runOnMachineFunction(MachineFunction &) override;
52};
53} // end anonymous namespace
54
63
64char ExpandPostRALegacy::ID = 0;
65char &llvm::ExpandPostRAPseudosID = ExpandPostRALegacy::ID;
66
67INITIALIZE_PASS(ExpandPostRALegacy, DEBUG_TYPE,
68 "Post-RA pseudo instruction expansion pass", false, false)
69
70bool ExpandPostRA::LowerSubregToReg(MachineInstr *MI) {
71 MachineBasicBlock *MBB = MI->getParent();
72 assert(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
73 MI->getOperand(1).isReg() && MI->getOperand(1).isUse() &&
74 MI->getOperand(2).isImm() && "Invalid subreg_to_reg");
75
76 Register DstReg = MI->getOperand(0).getReg();
77 Register InsReg = MI->getOperand(1).getReg();
78 assert(!MI->getOperand(1).getSubReg() && "SubIdx on physreg?");
79 unsigned SubIdx = MI->getOperand(2).getImm();
80
81 assert(SubIdx != 0 && "Invalid index for insert_subreg");
82 Register DstSubReg = TRI->getSubReg(DstReg, SubIdx);
83
84 assert(DstReg.isPhysical() &&
85 "Insert destination must be in a physical register");
86 assert(InsReg.isPhysical() &&
87 "Inserted value must be in a physical register");
88
89 LLVM_DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
90
91 if (MI->allDefsAreDead() || DstSubReg == InsReg) {
92 // No need to insert an identity copy instruction.
93 // Watch out for case like this:
94 // %rax = SUBREG_TO_REG killed %eax, 3
95 // We must leave %rax live.
96 MI->setDesc(TII->get(TargetOpcode::KILL));
97 MI->removeOperand(2); // SubIdx
98 LLVM_DEBUG(dbgs() << "subreg: replaced by: " << *MI);
99 return true;
100 }
101
102 TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,
103 MI->getOperand(1).isKill());
104
105 // Implicitly define DstReg for subsequent uses.
107 --CopyMI;
108 CopyMI->addRegisterDefined(DstReg);
109 LLVM_DEBUG(dbgs() << "subreg: " << *CopyMI);
110
111 MBB->erase(MI);
112 return true;
113}
114
115bool ExpandPostRALegacy::runOnMachineFunction(MachineFunction &MF) {
116 return ExpandPostRA().run(MF);
117}
118
119/// runOnMachineFunction - Reduce subregister inserts and extracts to register
120/// copies.
121///
122bool ExpandPostRA::run(MachineFunction &MF) {
123 LLVM_DEBUG(dbgs() << "Machine Function\n"
124 << "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"
125 << "********** Function: " << MF.getName() << '\n');
128
129 bool MadeChange = false;
130
131 for (MachineBasicBlock &MBB : MF) {
133 // Only expand pseudos.
134 if (!MI.isPseudo())
135 continue;
136
137 // Give targets a chance to expand even standard pseudos.
138 if (TII->expandPostRAPseudo(MI)) {
139 MadeChange = true;
140 continue;
141 }
142
143 // Expand standard pseudos.
144 switch (MI.getOpcode()) {
145 case TargetOpcode::SUBREG_TO_REG:
146 MadeChange |= LowerSubregToReg(&MI);
147 break;
148 case TargetOpcode::COPY:
149 TII->lowerCopy(&MI, TRI);
150 MadeChange = true;
151 break;
152 case TargetOpcode::DBG_VALUE:
153 continue;
154 case TargetOpcode::INSERT_SUBREG:
155 case TargetOpcode::EXTRACT_SUBREG:
156 llvm_unreachable("Sub-register pseudos should have been eliminated.");
157 }
158 }
159 }
160
161 return MadeChange;
162}
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