1480 lines
49 KiB
C++
1480 lines
49 KiB
C++
//===-- RISCVInstrInfo.cpp - RISCV Instruction Information ------*- 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 the RISCV implementation of the TargetInstrInfo class.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "RISCVInstrInfo.h"
|
|
#include "MCTargetDesc/RISCVMatInt.h"
|
|
#include "RISCV.h"
|
|
#include "RISCVMachineFunctionInfo.h"
|
|
#include "RISCVSubtarget.h"
|
|
#include "RISCVTargetMachine.h"
|
|
#include "llvm/ADT/STLExtras.h"
|
|
#include "llvm/ADT/SmallVector.h"
|
|
#include "llvm/Analysis/MemoryLocation.h"
|
|
#include "llvm/CodeGen/LiveIntervals.h"
|
|
#include "llvm/CodeGen/LiveVariables.h"
|
|
#include "llvm/CodeGen/MachineCombinerPattern.h"
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
#include "llvm/CodeGen/RegisterScavenging.h"
|
|
#include "llvm/IR/DebugInfoMetadata.h"
|
|
#include "llvm/MC/MCInstBuilder.h"
|
|
#include "llvm/MC/TargetRegistry.h"
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
using namespace llvm;
|
|
|
|
#define GEN_CHECK_COMPRESS_INSTR
|
|
#include "RISCVGenCompressInstEmitter.inc"
|
|
|
|
#define GET_INSTRINFO_CTOR_DTOR
|
|
#define GET_INSTRINFO_NAMED_OPS
|
|
#include "RISCVGenInstrInfo.inc"
|
|
|
|
static cl::opt<bool> PreferWholeRegisterMove(
|
|
"riscv-prefer-whole-register-move", cl::init(false), cl::Hidden,
|
|
cl::desc("Prefer whole register move for vector registers."));
|
|
|
|
namespace llvm::RISCVVPseudosTable {
|
|
|
|
using namespace RISCV;
|
|
|
|
#define GET_RISCVVPseudosTable_IMPL
|
|
#include "RISCVGenSearchableTables.inc"
|
|
|
|
} // namespace llvm::RISCVVPseudosTable
|
|
|
|
RISCVInstrInfo::RISCVInstrInfo(RISCVSubtarget &STI)
|
|
: RISCVGenInstrInfo(RISCV::ADJCALLSTACKDOWN, RISCV::ADJCALLSTACKUP),
|
|
STI(STI) {}
|
|
|
|
MCInst RISCVInstrInfo::getNop() const {
|
|
if (STI.hasStdExtCOrZca())
|
|
return MCInstBuilder(RISCV::C_NOP);
|
|
return MCInstBuilder(RISCV::ADDI)
|
|
.addReg(RISCV::X0)
|
|
.addReg(RISCV::X0)
|
|
.addImm(0);
|
|
}
|
|
|
|
unsigned RISCVInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
|
|
int &FrameIndex) const {
|
|
switch (MI.getOpcode()) {
|
|
default:
|
|
return 0;
|
|
case RISCV::LB:
|
|
case RISCV::LBU:
|
|
case RISCV::LH:
|
|
case RISCV::LHU:
|
|
case RISCV::FLH:
|
|
case RISCV::LW:
|
|
case RISCV::FLW:
|
|
case RISCV::LWU:
|
|
case RISCV::LD:
|
|
case RISCV::FLD:
|
|
break;
|
|
}
|
|
|
|
if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
|
|
MI.getOperand(2).getImm() == 0) {
|
|
FrameIndex = MI.getOperand(1).getIndex();
|
|
return MI.getOperand(0).getReg();
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
unsigned RISCVInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
|
|
int &FrameIndex) const {
|
|
switch (MI.getOpcode()) {
|
|
default:
|
|
return 0;
|
|
case RISCV::SB:
|
|
case RISCV::SH:
|
|
case RISCV::SW:
|
|
case RISCV::FSH:
|
|
case RISCV::FSW:
|
|
case RISCV::SD:
|
|
case RISCV::FSD:
|
|
break;
|
|
}
|
|
|
|
if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
|
|
MI.getOperand(2).getImm() == 0) {
|
|
FrameIndex = MI.getOperand(1).getIndex();
|
|
return MI.getOperand(0).getReg();
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static bool forwardCopyWillClobberTuple(unsigned DstReg, unsigned SrcReg,
|
|
unsigned NumRegs) {
|
|
return DstReg > SrcReg && (DstReg - SrcReg) < NumRegs;
|
|
}
|
|
|
|
void RISCVInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
|
|
MachineBasicBlock::iterator MBBI,
|
|
const DebugLoc &DL, MCRegister DstReg,
|
|
MCRegister SrcReg, bool KillSrc) const {
|
|
if (RISCV::GPRRegClass.contains(DstReg, SrcReg)) {
|
|
BuildMI(MBB, MBBI, DL, get(RISCV::ADDI), DstReg)
|
|
.addReg(SrcReg, getKillRegState(KillSrc))
|
|
.addImm(0);
|
|
return;
|
|
}
|
|
|
|
// vGPR -> sGPR move
|
|
if (RISCV::GPRRegClass.contains(DstReg) &&
|
|
RISCV::VGPRRegClass.contains(SrcReg)) {
|
|
BuildMI(MBB, MBBI, DL, get(RISCV::VMV_X_S), DstReg).addReg(SrcReg);
|
|
return;
|
|
}
|
|
|
|
// sGPR -> vGPR move
|
|
if (RISCV::GPRRegClass.contains(SrcReg) &&
|
|
RISCV::VGPRRegClass.contains(DstReg)) {
|
|
BuildMI(MBB, MBBI, DL, get(RISCV::VMV_S_X), DstReg).addReg(SrcReg);
|
|
return;
|
|
}
|
|
|
|
// Handle copy from csr
|
|
if (RISCV::VCSRRegClass.contains(SrcReg) &&
|
|
RISCV::GPRRegClass.contains(DstReg)) {
|
|
const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
|
|
BuildMI(MBB, MBBI, DL, get(RISCV::CSRRS), DstReg)
|
|
.addImm(RISCVSysReg::lookupSysRegByName(TRI.getName(SrcReg))->Encoding)
|
|
.addReg(RISCV::X0);
|
|
return;
|
|
}
|
|
|
|
// FPR->FPR copies
|
|
unsigned Opc;
|
|
if (RISCV::FPR16RegClass.contains(DstReg, SrcReg)) {
|
|
Opc = RISCV::FSGNJ_H;
|
|
} else if (RISCV::FPR32RegClass.contains(DstReg, SrcReg)) {
|
|
Opc = RISCV::FSGNJ_S;
|
|
} else if (RISCV::FPR64RegClass.contains(DstReg, SrcReg)) {
|
|
Opc = RISCV::FSGNJ_D;
|
|
} else {
|
|
llvm_unreachable("Impossible reg-to-reg copy");
|
|
}
|
|
|
|
BuildMI(MBB, MBBI, DL, get(Opc), DstReg)
|
|
.addReg(SrcReg, getKillRegState(KillSrc))
|
|
.addReg(SrcReg, getKillRegState(KillSrc));
|
|
}
|
|
|
|
void RISCVInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
|
|
MachineBasicBlock::iterator I,
|
|
Register SrcReg, bool IsKill, int FI,
|
|
const TargetRegisterClass *RC,
|
|
const TargetRegisterInfo *TRI) const {
|
|
DebugLoc DL;
|
|
if (I != MBB.end())
|
|
DL = I->getDebugLoc();
|
|
|
|
MachineFunction *MF = MBB.getParent();
|
|
MachineFrameInfo &MFI = MF->getFrameInfo();
|
|
|
|
unsigned Opcode;
|
|
bool IsScalableVector = true;
|
|
bool IsZvlsseg = true;
|
|
if (RISCV::GPRRegClass.hasSubClassEq(RC)) {
|
|
Opcode = TRI->getRegSizeInBits(RISCV::GPRRegClass) == 32 ?
|
|
RISCV::SW : RISCV::SD;
|
|
IsScalableVector = false;
|
|
} else if (RISCV::FPR16RegClass.hasSubClassEq(RC)) {
|
|
Opcode = RISCV::FSH;
|
|
IsScalableVector = false;
|
|
} else if (RISCV::FPR32RegClass.hasSubClassEq(RC)) {
|
|
Opcode = RISCV::FSW;
|
|
IsScalableVector = false;
|
|
} else if (RISCV::FPR64RegClass.hasSubClassEq(RC)) {
|
|
Opcode = RISCV::FSD;
|
|
IsScalableVector = false;
|
|
} else if (RISCV::VGPRRegClass.hasSubClassEq(RC)) {
|
|
Opcode = RISCV::VSE32_V;
|
|
IsZvlsseg = false;
|
|
} else
|
|
llvm_unreachable("Can't store this register to stack slot");
|
|
|
|
if (IsScalableVector) {
|
|
MachineMemOperand *MMO = MF->getMachineMemOperand(
|
|
MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOStore,
|
|
MemoryLocation::UnknownSize, MFI.getObjectAlign(FI));
|
|
|
|
MFI.setStackID(FI, TargetStackID::ScalableVector);
|
|
auto MIB = BuildMI(MBB, I, DL, get(Opcode))
|
|
.addReg(SrcReg, getKillRegState(IsKill))
|
|
.addFrameIndex(FI)
|
|
.addMemOperand(MMO);
|
|
if (IsZvlsseg) {
|
|
// For spilling/reloading Zvlsseg registers, append the dummy field for
|
|
// the scaled vector length. The argument will be used when expanding
|
|
// these pseudo instructions.
|
|
MIB.addReg(RISCV::X0);
|
|
}
|
|
} else {
|
|
MachineMemOperand *MMO = MF->getMachineMemOperand(
|
|
MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOStore,
|
|
MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
|
|
|
|
BuildMI(MBB, I, DL, get(Opcode))
|
|
.addReg(SrcReg, getKillRegState(IsKill))
|
|
.addFrameIndex(FI)
|
|
.addImm(0)
|
|
.addMemOperand(MMO);
|
|
}
|
|
}
|
|
|
|
void RISCVInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
|
|
MachineBasicBlock::iterator I,
|
|
Register DstReg, int FI,
|
|
const TargetRegisterClass *RC,
|
|
const TargetRegisterInfo *TRI) const {
|
|
DebugLoc DL;
|
|
if (I != MBB.end())
|
|
DL = I->getDebugLoc();
|
|
|
|
MachineFunction *MF = MBB.getParent();
|
|
MachineFrameInfo &MFI = MF->getFrameInfo();
|
|
|
|
unsigned Opcode;
|
|
bool IsScalableVector = true;
|
|
bool IsZvlsseg = true;
|
|
if (RISCV::GPRRegClass.hasSubClassEq(RC)) {
|
|
Opcode = TRI->getRegSizeInBits(RISCV::GPRRegClass) == 32 ?
|
|
RISCV::LW : RISCV::LD;
|
|
IsScalableVector = false;
|
|
} else if (RISCV::FPR16RegClass.hasSubClassEq(RC)) {
|
|
Opcode = RISCV::FLH;
|
|
IsScalableVector = false;
|
|
} else if (RISCV::FPR32RegClass.hasSubClassEq(RC)) {
|
|
Opcode = RISCV::FLW;
|
|
IsScalableVector = false;
|
|
} else if (RISCV::FPR64RegClass.hasSubClassEq(RC)) {
|
|
Opcode = RISCV::FLD;
|
|
IsScalableVector = false;
|
|
} else if (RISCV::VGPRRegClass.hasSubClassEq(RC)) {
|
|
Opcode = RISCV::VLE32_V;
|
|
IsZvlsseg = false;
|
|
} else
|
|
llvm_unreachable("Can't load this register from stack slot");
|
|
|
|
if (IsScalableVector) {
|
|
MachineMemOperand *MMO = MF->getMachineMemOperand(
|
|
MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOLoad,
|
|
MemoryLocation::UnknownSize, MFI.getObjectAlign(FI));
|
|
|
|
MFI.setStackID(FI, TargetStackID::ScalableVector);
|
|
auto MIB = BuildMI(MBB, I, DL, get(Opcode), DstReg)
|
|
.addFrameIndex(FI)
|
|
.addMemOperand(MMO);
|
|
if (IsZvlsseg) {
|
|
// For spilling/reloading Zvlsseg registers, append the dummy field for
|
|
// the scaled vector length. The argument will be used when expanding
|
|
// these pseudo instructions.
|
|
MIB.addReg(RISCV::X0);
|
|
}
|
|
} else {
|
|
MachineMemOperand *MMO = MF->getMachineMemOperand(
|
|
MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOLoad,
|
|
MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
|
|
|
|
BuildMI(MBB, I, DL, get(Opcode), DstReg)
|
|
.addFrameIndex(FI)
|
|
.addImm(0)
|
|
.addMemOperand(MMO);
|
|
}
|
|
}
|
|
|
|
MachineInstr *RISCVInstrInfo::foldMemoryOperandImpl(
|
|
MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
|
|
MachineBasicBlock::iterator InsertPt, int FrameIndex, LiveIntervals *LIS,
|
|
VirtRegMap *VRM) const {
|
|
const MachineFrameInfo &MFI = MF.getFrameInfo();
|
|
|
|
// The below optimizations narrow the load so they are only valid for little
|
|
// endian.
|
|
// TODO: Support big endian by adding an offset into the frame object?
|
|
if (MF.getDataLayout().isBigEndian())
|
|
return nullptr;
|
|
|
|
// Fold load from stack followed by sext.w into lw.
|
|
// TODO: Fold with sext.b, sext.h, zext.b, zext.h, zext.w?
|
|
if (Ops.size() != 1 || Ops[0] != 1)
|
|
return nullptr;
|
|
|
|
unsigned LoadOpc;
|
|
switch (MI.getOpcode()) {
|
|
default:
|
|
if (RISCV::isSEXT_W(MI)) {
|
|
LoadOpc = RISCV::LW;
|
|
break;
|
|
}
|
|
if (RISCV::isZEXT_W(MI)) {
|
|
LoadOpc = RISCV::LWU;
|
|
break;
|
|
}
|
|
if (RISCV::isZEXT_B(MI)) {
|
|
LoadOpc = RISCV::LBU;
|
|
break;
|
|
}
|
|
return nullptr;
|
|
case RISCV::SEXT_H:
|
|
LoadOpc = RISCV::LH;
|
|
break;
|
|
case RISCV::SEXT_B:
|
|
LoadOpc = RISCV::LB;
|
|
break;
|
|
case RISCV::ZEXT_H_RV32:
|
|
case RISCV::ZEXT_H_RV64:
|
|
LoadOpc = RISCV::LHU;
|
|
break;
|
|
}
|
|
|
|
MachineMemOperand *MMO = MF.getMachineMemOperand(
|
|
MachinePointerInfo::getFixedStack(MF, FrameIndex),
|
|
MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIndex),
|
|
MFI.getObjectAlign(FrameIndex));
|
|
|
|
Register DstReg = MI.getOperand(0).getReg();
|
|
return BuildMI(*MI.getParent(), InsertPt, MI.getDebugLoc(), get(LoadOpc),
|
|
DstReg)
|
|
.addFrameIndex(FrameIndex)
|
|
.addImm(0)
|
|
.addMemOperand(MMO);
|
|
}
|
|
|
|
void RISCVInstrInfo::movImm(MachineBasicBlock &MBB,
|
|
MachineBasicBlock::iterator MBBI,
|
|
const DebugLoc &DL, Register DstReg, uint64_t Val,
|
|
MachineInstr::MIFlag Flag) const {
|
|
Register SrcReg = RISCV::X0;
|
|
|
|
if (!STI.is64Bit() && !isInt<32>(Val))
|
|
report_fatal_error("Should only materialize 32-bit constants for RV32");
|
|
|
|
RISCVMatInt::InstSeq Seq =
|
|
RISCVMatInt::generateInstSeq(Val, STI.getFeatureBits());
|
|
assert(!Seq.empty());
|
|
|
|
for (RISCVMatInt::Inst &Inst : Seq) {
|
|
switch (Inst.getOpndKind()) {
|
|
case RISCVMatInt::Imm:
|
|
BuildMI(MBB, MBBI, DL, get(Inst.Opc), DstReg)
|
|
.addImm(Inst.Imm)
|
|
.setMIFlag(Flag);
|
|
break;
|
|
case RISCVMatInt::RegX0:
|
|
BuildMI(MBB, MBBI, DL, get(Inst.Opc), DstReg)
|
|
.addReg(SrcReg, RegState::Kill)
|
|
.addReg(RISCV::X0)
|
|
.setMIFlag(Flag);
|
|
break;
|
|
case RISCVMatInt::RegReg:
|
|
BuildMI(MBB, MBBI, DL, get(Inst.Opc), DstReg)
|
|
.addReg(SrcReg, RegState::Kill)
|
|
.addReg(SrcReg, RegState::Kill)
|
|
.setMIFlag(Flag);
|
|
break;
|
|
case RISCVMatInt::RegImm:
|
|
BuildMI(MBB, MBBI, DL, get(Inst.Opc), DstReg)
|
|
.addReg(SrcReg, RegState::Kill)
|
|
.addImm(Inst.Imm)
|
|
.setMIFlag(Flag);
|
|
break;
|
|
}
|
|
|
|
// Only the first instruction has X0 as its source.
|
|
SrcReg = DstReg;
|
|
}
|
|
}
|
|
|
|
static RISCVCC::CondCode getCondFromBranchOpc(unsigned Opc) {
|
|
switch (Opc) {
|
|
default:
|
|
return RISCVCC::COND_INVALID;
|
|
case RISCV::BEQ:
|
|
return RISCVCC::COND_EQ;
|
|
case RISCV::BNE:
|
|
return RISCVCC::COND_NE;
|
|
case RISCV::BLT:
|
|
return RISCVCC::COND_LT;
|
|
case RISCV::BGE:
|
|
return RISCVCC::COND_GE;
|
|
case RISCV::BLTU:
|
|
return RISCVCC::COND_LTU;
|
|
case RISCV::BGEU:
|
|
return RISCVCC::COND_GEU;
|
|
}
|
|
}
|
|
|
|
// The contents of values added to Cond are not examined outside of
|
|
// RISCVInstrInfo, giving us flexibility in what to push to it. For RISCV, we
|
|
// push BranchOpcode, Reg1, Reg2.
|
|
static void parseCondBranch(MachineInstr &LastInst, MachineBasicBlock *&Target,
|
|
SmallVectorImpl<MachineOperand> &Cond) {
|
|
// Block ends with fall-through condbranch.
|
|
assert(LastInst.getDesc().isConditionalBranch() &&
|
|
"Unknown conditional branch");
|
|
Target = LastInst.getOperand(2).getMBB();
|
|
unsigned CC = getCondFromBranchOpc(LastInst.getOpcode());
|
|
Cond.push_back(MachineOperand::CreateImm(CC));
|
|
Cond.push_back(LastInst.getOperand(0));
|
|
Cond.push_back(LastInst.getOperand(1));
|
|
}
|
|
|
|
const MCInstrDesc &RISCVInstrInfo::getBrCond(RISCVCC::CondCode CC) const {
|
|
switch (CC) {
|
|
default:
|
|
llvm_unreachable("Unknown condition code!");
|
|
case RISCVCC::COND_EQ:
|
|
return get(RISCV::BEQ);
|
|
case RISCVCC::COND_NE:
|
|
return get(RISCV::BNE);
|
|
case RISCVCC::COND_LT:
|
|
return get(RISCV::BLT);
|
|
case RISCVCC::COND_GE:
|
|
return get(RISCV::BGE);
|
|
case RISCVCC::COND_LTU:
|
|
return get(RISCV::BLTU);
|
|
case RISCVCC::COND_GEU:
|
|
return get(RISCV::BGEU);
|
|
}
|
|
}
|
|
|
|
RISCVCC::CondCode RISCVCC::getOppositeBranchCondition(RISCVCC::CondCode CC) {
|
|
switch (CC) {
|
|
default:
|
|
llvm_unreachable("Unrecognized conditional branch");
|
|
case RISCVCC::COND_EQ:
|
|
return RISCVCC::COND_NE;
|
|
case RISCVCC::COND_NE:
|
|
return RISCVCC::COND_EQ;
|
|
case RISCVCC::COND_LT:
|
|
return RISCVCC::COND_GE;
|
|
case RISCVCC::COND_GE:
|
|
return RISCVCC::COND_LT;
|
|
case RISCVCC::COND_LTU:
|
|
return RISCVCC::COND_GEU;
|
|
case RISCVCC::COND_GEU:
|
|
return RISCVCC::COND_LTU;
|
|
}
|
|
}
|
|
|
|
bool RISCVInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
|
|
MachineBasicBlock *&TBB,
|
|
MachineBasicBlock *&FBB,
|
|
SmallVectorImpl<MachineOperand> &Cond,
|
|
bool AllowModify) const {
|
|
TBB = FBB = nullptr;
|
|
Cond.clear();
|
|
|
|
// If the block has no terminators, it just falls into the block after it.
|
|
MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
|
|
if (I == MBB.end() || !isUnpredicatedTerminator(*I))
|
|
return false;
|
|
|
|
// Count the number of terminators and find the first unconditional or
|
|
// indirect branch.
|
|
MachineBasicBlock::iterator FirstUncondOrIndirectBr = MBB.end();
|
|
int NumTerminators = 0;
|
|
for (auto J = I.getReverse(); J != MBB.rend() && isUnpredicatedTerminator(*J);
|
|
J++) {
|
|
NumTerminators++;
|
|
if (J->getDesc().isUnconditionalBranch() ||
|
|
J->getDesc().isIndirectBranch()) {
|
|
FirstUncondOrIndirectBr = J.getReverse();
|
|
}
|
|
}
|
|
|
|
// If AllowModify is true, we can erase any terminators after
|
|
// FirstUncondOrIndirectBR.
|
|
if (AllowModify && FirstUncondOrIndirectBr != MBB.end()) {
|
|
while (std::next(FirstUncondOrIndirectBr) != MBB.end()) {
|
|
std::next(FirstUncondOrIndirectBr)->eraseFromParent();
|
|
NumTerminators--;
|
|
}
|
|
I = FirstUncondOrIndirectBr;
|
|
}
|
|
|
|
// We can't handle blocks that end in an indirect branch.
|
|
if (I->getDesc().isIndirectBranch())
|
|
return true;
|
|
|
|
// We can't handle blocks with more than 2 terminators.
|
|
if (NumTerminators > 2)
|
|
return true;
|
|
|
|
// Handle a single unconditional branch.
|
|
if (NumTerminators == 1 && I->getDesc().isUnconditionalBranch()) {
|
|
TBB = getBranchDestBlock(*I);
|
|
return false;
|
|
}
|
|
|
|
// Handle a single conditional branch.
|
|
if (NumTerminators == 1 && I->getDesc().isConditionalBranch()) {
|
|
parseCondBranch(*I, TBB, Cond);
|
|
return false;
|
|
}
|
|
|
|
// Handle a conditional branch followed by an unconditional branch.
|
|
if (NumTerminators == 2 && std::prev(I)->getDesc().isConditionalBranch() &&
|
|
I->getDesc().isUnconditionalBranch()) {
|
|
parseCondBranch(*std::prev(I), TBB, Cond);
|
|
FBB = getBranchDestBlock(*I);
|
|
return false;
|
|
}
|
|
|
|
// Otherwise, we can't handle this.
|
|
return true;
|
|
}
|
|
|
|
unsigned RISCVInstrInfo::removeBranch(MachineBasicBlock &MBB,
|
|
int *BytesRemoved) const {
|
|
if (BytesRemoved)
|
|
*BytesRemoved = 0;
|
|
MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
|
|
if (I == MBB.end())
|
|
return 0;
|
|
|
|
if (!I->getDesc().isUnconditionalBranch() &&
|
|
!I->getDesc().isConditionalBranch())
|
|
return 0;
|
|
|
|
// Remove the branch.
|
|
if (BytesRemoved)
|
|
*BytesRemoved += getInstSizeInBytes(*I);
|
|
I->eraseFromParent();
|
|
|
|
I = MBB.end();
|
|
|
|
if (I == MBB.begin())
|
|
return 1;
|
|
--I;
|
|
if (!I->getDesc().isConditionalBranch())
|
|
return 1;
|
|
|
|
// Remove the branch.
|
|
if (BytesRemoved)
|
|
*BytesRemoved += getInstSizeInBytes(*I);
|
|
I->eraseFromParent();
|
|
return 2;
|
|
}
|
|
|
|
// Inserts a branch into the end of the specific MachineBasicBlock, returning
|
|
// the number of instructions inserted.
|
|
unsigned RISCVInstrInfo::insertBranch(
|
|
MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
|
|
ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
|
|
if (BytesAdded)
|
|
*BytesAdded = 0;
|
|
|
|
// Shouldn't be a fall through.
|
|
assert(TBB && "insertBranch must not be told to insert a fallthrough");
|
|
assert((Cond.size() == 3 || Cond.size() == 0) &&
|
|
"RISCV branch conditions have two components!");
|
|
|
|
// Unconditional branch.
|
|
if (Cond.empty()) {
|
|
MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(TBB);
|
|
if (BytesAdded)
|
|
*BytesAdded += getInstSizeInBytes(MI);
|
|
return 1;
|
|
}
|
|
|
|
// Either a one or two-way conditional branch.
|
|
auto CC = static_cast<RISCVCC::CondCode>(Cond[0].getImm());
|
|
MachineInstr &CondMI =
|
|
*BuildMI(&MBB, DL, getBrCond(CC)).add(Cond[1]).add(Cond[2]).addMBB(TBB);
|
|
if (BytesAdded)
|
|
*BytesAdded += getInstSizeInBytes(CondMI);
|
|
|
|
// One-way conditional branch.
|
|
if (!FBB)
|
|
return 1;
|
|
|
|
// Two-way conditional branch.
|
|
MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(FBB);
|
|
if (BytesAdded)
|
|
*BytesAdded += getInstSizeInBytes(MI);
|
|
return 2;
|
|
}
|
|
|
|
void RISCVInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB,
|
|
MachineBasicBlock &DestBB,
|
|
MachineBasicBlock &RestoreBB,
|
|
const DebugLoc &DL, int64_t BrOffset,
|
|
RegScavenger *RS) const {
|
|
assert(RS && "RegScavenger required for long branching");
|
|
assert(MBB.empty() &&
|
|
"new block should be inserted for expanding unconditional branch");
|
|
assert(MBB.pred_size() == 1);
|
|
assert(RestoreBB.empty() &&
|
|
"restore block should be inserted for restoring clobbered registers");
|
|
|
|
MachineFunction *MF = MBB.getParent();
|
|
MachineRegisterInfo &MRI = MF->getRegInfo();
|
|
RISCVMachineFunctionInfo *RVFI = MF->getInfo<RISCVMachineFunctionInfo>();
|
|
const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
|
|
|
|
if (!isInt<32>(BrOffset))
|
|
report_fatal_error(
|
|
"Branch offsets outside of the signed 32-bit range not supported");
|
|
|
|
// FIXME: A virtual register must be used initially, as the register
|
|
// scavenger won't work with empty blocks (SIInstrInfo::insertIndirectBranch
|
|
// uses the same workaround).
|
|
Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
|
|
auto II = MBB.end();
|
|
// We may also update the jump target to RestoreBB later.
|
|
MachineInstr &MI = *BuildMI(MBB, II, DL, get(RISCV::PseudoJump))
|
|
.addReg(ScratchReg, RegState::Define | RegState::Dead)
|
|
.addMBB(&DestBB, RISCVII::MO_CALL);
|
|
|
|
RS->enterBasicBlockEnd(MBB);
|
|
Register TmpGPR =
|
|
RS->scavengeRegisterBackwards(RISCV::GPRRegClass, MI.getIterator(),
|
|
/*RestoreAfter=*/false, /*SpAdj=*/0,
|
|
/*AllowSpill=*/false);
|
|
if (TmpGPR != RISCV::NoRegister)
|
|
RS->setRegUsed(TmpGPR);
|
|
else {
|
|
// The case when there is no scavenged register needs special handling.
|
|
|
|
// Pick s11 because it doesn't make a difference.
|
|
TmpGPR = RISCV::X27;
|
|
|
|
int FrameIndex = RVFI->getBranchRelaxationScratchFrameIndex();
|
|
if (FrameIndex == -1)
|
|
report_fatal_error("underestimated function size");
|
|
|
|
storeRegToStackSlot(MBB, MI, TmpGPR, /*IsKill=*/true, FrameIndex,
|
|
&RISCV::GPRRegClass, TRI);
|
|
TRI->eliminateFrameIndex(std::prev(MI.getIterator()),
|
|
/*SpAdj=*/0, /*FIOperandNum=*/1);
|
|
|
|
MI.getOperand(1).setMBB(&RestoreBB);
|
|
|
|
loadRegFromStackSlot(RestoreBB, RestoreBB.end(), TmpGPR, FrameIndex,
|
|
&RISCV::GPRRegClass, TRI);
|
|
TRI->eliminateFrameIndex(RestoreBB.back(),
|
|
/*SpAdj=*/0, /*FIOperandNum=*/1);
|
|
}
|
|
|
|
MRI.replaceRegWith(ScratchReg, TmpGPR);
|
|
MRI.clearVirtRegs();
|
|
}
|
|
|
|
bool RISCVInstrInfo::reverseBranchCondition(
|
|
SmallVectorImpl<MachineOperand> &Cond) const {
|
|
assert((Cond.size() == 3) && "Invalid branch condition!");
|
|
auto CC = static_cast<RISCVCC::CondCode>(Cond[0].getImm());
|
|
Cond[0].setImm(getOppositeBranchCondition(CC));
|
|
return false;
|
|
}
|
|
|
|
MachineBasicBlock *
|
|
RISCVInstrInfo::getBranchDestBlock(const MachineInstr &MI) const {
|
|
assert(MI.getDesc().isBranch() && "Unexpected opcode!");
|
|
// The branch target is always the last operand.
|
|
int NumOp = MI.getNumExplicitOperands();
|
|
return MI.getOperand(NumOp - 1).getMBB();
|
|
}
|
|
|
|
bool RISCVInstrInfo::isBranchOffsetInRange(unsigned BranchOp,
|
|
int64_t BrOffset) const {
|
|
unsigned XLen = STI.getXLen();
|
|
// Ideally we could determine the supported branch offset from the
|
|
// RISCVII::FormMask, but this can't be used for Pseudo instructions like
|
|
// PseudoBR.
|
|
switch (BranchOp) {
|
|
default:
|
|
llvm_unreachable("Unexpected opcode!");
|
|
case RISCV::BEQ:
|
|
case RISCV::BNE:
|
|
case RISCV::BLT:
|
|
case RISCV::BGE:
|
|
case RISCV::BLTU:
|
|
case RISCV::BGEU:
|
|
return isIntN(13, BrOffset);
|
|
case RISCV::JAL:
|
|
case RISCV::PseudoBR:
|
|
return isIntN(21, BrOffset);
|
|
case RISCV::PseudoJump:
|
|
return isIntN(32, SignExtend64(BrOffset + 0x800, XLen));
|
|
}
|
|
}
|
|
|
|
unsigned RISCVInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
|
|
if (MI.isMetaInstruction())
|
|
return 0;
|
|
|
|
unsigned Opcode = MI.getOpcode();
|
|
|
|
if (Opcode == TargetOpcode::INLINEASM ||
|
|
Opcode == TargetOpcode::INLINEASM_BR) {
|
|
const MachineFunction &MF = *MI.getParent()->getParent();
|
|
const auto &TM = static_cast<const RISCVTargetMachine &>(MF.getTarget());
|
|
return getInlineAsmLength(MI.getOperand(0).getSymbolName(),
|
|
*TM.getMCAsmInfo());
|
|
}
|
|
|
|
if (MI.getParent() && MI.getParent()->getParent()) {
|
|
const auto MF = MI.getMF();
|
|
const auto &TM = static_cast<const RISCVTargetMachine &>(MF->getTarget());
|
|
const MCRegisterInfo &MRI = *TM.getMCRegisterInfo();
|
|
const MCSubtargetInfo &STI = *TM.getMCSubtargetInfo();
|
|
const RISCVSubtarget &ST = MF->getSubtarget<RISCVSubtarget>();
|
|
if (isCompressibleInst(MI, &ST, MRI, STI))
|
|
return 2;
|
|
}
|
|
return get(Opcode).getSize();
|
|
}
|
|
|
|
bool RISCVInstrInfo::isAsCheapAsAMove(const MachineInstr &MI) const {
|
|
const unsigned Opcode = MI.getOpcode();
|
|
switch (Opcode) {
|
|
default:
|
|
break;
|
|
case RISCV::FSGNJ_D:
|
|
case RISCV::FSGNJ_S:
|
|
case RISCV::FSGNJ_H:
|
|
// The canonical floating-point move is fsgnj rd, rs, rs.
|
|
return MI.getOperand(1).isReg() && MI.getOperand(2).isReg() &&
|
|
MI.getOperand(1).getReg() == MI.getOperand(2).getReg();
|
|
case RISCV::ADDI:
|
|
case RISCV::ORI:
|
|
case RISCV::XORI:
|
|
return (MI.getOperand(1).isReg() &&
|
|
MI.getOperand(1).getReg() == RISCV::X0) ||
|
|
(MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0);
|
|
}
|
|
return MI.isAsCheapAsAMove();
|
|
}
|
|
|
|
std::optional<DestSourcePair>
|
|
RISCVInstrInfo::isCopyInstrImpl(const MachineInstr &MI) const {
|
|
if (MI.isMoveReg())
|
|
return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
|
|
switch (MI.getOpcode()) {
|
|
default:
|
|
break;
|
|
case RISCV::ADDI:
|
|
// Operand 1 can be a frameindex but callers expect registers
|
|
if (MI.getOperand(1).isReg() && MI.getOperand(2).isImm() &&
|
|
MI.getOperand(2).getImm() == 0)
|
|
return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
|
|
break;
|
|
case RISCV::FSGNJ_D:
|
|
case RISCV::FSGNJ_S:
|
|
case RISCV::FSGNJ_H:
|
|
// The canonical floating-point move is fsgnj rd, rs, rs.
|
|
if (MI.getOperand(1).isReg() && MI.getOperand(2).isReg() &&
|
|
MI.getOperand(1).getReg() == MI.getOperand(2).getReg())
|
|
return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
|
|
break;
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
void RISCVInstrInfo::setSpecialOperandAttr(MachineInstr &OldMI1,
|
|
MachineInstr &OldMI2,
|
|
MachineInstr &NewMI1,
|
|
MachineInstr &NewMI2) const {
|
|
uint16_t IntersectedFlags = OldMI1.getFlags() & OldMI2.getFlags();
|
|
NewMI1.setFlags(IntersectedFlags);
|
|
NewMI2.setFlags(IntersectedFlags);
|
|
}
|
|
|
|
void RISCVInstrInfo::finalizeInsInstrs(
|
|
MachineInstr &Root, MachineCombinerPattern &P,
|
|
SmallVectorImpl<MachineInstr *> &InsInstrs) const {
|
|
int16_t FrmOpIdx =
|
|
RISCV::getNamedOperandIdx(Root.getOpcode(), RISCV::OpName::frm);
|
|
if (FrmOpIdx < 0) {
|
|
assert(all_of(InsInstrs,
|
|
[](MachineInstr *MI) {
|
|
return RISCV::getNamedOperandIdx(MI->getOpcode(),
|
|
RISCV::OpName::frm) < 0;
|
|
}) &&
|
|
"New instructions require FRM whereas the old one does not have it");
|
|
return;
|
|
}
|
|
|
|
const MachineOperand &FRM = Root.getOperand(FrmOpIdx);
|
|
MachineFunction &MF = *Root.getMF();
|
|
|
|
for (auto *NewMI : InsInstrs) {
|
|
assert(static_cast<unsigned>(RISCV::getNamedOperandIdx(
|
|
NewMI->getOpcode(), RISCV::OpName::frm)) ==
|
|
NewMI->getNumOperands() &&
|
|
"Instruction has unexpected number of operands");
|
|
MachineInstrBuilder MIB(MF, NewMI);
|
|
MIB.add(FRM);
|
|
if (FRM.getImm() == RISCVFPRndMode::DYN)
|
|
MIB.addUse(RISCV::FRM, RegState::Implicit);
|
|
}
|
|
}
|
|
|
|
static bool isFADD(unsigned Opc) {
|
|
switch (Opc) {
|
|
default:
|
|
return false;
|
|
case RISCV::FADD_H:
|
|
case RISCV::FADD_S:
|
|
case RISCV::FADD_D:
|
|
return true;
|
|
}
|
|
}
|
|
|
|
static bool isFSUB(unsigned Opc) {
|
|
switch (Opc) {
|
|
default:
|
|
return false;
|
|
case RISCV::FSUB_H:
|
|
case RISCV::FSUB_S:
|
|
case RISCV::FSUB_D:
|
|
return true;
|
|
}
|
|
}
|
|
|
|
static bool isFMUL(unsigned Opc) {
|
|
switch (Opc) {
|
|
default:
|
|
return false;
|
|
case RISCV::FMUL_H:
|
|
case RISCV::FMUL_S:
|
|
case RISCV::FMUL_D:
|
|
return true;
|
|
}
|
|
}
|
|
|
|
bool RISCVInstrInfo::hasReassociableSibling(const MachineInstr &Inst,
|
|
bool &Commuted) const {
|
|
if (!TargetInstrInfo::hasReassociableSibling(Inst, Commuted))
|
|
return false;
|
|
|
|
const MachineRegisterInfo &MRI = Inst.getMF()->getRegInfo();
|
|
unsigned OperandIdx = Commuted ? 2 : 1;
|
|
const MachineInstr &Sibling =
|
|
*MRI.getVRegDef(Inst.getOperand(OperandIdx).getReg());
|
|
|
|
return RISCV::hasEqualFRM(Inst, Sibling);
|
|
}
|
|
|
|
bool RISCVInstrInfo::isAssociativeAndCommutative(
|
|
const MachineInstr &Inst) const {
|
|
unsigned Opc = Inst.getOpcode();
|
|
if (isFADD(Opc) || isFMUL(Opc))
|
|
return Inst.getFlag(MachineInstr::MIFlag::FmReassoc) &&
|
|
Inst.getFlag(MachineInstr::MIFlag::FmNsz);
|
|
return false;
|
|
}
|
|
|
|
static bool canCombineFPFusedMultiply(const MachineInstr &Root,
|
|
const MachineOperand &MO,
|
|
bool DoRegPressureReduce) {
|
|
if (!MO.isReg() || !Register::isVirtualRegister(MO.getReg()))
|
|
return false;
|
|
const MachineRegisterInfo &MRI = Root.getMF()->getRegInfo();
|
|
MachineInstr *MI = MRI.getVRegDef(MO.getReg());
|
|
if (!MI || !isFMUL(MI->getOpcode()))
|
|
return false;
|
|
|
|
if (!Root.getFlag(MachineInstr::MIFlag::FmContract) ||
|
|
!MI->getFlag(MachineInstr::MIFlag::FmContract))
|
|
return false;
|
|
|
|
// Try combining even if fmul has more than one use as it eliminates
|
|
// dependency between fadd(fsub) and fmul. However, it can extend liveranges
|
|
// for fmul operands, so reject the transformation in register pressure
|
|
// reduction mode.
|
|
if (DoRegPressureReduce && !MRI.hasOneNonDBGUse(MI->getOperand(0).getReg()))
|
|
return false;
|
|
|
|
// Do not combine instructions from different basic blocks.
|
|
if (Root.getParent() != MI->getParent())
|
|
return false;
|
|
return RISCV::hasEqualFRM(Root, *MI);
|
|
}
|
|
|
|
static bool
|
|
getFPFusedMultiplyPatterns(MachineInstr &Root,
|
|
SmallVectorImpl<MachineCombinerPattern> &Patterns,
|
|
bool DoRegPressureReduce) {
|
|
unsigned Opc = Root.getOpcode();
|
|
bool IsFAdd = isFADD(Opc);
|
|
if (!IsFAdd && !isFSUB(Opc))
|
|
return false;
|
|
bool Added = false;
|
|
if (canCombineFPFusedMultiply(Root, Root.getOperand(1),
|
|
DoRegPressureReduce)) {
|
|
Patterns.push_back(IsFAdd ? MachineCombinerPattern::FMADD_AX
|
|
: MachineCombinerPattern::FMSUB);
|
|
Added = true;
|
|
}
|
|
if (canCombineFPFusedMultiply(Root, Root.getOperand(2),
|
|
DoRegPressureReduce)) {
|
|
Patterns.push_back(IsFAdd ? MachineCombinerPattern::FMADD_XA
|
|
: MachineCombinerPattern::FNMSUB);
|
|
Added = true;
|
|
}
|
|
return Added;
|
|
}
|
|
|
|
static bool getFPPatterns(MachineInstr &Root,
|
|
SmallVectorImpl<MachineCombinerPattern> &Patterns,
|
|
bool DoRegPressureReduce) {
|
|
return getFPFusedMultiplyPatterns(Root, Patterns, DoRegPressureReduce);
|
|
}
|
|
|
|
bool RISCVInstrInfo::getMachineCombinerPatterns(
|
|
MachineInstr &Root, SmallVectorImpl<MachineCombinerPattern> &Patterns,
|
|
bool DoRegPressureReduce) const {
|
|
|
|
if (getFPPatterns(Root, Patterns, DoRegPressureReduce))
|
|
return true;
|
|
|
|
return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns,
|
|
DoRegPressureReduce);
|
|
}
|
|
|
|
static unsigned getFPFusedMultiplyOpcode(unsigned RootOpc,
|
|
MachineCombinerPattern Pattern) {
|
|
switch (RootOpc) {
|
|
default:
|
|
llvm_unreachable("Unexpected opcode");
|
|
case RISCV::FADD_H:
|
|
return RISCV::FMADD_H;
|
|
case RISCV::FADD_S:
|
|
return RISCV::FMADD_S;
|
|
case RISCV::FADD_D:
|
|
return RISCV::FMADD_D;
|
|
case RISCV::FSUB_H:
|
|
return Pattern == MachineCombinerPattern::FMSUB ? RISCV::FMSUB_H
|
|
: RISCV::FNMSUB_H;
|
|
case RISCV::FSUB_S:
|
|
return Pattern == MachineCombinerPattern::FMSUB ? RISCV::FMSUB_S
|
|
: RISCV::FNMSUB_S;
|
|
case RISCV::FSUB_D:
|
|
return Pattern == MachineCombinerPattern::FMSUB ? RISCV::FMSUB_D
|
|
: RISCV::FNMSUB_D;
|
|
}
|
|
}
|
|
|
|
static unsigned getAddendOperandIdx(MachineCombinerPattern Pattern) {
|
|
switch (Pattern) {
|
|
default:
|
|
llvm_unreachable("Unexpected pattern");
|
|
case MachineCombinerPattern::FMADD_AX:
|
|
case MachineCombinerPattern::FMSUB:
|
|
return 2;
|
|
case MachineCombinerPattern::FMADD_XA:
|
|
case MachineCombinerPattern::FNMSUB:
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
static void combineFPFusedMultiply(MachineInstr &Root, MachineInstr &Prev,
|
|
MachineCombinerPattern Pattern,
|
|
SmallVectorImpl<MachineInstr *> &InsInstrs,
|
|
SmallVectorImpl<MachineInstr *> &DelInstrs) {
|
|
MachineFunction *MF = Root.getMF();
|
|
MachineRegisterInfo &MRI = MF->getRegInfo();
|
|
const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
|
|
|
|
MachineOperand &Mul1 = Prev.getOperand(1);
|
|
MachineOperand &Mul2 = Prev.getOperand(2);
|
|
MachineOperand &Dst = Root.getOperand(0);
|
|
MachineOperand &Addend = Root.getOperand(getAddendOperandIdx(Pattern));
|
|
|
|
Register DstReg = Dst.getReg();
|
|
unsigned FusedOpc = getFPFusedMultiplyOpcode(Root.getOpcode(), Pattern);
|
|
auto IntersectedFlags = Root.getFlags() & Prev.getFlags();
|
|
DebugLoc MergedLoc =
|
|
DILocation::getMergedLocation(Root.getDebugLoc(), Prev.getDebugLoc());
|
|
|
|
MachineInstrBuilder MIB =
|
|
BuildMI(*MF, MergedLoc, TII->get(FusedOpc), DstReg)
|
|
.addReg(Mul1.getReg(), getKillRegState(Mul1.isKill()))
|
|
.addReg(Mul2.getReg(), getKillRegState(Mul2.isKill()))
|
|
.addReg(Addend.getReg(), getKillRegState(Addend.isKill()))
|
|
.setMIFlags(IntersectedFlags);
|
|
|
|
// Mul operands are not killed anymore.
|
|
Mul1.setIsKill(false);
|
|
Mul2.setIsKill(false);
|
|
|
|
InsInstrs.push_back(MIB);
|
|
if (MRI.hasOneNonDBGUse(Prev.getOperand(0).getReg()))
|
|
DelInstrs.push_back(&Prev);
|
|
DelInstrs.push_back(&Root);
|
|
}
|
|
|
|
void RISCVInstrInfo::genAlternativeCodeSequence(
|
|
MachineInstr &Root, MachineCombinerPattern Pattern,
|
|
SmallVectorImpl<MachineInstr *> &InsInstrs,
|
|
SmallVectorImpl<MachineInstr *> &DelInstrs,
|
|
DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const {
|
|
MachineRegisterInfo &MRI = Root.getMF()->getRegInfo();
|
|
switch (Pattern) {
|
|
default:
|
|
TargetInstrInfo::genAlternativeCodeSequence(Root, Pattern, InsInstrs,
|
|
DelInstrs, InstrIdxForVirtReg);
|
|
return;
|
|
case MachineCombinerPattern::FMADD_AX:
|
|
case MachineCombinerPattern::FMSUB: {
|
|
MachineInstr &Prev = *MRI.getVRegDef(Root.getOperand(1).getReg());
|
|
combineFPFusedMultiply(Root, Prev, Pattern, InsInstrs, DelInstrs);
|
|
return;
|
|
}
|
|
case MachineCombinerPattern::FMADD_XA:
|
|
case MachineCombinerPattern::FNMSUB: {
|
|
MachineInstr &Prev = *MRI.getVRegDef(Root.getOperand(2).getReg());
|
|
combineFPFusedMultiply(Root, Prev, Pattern, InsInstrs, DelInstrs);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool RISCVInstrInfo::verifyInstruction(const MachineInstr &MI,
|
|
StringRef &ErrInfo) const {
|
|
MCInstrDesc const &Desc = MI.getDesc();
|
|
|
|
for (auto &OI : enumerate(Desc.operands())) {
|
|
unsigned OpType = OI.value().OperandType;
|
|
if (OpType >= RISCVOp::OPERAND_FIRST_RISCV_IMM &&
|
|
OpType <= RISCVOp::OPERAND_LAST_RISCV_IMM) {
|
|
const MachineOperand &MO = MI.getOperand(OI.index());
|
|
if (MO.isImm()) {
|
|
int64_t Imm = MO.getImm();
|
|
bool Ok;
|
|
switch (OpType) {
|
|
default:
|
|
llvm_unreachable("Unexpected operand type");
|
|
|
|
// clang-format off
|
|
#define CASE_OPERAND_UIMM(NUM) \
|
|
case RISCVOp::OPERAND_UIMM##NUM: \
|
|
Ok = isUInt<NUM>(Imm); \
|
|
break;
|
|
CASE_OPERAND_UIMM(2)
|
|
CASE_OPERAND_UIMM(3)
|
|
CASE_OPERAND_UIMM(4)
|
|
CASE_OPERAND_UIMM(5)
|
|
CASE_OPERAND_UIMM(7)
|
|
case RISCVOp::OPERAND_UIMM7_LSB00:
|
|
Ok = isShiftedUInt<5, 2>(Imm);
|
|
break;
|
|
case RISCVOp::OPERAND_UIMM8_LSB00:
|
|
Ok = isShiftedUInt<6, 2>(Imm);
|
|
break;
|
|
case RISCVOp::OPERAND_UIMM8_LSB000:
|
|
Ok = isShiftedUInt<5, 3>(Imm);
|
|
break;
|
|
CASE_OPERAND_UIMM(12)
|
|
CASE_OPERAND_UIMM(20)
|
|
// clang-format on
|
|
case RISCVOp::OPERAND_SIMM10_LSB0000_NONZERO:
|
|
Ok = isShiftedInt<6, 4>(Imm) && (Imm != 0);
|
|
break;
|
|
case RISCVOp::OPERAND_ZERO:
|
|
Ok = Imm == 0;
|
|
break;
|
|
case RISCVOp::OPERAND_SIMM5:
|
|
Ok = isInt<5>(Imm);
|
|
break;
|
|
case RISCVOp::OPERAND_SIMM5_PLUS1:
|
|
Ok = (isInt<5>(Imm) && Imm != -16) || Imm == 16;
|
|
break;
|
|
case RISCVOp::OPERAND_SIMM6:
|
|
Ok = isInt<6>(Imm);
|
|
break;
|
|
case RISCVOp::OPERAND_SIMM6_NONZERO:
|
|
Ok = Imm != 0 && isInt<6>(Imm);
|
|
break;
|
|
case RISCVOp::OPERAND_VTYPEI10:
|
|
Ok = isUInt<10>(Imm);
|
|
break;
|
|
case RISCVOp::OPERAND_VTYPEI11:
|
|
Ok = isUInt<11>(Imm);
|
|
break;
|
|
case RISCVOp::OPERAND_SIMM12:
|
|
Ok = isInt<12>(Imm);
|
|
break;
|
|
case RISCVOp::OPERAND_SIMM12_LSB00000:
|
|
Ok = isShiftedInt<7, 5>(Imm);
|
|
break;
|
|
case RISCVOp::OPERAND_UIMMLOG2XLEN:
|
|
Ok = STI.is64Bit() ? isUInt<6>(Imm) : isUInt<5>(Imm);
|
|
break;
|
|
case RISCVOp::OPERAND_UIMMLOG2XLEN_NONZERO:
|
|
Ok = STI.is64Bit() ? isUInt<6>(Imm) : isUInt<5>(Imm);
|
|
Ok = Ok && Imm != 0;
|
|
break;
|
|
case RISCVOp::OPERAND_UIMM_SHFL:
|
|
Ok = STI.is64Bit() ? isUInt<5>(Imm) : isUInt<4>(Imm);
|
|
break;
|
|
case RISCVOp::OPERAND_RVKRNUM:
|
|
Ok = Imm >= 0 && Imm <= 10;
|
|
break;
|
|
}
|
|
if (!Ok) {
|
|
ErrInfo = "Invalid immediate";
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
const uint64_t TSFlags = Desc.TSFlags;
|
|
if (RISCVII::hasSEWOp(TSFlags)) {
|
|
unsigned OpIdx = RISCVII::getSEWOpNum(Desc);
|
|
uint64_t Log2SEW = MI.getOperand(OpIdx).getImm();
|
|
if (Log2SEW > 31) {
|
|
ErrInfo = "Unexpected SEW value";
|
|
return false;
|
|
}
|
|
unsigned SEW = Log2SEW ? 1 << Log2SEW : 8;
|
|
if (!RISCVVType::isValidSEW(SEW)) {
|
|
ErrInfo = "Unexpected SEW value";
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// Return true if get the base operand, byte offset of an instruction and the
|
|
// memory width. Width is the size of memory that is being loaded/stored.
|
|
bool RISCVInstrInfo::getMemOperandWithOffsetWidth(
|
|
const MachineInstr &LdSt, const MachineOperand *&BaseReg, int64_t &Offset,
|
|
unsigned &Width, const TargetRegisterInfo *TRI) const {
|
|
if (!LdSt.mayLoadOrStore())
|
|
return false;
|
|
|
|
// Here we assume the standard RISC-V ISA, which uses a base+offset
|
|
// addressing mode. You'll need to relax these conditions to support custom
|
|
// load/stores instructions.
|
|
if (LdSt.getNumExplicitOperands() != 3)
|
|
return false;
|
|
if (!LdSt.getOperand(1).isReg() || !LdSt.getOperand(2).isImm())
|
|
return false;
|
|
|
|
if (!LdSt.hasOneMemOperand())
|
|
return false;
|
|
|
|
Width = (*LdSt.memoperands_begin())->getSize();
|
|
BaseReg = &LdSt.getOperand(1);
|
|
Offset = LdSt.getOperand(2).getImm();
|
|
return true;
|
|
}
|
|
|
|
bool RISCVInstrInfo::areMemAccessesTriviallyDisjoint(
|
|
const MachineInstr &MIa, const MachineInstr &MIb) const {
|
|
assert(MIa.mayLoadOrStore() && "MIa must be a load or store.");
|
|
assert(MIb.mayLoadOrStore() && "MIb must be a load or store.");
|
|
|
|
if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
|
|
MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
|
|
return false;
|
|
|
|
// Retrieve the base register, offset from the base register and width. Width
|
|
// is the size of memory that is being loaded/stored (e.g. 1, 2, 4). If
|
|
// base registers are identical, and the offset of a lower memory access +
|
|
// the width doesn't overlap the offset of a higher memory access,
|
|
// then the memory accesses are different.
|
|
const TargetRegisterInfo *TRI = STI.getRegisterInfo();
|
|
const MachineOperand *BaseOpA = nullptr, *BaseOpB = nullptr;
|
|
int64_t OffsetA = 0, OffsetB = 0;
|
|
unsigned int WidthA = 0, WidthB = 0;
|
|
if (getMemOperandWithOffsetWidth(MIa, BaseOpA, OffsetA, WidthA, TRI) &&
|
|
getMemOperandWithOffsetWidth(MIb, BaseOpB, OffsetB, WidthB, TRI)) {
|
|
if (BaseOpA->isIdenticalTo(*BaseOpB)) {
|
|
int LowOffset = std::min(OffsetA, OffsetB);
|
|
int HighOffset = std::max(OffsetA, OffsetB);
|
|
int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
|
|
if (LowOffset + LowWidth <= HighOffset)
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
std::pair<unsigned, unsigned>
|
|
RISCVInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
|
|
const unsigned Mask = RISCVII::MO_DIRECT_FLAG_MASK;
|
|
return std::make_pair(TF & Mask, TF & ~Mask);
|
|
}
|
|
|
|
ArrayRef<std::pair<unsigned, const char *>>
|
|
RISCVInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
|
|
using namespace RISCVII;
|
|
static const std::pair<unsigned, const char *> TargetFlags[] = {
|
|
{MO_CALL, "riscv-call"},
|
|
{MO_PLT, "riscv-plt"},
|
|
{MO_LO, "riscv-lo"},
|
|
{MO_HI, "riscv-hi"},
|
|
{MO_PCREL_LO, "riscv-pcrel-lo"},
|
|
{MO_PCREL_HI, "riscv-pcrel-hi"},
|
|
{MO_GOT_HI, "riscv-got-hi"},
|
|
{MO_TPREL_LO, "riscv-tprel-lo"},
|
|
{MO_TPREL_HI, "riscv-tprel-hi"},
|
|
{MO_TPREL_ADD, "riscv-tprel-add"},
|
|
{MO_TLS_GOT_HI, "riscv-tls-got-hi"},
|
|
{MO_TLS_GD_HI, "riscv-tls-gd-hi"}};
|
|
return makeArrayRef(TargetFlags);
|
|
}
|
|
bool RISCVInstrInfo::isFunctionSafeToOutlineFrom(
|
|
MachineFunction &MF, bool OutlineFromLinkOnceODRs) const {
|
|
const Function &F = MF.getFunction();
|
|
|
|
// Can F be deduplicated by the linker? If it can, don't outline from it.
|
|
if (!OutlineFromLinkOnceODRs && F.hasLinkOnceODRLinkage())
|
|
return false;
|
|
|
|
// Don't outline from functions with section markings; the program could
|
|
// expect that all the code is in the named section.
|
|
if (F.hasSection())
|
|
return false;
|
|
|
|
// It's safe to outline from MF.
|
|
return true;
|
|
}
|
|
|
|
bool RISCVInstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
|
|
unsigned &Flags) const {
|
|
// More accurate safety checking is done in getOutliningCandidateInfo.
|
|
return TargetInstrInfo::isMBBSafeToOutlineFrom(MBB, Flags);
|
|
}
|
|
|
|
// Enum values indicating how an outlined call should be constructed.
|
|
enum MachineOutlinerConstructionID {
|
|
MachineOutlinerDefault
|
|
};
|
|
|
|
bool RISCVInstrInfo::shouldOutlineFromFunctionByDefault(
|
|
MachineFunction &MF) const {
|
|
return MF.getFunction().hasMinSize();
|
|
}
|
|
|
|
outliner::OutlinedFunction RISCVInstrInfo::getOutliningCandidateInfo(
|
|
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
|
|
|
|
// First we need to filter out candidates where the X5 register (IE t0) can't
|
|
// be used to setup the function call.
|
|
auto CannotInsertCall = [](outliner::Candidate &C) {
|
|
const TargetRegisterInfo *TRI = C.getMF()->getSubtarget().getRegisterInfo();
|
|
return !C.isAvailableAcrossAndOutOfSeq(RISCV::X5, *TRI);
|
|
};
|
|
|
|
llvm::erase_if(RepeatedSequenceLocs, CannotInsertCall);
|
|
|
|
// If the sequence doesn't have enough candidates left, then we're done.
|
|
if (RepeatedSequenceLocs.size() < 2)
|
|
return outliner::OutlinedFunction();
|
|
|
|
unsigned SequenceSize = 0;
|
|
|
|
auto I = RepeatedSequenceLocs[0].front();
|
|
auto E = std::next(RepeatedSequenceLocs[0].back());
|
|
for (; I != E; ++I)
|
|
SequenceSize += getInstSizeInBytes(*I);
|
|
|
|
// call t0, function = 8 bytes.
|
|
unsigned CallOverhead = 8;
|
|
for (auto &C : RepeatedSequenceLocs)
|
|
C.setCallInfo(MachineOutlinerDefault, CallOverhead);
|
|
|
|
// jr t0 = 4 bytes, 2 bytes if compressed instructions are enabled.
|
|
unsigned FrameOverhead = 4;
|
|
if (RepeatedSequenceLocs[0]
|
|
.getMF()
|
|
->getSubtarget<RISCVSubtarget>()
|
|
.hasStdExtCOrZca())
|
|
FrameOverhead = 2;
|
|
|
|
return outliner::OutlinedFunction(RepeatedSequenceLocs, SequenceSize,
|
|
FrameOverhead, MachineOutlinerDefault);
|
|
}
|
|
|
|
outliner::InstrType
|
|
RISCVInstrInfo::getOutliningType(MachineBasicBlock::iterator &MBBI,
|
|
unsigned Flags) const {
|
|
MachineInstr &MI = *MBBI;
|
|
MachineBasicBlock *MBB = MI.getParent();
|
|
const TargetRegisterInfo *TRI =
|
|
MBB->getParent()->getSubtarget().getRegisterInfo();
|
|
const auto &F = MI.getMF()->getFunction();
|
|
|
|
// Positions generally can't safely be outlined.
|
|
if (MI.isPosition()) {
|
|
// We can manually strip out CFI instructions later.
|
|
if (MI.isCFIInstruction())
|
|
// If current function has exception handling code, we can't outline &
|
|
// strip these CFI instructions since it may break .eh_frame section
|
|
// needed in unwinding.
|
|
return F.needsUnwindTableEntry() ? outliner::InstrType::Illegal
|
|
: outliner::InstrType::Invisible;
|
|
|
|
return outliner::InstrType::Illegal;
|
|
}
|
|
|
|
// Don't trust the user to write safe inline assembly.
|
|
if (MI.isInlineAsm())
|
|
return outliner::InstrType::Illegal;
|
|
|
|
// We can't outline branches to other basic blocks.
|
|
if (MI.isTerminator() && !MBB->succ_empty())
|
|
return outliner::InstrType::Illegal;
|
|
|
|
// We need support for tail calls to outlined functions before return
|
|
// statements can be allowed.
|
|
if (MI.isReturn())
|
|
return outliner::InstrType::Illegal;
|
|
|
|
// Don't allow modifying the X5 register which we use for return addresses for
|
|
// these outlined functions.
|
|
if (MI.modifiesRegister(RISCV::X5, TRI) ||
|
|
MI.getDesc().hasImplicitDefOfPhysReg(RISCV::X5))
|
|
return outliner::InstrType::Illegal;
|
|
|
|
// Make sure the operands don't reference something unsafe.
|
|
for (const auto &MO : MI.operands()) {
|
|
if (MO.isMBB() || MO.isBlockAddress() || MO.isCPI() || MO.isJTI())
|
|
return outliner::InstrType::Illegal;
|
|
|
|
// pcrel-hi and pcrel-lo can't put in separate sections, filter that out
|
|
// if any possible.
|
|
if (MO.getTargetFlags() == RISCVII::MO_PCREL_LO &&
|
|
(MI.getMF()->getTarget().getFunctionSections() || F.hasComdat() ||
|
|
F.hasSection()))
|
|
return outliner::InstrType::Illegal;
|
|
}
|
|
|
|
// Don't allow instructions which won't be materialized to impact outlining
|
|
// analysis.
|
|
if (MI.isMetaInstruction())
|
|
return outliner::InstrType::Invisible;
|
|
|
|
return outliner::InstrType::Legal;
|
|
}
|
|
|
|
void RISCVInstrInfo::buildOutlinedFrame(
|
|
MachineBasicBlock &MBB, MachineFunction &MF,
|
|
const outliner::OutlinedFunction &OF) const {
|
|
|
|
// Strip out any CFI instructions
|
|
bool Changed = true;
|
|
while (Changed) {
|
|
Changed = false;
|
|
auto I = MBB.begin();
|
|
auto E = MBB.end();
|
|
for (; I != E; ++I) {
|
|
if (I->isCFIInstruction()) {
|
|
I->removeFromParent();
|
|
Changed = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
MBB.addLiveIn(RISCV::X5);
|
|
|
|
// Add in a return instruction to the end of the outlined frame.
|
|
MBB.insert(MBB.end(), BuildMI(MF, DebugLoc(), get(RISCV::JALR))
|
|
.addReg(RISCV::X0, RegState::Define)
|
|
.addReg(RISCV::X5)
|
|
.addImm(0));
|
|
}
|
|
|
|
MachineBasicBlock::iterator RISCVInstrInfo::insertOutlinedCall(
|
|
Module &M, MachineBasicBlock &MBB, MachineBasicBlock::iterator &It,
|
|
MachineFunction &MF, outliner::Candidate &C) const {
|
|
|
|
// Add in a call instruction to the outlined function at the given location.
|
|
It = MBB.insert(It,
|
|
BuildMI(MF, DebugLoc(), get(RISCV::PseudoCALLReg), RISCV::X5)
|
|
.addGlobalAddress(M.getNamedValue(MF.getName()), 0,
|
|
RISCVII::MO_CALL));
|
|
return It;
|
|
}
|
|
|
|
// MIR printer helper function to annotate Operands with a comment.
|
|
std::string RISCVInstrInfo::createMIROperandComment(
|
|
const MachineInstr &MI, const MachineOperand &Op, unsigned OpIdx,
|
|
const TargetRegisterInfo *TRI) const {
|
|
// Print a generic comment for this operand if there is one.
|
|
std::string GenericComment =
|
|
TargetInstrInfo::createMIROperandComment(MI, Op, OpIdx, TRI);
|
|
if (!GenericComment.empty())
|
|
return GenericComment;
|
|
|
|
// If not, we must have an immediate operand.
|
|
if (!Op.isImm())
|
|
return std::string();
|
|
|
|
std::string Comment;
|
|
raw_string_ostream OS(Comment);
|
|
|
|
uint64_t TSFlags = MI.getDesc().TSFlags;
|
|
|
|
OS.flush();
|
|
return Comment;
|
|
}
|
|
|
|
// Returns true if this is the sext.w pattern, addiw rd, rs1, 0.
|
|
bool RISCV::isSEXT_W(const MachineInstr &MI) {
|
|
return MI.getOpcode() == RISCV::ADDIW && MI.getOperand(1).isReg() &&
|
|
MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0;
|
|
}
|
|
|
|
// Returns true if this is the zext.w pattern, adduw rd, rs1, x0.
|
|
bool RISCV::isZEXT_W(const MachineInstr &MI) {
|
|
return MI.getOpcode() == RISCV::ADD_UW && MI.getOperand(1).isReg() &&
|
|
MI.getOperand(2).isReg() && MI.getOperand(2).getReg() == RISCV::X0;
|
|
}
|
|
|
|
// Returns true if this is the zext.b pattern, andi rd, rs1, 255.
|
|
bool RISCV::isZEXT_B(const MachineInstr &MI) {
|
|
return MI.getOpcode() == RISCV::ANDI && MI.getOperand(1).isReg() &&
|
|
MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 255;
|
|
}
|
|
|
|
bool RISCV::isRVVSpill(const MachineInstr &MI) {
|
|
switch (MI.getOpcode()) {
|
|
default:
|
|
return false;
|
|
case RISCV::VS1R_V:
|
|
case RISCV::VL1RE8_V:
|
|
case RISCV::VL1RE16_V:
|
|
case RISCV::VL1RE32_V:
|
|
case RISCV::VL1RE64_V:
|
|
return true;
|
|
}
|
|
}
|
|
|
|
bool RISCV::hasEqualFRM(const MachineInstr &MI1, const MachineInstr &MI2) {
|
|
int16_t MI1FrmOpIdx =
|
|
RISCV::getNamedOperandIdx(MI1.getOpcode(), RISCV::OpName::frm);
|
|
int16_t MI2FrmOpIdx =
|
|
RISCV::getNamedOperandIdx(MI2.getOpcode(), RISCV::OpName::frm);
|
|
if (MI1FrmOpIdx < 0 || MI2FrmOpIdx < 0)
|
|
return false;
|
|
MachineOperand FrmOp1 = MI1.getOperand(MI1FrmOpIdx);
|
|
MachineOperand FrmOp2 = MI2.getOperand(MI2FrmOpIdx);
|
|
return FrmOp1.getImm() == FrmOp2.getImm();
|
|
}
|