blob: edaab0063daaf487c3aef93a1eb36e237283915d [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 Arsenaulteb522e62017-02-27 22:15:25 +0000647
648 // Packed operations do not have a fabs modifier.
649 return VT == MVT::f32 || VT == MVT::f64 ||
650 (Subtarget->has16BitInsts() && VT == MVT::f16);
Tom Stellardc54731a2013-07-23 23:55:03 +0000651}
652
653bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000654 assert(VT.isFloatingPoint());
655 return VT == MVT::f32 || VT == MVT::f64 ||
656 (Subtarget->has16BitInsts() && VT == MVT::f16) ||
657 (Subtarget->hasVOP3PInsts() && VT == MVT::v2f16);
Tom Stellardc54731a2013-07-23 23:55:03 +0000658}
659
Matt Arsenault65ad1602015-05-24 00:51:27 +0000660bool AMDGPUTargetLowering:: storeOfVectorConstantIsCheap(EVT MemVT,
661 unsigned NumElem,
662 unsigned AS) const {
663 return true;
664}
665
Matt Arsenault61dc2352015-10-12 23:59:50 +0000666bool AMDGPUTargetLowering::aggressivelyPreferBuildVectorSources(EVT VecVT) const {
667 // There are few operations which truly have vector input operands. Any vector
668 // operation is going to involve operations on each component, and a
669 // build_vector will be a copy per element, so it always makes sense to use a
670 // build_vector input in place of the extracted element to avoid a copy into a
671 // super register.
672 //
673 // We should probably only do this if all users are extracts only, but this
674 // should be the common case.
675 return true;
676}
677
Benjamin Kramer53f9df42014-02-12 10:17:54 +0000678bool AMDGPUTargetLowering::isTruncateFree(EVT Source, EVT Dest) const {
Matt Arsenault0cdcd962014-02-10 19:57:42 +0000679 // Truncate is just accessing a subregister.
Tom Stellard115a6152016-11-10 16:02:37 +0000680
681 unsigned SrcSize = Source.getSizeInBits();
682 unsigned DestSize = Dest.getSizeInBits();
683
684 return DestSize < SrcSize && DestSize % 32 == 0 ;
Benjamin Kramer53f9df42014-02-12 10:17:54 +0000685}
686
687bool AMDGPUTargetLowering::isTruncateFree(Type *Source, Type *Dest) const {
688 // Truncate is just accessing a subregister.
Tom Stellard115a6152016-11-10 16:02:37 +0000689
690 unsigned SrcSize = Source->getScalarSizeInBits();
691 unsigned DestSize = Dest->getScalarSizeInBits();
692
693 if (DestSize== 16 && Subtarget->has16BitInsts())
694 return SrcSize >= 32;
695
696 return DestSize < SrcSize && DestSize % 32 == 0;
Matt Arsenault0cdcd962014-02-10 19:57:42 +0000697}
698
Matt Arsenaultb517c812014-03-27 17:23:31 +0000699bool AMDGPUTargetLowering::isZExtFree(Type *Src, Type *Dest) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000700 unsigned SrcSize = Src->getScalarSizeInBits();
701 unsigned DestSize = Dest->getScalarSizeInBits();
Matt Arsenaultb517c812014-03-27 17:23:31 +0000702
Tom Stellard115a6152016-11-10 16:02:37 +0000703 if (SrcSize == 16 && Subtarget->has16BitInsts())
704 return DestSize >= 32;
705
Matt Arsenaultb517c812014-03-27 17:23:31 +0000706 return SrcSize == 32 && DestSize == 64;
707}
708
709bool AMDGPUTargetLowering::isZExtFree(EVT Src, EVT Dest) const {
710 // Any register load of a 64-bit value really requires 2 32-bit moves. For all
711 // practical purposes, the extra mov 0 to load a 64-bit is free. As used,
712 // this will enable reducing 64-bit operations the 32-bit, which is always
713 // good.
Tom Stellard115a6152016-11-10 16:02:37 +0000714
715 if (Src == MVT::i16)
716 return Dest == MVT::i32 ||Dest == MVT::i64 ;
717
Matt Arsenaultb517c812014-03-27 17:23:31 +0000718 return Src == MVT::i32 && Dest == MVT::i64;
719}
720
Aaron Ballman3c81e462014-06-26 13:45:47 +0000721bool AMDGPUTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
722 return isZExtFree(Val.getValueType(), VT2);
723}
724
Matt Arsenaulta7f1e0c2014-03-24 19:43:31 +0000725bool AMDGPUTargetLowering::isNarrowingProfitable(EVT SrcVT, EVT DestVT) const {
726 // There aren't really 64-bit registers, but pairs of 32-bit ones and only a
727 // limited number of native 64-bit operations. Shrinking an operation to fit
728 // in a single 32-bit register should always be helpful. As currently used,
729 // this is much less general than the name suggests, and is only used in
730 // places trying to reduce the sizes of loads. Shrinking loads to < 32-bits is
731 // not profitable, and may actually be harmful.
732 return SrcVT.getSizeInBits() > 32 && DestVT.getSizeInBits() == 32;
733}
734
Tom Stellardc54731a2013-07-23 23:55:03 +0000735//===---------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +0000736// TargetLowering Callbacks
737//===---------------------------------------------------------------------===//
738
Tom Stellardca166212017-01-30 21:56:46 +0000739CCAssignFn *AMDGPUCallLowering::CCAssignFnForCall(CallingConv::ID CC,
740 bool IsVarArg) const {
741 return CC_AMDGPU;
742}
743
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000744/// The SelectionDAGBuilder will automatically promote function arguments
745/// with illegal types. However, this does not work for the AMDGPU targets
746/// since the function arguments are stored in memory as these illegal types.
747/// In order to handle this properly we need to get the original types sizes
748/// from the LLVM IR Function and fixup the ISD:InputArg values before
749/// passing them to AnalyzeFormalArguments()
Christian Konig2c8f6d52013-03-07 09:03:52 +0000750
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000751/// When the SelectionDAGBuilder computes the Ins, it takes care of splitting
752/// input values across multiple registers. Each item in the Ins array
753/// represents a single value that will be stored in regsters. Ins[x].VT is
754/// the value type of the value that will be stored in the register, so
755/// whatever SDNode we lower the argument to needs to be this type.
756///
757/// In order to correctly lower the arguments we need to know the size of each
758/// argument. Since Ins[x].VT gives us the size of the register that will
759/// hold the value, we need to look at Ins[x].ArgVT to see the 'real' type
760/// for the orignal function argument so that we can deduce the correct memory
761/// type to use for Ins[x]. In most cases the correct memory type will be
762/// Ins[x].ArgVT. However, this will not always be the case. If, for example,
763/// we have a kernel argument of type v8i8, this argument will be split into
764/// 8 parts and each part will be represented by its own item in the Ins array.
765/// For each part the Ins[x].ArgVT will be the v8i8, which is the full type of
766/// the argument before it was split. From this, we deduce that the memory type
767/// for each individual part is i8. We pass the memory type as LocVT to the
768/// calling convention analysis function and the register type (Ins[x].VT) as
769/// the ValVT.
770void AMDGPUTargetLowering::analyzeFormalArgumentsCompute(CCState &State,
771 const SmallVectorImpl<ISD::InputArg> &Ins) const {
772 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
773 const ISD::InputArg &In = Ins[i];
774 EVT MemVT;
775
776 unsigned NumRegs = getNumRegisters(State.getContext(), In.ArgVT);
777
Tom Stellard7998db62016-09-16 22:20:24 +0000778 if (!Subtarget->isAmdHsaOS() &&
779 (In.ArgVT == MVT::i16 || In.ArgVT == MVT::i8 || In.ArgVT == MVT::f16)) {
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000780 // The ABI says the caller will extend these values to 32-bits.
781 MemVT = In.ArgVT.isInteger() ? MVT::i32 : MVT::f32;
782 } else if (NumRegs == 1) {
783 // This argument is not split, so the IR type is the memory type.
784 assert(!In.Flags.isSplit());
785 if (In.ArgVT.isExtended()) {
786 // We have an extended type, like i24, so we should just use the register type
787 MemVT = In.VT;
788 } else {
789 MemVT = In.ArgVT;
790 }
791 } else if (In.ArgVT.isVector() && In.VT.isVector() &&
792 In.ArgVT.getScalarType() == In.VT.getScalarType()) {
793 assert(In.ArgVT.getVectorNumElements() > In.VT.getVectorNumElements());
794 // We have a vector value which has been split into a vector with
795 // the same scalar type, but fewer elements. This should handle
796 // all the floating-point vector types.
797 MemVT = In.VT;
798 } else if (In.ArgVT.isVector() &&
799 In.ArgVT.getVectorNumElements() == NumRegs) {
800 // This arg has been split so that each element is stored in a separate
801 // register.
802 MemVT = In.ArgVT.getScalarType();
803 } else if (In.ArgVT.isExtended()) {
804 // We have an extended type, like i65.
805 MemVT = In.VT;
806 } else {
807 unsigned MemoryBits = In.ArgVT.getStoreSizeInBits() / NumRegs;
808 assert(In.ArgVT.getStoreSizeInBits() % NumRegs == 0);
809 if (In.VT.isInteger()) {
810 MemVT = EVT::getIntegerVT(State.getContext(), MemoryBits);
811 } else if (In.VT.isVector()) {
812 assert(!In.VT.getScalarType().isFloatingPoint());
813 unsigned NumElements = In.VT.getVectorNumElements();
814 assert(MemoryBits % NumElements == 0);
815 // This vector type has been split into another vector type with
816 // a different elements size.
817 EVT ScalarVT = EVT::getIntegerVT(State.getContext(),
818 MemoryBits / NumElements);
819 MemVT = EVT::getVectorVT(State.getContext(), ScalarVT, NumElements);
820 } else {
821 llvm_unreachable("cannot deduce memory type.");
822 }
823 }
824
825 // Convert one element vectors to scalar.
826 if (MemVT.isVector() && MemVT.getVectorNumElements() == 1)
827 MemVT = MemVT.getScalarType();
828
829 if (MemVT.isExtended()) {
830 // This should really only happen if we have vec3 arguments
831 assert(MemVT.isVector() && MemVT.getVectorNumElements() == 3);
832 MemVT = MemVT.getPow2VectorType(State.getContext());
833 }
834
835 assert(MemVT.isSimple());
836 allocateKernArg(i, In.VT, MemVT.getSimpleVT(), CCValAssign::Full, In.Flags,
837 State);
838 }
839}
840
841void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
842 const SmallVectorImpl<ISD::InputArg> &Ins) const {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000843 State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
Tom Stellard75aadc22012-12-11 21:25:42 +0000844}
845
Marek Olsak8a0f3352016-01-13 17:23:04 +0000846void AMDGPUTargetLowering::AnalyzeReturn(CCState &State,
847 const SmallVectorImpl<ISD::OutputArg> &Outs) const {
848
849 State.AnalyzeReturn(Outs, RetCC_SI);
850}
851
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000852SDValue
853AMDGPUTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
854 bool isVarArg,
855 const SmallVectorImpl<ISD::OutputArg> &Outs,
856 const SmallVectorImpl<SDValue> &OutVals,
857 const SDLoc &DL, SelectionDAG &DAG) const {
Matt Arsenault9babdf42016-06-22 20:15:28 +0000858 return DAG.getNode(AMDGPUISD::ENDPGM, DL, MVT::Other, Chain);
Tom Stellard75aadc22012-12-11 21:25:42 +0000859}
860
861//===---------------------------------------------------------------------===//
862// Target specific lowering
863//===---------------------------------------------------------------------===//
864
Matt Arsenault16353872014-04-22 16:42:00 +0000865SDValue AMDGPUTargetLowering::LowerCall(CallLoweringInfo &CLI,
866 SmallVectorImpl<SDValue> &InVals) const {
867 SDValue Callee = CLI.Callee;
868 SelectionDAG &DAG = CLI.DAG;
869
870 const Function &Fn = *DAG.getMachineFunction().getFunction();
871
872 StringRef FuncName("<unknown>");
873
Matt Arsenaultde1c34102014-04-25 22:22:01 +0000874 if (const ExternalSymbolSDNode *G = dyn_cast<ExternalSymbolSDNode>(Callee))
875 FuncName = G->getSymbol();
876 else if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Matt Arsenault16353872014-04-22 16:42:00 +0000877 FuncName = G->getGlobal()->getName();
878
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000879 DiagnosticInfoUnsupported NoCalls(
880 Fn, "unsupported call to function " + FuncName, CLI.DL.getDebugLoc());
Matt Arsenault16353872014-04-22 16:42:00 +0000881 DAG.getContext()->diagnose(NoCalls);
Matt Arsenault9430b912016-05-18 16:10:11 +0000882
Matt Arsenault0b386362016-12-15 20:50:12 +0000883 if (!CLI.IsTailCall) {
884 for (unsigned I = 0, E = CLI.Ins.size(); I != E; ++I)
885 InVals.push_back(DAG.getUNDEF(CLI.Ins[I].VT));
886 }
Matt Arsenault9430b912016-05-18 16:10:11 +0000887
888 return DAG.getEntryNode();
Matt Arsenault16353872014-04-22 16:42:00 +0000889}
890
Matt Arsenault19c54882015-08-26 18:37:13 +0000891SDValue AMDGPUTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
892 SelectionDAG &DAG) const {
893 const Function &Fn = *DAG.getMachineFunction().getFunction();
894
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000895 DiagnosticInfoUnsupported NoDynamicAlloca(Fn, "unsupported dynamic alloca",
896 SDLoc(Op).getDebugLoc());
Matt Arsenault19c54882015-08-26 18:37:13 +0000897 DAG.getContext()->diagnose(NoDynamicAlloca);
Diana Picuse440f992016-06-23 09:19:16 +0000898 auto Ops = {DAG.getConstant(0, SDLoc(), Op.getValueType()), Op.getOperand(0)};
899 return DAG.getMergeValues(Ops, SDLoc());
Matt Arsenault19c54882015-08-26 18:37:13 +0000900}
901
Matt Arsenault14d46452014-06-15 20:23:38 +0000902SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op,
903 SelectionDAG &DAG) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000904 switch (Op.getOpcode()) {
905 default:
Matthias Braun8c209aa2017-01-28 02:02:38 +0000906 Op->print(errs(), &DAG);
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +0000907 llvm_unreachable("Custom lowering code for this"
908 "instruction is not implemented yet!");
Tom Stellard75aadc22012-12-11 21:25:42 +0000909 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000910 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
Tom Stellardd86003e2013-08-14 23:25:00 +0000911 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
912 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000913 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
914 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
Jan Vesely109efdf2014-06-22 21:43:00 +0000915 case ISD::SDIVREM: return LowerSDIVREM(Op, DAG);
Matt Arsenault16e31332014-09-10 21:44:27 +0000916 case ISD::FREM: return LowerFREM(Op, DAG);
Matt Arsenault46010932014-06-18 17:05:30 +0000917 case ISD::FCEIL: return LowerFCEIL(Op, DAG);
918 case ISD::FTRUNC: return LowerFTRUNC(Op, DAG);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +0000919 case ISD::FRINT: return LowerFRINT(Op, DAG);
Matt Arsenault692bd5e2014-06-18 22:03:45 +0000920 case ISD::FNEARBYINT: return LowerFNEARBYINT(Op, DAG);
Matt Arsenaultb0055482015-01-21 18:18:25 +0000921 case ISD::FROUND: return LowerFROUND(Op, DAG);
Matt Arsenault46010932014-06-18 17:05:30 +0000922 case ISD::FFLOOR: return LowerFFLOOR(Op, DAG);
Matt Arsenaultf7c95e32014-10-03 23:54:41 +0000923 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Tom Stellardc947d8c2013-10-30 17:22:05 +0000924 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Tom Stellard94c21bc2016-11-01 16:31:48 +0000925 case ISD::FP_TO_FP16: return LowerFP_TO_FP16(Op, DAG);
Matt Arsenaultc9961752014-10-03 23:54:56 +0000926 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
927 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Matt Arsenaultf058d672016-01-11 16:50:29 +0000928 case ISD::CTLZ:
929 case ISD::CTLZ_ZERO_UNDEF:
930 return LowerCTLZ(Op, DAG);
Matt Arsenault19c54882015-08-26 18:37:13 +0000931 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000932 }
933 return Op;
934}
935
Matt Arsenaultd125d742014-03-27 17:23:24 +0000936void AMDGPUTargetLowering::ReplaceNodeResults(SDNode *N,
937 SmallVectorImpl<SDValue> &Results,
938 SelectionDAG &DAG) const {
939 switch (N->getOpcode()) {
940 case ISD::SIGN_EXTEND_INREG:
941 // Different parts of legalization seem to interpret which type of
942 // sign_extend_inreg is the one to check for custom lowering. The extended
943 // from type is what really matters, but some places check for custom
944 // lowering of the result type. This results in trying to use
945 // ReplaceNodeResults to sext_in_reg to an illegal type, so we'll just do
946 // nothing here and let the illegal result integer be handled normally.
947 return;
Matt Arsenaultd125d742014-03-27 17:23:24 +0000948 default:
949 return;
950 }
951}
952
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000953static bool hasDefinedInitializer(const GlobalValue *GV) {
954 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
955 if (!GVar || !GVar->hasInitializer())
956 return false;
957
Matt Arsenault8226fc42016-03-02 23:00:21 +0000958 return !isa<UndefValue>(GVar->getInitializer());
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000959}
960
Tom Stellardc026e8b2013-06-28 15:47:08 +0000961SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
962 SDValue Op,
963 SelectionDAG &DAG) const {
964
Mehdi Amini44ede332015-07-09 02:09:04 +0000965 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardc026e8b2013-06-28 15:47:08 +0000966 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
Tom Stellardc026e8b2013-06-28 15:47:08 +0000967 const GlobalValue *GV = G->getGlobal();
Tom Stellardc026e8b2013-06-28 15:47:08 +0000968
Tom Stellard04c0e982014-01-22 19:24:21 +0000969 switch (G->getAddressSpace()) {
Tom Stellard04c0e982014-01-22 19:24:21 +0000970 case AMDGPUAS::LOCAL_ADDRESS: {
971 // XXX: What does the value of G->getOffset() mean?
972 assert(G->getOffset() == 0 &&
973 "Do not know what to do with an non-zero offset");
Tom Stellardc026e8b2013-06-28 15:47:08 +0000974
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000975 // TODO: We could emit code to handle the initialization somewhere.
976 if (hasDefinedInitializer(GV))
977 break;
978
Matt Arsenault52ef4012016-07-26 16:45:58 +0000979 unsigned Offset = MFI->allocateLDSGlobal(DL, *GV);
980 return DAG.getConstant(Offset, SDLoc(Op), Op.getValueType());
Tom Stellard04c0e982014-01-22 19:24:21 +0000981 }
Tom Stellard04c0e982014-01-22 19:24:21 +0000982 }
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000983
984 const Function &Fn = *DAG.getMachineFunction().getFunction();
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000985 DiagnosticInfoUnsupported BadInit(
986 Fn, "unsupported initializer for address space", SDLoc(Op).getDebugLoc());
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000987 DAG.getContext()->diagnose(BadInit);
988 return SDValue();
Tom Stellardc026e8b2013-06-28 15:47:08 +0000989}
990
Tom Stellardd86003e2013-08-14 23:25:00 +0000991SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
992 SelectionDAG &DAG) const {
993 SmallVector<SDValue, 8> Args;
Tom Stellardd86003e2013-08-14 23:25:00 +0000994
Tom Stellardff5cf0e2015-04-23 22:59:24 +0000995 for (const SDUse &U : Op->ops())
996 DAG.ExtractVectorElements(U.get(), Args);
Tom Stellardd86003e2013-08-14 23:25:00 +0000997
Ahmed Bougacha128f8732016-04-26 21:15:30 +0000998 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args);
Tom Stellardd86003e2013-08-14 23:25:00 +0000999}
1000
1001SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
1002 SelectionDAG &DAG) const {
1003
1004 SmallVector<SDValue, 8> Args;
Tom Stellardd86003e2013-08-14 23:25:00 +00001005 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Matt Arsenault9ec3cf22014-04-11 17:47:30 +00001006 EVT VT = Op.getValueType();
1007 DAG.ExtractVectorElements(Op.getOperand(0), Args, Start,
1008 VT.getVectorNumElements());
Tom Stellardd86003e2013-08-14 23:25:00 +00001009
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001010 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args);
Tom Stellardd86003e2013-08-14 23:25:00 +00001011}
1012
Tom Stellard75aadc22012-12-11 21:25:42 +00001013SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1014 SelectionDAG &DAG) const {
1015 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001016 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +00001017 EVT VT = Op.getValueType();
1018
1019 switch (IntrinsicID) {
Matt Arsenault2fdf2a12017-02-21 23:35:48 +00001020 default: return Op;
Matt Arsenault2fdf2a12017-02-21 23:35:48 +00001021 case AMDGPUIntrinsic::AMDGPU_bfe_i32:
1022 return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT,
1023 Op.getOperand(1),
1024 Op.getOperand(2),
1025 Op.getOperand(3));
Matt Arsenault4c537172014-03-31 18:21:18 +00001026
Matt Arsenault2fdf2a12017-02-21 23:35:48 +00001027 case AMDGPUIntrinsic::AMDGPU_bfe_u32:
1028 return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT,
1029 Op.getOperand(1),
1030 Op.getOperand(2),
1031 Op.getOperand(3));
Tom Stellard75aadc22012-12-11 21:25:42 +00001032 }
1033}
1034
Tom Stellard75aadc22012-12-11 21:25:42 +00001035/// \brief Generate Min/Max node
Matt Arsenaultda7a6562017-02-01 00:42:40 +00001036SDValue AMDGPUTargetLowering::combineFMinMaxLegacy(const SDLoc &DL, EVT VT,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001037 SDValue LHS, SDValue RHS,
1038 SDValue True, SDValue False,
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001039 SDValue CC,
1040 DAGCombinerInfo &DCI) const {
Matt Arsenaultda59f3d2014-11-13 23:03:09 +00001041 if (!(LHS == True && RHS == False) && !(LHS == False && RHS == True))
1042 return SDValue();
Tom Stellard75aadc22012-12-11 21:25:42 +00001043
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001044 SelectionDAG &DAG = DCI.DAG;
Tom Stellard75aadc22012-12-11 21:25:42 +00001045 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1046 switch (CCOpcode) {
1047 case ISD::SETOEQ:
1048 case ISD::SETONE:
1049 case ISD::SETUNE:
1050 case ISD::SETNE:
1051 case ISD::SETUEQ:
1052 case ISD::SETEQ:
1053 case ISD::SETFALSE:
1054 case ISD::SETFALSE2:
1055 case ISD::SETTRUE:
1056 case ISD::SETTRUE2:
1057 case ISD::SETUO:
1058 case ISD::SETO:
Matt Arsenaultda59f3d2014-11-13 23:03:09 +00001059 break;
Tom Stellard75aadc22012-12-11 21:25:42 +00001060 case ISD::SETULE:
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001061 case ISD::SETULT: {
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001062 if (LHS == True)
1063 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS);
1064 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS);
1065 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001066 case ISD::SETOLE:
1067 case ISD::SETOLT:
1068 case ISD::SETLE:
1069 case ISD::SETLT: {
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001070 // Ordered. Assume ordered for undefined.
1071
1072 // Only do this after legalization to avoid interfering with other combines
1073 // which might occur.
1074 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG &&
1075 !DCI.isCalledByLegalizer())
1076 return SDValue();
Marek Olsakbe047802014-12-07 12:19:03 +00001077
Matt Arsenault36094d72014-11-15 05:02:57 +00001078 // We need to permute the operands to get the correct NaN behavior. The
1079 // selected operand is the second one based on the failing compare with NaN,
1080 // so permute it based on the compare type the hardware uses.
1081 if (LHS == True)
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001082 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS);
1083 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS);
Tom Stellard75aadc22012-12-11 21:25:42 +00001084 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001085 case ISD::SETUGE:
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001086 case ISD::SETUGT: {
Matt Arsenault36094d72014-11-15 05:02:57 +00001087 if (LHS == True)
1088 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS);
1089 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS);
Tom Stellard75aadc22012-12-11 21:25:42 +00001090 }
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001091 case ISD::SETGT:
1092 case ISD::SETGE:
1093 case ISD::SETOGE:
1094 case ISD::SETOGT: {
1095 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG &&
1096 !DCI.isCalledByLegalizer())
1097 return SDValue();
1098
1099 if (LHS == True)
1100 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS);
1101 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS);
1102 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001103 case ISD::SETCC_INVALID:
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001104 llvm_unreachable("Invalid setcc condcode!");
Tom Stellard75aadc22012-12-11 21:25:42 +00001105 }
Tom Stellardafa8b532014-05-09 16:42:16 +00001106 return SDValue();
Tom Stellard75aadc22012-12-11 21:25:42 +00001107}
1108
Matt Arsenault6e3a4512016-01-18 22:01:13 +00001109std::pair<SDValue, SDValue>
1110AMDGPUTargetLowering::split64BitValue(SDValue Op, SelectionDAG &DAG) const {
1111 SDLoc SL(Op);
1112
1113 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1114
1115 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1116 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1117
1118 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1119 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1120
1121 return std::make_pair(Lo, Hi);
1122}
1123
Matt Arsenault33e3ece2016-01-18 22:09:04 +00001124SDValue AMDGPUTargetLowering::getLoHalf64(SDValue Op, SelectionDAG &DAG) const {
1125 SDLoc SL(Op);
1126
1127 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1128 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1129 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1130}
1131
1132SDValue AMDGPUTargetLowering::getHiHalf64(SDValue Op, SelectionDAG &DAG) const {
1133 SDLoc SL(Op);
1134
1135 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1136 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1137 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1138}
1139
Matt Arsenault83e60582014-07-24 17:10:35 +00001140SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue Op,
1141 SelectionDAG &DAG) const {
Matt Arsenault9c499c32016-04-14 23:31:26 +00001142 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault83e60582014-07-24 17:10:35 +00001143 EVT VT = Op.getValueType();
1144
Matt Arsenault9c499c32016-04-14 23:31:26 +00001145
Matt Arsenault83e60582014-07-24 17:10:35 +00001146 // If this is a 2 element vector, we really want to scalarize and not create
1147 // weird 1 element vectors.
1148 if (VT.getVectorNumElements() == 2)
Matt Arsenault9c499c32016-04-14 23:31:26 +00001149 return scalarizeVectorLoad(Load, DAG);
Matt Arsenault83e60582014-07-24 17:10:35 +00001150
Matt Arsenault83e60582014-07-24 17:10:35 +00001151 SDValue BasePtr = Load->getBasePtr();
1152 EVT PtrVT = BasePtr.getValueType();
1153 EVT MemVT = Load->getMemoryVT();
1154 SDLoc SL(Op);
Matt Arsenault52a52a52015-12-14 16:59:40 +00001155
1156 const MachinePointerInfo &SrcValue = Load->getMemOperand()->getPointerInfo();
Matt Arsenault83e60582014-07-24 17:10:35 +00001157
1158 EVT LoVT, HiVT;
1159 EVT LoMemVT, HiMemVT;
1160 SDValue Lo, Hi;
1161
1162 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
1163 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT);
1164 std::tie(Lo, Hi) = DAG.SplitVector(Op, SL, LoVT, HiVT);
Matt Arsenault52a52a52015-12-14 16:59:40 +00001165
1166 unsigned Size = LoMemVT.getStoreSize();
1167 unsigned BaseAlign = Load->getAlignment();
1168 unsigned HiAlign = MinAlign(BaseAlign, Size);
1169
Justin Lebar9c375812016-07-15 18:27:10 +00001170 SDValue LoLoad = DAG.getExtLoad(Load->getExtensionType(), SL, LoVT,
1171 Load->getChain(), BasePtr, SrcValue, LoMemVT,
1172 BaseAlign, Load->getMemOperand()->getFlags());
Matt Arsenault83e60582014-07-24 17:10:35 +00001173 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
Matt Arsenault52a52a52015-12-14 16:59:40 +00001174 DAG.getConstant(Size, SL, PtrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00001175 SDValue HiLoad =
1176 DAG.getExtLoad(Load->getExtensionType(), SL, HiVT, Load->getChain(),
1177 HiPtr, SrcValue.getWithOffset(LoMemVT.getStoreSize()),
1178 HiMemVT, HiAlign, Load->getMemOperand()->getFlags());
Matt Arsenault83e60582014-07-24 17:10:35 +00001179
1180 SDValue Ops[] = {
1181 DAG.getNode(ISD::CONCAT_VECTORS, SL, VT, LoLoad, HiLoad),
1182 DAG.getNode(ISD::TokenFactor, SL, MVT::Other,
1183 LoLoad.getValue(1), HiLoad.getValue(1))
1184 };
1185
1186 return DAG.getMergeValues(Ops, SL);
1187}
1188
Matt Arsenault83e60582014-07-24 17:10:35 +00001189SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op,
1190 SelectionDAG &DAG) const {
1191 StoreSDNode *Store = cast<StoreSDNode>(Op);
1192 SDValue Val = Store->getValue();
1193 EVT VT = Val.getValueType();
1194
1195 // If this is a 2 element vector, we really want to scalarize and not create
1196 // weird 1 element vectors.
1197 if (VT.getVectorNumElements() == 2)
Matt Arsenault9c499c32016-04-14 23:31:26 +00001198 return scalarizeVectorStore(Store, DAG);
Matt Arsenault83e60582014-07-24 17:10:35 +00001199
1200 EVT MemVT = Store->getMemoryVT();
1201 SDValue Chain = Store->getChain();
1202 SDValue BasePtr = Store->getBasePtr();
1203 SDLoc SL(Op);
1204
1205 EVT LoVT, HiVT;
1206 EVT LoMemVT, HiMemVT;
1207 SDValue Lo, Hi;
1208
1209 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
1210 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT);
1211 std::tie(Lo, Hi) = DAG.SplitVector(Val, SL, LoVT, HiVT);
1212
1213 EVT PtrVT = BasePtr.getValueType();
1214 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001215 DAG.getConstant(LoMemVT.getStoreSize(), SL,
1216 PtrVT));
Matt Arsenault83e60582014-07-24 17:10:35 +00001217
Matt Arsenault52a52a52015-12-14 16:59:40 +00001218 const MachinePointerInfo &SrcValue = Store->getMemOperand()->getPointerInfo();
1219 unsigned BaseAlign = Store->getAlignment();
1220 unsigned Size = LoMemVT.getStoreSize();
1221 unsigned HiAlign = MinAlign(BaseAlign, Size);
1222
Justin Lebar9c375812016-07-15 18:27:10 +00001223 SDValue LoStore =
1224 DAG.getTruncStore(Chain, SL, Lo, BasePtr, SrcValue, LoMemVT, BaseAlign,
1225 Store->getMemOperand()->getFlags());
1226 SDValue HiStore =
1227 DAG.getTruncStore(Chain, SL, Hi, HiPtr, SrcValue.getWithOffset(Size),
1228 HiMemVT, HiAlign, Store->getMemOperand()->getFlags());
Matt Arsenault83e60582014-07-24 17:10:35 +00001229
1230 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, LoStore, HiStore);
1231}
1232
Matt Arsenault0daeb632014-07-24 06:59:20 +00001233// This is a shortcut for integer division because we have fast i32<->f32
1234// conversions, and fast f32 reciprocal instructions. The fractional part of a
Matt Arsenault81a70952016-05-21 01:53:33 +00001235// float is enough to accurately represent up to a 24-bit signed integer.
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001236SDValue AMDGPUTargetLowering::LowerDIVREM24(SDValue Op, SelectionDAG &DAG,
1237 bool Sign) const {
Matt Arsenault1578aa72014-06-15 20:08:02 +00001238 SDLoc DL(Op);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001239 EVT VT = Op.getValueType();
Matt Arsenault1578aa72014-06-15 20:08:02 +00001240 SDValue LHS = Op.getOperand(0);
1241 SDValue RHS = Op.getOperand(1);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001242 MVT IntVT = MVT::i32;
1243 MVT FltVT = MVT::f32;
1244
Matt Arsenault81a70952016-05-21 01:53:33 +00001245 unsigned LHSSignBits = DAG.ComputeNumSignBits(LHS);
1246 if (LHSSignBits < 9)
1247 return SDValue();
1248
1249 unsigned RHSSignBits = DAG.ComputeNumSignBits(RHS);
1250 if (RHSSignBits < 9)
1251 return SDValue();
Jan Veselye5ca27d2014-08-12 17:31:20 +00001252
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001253 unsigned BitSize = VT.getSizeInBits();
Matt Arsenault81a70952016-05-21 01:53:33 +00001254 unsigned SignBits = std::min(LHSSignBits, RHSSignBits);
1255 unsigned DivBits = BitSize - SignBits;
1256 if (Sign)
1257 ++DivBits;
1258
1259 ISD::NodeType ToFp = Sign ? ISD::SINT_TO_FP : ISD::UINT_TO_FP;
1260 ISD::NodeType ToInt = Sign ? ISD::FP_TO_SINT : ISD::FP_TO_UINT;
Matt Arsenault0daeb632014-07-24 06:59:20 +00001261
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001262 SDValue jq = DAG.getConstant(1, DL, IntVT);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001263
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001264 if (Sign) {
Jan Veselye5ca27d2014-08-12 17:31:20 +00001265 // char|short jq = ia ^ ib;
1266 jq = DAG.getNode(ISD::XOR, DL, VT, LHS, RHS);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001267
Jan Veselye5ca27d2014-08-12 17:31:20 +00001268 // jq = jq >> (bitsize - 2)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001269 jq = DAG.getNode(ISD::SRA, DL, VT, jq,
1270 DAG.getConstant(BitSize - 2, DL, VT));
Matt Arsenault1578aa72014-06-15 20:08:02 +00001271
Jan Veselye5ca27d2014-08-12 17:31:20 +00001272 // jq = jq | 0x1
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001273 jq = DAG.getNode(ISD::OR, DL, VT, jq, DAG.getConstant(1, DL, VT));
Jan Veselye5ca27d2014-08-12 17:31:20 +00001274 }
Matt Arsenault1578aa72014-06-15 20:08:02 +00001275
1276 // int ia = (int)LHS;
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001277 SDValue ia = LHS;
Matt Arsenault1578aa72014-06-15 20:08:02 +00001278
1279 // int ib, (int)RHS;
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001280 SDValue ib = RHS;
Matt Arsenault1578aa72014-06-15 20:08:02 +00001281
1282 // float fa = (float)ia;
Jan Veselye5ca27d2014-08-12 17:31:20 +00001283 SDValue fa = DAG.getNode(ToFp, DL, FltVT, ia);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001284
1285 // float fb = (float)ib;
Jan Veselye5ca27d2014-08-12 17:31:20 +00001286 SDValue fb = DAG.getNode(ToFp, DL, FltVT, ib);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001287
Matt Arsenault0daeb632014-07-24 06:59:20 +00001288 SDValue fq = DAG.getNode(ISD::FMUL, DL, FltVT,
1289 fa, DAG.getNode(AMDGPUISD::RCP, DL, FltVT, fb));
Matt Arsenault1578aa72014-06-15 20:08:02 +00001290
1291 // fq = trunc(fq);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001292 fq = DAG.getNode(ISD::FTRUNC, DL, FltVT, fq);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001293
1294 // float fqneg = -fq;
Matt Arsenault0daeb632014-07-24 06:59:20 +00001295 SDValue fqneg = DAG.getNode(ISD::FNEG, DL, FltVT, fq);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001296
1297 // float fr = mad(fqneg, fb, fa);
Wei Ding4d3d4ca2017-02-24 23:00:29 +00001298 unsigned OpCode = Subtarget->hasFP32Denormals() ?
1299 (unsigned)AMDGPUISD::FMAD_FTZ :
1300 (unsigned)ISD::FMAD;
1301 SDValue fr = DAG.getNode(OpCode, DL, FltVT, fqneg, fb, fa);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001302
1303 // int iq = (int)fq;
Jan Veselye5ca27d2014-08-12 17:31:20 +00001304 SDValue iq = DAG.getNode(ToInt, DL, IntVT, fq);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001305
1306 // fr = fabs(fr);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001307 fr = DAG.getNode(ISD::FABS, DL, FltVT, fr);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001308
1309 // fb = fabs(fb);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001310 fb = DAG.getNode(ISD::FABS, DL, FltVT, fb);
1311
Mehdi Amini44ede332015-07-09 02:09:04 +00001312 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001313
1314 // int cv = fr >= fb;
Matt Arsenault0daeb632014-07-24 06:59:20 +00001315 SDValue cv = DAG.getSetCC(DL, SetCCVT, fr, fb, ISD::SETOGE);
1316
Matt Arsenault1578aa72014-06-15 20:08:02 +00001317 // jq = (cv ? jq : 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001318 jq = DAG.getNode(ISD::SELECT, DL, VT, cv, jq, DAG.getConstant(0, DL, VT));
Matt Arsenault0daeb632014-07-24 06:59:20 +00001319
Jan Veselye5ca27d2014-08-12 17:31:20 +00001320 // dst = iq + jq;
Jan Vesely4a33bc62014-08-12 17:31:17 +00001321 SDValue Div = DAG.getNode(ISD::ADD, DL, VT, iq, jq);
1322
Jan Veselye5ca27d2014-08-12 17:31:20 +00001323 // Rem needs compensation, it's easier to recompute it
Jan Vesely4a33bc62014-08-12 17:31:17 +00001324 SDValue Rem = DAG.getNode(ISD::MUL, DL, VT, Div, RHS);
1325 Rem = DAG.getNode(ISD::SUB, DL, VT, LHS, Rem);
1326
Matt Arsenault81a70952016-05-21 01:53:33 +00001327 // Truncate to number of bits this divide really is.
1328 if (Sign) {
1329 SDValue InRegSize
1330 = DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), DivBits));
1331 Div = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Div, InRegSize);
1332 Rem = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Rem, InRegSize);
1333 } else {
1334 SDValue TruncMask = DAG.getConstant((UINT64_C(1) << DivBits) - 1, DL, VT);
1335 Div = DAG.getNode(ISD::AND, DL, VT, Div, TruncMask);
1336 Rem = DAG.getNode(ISD::AND, DL, VT, Rem, TruncMask);
1337 }
1338
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001339 return DAG.getMergeValues({ Div, Rem }, DL);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001340}
1341
Tom Stellardbf69d762014-11-15 01:07:53 +00001342void AMDGPUTargetLowering::LowerUDIVREM64(SDValue Op,
1343 SelectionDAG &DAG,
1344 SmallVectorImpl<SDValue> &Results) const {
1345 assert(Op.getValueType() == MVT::i64);
1346
1347 SDLoc DL(Op);
1348 EVT VT = Op.getValueType();
1349 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
1350
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001351 SDValue one = DAG.getConstant(1, DL, HalfVT);
1352 SDValue zero = DAG.getConstant(0, DL, HalfVT);
Tom Stellardbf69d762014-11-15 01:07:53 +00001353
1354 //HiLo split
1355 SDValue LHS = Op.getOperand(0);
1356 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, zero);
1357 SDValue LHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, one);
1358
1359 SDValue RHS = Op.getOperand(1);
1360 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, zero);
1361 SDValue RHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, one);
1362
Jan Vesely5f715d32015-01-22 23:42:43 +00001363 if (VT == MVT::i64 &&
1364 DAG.MaskedValueIsZero(RHS, APInt::getHighBitsSet(64, 32)) &&
1365 DAG.MaskedValueIsZero(LHS, APInt::getHighBitsSet(64, 32))) {
1366
1367 SDValue Res = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(HalfVT, HalfVT),
1368 LHS_Lo, RHS_Lo);
1369
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001370 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(0), zero});
1371 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(1), zero});
Matt Arsenaultd275fca2016-03-01 05:06:05 +00001372
1373 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV));
1374 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM));
Jan Vesely5f715d32015-01-22 23:42:43 +00001375 return;
1376 }
1377
Tom Stellardbf69d762014-11-15 01:07:53 +00001378 // Get Speculative values
1379 SDValue DIV_Part = DAG.getNode(ISD::UDIV, DL, HalfVT, LHS_Hi, RHS_Lo);
1380 SDValue REM_Part = DAG.getNode(ISD::UREM, DL, HalfVT, LHS_Hi, RHS_Lo);
1381
Tom Stellardbf69d762014-11-15 01:07:53 +00001382 SDValue REM_Lo = DAG.getSelectCC(DL, RHS_Hi, zero, REM_Part, LHS_Hi, ISD::SETEQ);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001383 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {REM_Lo, zero});
Matt Arsenaultd275fca2016-03-01 05:06:05 +00001384 REM = DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM);
Tom Stellardbf69d762014-11-15 01:07:53 +00001385
1386 SDValue DIV_Hi = DAG.getSelectCC(DL, RHS_Hi, zero, DIV_Part, zero, ISD::SETEQ);
1387 SDValue DIV_Lo = zero;
1388
1389 const unsigned halfBitWidth = HalfVT.getSizeInBits();
1390
1391 for (unsigned i = 0; i < halfBitWidth; ++i) {
Jan Veselyf7987ca2015-01-22 23:42:39 +00001392 const unsigned bitPos = halfBitWidth - i - 1;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001393 SDValue POS = DAG.getConstant(bitPos, DL, HalfVT);
Jan Veselyf7987ca2015-01-22 23:42:39 +00001394 // Get value of high bit
Jan Vesely811ef522015-04-12 23:45:01 +00001395 SDValue HBit = DAG.getNode(ISD::SRL, DL, HalfVT, LHS_Lo, POS);
1396 HBit = DAG.getNode(ISD::AND, DL, HalfVT, HBit, one);
Jan Veselyf7987ca2015-01-22 23:42:39 +00001397 HBit = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, HBit);
Tom Stellardbf69d762014-11-15 01:07:53 +00001398
Jan Veselyf7987ca2015-01-22 23:42:39 +00001399 // Shift
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001400 REM = DAG.getNode(ISD::SHL, DL, VT, REM, DAG.getConstant(1, DL, VT));
Jan Veselyf7987ca2015-01-22 23:42:39 +00001401 // Add LHS high bit
1402 REM = DAG.getNode(ISD::OR, DL, VT, REM, HBit);
Tom Stellardbf69d762014-11-15 01:07:53 +00001403
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +00001404 SDValue BIT = DAG.getConstant(1ULL << bitPos, DL, HalfVT);
Tom Stellard83171b32014-11-15 01:07:57 +00001405 SDValue realBIT = DAG.getSelectCC(DL, REM, RHS, BIT, zero, ISD::SETUGE);
Tom Stellardbf69d762014-11-15 01:07:53 +00001406
1407 DIV_Lo = DAG.getNode(ISD::OR, DL, HalfVT, DIV_Lo, realBIT);
1408
1409 // Update REM
Tom Stellardbf69d762014-11-15 01:07:53 +00001410 SDValue REM_sub = DAG.getNode(ISD::SUB, DL, VT, REM, RHS);
Tom Stellard83171b32014-11-15 01:07:57 +00001411 REM = DAG.getSelectCC(DL, REM, RHS, REM_sub, REM, ISD::SETUGE);
Tom Stellardbf69d762014-11-15 01:07:53 +00001412 }
1413
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001414 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {DIV_Lo, DIV_Hi});
Matt Arsenaultd275fca2016-03-01 05:06:05 +00001415 DIV = DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV);
Tom Stellardbf69d762014-11-15 01:07:53 +00001416 Results.push_back(DIV);
1417 Results.push_back(REM);
1418}
1419
Tom Stellard75aadc22012-12-11 21:25:42 +00001420SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
Matt Arsenault46013d92014-05-11 21:24:41 +00001421 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001422 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +00001423 EVT VT = Op.getValueType();
1424
Tom Stellardbf69d762014-11-15 01:07:53 +00001425 if (VT == MVT::i64) {
1426 SmallVector<SDValue, 2> Results;
1427 LowerUDIVREM64(Op, DAG, Results);
1428 return DAG.getMergeValues(Results, DL);
1429 }
1430
Matt Arsenault81a70952016-05-21 01:53:33 +00001431 if (VT == MVT::i32) {
1432 if (SDValue Res = LowerDIVREM24(Op, DAG, false))
1433 return Res;
1434 }
1435
Tom Stellard75aadc22012-12-11 21:25:42 +00001436 SDValue Num = Op.getOperand(0);
1437 SDValue Den = Op.getOperand(1);
1438
Tom Stellard75aadc22012-12-11 21:25:42 +00001439 // RCP = URECIP(Den) = 2^32 / Den + e
1440 // e is rounding error.
1441 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
1442
Tom Stellard4349b192014-09-22 15:35:30 +00001443 // RCP_LO = mul(RCP, Den) */
1444 SDValue RCP_LO = DAG.getNode(ISD::MUL, DL, VT, RCP, Den);
Tom Stellard75aadc22012-12-11 21:25:42 +00001445
1446 // RCP_HI = mulhu (RCP, Den) */
1447 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
1448
1449 // NEG_RCP_LO = -RCP_LO
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001450 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001451 RCP_LO);
1452
1453 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001454 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001455 NEG_RCP_LO, RCP_LO,
1456 ISD::SETEQ);
1457 // Calculate the rounding error from the URECIP instruction
1458 // E = mulhu(ABS_RCP_LO, RCP)
1459 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
1460
1461 // RCP_A_E = RCP + E
1462 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
1463
1464 // RCP_S_E = RCP - E
1465 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
1466
1467 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001468 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001469 RCP_A_E, RCP_S_E,
1470 ISD::SETEQ);
1471 // Quotient = mulhu(Tmp0, Num)
1472 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
1473
1474 // Num_S_Remainder = Quotient * Den
Tom Stellard4349b192014-09-22 15:35:30 +00001475 SDValue Num_S_Remainder = DAG.getNode(ISD::MUL, DL, VT, Quotient, Den);
Tom Stellard75aadc22012-12-11 21:25:42 +00001476
1477 // Remainder = Num - Num_S_Remainder
1478 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
1479
1480 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
1481 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001482 DAG.getConstant(-1, DL, VT),
1483 DAG.getConstant(0, DL, VT),
Vincent Lejeune4f3751f2013-11-06 17:36:04 +00001484 ISD::SETUGE);
1485 // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0)
1486 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Num,
1487 Num_S_Remainder,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001488 DAG.getConstant(-1, DL, VT),
1489 DAG.getConstant(0, DL, VT),
Vincent Lejeune4f3751f2013-11-06 17:36:04 +00001490 ISD::SETUGE);
Tom Stellard75aadc22012-12-11 21:25:42 +00001491 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
1492 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
1493 Remainder_GE_Zero);
1494
1495 // Calculate Division result:
1496
1497 // Quotient_A_One = Quotient + 1
1498 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001499 DAG.getConstant(1, DL, VT));
Tom Stellard75aadc22012-12-11 21:25:42 +00001500
1501 // Quotient_S_One = Quotient - 1
1502 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001503 DAG.getConstant(1, DL, VT));
Tom Stellard75aadc22012-12-11 21:25:42 +00001504
1505 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001506 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001507 Quotient, Quotient_A_One, ISD::SETEQ);
1508
1509 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001510 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001511 Quotient_S_One, Div, ISD::SETEQ);
1512
1513 // Calculate Rem result:
1514
1515 // Remainder_S_Den = Remainder - Den
1516 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
1517
1518 // Remainder_A_Den = Remainder + Den
1519 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
1520
1521 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001522 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001523 Remainder, Remainder_S_Den, ISD::SETEQ);
1524
1525 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001526 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001527 Remainder_A_Den, Rem, ISD::SETEQ);
Matt Arsenault7939acd2014-04-07 16:44:24 +00001528 SDValue Ops[2] = {
1529 Div,
1530 Rem
1531 };
Craig Topper64941d92014-04-27 19:20:57 +00001532 return DAG.getMergeValues(Ops, DL);
Tom Stellard75aadc22012-12-11 21:25:42 +00001533}
1534
Jan Vesely109efdf2014-06-22 21:43:00 +00001535SDValue AMDGPUTargetLowering::LowerSDIVREM(SDValue Op,
1536 SelectionDAG &DAG) const {
1537 SDLoc DL(Op);
1538 EVT VT = Op.getValueType();
1539
Jan Vesely109efdf2014-06-22 21:43:00 +00001540 SDValue LHS = Op.getOperand(0);
1541 SDValue RHS = Op.getOperand(1);
1542
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001543 SDValue Zero = DAG.getConstant(0, DL, VT);
1544 SDValue NegOne = DAG.getConstant(-1, DL, VT);
Jan Vesely4a33bc62014-08-12 17:31:17 +00001545
Matt Arsenault81a70952016-05-21 01:53:33 +00001546 if (VT == MVT::i32) {
1547 if (SDValue Res = LowerDIVREM24(Op, DAG, true))
1548 return Res;
Jan Vesely5f715d32015-01-22 23:42:43 +00001549 }
Matt Arsenault81a70952016-05-21 01:53:33 +00001550
Jan Vesely5f715d32015-01-22 23:42:43 +00001551 if (VT == MVT::i64 &&
1552 DAG.ComputeNumSignBits(LHS) > 32 &&
1553 DAG.ComputeNumSignBits(RHS) > 32) {
1554 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
1555
1556 //HiLo split
1557 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, Zero);
1558 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, Zero);
1559 SDValue DIVREM = DAG.getNode(ISD::SDIVREM, DL, DAG.getVTList(HalfVT, HalfVT),
1560 LHS_Lo, RHS_Lo);
1561 SDValue Res[2] = {
1562 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(0)),
1563 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(1))
1564 };
1565 return DAG.getMergeValues(Res, DL);
1566 }
1567
Jan Vesely109efdf2014-06-22 21:43:00 +00001568 SDValue LHSign = DAG.getSelectCC(DL, LHS, Zero, NegOne, Zero, ISD::SETLT);
1569 SDValue RHSign = DAG.getSelectCC(DL, RHS, Zero, NegOne, Zero, ISD::SETLT);
1570 SDValue DSign = DAG.getNode(ISD::XOR, DL, VT, LHSign, RHSign);
1571 SDValue RSign = LHSign; // Remainder sign is the same as LHS
1572
1573 LHS = DAG.getNode(ISD::ADD, DL, VT, LHS, LHSign);
1574 RHS = DAG.getNode(ISD::ADD, DL, VT, RHS, RHSign);
1575
1576 LHS = DAG.getNode(ISD::XOR, DL, VT, LHS, LHSign);
1577 RHS = DAG.getNode(ISD::XOR, DL, VT, RHS, RHSign);
1578
1579 SDValue Div = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT), LHS, RHS);
1580 SDValue Rem = Div.getValue(1);
1581
1582 Div = DAG.getNode(ISD::XOR, DL, VT, Div, DSign);
1583 Rem = DAG.getNode(ISD::XOR, DL, VT, Rem, RSign);
1584
1585 Div = DAG.getNode(ISD::SUB, DL, VT, Div, DSign);
1586 Rem = DAG.getNode(ISD::SUB, DL, VT, Rem, RSign);
1587
1588 SDValue Res[2] = {
1589 Div,
1590 Rem
1591 };
1592 return DAG.getMergeValues(Res, DL);
1593}
1594
Matt Arsenault16e31332014-09-10 21:44:27 +00001595// (frem x, y) -> (fsub x, (fmul (ftrunc (fdiv x, y)), y))
1596SDValue AMDGPUTargetLowering::LowerFREM(SDValue Op, SelectionDAG &DAG) const {
1597 SDLoc SL(Op);
1598 EVT VT = Op.getValueType();
1599 SDValue X = Op.getOperand(0);
1600 SDValue Y = Op.getOperand(1);
1601
Sanjay Patela2607012015-09-16 16:31:21 +00001602 // TODO: Should this propagate fast-math-flags?
1603
Matt Arsenault16e31332014-09-10 21:44:27 +00001604 SDValue Div = DAG.getNode(ISD::FDIV, SL, VT, X, Y);
1605 SDValue Floor = DAG.getNode(ISD::FTRUNC, SL, VT, Div);
1606 SDValue Mul = DAG.getNode(ISD::FMUL, SL, VT, Floor, Y);
1607
1608 return DAG.getNode(ISD::FSUB, SL, VT, X, Mul);
1609}
1610
Matt Arsenault46010932014-06-18 17:05:30 +00001611SDValue AMDGPUTargetLowering::LowerFCEIL(SDValue Op, SelectionDAG &DAG) const {
1612 SDLoc SL(Op);
1613 SDValue Src = Op.getOperand(0);
1614
1615 // result = trunc(src)
1616 // if (src > 0.0 && src != result)
1617 // result += 1.0
1618
1619 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1620
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001621 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64);
1622 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001623
Mehdi Amini44ede332015-07-09 02:09:04 +00001624 EVT SetCCVT =
1625 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001626
1627 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOGT);
1628 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1629 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1630
1631 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, One, Zero);
Sanjay Patela2607012015-09-16 16:31:21 +00001632 // TODO: Should this propagate fast-math-flags?
Matt Arsenault46010932014-06-18 17:05:30 +00001633 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1634}
1635
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001636static SDValue extractF64Exponent(SDValue Hi, const SDLoc &SL,
1637 SelectionDAG &DAG) {
Matt Arsenaultb0055482015-01-21 18:18:25 +00001638 const unsigned FractBits = 52;
1639 const unsigned ExpBits = 11;
1640
1641 SDValue ExpPart = DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32,
1642 Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001643 DAG.getConstant(FractBits - 32, SL, MVT::i32),
1644 DAG.getConstant(ExpBits, SL, MVT::i32));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001645 SDValue Exp = DAG.getNode(ISD::SUB, SL, MVT::i32, ExpPart,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001646 DAG.getConstant(1023, SL, MVT::i32));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001647
1648 return Exp;
1649}
1650
Matt Arsenault46010932014-06-18 17:05:30 +00001651SDValue AMDGPUTargetLowering::LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const {
1652 SDLoc SL(Op);
1653 SDValue Src = Op.getOperand(0);
1654
1655 assert(Op.getValueType() == MVT::f64);
1656
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001657 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1658 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001659
1660 SDValue VecSrc = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1661
1662 // Extract the upper half, since this is where we will find the sign and
1663 // exponent.
1664 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, VecSrc, One);
1665
Matt Arsenaultb0055482015-01-21 18:18:25 +00001666 SDValue Exp = extractF64Exponent(Hi, SL, DAG);
Matt Arsenault46010932014-06-18 17:05:30 +00001667
Matt Arsenaultb0055482015-01-21 18:18:25 +00001668 const unsigned FractBits = 52;
Matt Arsenault46010932014-06-18 17:05:30 +00001669
1670 // Extract the sign bit.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001671 const SDValue SignBitMask = DAG.getConstant(UINT32_C(1) << 31, SL, MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001672 SDValue SignBit = DAG.getNode(ISD::AND, SL, MVT::i32, Hi, SignBitMask);
1673
1674 // Extend back to to 64-bits.
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001675 SDValue SignBit64 = DAG.getBuildVector(MVT::v2i32, SL, {Zero, SignBit});
Matt Arsenault46010932014-06-18 17:05:30 +00001676 SignBit64 = DAG.getNode(ISD::BITCAST, SL, MVT::i64, SignBit64);
1677
1678 SDValue BcInt = DAG.getNode(ISD::BITCAST, SL, MVT::i64, Src);
Matt Arsenault2b0fa432014-06-18 22:11:03 +00001679 const SDValue FractMask
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001680 = DAG.getConstant((UINT64_C(1) << FractBits) - 1, SL, MVT::i64);
Matt Arsenault46010932014-06-18 17:05:30 +00001681
1682 SDValue Shr = DAG.getNode(ISD::SRA, SL, MVT::i64, FractMask, Exp);
1683 SDValue Not = DAG.getNOT(SL, Shr, MVT::i64);
1684 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, BcInt, Not);
1685
Mehdi Amini44ede332015-07-09 02:09:04 +00001686 EVT SetCCVT =
1687 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001688
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001689 const SDValue FiftyOne = DAG.getConstant(FractBits - 1, SL, MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001690
1691 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
1692 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
1693
1694 SDValue Tmp1 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpLt0, SignBit64, Tmp0);
1695 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpGt51, BcInt, Tmp1);
1696
1697 return DAG.getNode(ISD::BITCAST, SL, MVT::f64, Tmp2);
1698}
1699
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001700SDValue AMDGPUTargetLowering::LowerFRINT(SDValue Op, SelectionDAG &DAG) const {
1701 SDLoc SL(Op);
1702 SDValue Src = Op.getOperand(0);
1703
1704 assert(Op.getValueType() == MVT::f64);
1705
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001706 APFloat C1Val(APFloat::IEEEdouble(), "0x1.0p+52");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001707 SDValue C1 = DAG.getConstantFP(C1Val, SL, MVT::f64);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001708 SDValue CopySign = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, C1, Src);
1709
Sanjay Patela2607012015-09-16 16:31:21 +00001710 // TODO: Should this propagate fast-math-flags?
1711
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001712 SDValue Tmp1 = DAG.getNode(ISD::FADD, SL, MVT::f64, Src, CopySign);
1713 SDValue Tmp2 = DAG.getNode(ISD::FSUB, SL, MVT::f64, Tmp1, CopySign);
1714
1715 SDValue Fabs = DAG.getNode(ISD::FABS, SL, MVT::f64, Src);
Matt Arsenaultd22626f2014-06-18 17:45:58 +00001716
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001717 APFloat C2Val(APFloat::IEEEdouble(), "0x1.fffffffffffffp+51");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001718 SDValue C2 = DAG.getConstantFP(C2Val, SL, MVT::f64);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001719
Mehdi Amini44ede332015-07-09 02:09:04 +00001720 EVT SetCCVT =
1721 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001722 SDValue Cond = DAG.getSetCC(SL, SetCCVT, Fabs, C2, ISD::SETOGT);
1723
1724 return DAG.getSelect(SL, MVT::f64, Cond, Src, Tmp2);
1725}
1726
Matt Arsenault692bd5e2014-06-18 22:03:45 +00001727SDValue AMDGPUTargetLowering::LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const {
1728 // FNEARBYINT and FRINT are the same, except in their handling of FP
1729 // exceptions. Those aren't really meaningful for us, and OpenCL only has
1730 // rint, so just treat them as equivalent.
1731 return DAG.getNode(ISD::FRINT, SDLoc(Op), Op.getValueType(), Op.getOperand(0));
1732}
1733
Matt Arsenaultb0055482015-01-21 18:18:25 +00001734// XXX - May require not supporting f32 denormals?
1735SDValue AMDGPUTargetLowering::LowerFROUND32(SDValue Op, SelectionDAG &DAG) const {
1736 SDLoc SL(Op);
1737 SDValue X = Op.getOperand(0);
1738
1739 SDValue T = DAG.getNode(ISD::FTRUNC, SL, MVT::f32, X);
1740
Sanjay Patela2607012015-09-16 16:31:21 +00001741 // TODO: Should this propagate fast-math-flags?
1742
Matt Arsenaultb0055482015-01-21 18:18:25 +00001743 SDValue Diff = DAG.getNode(ISD::FSUB, SL, MVT::f32, X, T);
1744
1745 SDValue AbsDiff = DAG.getNode(ISD::FABS, SL, MVT::f32, Diff);
1746
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001747 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f32);
1748 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
1749 const SDValue Half = DAG.getConstantFP(0.5, SL, MVT::f32);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001750
1751 SDValue SignOne = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f32, One, X);
1752
Mehdi Amini44ede332015-07-09 02:09:04 +00001753 EVT SetCCVT =
1754 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001755
1756 SDValue Cmp = DAG.getSetCC(SL, SetCCVT, AbsDiff, Half, ISD::SETOGE);
1757
1758 SDValue Sel = DAG.getNode(ISD::SELECT, SL, MVT::f32, Cmp, SignOne, Zero);
1759
1760 return DAG.getNode(ISD::FADD, SL, MVT::f32, T, Sel);
1761}
1762
1763SDValue AMDGPUTargetLowering::LowerFROUND64(SDValue Op, SelectionDAG &DAG) const {
1764 SDLoc SL(Op);
1765 SDValue X = Op.getOperand(0);
1766
1767 SDValue L = DAG.getNode(ISD::BITCAST, SL, MVT::i64, X);
1768
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001769 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1770 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1771 const SDValue NegOne = DAG.getConstant(-1, SL, MVT::i32);
1772 const SDValue FiftyOne = DAG.getConstant(51, SL, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00001773 EVT SetCCVT =
1774 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001775
1776 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1777
1778 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, One);
1779
1780 SDValue Exp = extractF64Exponent(Hi, SL, DAG);
1781
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001782 const SDValue Mask = DAG.getConstant(INT64_C(0x000fffffffffffff), SL,
1783 MVT::i64);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001784
1785 SDValue M = DAG.getNode(ISD::SRA, SL, MVT::i64, Mask, Exp);
1786 SDValue D = DAG.getNode(ISD::SRA, SL, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001787 DAG.getConstant(INT64_C(0x0008000000000000), SL,
1788 MVT::i64),
Matt Arsenaultb0055482015-01-21 18:18:25 +00001789 Exp);
1790
1791 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, L, M);
1792 SDValue Tmp1 = DAG.getSetCC(SL, SetCCVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001793 DAG.getConstant(0, SL, MVT::i64), Tmp0,
Matt Arsenaultb0055482015-01-21 18:18:25 +00001794 ISD::SETNE);
1795
1796 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, Tmp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001797 D, DAG.getConstant(0, SL, MVT::i64));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001798 SDValue K = DAG.getNode(ISD::ADD, SL, MVT::i64, L, Tmp2);
1799
1800 K = DAG.getNode(ISD::AND, SL, MVT::i64, K, DAG.getNOT(SL, M, MVT::i64));
1801 K = DAG.getNode(ISD::BITCAST, SL, MVT::f64, K);
1802
1803 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
1804 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
1805 SDValue ExpEqNegOne = DAG.getSetCC(SL, SetCCVT, NegOne, Exp, ISD::SETEQ);
1806
1807 SDValue Mag = DAG.getNode(ISD::SELECT, SL, MVT::f64,
1808 ExpEqNegOne,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001809 DAG.getConstantFP(1.0, SL, MVT::f64),
1810 DAG.getConstantFP(0.0, SL, MVT::f64));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001811
1812 SDValue S = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, Mag, X);
1813
1814 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpLt0, S, K);
1815 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpGt51, X, K);
1816
1817 return K;
1818}
1819
1820SDValue AMDGPUTargetLowering::LowerFROUND(SDValue Op, SelectionDAG &DAG) const {
1821 EVT VT = Op.getValueType();
1822
1823 if (VT == MVT::f32)
1824 return LowerFROUND32(Op, DAG);
1825
1826 if (VT == MVT::f64)
1827 return LowerFROUND64(Op, DAG);
1828
1829 llvm_unreachable("unhandled type");
1830}
1831
Matt Arsenault46010932014-06-18 17:05:30 +00001832SDValue AMDGPUTargetLowering::LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const {
1833 SDLoc SL(Op);
1834 SDValue Src = Op.getOperand(0);
1835
1836 // result = trunc(src);
1837 // if (src < 0.0 && src != result)
1838 // result += -1.0.
1839
1840 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1841
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001842 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64);
1843 const SDValue NegOne = DAG.getConstantFP(-1.0, SL, MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001844
Mehdi Amini44ede332015-07-09 02:09:04 +00001845 EVT SetCCVT =
1846 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001847
1848 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOLT);
1849 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1850 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1851
1852 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, NegOne, Zero);
Sanjay Patela2607012015-09-16 16:31:21 +00001853 // TODO: Should this propagate fast-math-flags?
Matt Arsenault46010932014-06-18 17:05:30 +00001854 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1855}
1856
Matt Arsenaultf058d672016-01-11 16:50:29 +00001857SDValue AMDGPUTargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
1858 SDLoc SL(Op);
1859 SDValue Src = Op.getOperand(0);
Matt Arsenaultf058d672016-01-11 16:50:29 +00001860 bool ZeroUndef = Op.getOpcode() == ISD::CTLZ_ZERO_UNDEF;
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00001861
1862 if (ZeroUndef && Src.getValueType() == MVT::i32)
1863 return DAG.getNode(AMDGPUISD::FFBH_U32, SL, MVT::i32, Src);
1864
Matt Arsenaultf058d672016-01-11 16:50:29 +00001865 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1866
1867 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1868 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1869
1870 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1871 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1872
1873 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(),
1874 *DAG.getContext(), MVT::i32);
1875
1876 SDValue Hi0 = DAG.getSetCC(SL, SetCCVT, Hi, Zero, ISD::SETEQ);
1877
1878 SDValue CtlzLo = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Lo);
1879 SDValue CtlzHi = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Hi);
1880
1881 const SDValue Bits32 = DAG.getConstant(32, SL, MVT::i32);
1882 SDValue Add = DAG.getNode(ISD::ADD, SL, MVT::i32, CtlzLo, Bits32);
1883
1884 // ctlz(x) = hi_32(x) == 0 ? ctlz(lo_32(x)) + 32 : ctlz(hi_32(x))
1885 SDValue NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32, Hi0, Add, CtlzHi);
1886
1887 if (!ZeroUndef) {
1888 // Test if the full 64-bit input is zero.
1889
1890 // FIXME: DAG combines turn what should be an s_and_b64 into a v_or_b32,
1891 // which we probably don't want.
1892 SDValue Lo0 = DAG.getSetCC(SL, SetCCVT, Lo, Zero, ISD::SETEQ);
1893 SDValue SrcIsZero = DAG.getNode(ISD::AND, SL, SetCCVT, Lo0, Hi0);
1894
1895 // TODO: If i64 setcc is half rate, it can result in 1 fewer instruction
1896 // with the same cycles, otherwise it is slower.
1897 // SDValue SrcIsZero = DAG.getSetCC(SL, SetCCVT, Src,
1898 // DAG.getConstant(0, SL, MVT::i64), ISD::SETEQ);
1899
1900 const SDValue Bits32 = DAG.getConstant(64, SL, MVT::i32);
1901
1902 // The instruction returns -1 for 0 input, but the defined intrinsic
1903 // behavior is to return the number of bits.
1904 NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32,
1905 SrcIsZero, Bits32, NewCtlz);
1906 }
1907
1908 return DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i64, NewCtlz);
1909}
1910
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00001911SDValue AMDGPUTargetLowering::LowerINT_TO_FP32(SDValue Op, SelectionDAG &DAG,
1912 bool Signed) const {
1913 // Unsigned
1914 // cul2f(ulong u)
1915 //{
1916 // uint lz = clz(u);
1917 // uint e = (u != 0) ? 127U + 63U - lz : 0;
1918 // u = (u << lz) & 0x7fffffffffffffffUL;
1919 // ulong t = u & 0xffffffffffUL;
1920 // uint v = (e << 23) | (uint)(u >> 40);
1921 // uint r = t > 0x8000000000UL ? 1U : (t == 0x8000000000UL ? v & 1U : 0U);
1922 // return as_float(v + r);
1923 //}
1924 // Signed
1925 // cl2f(long l)
1926 //{
1927 // long s = l >> 63;
1928 // float r = cul2f((l + s) ^ s);
1929 // return s ? -r : r;
1930 //}
1931
1932 SDLoc SL(Op);
1933 SDValue Src = Op.getOperand(0);
1934 SDValue L = Src;
1935
1936 SDValue S;
1937 if (Signed) {
1938 const SDValue SignBit = DAG.getConstant(63, SL, MVT::i64);
1939 S = DAG.getNode(ISD::SRA, SL, MVT::i64, L, SignBit);
1940
1941 SDValue LPlusS = DAG.getNode(ISD::ADD, SL, MVT::i64, L, S);
1942 L = DAG.getNode(ISD::XOR, SL, MVT::i64, LPlusS, S);
1943 }
1944
1945 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(),
1946 *DAG.getContext(), MVT::f32);
1947
1948
1949 SDValue ZeroI32 = DAG.getConstant(0, SL, MVT::i32);
1950 SDValue ZeroI64 = DAG.getConstant(0, SL, MVT::i64);
1951 SDValue LZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i64, L);
1952 LZ = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LZ);
1953
1954 SDValue K = DAG.getConstant(127U + 63U, SL, MVT::i32);
1955 SDValue E = DAG.getSelect(SL, MVT::i32,
1956 DAG.getSetCC(SL, SetCCVT, L, ZeroI64, ISD::SETNE),
1957 DAG.getNode(ISD::SUB, SL, MVT::i32, K, LZ),
1958 ZeroI32);
1959
1960 SDValue U = DAG.getNode(ISD::AND, SL, MVT::i64,
1961 DAG.getNode(ISD::SHL, SL, MVT::i64, L, LZ),
1962 DAG.getConstant((-1ULL) >> 1, SL, MVT::i64));
1963
1964 SDValue T = DAG.getNode(ISD::AND, SL, MVT::i64, U,
1965 DAG.getConstant(0xffffffffffULL, SL, MVT::i64));
1966
1967 SDValue UShl = DAG.getNode(ISD::SRL, SL, MVT::i64,
1968 U, DAG.getConstant(40, SL, MVT::i64));
1969
1970 SDValue V = DAG.getNode(ISD::OR, SL, MVT::i32,
1971 DAG.getNode(ISD::SHL, SL, MVT::i32, E, DAG.getConstant(23, SL, MVT::i32)),
1972 DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, UShl));
1973
1974 SDValue C = DAG.getConstant(0x8000000000ULL, SL, MVT::i64);
1975 SDValue RCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETUGT);
1976 SDValue TCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETEQ);
1977
1978 SDValue One = DAG.getConstant(1, SL, MVT::i32);
1979
1980 SDValue VTrunc1 = DAG.getNode(ISD::AND, SL, MVT::i32, V, One);
1981
1982 SDValue R = DAG.getSelect(SL, MVT::i32,
1983 RCmp,
1984 One,
1985 DAG.getSelect(SL, MVT::i32, TCmp, VTrunc1, ZeroI32));
1986 R = DAG.getNode(ISD::ADD, SL, MVT::i32, V, R);
1987 R = DAG.getNode(ISD::BITCAST, SL, MVT::f32, R);
1988
1989 if (!Signed)
1990 return R;
1991
1992 SDValue RNeg = DAG.getNode(ISD::FNEG, SL, MVT::f32, R);
1993 return DAG.getSelect(SL, MVT::f32, DAG.getSExtOrTrunc(S, SL, SetCCVT), RNeg, R);
1994}
1995
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001996SDValue AMDGPUTargetLowering::LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG,
1997 bool Signed) const {
1998 SDLoc SL(Op);
1999 SDValue Src = Op.getOperand(0);
2000
2001 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
2002
2003 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002004 DAG.getConstant(0, SL, MVT::i32));
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002005 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002006 DAG.getConstant(1, SL, MVT::i32));
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002007
2008 SDValue CvtHi = DAG.getNode(Signed ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
2009 SL, MVT::f64, Hi);
2010
2011 SDValue CvtLo = DAG.getNode(ISD::UINT_TO_FP, SL, MVT::f64, Lo);
2012
2013 SDValue LdExp = DAG.getNode(AMDGPUISD::LDEXP, SL, MVT::f64, CvtHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002014 DAG.getConstant(32, SL, MVT::i32));
Sanjay Patela2607012015-09-16 16:31:21 +00002015 // TODO: Should this propagate fast-math-flags?
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002016 return DAG.getNode(ISD::FADD, SL, MVT::f64, LdExp, CvtLo);
2017}
2018
Tom Stellardc947d8c2013-10-30 17:22:05 +00002019SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op,
2020 SelectionDAG &DAG) const {
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00002021 assert(Op.getOperand(0).getValueType() == MVT::i64 &&
2022 "operation should be legal");
Tom Stellardc947d8c2013-10-30 17:22:05 +00002023
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002024 // TODO: Factor out code common with LowerSINT_TO_FP.
2025
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002026 EVT DestVT = Op.getValueType();
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002027 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) {
2028 SDLoc DL(Op);
2029 SDValue Src = Op.getOperand(0);
2030
2031 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src);
2032 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op));
2033 SDValue FPRound =
2034 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag);
2035
2036 return FPRound;
2037 }
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002038
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00002039 if (DestVT == MVT::f32)
2040 return LowerINT_TO_FP32(Op, DAG, false);
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002041
Matt Arsenaultedc7dcb2016-07-28 00:32:05 +00002042 assert(DestVT == MVT::f64);
2043 return LowerINT_TO_FP64(Op, DAG, false);
Tom Stellardc947d8c2013-10-30 17:22:05 +00002044}
Tom Stellardfbab8272013-08-16 01:12:11 +00002045
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002046SDValue AMDGPUTargetLowering::LowerSINT_TO_FP(SDValue Op,
2047 SelectionDAG &DAG) const {
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00002048 assert(Op.getOperand(0).getValueType() == MVT::i64 &&
2049 "operation should be legal");
2050
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002051 // TODO: Factor out code common with LowerUINT_TO_FP.
2052
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00002053 EVT DestVT = Op.getValueType();
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002054 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) {
2055 SDLoc DL(Op);
2056 SDValue Src = Op.getOperand(0);
2057
2058 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src);
2059 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op));
2060 SDValue FPRound =
2061 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag);
2062
2063 return FPRound;
2064 }
2065
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00002066 if (DestVT == MVT::f32)
2067 return LowerINT_TO_FP32(Op, DAG, true);
2068
Matt Arsenaultedc7dcb2016-07-28 00:32:05 +00002069 assert(DestVT == MVT::f64);
2070 return LowerINT_TO_FP64(Op, DAG, true);
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002071}
2072
Matt Arsenaultc9961752014-10-03 23:54:56 +00002073SDValue AMDGPUTargetLowering::LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG,
2074 bool Signed) const {
2075 SDLoc SL(Op);
2076
2077 SDValue Src = Op.getOperand(0);
2078
2079 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
2080
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002081 SDValue K0 = DAG.getConstantFP(BitsToDouble(UINT64_C(0x3df0000000000000)), SL,
2082 MVT::f64);
2083 SDValue K1 = DAG.getConstantFP(BitsToDouble(UINT64_C(0xc1f0000000000000)), SL,
2084 MVT::f64);
Sanjay Patela2607012015-09-16 16:31:21 +00002085 // TODO: Should this propagate fast-math-flags?
Matt Arsenaultc9961752014-10-03 23:54:56 +00002086 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, Trunc, K0);
2087
2088 SDValue FloorMul = DAG.getNode(ISD::FFLOOR, SL, MVT::f64, Mul);
2089
2090
2091 SDValue Fma = DAG.getNode(ISD::FMA, SL, MVT::f64, FloorMul, K1, Trunc);
2092
2093 SDValue Hi = DAG.getNode(Signed ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, SL,
2094 MVT::i32, FloorMul);
2095 SDValue Lo = DAG.getNode(ISD::FP_TO_UINT, SL, MVT::i32, Fma);
2096
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002097 SDValue Result = DAG.getBuildVector(MVT::v2i32, SL, {Lo, Hi});
Matt Arsenaultc9961752014-10-03 23:54:56 +00002098
2099 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Result);
2100}
2101
Tom Stellard94c21bc2016-11-01 16:31:48 +00002102SDValue AMDGPUTargetLowering::LowerFP_TO_FP16(SDValue Op, SelectionDAG &DAG) const {
2103
2104 if (getTargetMachine().Options.UnsafeFPMath) {
2105 // There is a generic expand for FP_TO_FP16 with unsafe fast math.
2106 return SDValue();
2107 }
2108
2109 SDLoc DL(Op);
2110 SDValue N0 = Op.getOperand(0);
Tom Stellard9677b602016-11-01 17:20:03 +00002111 assert (N0.getSimpleValueType() == MVT::f64);
Tom Stellard94c21bc2016-11-01 16:31:48 +00002112
2113 // f64 -> f16 conversion using round-to-nearest-even rounding mode.
2114 const unsigned ExpMask = 0x7ff;
2115 const unsigned ExpBiasf64 = 1023;
2116 const unsigned ExpBiasf16 = 15;
2117 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2118 SDValue One = DAG.getConstant(1, DL, MVT::i32);
2119 SDValue U = DAG.getNode(ISD::BITCAST, DL, MVT::i64, N0);
2120 SDValue UH = DAG.getNode(ISD::SRL, DL, MVT::i64, U,
2121 DAG.getConstant(32, DL, MVT::i64));
2122 UH = DAG.getZExtOrTrunc(UH, DL, MVT::i32);
2123 U = DAG.getZExtOrTrunc(U, DL, MVT::i32);
2124 SDValue E = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2125 DAG.getConstant(20, DL, MVT::i64));
2126 E = DAG.getNode(ISD::AND, DL, MVT::i32, E,
2127 DAG.getConstant(ExpMask, DL, MVT::i32));
2128 // Subtract the fp64 exponent bias (1023) to get the real exponent and
2129 // add the f16 bias (15) to get the biased exponent for the f16 format.
2130 E = DAG.getNode(ISD::ADD, DL, MVT::i32, E,
2131 DAG.getConstant(-ExpBiasf64 + ExpBiasf16, DL, MVT::i32));
2132
2133 SDValue M = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2134 DAG.getConstant(8, DL, MVT::i32));
2135 M = DAG.getNode(ISD::AND, DL, MVT::i32, M,
2136 DAG.getConstant(0xffe, DL, MVT::i32));
2137
2138 SDValue MaskedSig = DAG.getNode(ISD::AND, DL, MVT::i32, UH,
2139 DAG.getConstant(0x1ff, DL, MVT::i32));
2140 MaskedSig = DAG.getNode(ISD::OR, DL, MVT::i32, MaskedSig, U);
2141
2142 SDValue Lo40Set = DAG.getSelectCC(DL, MaskedSig, Zero, Zero, One, ISD::SETEQ);
2143 M = DAG.getNode(ISD::OR, DL, MVT::i32, M, Lo40Set);
2144
2145 // (M != 0 ? 0x0200 : 0) | 0x7c00;
2146 SDValue I = DAG.getNode(ISD::OR, DL, MVT::i32,
2147 DAG.getSelectCC(DL, M, Zero, DAG.getConstant(0x0200, DL, MVT::i32),
2148 Zero, ISD::SETNE), DAG.getConstant(0x7c00, DL, MVT::i32));
2149
2150 // N = M | (E << 12);
2151 SDValue N = DAG.getNode(ISD::OR, DL, MVT::i32, M,
2152 DAG.getNode(ISD::SHL, DL, MVT::i32, E,
2153 DAG.getConstant(12, DL, MVT::i32)));
2154
2155 // B = clamp(1-E, 0, 13);
2156 SDValue OneSubExp = DAG.getNode(ISD::SUB, DL, MVT::i32,
2157 One, E);
2158 SDValue B = DAG.getNode(ISD::SMAX, DL, MVT::i32, OneSubExp, Zero);
2159 B = DAG.getNode(ISD::SMIN, DL, MVT::i32, B,
2160 DAG.getConstant(13, DL, MVT::i32));
2161
2162 SDValue SigSetHigh = DAG.getNode(ISD::OR, DL, MVT::i32, M,
2163 DAG.getConstant(0x1000, DL, MVT::i32));
2164
2165 SDValue D = DAG.getNode(ISD::SRL, DL, MVT::i32, SigSetHigh, B);
2166 SDValue D0 = DAG.getNode(ISD::SHL, DL, MVT::i32, D, B);
2167 SDValue D1 = DAG.getSelectCC(DL, D0, SigSetHigh, One, Zero, ISD::SETNE);
2168 D = DAG.getNode(ISD::OR, DL, MVT::i32, D, D1);
2169
2170 SDValue V = DAG.getSelectCC(DL, E, One, D, N, ISD::SETLT);
2171 SDValue VLow3 = DAG.getNode(ISD::AND, DL, MVT::i32, V,
2172 DAG.getConstant(0x7, DL, MVT::i32));
2173 V = DAG.getNode(ISD::SRL, DL, MVT::i32, V,
2174 DAG.getConstant(2, DL, MVT::i32));
2175 SDValue V0 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(3, DL, MVT::i32),
2176 One, Zero, ISD::SETEQ);
2177 SDValue V1 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(5, DL, MVT::i32),
2178 One, Zero, ISD::SETGT);
2179 V1 = DAG.getNode(ISD::OR, DL, MVT::i32, V0, V1);
2180 V = DAG.getNode(ISD::ADD, DL, MVT::i32, V, V1);
2181
2182 V = DAG.getSelectCC(DL, E, DAG.getConstant(30, DL, MVT::i32),
2183 DAG.getConstant(0x7c00, DL, MVT::i32), V, ISD::SETGT);
2184 V = DAG.getSelectCC(DL, E, DAG.getConstant(1039, DL, MVT::i32),
2185 I, V, ISD::SETEQ);
2186
2187 // Extract the sign bit.
2188 SDValue Sign = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2189 DAG.getConstant(16, DL, MVT::i32));
2190 Sign = DAG.getNode(ISD::AND, DL, MVT::i32, Sign,
2191 DAG.getConstant(0x8000, DL, MVT::i32));
2192
2193 V = DAG.getNode(ISD::OR, DL, MVT::i32, Sign, V);
2194 return DAG.getZExtOrTrunc(V, DL, Op.getValueType());
2195}
2196
Matt Arsenaultc9961752014-10-03 23:54:56 +00002197SDValue AMDGPUTargetLowering::LowerFP_TO_SINT(SDValue Op,
2198 SelectionDAG &DAG) const {
2199 SDValue Src = Op.getOperand(0);
2200
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002201 // TODO: Factor out code common with LowerFP_TO_UINT.
2202
2203 EVT SrcVT = Src.getValueType();
2204 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) {
2205 SDLoc DL(Op);
2206
2207 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src);
2208 SDValue FpToInt32 =
2209 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend);
2210
2211 return FpToInt32;
2212 }
2213
Matt Arsenaultc9961752014-10-03 23:54:56 +00002214 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64)
2215 return LowerFP64_TO_INT(Op, DAG, true);
2216
2217 return SDValue();
2218}
2219
2220SDValue AMDGPUTargetLowering::LowerFP_TO_UINT(SDValue Op,
2221 SelectionDAG &DAG) const {
2222 SDValue Src = Op.getOperand(0);
2223
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002224 // TODO: Factor out code common with LowerFP_TO_SINT.
2225
2226 EVT SrcVT = Src.getValueType();
2227 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) {
2228 SDLoc DL(Op);
2229
2230 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src);
2231 SDValue FpToInt32 =
2232 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend);
2233
2234 return FpToInt32;
2235 }
2236
Matt Arsenaultc9961752014-10-03 23:54:56 +00002237 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64)
2238 return LowerFP64_TO_INT(Op, DAG, false);
2239
2240 return SDValue();
2241}
2242
Matt Arsenaultfae02982014-03-17 18:58:11 +00002243SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
2244 SelectionDAG &DAG) const {
2245 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
2246 MVT VT = Op.getSimpleValueType();
2247 MVT ScalarVT = VT.getScalarType();
2248
Matt Arsenaultedc7dcb2016-07-28 00:32:05 +00002249 assert(VT.isVector());
Matt Arsenaultfae02982014-03-17 18:58:11 +00002250
2251 SDValue Src = Op.getOperand(0);
Matt Arsenault5dbd5db2014-04-22 03:49:30 +00002252 SDLoc DL(Op);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002253
Matt Arsenault5dbd5db2014-04-22 03:49:30 +00002254 // TODO: Don't scalarize on Evergreen?
2255 unsigned NElts = VT.getVectorNumElements();
2256 SmallVector<SDValue, 8> Args;
2257 DAG.ExtractVectorElements(Src, Args, 0, NElts);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002258
Matt Arsenault5dbd5db2014-04-22 03:49:30 +00002259 SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType());
2260 for (unsigned I = 0; I < NElts; ++I)
2261 Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002262
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002263 return DAG.getBuildVector(VT, DL, Args);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002264}
2265
Tom Stellard75aadc22012-12-11 21:25:42 +00002266//===----------------------------------------------------------------------===//
Tom Stellard50122a52014-04-07 19:45:41 +00002267// Custom DAG optimizations
2268//===----------------------------------------------------------------------===//
2269
2270static bool isU24(SDValue Op, SelectionDAG &DAG) {
2271 APInt KnownZero, KnownOne;
2272 EVT VT = Op.getValueType();
Jay Foada0653a32014-05-14 21:14:37 +00002273 DAG.computeKnownBits(Op, KnownZero, KnownOne);
Tom Stellard50122a52014-04-07 19:45:41 +00002274
2275 return (VT.getSizeInBits() - KnownZero.countLeadingOnes()) <= 24;
2276}
2277
2278static bool isI24(SDValue Op, SelectionDAG &DAG) {
2279 EVT VT = Op.getValueType();
2280
2281 // In order for this to be a signed 24-bit value, bit 23, must
2282 // be a sign bit.
2283 return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated
2284 // as unsigned 24-bit values.
2285 (VT.getSizeInBits() - DAG.ComputeNumSignBits(Op)) < 24;
2286}
2287
Tom Stellard09c2bd62016-10-14 19:14:29 +00002288static bool simplifyI24(SDNode *Node24, unsigned OpIdx,
2289 TargetLowering::DAGCombinerInfo &DCI) {
Tom Stellard50122a52014-04-07 19:45:41 +00002290
2291 SelectionDAG &DAG = DCI.DAG;
Tom Stellard09c2bd62016-10-14 19:14:29 +00002292 SDValue Op = Node24->getOperand(OpIdx);
Tom Stellard50122a52014-04-07 19:45:41 +00002293 EVT VT = Op.getValueType();
2294
2295 APInt Demanded = APInt::getLowBitsSet(VT.getSizeInBits(), 24);
2296 APInt KnownZero, KnownOne;
2297 TargetLowering::TargetLoweringOpt TLO(DAG, true, true);
Tom Stellard09c2bd62016-10-14 19:14:29 +00002298 if (TLO.SimplifyDemandedBits(Node24, OpIdx, Demanded, DCI))
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002299 return true;
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002300
2301 return false;
Tom Stellard50122a52014-04-07 19:45:41 +00002302}
2303
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002304template <typename IntTy>
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002305static SDValue constantFoldBFE(SelectionDAG &DAG, IntTy Src0, uint32_t Offset,
2306 uint32_t Width, const SDLoc &DL) {
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002307 if (Width + Offset < 32) {
Matt Arsenault46cbc432014-09-19 00:42:06 +00002308 uint32_t Shl = static_cast<uint32_t>(Src0) << (32 - Offset - Width);
2309 IntTy Result = static_cast<IntTy>(Shl) >> (32 - Width);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002310 return DAG.getConstant(Result, DL, MVT::i32);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002311 }
2312
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002313 return DAG.getConstant(Src0 >> Offset, DL, MVT::i32);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002314}
2315
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002316static bool hasVolatileUser(SDNode *Val) {
2317 for (SDNode *U : Val->uses()) {
2318 if (MemSDNode *M = dyn_cast<MemSDNode>(U)) {
2319 if (M->isVolatile())
2320 return true;
2321 }
2322 }
2323
2324 return false;
2325}
2326
Matt Arsenault8af47a02016-07-01 22:55:55 +00002327bool AMDGPUTargetLowering::shouldCombineMemoryType(EVT VT) const {
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002328 // i32 vectors are the canonical memory type.
2329 if (VT.getScalarType() == MVT::i32 || isTypeLegal(VT))
2330 return false;
2331
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002332 if (!VT.isByteSized())
2333 return false;
2334
2335 unsigned Size = VT.getStoreSize();
2336
2337 if ((Size == 1 || Size == 2 || Size == 4) && !VT.isVector())
2338 return false;
2339
2340 if (Size == 3 || (Size > 4 && (Size % 4 != 0)))
2341 return false;
2342
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002343 return true;
2344}
2345
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002346// Replace load of an illegal type with a store of a bitcast to a friendlier
2347// type.
2348SDValue AMDGPUTargetLowering::performLoadCombine(SDNode *N,
2349 DAGCombinerInfo &DCI) const {
2350 if (!DCI.isBeforeLegalize())
2351 return SDValue();
2352
2353 LoadSDNode *LN = cast<LoadSDNode>(N);
2354 if (LN->isVolatile() || !ISD::isNormalLoad(LN) || hasVolatileUser(LN))
2355 return SDValue();
2356
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002357 SDLoc SL(N);
2358 SelectionDAG &DAG = DCI.DAG;
2359 EVT VT = LN->getMemoryVT();
Matt Arsenault8af47a02016-07-01 22:55:55 +00002360
2361 unsigned Size = VT.getStoreSize();
2362 unsigned Align = LN->getAlignment();
2363 if (Align < Size && isTypeLegal(VT)) {
2364 bool IsFast;
2365 unsigned AS = LN->getAddressSpace();
2366
2367 // Expand unaligned loads earlier than legalization. Due to visitation order
2368 // problems during legalization, the emitted instructions to pack and unpack
2369 // the bytes again are not eliminated in the case of an unaligned copy.
2370 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) {
Matt Arsenaultb50eb8d2016-08-31 21:52:27 +00002371 if (VT.isVector())
2372 return scalarizeVectorLoad(LN, DAG);
2373
Matt Arsenault8af47a02016-07-01 22:55:55 +00002374 SDValue Ops[2];
2375 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(LN, DAG);
2376 return DAG.getMergeValues(Ops, SDLoc(N));
2377 }
2378
2379 if (!IsFast)
2380 return SDValue();
2381 }
2382
2383 if (!shouldCombineMemoryType(VT))
2384 return SDValue();
2385
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002386 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
2387
2388 SDValue NewLoad
2389 = DAG.getLoad(NewVT, SL, LN->getChain(),
2390 LN->getBasePtr(), LN->getMemOperand());
2391
2392 SDValue BC = DAG.getNode(ISD::BITCAST, SL, VT, NewLoad);
2393 DCI.CombineTo(N, BC, NewLoad.getValue(1));
2394 return SDValue(N, 0);
2395}
2396
2397// Replace store of an illegal type with a store of a bitcast to a friendlier
2398// type.
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002399SDValue AMDGPUTargetLowering::performStoreCombine(SDNode *N,
2400 DAGCombinerInfo &DCI) const {
2401 if (!DCI.isBeforeLegalize())
2402 return SDValue();
2403
2404 StoreSDNode *SN = cast<StoreSDNode>(N);
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002405 if (SN->isVolatile() || !ISD::isNormalStore(SN))
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002406 return SDValue();
2407
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002408 EVT VT = SN->getMemoryVT();
Matt Arsenault8af47a02016-07-01 22:55:55 +00002409 unsigned Size = VT.getStoreSize();
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002410
2411 SDLoc SL(N);
2412 SelectionDAG &DAG = DCI.DAG;
Matt Arsenault8af47a02016-07-01 22:55:55 +00002413 unsigned Align = SN->getAlignment();
2414 if (Align < Size && isTypeLegal(VT)) {
2415 bool IsFast;
2416 unsigned AS = SN->getAddressSpace();
2417
2418 // Expand unaligned stores earlier than legalization. Due to visitation
2419 // order problems during legalization, the emitted instructions to pack and
2420 // unpack the bytes again are not eliminated in the case of an unaligned
2421 // copy.
Matt Arsenaultb50eb8d2016-08-31 21:52:27 +00002422 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) {
2423 if (VT.isVector())
2424 return scalarizeVectorStore(SN, DAG);
2425
Matt Arsenault8af47a02016-07-01 22:55:55 +00002426 return expandUnalignedStore(SN, DAG);
Matt Arsenaultb50eb8d2016-08-31 21:52:27 +00002427 }
Matt Arsenault8af47a02016-07-01 22:55:55 +00002428
2429 if (!IsFast)
2430 return SDValue();
2431 }
2432
2433 if (!shouldCombineMemoryType(VT))
2434 return SDValue();
2435
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002436 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
Matt Arsenault8af47a02016-07-01 22:55:55 +00002437 SDValue Val = SN->getValue();
2438
2439 //DCI.AddToWorklist(Val.getNode());
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002440
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002441 bool OtherUses = !Val.hasOneUse();
2442 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NewVT, Val);
2443 if (OtherUses) {
2444 SDValue CastBack = DAG.getNode(ISD::BITCAST, SL, VT, CastVal);
2445 DAG.ReplaceAllUsesOfValueWith(Val, CastBack);
2446 }
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002447
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002448 return DAG.getStore(SN->getChain(), SL, CastVal,
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002449 SN->getBasePtr(), SN->getMemOperand());
2450}
2451
Matt Arsenault2fdf2a12017-02-21 23:35:48 +00002452SDValue AMDGPUTargetLowering::performClampCombine(SDNode *N,
2453 DAGCombinerInfo &DCI) const {
2454 ConstantFPSDNode *CSrc = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
2455 if (!CSrc)
2456 return SDValue();
2457
2458 const APFloat &F = CSrc->getValueAPF();
2459 APFloat Zero = APFloat::getZero(F.getSemantics());
2460 APFloat::cmpResult Cmp0 = F.compare(Zero);
2461 if (Cmp0 == APFloat::cmpLessThan ||
2462 (Cmp0 == APFloat::cmpUnordered && Subtarget->enableDX10Clamp())) {
2463 return DCI.DAG.getConstantFP(Zero, SDLoc(N), N->getValueType(0));
2464 }
2465
2466 APFloat One(F.getSemantics(), "1.0");
2467 APFloat::cmpResult Cmp1 = F.compare(One);
2468 if (Cmp1 == APFloat::cmpGreaterThan)
2469 return DCI.DAG.getConstantFP(One, SDLoc(N), N->getValueType(0));
2470
2471 return SDValue(CSrc, 0);
2472}
2473
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002474/// Split the 64-bit value \p LHS into two 32-bit components, and perform the
2475/// binary operation \p Opc to it with the corresponding constant operands.
2476SDValue AMDGPUTargetLowering::splitBinaryBitConstantOpImpl(
2477 DAGCombinerInfo &DCI, const SDLoc &SL,
2478 unsigned Opc, SDValue LHS,
2479 uint32_t ValLo, uint32_t ValHi) const {
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002480 SelectionDAG &DAG = DCI.DAG;
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002481 SDValue Lo, Hi;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002482 std::tie(Lo, Hi) = split64BitValue(LHS, DAG);
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002483
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002484 SDValue LoRHS = DAG.getConstant(ValLo, SL, MVT::i32);
2485 SDValue HiRHS = DAG.getConstant(ValHi, SL, MVT::i32);
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002486
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002487 SDValue LoAnd = DAG.getNode(Opc, SL, MVT::i32, Lo, LoRHS);
2488 SDValue HiAnd = DAG.getNode(Opc, SL, MVT::i32, Hi, HiRHS);
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002489
Matt Arsenaultefa3fe12016-04-22 22:48:38 +00002490 // Re-visit the ands. It's possible we eliminated one of them and it could
2491 // simplify the vector.
2492 DCI.AddToWorklist(Lo.getNode());
2493 DCI.AddToWorklist(Hi.getNode());
2494
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002495 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {LoAnd, HiAnd});
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002496 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2497}
2498
Matt Arsenault24692112015-07-14 18:20:33 +00002499SDValue AMDGPUTargetLowering::performShlCombine(SDNode *N,
2500 DAGCombinerInfo &DCI) const {
2501 if (N->getValueType(0) != MVT::i64)
2502 return SDValue();
2503
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002504 // i64 (shl x, C) -> (build_pair 0, (shl x, C -32))
Matt Arsenault24692112015-07-14 18:20:33 +00002505
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002506 // On some subtargets, 64-bit shift is a quarter rate instruction. In the
2507 // common case, splitting this into a move and a 32-bit shift is faster and
2508 // the same code size.
Matt Arsenault24692112015-07-14 18:20:33 +00002509 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002510 if (!RHS)
2511 return SDValue();
2512
2513 unsigned RHSVal = RHS->getZExtValue();
2514 if (RHSVal < 32)
Matt Arsenault24692112015-07-14 18:20:33 +00002515 return SDValue();
2516
2517 SDValue LHS = N->getOperand(0);
2518
2519 SDLoc SL(N);
2520 SelectionDAG &DAG = DCI.DAG;
2521
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002522 SDValue ShiftAmt = DAG.getConstant(RHSVal - 32, SL, MVT::i32);
2523
Matt Arsenault24692112015-07-14 18:20:33 +00002524 SDValue Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LHS);
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002525 SDValue NewShift = DAG.getNode(ISD::SHL, SL, MVT::i32, Lo, ShiftAmt);
Matt Arsenault24692112015-07-14 18:20:33 +00002526
2527 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
Matt Arsenault80edab92016-01-18 21:43:36 +00002528
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002529 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {Zero, NewShift});
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002530 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
Matt Arsenault24692112015-07-14 18:20:33 +00002531}
2532
Matt Arsenault33e3ece2016-01-18 22:09:04 +00002533SDValue AMDGPUTargetLowering::performSraCombine(SDNode *N,
2534 DAGCombinerInfo &DCI) const {
2535 if (N->getValueType(0) != MVT::i64)
2536 return SDValue();
2537
2538 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2539 if (!RHS)
2540 return SDValue();
2541
2542 SelectionDAG &DAG = DCI.DAG;
2543 SDLoc SL(N);
2544 unsigned RHSVal = RHS->getZExtValue();
2545
2546 // (sra i64:x, 32) -> build_pair x, (sra hi_32(x), 31)
2547 if (RHSVal == 32) {
2548 SDValue Hi = getHiHalf64(N->getOperand(0), DAG);
2549 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi,
2550 DAG.getConstant(31, SL, MVT::i32));
2551
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002552 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {Hi, NewShift});
Matt Arsenault33e3ece2016-01-18 22:09:04 +00002553 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec);
2554 }
2555
2556 // (sra i64:x, 63) -> build_pair (sra hi_32(x), 31), (sra hi_32(x), 31)
2557 if (RHSVal == 63) {
2558 SDValue Hi = getHiHalf64(N->getOperand(0), DAG);
2559 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi,
2560 DAG.getConstant(31, SL, MVT::i32));
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002561 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, NewShift});
Matt Arsenault33e3ece2016-01-18 22:09:04 +00002562 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec);
2563 }
2564
2565 return SDValue();
2566}
2567
Matt Arsenault80edab92016-01-18 21:43:36 +00002568SDValue AMDGPUTargetLowering::performSrlCombine(SDNode *N,
2569 DAGCombinerInfo &DCI) const {
2570 if (N->getValueType(0) != MVT::i64)
2571 return SDValue();
2572
2573 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2574 if (!RHS)
2575 return SDValue();
2576
2577 unsigned ShiftAmt = RHS->getZExtValue();
2578 if (ShiftAmt < 32)
2579 return SDValue();
2580
2581 // srl i64:x, C for C >= 32
2582 // =>
2583 // build_pair (srl hi_32(x), C - 32), 0
2584
2585 SelectionDAG &DAG = DCI.DAG;
2586 SDLoc SL(N);
2587
2588 SDValue One = DAG.getConstant(1, SL, MVT::i32);
2589 SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
2590
2591 SDValue VecOp = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, N->getOperand(0));
2592 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32,
2593 VecOp, One);
2594
2595 SDValue NewConst = DAG.getConstant(ShiftAmt - 32, SL, MVT::i32);
2596 SDValue NewShift = DAG.getNode(ISD::SRL, SL, MVT::i32, Hi, NewConst);
2597
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002598 SDValue BuildPair = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, Zero});
Matt Arsenault80edab92016-01-18 21:43:36 +00002599
2600 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildPair);
2601}
2602
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002603// We need to specifically handle i64 mul here to avoid unnecessary conversion
2604// instructions. If we only match on the legalized i64 mul expansion,
2605// SimplifyDemandedBits will be unable to remove them because there will be
2606// multiple uses due to the separate mul + mulh[su].
2607static SDValue getMul24(SelectionDAG &DAG, const SDLoc &SL,
2608 SDValue N0, SDValue N1, unsigned Size, bool Signed) {
2609 if (Size <= 32) {
2610 unsigned MulOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24;
2611 return DAG.getNode(MulOpc, SL, MVT::i32, N0, N1);
2612 }
2613
2614 // Because we want to eliminate extension instructions before the
2615 // operation, we need to create a single user here (i.e. not the separate
2616 // mul_lo + mul_hi) so that SimplifyDemandedBits will deal with it.
2617
2618 unsigned MulOpc = Signed ? AMDGPUISD::MUL_LOHI_I24 : AMDGPUISD::MUL_LOHI_U24;
2619
2620 SDValue Mul = DAG.getNode(MulOpc, SL,
2621 DAG.getVTList(MVT::i32, MVT::i32), N0, N1);
2622
2623 return DAG.getNode(ISD::BUILD_PAIR, SL, MVT::i64,
2624 Mul.getValue(0), Mul.getValue(1));
2625}
2626
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002627SDValue AMDGPUTargetLowering::performMulCombine(SDNode *N,
2628 DAGCombinerInfo &DCI) const {
2629 EVT VT = N->getValueType(0);
2630
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002631 unsigned Size = VT.getSizeInBits();
2632 if (VT.isVector() || Size > 64)
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002633 return SDValue();
2634
Tom Stellard115a6152016-11-10 16:02:37 +00002635 // There are i16 integer mul/mad.
2636 if (Subtarget->has16BitInsts() && VT.getScalarType().bitsLE(MVT::i16))
2637 return SDValue();
2638
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002639 SelectionDAG &DAG = DCI.DAG;
2640 SDLoc DL(N);
2641
2642 SDValue N0 = N->getOperand(0);
2643 SDValue N1 = N->getOperand(1);
2644 SDValue Mul;
2645
2646 if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) {
2647 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
2648 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002649 Mul = getMul24(DAG, DL, N0, N1, Size, false);
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002650 } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) {
2651 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
2652 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002653 Mul = getMul24(DAG, DL, N0, N1, Size, true);
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002654 } else {
2655 return SDValue();
2656 }
2657
2658 // We need to use sext even for MUL_U24, because MUL_U24 is used
2659 // for signed multiply of 8 and 16-bit types.
2660 return DAG.getSExtOrTrunc(Mul, DL, VT);
2661}
2662
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002663SDValue AMDGPUTargetLowering::performMulhsCombine(SDNode *N,
2664 DAGCombinerInfo &DCI) const {
2665 EVT VT = N->getValueType(0);
2666
2667 if (!Subtarget->hasMulI24() || VT.isVector())
2668 return SDValue();
2669
2670 SelectionDAG &DAG = DCI.DAG;
2671 SDLoc DL(N);
2672
2673 SDValue N0 = N->getOperand(0);
2674 SDValue N1 = N->getOperand(1);
2675
2676 if (!isI24(N0, DAG) || !isI24(N1, DAG))
2677 return SDValue();
2678
2679 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
2680 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
2681
2682 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_I24, DL, MVT::i32, N0, N1);
2683 DCI.AddToWorklist(Mulhi.getNode());
2684 return DAG.getSExtOrTrunc(Mulhi, DL, VT);
2685}
2686
2687SDValue AMDGPUTargetLowering::performMulhuCombine(SDNode *N,
2688 DAGCombinerInfo &DCI) const {
2689 EVT VT = N->getValueType(0);
2690
2691 if (!Subtarget->hasMulU24() || VT.isVector() || VT.getSizeInBits() > 32)
2692 return SDValue();
2693
2694 SelectionDAG &DAG = DCI.DAG;
2695 SDLoc DL(N);
2696
2697 SDValue N0 = N->getOperand(0);
2698 SDValue N1 = N->getOperand(1);
2699
2700 if (!isU24(N0, DAG) || !isU24(N1, DAG))
2701 return SDValue();
2702
2703 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
2704 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
2705
2706 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_U24, DL, MVT::i32, N0, N1);
2707 DCI.AddToWorklist(Mulhi.getNode());
2708 return DAG.getZExtOrTrunc(Mulhi, DL, VT);
2709}
2710
2711SDValue AMDGPUTargetLowering::performMulLoHi24Combine(
2712 SDNode *N, DAGCombinerInfo &DCI) const {
2713 SelectionDAG &DAG = DCI.DAG;
2714
Tom Stellard09c2bd62016-10-14 19:14:29 +00002715 // Simplify demanded bits before splitting into multiple users.
2716 if (simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI))
2717 return SDValue();
2718
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002719 SDValue N0 = N->getOperand(0);
2720 SDValue N1 = N->getOperand(1);
2721
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002722 bool Signed = (N->getOpcode() == AMDGPUISD::MUL_LOHI_I24);
2723
2724 unsigned MulLoOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24;
2725 unsigned MulHiOpc = Signed ? AMDGPUISD::MULHI_I24 : AMDGPUISD::MULHI_U24;
2726
2727 SDLoc SL(N);
2728
2729 SDValue MulLo = DAG.getNode(MulLoOpc, SL, MVT::i32, N0, N1);
2730 SDValue MulHi = DAG.getNode(MulHiOpc, SL, MVT::i32, N0, N1);
2731 return DAG.getMergeValues({ MulLo, MulHi }, SL);
2732}
2733
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002734static bool isNegativeOne(SDValue Val) {
2735 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val))
2736 return C->isAllOnesValue();
2737 return false;
2738}
2739
2740static bool isCtlzOpc(unsigned Opc) {
2741 return Opc == ISD::CTLZ || Opc == ISD::CTLZ_ZERO_UNDEF;
2742}
2743
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002744SDValue AMDGPUTargetLowering::getFFBH_U32(SelectionDAG &DAG,
2745 SDValue Op,
2746 const SDLoc &DL) const {
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002747 EVT VT = Op.getValueType();
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002748 EVT LegalVT = getTypeToTransformTo(*DAG.getContext(), VT);
2749 if (LegalVT != MVT::i32 && (Subtarget->has16BitInsts() &&
2750 LegalVT != MVT::i16))
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002751 return SDValue();
2752
2753 if (VT != MVT::i32)
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002754 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Op);
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002755
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002756 SDValue FFBH = DAG.getNode(AMDGPUISD::FFBH_U32, DL, MVT::i32, Op);
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002757 if (VT != MVT::i32)
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002758 FFBH = DAG.getNode(ISD::TRUNCATE, DL, VT, FFBH);
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002759
2760 return FFBH;
2761}
2762
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002763// The native instructions return -1 on 0 input. Optimize out a select that
2764// produces -1 on 0.
2765//
2766// TODO: If zero is not undef, we could also do this if the output is compared
2767// against the bitwidth.
2768//
2769// TODO: Should probably combine against FFBH_U32 instead of ctlz directly.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002770SDValue AMDGPUTargetLowering::performCtlzCombine(const SDLoc &SL, SDValue Cond,
2771 SDValue LHS, SDValue RHS,
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002772 DAGCombinerInfo &DCI) const {
2773 ConstantSDNode *CmpRhs = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2774 if (!CmpRhs || !CmpRhs->isNullValue())
2775 return SDValue();
2776
2777 SelectionDAG &DAG = DCI.DAG;
2778 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
2779 SDValue CmpLHS = Cond.getOperand(0);
2780
2781 // select (setcc x, 0, eq), -1, (ctlz_zero_undef x) -> ffbh_u32 x
2782 if (CCOpcode == ISD::SETEQ &&
2783 isCtlzOpc(RHS.getOpcode()) &&
2784 RHS.getOperand(0) == CmpLHS &&
2785 isNegativeOne(LHS)) {
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002786 return getFFBH_U32(DAG, CmpLHS, SL);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002787 }
2788
2789 // select (setcc x, 0, ne), (ctlz_zero_undef x), -1 -> ffbh_u32 x
2790 if (CCOpcode == ISD::SETNE &&
2791 isCtlzOpc(LHS.getOpcode()) &&
2792 LHS.getOperand(0) == CmpLHS &&
2793 isNegativeOne(RHS)) {
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002794 return getFFBH_U32(DAG, CmpLHS, SL);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002795 }
2796
2797 return SDValue();
2798}
2799
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002800static SDValue distributeOpThroughSelect(TargetLowering::DAGCombinerInfo &DCI,
2801 unsigned Op,
2802 const SDLoc &SL,
2803 SDValue Cond,
2804 SDValue N1,
2805 SDValue N2) {
2806 SelectionDAG &DAG = DCI.DAG;
2807 EVT VT = N1.getValueType();
2808
2809 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT, Cond,
2810 N1.getOperand(0), N2.getOperand(0));
2811 DCI.AddToWorklist(NewSelect.getNode());
2812 return DAG.getNode(Op, SL, VT, NewSelect);
2813}
2814
2815// Pull a free FP operation out of a select so it may fold into uses.
2816//
2817// select c, (fneg x), (fneg y) -> fneg (select c, x, y)
2818// select c, (fneg x), k -> fneg (select c, x, (fneg k))
2819//
2820// select c, (fabs x), (fabs y) -> fabs (select c, x, y)
2821// select c, (fabs x), +k -> fabs (select c, x, k)
2822static SDValue foldFreeOpFromSelect(TargetLowering::DAGCombinerInfo &DCI,
2823 SDValue N) {
2824 SelectionDAG &DAG = DCI.DAG;
2825 SDValue Cond = N.getOperand(0);
2826 SDValue LHS = N.getOperand(1);
2827 SDValue RHS = N.getOperand(2);
2828
2829 EVT VT = N.getValueType();
2830 if ((LHS.getOpcode() == ISD::FABS && RHS.getOpcode() == ISD::FABS) ||
2831 (LHS.getOpcode() == ISD::FNEG && RHS.getOpcode() == ISD::FNEG)) {
2832 return distributeOpThroughSelect(DCI, LHS.getOpcode(),
2833 SDLoc(N), Cond, LHS, RHS);
2834 }
2835
2836 bool Inv = false;
2837 if (RHS.getOpcode() == ISD::FABS || RHS.getOpcode() == ISD::FNEG) {
2838 std::swap(LHS, RHS);
2839 Inv = true;
2840 }
2841
2842 // TODO: Support vector constants.
2843 ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
2844 if ((LHS.getOpcode() == ISD::FNEG || LHS.getOpcode() == ISD::FABS) && CRHS) {
2845 SDLoc SL(N);
2846 // If one side is an fneg/fabs and the other is a constant, we can push the
2847 // fneg/fabs down. If it's an fabs, the constant needs to be non-negative.
2848 SDValue NewLHS = LHS.getOperand(0);
2849 SDValue NewRHS = RHS;
2850
Matt Arsenault45337df2017-01-12 18:58:15 +00002851 // Careful: if the neg can be folded up, don't try to pull it back down.
2852 bool ShouldFoldNeg = true;
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002853
Matt Arsenault45337df2017-01-12 18:58:15 +00002854 if (NewLHS.hasOneUse()) {
2855 unsigned Opc = NewLHS.getOpcode();
2856 if (LHS.getOpcode() == ISD::FNEG && fnegFoldsIntoOp(Opc))
2857 ShouldFoldNeg = false;
2858 if (LHS.getOpcode() == ISD::FABS && Opc == ISD::FMUL)
2859 ShouldFoldNeg = false;
2860 }
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002861
Matt Arsenault45337df2017-01-12 18:58:15 +00002862 if (ShouldFoldNeg) {
2863 if (LHS.getOpcode() == ISD::FNEG)
2864 NewRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2865 else if (CRHS->isNegative())
2866 return SDValue();
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002867
Matt Arsenault45337df2017-01-12 18:58:15 +00002868 if (Inv)
2869 std::swap(NewLHS, NewRHS);
2870
2871 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT,
2872 Cond, NewLHS, NewRHS);
2873 DCI.AddToWorklist(NewSelect.getNode());
2874 return DAG.getNode(LHS.getOpcode(), SL, VT, NewSelect);
2875 }
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002876 }
2877
2878 return SDValue();
2879}
2880
2881
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002882SDValue AMDGPUTargetLowering::performSelectCombine(SDNode *N,
2883 DAGCombinerInfo &DCI) const {
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002884 if (SDValue Folded = foldFreeOpFromSelect(DCI, SDValue(N, 0)))
2885 return Folded;
2886
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002887 SDValue Cond = N->getOperand(0);
2888 if (Cond.getOpcode() != ISD::SETCC)
2889 return SDValue();
2890
2891 EVT VT = N->getValueType(0);
2892 SDValue LHS = Cond.getOperand(0);
2893 SDValue RHS = Cond.getOperand(1);
2894 SDValue CC = Cond.getOperand(2);
2895
2896 SDValue True = N->getOperand(1);
2897 SDValue False = N->getOperand(2);
2898
Matt Arsenault0b26e472016-12-22 21:40:08 +00002899 if (Cond.hasOneUse()) { // TODO: Look for multiple select uses.
2900 SelectionDAG &DAG = DCI.DAG;
2901 if ((DAG.isConstantValueOfAnyType(True) ||
2902 DAG.isConstantValueOfAnyType(True)) &&
2903 (!DAG.isConstantValueOfAnyType(False) &&
2904 !DAG.isConstantValueOfAnyType(False))) {
2905 // Swap cmp + select pair to move constant to false input.
2906 // This will allow using VOPC cndmasks more often.
2907 // select (setcc x, y), k, x -> select (setcc y, x) x, x
2908
2909 SDLoc SL(N);
2910 ISD::CondCode NewCC = getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2911 LHS.getValueType().isInteger());
2912
2913 SDValue NewCond = DAG.getSetCC(SL, Cond.getValueType(), LHS, RHS, NewCC);
2914 return DAG.getNode(ISD::SELECT, SL, VT, NewCond, False, True);
2915 }
Matt Arsenault0b26e472016-12-22 21:40:08 +00002916
Matt Arsenaultda7a6562017-02-01 00:42:40 +00002917 if (VT == MVT::f32 && Subtarget->hasFminFmaxLegacy()) {
2918 SDValue MinMax
2919 = combineFMinMaxLegacy(SDLoc(N), VT, LHS, RHS, True, False, CC, DCI);
2920 // Revisit this node so we can catch min3/max3/med3 patterns.
2921 //DCI.AddToWorklist(MinMax.getNode());
2922 return MinMax;
2923 }
Matt Arsenault5b39b342016-01-28 20:53:48 +00002924 }
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002925
2926 // There's no reason to not do this if the condition has other uses.
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002927 return performCtlzCombine(SDLoc(N), Cond, True, False, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002928}
2929
Matt Arsenault2511c032017-02-03 00:23:15 +00002930static bool isConstantFPZero(SDValue N) {
2931 if (const ConstantFPSDNode *C = isConstOrConstSplatFP(N))
2932 return C->isZero() && !C->isNegative();
2933 return false;
2934}
2935
Matt Arsenaulte1b59532017-02-03 00:51:50 +00002936static unsigned inverseMinMax(unsigned Opc) {
2937 switch (Opc) {
2938 case ISD::FMAXNUM:
2939 return ISD::FMINNUM;
2940 case ISD::FMINNUM:
2941 return ISD::FMAXNUM;
2942 case AMDGPUISD::FMAX_LEGACY:
2943 return AMDGPUISD::FMIN_LEGACY;
2944 case AMDGPUISD::FMIN_LEGACY:
2945 return AMDGPUISD::FMAX_LEGACY;
2946 default:
2947 llvm_unreachable("invalid min/max opcode");
2948 }
2949}
2950
Matt Arsenault2529fba2017-01-12 00:09:34 +00002951SDValue AMDGPUTargetLowering::performFNegCombine(SDNode *N,
2952 DAGCombinerInfo &DCI) const {
2953 SelectionDAG &DAG = DCI.DAG;
2954 SDValue N0 = N->getOperand(0);
2955 EVT VT = N->getValueType(0);
2956
2957 unsigned Opc = N0.getOpcode();
2958
2959 // If the input has multiple uses and we can either fold the negate down, or
2960 // the other uses cannot, give up. This both prevents unprofitable
2961 // transformations and infinite loops: we won't repeatedly try to fold around
2962 // a negate that has no 'good' form.
Matt Arsenaulta8fcfad2017-02-02 23:21:23 +00002963 if (N0.hasOneUse()) {
2964 // This may be able to fold into the source, but at a code size cost. Don't
2965 // fold if the fold into the user is free.
2966 if (allUsesHaveSourceMods(N, 0))
2967 return SDValue();
2968 } else {
2969 if (fnegFoldsIntoOp(Opc) &&
2970 (allUsesHaveSourceMods(N) || !allUsesHaveSourceMods(N0.getNode())))
2971 return SDValue();
2972 }
Matt Arsenault2529fba2017-01-12 00:09:34 +00002973
2974 SDLoc SL(N);
2975 switch (Opc) {
2976 case ISD::FADD: {
Matt Arsenault3e6f9b52017-01-19 06:35:27 +00002977 if (!mayIgnoreSignedZero(N0))
2978 return SDValue();
2979
Matt Arsenault2529fba2017-01-12 00:09:34 +00002980 // (fneg (fadd x, y)) -> (fadd (fneg x), (fneg y))
2981 SDValue LHS = N0.getOperand(0);
2982 SDValue RHS = N0.getOperand(1);
2983
2984 if (LHS.getOpcode() != ISD::FNEG)
2985 LHS = DAG.getNode(ISD::FNEG, SL, VT, LHS);
2986 else
2987 LHS = LHS.getOperand(0);
2988
2989 if (RHS.getOpcode() != ISD::FNEG)
2990 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2991 else
2992 RHS = RHS.getOperand(0);
2993
Matt Arsenault7b49ad72017-01-23 19:08:34 +00002994 SDValue Res = DAG.getNode(ISD::FADD, SL, VT, LHS, RHS, N0->getFlags());
Matt Arsenault2529fba2017-01-12 00:09:34 +00002995 if (!N0.hasOneUse())
2996 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
2997 return Res;
2998 }
Matt Arsenaulta8c325e2017-01-12 18:26:30 +00002999 case ISD::FMUL:
3000 case AMDGPUISD::FMUL_LEGACY: {
Matt Arsenault4103a812017-01-12 00:23:20 +00003001 // (fneg (fmul x, y)) -> (fmul x, (fneg y))
Matt Arsenaulta8c325e2017-01-12 18:26:30 +00003002 // (fneg (fmul_legacy x, y)) -> (fmul_legacy x, (fneg y))
Matt Arsenault4103a812017-01-12 00:23:20 +00003003 SDValue LHS = N0.getOperand(0);
3004 SDValue RHS = N0.getOperand(1);
3005
3006 if (LHS.getOpcode() == ISD::FNEG)
3007 LHS = LHS.getOperand(0);
3008 else if (RHS.getOpcode() == ISD::FNEG)
3009 RHS = RHS.getOperand(0);
3010 else
3011 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
3012
Matt Arsenault7b49ad72017-01-23 19:08:34 +00003013 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, RHS, N0->getFlags());
Matt Arsenault4103a812017-01-12 00:23:20 +00003014 if (!N0.hasOneUse())
3015 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
3016 return Res;
3017 }
Matt Arsenault63f95372017-01-12 00:32:16 +00003018 case ISD::FMA:
3019 case ISD::FMAD: {
Matt Arsenault3e6f9b52017-01-19 06:35:27 +00003020 if (!mayIgnoreSignedZero(N0))
3021 return SDValue();
3022
Matt Arsenault63f95372017-01-12 00:32:16 +00003023 // (fneg (fma x, y, z)) -> (fma x, (fneg y), (fneg z))
3024 SDValue LHS = N0.getOperand(0);
3025 SDValue MHS = N0.getOperand(1);
3026 SDValue RHS = N0.getOperand(2);
3027
3028 if (LHS.getOpcode() == ISD::FNEG)
3029 LHS = LHS.getOperand(0);
3030 else if (MHS.getOpcode() == ISD::FNEG)
3031 MHS = MHS.getOperand(0);
3032 else
3033 MHS = DAG.getNode(ISD::FNEG, SL, VT, MHS);
3034
3035 if (RHS.getOpcode() != ISD::FNEG)
3036 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
3037 else
3038 RHS = RHS.getOperand(0);
3039
3040 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, MHS, RHS);
3041 if (!N0.hasOneUse())
3042 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
3043 return Res;
3044 }
Matt Arsenault2511c032017-02-03 00:23:15 +00003045 case ISD::FMAXNUM:
Matt Arsenaulte1b59532017-02-03 00:51:50 +00003046 case ISD::FMINNUM:
3047 case AMDGPUISD::FMAX_LEGACY:
3048 case AMDGPUISD::FMIN_LEGACY: {
Matt Arsenault2511c032017-02-03 00:23:15 +00003049 // fneg (fmaxnum x, y) -> fminnum (fneg x), (fneg y)
3050 // fneg (fminnum x, y) -> fmaxnum (fneg x), (fneg y)
Matt Arsenaulte1b59532017-02-03 00:51:50 +00003051 // fneg (fmax_legacy x, y) -> fmin_legacy (fneg x), (fneg y)
3052 // fneg (fmin_legacy x, y) -> fmax_legacy (fneg x), (fneg y)
3053
Matt Arsenault2511c032017-02-03 00:23:15 +00003054 SDValue LHS = N0.getOperand(0);
3055 SDValue RHS = N0.getOperand(1);
3056
3057 // 0 doesn't have a negated inline immediate.
3058 // TODO: Shouldn't fold 1/2pi either, and should be generalized to other
3059 // operations.
3060 if (isConstantFPZero(RHS))
3061 return SDValue();
3062
3063 SDValue NegLHS = DAG.getNode(ISD::FNEG, SL, VT, LHS);
3064 SDValue NegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
Matt Arsenaulte1b59532017-02-03 00:51:50 +00003065 unsigned Opposite = inverseMinMax(Opc);
Matt Arsenault2511c032017-02-03 00:23:15 +00003066
3067 SDValue Res = DAG.getNode(Opposite, SL, VT, NegLHS, NegRHS, N0->getFlags());
3068 if (!N0.hasOneUse())
3069 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
3070 return Res;
3071 }
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00003072 case ISD::FP_EXTEND:
Matt Arsenault53f0cc22017-01-26 01:25:36 +00003073 case ISD::FTRUNC:
3074 case ISD::FRINT:
3075 case ISD::FNEARBYINT: // XXX - Should fround be handled?
3076 case ISD::FSIN:
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00003077 case AMDGPUISD::RCP:
Matt Arsenault31c039e2017-01-12 18:48:09 +00003078 case AMDGPUISD::RCP_LEGACY:
Matt Arsenault31c039e2017-01-12 18:48:09 +00003079 case AMDGPUISD::SIN_HW: {
Matt Arsenault98d2bf102017-01-12 17:46:28 +00003080 SDValue CvtSrc = N0.getOperand(0);
3081 if (CvtSrc.getOpcode() == ISD::FNEG) {
3082 // (fneg (fp_extend (fneg x))) -> (fp_extend x)
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00003083 // (fneg (rcp (fneg x))) -> (rcp x)
Matt Arsenault4242d482017-01-12 17:46:33 +00003084 return DAG.getNode(Opc, SL, VT, CvtSrc.getOperand(0));
Matt Arsenault98d2bf102017-01-12 17:46:28 +00003085 }
3086
3087 if (!N0.hasOneUse())
3088 return SDValue();
3089
3090 // (fneg (fp_extend x)) -> (fp_extend (fneg x))
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00003091 // (fneg (rcp x)) -> (rcp (fneg x))
Matt Arsenault98d2bf102017-01-12 17:46:28 +00003092 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc);
Matt Arsenault7b49ad72017-01-23 19:08:34 +00003093 return DAG.getNode(Opc, SL, VT, Neg, N0->getFlags());
Matt Arsenault4242d482017-01-12 17:46:33 +00003094 }
3095 case ISD::FP_ROUND: {
3096 SDValue CvtSrc = N0.getOperand(0);
3097
3098 if (CvtSrc.getOpcode() == ISD::FNEG) {
3099 // (fneg (fp_round (fneg x))) -> (fp_round x)
3100 return DAG.getNode(ISD::FP_ROUND, SL, VT,
3101 CvtSrc.getOperand(0), N0.getOperand(1));
3102 }
3103
3104 if (!N0.hasOneUse())
3105 return SDValue();
3106
3107 // (fneg (fp_round x)) -> (fp_round (fneg x))
3108 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc);
3109 return DAG.getNode(ISD::FP_ROUND, SL, VT, Neg, N0.getOperand(1));
Matt Arsenault98d2bf102017-01-12 17:46:28 +00003110 }
Matt Arsenault9dba9bd2017-02-02 02:27:04 +00003111 case ISD::FP16_TO_FP: {
3112 // v_cvt_f32_f16 supports source modifiers on pre-VI targets without legal
3113 // f16, but legalization of f16 fneg ends up pulling it out of the source.
3114 // Put the fneg back as a legal source operation that can be matched later.
3115 SDLoc SL(N);
3116
3117 SDValue Src = N0.getOperand(0);
3118 EVT SrcVT = Src.getValueType();
3119
3120 // fneg (fp16_to_fp x) -> fp16_to_fp (xor x, 0x8000)
3121 SDValue IntFNeg = DAG.getNode(ISD::XOR, SL, SrcVT, Src,
3122 DAG.getConstant(0x8000, SL, SrcVT));
3123 return DAG.getNode(ISD::FP16_TO_FP, SL, N->getValueType(0), IntFNeg);
3124 }
3125 default:
3126 return SDValue();
3127 }
3128}
3129
3130SDValue AMDGPUTargetLowering::performFAbsCombine(SDNode *N,
3131 DAGCombinerInfo &DCI) const {
3132 SelectionDAG &DAG = DCI.DAG;
3133 SDValue N0 = N->getOperand(0);
3134
3135 if (!N0.hasOneUse())
3136 return SDValue();
3137
3138 switch (N0.getOpcode()) {
3139 case ISD::FP16_TO_FP: {
3140 assert(!Subtarget->has16BitInsts() && "should only see if f16 is illegal");
3141 SDLoc SL(N);
3142 SDValue Src = N0.getOperand(0);
3143 EVT SrcVT = Src.getValueType();
3144
3145 // fabs (fp16_to_fp x) -> fp16_to_fp (and x, 0x7fff)
3146 SDValue IntFAbs = DAG.getNode(ISD::AND, SL, SrcVT, Src,
3147 DAG.getConstant(0x7fff, SL, SrcVT));
3148 return DAG.getNode(ISD::FP16_TO_FP, SL, N->getValueType(0), IntFAbs);
3149 }
Matt Arsenault2529fba2017-01-12 00:09:34 +00003150 default:
3151 return SDValue();
3152 }
3153}
3154
Tom Stellard50122a52014-04-07 19:45:41 +00003155SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N,
Matt Arsenaultca3976f2014-07-15 02:06:31 +00003156 DAGCombinerInfo &DCI) const {
Tom Stellard50122a52014-04-07 19:45:41 +00003157 SelectionDAG &DAG = DCI.DAG;
3158 SDLoc DL(N);
3159
3160 switch(N->getOpcode()) {
Matt Arsenault24e33d12015-07-03 23:33:38 +00003161 default:
3162 break;
Matt Arsenault79003342016-04-14 21:58:07 +00003163 case ISD::BITCAST: {
3164 EVT DestVT = N->getValueType(0);
Matt Arsenaultd99ef112016-09-17 15:44:16 +00003165
3166 // Push casts through vector builds. This helps avoid emitting a large
3167 // number of copies when materializing floating point vector constants.
3168 //
3169 // vNt1 bitcast (vNt0 (build_vector t0:x, t0:y)) =>
3170 // vnt1 = build_vector (t1 (bitcast t0:x)), (t1 (bitcast t0:y))
3171 if (DestVT.isVector()) {
3172 SDValue Src = N->getOperand(0);
3173 if (Src.getOpcode() == ISD::BUILD_VECTOR) {
3174 EVT SrcVT = Src.getValueType();
3175 unsigned NElts = DestVT.getVectorNumElements();
3176
3177 if (SrcVT.getVectorNumElements() == NElts) {
3178 EVT DestEltVT = DestVT.getVectorElementType();
3179
3180 SmallVector<SDValue, 8> CastedElts;
3181 SDLoc SL(N);
3182 for (unsigned I = 0, E = SrcVT.getVectorNumElements(); I != E; ++I) {
3183 SDValue Elt = Src.getOperand(I);
3184 CastedElts.push_back(DAG.getNode(ISD::BITCAST, DL, DestEltVT, Elt));
3185 }
3186
3187 return DAG.getBuildVector(DestVT, SL, CastedElts);
3188 }
3189 }
3190 }
3191
Matt Arsenault79003342016-04-14 21:58:07 +00003192 if (DestVT.getSizeInBits() != 64 && !DestVT.isVector())
3193 break;
3194
3195 // Fold bitcasts of constants.
3196 //
3197 // v2i32 (bitcast i64:k) -> build_vector lo_32(k), hi_32(k)
3198 // TODO: Generalize and move to DAGCombiner
3199 SDValue Src = N->getOperand(0);
3200 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src)) {
3201 assert(Src.getValueType() == MVT::i64);
3202 SDLoc SL(N);
3203 uint64_t CVal = C->getZExtValue();
3204 return DAG.getNode(ISD::BUILD_VECTOR, SL, DestVT,
3205 DAG.getConstant(Lo_32(CVal), SL, MVT::i32),
3206 DAG.getConstant(Hi_32(CVal), SL, MVT::i32));
3207 }
3208
3209 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Src)) {
3210 const APInt &Val = C->getValueAPF().bitcastToAPInt();
3211 SDLoc SL(N);
3212 uint64_t CVal = Val.getZExtValue();
3213 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
3214 DAG.getConstant(Lo_32(CVal), SL, MVT::i32),
3215 DAG.getConstant(Hi_32(CVal), SL, MVT::i32));
3216
3217 return DAG.getNode(ISD::BITCAST, SL, DestVT, Vec);
3218 }
3219
3220 break;
3221 }
Matt Arsenault24692112015-07-14 18:20:33 +00003222 case ISD::SHL: {
3223 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3224 break;
3225
3226 return performShlCombine(N, DCI);
3227 }
Matt Arsenault80edab92016-01-18 21:43:36 +00003228 case ISD::SRL: {
3229 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3230 break;
3231
3232 return performSrlCombine(N, DCI);
3233 }
Matt Arsenault33e3ece2016-01-18 22:09:04 +00003234 case ISD::SRA: {
3235 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3236 break;
3237
3238 return performSraCombine(N, DCI);
3239 }
Matt Arsenault24e33d12015-07-03 23:33:38 +00003240 case ISD::MUL:
3241 return performMulCombine(N, DCI);
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003242 case ISD::MULHS:
3243 return performMulhsCombine(N, DCI);
3244 case ISD::MULHU:
3245 return performMulhuCombine(N, DCI);
Matt Arsenault24e33d12015-07-03 23:33:38 +00003246 case AMDGPUISD::MUL_I24:
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003247 case AMDGPUISD::MUL_U24:
3248 case AMDGPUISD::MULHI_I24:
3249 case AMDGPUISD::MULHI_U24: {
Tom Stellard6c7dd982016-10-21 20:25:11 +00003250 // If the first call to simplify is successfull, then N may end up being
3251 // deleted, so we shouldn't call simplifyI24 again.
3252 simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI);
Matt Arsenault24e33d12015-07-03 23:33:38 +00003253 return SDValue();
3254 }
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003255 case AMDGPUISD::MUL_LOHI_I24:
3256 case AMDGPUISD::MUL_LOHI_U24:
3257 return performMulLoHi24Combine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00003258 case ISD::SELECT:
3259 return performSelectCombine(N, DCI);
Matt Arsenault2529fba2017-01-12 00:09:34 +00003260 case ISD::FNEG:
3261 return performFNegCombine(N, DCI);
Matt Arsenault9dba9bd2017-02-02 02:27:04 +00003262 case ISD::FABS:
3263 return performFAbsCombine(N, DCI);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003264 case AMDGPUISD::BFE_I32:
3265 case AMDGPUISD::BFE_U32: {
3266 assert(!N->getValueType(0).isVector() &&
3267 "Vector handling of BFE not implemented");
3268 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
3269 if (!Width)
3270 break;
3271
3272 uint32_t WidthVal = Width->getZExtValue() & 0x1f;
3273 if (WidthVal == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003274 return DAG.getConstant(0, DL, MVT::i32);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003275
3276 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
3277 if (!Offset)
3278 break;
3279
3280 SDValue BitsFrom = N->getOperand(0);
3281 uint32_t OffsetVal = Offset->getZExtValue() & 0x1f;
3282
3283 bool Signed = N->getOpcode() == AMDGPUISD::BFE_I32;
3284
3285 if (OffsetVal == 0) {
3286 // This is already sign / zero extended, so try to fold away extra BFEs.
3287 unsigned SignBits = Signed ? (32 - WidthVal + 1) : (32 - WidthVal);
3288
3289 unsigned OpSignBits = DAG.ComputeNumSignBits(BitsFrom);
3290 if (OpSignBits >= SignBits)
3291 return BitsFrom;
Matt Arsenault05e96f42014-05-22 18:09:12 +00003292
3293 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), WidthVal);
3294 if (Signed) {
3295 // This is a sign_extend_inreg. Replace it to take advantage of existing
3296 // DAG Combines. If not eliminated, we will match back to BFE during
3297 // selection.
3298
3299 // TODO: The sext_inreg of extended types ends, although we can could
3300 // handle them in a single BFE.
3301 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, BitsFrom,
3302 DAG.getValueType(SmallVT));
3303 }
3304
3305 return DAG.getZeroExtendInReg(BitsFrom, DL, SmallVT);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003306 }
3307
Matt Arsenaultf1794202014-10-15 05:07:00 +00003308 if (ConstantSDNode *CVal = dyn_cast<ConstantSDNode>(BitsFrom)) {
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003309 if (Signed) {
3310 return constantFoldBFE<int32_t>(DAG,
Matt Arsenault46cbc432014-09-19 00:42:06 +00003311 CVal->getSExtValue(),
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003312 OffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003313 WidthVal,
3314 DL);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003315 }
3316
3317 return constantFoldBFE<uint32_t>(DAG,
Matt Arsenault6462f942014-09-18 15:52:26 +00003318 CVal->getZExtValue(),
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003319 OffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003320 WidthVal,
3321 DL);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003322 }
3323
Matt Arsenault05e96f42014-05-22 18:09:12 +00003324 if ((OffsetVal + WidthVal) >= 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003325 SDValue ShiftVal = DAG.getConstant(OffsetVal, DL, MVT::i32);
Matt Arsenault05e96f42014-05-22 18:09:12 +00003326 return DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, MVT::i32,
3327 BitsFrom, ShiftVal);
3328 }
3329
Matt Arsenault7b68fdf2014-10-15 17:58:34 +00003330 if (BitsFrom.hasOneUse()) {
Matt Arsenault6de7af42014-10-15 23:37:42 +00003331 APInt Demanded = APInt::getBitsSet(32,
3332 OffsetVal,
3333 OffsetVal + WidthVal);
3334
Matt Arsenault7b68fdf2014-10-15 17:58:34 +00003335 APInt KnownZero, KnownOne;
3336 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
3337 !DCI.isBeforeLegalizeOps());
3338 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3339 if (TLO.ShrinkDemandedConstant(BitsFrom, Demanded) ||
3340 TLI.SimplifyDemandedBits(BitsFrom, Demanded,
3341 KnownZero, KnownOne, TLO)) {
3342 DCI.CommitTargetLoweringOpt(TLO);
3343 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003344 }
3345
3346 break;
3347 }
Matt Arsenault327bb5a2016-07-01 22:47:50 +00003348 case ISD::LOAD:
3349 return performLoadCombine(N, DCI);
Matt Arsenaultca3976f2014-07-15 02:06:31 +00003350 case ISD::STORE:
3351 return performStoreCombine(N, DCI);
Matt Arsenault2fdf2a12017-02-21 23:35:48 +00003352 case AMDGPUISD::CLAMP:
3353 return performClampCombine(N, DCI);
Tom Stellard50122a52014-04-07 19:45:41 +00003354 }
3355 return SDValue();
3356}
3357
3358//===----------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +00003359// Helper functions
3360//===----------------------------------------------------------------------===//
3361
Tom Stellard75aadc22012-12-11 21:25:42 +00003362SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
3363 const TargetRegisterClass *RC,
3364 unsigned Reg, EVT VT) const {
3365 MachineFunction &MF = DAG.getMachineFunction();
3366 MachineRegisterInfo &MRI = MF.getRegInfo();
3367 unsigned VirtualRegister;
3368 if (!MRI.isLiveIn(Reg)) {
3369 VirtualRegister = MRI.createVirtualRegister(RC);
3370 MRI.addLiveIn(Reg, VirtualRegister);
3371 } else {
3372 VirtualRegister = MRI.getLiveInVirtReg(Reg);
3373 }
3374 return DAG.getRegister(VirtualRegister, VT);
3375}
3376
Tom Stellarddcb9f092015-07-09 21:20:37 +00003377uint32_t AMDGPUTargetLowering::getImplicitParameterOffset(
3378 const AMDGPUMachineFunction *MFI, const ImplicitParameter Param) const {
Tom Stellardb2869eb2016-09-09 19:28:00 +00003379 unsigned Alignment = Subtarget->getAlignmentForImplicitArgPtr();
3380 uint64_t ArgOffset = alignTo(MFI->getABIArgOffset(), Alignment);
Tom Stellarddcb9f092015-07-09 21:20:37 +00003381 switch (Param) {
3382 case GRID_DIM:
3383 return ArgOffset;
3384 case GRID_OFFSET:
3385 return ArgOffset + 4;
3386 }
3387 llvm_unreachable("unexpected implicit parameter type");
3388}
3389
Tom Stellard75aadc22012-12-11 21:25:42 +00003390#define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
3391
3392const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00003393 switch ((AMDGPUISD::NodeType)Opcode) {
3394 case AMDGPUISD::FIRST_NUMBER: break;
Tom Stellard75aadc22012-12-11 21:25:42 +00003395 // AMDIL DAG nodes
Tom Stellard75aadc22012-12-11 21:25:42 +00003396 NODE_NAME_CASE(CALL);
3397 NODE_NAME_CASE(UMUL);
Tom Stellard75aadc22012-12-11 21:25:42 +00003398 NODE_NAME_CASE(BRANCH_COND);
3399
3400 // AMDGPU DAG nodes
Matt Arsenault9babdf42016-06-22 20:15:28 +00003401 NODE_NAME_CASE(ENDPGM)
3402 NODE_NAME_CASE(RETURN)
Tom Stellard75aadc22012-12-11 21:25:42 +00003403 NODE_NAME_CASE(DWORDADDR)
3404 NODE_NAME_CASE(FRACT)
Wei Ding07e03712016-07-28 16:42:13 +00003405 NODE_NAME_CASE(SETCC)
Tom Stellard8485fa02016-12-07 02:42:15 +00003406 NODE_NAME_CASE(SETREG)
3407 NODE_NAME_CASE(FMA_W_CHAIN)
3408 NODE_NAME_CASE(FMUL_W_CHAIN)
Matt Arsenault5d47d4a2014-06-12 21:15:44 +00003409 NODE_NAME_CASE(CLAMP)
Matthias Braund04893f2015-05-07 21:33:59 +00003410 NODE_NAME_CASE(COS_HW)
3411 NODE_NAME_CASE(SIN_HW)
Matt Arsenaultda59f3d2014-11-13 23:03:09 +00003412 NODE_NAME_CASE(FMAX_LEGACY)
Matt Arsenaultda59f3d2014-11-13 23:03:09 +00003413 NODE_NAME_CASE(FMIN_LEGACY)
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003414 NODE_NAME_CASE(FMAX3)
3415 NODE_NAME_CASE(SMAX3)
3416 NODE_NAME_CASE(UMAX3)
3417 NODE_NAME_CASE(FMIN3)
3418 NODE_NAME_CASE(SMIN3)
3419 NODE_NAME_CASE(UMIN3)
Matt Arsenaultf639c322016-01-28 20:53:42 +00003420 NODE_NAME_CASE(FMED3)
3421 NODE_NAME_CASE(SMED3)
3422 NODE_NAME_CASE(UMED3)
Matt Arsenaulta0050b02014-06-19 01:19:19 +00003423 NODE_NAME_CASE(URECIP)
3424 NODE_NAME_CASE(DIV_SCALE)
3425 NODE_NAME_CASE(DIV_FMAS)
3426 NODE_NAME_CASE(DIV_FIXUP)
Wei Ding4d3d4ca2017-02-24 23:00:29 +00003427 NODE_NAME_CASE(FMAD_FTZ)
Matt Arsenaulta0050b02014-06-19 01:19:19 +00003428 NODE_NAME_CASE(TRIG_PREOP)
3429 NODE_NAME_CASE(RCP)
3430 NODE_NAME_CASE(RSQ)
Matt Arsenault32fc5272016-07-26 16:45:45 +00003431 NODE_NAME_CASE(RCP_LEGACY)
Matt Arsenault257d48d2014-06-24 22:13:39 +00003432 NODE_NAME_CASE(RSQ_LEGACY)
Matt Arsenault32fc5272016-07-26 16:45:45 +00003433 NODE_NAME_CASE(FMUL_LEGACY)
Matt Arsenault79963e82016-02-13 01:03:00 +00003434 NODE_NAME_CASE(RSQ_CLAMP)
Matt Arsenault2e7cc482014-08-15 17:30:25 +00003435 NODE_NAME_CASE(LDEXP)
Matt Arsenault4831ce52015-01-06 23:00:37 +00003436 NODE_NAME_CASE(FP_CLASS)
Matt Arsenaulta0050b02014-06-19 01:19:19 +00003437 NODE_NAME_CASE(DOT4)
Matthias Braund04893f2015-05-07 21:33:59 +00003438 NODE_NAME_CASE(CARRY)
3439 NODE_NAME_CASE(BORROW)
Matt Arsenaultfae02982014-03-17 18:58:11 +00003440 NODE_NAME_CASE(BFE_U32)
3441 NODE_NAME_CASE(BFE_I32)
Matt Arsenaultb3458362014-03-31 18:21:13 +00003442 NODE_NAME_CASE(BFI)
3443 NODE_NAME_CASE(BFM)
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00003444 NODE_NAME_CASE(FFBH_U32)
Matt Arsenaultb51dcb92016-07-18 18:40:51 +00003445 NODE_NAME_CASE(FFBH_I32)
Tom Stellard50122a52014-04-07 19:45:41 +00003446 NODE_NAME_CASE(MUL_U24)
3447 NODE_NAME_CASE(MUL_I24)
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003448 NODE_NAME_CASE(MULHI_U24)
3449 NODE_NAME_CASE(MULHI_I24)
3450 NODE_NAME_CASE(MUL_LOHI_U24)
3451 NODE_NAME_CASE(MUL_LOHI_I24)
Matt Arsenaulteb260202014-05-22 18:00:15 +00003452 NODE_NAME_CASE(MAD_U24)
3453 NODE_NAME_CASE(MAD_I24)
Matthias Braund04893f2015-05-07 21:33:59 +00003454 NODE_NAME_CASE(TEXTURE_FETCH)
Tom Stellard75aadc22012-12-11 21:25:42 +00003455 NODE_NAME_CASE(EXPORT)
Matt Arsenault7bee6ac2016-12-05 20:23:10 +00003456 NODE_NAME_CASE(EXPORT_DONE)
3457 NODE_NAME_CASE(R600_EXPORT)
Tom Stellardff62c352013-01-23 02:09:03 +00003458 NODE_NAME_CASE(CONST_ADDRESS)
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00003459 NODE_NAME_CASE(REGISTER_LOAD)
3460 NODE_NAME_CASE(REGISTER_STORE)
Tom Stellard9fa17912013-08-14 23:24:45 +00003461 NODE_NAME_CASE(LOAD_INPUT)
3462 NODE_NAME_CASE(SAMPLE)
3463 NODE_NAME_CASE(SAMPLEB)
3464 NODE_NAME_CASE(SAMPLED)
3465 NODE_NAME_CASE(SAMPLEL)
Matt Arsenault364a6742014-06-11 17:50:44 +00003466 NODE_NAME_CASE(CVT_F32_UBYTE0)
3467 NODE_NAME_CASE(CVT_F32_UBYTE1)
3468 NODE_NAME_CASE(CVT_F32_UBYTE2)
3469 NODE_NAME_CASE(CVT_F32_UBYTE3)
Matt Arsenault1f17c662017-02-22 00:27:34 +00003470 NODE_NAME_CASE(CVT_PKRTZ_F16_F32)
Tom Stellard880a80a2014-06-17 16:53:14 +00003471 NODE_NAME_CASE(BUILD_VERTICAL_VECTOR)
Tom Stellard067c8152014-07-21 14:01:14 +00003472 NODE_NAME_CASE(CONST_DATA_PTR)
Tom Stellardbf3e6e52016-06-14 20:29:59 +00003473 NODE_NAME_CASE(PC_ADD_REL_OFFSET)
Matt Arsenault03006fd2016-07-19 16:27:56 +00003474 NODE_NAME_CASE(KILL)
Jan Veselyf1705042017-01-20 21:24:26 +00003475 NODE_NAME_CASE(DUMMY_CHAIN)
Matthias Braund04893f2015-05-07 21:33:59 +00003476 case AMDGPUISD::FIRST_MEM_OPCODE_NUMBER: break;
Tom Stellardfc92e772015-05-12 14:18:14 +00003477 NODE_NAME_CASE(SENDMSG)
Jan Veselyd48445d2017-01-04 18:06:55 +00003478 NODE_NAME_CASE(SENDMSGHALT)
Tom Stellard2a9d9472015-05-12 15:00:46 +00003479 NODE_NAME_CASE(INTERP_MOV)
3480 NODE_NAME_CASE(INTERP_P1)
3481 NODE_NAME_CASE(INTERP_P2)
Tom Stellardd3ee8c12013-08-16 01:12:06 +00003482 NODE_NAME_CASE(STORE_MSKOR)
Matt Arsenaultdfaf4262016-04-25 19:27:09 +00003483 NODE_NAME_CASE(LOAD_CONSTANT)
Tom Stellardafcf12f2013-09-12 02:55:14 +00003484 NODE_NAME_CASE(TBUFFER_STORE_FORMAT)
Tom Stellard354a43c2016-04-01 18:27:37 +00003485 NODE_NAME_CASE(ATOMIC_CMP_SWAP)
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00003486 NODE_NAME_CASE(ATOMIC_INC)
3487 NODE_NAME_CASE(ATOMIC_DEC)
Tom Stellard6f9ef142016-12-20 17:19:44 +00003488 NODE_NAME_CASE(BUFFER_LOAD)
3489 NODE_NAME_CASE(BUFFER_LOAD_FORMAT)
Matthias Braund04893f2015-05-07 21:33:59 +00003490 case AMDGPUISD::LAST_AMDGPU_ISD_NUMBER: break;
Tom Stellard75aadc22012-12-11 21:25:42 +00003491 }
Matthias Braund04893f2015-05-07 21:33:59 +00003492 return nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +00003493}
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003494
Evandro Menezes21f9ce12016-11-10 23:31:06 +00003495SDValue AMDGPUTargetLowering::getSqrtEstimate(SDValue Operand,
3496 SelectionDAG &DAG, int Enabled,
3497 int &RefinementSteps,
3498 bool &UseOneConstNR,
3499 bool Reciprocal) const {
Matt Arsenaulte93d06a2015-01-13 20:53:18 +00003500 EVT VT = Operand.getValueType();
3501
3502 if (VT == MVT::f32) {
3503 RefinementSteps = 0;
3504 return DAG.getNode(AMDGPUISD::RSQ, SDLoc(Operand), VT, Operand);
3505 }
3506
3507 // TODO: There is also f64 rsq instruction, but the documentation is less
3508 // clear on its precision.
3509
3510 return SDValue();
3511}
3512
Matt Arsenaultbf0db912015-01-13 20:53:23 +00003513SDValue AMDGPUTargetLowering::getRecipEstimate(SDValue Operand,
Sanjay Patel0051efc2016-10-20 16:55:45 +00003514 SelectionDAG &DAG, int Enabled,
3515 int &RefinementSteps) const {
Matt Arsenaultbf0db912015-01-13 20:53:23 +00003516 EVT VT = Operand.getValueType();
3517
3518 if (VT == MVT::f32) {
3519 // Reciprocal, < 1 ulp error.
3520 //
3521 // This reciprocal approximation converges to < 0.5 ulp error with one
3522 // newton rhapson performed with two fused multiple adds (FMAs).
3523
3524 RefinementSteps = 0;
3525 return DAG.getNode(AMDGPUISD::RCP, SDLoc(Operand), VT, Operand);
3526 }
3527
3528 // TODO: There is also f64 rcp instruction, but the documentation is less
3529 // clear on its precision.
3530
3531 return SDValue();
3532}
3533
Jay Foada0653a32014-05-14 21:14:37 +00003534void AMDGPUTargetLowering::computeKnownBitsForTargetNode(
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003535 const SDValue Op,
3536 APInt &KnownZero,
3537 APInt &KnownOne,
3538 const SelectionDAG &DAG,
3539 unsigned Depth) const {
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003540
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003541 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0); // Don't know anything.
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003542
3543 APInt KnownZero2;
3544 APInt KnownOne2;
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003545 unsigned Opc = Op.getOpcode();
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003546
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003547 switch (Opc) {
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003548 default:
3549 break;
Jan Vesely808fff52015-04-30 17:15:56 +00003550 case AMDGPUISD::CARRY:
3551 case AMDGPUISD::BORROW: {
3552 KnownZero = APInt::getHighBitsSet(32, 31);
3553 break;
3554 }
3555
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003556 case AMDGPUISD::BFE_I32:
3557 case AMDGPUISD::BFE_U32: {
3558 ConstantSDNode *CWidth = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3559 if (!CWidth)
3560 return;
3561
3562 unsigned BitWidth = 32;
3563 uint32_t Width = CWidth->getZExtValue() & 0x1f;
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003564
Matt Arsenaulta3fe7c62014-10-16 20:07:40 +00003565 if (Opc == AMDGPUISD::BFE_U32)
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003566 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - Width);
3567
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003568 break;
3569 }
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003570 }
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003571}
Matt Arsenaultbf8694d2014-05-22 18:09:03 +00003572
3573unsigned AMDGPUTargetLowering::ComputeNumSignBitsForTargetNode(
3574 SDValue Op,
3575 const SelectionDAG &DAG,
3576 unsigned Depth) const {
3577 switch (Op.getOpcode()) {
3578 case AMDGPUISD::BFE_I32: {
3579 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3580 if (!Width)
3581 return 1;
3582
3583 unsigned SignBits = 32 - Width->getZExtValue() + 1;
Artyom Skrobov314ee042015-11-25 19:41:11 +00003584 if (!isNullConstant(Op.getOperand(1)))
Matt Arsenaultbf8694d2014-05-22 18:09:03 +00003585 return SignBits;
3586
3587 // TODO: Could probably figure something out with non-0 offsets.
3588 unsigned Op0SignBits = DAG.ComputeNumSignBits(Op.getOperand(0), Depth + 1);
3589 return std::max(SignBits, Op0SignBits);
3590 }
3591
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003592 case AMDGPUISD::BFE_U32: {
3593 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3594 return Width ? 32 - (Width->getZExtValue() & 0x1f) : 1;
3595 }
3596
Jan Vesely808fff52015-04-30 17:15:56 +00003597 case AMDGPUISD::CARRY:
3598 case AMDGPUISD::BORROW:
3599 return 31;
3600
Matt Arsenaultbf8694d2014-05-22 18:09:03 +00003601 default:
3602 return 1;
3603 }
3604}