blob: 5f547439c9aab351ffcce3b3a1d2ae82d999b341 [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 Weigandcd808232015-05-05 19:26:48 +0000104 addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000105 }
106
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000107 // Compute derived properties from the register classes
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000108 computeRegisterProperties(Subtarget.getRegisterInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000109
110 // Set up special registers.
111 setExceptionPointerRegister(SystemZ::R6D);
112 setExceptionSelectorRegister(SystemZ::R7D);
113 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
114
115 // TODO: It may be better to default to latency-oriented scheduling, however
116 // LLVM's current latency-oriented scheduler can't handle physreg definitions
Richard Sandiford14a44492013-05-22 13:38:45 +0000117 // such as SystemZ has with CC, so set this to the register-pressure
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000118 // scheduler, because it can.
119 setSchedulingPreference(Sched::RegPressure);
120
121 setBooleanContents(ZeroOrOneBooleanContent);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000122 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000123
124 // Instructions are strings of 2-byte aligned 2-byte values.
125 setMinFunctionAlignment(2);
126
127 // Handle operations that are handled in a similar way for all types.
128 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
129 I <= MVT::LAST_FP_VALUETYPE;
130 ++I) {
131 MVT VT = MVT::SimpleValueType(I);
132 if (isTypeLegal(VT)) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000133 // Lower SET_CC into an IPM-based sequence.
134 setOperationAction(ISD::SETCC, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000135
136 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
137 setOperationAction(ISD::SELECT, VT, Expand);
138
139 // Lower SELECT_CC and BR_CC into separate comparisons and branches.
140 setOperationAction(ISD::SELECT_CC, VT, Custom);
141 setOperationAction(ISD::BR_CC, VT, Custom);
142 }
143 }
144
145 // Expand jump table branches as address arithmetic followed by an
146 // indirect jump.
147 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
148
149 // Expand BRCOND into a BR_CC (see above).
150 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
151
152 // Handle integer types.
153 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
154 I <= MVT::LAST_INTEGER_VALUETYPE;
155 ++I) {
156 MVT VT = MVT::SimpleValueType(I);
157 if (isTypeLegal(VT)) {
158 // Expand individual DIV and REMs into DIVREMs.
159 setOperationAction(ISD::SDIV, VT, Expand);
160 setOperationAction(ISD::UDIV, VT, Expand);
161 setOperationAction(ISD::SREM, VT, Expand);
162 setOperationAction(ISD::UREM, VT, Expand);
163 setOperationAction(ISD::SDIVREM, VT, Custom);
164 setOperationAction(ISD::UDIVREM, VT, Custom);
165
Richard Sandifordbef3d7a2013-12-10 10:49:34 +0000166 // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and
167 // stores, putting a serialization instruction after the stores.
168 setOperationAction(ISD::ATOMIC_LOAD, VT, Custom);
169 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000170
Richard Sandiford41350a52013-12-24 15:18:04 +0000171 // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are
172 // available, or if the operand is constant.
173 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
174
Ulrich Weigandb4012182015-03-31 12:56:33 +0000175 // Use POPCNT on z196 and above.
176 if (Subtarget.hasPopulationCount())
177 setOperationAction(ISD::CTPOP, VT, Custom);
178 else
179 setOperationAction(ISD::CTPOP, VT, Expand);
180
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000181 // No special instructions for these.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000182 setOperationAction(ISD::CTTZ, VT, Expand);
183 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
184 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
185 setOperationAction(ISD::ROTR, VT, Expand);
186
Richard Sandiford7d86e472013-08-21 09:34:56 +0000187 // Use *MUL_LOHI where possible instead of MULH*.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000188 setOperationAction(ISD::MULHS, VT, Expand);
189 setOperationAction(ISD::MULHU, VT, Expand);
Richard Sandiford7d86e472013-08-21 09:34:56 +0000190 setOperationAction(ISD::SMUL_LOHI, VT, Custom);
191 setOperationAction(ISD::UMUL_LOHI, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000192
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000193 // Only z196 and above have native support for conversions to unsigned.
194 if (!Subtarget.hasFPExtension())
195 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000196 }
197 }
198
199 // Type legalization will convert 8- and 16-bit atomic operations into
200 // forms that operate on i32s (but still keeping the original memory VT).
201 // Lower them into full i32 operations.
202 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom);
203 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom);
204 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
205 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom);
206 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom);
207 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom);
208 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
209 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom);
210 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom);
211 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
212 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
213 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
214
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000215 // z10 has instructions for signed but not unsigned FP conversion.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000216 // Handle unsigned 32-bit types as signed 64-bit types.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000217 if (!Subtarget.hasFPExtension()) {
218 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
219 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
220 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000221
222 // We have native support for a 64-bit CTLZ, via FLOGR.
223 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
224 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
225
226 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
227 setOperationAction(ISD::OR, MVT::i64, Custom);
228
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000229 // FIXME: Can we support these natively?
230 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
231 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
232 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
233
234 // We have native instructions for i8, i16 and i32 extensions, but not i1.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000235 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000236 for (MVT VT : MVT::integer_valuetypes()) {
237 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
238 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
239 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
240 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000241
242 // Handle the various types of symbolic address.
243 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
244 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
245 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
246 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
247 setOperationAction(ISD::JumpTable, PtrVT, Custom);
248
249 // We need to handle dynamic allocations specially because of the
250 // 160-byte area at the bottom of the stack.
251 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
252
253 // Use custom expanders so that we can force the function to use
254 // a frame pointer.
255 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
256 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
257
Richard Sandiford03481332013-08-23 11:36:42 +0000258 // Handle prefetches with PFD or PFDRL.
259 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
260
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000261 for (MVT VT : MVT::vector_valuetypes()) {
262 // Assume by default that all vector operations need to be expanded.
263 for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode)
264 if (getOperationAction(Opcode, VT) == Legal)
265 setOperationAction(Opcode, VT, Expand);
266
267 // Likewise all truncating stores and extending loads.
268 for (MVT InnerVT : MVT::vector_valuetypes()) {
269 setTruncStoreAction(VT, InnerVT, Expand);
270 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
271 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
272 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
273 }
274
275 if (isTypeLegal(VT)) {
276 // These operations are legal for anything that can be stored in a
277 // vector register, even if there is no native support for the format
278 // as such.
279 setOperationAction(ISD::LOAD, VT, Legal);
280 setOperationAction(ISD::STORE, VT, Legal);
281 setOperationAction(ISD::VSELECT, VT, Legal);
282 setOperationAction(ISD::BITCAST, VT, Legal);
283 setOperationAction(ISD::UNDEF, VT, Legal);
284
285 // Likewise, except that we need to replace the nodes with something
286 // more specific.
287 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
288 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
289 }
290 }
291
292 // Handle integer vector types.
293 for (MVT VT : MVT::integer_vector_valuetypes()) {
294 if (isTypeLegal(VT)) {
295 // These operations have direct equivalents.
296 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal);
297 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal);
298 setOperationAction(ISD::ADD, VT, Legal);
299 setOperationAction(ISD::SUB, VT, Legal);
300 if (VT != MVT::v2i64)
301 setOperationAction(ISD::MUL, VT, Legal);
302 setOperationAction(ISD::AND, VT, Legal);
303 setOperationAction(ISD::OR, VT, Legal);
304 setOperationAction(ISD::XOR, VT, Legal);
305 setOperationAction(ISD::CTPOP, VT, Custom);
306 setOperationAction(ISD::CTTZ, VT, Legal);
307 setOperationAction(ISD::CTLZ, VT, Legal);
308 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Custom);
309 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Custom);
310
311 // Convert a GPR scalar to a vector by inserting it into element 0.
312 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
313
314 // Detect shifts by a scalar amount and convert them into
315 // V*_BY_SCALAR.
316 setOperationAction(ISD::SHL, VT, Custom);
317 setOperationAction(ISD::SRA, VT, Custom);
318 setOperationAction(ISD::SRL, VT, Custom);
319
320 // At present ROTL isn't matched by DAGCombiner. ROTR should be
321 // converted into ROTL.
322 setOperationAction(ISD::ROTL, VT, Expand);
323 setOperationAction(ISD::ROTR, VT, Expand);
324
325 // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands
326 // and inverting the result as necessary.
327 setOperationAction(ISD::SETCC, VT, Custom);
328 }
329 }
330
Ulrich Weigandcd808232015-05-05 19:26:48 +0000331 if (Subtarget.hasVector()) {
332 // There should be no need to check for float types other than v2f64
333 // since <2 x f32> isn't a legal type.
334 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
335 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
336 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
337 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
338 }
339
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000340 // Handle floating-point types.
341 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
342 I <= MVT::LAST_FP_VALUETYPE;
343 ++I) {
344 MVT VT = MVT::SimpleValueType(I);
345 if (isTypeLegal(VT)) {
346 // We can use FI for FRINT.
347 setOperationAction(ISD::FRINT, VT, Legal);
348
Richard Sandifordaf5f66a2013-08-21 09:04:20 +0000349 // We can use the extended form of FI for other rounding operations.
350 if (Subtarget.hasFPExtension()) {
351 setOperationAction(ISD::FNEARBYINT, VT, Legal);
352 setOperationAction(ISD::FFLOOR, VT, Legal);
353 setOperationAction(ISD::FCEIL, VT, Legal);
354 setOperationAction(ISD::FTRUNC, VT, Legal);
355 setOperationAction(ISD::FROUND, VT, Legal);
356 }
357
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000358 // No special instructions for these.
359 setOperationAction(ISD::FSIN, VT, Expand);
360 setOperationAction(ISD::FCOS, VT, Expand);
361 setOperationAction(ISD::FREM, VT, Expand);
362 }
363 }
364
Ulrich Weigandcd808232015-05-05 19:26:48 +0000365 // Handle floating-point vector types.
366 if (Subtarget.hasVector()) {
367 // Scalar-to-vector conversion is just a subreg.
368 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
369
370 // Some insertions and extractions can be done directly but others
371 // need to go via integers.
372 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
373 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
374
375 // These operations have direct equivalents.
376 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
377 setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
378 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
379 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
380 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
381 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
382 setOperationAction(ISD::FABS, MVT::v2f64, Legal);
383 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
384 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
385 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
386 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
387 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
388 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
389 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
390 }
391
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000392 // We have fused multiply-addition for f32 and f64 but not f128.
393 setOperationAction(ISD::FMA, MVT::f32, Legal);
394 setOperationAction(ISD::FMA, MVT::f64, Legal);
395 setOperationAction(ISD::FMA, MVT::f128, Expand);
396
397 // Needed so that we don't try to implement f128 constant loads using
398 // a load-and-extend of a f80 constant (in cases where the constant
399 // would fit in an f80).
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000400 for (MVT VT : MVT::fp_valuetypes())
401 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000402
403 // Floating-point truncation and stores need to be done separately.
404 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
405 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
406 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
407
408 // We have 64-bit FPR<->GPR moves, but need special handling for
409 // 32-bit forms.
410 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
411 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
412
413 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
414 // structure, but VAEND is a no-op.
415 setOperationAction(ISD::VASTART, MVT::Other, Custom);
416 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
417 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Richard Sandifordd131ff82013-07-08 09:35:23 +0000418
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000419 // Codes for which we want to perform some z-specific combinations.
420 setTargetDAGCombine(ISD::SIGN_EXTEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000421 setTargetDAGCombine(ISD::STORE);
422 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000423
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000424 // Handle intrinsics.
425 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
426
Richard Sandifordd131ff82013-07-08 09:35:23 +0000427 // We want to use MVC in preference to even a single load/store pair.
428 MaxStoresPerMemcpy = 0;
429 MaxStoresPerMemcpyOptSize = 0;
Richard Sandiford47660c12013-07-09 09:32:42 +0000430
431 // The main memset sequence is a byte store followed by an MVC.
432 // Two STC or MV..I stores win over that, but the kind of fused stores
433 // generated by target-independent code don't when the byte value is
434 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
435 // than "STC;MVC". Handle the choice in target-specific code instead.
436 MaxStoresPerMemset = 0;
437 MaxStoresPerMemsetOptSize = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000438}
439
Richard Sandifordabc010b2013-11-06 12:16:02 +0000440EVT SystemZTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
441 if (!VT.isVector())
442 return MVT::i32;
443 return VT.changeVectorElementTypeToInteger();
444}
445
446bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
Stephen Lin73de7bf2013-07-09 18:16:56 +0000447 VT = VT.getScalarType();
448
449 if (!VT.isSimple())
450 return false;
451
452 switch (VT.getSimpleVT().SimpleTy) {
453 case MVT::f32:
454 case MVT::f64:
455 return true;
456 case MVT::f128:
457 return false;
458 default:
459 break;
460 }
461
462 return false;
463}
464
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000465bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
466 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
467 return Imm.isZero() || Imm.isNegZero();
468}
469
Ulrich Weigand1f6666a2015-03-31 12:52:27 +0000470bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
471 // We can use CGFI or CLGFI.
472 return isInt<32>(Imm) || isUInt<32>(Imm);
473}
474
475bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const {
476 // We can use ALGFI or SLGFI.
477 return isUInt<32>(Imm) || isUInt<32>(-Imm);
478}
479
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000480bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
481 unsigned,
482 unsigned,
483 bool *Fast) const {
Richard Sandiford46af5a22013-05-30 09:45:42 +0000484 // Unaligned accesses should never be slower than the expanded version.
485 // We check specifically for aligned accesses in the few cases where
486 // they are required.
487 if (Fast)
488 *Fast = true;
489 return true;
490}
491
Richard Sandiford791bea42013-07-31 12:58:26 +0000492bool SystemZTargetLowering::isLegalAddressingMode(const AddrMode &AM,
493 Type *Ty) const {
494 // Punt on globals for now, although they can be used in limited
495 // RELATIVE LONG cases.
496 if (AM.BaseGV)
497 return false;
498
499 // Require a 20-bit signed offset.
500 if (!isInt<20>(AM.BaseOffs))
501 return false;
502
503 // Indexing is OK but no scale factor can be applied.
504 return AM.Scale == 0 || AM.Scale == 1;
505}
506
Richard Sandiford709bda62013-08-19 12:42:31 +0000507bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
508 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
509 return false;
510 unsigned FromBits = FromType->getPrimitiveSizeInBits();
511 unsigned ToBits = ToType->getPrimitiveSizeInBits();
512 return FromBits > ToBits;
513}
514
515bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
516 if (!FromVT.isInteger() || !ToVT.isInteger())
517 return false;
518 unsigned FromBits = FromVT.getSizeInBits();
519 unsigned ToBits = ToVT.getSizeInBits();
520 return FromBits > ToBits;
521}
522
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000523//===----------------------------------------------------------------------===//
524// Inline asm support
525//===----------------------------------------------------------------------===//
526
527TargetLowering::ConstraintType
528SystemZTargetLowering::getConstraintType(const std::string &Constraint) const {
529 if (Constraint.size() == 1) {
530 switch (Constraint[0]) {
531 case 'a': // Address register
532 case 'd': // Data register (equivalent to 'r')
533 case 'f': // Floating-point register
Richard Sandiford0755c932013-10-01 11:26:28 +0000534 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000535 case 'r': // General-purpose register
536 return C_RegisterClass;
537
538 case 'Q': // Memory with base and unsigned 12-bit displacement
539 case 'R': // Likewise, plus an index
540 case 'S': // Memory with base and signed 20-bit displacement
541 case 'T': // Likewise, plus an index
542 case 'm': // Equivalent to 'T'.
543 return C_Memory;
544
545 case 'I': // Unsigned 8-bit constant
546 case 'J': // Unsigned 12-bit constant
547 case 'K': // Signed 16-bit constant
548 case 'L': // Signed 20-bit displacement (on all targets we support)
549 case 'M': // 0x7fffffff
550 return C_Other;
551
552 default:
553 break;
554 }
555 }
556 return TargetLowering::getConstraintType(Constraint);
557}
558
559TargetLowering::ConstraintWeight SystemZTargetLowering::
560getSingleConstraintMatchWeight(AsmOperandInfo &info,
561 const char *constraint) const {
562 ConstraintWeight weight = CW_Invalid;
563 Value *CallOperandVal = info.CallOperandVal;
564 // If we don't have a value, we can't do a match,
565 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +0000566 if (!CallOperandVal)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000567 return CW_Default;
568 Type *type = CallOperandVal->getType();
569 // Look at the constraint type.
570 switch (*constraint) {
571 default:
572 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
573 break;
574
575 case 'a': // Address register
576 case 'd': // Data register (equivalent to 'r')
Richard Sandiford0755c932013-10-01 11:26:28 +0000577 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000578 case 'r': // General-purpose register
579 if (CallOperandVal->getType()->isIntegerTy())
580 weight = CW_Register;
581 break;
582
583 case 'f': // Floating-point register
584 if (type->isFloatingPointTy())
585 weight = CW_Register;
586 break;
587
588 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000589 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000590 if (isUInt<8>(C->getZExtValue()))
591 weight = CW_Constant;
592 break;
593
594 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000595 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000596 if (isUInt<12>(C->getZExtValue()))
597 weight = CW_Constant;
598 break;
599
600 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000601 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000602 if (isInt<16>(C->getSExtValue()))
603 weight = CW_Constant;
604 break;
605
606 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000607 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000608 if (isInt<20>(C->getSExtValue()))
609 weight = CW_Constant;
610 break;
611
612 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000613 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000614 if (C->getZExtValue() == 0x7fffffff)
615 weight = CW_Constant;
616 break;
617 }
618 return weight;
619}
620
Richard Sandifordb8204052013-07-12 09:08:12 +0000621// Parse a "{tNNN}" register constraint for which the register type "t"
622// has already been verified. MC is the class associated with "t" and
623// Map maps 0-based register numbers to LLVM register numbers.
624static std::pair<unsigned, const TargetRegisterClass *>
625parseRegisterNumber(const std::string &Constraint,
626 const TargetRegisterClass *RC, const unsigned *Map) {
627 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
628 if (isdigit(Constraint[2])) {
629 std::string Suffix(Constraint.data() + 2, Constraint.size() - 2);
630 unsigned Index = atoi(Suffix.c_str());
631 if (Index < 16 && Map[Index])
632 return std::make_pair(Map[Index], RC);
633 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000634 return std::make_pair(0U, nullptr);
Richard Sandifordb8204052013-07-12 09:08:12 +0000635}
636
Eric Christopher11e4df72015-02-26 22:38:43 +0000637std::pair<unsigned, const TargetRegisterClass *>
638SystemZTargetLowering::getRegForInlineAsmConstraint(
639 const TargetRegisterInfo *TRI, const std::string &Constraint,
640 MVT VT) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000641 if (Constraint.size() == 1) {
642 // GCC Constraint Letters
643 switch (Constraint[0]) {
644 default: break;
645 case 'd': // Data register (equivalent to 'r')
646 case 'r': // General-purpose register
647 if (VT == MVT::i64)
648 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
649 else if (VT == MVT::i128)
650 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
651 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
652
653 case 'a': // Address register
654 if (VT == MVT::i64)
655 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
656 else if (VT == MVT::i128)
657 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
658 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
659
Richard Sandiford0755c932013-10-01 11:26:28 +0000660 case 'h': // High-part register (an LLVM extension)
661 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
662
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000663 case 'f': // Floating-point register
664 if (VT == MVT::f64)
665 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
666 else if (VT == MVT::f128)
667 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
668 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
669 }
670 }
Richard Sandifordb8204052013-07-12 09:08:12 +0000671 if (Constraint[0] == '{') {
672 // We need to override the default register parsing for GPRs and FPRs
673 // because the interpretation depends on VT. The internal names of
674 // the registers are also different from the external names
675 // (F0D and F0S instead of F0, etc.).
676 if (Constraint[1] == 'r') {
677 if (VT == MVT::i32)
678 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
679 SystemZMC::GR32Regs);
680 if (VT == MVT::i128)
681 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
682 SystemZMC::GR128Regs);
683 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
684 SystemZMC::GR64Regs);
685 }
686 if (Constraint[1] == 'f') {
687 if (VT == MVT::f32)
688 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
689 SystemZMC::FP32Regs);
690 if (VT == MVT::f128)
691 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
692 SystemZMC::FP128Regs);
693 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
694 SystemZMC::FP64Regs);
695 }
696 }
Eric Christopher11e4df72015-02-26 22:38:43 +0000697 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000698}
699
700void SystemZTargetLowering::
701LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
702 std::vector<SDValue> &Ops,
703 SelectionDAG &DAG) const {
704 // Only support length 1 constraints for now.
705 if (Constraint.length() == 1) {
706 switch (Constraint[0]) {
707 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000708 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000709 if (isUInt<8>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000710 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000711 Op.getValueType()));
712 return;
713
714 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000715 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000716 if (isUInt<12>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000717 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000718 Op.getValueType()));
719 return;
720
721 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000722 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000723 if (isInt<16>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000724 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000725 Op.getValueType()));
726 return;
727
728 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000729 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000730 if (isInt<20>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000731 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000732 Op.getValueType()));
733 return;
734
735 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000736 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000737 if (C->getZExtValue() == 0x7fffffff)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000738 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000739 Op.getValueType()));
740 return;
741 }
742 }
743 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
744}
745
746//===----------------------------------------------------------------------===//
747// Calling conventions
748//===----------------------------------------------------------------------===//
749
750#include "SystemZGenCallingConv.inc"
751
Richard Sandiford709bda62013-08-19 12:42:31 +0000752bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
753 Type *ToType) const {
754 return isTruncateFree(FromType, ToType);
755}
756
757bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
758 if (!CI->isTailCall())
759 return false;
760 return true;
761}
762
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000763// Value is a value that has been passed to us in the location described by VA
764// (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
765// any loads onto Chain.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000766static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000767 CCValAssign &VA, SDValue Chain,
768 SDValue Value) {
769 // If the argument has been promoted from a smaller type, insert an
770 // assertion to capture this.
771 if (VA.getLocInfo() == CCValAssign::SExt)
772 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
773 DAG.getValueType(VA.getValVT()));
774 else if (VA.getLocInfo() == CCValAssign::ZExt)
775 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
776 DAG.getValueType(VA.getValVT()));
777
778 if (VA.isExtInLoc())
779 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
780 else if (VA.getLocInfo() == CCValAssign::Indirect)
781 Value = DAG.getLoad(VA.getValVT(), DL, Chain, Value,
782 MachinePointerInfo(), false, false, false, 0);
783 else
784 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
785 return Value;
786}
787
788// Value is a value of type VA.getValVT() that we need to copy into
789// the location described by VA. Return a copy of Value converted to
790// VA.getValVT(). The caller is responsible for handling indirect values.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000791static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000792 CCValAssign &VA, SDValue Value) {
793 switch (VA.getLocInfo()) {
794 case CCValAssign::SExt:
795 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
796 case CCValAssign::ZExt:
797 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
798 case CCValAssign::AExt:
799 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
800 case CCValAssign::Full:
801 return Value;
802 default:
803 llvm_unreachable("Unhandled getLocInfo()");
804 }
805}
806
807SDValue SystemZTargetLowering::
808LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
809 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000810 SDLoc DL, SelectionDAG &DAG,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000811 SmallVectorImpl<SDValue> &InVals) const {
812 MachineFunction &MF = DAG.getMachineFunction();
813 MachineFrameInfo *MFI = MF.getFrameInfo();
814 MachineRegisterInfo &MRI = MF.getRegInfo();
815 SystemZMachineFunctionInfo *FuncInfo =
Eric Christophera6734172015-01-31 00:06:45 +0000816 MF.getInfo<SystemZMachineFunctionInfo>();
817 auto *TFL =
818 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000819
820 // Assign locations to all of the incoming arguments.
821 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000822 SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000823 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
824
825 unsigned NumFixedGPRs = 0;
826 unsigned NumFixedFPRs = 0;
827 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
828 SDValue ArgValue;
829 CCValAssign &VA = ArgLocs[I];
830 EVT LocVT = VA.getLocVT();
831 if (VA.isRegLoc()) {
832 // Arguments passed in registers
833 const TargetRegisterClass *RC;
834 switch (LocVT.getSimpleVT().SimpleTy) {
835 default:
836 // Integers smaller than i64 should be promoted to i64.
837 llvm_unreachable("Unexpected argument type");
838 case MVT::i32:
839 NumFixedGPRs += 1;
840 RC = &SystemZ::GR32BitRegClass;
841 break;
842 case MVT::i64:
843 NumFixedGPRs += 1;
844 RC = &SystemZ::GR64BitRegClass;
845 break;
846 case MVT::f32:
847 NumFixedFPRs += 1;
848 RC = &SystemZ::FP32BitRegClass;
849 break;
850 case MVT::f64:
851 NumFixedFPRs += 1;
852 RC = &SystemZ::FP64BitRegClass;
853 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000854 case MVT::v16i8:
855 case MVT::v8i16:
856 case MVT::v4i32:
857 case MVT::v2i64:
Ulrich Weigandcd808232015-05-05 19:26:48 +0000858 case MVT::v2f64:
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000859 RC = &SystemZ::VR128BitRegClass;
860 break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000861 }
862
863 unsigned VReg = MRI.createVirtualRegister(RC);
864 MRI.addLiveIn(VA.getLocReg(), VReg);
865 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
866 } else {
867 assert(VA.isMemLoc() && "Argument not register or memory");
868
869 // Create the frame index object for this incoming parameter.
870 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
871 VA.getLocMemOffset(), true);
872
873 // Create the SelectionDAG nodes corresponding to a load
874 // from this parameter. Unpromoted ints and floats are
875 // passed as right-justified 8-byte values.
876 EVT PtrVT = getPointerTy();
877 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
878 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000879 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
880 DAG.getIntPtrConstant(4, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000881 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
882 MachinePointerInfo::getFixedStack(FI),
883 false, false, false, 0);
884 }
885
886 // Convert the value of the argument register into the value that's
887 // being passed.
888 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
889 }
890
891 if (IsVarArg) {
892 // Save the number of non-varargs registers for later use by va_start, etc.
893 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
894 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
895
896 // Likewise the address (in the form of a frame index) of where the
897 // first stack vararg would be. The 1-byte size here is arbitrary.
898 int64_t StackSize = CCInfo.getNextStackOffset();
899 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true));
900
901 // ...and a similar frame index for the caller-allocated save area
902 // that will be used to store the incoming registers.
903 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
904 unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true);
905 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
906
907 // Store the FPR varargs in the reserved frame slots. (We store the
908 // GPRs as part of the prologue.)
909 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
910 SDValue MemOps[SystemZ::NumArgFPRs];
911 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
912 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
913 int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true);
914 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
915 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
916 &SystemZ::FP64BitRegClass);
917 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
918 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
919 MachinePointerInfo::getFixedStack(FI),
920 false, false, 0);
921
922 }
923 // Join the stores, which are independent of one another.
924 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Craig Topper2d2aa0c2014-04-30 07:17:30 +0000925 makeArrayRef(&MemOps[NumFixedFPRs],
926 SystemZ::NumArgFPRs-NumFixedFPRs));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000927 }
928 }
929
930 return Chain;
931}
932
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000933static bool canUseSiblingCall(const CCState &ArgCCInfo,
Richard Sandiford709bda62013-08-19 12:42:31 +0000934 SmallVectorImpl<CCValAssign> &ArgLocs) {
935 // Punt if there are any indirect or stack arguments, or if the call
936 // needs the call-saved argument register R6.
937 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
938 CCValAssign &VA = ArgLocs[I];
939 if (VA.getLocInfo() == CCValAssign::Indirect)
940 return false;
941 if (!VA.isRegLoc())
942 return false;
943 unsigned Reg = VA.getLocReg();
Richard Sandiford0755c932013-10-01 11:26:28 +0000944 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
Richard Sandiford709bda62013-08-19 12:42:31 +0000945 return false;
946 }
947 return true;
948}
949
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000950SDValue
951SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
952 SmallVectorImpl<SDValue> &InVals) const {
953 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000954 SDLoc &DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000955 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
956 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
957 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000958 SDValue Chain = CLI.Chain;
959 SDValue Callee = CLI.Callee;
Richard Sandiford709bda62013-08-19 12:42:31 +0000960 bool &IsTailCall = CLI.IsTailCall;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000961 CallingConv::ID CallConv = CLI.CallConv;
962 bool IsVarArg = CLI.IsVarArg;
963 MachineFunction &MF = DAG.getMachineFunction();
964 EVT PtrVT = getPointerTy();
965
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000966 // Analyze the operands of the call, assigning locations to each operand.
967 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000968 SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000969 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
970
Richard Sandiford709bda62013-08-19 12:42:31 +0000971 // We don't support GuaranteedTailCallOpt, only automatically-detected
972 // sibling calls.
973 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs))
974 IsTailCall = false;
975
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000976 // Get a count of how many bytes are to be pushed on the stack.
977 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
978
979 // Mark the start of the call.
Richard Sandiford709bda62013-08-19 12:42:31 +0000980 if (!IsTailCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000981 Chain = DAG.getCALLSEQ_START(Chain,
982 DAG.getConstant(NumBytes, DL, PtrVT, true),
Richard Sandiford709bda62013-08-19 12:42:31 +0000983 DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000984
985 // Copy argument values to their designated locations.
986 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
987 SmallVector<SDValue, 8> MemOpChains;
988 SDValue StackPtr;
989 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
990 CCValAssign &VA = ArgLocs[I];
991 SDValue ArgValue = OutVals[I];
992
993 if (VA.getLocInfo() == CCValAssign::Indirect) {
994 // Store the argument in a stack slot and pass its address.
995 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
996 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
997 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, SpillSlot,
998 MachinePointerInfo::getFixedStack(FI),
999 false, false, 0));
1000 ArgValue = SpillSlot;
1001 } else
1002 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
1003
1004 if (VA.isRegLoc())
1005 // Queue up the argument copies and emit them at the end.
1006 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1007 else {
1008 assert(VA.isMemLoc() && "Argument not register or memory");
1009
1010 // Work out the address of the stack slot. Unpromoted ints and
1011 // floats are passed as right-justified 8-byte values.
1012 if (!StackPtr.getNode())
1013 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
1014 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
1015 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
1016 Offset += 4;
1017 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001018 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001019
1020 // Emit the store.
1021 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address,
1022 MachinePointerInfo(),
1023 false, false, 0));
1024 }
1025 }
1026
1027 // Join the stores, which are independent of one another.
1028 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001029 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001030
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001031 // Accept direct calls by converting symbolic call addresses to the
Richard Sandiford709bda62013-08-19 12:42:31 +00001032 // associated Target* opcodes. Force %r1 to be used for indirect
1033 // tail calls.
1034 SDValue Glue;
Richard Sandiford21f5d682014-03-06 11:22:58 +00001035 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001036 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
1037 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford21f5d682014-03-06 11:22:58 +00001038 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001039 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
1040 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford709bda62013-08-19 12:42:31 +00001041 } else if (IsTailCall) {
1042 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
1043 Glue = Chain.getValue(1);
1044 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
1045 }
1046
1047 // Build a sequence of copy-to-reg nodes, chained and glued together.
1048 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
1049 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
1050 RegsToPass[I].second, Glue);
1051 Glue = Chain.getValue(1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001052 }
1053
1054 // The first call operand is the chain and the second is the target address.
1055 SmallVector<SDValue, 8> Ops;
1056 Ops.push_back(Chain);
1057 Ops.push_back(Callee);
1058
1059 // Add argument registers to the end of the list so that they are
1060 // known live into the call.
1061 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
1062 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
1063 RegsToPass[I].second.getValueType()));
1064
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001065 // Add a register mask operand representing the call-preserved registers.
Eric Christophera6734172015-01-31 00:06:45 +00001066 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00001067 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001068 assert(Mask && "Missing call preserved mask for calling convention");
1069 Ops.push_back(DAG.getRegisterMask(Mask));
1070
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001071 // Glue the call to the argument copies, if any.
1072 if (Glue.getNode())
1073 Ops.push_back(Glue);
1074
1075 // Emit the call.
1076 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Richard Sandiford709bda62013-08-19 12:42:31 +00001077 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001078 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
1079 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001080 Glue = Chain.getValue(1);
1081
1082 // Mark the end of the call, which is glued to the call itself.
1083 Chain = DAG.getCALLSEQ_END(Chain,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001084 DAG.getConstant(NumBytes, DL, PtrVT, true),
1085 DAG.getConstant(0, DL, PtrVT, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001086 Glue, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001087 Glue = Chain.getValue(1);
1088
1089 // Assign locations to each value returned by this call.
1090 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001091 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001092 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
1093
1094 // Copy all of the result registers out of their specified physreg.
1095 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1096 CCValAssign &VA = RetLocs[I];
1097
1098 // Copy the value out, gluing the copy to the end of the call sequence.
1099 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
1100 VA.getLocVT(), Glue);
1101 Chain = RetValue.getValue(1);
1102 Glue = RetValue.getValue(2);
1103
1104 // Convert the value of the return register into the value that's
1105 // being returned.
1106 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
1107 }
1108
1109 return Chain;
1110}
1111
1112SDValue
1113SystemZTargetLowering::LowerReturn(SDValue Chain,
1114 CallingConv::ID CallConv, bool IsVarArg,
1115 const SmallVectorImpl<ISD::OutputArg> &Outs,
1116 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001117 SDLoc DL, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001118 MachineFunction &MF = DAG.getMachineFunction();
1119
1120 // Assign locations to each returned value.
1121 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001122 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001123 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
1124
1125 // Quick exit for void returns
1126 if (RetLocs.empty())
1127 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
1128
1129 // Copy the result values into the output registers.
1130 SDValue Glue;
1131 SmallVector<SDValue, 4> RetOps;
1132 RetOps.push_back(Chain);
1133 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1134 CCValAssign &VA = RetLocs[I];
1135 SDValue RetValue = OutVals[I];
1136
1137 // Make the return register live on exit.
1138 assert(VA.isRegLoc() && "Can only return in registers!");
1139
1140 // Promote the value as required.
1141 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
1142
1143 // Chain and glue the copies together.
1144 unsigned Reg = VA.getLocReg();
1145 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
1146 Glue = Chain.getValue(1);
1147 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
1148 }
1149
1150 // Update chain and glue.
1151 RetOps[0] = Chain;
1152 if (Glue.getNode())
1153 RetOps.push_back(Glue);
1154
Craig Topper48d114b2014-04-26 18:35:24 +00001155 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001156}
1157
Richard Sandiford9afe6132013-12-10 10:36:34 +00001158SDValue SystemZTargetLowering::
1159prepareVolatileOrAtomicLoad(SDValue Chain, SDLoc DL, SelectionDAG &DAG) const {
1160 return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain);
1161}
1162
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001163// Return true if Op is an intrinsic node with chain that returns the CC value
1164// as its only (other) argument. Provide the associated SystemZISD opcode and
1165// the mask of valid CC values if so.
1166static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode,
1167 unsigned &CCValid) {
1168 unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1169 switch (Id) {
1170 case Intrinsic::s390_tbegin:
1171 Opcode = SystemZISD::TBEGIN;
1172 CCValid = SystemZ::CCMASK_TBEGIN;
1173 return true;
1174
1175 case Intrinsic::s390_tbegin_nofloat:
1176 Opcode = SystemZISD::TBEGIN_NOFLOAT;
1177 CCValid = SystemZ::CCMASK_TBEGIN;
1178 return true;
1179
1180 case Intrinsic::s390_tend:
1181 Opcode = SystemZISD::TEND;
1182 CCValid = SystemZ::CCMASK_TEND;
1183 return true;
1184
1185 default:
1186 return false;
1187 }
1188}
1189
1190// Emit an intrinsic with chain with a glued value instead of its CC result.
1191static SDValue emitIntrinsicWithChainAndGlue(SelectionDAG &DAG, SDValue Op,
1192 unsigned Opcode) {
1193 // Copy all operands except the intrinsic ID.
1194 unsigned NumOps = Op.getNumOperands();
1195 SmallVector<SDValue, 6> Ops;
1196 Ops.reserve(NumOps - 1);
1197 Ops.push_back(Op.getOperand(0));
1198 for (unsigned I = 2; I < NumOps; ++I)
1199 Ops.push_back(Op.getOperand(I));
1200
1201 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
1202 SDVTList RawVTs = DAG.getVTList(MVT::Other, MVT::Glue);
1203 SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1204 SDValue OldChain = SDValue(Op.getNode(), 1);
1205 SDValue NewChain = SDValue(Intr.getNode(), 0);
1206 DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain);
1207 return Intr;
1208}
1209
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001210// CC is a comparison that will be implemented using an integer or
1211// floating-point comparison. Return the condition code mask for
1212// a branch on true. In the integer case, CCMASK_CMP_UO is set for
1213// unsigned comparisons and clear for signed ones. In the floating-point
1214// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1215static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1216#define CONV(X) \
1217 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1218 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1219 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1220
1221 switch (CC) {
1222 default:
1223 llvm_unreachable("Invalid integer condition!");
1224
1225 CONV(EQ);
1226 CONV(NE);
1227 CONV(GT);
1228 CONV(GE);
1229 CONV(LT);
1230 CONV(LE);
1231
1232 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1233 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1234 }
1235#undef CONV
1236}
1237
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001238// Return a sequence for getting a 1 from an IPM result when CC has a
1239// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1240// The handling of CC values outside CCValid doesn't matter.
1241static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1242 // Deal with cases where the result can be taken directly from a bit
1243 // of the IPM result.
1244 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1245 return IPMConversion(0, 0, SystemZ::IPM_CC);
1246 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1247 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1248
1249 // Deal with cases where we can add a value to force the sign bit
1250 // to contain the right value. Putting the bit in 31 means we can
1251 // use SRL rather than RISBG(L), and also makes it easier to get a
1252 // 0/-1 value, so it has priority over the other tests below.
1253 //
1254 // These sequences rely on the fact that the upper two bits of the
1255 // IPM result are zero.
1256 uint64_t TopBit = uint64_t(1) << 31;
1257 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1258 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1259 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1260 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1261 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1262 | SystemZ::CCMASK_1
1263 | SystemZ::CCMASK_2)))
1264 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1265 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1266 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1267 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1268 | SystemZ::CCMASK_2
1269 | SystemZ::CCMASK_3)))
1270 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1271
1272 // Next try inverting the value and testing a bit. 0/1 could be
1273 // handled this way too, but we dealt with that case above.
1274 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1275 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1276
1277 // Handle cases where adding a value forces a non-sign bit to contain
1278 // the right value.
1279 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1280 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1281 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1282 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1283
Alp Tokercb402912014-01-24 17:20:08 +00001284 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001285 // can be done by inverting the low CC bit and applying one of the
1286 // sign-based extractions above.
1287 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1288 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1289 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1290 return IPMConversion(1 << SystemZ::IPM_CC,
1291 TopBit - (3 << SystemZ::IPM_CC), 31);
1292 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1293 | SystemZ::CCMASK_1
1294 | SystemZ::CCMASK_3)))
1295 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1296 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1297 | SystemZ::CCMASK_2
1298 | SystemZ::CCMASK_3)))
1299 return IPMConversion(1 << SystemZ::IPM_CC,
1300 TopBit - (1 << SystemZ::IPM_CC), 31);
1301
1302 llvm_unreachable("Unexpected CC combination");
1303}
1304
Richard Sandifordd420f732013-12-13 15:28:45 +00001305// If C can be converted to a comparison against zero, adjust the operands
Richard Sandiforda0757082013-08-01 10:29:45 +00001306// as necessary.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001307static void adjustZeroCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001308 if (C.ICmpType == SystemZICMP::UnsignedOnly)
Richard Sandiforda0757082013-08-01 10:29:45 +00001309 return;
1310
Richard Sandiford21f5d682014-03-06 11:22:58 +00001311 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
Richard Sandiforda0757082013-08-01 10:29:45 +00001312 if (!ConstOp1)
1313 return;
1314
1315 int64_t Value = ConstOp1->getSExtValue();
Richard Sandifordd420f732013-12-13 15:28:45 +00001316 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1317 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1318 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1319 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1320 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001321 C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType());
Richard Sandiforda0757082013-08-01 10:29:45 +00001322 }
1323}
1324
Richard Sandifordd420f732013-12-13 15:28:45 +00001325// If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1326// adjust the operands as necessary.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001327static void adjustSubwordCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001328 // For us to make any changes, it must a comparison between a single-use
1329 // load and a constant.
Richard Sandifordd420f732013-12-13 15:28:45 +00001330 if (!C.Op0.hasOneUse() ||
1331 C.Op0.getOpcode() != ISD::LOAD ||
1332 C.Op1.getOpcode() != ISD::Constant)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001333 return;
1334
1335 // We must have an 8- or 16-bit load.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001336 auto *Load = cast<LoadSDNode>(C.Op0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001337 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1338 if (NumBits != 8 && NumBits != 16)
1339 return;
1340
1341 // The load must be an extending one and the constant must be within the
1342 // range of the unextended value.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001343 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001344 uint64_t Value = ConstOp1->getZExtValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001345 uint64_t Mask = (1 << NumBits) - 1;
1346 if (Load->getExtensionType() == ISD::SEXTLOAD) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001347 // Make sure that ConstOp1 is in range of C.Op0.
1348 int64_t SignedValue = ConstOp1->getSExtValue();
1349 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001350 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001351 if (C.ICmpType != SystemZICMP::SignedOnly) {
1352 // Unsigned comparison between two sign-extended values is equivalent
1353 // to unsigned comparison between two zero-extended values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001354 Value &= Mask;
Richard Sandifordd420f732013-12-13 15:28:45 +00001355 } else if (NumBits == 8) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001356 // Try to treat the comparison as unsigned, so that we can use CLI.
1357 // Adjust CCMask and Value as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001358 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001359 // Test whether the high bit of the byte is set.
Richard Sandifordd420f732013-12-13 15:28:45 +00001360 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1361 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001362 // Test whether the high bit of the byte is clear.
Richard Sandifordd420f732013-12-13 15:28:45 +00001363 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001364 else
1365 // No instruction exists for this combination.
1366 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001367 C.ICmpType = SystemZICMP::UnsignedOnly;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001368 }
1369 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1370 if (Value > Mask)
1371 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001372 assert(C.ICmpType == SystemZICMP::Any &&
1373 "Signedness shouldn't matter here.");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001374 } else
1375 return;
1376
1377 // Make sure that the first operand is an i32 of the right extension type.
Richard Sandifordd420f732013-12-13 15:28:45 +00001378 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1379 ISD::SEXTLOAD :
1380 ISD::ZEXTLOAD);
1381 if (C.Op0.getValueType() != MVT::i32 ||
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001382 Load->getExtensionType() != ExtType)
Richard Sandifordd420f732013-12-13 15:28:45 +00001383 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32,
1384 Load->getChain(), Load->getBasePtr(),
1385 Load->getPointerInfo(), Load->getMemoryVT(),
1386 Load->isVolatile(), Load->isNonTemporal(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00001387 Load->isInvariant(), Load->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001388
1389 // Make sure that the second operand is an i32 with the right value.
Richard Sandifordd420f732013-12-13 15:28:45 +00001390 if (C.Op1.getValueType() != MVT::i32 ||
1391 Value != ConstOp1->getZExtValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001392 C.Op1 = DAG.getConstant(Value, DL, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001393}
1394
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001395// Return true if Op is either an unextended load, or a load suitable
1396// for integer register-memory comparisons of type ICmpType.
1397static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001398 auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001399 if (Load) {
1400 // There are no instructions to compare a register with a memory byte.
1401 if (Load->getMemoryVT() == MVT::i8)
1402 return false;
1403 // Otherwise decide on extension type.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001404 switch (Load->getExtensionType()) {
1405 case ISD::NON_EXTLOAD:
Richard Sandiford24e597b2013-08-23 11:27:19 +00001406 return true;
1407 case ISD::SEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001408 return ICmpType != SystemZICMP::UnsignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001409 case ISD::ZEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001410 return ICmpType != SystemZICMP::SignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001411 default:
1412 break;
1413 }
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001414 }
Richard Sandiford24e597b2013-08-23 11:27:19 +00001415 return false;
1416}
1417
Richard Sandifordd420f732013-12-13 15:28:45 +00001418// Return true if it is better to swap the operands of C.
1419static bool shouldSwapCmpOperands(const Comparison &C) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001420 // Leave f128 comparisons alone, since they have no memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001421 if (C.Op0.getValueType() == MVT::f128)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001422 return false;
1423
1424 // Always keep a floating-point constant second, since comparisons with
1425 // zero can use LOAD TEST and comparisons with other constants make a
1426 // natural memory operand.
Richard Sandifordd420f732013-12-13 15:28:45 +00001427 if (isa<ConstantFPSDNode>(C.Op1))
Richard Sandiford24e597b2013-08-23 11:27:19 +00001428 return false;
1429
1430 // Never swap comparisons with zero since there are many ways to optimize
1431 // those later.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001432 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001433 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001434 return false;
1435
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001436 // Also keep natural memory operands second if the loaded value is
1437 // only used here. Several comparisons have memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001438 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001439 return false;
1440
Richard Sandiford24e597b2013-08-23 11:27:19 +00001441 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1442 // In that case we generally prefer the memory to be second.
Richard Sandifordd420f732013-12-13 15:28:45 +00001443 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001444 // The only exceptions are when the second operand is a constant and
1445 // we can use things like CHHSI.
Richard Sandifordd420f732013-12-13 15:28:45 +00001446 if (!ConstOp1)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001447 return true;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001448 // The unsigned memory-immediate instructions can handle 16-bit
1449 // unsigned integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001450 if (C.ICmpType != SystemZICMP::SignedOnly &&
1451 isUInt<16>(ConstOp1->getZExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001452 return false;
1453 // The signed memory-immediate instructions can handle 16-bit
1454 // signed integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001455 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1456 isInt<16>(ConstOp1->getSExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001457 return false;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001458 return true;
1459 }
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001460
1461 // Try to promote the use of CGFR and CLGFR.
Richard Sandifordd420f732013-12-13 15:28:45 +00001462 unsigned Opcode0 = C.Op0.getOpcode();
1463 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001464 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001465 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001466 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001467 if (C.ICmpType != SystemZICMP::SignedOnly &&
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001468 Opcode0 == ISD::AND &&
Richard Sandifordd420f732013-12-13 15:28:45 +00001469 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1470 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001471 return true;
1472
Richard Sandiford24e597b2013-08-23 11:27:19 +00001473 return false;
1474}
1475
Richard Sandiford73170f82013-12-11 11:45:08 +00001476// Return a version of comparison CC mask CCMask in which the LT and GT
1477// actions are swapped.
1478static unsigned reverseCCMask(unsigned CCMask) {
1479 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1480 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1481 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1482 (CCMask & SystemZ::CCMASK_CMP_UO));
1483}
1484
Richard Sandiford0847c452013-12-13 15:50:30 +00001485// Check whether C tests for equality between X and Y and whether X - Y
1486// or Y - X is also computed. In that case it's better to compare the
1487// result of the subtraction against zero.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001488static void adjustForSubtraction(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001489 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1490 C.CCMask == SystemZ::CCMASK_CMP_NE) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001491 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001492 SDNode *N = *I;
1493 if (N->getOpcode() == ISD::SUB &&
1494 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1495 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1496 C.Op0 = SDValue(N, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001497 C.Op1 = DAG.getConstant(0, DL, N->getValueType(0));
Richard Sandiford0847c452013-12-13 15:50:30 +00001498 return;
1499 }
1500 }
1501 }
1502}
1503
Richard Sandifordd420f732013-12-13 15:28:45 +00001504// Check whether C compares a floating-point value with zero and if that
1505// floating-point value is also negated. In this case we can use the
1506// negation to set CC, so avoiding separate LOAD AND TEST and
1507// LOAD (NEGATIVE/COMPLEMENT) instructions.
1508static void adjustForFNeg(Comparison &C) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001509 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
Richard Sandiford73170f82013-12-11 11:45:08 +00001510 if (C1 && C1->isZero()) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001511 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford73170f82013-12-11 11:45:08 +00001512 SDNode *N = *I;
1513 if (N->getOpcode() == ISD::FNEG) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001514 C.Op0 = SDValue(N, 0);
1515 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford73170f82013-12-11 11:45:08 +00001516 return;
1517 }
1518 }
1519 }
1520}
1521
Richard Sandifordd420f732013-12-13 15:28:45 +00001522// Check whether C compares (shl X, 32) with 0 and whether X is
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001523// also sign-extended. In that case it is better to test the result
1524// of the sign extension using LTGFR.
1525//
1526// This case is important because InstCombine transforms a comparison
1527// with (sext (trunc X)) into a comparison with (shl X, 32).
Richard Sandifordd420f732013-12-13 15:28:45 +00001528static void adjustForLTGFR(Comparison &C) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001529 // Check for a comparison between (shl X, 32) and 0.
Richard Sandifordd420f732013-12-13 15:28:45 +00001530 if (C.Op0.getOpcode() == ISD::SHL &&
1531 C.Op0.getValueType() == MVT::i64 &&
1532 C.Op1.getOpcode() == ISD::Constant &&
1533 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001534 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001535 if (C1 && C1->getZExtValue() == 32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001536 SDValue ShlOp0 = C.Op0.getOperand(0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001537 // See whether X has any SIGN_EXTEND_INREG uses.
Richard Sandiford28c111e2014-03-06 11:00:15 +00001538 for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001539 SDNode *N = *I;
1540 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
1541 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001542 C.Op0 = SDValue(N, 0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001543 return;
1544 }
1545 }
1546 }
1547 }
1548}
1549
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001550// If C compares the truncation of an extending load, try to compare
1551// the untruncated value instead. This exposes more opportunities to
1552// reuse CC.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001553static void adjustICmpTruncate(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001554 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
1555 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
1556 C.Op1.getOpcode() == ISD::Constant &&
1557 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001558 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001559 if (L->getMemoryVT().getStoreSizeInBits()
1560 <= C.Op0.getValueType().getSizeInBits()) {
1561 unsigned Type = L->getExtensionType();
1562 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
1563 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
1564 C.Op0 = C.Op0.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001565 C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType());
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001566 }
1567 }
1568 }
1569}
1570
Richard Sandiford030c1652013-09-13 09:09:50 +00001571// Return true if shift operation N has an in-range constant shift value.
1572// Store it in ShiftVal if so.
1573static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001574 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
Richard Sandiford030c1652013-09-13 09:09:50 +00001575 if (!Shift)
1576 return false;
1577
1578 uint64_t Amount = Shift->getZExtValue();
1579 if (Amount >= N.getValueType().getSizeInBits())
1580 return false;
1581
1582 ShiftVal = Amount;
1583 return true;
1584}
1585
1586// Check whether an AND with Mask is suitable for a TEST UNDER MASK
1587// instruction and whether the CC value is descriptive enough to handle
1588// a comparison of type Opcode between the AND result and CmpVal.
1589// CCMask says which comparison result is being tested and BitSize is
1590// the number of bits in the operands. If TEST UNDER MASK can be used,
1591// return the corresponding CC mask, otherwise return 0.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001592static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
1593 uint64_t Mask, uint64_t CmpVal,
1594 unsigned ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001595 assert(Mask != 0 && "ANDs with zero should have been removed by now");
1596
Richard Sandiford030c1652013-09-13 09:09:50 +00001597 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
1598 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
1599 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
1600 return 0;
1601
Richard Sandiford113c8702013-09-03 15:38:35 +00001602 // Work out the masks for the lowest and highest bits.
1603 unsigned HighShift = 63 - countLeadingZeros(Mask);
1604 uint64_t High = uint64_t(1) << HighShift;
1605 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
1606
1607 // Signed ordered comparisons are effectively unsigned if the sign
1608 // bit is dropped.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001609 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
Richard Sandiford113c8702013-09-03 15:38:35 +00001610
1611 // Check for equality comparisons with 0, or the equivalent.
1612 if (CmpVal == 0) {
1613 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1614 return SystemZ::CCMASK_TM_ALL_0;
1615 if (CCMask == SystemZ::CCMASK_CMP_NE)
1616 return SystemZ::CCMASK_TM_SOME_1;
1617 }
1618 if (EffectivelyUnsigned && CmpVal <= Low) {
1619 if (CCMask == SystemZ::CCMASK_CMP_LT)
1620 return SystemZ::CCMASK_TM_ALL_0;
1621 if (CCMask == SystemZ::CCMASK_CMP_GE)
1622 return SystemZ::CCMASK_TM_SOME_1;
1623 }
1624 if (EffectivelyUnsigned && CmpVal < Low) {
1625 if (CCMask == SystemZ::CCMASK_CMP_LE)
1626 return SystemZ::CCMASK_TM_ALL_0;
1627 if (CCMask == SystemZ::CCMASK_CMP_GT)
1628 return SystemZ::CCMASK_TM_SOME_1;
1629 }
1630
1631 // Check for equality comparisons with the mask, or the equivalent.
1632 if (CmpVal == Mask) {
1633 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1634 return SystemZ::CCMASK_TM_ALL_1;
1635 if (CCMask == SystemZ::CCMASK_CMP_NE)
1636 return SystemZ::CCMASK_TM_SOME_0;
1637 }
1638 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
1639 if (CCMask == SystemZ::CCMASK_CMP_GT)
1640 return SystemZ::CCMASK_TM_ALL_1;
1641 if (CCMask == SystemZ::CCMASK_CMP_LE)
1642 return SystemZ::CCMASK_TM_SOME_0;
1643 }
1644 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
1645 if (CCMask == SystemZ::CCMASK_CMP_GE)
1646 return SystemZ::CCMASK_TM_ALL_1;
1647 if (CCMask == SystemZ::CCMASK_CMP_LT)
1648 return SystemZ::CCMASK_TM_SOME_0;
1649 }
1650
1651 // Check for ordered comparisons with the top bit.
1652 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
1653 if (CCMask == SystemZ::CCMASK_CMP_LE)
1654 return SystemZ::CCMASK_TM_MSB_0;
1655 if (CCMask == SystemZ::CCMASK_CMP_GT)
1656 return SystemZ::CCMASK_TM_MSB_1;
1657 }
1658 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
1659 if (CCMask == SystemZ::CCMASK_CMP_LT)
1660 return SystemZ::CCMASK_TM_MSB_0;
1661 if (CCMask == SystemZ::CCMASK_CMP_GE)
1662 return SystemZ::CCMASK_TM_MSB_1;
1663 }
1664
1665 // If there are just two bits, we can do equality checks for Low and High
1666 // as well.
1667 if (Mask == Low + High) {
1668 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
1669 return SystemZ::CCMASK_TM_MIXED_MSB_0;
1670 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
1671 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
1672 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
1673 return SystemZ::CCMASK_TM_MIXED_MSB_1;
1674 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
1675 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
1676 }
1677
1678 // Looks like we've exhausted our options.
1679 return 0;
1680}
1681
Richard Sandifordd420f732013-12-13 15:28:45 +00001682// See whether C can be implemented as a TEST UNDER MASK instruction.
1683// Update the arguments with the TM version if so.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001684static void adjustForTestUnderMask(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001685 // Check that we have a comparison with a constant.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001686 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001687 if (!ConstOp1)
Richard Sandiford35b9be22013-08-28 10:31:43 +00001688 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001689 uint64_t CmpVal = ConstOp1->getZExtValue();
Richard Sandiford35b9be22013-08-28 10:31:43 +00001690
1691 // Check whether the nonconstant input is an AND with a constant mask.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001692 Comparison NewC(C);
1693 uint64_t MaskVal;
Craig Topper062a2ba2014-04-25 05:30:21 +00001694 ConstantSDNode *Mask = nullptr;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001695 if (C.Op0.getOpcode() == ISD::AND) {
1696 NewC.Op0 = C.Op0.getOperand(0);
1697 NewC.Op1 = C.Op0.getOperand(1);
1698 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
1699 if (!Mask)
1700 return;
1701 MaskVal = Mask->getZExtValue();
1702 } else {
1703 // There is no instruction to compare with a 64-bit immediate
1704 // so use TMHH instead if possible. We need an unsigned ordered
1705 // comparison with an i64 immediate.
1706 if (NewC.Op0.getValueType() != MVT::i64 ||
1707 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
1708 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
1709 NewC.ICmpType == SystemZICMP::SignedOnly)
1710 return;
1711 // Convert LE and GT comparisons into LT and GE.
1712 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
1713 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
1714 if (CmpVal == uint64_t(-1))
1715 return;
1716 CmpVal += 1;
1717 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1718 }
1719 // If the low N bits of Op1 are zero than the low N bits of Op0 can
1720 // be masked off without changing the result.
1721 MaskVal = -(CmpVal & -CmpVal);
1722 NewC.ICmpType = SystemZICMP::UnsignedOnly;
1723 }
Ulrich Weigandb8d76fb2015-03-30 13:46:59 +00001724 if (!MaskVal)
1725 return;
Richard Sandiford35b9be22013-08-28 10:31:43 +00001726
Richard Sandiford113c8702013-09-03 15:38:35 +00001727 // Check whether the combination of mask, comparison value and comparison
1728 // type are suitable.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001729 unsigned BitSize = NewC.Op0.getValueType().getSizeInBits();
Richard Sandiford030c1652013-09-13 09:09:50 +00001730 unsigned NewCCMask, ShiftVal;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001731 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
1732 NewC.Op0.getOpcode() == ISD::SHL &&
1733 isSimpleShift(NewC.Op0, ShiftVal) &&
1734 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
1735 MaskVal >> ShiftVal,
Richard Sandiford030c1652013-09-13 09:09:50 +00001736 CmpVal >> ShiftVal,
1737 SystemZICMP::Any))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001738 NewC.Op0 = NewC.Op0.getOperand(0);
1739 MaskVal >>= ShiftVal;
1740 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
1741 NewC.Op0.getOpcode() == ISD::SRL &&
1742 isSimpleShift(NewC.Op0, ShiftVal) &&
1743 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
Richard Sandiford030c1652013-09-13 09:09:50 +00001744 MaskVal << ShiftVal,
1745 CmpVal << ShiftVal,
1746 SystemZICMP::UnsignedOnly))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001747 NewC.Op0 = NewC.Op0.getOperand(0);
1748 MaskVal <<= ShiftVal;
Richard Sandiford030c1652013-09-13 09:09:50 +00001749 } else {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001750 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
1751 NewC.ICmpType);
Richard Sandiford030c1652013-09-13 09:09:50 +00001752 if (!NewCCMask)
1753 return;
1754 }
Richard Sandiford113c8702013-09-03 15:38:35 +00001755
Richard Sandiford35b9be22013-08-28 10:31:43 +00001756 // Go ahead and make the change.
Richard Sandifordd420f732013-12-13 15:28:45 +00001757 C.Opcode = SystemZISD::TM;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001758 C.Op0 = NewC.Op0;
1759 if (Mask && Mask->getZExtValue() == MaskVal)
1760 C.Op1 = SDValue(Mask, 0);
1761 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001762 C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType());
Richard Sandifordd420f732013-12-13 15:28:45 +00001763 C.CCValid = SystemZ::CCMASK_TM;
1764 C.CCMask = NewCCMask;
Richard Sandiford35b9be22013-08-28 10:31:43 +00001765}
1766
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001767// Return a Comparison that tests the condition-code result of intrinsic
1768// node Call against constant integer CC using comparison code Cond.
1769// Opcode is the opcode of the SystemZISD operation for the intrinsic
1770// and CCValid is the set of possible condition-code results.
1771static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode,
1772 SDValue Call, unsigned CCValid, uint64_t CC,
1773 ISD::CondCode Cond) {
1774 Comparison C(Call, SDValue());
1775 C.Opcode = Opcode;
1776 C.CCValid = CCValid;
1777 if (Cond == ISD::SETEQ)
1778 // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3.
1779 C.CCMask = CC < 4 ? 1 << (3 - CC) : 0;
1780 else if (Cond == ISD::SETNE)
1781 // ...and the inverse of that.
1782 C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1;
1783 else if (Cond == ISD::SETLT || Cond == ISD::SETULT)
1784 // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3,
1785 // always true for CC>3.
1786 C.CCMask = CC < 4 ? -1 << (4 - CC) : -1;
1787 else if (Cond == ISD::SETGE || Cond == ISD::SETUGE)
1788 // ...and the inverse of that.
1789 C.CCMask = CC < 4 ? ~(-1 << (4 - CC)) : 0;
1790 else if (Cond == ISD::SETLE || Cond == ISD::SETULE)
1791 // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true),
1792 // always true for CC>3.
1793 C.CCMask = CC < 4 ? -1 << (3 - CC) : -1;
1794 else if (Cond == ISD::SETGT || Cond == ISD::SETUGT)
1795 // ...and the inverse of that.
1796 C.CCMask = CC < 4 ? ~(-1 << (3 - CC)) : 0;
1797 else
1798 llvm_unreachable("Unexpected integer comparison type");
1799 C.CCMask &= CCValid;
1800 return C;
1801}
1802
Richard Sandifordd420f732013-12-13 15:28:45 +00001803// Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
1804static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001805 ISD::CondCode Cond, SDLoc DL) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001806 if (CmpOp1.getOpcode() == ISD::Constant) {
1807 uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue();
1808 unsigned Opcode, CCValid;
1809 if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
1810 CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) &&
1811 isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid))
1812 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
1813 }
Richard Sandifordd420f732013-12-13 15:28:45 +00001814 Comparison C(CmpOp0, CmpOp1);
1815 C.CCMask = CCMaskForCondCode(Cond);
1816 if (C.Op0.getValueType().isFloatingPoint()) {
1817 C.CCValid = SystemZ::CCMASK_FCMP;
1818 C.Opcode = SystemZISD::FCMP;
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001819 adjustForFNeg(C);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001820 } else {
Richard Sandifordd420f732013-12-13 15:28:45 +00001821 C.CCValid = SystemZ::CCMASK_ICMP;
1822 C.Opcode = SystemZISD::ICMP;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001823 // Choose the type of comparison. Equality and inequality tests can
1824 // use either signed or unsigned comparisons. The choice also doesn't
1825 // matter if both sign bits are known to be clear. In those cases we
1826 // want to give the main isel code the freedom to choose whichever
1827 // form fits best.
Richard Sandifordd420f732013-12-13 15:28:45 +00001828 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1829 C.CCMask == SystemZ::CCMASK_CMP_NE ||
1830 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
1831 C.ICmpType = SystemZICMP::Any;
1832 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
1833 C.ICmpType = SystemZICMP::UnsignedOnly;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001834 else
Richard Sandifordd420f732013-12-13 15:28:45 +00001835 C.ICmpType = SystemZICMP::SignedOnly;
1836 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001837 adjustZeroCmp(DAG, DL, C);
1838 adjustSubwordCmp(DAG, DL, C);
1839 adjustForSubtraction(DAG, DL, C);
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001840 adjustForLTGFR(C);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001841 adjustICmpTruncate(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001842 }
1843
Richard Sandifordd420f732013-12-13 15:28:45 +00001844 if (shouldSwapCmpOperands(C)) {
1845 std::swap(C.Op0, C.Op1);
1846 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford24e597b2013-08-23 11:27:19 +00001847 }
1848
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001849 adjustForTestUnderMask(DAG, DL, C);
Richard Sandifordd420f732013-12-13 15:28:45 +00001850 return C;
1851}
1852
1853// Emit the comparison instruction described by C.
1854static SDValue emitCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001855 if (!C.Op1.getNode()) {
1856 SDValue Op;
1857 switch (C.Op0.getOpcode()) {
1858 case ISD::INTRINSIC_W_CHAIN:
1859 Op = emitIntrinsicWithChainAndGlue(DAG, C.Op0, C.Opcode);
1860 break;
1861 default:
1862 llvm_unreachable("Invalid comparison operands");
1863 }
1864 return SDValue(Op.getNode(), Op->getNumValues() - 1);
1865 }
Richard Sandifordd420f732013-12-13 15:28:45 +00001866 if (C.Opcode == SystemZISD::ICMP)
1867 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001868 DAG.getConstant(C.ICmpType, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00001869 if (C.Opcode == SystemZISD::TM) {
1870 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
1871 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
1872 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001873 DAG.getConstant(RegisterOnly, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00001874 }
1875 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001876}
1877
Richard Sandiford7d86e472013-08-21 09:34:56 +00001878// Implement a 32-bit *MUL_LOHI operation by extending both operands to
1879// 64 bits. Extend is the extension type to use. Store the high part
1880// in Hi and the low part in Lo.
1881static void lowerMUL_LOHI32(SelectionDAG &DAG, SDLoc DL,
1882 unsigned Extend, SDValue Op0, SDValue Op1,
1883 SDValue &Hi, SDValue &Lo) {
1884 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
1885 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
1886 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001887 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
1888 DAG.getConstant(32, DL, MVT::i64));
Richard Sandiford7d86e472013-08-21 09:34:56 +00001889 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
1890 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
1891}
1892
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001893// Lower a binary operation that produces two VT results, one in each
1894// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
1895// Extend extends Op0 to a GR128, and Opcode performs the GR128 operation
1896// on the extended Op0 and (unextended) Op1. Store the even register result
1897// in Even and the odd register result in Odd.
Andrew Trickef9de2a2013-05-25 02:42:55 +00001898static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001899 unsigned Extend, unsigned Opcode,
1900 SDValue Op0, SDValue Op1,
1901 SDValue &Even, SDValue &Odd) {
1902 SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0);
1903 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped,
1904 SDValue(In128, 0), Op1);
1905 bool Is32Bit = is32Bit(VT);
Richard Sandifordd8163202013-09-13 09:12:44 +00001906 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
1907 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001908}
1909
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001910// Return an i32 value that is 1 if the CC value produced by Glue is
1911// in the mask CCMask and 0 otherwise. CC is known to have a value
1912// in CCValid, so other values can be ignored.
1913static SDValue emitSETCC(SelectionDAG &DAG, SDLoc DL, SDValue Glue,
1914 unsigned CCValid, unsigned CCMask) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001915 IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
1916 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
1917
1918 if (Conversion.XORValue)
1919 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001920 DAG.getConstant(Conversion.XORValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001921
1922 if (Conversion.AddValue)
1923 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001924 DAG.getConstant(Conversion.AddValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001925
1926 // The SHR/AND sequence should get optimized to an RISBG.
1927 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001928 DAG.getConstant(Conversion.Bit, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001929 if (Conversion.Bit != 31)
1930 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001931 DAG.getConstant(1, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001932 return Result;
1933}
1934
Ulrich Weigandcd808232015-05-05 19:26:48 +00001935// Return the SystemISD vector comparison operation for CC, or 0 if it cannot
1936// be done directly. IsFP is true if CC is for a floating-point rather than
1937// integer comparison.
1938static unsigned getVectorComparison(ISD::CondCode CC, bool IsFP) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001939 switch (CC) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00001940 case ISD::SETOEQ:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001941 case ISD::SETEQ:
Ulrich Weigandcd808232015-05-05 19:26:48 +00001942 return IsFP ? SystemZISD::VFCMPE : SystemZISD::VICMPE;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001943
Ulrich Weigandcd808232015-05-05 19:26:48 +00001944 case ISD::SETOGE:
1945 case ISD::SETGE:
1946 return IsFP ? SystemZISD::VFCMPHE : 0;
1947
1948 case ISD::SETOGT:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001949 case ISD::SETGT:
Ulrich Weigandcd808232015-05-05 19:26:48 +00001950 return IsFP ? SystemZISD::VFCMPH : SystemZISD::VICMPH;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001951
1952 case ISD::SETUGT:
Ulrich Weigandcd808232015-05-05 19:26:48 +00001953 return IsFP ? 0 : SystemZISD::VICMPHL;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001954
1955 default:
1956 return 0;
1957 }
1958}
1959
1960// Return the SystemZISD vector comparison operation for CC or its inverse,
1961// or 0 if neither can be done directly. Indicate in Invert whether the
Ulrich Weigandcd808232015-05-05 19:26:48 +00001962// result is for the inverse of CC. IsFP is true if CC is for a
1963// floating-point rather than integer comparison.
1964static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, bool IsFP,
1965 bool &Invert) {
1966 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001967 Invert = false;
1968 return Opcode;
1969 }
1970
Ulrich Weigandcd808232015-05-05 19:26:48 +00001971 CC = ISD::getSetCCInverse(CC, !IsFP);
1972 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001973 Invert = true;
1974 return Opcode;
1975 }
1976
1977 return 0;
1978}
1979
1980// Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing
1981// an integer mask of type VT.
1982static SDValue lowerVectorSETCC(SelectionDAG &DAG, SDLoc DL, EVT VT,
1983 ISD::CondCode CC, SDValue CmpOp0,
1984 SDValue CmpOp1) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00001985 bool IsFP = CmpOp0.getValueType().isFloatingPoint();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001986 bool Invert = false;
1987 SDValue Cmp;
Ulrich Weigandcd808232015-05-05 19:26:48 +00001988 switch (CC) {
1989 // Handle tests for order using (or (ogt y x) (oge x y)).
1990 case ISD::SETUO:
1991 Invert = true;
1992 case ISD::SETO: {
1993 assert(IsFP && "Unexpected integer comparison");
1994 SDValue LT = DAG.getNode(SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
1995 SDValue GE = DAG.getNode(SystemZISD::VFCMPHE, DL, VT, CmpOp0, CmpOp1);
1996 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE);
1997 break;
1998 }
1999
2000 // Handle <> tests using (or (ogt y x) (ogt x y)).
2001 case ISD::SETUEQ:
2002 Invert = true;
2003 case ISD::SETONE: {
2004 assert(IsFP && "Unexpected integer comparison");
2005 SDValue LT = DAG.getNode(SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2006 SDValue GT = DAG.getNode(SystemZISD::VFCMPH, DL, VT, CmpOp0, CmpOp1);
2007 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT);
2008 break;
2009 }
2010
2011 // Otherwise a single comparison is enough. It doesn't really
2012 // matter whether we try the inversion or the swap first, since
2013 // there are no cases where both work.
2014 default:
2015 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
2016 Cmp = DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1);
2017 else {
2018 CC = ISD::getSetCCSwappedOperands(CC);
2019 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
2020 Cmp = DAG.getNode(Opcode, DL, VT, CmpOp1, CmpOp0);
2021 else
2022 llvm_unreachable("Unhandled comparison");
2023 }
2024 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002025 }
2026 if (Invert) {
2027 SDValue Mask = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2028 DAG.getConstant(65535, DL, MVT::i32));
2029 Mask = DAG.getNode(ISD::BITCAST, DL, VT, Mask);
2030 Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask);
2031 }
2032 return Cmp;
2033}
2034
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002035SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
2036 SelectionDAG &DAG) const {
2037 SDValue CmpOp0 = Op.getOperand(0);
2038 SDValue CmpOp1 = Op.getOperand(1);
2039 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
2040 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002041 EVT VT = Op.getValueType();
2042 if (VT.isVector())
2043 return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002044
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002045 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002046 SDValue Glue = emitCmp(DAG, DL, C);
2047 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002048}
2049
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002050SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002051 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2052 SDValue CmpOp0 = Op.getOperand(2);
2053 SDValue CmpOp1 = Op.getOperand(3);
2054 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002055 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002056
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002057 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002058 SDValue Glue = emitCmp(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002059 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002060 Op.getOperand(0), DAG.getConstant(C.CCValid, DL, MVT::i32),
2061 DAG.getConstant(C.CCMask, DL, MVT::i32), Dest, Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002062}
2063
Richard Sandiford57485472013-12-13 15:35:00 +00002064// Return true if Pos is CmpOp and Neg is the negative of CmpOp,
2065// allowing Pos and Neg to be wider than CmpOp.
2066static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
2067 return (Neg.getOpcode() == ISD::SUB &&
2068 Neg.getOperand(0).getOpcode() == ISD::Constant &&
2069 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
2070 Neg.getOperand(1) == Pos &&
2071 (Pos == CmpOp ||
2072 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
2073 Pos.getOperand(0) == CmpOp)));
2074}
2075
2076// Return the absolute or negative absolute of Op; IsNegative decides which.
2077static SDValue getAbsolute(SelectionDAG &DAG, SDLoc DL, SDValue Op,
2078 bool IsNegative) {
2079 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
2080 if (IsNegative)
2081 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002082 DAG.getConstant(0, DL, Op.getValueType()), Op);
Richard Sandiford57485472013-12-13 15:35:00 +00002083 return Op;
2084}
2085
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002086SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
2087 SelectionDAG &DAG) const {
2088 SDValue CmpOp0 = Op.getOperand(0);
2089 SDValue CmpOp1 = Op.getOperand(1);
2090 SDValue TrueOp = Op.getOperand(2);
2091 SDValue FalseOp = Op.getOperand(3);
2092 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002093 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002094
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002095 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandiford57485472013-12-13 15:35:00 +00002096
2097 // Check for absolute and negative-absolute selections, including those
2098 // where the comparison value is sign-extended (for LPGFR and LNGFR).
2099 // This check supplements the one in DAGCombiner.
2100 if (C.Opcode == SystemZISD::ICMP &&
2101 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
2102 C.CCMask != SystemZ::CCMASK_CMP_NE &&
2103 C.Op1.getOpcode() == ISD::Constant &&
2104 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
2105 if (isAbsolute(C.Op0, TrueOp, FalseOp))
2106 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
2107 if (isAbsolute(C.Op0, FalseOp, TrueOp))
2108 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
2109 }
2110
Richard Sandifordd420f732013-12-13 15:28:45 +00002111 SDValue Glue = emitCmp(DAG, DL, C);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002112
2113 // Special case for handling -1/0 results. The shifts we use here
2114 // should get optimized with the IPM conversion sequence.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002115 auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
2116 auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002117 if (TrueC && FalseC) {
2118 int64_t TrueVal = TrueC->getSExtValue();
2119 int64_t FalseVal = FalseC->getSExtValue();
2120 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
2121 // Invert the condition if we want -1 on false.
2122 if (TrueVal == 0)
Richard Sandifordd420f732013-12-13 15:28:45 +00002123 C.CCMask ^= C.CCValid;
2124 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002125 EVT VT = Op.getValueType();
2126 // Extend the result to VT. Upper bits are ignored.
2127 if (!is32Bit(VT))
2128 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
2129 // Sign-extend from the low bit.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002130 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002131 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
2132 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
2133 }
2134 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002135
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002136 SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, DL, MVT::i32),
2137 DAG.getConstant(C.CCMask, DL, MVT::i32), Glue};
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002138
2139 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00002140 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002141}
2142
2143SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
2144 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002145 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002146 const GlobalValue *GV = Node->getGlobal();
2147 int64_t Offset = Node->getOffset();
2148 EVT PtrVT = getPointerTy();
Eric Christopher93bf97c2014-06-27 07:38:01 +00002149 Reloc::Model RM = DAG.getTarget().getRelocationModel();
2150 CodeModel::Model CM = DAG.getTarget().getCodeModel();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002151
2152 SDValue Result;
2153 if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) {
Richard Sandiford54b36912013-09-27 15:14:04 +00002154 // Assign anchors at 1<<12 byte boundaries.
2155 uint64_t Anchor = Offset & ~uint64_t(0xfff);
2156 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
2157 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2158
2159 // The offset can be folded into the address if it is aligned to a halfword.
2160 Offset -= Anchor;
2161 if (Offset != 0 && (Offset & 1) == 0) {
2162 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
2163 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002164 Offset = 0;
2165 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002166 } else {
2167 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
2168 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2169 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
2170 MachinePointerInfo::getGOT(), false, false, false, 0);
2171 }
2172
2173 // If there was a non-zero offset that we didn't fold, create an explicit
2174 // addition for it.
2175 if (Offset != 0)
2176 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002177 DAG.getConstant(Offset, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002178
2179 return Result;
2180}
2181
Ulrich Weigand7db69182015-02-18 09:13:27 +00002182SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
2183 SelectionDAG &DAG,
2184 unsigned Opcode,
2185 SDValue GOTOffset) const {
2186 SDLoc DL(Node);
2187 EVT PtrVT = getPointerTy();
2188 SDValue Chain = DAG.getEntryNode();
2189 SDValue Glue;
2190
2191 // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
2192 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
2193 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
2194 Glue = Chain.getValue(1);
2195 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
2196 Glue = Chain.getValue(1);
2197
2198 // The first call operand is the chain and the second is the TLS symbol.
2199 SmallVector<SDValue, 8> Ops;
2200 Ops.push_back(Chain);
2201 Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
2202 Node->getValueType(0),
2203 0, 0));
2204
2205 // Add argument registers to the end of the list so that they are
2206 // known live into the call.
2207 Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
2208 Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
2209
2210 // Add a register mask operand representing the call-preserved registers.
2211 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002212 const uint32_t *Mask =
2213 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002214 assert(Mask && "Missing call preserved mask for calling convention");
2215 Ops.push_back(DAG.getRegisterMask(Mask));
2216
2217 // Glue the call to the argument copies.
2218 Ops.push_back(Glue);
2219
2220 // Emit the call.
2221 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2222 Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
2223 Glue = Chain.getValue(1);
2224
2225 // Copy the return value from %r2.
2226 return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
2227}
2228
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002229SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
2230 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002231 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002232 const GlobalValue *GV = Node->getGlobal();
2233 EVT PtrVT = getPointerTy();
Eric Christopher93bf97c2014-06-27 07:38:01 +00002234 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002235
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002236 // The high part of the thread pointer is in access register 0.
2237 SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002238 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002239 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
2240
2241 // The low part of the thread pointer is in access register 1.
2242 SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002243 DAG.getConstant(1, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002244 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
2245
2246 // Merge them into a single 64-bit address.
2247 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002248 DAG.getConstant(32, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002249 SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
2250
Ulrich Weigand7db69182015-02-18 09:13:27 +00002251 // Get the offset of GA from the thread pointer, based on the TLS model.
2252 SDValue Offset;
2253 switch (model) {
2254 case TLSModel::GeneralDynamic: {
2255 // Load the GOT offset of the tls_index (module ID / per-symbol offset).
2256 SystemZConstantPoolValue *CPV =
2257 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002258
Ulrich Weigand7db69182015-02-18 09:13:27 +00002259 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
2260 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
2261 Offset, MachinePointerInfo::getConstantPool(),
2262 false, false, false, 0);
2263
2264 // Call __tls_get_offset to retrieve the offset.
2265 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
2266 break;
2267 }
2268
2269 case TLSModel::LocalDynamic: {
2270 // Load the GOT offset of the module ID.
2271 SystemZConstantPoolValue *CPV =
2272 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
2273
2274 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
2275 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
2276 Offset, MachinePointerInfo::getConstantPool(),
2277 false, false, false, 0);
2278
2279 // Call __tls_get_offset to retrieve the module base offset.
2280 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
2281
2282 // Note: The SystemZLDCleanupPass will remove redundant computations
2283 // of the module base offset. Count total number of local-dynamic
2284 // accesses to trigger execution of that pass.
2285 SystemZMachineFunctionInfo* MFI =
2286 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
2287 MFI->incNumLocalDynamicTLSAccesses();
2288
2289 // Add the per-symbol offset.
2290 CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
2291
2292 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8);
2293 DTPOffset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
2294 DTPOffset, MachinePointerInfo::getConstantPool(),
2295 false, false, false, 0);
2296
2297 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
2298 break;
2299 }
2300
2301 case TLSModel::InitialExec: {
2302 // Load the offset from the GOT.
2303 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2304 SystemZII::MO_INDNTPOFF);
2305 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
2306 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
2307 Offset, MachinePointerInfo::getGOT(),
2308 false, false, false, 0);
2309 break;
2310 }
2311
2312 case TLSModel::LocalExec: {
2313 // Force the offset into the constant pool and load it from there.
2314 SystemZConstantPoolValue *CPV =
2315 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
2316
2317 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
2318 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
2319 Offset, MachinePointerInfo::getConstantPool(),
2320 false, false, false, 0);
2321 break;
Ulrich Weigandb7e59092015-02-18 09:42:23 +00002322 }
Ulrich Weigand7db69182015-02-18 09:13:27 +00002323 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002324
2325 // Add the base and offset together.
2326 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
2327}
2328
2329SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
2330 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002331 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002332 const BlockAddress *BA = Node->getBlockAddress();
2333 int64_t Offset = Node->getOffset();
2334 EVT PtrVT = getPointerTy();
2335
2336 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
2337 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2338 return Result;
2339}
2340
2341SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
2342 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002343 SDLoc DL(JT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002344 EVT PtrVT = getPointerTy();
2345 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
2346
2347 // Use LARL to load the address of the table.
2348 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2349}
2350
2351SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
2352 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002353 SDLoc DL(CP);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002354 EVT PtrVT = getPointerTy();
2355
2356 SDValue Result;
2357 if (CP->isMachineConstantPoolEntry())
2358 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2359 CP->getAlignment());
2360 else
2361 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2362 CP->getAlignment(), CP->getOffset());
2363
2364 // Use LARL to load the address of the constant pool entry.
2365 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2366}
2367
2368SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
2369 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002370 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002371 SDValue In = Op.getOperand(0);
2372 EVT InVT = In.getValueType();
2373 EVT ResVT = Op.getValueType();
2374
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002375 // Convert loads directly. This is normally done by DAGCombiner,
2376 // but we need this case for bitcasts that are created during lowering
2377 // and which are then lowered themselves.
2378 if (auto *LoadN = dyn_cast<LoadSDNode>(In))
2379 return DAG.getLoad(ResVT, DL, LoadN->getChain(), LoadN->getBasePtr(),
2380 LoadN->getMemOperand());
2381
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002382 if (InVT == MVT::i32 && ResVT == MVT::f32) {
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002383 SDValue In64;
2384 if (Subtarget.hasHighWord()) {
2385 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
2386 MVT::i64);
2387 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
2388 MVT::i64, SDValue(U64, 0), In);
2389 } else {
2390 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
2391 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002392 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002393 }
2394 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002395 return DAG.getTargetExtractSubreg(SystemZ::subreg_r32,
Richard Sandifordd8163202013-09-13 09:12:44 +00002396 DL, MVT::f32, Out64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002397 }
2398 if (InVT == MVT::f32 && ResVT == MVT::i32) {
2399 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002400 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002401 MVT::f64, SDValue(U64, 0), In);
2402 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002403 if (Subtarget.hasHighWord())
2404 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
2405 MVT::i32, Out64);
2406 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002407 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002408 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002409 }
2410 llvm_unreachable("Unexpected bitcast combination");
2411}
2412
2413SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
2414 SelectionDAG &DAG) const {
2415 MachineFunction &MF = DAG.getMachineFunction();
2416 SystemZMachineFunctionInfo *FuncInfo =
2417 MF.getInfo<SystemZMachineFunctionInfo>();
2418 EVT PtrVT = getPointerTy();
2419
2420 SDValue Chain = Op.getOperand(0);
2421 SDValue Addr = Op.getOperand(1);
2422 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002423 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002424
2425 // The initial values of each field.
2426 const unsigned NumFields = 4;
2427 SDValue Fields[NumFields] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002428 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT),
2429 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002430 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
2431 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
2432 };
2433
2434 // Store each field into its respective slot.
2435 SDValue MemOps[NumFields];
2436 unsigned Offset = 0;
2437 for (unsigned I = 0; I < NumFields; ++I) {
2438 SDValue FieldAddr = Addr;
2439 if (Offset != 0)
2440 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002441 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002442 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
2443 MachinePointerInfo(SV, Offset),
2444 false, false, 0);
2445 Offset += 8;
2446 }
Craig Topper48d114b2014-04-26 18:35:24 +00002447 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002448}
2449
2450SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
2451 SelectionDAG &DAG) const {
2452 SDValue Chain = Op.getOperand(0);
2453 SDValue DstPtr = Op.getOperand(1);
2454 SDValue SrcPtr = Op.getOperand(2);
2455 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
2456 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002457 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002458
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002459 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002460 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00002461 /*isTailCall*/false,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002462 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
2463}
2464
2465SDValue SystemZTargetLowering::
2466lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
2467 SDValue Chain = Op.getOperand(0);
2468 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002469 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002470
2471 unsigned SPReg = getStackPointerRegisterToSaveRestore();
2472
2473 // Get a reference to the stack pointer.
2474 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
2475
2476 // Get the new stack pointer value.
2477 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, Size);
2478
2479 // Copy the new stack pointer back.
2480 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
2481
2482 // The allocated data lives above the 160 bytes allocated for the standard
2483 // frame, plus any outgoing stack arguments. We don't know how much that
2484 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
2485 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
2486 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
2487
2488 SDValue Ops[2] = { Result, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002489 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002490}
2491
Richard Sandiford7d86e472013-08-21 09:34:56 +00002492SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
2493 SelectionDAG &DAG) const {
2494 EVT VT = Op.getValueType();
2495 SDLoc DL(Op);
2496 SDValue Ops[2];
2497 if (is32Bit(VT))
2498 // Just do a normal 64-bit multiplication and extract the results.
2499 // We define this so that it can be used for constant division.
2500 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
2501 Op.getOperand(1), Ops[1], Ops[0]);
2502 else {
2503 // Do a full 128-bit multiplication based on UMUL_LOHI64:
2504 //
2505 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
2506 //
2507 // but using the fact that the upper halves are either all zeros
2508 // or all ones:
2509 //
2510 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
2511 //
2512 // and grouping the right terms together since they are quicker than the
2513 // multiplication:
2514 //
2515 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002516 SDValue C63 = DAG.getConstant(63, DL, MVT::i64);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002517 SDValue LL = Op.getOperand(0);
2518 SDValue RL = Op.getOperand(1);
2519 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
2520 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
2521 // UMUL_LOHI64 returns the low result in the odd register and the high
2522 // result in the even register. SMUL_LOHI is defined to return the
2523 // low half first, so the results are in reverse order.
2524 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2525 LL, RL, Ops[1], Ops[0]);
2526 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
2527 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
2528 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
2529 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
2530 }
Craig Topper64941d92014-04-27 19:20:57 +00002531 return DAG.getMergeValues(Ops, DL);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002532}
2533
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002534SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
2535 SelectionDAG &DAG) const {
2536 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002537 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002538 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00002539 if (is32Bit(VT))
2540 // Just do a normal 64-bit multiplication and extract the results.
2541 // We define this so that it can be used for constant division.
2542 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
2543 Op.getOperand(1), Ops[1], Ops[0]);
2544 else
2545 // UMUL_LOHI64 returns the low result in the odd register and the high
2546 // result in the even register. UMUL_LOHI is defined to return the
2547 // low half first, so the results are in reverse order.
2548 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2549 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002550 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002551}
2552
2553SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
2554 SelectionDAG &DAG) const {
2555 SDValue Op0 = Op.getOperand(0);
2556 SDValue Op1 = Op.getOperand(1);
2557 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002558 SDLoc DL(Op);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002559 unsigned Opcode;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002560
2561 // We use DSGF for 32-bit division.
2562 if (is32Bit(VT)) {
2563 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002564 Opcode = SystemZISD::SDIVREM32;
2565 } else if (DAG.ComputeNumSignBits(Op1) > 32) {
2566 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
2567 Opcode = SystemZISD::SDIVREM32;
2568 } else
2569 Opcode = SystemZISD::SDIVREM64;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002570
2571 // DSG(F) takes a 64-bit dividend, so the even register in the GR128
2572 // input is "don't care". The instruction returns the remainder in
2573 // the even register and the quotient in the odd register.
2574 SDValue Ops[2];
Richard Sandiforde6e78852013-07-02 15:40:22 +00002575 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002576 Op0, Op1, Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002577 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002578}
2579
2580SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
2581 SelectionDAG &DAG) const {
2582 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002583 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002584
2585 // DL(G) uses a double-width dividend, so we need to clear the even
2586 // register in the GR128 input. The instruction returns the remainder
2587 // in the even register and the quotient in the odd register.
2588 SDValue Ops[2];
2589 if (is32Bit(VT))
2590 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
2591 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2592 else
2593 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
2594 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002595 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002596}
2597
2598SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
2599 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
2600
2601 // Get the known-zero masks for each operand.
2602 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
2603 APInt KnownZero[2], KnownOne[2];
Jay Foada0653a32014-05-14 21:14:37 +00002604 DAG.computeKnownBits(Ops[0], KnownZero[0], KnownOne[0]);
2605 DAG.computeKnownBits(Ops[1], KnownZero[1], KnownOne[1]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002606
2607 // See if the upper 32 bits of one operand and the lower 32 bits of the
2608 // other are known zero. They are the low and high operands respectively.
2609 uint64_t Masks[] = { KnownZero[0].getZExtValue(),
2610 KnownZero[1].getZExtValue() };
2611 unsigned High, Low;
2612 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
2613 High = 1, Low = 0;
2614 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
2615 High = 0, Low = 1;
2616 else
2617 return Op;
2618
2619 SDValue LowOp = Ops[Low];
2620 SDValue HighOp = Ops[High];
2621
2622 // If the high part is a constant, we're better off using IILH.
2623 if (HighOp.getOpcode() == ISD::Constant)
2624 return Op;
2625
2626 // If the low part is a constant that is outside the range of LHI,
2627 // then we're better off using IILF.
2628 if (LowOp.getOpcode() == ISD::Constant) {
2629 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
2630 if (!isInt<16>(Value))
2631 return Op;
2632 }
2633
2634 // Check whether the high part is an AND that doesn't change the
2635 // high 32 bits and just masks out low bits. We can skip it if so.
2636 if (HighOp.getOpcode() == ISD::AND &&
2637 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
Richard Sandifordccc2a7c2013-12-03 11:01:54 +00002638 SDValue HighOp0 = HighOp.getOperand(0);
2639 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
2640 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
2641 HighOp = HighOp0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002642 }
2643
2644 // Take advantage of the fact that all GR32 operations only change the
2645 // low 32 bits by truncating Low to an i32 and inserting it directly
2646 // using a subreg. The interesting cases are those where the truncation
2647 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002648 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002649 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
Richard Sandiford87a44362013-09-30 10:28:35 +00002650 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002651 MVT::i64, HighOp, Low32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002652}
2653
Ulrich Weigandb4012182015-03-31 12:56:33 +00002654SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
2655 SelectionDAG &DAG) const {
2656 EVT VT = Op.getValueType();
Ulrich Weigandb4012182015-03-31 12:56:33 +00002657 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002658 Op = Op.getOperand(0);
2659
2660 // Handle vector types via VPOPCT.
2661 if (VT.isVector()) {
2662 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op);
2663 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op);
2664 switch (VT.getVectorElementType().getSizeInBits()) {
2665 case 8:
2666 break;
2667 case 16: {
2668 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
2669 SDValue Shift = DAG.getConstant(8, DL, MVT::i32);
2670 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift);
2671 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
2672 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift);
2673 break;
2674 }
2675 case 32: {
2676 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2677 DAG.getConstant(0, DL, MVT::i32));
2678 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
2679 break;
2680 }
2681 case 64: {
2682 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2683 DAG.getConstant(0, DL, MVT::i32));
2684 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp);
2685 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
2686 break;
2687 }
2688 default:
2689 llvm_unreachable("Unexpected type");
2690 }
2691 return Op;
2692 }
Ulrich Weigandb4012182015-03-31 12:56:33 +00002693
2694 // Get the known-zero mask for the operand.
Ulrich Weigandb4012182015-03-31 12:56:33 +00002695 APInt KnownZero, KnownOne;
2696 DAG.computeKnownBits(Op, KnownZero, KnownOne);
Ulrich Weigand050527b2015-03-31 19:28:50 +00002697 unsigned NumSignificantBits = (~KnownZero).getActiveBits();
2698 if (NumSignificantBits == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002699 return DAG.getConstant(0, DL, VT);
Ulrich Weigandb4012182015-03-31 12:56:33 +00002700
2701 // Skip known-zero high parts of the operand.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002702 int64_t OrigBitSize = VT.getSizeInBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00002703 int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits);
2704 BitSize = std::min(BitSize, OrigBitSize);
Ulrich Weigandb4012182015-03-31 12:56:33 +00002705
2706 // The POPCNT instruction counts the number of bits in each byte.
2707 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op);
2708 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op);
2709 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
2710
2711 // Add up per-byte counts in a binary tree. All bits of Op at
2712 // position larger than BitSize remain zero throughout.
2713 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002714 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00002715 if (BitSize != OrigBitSize)
2716 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002717 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00002718 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
2719 }
2720
2721 // Extract overall result from high byte.
2722 if (BitSize > 8)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002723 Op = DAG.getNode(ISD::SRL, DL, VT, Op,
2724 DAG.getConstant(BitSize - 8, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00002725
2726 return Op;
2727}
2728
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002729// Op is an atomic load. Lower it into a normal volatile load.
2730SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
2731 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002732 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002733 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
2734 Node->getChain(), Node->getBasePtr(),
2735 Node->getMemoryVT(), Node->getMemOperand());
2736}
2737
2738// Op is an atomic store. Lower it into a normal volatile store followed
2739// by a serialization.
2740SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
2741 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002742 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002743 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
2744 Node->getBasePtr(), Node->getMemoryVT(),
2745 Node->getMemOperand());
2746 return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other,
2747 Chain), 0);
2748}
2749
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002750// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
2751// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002752SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
2753 SelectionDAG &DAG,
2754 unsigned Opcode) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002755 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002756
2757 // 32-bit operations need no code outside the main loop.
2758 EVT NarrowVT = Node->getMemoryVT();
2759 EVT WideVT = MVT::i32;
2760 if (NarrowVT == WideVT)
2761 return Op;
2762
2763 int64_t BitSize = NarrowVT.getSizeInBits();
2764 SDValue ChainIn = Node->getChain();
2765 SDValue Addr = Node->getBasePtr();
2766 SDValue Src2 = Node->getVal();
2767 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002768 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002769 EVT PtrVT = Addr.getValueType();
2770
2771 // Convert atomic subtracts of constants into additions.
2772 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
Richard Sandiford21f5d682014-03-06 11:22:58 +00002773 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002774 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002775 Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002776 }
2777
2778 // Get the address of the containing word.
2779 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002780 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002781
2782 // Get the number of bits that the word must be rotated left in order
2783 // to bring the field to the top bits of a GR32.
2784 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002785 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002786 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
2787
2788 // Get the complementing shift amount, for rotating a field in the top
2789 // bits back to its proper position.
2790 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002791 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002792
2793 // Extend the source operand to 32 bits and prepare it for the inner loop.
2794 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
2795 // operations require the source to be shifted in advance. (This shift
2796 // can be folded if the source is constant.) For AND and NAND, the lower
2797 // bits must be set, while for other opcodes they should be left clear.
2798 if (Opcode != SystemZISD::ATOMIC_SWAPW)
2799 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002800 DAG.getConstant(32 - BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002801 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
2802 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
2803 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002804 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002805
2806 // Construct the ATOMIC_LOADW_* node.
2807 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
2808 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002809 DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002810 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002811 NarrowVT, MMO);
2812
2813 // Rotate the result of the final CS so that the field is in the lower
2814 // bits of a GR32, then truncate it.
2815 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002816 DAG.getConstant(BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002817 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
2818
2819 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002820 return DAG.getMergeValues(RetOps, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002821}
2822
Richard Sandiford41350a52013-12-24 15:18:04 +00002823// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
Richard Sandiford002019a2013-12-24 15:22:39 +00002824// into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
Richard Sandiford41350a52013-12-24 15:18:04 +00002825// operations into additions.
2826SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
2827 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002828 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandiford41350a52013-12-24 15:18:04 +00002829 EVT MemVT = Node->getMemoryVT();
2830 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
2831 // A full-width operation.
2832 assert(Op.getValueType() == MemVT && "Mismatched VTs");
2833 SDValue Src2 = Node->getVal();
2834 SDValue NegSrc2;
2835 SDLoc DL(Src2);
2836
Richard Sandiford21f5d682014-03-06 11:22:58 +00002837 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
Richard Sandiford41350a52013-12-24 15:18:04 +00002838 // Use an addition if the operand is constant and either LAA(G) is
2839 // available or the negative value is in the range of A(G)FHI.
2840 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
Eric Christopher93bf97c2014-06-27 07:38:01 +00002841 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002842 NegSrc2 = DAG.getConstant(Value, DL, MemVT);
Eric Christopher93bf97c2014-06-27 07:38:01 +00002843 } else if (Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00002844 // Use LAA(G) if available.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002845 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT),
Richard Sandiford41350a52013-12-24 15:18:04 +00002846 Src2);
2847
2848 if (NegSrc2.getNode())
2849 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
2850 Node->getChain(), Node->getBasePtr(), NegSrc2,
2851 Node->getMemOperand(), Node->getOrdering(),
2852 Node->getSynchScope());
2853
2854 // Use the node as-is.
2855 return Op;
2856 }
2857
2858 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
2859}
2860
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002861// Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
2862// into a fullword ATOMIC_CMP_SWAPW operation.
2863SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
2864 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002865 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002866
2867 // We have native support for 32-bit compare and swap.
2868 EVT NarrowVT = Node->getMemoryVT();
2869 EVT WideVT = MVT::i32;
2870 if (NarrowVT == WideVT)
2871 return Op;
2872
2873 int64_t BitSize = NarrowVT.getSizeInBits();
2874 SDValue ChainIn = Node->getOperand(0);
2875 SDValue Addr = Node->getOperand(1);
2876 SDValue CmpVal = Node->getOperand(2);
2877 SDValue SwapVal = Node->getOperand(3);
2878 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002879 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002880 EVT PtrVT = Addr.getValueType();
2881
2882 // Get the address of the containing word.
2883 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002884 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002885
2886 // Get the number of bits that the word must be rotated left in order
2887 // to bring the field to the top bits of a GR32.
2888 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002889 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002890 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
2891
2892 // Get the complementing shift amount, for rotating a field in the top
2893 // bits back to its proper position.
2894 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002895 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002896
2897 // Construct the ATOMIC_CMP_SWAPW node.
2898 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
2899 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002900 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002901 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00002902 VTList, Ops, NarrowVT, MMO);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002903 return AtomicOp;
2904}
2905
2906SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
2907 SelectionDAG &DAG) const {
2908 MachineFunction &MF = DAG.getMachineFunction();
2909 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002910 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002911 SystemZ::R15D, Op.getValueType());
2912}
2913
2914SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
2915 SelectionDAG &DAG) const {
2916 MachineFunction &MF = DAG.getMachineFunction();
2917 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002918 return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002919 SystemZ::R15D, Op.getOperand(1));
2920}
2921
Richard Sandiford03481332013-08-23 11:36:42 +00002922SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
2923 SelectionDAG &DAG) const {
2924 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2925 if (!IsData)
2926 // Just preserve the chain.
2927 return Op.getOperand(0);
2928
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002929 SDLoc DL(Op);
Richard Sandiford03481332013-08-23 11:36:42 +00002930 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2931 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
Richard Sandiford21f5d682014-03-06 11:22:58 +00002932 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
Richard Sandiford03481332013-08-23 11:36:42 +00002933 SDValue Ops[] = {
2934 Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002935 DAG.getConstant(Code, DL, MVT::i32),
Richard Sandiford03481332013-08-23 11:36:42 +00002936 Op.getOperand(1)
2937 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002938 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00002939 Node->getVTList(), Ops,
Richard Sandiford03481332013-08-23 11:36:42 +00002940 Node->getMemoryVT(), Node->getMemOperand());
2941}
2942
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002943// Return an i32 that contains the value of CC immediately after After,
2944// whose final operand must be MVT::Glue.
2945static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002946 SDLoc DL(After);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002947 SDValue Glue = SDValue(After, After->getNumValues() - 1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002948 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
2949 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
2950 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002951}
2952
2953SDValue
2954SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
2955 SelectionDAG &DAG) const {
2956 unsigned Opcode, CCValid;
2957 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) {
2958 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
2959 SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode);
2960 SDValue CC = getCCResult(DAG, Glued.getNode());
2961 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC);
2962 return SDValue();
2963 }
2964
2965 return SDValue();
2966}
2967
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002968namespace {
2969// Says that SystemZISD operation Opcode can be used to perform the equivalent
2970// of a VPERM with permute vector Bytes. If Opcode takes three operands,
2971// Operand is the constant third operand, otherwise it is the number of
2972// bytes in each element of the result.
2973struct Permute {
2974 unsigned Opcode;
2975 unsigned Operand;
2976 unsigned char Bytes[SystemZ::VectorBytes];
2977};
2978}
2979
2980static const Permute PermuteForms[] = {
2981 // VMRHG
2982 { SystemZISD::MERGE_HIGH, 8,
2983 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } },
2984 // VMRHF
2985 { SystemZISD::MERGE_HIGH, 4,
2986 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
2987 // VMRHH
2988 { SystemZISD::MERGE_HIGH, 2,
2989 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
2990 // VMRHB
2991 { SystemZISD::MERGE_HIGH, 1,
2992 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
2993 // VMRLG
2994 { SystemZISD::MERGE_LOW, 8,
2995 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } },
2996 // VMRLF
2997 { SystemZISD::MERGE_LOW, 4,
2998 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
2999 // VMRLH
3000 { SystemZISD::MERGE_LOW, 2,
3001 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
3002 // VMRLB
3003 { SystemZISD::MERGE_LOW, 1,
3004 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
3005 // VPKG
3006 { SystemZISD::PACK, 4,
3007 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } },
3008 // VPKF
3009 { SystemZISD::PACK, 2,
3010 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
3011 // VPKH
3012 { SystemZISD::PACK, 1,
3013 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
3014 // VPDI V1, V2, 4 (low half of V1, high half of V2)
3015 { SystemZISD::PERMUTE_DWORDS, 4,
3016 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } },
3017 // VPDI V1, V2, 1 (high half of V1, low half of V2)
3018 { SystemZISD::PERMUTE_DWORDS, 1,
3019 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } }
3020};
3021
3022// Called after matching a vector shuffle against a particular pattern.
3023// Both the original shuffle and the pattern have two vector operands.
3024// OpNos[0] is the operand of the original shuffle that should be used for
3025// operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything.
3026// OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and
3027// set OpNo0 and OpNo1 to the shuffle operands that should actually be used
3028// for operands 0 and 1 of the pattern.
3029static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) {
3030 if (OpNos[0] < 0) {
3031 if (OpNos[1] < 0)
3032 return false;
3033 OpNo0 = OpNo1 = OpNos[1];
3034 } else if (OpNos[1] < 0) {
3035 OpNo0 = OpNo1 = OpNos[0];
3036 } else {
3037 OpNo0 = OpNos[0];
3038 OpNo1 = OpNos[1];
3039 }
3040 return true;
3041}
3042
3043// Bytes is a VPERM-like permute vector, except that -1 is used for
3044// undefined bytes. Return true if the VPERM can be implemented using P.
3045// When returning true set OpNo0 to the VPERM operand that should be
3046// used for operand 0 of P and likewise OpNo1 for operand 1 of P.
3047//
3048// For example, if swapping the VPERM operands allows P to match, OpNo0
3049// will be 1 and OpNo1 will be 0. If instead Bytes only refers to one
3050// operand, but rewriting it to use two duplicated operands allows it to
3051// match P, then OpNo0 and OpNo1 will be the same.
3052static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P,
3053 unsigned &OpNo0, unsigned &OpNo1) {
3054 int OpNos[] = { -1, -1 };
3055 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
3056 int Elt = Bytes[I];
3057 if (Elt >= 0) {
3058 // Make sure that the two permute vectors use the same suboperand
3059 // byte number. Only the operand numbers (the high bits) are
3060 // allowed to differ.
3061 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1))
3062 return false;
3063 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes;
3064 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes;
3065 // Make sure that the operand mappings are consistent with previous
3066 // elements.
3067 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3068 return false;
3069 OpNos[ModelOpNo] = RealOpNo;
3070 }
3071 }
3072 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3073}
3074
3075// As above, but search for a matching permute.
3076static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes,
3077 unsigned &OpNo0, unsigned &OpNo1) {
3078 for (auto &P : PermuteForms)
3079 if (matchPermute(Bytes, P, OpNo0, OpNo1))
3080 return &P;
3081 return nullptr;
3082}
3083
3084// Bytes is a VPERM-like permute vector, except that -1 is used for
3085// undefined bytes. This permute is an operand of an outer permute.
3086// See whether redistributing the -1 bytes gives a shuffle that can be
3087// implemented using P. If so, set Transform to a VPERM-like permute vector
3088// that, when applied to the result of P, gives the original permute in Bytes.
3089static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3090 const Permute &P,
3091 SmallVectorImpl<int> &Transform) {
3092 unsigned To = 0;
3093 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) {
3094 int Elt = Bytes[From];
3095 if (Elt < 0)
3096 // Byte number From of the result is undefined.
3097 Transform[From] = -1;
3098 else {
3099 while (P.Bytes[To] != Elt) {
3100 To += 1;
3101 if (To == SystemZ::VectorBytes)
3102 return false;
3103 }
3104 Transform[From] = To;
3105 }
3106 }
3107 return true;
3108}
3109
3110// As above, but search for a matching permute.
3111static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3112 SmallVectorImpl<int> &Transform) {
3113 for (auto &P : PermuteForms)
3114 if (matchDoublePermute(Bytes, P, Transform))
3115 return &P;
3116 return nullptr;
3117}
3118
3119// Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask,
3120// as if it had type vNi8.
3121static void getVPermMask(ShuffleVectorSDNode *VSN,
3122 SmallVectorImpl<int> &Bytes) {
3123 EVT VT = VSN->getValueType(0);
3124 unsigned NumElements = VT.getVectorNumElements();
3125 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3126 Bytes.resize(NumElements * BytesPerElement, -1);
3127 for (unsigned I = 0; I < NumElements; ++I) {
3128 int Index = VSN->getMaskElt(I);
3129 if (Index >= 0)
3130 for (unsigned J = 0; J < BytesPerElement; ++J)
3131 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
3132 }
3133}
3134
3135// Bytes is a VPERM-like permute vector, except that -1 is used for
3136// undefined bytes. See whether bytes [Start, Start + BytesPerElement) of
3137// the result come from a contiguous sequence of bytes from one input.
3138// Set Base to the selector for the first byte if so.
3139static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start,
3140 unsigned BytesPerElement, int &Base) {
3141 Base = -1;
3142 for (unsigned I = 0; I < BytesPerElement; ++I) {
3143 if (Bytes[Start + I] >= 0) {
3144 unsigned Elem = Bytes[Start + I];
3145 if (Base < 0) {
3146 Base = Elem - I;
3147 // Make sure the bytes would come from one input operand.
3148 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size())
3149 return false;
3150 } else if (unsigned(Base) != Elem - I)
3151 return false;
3152 }
3153 }
3154 return true;
3155}
3156
3157// Bytes is a VPERM-like permute vector, except that -1 is used for
3158// undefined bytes. Return true if it can be performed using VSLDI.
3159// When returning true, set StartIndex to the shift amount and OpNo0
3160// and OpNo1 to the VPERM operands that should be used as the first
3161// and second shift operand respectively.
3162static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes,
3163 unsigned &StartIndex, unsigned &OpNo0,
3164 unsigned &OpNo1) {
3165 int OpNos[] = { -1, -1 };
3166 int Shift = -1;
3167 for (unsigned I = 0; I < 16; ++I) {
3168 int Index = Bytes[I];
3169 if (Index >= 0) {
3170 int ExpectedShift = (Index - I) % SystemZ::VectorBytes;
3171 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes;
3172 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes;
3173 if (Shift < 0)
3174 Shift = ExpectedShift;
3175 else if (Shift != ExpectedShift)
3176 return false;
3177 // Make sure that the operand mappings are consistent with previous
3178 // elements.
3179 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3180 return false;
3181 OpNos[ModelOpNo] = RealOpNo;
3182 }
3183 }
3184 StartIndex = Shift;
3185 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3186}
3187
3188// Create a node that performs P on operands Op0 and Op1, casting the
3189// operands to the appropriate type. The type of the result is determined by P.
3190static SDValue getPermuteNode(SelectionDAG &DAG, SDLoc DL,
3191 const Permute &P, SDValue Op0, SDValue Op1) {
3192 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input
3193 // elements of a PACK are twice as wide as the outputs.
3194 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 :
3195 P.Opcode == SystemZISD::PACK ? P.Operand * 2 :
3196 P.Operand);
3197 // Cast both operands to the appropriate type.
3198 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8),
3199 SystemZ::VectorBytes / InBytes);
3200 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0);
3201 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1);
3202 SDValue Op;
3203 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) {
3204 SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32);
3205 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2);
3206 } else if (P.Opcode == SystemZISD::PACK) {
3207 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8),
3208 SystemZ::VectorBytes / P.Operand);
3209 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1);
3210 } else {
3211 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1);
3212 }
3213 return Op;
3214}
3215
3216// Bytes is a VPERM-like permute vector, except that -1 is used for
3217// undefined bytes. Implement it on operands Ops[0] and Ops[1] using
3218// VSLDI or VPERM.
3219static SDValue getGeneralPermuteNode(SelectionDAG &DAG, SDLoc DL, SDValue *Ops,
3220 const SmallVectorImpl<int> &Bytes) {
3221 for (unsigned I = 0; I < 2; ++I)
3222 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]);
3223
3224 // First see whether VSLDI can be used.
3225 unsigned StartIndex, OpNo0, OpNo1;
3226 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1))
3227 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0],
3228 Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32));
3229
3230 // Fall back on VPERM. Construct an SDNode for the permute vector.
3231 SDValue IndexNodes[SystemZ::VectorBytes];
3232 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3233 if (Bytes[I] >= 0)
3234 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32);
3235 else
3236 IndexNodes[I] = DAG.getUNDEF(MVT::i32);
3237 SDValue Op2 = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v16i8, IndexNodes);
3238 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2);
3239}
3240
3241namespace {
3242// Describes a general N-operand vector shuffle.
3243struct GeneralShuffle {
3244 GeneralShuffle(EVT vt) : VT(vt) {}
3245 void addUndef();
3246 void add(SDValue, unsigned);
3247 SDValue getNode(SelectionDAG &, SDLoc);
3248
3249 // The operands of the shuffle.
3250 SmallVector<SDValue, SystemZ::VectorBytes> Ops;
3251
3252 // Index I is -1 if byte I of the result is undefined. Otherwise the
3253 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand
3254 // Bytes[I] / SystemZ::VectorBytes.
3255 SmallVector<int, SystemZ::VectorBytes> Bytes;
3256
3257 // The type of the shuffle result.
3258 EVT VT;
3259};
3260}
3261
3262// Add an extra undefined element to the shuffle.
3263void GeneralShuffle::addUndef() {
3264 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3265 for (unsigned I = 0; I < BytesPerElement; ++I)
3266 Bytes.push_back(-1);
3267}
3268
3269// Add an extra element to the shuffle, taking it from element Elem of Op.
3270// A null Op indicates a vector input whose value will be calculated later;
3271// there is at most one such input per shuffle and it always has the same
3272// type as the result.
3273void GeneralShuffle::add(SDValue Op, unsigned Elem) {
3274 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3275
3276 // The source vector can have wider elements than the result,
3277 // either through an explicit TRUNCATE or because of type legalization.
3278 // We want the least significant part.
3279 EVT FromVT = Op.getNode() ? Op.getValueType() : VT;
3280 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize();
3281 assert(FromBytesPerElement >= BytesPerElement &&
3282 "Invalid EXTRACT_VECTOR_ELT");
3283 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes +
3284 (FromBytesPerElement - BytesPerElement));
3285
3286 // Look through things like shuffles and bitcasts.
3287 while (Op.getNode()) {
3288 if (Op.getOpcode() == ISD::BITCAST)
3289 Op = Op.getOperand(0);
3290 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) {
3291 // See whether the bytes we need come from a contiguous part of one
3292 // operand.
3293 SmallVector<int, SystemZ::VectorBytes> OpBytes;
3294 getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes);
3295 int NewByte;
3296 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte))
3297 break;
3298 if (NewByte < 0) {
3299 addUndef();
3300 return;
3301 }
3302 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes);
3303 Byte = unsigned(NewByte) % SystemZ::VectorBytes;
3304 } else if (Op.getOpcode() == ISD::UNDEF) {
3305 addUndef();
3306 return;
3307 } else
3308 break;
3309 }
3310
3311 // Make sure that the source of the extraction is in Ops.
3312 unsigned OpNo = 0;
3313 for (; OpNo < Ops.size(); ++OpNo)
3314 if (Ops[OpNo] == Op)
3315 break;
3316 if (OpNo == Ops.size())
3317 Ops.push_back(Op);
3318
3319 // Add the element to Bytes.
3320 unsigned Base = OpNo * SystemZ::VectorBytes + Byte;
3321 for (unsigned I = 0; I < BytesPerElement; ++I)
3322 Bytes.push_back(Base + I);
3323}
3324
3325// Return SDNodes for the completed shuffle.
3326SDValue GeneralShuffle::getNode(SelectionDAG &DAG, SDLoc DL) {
3327 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector");
3328
3329 if (Ops.size() == 0)
3330 return DAG.getUNDEF(VT);
3331
3332 // Make sure that there are at least two shuffle operands.
3333 if (Ops.size() == 1)
3334 Ops.push_back(DAG.getUNDEF(MVT::v16i8));
3335
3336 // Create a tree of shuffles, deferring root node until after the loop.
3337 // Try to redistribute the undefined elements of non-root nodes so that
3338 // the non-root shuffles match something like a pack or merge, then adjust
3339 // the parent node's permute vector to compensate for the new order.
3340 // Among other things, this copes with vectors like <2 x i16> that were
3341 // padded with undefined elements during type legalization.
3342 //
3343 // In the best case this redistribution will lead to the whole tree
3344 // using packs and merges. It should rarely be a loss in other cases.
3345 unsigned Stride = 1;
3346 for (; Stride * 2 < Ops.size(); Stride *= 2) {
3347 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) {
3348 SDValue SubOps[] = { Ops[I], Ops[I + Stride] };
3349
3350 // Create a mask for just these two operands.
3351 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes);
3352 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3353 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes;
3354 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes;
3355 if (OpNo == I)
3356 NewBytes[J] = Byte;
3357 else if (OpNo == I + Stride)
3358 NewBytes[J] = SystemZ::VectorBytes + Byte;
3359 else
3360 NewBytes[J] = -1;
3361 }
3362 // See if it would be better to reorganize NewMask to avoid using VPERM.
3363 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes);
3364 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) {
3365 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]);
3366 // Applying NewBytesMap to Ops[I] gets back to NewBytes.
3367 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3368 if (NewBytes[J] >= 0) {
3369 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes &&
3370 "Invalid double permute");
3371 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J];
3372 } else
3373 assert(NewBytesMap[J] < 0 && "Invalid double permute");
3374 }
3375 } else {
3376 // Just use NewBytes on the operands.
3377 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes);
3378 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J)
3379 if (NewBytes[J] >= 0)
3380 Bytes[J] = I * SystemZ::VectorBytes + J;
3381 }
3382 }
3383 }
3384
3385 // Now we just have 2 inputs. Put the second operand in Ops[1].
3386 if (Stride > 1) {
3387 Ops[1] = Ops[Stride];
3388 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3389 if (Bytes[I] >= int(SystemZ::VectorBytes))
3390 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes;
3391 }
3392
3393 // Look for an instruction that can do the permute without resorting
3394 // to VPERM.
3395 unsigned OpNo0, OpNo1;
3396 SDValue Op;
3397 if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1))
3398 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]);
3399 else
3400 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes);
3401 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3402}
3403
Ulrich Weigandcd808232015-05-05 19:26:48 +00003404// Return true if the given BUILD_VECTOR is a scalar-to-vector conversion.
3405static bool isScalarToVector(SDValue Op) {
3406 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I)
3407 if (Op.getOperand(I).getOpcode() != ISD::UNDEF)
3408 return false;
3409 return true;
3410}
3411
3412// Return a vector of type VT that contains Value in the first element.
3413// The other elements don't matter.
3414static SDValue buildScalarToVector(SelectionDAG &DAG, SDLoc DL, EVT VT,
3415 SDValue Value) {
3416 // If we have a constant, replicate it to all elements and let the
3417 // BUILD_VECTOR lowering take care of it.
3418 if (Value.getOpcode() == ISD::Constant ||
3419 Value.getOpcode() == ISD::ConstantFP) {
3420 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value);
3421 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
3422 }
3423 if (Value.getOpcode() == ISD::UNDEF)
3424 return DAG.getUNDEF(VT);
3425 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
3426}
3427
3428// Return a vector of type VT in which Op0 is in element 0 and Op1 is in
3429// element 1. Used for cases in which replication is cheap.
3430static SDValue buildMergeScalars(SelectionDAG &DAG, SDLoc DL, EVT VT,
3431 SDValue Op0, SDValue Op1) {
3432 if (Op0.getOpcode() == ISD::UNDEF) {
3433 if (Op1.getOpcode() == ISD::UNDEF)
3434 return DAG.getUNDEF(VT);
3435 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1);
3436 }
3437 if (Op1.getOpcode() == ISD::UNDEF)
3438 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0);
3439 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT,
3440 buildScalarToVector(DAG, DL, VT, Op0),
3441 buildScalarToVector(DAG, DL, VT, Op1));
3442}
3443
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003444// Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64
3445// vector for them.
3446static SDValue joinDwords(SelectionDAG &DAG, SDLoc DL, SDValue Op0,
3447 SDValue Op1) {
3448 if (Op0.getOpcode() == ISD::UNDEF && Op1.getOpcode() == ISD::UNDEF)
3449 return DAG.getUNDEF(MVT::v2i64);
3450 // If one of the two inputs is undefined then replicate the other one,
3451 // in order to avoid using another register unnecessarily.
3452 if (Op0.getOpcode() == ISD::UNDEF)
3453 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
3454 else if (Op1.getOpcode() == ISD::UNDEF)
3455 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
3456 else {
3457 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
3458 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
3459 }
3460 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1);
3461}
3462
3463// Try to represent constant BUILD_VECTOR node BVN using a
3464// SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask
3465// on success.
3466static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) {
3467 EVT ElemVT = BVN->getValueType(0).getVectorElementType();
3468 unsigned BytesPerElement = ElemVT.getStoreSize();
3469 for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) {
3470 SDValue Op = BVN->getOperand(I);
3471 if (Op.getOpcode() != ISD::UNDEF) {
3472 uint64_t Value;
3473 if (Op.getOpcode() == ISD::Constant)
3474 Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue();
3475 else if (Op.getOpcode() == ISD::ConstantFP)
3476 Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt()
3477 .getZExtValue());
3478 else
3479 return false;
3480 for (unsigned J = 0; J < BytesPerElement; ++J) {
3481 uint64_t Byte = (Value >> (J * 8)) & 0xff;
3482 if (Byte == 0xff)
3483 Mask |= 1 << ((E - I - 1) * BytesPerElement + J);
3484 else if (Byte != 0)
3485 return false;
3486 }
3487 }
3488 }
3489 return true;
3490}
3491
3492// Try to load a vector constant in which BitsPerElement-bit value Value
3493// is replicated to fill the vector. VT is the type of the resulting
3494// constant, which may have elements of a different size from BitsPerElement.
3495// Return the SDValue of the constant on success, otherwise return
3496// an empty value.
3497static SDValue tryBuildVectorReplicate(SelectionDAG &DAG,
3498 const SystemZInstrInfo *TII,
3499 SDLoc DL, EVT VT, uint64_t Value,
3500 unsigned BitsPerElement) {
3501 // Signed 16-bit values can be replicated using VREPI.
3502 int64_t SignedValue = SignExtend64(Value, BitsPerElement);
3503 if (isInt<16>(SignedValue)) {
3504 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
3505 SystemZ::VectorBits / BitsPerElement);
3506 SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT,
3507 DAG.getConstant(SignedValue, DL, MVT::i32));
3508 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3509 }
3510 // See whether rotating the constant left some N places gives a value that
3511 // is one less than a power of 2 (i.e. all zeros followed by all ones).
3512 // If so we can use VGM.
3513 unsigned Start, End;
3514 if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) {
3515 // isRxSBGMask returns the bit numbers for a full 64-bit value,
3516 // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to
3517 // bit numbers for an BitsPerElement value, so that 0 denotes
3518 // 1 << (BitsPerElement-1).
3519 Start -= 64 - BitsPerElement;
3520 End -= 64 - BitsPerElement;
3521 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
3522 SystemZ::VectorBits / BitsPerElement);
3523 SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT,
3524 DAG.getConstant(Start, DL, MVT::i32),
3525 DAG.getConstant(End, DL, MVT::i32));
3526 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3527 }
3528 return SDValue();
3529}
3530
3531// If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually
3532// better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for
3533// the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR
3534// would benefit from this representation and return it if so.
3535static SDValue tryBuildVectorShuffle(SelectionDAG &DAG,
3536 BuildVectorSDNode *BVN) {
3537 EVT VT = BVN->getValueType(0);
3538 unsigned NumElements = VT.getVectorNumElements();
3539
3540 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation
3541 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still
3542 // need a BUILD_VECTOR, add an additional placeholder operand for that
3543 // BUILD_VECTOR and store its operands in ResidueOps.
3544 GeneralShuffle GS(VT);
3545 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps;
3546 bool FoundOne = false;
3547 for (unsigned I = 0; I < NumElements; ++I) {
3548 SDValue Op = BVN->getOperand(I);
3549 if (Op.getOpcode() == ISD::TRUNCATE)
3550 Op = Op.getOperand(0);
3551 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
3552 Op.getOperand(1).getOpcode() == ISD::Constant) {
3553 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
3554 GS.add(Op.getOperand(0), Elem);
3555 FoundOne = true;
3556 } else if (Op.getOpcode() == ISD::UNDEF) {
3557 GS.addUndef();
3558 } else {
3559 GS.add(SDValue(), ResidueOps.size());
3560 ResidueOps.push_back(Op);
3561 }
3562 }
3563
3564 // Nothing to do if there are no EXTRACT_VECTOR_ELTs.
3565 if (!FoundOne)
3566 return SDValue();
3567
3568 // Create the BUILD_VECTOR for the remaining elements, if any.
3569 if (!ResidueOps.empty()) {
3570 while (ResidueOps.size() < NumElements)
3571 ResidueOps.push_back(DAG.getUNDEF(VT.getVectorElementType()));
3572 for (auto &Op : GS.Ops) {
3573 if (!Op.getNode()) {
3574 Op = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BVN), VT, ResidueOps);
3575 break;
3576 }
3577 }
3578 }
3579 return GS.getNode(DAG, SDLoc(BVN));
3580}
3581
3582// Combine GPR scalar values Elems into a vector of type VT.
3583static SDValue buildVector(SelectionDAG &DAG, SDLoc DL, EVT VT,
3584 SmallVectorImpl<SDValue> &Elems) {
3585 // See whether there is a single replicated value.
3586 SDValue Single;
3587 unsigned int NumElements = Elems.size();
3588 unsigned int Count = 0;
3589 for (auto Elem : Elems) {
3590 if (Elem.getOpcode() != ISD::UNDEF) {
3591 if (!Single.getNode())
3592 Single = Elem;
3593 else if (Elem != Single) {
3594 Single = SDValue();
3595 break;
3596 }
3597 Count += 1;
3598 }
3599 }
3600 // There are three cases here:
3601 //
3602 // - if the only defined element is a loaded one, the best sequence
3603 // is a replicating load.
3604 //
3605 // - otherwise, if the only defined element is an i64 value, we will
3606 // end up with the same VLVGP sequence regardless of whether we short-cut
3607 // for replication or fall through to the later code.
3608 //
3609 // - otherwise, if the only defined element is an i32 or smaller value,
3610 // we would need 2 instructions to replicate it: VLVGP followed by VREPx.
3611 // This is only a win if the single defined element is used more than once.
3612 // In other cases we're better off using a single VLVGx.
3613 if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD))
3614 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single);
3615
3616 // The best way of building a v2i64 from two i64s is to use VLVGP.
3617 if (VT == MVT::v2i64)
3618 return joinDwords(DAG, DL, Elems[0], Elems[1]);
3619
Ulrich Weigandcd808232015-05-05 19:26:48 +00003620 // Use a 64-bit merge high to combine two doubles.
3621 if (VT == MVT::v2f64)
3622 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
3623
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003624 // Collect the constant terms.
3625 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue());
3626 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false);
3627
3628 unsigned NumConstants = 0;
3629 for (unsigned I = 0; I < NumElements; ++I) {
3630 SDValue Elem = Elems[I];
3631 if (Elem.getOpcode() == ISD::Constant ||
3632 Elem.getOpcode() == ISD::ConstantFP) {
3633 NumConstants += 1;
3634 Constants[I] = Elem;
3635 Done[I] = true;
3636 }
3637 }
3638 // If there was at least one constant, fill in the other elements of
3639 // Constants with undefs to get a full vector constant and use that
3640 // as the starting point.
3641 SDValue Result;
3642 if (NumConstants > 0) {
3643 for (unsigned I = 0; I < NumElements; ++I)
3644 if (!Constants[I].getNode())
3645 Constants[I] = DAG.getUNDEF(Elems[I].getValueType());
3646 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Constants);
3647 } else {
3648 // Otherwise try to use VLVGP to start the sequence in order to
3649 // avoid a false dependency on any previous contents of the vector
3650 // register. This only makes sense if one of the associated elements
3651 // is defined.
3652 unsigned I1 = NumElements / 2 - 1;
3653 unsigned I2 = NumElements - 1;
3654 bool Def1 = (Elems[I1].getOpcode() != ISD::UNDEF);
3655 bool Def2 = (Elems[I2].getOpcode() != ISD::UNDEF);
3656 if (Def1 || Def2) {
3657 SDValue Elem1 = Elems[Def1 ? I1 : I2];
3658 SDValue Elem2 = Elems[Def2 ? I2 : I1];
3659 Result = DAG.getNode(ISD::BITCAST, DL, VT,
3660 joinDwords(DAG, DL, Elem1, Elem2));
3661 Done[I1] = true;
3662 Done[I2] = true;
3663 } else
3664 Result = DAG.getUNDEF(VT);
3665 }
3666
3667 // Use VLVGx to insert the other elements.
3668 for (unsigned I = 0; I < NumElements; ++I)
3669 if (!Done[I] && Elems[I].getOpcode() != ISD::UNDEF)
3670 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I],
3671 DAG.getConstant(I, DL, MVT::i32));
3672 return Result;
3673}
3674
3675SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op,
3676 SelectionDAG &DAG) const {
3677 const SystemZInstrInfo *TII =
3678 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
3679 auto *BVN = cast<BuildVectorSDNode>(Op.getNode());
3680 SDLoc DL(Op);
3681 EVT VT = Op.getValueType();
3682
3683 if (BVN->isConstant()) {
3684 // Try using VECTOR GENERATE BYTE MASK. This is the architecturally-
3685 // preferred way of creating all-zero and all-one vectors so give it
3686 // priority over other methods below.
3687 uint64_t Mask = 0;
3688 if (tryBuildVectorByteMask(BVN, Mask)) {
3689 SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3690 DAG.getConstant(Mask, DL, MVT::i32));
3691 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3692 }
3693
3694 // Try using some form of replication.
3695 APInt SplatBits, SplatUndef;
3696 unsigned SplatBitSize;
3697 bool HasAnyUndefs;
3698 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
3699 8, true) &&
3700 SplatBitSize <= 64) {
3701 // First try assuming that any undefined bits above the highest set bit
3702 // and below the lowest set bit are 1s. This increases the likelihood of
3703 // being able to use a sign-extended element value in VECTOR REPLICATE
3704 // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK.
3705 uint64_t SplatBitsZ = SplatBits.getZExtValue();
3706 uint64_t SplatUndefZ = SplatUndef.getZExtValue();
3707 uint64_t Lower = (SplatUndefZ
3708 & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1));
3709 uint64_t Upper = (SplatUndefZ
3710 & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1));
3711 uint64_t Value = SplatBitsZ | Upper | Lower;
3712 SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value,
3713 SplatBitSize);
3714 if (Op.getNode())
3715 return Op;
3716
3717 // Now try assuming that any undefined bits between the first and
3718 // last defined set bits are set. This increases the chances of
3719 // using a non-wraparound mask.
3720 uint64_t Middle = SplatUndefZ & ~Upper & ~Lower;
3721 Value = SplatBitsZ | Middle;
3722 Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize);
3723 if (Op.getNode())
3724 return Op;
3725 }
3726
3727 // Fall back to loading it from memory.
3728 return SDValue();
3729 }
3730
3731 // See if we should use shuffles to construct the vector from other vectors.
3732 SDValue Res = tryBuildVectorShuffle(DAG, BVN);
3733 if (Res.getNode())
3734 return Res;
3735
Ulrich Weigandcd808232015-05-05 19:26:48 +00003736 // Detect SCALAR_TO_VECTOR conversions.
3737 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op))
3738 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0));
3739
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003740 // Otherwise use buildVector to build the vector up from GPRs.
3741 unsigned NumElements = Op.getNumOperands();
3742 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements);
3743 for (unsigned I = 0; I < NumElements; ++I)
3744 Ops[I] = Op.getOperand(I);
3745 return buildVector(DAG, DL, VT, Ops);
3746}
3747
3748SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
3749 SelectionDAG &DAG) const {
3750 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode());
3751 SDLoc DL(Op);
3752 EVT VT = Op.getValueType();
3753 unsigned NumElements = VT.getVectorNumElements();
3754
3755 if (VSN->isSplat()) {
3756 SDValue Op0 = Op.getOperand(0);
3757 unsigned Index = VSN->getSplatIndex();
3758 assert(Index < VT.getVectorNumElements() &&
3759 "Splat index should be defined and in first operand");
3760 // See whether the value we're splatting is directly available as a scalar.
3761 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
3762 Op0.getOpcode() == ISD::BUILD_VECTOR)
3763 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index));
3764 // Otherwise keep it as a vector-to-vector operation.
3765 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0),
3766 DAG.getConstant(Index, DL, MVT::i32));
3767 }
3768
3769 GeneralShuffle GS(VT);
3770 for (unsigned I = 0; I < NumElements; ++I) {
3771 int Elt = VSN->getMaskElt(I);
3772 if (Elt < 0)
3773 GS.addUndef();
3774 else
3775 GS.add(Op.getOperand(unsigned(Elt) / NumElements),
3776 unsigned(Elt) % NumElements);
3777 }
3778 return GS.getNode(DAG, SDLoc(VSN));
3779}
3780
3781SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
3782 SelectionDAG &DAG) const {
3783 SDLoc DL(Op);
3784 // Just insert the scalar into element 0 of an undefined vector.
3785 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL,
3786 Op.getValueType(), DAG.getUNDEF(Op.getValueType()),
3787 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32));
3788}
3789
Ulrich Weigandcd808232015-05-05 19:26:48 +00003790SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
3791 SelectionDAG &DAG) const {
3792 // Handle insertions of floating-point values.
3793 SDLoc DL(Op);
3794 SDValue Op0 = Op.getOperand(0);
3795 SDValue Op1 = Op.getOperand(1);
3796 SDValue Op2 = Op.getOperand(2);
3797 EVT VT = Op.getValueType();
3798
3799 // Insertions into constant indices can be done using VPDI. However,
3800 // if the inserted value is a bitcast or a constant then it's better
3801 // to use GPRs, as below.
3802 if (Op1.getOpcode() != ISD::BITCAST &&
3803 Op1.getOpcode() != ISD::ConstantFP &&
3804 Op2.getOpcode() == ISD::Constant) {
3805 uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue();
3806 unsigned Mask = VT.getVectorNumElements() - 1;
3807 if (Index <= Mask)
3808 return Op;
3809 }
3810
3811 // Otherwise bitcast to the equivalent integer form and insert via a GPR.
3812 MVT IntVT = MVT::getIntegerVT(VT.getVectorElementType().getSizeInBits());
3813 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements());
3814 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT,
3815 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0),
3816 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2);
3817 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
3818}
3819
3820SDValue
3821SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
3822 SelectionDAG &DAG) const {
3823 // Handle extractions of floating-point values.
3824 SDLoc DL(Op);
3825 SDValue Op0 = Op.getOperand(0);
3826 SDValue Op1 = Op.getOperand(1);
3827 EVT VT = Op.getValueType();
3828 EVT VecVT = Op0.getValueType();
3829
3830 // Extractions of constant indices can be done directly.
3831 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) {
3832 uint64_t Index = CIndexN->getZExtValue();
3833 unsigned Mask = VecVT.getVectorNumElements() - 1;
3834 if (Index <= Mask)
3835 return Op;
3836 }
3837
3838 // Otherwise bitcast to the equivalent integer form and extract via a GPR.
3839 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
3840 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements());
3841 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT,
3842 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1);
3843 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
3844}
3845
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003846SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG,
3847 unsigned ByScalar) const {
3848 // Look for cases where a vector shift can use the *_BY_SCALAR form.
3849 SDValue Op0 = Op.getOperand(0);
3850 SDValue Op1 = Op.getOperand(1);
3851 SDLoc DL(Op);
3852 EVT VT = Op.getValueType();
3853 unsigned ElemBitSize = VT.getVectorElementType().getSizeInBits();
3854
3855 // See whether the shift vector is a splat represented as BUILD_VECTOR.
3856 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) {
3857 APInt SplatBits, SplatUndef;
3858 unsigned SplatBitSize;
3859 bool HasAnyUndefs;
3860 // Check for constant splats. Use ElemBitSize as the minimum element
3861 // width and reject splats that need wider elements.
3862 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
3863 ElemBitSize, true) &&
3864 SplatBitSize == ElemBitSize) {
3865 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff,
3866 DL, MVT::i32);
3867 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
3868 }
3869 // Check for variable splats.
3870 BitVector UndefElements;
3871 SDValue Splat = BVN->getSplatValue(&UndefElements);
3872 if (Splat) {
3873 // Since i32 is the smallest legal type, we either need a no-op
3874 // or a truncation.
3875 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat);
3876 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
3877 }
3878 }
3879
3880 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR,
3881 // and the shift amount is directly available in a GPR.
3882 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) {
3883 if (VSN->isSplat()) {
3884 SDValue VSNOp0 = VSN->getOperand(0);
3885 unsigned Index = VSN->getSplatIndex();
3886 assert(Index < VT.getVectorNumElements() &&
3887 "Splat index should be defined and in first operand");
3888 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
3889 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) {
3890 // Since i32 is the smallest legal type, we either need a no-op
3891 // or a truncation.
3892 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32,
3893 VSNOp0.getOperand(Index));
3894 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
3895 }
3896 }
3897 }
3898
3899 // Otherwise just treat the current form as legal.
3900 return Op;
3901}
3902
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003903SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
3904 SelectionDAG &DAG) const {
3905 switch (Op.getOpcode()) {
3906 case ISD::BR_CC:
3907 return lowerBR_CC(Op, DAG);
3908 case ISD::SELECT_CC:
3909 return lowerSELECT_CC(Op, DAG);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00003910 case ISD::SETCC:
3911 return lowerSETCC(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003912 case ISD::GlobalAddress:
3913 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
3914 case ISD::GlobalTLSAddress:
3915 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
3916 case ISD::BlockAddress:
3917 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
3918 case ISD::JumpTable:
3919 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
3920 case ISD::ConstantPool:
3921 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
3922 case ISD::BITCAST:
3923 return lowerBITCAST(Op, DAG);
3924 case ISD::VASTART:
3925 return lowerVASTART(Op, DAG);
3926 case ISD::VACOPY:
3927 return lowerVACOPY(Op, DAG);
3928 case ISD::DYNAMIC_STACKALLOC:
3929 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003930 case ISD::SMUL_LOHI:
3931 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003932 case ISD::UMUL_LOHI:
3933 return lowerUMUL_LOHI(Op, DAG);
3934 case ISD::SDIVREM:
3935 return lowerSDIVREM(Op, DAG);
3936 case ISD::UDIVREM:
3937 return lowerUDIVREM(Op, DAG);
3938 case ISD::OR:
3939 return lowerOR(Op, DAG);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003940 case ISD::CTPOP:
3941 return lowerCTPOP(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003942 case ISD::CTLZ_ZERO_UNDEF:
3943 return DAG.getNode(ISD::CTLZ, SDLoc(Op),
3944 Op.getValueType(), Op.getOperand(0));
3945 case ISD::CTTZ_ZERO_UNDEF:
3946 return DAG.getNode(ISD::CTTZ, SDLoc(Op),
3947 Op.getValueType(), Op.getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003948 case ISD::ATOMIC_SWAP:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003949 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
3950 case ISD::ATOMIC_STORE:
3951 return lowerATOMIC_STORE(Op, DAG);
3952 case ISD::ATOMIC_LOAD:
3953 return lowerATOMIC_LOAD(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003954 case ISD::ATOMIC_LOAD_ADD:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003955 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003956 case ISD::ATOMIC_LOAD_SUB:
Richard Sandiford41350a52013-12-24 15:18:04 +00003957 return lowerATOMIC_LOAD_SUB(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003958 case ISD::ATOMIC_LOAD_AND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003959 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003960 case ISD::ATOMIC_LOAD_OR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003961 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003962 case ISD::ATOMIC_LOAD_XOR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003963 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003964 case ISD::ATOMIC_LOAD_NAND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003965 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003966 case ISD::ATOMIC_LOAD_MIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003967 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003968 case ISD::ATOMIC_LOAD_MAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003969 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003970 case ISD::ATOMIC_LOAD_UMIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003971 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003972 case ISD::ATOMIC_LOAD_UMAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003973 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003974 case ISD::ATOMIC_CMP_SWAP:
3975 return lowerATOMIC_CMP_SWAP(Op, DAG);
3976 case ISD::STACKSAVE:
3977 return lowerSTACKSAVE(Op, DAG);
3978 case ISD::STACKRESTORE:
3979 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00003980 case ISD::PREFETCH:
3981 return lowerPREFETCH(Op, DAG);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003982 case ISD::INTRINSIC_W_CHAIN:
3983 return lowerINTRINSIC_W_CHAIN(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003984 case ISD::BUILD_VECTOR:
3985 return lowerBUILD_VECTOR(Op, DAG);
3986 case ISD::VECTOR_SHUFFLE:
3987 return lowerVECTOR_SHUFFLE(Op, DAG);
3988 case ISD::SCALAR_TO_VECTOR:
3989 return lowerSCALAR_TO_VECTOR(Op, DAG);
Ulrich Weigandcd808232015-05-05 19:26:48 +00003990 case ISD::INSERT_VECTOR_ELT:
3991 return lowerINSERT_VECTOR_ELT(Op, DAG);
3992 case ISD::EXTRACT_VECTOR_ELT:
3993 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003994 case ISD::SHL:
3995 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR);
3996 case ISD::SRL:
3997 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR);
3998 case ISD::SRA:
3999 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004000 default:
4001 llvm_unreachable("Unexpected node to lower");
4002 }
4003}
4004
4005const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
4006#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
4007 switch (Opcode) {
4008 OPCODE(RET_FLAG);
4009 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00004010 OPCODE(SIBCALL);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004011 OPCODE(TLS_GDCALL);
4012 OPCODE(TLS_LDCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004013 OPCODE(PCREL_WRAPPER);
Richard Sandiford54b36912013-09-27 15:14:04 +00004014 OPCODE(PCREL_OFFSET);
Richard Sandiford57485472013-12-13 15:35:00 +00004015 OPCODE(IABS);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00004016 OPCODE(ICMP);
4017 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00004018 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004019 OPCODE(BR_CCMASK);
4020 OPCODE(SELECT_CCMASK);
4021 OPCODE(ADJDYNALLOC);
4022 OPCODE(EXTRACT_ACCESS);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004023 OPCODE(POPCNT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004024 OPCODE(UMUL_LOHI64);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004025 OPCODE(SDIVREM32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004026 OPCODE(SDIVREM64);
4027 OPCODE(UDIVREM32);
4028 OPCODE(UDIVREM64);
Richard Sandifordd131ff82013-07-08 09:35:23 +00004029 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004030 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00004031 OPCODE(NC);
4032 OPCODE(NC_LOOP);
4033 OPCODE(OC);
4034 OPCODE(OC_LOOP);
4035 OPCODE(XC);
4036 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00004037 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004038 OPCODE(CLC_LOOP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00004039 OPCODE(STPCPY);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004040 OPCODE(STRCMP);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00004041 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00004042 OPCODE(IPM);
Richard Sandiford9afe6132013-12-10 10:36:34 +00004043 OPCODE(SERIALIZE);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004044 OPCODE(TBEGIN);
4045 OPCODE(TBEGIN_NOFLOAT);
4046 OPCODE(TEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004047 OPCODE(BYTE_MASK);
4048 OPCODE(ROTATE_MASK);
4049 OPCODE(REPLICATE);
4050 OPCODE(JOIN_DWORDS);
4051 OPCODE(SPLAT);
4052 OPCODE(MERGE_HIGH);
4053 OPCODE(MERGE_LOW);
4054 OPCODE(SHL_DOUBLE);
4055 OPCODE(PERMUTE_DWORDS);
4056 OPCODE(PERMUTE);
4057 OPCODE(PACK);
4058 OPCODE(VSHL_BY_SCALAR);
4059 OPCODE(VSRL_BY_SCALAR);
4060 OPCODE(VSRA_BY_SCALAR);
4061 OPCODE(VSUM);
4062 OPCODE(VICMPE);
4063 OPCODE(VICMPH);
4064 OPCODE(VICMPHL);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004065 OPCODE(VFCMPE);
4066 OPCODE(VFCMPH);
4067 OPCODE(VFCMPHE);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004068 OPCODE(ATOMIC_SWAPW);
4069 OPCODE(ATOMIC_LOADW_ADD);
4070 OPCODE(ATOMIC_LOADW_SUB);
4071 OPCODE(ATOMIC_LOADW_AND);
4072 OPCODE(ATOMIC_LOADW_OR);
4073 OPCODE(ATOMIC_LOADW_XOR);
4074 OPCODE(ATOMIC_LOADW_NAND);
4075 OPCODE(ATOMIC_LOADW_MIN);
4076 OPCODE(ATOMIC_LOADW_MAX);
4077 OPCODE(ATOMIC_LOADW_UMIN);
4078 OPCODE(ATOMIC_LOADW_UMAX);
4079 OPCODE(ATOMIC_CMP_SWAPW);
Richard Sandiford03481332013-08-23 11:36:42 +00004080 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004081 }
Craig Topper062a2ba2014-04-25 05:30:21 +00004082 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004083#undef OPCODE
4084}
4085
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004086// Return true if VT is a vector whose elements are a whole number of bytes
4087// in width.
4088static bool canTreatAsByteVector(EVT VT) {
4089 return VT.isVector() && VT.getVectorElementType().getSizeInBits() % 8 == 0;
4090}
4091
4092// Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT
4093// producing a result of type ResVT. Op is a possibly bitcast version
4094// of the input vector and Index is the index (based on type VecVT) that
4095// should be extracted. Return the new extraction if a simplification
4096// was possible or if Force is true.
4097SDValue SystemZTargetLowering::combineExtract(SDLoc DL, EVT ResVT, EVT VecVT,
4098 SDValue Op, unsigned Index,
4099 DAGCombinerInfo &DCI,
4100 bool Force) const {
4101 SelectionDAG &DAG = DCI.DAG;
4102
4103 // The number of bytes being extracted.
4104 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4105
4106 for (;;) {
4107 unsigned Opcode = Op.getOpcode();
4108 if (Opcode == ISD::BITCAST)
4109 // Look through bitcasts.
4110 Op = Op.getOperand(0);
4111 else if (Opcode == ISD::VECTOR_SHUFFLE &&
4112 canTreatAsByteVector(Op.getValueType())) {
4113 // Get a VPERM-like permute mask and see whether the bytes covered
4114 // by the extracted element are a contiguous sequence from one
4115 // source operand.
4116 SmallVector<int, SystemZ::VectorBytes> Bytes;
4117 getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes);
4118 int First;
4119 if (!getShuffleInput(Bytes, Index * BytesPerElement,
4120 BytesPerElement, First))
4121 break;
4122 if (First < 0)
4123 return DAG.getUNDEF(ResVT);
4124 // Make sure the contiguous sequence starts at a multiple of the
4125 // original element size.
4126 unsigned Byte = unsigned(First) % Bytes.size();
4127 if (Byte % BytesPerElement != 0)
4128 break;
4129 // We can get the extracted value directly from an input.
4130 Index = Byte / BytesPerElement;
4131 Op = Op.getOperand(unsigned(First) / Bytes.size());
4132 Force = true;
4133 } else if (Opcode == ISD::BUILD_VECTOR &&
4134 canTreatAsByteVector(Op.getValueType())) {
4135 // We can only optimize this case if the BUILD_VECTOR elements are
4136 // at least as wide as the extracted value.
4137 EVT OpVT = Op.getValueType();
4138 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4139 if (OpBytesPerElement < BytesPerElement)
4140 break;
4141 // Make sure that the least-significant bit of the extracted value
4142 // is the least significant bit of an input.
4143 unsigned End = (Index + 1) * BytesPerElement;
4144 if (End % OpBytesPerElement != 0)
4145 break;
4146 // We're extracting the low part of one operand of the BUILD_VECTOR.
4147 Op = Op.getOperand(End / OpBytesPerElement - 1);
4148 if (!Op.getValueType().isInteger()) {
4149 EVT VT = MVT::getIntegerVT(Op.getValueType().getSizeInBits());
4150 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
4151 DCI.AddToWorklist(Op.getNode());
4152 }
4153 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits());
4154 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
4155 if (VT != ResVT) {
4156 DCI.AddToWorklist(Op.getNode());
4157 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op);
4158 }
4159 return Op;
4160 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||
4161 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG ||
4162 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) &&
4163 canTreatAsByteVector(Op.getValueType()) &&
4164 canTreatAsByteVector(Op.getOperand(0).getValueType())) {
4165 // Make sure that only the unextended bits are significant.
4166 EVT ExtVT = Op.getValueType();
4167 EVT OpVT = Op.getOperand(0).getValueType();
4168 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize();
4169 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4170 unsigned Byte = Index * BytesPerElement;
4171 unsigned SubByte = Byte % ExtBytesPerElement;
4172 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement;
4173 if (SubByte < MinSubByte ||
4174 SubByte + BytesPerElement > ExtBytesPerElement)
4175 break;
4176 // Get the byte offset of the unextended element
4177 Byte = Byte / ExtBytesPerElement * OpBytesPerElement;
4178 // ...then add the byte offset relative to that element.
4179 Byte += SubByte - MinSubByte;
4180 if (Byte % BytesPerElement != 0)
4181 break;
4182 Op = Op.getOperand(0);
4183 Index = Byte / BytesPerElement;
4184 Force = true;
4185 } else
4186 break;
4187 }
4188 if (Force) {
4189 if (Op.getValueType() != VecVT) {
4190 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op);
4191 DCI.AddToWorklist(Op.getNode());
4192 }
4193 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op,
4194 DAG.getConstant(Index, DL, MVT::i32));
4195 }
4196 return SDValue();
4197}
4198
4199// Optimize vector operations in scalar value Op on the basis that Op
4200// is truncated to TruncVT.
4201SDValue
4202SystemZTargetLowering::combineTruncateExtract(SDLoc DL, EVT TruncVT, SDValue Op,
4203 DAGCombinerInfo &DCI) const {
4204 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into
4205 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements
4206 // of type TruncVT.
4207 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4208 TruncVT.getSizeInBits() % 8 == 0) {
4209 SDValue Vec = Op.getOperand(0);
4210 EVT VecVT = Vec.getValueType();
4211 if (canTreatAsByteVector(VecVT)) {
4212 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
4213 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4214 unsigned TruncBytes = TruncVT.getStoreSize();
4215 if (BytesPerElement % TruncBytes == 0) {
4216 // Calculate the value of Y' in the above description. We are
4217 // splitting the original elements into Scale equal-sized pieces
4218 // and for truncation purposes want the last (least-significant)
4219 // of these pieces for IndexN. This is easiest to do by calculating
4220 // the start index of the following element and then subtracting 1.
4221 unsigned Scale = BytesPerElement / TruncBytes;
4222 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1;
4223
4224 // Defer the creation of the bitcast from X to combineExtract,
4225 // which might be able to optimize the extraction.
4226 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8),
4227 VecVT.getStoreSize() / TruncBytes);
4228 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT);
4229 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true);
4230 }
4231 }
4232 }
4233 }
4234 return SDValue();
4235}
4236
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004237SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
4238 DAGCombinerInfo &DCI) const {
4239 SelectionDAG &DAG = DCI.DAG;
4240 unsigned Opcode = N->getOpcode();
4241 if (Opcode == ISD::SIGN_EXTEND) {
4242 // Convert (sext (ashr (shl X, C1), C2)) to
4243 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
4244 // cheap as narrower ones.
4245 SDValue N0 = N->getOperand(0);
4246 EVT VT = N->getValueType(0);
4247 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
4248 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
4249 SDValue Inner = N0.getOperand(0);
4250 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
4251 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
4252 unsigned Extra = (VT.getSizeInBits() -
4253 N0.getValueType().getSizeInBits());
4254 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
4255 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
4256 EVT ShiftVT = N0.getOperand(1).getValueType();
4257 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
4258 Inner.getOperand(0));
4259 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004260 DAG.getConstant(NewShlAmt, SDLoc(Inner),
4261 ShiftVT));
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004262 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004263 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT));
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004264 }
4265 }
4266 }
4267 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004268 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better
4269 // for the extraction to be done on a vMiN value, so that we can use VSTE.
4270 // If X has wider elements then convert it to:
4271 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z).
4272 if (Opcode == ISD::STORE) {
4273 auto *SN = cast<StoreSDNode>(N);
4274 EVT MemVT = SN->getMemoryVT();
4275 if (MemVT.isInteger()) {
4276 SDValue Value = combineTruncateExtract(SDLoc(N), MemVT,
4277 SN->getValue(), DCI);
4278 if (Value.getNode()) {
4279 DCI.AddToWorklist(Value.getNode());
4280
4281 // Rewrite the store with the new form of stored value.
4282 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value,
4283 SN->getBasePtr(), SN->getMemoryVT(),
4284 SN->getMemOperand());
4285 }
4286 }
4287 }
4288 // Try to simplify a vector extraction.
4289 if (Opcode == ISD::EXTRACT_VECTOR_ELT) {
4290 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
4291 SDValue Op0 = N->getOperand(0);
4292 EVT VecVT = Op0.getValueType();
4293 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0,
4294 IndexN->getZExtValue(), DCI, false);
4295 }
4296 }
4297 // (join_dwords X, X) == (replicate X)
4298 if (Opcode == SystemZISD::JOIN_DWORDS &&
4299 N->getOperand(0) == N->getOperand(1))
4300 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0),
4301 N->getOperand(0));
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004302 return SDValue();
4303}
4304
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004305//===----------------------------------------------------------------------===//
4306// Custom insertion
4307//===----------------------------------------------------------------------===//
4308
4309// Create a new basic block after MBB.
4310static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
4311 MachineFunction &MF = *MBB->getParent();
4312 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00004313 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004314 return NewMBB;
4315}
4316
Richard Sandifordbe133a82013-08-28 09:01:51 +00004317// Split MBB after MI and return the new block (the one that contains
4318// instructions after MI).
4319static MachineBasicBlock *splitBlockAfter(MachineInstr *MI,
4320 MachineBasicBlock *MBB) {
4321 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
4322 NewMBB->splice(NewMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00004323 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Richard Sandifordbe133a82013-08-28 09:01:51 +00004324 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
4325 return NewMBB;
4326}
4327
Richard Sandiford5e318f02013-08-27 09:54:29 +00004328// Split MBB before MI and return the new block (the one that contains MI).
4329static MachineBasicBlock *splitBlockBefore(MachineInstr *MI,
4330 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004331 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004332 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004333 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
4334 return NewMBB;
4335}
4336
Richard Sandiford5e318f02013-08-27 09:54:29 +00004337// Force base value Base into a register before MI. Return the register.
4338static unsigned forceReg(MachineInstr *MI, MachineOperand &Base,
4339 const SystemZInstrInfo *TII) {
4340 if (Base.isReg())
4341 return Base.getReg();
4342
4343 MachineBasicBlock *MBB = MI->getParent();
4344 MachineFunction &MF = *MBB->getParent();
4345 MachineRegisterInfo &MRI = MF.getRegInfo();
4346
4347 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
4348 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg)
4349 .addOperand(Base).addImm(0).addReg(0);
4350 return Reg;
4351}
4352
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004353// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
4354MachineBasicBlock *
4355SystemZTargetLowering::emitSelect(MachineInstr *MI,
4356 MachineBasicBlock *MBB) const {
Eric Christophera6734172015-01-31 00:06:45 +00004357 const SystemZInstrInfo *TII =
4358 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004359
4360 unsigned DestReg = MI->getOperand(0).getReg();
4361 unsigned TrueReg = MI->getOperand(1).getReg();
4362 unsigned FalseReg = MI->getOperand(2).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00004363 unsigned CCValid = MI->getOperand(3).getImm();
4364 unsigned CCMask = MI->getOperand(4).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004365 DebugLoc DL = MI->getDebugLoc();
4366
4367 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004368 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004369 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
4370
4371 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00004372 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004373 // # fallthrough to FalseMBB
4374 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00004375 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4376 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004377 MBB->addSuccessor(JoinMBB);
4378 MBB->addSuccessor(FalseMBB);
4379
4380 // FalseMBB:
4381 // # fallthrough to JoinMBB
4382 MBB = FalseMBB;
4383 MBB->addSuccessor(JoinMBB);
4384
4385 // JoinMBB:
4386 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
4387 // ...
4388 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004389 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004390 .addReg(TrueReg).addMBB(StartMBB)
4391 .addReg(FalseReg).addMBB(FalseMBB);
4392
4393 MI->eraseFromParent();
4394 return JoinMBB;
4395}
4396
Richard Sandifordb86a8342013-06-27 09:27:40 +00004397// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
4398// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004399// happen when the condition is false rather than true. If a STORE ON
4400// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Richard Sandifordb86a8342013-06-27 09:27:40 +00004401MachineBasicBlock *
4402SystemZTargetLowering::emitCondStore(MachineInstr *MI,
4403 MachineBasicBlock *MBB,
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004404 unsigned StoreOpcode, unsigned STOCOpcode,
4405 bool Invert) const {
Eric Christophera6734172015-01-31 00:06:45 +00004406 const SystemZInstrInfo *TII =
4407 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordb86a8342013-06-27 09:27:40 +00004408
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004409 unsigned SrcReg = MI->getOperand(0).getReg();
4410 MachineOperand Base = MI->getOperand(1);
4411 int64_t Disp = MI->getOperand(2).getImm();
4412 unsigned IndexReg = MI->getOperand(3).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00004413 unsigned CCValid = MI->getOperand(4).getImm();
4414 unsigned CCMask = MI->getOperand(5).getImm();
Richard Sandifordb86a8342013-06-27 09:27:40 +00004415 DebugLoc DL = MI->getDebugLoc();
4416
4417 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
4418
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004419 // Use STOCOpcode if possible. We could use different store patterns in
4420 // order to avoid matching the index register, but the performance trade-offs
4421 // might be more complicated in that case.
Eric Christopher93bf97c2014-06-27 07:38:01 +00004422 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004423 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00004424 CCMask ^= CCValid;
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004425 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +00004426 .addReg(SrcReg).addOperand(Base).addImm(Disp)
4427 .addImm(CCValid).addImm(CCMask);
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004428 MI->eraseFromParent();
4429 return MBB;
4430 }
4431
Richard Sandifordb86a8342013-06-27 09:27:40 +00004432 // Get the condition needed to branch around the store.
4433 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00004434 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00004435
4436 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004437 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00004438 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
4439
4440 // StartMBB:
4441 // BRC CCMask, JoinMBB
4442 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00004443 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00004444 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4445 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00004446 MBB->addSuccessor(JoinMBB);
4447 MBB->addSuccessor(FalseMBB);
4448
4449 // FalseMBB:
4450 // store %SrcReg, %Disp(%Index,%Base)
4451 // # fallthrough to JoinMBB
4452 MBB = FalseMBB;
4453 BuildMI(MBB, DL, TII->get(StoreOpcode))
4454 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
4455 MBB->addSuccessor(JoinMBB);
4456
4457 MI->eraseFromParent();
4458 return JoinMBB;
4459}
4460
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004461// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
4462// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
4463// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
4464// BitSize is the width of the field in bits, or 0 if this is a partword
4465// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
4466// is one of the operands. Invert says whether the field should be
4467// inverted after performing BinOpcode (e.g. for NAND).
4468MachineBasicBlock *
4469SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI,
4470 MachineBasicBlock *MBB,
4471 unsigned BinOpcode,
4472 unsigned BitSize,
4473 bool Invert) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004474 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00004475 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00004476 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004477 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004478 bool IsSubWord = (BitSize < 32);
4479
4480 // Extract the operands. Base can be a register or a frame index.
4481 // Src2 can be a register or immediate.
4482 unsigned Dest = MI->getOperand(0).getReg();
4483 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
4484 int64_t Disp = MI->getOperand(2).getImm();
4485 MachineOperand Src2 = earlyUseOperand(MI->getOperand(3));
4486 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
4487 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
4488 DebugLoc DL = MI->getDebugLoc();
4489 if (IsSubWord)
4490 BitSize = MI->getOperand(6).getImm();
4491
4492 // Subword operations use 32-bit registers.
4493 const TargetRegisterClass *RC = (BitSize <= 32 ?
4494 &SystemZ::GR32BitRegClass :
4495 &SystemZ::GR64BitRegClass);
4496 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
4497 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
4498
4499 // Get the right opcodes for the displacement.
4500 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
4501 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
4502 assert(LOpcode && CSOpcode && "Displacement out of range");
4503
4504 // Create virtual registers for temporary results.
4505 unsigned OrigVal = MRI.createVirtualRegister(RC);
4506 unsigned OldVal = MRI.createVirtualRegister(RC);
4507 unsigned NewVal = (BinOpcode || IsSubWord ?
4508 MRI.createVirtualRegister(RC) : Src2.getReg());
4509 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
4510 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
4511
4512 // Insert a basic block for the main loop.
4513 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004514 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004515 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
4516
4517 // StartMBB:
4518 // ...
4519 // %OrigVal = L Disp(%Base)
4520 // # fall through to LoopMMB
4521 MBB = StartMBB;
4522 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
4523 .addOperand(Base).addImm(Disp).addReg(0);
4524 MBB->addSuccessor(LoopMBB);
4525
4526 // LoopMBB:
4527 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
4528 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
4529 // %RotatedNewVal = OP %RotatedOldVal, %Src2
4530 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
4531 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
4532 // JNE LoopMBB
4533 // # fall through to DoneMMB
4534 MBB = LoopMBB;
4535 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
4536 .addReg(OrigVal).addMBB(StartMBB)
4537 .addReg(Dest).addMBB(LoopMBB);
4538 if (IsSubWord)
4539 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
4540 .addReg(OldVal).addReg(BitShift).addImm(0);
4541 if (Invert) {
4542 // Perform the operation normally and then invert every bit of the field.
4543 unsigned Tmp = MRI.createVirtualRegister(RC);
4544 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
4545 .addReg(RotatedOldVal).addOperand(Src2);
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00004546 if (BitSize <= 32)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004547 // XILF with the upper BitSize bits set.
Richard Sandiford652784e2013-09-25 11:11:53 +00004548 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00004549 .addReg(Tmp).addImm(-1U << (32 - BitSize));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004550 else {
4551 // Use LCGR and add -1 to the result, which is more compact than
4552 // an XILF, XILH pair.
4553 unsigned Tmp2 = MRI.createVirtualRegister(RC);
4554 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
4555 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
4556 .addReg(Tmp2).addImm(-1);
4557 }
4558 } else if (BinOpcode)
4559 // A simply binary operation.
4560 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
4561 .addReg(RotatedOldVal).addOperand(Src2);
4562 else if (IsSubWord)
4563 // Use RISBG to rotate Src2 into position and use it to replace the
4564 // field in RotatedOldVal.
4565 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
4566 .addReg(RotatedOldVal).addReg(Src2.getReg())
4567 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
4568 if (IsSubWord)
4569 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
4570 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
4571 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
4572 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00004573 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4574 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004575 MBB->addSuccessor(LoopMBB);
4576 MBB->addSuccessor(DoneMBB);
4577
4578 MI->eraseFromParent();
4579 return DoneMBB;
4580}
4581
4582// Implement EmitInstrWithCustomInserter for pseudo
4583// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
4584// instruction that should be used to compare the current field with the
4585// minimum or maximum value. KeepOldMask is the BRC condition-code mask
4586// for when the current field should be kept. BitSize is the width of
4587// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
4588MachineBasicBlock *
4589SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI,
4590 MachineBasicBlock *MBB,
4591 unsigned CompareOpcode,
4592 unsigned KeepOldMask,
4593 unsigned BitSize) 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 unsigned Dest = MI->getOperand(0).getReg();
4602 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
4603 int64_t Disp = MI->getOperand(2).getImm();
4604 unsigned Src2 = MI->getOperand(3).getReg();
4605 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
4606 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
4607 DebugLoc DL = MI->getDebugLoc();
4608 if (IsSubWord)
4609 BitSize = MI->getOperand(6).getImm();
4610
4611 // Subword operations use 32-bit registers.
4612 const TargetRegisterClass *RC = (BitSize <= 32 ?
4613 &SystemZ::GR32BitRegClass :
4614 &SystemZ::GR64BitRegClass);
4615 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
4616 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
4617
4618 // Get the right opcodes for the displacement.
4619 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
4620 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
4621 assert(LOpcode && CSOpcode && "Displacement out of range");
4622
4623 // Create virtual registers for temporary results.
4624 unsigned OrigVal = MRI.createVirtualRegister(RC);
4625 unsigned OldVal = MRI.createVirtualRegister(RC);
4626 unsigned NewVal = MRI.createVirtualRegister(RC);
4627 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
4628 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
4629 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
4630
4631 // Insert 3 basic blocks for the loop.
4632 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004633 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004634 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
4635 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
4636 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
4637
4638 // StartMBB:
4639 // ...
4640 // %OrigVal = L Disp(%Base)
4641 // # fall through to LoopMMB
4642 MBB = StartMBB;
4643 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
4644 .addOperand(Base).addImm(Disp).addReg(0);
4645 MBB->addSuccessor(LoopMBB);
4646
4647 // LoopMBB:
4648 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
4649 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
4650 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00004651 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004652 MBB = LoopMBB;
4653 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
4654 .addReg(OrigVal).addMBB(StartMBB)
4655 .addReg(Dest).addMBB(UpdateMBB);
4656 if (IsSubWord)
4657 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
4658 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00004659 BuildMI(MBB, DL, TII->get(CompareOpcode))
4660 .addReg(RotatedOldVal).addReg(Src2);
4661 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00004662 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004663 MBB->addSuccessor(UpdateMBB);
4664 MBB->addSuccessor(UseAltMBB);
4665
4666 // UseAltMBB:
4667 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
4668 // # fall through to UpdateMMB
4669 MBB = UseAltMBB;
4670 if (IsSubWord)
4671 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
4672 .addReg(RotatedOldVal).addReg(Src2)
4673 .addImm(32).addImm(31 + BitSize).addImm(0);
4674 MBB->addSuccessor(UpdateMBB);
4675
4676 // UpdateMBB:
4677 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
4678 // [ %RotatedAltVal, UseAltMBB ]
4679 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
4680 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
4681 // JNE LoopMBB
4682 // # fall through to DoneMMB
4683 MBB = UpdateMBB;
4684 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
4685 .addReg(RotatedOldVal).addMBB(LoopMBB)
4686 .addReg(RotatedAltVal).addMBB(UseAltMBB);
4687 if (IsSubWord)
4688 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
4689 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
4690 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
4691 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00004692 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4693 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004694 MBB->addSuccessor(LoopMBB);
4695 MBB->addSuccessor(DoneMBB);
4696
4697 MI->eraseFromParent();
4698 return DoneMBB;
4699}
4700
4701// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
4702// instruction MI.
4703MachineBasicBlock *
4704SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI,
4705 MachineBasicBlock *MBB) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004706 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00004707 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00004708 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004709 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004710
4711 // Extract the operands. Base can be a register or a frame index.
4712 unsigned Dest = MI->getOperand(0).getReg();
4713 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
4714 int64_t Disp = MI->getOperand(2).getImm();
4715 unsigned OrigCmpVal = MI->getOperand(3).getReg();
4716 unsigned OrigSwapVal = MI->getOperand(4).getReg();
4717 unsigned BitShift = MI->getOperand(5).getReg();
4718 unsigned NegBitShift = MI->getOperand(6).getReg();
4719 int64_t BitSize = MI->getOperand(7).getImm();
4720 DebugLoc DL = MI->getDebugLoc();
4721
4722 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
4723
4724 // Get the right opcodes for the displacement.
4725 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
4726 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
4727 assert(LOpcode && CSOpcode && "Displacement out of range");
4728
4729 // Create virtual registers for temporary results.
4730 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
4731 unsigned OldVal = MRI.createVirtualRegister(RC);
4732 unsigned CmpVal = MRI.createVirtualRegister(RC);
4733 unsigned SwapVal = MRI.createVirtualRegister(RC);
4734 unsigned StoreVal = MRI.createVirtualRegister(RC);
4735 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
4736 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
4737 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
4738
4739 // Insert 2 basic blocks for the loop.
4740 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004741 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004742 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
4743 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
4744
4745 // StartMBB:
4746 // ...
4747 // %OrigOldVal = L Disp(%Base)
4748 // # fall through to LoopMMB
4749 MBB = StartMBB;
4750 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
4751 .addOperand(Base).addImm(Disp).addReg(0);
4752 MBB->addSuccessor(LoopMBB);
4753
4754 // LoopMBB:
4755 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
4756 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
4757 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
4758 // %Dest = RLL %OldVal, BitSize(%BitShift)
4759 // ^^ The low BitSize bits contain the field
4760 // of interest.
4761 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
4762 // ^^ Replace the upper 32-BitSize bits of the
4763 // comparison value with those that we loaded,
4764 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00004765 // CR %Dest, %RetryCmpVal
4766 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004767 // # Fall through to SetMBB
4768 MBB = LoopMBB;
4769 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
4770 .addReg(OrigOldVal).addMBB(StartMBB)
4771 .addReg(RetryOldVal).addMBB(SetMBB);
4772 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
4773 .addReg(OrigCmpVal).addMBB(StartMBB)
4774 .addReg(RetryCmpVal).addMBB(SetMBB);
4775 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
4776 .addReg(OrigSwapVal).addMBB(StartMBB)
4777 .addReg(RetrySwapVal).addMBB(SetMBB);
4778 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
4779 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
4780 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
4781 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00004782 BuildMI(MBB, DL, TII->get(SystemZ::CR))
4783 .addReg(Dest).addReg(RetryCmpVal);
4784 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00004785 .addImm(SystemZ::CCMASK_ICMP)
4786 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004787 MBB->addSuccessor(DoneMBB);
4788 MBB->addSuccessor(SetMBB);
4789
4790 // SetMBB:
4791 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
4792 // ^^ Replace the upper 32-BitSize bits of the new
4793 // value with those that we loaded.
4794 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
4795 // ^^ Rotate the new field to its proper position.
4796 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
4797 // JNE LoopMBB
4798 // # fall through to ExitMMB
4799 MBB = SetMBB;
4800 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
4801 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
4802 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
4803 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
4804 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
4805 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00004806 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4807 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004808 MBB->addSuccessor(LoopMBB);
4809 MBB->addSuccessor(DoneMBB);
4810
4811 MI->eraseFromParent();
4812 return DoneMBB;
4813}
4814
4815// Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true
4816// if the high register of the GR128 value must be cleared or false if
Richard Sandiford87a44362013-09-30 10:28:35 +00004817// it's "don't care". SubReg is subreg_l32 when extending a GR32
4818// and subreg_l64 when extending a GR64.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004819MachineBasicBlock *
4820SystemZTargetLowering::emitExt128(MachineInstr *MI,
4821 MachineBasicBlock *MBB,
4822 bool ClearEven, unsigned SubReg) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004823 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00004824 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00004825 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004826 MachineRegisterInfo &MRI = MF.getRegInfo();
4827 DebugLoc DL = MI->getDebugLoc();
4828
4829 unsigned Dest = MI->getOperand(0).getReg();
4830 unsigned Src = MI->getOperand(1).getReg();
4831 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
4832
4833 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
4834 if (ClearEven) {
4835 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
4836 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
4837
4838 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
4839 .addImm(0);
4840 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
Richard Sandiford87a44362013-09-30 10:28:35 +00004841 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004842 In128 = NewIn128;
4843 }
4844 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
4845 .addReg(In128).addReg(Src).addImm(SubReg);
4846
4847 MI->eraseFromParent();
4848 return MBB;
4849}
4850
Richard Sandifordd131ff82013-07-08 09:35:23 +00004851MachineBasicBlock *
Richard Sandiford564681c2013-08-12 10:28:10 +00004852SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI,
4853 MachineBasicBlock *MBB,
4854 unsigned Opcode) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +00004855 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00004856 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00004857 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandiford5e318f02013-08-27 09:54:29 +00004858 MachineRegisterInfo &MRI = MF.getRegInfo();
Richard Sandifordd131ff82013-07-08 09:35:23 +00004859 DebugLoc DL = MI->getDebugLoc();
4860
Richard Sandiford5e318f02013-08-27 09:54:29 +00004861 MachineOperand DestBase = earlyUseOperand(MI->getOperand(0));
Richard Sandifordd131ff82013-07-08 09:35:23 +00004862 uint64_t DestDisp = MI->getOperand(1).getImm();
Richard Sandiford5e318f02013-08-27 09:54:29 +00004863 MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2));
Richard Sandifordd131ff82013-07-08 09:35:23 +00004864 uint64_t SrcDisp = MI->getOperand(3).getImm();
4865 uint64_t Length = MI->getOperand(4).getImm();
4866
Richard Sandifordbe133a82013-08-28 09:01:51 +00004867 // When generating more than one CLC, all but the last will need to
4868 // branch to the end when a difference is found.
4869 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
Craig Topper062a2ba2014-04-25 05:30:21 +00004870 splitBlockAfter(MI, MBB) : nullptr);
Richard Sandifordbe133a82013-08-28 09:01:51 +00004871
Richard Sandiford5e318f02013-08-27 09:54:29 +00004872 // Check for the loop form, in which operand 5 is the trip count.
4873 if (MI->getNumExplicitOperands() > 5) {
4874 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
4875
4876 uint64_t StartCountReg = MI->getOperand(5).getReg();
4877 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
4878 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
4879 forceReg(MI, DestBase, TII));
4880
4881 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
4882 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
4883 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
4884 MRI.createVirtualRegister(RC));
4885 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
4886 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
4887 MRI.createVirtualRegister(RC));
4888
4889 RC = &SystemZ::GR64BitRegClass;
4890 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
4891 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
4892
4893 MachineBasicBlock *StartMBB = MBB;
4894 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
4895 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00004896 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004897
4898 // StartMBB:
4899 // # fall through to LoopMMB
4900 MBB->addSuccessor(LoopMBB);
4901
4902 // LoopMBB:
4903 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00004904 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00004905 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00004906 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00004907 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00004908 // [ %NextCountReg, NextMBB ]
4909 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00004910 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00004911 // ( JLH EndMBB )
4912 //
4913 // The prefetch is used only for MVC. The JLH is used only for CLC.
4914 MBB = LoopMBB;
4915
4916 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
4917 .addReg(StartDestReg).addMBB(StartMBB)
4918 .addReg(NextDestReg).addMBB(NextMBB);
4919 if (!HaveSingleBase)
4920 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
4921 .addReg(StartSrcReg).addMBB(StartMBB)
4922 .addReg(NextSrcReg).addMBB(NextMBB);
4923 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
4924 .addReg(StartCountReg).addMBB(StartMBB)
4925 .addReg(NextCountReg).addMBB(NextMBB);
4926 if (Opcode == SystemZ::MVC)
4927 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
4928 .addImm(SystemZ::PFD_WRITE)
4929 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
4930 BuildMI(MBB, DL, TII->get(Opcode))
4931 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
4932 .addReg(ThisSrcReg).addImm(SrcDisp);
4933 if (EndMBB) {
4934 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4935 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
4936 .addMBB(EndMBB);
4937 MBB->addSuccessor(EndMBB);
4938 MBB->addSuccessor(NextMBB);
4939 }
4940
4941 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00004942 // %NextDestReg = LA 256(%ThisDestReg)
4943 // %NextSrcReg = LA 256(%ThisSrcReg)
4944 // %NextCountReg = AGHI %ThisCountReg, -1
4945 // CGHI %NextCountReg, 0
4946 // JLH LoopMBB
4947 // # fall through to DoneMMB
4948 //
4949 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00004950 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004951
Richard Sandiford5e318f02013-08-27 09:54:29 +00004952 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
4953 .addReg(ThisDestReg).addImm(256).addReg(0);
4954 if (!HaveSingleBase)
4955 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
4956 .addReg(ThisSrcReg).addImm(256).addReg(0);
4957 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
4958 .addReg(ThisCountReg).addImm(-1);
4959 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
4960 .addReg(NextCountReg).addImm(0);
4961 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4962 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
4963 .addMBB(LoopMBB);
4964 MBB->addSuccessor(LoopMBB);
4965 MBB->addSuccessor(DoneMBB);
4966
4967 DestBase = MachineOperand::CreateReg(NextDestReg, false);
4968 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
4969 Length &= 255;
4970 MBB = DoneMBB;
4971 }
4972 // Handle any remaining bytes with straight-line code.
4973 while (Length > 0) {
4974 uint64_t ThisLength = std::min(Length, uint64_t(256));
4975 // The previous iteration might have created out-of-range displacements.
4976 // Apply them using LAY if so.
4977 if (!isUInt<12>(DestDisp)) {
4978 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
4979 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
4980 .addOperand(DestBase).addImm(DestDisp).addReg(0);
4981 DestBase = MachineOperand::CreateReg(Reg, false);
4982 DestDisp = 0;
4983 }
4984 if (!isUInt<12>(SrcDisp)) {
4985 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
4986 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
4987 .addOperand(SrcBase).addImm(SrcDisp).addReg(0);
4988 SrcBase = MachineOperand::CreateReg(Reg, false);
4989 SrcDisp = 0;
4990 }
4991 BuildMI(*MBB, MI, DL, TII->get(Opcode))
4992 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength)
4993 .addOperand(SrcBase).addImm(SrcDisp);
4994 DestDisp += ThisLength;
4995 SrcDisp += ThisLength;
4996 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00004997 // If there's another CLC to go, branch to the end if a difference
4998 // was found.
4999 if (EndMBB && Length > 0) {
5000 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
5001 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5002 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5003 .addMBB(EndMBB);
5004 MBB->addSuccessor(EndMBB);
5005 MBB->addSuccessor(NextMBB);
5006 MBB = NextMBB;
5007 }
5008 }
5009 if (EndMBB) {
5010 MBB->addSuccessor(EndMBB);
5011 MBB = EndMBB;
5012 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005013 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00005014
5015 MI->eraseFromParent();
5016 return MBB;
5017}
5018
Richard Sandifordca232712013-08-16 11:21:54 +00005019// Decompose string pseudo-instruction MI into a loop that continually performs
5020// Opcode until CC != 3.
5021MachineBasicBlock *
5022SystemZTargetLowering::emitStringWrapper(MachineInstr *MI,
5023 MachineBasicBlock *MBB,
5024 unsigned Opcode) const {
Richard Sandifordca232712013-08-16 11:21:54 +00005025 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005026 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005027 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordca232712013-08-16 11:21:54 +00005028 MachineRegisterInfo &MRI = MF.getRegInfo();
5029 DebugLoc DL = MI->getDebugLoc();
5030
5031 uint64_t End1Reg = MI->getOperand(0).getReg();
5032 uint64_t Start1Reg = MI->getOperand(1).getReg();
5033 uint64_t Start2Reg = MI->getOperand(2).getReg();
5034 uint64_t CharReg = MI->getOperand(3).getReg();
5035
5036 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
5037 uint64_t This1Reg = MRI.createVirtualRegister(RC);
5038 uint64_t This2Reg = MRI.createVirtualRegister(RC);
5039 uint64_t End2Reg = MRI.createVirtualRegister(RC);
5040
5041 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005042 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00005043 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5044
5045 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00005046 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00005047 MBB->addSuccessor(LoopMBB);
5048
5049 // LoopMBB:
5050 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
5051 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford7789b082013-09-30 08:48:38 +00005052 // R0L = %CharReg
5053 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
Richard Sandifordca232712013-08-16 11:21:54 +00005054 // JO LoopMBB
5055 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00005056 //
Richard Sandiford7789b082013-09-30 08:48:38 +00005057 // The load of R0L can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00005058 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00005059
5060 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
5061 .addReg(Start1Reg).addMBB(StartMBB)
5062 .addReg(End1Reg).addMBB(LoopMBB);
5063 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
5064 .addReg(Start2Reg).addMBB(StartMBB)
5065 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford7789b082013-09-30 08:48:38 +00005066 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00005067 BuildMI(MBB, DL, TII->get(Opcode))
5068 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
5069 .addReg(This1Reg).addReg(This2Reg);
5070 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5071 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
5072 MBB->addSuccessor(LoopMBB);
5073 MBB->addSuccessor(DoneMBB);
5074
5075 DoneMBB->addLiveIn(SystemZ::CC);
5076
5077 MI->eraseFromParent();
5078 return DoneMBB;
5079}
5080
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005081// Update TBEGIN instruction with final opcode and register clobbers.
5082MachineBasicBlock *
5083SystemZTargetLowering::emitTransactionBegin(MachineInstr *MI,
5084 MachineBasicBlock *MBB,
5085 unsigned Opcode,
5086 bool NoFloat) const {
5087 MachineFunction &MF = *MBB->getParent();
5088 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
5089 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
5090
5091 // Update opcode.
5092 MI->setDesc(TII->get(Opcode));
5093
5094 // We cannot handle a TBEGIN that clobbers the stack or frame pointer.
5095 // Make sure to add the corresponding GRSM bits if they are missing.
5096 uint64_t Control = MI->getOperand(2).getImm();
5097 static const unsigned GPRControlBit[16] = {
5098 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000,
5099 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100
5100 };
5101 Control |= GPRControlBit[15];
5102 if (TFI->hasFP(MF))
5103 Control |= GPRControlBit[11];
5104 MI->getOperand(2).setImm(Control);
5105
5106 // Add GPR clobbers.
5107 for (int I = 0; I < 16; I++) {
5108 if ((Control & GPRControlBit[I]) == 0) {
5109 unsigned Reg = SystemZMC::GR64Regs[I];
5110 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5111 }
5112 }
5113
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005114 // Add FPR/VR clobbers.
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005115 if (!NoFloat && (Control & 4) != 0) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005116 if (Subtarget.hasVector()) {
5117 for (int I = 0; I < 32; I++) {
5118 unsigned Reg = SystemZMC::VR128Regs[I];
5119 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5120 }
5121 } else {
5122 for (int I = 0; I < 16; I++) {
5123 unsigned Reg = SystemZMC::FP64Regs[I];
5124 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5125 }
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005126 }
5127 }
5128
5129 return MBB;
5130}
5131
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005132MachineBasicBlock *SystemZTargetLowering::
5133EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const {
5134 switch (MI->getOpcode()) {
Richard Sandiford7c5c0ea2013-10-01 13:10:16 +00005135 case SystemZ::Select32Mux:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005136 case SystemZ::Select32:
5137 case SystemZ::SelectF32:
5138 case SystemZ::Select64:
5139 case SystemZ::SelectF64:
5140 case SystemZ::SelectF128:
5141 return emitSelect(MI, MBB);
5142
Richard Sandiford2896d042013-10-01 14:33:55 +00005143 case SystemZ::CondStore8Mux:
5144 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
5145 case SystemZ::CondStore8MuxInv:
5146 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
5147 case SystemZ::CondStore16Mux:
5148 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
5149 case SystemZ::CondStore16MuxInv:
5150 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005151 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005152 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005153 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005154 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005155 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005156 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005157 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005158 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005159 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005160 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005161 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005162 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005163 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005164 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005165 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005166 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005167 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005168 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005169 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005170 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005171 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005172 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005173 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005174 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005175
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005176 case SystemZ::AEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00005177 return emitExt128(MI, MBB, false, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005178 case SystemZ::ZEXT128_32:
Richard Sandiford87a44362013-09-30 10:28:35 +00005179 return emitExt128(MI, MBB, true, SystemZ::subreg_l32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005180 case SystemZ::ZEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00005181 return emitExt128(MI, MBB, true, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005182
5183 case SystemZ::ATOMIC_SWAPW:
5184 return emitAtomicLoadBinary(MI, MBB, 0, 0);
5185 case SystemZ::ATOMIC_SWAP_32:
5186 return emitAtomicLoadBinary(MI, MBB, 0, 32);
5187 case SystemZ::ATOMIC_SWAP_64:
5188 return emitAtomicLoadBinary(MI, MBB, 0, 64);
5189
5190 case SystemZ::ATOMIC_LOADW_AR:
5191 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
5192 case SystemZ::ATOMIC_LOADW_AFI:
5193 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
5194 case SystemZ::ATOMIC_LOAD_AR:
5195 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
5196 case SystemZ::ATOMIC_LOAD_AHI:
5197 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
5198 case SystemZ::ATOMIC_LOAD_AFI:
5199 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
5200 case SystemZ::ATOMIC_LOAD_AGR:
5201 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
5202 case SystemZ::ATOMIC_LOAD_AGHI:
5203 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
5204 case SystemZ::ATOMIC_LOAD_AGFI:
5205 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
5206
5207 case SystemZ::ATOMIC_LOADW_SR:
5208 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
5209 case SystemZ::ATOMIC_LOAD_SR:
5210 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
5211 case SystemZ::ATOMIC_LOAD_SGR:
5212 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
5213
5214 case SystemZ::ATOMIC_LOADW_NR:
5215 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
5216 case SystemZ::ATOMIC_LOADW_NILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00005217 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005218 case SystemZ::ATOMIC_LOAD_NR:
5219 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005220 case SystemZ::ATOMIC_LOAD_NILL:
5221 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
5222 case SystemZ::ATOMIC_LOAD_NILH:
5223 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
5224 case SystemZ::ATOMIC_LOAD_NILF:
5225 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005226 case SystemZ::ATOMIC_LOAD_NGR:
5227 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005228 case SystemZ::ATOMIC_LOAD_NILL64:
5229 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
5230 case SystemZ::ATOMIC_LOAD_NILH64:
5231 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00005232 case SystemZ::ATOMIC_LOAD_NIHL64:
5233 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
5234 case SystemZ::ATOMIC_LOAD_NIHH64:
5235 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005236 case SystemZ::ATOMIC_LOAD_NILF64:
5237 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00005238 case SystemZ::ATOMIC_LOAD_NIHF64:
5239 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005240
5241 case SystemZ::ATOMIC_LOADW_OR:
5242 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
5243 case SystemZ::ATOMIC_LOADW_OILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00005244 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005245 case SystemZ::ATOMIC_LOAD_OR:
5246 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005247 case SystemZ::ATOMIC_LOAD_OILL:
5248 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
5249 case SystemZ::ATOMIC_LOAD_OILH:
5250 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
5251 case SystemZ::ATOMIC_LOAD_OILF:
5252 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005253 case SystemZ::ATOMIC_LOAD_OGR:
5254 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005255 case SystemZ::ATOMIC_LOAD_OILL64:
5256 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
5257 case SystemZ::ATOMIC_LOAD_OILH64:
5258 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00005259 case SystemZ::ATOMIC_LOAD_OIHL64:
5260 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
5261 case SystemZ::ATOMIC_LOAD_OIHH64:
5262 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005263 case SystemZ::ATOMIC_LOAD_OILF64:
5264 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00005265 case SystemZ::ATOMIC_LOAD_OIHF64:
5266 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005267
5268 case SystemZ::ATOMIC_LOADW_XR:
5269 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
5270 case SystemZ::ATOMIC_LOADW_XILF:
Richard Sandiford652784e2013-09-25 11:11:53 +00005271 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005272 case SystemZ::ATOMIC_LOAD_XR:
5273 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005274 case SystemZ::ATOMIC_LOAD_XILF:
5275 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005276 case SystemZ::ATOMIC_LOAD_XGR:
5277 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005278 case SystemZ::ATOMIC_LOAD_XILF64:
5279 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
Richard Sandiford5718dac2013-10-01 14:08:44 +00005280 case SystemZ::ATOMIC_LOAD_XIHF64:
5281 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005282
5283 case SystemZ::ATOMIC_LOADW_NRi:
5284 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
5285 case SystemZ::ATOMIC_LOADW_NILHi:
Richard Sandiford652784e2013-09-25 11:11:53 +00005286 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005287 case SystemZ::ATOMIC_LOAD_NRi:
5288 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005289 case SystemZ::ATOMIC_LOAD_NILLi:
5290 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
5291 case SystemZ::ATOMIC_LOAD_NILHi:
5292 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
5293 case SystemZ::ATOMIC_LOAD_NILFi:
5294 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005295 case SystemZ::ATOMIC_LOAD_NGRi:
5296 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005297 case SystemZ::ATOMIC_LOAD_NILL64i:
5298 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
5299 case SystemZ::ATOMIC_LOAD_NILH64i:
5300 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00005301 case SystemZ::ATOMIC_LOAD_NIHL64i:
5302 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
5303 case SystemZ::ATOMIC_LOAD_NIHH64i:
5304 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005305 case SystemZ::ATOMIC_LOAD_NILF64i:
5306 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00005307 case SystemZ::ATOMIC_LOAD_NIHF64i:
5308 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005309
5310 case SystemZ::ATOMIC_LOADW_MIN:
5311 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5312 SystemZ::CCMASK_CMP_LE, 0);
5313 case SystemZ::ATOMIC_LOAD_MIN_32:
5314 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5315 SystemZ::CCMASK_CMP_LE, 32);
5316 case SystemZ::ATOMIC_LOAD_MIN_64:
5317 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
5318 SystemZ::CCMASK_CMP_LE, 64);
5319
5320 case SystemZ::ATOMIC_LOADW_MAX:
5321 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5322 SystemZ::CCMASK_CMP_GE, 0);
5323 case SystemZ::ATOMIC_LOAD_MAX_32:
5324 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5325 SystemZ::CCMASK_CMP_GE, 32);
5326 case SystemZ::ATOMIC_LOAD_MAX_64:
5327 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
5328 SystemZ::CCMASK_CMP_GE, 64);
5329
5330 case SystemZ::ATOMIC_LOADW_UMIN:
5331 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5332 SystemZ::CCMASK_CMP_LE, 0);
5333 case SystemZ::ATOMIC_LOAD_UMIN_32:
5334 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5335 SystemZ::CCMASK_CMP_LE, 32);
5336 case SystemZ::ATOMIC_LOAD_UMIN_64:
5337 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
5338 SystemZ::CCMASK_CMP_LE, 64);
5339
5340 case SystemZ::ATOMIC_LOADW_UMAX:
5341 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5342 SystemZ::CCMASK_CMP_GE, 0);
5343 case SystemZ::ATOMIC_LOAD_UMAX_32:
5344 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5345 SystemZ::CCMASK_CMP_GE, 32);
5346 case SystemZ::ATOMIC_LOAD_UMAX_64:
5347 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
5348 SystemZ::CCMASK_CMP_GE, 64);
5349
5350 case SystemZ::ATOMIC_CMP_SWAPW:
5351 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005352 case SystemZ::MVCSequence:
5353 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00005354 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00005355 case SystemZ::NCSequence:
5356 case SystemZ::NCLoop:
5357 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
5358 case SystemZ::OCSequence:
5359 case SystemZ::OCLoop:
5360 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
5361 case SystemZ::XCSequence:
5362 case SystemZ::XCLoop:
5363 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005364 case SystemZ::CLCSequence:
5365 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00005366 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00005367 case SystemZ::CLSTLoop:
5368 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00005369 case SystemZ::MVSTLoop:
5370 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00005371 case SystemZ::SRSTLoop:
5372 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005373 case SystemZ::TBEGIN:
5374 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false);
5375 case SystemZ::TBEGIN_nofloat:
5376 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true);
5377 case SystemZ::TBEGINC:
5378 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005379 default:
5380 llvm_unreachable("Unexpected instr type to insert");
5381 }
5382}