2017-10-20 05:37:38 +08:00
|
|
|
//===-- RISCVInstrInfo.cpp - RISCV Instruction Information ------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains the RISCV implementation of the TargetInstrInfo class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "RISCVInstrInfo.h"
|
|
|
|
#include "RISCV.h"
|
|
|
|
#include "RISCVSubtarget.h"
|
|
|
|
#include "RISCVTargetMachine.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2018-01-11 05:05:07 +08:00
|
|
|
#include "llvm/CodeGen/RegisterScavenging.h"
|
2017-10-20 05:37:38 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
|
|
|
|
#define GET_INSTRINFO_CTOR_DTOR
|
|
|
|
#include "RISCVGenInstrInfo.inc"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2017-11-08 21:41:21 +08:00
|
|
|
RISCVInstrInfo::RISCVInstrInfo()
|
|
|
|
: RISCVGenInstrInfo(RISCV::ADJCALLSTACKDOWN, RISCV::ADJCALLSTACKUP) {}
|
2017-11-08 20:20:01 +08:00
|
|
|
|
|
|
|
void RISCVInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
const DebugLoc &DL, unsigned DstReg,
|
|
|
|
unsigned SrcReg, bool KillSrc) const {
|
2018-03-21 23:11:02 +08:00
|
|
|
if (RISCV::GPRRegClass.contains(DstReg, SrcReg)) {
|
|
|
|
BuildMI(MBB, MBBI, DL, get(RISCV::ADDI), DstReg)
|
|
|
|
.addReg(SrcReg, getKillRegState(KillSrc))
|
|
|
|
.addImm(0);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-08 20:20:01 +08:00
|
|
|
|
2018-04-12 13:50:06 +08:00
|
|
|
// FPR->FPR copies
|
|
|
|
unsigned Opc;
|
|
|
|
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));
|
2017-11-08 20:20:01 +08:00
|
|
|
}
|
2017-11-08 21:31:40 +08:00
|
|
|
|
|
|
|
void RISCVInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I,
|
|
|
|
unsigned SrcReg, bool IsKill, int FI,
|
|
|
|
const TargetRegisterClass *RC,
|
|
|
|
const TargetRegisterInfo *TRI) const {
|
|
|
|
DebugLoc DL;
|
|
|
|
if (I != MBB.end())
|
|
|
|
DL = I->getDebugLoc();
|
|
|
|
|
2018-03-21 23:11:02 +08:00
|
|
|
unsigned Opcode;
|
|
|
|
|
2017-12-07 20:45:05 +08:00
|
|
|
if (RISCV::GPRRegClass.hasSubClassEq(RC))
|
2018-03-21 23:11:02 +08:00
|
|
|
Opcode = RISCV::SW;
|
|
|
|
else if (RISCV::FPR32RegClass.hasSubClassEq(RC))
|
|
|
|
Opcode = RISCV::FSW;
|
2018-04-12 13:34:25 +08:00
|
|
|
else if (RISCV::FPR64RegClass.hasSubClassEq(RC))
|
|
|
|
Opcode = RISCV::FSD;
|
2017-11-08 21:31:40 +08:00
|
|
|
else
|
|
|
|
llvm_unreachable("Can't store this register to stack slot");
|
2018-03-21 23:11:02 +08:00
|
|
|
|
|
|
|
BuildMI(MBB, I, DL, get(Opcode))
|
|
|
|
.addReg(SrcReg, getKillRegState(IsKill))
|
|
|
|
.addFrameIndex(FI)
|
|
|
|
.addImm(0);
|
2017-11-08 21:31:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RISCVInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I,
|
|
|
|
unsigned DstReg, int FI,
|
|
|
|
const TargetRegisterClass *RC,
|
|
|
|
const TargetRegisterInfo *TRI) const {
|
|
|
|
DebugLoc DL;
|
|
|
|
if (I != MBB.end())
|
|
|
|
DL = I->getDebugLoc();
|
|
|
|
|
2018-03-21 23:11:02 +08:00
|
|
|
unsigned Opcode;
|
|
|
|
|
2017-12-07 20:45:05 +08:00
|
|
|
if (RISCV::GPRRegClass.hasSubClassEq(RC))
|
2018-03-21 23:11:02 +08:00
|
|
|
Opcode = RISCV::LW;
|
|
|
|
else if (RISCV::FPR32RegClass.hasSubClassEq(RC))
|
|
|
|
Opcode = RISCV::FLW;
|
2018-04-12 13:34:25 +08:00
|
|
|
else if (RISCV::FPR64RegClass.hasSubClassEq(RC))
|
|
|
|
Opcode = RISCV::FLD;
|
2017-11-08 21:31:40 +08:00
|
|
|
else
|
|
|
|
llvm_unreachable("Can't load this register from stack slot");
|
2018-03-21 23:11:02 +08:00
|
|
|
|
|
|
|
BuildMI(MBB, I, DL, get(Opcode), DstReg).addFrameIndex(FI).addImm(0);
|
2017-11-08 21:31:40 +08:00
|
|
|
}
|
2018-01-11 03:53:46 +08:00
|
|
|
|
|
|
|
void RISCVInstrInfo::movImm32(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
const DebugLoc &DL, unsigned DstReg, uint64_t Val,
|
|
|
|
MachineInstr::MIFlag Flag) const {
|
|
|
|
assert(isInt<32>(Val) && "Can only materialize 32-bit constants");
|
|
|
|
|
2018-04-19 03:02:31 +08:00
|
|
|
// TODO: If the value can be materialized using only one instruction, only
|
|
|
|
// insert a single instruction.
|
|
|
|
|
|
|
|
uint64_t Hi20 = ((Val + 0x800) >> 12) & 0xfffff;
|
|
|
|
uint64_t Lo12 = SignExtend64<12>(Val);
|
|
|
|
BuildMI(MBB, MBBI, DL, get(RISCV::LUI), DstReg)
|
|
|
|
.addImm(Hi20)
|
|
|
|
.setMIFlag(Flag);
|
|
|
|
BuildMI(MBB, MBBI, DL, get(RISCV::ADDI), DstReg)
|
|
|
|
.addReg(DstReg, RegState::Kill)
|
|
|
|
.addImm(Lo12)
|
2018-01-11 03:53:46 +08:00
|
|
|
.setMIFlag(Flag);
|
|
|
|
}
|
2018-01-11 04:47:00 +08:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
Cond.push_back(MachineOperand::CreateImm(LastInst.getOpcode()));
|
|
|
|
Cond.push_back(LastInst.getOperand(0));
|
|
|
|
Cond.push_back(LastInst.getOperand(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getOppositeBranchOpcode(int Opc) {
|
|
|
|
switch (Opc) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unrecognized conditional branch");
|
|
|
|
case RISCV::BEQ:
|
|
|
|
return RISCV::BNE;
|
|
|
|
case RISCV::BNE:
|
|
|
|
return RISCV::BEQ;
|
|
|
|
case RISCV::BLT:
|
|
|
|
return RISCV::BGE;
|
|
|
|
case RISCV::BGE:
|
|
|
|
return RISCV::BLT;
|
|
|
|
case RISCV::BLTU:
|
|
|
|
return RISCV::BGEU;
|
|
|
|
case RISCV::BGEU:
|
|
|
|
return RISCV::BLTU;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = I->getOperand(0).getMBB();
|
|
|
|
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 = I->getOperand(0).getMBB();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we can't handle this.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned RISCVInstrInfo::removeBranch(MachineBasicBlock &MBB,
|
|
|
|
int *BytesRemoved) const {
|
2018-01-11 05:05:07 +08:00
|
|
|
if (BytesRemoved)
|
|
|
|
*BytesRemoved = 0;
|
2018-01-11 04:47:00 +08:00
|
|
|
MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
|
|
|
|
if (I == MBB.end())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!I->getDesc().isUnconditionalBranch() &&
|
|
|
|
!I->getDesc().isConditionalBranch())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Remove the branch.
|
|
|
|
I->eraseFromParent();
|
2018-01-11 05:05:07 +08:00
|
|
|
if (BytesRemoved)
|
|
|
|
*BytesRemoved += getInstSizeInBytes(*I);
|
2018-01-11 04:47:00 +08:00
|
|
|
|
|
|
|
I = MBB.end();
|
|
|
|
|
|
|
|
if (I == MBB.begin())
|
|
|
|
return 1;
|
|
|
|
--I;
|
|
|
|
if (!I->getDesc().isConditionalBranch())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// Remove the branch.
|
|
|
|
I->eraseFromParent();
|
2018-01-11 05:05:07 +08:00
|
|
|
if (BytesRemoved)
|
|
|
|
*BytesRemoved += getInstSizeInBytes(*I);
|
2018-01-11 04:47:00 +08:00
|
|
|
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 {
|
2018-01-11 05:05:07 +08:00
|
|
|
if (BytesAdded)
|
|
|
|
*BytesAdded = 0;
|
2018-01-11 04:47:00 +08:00
|
|
|
|
|
|
|
// 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()) {
|
2018-01-11 05:05:07 +08:00
|
|
|
MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(TBB);
|
|
|
|
if (BytesAdded)
|
|
|
|
*BytesAdded += getInstSizeInBytes(MI);
|
2018-01-11 04:47:00 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Either a one or two-way conditional branch.
|
|
|
|
unsigned Opc = Cond[0].getImm();
|
2018-01-11 05:05:07 +08:00
|
|
|
MachineInstr &CondMI =
|
|
|
|
*BuildMI(&MBB, DL, get(Opc)).add(Cond[1]).add(Cond[2]).addMBB(TBB);
|
|
|
|
if (BytesAdded)
|
|
|
|
*BytesAdded += getInstSizeInBytes(CondMI);
|
2018-01-11 04:47:00 +08:00
|
|
|
|
|
|
|
// One-way conditional branch.
|
|
|
|
if (!FBB)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// Two-way conditional branch.
|
2018-01-11 05:05:07 +08:00
|
|
|
MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(FBB);
|
|
|
|
if (BytesAdded)
|
|
|
|
*BytesAdded += getInstSizeInBytes(MI);
|
2018-01-11 04:47:00 +08:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2018-01-11 05:05:07 +08:00
|
|
|
unsigned RISCVInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock &DestBB,
|
|
|
|
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);
|
|
|
|
|
|
|
|
MachineFunction *MF = MBB.getParent();
|
|
|
|
MachineRegisterInfo &MRI = MF->getRegInfo();
|
|
|
|
const auto &TM = static_cast<const RISCVTargetMachine &>(MF->getTarget());
|
|
|
|
const auto &STI = MF->getSubtarget<RISCVSubtarget>();
|
|
|
|
|
|
|
|
if (TM.isPositionIndependent() || STI.is64Bit())
|
|
|
|
report_fatal_error("Unable to insert indirect branch");
|
|
|
|
|
|
|
|
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).
|
|
|
|
unsigned ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
|
|
|
|
auto II = MBB.end();
|
|
|
|
|
|
|
|
MachineInstr &LuiMI = *BuildMI(MBB, II, DL, get(RISCV::LUI), ScratchReg)
|
|
|
|
.addMBB(&DestBB, RISCVII::MO_HI);
|
|
|
|
BuildMI(MBB, II, DL, get(RISCV::PseudoBRIND))
|
|
|
|
.addReg(ScratchReg, RegState::Kill)
|
|
|
|
.addMBB(&DestBB, RISCVII::MO_LO);
|
|
|
|
|
|
|
|
RS->enterBasicBlockEnd(MBB);
|
|
|
|
unsigned Scav = RS->scavengeRegisterBackwards(
|
|
|
|
RISCV::GPRRegClass, MachineBasicBlock::iterator(LuiMI), false, 0);
|
|
|
|
MRI.replaceRegWith(ScratchReg, Scav);
|
|
|
|
MRI.clearVirtRegs();
|
|
|
|
RS->setRegUsed(Scav);
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
2018-01-11 04:47:00 +08:00
|
|
|
bool RISCVInstrInfo::reverseBranchCondition(
|
|
|
|
SmallVectorImpl<MachineOperand> &Cond) const {
|
|
|
|
assert((Cond.size() == 3) && "Invalid branch condition!");
|
|
|
|
Cond[0].setImm(getOppositeBranchOpcode(Cond[0].getImm()));
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-11 05:05:07 +08:00
|
|
|
|
|
|
|
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 {
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned RISCVInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
|
|
|
|
unsigned Opcode = MI.getOpcode();
|
|
|
|
|
|
|
|
switch (Opcode) {
|
|
|
|
default: { return get(Opcode).getSize(); }
|
|
|
|
case TargetOpcode::EH_LABEL:
|
|
|
|
case TargetOpcode::IMPLICIT_DEF:
|
|
|
|
case TargetOpcode::KILL:
|
|
|
|
case TargetOpcode::DBG_VALUE:
|
|
|
|
return 0;
|
|
|
|
case TargetOpcode::INLINEASM: {
|
|
|
|
const MachineFunction &MF = *MI.getParent()->getParent();
|
|
|
|
const auto &TM = static_cast<const RISCVTargetMachine &>(MF.getTarget());
|
|
|
|
return getInlineAsmLength(MI.getOperand(0).getSymbolName(),
|
|
|
|
*TM.getMCAsmInfo());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|