2019-06-08 14:19:15 +08:00
|
|
|
//==---- SystemZPostRewrite.cpp - Select pseudos after RegAlloc ---*- C++ -*-=//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a pass that is run immediately after VirtRegRewriter
|
|
|
|
// but before MachineCopyPropagation. The purpose is to lower pseudos to
|
|
|
|
// target instructions before any later pass might substitute a register for
|
|
|
|
// another.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SystemZ.h"
|
|
|
|
#include "SystemZInstrInfo.h"
|
|
|
|
#include "SystemZSubtarget.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define SYSTEMZ_POSTREWRITE_NAME "SystemZ Post Rewrite pass"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "systemz-postrewrite"
|
|
|
|
STATISTIC(MemFoldCopies, "Number of copies inserted before folded mem ops.");
|
2019-09-16 15:29:37 +08:00
|
|
|
STATISTIC(LOCRMuxJumps, "Number of LOCRMux jump-sequences (lower is better)");
|
2019-06-08 14:19:15 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
void initializeSystemZPostRewritePass(PassRegistry&);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class SystemZPostRewrite : public MachineFunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
SystemZPostRewrite() : MachineFunctionPass(ID) {
|
|
|
|
initializeSystemZPostRewritePass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
const SystemZInstrInfo *TII;
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &Fn) override;
|
|
|
|
|
|
|
|
StringRef getPassName() const override { return SYSTEMZ_POSTREWRITE_NAME; }
|
|
|
|
|
|
|
|
private:
|
2019-09-16 15:29:37 +08:00
|
|
|
void selectLOCRMux(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
MachineBasicBlock::iterator &NextMBBI,
|
|
|
|
unsigned LowOpcode,
|
|
|
|
unsigned HighOpcode);
|
|
|
|
void selectSELRMux(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
MachineBasicBlock::iterator &NextMBBI,
|
|
|
|
unsigned LowOpcode,
|
|
|
|
unsigned HighOpcode);
|
|
|
|
bool expandCondMove(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
MachineBasicBlock::iterator &NextMBBI);
|
2019-06-08 14:19:15 +08:00
|
|
|
bool selectMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
|
|
|
|
MachineBasicBlock::iterator &NextMBBI);
|
|
|
|
bool selectMBB(MachineBasicBlock &MBB);
|
|
|
|
};
|
|
|
|
|
|
|
|
char SystemZPostRewrite::ID = 0;
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
INITIALIZE_PASS(SystemZPostRewrite, "systemz-post-rewrite",
|
|
|
|
SYSTEMZ_POSTREWRITE_NAME, false, false)
|
|
|
|
|
|
|
|
/// Returns an instance of the Post Rewrite pass.
|
|
|
|
FunctionPass *llvm::createSystemZPostRewritePass(SystemZTargetMachine &TM) {
|
|
|
|
return new SystemZPostRewrite();
|
|
|
|
}
|
|
|
|
|
2019-09-16 15:29:37 +08:00
|
|
|
// MI is a load-register-on-condition pseudo instruction. Replace it with
|
|
|
|
// LowOpcode if source and destination are both low GR32s and HighOpcode if
|
|
|
|
// source and destination are both high GR32s. Otherwise, a branch sequence
|
|
|
|
// is created.
|
|
|
|
void SystemZPostRewrite::selectLOCRMux(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
MachineBasicBlock::iterator &NextMBBI,
|
|
|
|
unsigned LowOpcode,
|
|
|
|
unsigned HighOpcode) {
|
|
|
|
Register DestReg = MBBI->getOperand(0).getReg();
|
|
|
|
Register SrcReg = MBBI->getOperand(2).getReg();
|
|
|
|
bool DestIsHigh = SystemZ::isHighReg(DestReg);
|
|
|
|
bool SrcIsHigh = SystemZ::isHighReg(SrcReg);
|
|
|
|
|
|
|
|
if (!DestIsHigh && !SrcIsHigh)
|
|
|
|
MBBI->setDesc(TII->get(LowOpcode));
|
|
|
|
else if (DestIsHigh && SrcIsHigh)
|
|
|
|
MBBI->setDesc(TII->get(HighOpcode));
|
|
|
|
else
|
|
|
|
expandCondMove(MBB, MBBI, NextMBBI);
|
|
|
|
}
|
|
|
|
|
|
|
|
// MI is a select pseudo instruction. Replace it with LowOpcode if source
|
|
|
|
// and destination are all low GR32s and HighOpcode if source and destination
|
|
|
|
// are all high GR32s. Otherwise, a branch sequence is created.
|
|
|
|
void SystemZPostRewrite::selectSELRMux(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
MachineBasicBlock::iterator &NextMBBI,
|
|
|
|
unsigned LowOpcode,
|
|
|
|
unsigned HighOpcode) {
|
|
|
|
Register DestReg = MBBI->getOperand(0).getReg();
|
|
|
|
Register Src1Reg = MBBI->getOperand(1).getReg();
|
|
|
|
Register Src2Reg = MBBI->getOperand(2).getReg();
|
|
|
|
bool DestIsHigh = SystemZ::isHighReg(DestReg);
|
|
|
|
bool Src1IsHigh = SystemZ::isHighReg(Src1Reg);
|
|
|
|
bool Src2IsHigh = SystemZ::isHighReg(Src2Reg);
|
|
|
|
|
|
|
|
// If sources and destination aren't all high or all low, we may be able to
|
|
|
|
// simplify the operation by moving one of the sources to the destination
|
|
|
|
// first. But only if this doesn't clobber the other source.
|
|
|
|
if (DestReg != Src1Reg && DestReg != Src2Reg) {
|
|
|
|
if (DestIsHigh != Src1IsHigh) {
|
|
|
|
BuildMI(*MBBI->getParent(), MBBI, MBBI->getDebugLoc(),
|
|
|
|
TII->get(SystemZ::COPY), DestReg)
|
|
|
|
.addReg(MBBI->getOperand(1).getReg(), getRegState(MBBI->getOperand(1)));
|
|
|
|
MBBI->getOperand(1).setReg(DestReg);
|
|
|
|
Src1Reg = DestReg;
|
|
|
|
Src1IsHigh = DestIsHigh;
|
|
|
|
} else if (DestIsHigh != Src2IsHigh) {
|
|
|
|
BuildMI(*MBBI->getParent(), MBBI, MBBI->getDebugLoc(),
|
|
|
|
TII->get(SystemZ::COPY), DestReg)
|
|
|
|
.addReg(MBBI->getOperand(2).getReg(), getRegState(MBBI->getOperand(2)));
|
|
|
|
MBBI->getOperand(2).setReg(DestReg);
|
|
|
|
Src2Reg = DestReg;
|
|
|
|
Src2IsHigh = DestIsHigh;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the destination (now) matches one source, prefer this to be first.
|
|
|
|
if (DestReg != Src1Reg && DestReg == Src2Reg) {
|
|
|
|
TII->commuteInstruction(*MBBI, false, 1, 2);
|
|
|
|
std::swap(Src1Reg, Src2Reg);
|
|
|
|
std::swap(Src1IsHigh, Src2IsHigh);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!DestIsHigh && !Src1IsHigh && !Src2IsHigh)
|
|
|
|
MBBI->setDesc(TII->get(LowOpcode));
|
|
|
|
else if (DestIsHigh && Src1IsHigh && Src2IsHigh)
|
|
|
|
MBBI->setDesc(TII->get(HighOpcode));
|
|
|
|
else
|
|
|
|
// Given the simplification above, we must already have a two-operand case.
|
|
|
|
expandCondMove(MBB, MBBI, NextMBBI);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace MBBI by a branch sequence that performs a conditional move of
|
|
|
|
// operand 2 to the destination register. Operand 1 is expected to be the
|
|
|
|
// same register as the destination.
|
|
|
|
bool SystemZPostRewrite::expandCondMove(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
MachineBasicBlock::iterator &NextMBBI) {
|
|
|
|
MachineFunction &MF = *MBB.getParent();
|
|
|
|
const BasicBlock *BB = MBB.getBasicBlock();
|
|
|
|
MachineInstr &MI = *MBBI;
|
|
|
|
DebugLoc DL = MI.getDebugLoc();
|
|
|
|
Register DestReg = MI.getOperand(0).getReg();
|
|
|
|
Register SrcReg = MI.getOperand(2).getReg();
|
|
|
|
unsigned CCValid = MI.getOperand(3).getImm();
|
|
|
|
unsigned CCMask = MI.getOperand(4).getImm();
|
|
|
|
assert(DestReg == MI.getOperand(1).getReg() &&
|
|
|
|
"Expected destination and first source operand to be the same.");
|
|
|
|
|
|
|
|
LivePhysRegs LiveRegs(TII->getRegisterInfo());
|
|
|
|
LiveRegs.addLiveOuts(MBB);
|
|
|
|
for (auto I = std::prev(MBB.end()); I != MBBI; --I)
|
|
|
|
LiveRegs.stepBackward(*I);
|
|
|
|
|
|
|
|
// Splice MBB at MI, moving the rest of the block into RestMBB.
|
|
|
|
MachineBasicBlock *RestMBB = MF.CreateMachineBasicBlock(BB);
|
|
|
|
MF.insert(std::next(MachineFunction::iterator(MBB)), RestMBB);
|
|
|
|
RestMBB->splice(RestMBB->begin(), &MBB, MI, MBB.end());
|
|
|
|
RestMBB->transferSuccessors(&MBB);
|
|
|
|
for (auto I = LiveRegs.begin(); I != LiveRegs.end(); ++I)
|
|
|
|
RestMBB->addLiveIn(*I);
|
|
|
|
|
|
|
|
// Create a new block MoveMBB to hold the move instruction.
|
|
|
|
MachineBasicBlock *MoveMBB = MF.CreateMachineBasicBlock(BB);
|
|
|
|
MF.insert(std::next(MachineFunction::iterator(MBB)), MoveMBB);
|
|
|
|
MoveMBB->addLiveIn(SrcReg);
|
|
|
|
for (auto I = LiveRegs.begin(); I != LiveRegs.end(); ++I)
|
|
|
|
MoveMBB->addLiveIn(*I);
|
|
|
|
|
|
|
|
// At the end of MBB, create a conditional branch to RestMBB if the
|
|
|
|
// condition is false, otherwise fall through to MoveMBB.
|
|
|
|
BuildMI(&MBB, DL, TII->get(SystemZ::BRC))
|
|
|
|
.addImm(CCValid).addImm(CCMask ^ CCValid).addMBB(RestMBB);
|
|
|
|
MBB.addSuccessor(RestMBB);
|
|
|
|
MBB.addSuccessor(MoveMBB);
|
|
|
|
|
|
|
|
// In MoveMBB, emit an instruction to move SrcReg into DestReg,
|
|
|
|
// then fall through to RestMBB.
|
|
|
|
BuildMI(*MoveMBB, MoveMBB->end(), DL, TII->get(SystemZ::COPY), DestReg)
|
|
|
|
.addReg(MI.getOperand(2).getReg(), getRegState(MI.getOperand(2)));
|
|
|
|
MoveMBB->addSuccessor(RestMBB);
|
|
|
|
|
|
|
|
NextMBBI = MBB.end();
|
|
|
|
MI.eraseFromParent();
|
|
|
|
LOCRMuxJumps++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-08 14:19:15 +08:00
|
|
|
/// If MBBI references a pseudo instruction that should be selected here,
|
|
|
|
/// do it and return true. Otherwise return false.
|
|
|
|
bool SystemZPostRewrite::selectMI(MachineBasicBlock &MBB,
|
2019-09-16 15:29:37 +08:00
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
MachineBasicBlock::iterator &NextMBBI) {
|
2019-06-08 14:19:15 +08:00
|
|
|
MachineInstr &MI = *MBBI;
|
|
|
|
unsigned Opcode = MI.getOpcode();
|
|
|
|
|
|
|
|
// Note: If this could be done during regalloc in foldMemoryOperandImpl()
|
|
|
|
// while also updating the LiveIntervals, there would be no need for the
|
|
|
|
// MemFoldPseudo to begin with.
|
|
|
|
int TargetMemOpcode = SystemZ::getTargetMemOpcode(Opcode);
|
|
|
|
if (TargetMemOpcode != -1) {
|
|
|
|
MI.setDesc(TII->get(TargetMemOpcode));
|
|
|
|
MI.tieOperands(0, 1);
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register DstReg = MI.getOperand(0).getReg();
|
2019-06-08 14:19:15 +08:00
|
|
|
MachineOperand &SrcMO = MI.getOperand(1);
|
|
|
|
if (DstReg != SrcMO.getReg()) {
|
|
|
|
BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(SystemZ::COPY), DstReg)
|
|
|
|
.addReg(SrcMO.getReg());
|
|
|
|
SrcMO.setReg(DstReg);
|
|
|
|
MemFoldCopies++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-16 15:29:37 +08:00
|
|
|
switch (Opcode) {
|
|
|
|
case SystemZ::LOCRMux:
|
|
|
|
selectLOCRMux(MBB, MBBI, NextMBBI, SystemZ::LOCR, SystemZ::LOCFHR);
|
|
|
|
return true;
|
|
|
|
case SystemZ::SELRMux:
|
|
|
|
selectSELRMux(MBB, MBBI, NextMBBI, SystemZ::SELR, SystemZ::SELFHR);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-08 14:19:15 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Iterate over the instructions in basic block MBB and select any
|
|
|
|
/// pseudo instructions. Return true if anything was modified.
|
|
|
|
bool SystemZPostRewrite::selectMBB(MachineBasicBlock &MBB) {
|
|
|
|
bool Modified = false;
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
|
|
|
|
while (MBBI != E) {
|
|
|
|
MachineBasicBlock::iterator NMBBI = std::next(MBBI);
|
|
|
|
Modified |= selectMI(MBB, MBBI, NMBBI);
|
|
|
|
MBBI = NMBBI;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SystemZPostRewrite::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
TII = static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
|
|
|
|
|
|
|
|
bool Modified = false;
|
|
|
|
for (auto &MBB : MF)
|
|
|
|
Modified |= selectMBB(MBB);
|
|
|
|
|
|
|
|
return Modified;
|
|
|
|
}
|
|
|
|
|