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-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);
|
|
|
|
|
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-02-10 12:01:24 +00:00
|
|
|
SDNode *SelectREAD_PARAM(SDNode *Node);
|
|
|
|
|
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);
|
|
|
|
|
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 PTXISD::READ_PARAM:
|
|
|
|
return SelectREAD_PARAM(Node);
|
|
|
|
case ISD::BRCOND:
|
|
|
|
return SelectBRCOND(Node);
|
|
|
|
default:
|
|
|
|
return SelectCode(Node);
|
|
|
|
}
|
2011-02-10 12:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SDNode *PTXDAGToDAGISel::SelectREAD_PARAM(SDNode *Node) {
|
2011-03-02 03:20:28 +00:00
|
|
|
SDValue index = Node->getOperand(1);
|
|
|
|
DebugLoc dl = Node->getDebugLoc();
|
|
|
|
unsigned opcode;
|
2011-02-10 12:01:24 +00:00
|
|
|
|
|
|
|
if (index.getOpcode() != ISD::TargetConstant)
|
|
|
|
llvm_unreachable("READ_PARAM: index is not ISD::TargetConstant");
|
|
|
|
|
2011-03-02 03:20:28 +00:00
|
|
|
if (Node->getValueType(0) == MVT::i16) {
|
|
|
|
opcode = PTX::LDpiU16;
|
|
|
|
}
|
|
|
|
else if (Node->getValueType(0) == MVT::i32) {
|
|
|
|
opcode = PTX::LDpiU32;
|
|
|
|
}
|
|
|
|
else if (Node->getValueType(0) == MVT::i64) {
|
|
|
|
opcode = PTX::LDpiU64;
|
|
|
|
}
|
|
|
|
else if (Node->getValueType(0) == MVT::f32) {
|
|
|
|
opcode = PTX::LDpiF32;
|
|
|
|
}
|
|
|
|
else if (Node->getValueType(0) == MVT::f64) {
|
|
|
|
opcode = PTX::LDpiF64;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
llvm_unreachable("Unknown parameter type for ld.param");
|
|
|
|
}
|
|
|
|
|
2011-02-10 12:01:24 +00:00
|
|
|
return PTXInstrInfo::
|
2011-03-02 03:20:28 +00:00
|
|
|
GetPTXMachineNode(CurDAG, opcode, dl, Node->getValueType(0), index);
|
2010-09-18 18:52:28 +00:00
|
|
|
}
|
2010-11-30 07:34:44 +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);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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>();
|
|
|
|
}
|
|
|
|
|