blob: 725bc9e7d0c9e3445e3095738d7659bb0a05725f [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 Holewinskibe8dc642013-02-12 14:18:49 +0000207 // Register custom handling for vector loads/stores
Justin Holewinski0497ab12013-03-30 14:29:21 +0000208 for (int i = MVT::FIRST_VECTOR_VALUETYPE; i <= MVT::LAST_VECTOR_VALUETYPE;
209 ++i) {
210 MVT VT = (MVT::SimpleValueType) i;
Justin Holewinskibe8dc642013-02-12 14:18:49 +0000211 if (IsPTXVectorType(VT)) {
212 setOperationAction(ISD::LOAD, VT, Custom);
213 setOperationAction(ISD::STORE, VT, Custom);
214 setOperationAction(ISD::INTRINSIC_W_CHAIN, VT, Custom);
215 }
216 }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000217
Justin Holewinskif8f70912013-06-28 17:57:59 +0000218 // Custom handling for i8 intrinsics
219 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i8, Custom);
220
Justin Holewinskidc372df2013-06-28 17:58:07 +0000221 setOperationAction(ISD::CTLZ, MVT::i16, Legal);
222 setOperationAction(ISD::CTLZ, MVT::i32, Legal);
223 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
224 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Legal);
225 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Legal);
226 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Legal);
227 setOperationAction(ISD::CTTZ, MVT::i16, Expand);
228 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
229 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
230 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Expand);
231 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
232 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
233 setOperationAction(ISD::CTPOP, MVT::i16, Legal);
234 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
235 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
236
Justin Holewinskiae556d32012-05-04 20:18:50 +0000237 // Now deduce the information based on the above mentioned
238 // actions
239 computeRegisterProperties();
240}
241
Justin Holewinskiae556d32012-05-04 20:18:50 +0000242const char *NVPTXTargetLowering::getTargetNodeName(unsigned Opcode) const {
243 switch (Opcode) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000244 default:
245 return 0;
246 case NVPTXISD::CALL:
247 return "NVPTXISD::CALL";
248 case NVPTXISD::RET_FLAG:
249 return "NVPTXISD::RET_FLAG";
250 case NVPTXISD::Wrapper:
251 return "NVPTXISD::Wrapper";
252 case NVPTXISD::NVBuiltin:
253 return "NVPTXISD::NVBuiltin";
254 case NVPTXISD::DeclareParam:
255 return "NVPTXISD::DeclareParam";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000256 case NVPTXISD::DeclareScalarParam:
257 return "NVPTXISD::DeclareScalarParam";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000258 case NVPTXISD::DeclareRet:
259 return "NVPTXISD::DeclareRet";
260 case NVPTXISD::DeclareRetParam:
261 return "NVPTXISD::DeclareRetParam";
262 case NVPTXISD::PrintCall:
263 return "NVPTXISD::PrintCall";
264 case NVPTXISD::LoadParam:
265 return "NVPTXISD::LoadParam";
Justin Holewinskife44314f2013-06-28 17:57:51 +0000266 case NVPTXISD::LoadParamV2:
267 return "NVPTXISD::LoadParamV2";
268 case NVPTXISD::LoadParamV4:
269 return "NVPTXISD::LoadParamV4";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000270 case NVPTXISD::StoreParam:
271 return "NVPTXISD::StoreParam";
Justin Holewinskife44314f2013-06-28 17:57:51 +0000272 case NVPTXISD::StoreParamV2:
273 return "NVPTXISD::StoreParamV2";
274 case NVPTXISD::StoreParamV4:
275 return "NVPTXISD::StoreParamV4";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000276 case NVPTXISD::StoreParamS32:
277 return "NVPTXISD::StoreParamS32";
278 case NVPTXISD::StoreParamU32:
279 return "NVPTXISD::StoreParamU32";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000280 case NVPTXISD::CallArgBegin:
281 return "NVPTXISD::CallArgBegin";
282 case NVPTXISD::CallArg:
283 return "NVPTXISD::CallArg";
284 case NVPTXISD::LastCallArg:
285 return "NVPTXISD::LastCallArg";
286 case NVPTXISD::CallArgEnd:
287 return "NVPTXISD::CallArgEnd";
288 case NVPTXISD::CallVoid:
289 return "NVPTXISD::CallVoid";
290 case NVPTXISD::CallVal:
291 return "NVPTXISD::CallVal";
292 case NVPTXISD::CallSymbol:
293 return "NVPTXISD::CallSymbol";
294 case NVPTXISD::Prototype:
295 return "NVPTXISD::Prototype";
296 case NVPTXISD::MoveParam:
297 return "NVPTXISD::MoveParam";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000298 case NVPTXISD::StoreRetval:
299 return "NVPTXISD::StoreRetval";
Justin Holewinskife44314f2013-06-28 17:57:51 +0000300 case NVPTXISD::StoreRetvalV2:
301 return "NVPTXISD::StoreRetvalV2";
302 case NVPTXISD::StoreRetvalV4:
303 return "NVPTXISD::StoreRetvalV4";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000304 case NVPTXISD::PseudoUseParam:
305 return "NVPTXISD::PseudoUseParam";
306 case NVPTXISD::RETURN:
307 return "NVPTXISD::RETURN";
308 case NVPTXISD::CallSeqBegin:
309 return "NVPTXISD::CallSeqBegin";
310 case NVPTXISD::CallSeqEnd:
311 return "NVPTXISD::CallSeqEnd";
312 case NVPTXISD::LoadV2:
313 return "NVPTXISD::LoadV2";
314 case NVPTXISD::LoadV4:
315 return "NVPTXISD::LoadV4";
316 case NVPTXISD::LDGV2:
317 return "NVPTXISD::LDGV2";
318 case NVPTXISD::LDGV4:
319 return "NVPTXISD::LDGV4";
320 case NVPTXISD::LDUV2:
321 return "NVPTXISD::LDUV2";
322 case NVPTXISD::LDUV4:
323 return "NVPTXISD::LDUV4";
324 case NVPTXISD::StoreV2:
325 return "NVPTXISD::StoreV2";
326 case NVPTXISD::StoreV4:
327 return "NVPTXISD::StoreV4";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000328 }
329}
330
Justin Holewinskibc451192012-11-29 14:26:24 +0000331bool NVPTXTargetLowering::shouldSplitVectorElementType(EVT VT) const {
332 return VT == MVT::i1;
333}
Justin Holewinskiae556d32012-05-04 20:18:50 +0000334
335SDValue
336NVPTXTargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000337 SDLoc dl(Op);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000338 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
339 Op = DAG.getTargetGlobalAddress(GV, dl, getPointerTy());
340 return DAG.getNode(NVPTXISD::Wrapper, dl, getPointerTy(), Op);
341}
342
Justin Holewinskif8f70912013-06-28 17:57:59 +0000343/*
Justin Holewinski0497ab12013-03-30 14:29:21 +0000344std::string NVPTXTargetLowering::getPrototype(
345 Type *retTy, const ArgListTy &Args,
346 const SmallVectorImpl<ISD::OutputArg> &Outs, unsigned retAlignment) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000347
348 bool isABI = (nvptxSubtarget.getSmVersion() >= 20);
349
350 std::stringstream O;
351 O << "prototype_" << uniqueCallSite << " : .callprototype ";
352
353 if (retTy->getTypeID() == Type::VoidTyID)
354 O << "()";
355 else {
356 O << "(";
357 if (isABI) {
358 if (retTy->isPrimitiveType() || retTy->isIntegerTy()) {
359 unsigned size = 0;
360 if (const IntegerType *ITy = dyn_cast<IntegerType>(retTy)) {
361 size = ITy->getBitWidth();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000362 if (size < 32)
363 size = 32;
364 } else {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000365 assert(retTy->isFloatingPointTy() &&
366 "Floating point type expected here");
367 size = retTy->getPrimitiveSizeInBits();
368 }
369
370 O << ".param .b" << size << " _";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000371 } else if (isa<PointerType>(retTy))
372 O << ".param .b" << getPointerTy().getSizeInBits() << " _";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000373 else {
374 if ((retTy->getTypeID() == Type::StructTyID) ||
375 isa<VectorType>(retTy)) {
376 SmallVector<EVT, 16> vtparts;
377 ComputeValueVTs(*this, retTy, vtparts);
378 unsigned totalsz = 0;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000379 for (unsigned i = 0, e = vtparts.size(); i != e; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000380 unsigned elems = 1;
381 EVT elemtype = vtparts[i];
382 if (vtparts[i].isVector()) {
383 elems = vtparts[i].getVectorNumElements();
384 elemtype = vtparts[i].getVectorElementType();
385 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000386 for (unsigned j = 0, je = elems; j != je; ++j) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000387 unsigned sz = elemtype.getSizeInBits();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000388 if (elemtype.isInteger() && (sz < 8))
389 sz = 8;
390 totalsz += sz / 8;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000391 }
392 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000393 O << ".param .align " << retAlignment << " .b8 _[" << totalsz << "]";
394 } else {
395 assert(false && "Unknown return type");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000396 }
397 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000398 } else {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000399 SmallVector<EVT, 16> vtparts;
400 ComputeValueVTs(*this, retTy, vtparts);
401 unsigned idx = 0;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000402 for (unsigned i = 0, e = vtparts.size(); i != e; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000403 unsigned elems = 1;
404 EVT elemtype = vtparts[i];
405 if (vtparts[i].isVector()) {
406 elems = vtparts[i].getVectorNumElements();
407 elemtype = vtparts[i].getVectorElementType();
408 }
409
Justin Holewinski0497ab12013-03-30 14:29:21 +0000410 for (unsigned j = 0, je = elems; j != je; ++j) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000411 unsigned sz = elemtype.getSizeInBits();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000412 if (elemtype.isInteger() && (sz < 32))
413 sz = 32;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000414 O << ".reg .b" << sz << " _";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000415 if (j < je - 1)
416 O << ", ";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000417 ++idx;
418 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000419 if (i < e - 1)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000420 O << ", ";
421 }
422 }
423 O << ") ";
424 }
425 O << "_ (";
426
427 bool first = true;
428 MVT thePointerTy = getPointerTy();
429
Justin Holewinski0497ab12013-03-30 14:29:21 +0000430 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000431 const Type *Ty = Args[i].Ty;
432 if (!first) {
433 O << ", ";
434 }
435 first = false;
436
437 if (Outs[i].Flags.isByVal() == false) {
438 unsigned sz = 0;
439 if (isa<IntegerType>(Ty)) {
440 sz = cast<IntegerType>(Ty)->getBitWidth();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000441 if (sz < 32)
442 sz = 32;
443 } else if (isa<PointerType>(Ty))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000444 sz = thePointerTy.getSizeInBits();
445 else
446 sz = Ty->getPrimitiveSizeInBits();
447 if (isABI)
448 O << ".param .b" << sz << " ";
449 else
450 O << ".reg .b" << sz << " ";
451 O << "_";
452 continue;
453 }
454 const PointerType *PTy = dyn_cast<PointerType>(Ty);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000455 assert(PTy && "Param with byval attribute should be a pointer type");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000456 Type *ETy = PTy->getElementType();
457
458 if (isABI) {
459 unsigned align = Outs[i].Flags.getByValAlign();
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000460 unsigned sz = getDataLayout()->getTypeAllocSize(ETy);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000461 O << ".param .align " << align << " .b8 ";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000462 O << "_";
463 O << "[" << sz << "]";
464 continue;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000465 } else {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000466 SmallVector<EVT, 16> vtparts;
467 ComputeValueVTs(*this, ETy, vtparts);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000468 for (unsigned i = 0, e = vtparts.size(); i != e; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000469 unsigned elems = 1;
470 EVT elemtype = vtparts[i];
471 if (vtparts[i].isVector()) {
472 elems = vtparts[i].getVectorNumElements();
473 elemtype = vtparts[i].getVectorElementType();
474 }
475
Justin Holewinski0497ab12013-03-30 14:29:21 +0000476 for (unsigned j = 0, je = elems; j != je; ++j) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000477 unsigned sz = elemtype.getSizeInBits();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000478 if (elemtype.isInteger() && (sz < 32))
479 sz = 32;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000480 O << ".reg .b" << sz << " ";
481 O << "_";
Justin Holewinski0497ab12013-03-30 14:29:21 +0000482 if (j < je - 1)
483 O << ", ";
Justin Holewinskiae556d32012-05-04 20:18:50 +0000484 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000485 if (i < e - 1)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000486 O << ", ";
487 }
488 continue;
489 }
490 }
491 O << ");";
492 return O.str();
Justin Holewinskif8f70912013-06-28 17:57:59 +0000493}*/
494
495std::string
496NVPTXTargetLowering::getPrototype(Type *retTy, const ArgListTy &Args,
497 const SmallVectorImpl<ISD::OutputArg> &Outs,
498 unsigned retAlignment,
499 const ImmutableCallSite *CS) const {
500
501 bool isABI = (nvptxSubtarget.getSmVersion() >= 20);
502 assert(isABI && "Non-ABI compilation is not supported");
503 if (!isABI)
504 return "";
505
506 std::stringstream O;
507 O << "prototype_" << uniqueCallSite << " : .callprototype ";
508
509 if (retTy->getTypeID() == Type::VoidTyID) {
510 O << "()";
511 } else {
512 O << "(";
513 if (retTy->isPrimitiveType() || retTy->isIntegerTy()) {
514 unsigned size = 0;
515 if (const IntegerType *ITy = dyn_cast<IntegerType>(retTy)) {
516 size = ITy->getBitWidth();
517 if (size < 32)
518 size = 32;
519 } else {
520 assert(retTy->isFloatingPointTy() &&
521 "Floating point type expected here");
522 size = retTy->getPrimitiveSizeInBits();
523 }
524
525 O << ".param .b" << size << " _";
526 } else if (isa<PointerType>(retTy)) {
527 O << ".param .b" << getPointerTy().getSizeInBits() << " _";
528 } else {
529 if ((retTy->getTypeID() == Type::StructTyID) || isa<VectorType>(retTy)) {
530 SmallVector<EVT, 16> vtparts;
531 ComputeValueVTs(*this, retTy, vtparts);
532 unsigned totalsz = 0;
533 for (unsigned i = 0, e = vtparts.size(); i != e; ++i) {
534 unsigned elems = 1;
535 EVT elemtype = vtparts[i];
536 if (vtparts[i].isVector()) {
537 elems = vtparts[i].getVectorNumElements();
538 elemtype = vtparts[i].getVectorElementType();
539 }
540 // TODO: no need to loop
541 for (unsigned j = 0, je = elems; j != je; ++j) {
542 unsigned sz = elemtype.getSizeInBits();
543 if (elemtype.isInteger() && (sz < 8))
544 sz = 8;
545 totalsz += sz / 8;
546 }
547 }
548 O << ".param .align " << retAlignment << " .b8 _[" << totalsz << "]";
549 } else {
550 assert(false && "Unknown return type");
551 }
552 }
553 O << ") ";
554 }
555 O << "_ (";
556
557 bool first = true;
558 MVT thePointerTy = getPointerTy();
559
560 unsigned OIdx = 0;
561 for (unsigned i = 0, e = Args.size(); i != e; ++i, ++OIdx) {
562 Type *Ty = Args[i].Ty;
563 if (!first) {
564 O << ", ";
565 }
566 first = false;
567
568 if (Outs[OIdx].Flags.isByVal() == false) {
569 if (Ty->isAggregateType() || Ty->isVectorTy()) {
570 unsigned align = 0;
571 const CallInst *CallI = cast<CallInst>(CS->getInstruction());
572 const DataLayout *TD = getDataLayout();
573 // +1 because index 0 is reserved for return type alignment
574 if (!llvm::getAlign(*CallI, i + 1, align))
575 align = TD->getABITypeAlignment(Ty);
576 unsigned sz = TD->getTypeAllocSize(Ty);
577 O << ".param .align " << align << " .b8 ";
578 O << "_";
579 O << "[" << sz << "]";
580 // update the index for Outs
581 SmallVector<EVT, 16> vtparts;
582 ComputeValueVTs(*this, Ty, vtparts);
583 if (unsigned len = vtparts.size())
584 OIdx += len - 1;
585 continue;
586 }
587 assert(getValueType(Ty) == Outs[OIdx].VT &&
588 "type mismatch between callee prototype and arguments");
589 // scalar type
590 unsigned sz = 0;
591 if (isa<IntegerType>(Ty)) {
592 sz = cast<IntegerType>(Ty)->getBitWidth();
593 if (sz < 32)
594 sz = 32;
595 } else if (isa<PointerType>(Ty))
596 sz = thePointerTy.getSizeInBits();
597 else
598 sz = Ty->getPrimitiveSizeInBits();
599 O << ".param .b" << sz << " ";
600 O << "_";
601 continue;
602 }
603 const PointerType *PTy = dyn_cast<PointerType>(Ty);
604 assert(PTy && "Param with byval attribute should be a pointer type");
605 Type *ETy = PTy->getElementType();
606
607 unsigned align = Outs[OIdx].Flags.getByValAlign();
608 unsigned sz = getDataLayout()->getTypeAllocSize(ETy);
609 O << ".param .align " << align << " .b8 ";
610 O << "_";
611 O << "[" << sz << "]";
612 }
613 O << ");";
614 return O.str();
615}
616
617unsigned
618NVPTXTargetLowering::getArgumentAlignment(SDValue Callee,
619 const ImmutableCallSite *CS,
620 Type *Ty,
621 unsigned Idx) const {
622 const DataLayout *TD = getDataLayout();
623 unsigned align = 0;
624 GlobalAddressSDNode *Func = dyn_cast<GlobalAddressSDNode>(Callee.getNode());
625
626 if (Func) { // direct call
627 assert(CS->getCalledFunction() &&
628 "direct call cannot find callee");
629 if (!llvm::getAlign(*(CS->getCalledFunction()), Idx, align))
630 align = TD->getABITypeAlignment(Ty);
631 }
632 else { // indirect call
633 const CallInst *CallI = dyn_cast<CallInst>(CS->getInstruction());
634 if (!llvm::getAlign(*CallI, Idx, align))
635 align = TD->getABITypeAlignment(Ty);
636 }
637
638 return align;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000639}
640
Justin Holewinski0497ab12013-03-30 14:29:21 +0000641SDValue NVPTXTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
642 SmallVectorImpl<SDValue> &InVals) const {
643 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000644 SDLoc dl = CLI.DL;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000645 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000646 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
647 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
648 SDValue Chain = CLI.Chain;
649 SDValue Callee = CLI.Callee;
650 bool &isTailCall = CLI.IsTailCall;
651 ArgListTy &Args = CLI.Args;
652 Type *retTy = CLI.RetTy;
653 ImmutableCallSite *CS = CLI.CS;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000654
Justin Holewinskiae556d32012-05-04 20:18:50 +0000655 bool isABI = (nvptxSubtarget.getSmVersion() >= 20);
Justin Holewinskif8f70912013-06-28 17:57:59 +0000656 assert(isABI && "Non-ABI compilation is not supported");
657 if (!isABI)
658 return Chain;
659 const DataLayout *TD = getDataLayout();
660 MachineFunction &MF = DAG.getMachineFunction();
661 const Function *F = MF.getFunction();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000662
663 SDValue tempChain = Chain;
Justin Holewinskif8f70912013-06-28 17:57:59 +0000664 Chain =
665 DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(uniqueCallSite, true),
666 dl);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000667 SDValue InFlag = Chain.getValue(1);
668
Justin Holewinskiae556d32012-05-04 20:18:50 +0000669 unsigned paramCount = 0;
Justin Holewinskif8f70912013-06-28 17:57:59 +0000670 // Args.size() and Outs.size() need not match.
671 // Outs.size() will be larger
672 // * if there is an aggregate argument with multiple fields (each field
673 // showing up separately in Outs)
674 // * if there is a vector argument with more than typical vector-length
675 // elements (generally if more than 4) where each vector element is
676 // individually present in Outs.
677 // So a different index should be used for indexing into Outs/OutVals.
678 // See similar issue in LowerFormalArguments.
679 unsigned OIdx = 0;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000680 // Declare the .params or .reg need to pass values
681 // to the function
Justin Holewinskif8f70912013-06-28 17:57:59 +0000682 for (unsigned i = 0, e = Args.size(); i != e; ++i, ++OIdx) {
683 EVT VT = Outs[OIdx].VT;
684 Type *Ty = Args[i].Ty;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000685
Justin Holewinskif8f70912013-06-28 17:57:59 +0000686 if (Outs[OIdx].Flags.isByVal() == false) {
687 if (Ty->isAggregateType()) {
688 // aggregate
689 SmallVector<EVT, 16> vtparts;
690 ComputeValueVTs(*this, Ty, vtparts);
691
692 unsigned align = getArgumentAlignment(Callee, CS, Ty, paramCount + 1);
693 // declare .param .align <align> .b8 .param<n>[<size>];
694 unsigned sz = TD->getTypeAllocSize(Ty);
695 SDVTList DeclareParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
696 SDValue DeclareParamOps[] = { Chain, DAG.getConstant(align, MVT::i32),
697 DAG.getConstant(paramCount, MVT::i32),
698 DAG.getConstant(sz, MVT::i32), InFlag };
699 Chain = DAG.getNode(NVPTXISD::DeclareParam, dl, DeclareParamVTs,
700 DeclareParamOps, 5);
701 InFlag = Chain.getValue(1);
702 unsigned curOffset = 0;
703 for (unsigned j = 0, je = vtparts.size(); j != je; ++j) {
704 unsigned elems = 1;
705 EVT elemtype = vtparts[j];
706 if (vtparts[j].isVector()) {
707 elems = vtparts[j].getVectorNumElements();
708 elemtype = vtparts[j].getVectorElementType();
709 }
710 for (unsigned k = 0, ke = elems; k != ke; ++k) {
711 unsigned sz = elemtype.getSizeInBits();
712 if (elemtype.isInteger() && (sz < 8))
713 sz = 8;
714 SDValue StVal = OutVals[OIdx];
715 if (elemtype.getSizeInBits() < 16) {
716 StVal = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, StVal);
717 }
718 SDVTList CopyParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
719 SDValue CopyParamOps[] = { Chain,
720 DAG.getConstant(paramCount, MVT::i32),
721 DAG.getConstant(curOffset, MVT::i32),
722 StVal, InFlag };
723 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreParam, dl,
724 CopyParamVTs, &CopyParamOps[0], 5,
725 elemtype, MachinePointerInfo());
726 InFlag = Chain.getValue(1);
727 curOffset += sz / 8;
728 ++OIdx;
729 }
730 }
731 if (vtparts.size() > 0)
732 --OIdx;
733 ++paramCount;
734 continue;
735 }
736 if (Ty->isVectorTy()) {
737 EVT ObjectVT = getValueType(Ty);
738 unsigned align = getArgumentAlignment(Callee, CS, Ty, paramCount + 1);
739 // declare .param .align <align> .b8 .param<n>[<size>];
740 unsigned sz = TD->getTypeAllocSize(Ty);
741 SDVTList DeclareParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
742 SDValue DeclareParamOps[] = { Chain, DAG.getConstant(align, MVT::i32),
743 DAG.getConstant(paramCount, MVT::i32),
744 DAG.getConstant(sz, MVT::i32), InFlag };
745 Chain = DAG.getNode(NVPTXISD::DeclareParam, dl, DeclareParamVTs,
746 DeclareParamOps, 5);
747 InFlag = Chain.getValue(1);
748 unsigned NumElts = ObjectVT.getVectorNumElements();
749 EVT EltVT = ObjectVT.getVectorElementType();
750 EVT MemVT = EltVT;
751 bool NeedExtend = false;
752 if (EltVT.getSizeInBits() < 16) {
753 NeedExtend = true;
754 EltVT = MVT::i16;
755 }
756
757 // V1 store
758 if (NumElts == 1) {
759 SDValue Elt = OutVals[OIdx++];
760 if (NeedExtend)
761 Elt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Elt);
762
763 SDVTList CopyParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
764 SDValue CopyParamOps[] = { Chain,
765 DAG.getConstant(paramCount, MVT::i32),
766 DAG.getConstant(0, MVT::i32), Elt,
767 InFlag };
768 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreParam, dl,
769 CopyParamVTs, &CopyParamOps[0], 5,
770 MemVT, MachinePointerInfo());
771 InFlag = Chain.getValue(1);
772 } else if (NumElts == 2) {
773 SDValue Elt0 = OutVals[OIdx++];
774 SDValue Elt1 = OutVals[OIdx++];
775 if (NeedExtend) {
776 Elt0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Elt0);
777 Elt1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Elt1);
778 }
779
780 SDVTList CopyParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
781 SDValue CopyParamOps[] = { Chain,
782 DAG.getConstant(paramCount, MVT::i32),
783 DAG.getConstant(0, MVT::i32), Elt0, Elt1,
784 InFlag };
785 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreParamV2, dl,
786 CopyParamVTs, &CopyParamOps[0], 6,
787 MemVT, MachinePointerInfo());
788 InFlag = Chain.getValue(1);
789 } else {
790 unsigned curOffset = 0;
791 // V4 stores
792 // We have at least 4 elements (<3 x Ty> expands to 4 elements) and
793 // the
794 // vector will be expanded to a power of 2 elements, so we know we can
795 // always round up to the next multiple of 4 when creating the vector
796 // stores.
797 // e.g. 4 elem => 1 st.v4
798 // 6 elem => 2 st.v4
799 // 8 elem => 2 st.v4
800 // 11 elem => 3 st.v4
801 unsigned VecSize = 4;
802 if (EltVT.getSizeInBits() == 64)
803 VecSize = 2;
804
805 // This is potentially only part of a vector, so assume all elements
806 // are packed together.
807 unsigned PerStoreOffset = MemVT.getStoreSizeInBits() / 8 * VecSize;
808
809 for (unsigned i = 0; i < NumElts; i += VecSize) {
810 // Get values
811 SDValue StoreVal;
812 SmallVector<SDValue, 8> Ops;
813 Ops.push_back(Chain);
814 Ops.push_back(DAG.getConstant(paramCount, MVT::i32));
815 Ops.push_back(DAG.getConstant(curOffset, MVT::i32));
816
817 unsigned Opc = NVPTXISD::StoreParamV2;
818
819 StoreVal = OutVals[OIdx++];
820 if (NeedExtend)
821 StoreVal = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal);
822 Ops.push_back(StoreVal);
823
824 if (i + 1 < NumElts) {
825 StoreVal = OutVals[OIdx++];
826 if (NeedExtend)
827 StoreVal =
828 DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal);
829 } else {
830 StoreVal = DAG.getUNDEF(EltVT);
831 }
832 Ops.push_back(StoreVal);
833
834 if (VecSize == 4) {
835 Opc = NVPTXISD::StoreParamV4;
836 if (i + 2 < NumElts) {
837 StoreVal = OutVals[OIdx++];
838 if (NeedExtend)
839 StoreVal =
840 DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal);
841 } else {
842 StoreVal = DAG.getUNDEF(EltVT);
843 }
844 Ops.push_back(StoreVal);
845
846 if (i + 3 < NumElts) {
847 StoreVal = OutVals[OIdx++];
848 if (NeedExtend)
849 StoreVal =
850 DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal);
851 } else {
852 StoreVal = DAG.getUNDEF(EltVT);
853 }
854 Ops.push_back(StoreVal);
855 }
856
857 SDVTList CopyParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
858 Chain = DAG.getMemIntrinsicNode(Opc, dl, CopyParamVTs, &Ops[0],
859 Ops.size(), MemVT,
860 MachinePointerInfo());
861 InFlag = Chain.getValue(1);
862 curOffset += PerStoreOffset;
863 }
864 }
865 ++paramCount;
866 --OIdx;
867 continue;
868 }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000869 // Plain scalar
870 // for ABI, declare .param .b<size> .param<n>;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000871 unsigned sz = VT.getSizeInBits();
Justin Holewinskif8f70912013-06-28 17:57:59 +0000872 bool needExtend = false;
873 if (VT.isInteger()) {
874 if (sz < 16)
875 needExtend = true;
876 if (sz < 32)
877 sz = 32;
878 }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000879 SDVTList DeclareParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
880 SDValue DeclareParamOps[] = { Chain,
881 DAG.getConstant(paramCount, MVT::i32),
882 DAG.getConstant(sz, MVT::i32),
Justin Holewinskif8f70912013-06-28 17:57:59 +0000883 DAG.getConstant(0, MVT::i32), InFlag };
Justin Holewinskiae556d32012-05-04 20:18:50 +0000884 Chain = DAG.getNode(NVPTXISD::DeclareScalarParam, dl, DeclareParamVTs,
885 DeclareParamOps, 5);
886 InFlag = Chain.getValue(1);
Justin Holewinskif8f70912013-06-28 17:57:59 +0000887 SDValue OutV = OutVals[OIdx];
888 if (needExtend) {
889 // zext/sext i1 to i16
890 unsigned opc = ISD::ZERO_EXTEND;
891 if (Outs[OIdx].Flags.isSExt())
892 opc = ISD::SIGN_EXTEND;
893 OutV = DAG.getNode(opc, dl, MVT::i16, OutV);
894 }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000895 SDVTList CopyParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
896 SDValue CopyParamOps[] = { Chain, DAG.getConstant(paramCount, MVT::i32),
Justin Holewinskif8f70912013-06-28 17:57:59 +0000897 DAG.getConstant(0, MVT::i32), OutV, InFlag };
Justin Holewinskiae556d32012-05-04 20:18:50 +0000898
899 unsigned opcode = NVPTXISD::StoreParam;
Justin Holewinskif8f70912013-06-28 17:57:59 +0000900 if (Outs[OIdx].Flags.isZExt())
901 opcode = NVPTXISD::StoreParamU32;
902 else if (Outs[OIdx].Flags.isSExt())
903 opcode = NVPTXISD::StoreParamS32;
904 Chain = DAG.getMemIntrinsicNode(opcode, dl, CopyParamVTs, CopyParamOps, 5,
905 VT, MachinePointerInfo());
Justin Holewinskiae556d32012-05-04 20:18:50 +0000906
907 InFlag = Chain.getValue(1);
908 ++paramCount;
909 continue;
910 }
911 // struct or vector
912 SmallVector<EVT, 16> vtparts;
913 const PointerType *PTy = dyn_cast<PointerType>(Args[i].Ty);
Justin Holewinski0497ab12013-03-30 14:29:21 +0000914 assert(PTy && "Type of a byval parameter should be pointer");
Justin Holewinskiae556d32012-05-04 20:18:50 +0000915 ComputeValueVTs(*this, PTy->getElementType(), vtparts);
916
Justin Holewinskif8f70912013-06-28 17:57:59 +0000917 // declare .param .align <align> .b8 .param<n>[<size>];
918 unsigned sz = Outs[OIdx].Flags.getByValSize();
919 SDVTList DeclareParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
920 // The ByValAlign in the Outs[OIdx].Flags is alway set at this point,
921 // so we don't need to worry about natural alignment or not.
922 // See TargetLowering::LowerCallTo().
923 SDValue DeclareParamOps[] = {
924 Chain, DAG.getConstant(Outs[OIdx].Flags.getByValAlign(), MVT::i32),
925 DAG.getConstant(paramCount, MVT::i32), DAG.getConstant(sz, MVT::i32),
926 InFlag
927 };
928 Chain = DAG.getNode(NVPTXISD::DeclareParam, dl, DeclareParamVTs,
929 DeclareParamOps, 5);
930 InFlag = Chain.getValue(1);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000931 unsigned curOffset = 0;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000932 for (unsigned j = 0, je = vtparts.size(); j != je; ++j) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000933 unsigned elems = 1;
934 EVT elemtype = vtparts[j];
935 if (vtparts[j].isVector()) {
936 elems = vtparts[j].getVectorNumElements();
937 elemtype = vtparts[j].getVectorElementType();
938 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000939 for (unsigned k = 0, ke = elems; k != ke; ++k) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000940 unsigned sz = elemtype.getSizeInBits();
Justin Holewinskif8f70912013-06-28 17:57:59 +0000941 if (elemtype.isInteger() && (sz < 8))
942 sz = 8;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000943 SDValue srcAddr =
Justin Holewinskif8f70912013-06-28 17:57:59 +0000944 DAG.getNode(ISD::ADD, dl, getPointerTy(), OutVals[OIdx],
Justin Holewinski0497ab12013-03-30 14:29:21 +0000945 DAG.getConstant(curOffset, getPointerTy()));
Justin Holewinskif8f70912013-06-28 17:57:59 +0000946 SDValue theVal = DAG.getLoad(elemtype, dl, tempChain, srcAddr,
947 MachinePointerInfo(), false, false, false,
948 0);
949 if (elemtype.getSizeInBits() < 16) {
950 theVal = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i16, theVal);
951 }
Justin Holewinskiae556d32012-05-04 20:18:50 +0000952 SDVTList CopyParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
953 SDValue CopyParamOps[] = { Chain, DAG.getConstant(paramCount, MVT::i32),
Justin Holewinskif8f70912013-06-28 17:57:59 +0000954 DAG.getConstant(curOffset, MVT::i32), theVal,
Justin Holewinskiae556d32012-05-04 20:18:50 +0000955 InFlag };
Justin Holewinskif8f70912013-06-28 17:57:59 +0000956 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreParam, dl, CopyParamVTs,
957 CopyParamOps, 5, elemtype,
958 MachinePointerInfo());
959
Justin Holewinskiae556d32012-05-04 20:18:50 +0000960 InFlag = Chain.getValue(1);
Justin Holewinskif8f70912013-06-28 17:57:59 +0000961 curOffset += sz / 8;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000962 }
963 }
Justin Holewinskif8f70912013-06-28 17:57:59 +0000964 ++paramCount;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000965 }
966
967 GlobalAddressSDNode *Func = dyn_cast<GlobalAddressSDNode>(Callee.getNode());
968 unsigned retAlignment = 0;
969
970 // Handle Result
Justin Holewinskiae556d32012-05-04 20:18:50 +0000971 if (Ins.size() > 0) {
972 SmallVector<EVT, 16> resvtparts;
973 ComputeValueVTs(*this, retTy, resvtparts);
974
Justin Holewinskif8f70912013-06-28 17:57:59 +0000975 // Declare
976 // .param .align 16 .b8 retval0[<size-in-bytes>], or
977 // .param .b<size-in-bits> retval0
978 unsigned resultsz = TD->getTypeAllocSizeInBits(retTy);
979 if (retTy->isPrimitiveType() || retTy->isIntegerTy() ||
980 retTy->isPointerTy()) {
981 // Scalar needs to be at least 32bit wide
982 if (resultsz < 32)
983 resultsz = 32;
984 SDVTList DeclareRetVTs = DAG.getVTList(MVT::Other, MVT::Glue);
985 SDValue DeclareRetOps[] = { Chain, DAG.getConstant(1, MVT::i32),
986 DAG.getConstant(resultsz, MVT::i32),
987 DAG.getConstant(0, MVT::i32), InFlag };
988 Chain = DAG.getNode(NVPTXISD::DeclareRet, dl, DeclareRetVTs,
989 DeclareRetOps, 5);
990 InFlag = Chain.getValue(1);
991 } else {
992 retAlignment = getArgumentAlignment(Callee, CS, retTy, 0);
993 SDVTList DeclareRetVTs = DAG.getVTList(MVT::Other, MVT::Glue);
994 SDValue DeclareRetOps[] = { Chain,
995 DAG.getConstant(retAlignment, MVT::i32),
996 DAG.getConstant(resultsz / 8, MVT::i32),
997 DAG.getConstant(0, MVT::i32), InFlag };
998 Chain = DAG.getNode(NVPTXISD::DeclareRetParam, dl, DeclareRetVTs,
999 DeclareRetOps, 5);
1000 InFlag = Chain.getValue(1);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001001 }
1002 }
1003
1004 if (!Func) {
1005 // This is indirect function call case : PTX requires a prototype of the
1006 // form
1007 // proto_0 : .callprototype(.param .b32 _) _ (.param .b32 _);
1008 // to be emitted, and the label has to used as the last arg of call
1009 // instruction.
1010 // The prototype is embedded in a string and put as the operand for an
1011 // INLINEASM SDNode.
1012 SDVTList InlineAsmVTs = DAG.getVTList(MVT::Other, MVT::Glue);
Justin Holewinskif8f70912013-06-28 17:57:59 +00001013 std::string proto_string =
1014 getPrototype(retTy, Args, Outs, retAlignment, CS);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001015 const char *asmstr = nvTM->getManagedStrPool()
1016 ->getManagedString(proto_string.c_str())->c_str();
1017 SDValue InlineAsmOps[] = {
1018 Chain, DAG.getTargetExternalSymbol(asmstr, getPointerTy()),
1019 DAG.getMDNode(0), DAG.getTargetConstant(0, MVT::i32), InFlag
1020 };
Justin Holewinskiae556d32012-05-04 20:18:50 +00001021 Chain = DAG.getNode(ISD::INLINEASM, dl, InlineAsmVTs, InlineAsmOps, 5);
1022 InFlag = Chain.getValue(1);
1023 }
1024 // Op to just print "call"
1025 SDVTList PrintCallVTs = DAG.getVTList(MVT::Other, MVT::Glue);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001026 SDValue PrintCallOps[] = {
Justin Holewinskif8f70912013-06-28 17:57:59 +00001027 Chain, DAG.getConstant((Ins.size() == 0) ? 0 : 1, MVT::i32), InFlag
Justin Holewinski0497ab12013-03-30 14:29:21 +00001028 };
1029 Chain = DAG.getNode(Func ? (NVPTXISD::PrintCallUni) : (NVPTXISD::PrintCall),
1030 dl, PrintCallVTs, PrintCallOps, 3);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001031 InFlag = Chain.getValue(1);
1032
1033 // Ops to print out the function name
1034 SDVTList CallVoidVTs = DAG.getVTList(MVT::Other, MVT::Glue);
1035 SDValue CallVoidOps[] = { Chain, Callee, InFlag };
1036 Chain = DAG.getNode(NVPTXISD::CallVoid, dl, CallVoidVTs, CallVoidOps, 3);
1037 InFlag = Chain.getValue(1);
1038
1039 // Ops to print out the param list
1040 SDVTList CallArgBeginVTs = DAG.getVTList(MVT::Other, MVT::Glue);
1041 SDValue CallArgBeginOps[] = { Chain, InFlag };
1042 Chain = DAG.getNode(NVPTXISD::CallArgBegin, dl, CallArgBeginVTs,
1043 CallArgBeginOps, 2);
1044 InFlag = Chain.getValue(1);
1045
Justin Holewinski0497ab12013-03-30 14:29:21 +00001046 for (unsigned i = 0, e = paramCount; i != e; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001047 unsigned opcode;
Justin Holewinski0497ab12013-03-30 14:29:21 +00001048 if (i == (e - 1))
Justin Holewinskiae556d32012-05-04 20:18:50 +00001049 opcode = NVPTXISD::LastCallArg;
1050 else
1051 opcode = NVPTXISD::CallArg;
1052 SDVTList CallArgVTs = DAG.getVTList(MVT::Other, MVT::Glue);
1053 SDValue CallArgOps[] = { Chain, DAG.getConstant(1, MVT::i32),
Justin Holewinski0497ab12013-03-30 14:29:21 +00001054 DAG.getConstant(i, MVT::i32), InFlag };
Justin Holewinskiae556d32012-05-04 20:18:50 +00001055 Chain = DAG.getNode(opcode, dl, CallArgVTs, CallArgOps, 4);
1056 InFlag = Chain.getValue(1);
1057 }
1058 SDVTList CallArgEndVTs = DAG.getVTList(MVT::Other, MVT::Glue);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001059 SDValue CallArgEndOps[] = { Chain, DAG.getConstant(Func ? 1 : 0, MVT::i32),
Justin Holewinskiae556d32012-05-04 20:18:50 +00001060 InFlag };
Justin Holewinski0497ab12013-03-30 14:29:21 +00001061 Chain =
1062 DAG.getNode(NVPTXISD::CallArgEnd, dl, CallArgEndVTs, CallArgEndOps, 3);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001063 InFlag = Chain.getValue(1);
1064
1065 if (!Func) {
1066 SDVTList PrototypeVTs = DAG.getVTList(MVT::Other, MVT::Glue);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001067 SDValue PrototypeOps[] = { Chain, DAG.getConstant(uniqueCallSite, MVT::i32),
Justin Holewinskiae556d32012-05-04 20:18:50 +00001068 InFlag };
1069 Chain = DAG.getNode(NVPTXISD::Prototype, dl, PrototypeVTs, PrototypeOps, 3);
1070 InFlag = Chain.getValue(1);
1071 }
1072
1073 // Generate loads from param memory/moves from registers for result
1074 if (Ins.size() > 0) {
Justin Holewinskif8f70912013-06-28 17:57:59 +00001075 unsigned resoffset = 0;
1076 if (retTy && retTy->isVectorTy()) {
1077 EVT ObjectVT = getValueType(retTy);
1078 unsigned NumElts = ObjectVT.getVectorNumElements();
1079 EVT EltVT = ObjectVT.getVectorElementType();
Benjamin Kramer3cc579a2013-06-29 22:51:12 +00001080 assert(nvTM->getTargetLowering()->getNumRegisters(F->getContext(),
1081 ObjectVT) == NumElts &&
Justin Holewinskif8f70912013-06-28 17:57:59 +00001082 "Vector was not scalarized");
1083 unsigned sz = EltVT.getSizeInBits();
1084 bool needTruncate = sz < 16 ? true : false;
1085
1086 if (NumElts == 1) {
1087 // Just a simple load
1088 std::vector<EVT> LoadRetVTs;
1089 if (needTruncate) {
1090 // If loading i1 result, generate
1091 // load i16
1092 // trunc i16 to i1
1093 LoadRetVTs.push_back(MVT::i16);
1094 } else
1095 LoadRetVTs.push_back(EltVT);
1096 LoadRetVTs.push_back(MVT::Other);
1097 LoadRetVTs.push_back(MVT::Glue);
1098 std::vector<SDValue> LoadRetOps;
1099 LoadRetOps.push_back(Chain);
1100 LoadRetOps.push_back(DAG.getConstant(1, MVT::i32));
1101 LoadRetOps.push_back(DAG.getConstant(0, MVT::i32));
1102 LoadRetOps.push_back(InFlag);
1103 SDValue retval = DAG.getMemIntrinsicNode(
1104 NVPTXISD::LoadParam, dl,
1105 DAG.getVTList(&LoadRetVTs[0], LoadRetVTs.size()), &LoadRetOps[0],
1106 LoadRetOps.size(), EltVT, MachinePointerInfo());
Justin Holewinskiae556d32012-05-04 20:18:50 +00001107 Chain = retval.getValue(1);
1108 InFlag = retval.getValue(2);
Justin Holewinskif8f70912013-06-28 17:57:59 +00001109 SDValue Ret0 = retval;
1110 if (needTruncate)
1111 Ret0 = DAG.getNode(ISD::TRUNCATE, dl, EltVT, Ret0);
1112 InVals.push_back(Ret0);
1113 } else if (NumElts == 2) {
1114 // LoadV2
1115 std::vector<EVT> LoadRetVTs;
1116 if (needTruncate) {
1117 // If loading i1 result, generate
1118 // load i16
1119 // trunc i16 to i1
1120 LoadRetVTs.push_back(MVT::i16);
1121 LoadRetVTs.push_back(MVT::i16);
1122 } else {
1123 LoadRetVTs.push_back(EltVT);
1124 LoadRetVTs.push_back(EltVT);
1125 }
1126 LoadRetVTs.push_back(MVT::Other);
1127 LoadRetVTs.push_back(MVT::Glue);
1128 std::vector<SDValue> LoadRetOps;
1129 LoadRetOps.push_back(Chain);
1130 LoadRetOps.push_back(DAG.getConstant(1, MVT::i32));
1131 LoadRetOps.push_back(DAG.getConstant(0, MVT::i32));
1132 LoadRetOps.push_back(InFlag);
1133 SDValue retval = DAG.getMemIntrinsicNode(
1134 NVPTXISD::LoadParamV2, dl,
1135 DAG.getVTList(&LoadRetVTs[0], LoadRetVTs.size()), &LoadRetOps[0],
1136 LoadRetOps.size(), EltVT, MachinePointerInfo());
1137 Chain = retval.getValue(2);
1138 InFlag = retval.getValue(3);
1139 SDValue Ret0 = retval.getValue(0);
1140 SDValue Ret1 = retval.getValue(1);
1141 if (needTruncate) {
1142 Ret0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ret0);
1143 InVals.push_back(Ret0);
1144 Ret1 = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Ret1);
1145 InVals.push_back(Ret1);
1146 } else {
1147 InVals.push_back(Ret0);
1148 InVals.push_back(Ret1);
1149 }
1150 } else {
1151 // Split into N LoadV4
1152 unsigned Ofst = 0;
1153 unsigned VecSize = 4;
1154 unsigned Opc = NVPTXISD::LoadParamV4;
1155 if (EltVT.getSizeInBits() == 64) {
1156 VecSize = 2;
1157 Opc = NVPTXISD::LoadParamV2;
1158 }
1159 EVT VecVT = EVT::getVectorVT(F->getContext(), EltVT, VecSize);
1160 for (unsigned i = 0; i < NumElts; i += VecSize) {
1161 SmallVector<EVT, 8> LoadRetVTs;
1162 if (needTruncate) {
1163 // If loading i1 result, generate
1164 // load i16
1165 // trunc i16 to i1
1166 for (unsigned j = 0; j < VecSize; ++j)
1167 LoadRetVTs.push_back(MVT::i16);
1168 } else {
1169 for (unsigned j = 0; j < VecSize; ++j)
1170 LoadRetVTs.push_back(EltVT);
1171 }
1172 LoadRetVTs.push_back(MVT::Other);
1173 LoadRetVTs.push_back(MVT::Glue);
1174 SmallVector<SDValue, 4> LoadRetOps;
1175 LoadRetOps.push_back(Chain);
1176 LoadRetOps.push_back(DAG.getConstant(1, MVT::i32));
1177 LoadRetOps.push_back(DAG.getConstant(Ofst, MVT::i32));
1178 LoadRetOps.push_back(InFlag);
1179 SDValue retval = DAG.getMemIntrinsicNode(
1180 Opc, dl, DAG.getVTList(&LoadRetVTs[0], LoadRetVTs.size()),
1181 &LoadRetOps[0], LoadRetOps.size(), EltVT, MachinePointerInfo());
1182 if (VecSize == 2) {
1183 Chain = retval.getValue(2);
1184 InFlag = retval.getValue(3);
1185 } else {
1186 Chain = retval.getValue(4);
1187 InFlag = retval.getValue(5);
1188 }
1189
1190 for (unsigned j = 0; j < VecSize; ++j) {
1191 if (i + j >= NumElts)
1192 break;
1193 SDValue Elt = retval.getValue(j);
1194 if (needTruncate)
1195 Elt = DAG.getNode(ISD::TRUNCATE, dl, EltVT, Elt);
1196 InVals.push_back(Elt);
1197 }
1198 Ofst += TD->getTypeAllocSize(VecVT.getTypeForEVT(F->getContext()));
1199 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001200 }
Justin Holewinski0497ab12013-03-30 14:29:21 +00001201 } else {
Justin Holewinskif8f70912013-06-28 17:57:59 +00001202 SmallVector<EVT, 16> VTs;
1203 ComputePTXValueVTs(*this, retTy, VTs);
1204 assert(VTs.size() == Ins.size() && "Bad value decomposition");
Justin Holewinski0497ab12013-03-30 14:29:21 +00001205 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
Justin Holewinskif8f70912013-06-28 17:57:59 +00001206 unsigned sz = VTs[i].getSizeInBits();
1207 bool needTruncate = sz < 8 ? true : false;
1208 if (VTs[i].isInteger() && (sz < 8))
1209 sz = 8;
1210
1211 SmallVector<EVT, 4> LoadRetVTs;
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001212 EVT TheLoadType = VTs[i];
1213 if (retTy->isIntegerTy() &&
1214 TD->getTypeAllocSizeInBits(retTy) < 32) {
1215 // This is for integer types only, and specifically not for
1216 // aggregates.
1217 LoadRetVTs.push_back(MVT::i32);
1218 TheLoadType = MVT::i32;
1219 } else if (sz < 16) {
Justin Holewinskif8f70912013-06-28 17:57:59 +00001220 // If loading i1/i8 result, generate
1221 // load i8 (-> i16)
1222 // trunc i16 to i1/i8
1223 LoadRetVTs.push_back(MVT::i16);
1224 } else
1225 LoadRetVTs.push_back(Ins[i].VT);
1226 LoadRetVTs.push_back(MVT::Other);
1227 LoadRetVTs.push_back(MVT::Glue);
1228
1229 SmallVector<SDValue, 4> LoadRetOps;
1230 LoadRetOps.push_back(Chain);
1231 LoadRetOps.push_back(DAG.getConstant(1, MVT::i32));
1232 LoadRetOps.push_back(DAG.getConstant(resoffset, MVT::i32));
1233 LoadRetOps.push_back(InFlag);
1234 SDValue retval = DAG.getMemIntrinsicNode(
1235 NVPTXISD::LoadParam, dl,
1236 DAG.getVTList(&LoadRetVTs[0], LoadRetVTs.size()), &LoadRetOps[0],
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001237 LoadRetOps.size(), TheLoadType, MachinePointerInfo());
Justin Holewinskif8f70912013-06-28 17:57:59 +00001238 Chain = retval.getValue(1);
1239 InFlag = retval.getValue(2);
1240 SDValue Ret0 = retval.getValue(0);
1241 if (needTruncate)
1242 Ret0 = DAG.getNode(ISD::TRUNCATE, dl, Ins[i].VT, Ret0);
1243 InVals.push_back(Ret0);
1244 resoffset += sz / 8;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001245 }
1246 }
1247 }
Justin Holewinskif8f70912013-06-28 17:57:59 +00001248
Justin Holewinski0497ab12013-03-30 14:29:21 +00001249 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(uniqueCallSite, true),
1250 DAG.getIntPtrConstant(uniqueCallSite + 1, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001251 InFlag, dl);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001252 uniqueCallSite++;
1253
1254 // set isTailCall to false for now, until we figure out how to express
1255 // tail call optimization in PTX
1256 isTailCall = false;
1257 return Chain;
1258}
Justin Holewinskiae556d32012-05-04 20:18:50 +00001259
1260// By default CONCAT_VECTORS is lowered by ExpandVectorBuildThroughStack()
1261// (see LegalizeDAG.cpp). This is slow and uses local memory.
1262// We use extract/insert/build vector just as what LegalizeOp() does in llvm 2.5
Justin Holewinski0497ab12013-03-30 14:29:21 +00001263SDValue
1264NVPTXTargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001265 SDNode *Node = Op.getNode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001266 SDLoc dl(Node);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001267 SmallVector<SDValue, 8> Ops;
1268 unsigned NumOperands = Node->getNumOperands();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001269 for (unsigned i = 0; i < NumOperands; ++i) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001270 SDValue SubOp = Node->getOperand(i);
1271 EVT VVT = SubOp.getNode()->getValueType(0);
1272 EVT EltVT = VVT.getVectorElementType();
1273 unsigned NumSubElem = VVT.getVectorNumElements();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001274 for (unsigned j = 0; j < NumSubElem; ++j) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001275 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, SubOp,
1276 DAG.getIntPtrConstant(j)));
1277 }
1278 }
Justin Holewinski0497ab12013-03-30 14:29:21 +00001279 return DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0), &Ops[0],
1280 Ops.size());
Justin Holewinskiae556d32012-05-04 20:18:50 +00001281}
1282
Justin Holewinski0497ab12013-03-30 14:29:21 +00001283SDValue
1284NVPTXTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001285 switch (Op.getOpcode()) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001286 case ISD::RETURNADDR:
1287 return SDValue();
1288 case ISD::FRAMEADDR:
1289 return SDValue();
1290 case ISD::GlobalAddress:
1291 return LowerGlobalAddress(Op, DAG);
1292 case ISD::INTRINSIC_W_CHAIN:
1293 return Op;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001294 case ISD::BUILD_VECTOR:
1295 case ISD::EXTRACT_SUBVECTOR:
1296 return Op;
Justin Holewinski0497ab12013-03-30 14:29:21 +00001297 case ISD::CONCAT_VECTORS:
1298 return LowerCONCAT_VECTORS(Op, DAG);
1299 case ISD::STORE:
1300 return LowerSTORE(Op, DAG);
1301 case ISD::LOAD:
1302 return LowerLOAD(Op, DAG);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001303 default:
David Blaikie891d0a32012-05-04 22:34:16 +00001304 llvm_unreachable("Custom lowering not defined for operation");
Justin Holewinskiae556d32012-05-04 20:18:50 +00001305 }
1306}
1307
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001308SDValue NVPTXTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1309 if (Op.getValueType() == MVT::i1)
1310 return LowerLOADi1(Op, DAG);
1311 else
1312 return SDValue();
1313}
1314
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001315// v = ld i1* addr
1316// =>
Justin Holewinskif8f70912013-06-28 17:57:59 +00001317// v1 = ld i8* addr (-> i16)
1318// v = trunc i16 to i1
Justin Holewinski0497ab12013-03-30 14:29:21 +00001319SDValue NVPTXTargetLowering::LowerLOADi1(SDValue Op, SelectionDAG &DAG) const {
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001320 SDNode *Node = Op.getNode();
1321 LoadSDNode *LD = cast<LoadSDNode>(Node);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001322 SDLoc dl(Node);
Justin Holewinski0497ab12013-03-30 14:29:21 +00001323 assert(LD->getExtensionType() == ISD::NON_EXTLOAD);
NAKAMURA Takumi5bbe0e12012-11-14 23:46:15 +00001324 assert(Node->getValueType(0) == MVT::i1 &&
1325 "Custom lowering for i1 load only");
Justin Holewinski0497ab12013-03-30 14:29:21 +00001326 SDValue newLD =
Justin Holewinskif8f70912013-06-28 17:57:59 +00001327 DAG.getLoad(MVT::i16, dl, LD->getChain(), LD->getBasePtr(),
Justin Holewinski0497ab12013-03-30 14:29:21 +00001328 LD->getPointerInfo(), LD->isVolatile(), LD->isNonTemporal(),
1329 LD->isInvariant(), LD->getAlignment());
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001330 SDValue result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, newLD);
1331 // The legalizer (the caller) is expecting two values from the legalized
1332 // load, so we build a MergeValues node for it. See ExpandUnalignedLoad()
1333 // in LegalizeDAG.cpp which also uses MergeValues.
Justin Holewinski0497ab12013-03-30 14:29:21 +00001334 SDValue Ops[] = { result, LD->getChain() };
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001335 return DAG.getMergeValues(Ops, 2, dl);
1336}
1337
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001338SDValue NVPTXTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1339 EVT ValVT = Op.getOperand(1).getValueType();
1340 if (ValVT == MVT::i1)
1341 return LowerSTOREi1(Op, DAG);
1342 else if (ValVT.isVector())
1343 return LowerSTOREVector(Op, DAG);
1344 else
1345 return SDValue();
1346}
1347
1348SDValue
1349NVPTXTargetLowering::LowerSTOREVector(SDValue Op, SelectionDAG &DAG) const {
1350 SDNode *N = Op.getNode();
1351 SDValue Val = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001352 SDLoc DL(N);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001353 EVT ValVT = Val.getValueType();
1354
1355 if (ValVT.isVector()) {
1356 // We only handle "native" vector sizes for now, e.g. <4 x double> is not
1357 // legal. We can (and should) split that into 2 stores of <2 x double> here
1358 // but I'm leaving that as a TODO for now.
1359 if (!ValVT.isSimple())
1360 return SDValue();
1361 switch (ValVT.getSimpleVT().SimpleTy) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001362 default:
1363 return SDValue();
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001364 case MVT::v2i8:
1365 case MVT::v2i16:
1366 case MVT::v2i32:
1367 case MVT::v2i64:
1368 case MVT::v2f32:
1369 case MVT::v2f64:
1370 case MVT::v4i8:
1371 case MVT::v4i16:
1372 case MVT::v4i32:
1373 case MVT::v4f32:
1374 // This is a "native" vector type
1375 break;
1376 }
1377
1378 unsigned Opcode = 0;
1379 EVT EltVT = ValVT.getVectorElementType();
1380 unsigned NumElts = ValVT.getVectorNumElements();
1381
1382 // Since StoreV2 is a target node, we cannot rely on DAG type legalization.
1383 // Therefore, we must ensure the type is legal. For i1 and i8, we set the
1384 // stored type to i16 and propogate the "real" type as the memory type.
Justin Holewinskif8f70912013-06-28 17:57:59 +00001385 bool NeedSExt = false;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001386 if (EltVT.getSizeInBits() < 16)
Justin Holewinskif8f70912013-06-28 17:57:59 +00001387 NeedSExt = true;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001388
1389 switch (NumElts) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001390 default:
1391 return SDValue();
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001392 case 2:
1393 Opcode = NVPTXISD::StoreV2;
1394 break;
1395 case 4: {
1396 Opcode = NVPTXISD::StoreV4;
1397 break;
1398 }
1399 }
1400
1401 SmallVector<SDValue, 8> Ops;
1402
1403 // First is the chain
1404 Ops.push_back(N->getOperand(0));
1405
1406 // Then the split values
1407 for (unsigned i = 0; i < NumElts; ++i) {
1408 SDValue ExtVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Val,
1409 DAG.getIntPtrConstant(i));
Justin Holewinskif8f70912013-06-28 17:57:59 +00001410 if (NeedSExt)
1411 ExtVal = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i16, ExtVal);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001412 Ops.push_back(ExtVal);
1413 }
1414
1415 // Then any remaining arguments
1416 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) {
1417 Ops.push_back(N->getOperand(i));
1418 }
1419
1420 MemSDNode *MemSD = cast<MemSDNode>(N);
1421
Justin Holewinski0497ab12013-03-30 14:29:21 +00001422 SDValue NewSt = DAG.getMemIntrinsicNode(
1423 Opcode, DL, DAG.getVTList(MVT::Other), &Ops[0], Ops.size(),
1424 MemSD->getMemoryVT(), MemSD->getMemOperand());
Justin Holewinskibe8dc642013-02-12 14:18:49 +00001425
1426 //return DCI.CombineTo(N, NewSt, true);
1427 return NewSt;
1428 }
1429
1430 return SDValue();
1431}
1432
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001433// st i1 v, addr
1434// =>
Justin Holewinskif8f70912013-06-28 17:57:59 +00001435// v1 = zxt v to i16
1436// st.u8 i16, addr
Justin Holewinski0497ab12013-03-30 14:29:21 +00001437SDValue NVPTXTargetLowering::LowerSTOREi1(SDValue Op, SelectionDAG &DAG) const {
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001438 SDNode *Node = Op.getNode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001439 SDLoc dl(Node);
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001440 StoreSDNode *ST = cast<StoreSDNode>(Node);
1441 SDValue Tmp1 = ST->getChain();
1442 SDValue Tmp2 = ST->getBasePtr();
1443 SDValue Tmp3 = ST->getValue();
NAKAMURA Takumi5bbe0e12012-11-14 23:46:15 +00001444 assert(Tmp3.getValueType() == MVT::i1 && "Custom lowering for i1 store only");
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001445 unsigned Alignment = ST->getAlignment();
1446 bool isVolatile = ST->isVolatile();
1447 bool isNonTemporal = ST->isNonTemporal();
Justin Holewinskif8f70912013-06-28 17:57:59 +00001448 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Tmp3);
1449 SDValue Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2,
1450 ST->getPointerInfo(), MVT::i8, isNonTemporal,
1451 isVolatile, Alignment);
Justin Holewinskic6462aa2012-11-14 19:19:16 +00001452 return Result;
1453}
1454
Justin Holewinski0497ab12013-03-30 14:29:21 +00001455SDValue NVPTXTargetLowering::getExtSymb(SelectionDAG &DAG, const char *inname,
1456 int idx, EVT v) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001457 std::string *name = nvTM->getManagedStrPool()->getManagedString(inname);
1458 std::stringstream suffix;
1459 suffix << idx;
1460 *name += suffix.str();
1461 return DAG.getTargetExternalSymbol(name->c_str(), v);
1462}
1463
1464SDValue
1465NVPTXTargetLowering::getParamSymbol(SelectionDAG &DAG, int idx, EVT v) const {
1466 return getExtSymb(DAG, ".PARAM", idx, v);
1467}
1468
Justin Holewinski0497ab12013-03-30 14:29:21 +00001469SDValue NVPTXTargetLowering::getParamHelpSymbol(SelectionDAG &DAG, int idx) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001470 return getExtSymb(DAG, ".HLPPARAM", idx);
1471}
1472
1473// Check to see if the kernel argument is image*_t or sampler_t
1474
1475bool llvm::isImageOrSamplerVal(const Value *arg, const Module *context) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00001476 static const char *const specialTypes[] = { "struct._image2d_t",
1477 "struct._image3d_t",
1478 "struct._sampler_t" };
Justin Holewinskiae556d32012-05-04 20:18:50 +00001479
1480 const Type *Ty = arg->getType();
1481 const PointerType *PTy = dyn_cast<PointerType>(Ty);
1482
1483 if (!PTy)
1484 return false;
1485
1486 if (!context)
1487 return false;
1488
1489 const StructType *STy = dyn_cast<StructType>(PTy->getElementType());
Justin Holewinskifb711152012-12-05 20:50:28 +00001490 const std::string TypeName = STy && !STy->isLiteral() ? STy->getName() : "";
Justin Holewinskiae556d32012-05-04 20:18:50 +00001491
Craig Toppere4260f92012-05-24 04:22:05 +00001492 for (int i = 0, e = array_lengthof(specialTypes); i != e; ++i)
Justin Holewinskiae556d32012-05-04 20:18:50 +00001493 if (TypeName == specialTypes[i])
1494 return true;
1495
1496 return false;
1497}
1498
Justin Holewinski0497ab12013-03-30 14:29:21 +00001499SDValue NVPTXTargetLowering::LowerFormalArguments(
1500 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001501 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc dl, SelectionDAG &DAG,
Justin Holewinski0497ab12013-03-30 14:29:21 +00001502 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001503 MachineFunction &MF = DAG.getMachineFunction();
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001504 const DataLayout *TD = getDataLayout();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001505
1506 const Function *F = MF.getFunction();
Bill Wendlinge94d8432012-12-07 23:16:57 +00001507 const AttributeSet &PAL = F->getAttributes();
Justin Holewinski44f5c602013-06-28 17:57:53 +00001508 const TargetLowering *TLI = nvTM->getTargetLowering();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001509
1510 SDValue Root = DAG.getRoot();
1511 std::vector<SDValue> OutChains;
1512
1513 bool isKernel = llvm::isKernelFunction(*F);
1514 bool isABI = (nvptxSubtarget.getSmVersion() >= 20);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001515 assert(isABI && "Non-ABI compilation is not supported");
1516 if (!isABI)
1517 return Chain;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001518
1519 std::vector<Type *> argTypes;
1520 std::vector<const Argument *> theArgs;
1521 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Justin Holewinski0497ab12013-03-30 14:29:21 +00001522 I != E; ++I) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001523 theArgs.push_back(I);
1524 argTypes.push_back(I->getType());
1525 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001526 // argTypes.size() (or theArgs.size()) and Ins.size() need not match.
1527 // Ins.size() will be larger
1528 // * if there is an aggregate argument with multiple fields (each field
1529 // showing up separately in Ins)
1530 // * if there is a vector argument with more than typical vector-length
1531 // elements (generally if more than 4) where each vector element is
1532 // individually present in Ins.
1533 // So a different index should be used for indexing into Ins.
1534 // See similar issue in LowerCall.
1535 unsigned InsIdx = 0;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001536
1537 int idx = 0;
Justin Holewinski44f5c602013-06-28 17:57:53 +00001538 for (unsigned i = 0, e = theArgs.size(); i != e; ++i, ++idx, ++InsIdx) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001539 Type *Ty = argTypes[i];
Justin Holewinskiae556d32012-05-04 20:18:50 +00001540
1541 // If the kernel argument is image*_t or sampler_t, convert it to
1542 // a i32 constant holding the parameter position. This can later
1543 // matched in the AsmPrinter to output the correct mangled name.
Justin Holewinski0497ab12013-03-30 14:29:21 +00001544 if (isImageOrSamplerVal(
1545 theArgs[i],
1546 (theArgs[i]->getParent() ? theArgs[i]->getParent()->getParent()
1547 : 0))) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001548 assert(isKernel && "Only kernels can have image/sampler params");
Justin Holewinski0497ab12013-03-30 14:29:21 +00001549 InVals.push_back(DAG.getConstant(i + 1, MVT::i32));
Justin Holewinskiae556d32012-05-04 20:18:50 +00001550 continue;
1551 }
1552
1553 if (theArgs[i]->use_empty()) {
1554 // argument is dead
Justin Holewinski44f5c602013-06-28 17:57:53 +00001555 if (Ty->isAggregateType()) {
1556 SmallVector<EVT, 16> vtparts;
1557
Justin Holewinskif8f70912013-06-28 17:57:59 +00001558 ComputePTXValueVTs(*this, Ty, vtparts);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001559 assert(vtparts.size() > 0 && "empty aggregate type not expected");
1560 for (unsigned parti = 0, parte = vtparts.size(); parti != parte;
1561 ++parti) {
1562 EVT partVT = vtparts[parti];
1563 InVals.push_back(DAG.getNode(ISD::UNDEF, dl, partVT));
1564 ++InsIdx;
Justin Holewinskie9884092013-03-24 21:17:47 +00001565 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001566 if (vtparts.size() > 0)
1567 --InsIdx;
1568 continue;
Justin Holewinskie9884092013-03-24 21:17:47 +00001569 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001570 if (Ty->isVectorTy()) {
1571 EVT ObjectVT = getValueType(Ty);
1572 unsigned NumRegs = TLI->getNumRegisters(F->getContext(), ObjectVT);
1573 for (unsigned parti = 0; parti < NumRegs; ++parti) {
1574 InVals.push_back(DAG.getNode(ISD::UNDEF, dl, Ins[InsIdx].VT));
1575 ++InsIdx;
1576 }
1577 if (NumRegs > 0)
1578 --InsIdx;
1579 continue;
1580 }
1581 InVals.push_back(DAG.getNode(ISD::UNDEF, dl, Ins[InsIdx].VT));
Justin Holewinskiae556d32012-05-04 20:18:50 +00001582 continue;
1583 }
1584
1585 // In the following cases, assign a node order of "idx+1"
Justin Holewinski44f5c602013-06-28 17:57:53 +00001586 // to newly created nodes. The SDNodes for params have to
Justin Holewinskiae556d32012-05-04 20:18:50 +00001587 // appear in the same order as their order of appearance
1588 // in the original function. "idx+1" holds that order.
Justin Holewinski0497ab12013-03-30 14:29:21 +00001589 if (PAL.hasAttribute(i + 1, Attribute::ByVal) == false) {
Justin Holewinski44f5c602013-06-28 17:57:53 +00001590 if (Ty->isAggregateType()) {
1591 SmallVector<EVT, 16> vtparts;
1592 SmallVector<uint64_t, 16> offsets;
1593
Justin Holewinskif8f70912013-06-28 17:57:59 +00001594 // NOTE: Here, we lose the ability to issue vector loads for vectors
1595 // that are a part of a struct. This should be investigated in the
1596 // future.
1597 ComputePTXValueVTs(*this, Ty, vtparts, &offsets, 0);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001598 assert(vtparts.size() > 0 && "empty aggregate type not expected");
1599 bool aggregateIsPacked = false;
1600 if (StructType *STy = llvm::dyn_cast<StructType>(Ty))
1601 aggregateIsPacked = STy->isPacked();
1602
1603 SDValue Arg = getParamSymbol(DAG, idx, getPointerTy());
1604 for (unsigned parti = 0, parte = vtparts.size(); parti != parte;
1605 ++parti) {
1606 EVT partVT = vtparts[parti];
1607 Value *srcValue = Constant::getNullValue(
1608 PointerType::get(partVT.getTypeForEVT(F->getContext()),
1609 llvm::ADDRESS_SPACE_PARAM));
1610 SDValue srcAddr =
1611 DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg,
1612 DAG.getConstant(offsets[parti], getPointerTy()));
1613 unsigned partAlign =
1614 aggregateIsPacked ? 1
1615 : TD->getABITypeAlignment(
1616 partVT.getTypeForEVT(F->getContext()));
Justin Holewinskif8f70912013-06-28 17:57:59 +00001617 SDValue p;
1618 if (Ins[InsIdx].VT.getSizeInBits() > partVT.getSizeInBits())
1619 p = DAG.getExtLoad(ISD::SEXTLOAD, dl, Ins[InsIdx].VT, Root, srcAddr,
1620 MachinePointerInfo(srcValue), partVT, false,
1621 false, partAlign);
1622 else
1623 p = DAG.getLoad(partVT, dl, Root, srcAddr,
1624 MachinePointerInfo(srcValue), false, false, false,
1625 partAlign);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001626 if (p.getNode())
1627 p.getNode()->setIROrder(idx + 1);
1628 InVals.push_back(p);
1629 ++InsIdx;
Justin Holewinskie9884092013-03-24 21:17:47 +00001630 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001631 if (vtparts.size() > 0)
1632 --InsIdx;
Justin Holewinskie9884092013-03-24 21:17:47 +00001633 continue;
1634 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001635 if (Ty->isVectorTy()) {
1636 EVT ObjectVT = getValueType(Ty);
Justin Holewinskiaaaf2892013-06-25 12:22:21 +00001637 SDValue Arg = getParamSymbol(DAG, idx, getPointerTy());
Justin Holewinski44f5c602013-06-28 17:57:53 +00001638 unsigned NumElts = ObjectVT.getVectorNumElements();
1639 assert(TLI->getNumRegisters(F->getContext(), ObjectVT) == NumElts &&
1640 "Vector was not scalarized");
1641 unsigned Ofst = 0;
1642 EVT EltVT = ObjectVT.getVectorElementType();
1643
1644 // V1 load
1645 // f32 = load ...
1646 if (NumElts == 1) {
1647 // We only have one element, so just directly load it
1648 Value *SrcValue = Constant::getNullValue(PointerType::get(
1649 EltVT.getTypeForEVT(F->getContext()), llvm::ADDRESS_SPACE_PARAM));
1650 SDValue SrcAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg,
1651 DAG.getConstant(Ofst, getPointerTy()));
1652 SDValue P = DAG.getLoad(
1653 EltVT, dl, Root, SrcAddr, MachinePointerInfo(SrcValue), false,
1654 false, true,
1655 TD->getABITypeAlignment(EltVT.getTypeForEVT(F->getContext())));
1656 if (P.getNode())
1657 P.getNode()->setIROrder(idx + 1);
1658
Justin Holewinskif8f70912013-06-28 17:57:59 +00001659 if (Ins[InsIdx].VT.getSizeInBits() > EltVT.getSizeInBits())
1660 P = DAG.getNode(ISD::SIGN_EXTEND, dl, Ins[InsIdx].VT, P);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001661 InVals.push_back(P);
1662 Ofst += TD->getTypeAllocSize(EltVT.getTypeForEVT(F->getContext()));
1663 ++InsIdx;
1664 } else if (NumElts == 2) {
1665 // V2 load
1666 // f32,f32 = load ...
1667 EVT VecVT = EVT::getVectorVT(F->getContext(), EltVT, 2);
1668 Value *SrcValue = Constant::getNullValue(PointerType::get(
1669 VecVT.getTypeForEVT(F->getContext()), llvm::ADDRESS_SPACE_PARAM));
1670 SDValue SrcAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg,
1671 DAG.getConstant(Ofst, getPointerTy()));
1672 SDValue P = DAG.getLoad(
1673 VecVT, dl, Root, SrcAddr, MachinePointerInfo(SrcValue), false,
1674 false, true,
1675 TD->getABITypeAlignment(VecVT.getTypeForEVT(F->getContext())));
1676 if (P.getNode())
1677 P.getNode()->setIROrder(idx + 1);
1678
1679 SDValue Elt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, P,
1680 DAG.getIntPtrConstant(0));
1681 SDValue Elt1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, P,
1682 DAG.getIntPtrConstant(1));
Justin Holewinskif8f70912013-06-28 17:57:59 +00001683
1684 if (Ins[InsIdx].VT.getSizeInBits() > EltVT.getSizeInBits()) {
1685 Elt0 = DAG.getNode(ISD::SIGN_EXTEND, dl, Ins[InsIdx].VT, Elt0);
1686 Elt1 = DAG.getNode(ISD::SIGN_EXTEND, dl, Ins[InsIdx].VT, Elt1);
1687 }
1688
Justin Holewinski44f5c602013-06-28 17:57:53 +00001689 InVals.push_back(Elt0);
1690 InVals.push_back(Elt1);
1691 Ofst += TD->getTypeAllocSize(VecVT.getTypeForEVT(F->getContext()));
1692 InsIdx += 2;
1693 } else {
1694 // V4 loads
1695 // We have at least 4 elements (<3 x Ty> expands to 4 elements) and
1696 // the
1697 // vector will be expanded to a power of 2 elements, so we know we can
1698 // always round up to the next multiple of 4 when creating the vector
1699 // loads.
1700 // e.g. 4 elem => 1 ld.v4
1701 // 6 elem => 2 ld.v4
1702 // 8 elem => 2 ld.v4
1703 // 11 elem => 3 ld.v4
1704 unsigned VecSize = 4;
1705 if (EltVT.getSizeInBits() == 64) {
1706 VecSize = 2;
1707 }
1708 EVT VecVT = EVT::getVectorVT(F->getContext(), EltVT, VecSize);
1709 for (unsigned i = 0; i < NumElts; i += VecSize) {
1710 Value *SrcValue = Constant::getNullValue(
1711 PointerType::get(VecVT.getTypeForEVT(F->getContext()),
1712 llvm::ADDRESS_SPACE_PARAM));
1713 SDValue SrcAddr =
1714 DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg,
1715 DAG.getConstant(Ofst, getPointerTy()));
1716 SDValue P = DAG.getLoad(
1717 VecVT, dl, Root, SrcAddr, MachinePointerInfo(SrcValue), false,
1718 false, true,
1719 TD->getABITypeAlignment(VecVT.getTypeForEVT(F->getContext())));
1720 if (P.getNode())
1721 P.getNode()->setIROrder(idx + 1);
1722
1723 for (unsigned j = 0; j < VecSize; ++j) {
1724 if (i + j >= NumElts)
1725 break;
1726 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, P,
1727 DAG.getIntPtrConstant(j));
Justin Holewinskif8f70912013-06-28 17:57:59 +00001728 if (Ins[InsIdx].VT.getSizeInBits() > EltVT.getSizeInBits())
1729 Elt = DAG.getNode(ISD::SIGN_EXTEND, dl, Ins[InsIdx].VT, Elt);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001730 InVals.push_back(Elt);
1731 }
1732 Ofst += TD->getTypeAllocSize(VecVT.getTypeForEVT(F->getContext()));
1733 InsIdx += VecSize;
1734 }
1735 }
1736
1737 if (NumElts > 0)
1738 --InsIdx;
1739 continue;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001740 }
Justin Holewinski44f5c602013-06-28 17:57:53 +00001741 // A plain scalar.
1742 EVT ObjectVT = getValueType(Ty);
Justin Holewinski44f5c602013-06-28 17:57:53 +00001743 // If ABI, load from the param symbol
1744 SDValue Arg = getParamSymbol(DAG, idx, getPointerTy());
1745 Value *srcValue = Constant::getNullValue(PointerType::get(
1746 ObjectVT.getTypeForEVT(F->getContext()), llvm::ADDRESS_SPACE_PARAM));
Justin Holewinskif8f70912013-06-28 17:57:59 +00001747 SDValue p;
1748 if (ObjectVT.getSizeInBits() < Ins[InsIdx].VT.getSizeInBits())
1749 p = DAG.getExtLoad(ISD::SEXTLOAD, dl, Ins[InsIdx].VT, Root, Arg,
1750 MachinePointerInfo(srcValue), ObjectVT, false, false,
1751 TD->getABITypeAlignment(ObjectVT.getTypeForEVT(F->getContext())));
1752 else
1753 p = DAG.getLoad(Ins[InsIdx].VT, dl, Root, Arg,
1754 MachinePointerInfo(srcValue), false, false, false,
1755 TD->getABITypeAlignment(ObjectVT.getTypeForEVT(F->getContext())));
Justin Holewinski44f5c602013-06-28 17:57:53 +00001756 if (p.getNode())
1757 p.getNode()->setIROrder(idx + 1);
1758 InVals.push_back(p);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001759 continue;
1760 }
1761
1762 // Param has ByVal attribute
Justin Holewinski44f5c602013-06-28 17:57:53 +00001763 // Return MoveParam(param symbol).
1764 // Ideally, the param symbol can be returned directly,
1765 // but when SDNode builder decides to use it in a CopyToReg(),
1766 // machine instruction fails because TargetExternalSymbol
1767 // (not lowered) is target dependent, and CopyToReg assumes
1768 // the source is lowered.
1769 EVT ObjectVT = getValueType(Ty);
1770 assert(ObjectVT == Ins[InsIdx].VT &&
1771 "Ins type did not match function type");
1772 SDValue Arg = getParamSymbol(DAG, idx, getPointerTy());
1773 SDValue p = DAG.getNode(NVPTXISD::MoveParam, dl, ObjectVT, Arg);
1774 if (p.getNode())
1775 p.getNode()->setIROrder(idx + 1);
1776 if (isKernel)
1777 InVals.push_back(p);
1778 else {
1779 SDValue p2 = DAG.getNode(
1780 ISD::INTRINSIC_WO_CHAIN, dl, ObjectVT,
1781 DAG.getConstant(Intrinsic::nvvm_ptr_local_to_gen, MVT::i32), p);
1782 InVals.push_back(p2);
Justin Holewinskiae556d32012-05-04 20:18:50 +00001783 }
1784 }
1785
1786 // Clang will check explicit VarArg and issue error if any. However, Clang
1787 // will let code with
Justin Holewinski44f5c602013-06-28 17:57:53 +00001788 // implicit var arg like f() pass. See bug 617733.
Justin Holewinskiae556d32012-05-04 20:18:50 +00001789 // We treat this case as if the arg list is empty.
Justin Holewinski44f5c602013-06-28 17:57:53 +00001790 // if (F.isVarArg()) {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001791 // assert(0 && "VarArg not supported yet!");
1792 //}
1793
1794 if (!OutChains.empty())
Justin Holewinski0497ab12013-03-30 14:29:21 +00001795 DAG.setRoot(DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &OutChains[0],
1796 OutChains.size()));
Justin Holewinskiae556d32012-05-04 20:18:50 +00001797
1798 return Chain;
1799}
1800
Justin Holewinski44f5c602013-06-28 17:57:53 +00001801
Justin Holewinski120baee2013-06-28 17:57:55 +00001802SDValue
1803NVPTXTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1804 bool isVarArg,
1805 const SmallVectorImpl<ISD::OutputArg> &Outs,
1806 const SmallVectorImpl<SDValue> &OutVals,
1807 SDLoc dl, SelectionDAG &DAG) const {
1808 MachineFunction &MF = DAG.getMachineFunction();
1809 const Function *F = MF.getFunction();
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001810 Type *RetTy = F->getReturnType();
Justin Holewinski120baee2013-06-28 17:57:55 +00001811 const DataLayout *TD = getDataLayout();
Justin Holewinskiae556d32012-05-04 20:18:50 +00001812
1813 bool isABI = (nvptxSubtarget.getSmVersion() >= 20);
Justin Holewinski120baee2013-06-28 17:57:55 +00001814 assert(isABI && "Non-ABI compilation is not supported");
1815 if (!isABI)
1816 return Chain;
Justin Holewinskiae556d32012-05-04 20:18:50 +00001817
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001818 if (VectorType *VTy = dyn_cast<VectorType>(RetTy)) {
Justin Holewinski120baee2013-06-28 17:57:55 +00001819 // If we have a vector type, the OutVals array will be the scalarized
1820 // components and we have combine them into 1 or more vector stores.
1821 unsigned NumElts = VTy->getNumElements();
1822 assert(NumElts == Outs.size() && "Bad scalarization of return value");
1823
Justin Holewinskif8f70912013-06-28 17:57:59 +00001824 // const_cast can be removed in later LLVM versions
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001825 EVT EltVT = getValueType(RetTy).getVectorElementType();
Justin Holewinskif8f70912013-06-28 17:57:59 +00001826 bool NeedExtend = false;
1827 if (EltVT.getSizeInBits() < 16)
1828 NeedExtend = true;
1829
Justin Holewinski120baee2013-06-28 17:57:55 +00001830 // V1 store
1831 if (NumElts == 1) {
1832 SDValue StoreVal = OutVals[0];
1833 // We only have one element, so just directly store it
Justin Holewinskif8f70912013-06-28 17:57:59 +00001834 if (NeedExtend)
1835 StoreVal = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal);
1836 SDValue Ops[] = { Chain, DAG.getConstant(0, MVT::i32), StoreVal };
1837 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreRetval, dl,
1838 DAG.getVTList(MVT::Other), &Ops[0], 3,
1839 EltVT, MachinePointerInfo());
1840
Justin Holewinski120baee2013-06-28 17:57:55 +00001841 } else if (NumElts == 2) {
1842 // V2 store
1843 SDValue StoreVal0 = OutVals[0];
1844 SDValue StoreVal1 = OutVals[1];
1845
Justin Holewinskif8f70912013-06-28 17:57:59 +00001846 if (NeedExtend) {
1847 StoreVal0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal0);
1848 StoreVal1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, StoreVal1);
Justin Holewinski120baee2013-06-28 17:57:55 +00001849 }
1850
Justin Holewinskif8f70912013-06-28 17:57:59 +00001851 SDValue Ops[] = { Chain, DAG.getConstant(0, MVT::i32), StoreVal0,
1852 StoreVal1 };
1853 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreRetvalV2, dl,
1854 DAG.getVTList(MVT::Other), &Ops[0], 4,
1855 EltVT, MachinePointerInfo());
Justin Holewinski120baee2013-06-28 17:57:55 +00001856 } else {
1857 // V4 stores
1858 // We have at least 4 elements (<3 x Ty> expands to 4 elements) and the
1859 // vector will be expanded to a power of 2 elements, so we know we can
1860 // always round up to the next multiple of 4 when creating the vector
1861 // stores.
1862 // e.g. 4 elem => 1 st.v4
1863 // 6 elem => 2 st.v4
1864 // 8 elem => 2 st.v4
1865 // 11 elem => 3 st.v4
1866
1867 unsigned VecSize = 4;
1868 if (OutVals[0].getValueType().getSizeInBits() == 64)
1869 VecSize = 2;
1870
1871 unsigned Offset = 0;
1872
1873 EVT VecVT =
1874 EVT::getVectorVT(F->getContext(), OutVals[0].getValueType(), VecSize);
1875 unsigned PerStoreOffset =
1876 TD->getTypeAllocSize(VecVT.getTypeForEVT(F->getContext()));
1877
Justin Holewinski120baee2013-06-28 17:57:55 +00001878 for (unsigned i = 0; i < NumElts; i += VecSize) {
1879 // Get values
1880 SDValue StoreVal;
1881 SmallVector<SDValue, 8> Ops;
1882 Ops.push_back(Chain);
1883 Ops.push_back(DAG.getConstant(Offset, MVT::i32));
1884 unsigned Opc = NVPTXISD::StoreRetvalV2;
Justin Holewinskif8f70912013-06-28 17:57:59 +00001885 EVT ExtendedVT = (NeedExtend) ? MVT::i16 : OutVals[0].getValueType();
Justin Holewinski120baee2013-06-28 17:57:55 +00001886
1887 StoreVal = OutVals[i];
Justin Holewinskif8f70912013-06-28 17:57:59 +00001888 if (NeedExtend)
1889 StoreVal = DAG.getNode(ISD::ZERO_EXTEND, dl, ExtendedVT, StoreVal);
Justin Holewinski120baee2013-06-28 17:57:55 +00001890 Ops.push_back(StoreVal);
1891
1892 if (i + 1 < NumElts) {
1893 StoreVal = OutVals[i + 1];
Justin Holewinskif8f70912013-06-28 17:57:59 +00001894 if (NeedExtend)
1895 StoreVal = DAG.getNode(ISD::ZERO_EXTEND, dl, ExtendedVT, StoreVal);
Justin Holewinski120baee2013-06-28 17:57:55 +00001896 } else {
1897 StoreVal = DAG.getUNDEF(ExtendedVT);
1898 }
1899 Ops.push_back(StoreVal);
1900
1901 if (VecSize == 4) {
1902 Opc = NVPTXISD::StoreRetvalV4;
1903 if (i + 2 < NumElts) {
1904 StoreVal = OutVals[i + 2];
Justin Holewinskif8f70912013-06-28 17:57:59 +00001905 if (NeedExtend)
1906 StoreVal =
1907 DAG.getNode(ISD::ZERO_EXTEND, dl, ExtendedVT, StoreVal);
Justin Holewinski120baee2013-06-28 17:57:55 +00001908 } else {
1909 StoreVal = DAG.getUNDEF(ExtendedVT);
1910 }
1911 Ops.push_back(StoreVal);
1912
1913 if (i + 3 < NumElts) {
1914 StoreVal = OutVals[i + 3];
Justin Holewinskif8f70912013-06-28 17:57:59 +00001915 if (NeedExtend)
1916 StoreVal =
1917 DAG.getNode(ISD::ZERO_EXTEND, dl, ExtendedVT, StoreVal);
Justin Holewinski120baee2013-06-28 17:57:55 +00001918 } else {
1919 StoreVal = DAG.getUNDEF(ExtendedVT);
1920 }
1921 Ops.push_back(StoreVal);
1922 }
1923
Justin Holewinskif8f70912013-06-28 17:57:59 +00001924 // Chain = DAG.getNode(Opc, dl, MVT::Other, &Ops[0], Ops.size());
1925 Chain =
1926 DAG.getMemIntrinsicNode(Opc, dl, DAG.getVTList(MVT::Other), &Ops[0],
1927 Ops.size(), EltVT, MachinePointerInfo());
Justin Holewinski120baee2013-06-28 17:57:55 +00001928 Offset += PerStoreOffset;
1929 }
1930 }
1931 } else {
Justin Holewinskif8f70912013-06-28 17:57:59 +00001932 SmallVector<EVT, 16> ValVTs;
1933 // const_cast is necessary since we are still using an LLVM version from
1934 // before the type system re-write.
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001935 ComputePTXValueVTs(*this, RetTy, ValVTs);
Justin Holewinskif8f70912013-06-28 17:57:59 +00001936 assert(ValVTs.size() == OutVals.size() && "Bad return value decomposition");
1937
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001938 unsigned SizeSoFar = 0;
Justin Holewinski120baee2013-06-28 17:57:55 +00001939 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1940 SDValue theVal = OutVals[i];
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001941 EVT TheValType = theVal.getValueType();
Justin Holewinski120baee2013-06-28 17:57:55 +00001942 unsigned numElems = 1;
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001943 if (TheValType.isVector())
1944 numElems = TheValType.getVectorNumElements();
Justin Holewinski120baee2013-06-28 17:57:55 +00001945 for (unsigned j = 0, je = numElems; j != je; ++j) {
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001946 SDValue TmpVal = theVal;
1947 if (TheValType.isVector())
1948 TmpVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
1949 TheValType.getVectorElementType(), TmpVal,
Justin Holewinski120baee2013-06-28 17:57:55 +00001950 DAG.getIntPtrConstant(j));
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001951 EVT TheStoreType = ValVTs[i];
1952 if (RetTy->isIntegerTy() &&
1953 TD->getTypeAllocSizeInBits(RetTy) < 32) {
1954 // The following zero-extension is for integer types only, and
1955 // specifically not for aggregates.
1956 TmpVal = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, TmpVal);
1957 TheStoreType = MVT::i32;
1958 }
1959 else if (TmpVal.getValueType().getSizeInBits() < 16)
1960 TmpVal = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i16, TmpVal);
1961
1962 SDValue Ops[] = { Chain, DAG.getConstant(SizeSoFar, MVT::i32), TmpVal };
Justin Holewinskif8f70912013-06-28 17:57:59 +00001963 Chain = DAG.getMemIntrinsicNode(NVPTXISD::StoreRetval, dl,
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001964 DAG.getVTList(MVT::Other), &Ops[0],
1965 3, TheStoreType,
1966 MachinePointerInfo());
1967 if(TheValType.isVector())
1968 SizeSoFar +=
1969 TheStoreType.getVectorElementType().getStoreSizeInBits() / 8;
Justin Holewinski120baee2013-06-28 17:57:55 +00001970 else
Justin Holewinskie04e4bd2013-06-28 17:58:10 +00001971 SizeSoFar += TheStoreType.getStoreSizeInBits()/8;
Justin Holewinski120baee2013-06-28 17:57:55 +00001972 }
Justin Holewinskiae556d32012-05-04 20:18:50 +00001973 }
1974 }
1975
1976 return DAG.getNode(NVPTXISD::RET_FLAG, dl, MVT::Other, Chain);
1977}
1978
Justin Holewinskif8f70912013-06-28 17:57:59 +00001979
Justin Holewinski0497ab12013-03-30 14:29:21 +00001980void NVPTXTargetLowering::LowerAsmOperandForConstraint(
1981 SDValue Op, std::string &Constraint, std::vector<SDValue> &Ops,
1982 SelectionDAG &DAG) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001983 if (Constraint.length() > 1)
1984 return;
1985 else
1986 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
1987}
1988
1989// NVPTX suuport vector of legal types of any length in Intrinsics because the
1990// NVPTX specific type legalizer
1991// will legalize them to the PTX supported length.
Justin Holewinski0497ab12013-03-30 14:29:21 +00001992bool NVPTXTargetLowering::isTypeSupportedInIntrinsic(MVT VT) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00001993 if (isTypeLegal(VT))
1994 return true;
1995 if (VT.isVector()) {
1996 MVT eVT = VT.getVectorElementType();
1997 if (isTypeLegal(eVT))
1998 return true;
1999 }
2000 return false;
2001}
2002
Justin Holewinskiae556d32012-05-04 20:18:50 +00002003// llvm.ptx.memcpy.const and llvm.ptx.memmove.const need to be modeled as
2004// TgtMemIntrinsic
2005// because we need the information that is only available in the "Value" type
2006// of destination
2007// pointer. In particular, the address space information.
Justin Holewinski0497ab12013-03-30 14:29:21 +00002008bool NVPTXTargetLowering::getTgtMemIntrinsic(
2009 IntrinsicInfo &Info, const CallInst &I, unsigned Intrinsic) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00002010 switch (Intrinsic) {
2011 default:
2012 return false;
2013
2014 case Intrinsic::nvvm_atomic_load_add_f32:
2015 Info.opc = ISD::INTRINSIC_W_CHAIN;
2016 Info.memVT = MVT::f32;
2017 Info.ptrVal = I.getArgOperand(0);
2018 Info.offset = 0;
2019 Info.vol = 0;
2020 Info.readMem = true;
2021 Info.writeMem = true;
2022 Info.align = 0;
2023 return true;
2024
2025 case Intrinsic::nvvm_atomic_load_inc_32:
2026 case Intrinsic::nvvm_atomic_load_dec_32:
2027 Info.opc = ISD::INTRINSIC_W_CHAIN;
2028 Info.memVT = MVT::i32;
2029 Info.ptrVal = I.getArgOperand(0);
2030 Info.offset = 0;
2031 Info.vol = 0;
2032 Info.readMem = true;
2033 Info.writeMem = true;
2034 Info.align = 0;
2035 return true;
2036
2037 case Intrinsic::nvvm_ldu_global_i:
2038 case Intrinsic::nvvm_ldu_global_f:
2039 case Intrinsic::nvvm_ldu_global_p:
2040
2041 Info.opc = ISD::INTRINSIC_W_CHAIN;
2042 if (Intrinsic == Intrinsic::nvvm_ldu_global_i)
Justin Holewinskif8f70912013-06-28 17:57:59 +00002043 Info.memVT = getValueType(I.getType());
Justin Holewinskiae556d32012-05-04 20:18:50 +00002044 else if (Intrinsic == Intrinsic::nvvm_ldu_global_p)
Justin Holewinskif8f70912013-06-28 17:57:59 +00002045 Info.memVT = getValueType(I.getType());
Justin Holewinskiae556d32012-05-04 20:18:50 +00002046 else
2047 Info.memVT = MVT::f32;
2048 Info.ptrVal = I.getArgOperand(0);
2049 Info.offset = 0;
2050 Info.vol = 0;
2051 Info.readMem = true;
2052 Info.writeMem = false;
2053 Info.align = 0;
2054 return true;
2055
2056 }
2057 return false;
2058}
2059
2060/// isLegalAddressingMode - Return true if the addressing mode represented
2061/// by AM is legal for this target, for a load/store of the specified type.
2062/// Used to guide target specific optimizations, like loop strength reduction
2063/// (LoopStrengthReduce.cpp) and memory optimization for address mode
2064/// (CodeGenPrepare.cpp)
Justin Holewinski0497ab12013-03-30 14:29:21 +00002065bool NVPTXTargetLowering::isLegalAddressingMode(const AddrMode &AM,
2066 Type *Ty) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00002067
2068 // AddrMode - This represents an addressing mode of:
2069 // BaseGV + BaseOffs + BaseReg + Scale*ScaleReg
2070 //
2071 // The legal address modes are
2072 // - [avar]
2073 // - [areg]
2074 // - [areg+immoff]
2075 // - [immAddr]
2076
2077 if (AM.BaseGV) {
2078 if (AM.BaseOffs || AM.HasBaseReg || AM.Scale)
2079 return false;
2080 return true;
2081 }
2082
2083 switch (AM.Scale) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00002084 case 0: // "r", "r+i" or "i" is allowed
Justin Holewinskiae556d32012-05-04 20:18:50 +00002085 break;
2086 case 1:
Justin Holewinski0497ab12013-03-30 14:29:21 +00002087 if (AM.HasBaseReg) // "r+r+i" or "r+r" is not allowed.
Justin Holewinskiae556d32012-05-04 20:18:50 +00002088 return false;
2089 // Otherwise we have r+i.
2090 break;
2091 default:
2092 // No scale > 1 is allowed
2093 return false;
2094 }
2095 return true;
2096}
2097
2098//===----------------------------------------------------------------------===//
2099// NVPTX Inline Assembly Support
2100//===----------------------------------------------------------------------===//
2101
2102/// getConstraintType - Given a constraint letter, return the type of
2103/// constraint it is for this target.
2104NVPTXTargetLowering::ConstraintType
2105NVPTXTargetLowering::getConstraintType(const std::string &Constraint) const {
2106 if (Constraint.size() == 1) {
2107 switch (Constraint[0]) {
2108 default:
2109 break;
2110 case 'r':
2111 case 'h':
2112 case 'c':
2113 case 'l':
2114 case 'f':
2115 case 'd':
2116 case '0':
2117 case 'N':
2118 return C_RegisterClass;
2119 }
2120 }
2121 return TargetLowering::getConstraintType(Constraint);
2122}
2123
Justin Holewinski0497ab12013-03-30 14:29:21 +00002124std::pair<unsigned, const TargetRegisterClass *>
Justin Holewinskiae556d32012-05-04 20:18:50 +00002125NVPTXTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00002126 MVT VT) const {
Justin Holewinskiae556d32012-05-04 20:18:50 +00002127 if (Constraint.size() == 1) {
2128 switch (Constraint[0]) {
2129 case 'c':
Justin Holewinskif8f70912013-06-28 17:57:59 +00002130 return std::make_pair(0U, &NVPTX::Int16RegsRegClass);
Justin Holewinskiae556d32012-05-04 20:18:50 +00002131 case 'h':
2132 return std::make_pair(0U, &NVPTX::Int16RegsRegClass);
2133 case 'r':
2134 return std::make_pair(0U, &NVPTX::Int32RegsRegClass);
2135 case 'l':
2136 case 'N':
2137 return std::make_pair(0U, &NVPTX::Int64RegsRegClass);
2138 case 'f':
2139 return std::make_pair(0U, &NVPTX::Float32RegsRegClass);
2140 case 'd':
2141 return std::make_pair(0U, &NVPTX::Float64RegsRegClass);
2142 }
2143 }
2144 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2145}
2146
Justin Holewinskiae556d32012-05-04 20:18:50 +00002147/// getFunctionAlignment - Return the Log2 alignment of this function.
2148unsigned NVPTXTargetLowering::getFunctionAlignment(const Function *) const {
2149 return 4;
2150}
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002151
2152/// ReplaceVectorLoad - Convert vector loads into multi-output scalar loads.
2153static void ReplaceLoadVector(SDNode *N, SelectionDAG &DAG,
Justin Holewinski0497ab12013-03-30 14:29:21 +00002154 SmallVectorImpl<SDValue> &Results) {
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002155 EVT ResVT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002156 SDLoc DL(N);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002157
2158 assert(ResVT.isVector() && "Vector load must have vector type");
2159
2160 // We only handle "native" vector sizes for now, e.g. <4 x double> is not
2161 // legal. We can (and should) split that into 2 loads of <2 x double> here
2162 // but I'm leaving that as a TODO for now.
2163 assert(ResVT.isSimple() && "Can only handle simple types");
2164 switch (ResVT.getSimpleVT().SimpleTy) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00002165 default:
2166 return;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002167 case MVT::v2i8:
2168 case MVT::v2i16:
2169 case MVT::v2i32:
2170 case MVT::v2i64:
2171 case MVT::v2f32:
2172 case MVT::v2f64:
2173 case MVT::v4i8:
2174 case MVT::v4i16:
2175 case MVT::v4i32:
2176 case MVT::v4f32:
2177 // This is a "native" vector type
2178 break;
2179 }
2180
2181 EVT EltVT = ResVT.getVectorElementType();
2182 unsigned NumElts = ResVT.getVectorNumElements();
2183
2184 // Since LoadV2 is a target node, we cannot rely on DAG type legalization.
2185 // Therefore, we must ensure the type is legal. For i1 and i8, we set the
2186 // loaded type to i16 and propogate the "real" type as the memory type.
2187 bool NeedTrunc = false;
2188 if (EltVT.getSizeInBits() < 16) {
2189 EltVT = MVT::i16;
2190 NeedTrunc = true;
2191 }
2192
2193 unsigned Opcode = 0;
2194 SDVTList LdResVTs;
2195
2196 switch (NumElts) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00002197 default:
2198 return;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002199 case 2:
2200 Opcode = NVPTXISD::LoadV2;
2201 LdResVTs = DAG.getVTList(EltVT, EltVT, MVT::Other);
2202 break;
2203 case 4: {
2204 Opcode = NVPTXISD::LoadV4;
2205 EVT ListVTs[] = { EltVT, EltVT, EltVT, EltVT, MVT::Other };
2206 LdResVTs = DAG.getVTList(ListVTs, 5);
2207 break;
2208 }
2209 }
2210
2211 SmallVector<SDValue, 8> OtherOps;
2212
2213 // Copy regular operands
2214 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2215 OtherOps.push_back(N->getOperand(i));
2216
2217 LoadSDNode *LD = cast<LoadSDNode>(N);
2218
2219 // The select routine does not have access to the LoadSDNode instance, so
2220 // pass along the extension information
2221 OtherOps.push_back(DAG.getIntPtrConstant(LD->getExtensionType()));
2222
2223 SDValue NewLD = DAG.getMemIntrinsicNode(Opcode, DL, LdResVTs, &OtherOps[0],
2224 OtherOps.size(), LD->getMemoryVT(),
2225 LD->getMemOperand());
2226
2227 SmallVector<SDValue, 4> ScalarRes;
2228
2229 for (unsigned i = 0; i < NumElts; ++i) {
2230 SDValue Res = NewLD.getValue(i);
2231 if (NeedTrunc)
2232 Res = DAG.getNode(ISD::TRUNCATE, DL, ResVT.getVectorElementType(), Res);
2233 ScalarRes.push_back(Res);
2234 }
2235
2236 SDValue LoadChain = NewLD.getValue(NumElts);
2237
Justin Holewinski0497ab12013-03-30 14:29:21 +00002238 SDValue BuildVec =
2239 DAG.getNode(ISD::BUILD_VECTOR, DL, ResVT, &ScalarRes[0], NumElts);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002240
2241 Results.push_back(BuildVec);
2242 Results.push_back(LoadChain);
2243}
2244
Justin Holewinski0497ab12013-03-30 14:29:21 +00002245static void ReplaceINTRINSIC_W_CHAIN(SDNode *N, SelectionDAG &DAG,
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002246 SmallVectorImpl<SDValue> &Results) {
2247 SDValue Chain = N->getOperand(0);
2248 SDValue Intrin = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002249 SDLoc DL(N);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002250
2251 // Get the intrinsic ID
2252 unsigned IntrinNo = cast<ConstantSDNode>(Intrin.getNode())->getZExtValue();
Justin Holewinski0497ab12013-03-30 14:29:21 +00002253 switch (IntrinNo) {
2254 default:
2255 return;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002256 case Intrinsic::nvvm_ldg_global_i:
2257 case Intrinsic::nvvm_ldg_global_f:
2258 case Intrinsic::nvvm_ldg_global_p:
2259 case Intrinsic::nvvm_ldu_global_i:
2260 case Intrinsic::nvvm_ldu_global_f:
2261 case Intrinsic::nvvm_ldu_global_p: {
2262 EVT ResVT = N->getValueType(0);
2263
2264 if (ResVT.isVector()) {
2265 // Vector LDG/LDU
2266
2267 unsigned NumElts = ResVT.getVectorNumElements();
2268 EVT EltVT = ResVT.getVectorElementType();
2269
Justin Holewinskif8f70912013-06-28 17:57:59 +00002270 // Since LDU/LDG are target nodes, we cannot rely on DAG type
2271 // legalization.
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002272 // Therefore, we must ensure the type is legal. For i1 and i8, we set the
2273 // loaded type to i16 and propogate the "real" type as the memory type.
2274 bool NeedTrunc = false;
2275 if (EltVT.getSizeInBits() < 16) {
2276 EltVT = MVT::i16;
2277 NeedTrunc = true;
2278 }
2279
2280 unsigned Opcode = 0;
2281 SDVTList LdResVTs;
2282
2283 switch (NumElts) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00002284 default:
2285 return;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002286 case 2:
Justin Holewinski0497ab12013-03-30 14:29:21 +00002287 switch (IntrinNo) {
2288 default:
2289 return;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002290 case Intrinsic::nvvm_ldg_global_i:
2291 case Intrinsic::nvvm_ldg_global_f:
2292 case Intrinsic::nvvm_ldg_global_p:
2293 Opcode = NVPTXISD::LDGV2;
2294 break;
2295 case Intrinsic::nvvm_ldu_global_i:
2296 case Intrinsic::nvvm_ldu_global_f:
2297 case Intrinsic::nvvm_ldu_global_p:
2298 Opcode = NVPTXISD::LDUV2;
2299 break;
2300 }
2301 LdResVTs = DAG.getVTList(EltVT, EltVT, MVT::Other);
2302 break;
2303 case 4: {
Justin Holewinski0497ab12013-03-30 14:29:21 +00002304 switch (IntrinNo) {
2305 default:
2306 return;
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002307 case Intrinsic::nvvm_ldg_global_i:
2308 case Intrinsic::nvvm_ldg_global_f:
2309 case Intrinsic::nvvm_ldg_global_p:
2310 Opcode = NVPTXISD::LDGV4;
2311 break;
2312 case Intrinsic::nvvm_ldu_global_i:
2313 case Intrinsic::nvvm_ldu_global_f:
2314 case Intrinsic::nvvm_ldu_global_p:
2315 Opcode = NVPTXISD::LDUV4;
2316 break;
2317 }
2318 EVT ListVTs[] = { EltVT, EltVT, EltVT, EltVT, MVT::Other };
2319 LdResVTs = DAG.getVTList(ListVTs, 5);
2320 break;
2321 }
2322 }
2323
2324 SmallVector<SDValue, 8> OtherOps;
2325
2326 // Copy regular operands
2327
2328 OtherOps.push_back(Chain); // Chain
Justin Holewinski0497ab12013-03-30 14:29:21 +00002329 // Skip operand 1 (intrinsic ID)
Justin Holewinskif8f70912013-06-28 17:57:59 +00002330 // Others
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002331 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i)
2332 OtherOps.push_back(N->getOperand(i));
2333
2334 MemIntrinsicSDNode *MemSD = cast<MemIntrinsicSDNode>(N);
2335
Justin Holewinski0497ab12013-03-30 14:29:21 +00002336 SDValue NewLD = DAG.getMemIntrinsicNode(
2337 Opcode, DL, LdResVTs, &OtherOps[0], OtherOps.size(),
2338 MemSD->getMemoryVT(), MemSD->getMemOperand());
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002339
2340 SmallVector<SDValue, 4> ScalarRes;
2341
2342 for (unsigned i = 0; i < NumElts; ++i) {
2343 SDValue Res = NewLD.getValue(i);
2344 if (NeedTrunc)
Justin Holewinski0497ab12013-03-30 14:29:21 +00002345 Res =
2346 DAG.getNode(ISD::TRUNCATE, DL, ResVT.getVectorElementType(), Res);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002347 ScalarRes.push_back(Res);
2348 }
2349
2350 SDValue LoadChain = NewLD.getValue(NumElts);
2351
Justin Holewinski0497ab12013-03-30 14:29:21 +00002352 SDValue BuildVec =
2353 DAG.getNode(ISD::BUILD_VECTOR, DL, ResVT, &ScalarRes[0], NumElts);
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002354
2355 Results.push_back(BuildVec);
2356 Results.push_back(LoadChain);
2357 } else {
2358 // i8 LDG/LDU
2359 assert(ResVT.isSimple() && ResVT.getSimpleVT().SimpleTy == MVT::i8 &&
2360 "Custom handling of non-i8 ldu/ldg?");
2361
2362 // Just copy all operands as-is
2363 SmallVector<SDValue, 4> Ops;
2364 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2365 Ops.push_back(N->getOperand(i));
2366
2367 // Force output to i16
2368 SDVTList LdResVTs = DAG.getVTList(MVT::i16, MVT::Other);
2369
2370 MemIntrinsicSDNode *MemSD = cast<MemIntrinsicSDNode>(N);
2371
2372 // We make sure the memory type is i8, which will be used during isel
2373 // to select the proper instruction.
Justin Holewinski0497ab12013-03-30 14:29:21 +00002374 SDValue NewLD =
2375 DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, DL, LdResVTs, &Ops[0],
2376 Ops.size(), MVT::i8, MemSD->getMemOperand());
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002377
Justin Holewinskie8c93e32013-07-01 12:58:48 +00002378 Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i8,
2379 NewLD.getValue(0)));
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002380 Results.push_back(NewLD.getValue(1));
2381 }
2382 }
2383 }
2384}
2385
Justin Holewinski0497ab12013-03-30 14:29:21 +00002386void NVPTXTargetLowering::ReplaceNodeResults(
2387 SDNode *N, SmallVectorImpl<SDValue> &Results, SelectionDAG &DAG) const {
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002388 switch (N->getOpcode()) {
Justin Holewinski0497ab12013-03-30 14:29:21 +00002389 default:
2390 report_fatal_error("Unhandled custom legalization");
Justin Holewinskibe8dc642013-02-12 14:18:49 +00002391 case ISD::LOAD:
2392 ReplaceLoadVector(N, DAG, Results);
2393 return;
2394 case ISD::INTRINSIC_W_CHAIN:
2395 ReplaceINTRINSIC_W_CHAIN(N, DAG, Results);
2396 return;
2397 }
2398}