blob: d4cc31b1a2701ae97373698e918b812be91bb312 [file] [log] [blame]
Justin Holewinskiae556d32012-05-04 20:18:50 +00001//
2// The LLVM Compiler Infrastructure
3//
4// This file is distributed under the University of Illinois Open Source
5// License. See LICENSE.TXT for details.
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the interfaces that NVPTX uses to lower LLVM code into a
10// selection DAG.
11//
12//===----------------------------------------------------------------------===//
13
Justin Holewinskiae556d32012-05-04 20:18:50 +000014#include "NVPTXISelLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "NVPTX.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000016#include "NVPTXTargetMachine.h"
17#include "NVPTXTargetObjectFile.h"
18#include "NVPTXUtilities.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000019#include "llvm/CodeGen/Analysis.h"
20#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000024#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/DerivedTypes.h"
26#include "llvm/IR/Function.h"
27#include "llvm/IR/GlobalValue.h"
28#include "llvm/IR/IntrinsicInst.h"
29#include "llvm/IR/Intrinsics.h"
30#include "llvm/IR/Module.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000031#include "llvm/MC/MCSectionELF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/CallSite.h"
33#include "llvm/Support/CommandLine.h"
34#include "llvm/Support/Debug.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/raw_ostream.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000037#include <sstream>
38
39#undef DEBUG_TYPE
40#define DEBUG_TYPE "nvptx-lower"
41
42using namespace llvm;
43
44static unsigned int uniqueCallSite = 0;
45
Justin Holewinski0497ab12013-03-30 14:29:21 +000046static cl::opt<bool> sched4reg(
47 "nvptx-sched4reg",
48 cl::desc("NVPTX Specific: schedule for register pressue"), cl::init(false));
Justin Holewinskiae556d32012-05-04 20:18:50 +000049
Justin Holewinskibe8dc642013-02-12 14:18:49 +000050static bool IsPTXVectorType(MVT VT) {
51 switch (VT.SimpleTy) {
Justin Holewinski0497ab12013-03-30 14:29:21 +000052 default:
53 return false;
Justin Holewinskif8f70912013-06-28 17:57:59 +000054 case MVT::v2i1:
55 case MVT::v4i1:
Justin Holewinskibe8dc642013-02-12 14:18:49 +000056 case MVT::v2i8:
57 case MVT::v4i8:
58 case MVT::v2i16:
59 case MVT::v4i16:
60 case MVT::v2i32:
61 case MVT::v4i32:
62 case MVT::v2i64:
63 case MVT::v2f32:
64 case MVT::v4f32:
65 case MVT::v2f64:
Justin Holewinski0497ab12013-03-30 14:29:21 +000066 return true;
Justin Holewinskibe8dc642013-02-12 14:18:49 +000067 }
68}
69
Justin Holewinskif8f70912013-06-28 17:57:59 +000070/// ComputePTXValueVTs - For the given Type \p Ty, returns the set of primitive
71/// EVTs that compose it. Unlike ComputeValueVTs, this will break apart vectors
72/// into their primitive components.
73/// NOTE: This is a band-aid for code that expects ComputeValueVTs to return the
74/// same number of types as the Ins/Outs arrays in LowerFormalArguments,
75/// LowerCall, and LowerReturn.
76static void ComputePTXValueVTs(const TargetLowering &TLI, Type *Ty,
77 SmallVectorImpl<EVT> &ValueVTs,
78 SmallVectorImpl<uint64_t> *Offsets = 0,
79 uint64_t StartingOffset = 0) {
80 SmallVector<EVT, 16> TempVTs;
81 SmallVector<uint64_t, 16> TempOffsets;
82
83 ComputeValueVTs(TLI, Ty, TempVTs, &TempOffsets, StartingOffset);
84 for (unsigned i = 0, e = TempVTs.size(); i != e; ++i) {
85 EVT VT = TempVTs[i];
86 uint64_t Off = TempOffsets[i];
87 if (VT.isVector())
88 for (unsigned j = 0, je = VT.getVectorNumElements(); j != je; ++j) {
89 ValueVTs.push_back(VT.getVectorElementType());
90 if (Offsets)
91 Offsets->push_back(Off+j*VT.getVectorElementType().getStoreSize());
92 }
93 else {
94 ValueVTs.push_back(VT);
95 if (Offsets)
96 Offsets->push_back(Off);
97 }
98 }
99}
100
Justin Holewinskiae556d32012-05-04 20:18:50 +0000101// NVPTXTargetLowering Constructor.
102NVPTXTargetLowering::NVPTXTargetLowering(NVPTXTargetMachine &TM)
Justin Holewinski0497ab12013-03-30 14:29:21 +0000103 : TargetLowering(TM, new NVPTXTargetObjectFile()), nvTM(&TM),
104 nvptxSubtarget(TM.getSubtarget<NVPTXSubtarget>()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000105
106 // always lower memset, memcpy, and memmove intrinsics to load/store
107 // instructions, rather
108 // then generating calls to memset, mempcy or memmove.
Justin Holewinski0497ab12013-03-30 14:29:21 +0000109 MaxStoresPerMemset = (unsigned) 0xFFFFFFFF;
110 MaxStoresPerMemcpy = (unsigned) 0xFFFFFFFF;
111 MaxStoresPerMemmove = (unsigned) 0xFFFFFFFF;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000112
113 setBooleanContents(ZeroOrNegativeOneBooleanContent);
114
115 // Jump is Expensive. Don't create extra control flow for 'and', 'or'
116 // condition branches.
117 setJumpIsExpensive(true);
118
119 // By default, use the Source scheduling
120 if (sched4reg)
121 setSchedulingPreference(Sched::RegPressure);
122 else
123 setSchedulingPreference(Sched::Source);
124
125 addRegisterClass(MVT::i1, &NVPTX::Int1RegsRegClass);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000126 addRegisterClass(MVT::i16, &NVPTX::Int16RegsRegClass);
127 addRegisterClass(MVT::i32, &NVPTX::Int32RegsRegClass);
128 addRegisterClass(MVT::i64, &NVPTX::Int64RegsRegClass);
129 addRegisterClass(MVT::f32, &NVPTX::Float32RegsRegClass);
130 addRegisterClass(MVT::f64, &NVPTX::Float64RegsRegClass);
131
Justin Holewinskiae556d32012-05-04 20:18:50 +0000132 // Operations not directly supported by NVPTX.
Justin Holewinski0497ab12013-03-30 14:29:21 +0000133 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
134 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
135 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
136 setOperationAction(ISD::BR_CC, MVT::i1, Expand);
137 setOperationAction(ISD::BR_CC, MVT::i8, Expand);
138 setOperationAction(ISD::BR_CC, MVT::i16, Expand);
139 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
140 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Justin Holewinski318c6252013-07-01 12:58:56 +0000141 // Some SIGN_EXTEND_INREG can be done using cvt instruction.
142 // For others we will expand to a SHL/SRA pair.
143 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i64, Legal);
144 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
145 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
146 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000147 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000148
149 if (nvptxSubtarget.hasROT64()) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000150 setOperationAction(ISD::ROTL, MVT::i64, Legal);
151 setOperationAction(ISD::ROTR, MVT::i64, Legal);
152 } else {
153 setOperationAction(ISD::ROTL, MVT::i64, Expand);
154 setOperationAction(ISD::ROTR, MVT::i64, Expand);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000155 }
156 if (nvptxSubtarget.hasROT32()) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000157 setOperationAction(ISD::ROTL, MVT::i32, Legal);
158 setOperationAction(ISD::ROTR, MVT::i32, Legal);
159 } else {
160 setOperationAction(ISD::ROTL, MVT::i32, Expand);
161 setOperationAction(ISD::ROTR, MVT::i32, Expand);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000162 }
163
Justin Holewinski0497ab12013-03-30 14:29:21 +0000164 setOperationAction(ISD::ROTL, MVT::i16, Expand);
165 setOperationAction(ISD::ROTR, MVT::i16, Expand);
166 setOperationAction(ISD::ROTL, MVT::i8, Expand);
167 setOperationAction(ISD::ROTR, MVT::i8, Expand);
168 setOperationAction(ISD::BSWAP, MVT::i16, Expand);
169 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
170 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000171
172 // Indirect branch is not supported.
173 // This also disables Jump Table creation.
Justin Holewinski0497ab12013-03-30 14:29:21 +0000174 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
175 setOperationAction(ISD::BRIND, MVT::Other, Expand);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000176
Justin Holewinski0497ab12013-03-30 14:29:21 +0000177 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
178 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000179
180 // We want to legalize constant related memmove and memcopy
181 // intrinsics.
182 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
183
184 // Turn FP extload into load/fextend
185 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
186 // Turn FP truncstore into trunc + store.
187 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
188
189 // PTX does not support load / store predicate registers
Justin Holewinskic6462aa2012-11-14 19:19:16 +0000190 setOperationAction(ISD::LOAD, MVT::i1, Custom);
191 setOperationAction(ISD::STORE, MVT::i1, Custom);
192
Justin Holewinskiae556d32012-05-04 20:18:50 +0000193 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
194 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000195 setTruncStoreAction(MVT::i64, MVT::i1, Expand);
196 setTruncStoreAction(MVT::i32, MVT::i1, Expand);
197 setTruncStoreAction(MVT::i16, MVT::i1, Expand);
198 setTruncStoreAction(MVT::i8, MVT::i1, Expand);
199
200 // This is legal in NVPTX
Justin Holewinski0497ab12013-03-30 14:29:21 +0000201 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
202 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000203
204 // TRAP can be lowered to PTX trap
Justin Holewinski0497ab12013-03-30 14:29:21 +0000205 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000206
Justin Holewinski51cb1342013-07-01 12:59:04 +0000207 setOperationAction(ISD::ADDC, MVT::i64, Expand);
208 setOperationAction(ISD::ADDE, MVT::i64, Expand);
209
Justin Holewinskibe8dc642013-02-12 14:18:49 +0000210 // Register custom handling for vector loads/stores
Justin Holewinski0497ab12013-03-30 14:29:21 +0000211 for (int i = MVT::FIRST_VECTOR_VALUETYPE; i <= MVT::LAST_VECTOR_VALUETYPE;
212 ++i) {
213 MVT VT = (MVT::SimpleValueType) i;
Justin Holewinskibe8dc642013-02-12 14:18:49 +0000214 if (IsPTXVectorType(VT)) {
215 setOperationAction(ISD::LOAD, VT, Custom);
216 setOperationAction(ISD::STORE, VT, Custom);
217 setOperationAction(ISD::INTRINSIC_W_CHAIN, VT, Custom);
218 }
219 }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000220
Justin Holewinskif8f70912013-06-28 17:57:59 +0000221 // Custom handling for i8 intrinsics
222 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i8, Custom);
223
Justin Holewinskidc372df2013-06-28 17:58:07 +0000224 setOperationAction(ISD::CTLZ, MVT::i16, Legal);
225 setOperationAction(ISD::CTLZ, MVT::i32, Legal);
226 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
227 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Legal);
228 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Legal);
229 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Legal);
230 setOperationAction(ISD::CTTZ, MVT::i16, Expand);
231 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
232 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
233 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Expand);
234 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
235 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
236 setOperationAction(ISD::CTPOP, MVT::i16, Legal);
237 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
238 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
239
Justin Holewinskiae556d32012-05-04 20:18:50 +0000240 // Now deduce the information based on the above mentioned
241 // actions
242 computeRegisterProperties();
243}
244
Justin Holewinskiae556d32012-05-04 20:18:50 +0000245const char *NVPTXTargetLowering::getTargetNodeName(unsigned Opcode) const {
246 switch (Opcode) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000247 default:
248 return 0;
249 case NVPTXISD::CALL:
250 return "NVPTXISD::CALL";
251 case NVPTXISD::RET_FLAG:
252 return "NVPTXISD::RET_FLAG";
253 case NVPTXISD::Wrapper:
254 return "NVPTXISD::Wrapper";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000255 case NVPTXISD::DeclareParam:
256 return "NVPTXISD::DeclareParam";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000257 case NVPTXISD::DeclareScalarParam:
258 return "NVPTXISD::DeclareScalarParam";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000259 case NVPTXISD::DeclareRet:
260 return "NVPTXISD::DeclareRet";
261 case NVPTXISD::DeclareRetParam:
262 return "NVPTXISD::DeclareRetParam";
263 case NVPTXISD::PrintCall:
264 return "NVPTXISD::PrintCall";
265 case NVPTXISD::LoadParam:
266 return "NVPTXISD::LoadParam";
Justin Holewinskife44314f2013-06-28 17:57:51 +0000267 case NVPTXISD::LoadParamV2:
268 return "NVPTXISD::LoadParamV2";
269 case NVPTXISD::LoadParamV4:
270 return "NVPTXISD::LoadParamV4";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000271 case NVPTXISD::StoreParam:
272 return "NVPTXISD::StoreParam";
Justin Holewinskife44314f2013-06-28 17:57:51 +0000273 case NVPTXISD::StoreParamV2:
274 return "NVPTXISD::StoreParamV2";
275 case NVPTXISD::StoreParamV4:
276 return "NVPTXISD::StoreParamV4";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000277 case NVPTXISD::StoreParamS32:
278 return "NVPTXISD::StoreParamS32";
279 case NVPTXISD::StoreParamU32:
280 return "NVPTXISD::StoreParamU32";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000281 case NVPTXISD::CallArgBegin:
282 return "NVPTXISD::CallArgBegin";
283 case NVPTXISD::CallArg:
284 return "NVPTXISD::CallArg";
285 case NVPTXISD::LastCallArg:
286 return "NVPTXISD::LastCallArg";
287 case NVPTXISD::CallArgEnd:
288 return "NVPTXISD::CallArgEnd";
289 case NVPTXISD::CallVoid:
290 return "NVPTXISD::CallVoid";
291 case NVPTXISD::CallVal:
292 return "NVPTXISD::CallVal";
293 case NVPTXISD::CallSymbol:
294 return "NVPTXISD::CallSymbol";
295 case NVPTXISD::Prototype:
296 return "NVPTXISD::Prototype";
297 case NVPTXISD::MoveParam:
298 return "NVPTXISD::MoveParam";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000299 case NVPTXISD::StoreRetval:
300 return "NVPTXISD::StoreRetval";
Justin Holewinskife44314f2013-06-28 17:57:51 +0000301 case NVPTXISD::StoreRetvalV2:
302 return "NVPTXISD::StoreRetvalV2";
303 case NVPTXISD::StoreRetvalV4:
304 return "NVPTXISD::StoreRetvalV4";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000305 case NVPTXISD::PseudoUseParam:
306 return "NVPTXISD::PseudoUseParam";
307 case NVPTXISD::RETURN:
308 return "NVPTXISD::RETURN";
309 case NVPTXISD::CallSeqBegin:
310 return "NVPTXISD::CallSeqBegin";
311 case NVPTXISD::CallSeqEnd:
312 return "NVPTXISD::CallSeqEnd";
313 case NVPTXISD::LoadV2:
314 return "NVPTXISD::LoadV2";
315 case NVPTXISD::LoadV4:
316 return "NVPTXISD::LoadV4";
317 case NVPTXISD::LDGV2:
318 return "NVPTXISD::LDGV2";
319 case NVPTXISD::LDGV4:
320 return "NVPTXISD::LDGV4";
321 case NVPTXISD::LDUV2:
322 return "NVPTXISD::LDUV2";
323 case NVPTXISD::LDUV4:
324 return "NVPTXISD::LDUV4";
325 case NVPTXISD::StoreV2:
326 return "NVPTXISD::StoreV2";
327 case NVPTXISD::StoreV4:
328 return "NVPTXISD::StoreV4";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000329 }
330}
331
Justin Holewinskibc451192012-11-29 14:26:24 +0000332bool NVPTXTargetLowering::shouldSplitVectorElementType(EVT VT) const {
333 return VT == MVT::i1;
334}
Justin Holewinskiae556d32012-05-04 20:18:50 +0000335
336SDValue
337NVPTXTargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000338 SDLoc dl(Op);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000339 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
340 Op = DAG.getTargetGlobalAddress(GV, dl, getPointerTy());
341 return DAG.getNode(NVPTXISD::Wrapper, dl, getPointerTy(), Op);
342}
343
Justin Holewinskif8f70912013-06-28 17:57:59 +0000344std::string
345NVPTXTargetLowering::getPrototype(Type *retTy, const ArgListTy &Args,
346 const SmallVectorImpl<ISD::OutputArg> &Outs,
347 unsigned retAlignment,
348 const ImmutableCallSite *CS) const {
349
350 bool isABI = (nvptxSubtarget.getSmVersion() >= 20);
351 assert(isABI && "Non-ABI compilation is not supported");
352 if (!isABI)
353 return "";
354
355 std::stringstream O;
356 O << "prototype_" << uniqueCallSite << " : .callprototype ";
357
358 if (retTy->getTypeID() == Type::VoidTyID) {
359 O << "()";
360 } else {
361 O << "(";
362 if (retTy->isPrimitiveType() || retTy->isIntegerTy()) {
363 unsigned size = 0;
364 if (const IntegerType *ITy = dyn_cast<IntegerType>(retTy)) {
365 size = ITy->getBitWidth();
366 if (size < 32)
367 size = 32;
368 } else {
369 assert(retTy->isFloatingPointTy() &&
370 "Floating point type expected here");
371 size = retTy->getPrimitiveSizeInBits();
372 }
373
374 O << ".param .b" << size << " _";
375 } else if (isa<PointerType>(retTy)) {
376 O << ".param .b" << getPointerTy().getSizeInBits() << " _";
377 } else {
378 if ((retTy->getTypeID() == Type::StructTyID) || isa<VectorType>(retTy)) {
379 SmallVector<EVT, 16> vtparts;
380 ComputeValueVTs(*this, retTy, vtparts);
381 unsigned totalsz = 0;
382 for (unsigned i = 0, e = vtparts.size(); i != e; ++i) {
383 unsigned elems = 1;
384 EVT elemtype = vtparts[i];
385 if (vtparts[i].isVector()) {
386 elems = vtparts[i].getVectorNumElements();
387 elemtype = vtparts[i].getVectorElementType();
388 }
389 // TODO: no need to loop
390 for (unsigned j = 0, je = elems; j != je; ++j) {
391 unsigned sz = elemtype.getSizeInBits();
392 if (elemtype.isInteger() && (sz < 8))
393 sz = 8;
394 totalsz += sz / 8;
395 }
396 }
397 O << ".param .align " << retAlignment << " .b8 _[" << totalsz << "]";
398 } else {
399 assert(false && "Unknown return type");
400 }
401 }
402 O << ") ";
403 }
404 O << "_ (";
405
406 bool first = true;
407 MVT thePointerTy = getPointerTy();
408
409 unsigned OIdx = 0;
410 for (unsigned i = 0, e = Args.size(); i != e; ++i, ++OIdx) {
411 Type *Ty = Args[i].Ty;
412 if (!first) {
413 O << ", ";
414 }
415 first = false;
416
417 if (Outs[OIdx].Flags.isByVal() == false) {
418 if (Ty->isAggregateType() || Ty->isVectorTy()) {
419 unsigned align = 0;
420 const CallInst *CallI = cast<CallInst>(CS->getInstruction());
421 const DataLayout *TD = getDataLayout();
422 // +1 because index 0 is reserved for return type alignment
423 if (!llvm::getAlign(*CallI, i + 1, align))
424 align = TD->getABITypeAlignment(Ty);
425 unsigned sz = TD->getTypeAllocSize(Ty);
426 O << ".param .align " << align << " .b8 ";
427 O << "_";
428 O << "[" << sz << "]";
429 // update the index for Outs
430 SmallVector<EVT, 16> vtparts;
431 ComputeValueVTs(*this, Ty, vtparts);
432 if (unsigned len = vtparts.size())
433 OIdx += len - 1;
434 continue;
435 }
Justin Holewinskidff28d22013-07-01 12:59:01 +0000436 // i8 types in IR will be i16 types in SDAG
437 assert((getValueType(Ty) == Outs[OIdx].VT ||
438 (getValueType(Ty) == MVT::i8 && Outs[OIdx].VT == MVT::i16)) &&
Justin Holewinskif8f70912013-06-28 17:57:59 +0000439 "type mismatch between callee prototype and arguments");
440 // scalar type
441 unsigned sz = 0;
442 if (isa<IntegerType>(Ty)) {
443 sz = cast<IntegerType>(Ty)->getBitWidth();
444 if (sz < 32)
445 sz = 32;
446 } else if (isa<PointerType>(Ty))
447 sz = thePointerTy.getSizeInBits();
448 else
449 sz = Ty->getPrimitiveSizeInBits();
450 O << ".param .b" << sz << " ";
451 O << "_";
452 continue;
453 }
454 const PointerType *PTy = dyn_cast<PointerType>(Ty);
455 assert(PTy && "Param with byval attribute should be a pointer type");
456 Type *ETy = PTy->getElementType();
457
458 unsigned align = Outs[OIdx].Flags.getByValAlign();
459 unsigned sz = getDataLayout()->getTypeAllocSize(ETy);
460 O << ".param .align " << align << " .b8 ";
461 O << "_";
462 O << "[" << sz << "]";
463 }
464 O << ");";
465 return O.str();
466}
467
468unsigned
469NVPTXTargetLowering::getArgumentAlignment(SDValue Callee,
470 const ImmutableCallSite *CS,
471 Type *Ty,
472 unsigned Idx) const {
473 const DataLayout *TD = getDataLayout();
474 unsigned align = 0;
475 GlobalAddressSDNode *Func = dyn_cast<GlobalAddressSDNode>(Callee.getNode());
476
477 if (Func) { // direct call
478 assert(CS->getCalledFunction() &&
479 "direct call cannot find callee");
480 if (!llvm::getAlign(*(CS->getCalledFunction()), Idx, align))
481 align = TD->getABITypeAlignment(Ty);
482 }
483 else { // indirect call
484 const CallInst *CallI = dyn_cast<CallInst>(CS->getInstruction());
485 if (!llvm::getAlign(*CallI, Idx, align))
486 align = TD->getABITypeAlignment(Ty);
487 }
488
489 return align;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000490}
491
Justin Holewinski0497ab12013-03-30 14:29:21 +0000492SDValue NVPTXTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
493 SmallVectorImpl<SDValue> &InVals) const {
494 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000495 SDLoc dl = CLI.DL;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000496 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000497 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
498 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
499 SDValue Chain = CLI.Chain;
500 SDValue Callee = CLI.Callee;
501 bool &isTailCall = CLI.IsTailCall;
502 ArgListTy &Args = CLI.Args;
503 Type *retTy = CLI.RetTy;
504 ImmutableCallSite *CS = CLI.CS;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000505
Justin Holewinskiae556d32012-05-04 20:18:50 +0000506 bool isABI = (nvptxSubtarget.getSmVersion() >= 20);
Justin Holewinskif8f70912013-06-28 17:57:59 +0000507 assert(isABI && "Non-ABI compilation is not supported");
508 if (!isABI)
509 return Chain;
510 const DataLayout *TD = getDataLayout();
511 MachineFunction &MF = DAG.getMachineFunction();
512 const Function *F = MF.getFunction();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000513
514 SDValue tempChain = Chain;
Justin Holewinskif8f70912013-06-28 17:57:59 +0000515 Chain =
516 DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(uniqueCallSite, true),
517 dl);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000518 SDValue InFlag = Chain.getValue(1);
519
Justin Holewinskiae556d32012-05-04 20:18:50 +0000520 unsigned paramCount = 0;
Justin Holewinskif8f70912013-06-28 17:57:59 +0000521 // Args.size() and Outs.size() need not match.
522 // Outs.size() will be larger
523 // * if there is an aggregate argument with multiple fields (each field
524 // showing up separately in Outs)
525 // * if there is a vector argument with more than typical vector-length
526 // elements (generally if more than 4) where each vector element is
527 // individually present in Outs.
528 // So a different index should be used for indexing into Outs/OutVals.
529 // See similar issue in LowerFormalArguments.
530 unsigned OIdx = 0;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000531 // Declare the .params or .reg need to pass values
532 // to the function
Justin Holewinskif8f70912013-06-28 17:57:59 +0000533 for (unsigned i = 0, e = Args.size(); i != e; ++i, ++OIdx) {
534 EVT VT = Outs[OIdx].VT;
535 Type *Ty = Args[i].Ty;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000536
Justin Holewinskif8f70912013-06-28 17:57:59 +0000537 if (Outs[OIdx].Flags.isByVal() == false) {
538 if (Ty->isAggregateType()) {
539 // aggregate
540 SmallVector<EVT, 16> vtparts;
541 ComputeValueVTs(*this, Ty, vtparts);
542
543 unsigned align = getArgumentAlignment(Callee, CS, Ty, paramCount + 1);
544 // declare .param .align <align> .b8 .param<n>[<size>];
545 unsigned sz = TD->getTypeAllocSize(Ty);
546 SDVTList DeclareParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
547 SDValue DeclareParamOps[] = { Chain, DAG.getConstant(align, MVT::i32),
548 DAG.getConstant(paramCount, MVT::i32),
549 DAG.getConstant(sz, MVT::i32), InFlag };
550 Chain = DAG.getNode(NVPTXISD::DeclareParam, dl, DeclareParamVTs,
551 DeclareParamOps, 5);
552 InFlag = Chain.getValue(1);
553 unsigned curOffset = 0;
554 for (unsigned j = 0, je = vtparts.size(); j != je; ++j) {
555 unsigned elems = 1;
556 EVT elemtype = vtparts[j];
557 if (vtparts[j].isVector()) {
558 elems = vtparts[j].getVectorNumElements();
559 elemtype = vtparts[j].getVectorElementType();
560 }
561 for (unsigned k = 0, ke = elems; k != ke; ++k) {
562 unsigned sz = elemtype.getSizeInBits();
563 if (elemtype.isInteger() && (sz < 8))
564 sz = 8;
565 SDValue StVal = OutVals[OIdx];
566 if (elemtype.getSizeInBits() < 16) {
Justin Holewinskia2911282013-07-01 12:58:58 +0000567 StVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i16, StVal);
Justin Holewinskif8f70912013-06-28 17:57:59 +0000568 }
569 SDVTList CopyParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
570 SDValue CopyParamOps[] = { Chain,
571 DAG.getConstant(paramCount, MVT::i32),
572 DAG.getConstant(curOffset, MVT::i32),
573 StVal, InFlag };
574 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreParam, dl,
575 CopyParamVTs, &CopyParamOps[0], 5,
576 elemtype, MachinePointerInfo());
577 InFlag = Chain.getValue(1);
578 curOffset += sz / 8;
579 ++OIdx;
580 }
581 }
582 if (vtparts.size() > 0)
583 --OIdx;
584 ++paramCount;
585 continue;
586 }
587 if (Ty->isVectorTy()) {
588 EVT ObjectVT = getValueType(Ty);
589 unsigned align = getArgumentAlignment(Callee, CS, Ty, paramCount + 1);
590 // declare .param .align <align> .b8 .param<n>[<size>];
591 unsigned sz = TD->getTypeAllocSize(Ty);
592 SDVTList DeclareParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
593 SDValue DeclareParamOps[] = { Chain, DAG.getConstant(align, MVT::i32),
594 DAG.getConstant(paramCount, MVT::i32),
595 DAG.getConstant(sz, MVT::i32), InFlag };
596 Chain = DAG.getNode(NVPTXISD::DeclareParam, dl, DeclareParamVTs,
597 DeclareParamOps, 5);
598 InFlag = Chain.getValue(1);
599 unsigned NumElts = ObjectVT.getVectorNumElements();
600 EVT EltVT = ObjectVT.getVectorElementType();
601 EVT MemVT = EltVT;
602 bool NeedExtend = false;
603 if (EltVT.getSizeInBits() < 16) {
604 NeedExtend = true;
605 EltVT = MVT::i16;
606 }
607
608 // V1 store
609 if (NumElts == 1) {
610 SDValue Elt = OutVals[OIdx++];
611 if (NeedExtend)
612 Elt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Elt);
613
614 SDVTList CopyParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
615 SDValue CopyParamOps[] = { Chain,
616 DAG.getConstant(paramCount, MVT::i32),
617 DAG.getConstant(0, MVT::i32), Elt,
618 InFlag };
619 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreParam, dl,
620 CopyParamVTs, &CopyParamOps[0], 5,
621 MemVT, MachinePointerInfo());
622 InFlag = Chain.getValue(1);
623 } else if (NumElts == 2) {
624 SDValue Elt0 = OutVals[OIdx++];
625 SDValue Elt1 = OutVals[OIdx++];
626 if (NeedExtend) {
627 Elt0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Elt0);
628 Elt1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Elt1);
629 }
630
631 SDVTList CopyParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
632 SDValue CopyParamOps[] = { Chain,
633 DAG.getConstant(paramCount, MVT::i32),
634 DAG.getConstant(0, MVT::i32), Elt0, Elt1,
635 InFlag };
636 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreParamV2, dl,
637 CopyParamVTs, &CopyParamOps[0], 6,
638 MemVT, MachinePointerInfo());
639 InFlag = Chain.getValue(1);
640 } else {
641 unsigned curOffset = 0;
642 // V4 stores
643 // We have at least 4 elements (<3 x Ty> expands to 4 elements) and
644 // the
645 // vector will be expanded to a power of 2 elements, so we know we can
646 // always round up to the next multiple of 4 when creating the vector
647 // stores.
648 // e.g. 4 elem => 1 st.v4
649 // 6 elem => 2 st.v4
650 // 8 elem => 2 st.v4
651 // 11 elem => 3 st.v4
652 unsigned VecSize = 4;
653 if (EltVT.getSizeInBits() == 64)
654 VecSize = 2;
655
656 // This is potentially only part of a vector, so assume all elements
657 // are packed together.
658 unsigned PerStoreOffset = MemVT.getStoreSizeInBits() / 8 * VecSize;
659
660 for (unsigned i = 0; i < NumElts; i += VecSize) {
661 // Get values
662 SDValue StoreVal;
663 SmallVector<SDValue, 8> Ops;
664 Ops.push_back(Chain);
665 Ops.push_back(DAG.getConstant(paramCount, MVT::i32));
666 Ops.push_back(DAG.getConstant(curOffset, MVT::i32));
667
668 unsigned Opc = NVPTXISD::StoreParamV2;
669
670 StoreVal = OutVals[OIdx++];
671 if (NeedExtend)
672 StoreVal = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal);
673 Ops.push_back(StoreVal);
674
675 if (i + 1 < NumElts) {
676 StoreVal = OutVals[OIdx++];
677 if (NeedExtend)
678 StoreVal =
679 DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal);
680 } else {
681 StoreVal = DAG.getUNDEF(EltVT);
682 }
683 Ops.push_back(StoreVal);
684
685 if (VecSize == 4) {
686 Opc = NVPTXISD::StoreParamV4;
687 if (i + 2 < NumElts) {
688 StoreVal = OutVals[OIdx++];
689 if (NeedExtend)
690 StoreVal =
691 DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal);
692 } else {
693 StoreVal = DAG.getUNDEF(EltVT);
694 }
695 Ops.push_back(StoreVal);
696
697 if (i + 3 < NumElts) {
698 StoreVal = OutVals[OIdx++];
699 if (NeedExtend)
700 StoreVal =
701 DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal);
702 } else {
703 StoreVal = DAG.getUNDEF(EltVT);
704 }
705 Ops.push_back(StoreVal);
706 }
707
Justin Holewinskidff28d22013-07-01 12:59:01 +0000708 Ops.push_back(InFlag);
709
Justin Holewinskif8f70912013-06-28 17:57:59 +0000710 SDVTList CopyParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
711 Chain = DAG.getMemIntrinsicNode(Opc, dl, CopyParamVTs, &Ops[0],
712 Ops.size(), MemVT,
713 MachinePointerInfo());
714 InFlag = Chain.getValue(1);
715 curOffset += PerStoreOffset;
716 }
717 }
718 ++paramCount;
719 --OIdx;
720 continue;
721 }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000722 // Plain scalar
723 // for ABI, declare .param .b<size> .param<n>;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000724 unsigned sz = VT.getSizeInBits();
Justin Holewinskif8f70912013-06-28 17:57:59 +0000725 bool needExtend = false;
726 if (VT.isInteger()) {
727 if (sz < 16)
728 needExtend = true;
729 if (sz < 32)
730 sz = 32;
731 }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000732 SDVTList DeclareParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
733 SDValue DeclareParamOps[] = { Chain,
734 DAG.getConstant(paramCount, MVT::i32),
735 DAG.getConstant(sz, MVT::i32),
Justin Holewinskif8f70912013-06-28 17:57:59 +0000736 DAG.getConstant(0, MVT::i32), InFlag };
Justin Holewinskiae556d32012-05-04 20:18:50 +0000737 Chain = DAG.getNode(NVPTXISD::DeclareScalarParam, dl, DeclareParamVTs,
738 DeclareParamOps, 5);
739 InFlag = Chain.getValue(1);
Justin Holewinskif8f70912013-06-28 17:57:59 +0000740 SDValue OutV = OutVals[OIdx];
741 if (needExtend) {
742 // zext/sext i1 to i16
743 unsigned opc = ISD::ZERO_EXTEND;
744 if (Outs[OIdx].Flags.isSExt())
745 opc = ISD::SIGN_EXTEND;
746 OutV = DAG.getNode(opc, dl, MVT::i16, OutV);
747 }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000748 SDVTList CopyParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
749 SDValue CopyParamOps[] = { Chain, DAG.getConstant(paramCount, MVT::i32),
Justin Holewinskif8f70912013-06-28 17:57:59 +0000750 DAG.getConstant(0, MVT::i32), OutV, InFlag };
Justin Holewinskiae556d32012-05-04 20:18:50 +0000751
752 unsigned opcode = NVPTXISD::StoreParam;
Justin Holewinskif8f70912013-06-28 17:57:59 +0000753 if (Outs[OIdx].Flags.isZExt())
754 opcode = NVPTXISD::StoreParamU32;
755 else if (Outs[OIdx].Flags.isSExt())
756 opcode = NVPTXISD::StoreParamS32;
757 Chain = DAG.getMemIntrinsicNode(opcode, dl, CopyParamVTs, CopyParamOps, 5,
758 VT, MachinePointerInfo());
Justin Holewinskiae556d32012-05-04 20:18:50 +0000759
760 InFlag = Chain.getValue(1);
761 ++paramCount;
762 continue;
763 }
764 // struct or vector
765 SmallVector<EVT, 16> vtparts;
766 const PointerType *PTy = dyn_cast<PointerType>(Args[i].Ty);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000767 assert(PTy && "Type of a byval parameter should be pointer");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000768 ComputeValueVTs(*this, PTy->getElementType(), vtparts);
769
Justin Holewinskif8f70912013-06-28 17:57:59 +0000770 // declare .param .align <align> .b8 .param<n>[<size>];
771 unsigned sz = Outs[OIdx].Flags.getByValSize();
772 SDVTList DeclareParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
773 // The ByValAlign in the Outs[OIdx].Flags is alway set at this point,
774 // so we don't need to worry about natural alignment or not.
775 // See TargetLowering::LowerCallTo().
776 SDValue DeclareParamOps[] = {
777 Chain, DAG.getConstant(Outs[OIdx].Flags.getByValAlign(), MVT::i32),
778 DAG.getConstant(paramCount, MVT::i32), DAG.getConstant(sz, MVT::i32),
779 InFlag
780 };
781 Chain = DAG.getNode(NVPTXISD::DeclareParam, dl, DeclareParamVTs,
782 DeclareParamOps, 5);
783 InFlag = Chain.getValue(1);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000784 unsigned curOffset = 0;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000785 for (unsigned j = 0, je = vtparts.size(); j != je; ++j) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000786 unsigned elems = 1;
787 EVT elemtype = vtparts[j];
788 if (vtparts[j].isVector()) {
789 elems = vtparts[j].getVectorNumElements();
790 elemtype = vtparts[j].getVectorElementType();
791 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000792 for (unsigned k = 0, ke = elems; k != ke; ++k) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000793 unsigned sz = elemtype.getSizeInBits();
Justin Holewinskif8f70912013-06-28 17:57:59 +0000794 if (elemtype.isInteger() && (sz < 8))
795 sz = 8;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000796 SDValue srcAddr =
Justin Holewinskif8f70912013-06-28 17:57:59 +0000797 DAG.getNode(ISD::ADD, dl, getPointerTy(), OutVals[OIdx],
Justin Holewinski0497ab12013-03-30 14:29:21 +0000798 DAG.getConstant(curOffset, getPointerTy()));
Justin Holewinskif8f70912013-06-28 17:57:59 +0000799 SDValue theVal = DAG.getLoad(elemtype, dl, tempChain, srcAddr,
800 MachinePointerInfo(), false, false, false,
801 0);
802 if (elemtype.getSizeInBits() < 16) {
Justin Holewinskia2911282013-07-01 12:58:58 +0000803 theVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i16, theVal);
Justin Holewinskif8f70912013-06-28 17:57:59 +0000804 }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000805 SDVTList CopyParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
806 SDValue CopyParamOps[] = { Chain, DAG.getConstant(paramCount, MVT::i32),
Justin Holewinskif8f70912013-06-28 17:57:59 +0000807 DAG.getConstant(curOffset, MVT::i32), theVal,
Justin Holewinskiae556d32012-05-04 20:18:50 +0000808 InFlag };
Justin Holewinskif8f70912013-06-28 17:57:59 +0000809 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreParam, dl, CopyParamVTs,
810 CopyParamOps, 5, elemtype,
811 MachinePointerInfo());
812
Justin Holewinskiae556d32012-05-04 20:18:50 +0000813 InFlag = Chain.getValue(1);
Justin Holewinskif8f70912013-06-28 17:57:59 +0000814 curOffset += sz / 8;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000815 }
816 }
Justin Holewinskif8f70912013-06-28 17:57:59 +0000817 ++paramCount;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000818 }
819
820 GlobalAddressSDNode *Func = dyn_cast<GlobalAddressSDNode>(Callee.getNode());
821 unsigned retAlignment = 0;
822
823 // Handle Result
Justin Holewinskiae556d32012-05-04 20:18:50 +0000824 if (Ins.size() > 0) {
825 SmallVector<EVT, 16> resvtparts;
826 ComputeValueVTs(*this, retTy, resvtparts);
827
Justin Holewinskif8f70912013-06-28 17:57:59 +0000828 // Declare
829 // .param .align 16 .b8 retval0[<size-in-bytes>], or
830 // .param .b<size-in-bits> retval0
831 unsigned resultsz = TD->getTypeAllocSizeInBits(retTy);
832 if (retTy->isPrimitiveType() || retTy->isIntegerTy() ||
833 retTy->isPointerTy()) {
834 // Scalar needs to be at least 32bit wide
835 if (resultsz < 32)
836 resultsz = 32;
837 SDVTList DeclareRetVTs = DAG.getVTList(MVT::Other, MVT::Glue);
838 SDValue DeclareRetOps[] = { Chain, DAG.getConstant(1, MVT::i32),
839 DAG.getConstant(resultsz, MVT::i32),
840 DAG.getConstant(0, MVT::i32), InFlag };
841 Chain = DAG.getNode(NVPTXISD::DeclareRet, dl, DeclareRetVTs,
842 DeclareRetOps, 5);
843 InFlag = Chain.getValue(1);
844 } else {
845 retAlignment = getArgumentAlignment(Callee, CS, retTy, 0);
846 SDVTList DeclareRetVTs = DAG.getVTList(MVT::Other, MVT::Glue);
847 SDValue DeclareRetOps[] = { Chain,
848 DAG.getConstant(retAlignment, MVT::i32),
849 DAG.getConstant(resultsz / 8, MVT::i32),
850 DAG.getConstant(0, MVT::i32), InFlag };
851 Chain = DAG.getNode(NVPTXISD::DeclareRetParam, dl, DeclareRetVTs,
852 DeclareRetOps, 5);
853 InFlag = Chain.getValue(1);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000854 }
855 }
856
857 if (!Func) {
858 // This is indirect function call case : PTX requires a prototype of the
859 // form
860 // proto_0 : .callprototype(.param .b32 _) _ (.param .b32 _);
861 // to be emitted, and the label has to used as the last arg of call
862 // instruction.
863 // The prototype is embedded in a string and put as the operand for an
864 // INLINEASM SDNode.
865 SDVTList InlineAsmVTs = DAG.getVTList(MVT::Other, MVT::Glue);
Justin Holewinskif8f70912013-06-28 17:57:59 +0000866 std::string proto_string =
867 getPrototype(retTy, Args, Outs, retAlignment, CS);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000868 const char *asmstr = nvTM->getManagedStrPool()
869 ->getManagedString(proto_string.c_str())->c_str();
870 SDValue InlineAsmOps[] = {
871 Chain, DAG.getTargetExternalSymbol(asmstr, getPointerTy()),
872 DAG.getMDNode(0), DAG.getTargetConstant(0, MVT::i32), InFlag
873 };
Justin Holewinskiae556d32012-05-04 20:18:50 +0000874 Chain = DAG.getNode(ISD::INLINEASM, dl, InlineAsmVTs, InlineAsmOps, 5);
875 InFlag = Chain.getValue(1);
876 }
877 // Op to just print "call"
878 SDVTList PrintCallVTs = DAG.getVTList(MVT::Other, MVT::Glue);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000879 SDValue PrintCallOps[] = {
Justin Holewinskif8f70912013-06-28 17:57:59 +0000880 Chain, DAG.getConstant((Ins.size() == 0) ? 0 : 1, MVT::i32), InFlag
Justin Holewinski0497ab12013-03-30 14:29:21 +0000881 };
882 Chain = DAG.getNode(Func ? (NVPTXISD::PrintCallUni) : (NVPTXISD::PrintCall),
883 dl, PrintCallVTs, PrintCallOps, 3);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000884 InFlag = Chain.getValue(1);
885
886 // Ops to print out the function name
887 SDVTList CallVoidVTs = DAG.getVTList(MVT::Other, MVT::Glue);
888 SDValue CallVoidOps[] = { Chain, Callee, InFlag };
889 Chain = DAG.getNode(NVPTXISD::CallVoid, dl, CallVoidVTs, CallVoidOps, 3);
890 InFlag = Chain.getValue(1);
891
892 // Ops to print out the param list
893 SDVTList CallArgBeginVTs = DAG.getVTList(MVT::Other, MVT::Glue);
894 SDValue CallArgBeginOps[] = { Chain, InFlag };
895 Chain = DAG.getNode(NVPTXISD::CallArgBegin, dl, CallArgBeginVTs,
896 CallArgBeginOps, 2);
897 InFlag = Chain.getValue(1);
898
Justin Holewinski0497ab12013-03-30 14:29:21 +0000899 for (unsigned i = 0, e = paramCount; i != e; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000900 unsigned opcode;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000901 if (i == (e - 1))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000902 opcode = NVPTXISD::LastCallArg;
903 else
904 opcode = NVPTXISD::CallArg;
905 SDVTList CallArgVTs = DAG.getVTList(MVT::Other, MVT::Glue);
906 SDValue CallArgOps[] = { Chain, DAG.getConstant(1, MVT::i32),
Justin Holewinski0497ab12013-03-30 14:29:21 +0000907 DAG.getConstant(i, MVT::i32), InFlag };
Justin Holewinskiae556d32012-05-04 20:18:50 +0000908 Chain = DAG.getNode(opcode, dl, CallArgVTs, CallArgOps, 4);
909 InFlag = Chain.getValue(1);
910 }
911 SDVTList CallArgEndVTs = DAG.getVTList(MVT::Other, MVT::Glue);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000912 SDValue CallArgEndOps[] = { Chain, DAG.getConstant(Func ? 1 : 0, MVT::i32),
Justin Holewinskiae556d32012-05-04 20:18:50 +0000913 InFlag };
Justin Holewinski0497ab12013-03-30 14:29:21 +0000914 Chain =
915 DAG.getNode(NVPTXISD::CallArgEnd, dl, CallArgEndVTs, CallArgEndOps, 3);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000916 InFlag = Chain.getValue(1);
917
918 if (!Func) {
919 SDVTList PrototypeVTs = DAG.getVTList(MVT::Other, MVT::Glue);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000920 SDValue PrototypeOps[] = { Chain, DAG.getConstant(uniqueCallSite, MVT::i32),
Justin Holewinskiae556d32012-05-04 20:18:50 +0000921 InFlag };
922 Chain = DAG.getNode(NVPTXISD::Prototype, dl, PrototypeVTs, PrototypeOps, 3);
923 InFlag = Chain.getValue(1);
924 }
925
926 // Generate loads from param memory/moves from registers for result
927 if (Ins.size() > 0) {
Justin Holewinskif8f70912013-06-28 17:57:59 +0000928 unsigned resoffset = 0;
929 if (retTy && retTy->isVectorTy()) {
930 EVT ObjectVT = getValueType(retTy);
931 unsigned NumElts = ObjectVT.getVectorNumElements();
932 EVT EltVT = ObjectVT.getVectorElementType();
Benjamin Kramer3cc579a2013-06-29 22:51:12 +0000933 assert(nvTM->getTargetLowering()->getNumRegisters(F->getContext(),
934 ObjectVT) == NumElts &&
Justin Holewinskif8f70912013-06-28 17:57:59 +0000935 "Vector was not scalarized");
936 unsigned sz = EltVT.getSizeInBits();
937 bool needTruncate = sz < 16 ? true : false;
938
939 if (NumElts == 1) {
940 // Just a simple load
941 std::vector<EVT> LoadRetVTs;
942 if (needTruncate) {
943 // If loading i1 result, generate
944 // load i16
945 // trunc i16 to i1
946 LoadRetVTs.push_back(MVT::i16);
947 } else
948 LoadRetVTs.push_back(EltVT);
949 LoadRetVTs.push_back(MVT::Other);
950 LoadRetVTs.push_back(MVT::Glue);
951 std::vector<SDValue> LoadRetOps;
952 LoadRetOps.push_back(Chain);
953 LoadRetOps.push_back(DAG.getConstant(1, MVT::i32));
954 LoadRetOps.push_back(DAG.getConstant(0, MVT::i32));
955 LoadRetOps.push_back(InFlag);
956 SDValue retval = DAG.getMemIntrinsicNode(
957 NVPTXISD::LoadParam, dl,
958 DAG.getVTList(&LoadRetVTs[0], LoadRetVTs.size()), &LoadRetOps[0],
959 LoadRetOps.size(), EltVT, MachinePointerInfo());
Justin Holewinskiae556d32012-05-04 20:18:50 +0000960 Chain = retval.getValue(1);
961 InFlag = retval.getValue(2);
Justin Holewinskif8f70912013-06-28 17:57:59 +0000962 SDValue Ret0 = retval;
963 if (needTruncate)
964 Ret0 = DAG.getNode(ISD::TRUNCATE, dl, EltVT, Ret0);
965 InVals.push_back(Ret0);
966 } else if (NumElts == 2) {
967 // LoadV2
968 std::vector<EVT> LoadRetVTs;
969 if (needTruncate) {
970 // If loading i1 result, generate
971 // load i16
972 // trunc i16 to i1
973 LoadRetVTs.push_back(MVT::i16);
974 LoadRetVTs.push_back(MVT::i16);
975 } else {
976 LoadRetVTs.push_back(EltVT);
977 LoadRetVTs.push_back(EltVT);
978 }
979 LoadRetVTs.push_back(MVT::Other);
980 LoadRetVTs.push_back(MVT::Glue);
981 std::vector<SDValue> LoadRetOps;
982 LoadRetOps.push_back(Chain);
983 LoadRetOps.push_back(DAG.getConstant(1, MVT::i32));
984 LoadRetOps.push_back(DAG.getConstant(0, MVT::i32));
985 LoadRetOps.push_back(InFlag);
986 SDValue retval = DAG.getMemIntrinsicNode(
987 NVPTXISD::LoadParamV2, dl,
988 DAG.getVTList(&LoadRetVTs[0], LoadRetVTs.size()), &LoadRetOps[0],
989 LoadRetOps.size(), EltVT, MachinePointerInfo());
990 Chain = retval.getValue(2);
991 InFlag = retval.getValue(3);
992 SDValue Ret0 = retval.getValue(0);
993 SDValue Ret1 = retval.getValue(1);
994 if (needTruncate) {
995 Ret0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ret0);
996 InVals.push_back(Ret0);
997 Ret1 = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ret1);
998 InVals.push_back(Ret1);
999 } else {
1000 InVals.push_back(Ret0);
1001 InVals.push_back(Ret1);
1002 }
1003 } else {
1004 // Split into N LoadV4
1005 unsigned Ofst = 0;
1006 unsigned VecSize = 4;
1007 unsigned Opc = NVPTXISD::LoadParamV4;
1008 if (EltVT.getSizeInBits() == 64) {
1009 VecSize = 2;
1010 Opc = NVPTXISD::LoadParamV2;
1011 }
1012 EVT VecVT = EVT::getVectorVT(F->getContext(), EltVT, VecSize);
1013 for (unsigned i = 0; i < NumElts; i += VecSize) {
1014 SmallVector<EVT, 8> LoadRetVTs;
1015 if (needTruncate) {
1016 // If loading i1 result, generate
1017 // load i16
1018 // trunc i16 to i1
1019 for (unsigned j = 0; j < VecSize; ++j)
1020 LoadRetVTs.push_back(MVT::i16);
1021 } else {
1022 for (unsigned j = 0; j < VecSize; ++j)
1023 LoadRetVTs.push_back(EltVT);
1024 }
1025 LoadRetVTs.push_back(MVT::Other);
1026 LoadRetVTs.push_back(MVT::Glue);
1027 SmallVector<SDValue, 4> LoadRetOps;
1028 LoadRetOps.push_back(Chain);
1029 LoadRetOps.push_back(DAG.getConstant(1, MVT::i32));
1030 LoadRetOps.push_back(DAG.getConstant(Ofst, MVT::i32));
1031 LoadRetOps.push_back(InFlag);
1032 SDValue retval = DAG.getMemIntrinsicNode(
1033 Opc, dl, DAG.getVTList(&LoadRetVTs[0], LoadRetVTs.size()),
1034 &LoadRetOps[0], LoadRetOps.size(), EltVT, MachinePointerInfo());
1035 if (VecSize == 2) {
1036 Chain = retval.getValue(2);
1037 InFlag = retval.getValue(3);
1038 } else {
1039 Chain = retval.getValue(4);
1040 InFlag = retval.getValue(5);
1041 }
1042
1043 for (unsigned j = 0; j < VecSize; ++j) {
1044 if (i + j >= NumElts)
1045 break;
1046 SDValue Elt = retval.getValue(j);
1047 if (needTruncate)
1048 Elt = DAG.getNode(ISD::TRUNCATE, dl, EltVT, Elt);
1049 InVals.push_back(Elt);
1050 }
1051 Ofst += TD->getTypeAllocSize(VecVT.getTypeForEVT(F->getContext()));
1052 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001053 }
Justin Holewinski0497ab12013-03-30 14:29:21 +00001054 } else {
Justin Holewinskif8f70912013-06-28 17:57:59 +00001055 SmallVector<EVT, 16> VTs;
1056 ComputePTXValueVTs(*this, retTy, VTs);
1057 assert(VTs.size() == Ins.size() && "Bad value decomposition");
Justin Holewinski0497ab12013-03-30 14:29:21 +00001058 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
Justin Holewinskif8f70912013-06-28 17:57:59 +00001059 unsigned sz = VTs[i].getSizeInBits();
1060 bool needTruncate = sz < 8 ? true : false;
1061 if (VTs[i].isInteger() && (sz < 8))
1062 sz = 8;
1063
1064 SmallVector<EVT, 4> LoadRetVTs;
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001065 EVT TheLoadType = VTs[i];
1066 if (retTy->isIntegerTy() &&
1067 TD->getTypeAllocSizeInBits(retTy) < 32) {
1068 // This is for integer types only, and specifically not for
1069 // aggregates.
1070 LoadRetVTs.push_back(MVT::i32);
1071 TheLoadType = MVT::i32;
1072 } else if (sz < 16) {
Justin Holewinskif8f70912013-06-28 17:57:59 +00001073 // If loading i1/i8 result, generate
1074 // load i8 (-> i16)
1075 // trunc i16 to i1/i8
1076 LoadRetVTs.push_back(MVT::i16);
1077 } else
1078 LoadRetVTs.push_back(Ins[i].VT);
1079 LoadRetVTs.push_back(MVT::Other);
1080 LoadRetVTs.push_back(MVT::Glue);
1081
1082 SmallVector<SDValue, 4> LoadRetOps;
1083 LoadRetOps.push_back(Chain);
1084 LoadRetOps.push_back(DAG.getConstant(1, MVT::i32));
1085 LoadRetOps.push_back(DAG.getConstant(resoffset, MVT::i32));
1086 LoadRetOps.push_back(InFlag);
1087 SDValue retval = DAG.getMemIntrinsicNode(
1088 NVPTXISD::LoadParam, dl,
1089 DAG.getVTList(&LoadRetVTs[0], LoadRetVTs.size()), &LoadRetOps[0],
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001090 LoadRetOps.size(), TheLoadType, MachinePointerInfo());
Justin Holewinskif8f70912013-06-28 17:57:59 +00001091 Chain = retval.getValue(1);
1092 InFlag = retval.getValue(2);
1093 SDValue Ret0 = retval.getValue(0);
1094 if (needTruncate)
1095 Ret0 = DAG.getNode(ISD::TRUNCATE, dl, Ins[i].VT, Ret0);
1096 InVals.push_back(Ret0);
1097 resoffset += sz / 8;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001098 }
1099 }
1100 }
Justin Holewinskif8f70912013-06-28 17:57:59 +00001101
Justin Holewinski0497ab12013-03-30 14:29:21 +00001102 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(uniqueCallSite, true),
1103 DAG.getIntPtrConstant(uniqueCallSite + 1, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001104 InFlag, dl);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001105 uniqueCallSite++;
1106
1107 // set isTailCall to false for now, until we figure out how to express
1108 // tail call optimization in PTX
1109 isTailCall = false;
1110 return Chain;
1111}
Justin Holewinskiae556d32012-05-04 20:18:50 +00001112
1113// By default CONCAT_VECTORS is lowered by ExpandVectorBuildThroughStack()
1114// (see LegalizeDAG.cpp). This is slow and uses local memory.
1115// We use extract/insert/build vector just as what LegalizeOp() does in llvm 2.5
Justin Holewinski0497ab12013-03-30 14:29:21 +00001116SDValue
1117NVPTXTargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001118 SDNode *Node = Op.getNode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001119 SDLoc dl(Node);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001120 SmallVector<SDValue, 8> Ops;
1121 unsigned NumOperands = Node->getNumOperands();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001122 for (unsigned i = 0; i < NumOperands; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001123 SDValue SubOp = Node->getOperand(i);
1124 EVT VVT = SubOp.getNode()->getValueType(0);
1125 EVT EltVT = VVT.getVectorElementType();
1126 unsigned NumSubElem = VVT.getVectorNumElements();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001127 for (unsigned j = 0; j < NumSubElem; ++j) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001128 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, SubOp,
1129 DAG.getIntPtrConstant(j)));
1130 }
1131 }
Justin Holewinski0497ab12013-03-30 14:29:21 +00001132 return DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0), &Ops[0],
1133 Ops.size());
Justin Holewinskiae556d32012-05-04 20:18:50 +00001134}
1135
Justin Holewinski0497ab12013-03-30 14:29:21 +00001136SDValue
1137NVPTXTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001138 switch (Op.getOpcode()) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001139 case ISD::RETURNADDR:
1140 return SDValue();
1141 case ISD::FRAMEADDR:
1142 return SDValue();
1143 case ISD::GlobalAddress:
1144 return LowerGlobalAddress(Op, DAG);
1145 case ISD::INTRINSIC_W_CHAIN:
1146 return Op;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001147 case ISD::BUILD_VECTOR:
1148 case ISD::EXTRACT_SUBVECTOR:
1149 return Op;
Justin Holewinski0497ab12013-03-30 14:29:21 +00001150 case ISD::CONCAT_VECTORS:
1151 return LowerCONCAT_VECTORS(Op, DAG);
1152 case ISD::STORE:
1153 return LowerSTORE(Op, DAG);
1154 case ISD::LOAD:
1155 return LowerLOAD(Op, DAG);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001156 default:
David Blaikie891d0a32012-05-04 22:34:16 +00001157 llvm_unreachable("Custom lowering not defined for operation");
Justin Holewinskiae556d32012-05-04 20:18:50 +00001158 }
1159}
1160
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001161SDValue NVPTXTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1162 if (Op.getValueType() == MVT::i1)
1163 return LowerLOADi1(Op, DAG);
1164 else
1165 return SDValue();
1166}
1167
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001168// v = ld i1* addr
1169// =>
Justin Holewinskif8f70912013-06-28 17:57:59 +00001170// v1 = ld i8* addr (-> i16)
1171// v = trunc i16 to i1
Justin Holewinski0497ab12013-03-30 14:29:21 +00001172SDValue NVPTXTargetLowering::LowerLOADi1(SDValue Op, SelectionDAG &DAG) const {
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001173 SDNode *Node = Op.getNode();
1174 LoadSDNode *LD = cast<LoadSDNode>(Node);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001175 SDLoc dl(Node);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001176 assert(LD->getExtensionType() == ISD::NON_EXTLOAD);
NAKAMURA Takumi5bbe0e12012-11-14 23:46:15 +00001177 assert(Node->getValueType(0) == MVT::i1 &&
1178 "Custom lowering for i1 load only");
Justin Holewinski0497ab12013-03-30 14:29:21 +00001179 SDValue newLD =
Justin Holewinskif8f70912013-06-28 17:57:59 +00001180 DAG.getLoad(MVT::i16, dl, LD->getChain(), LD->getBasePtr(),
Justin Holewinski0497ab12013-03-30 14:29:21 +00001181 LD->getPointerInfo(), LD->isVolatile(), LD->isNonTemporal(),
1182 LD->isInvariant(), LD->getAlignment());
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001183 SDValue result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, newLD);
1184 // The legalizer (the caller) is expecting two values from the legalized
1185 // load, so we build a MergeValues node for it. See ExpandUnalignedLoad()
1186 // in LegalizeDAG.cpp which also uses MergeValues.
Justin Holewinski0497ab12013-03-30 14:29:21 +00001187 SDValue Ops[] = { result, LD->getChain() };
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001188 return DAG.getMergeValues(Ops, 2, dl);
1189}
1190
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001191SDValue NVPTXTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1192 EVT ValVT = Op.getOperand(1).getValueType();
1193 if (ValVT == MVT::i1)
1194 return LowerSTOREi1(Op, DAG);
1195 else if (ValVT.isVector())
1196 return LowerSTOREVector(Op, DAG);
1197 else
1198 return SDValue();
1199}
1200
1201SDValue
1202NVPTXTargetLowering::LowerSTOREVector(SDValue Op, SelectionDAG &DAG) const {
1203 SDNode *N = Op.getNode();
1204 SDValue Val = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001205 SDLoc DL(N);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001206 EVT ValVT = Val.getValueType();
1207
1208 if (ValVT.isVector()) {
1209 // We only handle "native" vector sizes for now, e.g. <4 x double> is not
1210 // legal. We can (and should) split that into 2 stores of <2 x double> here
1211 // but I'm leaving that as a TODO for now.
1212 if (!ValVT.isSimple())
1213 return SDValue();
1214 switch (ValVT.getSimpleVT().SimpleTy) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001215 default:
1216 return SDValue();
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001217 case MVT::v2i8:
1218 case MVT::v2i16:
1219 case MVT::v2i32:
1220 case MVT::v2i64:
1221 case MVT::v2f32:
1222 case MVT::v2f64:
1223 case MVT::v4i8:
1224 case MVT::v4i16:
1225 case MVT::v4i32:
1226 case MVT::v4f32:
1227 // This is a "native" vector type
1228 break;
1229 }
1230
1231 unsigned Opcode = 0;
1232 EVT EltVT = ValVT.getVectorElementType();
1233 unsigned NumElts = ValVT.getVectorNumElements();
1234
1235 // Since StoreV2 is a target node, we cannot rely on DAG type legalization.
1236 // Therefore, we must ensure the type is legal. For i1 and i8, we set the
1237 // stored type to i16 and propogate the "real" type as the memory type.
Justin Holewinskia2911282013-07-01 12:58:58 +00001238 bool NeedExt = false;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001239 if (EltVT.getSizeInBits() < 16)
Justin Holewinskia2911282013-07-01 12:58:58 +00001240 NeedExt = true;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001241
1242 switch (NumElts) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001243 default:
1244 return SDValue();
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001245 case 2:
1246 Opcode = NVPTXISD::StoreV2;
1247 break;
1248 case 4: {
1249 Opcode = NVPTXISD::StoreV4;
1250 break;
1251 }
1252 }
1253
1254 SmallVector<SDValue, 8> Ops;
1255
1256 // First is the chain
1257 Ops.push_back(N->getOperand(0));
1258
1259 // Then the split values
1260 for (unsigned i = 0; i < NumElts; ++i) {
1261 SDValue ExtVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Val,
1262 DAG.getIntPtrConstant(i));
Justin Holewinskia2911282013-07-01 12:58:58 +00001263 if (NeedExt)
1264 ExtVal = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i16, ExtVal);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001265 Ops.push_back(ExtVal);
1266 }
1267
1268 // Then any remaining arguments
1269 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) {
1270 Ops.push_back(N->getOperand(i));
1271 }
1272
1273 MemSDNode *MemSD = cast<MemSDNode>(N);
1274
Justin Holewinski0497ab12013-03-30 14:29:21 +00001275 SDValue NewSt = DAG.getMemIntrinsicNode(
1276 Opcode, DL, DAG.getVTList(MVT::Other), &Ops[0], Ops.size(),
1277 MemSD->getMemoryVT(), MemSD->getMemOperand());
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001278
1279 //return DCI.CombineTo(N, NewSt, true);
1280 return NewSt;
1281 }
1282
1283 return SDValue();
1284}
1285
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001286// st i1 v, addr
1287// =>
Justin Holewinskif8f70912013-06-28 17:57:59 +00001288// v1 = zxt v to i16
1289// st.u8 i16, addr
Justin Holewinski0497ab12013-03-30 14:29:21 +00001290SDValue NVPTXTargetLowering::LowerSTOREi1(SDValue Op, SelectionDAG &DAG) const {
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001291 SDNode *Node = Op.getNode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001292 SDLoc dl(Node);
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001293 StoreSDNode *ST = cast<StoreSDNode>(Node);
1294 SDValue Tmp1 = ST->getChain();
1295 SDValue Tmp2 = ST->getBasePtr();
1296 SDValue Tmp3 = ST->getValue();
NAKAMURA Takumi5bbe0e12012-11-14 23:46:15 +00001297 assert(Tmp3.getValueType() == MVT::i1 && "Custom lowering for i1 store only");
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001298 unsigned Alignment = ST->getAlignment();
1299 bool isVolatile = ST->isVolatile();
1300 bool isNonTemporal = ST->isNonTemporal();
Justin Holewinskif8f70912013-06-28 17:57:59 +00001301 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Tmp3);
1302 SDValue Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2,
1303 ST->getPointerInfo(), MVT::i8, isNonTemporal,
1304 isVolatile, Alignment);
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001305 return Result;
1306}
1307
Justin Holewinski0497ab12013-03-30 14:29:21 +00001308SDValue NVPTXTargetLowering::getExtSymb(SelectionDAG &DAG, const char *inname,
1309 int idx, EVT v) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001310 std::string *name = nvTM->getManagedStrPool()->getManagedString(inname);
1311 std::stringstream suffix;
1312 suffix << idx;
1313 *name += suffix.str();
1314 return DAG.getTargetExternalSymbol(name->c_str(), v);
1315}
1316
1317SDValue
1318NVPTXTargetLowering::getParamSymbol(SelectionDAG &DAG, int idx, EVT v) const {
1319 return getExtSymb(DAG, ".PARAM", idx, v);
1320}
1321
Justin Holewinski0497ab12013-03-30 14:29:21 +00001322SDValue NVPTXTargetLowering::getParamHelpSymbol(SelectionDAG &DAG, int idx) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001323 return getExtSymb(DAG, ".HLPPARAM", idx);
1324}
1325
1326// Check to see if the kernel argument is image*_t or sampler_t
1327
1328bool llvm::isImageOrSamplerVal(const Value *arg, const Module *context) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001329 static const char *const specialTypes[] = { "struct._image2d_t",
1330 "struct._image3d_t",
1331 "struct._sampler_t" };
Justin Holewinskiae556d32012-05-04 20:18:50 +00001332
1333 const Type *Ty = arg->getType();
1334 const PointerType *PTy = dyn_cast<PointerType>(Ty);
1335
1336 if (!PTy)
1337 return false;
1338
1339 if (!context)
1340 return false;
1341
1342 const StructType *STy = dyn_cast<StructType>(PTy->getElementType());
Justin Holewinskifb711152012-12-05 20:50:28 +00001343 const std::string TypeName = STy && !STy->isLiteral() ? STy->getName() : "";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001344
Craig Toppere4260f92012-05-24 04:22:05 +00001345 for (int i = 0, e = array_lengthof(specialTypes); i != e; ++i)
Justin Holewinskiae556d32012-05-04 20:18:50 +00001346 if (TypeName == specialTypes[i])
1347 return true;
1348
1349 return false;
1350}
1351
Justin Holewinski0497ab12013-03-30 14:29:21 +00001352SDValue NVPTXTargetLowering::LowerFormalArguments(
1353 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001354 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc dl, SelectionDAG &DAG,
Justin Holewinski0497ab12013-03-30 14:29:21 +00001355 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001356 MachineFunction &MF = DAG.getMachineFunction();
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001357 const DataLayout *TD = getDataLayout();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001358
1359 const Function *F = MF.getFunction();
Bill Wendlinge94d8432012-12-07 23:16:57 +00001360 const AttributeSet &PAL = F->getAttributes();
Justin Holewinski44f5c602013-06-28 17:57:53 +00001361 const TargetLowering *TLI = nvTM->getTargetLowering();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001362
1363 SDValue Root = DAG.getRoot();
1364 std::vector<SDValue> OutChains;
1365
1366 bool isKernel = llvm::isKernelFunction(*F);
1367 bool isABI = (nvptxSubtarget.getSmVersion() >= 20);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001368 assert(isABI && "Non-ABI compilation is not supported");
1369 if (!isABI)
1370 return Chain;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001371
1372 std::vector<Type *> argTypes;
1373 std::vector<const Argument *> theArgs;
1374 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001375 I != E; ++I) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001376 theArgs.push_back(I);
1377 argTypes.push_back(I->getType());
1378 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001379 // argTypes.size() (or theArgs.size()) and Ins.size() need not match.
1380 // Ins.size() will be larger
1381 // * if there is an aggregate argument with multiple fields (each field
1382 // showing up separately in Ins)
1383 // * if there is a vector argument with more than typical vector-length
1384 // elements (generally if more than 4) where each vector element is
1385 // individually present in Ins.
1386 // So a different index should be used for indexing into Ins.
1387 // See similar issue in LowerCall.
1388 unsigned InsIdx = 0;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001389
1390 int idx = 0;
Justin Holewinski44f5c602013-06-28 17:57:53 +00001391 for (unsigned i = 0, e = theArgs.size(); i != e; ++i, ++idx, ++InsIdx) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001392 Type *Ty = argTypes[i];
Justin Holewinskiae556d32012-05-04 20:18:50 +00001393
1394 // If the kernel argument is image*_t or sampler_t, convert it to
1395 // a i32 constant holding the parameter position. This can later
1396 // matched in the AsmPrinter to output the correct mangled name.
Justin Holewinski0497ab12013-03-30 14:29:21 +00001397 if (isImageOrSamplerVal(
1398 theArgs[i],
1399 (theArgs[i]->getParent() ? theArgs[i]->getParent()->getParent()
1400 : 0))) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001401 assert(isKernel && "Only kernels can have image/sampler params");
Justin Holewinski0497ab12013-03-30 14:29:21 +00001402 InVals.push_back(DAG.getConstant(i + 1, MVT::i32));
Justin Holewinskiae556d32012-05-04 20:18:50 +00001403 continue;
1404 }
1405
1406 if (theArgs[i]->use_empty()) {
1407 // argument is dead
Justin Holewinski44f5c602013-06-28 17:57:53 +00001408 if (Ty->isAggregateType()) {
1409 SmallVector<EVT, 16> vtparts;
1410
Justin Holewinskif8f70912013-06-28 17:57:59 +00001411 ComputePTXValueVTs(*this, Ty, vtparts);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001412 assert(vtparts.size() > 0 && "empty aggregate type not expected");
1413 for (unsigned parti = 0, parte = vtparts.size(); parti != parte;
1414 ++parti) {
1415 EVT partVT = vtparts[parti];
1416 InVals.push_back(DAG.getNode(ISD::UNDEF, dl, partVT));
1417 ++InsIdx;
Justin Holewinskie9884092013-03-24 21:17:47 +00001418 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001419 if (vtparts.size() > 0)
1420 --InsIdx;
1421 continue;
Justin Holewinskie9884092013-03-24 21:17:47 +00001422 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001423 if (Ty->isVectorTy()) {
1424 EVT ObjectVT = getValueType(Ty);
1425 unsigned NumRegs = TLI->getNumRegisters(F->getContext(), ObjectVT);
1426 for (unsigned parti = 0; parti < NumRegs; ++parti) {
1427 InVals.push_back(DAG.getNode(ISD::UNDEF, dl, Ins[InsIdx].VT));
1428 ++InsIdx;
1429 }
1430 if (NumRegs > 0)
1431 --InsIdx;
1432 continue;
1433 }
1434 InVals.push_back(DAG.getNode(ISD::UNDEF, dl, Ins[InsIdx].VT));
Justin Holewinskiae556d32012-05-04 20:18:50 +00001435 continue;
1436 }
1437
1438 // In the following cases, assign a node order of "idx+1"
Justin Holewinski44f5c602013-06-28 17:57:53 +00001439 // to newly created nodes. The SDNodes for params have to
Justin Holewinskiae556d32012-05-04 20:18:50 +00001440 // appear in the same order as their order of appearance
1441 // in the original function. "idx+1" holds that order.
Justin Holewinski0497ab12013-03-30 14:29:21 +00001442 if (PAL.hasAttribute(i + 1, Attribute::ByVal) == false) {
Justin Holewinski44f5c602013-06-28 17:57:53 +00001443 if (Ty->isAggregateType()) {
1444 SmallVector<EVT, 16> vtparts;
1445 SmallVector<uint64_t, 16> offsets;
1446
Justin Holewinskif8f70912013-06-28 17:57:59 +00001447 // NOTE: Here, we lose the ability to issue vector loads for vectors
1448 // that are a part of a struct. This should be investigated in the
1449 // future.
1450 ComputePTXValueVTs(*this, Ty, vtparts, &offsets, 0);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001451 assert(vtparts.size() > 0 && "empty aggregate type not expected");
1452 bool aggregateIsPacked = false;
1453 if (StructType *STy = llvm::dyn_cast<StructType>(Ty))
1454 aggregateIsPacked = STy->isPacked();
1455
1456 SDValue Arg = getParamSymbol(DAG, idx, getPointerTy());
1457 for (unsigned parti = 0, parte = vtparts.size(); parti != parte;
1458 ++parti) {
1459 EVT partVT = vtparts[parti];
1460 Value *srcValue = Constant::getNullValue(
1461 PointerType::get(partVT.getTypeForEVT(F->getContext()),
1462 llvm::ADDRESS_SPACE_PARAM));
1463 SDValue srcAddr =
1464 DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg,
1465 DAG.getConstant(offsets[parti], getPointerTy()));
1466 unsigned partAlign =
1467 aggregateIsPacked ? 1
1468 : TD->getABITypeAlignment(
1469 partVT.getTypeForEVT(F->getContext()));
Justin Holewinskia2911282013-07-01 12:58:58 +00001470 SDValue p;
1471 if (Ins[InsIdx].VT.getSizeInBits() > partVT.getSizeInBits()) {
1472 ISD::LoadExtType ExtOp = Ins[InsIdx].Flags.isSExt() ?
1473 ISD::SEXTLOAD : ISD::ZEXTLOAD;
1474 p = DAG.getExtLoad(ExtOp, dl, Ins[InsIdx].VT, Root, srcAddr,
Justin Holewinskif8f70912013-06-28 17:57:59 +00001475 MachinePointerInfo(srcValue), partVT, false,
1476 false, partAlign);
Justin Holewinskia2911282013-07-01 12:58:58 +00001477 } else {
Justin Holewinskif8f70912013-06-28 17:57:59 +00001478 p = DAG.getLoad(partVT, dl, Root, srcAddr,
1479 MachinePointerInfo(srcValue), false, false, false,
1480 partAlign);
Justin Holewinskia2911282013-07-01 12:58:58 +00001481 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001482 if (p.getNode())
1483 p.getNode()->setIROrder(idx + 1);
1484 InVals.push_back(p);
1485 ++InsIdx;
Justin Holewinskie9884092013-03-24 21:17:47 +00001486 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001487 if (vtparts.size() > 0)
1488 --InsIdx;
Justin Holewinskie9884092013-03-24 21:17:47 +00001489 continue;
1490 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001491 if (Ty->isVectorTy()) {
1492 EVT ObjectVT = getValueType(Ty);
Justin Holewinskiaaaf2892013-06-25 12:22:21 +00001493 SDValue Arg = getParamSymbol(DAG, idx, getPointerTy());
Justin Holewinski44f5c602013-06-28 17:57:53 +00001494 unsigned NumElts = ObjectVT.getVectorNumElements();
1495 assert(TLI->getNumRegisters(F->getContext(), ObjectVT) == NumElts &&
1496 "Vector was not scalarized");
1497 unsigned Ofst = 0;
1498 EVT EltVT = ObjectVT.getVectorElementType();
1499
1500 // V1 load
1501 // f32 = load ...
1502 if (NumElts == 1) {
1503 // We only have one element, so just directly load it
1504 Value *SrcValue = Constant::getNullValue(PointerType::get(
1505 EltVT.getTypeForEVT(F->getContext()), llvm::ADDRESS_SPACE_PARAM));
1506 SDValue SrcAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg,
1507 DAG.getConstant(Ofst, getPointerTy()));
1508 SDValue P = DAG.getLoad(
1509 EltVT, dl, Root, SrcAddr, MachinePointerInfo(SrcValue), false,
1510 false, true,
1511 TD->getABITypeAlignment(EltVT.getTypeForEVT(F->getContext())));
1512 if (P.getNode())
1513 P.getNode()->setIROrder(idx + 1);
1514
Justin Holewinskif8f70912013-06-28 17:57:59 +00001515 if (Ins[InsIdx].VT.getSizeInBits() > EltVT.getSizeInBits())
Justin Holewinskia2911282013-07-01 12:58:58 +00001516 P = DAG.getNode(ISD::ANY_EXTEND, dl, Ins[InsIdx].VT, P);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001517 InVals.push_back(P);
1518 Ofst += TD->getTypeAllocSize(EltVT.getTypeForEVT(F->getContext()));
1519 ++InsIdx;
1520 } else if (NumElts == 2) {
1521 // V2 load
1522 // f32,f32 = load ...
1523 EVT VecVT = EVT::getVectorVT(F->getContext(), EltVT, 2);
1524 Value *SrcValue = Constant::getNullValue(PointerType::get(
1525 VecVT.getTypeForEVT(F->getContext()), llvm::ADDRESS_SPACE_PARAM));
1526 SDValue SrcAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg,
1527 DAG.getConstant(Ofst, getPointerTy()));
1528 SDValue P = DAG.getLoad(
1529 VecVT, dl, Root, SrcAddr, MachinePointerInfo(SrcValue), false,
1530 false, true,
1531 TD->getABITypeAlignment(VecVT.getTypeForEVT(F->getContext())));
1532 if (P.getNode())
1533 P.getNode()->setIROrder(idx + 1);
1534
1535 SDValue Elt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, P,
1536 DAG.getIntPtrConstant(0));
1537 SDValue Elt1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, P,
1538 DAG.getIntPtrConstant(1));
Justin Holewinskif8f70912013-06-28 17:57:59 +00001539
1540 if (Ins[InsIdx].VT.getSizeInBits() > EltVT.getSizeInBits()) {
Justin Holewinskia2911282013-07-01 12:58:58 +00001541 Elt0 = DAG.getNode(ISD::ANY_EXTEND, dl, Ins[InsIdx].VT, Elt0);
1542 Elt1 = DAG.getNode(ISD::ANY_EXTEND, dl, Ins[InsIdx].VT, Elt1);
Justin Holewinskif8f70912013-06-28 17:57:59 +00001543 }
1544
Justin Holewinski44f5c602013-06-28 17:57:53 +00001545 InVals.push_back(Elt0);
1546 InVals.push_back(Elt1);
1547 Ofst += TD->getTypeAllocSize(VecVT.getTypeForEVT(F->getContext()));
1548 InsIdx += 2;
1549 } else {
1550 // V4 loads
1551 // We have at least 4 elements (<3 x Ty> expands to 4 elements) and
1552 // the
1553 // vector will be expanded to a power of 2 elements, so we know we can
1554 // always round up to the next multiple of 4 when creating the vector
1555 // loads.
1556 // e.g. 4 elem => 1 ld.v4
1557 // 6 elem => 2 ld.v4
1558 // 8 elem => 2 ld.v4
1559 // 11 elem => 3 ld.v4
1560 unsigned VecSize = 4;
1561 if (EltVT.getSizeInBits() == 64) {
1562 VecSize = 2;
1563 }
1564 EVT VecVT = EVT::getVectorVT(F->getContext(), EltVT, VecSize);
1565 for (unsigned i = 0; i < NumElts; i += VecSize) {
1566 Value *SrcValue = Constant::getNullValue(
1567 PointerType::get(VecVT.getTypeForEVT(F->getContext()),
1568 llvm::ADDRESS_SPACE_PARAM));
1569 SDValue SrcAddr =
1570 DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg,
1571 DAG.getConstant(Ofst, getPointerTy()));
1572 SDValue P = DAG.getLoad(
1573 VecVT, dl, Root, SrcAddr, MachinePointerInfo(SrcValue), false,
1574 false, true,
1575 TD->getABITypeAlignment(VecVT.getTypeForEVT(F->getContext())));
1576 if (P.getNode())
1577 P.getNode()->setIROrder(idx + 1);
1578
1579 for (unsigned j = 0; j < VecSize; ++j) {
1580 if (i + j >= NumElts)
1581 break;
1582 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, P,
1583 DAG.getIntPtrConstant(j));
Justin Holewinskif8f70912013-06-28 17:57:59 +00001584 if (Ins[InsIdx].VT.getSizeInBits() > EltVT.getSizeInBits())
Justin Holewinskia2911282013-07-01 12:58:58 +00001585 Elt = DAG.getNode(ISD::ANY_EXTEND, dl, Ins[InsIdx].VT, Elt);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001586 InVals.push_back(Elt);
1587 }
1588 Ofst += TD->getTypeAllocSize(VecVT.getTypeForEVT(F->getContext()));
Justin Holewinski44f5c602013-06-28 17:57:53 +00001589 }
Justin Holewinskidff28d22013-07-01 12:59:01 +00001590 InsIdx += VecSize;
Justin Holewinski44f5c602013-06-28 17:57:53 +00001591 }
1592
1593 if (NumElts > 0)
1594 --InsIdx;
1595 continue;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001596 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001597 // A plain scalar.
1598 EVT ObjectVT = getValueType(Ty);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001599 // If ABI, load from the param symbol
1600 SDValue Arg = getParamSymbol(DAG, idx, getPointerTy());
1601 Value *srcValue = Constant::getNullValue(PointerType::get(
1602 ObjectVT.getTypeForEVT(F->getContext()), llvm::ADDRESS_SPACE_PARAM));
Justin Holewinskif8f70912013-06-28 17:57:59 +00001603 SDValue p;
Justin Holewinskia2911282013-07-01 12:58:58 +00001604 if (ObjectVT.getSizeInBits() < Ins[InsIdx].VT.getSizeInBits()) {
1605 ISD::LoadExtType ExtOp = Ins[InsIdx].Flags.isSExt() ?
1606 ISD::SEXTLOAD : ISD::ZEXTLOAD;
1607 p = DAG.getExtLoad(ExtOp, dl, Ins[InsIdx].VT, Root, Arg,
Justin Holewinskif8f70912013-06-28 17:57:59 +00001608 MachinePointerInfo(srcValue), ObjectVT, false, false,
Justin Holewinskia2911282013-07-01 12:58:58 +00001609 TD->getABITypeAlignment(ObjectVT.getTypeForEVT(F->getContext())));
1610 } else {
Justin Holewinskif8f70912013-06-28 17:57:59 +00001611 p = DAG.getLoad(Ins[InsIdx].VT, dl, Root, Arg,
1612 MachinePointerInfo(srcValue), false, false, false,
Justin Holewinskia2911282013-07-01 12:58:58 +00001613 TD->getABITypeAlignment(ObjectVT.getTypeForEVT(F->getContext())));
1614 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001615 if (p.getNode())
1616 p.getNode()->setIROrder(idx + 1);
1617 InVals.push_back(p);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001618 continue;
1619 }
1620
1621 // Param has ByVal attribute
Justin Holewinski44f5c602013-06-28 17:57:53 +00001622 // Return MoveParam(param symbol).
1623 // Ideally, the param symbol can be returned directly,
1624 // but when SDNode builder decides to use it in a CopyToReg(),
1625 // machine instruction fails because TargetExternalSymbol
1626 // (not lowered) is target dependent, and CopyToReg assumes
1627 // the source is lowered.
1628 EVT ObjectVT = getValueType(Ty);
1629 assert(ObjectVT == Ins[InsIdx].VT &&
1630 "Ins type did not match function type");
1631 SDValue Arg = getParamSymbol(DAG, idx, getPointerTy());
1632 SDValue p = DAG.getNode(NVPTXISD::MoveParam, dl, ObjectVT, Arg);
1633 if (p.getNode())
1634 p.getNode()->setIROrder(idx + 1);
1635 if (isKernel)
1636 InVals.push_back(p);
1637 else {
1638 SDValue p2 = DAG.getNode(
1639 ISD::INTRINSIC_WO_CHAIN, dl, ObjectVT,
1640 DAG.getConstant(Intrinsic::nvvm_ptr_local_to_gen, MVT::i32), p);
1641 InVals.push_back(p2);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001642 }
1643 }
1644
1645 // Clang will check explicit VarArg and issue error if any. However, Clang
1646 // will let code with
Justin Holewinski44f5c602013-06-28 17:57:53 +00001647 // implicit var arg like f() pass. See bug 617733.
Justin Holewinskiae556d32012-05-04 20:18:50 +00001648 // We treat this case as if the arg list is empty.
Justin Holewinski44f5c602013-06-28 17:57:53 +00001649 // if (F.isVarArg()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001650 // assert(0 && "VarArg not supported yet!");
1651 //}
1652
1653 if (!OutChains.empty())
Justin Holewinski0497ab12013-03-30 14:29:21 +00001654 DAG.setRoot(DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &OutChains[0],
1655 OutChains.size()));
Justin Holewinskiae556d32012-05-04 20:18:50 +00001656
1657 return Chain;
1658}
1659
Justin Holewinski44f5c602013-06-28 17:57:53 +00001660
Justin Holewinski120baee2013-06-28 17:57:55 +00001661SDValue
1662NVPTXTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1663 bool isVarArg,
1664 const SmallVectorImpl<ISD::OutputArg> &Outs,
1665 const SmallVectorImpl<SDValue> &OutVals,
1666 SDLoc dl, SelectionDAG &DAG) const {
1667 MachineFunction &MF = DAG.getMachineFunction();
1668 const Function *F = MF.getFunction();
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001669 Type *RetTy = F->getReturnType();
Justin Holewinski120baee2013-06-28 17:57:55 +00001670 const DataLayout *TD = getDataLayout();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001671
1672 bool isABI = (nvptxSubtarget.getSmVersion() >= 20);
Justin Holewinski120baee2013-06-28 17:57:55 +00001673 assert(isABI && "Non-ABI compilation is not supported");
1674 if (!isABI)
1675 return Chain;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001676
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001677 if (VectorType *VTy = dyn_cast<VectorType>(RetTy)) {
Justin Holewinski120baee2013-06-28 17:57:55 +00001678 // If we have a vector type, the OutVals array will be the scalarized
1679 // components and we have combine them into 1 or more vector stores.
1680 unsigned NumElts = VTy->getNumElements();
1681 assert(NumElts == Outs.size() && "Bad scalarization of return value");
1682
Justin Holewinskif8f70912013-06-28 17:57:59 +00001683 // const_cast can be removed in later LLVM versions
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001684 EVT EltVT = getValueType(RetTy).getVectorElementType();
Justin Holewinskif8f70912013-06-28 17:57:59 +00001685 bool NeedExtend = false;
1686 if (EltVT.getSizeInBits() < 16)
1687 NeedExtend = true;
1688
Justin Holewinski120baee2013-06-28 17:57:55 +00001689 // V1 store
1690 if (NumElts == 1) {
1691 SDValue StoreVal = OutVals[0];
1692 // We only have one element, so just directly store it
Justin Holewinskif8f70912013-06-28 17:57:59 +00001693 if (NeedExtend)
1694 StoreVal = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal);
1695 SDValue Ops[] = { Chain, DAG.getConstant(0, MVT::i32), StoreVal };
1696 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreRetval, dl,
1697 DAG.getVTList(MVT::Other), &Ops[0], 3,
1698 EltVT, MachinePointerInfo());
1699
Justin Holewinski120baee2013-06-28 17:57:55 +00001700 } else if (NumElts == 2) {
1701 // V2 store
1702 SDValue StoreVal0 = OutVals[0];
1703 SDValue StoreVal1 = OutVals[1];
1704
Justin Holewinskif8f70912013-06-28 17:57:59 +00001705 if (NeedExtend) {
1706 StoreVal0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal0);
1707 StoreVal1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal1);
Justin Holewinski120baee2013-06-28 17:57:55 +00001708 }
1709
Justin Holewinskif8f70912013-06-28 17:57:59 +00001710 SDValue Ops[] = { Chain, DAG.getConstant(0, MVT::i32), StoreVal0,
1711 StoreVal1 };
1712 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreRetvalV2, dl,
1713 DAG.getVTList(MVT::Other), &Ops[0], 4,
1714 EltVT, MachinePointerInfo());
Justin Holewinski120baee2013-06-28 17:57:55 +00001715 } else {
1716 // V4 stores
1717 // We have at least 4 elements (<3 x Ty> expands to 4 elements) and the
1718 // vector will be expanded to a power of 2 elements, so we know we can
1719 // always round up to the next multiple of 4 when creating the vector
1720 // stores.
1721 // e.g. 4 elem => 1 st.v4
1722 // 6 elem => 2 st.v4
1723 // 8 elem => 2 st.v4
1724 // 11 elem => 3 st.v4
1725
1726 unsigned VecSize = 4;
1727 if (OutVals[0].getValueType().getSizeInBits() == 64)
1728 VecSize = 2;
1729
1730 unsigned Offset = 0;
1731
1732 EVT VecVT =
1733 EVT::getVectorVT(F->getContext(), OutVals[0].getValueType(), VecSize);
1734 unsigned PerStoreOffset =
1735 TD->getTypeAllocSize(VecVT.getTypeForEVT(F->getContext()));
1736
Justin Holewinski120baee2013-06-28 17:57:55 +00001737 for (unsigned i = 0; i < NumElts; i += VecSize) {
1738 // Get values
1739 SDValue StoreVal;
1740 SmallVector<SDValue, 8> Ops;
1741 Ops.push_back(Chain);
1742 Ops.push_back(DAG.getConstant(Offset, MVT::i32));
1743 unsigned Opc = NVPTXISD::StoreRetvalV2;
Justin Holewinskif8f70912013-06-28 17:57:59 +00001744 EVT ExtendedVT = (NeedExtend) ? MVT::i16 : OutVals[0].getValueType();
Justin Holewinski120baee2013-06-28 17:57:55 +00001745
1746 StoreVal = OutVals[i];
Justin Holewinskif8f70912013-06-28 17:57:59 +00001747 if (NeedExtend)
1748 StoreVal = DAG.getNode(ISD::ZERO_EXTEND, dl, ExtendedVT, StoreVal);
Justin Holewinski120baee2013-06-28 17:57:55 +00001749 Ops.push_back(StoreVal);
1750
1751 if (i + 1 < NumElts) {
1752 StoreVal = OutVals[i + 1];
Justin Holewinskif8f70912013-06-28 17:57:59 +00001753 if (NeedExtend)
1754 StoreVal = DAG.getNode(ISD::ZERO_EXTEND, dl, ExtendedVT, StoreVal);
Justin Holewinski120baee2013-06-28 17:57:55 +00001755 } else {
1756 StoreVal = DAG.getUNDEF(ExtendedVT);
1757 }
1758 Ops.push_back(StoreVal);
1759
1760 if (VecSize == 4) {
1761 Opc = NVPTXISD::StoreRetvalV4;
1762 if (i + 2 < NumElts) {
1763 StoreVal = OutVals[i + 2];
Justin Holewinskif8f70912013-06-28 17:57:59 +00001764 if (NeedExtend)
1765 StoreVal =
1766 DAG.getNode(ISD::ZERO_EXTEND, dl, ExtendedVT, StoreVal);
Justin Holewinski120baee2013-06-28 17:57:55 +00001767 } else {
1768 StoreVal = DAG.getUNDEF(ExtendedVT);
1769 }
1770 Ops.push_back(StoreVal);
1771
1772 if (i + 3 < NumElts) {
1773 StoreVal = OutVals[i + 3];
Justin Holewinskif8f70912013-06-28 17:57:59 +00001774 if (NeedExtend)
1775 StoreVal =
1776 DAG.getNode(ISD::ZERO_EXTEND, dl, ExtendedVT, StoreVal);
Justin Holewinski120baee2013-06-28 17:57:55 +00001777 } else {
1778 StoreVal = DAG.getUNDEF(ExtendedVT);
1779 }
1780 Ops.push_back(StoreVal);
1781 }
1782
Justin Holewinskif8f70912013-06-28 17:57:59 +00001783 // Chain = DAG.getNode(Opc, dl, MVT::Other, &Ops[0], Ops.size());
1784 Chain =
1785 DAG.getMemIntrinsicNode(Opc, dl, DAG.getVTList(MVT::Other), &Ops[0],
1786 Ops.size(), EltVT, MachinePointerInfo());
Justin Holewinski120baee2013-06-28 17:57:55 +00001787 Offset += PerStoreOffset;
1788 }
1789 }
1790 } else {
Justin Holewinskif8f70912013-06-28 17:57:59 +00001791 SmallVector<EVT, 16> ValVTs;
1792 // const_cast is necessary since we are still using an LLVM version from
1793 // before the type system re-write.
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001794 ComputePTXValueVTs(*this, RetTy, ValVTs);
Justin Holewinskif8f70912013-06-28 17:57:59 +00001795 assert(ValVTs.size() == OutVals.size() && "Bad return value decomposition");
1796
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001797 unsigned SizeSoFar = 0;
Justin Holewinski120baee2013-06-28 17:57:55 +00001798 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1799 SDValue theVal = OutVals[i];
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001800 EVT TheValType = theVal.getValueType();
Justin Holewinski120baee2013-06-28 17:57:55 +00001801 unsigned numElems = 1;
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001802 if (TheValType.isVector())
1803 numElems = TheValType.getVectorNumElements();
Justin Holewinski120baee2013-06-28 17:57:55 +00001804 for (unsigned j = 0, je = numElems; j != je; ++j) {
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001805 SDValue TmpVal = theVal;
1806 if (TheValType.isVector())
1807 TmpVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
1808 TheValType.getVectorElementType(), TmpVal,
Justin Holewinski120baee2013-06-28 17:57:55 +00001809 DAG.getIntPtrConstant(j));
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001810 EVT TheStoreType = ValVTs[i];
1811 if (RetTy->isIntegerTy() &&
1812 TD->getTypeAllocSizeInBits(RetTy) < 32) {
1813 // The following zero-extension is for integer types only, and
1814 // specifically not for aggregates.
1815 TmpVal = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, TmpVal);
1816 TheStoreType = MVT::i32;
1817 }
1818 else if (TmpVal.getValueType().getSizeInBits() < 16)
1819 TmpVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i16, TmpVal);
1820
1821 SDValue Ops[] = { Chain, DAG.getConstant(SizeSoFar, MVT::i32), TmpVal };
Justin Holewinskif8f70912013-06-28 17:57:59 +00001822 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreRetval, dl,
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001823 DAG.getVTList(MVT::Other), &Ops[0],
1824 3, TheStoreType,
1825 MachinePointerInfo());
1826 if(TheValType.isVector())
1827 SizeSoFar +=
1828 TheStoreType.getVectorElementType().getStoreSizeInBits() / 8;
Justin Holewinski120baee2013-06-28 17:57:55 +00001829 else
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001830 SizeSoFar += TheStoreType.getStoreSizeInBits()/8;
Justin Holewinski120baee2013-06-28 17:57:55 +00001831 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001832 }
1833 }
1834
1835 return DAG.getNode(NVPTXISD::RET_FLAG, dl, MVT::Other, Chain);
1836}
1837
Justin Holewinskif8f70912013-06-28 17:57:59 +00001838
Justin Holewinski0497ab12013-03-30 14:29:21 +00001839void NVPTXTargetLowering::LowerAsmOperandForConstraint(
1840 SDValue Op, std::string &Constraint, std::vector<SDValue> &Ops,
1841 SelectionDAG &DAG) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001842 if (Constraint.length() > 1)
1843 return;
1844 else
1845 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
1846}
1847
1848// NVPTX suuport vector of legal types of any length in Intrinsics because the
1849// NVPTX specific type legalizer
1850// will legalize them to the PTX supported length.
Justin Holewinski0497ab12013-03-30 14:29:21 +00001851bool NVPTXTargetLowering::isTypeSupportedInIntrinsic(MVT VT) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001852 if (isTypeLegal(VT))
1853 return true;
1854 if (VT.isVector()) {
1855 MVT eVT = VT.getVectorElementType();
1856 if (isTypeLegal(eVT))
1857 return true;
1858 }
1859 return false;
1860}
1861
Justin Holewinskiae556d32012-05-04 20:18:50 +00001862// llvm.ptx.memcpy.const and llvm.ptx.memmove.const need to be modeled as
1863// TgtMemIntrinsic
1864// because we need the information that is only available in the "Value" type
1865// of destination
1866// pointer. In particular, the address space information.
Justin Holewinski0497ab12013-03-30 14:29:21 +00001867bool NVPTXTargetLowering::getTgtMemIntrinsic(
1868 IntrinsicInfo &Info, const CallInst &I, unsigned Intrinsic) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001869 switch (Intrinsic) {
1870 default:
1871 return false;
1872
1873 case Intrinsic::nvvm_atomic_load_add_f32:
1874 Info.opc = ISD::INTRINSIC_W_CHAIN;
1875 Info.memVT = MVT::f32;
1876 Info.ptrVal = I.getArgOperand(0);
1877 Info.offset = 0;
1878 Info.vol = 0;
1879 Info.readMem = true;
1880 Info.writeMem = true;
1881 Info.align = 0;
1882 return true;
1883
1884 case Intrinsic::nvvm_atomic_load_inc_32:
1885 case Intrinsic::nvvm_atomic_load_dec_32:
1886 Info.opc = ISD::INTRINSIC_W_CHAIN;
1887 Info.memVT = MVT::i32;
1888 Info.ptrVal = I.getArgOperand(0);
1889 Info.offset = 0;
1890 Info.vol = 0;
1891 Info.readMem = true;
1892 Info.writeMem = true;
1893 Info.align = 0;
1894 return true;
1895
1896 case Intrinsic::nvvm_ldu_global_i:
1897 case Intrinsic::nvvm_ldu_global_f:
1898 case Intrinsic::nvvm_ldu_global_p:
1899
1900 Info.opc = ISD::INTRINSIC_W_CHAIN;
1901 if (Intrinsic == Intrinsic::nvvm_ldu_global_i)
Justin Holewinskif8f70912013-06-28 17:57:59 +00001902 Info.memVT = getValueType(I.getType());
Justin Holewinskiae556d32012-05-04 20:18:50 +00001903 else if (Intrinsic == Intrinsic::nvvm_ldu_global_p)
Justin Holewinskif8f70912013-06-28 17:57:59 +00001904 Info.memVT = getValueType(I.getType());
Justin Holewinskiae556d32012-05-04 20:18:50 +00001905 else
1906 Info.memVT = MVT::f32;
1907 Info.ptrVal = I.getArgOperand(0);
1908 Info.offset = 0;
1909 Info.vol = 0;
1910 Info.readMem = true;
1911 Info.writeMem = false;
1912 Info.align = 0;
1913 return true;
1914
1915 }
1916 return false;
1917}
1918
1919/// isLegalAddressingMode - Return true if the addressing mode represented
1920/// by AM is legal for this target, for a load/store of the specified type.
1921/// Used to guide target specific optimizations, like loop strength reduction
1922/// (LoopStrengthReduce.cpp) and memory optimization for address mode
1923/// (CodeGenPrepare.cpp)
Justin Holewinski0497ab12013-03-30 14:29:21 +00001924bool NVPTXTargetLowering::isLegalAddressingMode(const AddrMode &AM,
1925 Type *Ty) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001926
1927 // AddrMode - This represents an addressing mode of:
1928 // BaseGV + BaseOffs + BaseReg + Scale*ScaleReg
1929 //
1930 // The legal address modes are
1931 // - [avar]
1932 // - [areg]
1933 // - [areg+immoff]
1934 // - [immAddr]
1935
1936 if (AM.BaseGV) {
1937 if (AM.BaseOffs || AM.HasBaseReg || AM.Scale)
1938 return false;
1939 return true;
1940 }
1941
1942 switch (AM.Scale) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001943 case 0: // "r", "r+i" or "i" is allowed
Justin Holewinskiae556d32012-05-04 20:18:50 +00001944 break;
1945 case 1:
Justin Holewinski0497ab12013-03-30 14:29:21 +00001946 if (AM.HasBaseReg) // "r+r+i" or "r+r" is not allowed.
Justin Holewinskiae556d32012-05-04 20:18:50 +00001947 return false;
1948 // Otherwise we have r+i.
1949 break;
1950 default:
1951 // No scale > 1 is allowed
1952 return false;
1953 }
1954 return true;
1955}
1956
1957//===----------------------------------------------------------------------===//
1958// NVPTX Inline Assembly Support
1959//===----------------------------------------------------------------------===//
1960
1961/// getConstraintType - Given a constraint letter, return the type of
1962/// constraint it is for this target.
1963NVPTXTargetLowering::ConstraintType
1964NVPTXTargetLowering::getConstraintType(const std::string &Constraint) const {
1965 if (Constraint.size() == 1) {
1966 switch (Constraint[0]) {
1967 default:
1968 break;
1969 case 'r':
1970 case 'h':
1971 case 'c':
1972 case 'l':
1973 case 'f':
1974 case 'd':
1975 case '0':
1976 case 'N':
1977 return C_RegisterClass;
1978 }
1979 }
1980 return TargetLowering::getConstraintType(Constraint);
1981}
1982
Justin Holewinski0497ab12013-03-30 14:29:21 +00001983std::pair<unsigned, const TargetRegisterClass *>
Justin Holewinskiae556d32012-05-04 20:18:50 +00001984NVPTXTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00001985 MVT VT) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001986 if (Constraint.size() == 1) {
1987 switch (Constraint[0]) {
1988 case 'c':
Justin Holewinskif8f70912013-06-28 17:57:59 +00001989 return std::make_pair(0U, &NVPTX::Int16RegsRegClass);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001990 case 'h':
1991 return std::make_pair(0U, &NVPTX::Int16RegsRegClass);
1992 case 'r':
1993 return std::make_pair(0U, &NVPTX::Int32RegsRegClass);
1994 case 'l':
1995 case 'N':
1996 return std::make_pair(0U, &NVPTX::Int64RegsRegClass);
1997 case 'f':
1998 return std::make_pair(0U, &NVPTX::Float32RegsRegClass);
1999 case 'd':
2000 return std::make_pair(0U, &NVPTX::Float64RegsRegClass);
2001 }
2002 }
2003 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2004}
2005
Justin Holewinskiae556d32012-05-04 20:18:50 +00002006/// getFunctionAlignment - Return the Log2 alignment of this function.
2007unsigned NVPTXTargetLowering::getFunctionAlignment(const Function *) const {
2008 return 4;
2009}
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002010
2011/// ReplaceVectorLoad - Convert vector loads into multi-output scalar loads.
2012static void ReplaceLoadVector(SDNode *N, SelectionDAG &DAG,
Justin Holewinski0497ab12013-03-30 14:29:21 +00002013 SmallVectorImpl<SDValue> &Results) {
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002014 EVT ResVT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002015 SDLoc DL(N);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002016
2017 assert(ResVT.isVector() && "Vector load must have vector type");
2018
2019 // We only handle "native" vector sizes for now, e.g. <4 x double> is not
2020 // legal. We can (and should) split that into 2 loads of <2 x double> here
2021 // but I'm leaving that as a TODO for now.
2022 assert(ResVT.isSimple() && "Can only handle simple types");
2023 switch (ResVT.getSimpleVT().SimpleTy) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00002024 default:
2025 return;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002026 case MVT::v2i8:
2027 case MVT::v2i16:
2028 case MVT::v2i32:
2029 case MVT::v2i64:
2030 case MVT::v2f32:
2031 case MVT::v2f64:
2032 case MVT::v4i8:
2033 case MVT::v4i16:
2034 case MVT::v4i32:
2035 case MVT::v4f32:
2036 // This is a "native" vector type
2037 break;
2038 }
2039
2040 EVT EltVT = ResVT.getVectorElementType();
2041 unsigned NumElts = ResVT.getVectorNumElements();
2042
2043 // Since LoadV2 is a target node, we cannot rely on DAG type legalization.
2044 // Therefore, we must ensure the type is legal. For i1 and i8, we set the
2045 // loaded type to i16 and propogate the "real" type as the memory type.
2046 bool NeedTrunc = false;
2047 if (EltVT.getSizeInBits() < 16) {
2048 EltVT = MVT::i16;
2049 NeedTrunc = true;
2050 }
2051
2052 unsigned Opcode = 0;
2053 SDVTList LdResVTs;
2054
2055 switch (NumElts) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00002056 default:
2057 return;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002058 case 2:
2059 Opcode = NVPTXISD::LoadV2;
2060 LdResVTs = DAG.getVTList(EltVT, EltVT, MVT::Other);
2061 break;
2062 case 4: {
2063 Opcode = NVPTXISD::LoadV4;
2064 EVT ListVTs[] = { EltVT, EltVT, EltVT, EltVT, MVT::Other };
2065 LdResVTs = DAG.getVTList(ListVTs, 5);
2066 break;
2067 }
2068 }
2069
2070 SmallVector<SDValue, 8> OtherOps;
2071
2072 // Copy regular operands
2073 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2074 OtherOps.push_back(N->getOperand(i));
2075
2076 LoadSDNode *LD = cast<LoadSDNode>(N);
2077
2078 // The select routine does not have access to the LoadSDNode instance, so
2079 // pass along the extension information
2080 OtherOps.push_back(DAG.getIntPtrConstant(LD->getExtensionType()));
2081
2082 SDValue NewLD = DAG.getMemIntrinsicNode(Opcode, DL, LdResVTs, &OtherOps[0],
2083 OtherOps.size(), LD->getMemoryVT(),
2084 LD->getMemOperand());
2085
2086 SmallVector<SDValue, 4> ScalarRes;
2087
2088 for (unsigned i = 0; i < NumElts; ++i) {
2089 SDValue Res = NewLD.getValue(i);
2090 if (NeedTrunc)
2091 Res = DAG.getNode(ISD::TRUNCATE, DL, ResVT.getVectorElementType(), Res);
2092 ScalarRes.push_back(Res);
2093 }
2094
2095 SDValue LoadChain = NewLD.getValue(NumElts);
2096
Justin Holewinski0497ab12013-03-30 14:29:21 +00002097 SDValue BuildVec =
2098 DAG.getNode(ISD::BUILD_VECTOR, DL, ResVT, &ScalarRes[0], NumElts);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002099
2100 Results.push_back(BuildVec);
2101 Results.push_back(LoadChain);
2102}
2103
Justin Holewinski0497ab12013-03-30 14:29:21 +00002104static void ReplaceINTRINSIC_W_CHAIN(SDNode *N, SelectionDAG &DAG,
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002105 SmallVectorImpl<SDValue> &Results) {
2106 SDValue Chain = N->getOperand(0);
2107 SDValue Intrin = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002108 SDLoc DL(N);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002109
2110 // Get the intrinsic ID
2111 unsigned IntrinNo = cast<ConstantSDNode>(Intrin.getNode())->getZExtValue();
Justin Holewinski0497ab12013-03-30 14:29:21 +00002112 switch (IntrinNo) {
2113 default:
2114 return;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002115 case Intrinsic::nvvm_ldg_global_i:
2116 case Intrinsic::nvvm_ldg_global_f:
2117 case Intrinsic::nvvm_ldg_global_p:
2118 case Intrinsic::nvvm_ldu_global_i:
2119 case Intrinsic::nvvm_ldu_global_f:
2120 case Intrinsic::nvvm_ldu_global_p: {
2121 EVT ResVT = N->getValueType(0);
2122
2123 if (ResVT.isVector()) {
2124 // Vector LDG/LDU
2125
2126 unsigned NumElts = ResVT.getVectorNumElements();
2127 EVT EltVT = ResVT.getVectorElementType();
2128
Justin Holewinskif8f70912013-06-28 17:57:59 +00002129 // Since LDU/LDG are target nodes, we cannot rely on DAG type
2130 // legalization.
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002131 // Therefore, we must ensure the type is legal. For i1 and i8, we set the
2132 // loaded type to i16 and propogate the "real" type as the memory type.
2133 bool NeedTrunc = false;
2134 if (EltVT.getSizeInBits() < 16) {
2135 EltVT = MVT::i16;
2136 NeedTrunc = true;
2137 }
2138
2139 unsigned Opcode = 0;
2140 SDVTList LdResVTs;
2141
2142 switch (NumElts) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00002143 default:
2144 return;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002145 case 2:
Justin Holewinski0497ab12013-03-30 14:29:21 +00002146 switch (IntrinNo) {
2147 default:
2148 return;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002149 case Intrinsic::nvvm_ldg_global_i:
2150 case Intrinsic::nvvm_ldg_global_f:
2151 case Intrinsic::nvvm_ldg_global_p:
2152 Opcode = NVPTXISD::LDGV2;
2153 break;
2154 case Intrinsic::nvvm_ldu_global_i:
2155 case Intrinsic::nvvm_ldu_global_f:
2156 case Intrinsic::nvvm_ldu_global_p:
2157 Opcode = NVPTXISD::LDUV2;
2158 break;
2159 }
2160 LdResVTs = DAG.getVTList(EltVT, EltVT, MVT::Other);
2161 break;
2162 case 4: {
Justin Holewinski0497ab12013-03-30 14:29:21 +00002163 switch (IntrinNo) {
2164 default:
2165 return;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002166 case Intrinsic::nvvm_ldg_global_i:
2167 case Intrinsic::nvvm_ldg_global_f:
2168 case Intrinsic::nvvm_ldg_global_p:
2169 Opcode = NVPTXISD::LDGV4;
2170 break;
2171 case Intrinsic::nvvm_ldu_global_i:
2172 case Intrinsic::nvvm_ldu_global_f:
2173 case Intrinsic::nvvm_ldu_global_p:
2174 Opcode = NVPTXISD::LDUV4;
2175 break;
2176 }
2177 EVT ListVTs[] = { EltVT, EltVT, EltVT, EltVT, MVT::Other };
2178 LdResVTs = DAG.getVTList(ListVTs, 5);
2179 break;
2180 }
2181 }
2182
2183 SmallVector<SDValue, 8> OtherOps;
2184
2185 // Copy regular operands
2186
2187 OtherOps.push_back(Chain); // Chain
Justin Holewinski0497ab12013-03-30 14:29:21 +00002188 // Skip operand 1 (intrinsic ID)
Justin Holewinskif8f70912013-06-28 17:57:59 +00002189 // Others
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002190 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i)
2191 OtherOps.push_back(N->getOperand(i));
2192
2193 MemIntrinsicSDNode *MemSD = cast<MemIntrinsicSDNode>(N);
2194
Justin Holewinski0497ab12013-03-30 14:29:21 +00002195 SDValue NewLD = DAG.getMemIntrinsicNode(
2196 Opcode, DL, LdResVTs, &OtherOps[0], OtherOps.size(),
2197 MemSD->getMemoryVT(), MemSD->getMemOperand());
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002198
2199 SmallVector<SDValue, 4> ScalarRes;
2200
2201 for (unsigned i = 0; i < NumElts; ++i) {
2202 SDValue Res = NewLD.getValue(i);
2203 if (NeedTrunc)
Justin Holewinski0497ab12013-03-30 14:29:21 +00002204 Res =
2205 DAG.getNode(ISD::TRUNCATE, DL, ResVT.getVectorElementType(), Res);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002206 ScalarRes.push_back(Res);
2207 }
2208
2209 SDValue LoadChain = NewLD.getValue(NumElts);
2210
Justin Holewinski0497ab12013-03-30 14:29:21 +00002211 SDValue BuildVec =
2212 DAG.getNode(ISD::BUILD_VECTOR, DL, ResVT, &ScalarRes[0], NumElts);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002213
2214 Results.push_back(BuildVec);
2215 Results.push_back(LoadChain);
2216 } else {
2217 // i8 LDG/LDU
2218 assert(ResVT.isSimple() && ResVT.getSimpleVT().SimpleTy == MVT::i8 &&
2219 "Custom handling of non-i8 ldu/ldg?");
2220
2221 // Just copy all operands as-is
2222 SmallVector<SDValue, 4> Ops;
2223 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2224 Ops.push_back(N->getOperand(i));
2225
2226 // Force output to i16
2227 SDVTList LdResVTs = DAG.getVTList(MVT::i16, MVT::Other);
2228
2229 MemIntrinsicSDNode *MemSD = cast<MemIntrinsicSDNode>(N);
2230
2231 // We make sure the memory type is i8, which will be used during isel
2232 // to select the proper instruction.
Justin Holewinski0497ab12013-03-30 14:29:21 +00002233 SDValue NewLD =
2234 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, DL, LdResVTs, &Ops[0],
2235 Ops.size(), MVT::i8, MemSD->getMemOperand());
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002236
Justin Holewinskie8c93e32013-07-01 12:58:48 +00002237 Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i8,
2238 NewLD.getValue(0)));
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002239 Results.push_back(NewLD.getValue(1));
2240 }
2241 }
2242 }
2243}
2244
Justin Holewinski0497ab12013-03-30 14:29:21 +00002245void NVPTXTargetLowering::ReplaceNodeResults(
2246 SDNode *N, SmallVectorImpl<SDValue> &Results, SelectionDAG &DAG) const {
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002247 switch (N->getOpcode()) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00002248 default:
2249 report_fatal_error("Unhandled custom legalization");
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002250 case ISD::LOAD:
2251 ReplaceLoadVector(N, DAG, Results);
2252 return;
2253 case ISD::INTRINSIC_W_CHAIN:
2254 ReplaceINTRINSIC_W_CHAIN(N, DAG, Results);
2255 return;
2256 }
2257}