2010-09-18 18:52:28 +00:00
|
|
|
//===-- PTXISelLowering.cpp - PTX DAG Lowering Implementation -------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the PTXTargetLowering class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-10-19 13:14:40 +00:00
|
|
|
#include "PTX.h"
|
2010-09-18 18:52:28 +00:00
|
|
|
#include "PTXISelLowering.h"
|
2010-11-08 03:00:52 +00:00
|
|
|
#include "PTXMachineFunctionInfo.h"
|
2010-09-18 18:52:28 +00:00
|
|
|
#include "PTXRegisterInfo.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-10-19 13:14:40 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2010-09-18 18:52:28 +00:00
|
|
|
#include "llvm/CodeGen/SelectionDAG.h"
|
|
|
|
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.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;
|
|
|
|
|
|
|
|
PTXTargetLowering::PTXTargetLowering(TargetMachine &TM)
|
|
|
|
: TargetLowering(TM, new TargetLoweringObjectFileELF()) {
|
|
|
|
// Set up the register classes.
|
2010-10-19 13:14:40 +00:00
|
|
|
addRegisterClass(MVT::i1, PTX::PredsRegisterClass);
|
2011-03-02 03:20:28 +00:00
|
|
|
addRegisterClass(MVT::i16, PTX::RRegu16RegisterClass);
|
|
|
|
addRegisterClass(MVT::i32, PTX::RRegu32RegisterClass);
|
|
|
|
addRegisterClass(MVT::i64, PTX::RRegu64RegisterClass);
|
2011-02-28 06:34:09 +00:00
|
|
|
addRegisterClass(MVT::f32, PTX::RRegf32RegisterClass);
|
2011-03-02 03:20:28 +00:00
|
|
|
addRegisterClass(MVT::f64, PTX::RRegf64RegisterClass);
|
|
|
|
|
2010-12-22 10:38:51 +00:00
|
|
|
setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
|
|
|
|
|
2011-02-28 06:34:09 +00:00
|
|
|
setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
|
2011-03-02 03:20:28 +00:00
|
|
|
setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
|
|
|
|
|
2010-12-22 10:38:51 +00:00
|
|
|
// Customize translation of memory addresses
|
|
|
|
setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
|
2011-03-23 16:58:51 +00:00
|
|
|
setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
|
2010-12-22 10:38:51 +00:00
|
|
|
|
2011-03-18 11:08:52 +00:00
|
|
|
// Expand BR_CC into BRCOND
|
|
|
|
setOperationAction(ISD::BR_CC, MVT::Other, Expand);
|
|
|
|
|
2010-09-18 18:52:28 +00:00
|
|
|
// Compute derived properties from the register classes
|
|
|
|
computeRegisterProperties();
|
|
|
|
}
|
|
|
|
|
2010-12-22 10:38:51 +00:00
|
|
|
SDValue PTXTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
|
|
|
|
switch (Op.getOpcode()) {
|
2011-03-18 11:08:52 +00:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Unimplemented operand");
|
|
|
|
case ISD::GlobalAddress:
|
|
|
|
return LowerGlobalAddress(Op, DAG);
|
2010-12-22 10:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-18 18:52:28 +00:00
|
|
|
const char *PTXTargetLowering::getTargetNodeName(unsigned Opcode) const {
|
|
|
|
switch (Opcode) {
|
2011-02-10 12:01:24 +00:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown opcode");
|
2011-03-18 19:24:28 +00:00
|
|
|
case PTXISD::COPY_ADDRESS:
|
|
|
|
return "PTXISD::COPY_ADDRESS";
|
2011-02-10 12:01:24 +00:00
|
|
|
case PTXISD::READ_PARAM:
|
|
|
|
return "PTXISD::READ_PARAM";
|
|
|
|
case PTXISD::EXIT:
|
|
|
|
return "PTXISD::EXIT";
|
|
|
|
case PTXISD::RET:
|
|
|
|
return "PTXISD::RET";
|
2010-09-18 18:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-22 10:38:51 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Custom Lower Operation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
SDValue PTXTargetLowering::
|
|
|
|
LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
|
|
|
|
EVT PtrVT = getPointerTy();
|
|
|
|
DebugLoc dl = Op.getDebugLoc();
|
|
|
|
const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
|
2011-03-18 19:24:28 +00:00
|
|
|
|
2011-03-23 16:58:51 +00:00
|
|
|
assert(PtrVT.isSimple() && "Pointer must be to primitive type.");
|
|
|
|
|
2011-03-18 19:24:28 +00:00
|
|
|
SDValue targetGlobal = DAG.getTargetGlobalAddress(GV, dl, PtrVT);
|
|
|
|
SDValue movInstr = DAG.getNode(PTXISD::COPY_ADDRESS,
|
|
|
|
dl,
|
2011-03-23 16:58:51 +00:00
|
|
|
PtrVT.getSimpleVT(),
|
2011-03-18 19:24:28 +00:00
|
|
|
targetGlobal);
|
|
|
|
|
|
|
|
return movInstr;
|
2010-12-22 10:38:51 +00:00
|
|
|
}
|
|
|
|
|
2010-09-18 18:52:28 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Calling Convention Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-10-22 17:35:07 +00:00
|
|
|
namespace {
|
|
|
|
struct argmap_entry {
|
2010-10-19 13:14:40 +00:00
|
|
|
MVT::SimpleValueType VT;
|
|
|
|
TargetRegisterClass *RC;
|
|
|
|
TargetRegisterClass::iterator loc;
|
|
|
|
|
|
|
|
argmap_entry(MVT::SimpleValueType _VT, TargetRegisterClass *_RC)
|
|
|
|
: VT(_VT), RC(_RC), loc(_RC->begin()) {}
|
|
|
|
|
2010-10-22 17:35:07 +00:00
|
|
|
void reset() { loc = RC->begin(); }
|
|
|
|
bool operator==(MVT::SimpleValueType _VT) const { return VT == _VT; }
|
2010-10-19 13:14:40 +00:00
|
|
|
} argmap[] = {
|
|
|
|
argmap_entry(MVT::i1, PTX::PredsRegisterClass),
|
2011-03-02 03:20:28 +00:00
|
|
|
argmap_entry(MVT::i16, PTX::RRegu16RegisterClass),
|
|
|
|
argmap_entry(MVT::i32, PTX::RRegu32RegisterClass),
|
|
|
|
argmap_entry(MVT::i64, PTX::RRegu64RegisterClass),
|
|
|
|
argmap_entry(MVT::f32, PTX::RRegf32RegisterClass),
|
|
|
|
argmap_entry(MVT::f64, PTX::RRegf64RegisterClass)
|
2010-10-19 13:14:40 +00:00
|
|
|
};
|
2011-03-02 03:20:28 +00:00
|
|
|
} // end anonymous namespace
|
2010-10-19 13:14:40 +00:00
|
|
|
|
2010-09-18 18:52:28 +00:00
|
|
|
SDValue PTXTargetLowering::
|
|
|
|
LowerFormalArguments(SDValue Chain,
|
|
|
|
CallingConv::ID CallConv,
|
|
|
|
bool isVarArg,
|
|
|
|
const SmallVectorImpl<ISD::InputArg> &Ins,
|
|
|
|
DebugLoc dl,
|
|
|
|
SelectionDAG &DAG,
|
|
|
|
SmallVectorImpl<SDValue> &InVals) const {
|
2010-10-19 13:14:40 +00:00
|
|
|
if (isVarArg) llvm_unreachable("PTX does not support varargs");
|
|
|
|
|
2010-11-08 03:00:52 +00:00
|
|
|
MachineFunction &MF = DAG.getMachineFunction();
|
|
|
|
PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
|
|
|
|
|
2010-10-19 13:14:40 +00:00
|
|
|
switch (CallConv) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unsupported calling convention");
|
|
|
|
break;
|
|
|
|
case CallingConv::PTX_Kernel:
|
2011-02-10 12:01:24 +00:00
|
|
|
MFI->setKernel(true);
|
2010-10-19 13:14:40 +00:00
|
|
|
break;
|
|
|
|
case CallingConv::PTX_Device:
|
2010-11-08 03:00:52 +00:00
|
|
|
MFI->setKernel(false);
|
2010-10-19 13:14:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-02-10 12:01:24 +00:00
|
|
|
// Make sure we don't add argument registers twice
|
|
|
|
if (MFI->isDoneAddArg())
|
|
|
|
llvm_unreachable("cannot add argument registers twice");
|
|
|
|
|
2010-10-19 13:14:40 +00:00
|
|
|
// Reset argmap before allocation
|
|
|
|
for (struct argmap_entry *i = argmap, *e = argmap + array_lengthof(argmap);
|
|
|
|
i != e; ++ i)
|
|
|
|
i->reset();
|
|
|
|
|
|
|
|
for (int i = 0, e = Ins.size(); i != e; ++ i) {
|
2010-11-03 11:35:31 +00:00
|
|
|
MVT::SimpleValueType VT = Ins[i].VT.SimpleTy;
|
2010-10-19 13:14:40 +00:00
|
|
|
|
|
|
|
struct argmap_entry *entry = std::find(argmap,
|
|
|
|
argmap + array_lengthof(argmap), VT);
|
|
|
|
if (entry == argmap + array_lengthof(argmap))
|
|
|
|
llvm_unreachable("Type of argument is not supported");
|
|
|
|
|
2011-02-10 12:01:24 +00:00
|
|
|
if (MFI->isKernel() && entry->RC == PTX::PredsRegisterClass)
|
|
|
|
llvm_unreachable("cannot pass preds to kernel");
|
|
|
|
|
|
|
|
MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
|
2010-11-08 03:00:52 +00:00
|
|
|
|
2011-02-10 12:01:24 +00:00
|
|
|
unsigned preg = *++(entry->loc); // allocate start from register 1
|
|
|
|
unsigned vreg = RegInfo.createVirtualRegister(entry->RC);
|
|
|
|
RegInfo.addLiveIn(preg, vreg);
|
|
|
|
|
|
|
|
MFI->addArgReg(preg);
|
|
|
|
|
|
|
|
SDValue inval;
|
|
|
|
if (MFI->isKernel())
|
|
|
|
inval = DAG.getNode(PTXISD::READ_PARAM, dl, VT, Chain,
|
|
|
|
DAG.getTargetConstant(i, MVT::i32));
|
|
|
|
else
|
|
|
|
inval = DAG.getCopyFromReg(Chain, dl, vreg, VT);
|
|
|
|
InVals.push_back(inval);
|
2010-10-19 13:14:40 +00:00
|
|
|
}
|
|
|
|
|
2011-02-10 12:01:24 +00:00
|
|
|
MFI->doneAddArg();
|
2010-11-08 03:00:52 +00:00
|
|
|
|
2010-09-18 18:52:28 +00:00
|
|
|
return Chain;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDValue PTXTargetLowering::
|
|
|
|
LowerReturn(SDValue Chain,
|
|
|
|
CallingConv::ID CallConv,
|
|
|
|
bool isVarArg,
|
|
|
|
const SmallVectorImpl<ISD::OutputArg> &Outs,
|
|
|
|
const SmallVectorImpl<SDValue> &OutVals,
|
|
|
|
DebugLoc dl,
|
|
|
|
SelectionDAG &DAG) const {
|
2010-10-19 13:14:40 +00:00
|
|
|
if (isVarArg) llvm_unreachable("PTX does not support varargs");
|
2010-09-25 07:46:17 +00:00
|
|
|
|
|
|
|
switch (CallConv) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unsupported calling convention.");
|
|
|
|
case CallingConv::PTX_Kernel:
|
|
|
|
assert(Outs.size() == 0 && "Kernel must return void.");
|
|
|
|
return DAG.getNode(PTXISD::EXIT, dl, MVT::Other, Chain);
|
|
|
|
case CallingConv::PTX_Device:
|
|
|
|
assert(Outs.size() <= 1 && "Can at most return one value.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PTX_Device
|
|
|
|
|
2010-10-19 13:14:40 +00:00
|
|
|
// return void
|
2010-09-25 07:46:17 +00:00
|
|
|
if (Outs.size() == 0)
|
|
|
|
return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain);
|
|
|
|
|
|
|
|
SDValue Flag;
|
2011-02-28 06:34:09 +00:00
|
|
|
unsigned reg;
|
|
|
|
|
2011-03-02 03:20:28 +00:00
|
|
|
if (Outs[0].VT == MVT::i16) {
|
|
|
|
reg = PTX::RH0;
|
|
|
|
}
|
|
|
|
else if (Outs[0].VT == MVT::i32) {
|
2011-02-28 06:34:09 +00:00
|
|
|
reg = PTX::R0;
|
|
|
|
}
|
2011-03-02 03:20:28 +00:00
|
|
|
else if (Outs[0].VT == MVT::i64) {
|
|
|
|
reg = PTX::RD0;
|
|
|
|
}
|
2011-02-28 06:34:09 +00:00
|
|
|
else if (Outs[0].VT == MVT::f32) {
|
|
|
|
reg = PTX::F0;
|
|
|
|
}
|
|
|
|
else {
|
2011-03-15 08:41:24 +00:00
|
|
|
assert(Outs[0].VT == MVT::f64 && "Can return only basic types");
|
|
|
|
reg = PTX::FD0;
|
2011-02-28 06:34:09 +00:00
|
|
|
}
|
2010-10-19 13:14:40 +00:00
|
|
|
|
2010-11-08 03:00:52 +00:00
|
|
|
MachineFunction &MF = DAG.getMachineFunction();
|
|
|
|
PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
|
|
|
|
MFI->setRetReg(reg);
|
|
|
|
|
2010-10-19 13:14:40 +00:00
|
|
|
// If this is the first return lowered for this function, add the regs to the
|
|
|
|
// liveout set for the function
|
|
|
|
if (DAG.getMachineFunction().getRegInfo().liveout_empty())
|
|
|
|
DAG.getMachineFunction().getRegInfo().addLiveOut(reg);
|
|
|
|
|
|
|
|
// Copy the result values into the output registers
|
|
|
|
Chain = DAG.getCopyToReg(Chain, dl, reg, OutVals[0], Flag);
|
|
|
|
|
|
|
|
// Guarantee that all emitted copies are stuck together,
|
|
|
|
// avoiding something bad
|
|
|
|
Flag = Chain.getValue(1);
|
|
|
|
|
2010-09-25 07:46:17 +00:00
|
|
|
return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain, Flag);
|
2010-09-18 18:52:28 +00:00
|
|
|
}
|