#include "LanaiAluCode.h"
#include "LanaiMachineFunctionInfo.h"
#include "LanaiRegisterInfo.h"
#include "LanaiSubtarget.h"
#include "LanaiTargetMachine.h"
#include "llvm/CodeGen/MachineConstantPool.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/SelectionDAGISel.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
using namespace llvm;
#define DEBUG_TYPE "lanai-isel"
namespace {
class LanaiDAGToDAGISel : public SelectionDAGISel {
public:
explicit LanaiDAGToDAGISel(LanaiTargetMachine &TargetMachine)
: SelectionDAGISel(TargetMachine) {}
bool runOnMachineFunction(MachineFunction &MF) override {
return SelectionDAGISel::runOnMachineFunction(MF);
}
StringRef getPassName() const override {
return "Lanai DAG->DAG Pattern Instruction Selection";
}
bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintCode,
std::vector<SDValue> &OutOps) override;
private:
#include "LanaiGenDAGISel.inc"
void Select(SDNode *N) override;
void selectFrameIndex(SDNode *N);
bool selectAddrRi(SDValue Addr, SDValue &Base, SDValue &Offset,
SDValue &AluOp);
bool selectAddrRr(SDValue Addr, SDValue &R1, SDValue &R2, SDValue &AluOp);
bool selectAddrSls(SDValue Addr, SDValue &Offset);
bool selectAddrSpls(SDValue Addr, SDValue &Base, SDValue &Offset,
SDValue &AluOp);
inline SDValue getI32Imm(unsigned Imm, const SDLoc &DL) {
return CurDAG->getTargetConstant(Imm, DL, MVT::i32);
}
private:
bool selectAddrRiSpls(SDValue Addr, SDValue &Base, SDValue &Offset,
SDValue &AluOp, bool RiMode);
};
bool canBeRepresentedAsSls(const ConstantSDNode &CN) {
return isInt<21>(CN.getSExtValue()) && ((CN.getSExtValue() & 0x3) == 0);
}
}
bool LanaiDAGToDAGISel::selectAddrSls(SDValue Addr, SDValue &Offset) {
if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr)) {
SDLoc DL(Addr);
if (canBeRepresentedAsSls(*CN)) {
int32_t Imm = CN->getSExtValue();
Offset = CurDAG->getTargetConstant(Imm, DL, CN->getValueType(0));
return true;
}
}
if (Addr.getOpcode() == ISD::OR &&
Addr.getOperand(1).getOpcode() == LanaiISD::SMALL) {
Offset = Addr.getOperand(1).getOperand(0);
return true;
}
return false;
}
bool LanaiDAGToDAGISel::selectAddrRiSpls(SDValue Addr, SDValue &Base,
SDValue &Offset, SDValue &AluOp,
bool RiMode) {
SDLoc DL(Addr);
if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr)) {
if (RiMode) {
if (isInt<16>(CN->getSExtValue())) {
int16_t Imm = CN->getSExtValue();
Offset = CurDAG->getTargetConstant(Imm, DL, CN->getValueType(0));
Base = CurDAG->getRegister(Lanai::R0, CN->getValueType(0));
AluOp = CurDAG->getTargetConstant(LPAC::ADD, DL, MVT::i32);
return true;
}
if (canBeRepresentedAsSls(*CN))
return false;
} else {
if (isInt<10>(CN->getSExtValue())) {
int16_t Imm = CN->getSExtValue();
Offset = CurDAG->getTargetConstant(Imm, DL, CN->getValueType(0));
Base = CurDAG->getRegister(Lanai::R0, CN->getValueType(0));
AluOp = CurDAG->getTargetConstant(LPAC::ADD, DL, MVT::i32);
return true;
}
}
}
if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Base = CurDAG->getTargetFrameIndex(
FIN->getIndex(),
getTargetLowering()->getPointerTy(CurDAG->getDataLayout()));
Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
AluOp = CurDAG->getTargetConstant(LPAC::ADD, DL, MVT::i32);
return true;
}
if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Addr.getOpcode() == ISD::TargetGlobalAddress))
return false;
ISD::NodeType AluOperator = static_cast<ISD::NodeType>(Addr.getOpcode());
if (AluOperator == ISD::ADD) {
AluOp = CurDAG->getTargetConstant(LPAC::ADD, DL, MVT::i32);
if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
if ((RiMode && isInt<16>(CN->getSExtValue())) ||
(!RiMode && isInt<10>(CN->getSExtValue()))) {
if (FrameIndexSDNode *FIN =
dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
Base = CurDAG->getTargetFrameIndex(
FIN->getIndex(),
getTargetLowering()->getPointerTy(CurDAG->getDataLayout()));
} else {
Base = Addr.getOperand(0);
}
Offset = CurDAG->getTargetConstant(CN->getSExtValue(), DL, MVT::i32);
return true;
}
}
if (AluOperator == ISD::OR && RiMode &&
Addr.getOperand(1).getOpcode() == LanaiISD::SMALL)
return false;
Base = Addr;
Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
AluOp = CurDAG->getTargetConstant(LPAC::ADD, DL, MVT::i32);
return true;
}
bool LanaiDAGToDAGISel::selectAddrRi(SDValue Addr, SDValue &Base,
SDValue &Offset, SDValue &AluOp) {
return selectAddrRiSpls(Addr, Base, Offset, AluOp, true);
}
bool LanaiDAGToDAGISel::selectAddrSpls(SDValue Addr, SDValue &Base,
SDValue &Offset, SDValue &AluOp) {
return selectAddrRiSpls(Addr, Base, Offset, AluOp, false);
}
bool LanaiDAGToDAGISel::selectAddrRr(SDValue Addr, SDValue &R1, SDValue &R2,
SDValue &AluOp) {
if (Addr.getOpcode() == ISD::FrameIndex)
return false;
if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Addr.getOpcode() == ISD::TargetGlobalAddress))
return false;
ISD::NodeType AluOperator = static_cast<ISD::NodeType>(Addr.getOpcode());
LPAC::AluCode AluCode = LPAC::isdToLanaiAluCode(AluOperator);
if (AluCode != LPAC::UNKNOWN) {
if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
if (isInt<16>(CN->getSExtValue()))
return false;
if (Addr.getOperand(0).getOpcode() == LanaiISD::HI ||
Addr.getOperand(0).getOpcode() == LanaiISD::LO ||
Addr.getOperand(0).getOpcode() == LanaiISD::SMALL ||
Addr.getOperand(1).getOpcode() == LanaiISD::HI ||
Addr.getOperand(1).getOpcode() == LanaiISD::LO ||
Addr.getOperand(1).getOpcode() == LanaiISD::SMALL)
return false;
R1 = Addr.getOperand(0);
R2 = Addr.getOperand(1);
AluOp = CurDAG->getTargetConstant(AluCode, SDLoc(Addr), MVT::i32);
return true;
}
return false;
}
bool LanaiDAGToDAGISel::SelectInlineAsmMemoryOperand(
const SDValue &Op, unsigned ConstraintCode, std::vector<SDValue> &OutOps) {
SDValue Op0, Op1, AluOp;
switch (ConstraintCode) {
default:
return true;
case InlineAsm::Constraint_m: if (!selectAddrRr(Op, Op0, Op1, AluOp) &&
!selectAddrRi(Op, Op0, Op1, AluOp))
return true;
break;
}
OutOps.push_back(Op0);
OutOps.push_back(Op1);
OutOps.push_back(AluOp);
return false;
}
void LanaiDAGToDAGISel::Select(SDNode *Node) {
unsigned Opcode = Node->getOpcode();
if (Node->isMachineOpcode()) {
LLVM_DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
return;
}
EVT VT = Node->getValueType(0);
switch (Opcode) {
case ISD::Constant:
if (VT == MVT::i32) {
ConstantSDNode *ConstNode = cast<ConstantSDNode>(Node);
if (ConstNode->isZero()) {
SDValue New = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
SDLoc(Node), Lanai::R0, MVT::i32);
return ReplaceNode(Node, New.getNode());
}
if (ConstNode->isAllOnes()) {
SDValue New = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
SDLoc(Node), Lanai::R1, MVT::i32);
return ReplaceNode(Node, New.getNode());
}
}
break;
case ISD::FrameIndex:
selectFrameIndex(Node);
return;
default:
break;
}
SelectCode(Node);
}
void LanaiDAGToDAGISel::selectFrameIndex(SDNode *Node) {
SDLoc DL(Node);
SDValue Imm = CurDAG->getTargetConstant(0, DL, MVT::i32);
int FI = cast<FrameIndexSDNode>(Node)->getIndex();
EVT VT = Node->getValueType(0);
SDValue TFI = CurDAG->getTargetFrameIndex(FI, VT);
unsigned Opc = Lanai::ADD_I_LO;
if (Node->hasOneUse()) {
CurDAG->SelectNodeTo(Node, Opc, VT, TFI, Imm);
return;
}
ReplaceNode(Node, CurDAG->getMachineNode(Opc, DL, VT, TFI, Imm));
}
FunctionPass *llvm::createLanaiISelDag(LanaiTargetMachine &TM) {
return new LanaiDAGToDAGISel(TM);
}