blob: f28afa89bd279da06d80cb190136b4b96ebb6379 [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 Dave93f9d5c2017-02-02 18:24:55 +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 Arsenault0699ef32017-02-09 22:00:42 +0000464 // memcpy/memmove/memset are expanded in the IR, so we shouldn't need to worry
465 // about these during lowering.
466 MaxStoresPerMemcpy = 0xffffffff;
467 MaxStoresPerMemmove = 0xffffffff;
468 MaxStoresPerMemset = 0xffffffff;
Matt Arsenault71e66762016-05-21 02:27:49 +0000469
470 setTargetDAGCombine(ISD::BITCAST);
Matt Arsenault71e66762016-05-21 02:27:49 +0000471 setTargetDAGCombine(ISD::SHL);
472 setTargetDAGCombine(ISD::SRA);
473 setTargetDAGCombine(ISD::SRL);
474 setTargetDAGCombine(ISD::MUL);
Matt Arsenault2712d4a2016-08-27 01:32:27 +0000475 setTargetDAGCombine(ISD::MULHU);
476 setTargetDAGCombine(ISD::MULHS);
Matt Arsenault71e66762016-05-21 02:27:49 +0000477 setTargetDAGCombine(ISD::SELECT);
478 setTargetDAGCombine(ISD::SELECT_CC);
479 setTargetDAGCombine(ISD::STORE);
480 setTargetDAGCombine(ISD::FADD);
481 setTargetDAGCombine(ISD::FSUB);
Matt Arsenault2529fba2017-01-12 00:09:34 +0000482 setTargetDAGCombine(ISD::FNEG);
Matt Arsenault9dba9bd2017-02-02 02:27:04 +0000483 setTargetDAGCombine(ISD::FABS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000484}
485
Tom Stellard28d06de2013-08-05 22:22:07 +0000486//===----------------------------------------------------------------------===//
487// Target Information
488//===----------------------------------------------------------------------===//
489
Matt Arsenaulta8fcfad2017-02-02 23:21:23 +0000490LLVM_READNONE
Matt Arsenault45337df2017-01-12 18:58:15 +0000491static bool fnegFoldsIntoOp(unsigned Opc) {
492 switch (Opc) {
493 case ISD::FADD:
494 case ISD::FSUB:
495 case ISD::FMUL:
496 case ISD::FMA:
497 case ISD::FMAD:
Matt Arsenault2511c032017-02-03 00:23:15 +0000498 case ISD::FMINNUM:
499 case ISD::FMAXNUM:
Matt Arsenault45337df2017-01-12 18:58:15 +0000500 case ISD::FSIN:
Matt Arsenault53f0cc22017-01-26 01:25:36 +0000501 case ISD::FTRUNC:
502 case ISD::FRINT:
503 case ISD::FNEARBYINT:
Matt Arsenault45337df2017-01-12 18:58:15 +0000504 case AMDGPUISD::RCP:
505 case AMDGPUISD::RCP_LEGACY:
506 case AMDGPUISD::SIN_HW:
507 case AMDGPUISD::FMUL_LEGACY:
Matt Arsenaulte1b59532017-02-03 00:51:50 +0000508 case AMDGPUISD::FMIN_LEGACY:
509 case AMDGPUISD::FMAX_LEGACY:
Matt Arsenault45337df2017-01-12 18:58:15 +0000510 return true;
511 default:
512 return false;
513 }
514}
515
Matt Arsenaulta8fcfad2017-02-02 23:21:23 +0000516/// \p returns true if the operation will definitely need to use a 64-bit
517/// encoding, and thus will use a VOP3 encoding regardless of the source
518/// modifiers.
519LLVM_READONLY
520static bool opMustUseVOP3Encoding(const SDNode *N, MVT VT) {
521 return N->getNumOperands() > 2 || VT == MVT::f64;
522}
523
524// Most FP instructions support source modifiers, but this could be refined
525// slightly.
526LLVM_READONLY
527static bool hasSourceMods(const SDNode *N) {
528 if (isa<MemSDNode>(N))
529 return false;
530
531 switch (N->getOpcode()) {
532 case ISD::CopyToReg:
533 case ISD::SELECT:
534 case ISD::FDIV:
535 case ISD::FREM:
536 case ISD::INLINEASM:
537 case AMDGPUISD::INTERP_P1:
538 case AMDGPUISD::INTERP_P2:
539 case AMDGPUISD::DIV_SCALE:
540 return false;
541 default:
542 return true;
543 }
544}
545
546static bool allUsesHaveSourceMods(const SDNode *N, unsigned CostThreshold = 4) {
547 // Some users (such as 3-operand FMA/MAD) must use a VOP3 encoding, and thus
548 // it is truly free to use a source modifier in all cases. If there are
549 // multiple users but for each one will necessitate using VOP3, there will be
550 // a code size increase. Try to avoid increasing code size unless we know it
551 // will save on the instruction count.
552 unsigned NumMayIncreaseSize = 0;
553 MVT VT = N->getValueType(0).getScalarType().getSimpleVT();
554
555 // XXX - Should this limit number of uses to check?
556 for (const SDNode *U : N->uses()) {
557 if (!hasSourceMods(U))
558 return false;
559
560 if (!opMustUseVOP3Encoding(U, VT)) {
561 if (++NumMayIncreaseSize > CostThreshold)
562 return false;
563 }
564 }
565
566 return true;
567}
568
Mehdi Amini44ede332015-07-09 02:09:04 +0000569MVT AMDGPUTargetLowering::getVectorIdxTy(const DataLayout &) const {
Tom Stellard28d06de2013-08-05 22:22:07 +0000570 return MVT::i32;
571}
572
Matt Arsenaultd5f91fd2014-06-23 18:00:52 +0000573bool AMDGPUTargetLowering::isSelectSupported(SelectSupportKind SelType) const {
574 return true;
575}
576
Matt Arsenault14d46452014-06-15 20:23:38 +0000577// The backend supports 32 and 64 bit floating point immediates.
578// FIXME: Why are we reporting vectors of FP immediates as legal?
579bool AMDGPUTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
580 EVT ScalarVT = VT.getScalarType();
Matt Arsenault4e55c1e2016-12-22 03:05:30 +0000581 return (ScalarVT == MVT::f32 || ScalarVT == MVT::f64 ||
582 (ScalarVT == MVT::f16 && Subtarget->has16BitInsts()));
Matt Arsenault14d46452014-06-15 20:23:38 +0000583}
584
585// We don't want to shrink f64 / f32 constants.
586bool AMDGPUTargetLowering::ShouldShrinkFPConstant(EVT VT) const {
587 EVT ScalarVT = VT.getScalarType();
588 return (ScalarVT != MVT::f32 && ScalarVT != MVT::f64);
589}
590
Matt Arsenault810cb622014-12-12 00:00:24 +0000591bool AMDGPUTargetLowering::shouldReduceLoadWidth(SDNode *N,
592 ISD::LoadExtType,
593 EVT NewVT) const {
594
595 unsigned NewSize = NewVT.getStoreSizeInBits();
596
597 // If we are reducing to a 32-bit load, this is always better.
598 if (NewSize == 32)
599 return true;
600
601 EVT OldVT = N->getValueType(0);
602 unsigned OldSize = OldVT.getStoreSizeInBits();
603
604 // Don't produce extloads from sub 32-bit types. SI doesn't have scalar
605 // extloads, so doing one requires using a buffer_load. In cases where we
606 // still couldn't use a scalar load, using the wider load shouldn't really
607 // hurt anything.
608
609 // If the old size already had to be an extload, there's no harm in continuing
610 // to reduce the width.
611 return (OldSize < 32);
612}
613
Matt Arsenaultc5559bb2013-11-15 04:42:23 +0000614bool AMDGPUTargetLowering::isLoadBitCastBeneficial(EVT LoadTy,
615 EVT CastTy) const {
Matt Arsenaultc5559bb2013-11-15 04:42:23 +0000616
Matt Arsenault327bb5a2016-07-01 22:47:50 +0000617 assert(LoadTy.getSizeInBits() == CastTy.getSizeInBits());
Matt Arsenaultc5559bb2013-11-15 04:42:23 +0000618
Matt Arsenault327bb5a2016-07-01 22:47:50 +0000619 if (LoadTy.getScalarType() == MVT::i32)
620 return false;
621
622 unsigned LScalarSize = LoadTy.getScalarSizeInBits();
623 unsigned CastScalarSize = CastTy.getScalarSizeInBits();
624
625 return (LScalarSize < CastScalarSize) ||
626 (CastScalarSize >= 32);
Matt Arsenaultc5559bb2013-11-15 04:42:23 +0000627}
Tom Stellard28d06de2013-08-05 22:22:07 +0000628
Matt Arsenaultb56d8432015-01-13 19:46:48 +0000629// SI+ has instructions for cttz / ctlz for 32-bit values. This is probably also
630// profitable with the expansion for 64-bit since it's generally good to
631// speculate things.
632// FIXME: These should really have the size as a parameter.
633bool AMDGPUTargetLowering::isCheapToSpeculateCttz() const {
634 return true;
635}
636
637bool AMDGPUTargetLowering::isCheapToSpeculateCtlz() const {
638 return true;
639}
640
Tom Stellard75aadc22012-12-11 21:25:42 +0000641//===---------------------------------------------------------------------===//
Tom Stellardc54731a2013-07-23 23:55:03 +0000642// Target Properties
643//===---------------------------------------------------------------------===//
644
645bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const {
646 assert(VT.isFloatingPoint());
Matt Arsenaultc79dc702016-11-15 02:25:28 +0000647 return VT == MVT::f32 || VT == MVT::f64 || (Subtarget->has16BitInsts() &&
648 VT == MVT::f16);
Tom Stellardc54731a2013-07-23 23:55:03 +0000649}
650
651bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
Matt Arsenaultc79dc702016-11-15 02:25:28 +0000652 return isFAbsFree(VT);
Tom Stellardc54731a2013-07-23 23:55:03 +0000653}
654
Matt Arsenault65ad1602015-05-24 00:51:27 +0000655bool AMDGPUTargetLowering:: storeOfVectorConstantIsCheap(EVT MemVT,
656 unsigned NumElem,
657 unsigned AS) const {
658 return true;
659}
660
Matt Arsenault61dc2352015-10-12 23:59:50 +0000661bool AMDGPUTargetLowering::aggressivelyPreferBuildVectorSources(EVT VecVT) const {
662 // There are few operations which truly have vector input operands. Any vector
663 // operation is going to involve operations on each component, and a
664 // build_vector will be a copy per element, so it always makes sense to use a
665 // build_vector input in place of the extracted element to avoid a copy into a
666 // super register.
667 //
668 // We should probably only do this if all users are extracts only, but this
669 // should be the common case.
670 return true;
671}
672
Benjamin Kramer53f9df42014-02-12 10:17:54 +0000673bool AMDGPUTargetLowering::isTruncateFree(EVT Source, EVT Dest) const {
Matt Arsenault0cdcd962014-02-10 19:57:42 +0000674 // Truncate is just accessing a subregister.
Tom Stellard115a6152016-11-10 16:02:37 +0000675
676 unsigned SrcSize = Source.getSizeInBits();
677 unsigned DestSize = Dest.getSizeInBits();
678
679 return DestSize < SrcSize && DestSize % 32 == 0 ;
Benjamin Kramer53f9df42014-02-12 10:17:54 +0000680}
681
682bool AMDGPUTargetLowering::isTruncateFree(Type *Source, Type *Dest) const {
683 // Truncate is just accessing a subregister.
Tom Stellard115a6152016-11-10 16:02:37 +0000684
685 unsigned SrcSize = Source->getScalarSizeInBits();
686 unsigned DestSize = Dest->getScalarSizeInBits();
687
688 if (DestSize== 16 && Subtarget->has16BitInsts())
689 return SrcSize >= 32;
690
691 return DestSize < SrcSize && DestSize % 32 == 0;
Matt Arsenault0cdcd962014-02-10 19:57:42 +0000692}
693
Matt Arsenaultb517c812014-03-27 17:23:31 +0000694bool AMDGPUTargetLowering::isZExtFree(Type *Src, Type *Dest) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000695 unsigned SrcSize = Src->getScalarSizeInBits();
696 unsigned DestSize = Dest->getScalarSizeInBits();
Matt Arsenaultb517c812014-03-27 17:23:31 +0000697
Tom Stellard115a6152016-11-10 16:02:37 +0000698 if (SrcSize == 16 && Subtarget->has16BitInsts())
699 return DestSize >= 32;
700
Matt Arsenaultb517c812014-03-27 17:23:31 +0000701 return SrcSize == 32 && DestSize == 64;
702}
703
704bool AMDGPUTargetLowering::isZExtFree(EVT Src, EVT Dest) const {
705 // Any register load of a 64-bit value really requires 2 32-bit moves. For all
706 // practical purposes, the extra mov 0 to load a 64-bit is free. As used,
707 // this will enable reducing 64-bit operations the 32-bit, which is always
708 // good.
Tom Stellard115a6152016-11-10 16:02:37 +0000709
710 if (Src == MVT::i16)
711 return Dest == MVT::i32 ||Dest == MVT::i64 ;
712
Matt Arsenaultb517c812014-03-27 17:23:31 +0000713 return Src == MVT::i32 && Dest == MVT::i64;
714}
715
Aaron Ballman3c81e462014-06-26 13:45:47 +0000716bool AMDGPUTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
717 return isZExtFree(Val.getValueType(), VT2);
718}
719
Matt Arsenaulta7f1e0c2014-03-24 19:43:31 +0000720bool AMDGPUTargetLowering::isNarrowingProfitable(EVT SrcVT, EVT DestVT) const {
721 // There aren't really 64-bit registers, but pairs of 32-bit ones and only a
722 // limited number of native 64-bit operations. Shrinking an operation to fit
723 // in a single 32-bit register should always be helpful. As currently used,
724 // this is much less general than the name suggests, and is only used in
725 // places trying to reduce the sizes of loads. Shrinking loads to < 32-bits is
726 // not profitable, and may actually be harmful.
727 return SrcVT.getSizeInBits() > 32 && DestVT.getSizeInBits() == 32;
728}
729
Tom Stellardc54731a2013-07-23 23:55:03 +0000730//===---------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +0000731// TargetLowering Callbacks
732//===---------------------------------------------------------------------===//
733
Tom Stellardca166212017-01-30 21:56:46 +0000734CCAssignFn *AMDGPUCallLowering::CCAssignFnForCall(CallingConv::ID CC,
735 bool IsVarArg) const {
736 return CC_AMDGPU;
737}
738
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000739/// The SelectionDAGBuilder will automatically promote function arguments
740/// with illegal types. However, this does not work for the AMDGPU targets
741/// since the function arguments are stored in memory as these illegal types.
742/// In order to handle this properly we need to get the original types sizes
743/// from the LLVM IR Function and fixup the ISD:InputArg values before
744/// passing them to AnalyzeFormalArguments()
Christian Konig2c8f6d52013-03-07 09:03:52 +0000745
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000746/// When the SelectionDAGBuilder computes the Ins, it takes care of splitting
747/// input values across multiple registers. Each item in the Ins array
748/// represents a single value that will be stored in regsters. Ins[x].VT is
749/// the value type of the value that will be stored in the register, so
750/// whatever SDNode we lower the argument to needs to be this type.
751///
752/// In order to correctly lower the arguments we need to know the size of each
753/// argument. Since Ins[x].VT gives us the size of the register that will
754/// hold the value, we need to look at Ins[x].ArgVT to see the 'real' type
755/// for the orignal function argument so that we can deduce the correct memory
756/// type to use for Ins[x]. In most cases the correct memory type will be
757/// Ins[x].ArgVT. However, this will not always be the case. If, for example,
758/// we have a kernel argument of type v8i8, this argument will be split into
759/// 8 parts and each part will be represented by its own item in the Ins array.
760/// For each part the Ins[x].ArgVT will be the v8i8, which is the full type of
761/// the argument before it was split. From this, we deduce that the memory type
762/// for each individual part is i8. We pass the memory type as LocVT to the
763/// calling convention analysis function and the register type (Ins[x].VT) as
764/// the ValVT.
765void AMDGPUTargetLowering::analyzeFormalArgumentsCompute(CCState &State,
766 const SmallVectorImpl<ISD::InputArg> &Ins) const {
767 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
768 const ISD::InputArg &In = Ins[i];
769 EVT MemVT;
770
771 unsigned NumRegs = getNumRegisters(State.getContext(), In.ArgVT);
772
Tom Stellard7998db62016-09-16 22:20:24 +0000773 if (!Subtarget->isAmdHsaOS() &&
774 (In.ArgVT == MVT::i16 || In.ArgVT == MVT::i8 || In.ArgVT == MVT::f16)) {
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000775 // The ABI says the caller will extend these values to 32-bits.
776 MemVT = In.ArgVT.isInteger() ? MVT::i32 : MVT::f32;
777 } else if (NumRegs == 1) {
778 // This argument is not split, so the IR type is the memory type.
779 assert(!In.Flags.isSplit());
780 if (In.ArgVT.isExtended()) {
781 // We have an extended type, like i24, so we should just use the register type
782 MemVT = In.VT;
783 } else {
784 MemVT = In.ArgVT;
785 }
786 } else if (In.ArgVT.isVector() && In.VT.isVector() &&
787 In.ArgVT.getScalarType() == In.VT.getScalarType()) {
788 assert(In.ArgVT.getVectorNumElements() > In.VT.getVectorNumElements());
789 // We have a vector value which has been split into a vector with
790 // the same scalar type, but fewer elements. This should handle
791 // all the floating-point vector types.
792 MemVT = In.VT;
793 } else if (In.ArgVT.isVector() &&
794 In.ArgVT.getVectorNumElements() == NumRegs) {
795 // This arg has been split so that each element is stored in a separate
796 // register.
797 MemVT = In.ArgVT.getScalarType();
798 } else if (In.ArgVT.isExtended()) {
799 // We have an extended type, like i65.
800 MemVT = In.VT;
801 } else {
802 unsigned MemoryBits = In.ArgVT.getStoreSizeInBits() / NumRegs;
803 assert(In.ArgVT.getStoreSizeInBits() % NumRegs == 0);
804 if (In.VT.isInteger()) {
805 MemVT = EVT::getIntegerVT(State.getContext(), MemoryBits);
806 } else if (In.VT.isVector()) {
807 assert(!In.VT.getScalarType().isFloatingPoint());
808 unsigned NumElements = In.VT.getVectorNumElements();
809 assert(MemoryBits % NumElements == 0);
810 // This vector type has been split into another vector type with
811 // a different elements size.
812 EVT ScalarVT = EVT::getIntegerVT(State.getContext(),
813 MemoryBits / NumElements);
814 MemVT = EVT::getVectorVT(State.getContext(), ScalarVT, NumElements);
815 } else {
816 llvm_unreachable("cannot deduce memory type.");
817 }
818 }
819
820 // Convert one element vectors to scalar.
821 if (MemVT.isVector() && MemVT.getVectorNumElements() == 1)
822 MemVT = MemVT.getScalarType();
823
824 if (MemVT.isExtended()) {
825 // This should really only happen if we have vec3 arguments
826 assert(MemVT.isVector() && MemVT.getVectorNumElements() == 3);
827 MemVT = MemVT.getPow2VectorType(State.getContext());
828 }
829
830 assert(MemVT.isSimple());
831 allocateKernArg(i, In.VT, MemVT.getSimpleVT(), CCValAssign::Full, In.Flags,
832 State);
833 }
834}
835
836void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
837 const SmallVectorImpl<ISD::InputArg> &Ins) const {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000838 State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
Tom Stellard75aadc22012-12-11 21:25:42 +0000839}
840
Marek Olsak8a0f3352016-01-13 17:23:04 +0000841void AMDGPUTargetLowering::AnalyzeReturn(CCState &State,
842 const SmallVectorImpl<ISD::OutputArg> &Outs) const {
843
844 State.AnalyzeReturn(Outs, RetCC_SI);
845}
846
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000847SDValue
848AMDGPUTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
849 bool isVarArg,
850 const SmallVectorImpl<ISD::OutputArg> &Outs,
851 const SmallVectorImpl<SDValue> &OutVals,
852 const SDLoc &DL, SelectionDAG &DAG) const {
Matt Arsenault9babdf42016-06-22 20:15:28 +0000853 return DAG.getNode(AMDGPUISD::ENDPGM, DL, MVT::Other, Chain);
Tom Stellard75aadc22012-12-11 21:25:42 +0000854}
855
856//===---------------------------------------------------------------------===//
857// Target specific lowering
858//===---------------------------------------------------------------------===//
859
Matt Arsenault16353872014-04-22 16:42:00 +0000860SDValue AMDGPUTargetLowering::LowerCall(CallLoweringInfo &CLI,
861 SmallVectorImpl<SDValue> &InVals) const {
862 SDValue Callee = CLI.Callee;
863 SelectionDAG &DAG = CLI.DAG;
864
865 const Function &Fn = *DAG.getMachineFunction().getFunction();
866
867 StringRef FuncName("<unknown>");
868
Matt Arsenaultde1c34102014-04-25 22:22:01 +0000869 if (const ExternalSymbolSDNode *G = dyn_cast<ExternalSymbolSDNode>(Callee))
870 FuncName = G->getSymbol();
871 else if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Matt Arsenault16353872014-04-22 16:42:00 +0000872 FuncName = G->getGlobal()->getName();
873
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000874 DiagnosticInfoUnsupported NoCalls(
875 Fn, "unsupported call to function " + FuncName, CLI.DL.getDebugLoc());
Matt Arsenault16353872014-04-22 16:42:00 +0000876 DAG.getContext()->diagnose(NoCalls);
Matt Arsenault9430b912016-05-18 16:10:11 +0000877
Matt Arsenault0b386362016-12-15 20:50:12 +0000878 if (!CLI.IsTailCall) {
879 for (unsigned I = 0, E = CLI.Ins.size(); I != E; ++I)
880 InVals.push_back(DAG.getUNDEF(CLI.Ins[I].VT));
881 }
Matt Arsenault9430b912016-05-18 16:10:11 +0000882
883 return DAG.getEntryNode();
Matt Arsenault16353872014-04-22 16:42:00 +0000884}
885
Matt Arsenault19c54882015-08-26 18:37:13 +0000886SDValue AMDGPUTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
887 SelectionDAG &DAG) const {
888 const Function &Fn = *DAG.getMachineFunction().getFunction();
889
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000890 DiagnosticInfoUnsupported NoDynamicAlloca(Fn, "unsupported dynamic alloca",
891 SDLoc(Op).getDebugLoc());
Matt Arsenault19c54882015-08-26 18:37:13 +0000892 DAG.getContext()->diagnose(NoDynamicAlloca);
Diana Picuse440f992016-06-23 09:19:16 +0000893 auto Ops = {DAG.getConstant(0, SDLoc(), Op.getValueType()), Op.getOperand(0)};
894 return DAG.getMergeValues(Ops, SDLoc());
Matt Arsenault19c54882015-08-26 18:37:13 +0000895}
896
Matt Arsenault14d46452014-06-15 20:23:38 +0000897SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op,
898 SelectionDAG &DAG) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000899 switch (Op.getOpcode()) {
900 default:
Matthias Braun8c209aa2017-01-28 02:02:38 +0000901 Op->print(errs(), &DAG);
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +0000902 llvm_unreachable("Custom lowering code for this"
903 "instruction is not implemented yet!");
Tom Stellard75aadc22012-12-11 21:25:42 +0000904 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000905 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
Tom Stellardd86003e2013-08-14 23:25:00 +0000906 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
907 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000908 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
909 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
Jan Vesely109efdf2014-06-22 21:43:00 +0000910 case ISD::SDIVREM: return LowerSDIVREM(Op, DAG);
Matt Arsenault16e31332014-09-10 21:44:27 +0000911 case ISD::FREM: return LowerFREM(Op, DAG);
Matt Arsenault46010932014-06-18 17:05:30 +0000912 case ISD::FCEIL: return LowerFCEIL(Op, DAG);
913 case ISD::FTRUNC: return LowerFTRUNC(Op, DAG);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +0000914 case ISD::FRINT: return LowerFRINT(Op, DAG);
Matt Arsenault692bd5e2014-06-18 22:03:45 +0000915 case ISD::FNEARBYINT: return LowerFNEARBYINT(Op, DAG);
Matt Arsenaultb0055482015-01-21 18:18:25 +0000916 case ISD::FROUND: return LowerFROUND(Op, DAG);
Matt Arsenault46010932014-06-18 17:05:30 +0000917 case ISD::FFLOOR: return LowerFFLOOR(Op, DAG);
Matt Arsenaultf7c95e32014-10-03 23:54:41 +0000918 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Tom Stellardc947d8c2013-10-30 17:22:05 +0000919 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Tom Stellard94c21bc2016-11-01 16:31:48 +0000920 case ISD::FP_TO_FP16: return LowerFP_TO_FP16(Op, DAG);
Matt Arsenaultc9961752014-10-03 23:54:56 +0000921 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
922 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Matt Arsenaultf058d672016-01-11 16:50:29 +0000923 case ISD::CTLZ:
924 case ISD::CTLZ_ZERO_UNDEF:
925 return LowerCTLZ(Op, DAG);
Matt Arsenault19c54882015-08-26 18:37:13 +0000926 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000927 }
928 return Op;
929}
930
Matt Arsenaultd125d742014-03-27 17:23:24 +0000931void AMDGPUTargetLowering::ReplaceNodeResults(SDNode *N,
932 SmallVectorImpl<SDValue> &Results,
933 SelectionDAG &DAG) const {
934 switch (N->getOpcode()) {
935 case ISD::SIGN_EXTEND_INREG:
936 // Different parts of legalization seem to interpret which type of
937 // sign_extend_inreg is the one to check for custom lowering. The extended
938 // from type is what really matters, but some places check for custom
939 // lowering of the result type. This results in trying to use
940 // ReplaceNodeResults to sext_in_reg to an illegal type, so we'll just do
941 // nothing here and let the illegal result integer be handled normally.
942 return;
Matt Arsenaultd125d742014-03-27 17:23:24 +0000943 default:
944 return;
945 }
946}
947
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000948static bool hasDefinedInitializer(const GlobalValue *GV) {
949 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
950 if (!GVar || !GVar->hasInitializer())
951 return false;
952
Matt Arsenault8226fc42016-03-02 23:00:21 +0000953 return !isa<UndefValue>(GVar->getInitializer());
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000954}
955
Tom Stellardc026e8b2013-06-28 15:47:08 +0000956SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
957 SDValue Op,
958 SelectionDAG &DAG) const {
959
Mehdi Amini44ede332015-07-09 02:09:04 +0000960 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardc026e8b2013-06-28 15:47:08 +0000961 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
Tom Stellardc026e8b2013-06-28 15:47:08 +0000962 const GlobalValue *GV = G->getGlobal();
Tom Stellardc026e8b2013-06-28 15:47:08 +0000963
Tom Stellard04c0e982014-01-22 19:24:21 +0000964 switch (G->getAddressSpace()) {
Tom Stellard04c0e982014-01-22 19:24:21 +0000965 case AMDGPUAS::LOCAL_ADDRESS: {
966 // XXX: What does the value of G->getOffset() mean?
967 assert(G->getOffset() == 0 &&
968 "Do not know what to do with an non-zero offset");
Tom Stellardc026e8b2013-06-28 15:47:08 +0000969
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000970 // TODO: We could emit code to handle the initialization somewhere.
971 if (hasDefinedInitializer(GV))
972 break;
973
Matt Arsenault52ef4012016-07-26 16:45:58 +0000974 unsigned Offset = MFI->allocateLDSGlobal(DL, *GV);
975 return DAG.getConstant(Offset, SDLoc(Op), Op.getValueType());
Tom Stellard04c0e982014-01-22 19:24:21 +0000976 }
Tom Stellard04c0e982014-01-22 19:24:21 +0000977 }
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000978
979 const Function &Fn = *DAG.getMachineFunction().getFunction();
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000980 DiagnosticInfoUnsupported BadInit(
981 Fn, "unsupported initializer for address space", SDLoc(Op).getDebugLoc());
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000982 DAG.getContext()->diagnose(BadInit);
983 return SDValue();
Tom Stellardc026e8b2013-06-28 15:47:08 +0000984}
985
Tom Stellardd86003e2013-08-14 23:25:00 +0000986SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
987 SelectionDAG &DAG) const {
988 SmallVector<SDValue, 8> Args;
Tom Stellardd86003e2013-08-14 23:25:00 +0000989
Tom Stellardff5cf0e2015-04-23 22:59:24 +0000990 for (const SDUse &U : Op->ops())
991 DAG.ExtractVectorElements(U.get(), Args);
Tom Stellardd86003e2013-08-14 23:25:00 +0000992
Ahmed Bougacha128f8732016-04-26 21:15:30 +0000993 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args);
Tom Stellardd86003e2013-08-14 23:25:00 +0000994}
995
996SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
997 SelectionDAG &DAG) const {
998
999 SmallVector<SDValue, 8> Args;
Tom Stellardd86003e2013-08-14 23:25:00 +00001000 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00001001 EVT VT = Op.getValueType();
1002 DAG.ExtractVectorElements(Op.getOperand(0), Args, Start,
1003 VT.getVectorNumElements());
Tom Stellardd86003e2013-08-14 23:25:00 +00001004
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001005 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args);
Tom Stellardd86003e2013-08-14 23:25:00 +00001006}
1007
Tom Stellard75aadc22012-12-11 21:25:42 +00001008SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1009 SelectionDAG &DAG) const {
1010 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001011 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +00001012 EVT VT = Op.getValueType();
1013
1014 switch (IntrinsicID) {
Matt Arsenault2fdf2a12017-02-21 23:35:48 +00001015 default: return Op;
Matt Arsenault2fdf2a12017-02-21 23:35:48 +00001016 case AMDGPUIntrinsic::AMDGPU_bfe_i32:
1017 return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT,
1018 Op.getOperand(1),
1019 Op.getOperand(2),
1020 Op.getOperand(3));
Matt Arsenault4c537172014-03-31 18:21:18 +00001021
Matt Arsenault2fdf2a12017-02-21 23:35:48 +00001022 case AMDGPUIntrinsic::AMDGPU_bfe_u32:
1023 return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT,
1024 Op.getOperand(1),
1025 Op.getOperand(2),
1026 Op.getOperand(3));
Tom Stellard75aadc22012-12-11 21:25:42 +00001027 }
1028}
1029
Tom Stellard75aadc22012-12-11 21:25:42 +00001030/// \brief Generate Min/Max node
Matt Arsenaultda7a6562017-02-01 00:42:40 +00001031SDValue AMDGPUTargetLowering::combineFMinMaxLegacy(const SDLoc &DL, EVT VT,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001032 SDValue LHS, SDValue RHS,
1033 SDValue True, SDValue False,
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001034 SDValue CC,
1035 DAGCombinerInfo &DCI) const {
Matt Arsenaultda59f3d2014-11-13 23:03:09 +00001036 if (!(LHS == True && RHS == False) && !(LHS == False && RHS == True))
1037 return SDValue();
Tom Stellard75aadc22012-12-11 21:25:42 +00001038
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001039 SelectionDAG &DAG = DCI.DAG;
Tom Stellard75aadc22012-12-11 21:25:42 +00001040 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1041 switch (CCOpcode) {
1042 case ISD::SETOEQ:
1043 case ISD::SETONE:
1044 case ISD::SETUNE:
1045 case ISD::SETNE:
1046 case ISD::SETUEQ:
1047 case ISD::SETEQ:
1048 case ISD::SETFALSE:
1049 case ISD::SETFALSE2:
1050 case ISD::SETTRUE:
1051 case ISD::SETTRUE2:
1052 case ISD::SETUO:
1053 case ISD::SETO:
Matt Arsenaultda59f3d2014-11-13 23:03:09 +00001054 break;
Tom Stellard75aadc22012-12-11 21:25:42 +00001055 case ISD::SETULE:
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001056 case ISD::SETULT: {
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001057 if (LHS == True)
1058 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS);
1059 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS);
1060 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001061 case ISD::SETOLE:
1062 case ISD::SETOLT:
1063 case ISD::SETLE:
1064 case ISD::SETLT: {
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001065 // Ordered. Assume ordered for undefined.
1066
1067 // Only do this after legalization to avoid interfering with other combines
1068 // which might occur.
1069 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG &&
1070 !DCI.isCalledByLegalizer())
1071 return SDValue();
Marek Olsakbe047802014-12-07 12:19:03 +00001072
Matt Arsenault36094d72014-11-15 05:02:57 +00001073 // We need to permute the operands to get the correct NaN behavior. The
1074 // selected operand is the second one based on the failing compare with NaN,
1075 // so permute it based on the compare type the hardware uses.
1076 if (LHS == True)
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001077 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS);
1078 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS);
Tom Stellard75aadc22012-12-11 21:25:42 +00001079 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001080 case ISD::SETUGE:
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001081 case ISD::SETUGT: {
Matt Arsenault36094d72014-11-15 05:02:57 +00001082 if (LHS == True)
1083 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS);
1084 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS);
Tom Stellard75aadc22012-12-11 21:25:42 +00001085 }
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001086 case ISD::SETGT:
1087 case ISD::SETGE:
1088 case ISD::SETOGE:
1089 case ISD::SETOGT: {
1090 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG &&
1091 !DCI.isCalledByLegalizer())
1092 return SDValue();
1093
1094 if (LHS == True)
1095 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS);
1096 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS);
1097 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001098 case ISD::SETCC_INVALID:
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001099 llvm_unreachable("Invalid setcc condcode!");
Tom Stellard75aadc22012-12-11 21:25:42 +00001100 }
Tom Stellardafa8b532014-05-09 16:42:16 +00001101 return SDValue();
Tom Stellard75aadc22012-12-11 21:25:42 +00001102}
1103
Matt Arsenault6e3a4512016-01-18 22:01:13 +00001104std::pair<SDValue, SDValue>
1105AMDGPUTargetLowering::split64BitValue(SDValue Op, SelectionDAG &DAG) const {
1106 SDLoc SL(Op);
1107
1108 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1109
1110 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1111 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1112
1113 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1114 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1115
1116 return std::make_pair(Lo, Hi);
1117}
1118
Matt Arsenault33e3ece2016-01-18 22:09:04 +00001119SDValue AMDGPUTargetLowering::getLoHalf64(SDValue Op, SelectionDAG &DAG) const {
1120 SDLoc SL(Op);
1121
1122 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1123 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1124 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1125}
1126
1127SDValue AMDGPUTargetLowering::getHiHalf64(SDValue Op, SelectionDAG &DAG) const {
1128 SDLoc SL(Op);
1129
1130 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1131 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1132 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1133}
1134
Matt Arsenault83e60582014-07-24 17:10:35 +00001135SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue Op,
1136 SelectionDAG &DAG) const {
Matt Arsenault9c499c32016-04-14 23:31:26 +00001137 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault83e60582014-07-24 17:10:35 +00001138 EVT VT = Op.getValueType();
1139
Matt Arsenault9c499c32016-04-14 23:31:26 +00001140
Matt Arsenault83e60582014-07-24 17:10:35 +00001141 // If this is a 2 element vector, we really want to scalarize and not create
1142 // weird 1 element vectors.
1143 if (VT.getVectorNumElements() == 2)
Matt Arsenault9c499c32016-04-14 23:31:26 +00001144 return scalarizeVectorLoad(Load, DAG);
Matt Arsenault83e60582014-07-24 17:10:35 +00001145
Matt Arsenault83e60582014-07-24 17:10:35 +00001146 SDValue BasePtr = Load->getBasePtr();
1147 EVT PtrVT = BasePtr.getValueType();
1148 EVT MemVT = Load->getMemoryVT();
1149 SDLoc SL(Op);
Matt Arsenault52a52a52015-12-14 16:59:40 +00001150
1151 const MachinePointerInfo &SrcValue = Load->getMemOperand()->getPointerInfo();
Matt Arsenault83e60582014-07-24 17:10:35 +00001152
1153 EVT LoVT, HiVT;
1154 EVT LoMemVT, HiMemVT;
1155 SDValue Lo, Hi;
1156
1157 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
1158 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT);
1159 std::tie(Lo, Hi) = DAG.SplitVector(Op, SL, LoVT, HiVT);
Matt Arsenault52a52a52015-12-14 16:59:40 +00001160
1161 unsigned Size = LoMemVT.getStoreSize();
1162 unsigned BaseAlign = Load->getAlignment();
1163 unsigned HiAlign = MinAlign(BaseAlign, Size);
1164
Justin Lebar9c375812016-07-15 18:27:10 +00001165 SDValue LoLoad = DAG.getExtLoad(Load->getExtensionType(), SL, LoVT,
1166 Load->getChain(), BasePtr, SrcValue, LoMemVT,
1167 BaseAlign, Load->getMemOperand()->getFlags());
Matt Arsenault83e60582014-07-24 17:10:35 +00001168 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
Matt Arsenault52a52a52015-12-14 16:59:40 +00001169 DAG.getConstant(Size, SL, PtrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00001170 SDValue HiLoad =
1171 DAG.getExtLoad(Load->getExtensionType(), SL, HiVT, Load->getChain(),
1172 HiPtr, SrcValue.getWithOffset(LoMemVT.getStoreSize()),
1173 HiMemVT, HiAlign, Load->getMemOperand()->getFlags());
Matt Arsenault83e60582014-07-24 17:10:35 +00001174
1175 SDValue Ops[] = {
1176 DAG.getNode(ISD::CONCAT_VECTORS, SL, VT, LoLoad, HiLoad),
1177 DAG.getNode(ISD::TokenFactor, SL, MVT::Other,
1178 LoLoad.getValue(1), HiLoad.getValue(1))
1179 };
1180
1181 return DAG.getMergeValues(Ops, SL);
1182}
1183
Matt Arsenault83e60582014-07-24 17:10:35 +00001184SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op,
1185 SelectionDAG &DAG) const {
1186 StoreSDNode *Store = cast<StoreSDNode>(Op);
1187 SDValue Val = Store->getValue();
1188 EVT VT = Val.getValueType();
1189
1190 // If this is a 2 element vector, we really want to scalarize and not create
1191 // weird 1 element vectors.
1192 if (VT.getVectorNumElements() == 2)
Matt Arsenault9c499c32016-04-14 23:31:26 +00001193 return scalarizeVectorStore(Store, DAG);
Matt Arsenault83e60582014-07-24 17:10:35 +00001194
1195 EVT MemVT = Store->getMemoryVT();
1196 SDValue Chain = Store->getChain();
1197 SDValue BasePtr = Store->getBasePtr();
1198 SDLoc SL(Op);
1199
1200 EVT LoVT, HiVT;
1201 EVT LoMemVT, HiMemVT;
1202 SDValue Lo, Hi;
1203
1204 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
1205 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT);
1206 std::tie(Lo, Hi) = DAG.SplitVector(Val, SL, LoVT, HiVT);
1207
1208 EVT PtrVT = BasePtr.getValueType();
1209 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001210 DAG.getConstant(LoMemVT.getStoreSize(), SL,
1211 PtrVT));
Matt Arsenault83e60582014-07-24 17:10:35 +00001212
Matt Arsenault52a52a52015-12-14 16:59:40 +00001213 const MachinePointerInfo &SrcValue = Store->getMemOperand()->getPointerInfo();
1214 unsigned BaseAlign = Store->getAlignment();
1215 unsigned Size = LoMemVT.getStoreSize();
1216 unsigned HiAlign = MinAlign(BaseAlign, Size);
1217
Justin Lebar9c375812016-07-15 18:27:10 +00001218 SDValue LoStore =
1219 DAG.getTruncStore(Chain, SL, Lo, BasePtr, SrcValue, LoMemVT, BaseAlign,
1220 Store->getMemOperand()->getFlags());
1221 SDValue HiStore =
1222 DAG.getTruncStore(Chain, SL, Hi, HiPtr, SrcValue.getWithOffset(Size),
1223 HiMemVT, HiAlign, Store->getMemOperand()->getFlags());
Matt Arsenault83e60582014-07-24 17:10:35 +00001224
1225 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, LoStore, HiStore);
1226}
1227
Matt Arsenault0daeb632014-07-24 06:59:20 +00001228// This is a shortcut for integer division because we have fast i32<->f32
1229// conversions, and fast f32 reciprocal instructions. The fractional part of a
Matt Arsenault81a70952016-05-21 01:53:33 +00001230// float is enough to accurately represent up to a 24-bit signed integer.
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001231SDValue AMDGPUTargetLowering::LowerDIVREM24(SDValue Op, SelectionDAG &DAG,
1232 bool Sign) const {
Matt Arsenault1578aa72014-06-15 20:08:02 +00001233 SDLoc DL(Op);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001234 EVT VT = Op.getValueType();
Matt Arsenault1578aa72014-06-15 20:08:02 +00001235 SDValue LHS = Op.getOperand(0);
1236 SDValue RHS = Op.getOperand(1);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001237 MVT IntVT = MVT::i32;
1238 MVT FltVT = MVT::f32;
1239
Matt Arsenault81a70952016-05-21 01:53:33 +00001240 unsigned LHSSignBits = DAG.ComputeNumSignBits(LHS);
1241 if (LHSSignBits < 9)
1242 return SDValue();
1243
1244 unsigned RHSSignBits = DAG.ComputeNumSignBits(RHS);
1245 if (RHSSignBits < 9)
1246 return SDValue();
Jan Veselye5ca27d2014-08-12 17:31:20 +00001247
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001248 unsigned BitSize = VT.getSizeInBits();
Matt Arsenault81a70952016-05-21 01:53:33 +00001249 unsigned SignBits = std::min(LHSSignBits, RHSSignBits);
1250 unsigned DivBits = BitSize - SignBits;
1251 if (Sign)
1252 ++DivBits;
1253
1254 ISD::NodeType ToFp = Sign ? ISD::SINT_TO_FP : ISD::UINT_TO_FP;
1255 ISD::NodeType ToInt = Sign ? ISD::FP_TO_SINT : ISD::FP_TO_UINT;
Matt Arsenault0daeb632014-07-24 06:59:20 +00001256
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001257 SDValue jq = DAG.getConstant(1, DL, IntVT);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001258
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001259 if (Sign) {
Jan Veselye5ca27d2014-08-12 17:31:20 +00001260 // char|short jq = ia ^ ib;
1261 jq = DAG.getNode(ISD::XOR, DL, VT, LHS, RHS);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001262
Jan Veselye5ca27d2014-08-12 17:31:20 +00001263 // jq = jq >> (bitsize - 2)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001264 jq = DAG.getNode(ISD::SRA, DL, VT, jq,
1265 DAG.getConstant(BitSize - 2, DL, VT));
Matt Arsenault1578aa72014-06-15 20:08:02 +00001266
Jan Veselye5ca27d2014-08-12 17:31:20 +00001267 // jq = jq | 0x1
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001268 jq = DAG.getNode(ISD::OR, DL, VT, jq, DAG.getConstant(1, DL, VT));
Jan Veselye5ca27d2014-08-12 17:31:20 +00001269 }
Matt Arsenault1578aa72014-06-15 20:08:02 +00001270
1271 // int ia = (int)LHS;
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001272 SDValue ia = LHS;
Matt Arsenault1578aa72014-06-15 20:08:02 +00001273
1274 // int ib, (int)RHS;
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001275 SDValue ib = RHS;
Matt Arsenault1578aa72014-06-15 20:08:02 +00001276
1277 // float fa = (float)ia;
Jan Veselye5ca27d2014-08-12 17:31:20 +00001278 SDValue fa = DAG.getNode(ToFp, DL, FltVT, ia);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001279
1280 // float fb = (float)ib;
Jan Veselye5ca27d2014-08-12 17:31:20 +00001281 SDValue fb = DAG.getNode(ToFp, DL, FltVT, ib);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001282
Matt Arsenault0daeb632014-07-24 06:59:20 +00001283 SDValue fq = DAG.getNode(ISD::FMUL, DL, FltVT,
1284 fa, DAG.getNode(AMDGPUISD::RCP, DL, FltVT, fb));
Matt Arsenault1578aa72014-06-15 20:08:02 +00001285
1286 // fq = trunc(fq);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001287 fq = DAG.getNode(ISD::FTRUNC, DL, FltVT, fq);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001288
1289 // float fqneg = -fq;
Matt Arsenault0daeb632014-07-24 06:59:20 +00001290 SDValue fqneg = DAG.getNode(ISD::FNEG, DL, FltVT, fq);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001291
1292 // float fr = mad(fqneg, fb, fa);
Wei Ding4d3d4ca2017-02-24 23:00:29 +00001293 unsigned OpCode = Subtarget->hasFP32Denormals() ?
1294 (unsigned)AMDGPUISD::FMAD_FTZ :
1295 (unsigned)ISD::FMAD;
1296 SDValue fr = DAG.getNode(OpCode, DL, FltVT, fqneg, fb, fa);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001297
1298 // int iq = (int)fq;
Jan Veselye5ca27d2014-08-12 17:31:20 +00001299 SDValue iq = DAG.getNode(ToInt, DL, IntVT, fq);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001300
1301 // fr = fabs(fr);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001302 fr = DAG.getNode(ISD::FABS, DL, FltVT, fr);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001303
1304 // fb = fabs(fb);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001305 fb = DAG.getNode(ISD::FABS, DL, FltVT, fb);
1306
Mehdi Amini44ede332015-07-09 02:09:04 +00001307 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001308
1309 // int cv = fr >= fb;
Matt Arsenault0daeb632014-07-24 06:59:20 +00001310 SDValue cv = DAG.getSetCC(DL, SetCCVT, fr, fb, ISD::SETOGE);
1311
Matt Arsenault1578aa72014-06-15 20:08:02 +00001312 // jq = (cv ? jq : 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001313 jq = DAG.getNode(ISD::SELECT, DL, VT, cv, jq, DAG.getConstant(0, DL, VT));
Matt Arsenault0daeb632014-07-24 06:59:20 +00001314
Jan Veselye5ca27d2014-08-12 17:31:20 +00001315 // dst = iq + jq;
Jan Vesely4a33bc62014-08-12 17:31:17 +00001316 SDValue Div = DAG.getNode(ISD::ADD, DL, VT, iq, jq);
1317
Jan Veselye5ca27d2014-08-12 17:31:20 +00001318 // Rem needs compensation, it's easier to recompute it
Jan Vesely4a33bc62014-08-12 17:31:17 +00001319 SDValue Rem = DAG.getNode(ISD::MUL, DL, VT, Div, RHS);
1320 Rem = DAG.getNode(ISD::SUB, DL, VT, LHS, Rem);
1321
Matt Arsenault81a70952016-05-21 01:53:33 +00001322 // Truncate to number of bits this divide really is.
1323 if (Sign) {
1324 SDValue InRegSize
1325 = DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), DivBits));
1326 Div = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Div, InRegSize);
1327 Rem = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Rem, InRegSize);
1328 } else {
1329 SDValue TruncMask = DAG.getConstant((UINT64_C(1) << DivBits) - 1, DL, VT);
1330 Div = DAG.getNode(ISD::AND, DL, VT, Div, TruncMask);
1331 Rem = DAG.getNode(ISD::AND, DL, VT, Rem, TruncMask);
1332 }
1333
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001334 return DAG.getMergeValues({ Div, Rem }, DL);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001335}
1336
Tom Stellardbf69d762014-11-15 01:07:53 +00001337void AMDGPUTargetLowering::LowerUDIVREM64(SDValue Op,
1338 SelectionDAG &DAG,
1339 SmallVectorImpl<SDValue> &Results) const {
1340 assert(Op.getValueType() == MVT::i64);
1341
1342 SDLoc DL(Op);
1343 EVT VT = Op.getValueType();
1344 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
1345
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001346 SDValue one = DAG.getConstant(1, DL, HalfVT);
1347 SDValue zero = DAG.getConstant(0, DL, HalfVT);
Tom Stellardbf69d762014-11-15 01:07:53 +00001348
1349 //HiLo split
1350 SDValue LHS = Op.getOperand(0);
1351 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, zero);
1352 SDValue LHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, one);
1353
1354 SDValue RHS = Op.getOperand(1);
1355 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, zero);
1356 SDValue RHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, one);
1357
Jan Vesely5f715d32015-01-22 23:42:43 +00001358 if (VT == MVT::i64 &&
1359 DAG.MaskedValueIsZero(RHS, APInt::getHighBitsSet(64, 32)) &&
1360 DAG.MaskedValueIsZero(LHS, APInt::getHighBitsSet(64, 32))) {
1361
1362 SDValue Res = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(HalfVT, HalfVT),
1363 LHS_Lo, RHS_Lo);
1364
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001365 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(0), zero});
1366 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(1), zero});
Matt Arsenaultd275fca2016-03-01 05:06:05 +00001367
1368 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV));
1369 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM));
Jan Vesely5f715d32015-01-22 23:42:43 +00001370 return;
1371 }
1372
Tom Stellardbf69d762014-11-15 01:07:53 +00001373 // Get Speculative values
1374 SDValue DIV_Part = DAG.getNode(ISD::UDIV, DL, HalfVT, LHS_Hi, RHS_Lo);
1375 SDValue REM_Part = DAG.getNode(ISD::UREM, DL, HalfVT, LHS_Hi, RHS_Lo);
1376
Tom Stellardbf69d762014-11-15 01:07:53 +00001377 SDValue REM_Lo = DAG.getSelectCC(DL, RHS_Hi, zero, REM_Part, LHS_Hi, ISD::SETEQ);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001378 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {REM_Lo, zero});
Matt Arsenaultd275fca2016-03-01 05:06:05 +00001379 REM = DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM);
Tom Stellardbf69d762014-11-15 01:07:53 +00001380
1381 SDValue DIV_Hi = DAG.getSelectCC(DL, RHS_Hi, zero, DIV_Part, zero, ISD::SETEQ);
1382 SDValue DIV_Lo = zero;
1383
1384 const unsigned halfBitWidth = HalfVT.getSizeInBits();
1385
1386 for (unsigned i = 0; i < halfBitWidth; ++i) {
Jan Veselyf7987ca2015-01-22 23:42:39 +00001387 const unsigned bitPos = halfBitWidth - i - 1;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001388 SDValue POS = DAG.getConstant(bitPos, DL, HalfVT);
Jan Veselyf7987ca2015-01-22 23:42:39 +00001389 // Get value of high bit
Jan Vesely811ef522015-04-12 23:45:01 +00001390 SDValue HBit = DAG.getNode(ISD::SRL, DL, HalfVT, LHS_Lo, POS);
1391 HBit = DAG.getNode(ISD::AND, DL, HalfVT, HBit, one);
Jan Veselyf7987ca2015-01-22 23:42:39 +00001392 HBit = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, HBit);
Tom Stellardbf69d762014-11-15 01:07:53 +00001393
Jan Veselyf7987ca2015-01-22 23:42:39 +00001394 // Shift
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001395 REM = DAG.getNode(ISD::SHL, DL, VT, REM, DAG.getConstant(1, DL, VT));
Jan Veselyf7987ca2015-01-22 23:42:39 +00001396 // Add LHS high bit
1397 REM = DAG.getNode(ISD::OR, DL, VT, REM, HBit);
Tom Stellardbf69d762014-11-15 01:07:53 +00001398
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +00001399 SDValue BIT = DAG.getConstant(1ULL << bitPos, DL, HalfVT);
Tom Stellard83171b32014-11-15 01:07:57 +00001400 SDValue realBIT = DAG.getSelectCC(DL, REM, RHS, BIT, zero, ISD::SETUGE);
Tom Stellardbf69d762014-11-15 01:07:53 +00001401
1402 DIV_Lo = DAG.getNode(ISD::OR, DL, HalfVT, DIV_Lo, realBIT);
1403
1404 // Update REM
Tom Stellardbf69d762014-11-15 01:07:53 +00001405 SDValue REM_sub = DAG.getNode(ISD::SUB, DL, VT, REM, RHS);
Tom Stellard83171b32014-11-15 01:07:57 +00001406 REM = DAG.getSelectCC(DL, REM, RHS, REM_sub, REM, ISD::SETUGE);
Tom Stellardbf69d762014-11-15 01:07:53 +00001407 }
1408
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001409 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {DIV_Lo, DIV_Hi});
Matt Arsenaultd275fca2016-03-01 05:06:05 +00001410 DIV = DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV);
Tom Stellardbf69d762014-11-15 01:07:53 +00001411 Results.push_back(DIV);
1412 Results.push_back(REM);
1413}
1414
Tom Stellard75aadc22012-12-11 21:25:42 +00001415SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
Matt Arsenault46013d92014-05-11 21:24:41 +00001416 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001417 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +00001418 EVT VT = Op.getValueType();
1419
Tom Stellardbf69d762014-11-15 01:07:53 +00001420 if (VT == MVT::i64) {
1421 SmallVector<SDValue, 2> Results;
1422 LowerUDIVREM64(Op, DAG, Results);
1423 return DAG.getMergeValues(Results, DL);
1424 }
1425
Matt Arsenault81a70952016-05-21 01:53:33 +00001426 if (VT == MVT::i32) {
1427 if (SDValue Res = LowerDIVREM24(Op, DAG, false))
1428 return Res;
1429 }
1430
Tom Stellard75aadc22012-12-11 21:25:42 +00001431 SDValue Num = Op.getOperand(0);
1432 SDValue Den = Op.getOperand(1);
1433
Tom Stellard75aadc22012-12-11 21:25:42 +00001434 // RCP = URECIP(Den) = 2^32 / Den + e
1435 // e is rounding error.
1436 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
1437
Tom Stellard4349b192014-09-22 15:35:30 +00001438 // RCP_LO = mul(RCP, Den) */
1439 SDValue RCP_LO = DAG.getNode(ISD::MUL, DL, VT, RCP, Den);
Tom Stellard75aadc22012-12-11 21:25:42 +00001440
1441 // RCP_HI = mulhu (RCP, Den) */
1442 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
1443
1444 // NEG_RCP_LO = -RCP_LO
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001445 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001446 RCP_LO);
1447
1448 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001449 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001450 NEG_RCP_LO, RCP_LO,
1451 ISD::SETEQ);
1452 // Calculate the rounding error from the URECIP instruction
1453 // E = mulhu(ABS_RCP_LO, RCP)
1454 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
1455
1456 // RCP_A_E = RCP + E
1457 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
1458
1459 // RCP_S_E = RCP - E
1460 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
1461
1462 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001463 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001464 RCP_A_E, RCP_S_E,
1465 ISD::SETEQ);
1466 // Quotient = mulhu(Tmp0, Num)
1467 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
1468
1469 // Num_S_Remainder = Quotient * Den
Tom Stellard4349b192014-09-22 15:35:30 +00001470 SDValue Num_S_Remainder = DAG.getNode(ISD::MUL, DL, VT, Quotient, Den);
Tom Stellard75aadc22012-12-11 21:25:42 +00001471
1472 // Remainder = Num - Num_S_Remainder
1473 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
1474
1475 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
1476 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001477 DAG.getConstant(-1, DL, VT),
1478 DAG.getConstant(0, DL, VT),
Vincent Lejeune4f3751f2013-11-06 17:36:04 +00001479 ISD::SETUGE);
1480 // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0)
1481 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Num,
1482 Num_S_Remainder,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001483 DAG.getConstant(-1, DL, VT),
1484 DAG.getConstant(0, DL, VT),
Vincent Lejeune4f3751f2013-11-06 17:36:04 +00001485 ISD::SETUGE);
Tom Stellard75aadc22012-12-11 21:25:42 +00001486 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
1487 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
1488 Remainder_GE_Zero);
1489
1490 // Calculate Division result:
1491
1492 // Quotient_A_One = Quotient + 1
1493 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001494 DAG.getConstant(1, DL, VT));
Tom Stellard75aadc22012-12-11 21:25:42 +00001495
1496 // Quotient_S_One = Quotient - 1
1497 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001498 DAG.getConstant(1, DL, VT));
Tom Stellard75aadc22012-12-11 21:25:42 +00001499
1500 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001501 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001502 Quotient, Quotient_A_One, ISD::SETEQ);
1503
1504 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001505 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001506 Quotient_S_One, Div, ISD::SETEQ);
1507
1508 // Calculate Rem result:
1509
1510 // Remainder_S_Den = Remainder - Den
1511 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
1512
1513 // Remainder_A_Den = Remainder + Den
1514 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
1515
1516 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001517 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001518 Remainder, Remainder_S_Den, ISD::SETEQ);
1519
1520 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001521 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001522 Remainder_A_Den, Rem, ISD::SETEQ);
Matt Arsenault7939acd2014-04-07 16:44:24 +00001523 SDValue Ops[2] = {
1524 Div,
1525 Rem
1526 };
Craig Topper64941d92014-04-27 19:20:57 +00001527 return DAG.getMergeValues(Ops, DL);
Tom Stellard75aadc22012-12-11 21:25:42 +00001528}
1529
Jan Vesely109efdf2014-06-22 21:43:00 +00001530SDValue AMDGPUTargetLowering::LowerSDIVREM(SDValue Op,
1531 SelectionDAG &DAG) const {
1532 SDLoc DL(Op);
1533 EVT VT = Op.getValueType();
1534
Jan Vesely109efdf2014-06-22 21:43:00 +00001535 SDValue LHS = Op.getOperand(0);
1536 SDValue RHS = Op.getOperand(1);
1537
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001538 SDValue Zero = DAG.getConstant(0, DL, VT);
1539 SDValue NegOne = DAG.getConstant(-1, DL, VT);
Jan Vesely4a33bc62014-08-12 17:31:17 +00001540
Matt Arsenault81a70952016-05-21 01:53:33 +00001541 if (VT == MVT::i32) {
1542 if (SDValue Res = LowerDIVREM24(Op, DAG, true))
1543 return Res;
Jan Vesely5f715d32015-01-22 23:42:43 +00001544 }
Matt Arsenault81a70952016-05-21 01:53:33 +00001545
Jan Vesely5f715d32015-01-22 23:42:43 +00001546 if (VT == MVT::i64 &&
1547 DAG.ComputeNumSignBits(LHS) > 32 &&
1548 DAG.ComputeNumSignBits(RHS) > 32) {
1549 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
1550
1551 //HiLo split
1552 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, Zero);
1553 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, Zero);
1554 SDValue DIVREM = DAG.getNode(ISD::SDIVREM, DL, DAG.getVTList(HalfVT, HalfVT),
1555 LHS_Lo, RHS_Lo);
1556 SDValue Res[2] = {
1557 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(0)),
1558 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(1))
1559 };
1560 return DAG.getMergeValues(Res, DL);
1561 }
1562
Jan Vesely109efdf2014-06-22 21:43:00 +00001563 SDValue LHSign = DAG.getSelectCC(DL, LHS, Zero, NegOne, Zero, ISD::SETLT);
1564 SDValue RHSign = DAG.getSelectCC(DL, RHS, Zero, NegOne, Zero, ISD::SETLT);
1565 SDValue DSign = DAG.getNode(ISD::XOR, DL, VT, LHSign, RHSign);
1566 SDValue RSign = LHSign; // Remainder sign is the same as LHS
1567
1568 LHS = DAG.getNode(ISD::ADD, DL, VT, LHS, LHSign);
1569 RHS = DAG.getNode(ISD::ADD, DL, VT, RHS, RHSign);
1570
1571 LHS = DAG.getNode(ISD::XOR, DL, VT, LHS, LHSign);
1572 RHS = DAG.getNode(ISD::XOR, DL, VT, RHS, RHSign);
1573
1574 SDValue Div = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT), LHS, RHS);
1575 SDValue Rem = Div.getValue(1);
1576
1577 Div = DAG.getNode(ISD::XOR, DL, VT, Div, DSign);
1578 Rem = DAG.getNode(ISD::XOR, DL, VT, Rem, RSign);
1579
1580 Div = DAG.getNode(ISD::SUB, DL, VT, Div, DSign);
1581 Rem = DAG.getNode(ISD::SUB, DL, VT, Rem, RSign);
1582
1583 SDValue Res[2] = {
1584 Div,
1585 Rem
1586 };
1587 return DAG.getMergeValues(Res, DL);
1588}
1589
Matt Arsenault16e31332014-09-10 21:44:27 +00001590// (frem x, y) -> (fsub x, (fmul (ftrunc (fdiv x, y)), y))
1591SDValue AMDGPUTargetLowering::LowerFREM(SDValue Op, SelectionDAG &DAG) const {
1592 SDLoc SL(Op);
1593 EVT VT = Op.getValueType();
1594 SDValue X = Op.getOperand(0);
1595 SDValue Y = Op.getOperand(1);
1596
Sanjay Patela2607012015-09-16 16:31:21 +00001597 // TODO: Should this propagate fast-math-flags?
1598
Matt Arsenault16e31332014-09-10 21:44:27 +00001599 SDValue Div = DAG.getNode(ISD::FDIV, SL, VT, X, Y);
1600 SDValue Floor = DAG.getNode(ISD::FTRUNC, SL, VT, Div);
1601 SDValue Mul = DAG.getNode(ISD::FMUL, SL, VT, Floor, Y);
1602
1603 return DAG.getNode(ISD::FSUB, SL, VT, X, Mul);
1604}
1605
Matt Arsenault46010932014-06-18 17:05:30 +00001606SDValue AMDGPUTargetLowering::LowerFCEIL(SDValue Op, SelectionDAG &DAG) const {
1607 SDLoc SL(Op);
1608 SDValue Src = Op.getOperand(0);
1609
1610 // result = trunc(src)
1611 // if (src > 0.0 && src != result)
1612 // result += 1.0
1613
1614 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1615
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001616 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64);
1617 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001618
Mehdi Amini44ede332015-07-09 02:09:04 +00001619 EVT SetCCVT =
1620 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001621
1622 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOGT);
1623 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1624 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1625
1626 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, One, Zero);
Sanjay Patela2607012015-09-16 16:31:21 +00001627 // TODO: Should this propagate fast-math-flags?
Matt Arsenault46010932014-06-18 17:05:30 +00001628 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1629}
1630
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001631static SDValue extractF64Exponent(SDValue Hi, const SDLoc &SL,
1632 SelectionDAG &DAG) {
Matt Arsenaultb0055482015-01-21 18:18:25 +00001633 const unsigned FractBits = 52;
1634 const unsigned ExpBits = 11;
1635
1636 SDValue ExpPart = DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32,
1637 Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001638 DAG.getConstant(FractBits - 32, SL, MVT::i32),
1639 DAG.getConstant(ExpBits, SL, MVT::i32));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001640 SDValue Exp = DAG.getNode(ISD::SUB, SL, MVT::i32, ExpPart,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001641 DAG.getConstant(1023, SL, MVT::i32));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001642
1643 return Exp;
1644}
1645
Matt Arsenault46010932014-06-18 17:05:30 +00001646SDValue AMDGPUTargetLowering::LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const {
1647 SDLoc SL(Op);
1648 SDValue Src = Op.getOperand(0);
1649
1650 assert(Op.getValueType() == MVT::f64);
1651
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001652 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1653 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001654
1655 SDValue VecSrc = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1656
1657 // Extract the upper half, since this is where we will find the sign and
1658 // exponent.
1659 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, VecSrc, One);
1660
Matt Arsenaultb0055482015-01-21 18:18:25 +00001661 SDValue Exp = extractF64Exponent(Hi, SL, DAG);
Matt Arsenault46010932014-06-18 17:05:30 +00001662
Matt Arsenaultb0055482015-01-21 18:18:25 +00001663 const unsigned FractBits = 52;
Matt Arsenault46010932014-06-18 17:05:30 +00001664
1665 // Extract the sign bit.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001666 const SDValue SignBitMask = DAG.getConstant(UINT32_C(1) << 31, SL, MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001667 SDValue SignBit = DAG.getNode(ISD::AND, SL, MVT::i32, Hi, SignBitMask);
1668
1669 // Extend back to to 64-bits.
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001670 SDValue SignBit64 = DAG.getBuildVector(MVT::v2i32, SL, {Zero, SignBit});
Matt Arsenault46010932014-06-18 17:05:30 +00001671 SignBit64 = DAG.getNode(ISD::BITCAST, SL, MVT::i64, SignBit64);
1672
1673 SDValue BcInt = DAG.getNode(ISD::BITCAST, SL, MVT::i64, Src);
Matt Arsenault2b0fa432014-06-18 22:11:03 +00001674 const SDValue FractMask
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001675 = DAG.getConstant((UINT64_C(1) << FractBits) - 1, SL, MVT::i64);
Matt Arsenault46010932014-06-18 17:05:30 +00001676
1677 SDValue Shr = DAG.getNode(ISD::SRA, SL, MVT::i64, FractMask, Exp);
1678 SDValue Not = DAG.getNOT(SL, Shr, MVT::i64);
1679 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, BcInt, Not);
1680
Mehdi Amini44ede332015-07-09 02:09:04 +00001681 EVT SetCCVT =
1682 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001683
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001684 const SDValue FiftyOne = DAG.getConstant(FractBits - 1, SL, MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001685
1686 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
1687 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
1688
1689 SDValue Tmp1 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpLt0, SignBit64, Tmp0);
1690 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpGt51, BcInt, Tmp1);
1691
1692 return DAG.getNode(ISD::BITCAST, SL, MVT::f64, Tmp2);
1693}
1694
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001695SDValue AMDGPUTargetLowering::LowerFRINT(SDValue Op, SelectionDAG &DAG) const {
1696 SDLoc SL(Op);
1697 SDValue Src = Op.getOperand(0);
1698
1699 assert(Op.getValueType() == MVT::f64);
1700
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001701 APFloat C1Val(APFloat::IEEEdouble(), "0x1.0p+52");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001702 SDValue C1 = DAG.getConstantFP(C1Val, SL, MVT::f64);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001703 SDValue CopySign = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, C1, Src);
1704
Sanjay Patela2607012015-09-16 16:31:21 +00001705 // TODO: Should this propagate fast-math-flags?
1706
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001707 SDValue Tmp1 = DAG.getNode(ISD::FADD, SL, MVT::f64, Src, CopySign);
1708 SDValue Tmp2 = DAG.getNode(ISD::FSUB, SL, MVT::f64, Tmp1, CopySign);
1709
1710 SDValue Fabs = DAG.getNode(ISD::FABS, SL, MVT::f64, Src);
Matt Arsenaultd22626f2014-06-18 17:45:58 +00001711
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001712 APFloat C2Val(APFloat::IEEEdouble(), "0x1.fffffffffffffp+51");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001713 SDValue C2 = DAG.getConstantFP(C2Val, SL, MVT::f64);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001714
Mehdi Amini44ede332015-07-09 02:09:04 +00001715 EVT SetCCVT =
1716 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001717 SDValue Cond = DAG.getSetCC(SL, SetCCVT, Fabs, C2, ISD::SETOGT);
1718
1719 return DAG.getSelect(SL, MVT::f64, Cond, Src, Tmp2);
1720}
1721
Matt Arsenault692bd5e2014-06-18 22:03:45 +00001722SDValue AMDGPUTargetLowering::LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const {
1723 // FNEARBYINT and FRINT are the same, except in their handling of FP
1724 // exceptions. Those aren't really meaningful for us, and OpenCL only has
1725 // rint, so just treat them as equivalent.
1726 return DAG.getNode(ISD::FRINT, SDLoc(Op), Op.getValueType(), Op.getOperand(0));
1727}
1728
Matt Arsenaultb0055482015-01-21 18:18:25 +00001729// XXX - May require not supporting f32 denormals?
1730SDValue AMDGPUTargetLowering::LowerFROUND32(SDValue Op, SelectionDAG &DAG) const {
1731 SDLoc SL(Op);
1732 SDValue X = Op.getOperand(0);
1733
1734 SDValue T = DAG.getNode(ISD::FTRUNC, SL, MVT::f32, X);
1735
Sanjay Patela2607012015-09-16 16:31:21 +00001736 // TODO: Should this propagate fast-math-flags?
1737
Matt Arsenaultb0055482015-01-21 18:18:25 +00001738 SDValue Diff = DAG.getNode(ISD::FSUB, SL, MVT::f32, X, T);
1739
1740 SDValue AbsDiff = DAG.getNode(ISD::FABS, SL, MVT::f32, Diff);
1741
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001742 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f32);
1743 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
1744 const SDValue Half = DAG.getConstantFP(0.5, SL, MVT::f32);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001745
1746 SDValue SignOne = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f32, One, X);
1747
Mehdi Amini44ede332015-07-09 02:09:04 +00001748 EVT SetCCVT =
1749 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001750
1751 SDValue Cmp = DAG.getSetCC(SL, SetCCVT, AbsDiff, Half, ISD::SETOGE);
1752
1753 SDValue Sel = DAG.getNode(ISD::SELECT, SL, MVT::f32, Cmp, SignOne, Zero);
1754
1755 return DAG.getNode(ISD::FADD, SL, MVT::f32, T, Sel);
1756}
1757
1758SDValue AMDGPUTargetLowering::LowerFROUND64(SDValue Op, SelectionDAG &DAG) const {
1759 SDLoc SL(Op);
1760 SDValue X = Op.getOperand(0);
1761
1762 SDValue L = DAG.getNode(ISD::BITCAST, SL, MVT::i64, X);
1763
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001764 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1765 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1766 const SDValue NegOne = DAG.getConstant(-1, SL, MVT::i32);
1767 const SDValue FiftyOne = DAG.getConstant(51, SL, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00001768 EVT SetCCVT =
1769 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001770
1771 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1772
1773 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, One);
1774
1775 SDValue Exp = extractF64Exponent(Hi, SL, DAG);
1776
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001777 const SDValue Mask = DAG.getConstant(INT64_C(0x000fffffffffffff), SL,
1778 MVT::i64);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001779
1780 SDValue M = DAG.getNode(ISD::SRA, SL, MVT::i64, Mask, Exp);
1781 SDValue D = DAG.getNode(ISD::SRA, SL, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001782 DAG.getConstant(INT64_C(0x0008000000000000), SL,
1783 MVT::i64),
Matt Arsenaultb0055482015-01-21 18:18:25 +00001784 Exp);
1785
1786 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, L, M);
1787 SDValue Tmp1 = DAG.getSetCC(SL, SetCCVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001788 DAG.getConstant(0, SL, MVT::i64), Tmp0,
Matt Arsenaultb0055482015-01-21 18:18:25 +00001789 ISD::SETNE);
1790
1791 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, Tmp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001792 D, DAG.getConstant(0, SL, MVT::i64));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001793 SDValue K = DAG.getNode(ISD::ADD, SL, MVT::i64, L, Tmp2);
1794
1795 K = DAG.getNode(ISD::AND, SL, MVT::i64, K, DAG.getNOT(SL, M, MVT::i64));
1796 K = DAG.getNode(ISD::BITCAST, SL, MVT::f64, K);
1797
1798 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
1799 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
1800 SDValue ExpEqNegOne = DAG.getSetCC(SL, SetCCVT, NegOne, Exp, ISD::SETEQ);
1801
1802 SDValue Mag = DAG.getNode(ISD::SELECT, SL, MVT::f64,
1803 ExpEqNegOne,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001804 DAG.getConstantFP(1.0, SL, MVT::f64),
1805 DAG.getConstantFP(0.0, SL, MVT::f64));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001806
1807 SDValue S = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, Mag, X);
1808
1809 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpLt0, S, K);
1810 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpGt51, X, K);
1811
1812 return K;
1813}
1814
1815SDValue AMDGPUTargetLowering::LowerFROUND(SDValue Op, SelectionDAG &DAG) const {
1816 EVT VT = Op.getValueType();
1817
1818 if (VT == MVT::f32)
1819 return LowerFROUND32(Op, DAG);
1820
1821 if (VT == MVT::f64)
1822 return LowerFROUND64(Op, DAG);
1823
1824 llvm_unreachable("unhandled type");
1825}
1826
Matt Arsenault46010932014-06-18 17:05:30 +00001827SDValue AMDGPUTargetLowering::LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const {
1828 SDLoc SL(Op);
1829 SDValue Src = Op.getOperand(0);
1830
1831 // result = trunc(src);
1832 // if (src < 0.0 && src != result)
1833 // result += -1.0.
1834
1835 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1836
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001837 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64);
1838 const SDValue NegOne = DAG.getConstantFP(-1.0, SL, MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001839
Mehdi Amini44ede332015-07-09 02:09:04 +00001840 EVT SetCCVT =
1841 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001842
1843 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOLT);
1844 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1845 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1846
1847 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, NegOne, Zero);
Sanjay Patela2607012015-09-16 16:31:21 +00001848 // TODO: Should this propagate fast-math-flags?
Matt Arsenault46010932014-06-18 17:05:30 +00001849 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1850}
1851
Matt Arsenaultf058d672016-01-11 16:50:29 +00001852SDValue AMDGPUTargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
1853 SDLoc SL(Op);
1854 SDValue Src = Op.getOperand(0);
Matt Arsenaultf058d672016-01-11 16:50:29 +00001855 bool ZeroUndef = Op.getOpcode() == ISD::CTLZ_ZERO_UNDEF;
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00001856
1857 if (ZeroUndef && Src.getValueType() == MVT::i32)
1858 return DAG.getNode(AMDGPUISD::FFBH_U32, SL, MVT::i32, Src);
1859
Matt Arsenaultf058d672016-01-11 16:50:29 +00001860 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1861
1862 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1863 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1864
1865 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1866 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1867
1868 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(),
1869 *DAG.getContext(), MVT::i32);
1870
1871 SDValue Hi0 = DAG.getSetCC(SL, SetCCVT, Hi, Zero, ISD::SETEQ);
1872
1873 SDValue CtlzLo = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Lo);
1874 SDValue CtlzHi = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Hi);
1875
1876 const SDValue Bits32 = DAG.getConstant(32, SL, MVT::i32);
1877 SDValue Add = DAG.getNode(ISD::ADD, SL, MVT::i32, CtlzLo, Bits32);
1878
1879 // ctlz(x) = hi_32(x) == 0 ? ctlz(lo_32(x)) + 32 : ctlz(hi_32(x))
1880 SDValue NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32, Hi0, Add, CtlzHi);
1881
1882 if (!ZeroUndef) {
1883 // Test if the full 64-bit input is zero.
1884
1885 // FIXME: DAG combines turn what should be an s_and_b64 into a v_or_b32,
1886 // which we probably don't want.
1887 SDValue Lo0 = DAG.getSetCC(SL, SetCCVT, Lo, Zero, ISD::SETEQ);
1888 SDValue SrcIsZero = DAG.getNode(ISD::AND, SL, SetCCVT, Lo0, Hi0);
1889
1890 // TODO: If i64 setcc is half rate, it can result in 1 fewer instruction
1891 // with the same cycles, otherwise it is slower.
1892 // SDValue SrcIsZero = DAG.getSetCC(SL, SetCCVT, Src,
1893 // DAG.getConstant(0, SL, MVT::i64), ISD::SETEQ);
1894
1895 const SDValue Bits32 = DAG.getConstant(64, SL, MVT::i32);
1896
1897 // The instruction returns -1 for 0 input, but the defined intrinsic
1898 // behavior is to return the number of bits.
1899 NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32,
1900 SrcIsZero, Bits32, NewCtlz);
1901 }
1902
1903 return DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i64, NewCtlz);
1904}
1905
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00001906SDValue AMDGPUTargetLowering::LowerINT_TO_FP32(SDValue Op, SelectionDAG &DAG,
1907 bool Signed) const {
1908 // Unsigned
1909 // cul2f(ulong u)
1910 //{
1911 // uint lz = clz(u);
1912 // uint e = (u != 0) ? 127U + 63U - lz : 0;
1913 // u = (u << lz) & 0x7fffffffffffffffUL;
1914 // ulong t = u & 0xffffffffffUL;
1915 // uint v = (e << 23) | (uint)(u >> 40);
1916 // uint r = t > 0x8000000000UL ? 1U : (t == 0x8000000000UL ? v & 1U : 0U);
1917 // return as_float(v + r);
1918 //}
1919 // Signed
1920 // cl2f(long l)
1921 //{
1922 // long s = l >> 63;
1923 // float r = cul2f((l + s) ^ s);
1924 // return s ? -r : r;
1925 //}
1926
1927 SDLoc SL(Op);
1928 SDValue Src = Op.getOperand(0);
1929 SDValue L = Src;
1930
1931 SDValue S;
1932 if (Signed) {
1933 const SDValue SignBit = DAG.getConstant(63, SL, MVT::i64);
1934 S = DAG.getNode(ISD::SRA, SL, MVT::i64, L, SignBit);
1935
1936 SDValue LPlusS = DAG.getNode(ISD::ADD, SL, MVT::i64, L, S);
1937 L = DAG.getNode(ISD::XOR, SL, MVT::i64, LPlusS, S);
1938 }
1939
1940 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(),
1941 *DAG.getContext(), MVT::f32);
1942
1943
1944 SDValue ZeroI32 = DAG.getConstant(0, SL, MVT::i32);
1945 SDValue ZeroI64 = DAG.getConstant(0, SL, MVT::i64);
1946 SDValue LZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i64, L);
1947 LZ = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LZ);
1948
1949 SDValue K = DAG.getConstant(127U + 63U, SL, MVT::i32);
1950 SDValue E = DAG.getSelect(SL, MVT::i32,
1951 DAG.getSetCC(SL, SetCCVT, L, ZeroI64, ISD::SETNE),
1952 DAG.getNode(ISD::SUB, SL, MVT::i32, K, LZ),
1953 ZeroI32);
1954
1955 SDValue U = DAG.getNode(ISD::AND, SL, MVT::i64,
1956 DAG.getNode(ISD::SHL, SL, MVT::i64, L, LZ),
1957 DAG.getConstant((-1ULL) >> 1, SL, MVT::i64));
1958
1959 SDValue T = DAG.getNode(ISD::AND, SL, MVT::i64, U,
1960 DAG.getConstant(0xffffffffffULL, SL, MVT::i64));
1961
1962 SDValue UShl = DAG.getNode(ISD::SRL, SL, MVT::i64,
1963 U, DAG.getConstant(40, SL, MVT::i64));
1964
1965 SDValue V = DAG.getNode(ISD::OR, SL, MVT::i32,
1966 DAG.getNode(ISD::SHL, SL, MVT::i32, E, DAG.getConstant(23, SL, MVT::i32)),
1967 DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, UShl));
1968
1969 SDValue C = DAG.getConstant(0x8000000000ULL, SL, MVT::i64);
1970 SDValue RCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETUGT);
1971 SDValue TCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETEQ);
1972
1973 SDValue One = DAG.getConstant(1, SL, MVT::i32);
1974
1975 SDValue VTrunc1 = DAG.getNode(ISD::AND, SL, MVT::i32, V, One);
1976
1977 SDValue R = DAG.getSelect(SL, MVT::i32,
1978 RCmp,
1979 One,
1980 DAG.getSelect(SL, MVT::i32, TCmp, VTrunc1, ZeroI32));
1981 R = DAG.getNode(ISD::ADD, SL, MVT::i32, V, R);
1982 R = DAG.getNode(ISD::BITCAST, SL, MVT::f32, R);
1983
1984 if (!Signed)
1985 return R;
1986
1987 SDValue RNeg = DAG.getNode(ISD::FNEG, SL, MVT::f32, R);
1988 return DAG.getSelect(SL, MVT::f32, DAG.getSExtOrTrunc(S, SL, SetCCVT), RNeg, R);
1989}
1990
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001991SDValue AMDGPUTargetLowering::LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG,
1992 bool Signed) const {
1993 SDLoc SL(Op);
1994 SDValue Src = Op.getOperand(0);
1995
1996 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1997
1998 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001999 DAG.getConstant(0, SL, MVT::i32));
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002000 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002001 DAG.getConstant(1, SL, MVT::i32));
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002002
2003 SDValue CvtHi = DAG.getNode(Signed ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
2004 SL, MVT::f64, Hi);
2005
2006 SDValue CvtLo = DAG.getNode(ISD::UINT_TO_FP, SL, MVT::f64, Lo);
2007
2008 SDValue LdExp = DAG.getNode(AMDGPUISD::LDEXP, SL, MVT::f64, CvtHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002009 DAG.getConstant(32, SL, MVT::i32));
Sanjay Patela2607012015-09-16 16:31:21 +00002010 // TODO: Should this propagate fast-math-flags?
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002011 return DAG.getNode(ISD::FADD, SL, MVT::f64, LdExp, CvtLo);
2012}
2013
Tom Stellardc947d8c2013-10-30 17:22:05 +00002014SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op,
2015 SelectionDAG &DAG) const {
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00002016 assert(Op.getOperand(0).getValueType() == MVT::i64 &&
2017 "operation should be legal");
Tom Stellardc947d8c2013-10-30 17:22:05 +00002018
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002019 // TODO: Factor out code common with LowerSINT_TO_FP.
2020
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002021 EVT DestVT = Op.getValueType();
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002022 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) {
2023 SDLoc DL(Op);
2024 SDValue Src = Op.getOperand(0);
2025
2026 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src);
2027 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op));
2028 SDValue FPRound =
2029 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag);
2030
2031 return FPRound;
2032 }
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002033
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00002034 if (DestVT == MVT::f32)
2035 return LowerINT_TO_FP32(Op, DAG, false);
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002036
Matt Arsenaultedc7dcb2016-07-28 00:32:05 +00002037 assert(DestVT == MVT::f64);
2038 return LowerINT_TO_FP64(Op, DAG, false);
Tom Stellardc947d8c2013-10-30 17:22:05 +00002039}
Tom Stellardfbab8272013-08-16 01:12:11 +00002040
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002041SDValue AMDGPUTargetLowering::LowerSINT_TO_FP(SDValue Op,
2042 SelectionDAG &DAG) const {
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00002043 assert(Op.getOperand(0).getValueType() == MVT::i64 &&
2044 "operation should be legal");
2045
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002046 // TODO: Factor out code common with LowerUINT_TO_FP.
2047
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00002048 EVT DestVT = Op.getValueType();
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002049 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) {
2050 SDLoc DL(Op);
2051 SDValue Src = Op.getOperand(0);
2052
2053 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src);
2054 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op));
2055 SDValue FPRound =
2056 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag);
2057
2058 return FPRound;
2059 }
2060
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00002061 if (DestVT == MVT::f32)
2062 return LowerINT_TO_FP32(Op, DAG, true);
2063
Matt Arsenaultedc7dcb2016-07-28 00:32:05 +00002064 assert(DestVT == MVT::f64);
2065 return LowerINT_TO_FP64(Op, DAG, true);
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002066}
2067
Matt Arsenaultc9961752014-10-03 23:54:56 +00002068SDValue AMDGPUTargetLowering::LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG,
2069 bool Signed) const {
2070 SDLoc SL(Op);
2071
2072 SDValue Src = Op.getOperand(0);
2073
2074 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
2075
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002076 SDValue K0 = DAG.getConstantFP(BitsToDouble(UINT64_C(0x3df0000000000000)), SL,
2077 MVT::f64);
2078 SDValue K1 = DAG.getConstantFP(BitsToDouble(UINT64_C(0xc1f0000000000000)), SL,
2079 MVT::f64);
Sanjay Patela2607012015-09-16 16:31:21 +00002080 // TODO: Should this propagate fast-math-flags?
Matt Arsenaultc9961752014-10-03 23:54:56 +00002081 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, Trunc, K0);
2082
2083 SDValue FloorMul = DAG.getNode(ISD::FFLOOR, SL, MVT::f64, Mul);
2084
2085
2086 SDValue Fma = DAG.getNode(ISD::FMA, SL, MVT::f64, FloorMul, K1, Trunc);
2087
2088 SDValue Hi = DAG.getNode(Signed ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, SL,
2089 MVT::i32, FloorMul);
2090 SDValue Lo = DAG.getNode(ISD::FP_TO_UINT, SL, MVT::i32, Fma);
2091
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002092 SDValue Result = DAG.getBuildVector(MVT::v2i32, SL, {Lo, Hi});
Matt Arsenaultc9961752014-10-03 23:54:56 +00002093
2094 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Result);
2095}
2096
Tom Stellard94c21bc2016-11-01 16:31:48 +00002097SDValue AMDGPUTargetLowering::LowerFP_TO_FP16(SDValue Op, SelectionDAG &DAG) const {
2098
2099 if (getTargetMachine().Options.UnsafeFPMath) {
2100 // There is a generic expand for FP_TO_FP16 with unsafe fast math.
2101 return SDValue();
2102 }
2103
2104 SDLoc DL(Op);
2105 SDValue N0 = Op.getOperand(0);
Tom Stellard9677b602016-11-01 17:20:03 +00002106 assert (N0.getSimpleValueType() == MVT::f64);
Tom Stellard94c21bc2016-11-01 16:31:48 +00002107
2108 // f64 -> f16 conversion using round-to-nearest-even rounding mode.
2109 const unsigned ExpMask = 0x7ff;
2110 const unsigned ExpBiasf64 = 1023;
2111 const unsigned ExpBiasf16 = 15;
2112 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2113 SDValue One = DAG.getConstant(1, DL, MVT::i32);
2114 SDValue U = DAG.getNode(ISD::BITCAST, DL, MVT::i64, N0);
2115 SDValue UH = DAG.getNode(ISD::SRL, DL, MVT::i64, U,
2116 DAG.getConstant(32, DL, MVT::i64));
2117 UH = DAG.getZExtOrTrunc(UH, DL, MVT::i32);
2118 U = DAG.getZExtOrTrunc(U, DL, MVT::i32);
2119 SDValue E = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2120 DAG.getConstant(20, DL, MVT::i64));
2121 E = DAG.getNode(ISD::AND, DL, MVT::i32, E,
2122 DAG.getConstant(ExpMask, DL, MVT::i32));
2123 // Subtract the fp64 exponent bias (1023) to get the real exponent and
2124 // add the f16 bias (15) to get the biased exponent for the f16 format.
2125 E = DAG.getNode(ISD::ADD, DL, MVT::i32, E,
2126 DAG.getConstant(-ExpBiasf64 + ExpBiasf16, DL, MVT::i32));
2127
2128 SDValue M = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2129 DAG.getConstant(8, DL, MVT::i32));
2130 M = DAG.getNode(ISD::AND, DL, MVT::i32, M,
2131 DAG.getConstant(0xffe, DL, MVT::i32));
2132
2133 SDValue MaskedSig = DAG.getNode(ISD::AND, DL, MVT::i32, UH,
2134 DAG.getConstant(0x1ff, DL, MVT::i32));
2135 MaskedSig = DAG.getNode(ISD::OR, DL, MVT::i32, MaskedSig, U);
2136
2137 SDValue Lo40Set = DAG.getSelectCC(DL, MaskedSig, Zero, Zero, One, ISD::SETEQ);
2138 M = DAG.getNode(ISD::OR, DL, MVT::i32, M, Lo40Set);
2139
2140 // (M != 0 ? 0x0200 : 0) | 0x7c00;
2141 SDValue I = DAG.getNode(ISD::OR, DL, MVT::i32,
2142 DAG.getSelectCC(DL, M, Zero, DAG.getConstant(0x0200, DL, MVT::i32),
2143 Zero, ISD::SETNE), DAG.getConstant(0x7c00, DL, MVT::i32));
2144
2145 // N = M | (E << 12);
2146 SDValue N = DAG.getNode(ISD::OR, DL, MVT::i32, M,
2147 DAG.getNode(ISD::SHL, DL, MVT::i32, E,
2148 DAG.getConstant(12, DL, MVT::i32)));
2149
2150 // B = clamp(1-E, 0, 13);
2151 SDValue OneSubExp = DAG.getNode(ISD::SUB, DL, MVT::i32,
2152 One, E);
2153 SDValue B = DAG.getNode(ISD::SMAX, DL, MVT::i32, OneSubExp, Zero);
2154 B = DAG.getNode(ISD::SMIN, DL, MVT::i32, B,
2155 DAG.getConstant(13, DL, MVT::i32));
2156
2157 SDValue SigSetHigh = DAG.getNode(ISD::OR, DL, MVT::i32, M,
2158 DAG.getConstant(0x1000, DL, MVT::i32));
2159
2160 SDValue D = DAG.getNode(ISD::SRL, DL, MVT::i32, SigSetHigh, B);
2161 SDValue D0 = DAG.getNode(ISD::SHL, DL, MVT::i32, D, B);
2162 SDValue D1 = DAG.getSelectCC(DL, D0, SigSetHigh, One, Zero, ISD::SETNE);
2163 D = DAG.getNode(ISD::OR, DL, MVT::i32, D, D1);
2164
2165 SDValue V = DAG.getSelectCC(DL, E, One, D, N, ISD::SETLT);
2166 SDValue VLow3 = DAG.getNode(ISD::AND, DL, MVT::i32, V,
2167 DAG.getConstant(0x7, DL, MVT::i32));
2168 V = DAG.getNode(ISD::SRL, DL, MVT::i32, V,
2169 DAG.getConstant(2, DL, MVT::i32));
2170 SDValue V0 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(3, DL, MVT::i32),
2171 One, Zero, ISD::SETEQ);
2172 SDValue V1 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(5, DL, MVT::i32),
2173 One, Zero, ISD::SETGT);
2174 V1 = DAG.getNode(ISD::OR, DL, MVT::i32, V0, V1);
2175 V = DAG.getNode(ISD::ADD, DL, MVT::i32, V, V1);
2176
2177 V = DAG.getSelectCC(DL, E, DAG.getConstant(30, DL, MVT::i32),
2178 DAG.getConstant(0x7c00, DL, MVT::i32), V, ISD::SETGT);
2179 V = DAG.getSelectCC(DL, E, DAG.getConstant(1039, DL, MVT::i32),
2180 I, V, ISD::SETEQ);
2181
2182 // Extract the sign bit.
2183 SDValue Sign = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2184 DAG.getConstant(16, DL, MVT::i32));
2185 Sign = DAG.getNode(ISD::AND, DL, MVT::i32, Sign,
2186 DAG.getConstant(0x8000, DL, MVT::i32));
2187
2188 V = DAG.getNode(ISD::OR, DL, MVT::i32, Sign, V);
2189 return DAG.getZExtOrTrunc(V, DL, Op.getValueType());
2190}
2191
Matt Arsenaultc9961752014-10-03 23:54:56 +00002192SDValue AMDGPUTargetLowering::LowerFP_TO_SINT(SDValue Op,
2193 SelectionDAG &DAG) const {
2194 SDValue Src = Op.getOperand(0);
2195
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002196 // TODO: Factor out code common with LowerFP_TO_UINT.
2197
2198 EVT SrcVT = Src.getValueType();
2199 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) {
2200 SDLoc DL(Op);
2201
2202 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src);
2203 SDValue FpToInt32 =
2204 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend);
2205
2206 return FpToInt32;
2207 }
2208
Matt Arsenaultc9961752014-10-03 23:54:56 +00002209 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64)
2210 return LowerFP64_TO_INT(Op, DAG, true);
2211
2212 return SDValue();
2213}
2214
2215SDValue AMDGPUTargetLowering::LowerFP_TO_UINT(SDValue Op,
2216 SelectionDAG &DAG) const {
2217 SDValue Src = Op.getOperand(0);
2218
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002219 // TODO: Factor out code common with LowerFP_TO_SINT.
2220
2221 EVT SrcVT = Src.getValueType();
2222 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) {
2223 SDLoc DL(Op);
2224
2225 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src);
2226 SDValue FpToInt32 =
2227 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend);
2228
2229 return FpToInt32;
2230 }
2231
Matt Arsenaultc9961752014-10-03 23:54:56 +00002232 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64)
2233 return LowerFP64_TO_INT(Op, DAG, false);
2234
2235 return SDValue();
2236}
2237
Matt Arsenaultfae02982014-03-17 18:58:11 +00002238SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
2239 SelectionDAG &DAG) const {
2240 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
2241 MVT VT = Op.getSimpleValueType();
2242 MVT ScalarVT = VT.getScalarType();
2243
Matt Arsenaultedc7dcb2016-07-28 00:32:05 +00002244 assert(VT.isVector());
Matt Arsenaultfae02982014-03-17 18:58:11 +00002245
2246 SDValue Src = Op.getOperand(0);
Matt Arsenault5dbd5db2014-04-22 03:49:30 +00002247 SDLoc DL(Op);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002248
Matt Arsenault5dbd5db2014-04-22 03:49:30 +00002249 // TODO: Don't scalarize on Evergreen?
2250 unsigned NElts = VT.getVectorNumElements();
2251 SmallVector<SDValue, 8> Args;
2252 DAG.ExtractVectorElements(Src, Args, 0, NElts);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002253
Matt Arsenault5dbd5db2014-04-22 03:49:30 +00002254 SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType());
2255 for (unsigned I = 0; I < NElts; ++I)
2256 Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002257
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002258 return DAG.getBuildVector(VT, DL, Args);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002259}
2260
Tom Stellard75aadc22012-12-11 21:25:42 +00002261//===----------------------------------------------------------------------===//
Tom Stellard50122a52014-04-07 19:45:41 +00002262// Custom DAG optimizations
2263//===----------------------------------------------------------------------===//
2264
2265static bool isU24(SDValue Op, SelectionDAG &DAG) {
2266 APInt KnownZero, KnownOne;
2267 EVT VT = Op.getValueType();
Jay Foada0653a32014-05-14 21:14:37 +00002268 DAG.computeKnownBits(Op, KnownZero, KnownOne);
Tom Stellard50122a52014-04-07 19:45:41 +00002269
2270 return (VT.getSizeInBits() - KnownZero.countLeadingOnes()) <= 24;
2271}
2272
2273static bool isI24(SDValue Op, SelectionDAG &DAG) {
2274 EVT VT = Op.getValueType();
2275
2276 // In order for this to be a signed 24-bit value, bit 23, must
2277 // be a sign bit.
2278 return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated
2279 // as unsigned 24-bit values.
2280 (VT.getSizeInBits() - DAG.ComputeNumSignBits(Op)) < 24;
2281}
2282
Tom Stellard09c2bd62016-10-14 19:14:29 +00002283static bool simplifyI24(SDNode *Node24, unsigned OpIdx,
2284 TargetLowering::DAGCombinerInfo &DCI) {
Tom Stellard50122a52014-04-07 19:45:41 +00002285
2286 SelectionDAG &DAG = DCI.DAG;
Tom Stellard09c2bd62016-10-14 19:14:29 +00002287 SDValue Op = Node24->getOperand(OpIdx);
Tom Stellard50122a52014-04-07 19:45:41 +00002288 EVT VT = Op.getValueType();
2289
2290 APInt Demanded = APInt::getLowBitsSet(VT.getSizeInBits(), 24);
2291 APInt KnownZero, KnownOne;
2292 TargetLowering::TargetLoweringOpt TLO(DAG, true, true);
Tom Stellard09c2bd62016-10-14 19:14:29 +00002293 if (TLO.SimplifyDemandedBits(Node24, OpIdx, Demanded, DCI))
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002294 return true;
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002295
2296 return false;
Tom Stellard50122a52014-04-07 19:45:41 +00002297}
2298
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002299template <typename IntTy>
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002300static SDValue constantFoldBFE(SelectionDAG &DAG, IntTy Src0, uint32_t Offset,
2301 uint32_t Width, const SDLoc &DL) {
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002302 if (Width + Offset < 32) {
Matt Arsenault46cbc432014-09-19 00:42:06 +00002303 uint32_t Shl = static_cast<uint32_t>(Src0) << (32 - Offset - Width);
2304 IntTy Result = static_cast<IntTy>(Shl) >> (32 - Width);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002305 return DAG.getConstant(Result, DL, MVT::i32);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002306 }
2307
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002308 return DAG.getConstant(Src0 >> Offset, DL, MVT::i32);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002309}
2310
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002311static bool hasVolatileUser(SDNode *Val) {
2312 for (SDNode *U : Val->uses()) {
2313 if (MemSDNode *M = dyn_cast<MemSDNode>(U)) {
2314 if (M->isVolatile())
2315 return true;
2316 }
2317 }
2318
2319 return false;
2320}
2321
Matt Arsenault8af47a02016-07-01 22:55:55 +00002322bool AMDGPUTargetLowering::shouldCombineMemoryType(EVT VT) const {
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002323 // i32 vectors are the canonical memory type.
2324 if (VT.getScalarType() == MVT::i32 || isTypeLegal(VT))
2325 return false;
2326
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002327 if (!VT.isByteSized())
2328 return false;
2329
2330 unsigned Size = VT.getStoreSize();
2331
2332 if ((Size == 1 || Size == 2 || Size == 4) && !VT.isVector())
2333 return false;
2334
2335 if (Size == 3 || (Size > 4 && (Size % 4 != 0)))
2336 return false;
2337
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002338 return true;
2339}
2340
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002341// Replace load of an illegal type with a store of a bitcast to a friendlier
2342// type.
2343SDValue AMDGPUTargetLowering::performLoadCombine(SDNode *N,
2344 DAGCombinerInfo &DCI) const {
2345 if (!DCI.isBeforeLegalize())
2346 return SDValue();
2347
2348 LoadSDNode *LN = cast<LoadSDNode>(N);
2349 if (LN->isVolatile() || !ISD::isNormalLoad(LN) || hasVolatileUser(LN))
2350 return SDValue();
2351
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002352 SDLoc SL(N);
2353 SelectionDAG &DAG = DCI.DAG;
2354 EVT VT = LN->getMemoryVT();
Matt Arsenault8af47a02016-07-01 22:55:55 +00002355
2356 unsigned Size = VT.getStoreSize();
2357 unsigned Align = LN->getAlignment();
2358 if (Align < Size && isTypeLegal(VT)) {
2359 bool IsFast;
2360 unsigned AS = LN->getAddressSpace();
2361
2362 // Expand unaligned loads earlier than legalization. Due to visitation order
2363 // problems during legalization, the emitted instructions to pack and unpack
2364 // the bytes again are not eliminated in the case of an unaligned copy.
2365 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) {
Matt Arsenaultb50eb8d2016-08-31 21:52:27 +00002366 if (VT.isVector())
2367 return scalarizeVectorLoad(LN, DAG);
2368
Matt Arsenault8af47a02016-07-01 22:55:55 +00002369 SDValue Ops[2];
2370 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(LN, DAG);
2371 return DAG.getMergeValues(Ops, SDLoc(N));
2372 }
2373
2374 if (!IsFast)
2375 return SDValue();
2376 }
2377
2378 if (!shouldCombineMemoryType(VT))
2379 return SDValue();
2380
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002381 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
2382
2383 SDValue NewLoad
2384 = DAG.getLoad(NewVT, SL, LN->getChain(),
2385 LN->getBasePtr(), LN->getMemOperand());
2386
2387 SDValue BC = DAG.getNode(ISD::BITCAST, SL, VT, NewLoad);
2388 DCI.CombineTo(N, BC, NewLoad.getValue(1));
2389 return SDValue(N, 0);
2390}
2391
2392// Replace store of an illegal type with a store of a bitcast to a friendlier
2393// type.
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002394SDValue AMDGPUTargetLowering::performStoreCombine(SDNode *N,
2395 DAGCombinerInfo &DCI) const {
2396 if (!DCI.isBeforeLegalize())
2397 return SDValue();
2398
2399 StoreSDNode *SN = cast<StoreSDNode>(N);
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002400 if (SN->isVolatile() || !ISD::isNormalStore(SN))
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002401 return SDValue();
2402
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002403 EVT VT = SN->getMemoryVT();
Matt Arsenault8af47a02016-07-01 22:55:55 +00002404 unsigned Size = VT.getStoreSize();
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002405
2406 SDLoc SL(N);
2407 SelectionDAG &DAG = DCI.DAG;
Matt Arsenault8af47a02016-07-01 22:55:55 +00002408 unsigned Align = SN->getAlignment();
2409 if (Align < Size && isTypeLegal(VT)) {
2410 bool IsFast;
2411 unsigned AS = SN->getAddressSpace();
2412
2413 // Expand unaligned stores earlier than legalization. Due to visitation
2414 // order problems during legalization, the emitted instructions to pack and
2415 // unpack the bytes again are not eliminated in the case of an unaligned
2416 // copy.
Matt Arsenaultb50eb8d2016-08-31 21:52:27 +00002417 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) {
2418 if (VT.isVector())
2419 return scalarizeVectorStore(SN, DAG);
2420
Matt Arsenault8af47a02016-07-01 22:55:55 +00002421 return expandUnalignedStore(SN, DAG);
Matt Arsenaultb50eb8d2016-08-31 21:52:27 +00002422 }
Matt Arsenault8af47a02016-07-01 22:55:55 +00002423
2424 if (!IsFast)
2425 return SDValue();
2426 }
2427
2428 if (!shouldCombineMemoryType(VT))
2429 return SDValue();
2430
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002431 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
Matt Arsenault8af47a02016-07-01 22:55:55 +00002432 SDValue Val = SN->getValue();
2433
2434 //DCI.AddToWorklist(Val.getNode());
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002435
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002436 bool OtherUses = !Val.hasOneUse();
2437 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NewVT, Val);
2438 if (OtherUses) {
2439 SDValue CastBack = DAG.getNode(ISD::BITCAST, SL, VT, CastVal);
2440 DAG.ReplaceAllUsesOfValueWith(Val, CastBack);
2441 }
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002442
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002443 return DAG.getStore(SN->getChain(), SL, CastVal,
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002444 SN->getBasePtr(), SN->getMemOperand());
2445}
2446
Matt Arsenault2fdf2a12017-02-21 23:35:48 +00002447SDValue AMDGPUTargetLowering::performClampCombine(SDNode *N,
2448 DAGCombinerInfo &DCI) const {
2449 ConstantFPSDNode *CSrc = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
2450 if (!CSrc)
2451 return SDValue();
2452
2453 const APFloat &F = CSrc->getValueAPF();
2454 APFloat Zero = APFloat::getZero(F.getSemantics());
2455 APFloat::cmpResult Cmp0 = F.compare(Zero);
2456 if (Cmp0 == APFloat::cmpLessThan ||
2457 (Cmp0 == APFloat::cmpUnordered && Subtarget->enableDX10Clamp())) {
2458 return DCI.DAG.getConstantFP(Zero, SDLoc(N), N->getValueType(0));
2459 }
2460
2461 APFloat One(F.getSemantics(), "1.0");
2462 APFloat::cmpResult Cmp1 = F.compare(One);
2463 if (Cmp1 == APFloat::cmpGreaterThan)
2464 return DCI.DAG.getConstantFP(One, SDLoc(N), N->getValueType(0));
2465
2466 return SDValue(CSrc, 0);
2467}
2468
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002469/// Split the 64-bit value \p LHS into two 32-bit components, and perform the
2470/// binary operation \p Opc to it with the corresponding constant operands.
2471SDValue AMDGPUTargetLowering::splitBinaryBitConstantOpImpl(
2472 DAGCombinerInfo &DCI, const SDLoc &SL,
2473 unsigned Opc, SDValue LHS,
2474 uint32_t ValLo, uint32_t ValHi) const {
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002475 SelectionDAG &DAG = DCI.DAG;
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002476 SDValue Lo, Hi;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002477 std::tie(Lo, Hi) = split64BitValue(LHS, DAG);
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002478
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002479 SDValue LoRHS = DAG.getConstant(ValLo, SL, MVT::i32);
2480 SDValue HiRHS = DAG.getConstant(ValHi, SL, MVT::i32);
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002481
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002482 SDValue LoAnd = DAG.getNode(Opc, SL, MVT::i32, Lo, LoRHS);
2483 SDValue HiAnd = DAG.getNode(Opc, SL, MVT::i32, Hi, HiRHS);
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002484
Matt Arsenaultefa3fe12016-04-22 22:48:38 +00002485 // Re-visit the ands. It's possible we eliminated one of them and it could
2486 // simplify the vector.
2487 DCI.AddToWorklist(Lo.getNode());
2488 DCI.AddToWorklist(Hi.getNode());
2489
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002490 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {LoAnd, HiAnd});
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002491 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2492}
2493
Matt Arsenault24692112015-07-14 18:20:33 +00002494SDValue AMDGPUTargetLowering::performShlCombine(SDNode *N,
2495 DAGCombinerInfo &DCI) const {
2496 if (N->getValueType(0) != MVT::i64)
2497 return SDValue();
2498
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002499 // i64 (shl x, C) -> (build_pair 0, (shl x, C -32))
Matt Arsenault24692112015-07-14 18:20:33 +00002500
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002501 // On some subtargets, 64-bit shift is a quarter rate instruction. In the
2502 // common case, splitting this into a move and a 32-bit shift is faster and
2503 // the same code size.
Matt Arsenault24692112015-07-14 18:20:33 +00002504 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002505 if (!RHS)
2506 return SDValue();
2507
2508 unsigned RHSVal = RHS->getZExtValue();
2509 if (RHSVal < 32)
Matt Arsenault24692112015-07-14 18:20:33 +00002510 return SDValue();
2511
2512 SDValue LHS = N->getOperand(0);
2513
2514 SDLoc SL(N);
2515 SelectionDAG &DAG = DCI.DAG;
2516
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002517 SDValue ShiftAmt = DAG.getConstant(RHSVal - 32, SL, MVT::i32);
2518
Matt Arsenault24692112015-07-14 18:20:33 +00002519 SDValue Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LHS);
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002520 SDValue NewShift = DAG.getNode(ISD::SHL, SL, MVT::i32, Lo, ShiftAmt);
Matt Arsenault24692112015-07-14 18:20:33 +00002521
2522 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
Matt Arsenault80edab92016-01-18 21:43:36 +00002523
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002524 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {Zero, NewShift});
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002525 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
Matt Arsenault24692112015-07-14 18:20:33 +00002526}
2527
Matt Arsenault33e3ece2016-01-18 22:09:04 +00002528SDValue AMDGPUTargetLowering::performSraCombine(SDNode *N,
2529 DAGCombinerInfo &DCI) const {
2530 if (N->getValueType(0) != MVT::i64)
2531 return SDValue();
2532
2533 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2534 if (!RHS)
2535 return SDValue();
2536
2537 SelectionDAG &DAG = DCI.DAG;
2538 SDLoc SL(N);
2539 unsigned RHSVal = RHS->getZExtValue();
2540
2541 // (sra i64:x, 32) -> build_pair x, (sra hi_32(x), 31)
2542 if (RHSVal == 32) {
2543 SDValue Hi = getHiHalf64(N->getOperand(0), DAG);
2544 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi,
2545 DAG.getConstant(31, SL, MVT::i32));
2546
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002547 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {Hi, NewShift});
Matt Arsenault33e3ece2016-01-18 22:09:04 +00002548 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec);
2549 }
2550
2551 // (sra i64:x, 63) -> build_pair (sra hi_32(x), 31), (sra hi_32(x), 31)
2552 if (RHSVal == 63) {
2553 SDValue Hi = getHiHalf64(N->getOperand(0), DAG);
2554 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi,
2555 DAG.getConstant(31, SL, MVT::i32));
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002556 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, NewShift});
Matt Arsenault33e3ece2016-01-18 22:09:04 +00002557 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec);
2558 }
2559
2560 return SDValue();
2561}
2562
Matt Arsenault80edab92016-01-18 21:43:36 +00002563SDValue AMDGPUTargetLowering::performSrlCombine(SDNode *N,
2564 DAGCombinerInfo &DCI) const {
2565 if (N->getValueType(0) != MVT::i64)
2566 return SDValue();
2567
2568 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2569 if (!RHS)
2570 return SDValue();
2571
2572 unsigned ShiftAmt = RHS->getZExtValue();
2573 if (ShiftAmt < 32)
2574 return SDValue();
2575
2576 // srl i64:x, C for C >= 32
2577 // =>
2578 // build_pair (srl hi_32(x), C - 32), 0
2579
2580 SelectionDAG &DAG = DCI.DAG;
2581 SDLoc SL(N);
2582
2583 SDValue One = DAG.getConstant(1, SL, MVT::i32);
2584 SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
2585
2586 SDValue VecOp = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, N->getOperand(0));
2587 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32,
2588 VecOp, One);
2589
2590 SDValue NewConst = DAG.getConstant(ShiftAmt - 32, SL, MVT::i32);
2591 SDValue NewShift = DAG.getNode(ISD::SRL, SL, MVT::i32, Hi, NewConst);
2592
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002593 SDValue BuildPair = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, Zero});
Matt Arsenault80edab92016-01-18 21:43:36 +00002594
2595 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildPair);
2596}
2597
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002598// We need to specifically handle i64 mul here to avoid unnecessary conversion
2599// instructions. If we only match on the legalized i64 mul expansion,
2600// SimplifyDemandedBits will be unable to remove them because there will be
2601// multiple uses due to the separate mul + mulh[su].
2602static SDValue getMul24(SelectionDAG &DAG, const SDLoc &SL,
2603 SDValue N0, SDValue N1, unsigned Size, bool Signed) {
2604 if (Size <= 32) {
2605 unsigned MulOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24;
2606 return DAG.getNode(MulOpc, SL, MVT::i32, N0, N1);
2607 }
2608
2609 // Because we want to eliminate extension instructions before the
2610 // operation, we need to create a single user here (i.e. not the separate
2611 // mul_lo + mul_hi) so that SimplifyDemandedBits will deal with it.
2612
2613 unsigned MulOpc = Signed ? AMDGPUISD::MUL_LOHI_I24 : AMDGPUISD::MUL_LOHI_U24;
2614
2615 SDValue Mul = DAG.getNode(MulOpc, SL,
2616 DAG.getVTList(MVT::i32, MVT::i32), N0, N1);
2617
2618 return DAG.getNode(ISD::BUILD_PAIR, SL, MVT::i64,
2619 Mul.getValue(0), Mul.getValue(1));
2620}
2621
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002622SDValue AMDGPUTargetLowering::performMulCombine(SDNode *N,
2623 DAGCombinerInfo &DCI) const {
2624 EVT VT = N->getValueType(0);
2625
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002626 unsigned Size = VT.getSizeInBits();
2627 if (VT.isVector() || Size > 64)
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002628 return SDValue();
2629
Tom Stellard115a6152016-11-10 16:02:37 +00002630 // There are i16 integer mul/mad.
2631 if (Subtarget->has16BitInsts() && VT.getScalarType().bitsLE(MVT::i16))
2632 return SDValue();
2633
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002634 SelectionDAG &DAG = DCI.DAG;
2635 SDLoc DL(N);
2636
2637 SDValue N0 = N->getOperand(0);
2638 SDValue N1 = N->getOperand(1);
2639 SDValue Mul;
2640
2641 if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) {
2642 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
2643 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002644 Mul = getMul24(DAG, DL, N0, N1, Size, false);
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002645 } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) {
2646 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
2647 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002648 Mul = getMul24(DAG, DL, N0, N1, Size, true);
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002649 } else {
2650 return SDValue();
2651 }
2652
2653 // We need to use sext even for MUL_U24, because MUL_U24 is used
2654 // for signed multiply of 8 and 16-bit types.
2655 return DAG.getSExtOrTrunc(Mul, DL, VT);
2656}
2657
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002658SDValue AMDGPUTargetLowering::performMulhsCombine(SDNode *N,
2659 DAGCombinerInfo &DCI) const {
2660 EVT VT = N->getValueType(0);
2661
2662 if (!Subtarget->hasMulI24() || VT.isVector())
2663 return SDValue();
2664
2665 SelectionDAG &DAG = DCI.DAG;
2666 SDLoc DL(N);
2667
2668 SDValue N0 = N->getOperand(0);
2669 SDValue N1 = N->getOperand(1);
2670
2671 if (!isI24(N0, DAG) || !isI24(N1, DAG))
2672 return SDValue();
2673
2674 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
2675 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
2676
2677 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_I24, DL, MVT::i32, N0, N1);
2678 DCI.AddToWorklist(Mulhi.getNode());
2679 return DAG.getSExtOrTrunc(Mulhi, DL, VT);
2680}
2681
2682SDValue AMDGPUTargetLowering::performMulhuCombine(SDNode *N,
2683 DAGCombinerInfo &DCI) const {
2684 EVT VT = N->getValueType(0);
2685
2686 if (!Subtarget->hasMulU24() || VT.isVector() || VT.getSizeInBits() > 32)
2687 return SDValue();
2688
2689 SelectionDAG &DAG = DCI.DAG;
2690 SDLoc DL(N);
2691
2692 SDValue N0 = N->getOperand(0);
2693 SDValue N1 = N->getOperand(1);
2694
2695 if (!isU24(N0, DAG) || !isU24(N1, DAG))
2696 return SDValue();
2697
2698 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
2699 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
2700
2701 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_U24, DL, MVT::i32, N0, N1);
2702 DCI.AddToWorklist(Mulhi.getNode());
2703 return DAG.getZExtOrTrunc(Mulhi, DL, VT);
2704}
2705
2706SDValue AMDGPUTargetLowering::performMulLoHi24Combine(
2707 SDNode *N, DAGCombinerInfo &DCI) const {
2708 SelectionDAG &DAG = DCI.DAG;
2709
Tom Stellard09c2bd62016-10-14 19:14:29 +00002710 // Simplify demanded bits before splitting into multiple users.
2711 if (simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI))
2712 return SDValue();
2713
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002714 SDValue N0 = N->getOperand(0);
2715 SDValue N1 = N->getOperand(1);
2716
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002717 bool Signed = (N->getOpcode() == AMDGPUISD::MUL_LOHI_I24);
2718
2719 unsigned MulLoOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24;
2720 unsigned MulHiOpc = Signed ? AMDGPUISD::MULHI_I24 : AMDGPUISD::MULHI_U24;
2721
2722 SDLoc SL(N);
2723
2724 SDValue MulLo = DAG.getNode(MulLoOpc, SL, MVT::i32, N0, N1);
2725 SDValue MulHi = DAG.getNode(MulHiOpc, SL, MVT::i32, N0, N1);
2726 return DAG.getMergeValues({ MulLo, MulHi }, SL);
2727}
2728
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002729static bool isNegativeOne(SDValue Val) {
2730 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val))
2731 return C->isAllOnesValue();
2732 return false;
2733}
2734
2735static bool isCtlzOpc(unsigned Opc) {
2736 return Opc == ISD::CTLZ || Opc == ISD::CTLZ_ZERO_UNDEF;
2737}
2738
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002739SDValue AMDGPUTargetLowering::getFFBH_U32(SelectionDAG &DAG,
2740 SDValue Op,
2741 const SDLoc &DL) const {
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002742 EVT VT = Op.getValueType();
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002743 EVT LegalVT = getTypeToTransformTo(*DAG.getContext(), VT);
2744 if (LegalVT != MVT::i32 && (Subtarget->has16BitInsts() &&
2745 LegalVT != MVT::i16))
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002746 return SDValue();
2747
2748 if (VT != MVT::i32)
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002749 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Op);
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002750
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002751 SDValue FFBH = DAG.getNode(AMDGPUISD::FFBH_U32, DL, MVT::i32, Op);
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002752 if (VT != MVT::i32)
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002753 FFBH = DAG.getNode(ISD::TRUNCATE, DL, VT, FFBH);
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002754
2755 return FFBH;
2756}
2757
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002758// The native instructions return -1 on 0 input. Optimize out a select that
2759// produces -1 on 0.
2760//
2761// TODO: If zero is not undef, we could also do this if the output is compared
2762// against the bitwidth.
2763//
2764// TODO: Should probably combine against FFBH_U32 instead of ctlz directly.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002765SDValue AMDGPUTargetLowering::performCtlzCombine(const SDLoc &SL, SDValue Cond,
2766 SDValue LHS, SDValue RHS,
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002767 DAGCombinerInfo &DCI) const {
2768 ConstantSDNode *CmpRhs = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2769 if (!CmpRhs || !CmpRhs->isNullValue())
2770 return SDValue();
2771
2772 SelectionDAG &DAG = DCI.DAG;
2773 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
2774 SDValue CmpLHS = Cond.getOperand(0);
2775
2776 // select (setcc x, 0, eq), -1, (ctlz_zero_undef x) -> ffbh_u32 x
2777 if (CCOpcode == ISD::SETEQ &&
2778 isCtlzOpc(RHS.getOpcode()) &&
2779 RHS.getOperand(0) == CmpLHS &&
2780 isNegativeOne(LHS)) {
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002781 return getFFBH_U32(DAG, CmpLHS, SL);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002782 }
2783
2784 // select (setcc x, 0, ne), (ctlz_zero_undef x), -1 -> ffbh_u32 x
2785 if (CCOpcode == ISD::SETNE &&
2786 isCtlzOpc(LHS.getOpcode()) &&
2787 LHS.getOperand(0) == CmpLHS &&
2788 isNegativeOne(RHS)) {
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002789 return getFFBH_U32(DAG, CmpLHS, SL);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002790 }
2791
2792 return SDValue();
2793}
2794
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002795static SDValue distributeOpThroughSelect(TargetLowering::DAGCombinerInfo &DCI,
2796 unsigned Op,
2797 const SDLoc &SL,
2798 SDValue Cond,
2799 SDValue N1,
2800 SDValue N2) {
2801 SelectionDAG &DAG = DCI.DAG;
2802 EVT VT = N1.getValueType();
2803
2804 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT, Cond,
2805 N1.getOperand(0), N2.getOperand(0));
2806 DCI.AddToWorklist(NewSelect.getNode());
2807 return DAG.getNode(Op, SL, VT, NewSelect);
2808}
2809
2810// Pull a free FP operation out of a select so it may fold into uses.
2811//
2812// select c, (fneg x), (fneg y) -> fneg (select c, x, y)
2813// select c, (fneg x), k -> fneg (select c, x, (fneg k))
2814//
2815// select c, (fabs x), (fabs y) -> fabs (select c, x, y)
2816// select c, (fabs x), +k -> fabs (select c, x, k)
2817static SDValue foldFreeOpFromSelect(TargetLowering::DAGCombinerInfo &DCI,
2818 SDValue N) {
2819 SelectionDAG &DAG = DCI.DAG;
2820 SDValue Cond = N.getOperand(0);
2821 SDValue LHS = N.getOperand(1);
2822 SDValue RHS = N.getOperand(2);
2823
2824 EVT VT = N.getValueType();
2825 if ((LHS.getOpcode() == ISD::FABS && RHS.getOpcode() == ISD::FABS) ||
2826 (LHS.getOpcode() == ISD::FNEG && RHS.getOpcode() == ISD::FNEG)) {
2827 return distributeOpThroughSelect(DCI, LHS.getOpcode(),
2828 SDLoc(N), Cond, LHS, RHS);
2829 }
2830
2831 bool Inv = false;
2832 if (RHS.getOpcode() == ISD::FABS || RHS.getOpcode() == ISD::FNEG) {
2833 std::swap(LHS, RHS);
2834 Inv = true;
2835 }
2836
2837 // TODO: Support vector constants.
2838 ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
2839 if ((LHS.getOpcode() == ISD::FNEG || LHS.getOpcode() == ISD::FABS) && CRHS) {
2840 SDLoc SL(N);
2841 // If one side is an fneg/fabs and the other is a constant, we can push the
2842 // fneg/fabs down. If it's an fabs, the constant needs to be non-negative.
2843 SDValue NewLHS = LHS.getOperand(0);
2844 SDValue NewRHS = RHS;
2845
Matt Arsenault45337df2017-01-12 18:58:15 +00002846 // Careful: if the neg can be folded up, don't try to pull it back down.
2847 bool ShouldFoldNeg = true;
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002848
Matt Arsenault45337df2017-01-12 18:58:15 +00002849 if (NewLHS.hasOneUse()) {
2850 unsigned Opc = NewLHS.getOpcode();
2851 if (LHS.getOpcode() == ISD::FNEG && fnegFoldsIntoOp(Opc))
2852 ShouldFoldNeg = false;
2853 if (LHS.getOpcode() == ISD::FABS && Opc == ISD::FMUL)
2854 ShouldFoldNeg = false;
2855 }
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002856
Matt Arsenault45337df2017-01-12 18:58:15 +00002857 if (ShouldFoldNeg) {
2858 if (LHS.getOpcode() == ISD::FNEG)
2859 NewRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2860 else if (CRHS->isNegative())
2861 return SDValue();
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002862
Matt Arsenault45337df2017-01-12 18:58:15 +00002863 if (Inv)
2864 std::swap(NewLHS, NewRHS);
2865
2866 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT,
2867 Cond, NewLHS, NewRHS);
2868 DCI.AddToWorklist(NewSelect.getNode());
2869 return DAG.getNode(LHS.getOpcode(), SL, VT, NewSelect);
2870 }
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002871 }
2872
2873 return SDValue();
2874}
2875
2876
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002877SDValue AMDGPUTargetLowering::performSelectCombine(SDNode *N,
2878 DAGCombinerInfo &DCI) const {
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002879 if (SDValue Folded = foldFreeOpFromSelect(DCI, SDValue(N, 0)))
2880 return Folded;
2881
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002882 SDValue Cond = N->getOperand(0);
2883 if (Cond.getOpcode() != ISD::SETCC)
2884 return SDValue();
2885
2886 EVT VT = N->getValueType(0);
2887 SDValue LHS = Cond.getOperand(0);
2888 SDValue RHS = Cond.getOperand(1);
2889 SDValue CC = Cond.getOperand(2);
2890
2891 SDValue True = N->getOperand(1);
2892 SDValue False = N->getOperand(2);
2893
Matt Arsenault0b26e472016-12-22 21:40:08 +00002894 if (Cond.hasOneUse()) { // TODO: Look for multiple select uses.
2895 SelectionDAG &DAG = DCI.DAG;
2896 if ((DAG.isConstantValueOfAnyType(True) ||
2897 DAG.isConstantValueOfAnyType(True)) &&
2898 (!DAG.isConstantValueOfAnyType(False) &&
2899 !DAG.isConstantValueOfAnyType(False))) {
2900 // Swap cmp + select pair to move constant to false input.
2901 // This will allow using VOPC cndmasks more often.
2902 // select (setcc x, y), k, x -> select (setcc y, x) x, x
2903
2904 SDLoc SL(N);
2905 ISD::CondCode NewCC = getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2906 LHS.getValueType().isInteger());
2907
2908 SDValue NewCond = DAG.getSetCC(SL, Cond.getValueType(), LHS, RHS, NewCC);
2909 return DAG.getNode(ISD::SELECT, SL, VT, NewCond, False, True);
2910 }
Matt Arsenault0b26e472016-12-22 21:40:08 +00002911
Matt Arsenaultda7a6562017-02-01 00:42:40 +00002912 if (VT == MVT::f32 && Subtarget->hasFminFmaxLegacy()) {
2913 SDValue MinMax
2914 = combineFMinMaxLegacy(SDLoc(N), VT, LHS, RHS, True, False, CC, DCI);
2915 // Revisit this node so we can catch min3/max3/med3 patterns.
2916 //DCI.AddToWorklist(MinMax.getNode());
2917 return MinMax;
2918 }
Matt Arsenault5b39b342016-01-28 20:53:48 +00002919 }
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002920
2921 // There's no reason to not do this if the condition has other uses.
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002922 return performCtlzCombine(SDLoc(N), Cond, True, False, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002923}
2924
Matt Arsenault2511c032017-02-03 00:23:15 +00002925static bool isConstantFPZero(SDValue N) {
2926 if (const ConstantFPSDNode *C = isConstOrConstSplatFP(N))
2927 return C->isZero() && !C->isNegative();
2928 return false;
2929}
2930
Matt Arsenaulte1b59532017-02-03 00:51:50 +00002931static unsigned inverseMinMax(unsigned Opc) {
2932 switch (Opc) {
2933 case ISD::FMAXNUM:
2934 return ISD::FMINNUM;
2935 case ISD::FMINNUM:
2936 return ISD::FMAXNUM;
2937 case AMDGPUISD::FMAX_LEGACY:
2938 return AMDGPUISD::FMIN_LEGACY;
2939 case AMDGPUISD::FMIN_LEGACY:
2940 return AMDGPUISD::FMAX_LEGACY;
2941 default:
2942 llvm_unreachable("invalid min/max opcode");
2943 }
2944}
2945
Matt Arsenault2529fba2017-01-12 00:09:34 +00002946SDValue AMDGPUTargetLowering::performFNegCombine(SDNode *N,
2947 DAGCombinerInfo &DCI) const {
2948 SelectionDAG &DAG = DCI.DAG;
2949 SDValue N0 = N->getOperand(0);
2950 EVT VT = N->getValueType(0);
2951
2952 unsigned Opc = N0.getOpcode();
2953
2954 // If the input has multiple uses and we can either fold the negate down, or
2955 // the other uses cannot, give up. This both prevents unprofitable
2956 // transformations and infinite loops: we won't repeatedly try to fold around
2957 // a negate that has no 'good' form.
Matt Arsenaulta8fcfad2017-02-02 23:21:23 +00002958 if (N0.hasOneUse()) {
2959 // This may be able to fold into the source, but at a code size cost. Don't
2960 // fold if the fold into the user is free.
2961 if (allUsesHaveSourceMods(N, 0))
2962 return SDValue();
2963 } else {
2964 if (fnegFoldsIntoOp(Opc) &&
2965 (allUsesHaveSourceMods(N) || !allUsesHaveSourceMods(N0.getNode())))
2966 return SDValue();
2967 }
Matt Arsenault2529fba2017-01-12 00:09:34 +00002968
2969 SDLoc SL(N);
2970 switch (Opc) {
2971 case ISD::FADD: {
Matt Arsenault3e6f9b52017-01-19 06:35:27 +00002972 if (!mayIgnoreSignedZero(N0))
2973 return SDValue();
2974
Matt Arsenault2529fba2017-01-12 00:09:34 +00002975 // (fneg (fadd x, y)) -> (fadd (fneg x), (fneg y))
2976 SDValue LHS = N0.getOperand(0);
2977 SDValue RHS = N0.getOperand(1);
2978
2979 if (LHS.getOpcode() != ISD::FNEG)
2980 LHS = DAG.getNode(ISD::FNEG, SL, VT, LHS);
2981 else
2982 LHS = LHS.getOperand(0);
2983
2984 if (RHS.getOpcode() != ISD::FNEG)
2985 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2986 else
2987 RHS = RHS.getOperand(0);
2988
Matt Arsenault7b49ad72017-01-23 19:08:34 +00002989 SDValue Res = DAG.getNode(ISD::FADD, SL, VT, LHS, RHS, N0->getFlags());
Matt Arsenault2529fba2017-01-12 00:09:34 +00002990 if (!N0.hasOneUse())
2991 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
2992 return Res;
2993 }
Matt Arsenaulta8c325e2017-01-12 18:26:30 +00002994 case ISD::FMUL:
2995 case AMDGPUISD::FMUL_LEGACY: {
Matt Arsenault4103a812017-01-12 00:23:20 +00002996 // (fneg (fmul x, y)) -> (fmul x, (fneg y))
Matt Arsenaulta8c325e2017-01-12 18:26:30 +00002997 // (fneg (fmul_legacy x, y)) -> (fmul_legacy x, (fneg y))
Matt Arsenault4103a812017-01-12 00:23:20 +00002998 SDValue LHS = N0.getOperand(0);
2999 SDValue RHS = N0.getOperand(1);
3000
3001 if (LHS.getOpcode() == ISD::FNEG)
3002 LHS = LHS.getOperand(0);
3003 else if (RHS.getOpcode() == ISD::FNEG)
3004 RHS = RHS.getOperand(0);
3005 else
3006 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
3007
Matt Arsenault7b49ad72017-01-23 19:08:34 +00003008 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, RHS, N0->getFlags());
Matt Arsenault4103a812017-01-12 00:23:20 +00003009 if (!N0.hasOneUse())
3010 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
3011 return Res;
3012 }
Matt Arsenault63f95372017-01-12 00:32:16 +00003013 case ISD::FMA:
3014 case ISD::FMAD: {
Matt Arsenault3e6f9b52017-01-19 06:35:27 +00003015 if (!mayIgnoreSignedZero(N0))
3016 return SDValue();
3017
Matt Arsenault63f95372017-01-12 00:32:16 +00003018 // (fneg (fma x, y, z)) -> (fma x, (fneg y), (fneg z))
3019 SDValue LHS = N0.getOperand(0);
3020 SDValue MHS = N0.getOperand(1);
3021 SDValue RHS = N0.getOperand(2);
3022
3023 if (LHS.getOpcode() == ISD::FNEG)
3024 LHS = LHS.getOperand(0);
3025 else if (MHS.getOpcode() == ISD::FNEG)
3026 MHS = MHS.getOperand(0);
3027 else
3028 MHS = DAG.getNode(ISD::FNEG, SL, VT, MHS);
3029
3030 if (RHS.getOpcode() != ISD::FNEG)
3031 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
3032 else
3033 RHS = RHS.getOperand(0);
3034
3035 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, MHS, RHS);
3036 if (!N0.hasOneUse())
3037 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
3038 return Res;
3039 }
Matt Arsenault2511c032017-02-03 00:23:15 +00003040 case ISD::FMAXNUM:
Matt Arsenaulte1b59532017-02-03 00:51:50 +00003041 case ISD::FMINNUM:
3042 case AMDGPUISD::FMAX_LEGACY:
3043 case AMDGPUISD::FMIN_LEGACY: {
Matt Arsenault2511c032017-02-03 00:23:15 +00003044 // fneg (fmaxnum x, y) -> fminnum (fneg x), (fneg y)
3045 // fneg (fminnum x, y) -> fmaxnum (fneg x), (fneg y)
Matt Arsenaulte1b59532017-02-03 00:51:50 +00003046 // fneg (fmax_legacy x, y) -> fmin_legacy (fneg x), (fneg y)
3047 // fneg (fmin_legacy x, y) -> fmax_legacy (fneg x), (fneg y)
3048
Matt Arsenault2511c032017-02-03 00:23:15 +00003049 SDValue LHS = N0.getOperand(0);
3050 SDValue RHS = N0.getOperand(1);
3051
3052 // 0 doesn't have a negated inline immediate.
3053 // TODO: Shouldn't fold 1/2pi either, and should be generalized to other
3054 // operations.
3055 if (isConstantFPZero(RHS))
3056 return SDValue();
3057
3058 SDValue NegLHS = DAG.getNode(ISD::FNEG, SL, VT, LHS);
3059 SDValue NegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
Matt Arsenaulte1b59532017-02-03 00:51:50 +00003060 unsigned Opposite = inverseMinMax(Opc);
Matt Arsenault2511c032017-02-03 00:23:15 +00003061
3062 SDValue Res = DAG.getNode(Opposite, SL, VT, NegLHS, NegRHS, N0->getFlags());
3063 if (!N0.hasOneUse())
3064 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
3065 return Res;
3066 }
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00003067 case ISD::FP_EXTEND:
Matt Arsenault53f0cc22017-01-26 01:25:36 +00003068 case ISD::FTRUNC:
3069 case ISD::FRINT:
3070 case ISD::FNEARBYINT: // XXX - Should fround be handled?
3071 case ISD::FSIN:
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00003072 case AMDGPUISD::RCP:
Matt Arsenault31c039e2017-01-12 18:48:09 +00003073 case AMDGPUISD::RCP_LEGACY:
Matt Arsenault31c039e2017-01-12 18:48:09 +00003074 case AMDGPUISD::SIN_HW: {
Matt Arsenault98d2bf102017-01-12 17:46:28 +00003075 SDValue CvtSrc = N0.getOperand(0);
3076 if (CvtSrc.getOpcode() == ISD::FNEG) {
3077 // (fneg (fp_extend (fneg x))) -> (fp_extend x)
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00003078 // (fneg (rcp (fneg x))) -> (rcp x)
Matt Arsenault4242d482017-01-12 17:46:33 +00003079 return DAG.getNode(Opc, SL, VT, CvtSrc.getOperand(0));
Matt Arsenault98d2bf102017-01-12 17:46:28 +00003080 }
3081
3082 if (!N0.hasOneUse())
3083 return SDValue();
3084
3085 // (fneg (fp_extend x)) -> (fp_extend (fneg x))
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00003086 // (fneg (rcp x)) -> (rcp (fneg x))
Matt Arsenault98d2bf102017-01-12 17:46:28 +00003087 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc);
Matt Arsenault7b49ad72017-01-23 19:08:34 +00003088 return DAG.getNode(Opc, SL, VT, Neg, N0->getFlags());
Matt Arsenault4242d482017-01-12 17:46:33 +00003089 }
3090 case ISD::FP_ROUND: {
3091 SDValue CvtSrc = N0.getOperand(0);
3092
3093 if (CvtSrc.getOpcode() == ISD::FNEG) {
3094 // (fneg (fp_round (fneg x))) -> (fp_round x)
3095 return DAG.getNode(ISD::FP_ROUND, SL, VT,
3096 CvtSrc.getOperand(0), N0.getOperand(1));
3097 }
3098
3099 if (!N0.hasOneUse())
3100 return SDValue();
3101
3102 // (fneg (fp_round x)) -> (fp_round (fneg x))
3103 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc);
3104 return DAG.getNode(ISD::FP_ROUND, SL, VT, Neg, N0.getOperand(1));
Matt Arsenault98d2bf102017-01-12 17:46:28 +00003105 }
Matt Arsenault9dba9bd2017-02-02 02:27:04 +00003106 case ISD::FP16_TO_FP: {
3107 // v_cvt_f32_f16 supports source modifiers on pre-VI targets without legal
3108 // f16, but legalization of f16 fneg ends up pulling it out of the source.
3109 // Put the fneg back as a legal source operation that can be matched later.
3110 SDLoc SL(N);
3111
3112 SDValue Src = N0.getOperand(0);
3113 EVT SrcVT = Src.getValueType();
3114
3115 // fneg (fp16_to_fp x) -> fp16_to_fp (xor x, 0x8000)
3116 SDValue IntFNeg = DAG.getNode(ISD::XOR, SL, SrcVT, Src,
3117 DAG.getConstant(0x8000, SL, SrcVT));
3118 return DAG.getNode(ISD::FP16_TO_FP, SL, N->getValueType(0), IntFNeg);
3119 }
3120 default:
3121 return SDValue();
3122 }
3123}
3124
3125SDValue AMDGPUTargetLowering::performFAbsCombine(SDNode *N,
3126 DAGCombinerInfo &DCI) const {
3127 SelectionDAG &DAG = DCI.DAG;
3128 SDValue N0 = N->getOperand(0);
3129
3130 if (!N0.hasOneUse())
3131 return SDValue();
3132
3133 switch (N0.getOpcode()) {
3134 case ISD::FP16_TO_FP: {
3135 assert(!Subtarget->has16BitInsts() && "should only see if f16 is illegal");
3136 SDLoc SL(N);
3137 SDValue Src = N0.getOperand(0);
3138 EVT SrcVT = Src.getValueType();
3139
3140 // fabs (fp16_to_fp x) -> fp16_to_fp (and x, 0x7fff)
3141 SDValue IntFAbs = DAG.getNode(ISD::AND, SL, SrcVT, Src,
3142 DAG.getConstant(0x7fff, SL, SrcVT));
3143 return DAG.getNode(ISD::FP16_TO_FP, SL, N->getValueType(0), IntFAbs);
3144 }
Matt Arsenault2529fba2017-01-12 00:09:34 +00003145 default:
3146 return SDValue();
3147 }
3148}
3149
Tom Stellard50122a52014-04-07 19:45:41 +00003150SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N,
Matt Arsenaultca3976f2014-07-15 02:06:31 +00003151 DAGCombinerInfo &DCI) const {
Tom Stellard50122a52014-04-07 19:45:41 +00003152 SelectionDAG &DAG = DCI.DAG;
3153 SDLoc DL(N);
3154
3155 switch(N->getOpcode()) {
Matt Arsenault24e33d12015-07-03 23:33:38 +00003156 default:
3157 break;
Matt Arsenault79003342016-04-14 21:58:07 +00003158 case ISD::BITCAST: {
3159 EVT DestVT = N->getValueType(0);
Matt Arsenaultd99ef112016-09-17 15:44:16 +00003160
3161 // Push casts through vector builds. This helps avoid emitting a large
3162 // number of copies when materializing floating point vector constants.
3163 //
3164 // vNt1 bitcast (vNt0 (build_vector t0:x, t0:y)) =>
3165 // vnt1 = build_vector (t1 (bitcast t0:x)), (t1 (bitcast t0:y))
3166 if (DestVT.isVector()) {
3167 SDValue Src = N->getOperand(0);
3168 if (Src.getOpcode() == ISD::BUILD_VECTOR) {
3169 EVT SrcVT = Src.getValueType();
3170 unsigned NElts = DestVT.getVectorNumElements();
3171
3172 if (SrcVT.getVectorNumElements() == NElts) {
3173 EVT DestEltVT = DestVT.getVectorElementType();
3174
3175 SmallVector<SDValue, 8> CastedElts;
3176 SDLoc SL(N);
3177 for (unsigned I = 0, E = SrcVT.getVectorNumElements(); I != E; ++I) {
3178 SDValue Elt = Src.getOperand(I);
3179 CastedElts.push_back(DAG.getNode(ISD::BITCAST, DL, DestEltVT, Elt));
3180 }
3181
3182 return DAG.getBuildVector(DestVT, SL, CastedElts);
3183 }
3184 }
3185 }
3186
Matt Arsenault79003342016-04-14 21:58:07 +00003187 if (DestVT.getSizeInBits() != 64 && !DestVT.isVector())
3188 break;
3189
3190 // Fold bitcasts of constants.
3191 //
3192 // v2i32 (bitcast i64:k) -> build_vector lo_32(k), hi_32(k)
3193 // TODO: Generalize and move to DAGCombiner
3194 SDValue Src = N->getOperand(0);
3195 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src)) {
3196 assert(Src.getValueType() == MVT::i64);
3197 SDLoc SL(N);
3198 uint64_t CVal = C->getZExtValue();
3199 return DAG.getNode(ISD::BUILD_VECTOR, SL, DestVT,
3200 DAG.getConstant(Lo_32(CVal), SL, MVT::i32),
3201 DAG.getConstant(Hi_32(CVal), SL, MVT::i32));
3202 }
3203
3204 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Src)) {
3205 const APInt &Val = C->getValueAPF().bitcastToAPInt();
3206 SDLoc SL(N);
3207 uint64_t CVal = Val.getZExtValue();
3208 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
3209 DAG.getConstant(Lo_32(CVal), SL, MVT::i32),
3210 DAG.getConstant(Hi_32(CVal), SL, MVT::i32));
3211
3212 return DAG.getNode(ISD::BITCAST, SL, DestVT, Vec);
3213 }
3214
3215 break;
3216 }
Matt Arsenault24692112015-07-14 18:20:33 +00003217 case ISD::SHL: {
3218 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3219 break;
3220
3221 return performShlCombine(N, DCI);
3222 }
Matt Arsenault80edab92016-01-18 21:43:36 +00003223 case ISD::SRL: {
3224 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3225 break;
3226
3227 return performSrlCombine(N, DCI);
3228 }
Matt Arsenault33e3ece2016-01-18 22:09:04 +00003229 case ISD::SRA: {
3230 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3231 break;
3232
3233 return performSraCombine(N, DCI);
3234 }
Matt Arsenault24e33d12015-07-03 23:33:38 +00003235 case ISD::MUL:
3236 return performMulCombine(N, DCI);
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003237 case ISD::MULHS:
3238 return performMulhsCombine(N, DCI);
3239 case ISD::MULHU:
3240 return performMulhuCombine(N, DCI);
Matt Arsenault24e33d12015-07-03 23:33:38 +00003241 case AMDGPUISD::MUL_I24:
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003242 case AMDGPUISD::MUL_U24:
3243 case AMDGPUISD::MULHI_I24:
3244 case AMDGPUISD::MULHI_U24: {
Tom Stellard6c7dd982016-10-21 20:25:11 +00003245 // If the first call to simplify is successfull, then N may end up being
3246 // deleted, so we shouldn't call simplifyI24 again.
3247 simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI);
Matt Arsenault24e33d12015-07-03 23:33:38 +00003248 return SDValue();
3249 }
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003250 case AMDGPUISD::MUL_LOHI_I24:
3251 case AMDGPUISD::MUL_LOHI_U24:
3252 return performMulLoHi24Combine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00003253 case ISD::SELECT:
3254 return performSelectCombine(N, DCI);
Matt Arsenault2529fba2017-01-12 00:09:34 +00003255 case ISD::FNEG:
3256 return performFNegCombine(N, DCI);
Matt Arsenault9dba9bd2017-02-02 02:27:04 +00003257 case ISD::FABS:
3258 return performFAbsCombine(N, DCI);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003259 case AMDGPUISD::BFE_I32:
3260 case AMDGPUISD::BFE_U32: {
3261 assert(!N->getValueType(0).isVector() &&
3262 "Vector handling of BFE not implemented");
3263 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
3264 if (!Width)
3265 break;
3266
3267 uint32_t WidthVal = Width->getZExtValue() & 0x1f;
3268 if (WidthVal == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003269 return DAG.getConstant(0, DL, MVT::i32);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003270
3271 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
3272 if (!Offset)
3273 break;
3274
3275 SDValue BitsFrom = N->getOperand(0);
3276 uint32_t OffsetVal = Offset->getZExtValue() & 0x1f;
3277
3278 bool Signed = N->getOpcode() == AMDGPUISD::BFE_I32;
3279
3280 if (OffsetVal == 0) {
3281 // This is already sign / zero extended, so try to fold away extra BFEs.
3282 unsigned SignBits = Signed ? (32 - WidthVal + 1) : (32 - WidthVal);
3283
3284 unsigned OpSignBits = DAG.ComputeNumSignBits(BitsFrom);
3285 if (OpSignBits >= SignBits)
3286 return BitsFrom;
Matt Arsenault05e96f42014-05-22 18:09:12 +00003287
3288 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), WidthVal);
3289 if (Signed) {
3290 // This is a sign_extend_inreg. Replace it to take advantage of existing
3291 // DAG Combines. If not eliminated, we will match back to BFE during
3292 // selection.
3293
3294 // TODO: The sext_inreg of extended types ends, although we can could
3295 // handle them in a single BFE.
3296 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, BitsFrom,
3297 DAG.getValueType(SmallVT));
3298 }
3299
3300 return DAG.getZeroExtendInReg(BitsFrom, DL, SmallVT);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003301 }
3302
Matt Arsenaultf1794202014-10-15 05:07:00 +00003303 if (ConstantSDNode *CVal = dyn_cast<ConstantSDNode>(BitsFrom)) {
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003304 if (Signed) {
3305 return constantFoldBFE<int32_t>(DAG,
Matt Arsenault46cbc432014-09-19 00:42:06 +00003306 CVal->getSExtValue(),
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003307 OffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003308 WidthVal,
3309 DL);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003310 }
3311
3312 return constantFoldBFE<uint32_t>(DAG,
Matt Arsenault6462f942014-09-18 15:52:26 +00003313 CVal->getZExtValue(),
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003314 OffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003315 WidthVal,
3316 DL);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003317 }
3318
Matt Arsenault05e96f42014-05-22 18:09:12 +00003319 if ((OffsetVal + WidthVal) >= 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003320 SDValue ShiftVal = DAG.getConstant(OffsetVal, DL, MVT::i32);
Matt Arsenault05e96f42014-05-22 18:09:12 +00003321 return DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, MVT::i32,
3322 BitsFrom, ShiftVal);
3323 }
3324
Matt Arsenault7b68fdf2014-10-15 17:58:34 +00003325 if (BitsFrom.hasOneUse()) {
Matt Arsenault6de7af42014-10-15 23:37:42 +00003326 APInt Demanded = APInt::getBitsSet(32,
3327 OffsetVal,
3328 OffsetVal + WidthVal);
3329
Matt Arsenault7b68fdf2014-10-15 17:58:34 +00003330 APInt KnownZero, KnownOne;
3331 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
3332 !DCI.isBeforeLegalizeOps());
3333 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3334 if (TLO.ShrinkDemandedConstant(BitsFrom, Demanded) ||
3335 TLI.SimplifyDemandedBits(BitsFrom, Demanded,
3336 KnownZero, KnownOne, TLO)) {
3337 DCI.CommitTargetLoweringOpt(TLO);
3338 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003339 }
3340
3341 break;
3342 }
Matt Arsenault327bb5a2016-07-01 22:47:50 +00003343 case ISD::LOAD:
3344 return performLoadCombine(N, DCI);
Matt Arsenaultca3976f2014-07-15 02:06:31 +00003345 case ISD::STORE:
3346 return performStoreCombine(N, DCI);
Matt Arsenault2fdf2a12017-02-21 23:35:48 +00003347 case AMDGPUISD::CLAMP:
3348 return performClampCombine(N, DCI);
Tom Stellard50122a52014-04-07 19:45:41 +00003349 }
3350 return SDValue();
3351}
3352
3353//===----------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +00003354// Helper functions
3355//===----------------------------------------------------------------------===//
3356
Tom Stellard75aadc22012-12-11 21:25:42 +00003357SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
3358 const TargetRegisterClass *RC,
3359 unsigned Reg, EVT VT) const {
3360 MachineFunction &MF = DAG.getMachineFunction();
3361 MachineRegisterInfo &MRI = MF.getRegInfo();
3362 unsigned VirtualRegister;
3363 if (!MRI.isLiveIn(Reg)) {
3364 VirtualRegister = MRI.createVirtualRegister(RC);
3365 MRI.addLiveIn(Reg, VirtualRegister);
3366 } else {
3367 VirtualRegister = MRI.getLiveInVirtReg(Reg);
3368 }
3369 return DAG.getRegister(VirtualRegister, VT);
3370}
3371
Tom Stellarddcb9f092015-07-09 21:20:37 +00003372uint32_t AMDGPUTargetLowering::getImplicitParameterOffset(
3373 const AMDGPUMachineFunction *MFI, const ImplicitParameter Param) const {
Tom Stellardb2869eb2016-09-09 19:28:00 +00003374 unsigned Alignment = Subtarget->getAlignmentForImplicitArgPtr();
3375 uint64_t ArgOffset = alignTo(MFI->getABIArgOffset(), Alignment);
Tom Stellarddcb9f092015-07-09 21:20:37 +00003376 switch (Param) {
3377 case GRID_DIM:
3378 return ArgOffset;
3379 case GRID_OFFSET:
3380 return ArgOffset + 4;
3381 }
3382 llvm_unreachable("unexpected implicit parameter type");
3383}
3384
Tom Stellard75aadc22012-12-11 21:25:42 +00003385#define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
3386
3387const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00003388 switch ((AMDGPUISD::NodeType)Opcode) {
3389 case AMDGPUISD::FIRST_NUMBER: break;
Tom Stellard75aadc22012-12-11 21:25:42 +00003390 // AMDIL DAG nodes
Tom Stellard75aadc22012-12-11 21:25:42 +00003391 NODE_NAME_CASE(CALL);
3392 NODE_NAME_CASE(UMUL);
Tom Stellard75aadc22012-12-11 21:25:42 +00003393 NODE_NAME_CASE(BRANCH_COND);
3394
3395 // AMDGPU DAG nodes
Matt Arsenault9babdf42016-06-22 20:15:28 +00003396 NODE_NAME_CASE(ENDPGM)
3397 NODE_NAME_CASE(RETURN)
Tom Stellard75aadc22012-12-11 21:25:42 +00003398 NODE_NAME_CASE(DWORDADDR)
3399 NODE_NAME_CASE(FRACT)
Wei Ding07e03712016-07-28 16:42:13 +00003400 NODE_NAME_CASE(SETCC)
Tom Stellard8485fa02016-12-07 02:42:15 +00003401 NODE_NAME_CASE(SETREG)
3402 NODE_NAME_CASE(FMA_W_CHAIN)
3403 NODE_NAME_CASE(FMUL_W_CHAIN)
Matt Arsenault5d47d4a2014-06-12 21:15:44 +00003404 NODE_NAME_CASE(CLAMP)
Matthias Braund04893f2015-05-07 21:33:59 +00003405 NODE_NAME_CASE(COS_HW)
3406 NODE_NAME_CASE(SIN_HW)
Matt Arsenaultda59f3d2014-11-13 23:03:09 +00003407 NODE_NAME_CASE(FMAX_LEGACY)
Matt Arsenaultda59f3d2014-11-13 23:03:09 +00003408 NODE_NAME_CASE(FMIN_LEGACY)
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003409 NODE_NAME_CASE(FMAX3)
3410 NODE_NAME_CASE(SMAX3)
3411 NODE_NAME_CASE(UMAX3)
3412 NODE_NAME_CASE(FMIN3)
3413 NODE_NAME_CASE(SMIN3)
3414 NODE_NAME_CASE(UMIN3)
Matt Arsenaultf639c322016-01-28 20:53:42 +00003415 NODE_NAME_CASE(FMED3)
3416 NODE_NAME_CASE(SMED3)
3417 NODE_NAME_CASE(UMED3)
Matt Arsenaulta0050b02014-06-19 01:19:19 +00003418 NODE_NAME_CASE(URECIP)
3419 NODE_NAME_CASE(DIV_SCALE)
3420 NODE_NAME_CASE(DIV_FMAS)
3421 NODE_NAME_CASE(DIV_FIXUP)
Wei Ding4d3d4ca2017-02-24 23:00:29 +00003422 NODE_NAME_CASE(FMAD_FTZ)
Matt Arsenaulta0050b02014-06-19 01:19:19 +00003423 NODE_NAME_CASE(TRIG_PREOP)
3424 NODE_NAME_CASE(RCP)
3425 NODE_NAME_CASE(RSQ)
Matt Arsenault32fc5272016-07-26 16:45:45 +00003426 NODE_NAME_CASE(RCP_LEGACY)
Matt Arsenault257d48d2014-06-24 22:13:39 +00003427 NODE_NAME_CASE(RSQ_LEGACY)
Matt Arsenault32fc5272016-07-26 16:45:45 +00003428 NODE_NAME_CASE(FMUL_LEGACY)
Matt Arsenault79963e82016-02-13 01:03:00 +00003429 NODE_NAME_CASE(RSQ_CLAMP)
Matt Arsenault2e7cc482014-08-15 17:30:25 +00003430 NODE_NAME_CASE(LDEXP)
Matt Arsenault4831ce52015-01-06 23:00:37 +00003431 NODE_NAME_CASE(FP_CLASS)
Matt Arsenaulta0050b02014-06-19 01:19:19 +00003432 NODE_NAME_CASE(DOT4)
Matthias Braund04893f2015-05-07 21:33:59 +00003433 NODE_NAME_CASE(CARRY)
3434 NODE_NAME_CASE(BORROW)
Matt Arsenaultfae02982014-03-17 18:58:11 +00003435 NODE_NAME_CASE(BFE_U32)
3436 NODE_NAME_CASE(BFE_I32)
Matt Arsenaultb3458362014-03-31 18:21:13 +00003437 NODE_NAME_CASE(BFI)
3438 NODE_NAME_CASE(BFM)
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00003439 NODE_NAME_CASE(FFBH_U32)
Matt Arsenaultb51dcb92016-07-18 18:40:51 +00003440 NODE_NAME_CASE(FFBH_I32)
Tom Stellard50122a52014-04-07 19:45:41 +00003441 NODE_NAME_CASE(MUL_U24)
3442 NODE_NAME_CASE(MUL_I24)
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003443 NODE_NAME_CASE(MULHI_U24)
3444 NODE_NAME_CASE(MULHI_I24)
3445 NODE_NAME_CASE(MUL_LOHI_U24)
3446 NODE_NAME_CASE(MUL_LOHI_I24)
Matt Arsenaulteb260202014-05-22 18:00:15 +00003447 NODE_NAME_CASE(MAD_U24)
3448 NODE_NAME_CASE(MAD_I24)
Matthias Braund04893f2015-05-07 21:33:59 +00003449 NODE_NAME_CASE(TEXTURE_FETCH)
Tom Stellard75aadc22012-12-11 21:25:42 +00003450 NODE_NAME_CASE(EXPORT)
Matt Arsenault7bee6ac2016-12-05 20:23:10 +00003451 NODE_NAME_CASE(EXPORT_DONE)
3452 NODE_NAME_CASE(R600_EXPORT)
Tom Stellardff62c352013-01-23 02:09:03 +00003453 NODE_NAME_CASE(CONST_ADDRESS)
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00003454 NODE_NAME_CASE(REGISTER_LOAD)
3455 NODE_NAME_CASE(REGISTER_STORE)
Tom Stellard9fa17912013-08-14 23:24:45 +00003456 NODE_NAME_CASE(LOAD_INPUT)
3457 NODE_NAME_CASE(SAMPLE)
3458 NODE_NAME_CASE(SAMPLEB)
3459 NODE_NAME_CASE(SAMPLED)
3460 NODE_NAME_CASE(SAMPLEL)
Matt Arsenault364a6742014-06-11 17:50:44 +00003461 NODE_NAME_CASE(CVT_F32_UBYTE0)
3462 NODE_NAME_CASE(CVT_F32_UBYTE1)
3463 NODE_NAME_CASE(CVT_F32_UBYTE2)
3464 NODE_NAME_CASE(CVT_F32_UBYTE3)
Matt Arsenault1f17c662017-02-22 00:27:34 +00003465 NODE_NAME_CASE(CVT_PKRTZ_F16_F32)
Tom Stellard880a80a2014-06-17 16:53:14 +00003466 NODE_NAME_CASE(BUILD_VERTICAL_VECTOR)
Tom Stellard067c8152014-07-21 14:01:14 +00003467 NODE_NAME_CASE(CONST_DATA_PTR)
Tom Stellardbf3e6e52016-06-14 20:29:59 +00003468 NODE_NAME_CASE(PC_ADD_REL_OFFSET)
Matt Arsenault03006fd2016-07-19 16:27:56 +00003469 NODE_NAME_CASE(KILL)
Jan Veselyf1705042017-01-20 21:24:26 +00003470 NODE_NAME_CASE(DUMMY_CHAIN)
Matthias Braund04893f2015-05-07 21:33:59 +00003471 case AMDGPUISD::FIRST_MEM_OPCODE_NUMBER: break;
Tom Stellardfc92e772015-05-12 14:18:14 +00003472 NODE_NAME_CASE(SENDMSG)
Jan Veselyd48445d2017-01-04 18:06:55 +00003473 NODE_NAME_CASE(SENDMSGHALT)
Tom Stellard2a9d9472015-05-12 15:00:46 +00003474 NODE_NAME_CASE(INTERP_MOV)
3475 NODE_NAME_CASE(INTERP_P1)
3476 NODE_NAME_CASE(INTERP_P2)
Tom Stellardd3ee8c12013-08-16 01:12:06 +00003477 NODE_NAME_CASE(STORE_MSKOR)
Matt Arsenaultdfaf4262016-04-25 19:27:09 +00003478 NODE_NAME_CASE(LOAD_CONSTANT)
Tom Stellardafcf12f2013-09-12 02:55:14 +00003479 NODE_NAME_CASE(TBUFFER_STORE_FORMAT)
Tom Stellard354a43c2016-04-01 18:27:37 +00003480 NODE_NAME_CASE(ATOMIC_CMP_SWAP)
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00003481 NODE_NAME_CASE(ATOMIC_INC)
3482 NODE_NAME_CASE(ATOMIC_DEC)
Tom Stellard6f9ef142016-12-20 17:19:44 +00003483 NODE_NAME_CASE(BUFFER_LOAD)
3484 NODE_NAME_CASE(BUFFER_LOAD_FORMAT)
Matthias Braund04893f2015-05-07 21:33:59 +00003485 case AMDGPUISD::LAST_AMDGPU_ISD_NUMBER: break;
Tom Stellard75aadc22012-12-11 21:25:42 +00003486 }
Matthias Braund04893f2015-05-07 21:33:59 +00003487 return nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +00003488}
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003489
Evandro Menezes21f9ce12016-11-10 23:31:06 +00003490SDValue AMDGPUTargetLowering::getSqrtEstimate(SDValue Operand,
3491 SelectionDAG &DAG, int Enabled,
3492 int &RefinementSteps,
3493 bool &UseOneConstNR,
3494 bool Reciprocal) const {
Matt Arsenaulte93d06a2015-01-13 20:53:18 +00003495 EVT VT = Operand.getValueType();
3496
3497 if (VT == MVT::f32) {
3498 RefinementSteps = 0;
3499 return DAG.getNode(AMDGPUISD::RSQ, SDLoc(Operand), VT, Operand);
3500 }
3501
3502 // TODO: There is also f64 rsq instruction, but the documentation is less
3503 // clear on its precision.
3504
3505 return SDValue();
3506}
3507
Matt Arsenaultbf0db912015-01-13 20:53:23 +00003508SDValue AMDGPUTargetLowering::getRecipEstimate(SDValue Operand,
Sanjay Patel0051efc2016-10-20 16:55:45 +00003509 SelectionDAG &DAG, int Enabled,
3510 int &RefinementSteps) const {
Matt Arsenaultbf0db912015-01-13 20:53:23 +00003511 EVT VT = Operand.getValueType();
3512
3513 if (VT == MVT::f32) {
3514 // Reciprocal, < 1 ulp error.
3515 //
3516 // This reciprocal approximation converges to < 0.5 ulp error with one
3517 // newton rhapson performed with two fused multiple adds (FMAs).
3518
3519 RefinementSteps = 0;
3520 return DAG.getNode(AMDGPUISD::RCP, SDLoc(Operand), VT, Operand);
3521 }
3522
3523 // TODO: There is also f64 rcp instruction, but the documentation is less
3524 // clear on its precision.
3525
3526 return SDValue();
3527}
3528
Jay Foada0653a32014-05-14 21:14:37 +00003529void AMDGPUTargetLowering::computeKnownBitsForTargetNode(
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003530 const SDValue Op,
3531 APInt &KnownZero,
3532 APInt &KnownOne,
3533 const SelectionDAG &DAG,
3534 unsigned Depth) const {
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003535
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003536 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0); // Don't know anything.
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003537
3538 APInt KnownZero2;
3539 APInt KnownOne2;
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003540 unsigned Opc = Op.getOpcode();
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003541
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003542 switch (Opc) {
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003543 default:
3544 break;
Jan Vesely808fff52015-04-30 17:15:56 +00003545 case AMDGPUISD::CARRY:
3546 case AMDGPUISD::BORROW: {
3547 KnownZero = APInt::getHighBitsSet(32, 31);
3548 break;
3549 }
3550
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003551 case AMDGPUISD::BFE_I32:
3552 case AMDGPUISD::BFE_U32: {
3553 ConstantSDNode *CWidth = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3554 if (!CWidth)
3555 return;
3556
3557 unsigned BitWidth = 32;
3558 uint32_t Width = CWidth->getZExtValue() & 0x1f;
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003559
Matt Arsenaulta3fe7c62014-10-16 20:07:40 +00003560 if (Opc == AMDGPUISD::BFE_U32)
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003561 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - Width);
3562
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003563 break;
3564 }
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003565 }
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003566}
Matt Arsenaultbf8694d2014-05-22 18:09:03 +00003567
3568unsigned AMDGPUTargetLowering::ComputeNumSignBitsForTargetNode(
3569 SDValue Op,
3570 const SelectionDAG &DAG,
3571 unsigned Depth) const {
3572 switch (Op.getOpcode()) {
3573 case AMDGPUISD::BFE_I32: {
3574 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3575 if (!Width)
3576 return 1;
3577
3578 unsigned SignBits = 32 - Width->getZExtValue() + 1;
Artyom Skrobov314ee042015-11-25 19:41:11 +00003579 if (!isNullConstant(Op.getOperand(1)))
Matt Arsenaultbf8694d2014-05-22 18:09:03 +00003580 return SignBits;
3581
3582 // TODO: Could probably figure something out with non-0 offsets.
3583 unsigned Op0SignBits = DAG.ComputeNumSignBits(Op.getOperand(0), Depth + 1);
3584 return std::max(SignBits, Op0SignBits);
3585 }
3586
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003587 case AMDGPUISD::BFE_U32: {
3588 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3589 return Width ? 32 - (Width->getZExtValue() & 0x1f) : 1;
3590 }
3591
Jan Vesely808fff52015-04-30 17:15:56 +00003592 case AMDGPUISD::CARRY:
3593 case AMDGPUISD::BORROW:
3594 return 31;
3595
Matt Arsenaultbf8694d2014-05-22 18:09:03 +00003596 default:
3597 return 1;
3598 }
3599}