1 //===- ARCExpandPseudosPass - ARC expand pseudo loads -----------*- C++ -*-===// 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 pass expands stores with large offsets into an appropriate sequence. 10 //===----------------------------------------------------------------------===// 11 12 #include "ARC.h" 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" 21 22 using namespace llvm; 23 24 #define DEBUG_TYPE "arc-expand-pseudos" 25 26 namespace { 27 28 class ARCExpandPseudos : public MachineFunctionPass { 29 public: 30 static char ID; 31 ARCExpandPseudos() : MachineFunctionPass(ID) {} 32 33 bool runOnMachineFunction(MachineFunction &Fn) override; 34 35 StringRef getPassName() const override { return "ARC Expand Pseudos"; } 36 37 private: 38 void ExpandStore(MachineFunction &, MachineBasicBlock::iterator); 39 void ExpandCTLZ(MachineFunction &, MachineBasicBlock::iterator); 40 41 const ARCInstrInfo *TII; 42 }; 43 44 char ARCExpandPseudos::ID = 0; 45 46 } // end anonymous namespace 47 48 static unsigned getMappedOp(unsigned PseudoOp) { 49 switch (PseudoOp) { 50 case ARC::ST_FAR: 51 return ARC::ST_rs9; 52 case ARC::STH_FAR: 53 return ARC::STH_rs9; 54 case ARC::STB_FAR: 55 return ARC::STB_rs9; 56 default: 57 llvm_unreachable("Unhandled pseudo op."); 58 } 59 } 60 61 void ARCExpandPseudos::ExpandStore(MachineFunction &MF, 62 MachineBasicBlock::iterator SII) { 63 MachineInstr &SI = *SII; 64 Register AddrReg = MF.getRegInfo().createVirtualRegister(&ARC::GPR32RegClass); 65 Register AddOpc = 66 isUInt<6>(SI.getOperand(2).getImm()) ? ARC::ADD_rru6 : ARC::ADD_rrlimm; 67 BuildMI(*SI.getParent(), SI, SI.getDebugLoc(), TII->get(AddOpc), AddrReg) 68 .addReg(SI.getOperand(1).getReg()) 69 .addImm(SI.getOperand(2).getImm()); 70 BuildMI(*SI.getParent(), SI, SI.getDebugLoc(), 71 TII->get(getMappedOp(SI.getOpcode()))) 72 .addReg(SI.getOperand(0).getReg()) 73 .addReg(AddrReg) 74 .addImm(0); 75 SI.eraseFromParent(); 76 } 77 78 void ARCExpandPseudos::ExpandCTLZ(MachineFunction &MF, 79 MachineBasicBlock::iterator MII) { 80 // Expand: 81 // %R2<def> = CTLZ %R0, %STATUS<imp-def> 82 // To: 83 // %R2<def> = FLS_f_rr %R0, %STATUS<imp-def> 84 // %R2<def,tied1> = MOV_cc_ru6 %R2<tied0>, 32, pred:1, %STATUS<imp-use> 85 // %R2<def,tied1> = RSUB_cc_rru6 %R2<tied0>, 31, pred:2, %STATUS<imp-use> 86 MachineInstr &MI = *MII; 87 const MachineOperand &Dest = MI.getOperand(0); 88 const MachineOperand &Src = MI.getOperand(1); 89 Register Ra = MF.getRegInfo().createVirtualRegister(&ARC::GPR32RegClass); 90 Register Rb = MF.getRegInfo().createVirtualRegister(&ARC::GPR32RegClass); 91 92 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(ARC::FLS_f_rr), Ra) 93 .add(Src); 94 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(ARC::MOV_cc_ru6), Rb) 95 .addImm(32) 96 .addImm(ARCCC::EQ) 97 .addReg(Ra); 98 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(ARC::RSUB_cc_rru6)) 99 .add(Dest) 100 .addImm(31) 101 .addImm(ARCCC::NE) 102 .addReg(Rb); 103 104 MI.eraseFromParent(); 105 } 106 107 bool ARCExpandPseudos::runOnMachineFunction(MachineFunction &MF) { 108 const ARCSubtarget *STI = &MF.getSubtarget<ARCSubtarget>(); 109 TII = STI->getInstrInfo(); 110 bool Expanded = false; 111 for (auto &MBB : MF) { 112 MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end(); 113 while (MBBI != E) { 114 MachineBasicBlock::iterator NMBBI = std::next(MBBI); 115 switch (MBBI->getOpcode()) { 116 case ARC::ST_FAR: 117 case ARC::STH_FAR: 118 case ARC::STB_FAR: 119 ExpandStore(MF, MBBI); 120 Expanded = true; 121 break; 122 case ARC::CTLZ: 123 ExpandCTLZ(MF, MBBI); 124 Expanded = true; 125 break; 126 default: 127 break; 128 } 129 MBBI = NMBBI; 130 } 131 } 132 return Expanded; 133 } 134 135 FunctionPass *llvm::createARCExpandPseudosPass() { 136 return new ARCExpandPseudos(); 137 } 138