26
26
27
27
using namespace llvm ;
28
28
29
+ static cl::opt<bool >
30
+ DisableLeafProc (" disable-sparc-leaf-proc" ,
31
+ cl::init (true ),
32
+ cl::desc(" Disable Sparc leaf procedure optimization." ),
33
+ cl::Hidden);
34
+
35
+
29
36
void SparcFrameLowering::emitPrologue (MachineFunction &MF) const {
37
+ SparcMachineFunctionInfo *FuncInfo = MF.getInfo <SparcMachineFunctionInfo>();
38
+ if (FuncInfo->isLeafProc ())
39
+ return ;
40
+
30
41
MachineBasicBlock &MBB = MF.front ();
31
42
MachineFrameInfo *MFI = MF.getFrameInfo ();
32
43
const SparcInstrInfo &TII =
@@ -97,6 +108,9 @@ eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
97
108
98
109
void SparcFrameLowering::emitEpilogue (MachineFunction &MF,
99
110
MachineBasicBlock &MBB) const {
111
+ SparcMachineFunctionInfo *FuncInfo = MF.getInfo <SparcMachineFunctionInfo>();
112
+ if (FuncInfo->isLeafProc ())
113
+ return ;
100
114
MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr ();
101
115
const SparcInstrInfo &TII =
102
116
*static_cast <const SparcInstrInfo*>(MF.getTarget ().getInstrInfo ());
@@ -121,3 +135,65 @@ bool SparcFrameLowering::hasFP(const MachineFunction &MF) const {
121
135
MFI->hasVarSizedObjects () || MFI->isFrameAddressTaken ();
122
136
}
123
137
138
+
139
+ static bool verifyLeafProcRegUse (MachineRegisterInfo *MRI)
140
+ {
141
+
142
+ for (unsigned reg = SP::I0; reg <= SP::I7; ++reg)
143
+ if (MRI->isPhysRegUsed (reg))
144
+ return false ;
145
+
146
+ for (unsigned reg = SP::L0; reg <= SP::L7; ++reg)
147
+ if (MRI->isPhysRegUsed (reg))
148
+ return false ;
149
+
150
+ return true ;
151
+ }
152
+
153
+ bool SparcFrameLowering::isLeafProc (MachineFunction &MF) const
154
+ {
155
+
156
+ MachineRegisterInfo &MRI = MF.getRegInfo ();
157
+ MachineFrameInfo *MFI = MF.getFrameInfo ();
158
+
159
+ return !(MFI->hasCalls () // has calls
160
+ || MRI.isPhysRegUsed (SP::L0) // Too many registers needed
161
+ || MRI.isPhysRegUsed (SP::O6) // %SP is used
162
+ || hasFP (MF)); // need %FP
163
+ }
164
+
165
+ void SparcFrameLowering::remapRegsForLeafProc (MachineFunction &MF) const {
166
+
167
+ MachineRegisterInfo &MRI = MF.getRegInfo ();
168
+
169
+ // remap %i[0-7] to %o[0-7]
170
+ for (unsigned reg = SP::I0; reg <= SP::I7; ++reg) {
171
+ if (!MRI.isPhysRegUsed (reg))
172
+ continue ;
173
+ unsigned mapped_reg = (reg - SP::I0 + SP::O0);
174
+ assert (!MRI.isPhysRegUsed (mapped_reg));
175
+
176
+ // Replace I register with O register
177
+ MRI.replaceRegWith (reg, mapped_reg);
178
+
179
+ // mark the reg unused.
180
+ MRI.setPhysRegUnused (reg);
181
+ }
182
+
183
+ assert (verifyLeafProcRegUse (&MRI));
184
+ #ifdef XDEBUG
185
+ MF.verify (0 , " After LeafProc Remapping" );
186
+ #endif
187
+ }
188
+
189
+ void SparcFrameLowering::processFunctionBeforeCalleeSavedScan
190
+ (MachineFunction &MF, RegScavenger *RS) const {
191
+
192
+ if (!DisableLeafProc && isLeafProc (MF)) {
193
+ SparcMachineFunctionInfo *MFI = MF.getInfo <SparcMachineFunctionInfo>();
194
+ MFI->setLeafProc (true );
195
+
196
+ remapRegsForLeafProc (MF);
197
+ }
198
+
199
+ }
0 commit comments