Path: blob/main/contrib/llvm-project/llvm/lib/CodeGen/ExpandPostRAPseudos.cpp
35233 views
//===-- ExpandPostRAPseudos.cpp - Pseudo instruction expansion pass -------===//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 file defines a pass that expands COPY and SUBREG_TO_REG pseudo9// instructions after register allocation.10//11//===----------------------------------------------------------------------===//1213#include "llvm/CodeGen/MachineFunctionPass.h"14#include "llvm/CodeGen/MachineInstr.h"15#include "llvm/CodeGen/Passes.h"16#include "llvm/CodeGen/TargetInstrInfo.h"17#include "llvm/CodeGen/TargetRegisterInfo.h"18#include "llvm/CodeGen/TargetSubtargetInfo.h"19#include "llvm/InitializePasses.h"20#include "llvm/Support/Debug.h"21#include "llvm/Support/raw_ostream.h"2223using namespace llvm;2425#define DEBUG_TYPE "postrapseudos"2627namespace {28struct ExpandPostRA : public MachineFunctionPass {29private:30const TargetRegisterInfo *TRI = nullptr;31const TargetInstrInfo *TII = nullptr;3233public:34static char ID; // Pass identification, replacement for typeid35ExpandPostRA() : MachineFunctionPass(ID) {}3637void getAnalysisUsage(AnalysisUsage &AU) const override {38AU.setPreservesCFG();39AU.addPreservedID(MachineLoopInfoID);40AU.addPreservedID(MachineDominatorsID);41MachineFunctionPass::getAnalysisUsage(AU);42}4344/// runOnMachineFunction - pass entry point45bool runOnMachineFunction(MachineFunction&) override;4647private:48bool LowerSubregToReg(MachineInstr *MI);49};50} // end anonymous namespace5152char ExpandPostRA::ID = 0;53char &llvm::ExpandPostRAPseudosID = ExpandPostRA::ID;5455INITIALIZE_PASS(ExpandPostRA, DEBUG_TYPE,56"Post-RA pseudo instruction expansion pass", false, false)5758bool ExpandPostRA::LowerSubregToReg(MachineInstr *MI) {59MachineBasicBlock *MBB = MI->getParent();60assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&61MI->getOperand(1).isImm() &&62(MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&63MI->getOperand(3).isImm() && "Invalid subreg_to_reg");6465Register DstReg = MI->getOperand(0).getReg();66Register InsReg = MI->getOperand(2).getReg();67assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?");68unsigned SubIdx = MI->getOperand(3).getImm();6970assert(SubIdx != 0 && "Invalid index for insert_subreg");71Register DstSubReg = TRI->getSubReg(DstReg, SubIdx);7273assert(DstReg.isPhysical() &&74"Insert destination must be in a physical register");75assert(InsReg.isPhysical() &&76"Inserted value must be in a physical register");7778LLVM_DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);7980if (MI->allDefsAreDead()) {81MI->setDesc(TII->get(TargetOpcode::KILL));82MI->removeOperand(3); // SubIdx83MI->removeOperand(1); // Imm84LLVM_DEBUG(dbgs() << "subreg: replaced by: " << *MI);85return true;86}8788if (DstSubReg == InsReg) {89// No need to insert an identity copy instruction.90// Watch out for case like this:91// %rax = SUBREG_TO_REG 0, killed %eax, 392// We must leave %rax live.93if (DstReg != InsReg) {94MI->setDesc(TII->get(TargetOpcode::KILL));95MI->removeOperand(3); // SubIdx96MI->removeOperand(1); // Imm97LLVM_DEBUG(dbgs() << "subreg: replace by: " << *MI);98return true;99}100LLVM_DEBUG(dbgs() << "subreg: eliminated!");101} else {102TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,103MI->getOperand(2).isKill());104105// Implicitly define DstReg for subsequent uses.106MachineBasicBlock::iterator CopyMI = MI;107--CopyMI;108CopyMI->addRegisterDefined(DstReg);109LLVM_DEBUG(dbgs() << "subreg: " << *CopyMI);110}111112LLVM_DEBUG(dbgs() << '\n');113MBB->erase(MI);114return true;115}116117/// runOnMachineFunction - Reduce subregister inserts and extracts to register118/// copies.119///120bool ExpandPostRA::runOnMachineFunction(MachineFunction &MF) {121LLVM_DEBUG(dbgs() << "Machine Function\n"122<< "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"123<< "********** Function: " << MF.getName() << '\n');124TRI = MF.getSubtarget().getRegisterInfo();125TII = MF.getSubtarget().getInstrInfo();126127bool MadeChange = false;128129for (MachineBasicBlock &MBB : MF) {130for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {131// Only expand pseudos.132if (!MI.isPseudo())133continue;134135// Give targets a chance to expand even standard pseudos.136if (TII->expandPostRAPseudo(MI)) {137MadeChange = true;138continue;139}140141// Expand standard pseudos.142switch (MI.getOpcode()) {143case TargetOpcode::SUBREG_TO_REG:144MadeChange |= LowerSubregToReg(&MI);145break;146case TargetOpcode::COPY:147TII->lowerCopy(&MI, TRI);148MadeChange = true;149break;150case TargetOpcode::DBG_VALUE:151continue;152case TargetOpcode::INSERT_SUBREG:153case TargetOpcode::EXTRACT_SUBREG:154llvm_unreachable("Sub-register pseudos should have been eliminated.");155}156}157}158159return MadeChange;160}161162163