Path: blob/main/contrib/llvm-project/llvm/lib/Target/ARC/ARCExpandPseudos.cpp
35266 views
//===- ARCExpandPseudosPass - ARC expand pseudo loads -----------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This pass expands stores with large offsets into an appropriate sequence.9//===----------------------------------------------------------------------===//1011#include "ARC.h"12#include "ARCInstrInfo.h"13#include "ARCRegisterInfo.h"14#include "ARCSubtarget.h"15#include "MCTargetDesc/ARCInfo.h"16#include "llvm/ADT/Statistic.h"17#include "llvm/CodeGen/MachineFunctionPass.h"18#include "llvm/CodeGen/MachineInstrBuilder.h"19#include "llvm/CodeGen/MachineRegisterInfo.h"2021using namespace llvm;2223#define DEBUG_TYPE "arc-expand-pseudos"2425namespace {2627class ARCExpandPseudos : public MachineFunctionPass {28public:29static char ID;30ARCExpandPseudos() : MachineFunctionPass(ID) {}3132bool runOnMachineFunction(MachineFunction &Fn) override;3334StringRef getPassName() const override { return "ARC Expand Pseudos"; }3536private:37void expandStore(MachineFunction &, MachineBasicBlock::iterator);38void expandCTLZ(MachineFunction &, MachineBasicBlock::iterator);39void expandCTTZ(MachineFunction &, MachineBasicBlock::iterator);4041const ARCInstrInfo *TII;42};4344char ARCExpandPseudos::ID = 0;4546} // end anonymous namespace4748static unsigned getMappedOp(unsigned PseudoOp) {49switch (PseudoOp) {50case ARC::ST_FAR:51return ARC::ST_rs9;52case ARC::STH_FAR:53return ARC::STH_rs9;54case ARC::STB_FAR:55return ARC::STB_rs9;56default:57llvm_unreachable("Unhandled pseudo op.");58}59}6061void ARCExpandPseudos::expandStore(MachineFunction &MF,62MachineBasicBlock::iterator SII) {63MachineInstr &SI = *SII;64Register AddrReg = MF.getRegInfo().createVirtualRegister(&ARC::GPR32RegClass);65Register AddOpc =66isUInt<6>(SI.getOperand(2).getImm()) ? ARC::ADD_rru6 : ARC::ADD_rrlimm;67BuildMI(*SI.getParent(), SI, SI.getDebugLoc(), TII->get(AddOpc), AddrReg)68.addReg(SI.getOperand(1).getReg())69.addImm(SI.getOperand(2).getImm());70BuildMI(*SI.getParent(), SI, SI.getDebugLoc(),71TII->get(getMappedOp(SI.getOpcode())))72.addReg(SI.getOperand(0).getReg())73.addReg(AddrReg)74.addImm(0);75SI.eraseFromParent();76}7778void ARCExpandPseudos::expandCTLZ(MachineFunction &MF,79MachineBasicBlock::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>86MachineInstr &MI = *MII;87const MachineOperand &Dest = MI.getOperand(0);88const MachineOperand &Src = MI.getOperand(1);89Register Ra = MF.getRegInfo().createVirtualRegister(&ARC::GPR32RegClass);90Register Rb = MF.getRegInfo().createVirtualRegister(&ARC::GPR32RegClass);9192BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(ARC::FLS_f_rr), Ra)93.add(Src);94BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(ARC::MOV_cc_ru6), Rb)95.addImm(32)96.addImm(ARCCC::EQ)97.addReg(Ra);98BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(ARC::RSUB_cc_rru6))99.add(Dest)100.addImm(31)101.addImm(ARCCC::NE)102.addReg(Rb);103104MI.eraseFromParent();105}106107void ARCExpandPseudos::expandCTTZ(MachineFunction &MF,108MachineBasicBlock::iterator MII) {109// Expand:110// %R0<def> = CTTZ %R0<kill>, %STATUS<imp-def>111// To:112// %R0<def> = FFS_f_rr %R0<kill>, %STATUS<imp-def>113// %R0<def,tied1> = MOVcc_ru6 %R0<tied0>, 32, pred:1, %STATUS<imp-use>114MachineInstr &MI = *MII;115const MachineOperand &Dest = MI.getOperand(0);116const MachineOperand &Src = MI.getOperand(1);117Register R = MF.getRegInfo().createVirtualRegister(&ARC::GPR32RegClass);118119BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(ARC::FFS_f_rr), R)120.add(Src);121BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(ARC::MOV_cc_ru6))122.add(Dest)123.addImm(32)124.addImm(ARCCC::EQ)125.addReg(R);126127MI.eraseFromParent();128}129130bool ARCExpandPseudos::runOnMachineFunction(MachineFunction &MF) {131const ARCSubtarget *STI = &MF.getSubtarget<ARCSubtarget>();132TII = STI->getInstrInfo();133bool Expanded = false;134for (auto &MBB : MF) {135MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();136while (MBBI != E) {137MachineBasicBlock::iterator NMBBI = std::next(MBBI);138switch (MBBI->getOpcode()) {139case ARC::ST_FAR:140case ARC::STH_FAR:141case ARC::STB_FAR:142expandStore(MF, MBBI);143Expanded = true;144break;145case ARC::CTLZ:146expandCTLZ(MF, MBBI);147Expanded = true;148break;149case ARC::CTTZ:150expandCTTZ(MF, MBBI);151Expanded = true;152break;153default:154break;155}156MBBI = NMBBI;157}158}159return Expanded;160}161162FunctionPass *llvm::createARCExpandPseudosPass() {163return new ARCExpandPseudos();164}165166167