blob: 04fb784b8536fa99c8846edab8f79f0d51df2432 [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";
255 case NVPTXISD::NVBuiltin:
256 return "NVPTXISD::NVBuiltin";
257 case NVPTXISD::DeclareParam:
258 return "NVPTXISD::DeclareParam";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000259 case NVPTXISD::DeclareScalarParam:
260 return "NVPTXISD::DeclareScalarParam";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000261 case NVPTXISD::DeclareRet:
262 return "NVPTXISD::DeclareRet";
263 case NVPTXISD::DeclareRetParam:
264 return "NVPTXISD::DeclareRetParam";
265 case NVPTXISD::PrintCall:
266 return "NVPTXISD::PrintCall";
267 case NVPTXISD::LoadParam:
268 return "NVPTXISD::LoadParam";
Justin Holewinskife44314f2013-06-28 17:57:51 +0000269 case NVPTXISD::LoadParamV2:
270 return "NVPTXISD::LoadParamV2";
271 case NVPTXISD::LoadParamV4:
272 return "NVPTXISD::LoadParamV4";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000273 case NVPTXISD::StoreParam:
274 return "NVPTXISD::StoreParam";
Justin Holewinskife44314f2013-06-28 17:57:51 +0000275 case NVPTXISD::StoreParamV2:
276 return "NVPTXISD::StoreParamV2";
277 case NVPTXISD::StoreParamV4:
278 return "NVPTXISD::StoreParamV4";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000279 case NVPTXISD::StoreParamS32:
280 return "NVPTXISD::StoreParamS32";
281 case NVPTXISD::StoreParamU32:
282 return "NVPTXISD::StoreParamU32";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000283 case NVPTXISD::CallArgBegin:
284 return "NVPTXISD::CallArgBegin";
285 case NVPTXISD::CallArg:
286 return "NVPTXISD::CallArg";
287 case NVPTXISD::LastCallArg:
288 return "NVPTXISD::LastCallArg";
289 case NVPTXISD::CallArgEnd:
290 return "NVPTXISD::CallArgEnd";
291 case NVPTXISD::CallVoid:
292 return "NVPTXISD::CallVoid";
293 case NVPTXISD::CallVal:
294 return "NVPTXISD::CallVal";
295 case NVPTXISD::CallSymbol:
296 return "NVPTXISD::CallSymbol";
297 case NVPTXISD::Prototype:
298 return "NVPTXISD::Prototype";
299 case NVPTXISD::MoveParam:
300 return "NVPTXISD::MoveParam";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000301 case NVPTXISD::StoreRetval:
302 return "NVPTXISD::StoreRetval";
Justin Holewinskife44314f2013-06-28 17:57:51 +0000303 case NVPTXISD::StoreRetvalV2:
304 return "NVPTXISD::StoreRetvalV2";
305 case NVPTXISD::StoreRetvalV4:
306 return "NVPTXISD::StoreRetvalV4";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000307 case NVPTXISD::PseudoUseParam:
308 return "NVPTXISD::PseudoUseParam";
309 case NVPTXISD::RETURN:
310 return "NVPTXISD::RETURN";
311 case NVPTXISD::CallSeqBegin:
312 return "NVPTXISD::CallSeqBegin";
313 case NVPTXISD::CallSeqEnd:
314 return "NVPTXISD::CallSeqEnd";
315 case NVPTXISD::LoadV2:
316 return "NVPTXISD::LoadV2";
317 case NVPTXISD::LoadV4:
318 return "NVPTXISD::LoadV4";
319 case NVPTXISD::LDGV2:
320 return "NVPTXISD::LDGV2";
321 case NVPTXISD::LDGV4:
322 return "NVPTXISD::LDGV4";
323 case NVPTXISD::LDUV2:
324 return "NVPTXISD::LDUV2";
325 case NVPTXISD::LDUV4:
326 return "NVPTXISD::LDUV4";
327 case NVPTXISD::StoreV2:
328 return "NVPTXISD::StoreV2";
329 case NVPTXISD::StoreV4:
330 return "NVPTXISD::StoreV4";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000331 }
332}
333
Justin Holewinskibc451192012-11-29 14:26:24 +0000334bool NVPTXTargetLowering::shouldSplitVectorElementType(EVT VT) const {
335 return VT == MVT::i1;
336}
Justin Holewinskiae556d32012-05-04 20:18:50 +0000337
338SDValue
339NVPTXTargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000340 SDLoc dl(Op);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000341 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
342 Op = DAG.getTargetGlobalAddress(GV, dl, getPointerTy());
343 return DAG.getNode(NVPTXISD::Wrapper, dl, getPointerTy(), Op);
344}
345
Justin Holewinskif8f70912013-06-28 17:57:59 +0000346std::string
347NVPTXTargetLowering::getPrototype(Type *retTy, const ArgListTy &Args,
348 const SmallVectorImpl<ISD::OutputArg> &Outs,
349 unsigned retAlignment,
350 const ImmutableCallSite *CS) const {
351
352 bool isABI = (nvptxSubtarget.getSmVersion() >= 20);
353 assert(isABI && "Non-ABI compilation is not supported");
354 if (!isABI)
355 return "";
356
357 std::stringstream O;
358 O << "prototype_" << uniqueCallSite << " : .callprototype ";
359
360 if (retTy->getTypeID() == Type::VoidTyID) {
361 O << "()";
362 } else {
363 O << "(";
364 if (retTy->isPrimitiveType() || retTy->isIntegerTy()) {
365 unsigned size = 0;
366 if (const IntegerType *ITy = dyn_cast<IntegerType>(retTy)) {
367 size = ITy->getBitWidth();
368 if (size < 32)
369 size = 32;
370 } else {
371 assert(retTy->isFloatingPointTy() &&
372 "Floating point type expected here");
373 size = retTy->getPrimitiveSizeInBits();
374 }
375
376 O << ".param .b" << size << " _";
377 } else if (isa<PointerType>(retTy)) {
378 O << ".param .b" << getPointerTy().getSizeInBits() << " _";
379 } else {
380 if ((retTy->getTypeID() == Type::StructTyID) || isa<VectorType>(retTy)) {
381 SmallVector<EVT, 16> vtparts;
382 ComputeValueVTs(*this, retTy, vtparts);
383 unsigned totalsz = 0;
384 for (unsigned i = 0, e = vtparts.size(); i != e; ++i) {
385 unsigned elems = 1;
386 EVT elemtype = vtparts[i];
387 if (vtparts[i].isVector()) {
388 elems = vtparts[i].getVectorNumElements();
389 elemtype = vtparts[i].getVectorElementType();
390 }
391 // TODO: no need to loop
392 for (unsigned j = 0, je = elems; j != je; ++j) {
393 unsigned sz = elemtype.getSizeInBits();
394 if (elemtype.isInteger() && (sz < 8))
395 sz = 8;
396 totalsz += sz / 8;
397 }
398 }
399 O << ".param .align " << retAlignment << " .b8 _[" << totalsz << "]";
400 } else {
401 assert(false && "Unknown return type");
402 }
403 }
404 O << ") ";
405 }
406 O << "_ (";
407
408 bool first = true;
409 MVT thePointerTy = getPointerTy();
410
411 unsigned OIdx = 0;
412 for (unsigned i = 0, e = Args.size(); i != e; ++i, ++OIdx) {
413 Type *Ty = Args[i].Ty;
414 if (!first) {
415 O << ", ";
416 }
417 first = false;
418
419 if (Outs[OIdx].Flags.isByVal() == false) {
420 if (Ty->isAggregateType() || Ty->isVectorTy()) {
421 unsigned align = 0;
422 const CallInst *CallI = cast<CallInst>(CS->getInstruction());
423 const DataLayout *TD = getDataLayout();
424 // +1 because index 0 is reserved for return type alignment
425 if (!llvm::getAlign(*CallI, i + 1, align))
426 align = TD->getABITypeAlignment(Ty);
427 unsigned sz = TD->getTypeAllocSize(Ty);
428 O << ".param .align " << align << " .b8 ";
429 O << "_";
430 O << "[" << sz << "]";
431 // update the index for Outs
432 SmallVector<EVT, 16> vtparts;
433 ComputeValueVTs(*this, Ty, vtparts);
434 if (unsigned len = vtparts.size())
435 OIdx += len - 1;
436 continue;
437 }
Justin Holewinskidff28d22013-07-01 12:59:01 +0000438 // i8 types in IR will be i16 types in SDAG
439 assert((getValueType(Ty) == Outs[OIdx].VT ||
440 (getValueType(Ty) == MVT::i8 && Outs[OIdx].VT == MVT::i16)) &&
Justin Holewinskif8f70912013-06-28 17:57:59 +0000441 "type mismatch between callee prototype and arguments");
442 // scalar type
443 unsigned sz = 0;
444 if (isa<IntegerType>(Ty)) {
445 sz = cast<IntegerType>(Ty)->getBitWidth();
446 if (sz < 32)
447 sz = 32;
448 } else if (isa<PointerType>(Ty))
449 sz = thePointerTy.getSizeInBits();
450 else
451 sz = Ty->getPrimitiveSizeInBits();
452 O << ".param .b" << sz << " ";
453 O << "_";
454 continue;
455 }
456 const PointerType *PTy = dyn_cast<PointerType>(Ty);
457 assert(PTy && "Param with byval attribute should be a pointer type");
458 Type *ETy = PTy->getElementType();
459
460 unsigned align = Outs[OIdx].Flags.getByValAlign();
461 unsigned sz = getDataLayout()->getTypeAllocSize(ETy);
462 O << ".param .align " << align << " .b8 ";
463 O << "_";
464 O << "[" << sz << "]";
465 }
466 O << ");";
467 return O.str();
468}
469
470unsigned
471NVPTXTargetLowering::getArgumentAlignment(SDValue Callee,
472 const ImmutableCallSite *CS,
473 Type *Ty,
474 unsigned Idx) const {
475 const DataLayout *TD = getDataLayout();
476 unsigned align = 0;
477 GlobalAddressSDNode *Func = dyn_cast<GlobalAddressSDNode>(Callee.getNode());
478
479 if (Func) { // direct call
480 assert(CS->getCalledFunction() &&
481 "direct call cannot find callee");
482 if (!llvm::getAlign(*(CS->getCalledFunction()), Idx, align))
483 align = TD->getABITypeAlignment(Ty);
484 }
485 else { // indirect call
486 const CallInst *CallI = dyn_cast<CallInst>(CS->getInstruction());
487 if (!llvm::getAlign(*CallI, Idx, align))
488 align = TD->getABITypeAlignment(Ty);
489 }
490
491 return align;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000492}
493
Justin Holewinski0497ab12013-03-30 14:29:21 +0000494SDValue NVPTXTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
495 SmallVectorImpl<SDValue> &InVals) const {
496 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000497 SDLoc dl = CLI.DL;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000498 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000499 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
500 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
501 SDValue Chain = CLI.Chain;
502 SDValue Callee = CLI.Callee;
503 bool &isTailCall = CLI.IsTailCall;
504 ArgListTy &Args = CLI.Args;
505 Type *retTy = CLI.RetTy;
506 ImmutableCallSite *CS = CLI.CS;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000507
Justin Holewinskiae556d32012-05-04 20:18:50 +0000508 bool isABI = (nvptxSubtarget.getSmVersion() >= 20);
Justin Holewinskif8f70912013-06-28 17:57:59 +0000509 assert(isABI && "Non-ABI compilation is not supported");
510 if (!isABI)
511 return Chain;
512 const DataLayout *TD = getDataLayout();
513 MachineFunction &MF = DAG.getMachineFunction();
514 const Function *F = MF.getFunction();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000515
516 SDValue tempChain = Chain;
Justin Holewinskif8f70912013-06-28 17:57:59 +0000517 Chain =
518 DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(uniqueCallSite, true),
519 dl);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000520 SDValue InFlag = Chain.getValue(1);
521
Justin Holewinskiae556d32012-05-04 20:18:50 +0000522 unsigned paramCount = 0;
Justin Holewinskif8f70912013-06-28 17:57:59 +0000523 // Args.size() and Outs.size() need not match.
524 // Outs.size() will be larger
525 // * if there is an aggregate argument with multiple fields (each field
526 // showing up separately in Outs)
527 // * if there is a vector argument with more than typical vector-length
528 // elements (generally if more than 4) where each vector element is
529 // individually present in Outs.
530 // So a different index should be used for indexing into Outs/OutVals.
531 // See similar issue in LowerFormalArguments.
532 unsigned OIdx = 0;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000533 // Declare the .params or .reg need to pass values
534 // to the function
Justin Holewinskif8f70912013-06-28 17:57:59 +0000535 for (unsigned i = 0, e = Args.size(); i != e; ++i, ++OIdx) {
536 EVT VT = Outs[OIdx].VT;
537 Type *Ty = Args[i].Ty;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000538
Justin Holewinskif8f70912013-06-28 17:57:59 +0000539 if (Outs[OIdx].Flags.isByVal() == false) {
540 if (Ty->isAggregateType()) {
541 // aggregate
542 SmallVector<EVT, 16> vtparts;
543 ComputeValueVTs(*this, Ty, vtparts);
544
545 unsigned align = getArgumentAlignment(Callee, CS, Ty, paramCount + 1);
546 // declare .param .align <align> .b8 .param<n>[<size>];
547 unsigned sz = TD->getTypeAllocSize(Ty);
548 SDVTList DeclareParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
549 SDValue DeclareParamOps[] = { Chain, DAG.getConstant(align, MVT::i32),
550 DAG.getConstant(paramCount, MVT::i32),
551 DAG.getConstant(sz, MVT::i32), InFlag };
552 Chain = DAG.getNode(NVPTXISD::DeclareParam, dl, DeclareParamVTs,
553 DeclareParamOps, 5);
554 InFlag = Chain.getValue(1);
555 unsigned curOffset = 0;
556 for (unsigned j = 0, je = vtparts.size(); j != je; ++j) {
557 unsigned elems = 1;
558 EVT elemtype = vtparts[j];
559 if (vtparts[j].isVector()) {
560 elems = vtparts[j].getVectorNumElements();
561 elemtype = vtparts[j].getVectorElementType();
562 }
563 for (unsigned k = 0, ke = elems; k != ke; ++k) {
564 unsigned sz = elemtype.getSizeInBits();
565 if (elemtype.isInteger() && (sz < 8))
566 sz = 8;
567 SDValue StVal = OutVals[OIdx];
568 if (elemtype.getSizeInBits() < 16) {
Justin Holewinskia2911282013-07-01 12:58:58 +0000569 StVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i16, StVal);
Justin Holewinskif8f70912013-06-28 17:57:59 +0000570 }
571 SDVTList CopyParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
572 SDValue CopyParamOps[] = { Chain,
573 DAG.getConstant(paramCount, MVT::i32),
574 DAG.getConstant(curOffset, MVT::i32),
575 StVal, InFlag };
576 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreParam, dl,
577 CopyParamVTs, &CopyParamOps[0], 5,
578 elemtype, MachinePointerInfo());
579 InFlag = Chain.getValue(1);
580 curOffset += sz / 8;
581 ++OIdx;
582 }
583 }
584 if (vtparts.size() > 0)
585 --OIdx;
586 ++paramCount;
587 continue;
588 }
589 if (Ty->isVectorTy()) {
590 EVT ObjectVT = getValueType(Ty);
591 unsigned align = getArgumentAlignment(Callee, CS, Ty, paramCount + 1);
592 // declare .param .align <align> .b8 .param<n>[<size>];
593 unsigned sz = TD->getTypeAllocSize(Ty);
594 SDVTList DeclareParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
595 SDValue DeclareParamOps[] = { Chain, DAG.getConstant(align, MVT::i32),
596 DAG.getConstant(paramCount, MVT::i32),
597 DAG.getConstant(sz, MVT::i32), InFlag };
598 Chain = DAG.getNode(NVPTXISD::DeclareParam, dl, DeclareParamVTs,
599 DeclareParamOps, 5);
600 InFlag = Chain.getValue(1);
601 unsigned NumElts = ObjectVT.getVectorNumElements();
602 EVT EltVT = ObjectVT.getVectorElementType();
603 EVT MemVT = EltVT;
604 bool NeedExtend = false;
605 if (EltVT.getSizeInBits() < 16) {
606 NeedExtend = true;
607 EltVT = MVT::i16;
608 }
609
610 // V1 store
611 if (NumElts == 1) {
612 SDValue Elt = OutVals[OIdx++];
613 if (NeedExtend)
614 Elt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Elt);
615
616 SDVTList CopyParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
617 SDValue CopyParamOps[] = { Chain,
618 DAG.getConstant(paramCount, MVT::i32),
619 DAG.getConstant(0, MVT::i32), Elt,
620 InFlag };
621 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreParam, dl,
622 CopyParamVTs, &CopyParamOps[0], 5,
623 MemVT, MachinePointerInfo());
624 InFlag = Chain.getValue(1);
625 } else if (NumElts == 2) {
626 SDValue Elt0 = OutVals[OIdx++];
627 SDValue Elt1 = OutVals[OIdx++];
628 if (NeedExtend) {
629 Elt0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Elt0);
630 Elt1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Elt1);
631 }
632
633 SDVTList CopyParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
634 SDValue CopyParamOps[] = { Chain,
635 DAG.getConstant(paramCount, MVT::i32),
636 DAG.getConstant(0, MVT::i32), Elt0, Elt1,
637 InFlag };
638 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreParamV2, dl,
639 CopyParamVTs, &CopyParamOps[0], 6,
640 MemVT, MachinePointerInfo());
641 InFlag = Chain.getValue(1);
642 } else {
643 unsigned curOffset = 0;
644 // V4 stores
645 // We have at least 4 elements (<3 x Ty> expands to 4 elements) and
646 // the
647 // vector will be expanded to a power of 2 elements, so we know we can
648 // always round up to the next multiple of 4 when creating the vector
649 // stores.
650 // e.g. 4 elem => 1 st.v4
651 // 6 elem => 2 st.v4
652 // 8 elem => 2 st.v4
653 // 11 elem => 3 st.v4
654 unsigned VecSize = 4;
655 if (EltVT.getSizeInBits() == 64)
656 VecSize = 2;
657
658 // This is potentially only part of a vector, so assume all elements
659 // are packed together.
660 unsigned PerStoreOffset = MemVT.getStoreSizeInBits() / 8 * VecSize;
661
662 for (unsigned i = 0; i < NumElts; i += VecSize) {
663 // Get values
664 SDValue StoreVal;
665 SmallVector<SDValue, 8> Ops;
666 Ops.push_back(Chain);
667 Ops.push_back(DAG.getConstant(paramCount, MVT::i32));
668 Ops.push_back(DAG.getConstant(curOffset, MVT::i32));
669
670 unsigned Opc = NVPTXISD::StoreParamV2;
671
672 StoreVal = OutVals[OIdx++];
673 if (NeedExtend)
674 StoreVal = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal);
675 Ops.push_back(StoreVal);
676
677 if (i + 1 < NumElts) {
678 StoreVal = OutVals[OIdx++];
679 if (NeedExtend)
680 StoreVal =
681 DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal);
682 } else {
683 StoreVal = DAG.getUNDEF(EltVT);
684 }
685 Ops.push_back(StoreVal);
686
687 if (VecSize == 4) {
688 Opc = NVPTXISD::StoreParamV4;
689 if (i + 2 < NumElts) {
690 StoreVal = OutVals[OIdx++];
691 if (NeedExtend)
692 StoreVal =
693 DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal);
694 } else {
695 StoreVal = DAG.getUNDEF(EltVT);
696 }
697 Ops.push_back(StoreVal);
698
699 if (i + 3 < NumElts) {
700 StoreVal = OutVals[OIdx++];
701 if (NeedExtend)
702 StoreVal =
703 DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal);
704 } else {
705 StoreVal = DAG.getUNDEF(EltVT);
706 }
707 Ops.push_back(StoreVal);
708 }
709
Justin Holewinskidff28d22013-07-01 12:59:01 +0000710 Ops.push_back(InFlag);
711
Justin Holewinskif8f70912013-06-28 17:57:59 +0000712 SDVTList CopyParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
713 Chain = DAG.getMemIntrinsicNode(Opc, dl, CopyParamVTs, &Ops[0],
714 Ops.size(), MemVT,
715 MachinePointerInfo());
716 InFlag = Chain.getValue(1);
717 curOffset += PerStoreOffset;
718 }
719 }
720 ++paramCount;
721 --OIdx;
722 continue;
723 }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000724 // Plain scalar
725 // for ABI, declare .param .b<size> .param<n>;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000726 unsigned sz = VT.getSizeInBits();
Justin Holewinskif8f70912013-06-28 17:57:59 +0000727 bool needExtend = false;
728 if (VT.isInteger()) {
729 if (sz < 16)
730 needExtend = true;
731 if (sz < 32)
732 sz = 32;
733 }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000734 SDVTList DeclareParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
735 SDValue DeclareParamOps[] = { Chain,
736 DAG.getConstant(paramCount, MVT::i32),
737 DAG.getConstant(sz, MVT::i32),
Justin Holewinskif8f70912013-06-28 17:57:59 +0000738 DAG.getConstant(0, MVT::i32), InFlag };
Justin Holewinskiae556d32012-05-04 20:18:50 +0000739 Chain = DAG.getNode(NVPTXISD::DeclareScalarParam, dl, DeclareParamVTs,
740 DeclareParamOps, 5);
741 InFlag = Chain.getValue(1);
Justin Holewinskif8f70912013-06-28 17:57:59 +0000742 SDValue OutV = OutVals[OIdx];
743 if (needExtend) {
744 // zext/sext i1 to i16
745 unsigned opc = ISD::ZERO_EXTEND;
746 if (Outs[OIdx].Flags.isSExt())
747 opc = ISD::SIGN_EXTEND;
748 OutV = DAG.getNode(opc, dl, MVT::i16, OutV);
749 }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000750 SDVTList CopyParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
751 SDValue CopyParamOps[] = { Chain, DAG.getConstant(paramCount, MVT::i32),
Justin Holewinskif8f70912013-06-28 17:57:59 +0000752 DAG.getConstant(0, MVT::i32), OutV, InFlag };
Justin Holewinskiae556d32012-05-04 20:18:50 +0000753
754 unsigned opcode = NVPTXISD::StoreParam;
Justin Holewinskif8f70912013-06-28 17:57:59 +0000755 if (Outs[OIdx].Flags.isZExt())
756 opcode = NVPTXISD::StoreParamU32;
757 else if (Outs[OIdx].Flags.isSExt())
758 opcode = NVPTXISD::StoreParamS32;
759 Chain = DAG.getMemIntrinsicNode(opcode, dl, CopyParamVTs, CopyParamOps, 5,
760 VT, MachinePointerInfo());
Justin Holewinskiae556d32012-05-04 20:18:50 +0000761
762 InFlag = Chain.getValue(1);
763 ++paramCount;
764 continue;
765 }
766 // struct or vector
767 SmallVector<EVT, 16> vtparts;
768 const PointerType *PTy = dyn_cast<PointerType>(Args[i].Ty);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000769 assert(PTy && "Type of a byval parameter should be pointer");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000770 ComputeValueVTs(*this, PTy->getElementType(), vtparts);
771
Justin Holewinskif8f70912013-06-28 17:57:59 +0000772 // declare .param .align <align> .b8 .param<n>[<size>];
773 unsigned sz = Outs[OIdx].Flags.getByValSize();
774 SDVTList DeclareParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
775 // The ByValAlign in the Outs[OIdx].Flags is alway set at this point,
776 // so we don't need to worry about natural alignment or not.
777 // See TargetLowering::LowerCallTo().
778 SDValue DeclareParamOps[] = {
779 Chain, DAG.getConstant(Outs[OIdx].Flags.getByValAlign(), MVT::i32),
780 DAG.getConstant(paramCount, MVT::i32), DAG.getConstant(sz, MVT::i32),
781 InFlag
782 };
783 Chain = DAG.getNode(NVPTXISD::DeclareParam, dl, DeclareParamVTs,
784 DeclareParamOps, 5);
785 InFlag = Chain.getValue(1);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000786 unsigned curOffset = 0;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000787 for (unsigned j = 0, je = vtparts.size(); j != je; ++j) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000788 unsigned elems = 1;
789 EVT elemtype = vtparts[j];
790 if (vtparts[j].isVector()) {
791 elems = vtparts[j].getVectorNumElements();
792 elemtype = vtparts[j].getVectorElementType();
793 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000794 for (unsigned k = 0, ke = elems; k != ke; ++k) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000795 unsigned sz = elemtype.getSizeInBits();
Justin Holewinskif8f70912013-06-28 17:57:59 +0000796 if (elemtype.isInteger() && (sz < 8))
797 sz = 8;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000798 SDValue srcAddr =
Justin Holewinskif8f70912013-06-28 17:57:59 +0000799 DAG.getNode(ISD::ADD, dl, getPointerTy(), OutVals[OIdx],
Justin Holewinski0497ab12013-03-30 14:29:21 +0000800 DAG.getConstant(curOffset, getPointerTy()));
Justin Holewinskif8f70912013-06-28 17:57:59 +0000801 SDValue theVal = DAG.getLoad(elemtype, dl, tempChain, srcAddr,
802 MachinePointerInfo(), false, false, false,
803 0);
804 if (elemtype.getSizeInBits() < 16) {
Justin Holewinskia2911282013-07-01 12:58:58 +0000805 theVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i16, theVal);
Justin Holewinskif8f70912013-06-28 17:57:59 +0000806 }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000807 SDVTList CopyParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
808 SDValue CopyParamOps[] = { Chain, DAG.getConstant(paramCount, MVT::i32),
Justin Holewinskif8f70912013-06-28 17:57:59 +0000809 DAG.getConstant(curOffset, MVT::i32), theVal,
Justin Holewinskiae556d32012-05-04 20:18:50 +0000810 InFlag };
Justin Holewinskif8f70912013-06-28 17:57:59 +0000811 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreParam, dl, CopyParamVTs,
812 CopyParamOps, 5, elemtype,
813 MachinePointerInfo());
814
Justin Holewinskiae556d32012-05-04 20:18:50 +0000815 InFlag = Chain.getValue(1);
Justin Holewinskif8f70912013-06-28 17:57:59 +0000816 curOffset += sz / 8;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000817 }
818 }
Justin Holewinskif8f70912013-06-28 17:57:59 +0000819 ++paramCount;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000820 }
821
822 GlobalAddressSDNode *Func = dyn_cast<GlobalAddressSDNode>(Callee.getNode());
823 unsigned retAlignment = 0;
824
825 // Handle Result
Justin Holewinskiae556d32012-05-04 20:18:50 +0000826 if (Ins.size() > 0) {
827 SmallVector<EVT, 16> resvtparts;
828 ComputeValueVTs(*this, retTy, resvtparts);
829
Justin Holewinskif8f70912013-06-28 17:57:59 +0000830 // Declare
831 // .param .align 16 .b8 retval0[<size-in-bytes>], or
832 // .param .b<size-in-bits> retval0
833 unsigned resultsz = TD->getTypeAllocSizeInBits(retTy);
834 if (retTy->isPrimitiveType() || retTy->isIntegerTy() ||
835 retTy->isPointerTy()) {
836 // Scalar needs to be at least 32bit wide
837 if (resultsz < 32)
838 resultsz = 32;
839 SDVTList DeclareRetVTs = DAG.getVTList(MVT::Other, MVT::Glue);
840 SDValue DeclareRetOps[] = { Chain, DAG.getConstant(1, MVT::i32),
841 DAG.getConstant(resultsz, MVT::i32),
842 DAG.getConstant(0, MVT::i32), InFlag };
843 Chain = DAG.getNode(NVPTXISD::DeclareRet, dl, DeclareRetVTs,
844 DeclareRetOps, 5);
845 InFlag = Chain.getValue(1);
846 } else {
847 retAlignment = getArgumentAlignment(Callee, CS, retTy, 0);
848 SDVTList DeclareRetVTs = DAG.getVTList(MVT::Other, MVT::Glue);
849 SDValue DeclareRetOps[] = { Chain,
850 DAG.getConstant(retAlignment, MVT::i32),
851 DAG.getConstant(resultsz / 8, MVT::i32),
852 DAG.getConstant(0, MVT::i32), InFlag };
853 Chain = DAG.getNode(NVPTXISD::DeclareRetParam, dl, DeclareRetVTs,
854 DeclareRetOps, 5);
855 InFlag = Chain.getValue(1);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000856 }
857 }
858
859 if (!Func) {
860 // This is indirect function call case : PTX requires a prototype of the
861 // form
862 // proto_0 : .callprototype(.param .b32 _) _ (.param .b32 _);
863 // to be emitted, and the label has to used as the last arg of call
864 // instruction.
865 // The prototype is embedded in a string and put as the operand for an
866 // INLINEASM SDNode.
867 SDVTList InlineAsmVTs = DAG.getVTList(MVT::Other, MVT::Glue);
Justin Holewinskif8f70912013-06-28 17:57:59 +0000868 std::string proto_string =
869 getPrototype(retTy, Args, Outs, retAlignment, CS);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000870 const char *asmstr = nvTM->getManagedStrPool()
871 ->getManagedString(proto_string.c_str())->c_str();
872 SDValue InlineAsmOps[] = {
873 Chain, DAG.getTargetExternalSymbol(asmstr, getPointerTy()),
874 DAG.getMDNode(0), DAG.getTargetConstant(0, MVT::i32), InFlag
875 };
Justin Holewinskiae556d32012-05-04 20:18:50 +0000876 Chain = DAG.getNode(ISD::INLINEASM, dl, InlineAsmVTs, InlineAsmOps, 5);
877 InFlag = Chain.getValue(1);
878 }
879 // Op to just print "call"
880 SDVTList PrintCallVTs = DAG.getVTList(MVT::Other, MVT::Glue);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000881 SDValue PrintCallOps[] = {
Justin Holewinskif8f70912013-06-28 17:57:59 +0000882 Chain, DAG.getConstant((Ins.size() == 0) ? 0 : 1, MVT::i32), InFlag
Justin Holewinski0497ab12013-03-30 14:29:21 +0000883 };
884 Chain = DAG.getNode(Func ? (NVPTXISD::PrintCallUni) : (NVPTXISD::PrintCall),
885 dl, PrintCallVTs, PrintCallOps, 3);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000886 InFlag = Chain.getValue(1);
887
888 // Ops to print out the function name
889 SDVTList CallVoidVTs = DAG.getVTList(MVT::Other, MVT::Glue);
890 SDValue CallVoidOps[] = { Chain, Callee, InFlag };
891 Chain = DAG.getNode(NVPTXISD::CallVoid, dl, CallVoidVTs, CallVoidOps, 3);
892 InFlag = Chain.getValue(1);
893
894 // Ops to print out the param list
895 SDVTList CallArgBeginVTs = DAG.getVTList(MVT::Other, MVT::Glue);
896 SDValue CallArgBeginOps[] = { Chain, InFlag };
897 Chain = DAG.getNode(NVPTXISD::CallArgBegin, dl, CallArgBeginVTs,
898 CallArgBeginOps, 2);
899 InFlag = Chain.getValue(1);
900
Justin Holewinski0497ab12013-03-30 14:29:21 +0000901 for (unsigned i = 0, e = paramCount; i != e; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000902 unsigned opcode;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000903 if (i == (e - 1))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000904 opcode = NVPTXISD::LastCallArg;
905 else
906 opcode = NVPTXISD::CallArg;
907 SDVTList CallArgVTs = DAG.getVTList(MVT::Other, MVT::Glue);
908 SDValue CallArgOps[] = { Chain, DAG.getConstant(1, MVT::i32),
Justin Holewinski0497ab12013-03-30 14:29:21 +0000909 DAG.getConstant(i, MVT::i32), InFlag };
Justin Holewinskiae556d32012-05-04 20:18:50 +0000910 Chain = DAG.getNode(opcode, dl, CallArgVTs, CallArgOps, 4);
911 InFlag = Chain.getValue(1);
912 }
913 SDVTList CallArgEndVTs = DAG.getVTList(MVT::Other, MVT::Glue);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000914 SDValue CallArgEndOps[] = { Chain, DAG.getConstant(Func ? 1 : 0, MVT::i32),
Justin Holewinskiae556d32012-05-04 20:18:50 +0000915 InFlag };
Justin Holewinski0497ab12013-03-30 14:29:21 +0000916 Chain =
917 DAG.getNode(NVPTXISD::CallArgEnd, dl, CallArgEndVTs, CallArgEndOps, 3);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000918 InFlag = Chain.getValue(1);
919
920 if (!Func) {
921 SDVTList PrototypeVTs = DAG.getVTList(MVT::Other, MVT::Glue);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000922 SDValue PrototypeOps[] = { Chain, DAG.getConstant(uniqueCallSite, MVT::i32),
Justin Holewinskiae556d32012-05-04 20:18:50 +0000923 InFlag };
924 Chain = DAG.getNode(NVPTXISD::Prototype, dl, PrototypeVTs, PrototypeOps, 3);
925 InFlag = Chain.getValue(1);
926 }
927
928 // Generate loads from param memory/moves from registers for result
929 if (Ins.size() > 0) {
Justin Holewinskif8f70912013-06-28 17:57:59 +0000930 unsigned resoffset = 0;
931 if (retTy && retTy->isVectorTy()) {
932 EVT ObjectVT = getValueType(retTy);
933 unsigned NumElts = ObjectVT.getVectorNumElements();
934 EVT EltVT = ObjectVT.getVectorElementType();
Benjamin Kramer3cc579a2013-06-29 22:51:12 +0000935 assert(nvTM->getTargetLowering()->getNumRegisters(F->getContext(),
936 ObjectVT) == NumElts &&
Justin Holewinskif8f70912013-06-28 17:57:59 +0000937 "Vector was not scalarized");
938 unsigned sz = EltVT.getSizeInBits();
939 bool needTruncate = sz < 16 ? true : false;
940
941 if (NumElts == 1) {
942 // Just a simple load
943 std::vector<EVT> LoadRetVTs;
944 if (needTruncate) {
945 // If loading i1 result, generate
946 // load i16
947 // trunc i16 to i1
948 LoadRetVTs.push_back(MVT::i16);
949 } else
950 LoadRetVTs.push_back(EltVT);
951 LoadRetVTs.push_back(MVT::Other);
952 LoadRetVTs.push_back(MVT::Glue);
953 std::vector<SDValue> LoadRetOps;
954 LoadRetOps.push_back(Chain);
955 LoadRetOps.push_back(DAG.getConstant(1, MVT::i32));
956 LoadRetOps.push_back(DAG.getConstant(0, MVT::i32));
957 LoadRetOps.push_back(InFlag);
958 SDValue retval = DAG.getMemIntrinsicNode(
959 NVPTXISD::LoadParam, dl,
960 DAG.getVTList(&LoadRetVTs[0], LoadRetVTs.size()), &LoadRetOps[0],
961 LoadRetOps.size(), EltVT, MachinePointerInfo());
Justin Holewinskiae556d32012-05-04 20:18:50 +0000962 Chain = retval.getValue(1);
963 InFlag = retval.getValue(2);
Justin Holewinskif8f70912013-06-28 17:57:59 +0000964 SDValue Ret0 = retval;
965 if (needTruncate)
966 Ret0 = DAG.getNode(ISD::TRUNCATE, dl, EltVT, Ret0);
967 InVals.push_back(Ret0);
968 } else if (NumElts == 2) {
969 // LoadV2
970 std::vector<EVT> LoadRetVTs;
971 if (needTruncate) {
972 // If loading i1 result, generate
973 // load i16
974 // trunc i16 to i1
975 LoadRetVTs.push_back(MVT::i16);
976 LoadRetVTs.push_back(MVT::i16);
977 } else {
978 LoadRetVTs.push_back(EltVT);
979 LoadRetVTs.push_back(EltVT);
980 }
981 LoadRetVTs.push_back(MVT::Other);
982 LoadRetVTs.push_back(MVT::Glue);
983 std::vector<SDValue> LoadRetOps;
984 LoadRetOps.push_back(Chain);
985 LoadRetOps.push_back(DAG.getConstant(1, MVT::i32));
986 LoadRetOps.push_back(DAG.getConstant(0, MVT::i32));
987 LoadRetOps.push_back(InFlag);
988 SDValue retval = DAG.getMemIntrinsicNode(
989 NVPTXISD::LoadParamV2, dl,
990 DAG.getVTList(&LoadRetVTs[0], LoadRetVTs.size()), &LoadRetOps[0],
991 LoadRetOps.size(), EltVT, MachinePointerInfo());
992 Chain = retval.getValue(2);
993 InFlag = retval.getValue(3);
994 SDValue Ret0 = retval.getValue(0);
995 SDValue Ret1 = retval.getValue(1);
996 if (needTruncate) {
997 Ret0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ret0);
998 InVals.push_back(Ret0);
999 Ret1 = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ret1);
1000 InVals.push_back(Ret1);
1001 } else {
1002 InVals.push_back(Ret0);
1003 InVals.push_back(Ret1);
1004 }
1005 } else {
1006 // Split into N LoadV4
1007 unsigned Ofst = 0;
1008 unsigned VecSize = 4;
1009 unsigned Opc = NVPTXISD::LoadParamV4;
1010 if (EltVT.getSizeInBits() == 64) {
1011 VecSize = 2;
1012 Opc = NVPTXISD::LoadParamV2;
1013 }
1014 EVT VecVT = EVT::getVectorVT(F->getContext(), EltVT, VecSize);
1015 for (unsigned i = 0; i < NumElts; i += VecSize) {
1016 SmallVector<EVT, 8> LoadRetVTs;
1017 if (needTruncate) {
1018 // If loading i1 result, generate
1019 // load i16
1020 // trunc i16 to i1
1021 for (unsigned j = 0; j < VecSize; ++j)
1022 LoadRetVTs.push_back(MVT::i16);
1023 } else {
1024 for (unsigned j = 0; j < VecSize; ++j)
1025 LoadRetVTs.push_back(EltVT);
1026 }
1027 LoadRetVTs.push_back(MVT::Other);
1028 LoadRetVTs.push_back(MVT::Glue);
1029 SmallVector<SDValue, 4> LoadRetOps;
1030 LoadRetOps.push_back(Chain);
1031 LoadRetOps.push_back(DAG.getConstant(1, MVT::i32));
1032 LoadRetOps.push_back(DAG.getConstant(Ofst, MVT::i32));
1033 LoadRetOps.push_back(InFlag);
1034 SDValue retval = DAG.getMemIntrinsicNode(
1035 Opc, dl, DAG.getVTList(&LoadRetVTs[0], LoadRetVTs.size()),
1036 &LoadRetOps[0], LoadRetOps.size(), EltVT, MachinePointerInfo());
1037 if (VecSize == 2) {
1038 Chain = retval.getValue(2);
1039 InFlag = retval.getValue(3);
1040 } else {
1041 Chain = retval.getValue(4);
1042 InFlag = retval.getValue(5);
1043 }
1044
1045 for (unsigned j = 0; j < VecSize; ++j) {
1046 if (i + j >= NumElts)
1047 break;
1048 SDValue Elt = retval.getValue(j);
1049 if (needTruncate)
1050 Elt = DAG.getNode(ISD::TRUNCATE, dl, EltVT, Elt);
1051 InVals.push_back(Elt);
1052 }
1053 Ofst += TD->getTypeAllocSize(VecVT.getTypeForEVT(F->getContext()));
1054 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001055 }
Justin Holewinski0497ab12013-03-30 14:29:21 +00001056 } else {
Justin Holewinskif8f70912013-06-28 17:57:59 +00001057 SmallVector<EVT, 16> VTs;
1058 ComputePTXValueVTs(*this, retTy, VTs);
1059 assert(VTs.size() == Ins.size() && "Bad value decomposition");
Justin Holewinski0497ab12013-03-30 14:29:21 +00001060 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
Justin Holewinskif8f70912013-06-28 17:57:59 +00001061 unsigned sz = VTs[i].getSizeInBits();
1062 bool needTruncate = sz < 8 ? true : false;
1063 if (VTs[i].isInteger() && (sz < 8))
1064 sz = 8;
1065
1066 SmallVector<EVT, 4> LoadRetVTs;
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001067 EVT TheLoadType = VTs[i];
1068 if (retTy->isIntegerTy() &&
1069 TD->getTypeAllocSizeInBits(retTy) < 32) {
1070 // This is for integer types only, and specifically not for
1071 // aggregates.
1072 LoadRetVTs.push_back(MVT::i32);
1073 TheLoadType = MVT::i32;
1074 } else if (sz < 16) {
Justin Holewinskif8f70912013-06-28 17:57:59 +00001075 // If loading i1/i8 result, generate
1076 // load i8 (-> i16)
1077 // trunc i16 to i1/i8
1078 LoadRetVTs.push_back(MVT::i16);
1079 } else
1080 LoadRetVTs.push_back(Ins[i].VT);
1081 LoadRetVTs.push_back(MVT::Other);
1082 LoadRetVTs.push_back(MVT::Glue);
1083
1084 SmallVector<SDValue, 4> LoadRetOps;
1085 LoadRetOps.push_back(Chain);
1086 LoadRetOps.push_back(DAG.getConstant(1, MVT::i32));
1087 LoadRetOps.push_back(DAG.getConstant(resoffset, MVT::i32));
1088 LoadRetOps.push_back(InFlag);
1089 SDValue retval = DAG.getMemIntrinsicNode(
1090 NVPTXISD::LoadParam, dl,
1091 DAG.getVTList(&LoadRetVTs[0], LoadRetVTs.size()), &LoadRetOps[0],
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001092 LoadRetOps.size(), TheLoadType, MachinePointerInfo());
Justin Holewinskif8f70912013-06-28 17:57:59 +00001093 Chain = retval.getValue(1);
1094 InFlag = retval.getValue(2);
1095 SDValue Ret0 = retval.getValue(0);
1096 if (needTruncate)
1097 Ret0 = DAG.getNode(ISD::TRUNCATE, dl, Ins[i].VT, Ret0);
1098 InVals.push_back(Ret0);
1099 resoffset += sz / 8;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001100 }
1101 }
1102 }
Justin Holewinskif8f70912013-06-28 17:57:59 +00001103
Justin Holewinski0497ab12013-03-30 14:29:21 +00001104 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(uniqueCallSite, true),
1105 DAG.getIntPtrConstant(uniqueCallSite + 1, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001106 InFlag, dl);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001107 uniqueCallSite++;
1108
1109 // set isTailCall to false for now, until we figure out how to express
1110 // tail call optimization in PTX
1111 isTailCall = false;
1112 return Chain;
1113}
Justin Holewinskiae556d32012-05-04 20:18:50 +00001114
1115// By default CONCAT_VECTORS is lowered by ExpandVectorBuildThroughStack()
1116// (see LegalizeDAG.cpp). This is slow and uses local memory.
1117// We use extract/insert/build vector just as what LegalizeOp() does in llvm 2.5
Justin Holewinski0497ab12013-03-30 14:29:21 +00001118SDValue
1119NVPTXTargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001120 SDNode *Node = Op.getNode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001121 SDLoc dl(Node);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001122 SmallVector<SDValue, 8> Ops;
1123 unsigned NumOperands = Node->getNumOperands();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001124 for (unsigned i = 0; i < NumOperands; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001125 SDValue SubOp = Node->getOperand(i);
1126 EVT VVT = SubOp.getNode()->getValueType(0);
1127 EVT EltVT = VVT.getVectorElementType();
1128 unsigned NumSubElem = VVT.getVectorNumElements();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001129 for (unsigned j = 0; j < NumSubElem; ++j) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001130 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, SubOp,
1131 DAG.getIntPtrConstant(j)));
1132 }
1133 }
Justin Holewinski0497ab12013-03-30 14:29:21 +00001134 return DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0), &Ops[0],
1135 Ops.size());
Justin Holewinskiae556d32012-05-04 20:18:50 +00001136}
1137
Justin Holewinski0497ab12013-03-30 14:29:21 +00001138SDValue
1139NVPTXTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001140 switch (Op.getOpcode()) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001141 case ISD::RETURNADDR:
1142 return SDValue();
1143 case ISD::FRAMEADDR:
1144 return SDValue();
1145 case ISD::GlobalAddress:
1146 return LowerGlobalAddress(Op, DAG);
1147 case ISD::INTRINSIC_W_CHAIN:
1148 return Op;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001149 case ISD::BUILD_VECTOR:
1150 case ISD::EXTRACT_SUBVECTOR:
1151 return Op;
Justin Holewinski0497ab12013-03-30 14:29:21 +00001152 case ISD::CONCAT_VECTORS:
1153 return LowerCONCAT_VECTORS(Op, DAG);
1154 case ISD::STORE:
1155 return LowerSTORE(Op, DAG);
1156 case ISD::LOAD:
1157 return LowerLOAD(Op, DAG);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001158 default:
David Blaikie891d0a32012-05-04 22:34:16 +00001159 llvm_unreachable("Custom lowering not defined for operation");
Justin Holewinskiae556d32012-05-04 20:18:50 +00001160 }
1161}
1162
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001163SDValue NVPTXTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1164 if (Op.getValueType() == MVT::i1)
1165 return LowerLOADi1(Op, DAG);
1166 else
1167 return SDValue();
1168}
1169
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001170// v = ld i1* addr
1171// =>
Justin Holewinskif8f70912013-06-28 17:57:59 +00001172// v1 = ld i8* addr (-> i16)
1173// v = trunc i16 to i1
Justin Holewinski0497ab12013-03-30 14:29:21 +00001174SDValue NVPTXTargetLowering::LowerLOADi1(SDValue Op, SelectionDAG &DAG) const {
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001175 SDNode *Node = Op.getNode();
1176 LoadSDNode *LD = cast<LoadSDNode>(Node);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001177 SDLoc dl(Node);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001178 assert(LD->getExtensionType() == ISD::NON_EXTLOAD);
NAKAMURA Takumi5bbe0e12012-11-14 23:46:15 +00001179 assert(Node->getValueType(0) == MVT::i1 &&
1180 "Custom lowering for i1 load only");
Justin Holewinski0497ab12013-03-30 14:29:21 +00001181 SDValue newLD =
Justin Holewinskif8f70912013-06-28 17:57:59 +00001182 DAG.getLoad(MVT::i16, dl, LD->getChain(), LD->getBasePtr(),
Justin Holewinski0497ab12013-03-30 14:29:21 +00001183 LD->getPointerInfo(), LD->isVolatile(), LD->isNonTemporal(),
1184 LD->isInvariant(), LD->getAlignment());
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001185 SDValue result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, newLD);
1186 // The legalizer (the caller) is expecting two values from the legalized
1187 // load, so we build a MergeValues node for it. See ExpandUnalignedLoad()
1188 // in LegalizeDAG.cpp which also uses MergeValues.
Justin Holewinski0497ab12013-03-30 14:29:21 +00001189 SDValue Ops[] = { result, LD->getChain() };
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001190 return DAG.getMergeValues(Ops, 2, dl);
1191}
1192
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001193SDValue NVPTXTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1194 EVT ValVT = Op.getOperand(1).getValueType();
1195 if (ValVT == MVT::i1)
1196 return LowerSTOREi1(Op, DAG);
1197 else if (ValVT.isVector())
1198 return LowerSTOREVector(Op, DAG);
1199 else
1200 return SDValue();
1201}
1202
1203SDValue
1204NVPTXTargetLowering::LowerSTOREVector(SDValue Op, SelectionDAG &DAG) const {
1205 SDNode *N = Op.getNode();
1206 SDValue Val = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001207 SDLoc DL(N);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001208 EVT ValVT = Val.getValueType();
1209
1210 if (ValVT.isVector()) {
1211 // We only handle "native" vector sizes for now, e.g. <4 x double> is not
1212 // legal. We can (and should) split that into 2 stores of <2 x double> here
1213 // but I'm leaving that as a TODO for now.
1214 if (!ValVT.isSimple())
1215 return SDValue();
1216 switch (ValVT.getSimpleVT().SimpleTy) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001217 default:
1218 return SDValue();
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001219 case MVT::v2i8:
1220 case MVT::v2i16:
1221 case MVT::v2i32:
1222 case MVT::v2i64:
1223 case MVT::v2f32:
1224 case MVT::v2f64:
1225 case MVT::v4i8:
1226 case MVT::v4i16:
1227 case MVT::v4i32:
1228 case MVT::v4f32:
1229 // This is a "native" vector type
1230 break;
1231 }
1232
1233 unsigned Opcode = 0;
1234 EVT EltVT = ValVT.getVectorElementType();
1235 unsigned NumElts = ValVT.getVectorNumElements();
1236
1237 // Since StoreV2 is a target node, we cannot rely on DAG type legalization.
1238 // Therefore, we must ensure the type is legal. For i1 and i8, we set the
1239 // stored type to i16 and propogate the "real" type as the memory type.
Justin Holewinskia2911282013-07-01 12:58:58 +00001240 bool NeedExt = false;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001241 if (EltVT.getSizeInBits() < 16)
Justin Holewinskia2911282013-07-01 12:58:58 +00001242 NeedExt = true;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001243
1244 switch (NumElts) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001245 default:
1246 return SDValue();
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001247 case 2:
1248 Opcode = NVPTXISD::StoreV2;
1249 break;
1250 case 4: {
1251 Opcode = NVPTXISD::StoreV4;
1252 break;
1253 }
1254 }
1255
1256 SmallVector<SDValue, 8> Ops;
1257
1258 // First is the chain
1259 Ops.push_back(N->getOperand(0));
1260
1261 // Then the split values
1262 for (unsigned i = 0; i < NumElts; ++i) {
1263 SDValue ExtVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Val,
1264 DAG.getIntPtrConstant(i));
Justin Holewinskia2911282013-07-01 12:58:58 +00001265 if (NeedExt)
1266 ExtVal = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i16, ExtVal);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001267 Ops.push_back(ExtVal);
1268 }
1269
1270 // Then any remaining arguments
1271 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) {
1272 Ops.push_back(N->getOperand(i));
1273 }
1274
1275 MemSDNode *MemSD = cast<MemSDNode>(N);
1276
Justin Holewinski0497ab12013-03-30 14:29:21 +00001277 SDValue NewSt = DAG.getMemIntrinsicNode(
1278 Opcode, DL, DAG.getVTList(MVT::Other), &Ops[0], Ops.size(),
1279 MemSD->getMemoryVT(), MemSD->getMemOperand());
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001280
1281 //return DCI.CombineTo(N, NewSt, true);
1282 return NewSt;
1283 }
1284
1285 return SDValue();
1286}
1287
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001288// st i1 v, addr
1289// =>
Justin Holewinskif8f70912013-06-28 17:57:59 +00001290// v1 = zxt v to i16
1291// st.u8 i16, addr
Justin Holewinski0497ab12013-03-30 14:29:21 +00001292SDValue NVPTXTargetLowering::LowerSTOREi1(SDValue Op, SelectionDAG &DAG) const {
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001293 SDNode *Node = Op.getNode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001294 SDLoc dl(Node);
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001295 StoreSDNode *ST = cast<StoreSDNode>(Node);
1296 SDValue Tmp1 = ST->getChain();
1297 SDValue Tmp2 = ST->getBasePtr();
1298 SDValue Tmp3 = ST->getValue();
NAKAMURA Takumi5bbe0e12012-11-14 23:46:15 +00001299 assert(Tmp3.getValueType() == MVT::i1 && "Custom lowering for i1 store only");
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001300 unsigned Alignment = ST->getAlignment();
1301 bool isVolatile = ST->isVolatile();
1302 bool isNonTemporal = ST->isNonTemporal();
Justin Holewinskif8f70912013-06-28 17:57:59 +00001303 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Tmp3);
1304 SDValue Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2,
1305 ST->getPointerInfo(), MVT::i8, isNonTemporal,
1306 isVolatile, Alignment);
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001307 return Result;
1308}
1309
Justin Holewinski0497ab12013-03-30 14:29:21 +00001310SDValue NVPTXTargetLowering::getExtSymb(SelectionDAG &DAG, const char *inname,
1311 int idx, EVT v) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001312 std::string *name = nvTM->getManagedStrPool()->getManagedString(inname);
1313 std::stringstream suffix;
1314 suffix << idx;
1315 *name += suffix.str();
1316 return DAG.getTargetExternalSymbol(name->c_str(), v);
1317}
1318
1319SDValue
1320NVPTXTargetLowering::getParamSymbol(SelectionDAG &DAG, int idx, EVT v) const {
1321 return getExtSymb(DAG, ".PARAM", idx, v);
1322}
1323
Justin Holewinski0497ab12013-03-30 14:29:21 +00001324SDValue NVPTXTargetLowering::getParamHelpSymbol(SelectionDAG &DAG, int idx) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001325 return getExtSymb(DAG, ".HLPPARAM", idx);
1326}
1327
1328// Check to see if the kernel argument is image*_t or sampler_t
1329
1330bool llvm::isImageOrSamplerVal(const Value *arg, const Module *context) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001331 static const char *const specialTypes[] = { "struct._image2d_t",
1332 "struct._image3d_t",
1333 "struct._sampler_t" };
Justin Holewinskiae556d32012-05-04 20:18:50 +00001334
1335 const Type *Ty = arg->getType();
1336 const PointerType *PTy = dyn_cast<PointerType>(Ty);
1337
1338 if (!PTy)
1339 return false;
1340
1341 if (!context)
1342 return false;
1343
1344 const StructType *STy = dyn_cast<StructType>(PTy->getElementType());
Justin Holewinskifb711152012-12-05 20:50:28 +00001345 const std::string TypeName = STy && !STy->isLiteral() ? STy->getName() : "";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001346
Craig Toppere4260f92012-05-24 04:22:05 +00001347 for (int i = 0, e = array_lengthof(specialTypes); i != e; ++i)
Justin Holewinskiae556d32012-05-04 20:18:50 +00001348 if (TypeName == specialTypes[i])
1349 return true;
1350
1351 return false;
1352}
1353
Justin Holewinski0497ab12013-03-30 14:29:21 +00001354SDValue NVPTXTargetLowering::LowerFormalArguments(
1355 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001356 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc dl, SelectionDAG &DAG,
Justin Holewinski0497ab12013-03-30 14:29:21 +00001357 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001358 MachineFunction &MF = DAG.getMachineFunction();
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001359 const DataLayout *TD = getDataLayout();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001360
1361 const Function *F = MF.getFunction();
Bill Wendlinge94d8432012-12-07 23:16:57 +00001362 const AttributeSet &PAL = F->getAttributes();
Justin Holewinski44f5c602013-06-28 17:57:53 +00001363 const TargetLowering *TLI = nvTM->getTargetLowering();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001364
1365 SDValue Root = DAG.getRoot();
1366 std::vector<SDValue> OutChains;
1367
1368 bool isKernel = llvm::isKernelFunction(*F);
1369 bool isABI = (nvptxSubtarget.getSmVersion() >= 20);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001370 assert(isABI && "Non-ABI compilation is not supported");
1371 if (!isABI)
1372 return Chain;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001373
1374 std::vector<Type *> argTypes;
1375 std::vector<const Argument *> theArgs;
1376 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001377 I != E; ++I) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001378 theArgs.push_back(I);
1379 argTypes.push_back(I->getType());
1380 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001381 // argTypes.size() (or theArgs.size()) and Ins.size() need not match.
1382 // Ins.size() will be larger
1383 // * if there is an aggregate argument with multiple fields (each field
1384 // showing up separately in Ins)
1385 // * if there is a vector argument with more than typical vector-length
1386 // elements (generally if more than 4) where each vector element is
1387 // individually present in Ins.
1388 // So a different index should be used for indexing into Ins.
1389 // See similar issue in LowerCall.
1390 unsigned InsIdx = 0;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001391
1392 int idx = 0;
Justin Holewinski44f5c602013-06-28 17:57:53 +00001393 for (unsigned i = 0, e = theArgs.size(); i != e; ++i, ++idx, ++InsIdx) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001394 Type *Ty = argTypes[i];
Justin Holewinskiae556d32012-05-04 20:18:50 +00001395
1396 // If the kernel argument is image*_t or sampler_t, convert it to
1397 // a i32 constant holding the parameter position. This can later
1398 // matched in the AsmPrinter to output the correct mangled name.
Justin Holewinski0497ab12013-03-30 14:29:21 +00001399 if (isImageOrSamplerVal(
1400 theArgs[i],
1401 (theArgs[i]->getParent() ? theArgs[i]->getParent()->getParent()
1402 : 0))) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001403 assert(isKernel && "Only kernels can have image/sampler params");
Justin Holewinski0497ab12013-03-30 14:29:21 +00001404 InVals.push_back(DAG.getConstant(i + 1, MVT::i32));
Justin Holewinskiae556d32012-05-04 20:18:50 +00001405 continue;
1406 }
1407
1408 if (theArgs[i]->use_empty()) {
1409 // argument is dead
Justin Holewinski44f5c602013-06-28 17:57:53 +00001410 if (Ty->isAggregateType()) {
1411 SmallVector<EVT, 16> vtparts;
1412
Justin Holewinskif8f70912013-06-28 17:57:59 +00001413 ComputePTXValueVTs(*this, Ty, vtparts);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001414 assert(vtparts.size() > 0 && "empty aggregate type not expected");
1415 for (unsigned parti = 0, parte = vtparts.size(); parti != parte;
1416 ++parti) {
1417 EVT partVT = vtparts[parti];
1418 InVals.push_back(DAG.getNode(ISD::UNDEF, dl, partVT));
1419 ++InsIdx;
Justin Holewinskie9884092013-03-24 21:17:47 +00001420 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001421 if (vtparts.size() > 0)
1422 --InsIdx;
1423 continue;
Justin Holewinskie9884092013-03-24 21:17:47 +00001424 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001425 if (Ty->isVectorTy()) {
1426 EVT ObjectVT = getValueType(Ty);
1427 unsigned NumRegs = TLI->getNumRegisters(F->getContext(), ObjectVT);
1428 for (unsigned parti = 0; parti < NumRegs; ++parti) {
1429 InVals.push_back(DAG.getNode(ISD::UNDEF, dl, Ins[InsIdx].VT));
1430 ++InsIdx;
1431 }
1432 if (NumRegs > 0)
1433 --InsIdx;
1434 continue;
1435 }
1436 InVals.push_back(DAG.getNode(ISD::UNDEF, dl, Ins[InsIdx].VT));
Justin Holewinskiae556d32012-05-04 20:18:50 +00001437 continue;
1438 }
1439
1440 // In the following cases, assign a node order of "idx+1"
Justin Holewinski44f5c602013-06-28 17:57:53 +00001441 // to newly created nodes. The SDNodes for params have to
Justin Holewinskiae556d32012-05-04 20:18:50 +00001442 // appear in the same order as their order of appearance
1443 // in the original function. "idx+1" holds that order.
Justin Holewinski0497ab12013-03-30 14:29:21 +00001444 if (PAL.hasAttribute(i + 1, Attribute::ByVal) == false) {
Justin Holewinski44f5c602013-06-28 17:57:53 +00001445 if (Ty->isAggregateType()) {
1446 SmallVector<EVT, 16> vtparts;
1447 SmallVector<uint64_t, 16> offsets;
1448
Justin Holewinskif8f70912013-06-28 17:57:59 +00001449 // NOTE: Here, we lose the ability to issue vector loads for vectors
1450 // that are a part of a struct. This should be investigated in the
1451 // future.
1452 ComputePTXValueVTs(*this, Ty, vtparts, &offsets, 0);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001453 assert(vtparts.size() > 0 && "empty aggregate type not expected");
1454 bool aggregateIsPacked = false;
1455 if (StructType *STy = llvm::dyn_cast<StructType>(Ty))
1456 aggregateIsPacked = STy->isPacked();
1457
1458 SDValue Arg = getParamSymbol(DAG, idx, getPointerTy());
1459 for (unsigned parti = 0, parte = vtparts.size(); parti != parte;
1460 ++parti) {
1461 EVT partVT = vtparts[parti];
1462 Value *srcValue = Constant::getNullValue(
1463 PointerType::get(partVT.getTypeForEVT(F->getContext()),
1464 llvm::ADDRESS_SPACE_PARAM));
1465 SDValue srcAddr =
1466 DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg,
1467 DAG.getConstant(offsets[parti], getPointerTy()));
1468 unsigned partAlign =
1469 aggregateIsPacked ? 1
1470 : TD->getABITypeAlignment(
1471 partVT.getTypeForEVT(F->getContext()));
Justin Holewinskia2911282013-07-01 12:58:58 +00001472 SDValue p;
1473 if (Ins[InsIdx].VT.getSizeInBits() > partVT.getSizeInBits()) {
1474 ISD::LoadExtType ExtOp = Ins[InsIdx].Flags.isSExt() ?
1475 ISD::SEXTLOAD : ISD::ZEXTLOAD;
1476 p = DAG.getExtLoad(ExtOp, dl, Ins[InsIdx].VT, Root, srcAddr,
Justin Holewinskif8f70912013-06-28 17:57:59 +00001477 MachinePointerInfo(srcValue), partVT, false,
1478 false, partAlign);
Justin Holewinskia2911282013-07-01 12:58:58 +00001479 } else {
Justin Holewinskif8f70912013-06-28 17:57:59 +00001480 p = DAG.getLoad(partVT, dl, Root, srcAddr,
1481 MachinePointerInfo(srcValue), false, false, false,
1482 partAlign);
Justin Holewinskia2911282013-07-01 12:58:58 +00001483 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001484 if (p.getNode())
1485 p.getNode()->setIROrder(idx + 1);
1486 InVals.push_back(p);
1487 ++InsIdx;
Justin Holewinskie9884092013-03-24 21:17:47 +00001488 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001489 if (vtparts.size() > 0)
1490 --InsIdx;
Justin Holewinskie9884092013-03-24 21:17:47 +00001491 continue;
1492 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001493 if (Ty->isVectorTy()) {
1494 EVT ObjectVT = getValueType(Ty);
Justin Holewinskiaaaf2892013-06-25 12:22:21 +00001495 SDValue Arg = getParamSymbol(DAG, idx, getPointerTy());
Justin Holewinski44f5c602013-06-28 17:57:53 +00001496 unsigned NumElts = ObjectVT.getVectorNumElements();
1497 assert(TLI->getNumRegisters(F->getContext(), ObjectVT) == NumElts &&
1498 "Vector was not scalarized");
1499 unsigned Ofst = 0;
1500 EVT EltVT = ObjectVT.getVectorElementType();
1501
1502 // V1 load
1503 // f32 = load ...
1504 if (NumElts == 1) {
1505 // We only have one element, so just directly load it
1506 Value *SrcValue = Constant::getNullValue(PointerType::get(
1507 EltVT.getTypeForEVT(F->getContext()), llvm::ADDRESS_SPACE_PARAM));
1508 SDValue SrcAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg,
1509 DAG.getConstant(Ofst, getPointerTy()));
1510 SDValue P = DAG.getLoad(
1511 EltVT, dl, Root, SrcAddr, MachinePointerInfo(SrcValue), false,
1512 false, true,
1513 TD->getABITypeAlignment(EltVT.getTypeForEVT(F->getContext())));
1514 if (P.getNode())
1515 P.getNode()->setIROrder(idx + 1);
1516
Justin Holewinskif8f70912013-06-28 17:57:59 +00001517 if (Ins[InsIdx].VT.getSizeInBits() > EltVT.getSizeInBits())
Justin Holewinskia2911282013-07-01 12:58:58 +00001518 P = DAG.getNode(ISD::ANY_EXTEND, dl, Ins[InsIdx].VT, P);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001519 InVals.push_back(P);
1520 Ofst += TD->getTypeAllocSize(EltVT.getTypeForEVT(F->getContext()));
1521 ++InsIdx;
1522 } else if (NumElts == 2) {
1523 // V2 load
1524 // f32,f32 = load ...
1525 EVT VecVT = EVT::getVectorVT(F->getContext(), EltVT, 2);
1526 Value *SrcValue = Constant::getNullValue(PointerType::get(
1527 VecVT.getTypeForEVT(F->getContext()), llvm::ADDRESS_SPACE_PARAM));
1528 SDValue SrcAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg,
1529 DAG.getConstant(Ofst, getPointerTy()));
1530 SDValue P = DAG.getLoad(
1531 VecVT, dl, Root, SrcAddr, MachinePointerInfo(SrcValue), false,
1532 false, true,
1533 TD->getABITypeAlignment(VecVT.getTypeForEVT(F->getContext())));
1534 if (P.getNode())
1535 P.getNode()->setIROrder(idx + 1);
1536
1537 SDValue Elt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, P,
1538 DAG.getIntPtrConstant(0));
1539 SDValue Elt1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, P,
1540 DAG.getIntPtrConstant(1));
Justin Holewinskif8f70912013-06-28 17:57:59 +00001541
1542 if (Ins[InsIdx].VT.getSizeInBits() > EltVT.getSizeInBits()) {
Justin Holewinskia2911282013-07-01 12:58:58 +00001543 Elt0 = DAG.getNode(ISD::ANY_EXTEND, dl, Ins[InsIdx].VT, Elt0);
1544 Elt1 = DAG.getNode(ISD::ANY_EXTEND, dl, Ins[InsIdx].VT, Elt1);
Justin Holewinskif8f70912013-06-28 17:57:59 +00001545 }
1546
Justin Holewinski44f5c602013-06-28 17:57:53 +00001547 InVals.push_back(Elt0);
1548 InVals.push_back(Elt1);
1549 Ofst += TD->getTypeAllocSize(VecVT.getTypeForEVT(F->getContext()));
1550 InsIdx += 2;
1551 } else {
1552 // V4 loads
1553 // We have at least 4 elements (<3 x Ty> expands to 4 elements) and
1554 // the
1555 // vector will be expanded to a power of 2 elements, so we know we can
1556 // always round up to the next multiple of 4 when creating the vector
1557 // loads.
1558 // e.g. 4 elem => 1 ld.v4
1559 // 6 elem => 2 ld.v4
1560 // 8 elem => 2 ld.v4
1561 // 11 elem => 3 ld.v4
1562 unsigned VecSize = 4;
1563 if (EltVT.getSizeInBits() == 64) {
1564 VecSize = 2;
1565 }
1566 EVT VecVT = EVT::getVectorVT(F->getContext(), EltVT, VecSize);
1567 for (unsigned i = 0; i < NumElts; i += VecSize) {
1568 Value *SrcValue = Constant::getNullValue(
1569 PointerType::get(VecVT.getTypeForEVT(F->getContext()),
1570 llvm::ADDRESS_SPACE_PARAM));
1571 SDValue SrcAddr =
1572 DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg,
1573 DAG.getConstant(Ofst, getPointerTy()));
1574 SDValue P = DAG.getLoad(
1575 VecVT, dl, Root, SrcAddr, MachinePointerInfo(SrcValue), false,
1576 false, true,
1577 TD->getABITypeAlignment(VecVT.getTypeForEVT(F->getContext())));
1578 if (P.getNode())
1579 P.getNode()->setIROrder(idx + 1);
1580
1581 for (unsigned j = 0; j < VecSize; ++j) {
1582 if (i + j >= NumElts)
1583 break;
1584 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, P,
1585 DAG.getIntPtrConstant(j));
Justin Holewinskif8f70912013-06-28 17:57:59 +00001586 if (Ins[InsIdx].VT.getSizeInBits() > EltVT.getSizeInBits())
Justin Holewinskia2911282013-07-01 12:58:58 +00001587 Elt = DAG.getNode(ISD::ANY_EXTEND, dl, Ins[InsIdx].VT, Elt);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001588 InVals.push_back(Elt);
1589 }
1590 Ofst += TD->getTypeAllocSize(VecVT.getTypeForEVT(F->getContext()));
Justin Holewinski44f5c602013-06-28 17:57:53 +00001591 }
Justin Holewinskidff28d22013-07-01 12:59:01 +00001592 InsIdx += VecSize;
Justin Holewinski44f5c602013-06-28 17:57:53 +00001593 }
1594
1595 if (NumElts > 0)
1596 --InsIdx;
1597 continue;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001598 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001599 // A plain scalar.
1600 EVT ObjectVT = getValueType(Ty);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001601 // If ABI, load from the param symbol
1602 SDValue Arg = getParamSymbol(DAG, idx, getPointerTy());
1603 Value *srcValue = Constant::getNullValue(PointerType::get(
1604 ObjectVT.getTypeForEVT(F->getContext()), llvm::ADDRESS_SPACE_PARAM));
Justin Holewinskif8f70912013-06-28 17:57:59 +00001605 SDValue p;
Justin Holewinskia2911282013-07-01 12:58:58 +00001606 if (ObjectVT.getSizeInBits() < Ins[InsIdx].VT.getSizeInBits()) {
1607 ISD::LoadExtType ExtOp = Ins[InsIdx].Flags.isSExt() ?
1608 ISD::SEXTLOAD : ISD::ZEXTLOAD;
1609 p = DAG.getExtLoad(ExtOp, dl, Ins[InsIdx].VT, Root, Arg,
Justin Holewinskif8f70912013-06-28 17:57:59 +00001610 MachinePointerInfo(srcValue), ObjectVT, false, false,
Justin Holewinskia2911282013-07-01 12:58:58 +00001611 TD->getABITypeAlignment(ObjectVT.getTypeForEVT(F->getContext())));
1612 } else {
Justin Holewinskif8f70912013-06-28 17:57:59 +00001613 p = DAG.getLoad(Ins[InsIdx].VT, dl, Root, Arg,
1614 MachinePointerInfo(srcValue), false, false, false,
Justin Holewinskia2911282013-07-01 12:58:58 +00001615 TD->getABITypeAlignment(ObjectVT.getTypeForEVT(F->getContext())));
1616 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001617 if (p.getNode())
1618 p.getNode()->setIROrder(idx + 1);
1619 InVals.push_back(p);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001620 continue;
1621 }
1622
1623 // Param has ByVal attribute
Justin Holewinski44f5c602013-06-28 17:57:53 +00001624 // Return MoveParam(param symbol).
1625 // Ideally, the param symbol can be returned directly,
1626 // but when SDNode builder decides to use it in a CopyToReg(),
1627 // machine instruction fails because TargetExternalSymbol
1628 // (not lowered) is target dependent, and CopyToReg assumes
1629 // the source is lowered.
1630 EVT ObjectVT = getValueType(Ty);
1631 assert(ObjectVT == Ins[InsIdx].VT &&
1632 "Ins type did not match function type");
1633 SDValue Arg = getParamSymbol(DAG, idx, getPointerTy());
1634 SDValue p = DAG.getNode(NVPTXISD::MoveParam, dl, ObjectVT, Arg);
1635 if (p.getNode())
1636 p.getNode()->setIROrder(idx + 1);
1637 if (isKernel)
1638 InVals.push_back(p);
1639 else {
1640 SDValue p2 = DAG.getNode(
1641 ISD::INTRINSIC_WO_CHAIN, dl, ObjectVT,
1642 DAG.getConstant(Intrinsic::nvvm_ptr_local_to_gen, MVT::i32), p);
1643 InVals.push_back(p2);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001644 }
1645 }
1646
1647 // Clang will check explicit VarArg and issue error if any. However, Clang
1648 // will let code with
Justin Holewinski44f5c602013-06-28 17:57:53 +00001649 // implicit var arg like f() pass. See bug 617733.
Justin Holewinskiae556d32012-05-04 20:18:50 +00001650 // We treat this case as if the arg list is empty.
Justin Holewinski44f5c602013-06-28 17:57:53 +00001651 // if (F.isVarArg()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001652 // assert(0 && "VarArg not supported yet!");
1653 //}
1654
1655 if (!OutChains.empty())
Justin Holewinski0497ab12013-03-30 14:29:21 +00001656 DAG.setRoot(DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &OutChains[0],
1657 OutChains.size()));
Justin Holewinskiae556d32012-05-04 20:18:50 +00001658
1659 return Chain;
1660}
1661
Justin Holewinski44f5c602013-06-28 17:57:53 +00001662
Justin Holewinski120baee2013-06-28 17:57:55 +00001663SDValue
1664NVPTXTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1665 bool isVarArg,
1666 const SmallVectorImpl<ISD::OutputArg> &Outs,
1667 const SmallVectorImpl<SDValue> &OutVals,
1668 SDLoc dl, SelectionDAG &DAG) const {
1669 MachineFunction &MF = DAG.getMachineFunction();
1670 const Function *F = MF.getFunction();
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001671 Type *RetTy = F->getReturnType();
Justin Holewinski120baee2013-06-28 17:57:55 +00001672 const DataLayout *TD = getDataLayout();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001673
1674 bool isABI = (nvptxSubtarget.getSmVersion() >= 20);
Justin Holewinski120baee2013-06-28 17:57:55 +00001675 assert(isABI && "Non-ABI compilation is not supported");
1676 if (!isABI)
1677 return Chain;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001678
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001679 if (VectorType *VTy = dyn_cast<VectorType>(RetTy)) {
Justin Holewinski120baee2013-06-28 17:57:55 +00001680 // If we have a vector type, the OutVals array will be the scalarized
1681 // components and we have combine them into 1 or more vector stores.
1682 unsigned NumElts = VTy->getNumElements();
1683 assert(NumElts == Outs.size() && "Bad scalarization of return value");
1684
Justin Holewinskif8f70912013-06-28 17:57:59 +00001685 // const_cast can be removed in later LLVM versions
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001686 EVT EltVT = getValueType(RetTy).getVectorElementType();
Justin Holewinskif8f70912013-06-28 17:57:59 +00001687 bool NeedExtend = false;
1688 if (EltVT.getSizeInBits() < 16)
1689 NeedExtend = true;
1690
Justin Holewinski120baee2013-06-28 17:57:55 +00001691 // V1 store
1692 if (NumElts == 1) {
1693 SDValue StoreVal = OutVals[0];
1694 // We only have one element, so just directly store it
Justin Holewinskif8f70912013-06-28 17:57:59 +00001695 if (NeedExtend)
1696 StoreVal = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal);
1697 SDValue Ops[] = { Chain, DAG.getConstant(0, MVT::i32), StoreVal };
1698 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreRetval, dl,
1699 DAG.getVTList(MVT::Other), &Ops[0], 3,
1700 EltVT, MachinePointerInfo());
1701
Justin Holewinski120baee2013-06-28 17:57:55 +00001702 } else if (NumElts == 2) {
1703 // V2 store
1704 SDValue StoreVal0 = OutVals[0];
1705 SDValue StoreVal1 = OutVals[1];
1706
Justin Holewinskif8f70912013-06-28 17:57:59 +00001707 if (NeedExtend) {
1708 StoreVal0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal0);
1709 StoreVal1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal1);
Justin Holewinski120baee2013-06-28 17:57:55 +00001710 }
1711
Justin Holewinskif8f70912013-06-28 17:57:59 +00001712 SDValue Ops[] = { Chain, DAG.getConstant(0, MVT::i32), StoreVal0,
1713 StoreVal1 };
1714 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreRetvalV2, dl,
1715 DAG.getVTList(MVT::Other), &Ops[0], 4,
1716 EltVT, MachinePointerInfo());
Justin Holewinski120baee2013-06-28 17:57:55 +00001717 } else {
1718 // V4 stores
1719 // We have at least 4 elements (<3 x Ty> expands to 4 elements) and the
1720 // vector will be expanded to a power of 2 elements, so we know we can
1721 // always round up to the next multiple of 4 when creating the vector
1722 // stores.
1723 // e.g. 4 elem => 1 st.v4
1724 // 6 elem => 2 st.v4
1725 // 8 elem => 2 st.v4
1726 // 11 elem => 3 st.v4
1727
1728 unsigned VecSize = 4;
1729 if (OutVals[0].getValueType().getSizeInBits() == 64)
1730 VecSize = 2;
1731
1732 unsigned Offset = 0;
1733
1734 EVT VecVT =
1735 EVT::getVectorVT(F->getContext(), OutVals[0].getValueType(), VecSize);
1736 unsigned PerStoreOffset =
1737 TD->getTypeAllocSize(VecVT.getTypeForEVT(F->getContext()));
1738
Justin Holewinski120baee2013-06-28 17:57:55 +00001739 for (unsigned i = 0; i < NumElts; i += VecSize) {
1740 // Get values
1741 SDValue StoreVal;
1742 SmallVector<SDValue, 8> Ops;
1743 Ops.push_back(Chain);
1744 Ops.push_back(DAG.getConstant(Offset, MVT::i32));
1745 unsigned Opc = NVPTXISD::StoreRetvalV2;
Justin Holewinskif8f70912013-06-28 17:57:59 +00001746 EVT ExtendedVT = (NeedExtend) ? MVT::i16 : OutVals[0].getValueType();
Justin Holewinski120baee2013-06-28 17:57:55 +00001747
1748 StoreVal = OutVals[i];
Justin Holewinskif8f70912013-06-28 17:57:59 +00001749 if (NeedExtend)
1750 StoreVal = DAG.getNode(ISD::ZERO_EXTEND, dl, ExtendedVT, StoreVal);
Justin Holewinski120baee2013-06-28 17:57:55 +00001751 Ops.push_back(StoreVal);
1752
1753 if (i + 1 < NumElts) {
1754 StoreVal = OutVals[i + 1];
Justin Holewinskif8f70912013-06-28 17:57:59 +00001755 if (NeedExtend)
1756 StoreVal = DAG.getNode(ISD::ZERO_EXTEND, dl, ExtendedVT, StoreVal);
Justin Holewinski120baee2013-06-28 17:57:55 +00001757 } else {
1758 StoreVal = DAG.getUNDEF(ExtendedVT);
1759 }
1760 Ops.push_back(StoreVal);
1761
1762 if (VecSize == 4) {
1763 Opc = NVPTXISD::StoreRetvalV4;
1764 if (i + 2 < NumElts) {
1765 StoreVal = OutVals[i + 2];
Justin Holewinskif8f70912013-06-28 17:57:59 +00001766 if (NeedExtend)
1767 StoreVal =
1768 DAG.getNode(ISD::ZERO_EXTEND, dl, ExtendedVT, StoreVal);
Justin Holewinski120baee2013-06-28 17:57:55 +00001769 } else {
1770 StoreVal = DAG.getUNDEF(ExtendedVT);
1771 }
1772 Ops.push_back(StoreVal);
1773
1774 if (i + 3 < NumElts) {
1775 StoreVal = OutVals[i + 3];
Justin Holewinskif8f70912013-06-28 17:57:59 +00001776 if (NeedExtend)
1777 StoreVal =
1778 DAG.getNode(ISD::ZERO_EXTEND, dl, ExtendedVT, StoreVal);
Justin Holewinski120baee2013-06-28 17:57:55 +00001779 } else {
1780 StoreVal = DAG.getUNDEF(ExtendedVT);
1781 }
1782 Ops.push_back(StoreVal);
1783 }
1784
Justin Holewinskif8f70912013-06-28 17:57:59 +00001785 // Chain = DAG.getNode(Opc, dl, MVT::Other, &Ops[0], Ops.size());
1786 Chain =
1787 DAG.getMemIntrinsicNode(Opc, dl, DAG.getVTList(MVT::Other), &Ops[0],
1788 Ops.size(), EltVT, MachinePointerInfo());
Justin Holewinski120baee2013-06-28 17:57:55 +00001789 Offset += PerStoreOffset;
1790 }
1791 }
1792 } else {
Justin Holewinskif8f70912013-06-28 17:57:59 +00001793 SmallVector<EVT, 16> ValVTs;
1794 // const_cast is necessary since we are still using an LLVM version from
1795 // before the type system re-write.
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001796 ComputePTXValueVTs(*this, RetTy, ValVTs);
Justin Holewinskif8f70912013-06-28 17:57:59 +00001797 assert(ValVTs.size() == OutVals.size() && "Bad return value decomposition");
1798
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001799 unsigned SizeSoFar = 0;
Justin Holewinski120baee2013-06-28 17:57:55 +00001800 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1801 SDValue theVal = OutVals[i];
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001802 EVT TheValType = theVal.getValueType();
Justin Holewinski120baee2013-06-28 17:57:55 +00001803 unsigned numElems = 1;
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001804 if (TheValType.isVector())
1805 numElems = TheValType.getVectorNumElements();
Justin Holewinski120baee2013-06-28 17:57:55 +00001806 for (unsigned j = 0, je = numElems; j != je; ++j) {
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001807 SDValue TmpVal = theVal;
1808 if (TheValType.isVector())
1809 TmpVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
1810 TheValType.getVectorElementType(), TmpVal,
Justin Holewinski120baee2013-06-28 17:57:55 +00001811 DAG.getIntPtrConstant(j));
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001812 EVT TheStoreType = ValVTs[i];
1813 if (RetTy->isIntegerTy() &&
1814 TD->getTypeAllocSizeInBits(RetTy) < 32) {
1815 // The following zero-extension is for integer types only, and
1816 // specifically not for aggregates.
1817 TmpVal = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, TmpVal);
1818 TheStoreType = MVT::i32;
1819 }
1820 else if (TmpVal.getValueType().getSizeInBits() < 16)
1821 TmpVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i16, TmpVal);
1822
1823 SDValue Ops[] = { Chain, DAG.getConstant(SizeSoFar, MVT::i32), TmpVal };
Justin Holewinskif8f70912013-06-28 17:57:59 +00001824 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreRetval, dl,
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001825 DAG.getVTList(MVT::Other), &Ops[0],
1826 3, TheStoreType,
1827 MachinePointerInfo());
1828 if(TheValType.isVector())
1829 SizeSoFar +=
1830 TheStoreType.getVectorElementType().getStoreSizeInBits() / 8;
Justin Holewinski120baee2013-06-28 17:57:55 +00001831 else
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001832 SizeSoFar += TheStoreType.getStoreSizeInBits()/8;
Justin Holewinski120baee2013-06-28 17:57:55 +00001833 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001834 }
1835 }
1836
1837 return DAG.getNode(NVPTXISD::RET_FLAG, dl, MVT::Other, Chain);
1838}
1839
Justin Holewinskif8f70912013-06-28 17:57:59 +00001840
Justin Holewinski0497ab12013-03-30 14:29:21 +00001841void NVPTXTargetLowering::LowerAsmOperandForConstraint(
1842 SDValue Op, std::string &Constraint, std::vector<SDValue> &Ops,
1843 SelectionDAG &DAG) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001844 if (Constraint.length() > 1)
1845 return;
1846 else
1847 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
1848}
1849
1850// NVPTX suuport vector of legal types of any length in Intrinsics because the
1851// NVPTX specific type legalizer
1852// will legalize them to the PTX supported length.
Justin Holewinski0497ab12013-03-30 14:29:21 +00001853bool NVPTXTargetLowering::isTypeSupportedInIntrinsic(MVT VT) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001854 if (isTypeLegal(VT))
1855 return true;
1856 if (VT.isVector()) {
1857 MVT eVT = VT.getVectorElementType();
1858 if (isTypeLegal(eVT))
1859 return true;
1860 }
1861 return false;
1862}
1863
Justin Holewinskiae556d32012-05-04 20:18:50 +00001864// llvm.ptx.memcpy.const and llvm.ptx.memmove.const need to be modeled as
1865// TgtMemIntrinsic
1866// because we need the information that is only available in the "Value" type
1867// of destination
1868// pointer. In particular, the address space information.
Justin Holewinski0497ab12013-03-30 14:29:21 +00001869bool NVPTXTargetLowering::getTgtMemIntrinsic(
1870 IntrinsicInfo &Info, const CallInst &I, unsigned Intrinsic) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001871 switch (Intrinsic) {
1872 default:
1873 return false;
1874
1875 case Intrinsic::nvvm_atomic_load_add_f32:
1876 Info.opc = ISD::INTRINSIC_W_CHAIN;
1877 Info.memVT = MVT::f32;
1878 Info.ptrVal = I.getArgOperand(0);
1879 Info.offset = 0;
1880 Info.vol = 0;
1881 Info.readMem = true;
1882 Info.writeMem = true;
1883 Info.align = 0;
1884 return true;
1885
1886 case Intrinsic::nvvm_atomic_load_inc_32:
1887 case Intrinsic::nvvm_atomic_load_dec_32:
1888 Info.opc = ISD::INTRINSIC_W_CHAIN;
1889 Info.memVT = MVT::i32;
1890 Info.ptrVal = I.getArgOperand(0);
1891 Info.offset = 0;
1892 Info.vol = 0;
1893 Info.readMem = true;
1894 Info.writeMem = true;
1895 Info.align = 0;
1896 return true;
1897
1898 case Intrinsic::nvvm_ldu_global_i:
1899 case Intrinsic::nvvm_ldu_global_f:
1900 case Intrinsic::nvvm_ldu_global_p:
1901
1902 Info.opc = ISD::INTRINSIC_W_CHAIN;
1903 if (Intrinsic == Intrinsic::nvvm_ldu_global_i)
Justin Holewinskif8f70912013-06-28 17:57:59 +00001904 Info.memVT = getValueType(I.getType());
Justin Holewinskiae556d32012-05-04 20:18:50 +00001905 else if (Intrinsic == Intrinsic::nvvm_ldu_global_p)
Justin Holewinskif8f70912013-06-28 17:57:59 +00001906 Info.memVT = getValueType(I.getType());
Justin Holewinskiae556d32012-05-04 20:18:50 +00001907 else
1908 Info.memVT = MVT::f32;
1909 Info.ptrVal = I.getArgOperand(0);
1910 Info.offset = 0;
1911 Info.vol = 0;
1912 Info.readMem = true;
1913 Info.writeMem = false;
1914 Info.align = 0;
1915 return true;
1916
1917 }
1918 return false;
1919}
1920
1921/// isLegalAddressingMode - Return true if the addressing mode represented
1922/// by AM is legal for this target, for a load/store of the specified type.
1923/// Used to guide target specific optimizations, like loop strength reduction
1924/// (LoopStrengthReduce.cpp) and memory optimization for address mode
1925/// (CodeGenPrepare.cpp)
Justin Holewinski0497ab12013-03-30 14:29:21 +00001926bool NVPTXTargetLowering::isLegalAddressingMode(const AddrMode &AM,
1927 Type *Ty) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001928
1929 // AddrMode - This represents an addressing mode of:
1930 // BaseGV + BaseOffs + BaseReg + Scale*ScaleReg
1931 //
1932 // The legal address modes are
1933 // - [avar]
1934 // - [areg]
1935 // - [areg+immoff]
1936 // - [immAddr]
1937
1938 if (AM.BaseGV) {
1939 if (AM.BaseOffs || AM.HasBaseReg || AM.Scale)
1940 return false;
1941 return true;
1942 }
1943
1944 switch (AM.Scale) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001945 case 0: // "r", "r+i" or "i" is allowed
Justin Holewinskiae556d32012-05-04 20:18:50 +00001946 break;
1947 case 1:
Justin Holewinski0497ab12013-03-30 14:29:21 +00001948 if (AM.HasBaseReg) // "r+r+i" or "r+r" is not allowed.
Justin Holewinskiae556d32012-05-04 20:18:50 +00001949 return false;
1950 // Otherwise we have r+i.
1951 break;
1952 default:
1953 // No scale > 1 is allowed
1954 return false;
1955 }
1956 return true;
1957}
1958
1959//===----------------------------------------------------------------------===//
1960// NVPTX Inline Assembly Support
1961//===----------------------------------------------------------------------===//
1962
1963/// getConstraintType - Given a constraint letter, return the type of
1964/// constraint it is for this target.
1965NVPTXTargetLowering::ConstraintType
1966NVPTXTargetLowering::getConstraintType(const std::string &Constraint) const {
1967 if (Constraint.size() == 1) {
1968 switch (Constraint[0]) {
1969 default:
1970 break;
1971 case 'r':
1972 case 'h':
1973 case 'c':
1974 case 'l':
1975 case 'f':
1976 case 'd':
1977 case '0':
1978 case 'N':
1979 return C_RegisterClass;
1980 }
1981 }
1982 return TargetLowering::getConstraintType(Constraint);
1983}
1984
Justin Holewinski0497ab12013-03-30 14:29:21 +00001985std::pair<unsigned, const TargetRegisterClass *>
Justin Holewinskiae556d32012-05-04 20:18:50 +00001986NVPTXTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00001987 MVT VT) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001988 if (Constraint.size() == 1) {
1989 switch (Constraint[0]) {
1990 case 'c':
Justin Holewinskif8f70912013-06-28 17:57:59 +00001991 return std::make_pair(0U, &NVPTX::Int16RegsRegClass);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001992 case 'h':
1993 return std::make_pair(0U, &NVPTX::Int16RegsRegClass);
1994 case 'r':
1995 return std::make_pair(0U, &NVPTX::Int32RegsRegClass);
1996 case 'l':
1997 case 'N':
1998 return std::make_pair(0U, &NVPTX::Int64RegsRegClass);
1999 case 'f':
2000 return std::make_pair(0U, &NVPTX::Float32RegsRegClass);
2001 case 'd':
2002 return std::make_pair(0U, &NVPTX::Float64RegsRegClass);
2003 }
2004 }
2005 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2006}
2007
Justin Holewinskiae556d32012-05-04 20:18:50 +00002008/// getFunctionAlignment - Return the Log2 alignment of this function.
2009unsigned NVPTXTargetLowering::getFunctionAlignment(const Function *) const {
2010 return 4;
2011}
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002012
2013/// ReplaceVectorLoad - Convert vector loads into multi-output scalar loads.
2014static void ReplaceLoadVector(SDNode *N, SelectionDAG &DAG,
Justin Holewinski0497ab12013-03-30 14:29:21 +00002015 SmallVectorImpl<SDValue> &Results) {
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002016 EVT ResVT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002017 SDLoc DL(N);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002018
2019 assert(ResVT.isVector() && "Vector load must have vector type");
2020
2021 // We only handle "native" vector sizes for now, e.g. <4 x double> is not
2022 // legal. We can (and should) split that into 2 loads of <2 x double> here
2023 // but I'm leaving that as a TODO for now.
2024 assert(ResVT.isSimple() && "Can only handle simple types");
2025 switch (ResVT.getSimpleVT().SimpleTy) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00002026 default:
2027 return;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002028 case MVT::v2i8:
2029 case MVT::v2i16:
2030 case MVT::v2i32:
2031 case MVT::v2i64:
2032 case MVT::v2f32:
2033 case MVT::v2f64:
2034 case MVT::v4i8:
2035 case MVT::v4i16:
2036 case MVT::v4i32:
2037 case MVT::v4f32:
2038 // This is a "native" vector type
2039 break;
2040 }
2041
2042 EVT EltVT = ResVT.getVectorElementType();
2043 unsigned NumElts = ResVT.getVectorNumElements();
2044
2045 // Since LoadV2 is a target node, we cannot rely on DAG type legalization.
2046 // Therefore, we must ensure the type is legal. For i1 and i8, we set the
2047 // loaded type to i16 and propogate the "real" type as the memory type.
2048 bool NeedTrunc = false;
2049 if (EltVT.getSizeInBits() < 16) {
2050 EltVT = MVT::i16;
2051 NeedTrunc = true;
2052 }
2053
2054 unsigned Opcode = 0;
2055 SDVTList LdResVTs;
2056
2057 switch (NumElts) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00002058 default:
2059 return;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002060 case 2:
2061 Opcode = NVPTXISD::LoadV2;
2062 LdResVTs = DAG.getVTList(EltVT, EltVT, MVT::Other);
2063 break;
2064 case 4: {
2065 Opcode = NVPTXISD::LoadV4;
2066 EVT ListVTs[] = { EltVT, EltVT, EltVT, EltVT, MVT::Other };
2067 LdResVTs = DAG.getVTList(ListVTs, 5);
2068 break;
2069 }
2070 }
2071
2072 SmallVector<SDValue, 8> OtherOps;
2073
2074 // Copy regular operands
2075 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2076 OtherOps.push_back(N->getOperand(i));
2077
2078 LoadSDNode *LD = cast<LoadSDNode>(N);
2079
2080 // The select routine does not have access to the LoadSDNode instance, so
2081 // pass along the extension information
2082 OtherOps.push_back(DAG.getIntPtrConstant(LD->getExtensionType()));
2083
2084 SDValue NewLD = DAG.getMemIntrinsicNode(Opcode, DL, LdResVTs, &OtherOps[0],
2085 OtherOps.size(), LD->getMemoryVT(),
2086 LD->getMemOperand());
2087
2088 SmallVector<SDValue, 4> ScalarRes;
2089
2090 for (unsigned i = 0; i < NumElts; ++i) {
2091 SDValue Res = NewLD.getValue(i);
2092 if (NeedTrunc)
2093 Res = DAG.getNode(ISD::TRUNCATE, DL, ResVT.getVectorElementType(), Res);
2094 ScalarRes.push_back(Res);
2095 }
2096
2097 SDValue LoadChain = NewLD.getValue(NumElts);
2098
Justin Holewinski0497ab12013-03-30 14:29:21 +00002099 SDValue BuildVec =
2100 DAG.getNode(ISD::BUILD_VECTOR, DL, ResVT, &ScalarRes[0], NumElts);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002101
2102 Results.push_back(BuildVec);
2103 Results.push_back(LoadChain);
2104}
2105
Justin Holewinski0497ab12013-03-30 14:29:21 +00002106static void ReplaceINTRINSIC_W_CHAIN(SDNode *N, SelectionDAG &DAG,
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002107 SmallVectorImpl<SDValue> &Results) {
2108 SDValue Chain = N->getOperand(0);
2109 SDValue Intrin = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002110 SDLoc DL(N);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002111
2112 // Get the intrinsic ID
2113 unsigned IntrinNo = cast<ConstantSDNode>(Intrin.getNode())->getZExtValue();
Justin Holewinski0497ab12013-03-30 14:29:21 +00002114 switch (IntrinNo) {
2115 default:
2116 return;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002117 case Intrinsic::nvvm_ldg_global_i:
2118 case Intrinsic::nvvm_ldg_global_f:
2119 case Intrinsic::nvvm_ldg_global_p:
2120 case Intrinsic::nvvm_ldu_global_i:
2121 case Intrinsic::nvvm_ldu_global_f:
2122 case Intrinsic::nvvm_ldu_global_p: {
2123 EVT ResVT = N->getValueType(0);
2124
2125 if (ResVT.isVector()) {
2126 // Vector LDG/LDU
2127
2128 unsigned NumElts = ResVT.getVectorNumElements();
2129 EVT EltVT = ResVT.getVectorElementType();
2130
Justin Holewinskif8f70912013-06-28 17:57:59 +00002131 // Since LDU/LDG are target nodes, we cannot rely on DAG type
2132 // legalization.
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002133 // Therefore, we must ensure the type is legal. For i1 and i8, we set the
2134 // loaded type to i16 and propogate the "real" type as the memory type.
2135 bool NeedTrunc = false;
2136 if (EltVT.getSizeInBits() < 16) {
2137 EltVT = MVT::i16;
2138 NeedTrunc = true;
2139 }
2140
2141 unsigned Opcode = 0;
2142 SDVTList LdResVTs;
2143
2144 switch (NumElts) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00002145 default:
2146 return;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002147 case 2:
Justin Holewinski0497ab12013-03-30 14:29:21 +00002148 switch (IntrinNo) {
2149 default:
2150 return;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002151 case Intrinsic::nvvm_ldg_global_i:
2152 case Intrinsic::nvvm_ldg_global_f:
2153 case Intrinsic::nvvm_ldg_global_p:
2154 Opcode = NVPTXISD::LDGV2;
2155 break;
2156 case Intrinsic::nvvm_ldu_global_i:
2157 case Intrinsic::nvvm_ldu_global_f:
2158 case Intrinsic::nvvm_ldu_global_p:
2159 Opcode = NVPTXISD::LDUV2;
2160 break;
2161 }
2162 LdResVTs = DAG.getVTList(EltVT, EltVT, MVT::Other);
2163 break;
2164 case 4: {
Justin Holewinski0497ab12013-03-30 14:29:21 +00002165 switch (IntrinNo) {
2166 default:
2167 return;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002168 case Intrinsic::nvvm_ldg_global_i:
2169 case Intrinsic::nvvm_ldg_global_f:
2170 case Intrinsic::nvvm_ldg_global_p:
2171 Opcode = NVPTXISD::LDGV4;
2172 break;
2173 case Intrinsic::nvvm_ldu_global_i:
2174 case Intrinsic::nvvm_ldu_global_f:
2175 case Intrinsic::nvvm_ldu_global_p:
2176 Opcode = NVPTXISD::LDUV4;
2177 break;
2178 }
2179 EVT ListVTs[] = { EltVT, EltVT, EltVT, EltVT, MVT::Other };
2180 LdResVTs = DAG.getVTList(ListVTs, 5);
2181 break;
2182 }
2183 }
2184
2185 SmallVector<SDValue, 8> OtherOps;
2186
2187 // Copy regular operands
2188
2189 OtherOps.push_back(Chain); // Chain
Justin Holewinski0497ab12013-03-30 14:29:21 +00002190 // Skip operand 1 (intrinsic ID)
Justin Holewinskif8f70912013-06-28 17:57:59 +00002191 // Others
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002192 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i)
2193 OtherOps.push_back(N->getOperand(i));
2194
2195 MemIntrinsicSDNode *MemSD = cast<MemIntrinsicSDNode>(N);
2196
Justin Holewinski0497ab12013-03-30 14:29:21 +00002197 SDValue NewLD = DAG.getMemIntrinsicNode(
2198 Opcode, DL, LdResVTs, &OtherOps[0], OtherOps.size(),
2199 MemSD->getMemoryVT(), MemSD->getMemOperand());
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002200
2201 SmallVector<SDValue, 4> ScalarRes;
2202
2203 for (unsigned i = 0; i < NumElts; ++i) {
2204 SDValue Res = NewLD.getValue(i);
2205 if (NeedTrunc)
Justin Holewinski0497ab12013-03-30 14:29:21 +00002206 Res =
2207 DAG.getNode(ISD::TRUNCATE, DL, ResVT.getVectorElementType(), Res);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002208 ScalarRes.push_back(Res);
2209 }
2210
2211 SDValue LoadChain = NewLD.getValue(NumElts);
2212
Justin Holewinski0497ab12013-03-30 14:29:21 +00002213 SDValue BuildVec =
2214 DAG.getNode(ISD::BUILD_VECTOR, DL, ResVT, &ScalarRes[0], NumElts);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002215
2216 Results.push_back(BuildVec);
2217 Results.push_back(LoadChain);
2218 } else {
2219 // i8 LDG/LDU
2220 assert(ResVT.isSimple() && ResVT.getSimpleVT().SimpleTy == MVT::i8 &&
2221 "Custom handling of non-i8 ldu/ldg?");
2222
2223 // Just copy all operands as-is
2224 SmallVector<SDValue, 4> Ops;
2225 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2226 Ops.push_back(N->getOperand(i));
2227
2228 // Force output to i16
2229 SDVTList LdResVTs = DAG.getVTList(MVT::i16, MVT::Other);
2230
2231 MemIntrinsicSDNode *MemSD = cast<MemIntrinsicSDNode>(N);
2232
2233 // We make sure the memory type is i8, which will be used during isel
2234 // to select the proper instruction.
Justin Holewinski0497ab12013-03-30 14:29:21 +00002235 SDValue NewLD =
2236 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, DL, LdResVTs, &Ops[0],
2237 Ops.size(), MVT::i8, MemSD->getMemOperand());
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002238
Justin Holewinskie8c93e32013-07-01 12:58:48 +00002239 Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i8,
2240 NewLD.getValue(0)));
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002241 Results.push_back(NewLD.getValue(1));
2242 }
2243 }
2244 }
2245}
2246
Justin Holewinski0497ab12013-03-30 14:29:21 +00002247void NVPTXTargetLowering::ReplaceNodeResults(
2248 SDNode *N, SmallVectorImpl<SDValue> &Results, SelectionDAG &DAG) const {
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002249 switch (N->getOpcode()) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00002250 default:
2251 report_fatal_error("Unhandled custom legalization");
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002252 case ISD::LOAD:
2253 ReplaceLoadVector(N, DAG, Results);
2254 return;
2255 case ISD::INTRINSIC_W_CHAIN:
2256 ReplaceINTRINSIC_W_CHAIN(N, DAG, Results);
2257 return;
2258 }
2259}