Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 1 | //===-- PTXISelLowering.cpp - PTX DAG Lowering Implementation -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the PTXTargetLowering class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 14 | #include "PTX.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 15 | #include "PTXISelLowering.h" |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 16 | #include "PTXMachineFunctionInfo.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 17 | #include "PTXRegisterInfo.h" |
| 18 | #include "llvm/Support/ErrorHandling.h" |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFunction.h" |
| 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/SelectionDAG.h" |
| 22 | #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | PTXTargetLowering::PTXTargetLowering(TargetMachine &TM) |
| 27 | : TargetLowering(TM, new TargetLoweringObjectFileELF()) { |
| 28 | // Set up the register classes. |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 29 | addRegisterClass(MVT::i1, PTX::PredsRegisterClass); |
| 30 | addRegisterClass(MVT::i32, PTX::RRegs32RegisterClass); |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 31 | addRegisterClass(MVT::f32, PTX::RRegf32RegisterClass); |
| 32 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 33 | setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand); |
| 34 | |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 35 | setOperationAction(ISD::ConstantFP, MVT::f32, Legal); |
| 36 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 37 | // Customize translation of memory addresses |
| 38 | setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); |
| 39 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 40 | // Compute derived properties from the register classes |
| 41 | computeRegisterProperties(); |
| 42 | } |
| 43 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 44 | SDValue PTXTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
| 45 | switch (Op.getOpcode()) { |
| 46 | default: llvm_unreachable("Unimplemented operand"); |
| 47 | case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); |
| 48 | } |
| 49 | } |
| 50 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 51 | const char *PTXTargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 52 | switch (Opcode) { |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 53 | default: |
| 54 | llvm_unreachable("Unknown opcode"); |
| 55 | case PTXISD::READ_PARAM: |
| 56 | return "PTXISD::READ_PARAM"; |
| 57 | case PTXISD::EXIT: |
| 58 | return "PTXISD::EXIT"; |
| 59 | case PTXISD::RET: |
| 60 | return "PTXISD::RET"; |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | //===----------------------------------------------------------------------===// |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 65 | // Custom Lower Operation |
| 66 | //===----------------------------------------------------------------------===// |
| 67 | |
| 68 | SDValue PTXTargetLowering:: |
| 69 | LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const { |
| 70 | EVT PtrVT = getPointerTy(); |
| 71 | DebugLoc dl = Op.getDebugLoc(); |
| 72 | const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
| 73 | return DAG.getTargetGlobalAddress(GV, dl, PtrVT); |
| 74 | } |
| 75 | |
| 76 | //===----------------------------------------------------------------------===// |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 77 | // Calling Convention Implementation |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | |
Benjamin Kramer | a3ac427 | 2010-10-22 17:35:07 +0000 | [diff] [blame] | 80 | namespace { |
| 81 | struct argmap_entry { |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 82 | MVT::SimpleValueType VT; |
| 83 | TargetRegisterClass *RC; |
| 84 | TargetRegisterClass::iterator loc; |
| 85 | |
| 86 | argmap_entry(MVT::SimpleValueType _VT, TargetRegisterClass *_RC) |
| 87 | : VT(_VT), RC(_RC), loc(_RC->begin()) {} |
| 88 | |
Benjamin Kramer | a3ac427 | 2010-10-22 17:35:07 +0000 | [diff] [blame] | 89 | void reset() { loc = RC->begin(); } |
| 90 | bool operator==(MVT::SimpleValueType _VT) const { return VT == _VT; } |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 91 | } argmap[] = { |
| 92 | argmap_entry(MVT::i1, PTX::PredsRegisterClass), |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 93 | argmap_entry(MVT::i32, PTX::RRegs32RegisterClass), |
| 94 | argmap_entry(MVT::f32, PTX::RRegf32RegisterClass) |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 95 | }; |
Benjamin Kramer | a3ac427 | 2010-10-22 17:35:07 +0000 | [diff] [blame] | 96 | } // end anonymous namespace |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 97 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 98 | SDValue PTXTargetLowering:: |
| 99 | LowerFormalArguments(SDValue Chain, |
| 100 | CallingConv::ID CallConv, |
| 101 | bool isVarArg, |
| 102 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 103 | DebugLoc dl, |
| 104 | SelectionDAG &DAG, |
| 105 | SmallVectorImpl<SDValue> &InVals) const { |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 106 | if (isVarArg) llvm_unreachable("PTX does not support varargs"); |
| 107 | |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 108 | MachineFunction &MF = DAG.getMachineFunction(); |
| 109 | PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>(); |
| 110 | |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 111 | switch (CallConv) { |
| 112 | default: |
| 113 | llvm_unreachable("Unsupported calling convention"); |
| 114 | break; |
| 115 | case CallingConv::PTX_Kernel: |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 116 | MFI->setKernel(true); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 117 | break; |
| 118 | case CallingConv::PTX_Device: |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 119 | MFI->setKernel(false); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 120 | break; |
| 121 | } |
| 122 | |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 123 | // Make sure we don't add argument registers twice |
| 124 | if (MFI->isDoneAddArg()) |
| 125 | llvm_unreachable("cannot add argument registers twice"); |
| 126 | |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 127 | // Reset argmap before allocation |
| 128 | for (struct argmap_entry *i = argmap, *e = argmap + array_lengthof(argmap); |
| 129 | i != e; ++ i) |
| 130 | i->reset(); |
| 131 | |
| 132 | for (int i = 0, e = Ins.size(); i != e; ++ i) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 133 | MVT::SimpleValueType VT = Ins[i].VT.SimpleTy; |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 134 | |
| 135 | struct argmap_entry *entry = std::find(argmap, |
| 136 | argmap + array_lengthof(argmap), VT); |
| 137 | if (entry == argmap + array_lengthof(argmap)) |
| 138 | llvm_unreachable("Type of argument is not supported"); |
| 139 | |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 140 | if (MFI->isKernel() && entry->RC == PTX::PredsRegisterClass) |
| 141 | llvm_unreachable("cannot pass preds to kernel"); |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 142 | |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 143 | MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo(); |
| 144 | |
| 145 | unsigned preg = *++(entry->loc); // allocate start from register 1 |
| 146 | unsigned vreg = RegInfo.createVirtualRegister(entry->RC); |
| 147 | RegInfo.addLiveIn(preg, vreg); |
| 148 | |
| 149 | MFI->addArgReg(preg); |
| 150 | |
| 151 | SDValue inval; |
| 152 | if (MFI->isKernel()) |
| 153 | inval = DAG.getNode(PTXISD::READ_PARAM, dl, VT, Chain, |
| 154 | DAG.getTargetConstant(i, MVT::i32)); |
| 155 | else |
| 156 | inval = DAG.getCopyFromReg(Chain, dl, vreg, VT); |
| 157 | InVals.push_back(inval); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 160 | MFI->doneAddArg(); |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 161 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 162 | return Chain; |
| 163 | } |
| 164 | |
| 165 | SDValue PTXTargetLowering:: |
| 166 | LowerReturn(SDValue Chain, |
| 167 | CallingConv::ID CallConv, |
| 168 | bool isVarArg, |
| 169 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 170 | const SmallVectorImpl<SDValue> &OutVals, |
| 171 | DebugLoc dl, |
| 172 | SelectionDAG &DAG) const { |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 173 | if (isVarArg) llvm_unreachable("PTX does not support varargs"); |
Che-Liang Chiou | f9930da | 2010-09-25 07:46:17 +0000 | [diff] [blame] | 174 | |
| 175 | switch (CallConv) { |
| 176 | default: |
| 177 | llvm_unreachable("Unsupported calling convention."); |
| 178 | case CallingConv::PTX_Kernel: |
| 179 | assert(Outs.size() == 0 && "Kernel must return void."); |
| 180 | return DAG.getNode(PTXISD::EXIT, dl, MVT::Other, Chain); |
| 181 | case CallingConv::PTX_Device: |
| 182 | assert(Outs.size() <= 1 && "Can at most return one value."); |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | // PTX_Device |
| 187 | |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 188 | // return void |
Che-Liang Chiou | f9930da | 2010-09-25 07:46:17 +0000 | [diff] [blame] | 189 | if (Outs.size() == 0) |
| 190 | return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain); |
| 191 | |
Che-Liang Chiou | f9930da | 2010-09-25 07:46:17 +0000 | [diff] [blame] | 192 | SDValue Flag; |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 193 | unsigned reg; |
| 194 | |
| 195 | if (Outs[0].VT == MVT::i32) { |
| 196 | reg = PTX::R0; |
| 197 | } |
| 198 | else if (Outs[0].VT == MVT::f32) { |
| 199 | reg = PTX::F0; |
| 200 | } |
| 201 | else { |
| 202 | assert(false && "Can return only basic types"); |
| 203 | } |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 204 | |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 205 | MachineFunction &MF = DAG.getMachineFunction(); |
| 206 | PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>(); |
| 207 | MFI->setRetReg(reg); |
| 208 | |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 209 | // If this is the first return lowered for this function, add the regs to the |
| 210 | // liveout set for the function |
| 211 | if (DAG.getMachineFunction().getRegInfo().liveout_empty()) |
| 212 | DAG.getMachineFunction().getRegInfo().addLiveOut(reg); |
| 213 | |
| 214 | // Copy the result values into the output registers |
| 215 | Chain = DAG.getCopyToReg(Chain, dl, reg, OutVals[0], Flag); |
| 216 | |
| 217 | // Guarantee that all emitted copies are stuck together, |
| 218 | // avoiding something bad |
| 219 | Flag = Chain.getValue(1); |
| 220 | |
Che-Liang Chiou | f9930da | 2010-09-25 07:46:17 +0000 | [diff] [blame] | 221 | return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain, Flag); |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 222 | } |