2010-09-18 18:52:28 +00:00
|
|
|
//===-- PTXISelDAGToDAG.cpp - A dag to dag inst selector for PTX ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines an instruction selector for the PTX target.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "PTX.h"
|
|
|
|
#include "PTXTargetMachine.h"
|
|
|
|
#include "llvm/CodeGen/SelectionDAGISel.h"
|
2010-11-30 07:34:44 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2011-06-23 18:10:03 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2011-03-02 03:20:28 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-09-18 18:52:28 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// PTXDAGToDAGISel - PTX specific code to select PTX machine
|
|
|
|
// instructions for SelectionDAG operations.
|
|
|
|
class PTXDAGToDAGISel : public SelectionDAGISel {
|
|
|
|
public:
|
|
|
|
PTXDAGToDAGISel(PTXTargetMachine &TM, CodeGenOpt::Level OptLevel);
|
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "PTX DAG->DAG Pattern Instruction Selection";
|
|
|
|
}
|
|
|
|
|
|
|
|
SDNode *Select(SDNode *Node);
|
|
|
|
|
2010-11-30 07:34:44 +00:00
|
|
|
// Complex Pattern Selectors.
|
2010-12-22 10:38:51 +00:00
|
|
|
bool SelectADDRrr(SDValue &Addr, SDValue &R1, SDValue &R2);
|
2010-11-30 07:34:44 +00:00
|
|
|
bool SelectADDRri(SDValue &Addr, SDValue &Base, SDValue &Offset);
|
|
|
|
bool SelectADDRii(SDValue &Addr, SDValue &Base, SDValue &Offset);
|
2011-09-26 16:20:25 +00:00
|
|
|
bool SelectADDRlocal(SDValue &Addr, SDValue &Base, SDValue &Offset);
|
2010-11-30 07:34:44 +00:00
|
|
|
|
2010-09-18 18:52:28 +00:00
|
|
|
// Include the pieces auto'gened from the target description
|
|
|
|
#include "PTXGenDAGISel.inc"
|
|
|
|
|
2010-11-30 07:34:44 +00:00
|
|
|
private:
|
2011-03-18 11:08:52 +00:00
|
|
|
// We need this only because we can't match intruction BRAdp
|
|
|
|
// pattern (PTXbrcond bb:$d, ...) in PTXInstrInfo.td
|
|
|
|
SDNode *SelectBRCOND(SDNode *Node);
|
|
|
|
|
2011-09-22 16:45:46 +00:00
|
|
|
SDNode *SelectREADPARAM(SDNode *Node);
|
|
|
|
SDNode *SelectWRITEPARAM(SDNode *Node);
|
2011-09-26 16:20:25 +00:00
|
|
|
SDNode *SelectFrameIndex(SDNode *Node);
|
2011-09-22 16:45:46 +00:00
|
|
|
|
2010-12-22 10:38:51 +00:00
|
|
|
bool isImm(const SDValue &operand);
|
|
|
|
bool SelectImm(const SDValue &operand, SDValue &imm);
|
2011-03-02 07:36:48 +00:00
|
|
|
|
|
|
|
const PTXSubtarget& getSubtarget() const;
|
2010-09-18 18:52:28 +00:00
|
|
|
}; // class PTXDAGToDAGISel
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// createPTXISelDag - This pass converts a legalized DAG into a
|
|
|
|
// PTX-specific DAG, ready for instruction scheduling
|
|
|
|
FunctionPass *llvm::createPTXISelDag(PTXTargetMachine &TM,
|
|
|
|
CodeGenOpt::Level OptLevel) {
|
|
|
|
return new PTXDAGToDAGISel(TM, OptLevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
PTXDAGToDAGISel::PTXDAGToDAGISel(PTXTargetMachine &TM,
|
|
|
|
CodeGenOpt::Level OptLevel)
|
|
|
|
: SelectionDAGISel(TM, OptLevel) {}
|
|
|
|
|
|
|
|
SDNode *PTXDAGToDAGISel::Select(SDNode *Node) {
|
2011-03-18 11:08:52 +00:00
|
|
|
switch (Node->getOpcode()) {
|
|
|
|
case ISD::BRCOND:
|
|
|
|
return SelectBRCOND(Node);
|
2011-09-22 16:45:46 +00:00
|
|
|
case PTXISD::READ_PARAM:
|
|
|
|
return SelectREADPARAM(Node);
|
|
|
|
case PTXISD::WRITE_PARAM:
|
|
|
|
return SelectWRITEPARAM(Node);
|
2011-09-26 16:20:25 +00:00
|
|
|
case ISD::FrameIndex:
|
|
|
|
return SelectFrameIndex(Node);
|
2011-03-18 11:08:52 +00:00
|
|
|
default:
|
|
|
|
return SelectCode(Node);
|
|
|
|
}
|
2011-02-10 12:01:24 +00:00
|
|
|
}
|
|
|
|
|
2011-03-18 11:08:52 +00:00
|
|
|
SDNode *PTXDAGToDAGISel::SelectBRCOND(SDNode *Node) {
|
|
|
|
assert(Node->getNumOperands() >= 3);
|
|
|
|
|
|
|
|
SDValue Chain = Node->getOperand(0);
|
|
|
|
SDValue Pred = Node->getOperand(1);
|
|
|
|
SDValue Target = Node->getOperand(2); // branch target
|
|
|
|
SDValue PredOp = CurDAG->getTargetConstant(PTX::PRED_NORMAL, MVT::i32);
|
|
|
|
DebugLoc dl = Node->getDebugLoc();
|
|
|
|
|
|
|
|
assert(Target.getOpcode() == ISD::BasicBlock);
|
|
|
|
assert(Pred.getValueType() == MVT::i1);
|
|
|
|
|
|
|
|
// Emit BRAdp
|
|
|
|
SDValue Ops[] = { Target, Pred, PredOp, Chain };
|
|
|
|
return CurDAG->getMachineNode(PTX::BRAdp, dl, MVT::Other, Ops, 4);
|
|
|
|
}
|
|
|
|
|
2011-09-22 16:45:46 +00:00
|
|
|
SDNode *PTXDAGToDAGISel::SelectREADPARAM(SDNode *Node) {
|
|
|
|
SDValue Chain = Node->getOperand(0);
|
|
|
|
SDValue Index = Node->getOperand(1);
|
|
|
|
|
|
|
|
int OpCode;
|
|
|
|
|
|
|
|
// Get the type of parameter we are reading
|
|
|
|
EVT VT = Node->getValueType(0);
|
|
|
|
assert(VT.isSimple() && "READ_PARAM only implemented for MVT types");
|
|
|
|
|
|
|
|
MVT Type = VT.getSimpleVT();
|
|
|
|
|
|
|
|
if (Type == MVT::i1)
|
|
|
|
OpCode = PTX::READPARAMPRED;
|
|
|
|
else if (Type == MVT::i16)
|
|
|
|
OpCode = PTX::READPARAMI16;
|
|
|
|
else if (Type == MVT::i32)
|
|
|
|
OpCode = PTX::READPARAMI32;
|
|
|
|
else if (Type == MVT::i64)
|
|
|
|
OpCode = PTX::READPARAMI64;
|
|
|
|
else if (Type == MVT::f32)
|
|
|
|
OpCode = PTX::READPARAMF32;
|
|
|
|
else if (Type == MVT::f64)
|
|
|
|
OpCode = PTX::READPARAMF64;
|
|
|
|
|
|
|
|
SDValue Pred = CurDAG->getRegister(PTX::NoRegister, MVT::i1);
|
|
|
|
SDValue PredOp = CurDAG->getTargetConstant(PTX::PRED_NORMAL, MVT::i32);
|
|
|
|
DebugLoc dl = Node->getDebugLoc();
|
|
|
|
|
|
|
|
SDValue Ops[] = { Index, Pred, PredOp, Chain };
|
|
|
|
return CurDAG->getMachineNode(OpCode, dl, VT, Ops, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
SDNode *PTXDAGToDAGISel::SelectWRITEPARAM(SDNode *Node) {
|
|
|
|
|
|
|
|
SDValue Chain = Node->getOperand(0);
|
|
|
|
SDValue Value = Node->getOperand(1);
|
|
|
|
|
|
|
|
int OpCode;
|
|
|
|
|
|
|
|
//Node->dumpr(CurDAG);
|
|
|
|
|
|
|
|
// Get the type of parameter we are writing
|
|
|
|
EVT VT = Value->getValueType(0);
|
|
|
|
assert(VT.isSimple() && "WRITE_PARAM only implemented for MVT types");
|
|
|
|
|
|
|
|
MVT Type = VT.getSimpleVT();
|
|
|
|
|
|
|
|
if (Type == MVT::i1)
|
|
|
|
OpCode = PTX::WRITEPARAMPRED;
|
|
|
|
else if (Type == MVT::i16)
|
|
|
|
OpCode = PTX::WRITEPARAMI16;
|
|
|
|
else if (Type == MVT::i32)
|
|
|
|
OpCode = PTX::WRITEPARAMI32;
|
|
|
|
else if (Type == MVT::i64)
|
|
|
|
OpCode = PTX::WRITEPARAMI64;
|
|
|
|
else if (Type == MVT::f32)
|
|
|
|
OpCode = PTX::WRITEPARAMF32;
|
|
|
|
else if (Type == MVT::f64)
|
|
|
|
OpCode = PTX::WRITEPARAMF64;
|
|
|
|
else
|
|
|
|
llvm_unreachable("Invalid type in SelectWRITEPARAM");
|
|
|
|
|
|
|
|
SDValue Pred = CurDAG->getRegister(PTX::NoRegister, MVT::i1);
|
|
|
|
SDValue PredOp = CurDAG->getTargetConstant(PTX::PRED_NORMAL, MVT::i32);
|
|
|
|
DebugLoc dl = Node->getDebugLoc();
|
|
|
|
|
|
|
|
SDValue Ops[] = { Value, Pred, PredOp, Chain };
|
|
|
|
SDNode* Ret = CurDAG->getMachineNode(OpCode, dl, MVT::Other, Ops, 4);
|
|
|
|
|
|
|
|
//dbgs() << "SelectWRITEPARAM produced:\n\t";
|
|
|
|
//Ret->dumpr(CurDAG);
|
|
|
|
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2011-09-26 16:20:25 +00:00
|
|
|
SDNode *PTXDAGToDAGISel::SelectFrameIndex(SDNode *Node) {
|
|
|
|
int FI = cast<FrameIndexSDNode>(Node)->getIndex();
|
|
|
|
//dbgs() << "Selecting FrameIndex at index " << FI << "\n";
|
|
|
|
SDValue TFI = CurDAG->getTargetFrameIndex(FI, Node->getValueType(0));
|
|
|
|
|
|
|
|
//unsigned OpCode = PTX::LOAD_LOCAL_F32;
|
|
|
|
|
|
|
|
//for (SDNode::use_iterator i = Node->use_begin(), e = Node->use_end();
|
|
|
|
// i != e; ++i) {
|
|
|
|
// SDNode *Use = *i;
|
|
|
|
// dbgs() << "USE: ";
|
|
|
|
// Use->dumpr(CurDAG);
|
|
|
|
//}
|
|
|
|
|
|
|
|
return Node;
|
|
|
|
//return CurDAG->getMachineNode(OpCode, Node->getDebugLoc(),
|
|
|
|
// Node->getValueType(0), TFI);
|
|
|
|
}
|
|
|
|
|
2010-12-22 10:38:51 +00:00
|
|
|
// Match memory operand of the form [reg+reg]
|
|
|
|
bool PTXDAGToDAGISel::SelectADDRrr(SDValue &Addr, SDValue &R1, SDValue &R2) {
|
|
|
|
if (Addr.getOpcode() != ISD::ADD || Addr.getNumOperands() < 2 ||
|
|
|
|
isImm(Addr.getOperand(0)) || isImm(Addr.getOperand(1)))
|
|
|
|
return false;
|
|
|
|
|
2011-03-23 16:58:51 +00:00
|
|
|
assert(Addr.getValueType().isSimple() && "Type must be simple");
|
|
|
|
|
2011-01-01 11:58:58 +00:00
|
|
|
R1 = Addr;
|
2011-03-23 16:58:51 +00:00
|
|
|
R2 = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT());
|
|
|
|
|
2010-12-22 10:38:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match memory operand of the form [reg], [imm+reg], and [reg+imm]
|
2010-11-30 07:34:44 +00:00
|
|
|
bool PTXDAGToDAGISel::SelectADDRri(SDValue &Addr, SDValue &Base,
|
|
|
|
SDValue &Offset) {
|
2010-12-22 10:38:51 +00:00
|
|
|
if (Addr.getOpcode() != ISD::ADD) {
|
2011-01-01 11:58:58 +00:00
|
|
|
// let SelectADDRii handle the [imm] case
|
2010-12-22 10:38:51 +00:00
|
|
|
if (isImm(Addr))
|
|
|
|
return false;
|
2011-01-01 11:58:58 +00:00
|
|
|
// it is [reg]
|
2011-03-23 16:58:51 +00:00
|
|
|
|
|
|
|
assert(Addr.getValueType().isSimple() && "Type must be simple");
|
|
|
|
|
2010-12-22 10:38:51 +00:00
|
|
|
Base = Addr;
|
2011-03-23 16:58:51 +00:00
|
|
|
Offset = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT());
|
|
|
|
|
2010-12-22 10:38:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-01 11:58:58 +00:00
|
|
|
if (Addr.getNumOperands() < 2)
|
|
|
|
return false;
|
|
|
|
|
2010-12-22 10:38:51 +00:00
|
|
|
// let SelectADDRii handle the [imm+imm] case
|
2011-01-01 11:58:58 +00:00
|
|
|
if (isImm(Addr.getOperand(0)) && isImm(Addr.getOperand(1)))
|
2010-12-22 10:38:51 +00:00
|
|
|
return false;
|
2010-11-30 07:34:44 +00:00
|
|
|
|
|
|
|
// try [reg+imm] and [imm+reg]
|
2010-12-22 10:38:51 +00:00
|
|
|
for (int i = 0; i < 2; i ++)
|
|
|
|
if (SelectImm(Addr.getOperand(1-i), Offset)) {
|
|
|
|
Base = Addr.getOperand(i);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-01 11:58:58 +00:00
|
|
|
// neither [reg+imm] nor [imm+reg]
|
2010-12-22 10:38:51 +00:00
|
|
|
return false;
|
2010-11-30 07:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Match memory operand of the form [imm+imm] and [imm]
|
|
|
|
bool PTXDAGToDAGISel::SelectADDRii(SDValue &Addr, SDValue &Base,
|
|
|
|
SDValue &Offset) {
|
2010-12-22 10:38:51 +00:00
|
|
|
// is [imm+imm]?
|
2010-11-30 07:34:44 +00:00
|
|
|
if (Addr.getOpcode() == ISD::ADD) {
|
|
|
|
return SelectImm(Addr.getOperand(0), Base) &&
|
|
|
|
SelectImm(Addr.getOperand(1), Offset);
|
|
|
|
}
|
|
|
|
|
2010-12-22 10:38:51 +00:00
|
|
|
// is [imm]?
|
2010-11-30 07:34:44 +00:00
|
|
|
if (SelectImm(Addr, Base)) {
|
2011-03-23 16:58:51 +00:00
|
|
|
assert(Addr.getValueType().isSimple() && "Type must be simple");
|
|
|
|
|
|
|
|
Offset = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT());
|
|
|
|
|
2010-11-30 07:34:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-26 16:20:25 +00:00
|
|
|
// Match memory operand of the form [reg], [imm+reg], and [reg+imm]
|
|
|
|
bool PTXDAGToDAGISel::SelectADDRlocal(SDValue &Addr, SDValue &Base,
|
|
|
|
SDValue &Offset) {
|
|
|
|
if (Addr.getOpcode() != ISD::ADD) {
|
|
|
|
// let SelectADDRii handle the [imm] case
|
|
|
|
if (isImm(Addr))
|
|
|
|
return false;
|
|
|
|
// it is [reg]
|
|
|
|
|
|
|
|
assert(Addr.getValueType().isSimple() && "Type must be simple");
|
|
|
|
|
|
|
|
Base = Addr;
|
|
|
|
Offset = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Addr.getNumOperands() < 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// let SelectADDRii handle the [imm+imm] case
|
|
|
|
if (isImm(Addr.getOperand(0)) && isImm(Addr.getOperand(1)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// try [reg+imm] and [imm+reg]
|
|
|
|
for (int i = 0; i < 2; i ++)
|
|
|
|
if (SelectImm(Addr.getOperand(1-i), Offset)) {
|
|
|
|
Base = Addr.getOperand(i);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// neither [reg+imm] nor [imm+reg]
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-30 07:34:44 +00:00
|
|
|
bool PTXDAGToDAGISel::isImm(const SDValue &operand) {
|
|
|
|
return ConstantSDNode::classof(operand.getNode());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PTXDAGToDAGISel::SelectImm(const SDValue &operand, SDValue &imm) {
|
|
|
|
SDNode *node = operand.getNode();
|
|
|
|
if (!ConstantSDNode::classof(node))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ConstantSDNode *CN = cast<ConstantSDNode>(node);
|
2011-03-23 16:58:51 +00:00
|
|
|
imm = CurDAG->getTargetConstant(*CN->getConstantIntValue(),
|
|
|
|
operand.getValueType());
|
2010-11-30 07:34:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-03-02 07:36:48 +00:00
|
|
|
|
|
|
|
const PTXSubtarget& PTXDAGToDAGISel::getSubtarget() const
|
|
|
|
{
|
|
|
|
return TM.getSubtarget<PTXSubtarget>();
|
|
|
|
}
|
|
|
|
|