blob: 391cb8c6fc99c2aa168eea58bf68ba8815a89b6e [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- SystemZISelLowering.cpp - SystemZ DAG lowering implementation -----===//
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// This file implements the SystemZTargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
Ulrich Weigand5f613df2013-05-06 16:15:19 +000014#include "SystemZISelLowering.h"
15#include "SystemZCallingConv.h"
16#include "SystemZConstantPoolValue.h"
17#include "SystemZMachineFunctionInfo.h"
18#include "SystemZTargetMachine.h"
19#include "llvm/CodeGen/CallingConvLower.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
22#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Ulrich Weigand57c85f52015-04-01 12:51:43 +000023#include "llvm/IR/Intrinsics.h"
Will Dietz981af002013-10-12 00:55:57 +000024#include <cctype>
25
Ulrich Weigand5f613df2013-05-06 16:15:19 +000026using namespace llvm;
27
Chandler Carruth84e68b22014-04-22 02:41:26 +000028#define DEBUG_TYPE "systemz-lower"
29
Richard Sandifordf722a8e302013-10-16 11:10:55 +000030namespace {
31// Represents a sequence for extracting a 0/1 value from an IPM result:
32// (((X ^ XORValue) + AddValue) >> Bit)
33struct IPMConversion {
34 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
35 : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
36
37 int64_t XORValue;
38 int64_t AddValue;
39 unsigned Bit;
40};
Richard Sandifordd420f732013-12-13 15:28:45 +000041
42// Represents information about a comparison.
43struct Comparison {
44 Comparison(SDValue Op0In, SDValue Op1In)
45 : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {}
46
47 // The operands to the comparison.
48 SDValue Op0, Op1;
49
50 // The opcode that should be used to compare Op0 and Op1.
51 unsigned Opcode;
52
53 // A SystemZICMP value. Only used for integer comparisons.
54 unsigned ICmpType;
55
56 // The mask of CC values that Opcode can produce.
57 unsigned CCValid;
58
59 // The mask of CC values for which the original condition is true.
60 unsigned CCMask;
61};
Richard Sandifordc2312692014-03-06 10:38:30 +000062} // end anonymous namespace
Richard Sandifordf722a8e302013-10-16 11:10:55 +000063
Ulrich Weigand5f613df2013-05-06 16:15:19 +000064// Classify VT as either 32 or 64 bit.
65static bool is32Bit(EVT VT) {
66 switch (VT.getSimpleVT().SimpleTy) {
67 case MVT::i32:
68 return true;
69 case MVT::i64:
70 return false;
71 default:
72 llvm_unreachable("Unsupported type");
73 }
74}
75
76// Return a version of MachineOperand that can be safely used before the
77// final use.
78static MachineOperand earlyUseOperand(MachineOperand Op) {
79 if (Op.isReg())
80 Op.setIsKill(false);
81 return Op;
82}
83
Eric Christophera6734172015-01-31 00:06:45 +000084SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &tm,
85 const SystemZSubtarget &STI)
86 : TargetLowering(tm), Subtarget(STI) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000087 MVT PtrVT = getPointerTy();
88
89 // Set up the register classes.
Richard Sandiford0755c932013-10-01 11:26:28 +000090 if (Subtarget.hasHighWord())
91 addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass);
92 else
93 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
Ulrich Weigand5f613df2013-05-06 16:15:19 +000094 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
95 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
96 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
97 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
98
Ulrich Weigandce4c1092015-05-05 19:25:42 +000099 if (Subtarget.hasVector()) {
100 addRegisterClass(MVT::v16i8, &SystemZ::VR128BitRegClass);
101 addRegisterClass(MVT::v8i16, &SystemZ::VR128BitRegClass);
102 addRegisterClass(MVT::v4i32, &SystemZ::VR128BitRegClass);
103 addRegisterClass(MVT::v2i64, &SystemZ::VR128BitRegClass);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000104 addRegisterClass(MVT::v4f32, &SystemZ::VR128BitRegClass);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000105 addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000106 }
107
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000108 // Compute derived properties from the register classes
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000109 computeRegisterProperties(Subtarget.getRegisterInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000110
111 // Set up special registers.
112 setExceptionPointerRegister(SystemZ::R6D);
113 setExceptionSelectorRegister(SystemZ::R7D);
114 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
115
116 // TODO: It may be better to default to latency-oriented scheduling, however
117 // LLVM's current latency-oriented scheduler can't handle physreg definitions
Richard Sandiford14a44492013-05-22 13:38:45 +0000118 // such as SystemZ has with CC, so set this to the register-pressure
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000119 // scheduler, because it can.
120 setSchedulingPreference(Sched::RegPressure);
121
122 setBooleanContents(ZeroOrOneBooleanContent);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000123 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000124
125 // Instructions are strings of 2-byte aligned 2-byte values.
126 setMinFunctionAlignment(2);
127
128 // Handle operations that are handled in a similar way for all types.
129 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
130 I <= MVT::LAST_FP_VALUETYPE;
131 ++I) {
132 MVT VT = MVT::SimpleValueType(I);
133 if (isTypeLegal(VT)) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000134 // Lower SET_CC into an IPM-based sequence.
135 setOperationAction(ISD::SETCC, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000136
137 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
138 setOperationAction(ISD::SELECT, VT, Expand);
139
140 // Lower SELECT_CC and BR_CC into separate comparisons and branches.
141 setOperationAction(ISD::SELECT_CC, VT, Custom);
142 setOperationAction(ISD::BR_CC, VT, Custom);
143 }
144 }
145
146 // Expand jump table branches as address arithmetic followed by an
147 // indirect jump.
148 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
149
150 // Expand BRCOND into a BR_CC (see above).
151 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
152
153 // Handle integer types.
154 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
155 I <= MVT::LAST_INTEGER_VALUETYPE;
156 ++I) {
157 MVT VT = MVT::SimpleValueType(I);
158 if (isTypeLegal(VT)) {
159 // Expand individual DIV and REMs into DIVREMs.
160 setOperationAction(ISD::SDIV, VT, Expand);
161 setOperationAction(ISD::UDIV, VT, Expand);
162 setOperationAction(ISD::SREM, VT, Expand);
163 setOperationAction(ISD::UREM, VT, Expand);
164 setOperationAction(ISD::SDIVREM, VT, Custom);
165 setOperationAction(ISD::UDIVREM, VT, Custom);
166
Richard Sandifordbef3d7a2013-12-10 10:49:34 +0000167 // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and
168 // stores, putting a serialization instruction after the stores.
169 setOperationAction(ISD::ATOMIC_LOAD, VT, Custom);
170 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000171
Richard Sandiford41350a52013-12-24 15:18:04 +0000172 // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are
173 // available, or if the operand is constant.
174 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
175
Ulrich Weigandb4012182015-03-31 12:56:33 +0000176 // Use POPCNT on z196 and above.
177 if (Subtarget.hasPopulationCount())
178 setOperationAction(ISD::CTPOP, VT, Custom);
179 else
180 setOperationAction(ISD::CTPOP, VT, Expand);
181
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000182 // No special instructions for these.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000183 setOperationAction(ISD::CTTZ, VT, Expand);
184 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
185 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
186 setOperationAction(ISD::ROTR, VT, Expand);
187
Richard Sandiford7d86e472013-08-21 09:34:56 +0000188 // Use *MUL_LOHI where possible instead of MULH*.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000189 setOperationAction(ISD::MULHS, VT, Expand);
190 setOperationAction(ISD::MULHU, VT, Expand);
Richard Sandiford7d86e472013-08-21 09:34:56 +0000191 setOperationAction(ISD::SMUL_LOHI, VT, Custom);
192 setOperationAction(ISD::UMUL_LOHI, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000193
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000194 // Only z196 and above have native support for conversions to unsigned.
195 if (!Subtarget.hasFPExtension())
196 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000197 }
198 }
199
200 // Type legalization will convert 8- and 16-bit atomic operations into
201 // forms that operate on i32s (but still keeping the original memory VT).
202 // Lower them into full i32 operations.
203 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom);
204 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom);
205 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
206 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom);
207 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom);
208 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom);
209 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
210 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom);
211 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom);
212 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
213 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
214 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
215
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000216 // z10 has instructions for signed but not unsigned FP conversion.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000217 // Handle unsigned 32-bit types as signed 64-bit types.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000218 if (!Subtarget.hasFPExtension()) {
219 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
220 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
221 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000222
223 // We have native support for a 64-bit CTLZ, via FLOGR.
224 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
225 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
226
227 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
228 setOperationAction(ISD::OR, MVT::i64, Custom);
229
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000230 // FIXME: Can we support these natively?
231 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
232 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
233 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
234
235 // We have native instructions for i8, i16 and i32 extensions, but not i1.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000236 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000237 for (MVT VT : MVT::integer_valuetypes()) {
238 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
239 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
240 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
241 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000242
243 // Handle the various types of symbolic address.
244 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
245 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
246 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
247 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
248 setOperationAction(ISD::JumpTable, PtrVT, Custom);
249
250 // We need to handle dynamic allocations specially because of the
251 // 160-byte area at the bottom of the stack.
252 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
253
254 // Use custom expanders so that we can force the function to use
255 // a frame pointer.
256 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
257 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
258
Richard Sandiford03481332013-08-23 11:36:42 +0000259 // Handle prefetches with PFD or PFDRL.
260 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
261
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000262 for (MVT VT : MVT::vector_valuetypes()) {
263 // Assume by default that all vector operations need to be expanded.
264 for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode)
265 if (getOperationAction(Opcode, VT) == Legal)
266 setOperationAction(Opcode, VT, Expand);
267
268 // Likewise all truncating stores and extending loads.
269 for (MVT InnerVT : MVT::vector_valuetypes()) {
270 setTruncStoreAction(VT, InnerVT, Expand);
271 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
272 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
273 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
274 }
275
276 if (isTypeLegal(VT)) {
277 // These operations are legal for anything that can be stored in a
278 // vector register, even if there is no native support for the format
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000279 // as such. In particular, we can do these for v4f32 even though there
280 // are no specific instructions for that format.
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000281 setOperationAction(ISD::LOAD, VT, Legal);
282 setOperationAction(ISD::STORE, VT, Legal);
283 setOperationAction(ISD::VSELECT, VT, Legal);
284 setOperationAction(ISD::BITCAST, VT, Legal);
285 setOperationAction(ISD::UNDEF, VT, Legal);
286
287 // Likewise, except that we need to replace the nodes with something
288 // more specific.
289 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
290 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
291 }
292 }
293
294 // Handle integer vector types.
295 for (MVT VT : MVT::integer_vector_valuetypes()) {
296 if (isTypeLegal(VT)) {
297 // These operations have direct equivalents.
298 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal);
299 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal);
300 setOperationAction(ISD::ADD, VT, Legal);
301 setOperationAction(ISD::SUB, VT, Legal);
302 if (VT != MVT::v2i64)
303 setOperationAction(ISD::MUL, VT, Legal);
304 setOperationAction(ISD::AND, VT, Legal);
305 setOperationAction(ISD::OR, VT, Legal);
306 setOperationAction(ISD::XOR, VT, Legal);
307 setOperationAction(ISD::CTPOP, VT, Custom);
308 setOperationAction(ISD::CTTZ, VT, Legal);
309 setOperationAction(ISD::CTLZ, VT, Legal);
310 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Custom);
311 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Custom);
312
313 // Convert a GPR scalar to a vector by inserting it into element 0.
314 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
315
316 // Detect shifts by a scalar amount and convert them into
317 // V*_BY_SCALAR.
318 setOperationAction(ISD::SHL, VT, Custom);
319 setOperationAction(ISD::SRA, VT, Custom);
320 setOperationAction(ISD::SRL, VT, Custom);
321
322 // At present ROTL isn't matched by DAGCombiner. ROTR should be
323 // converted into ROTL.
324 setOperationAction(ISD::ROTL, VT, Expand);
325 setOperationAction(ISD::ROTR, VT, Expand);
326
327 // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands
328 // and inverting the result as necessary.
329 setOperationAction(ISD::SETCC, VT, Custom);
330 }
331 }
332
Ulrich Weigandcd808232015-05-05 19:26:48 +0000333 if (Subtarget.hasVector()) {
334 // There should be no need to check for float types other than v2f64
335 // since <2 x f32> isn't a legal type.
336 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
337 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
338 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
339 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
340 }
341
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000342 // Handle floating-point types.
343 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
344 I <= MVT::LAST_FP_VALUETYPE;
345 ++I) {
346 MVT VT = MVT::SimpleValueType(I);
347 if (isTypeLegal(VT)) {
348 // We can use FI for FRINT.
349 setOperationAction(ISD::FRINT, VT, Legal);
350
Richard Sandifordaf5f66a2013-08-21 09:04:20 +0000351 // We can use the extended form of FI for other rounding operations.
352 if (Subtarget.hasFPExtension()) {
353 setOperationAction(ISD::FNEARBYINT, VT, Legal);
354 setOperationAction(ISD::FFLOOR, VT, Legal);
355 setOperationAction(ISD::FCEIL, VT, Legal);
356 setOperationAction(ISD::FTRUNC, VT, Legal);
357 setOperationAction(ISD::FROUND, VT, Legal);
358 }
359
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000360 // No special instructions for these.
361 setOperationAction(ISD::FSIN, VT, Expand);
362 setOperationAction(ISD::FCOS, VT, Expand);
363 setOperationAction(ISD::FREM, VT, Expand);
364 }
365 }
366
Ulrich Weigandcd808232015-05-05 19:26:48 +0000367 // Handle floating-point vector types.
368 if (Subtarget.hasVector()) {
369 // Scalar-to-vector conversion is just a subreg.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000370 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000371 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
372
373 // Some insertions and extractions can be done directly but others
374 // need to go via integers.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000375 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000376 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000377 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000378 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
379
380 // These operations have direct equivalents.
381 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
382 setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
383 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
384 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
385 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
386 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
387 setOperationAction(ISD::FABS, MVT::v2f64, Legal);
388 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
389 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
390 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
391 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
392 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
393 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
394 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
395 }
396
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000397 // We have fused multiply-addition for f32 and f64 but not f128.
398 setOperationAction(ISD::FMA, MVT::f32, Legal);
399 setOperationAction(ISD::FMA, MVT::f64, Legal);
400 setOperationAction(ISD::FMA, MVT::f128, Expand);
401
402 // Needed so that we don't try to implement f128 constant loads using
403 // a load-and-extend of a f80 constant (in cases where the constant
404 // would fit in an f80).
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000405 for (MVT VT : MVT::fp_valuetypes())
406 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000407
408 // Floating-point truncation and stores need to be done separately.
409 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
410 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
411 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
412
413 // We have 64-bit FPR<->GPR moves, but need special handling for
414 // 32-bit forms.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000415 if (!Subtarget.hasVector()) {
416 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
417 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
418 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000419
420 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
421 // structure, but VAEND is a no-op.
422 setOperationAction(ISD::VASTART, MVT::Other, Custom);
423 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
424 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Richard Sandifordd131ff82013-07-08 09:35:23 +0000425
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000426 // Codes for which we want to perform some z-specific combinations.
427 setTargetDAGCombine(ISD::SIGN_EXTEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000428 setTargetDAGCombine(ISD::STORE);
429 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000430 setTargetDAGCombine(ISD::FP_ROUND);
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000431
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000432 // Handle intrinsics.
433 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
434
Richard Sandifordd131ff82013-07-08 09:35:23 +0000435 // We want to use MVC in preference to even a single load/store pair.
436 MaxStoresPerMemcpy = 0;
437 MaxStoresPerMemcpyOptSize = 0;
Richard Sandiford47660c12013-07-09 09:32:42 +0000438
439 // The main memset sequence is a byte store followed by an MVC.
440 // Two STC or MV..I stores win over that, but the kind of fused stores
441 // generated by target-independent code don't when the byte value is
442 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
443 // than "STC;MVC". Handle the choice in target-specific code instead.
444 MaxStoresPerMemset = 0;
445 MaxStoresPerMemsetOptSize = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000446}
447
Richard Sandifordabc010b2013-11-06 12:16:02 +0000448EVT SystemZTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
449 if (!VT.isVector())
450 return MVT::i32;
451 return VT.changeVectorElementTypeToInteger();
452}
453
454bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
Stephen Lin73de7bf2013-07-09 18:16:56 +0000455 VT = VT.getScalarType();
456
457 if (!VT.isSimple())
458 return false;
459
460 switch (VT.getSimpleVT().SimpleTy) {
461 case MVT::f32:
462 case MVT::f64:
463 return true;
464 case MVT::f128:
465 return false;
466 default:
467 break;
468 }
469
470 return false;
471}
472
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000473bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
474 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
475 return Imm.isZero() || Imm.isNegZero();
476}
477
Ulrich Weigand1f6666a2015-03-31 12:52:27 +0000478bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
479 // We can use CGFI or CLGFI.
480 return isInt<32>(Imm) || isUInt<32>(Imm);
481}
482
483bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const {
484 // We can use ALGFI or SLGFI.
485 return isUInt<32>(Imm) || isUInt<32>(-Imm);
486}
487
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000488bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
489 unsigned,
490 unsigned,
491 bool *Fast) const {
Richard Sandiford46af5a22013-05-30 09:45:42 +0000492 // Unaligned accesses should never be slower than the expanded version.
493 // We check specifically for aligned accesses in the few cases where
494 // they are required.
495 if (Fast)
496 *Fast = true;
497 return true;
498}
499
Richard Sandiford791bea42013-07-31 12:58:26 +0000500bool SystemZTargetLowering::isLegalAddressingMode(const AddrMode &AM,
501 Type *Ty) const {
502 // Punt on globals for now, although they can be used in limited
503 // RELATIVE LONG cases.
504 if (AM.BaseGV)
505 return false;
506
507 // Require a 20-bit signed offset.
508 if (!isInt<20>(AM.BaseOffs))
509 return false;
510
511 // Indexing is OK but no scale factor can be applied.
512 return AM.Scale == 0 || AM.Scale == 1;
513}
514
Richard Sandiford709bda62013-08-19 12:42:31 +0000515bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
516 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
517 return false;
518 unsigned FromBits = FromType->getPrimitiveSizeInBits();
519 unsigned ToBits = ToType->getPrimitiveSizeInBits();
520 return FromBits > ToBits;
521}
522
523bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
524 if (!FromVT.isInteger() || !ToVT.isInteger())
525 return false;
526 unsigned FromBits = FromVT.getSizeInBits();
527 unsigned ToBits = ToVT.getSizeInBits();
528 return FromBits > ToBits;
529}
530
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000531//===----------------------------------------------------------------------===//
532// Inline asm support
533//===----------------------------------------------------------------------===//
534
535TargetLowering::ConstraintType
536SystemZTargetLowering::getConstraintType(const std::string &Constraint) const {
537 if (Constraint.size() == 1) {
538 switch (Constraint[0]) {
539 case 'a': // Address register
540 case 'd': // Data register (equivalent to 'r')
541 case 'f': // Floating-point register
Richard Sandiford0755c932013-10-01 11:26:28 +0000542 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000543 case 'r': // General-purpose register
544 return C_RegisterClass;
545
546 case 'Q': // Memory with base and unsigned 12-bit displacement
547 case 'R': // Likewise, plus an index
548 case 'S': // Memory with base and signed 20-bit displacement
549 case 'T': // Likewise, plus an index
550 case 'm': // Equivalent to 'T'.
551 return C_Memory;
552
553 case 'I': // Unsigned 8-bit constant
554 case 'J': // Unsigned 12-bit constant
555 case 'K': // Signed 16-bit constant
556 case 'L': // Signed 20-bit displacement (on all targets we support)
557 case 'M': // 0x7fffffff
558 return C_Other;
559
560 default:
561 break;
562 }
563 }
564 return TargetLowering::getConstraintType(Constraint);
565}
566
567TargetLowering::ConstraintWeight SystemZTargetLowering::
568getSingleConstraintMatchWeight(AsmOperandInfo &info,
569 const char *constraint) const {
570 ConstraintWeight weight = CW_Invalid;
571 Value *CallOperandVal = info.CallOperandVal;
572 // If we don't have a value, we can't do a match,
573 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +0000574 if (!CallOperandVal)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000575 return CW_Default;
576 Type *type = CallOperandVal->getType();
577 // Look at the constraint type.
578 switch (*constraint) {
579 default:
580 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
581 break;
582
583 case 'a': // Address register
584 case 'd': // Data register (equivalent to 'r')
Richard Sandiford0755c932013-10-01 11:26:28 +0000585 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000586 case 'r': // General-purpose register
587 if (CallOperandVal->getType()->isIntegerTy())
588 weight = CW_Register;
589 break;
590
591 case 'f': // Floating-point register
592 if (type->isFloatingPointTy())
593 weight = CW_Register;
594 break;
595
596 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000597 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000598 if (isUInt<8>(C->getZExtValue()))
599 weight = CW_Constant;
600 break;
601
602 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000603 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000604 if (isUInt<12>(C->getZExtValue()))
605 weight = CW_Constant;
606 break;
607
608 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000609 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000610 if (isInt<16>(C->getSExtValue()))
611 weight = CW_Constant;
612 break;
613
614 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000615 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000616 if (isInt<20>(C->getSExtValue()))
617 weight = CW_Constant;
618 break;
619
620 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000621 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000622 if (C->getZExtValue() == 0x7fffffff)
623 weight = CW_Constant;
624 break;
625 }
626 return weight;
627}
628
Richard Sandifordb8204052013-07-12 09:08:12 +0000629// Parse a "{tNNN}" register constraint for which the register type "t"
630// has already been verified. MC is the class associated with "t" and
631// Map maps 0-based register numbers to LLVM register numbers.
632static std::pair<unsigned, const TargetRegisterClass *>
633parseRegisterNumber(const std::string &Constraint,
634 const TargetRegisterClass *RC, const unsigned *Map) {
635 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
636 if (isdigit(Constraint[2])) {
637 std::string Suffix(Constraint.data() + 2, Constraint.size() - 2);
638 unsigned Index = atoi(Suffix.c_str());
639 if (Index < 16 && Map[Index])
640 return std::make_pair(Map[Index], RC);
641 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000642 return std::make_pair(0U, nullptr);
Richard Sandifordb8204052013-07-12 09:08:12 +0000643}
644
Eric Christopher11e4df72015-02-26 22:38:43 +0000645std::pair<unsigned, const TargetRegisterClass *>
646SystemZTargetLowering::getRegForInlineAsmConstraint(
647 const TargetRegisterInfo *TRI, const std::string &Constraint,
648 MVT VT) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000649 if (Constraint.size() == 1) {
650 // GCC Constraint Letters
651 switch (Constraint[0]) {
652 default: break;
653 case 'd': // Data register (equivalent to 'r')
654 case 'r': // General-purpose register
655 if (VT == MVT::i64)
656 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
657 else if (VT == MVT::i128)
658 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
659 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
660
661 case 'a': // Address register
662 if (VT == MVT::i64)
663 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
664 else if (VT == MVT::i128)
665 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
666 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
667
Richard Sandiford0755c932013-10-01 11:26:28 +0000668 case 'h': // High-part register (an LLVM extension)
669 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
670
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000671 case 'f': // Floating-point register
672 if (VT == MVT::f64)
673 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
674 else if (VT == MVT::f128)
675 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
676 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
677 }
678 }
Richard Sandifordb8204052013-07-12 09:08:12 +0000679 if (Constraint[0] == '{') {
680 // We need to override the default register parsing for GPRs and FPRs
681 // because the interpretation depends on VT. The internal names of
682 // the registers are also different from the external names
683 // (F0D and F0S instead of F0, etc.).
684 if (Constraint[1] == 'r') {
685 if (VT == MVT::i32)
686 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
687 SystemZMC::GR32Regs);
688 if (VT == MVT::i128)
689 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
690 SystemZMC::GR128Regs);
691 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
692 SystemZMC::GR64Regs);
693 }
694 if (Constraint[1] == 'f') {
695 if (VT == MVT::f32)
696 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
697 SystemZMC::FP32Regs);
698 if (VT == MVT::f128)
699 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
700 SystemZMC::FP128Regs);
701 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
702 SystemZMC::FP64Regs);
703 }
704 }
Eric Christopher11e4df72015-02-26 22:38:43 +0000705 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000706}
707
708void SystemZTargetLowering::
709LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
710 std::vector<SDValue> &Ops,
711 SelectionDAG &DAG) const {
712 // Only support length 1 constraints for now.
713 if (Constraint.length() == 1) {
714 switch (Constraint[0]) {
715 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000716 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000717 if (isUInt<8>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000718 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000719 Op.getValueType()));
720 return;
721
722 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000723 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000724 if (isUInt<12>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000725 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000726 Op.getValueType()));
727 return;
728
729 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000730 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000731 if (isInt<16>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000732 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000733 Op.getValueType()));
734 return;
735
736 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000737 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000738 if (isInt<20>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000739 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000740 Op.getValueType()));
741 return;
742
743 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000744 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000745 if (C->getZExtValue() == 0x7fffffff)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000746 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000747 Op.getValueType()));
748 return;
749 }
750 }
751 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
752}
753
754//===----------------------------------------------------------------------===//
755// Calling conventions
756//===----------------------------------------------------------------------===//
757
758#include "SystemZGenCallingConv.inc"
759
Richard Sandiford709bda62013-08-19 12:42:31 +0000760bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
761 Type *ToType) const {
762 return isTruncateFree(FromType, ToType);
763}
764
765bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
766 if (!CI->isTailCall())
767 return false;
768 return true;
769}
770
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000771// Value is a value that has been passed to us in the location described by VA
772// (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
773// any loads onto Chain.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000774static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000775 CCValAssign &VA, SDValue Chain,
776 SDValue Value) {
777 // If the argument has been promoted from a smaller type, insert an
778 // assertion to capture this.
779 if (VA.getLocInfo() == CCValAssign::SExt)
780 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
781 DAG.getValueType(VA.getValVT()));
782 else if (VA.getLocInfo() == CCValAssign::ZExt)
783 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
784 DAG.getValueType(VA.getValVT()));
785
786 if (VA.isExtInLoc())
787 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
788 else if (VA.getLocInfo() == CCValAssign::Indirect)
789 Value = DAG.getLoad(VA.getValVT(), DL, Chain, Value,
790 MachinePointerInfo(), false, false, false, 0);
791 else
792 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
793 return Value;
794}
795
796// Value is a value of type VA.getValVT() that we need to copy into
797// the location described by VA. Return a copy of Value converted to
798// VA.getValVT(). The caller is responsible for handling indirect values.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000799static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000800 CCValAssign &VA, SDValue Value) {
801 switch (VA.getLocInfo()) {
802 case CCValAssign::SExt:
803 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
804 case CCValAssign::ZExt:
805 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
806 case CCValAssign::AExt:
807 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
808 case CCValAssign::Full:
809 return Value;
810 default:
811 llvm_unreachable("Unhandled getLocInfo()");
812 }
813}
814
815SDValue SystemZTargetLowering::
816LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
817 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000818 SDLoc DL, SelectionDAG &DAG,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000819 SmallVectorImpl<SDValue> &InVals) const {
820 MachineFunction &MF = DAG.getMachineFunction();
821 MachineFrameInfo *MFI = MF.getFrameInfo();
822 MachineRegisterInfo &MRI = MF.getRegInfo();
823 SystemZMachineFunctionInfo *FuncInfo =
Eric Christophera6734172015-01-31 00:06:45 +0000824 MF.getInfo<SystemZMachineFunctionInfo>();
825 auto *TFL =
826 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000827
828 // Assign locations to all of the incoming arguments.
829 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000830 SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000831 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
832
833 unsigned NumFixedGPRs = 0;
834 unsigned NumFixedFPRs = 0;
835 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
836 SDValue ArgValue;
837 CCValAssign &VA = ArgLocs[I];
838 EVT LocVT = VA.getLocVT();
839 if (VA.isRegLoc()) {
840 // Arguments passed in registers
841 const TargetRegisterClass *RC;
842 switch (LocVT.getSimpleVT().SimpleTy) {
843 default:
844 // Integers smaller than i64 should be promoted to i64.
845 llvm_unreachable("Unexpected argument type");
846 case MVT::i32:
847 NumFixedGPRs += 1;
848 RC = &SystemZ::GR32BitRegClass;
849 break;
850 case MVT::i64:
851 NumFixedGPRs += 1;
852 RC = &SystemZ::GR64BitRegClass;
853 break;
854 case MVT::f32:
855 NumFixedFPRs += 1;
856 RC = &SystemZ::FP32BitRegClass;
857 break;
858 case MVT::f64:
859 NumFixedFPRs += 1;
860 RC = &SystemZ::FP64BitRegClass;
861 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000862 case MVT::v16i8:
863 case MVT::v8i16:
864 case MVT::v4i32:
865 case MVT::v2i64:
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000866 case MVT::v4f32:
Ulrich Weigandcd808232015-05-05 19:26:48 +0000867 case MVT::v2f64:
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000868 RC = &SystemZ::VR128BitRegClass;
869 break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000870 }
871
872 unsigned VReg = MRI.createVirtualRegister(RC);
873 MRI.addLiveIn(VA.getLocReg(), VReg);
874 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
875 } else {
876 assert(VA.isMemLoc() && "Argument not register or memory");
877
878 // Create the frame index object for this incoming parameter.
879 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
880 VA.getLocMemOffset(), true);
881
882 // Create the SelectionDAG nodes corresponding to a load
883 // from this parameter. Unpromoted ints and floats are
884 // passed as right-justified 8-byte values.
885 EVT PtrVT = getPointerTy();
886 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
887 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000888 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
889 DAG.getIntPtrConstant(4, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000890 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
891 MachinePointerInfo::getFixedStack(FI),
892 false, false, false, 0);
893 }
894
895 // Convert the value of the argument register into the value that's
896 // being passed.
897 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
898 }
899
900 if (IsVarArg) {
901 // Save the number of non-varargs registers for later use by va_start, etc.
902 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
903 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
904
905 // Likewise the address (in the form of a frame index) of where the
906 // first stack vararg would be. The 1-byte size here is arbitrary.
907 int64_t StackSize = CCInfo.getNextStackOffset();
908 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true));
909
910 // ...and a similar frame index for the caller-allocated save area
911 // that will be used to store the incoming registers.
912 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
913 unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true);
914 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
915
916 // Store the FPR varargs in the reserved frame slots. (We store the
917 // GPRs as part of the prologue.)
918 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
919 SDValue MemOps[SystemZ::NumArgFPRs];
920 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
921 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
922 int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true);
923 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
924 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
925 &SystemZ::FP64BitRegClass);
926 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
927 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
928 MachinePointerInfo::getFixedStack(FI),
929 false, false, 0);
930
931 }
932 // Join the stores, which are independent of one another.
933 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Craig Topper2d2aa0c2014-04-30 07:17:30 +0000934 makeArrayRef(&MemOps[NumFixedFPRs],
935 SystemZ::NumArgFPRs-NumFixedFPRs));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000936 }
937 }
938
939 return Chain;
940}
941
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000942static bool canUseSiblingCall(const CCState &ArgCCInfo,
Richard Sandiford709bda62013-08-19 12:42:31 +0000943 SmallVectorImpl<CCValAssign> &ArgLocs) {
944 // Punt if there are any indirect or stack arguments, or if the call
945 // needs the call-saved argument register R6.
946 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
947 CCValAssign &VA = ArgLocs[I];
948 if (VA.getLocInfo() == CCValAssign::Indirect)
949 return false;
950 if (!VA.isRegLoc())
951 return false;
952 unsigned Reg = VA.getLocReg();
Richard Sandiford0755c932013-10-01 11:26:28 +0000953 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
Richard Sandiford709bda62013-08-19 12:42:31 +0000954 return false;
955 }
956 return true;
957}
958
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000959SDValue
960SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
961 SmallVectorImpl<SDValue> &InVals) const {
962 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000963 SDLoc &DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000964 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
965 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
966 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000967 SDValue Chain = CLI.Chain;
968 SDValue Callee = CLI.Callee;
Richard Sandiford709bda62013-08-19 12:42:31 +0000969 bool &IsTailCall = CLI.IsTailCall;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000970 CallingConv::ID CallConv = CLI.CallConv;
971 bool IsVarArg = CLI.IsVarArg;
972 MachineFunction &MF = DAG.getMachineFunction();
973 EVT PtrVT = getPointerTy();
974
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000975 // Analyze the operands of the call, assigning locations to each operand.
976 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000977 SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000978 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
979
Richard Sandiford709bda62013-08-19 12:42:31 +0000980 // We don't support GuaranteedTailCallOpt, only automatically-detected
981 // sibling calls.
982 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs))
983 IsTailCall = false;
984
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000985 // Get a count of how many bytes are to be pushed on the stack.
986 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
987
988 // Mark the start of the call.
Richard Sandiford709bda62013-08-19 12:42:31 +0000989 if (!IsTailCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000990 Chain = DAG.getCALLSEQ_START(Chain,
991 DAG.getConstant(NumBytes, DL, PtrVT, true),
Richard Sandiford709bda62013-08-19 12:42:31 +0000992 DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000993
994 // Copy argument values to their designated locations.
995 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
996 SmallVector<SDValue, 8> MemOpChains;
997 SDValue StackPtr;
998 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
999 CCValAssign &VA = ArgLocs[I];
1000 SDValue ArgValue = OutVals[I];
1001
1002 if (VA.getLocInfo() == CCValAssign::Indirect) {
1003 // Store the argument in a stack slot and pass its address.
1004 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
1005 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
1006 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, SpillSlot,
1007 MachinePointerInfo::getFixedStack(FI),
1008 false, false, 0));
1009 ArgValue = SpillSlot;
1010 } else
1011 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
1012
1013 if (VA.isRegLoc())
1014 // Queue up the argument copies and emit them at the end.
1015 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1016 else {
1017 assert(VA.isMemLoc() && "Argument not register or memory");
1018
1019 // Work out the address of the stack slot. Unpromoted ints and
1020 // floats are passed as right-justified 8-byte values.
1021 if (!StackPtr.getNode())
1022 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
1023 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
1024 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
1025 Offset += 4;
1026 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001027 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001028
1029 // Emit the store.
1030 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address,
1031 MachinePointerInfo(),
1032 false, false, 0));
1033 }
1034 }
1035
1036 // Join the stores, which are independent of one another.
1037 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001038 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001039
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001040 // Accept direct calls by converting symbolic call addresses to the
Richard Sandiford709bda62013-08-19 12:42:31 +00001041 // associated Target* opcodes. Force %r1 to be used for indirect
1042 // tail calls.
1043 SDValue Glue;
Richard Sandiford21f5d682014-03-06 11:22:58 +00001044 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001045 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
1046 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford21f5d682014-03-06 11:22:58 +00001047 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001048 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
1049 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford709bda62013-08-19 12:42:31 +00001050 } else if (IsTailCall) {
1051 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
1052 Glue = Chain.getValue(1);
1053 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
1054 }
1055
1056 // Build a sequence of copy-to-reg nodes, chained and glued together.
1057 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
1058 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
1059 RegsToPass[I].second, Glue);
1060 Glue = Chain.getValue(1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001061 }
1062
1063 // The first call operand is the chain and the second is the target address.
1064 SmallVector<SDValue, 8> Ops;
1065 Ops.push_back(Chain);
1066 Ops.push_back(Callee);
1067
1068 // Add argument registers to the end of the list so that they are
1069 // known live into the call.
1070 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
1071 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
1072 RegsToPass[I].second.getValueType()));
1073
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001074 // Add a register mask operand representing the call-preserved registers.
Eric Christophera6734172015-01-31 00:06:45 +00001075 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00001076 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001077 assert(Mask && "Missing call preserved mask for calling convention");
1078 Ops.push_back(DAG.getRegisterMask(Mask));
1079
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001080 // Glue the call to the argument copies, if any.
1081 if (Glue.getNode())
1082 Ops.push_back(Glue);
1083
1084 // Emit the call.
1085 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Richard Sandiford709bda62013-08-19 12:42:31 +00001086 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001087 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
1088 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001089 Glue = Chain.getValue(1);
1090
1091 // Mark the end of the call, which is glued to the call itself.
1092 Chain = DAG.getCALLSEQ_END(Chain,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001093 DAG.getConstant(NumBytes, DL, PtrVT, true),
1094 DAG.getConstant(0, DL, PtrVT, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001095 Glue, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001096 Glue = Chain.getValue(1);
1097
1098 // Assign locations to each value returned by this call.
1099 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001100 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001101 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
1102
1103 // Copy all of the result registers out of their specified physreg.
1104 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1105 CCValAssign &VA = RetLocs[I];
1106
1107 // Copy the value out, gluing the copy to the end of the call sequence.
1108 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
1109 VA.getLocVT(), Glue);
1110 Chain = RetValue.getValue(1);
1111 Glue = RetValue.getValue(2);
1112
1113 // Convert the value of the return register into the value that's
1114 // being returned.
1115 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
1116 }
1117
1118 return Chain;
1119}
1120
1121SDValue
1122SystemZTargetLowering::LowerReturn(SDValue Chain,
1123 CallingConv::ID CallConv, bool IsVarArg,
1124 const SmallVectorImpl<ISD::OutputArg> &Outs,
1125 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001126 SDLoc DL, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001127 MachineFunction &MF = DAG.getMachineFunction();
1128
1129 // Assign locations to each returned value.
1130 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001131 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001132 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
1133
1134 // Quick exit for void returns
1135 if (RetLocs.empty())
1136 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
1137
1138 // Copy the result values into the output registers.
1139 SDValue Glue;
1140 SmallVector<SDValue, 4> RetOps;
1141 RetOps.push_back(Chain);
1142 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1143 CCValAssign &VA = RetLocs[I];
1144 SDValue RetValue = OutVals[I];
1145
1146 // Make the return register live on exit.
1147 assert(VA.isRegLoc() && "Can only return in registers!");
1148
1149 // Promote the value as required.
1150 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
1151
1152 // Chain and glue the copies together.
1153 unsigned Reg = VA.getLocReg();
1154 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
1155 Glue = Chain.getValue(1);
1156 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
1157 }
1158
1159 // Update chain and glue.
1160 RetOps[0] = Chain;
1161 if (Glue.getNode())
1162 RetOps.push_back(Glue);
1163
Craig Topper48d114b2014-04-26 18:35:24 +00001164 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001165}
1166
Richard Sandiford9afe6132013-12-10 10:36:34 +00001167SDValue SystemZTargetLowering::
1168prepareVolatileOrAtomicLoad(SDValue Chain, SDLoc DL, SelectionDAG &DAG) const {
1169 return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain);
1170}
1171
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001172// Return true if Op is an intrinsic node with chain that returns the CC value
1173// as its only (other) argument. Provide the associated SystemZISD opcode and
1174// the mask of valid CC values if so.
1175static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode,
1176 unsigned &CCValid) {
1177 unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1178 switch (Id) {
1179 case Intrinsic::s390_tbegin:
1180 Opcode = SystemZISD::TBEGIN;
1181 CCValid = SystemZ::CCMASK_TBEGIN;
1182 return true;
1183
1184 case Intrinsic::s390_tbegin_nofloat:
1185 Opcode = SystemZISD::TBEGIN_NOFLOAT;
1186 CCValid = SystemZ::CCMASK_TBEGIN;
1187 return true;
1188
1189 case Intrinsic::s390_tend:
1190 Opcode = SystemZISD::TEND;
1191 CCValid = SystemZ::CCMASK_TEND;
1192 return true;
1193
1194 default:
1195 return false;
1196 }
1197}
1198
1199// Emit an intrinsic with chain with a glued value instead of its CC result.
1200static SDValue emitIntrinsicWithChainAndGlue(SelectionDAG &DAG, SDValue Op,
1201 unsigned Opcode) {
1202 // Copy all operands except the intrinsic ID.
1203 unsigned NumOps = Op.getNumOperands();
1204 SmallVector<SDValue, 6> Ops;
1205 Ops.reserve(NumOps - 1);
1206 Ops.push_back(Op.getOperand(0));
1207 for (unsigned I = 2; I < NumOps; ++I)
1208 Ops.push_back(Op.getOperand(I));
1209
1210 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
1211 SDVTList RawVTs = DAG.getVTList(MVT::Other, MVT::Glue);
1212 SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1213 SDValue OldChain = SDValue(Op.getNode(), 1);
1214 SDValue NewChain = SDValue(Intr.getNode(), 0);
1215 DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain);
1216 return Intr;
1217}
1218
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001219// CC is a comparison that will be implemented using an integer or
1220// floating-point comparison. Return the condition code mask for
1221// a branch on true. In the integer case, CCMASK_CMP_UO is set for
1222// unsigned comparisons and clear for signed ones. In the floating-point
1223// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1224static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1225#define CONV(X) \
1226 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1227 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1228 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1229
1230 switch (CC) {
1231 default:
1232 llvm_unreachable("Invalid integer condition!");
1233
1234 CONV(EQ);
1235 CONV(NE);
1236 CONV(GT);
1237 CONV(GE);
1238 CONV(LT);
1239 CONV(LE);
1240
1241 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1242 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1243 }
1244#undef CONV
1245}
1246
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001247// Return a sequence for getting a 1 from an IPM result when CC has a
1248// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1249// The handling of CC values outside CCValid doesn't matter.
1250static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1251 // Deal with cases where the result can be taken directly from a bit
1252 // of the IPM result.
1253 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1254 return IPMConversion(0, 0, SystemZ::IPM_CC);
1255 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1256 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1257
1258 // Deal with cases where we can add a value to force the sign bit
1259 // to contain the right value. Putting the bit in 31 means we can
1260 // use SRL rather than RISBG(L), and also makes it easier to get a
1261 // 0/-1 value, so it has priority over the other tests below.
1262 //
1263 // These sequences rely on the fact that the upper two bits of the
1264 // IPM result are zero.
1265 uint64_t TopBit = uint64_t(1) << 31;
1266 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1267 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1268 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1269 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1270 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1271 | SystemZ::CCMASK_1
1272 | SystemZ::CCMASK_2)))
1273 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1274 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1275 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1276 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1277 | SystemZ::CCMASK_2
1278 | SystemZ::CCMASK_3)))
1279 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1280
1281 // Next try inverting the value and testing a bit. 0/1 could be
1282 // handled this way too, but we dealt with that case above.
1283 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1284 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1285
1286 // Handle cases where adding a value forces a non-sign bit to contain
1287 // the right value.
1288 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1289 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1290 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1291 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1292
Alp Tokercb402912014-01-24 17:20:08 +00001293 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001294 // can be done by inverting the low CC bit and applying one of the
1295 // sign-based extractions above.
1296 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1297 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1298 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1299 return IPMConversion(1 << SystemZ::IPM_CC,
1300 TopBit - (3 << SystemZ::IPM_CC), 31);
1301 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1302 | SystemZ::CCMASK_1
1303 | SystemZ::CCMASK_3)))
1304 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1305 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1306 | SystemZ::CCMASK_2
1307 | SystemZ::CCMASK_3)))
1308 return IPMConversion(1 << SystemZ::IPM_CC,
1309 TopBit - (1 << SystemZ::IPM_CC), 31);
1310
1311 llvm_unreachable("Unexpected CC combination");
1312}
1313
Richard Sandifordd420f732013-12-13 15:28:45 +00001314// If C can be converted to a comparison against zero, adjust the operands
Richard Sandiforda0757082013-08-01 10:29:45 +00001315// as necessary.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001316static void adjustZeroCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001317 if (C.ICmpType == SystemZICMP::UnsignedOnly)
Richard Sandiforda0757082013-08-01 10:29:45 +00001318 return;
1319
Richard Sandiford21f5d682014-03-06 11:22:58 +00001320 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
Richard Sandiforda0757082013-08-01 10:29:45 +00001321 if (!ConstOp1)
1322 return;
1323
1324 int64_t Value = ConstOp1->getSExtValue();
Richard Sandifordd420f732013-12-13 15:28:45 +00001325 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1326 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1327 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1328 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1329 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001330 C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType());
Richard Sandiforda0757082013-08-01 10:29:45 +00001331 }
1332}
1333
Richard Sandifordd420f732013-12-13 15:28:45 +00001334// If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1335// adjust the operands as necessary.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001336static void adjustSubwordCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001337 // For us to make any changes, it must a comparison between a single-use
1338 // load and a constant.
Richard Sandifordd420f732013-12-13 15:28:45 +00001339 if (!C.Op0.hasOneUse() ||
1340 C.Op0.getOpcode() != ISD::LOAD ||
1341 C.Op1.getOpcode() != ISD::Constant)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001342 return;
1343
1344 // We must have an 8- or 16-bit load.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001345 auto *Load = cast<LoadSDNode>(C.Op0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001346 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1347 if (NumBits != 8 && NumBits != 16)
1348 return;
1349
1350 // The load must be an extending one and the constant must be within the
1351 // range of the unextended value.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001352 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001353 uint64_t Value = ConstOp1->getZExtValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001354 uint64_t Mask = (1 << NumBits) - 1;
1355 if (Load->getExtensionType() == ISD::SEXTLOAD) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001356 // Make sure that ConstOp1 is in range of C.Op0.
1357 int64_t SignedValue = ConstOp1->getSExtValue();
1358 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001359 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001360 if (C.ICmpType != SystemZICMP::SignedOnly) {
1361 // Unsigned comparison between two sign-extended values is equivalent
1362 // to unsigned comparison between two zero-extended values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001363 Value &= Mask;
Richard Sandifordd420f732013-12-13 15:28:45 +00001364 } else if (NumBits == 8) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001365 // Try to treat the comparison as unsigned, so that we can use CLI.
1366 // Adjust CCMask and Value as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001367 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001368 // Test whether the high bit of the byte is set.
Richard Sandifordd420f732013-12-13 15:28:45 +00001369 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1370 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001371 // Test whether the high bit of the byte is clear.
Richard Sandifordd420f732013-12-13 15:28:45 +00001372 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001373 else
1374 // No instruction exists for this combination.
1375 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001376 C.ICmpType = SystemZICMP::UnsignedOnly;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001377 }
1378 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1379 if (Value > Mask)
1380 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001381 assert(C.ICmpType == SystemZICMP::Any &&
1382 "Signedness shouldn't matter here.");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001383 } else
1384 return;
1385
1386 // Make sure that the first operand is an i32 of the right extension type.
Richard Sandifordd420f732013-12-13 15:28:45 +00001387 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1388 ISD::SEXTLOAD :
1389 ISD::ZEXTLOAD);
1390 if (C.Op0.getValueType() != MVT::i32 ||
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001391 Load->getExtensionType() != ExtType)
Richard Sandifordd420f732013-12-13 15:28:45 +00001392 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32,
1393 Load->getChain(), Load->getBasePtr(),
1394 Load->getPointerInfo(), Load->getMemoryVT(),
1395 Load->isVolatile(), Load->isNonTemporal(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00001396 Load->isInvariant(), Load->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001397
1398 // Make sure that the second operand is an i32 with the right value.
Richard Sandifordd420f732013-12-13 15:28:45 +00001399 if (C.Op1.getValueType() != MVT::i32 ||
1400 Value != ConstOp1->getZExtValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001401 C.Op1 = DAG.getConstant(Value, DL, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001402}
1403
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001404// Return true if Op is either an unextended load, or a load suitable
1405// for integer register-memory comparisons of type ICmpType.
1406static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001407 auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001408 if (Load) {
1409 // There are no instructions to compare a register with a memory byte.
1410 if (Load->getMemoryVT() == MVT::i8)
1411 return false;
1412 // Otherwise decide on extension type.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001413 switch (Load->getExtensionType()) {
1414 case ISD::NON_EXTLOAD:
Richard Sandiford24e597b2013-08-23 11:27:19 +00001415 return true;
1416 case ISD::SEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001417 return ICmpType != SystemZICMP::UnsignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001418 case ISD::ZEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001419 return ICmpType != SystemZICMP::SignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001420 default:
1421 break;
1422 }
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001423 }
Richard Sandiford24e597b2013-08-23 11:27:19 +00001424 return false;
1425}
1426
Richard Sandifordd420f732013-12-13 15:28:45 +00001427// Return true if it is better to swap the operands of C.
1428static bool shouldSwapCmpOperands(const Comparison &C) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001429 // Leave f128 comparisons alone, since they have no memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001430 if (C.Op0.getValueType() == MVT::f128)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001431 return false;
1432
1433 // Always keep a floating-point constant second, since comparisons with
1434 // zero can use LOAD TEST and comparisons with other constants make a
1435 // natural memory operand.
Richard Sandifordd420f732013-12-13 15:28:45 +00001436 if (isa<ConstantFPSDNode>(C.Op1))
Richard Sandiford24e597b2013-08-23 11:27:19 +00001437 return false;
1438
1439 // Never swap comparisons with zero since there are many ways to optimize
1440 // those later.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001441 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001442 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001443 return false;
1444
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001445 // Also keep natural memory operands second if the loaded value is
1446 // only used here. Several comparisons have memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001447 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001448 return false;
1449
Richard Sandiford24e597b2013-08-23 11:27:19 +00001450 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1451 // In that case we generally prefer the memory to be second.
Richard Sandifordd420f732013-12-13 15:28:45 +00001452 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001453 // The only exceptions are when the second operand is a constant and
1454 // we can use things like CHHSI.
Richard Sandifordd420f732013-12-13 15:28:45 +00001455 if (!ConstOp1)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001456 return true;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001457 // The unsigned memory-immediate instructions can handle 16-bit
1458 // unsigned integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001459 if (C.ICmpType != SystemZICMP::SignedOnly &&
1460 isUInt<16>(ConstOp1->getZExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001461 return false;
1462 // The signed memory-immediate instructions can handle 16-bit
1463 // signed integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001464 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1465 isInt<16>(ConstOp1->getSExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001466 return false;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001467 return true;
1468 }
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001469
1470 // Try to promote the use of CGFR and CLGFR.
Richard Sandifordd420f732013-12-13 15:28:45 +00001471 unsigned Opcode0 = C.Op0.getOpcode();
1472 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001473 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001474 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001475 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001476 if (C.ICmpType != SystemZICMP::SignedOnly &&
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001477 Opcode0 == ISD::AND &&
Richard Sandifordd420f732013-12-13 15:28:45 +00001478 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1479 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001480 return true;
1481
Richard Sandiford24e597b2013-08-23 11:27:19 +00001482 return false;
1483}
1484
Richard Sandiford73170f82013-12-11 11:45:08 +00001485// Return a version of comparison CC mask CCMask in which the LT and GT
1486// actions are swapped.
1487static unsigned reverseCCMask(unsigned CCMask) {
1488 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1489 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1490 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1491 (CCMask & SystemZ::CCMASK_CMP_UO));
1492}
1493
Richard Sandiford0847c452013-12-13 15:50:30 +00001494// Check whether C tests for equality between X and Y and whether X - Y
1495// or Y - X is also computed. In that case it's better to compare the
1496// result of the subtraction against zero.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001497static void adjustForSubtraction(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001498 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1499 C.CCMask == SystemZ::CCMASK_CMP_NE) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001500 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001501 SDNode *N = *I;
1502 if (N->getOpcode() == ISD::SUB &&
1503 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1504 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1505 C.Op0 = SDValue(N, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001506 C.Op1 = DAG.getConstant(0, DL, N->getValueType(0));
Richard Sandiford0847c452013-12-13 15:50:30 +00001507 return;
1508 }
1509 }
1510 }
1511}
1512
Richard Sandifordd420f732013-12-13 15:28:45 +00001513// Check whether C compares a floating-point value with zero and if that
1514// floating-point value is also negated. In this case we can use the
1515// negation to set CC, so avoiding separate LOAD AND TEST and
1516// LOAD (NEGATIVE/COMPLEMENT) instructions.
1517static void adjustForFNeg(Comparison &C) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001518 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
Richard Sandiford73170f82013-12-11 11:45:08 +00001519 if (C1 && C1->isZero()) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001520 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford73170f82013-12-11 11:45:08 +00001521 SDNode *N = *I;
1522 if (N->getOpcode() == ISD::FNEG) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001523 C.Op0 = SDValue(N, 0);
1524 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford73170f82013-12-11 11:45:08 +00001525 return;
1526 }
1527 }
1528 }
1529}
1530
Richard Sandifordd420f732013-12-13 15:28:45 +00001531// Check whether C compares (shl X, 32) with 0 and whether X is
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001532// also sign-extended. In that case it is better to test the result
1533// of the sign extension using LTGFR.
1534//
1535// This case is important because InstCombine transforms a comparison
1536// with (sext (trunc X)) into a comparison with (shl X, 32).
Richard Sandifordd420f732013-12-13 15:28:45 +00001537static void adjustForLTGFR(Comparison &C) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001538 // Check for a comparison between (shl X, 32) and 0.
Richard Sandifordd420f732013-12-13 15:28:45 +00001539 if (C.Op0.getOpcode() == ISD::SHL &&
1540 C.Op0.getValueType() == MVT::i64 &&
1541 C.Op1.getOpcode() == ISD::Constant &&
1542 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001543 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001544 if (C1 && C1->getZExtValue() == 32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001545 SDValue ShlOp0 = C.Op0.getOperand(0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001546 // See whether X has any SIGN_EXTEND_INREG uses.
Richard Sandiford28c111e2014-03-06 11:00:15 +00001547 for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001548 SDNode *N = *I;
1549 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
1550 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001551 C.Op0 = SDValue(N, 0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001552 return;
1553 }
1554 }
1555 }
1556 }
1557}
1558
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001559// If C compares the truncation of an extending load, try to compare
1560// the untruncated value instead. This exposes more opportunities to
1561// reuse CC.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001562static void adjustICmpTruncate(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001563 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
1564 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
1565 C.Op1.getOpcode() == ISD::Constant &&
1566 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001567 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001568 if (L->getMemoryVT().getStoreSizeInBits()
1569 <= C.Op0.getValueType().getSizeInBits()) {
1570 unsigned Type = L->getExtensionType();
1571 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
1572 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
1573 C.Op0 = C.Op0.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001574 C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType());
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001575 }
1576 }
1577 }
1578}
1579
Richard Sandiford030c1652013-09-13 09:09:50 +00001580// Return true if shift operation N has an in-range constant shift value.
1581// Store it in ShiftVal if so.
1582static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001583 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
Richard Sandiford030c1652013-09-13 09:09:50 +00001584 if (!Shift)
1585 return false;
1586
1587 uint64_t Amount = Shift->getZExtValue();
1588 if (Amount >= N.getValueType().getSizeInBits())
1589 return false;
1590
1591 ShiftVal = Amount;
1592 return true;
1593}
1594
1595// Check whether an AND with Mask is suitable for a TEST UNDER MASK
1596// instruction and whether the CC value is descriptive enough to handle
1597// a comparison of type Opcode between the AND result and CmpVal.
1598// CCMask says which comparison result is being tested and BitSize is
1599// the number of bits in the operands. If TEST UNDER MASK can be used,
1600// return the corresponding CC mask, otherwise return 0.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001601static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
1602 uint64_t Mask, uint64_t CmpVal,
1603 unsigned ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001604 assert(Mask != 0 && "ANDs with zero should have been removed by now");
1605
Richard Sandiford030c1652013-09-13 09:09:50 +00001606 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
1607 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
1608 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
1609 return 0;
1610
Richard Sandiford113c8702013-09-03 15:38:35 +00001611 // Work out the masks for the lowest and highest bits.
1612 unsigned HighShift = 63 - countLeadingZeros(Mask);
1613 uint64_t High = uint64_t(1) << HighShift;
1614 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
1615
1616 // Signed ordered comparisons are effectively unsigned if the sign
1617 // bit is dropped.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001618 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
Richard Sandiford113c8702013-09-03 15:38:35 +00001619
1620 // Check for equality comparisons with 0, or the equivalent.
1621 if (CmpVal == 0) {
1622 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1623 return SystemZ::CCMASK_TM_ALL_0;
1624 if (CCMask == SystemZ::CCMASK_CMP_NE)
1625 return SystemZ::CCMASK_TM_SOME_1;
1626 }
1627 if (EffectivelyUnsigned && CmpVal <= Low) {
1628 if (CCMask == SystemZ::CCMASK_CMP_LT)
1629 return SystemZ::CCMASK_TM_ALL_0;
1630 if (CCMask == SystemZ::CCMASK_CMP_GE)
1631 return SystemZ::CCMASK_TM_SOME_1;
1632 }
1633 if (EffectivelyUnsigned && CmpVal < Low) {
1634 if (CCMask == SystemZ::CCMASK_CMP_LE)
1635 return SystemZ::CCMASK_TM_ALL_0;
1636 if (CCMask == SystemZ::CCMASK_CMP_GT)
1637 return SystemZ::CCMASK_TM_SOME_1;
1638 }
1639
1640 // Check for equality comparisons with the mask, or the equivalent.
1641 if (CmpVal == Mask) {
1642 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1643 return SystemZ::CCMASK_TM_ALL_1;
1644 if (CCMask == SystemZ::CCMASK_CMP_NE)
1645 return SystemZ::CCMASK_TM_SOME_0;
1646 }
1647 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
1648 if (CCMask == SystemZ::CCMASK_CMP_GT)
1649 return SystemZ::CCMASK_TM_ALL_1;
1650 if (CCMask == SystemZ::CCMASK_CMP_LE)
1651 return SystemZ::CCMASK_TM_SOME_0;
1652 }
1653 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
1654 if (CCMask == SystemZ::CCMASK_CMP_GE)
1655 return SystemZ::CCMASK_TM_ALL_1;
1656 if (CCMask == SystemZ::CCMASK_CMP_LT)
1657 return SystemZ::CCMASK_TM_SOME_0;
1658 }
1659
1660 // Check for ordered comparisons with the top bit.
1661 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
1662 if (CCMask == SystemZ::CCMASK_CMP_LE)
1663 return SystemZ::CCMASK_TM_MSB_0;
1664 if (CCMask == SystemZ::CCMASK_CMP_GT)
1665 return SystemZ::CCMASK_TM_MSB_1;
1666 }
1667 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
1668 if (CCMask == SystemZ::CCMASK_CMP_LT)
1669 return SystemZ::CCMASK_TM_MSB_0;
1670 if (CCMask == SystemZ::CCMASK_CMP_GE)
1671 return SystemZ::CCMASK_TM_MSB_1;
1672 }
1673
1674 // If there are just two bits, we can do equality checks for Low and High
1675 // as well.
1676 if (Mask == Low + High) {
1677 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
1678 return SystemZ::CCMASK_TM_MIXED_MSB_0;
1679 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
1680 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
1681 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
1682 return SystemZ::CCMASK_TM_MIXED_MSB_1;
1683 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
1684 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
1685 }
1686
1687 // Looks like we've exhausted our options.
1688 return 0;
1689}
1690
Richard Sandifordd420f732013-12-13 15:28:45 +00001691// See whether C can be implemented as a TEST UNDER MASK instruction.
1692// Update the arguments with the TM version if so.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001693static void adjustForTestUnderMask(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001694 // Check that we have a comparison with a constant.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001695 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001696 if (!ConstOp1)
Richard Sandiford35b9be22013-08-28 10:31:43 +00001697 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001698 uint64_t CmpVal = ConstOp1->getZExtValue();
Richard Sandiford35b9be22013-08-28 10:31:43 +00001699
1700 // Check whether the nonconstant input is an AND with a constant mask.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001701 Comparison NewC(C);
1702 uint64_t MaskVal;
Craig Topper062a2ba2014-04-25 05:30:21 +00001703 ConstantSDNode *Mask = nullptr;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001704 if (C.Op0.getOpcode() == ISD::AND) {
1705 NewC.Op0 = C.Op0.getOperand(0);
1706 NewC.Op1 = C.Op0.getOperand(1);
1707 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
1708 if (!Mask)
1709 return;
1710 MaskVal = Mask->getZExtValue();
1711 } else {
1712 // There is no instruction to compare with a 64-bit immediate
1713 // so use TMHH instead if possible. We need an unsigned ordered
1714 // comparison with an i64 immediate.
1715 if (NewC.Op0.getValueType() != MVT::i64 ||
1716 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
1717 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
1718 NewC.ICmpType == SystemZICMP::SignedOnly)
1719 return;
1720 // Convert LE and GT comparisons into LT and GE.
1721 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
1722 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
1723 if (CmpVal == uint64_t(-1))
1724 return;
1725 CmpVal += 1;
1726 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1727 }
1728 // If the low N bits of Op1 are zero than the low N bits of Op0 can
1729 // be masked off without changing the result.
1730 MaskVal = -(CmpVal & -CmpVal);
1731 NewC.ICmpType = SystemZICMP::UnsignedOnly;
1732 }
Ulrich Weigandb8d76fb2015-03-30 13:46:59 +00001733 if (!MaskVal)
1734 return;
Richard Sandiford35b9be22013-08-28 10:31:43 +00001735
Richard Sandiford113c8702013-09-03 15:38:35 +00001736 // Check whether the combination of mask, comparison value and comparison
1737 // type are suitable.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001738 unsigned BitSize = NewC.Op0.getValueType().getSizeInBits();
Richard Sandiford030c1652013-09-13 09:09:50 +00001739 unsigned NewCCMask, ShiftVal;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001740 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
1741 NewC.Op0.getOpcode() == ISD::SHL &&
1742 isSimpleShift(NewC.Op0, ShiftVal) &&
1743 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
1744 MaskVal >> ShiftVal,
Richard Sandiford030c1652013-09-13 09:09:50 +00001745 CmpVal >> ShiftVal,
1746 SystemZICMP::Any))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001747 NewC.Op0 = NewC.Op0.getOperand(0);
1748 MaskVal >>= ShiftVal;
1749 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
1750 NewC.Op0.getOpcode() == ISD::SRL &&
1751 isSimpleShift(NewC.Op0, ShiftVal) &&
1752 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
Richard Sandiford030c1652013-09-13 09:09:50 +00001753 MaskVal << ShiftVal,
1754 CmpVal << ShiftVal,
1755 SystemZICMP::UnsignedOnly))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001756 NewC.Op0 = NewC.Op0.getOperand(0);
1757 MaskVal <<= ShiftVal;
Richard Sandiford030c1652013-09-13 09:09:50 +00001758 } else {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001759 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
1760 NewC.ICmpType);
Richard Sandiford030c1652013-09-13 09:09:50 +00001761 if (!NewCCMask)
1762 return;
1763 }
Richard Sandiford113c8702013-09-03 15:38:35 +00001764
Richard Sandiford35b9be22013-08-28 10:31:43 +00001765 // Go ahead and make the change.
Richard Sandifordd420f732013-12-13 15:28:45 +00001766 C.Opcode = SystemZISD::TM;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001767 C.Op0 = NewC.Op0;
1768 if (Mask && Mask->getZExtValue() == MaskVal)
1769 C.Op1 = SDValue(Mask, 0);
1770 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001771 C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType());
Richard Sandifordd420f732013-12-13 15:28:45 +00001772 C.CCValid = SystemZ::CCMASK_TM;
1773 C.CCMask = NewCCMask;
Richard Sandiford35b9be22013-08-28 10:31:43 +00001774}
1775
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001776// Return a Comparison that tests the condition-code result of intrinsic
1777// node Call against constant integer CC using comparison code Cond.
1778// Opcode is the opcode of the SystemZISD operation for the intrinsic
1779// and CCValid is the set of possible condition-code results.
1780static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode,
1781 SDValue Call, unsigned CCValid, uint64_t CC,
1782 ISD::CondCode Cond) {
1783 Comparison C(Call, SDValue());
1784 C.Opcode = Opcode;
1785 C.CCValid = CCValid;
1786 if (Cond == ISD::SETEQ)
1787 // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3.
1788 C.CCMask = CC < 4 ? 1 << (3 - CC) : 0;
1789 else if (Cond == ISD::SETNE)
1790 // ...and the inverse of that.
1791 C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1;
1792 else if (Cond == ISD::SETLT || Cond == ISD::SETULT)
1793 // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3,
1794 // always true for CC>3.
1795 C.CCMask = CC < 4 ? -1 << (4 - CC) : -1;
1796 else if (Cond == ISD::SETGE || Cond == ISD::SETUGE)
1797 // ...and the inverse of that.
1798 C.CCMask = CC < 4 ? ~(-1 << (4 - CC)) : 0;
1799 else if (Cond == ISD::SETLE || Cond == ISD::SETULE)
1800 // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true),
1801 // always true for CC>3.
1802 C.CCMask = CC < 4 ? -1 << (3 - CC) : -1;
1803 else if (Cond == ISD::SETGT || Cond == ISD::SETUGT)
1804 // ...and the inverse of that.
1805 C.CCMask = CC < 4 ? ~(-1 << (3 - CC)) : 0;
1806 else
1807 llvm_unreachable("Unexpected integer comparison type");
1808 C.CCMask &= CCValid;
1809 return C;
1810}
1811
Richard Sandifordd420f732013-12-13 15:28:45 +00001812// Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
1813static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001814 ISD::CondCode Cond, SDLoc DL) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001815 if (CmpOp1.getOpcode() == ISD::Constant) {
1816 uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue();
1817 unsigned Opcode, CCValid;
1818 if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
1819 CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) &&
1820 isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid))
1821 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
1822 }
Richard Sandifordd420f732013-12-13 15:28:45 +00001823 Comparison C(CmpOp0, CmpOp1);
1824 C.CCMask = CCMaskForCondCode(Cond);
1825 if (C.Op0.getValueType().isFloatingPoint()) {
1826 C.CCValid = SystemZ::CCMASK_FCMP;
1827 C.Opcode = SystemZISD::FCMP;
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001828 adjustForFNeg(C);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001829 } else {
Richard Sandifordd420f732013-12-13 15:28:45 +00001830 C.CCValid = SystemZ::CCMASK_ICMP;
1831 C.Opcode = SystemZISD::ICMP;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001832 // Choose the type of comparison. Equality and inequality tests can
1833 // use either signed or unsigned comparisons. The choice also doesn't
1834 // matter if both sign bits are known to be clear. In those cases we
1835 // want to give the main isel code the freedom to choose whichever
1836 // form fits best.
Richard Sandifordd420f732013-12-13 15:28:45 +00001837 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1838 C.CCMask == SystemZ::CCMASK_CMP_NE ||
1839 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
1840 C.ICmpType = SystemZICMP::Any;
1841 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
1842 C.ICmpType = SystemZICMP::UnsignedOnly;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001843 else
Richard Sandifordd420f732013-12-13 15:28:45 +00001844 C.ICmpType = SystemZICMP::SignedOnly;
1845 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001846 adjustZeroCmp(DAG, DL, C);
1847 adjustSubwordCmp(DAG, DL, C);
1848 adjustForSubtraction(DAG, DL, C);
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001849 adjustForLTGFR(C);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001850 adjustICmpTruncate(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001851 }
1852
Richard Sandifordd420f732013-12-13 15:28:45 +00001853 if (shouldSwapCmpOperands(C)) {
1854 std::swap(C.Op0, C.Op1);
1855 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford24e597b2013-08-23 11:27:19 +00001856 }
1857
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001858 adjustForTestUnderMask(DAG, DL, C);
Richard Sandifordd420f732013-12-13 15:28:45 +00001859 return C;
1860}
1861
1862// Emit the comparison instruction described by C.
1863static SDValue emitCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001864 if (!C.Op1.getNode()) {
1865 SDValue Op;
1866 switch (C.Op0.getOpcode()) {
1867 case ISD::INTRINSIC_W_CHAIN:
1868 Op = emitIntrinsicWithChainAndGlue(DAG, C.Op0, C.Opcode);
1869 break;
1870 default:
1871 llvm_unreachable("Invalid comparison operands");
1872 }
1873 return SDValue(Op.getNode(), Op->getNumValues() - 1);
1874 }
Richard Sandifordd420f732013-12-13 15:28:45 +00001875 if (C.Opcode == SystemZISD::ICMP)
1876 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001877 DAG.getConstant(C.ICmpType, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00001878 if (C.Opcode == SystemZISD::TM) {
1879 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
1880 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
1881 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001882 DAG.getConstant(RegisterOnly, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00001883 }
1884 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001885}
1886
Richard Sandiford7d86e472013-08-21 09:34:56 +00001887// Implement a 32-bit *MUL_LOHI operation by extending both operands to
1888// 64 bits. Extend is the extension type to use. Store the high part
1889// in Hi and the low part in Lo.
1890static void lowerMUL_LOHI32(SelectionDAG &DAG, SDLoc DL,
1891 unsigned Extend, SDValue Op0, SDValue Op1,
1892 SDValue &Hi, SDValue &Lo) {
1893 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
1894 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
1895 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001896 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
1897 DAG.getConstant(32, DL, MVT::i64));
Richard Sandiford7d86e472013-08-21 09:34:56 +00001898 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
1899 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
1900}
1901
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001902// Lower a binary operation that produces two VT results, one in each
1903// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
1904// Extend extends Op0 to a GR128, and Opcode performs the GR128 operation
1905// on the extended Op0 and (unextended) Op1. Store the even register result
1906// in Even and the odd register result in Odd.
Andrew Trickef9de2a2013-05-25 02:42:55 +00001907static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001908 unsigned Extend, unsigned Opcode,
1909 SDValue Op0, SDValue Op1,
1910 SDValue &Even, SDValue &Odd) {
1911 SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0);
1912 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped,
1913 SDValue(In128, 0), Op1);
1914 bool Is32Bit = is32Bit(VT);
Richard Sandifordd8163202013-09-13 09:12:44 +00001915 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
1916 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001917}
1918
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001919// Return an i32 value that is 1 if the CC value produced by Glue is
1920// in the mask CCMask and 0 otherwise. CC is known to have a value
1921// in CCValid, so other values can be ignored.
1922static SDValue emitSETCC(SelectionDAG &DAG, SDLoc DL, SDValue Glue,
1923 unsigned CCValid, unsigned CCMask) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001924 IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
1925 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
1926
1927 if (Conversion.XORValue)
1928 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001929 DAG.getConstant(Conversion.XORValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001930
1931 if (Conversion.AddValue)
1932 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001933 DAG.getConstant(Conversion.AddValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001934
1935 // The SHR/AND sequence should get optimized to an RISBG.
1936 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001937 DAG.getConstant(Conversion.Bit, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001938 if (Conversion.Bit != 31)
1939 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001940 DAG.getConstant(1, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001941 return Result;
1942}
1943
Ulrich Weigandcd808232015-05-05 19:26:48 +00001944// Return the SystemISD vector comparison operation for CC, or 0 if it cannot
1945// be done directly. IsFP is true if CC is for a floating-point rather than
1946// integer comparison.
1947static unsigned getVectorComparison(ISD::CondCode CC, bool IsFP) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001948 switch (CC) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00001949 case ISD::SETOEQ:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001950 case ISD::SETEQ:
Ulrich Weigandcd808232015-05-05 19:26:48 +00001951 return IsFP ? SystemZISD::VFCMPE : SystemZISD::VICMPE;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001952
Ulrich Weigandcd808232015-05-05 19:26:48 +00001953 case ISD::SETOGE:
1954 case ISD::SETGE:
1955 return IsFP ? SystemZISD::VFCMPHE : 0;
1956
1957 case ISD::SETOGT:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001958 case ISD::SETGT:
Ulrich Weigandcd808232015-05-05 19:26:48 +00001959 return IsFP ? SystemZISD::VFCMPH : SystemZISD::VICMPH;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001960
1961 case ISD::SETUGT:
Ulrich Weigandcd808232015-05-05 19:26:48 +00001962 return IsFP ? 0 : SystemZISD::VICMPHL;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001963
1964 default:
1965 return 0;
1966 }
1967}
1968
1969// Return the SystemZISD vector comparison operation for CC or its inverse,
1970// or 0 if neither can be done directly. Indicate in Invert whether the
Ulrich Weigandcd808232015-05-05 19:26:48 +00001971// result is for the inverse of CC. IsFP is true if CC is for a
1972// floating-point rather than integer comparison.
1973static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, bool IsFP,
1974 bool &Invert) {
1975 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001976 Invert = false;
1977 return Opcode;
1978 }
1979
Ulrich Weigandcd808232015-05-05 19:26:48 +00001980 CC = ISD::getSetCCInverse(CC, !IsFP);
1981 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001982 Invert = true;
1983 return Opcode;
1984 }
1985
1986 return 0;
1987}
1988
Ulrich Weigand80b3af72015-05-05 19:27:45 +00001989// Return a v2f64 that contains the extended form of elements Start and Start+1
1990// of v4f32 value Op.
1991static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, SDLoc DL,
1992 SDValue Op) {
1993 int Mask[] = { Start, -1, Start + 1, -1 };
1994 Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask);
1995 return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op);
1996}
1997
1998// Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode,
1999// producing a result of type VT.
2000static SDValue getVectorCmp(SelectionDAG &DAG, unsigned Opcode, SDLoc DL,
2001 EVT VT, SDValue CmpOp0, SDValue CmpOp1) {
2002 // There is no hardware support for v4f32, so extend the vector into
2003 // two v2f64s and compare those.
2004 if (CmpOp0.getValueType() == MVT::v4f32) {
2005 SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0);
2006 SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0);
2007 SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1);
2008 SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1);
2009 SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1);
2010 SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1);
2011 return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes);
2012 }
2013 return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1);
2014}
2015
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002016// Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing
2017// an integer mask of type VT.
2018static SDValue lowerVectorSETCC(SelectionDAG &DAG, SDLoc DL, EVT VT,
2019 ISD::CondCode CC, SDValue CmpOp0,
2020 SDValue CmpOp1) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002021 bool IsFP = CmpOp0.getValueType().isFloatingPoint();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002022 bool Invert = false;
2023 SDValue Cmp;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002024 switch (CC) {
2025 // Handle tests for order using (or (ogt y x) (oge x y)).
2026 case ISD::SETUO:
2027 Invert = true;
2028 case ISD::SETO: {
2029 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002030 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2031 SDValue GE = getVectorCmp(DAG, SystemZISD::VFCMPHE, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002032 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE);
2033 break;
2034 }
2035
2036 // Handle <> tests using (or (ogt y x) (ogt x y)).
2037 case ISD::SETUEQ:
2038 Invert = true;
2039 case ISD::SETONE: {
2040 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002041 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2042 SDValue GT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002043 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT);
2044 break;
2045 }
2046
2047 // Otherwise a single comparison is enough. It doesn't really
2048 // matter whether we try the inversion or the swap first, since
2049 // there are no cases where both work.
2050 default:
2051 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002052 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002053 else {
2054 CC = ISD::getSetCCSwappedOperands(CC);
2055 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002056 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002057 else
2058 llvm_unreachable("Unhandled comparison");
2059 }
2060 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002061 }
2062 if (Invert) {
2063 SDValue Mask = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2064 DAG.getConstant(65535, DL, MVT::i32));
2065 Mask = DAG.getNode(ISD::BITCAST, DL, VT, Mask);
2066 Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask);
2067 }
2068 return Cmp;
2069}
2070
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002071SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
2072 SelectionDAG &DAG) const {
2073 SDValue CmpOp0 = Op.getOperand(0);
2074 SDValue CmpOp1 = Op.getOperand(1);
2075 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
2076 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002077 EVT VT = Op.getValueType();
2078 if (VT.isVector())
2079 return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002080
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002081 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002082 SDValue Glue = emitCmp(DAG, DL, C);
2083 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002084}
2085
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002086SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002087 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2088 SDValue CmpOp0 = Op.getOperand(2);
2089 SDValue CmpOp1 = Op.getOperand(3);
2090 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002091 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002092
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002093 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002094 SDValue Glue = emitCmp(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002095 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002096 Op.getOperand(0), DAG.getConstant(C.CCValid, DL, MVT::i32),
2097 DAG.getConstant(C.CCMask, DL, MVT::i32), Dest, Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002098}
2099
Richard Sandiford57485472013-12-13 15:35:00 +00002100// Return true if Pos is CmpOp and Neg is the negative of CmpOp,
2101// allowing Pos and Neg to be wider than CmpOp.
2102static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
2103 return (Neg.getOpcode() == ISD::SUB &&
2104 Neg.getOperand(0).getOpcode() == ISD::Constant &&
2105 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
2106 Neg.getOperand(1) == Pos &&
2107 (Pos == CmpOp ||
2108 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
2109 Pos.getOperand(0) == CmpOp)));
2110}
2111
2112// Return the absolute or negative absolute of Op; IsNegative decides which.
2113static SDValue getAbsolute(SelectionDAG &DAG, SDLoc DL, SDValue Op,
2114 bool IsNegative) {
2115 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
2116 if (IsNegative)
2117 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002118 DAG.getConstant(0, DL, Op.getValueType()), Op);
Richard Sandiford57485472013-12-13 15:35:00 +00002119 return Op;
2120}
2121
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002122SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
2123 SelectionDAG &DAG) const {
2124 SDValue CmpOp0 = Op.getOperand(0);
2125 SDValue CmpOp1 = Op.getOperand(1);
2126 SDValue TrueOp = Op.getOperand(2);
2127 SDValue FalseOp = Op.getOperand(3);
2128 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002129 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002130
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002131 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandiford57485472013-12-13 15:35:00 +00002132
2133 // Check for absolute and negative-absolute selections, including those
2134 // where the comparison value is sign-extended (for LPGFR and LNGFR).
2135 // This check supplements the one in DAGCombiner.
2136 if (C.Opcode == SystemZISD::ICMP &&
2137 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
2138 C.CCMask != SystemZ::CCMASK_CMP_NE &&
2139 C.Op1.getOpcode() == ISD::Constant &&
2140 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
2141 if (isAbsolute(C.Op0, TrueOp, FalseOp))
2142 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
2143 if (isAbsolute(C.Op0, FalseOp, TrueOp))
2144 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
2145 }
2146
Richard Sandifordd420f732013-12-13 15:28:45 +00002147 SDValue Glue = emitCmp(DAG, DL, C);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002148
2149 // Special case for handling -1/0 results. The shifts we use here
2150 // should get optimized with the IPM conversion sequence.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002151 auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
2152 auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002153 if (TrueC && FalseC) {
2154 int64_t TrueVal = TrueC->getSExtValue();
2155 int64_t FalseVal = FalseC->getSExtValue();
2156 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
2157 // Invert the condition if we want -1 on false.
2158 if (TrueVal == 0)
Richard Sandifordd420f732013-12-13 15:28:45 +00002159 C.CCMask ^= C.CCValid;
2160 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002161 EVT VT = Op.getValueType();
2162 // Extend the result to VT. Upper bits are ignored.
2163 if (!is32Bit(VT))
2164 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
2165 // Sign-extend from the low bit.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002166 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002167 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
2168 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
2169 }
2170 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002171
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002172 SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, DL, MVT::i32),
2173 DAG.getConstant(C.CCMask, DL, MVT::i32), Glue};
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002174
2175 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00002176 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002177}
2178
2179SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
2180 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002181 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002182 const GlobalValue *GV = Node->getGlobal();
2183 int64_t Offset = Node->getOffset();
2184 EVT PtrVT = getPointerTy();
Eric Christopher93bf97c2014-06-27 07:38:01 +00002185 Reloc::Model RM = DAG.getTarget().getRelocationModel();
2186 CodeModel::Model CM = DAG.getTarget().getCodeModel();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002187
2188 SDValue Result;
2189 if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) {
Richard Sandiford54b36912013-09-27 15:14:04 +00002190 // Assign anchors at 1<<12 byte boundaries.
2191 uint64_t Anchor = Offset & ~uint64_t(0xfff);
2192 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
2193 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2194
2195 // The offset can be folded into the address if it is aligned to a halfword.
2196 Offset -= Anchor;
2197 if (Offset != 0 && (Offset & 1) == 0) {
2198 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
2199 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002200 Offset = 0;
2201 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002202 } else {
2203 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
2204 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2205 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
2206 MachinePointerInfo::getGOT(), false, false, false, 0);
2207 }
2208
2209 // If there was a non-zero offset that we didn't fold, create an explicit
2210 // addition for it.
2211 if (Offset != 0)
2212 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002213 DAG.getConstant(Offset, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002214
2215 return Result;
2216}
2217
Ulrich Weigand7db69182015-02-18 09:13:27 +00002218SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
2219 SelectionDAG &DAG,
2220 unsigned Opcode,
2221 SDValue GOTOffset) const {
2222 SDLoc DL(Node);
2223 EVT PtrVT = getPointerTy();
2224 SDValue Chain = DAG.getEntryNode();
2225 SDValue Glue;
2226
2227 // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
2228 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
2229 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
2230 Glue = Chain.getValue(1);
2231 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
2232 Glue = Chain.getValue(1);
2233
2234 // The first call operand is the chain and the second is the TLS symbol.
2235 SmallVector<SDValue, 8> Ops;
2236 Ops.push_back(Chain);
2237 Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
2238 Node->getValueType(0),
2239 0, 0));
2240
2241 // Add argument registers to the end of the list so that they are
2242 // known live into the call.
2243 Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
2244 Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
2245
2246 // Add a register mask operand representing the call-preserved registers.
2247 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002248 const uint32_t *Mask =
2249 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002250 assert(Mask && "Missing call preserved mask for calling convention");
2251 Ops.push_back(DAG.getRegisterMask(Mask));
2252
2253 // Glue the call to the argument copies.
2254 Ops.push_back(Glue);
2255
2256 // Emit the call.
2257 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2258 Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
2259 Glue = Chain.getValue(1);
2260
2261 // Copy the return value from %r2.
2262 return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
2263}
2264
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002265SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
2266 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002267 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002268 const GlobalValue *GV = Node->getGlobal();
2269 EVT PtrVT = getPointerTy();
Eric Christopher93bf97c2014-06-27 07:38:01 +00002270 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002271
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002272 // The high part of the thread pointer is in access register 0.
2273 SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002274 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002275 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
2276
2277 // The low part of the thread pointer is in access register 1.
2278 SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002279 DAG.getConstant(1, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002280 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
2281
2282 // Merge them into a single 64-bit address.
2283 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002284 DAG.getConstant(32, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002285 SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
2286
Ulrich Weigand7db69182015-02-18 09:13:27 +00002287 // Get the offset of GA from the thread pointer, based on the TLS model.
2288 SDValue Offset;
2289 switch (model) {
2290 case TLSModel::GeneralDynamic: {
2291 // Load the GOT offset of the tls_index (module ID / per-symbol offset).
2292 SystemZConstantPoolValue *CPV =
2293 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002294
Ulrich Weigand7db69182015-02-18 09:13:27 +00002295 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
2296 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
2297 Offset, MachinePointerInfo::getConstantPool(),
2298 false, false, false, 0);
2299
2300 // Call __tls_get_offset to retrieve the offset.
2301 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
2302 break;
2303 }
2304
2305 case TLSModel::LocalDynamic: {
2306 // Load the GOT offset of the module ID.
2307 SystemZConstantPoolValue *CPV =
2308 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
2309
2310 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
2311 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
2312 Offset, MachinePointerInfo::getConstantPool(),
2313 false, false, false, 0);
2314
2315 // Call __tls_get_offset to retrieve the module base offset.
2316 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
2317
2318 // Note: The SystemZLDCleanupPass will remove redundant computations
2319 // of the module base offset. Count total number of local-dynamic
2320 // accesses to trigger execution of that pass.
2321 SystemZMachineFunctionInfo* MFI =
2322 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
2323 MFI->incNumLocalDynamicTLSAccesses();
2324
2325 // Add the per-symbol offset.
2326 CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
2327
2328 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8);
2329 DTPOffset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
2330 DTPOffset, MachinePointerInfo::getConstantPool(),
2331 false, false, false, 0);
2332
2333 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
2334 break;
2335 }
2336
2337 case TLSModel::InitialExec: {
2338 // Load the offset from the GOT.
2339 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2340 SystemZII::MO_INDNTPOFF);
2341 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
2342 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
2343 Offset, MachinePointerInfo::getGOT(),
2344 false, false, false, 0);
2345 break;
2346 }
2347
2348 case TLSModel::LocalExec: {
2349 // Force the offset into the constant pool and load it from there.
2350 SystemZConstantPoolValue *CPV =
2351 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
2352
2353 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
2354 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
2355 Offset, MachinePointerInfo::getConstantPool(),
2356 false, false, false, 0);
2357 break;
Ulrich Weigandb7e59092015-02-18 09:42:23 +00002358 }
Ulrich Weigand7db69182015-02-18 09:13:27 +00002359 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002360
2361 // Add the base and offset together.
2362 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
2363}
2364
2365SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
2366 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002367 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002368 const BlockAddress *BA = Node->getBlockAddress();
2369 int64_t Offset = Node->getOffset();
2370 EVT PtrVT = getPointerTy();
2371
2372 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
2373 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2374 return Result;
2375}
2376
2377SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
2378 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002379 SDLoc DL(JT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002380 EVT PtrVT = getPointerTy();
2381 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
2382
2383 // Use LARL to load the address of the table.
2384 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2385}
2386
2387SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
2388 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002389 SDLoc DL(CP);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002390 EVT PtrVT = getPointerTy();
2391
2392 SDValue Result;
2393 if (CP->isMachineConstantPoolEntry())
2394 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2395 CP->getAlignment());
2396 else
2397 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2398 CP->getAlignment(), CP->getOffset());
2399
2400 // Use LARL to load the address of the constant pool entry.
2401 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2402}
2403
2404SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
2405 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002406 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002407 SDValue In = Op.getOperand(0);
2408 EVT InVT = In.getValueType();
2409 EVT ResVT = Op.getValueType();
2410
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002411 // Convert loads directly. This is normally done by DAGCombiner,
2412 // but we need this case for bitcasts that are created during lowering
2413 // and which are then lowered themselves.
2414 if (auto *LoadN = dyn_cast<LoadSDNode>(In))
2415 return DAG.getLoad(ResVT, DL, LoadN->getChain(), LoadN->getBasePtr(),
2416 LoadN->getMemOperand());
2417
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002418 if (InVT == MVT::i32 && ResVT == MVT::f32) {
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002419 SDValue In64;
2420 if (Subtarget.hasHighWord()) {
2421 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
2422 MVT::i64);
2423 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
2424 MVT::i64, SDValue(U64, 0), In);
2425 } else {
2426 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
2427 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002428 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002429 }
2430 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002431 return DAG.getTargetExtractSubreg(SystemZ::subreg_r32,
Richard Sandifordd8163202013-09-13 09:12:44 +00002432 DL, MVT::f32, Out64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002433 }
2434 if (InVT == MVT::f32 && ResVT == MVT::i32) {
2435 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002436 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002437 MVT::f64, SDValue(U64, 0), In);
2438 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002439 if (Subtarget.hasHighWord())
2440 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
2441 MVT::i32, Out64);
2442 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002443 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002444 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002445 }
2446 llvm_unreachable("Unexpected bitcast combination");
2447}
2448
2449SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
2450 SelectionDAG &DAG) const {
2451 MachineFunction &MF = DAG.getMachineFunction();
2452 SystemZMachineFunctionInfo *FuncInfo =
2453 MF.getInfo<SystemZMachineFunctionInfo>();
2454 EVT PtrVT = getPointerTy();
2455
2456 SDValue Chain = Op.getOperand(0);
2457 SDValue Addr = Op.getOperand(1);
2458 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002459 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002460
2461 // The initial values of each field.
2462 const unsigned NumFields = 4;
2463 SDValue Fields[NumFields] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002464 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT),
2465 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002466 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
2467 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
2468 };
2469
2470 // Store each field into its respective slot.
2471 SDValue MemOps[NumFields];
2472 unsigned Offset = 0;
2473 for (unsigned I = 0; I < NumFields; ++I) {
2474 SDValue FieldAddr = Addr;
2475 if (Offset != 0)
2476 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002477 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002478 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
2479 MachinePointerInfo(SV, Offset),
2480 false, false, 0);
2481 Offset += 8;
2482 }
Craig Topper48d114b2014-04-26 18:35:24 +00002483 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002484}
2485
2486SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
2487 SelectionDAG &DAG) const {
2488 SDValue Chain = Op.getOperand(0);
2489 SDValue DstPtr = Op.getOperand(1);
2490 SDValue SrcPtr = Op.getOperand(2);
2491 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
2492 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002493 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002494
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002495 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002496 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00002497 /*isTailCall*/false,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002498 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
2499}
2500
2501SDValue SystemZTargetLowering::
2502lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
2503 SDValue Chain = Op.getOperand(0);
2504 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002505 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002506
2507 unsigned SPReg = getStackPointerRegisterToSaveRestore();
2508
2509 // Get a reference to the stack pointer.
2510 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
2511
2512 // Get the new stack pointer value.
2513 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, Size);
2514
2515 // Copy the new stack pointer back.
2516 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
2517
2518 // The allocated data lives above the 160 bytes allocated for the standard
2519 // frame, plus any outgoing stack arguments. We don't know how much that
2520 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
2521 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
2522 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
2523
2524 SDValue Ops[2] = { Result, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002525 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002526}
2527
Richard Sandiford7d86e472013-08-21 09:34:56 +00002528SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
2529 SelectionDAG &DAG) const {
2530 EVT VT = Op.getValueType();
2531 SDLoc DL(Op);
2532 SDValue Ops[2];
2533 if (is32Bit(VT))
2534 // Just do a normal 64-bit multiplication and extract the results.
2535 // We define this so that it can be used for constant division.
2536 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
2537 Op.getOperand(1), Ops[1], Ops[0]);
2538 else {
2539 // Do a full 128-bit multiplication based on UMUL_LOHI64:
2540 //
2541 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
2542 //
2543 // but using the fact that the upper halves are either all zeros
2544 // or all ones:
2545 //
2546 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
2547 //
2548 // and grouping the right terms together since they are quicker than the
2549 // multiplication:
2550 //
2551 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002552 SDValue C63 = DAG.getConstant(63, DL, MVT::i64);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002553 SDValue LL = Op.getOperand(0);
2554 SDValue RL = Op.getOperand(1);
2555 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
2556 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
2557 // UMUL_LOHI64 returns the low result in the odd register and the high
2558 // result in the even register. SMUL_LOHI is defined to return the
2559 // low half first, so the results are in reverse order.
2560 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2561 LL, RL, Ops[1], Ops[0]);
2562 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
2563 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
2564 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
2565 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
2566 }
Craig Topper64941d92014-04-27 19:20:57 +00002567 return DAG.getMergeValues(Ops, DL);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002568}
2569
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002570SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
2571 SelectionDAG &DAG) const {
2572 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002573 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002574 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00002575 if (is32Bit(VT))
2576 // Just do a normal 64-bit multiplication and extract the results.
2577 // We define this so that it can be used for constant division.
2578 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
2579 Op.getOperand(1), Ops[1], Ops[0]);
2580 else
2581 // UMUL_LOHI64 returns the low result in the odd register and the high
2582 // result in the even register. UMUL_LOHI is defined to return the
2583 // low half first, so the results are in reverse order.
2584 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2585 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002586 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002587}
2588
2589SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
2590 SelectionDAG &DAG) const {
2591 SDValue Op0 = Op.getOperand(0);
2592 SDValue Op1 = Op.getOperand(1);
2593 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002594 SDLoc DL(Op);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002595 unsigned Opcode;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002596
2597 // We use DSGF for 32-bit division.
2598 if (is32Bit(VT)) {
2599 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002600 Opcode = SystemZISD::SDIVREM32;
2601 } else if (DAG.ComputeNumSignBits(Op1) > 32) {
2602 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
2603 Opcode = SystemZISD::SDIVREM32;
2604 } else
2605 Opcode = SystemZISD::SDIVREM64;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002606
2607 // DSG(F) takes a 64-bit dividend, so the even register in the GR128
2608 // input is "don't care". The instruction returns the remainder in
2609 // the even register and the quotient in the odd register.
2610 SDValue Ops[2];
Richard Sandiforde6e78852013-07-02 15:40:22 +00002611 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002612 Op0, Op1, Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002613 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002614}
2615
2616SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
2617 SelectionDAG &DAG) const {
2618 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002619 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002620
2621 // DL(G) uses a double-width dividend, so we need to clear the even
2622 // register in the GR128 input. The instruction returns the remainder
2623 // in the even register and the quotient in the odd register.
2624 SDValue Ops[2];
2625 if (is32Bit(VT))
2626 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
2627 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2628 else
2629 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
2630 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002631 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002632}
2633
2634SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
2635 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
2636
2637 // Get the known-zero masks for each operand.
2638 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
2639 APInt KnownZero[2], KnownOne[2];
Jay Foada0653a32014-05-14 21:14:37 +00002640 DAG.computeKnownBits(Ops[0], KnownZero[0], KnownOne[0]);
2641 DAG.computeKnownBits(Ops[1], KnownZero[1], KnownOne[1]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002642
2643 // See if the upper 32 bits of one operand and the lower 32 bits of the
2644 // other are known zero. They are the low and high operands respectively.
2645 uint64_t Masks[] = { KnownZero[0].getZExtValue(),
2646 KnownZero[1].getZExtValue() };
2647 unsigned High, Low;
2648 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
2649 High = 1, Low = 0;
2650 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
2651 High = 0, Low = 1;
2652 else
2653 return Op;
2654
2655 SDValue LowOp = Ops[Low];
2656 SDValue HighOp = Ops[High];
2657
2658 // If the high part is a constant, we're better off using IILH.
2659 if (HighOp.getOpcode() == ISD::Constant)
2660 return Op;
2661
2662 // If the low part is a constant that is outside the range of LHI,
2663 // then we're better off using IILF.
2664 if (LowOp.getOpcode() == ISD::Constant) {
2665 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
2666 if (!isInt<16>(Value))
2667 return Op;
2668 }
2669
2670 // Check whether the high part is an AND that doesn't change the
2671 // high 32 bits and just masks out low bits. We can skip it if so.
2672 if (HighOp.getOpcode() == ISD::AND &&
2673 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
Richard Sandifordccc2a7c2013-12-03 11:01:54 +00002674 SDValue HighOp0 = HighOp.getOperand(0);
2675 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
2676 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
2677 HighOp = HighOp0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002678 }
2679
2680 // Take advantage of the fact that all GR32 operations only change the
2681 // low 32 bits by truncating Low to an i32 and inserting it directly
2682 // using a subreg. The interesting cases are those where the truncation
2683 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002684 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002685 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
Richard Sandiford87a44362013-09-30 10:28:35 +00002686 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002687 MVT::i64, HighOp, Low32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002688}
2689
Ulrich Weigandb4012182015-03-31 12:56:33 +00002690SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
2691 SelectionDAG &DAG) const {
2692 EVT VT = Op.getValueType();
Ulrich Weigandb4012182015-03-31 12:56:33 +00002693 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002694 Op = Op.getOperand(0);
2695
2696 // Handle vector types via VPOPCT.
2697 if (VT.isVector()) {
2698 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op);
2699 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op);
2700 switch (VT.getVectorElementType().getSizeInBits()) {
2701 case 8:
2702 break;
2703 case 16: {
2704 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
2705 SDValue Shift = DAG.getConstant(8, DL, MVT::i32);
2706 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift);
2707 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
2708 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift);
2709 break;
2710 }
2711 case 32: {
2712 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2713 DAG.getConstant(0, DL, MVT::i32));
2714 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
2715 break;
2716 }
2717 case 64: {
2718 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2719 DAG.getConstant(0, DL, MVT::i32));
2720 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp);
2721 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
2722 break;
2723 }
2724 default:
2725 llvm_unreachable("Unexpected type");
2726 }
2727 return Op;
2728 }
Ulrich Weigandb4012182015-03-31 12:56:33 +00002729
2730 // Get the known-zero mask for the operand.
Ulrich Weigandb4012182015-03-31 12:56:33 +00002731 APInt KnownZero, KnownOne;
2732 DAG.computeKnownBits(Op, KnownZero, KnownOne);
Ulrich Weigand050527b2015-03-31 19:28:50 +00002733 unsigned NumSignificantBits = (~KnownZero).getActiveBits();
2734 if (NumSignificantBits == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002735 return DAG.getConstant(0, DL, VT);
Ulrich Weigandb4012182015-03-31 12:56:33 +00002736
2737 // Skip known-zero high parts of the operand.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002738 int64_t OrigBitSize = VT.getSizeInBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00002739 int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits);
2740 BitSize = std::min(BitSize, OrigBitSize);
Ulrich Weigandb4012182015-03-31 12:56:33 +00002741
2742 // The POPCNT instruction counts the number of bits in each byte.
2743 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op);
2744 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op);
2745 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
2746
2747 // Add up per-byte counts in a binary tree. All bits of Op at
2748 // position larger than BitSize remain zero throughout.
2749 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002750 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00002751 if (BitSize != OrigBitSize)
2752 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002753 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00002754 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
2755 }
2756
2757 // Extract overall result from high byte.
2758 if (BitSize > 8)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002759 Op = DAG.getNode(ISD::SRL, DL, VT, Op,
2760 DAG.getConstant(BitSize - 8, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00002761
2762 return Op;
2763}
2764
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002765// Op is an atomic load. Lower it into a normal volatile load.
2766SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
2767 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002768 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002769 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
2770 Node->getChain(), Node->getBasePtr(),
2771 Node->getMemoryVT(), Node->getMemOperand());
2772}
2773
2774// Op is an atomic store. Lower it into a normal volatile store followed
2775// by a serialization.
2776SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
2777 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002778 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002779 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
2780 Node->getBasePtr(), Node->getMemoryVT(),
2781 Node->getMemOperand());
2782 return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other,
2783 Chain), 0);
2784}
2785
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002786// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
2787// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002788SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
2789 SelectionDAG &DAG,
2790 unsigned Opcode) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002791 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002792
2793 // 32-bit operations need no code outside the main loop.
2794 EVT NarrowVT = Node->getMemoryVT();
2795 EVT WideVT = MVT::i32;
2796 if (NarrowVT == WideVT)
2797 return Op;
2798
2799 int64_t BitSize = NarrowVT.getSizeInBits();
2800 SDValue ChainIn = Node->getChain();
2801 SDValue Addr = Node->getBasePtr();
2802 SDValue Src2 = Node->getVal();
2803 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002804 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002805 EVT PtrVT = Addr.getValueType();
2806
2807 // Convert atomic subtracts of constants into additions.
2808 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
Richard Sandiford21f5d682014-03-06 11:22:58 +00002809 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002810 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002811 Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002812 }
2813
2814 // Get the address of the containing word.
2815 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002816 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002817
2818 // Get the number of bits that the word must be rotated left in order
2819 // to bring the field to the top bits of a GR32.
2820 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002821 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002822 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
2823
2824 // Get the complementing shift amount, for rotating a field in the top
2825 // bits back to its proper position.
2826 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002827 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002828
2829 // Extend the source operand to 32 bits and prepare it for the inner loop.
2830 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
2831 // operations require the source to be shifted in advance. (This shift
2832 // can be folded if the source is constant.) For AND and NAND, the lower
2833 // bits must be set, while for other opcodes they should be left clear.
2834 if (Opcode != SystemZISD::ATOMIC_SWAPW)
2835 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002836 DAG.getConstant(32 - BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002837 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
2838 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
2839 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002840 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002841
2842 // Construct the ATOMIC_LOADW_* node.
2843 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
2844 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002845 DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002846 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002847 NarrowVT, MMO);
2848
2849 // Rotate the result of the final CS so that the field is in the lower
2850 // bits of a GR32, then truncate it.
2851 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002852 DAG.getConstant(BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002853 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
2854
2855 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002856 return DAG.getMergeValues(RetOps, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002857}
2858
Richard Sandiford41350a52013-12-24 15:18:04 +00002859// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
Richard Sandiford002019a2013-12-24 15:22:39 +00002860// into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
Richard Sandiford41350a52013-12-24 15:18:04 +00002861// operations into additions.
2862SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
2863 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002864 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandiford41350a52013-12-24 15:18:04 +00002865 EVT MemVT = Node->getMemoryVT();
2866 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
2867 // A full-width operation.
2868 assert(Op.getValueType() == MemVT && "Mismatched VTs");
2869 SDValue Src2 = Node->getVal();
2870 SDValue NegSrc2;
2871 SDLoc DL(Src2);
2872
Richard Sandiford21f5d682014-03-06 11:22:58 +00002873 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
Richard Sandiford41350a52013-12-24 15:18:04 +00002874 // Use an addition if the operand is constant and either LAA(G) is
2875 // available or the negative value is in the range of A(G)FHI.
2876 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
Eric Christopher93bf97c2014-06-27 07:38:01 +00002877 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002878 NegSrc2 = DAG.getConstant(Value, DL, MemVT);
Eric Christopher93bf97c2014-06-27 07:38:01 +00002879 } else if (Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00002880 // Use LAA(G) if available.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002881 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT),
Richard Sandiford41350a52013-12-24 15:18:04 +00002882 Src2);
2883
2884 if (NegSrc2.getNode())
2885 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
2886 Node->getChain(), Node->getBasePtr(), NegSrc2,
2887 Node->getMemOperand(), Node->getOrdering(),
2888 Node->getSynchScope());
2889
2890 // Use the node as-is.
2891 return Op;
2892 }
2893
2894 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
2895}
2896
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002897// Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
2898// into a fullword ATOMIC_CMP_SWAPW operation.
2899SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
2900 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002901 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002902
2903 // We have native support for 32-bit compare and swap.
2904 EVT NarrowVT = Node->getMemoryVT();
2905 EVT WideVT = MVT::i32;
2906 if (NarrowVT == WideVT)
2907 return Op;
2908
2909 int64_t BitSize = NarrowVT.getSizeInBits();
2910 SDValue ChainIn = Node->getOperand(0);
2911 SDValue Addr = Node->getOperand(1);
2912 SDValue CmpVal = Node->getOperand(2);
2913 SDValue SwapVal = Node->getOperand(3);
2914 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002915 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002916 EVT PtrVT = Addr.getValueType();
2917
2918 // Get the address of the containing word.
2919 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002920 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002921
2922 // Get the number of bits that the word must be rotated left in order
2923 // to bring the field to the top bits of a GR32.
2924 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002925 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002926 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
2927
2928 // Get the complementing shift amount, for rotating a field in the top
2929 // bits back to its proper position.
2930 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002931 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002932
2933 // Construct the ATOMIC_CMP_SWAPW node.
2934 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
2935 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002936 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002937 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00002938 VTList, Ops, NarrowVT, MMO);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002939 return AtomicOp;
2940}
2941
2942SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
2943 SelectionDAG &DAG) const {
2944 MachineFunction &MF = DAG.getMachineFunction();
2945 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002946 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002947 SystemZ::R15D, Op.getValueType());
2948}
2949
2950SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
2951 SelectionDAG &DAG) const {
2952 MachineFunction &MF = DAG.getMachineFunction();
2953 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002954 return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002955 SystemZ::R15D, Op.getOperand(1));
2956}
2957
Richard Sandiford03481332013-08-23 11:36:42 +00002958SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
2959 SelectionDAG &DAG) const {
2960 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2961 if (!IsData)
2962 // Just preserve the chain.
2963 return Op.getOperand(0);
2964
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002965 SDLoc DL(Op);
Richard Sandiford03481332013-08-23 11:36:42 +00002966 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2967 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
Richard Sandiford21f5d682014-03-06 11:22:58 +00002968 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
Richard Sandiford03481332013-08-23 11:36:42 +00002969 SDValue Ops[] = {
2970 Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002971 DAG.getConstant(Code, DL, MVT::i32),
Richard Sandiford03481332013-08-23 11:36:42 +00002972 Op.getOperand(1)
2973 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002974 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00002975 Node->getVTList(), Ops,
Richard Sandiford03481332013-08-23 11:36:42 +00002976 Node->getMemoryVT(), Node->getMemOperand());
2977}
2978
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002979// Return an i32 that contains the value of CC immediately after After,
2980// whose final operand must be MVT::Glue.
2981static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002982 SDLoc DL(After);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002983 SDValue Glue = SDValue(After, After->getNumValues() - 1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002984 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
2985 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
2986 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002987}
2988
2989SDValue
2990SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2991 SelectionDAG &DAG) const {
2992 unsigned Opcode, CCValid;
2993 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) {
2994 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
2995 SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode);
2996 SDValue CC = getCCResult(DAG, Glued.getNode());
2997 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC);
2998 return SDValue();
2999 }
3000
3001 return SDValue();
3002}
3003
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003004namespace {
3005// Says that SystemZISD operation Opcode can be used to perform the equivalent
3006// of a VPERM with permute vector Bytes. If Opcode takes three operands,
3007// Operand is the constant third operand, otherwise it is the number of
3008// bytes in each element of the result.
3009struct Permute {
3010 unsigned Opcode;
3011 unsigned Operand;
3012 unsigned char Bytes[SystemZ::VectorBytes];
3013};
3014}
3015
3016static const Permute PermuteForms[] = {
3017 // VMRHG
3018 { SystemZISD::MERGE_HIGH, 8,
3019 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } },
3020 // VMRHF
3021 { SystemZISD::MERGE_HIGH, 4,
3022 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
3023 // VMRHH
3024 { SystemZISD::MERGE_HIGH, 2,
3025 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
3026 // VMRHB
3027 { SystemZISD::MERGE_HIGH, 1,
3028 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
3029 // VMRLG
3030 { SystemZISD::MERGE_LOW, 8,
3031 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } },
3032 // VMRLF
3033 { SystemZISD::MERGE_LOW, 4,
3034 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
3035 // VMRLH
3036 { SystemZISD::MERGE_LOW, 2,
3037 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
3038 // VMRLB
3039 { SystemZISD::MERGE_LOW, 1,
3040 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
3041 // VPKG
3042 { SystemZISD::PACK, 4,
3043 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } },
3044 // VPKF
3045 { SystemZISD::PACK, 2,
3046 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
3047 // VPKH
3048 { SystemZISD::PACK, 1,
3049 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
3050 // VPDI V1, V2, 4 (low half of V1, high half of V2)
3051 { SystemZISD::PERMUTE_DWORDS, 4,
3052 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } },
3053 // VPDI V1, V2, 1 (high half of V1, low half of V2)
3054 { SystemZISD::PERMUTE_DWORDS, 1,
3055 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } }
3056};
3057
3058// Called after matching a vector shuffle against a particular pattern.
3059// Both the original shuffle and the pattern have two vector operands.
3060// OpNos[0] is the operand of the original shuffle that should be used for
3061// operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything.
3062// OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and
3063// set OpNo0 and OpNo1 to the shuffle operands that should actually be used
3064// for operands 0 and 1 of the pattern.
3065static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) {
3066 if (OpNos[0] < 0) {
3067 if (OpNos[1] < 0)
3068 return false;
3069 OpNo0 = OpNo1 = OpNos[1];
3070 } else if (OpNos[1] < 0) {
3071 OpNo0 = OpNo1 = OpNos[0];
3072 } else {
3073 OpNo0 = OpNos[0];
3074 OpNo1 = OpNos[1];
3075 }
3076 return true;
3077}
3078
3079// Bytes is a VPERM-like permute vector, except that -1 is used for
3080// undefined bytes. Return true if the VPERM can be implemented using P.
3081// When returning true set OpNo0 to the VPERM operand that should be
3082// used for operand 0 of P and likewise OpNo1 for operand 1 of P.
3083//
3084// For example, if swapping the VPERM operands allows P to match, OpNo0
3085// will be 1 and OpNo1 will be 0. If instead Bytes only refers to one
3086// operand, but rewriting it to use two duplicated operands allows it to
3087// match P, then OpNo0 and OpNo1 will be the same.
3088static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P,
3089 unsigned &OpNo0, unsigned &OpNo1) {
3090 int OpNos[] = { -1, -1 };
3091 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
3092 int Elt = Bytes[I];
3093 if (Elt >= 0) {
3094 // Make sure that the two permute vectors use the same suboperand
3095 // byte number. Only the operand numbers (the high bits) are
3096 // allowed to differ.
3097 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1))
3098 return false;
3099 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes;
3100 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes;
3101 // Make sure that the operand mappings are consistent with previous
3102 // elements.
3103 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3104 return false;
3105 OpNos[ModelOpNo] = RealOpNo;
3106 }
3107 }
3108 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3109}
3110
3111// As above, but search for a matching permute.
3112static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes,
3113 unsigned &OpNo0, unsigned &OpNo1) {
3114 for (auto &P : PermuteForms)
3115 if (matchPermute(Bytes, P, OpNo0, OpNo1))
3116 return &P;
3117 return nullptr;
3118}
3119
3120// Bytes is a VPERM-like permute vector, except that -1 is used for
3121// undefined bytes. This permute is an operand of an outer permute.
3122// See whether redistributing the -1 bytes gives a shuffle that can be
3123// implemented using P. If so, set Transform to a VPERM-like permute vector
3124// that, when applied to the result of P, gives the original permute in Bytes.
3125static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3126 const Permute &P,
3127 SmallVectorImpl<int> &Transform) {
3128 unsigned To = 0;
3129 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) {
3130 int Elt = Bytes[From];
3131 if (Elt < 0)
3132 // Byte number From of the result is undefined.
3133 Transform[From] = -1;
3134 else {
3135 while (P.Bytes[To] != Elt) {
3136 To += 1;
3137 if (To == SystemZ::VectorBytes)
3138 return false;
3139 }
3140 Transform[From] = To;
3141 }
3142 }
3143 return true;
3144}
3145
3146// As above, but search for a matching permute.
3147static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3148 SmallVectorImpl<int> &Transform) {
3149 for (auto &P : PermuteForms)
3150 if (matchDoublePermute(Bytes, P, Transform))
3151 return &P;
3152 return nullptr;
3153}
3154
3155// Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask,
3156// as if it had type vNi8.
3157static void getVPermMask(ShuffleVectorSDNode *VSN,
3158 SmallVectorImpl<int> &Bytes) {
3159 EVT VT = VSN->getValueType(0);
3160 unsigned NumElements = VT.getVectorNumElements();
3161 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3162 Bytes.resize(NumElements * BytesPerElement, -1);
3163 for (unsigned I = 0; I < NumElements; ++I) {
3164 int Index = VSN->getMaskElt(I);
3165 if (Index >= 0)
3166 for (unsigned J = 0; J < BytesPerElement; ++J)
3167 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
3168 }
3169}
3170
3171// Bytes is a VPERM-like permute vector, except that -1 is used for
3172// undefined bytes. See whether bytes [Start, Start + BytesPerElement) of
3173// the result come from a contiguous sequence of bytes from one input.
3174// Set Base to the selector for the first byte if so.
3175static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start,
3176 unsigned BytesPerElement, int &Base) {
3177 Base = -1;
3178 for (unsigned I = 0; I < BytesPerElement; ++I) {
3179 if (Bytes[Start + I] >= 0) {
3180 unsigned Elem = Bytes[Start + I];
3181 if (Base < 0) {
3182 Base = Elem - I;
3183 // Make sure the bytes would come from one input operand.
3184 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size())
3185 return false;
3186 } else if (unsigned(Base) != Elem - I)
3187 return false;
3188 }
3189 }
3190 return true;
3191}
3192
3193// Bytes is a VPERM-like permute vector, except that -1 is used for
3194// undefined bytes. Return true if it can be performed using VSLDI.
3195// When returning true, set StartIndex to the shift amount and OpNo0
3196// and OpNo1 to the VPERM operands that should be used as the first
3197// and second shift operand respectively.
3198static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes,
3199 unsigned &StartIndex, unsigned &OpNo0,
3200 unsigned &OpNo1) {
3201 int OpNos[] = { -1, -1 };
3202 int Shift = -1;
3203 for (unsigned I = 0; I < 16; ++I) {
3204 int Index = Bytes[I];
3205 if (Index >= 0) {
3206 int ExpectedShift = (Index - I) % SystemZ::VectorBytes;
3207 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes;
3208 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes;
3209 if (Shift < 0)
3210 Shift = ExpectedShift;
3211 else if (Shift != ExpectedShift)
3212 return false;
3213 // Make sure that the operand mappings are consistent with previous
3214 // elements.
3215 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3216 return false;
3217 OpNos[ModelOpNo] = RealOpNo;
3218 }
3219 }
3220 StartIndex = Shift;
3221 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3222}
3223
3224// Create a node that performs P on operands Op0 and Op1, casting the
3225// operands to the appropriate type. The type of the result is determined by P.
3226static SDValue getPermuteNode(SelectionDAG &DAG, SDLoc DL,
3227 const Permute &P, SDValue Op0, SDValue Op1) {
3228 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input
3229 // elements of a PACK are twice as wide as the outputs.
3230 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 :
3231 P.Opcode == SystemZISD::PACK ? P.Operand * 2 :
3232 P.Operand);
3233 // Cast both operands to the appropriate type.
3234 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8),
3235 SystemZ::VectorBytes / InBytes);
3236 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0);
3237 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1);
3238 SDValue Op;
3239 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) {
3240 SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32);
3241 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2);
3242 } else if (P.Opcode == SystemZISD::PACK) {
3243 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8),
3244 SystemZ::VectorBytes / P.Operand);
3245 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1);
3246 } else {
3247 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1);
3248 }
3249 return Op;
3250}
3251
3252// Bytes is a VPERM-like permute vector, except that -1 is used for
3253// undefined bytes. Implement it on operands Ops[0] and Ops[1] using
3254// VSLDI or VPERM.
3255static SDValue getGeneralPermuteNode(SelectionDAG &DAG, SDLoc DL, SDValue *Ops,
3256 const SmallVectorImpl<int> &Bytes) {
3257 for (unsigned I = 0; I < 2; ++I)
3258 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]);
3259
3260 // First see whether VSLDI can be used.
3261 unsigned StartIndex, OpNo0, OpNo1;
3262 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1))
3263 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0],
3264 Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32));
3265
3266 // Fall back on VPERM. Construct an SDNode for the permute vector.
3267 SDValue IndexNodes[SystemZ::VectorBytes];
3268 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3269 if (Bytes[I] >= 0)
3270 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32);
3271 else
3272 IndexNodes[I] = DAG.getUNDEF(MVT::i32);
3273 SDValue Op2 = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v16i8, IndexNodes);
3274 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2);
3275}
3276
3277namespace {
3278// Describes a general N-operand vector shuffle.
3279struct GeneralShuffle {
3280 GeneralShuffle(EVT vt) : VT(vt) {}
3281 void addUndef();
3282 void add(SDValue, unsigned);
3283 SDValue getNode(SelectionDAG &, SDLoc);
3284
3285 // The operands of the shuffle.
3286 SmallVector<SDValue, SystemZ::VectorBytes> Ops;
3287
3288 // Index I is -1 if byte I of the result is undefined. Otherwise the
3289 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand
3290 // Bytes[I] / SystemZ::VectorBytes.
3291 SmallVector<int, SystemZ::VectorBytes> Bytes;
3292
3293 // The type of the shuffle result.
3294 EVT VT;
3295};
3296}
3297
3298// Add an extra undefined element to the shuffle.
3299void GeneralShuffle::addUndef() {
3300 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3301 for (unsigned I = 0; I < BytesPerElement; ++I)
3302 Bytes.push_back(-1);
3303}
3304
3305// Add an extra element to the shuffle, taking it from element Elem of Op.
3306// A null Op indicates a vector input whose value will be calculated later;
3307// there is at most one such input per shuffle and it always has the same
3308// type as the result.
3309void GeneralShuffle::add(SDValue Op, unsigned Elem) {
3310 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3311
3312 // The source vector can have wider elements than the result,
3313 // either through an explicit TRUNCATE or because of type legalization.
3314 // We want the least significant part.
3315 EVT FromVT = Op.getNode() ? Op.getValueType() : VT;
3316 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize();
3317 assert(FromBytesPerElement >= BytesPerElement &&
3318 "Invalid EXTRACT_VECTOR_ELT");
3319 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes +
3320 (FromBytesPerElement - BytesPerElement));
3321
3322 // Look through things like shuffles and bitcasts.
3323 while (Op.getNode()) {
3324 if (Op.getOpcode() == ISD::BITCAST)
3325 Op = Op.getOperand(0);
3326 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) {
3327 // See whether the bytes we need come from a contiguous part of one
3328 // operand.
3329 SmallVector<int, SystemZ::VectorBytes> OpBytes;
3330 getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes);
3331 int NewByte;
3332 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte))
3333 break;
3334 if (NewByte < 0) {
3335 addUndef();
3336 return;
3337 }
3338 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes);
3339 Byte = unsigned(NewByte) % SystemZ::VectorBytes;
3340 } else if (Op.getOpcode() == ISD::UNDEF) {
3341 addUndef();
3342 return;
3343 } else
3344 break;
3345 }
3346
3347 // Make sure that the source of the extraction is in Ops.
3348 unsigned OpNo = 0;
3349 for (; OpNo < Ops.size(); ++OpNo)
3350 if (Ops[OpNo] == Op)
3351 break;
3352 if (OpNo == Ops.size())
3353 Ops.push_back(Op);
3354
3355 // Add the element to Bytes.
3356 unsigned Base = OpNo * SystemZ::VectorBytes + Byte;
3357 for (unsigned I = 0; I < BytesPerElement; ++I)
3358 Bytes.push_back(Base + I);
3359}
3360
3361// Return SDNodes for the completed shuffle.
3362SDValue GeneralShuffle::getNode(SelectionDAG &DAG, SDLoc DL) {
3363 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector");
3364
3365 if (Ops.size() == 0)
3366 return DAG.getUNDEF(VT);
3367
3368 // Make sure that there are at least two shuffle operands.
3369 if (Ops.size() == 1)
3370 Ops.push_back(DAG.getUNDEF(MVT::v16i8));
3371
3372 // Create a tree of shuffles, deferring root node until after the loop.
3373 // Try to redistribute the undefined elements of non-root nodes so that
3374 // the non-root shuffles match something like a pack or merge, then adjust
3375 // the parent node's permute vector to compensate for the new order.
3376 // Among other things, this copes with vectors like <2 x i16> that were
3377 // padded with undefined elements during type legalization.
3378 //
3379 // In the best case this redistribution will lead to the whole tree
3380 // using packs and merges. It should rarely be a loss in other cases.
3381 unsigned Stride = 1;
3382 for (; Stride * 2 < Ops.size(); Stride *= 2) {
3383 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) {
3384 SDValue SubOps[] = { Ops[I], Ops[I + Stride] };
3385
3386 // Create a mask for just these two operands.
3387 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes);
3388 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3389 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes;
3390 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes;
3391 if (OpNo == I)
3392 NewBytes[J] = Byte;
3393 else if (OpNo == I + Stride)
3394 NewBytes[J] = SystemZ::VectorBytes + Byte;
3395 else
3396 NewBytes[J] = -1;
3397 }
3398 // See if it would be better to reorganize NewMask to avoid using VPERM.
3399 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes);
3400 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) {
3401 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]);
3402 // Applying NewBytesMap to Ops[I] gets back to NewBytes.
3403 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3404 if (NewBytes[J] >= 0) {
3405 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes &&
3406 "Invalid double permute");
3407 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J];
3408 } else
3409 assert(NewBytesMap[J] < 0 && "Invalid double permute");
3410 }
3411 } else {
3412 // Just use NewBytes on the operands.
3413 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes);
3414 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J)
3415 if (NewBytes[J] >= 0)
3416 Bytes[J] = I * SystemZ::VectorBytes + J;
3417 }
3418 }
3419 }
3420
3421 // Now we just have 2 inputs. Put the second operand in Ops[1].
3422 if (Stride > 1) {
3423 Ops[1] = Ops[Stride];
3424 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3425 if (Bytes[I] >= int(SystemZ::VectorBytes))
3426 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes;
3427 }
3428
3429 // Look for an instruction that can do the permute without resorting
3430 // to VPERM.
3431 unsigned OpNo0, OpNo1;
3432 SDValue Op;
3433 if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1))
3434 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]);
3435 else
3436 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes);
3437 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3438}
3439
Ulrich Weigandcd808232015-05-05 19:26:48 +00003440// Return true if the given BUILD_VECTOR is a scalar-to-vector conversion.
3441static bool isScalarToVector(SDValue Op) {
3442 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I)
3443 if (Op.getOperand(I).getOpcode() != ISD::UNDEF)
3444 return false;
3445 return true;
3446}
3447
3448// Return a vector of type VT that contains Value in the first element.
3449// The other elements don't matter.
3450static SDValue buildScalarToVector(SelectionDAG &DAG, SDLoc DL, EVT VT,
3451 SDValue Value) {
3452 // If we have a constant, replicate it to all elements and let the
3453 // BUILD_VECTOR lowering take care of it.
3454 if (Value.getOpcode() == ISD::Constant ||
3455 Value.getOpcode() == ISD::ConstantFP) {
3456 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value);
3457 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
3458 }
3459 if (Value.getOpcode() == ISD::UNDEF)
3460 return DAG.getUNDEF(VT);
3461 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
3462}
3463
3464// Return a vector of type VT in which Op0 is in element 0 and Op1 is in
3465// element 1. Used for cases in which replication is cheap.
3466static SDValue buildMergeScalars(SelectionDAG &DAG, SDLoc DL, EVT VT,
3467 SDValue Op0, SDValue Op1) {
3468 if (Op0.getOpcode() == ISD::UNDEF) {
3469 if (Op1.getOpcode() == ISD::UNDEF)
3470 return DAG.getUNDEF(VT);
3471 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1);
3472 }
3473 if (Op1.getOpcode() == ISD::UNDEF)
3474 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0);
3475 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT,
3476 buildScalarToVector(DAG, DL, VT, Op0),
3477 buildScalarToVector(DAG, DL, VT, Op1));
3478}
3479
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003480// Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64
3481// vector for them.
3482static SDValue joinDwords(SelectionDAG &DAG, SDLoc DL, SDValue Op0,
3483 SDValue Op1) {
3484 if (Op0.getOpcode() == ISD::UNDEF && Op1.getOpcode() == ISD::UNDEF)
3485 return DAG.getUNDEF(MVT::v2i64);
3486 // If one of the two inputs is undefined then replicate the other one,
3487 // in order to avoid using another register unnecessarily.
3488 if (Op0.getOpcode() == ISD::UNDEF)
3489 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
3490 else if (Op1.getOpcode() == ISD::UNDEF)
3491 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
3492 else {
3493 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
3494 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
3495 }
3496 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1);
3497}
3498
3499// Try to represent constant BUILD_VECTOR node BVN using a
3500// SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask
3501// on success.
3502static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) {
3503 EVT ElemVT = BVN->getValueType(0).getVectorElementType();
3504 unsigned BytesPerElement = ElemVT.getStoreSize();
3505 for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) {
3506 SDValue Op = BVN->getOperand(I);
3507 if (Op.getOpcode() != ISD::UNDEF) {
3508 uint64_t Value;
3509 if (Op.getOpcode() == ISD::Constant)
3510 Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue();
3511 else if (Op.getOpcode() == ISD::ConstantFP)
3512 Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt()
3513 .getZExtValue());
3514 else
3515 return false;
3516 for (unsigned J = 0; J < BytesPerElement; ++J) {
3517 uint64_t Byte = (Value >> (J * 8)) & 0xff;
3518 if (Byte == 0xff)
3519 Mask |= 1 << ((E - I - 1) * BytesPerElement + J);
3520 else if (Byte != 0)
3521 return false;
3522 }
3523 }
3524 }
3525 return true;
3526}
3527
3528// Try to load a vector constant in which BitsPerElement-bit value Value
3529// is replicated to fill the vector. VT is the type of the resulting
3530// constant, which may have elements of a different size from BitsPerElement.
3531// Return the SDValue of the constant on success, otherwise return
3532// an empty value.
3533static SDValue tryBuildVectorReplicate(SelectionDAG &DAG,
3534 const SystemZInstrInfo *TII,
3535 SDLoc DL, EVT VT, uint64_t Value,
3536 unsigned BitsPerElement) {
3537 // Signed 16-bit values can be replicated using VREPI.
3538 int64_t SignedValue = SignExtend64(Value, BitsPerElement);
3539 if (isInt<16>(SignedValue)) {
3540 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
3541 SystemZ::VectorBits / BitsPerElement);
3542 SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT,
3543 DAG.getConstant(SignedValue, DL, MVT::i32));
3544 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3545 }
3546 // See whether rotating the constant left some N places gives a value that
3547 // is one less than a power of 2 (i.e. all zeros followed by all ones).
3548 // If so we can use VGM.
3549 unsigned Start, End;
3550 if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) {
3551 // isRxSBGMask returns the bit numbers for a full 64-bit value,
3552 // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to
3553 // bit numbers for an BitsPerElement value, so that 0 denotes
3554 // 1 << (BitsPerElement-1).
3555 Start -= 64 - BitsPerElement;
3556 End -= 64 - BitsPerElement;
3557 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
3558 SystemZ::VectorBits / BitsPerElement);
3559 SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT,
3560 DAG.getConstant(Start, DL, MVT::i32),
3561 DAG.getConstant(End, DL, MVT::i32));
3562 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3563 }
3564 return SDValue();
3565}
3566
3567// If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually
3568// better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for
3569// the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR
3570// would benefit from this representation and return it if so.
3571static SDValue tryBuildVectorShuffle(SelectionDAG &DAG,
3572 BuildVectorSDNode *BVN) {
3573 EVT VT = BVN->getValueType(0);
3574 unsigned NumElements = VT.getVectorNumElements();
3575
3576 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation
3577 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still
3578 // need a BUILD_VECTOR, add an additional placeholder operand for that
3579 // BUILD_VECTOR and store its operands in ResidueOps.
3580 GeneralShuffle GS(VT);
3581 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps;
3582 bool FoundOne = false;
3583 for (unsigned I = 0; I < NumElements; ++I) {
3584 SDValue Op = BVN->getOperand(I);
3585 if (Op.getOpcode() == ISD::TRUNCATE)
3586 Op = Op.getOperand(0);
3587 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
3588 Op.getOperand(1).getOpcode() == ISD::Constant) {
3589 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
3590 GS.add(Op.getOperand(0), Elem);
3591 FoundOne = true;
3592 } else if (Op.getOpcode() == ISD::UNDEF) {
3593 GS.addUndef();
3594 } else {
3595 GS.add(SDValue(), ResidueOps.size());
3596 ResidueOps.push_back(Op);
3597 }
3598 }
3599
3600 // Nothing to do if there are no EXTRACT_VECTOR_ELTs.
3601 if (!FoundOne)
3602 return SDValue();
3603
3604 // Create the BUILD_VECTOR for the remaining elements, if any.
3605 if (!ResidueOps.empty()) {
3606 while (ResidueOps.size() < NumElements)
3607 ResidueOps.push_back(DAG.getUNDEF(VT.getVectorElementType()));
3608 for (auto &Op : GS.Ops) {
3609 if (!Op.getNode()) {
3610 Op = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BVN), VT, ResidueOps);
3611 break;
3612 }
3613 }
3614 }
3615 return GS.getNode(DAG, SDLoc(BVN));
3616}
3617
3618// Combine GPR scalar values Elems into a vector of type VT.
3619static SDValue buildVector(SelectionDAG &DAG, SDLoc DL, EVT VT,
3620 SmallVectorImpl<SDValue> &Elems) {
3621 // See whether there is a single replicated value.
3622 SDValue Single;
3623 unsigned int NumElements = Elems.size();
3624 unsigned int Count = 0;
3625 for (auto Elem : Elems) {
3626 if (Elem.getOpcode() != ISD::UNDEF) {
3627 if (!Single.getNode())
3628 Single = Elem;
3629 else if (Elem != Single) {
3630 Single = SDValue();
3631 break;
3632 }
3633 Count += 1;
3634 }
3635 }
3636 // There are three cases here:
3637 //
3638 // - if the only defined element is a loaded one, the best sequence
3639 // is a replicating load.
3640 //
3641 // - otherwise, if the only defined element is an i64 value, we will
3642 // end up with the same VLVGP sequence regardless of whether we short-cut
3643 // for replication or fall through to the later code.
3644 //
3645 // - otherwise, if the only defined element is an i32 or smaller value,
3646 // we would need 2 instructions to replicate it: VLVGP followed by VREPx.
3647 // This is only a win if the single defined element is used more than once.
3648 // In other cases we're better off using a single VLVGx.
3649 if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD))
3650 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single);
3651
3652 // The best way of building a v2i64 from two i64s is to use VLVGP.
3653 if (VT == MVT::v2i64)
3654 return joinDwords(DAG, DL, Elems[0], Elems[1]);
3655
Ulrich Weigandcd808232015-05-05 19:26:48 +00003656 // Use a 64-bit merge high to combine two doubles.
3657 if (VT == MVT::v2f64)
3658 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
3659
Ulrich Weigand80b3af72015-05-05 19:27:45 +00003660 // Build v4f32 values directly from the FPRs:
3661 //
3662 // <Axxx> <Bxxx> <Cxxxx> <Dxxx>
3663 // V V VMRHF
3664 // <ABxx> <CDxx>
3665 // V VMRHG
3666 // <ABCD>
3667 if (VT == MVT::v4f32) {
3668 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
3669 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]);
3670 // Avoid unnecessary undefs by reusing the other operand.
3671 if (Op01.getOpcode() == ISD::UNDEF)
3672 Op01 = Op23;
3673 else if (Op23.getOpcode() == ISD::UNDEF)
3674 Op23 = Op01;
3675 // Merging identical replications is a no-op.
3676 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23)
3677 return Op01;
3678 Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01);
3679 Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23);
3680 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH,
3681 DL, MVT::v2i64, Op01, Op23);
3682 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3683 }
3684
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003685 // Collect the constant terms.
3686 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue());
3687 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false);
3688
3689 unsigned NumConstants = 0;
3690 for (unsigned I = 0; I < NumElements; ++I) {
3691 SDValue Elem = Elems[I];
3692 if (Elem.getOpcode() == ISD::Constant ||
3693 Elem.getOpcode() == ISD::ConstantFP) {
3694 NumConstants += 1;
3695 Constants[I] = Elem;
3696 Done[I] = true;
3697 }
3698 }
3699 // If there was at least one constant, fill in the other elements of
3700 // Constants with undefs to get a full vector constant and use that
3701 // as the starting point.
3702 SDValue Result;
3703 if (NumConstants > 0) {
3704 for (unsigned I = 0; I < NumElements; ++I)
3705 if (!Constants[I].getNode())
3706 Constants[I] = DAG.getUNDEF(Elems[I].getValueType());
3707 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Constants);
3708 } else {
3709 // Otherwise try to use VLVGP to start the sequence in order to
3710 // avoid a false dependency on any previous contents of the vector
3711 // register. This only makes sense if one of the associated elements
3712 // is defined.
3713 unsigned I1 = NumElements / 2 - 1;
3714 unsigned I2 = NumElements - 1;
3715 bool Def1 = (Elems[I1].getOpcode() != ISD::UNDEF);
3716 bool Def2 = (Elems[I2].getOpcode() != ISD::UNDEF);
3717 if (Def1 || Def2) {
3718 SDValue Elem1 = Elems[Def1 ? I1 : I2];
3719 SDValue Elem2 = Elems[Def2 ? I2 : I1];
3720 Result = DAG.getNode(ISD::BITCAST, DL, VT,
3721 joinDwords(DAG, DL, Elem1, Elem2));
3722 Done[I1] = true;
3723 Done[I2] = true;
3724 } else
3725 Result = DAG.getUNDEF(VT);
3726 }
3727
3728 // Use VLVGx to insert the other elements.
3729 for (unsigned I = 0; I < NumElements; ++I)
3730 if (!Done[I] && Elems[I].getOpcode() != ISD::UNDEF)
3731 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I],
3732 DAG.getConstant(I, DL, MVT::i32));
3733 return Result;
3734}
3735
3736SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op,
3737 SelectionDAG &DAG) const {
3738 const SystemZInstrInfo *TII =
3739 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
3740 auto *BVN = cast<BuildVectorSDNode>(Op.getNode());
3741 SDLoc DL(Op);
3742 EVT VT = Op.getValueType();
3743
3744 if (BVN->isConstant()) {
3745 // Try using VECTOR GENERATE BYTE MASK. This is the architecturally-
3746 // preferred way of creating all-zero and all-one vectors so give it
3747 // priority over other methods below.
3748 uint64_t Mask = 0;
3749 if (tryBuildVectorByteMask(BVN, Mask)) {
3750 SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3751 DAG.getConstant(Mask, DL, MVT::i32));
3752 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3753 }
3754
3755 // Try using some form of replication.
3756 APInt SplatBits, SplatUndef;
3757 unsigned SplatBitSize;
3758 bool HasAnyUndefs;
3759 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
3760 8, true) &&
3761 SplatBitSize <= 64) {
3762 // First try assuming that any undefined bits above the highest set bit
3763 // and below the lowest set bit are 1s. This increases the likelihood of
3764 // being able to use a sign-extended element value in VECTOR REPLICATE
3765 // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK.
3766 uint64_t SplatBitsZ = SplatBits.getZExtValue();
3767 uint64_t SplatUndefZ = SplatUndef.getZExtValue();
3768 uint64_t Lower = (SplatUndefZ
3769 & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1));
3770 uint64_t Upper = (SplatUndefZ
3771 & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1));
3772 uint64_t Value = SplatBitsZ | Upper | Lower;
3773 SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value,
3774 SplatBitSize);
3775 if (Op.getNode())
3776 return Op;
3777
3778 // Now try assuming that any undefined bits between the first and
3779 // last defined set bits are set. This increases the chances of
3780 // using a non-wraparound mask.
3781 uint64_t Middle = SplatUndefZ & ~Upper & ~Lower;
3782 Value = SplatBitsZ | Middle;
3783 Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize);
3784 if (Op.getNode())
3785 return Op;
3786 }
3787
3788 // Fall back to loading it from memory.
3789 return SDValue();
3790 }
3791
3792 // See if we should use shuffles to construct the vector from other vectors.
3793 SDValue Res = tryBuildVectorShuffle(DAG, BVN);
3794 if (Res.getNode())
3795 return Res;
3796
Ulrich Weigandcd808232015-05-05 19:26:48 +00003797 // Detect SCALAR_TO_VECTOR conversions.
3798 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op))
3799 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0));
3800
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003801 // Otherwise use buildVector to build the vector up from GPRs.
3802 unsigned NumElements = Op.getNumOperands();
3803 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements);
3804 for (unsigned I = 0; I < NumElements; ++I)
3805 Ops[I] = Op.getOperand(I);
3806 return buildVector(DAG, DL, VT, Ops);
3807}
3808
3809SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
3810 SelectionDAG &DAG) const {
3811 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode());
3812 SDLoc DL(Op);
3813 EVT VT = Op.getValueType();
3814 unsigned NumElements = VT.getVectorNumElements();
3815
3816 if (VSN->isSplat()) {
3817 SDValue Op0 = Op.getOperand(0);
3818 unsigned Index = VSN->getSplatIndex();
3819 assert(Index < VT.getVectorNumElements() &&
3820 "Splat index should be defined and in first operand");
3821 // See whether the value we're splatting is directly available as a scalar.
3822 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
3823 Op0.getOpcode() == ISD::BUILD_VECTOR)
3824 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index));
3825 // Otherwise keep it as a vector-to-vector operation.
3826 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0),
3827 DAG.getConstant(Index, DL, MVT::i32));
3828 }
3829
3830 GeneralShuffle GS(VT);
3831 for (unsigned I = 0; I < NumElements; ++I) {
3832 int Elt = VSN->getMaskElt(I);
3833 if (Elt < 0)
3834 GS.addUndef();
3835 else
3836 GS.add(Op.getOperand(unsigned(Elt) / NumElements),
3837 unsigned(Elt) % NumElements);
3838 }
3839 return GS.getNode(DAG, SDLoc(VSN));
3840}
3841
3842SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
3843 SelectionDAG &DAG) const {
3844 SDLoc DL(Op);
3845 // Just insert the scalar into element 0 of an undefined vector.
3846 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL,
3847 Op.getValueType(), DAG.getUNDEF(Op.getValueType()),
3848 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32));
3849}
3850
Ulrich Weigandcd808232015-05-05 19:26:48 +00003851SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
3852 SelectionDAG &DAG) const {
3853 // Handle insertions of floating-point values.
3854 SDLoc DL(Op);
3855 SDValue Op0 = Op.getOperand(0);
3856 SDValue Op1 = Op.getOperand(1);
3857 SDValue Op2 = Op.getOperand(2);
3858 EVT VT = Op.getValueType();
3859
Ulrich Weigand80b3af72015-05-05 19:27:45 +00003860 // Insertions into constant indices of a v2f64 can be done using VPDI.
3861 // However, if the inserted value is a bitcast or a constant then it's
3862 // better to use GPRs, as below.
3863 if (VT == MVT::v2f64 &&
3864 Op1.getOpcode() != ISD::BITCAST &&
Ulrich Weigandcd808232015-05-05 19:26:48 +00003865 Op1.getOpcode() != ISD::ConstantFP &&
3866 Op2.getOpcode() == ISD::Constant) {
3867 uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue();
3868 unsigned Mask = VT.getVectorNumElements() - 1;
3869 if (Index <= Mask)
3870 return Op;
3871 }
3872
3873 // Otherwise bitcast to the equivalent integer form and insert via a GPR.
3874 MVT IntVT = MVT::getIntegerVT(VT.getVectorElementType().getSizeInBits());
3875 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements());
3876 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT,
3877 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0),
3878 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2);
3879 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
3880}
3881
3882SDValue
3883SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
3884 SelectionDAG &DAG) const {
3885 // Handle extractions of floating-point values.
3886 SDLoc DL(Op);
3887 SDValue Op0 = Op.getOperand(0);
3888 SDValue Op1 = Op.getOperand(1);
3889 EVT VT = Op.getValueType();
3890 EVT VecVT = Op0.getValueType();
3891
3892 // Extractions of constant indices can be done directly.
3893 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) {
3894 uint64_t Index = CIndexN->getZExtValue();
3895 unsigned Mask = VecVT.getVectorNumElements() - 1;
3896 if (Index <= Mask)
3897 return Op;
3898 }
3899
3900 // Otherwise bitcast to the equivalent integer form and extract via a GPR.
3901 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
3902 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements());
3903 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT,
3904 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1);
3905 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
3906}
3907
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003908SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG,
3909 unsigned ByScalar) const {
3910 // Look for cases where a vector shift can use the *_BY_SCALAR form.
3911 SDValue Op0 = Op.getOperand(0);
3912 SDValue Op1 = Op.getOperand(1);
3913 SDLoc DL(Op);
3914 EVT VT = Op.getValueType();
3915 unsigned ElemBitSize = VT.getVectorElementType().getSizeInBits();
3916
3917 // See whether the shift vector is a splat represented as BUILD_VECTOR.
3918 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) {
3919 APInt SplatBits, SplatUndef;
3920 unsigned SplatBitSize;
3921 bool HasAnyUndefs;
3922 // Check for constant splats. Use ElemBitSize as the minimum element
3923 // width and reject splats that need wider elements.
3924 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
3925 ElemBitSize, true) &&
3926 SplatBitSize == ElemBitSize) {
3927 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff,
3928 DL, MVT::i32);
3929 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
3930 }
3931 // Check for variable splats.
3932 BitVector UndefElements;
3933 SDValue Splat = BVN->getSplatValue(&UndefElements);
3934 if (Splat) {
3935 // Since i32 is the smallest legal type, we either need a no-op
3936 // or a truncation.
3937 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat);
3938 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
3939 }
3940 }
3941
3942 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR,
3943 // and the shift amount is directly available in a GPR.
3944 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) {
3945 if (VSN->isSplat()) {
3946 SDValue VSNOp0 = VSN->getOperand(0);
3947 unsigned Index = VSN->getSplatIndex();
3948 assert(Index < VT.getVectorNumElements() &&
3949 "Splat index should be defined and in first operand");
3950 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
3951 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) {
3952 // Since i32 is the smallest legal type, we either need a no-op
3953 // or a truncation.
3954 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32,
3955 VSNOp0.getOperand(Index));
3956 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
3957 }
3958 }
3959 }
3960
3961 // Otherwise just treat the current form as legal.
3962 return Op;
3963}
3964
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003965SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
3966 SelectionDAG &DAG) const {
3967 switch (Op.getOpcode()) {
3968 case ISD::BR_CC:
3969 return lowerBR_CC(Op, DAG);
3970 case ISD::SELECT_CC:
3971 return lowerSELECT_CC(Op, DAG);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00003972 case ISD::SETCC:
3973 return lowerSETCC(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003974 case ISD::GlobalAddress:
3975 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
3976 case ISD::GlobalTLSAddress:
3977 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
3978 case ISD::BlockAddress:
3979 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
3980 case ISD::JumpTable:
3981 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
3982 case ISD::ConstantPool:
3983 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
3984 case ISD::BITCAST:
3985 return lowerBITCAST(Op, DAG);
3986 case ISD::VASTART:
3987 return lowerVASTART(Op, DAG);
3988 case ISD::VACOPY:
3989 return lowerVACOPY(Op, DAG);
3990 case ISD::DYNAMIC_STACKALLOC:
3991 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003992 case ISD::SMUL_LOHI:
3993 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003994 case ISD::UMUL_LOHI:
3995 return lowerUMUL_LOHI(Op, DAG);
3996 case ISD::SDIVREM:
3997 return lowerSDIVREM(Op, DAG);
3998 case ISD::UDIVREM:
3999 return lowerUDIVREM(Op, DAG);
4000 case ISD::OR:
4001 return lowerOR(Op, DAG);
Ulrich Weigandb4012182015-03-31 12:56:33 +00004002 case ISD::CTPOP:
4003 return lowerCTPOP(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004004 case ISD::CTLZ_ZERO_UNDEF:
4005 return DAG.getNode(ISD::CTLZ, SDLoc(Op),
4006 Op.getValueType(), Op.getOperand(0));
4007 case ISD::CTTZ_ZERO_UNDEF:
4008 return DAG.getNode(ISD::CTTZ, SDLoc(Op),
4009 Op.getValueType(), Op.getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004010 case ISD::ATOMIC_SWAP:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004011 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
4012 case ISD::ATOMIC_STORE:
4013 return lowerATOMIC_STORE(Op, DAG);
4014 case ISD::ATOMIC_LOAD:
4015 return lowerATOMIC_LOAD(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004016 case ISD::ATOMIC_LOAD_ADD:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004017 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004018 case ISD::ATOMIC_LOAD_SUB:
Richard Sandiford41350a52013-12-24 15:18:04 +00004019 return lowerATOMIC_LOAD_SUB(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004020 case ISD::ATOMIC_LOAD_AND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004021 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004022 case ISD::ATOMIC_LOAD_OR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004023 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004024 case ISD::ATOMIC_LOAD_XOR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004025 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004026 case ISD::ATOMIC_LOAD_NAND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004027 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004028 case ISD::ATOMIC_LOAD_MIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004029 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004030 case ISD::ATOMIC_LOAD_MAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004031 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004032 case ISD::ATOMIC_LOAD_UMIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004033 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004034 case ISD::ATOMIC_LOAD_UMAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004035 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004036 case ISD::ATOMIC_CMP_SWAP:
4037 return lowerATOMIC_CMP_SWAP(Op, DAG);
4038 case ISD::STACKSAVE:
4039 return lowerSTACKSAVE(Op, DAG);
4040 case ISD::STACKRESTORE:
4041 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00004042 case ISD::PREFETCH:
4043 return lowerPREFETCH(Op, DAG);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004044 case ISD::INTRINSIC_W_CHAIN:
4045 return lowerINTRINSIC_W_CHAIN(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004046 case ISD::BUILD_VECTOR:
4047 return lowerBUILD_VECTOR(Op, DAG);
4048 case ISD::VECTOR_SHUFFLE:
4049 return lowerVECTOR_SHUFFLE(Op, DAG);
4050 case ISD::SCALAR_TO_VECTOR:
4051 return lowerSCALAR_TO_VECTOR(Op, DAG);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004052 case ISD::INSERT_VECTOR_ELT:
4053 return lowerINSERT_VECTOR_ELT(Op, DAG);
4054 case ISD::EXTRACT_VECTOR_ELT:
4055 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004056 case ISD::SHL:
4057 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR);
4058 case ISD::SRL:
4059 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR);
4060 case ISD::SRA:
4061 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004062 default:
4063 llvm_unreachable("Unexpected node to lower");
4064 }
4065}
4066
4067const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
4068#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
4069 switch (Opcode) {
4070 OPCODE(RET_FLAG);
4071 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00004072 OPCODE(SIBCALL);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004073 OPCODE(TLS_GDCALL);
4074 OPCODE(TLS_LDCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004075 OPCODE(PCREL_WRAPPER);
Richard Sandiford54b36912013-09-27 15:14:04 +00004076 OPCODE(PCREL_OFFSET);
Richard Sandiford57485472013-12-13 15:35:00 +00004077 OPCODE(IABS);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00004078 OPCODE(ICMP);
4079 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00004080 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004081 OPCODE(BR_CCMASK);
4082 OPCODE(SELECT_CCMASK);
4083 OPCODE(ADJDYNALLOC);
4084 OPCODE(EXTRACT_ACCESS);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004085 OPCODE(POPCNT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004086 OPCODE(UMUL_LOHI64);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004087 OPCODE(SDIVREM32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004088 OPCODE(SDIVREM64);
4089 OPCODE(UDIVREM32);
4090 OPCODE(UDIVREM64);
Richard Sandifordd131ff82013-07-08 09:35:23 +00004091 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004092 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00004093 OPCODE(NC);
4094 OPCODE(NC_LOOP);
4095 OPCODE(OC);
4096 OPCODE(OC_LOOP);
4097 OPCODE(XC);
4098 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00004099 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004100 OPCODE(CLC_LOOP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00004101 OPCODE(STPCPY);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004102 OPCODE(STRCMP);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00004103 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00004104 OPCODE(IPM);
Richard Sandiford9afe6132013-12-10 10:36:34 +00004105 OPCODE(SERIALIZE);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004106 OPCODE(TBEGIN);
4107 OPCODE(TBEGIN_NOFLOAT);
4108 OPCODE(TEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004109 OPCODE(BYTE_MASK);
4110 OPCODE(ROTATE_MASK);
4111 OPCODE(REPLICATE);
4112 OPCODE(JOIN_DWORDS);
4113 OPCODE(SPLAT);
4114 OPCODE(MERGE_HIGH);
4115 OPCODE(MERGE_LOW);
4116 OPCODE(SHL_DOUBLE);
4117 OPCODE(PERMUTE_DWORDS);
4118 OPCODE(PERMUTE);
4119 OPCODE(PACK);
4120 OPCODE(VSHL_BY_SCALAR);
4121 OPCODE(VSRL_BY_SCALAR);
4122 OPCODE(VSRA_BY_SCALAR);
4123 OPCODE(VSUM);
4124 OPCODE(VICMPE);
4125 OPCODE(VICMPH);
4126 OPCODE(VICMPHL);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004127 OPCODE(VFCMPE);
4128 OPCODE(VFCMPH);
4129 OPCODE(VFCMPHE);
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004130 OPCODE(VEXTEND);
4131 OPCODE(VROUND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004132 OPCODE(ATOMIC_SWAPW);
4133 OPCODE(ATOMIC_LOADW_ADD);
4134 OPCODE(ATOMIC_LOADW_SUB);
4135 OPCODE(ATOMIC_LOADW_AND);
4136 OPCODE(ATOMIC_LOADW_OR);
4137 OPCODE(ATOMIC_LOADW_XOR);
4138 OPCODE(ATOMIC_LOADW_NAND);
4139 OPCODE(ATOMIC_LOADW_MIN);
4140 OPCODE(ATOMIC_LOADW_MAX);
4141 OPCODE(ATOMIC_LOADW_UMIN);
4142 OPCODE(ATOMIC_LOADW_UMAX);
4143 OPCODE(ATOMIC_CMP_SWAPW);
Richard Sandiford03481332013-08-23 11:36:42 +00004144 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004145 }
Craig Topper062a2ba2014-04-25 05:30:21 +00004146 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004147#undef OPCODE
4148}
4149
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004150// Return true if VT is a vector whose elements are a whole number of bytes
4151// in width.
4152static bool canTreatAsByteVector(EVT VT) {
4153 return VT.isVector() && VT.getVectorElementType().getSizeInBits() % 8 == 0;
4154}
4155
4156// Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT
4157// producing a result of type ResVT. Op is a possibly bitcast version
4158// of the input vector and Index is the index (based on type VecVT) that
4159// should be extracted. Return the new extraction if a simplification
4160// was possible or if Force is true.
4161SDValue SystemZTargetLowering::combineExtract(SDLoc DL, EVT ResVT, EVT VecVT,
4162 SDValue Op, unsigned Index,
4163 DAGCombinerInfo &DCI,
4164 bool Force) const {
4165 SelectionDAG &DAG = DCI.DAG;
4166
4167 // The number of bytes being extracted.
4168 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4169
4170 for (;;) {
4171 unsigned Opcode = Op.getOpcode();
4172 if (Opcode == ISD::BITCAST)
4173 // Look through bitcasts.
4174 Op = Op.getOperand(0);
4175 else if (Opcode == ISD::VECTOR_SHUFFLE &&
4176 canTreatAsByteVector(Op.getValueType())) {
4177 // Get a VPERM-like permute mask and see whether the bytes covered
4178 // by the extracted element are a contiguous sequence from one
4179 // source operand.
4180 SmallVector<int, SystemZ::VectorBytes> Bytes;
4181 getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes);
4182 int First;
4183 if (!getShuffleInput(Bytes, Index * BytesPerElement,
4184 BytesPerElement, First))
4185 break;
4186 if (First < 0)
4187 return DAG.getUNDEF(ResVT);
4188 // Make sure the contiguous sequence starts at a multiple of the
4189 // original element size.
4190 unsigned Byte = unsigned(First) % Bytes.size();
4191 if (Byte % BytesPerElement != 0)
4192 break;
4193 // We can get the extracted value directly from an input.
4194 Index = Byte / BytesPerElement;
4195 Op = Op.getOperand(unsigned(First) / Bytes.size());
4196 Force = true;
4197 } else if (Opcode == ISD::BUILD_VECTOR &&
4198 canTreatAsByteVector(Op.getValueType())) {
4199 // We can only optimize this case if the BUILD_VECTOR elements are
4200 // at least as wide as the extracted value.
4201 EVT OpVT = Op.getValueType();
4202 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4203 if (OpBytesPerElement < BytesPerElement)
4204 break;
4205 // Make sure that the least-significant bit of the extracted value
4206 // is the least significant bit of an input.
4207 unsigned End = (Index + 1) * BytesPerElement;
4208 if (End % OpBytesPerElement != 0)
4209 break;
4210 // We're extracting the low part of one operand of the BUILD_VECTOR.
4211 Op = Op.getOperand(End / OpBytesPerElement - 1);
4212 if (!Op.getValueType().isInteger()) {
4213 EVT VT = MVT::getIntegerVT(Op.getValueType().getSizeInBits());
4214 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
4215 DCI.AddToWorklist(Op.getNode());
4216 }
4217 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits());
4218 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
4219 if (VT != ResVT) {
4220 DCI.AddToWorklist(Op.getNode());
4221 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op);
4222 }
4223 return Op;
4224 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||
4225 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG ||
4226 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) &&
4227 canTreatAsByteVector(Op.getValueType()) &&
4228 canTreatAsByteVector(Op.getOperand(0).getValueType())) {
4229 // Make sure that only the unextended bits are significant.
4230 EVT ExtVT = Op.getValueType();
4231 EVT OpVT = Op.getOperand(0).getValueType();
4232 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize();
4233 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4234 unsigned Byte = Index * BytesPerElement;
4235 unsigned SubByte = Byte % ExtBytesPerElement;
4236 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement;
4237 if (SubByte < MinSubByte ||
4238 SubByte + BytesPerElement > ExtBytesPerElement)
4239 break;
4240 // Get the byte offset of the unextended element
4241 Byte = Byte / ExtBytesPerElement * OpBytesPerElement;
4242 // ...then add the byte offset relative to that element.
4243 Byte += SubByte - MinSubByte;
4244 if (Byte % BytesPerElement != 0)
4245 break;
4246 Op = Op.getOperand(0);
4247 Index = Byte / BytesPerElement;
4248 Force = true;
4249 } else
4250 break;
4251 }
4252 if (Force) {
4253 if (Op.getValueType() != VecVT) {
4254 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op);
4255 DCI.AddToWorklist(Op.getNode());
4256 }
4257 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op,
4258 DAG.getConstant(Index, DL, MVT::i32));
4259 }
4260 return SDValue();
4261}
4262
4263// Optimize vector operations in scalar value Op on the basis that Op
4264// is truncated to TruncVT.
4265SDValue
4266SystemZTargetLowering::combineTruncateExtract(SDLoc DL, EVT TruncVT, SDValue Op,
4267 DAGCombinerInfo &DCI) const {
4268 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into
4269 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements
4270 // of type TruncVT.
4271 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4272 TruncVT.getSizeInBits() % 8 == 0) {
4273 SDValue Vec = Op.getOperand(0);
4274 EVT VecVT = Vec.getValueType();
4275 if (canTreatAsByteVector(VecVT)) {
4276 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
4277 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4278 unsigned TruncBytes = TruncVT.getStoreSize();
4279 if (BytesPerElement % TruncBytes == 0) {
4280 // Calculate the value of Y' in the above description. We are
4281 // splitting the original elements into Scale equal-sized pieces
4282 // and for truncation purposes want the last (least-significant)
4283 // of these pieces for IndexN. This is easiest to do by calculating
4284 // the start index of the following element and then subtracting 1.
4285 unsigned Scale = BytesPerElement / TruncBytes;
4286 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1;
4287
4288 // Defer the creation of the bitcast from X to combineExtract,
4289 // which might be able to optimize the extraction.
4290 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8),
4291 VecVT.getStoreSize() / TruncBytes);
4292 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT);
4293 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true);
4294 }
4295 }
4296 }
4297 }
4298 return SDValue();
4299}
4300
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004301SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
4302 DAGCombinerInfo &DCI) const {
4303 SelectionDAG &DAG = DCI.DAG;
4304 unsigned Opcode = N->getOpcode();
4305 if (Opcode == ISD::SIGN_EXTEND) {
4306 // Convert (sext (ashr (shl X, C1), C2)) to
4307 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
4308 // cheap as narrower ones.
4309 SDValue N0 = N->getOperand(0);
4310 EVT VT = N->getValueType(0);
4311 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
4312 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
4313 SDValue Inner = N0.getOperand(0);
4314 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
4315 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
4316 unsigned Extra = (VT.getSizeInBits() -
4317 N0.getValueType().getSizeInBits());
4318 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
4319 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
4320 EVT ShiftVT = N0.getOperand(1).getValueType();
4321 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
4322 Inner.getOperand(0));
4323 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004324 DAG.getConstant(NewShlAmt, SDLoc(Inner),
4325 ShiftVT));
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004326 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004327 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT));
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004328 }
4329 }
4330 }
4331 }
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004332 // (z_merge_high 0, 0) -> 0. This is mostly useful for using VLLEZF
4333 // for v4f32.
4334 if (Opcode == SystemZISD::MERGE_HIGH) {
4335 SDValue Op0 = N->getOperand(0);
4336 SDValue Op1 = N->getOperand(1);
4337 if (Op0 == Op1) {
4338 if (Op0.getOpcode() == ISD::BITCAST)
4339 Op0 = Op0.getOperand(0);
4340 if (Op0.getOpcode() == SystemZISD::BYTE_MASK &&
4341 cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0)
4342 return Op1;
4343 }
4344 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004345 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better
4346 // for the extraction to be done on a vMiN value, so that we can use VSTE.
4347 // If X has wider elements then convert it to:
4348 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z).
4349 if (Opcode == ISD::STORE) {
4350 auto *SN = cast<StoreSDNode>(N);
4351 EVT MemVT = SN->getMemoryVT();
4352 if (MemVT.isInteger()) {
4353 SDValue Value = combineTruncateExtract(SDLoc(N), MemVT,
4354 SN->getValue(), DCI);
4355 if (Value.getNode()) {
4356 DCI.AddToWorklist(Value.getNode());
4357
4358 // Rewrite the store with the new form of stored value.
4359 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value,
4360 SN->getBasePtr(), SN->getMemoryVT(),
4361 SN->getMemOperand());
4362 }
4363 }
4364 }
4365 // Try to simplify a vector extraction.
4366 if (Opcode == ISD::EXTRACT_VECTOR_ELT) {
4367 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
4368 SDValue Op0 = N->getOperand(0);
4369 EVT VecVT = Op0.getValueType();
4370 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0,
4371 IndexN->getZExtValue(), DCI, false);
4372 }
4373 }
4374 // (join_dwords X, X) == (replicate X)
4375 if (Opcode == SystemZISD::JOIN_DWORDS &&
4376 N->getOperand(0) == N->getOperand(1))
4377 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0),
4378 N->getOperand(0));
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004379 // (fround (extract_vector_elt X 0))
4380 // (fround (extract_vector_elt X 1)) ->
4381 // (extract_vector_elt (VROUND X) 0)
4382 // (extract_vector_elt (VROUND X) 1)
4383 //
4384 // This is a special case since the target doesn't really support v2f32s.
4385 if (Opcode == ISD::FP_ROUND) {
4386 SDValue Op0 = N->getOperand(0);
4387 if (N->getValueType(0) == MVT::f32 &&
4388 Op0.hasOneUse() &&
4389 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4390 Op0.getOperand(0).getValueType() == MVT::v2f64 &&
4391 Op0.getOperand(1).getOpcode() == ISD::Constant &&
4392 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) {
4393 SDValue Vec = Op0.getOperand(0);
4394 for (auto *U : Vec->uses()) {
4395 if (U != Op0.getNode() &&
4396 U->hasOneUse() &&
4397 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4398 U->getOperand(0) == Vec &&
4399 U->getOperand(1).getOpcode() == ISD::Constant &&
4400 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) {
4401 SDValue OtherRound = SDValue(*U->use_begin(), 0);
4402 if (OtherRound.getOpcode() == ISD::FP_ROUND &&
4403 OtherRound.getOperand(0) == SDValue(U, 0) &&
4404 OtherRound.getValueType() == MVT::f32) {
4405 SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N),
4406 MVT::v4f32, Vec);
4407 DCI.AddToWorklist(VRound.getNode());
4408 SDValue Extract1 =
4409 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32,
4410 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32));
4411 DCI.AddToWorklist(Extract1.getNode());
4412 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1);
4413 SDValue Extract0 =
4414 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32,
4415 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
4416 return Extract0;
4417 }
4418 }
4419 }
4420 }
4421 }
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004422 return SDValue();
4423}
4424
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004425//===----------------------------------------------------------------------===//
4426// Custom insertion
4427//===----------------------------------------------------------------------===//
4428
4429// Create a new basic block after MBB.
4430static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
4431 MachineFunction &MF = *MBB->getParent();
4432 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00004433 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004434 return NewMBB;
4435}
4436
Richard Sandifordbe133a82013-08-28 09:01:51 +00004437// Split MBB after MI and return the new block (the one that contains
4438// instructions after MI).
4439static MachineBasicBlock *splitBlockAfter(MachineInstr *MI,
4440 MachineBasicBlock *MBB) {
4441 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
4442 NewMBB->splice(NewMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00004443 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Richard Sandifordbe133a82013-08-28 09:01:51 +00004444 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
4445 return NewMBB;
4446}
4447
Richard Sandiford5e318f02013-08-27 09:54:29 +00004448// Split MBB before MI and return the new block (the one that contains MI).
4449static MachineBasicBlock *splitBlockBefore(MachineInstr *MI,
4450 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004451 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004452 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004453 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
4454 return NewMBB;
4455}
4456
Richard Sandiford5e318f02013-08-27 09:54:29 +00004457// Force base value Base into a register before MI. Return the register.
4458static unsigned forceReg(MachineInstr *MI, MachineOperand &Base,
4459 const SystemZInstrInfo *TII) {
4460 if (Base.isReg())
4461 return Base.getReg();
4462
4463 MachineBasicBlock *MBB = MI->getParent();
4464 MachineFunction &MF = *MBB->getParent();
4465 MachineRegisterInfo &MRI = MF.getRegInfo();
4466
4467 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
4468 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg)
4469 .addOperand(Base).addImm(0).addReg(0);
4470 return Reg;
4471}
4472
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004473// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
4474MachineBasicBlock *
4475SystemZTargetLowering::emitSelect(MachineInstr *MI,
4476 MachineBasicBlock *MBB) const {
Eric Christophera6734172015-01-31 00:06:45 +00004477 const SystemZInstrInfo *TII =
4478 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004479
4480 unsigned DestReg = MI->getOperand(0).getReg();
4481 unsigned TrueReg = MI->getOperand(1).getReg();
4482 unsigned FalseReg = MI->getOperand(2).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00004483 unsigned CCValid = MI->getOperand(3).getImm();
4484 unsigned CCMask = MI->getOperand(4).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004485 DebugLoc DL = MI->getDebugLoc();
4486
4487 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004488 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004489 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
4490
4491 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00004492 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004493 // # fallthrough to FalseMBB
4494 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00004495 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4496 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004497 MBB->addSuccessor(JoinMBB);
4498 MBB->addSuccessor(FalseMBB);
4499
4500 // FalseMBB:
4501 // # fallthrough to JoinMBB
4502 MBB = FalseMBB;
4503 MBB->addSuccessor(JoinMBB);
4504
4505 // JoinMBB:
4506 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
4507 // ...
4508 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004509 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004510 .addReg(TrueReg).addMBB(StartMBB)
4511 .addReg(FalseReg).addMBB(FalseMBB);
4512
4513 MI->eraseFromParent();
4514 return JoinMBB;
4515}
4516
Richard Sandifordb86a8342013-06-27 09:27:40 +00004517// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
4518// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004519// happen when the condition is false rather than true. If a STORE ON
4520// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Richard Sandifordb86a8342013-06-27 09:27:40 +00004521MachineBasicBlock *
4522SystemZTargetLowering::emitCondStore(MachineInstr *MI,
4523 MachineBasicBlock *MBB,
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004524 unsigned StoreOpcode, unsigned STOCOpcode,
4525 bool Invert) const {
Eric Christophera6734172015-01-31 00:06:45 +00004526 const SystemZInstrInfo *TII =
4527 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordb86a8342013-06-27 09:27:40 +00004528
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004529 unsigned SrcReg = MI->getOperand(0).getReg();
4530 MachineOperand Base = MI->getOperand(1);
4531 int64_t Disp = MI->getOperand(2).getImm();
4532 unsigned IndexReg = MI->getOperand(3).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00004533 unsigned CCValid = MI->getOperand(4).getImm();
4534 unsigned CCMask = MI->getOperand(5).getImm();
Richard Sandifordb86a8342013-06-27 09:27:40 +00004535 DebugLoc DL = MI->getDebugLoc();
4536
4537 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
4538
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004539 // Use STOCOpcode if possible. We could use different store patterns in
4540 // order to avoid matching the index register, but the performance trade-offs
4541 // might be more complicated in that case.
Eric Christopher93bf97c2014-06-27 07:38:01 +00004542 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004543 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00004544 CCMask ^= CCValid;
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004545 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +00004546 .addReg(SrcReg).addOperand(Base).addImm(Disp)
4547 .addImm(CCValid).addImm(CCMask);
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004548 MI->eraseFromParent();
4549 return MBB;
4550 }
4551
Richard Sandifordb86a8342013-06-27 09:27:40 +00004552 // Get the condition needed to branch around the store.
4553 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00004554 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00004555
4556 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004557 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00004558 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
4559
4560 // StartMBB:
4561 // BRC CCMask, JoinMBB
4562 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00004563 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00004564 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4565 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00004566 MBB->addSuccessor(JoinMBB);
4567 MBB->addSuccessor(FalseMBB);
4568
4569 // FalseMBB:
4570 // store %SrcReg, %Disp(%Index,%Base)
4571 // # fallthrough to JoinMBB
4572 MBB = FalseMBB;
4573 BuildMI(MBB, DL, TII->get(StoreOpcode))
4574 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
4575 MBB->addSuccessor(JoinMBB);
4576
4577 MI->eraseFromParent();
4578 return JoinMBB;
4579}
4580
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004581// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
4582// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
4583// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
4584// BitSize is the width of the field in bits, or 0 if this is a partword
4585// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
4586// is one of the operands. Invert says whether the field should be
4587// inverted after performing BinOpcode (e.g. for NAND).
4588MachineBasicBlock *
4589SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI,
4590 MachineBasicBlock *MBB,
4591 unsigned BinOpcode,
4592 unsigned BitSize,
4593 bool Invert) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004594 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00004595 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00004596 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004597 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004598 bool IsSubWord = (BitSize < 32);
4599
4600 // Extract the operands. Base can be a register or a frame index.
4601 // Src2 can be a register or immediate.
4602 unsigned Dest = MI->getOperand(0).getReg();
4603 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
4604 int64_t Disp = MI->getOperand(2).getImm();
4605 MachineOperand Src2 = earlyUseOperand(MI->getOperand(3));
4606 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
4607 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
4608 DebugLoc DL = MI->getDebugLoc();
4609 if (IsSubWord)
4610 BitSize = MI->getOperand(6).getImm();
4611
4612 // Subword operations use 32-bit registers.
4613 const TargetRegisterClass *RC = (BitSize <= 32 ?
4614 &SystemZ::GR32BitRegClass :
4615 &SystemZ::GR64BitRegClass);
4616 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
4617 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
4618
4619 // Get the right opcodes for the displacement.
4620 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
4621 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
4622 assert(LOpcode && CSOpcode && "Displacement out of range");
4623
4624 // Create virtual registers for temporary results.
4625 unsigned OrigVal = MRI.createVirtualRegister(RC);
4626 unsigned OldVal = MRI.createVirtualRegister(RC);
4627 unsigned NewVal = (BinOpcode || IsSubWord ?
4628 MRI.createVirtualRegister(RC) : Src2.getReg());
4629 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
4630 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
4631
4632 // Insert a basic block for the main loop.
4633 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004634 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004635 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
4636
4637 // StartMBB:
4638 // ...
4639 // %OrigVal = L Disp(%Base)
4640 // # fall through to LoopMMB
4641 MBB = StartMBB;
4642 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
4643 .addOperand(Base).addImm(Disp).addReg(0);
4644 MBB->addSuccessor(LoopMBB);
4645
4646 // LoopMBB:
4647 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
4648 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
4649 // %RotatedNewVal = OP %RotatedOldVal, %Src2
4650 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
4651 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
4652 // JNE LoopMBB
4653 // # fall through to DoneMMB
4654 MBB = LoopMBB;
4655 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
4656 .addReg(OrigVal).addMBB(StartMBB)
4657 .addReg(Dest).addMBB(LoopMBB);
4658 if (IsSubWord)
4659 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
4660 .addReg(OldVal).addReg(BitShift).addImm(0);
4661 if (Invert) {
4662 // Perform the operation normally and then invert every bit of the field.
4663 unsigned Tmp = MRI.createVirtualRegister(RC);
4664 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
4665 .addReg(RotatedOldVal).addOperand(Src2);
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00004666 if (BitSize <= 32)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004667 // XILF with the upper BitSize bits set.
Richard Sandiford652784e2013-09-25 11:11:53 +00004668 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00004669 .addReg(Tmp).addImm(-1U << (32 - BitSize));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004670 else {
4671 // Use LCGR and add -1 to the result, which is more compact than
4672 // an XILF, XILH pair.
4673 unsigned Tmp2 = MRI.createVirtualRegister(RC);
4674 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
4675 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
4676 .addReg(Tmp2).addImm(-1);
4677 }
4678 } else if (BinOpcode)
4679 // A simply binary operation.
4680 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
4681 .addReg(RotatedOldVal).addOperand(Src2);
4682 else if (IsSubWord)
4683 // Use RISBG to rotate Src2 into position and use it to replace the
4684 // field in RotatedOldVal.
4685 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
4686 .addReg(RotatedOldVal).addReg(Src2.getReg())
4687 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
4688 if (IsSubWord)
4689 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
4690 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
4691 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
4692 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00004693 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4694 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004695 MBB->addSuccessor(LoopMBB);
4696 MBB->addSuccessor(DoneMBB);
4697
4698 MI->eraseFromParent();
4699 return DoneMBB;
4700}
4701
4702// Implement EmitInstrWithCustomInserter for pseudo
4703// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
4704// instruction that should be used to compare the current field with the
4705// minimum or maximum value. KeepOldMask is the BRC condition-code mask
4706// for when the current field should be kept. BitSize is the width of
4707// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
4708MachineBasicBlock *
4709SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI,
4710 MachineBasicBlock *MBB,
4711 unsigned CompareOpcode,
4712 unsigned KeepOldMask,
4713 unsigned BitSize) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004714 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00004715 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00004716 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004717 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004718 bool IsSubWord = (BitSize < 32);
4719
4720 // Extract the operands. Base can be a register or a frame index.
4721 unsigned Dest = MI->getOperand(0).getReg();
4722 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
4723 int64_t Disp = MI->getOperand(2).getImm();
4724 unsigned Src2 = MI->getOperand(3).getReg();
4725 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
4726 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
4727 DebugLoc DL = MI->getDebugLoc();
4728 if (IsSubWord)
4729 BitSize = MI->getOperand(6).getImm();
4730
4731 // Subword operations use 32-bit registers.
4732 const TargetRegisterClass *RC = (BitSize <= 32 ?
4733 &SystemZ::GR32BitRegClass :
4734 &SystemZ::GR64BitRegClass);
4735 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
4736 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
4737
4738 // Get the right opcodes for the displacement.
4739 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
4740 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
4741 assert(LOpcode && CSOpcode && "Displacement out of range");
4742
4743 // Create virtual registers for temporary results.
4744 unsigned OrigVal = MRI.createVirtualRegister(RC);
4745 unsigned OldVal = MRI.createVirtualRegister(RC);
4746 unsigned NewVal = MRI.createVirtualRegister(RC);
4747 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
4748 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
4749 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
4750
4751 // Insert 3 basic blocks for the loop.
4752 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004753 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004754 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
4755 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
4756 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
4757
4758 // StartMBB:
4759 // ...
4760 // %OrigVal = L Disp(%Base)
4761 // # fall through to LoopMMB
4762 MBB = StartMBB;
4763 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
4764 .addOperand(Base).addImm(Disp).addReg(0);
4765 MBB->addSuccessor(LoopMBB);
4766
4767 // LoopMBB:
4768 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
4769 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
4770 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00004771 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004772 MBB = LoopMBB;
4773 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
4774 .addReg(OrigVal).addMBB(StartMBB)
4775 .addReg(Dest).addMBB(UpdateMBB);
4776 if (IsSubWord)
4777 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
4778 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00004779 BuildMI(MBB, DL, TII->get(CompareOpcode))
4780 .addReg(RotatedOldVal).addReg(Src2);
4781 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00004782 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004783 MBB->addSuccessor(UpdateMBB);
4784 MBB->addSuccessor(UseAltMBB);
4785
4786 // UseAltMBB:
4787 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
4788 // # fall through to UpdateMMB
4789 MBB = UseAltMBB;
4790 if (IsSubWord)
4791 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
4792 .addReg(RotatedOldVal).addReg(Src2)
4793 .addImm(32).addImm(31 + BitSize).addImm(0);
4794 MBB->addSuccessor(UpdateMBB);
4795
4796 // UpdateMBB:
4797 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
4798 // [ %RotatedAltVal, UseAltMBB ]
4799 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
4800 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
4801 // JNE LoopMBB
4802 // # fall through to DoneMMB
4803 MBB = UpdateMBB;
4804 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
4805 .addReg(RotatedOldVal).addMBB(LoopMBB)
4806 .addReg(RotatedAltVal).addMBB(UseAltMBB);
4807 if (IsSubWord)
4808 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
4809 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
4810 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
4811 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00004812 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4813 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004814 MBB->addSuccessor(LoopMBB);
4815 MBB->addSuccessor(DoneMBB);
4816
4817 MI->eraseFromParent();
4818 return DoneMBB;
4819}
4820
4821// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
4822// instruction MI.
4823MachineBasicBlock *
4824SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI,
4825 MachineBasicBlock *MBB) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004826 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00004827 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00004828 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004829 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004830
4831 // Extract the operands. Base can be a register or a frame index.
4832 unsigned Dest = MI->getOperand(0).getReg();
4833 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
4834 int64_t Disp = MI->getOperand(2).getImm();
4835 unsigned OrigCmpVal = MI->getOperand(3).getReg();
4836 unsigned OrigSwapVal = MI->getOperand(4).getReg();
4837 unsigned BitShift = MI->getOperand(5).getReg();
4838 unsigned NegBitShift = MI->getOperand(6).getReg();
4839 int64_t BitSize = MI->getOperand(7).getImm();
4840 DebugLoc DL = MI->getDebugLoc();
4841
4842 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
4843
4844 // Get the right opcodes for the displacement.
4845 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
4846 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
4847 assert(LOpcode && CSOpcode && "Displacement out of range");
4848
4849 // Create virtual registers for temporary results.
4850 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
4851 unsigned OldVal = MRI.createVirtualRegister(RC);
4852 unsigned CmpVal = MRI.createVirtualRegister(RC);
4853 unsigned SwapVal = MRI.createVirtualRegister(RC);
4854 unsigned StoreVal = MRI.createVirtualRegister(RC);
4855 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
4856 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
4857 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
4858
4859 // Insert 2 basic blocks for the loop.
4860 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004861 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004862 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
4863 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
4864
4865 // StartMBB:
4866 // ...
4867 // %OrigOldVal = L Disp(%Base)
4868 // # fall through to LoopMMB
4869 MBB = StartMBB;
4870 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
4871 .addOperand(Base).addImm(Disp).addReg(0);
4872 MBB->addSuccessor(LoopMBB);
4873
4874 // LoopMBB:
4875 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
4876 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
4877 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
4878 // %Dest = RLL %OldVal, BitSize(%BitShift)
4879 // ^^ The low BitSize bits contain the field
4880 // of interest.
4881 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
4882 // ^^ Replace the upper 32-BitSize bits of the
4883 // comparison value with those that we loaded,
4884 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00004885 // CR %Dest, %RetryCmpVal
4886 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004887 // # Fall through to SetMBB
4888 MBB = LoopMBB;
4889 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
4890 .addReg(OrigOldVal).addMBB(StartMBB)
4891 .addReg(RetryOldVal).addMBB(SetMBB);
4892 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
4893 .addReg(OrigCmpVal).addMBB(StartMBB)
4894 .addReg(RetryCmpVal).addMBB(SetMBB);
4895 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
4896 .addReg(OrigSwapVal).addMBB(StartMBB)
4897 .addReg(RetrySwapVal).addMBB(SetMBB);
4898 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
4899 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
4900 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
4901 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00004902 BuildMI(MBB, DL, TII->get(SystemZ::CR))
4903 .addReg(Dest).addReg(RetryCmpVal);
4904 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00004905 .addImm(SystemZ::CCMASK_ICMP)
4906 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004907 MBB->addSuccessor(DoneMBB);
4908 MBB->addSuccessor(SetMBB);
4909
4910 // SetMBB:
4911 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
4912 // ^^ Replace the upper 32-BitSize bits of the new
4913 // value with those that we loaded.
4914 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
4915 // ^^ Rotate the new field to its proper position.
4916 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
4917 // JNE LoopMBB
4918 // # fall through to ExitMMB
4919 MBB = SetMBB;
4920 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
4921 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
4922 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
4923 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
4924 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
4925 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00004926 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4927 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004928 MBB->addSuccessor(LoopMBB);
4929 MBB->addSuccessor(DoneMBB);
4930
4931 MI->eraseFromParent();
4932 return DoneMBB;
4933}
4934
4935// Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true
4936// if the high register of the GR128 value must be cleared or false if
Richard Sandiford87a44362013-09-30 10:28:35 +00004937// it's "don't care". SubReg is subreg_l32 when extending a GR32
4938// and subreg_l64 when extending a GR64.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004939MachineBasicBlock *
4940SystemZTargetLowering::emitExt128(MachineInstr *MI,
4941 MachineBasicBlock *MBB,
4942 bool ClearEven, unsigned SubReg) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004943 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00004944 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00004945 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004946 MachineRegisterInfo &MRI = MF.getRegInfo();
4947 DebugLoc DL = MI->getDebugLoc();
4948
4949 unsigned Dest = MI->getOperand(0).getReg();
4950 unsigned Src = MI->getOperand(1).getReg();
4951 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
4952
4953 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
4954 if (ClearEven) {
4955 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
4956 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
4957
4958 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
4959 .addImm(0);
4960 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
Richard Sandiford87a44362013-09-30 10:28:35 +00004961 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004962 In128 = NewIn128;
4963 }
4964 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
4965 .addReg(In128).addReg(Src).addImm(SubReg);
4966
4967 MI->eraseFromParent();
4968 return MBB;
4969}
4970
Richard Sandifordd131ff82013-07-08 09:35:23 +00004971MachineBasicBlock *
Richard Sandiford564681c2013-08-12 10:28:10 +00004972SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI,
4973 MachineBasicBlock *MBB,
4974 unsigned Opcode) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +00004975 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00004976 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00004977 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandiford5e318f02013-08-27 09:54:29 +00004978 MachineRegisterInfo &MRI = MF.getRegInfo();
Richard Sandifordd131ff82013-07-08 09:35:23 +00004979 DebugLoc DL = MI->getDebugLoc();
4980
Richard Sandiford5e318f02013-08-27 09:54:29 +00004981 MachineOperand DestBase = earlyUseOperand(MI->getOperand(0));
Richard Sandifordd131ff82013-07-08 09:35:23 +00004982 uint64_t DestDisp = MI->getOperand(1).getImm();
Richard Sandiford5e318f02013-08-27 09:54:29 +00004983 MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2));
Richard Sandifordd131ff82013-07-08 09:35:23 +00004984 uint64_t SrcDisp = MI->getOperand(3).getImm();
4985 uint64_t Length = MI->getOperand(4).getImm();
4986
Richard Sandifordbe133a82013-08-28 09:01:51 +00004987 // When generating more than one CLC, all but the last will need to
4988 // branch to the end when a difference is found.
4989 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
Craig Topper062a2ba2014-04-25 05:30:21 +00004990 splitBlockAfter(MI, MBB) : nullptr);
Richard Sandifordbe133a82013-08-28 09:01:51 +00004991
Richard Sandiford5e318f02013-08-27 09:54:29 +00004992 // Check for the loop form, in which operand 5 is the trip count.
4993 if (MI->getNumExplicitOperands() > 5) {
4994 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
4995
4996 uint64_t StartCountReg = MI->getOperand(5).getReg();
4997 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
4998 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
4999 forceReg(MI, DestBase, TII));
5000
5001 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
5002 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
5003 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
5004 MRI.createVirtualRegister(RC));
5005 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
5006 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
5007 MRI.createVirtualRegister(RC));
5008
5009 RC = &SystemZ::GR64BitRegClass;
5010 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
5011 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
5012
5013 MachineBasicBlock *StartMBB = MBB;
5014 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
5015 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00005016 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005017
5018 // StartMBB:
5019 // # fall through to LoopMMB
5020 MBB->addSuccessor(LoopMBB);
5021
5022 // LoopMBB:
5023 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005024 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00005025 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005026 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00005027 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005028 // [ %NextCountReg, NextMBB ]
5029 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00005030 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00005031 // ( JLH EndMBB )
5032 //
5033 // The prefetch is used only for MVC. The JLH is used only for CLC.
5034 MBB = LoopMBB;
5035
5036 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
5037 .addReg(StartDestReg).addMBB(StartMBB)
5038 .addReg(NextDestReg).addMBB(NextMBB);
5039 if (!HaveSingleBase)
5040 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
5041 .addReg(StartSrcReg).addMBB(StartMBB)
5042 .addReg(NextSrcReg).addMBB(NextMBB);
5043 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
5044 .addReg(StartCountReg).addMBB(StartMBB)
5045 .addReg(NextCountReg).addMBB(NextMBB);
5046 if (Opcode == SystemZ::MVC)
5047 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
5048 .addImm(SystemZ::PFD_WRITE)
5049 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
5050 BuildMI(MBB, DL, TII->get(Opcode))
5051 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
5052 .addReg(ThisSrcReg).addImm(SrcDisp);
5053 if (EndMBB) {
5054 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5055 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5056 .addMBB(EndMBB);
5057 MBB->addSuccessor(EndMBB);
5058 MBB->addSuccessor(NextMBB);
5059 }
5060
5061 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00005062 // %NextDestReg = LA 256(%ThisDestReg)
5063 // %NextSrcReg = LA 256(%ThisSrcReg)
5064 // %NextCountReg = AGHI %ThisCountReg, -1
5065 // CGHI %NextCountReg, 0
5066 // JLH LoopMBB
5067 // # fall through to DoneMMB
5068 //
5069 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00005070 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005071
Richard Sandiford5e318f02013-08-27 09:54:29 +00005072 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
5073 .addReg(ThisDestReg).addImm(256).addReg(0);
5074 if (!HaveSingleBase)
5075 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
5076 .addReg(ThisSrcReg).addImm(256).addReg(0);
5077 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
5078 .addReg(ThisCountReg).addImm(-1);
5079 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
5080 .addReg(NextCountReg).addImm(0);
5081 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5082 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5083 .addMBB(LoopMBB);
5084 MBB->addSuccessor(LoopMBB);
5085 MBB->addSuccessor(DoneMBB);
5086
5087 DestBase = MachineOperand::CreateReg(NextDestReg, false);
5088 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
5089 Length &= 255;
5090 MBB = DoneMBB;
5091 }
5092 // Handle any remaining bytes with straight-line code.
5093 while (Length > 0) {
5094 uint64_t ThisLength = std::min(Length, uint64_t(256));
5095 // The previous iteration might have created out-of-range displacements.
5096 // Apply them using LAY if so.
5097 if (!isUInt<12>(DestDisp)) {
5098 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
5099 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
5100 .addOperand(DestBase).addImm(DestDisp).addReg(0);
5101 DestBase = MachineOperand::CreateReg(Reg, false);
5102 DestDisp = 0;
5103 }
5104 if (!isUInt<12>(SrcDisp)) {
5105 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
5106 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
5107 .addOperand(SrcBase).addImm(SrcDisp).addReg(0);
5108 SrcBase = MachineOperand::CreateReg(Reg, false);
5109 SrcDisp = 0;
5110 }
5111 BuildMI(*MBB, MI, DL, TII->get(Opcode))
5112 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength)
5113 .addOperand(SrcBase).addImm(SrcDisp);
5114 DestDisp += ThisLength;
5115 SrcDisp += ThisLength;
5116 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00005117 // If there's another CLC to go, branch to the end if a difference
5118 // was found.
5119 if (EndMBB && Length > 0) {
5120 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
5121 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5122 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5123 .addMBB(EndMBB);
5124 MBB->addSuccessor(EndMBB);
5125 MBB->addSuccessor(NextMBB);
5126 MBB = NextMBB;
5127 }
5128 }
5129 if (EndMBB) {
5130 MBB->addSuccessor(EndMBB);
5131 MBB = EndMBB;
5132 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005133 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00005134
5135 MI->eraseFromParent();
5136 return MBB;
5137}
5138
Richard Sandifordca232712013-08-16 11:21:54 +00005139// Decompose string pseudo-instruction MI into a loop that continually performs
5140// Opcode until CC != 3.
5141MachineBasicBlock *
5142SystemZTargetLowering::emitStringWrapper(MachineInstr *MI,
5143 MachineBasicBlock *MBB,
5144 unsigned Opcode) const {
Richard Sandifordca232712013-08-16 11:21:54 +00005145 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005146 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005147 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordca232712013-08-16 11:21:54 +00005148 MachineRegisterInfo &MRI = MF.getRegInfo();
5149 DebugLoc DL = MI->getDebugLoc();
5150
5151 uint64_t End1Reg = MI->getOperand(0).getReg();
5152 uint64_t Start1Reg = MI->getOperand(1).getReg();
5153 uint64_t Start2Reg = MI->getOperand(2).getReg();
5154 uint64_t CharReg = MI->getOperand(3).getReg();
5155
5156 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
5157 uint64_t This1Reg = MRI.createVirtualRegister(RC);
5158 uint64_t This2Reg = MRI.createVirtualRegister(RC);
5159 uint64_t End2Reg = MRI.createVirtualRegister(RC);
5160
5161 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005162 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00005163 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5164
5165 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00005166 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00005167 MBB->addSuccessor(LoopMBB);
5168
5169 // LoopMBB:
5170 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
5171 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford7789b082013-09-30 08:48:38 +00005172 // R0L = %CharReg
5173 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
Richard Sandifordca232712013-08-16 11:21:54 +00005174 // JO LoopMBB
5175 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00005176 //
Richard Sandiford7789b082013-09-30 08:48:38 +00005177 // The load of R0L can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00005178 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00005179
5180 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
5181 .addReg(Start1Reg).addMBB(StartMBB)
5182 .addReg(End1Reg).addMBB(LoopMBB);
5183 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
5184 .addReg(Start2Reg).addMBB(StartMBB)
5185 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford7789b082013-09-30 08:48:38 +00005186 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00005187 BuildMI(MBB, DL, TII->get(Opcode))
5188 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
5189 .addReg(This1Reg).addReg(This2Reg);
5190 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5191 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
5192 MBB->addSuccessor(LoopMBB);
5193 MBB->addSuccessor(DoneMBB);
5194
5195 DoneMBB->addLiveIn(SystemZ::CC);
5196
5197 MI->eraseFromParent();
5198 return DoneMBB;
5199}
5200
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005201// Update TBEGIN instruction with final opcode and register clobbers.
5202MachineBasicBlock *
5203SystemZTargetLowering::emitTransactionBegin(MachineInstr *MI,
5204 MachineBasicBlock *MBB,
5205 unsigned Opcode,
5206 bool NoFloat) const {
5207 MachineFunction &MF = *MBB->getParent();
5208 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
5209 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
5210
5211 // Update opcode.
5212 MI->setDesc(TII->get(Opcode));
5213
5214 // We cannot handle a TBEGIN that clobbers the stack or frame pointer.
5215 // Make sure to add the corresponding GRSM bits if they are missing.
5216 uint64_t Control = MI->getOperand(2).getImm();
5217 static const unsigned GPRControlBit[16] = {
5218 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000,
5219 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100
5220 };
5221 Control |= GPRControlBit[15];
5222 if (TFI->hasFP(MF))
5223 Control |= GPRControlBit[11];
5224 MI->getOperand(2).setImm(Control);
5225
5226 // Add GPR clobbers.
5227 for (int I = 0; I < 16; I++) {
5228 if ((Control & GPRControlBit[I]) == 0) {
5229 unsigned Reg = SystemZMC::GR64Regs[I];
5230 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5231 }
5232 }
5233
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005234 // Add FPR/VR clobbers.
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005235 if (!NoFloat && (Control & 4) != 0) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005236 if (Subtarget.hasVector()) {
5237 for (int I = 0; I < 32; I++) {
5238 unsigned Reg = SystemZMC::VR128Regs[I];
5239 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5240 }
5241 } else {
5242 for (int I = 0; I < 16; I++) {
5243 unsigned Reg = SystemZMC::FP64Regs[I];
5244 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5245 }
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005246 }
5247 }
5248
5249 return MBB;
5250}
5251
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005252MachineBasicBlock *SystemZTargetLowering::
5253EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const {
5254 switch (MI->getOpcode()) {
Richard Sandiford7c5c0ea2013-10-01 13:10:16 +00005255 case SystemZ::Select32Mux:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005256 case SystemZ::Select32:
5257 case SystemZ::SelectF32:
5258 case SystemZ::Select64:
5259 case SystemZ::SelectF64:
5260 case SystemZ::SelectF128:
5261 return emitSelect(MI, MBB);
5262
Richard Sandiford2896d042013-10-01 14:33:55 +00005263 case SystemZ::CondStore8Mux:
5264 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
5265 case SystemZ::CondStore8MuxInv:
5266 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
5267 case SystemZ::CondStore16Mux:
5268 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
5269 case SystemZ::CondStore16MuxInv:
5270 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005271 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005272 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005273 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005274 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005275 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005276 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005277 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005278 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005279 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005280 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005281 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005282 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005283 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005284 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005285 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005286 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005287 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005288 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005289 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005290 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005291 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005292 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005293 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005294 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005295
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005296 case SystemZ::AEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00005297 return emitExt128(MI, MBB, false, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005298 case SystemZ::ZEXT128_32:
Richard Sandiford87a44362013-09-30 10:28:35 +00005299 return emitExt128(MI, MBB, true, SystemZ::subreg_l32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005300 case SystemZ::ZEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00005301 return emitExt128(MI, MBB, true, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005302
5303 case SystemZ::ATOMIC_SWAPW:
5304 return emitAtomicLoadBinary(MI, MBB, 0, 0);
5305 case SystemZ::ATOMIC_SWAP_32:
5306 return emitAtomicLoadBinary(MI, MBB, 0, 32);
5307 case SystemZ::ATOMIC_SWAP_64:
5308 return emitAtomicLoadBinary(MI, MBB, 0, 64);
5309
5310 case SystemZ::ATOMIC_LOADW_AR:
5311 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
5312 case SystemZ::ATOMIC_LOADW_AFI:
5313 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
5314 case SystemZ::ATOMIC_LOAD_AR:
5315 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
5316 case SystemZ::ATOMIC_LOAD_AHI:
5317 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
5318 case SystemZ::ATOMIC_LOAD_AFI:
5319 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
5320 case SystemZ::ATOMIC_LOAD_AGR:
5321 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
5322 case SystemZ::ATOMIC_LOAD_AGHI:
5323 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
5324 case SystemZ::ATOMIC_LOAD_AGFI:
5325 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
5326
5327 case SystemZ::ATOMIC_LOADW_SR:
5328 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
5329 case SystemZ::ATOMIC_LOAD_SR:
5330 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
5331 case SystemZ::ATOMIC_LOAD_SGR:
5332 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
5333
5334 case SystemZ::ATOMIC_LOADW_NR:
5335 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
5336 case SystemZ::ATOMIC_LOADW_NILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00005337 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005338 case SystemZ::ATOMIC_LOAD_NR:
5339 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005340 case SystemZ::ATOMIC_LOAD_NILL:
5341 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
5342 case SystemZ::ATOMIC_LOAD_NILH:
5343 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
5344 case SystemZ::ATOMIC_LOAD_NILF:
5345 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005346 case SystemZ::ATOMIC_LOAD_NGR:
5347 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005348 case SystemZ::ATOMIC_LOAD_NILL64:
5349 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
5350 case SystemZ::ATOMIC_LOAD_NILH64:
5351 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00005352 case SystemZ::ATOMIC_LOAD_NIHL64:
5353 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
5354 case SystemZ::ATOMIC_LOAD_NIHH64:
5355 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005356 case SystemZ::ATOMIC_LOAD_NILF64:
5357 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00005358 case SystemZ::ATOMIC_LOAD_NIHF64:
5359 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005360
5361 case SystemZ::ATOMIC_LOADW_OR:
5362 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
5363 case SystemZ::ATOMIC_LOADW_OILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00005364 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005365 case SystemZ::ATOMIC_LOAD_OR:
5366 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005367 case SystemZ::ATOMIC_LOAD_OILL:
5368 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
5369 case SystemZ::ATOMIC_LOAD_OILH:
5370 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
5371 case SystemZ::ATOMIC_LOAD_OILF:
5372 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005373 case SystemZ::ATOMIC_LOAD_OGR:
5374 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005375 case SystemZ::ATOMIC_LOAD_OILL64:
5376 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
5377 case SystemZ::ATOMIC_LOAD_OILH64:
5378 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00005379 case SystemZ::ATOMIC_LOAD_OIHL64:
5380 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
5381 case SystemZ::ATOMIC_LOAD_OIHH64:
5382 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005383 case SystemZ::ATOMIC_LOAD_OILF64:
5384 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00005385 case SystemZ::ATOMIC_LOAD_OIHF64:
5386 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005387
5388 case SystemZ::ATOMIC_LOADW_XR:
5389 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
5390 case SystemZ::ATOMIC_LOADW_XILF:
Richard Sandiford652784e2013-09-25 11:11:53 +00005391 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005392 case SystemZ::ATOMIC_LOAD_XR:
5393 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005394 case SystemZ::ATOMIC_LOAD_XILF:
5395 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005396 case SystemZ::ATOMIC_LOAD_XGR:
5397 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005398 case SystemZ::ATOMIC_LOAD_XILF64:
5399 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
Richard Sandiford5718dac2013-10-01 14:08:44 +00005400 case SystemZ::ATOMIC_LOAD_XIHF64:
5401 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005402
5403 case SystemZ::ATOMIC_LOADW_NRi:
5404 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
5405 case SystemZ::ATOMIC_LOADW_NILHi:
Richard Sandiford652784e2013-09-25 11:11:53 +00005406 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005407 case SystemZ::ATOMIC_LOAD_NRi:
5408 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005409 case SystemZ::ATOMIC_LOAD_NILLi:
5410 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
5411 case SystemZ::ATOMIC_LOAD_NILHi:
5412 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
5413 case SystemZ::ATOMIC_LOAD_NILFi:
5414 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005415 case SystemZ::ATOMIC_LOAD_NGRi:
5416 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005417 case SystemZ::ATOMIC_LOAD_NILL64i:
5418 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
5419 case SystemZ::ATOMIC_LOAD_NILH64i:
5420 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00005421 case SystemZ::ATOMIC_LOAD_NIHL64i:
5422 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
5423 case SystemZ::ATOMIC_LOAD_NIHH64i:
5424 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005425 case SystemZ::ATOMIC_LOAD_NILF64i:
5426 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00005427 case SystemZ::ATOMIC_LOAD_NIHF64i:
5428 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005429
5430 case SystemZ::ATOMIC_LOADW_MIN:
5431 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5432 SystemZ::CCMASK_CMP_LE, 0);
5433 case SystemZ::ATOMIC_LOAD_MIN_32:
5434 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5435 SystemZ::CCMASK_CMP_LE, 32);
5436 case SystemZ::ATOMIC_LOAD_MIN_64:
5437 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
5438 SystemZ::CCMASK_CMP_LE, 64);
5439
5440 case SystemZ::ATOMIC_LOADW_MAX:
5441 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5442 SystemZ::CCMASK_CMP_GE, 0);
5443 case SystemZ::ATOMIC_LOAD_MAX_32:
5444 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5445 SystemZ::CCMASK_CMP_GE, 32);
5446 case SystemZ::ATOMIC_LOAD_MAX_64:
5447 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
5448 SystemZ::CCMASK_CMP_GE, 64);
5449
5450 case SystemZ::ATOMIC_LOADW_UMIN:
5451 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5452 SystemZ::CCMASK_CMP_LE, 0);
5453 case SystemZ::ATOMIC_LOAD_UMIN_32:
5454 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5455 SystemZ::CCMASK_CMP_LE, 32);
5456 case SystemZ::ATOMIC_LOAD_UMIN_64:
5457 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
5458 SystemZ::CCMASK_CMP_LE, 64);
5459
5460 case SystemZ::ATOMIC_LOADW_UMAX:
5461 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5462 SystemZ::CCMASK_CMP_GE, 0);
5463 case SystemZ::ATOMIC_LOAD_UMAX_32:
5464 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5465 SystemZ::CCMASK_CMP_GE, 32);
5466 case SystemZ::ATOMIC_LOAD_UMAX_64:
5467 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
5468 SystemZ::CCMASK_CMP_GE, 64);
5469
5470 case SystemZ::ATOMIC_CMP_SWAPW:
5471 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005472 case SystemZ::MVCSequence:
5473 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00005474 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00005475 case SystemZ::NCSequence:
5476 case SystemZ::NCLoop:
5477 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
5478 case SystemZ::OCSequence:
5479 case SystemZ::OCLoop:
5480 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
5481 case SystemZ::XCSequence:
5482 case SystemZ::XCLoop:
5483 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005484 case SystemZ::CLCSequence:
5485 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00005486 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00005487 case SystemZ::CLSTLoop:
5488 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00005489 case SystemZ::MVSTLoop:
5490 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00005491 case SystemZ::SRSTLoop:
5492 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005493 case SystemZ::TBEGIN:
5494 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false);
5495 case SystemZ::TBEGIN_nofloat:
5496 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true);
5497 case SystemZ::TBEGINC:
5498 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005499 default:
5500 llvm_unreachable("Unexpected instr type to insert");
5501 }
5502}