blob: e7f15bcb0818b05741ac5e5803cd86b920610fe6 [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
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000975SDValue AMDGPUTargetLowering::CombineFMinMaxLegacy(const SDLoc &DL, EVT VT,
976 SDValue LHS, SDValue RHS,
977 SDValue True, SDValue False,
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +0000978 SDValue CC,
979 DAGCombinerInfo &DCI) const {
980 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
981 return SDValue();
982
Matt Arsenaultda59f3d2014-11-13 23:03:09 +0000983 if (!(LHS == True && RHS == False) && !(LHS == False && RHS == True))
984 return SDValue();
Tom Stellard75aadc22012-12-11 21:25:42 +0000985
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +0000986 SelectionDAG &DAG = DCI.DAG;
Tom Stellard75aadc22012-12-11 21:25:42 +0000987 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
988 switch (CCOpcode) {
989 case ISD::SETOEQ:
990 case ISD::SETONE:
991 case ISD::SETUNE:
992 case ISD::SETNE:
993 case ISD::SETUEQ:
994 case ISD::SETEQ:
995 case ISD::SETFALSE:
996 case ISD::SETFALSE2:
997 case ISD::SETTRUE:
998 case ISD::SETTRUE2:
999 case ISD::SETUO:
1000 case ISD::SETO:
Matt Arsenaultda59f3d2014-11-13 23:03:09 +00001001 break;
Tom Stellard75aadc22012-12-11 21:25:42 +00001002 case ISD::SETULE:
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001003 case ISD::SETULT: {
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001004 if (LHS == True)
1005 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS);
1006 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS);
1007 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001008 case ISD::SETOLE:
1009 case ISD::SETOLT:
1010 case ISD::SETLE:
1011 case ISD::SETLT: {
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001012 // Ordered. Assume ordered for undefined.
1013
1014 // Only do this after legalization to avoid interfering with other combines
1015 // which might occur.
1016 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG &&
1017 !DCI.isCalledByLegalizer())
1018 return SDValue();
Marek Olsakbe047802014-12-07 12:19:03 +00001019
Matt Arsenault36094d72014-11-15 05:02:57 +00001020 // We need to permute the operands to get the correct NaN behavior. The
1021 // selected operand is the second one based on the failing compare with NaN,
1022 // so permute it based on the compare type the hardware uses.
1023 if (LHS == True)
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001024 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS);
1025 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS);
Tom Stellard75aadc22012-12-11 21:25:42 +00001026 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001027 case ISD::SETUGE:
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001028 case ISD::SETUGT: {
Matt Arsenault36094d72014-11-15 05:02:57 +00001029 if (LHS == True)
1030 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS);
1031 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS);
Tom Stellard75aadc22012-12-11 21:25:42 +00001032 }
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001033 case ISD::SETGT:
1034 case ISD::SETGE:
1035 case ISD::SETOGE:
1036 case ISD::SETOGT: {
1037 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG &&
1038 !DCI.isCalledByLegalizer())
1039 return SDValue();
1040
1041 if (LHS == True)
1042 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS);
1043 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS);
1044 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001045 case ISD::SETCC_INVALID:
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001046 llvm_unreachable("Invalid setcc condcode!");
Tom Stellard75aadc22012-12-11 21:25:42 +00001047 }
Tom Stellardafa8b532014-05-09 16:42:16 +00001048 return SDValue();
Tom Stellard75aadc22012-12-11 21:25:42 +00001049}
1050
Matt Arsenault6e3a4512016-01-18 22:01:13 +00001051std::pair<SDValue, SDValue>
1052AMDGPUTargetLowering::split64BitValue(SDValue Op, SelectionDAG &DAG) const {
1053 SDLoc SL(Op);
1054
1055 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1056
1057 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1058 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1059
1060 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1061 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1062
1063 return std::make_pair(Lo, Hi);
1064}
1065
Matt Arsenault33e3ece2016-01-18 22:09:04 +00001066SDValue AMDGPUTargetLowering::getLoHalf64(SDValue Op, SelectionDAG &DAG) const {
1067 SDLoc SL(Op);
1068
1069 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1070 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1071 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1072}
1073
1074SDValue AMDGPUTargetLowering::getHiHalf64(SDValue Op, SelectionDAG &DAG) const {
1075 SDLoc SL(Op);
1076
1077 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1078 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1079 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1080}
1081
Matt Arsenault83e60582014-07-24 17:10:35 +00001082SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue Op,
1083 SelectionDAG &DAG) const {
Matt Arsenault9c499c32016-04-14 23:31:26 +00001084 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault83e60582014-07-24 17:10:35 +00001085 EVT VT = Op.getValueType();
1086
Matt Arsenault9c499c32016-04-14 23:31:26 +00001087
Matt Arsenault83e60582014-07-24 17:10:35 +00001088 // If this is a 2 element vector, we really want to scalarize and not create
1089 // weird 1 element vectors.
1090 if (VT.getVectorNumElements() == 2)
Matt Arsenault9c499c32016-04-14 23:31:26 +00001091 return scalarizeVectorLoad(Load, DAG);
Matt Arsenault83e60582014-07-24 17:10:35 +00001092
Matt Arsenault83e60582014-07-24 17:10:35 +00001093 SDValue BasePtr = Load->getBasePtr();
1094 EVT PtrVT = BasePtr.getValueType();
1095 EVT MemVT = Load->getMemoryVT();
1096 SDLoc SL(Op);
Matt Arsenault52a52a52015-12-14 16:59:40 +00001097
1098 const MachinePointerInfo &SrcValue = Load->getMemOperand()->getPointerInfo();
Matt Arsenault83e60582014-07-24 17:10:35 +00001099
1100 EVT LoVT, HiVT;
1101 EVT LoMemVT, HiMemVT;
1102 SDValue Lo, Hi;
1103
1104 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
1105 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT);
1106 std::tie(Lo, Hi) = DAG.SplitVector(Op, SL, LoVT, HiVT);
Matt Arsenault52a52a52015-12-14 16:59:40 +00001107
1108 unsigned Size = LoMemVT.getStoreSize();
1109 unsigned BaseAlign = Load->getAlignment();
1110 unsigned HiAlign = MinAlign(BaseAlign, Size);
1111
Justin Lebar9c375812016-07-15 18:27:10 +00001112 SDValue LoLoad = DAG.getExtLoad(Load->getExtensionType(), SL, LoVT,
1113 Load->getChain(), BasePtr, SrcValue, LoMemVT,
1114 BaseAlign, Load->getMemOperand()->getFlags());
Matt Arsenault83e60582014-07-24 17:10:35 +00001115 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
Matt Arsenault52a52a52015-12-14 16:59:40 +00001116 DAG.getConstant(Size, SL, PtrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00001117 SDValue HiLoad =
1118 DAG.getExtLoad(Load->getExtensionType(), SL, HiVT, Load->getChain(),
1119 HiPtr, SrcValue.getWithOffset(LoMemVT.getStoreSize()),
1120 HiMemVT, HiAlign, Load->getMemOperand()->getFlags());
Matt Arsenault83e60582014-07-24 17:10:35 +00001121
1122 SDValue Ops[] = {
1123 DAG.getNode(ISD::CONCAT_VECTORS, SL, VT, LoLoad, HiLoad),
1124 DAG.getNode(ISD::TokenFactor, SL, MVT::Other,
1125 LoLoad.getValue(1), HiLoad.getValue(1))
1126 };
1127
1128 return DAG.getMergeValues(Ops, SL);
1129}
1130
Matt Arsenault83e60582014-07-24 17:10:35 +00001131SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op,
1132 SelectionDAG &DAG) const {
1133 StoreSDNode *Store = cast<StoreSDNode>(Op);
1134 SDValue Val = Store->getValue();
1135 EVT VT = Val.getValueType();
1136
1137 // If this is a 2 element vector, we really want to scalarize and not create
1138 // weird 1 element vectors.
1139 if (VT.getVectorNumElements() == 2)
Matt Arsenault9c499c32016-04-14 23:31:26 +00001140 return scalarizeVectorStore(Store, DAG);
Matt Arsenault83e60582014-07-24 17:10:35 +00001141
1142 EVT MemVT = Store->getMemoryVT();
1143 SDValue Chain = Store->getChain();
1144 SDValue BasePtr = Store->getBasePtr();
1145 SDLoc SL(Op);
1146
1147 EVT LoVT, HiVT;
1148 EVT LoMemVT, HiMemVT;
1149 SDValue Lo, Hi;
1150
1151 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
1152 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT);
1153 std::tie(Lo, Hi) = DAG.SplitVector(Val, SL, LoVT, HiVT);
1154
1155 EVT PtrVT = BasePtr.getValueType();
1156 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001157 DAG.getConstant(LoMemVT.getStoreSize(), SL,
1158 PtrVT));
Matt Arsenault83e60582014-07-24 17:10:35 +00001159
Matt Arsenault52a52a52015-12-14 16:59:40 +00001160 const MachinePointerInfo &SrcValue = Store->getMemOperand()->getPointerInfo();
1161 unsigned BaseAlign = Store->getAlignment();
1162 unsigned Size = LoMemVT.getStoreSize();
1163 unsigned HiAlign = MinAlign(BaseAlign, Size);
1164
Justin Lebar9c375812016-07-15 18:27:10 +00001165 SDValue LoStore =
1166 DAG.getTruncStore(Chain, SL, Lo, BasePtr, SrcValue, LoMemVT, BaseAlign,
1167 Store->getMemOperand()->getFlags());
1168 SDValue HiStore =
1169 DAG.getTruncStore(Chain, SL, Hi, HiPtr, SrcValue.getWithOffset(Size),
1170 HiMemVT, HiAlign, Store->getMemOperand()->getFlags());
Matt Arsenault83e60582014-07-24 17:10:35 +00001171
1172 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, LoStore, HiStore);
1173}
1174
Matt Arsenault0daeb632014-07-24 06:59:20 +00001175// This is a shortcut for integer division because we have fast i32<->f32
1176// conversions, and fast f32 reciprocal instructions. The fractional part of a
Matt Arsenault81a70952016-05-21 01:53:33 +00001177// float is enough to accurately represent up to a 24-bit signed integer.
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001178SDValue AMDGPUTargetLowering::LowerDIVREM24(SDValue Op, SelectionDAG &DAG,
1179 bool Sign) const {
Matt Arsenault1578aa72014-06-15 20:08:02 +00001180 SDLoc DL(Op);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001181 EVT VT = Op.getValueType();
Matt Arsenault1578aa72014-06-15 20:08:02 +00001182 SDValue LHS = Op.getOperand(0);
1183 SDValue RHS = Op.getOperand(1);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001184 MVT IntVT = MVT::i32;
1185 MVT FltVT = MVT::f32;
1186
Matt Arsenault81a70952016-05-21 01:53:33 +00001187 unsigned LHSSignBits = DAG.ComputeNumSignBits(LHS);
1188 if (LHSSignBits < 9)
1189 return SDValue();
1190
1191 unsigned RHSSignBits = DAG.ComputeNumSignBits(RHS);
1192 if (RHSSignBits < 9)
1193 return SDValue();
Jan Veselye5ca27d2014-08-12 17:31:20 +00001194
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001195 unsigned BitSize = VT.getSizeInBits();
Matt Arsenault81a70952016-05-21 01:53:33 +00001196 unsigned SignBits = std::min(LHSSignBits, RHSSignBits);
1197 unsigned DivBits = BitSize - SignBits;
1198 if (Sign)
1199 ++DivBits;
1200
1201 ISD::NodeType ToFp = Sign ? ISD::SINT_TO_FP : ISD::UINT_TO_FP;
1202 ISD::NodeType ToInt = Sign ? ISD::FP_TO_SINT : ISD::FP_TO_UINT;
Matt Arsenault0daeb632014-07-24 06:59:20 +00001203
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001204 SDValue jq = DAG.getConstant(1, DL, IntVT);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001205
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001206 if (Sign) {
Jan Veselye5ca27d2014-08-12 17:31:20 +00001207 // char|short jq = ia ^ ib;
1208 jq = DAG.getNode(ISD::XOR, DL, VT, LHS, RHS);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001209
Jan Veselye5ca27d2014-08-12 17:31:20 +00001210 // jq = jq >> (bitsize - 2)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001211 jq = DAG.getNode(ISD::SRA, DL, VT, jq,
1212 DAG.getConstant(BitSize - 2, DL, VT));
Matt Arsenault1578aa72014-06-15 20:08:02 +00001213
Jan Veselye5ca27d2014-08-12 17:31:20 +00001214 // jq = jq | 0x1
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001215 jq = DAG.getNode(ISD::OR, DL, VT, jq, DAG.getConstant(1, DL, VT));
Jan Veselye5ca27d2014-08-12 17:31:20 +00001216 }
Matt Arsenault1578aa72014-06-15 20:08:02 +00001217
1218 // int ia = (int)LHS;
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001219 SDValue ia = LHS;
Matt Arsenault1578aa72014-06-15 20:08:02 +00001220
1221 // int ib, (int)RHS;
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001222 SDValue ib = RHS;
Matt Arsenault1578aa72014-06-15 20:08:02 +00001223
1224 // float fa = (float)ia;
Jan Veselye5ca27d2014-08-12 17:31:20 +00001225 SDValue fa = DAG.getNode(ToFp, DL, FltVT, ia);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001226
1227 // float fb = (float)ib;
Jan Veselye5ca27d2014-08-12 17:31:20 +00001228 SDValue fb = DAG.getNode(ToFp, DL, FltVT, ib);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001229
Matt Arsenault0daeb632014-07-24 06:59:20 +00001230 SDValue fq = DAG.getNode(ISD::FMUL, DL, FltVT,
1231 fa, DAG.getNode(AMDGPUISD::RCP, DL, FltVT, fb));
Matt Arsenault1578aa72014-06-15 20:08:02 +00001232
1233 // fq = trunc(fq);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001234 fq = DAG.getNode(ISD::FTRUNC, DL, FltVT, fq);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001235
1236 // float fqneg = -fq;
Matt Arsenault0daeb632014-07-24 06:59:20 +00001237 SDValue fqneg = DAG.getNode(ISD::FNEG, DL, FltVT, fq);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001238
1239 // float fr = mad(fqneg, fb, fa);
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001240 SDValue fr = DAG.getNode(ISD::FMAD, DL, FltVT, fqneg, fb, fa);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001241
1242 // int iq = (int)fq;
Jan Veselye5ca27d2014-08-12 17:31:20 +00001243 SDValue iq = DAG.getNode(ToInt, DL, IntVT, fq);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001244
1245 // fr = fabs(fr);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001246 fr = DAG.getNode(ISD::FABS, DL, FltVT, fr);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001247
1248 // fb = fabs(fb);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001249 fb = DAG.getNode(ISD::FABS, DL, FltVT, fb);
1250
Mehdi Amini44ede332015-07-09 02:09:04 +00001251 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001252
1253 // int cv = fr >= fb;
Matt Arsenault0daeb632014-07-24 06:59:20 +00001254 SDValue cv = DAG.getSetCC(DL, SetCCVT, fr, fb, ISD::SETOGE);
1255
Matt Arsenault1578aa72014-06-15 20:08:02 +00001256 // jq = (cv ? jq : 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001257 jq = DAG.getNode(ISD::SELECT, DL, VT, cv, jq, DAG.getConstant(0, DL, VT));
Matt Arsenault0daeb632014-07-24 06:59:20 +00001258
Jan Veselye5ca27d2014-08-12 17:31:20 +00001259 // dst = iq + jq;
Jan Vesely4a33bc62014-08-12 17:31:17 +00001260 SDValue Div = DAG.getNode(ISD::ADD, DL, VT, iq, jq);
1261
Jan Veselye5ca27d2014-08-12 17:31:20 +00001262 // Rem needs compensation, it's easier to recompute it
Jan Vesely4a33bc62014-08-12 17:31:17 +00001263 SDValue Rem = DAG.getNode(ISD::MUL, DL, VT, Div, RHS);
1264 Rem = DAG.getNode(ISD::SUB, DL, VT, LHS, Rem);
1265
Matt Arsenault81a70952016-05-21 01:53:33 +00001266 // Truncate to number of bits this divide really is.
1267 if (Sign) {
1268 SDValue InRegSize
1269 = DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), DivBits));
1270 Div = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Div, InRegSize);
1271 Rem = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Rem, InRegSize);
1272 } else {
1273 SDValue TruncMask = DAG.getConstant((UINT64_C(1) << DivBits) - 1, DL, VT);
1274 Div = DAG.getNode(ISD::AND, DL, VT, Div, TruncMask);
1275 Rem = DAG.getNode(ISD::AND, DL, VT, Rem, TruncMask);
1276 }
1277
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001278 return DAG.getMergeValues({ Div, Rem }, DL);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001279}
1280
Tom Stellardbf69d762014-11-15 01:07:53 +00001281void AMDGPUTargetLowering::LowerUDIVREM64(SDValue Op,
1282 SelectionDAG &DAG,
1283 SmallVectorImpl<SDValue> &Results) const {
1284 assert(Op.getValueType() == MVT::i64);
1285
1286 SDLoc DL(Op);
1287 EVT VT = Op.getValueType();
1288 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
1289
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001290 SDValue one = DAG.getConstant(1, DL, HalfVT);
1291 SDValue zero = DAG.getConstant(0, DL, HalfVT);
Tom Stellardbf69d762014-11-15 01:07:53 +00001292
1293 //HiLo split
1294 SDValue LHS = Op.getOperand(0);
1295 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, zero);
1296 SDValue LHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, one);
1297
1298 SDValue RHS = Op.getOperand(1);
1299 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, zero);
1300 SDValue RHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, one);
1301
Jan Vesely5f715d32015-01-22 23:42:43 +00001302 if (VT == MVT::i64 &&
1303 DAG.MaskedValueIsZero(RHS, APInt::getHighBitsSet(64, 32)) &&
1304 DAG.MaskedValueIsZero(LHS, APInt::getHighBitsSet(64, 32))) {
1305
1306 SDValue Res = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(HalfVT, HalfVT),
1307 LHS_Lo, RHS_Lo);
1308
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001309 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(0), zero});
1310 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(1), zero});
Matt Arsenaultd275fca2016-03-01 05:06:05 +00001311
1312 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV));
1313 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM));
Jan Vesely5f715d32015-01-22 23:42:43 +00001314 return;
1315 }
1316
Tom Stellardbf69d762014-11-15 01:07:53 +00001317 // Get Speculative values
1318 SDValue DIV_Part = DAG.getNode(ISD::UDIV, DL, HalfVT, LHS_Hi, RHS_Lo);
1319 SDValue REM_Part = DAG.getNode(ISD::UREM, DL, HalfVT, LHS_Hi, RHS_Lo);
1320
Tom Stellardbf69d762014-11-15 01:07:53 +00001321 SDValue REM_Lo = DAG.getSelectCC(DL, RHS_Hi, zero, REM_Part, LHS_Hi, ISD::SETEQ);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001322 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {REM_Lo, zero});
Matt Arsenaultd275fca2016-03-01 05:06:05 +00001323 REM = DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM);
Tom Stellardbf69d762014-11-15 01:07:53 +00001324
1325 SDValue DIV_Hi = DAG.getSelectCC(DL, RHS_Hi, zero, DIV_Part, zero, ISD::SETEQ);
1326 SDValue DIV_Lo = zero;
1327
1328 const unsigned halfBitWidth = HalfVT.getSizeInBits();
1329
1330 for (unsigned i = 0; i < halfBitWidth; ++i) {
Jan Veselyf7987ca2015-01-22 23:42:39 +00001331 const unsigned bitPos = halfBitWidth - i - 1;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001332 SDValue POS = DAG.getConstant(bitPos, DL, HalfVT);
Jan Veselyf7987ca2015-01-22 23:42:39 +00001333 // Get value of high bit
Jan Vesely811ef522015-04-12 23:45:01 +00001334 SDValue HBit = DAG.getNode(ISD::SRL, DL, HalfVT, LHS_Lo, POS);
1335 HBit = DAG.getNode(ISD::AND, DL, HalfVT, HBit, one);
Jan Veselyf7987ca2015-01-22 23:42:39 +00001336 HBit = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, HBit);
Tom Stellardbf69d762014-11-15 01:07:53 +00001337
Jan Veselyf7987ca2015-01-22 23:42:39 +00001338 // Shift
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001339 REM = DAG.getNode(ISD::SHL, DL, VT, REM, DAG.getConstant(1, DL, VT));
Jan Veselyf7987ca2015-01-22 23:42:39 +00001340 // Add LHS high bit
1341 REM = DAG.getNode(ISD::OR, DL, VT, REM, HBit);
Tom Stellardbf69d762014-11-15 01:07:53 +00001342
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +00001343 SDValue BIT = DAG.getConstant(1ULL << bitPos, DL, HalfVT);
Tom Stellard83171b32014-11-15 01:07:57 +00001344 SDValue realBIT = DAG.getSelectCC(DL, REM, RHS, BIT, zero, ISD::SETUGE);
Tom Stellardbf69d762014-11-15 01:07:53 +00001345
1346 DIV_Lo = DAG.getNode(ISD::OR, DL, HalfVT, DIV_Lo, realBIT);
1347
1348 // Update REM
Tom Stellardbf69d762014-11-15 01:07:53 +00001349 SDValue REM_sub = DAG.getNode(ISD::SUB, DL, VT, REM, RHS);
Tom Stellard83171b32014-11-15 01:07:57 +00001350 REM = DAG.getSelectCC(DL, REM, RHS, REM_sub, REM, ISD::SETUGE);
Tom Stellardbf69d762014-11-15 01:07:53 +00001351 }
1352
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001353 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {DIV_Lo, DIV_Hi});
Matt Arsenaultd275fca2016-03-01 05:06:05 +00001354 DIV = DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV);
Tom Stellardbf69d762014-11-15 01:07:53 +00001355 Results.push_back(DIV);
1356 Results.push_back(REM);
1357}
1358
Tom Stellard75aadc22012-12-11 21:25:42 +00001359SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
Matt Arsenault46013d92014-05-11 21:24:41 +00001360 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001361 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +00001362 EVT VT = Op.getValueType();
1363
Tom Stellardbf69d762014-11-15 01:07:53 +00001364 if (VT == MVT::i64) {
1365 SmallVector<SDValue, 2> Results;
1366 LowerUDIVREM64(Op, DAG, Results);
1367 return DAG.getMergeValues(Results, DL);
1368 }
1369
Matt Arsenault81a70952016-05-21 01:53:33 +00001370 if (VT == MVT::i32) {
1371 if (SDValue Res = LowerDIVREM24(Op, DAG, false))
1372 return Res;
1373 }
1374
Tom Stellard75aadc22012-12-11 21:25:42 +00001375 SDValue Num = Op.getOperand(0);
1376 SDValue Den = Op.getOperand(1);
1377
Tom Stellard75aadc22012-12-11 21:25:42 +00001378 // RCP = URECIP(Den) = 2^32 / Den + e
1379 // e is rounding error.
1380 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
1381
Tom Stellard4349b192014-09-22 15:35:30 +00001382 // RCP_LO = mul(RCP, Den) */
1383 SDValue RCP_LO = DAG.getNode(ISD::MUL, DL, VT, RCP, Den);
Tom Stellard75aadc22012-12-11 21:25:42 +00001384
1385 // RCP_HI = mulhu (RCP, Den) */
1386 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
1387
1388 // NEG_RCP_LO = -RCP_LO
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001389 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001390 RCP_LO);
1391
1392 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001393 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001394 NEG_RCP_LO, RCP_LO,
1395 ISD::SETEQ);
1396 // Calculate the rounding error from the URECIP instruction
1397 // E = mulhu(ABS_RCP_LO, RCP)
1398 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
1399
1400 // RCP_A_E = RCP + E
1401 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
1402
1403 // RCP_S_E = RCP - E
1404 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
1405
1406 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001407 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001408 RCP_A_E, RCP_S_E,
1409 ISD::SETEQ);
1410 // Quotient = mulhu(Tmp0, Num)
1411 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
1412
1413 // Num_S_Remainder = Quotient * Den
Tom Stellard4349b192014-09-22 15:35:30 +00001414 SDValue Num_S_Remainder = DAG.getNode(ISD::MUL, DL, VT, Quotient, Den);
Tom Stellard75aadc22012-12-11 21:25:42 +00001415
1416 // Remainder = Num - Num_S_Remainder
1417 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
1418
1419 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
1420 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001421 DAG.getConstant(-1, DL, VT),
1422 DAG.getConstant(0, DL, VT),
Vincent Lejeune4f3751f2013-11-06 17:36:04 +00001423 ISD::SETUGE);
1424 // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0)
1425 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Num,
1426 Num_S_Remainder,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001427 DAG.getConstant(-1, DL, VT),
1428 DAG.getConstant(0, DL, VT),
Vincent Lejeune4f3751f2013-11-06 17:36:04 +00001429 ISD::SETUGE);
Tom Stellard75aadc22012-12-11 21:25:42 +00001430 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
1431 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
1432 Remainder_GE_Zero);
1433
1434 // Calculate Division result:
1435
1436 // Quotient_A_One = Quotient + 1
1437 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001438 DAG.getConstant(1, DL, VT));
Tom Stellard75aadc22012-12-11 21:25:42 +00001439
1440 // Quotient_S_One = Quotient - 1
1441 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001442 DAG.getConstant(1, DL, VT));
Tom Stellard75aadc22012-12-11 21:25:42 +00001443
1444 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001445 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001446 Quotient, Quotient_A_One, ISD::SETEQ);
1447
1448 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001449 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001450 Quotient_S_One, Div, ISD::SETEQ);
1451
1452 // Calculate Rem result:
1453
1454 // Remainder_S_Den = Remainder - Den
1455 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
1456
1457 // Remainder_A_Den = Remainder + Den
1458 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
1459
1460 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001461 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001462 Remainder, Remainder_S_Den, ISD::SETEQ);
1463
1464 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001465 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001466 Remainder_A_Den, Rem, ISD::SETEQ);
Matt Arsenault7939acd2014-04-07 16:44:24 +00001467 SDValue Ops[2] = {
1468 Div,
1469 Rem
1470 };
Craig Topper64941d92014-04-27 19:20:57 +00001471 return DAG.getMergeValues(Ops, DL);
Tom Stellard75aadc22012-12-11 21:25:42 +00001472}
1473
Jan Vesely109efdf2014-06-22 21:43:00 +00001474SDValue AMDGPUTargetLowering::LowerSDIVREM(SDValue Op,
1475 SelectionDAG &DAG) const {
1476 SDLoc DL(Op);
1477 EVT VT = Op.getValueType();
1478
Jan Vesely109efdf2014-06-22 21:43:00 +00001479 SDValue LHS = Op.getOperand(0);
1480 SDValue RHS = Op.getOperand(1);
1481
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001482 SDValue Zero = DAG.getConstant(0, DL, VT);
1483 SDValue NegOne = DAG.getConstant(-1, DL, VT);
Jan Vesely4a33bc62014-08-12 17:31:17 +00001484
Matt Arsenault81a70952016-05-21 01:53:33 +00001485 if (VT == MVT::i32) {
1486 if (SDValue Res = LowerDIVREM24(Op, DAG, true))
1487 return Res;
Jan Vesely5f715d32015-01-22 23:42:43 +00001488 }
Matt Arsenault81a70952016-05-21 01:53:33 +00001489
Jan Vesely5f715d32015-01-22 23:42:43 +00001490 if (VT == MVT::i64 &&
1491 DAG.ComputeNumSignBits(LHS) > 32 &&
1492 DAG.ComputeNumSignBits(RHS) > 32) {
1493 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
1494
1495 //HiLo split
1496 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, Zero);
1497 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, Zero);
1498 SDValue DIVREM = DAG.getNode(ISD::SDIVREM, DL, DAG.getVTList(HalfVT, HalfVT),
1499 LHS_Lo, RHS_Lo);
1500 SDValue Res[2] = {
1501 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(0)),
1502 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(1))
1503 };
1504 return DAG.getMergeValues(Res, DL);
1505 }
1506
Jan Vesely109efdf2014-06-22 21:43:00 +00001507 SDValue LHSign = DAG.getSelectCC(DL, LHS, Zero, NegOne, Zero, ISD::SETLT);
1508 SDValue RHSign = DAG.getSelectCC(DL, RHS, Zero, NegOne, Zero, ISD::SETLT);
1509 SDValue DSign = DAG.getNode(ISD::XOR, DL, VT, LHSign, RHSign);
1510 SDValue RSign = LHSign; // Remainder sign is the same as LHS
1511
1512 LHS = DAG.getNode(ISD::ADD, DL, VT, LHS, LHSign);
1513 RHS = DAG.getNode(ISD::ADD, DL, VT, RHS, RHSign);
1514
1515 LHS = DAG.getNode(ISD::XOR, DL, VT, LHS, LHSign);
1516 RHS = DAG.getNode(ISD::XOR, DL, VT, RHS, RHSign);
1517
1518 SDValue Div = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT), LHS, RHS);
1519 SDValue Rem = Div.getValue(1);
1520
1521 Div = DAG.getNode(ISD::XOR, DL, VT, Div, DSign);
1522 Rem = DAG.getNode(ISD::XOR, DL, VT, Rem, RSign);
1523
1524 Div = DAG.getNode(ISD::SUB, DL, VT, Div, DSign);
1525 Rem = DAG.getNode(ISD::SUB, DL, VT, Rem, RSign);
1526
1527 SDValue Res[2] = {
1528 Div,
1529 Rem
1530 };
1531 return DAG.getMergeValues(Res, DL);
1532}
1533
Matt Arsenault16e31332014-09-10 21:44:27 +00001534// (frem x, y) -> (fsub x, (fmul (ftrunc (fdiv x, y)), y))
1535SDValue AMDGPUTargetLowering::LowerFREM(SDValue Op, SelectionDAG &DAG) const {
1536 SDLoc SL(Op);
1537 EVT VT = Op.getValueType();
1538 SDValue X = Op.getOperand(0);
1539 SDValue Y = Op.getOperand(1);
1540
Sanjay Patela2607012015-09-16 16:31:21 +00001541 // TODO: Should this propagate fast-math-flags?
1542
Matt Arsenault16e31332014-09-10 21:44:27 +00001543 SDValue Div = DAG.getNode(ISD::FDIV, SL, VT, X, Y);
1544 SDValue Floor = DAG.getNode(ISD::FTRUNC, SL, VT, Div);
1545 SDValue Mul = DAG.getNode(ISD::FMUL, SL, VT, Floor, Y);
1546
1547 return DAG.getNode(ISD::FSUB, SL, VT, X, Mul);
1548}
1549
Matt Arsenault46010932014-06-18 17:05:30 +00001550SDValue AMDGPUTargetLowering::LowerFCEIL(SDValue Op, SelectionDAG &DAG) const {
1551 SDLoc SL(Op);
1552 SDValue Src = Op.getOperand(0);
1553
1554 // result = trunc(src)
1555 // if (src > 0.0 && src != result)
1556 // result += 1.0
1557
1558 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1559
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001560 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64);
1561 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001562
Mehdi Amini44ede332015-07-09 02:09:04 +00001563 EVT SetCCVT =
1564 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001565
1566 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOGT);
1567 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1568 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1569
1570 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, One, Zero);
Sanjay Patela2607012015-09-16 16:31:21 +00001571 // TODO: Should this propagate fast-math-flags?
Matt Arsenault46010932014-06-18 17:05:30 +00001572 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1573}
1574
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001575static SDValue extractF64Exponent(SDValue Hi, const SDLoc &SL,
1576 SelectionDAG &DAG) {
Matt Arsenaultb0055482015-01-21 18:18:25 +00001577 const unsigned FractBits = 52;
1578 const unsigned ExpBits = 11;
1579
1580 SDValue ExpPart = DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32,
1581 Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001582 DAG.getConstant(FractBits - 32, SL, MVT::i32),
1583 DAG.getConstant(ExpBits, SL, MVT::i32));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001584 SDValue Exp = DAG.getNode(ISD::SUB, SL, MVT::i32, ExpPart,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001585 DAG.getConstant(1023, SL, MVT::i32));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001586
1587 return Exp;
1588}
1589
Matt Arsenault46010932014-06-18 17:05:30 +00001590SDValue AMDGPUTargetLowering::LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const {
1591 SDLoc SL(Op);
1592 SDValue Src = Op.getOperand(0);
1593
1594 assert(Op.getValueType() == MVT::f64);
1595
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001596 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1597 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001598
1599 SDValue VecSrc = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1600
1601 // Extract the upper half, since this is where we will find the sign and
1602 // exponent.
1603 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, VecSrc, One);
1604
Matt Arsenaultb0055482015-01-21 18:18:25 +00001605 SDValue Exp = extractF64Exponent(Hi, SL, DAG);
Matt Arsenault46010932014-06-18 17:05:30 +00001606
Matt Arsenaultb0055482015-01-21 18:18:25 +00001607 const unsigned FractBits = 52;
Matt Arsenault46010932014-06-18 17:05:30 +00001608
1609 // Extract the sign bit.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001610 const SDValue SignBitMask = DAG.getConstant(UINT32_C(1) << 31, SL, MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001611 SDValue SignBit = DAG.getNode(ISD::AND, SL, MVT::i32, Hi, SignBitMask);
1612
1613 // Extend back to to 64-bits.
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001614 SDValue SignBit64 = DAG.getBuildVector(MVT::v2i32, SL, {Zero, SignBit});
Matt Arsenault46010932014-06-18 17:05:30 +00001615 SignBit64 = DAG.getNode(ISD::BITCAST, SL, MVT::i64, SignBit64);
1616
1617 SDValue BcInt = DAG.getNode(ISD::BITCAST, SL, MVT::i64, Src);
Matt Arsenault2b0fa432014-06-18 22:11:03 +00001618 const SDValue FractMask
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001619 = DAG.getConstant((UINT64_C(1) << FractBits) - 1, SL, MVT::i64);
Matt Arsenault46010932014-06-18 17:05:30 +00001620
1621 SDValue Shr = DAG.getNode(ISD::SRA, SL, MVT::i64, FractMask, Exp);
1622 SDValue Not = DAG.getNOT(SL, Shr, MVT::i64);
1623 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, BcInt, Not);
1624
Mehdi Amini44ede332015-07-09 02:09:04 +00001625 EVT SetCCVT =
1626 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001627
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001628 const SDValue FiftyOne = DAG.getConstant(FractBits - 1, SL, MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001629
1630 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
1631 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
1632
1633 SDValue Tmp1 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpLt0, SignBit64, Tmp0);
1634 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpGt51, BcInt, Tmp1);
1635
1636 return DAG.getNode(ISD::BITCAST, SL, MVT::f64, Tmp2);
1637}
1638
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001639SDValue AMDGPUTargetLowering::LowerFRINT(SDValue Op, SelectionDAG &DAG) const {
1640 SDLoc SL(Op);
1641 SDValue Src = Op.getOperand(0);
1642
1643 assert(Op.getValueType() == MVT::f64);
1644
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001645 APFloat C1Val(APFloat::IEEEdouble(), "0x1.0p+52");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001646 SDValue C1 = DAG.getConstantFP(C1Val, SL, MVT::f64);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001647 SDValue CopySign = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, C1, Src);
1648
Sanjay Patela2607012015-09-16 16:31:21 +00001649 // TODO: Should this propagate fast-math-flags?
1650
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001651 SDValue Tmp1 = DAG.getNode(ISD::FADD, SL, MVT::f64, Src, CopySign);
1652 SDValue Tmp2 = DAG.getNode(ISD::FSUB, SL, MVT::f64, Tmp1, CopySign);
1653
1654 SDValue Fabs = DAG.getNode(ISD::FABS, SL, MVT::f64, Src);
Matt Arsenaultd22626f2014-06-18 17:45:58 +00001655
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001656 APFloat C2Val(APFloat::IEEEdouble(), "0x1.fffffffffffffp+51");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001657 SDValue C2 = DAG.getConstantFP(C2Val, SL, MVT::f64);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001658
Mehdi Amini44ede332015-07-09 02:09:04 +00001659 EVT SetCCVT =
1660 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001661 SDValue Cond = DAG.getSetCC(SL, SetCCVT, Fabs, C2, ISD::SETOGT);
1662
1663 return DAG.getSelect(SL, MVT::f64, Cond, Src, Tmp2);
1664}
1665
Matt Arsenault692bd5e2014-06-18 22:03:45 +00001666SDValue AMDGPUTargetLowering::LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const {
1667 // FNEARBYINT and FRINT are the same, except in their handling of FP
1668 // exceptions. Those aren't really meaningful for us, and OpenCL only has
1669 // rint, so just treat them as equivalent.
1670 return DAG.getNode(ISD::FRINT, SDLoc(Op), Op.getValueType(), Op.getOperand(0));
1671}
1672
Matt Arsenaultb0055482015-01-21 18:18:25 +00001673// XXX - May require not supporting f32 denormals?
1674SDValue AMDGPUTargetLowering::LowerFROUND32(SDValue Op, SelectionDAG &DAG) const {
1675 SDLoc SL(Op);
1676 SDValue X = Op.getOperand(0);
1677
1678 SDValue T = DAG.getNode(ISD::FTRUNC, SL, MVT::f32, X);
1679
Sanjay Patela2607012015-09-16 16:31:21 +00001680 // TODO: Should this propagate fast-math-flags?
1681
Matt Arsenaultb0055482015-01-21 18:18:25 +00001682 SDValue Diff = DAG.getNode(ISD::FSUB, SL, MVT::f32, X, T);
1683
1684 SDValue AbsDiff = DAG.getNode(ISD::FABS, SL, MVT::f32, Diff);
1685
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001686 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f32);
1687 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
1688 const SDValue Half = DAG.getConstantFP(0.5, SL, MVT::f32);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001689
1690 SDValue SignOne = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f32, One, X);
1691
Mehdi Amini44ede332015-07-09 02:09:04 +00001692 EVT SetCCVT =
1693 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001694
1695 SDValue Cmp = DAG.getSetCC(SL, SetCCVT, AbsDiff, Half, ISD::SETOGE);
1696
1697 SDValue Sel = DAG.getNode(ISD::SELECT, SL, MVT::f32, Cmp, SignOne, Zero);
1698
1699 return DAG.getNode(ISD::FADD, SL, MVT::f32, T, Sel);
1700}
1701
1702SDValue AMDGPUTargetLowering::LowerFROUND64(SDValue Op, SelectionDAG &DAG) const {
1703 SDLoc SL(Op);
1704 SDValue X = Op.getOperand(0);
1705
1706 SDValue L = DAG.getNode(ISD::BITCAST, SL, MVT::i64, X);
1707
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001708 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1709 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1710 const SDValue NegOne = DAG.getConstant(-1, SL, MVT::i32);
1711 const SDValue FiftyOne = DAG.getConstant(51, SL, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00001712 EVT SetCCVT =
1713 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001714
1715 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1716
1717 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, One);
1718
1719 SDValue Exp = extractF64Exponent(Hi, SL, DAG);
1720
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001721 const SDValue Mask = DAG.getConstant(INT64_C(0x000fffffffffffff), SL,
1722 MVT::i64);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001723
1724 SDValue M = DAG.getNode(ISD::SRA, SL, MVT::i64, Mask, Exp);
1725 SDValue D = DAG.getNode(ISD::SRA, SL, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001726 DAG.getConstant(INT64_C(0x0008000000000000), SL,
1727 MVT::i64),
Matt Arsenaultb0055482015-01-21 18:18:25 +00001728 Exp);
1729
1730 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, L, M);
1731 SDValue Tmp1 = DAG.getSetCC(SL, SetCCVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001732 DAG.getConstant(0, SL, MVT::i64), Tmp0,
Matt Arsenaultb0055482015-01-21 18:18:25 +00001733 ISD::SETNE);
1734
1735 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, Tmp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001736 D, DAG.getConstant(0, SL, MVT::i64));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001737 SDValue K = DAG.getNode(ISD::ADD, SL, MVT::i64, L, Tmp2);
1738
1739 K = DAG.getNode(ISD::AND, SL, MVT::i64, K, DAG.getNOT(SL, M, MVT::i64));
1740 K = DAG.getNode(ISD::BITCAST, SL, MVT::f64, K);
1741
1742 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
1743 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
1744 SDValue ExpEqNegOne = DAG.getSetCC(SL, SetCCVT, NegOne, Exp, ISD::SETEQ);
1745
1746 SDValue Mag = DAG.getNode(ISD::SELECT, SL, MVT::f64,
1747 ExpEqNegOne,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001748 DAG.getConstantFP(1.0, SL, MVT::f64),
1749 DAG.getConstantFP(0.0, SL, MVT::f64));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001750
1751 SDValue S = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, Mag, X);
1752
1753 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpLt0, S, K);
1754 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpGt51, X, K);
1755
1756 return K;
1757}
1758
1759SDValue AMDGPUTargetLowering::LowerFROUND(SDValue Op, SelectionDAG &DAG) const {
1760 EVT VT = Op.getValueType();
1761
1762 if (VT == MVT::f32)
1763 return LowerFROUND32(Op, DAG);
1764
1765 if (VT == MVT::f64)
1766 return LowerFROUND64(Op, DAG);
1767
1768 llvm_unreachable("unhandled type");
1769}
1770
Matt Arsenault46010932014-06-18 17:05:30 +00001771SDValue AMDGPUTargetLowering::LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const {
1772 SDLoc SL(Op);
1773 SDValue Src = Op.getOperand(0);
1774
1775 // result = trunc(src);
1776 // if (src < 0.0 && src != result)
1777 // result += -1.0.
1778
1779 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1780
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001781 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64);
1782 const SDValue NegOne = DAG.getConstantFP(-1.0, SL, MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001783
Mehdi Amini44ede332015-07-09 02:09:04 +00001784 EVT SetCCVT =
1785 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001786
1787 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOLT);
1788 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1789 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1790
1791 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, NegOne, Zero);
Sanjay Patela2607012015-09-16 16:31:21 +00001792 // TODO: Should this propagate fast-math-flags?
Matt Arsenault46010932014-06-18 17:05:30 +00001793 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1794}
1795
Matt Arsenaultf058d672016-01-11 16:50:29 +00001796SDValue AMDGPUTargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
1797 SDLoc SL(Op);
1798 SDValue Src = Op.getOperand(0);
Matt Arsenaultf058d672016-01-11 16:50:29 +00001799 bool ZeroUndef = Op.getOpcode() == ISD::CTLZ_ZERO_UNDEF;
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00001800
1801 if (ZeroUndef && Src.getValueType() == MVT::i32)
1802 return DAG.getNode(AMDGPUISD::FFBH_U32, SL, MVT::i32, Src);
1803
Matt Arsenaultf058d672016-01-11 16:50:29 +00001804 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1805
1806 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1807 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1808
1809 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1810 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1811
1812 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(),
1813 *DAG.getContext(), MVT::i32);
1814
1815 SDValue Hi0 = DAG.getSetCC(SL, SetCCVT, Hi, Zero, ISD::SETEQ);
1816
1817 SDValue CtlzLo = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Lo);
1818 SDValue CtlzHi = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Hi);
1819
1820 const SDValue Bits32 = DAG.getConstant(32, SL, MVT::i32);
1821 SDValue Add = DAG.getNode(ISD::ADD, SL, MVT::i32, CtlzLo, Bits32);
1822
1823 // ctlz(x) = hi_32(x) == 0 ? ctlz(lo_32(x)) + 32 : ctlz(hi_32(x))
1824 SDValue NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32, Hi0, Add, CtlzHi);
1825
1826 if (!ZeroUndef) {
1827 // Test if the full 64-bit input is zero.
1828
1829 // FIXME: DAG combines turn what should be an s_and_b64 into a v_or_b32,
1830 // which we probably don't want.
1831 SDValue Lo0 = DAG.getSetCC(SL, SetCCVT, Lo, Zero, ISD::SETEQ);
1832 SDValue SrcIsZero = DAG.getNode(ISD::AND, SL, SetCCVT, Lo0, Hi0);
1833
1834 // TODO: If i64 setcc is half rate, it can result in 1 fewer instruction
1835 // with the same cycles, otherwise it is slower.
1836 // SDValue SrcIsZero = DAG.getSetCC(SL, SetCCVT, Src,
1837 // DAG.getConstant(0, SL, MVT::i64), ISD::SETEQ);
1838
1839 const SDValue Bits32 = DAG.getConstant(64, SL, MVT::i32);
1840
1841 // The instruction returns -1 for 0 input, but the defined intrinsic
1842 // behavior is to return the number of bits.
1843 NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32,
1844 SrcIsZero, Bits32, NewCtlz);
1845 }
1846
1847 return DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i64, NewCtlz);
1848}
1849
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00001850SDValue AMDGPUTargetLowering::LowerINT_TO_FP32(SDValue Op, SelectionDAG &DAG,
1851 bool Signed) const {
1852 // Unsigned
1853 // cul2f(ulong u)
1854 //{
1855 // uint lz = clz(u);
1856 // uint e = (u != 0) ? 127U + 63U - lz : 0;
1857 // u = (u << lz) & 0x7fffffffffffffffUL;
1858 // ulong t = u & 0xffffffffffUL;
1859 // uint v = (e << 23) | (uint)(u >> 40);
1860 // uint r = t > 0x8000000000UL ? 1U : (t == 0x8000000000UL ? v & 1U : 0U);
1861 // return as_float(v + r);
1862 //}
1863 // Signed
1864 // cl2f(long l)
1865 //{
1866 // long s = l >> 63;
1867 // float r = cul2f((l + s) ^ s);
1868 // return s ? -r : r;
1869 //}
1870
1871 SDLoc SL(Op);
1872 SDValue Src = Op.getOperand(0);
1873 SDValue L = Src;
1874
1875 SDValue S;
1876 if (Signed) {
1877 const SDValue SignBit = DAG.getConstant(63, SL, MVT::i64);
1878 S = DAG.getNode(ISD::SRA, SL, MVT::i64, L, SignBit);
1879
1880 SDValue LPlusS = DAG.getNode(ISD::ADD, SL, MVT::i64, L, S);
1881 L = DAG.getNode(ISD::XOR, SL, MVT::i64, LPlusS, S);
1882 }
1883
1884 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(),
1885 *DAG.getContext(), MVT::f32);
1886
1887
1888 SDValue ZeroI32 = DAG.getConstant(0, SL, MVT::i32);
1889 SDValue ZeroI64 = DAG.getConstant(0, SL, MVT::i64);
1890 SDValue LZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i64, L);
1891 LZ = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LZ);
1892
1893 SDValue K = DAG.getConstant(127U + 63U, SL, MVT::i32);
1894 SDValue E = DAG.getSelect(SL, MVT::i32,
1895 DAG.getSetCC(SL, SetCCVT, L, ZeroI64, ISD::SETNE),
1896 DAG.getNode(ISD::SUB, SL, MVT::i32, K, LZ),
1897 ZeroI32);
1898
1899 SDValue U = DAG.getNode(ISD::AND, SL, MVT::i64,
1900 DAG.getNode(ISD::SHL, SL, MVT::i64, L, LZ),
1901 DAG.getConstant((-1ULL) >> 1, SL, MVT::i64));
1902
1903 SDValue T = DAG.getNode(ISD::AND, SL, MVT::i64, U,
1904 DAG.getConstant(0xffffffffffULL, SL, MVT::i64));
1905
1906 SDValue UShl = DAG.getNode(ISD::SRL, SL, MVT::i64,
1907 U, DAG.getConstant(40, SL, MVT::i64));
1908
1909 SDValue V = DAG.getNode(ISD::OR, SL, MVT::i32,
1910 DAG.getNode(ISD::SHL, SL, MVT::i32, E, DAG.getConstant(23, SL, MVT::i32)),
1911 DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, UShl));
1912
1913 SDValue C = DAG.getConstant(0x8000000000ULL, SL, MVT::i64);
1914 SDValue RCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETUGT);
1915 SDValue TCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETEQ);
1916
1917 SDValue One = DAG.getConstant(1, SL, MVT::i32);
1918
1919 SDValue VTrunc1 = DAG.getNode(ISD::AND, SL, MVT::i32, V, One);
1920
1921 SDValue R = DAG.getSelect(SL, MVT::i32,
1922 RCmp,
1923 One,
1924 DAG.getSelect(SL, MVT::i32, TCmp, VTrunc1, ZeroI32));
1925 R = DAG.getNode(ISD::ADD, SL, MVT::i32, V, R);
1926 R = DAG.getNode(ISD::BITCAST, SL, MVT::f32, R);
1927
1928 if (!Signed)
1929 return R;
1930
1931 SDValue RNeg = DAG.getNode(ISD::FNEG, SL, MVT::f32, R);
1932 return DAG.getSelect(SL, MVT::f32, DAG.getSExtOrTrunc(S, SL, SetCCVT), RNeg, R);
1933}
1934
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001935SDValue AMDGPUTargetLowering::LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG,
1936 bool Signed) const {
1937 SDLoc SL(Op);
1938 SDValue Src = Op.getOperand(0);
1939
1940 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1941
1942 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001943 DAG.getConstant(0, SL, MVT::i32));
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001944 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001945 DAG.getConstant(1, SL, MVT::i32));
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001946
1947 SDValue CvtHi = DAG.getNode(Signed ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
1948 SL, MVT::f64, Hi);
1949
1950 SDValue CvtLo = DAG.getNode(ISD::UINT_TO_FP, SL, MVT::f64, Lo);
1951
1952 SDValue LdExp = DAG.getNode(AMDGPUISD::LDEXP, SL, MVT::f64, CvtHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001953 DAG.getConstant(32, SL, MVT::i32));
Sanjay Patela2607012015-09-16 16:31:21 +00001954 // TODO: Should this propagate fast-math-flags?
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001955 return DAG.getNode(ISD::FADD, SL, MVT::f64, LdExp, CvtLo);
1956}
1957
Tom Stellardc947d8c2013-10-30 17:22:05 +00001958SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op,
1959 SelectionDAG &DAG) const {
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00001960 assert(Op.getOperand(0).getValueType() == MVT::i64 &&
1961 "operation should be legal");
Tom Stellardc947d8c2013-10-30 17:22:05 +00001962
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00001963 // TODO: Factor out code common with LowerSINT_TO_FP.
1964
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001965 EVT DestVT = Op.getValueType();
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00001966 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) {
1967 SDLoc DL(Op);
1968 SDValue Src = Op.getOperand(0);
1969
1970 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src);
1971 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op));
1972 SDValue FPRound =
1973 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag);
1974
1975 return FPRound;
1976 }
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001977
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00001978 if (DestVT == MVT::f32)
1979 return LowerINT_TO_FP32(Op, DAG, false);
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001980
Matt Arsenaultedc7dcb2016-07-28 00:32:05 +00001981 assert(DestVT == MVT::f64);
1982 return LowerINT_TO_FP64(Op, DAG, false);
Tom Stellardc947d8c2013-10-30 17:22:05 +00001983}
Tom Stellardfbab8272013-08-16 01:12:11 +00001984
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001985SDValue AMDGPUTargetLowering::LowerSINT_TO_FP(SDValue Op,
1986 SelectionDAG &DAG) const {
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00001987 assert(Op.getOperand(0).getValueType() == MVT::i64 &&
1988 "operation should be legal");
1989
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00001990 // TODO: Factor out code common with LowerUINT_TO_FP.
1991
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00001992 EVT DestVT = Op.getValueType();
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00001993 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) {
1994 SDLoc DL(Op);
1995 SDValue Src = Op.getOperand(0);
1996
1997 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src);
1998 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op));
1999 SDValue FPRound =
2000 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag);
2001
2002 return FPRound;
2003 }
2004
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00002005 if (DestVT == MVT::f32)
2006 return LowerINT_TO_FP32(Op, DAG, true);
2007
Matt Arsenaultedc7dcb2016-07-28 00:32:05 +00002008 assert(DestVT == MVT::f64);
2009 return LowerINT_TO_FP64(Op, DAG, true);
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002010}
2011
Matt Arsenaultc9961752014-10-03 23:54:56 +00002012SDValue AMDGPUTargetLowering::LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG,
2013 bool Signed) const {
2014 SDLoc SL(Op);
2015
2016 SDValue Src = Op.getOperand(0);
2017
2018 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
2019
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002020 SDValue K0 = DAG.getConstantFP(BitsToDouble(UINT64_C(0x3df0000000000000)), SL,
2021 MVT::f64);
2022 SDValue K1 = DAG.getConstantFP(BitsToDouble(UINT64_C(0xc1f0000000000000)), SL,
2023 MVT::f64);
Sanjay Patela2607012015-09-16 16:31:21 +00002024 // TODO: Should this propagate fast-math-flags?
Matt Arsenaultc9961752014-10-03 23:54:56 +00002025 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, Trunc, K0);
2026
2027 SDValue FloorMul = DAG.getNode(ISD::FFLOOR, SL, MVT::f64, Mul);
2028
2029
2030 SDValue Fma = DAG.getNode(ISD::FMA, SL, MVT::f64, FloorMul, K1, Trunc);
2031
2032 SDValue Hi = DAG.getNode(Signed ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, SL,
2033 MVT::i32, FloorMul);
2034 SDValue Lo = DAG.getNode(ISD::FP_TO_UINT, SL, MVT::i32, Fma);
2035
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002036 SDValue Result = DAG.getBuildVector(MVT::v2i32, SL, {Lo, Hi});
Matt Arsenaultc9961752014-10-03 23:54:56 +00002037
2038 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Result);
2039}
2040
Tom Stellard94c21bc2016-11-01 16:31:48 +00002041SDValue AMDGPUTargetLowering::LowerFP_TO_FP16(SDValue Op, SelectionDAG &DAG) const {
2042
2043 if (getTargetMachine().Options.UnsafeFPMath) {
2044 // There is a generic expand for FP_TO_FP16 with unsafe fast math.
2045 return SDValue();
2046 }
2047
2048 SDLoc DL(Op);
2049 SDValue N0 = Op.getOperand(0);
Tom Stellard9677b602016-11-01 17:20:03 +00002050 assert (N0.getSimpleValueType() == MVT::f64);
Tom Stellard94c21bc2016-11-01 16:31:48 +00002051
2052 // f64 -> f16 conversion using round-to-nearest-even rounding mode.
2053 const unsigned ExpMask = 0x7ff;
2054 const unsigned ExpBiasf64 = 1023;
2055 const unsigned ExpBiasf16 = 15;
2056 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2057 SDValue One = DAG.getConstant(1, DL, MVT::i32);
2058 SDValue U = DAG.getNode(ISD::BITCAST, DL, MVT::i64, N0);
2059 SDValue UH = DAG.getNode(ISD::SRL, DL, MVT::i64, U,
2060 DAG.getConstant(32, DL, MVT::i64));
2061 UH = DAG.getZExtOrTrunc(UH, DL, MVT::i32);
2062 U = DAG.getZExtOrTrunc(U, DL, MVT::i32);
2063 SDValue E = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2064 DAG.getConstant(20, DL, MVT::i64));
2065 E = DAG.getNode(ISD::AND, DL, MVT::i32, E,
2066 DAG.getConstant(ExpMask, DL, MVT::i32));
2067 // Subtract the fp64 exponent bias (1023) to get the real exponent and
2068 // add the f16 bias (15) to get the biased exponent for the f16 format.
2069 E = DAG.getNode(ISD::ADD, DL, MVT::i32, E,
2070 DAG.getConstant(-ExpBiasf64 + ExpBiasf16, DL, MVT::i32));
2071
2072 SDValue M = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2073 DAG.getConstant(8, DL, MVT::i32));
2074 M = DAG.getNode(ISD::AND, DL, MVT::i32, M,
2075 DAG.getConstant(0xffe, DL, MVT::i32));
2076
2077 SDValue MaskedSig = DAG.getNode(ISD::AND, DL, MVT::i32, UH,
2078 DAG.getConstant(0x1ff, DL, MVT::i32));
2079 MaskedSig = DAG.getNode(ISD::OR, DL, MVT::i32, MaskedSig, U);
2080
2081 SDValue Lo40Set = DAG.getSelectCC(DL, MaskedSig, Zero, Zero, One, ISD::SETEQ);
2082 M = DAG.getNode(ISD::OR, DL, MVT::i32, M, Lo40Set);
2083
2084 // (M != 0 ? 0x0200 : 0) | 0x7c00;
2085 SDValue I = DAG.getNode(ISD::OR, DL, MVT::i32,
2086 DAG.getSelectCC(DL, M, Zero, DAG.getConstant(0x0200, DL, MVT::i32),
2087 Zero, ISD::SETNE), DAG.getConstant(0x7c00, DL, MVT::i32));
2088
2089 // N = M | (E << 12);
2090 SDValue N = DAG.getNode(ISD::OR, DL, MVT::i32, M,
2091 DAG.getNode(ISD::SHL, DL, MVT::i32, E,
2092 DAG.getConstant(12, DL, MVT::i32)));
2093
2094 // B = clamp(1-E, 0, 13);
2095 SDValue OneSubExp = DAG.getNode(ISD::SUB, DL, MVT::i32,
2096 One, E);
2097 SDValue B = DAG.getNode(ISD::SMAX, DL, MVT::i32, OneSubExp, Zero);
2098 B = DAG.getNode(ISD::SMIN, DL, MVT::i32, B,
2099 DAG.getConstant(13, DL, MVT::i32));
2100
2101 SDValue SigSetHigh = DAG.getNode(ISD::OR, DL, MVT::i32, M,
2102 DAG.getConstant(0x1000, DL, MVT::i32));
2103
2104 SDValue D = DAG.getNode(ISD::SRL, DL, MVT::i32, SigSetHigh, B);
2105 SDValue D0 = DAG.getNode(ISD::SHL, DL, MVT::i32, D, B);
2106 SDValue D1 = DAG.getSelectCC(DL, D0, SigSetHigh, One, Zero, ISD::SETNE);
2107 D = DAG.getNode(ISD::OR, DL, MVT::i32, D, D1);
2108
2109 SDValue V = DAG.getSelectCC(DL, E, One, D, N, ISD::SETLT);
2110 SDValue VLow3 = DAG.getNode(ISD::AND, DL, MVT::i32, V,
2111 DAG.getConstant(0x7, DL, MVT::i32));
2112 V = DAG.getNode(ISD::SRL, DL, MVT::i32, V,
2113 DAG.getConstant(2, DL, MVT::i32));
2114 SDValue V0 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(3, DL, MVT::i32),
2115 One, Zero, ISD::SETEQ);
2116 SDValue V1 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(5, DL, MVT::i32),
2117 One, Zero, ISD::SETGT);
2118 V1 = DAG.getNode(ISD::OR, DL, MVT::i32, V0, V1);
2119 V = DAG.getNode(ISD::ADD, DL, MVT::i32, V, V1);
2120
2121 V = DAG.getSelectCC(DL, E, DAG.getConstant(30, DL, MVT::i32),
2122 DAG.getConstant(0x7c00, DL, MVT::i32), V, ISD::SETGT);
2123 V = DAG.getSelectCC(DL, E, DAG.getConstant(1039, DL, MVT::i32),
2124 I, V, ISD::SETEQ);
2125
2126 // Extract the sign bit.
2127 SDValue Sign = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2128 DAG.getConstant(16, DL, MVT::i32));
2129 Sign = DAG.getNode(ISD::AND, DL, MVT::i32, Sign,
2130 DAG.getConstant(0x8000, DL, MVT::i32));
2131
2132 V = DAG.getNode(ISD::OR, DL, MVT::i32, Sign, V);
2133 return DAG.getZExtOrTrunc(V, DL, Op.getValueType());
2134}
2135
Matt Arsenaultc9961752014-10-03 23:54:56 +00002136SDValue AMDGPUTargetLowering::LowerFP_TO_SINT(SDValue Op,
2137 SelectionDAG &DAG) const {
2138 SDValue Src = Op.getOperand(0);
2139
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002140 // TODO: Factor out code common with LowerFP_TO_UINT.
2141
2142 EVT SrcVT = Src.getValueType();
2143 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) {
2144 SDLoc DL(Op);
2145
2146 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src);
2147 SDValue FpToInt32 =
2148 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend);
2149
2150 return FpToInt32;
2151 }
2152
Matt Arsenaultc9961752014-10-03 23:54:56 +00002153 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64)
2154 return LowerFP64_TO_INT(Op, DAG, true);
2155
2156 return SDValue();
2157}
2158
2159SDValue AMDGPUTargetLowering::LowerFP_TO_UINT(SDValue Op,
2160 SelectionDAG &DAG) const {
2161 SDValue Src = Op.getOperand(0);
2162
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002163 // TODO: Factor out code common with LowerFP_TO_SINT.
2164
2165 EVT SrcVT = Src.getValueType();
2166 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) {
2167 SDLoc DL(Op);
2168
2169 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src);
2170 SDValue FpToInt32 =
2171 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend);
2172
2173 return FpToInt32;
2174 }
2175
Matt Arsenaultc9961752014-10-03 23:54:56 +00002176 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64)
2177 return LowerFP64_TO_INT(Op, DAG, false);
2178
2179 return SDValue();
2180}
2181
Matt Arsenaultfae02982014-03-17 18:58:11 +00002182SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
2183 SelectionDAG &DAG) const {
2184 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
2185 MVT VT = Op.getSimpleValueType();
2186 MVT ScalarVT = VT.getScalarType();
2187
Matt Arsenaultedc7dcb2016-07-28 00:32:05 +00002188 assert(VT.isVector());
Matt Arsenaultfae02982014-03-17 18:58:11 +00002189
2190 SDValue Src = Op.getOperand(0);
Matt Arsenault5dbd5db2014-04-22 03:49:30 +00002191 SDLoc DL(Op);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002192
Matt Arsenault5dbd5db2014-04-22 03:49:30 +00002193 // TODO: Don't scalarize on Evergreen?
2194 unsigned NElts = VT.getVectorNumElements();
2195 SmallVector<SDValue, 8> Args;
2196 DAG.ExtractVectorElements(Src, Args, 0, NElts);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002197
Matt Arsenault5dbd5db2014-04-22 03:49:30 +00002198 SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType());
2199 for (unsigned I = 0; I < NElts; ++I)
2200 Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002201
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002202 return DAG.getBuildVector(VT, DL, Args);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002203}
2204
Tom Stellard75aadc22012-12-11 21:25:42 +00002205//===----------------------------------------------------------------------===//
Tom Stellard50122a52014-04-07 19:45:41 +00002206// Custom DAG optimizations
2207//===----------------------------------------------------------------------===//
2208
2209static bool isU24(SDValue Op, SelectionDAG &DAG) {
2210 APInt KnownZero, KnownOne;
2211 EVT VT = Op.getValueType();
Jay Foada0653a32014-05-14 21:14:37 +00002212 DAG.computeKnownBits(Op, KnownZero, KnownOne);
Tom Stellard50122a52014-04-07 19:45:41 +00002213
2214 return (VT.getSizeInBits() - KnownZero.countLeadingOnes()) <= 24;
2215}
2216
2217static bool isI24(SDValue Op, SelectionDAG &DAG) {
2218 EVT VT = Op.getValueType();
2219
2220 // In order for this to be a signed 24-bit value, bit 23, must
2221 // be a sign bit.
2222 return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated
2223 // as unsigned 24-bit values.
2224 (VT.getSizeInBits() - DAG.ComputeNumSignBits(Op)) < 24;
2225}
2226
Tom Stellard09c2bd62016-10-14 19:14:29 +00002227static bool simplifyI24(SDNode *Node24, unsigned OpIdx,
2228 TargetLowering::DAGCombinerInfo &DCI) {
Tom Stellard50122a52014-04-07 19:45:41 +00002229
2230 SelectionDAG &DAG = DCI.DAG;
Tom Stellard09c2bd62016-10-14 19:14:29 +00002231 SDValue Op = Node24->getOperand(OpIdx);
Tom Stellard50122a52014-04-07 19:45:41 +00002232 EVT VT = Op.getValueType();
2233
2234 APInt Demanded = APInt::getLowBitsSet(VT.getSizeInBits(), 24);
2235 APInt KnownZero, KnownOne;
2236 TargetLowering::TargetLoweringOpt TLO(DAG, true, true);
Tom Stellard09c2bd62016-10-14 19:14:29 +00002237 if (TLO.SimplifyDemandedBits(Node24, OpIdx, Demanded, DCI))
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002238 return true;
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002239
2240 return false;
Tom Stellard50122a52014-04-07 19:45:41 +00002241}
2242
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002243template <typename IntTy>
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002244static SDValue constantFoldBFE(SelectionDAG &DAG, IntTy Src0, uint32_t Offset,
2245 uint32_t Width, const SDLoc &DL) {
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002246 if (Width + Offset < 32) {
Matt Arsenault46cbc432014-09-19 00:42:06 +00002247 uint32_t Shl = static_cast<uint32_t>(Src0) << (32 - Offset - Width);
2248 IntTy Result = static_cast<IntTy>(Shl) >> (32 - Width);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002249 return DAG.getConstant(Result, DL, MVT::i32);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002250 }
2251
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002252 return DAG.getConstant(Src0 >> Offset, DL, MVT::i32);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002253}
2254
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002255static bool hasVolatileUser(SDNode *Val) {
2256 for (SDNode *U : Val->uses()) {
2257 if (MemSDNode *M = dyn_cast<MemSDNode>(U)) {
2258 if (M->isVolatile())
2259 return true;
2260 }
2261 }
2262
2263 return false;
2264}
2265
Matt Arsenault8af47a02016-07-01 22:55:55 +00002266bool AMDGPUTargetLowering::shouldCombineMemoryType(EVT VT) const {
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002267 // i32 vectors are the canonical memory type.
2268 if (VT.getScalarType() == MVT::i32 || isTypeLegal(VT))
2269 return false;
2270
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002271 if (!VT.isByteSized())
2272 return false;
2273
2274 unsigned Size = VT.getStoreSize();
2275
2276 if ((Size == 1 || Size == 2 || Size == 4) && !VT.isVector())
2277 return false;
2278
2279 if (Size == 3 || (Size > 4 && (Size % 4 != 0)))
2280 return false;
2281
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002282 return true;
2283}
2284
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002285// Replace load of an illegal type with a store of a bitcast to a friendlier
2286// type.
2287SDValue AMDGPUTargetLowering::performLoadCombine(SDNode *N,
2288 DAGCombinerInfo &DCI) const {
2289 if (!DCI.isBeforeLegalize())
2290 return SDValue();
2291
2292 LoadSDNode *LN = cast<LoadSDNode>(N);
2293 if (LN->isVolatile() || !ISD::isNormalLoad(LN) || hasVolatileUser(LN))
2294 return SDValue();
2295
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002296 SDLoc SL(N);
2297 SelectionDAG &DAG = DCI.DAG;
2298 EVT VT = LN->getMemoryVT();
Matt Arsenault8af47a02016-07-01 22:55:55 +00002299
2300 unsigned Size = VT.getStoreSize();
2301 unsigned Align = LN->getAlignment();
2302 if (Align < Size && isTypeLegal(VT)) {
2303 bool IsFast;
2304 unsigned AS = LN->getAddressSpace();
2305
2306 // Expand unaligned loads earlier than legalization. Due to visitation order
2307 // problems during legalization, the emitted instructions to pack and unpack
2308 // the bytes again are not eliminated in the case of an unaligned copy.
2309 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) {
Matt Arsenaultb50eb8d2016-08-31 21:52:27 +00002310 if (VT.isVector())
2311 return scalarizeVectorLoad(LN, DAG);
2312
Matt Arsenault8af47a02016-07-01 22:55:55 +00002313 SDValue Ops[2];
2314 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(LN, DAG);
2315 return DAG.getMergeValues(Ops, SDLoc(N));
2316 }
2317
2318 if (!IsFast)
2319 return SDValue();
2320 }
2321
2322 if (!shouldCombineMemoryType(VT))
2323 return SDValue();
2324
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002325 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
2326
2327 SDValue NewLoad
2328 = DAG.getLoad(NewVT, SL, LN->getChain(),
2329 LN->getBasePtr(), LN->getMemOperand());
2330
2331 SDValue BC = DAG.getNode(ISD::BITCAST, SL, VT, NewLoad);
2332 DCI.CombineTo(N, BC, NewLoad.getValue(1));
2333 return SDValue(N, 0);
2334}
2335
2336// Replace store of an illegal type with a store of a bitcast to a friendlier
2337// type.
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002338SDValue AMDGPUTargetLowering::performStoreCombine(SDNode *N,
2339 DAGCombinerInfo &DCI) const {
2340 if (!DCI.isBeforeLegalize())
2341 return SDValue();
2342
2343 StoreSDNode *SN = cast<StoreSDNode>(N);
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002344 if (SN->isVolatile() || !ISD::isNormalStore(SN))
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002345 return SDValue();
2346
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002347 EVT VT = SN->getMemoryVT();
Matt Arsenault8af47a02016-07-01 22:55:55 +00002348 unsigned Size = VT.getStoreSize();
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002349
2350 SDLoc SL(N);
2351 SelectionDAG &DAG = DCI.DAG;
Matt Arsenault8af47a02016-07-01 22:55:55 +00002352 unsigned Align = SN->getAlignment();
2353 if (Align < Size && isTypeLegal(VT)) {
2354 bool IsFast;
2355 unsigned AS = SN->getAddressSpace();
2356
2357 // Expand unaligned stores earlier than legalization. Due to visitation
2358 // order problems during legalization, the emitted instructions to pack and
2359 // unpack the bytes again are not eliminated in the case of an unaligned
2360 // copy.
Matt Arsenaultb50eb8d2016-08-31 21:52:27 +00002361 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) {
2362 if (VT.isVector())
2363 return scalarizeVectorStore(SN, DAG);
2364
Matt Arsenault8af47a02016-07-01 22:55:55 +00002365 return expandUnalignedStore(SN, DAG);
Matt Arsenaultb50eb8d2016-08-31 21:52:27 +00002366 }
Matt Arsenault8af47a02016-07-01 22:55:55 +00002367
2368 if (!IsFast)
2369 return SDValue();
2370 }
2371
2372 if (!shouldCombineMemoryType(VT))
2373 return SDValue();
2374
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002375 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
Matt Arsenault8af47a02016-07-01 22:55:55 +00002376 SDValue Val = SN->getValue();
2377
2378 //DCI.AddToWorklist(Val.getNode());
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002379
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002380 bool OtherUses = !Val.hasOneUse();
2381 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NewVT, Val);
2382 if (OtherUses) {
2383 SDValue CastBack = DAG.getNode(ISD::BITCAST, SL, VT, CastVal);
2384 DAG.ReplaceAllUsesOfValueWith(Val, CastBack);
2385 }
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002386
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002387 return DAG.getStore(SN->getChain(), SL, CastVal,
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002388 SN->getBasePtr(), SN->getMemOperand());
2389}
2390
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002391/// Split the 64-bit value \p LHS into two 32-bit components, and perform the
2392/// binary operation \p Opc to it with the corresponding constant operands.
2393SDValue AMDGPUTargetLowering::splitBinaryBitConstantOpImpl(
2394 DAGCombinerInfo &DCI, const SDLoc &SL,
2395 unsigned Opc, SDValue LHS,
2396 uint32_t ValLo, uint32_t ValHi) const {
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002397 SelectionDAG &DAG = DCI.DAG;
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002398 SDValue Lo, Hi;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002399 std::tie(Lo, Hi) = split64BitValue(LHS, DAG);
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002400
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002401 SDValue LoRHS = DAG.getConstant(ValLo, SL, MVT::i32);
2402 SDValue HiRHS = DAG.getConstant(ValHi, SL, MVT::i32);
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002403
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002404 SDValue LoAnd = DAG.getNode(Opc, SL, MVT::i32, Lo, LoRHS);
2405 SDValue HiAnd = DAG.getNode(Opc, SL, MVT::i32, Hi, HiRHS);
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002406
Matt Arsenaultefa3fe12016-04-22 22:48:38 +00002407 // Re-visit the ands. It's possible we eliminated one of them and it could
2408 // simplify the vector.
2409 DCI.AddToWorklist(Lo.getNode());
2410 DCI.AddToWorklist(Hi.getNode());
2411
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002412 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {LoAnd, HiAnd});
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002413 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2414}
2415
Matt Arsenault24692112015-07-14 18:20:33 +00002416SDValue AMDGPUTargetLowering::performShlCombine(SDNode *N,
2417 DAGCombinerInfo &DCI) const {
2418 if (N->getValueType(0) != MVT::i64)
2419 return SDValue();
2420
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002421 // i64 (shl x, C) -> (build_pair 0, (shl x, C -32))
Matt Arsenault24692112015-07-14 18:20:33 +00002422
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002423 // On some subtargets, 64-bit shift is a quarter rate instruction. In the
2424 // common case, splitting this into a move and a 32-bit shift is faster and
2425 // the same code size.
Matt Arsenault24692112015-07-14 18:20:33 +00002426 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002427 if (!RHS)
2428 return SDValue();
2429
2430 unsigned RHSVal = RHS->getZExtValue();
2431 if (RHSVal < 32)
Matt Arsenault24692112015-07-14 18:20:33 +00002432 return SDValue();
2433
2434 SDValue LHS = N->getOperand(0);
2435
2436 SDLoc SL(N);
2437 SelectionDAG &DAG = DCI.DAG;
2438
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002439 SDValue ShiftAmt = DAG.getConstant(RHSVal - 32, SL, MVT::i32);
2440
Matt Arsenault24692112015-07-14 18:20:33 +00002441 SDValue Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LHS);
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002442 SDValue NewShift = DAG.getNode(ISD::SHL, SL, MVT::i32, Lo, ShiftAmt);
Matt Arsenault24692112015-07-14 18:20:33 +00002443
2444 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
Matt Arsenault80edab92016-01-18 21:43:36 +00002445
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002446 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {Zero, NewShift});
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002447 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
Matt Arsenault24692112015-07-14 18:20:33 +00002448}
2449
Matt Arsenault33e3ece2016-01-18 22:09:04 +00002450SDValue AMDGPUTargetLowering::performSraCombine(SDNode *N,
2451 DAGCombinerInfo &DCI) const {
2452 if (N->getValueType(0) != MVT::i64)
2453 return SDValue();
2454
2455 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2456 if (!RHS)
2457 return SDValue();
2458
2459 SelectionDAG &DAG = DCI.DAG;
2460 SDLoc SL(N);
2461 unsigned RHSVal = RHS->getZExtValue();
2462
2463 // (sra i64:x, 32) -> build_pair x, (sra hi_32(x), 31)
2464 if (RHSVal == 32) {
2465 SDValue Hi = getHiHalf64(N->getOperand(0), DAG);
2466 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi,
2467 DAG.getConstant(31, SL, MVT::i32));
2468
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002469 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {Hi, NewShift});
Matt Arsenault33e3ece2016-01-18 22:09:04 +00002470 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec);
2471 }
2472
2473 // (sra i64:x, 63) -> build_pair (sra hi_32(x), 31), (sra hi_32(x), 31)
2474 if (RHSVal == 63) {
2475 SDValue Hi = getHiHalf64(N->getOperand(0), DAG);
2476 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi,
2477 DAG.getConstant(31, SL, MVT::i32));
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002478 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, NewShift});
Matt Arsenault33e3ece2016-01-18 22:09:04 +00002479 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec);
2480 }
2481
2482 return SDValue();
2483}
2484
Matt Arsenault80edab92016-01-18 21:43:36 +00002485SDValue AMDGPUTargetLowering::performSrlCombine(SDNode *N,
2486 DAGCombinerInfo &DCI) const {
2487 if (N->getValueType(0) != MVT::i64)
2488 return SDValue();
2489
2490 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2491 if (!RHS)
2492 return SDValue();
2493
2494 unsigned ShiftAmt = RHS->getZExtValue();
2495 if (ShiftAmt < 32)
2496 return SDValue();
2497
2498 // srl i64:x, C for C >= 32
2499 // =>
2500 // build_pair (srl hi_32(x), C - 32), 0
2501
2502 SelectionDAG &DAG = DCI.DAG;
2503 SDLoc SL(N);
2504
2505 SDValue One = DAG.getConstant(1, SL, MVT::i32);
2506 SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
2507
2508 SDValue VecOp = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, N->getOperand(0));
2509 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32,
2510 VecOp, One);
2511
2512 SDValue NewConst = DAG.getConstant(ShiftAmt - 32, SL, MVT::i32);
2513 SDValue NewShift = DAG.getNode(ISD::SRL, SL, MVT::i32, Hi, NewConst);
2514
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002515 SDValue BuildPair = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, Zero});
Matt Arsenault80edab92016-01-18 21:43:36 +00002516
2517 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildPair);
2518}
2519
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002520// We need to specifically handle i64 mul here to avoid unnecessary conversion
2521// instructions. If we only match on the legalized i64 mul expansion,
2522// SimplifyDemandedBits will be unable to remove them because there will be
2523// multiple uses due to the separate mul + mulh[su].
2524static SDValue getMul24(SelectionDAG &DAG, const SDLoc &SL,
2525 SDValue N0, SDValue N1, unsigned Size, bool Signed) {
2526 if (Size <= 32) {
2527 unsigned MulOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24;
2528 return DAG.getNode(MulOpc, SL, MVT::i32, N0, N1);
2529 }
2530
2531 // Because we want to eliminate extension instructions before the
2532 // operation, we need to create a single user here (i.e. not the separate
2533 // mul_lo + mul_hi) so that SimplifyDemandedBits will deal with it.
2534
2535 unsigned MulOpc = Signed ? AMDGPUISD::MUL_LOHI_I24 : AMDGPUISD::MUL_LOHI_U24;
2536
2537 SDValue Mul = DAG.getNode(MulOpc, SL,
2538 DAG.getVTList(MVT::i32, MVT::i32), N0, N1);
2539
2540 return DAG.getNode(ISD::BUILD_PAIR, SL, MVT::i64,
2541 Mul.getValue(0), Mul.getValue(1));
2542}
2543
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002544SDValue AMDGPUTargetLowering::performMulCombine(SDNode *N,
2545 DAGCombinerInfo &DCI) const {
2546 EVT VT = N->getValueType(0);
2547
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002548 unsigned Size = VT.getSizeInBits();
2549 if (VT.isVector() || Size > 64)
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002550 return SDValue();
2551
Tom Stellard115a6152016-11-10 16:02:37 +00002552 // There are i16 integer mul/mad.
2553 if (Subtarget->has16BitInsts() && VT.getScalarType().bitsLE(MVT::i16))
2554 return SDValue();
2555
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002556 SelectionDAG &DAG = DCI.DAG;
2557 SDLoc DL(N);
2558
2559 SDValue N0 = N->getOperand(0);
2560 SDValue N1 = N->getOperand(1);
2561 SDValue Mul;
2562
2563 if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) {
2564 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
2565 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002566 Mul = getMul24(DAG, DL, N0, N1, Size, false);
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002567 } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) {
2568 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
2569 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002570 Mul = getMul24(DAG, DL, N0, N1, Size, true);
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002571 } else {
2572 return SDValue();
2573 }
2574
2575 // We need to use sext even for MUL_U24, because MUL_U24 is used
2576 // for signed multiply of 8 and 16-bit types.
2577 return DAG.getSExtOrTrunc(Mul, DL, VT);
2578}
2579
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002580SDValue AMDGPUTargetLowering::performMulhsCombine(SDNode *N,
2581 DAGCombinerInfo &DCI) const {
2582 EVT VT = N->getValueType(0);
2583
2584 if (!Subtarget->hasMulI24() || VT.isVector())
2585 return SDValue();
2586
2587 SelectionDAG &DAG = DCI.DAG;
2588 SDLoc DL(N);
2589
2590 SDValue N0 = N->getOperand(0);
2591 SDValue N1 = N->getOperand(1);
2592
2593 if (!isI24(N0, DAG) || !isI24(N1, DAG))
2594 return SDValue();
2595
2596 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
2597 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
2598
2599 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_I24, DL, MVT::i32, N0, N1);
2600 DCI.AddToWorklist(Mulhi.getNode());
2601 return DAG.getSExtOrTrunc(Mulhi, DL, VT);
2602}
2603
2604SDValue AMDGPUTargetLowering::performMulhuCombine(SDNode *N,
2605 DAGCombinerInfo &DCI) const {
2606 EVT VT = N->getValueType(0);
2607
2608 if (!Subtarget->hasMulU24() || VT.isVector() || VT.getSizeInBits() > 32)
2609 return SDValue();
2610
2611 SelectionDAG &DAG = DCI.DAG;
2612 SDLoc DL(N);
2613
2614 SDValue N0 = N->getOperand(0);
2615 SDValue N1 = N->getOperand(1);
2616
2617 if (!isU24(N0, DAG) || !isU24(N1, DAG))
2618 return SDValue();
2619
2620 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
2621 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
2622
2623 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_U24, DL, MVT::i32, N0, N1);
2624 DCI.AddToWorklist(Mulhi.getNode());
2625 return DAG.getZExtOrTrunc(Mulhi, DL, VT);
2626}
2627
2628SDValue AMDGPUTargetLowering::performMulLoHi24Combine(
2629 SDNode *N, DAGCombinerInfo &DCI) const {
2630 SelectionDAG &DAG = DCI.DAG;
2631
Tom Stellard09c2bd62016-10-14 19:14:29 +00002632 // Simplify demanded bits before splitting into multiple users.
2633 if (simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI))
2634 return SDValue();
2635
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002636 SDValue N0 = N->getOperand(0);
2637 SDValue N1 = N->getOperand(1);
2638
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002639 bool Signed = (N->getOpcode() == AMDGPUISD::MUL_LOHI_I24);
2640
2641 unsigned MulLoOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24;
2642 unsigned MulHiOpc = Signed ? AMDGPUISD::MULHI_I24 : AMDGPUISD::MULHI_U24;
2643
2644 SDLoc SL(N);
2645
2646 SDValue MulLo = DAG.getNode(MulLoOpc, SL, MVT::i32, N0, N1);
2647 SDValue MulHi = DAG.getNode(MulHiOpc, SL, MVT::i32, N0, N1);
2648 return DAG.getMergeValues({ MulLo, MulHi }, SL);
2649}
2650
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002651static bool isNegativeOne(SDValue Val) {
2652 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val))
2653 return C->isAllOnesValue();
2654 return false;
2655}
2656
2657static bool isCtlzOpc(unsigned Opc) {
2658 return Opc == ISD::CTLZ || Opc == ISD::CTLZ_ZERO_UNDEF;
2659}
2660
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002661SDValue AMDGPUTargetLowering::getFFBH_U32(SelectionDAG &DAG,
2662 SDValue Op,
2663 const SDLoc &DL) const {
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002664 EVT VT = Op.getValueType();
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002665 EVT LegalVT = getTypeToTransformTo(*DAG.getContext(), VT);
2666 if (LegalVT != MVT::i32 && (Subtarget->has16BitInsts() &&
2667 LegalVT != MVT::i16))
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002668 return SDValue();
2669
2670 if (VT != MVT::i32)
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002671 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Op);
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002672
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002673 SDValue FFBH = DAG.getNode(AMDGPUISD::FFBH_U32, DL, MVT::i32, Op);
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002674 if (VT != MVT::i32)
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002675 FFBH = DAG.getNode(ISD::TRUNCATE, DL, VT, FFBH);
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002676
2677 return FFBH;
2678}
2679
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002680// The native instructions return -1 on 0 input. Optimize out a select that
2681// produces -1 on 0.
2682//
2683// TODO: If zero is not undef, we could also do this if the output is compared
2684// against the bitwidth.
2685//
2686// TODO: Should probably combine against FFBH_U32 instead of ctlz directly.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002687SDValue AMDGPUTargetLowering::performCtlzCombine(const SDLoc &SL, SDValue Cond,
2688 SDValue LHS, SDValue RHS,
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002689 DAGCombinerInfo &DCI) const {
2690 ConstantSDNode *CmpRhs = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2691 if (!CmpRhs || !CmpRhs->isNullValue())
2692 return SDValue();
2693
2694 SelectionDAG &DAG = DCI.DAG;
2695 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
2696 SDValue CmpLHS = Cond.getOperand(0);
2697
2698 // select (setcc x, 0, eq), -1, (ctlz_zero_undef x) -> ffbh_u32 x
2699 if (CCOpcode == ISD::SETEQ &&
2700 isCtlzOpc(RHS.getOpcode()) &&
2701 RHS.getOperand(0) == CmpLHS &&
2702 isNegativeOne(LHS)) {
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002703 return getFFBH_U32(DAG, CmpLHS, SL);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002704 }
2705
2706 // select (setcc x, 0, ne), (ctlz_zero_undef x), -1 -> ffbh_u32 x
2707 if (CCOpcode == ISD::SETNE &&
2708 isCtlzOpc(LHS.getOpcode()) &&
2709 LHS.getOperand(0) == CmpLHS &&
2710 isNegativeOne(RHS)) {
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002711 return getFFBH_U32(DAG, CmpLHS, SL);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002712 }
2713
2714 return SDValue();
2715}
2716
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002717static SDValue distributeOpThroughSelect(TargetLowering::DAGCombinerInfo &DCI,
2718 unsigned Op,
2719 const SDLoc &SL,
2720 SDValue Cond,
2721 SDValue N1,
2722 SDValue N2) {
2723 SelectionDAG &DAG = DCI.DAG;
2724 EVT VT = N1.getValueType();
2725
2726 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT, Cond,
2727 N1.getOperand(0), N2.getOperand(0));
2728 DCI.AddToWorklist(NewSelect.getNode());
2729 return DAG.getNode(Op, SL, VT, NewSelect);
2730}
2731
2732// Pull a free FP operation out of a select so it may fold into uses.
2733//
2734// select c, (fneg x), (fneg y) -> fneg (select c, x, y)
2735// select c, (fneg x), k -> fneg (select c, x, (fneg k))
2736//
2737// select c, (fabs x), (fabs y) -> fabs (select c, x, y)
2738// select c, (fabs x), +k -> fabs (select c, x, k)
2739static SDValue foldFreeOpFromSelect(TargetLowering::DAGCombinerInfo &DCI,
2740 SDValue N) {
2741 SelectionDAG &DAG = DCI.DAG;
2742 SDValue Cond = N.getOperand(0);
2743 SDValue LHS = N.getOperand(1);
2744 SDValue RHS = N.getOperand(2);
2745
2746 EVT VT = N.getValueType();
2747 if ((LHS.getOpcode() == ISD::FABS && RHS.getOpcode() == ISD::FABS) ||
2748 (LHS.getOpcode() == ISD::FNEG && RHS.getOpcode() == ISD::FNEG)) {
2749 return distributeOpThroughSelect(DCI, LHS.getOpcode(),
2750 SDLoc(N), Cond, LHS, RHS);
2751 }
2752
2753 bool Inv = false;
2754 if (RHS.getOpcode() == ISD::FABS || RHS.getOpcode() == ISD::FNEG) {
2755 std::swap(LHS, RHS);
2756 Inv = true;
2757 }
2758
2759 // TODO: Support vector constants.
2760 ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
2761 if ((LHS.getOpcode() == ISD::FNEG || LHS.getOpcode() == ISD::FABS) && CRHS) {
2762 SDLoc SL(N);
2763 // If one side is an fneg/fabs and the other is a constant, we can push the
2764 // fneg/fabs down. If it's an fabs, the constant needs to be non-negative.
2765 SDValue NewLHS = LHS.getOperand(0);
2766 SDValue NewRHS = RHS;
2767
Matt Arsenault45337df2017-01-12 18:58:15 +00002768 // Careful: if the neg can be folded up, don't try to pull it back down.
2769 bool ShouldFoldNeg = true;
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002770
Matt Arsenault45337df2017-01-12 18:58:15 +00002771 if (NewLHS.hasOneUse()) {
2772 unsigned Opc = NewLHS.getOpcode();
2773 if (LHS.getOpcode() == ISD::FNEG && fnegFoldsIntoOp(Opc))
2774 ShouldFoldNeg = false;
2775 if (LHS.getOpcode() == ISD::FABS && Opc == ISD::FMUL)
2776 ShouldFoldNeg = false;
2777 }
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002778
Matt Arsenault45337df2017-01-12 18:58:15 +00002779 if (ShouldFoldNeg) {
2780 if (LHS.getOpcode() == ISD::FNEG)
2781 NewRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2782 else if (CRHS->isNegative())
2783 return SDValue();
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002784
Matt Arsenault45337df2017-01-12 18:58:15 +00002785 if (Inv)
2786 std::swap(NewLHS, NewRHS);
2787
2788 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT,
2789 Cond, NewLHS, NewRHS);
2790 DCI.AddToWorklist(NewSelect.getNode());
2791 return DAG.getNode(LHS.getOpcode(), SL, VT, NewSelect);
2792 }
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002793 }
2794
2795 return SDValue();
2796}
2797
2798
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002799SDValue AMDGPUTargetLowering::performSelectCombine(SDNode *N,
2800 DAGCombinerInfo &DCI) const {
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002801 if (SDValue Folded = foldFreeOpFromSelect(DCI, SDValue(N, 0)))
2802 return Folded;
2803
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002804 SDValue Cond = N->getOperand(0);
2805 if (Cond.getOpcode() != ISD::SETCC)
2806 return SDValue();
2807
2808 EVT VT = N->getValueType(0);
2809 SDValue LHS = Cond.getOperand(0);
2810 SDValue RHS = Cond.getOperand(1);
2811 SDValue CC = Cond.getOperand(2);
2812
2813 SDValue True = N->getOperand(1);
2814 SDValue False = N->getOperand(2);
2815
Matt Arsenault0b26e472016-12-22 21:40:08 +00002816 if (Cond.hasOneUse()) { // TODO: Look for multiple select uses.
2817 SelectionDAG &DAG = DCI.DAG;
2818 if ((DAG.isConstantValueOfAnyType(True) ||
2819 DAG.isConstantValueOfAnyType(True)) &&
2820 (!DAG.isConstantValueOfAnyType(False) &&
2821 !DAG.isConstantValueOfAnyType(False))) {
2822 // Swap cmp + select pair to move constant to false input.
2823 // This will allow using VOPC cndmasks more often.
2824 // select (setcc x, y), k, x -> select (setcc y, x) x, x
2825
2826 SDLoc SL(N);
2827 ISD::CondCode NewCC = getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2828 LHS.getValueType().isInteger());
2829
2830 SDValue NewCond = DAG.getSetCC(SL, Cond.getValueType(), LHS, RHS, NewCC);
2831 return DAG.getNode(ISD::SELECT, SL, VT, NewCond, False, True);
2832 }
2833 }
2834
Matt Arsenault5b39b342016-01-28 20:53:48 +00002835 if (VT == MVT::f32 && Cond.hasOneUse()) {
2836 SDValue MinMax
2837 = CombineFMinMaxLegacy(SDLoc(N), VT, LHS, RHS, True, False, CC, DCI);
2838 // Revisit this node so we can catch min3/max3/med3 patterns.
2839 //DCI.AddToWorklist(MinMax.getNode());
2840 return MinMax;
2841 }
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002842
2843 // There's no reason to not do this if the condition has other uses.
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002844 return performCtlzCombine(SDLoc(N), Cond, True, False, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002845}
2846
Matt Arsenault2529fba2017-01-12 00:09:34 +00002847SDValue AMDGPUTargetLowering::performFNegCombine(SDNode *N,
2848 DAGCombinerInfo &DCI) const {
2849 SelectionDAG &DAG = DCI.DAG;
2850 SDValue N0 = N->getOperand(0);
2851 EVT VT = N->getValueType(0);
2852
2853 unsigned Opc = N0.getOpcode();
2854
2855 // If the input has multiple uses and we can either fold the negate down, or
2856 // the other uses cannot, give up. This both prevents unprofitable
2857 // transformations and infinite loops: we won't repeatedly try to fold around
2858 // a negate that has no 'good' form.
2859 //
2860 // TODO: Check users can fold
2861 if (fnegFoldsIntoOp(Opc) && !N0.hasOneUse())
2862 return SDValue();
2863
2864 SDLoc SL(N);
2865 switch (Opc) {
2866 case ISD::FADD: {
Matt Arsenault3e6f9b52017-01-19 06:35:27 +00002867 if (!mayIgnoreSignedZero(N0))
2868 return SDValue();
2869
Matt Arsenault2529fba2017-01-12 00:09:34 +00002870 // (fneg (fadd x, y)) -> (fadd (fneg x), (fneg y))
2871 SDValue LHS = N0.getOperand(0);
2872 SDValue RHS = N0.getOperand(1);
2873
2874 if (LHS.getOpcode() != ISD::FNEG)
2875 LHS = DAG.getNode(ISD::FNEG, SL, VT, LHS);
2876 else
2877 LHS = LHS.getOperand(0);
2878
2879 if (RHS.getOpcode() != ISD::FNEG)
2880 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2881 else
2882 RHS = RHS.getOperand(0);
2883
Matt Arsenault7b49ad72017-01-23 19:08:34 +00002884 SDValue Res = DAG.getNode(ISD::FADD, SL, VT, LHS, RHS, N0->getFlags());
Matt Arsenault2529fba2017-01-12 00:09:34 +00002885 if (!N0.hasOneUse())
2886 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
2887 return Res;
2888 }
Matt Arsenaulta8c325e2017-01-12 18:26:30 +00002889 case ISD::FMUL:
2890 case AMDGPUISD::FMUL_LEGACY: {
Matt Arsenault4103a812017-01-12 00:23:20 +00002891 // (fneg (fmul x, y)) -> (fmul x, (fneg y))
Matt Arsenaulta8c325e2017-01-12 18:26:30 +00002892 // (fneg (fmul_legacy x, y)) -> (fmul_legacy x, (fneg y))
Matt Arsenault4103a812017-01-12 00:23:20 +00002893 SDValue LHS = N0.getOperand(0);
2894 SDValue RHS = N0.getOperand(1);
2895
2896 if (LHS.getOpcode() == ISD::FNEG)
2897 LHS = LHS.getOperand(0);
2898 else if (RHS.getOpcode() == ISD::FNEG)
2899 RHS = RHS.getOperand(0);
2900 else
2901 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2902
Matt Arsenault7b49ad72017-01-23 19:08:34 +00002903 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, RHS, N0->getFlags());
Matt Arsenault4103a812017-01-12 00:23:20 +00002904 if (!N0.hasOneUse())
2905 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
2906 return Res;
2907 }
Matt Arsenault63f95372017-01-12 00:32:16 +00002908 case ISD::FMA:
2909 case ISD::FMAD: {
Matt Arsenault3e6f9b52017-01-19 06:35:27 +00002910 if (!mayIgnoreSignedZero(N0))
2911 return SDValue();
2912
Matt Arsenault63f95372017-01-12 00:32:16 +00002913 // (fneg (fma x, y, z)) -> (fma x, (fneg y), (fneg z))
2914 SDValue LHS = N0.getOperand(0);
2915 SDValue MHS = N0.getOperand(1);
2916 SDValue RHS = N0.getOperand(2);
2917
2918 if (LHS.getOpcode() == ISD::FNEG)
2919 LHS = LHS.getOperand(0);
2920 else if (MHS.getOpcode() == ISD::FNEG)
2921 MHS = MHS.getOperand(0);
2922 else
2923 MHS = DAG.getNode(ISD::FNEG, SL, VT, MHS);
2924
2925 if (RHS.getOpcode() != ISD::FNEG)
2926 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2927 else
2928 RHS = RHS.getOperand(0);
2929
2930 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, MHS, RHS);
2931 if (!N0.hasOneUse())
2932 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
2933 return Res;
2934 }
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00002935 case ISD::FP_EXTEND:
Matt Arsenault53f0cc22017-01-26 01:25:36 +00002936 case ISD::FTRUNC:
2937 case ISD::FRINT:
2938 case ISD::FNEARBYINT: // XXX - Should fround be handled?
2939 case ISD::FSIN:
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00002940 case AMDGPUISD::RCP:
Matt Arsenault31c039e2017-01-12 18:48:09 +00002941 case AMDGPUISD::RCP_LEGACY:
Matt Arsenault31c039e2017-01-12 18:48:09 +00002942 case AMDGPUISD::SIN_HW: {
Matt Arsenault98d2bf102017-01-12 17:46:28 +00002943 SDValue CvtSrc = N0.getOperand(0);
2944 if (CvtSrc.getOpcode() == ISD::FNEG) {
2945 // (fneg (fp_extend (fneg x))) -> (fp_extend x)
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00002946 // (fneg (rcp (fneg x))) -> (rcp x)
Matt Arsenault4242d482017-01-12 17:46:33 +00002947 return DAG.getNode(Opc, SL, VT, CvtSrc.getOperand(0));
Matt Arsenault98d2bf102017-01-12 17:46:28 +00002948 }
2949
2950 if (!N0.hasOneUse())
2951 return SDValue();
2952
2953 // (fneg (fp_extend x)) -> (fp_extend (fneg x))
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00002954 // (fneg (rcp x)) -> (rcp (fneg x))
Matt Arsenault98d2bf102017-01-12 17:46:28 +00002955 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc);
Matt Arsenault7b49ad72017-01-23 19:08:34 +00002956 return DAG.getNode(Opc, SL, VT, Neg, N0->getFlags());
Matt Arsenault4242d482017-01-12 17:46:33 +00002957 }
2958 case ISD::FP_ROUND: {
2959 SDValue CvtSrc = N0.getOperand(0);
2960
2961 if (CvtSrc.getOpcode() == ISD::FNEG) {
2962 // (fneg (fp_round (fneg x))) -> (fp_round x)
2963 return DAG.getNode(ISD::FP_ROUND, SL, VT,
2964 CvtSrc.getOperand(0), N0.getOperand(1));
2965 }
2966
2967 if (!N0.hasOneUse())
2968 return SDValue();
2969
2970 // (fneg (fp_round x)) -> (fp_round (fneg x))
2971 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc);
2972 return DAG.getNode(ISD::FP_ROUND, SL, VT, Neg, N0.getOperand(1));
Matt Arsenault98d2bf102017-01-12 17:46:28 +00002973 }
Matt Arsenault2529fba2017-01-12 00:09:34 +00002974 default:
2975 return SDValue();
2976 }
2977}
2978
Tom Stellard50122a52014-04-07 19:45:41 +00002979SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N,
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002980 DAGCombinerInfo &DCI) const {
Tom Stellard50122a52014-04-07 19:45:41 +00002981 SelectionDAG &DAG = DCI.DAG;
2982 SDLoc DL(N);
2983
2984 switch(N->getOpcode()) {
Matt Arsenault24e33d12015-07-03 23:33:38 +00002985 default:
2986 break;
Matt Arsenault79003342016-04-14 21:58:07 +00002987 case ISD::BITCAST: {
2988 EVT DestVT = N->getValueType(0);
Matt Arsenaultd99ef112016-09-17 15:44:16 +00002989
2990 // Push casts through vector builds. This helps avoid emitting a large
2991 // number of copies when materializing floating point vector constants.
2992 //
2993 // vNt1 bitcast (vNt0 (build_vector t0:x, t0:y)) =>
2994 // vnt1 = build_vector (t1 (bitcast t0:x)), (t1 (bitcast t0:y))
2995 if (DestVT.isVector()) {
2996 SDValue Src = N->getOperand(0);
2997 if (Src.getOpcode() == ISD::BUILD_VECTOR) {
2998 EVT SrcVT = Src.getValueType();
2999 unsigned NElts = DestVT.getVectorNumElements();
3000
3001 if (SrcVT.getVectorNumElements() == NElts) {
3002 EVT DestEltVT = DestVT.getVectorElementType();
3003
3004 SmallVector<SDValue, 8> CastedElts;
3005 SDLoc SL(N);
3006 for (unsigned I = 0, E = SrcVT.getVectorNumElements(); I != E; ++I) {
3007 SDValue Elt = Src.getOperand(I);
3008 CastedElts.push_back(DAG.getNode(ISD::BITCAST, DL, DestEltVT, Elt));
3009 }
3010
3011 return DAG.getBuildVector(DestVT, SL, CastedElts);
3012 }
3013 }
3014 }
3015
Matt Arsenault79003342016-04-14 21:58:07 +00003016 if (DestVT.getSizeInBits() != 64 && !DestVT.isVector())
3017 break;
3018
3019 // Fold bitcasts of constants.
3020 //
3021 // v2i32 (bitcast i64:k) -> build_vector lo_32(k), hi_32(k)
3022 // TODO: Generalize and move to DAGCombiner
3023 SDValue Src = N->getOperand(0);
3024 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src)) {
3025 assert(Src.getValueType() == MVT::i64);
3026 SDLoc SL(N);
3027 uint64_t CVal = C->getZExtValue();
3028 return DAG.getNode(ISD::BUILD_VECTOR, SL, DestVT,
3029 DAG.getConstant(Lo_32(CVal), SL, MVT::i32),
3030 DAG.getConstant(Hi_32(CVal), SL, MVT::i32));
3031 }
3032
3033 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Src)) {
3034 const APInt &Val = C->getValueAPF().bitcastToAPInt();
3035 SDLoc SL(N);
3036 uint64_t CVal = Val.getZExtValue();
3037 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
3038 DAG.getConstant(Lo_32(CVal), SL, MVT::i32),
3039 DAG.getConstant(Hi_32(CVal), SL, MVT::i32));
3040
3041 return DAG.getNode(ISD::BITCAST, SL, DestVT, Vec);
3042 }
3043
3044 break;
3045 }
Matt Arsenault24692112015-07-14 18:20:33 +00003046 case ISD::SHL: {
3047 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3048 break;
3049
3050 return performShlCombine(N, DCI);
3051 }
Matt Arsenault80edab92016-01-18 21:43:36 +00003052 case ISD::SRL: {
3053 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3054 break;
3055
3056 return performSrlCombine(N, DCI);
3057 }
Matt Arsenault33e3ece2016-01-18 22:09:04 +00003058 case ISD::SRA: {
3059 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3060 break;
3061
3062 return performSraCombine(N, DCI);
3063 }
Matt Arsenault24e33d12015-07-03 23:33:38 +00003064 case ISD::MUL:
3065 return performMulCombine(N, DCI);
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003066 case ISD::MULHS:
3067 return performMulhsCombine(N, DCI);
3068 case ISD::MULHU:
3069 return performMulhuCombine(N, DCI);
Matt Arsenault24e33d12015-07-03 23:33:38 +00003070 case AMDGPUISD::MUL_I24:
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003071 case AMDGPUISD::MUL_U24:
3072 case AMDGPUISD::MULHI_I24:
3073 case AMDGPUISD::MULHI_U24: {
Tom Stellard6c7dd982016-10-21 20:25:11 +00003074 // If the first call to simplify is successfull, then N may end up being
3075 // deleted, so we shouldn't call simplifyI24 again.
3076 simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI);
Matt Arsenault24e33d12015-07-03 23:33:38 +00003077 return SDValue();
3078 }
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003079 case AMDGPUISD::MUL_LOHI_I24:
3080 case AMDGPUISD::MUL_LOHI_U24:
3081 return performMulLoHi24Combine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00003082 case ISD::SELECT:
3083 return performSelectCombine(N, DCI);
Matt Arsenault2529fba2017-01-12 00:09:34 +00003084 case ISD::FNEG:
3085 return performFNegCombine(N, DCI);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003086 case AMDGPUISD::BFE_I32:
3087 case AMDGPUISD::BFE_U32: {
3088 assert(!N->getValueType(0).isVector() &&
3089 "Vector handling of BFE not implemented");
3090 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
3091 if (!Width)
3092 break;
3093
3094 uint32_t WidthVal = Width->getZExtValue() & 0x1f;
3095 if (WidthVal == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003096 return DAG.getConstant(0, DL, MVT::i32);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003097
3098 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
3099 if (!Offset)
3100 break;
3101
3102 SDValue BitsFrom = N->getOperand(0);
3103 uint32_t OffsetVal = Offset->getZExtValue() & 0x1f;
3104
3105 bool Signed = N->getOpcode() == AMDGPUISD::BFE_I32;
3106
3107 if (OffsetVal == 0) {
3108 // This is already sign / zero extended, so try to fold away extra BFEs.
3109 unsigned SignBits = Signed ? (32 - WidthVal + 1) : (32 - WidthVal);
3110
3111 unsigned OpSignBits = DAG.ComputeNumSignBits(BitsFrom);
3112 if (OpSignBits >= SignBits)
3113 return BitsFrom;
Matt Arsenault05e96f42014-05-22 18:09:12 +00003114
3115 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), WidthVal);
3116 if (Signed) {
3117 // This is a sign_extend_inreg. Replace it to take advantage of existing
3118 // DAG Combines. If not eliminated, we will match back to BFE during
3119 // selection.
3120
3121 // TODO: The sext_inreg of extended types ends, although we can could
3122 // handle them in a single BFE.
3123 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, BitsFrom,
3124 DAG.getValueType(SmallVT));
3125 }
3126
3127 return DAG.getZeroExtendInReg(BitsFrom, DL, SmallVT);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003128 }
3129
Matt Arsenaultf1794202014-10-15 05:07:00 +00003130 if (ConstantSDNode *CVal = dyn_cast<ConstantSDNode>(BitsFrom)) {
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003131 if (Signed) {
3132 return constantFoldBFE<int32_t>(DAG,
Matt Arsenault46cbc432014-09-19 00:42:06 +00003133 CVal->getSExtValue(),
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003134 OffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003135 WidthVal,
3136 DL);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003137 }
3138
3139 return constantFoldBFE<uint32_t>(DAG,
Matt Arsenault6462f942014-09-18 15:52:26 +00003140 CVal->getZExtValue(),
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003141 OffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003142 WidthVal,
3143 DL);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003144 }
3145
Matt Arsenault05e96f42014-05-22 18:09:12 +00003146 if ((OffsetVal + WidthVal) >= 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003147 SDValue ShiftVal = DAG.getConstant(OffsetVal, DL, MVT::i32);
Matt Arsenault05e96f42014-05-22 18:09:12 +00003148 return DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, MVT::i32,
3149 BitsFrom, ShiftVal);
3150 }
3151
Matt Arsenault7b68fdf2014-10-15 17:58:34 +00003152 if (BitsFrom.hasOneUse()) {
Matt Arsenault6de7af42014-10-15 23:37:42 +00003153 APInt Demanded = APInt::getBitsSet(32,
3154 OffsetVal,
3155 OffsetVal + WidthVal);
3156
Matt Arsenault7b68fdf2014-10-15 17:58:34 +00003157 APInt KnownZero, KnownOne;
3158 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
3159 !DCI.isBeforeLegalizeOps());
3160 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3161 if (TLO.ShrinkDemandedConstant(BitsFrom, Demanded) ||
3162 TLI.SimplifyDemandedBits(BitsFrom, Demanded,
3163 KnownZero, KnownOne, TLO)) {
3164 DCI.CommitTargetLoweringOpt(TLO);
3165 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003166 }
3167
3168 break;
3169 }
Matt Arsenault327bb5a2016-07-01 22:47:50 +00003170 case ISD::LOAD:
3171 return performLoadCombine(N, DCI);
Matt Arsenaultca3976f2014-07-15 02:06:31 +00003172 case ISD::STORE:
3173 return performStoreCombine(N, DCI);
Tom Stellard50122a52014-04-07 19:45:41 +00003174 }
3175 return SDValue();
3176}
3177
3178//===----------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +00003179// Helper functions
3180//===----------------------------------------------------------------------===//
3181
Tom Stellard75aadc22012-12-11 21:25:42 +00003182SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
3183 const TargetRegisterClass *RC,
3184 unsigned Reg, EVT VT) const {
3185 MachineFunction &MF = DAG.getMachineFunction();
3186 MachineRegisterInfo &MRI = MF.getRegInfo();
3187 unsigned VirtualRegister;
3188 if (!MRI.isLiveIn(Reg)) {
3189 VirtualRegister = MRI.createVirtualRegister(RC);
3190 MRI.addLiveIn(Reg, VirtualRegister);
3191 } else {
3192 VirtualRegister = MRI.getLiveInVirtReg(Reg);
3193 }
3194 return DAG.getRegister(VirtualRegister, VT);
3195}
3196
Tom Stellarddcb9f092015-07-09 21:20:37 +00003197uint32_t AMDGPUTargetLowering::getImplicitParameterOffset(
3198 const AMDGPUMachineFunction *MFI, const ImplicitParameter Param) const {
Tom Stellardb2869eb2016-09-09 19:28:00 +00003199 unsigned Alignment = Subtarget->getAlignmentForImplicitArgPtr();
3200 uint64_t ArgOffset = alignTo(MFI->getABIArgOffset(), Alignment);
Tom Stellarddcb9f092015-07-09 21:20:37 +00003201 switch (Param) {
3202 case GRID_DIM:
3203 return ArgOffset;
3204 case GRID_OFFSET:
3205 return ArgOffset + 4;
3206 }
3207 llvm_unreachable("unexpected implicit parameter type");
3208}
3209
Tom Stellard75aadc22012-12-11 21:25:42 +00003210#define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
3211
3212const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00003213 switch ((AMDGPUISD::NodeType)Opcode) {
3214 case AMDGPUISD::FIRST_NUMBER: break;
Tom Stellard75aadc22012-12-11 21:25:42 +00003215 // AMDIL DAG nodes
Tom Stellard75aadc22012-12-11 21:25:42 +00003216 NODE_NAME_CASE(CALL);
3217 NODE_NAME_CASE(UMUL);
Tom Stellard75aadc22012-12-11 21:25:42 +00003218 NODE_NAME_CASE(BRANCH_COND);
3219
3220 // AMDGPU DAG nodes
Matt Arsenault9babdf42016-06-22 20:15:28 +00003221 NODE_NAME_CASE(ENDPGM)
3222 NODE_NAME_CASE(RETURN)
Tom Stellard75aadc22012-12-11 21:25:42 +00003223 NODE_NAME_CASE(DWORDADDR)
3224 NODE_NAME_CASE(FRACT)
Wei Ding07e03712016-07-28 16:42:13 +00003225 NODE_NAME_CASE(SETCC)
Tom Stellard8485fa02016-12-07 02:42:15 +00003226 NODE_NAME_CASE(SETREG)
3227 NODE_NAME_CASE(FMA_W_CHAIN)
3228 NODE_NAME_CASE(FMUL_W_CHAIN)
Matt Arsenault5d47d4a2014-06-12 21:15:44 +00003229 NODE_NAME_CASE(CLAMP)
Matthias Braund04893f2015-05-07 21:33:59 +00003230 NODE_NAME_CASE(COS_HW)
3231 NODE_NAME_CASE(SIN_HW)
Matt Arsenaultda59f3d2014-11-13 23:03:09 +00003232 NODE_NAME_CASE(FMAX_LEGACY)
Matt Arsenaultda59f3d2014-11-13 23:03:09 +00003233 NODE_NAME_CASE(FMIN_LEGACY)
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003234 NODE_NAME_CASE(FMAX3)
3235 NODE_NAME_CASE(SMAX3)
3236 NODE_NAME_CASE(UMAX3)
3237 NODE_NAME_CASE(FMIN3)
3238 NODE_NAME_CASE(SMIN3)
3239 NODE_NAME_CASE(UMIN3)
Matt Arsenaultf639c322016-01-28 20:53:42 +00003240 NODE_NAME_CASE(FMED3)
3241 NODE_NAME_CASE(SMED3)
3242 NODE_NAME_CASE(UMED3)
Matt Arsenaulta0050b02014-06-19 01:19:19 +00003243 NODE_NAME_CASE(URECIP)
3244 NODE_NAME_CASE(DIV_SCALE)
3245 NODE_NAME_CASE(DIV_FMAS)
3246 NODE_NAME_CASE(DIV_FIXUP)
3247 NODE_NAME_CASE(TRIG_PREOP)
3248 NODE_NAME_CASE(RCP)
3249 NODE_NAME_CASE(RSQ)
Matt Arsenault32fc5272016-07-26 16:45:45 +00003250 NODE_NAME_CASE(RCP_LEGACY)
Matt Arsenault257d48d2014-06-24 22:13:39 +00003251 NODE_NAME_CASE(RSQ_LEGACY)
Matt Arsenault32fc5272016-07-26 16:45:45 +00003252 NODE_NAME_CASE(FMUL_LEGACY)
Matt Arsenault79963e82016-02-13 01:03:00 +00003253 NODE_NAME_CASE(RSQ_CLAMP)
Matt Arsenault2e7cc482014-08-15 17:30:25 +00003254 NODE_NAME_CASE(LDEXP)
Matt Arsenault4831ce52015-01-06 23:00:37 +00003255 NODE_NAME_CASE(FP_CLASS)
Matt Arsenaulta0050b02014-06-19 01:19:19 +00003256 NODE_NAME_CASE(DOT4)
Matthias Braund04893f2015-05-07 21:33:59 +00003257 NODE_NAME_CASE(CARRY)
3258 NODE_NAME_CASE(BORROW)
Matt Arsenaultfae02982014-03-17 18:58:11 +00003259 NODE_NAME_CASE(BFE_U32)
3260 NODE_NAME_CASE(BFE_I32)
Matt Arsenaultb3458362014-03-31 18:21:13 +00003261 NODE_NAME_CASE(BFI)
3262 NODE_NAME_CASE(BFM)
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00003263 NODE_NAME_CASE(FFBH_U32)
Matt Arsenaultb51dcb92016-07-18 18:40:51 +00003264 NODE_NAME_CASE(FFBH_I32)
Tom Stellard50122a52014-04-07 19:45:41 +00003265 NODE_NAME_CASE(MUL_U24)
3266 NODE_NAME_CASE(MUL_I24)
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003267 NODE_NAME_CASE(MULHI_U24)
3268 NODE_NAME_CASE(MULHI_I24)
3269 NODE_NAME_CASE(MUL_LOHI_U24)
3270 NODE_NAME_CASE(MUL_LOHI_I24)
Matt Arsenaulteb260202014-05-22 18:00:15 +00003271 NODE_NAME_CASE(MAD_U24)
3272 NODE_NAME_CASE(MAD_I24)
Matthias Braund04893f2015-05-07 21:33:59 +00003273 NODE_NAME_CASE(TEXTURE_FETCH)
Tom Stellard75aadc22012-12-11 21:25:42 +00003274 NODE_NAME_CASE(EXPORT)
Matt Arsenault7bee6ac2016-12-05 20:23:10 +00003275 NODE_NAME_CASE(EXPORT_DONE)
3276 NODE_NAME_CASE(R600_EXPORT)
Tom Stellardff62c352013-01-23 02:09:03 +00003277 NODE_NAME_CASE(CONST_ADDRESS)
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00003278 NODE_NAME_CASE(REGISTER_LOAD)
3279 NODE_NAME_CASE(REGISTER_STORE)
Tom Stellard9fa17912013-08-14 23:24:45 +00003280 NODE_NAME_CASE(LOAD_INPUT)
3281 NODE_NAME_CASE(SAMPLE)
3282 NODE_NAME_CASE(SAMPLEB)
3283 NODE_NAME_CASE(SAMPLED)
3284 NODE_NAME_CASE(SAMPLEL)
Matt Arsenault364a6742014-06-11 17:50:44 +00003285 NODE_NAME_CASE(CVT_F32_UBYTE0)
3286 NODE_NAME_CASE(CVT_F32_UBYTE1)
3287 NODE_NAME_CASE(CVT_F32_UBYTE2)
3288 NODE_NAME_CASE(CVT_F32_UBYTE3)
Tom Stellard880a80a2014-06-17 16:53:14 +00003289 NODE_NAME_CASE(BUILD_VERTICAL_VECTOR)
Tom Stellard067c8152014-07-21 14:01:14 +00003290 NODE_NAME_CASE(CONST_DATA_PTR)
Tom Stellardbf3e6e52016-06-14 20:29:59 +00003291 NODE_NAME_CASE(PC_ADD_REL_OFFSET)
Matt Arsenault03006fd2016-07-19 16:27:56 +00003292 NODE_NAME_CASE(KILL)
Jan Veselyf1705042017-01-20 21:24:26 +00003293 NODE_NAME_CASE(DUMMY_CHAIN)
Matthias Braund04893f2015-05-07 21:33:59 +00003294 case AMDGPUISD::FIRST_MEM_OPCODE_NUMBER: break;
Tom Stellardfc92e772015-05-12 14:18:14 +00003295 NODE_NAME_CASE(SENDMSG)
Jan Veselyd48445d2017-01-04 18:06:55 +00003296 NODE_NAME_CASE(SENDMSGHALT)
Tom Stellard2a9d9472015-05-12 15:00:46 +00003297 NODE_NAME_CASE(INTERP_MOV)
3298 NODE_NAME_CASE(INTERP_P1)
3299 NODE_NAME_CASE(INTERP_P2)
Tom Stellardd3ee8c12013-08-16 01:12:06 +00003300 NODE_NAME_CASE(STORE_MSKOR)
Matt Arsenaultdfaf4262016-04-25 19:27:09 +00003301 NODE_NAME_CASE(LOAD_CONSTANT)
Tom Stellardafcf12f2013-09-12 02:55:14 +00003302 NODE_NAME_CASE(TBUFFER_STORE_FORMAT)
Tom Stellard354a43c2016-04-01 18:27:37 +00003303 NODE_NAME_CASE(ATOMIC_CMP_SWAP)
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00003304 NODE_NAME_CASE(ATOMIC_INC)
3305 NODE_NAME_CASE(ATOMIC_DEC)
Tom Stellard6f9ef142016-12-20 17:19:44 +00003306 NODE_NAME_CASE(BUFFER_LOAD)
3307 NODE_NAME_CASE(BUFFER_LOAD_FORMAT)
Matthias Braund04893f2015-05-07 21:33:59 +00003308 case AMDGPUISD::LAST_AMDGPU_ISD_NUMBER: break;
Tom Stellard75aadc22012-12-11 21:25:42 +00003309 }
Matthias Braund04893f2015-05-07 21:33:59 +00003310 return nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +00003311}
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003312
Evandro Menezes21f9ce12016-11-10 23:31:06 +00003313SDValue AMDGPUTargetLowering::getSqrtEstimate(SDValue Operand,
3314 SelectionDAG &DAG, int Enabled,
3315 int &RefinementSteps,
3316 bool &UseOneConstNR,
3317 bool Reciprocal) const {
Matt Arsenaulte93d06a2015-01-13 20:53:18 +00003318 EVT VT = Operand.getValueType();
3319
3320 if (VT == MVT::f32) {
3321 RefinementSteps = 0;
3322 return DAG.getNode(AMDGPUISD::RSQ, SDLoc(Operand), VT, Operand);
3323 }
3324
3325 // TODO: There is also f64 rsq instruction, but the documentation is less
3326 // clear on its precision.
3327
3328 return SDValue();
3329}
3330
Matt Arsenaultbf0db912015-01-13 20:53:23 +00003331SDValue AMDGPUTargetLowering::getRecipEstimate(SDValue Operand,
Sanjay Patel0051efc2016-10-20 16:55:45 +00003332 SelectionDAG &DAG, int Enabled,
3333 int &RefinementSteps) const {
Matt Arsenaultbf0db912015-01-13 20:53:23 +00003334 EVT VT = Operand.getValueType();
3335
3336 if (VT == MVT::f32) {
3337 // Reciprocal, < 1 ulp error.
3338 //
3339 // This reciprocal approximation converges to < 0.5 ulp error with one
3340 // newton rhapson performed with two fused multiple adds (FMAs).
3341
3342 RefinementSteps = 0;
3343 return DAG.getNode(AMDGPUISD::RCP, SDLoc(Operand), VT, Operand);
3344 }
3345
3346 // TODO: There is also f64 rcp instruction, but the documentation is less
3347 // clear on its precision.
3348
3349 return SDValue();
3350}
3351
Jay Foada0653a32014-05-14 21:14:37 +00003352void AMDGPUTargetLowering::computeKnownBitsForTargetNode(
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003353 const SDValue Op,
3354 APInt &KnownZero,
3355 APInt &KnownOne,
3356 const SelectionDAG &DAG,
3357 unsigned Depth) const {
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003358
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003359 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0); // Don't know anything.
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003360
3361 APInt KnownZero2;
3362 APInt KnownOne2;
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003363 unsigned Opc = Op.getOpcode();
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003364
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003365 switch (Opc) {
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003366 default:
3367 break;
Jan Vesely808fff52015-04-30 17:15:56 +00003368 case AMDGPUISD::CARRY:
3369 case AMDGPUISD::BORROW: {
3370 KnownZero = APInt::getHighBitsSet(32, 31);
3371 break;
3372 }
3373
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003374 case AMDGPUISD::BFE_I32:
3375 case AMDGPUISD::BFE_U32: {
3376 ConstantSDNode *CWidth = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3377 if (!CWidth)
3378 return;
3379
3380 unsigned BitWidth = 32;
3381 uint32_t Width = CWidth->getZExtValue() & 0x1f;
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003382
Matt Arsenaulta3fe7c62014-10-16 20:07:40 +00003383 if (Opc == AMDGPUISD::BFE_U32)
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003384 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - Width);
3385
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003386 break;
3387 }
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003388 }
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003389}
Matt Arsenaultbf8694d2014-05-22 18:09:03 +00003390
3391unsigned AMDGPUTargetLowering::ComputeNumSignBitsForTargetNode(
3392 SDValue Op,
3393 const SelectionDAG &DAG,
3394 unsigned Depth) const {
3395 switch (Op.getOpcode()) {
3396 case AMDGPUISD::BFE_I32: {
3397 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3398 if (!Width)
3399 return 1;
3400
3401 unsigned SignBits = 32 - Width->getZExtValue() + 1;
Artyom Skrobov314ee042015-11-25 19:41:11 +00003402 if (!isNullConstant(Op.getOperand(1)))
Matt Arsenaultbf8694d2014-05-22 18:09:03 +00003403 return SignBits;
3404
3405 // TODO: Could probably figure something out with non-0 offsets.
3406 unsigned Op0SignBits = DAG.ComputeNumSignBits(Op.getOperand(0), Depth + 1);
3407 return std::max(SignBits, Op0SignBits);
3408 }
3409
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003410 case AMDGPUISD::BFE_U32: {
3411 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3412 return Width ? 32 - (Width->getZExtValue() & 0x1f) : 1;
3413 }
3414
Jan Vesely808fff52015-04-30 17:15:56 +00003415 case AMDGPUISD::CARRY:
3416 case AMDGPUISD::BORROW:
3417 return 31;
3418
Matt Arsenaultbf8694d2014-05-22 18:09:03 +00003419 default:
3420 return 1;
3421 }
3422}