19
19
#include " llvm/CodeGen/LivePhysRegs.h"
20
20
#include " llvm/CodeGen/MachineFunctionPass.h"
21
21
#include " llvm/CodeGen/MachineInstrBuilder.h"
22
+ #include " llvm/MC/MCContext.h"
22
23
23
24
using namespace llvm ;
24
25
25
26
#define RISCV_EXPAND_PSEUDO_NAME " RISCV pseudo instruction expansion pass"
27
+ #define RISCV_PRERA_EXPAND_PSEUDO_NAME " RISCV Pre-RA pseudo instruction expansion pass"
26
28
27
29
namespace {
28
30
@@ -43,22 +45,6 @@ class RISCVExpandPseudo : public MachineFunctionPass {
43
45
bool expandMBB (MachineBasicBlock &MBB);
44
46
bool expandMI (MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
45
47
MachineBasicBlock::iterator &NextMBBI);
46
- bool expandAuipcInstPair (MachineBasicBlock &MBB,
47
- MachineBasicBlock::iterator MBBI,
48
- MachineBasicBlock::iterator &NextMBBI,
49
- unsigned FlagsHi, unsigned SecondOpcode);
50
- bool expandLoadLocalAddress (MachineBasicBlock &MBB,
51
- MachineBasicBlock::iterator MBBI,
52
- MachineBasicBlock::iterator &NextMBBI);
53
- bool expandLoadAddress (MachineBasicBlock &MBB,
54
- MachineBasicBlock::iterator MBBI,
55
- MachineBasicBlock::iterator &NextMBBI);
56
- bool expandLoadTLSIEAddress (MachineBasicBlock &MBB,
57
- MachineBasicBlock::iterator MBBI,
58
- MachineBasicBlock::iterator &NextMBBI);
59
- bool expandLoadTLSGDAddress (MachineBasicBlock &MBB,
60
- MachineBasicBlock::iterator MBBI,
61
- MachineBasicBlock::iterator &NextMBBI);
62
48
bool expandVSetVL (MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI);
63
49
bool expandVMSET_VMCLR (MachineBasicBlock &MBB,
64
50
MachineBasicBlock::iterator MBBI, unsigned Opcode);
@@ -96,14 +82,6 @@ bool RISCVExpandPseudo::expandMI(MachineBasicBlock &MBB,
96
82
// expanded instructions for each pseudo is correct in the Size field of the
97
83
// tablegen definition for the pseudo.
98
84
switch (MBBI->getOpcode ()) {
99
- case RISCV::PseudoLLA:
100
- return expandLoadLocalAddress (MBB, MBBI, NextMBBI);
101
- case RISCV::PseudoLA:
102
- return expandLoadAddress (MBB, MBBI, NextMBBI);
103
- case RISCV::PseudoLA_TLS_IE:
104
- return expandLoadTLSIEAddress (MBB, MBBI, NextMBBI);
105
- case RISCV::PseudoLA_TLS_GD:
106
- return expandLoadTLSGDAddress (MBB, MBBI, NextMBBI);
107
85
case RISCV::PseudoVSETVLI:
108
86
case RISCV::PseudoVSETVLIX0:
109
87
case RISCV::PseudoVSETIVLI:
@@ -155,90 +133,6 @@ bool RISCVExpandPseudo::expandMI(MachineBasicBlock &MBB,
155
133
return false ;
156
134
}
157
135
158
- bool RISCVExpandPseudo::expandAuipcInstPair (
159
- MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
160
- MachineBasicBlock::iterator &NextMBBI, unsigned FlagsHi,
161
- unsigned SecondOpcode) {
162
- MachineFunction *MF = MBB.getParent ();
163
- MachineInstr &MI = *MBBI;
164
- DebugLoc DL = MI.getDebugLoc ();
165
-
166
- Register DestReg = MI.getOperand (0 ).getReg ();
167
- const MachineOperand &Symbol = MI.getOperand (1 );
168
-
169
- MachineBasicBlock *NewMBB = MF->CreateMachineBasicBlock (MBB.getBasicBlock ());
170
-
171
- // Tell AsmPrinter that we unconditionally want the symbol of this label to be
172
- // emitted.
173
- NewMBB->setLabelMustBeEmitted ();
174
-
175
- MF->insert (++MBB.getIterator (), NewMBB);
176
-
177
- BuildMI (NewMBB, DL, TII->get (RISCV::AUIPC), DestReg)
178
- .addDisp (Symbol, 0 , FlagsHi);
179
- BuildMI (NewMBB, DL, TII->get (SecondOpcode), DestReg)
180
- .addReg (DestReg)
181
- .addMBB (NewMBB, RISCVII::MO_PCREL_LO);
182
-
183
- // Move all the rest of the instructions to NewMBB.
184
- NewMBB->splice (NewMBB->end (), &MBB, std::next (MBBI), MBB.end ());
185
- // Update machine-CFG edges.
186
- NewMBB->transferSuccessorsAndUpdatePHIs (&MBB);
187
- // Make the original basic block fall-through to the new.
188
- MBB.addSuccessor (NewMBB);
189
-
190
- // Make sure live-ins are correctly attached to this new basic block.
191
- LivePhysRegs LiveRegs;
192
- computeAndAddLiveIns (LiveRegs, *NewMBB);
193
-
194
- NextMBBI = MBB.end ();
195
- MI.eraseFromParent ();
196
- return true ;
197
- }
198
-
199
- bool RISCVExpandPseudo::expandLoadLocalAddress (
200
- MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
201
- MachineBasicBlock::iterator &NextMBBI) {
202
- return expandAuipcInstPair (MBB, MBBI, NextMBBI, RISCVII::MO_PCREL_HI,
203
- RISCV::ADDI);
204
- }
205
-
206
- bool RISCVExpandPseudo::expandLoadAddress (
207
- MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
208
- MachineBasicBlock::iterator &NextMBBI) {
209
- MachineFunction *MF = MBB.getParent ();
210
-
211
- unsigned SecondOpcode;
212
- unsigned FlagsHi;
213
- if (MF->getTarget ().isPositionIndependent ()) {
214
- const auto &STI = MF->getSubtarget <RISCVSubtarget>();
215
- SecondOpcode = STI.is64Bit () ? RISCV::LD : RISCV::LW;
216
- FlagsHi = RISCVII::MO_GOT_HI;
217
- } else {
218
- SecondOpcode = RISCV::ADDI;
219
- FlagsHi = RISCVII::MO_PCREL_HI;
220
- }
221
- return expandAuipcInstPair (MBB, MBBI, NextMBBI, FlagsHi, SecondOpcode);
222
- }
223
-
224
- bool RISCVExpandPseudo::expandLoadTLSIEAddress (
225
- MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
226
- MachineBasicBlock::iterator &NextMBBI) {
227
- MachineFunction *MF = MBB.getParent ();
228
-
229
- const auto &STI = MF->getSubtarget <RISCVSubtarget>();
230
- unsigned SecondOpcode = STI.is64Bit () ? RISCV::LD : RISCV::LW;
231
- return expandAuipcInstPair (MBB, MBBI, NextMBBI, RISCVII::MO_TLS_GOT_HI,
232
- SecondOpcode);
233
- }
234
-
235
- bool RISCVExpandPseudo::expandLoadTLSGDAddress (
236
- MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
237
- MachineBasicBlock::iterator &NextMBBI) {
238
- return expandAuipcInstPair (MBB, MBBI, NextMBBI, RISCVII::MO_TLS_GD_HI,
239
- RISCV::ADDI);
240
- }
241
-
242
136
bool RISCVExpandPseudo::expandVSetVL (MachineBasicBlock &MBB,
243
137
MachineBasicBlock::iterator MBBI) {
244
138
assert (MBBI->getNumExplicitOperands () == 3 && MBBI->getNumOperands () >= 5 &&
@@ -377,12 +271,167 @@ bool RISCVExpandPseudo::expandVRELOAD(MachineBasicBlock &MBB,
377
271
return true ;
378
272
}
379
273
274
+ class RISCVPreRAExpandPseudo : public MachineFunctionPass {
275
+ public:
276
+ const RISCVInstrInfo *TII;
277
+ static char ID;
278
+
279
+ RISCVPreRAExpandPseudo () : MachineFunctionPass(ID) {
280
+ initializeRISCVPreRAExpandPseudoPass (*PassRegistry::getPassRegistry ());
281
+ }
282
+
283
+ bool runOnMachineFunction (MachineFunction &MF) override ;
284
+
285
+ void getAnalysisUsage (AnalysisUsage &AU) const override {
286
+ AU.setPreservesCFG ();
287
+ MachineFunctionPass::getAnalysisUsage (AU);
288
+ }
289
+ StringRef getPassName () const override {
290
+ return RISCV_PRERA_EXPAND_PSEUDO_NAME;
291
+ }
292
+
293
+ private:
294
+ bool expandMBB (MachineBasicBlock &MBB);
295
+ bool expandMI (MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
296
+ MachineBasicBlock::iterator &NextMBBI);
297
+ bool expandAuipcInstPair (MachineBasicBlock &MBB,
298
+ MachineBasicBlock::iterator MBBI,
299
+ MachineBasicBlock::iterator &NextMBBI,
300
+ unsigned FlagsHi, unsigned SecondOpcode);
301
+ bool expandLoadLocalAddress (MachineBasicBlock &MBB,
302
+ MachineBasicBlock::iterator MBBI,
303
+ MachineBasicBlock::iterator &NextMBBI);
304
+ bool expandLoadAddress (MachineBasicBlock &MBB,
305
+ MachineBasicBlock::iterator MBBI,
306
+ MachineBasicBlock::iterator &NextMBBI);
307
+ bool expandLoadTLSIEAddress (MachineBasicBlock &MBB,
308
+ MachineBasicBlock::iterator MBBI,
309
+ MachineBasicBlock::iterator &NextMBBI);
310
+ bool expandLoadTLSGDAddress (MachineBasicBlock &MBB,
311
+ MachineBasicBlock::iterator MBBI,
312
+ MachineBasicBlock::iterator &NextMBBI);
313
+ };
314
+
315
+ char RISCVPreRAExpandPseudo::ID = 0 ;
316
+
317
+ bool RISCVPreRAExpandPseudo::runOnMachineFunction (MachineFunction &MF) {
318
+ TII = static_cast <const RISCVInstrInfo *>(MF.getSubtarget ().getInstrInfo ());
319
+ bool Modified = false ;
320
+ for (auto &MBB : MF)
321
+ Modified |= expandMBB (MBB);
322
+ return Modified;
323
+ }
324
+
325
+ bool RISCVPreRAExpandPseudo::expandMBB (MachineBasicBlock &MBB) {
326
+ bool Modified = false ;
327
+
328
+ MachineBasicBlock::iterator MBBI = MBB.begin (), E = MBB.end ();
329
+ while (MBBI != E) {
330
+ MachineBasicBlock::iterator NMBBI = std::next (MBBI);
331
+ Modified |= expandMI (MBB, MBBI, NMBBI);
332
+ MBBI = NMBBI;
333
+ }
334
+
335
+ return Modified;
336
+ }
337
+
338
+ bool RISCVPreRAExpandPseudo::expandMI (MachineBasicBlock &MBB,
339
+ MachineBasicBlock::iterator MBBI,
340
+ MachineBasicBlock::iterator &NextMBBI) {
341
+
342
+ switch (MBBI->getOpcode ()) {
343
+ case RISCV::PseudoLLA:
344
+ return expandLoadLocalAddress (MBB, MBBI, NextMBBI);
345
+ case RISCV::PseudoLA:
346
+ return expandLoadAddress (MBB, MBBI, NextMBBI);
347
+ case RISCV::PseudoLA_TLS_IE:
348
+ return expandLoadTLSIEAddress (MBB, MBBI, NextMBBI);
349
+ case RISCV::PseudoLA_TLS_GD:
350
+ return expandLoadTLSGDAddress (MBB, MBBI, NextMBBI);
351
+ }
352
+ return false ;
353
+ }
354
+
355
+ bool RISCVPreRAExpandPseudo::expandAuipcInstPair (
356
+ MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
357
+ MachineBasicBlock::iterator &NextMBBI, unsigned FlagsHi,
358
+ unsigned SecondOpcode) {
359
+ MachineFunction *MF = MBB.getParent ();
360
+ MachineInstr &MI = *MBBI;
361
+ DebugLoc DL = MI.getDebugLoc ();
362
+
363
+ Register DestReg = MI.getOperand (0 ).getReg ();
364
+ Register ScratchReg =
365
+ MF->getRegInfo ().createVirtualRegister (&RISCV::GPRRegClass);
366
+
367
+ MachineOperand &Symbol = MI.getOperand (1 );
368
+ Symbol.setTargetFlags (FlagsHi);
369
+ MCSymbol *AUIPCSymbol = MF->getContext ().createNamedTempSymbol (" pcrel_hi" );
370
+
371
+ MachineInstr *MIAUIPC =
372
+ BuildMI (MBB, MBBI, DL, TII->get (RISCV::AUIPC), ScratchReg).add (Symbol);
373
+ MIAUIPC->setPreInstrSymbol (*MF, AUIPCSymbol);
374
+
375
+ MachineInstr *SecondMI =
376
+ BuildMI (MBB, MBBI, DL, TII->get (SecondOpcode), DestReg)
377
+ .addReg (ScratchReg)
378
+ .addSym (AUIPCSymbol, RISCVII::MO_PCREL_LO);
379
+
380
+ if (MI.hasOneMemOperand ())
381
+ SecondMI->addMemOperand (*MF, *MI.memoperands_begin ());
382
+
383
+ MI.eraseFromParent ();
384
+ return true ;
385
+ }
386
+
387
+ bool RISCVPreRAExpandPseudo::expandLoadLocalAddress (
388
+ MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
389
+ MachineBasicBlock::iterator &NextMBBI) {
390
+ return expandAuipcInstPair (MBB, MBBI, NextMBBI, RISCVII::MO_PCREL_HI,
391
+ RISCV::ADDI);
392
+ }
393
+
394
+ bool RISCVPreRAExpandPseudo::expandLoadAddress (
395
+ MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
396
+ MachineBasicBlock::iterator &NextMBBI) {
397
+ MachineFunction *MF = MBB.getParent ();
398
+
399
+ assert (MF->getTarget ().isPositionIndependent ());
400
+ const auto &STI = MF->getSubtarget <RISCVSubtarget>();
401
+ unsigned SecondOpcode = STI.is64Bit () ? RISCV::LD : RISCV::LW;
402
+ return expandAuipcInstPair (MBB, MBBI, NextMBBI, RISCVII::MO_GOT_HI,
403
+ SecondOpcode);
404
+ }
405
+
406
+ bool RISCVPreRAExpandPseudo::expandLoadTLSIEAddress (
407
+ MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
408
+ MachineBasicBlock::iterator &NextMBBI) {
409
+ MachineFunction *MF = MBB.getParent ();
410
+
411
+ const auto &STI = MF->getSubtarget <RISCVSubtarget>();
412
+ unsigned SecondOpcode = STI.is64Bit () ? RISCV::LD : RISCV::LW;
413
+ return expandAuipcInstPair (MBB, MBBI, NextMBBI, RISCVII::MO_TLS_GOT_HI,
414
+ SecondOpcode);
415
+ }
416
+
417
+ bool RISCVPreRAExpandPseudo::expandLoadTLSGDAddress (
418
+ MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
419
+ MachineBasicBlock::iterator &NextMBBI) {
420
+ return expandAuipcInstPair (MBB, MBBI, NextMBBI, RISCVII::MO_TLS_GD_HI,
421
+ RISCV::ADDI);
422
+ }
423
+
380
424
} // end of anonymous namespace
381
425
382
426
INITIALIZE_PASS (RISCVExpandPseudo, " riscv-expand-pseudo" ,
383
427
RISCV_EXPAND_PSEUDO_NAME, false , false )
428
+
429
+ INITIALIZE_PASS(RISCVPreRAExpandPseudo, " riscv-prera-expand-pseudo" ,
430
+ RISCV_PRERA_EXPAND_PSEUDO_NAME, false , false )
431
+
384
432
namespace llvm {
385
433
386
434
FunctionPass *createRISCVExpandPseudoPass () { return new RISCVExpandPseudo (); }
435
+ FunctionPass *createRISCVPreRAExpandPseudoPass () { return new RISCVPreRAExpandPseudo (); }
387
436
388
437
} // end of namespace llvm
0 commit comments