SDValue PTXTargetLowering:: LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool isVarArg, const SmallVectorImpl<ISD::InputArg> &Ins, DebugLoc dl, SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { if (isVarArg) llvm_unreachable("PTX does not support varargs"); MachineFunction &MF = DAG.getMachineFunction(); PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>(); switch (CallConv) { default: llvm_unreachable("Unsupported calling convention"); break; case CallingConv::PTX_Kernel: MFI->setKernel(true); break; case CallingConv::PTX_Device: MFI->setKernel(false); break; } if (MFI->isKernel()) { // For kernel functions, we just need to emit the proper READ_PARAM ISDs for (unsigned i = 0, e = Ins.size(); i != e; ++i) { assert(Ins[i].VT != MVT::i1 && "Kernels cannot take pred operands"); SDValue ArgValue = DAG.getNode(PTXISD::READ_PARAM, dl, Ins[i].VT, Chain, DAG.getTargetConstant(i, MVT::i32)); InVals.push_back(ArgValue); // 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()); } } return Chain; }
SDValue PTXTargetLowering:: LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool isVarArg, const SmallVectorImpl<ISD::InputArg> &Ins, DebugLoc dl, SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { if (isVarArg) llvm_unreachable("PTX does not support varargs"); MachineFunction &MF = DAG.getMachineFunction(); PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>(); switch (CallConv) { default: llvm_unreachable("Unsupported calling convention"); break; case CallingConv::PTX_Kernel: MFI->setKernel(true); break; case CallingConv::PTX_Device: MFI->setKernel(false); break; } // Make sure we don't add argument registers twice if (MFI->isDoneAddArg()) llvm_unreachable("cannot add argument registers twice"); // 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) { MVT::SimpleValueType VT = Ins[i].VT.SimpleTy; 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"); if (MFI->isKernel() && entry->RC == PTX::PredsRegisterClass) llvm_unreachable("cannot pass preds to kernel"); MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo(); 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); } MFI->doneAddArg(); return Chain; }
SDValue PTXTargetLowering:: LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool isVarArg, const SmallVectorImpl<ISD::InputArg> &Ins, DebugLoc dl, SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { if (isVarArg) llvm_unreachable("PTX does not support varargs"); MachineFunction &MF = DAG.getMachineFunction(); const PTXSubtarget& ST = getTargetMachine().getSubtarget<PTXSubtarget>(); PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>(); PTXParamManager &PM = MFI->getParamManager(); switch (CallConv) { default: llvm_unreachable("Unsupported calling convention"); break; case CallingConv::PTX_Kernel: MFI->setKernel(true); break; case CallingConv::PTX_Device: MFI->setKernel(false); break; } // We do one of two things here: // IsKernel || SM >= 2.0 -> Use param space for arguments // SM < 2.0 -> Use registers for arguments if (MFI->isKernel() || ST.useParamSpaceForDeviceArgs()) { // We just need to emit the proper LOAD_PARAM ISDs for (unsigned i = 0, e = Ins.size(); i != e; ++i) { assert((!MFI->isKernel() || Ins[i].VT != MVT::i1) && "Kernels cannot take pred operands"); unsigned ParamSize = Ins[i].VT.getStoreSizeInBits(); unsigned Param = PM.addArgumentParam(ParamSize); const std::string &ParamName = PM.getParamName(Param); SDValue ParamValue = DAG.getTargetExternalSymbol(ParamName.c_str(), MVT::Other); SDValue ArgValue = DAG.getNode(PTXISD::LOAD_PARAM, dl, Ins[i].VT, Chain, ParamValue); InVals.push_back(ArgValue); } } else { for (unsigned i = 0, e = Ins.size(); i != e; ++i) { EVT RegVT = Ins[i].VT; TargetRegisterClass* TRC = getRegClassFor(RegVT); // Use a unique index in the instruction to prevent instruction folding. // Yes, this is a hack. SDValue Index = DAG.getTargetConstant(i, MVT::i32); unsigned Reg = MF.getRegInfo().createVirtualRegister(TRC); SDValue ArgValue = DAG.getNode(PTXISD::READ_PARAM, dl, RegVT, Chain, Index); InVals.push_back(ArgValue); MFI->addArgReg(Reg); } } return Chain; }