blob: 70eb3e1a82cff37f7b849a3118af3b1264f41174 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- AMDGPUISelLowering.cpp - AMDGPU Common DAG lowering functions -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// \brief This is the parent TargetLowering class for hardware code gen
12/// targets.
13//
14//===----------------------------------------------------------------------===//
15
16#include "AMDGPUISelLowering.h"
Tom Stellarded882c22013-06-03 17:40:11 +000017#include "AMDGPU.h"
Tom Stellardca166212017-01-30 21:56:46 +000018#include "AMDGPUCallLowering.h"
Tom Stellard81d871d2013-11-13 23:36:50 +000019#include "AMDGPUFrameLowering.h"
Matt Arsenaultc791f392014-06-23 18:00:31 +000020#include "AMDGPUIntrinsicInfo.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000021#include "AMDGPURegisterInfo.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000022#include "AMDGPUSubtarget.h"
Tom Stellardacfeebf2013-07-23 01:48:05 +000023#include "R600MachineFunctionInfo.h"
Tom Stellarded882c22013-06-03 17:40:11 +000024#include "SIMachineFunctionInfo.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000025#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000026#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineRegisterInfo.h"
28#include "llvm/CodeGen/SelectionDAG.h"
29#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Tom Stellardc026e8b2013-06-28 15:47:08 +000030#include "llvm/IR/DataLayout.h"
Oliver Stannard7e7d9832016-02-02 13:52:43 +000031#include "llvm/IR/DiagnosticInfo.h"
Matt Arsenault6e3a4512016-01-18 22:01:13 +000032#include "SIInstrInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000033using namespace llvm;
Matt Arsenault16353872014-04-22 16:42:00 +000034
Matt Arsenaulte935f052016-06-18 05:15:53 +000035static bool allocateKernArg(unsigned ValNo, MVT ValVT, MVT LocVT,
36 CCValAssign::LocInfo LocInfo,
37 ISD::ArgFlagsTy ArgFlags, CCState &State) {
38 MachineFunction &MF = State.getMachineFunction();
39 AMDGPUMachineFunction *MFI = MF.getInfo<AMDGPUMachineFunction>();
Tom Stellardaf775432013-10-23 00:44:32 +000040
Tom Stellardbbeb45a2016-09-16 21:53:00 +000041 uint64_t Offset = MFI->allocateKernArg(LocVT.getStoreSize(),
Matt Arsenaulte935f052016-06-18 05:15:53 +000042 ArgFlags.getOrigAlign());
43 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tom Stellardaf775432013-10-23 00:44:32 +000044 return true;
45}
Tom Stellard75aadc22012-12-11 21:25:42 +000046
Christian Konig2c8f6d52013-03-07 09:03:52 +000047#include "AMDGPUGenCallingConv.inc"
48
Matt Arsenaultc9df7942014-06-11 03:29:54 +000049// Find a larger type to do a load / store of a vector with.
50EVT AMDGPUTargetLowering::getEquivalentMemType(LLVMContext &Ctx, EVT VT) {
51 unsigned StoreSize = VT.getStoreSizeInBits();
52 if (StoreSize <= 32)
53 return EVT::getIntegerVT(Ctx, StoreSize);
54
55 assert(StoreSize % 32 == 0 && "Store size not a multiple of 32");
56 return EVT::getVectorVT(Ctx, MVT::i32, StoreSize / 32);
57}
58
Matt Arsenault43e92fe2016-06-24 06:30:11 +000059AMDGPUTargetLowering::AMDGPUTargetLowering(const TargetMachine &TM,
Eric Christopher7792e322015-01-30 23:24:40 +000060 const AMDGPUSubtarget &STI)
61 : TargetLowering(TM), Subtarget(&STI) {
Tom Stellard75aadc22012-12-11 21:25:42 +000062 // Lower floating point store/load to integer store/load to reduce the number
63 // of patterns in tablegen.
Tom Stellard75aadc22012-12-11 21:25:42 +000064 setOperationAction(ISD::LOAD, MVT::f32, Promote);
65 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
66
Tom Stellardadf732c2013-07-18 21:43:48 +000067 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
68 AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32);
69
Tom Stellard75aadc22012-12-11 21:25:42 +000070 setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
71 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
72
Tom Stellardaf775432013-10-23 00:44:32 +000073 setOperationAction(ISD::LOAD, MVT::v8f32, Promote);
74 AddPromotedToType(ISD::LOAD, MVT::v8f32, MVT::v8i32);
75
76 setOperationAction(ISD::LOAD, MVT::v16f32, Promote);
77 AddPromotedToType(ISD::LOAD, MVT::v16f32, MVT::v16i32);
78
Matt Arsenault71e66762016-05-21 02:27:49 +000079 setOperationAction(ISD::LOAD, MVT::i64, Promote);
80 AddPromotedToType(ISD::LOAD, MVT::i64, MVT::v2i32);
81
82 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
83 AddPromotedToType(ISD::LOAD, MVT::v2i64, MVT::v4i32);
84
Tom Stellard7512c082013-07-12 18:14:56 +000085 setOperationAction(ISD::LOAD, MVT::f64, Promote);
Matt Arsenault71e66762016-05-21 02:27:49 +000086 AddPromotedToType(ISD::LOAD, MVT::f64, MVT::v2i32);
Tom Stellard7512c082013-07-12 18:14:56 +000087
Matt Arsenaulte8a076a2014-05-08 18:01:56 +000088 setOperationAction(ISD::LOAD, MVT::v2f64, Promote);
Matt Arsenault71e66762016-05-21 02:27:49 +000089 AddPromotedToType(ISD::LOAD, MVT::v2f64, MVT::v4i32);
Tom Stellard0344cdf2013-08-01 15:23:42 +000090
Matt Arsenaultbd223422015-01-14 01:35:17 +000091 // There are no 64-bit extloads. These should be done as a 32-bit extload and
92 // an extension to 64-bit.
93 for (MVT VT : MVT::integer_valuetypes()) {
94 setLoadExtAction(ISD::EXTLOAD, MVT::i64, VT, Expand);
95 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, VT, Expand);
96 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, VT, Expand);
97 }
98
Matt Arsenault71e66762016-05-21 02:27:49 +000099 for (MVT VT : MVT::integer_valuetypes()) {
100 if (VT == MVT::i64)
101 continue;
102
103 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
104 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal);
105 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal);
106 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
107
108 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
109 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal);
110 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal);
111 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
112
113 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
114 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal);
115 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal);
116 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
117 }
118
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000119 for (MVT VT : MVT::integer_vector_valuetypes()) {
120 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i8, Expand);
121 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i8, Expand);
122 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i8, Expand);
123 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i8, Expand);
124 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i8, Expand);
125 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v4i8, Expand);
126 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i16, Expand);
127 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i16, Expand);
128 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i16, Expand);
129 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i16, Expand);
130 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i16, Expand);
131 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v4i16, Expand);
132 }
Tom Stellardb03edec2013-08-16 01:12:16 +0000133
Matt Arsenault71e66762016-05-21 02:27:49 +0000134 setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand);
135 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, MVT::v2f16, Expand);
136 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, MVT::v4f16, Expand);
137 setLoadExtAction(ISD::EXTLOAD, MVT::v8f32, MVT::v8f16, Expand);
138
139 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
140 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand);
141 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f32, Expand);
142 setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8f32, Expand);
143
144 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Expand);
145 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand);
146 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f16, Expand);
147 setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8f16, Expand);
148
149 setOperationAction(ISD::STORE, MVT::f32, Promote);
150 AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32);
151
152 setOperationAction(ISD::STORE, MVT::v2f32, Promote);
153 AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32);
154
155 setOperationAction(ISD::STORE, MVT::v4f32, Promote);
156 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
157
158 setOperationAction(ISD::STORE, MVT::v8f32, Promote);
159 AddPromotedToType(ISD::STORE, MVT::v8f32, MVT::v8i32);
160
161 setOperationAction(ISD::STORE, MVT::v16f32, Promote);
162 AddPromotedToType(ISD::STORE, MVT::v16f32, MVT::v16i32);
163
164 setOperationAction(ISD::STORE, MVT::i64, Promote);
165 AddPromotedToType(ISD::STORE, MVT::i64, MVT::v2i32);
166
167 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
168 AddPromotedToType(ISD::STORE, MVT::v2i64, MVT::v4i32);
169
170 setOperationAction(ISD::STORE, MVT::f64, Promote);
171 AddPromotedToType(ISD::STORE, MVT::f64, MVT::v2i32);
172
173 setOperationAction(ISD::STORE, MVT::v2f64, Promote);
174 AddPromotedToType(ISD::STORE, MVT::v2f64, MVT::v4i32);
175
Matt Arsenault71e66762016-05-21 02:27:49 +0000176 setTruncStoreAction(MVT::i64, MVT::i1, Expand);
177 setTruncStoreAction(MVT::i64, MVT::i8, Expand);
178 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
179 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
180
181 setTruncStoreAction(MVT::v2i64, MVT::v2i1, Expand);
182 setTruncStoreAction(MVT::v2i64, MVT::v2i8, Expand);
183 setTruncStoreAction(MVT::v2i64, MVT::v2i16, Expand);
184 setTruncStoreAction(MVT::v2i64, MVT::v2i32, Expand);
185
186 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
187 setTruncStoreAction(MVT::v2f32, MVT::v2f16, Expand);
188 setTruncStoreAction(MVT::v4f32, MVT::v4f16, Expand);
189 setTruncStoreAction(MVT::v8f32, MVT::v8f16, Expand);
190
191 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
192 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
193
194 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
195 setTruncStoreAction(MVT::v2f64, MVT::v2f16, Expand);
196
197 setTruncStoreAction(MVT::v4f64, MVT::v4f32, Expand);
198 setTruncStoreAction(MVT::v4f64, MVT::v4f16, Expand);
199
200 setTruncStoreAction(MVT::v8f64, MVT::v8f32, Expand);
201 setTruncStoreAction(MVT::v8f64, MVT::v8f16, Expand);
202
203
204 setOperationAction(ISD::Constant, MVT::i32, Legal);
205 setOperationAction(ISD::Constant, MVT::i64, Legal);
206 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
207 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
208
209 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
210 setOperationAction(ISD::BRIND, MVT::Other, Expand);
211
212 // This is totally unsupported, just custom lower to produce an error.
213 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
214
215 // We need to custom lower some of the intrinsics
216 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
217 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
218
219 // Library functions. These default to Expand, but we have instructions
220 // for them.
221 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
222 setOperationAction(ISD::FEXP2, MVT::f32, Legal);
223 setOperationAction(ISD::FPOW, MVT::f32, Legal);
224 setOperationAction(ISD::FLOG2, MVT::f32, Legal);
225 setOperationAction(ISD::FABS, MVT::f32, Legal);
226 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
227 setOperationAction(ISD::FRINT, MVT::f32, Legal);
228 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
229 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
230 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
231
232 setOperationAction(ISD::FROUND, MVT::f32, Custom);
233 setOperationAction(ISD::FROUND, MVT::f64, Custom);
234
235 setOperationAction(ISD::FNEARBYINT, MVT::f32, Custom);
236 setOperationAction(ISD::FNEARBYINT, MVT::f64, Custom);
237
238 setOperationAction(ISD::FREM, MVT::f32, Custom);
239 setOperationAction(ISD::FREM, MVT::f64, Custom);
240
241 // v_mad_f32 does not support denormals according to some sources.
242 if (!Subtarget->hasFP32Denormals())
243 setOperationAction(ISD::FMAD, MVT::f32, Legal);
244
245 // Expand to fneg + fadd.
246 setOperationAction(ISD::FSUB, MVT::f64, Expand);
247
248 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
249 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Custom);
250 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
251 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
252 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Custom);
253 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Custom);
254 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4f32, Custom);
255 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom);
256 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8f32, Custom);
257 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8i32, Custom);
Tom Stellardaeb45642014-02-04 17:18:43 +0000258
Matt Arsenaulte8208ec2014-06-18 17:05:26 +0000259 if (Subtarget->getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) {
Matt Arsenault46010932014-06-18 17:05:30 +0000260 setOperationAction(ISD::FCEIL, MVT::f64, Custom);
261 setOperationAction(ISD::FTRUNC, MVT::f64, Custom);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +0000262 setOperationAction(ISD::FRINT, MVT::f64, Custom);
Matt Arsenault46010932014-06-18 17:05:30 +0000263 setOperationAction(ISD::FFLOOR, MVT::f64, Custom);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +0000264 }
265
Matt Arsenault6e439652014-06-10 19:00:20 +0000266 if (!Subtarget->hasBFI()) {
267 // fcopysign can be done in a single instruction with BFI.
268 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
269 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
270 }
271
Tim Northoverf861de32014-07-18 08:43:24 +0000272 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
Tom Stellard94c21bc2016-11-01 16:31:48 +0000273 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Custom);
Tim Northoverf861de32014-07-18 08:43:24 +0000274
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000275 const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 };
276 for (MVT VT : ScalarIntVTs) {
Matt Arsenault71e66762016-05-21 02:27:49 +0000277 // These should use [SU]DIVREM, so set them to expand
Jan Vesely4a33bc62014-08-12 17:31:17 +0000278 setOperationAction(ISD::SDIV, VT, Expand);
Matt Arsenault71e66762016-05-21 02:27:49 +0000279 setOperationAction(ISD::UDIV, VT, Expand);
280 setOperationAction(ISD::SREM, VT, Expand);
281 setOperationAction(ISD::UREM, VT, Expand);
Matt Arsenault717c1d02014-06-15 21:08:58 +0000282
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000283 // GPU does not have divrem function for signed or unsigned.
Jan Vesely109efdf2014-06-22 21:43:00 +0000284 setOperationAction(ISD::SDIVREM, VT, Custom);
Matt Arsenault717c1d02014-06-15 21:08:58 +0000285 setOperationAction(ISD::UDIVREM, VT, Custom);
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000286
287 // GPU does not have [S|U]MUL_LOHI functions as a single instruction.
288 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
289 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
290
291 setOperationAction(ISD::BSWAP, VT, Expand);
292 setOperationAction(ISD::CTTZ, VT, Expand);
293 setOperationAction(ISD::CTLZ, VT, Expand);
294 }
295
Matt Arsenault60425062014-06-10 19:18:28 +0000296 if (!Subtarget->hasBCNT(32))
297 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
298
299 if (!Subtarget->hasBCNT(64))
300 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
301
Matt Arsenault717c1d02014-06-15 21:08:58 +0000302 // The hardware supports 32-bit ROTR, but not ROTL.
303 setOperationAction(ISD::ROTL, MVT::i32, Expand);
304 setOperationAction(ISD::ROTL, MVT::i64, Expand);
305 setOperationAction(ISD::ROTR, MVT::i64, Expand);
306
307 setOperationAction(ISD::MUL, MVT::i64, Expand);
308 setOperationAction(ISD::MULHU, MVT::i64, Expand);
309 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Matt Arsenault717c1d02014-06-15 21:08:58 +0000310 setOperationAction(ISD::UDIV, MVT::i32, Expand);
311 setOperationAction(ISD::UREM, MVT::i32, Expand);
312 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
Matt Arsenaultf7c95e32014-10-03 23:54:41 +0000313 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Matt Arsenaultc9961752014-10-03 23:54:56 +0000314 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
315 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
Matt Arsenault717c1d02014-06-15 21:08:58 +0000316 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Matt Arsenaultb5b51102014-06-10 19:18:21 +0000317
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000318 setOperationAction(ISD::SMIN, MVT::i32, Legal);
319 setOperationAction(ISD::UMIN, MVT::i32, Legal);
320 setOperationAction(ISD::SMAX, MVT::i32, Legal);
321 setOperationAction(ISD::UMAX, MVT::i32, Legal);
322
Matt Arsenaultde5fbe92016-01-11 17:02:00 +0000323 if (Subtarget->hasFFBH())
324 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Custom);
Jan Vesely6ddb8dd2014-07-15 15:51:09 +0000325
Craig Topper33772c52016-04-28 03:34:31 +0000326 if (Subtarget->hasFFBL())
327 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Legal);
Matt Arsenault5ca3c722016-01-11 16:37:46 +0000328
Matt Arsenaultf058d672016-01-11 16:50:29 +0000329 setOperationAction(ISD::CTLZ, MVT::i64, Custom);
330 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
331
Matt Arsenault59b8b772016-03-01 04:58:17 +0000332 // We only really have 32-bit BFE instructions (and 16-bit on VI).
333 //
334 // On SI+ there are 64-bit BFEs, but they are scalar only and there isn't any
335 // effort to match them now. We want this to be false for i64 cases when the
336 // extraction isn't restricted to the upper or lower half. Ideally we would
337 // have some pass reduce 64-bit extracts to 32-bit if possible. Extracts that
338 // span the midpoint are probably relatively rare, so don't worry about them
339 // for now.
340 if (Subtarget->hasBFE())
341 setHasExtractBitsInsn(true);
342
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000343 static const MVT::SimpleValueType VectorIntTypes[] = {
Tom Stellardf6d80232013-08-21 22:14:17 +0000344 MVT::v2i32, MVT::v4i32
Aaron Watry0a794a462013-06-25 13:55:57 +0000345 };
Aaron Watry0a794a462013-06-25 13:55:57 +0000346
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000347 for (MVT VT : VectorIntTypes) {
Matt Arsenaultb5b51102014-06-10 19:18:21 +0000348 // Expand the following operations for the current type by default.
Aaron Watry0a794a462013-06-25 13:55:57 +0000349 setOperationAction(ISD::ADD, VT, Expand);
350 setOperationAction(ISD::AND, VT, Expand);
Tom Stellardaa313d02013-07-30 14:31:03 +0000351 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
352 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Aaron Watry0a794a462013-06-25 13:55:57 +0000353 setOperationAction(ISD::MUL, VT, Expand);
Valery Pykhtin8a89d362016-11-01 10:26:48 +0000354 setOperationAction(ISD::MULHU, VT, Expand);
355 setOperationAction(ISD::MULHS, VT, Expand);
Aaron Watry0a794a462013-06-25 13:55:57 +0000356 setOperationAction(ISD::OR, VT, Expand);
357 setOperationAction(ISD::SHL, VT, Expand);
Aaron Watry0a794a462013-06-25 13:55:57 +0000358 setOperationAction(ISD::SRA, VT, Expand);
Matt Arsenault825fb0b2014-06-13 04:00:30 +0000359 setOperationAction(ISD::SRL, VT, Expand);
360 setOperationAction(ISD::ROTL, VT, Expand);
361 setOperationAction(ISD::ROTR, VT, Expand);
Aaron Watry0a794a462013-06-25 13:55:57 +0000362 setOperationAction(ISD::SUB, VT, Expand);
Matt Arsenault825fb0b2014-06-13 04:00:30 +0000363 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
Tom Stellardaa313d02013-07-30 14:31:03 +0000364 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
Jan Vesely109efdf2014-06-22 21:43:00 +0000365 setOperationAction(ISD::SDIV, VT, Expand);
Matt Arsenault717c1d02014-06-15 21:08:58 +0000366 setOperationAction(ISD::UDIV, VT, Expand);
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000367 setOperationAction(ISD::SREM, VT, Expand);
Aaron Watry0a794a462013-06-25 13:55:57 +0000368 setOperationAction(ISD::UREM, VT, Expand);
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000369 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
370 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Jan Vesely109efdf2014-06-22 21:43:00 +0000371 setOperationAction(ISD::SDIVREM, VT, Custom);
Artyom Skrobov63471332015-10-15 09:18:47 +0000372 setOperationAction(ISD::UDIVREM, VT, Expand);
Matt Arsenaultc4d3d3a2014-06-23 18:00:49 +0000373 setOperationAction(ISD::ADDC, VT, Expand);
374 setOperationAction(ISD::SUBC, VT, Expand);
375 setOperationAction(ISD::ADDE, VT, Expand);
376 setOperationAction(ISD::SUBE, VT, Expand);
Matt Arsenault9fe669c2014-03-06 17:34:03 +0000377 setOperationAction(ISD::SELECT, VT, Expand);
Tom Stellard67ae4762013-07-18 21:43:35 +0000378 setOperationAction(ISD::VSELECT, VT, Expand);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000379 setOperationAction(ISD::SELECT_CC, VT, Expand);
Aaron Watry0a794a462013-06-25 13:55:57 +0000380 setOperationAction(ISD::XOR, VT, Expand);
Matt Arsenault13ccc8f2014-06-09 16:20:25 +0000381 setOperationAction(ISD::BSWAP, VT, Expand);
Matt Arsenaultb5b51102014-06-10 19:18:21 +0000382 setOperationAction(ISD::CTPOP, VT, Expand);
383 setOperationAction(ISD::CTTZ, VT, Expand);
384 setOperationAction(ISD::CTLZ, VT, Expand);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000385 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
Aaron Watry0a794a462013-06-25 13:55:57 +0000386 }
Tom Stellarda92ff872013-08-16 23:51:24 +0000387
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000388 static const MVT::SimpleValueType FloatVectorTypes[] = {
Tom Stellardf6d80232013-08-21 22:14:17 +0000389 MVT::v2f32, MVT::v4f32
Tom Stellarda92ff872013-08-16 23:51:24 +0000390 };
Tom Stellarda92ff872013-08-16 23:51:24 +0000391
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000392 for (MVT VT : FloatVectorTypes) {
Tom Stellard175e7a82013-11-27 21:23:39 +0000393 setOperationAction(ISD::FABS, VT, Expand);
Matt Arsenault7c936902014-10-21 23:01:01 +0000394 setOperationAction(ISD::FMINNUM, VT, Expand);
395 setOperationAction(ISD::FMAXNUM, VT, Expand);
Tom Stellarda92ff872013-08-16 23:51:24 +0000396 setOperationAction(ISD::FADD, VT, Expand);
Jan Vesely85f0dbc2014-06-18 17:57:29 +0000397 setOperationAction(ISD::FCEIL, VT, Expand);
Tom Stellard3dbf1f82014-05-02 15:41:47 +0000398 setOperationAction(ISD::FCOS, VT, Expand);
Tom Stellarda92ff872013-08-16 23:51:24 +0000399 setOperationAction(ISD::FDIV, VT, Expand);
Tom Stellard5222a882014-06-20 17:06:05 +0000400 setOperationAction(ISD::FEXP2, VT, Expand);
Tom Stellarda79e9f02014-06-20 17:06:07 +0000401 setOperationAction(ISD::FLOG2, VT, Expand);
Matt Arsenault16e31332014-09-10 21:44:27 +0000402 setOperationAction(ISD::FREM, VT, Expand);
Tom Stellardbfebd1f2014-02-04 17:18:37 +0000403 setOperationAction(ISD::FPOW, VT, Expand);
Tom Stellardad3aff22013-08-16 23:51:29 +0000404 setOperationAction(ISD::FFLOOR, VT, Expand);
Tom Stellardeddfa692013-12-20 05:11:55 +0000405 setOperationAction(ISD::FTRUNC, VT, Expand);
Tom Stellarda92ff872013-08-16 23:51:24 +0000406 setOperationAction(ISD::FMUL, VT, Expand);
Matt Arsenaultc6f8fdb2014-06-26 01:28:05 +0000407 setOperationAction(ISD::FMA, VT, Expand);
Tom Stellardb249b752013-08-16 23:51:33 +0000408 setOperationAction(ISD::FRINT, VT, Expand);
Matt Arsenault692bd5e2014-06-18 22:03:45 +0000409 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Tom Stellarde118b8b2013-10-29 16:37:20 +0000410 setOperationAction(ISD::FSQRT, VT, Expand);
Tom Stellard3dbf1f82014-05-02 15:41:47 +0000411 setOperationAction(ISD::FSIN, VT, Expand);
Tom Stellarda92ff872013-08-16 23:51:24 +0000412 setOperationAction(ISD::FSUB, VT, Expand);
Matt Arsenault616a8e42014-06-01 07:38:21 +0000413 setOperationAction(ISD::FNEG, VT, Expand);
Matt Arsenault616a8e42014-06-01 07:38:21 +0000414 setOperationAction(ISD::VSELECT, VT, Expand);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000415 setOperationAction(ISD::SELECT_CC, VT, Expand);
Matt Arsenault6e439652014-06-10 19:00:20 +0000416 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000417 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
Tom Stellarda92ff872013-08-16 23:51:24 +0000418 }
Matt Arsenaultfae02982014-03-17 18:58:11 +0000419
Matt Arsenault1cc49912016-05-25 17:34:58 +0000420 // This causes using an unrolled select operation rather than expansion with
421 // bit operations. This is in general better, but the alternative using BFI
422 // instructions may be better if the select sources are SGPRs.
423 setOperationAction(ISD::SELECT, MVT::v2f32, Promote);
424 AddPromotedToType(ISD::SELECT, MVT::v2f32, MVT::v2i32);
425
426 setOperationAction(ISD::SELECT, MVT::v4f32, Promote);
427 AddPromotedToType(ISD::SELECT, MVT::v4f32, MVT::v4i32);
428
Matt Arsenault38d8ed22016-12-09 17:49:14 +0000429 // There are no libcalls of any kind.
430 for (int I = 0; I < RTLIB::UNKNOWN_LIBCALL; ++I)
431 setLibcallName(static_cast<RTLIB::Libcall>(I), nullptr);
432
Matt Arsenaultfcdddf92014-11-26 21:23:15 +0000433 setBooleanContents(ZeroOrNegativeOneBooleanContent);
434 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
435
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000436 setSchedulingPreference(Sched::RegPressure);
437 setJumpIsExpensive(true);
Matt Arsenault88716832017-01-10 19:08:15 +0000438
439 // FIXME: This is only partially true. If we have to do vector compares, any
440 // SGPR pair can be a condition register. If we have a uniform condition, we
441 // are better off doing SALU operations, where there is only one SCC. For now,
442 // we don't have a way of knowing during instruction selection if a condition
443 // will be uniform and we always use vector compares. Assume we are using
444 // vector compares until that is fixed.
Stanislav Mekhanoshin0ee250e2016-11-28 18:58:49 +0000445 setHasMultipleConditionRegisters(true);
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000446
Matt Arsenault996a0ef2014-08-09 03:46:58 +0000447 // SI at least has hardware support for floating point exceptions, but no way
448 // of using or handling them is implemented. They are also optional in OpenCL
449 // (Section 7.3)
Matt Arsenaultf639c322016-01-28 20:53:42 +0000450 setHasFloatingPointExceptions(Subtarget->hasFPExceptions());
Matt Arsenault996a0ef2014-08-09 03:46:58 +0000451
Matt Arsenaultd5f91fd2014-06-23 18:00:52 +0000452 PredictableSelectIsExpensive = false;
453
Nirav Daved32a4212017-01-26 16:46:13 +0000454 // We want to find all load dependencies for long chains of stores to enable
455 // merging into very wide vectors. The problem is with vectors with > 4
456 // elements. MergeConsecutiveStores will attempt to merge these because x8/x16
457 // vectors are a legal type, even though we have to split the loads
458 // usually. When we can more precisely specify load legality per address
459 // space, we should be able to make FindBetterChain/MergeConsecutiveStores
460 // smarter so that they can figure out what to do in 2 iterations without all
461 // N > 4 stores on the same chain.
462 GatherAllAliasesMaxDepth = 16;
463
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000464 // FIXME: Need to really handle these.
465 MaxStoresPerMemcpy = 4096;
466 MaxStoresPerMemmove = 4096;
467 MaxStoresPerMemset = 4096;
Matt Arsenault71e66762016-05-21 02:27:49 +0000468
469 setTargetDAGCombine(ISD::BITCAST);
Matt Arsenault71e66762016-05-21 02:27:49 +0000470 setTargetDAGCombine(ISD::SHL);
471 setTargetDAGCombine(ISD::SRA);
472 setTargetDAGCombine(ISD::SRL);
473 setTargetDAGCombine(ISD::MUL);
Matt Arsenault2712d4a2016-08-27 01:32:27 +0000474 setTargetDAGCombine(ISD::MULHU);
475 setTargetDAGCombine(ISD::MULHS);
Matt Arsenault71e66762016-05-21 02:27:49 +0000476 setTargetDAGCombine(ISD::SELECT);
477 setTargetDAGCombine(ISD::SELECT_CC);
478 setTargetDAGCombine(ISD::STORE);
479 setTargetDAGCombine(ISD::FADD);
480 setTargetDAGCombine(ISD::FSUB);
Matt Arsenault2529fba2017-01-12 00:09:34 +0000481 setTargetDAGCombine(ISD::FNEG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000482}
483
Tom Stellard28d06de2013-08-05 22:22:07 +0000484//===----------------------------------------------------------------------===//
485// Target Information
486//===----------------------------------------------------------------------===//
487
Matt Arsenault45337df2017-01-12 18:58:15 +0000488static bool fnegFoldsIntoOp(unsigned Opc) {
489 switch (Opc) {
490 case ISD::FADD:
491 case ISD::FSUB:
492 case ISD::FMUL:
493 case ISD::FMA:
494 case ISD::FMAD:
495 case ISD::FSIN:
Matt Arsenault53f0cc22017-01-26 01:25:36 +0000496 case ISD::FTRUNC:
497 case ISD::FRINT:
498 case ISD::FNEARBYINT:
Matt Arsenault45337df2017-01-12 18:58:15 +0000499 case AMDGPUISD::RCP:
500 case AMDGPUISD::RCP_LEGACY:
501 case AMDGPUISD::SIN_HW:
502 case AMDGPUISD::FMUL_LEGACY:
503 return true;
504 default:
505 return false;
506 }
507}
508
Mehdi Amini44ede332015-07-09 02:09:04 +0000509MVT AMDGPUTargetLowering::getVectorIdxTy(const DataLayout &) const {
Tom Stellard28d06de2013-08-05 22:22:07 +0000510 return MVT::i32;
511}
512
Matt Arsenaultd5f91fd2014-06-23 18:00:52 +0000513bool AMDGPUTargetLowering::isSelectSupported(SelectSupportKind SelType) const {
514 return true;
515}
516
Matt Arsenault14d46452014-06-15 20:23:38 +0000517// The backend supports 32 and 64 bit floating point immediates.
518// FIXME: Why are we reporting vectors of FP immediates as legal?
519bool AMDGPUTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
520 EVT ScalarVT = VT.getScalarType();
Matt Arsenault4e55c1e2016-12-22 03:05:30 +0000521 return (ScalarVT == MVT::f32 || ScalarVT == MVT::f64 ||
522 (ScalarVT == MVT::f16 && Subtarget->has16BitInsts()));
Matt Arsenault14d46452014-06-15 20:23:38 +0000523}
524
525// We don't want to shrink f64 / f32 constants.
526bool AMDGPUTargetLowering::ShouldShrinkFPConstant(EVT VT) const {
527 EVT ScalarVT = VT.getScalarType();
528 return (ScalarVT != MVT::f32 && ScalarVT != MVT::f64);
529}
530
Matt Arsenault810cb622014-12-12 00:00:24 +0000531bool AMDGPUTargetLowering::shouldReduceLoadWidth(SDNode *N,
532 ISD::LoadExtType,
533 EVT NewVT) const {
534
535 unsigned NewSize = NewVT.getStoreSizeInBits();
536
537 // If we are reducing to a 32-bit load, this is always better.
538 if (NewSize == 32)
539 return true;
540
541 EVT OldVT = N->getValueType(0);
542 unsigned OldSize = OldVT.getStoreSizeInBits();
543
544 // Don't produce extloads from sub 32-bit types. SI doesn't have scalar
545 // extloads, so doing one requires using a buffer_load. In cases where we
546 // still couldn't use a scalar load, using the wider load shouldn't really
547 // hurt anything.
548
549 // If the old size already had to be an extload, there's no harm in continuing
550 // to reduce the width.
551 return (OldSize < 32);
552}
553
Matt Arsenaultc5559bb2013-11-15 04:42:23 +0000554bool AMDGPUTargetLowering::isLoadBitCastBeneficial(EVT LoadTy,
555 EVT CastTy) const {
Matt Arsenaultc5559bb2013-11-15 04:42:23 +0000556
Matt Arsenault327bb5a2016-07-01 22:47:50 +0000557 assert(LoadTy.getSizeInBits() == CastTy.getSizeInBits());
Matt Arsenaultc5559bb2013-11-15 04:42:23 +0000558
Matt Arsenault327bb5a2016-07-01 22:47:50 +0000559 if (LoadTy.getScalarType() == MVT::i32)
560 return false;
561
562 unsigned LScalarSize = LoadTy.getScalarSizeInBits();
563 unsigned CastScalarSize = CastTy.getScalarSizeInBits();
564
565 return (LScalarSize < CastScalarSize) ||
566 (CastScalarSize >= 32);
Matt Arsenaultc5559bb2013-11-15 04:42:23 +0000567}
Tom Stellard28d06de2013-08-05 22:22:07 +0000568
Matt Arsenaultb56d8432015-01-13 19:46:48 +0000569// SI+ has instructions for cttz / ctlz for 32-bit values. This is probably also
570// profitable with the expansion for 64-bit since it's generally good to
571// speculate things.
572// FIXME: These should really have the size as a parameter.
573bool AMDGPUTargetLowering::isCheapToSpeculateCttz() const {
574 return true;
575}
576
577bool AMDGPUTargetLowering::isCheapToSpeculateCtlz() const {
578 return true;
579}
580
Tom Stellard75aadc22012-12-11 21:25:42 +0000581//===---------------------------------------------------------------------===//
Tom Stellardc54731a2013-07-23 23:55:03 +0000582// Target Properties
583//===---------------------------------------------------------------------===//
584
585bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const {
586 assert(VT.isFloatingPoint());
Matt Arsenaultc79dc702016-11-15 02:25:28 +0000587 return VT == MVT::f32 || VT == MVT::f64 || (Subtarget->has16BitInsts() &&
588 VT == MVT::f16);
Tom Stellardc54731a2013-07-23 23:55:03 +0000589}
590
591bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
Matt Arsenaultc79dc702016-11-15 02:25:28 +0000592 return isFAbsFree(VT);
Tom Stellardc54731a2013-07-23 23:55:03 +0000593}
594
Matt Arsenault65ad1602015-05-24 00:51:27 +0000595bool AMDGPUTargetLowering:: storeOfVectorConstantIsCheap(EVT MemVT,
596 unsigned NumElem,
597 unsigned AS) const {
598 return true;
599}
600
Matt Arsenault61dc2352015-10-12 23:59:50 +0000601bool AMDGPUTargetLowering::aggressivelyPreferBuildVectorSources(EVT VecVT) const {
602 // There are few operations which truly have vector input operands. Any vector
603 // operation is going to involve operations on each component, and a
604 // build_vector will be a copy per element, so it always makes sense to use a
605 // build_vector input in place of the extracted element to avoid a copy into a
606 // super register.
607 //
608 // We should probably only do this if all users are extracts only, but this
609 // should be the common case.
610 return true;
611}
612
Benjamin Kramer53f9df42014-02-12 10:17:54 +0000613bool AMDGPUTargetLowering::isTruncateFree(EVT Source, EVT Dest) const {
Matt Arsenault0cdcd962014-02-10 19:57:42 +0000614 // Truncate is just accessing a subregister.
Tom Stellard115a6152016-11-10 16:02:37 +0000615
616 unsigned SrcSize = Source.getSizeInBits();
617 unsigned DestSize = Dest.getSizeInBits();
618
619 return DestSize < SrcSize && DestSize % 32 == 0 ;
Benjamin Kramer53f9df42014-02-12 10:17:54 +0000620}
621
622bool AMDGPUTargetLowering::isTruncateFree(Type *Source, Type *Dest) const {
623 // Truncate is just accessing a subregister.
Tom Stellard115a6152016-11-10 16:02:37 +0000624
625 unsigned SrcSize = Source->getScalarSizeInBits();
626 unsigned DestSize = Dest->getScalarSizeInBits();
627
628 if (DestSize== 16 && Subtarget->has16BitInsts())
629 return SrcSize >= 32;
630
631 return DestSize < SrcSize && DestSize % 32 == 0;
Matt Arsenault0cdcd962014-02-10 19:57:42 +0000632}
633
Matt Arsenaultb517c812014-03-27 17:23:31 +0000634bool AMDGPUTargetLowering::isZExtFree(Type *Src, Type *Dest) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000635 unsigned SrcSize = Src->getScalarSizeInBits();
636 unsigned DestSize = Dest->getScalarSizeInBits();
Matt Arsenaultb517c812014-03-27 17:23:31 +0000637
Tom Stellard115a6152016-11-10 16:02:37 +0000638 if (SrcSize == 16 && Subtarget->has16BitInsts())
639 return DestSize >= 32;
640
Matt Arsenaultb517c812014-03-27 17:23:31 +0000641 return SrcSize == 32 && DestSize == 64;
642}
643
644bool AMDGPUTargetLowering::isZExtFree(EVT Src, EVT Dest) const {
645 // Any register load of a 64-bit value really requires 2 32-bit moves. For all
646 // practical purposes, the extra mov 0 to load a 64-bit is free. As used,
647 // this will enable reducing 64-bit operations the 32-bit, which is always
648 // good.
Tom Stellard115a6152016-11-10 16:02:37 +0000649
650 if (Src == MVT::i16)
651 return Dest == MVT::i32 ||Dest == MVT::i64 ;
652
Matt Arsenaultb517c812014-03-27 17:23:31 +0000653 return Src == MVT::i32 && Dest == MVT::i64;
654}
655
Aaron Ballman3c81e462014-06-26 13:45:47 +0000656bool AMDGPUTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
657 return isZExtFree(Val.getValueType(), VT2);
658}
659
Matt Arsenaulta7f1e0c2014-03-24 19:43:31 +0000660bool AMDGPUTargetLowering::isNarrowingProfitable(EVT SrcVT, EVT DestVT) const {
661 // There aren't really 64-bit registers, but pairs of 32-bit ones and only a
662 // limited number of native 64-bit operations. Shrinking an operation to fit
663 // in a single 32-bit register should always be helpful. As currently used,
664 // this is much less general than the name suggests, and is only used in
665 // places trying to reduce the sizes of loads. Shrinking loads to < 32-bits is
666 // not profitable, and may actually be harmful.
667 return SrcVT.getSizeInBits() > 32 && DestVT.getSizeInBits() == 32;
668}
669
Tom Stellardc54731a2013-07-23 23:55:03 +0000670//===---------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +0000671// TargetLowering Callbacks
672//===---------------------------------------------------------------------===//
673
Tom Stellardca166212017-01-30 21:56:46 +0000674CCAssignFn *AMDGPUCallLowering::CCAssignFnForCall(CallingConv::ID CC,
675 bool IsVarArg) const {
676 return CC_AMDGPU;
677}
678
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000679/// The SelectionDAGBuilder will automatically promote function arguments
680/// with illegal types. However, this does not work for the AMDGPU targets
681/// since the function arguments are stored in memory as these illegal types.
682/// In order to handle this properly we need to get the original types sizes
683/// from the LLVM IR Function and fixup the ISD:InputArg values before
684/// passing them to AnalyzeFormalArguments()
Christian Konig2c8f6d52013-03-07 09:03:52 +0000685
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000686/// When the SelectionDAGBuilder computes the Ins, it takes care of splitting
687/// input values across multiple registers. Each item in the Ins array
688/// represents a single value that will be stored in regsters. Ins[x].VT is
689/// the value type of the value that will be stored in the register, so
690/// whatever SDNode we lower the argument to needs to be this type.
691///
692/// In order to correctly lower the arguments we need to know the size of each
693/// argument. Since Ins[x].VT gives us the size of the register that will
694/// hold the value, we need to look at Ins[x].ArgVT to see the 'real' type
695/// for the orignal function argument so that we can deduce the correct memory
696/// type to use for Ins[x]. In most cases the correct memory type will be
697/// Ins[x].ArgVT. However, this will not always be the case. If, for example,
698/// we have a kernel argument of type v8i8, this argument will be split into
699/// 8 parts and each part will be represented by its own item in the Ins array.
700/// For each part the Ins[x].ArgVT will be the v8i8, which is the full type of
701/// the argument before it was split. From this, we deduce that the memory type
702/// for each individual part is i8. We pass the memory type as LocVT to the
703/// calling convention analysis function and the register type (Ins[x].VT) as
704/// the ValVT.
705void AMDGPUTargetLowering::analyzeFormalArgumentsCompute(CCState &State,
706 const SmallVectorImpl<ISD::InputArg> &Ins) const {
707 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
708 const ISD::InputArg &In = Ins[i];
709 EVT MemVT;
710
711 unsigned NumRegs = getNumRegisters(State.getContext(), In.ArgVT);
712
Tom Stellard7998db62016-09-16 22:20:24 +0000713 if (!Subtarget->isAmdHsaOS() &&
714 (In.ArgVT == MVT::i16 || In.ArgVT == MVT::i8 || In.ArgVT == MVT::f16)) {
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000715 // The ABI says the caller will extend these values to 32-bits.
716 MemVT = In.ArgVT.isInteger() ? MVT::i32 : MVT::f32;
717 } else if (NumRegs == 1) {
718 // This argument is not split, so the IR type is the memory type.
719 assert(!In.Flags.isSplit());
720 if (In.ArgVT.isExtended()) {
721 // We have an extended type, like i24, so we should just use the register type
722 MemVT = In.VT;
723 } else {
724 MemVT = In.ArgVT;
725 }
726 } else if (In.ArgVT.isVector() && In.VT.isVector() &&
727 In.ArgVT.getScalarType() == In.VT.getScalarType()) {
728 assert(In.ArgVT.getVectorNumElements() > In.VT.getVectorNumElements());
729 // We have a vector value which has been split into a vector with
730 // the same scalar type, but fewer elements. This should handle
731 // all the floating-point vector types.
732 MemVT = In.VT;
733 } else if (In.ArgVT.isVector() &&
734 In.ArgVT.getVectorNumElements() == NumRegs) {
735 // This arg has been split so that each element is stored in a separate
736 // register.
737 MemVT = In.ArgVT.getScalarType();
738 } else if (In.ArgVT.isExtended()) {
739 // We have an extended type, like i65.
740 MemVT = In.VT;
741 } else {
742 unsigned MemoryBits = In.ArgVT.getStoreSizeInBits() / NumRegs;
743 assert(In.ArgVT.getStoreSizeInBits() % NumRegs == 0);
744 if (In.VT.isInteger()) {
745 MemVT = EVT::getIntegerVT(State.getContext(), MemoryBits);
746 } else if (In.VT.isVector()) {
747 assert(!In.VT.getScalarType().isFloatingPoint());
748 unsigned NumElements = In.VT.getVectorNumElements();
749 assert(MemoryBits % NumElements == 0);
750 // This vector type has been split into another vector type with
751 // a different elements size.
752 EVT ScalarVT = EVT::getIntegerVT(State.getContext(),
753 MemoryBits / NumElements);
754 MemVT = EVT::getVectorVT(State.getContext(), ScalarVT, NumElements);
755 } else {
756 llvm_unreachable("cannot deduce memory type.");
757 }
758 }
759
760 // Convert one element vectors to scalar.
761 if (MemVT.isVector() && MemVT.getVectorNumElements() == 1)
762 MemVT = MemVT.getScalarType();
763
764 if (MemVT.isExtended()) {
765 // This should really only happen if we have vec3 arguments
766 assert(MemVT.isVector() && MemVT.getVectorNumElements() == 3);
767 MemVT = MemVT.getPow2VectorType(State.getContext());
768 }
769
770 assert(MemVT.isSimple());
771 allocateKernArg(i, In.VT, MemVT.getSimpleVT(), CCValAssign::Full, In.Flags,
772 State);
773 }
774}
775
776void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
777 const SmallVectorImpl<ISD::InputArg> &Ins) const {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000778 State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
Tom Stellard75aadc22012-12-11 21:25:42 +0000779}
780
Marek Olsak8a0f3352016-01-13 17:23:04 +0000781void AMDGPUTargetLowering::AnalyzeReturn(CCState &State,
782 const SmallVectorImpl<ISD::OutputArg> &Outs) const {
783
784 State.AnalyzeReturn(Outs, RetCC_SI);
785}
786
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000787SDValue
788AMDGPUTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
789 bool isVarArg,
790 const SmallVectorImpl<ISD::OutputArg> &Outs,
791 const SmallVectorImpl<SDValue> &OutVals,
792 const SDLoc &DL, SelectionDAG &DAG) const {
Matt Arsenault9babdf42016-06-22 20:15:28 +0000793 return DAG.getNode(AMDGPUISD::ENDPGM, DL, MVT::Other, Chain);
Tom Stellard75aadc22012-12-11 21:25:42 +0000794}
795
796//===---------------------------------------------------------------------===//
797// Target specific lowering
798//===---------------------------------------------------------------------===//
799
Matt Arsenault16353872014-04-22 16:42:00 +0000800SDValue AMDGPUTargetLowering::LowerCall(CallLoweringInfo &CLI,
801 SmallVectorImpl<SDValue> &InVals) const {
802 SDValue Callee = CLI.Callee;
803 SelectionDAG &DAG = CLI.DAG;
804
805 const Function &Fn = *DAG.getMachineFunction().getFunction();
806
807 StringRef FuncName("<unknown>");
808
Matt Arsenaultde1c34102014-04-25 22:22:01 +0000809 if (const ExternalSymbolSDNode *G = dyn_cast<ExternalSymbolSDNode>(Callee))
810 FuncName = G->getSymbol();
811 else if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Matt Arsenault16353872014-04-22 16:42:00 +0000812 FuncName = G->getGlobal()->getName();
813
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000814 DiagnosticInfoUnsupported NoCalls(
815 Fn, "unsupported call to function " + FuncName, CLI.DL.getDebugLoc());
Matt Arsenault16353872014-04-22 16:42:00 +0000816 DAG.getContext()->diagnose(NoCalls);
Matt Arsenault9430b912016-05-18 16:10:11 +0000817
Matt Arsenault0b386362016-12-15 20:50:12 +0000818 if (!CLI.IsTailCall) {
819 for (unsigned I = 0, E = CLI.Ins.size(); I != E; ++I)
820 InVals.push_back(DAG.getUNDEF(CLI.Ins[I].VT));
821 }
Matt Arsenault9430b912016-05-18 16:10:11 +0000822
823 return DAG.getEntryNode();
Matt Arsenault16353872014-04-22 16:42:00 +0000824}
825
Matt Arsenault19c54882015-08-26 18:37:13 +0000826SDValue AMDGPUTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
827 SelectionDAG &DAG) const {
828 const Function &Fn = *DAG.getMachineFunction().getFunction();
829
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000830 DiagnosticInfoUnsupported NoDynamicAlloca(Fn, "unsupported dynamic alloca",
831 SDLoc(Op).getDebugLoc());
Matt Arsenault19c54882015-08-26 18:37:13 +0000832 DAG.getContext()->diagnose(NoDynamicAlloca);
Diana Picuse440f992016-06-23 09:19:16 +0000833 auto Ops = {DAG.getConstant(0, SDLoc(), Op.getValueType()), Op.getOperand(0)};
834 return DAG.getMergeValues(Ops, SDLoc());
Matt Arsenault19c54882015-08-26 18:37:13 +0000835}
836
Matt Arsenault14d46452014-06-15 20:23:38 +0000837SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op,
838 SelectionDAG &DAG) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000839 switch (Op.getOpcode()) {
840 default:
Matthias Braun8c209aa2017-01-28 02:02:38 +0000841 Op->print(errs(), &DAG);
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +0000842 llvm_unreachable("Custom lowering code for this"
843 "instruction is not implemented yet!");
Tom Stellard75aadc22012-12-11 21:25:42 +0000844 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000845 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
Tom Stellardd86003e2013-08-14 23:25:00 +0000846 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
847 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000848 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
849 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
Jan Vesely109efdf2014-06-22 21:43:00 +0000850 case ISD::SDIVREM: return LowerSDIVREM(Op, DAG);
Matt Arsenault16e31332014-09-10 21:44:27 +0000851 case ISD::FREM: return LowerFREM(Op, DAG);
Matt Arsenault46010932014-06-18 17:05:30 +0000852 case ISD::FCEIL: return LowerFCEIL(Op, DAG);
853 case ISD::FTRUNC: return LowerFTRUNC(Op, DAG);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +0000854 case ISD::FRINT: return LowerFRINT(Op, DAG);
Matt Arsenault692bd5e2014-06-18 22:03:45 +0000855 case ISD::FNEARBYINT: return LowerFNEARBYINT(Op, DAG);
Matt Arsenaultb0055482015-01-21 18:18:25 +0000856 case ISD::FROUND: return LowerFROUND(Op, DAG);
Matt Arsenault46010932014-06-18 17:05:30 +0000857 case ISD::FFLOOR: return LowerFFLOOR(Op, DAG);
Matt Arsenaultf7c95e32014-10-03 23:54:41 +0000858 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Tom Stellardc947d8c2013-10-30 17:22:05 +0000859 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Tom Stellard94c21bc2016-11-01 16:31:48 +0000860 case ISD::FP_TO_FP16: return LowerFP_TO_FP16(Op, DAG);
Matt Arsenaultc9961752014-10-03 23:54:56 +0000861 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
862 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Matt Arsenaultf058d672016-01-11 16:50:29 +0000863 case ISD::CTLZ:
864 case ISD::CTLZ_ZERO_UNDEF:
865 return LowerCTLZ(Op, DAG);
Matt Arsenault19c54882015-08-26 18:37:13 +0000866 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000867 }
868 return Op;
869}
870
Matt Arsenaultd125d742014-03-27 17:23:24 +0000871void AMDGPUTargetLowering::ReplaceNodeResults(SDNode *N,
872 SmallVectorImpl<SDValue> &Results,
873 SelectionDAG &DAG) const {
874 switch (N->getOpcode()) {
875 case ISD::SIGN_EXTEND_INREG:
876 // Different parts of legalization seem to interpret which type of
877 // sign_extend_inreg is the one to check for custom lowering. The extended
878 // from type is what really matters, but some places check for custom
879 // lowering of the result type. This results in trying to use
880 // ReplaceNodeResults to sext_in_reg to an illegal type, so we'll just do
881 // nothing here and let the illegal result integer be handled normally.
882 return;
Matt Arsenaultd125d742014-03-27 17:23:24 +0000883 default:
884 return;
885 }
886}
887
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000888static bool hasDefinedInitializer(const GlobalValue *GV) {
889 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
890 if (!GVar || !GVar->hasInitializer())
891 return false;
892
Matt Arsenault8226fc42016-03-02 23:00:21 +0000893 return !isa<UndefValue>(GVar->getInitializer());
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000894}
895
Tom Stellardc026e8b2013-06-28 15:47:08 +0000896SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
897 SDValue Op,
898 SelectionDAG &DAG) const {
899
Mehdi Amini44ede332015-07-09 02:09:04 +0000900 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardc026e8b2013-06-28 15:47:08 +0000901 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
Tom Stellardc026e8b2013-06-28 15:47:08 +0000902 const GlobalValue *GV = G->getGlobal();
Tom Stellardc026e8b2013-06-28 15:47:08 +0000903
Tom Stellard04c0e982014-01-22 19:24:21 +0000904 switch (G->getAddressSpace()) {
Tom Stellard04c0e982014-01-22 19:24:21 +0000905 case AMDGPUAS::LOCAL_ADDRESS: {
906 // XXX: What does the value of G->getOffset() mean?
907 assert(G->getOffset() == 0 &&
908 "Do not know what to do with an non-zero offset");
Tom Stellardc026e8b2013-06-28 15:47:08 +0000909
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000910 // TODO: We could emit code to handle the initialization somewhere.
911 if (hasDefinedInitializer(GV))
912 break;
913
Matt Arsenault52ef4012016-07-26 16:45:58 +0000914 unsigned Offset = MFI->allocateLDSGlobal(DL, *GV);
915 return DAG.getConstant(Offset, SDLoc(Op), Op.getValueType());
Tom Stellard04c0e982014-01-22 19:24:21 +0000916 }
Tom Stellard04c0e982014-01-22 19:24:21 +0000917 }
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000918
919 const Function &Fn = *DAG.getMachineFunction().getFunction();
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000920 DiagnosticInfoUnsupported BadInit(
921 Fn, "unsupported initializer for address space", SDLoc(Op).getDebugLoc());
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000922 DAG.getContext()->diagnose(BadInit);
923 return SDValue();
Tom Stellardc026e8b2013-06-28 15:47:08 +0000924}
925
Tom Stellardd86003e2013-08-14 23:25:00 +0000926SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
927 SelectionDAG &DAG) const {
928 SmallVector<SDValue, 8> Args;
Tom Stellardd86003e2013-08-14 23:25:00 +0000929
Tom Stellardff5cf0e2015-04-23 22:59:24 +0000930 for (const SDUse &U : Op->ops())
931 DAG.ExtractVectorElements(U.get(), Args);
Tom Stellardd86003e2013-08-14 23:25:00 +0000932
Ahmed Bougacha128f8732016-04-26 21:15:30 +0000933 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args);
Tom Stellardd86003e2013-08-14 23:25:00 +0000934}
935
936SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
937 SelectionDAG &DAG) const {
938
939 SmallVector<SDValue, 8> Args;
Tom Stellardd86003e2013-08-14 23:25:00 +0000940 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Matt Arsenault9ec3cf22014-04-11 17:47:30 +0000941 EVT VT = Op.getValueType();
942 DAG.ExtractVectorElements(Op.getOperand(0), Args, Start,
943 VT.getVectorNumElements());
Tom Stellardd86003e2013-08-14 23:25:00 +0000944
Ahmed Bougacha128f8732016-04-26 21:15:30 +0000945 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args);
Tom Stellardd86003e2013-08-14 23:25:00 +0000946}
947
Tom Stellard75aadc22012-12-11 21:25:42 +0000948SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
949 SelectionDAG &DAG) const {
950 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000951 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000952 EVT VT = Op.getValueType();
953
954 switch (IntrinsicID) {
955 default: return Op;
Matt Arsenaultf0711022016-07-13 19:42:06 +0000956 case AMDGPUIntrinsic::AMDGPU_clamp: // Legacy name.
Matt Arsenault5d47d4a2014-06-12 21:15:44 +0000957 return DAG.getNode(AMDGPUISD::CLAMP, DL, VT,
958 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
959
Matt Arsenault4c537172014-03-31 18:21:18 +0000960 case AMDGPUIntrinsic::AMDGPU_bfe_i32:
961 return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT,
962 Op.getOperand(1),
963 Op.getOperand(2),
964 Op.getOperand(3));
965
966 case AMDGPUIntrinsic::AMDGPU_bfe_u32:
967 return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT,
968 Op.getOperand(1),
969 Op.getOperand(2),
970 Op.getOperand(3));
Tom Stellard75aadc22012-12-11 21:25:42 +0000971 }
972}
973
Tom Stellard75aadc22012-12-11 21:25:42 +0000974/// \brief Generate Min/Max node
Matt Arsenaultda7a6562017-02-01 00:42:40 +0000975SDValue AMDGPUTargetLowering::combineFMinMaxLegacy(const SDLoc &DL, EVT VT,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000976 SDValue LHS, SDValue RHS,
977 SDValue True, SDValue False,
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +0000978 SDValue CC,
979 DAGCombinerInfo &DCI) const {
Matt Arsenaultda59f3d2014-11-13 23:03:09 +0000980 if (!(LHS == True && RHS == False) && !(LHS == False && RHS == True))
981 return SDValue();
Tom Stellard75aadc22012-12-11 21:25:42 +0000982
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +0000983 SelectionDAG &DAG = DCI.DAG;
Tom Stellard75aadc22012-12-11 21:25:42 +0000984 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
985 switch (CCOpcode) {
986 case ISD::SETOEQ:
987 case ISD::SETONE:
988 case ISD::SETUNE:
989 case ISD::SETNE:
990 case ISD::SETUEQ:
991 case ISD::SETEQ:
992 case ISD::SETFALSE:
993 case ISD::SETFALSE2:
994 case ISD::SETTRUE:
995 case ISD::SETTRUE2:
996 case ISD::SETUO:
997 case ISD::SETO:
Matt Arsenaultda59f3d2014-11-13 23:03:09 +0000998 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000999 case ISD::SETULE:
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001000 case ISD::SETULT: {
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001001 if (LHS == True)
1002 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS);
1003 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS);
1004 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001005 case ISD::SETOLE:
1006 case ISD::SETOLT:
1007 case ISD::SETLE:
1008 case ISD::SETLT: {
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001009 // Ordered. Assume ordered for undefined.
1010
1011 // Only do this after legalization to avoid interfering with other combines
1012 // which might occur.
1013 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG &&
1014 !DCI.isCalledByLegalizer())
1015 return SDValue();
Marek Olsakbe047802014-12-07 12:19:03 +00001016
Matt Arsenault36094d72014-11-15 05:02:57 +00001017 // We need to permute the operands to get the correct NaN behavior. The
1018 // selected operand is the second one based on the failing compare with NaN,
1019 // so permute it based on the compare type the hardware uses.
1020 if (LHS == True)
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001021 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS);
1022 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS);
Tom Stellard75aadc22012-12-11 21:25:42 +00001023 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001024 case ISD::SETUGE:
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001025 case ISD::SETUGT: {
Matt Arsenault36094d72014-11-15 05:02:57 +00001026 if (LHS == True)
1027 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS);
1028 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS);
Tom Stellard75aadc22012-12-11 21:25:42 +00001029 }
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001030 case ISD::SETGT:
1031 case ISD::SETGE:
1032 case ISD::SETOGE:
1033 case ISD::SETOGT: {
1034 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG &&
1035 !DCI.isCalledByLegalizer())
1036 return SDValue();
1037
1038 if (LHS == True)
1039 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS);
1040 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS);
1041 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001042 case ISD::SETCC_INVALID:
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001043 llvm_unreachable("Invalid setcc condcode!");
Tom Stellard75aadc22012-12-11 21:25:42 +00001044 }
Tom Stellardafa8b532014-05-09 16:42:16 +00001045 return SDValue();
Tom Stellard75aadc22012-12-11 21:25:42 +00001046}
1047
Matt Arsenault6e3a4512016-01-18 22:01:13 +00001048std::pair<SDValue, SDValue>
1049AMDGPUTargetLowering::split64BitValue(SDValue Op, SelectionDAG &DAG) const {
1050 SDLoc SL(Op);
1051
1052 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1053
1054 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1055 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1056
1057 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1058 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1059
1060 return std::make_pair(Lo, Hi);
1061}
1062
Matt Arsenault33e3ece2016-01-18 22:09:04 +00001063SDValue AMDGPUTargetLowering::getLoHalf64(SDValue Op, SelectionDAG &DAG) const {
1064 SDLoc SL(Op);
1065
1066 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1067 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1068 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1069}
1070
1071SDValue AMDGPUTargetLowering::getHiHalf64(SDValue Op, SelectionDAG &DAG) const {
1072 SDLoc SL(Op);
1073
1074 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1075 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1076 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1077}
1078
Matt Arsenault83e60582014-07-24 17:10:35 +00001079SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue Op,
1080 SelectionDAG &DAG) const {
Matt Arsenault9c499c32016-04-14 23:31:26 +00001081 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault83e60582014-07-24 17:10:35 +00001082 EVT VT = Op.getValueType();
1083
Matt Arsenault9c499c32016-04-14 23:31:26 +00001084
Matt Arsenault83e60582014-07-24 17:10:35 +00001085 // If this is a 2 element vector, we really want to scalarize and not create
1086 // weird 1 element vectors.
1087 if (VT.getVectorNumElements() == 2)
Matt Arsenault9c499c32016-04-14 23:31:26 +00001088 return scalarizeVectorLoad(Load, DAG);
Matt Arsenault83e60582014-07-24 17:10:35 +00001089
Matt Arsenault83e60582014-07-24 17:10:35 +00001090 SDValue BasePtr = Load->getBasePtr();
1091 EVT PtrVT = BasePtr.getValueType();
1092 EVT MemVT = Load->getMemoryVT();
1093 SDLoc SL(Op);
Matt Arsenault52a52a52015-12-14 16:59:40 +00001094
1095 const MachinePointerInfo &SrcValue = Load->getMemOperand()->getPointerInfo();
Matt Arsenault83e60582014-07-24 17:10:35 +00001096
1097 EVT LoVT, HiVT;
1098 EVT LoMemVT, HiMemVT;
1099 SDValue Lo, Hi;
1100
1101 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
1102 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT);
1103 std::tie(Lo, Hi) = DAG.SplitVector(Op, SL, LoVT, HiVT);
Matt Arsenault52a52a52015-12-14 16:59:40 +00001104
1105 unsigned Size = LoMemVT.getStoreSize();
1106 unsigned BaseAlign = Load->getAlignment();
1107 unsigned HiAlign = MinAlign(BaseAlign, Size);
1108
Justin Lebar9c375812016-07-15 18:27:10 +00001109 SDValue LoLoad = DAG.getExtLoad(Load->getExtensionType(), SL, LoVT,
1110 Load->getChain(), BasePtr, SrcValue, LoMemVT,
1111 BaseAlign, Load->getMemOperand()->getFlags());
Matt Arsenault83e60582014-07-24 17:10:35 +00001112 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
Matt Arsenault52a52a52015-12-14 16:59:40 +00001113 DAG.getConstant(Size, SL, PtrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00001114 SDValue HiLoad =
1115 DAG.getExtLoad(Load->getExtensionType(), SL, HiVT, Load->getChain(),
1116 HiPtr, SrcValue.getWithOffset(LoMemVT.getStoreSize()),
1117 HiMemVT, HiAlign, Load->getMemOperand()->getFlags());
Matt Arsenault83e60582014-07-24 17:10:35 +00001118
1119 SDValue Ops[] = {
1120 DAG.getNode(ISD::CONCAT_VECTORS, SL, VT, LoLoad, HiLoad),
1121 DAG.getNode(ISD::TokenFactor, SL, MVT::Other,
1122 LoLoad.getValue(1), HiLoad.getValue(1))
1123 };
1124
1125 return DAG.getMergeValues(Ops, SL);
1126}
1127
Matt Arsenault83e60582014-07-24 17:10:35 +00001128SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op,
1129 SelectionDAG &DAG) const {
1130 StoreSDNode *Store = cast<StoreSDNode>(Op);
1131 SDValue Val = Store->getValue();
1132 EVT VT = Val.getValueType();
1133
1134 // If this is a 2 element vector, we really want to scalarize and not create
1135 // weird 1 element vectors.
1136 if (VT.getVectorNumElements() == 2)
Matt Arsenault9c499c32016-04-14 23:31:26 +00001137 return scalarizeVectorStore(Store, DAG);
Matt Arsenault83e60582014-07-24 17:10:35 +00001138
1139 EVT MemVT = Store->getMemoryVT();
1140 SDValue Chain = Store->getChain();
1141 SDValue BasePtr = Store->getBasePtr();
1142 SDLoc SL(Op);
1143
1144 EVT LoVT, HiVT;
1145 EVT LoMemVT, HiMemVT;
1146 SDValue Lo, Hi;
1147
1148 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
1149 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT);
1150 std::tie(Lo, Hi) = DAG.SplitVector(Val, SL, LoVT, HiVT);
1151
1152 EVT PtrVT = BasePtr.getValueType();
1153 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001154 DAG.getConstant(LoMemVT.getStoreSize(), SL,
1155 PtrVT));
Matt Arsenault83e60582014-07-24 17:10:35 +00001156
Matt Arsenault52a52a52015-12-14 16:59:40 +00001157 const MachinePointerInfo &SrcValue = Store->getMemOperand()->getPointerInfo();
1158 unsigned BaseAlign = Store->getAlignment();
1159 unsigned Size = LoMemVT.getStoreSize();
1160 unsigned HiAlign = MinAlign(BaseAlign, Size);
1161
Justin Lebar9c375812016-07-15 18:27:10 +00001162 SDValue LoStore =
1163 DAG.getTruncStore(Chain, SL, Lo, BasePtr, SrcValue, LoMemVT, BaseAlign,
1164 Store->getMemOperand()->getFlags());
1165 SDValue HiStore =
1166 DAG.getTruncStore(Chain, SL, Hi, HiPtr, SrcValue.getWithOffset(Size),
1167 HiMemVT, HiAlign, Store->getMemOperand()->getFlags());
Matt Arsenault83e60582014-07-24 17:10:35 +00001168
1169 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, LoStore, HiStore);
1170}
1171
Matt Arsenault0daeb632014-07-24 06:59:20 +00001172// This is a shortcut for integer division because we have fast i32<->f32
1173// conversions, and fast f32 reciprocal instructions. The fractional part of a
Matt Arsenault81a70952016-05-21 01:53:33 +00001174// float is enough to accurately represent up to a 24-bit signed integer.
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001175SDValue AMDGPUTargetLowering::LowerDIVREM24(SDValue Op, SelectionDAG &DAG,
1176 bool Sign) const {
Matt Arsenault1578aa72014-06-15 20:08:02 +00001177 SDLoc DL(Op);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001178 EVT VT = Op.getValueType();
Matt Arsenault1578aa72014-06-15 20:08:02 +00001179 SDValue LHS = Op.getOperand(0);
1180 SDValue RHS = Op.getOperand(1);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001181 MVT IntVT = MVT::i32;
1182 MVT FltVT = MVT::f32;
1183
Matt Arsenault81a70952016-05-21 01:53:33 +00001184 unsigned LHSSignBits = DAG.ComputeNumSignBits(LHS);
1185 if (LHSSignBits < 9)
1186 return SDValue();
1187
1188 unsigned RHSSignBits = DAG.ComputeNumSignBits(RHS);
1189 if (RHSSignBits < 9)
1190 return SDValue();
Jan Veselye5ca27d2014-08-12 17:31:20 +00001191
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001192 unsigned BitSize = VT.getSizeInBits();
Matt Arsenault81a70952016-05-21 01:53:33 +00001193 unsigned SignBits = std::min(LHSSignBits, RHSSignBits);
1194 unsigned DivBits = BitSize - SignBits;
1195 if (Sign)
1196 ++DivBits;
1197
1198 ISD::NodeType ToFp = Sign ? ISD::SINT_TO_FP : ISD::UINT_TO_FP;
1199 ISD::NodeType ToInt = Sign ? ISD::FP_TO_SINT : ISD::FP_TO_UINT;
Matt Arsenault0daeb632014-07-24 06:59:20 +00001200
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001201 SDValue jq = DAG.getConstant(1, DL, IntVT);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001202
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001203 if (Sign) {
Jan Veselye5ca27d2014-08-12 17:31:20 +00001204 // char|short jq = ia ^ ib;
1205 jq = DAG.getNode(ISD::XOR, DL, VT, LHS, RHS);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001206
Jan Veselye5ca27d2014-08-12 17:31:20 +00001207 // jq = jq >> (bitsize - 2)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001208 jq = DAG.getNode(ISD::SRA, DL, VT, jq,
1209 DAG.getConstant(BitSize - 2, DL, VT));
Matt Arsenault1578aa72014-06-15 20:08:02 +00001210
Jan Veselye5ca27d2014-08-12 17:31:20 +00001211 // jq = jq | 0x1
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001212 jq = DAG.getNode(ISD::OR, DL, VT, jq, DAG.getConstant(1, DL, VT));
Jan Veselye5ca27d2014-08-12 17:31:20 +00001213 }
Matt Arsenault1578aa72014-06-15 20:08:02 +00001214
1215 // int ia = (int)LHS;
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001216 SDValue ia = LHS;
Matt Arsenault1578aa72014-06-15 20:08:02 +00001217
1218 // int ib, (int)RHS;
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001219 SDValue ib = RHS;
Matt Arsenault1578aa72014-06-15 20:08:02 +00001220
1221 // float fa = (float)ia;
Jan Veselye5ca27d2014-08-12 17:31:20 +00001222 SDValue fa = DAG.getNode(ToFp, DL, FltVT, ia);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001223
1224 // float fb = (float)ib;
Jan Veselye5ca27d2014-08-12 17:31:20 +00001225 SDValue fb = DAG.getNode(ToFp, DL, FltVT, ib);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001226
Matt Arsenault0daeb632014-07-24 06:59:20 +00001227 SDValue fq = DAG.getNode(ISD::FMUL, DL, FltVT,
1228 fa, DAG.getNode(AMDGPUISD::RCP, DL, FltVT, fb));
Matt Arsenault1578aa72014-06-15 20:08:02 +00001229
1230 // fq = trunc(fq);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001231 fq = DAG.getNode(ISD::FTRUNC, DL, FltVT, fq);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001232
1233 // float fqneg = -fq;
Matt Arsenault0daeb632014-07-24 06:59:20 +00001234 SDValue fqneg = DAG.getNode(ISD::FNEG, DL, FltVT, fq);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001235
1236 // float fr = mad(fqneg, fb, fa);
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001237 SDValue fr = DAG.getNode(ISD::FMAD, DL, FltVT, fqneg, fb, fa);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001238
1239 // int iq = (int)fq;
Jan Veselye5ca27d2014-08-12 17:31:20 +00001240 SDValue iq = DAG.getNode(ToInt, DL, IntVT, fq);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001241
1242 // fr = fabs(fr);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001243 fr = DAG.getNode(ISD::FABS, DL, FltVT, fr);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001244
1245 // fb = fabs(fb);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001246 fb = DAG.getNode(ISD::FABS, DL, FltVT, fb);
1247
Mehdi Amini44ede332015-07-09 02:09:04 +00001248 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001249
1250 // int cv = fr >= fb;
Matt Arsenault0daeb632014-07-24 06:59:20 +00001251 SDValue cv = DAG.getSetCC(DL, SetCCVT, fr, fb, ISD::SETOGE);
1252
Matt Arsenault1578aa72014-06-15 20:08:02 +00001253 // jq = (cv ? jq : 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001254 jq = DAG.getNode(ISD::SELECT, DL, VT, cv, jq, DAG.getConstant(0, DL, VT));
Matt Arsenault0daeb632014-07-24 06:59:20 +00001255
Jan Veselye5ca27d2014-08-12 17:31:20 +00001256 // dst = iq + jq;
Jan Vesely4a33bc62014-08-12 17:31:17 +00001257 SDValue Div = DAG.getNode(ISD::ADD, DL, VT, iq, jq);
1258
Jan Veselye5ca27d2014-08-12 17:31:20 +00001259 // Rem needs compensation, it's easier to recompute it
Jan Vesely4a33bc62014-08-12 17:31:17 +00001260 SDValue Rem = DAG.getNode(ISD::MUL, DL, VT, Div, RHS);
1261 Rem = DAG.getNode(ISD::SUB, DL, VT, LHS, Rem);
1262
Matt Arsenault81a70952016-05-21 01:53:33 +00001263 // Truncate to number of bits this divide really is.
1264 if (Sign) {
1265 SDValue InRegSize
1266 = DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), DivBits));
1267 Div = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Div, InRegSize);
1268 Rem = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Rem, InRegSize);
1269 } else {
1270 SDValue TruncMask = DAG.getConstant((UINT64_C(1) << DivBits) - 1, DL, VT);
1271 Div = DAG.getNode(ISD::AND, DL, VT, Div, TruncMask);
1272 Rem = DAG.getNode(ISD::AND, DL, VT, Rem, TruncMask);
1273 }
1274
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001275 return DAG.getMergeValues({ Div, Rem }, DL);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001276}
1277
Tom Stellardbf69d762014-11-15 01:07:53 +00001278void AMDGPUTargetLowering::LowerUDIVREM64(SDValue Op,
1279 SelectionDAG &DAG,
1280 SmallVectorImpl<SDValue> &Results) const {
1281 assert(Op.getValueType() == MVT::i64);
1282
1283 SDLoc DL(Op);
1284 EVT VT = Op.getValueType();
1285 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
1286
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001287 SDValue one = DAG.getConstant(1, DL, HalfVT);
1288 SDValue zero = DAG.getConstant(0, DL, HalfVT);
Tom Stellardbf69d762014-11-15 01:07:53 +00001289
1290 //HiLo split
1291 SDValue LHS = Op.getOperand(0);
1292 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, zero);
1293 SDValue LHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, one);
1294
1295 SDValue RHS = Op.getOperand(1);
1296 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, zero);
1297 SDValue RHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, one);
1298
Jan Vesely5f715d32015-01-22 23:42:43 +00001299 if (VT == MVT::i64 &&
1300 DAG.MaskedValueIsZero(RHS, APInt::getHighBitsSet(64, 32)) &&
1301 DAG.MaskedValueIsZero(LHS, APInt::getHighBitsSet(64, 32))) {
1302
1303 SDValue Res = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(HalfVT, HalfVT),
1304 LHS_Lo, RHS_Lo);
1305
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001306 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(0), zero});
1307 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(1), zero});
Matt Arsenaultd275fca2016-03-01 05:06:05 +00001308
1309 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV));
1310 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM));
Jan Vesely5f715d32015-01-22 23:42:43 +00001311 return;
1312 }
1313
Tom Stellardbf69d762014-11-15 01:07:53 +00001314 // Get Speculative values
1315 SDValue DIV_Part = DAG.getNode(ISD::UDIV, DL, HalfVT, LHS_Hi, RHS_Lo);
1316 SDValue REM_Part = DAG.getNode(ISD::UREM, DL, HalfVT, LHS_Hi, RHS_Lo);
1317
Tom Stellardbf69d762014-11-15 01:07:53 +00001318 SDValue REM_Lo = DAG.getSelectCC(DL, RHS_Hi, zero, REM_Part, LHS_Hi, ISD::SETEQ);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001319 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {REM_Lo, zero});
Matt Arsenaultd275fca2016-03-01 05:06:05 +00001320 REM = DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM);
Tom Stellardbf69d762014-11-15 01:07:53 +00001321
1322 SDValue DIV_Hi = DAG.getSelectCC(DL, RHS_Hi, zero, DIV_Part, zero, ISD::SETEQ);
1323 SDValue DIV_Lo = zero;
1324
1325 const unsigned halfBitWidth = HalfVT.getSizeInBits();
1326
1327 for (unsigned i = 0; i < halfBitWidth; ++i) {
Jan Veselyf7987ca2015-01-22 23:42:39 +00001328 const unsigned bitPos = halfBitWidth - i - 1;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001329 SDValue POS = DAG.getConstant(bitPos, DL, HalfVT);
Jan Veselyf7987ca2015-01-22 23:42:39 +00001330 // Get value of high bit
Jan Vesely811ef522015-04-12 23:45:01 +00001331 SDValue HBit = DAG.getNode(ISD::SRL, DL, HalfVT, LHS_Lo, POS);
1332 HBit = DAG.getNode(ISD::AND, DL, HalfVT, HBit, one);
Jan Veselyf7987ca2015-01-22 23:42:39 +00001333 HBit = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, HBit);
Tom Stellardbf69d762014-11-15 01:07:53 +00001334
Jan Veselyf7987ca2015-01-22 23:42:39 +00001335 // Shift
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001336 REM = DAG.getNode(ISD::SHL, DL, VT, REM, DAG.getConstant(1, DL, VT));
Jan Veselyf7987ca2015-01-22 23:42:39 +00001337 // Add LHS high bit
1338 REM = DAG.getNode(ISD::OR, DL, VT, REM, HBit);
Tom Stellardbf69d762014-11-15 01:07:53 +00001339
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +00001340 SDValue BIT = DAG.getConstant(1ULL << bitPos, DL, HalfVT);
Tom Stellard83171b32014-11-15 01:07:57 +00001341 SDValue realBIT = DAG.getSelectCC(DL, REM, RHS, BIT, zero, ISD::SETUGE);
Tom Stellardbf69d762014-11-15 01:07:53 +00001342
1343 DIV_Lo = DAG.getNode(ISD::OR, DL, HalfVT, DIV_Lo, realBIT);
1344
1345 // Update REM
Tom Stellardbf69d762014-11-15 01:07:53 +00001346 SDValue REM_sub = DAG.getNode(ISD::SUB, DL, VT, REM, RHS);
Tom Stellard83171b32014-11-15 01:07:57 +00001347 REM = DAG.getSelectCC(DL, REM, RHS, REM_sub, REM, ISD::SETUGE);
Tom Stellardbf69d762014-11-15 01:07:53 +00001348 }
1349
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001350 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {DIV_Lo, DIV_Hi});
Matt Arsenaultd275fca2016-03-01 05:06:05 +00001351 DIV = DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV);
Tom Stellardbf69d762014-11-15 01:07:53 +00001352 Results.push_back(DIV);
1353 Results.push_back(REM);
1354}
1355
Tom Stellard75aadc22012-12-11 21:25:42 +00001356SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
Matt Arsenault46013d92014-05-11 21:24:41 +00001357 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001358 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +00001359 EVT VT = Op.getValueType();
1360
Tom Stellardbf69d762014-11-15 01:07:53 +00001361 if (VT == MVT::i64) {
1362 SmallVector<SDValue, 2> Results;
1363 LowerUDIVREM64(Op, DAG, Results);
1364 return DAG.getMergeValues(Results, DL);
1365 }
1366
Matt Arsenault81a70952016-05-21 01:53:33 +00001367 if (VT == MVT::i32) {
1368 if (SDValue Res = LowerDIVREM24(Op, DAG, false))
1369 return Res;
1370 }
1371
Tom Stellard75aadc22012-12-11 21:25:42 +00001372 SDValue Num = Op.getOperand(0);
1373 SDValue Den = Op.getOperand(1);
1374
Tom Stellard75aadc22012-12-11 21:25:42 +00001375 // RCP = URECIP(Den) = 2^32 / Den + e
1376 // e is rounding error.
1377 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
1378
Tom Stellard4349b192014-09-22 15:35:30 +00001379 // RCP_LO = mul(RCP, Den) */
1380 SDValue RCP_LO = DAG.getNode(ISD::MUL, DL, VT, RCP, Den);
Tom Stellard75aadc22012-12-11 21:25:42 +00001381
1382 // RCP_HI = mulhu (RCP, Den) */
1383 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
1384
1385 // NEG_RCP_LO = -RCP_LO
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001386 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001387 RCP_LO);
1388
1389 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001390 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001391 NEG_RCP_LO, RCP_LO,
1392 ISD::SETEQ);
1393 // Calculate the rounding error from the URECIP instruction
1394 // E = mulhu(ABS_RCP_LO, RCP)
1395 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
1396
1397 // RCP_A_E = RCP + E
1398 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
1399
1400 // RCP_S_E = RCP - E
1401 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
1402
1403 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001404 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001405 RCP_A_E, RCP_S_E,
1406 ISD::SETEQ);
1407 // Quotient = mulhu(Tmp0, Num)
1408 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
1409
1410 // Num_S_Remainder = Quotient * Den
Tom Stellard4349b192014-09-22 15:35:30 +00001411 SDValue Num_S_Remainder = DAG.getNode(ISD::MUL, DL, VT, Quotient, Den);
Tom Stellard75aadc22012-12-11 21:25:42 +00001412
1413 // Remainder = Num - Num_S_Remainder
1414 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
1415
1416 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
1417 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001418 DAG.getConstant(-1, DL, VT),
1419 DAG.getConstant(0, DL, VT),
Vincent Lejeune4f3751f2013-11-06 17:36:04 +00001420 ISD::SETUGE);
1421 // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0)
1422 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Num,
1423 Num_S_Remainder,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001424 DAG.getConstant(-1, DL, VT),
1425 DAG.getConstant(0, DL, VT),
Vincent Lejeune4f3751f2013-11-06 17:36:04 +00001426 ISD::SETUGE);
Tom Stellard75aadc22012-12-11 21:25:42 +00001427 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
1428 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
1429 Remainder_GE_Zero);
1430
1431 // Calculate Division result:
1432
1433 // Quotient_A_One = Quotient + 1
1434 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001435 DAG.getConstant(1, DL, VT));
Tom Stellard75aadc22012-12-11 21:25:42 +00001436
1437 // Quotient_S_One = Quotient - 1
1438 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001439 DAG.getConstant(1, DL, VT));
Tom Stellard75aadc22012-12-11 21:25:42 +00001440
1441 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001442 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001443 Quotient, Quotient_A_One, ISD::SETEQ);
1444
1445 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001446 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001447 Quotient_S_One, Div, ISD::SETEQ);
1448
1449 // Calculate Rem result:
1450
1451 // Remainder_S_Den = Remainder - Den
1452 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
1453
1454 // Remainder_A_Den = Remainder + Den
1455 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
1456
1457 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001458 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001459 Remainder, Remainder_S_Den, ISD::SETEQ);
1460
1461 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001462 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001463 Remainder_A_Den, Rem, ISD::SETEQ);
Matt Arsenault7939acd2014-04-07 16:44:24 +00001464 SDValue Ops[2] = {
1465 Div,
1466 Rem
1467 };
Craig Topper64941d92014-04-27 19:20:57 +00001468 return DAG.getMergeValues(Ops, DL);
Tom Stellard75aadc22012-12-11 21:25:42 +00001469}
1470
Jan Vesely109efdf2014-06-22 21:43:00 +00001471SDValue AMDGPUTargetLowering::LowerSDIVREM(SDValue Op,
1472 SelectionDAG &DAG) const {
1473 SDLoc DL(Op);
1474 EVT VT = Op.getValueType();
1475
Jan Vesely109efdf2014-06-22 21:43:00 +00001476 SDValue LHS = Op.getOperand(0);
1477 SDValue RHS = Op.getOperand(1);
1478
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001479 SDValue Zero = DAG.getConstant(0, DL, VT);
1480 SDValue NegOne = DAG.getConstant(-1, DL, VT);
Jan Vesely4a33bc62014-08-12 17:31:17 +00001481
Matt Arsenault81a70952016-05-21 01:53:33 +00001482 if (VT == MVT::i32) {
1483 if (SDValue Res = LowerDIVREM24(Op, DAG, true))
1484 return Res;
Jan Vesely5f715d32015-01-22 23:42:43 +00001485 }
Matt Arsenault81a70952016-05-21 01:53:33 +00001486
Jan Vesely5f715d32015-01-22 23:42:43 +00001487 if (VT == MVT::i64 &&
1488 DAG.ComputeNumSignBits(LHS) > 32 &&
1489 DAG.ComputeNumSignBits(RHS) > 32) {
1490 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
1491
1492 //HiLo split
1493 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, Zero);
1494 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, Zero);
1495 SDValue DIVREM = DAG.getNode(ISD::SDIVREM, DL, DAG.getVTList(HalfVT, HalfVT),
1496 LHS_Lo, RHS_Lo);
1497 SDValue Res[2] = {
1498 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(0)),
1499 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(1))
1500 };
1501 return DAG.getMergeValues(Res, DL);
1502 }
1503
Jan Vesely109efdf2014-06-22 21:43:00 +00001504 SDValue LHSign = DAG.getSelectCC(DL, LHS, Zero, NegOne, Zero, ISD::SETLT);
1505 SDValue RHSign = DAG.getSelectCC(DL, RHS, Zero, NegOne, Zero, ISD::SETLT);
1506 SDValue DSign = DAG.getNode(ISD::XOR, DL, VT, LHSign, RHSign);
1507 SDValue RSign = LHSign; // Remainder sign is the same as LHS
1508
1509 LHS = DAG.getNode(ISD::ADD, DL, VT, LHS, LHSign);
1510 RHS = DAG.getNode(ISD::ADD, DL, VT, RHS, RHSign);
1511
1512 LHS = DAG.getNode(ISD::XOR, DL, VT, LHS, LHSign);
1513 RHS = DAG.getNode(ISD::XOR, DL, VT, RHS, RHSign);
1514
1515 SDValue Div = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT), LHS, RHS);
1516 SDValue Rem = Div.getValue(1);
1517
1518 Div = DAG.getNode(ISD::XOR, DL, VT, Div, DSign);
1519 Rem = DAG.getNode(ISD::XOR, DL, VT, Rem, RSign);
1520
1521 Div = DAG.getNode(ISD::SUB, DL, VT, Div, DSign);
1522 Rem = DAG.getNode(ISD::SUB, DL, VT, Rem, RSign);
1523
1524 SDValue Res[2] = {
1525 Div,
1526 Rem
1527 };
1528 return DAG.getMergeValues(Res, DL);
1529}
1530
Matt Arsenault16e31332014-09-10 21:44:27 +00001531// (frem x, y) -> (fsub x, (fmul (ftrunc (fdiv x, y)), y))
1532SDValue AMDGPUTargetLowering::LowerFREM(SDValue Op, SelectionDAG &DAG) const {
1533 SDLoc SL(Op);
1534 EVT VT = Op.getValueType();
1535 SDValue X = Op.getOperand(0);
1536 SDValue Y = Op.getOperand(1);
1537
Sanjay Patela2607012015-09-16 16:31:21 +00001538 // TODO: Should this propagate fast-math-flags?
1539
Matt Arsenault16e31332014-09-10 21:44:27 +00001540 SDValue Div = DAG.getNode(ISD::FDIV, SL, VT, X, Y);
1541 SDValue Floor = DAG.getNode(ISD::FTRUNC, SL, VT, Div);
1542 SDValue Mul = DAG.getNode(ISD::FMUL, SL, VT, Floor, Y);
1543
1544 return DAG.getNode(ISD::FSUB, SL, VT, X, Mul);
1545}
1546
Matt Arsenault46010932014-06-18 17:05:30 +00001547SDValue AMDGPUTargetLowering::LowerFCEIL(SDValue Op, SelectionDAG &DAG) const {
1548 SDLoc SL(Op);
1549 SDValue Src = Op.getOperand(0);
1550
1551 // result = trunc(src)
1552 // if (src > 0.0 && src != result)
1553 // result += 1.0
1554
1555 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1556
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001557 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64);
1558 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001559
Mehdi Amini44ede332015-07-09 02:09:04 +00001560 EVT SetCCVT =
1561 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001562
1563 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOGT);
1564 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1565 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1566
1567 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, One, Zero);
Sanjay Patela2607012015-09-16 16:31:21 +00001568 // TODO: Should this propagate fast-math-flags?
Matt Arsenault46010932014-06-18 17:05:30 +00001569 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1570}
1571
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001572static SDValue extractF64Exponent(SDValue Hi, const SDLoc &SL,
1573 SelectionDAG &DAG) {
Matt Arsenaultb0055482015-01-21 18:18:25 +00001574 const unsigned FractBits = 52;
1575 const unsigned ExpBits = 11;
1576
1577 SDValue ExpPart = DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32,
1578 Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001579 DAG.getConstant(FractBits - 32, SL, MVT::i32),
1580 DAG.getConstant(ExpBits, SL, MVT::i32));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001581 SDValue Exp = DAG.getNode(ISD::SUB, SL, MVT::i32, ExpPart,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001582 DAG.getConstant(1023, SL, MVT::i32));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001583
1584 return Exp;
1585}
1586
Matt Arsenault46010932014-06-18 17:05:30 +00001587SDValue AMDGPUTargetLowering::LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const {
1588 SDLoc SL(Op);
1589 SDValue Src = Op.getOperand(0);
1590
1591 assert(Op.getValueType() == MVT::f64);
1592
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001593 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1594 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001595
1596 SDValue VecSrc = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1597
1598 // Extract the upper half, since this is where we will find the sign and
1599 // exponent.
1600 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, VecSrc, One);
1601
Matt Arsenaultb0055482015-01-21 18:18:25 +00001602 SDValue Exp = extractF64Exponent(Hi, SL, DAG);
Matt Arsenault46010932014-06-18 17:05:30 +00001603
Matt Arsenaultb0055482015-01-21 18:18:25 +00001604 const unsigned FractBits = 52;
Matt Arsenault46010932014-06-18 17:05:30 +00001605
1606 // Extract the sign bit.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001607 const SDValue SignBitMask = DAG.getConstant(UINT32_C(1) << 31, SL, MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001608 SDValue SignBit = DAG.getNode(ISD::AND, SL, MVT::i32, Hi, SignBitMask);
1609
1610 // Extend back to to 64-bits.
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001611 SDValue SignBit64 = DAG.getBuildVector(MVT::v2i32, SL, {Zero, SignBit});
Matt Arsenault46010932014-06-18 17:05:30 +00001612 SignBit64 = DAG.getNode(ISD::BITCAST, SL, MVT::i64, SignBit64);
1613
1614 SDValue BcInt = DAG.getNode(ISD::BITCAST, SL, MVT::i64, Src);
Matt Arsenault2b0fa432014-06-18 22:11:03 +00001615 const SDValue FractMask
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001616 = DAG.getConstant((UINT64_C(1) << FractBits) - 1, SL, MVT::i64);
Matt Arsenault46010932014-06-18 17:05:30 +00001617
1618 SDValue Shr = DAG.getNode(ISD::SRA, SL, MVT::i64, FractMask, Exp);
1619 SDValue Not = DAG.getNOT(SL, Shr, MVT::i64);
1620 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, BcInt, Not);
1621
Mehdi Amini44ede332015-07-09 02:09:04 +00001622 EVT SetCCVT =
1623 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001624
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001625 const SDValue FiftyOne = DAG.getConstant(FractBits - 1, SL, MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001626
1627 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
1628 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
1629
1630 SDValue Tmp1 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpLt0, SignBit64, Tmp0);
1631 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpGt51, BcInt, Tmp1);
1632
1633 return DAG.getNode(ISD::BITCAST, SL, MVT::f64, Tmp2);
1634}
1635
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001636SDValue AMDGPUTargetLowering::LowerFRINT(SDValue Op, SelectionDAG &DAG) const {
1637 SDLoc SL(Op);
1638 SDValue Src = Op.getOperand(0);
1639
1640 assert(Op.getValueType() == MVT::f64);
1641
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001642 APFloat C1Val(APFloat::IEEEdouble(), "0x1.0p+52");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001643 SDValue C1 = DAG.getConstantFP(C1Val, SL, MVT::f64);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001644 SDValue CopySign = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, C1, Src);
1645
Sanjay Patela2607012015-09-16 16:31:21 +00001646 // TODO: Should this propagate fast-math-flags?
1647
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001648 SDValue Tmp1 = DAG.getNode(ISD::FADD, SL, MVT::f64, Src, CopySign);
1649 SDValue Tmp2 = DAG.getNode(ISD::FSUB, SL, MVT::f64, Tmp1, CopySign);
1650
1651 SDValue Fabs = DAG.getNode(ISD::FABS, SL, MVT::f64, Src);
Matt Arsenaultd22626f2014-06-18 17:45:58 +00001652
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001653 APFloat C2Val(APFloat::IEEEdouble(), "0x1.fffffffffffffp+51");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001654 SDValue C2 = DAG.getConstantFP(C2Val, SL, MVT::f64);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001655
Mehdi Amini44ede332015-07-09 02:09:04 +00001656 EVT SetCCVT =
1657 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001658 SDValue Cond = DAG.getSetCC(SL, SetCCVT, Fabs, C2, ISD::SETOGT);
1659
1660 return DAG.getSelect(SL, MVT::f64, Cond, Src, Tmp2);
1661}
1662
Matt Arsenault692bd5e2014-06-18 22:03:45 +00001663SDValue AMDGPUTargetLowering::LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const {
1664 // FNEARBYINT and FRINT are the same, except in their handling of FP
1665 // exceptions. Those aren't really meaningful for us, and OpenCL only has
1666 // rint, so just treat them as equivalent.
1667 return DAG.getNode(ISD::FRINT, SDLoc(Op), Op.getValueType(), Op.getOperand(0));
1668}
1669
Matt Arsenaultb0055482015-01-21 18:18:25 +00001670// XXX - May require not supporting f32 denormals?
1671SDValue AMDGPUTargetLowering::LowerFROUND32(SDValue Op, SelectionDAG &DAG) const {
1672 SDLoc SL(Op);
1673 SDValue X = Op.getOperand(0);
1674
1675 SDValue T = DAG.getNode(ISD::FTRUNC, SL, MVT::f32, X);
1676
Sanjay Patela2607012015-09-16 16:31:21 +00001677 // TODO: Should this propagate fast-math-flags?
1678
Matt Arsenaultb0055482015-01-21 18:18:25 +00001679 SDValue Diff = DAG.getNode(ISD::FSUB, SL, MVT::f32, X, T);
1680
1681 SDValue AbsDiff = DAG.getNode(ISD::FABS, SL, MVT::f32, Diff);
1682
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001683 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f32);
1684 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
1685 const SDValue Half = DAG.getConstantFP(0.5, SL, MVT::f32);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001686
1687 SDValue SignOne = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f32, One, X);
1688
Mehdi Amini44ede332015-07-09 02:09:04 +00001689 EVT SetCCVT =
1690 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001691
1692 SDValue Cmp = DAG.getSetCC(SL, SetCCVT, AbsDiff, Half, ISD::SETOGE);
1693
1694 SDValue Sel = DAG.getNode(ISD::SELECT, SL, MVT::f32, Cmp, SignOne, Zero);
1695
1696 return DAG.getNode(ISD::FADD, SL, MVT::f32, T, Sel);
1697}
1698
1699SDValue AMDGPUTargetLowering::LowerFROUND64(SDValue Op, SelectionDAG &DAG) const {
1700 SDLoc SL(Op);
1701 SDValue X = Op.getOperand(0);
1702
1703 SDValue L = DAG.getNode(ISD::BITCAST, SL, MVT::i64, X);
1704
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001705 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1706 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1707 const SDValue NegOne = DAG.getConstant(-1, SL, MVT::i32);
1708 const SDValue FiftyOne = DAG.getConstant(51, SL, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00001709 EVT SetCCVT =
1710 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001711
1712 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1713
1714 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, One);
1715
1716 SDValue Exp = extractF64Exponent(Hi, SL, DAG);
1717
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001718 const SDValue Mask = DAG.getConstant(INT64_C(0x000fffffffffffff), SL,
1719 MVT::i64);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001720
1721 SDValue M = DAG.getNode(ISD::SRA, SL, MVT::i64, Mask, Exp);
1722 SDValue D = DAG.getNode(ISD::SRA, SL, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001723 DAG.getConstant(INT64_C(0x0008000000000000), SL,
1724 MVT::i64),
Matt Arsenaultb0055482015-01-21 18:18:25 +00001725 Exp);
1726
1727 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, L, M);
1728 SDValue Tmp1 = DAG.getSetCC(SL, SetCCVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001729 DAG.getConstant(0, SL, MVT::i64), Tmp0,
Matt Arsenaultb0055482015-01-21 18:18:25 +00001730 ISD::SETNE);
1731
1732 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, Tmp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001733 D, DAG.getConstant(0, SL, MVT::i64));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001734 SDValue K = DAG.getNode(ISD::ADD, SL, MVT::i64, L, Tmp2);
1735
1736 K = DAG.getNode(ISD::AND, SL, MVT::i64, K, DAG.getNOT(SL, M, MVT::i64));
1737 K = DAG.getNode(ISD::BITCAST, SL, MVT::f64, K);
1738
1739 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
1740 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
1741 SDValue ExpEqNegOne = DAG.getSetCC(SL, SetCCVT, NegOne, Exp, ISD::SETEQ);
1742
1743 SDValue Mag = DAG.getNode(ISD::SELECT, SL, MVT::f64,
1744 ExpEqNegOne,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001745 DAG.getConstantFP(1.0, SL, MVT::f64),
1746 DAG.getConstantFP(0.0, SL, MVT::f64));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001747
1748 SDValue S = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, Mag, X);
1749
1750 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpLt0, S, K);
1751 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpGt51, X, K);
1752
1753 return K;
1754}
1755
1756SDValue AMDGPUTargetLowering::LowerFROUND(SDValue Op, SelectionDAG &DAG) const {
1757 EVT VT = Op.getValueType();
1758
1759 if (VT == MVT::f32)
1760 return LowerFROUND32(Op, DAG);
1761
1762 if (VT == MVT::f64)
1763 return LowerFROUND64(Op, DAG);
1764
1765 llvm_unreachable("unhandled type");
1766}
1767
Matt Arsenault46010932014-06-18 17:05:30 +00001768SDValue AMDGPUTargetLowering::LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const {
1769 SDLoc SL(Op);
1770 SDValue Src = Op.getOperand(0);
1771
1772 // result = trunc(src);
1773 // if (src < 0.0 && src != result)
1774 // result += -1.0.
1775
1776 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1777
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001778 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64);
1779 const SDValue NegOne = DAG.getConstantFP(-1.0, SL, MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001780
Mehdi Amini44ede332015-07-09 02:09:04 +00001781 EVT SetCCVT =
1782 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001783
1784 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOLT);
1785 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1786 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1787
1788 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, NegOne, Zero);
Sanjay Patela2607012015-09-16 16:31:21 +00001789 // TODO: Should this propagate fast-math-flags?
Matt Arsenault46010932014-06-18 17:05:30 +00001790 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1791}
1792
Matt Arsenaultf058d672016-01-11 16:50:29 +00001793SDValue AMDGPUTargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
1794 SDLoc SL(Op);
1795 SDValue Src = Op.getOperand(0);
Matt Arsenaultf058d672016-01-11 16:50:29 +00001796 bool ZeroUndef = Op.getOpcode() == ISD::CTLZ_ZERO_UNDEF;
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00001797
1798 if (ZeroUndef && Src.getValueType() == MVT::i32)
1799 return DAG.getNode(AMDGPUISD::FFBH_U32, SL, MVT::i32, Src);
1800
Matt Arsenaultf058d672016-01-11 16:50:29 +00001801 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1802
1803 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1804 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1805
1806 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1807 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1808
1809 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(),
1810 *DAG.getContext(), MVT::i32);
1811
1812 SDValue Hi0 = DAG.getSetCC(SL, SetCCVT, Hi, Zero, ISD::SETEQ);
1813
1814 SDValue CtlzLo = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Lo);
1815 SDValue CtlzHi = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Hi);
1816
1817 const SDValue Bits32 = DAG.getConstant(32, SL, MVT::i32);
1818 SDValue Add = DAG.getNode(ISD::ADD, SL, MVT::i32, CtlzLo, Bits32);
1819
1820 // ctlz(x) = hi_32(x) == 0 ? ctlz(lo_32(x)) + 32 : ctlz(hi_32(x))
1821 SDValue NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32, Hi0, Add, CtlzHi);
1822
1823 if (!ZeroUndef) {
1824 // Test if the full 64-bit input is zero.
1825
1826 // FIXME: DAG combines turn what should be an s_and_b64 into a v_or_b32,
1827 // which we probably don't want.
1828 SDValue Lo0 = DAG.getSetCC(SL, SetCCVT, Lo, Zero, ISD::SETEQ);
1829 SDValue SrcIsZero = DAG.getNode(ISD::AND, SL, SetCCVT, Lo0, Hi0);
1830
1831 // TODO: If i64 setcc is half rate, it can result in 1 fewer instruction
1832 // with the same cycles, otherwise it is slower.
1833 // SDValue SrcIsZero = DAG.getSetCC(SL, SetCCVT, Src,
1834 // DAG.getConstant(0, SL, MVT::i64), ISD::SETEQ);
1835
1836 const SDValue Bits32 = DAG.getConstant(64, SL, MVT::i32);
1837
1838 // The instruction returns -1 for 0 input, but the defined intrinsic
1839 // behavior is to return the number of bits.
1840 NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32,
1841 SrcIsZero, Bits32, NewCtlz);
1842 }
1843
1844 return DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i64, NewCtlz);
1845}
1846
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00001847SDValue AMDGPUTargetLowering::LowerINT_TO_FP32(SDValue Op, SelectionDAG &DAG,
1848 bool Signed) const {
1849 // Unsigned
1850 // cul2f(ulong u)
1851 //{
1852 // uint lz = clz(u);
1853 // uint e = (u != 0) ? 127U + 63U - lz : 0;
1854 // u = (u << lz) & 0x7fffffffffffffffUL;
1855 // ulong t = u & 0xffffffffffUL;
1856 // uint v = (e << 23) | (uint)(u >> 40);
1857 // uint r = t > 0x8000000000UL ? 1U : (t == 0x8000000000UL ? v & 1U : 0U);
1858 // return as_float(v + r);
1859 //}
1860 // Signed
1861 // cl2f(long l)
1862 //{
1863 // long s = l >> 63;
1864 // float r = cul2f((l + s) ^ s);
1865 // return s ? -r : r;
1866 //}
1867
1868 SDLoc SL(Op);
1869 SDValue Src = Op.getOperand(0);
1870 SDValue L = Src;
1871
1872 SDValue S;
1873 if (Signed) {
1874 const SDValue SignBit = DAG.getConstant(63, SL, MVT::i64);
1875 S = DAG.getNode(ISD::SRA, SL, MVT::i64, L, SignBit);
1876
1877 SDValue LPlusS = DAG.getNode(ISD::ADD, SL, MVT::i64, L, S);
1878 L = DAG.getNode(ISD::XOR, SL, MVT::i64, LPlusS, S);
1879 }
1880
1881 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(),
1882 *DAG.getContext(), MVT::f32);
1883
1884
1885 SDValue ZeroI32 = DAG.getConstant(0, SL, MVT::i32);
1886 SDValue ZeroI64 = DAG.getConstant(0, SL, MVT::i64);
1887 SDValue LZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i64, L);
1888 LZ = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LZ);
1889
1890 SDValue K = DAG.getConstant(127U + 63U, SL, MVT::i32);
1891 SDValue E = DAG.getSelect(SL, MVT::i32,
1892 DAG.getSetCC(SL, SetCCVT, L, ZeroI64, ISD::SETNE),
1893 DAG.getNode(ISD::SUB, SL, MVT::i32, K, LZ),
1894 ZeroI32);
1895
1896 SDValue U = DAG.getNode(ISD::AND, SL, MVT::i64,
1897 DAG.getNode(ISD::SHL, SL, MVT::i64, L, LZ),
1898 DAG.getConstant((-1ULL) >> 1, SL, MVT::i64));
1899
1900 SDValue T = DAG.getNode(ISD::AND, SL, MVT::i64, U,
1901 DAG.getConstant(0xffffffffffULL, SL, MVT::i64));
1902
1903 SDValue UShl = DAG.getNode(ISD::SRL, SL, MVT::i64,
1904 U, DAG.getConstant(40, SL, MVT::i64));
1905
1906 SDValue V = DAG.getNode(ISD::OR, SL, MVT::i32,
1907 DAG.getNode(ISD::SHL, SL, MVT::i32, E, DAG.getConstant(23, SL, MVT::i32)),
1908 DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, UShl));
1909
1910 SDValue C = DAG.getConstant(0x8000000000ULL, SL, MVT::i64);
1911 SDValue RCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETUGT);
1912 SDValue TCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETEQ);
1913
1914 SDValue One = DAG.getConstant(1, SL, MVT::i32);
1915
1916 SDValue VTrunc1 = DAG.getNode(ISD::AND, SL, MVT::i32, V, One);
1917
1918 SDValue R = DAG.getSelect(SL, MVT::i32,
1919 RCmp,
1920 One,
1921 DAG.getSelect(SL, MVT::i32, TCmp, VTrunc1, ZeroI32));
1922 R = DAG.getNode(ISD::ADD, SL, MVT::i32, V, R);
1923 R = DAG.getNode(ISD::BITCAST, SL, MVT::f32, R);
1924
1925 if (!Signed)
1926 return R;
1927
1928 SDValue RNeg = DAG.getNode(ISD::FNEG, SL, MVT::f32, R);
1929 return DAG.getSelect(SL, MVT::f32, DAG.getSExtOrTrunc(S, SL, SetCCVT), RNeg, R);
1930}
1931
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001932SDValue AMDGPUTargetLowering::LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG,
1933 bool Signed) const {
1934 SDLoc SL(Op);
1935 SDValue Src = Op.getOperand(0);
1936
1937 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1938
1939 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001940 DAG.getConstant(0, SL, MVT::i32));
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001941 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001942 DAG.getConstant(1, SL, MVT::i32));
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001943
1944 SDValue CvtHi = DAG.getNode(Signed ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
1945 SL, MVT::f64, Hi);
1946
1947 SDValue CvtLo = DAG.getNode(ISD::UINT_TO_FP, SL, MVT::f64, Lo);
1948
1949 SDValue LdExp = DAG.getNode(AMDGPUISD::LDEXP, SL, MVT::f64, CvtHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001950 DAG.getConstant(32, SL, MVT::i32));
Sanjay Patela2607012015-09-16 16:31:21 +00001951 // TODO: Should this propagate fast-math-flags?
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001952 return DAG.getNode(ISD::FADD, SL, MVT::f64, LdExp, CvtLo);
1953}
1954
Tom Stellardc947d8c2013-10-30 17:22:05 +00001955SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op,
1956 SelectionDAG &DAG) const {
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00001957 assert(Op.getOperand(0).getValueType() == MVT::i64 &&
1958 "operation should be legal");
Tom Stellardc947d8c2013-10-30 17:22:05 +00001959
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00001960 // TODO: Factor out code common with LowerSINT_TO_FP.
1961
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001962 EVT DestVT = Op.getValueType();
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00001963 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) {
1964 SDLoc DL(Op);
1965 SDValue Src = Op.getOperand(0);
1966
1967 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src);
1968 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op));
1969 SDValue FPRound =
1970 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag);
1971
1972 return FPRound;
1973 }
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001974
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00001975 if (DestVT == MVT::f32)
1976 return LowerINT_TO_FP32(Op, DAG, false);
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001977
Matt Arsenaultedc7dcb2016-07-28 00:32:05 +00001978 assert(DestVT == MVT::f64);
1979 return LowerINT_TO_FP64(Op, DAG, false);
Tom Stellardc947d8c2013-10-30 17:22:05 +00001980}
Tom Stellardfbab8272013-08-16 01:12:11 +00001981
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001982SDValue AMDGPUTargetLowering::LowerSINT_TO_FP(SDValue Op,
1983 SelectionDAG &DAG) const {
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00001984 assert(Op.getOperand(0).getValueType() == MVT::i64 &&
1985 "operation should be legal");
1986
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00001987 // TODO: Factor out code common with LowerUINT_TO_FP.
1988
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00001989 EVT DestVT = Op.getValueType();
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00001990 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) {
1991 SDLoc DL(Op);
1992 SDValue Src = Op.getOperand(0);
1993
1994 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src);
1995 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op));
1996 SDValue FPRound =
1997 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag);
1998
1999 return FPRound;
2000 }
2001
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00002002 if (DestVT == MVT::f32)
2003 return LowerINT_TO_FP32(Op, DAG, true);
2004
Matt Arsenaultedc7dcb2016-07-28 00:32:05 +00002005 assert(DestVT == MVT::f64);
2006 return LowerINT_TO_FP64(Op, DAG, true);
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002007}
2008
Matt Arsenaultc9961752014-10-03 23:54:56 +00002009SDValue AMDGPUTargetLowering::LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG,
2010 bool Signed) const {
2011 SDLoc SL(Op);
2012
2013 SDValue Src = Op.getOperand(0);
2014
2015 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
2016
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002017 SDValue K0 = DAG.getConstantFP(BitsToDouble(UINT64_C(0x3df0000000000000)), SL,
2018 MVT::f64);
2019 SDValue K1 = DAG.getConstantFP(BitsToDouble(UINT64_C(0xc1f0000000000000)), SL,
2020 MVT::f64);
Sanjay Patela2607012015-09-16 16:31:21 +00002021 // TODO: Should this propagate fast-math-flags?
Matt Arsenaultc9961752014-10-03 23:54:56 +00002022 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, Trunc, K0);
2023
2024 SDValue FloorMul = DAG.getNode(ISD::FFLOOR, SL, MVT::f64, Mul);
2025
2026
2027 SDValue Fma = DAG.getNode(ISD::FMA, SL, MVT::f64, FloorMul, K1, Trunc);
2028
2029 SDValue Hi = DAG.getNode(Signed ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, SL,
2030 MVT::i32, FloorMul);
2031 SDValue Lo = DAG.getNode(ISD::FP_TO_UINT, SL, MVT::i32, Fma);
2032
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002033 SDValue Result = DAG.getBuildVector(MVT::v2i32, SL, {Lo, Hi});
Matt Arsenaultc9961752014-10-03 23:54:56 +00002034
2035 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Result);
2036}
2037
Tom Stellard94c21bc2016-11-01 16:31:48 +00002038SDValue AMDGPUTargetLowering::LowerFP_TO_FP16(SDValue Op, SelectionDAG &DAG) const {
2039
2040 if (getTargetMachine().Options.UnsafeFPMath) {
2041 // There is a generic expand for FP_TO_FP16 with unsafe fast math.
2042 return SDValue();
2043 }
2044
2045 SDLoc DL(Op);
2046 SDValue N0 = Op.getOperand(0);
Tom Stellard9677b602016-11-01 17:20:03 +00002047 assert (N0.getSimpleValueType() == MVT::f64);
Tom Stellard94c21bc2016-11-01 16:31:48 +00002048
2049 // f64 -> f16 conversion using round-to-nearest-even rounding mode.
2050 const unsigned ExpMask = 0x7ff;
2051 const unsigned ExpBiasf64 = 1023;
2052 const unsigned ExpBiasf16 = 15;
2053 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2054 SDValue One = DAG.getConstant(1, DL, MVT::i32);
2055 SDValue U = DAG.getNode(ISD::BITCAST, DL, MVT::i64, N0);
2056 SDValue UH = DAG.getNode(ISD::SRL, DL, MVT::i64, U,
2057 DAG.getConstant(32, DL, MVT::i64));
2058 UH = DAG.getZExtOrTrunc(UH, DL, MVT::i32);
2059 U = DAG.getZExtOrTrunc(U, DL, MVT::i32);
2060 SDValue E = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2061 DAG.getConstant(20, DL, MVT::i64));
2062 E = DAG.getNode(ISD::AND, DL, MVT::i32, E,
2063 DAG.getConstant(ExpMask, DL, MVT::i32));
2064 // Subtract the fp64 exponent bias (1023) to get the real exponent and
2065 // add the f16 bias (15) to get the biased exponent for the f16 format.
2066 E = DAG.getNode(ISD::ADD, DL, MVT::i32, E,
2067 DAG.getConstant(-ExpBiasf64 + ExpBiasf16, DL, MVT::i32));
2068
2069 SDValue M = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2070 DAG.getConstant(8, DL, MVT::i32));
2071 M = DAG.getNode(ISD::AND, DL, MVT::i32, M,
2072 DAG.getConstant(0xffe, DL, MVT::i32));
2073
2074 SDValue MaskedSig = DAG.getNode(ISD::AND, DL, MVT::i32, UH,
2075 DAG.getConstant(0x1ff, DL, MVT::i32));
2076 MaskedSig = DAG.getNode(ISD::OR, DL, MVT::i32, MaskedSig, U);
2077
2078 SDValue Lo40Set = DAG.getSelectCC(DL, MaskedSig, Zero, Zero, One, ISD::SETEQ);
2079 M = DAG.getNode(ISD::OR, DL, MVT::i32, M, Lo40Set);
2080
2081 // (M != 0 ? 0x0200 : 0) | 0x7c00;
2082 SDValue I = DAG.getNode(ISD::OR, DL, MVT::i32,
2083 DAG.getSelectCC(DL, M, Zero, DAG.getConstant(0x0200, DL, MVT::i32),
2084 Zero, ISD::SETNE), DAG.getConstant(0x7c00, DL, MVT::i32));
2085
2086 // N = M | (E << 12);
2087 SDValue N = DAG.getNode(ISD::OR, DL, MVT::i32, M,
2088 DAG.getNode(ISD::SHL, DL, MVT::i32, E,
2089 DAG.getConstant(12, DL, MVT::i32)));
2090
2091 // B = clamp(1-E, 0, 13);
2092 SDValue OneSubExp = DAG.getNode(ISD::SUB, DL, MVT::i32,
2093 One, E);
2094 SDValue B = DAG.getNode(ISD::SMAX, DL, MVT::i32, OneSubExp, Zero);
2095 B = DAG.getNode(ISD::SMIN, DL, MVT::i32, B,
2096 DAG.getConstant(13, DL, MVT::i32));
2097
2098 SDValue SigSetHigh = DAG.getNode(ISD::OR, DL, MVT::i32, M,
2099 DAG.getConstant(0x1000, DL, MVT::i32));
2100
2101 SDValue D = DAG.getNode(ISD::SRL, DL, MVT::i32, SigSetHigh, B);
2102 SDValue D0 = DAG.getNode(ISD::SHL, DL, MVT::i32, D, B);
2103 SDValue D1 = DAG.getSelectCC(DL, D0, SigSetHigh, One, Zero, ISD::SETNE);
2104 D = DAG.getNode(ISD::OR, DL, MVT::i32, D, D1);
2105
2106 SDValue V = DAG.getSelectCC(DL, E, One, D, N, ISD::SETLT);
2107 SDValue VLow3 = DAG.getNode(ISD::AND, DL, MVT::i32, V,
2108 DAG.getConstant(0x7, DL, MVT::i32));
2109 V = DAG.getNode(ISD::SRL, DL, MVT::i32, V,
2110 DAG.getConstant(2, DL, MVT::i32));
2111 SDValue V0 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(3, DL, MVT::i32),
2112 One, Zero, ISD::SETEQ);
2113 SDValue V1 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(5, DL, MVT::i32),
2114 One, Zero, ISD::SETGT);
2115 V1 = DAG.getNode(ISD::OR, DL, MVT::i32, V0, V1);
2116 V = DAG.getNode(ISD::ADD, DL, MVT::i32, V, V1);
2117
2118 V = DAG.getSelectCC(DL, E, DAG.getConstant(30, DL, MVT::i32),
2119 DAG.getConstant(0x7c00, DL, MVT::i32), V, ISD::SETGT);
2120 V = DAG.getSelectCC(DL, E, DAG.getConstant(1039, DL, MVT::i32),
2121 I, V, ISD::SETEQ);
2122
2123 // Extract the sign bit.
2124 SDValue Sign = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2125 DAG.getConstant(16, DL, MVT::i32));
2126 Sign = DAG.getNode(ISD::AND, DL, MVT::i32, Sign,
2127 DAG.getConstant(0x8000, DL, MVT::i32));
2128
2129 V = DAG.getNode(ISD::OR, DL, MVT::i32, Sign, V);
2130 return DAG.getZExtOrTrunc(V, DL, Op.getValueType());
2131}
2132
Matt Arsenaultc9961752014-10-03 23:54:56 +00002133SDValue AMDGPUTargetLowering::LowerFP_TO_SINT(SDValue Op,
2134 SelectionDAG &DAG) const {
2135 SDValue Src = Op.getOperand(0);
2136
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002137 // TODO: Factor out code common with LowerFP_TO_UINT.
2138
2139 EVT SrcVT = Src.getValueType();
2140 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) {
2141 SDLoc DL(Op);
2142
2143 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src);
2144 SDValue FpToInt32 =
2145 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend);
2146
2147 return FpToInt32;
2148 }
2149
Matt Arsenaultc9961752014-10-03 23:54:56 +00002150 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64)
2151 return LowerFP64_TO_INT(Op, DAG, true);
2152
2153 return SDValue();
2154}
2155
2156SDValue AMDGPUTargetLowering::LowerFP_TO_UINT(SDValue Op,
2157 SelectionDAG &DAG) const {
2158 SDValue Src = Op.getOperand(0);
2159
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002160 // TODO: Factor out code common with LowerFP_TO_SINT.
2161
2162 EVT SrcVT = Src.getValueType();
2163 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) {
2164 SDLoc DL(Op);
2165
2166 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src);
2167 SDValue FpToInt32 =
2168 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend);
2169
2170 return FpToInt32;
2171 }
2172
Matt Arsenaultc9961752014-10-03 23:54:56 +00002173 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64)
2174 return LowerFP64_TO_INT(Op, DAG, false);
2175
2176 return SDValue();
2177}
2178
Matt Arsenaultfae02982014-03-17 18:58:11 +00002179SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
2180 SelectionDAG &DAG) const {
2181 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
2182 MVT VT = Op.getSimpleValueType();
2183 MVT ScalarVT = VT.getScalarType();
2184
Matt Arsenaultedc7dcb2016-07-28 00:32:05 +00002185 assert(VT.isVector());
Matt Arsenaultfae02982014-03-17 18:58:11 +00002186
2187 SDValue Src = Op.getOperand(0);
Matt Arsenault5dbd5db2014-04-22 03:49:30 +00002188 SDLoc DL(Op);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002189
Matt Arsenault5dbd5db2014-04-22 03:49:30 +00002190 // TODO: Don't scalarize on Evergreen?
2191 unsigned NElts = VT.getVectorNumElements();
2192 SmallVector<SDValue, 8> Args;
2193 DAG.ExtractVectorElements(Src, Args, 0, NElts);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002194
Matt Arsenault5dbd5db2014-04-22 03:49:30 +00002195 SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType());
2196 for (unsigned I = 0; I < NElts; ++I)
2197 Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002198
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002199 return DAG.getBuildVector(VT, DL, Args);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002200}
2201
Tom Stellard75aadc22012-12-11 21:25:42 +00002202//===----------------------------------------------------------------------===//
Tom Stellard50122a52014-04-07 19:45:41 +00002203// Custom DAG optimizations
2204//===----------------------------------------------------------------------===//
2205
2206static bool isU24(SDValue Op, SelectionDAG &DAG) {
2207 APInt KnownZero, KnownOne;
2208 EVT VT = Op.getValueType();
Jay Foada0653a32014-05-14 21:14:37 +00002209 DAG.computeKnownBits(Op, KnownZero, KnownOne);
Tom Stellard50122a52014-04-07 19:45:41 +00002210
2211 return (VT.getSizeInBits() - KnownZero.countLeadingOnes()) <= 24;
2212}
2213
2214static bool isI24(SDValue Op, SelectionDAG &DAG) {
2215 EVT VT = Op.getValueType();
2216
2217 // In order for this to be a signed 24-bit value, bit 23, must
2218 // be a sign bit.
2219 return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated
2220 // as unsigned 24-bit values.
2221 (VT.getSizeInBits() - DAG.ComputeNumSignBits(Op)) < 24;
2222}
2223
Tom Stellard09c2bd62016-10-14 19:14:29 +00002224static bool simplifyI24(SDNode *Node24, unsigned OpIdx,
2225 TargetLowering::DAGCombinerInfo &DCI) {
Tom Stellard50122a52014-04-07 19:45:41 +00002226
2227 SelectionDAG &DAG = DCI.DAG;
Tom Stellard09c2bd62016-10-14 19:14:29 +00002228 SDValue Op = Node24->getOperand(OpIdx);
Tom Stellard50122a52014-04-07 19:45:41 +00002229 EVT VT = Op.getValueType();
2230
2231 APInt Demanded = APInt::getLowBitsSet(VT.getSizeInBits(), 24);
2232 APInt KnownZero, KnownOne;
2233 TargetLowering::TargetLoweringOpt TLO(DAG, true, true);
Tom Stellard09c2bd62016-10-14 19:14:29 +00002234 if (TLO.SimplifyDemandedBits(Node24, OpIdx, Demanded, DCI))
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002235 return true;
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002236
2237 return false;
Tom Stellard50122a52014-04-07 19:45:41 +00002238}
2239
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002240template <typename IntTy>
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002241static SDValue constantFoldBFE(SelectionDAG &DAG, IntTy Src0, uint32_t Offset,
2242 uint32_t Width, const SDLoc &DL) {
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002243 if (Width + Offset < 32) {
Matt Arsenault46cbc432014-09-19 00:42:06 +00002244 uint32_t Shl = static_cast<uint32_t>(Src0) << (32 - Offset - Width);
2245 IntTy Result = static_cast<IntTy>(Shl) >> (32 - Width);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002246 return DAG.getConstant(Result, DL, MVT::i32);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002247 }
2248
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002249 return DAG.getConstant(Src0 >> Offset, DL, MVT::i32);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002250}
2251
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002252static bool hasVolatileUser(SDNode *Val) {
2253 for (SDNode *U : Val->uses()) {
2254 if (MemSDNode *M = dyn_cast<MemSDNode>(U)) {
2255 if (M->isVolatile())
2256 return true;
2257 }
2258 }
2259
2260 return false;
2261}
2262
Matt Arsenault8af47a02016-07-01 22:55:55 +00002263bool AMDGPUTargetLowering::shouldCombineMemoryType(EVT VT) const {
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002264 // i32 vectors are the canonical memory type.
2265 if (VT.getScalarType() == MVT::i32 || isTypeLegal(VT))
2266 return false;
2267
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002268 if (!VT.isByteSized())
2269 return false;
2270
2271 unsigned Size = VT.getStoreSize();
2272
2273 if ((Size == 1 || Size == 2 || Size == 4) && !VT.isVector())
2274 return false;
2275
2276 if (Size == 3 || (Size > 4 && (Size % 4 != 0)))
2277 return false;
2278
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002279 return true;
2280}
2281
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002282// Replace load of an illegal type with a store of a bitcast to a friendlier
2283// type.
2284SDValue AMDGPUTargetLowering::performLoadCombine(SDNode *N,
2285 DAGCombinerInfo &DCI) const {
2286 if (!DCI.isBeforeLegalize())
2287 return SDValue();
2288
2289 LoadSDNode *LN = cast<LoadSDNode>(N);
2290 if (LN->isVolatile() || !ISD::isNormalLoad(LN) || hasVolatileUser(LN))
2291 return SDValue();
2292
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002293 SDLoc SL(N);
2294 SelectionDAG &DAG = DCI.DAG;
2295 EVT VT = LN->getMemoryVT();
Matt Arsenault8af47a02016-07-01 22:55:55 +00002296
2297 unsigned Size = VT.getStoreSize();
2298 unsigned Align = LN->getAlignment();
2299 if (Align < Size && isTypeLegal(VT)) {
2300 bool IsFast;
2301 unsigned AS = LN->getAddressSpace();
2302
2303 // Expand unaligned loads earlier than legalization. Due to visitation order
2304 // problems during legalization, the emitted instructions to pack and unpack
2305 // the bytes again are not eliminated in the case of an unaligned copy.
2306 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) {
Matt Arsenaultb50eb8d2016-08-31 21:52:27 +00002307 if (VT.isVector())
2308 return scalarizeVectorLoad(LN, DAG);
2309
Matt Arsenault8af47a02016-07-01 22:55:55 +00002310 SDValue Ops[2];
2311 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(LN, DAG);
2312 return DAG.getMergeValues(Ops, SDLoc(N));
2313 }
2314
2315 if (!IsFast)
2316 return SDValue();
2317 }
2318
2319 if (!shouldCombineMemoryType(VT))
2320 return SDValue();
2321
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002322 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
2323
2324 SDValue NewLoad
2325 = DAG.getLoad(NewVT, SL, LN->getChain(),
2326 LN->getBasePtr(), LN->getMemOperand());
2327
2328 SDValue BC = DAG.getNode(ISD::BITCAST, SL, VT, NewLoad);
2329 DCI.CombineTo(N, BC, NewLoad.getValue(1));
2330 return SDValue(N, 0);
2331}
2332
2333// Replace store of an illegal type with a store of a bitcast to a friendlier
2334// type.
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002335SDValue AMDGPUTargetLowering::performStoreCombine(SDNode *N,
2336 DAGCombinerInfo &DCI) const {
2337 if (!DCI.isBeforeLegalize())
2338 return SDValue();
2339
2340 StoreSDNode *SN = cast<StoreSDNode>(N);
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002341 if (SN->isVolatile() || !ISD::isNormalStore(SN))
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002342 return SDValue();
2343
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002344 EVT VT = SN->getMemoryVT();
Matt Arsenault8af47a02016-07-01 22:55:55 +00002345 unsigned Size = VT.getStoreSize();
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002346
2347 SDLoc SL(N);
2348 SelectionDAG &DAG = DCI.DAG;
Matt Arsenault8af47a02016-07-01 22:55:55 +00002349 unsigned Align = SN->getAlignment();
2350 if (Align < Size && isTypeLegal(VT)) {
2351 bool IsFast;
2352 unsigned AS = SN->getAddressSpace();
2353
2354 // Expand unaligned stores earlier than legalization. Due to visitation
2355 // order problems during legalization, the emitted instructions to pack and
2356 // unpack the bytes again are not eliminated in the case of an unaligned
2357 // copy.
Matt Arsenaultb50eb8d2016-08-31 21:52:27 +00002358 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) {
2359 if (VT.isVector())
2360 return scalarizeVectorStore(SN, DAG);
2361
Matt Arsenault8af47a02016-07-01 22:55:55 +00002362 return expandUnalignedStore(SN, DAG);
Matt Arsenaultb50eb8d2016-08-31 21:52:27 +00002363 }
Matt Arsenault8af47a02016-07-01 22:55:55 +00002364
2365 if (!IsFast)
2366 return SDValue();
2367 }
2368
2369 if (!shouldCombineMemoryType(VT))
2370 return SDValue();
2371
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002372 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
Matt Arsenault8af47a02016-07-01 22:55:55 +00002373 SDValue Val = SN->getValue();
2374
2375 //DCI.AddToWorklist(Val.getNode());
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002376
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002377 bool OtherUses = !Val.hasOneUse();
2378 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NewVT, Val);
2379 if (OtherUses) {
2380 SDValue CastBack = DAG.getNode(ISD::BITCAST, SL, VT, CastVal);
2381 DAG.ReplaceAllUsesOfValueWith(Val, CastBack);
2382 }
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002383
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002384 return DAG.getStore(SN->getChain(), SL, CastVal,
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002385 SN->getBasePtr(), SN->getMemOperand());
2386}
2387
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002388/// Split the 64-bit value \p LHS into two 32-bit components, and perform the
2389/// binary operation \p Opc to it with the corresponding constant operands.
2390SDValue AMDGPUTargetLowering::splitBinaryBitConstantOpImpl(
2391 DAGCombinerInfo &DCI, const SDLoc &SL,
2392 unsigned Opc, SDValue LHS,
2393 uint32_t ValLo, uint32_t ValHi) const {
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002394 SelectionDAG &DAG = DCI.DAG;
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002395 SDValue Lo, Hi;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002396 std::tie(Lo, Hi) = split64BitValue(LHS, DAG);
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002397
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002398 SDValue LoRHS = DAG.getConstant(ValLo, SL, MVT::i32);
2399 SDValue HiRHS = DAG.getConstant(ValHi, SL, MVT::i32);
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002400
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002401 SDValue LoAnd = DAG.getNode(Opc, SL, MVT::i32, Lo, LoRHS);
2402 SDValue HiAnd = DAG.getNode(Opc, SL, MVT::i32, Hi, HiRHS);
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002403
Matt Arsenaultefa3fe12016-04-22 22:48:38 +00002404 // Re-visit the ands. It's possible we eliminated one of them and it could
2405 // simplify the vector.
2406 DCI.AddToWorklist(Lo.getNode());
2407 DCI.AddToWorklist(Hi.getNode());
2408
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002409 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {LoAnd, HiAnd});
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002410 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2411}
2412
Matt Arsenault24692112015-07-14 18:20:33 +00002413SDValue AMDGPUTargetLowering::performShlCombine(SDNode *N,
2414 DAGCombinerInfo &DCI) const {
2415 if (N->getValueType(0) != MVT::i64)
2416 return SDValue();
2417
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002418 // i64 (shl x, C) -> (build_pair 0, (shl x, C -32))
Matt Arsenault24692112015-07-14 18:20:33 +00002419
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002420 // On some subtargets, 64-bit shift is a quarter rate instruction. In the
2421 // common case, splitting this into a move and a 32-bit shift is faster and
2422 // the same code size.
Matt Arsenault24692112015-07-14 18:20:33 +00002423 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002424 if (!RHS)
2425 return SDValue();
2426
2427 unsigned RHSVal = RHS->getZExtValue();
2428 if (RHSVal < 32)
Matt Arsenault24692112015-07-14 18:20:33 +00002429 return SDValue();
2430
2431 SDValue LHS = N->getOperand(0);
2432
2433 SDLoc SL(N);
2434 SelectionDAG &DAG = DCI.DAG;
2435
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002436 SDValue ShiftAmt = DAG.getConstant(RHSVal - 32, SL, MVT::i32);
2437
Matt Arsenault24692112015-07-14 18:20:33 +00002438 SDValue Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LHS);
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002439 SDValue NewShift = DAG.getNode(ISD::SHL, SL, MVT::i32, Lo, ShiftAmt);
Matt Arsenault24692112015-07-14 18:20:33 +00002440
2441 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
Matt Arsenault80edab92016-01-18 21:43:36 +00002442
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002443 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {Zero, NewShift});
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002444 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
Matt Arsenault24692112015-07-14 18:20:33 +00002445}
2446
Matt Arsenault33e3ece2016-01-18 22:09:04 +00002447SDValue AMDGPUTargetLowering::performSraCombine(SDNode *N,
2448 DAGCombinerInfo &DCI) const {
2449 if (N->getValueType(0) != MVT::i64)
2450 return SDValue();
2451
2452 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2453 if (!RHS)
2454 return SDValue();
2455
2456 SelectionDAG &DAG = DCI.DAG;
2457 SDLoc SL(N);
2458 unsigned RHSVal = RHS->getZExtValue();
2459
2460 // (sra i64:x, 32) -> build_pair x, (sra hi_32(x), 31)
2461 if (RHSVal == 32) {
2462 SDValue Hi = getHiHalf64(N->getOperand(0), DAG);
2463 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi,
2464 DAG.getConstant(31, SL, MVT::i32));
2465
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002466 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {Hi, NewShift});
Matt Arsenault33e3ece2016-01-18 22:09:04 +00002467 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec);
2468 }
2469
2470 // (sra i64:x, 63) -> build_pair (sra hi_32(x), 31), (sra hi_32(x), 31)
2471 if (RHSVal == 63) {
2472 SDValue Hi = getHiHalf64(N->getOperand(0), DAG);
2473 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi,
2474 DAG.getConstant(31, SL, MVT::i32));
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002475 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, NewShift});
Matt Arsenault33e3ece2016-01-18 22:09:04 +00002476 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec);
2477 }
2478
2479 return SDValue();
2480}
2481
Matt Arsenault80edab92016-01-18 21:43:36 +00002482SDValue AMDGPUTargetLowering::performSrlCombine(SDNode *N,
2483 DAGCombinerInfo &DCI) const {
2484 if (N->getValueType(0) != MVT::i64)
2485 return SDValue();
2486
2487 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2488 if (!RHS)
2489 return SDValue();
2490
2491 unsigned ShiftAmt = RHS->getZExtValue();
2492 if (ShiftAmt < 32)
2493 return SDValue();
2494
2495 // srl i64:x, C for C >= 32
2496 // =>
2497 // build_pair (srl hi_32(x), C - 32), 0
2498
2499 SelectionDAG &DAG = DCI.DAG;
2500 SDLoc SL(N);
2501
2502 SDValue One = DAG.getConstant(1, SL, MVT::i32);
2503 SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
2504
2505 SDValue VecOp = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, N->getOperand(0));
2506 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32,
2507 VecOp, One);
2508
2509 SDValue NewConst = DAG.getConstant(ShiftAmt - 32, SL, MVT::i32);
2510 SDValue NewShift = DAG.getNode(ISD::SRL, SL, MVT::i32, Hi, NewConst);
2511
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002512 SDValue BuildPair = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, Zero});
Matt Arsenault80edab92016-01-18 21:43:36 +00002513
2514 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildPair);
2515}
2516
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002517// We need to specifically handle i64 mul here to avoid unnecessary conversion
2518// instructions. If we only match on the legalized i64 mul expansion,
2519// SimplifyDemandedBits will be unable to remove them because there will be
2520// multiple uses due to the separate mul + mulh[su].
2521static SDValue getMul24(SelectionDAG &DAG, const SDLoc &SL,
2522 SDValue N0, SDValue N1, unsigned Size, bool Signed) {
2523 if (Size <= 32) {
2524 unsigned MulOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24;
2525 return DAG.getNode(MulOpc, SL, MVT::i32, N0, N1);
2526 }
2527
2528 // Because we want to eliminate extension instructions before the
2529 // operation, we need to create a single user here (i.e. not the separate
2530 // mul_lo + mul_hi) so that SimplifyDemandedBits will deal with it.
2531
2532 unsigned MulOpc = Signed ? AMDGPUISD::MUL_LOHI_I24 : AMDGPUISD::MUL_LOHI_U24;
2533
2534 SDValue Mul = DAG.getNode(MulOpc, SL,
2535 DAG.getVTList(MVT::i32, MVT::i32), N0, N1);
2536
2537 return DAG.getNode(ISD::BUILD_PAIR, SL, MVT::i64,
2538 Mul.getValue(0), Mul.getValue(1));
2539}
2540
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002541SDValue AMDGPUTargetLowering::performMulCombine(SDNode *N,
2542 DAGCombinerInfo &DCI) const {
2543 EVT VT = N->getValueType(0);
2544
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002545 unsigned Size = VT.getSizeInBits();
2546 if (VT.isVector() || Size > 64)
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002547 return SDValue();
2548
Tom Stellard115a6152016-11-10 16:02:37 +00002549 // There are i16 integer mul/mad.
2550 if (Subtarget->has16BitInsts() && VT.getScalarType().bitsLE(MVT::i16))
2551 return SDValue();
2552
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002553 SelectionDAG &DAG = DCI.DAG;
2554 SDLoc DL(N);
2555
2556 SDValue N0 = N->getOperand(0);
2557 SDValue N1 = N->getOperand(1);
2558 SDValue Mul;
2559
2560 if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) {
2561 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
2562 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002563 Mul = getMul24(DAG, DL, N0, N1, Size, false);
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002564 } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) {
2565 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
2566 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002567 Mul = getMul24(DAG, DL, N0, N1, Size, true);
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002568 } else {
2569 return SDValue();
2570 }
2571
2572 // We need to use sext even for MUL_U24, because MUL_U24 is used
2573 // for signed multiply of 8 and 16-bit types.
2574 return DAG.getSExtOrTrunc(Mul, DL, VT);
2575}
2576
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002577SDValue AMDGPUTargetLowering::performMulhsCombine(SDNode *N,
2578 DAGCombinerInfo &DCI) const {
2579 EVT VT = N->getValueType(0);
2580
2581 if (!Subtarget->hasMulI24() || VT.isVector())
2582 return SDValue();
2583
2584 SelectionDAG &DAG = DCI.DAG;
2585 SDLoc DL(N);
2586
2587 SDValue N0 = N->getOperand(0);
2588 SDValue N1 = N->getOperand(1);
2589
2590 if (!isI24(N0, DAG) || !isI24(N1, DAG))
2591 return SDValue();
2592
2593 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
2594 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
2595
2596 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_I24, DL, MVT::i32, N0, N1);
2597 DCI.AddToWorklist(Mulhi.getNode());
2598 return DAG.getSExtOrTrunc(Mulhi, DL, VT);
2599}
2600
2601SDValue AMDGPUTargetLowering::performMulhuCombine(SDNode *N,
2602 DAGCombinerInfo &DCI) const {
2603 EVT VT = N->getValueType(0);
2604
2605 if (!Subtarget->hasMulU24() || VT.isVector() || VT.getSizeInBits() > 32)
2606 return SDValue();
2607
2608 SelectionDAG &DAG = DCI.DAG;
2609 SDLoc DL(N);
2610
2611 SDValue N0 = N->getOperand(0);
2612 SDValue N1 = N->getOperand(1);
2613
2614 if (!isU24(N0, DAG) || !isU24(N1, DAG))
2615 return SDValue();
2616
2617 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
2618 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
2619
2620 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_U24, DL, MVT::i32, N0, N1);
2621 DCI.AddToWorklist(Mulhi.getNode());
2622 return DAG.getZExtOrTrunc(Mulhi, DL, VT);
2623}
2624
2625SDValue AMDGPUTargetLowering::performMulLoHi24Combine(
2626 SDNode *N, DAGCombinerInfo &DCI) const {
2627 SelectionDAG &DAG = DCI.DAG;
2628
Tom Stellard09c2bd62016-10-14 19:14:29 +00002629 // Simplify demanded bits before splitting into multiple users.
2630 if (simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI))
2631 return SDValue();
2632
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002633 SDValue N0 = N->getOperand(0);
2634 SDValue N1 = N->getOperand(1);
2635
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002636 bool Signed = (N->getOpcode() == AMDGPUISD::MUL_LOHI_I24);
2637
2638 unsigned MulLoOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24;
2639 unsigned MulHiOpc = Signed ? AMDGPUISD::MULHI_I24 : AMDGPUISD::MULHI_U24;
2640
2641 SDLoc SL(N);
2642
2643 SDValue MulLo = DAG.getNode(MulLoOpc, SL, MVT::i32, N0, N1);
2644 SDValue MulHi = DAG.getNode(MulHiOpc, SL, MVT::i32, N0, N1);
2645 return DAG.getMergeValues({ MulLo, MulHi }, SL);
2646}
2647
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002648static bool isNegativeOne(SDValue Val) {
2649 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val))
2650 return C->isAllOnesValue();
2651 return false;
2652}
2653
2654static bool isCtlzOpc(unsigned Opc) {
2655 return Opc == ISD::CTLZ || Opc == ISD::CTLZ_ZERO_UNDEF;
2656}
2657
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002658SDValue AMDGPUTargetLowering::getFFBH_U32(SelectionDAG &DAG,
2659 SDValue Op,
2660 const SDLoc &DL) const {
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002661 EVT VT = Op.getValueType();
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002662 EVT LegalVT = getTypeToTransformTo(*DAG.getContext(), VT);
2663 if (LegalVT != MVT::i32 && (Subtarget->has16BitInsts() &&
2664 LegalVT != MVT::i16))
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002665 return SDValue();
2666
2667 if (VT != MVT::i32)
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002668 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Op);
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002669
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002670 SDValue FFBH = DAG.getNode(AMDGPUISD::FFBH_U32, DL, MVT::i32, Op);
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002671 if (VT != MVT::i32)
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002672 FFBH = DAG.getNode(ISD::TRUNCATE, DL, VT, FFBH);
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002673
2674 return FFBH;
2675}
2676
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002677// The native instructions return -1 on 0 input. Optimize out a select that
2678// produces -1 on 0.
2679//
2680// TODO: If zero is not undef, we could also do this if the output is compared
2681// against the bitwidth.
2682//
2683// TODO: Should probably combine against FFBH_U32 instead of ctlz directly.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002684SDValue AMDGPUTargetLowering::performCtlzCombine(const SDLoc &SL, SDValue Cond,
2685 SDValue LHS, SDValue RHS,
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002686 DAGCombinerInfo &DCI) const {
2687 ConstantSDNode *CmpRhs = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2688 if (!CmpRhs || !CmpRhs->isNullValue())
2689 return SDValue();
2690
2691 SelectionDAG &DAG = DCI.DAG;
2692 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
2693 SDValue CmpLHS = Cond.getOperand(0);
2694
2695 // select (setcc x, 0, eq), -1, (ctlz_zero_undef x) -> ffbh_u32 x
2696 if (CCOpcode == ISD::SETEQ &&
2697 isCtlzOpc(RHS.getOpcode()) &&
2698 RHS.getOperand(0) == CmpLHS &&
2699 isNegativeOne(LHS)) {
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002700 return getFFBH_U32(DAG, CmpLHS, SL);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002701 }
2702
2703 // select (setcc x, 0, ne), (ctlz_zero_undef x), -1 -> ffbh_u32 x
2704 if (CCOpcode == ISD::SETNE &&
2705 isCtlzOpc(LHS.getOpcode()) &&
2706 LHS.getOperand(0) == CmpLHS &&
2707 isNegativeOne(RHS)) {
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002708 return getFFBH_U32(DAG, CmpLHS, SL);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002709 }
2710
2711 return SDValue();
2712}
2713
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002714static SDValue distributeOpThroughSelect(TargetLowering::DAGCombinerInfo &DCI,
2715 unsigned Op,
2716 const SDLoc &SL,
2717 SDValue Cond,
2718 SDValue N1,
2719 SDValue N2) {
2720 SelectionDAG &DAG = DCI.DAG;
2721 EVT VT = N1.getValueType();
2722
2723 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT, Cond,
2724 N1.getOperand(0), N2.getOperand(0));
2725 DCI.AddToWorklist(NewSelect.getNode());
2726 return DAG.getNode(Op, SL, VT, NewSelect);
2727}
2728
2729// Pull a free FP operation out of a select so it may fold into uses.
2730//
2731// select c, (fneg x), (fneg y) -> fneg (select c, x, y)
2732// select c, (fneg x), k -> fneg (select c, x, (fneg k))
2733//
2734// select c, (fabs x), (fabs y) -> fabs (select c, x, y)
2735// select c, (fabs x), +k -> fabs (select c, x, k)
2736static SDValue foldFreeOpFromSelect(TargetLowering::DAGCombinerInfo &DCI,
2737 SDValue N) {
2738 SelectionDAG &DAG = DCI.DAG;
2739 SDValue Cond = N.getOperand(0);
2740 SDValue LHS = N.getOperand(1);
2741 SDValue RHS = N.getOperand(2);
2742
2743 EVT VT = N.getValueType();
2744 if ((LHS.getOpcode() == ISD::FABS && RHS.getOpcode() == ISD::FABS) ||
2745 (LHS.getOpcode() == ISD::FNEG && RHS.getOpcode() == ISD::FNEG)) {
2746 return distributeOpThroughSelect(DCI, LHS.getOpcode(),
2747 SDLoc(N), Cond, LHS, RHS);
2748 }
2749
2750 bool Inv = false;
2751 if (RHS.getOpcode() == ISD::FABS || RHS.getOpcode() == ISD::FNEG) {
2752 std::swap(LHS, RHS);
2753 Inv = true;
2754 }
2755
2756 // TODO: Support vector constants.
2757 ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
2758 if ((LHS.getOpcode() == ISD::FNEG || LHS.getOpcode() == ISD::FABS) && CRHS) {
2759 SDLoc SL(N);
2760 // If one side is an fneg/fabs and the other is a constant, we can push the
2761 // fneg/fabs down. If it's an fabs, the constant needs to be non-negative.
2762 SDValue NewLHS = LHS.getOperand(0);
2763 SDValue NewRHS = RHS;
2764
Matt Arsenault45337df2017-01-12 18:58:15 +00002765 // Careful: if the neg can be folded up, don't try to pull it back down.
2766 bool ShouldFoldNeg = true;
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002767
Matt Arsenault45337df2017-01-12 18:58:15 +00002768 if (NewLHS.hasOneUse()) {
2769 unsigned Opc = NewLHS.getOpcode();
2770 if (LHS.getOpcode() == ISD::FNEG && fnegFoldsIntoOp(Opc))
2771 ShouldFoldNeg = false;
2772 if (LHS.getOpcode() == ISD::FABS && Opc == ISD::FMUL)
2773 ShouldFoldNeg = false;
2774 }
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002775
Matt Arsenault45337df2017-01-12 18:58:15 +00002776 if (ShouldFoldNeg) {
2777 if (LHS.getOpcode() == ISD::FNEG)
2778 NewRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2779 else if (CRHS->isNegative())
2780 return SDValue();
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002781
Matt Arsenault45337df2017-01-12 18:58:15 +00002782 if (Inv)
2783 std::swap(NewLHS, NewRHS);
2784
2785 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT,
2786 Cond, NewLHS, NewRHS);
2787 DCI.AddToWorklist(NewSelect.getNode());
2788 return DAG.getNode(LHS.getOpcode(), SL, VT, NewSelect);
2789 }
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002790 }
2791
2792 return SDValue();
2793}
2794
2795
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002796SDValue AMDGPUTargetLowering::performSelectCombine(SDNode *N,
2797 DAGCombinerInfo &DCI) const {
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002798 if (SDValue Folded = foldFreeOpFromSelect(DCI, SDValue(N, 0)))
2799 return Folded;
2800
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002801 SDValue Cond = N->getOperand(0);
2802 if (Cond.getOpcode() != ISD::SETCC)
2803 return SDValue();
2804
2805 EVT VT = N->getValueType(0);
2806 SDValue LHS = Cond.getOperand(0);
2807 SDValue RHS = Cond.getOperand(1);
2808 SDValue CC = Cond.getOperand(2);
2809
2810 SDValue True = N->getOperand(1);
2811 SDValue False = N->getOperand(2);
2812
Matt Arsenault0b26e472016-12-22 21:40:08 +00002813 if (Cond.hasOneUse()) { // TODO: Look for multiple select uses.
2814 SelectionDAG &DAG = DCI.DAG;
2815 if ((DAG.isConstantValueOfAnyType(True) ||
2816 DAG.isConstantValueOfAnyType(True)) &&
2817 (!DAG.isConstantValueOfAnyType(False) &&
2818 !DAG.isConstantValueOfAnyType(False))) {
2819 // Swap cmp + select pair to move constant to false input.
2820 // This will allow using VOPC cndmasks more often.
2821 // select (setcc x, y), k, x -> select (setcc y, x) x, x
2822
2823 SDLoc SL(N);
2824 ISD::CondCode NewCC = getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2825 LHS.getValueType().isInteger());
2826
2827 SDValue NewCond = DAG.getSetCC(SL, Cond.getValueType(), LHS, RHS, NewCC);
2828 return DAG.getNode(ISD::SELECT, SL, VT, NewCond, False, True);
2829 }
Matt Arsenault0b26e472016-12-22 21:40:08 +00002830
Matt Arsenaultda7a6562017-02-01 00:42:40 +00002831 if (VT == MVT::f32 && Subtarget->hasFminFmaxLegacy()) {
2832 SDValue MinMax
2833 = combineFMinMaxLegacy(SDLoc(N), VT, LHS, RHS, True, False, CC, DCI);
2834 // Revisit this node so we can catch min3/max3/med3 patterns.
2835 //DCI.AddToWorklist(MinMax.getNode());
2836 return MinMax;
2837 }
Matt Arsenault5b39b342016-01-28 20:53:48 +00002838 }
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002839
2840 // There's no reason to not do this if the condition has other uses.
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002841 return performCtlzCombine(SDLoc(N), Cond, True, False, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002842}
2843
Matt Arsenault2529fba2017-01-12 00:09:34 +00002844SDValue AMDGPUTargetLowering::performFNegCombine(SDNode *N,
2845 DAGCombinerInfo &DCI) const {
2846 SelectionDAG &DAG = DCI.DAG;
2847 SDValue N0 = N->getOperand(0);
2848 EVT VT = N->getValueType(0);
2849
2850 unsigned Opc = N0.getOpcode();
2851
2852 // If the input has multiple uses and we can either fold the negate down, or
2853 // the other uses cannot, give up. This both prevents unprofitable
2854 // transformations and infinite loops: we won't repeatedly try to fold around
2855 // a negate that has no 'good' form.
2856 //
2857 // TODO: Check users can fold
2858 if (fnegFoldsIntoOp(Opc) && !N0.hasOneUse())
2859 return SDValue();
2860
2861 SDLoc SL(N);
2862 switch (Opc) {
2863 case ISD::FADD: {
Matt Arsenault3e6f9b52017-01-19 06:35:27 +00002864 if (!mayIgnoreSignedZero(N0))
2865 return SDValue();
2866
Matt Arsenault2529fba2017-01-12 00:09:34 +00002867 // (fneg (fadd x, y)) -> (fadd (fneg x), (fneg y))
2868 SDValue LHS = N0.getOperand(0);
2869 SDValue RHS = N0.getOperand(1);
2870
2871 if (LHS.getOpcode() != ISD::FNEG)
2872 LHS = DAG.getNode(ISD::FNEG, SL, VT, LHS);
2873 else
2874 LHS = LHS.getOperand(0);
2875
2876 if (RHS.getOpcode() != ISD::FNEG)
2877 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2878 else
2879 RHS = RHS.getOperand(0);
2880
Matt Arsenault7b49ad72017-01-23 19:08:34 +00002881 SDValue Res = DAG.getNode(ISD::FADD, SL, VT, LHS, RHS, N0->getFlags());
Matt Arsenault2529fba2017-01-12 00:09:34 +00002882 if (!N0.hasOneUse())
2883 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
2884 return Res;
2885 }
Matt Arsenaulta8c325e2017-01-12 18:26:30 +00002886 case ISD::FMUL:
2887 case AMDGPUISD::FMUL_LEGACY: {
Matt Arsenault4103a812017-01-12 00:23:20 +00002888 // (fneg (fmul x, y)) -> (fmul x, (fneg y))
Matt Arsenaulta8c325e2017-01-12 18:26:30 +00002889 // (fneg (fmul_legacy x, y)) -> (fmul_legacy x, (fneg y))
Matt Arsenault4103a812017-01-12 00:23:20 +00002890 SDValue LHS = N0.getOperand(0);
2891 SDValue RHS = N0.getOperand(1);
2892
2893 if (LHS.getOpcode() == ISD::FNEG)
2894 LHS = LHS.getOperand(0);
2895 else if (RHS.getOpcode() == ISD::FNEG)
2896 RHS = RHS.getOperand(0);
2897 else
2898 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2899
Matt Arsenault7b49ad72017-01-23 19:08:34 +00002900 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, RHS, N0->getFlags());
Matt Arsenault4103a812017-01-12 00:23:20 +00002901 if (!N0.hasOneUse())
2902 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
2903 return Res;
2904 }
Matt Arsenault63f95372017-01-12 00:32:16 +00002905 case ISD::FMA:
2906 case ISD::FMAD: {
Matt Arsenault3e6f9b52017-01-19 06:35:27 +00002907 if (!mayIgnoreSignedZero(N0))
2908 return SDValue();
2909
Matt Arsenault63f95372017-01-12 00:32:16 +00002910 // (fneg (fma x, y, z)) -> (fma x, (fneg y), (fneg z))
2911 SDValue LHS = N0.getOperand(0);
2912 SDValue MHS = N0.getOperand(1);
2913 SDValue RHS = N0.getOperand(2);
2914
2915 if (LHS.getOpcode() == ISD::FNEG)
2916 LHS = LHS.getOperand(0);
2917 else if (MHS.getOpcode() == ISD::FNEG)
2918 MHS = MHS.getOperand(0);
2919 else
2920 MHS = DAG.getNode(ISD::FNEG, SL, VT, MHS);
2921
2922 if (RHS.getOpcode() != ISD::FNEG)
2923 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2924 else
2925 RHS = RHS.getOperand(0);
2926
2927 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, MHS, RHS);
2928 if (!N0.hasOneUse())
2929 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
2930 return Res;
2931 }
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00002932 case ISD::FP_EXTEND:
Matt Arsenault53f0cc22017-01-26 01:25:36 +00002933 case ISD::FTRUNC:
2934 case ISD::FRINT:
2935 case ISD::FNEARBYINT: // XXX - Should fround be handled?
2936 case ISD::FSIN:
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00002937 case AMDGPUISD::RCP:
Matt Arsenault31c039e2017-01-12 18:48:09 +00002938 case AMDGPUISD::RCP_LEGACY:
Matt Arsenault31c039e2017-01-12 18:48:09 +00002939 case AMDGPUISD::SIN_HW: {
Matt Arsenault98d2bf102017-01-12 17:46:28 +00002940 SDValue CvtSrc = N0.getOperand(0);
2941 if (CvtSrc.getOpcode() == ISD::FNEG) {
2942 // (fneg (fp_extend (fneg x))) -> (fp_extend x)
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00002943 // (fneg (rcp (fneg x))) -> (rcp x)
Matt Arsenault4242d482017-01-12 17:46:33 +00002944 return DAG.getNode(Opc, SL, VT, CvtSrc.getOperand(0));
Matt Arsenault98d2bf102017-01-12 17:46:28 +00002945 }
2946
2947 if (!N0.hasOneUse())
2948 return SDValue();
2949
2950 // (fneg (fp_extend x)) -> (fp_extend (fneg x))
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00002951 // (fneg (rcp x)) -> (rcp (fneg x))
Matt Arsenault98d2bf102017-01-12 17:46:28 +00002952 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc);
Matt Arsenault7b49ad72017-01-23 19:08:34 +00002953 return DAG.getNode(Opc, SL, VT, Neg, N0->getFlags());
Matt Arsenault4242d482017-01-12 17:46:33 +00002954 }
2955 case ISD::FP_ROUND: {
2956 SDValue CvtSrc = N0.getOperand(0);
2957
2958 if (CvtSrc.getOpcode() == ISD::FNEG) {
2959 // (fneg (fp_round (fneg x))) -> (fp_round x)
2960 return DAG.getNode(ISD::FP_ROUND, SL, VT,
2961 CvtSrc.getOperand(0), N0.getOperand(1));
2962 }
2963
2964 if (!N0.hasOneUse())
2965 return SDValue();
2966
2967 // (fneg (fp_round x)) -> (fp_round (fneg x))
2968 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc);
2969 return DAG.getNode(ISD::FP_ROUND, SL, VT, Neg, N0.getOperand(1));
Matt Arsenault98d2bf102017-01-12 17:46:28 +00002970 }
Matt Arsenault2529fba2017-01-12 00:09:34 +00002971 default:
2972 return SDValue();
2973 }
2974}
2975
Tom Stellard50122a52014-04-07 19:45:41 +00002976SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N,
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002977 DAGCombinerInfo &DCI) const {
Tom Stellard50122a52014-04-07 19:45:41 +00002978 SelectionDAG &DAG = DCI.DAG;
2979 SDLoc DL(N);
2980
2981 switch(N->getOpcode()) {
Matt Arsenault24e33d12015-07-03 23:33:38 +00002982 default:
2983 break;
Matt Arsenault79003342016-04-14 21:58:07 +00002984 case ISD::BITCAST: {
2985 EVT DestVT = N->getValueType(0);
Matt Arsenaultd99ef112016-09-17 15:44:16 +00002986
2987 // Push casts through vector builds. This helps avoid emitting a large
2988 // number of copies when materializing floating point vector constants.
2989 //
2990 // vNt1 bitcast (vNt0 (build_vector t0:x, t0:y)) =>
2991 // vnt1 = build_vector (t1 (bitcast t0:x)), (t1 (bitcast t0:y))
2992 if (DestVT.isVector()) {
2993 SDValue Src = N->getOperand(0);
2994 if (Src.getOpcode() == ISD::BUILD_VECTOR) {
2995 EVT SrcVT = Src.getValueType();
2996 unsigned NElts = DestVT.getVectorNumElements();
2997
2998 if (SrcVT.getVectorNumElements() == NElts) {
2999 EVT DestEltVT = DestVT.getVectorElementType();
3000
3001 SmallVector<SDValue, 8> CastedElts;
3002 SDLoc SL(N);
3003 for (unsigned I = 0, E = SrcVT.getVectorNumElements(); I != E; ++I) {
3004 SDValue Elt = Src.getOperand(I);
3005 CastedElts.push_back(DAG.getNode(ISD::BITCAST, DL, DestEltVT, Elt));
3006 }
3007
3008 return DAG.getBuildVector(DestVT, SL, CastedElts);
3009 }
3010 }
3011 }
3012
Matt Arsenault79003342016-04-14 21:58:07 +00003013 if (DestVT.getSizeInBits() != 64 && !DestVT.isVector())
3014 break;
3015
3016 // Fold bitcasts of constants.
3017 //
3018 // v2i32 (bitcast i64:k) -> build_vector lo_32(k), hi_32(k)
3019 // TODO: Generalize and move to DAGCombiner
3020 SDValue Src = N->getOperand(0);
3021 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src)) {
3022 assert(Src.getValueType() == MVT::i64);
3023 SDLoc SL(N);
3024 uint64_t CVal = C->getZExtValue();
3025 return DAG.getNode(ISD::BUILD_VECTOR, SL, DestVT,
3026 DAG.getConstant(Lo_32(CVal), SL, MVT::i32),
3027 DAG.getConstant(Hi_32(CVal), SL, MVT::i32));
3028 }
3029
3030 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Src)) {
3031 const APInt &Val = C->getValueAPF().bitcastToAPInt();
3032 SDLoc SL(N);
3033 uint64_t CVal = Val.getZExtValue();
3034 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
3035 DAG.getConstant(Lo_32(CVal), SL, MVT::i32),
3036 DAG.getConstant(Hi_32(CVal), SL, MVT::i32));
3037
3038 return DAG.getNode(ISD::BITCAST, SL, DestVT, Vec);
3039 }
3040
3041 break;
3042 }
Matt Arsenault24692112015-07-14 18:20:33 +00003043 case ISD::SHL: {
3044 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3045 break;
3046
3047 return performShlCombine(N, DCI);
3048 }
Matt Arsenault80edab92016-01-18 21:43:36 +00003049 case ISD::SRL: {
3050 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3051 break;
3052
3053 return performSrlCombine(N, DCI);
3054 }
Matt Arsenault33e3ece2016-01-18 22:09:04 +00003055 case ISD::SRA: {
3056 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3057 break;
3058
3059 return performSraCombine(N, DCI);
3060 }
Matt Arsenault24e33d12015-07-03 23:33:38 +00003061 case ISD::MUL:
3062 return performMulCombine(N, DCI);
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003063 case ISD::MULHS:
3064 return performMulhsCombine(N, DCI);
3065 case ISD::MULHU:
3066 return performMulhuCombine(N, DCI);
Matt Arsenault24e33d12015-07-03 23:33:38 +00003067 case AMDGPUISD::MUL_I24:
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003068 case AMDGPUISD::MUL_U24:
3069 case AMDGPUISD::MULHI_I24:
3070 case AMDGPUISD::MULHI_U24: {
Tom Stellard6c7dd982016-10-21 20:25:11 +00003071 // If the first call to simplify is successfull, then N may end up being
3072 // deleted, so we shouldn't call simplifyI24 again.
3073 simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI);
Matt Arsenault24e33d12015-07-03 23:33:38 +00003074 return SDValue();
3075 }
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003076 case AMDGPUISD::MUL_LOHI_I24:
3077 case AMDGPUISD::MUL_LOHI_U24:
3078 return performMulLoHi24Combine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00003079 case ISD::SELECT:
3080 return performSelectCombine(N, DCI);
Matt Arsenault2529fba2017-01-12 00:09:34 +00003081 case ISD::FNEG:
3082 return performFNegCombine(N, DCI);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003083 case AMDGPUISD::BFE_I32:
3084 case AMDGPUISD::BFE_U32: {
3085 assert(!N->getValueType(0).isVector() &&
3086 "Vector handling of BFE not implemented");
3087 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
3088 if (!Width)
3089 break;
3090
3091 uint32_t WidthVal = Width->getZExtValue() & 0x1f;
3092 if (WidthVal == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003093 return DAG.getConstant(0, DL, MVT::i32);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003094
3095 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
3096 if (!Offset)
3097 break;
3098
3099 SDValue BitsFrom = N->getOperand(0);
3100 uint32_t OffsetVal = Offset->getZExtValue() & 0x1f;
3101
3102 bool Signed = N->getOpcode() == AMDGPUISD::BFE_I32;
3103
3104 if (OffsetVal == 0) {
3105 // This is already sign / zero extended, so try to fold away extra BFEs.
3106 unsigned SignBits = Signed ? (32 - WidthVal + 1) : (32 - WidthVal);
3107
3108 unsigned OpSignBits = DAG.ComputeNumSignBits(BitsFrom);
3109 if (OpSignBits >= SignBits)
3110 return BitsFrom;
Matt Arsenault05e96f42014-05-22 18:09:12 +00003111
3112 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), WidthVal);
3113 if (Signed) {
3114 // This is a sign_extend_inreg. Replace it to take advantage of existing
3115 // DAG Combines. If not eliminated, we will match back to BFE during
3116 // selection.
3117
3118 // TODO: The sext_inreg of extended types ends, although we can could
3119 // handle them in a single BFE.
3120 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, BitsFrom,
3121 DAG.getValueType(SmallVT));
3122 }
3123
3124 return DAG.getZeroExtendInReg(BitsFrom, DL, SmallVT);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003125 }
3126
Matt Arsenaultf1794202014-10-15 05:07:00 +00003127 if (ConstantSDNode *CVal = dyn_cast<ConstantSDNode>(BitsFrom)) {
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003128 if (Signed) {
3129 return constantFoldBFE<int32_t>(DAG,
Matt Arsenault46cbc432014-09-19 00:42:06 +00003130 CVal->getSExtValue(),
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003131 OffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003132 WidthVal,
3133 DL);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003134 }
3135
3136 return constantFoldBFE<uint32_t>(DAG,
Matt Arsenault6462f942014-09-18 15:52:26 +00003137 CVal->getZExtValue(),
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003138 OffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003139 WidthVal,
3140 DL);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003141 }
3142
Matt Arsenault05e96f42014-05-22 18:09:12 +00003143 if ((OffsetVal + WidthVal) >= 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003144 SDValue ShiftVal = DAG.getConstant(OffsetVal, DL, MVT::i32);
Matt Arsenault05e96f42014-05-22 18:09:12 +00003145 return DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, MVT::i32,
3146 BitsFrom, ShiftVal);
3147 }
3148
Matt Arsenault7b68fdf2014-10-15 17:58:34 +00003149 if (BitsFrom.hasOneUse()) {
Matt Arsenault6de7af42014-10-15 23:37:42 +00003150 APInt Demanded = APInt::getBitsSet(32,
3151 OffsetVal,
3152 OffsetVal + WidthVal);
3153
Matt Arsenault7b68fdf2014-10-15 17:58:34 +00003154 APInt KnownZero, KnownOne;
3155 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
3156 !DCI.isBeforeLegalizeOps());
3157 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3158 if (TLO.ShrinkDemandedConstant(BitsFrom, Demanded) ||
3159 TLI.SimplifyDemandedBits(BitsFrom, Demanded,
3160 KnownZero, KnownOne, TLO)) {
3161 DCI.CommitTargetLoweringOpt(TLO);
3162 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003163 }
3164
3165 break;
3166 }
Matt Arsenault327bb5a2016-07-01 22:47:50 +00003167 case ISD::LOAD:
3168 return performLoadCombine(N, DCI);
Matt Arsenaultca3976f2014-07-15 02:06:31 +00003169 case ISD::STORE:
3170 return performStoreCombine(N, DCI);
Tom Stellard50122a52014-04-07 19:45:41 +00003171 }
3172 return SDValue();
3173}
3174
3175//===----------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +00003176// Helper functions
3177//===----------------------------------------------------------------------===//
3178
Tom Stellard75aadc22012-12-11 21:25:42 +00003179SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
3180 const TargetRegisterClass *RC,
3181 unsigned Reg, EVT VT) const {
3182 MachineFunction &MF = DAG.getMachineFunction();
3183 MachineRegisterInfo &MRI = MF.getRegInfo();
3184 unsigned VirtualRegister;
3185 if (!MRI.isLiveIn(Reg)) {
3186 VirtualRegister = MRI.createVirtualRegister(RC);
3187 MRI.addLiveIn(Reg, VirtualRegister);
3188 } else {
3189 VirtualRegister = MRI.getLiveInVirtReg(Reg);
3190 }
3191 return DAG.getRegister(VirtualRegister, VT);
3192}
3193
Tom Stellarddcb9f092015-07-09 21:20:37 +00003194uint32_t AMDGPUTargetLowering::getImplicitParameterOffset(
3195 const AMDGPUMachineFunction *MFI, const ImplicitParameter Param) const {
Tom Stellardb2869eb2016-09-09 19:28:00 +00003196 unsigned Alignment = Subtarget->getAlignmentForImplicitArgPtr();
3197 uint64_t ArgOffset = alignTo(MFI->getABIArgOffset(), Alignment);
Tom Stellarddcb9f092015-07-09 21:20:37 +00003198 switch (Param) {
3199 case GRID_DIM:
3200 return ArgOffset;
3201 case GRID_OFFSET:
3202 return ArgOffset + 4;
3203 }
3204 llvm_unreachable("unexpected implicit parameter type");
3205}
3206
Tom Stellard75aadc22012-12-11 21:25:42 +00003207#define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
3208
3209const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00003210 switch ((AMDGPUISD::NodeType)Opcode) {
3211 case AMDGPUISD::FIRST_NUMBER: break;
Tom Stellard75aadc22012-12-11 21:25:42 +00003212 // AMDIL DAG nodes
Tom Stellard75aadc22012-12-11 21:25:42 +00003213 NODE_NAME_CASE(CALL);
3214 NODE_NAME_CASE(UMUL);
Tom Stellard75aadc22012-12-11 21:25:42 +00003215 NODE_NAME_CASE(BRANCH_COND);
3216
3217 // AMDGPU DAG nodes
Matt Arsenault9babdf42016-06-22 20:15:28 +00003218 NODE_NAME_CASE(ENDPGM)
3219 NODE_NAME_CASE(RETURN)
Tom Stellard75aadc22012-12-11 21:25:42 +00003220 NODE_NAME_CASE(DWORDADDR)
3221 NODE_NAME_CASE(FRACT)
Wei Ding07e03712016-07-28 16:42:13 +00003222 NODE_NAME_CASE(SETCC)
Tom Stellard8485fa02016-12-07 02:42:15 +00003223 NODE_NAME_CASE(SETREG)
3224 NODE_NAME_CASE(FMA_W_CHAIN)
3225 NODE_NAME_CASE(FMUL_W_CHAIN)
Matt Arsenault5d47d4a2014-06-12 21:15:44 +00003226 NODE_NAME_CASE(CLAMP)
Matthias Braund04893f2015-05-07 21:33:59 +00003227 NODE_NAME_CASE(COS_HW)
3228 NODE_NAME_CASE(SIN_HW)
Matt Arsenaultda59f3d2014-11-13 23:03:09 +00003229 NODE_NAME_CASE(FMAX_LEGACY)
Matt Arsenaultda59f3d2014-11-13 23:03:09 +00003230 NODE_NAME_CASE(FMIN_LEGACY)
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003231 NODE_NAME_CASE(FMAX3)
3232 NODE_NAME_CASE(SMAX3)
3233 NODE_NAME_CASE(UMAX3)
3234 NODE_NAME_CASE(FMIN3)
3235 NODE_NAME_CASE(SMIN3)
3236 NODE_NAME_CASE(UMIN3)
Matt Arsenaultf639c322016-01-28 20:53:42 +00003237 NODE_NAME_CASE(FMED3)
3238 NODE_NAME_CASE(SMED3)
3239 NODE_NAME_CASE(UMED3)
Matt Arsenaulta0050b02014-06-19 01:19:19 +00003240 NODE_NAME_CASE(URECIP)
3241 NODE_NAME_CASE(DIV_SCALE)
3242 NODE_NAME_CASE(DIV_FMAS)
3243 NODE_NAME_CASE(DIV_FIXUP)
3244 NODE_NAME_CASE(TRIG_PREOP)
3245 NODE_NAME_CASE(RCP)
3246 NODE_NAME_CASE(RSQ)
Matt Arsenault32fc5272016-07-26 16:45:45 +00003247 NODE_NAME_CASE(RCP_LEGACY)
Matt Arsenault257d48d2014-06-24 22:13:39 +00003248 NODE_NAME_CASE(RSQ_LEGACY)
Matt Arsenault32fc5272016-07-26 16:45:45 +00003249 NODE_NAME_CASE(FMUL_LEGACY)
Matt Arsenault79963e82016-02-13 01:03:00 +00003250 NODE_NAME_CASE(RSQ_CLAMP)
Matt Arsenault2e7cc482014-08-15 17:30:25 +00003251 NODE_NAME_CASE(LDEXP)
Matt Arsenault4831ce52015-01-06 23:00:37 +00003252 NODE_NAME_CASE(FP_CLASS)
Matt Arsenaulta0050b02014-06-19 01:19:19 +00003253 NODE_NAME_CASE(DOT4)
Matthias Braund04893f2015-05-07 21:33:59 +00003254 NODE_NAME_CASE(CARRY)
3255 NODE_NAME_CASE(BORROW)
Matt Arsenaultfae02982014-03-17 18:58:11 +00003256 NODE_NAME_CASE(BFE_U32)
3257 NODE_NAME_CASE(BFE_I32)
Matt Arsenaultb3458362014-03-31 18:21:13 +00003258 NODE_NAME_CASE(BFI)
3259 NODE_NAME_CASE(BFM)
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00003260 NODE_NAME_CASE(FFBH_U32)
Matt Arsenaultb51dcb92016-07-18 18:40:51 +00003261 NODE_NAME_CASE(FFBH_I32)
Tom Stellard50122a52014-04-07 19:45:41 +00003262 NODE_NAME_CASE(MUL_U24)
3263 NODE_NAME_CASE(MUL_I24)
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003264 NODE_NAME_CASE(MULHI_U24)
3265 NODE_NAME_CASE(MULHI_I24)
3266 NODE_NAME_CASE(MUL_LOHI_U24)
3267 NODE_NAME_CASE(MUL_LOHI_I24)
Matt Arsenaulteb260202014-05-22 18:00:15 +00003268 NODE_NAME_CASE(MAD_U24)
3269 NODE_NAME_CASE(MAD_I24)
Matthias Braund04893f2015-05-07 21:33:59 +00003270 NODE_NAME_CASE(TEXTURE_FETCH)
Tom Stellard75aadc22012-12-11 21:25:42 +00003271 NODE_NAME_CASE(EXPORT)
Matt Arsenault7bee6ac2016-12-05 20:23:10 +00003272 NODE_NAME_CASE(EXPORT_DONE)
3273 NODE_NAME_CASE(R600_EXPORT)
Tom Stellardff62c352013-01-23 02:09:03 +00003274 NODE_NAME_CASE(CONST_ADDRESS)
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00003275 NODE_NAME_CASE(REGISTER_LOAD)
3276 NODE_NAME_CASE(REGISTER_STORE)
Tom Stellard9fa17912013-08-14 23:24:45 +00003277 NODE_NAME_CASE(LOAD_INPUT)
3278 NODE_NAME_CASE(SAMPLE)
3279 NODE_NAME_CASE(SAMPLEB)
3280 NODE_NAME_CASE(SAMPLED)
3281 NODE_NAME_CASE(SAMPLEL)
Matt Arsenault364a6742014-06-11 17:50:44 +00003282 NODE_NAME_CASE(CVT_F32_UBYTE0)
3283 NODE_NAME_CASE(CVT_F32_UBYTE1)
3284 NODE_NAME_CASE(CVT_F32_UBYTE2)
3285 NODE_NAME_CASE(CVT_F32_UBYTE3)
Tom Stellard880a80a2014-06-17 16:53:14 +00003286 NODE_NAME_CASE(BUILD_VERTICAL_VECTOR)
Tom Stellard067c8152014-07-21 14:01:14 +00003287 NODE_NAME_CASE(CONST_DATA_PTR)
Tom Stellardbf3e6e52016-06-14 20:29:59 +00003288 NODE_NAME_CASE(PC_ADD_REL_OFFSET)
Matt Arsenault03006fd2016-07-19 16:27:56 +00003289 NODE_NAME_CASE(KILL)
Jan Veselyf1705042017-01-20 21:24:26 +00003290 NODE_NAME_CASE(DUMMY_CHAIN)
Matthias Braund04893f2015-05-07 21:33:59 +00003291 case AMDGPUISD::FIRST_MEM_OPCODE_NUMBER: break;
Tom Stellardfc92e772015-05-12 14:18:14 +00003292 NODE_NAME_CASE(SENDMSG)
Jan Veselyd48445d2017-01-04 18:06:55 +00003293 NODE_NAME_CASE(SENDMSGHALT)
Tom Stellard2a9d9472015-05-12 15:00:46 +00003294 NODE_NAME_CASE(INTERP_MOV)
3295 NODE_NAME_CASE(INTERP_P1)
3296 NODE_NAME_CASE(INTERP_P2)
Tom Stellardd3ee8c12013-08-16 01:12:06 +00003297 NODE_NAME_CASE(STORE_MSKOR)
Matt Arsenaultdfaf4262016-04-25 19:27:09 +00003298 NODE_NAME_CASE(LOAD_CONSTANT)
Tom Stellardafcf12f2013-09-12 02:55:14 +00003299 NODE_NAME_CASE(TBUFFER_STORE_FORMAT)
Tom Stellard354a43c2016-04-01 18:27:37 +00003300 NODE_NAME_CASE(ATOMIC_CMP_SWAP)
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00003301 NODE_NAME_CASE(ATOMIC_INC)
3302 NODE_NAME_CASE(ATOMIC_DEC)
Tom Stellard6f9ef142016-12-20 17:19:44 +00003303 NODE_NAME_CASE(BUFFER_LOAD)
3304 NODE_NAME_CASE(BUFFER_LOAD_FORMAT)
Matthias Braund04893f2015-05-07 21:33:59 +00003305 case AMDGPUISD::LAST_AMDGPU_ISD_NUMBER: break;
Tom Stellard75aadc22012-12-11 21:25:42 +00003306 }
Matthias Braund04893f2015-05-07 21:33:59 +00003307 return nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +00003308}
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003309
Evandro Menezes21f9ce12016-11-10 23:31:06 +00003310SDValue AMDGPUTargetLowering::getSqrtEstimate(SDValue Operand,
3311 SelectionDAG &DAG, int Enabled,
3312 int &RefinementSteps,
3313 bool &UseOneConstNR,
3314 bool Reciprocal) const {
Matt Arsenaulte93d06a2015-01-13 20:53:18 +00003315 EVT VT = Operand.getValueType();
3316
3317 if (VT == MVT::f32) {
3318 RefinementSteps = 0;
3319 return DAG.getNode(AMDGPUISD::RSQ, SDLoc(Operand), VT, Operand);
3320 }
3321
3322 // TODO: There is also f64 rsq instruction, but the documentation is less
3323 // clear on its precision.
3324
3325 return SDValue();
3326}
3327
Matt Arsenaultbf0db912015-01-13 20:53:23 +00003328SDValue AMDGPUTargetLowering::getRecipEstimate(SDValue Operand,
Sanjay Patel0051efc2016-10-20 16:55:45 +00003329 SelectionDAG &DAG, int Enabled,
3330 int &RefinementSteps) const {
Matt Arsenaultbf0db912015-01-13 20:53:23 +00003331 EVT VT = Operand.getValueType();
3332
3333 if (VT == MVT::f32) {
3334 // Reciprocal, < 1 ulp error.
3335 //
3336 // This reciprocal approximation converges to < 0.5 ulp error with one
3337 // newton rhapson performed with two fused multiple adds (FMAs).
3338
3339 RefinementSteps = 0;
3340 return DAG.getNode(AMDGPUISD::RCP, SDLoc(Operand), VT, Operand);
3341 }
3342
3343 // TODO: There is also f64 rcp instruction, but the documentation is less
3344 // clear on its precision.
3345
3346 return SDValue();
3347}
3348
Jay Foada0653a32014-05-14 21:14:37 +00003349void AMDGPUTargetLowering::computeKnownBitsForTargetNode(
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003350 const SDValue Op,
3351 APInt &KnownZero,
3352 APInt &KnownOne,
3353 const SelectionDAG &DAG,
3354 unsigned Depth) const {
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003355
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003356 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0); // Don't know anything.
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003357
3358 APInt KnownZero2;
3359 APInt KnownOne2;
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003360 unsigned Opc = Op.getOpcode();
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003361
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003362 switch (Opc) {
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003363 default:
3364 break;
Jan Vesely808fff52015-04-30 17:15:56 +00003365 case AMDGPUISD::CARRY:
3366 case AMDGPUISD::BORROW: {
3367 KnownZero = APInt::getHighBitsSet(32, 31);
3368 break;
3369 }
3370
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003371 case AMDGPUISD::BFE_I32:
3372 case AMDGPUISD::BFE_U32: {
3373 ConstantSDNode *CWidth = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3374 if (!CWidth)
3375 return;
3376
3377 unsigned BitWidth = 32;
3378 uint32_t Width = CWidth->getZExtValue() & 0x1f;
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003379
Matt Arsenaulta3fe7c62014-10-16 20:07:40 +00003380 if (Opc == AMDGPUISD::BFE_U32)
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003381 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - Width);
3382
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003383 break;
3384 }
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003385 }
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003386}
Matt Arsenaultbf8694d2014-05-22 18:09:03 +00003387
3388unsigned AMDGPUTargetLowering::ComputeNumSignBitsForTargetNode(
3389 SDValue Op,
3390 const SelectionDAG &DAG,
3391 unsigned Depth) const {
3392 switch (Op.getOpcode()) {
3393 case AMDGPUISD::BFE_I32: {
3394 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3395 if (!Width)
3396 return 1;
3397
3398 unsigned SignBits = 32 - Width->getZExtValue() + 1;
Artyom Skrobov314ee042015-11-25 19:41:11 +00003399 if (!isNullConstant(Op.getOperand(1)))
Matt Arsenaultbf8694d2014-05-22 18:09:03 +00003400 return SignBits;
3401
3402 // TODO: Could probably figure something out with non-0 offsets.
3403 unsigned Op0SignBits = DAG.ComputeNumSignBits(Op.getOperand(0), Depth + 1);
3404 return std::max(SignBits, Op0SignBits);
3405 }
3406
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003407 case AMDGPUISD::BFE_U32: {
3408 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3409 return Width ? 32 - (Width->getZExtValue() & 0x1f) : 1;
3410 }
3411
Jan Vesely808fff52015-04-30 17:15:56 +00003412 case AMDGPUISD::CARRY:
3413 case AMDGPUISD::BORROW:
3414 return 31;
3415
Matt Arsenaultbf8694d2014-05-22 18:09:03 +00003416 default:
3417 return 1;
3418 }
3419}