Path: blob/main/contrib/llvm-project/llvm/lib/Target/SystemZ/SystemZPostRewrite.cpp
35269 views
//==---- SystemZPostRewrite.cpp - Select pseudos after RegAlloc ---*- 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 file contains a pass that is run immediately after VirtRegRewriter9// but before MachineCopyPropagation. The purpose is to lower pseudos to10// target instructions before any later pass might substitute a register for11// another.12//13//===----------------------------------------------------------------------===//1415#include "SystemZ.h"16#include "SystemZInstrInfo.h"17#include "SystemZSubtarget.h"18#include "llvm/ADT/Statistic.h"19#include "llvm/CodeGen/LivePhysRegs.h"20#include "llvm/CodeGen/MachineFunctionPass.h"21#include "llvm/CodeGen/MachineInstrBuilder.h"22using namespace llvm;2324#define DEBUG_TYPE "systemz-postrewrite"25STATISTIC(MemFoldCopies, "Number of copies inserted before folded mem ops.");26STATISTIC(LOCRMuxJumps, "Number of LOCRMux jump-sequences (lower is better)");2728namespace {2930class SystemZPostRewrite : public MachineFunctionPass {31public:32static char ID;33SystemZPostRewrite() : MachineFunctionPass(ID) {34initializeSystemZPostRewritePass(*PassRegistry::getPassRegistry());35}3637const SystemZInstrInfo *TII;3839bool runOnMachineFunction(MachineFunction &Fn) override;4041private:42void selectLOCRMux(MachineBasicBlock &MBB,43MachineBasicBlock::iterator MBBI,44MachineBasicBlock::iterator &NextMBBI,45unsigned LowOpcode,46unsigned HighOpcode);47void selectSELRMux(MachineBasicBlock &MBB,48MachineBasicBlock::iterator MBBI,49MachineBasicBlock::iterator &NextMBBI,50unsigned LowOpcode,51unsigned HighOpcode);52bool expandCondMove(MachineBasicBlock &MBB,53MachineBasicBlock::iterator MBBI,54MachineBasicBlock::iterator &NextMBBI);55bool selectMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,56MachineBasicBlock::iterator &NextMBBI);57bool selectMBB(MachineBasicBlock &MBB);58};5960char SystemZPostRewrite::ID = 0;6162} // end anonymous namespace6364INITIALIZE_PASS(SystemZPostRewrite, "systemz-post-rewrite",65"SystemZ Post Rewrite pass", false, false)6667/// Returns an instance of the Post Rewrite pass.68FunctionPass *llvm::createSystemZPostRewritePass(SystemZTargetMachine &TM) {69return new SystemZPostRewrite();70}7172// MI is a load-register-on-condition pseudo instruction. Replace it with73// LowOpcode if source and destination are both low GR32s and HighOpcode if74// source and destination are both high GR32s. Otherwise, a branch sequence75// is created.76void SystemZPostRewrite::selectLOCRMux(MachineBasicBlock &MBB,77MachineBasicBlock::iterator MBBI,78MachineBasicBlock::iterator &NextMBBI,79unsigned LowOpcode,80unsigned HighOpcode) {81Register DestReg = MBBI->getOperand(0).getReg();82Register SrcReg = MBBI->getOperand(2).getReg();83bool DestIsHigh = SystemZ::isHighReg(DestReg);84bool SrcIsHigh = SystemZ::isHighReg(SrcReg);8586if (!DestIsHigh && !SrcIsHigh)87MBBI->setDesc(TII->get(LowOpcode));88else if (DestIsHigh && SrcIsHigh)89MBBI->setDesc(TII->get(HighOpcode));90else91expandCondMove(MBB, MBBI, NextMBBI);92}9394// MI is a select pseudo instruction. Replace it with LowOpcode if source95// and destination are all low GR32s and HighOpcode if source and destination96// are all high GR32s. Otherwise, a branch sequence is created.97void SystemZPostRewrite::selectSELRMux(MachineBasicBlock &MBB,98MachineBasicBlock::iterator MBBI,99MachineBasicBlock::iterator &NextMBBI,100unsigned LowOpcode,101unsigned HighOpcode) {102Register DestReg = MBBI->getOperand(0).getReg();103Register Src1Reg = MBBI->getOperand(1).getReg();104Register Src2Reg = MBBI->getOperand(2).getReg();105bool DestIsHigh = SystemZ::isHighReg(DestReg);106bool Src1IsHigh = SystemZ::isHighReg(Src1Reg);107bool Src2IsHigh = SystemZ::isHighReg(Src2Reg);108109// If sources and destination aren't all high or all low, we may be able to110// simplify the operation by moving one of the sources to the destination111// first. But only if this doesn't clobber the other source.112if (DestReg != Src1Reg && DestReg != Src2Reg) {113if (DestIsHigh != Src1IsHigh) {114BuildMI(*MBBI->getParent(), MBBI, MBBI->getDebugLoc(),115TII->get(SystemZ::COPY), DestReg)116.addReg(MBBI->getOperand(1).getReg(), getRegState(MBBI->getOperand(1)));117MBBI->getOperand(1).setReg(DestReg);118Src1Reg = DestReg;119Src1IsHigh = DestIsHigh;120} else if (DestIsHigh != Src2IsHigh) {121BuildMI(*MBBI->getParent(), MBBI, MBBI->getDebugLoc(),122TII->get(SystemZ::COPY), DestReg)123.addReg(MBBI->getOperand(2).getReg(), getRegState(MBBI->getOperand(2)));124MBBI->getOperand(2).setReg(DestReg);125Src2Reg = DestReg;126Src2IsHigh = DestIsHigh;127}128}129130// If the destination (now) matches one source, prefer this to be first.131if (DestReg != Src1Reg && DestReg == Src2Reg) {132TII->commuteInstruction(*MBBI, false, 1, 2);133std::swap(Src1Reg, Src2Reg);134std::swap(Src1IsHigh, Src2IsHigh);135}136137if (!DestIsHigh && !Src1IsHigh && !Src2IsHigh)138MBBI->setDesc(TII->get(LowOpcode));139else if (DestIsHigh && Src1IsHigh && Src2IsHigh)140MBBI->setDesc(TII->get(HighOpcode));141else142// Given the simplification above, we must already have a two-operand case.143expandCondMove(MBB, MBBI, NextMBBI);144}145146// Replace MBBI by a branch sequence that performs a conditional move of147// operand 2 to the destination register. Operand 1 is expected to be the148// same register as the destination.149bool SystemZPostRewrite::expandCondMove(MachineBasicBlock &MBB,150MachineBasicBlock::iterator MBBI,151MachineBasicBlock::iterator &NextMBBI) {152MachineFunction &MF = *MBB.getParent();153const BasicBlock *BB = MBB.getBasicBlock();154MachineInstr &MI = *MBBI;155DebugLoc DL = MI.getDebugLoc();156Register DestReg = MI.getOperand(0).getReg();157Register SrcReg = MI.getOperand(2).getReg();158unsigned CCValid = MI.getOperand(3).getImm();159unsigned CCMask = MI.getOperand(4).getImm();160assert(DestReg == MI.getOperand(1).getReg() &&161"Expected destination and first source operand to be the same.");162163LivePhysRegs LiveRegs(TII->getRegisterInfo());164LiveRegs.addLiveOuts(MBB);165for (auto I = std::prev(MBB.end()); I != MBBI; --I)166LiveRegs.stepBackward(*I);167168// Splice MBB at MI, moving the rest of the block into RestMBB.169MachineBasicBlock *RestMBB = MF.CreateMachineBasicBlock(BB);170MF.insert(std::next(MachineFunction::iterator(MBB)), RestMBB);171RestMBB->splice(RestMBB->begin(), &MBB, MI, MBB.end());172RestMBB->transferSuccessors(&MBB);173for (MCPhysReg R : LiveRegs)174RestMBB->addLiveIn(R);175176// Create a new block MoveMBB to hold the move instruction.177MachineBasicBlock *MoveMBB = MF.CreateMachineBasicBlock(BB);178MF.insert(std::next(MachineFunction::iterator(MBB)), MoveMBB);179MoveMBB->addLiveIn(SrcReg);180for (MCPhysReg R : LiveRegs)181MoveMBB->addLiveIn(R);182183// At the end of MBB, create a conditional branch to RestMBB if the184// condition is false, otherwise fall through to MoveMBB.185BuildMI(&MBB, DL, TII->get(SystemZ::BRC))186.addImm(CCValid).addImm(CCMask ^ CCValid).addMBB(RestMBB);187MBB.addSuccessor(RestMBB);188MBB.addSuccessor(MoveMBB);189190// In MoveMBB, emit an instruction to move SrcReg into DestReg,191// then fall through to RestMBB.192BuildMI(*MoveMBB, MoveMBB->end(), DL, TII->get(SystemZ::COPY), DestReg)193.addReg(MI.getOperand(2).getReg(), getRegState(MI.getOperand(2)));194MoveMBB->addSuccessor(RestMBB);195196NextMBBI = MBB.end();197MI.eraseFromParent();198LOCRMuxJumps++;199return true;200}201202/// If MBBI references a pseudo instruction that should be selected here,203/// do it and return true. Otherwise return false.204bool SystemZPostRewrite::selectMI(MachineBasicBlock &MBB,205MachineBasicBlock::iterator MBBI,206MachineBasicBlock::iterator &NextMBBI) {207MachineInstr &MI = *MBBI;208unsigned Opcode = MI.getOpcode();209210// Note: If this could be done during regalloc in foldMemoryOperandImpl()211// while also updating the LiveIntervals, there would be no need for the212// MemFoldPseudo to begin with.213int TargetMemOpcode = SystemZ::getTargetMemOpcode(Opcode);214if (TargetMemOpcode != -1) {215MI.setDesc(TII->get(TargetMemOpcode));216MI.tieOperands(0, 1);217Register DstReg = MI.getOperand(0).getReg();218MachineOperand &SrcMO = MI.getOperand(1);219if (DstReg != SrcMO.getReg()) {220BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(SystemZ::COPY), DstReg)221.addReg(SrcMO.getReg());222SrcMO.setReg(DstReg);223MemFoldCopies++;224}225return true;226}227228switch (Opcode) {229case SystemZ::LOCRMux:230selectLOCRMux(MBB, MBBI, NextMBBI, SystemZ::LOCR, SystemZ::LOCFHR);231return true;232case SystemZ::SELRMux:233selectSELRMux(MBB, MBBI, NextMBBI, SystemZ::SELR, SystemZ::SELFHR);234return true;235}236237return false;238}239240/// Iterate over the instructions in basic block MBB and select any241/// pseudo instructions. Return true if anything was modified.242bool SystemZPostRewrite::selectMBB(MachineBasicBlock &MBB) {243bool Modified = false;244245MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();246while (MBBI != E) {247MachineBasicBlock::iterator NMBBI = std::next(MBBI);248Modified |= selectMI(MBB, MBBI, NMBBI);249MBBI = NMBBI;250}251252return Modified;253}254255bool SystemZPostRewrite::runOnMachineFunction(MachineFunction &MF) {256TII = MF.getSubtarget<SystemZSubtarget>().getInstrInfo();257258bool Modified = false;259for (auto &MBB : MF)260Modified |= selectMBB(MBB);261262return Modified;263}264265266267