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"
|
2011-06-23 18:10:03 +00:00
|
|
|
#include "PTXSubtarget.h"
|
2010-09-18 18:52:28 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2011-06-16 17:50:00 +00:00
|
|
|
#include "llvm/CodeGen/CallingConvLower.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;
|
|
|
|
|
2011-06-16 17:50:00 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Calling Convention Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "PTXGenCallingConv.inc"
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TargetLowering Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-09-18 18:52:28 +00:00
|
|
|
PTXTargetLowering::PTXTargetLowering(TargetMachine &TM)
|
|
|
|
: TargetLowering(TM, new TargetLoweringObjectFileELF()) {
|
|
|
|
// Set up the register classes.
|
2011-06-16 17:49:58 +00:00
|
|
|
addRegisterClass(MVT::i1, PTX::RegPredRegisterClass);
|
|
|
|
addRegisterClass(MVT::i16, PTX::RegI16RegisterClass);
|
|
|
|
addRegisterClass(MVT::i32, PTX::RegI32RegisterClass);
|
|
|
|
addRegisterClass(MVT::i64, PTX::RegI64RegisterClass);
|
|
|
|
addRegisterClass(MVT::f32, PTX::RegF32RegisterClass);
|
|
|
|
addRegisterClass(MVT::f64, PTX::RegF64RegisterClass);
|
2011-03-02 03:20:28 +00:00
|
|
|
|
2011-04-28 00:19:52 +00:00
|
|
|
setBooleanContents(ZeroOrOneBooleanContent);
|
2011-06-25 18:16:28 +00:00
|
|
|
setMinFunctionAlignment(2);
|
2011-06-24 19:27:10 +00:00
|
|
|
|
2011-06-25 18:16:28 +00:00
|
|
|
////////////////////////////////////
|
|
|
|
/////////// Expansion //////////////
|
|
|
|
////////////////////////////////////
|
2011-06-24 19:27:10 +00:00
|
|
|
|
2011-06-25 18:16:28 +00:00
|
|
|
// (any/zero/sign) extload => load + (any/zero/sign) extend
|
2011-06-24 19:27:10 +00:00
|
|
|
|
2011-04-28 00:19:52 +00:00
|
|
|
setLoadExtAction(ISD::EXTLOAD, MVT::i16, Expand);
|
|
|
|
setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Expand);
|
2011-06-24 19:27:10 +00:00
|
|
|
setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand);
|
2011-06-25 18:16:28 +00:00
|
|
|
|
|
|
|
// f32 extload => load + fextend
|
|
|
|
|
|
|
|
setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
|
|
|
|
|
|
|
|
// f64 truncstore => trunc + store
|
|
|
|
|
|
|
|
setTruncStoreAction(MVT::f64, MVT::f32, Expand);
|
|
|
|
|
|
|
|
// sign_extend_inreg => sign_extend
|
|
|
|
|
|
|
|
setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
|
|
|
|
|
|
|
|
// br_cc => brcond
|
|
|
|
|
2011-03-18 11:08:52 +00:00
|
|
|
setOperationAction(ISD::BR_CC, MVT::Other, Expand);
|
|
|
|
|
2011-06-25 18:16:28 +00:00
|
|
|
// select_cc => setcc
|
|
|
|
|
2011-04-28 00:19:56 +00:00
|
|
|
setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
|
|
|
|
setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
|
|
|
|
setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
|
2011-06-25 18:16:28 +00:00
|
|
|
|
|
|
|
////////////////////////////////////
|
|
|
|
//////////// Legal /////////////////
|
|
|
|
////////////////////////////////////
|
|
|
|
|
|
|
|
setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
|
|
|
|
setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
|
|
|
|
|
|
|
|
////////////////////////////////////
|
|
|
|
//////////// Custom ////////////////
|
|
|
|
////////////////////////////////////
|
|
|
|
|
|
|
|
// customise setcc to use bitwise logic if possible
|
|
|
|
|
2011-04-28 00:19:56 +00:00
|
|
|
setOperationAction(ISD::SETCC, MVT::i1, Custom);
|
2011-05-06 20:34:06 +00:00
|
|
|
|
2011-06-25 18:16:28 +00:00
|
|
|
// customize translation of memory addresses
|
|
|
|
|
|
|
|
setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
|
|
|
|
setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
|
2011-05-06 20:34:06 +00:00
|
|
|
|
2010-09-18 18:52:28 +00:00
|
|
|
// Compute derived properties from the register classes
|
|
|
|
computeRegisterProperties();
|
|
|
|
}
|
|
|
|
|
2011-04-28 00:19:56 +00:00
|
|
|
MVT::SimpleValueType PTXTargetLowering::getSetCCResultType(EVT VT) const {
|
|
|
|
return MVT::i1;
|
|
|
|
}
|
|
|
|
|
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");
|
2011-04-28 00:19:56 +00:00
|
|
|
case ISD::SETCC:
|
|
|
|
return LowerSETCC(Op, DAG);
|
2011-03-18 11:08:52 +00:00
|
|
|
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-06-23 18:10:05 +00:00
|
|
|
case PTXISD::LOAD_PARAM:
|
|
|
|
return "PTXISD::LOAD_PARAM";
|
2011-06-23 18:10:03 +00:00
|
|
|
case PTXISD::STORE_PARAM:
|
|
|
|
return "PTXISD::STORE_PARAM";
|
2011-02-10 12:01:24 +00:00
|
|
|
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
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-04-28 00:19:56 +00:00
|
|
|
SDValue PTXTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
|
|
|
|
assert(Op.getValueType() == MVT::i1 && "SetCC type must be 1-bit integer");
|
|
|
|
SDValue Op0 = Op.getOperand(0);
|
|
|
|
SDValue Op1 = Op.getOperand(1);
|
|
|
|
SDValue Op2 = Op.getOperand(2);
|
|
|
|
DebugLoc dl = Op.getDebugLoc();
|
|
|
|
ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
|
2011-06-16 15:17:11 +00:00
|
|
|
|
2011-04-28 00:19:56 +00:00
|
|
|
// Look for X == 0, X == 1, X != 0, or X != 1
|
|
|
|
// We can simplify these to bitwise logic
|
2011-06-16 15:17:11 +00:00
|
|
|
|
2011-04-28 00:19:56 +00:00
|
|
|
if (Op1.getOpcode() == ISD::Constant &&
|
|
|
|
(cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
|
|
|
|
cast<ConstantSDNode>(Op1)->isNullValue()) &&
|
|
|
|
(CC == ISD::SETEQ || CC == ISD::SETNE)) {
|
|
|
|
|
2011-06-16 15:17:11 +00:00
|
|
|
return DAG.getNode(ISD::AND, dl, MVT::i1, Op0, Op1);
|
2011-04-28 00:19:56 +00:00
|
|
|
}
|
2011-06-16 15:17:11 +00:00
|
|
|
|
2011-04-28 00:19:56 +00:00
|
|
|
return DAG.getNode(ISD::SETCC, dl, MVT::i1, Op0, Op1, Op2);
|
|
|
|
}
|
|
|
|
|
2010-12-22 10:38:51 +00:00
|
|
|
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
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
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();
|
2011-06-23 18:10:03 +00:00
|
|
|
const PTXSubtarget& ST = getTargetMachine().getSubtarget<PTXSubtarget>();
|
2010-11-08 03:00:52 +00:00
|
|
|
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-06-23 18:10:03 +00:00
|
|
|
// We do one of two things here:
|
|
|
|
// IsKernel || SM >= 2.0 -> Use param space for arguments
|
|
|
|
// SM < 2.0 -> Use registers for arguments
|
2011-06-24 16:27:49 +00:00
|
|
|
if (MFI->isKernel() || ST.useParamSpaceForDeviceArgs()) {
|
2011-06-23 18:10:05 +00:00
|
|
|
// We just need to emit the proper LOAD_PARAM ISDs
|
2011-06-16 17:50:00 +00:00
|
|
|
for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
|
2010-10-19 13:14:40 +00:00
|
|
|
|
2011-06-23 18:10:03 +00:00
|
|
|
assert((!MFI->isKernel() || Ins[i].VT != MVT::i1) &&
|
|
|
|
"Kernels cannot take pred operands");
|
2010-10-19 13:14:40 +00:00
|
|
|
|
2011-06-23 18:10:05 +00:00
|
|
|
SDValue ArgValue = DAG.getNode(PTXISD::LOAD_PARAM, dl, Ins[i].VT, Chain,
|
2011-06-16 17:50:00 +00:00
|
|
|
DAG.getTargetConstant(i, MVT::i32));
|
|
|
|
InVals.push_back(ArgValue);
|
2011-02-10 12:01:24 +00:00
|
|
|
|
2011-06-16 17:50:00 +00:00
|
|
|
// Instead of storing a physical register in our argument list, we just
|
|
|
|
// store the total size of the parameter, in bits. The ASM printer
|
|
|
|
// knows how to process this.
|
|
|
|
MFI->addArgReg(Ins[i].VT.getStoreSizeInBits());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// For device functions, we use the PTX calling convention to do register
|
|
|
|
// assignments then create CopyFromReg ISDs for the allocated registers
|
|
|
|
|
|
|
|
SmallVector<CCValAssign, 16> ArgLocs;
|
|
|
|
CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), ArgLocs,
|
|
|
|
*DAG.getContext());
|
|
|
|
|
|
|
|
CCInfo.AnalyzeFormalArguments(Ins, CC_PTX);
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
|
|
|
|
|
|
|
|
CCValAssign& VA = ArgLocs[i];
|
|
|
|
EVT RegVT = VA.getLocVT();
|
|
|
|
TargetRegisterClass* TRC = 0;
|
|
|
|
|
|
|
|
assert(VA.isRegLoc() && "CCValAssign must be RegLoc");
|
|
|
|
|
|
|
|
// Determine which register class we need
|
|
|
|
if (RegVT == MVT::i1) {
|
|
|
|
TRC = PTX::RegPredRegisterClass;
|
|
|
|
}
|
|
|
|
else if (RegVT == MVT::i16) {
|
|
|
|
TRC = PTX::RegI16RegisterClass;
|
|
|
|
}
|
|
|
|
else if (RegVT == MVT::i32) {
|
|
|
|
TRC = PTX::RegI32RegisterClass;
|
|
|
|
}
|
|
|
|
else if (RegVT == MVT::i64) {
|
|
|
|
TRC = PTX::RegI64RegisterClass;
|
|
|
|
}
|
|
|
|
else if (RegVT == MVT::f32) {
|
|
|
|
TRC = PTX::RegF32RegisterClass;
|
|
|
|
}
|
|
|
|
else if (RegVT == MVT::f64) {
|
|
|
|
TRC = PTX::RegF64RegisterClass;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
llvm_unreachable("Unknown parameter type");
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Reg = MF.getRegInfo().createVirtualRegister(TRC);
|
|
|
|
MF.getRegInfo().addLiveIn(VA.getLocReg(), Reg);
|
|
|
|
|
|
|
|
SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
|
|
|
|
InVals.push_back(ArgValue);
|
|
|
|
|
|
|
|
MFI->addArgReg(VA.getLocReg());
|
|
|
|
}
|
2010-10-19 13:14:40 +00:00
|
|
|
}
|
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:
|
2011-06-16 17:50:00 +00:00
|
|
|
//assert(Outs.size() <= 1 && "Can at most return one value.");
|
2010-09-25 07:46:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-06-16 17:50:00 +00:00
|
|
|
MachineFunction& MF = DAG.getMachineFunction();
|
|
|
|
PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
|
2010-09-25 07:46:17 +00:00
|
|
|
|
|
|
|
SDValue Flag;
|
2011-02-28 06:34:09 +00:00
|
|
|
|
2011-06-23 18:10:13 +00:00
|
|
|
// Even though we could use the .param space for return arguments for
|
|
|
|
// device functions if SM >= 2.0 and the number of return arguments is
|
|
|
|
// only 1, we just always use registers since this makes the codegen
|
|
|
|
// easier.
|
|
|
|
SmallVector<CCValAssign, 16> RVLocs;
|
|
|
|
CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
|
|
|
|
getTargetMachine(), RVLocs, *DAG.getContext());
|
2010-10-19 13:14:40 +00:00
|
|
|
|
2011-06-23 18:10:13 +00:00
|
|
|
CCInfo.AnalyzeReturn(Outs, RetCC_PTX);
|
2011-06-16 17:50:00 +00:00
|
|
|
|
2011-06-23 18:10:13 +00:00
|
|
|
for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
|
|
|
|
CCValAssign& VA = RVLocs[i];
|
2010-11-08 03:00:52 +00:00
|
|
|
|
2011-06-23 18:10:13 +00:00
|
|
|
assert(VA.isRegLoc() && "CCValAssign must be RegLoc");
|
2010-10-19 13:14:40 +00:00
|
|
|
|
2011-06-23 18:10:13 +00:00
|
|
|
unsigned Reg = VA.getLocReg();
|
2010-10-19 13:14:40 +00:00
|
|
|
|
2011-06-23 18:10:13 +00:00
|
|
|
DAG.getMachineFunction().getRegInfo().addLiveOut(Reg);
|
2010-10-19 13:14:40 +00:00
|
|
|
|
2011-06-23 18:10:13 +00:00
|
|
|
Chain = DAG.getCopyToReg(Chain, dl, Reg, OutVals[i], Flag);
|
2011-06-16 17:50:00 +00:00
|
|
|
|
2011-06-23 18:10:13 +00:00
|
|
|
// Guarantee that all emitted copies are stuck together,
|
|
|
|
// avoiding something bad
|
|
|
|
Flag = Chain.getValue(1);
|
2011-06-16 17:50:00 +00:00
|
|
|
|
2011-06-23 18:10:13 +00:00
|
|
|
MFI->addRetReg(Reg);
|
2011-06-16 17:50:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Flag.getNode() == 0) {
|
|
|
|
return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain, Flag);
|
|
|
|
}
|
2010-09-18 18:52:28 +00:00
|
|
|
}
|