1 //===- ARCExpandPseudosPass - ARC expand pseudo loads -----------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This pass expands stores with large offsets into an appropriate sequence.
10 //===----------------------------------------------------------------------===//
13 #include "ARCInstrInfo.h"
14 #include "ARCRegisterInfo.h"
15 #include "ARCSubtarget.h"
16 #include "MCTargetDesc/ARCInfo.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #define DEBUG_TYPE "arc-expand-pseudos"
28 class ARCExpandPseudos
: public MachineFunctionPass
{
31 ARCExpandPseudos() : MachineFunctionPass(ID
) {}
33 bool runOnMachineFunction(MachineFunction
&Fn
) override
;
35 StringRef
getPassName() const override
{ return "ARC Expand Pseudos"; }
38 void expandStore(MachineFunction
&, MachineBasicBlock::iterator
);
39 void expandCTLZ(MachineFunction
&, MachineBasicBlock::iterator
);
40 void expandCTTZ(MachineFunction
&, MachineBasicBlock::iterator
);
42 const ARCInstrInfo
*TII
;
45 char ARCExpandPseudos::ID
= 0;
47 } // end anonymous namespace
49 static unsigned getMappedOp(unsigned PseudoOp
) {
58 llvm_unreachable("Unhandled pseudo op.");
62 void ARCExpandPseudos::expandStore(MachineFunction
&MF
,
63 MachineBasicBlock::iterator SII
) {
64 MachineInstr
&SI
= *SII
;
65 Register AddrReg
= MF
.getRegInfo().createVirtualRegister(&ARC::GPR32RegClass
);
67 isUInt
<6>(SI
.getOperand(2).getImm()) ? ARC::ADD_rru6
: ARC::ADD_rrlimm
;
68 BuildMI(*SI
.getParent(), SI
, SI
.getDebugLoc(), TII
->get(AddOpc
), AddrReg
)
69 .addReg(SI
.getOperand(1).getReg())
70 .addImm(SI
.getOperand(2).getImm());
71 BuildMI(*SI
.getParent(), SI
, SI
.getDebugLoc(),
72 TII
->get(getMappedOp(SI
.getOpcode())))
73 .addReg(SI
.getOperand(0).getReg())
79 void ARCExpandPseudos::expandCTLZ(MachineFunction
&MF
,
80 MachineBasicBlock::iterator MII
) {
82 // %R2<def> = CTLZ %R0, %STATUS<imp-def>
84 // %R2<def> = FLS_f_rr %R0, %STATUS<imp-def>
85 // %R2<def,tied1> = MOV_cc_ru6 %R2<tied0>, 32, pred:1, %STATUS<imp-use>
86 // %R2<def,tied1> = RSUB_cc_rru6 %R2<tied0>, 31, pred:2, %STATUS<imp-use>
87 MachineInstr
&MI
= *MII
;
88 const MachineOperand
&Dest
= MI
.getOperand(0);
89 const MachineOperand
&Src
= MI
.getOperand(1);
90 Register Ra
= MF
.getRegInfo().createVirtualRegister(&ARC::GPR32RegClass
);
91 Register Rb
= MF
.getRegInfo().createVirtualRegister(&ARC::GPR32RegClass
);
93 BuildMI(*MI
.getParent(), MI
, MI
.getDebugLoc(), TII
->get(ARC::FLS_f_rr
), Ra
)
95 BuildMI(*MI
.getParent(), MI
, MI
.getDebugLoc(), TII
->get(ARC::MOV_cc_ru6
), Rb
)
99 BuildMI(*MI
.getParent(), MI
, MI
.getDebugLoc(), TII
->get(ARC::RSUB_cc_rru6
))
105 MI
.eraseFromParent();
108 void ARCExpandPseudos::expandCTTZ(MachineFunction
&MF
,
109 MachineBasicBlock::iterator MII
) {
111 // %R0<def> = CTTZ %R0<kill>, %STATUS<imp-def>
113 // %R0<def> = FFS_f_rr %R0<kill>, %STATUS<imp-def>
114 // %R0<def,tied1> = MOVcc_ru6 %R0<tied0>, 32, pred:1, %STATUS<imp-use>
115 MachineInstr
&MI
= *MII
;
116 const MachineOperand
&Dest
= MI
.getOperand(0);
117 const MachineOperand
&Src
= MI
.getOperand(1);
118 Register R
= MF
.getRegInfo().createVirtualRegister(&ARC::GPR32RegClass
);
120 BuildMI(*MI
.getParent(), MI
, MI
.getDebugLoc(), TII
->get(ARC::FFS_f_rr
), R
)
122 BuildMI(*MI
.getParent(), MI
, MI
.getDebugLoc(), TII
->get(ARC::MOV_cc_ru6
))
128 MI
.eraseFromParent();
131 bool ARCExpandPseudos::runOnMachineFunction(MachineFunction
&MF
) {
132 const ARCSubtarget
*STI
= &MF
.getSubtarget
<ARCSubtarget
>();
133 TII
= STI
->getInstrInfo();
134 bool Expanded
= false;
135 for (auto &MBB
: MF
) {
136 MachineBasicBlock::iterator MBBI
= MBB
.begin(), E
= MBB
.end();
138 MachineBasicBlock::iterator NMBBI
= std::next(MBBI
);
139 switch (MBBI
->getOpcode()) {
143 expandStore(MF
, MBBI
);
147 expandCTLZ(MF
, MBBI
);
151 expandCTTZ(MF
, MBBI
);
163 FunctionPass
*llvm::createARCExpandPseudosPass() {
164 return new ARCExpandPseudos();