blob: a670185c378ddc948086b39ab726d02c9a3bac7d [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
Mehdi Amini44ede332015-07-09 02:09:04 +000084SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM,
Eric Christophera6734172015-01-31 00:06:45 +000085 const SystemZSubtarget &STI)
Mehdi Amini44ede332015-07-09 02:09:04 +000086 : TargetLowering(TM), Subtarget(STI) {
Mehdi Amini26d48132015-07-24 16:04:22 +000087 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
Ulrich Weigand5f613df2013-05-06 16:15:19 +000088
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 Weigand49506d72015-05-05 19:28:34 +000094 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
95 if (Subtarget.hasVector()) {
96 addRegisterClass(MVT::f32, &SystemZ::VR32BitRegClass);
97 addRegisterClass(MVT::f64, &SystemZ::VR64BitRegClass);
98 } else {
99 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
100 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
101 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000102 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
103
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000104 if (Subtarget.hasVector()) {
105 addRegisterClass(MVT::v16i8, &SystemZ::VR128BitRegClass);
106 addRegisterClass(MVT::v8i16, &SystemZ::VR128BitRegClass);
107 addRegisterClass(MVT::v4i32, &SystemZ::VR128BitRegClass);
108 addRegisterClass(MVT::v2i64, &SystemZ::VR128BitRegClass);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000109 addRegisterClass(MVT::v4f32, &SystemZ::VR128BitRegClass);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000110 addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000111 }
112
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000113 // Compute derived properties from the register classes
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000114 computeRegisterProperties(Subtarget.getRegisterInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000115
116 // Set up special registers.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000117 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
118
119 // TODO: It may be better to default to latency-oriented scheduling, however
120 // LLVM's current latency-oriented scheduler can't handle physreg definitions
Richard Sandiford14a44492013-05-22 13:38:45 +0000121 // such as SystemZ has with CC, so set this to the register-pressure
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000122 // scheduler, because it can.
123 setSchedulingPreference(Sched::RegPressure);
124
125 setBooleanContents(ZeroOrOneBooleanContent);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000126 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000127
128 // Instructions are strings of 2-byte aligned 2-byte values.
129 setMinFunctionAlignment(2);
130
131 // Handle operations that are handled in a similar way for all types.
132 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
133 I <= MVT::LAST_FP_VALUETYPE;
134 ++I) {
135 MVT VT = MVT::SimpleValueType(I);
136 if (isTypeLegal(VT)) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000137 // Lower SET_CC into an IPM-based sequence.
138 setOperationAction(ISD::SETCC, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000139
140 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
141 setOperationAction(ISD::SELECT, VT, Expand);
142
143 // Lower SELECT_CC and BR_CC into separate comparisons and branches.
144 setOperationAction(ISD::SELECT_CC, VT, Custom);
145 setOperationAction(ISD::BR_CC, VT, Custom);
146 }
147 }
148
149 // Expand jump table branches as address arithmetic followed by an
150 // indirect jump.
151 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
152
153 // Expand BRCOND into a BR_CC (see above).
154 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
155
156 // Handle integer types.
157 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
158 I <= MVT::LAST_INTEGER_VALUETYPE;
159 ++I) {
160 MVT VT = MVT::SimpleValueType(I);
161 if (isTypeLegal(VT)) {
162 // Expand individual DIV and REMs into DIVREMs.
163 setOperationAction(ISD::SDIV, VT, Expand);
164 setOperationAction(ISD::UDIV, VT, Expand);
165 setOperationAction(ISD::SREM, VT, Expand);
166 setOperationAction(ISD::UREM, VT, Expand);
167 setOperationAction(ISD::SDIVREM, VT, Custom);
168 setOperationAction(ISD::UDIVREM, VT, Custom);
169
Richard Sandifordbef3d7a2013-12-10 10:49:34 +0000170 // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and
171 // stores, putting a serialization instruction after the stores.
172 setOperationAction(ISD::ATOMIC_LOAD, VT, Custom);
173 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000174
Richard Sandiford41350a52013-12-24 15:18:04 +0000175 // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are
176 // available, or if the operand is constant.
177 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
178
Ulrich Weigandb4012182015-03-31 12:56:33 +0000179 // Use POPCNT on z196 and above.
180 if (Subtarget.hasPopulationCount())
181 setOperationAction(ISD::CTPOP, VT, Custom);
182 else
183 setOperationAction(ISD::CTPOP, VT, Expand);
184
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000185 // No special instructions for these.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000186 setOperationAction(ISD::CTTZ, VT, Expand);
187 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
188 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
189 setOperationAction(ISD::ROTR, VT, Expand);
190
Richard Sandiford7d86e472013-08-21 09:34:56 +0000191 // Use *MUL_LOHI where possible instead of MULH*.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000192 setOperationAction(ISD::MULHS, VT, Expand);
193 setOperationAction(ISD::MULHU, VT, Expand);
Richard Sandiford7d86e472013-08-21 09:34:56 +0000194 setOperationAction(ISD::SMUL_LOHI, VT, Custom);
195 setOperationAction(ISD::UMUL_LOHI, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000196
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000197 // Only z196 and above have native support for conversions to unsigned.
198 if (!Subtarget.hasFPExtension())
199 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000200 }
201 }
202
203 // Type legalization will convert 8- and 16-bit atomic operations into
204 // forms that operate on i32s (but still keeping the original memory VT).
205 // Lower them into full i32 operations.
206 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom);
207 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom);
208 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
209 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom);
210 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom);
211 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom);
212 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
213 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom);
214 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom);
215 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
216 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
217 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
218
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000219 // z10 has instructions for signed but not unsigned FP conversion.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000220 // Handle unsigned 32-bit types as signed 64-bit types.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000221 if (!Subtarget.hasFPExtension()) {
222 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
223 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
224 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000225
226 // We have native support for a 64-bit CTLZ, via FLOGR.
227 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
228 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
229
230 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
231 setOperationAction(ISD::OR, MVT::i64, Custom);
232
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000233 // FIXME: Can we support these natively?
234 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
235 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
236 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
237
238 // We have native instructions for i8, i16 and i32 extensions, but not i1.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000239 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000240 for (MVT VT : MVT::integer_valuetypes()) {
241 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
242 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
243 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
244 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000245
246 // Handle the various types of symbolic address.
247 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
248 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
249 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
250 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
251 setOperationAction(ISD::JumpTable, PtrVT, Custom);
252
253 // We need to handle dynamic allocations specially because of the
254 // 160-byte area at the bottom of the stack.
255 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
256
257 // Use custom expanders so that we can force the function to use
258 // a frame pointer.
259 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
260 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
261
Richard Sandiford03481332013-08-23 11:36:42 +0000262 // Handle prefetches with PFD or PFDRL.
263 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
264
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000265 for (MVT VT : MVT::vector_valuetypes()) {
266 // Assume by default that all vector operations need to be expanded.
267 for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode)
268 if (getOperationAction(Opcode, VT) == Legal)
269 setOperationAction(Opcode, VT, Expand);
270
271 // Likewise all truncating stores and extending loads.
272 for (MVT InnerVT : MVT::vector_valuetypes()) {
273 setTruncStoreAction(VT, InnerVT, Expand);
274 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
275 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
276 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
277 }
278
279 if (isTypeLegal(VT)) {
280 // These operations are legal for anything that can be stored in a
281 // vector register, even if there is no native support for the format
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000282 // as such. In particular, we can do these for v4f32 even though there
283 // are no specific instructions for that format.
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000284 setOperationAction(ISD::LOAD, VT, Legal);
285 setOperationAction(ISD::STORE, VT, Legal);
286 setOperationAction(ISD::VSELECT, VT, Legal);
287 setOperationAction(ISD::BITCAST, VT, Legal);
288 setOperationAction(ISD::UNDEF, VT, Legal);
289
290 // Likewise, except that we need to replace the nodes with something
291 // more specific.
292 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
293 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
294 }
295 }
296
297 // Handle integer vector types.
298 for (MVT VT : MVT::integer_vector_valuetypes()) {
299 if (isTypeLegal(VT)) {
300 // These operations have direct equivalents.
301 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal);
302 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal);
303 setOperationAction(ISD::ADD, VT, Legal);
304 setOperationAction(ISD::SUB, VT, Legal);
305 if (VT != MVT::v2i64)
306 setOperationAction(ISD::MUL, VT, Legal);
307 setOperationAction(ISD::AND, VT, Legal);
308 setOperationAction(ISD::OR, VT, Legal);
309 setOperationAction(ISD::XOR, VT, Legal);
310 setOperationAction(ISD::CTPOP, VT, Custom);
311 setOperationAction(ISD::CTTZ, VT, Legal);
312 setOperationAction(ISD::CTLZ, VT, Legal);
313 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Custom);
314 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Custom);
315
316 // Convert a GPR scalar to a vector by inserting it into element 0.
317 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
318
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000319 // Use a series of unpacks for extensions.
320 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Custom);
321 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Custom);
322
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000323 // Detect shifts by a scalar amount and convert them into
324 // V*_BY_SCALAR.
325 setOperationAction(ISD::SHL, VT, Custom);
326 setOperationAction(ISD::SRA, VT, Custom);
327 setOperationAction(ISD::SRL, VT, Custom);
328
329 // At present ROTL isn't matched by DAGCombiner. ROTR should be
330 // converted into ROTL.
331 setOperationAction(ISD::ROTL, VT, Expand);
332 setOperationAction(ISD::ROTR, VT, Expand);
333
334 // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands
335 // and inverting the result as necessary.
336 setOperationAction(ISD::SETCC, VT, Custom);
337 }
338 }
339
Ulrich Weigandcd808232015-05-05 19:26:48 +0000340 if (Subtarget.hasVector()) {
341 // There should be no need to check for float types other than v2f64
342 // since <2 x f32> isn't a legal type.
343 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
344 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
345 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
346 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
347 }
348
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000349 // Handle floating-point types.
350 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
351 I <= MVT::LAST_FP_VALUETYPE;
352 ++I) {
353 MVT VT = MVT::SimpleValueType(I);
354 if (isTypeLegal(VT)) {
355 // We can use FI for FRINT.
356 setOperationAction(ISD::FRINT, VT, Legal);
357
Richard Sandifordaf5f66a2013-08-21 09:04:20 +0000358 // We can use the extended form of FI for other rounding operations.
359 if (Subtarget.hasFPExtension()) {
360 setOperationAction(ISD::FNEARBYINT, VT, Legal);
361 setOperationAction(ISD::FFLOOR, VT, Legal);
362 setOperationAction(ISD::FCEIL, VT, Legal);
363 setOperationAction(ISD::FTRUNC, VT, Legal);
364 setOperationAction(ISD::FROUND, VT, Legal);
365 }
366
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000367 // No special instructions for these.
368 setOperationAction(ISD::FSIN, VT, Expand);
369 setOperationAction(ISD::FCOS, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000370 setOperationAction(ISD::FSINCOS, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000371 setOperationAction(ISD::FREM, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000372 setOperationAction(ISD::FPOW, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000373 }
374 }
375
Ulrich Weigandcd808232015-05-05 19:26:48 +0000376 // Handle floating-point vector types.
377 if (Subtarget.hasVector()) {
378 // Scalar-to-vector conversion is just a subreg.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000379 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000380 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
381
382 // Some insertions and extractions can be done directly but others
383 // need to go via integers.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000384 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000385 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000386 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000387 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
388
389 // These operations have direct equivalents.
390 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
391 setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
392 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
393 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
394 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
395 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
396 setOperationAction(ISD::FABS, MVT::v2f64, Legal);
397 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
398 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
399 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
400 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
401 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
402 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
403 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
404 }
405
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000406 // We have fused multiply-addition for f32 and f64 but not f128.
407 setOperationAction(ISD::FMA, MVT::f32, Legal);
408 setOperationAction(ISD::FMA, MVT::f64, Legal);
409 setOperationAction(ISD::FMA, MVT::f128, Expand);
410
411 // Needed so that we don't try to implement f128 constant loads using
412 // a load-and-extend of a f80 constant (in cases where the constant
413 // would fit in an f80).
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000414 for (MVT VT : MVT::fp_valuetypes())
415 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000416
417 // Floating-point truncation and stores need to be done separately.
418 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
419 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
420 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
421
422 // We have 64-bit FPR<->GPR moves, but need special handling for
423 // 32-bit forms.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000424 if (!Subtarget.hasVector()) {
425 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
426 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
427 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000428
429 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
430 // structure, but VAEND is a no-op.
431 setOperationAction(ISD::VASTART, MVT::Other, Custom);
432 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
433 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Richard Sandifordd131ff82013-07-08 09:35:23 +0000434
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000435 // Codes for which we want to perform some z-specific combinations.
436 setTargetDAGCombine(ISD::SIGN_EXTEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000437 setTargetDAGCombine(ISD::STORE);
438 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000439 setTargetDAGCombine(ISD::FP_ROUND);
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000440
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000441 // Handle intrinsics.
442 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Ulrich Weigandc1708b22015-05-05 19:31:09 +0000443 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000444
Richard Sandifordd131ff82013-07-08 09:35:23 +0000445 // We want to use MVC in preference to even a single load/store pair.
446 MaxStoresPerMemcpy = 0;
447 MaxStoresPerMemcpyOptSize = 0;
Richard Sandiford47660c12013-07-09 09:32:42 +0000448
449 // The main memset sequence is a byte store followed by an MVC.
450 // Two STC or MV..I stores win over that, but the kind of fused stores
451 // generated by target-independent code don't when the byte value is
452 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
453 // than "STC;MVC". Handle the choice in target-specific code instead.
454 MaxStoresPerMemset = 0;
455 MaxStoresPerMemsetOptSize = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000456}
457
Mehdi Amini44ede332015-07-09 02:09:04 +0000458EVT SystemZTargetLowering::getSetCCResultType(const DataLayout &DL,
459 LLVMContext &, EVT VT) const {
Richard Sandifordabc010b2013-11-06 12:16:02 +0000460 if (!VT.isVector())
461 return MVT::i32;
462 return VT.changeVectorElementTypeToInteger();
463}
464
465bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
Stephen Lin73de7bf2013-07-09 18:16:56 +0000466 VT = VT.getScalarType();
467
468 if (!VT.isSimple())
469 return false;
470
471 switch (VT.getSimpleVT().SimpleTy) {
472 case MVT::f32:
473 case MVT::f64:
474 return true;
475 case MVT::f128:
476 return false;
477 default:
478 break;
479 }
480
481 return false;
482}
483
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000484bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
485 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
486 return Imm.isZero() || Imm.isNegZero();
487}
488
Ulrich Weigand1f6666a2015-03-31 12:52:27 +0000489bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
490 // We can use CGFI or CLGFI.
491 return isInt<32>(Imm) || isUInt<32>(Imm);
492}
493
494bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const {
495 // We can use ALGFI or SLGFI.
496 return isUInt<32>(Imm) || isUInt<32>(-Imm);
497}
498
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000499bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
500 unsigned,
501 unsigned,
502 bool *Fast) const {
Richard Sandiford46af5a22013-05-30 09:45:42 +0000503 // Unaligned accesses should never be slower than the expanded version.
504 // We check specifically for aligned accesses in the few cases where
505 // they are required.
506 if (Fast)
507 *Fast = true;
508 return true;
509}
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +0000510
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000511bool SystemZTargetLowering::isLegalAddressingMode(const DataLayout &DL,
512 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +0000513 unsigned AS) const {
Richard Sandiford791bea42013-07-31 12:58:26 +0000514 // Punt on globals for now, although they can be used in limited
515 // RELATIVE LONG cases.
516 if (AM.BaseGV)
517 return false;
518
519 // Require a 20-bit signed offset.
520 if (!isInt<20>(AM.BaseOffs))
521 return false;
522
523 // Indexing is OK but no scale factor can be applied.
524 return AM.Scale == 0 || AM.Scale == 1;
525}
526
Richard Sandiford709bda62013-08-19 12:42:31 +0000527bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
528 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
529 return false;
530 unsigned FromBits = FromType->getPrimitiveSizeInBits();
531 unsigned ToBits = ToType->getPrimitiveSizeInBits();
532 return FromBits > ToBits;
533}
534
535bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
536 if (!FromVT.isInteger() || !ToVT.isInteger())
537 return false;
538 unsigned FromBits = FromVT.getSizeInBits();
539 unsigned ToBits = ToVT.getSizeInBits();
540 return FromBits > ToBits;
541}
542
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000543//===----------------------------------------------------------------------===//
544// Inline asm support
545//===----------------------------------------------------------------------===//
546
547TargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000548SystemZTargetLowering::getConstraintType(StringRef Constraint) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000549 if (Constraint.size() == 1) {
550 switch (Constraint[0]) {
551 case 'a': // Address register
552 case 'd': // Data register (equivalent to 'r')
553 case 'f': // Floating-point register
Richard Sandiford0755c932013-10-01 11:26:28 +0000554 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000555 case 'r': // General-purpose register
556 return C_RegisterClass;
557
558 case 'Q': // Memory with base and unsigned 12-bit displacement
559 case 'R': // Likewise, plus an index
560 case 'S': // Memory with base and signed 20-bit displacement
561 case 'T': // Likewise, plus an index
562 case 'm': // Equivalent to 'T'.
563 return C_Memory;
564
565 case 'I': // Unsigned 8-bit constant
566 case 'J': // Unsigned 12-bit constant
567 case 'K': // Signed 16-bit constant
568 case 'L': // Signed 20-bit displacement (on all targets we support)
569 case 'M': // 0x7fffffff
570 return C_Other;
571
572 default:
573 break;
574 }
575 }
576 return TargetLowering::getConstraintType(Constraint);
577}
578
579TargetLowering::ConstraintWeight SystemZTargetLowering::
580getSingleConstraintMatchWeight(AsmOperandInfo &info,
581 const char *constraint) const {
582 ConstraintWeight weight = CW_Invalid;
583 Value *CallOperandVal = info.CallOperandVal;
584 // If we don't have a value, we can't do a match,
585 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +0000586 if (!CallOperandVal)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000587 return CW_Default;
588 Type *type = CallOperandVal->getType();
589 // Look at the constraint type.
590 switch (*constraint) {
591 default:
592 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
593 break;
594
595 case 'a': // Address register
596 case 'd': // Data register (equivalent to 'r')
Richard Sandiford0755c932013-10-01 11:26:28 +0000597 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000598 case 'r': // General-purpose register
599 if (CallOperandVal->getType()->isIntegerTy())
600 weight = CW_Register;
601 break;
602
603 case 'f': // Floating-point register
604 if (type->isFloatingPointTy())
605 weight = CW_Register;
606 break;
607
608 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000609 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000610 if (isUInt<8>(C->getZExtValue()))
611 weight = CW_Constant;
612 break;
613
614 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000615 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000616 if (isUInt<12>(C->getZExtValue()))
617 weight = CW_Constant;
618 break;
619
620 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000621 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000622 if (isInt<16>(C->getSExtValue()))
623 weight = CW_Constant;
624 break;
625
626 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000627 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000628 if (isInt<20>(C->getSExtValue()))
629 weight = CW_Constant;
630 break;
631
632 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000633 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000634 if (C->getZExtValue() == 0x7fffffff)
635 weight = CW_Constant;
636 break;
637 }
638 return weight;
639}
640
Richard Sandifordb8204052013-07-12 09:08:12 +0000641// Parse a "{tNNN}" register constraint for which the register type "t"
642// has already been verified. MC is the class associated with "t" and
643// Map maps 0-based register numbers to LLVM register numbers.
644static std::pair<unsigned, const TargetRegisterClass *>
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000645parseRegisterNumber(StringRef Constraint, const TargetRegisterClass *RC,
646 const unsigned *Map) {
Richard Sandifordb8204052013-07-12 09:08:12 +0000647 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
648 if (isdigit(Constraint[2])) {
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000649 unsigned Index;
650 bool Failed =
651 Constraint.slice(2, Constraint.size() - 1).getAsInteger(10, Index);
652 if (!Failed && Index < 16 && Map[Index])
Richard Sandifordb8204052013-07-12 09:08:12 +0000653 return std::make_pair(Map[Index], RC);
654 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000655 return std::make_pair(0U, nullptr);
Richard Sandifordb8204052013-07-12 09:08:12 +0000656}
657
Eric Christopher11e4df72015-02-26 22:38:43 +0000658std::pair<unsigned, const TargetRegisterClass *>
659SystemZTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000660 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000661 if (Constraint.size() == 1) {
662 // GCC Constraint Letters
663 switch (Constraint[0]) {
664 default: break;
665 case 'd': // Data register (equivalent to 'r')
666 case 'r': // General-purpose register
667 if (VT == MVT::i64)
668 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
669 else if (VT == MVT::i128)
670 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
671 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
672
673 case 'a': // Address register
674 if (VT == MVT::i64)
675 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
676 else if (VT == MVT::i128)
677 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
678 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
679
Richard Sandiford0755c932013-10-01 11:26:28 +0000680 case 'h': // High-part register (an LLVM extension)
681 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
682
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000683 case 'f': // Floating-point register
684 if (VT == MVT::f64)
685 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
686 else if (VT == MVT::f128)
687 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
688 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
689 }
690 }
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000691 if (Constraint.size() > 0 && Constraint[0] == '{') {
Richard Sandifordb8204052013-07-12 09:08:12 +0000692 // We need to override the default register parsing for GPRs and FPRs
693 // because the interpretation depends on VT. The internal names of
694 // the registers are also different from the external names
695 // (F0D and F0S instead of F0, etc.).
696 if (Constraint[1] == 'r') {
697 if (VT == MVT::i32)
698 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
699 SystemZMC::GR32Regs);
700 if (VT == MVT::i128)
701 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
702 SystemZMC::GR128Regs);
703 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
704 SystemZMC::GR64Regs);
705 }
706 if (Constraint[1] == 'f') {
707 if (VT == MVT::f32)
708 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
709 SystemZMC::FP32Regs);
710 if (VT == MVT::f128)
711 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
712 SystemZMC::FP128Regs);
713 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
714 SystemZMC::FP64Regs);
715 }
716 }
Eric Christopher11e4df72015-02-26 22:38:43 +0000717 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000718}
719
720void SystemZTargetLowering::
721LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
722 std::vector<SDValue> &Ops,
723 SelectionDAG &DAG) const {
724 // Only support length 1 constraints for now.
725 if (Constraint.length() == 1) {
726 switch (Constraint[0]) {
727 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000728 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000729 if (isUInt<8>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000730 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000731 Op.getValueType()));
732 return;
733
734 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000735 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000736 if (isUInt<12>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000737 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000738 Op.getValueType()));
739 return;
740
741 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000742 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000743 if (isInt<16>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000744 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000745 Op.getValueType()));
746 return;
747
748 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000749 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000750 if (isInt<20>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000751 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000752 Op.getValueType()));
753 return;
754
755 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000756 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000757 if (C->getZExtValue() == 0x7fffffff)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000758 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000759 Op.getValueType()));
760 return;
761 }
762 }
763 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
764}
765
766//===----------------------------------------------------------------------===//
767// Calling conventions
768//===----------------------------------------------------------------------===//
769
770#include "SystemZGenCallingConv.inc"
771
Richard Sandiford709bda62013-08-19 12:42:31 +0000772bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
773 Type *ToType) const {
774 return isTruncateFree(FromType, ToType);
775}
776
777bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Ulrich Weigand19d24d22015-11-13 13:00:27 +0000778 return CI->isTailCall();
Richard Sandiford709bda62013-08-19 12:42:31 +0000779}
780
Ulrich Weigand5211f9f2015-05-05 19:30:05 +0000781// We do not yet support 128-bit single-element vector types. If the user
782// attempts to use such types as function argument or return type, prefer
783// to error out instead of emitting code violating the ABI.
784static void VerifyVectorType(MVT VT, EVT ArgVT) {
785 if (ArgVT.isVector() && !VT.isVector())
786 report_fatal_error("Unsupported vector argument or return type");
787}
788
789static void VerifyVectorTypes(const SmallVectorImpl<ISD::InputArg> &Ins) {
790 for (unsigned i = 0; i < Ins.size(); ++i)
791 VerifyVectorType(Ins[i].VT, Ins[i].ArgVT);
792}
793
794static void VerifyVectorTypes(const SmallVectorImpl<ISD::OutputArg> &Outs) {
795 for (unsigned i = 0; i < Outs.size(); ++i)
796 VerifyVectorType(Outs[i].VT, Outs[i].ArgVT);
797}
798
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000799// Value is a value that has been passed to us in the location described by VA
800// (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
801// any loads onto Chain.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000802static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000803 CCValAssign &VA, SDValue Chain,
804 SDValue Value) {
805 // If the argument has been promoted from a smaller type, insert an
806 // assertion to capture this.
807 if (VA.getLocInfo() == CCValAssign::SExt)
808 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
809 DAG.getValueType(VA.getValVT()));
810 else if (VA.getLocInfo() == CCValAssign::ZExt)
811 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
812 DAG.getValueType(VA.getValVT()));
813
814 if (VA.isExtInLoc())
815 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000816 else if (VA.getLocInfo() == CCValAssign::BCvt) {
817 // If this is a short vector argument loaded from the stack,
818 // extend from i64 to full vector size and then bitcast.
819 assert(VA.getLocVT() == MVT::i64);
820 assert(VA.getValVT().isVector());
821 Value = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i64,
822 Value, DAG.getUNDEF(MVT::i64));
823 Value = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Value);
824 } else
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000825 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
826 return Value;
827}
828
829// Value is a value of type VA.getValVT() that we need to copy into
830// the location described by VA. Return a copy of Value converted to
831// VA.getValVT(). The caller is responsible for handling indirect values.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000832static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000833 CCValAssign &VA, SDValue Value) {
834 switch (VA.getLocInfo()) {
835 case CCValAssign::SExt:
836 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
837 case CCValAssign::ZExt:
838 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
839 case CCValAssign::AExt:
840 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000841 case CCValAssign::BCvt:
842 // If this is a short vector argument to be stored to the stack,
843 // bitcast to v2i64 and then extract first element.
844 assert(VA.getLocVT() == MVT::i64);
845 assert(VA.getValVT().isVector());
846 Value = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Value);
847 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VA.getLocVT(), Value,
848 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000849 case CCValAssign::Full:
850 return Value;
851 default:
852 llvm_unreachable("Unhandled getLocInfo()");
853 }
854}
855
856SDValue SystemZTargetLowering::
857LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
858 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000859 SDLoc DL, SelectionDAG &DAG,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000860 SmallVectorImpl<SDValue> &InVals) const {
861 MachineFunction &MF = DAG.getMachineFunction();
862 MachineFrameInfo *MFI = MF.getFrameInfo();
863 MachineRegisterInfo &MRI = MF.getRegInfo();
864 SystemZMachineFunctionInfo *FuncInfo =
Eric Christophera6734172015-01-31 00:06:45 +0000865 MF.getInfo<SystemZMachineFunctionInfo>();
866 auto *TFL =
867 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering());
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +0000868 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000869
Ulrich Weigand5211f9f2015-05-05 19:30:05 +0000870 // Detect unsupported vector argument types.
871 if (Subtarget.hasVector())
872 VerifyVectorTypes(Ins);
873
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000874 // Assign locations to all of the incoming arguments.
875 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000876 SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000877 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
878
879 unsigned NumFixedGPRs = 0;
880 unsigned NumFixedFPRs = 0;
881 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
882 SDValue ArgValue;
883 CCValAssign &VA = ArgLocs[I];
884 EVT LocVT = VA.getLocVT();
885 if (VA.isRegLoc()) {
886 // Arguments passed in registers
887 const TargetRegisterClass *RC;
888 switch (LocVT.getSimpleVT().SimpleTy) {
889 default:
890 // Integers smaller than i64 should be promoted to i64.
891 llvm_unreachable("Unexpected argument type");
892 case MVT::i32:
893 NumFixedGPRs += 1;
894 RC = &SystemZ::GR32BitRegClass;
895 break;
896 case MVT::i64:
897 NumFixedGPRs += 1;
898 RC = &SystemZ::GR64BitRegClass;
899 break;
900 case MVT::f32:
901 NumFixedFPRs += 1;
902 RC = &SystemZ::FP32BitRegClass;
903 break;
904 case MVT::f64:
905 NumFixedFPRs += 1;
906 RC = &SystemZ::FP64BitRegClass;
907 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000908 case MVT::v16i8:
909 case MVT::v8i16:
910 case MVT::v4i32:
911 case MVT::v2i64:
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000912 case MVT::v4f32:
Ulrich Weigandcd808232015-05-05 19:26:48 +0000913 case MVT::v2f64:
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000914 RC = &SystemZ::VR128BitRegClass;
915 break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000916 }
917
918 unsigned VReg = MRI.createVirtualRegister(RC);
919 MRI.addLiveIn(VA.getLocReg(), VReg);
920 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
921 } else {
922 assert(VA.isMemLoc() && "Argument not register or memory");
923
924 // Create the frame index object for this incoming parameter.
925 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
926 VA.getLocMemOffset(), true);
927
928 // Create the SelectionDAG nodes corresponding to a load
929 // from this parameter. Unpromoted ints and floats are
930 // passed as right-justified 8-byte values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000931 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
932 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000933 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
934 DAG.getIntPtrConstant(4, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000935 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
Alex Lorenze40c8a22015-08-11 23:09:45 +0000936 MachinePointerInfo::getFixedStack(MF, FI), false,
937 false, false, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000938 }
939
940 // Convert the value of the argument register into the value that's
941 // being passed.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +0000942 if (VA.getLocInfo() == CCValAssign::Indirect) {
943 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain,
944 ArgValue, MachinePointerInfo(),
945 false, false, false, 0));
946 // If the original argument was split (e.g. i128), we need
947 // to load all parts of it here (using the same address).
948 unsigned ArgIndex = Ins[I].OrigArgIndex;
949 assert (Ins[I].PartOffset == 0);
950 while (I + 1 != E && Ins[I + 1].OrigArgIndex == ArgIndex) {
951 CCValAssign &PartVA = ArgLocs[I + 1];
952 unsigned PartOffset = Ins[I + 1].PartOffset;
953 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
954 DAG.getIntPtrConstant(PartOffset, DL));
955 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain,
956 Address, MachinePointerInfo(),
957 false, false, false, 0));
958 ++I;
959 }
960 } else
961 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000962 }
963
964 if (IsVarArg) {
965 // Save the number of non-varargs registers for later use by va_start, etc.
966 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
967 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
968
969 // Likewise the address (in the form of a frame index) of where the
970 // first stack vararg would be. The 1-byte size here is arbitrary.
971 int64_t StackSize = CCInfo.getNextStackOffset();
972 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true));
973
974 // ...and a similar frame index for the caller-allocated save area
975 // that will be used to store the incoming registers.
976 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
977 unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true);
978 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
979
980 // Store the FPR varargs in the reserved frame slots. (We store the
981 // GPRs as part of the prologue.)
982 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
983 SDValue MemOps[SystemZ::NumArgFPRs];
984 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
985 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
986 int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000987 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000988 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
989 &SystemZ::FP64BitRegClass);
990 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
991 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
Alex Lorenze40c8a22015-08-11 23:09:45 +0000992 MachinePointerInfo::getFixedStack(MF, FI),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000993 false, false, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000994 }
995 // Join the stores, which are independent of one another.
996 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Craig Topper2d2aa0c2014-04-30 07:17:30 +0000997 makeArrayRef(&MemOps[NumFixedFPRs],
998 SystemZ::NumArgFPRs-NumFixedFPRs));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000999 }
1000 }
1001
1002 return Chain;
1003}
1004
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +00001005static bool canUseSiblingCall(const CCState &ArgCCInfo,
Richard Sandiford709bda62013-08-19 12:42:31 +00001006 SmallVectorImpl<CCValAssign> &ArgLocs) {
1007 // Punt if there are any indirect or stack arguments, or if the call
1008 // needs the call-saved argument register R6.
1009 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1010 CCValAssign &VA = ArgLocs[I];
1011 if (VA.getLocInfo() == CCValAssign::Indirect)
1012 return false;
1013 if (!VA.isRegLoc())
1014 return false;
1015 unsigned Reg = VA.getLocReg();
Richard Sandiford0755c932013-10-01 11:26:28 +00001016 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
Richard Sandiford709bda62013-08-19 12:42:31 +00001017 return false;
1018 }
1019 return true;
1020}
1021
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001022SDValue
1023SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
1024 SmallVectorImpl<SDValue> &InVals) const {
1025 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001026 SDLoc &DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001027 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1028 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1029 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001030 SDValue Chain = CLI.Chain;
1031 SDValue Callee = CLI.Callee;
Richard Sandiford709bda62013-08-19 12:42:31 +00001032 bool &IsTailCall = CLI.IsTailCall;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001033 CallingConv::ID CallConv = CLI.CallConv;
1034 bool IsVarArg = CLI.IsVarArg;
1035 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +00001036 EVT PtrVT = getPointerTy(MF.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001037
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001038 // Detect unsupported vector argument and return types.
1039 if (Subtarget.hasVector()) {
1040 VerifyVectorTypes(Outs);
1041 VerifyVectorTypes(Ins);
1042 }
1043
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001044 // Analyze the operands of the call, assigning locations to each operand.
1045 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001046 SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001047 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
1048
Richard Sandiford709bda62013-08-19 12:42:31 +00001049 // We don't support GuaranteedTailCallOpt, only automatically-detected
1050 // sibling calls.
1051 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs))
1052 IsTailCall = false;
1053
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001054 // Get a count of how many bytes are to be pushed on the stack.
1055 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1056
1057 // Mark the start of the call.
Richard Sandiford709bda62013-08-19 12:42:31 +00001058 if (!IsTailCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001059 Chain = DAG.getCALLSEQ_START(Chain,
1060 DAG.getConstant(NumBytes, DL, PtrVT, true),
Richard Sandiford709bda62013-08-19 12:42:31 +00001061 DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001062
1063 // Copy argument values to their designated locations.
1064 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
1065 SmallVector<SDValue, 8> MemOpChains;
1066 SDValue StackPtr;
1067 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1068 CCValAssign &VA = ArgLocs[I];
1069 SDValue ArgValue = OutVals[I];
1070
1071 if (VA.getLocInfo() == CCValAssign::Indirect) {
1072 // Store the argument in a stack slot and pass its address.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001073 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[I].ArgVT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001074 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001075 MemOpChains.push_back(DAG.getStore(
1076 Chain, DL, ArgValue, SpillSlot,
1077 MachinePointerInfo::getFixedStack(MF, FI), false, false, 0));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001078 // If the original argument was split (e.g. i128), we need
1079 // to store all parts of it here (and pass just one address).
1080 unsigned ArgIndex = Outs[I].OrigArgIndex;
1081 assert (Outs[I].PartOffset == 0);
1082 while (I + 1 != E && Outs[I + 1].OrigArgIndex == ArgIndex) {
1083 SDValue PartValue = OutVals[I + 1];
1084 unsigned PartOffset = Outs[I + 1].PartOffset;
1085 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
1086 DAG.getIntPtrConstant(PartOffset, DL));
1087 MemOpChains.push_back(DAG.getStore(
1088 Chain, DL, PartValue, Address,
1089 MachinePointerInfo::getFixedStack(MF, FI), false, false, 0));
1090 ++I;
1091 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001092 ArgValue = SpillSlot;
1093 } else
1094 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
1095
1096 if (VA.isRegLoc())
1097 // Queue up the argument copies and emit them at the end.
1098 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1099 else {
1100 assert(VA.isMemLoc() && "Argument not register or memory");
1101
1102 // Work out the address of the stack slot. Unpromoted ints and
1103 // floats are passed as right-justified 8-byte values.
1104 if (!StackPtr.getNode())
1105 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
1106 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
1107 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
1108 Offset += 4;
1109 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001110 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001111
1112 // Emit the store.
1113 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address,
1114 MachinePointerInfo(),
1115 false, false, 0));
1116 }
1117 }
1118
1119 // Join the stores, which are independent of one another.
1120 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001121 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001122
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001123 // Accept direct calls by converting symbolic call addresses to the
Richard Sandiford709bda62013-08-19 12:42:31 +00001124 // associated Target* opcodes. Force %r1 to be used for indirect
1125 // tail calls.
1126 SDValue Glue;
Richard Sandiford21f5d682014-03-06 11:22:58 +00001127 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001128 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
1129 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford21f5d682014-03-06 11:22:58 +00001130 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001131 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
1132 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford709bda62013-08-19 12:42:31 +00001133 } else if (IsTailCall) {
1134 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
1135 Glue = Chain.getValue(1);
1136 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
1137 }
1138
1139 // Build a sequence of copy-to-reg nodes, chained and glued together.
1140 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
1141 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
1142 RegsToPass[I].second, Glue);
1143 Glue = Chain.getValue(1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001144 }
1145
1146 // The first call operand is the chain and the second is the target address.
1147 SmallVector<SDValue, 8> Ops;
1148 Ops.push_back(Chain);
1149 Ops.push_back(Callee);
1150
1151 // Add argument registers to the end of the list so that they are
1152 // known live into the call.
1153 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
1154 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
1155 RegsToPass[I].second.getValueType()));
1156
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001157 // Add a register mask operand representing the call-preserved registers.
Eric Christophera6734172015-01-31 00:06:45 +00001158 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00001159 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001160 assert(Mask && "Missing call preserved mask for calling convention");
1161 Ops.push_back(DAG.getRegisterMask(Mask));
1162
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001163 // Glue the call to the argument copies, if any.
1164 if (Glue.getNode())
1165 Ops.push_back(Glue);
1166
1167 // Emit the call.
1168 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Richard Sandiford709bda62013-08-19 12:42:31 +00001169 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001170 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
1171 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001172 Glue = Chain.getValue(1);
1173
1174 // Mark the end of the call, which is glued to the call itself.
1175 Chain = DAG.getCALLSEQ_END(Chain,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001176 DAG.getConstant(NumBytes, DL, PtrVT, true),
1177 DAG.getConstant(0, DL, PtrVT, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001178 Glue, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001179 Glue = Chain.getValue(1);
1180
1181 // Assign locations to each value returned by this call.
1182 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001183 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001184 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
1185
1186 // Copy all of the result registers out of their specified physreg.
1187 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1188 CCValAssign &VA = RetLocs[I];
1189
1190 // Copy the value out, gluing the copy to the end of the call sequence.
1191 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
1192 VA.getLocVT(), Glue);
1193 Chain = RetValue.getValue(1);
1194 Glue = RetValue.getValue(2);
1195
1196 // Convert the value of the return register into the value that's
1197 // being returned.
1198 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
1199 }
1200
1201 return Chain;
1202}
1203
Ulrich Weiganda887f062015-08-13 13:37:06 +00001204bool SystemZTargetLowering::
1205CanLowerReturn(CallingConv::ID CallConv,
1206 MachineFunction &MF, bool isVarArg,
1207 const SmallVectorImpl<ISD::OutputArg> &Outs,
1208 LLVMContext &Context) const {
1209 // Detect unsupported vector return types.
1210 if (Subtarget.hasVector())
1211 VerifyVectorTypes(Outs);
1212
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001213 // Special case that we cannot easily detect in RetCC_SystemZ since
1214 // i128 is not a legal type.
1215 for (auto &Out : Outs)
1216 if (Out.ArgVT == MVT::i128)
1217 return false;
1218
Ulrich Weiganda887f062015-08-13 13:37:06 +00001219 SmallVector<CCValAssign, 16> RetLocs;
1220 CCState RetCCInfo(CallConv, isVarArg, MF, RetLocs, Context);
1221 return RetCCInfo.CheckReturn(Outs, RetCC_SystemZ);
1222}
1223
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001224SDValue
1225SystemZTargetLowering::LowerReturn(SDValue Chain,
1226 CallingConv::ID CallConv, bool IsVarArg,
1227 const SmallVectorImpl<ISD::OutputArg> &Outs,
1228 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001229 SDLoc DL, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001230 MachineFunction &MF = DAG.getMachineFunction();
1231
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001232 // Detect unsupported vector return types.
1233 if (Subtarget.hasVector())
1234 VerifyVectorTypes(Outs);
1235
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001236 // Assign locations to each returned value.
1237 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001238 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001239 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
1240
1241 // Quick exit for void returns
1242 if (RetLocs.empty())
1243 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
1244
1245 // Copy the result values into the output registers.
1246 SDValue Glue;
1247 SmallVector<SDValue, 4> RetOps;
1248 RetOps.push_back(Chain);
1249 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1250 CCValAssign &VA = RetLocs[I];
1251 SDValue RetValue = OutVals[I];
1252
1253 // Make the return register live on exit.
1254 assert(VA.isRegLoc() && "Can only return in registers!");
1255
1256 // Promote the value as required.
1257 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
1258
1259 // Chain and glue the copies together.
1260 unsigned Reg = VA.getLocReg();
1261 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
1262 Glue = Chain.getValue(1);
1263 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
1264 }
1265
1266 // Update chain and glue.
1267 RetOps[0] = Chain;
1268 if (Glue.getNode())
1269 RetOps.push_back(Glue);
1270
Craig Topper48d114b2014-04-26 18:35:24 +00001271 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001272}
1273
Richard Sandiford9afe6132013-12-10 10:36:34 +00001274SDValue SystemZTargetLowering::
1275prepareVolatileOrAtomicLoad(SDValue Chain, SDLoc DL, SelectionDAG &DAG) const {
1276 return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain);
1277}
1278
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001279// Return true if Op is an intrinsic node with chain that returns the CC value
1280// as its only (other) argument. Provide the associated SystemZISD opcode and
1281// the mask of valid CC values if so.
1282static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode,
1283 unsigned &CCValid) {
1284 unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1285 switch (Id) {
1286 case Intrinsic::s390_tbegin:
1287 Opcode = SystemZISD::TBEGIN;
1288 CCValid = SystemZ::CCMASK_TBEGIN;
1289 return true;
1290
1291 case Intrinsic::s390_tbegin_nofloat:
1292 Opcode = SystemZISD::TBEGIN_NOFLOAT;
1293 CCValid = SystemZ::CCMASK_TBEGIN;
1294 return true;
1295
1296 case Intrinsic::s390_tend:
1297 Opcode = SystemZISD::TEND;
1298 CCValid = SystemZ::CCMASK_TEND;
1299 return true;
1300
1301 default:
1302 return false;
1303 }
1304}
1305
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001306// Return true if Op is an intrinsic node without chain that returns the
1307// CC value as its final argument. Provide the associated SystemZISD
1308// opcode and the mask of valid CC values if so.
1309static bool isIntrinsicWithCC(SDValue Op, unsigned &Opcode, unsigned &CCValid) {
1310 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1311 switch (Id) {
1312 case Intrinsic::s390_vpkshs:
1313 case Intrinsic::s390_vpksfs:
1314 case Intrinsic::s390_vpksgs:
1315 Opcode = SystemZISD::PACKS_CC;
1316 CCValid = SystemZ::CCMASK_VCMP;
1317 return true;
1318
1319 case Intrinsic::s390_vpklshs:
1320 case Intrinsic::s390_vpklsfs:
1321 case Intrinsic::s390_vpklsgs:
1322 Opcode = SystemZISD::PACKLS_CC;
1323 CCValid = SystemZ::CCMASK_VCMP;
1324 return true;
1325
1326 case Intrinsic::s390_vceqbs:
1327 case Intrinsic::s390_vceqhs:
1328 case Intrinsic::s390_vceqfs:
1329 case Intrinsic::s390_vceqgs:
1330 Opcode = SystemZISD::VICMPES;
1331 CCValid = SystemZ::CCMASK_VCMP;
1332 return true;
1333
1334 case Intrinsic::s390_vchbs:
1335 case Intrinsic::s390_vchhs:
1336 case Intrinsic::s390_vchfs:
1337 case Intrinsic::s390_vchgs:
1338 Opcode = SystemZISD::VICMPHS;
1339 CCValid = SystemZ::CCMASK_VCMP;
1340 return true;
1341
1342 case Intrinsic::s390_vchlbs:
1343 case Intrinsic::s390_vchlhs:
1344 case Intrinsic::s390_vchlfs:
1345 case Intrinsic::s390_vchlgs:
1346 Opcode = SystemZISD::VICMPHLS;
1347 CCValid = SystemZ::CCMASK_VCMP;
1348 return true;
1349
1350 case Intrinsic::s390_vtm:
1351 Opcode = SystemZISD::VTM;
1352 CCValid = SystemZ::CCMASK_VCMP;
1353 return true;
1354
1355 case Intrinsic::s390_vfaebs:
1356 case Intrinsic::s390_vfaehs:
1357 case Intrinsic::s390_vfaefs:
1358 Opcode = SystemZISD::VFAE_CC;
1359 CCValid = SystemZ::CCMASK_ANY;
1360 return true;
1361
1362 case Intrinsic::s390_vfaezbs:
1363 case Intrinsic::s390_vfaezhs:
1364 case Intrinsic::s390_vfaezfs:
1365 Opcode = SystemZISD::VFAEZ_CC;
1366 CCValid = SystemZ::CCMASK_ANY;
1367 return true;
1368
1369 case Intrinsic::s390_vfeebs:
1370 case Intrinsic::s390_vfeehs:
1371 case Intrinsic::s390_vfeefs:
1372 Opcode = SystemZISD::VFEE_CC;
1373 CCValid = SystemZ::CCMASK_ANY;
1374 return true;
1375
1376 case Intrinsic::s390_vfeezbs:
1377 case Intrinsic::s390_vfeezhs:
1378 case Intrinsic::s390_vfeezfs:
1379 Opcode = SystemZISD::VFEEZ_CC;
1380 CCValid = SystemZ::CCMASK_ANY;
1381 return true;
1382
1383 case Intrinsic::s390_vfenebs:
1384 case Intrinsic::s390_vfenehs:
1385 case Intrinsic::s390_vfenefs:
1386 Opcode = SystemZISD::VFENE_CC;
1387 CCValid = SystemZ::CCMASK_ANY;
1388 return true;
1389
1390 case Intrinsic::s390_vfenezbs:
1391 case Intrinsic::s390_vfenezhs:
1392 case Intrinsic::s390_vfenezfs:
1393 Opcode = SystemZISD::VFENEZ_CC;
1394 CCValid = SystemZ::CCMASK_ANY;
1395 return true;
1396
1397 case Intrinsic::s390_vistrbs:
1398 case Intrinsic::s390_vistrhs:
1399 case Intrinsic::s390_vistrfs:
1400 Opcode = SystemZISD::VISTR_CC;
1401 CCValid = SystemZ::CCMASK_0 | SystemZ::CCMASK_3;
1402 return true;
1403
1404 case Intrinsic::s390_vstrcbs:
1405 case Intrinsic::s390_vstrchs:
1406 case Intrinsic::s390_vstrcfs:
1407 Opcode = SystemZISD::VSTRC_CC;
1408 CCValid = SystemZ::CCMASK_ANY;
1409 return true;
1410
1411 case Intrinsic::s390_vstrczbs:
1412 case Intrinsic::s390_vstrczhs:
1413 case Intrinsic::s390_vstrczfs:
1414 Opcode = SystemZISD::VSTRCZ_CC;
1415 CCValid = SystemZ::CCMASK_ANY;
1416 return true;
1417
1418 case Intrinsic::s390_vfcedbs:
1419 Opcode = SystemZISD::VFCMPES;
1420 CCValid = SystemZ::CCMASK_VCMP;
1421 return true;
1422
1423 case Intrinsic::s390_vfchdbs:
1424 Opcode = SystemZISD::VFCMPHS;
1425 CCValid = SystemZ::CCMASK_VCMP;
1426 return true;
1427
1428 case Intrinsic::s390_vfchedbs:
1429 Opcode = SystemZISD::VFCMPHES;
1430 CCValid = SystemZ::CCMASK_VCMP;
1431 return true;
1432
1433 case Intrinsic::s390_vftcidb:
1434 Opcode = SystemZISD::VFTCI;
1435 CCValid = SystemZ::CCMASK_VCMP;
1436 return true;
1437
1438 default:
1439 return false;
1440 }
1441}
1442
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001443// Emit an intrinsic with chain with a glued value instead of its CC result.
1444static SDValue emitIntrinsicWithChainAndGlue(SelectionDAG &DAG, SDValue Op,
1445 unsigned Opcode) {
1446 // Copy all operands except the intrinsic ID.
1447 unsigned NumOps = Op.getNumOperands();
1448 SmallVector<SDValue, 6> Ops;
1449 Ops.reserve(NumOps - 1);
1450 Ops.push_back(Op.getOperand(0));
1451 for (unsigned I = 2; I < NumOps; ++I)
1452 Ops.push_back(Op.getOperand(I));
1453
1454 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
1455 SDVTList RawVTs = DAG.getVTList(MVT::Other, MVT::Glue);
1456 SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1457 SDValue OldChain = SDValue(Op.getNode(), 1);
1458 SDValue NewChain = SDValue(Intr.getNode(), 0);
1459 DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain);
1460 return Intr;
1461}
1462
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001463// Emit an intrinsic with a glued value instead of its CC result.
1464static SDValue emitIntrinsicWithGlue(SelectionDAG &DAG, SDValue Op,
1465 unsigned Opcode) {
1466 // Copy all operands except the intrinsic ID.
1467 unsigned NumOps = Op.getNumOperands();
1468 SmallVector<SDValue, 6> Ops;
1469 Ops.reserve(NumOps - 1);
1470 for (unsigned I = 1; I < NumOps; ++I)
1471 Ops.push_back(Op.getOperand(I));
1472
1473 if (Op->getNumValues() == 1)
1474 return DAG.getNode(Opcode, SDLoc(Op), MVT::Glue, Ops);
1475 assert(Op->getNumValues() == 2 && "Expected exactly one non-CC result");
1476 SDVTList RawVTs = DAG.getVTList(Op->getValueType(0), MVT::Glue);
1477 return DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1478}
1479
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001480// CC is a comparison that will be implemented using an integer or
1481// floating-point comparison. Return the condition code mask for
1482// a branch on true. In the integer case, CCMASK_CMP_UO is set for
1483// unsigned comparisons and clear for signed ones. In the floating-point
1484// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1485static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1486#define CONV(X) \
1487 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1488 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1489 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1490
1491 switch (CC) {
1492 default:
1493 llvm_unreachable("Invalid integer condition!");
1494
1495 CONV(EQ);
1496 CONV(NE);
1497 CONV(GT);
1498 CONV(GE);
1499 CONV(LT);
1500 CONV(LE);
1501
1502 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1503 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1504 }
1505#undef CONV
1506}
1507
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001508// Return a sequence for getting a 1 from an IPM result when CC has a
1509// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1510// The handling of CC values outside CCValid doesn't matter.
1511static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1512 // Deal with cases where the result can be taken directly from a bit
1513 // of the IPM result.
1514 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1515 return IPMConversion(0, 0, SystemZ::IPM_CC);
1516 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1517 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1518
1519 // Deal with cases where we can add a value to force the sign bit
1520 // to contain the right value. Putting the bit in 31 means we can
1521 // use SRL rather than RISBG(L), and also makes it easier to get a
1522 // 0/-1 value, so it has priority over the other tests below.
1523 //
1524 // These sequences rely on the fact that the upper two bits of the
1525 // IPM result are zero.
1526 uint64_t TopBit = uint64_t(1) << 31;
1527 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1528 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1529 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1530 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1531 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1532 | SystemZ::CCMASK_1
1533 | SystemZ::CCMASK_2)))
1534 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1535 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1536 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1537 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1538 | SystemZ::CCMASK_2
1539 | SystemZ::CCMASK_3)))
1540 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1541
1542 // Next try inverting the value and testing a bit. 0/1 could be
1543 // handled this way too, but we dealt with that case above.
1544 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1545 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1546
1547 // Handle cases where adding a value forces a non-sign bit to contain
1548 // the right value.
1549 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1550 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1551 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1552 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1553
Alp Tokercb402912014-01-24 17:20:08 +00001554 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001555 // can be done by inverting the low CC bit and applying one of the
1556 // sign-based extractions above.
1557 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1558 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1559 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1560 return IPMConversion(1 << SystemZ::IPM_CC,
1561 TopBit - (3 << SystemZ::IPM_CC), 31);
1562 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1563 | SystemZ::CCMASK_1
1564 | SystemZ::CCMASK_3)))
1565 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1566 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1567 | SystemZ::CCMASK_2
1568 | SystemZ::CCMASK_3)))
1569 return IPMConversion(1 << SystemZ::IPM_CC,
1570 TopBit - (1 << SystemZ::IPM_CC), 31);
1571
1572 llvm_unreachable("Unexpected CC combination");
1573}
1574
Richard Sandifordd420f732013-12-13 15:28:45 +00001575// If C can be converted to a comparison against zero, adjust the operands
Richard Sandiforda0757082013-08-01 10:29:45 +00001576// as necessary.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001577static void adjustZeroCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001578 if (C.ICmpType == SystemZICMP::UnsignedOnly)
Richard Sandiforda0757082013-08-01 10:29:45 +00001579 return;
1580
Richard Sandiford21f5d682014-03-06 11:22:58 +00001581 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
Richard Sandiforda0757082013-08-01 10:29:45 +00001582 if (!ConstOp1)
1583 return;
1584
1585 int64_t Value = ConstOp1->getSExtValue();
Richard Sandifordd420f732013-12-13 15:28:45 +00001586 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1587 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1588 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1589 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1590 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001591 C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType());
Richard Sandiforda0757082013-08-01 10:29:45 +00001592 }
1593}
1594
Richard Sandifordd420f732013-12-13 15:28:45 +00001595// If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1596// adjust the operands as necessary.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001597static void adjustSubwordCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001598 // For us to make any changes, it must a comparison between a single-use
1599 // load and a constant.
Richard Sandifordd420f732013-12-13 15:28:45 +00001600 if (!C.Op0.hasOneUse() ||
1601 C.Op0.getOpcode() != ISD::LOAD ||
1602 C.Op1.getOpcode() != ISD::Constant)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001603 return;
1604
1605 // We must have an 8- or 16-bit load.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001606 auto *Load = cast<LoadSDNode>(C.Op0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001607 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1608 if (NumBits != 8 && NumBits != 16)
1609 return;
1610
1611 // The load must be an extending one and the constant must be within the
1612 // range of the unextended value.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001613 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001614 uint64_t Value = ConstOp1->getZExtValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001615 uint64_t Mask = (1 << NumBits) - 1;
1616 if (Load->getExtensionType() == ISD::SEXTLOAD) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001617 // Make sure that ConstOp1 is in range of C.Op0.
1618 int64_t SignedValue = ConstOp1->getSExtValue();
1619 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001620 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001621 if (C.ICmpType != SystemZICMP::SignedOnly) {
1622 // Unsigned comparison between two sign-extended values is equivalent
1623 // to unsigned comparison between two zero-extended values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001624 Value &= Mask;
Richard Sandifordd420f732013-12-13 15:28:45 +00001625 } else if (NumBits == 8) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001626 // Try to treat the comparison as unsigned, so that we can use CLI.
1627 // Adjust CCMask and Value as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001628 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001629 // Test whether the high bit of the byte is set.
Richard Sandifordd420f732013-12-13 15:28:45 +00001630 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1631 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001632 // Test whether the high bit of the byte is clear.
Richard Sandifordd420f732013-12-13 15:28:45 +00001633 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001634 else
1635 // No instruction exists for this combination.
1636 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001637 C.ICmpType = SystemZICMP::UnsignedOnly;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001638 }
1639 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1640 if (Value > Mask)
1641 return;
Ulrich Weigand47f36492015-12-16 18:04:06 +00001642 // If the constant is in range, we can use any comparison.
1643 C.ICmpType = SystemZICMP::Any;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001644 } else
1645 return;
1646
1647 // Make sure that the first operand is an i32 of the right extension type.
Richard Sandifordd420f732013-12-13 15:28:45 +00001648 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1649 ISD::SEXTLOAD :
1650 ISD::ZEXTLOAD);
1651 if (C.Op0.getValueType() != MVT::i32 ||
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001652 Load->getExtensionType() != ExtType)
Richard Sandifordd420f732013-12-13 15:28:45 +00001653 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32,
1654 Load->getChain(), Load->getBasePtr(),
1655 Load->getPointerInfo(), Load->getMemoryVT(),
1656 Load->isVolatile(), Load->isNonTemporal(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00001657 Load->isInvariant(), Load->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001658
1659 // Make sure that the second operand is an i32 with the right value.
Richard Sandifordd420f732013-12-13 15:28:45 +00001660 if (C.Op1.getValueType() != MVT::i32 ||
1661 Value != ConstOp1->getZExtValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001662 C.Op1 = DAG.getConstant(Value, DL, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001663}
1664
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001665// Return true if Op is either an unextended load, or a load suitable
1666// for integer register-memory comparisons of type ICmpType.
1667static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001668 auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001669 if (Load) {
1670 // There are no instructions to compare a register with a memory byte.
1671 if (Load->getMemoryVT() == MVT::i8)
1672 return false;
1673 // Otherwise decide on extension type.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001674 switch (Load->getExtensionType()) {
1675 case ISD::NON_EXTLOAD:
Richard Sandiford24e597b2013-08-23 11:27:19 +00001676 return true;
1677 case ISD::SEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001678 return ICmpType != SystemZICMP::UnsignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001679 case ISD::ZEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001680 return ICmpType != SystemZICMP::SignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001681 default:
1682 break;
1683 }
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001684 }
Richard Sandiford24e597b2013-08-23 11:27:19 +00001685 return false;
1686}
1687
Richard Sandifordd420f732013-12-13 15:28:45 +00001688// Return true if it is better to swap the operands of C.
1689static bool shouldSwapCmpOperands(const Comparison &C) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001690 // Leave f128 comparisons alone, since they have no memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001691 if (C.Op0.getValueType() == MVT::f128)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001692 return false;
1693
1694 // Always keep a floating-point constant second, since comparisons with
1695 // zero can use LOAD TEST and comparisons with other constants make a
1696 // natural memory operand.
Richard Sandifordd420f732013-12-13 15:28:45 +00001697 if (isa<ConstantFPSDNode>(C.Op1))
Richard Sandiford24e597b2013-08-23 11:27:19 +00001698 return false;
1699
1700 // Never swap comparisons with zero since there are many ways to optimize
1701 // those later.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001702 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001703 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001704 return false;
1705
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001706 // Also keep natural memory operands second if the loaded value is
1707 // only used here. Several comparisons have memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001708 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001709 return false;
1710
Richard Sandiford24e597b2013-08-23 11:27:19 +00001711 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1712 // In that case we generally prefer the memory to be second.
Richard Sandifordd420f732013-12-13 15:28:45 +00001713 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001714 // The only exceptions are when the second operand is a constant and
1715 // we can use things like CHHSI.
Richard Sandifordd420f732013-12-13 15:28:45 +00001716 if (!ConstOp1)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001717 return true;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001718 // The unsigned memory-immediate instructions can handle 16-bit
1719 // unsigned integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001720 if (C.ICmpType != SystemZICMP::SignedOnly &&
1721 isUInt<16>(ConstOp1->getZExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001722 return false;
1723 // The signed memory-immediate instructions can handle 16-bit
1724 // signed integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001725 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1726 isInt<16>(ConstOp1->getSExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001727 return false;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001728 return true;
1729 }
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001730
1731 // Try to promote the use of CGFR and CLGFR.
Richard Sandifordd420f732013-12-13 15:28:45 +00001732 unsigned Opcode0 = C.Op0.getOpcode();
1733 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001734 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001735 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001736 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001737 if (C.ICmpType != SystemZICMP::SignedOnly &&
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001738 Opcode0 == ISD::AND &&
Richard Sandifordd420f732013-12-13 15:28:45 +00001739 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1740 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001741 return true;
1742
Richard Sandiford24e597b2013-08-23 11:27:19 +00001743 return false;
1744}
1745
Richard Sandiford73170f82013-12-11 11:45:08 +00001746// Return a version of comparison CC mask CCMask in which the LT and GT
1747// actions are swapped.
1748static unsigned reverseCCMask(unsigned CCMask) {
1749 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1750 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1751 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1752 (CCMask & SystemZ::CCMASK_CMP_UO));
1753}
1754
Richard Sandiford0847c452013-12-13 15:50:30 +00001755// Check whether C tests for equality between X and Y and whether X - Y
1756// or Y - X is also computed. In that case it's better to compare the
1757// result of the subtraction against zero.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001758static void adjustForSubtraction(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001759 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1760 C.CCMask == SystemZ::CCMASK_CMP_NE) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001761 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001762 SDNode *N = *I;
1763 if (N->getOpcode() == ISD::SUB &&
1764 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1765 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1766 C.Op0 = SDValue(N, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001767 C.Op1 = DAG.getConstant(0, DL, N->getValueType(0));
Richard Sandiford0847c452013-12-13 15:50:30 +00001768 return;
1769 }
1770 }
1771 }
1772}
1773
Richard Sandifordd420f732013-12-13 15:28:45 +00001774// Check whether C compares a floating-point value with zero and if that
1775// floating-point value is also negated. In this case we can use the
1776// negation to set CC, so avoiding separate LOAD AND TEST and
1777// LOAD (NEGATIVE/COMPLEMENT) instructions.
1778static void adjustForFNeg(Comparison &C) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001779 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
Richard Sandiford73170f82013-12-11 11:45:08 +00001780 if (C1 && C1->isZero()) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001781 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford73170f82013-12-11 11:45:08 +00001782 SDNode *N = *I;
1783 if (N->getOpcode() == ISD::FNEG) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001784 C.Op0 = SDValue(N, 0);
1785 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford73170f82013-12-11 11:45:08 +00001786 return;
1787 }
1788 }
1789 }
1790}
1791
Richard Sandifordd420f732013-12-13 15:28:45 +00001792// Check whether C compares (shl X, 32) with 0 and whether X is
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001793// also sign-extended. In that case it is better to test the result
1794// of the sign extension using LTGFR.
1795//
1796// This case is important because InstCombine transforms a comparison
1797// with (sext (trunc X)) into a comparison with (shl X, 32).
Richard Sandifordd420f732013-12-13 15:28:45 +00001798static void adjustForLTGFR(Comparison &C) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001799 // Check for a comparison between (shl X, 32) and 0.
Richard Sandifordd420f732013-12-13 15:28:45 +00001800 if (C.Op0.getOpcode() == ISD::SHL &&
1801 C.Op0.getValueType() == MVT::i64 &&
1802 C.Op1.getOpcode() == ISD::Constant &&
1803 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001804 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001805 if (C1 && C1->getZExtValue() == 32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001806 SDValue ShlOp0 = C.Op0.getOperand(0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001807 // See whether X has any SIGN_EXTEND_INREG uses.
Richard Sandiford28c111e2014-03-06 11:00:15 +00001808 for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001809 SDNode *N = *I;
1810 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
1811 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001812 C.Op0 = SDValue(N, 0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001813 return;
1814 }
1815 }
1816 }
1817 }
1818}
1819
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001820// If C compares the truncation of an extending load, try to compare
1821// the untruncated value instead. This exposes more opportunities to
1822// reuse CC.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001823static void adjustICmpTruncate(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001824 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
1825 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
1826 C.Op1.getOpcode() == ISD::Constant &&
1827 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001828 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001829 if (L->getMemoryVT().getStoreSizeInBits()
1830 <= C.Op0.getValueType().getSizeInBits()) {
1831 unsigned Type = L->getExtensionType();
1832 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
1833 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
1834 C.Op0 = C.Op0.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001835 C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType());
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001836 }
1837 }
1838 }
1839}
1840
Richard Sandiford030c1652013-09-13 09:09:50 +00001841// Return true if shift operation N has an in-range constant shift value.
1842// Store it in ShiftVal if so.
1843static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001844 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
Richard Sandiford030c1652013-09-13 09:09:50 +00001845 if (!Shift)
1846 return false;
1847
1848 uint64_t Amount = Shift->getZExtValue();
1849 if (Amount >= N.getValueType().getSizeInBits())
1850 return false;
1851
1852 ShiftVal = Amount;
1853 return true;
1854}
1855
1856// Check whether an AND with Mask is suitable for a TEST UNDER MASK
1857// instruction and whether the CC value is descriptive enough to handle
1858// a comparison of type Opcode between the AND result and CmpVal.
1859// CCMask says which comparison result is being tested and BitSize is
1860// the number of bits in the operands. If TEST UNDER MASK can be used,
1861// return the corresponding CC mask, otherwise return 0.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001862static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
1863 uint64_t Mask, uint64_t CmpVal,
1864 unsigned ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001865 assert(Mask != 0 && "ANDs with zero should have been removed by now");
1866
Richard Sandiford030c1652013-09-13 09:09:50 +00001867 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
1868 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
1869 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
1870 return 0;
1871
Richard Sandiford113c8702013-09-03 15:38:35 +00001872 // Work out the masks for the lowest and highest bits.
1873 unsigned HighShift = 63 - countLeadingZeros(Mask);
1874 uint64_t High = uint64_t(1) << HighShift;
1875 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
1876
1877 // Signed ordered comparisons are effectively unsigned if the sign
1878 // bit is dropped.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001879 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
Richard Sandiford113c8702013-09-03 15:38:35 +00001880
1881 // Check for equality comparisons with 0, or the equivalent.
1882 if (CmpVal == 0) {
1883 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1884 return SystemZ::CCMASK_TM_ALL_0;
1885 if (CCMask == SystemZ::CCMASK_CMP_NE)
1886 return SystemZ::CCMASK_TM_SOME_1;
1887 }
Ulrich Weigand4a4d4ab2016-02-01 18:31:19 +00001888 if (EffectivelyUnsigned && CmpVal > 0 && CmpVal <= Low) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001889 if (CCMask == SystemZ::CCMASK_CMP_LT)
1890 return SystemZ::CCMASK_TM_ALL_0;
1891 if (CCMask == SystemZ::CCMASK_CMP_GE)
1892 return SystemZ::CCMASK_TM_SOME_1;
1893 }
1894 if (EffectivelyUnsigned && CmpVal < Low) {
1895 if (CCMask == SystemZ::CCMASK_CMP_LE)
1896 return SystemZ::CCMASK_TM_ALL_0;
1897 if (CCMask == SystemZ::CCMASK_CMP_GT)
1898 return SystemZ::CCMASK_TM_SOME_1;
1899 }
1900
1901 // Check for equality comparisons with the mask, or the equivalent.
1902 if (CmpVal == Mask) {
1903 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1904 return SystemZ::CCMASK_TM_ALL_1;
1905 if (CCMask == SystemZ::CCMASK_CMP_NE)
1906 return SystemZ::CCMASK_TM_SOME_0;
1907 }
1908 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
1909 if (CCMask == SystemZ::CCMASK_CMP_GT)
1910 return SystemZ::CCMASK_TM_ALL_1;
1911 if (CCMask == SystemZ::CCMASK_CMP_LE)
1912 return SystemZ::CCMASK_TM_SOME_0;
1913 }
1914 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
1915 if (CCMask == SystemZ::CCMASK_CMP_GE)
1916 return SystemZ::CCMASK_TM_ALL_1;
1917 if (CCMask == SystemZ::CCMASK_CMP_LT)
1918 return SystemZ::CCMASK_TM_SOME_0;
1919 }
1920
1921 // Check for ordered comparisons with the top bit.
1922 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
1923 if (CCMask == SystemZ::CCMASK_CMP_LE)
1924 return SystemZ::CCMASK_TM_MSB_0;
1925 if (CCMask == SystemZ::CCMASK_CMP_GT)
1926 return SystemZ::CCMASK_TM_MSB_1;
1927 }
1928 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
1929 if (CCMask == SystemZ::CCMASK_CMP_LT)
1930 return SystemZ::CCMASK_TM_MSB_0;
1931 if (CCMask == SystemZ::CCMASK_CMP_GE)
1932 return SystemZ::CCMASK_TM_MSB_1;
1933 }
1934
1935 // If there are just two bits, we can do equality checks for Low and High
1936 // as well.
1937 if (Mask == Low + High) {
1938 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
1939 return SystemZ::CCMASK_TM_MIXED_MSB_0;
1940 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
1941 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
1942 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
1943 return SystemZ::CCMASK_TM_MIXED_MSB_1;
1944 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
1945 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
1946 }
1947
1948 // Looks like we've exhausted our options.
1949 return 0;
1950}
1951
Richard Sandifordd420f732013-12-13 15:28:45 +00001952// See whether C can be implemented as a TEST UNDER MASK instruction.
1953// Update the arguments with the TM version if so.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001954static void adjustForTestUnderMask(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001955 // Check that we have a comparison with a constant.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001956 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001957 if (!ConstOp1)
Richard Sandiford35b9be22013-08-28 10:31:43 +00001958 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001959 uint64_t CmpVal = ConstOp1->getZExtValue();
Richard Sandiford35b9be22013-08-28 10:31:43 +00001960
1961 // Check whether the nonconstant input is an AND with a constant mask.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001962 Comparison NewC(C);
1963 uint64_t MaskVal;
Craig Topper062a2ba2014-04-25 05:30:21 +00001964 ConstantSDNode *Mask = nullptr;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001965 if (C.Op0.getOpcode() == ISD::AND) {
1966 NewC.Op0 = C.Op0.getOperand(0);
1967 NewC.Op1 = C.Op0.getOperand(1);
1968 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
1969 if (!Mask)
1970 return;
1971 MaskVal = Mask->getZExtValue();
1972 } else {
1973 // There is no instruction to compare with a 64-bit immediate
1974 // so use TMHH instead if possible. We need an unsigned ordered
1975 // comparison with an i64 immediate.
1976 if (NewC.Op0.getValueType() != MVT::i64 ||
1977 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
1978 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
1979 NewC.ICmpType == SystemZICMP::SignedOnly)
1980 return;
1981 // Convert LE and GT comparisons into LT and GE.
1982 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
1983 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
1984 if (CmpVal == uint64_t(-1))
1985 return;
1986 CmpVal += 1;
1987 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1988 }
1989 // If the low N bits of Op1 are zero than the low N bits of Op0 can
1990 // be masked off without changing the result.
1991 MaskVal = -(CmpVal & -CmpVal);
1992 NewC.ICmpType = SystemZICMP::UnsignedOnly;
1993 }
Ulrich Weigandb8d76fb2015-03-30 13:46:59 +00001994 if (!MaskVal)
1995 return;
Richard Sandiford35b9be22013-08-28 10:31:43 +00001996
Richard Sandiford113c8702013-09-03 15:38:35 +00001997 // Check whether the combination of mask, comparison value and comparison
1998 // type are suitable.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001999 unsigned BitSize = NewC.Op0.getValueType().getSizeInBits();
Richard Sandiford030c1652013-09-13 09:09:50 +00002000 unsigned NewCCMask, ShiftVal;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002001 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2002 NewC.Op0.getOpcode() == ISD::SHL &&
2003 isSimpleShift(NewC.Op0, ShiftVal) &&
2004 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
2005 MaskVal >> ShiftVal,
Richard Sandiford030c1652013-09-13 09:09:50 +00002006 CmpVal >> ShiftVal,
2007 SystemZICMP::Any))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002008 NewC.Op0 = NewC.Op0.getOperand(0);
2009 MaskVal >>= ShiftVal;
2010 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2011 NewC.Op0.getOpcode() == ISD::SRL &&
2012 isSimpleShift(NewC.Op0, ShiftVal) &&
2013 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
Richard Sandiford030c1652013-09-13 09:09:50 +00002014 MaskVal << ShiftVal,
2015 CmpVal << ShiftVal,
2016 SystemZICMP::UnsignedOnly))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002017 NewC.Op0 = NewC.Op0.getOperand(0);
2018 MaskVal <<= ShiftVal;
Richard Sandiford030c1652013-09-13 09:09:50 +00002019 } else {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002020 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
2021 NewC.ICmpType);
Richard Sandiford030c1652013-09-13 09:09:50 +00002022 if (!NewCCMask)
2023 return;
2024 }
Richard Sandiford113c8702013-09-03 15:38:35 +00002025
Richard Sandiford35b9be22013-08-28 10:31:43 +00002026 // Go ahead and make the change.
Richard Sandifordd420f732013-12-13 15:28:45 +00002027 C.Opcode = SystemZISD::TM;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002028 C.Op0 = NewC.Op0;
2029 if (Mask && Mask->getZExtValue() == MaskVal)
2030 C.Op1 = SDValue(Mask, 0);
2031 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002032 C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType());
Richard Sandifordd420f732013-12-13 15:28:45 +00002033 C.CCValid = SystemZ::CCMASK_TM;
2034 C.CCMask = NewCCMask;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002035}
2036
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002037// Return a Comparison that tests the condition-code result of intrinsic
2038// node Call against constant integer CC using comparison code Cond.
2039// Opcode is the opcode of the SystemZISD operation for the intrinsic
2040// and CCValid is the set of possible condition-code results.
2041static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode,
2042 SDValue Call, unsigned CCValid, uint64_t CC,
2043 ISD::CondCode Cond) {
2044 Comparison C(Call, SDValue());
2045 C.Opcode = Opcode;
2046 C.CCValid = CCValid;
2047 if (Cond == ISD::SETEQ)
2048 // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3.
2049 C.CCMask = CC < 4 ? 1 << (3 - CC) : 0;
2050 else if (Cond == ISD::SETNE)
2051 // ...and the inverse of that.
2052 C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1;
2053 else if (Cond == ISD::SETLT || Cond == ISD::SETULT)
2054 // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3,
2055 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002056 C.CCMask = CC < 4 ? ~0U << (4 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002057 else if (Cond == ISD::SETGE || Cond == ISD::SETUGE)
2058 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002059 C.CCMask = CC < 4 ? ~(~0U << (4 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002060 else if (Cond == ISD::SETLE || Cond == ISD::SETULE)
2061 // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true),
2062 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002063 C.CCMask = CC < 4 ? ~0U << (3 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002064 else if (Cond == ISD::SETGT || Cond == ISD::SETUGT)
2065 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002066 C.CCMask = CC < 4 ? ~(~0U << (3 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002067 else
2068 llvm_unreachable("Unexpected integer comparison type");
2069 C.CCMask &= CCValid;
2070 return C;
2071}
2072
Richard Sandifordd420f732013-12-13 15:28:45 +00002073// Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
2074static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002075 ISD::CondCode Cond, SDLoc DL) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002076 if (CmpOp1.getOpcode() == ISD::Constant) {
2077 uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue();
2078 unsigned Opcode, CCValid;
2079 if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
2080 CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) &&
2081 isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid))
2082 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002083 if (CmpOp0.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
2084 CmpOp0.getResNo() == CmpOp0->getNumValues() - 1 &&
2085 isIntrinsicWithCC(CmpOp0, Opcode, CCValid))
2086 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002087 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002088 Comparison C(CmpOp0, CmpOp1);
2089 C.CCMask = CCMaskForCondCode(Cond);
2090 if (C.Op0.getValueType().isFloatingPoint()) {
2091 C.CCValid = SystemZ::CCMASK_FCMP;
2092 C.Opcode = SystemZISD::FCMP;
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002093 adjustForFNeg(C);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002094 } else {
Richard Sandifordd420f732013-12-13 15:28:45 +00002095 C.CCValid = SystemZ::CCMASK_ICMP;
2096 C.Opcode = SystemZISD::ICMP;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002097 // Choose the type of comparison. Equality and inequality tests can
2098 // use either signed or unsigned comparisons. The choice also doesn't
2099 // matter if both sign bits are known to be clear. In those cases we
2100 // want to give the main isel code the freedom to choose whichever
2101 // form fits best.
Richard Sandifordd420f732013-12-13 15:28:45 +00002102 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
2103 C.CCMask == SystemZ::CCMASK_CMP_NE ||
2104 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
2105 C.ICmpType = SystemZICMP::Any;
2106 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
2107 C.ICmpType = SystemZICMP::UnsignedOnly;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002108 else
Richard Sandifordd420f732013-12-13 15:28:45 +00002109 C.ICmpType = SystemZICMP::SignedOnly;
2110 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002111 adjustZeroCmp(DAG, DL, C);
2112 adjustSubwordCmp(DAG, DL, C);
2113 adjustForSubtraction(DAG, DL, C);
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002114 adjustForLTGFR(C);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002115 adjustICmpTruncate(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002116 }
2117
Richard Sandifordd420f732013-12-13 15:28:45 +00002118 if (shouldSwapCmpOperands(C)) {
2119 std::swap(C.Op0, C.Op1);
2120 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford24e597b2013-08-23 11:27:19 +00002121 }
2122
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002123 adjustForTestUnderMask(DAG, DL, C);
Richard Sandifordd420f732013-12-13 15:28:45 +00002124 return C;
2125}
2126
2127// Emit the comparison instruction described by C.
2128static SDValue emitCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002129 if (!C.Op1.getNode()) {
2130 SDValue Op;
2131 switch (C.Op0.getOpcode()) {
2132 case ISD::INTRINSIC_W_CHAIN:
2133 Op = emitIntrinsicWithChainAndGlue(DAG, C.Op0, C.Opcode);
2134 break;
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002135 case ISD::INTRINSIC_WO_CHAIN:
2136 Op = emitIntrinsicWithGlue(DAG, C.Op0, C.Opcode);
2137 break;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002138 default:
2139 llvm_unreachable("Invalid comparison operands");
2140 }
2141 return SDValue(Op.getNode(), Op->getNumValues() - 1);
2142 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002143 if (C.Opcode == SystemZISD::ICMP)
2144 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002145 DAG.getConstant(C.ICmpType, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002146 if (C.Opcode == SystemZISD::TM) {
2147 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
2148 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
2149 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002150 DAG.getConstant(RegisterOnly, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002151 }
2152 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002153}
2154
Richard Sandiford7d86e472013-08-21 09:34:56 +00002155// Implement a 32-bit *MUL_LOHI operation by extending both operands to
2156// 64 bits. Extend is the extension type to use. Store the high part
2157// in Hi and the low part in Lo.
2158static void lowerMUL_LOHI32(SelectionDAG &DAG, SDLoc DL,
2159 unsigned Extend, SDValue Op0, SDValue Op1,
2160 SDValue &Hi, SDValue &Lo) {
2161 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
2162 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
2163 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002164 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
2165 DAG.getConstant(32, DL, MVT::i64));
Richard Sandiford7d86e472013-08-21 09:34:56 +00002166 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
2167 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
2168}
2169
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002170// Lower a binary operation that produces two VT results, one in each
2171// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
2172// Extend extends Op0 to a GR128, and Opcode performs the GR128 operation
2173// on the extended Op0 and (unextended) Op1. Store the even register result
2174// in Even and the odd register result in Odd.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002175static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002176 unsigned Extend, unsigned Opcode,
2177 SDValue Op0, SDValue Op1,
2178 SDValue &Even, SDValue &Odd) {
2179 SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0);
2180 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped,
2181 SDValue(In128, 0), Op1);
2182 bool Is32Bit = is32Bit(VT);
Richard Sandifordd8163202013-09-13 09:12:44 +00002183 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
2184 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002185}
2186
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002187// Return an i32 value that is 1 if the CC value produced by Glue is
2188// in the mask CCMask and 0 otherwise. CC is known to have a value
2189// in CCValid, so other values can be ignored.
2190static SDValue emitSETCC(SelectionDAG &DAG, SDLoc DL, SDValue Glue,
2191 unsigned CCValid, unsigned CCMask) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002192 IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
2193 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
2194
2195 if (Conversion.XORValue)
2196 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002197 DAG.getConstant(Conversion.XORValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002198
2199 if (Conversion.AddValue)
2200 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002201 DAG.getConstant(Conversion.AddValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002202
2203 // The SHR/AND sequence should get optimized to an RISBG.
2204 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002205 DAG.getConstant(Conversion.Bit, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002206 if (Conversion.Bit != 31)
2207 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002208 DAG.getConstant(1, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002209 return Result;
2210}
2211
Ulrich Weigandcd808232015-05-05 19:26:48 +00002212// Return the SystemISD vector comparison operation for CC, or 0 if it cannot
2213// be done directly. IsFP is true if CC is for a floating-point rather than
2214// integer comparison.
2215static unsigned getVectorComparison(ISD::CondCode CC, bool IsFP) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002216 switch (CC) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002217 case ISD::SETOEQ:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002218 case ISD::SETEQ:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002219 return IsFP ? SystemZISD::VFCMPE : SystemZISD::VICMPE;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002220
Ulrich Weigandcd808232015-05-05 19:26:48 +00002221 case ISD::SETOGE:
2222 case ISD::SETGE:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002223 return IsFP ? SystemZISD::VFCMPHE : static_cast<SystemZISD::NodeType>(0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002224
2225 case ISD::SETOGT:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002226 case ISD::SETGT:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002227 return IsFP ? SystemZISD::VFCMPH : SystemZISD::VICMPH;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002228
2229 case ISD::SETUGT:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002230 return IsFP ? static_cast<SystemZISD::NodeType>(0) : SystemZISD::VICMPHL;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002231
2232 default:
2233 return 0;
2234 }
2235}
2236
2237// Return the SystemZISD vector comparison operation for CC or its inverse,
2238// or 0 if neither can be done directly. Indicate in Invert whether the
Ulrich Weigandcd808232015-05-05 19:26:48 +00002239// result is for the inverse of CC. IsFP is true if CC is for a
2240// floating-point rather than integer comparison.
2241static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, bool IsFP,
2242 bool &Invert) {
2243 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002244 Invert = false;
2245 return Opcode;
2246 }
2247
Ulrich Weigandcd808232015-05-05 19:26:48 +00002248 CC = ISD::getSetCCInverse(CC, !IsFP);
2249 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002250 Invert = true;
2251 return Opcode;
2252 }
2253
2254 return 0;
2255}
2256
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002257// Return a v2f64 that contains the extended form of elements Start and Start+1
2258// of v4f32 value Op.
2259static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, SDLoc DL,
2260 SDValue Op) {
2261 int Mask[] = { Start, -1, Start + 1, -1 };
2262 Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask);
2263 return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op);
2264}
2265
2266// Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode,
2267// producing a result of type VT.
2268static SDValue getVectorCmp(SelectionDAG &DAG, unsigned Opcode, SDLoc DL,
2269 EVT VT, SDValue CmpOp0, SDValue CmpOp1) {
2270 // There is no hardware support for v4f32, so extend the vector into
2271 // two v2f64s and compare those.
2272 if (CmpOp0.getValueType() == MVT::v4f32) {
2273 SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0);
2274 SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0);
2275 SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1);
2276 SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1);
2277 SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1);
2278 SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1);
2279 return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes);
2280 }
2281 return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1);
2282}
2283
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002284// Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing
2285// an integer mask of type VT.
2286static SDValue lowerVectorSETCC(SelectionDAG &DAG, SDLoc DL, EVT VT,
2287 ISD::CondCode CC, SDValue CmpOp0,
2288 SDValue CmpOp1) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002289 bool IsFP = CmpOp0.getValueType().isFloatingPoint();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002290 bool Invert = false;
2291 SDValue Cmp;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002292 switch (CC) {
2293 // Handle tests for order using (or (ogt y x) (oge x y)).
2294 case ISD::SETUO:
2295 Invert = true;
2296 case ISD::SETO: {
2297 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002298 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2299 SDValue GE = getVectorCmp(DAG, SystemZISD::VFCMPHE, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002300 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE);
2301 break;
2302 }
2303
2304 // Handle <> tests using (or (ogt y x) (ogt x y)).
2305 case ISD::SETUEQ:
2306 Invert = true;
2307 case ISD::SETONE: {
2308 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002309 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2310 SDValue GT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002311 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT);
2312 break;
2313 }
2314
2315 // Otherwise a single comparison is enough. It doesn't really
2316 // matter whether we try the inversion or the swap first, since
2317 // there are no cases where both work.
2318 default:
2319 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002320 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002321 else {
2322 CC = ISD::getSetCCSwappedOperands(CC);
2323 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002324 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002325 else
2326 llvm_unreachable("Unhandled comparison");
2327 }
2328 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002329 }
2330 if (Invert) {
2331 SDValue Mask = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2332 DAG.getConstant(65535, DL, MVT::i32));
2333 Mask = DAG.getNode(ISD::BITCAST, DL, VT, Mask);
2334 Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask);
2335 }
2336 return Cmp;
2337}
2338
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002339SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
2340 SelectionDAG &DAG) const {
2341 SDValue CmpOp0 = Op.getOperand(0);
2342 SDValue CmpOp1 = Op.getOperand(1);
2343 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
2344 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002345 EVT VT = Op.getValueType();
2346 if (VT.isVector())
2347 return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002348
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002349 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002350 SDValue Glue = emitCmp(DAG, DL, C);
2351 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002352}
2353
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002354SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002355 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2356 SDValue CmpOp0 = Op.getOperand(2);
2357 SDValue CmpOp1 = Op.getOperand(3);
2358 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002359 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002360
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002361 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002362 SDValue Glue = emitCmp(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002363 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002364 Op.getOperand(0), DAG.getConstant(C.CCValid, DL, MVT::i32),
2365 DAG.getConstant(C.CCMask, DL, MVT::i32), Dest, Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002366}
2367
Richard Sandiford57485472013-12-13 15:35:00 +00002368// Return true if Pos is CmpOp and Neg is the negative of CmpOp,
2369// allowing Pos and Neg to be wider than CmpOp.
2370static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
2371 return (Neg.getOpcode() == ISD::SUB &&
2372 Neg.getOperand(0).getOpcode() == ISD::Constant &&
2373 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
2374 Neg.getOperand(1) == Pos &&
2375 (Pos == CmpOp ||
2376 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
2377 Pos.getOperand(0) == CmpOp)));
2378}
2379
2380// Return the absolute or negative absolute of Op; IsNegative decides which.
2381static SDValue getAbsolute(SelectionDAG &DAG, SDLoc DL, SDValue Op,
2382 bool IsNegative) {
2383 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
2384 if (IsNegative)
2385 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002386 DAG.getConstant(0, DL, Op.getValueType()), Op);
Richard Sandiford57485472013-12-13 15:35:00 +00002387 return Op;
2388}
2389
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002390SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
2391 SelectionDAG &DAG) const {
2392 SDValue CmpOp0 = Op.getOperand(0);
2393 SDValue CmpOp1 = Op.getOperand(1);
2394 SDValue TrueOp = Op.getOperand(2);
2395 SDValue FalseOp = Op.getOperand(3);
2396 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002397 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002398
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002399 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandiford57485472013-12-13 15:35:00 +00002400
2401 // Check for absolute and negative-absolute selections, including those
2402 // where the comparison value is sign-extended (for LPGFR and LNGFR).
2403 // This check supplements the one in DAGCombiner.
2404 if (C.Opcode == SystemZISD::ICMP &&
2405 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
2406 C.CCMask != SystemZ::CCMASK_CMP_NE &&
2407 C.Op1.getOpcode() == ISD::Constant &&
2408 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
2409 if (isAbsolute(C.Op0, TrueOp, FalseOp))
2410 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
2411 if (isAbsolute(C.Op0, FalseOp, TrueOp))
2412 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
2413 }
2414
Richard Sandifordd420f732013-12-13 15:28:45 +00002415 SDValue Glue = emitCmp(DAG, DL, C);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002416
2417 // Special case for handling -1/0 results. The shifts we use here
2418 // should get optimized with the IPM conversion sequence.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002419 auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
2420 auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002421 if (TrueC && FalseC) {
2422 int64_t TrueVal = TrueC->getSExtValue();
2423 int64_t FalseVal = FalseC->getSExtValue();
2424 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
2425 // Invert the condition if we want -1 on false.
2426 if (TrueVal == 0)
Richard Sandifordd420f732013-12-13 15:28:45 +00002427 C.CCMask ^= C.CCValid;
2428 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002429 EVT VT = Op.getValueType();
2430 // Extend the result to VT. Upper bits are ignored.
2431 if (!is32Bit(VT))
2432 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
2433 // Sign-extend from the low bit.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002434 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002435 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
2436 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
2437 }
2438 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002439
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002440 SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, DL, MVT::i32),
2441 DAG.getConstant(C.CCMask, DL, MVT::i32), Glue};
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002442
2443 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00002444 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002445}
2446
2447SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
2448 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002449 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002450 const GlobalValue *GV = Node->getGlobal();
2451 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002452 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopher93bf97c2014-06-27 07:38:01 +00002453 Reloc::Model RM = DAG.getTarget().getRelocationModel();
2454 CodeModel::Model CM = DAG.getTarget().getCodeModel();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002455
2456 SDValue Result;
2457 if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) {
Richard Sandiford54b36912013-09-27 15:14:04 +00002458 // Assign anchors at 1<<12 byte boundaries.
2459 uint64_t Anchor = Offset & ~uint64_t(0xfff);
2460 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
2461 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2462
2463 // The offset can be folded into the address if it is aligned to a halfword.
2464 Offset -= Anchor;
2465 if (Offset != 0 && (Offset & 1) == 0) {
2466 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
2467 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002468 Offset = 0;
2469 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002470 } else {
2471 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
2472 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2473 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002474 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2475 false, false, false, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002476 }
2477
2478 // If there was a non-zero offset that we didn't fold, create an explicit
2479 // addition for it.
2480 if (Offset != 0)
2481 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002482 DAG.getConstant(Offset, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002483
2484 return Result;
2485}
2486
Ulrich Weigand7db69182015-02-18 09:13:27 +00002487SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
2488 SelectionDAG &DAG,
2489 unsigned Opcode,
2490 SDValue GOTOffset) const {
2491 SDLoc DL(Node);
Mehdi Amini44ede332015-07-09 02:09:04 +00002492 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand7db69182015-02-18 09:13:27 +00002493 SDValue Chain = DAG.getEntryNode();
2494 SDValue Glue;
2495
2496 // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
2497 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
2498 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
2499 Glue = Chain.getValue(1);
2500 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
2501 Glue = Chain.getValue(1);
2502
2503 // The first call operand is the chain and the second is the TLS symbol.
2504 SmallVector<SDValue, 8> Ops;
2505 Ops.push_back(Chain);
2506 Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
2507 Node->getValueType(0),
2508 0, 0));
2509
2510 // Add argument registers to the end of the list so that they are
2511 // known live into the call.
2512 Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
2513 Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
2514
2515 // Add a register mask operand representing the call-preserved registers.
2516 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002517 const uint32_t *Mask =
2518 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002519 assert(Mask && "Missing call preserved mask for calling convention");
2520 Ops.push_back(DAG.getRegisterMask(Mask));
2521
2522 // Glue the call to the argument copies.
2523 Ops.push_back(Glue);
2524
2525 // Emit the call.
2526 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2527 Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
2528 Glue = Chain.getValue(1);
2529
2530 // Copy the return value from %r2.
2531 return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
2532}
2533
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002534SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002535 SelectionDAG &DAG) const {
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002536 if (DAG.getTarget().Options.EmulatedTLS)
2537 return LowerToTLSEmulatedModel(Node, DAG);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002538 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002539 const GlobalValue *GV = Node->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00002540 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopher93bf97c2014-06-27 07:38:01 +00002541 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002542
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002543 // The high part of the thread pointer is in access register 0.
2544 SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002545 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002546 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
2547
2548 // The low part of the thread pointer is in access register 1.
2549 SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002550 DAG.getConstant(1, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002551 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
2552
2553 // Merge them into a single 64-bit address.
2554 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002555 DAG.getConstant(32, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002556 SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
2557
Ulrich Weigand7db69182015-02-18 09:13:27 +00002558 // Get the offset of GA from the thread pointer, based on the TLS model.
2559 SDValue Offset;
2560 switch (model) {
2561 case TLSModel::GeneralDynamic: {
2562 // Load the GOT offset of the tls_index (module ID / per-symbol offset).
2563 SystemZConstantPoolValue *CPV =
2564 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002565
Ulrich Weigand7db69182015-02-18 09:13:27 +00002566 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002567 Offset = DAG.getLoad(
2568 PtrVT, DL, DAG.getEntryNode(), Offset,
2569 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2570 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002571
2572 // Call __tls_get_offset to retrieve the offset.
2573 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
2574 break;
2575 }
2576
2577 case TLSModel::LocalDynamic: {
2578 // Load the GOT offset of the module ID.
2579 SystemZConstantPoolValue *CPV =
2580 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
2581
2582 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002583 Offset = DAG.getLoad(
2584 PtrVT, DL, DAG.getEntryNode(), Offset,
2585 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2586 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002587
2588 // Call __tls_get_offset to retrieve the module base offset.
2589 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
2590
2591 // Note: The SystemZLDCleanupPass will remove redundant computations
2592 // of the module base offset. Count total number of local-dynamic
2593 // accesses to trigger execution of that pass.
2594 SystemZMachineFunctionInfo* MFI =
2595 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
2596 MFI->incNumLocalDynamicTLSAccesses();
2597
2598 // Add the per-symbol offset.
2599 CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
2600
2601 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002602 DTPOffset = DAG.getLoad(
2603 PtrVT, DL, DAG.getEntryNode(), DTPOffset,
2604 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2605 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002606
2607 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
2608 break;
2609 }
2610
2611 case TLSModel::InitialExec: {
2612 // Load the offset from the GOT.
2613 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2614 SystemZII::MO_INDNTPOFF);
2615 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002616 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Offset,
2617 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
Ulrich Weigand7db69182015-02-18 09:13:27 +00002618 false, false, false, 0);
2619 break;
2620 }
2621
2622 case TLSModel::LocalExec: {
2623 // Force the offset into the constant pool and load it from there.
2624 SystemZConstantPoolValue *CPV =
2625 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
2626
2627 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002628 Offset = DAG.getLoad(
2629 PtrVT, DL, DAG.getEntryNode(), Offset,
2630 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2631 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002632 break;
Ulrich Weigandb7e59092015-02-18 09:42:23 +00002633 }
Ulrich Weigand7db69182015-02-18 09:13:27 +00002634 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002635
2636 // Add the base and offset together.
2637 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
2638}
2639
2640SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
2641 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002642 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002643 const BlockAddress *BA = Node->getBlockAddress();
2644 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002645 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002646
2647 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
2648 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2649 return Result;
2650}
2651
2652SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
2653 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002654 SDLoc DL(JT);
Mehdi Amini44ede332015-07-09 02:09:04 +00002655 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002656 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
2657
2658 // Use LARL to load the address of the table.
2659 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2660}
2661
2662SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
2663 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002664 SDLoc DL(CP);
Mehdi Amini44ede332015-07-09 02:09:04 +00002665 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002666
2667 SDValue Result;
2668 if (CP->isMachineConstantPoolEntry())
2669 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002670 CP->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002671 else
2672 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002673 CP->getAlignment(), CP->getOffset());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002674
2675 // Use LARL to load the address of the constant pool entry.
2676 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2677}
2678
2679SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
2680 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002681 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002682 SDValue In = Op.getOperand(0);
2683 EVT InVT = In.getValueType();
2684 EVT ResVT = Op.getValueType();
2685
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002686 // Convert loads directly. This is normally done by DAGCombiner,
2687 // but we need this case for bitcasts that are created during lowering
2688 // and which are then lowered themselves.
2689 if (auto *LoadN = dyn_cast<LoadSDNode>(In))
2690 return DAG.getLoad(ResVT, DL, LoadN->getChain(), LoadN->getBasePtr(),
2691 LoadN->getMemOperand());
2692
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002693 if (InVT == MVT::i32 && ResVT == MVT::f32) {
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002694 SDValue In64;
2695 if (Subtarget.hasHighWord()) {
2696 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
2697 MVT::i64);
2698 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
2699 MVT::i64, SDValue(U64, 0), In);
2700 } else {
2701 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
2702 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002703 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002704 }
2705 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002706 return DAG.getTargetExtractSubreg(SystemZ::subreg_r32,
Richard Sandifordd8163202013-09-13 09:12:44 +00002707 DL, MVT::f32, Out64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002708 }
2709 if (InVT == MVT::f32 && ResVT == MVT::i32) {
2710 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002711 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002712 MVT::f64, SDValue(U64, 0), In);
2713 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002714 if (Subtarget.hasHighWord())
2715 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
2716 MVT::i32, Out64);
2717 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002718 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002719 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002720 }
2721 llvm_unreachable("Unexpected bitcast combination");
2722}
2723
2724SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
2725 SelectionDAG &DAG) const {
2726 MachineFunction &MF = DAG.getMachineFunction();
2727 SystemZMachineFunctionInfo *FuncInfo =
2728 MF.getInfo<SystemZMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002729 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002730
2731 SDValue Chain = Op.getOperand(0);
2732 SDValue Addr = Op.getOperand(1);
2733 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002734 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002735
2736 // The initial values of each field.
2737 const unsigned NumFields = 4;
2738 SDValue Fields[NumFields] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002739 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT),
2740 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002741 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
2742 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
2743 };
2744
2745 // Store each field into its respective slot.
2746 SDValue MemOps[NumFields];
2747 unsigned Offset = 0;
2748 for (unsigned I = 0; I < NumFields; ++I) {
2749 SDValue FieldAddr = Addr;
2750 if (Offset != 0)
2751 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002752 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002753 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
2754 MachinePointerInfo(SV, Offset),
2755 false, false, 0);
2756 Offset += 8;
2757 }
Craig Topper48d114b2014-04-26 18:35:24 +00002758 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002759}
2760
2761SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
2762 SelectionDAG &DAG) const {
2763 SDValue Chain = Op.getOperand(0);
2764 SDValue DstPtr = Op.getOperand(1);
2765 SDValue SrcPtr = Op.getOperand(2);
2766 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
2767 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002768 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002769
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002770 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002771 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00002772 /*isTailCall*/false,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002773 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
2774}
2775
2776SDValue SystemZTargetLowering::
2777lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002778 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
2779 bool RealignOpt = !DAG.getMachineFunction().getFunction()->
2780 hasFnAttribute("no-realign-stack");
2781
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002782 SDValue Chain = Op.getOperand(0);
2783 SDValue Size = Op.getOperand(1);
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002784 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002785 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002786
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002787 // If user has set the no alignment function attribute, ignore
2788 // alloca alignments.
2789 uint64_t AlignVal = (RealignOpt ?
2790 dyn_cast<ConstantSDNode>(Align)->getZExtValue() : 0);
2791
2792 uint64_t StackAlign = TFI->getStackAlignment();
2793 uint64_t RequiredAlign = std::max(AlignVal, StackAlign);
2794 uint64_t ExtraAlignSpace = RequiredAlign - StackAlign;
2795
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002796 unsigned SPReg = getStackPointerRegisterToSaveRestore();
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002797 SDValue NeededSpace = Size;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002798
2799 // Get a reference to the stack pointer.
2800 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
2801
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002802 // Add extra space for alignment if needed.
2803 if (ExtraAlignSpace)
2804 NeededSpace = DAG.getNode(ISD::ADD, DL, MVT::i64, NeededSpace,
2805 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
2806
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002807 // Get the new stack pointer value.
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002808 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, NeededSpace);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002809
2810 // Copy the new stack pointer back.
2811 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
2812
2813 // The allocated data lives above the 160 bytes allocated for the standard
2814 // frame, plus any outgoing stack arguments. We don't know how much that
2815 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
2816 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
2817 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
2818
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002819 // Dynamically realign if needed.
2820 if (RequiredAlign > StackAlign) {
2821 Result =
2822 DAG.getNode(ISD::ADD, DL, MVT::i64, Result,
2823 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
2824 Result =
2825 DAG.getNode(ISD::AND, DL, MVT::i64, Result,
2826 DAG.getConstant(~(RequiredAlign - 1), DL, MVT::i64));
2827 }
2828
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002829 SDValue Ops[2] = { Result, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002830 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002831}
2832
Richard Sandiford7d86e472013-08-21 09:34:56 +00002833SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
2834 SelectionDAG &DAG) const {
2835 EVT VT = Op.getValueType();
2836 SDLoc DL(Op);
2837 SDValue Ops[2];
2838 if (is32Bit(VT))
2839 // Just do a normal 64-bit multiplication and extract the results.
2840 // We define this so that it can be used for constant division.
2841 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
2842 Op.getOperand(1), Ops[1], Ops[0]);
2843 else {
2844 // Do a full 128-bit multiplication based on UMUL_LOHI64:
2845 //
2846 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
2847 //
2848 // but using the fact that the upper halves are either all zeros
2849 // or all ones:
2850 //
2851 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
2852 //
2853 // and grouping the right terms together since they are quicker than the
2854 // multiplication:
2855 //
2856 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002857 SDValue C63 = DAG.getConstant(63, DL, MVT::i64);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002858 SDValue LL = Op.getOperand(0);
2859 SDValue RL = Op.getOperand(1);
2860 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
2861 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
2862 // UMUL_LOHI64 returns the low result in the odd register and the high
2863 // result in the even register. SMUL_LOHI is defined to return the
2864 // low half first, so the results are in reverse order.
2865 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2866 LL, RL, Ops[1], Ops[0]);
2867 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
2868 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
2869 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
2870 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
2871 }
Craig Topper64941d92014-04-27 19:20:57 +00002872 return DAG.getMergeValues(Ops, DL);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002873}
2874
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002875SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
2876 SelectionDAG &DAG) const {
2877 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002878 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002879 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00002880 if (is32Bit(VT))
2881 // Just do a normal 64-bit multiplication and extract the results.
2882 // We define this so that it can be used for constant division.
2883 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
2884 Op.getOperand(1), Ops[1], Ops[0]);
2885 else
2886 // UMUL_LOHI64 returns the low result in the odd register and the high
2887 // result in the even register. UMUL_LOHI is defined to return the
2888 // low half first, so the results are in reverse order.
2889 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2890 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002891 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002892}
2893
2894SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
2895 SelectionDAG &DAG) const {
2896 SDValue Op0 = Op.getOperand(0);
2897 SDValue Op1 = Op.getOperand(1);
2898 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002899 SDLoc DL(Op);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002900 unsigned Opcode;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002901
2902 // We use DSGF for 32-bit division.
2903 if (is32Bit(VT)) {
2904 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002905 Opcode = SystemZISD::SDIVREM32;
2906 } else if (DAG.ComputeNumSignBits(Op1) > 32) {
2907 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
2908 Opcode = SystemZISD::SDIVREM32;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00002909 } else
Richard Sandiforde6e78852013-07-02 15:40:22 +00002910 Opcode = SystemZISD::SDIVREM64;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002911
2912 // DSG(F) takes a 64-bit dividend, so the even register in the GR128
2913 // input is "don't care". The instruction returns the remainder in
2914 // the even register and the quotient in the odd register.
2915 SDValue Ops[2];
Richard Sandiforde6e78852013-07-02 15:40:22 +00002916 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002917 Op0, Op1, Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002918 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002919}
2920
2921SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
2922 SelectionDAG &DAG) const {
2923 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002924 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002925
2926 // DL(G) uses a double-width dividend, so we need to clear the even
2927 // register in the GR128 input. The instruction returns the remainder
2928 // in the even register and the quotient in the odd register.
2929 SDValue Ops[2];
2930 if (is32Bit(VT))
2931 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
2932 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2933 else
2934 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
2935 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002936 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002937}
2938
2939SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
2940 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
2941
2942 // Get the known-zero masks for each operand.
2943 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
2944 APInt KnownZero[2], KnownOne[2];
Jay Foada0653a32014-05-14 21:14:37 +00002945 DAG.computeKnownBits(Ops[0], KnownZero[0], KnownOne[0]);
2946 DAG.computeKnownBits(Ops[1], KnownZero[1], KnownOne[1]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002947
2948 // See if the upper 32 bits of one operand and the lower 32 bits of the
2949 // other are known zero. They are the low and high operands respectively.
2950 uint64_t Masks[] = { KnownZero[0].getZExtValue(),
2951 KnownZero[1].getZExtValue() };
2952 unsigned High, Low;
2953 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
2954 High = 1, Low = 0;
2955 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
2956 High = 0, Low = 1;
2957 else
2958 return Op;
2959
2960 SDValue LowOp = Ops[Low];
2961 SDValue HighOp = Ops[High];
2962
2963 // If the high part is a constant, we're better off using IILH.
2964 if (HighOp.getOpcode() == ISD::Constant)
2965 return Op;
2966
2967 // If the low part is a constant that is outside the range of LHI,
2968 // then we're better off using IILF.
2969 if (LowOp.getOpcode() == ISD::Constant) {
2970 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
2971 if (!isInt<16>(Value))
2972 return Op;
2973 }
2974
2975 // Check whether the high part is an AND that doesn't change the
2976 // high 32 bits and just masks out low bits. We can skip it if so.
2977 if (HighOp.getOpcode() == ISD::AND &&
2978 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
Richard Sandifordccc2a7c2013-12-03 11:01:54 +00002979 SDValue HighOp0 = HighOp.getOperand(0);
2980 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
2981 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
2982 HighOp = HighOp0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002983 }
2984
2985 // Take advantage of the fact that all GR32 operations only change the
2986 // low 32 bits by truncating Low to an i32 and inserting it directly
2987 // using a subreg. The interesting cases are those where the truncation
2988 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002989 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002990 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
Richard Sandiford87a44362013-09-30 10:28:35 +00002991 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002992 MVT::i64, HighOp, Low32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002993}
2994
Ulrich Weigandb4012182015-03-31 12:56:33 +00002995SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
2996 SelectionDAG &DAG) const {
2997 EVT VT = Op.getValueType();
Ulrich Weigandb4012182015-03-31 12:56:33 +00002998 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002999 Op = Op.getOperand(0);
3000
3001 // Handle vector types via VPOPCT.
3002 if (VT.isVector()) {
3003 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op);
3004 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op);
3005 switch (VT.getVectorElementType().getSizeInBits()) {
3006 case 8:
3007 break;
3008 case 16: {
3009 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
3010 SDValue Shift = DAG.getConstant(8, DL, MVT::i32);
3011 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift);
3012 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3013 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift);
3014 break;
3015 }
3016 case 32: {
3017 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3018 DAG.getConstant(0, DL, MVT::i32));
3019 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3020 break;
3021 }
3022 case 64: {
3023 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3024 DAG.getConstant(0, DL, MVT::i32));
3025 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp);
3026 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3027 break;
3028 }
3029 default:
3030 llvm_unreachable("Unexpected type");
3031 }
3032 return Op;
3033 }
Ulrich Weigandb4012182015-03-31 12:56:33 +00003034
3035 // Get the known-zero mask for the operand.
Ulrich Weigandb4012182015-03-31 12:56:33 +00003036 APInt KnownZero, KnownOne;
3037 DAG.computeKnownBits(Op, KnownZero, KnownOne);
Ulrich Weigand050527b2015-03-31 19:28:50 +00003038 unsigned NumSignificantBits = (~KnownZero).getActiveBits();
3039 if (NumSignificantBits == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003040 return DAG.getConstant(0, DL, VT);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003041
3042 // Skip known-zero high parts of the operand.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003043 int64_t OrigBitSize = VT.getSizeInBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003044 int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits);
3045 BitSize = std::min(BitSize, OrigBitSize);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003046
3047 // The POPCNT instruction counts the number of bits in each byte.
3048 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op);
3049 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op);
3050 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
3051
3052 // Add up per-byte counts in a binary tree. All bits of Op at
3053 // position larger than BitSize remain zero throughout.
3054 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003055 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003056 if (BitSize != OrigBitSize)
3057 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003058 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003059 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3060 }
3061
3062 // Extract overall result from high byte.
3063 if (BitSize > 8)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003064 Op = DAG.getNode(ISD::SRL, DL, VT, Op,
3065 DAG.getConstant(BitSize - 8, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003066
3067 return Op;
3068}
3069
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003070// Op is an atomic load. Lower it into a normal volatile load.
3071SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
3072 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003073 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003074 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
3075 Node->getChain(), Node->getBasePtr(),
3076 Node->getMemoryVT(), Node->getMemOperand());
3077}
3078
3079// Op is an atomic store. Lower it into a normal volatile store followed
3080// by a serialization.
3081SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
3082 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003083 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003084 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
3085 Node->getBasePtr(), Node->getMemoryVT(),
3086 Node->getMemOperand());
3087 return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other,
3088 Chain), 0);
3089}
3090
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003091// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
3092// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003093SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
3094 SelectionDAG &DAG,
3095 unsigned Opcode) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003096 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003097
3098 // 32-bit operations need no code outside the main loop.
3099 EVT NarrowVT = Node->getMemoryVT();
3100 EVT WideVT = MVT::i32;
3101 if (NarrowVT == WideVT)
3102 return Op;
3103
3104 int64_t BitSize = NarrowVT.getSizeInBits();
3105 SDValue ChainIn = Node->getChain();
3106 SDValue Addr = Node->getBasePtr();
3107 SDValue Src2 = Node->getVal();
3108 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003109 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003110 EVT PtrVT = Addr.getValueType();
3111
3112 // Convert atomic subtracts of constants into additions.
3113 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
Richard Sandiford21f5d682014-03-06 11:22:58 +00003114 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003115 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003116 Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003117 }
3118
3119 // Get the address of the containing word.
3120 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003121 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003122
3123 // Get the number of bits that the word must be rotated left in order
3124 // to bring the field to the top bits of a GR32.
3125 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003126 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003127 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3128
3129 // Get the complementing shift amount, for rotating a field in the top
3130 // bits back to its proper position.
3131 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003132 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003133
3134 // Extend the source operand to 32 bits and prepare it for the inner loop.
3135 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
3136 // operations require the source to be shifted in advance. (This shift
3137 // can be folded if the source is constant.) For AND and NAND, the lower
3138 // bits must be set, while for other opcodes they should be left clear.
3139 if (Opcode != SystemZISD::ATOMIC_SWAPW)
3140 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003141 DAG.getConstant(32 - BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003142 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
3143 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
3144 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003145 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003146
3147 // Construct the ATOMIC_LOADW_* node.
3148 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3149 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003150 DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003151 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003152 NarrowVT, MMO);
3153
3154 // Rotate the result of the final CS so that the field is in the lower
3155 // bits of a GR32, then truncate it.
3156 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003157 DAG.getConstant(BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003158 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
3159
3160 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00003161 return DAG.getMergeValues(RetOps, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003162}
3163
Richard Sandiford41350a52013-12-24 15:18:04 +00003164// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
Richard Sandiford002019a2013-12-24 15:22:39 +00003165// into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
Richard Sandiford41350a52013-12-24 15:18:04 +00003166// operations into additions.
3167SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
3168 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003169 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandiford41350a52013-12-24 15:18:04 +00003170 EVT MemVT = Node->getMemoryVT();
3171 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
3172 // A full-width operation.
3173 assert(Op.getValueType() == MemVT && "Mismatched VTs");
3174 SDValue Src2 = Node->getVal();
3175 SDValue NegSrc2;
3176 SDLoc DL(Src2);
3177
Richard Sandiford21f5d682014-03-06 11:22:58 +00003178 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
Richard Sandiford41350a52013-12-24 15:18:04 +00003179 // Use an addition if the operand is constant and either LAA(G) is
3180 // available or the negative value is in the range of A(G)FHI.
3181 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
Eric Christopher93bf97c2014-06-27 07:38:01 +00003182 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003183 NegSrc2 = DAG.getConstant(Value, DL, MemVT);
Eric Christopher93bf97c2014-06-27 07:38:01 +00003184 } else if (Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00003185 // Use LAA(G) if available.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003186 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT),
Richard Sandiford41350a52013-12-24 15:18:04 +00003187 Src2);
3188
3189 if (NegSrc2.getNode())
3190 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
3191 Node->getChain(), Node->getBasePtr(), NegSrc2,
3192 Node->getMemOperand(), Node->getOrdering(),
3193 Node->getSynchScope());
3194
3195 // Use the node as-is.
3196 return Op;
3197 }
3198
3199 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
3200}
3201
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003202// Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
3203// into a fullword ATOMIC_CMP_SWAPW operation.
3204SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
3205 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003206 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003207
3208 // We have native support for 32-bit compare and swap.
3209 EVT NarrowVT = Node->getMemoryVT();
3210 EVT WideVT = MVT::i32;
3211 if (NarrowVT == WideVT)
3212 return Op;
3213
3214 int64_t BitSize = NarrowVT.getSizeInBits();
3215 SDValue ChainIn = Node->getOperand(0);
3216 SDValue Addr = Node->getOperand(1);
3217 SDValue CmpVal = Node->getOperand(2);
3218 SDValue SwapVal = Node->getOperand(3);
3219 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003220 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003221 EVT PtrVT = Addr.getValueType();
3222
3223 // Get the address of the containing word.
3224 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003225 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003226
3227 // Get the number of bits that the word must be rotated left in order
3228 // to bring the field to the top bits of a GR32.
3229 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003230 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003231 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3232
3233 // Get the complementing shift amount, for rotating a field in the top
3234 // bits back to its proper position.
3235 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003236 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003237
3238 // Construct the ATOMIC_CMP_SWAPW node.
3239 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3240 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003241 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003242 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003243 VTList, Ops, NarrowVT, MMO);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003244 return AtomicOp;
3245}
3246
3247SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
3248 SelectionDAG &DAG) const {
3249 MachineFunction &MF = DAG.getMachineFunction();
3250 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003251 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003252 SystemZ::R15D, Op.getValueType());
3253}
3254
3255SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
3256 SelectionDAG &DAG) const {
3257 MachineFunction &MF = DAG.getMachineFunction();
3258 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003259 return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003260 SystemZ::R15D, Op.getOperand(1));
3261}
3262
Richard Sandiford03481332013-08-23 11:36:42 +00003263SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
3264 SelectionDAG &DAG) const {
3265 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3266 if (!IsData)
3267 // Just preserve the chain.
3268 return Op.getOperand(0);
3269
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003270 SDLoc DL(Op);
Richard Sandiford03481332013-08-23 11:36:42 +00003271 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
3272 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
Richard Sandiford21f5d682014-03-06 11:22:58 +00003273 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
Richard Sandiford03481332013-08-23 11:36:42 +00003274 SDValue Ops[] = {
3275 Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003276 DAG.getConstant(Code, DL, MVT::i32),
Richard Sandiford03481332013-08-23 11:36:42 +00003277 Op.getOperand(1)
3278 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003279 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003280 Node->getVTList(), Ops,
Richard Sandiford03481332013-08-23 11:36:42 +00003281 Node->getMemoryVT(), Node->getMemOperand());
3282}
3283
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003284// Return an i32 that contains the value of CC immediately after After,
3285// whose final operand must be MVT::Glue.
3286static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003287 SDLoc DL(After);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003288 SDValue Glue = SDValue(After, After->getNumValues() - 1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003289 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
3290 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
3291 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003292}
3293
3294SDValue
3295SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
3296 SelectionDAG &DAG) const {
3297 unsigned Opcode, CCValid;
3298 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) {
3299 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
3300 SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode);
3301 SDValue CC = getCCResult(DAG, Glued.getNode());
3302 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC);
3303 return SDValue();
3304 }
3305
3306 return SDValue();
3307}
3308
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003309SDValue
3310SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
3311 SelectionDAG &DAG) const {
3312 unsigned Opcode, CCValid;
3313 if (isIntrinsicWithCC(Op, Opcode, CCValid)) {
3314 SDValue Glued = emitIntrinsicWithGlue(DAG, Op, Opcode);
3315 SDValue CC = getCCResult(DAG, Glued.getNode());
3316 if (Op->getNumValues() == 1)
3317 return CC;
3318 assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result");
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00003319 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), Glued,
3320 CC);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003321 }
3322
3323 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3324 switch (Id) {
3325 case Intrinsic::s390_vpdi:
3326 return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(),
3327 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3328
3329 case Intrinsic::s390_vperm:
3330 return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(),
3331 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3332
3333 case Intrinsic::s390_vuphb:
3334 case Intrinsic::s390_vuphh:
3335 case Intrinsic::s390_vuphf:
3336 return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(),
3337 Op.getOperand(1));
3338
3339 case Intrinsic::s390_vuplhb:
3340 case Intrinsic::s390_vuplhh:
3341 case Intrinsic::s390_vuplhf:
3342 return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(),
3343 Op.getOperand(1));
3344
3345 case Intrinsic::s390_vuplb:
3346 case Intrinsic::s390_vuplhw:
3347 case Intrinsic::s390_vuplf:
3348 return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(),
3349 Op.getOperand(1));
3350
3351 case Intrinsic::s390_vupllb:
3352 case Intrinsic::s390_vupllh:
3353 case Intrinsic::s390_vupllf:
3354 return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(),
3355 Op.getOperand(1));
3356
3357 case Intrinsic::s390_vsumb:
3358 case Intrinsic::s390_vsumh:
3359 case Intrinsic::s390_vsumgh:
3360 case Intrinsic::s390_vsumgf:
3361 case Intrinsic::s390_vsumqf:
3362 case Intrinsic::s390_vsumqg:
3363 return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(),
3364 Op.getOperand(1), Op.getOperand(2));
3365 }
3366
3367 return SDValue();
3368}
3369
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003370namespace {
3371// Says that SystemZISD operation Opcode can be used to perform the equivalent
3372// of a VPERM with permute vector Bytes. If Opcode takes three operands,
3373// Operand is the constant third operand, otherwise it is the number of
3374// bytes in each element of the result.
3375struct Permute {
3376 unsigned Opcode;
3377 unsigned Operand;
3378 unsigned char Bytes[SystemZ::VectorBytes];
3379};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003380}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003381
3382static const Permute PermuteForms[] = {
3383 // VMRHG
3384 { SystemZISD::MERGE_HIGH, 8,
3385 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } },
3386 // VMRHF
3387 { SystemZISD::MERGE_HIGH, 4,
3388 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
3389 // VMRHH
3390 { SystemZISD::MERGE_HIGH, 2,
3391 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
3392 // VMRHB
3393 { SystemZISD::MERGE_HIGH, 1,
3394 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
3395 // VMRLG
3396 { SystemZISD::MERGE_LOW, 8,
3397 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } },
3398 // VMRLF
3399 { SystemZISD::MERGE_LOW, 4,
3400 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
3401 // VMRLH
3402 { SystemZISD::MERGE_LOW, 2,
3403 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
3404 // VMRLB
3405 { SystemZISD::MERGE_LOW, 1,
3406 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
3407 // VPKG
3408 { SystemZISD::PACK, 4,
3409 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } },
3410 // VPKF
3411 { SystemZISD::PACK, 2,
3412 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
3413 // VPKH
3414 { SystemZISD::PACK, 1,
3415 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
3416 // VPDI V1, V2, 4 (low half of V1, high half of V2)
3417 { SystemZISD::PERMUTE_DWORDS, 4,
3418 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } },
3419 // VPDI V1, V2, 1 (high half of V1, low half of V2)
3420 { SystemZISD::PERMUTE_DWORDS, 1,
3421 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } }
3422};
3423
3424// Called after matching a vector shuffle against a particular pattern.
3425// Both the original shuffle and the pattern have two vector operands.
3426// OpNos[0] is the operand of the original shuffle that should be used for
3427// operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything.
3428// OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and
3429// set OpNo0 and OpNo1 to the shuffle operands that should actually be used
3430// for operands 0 and 1 of the pattern.
3431static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) {
3432 if (OpNos[0] < 0) {
3433 if (OpNos[1] < 0)
3434 return false;
3435 OpNo0 = OpNo1 = OpNos[1];
3436 } else if (OpNos[1] < 0) {
3437 OpNo0 = OpNo1 = OpNos[0];
3438 } else {
3439 OpNo0 = OpNos[0];
3440 OpNo1 = OpNos[1];
3441 }
3442 return true;
3443}
3444
3445// Bytes is a VPERM-like permute vector, except that -1 is used for
3446// undefined bytes. Return true if the VPERM can be implemented using P.
3447// When returning true set OpNo0 to the VPERM operand that should be
3448// used for operand 0 of P and likewise OpNo1 for operand 1 of P.
3449//
3450// For example, if swapping the VPERM operands allows P to match, OpNo0
3451// will be 1 and OpNo1 will be 0. If instead Bytes only refers to one
3452// operand, but rewriting it to use two duplicated operands allows it to
3453// match P, then OpNo0 and OpNo1 will be the same.
3454static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P,
3455 unsigned &OpNo0, unsigned &OpNo1) {
3456 int OpNos[] = { -1, -1 };
3457 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
3458 int Elt = Bytes[I];
3459 if (Elt >= 0) {
3460 // Make sure that the two permute vectors use the same suboperand
3461 // byte number. Only the operand numbers (the high bits) are
3462 // allowed to differ.
3463 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1))
3464 return false;
3465 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes;
3466 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes;
3467 // Make sure that the operand mappings are consistent with previous
3468 // elements.
3469 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3470 return false;
3471 OpNos[ModelOpNo] = RealOpNo;
3472 }
3473 }
3474 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3475}
3476
3477// As above, but search for a matching permute.
3478static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes,
3479 unsigned &OpNo0, unsigned &OpNo1) {
3480 for (auto &P : PermuteForms)
3481 if (matchPermute(Bytes, P, OpNo0, OpNo1))
3482 return &P;
3483 return nullptr;
3484}
3485
3486// Bytes is a VPERM-like permute vector, except that -1 is used for
3487// undefined bytes. This permute is an operand of an outer permute.
3488// See whether redistributing the -1 bytes gives a shuffle that can be
3489// implemented using P. If so, set Transform to a VPERM-like permute vector
3490// that, when applied to the result of P, gives the original permute in Bytes.
3491static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3492 const Permute &P,
3493 SmallVectorImpl<int> &Transform) {
3494 unsigned To = 0;
3495 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) {
3496 int Elt = Bytes[From];
3497 if (Elt < 0)
3498 // Byte number From of the result is undefined.
3499 Transform[From] = -1;
3500 else {
3501 while (P.Bytes[To] != Elt) {
3502 To += 1;
3503 if (To == SystemZ::VectorBytes)
3504 return false;
3505 }
3506 Transform[From] = To;
3507 }
3508 }
3509 return true;
3510}
3511
3512// As above, but search for a matching permute.
3513static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3514 SmallVectorImpl<int> &Transform) {
3515 for (auto &P : PermuteForms)
3516 if (matchDoublePermute(Bytes, P, Transform))
3517 return &P;
3518 return nullptr;
3519}
3520
3521// Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask,
3522// as if it had type vNi8.
3523static void getVPermMask(ShuffleVectorSDNode *VSN,
3524 SmallVectorImpl<int> &Bytes) {
3525 EVT VT = VSN->getValueType(0);
3526 unsigned NumElements = VT.getVectorNumElements();
3527 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3528 Bytes.resize(NumElements * BytesPerElement, -1);
3529 for (unsigned I = 0; I < NumElements; ++I) {
3530 int Index = VSN->getMaskElt(I);
3531 if (Index >= 0)
3532 for (unsigned J = 0; J < BytesPerElement; ++J)
3533 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
3534 }
3535}
3536
3537// Bytes is a VPERM-like permute vector, except that -1 is used for
3538// undefined bytes. See whether bytes [Start, Start + BytesPerElement) of
3539// the result come from a contiguous sequence of bytes from one input.
3540// Set Base to the selector for the first byte if so.
3541static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start,
3542 unsigned BytesPerElement, int &Base) {
3543 Base = -1;
3544 for (unsigned I = 0; I < BytesPerElement; ++I) {
3545 if (Bytes[Start + I] >= 0) {
3546 unsigned Elem = Bytes[Start + I];
3547 if (Base < 0) {
3548 Base = Elem - I;
3549 // Make sure the bytes would come from one input operand.
3550 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size())
3551 return false;
3552 } else if (unsigned(Base) != Elem - I)
3553 return false;
3554 }
3555 }
3556 return true;
3557}
3558
3559// Bytes is a VPERM-like permute vector, except that -1 is used for
3560// undefined bytes. Return true if it can be performed using VSLDI.
3561// When returning true, set StartIndex to the shift amount and OpNo0
3562// and OpNo1 to the VPERM operands that should be used as the first
3563// and second shift operand respectively.
3564static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes,
3565 unsigned &StartIndex, unsigned &OpNo0,
3566 unsigned &OpNo1) {
3567 int OpNos[] = { -1, -1 };
3568 int Shift = -1;
3569 for (unsigned I = 0; I < 16; ++I) {
3570 int Index = Bytes[I];
3571 if (Index >= 0) {
3572 int ExpectedShift = (Index - I) % SystemZ::VectorBytes;
3573 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes;
3574 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes;
3575 if (Shift < 0)
3576 Shift = ExpectedShift;
3577 else if (Shift != ExpectedShift)
3578 return false;
3579 // Make sure that the operand mappings are consistent with previous
3580 // elements.
3581 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3582 return false;
3583 OpNos[ModelOpNo] = RealOpNo;
3584 }
3585 }
3586 StartIndex = Shift;
3587 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3588}
3589
3590// Create a node that performs P on operands Op0 and Op1, casting the
3591// operands to the appropriate type. The type of the result is determined by P.
3592static SDValue getPermuteNode(SelectionDAG &DAG, SDLoc DL,
3593 const Permute &P, SDValue Op0, SDValue Op1) {
3594 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input
3595 // elements of a PACK are twice as wide as the outputs.
3596 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 :
3597 P.Opcode == SystemZISD::PACK ? P.Operand * 2 :
3598 P.Operand);
3599 // Cast both operands to the appropriate type.
3600 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8),
3601 SystemZ::VectorBytes / InBytes);
3602 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0);
3603 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1);
3604 SDValue Op;
3605 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) {
3606 SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32);
3607 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2);
3608 } else if (P.Opcode == SystemZISD::PACK) {
3609 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8),
3610 SystemZ::VectorBytes / P.Operand);
3611 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1);
3612 } else {
3613 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1);
3614 }
3615 return Op;
3616}
3617
3618// Bytes is a VPERM-like permute vector, except that -1 is used for
3619// undefined bytes. Implement it on operands Ops[0] and Ops[1] using
3620// VSLDI or VPERM.
3621static SDValue getGeneralPermuteNode(SelectionDAG &DAG, SDLoc DL, SDValue *Ops,
3622 const SmallVectorImpl<int> &Bytes) {
3623 for (unsigned I = 0; I < 2; ++I)
3624 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]);
3625
3626 // First see whether VSLDI can be used.
3627 unsigned StartIndex, OpNo0, OpNo1;
3628 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1))
3629 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0],
3630 Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32));
3631
3632 // Fall back on VPERM. Construct an SDNode for the permute vector.
3633 SDValue IndexNodes[SystemZ::VectorBytes];
3634 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3635 if (Bytes[I] >= 0)
3636 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32);
3637 else
3638 IndexNodes[I] = DAG.getUNDEF(MVT::i32);
3639 SDValue Op2 = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v16i8, IndexNodes);
3640 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2);
3641}
3642
3643namespace {
3644// Describes a general N-operand vector shuffle.
3645struct GeneralShuffle {
3646 GeneralShuffle(EVT vt) : VT(vt) {}
3647 void addUndef();
3648 void add(SDValue, unsigned);
3649 SDValue getNode(SelectionDAG &, SDLoc);
3650
3651 // The operands of the shuffle.
3652 SmallVector<SDValue, SystemZ::VectorBytes> Ops;
3653
3654 // Index I is -1 if byte I of the result is undefined. Otherwise the
3655 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand
3656 // Bytes[I] / SystemZ::VectorBytes.
3657 SmallVector<int, SystemZ::VectorBytes> Bytes;
3658
3659 // The type of the shuffle result.
3660 EVT VT;
3661};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003662}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003663
3664// Add an extra undefined element to the shuffle.
3665void GeneralShuffle::addUndef() {
3666 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3667 for (unsigned I = 0; I < BytesPerElement; ++I)
3668 Bytes.push_back(-1);
3669}
3670
3671// Add an extra element to the shuffle, taking it from element Elem of Op.
3672// A null Op indicates a vector input whose value will be calculated later;
3673// there is at most one such input per shuffle and it always has the same
3674// type as the result.
3675void GeneralShuffle::add(SDValue Op, unsigned Elem) {
3676 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3677
3678 // The source vector can have wider elements than the result,
3679 // either through an explicit TRUNCATE or because of type legalization.
3680 // We want the least significant part.
3681 EVT FromVT = Op.getNode() ? Op.getValueType() : VT;
3682 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize();
3683 assert(FromBytesPerElement >= BytesPerElement &&
3684 "Invalid EXTRACT_VECTOR_ELT");
3685 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes +
3686 (FromBytesPerElement - BytesPerElement));
3687
3688 // Look through things like shuffles and bitcasts.
3689 while (Op.getNode()) {
3690 if (Op.getOpcode() == ISD::BITCAST)
3691 Op = Op.getOperand(0);
3692 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) {
3693 // See whether the bytes we need come from a contiguous part of one
3694 // operand.
3695 SmallVector<int, SystemZ::VectorBytes> OpBytes;
3696 getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes);
3697 int NewByte;
3698 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte))
3699 break;
3700 if (NewByte < 0) {
3701 addUndef();
3702 return;
3703 }
3704 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes);
3705 Byte = unsigned(NewByte) % SystemZ::VectorBytes;
Sanjay Patel57195842016-03-14 17:28:46 +00003706 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003707 addUndef();
3708 return;
3709 } else
3710 break;
3711 }
3712
3713 // Make sure that the source of the extraction is in Ops.
3714 unsigned OpNo = 0;
3715 for (; OpNo < Ops.size(); ++OpNo)
3716 if (Ops[OpNo] == Op)
3717 break;
3718 if (OpNo == Ops.size())
3719 Ops.push_back(Op);
3720
3721 // Add the element to Bytes.
3722 unsigned Base = OpNo * SystemZ::VectorBytes + Byte;
3723 for (unsigned I = 0; I < BytesPerElement; ++I)
3724 Bytes.push_back(Base + I);
3725}
3726
3727// Return SDNodes for the completed shuffle.
3728SDValue GeneralShuffle::getNode(SelectionDAG &DAG, SDLoc DL) {
3729 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector");
3730
3731 if (Ops.size() == 0)
3732 return DAG.getUNDEF(VT);
3733
3734 // Make sure that there are at least two shuffle operands.
3735 if (Ops.size() == 1)
3736 Ops.push_back(DAG.getUNDEF(MVT::v16i8));
3737
3738 // Create a tree of shuffles, deferring root node until after the loop.
3739 // Try to redistribute the undefined elements of non-root nodes so that
3740 // the non-root shuffles match something like a pack or merge, then adjust
3741 // the parent node's permute vector to compensate for the new order.
3742 // Among other things, this copes with vectors like <2 x i16> that were
3743 // padded with undefined elements during type legalization.
3744 //
3745 // In the best case this redistribution will lead to the whole tree
3746 // using packs and merges. It should rarely be a loss in other cases.
3747 unsigned Stride = 1;
3748 for (; Stride * 2 < Ops.size(); Stride *= 2) {
3749 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) {
3750 SDValue SubOps[] = { Ops[I], Ops[I + Stride] };
3751
3752 // Create a mask for just these two operands.
3753 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes);
3754 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3755 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes;
3756 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes;
3757 if (OpNo == I)
3758 NewBytes[J] = Byte;
3759 else if (OpNo == I + Stride)
3760 NewBytes[J] = SystemZ::VectorBytes + Byte;
3761 else
3762 NewBytes[J] = -1;
3763 }
3764 // See if it would be better to reorganize NewMask to avoid using VPERM.
3765 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes);
3766 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) {
3767 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]);
3768 // Applying NewBytesMap to Ops[I] gets back to NewBytes.
3769 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3770 if (NewBytes[J] >= 0) {
3771 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes &&
3772 "Invalid double permute");
3773 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J];
3774 } else
3775 assert(NewBytesMap[J] < 0 && "Invalid double permute");
3776 }
3777 } else {
3778 // Just use NewBytes on the operands.
3779 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes);
3780 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J)
3781 if (NewBytes[J] >= 0)
3782 Bytes[J] = I * SystemZ::VectorBytes + J;
3783 }
3784 }
3785 }
3786
3787 // Now we just have 2 inputs. Put the second operand in Ops[1].
3788 if (Stride > 1) {
3789 Ops[1] = Ops[Stride];
3790 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3791 if (Bytes[I] >= int(SystemZ::VectorBytes))
3792 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes;
3793 }
3794
3795 // Look for an instruction that can do the permute without resorting
3796 // to VPERM.
3797 unsigned OpNo0, OpNo1;
3798 SDValue Op;
3799 if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1))
3800 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]);
3801 else
3802 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes);
3803 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3804}
3805
Ulrich Weigandcd808232015-05-05 19:26:48 +00003806// Return true if the given BUILD_VECTOR is a scalar-to-vector conversion.
3807static bool isScalarToVector(SDValue Op) {
3808 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00003809 if (!Op.getOperand(I).isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003810 return false;
3811 return true;
3812}
3813
3814// Return a vector of type VT that contains Value in the first element.
3815// The other elements don't matter.
3816static SDValue buildScalarToVector(SelectionDAG &DAG, SDLoc DL, EVT VT,
3817 SDValue Value) {
3818 // If we have a constant, replicate it to all elements and let the
3819 // BUILD_VECTOR lowering take care of it.
3820 if (Value.getOpcode() == ISD::Constant ||
3821 Value.getOpcode() == ISD::ConstantFP) {
3822 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value);
3823 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
3824 }
Sanjay Patel57195842016-03-14 17:28:46 +00003825 if (Value.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003826 return DAG.getUNDEF(VT);
3827 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
3828}
3829
3830// Return a vector of type VT in which Op0 is in element 0 and Op1 is in
3831// element 1. Used for cases in which replication is cheap.
3832static SDValue buildMergeScalars(SelectionDAG &DAG, SDLoc DL, EVT VT,
3833 SDValue Op0, SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00003834 if (Op0.isUndef()) {
3835 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003836 return DAG.getUNDEF(VT);
3837 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1);
3838 }
Sanjay Patel57195842016-03-14 17:28:46 +00003839 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003840 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0);
3841 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT,
3842 buildScalarToVector(DAG, DL, VT, Op0),
3843 buildScalarToVector(DAG, DL, VT, Op1));
3844}
3845
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003846// Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64
3847// vector for them.
3848static SDValue joinDwords(SelectionDAG &DAG, SDLoc DL, SDValue Op0,
3849 SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00003850 if (Op0.isUndef() && Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003851 return DAG.getUNDEF(MVT::v2i64);
3852 // If one of the two inputs is undefined then replicate the other one,
3853 // in order to avoid using another register unnecessarily.
Sanjay Patel57195842016-03-14 17:28:46 +00003854 if (Op0.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003855 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
Sanjay Patel57195842016-03-14 17:28:46 +00003856 else if (Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003857 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
3858 else {
3859 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
3860 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
3861 }
3862 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1);
3863}
3864
3865// Try to represent constant BUILD_VECTOR node BVN using a
3866// SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask
3867// on success.
3868static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) {
3869 EVT ElemVT = BVN->getValueType(0).getVectorElementType();
3870 unsigned BytesPerElement = ElemVT.getStoreSize();
3871 for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) {
3872 SDValue Op = BVN->getOperand(I);
Sanjay Patel75068522016-03-14 18:09:43 +00003873 if (!Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003874 uint64_t Value;
3875 if (Op.getOpcode() == ISD::Constant)
3876 Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue();
3877 else if (Op.getOpcode() == ISD::ConstantFP)
3878 Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt()
3879 .getZExtValue());
3880 else
3881 return false;
3882 for (unsigned J = 0; J < BytesPerElement; ++J) {
3883 uint64_t Byte = (Value >> (J * 8)) & 0xff;
3884 if (Byte == 0xff)
Aaron Ballman2a3aa1f242015-05-11 12:45:53 +00003885 Mask |= 1ULL << ((E - I - 1) * BytesPerElement + J);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003886 else if (Byte != 0)
3887 return false;
3888 }
3889 }
3890 }
3891 return true;
3892}
3893
3894// Try to load a vector constant in which BitsPerElement-bit value Value
3895// is replicated to fill the vector. VT is the type of the resulting
3896// constant, which may have elements of a different size from BitsPerElement.
3897// Return the SDValue of the constant on success, otherwise return
3898// an empty value.
3899static SDValue tryBuildVectorReplicate(SelectionDAG &DAG,
3900 const SystemZInstrInfo *TII,
3901 SDLoc DL, EVT VT, uint64_t Value,
3902 unsigned BitsPerElement) {
3903 // Signed 16-bit values can be replicated using VREPI.
3904 int64_t SignedValue = SignExtend64(Value, BitsPerElement);
3905 if (isInt<16>(SignedValue)) {
3906 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
3907 SystemZ::VectorBits / BitsPerElement);
3908 SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT,
3909 DAG.getConstant(SignedValue, DL, MVT::i32));
3910 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3911 }
3912 // See whether rotating the constant left some N places gives a value that
3913 // is one less than a power of 2 (i.e. all zeros followed by all ones).
3914 // If so we can use VGM.
3915 unsigned Start, End;
3916 if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) {
3917 // isRxSBGMask returns the bit numbers for a full 64-bit value,
3918 // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to
3919 // bit numbers for an BitsPerElement value, so that 0 denotes
3920 // 1 << (BitsPerElement-1).
3921 Start -= 64 - BitsPerElement;
3922 End -= 64 - BitsPerElement;
3923 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
3924 SystemZ::VectorBits / BitsPerElement);
3925 SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT,
3926 DAG.getConstant(Start, DL, MVT::i32),
3927 DAG.getConstant(End, DL, MVT::i32));
3928 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3929 }
3930 return SDValue();
3931}
3932
3933// If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually
3934// better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for
3935// the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR
3936// would benefit from this representation and return it if so.
3937static SDValue tryBuildVectorShuffle(SelectionDAG &DAG,
3938 BuildVectorSDNode *BVN) {
3939 EVT VT = BVN->getValueType(0);
3940 unsigned NumElements = VT.getVectorNumElements();
3941
3942 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation
3943 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still
3944 // need a BUILD_VECTOR, add an additional placeholder operand for that
3945 // BUILD_VECTOR and store its operands in ResidueOps.
3946 GeneralShuffle GS(VT);
3947 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps;
3948 bool FoundOne = false;
3949 for (unsigned I = 0; I < NumElements; ++I) {
3950 SDValue Op = BVN->getOperand(I);
3951 if (Op.getOpcode() == ISD::TRUNCATE)
3952 Op = Op.getOperand(0);
3953 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
3954 Op.getOperand(1).getOpcode() == ISD::Constant) {
3955 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
3956 GS.add(Op.getOperand(0), Elem);
3957 FoundOne = true;
Sanjay Patel57195842016-03-14 17:28:46 +00003958 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003959 GS.addUndef();
3960 } else {
3961 GS.add(SDValue(), ResidueOps.size());
Ulrich Weigande861e642015-09-15 14:27:46 +00003962 ResidueOps.push_back(BVN->getOperand(I));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003963 }
3964 }
3965
3966 // Nothing to do if there are no EXTRACT_VECTOR_ELTs.
3967 if (!FoundOne)
3968 return SDValue();
3969
3970 // Create the BUILD_VECTOR for the remaining elements, if any.
3971 if (!ResidueOps.empty()) {
3972 while (ResidueOps.size() < NumElements)
Ulrich Weigandf4d14f72015-10-08 17:46:59 +00003973 ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType()));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003974 for (auto &Op : GS.Ops) {
3975 if (!Op.getNode()) {
3976 Op = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BVN), VT, ResidueOps);
3977 break;
3978 }
3979 }
3980 }
3981 return GS.getNode(DAG, SDLoc(BVN));
3982}
3983
3984// Combine GPR scalar values Elems into a vector of type VT.
3985static SDValue buildVector(SelectionDAG &DAG, SDLoc DL, EVT VT,
3986 SmallVectorImpl<SDValue> &Elems) {
3987 // See whether there is a single replicated value.
3988 SDValue Single;
3989 unsigned int NumElements = Elems.size();
3990 unsigned int Count = 0;
3991 for (auto Elem : Elems) {
Sanjay Patel75068522016-03-14 18:09:43 +00003992 if (!Elem.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003993 if (!Single.getNode())
3994 Single = Elem;
3995 else if (Elem != Single) {
3996 Single = SDValue();
3997 break;
3998 }
3999 Count += 1;
4000 }
4001 }
4002 // There are three cases here:
4003 //
4004 // - if the only defined element is a loaded one, the best sequence
4005 // is a replicating load.
4006 //
4007 // - otherwise, if the only defined element is an i64 value, we will
4008 // end up with the same VLVGP sequence regardless of whether we short-cut
4009 // for replication or fall through to the later code.
4010 //
4011 // - otherwise, if the only defined element is an i32 or smaller value,
4012 // we would need 2 instructions to replicate it: VLVGP followed by VREPx.
4013 // This is only a win if the single defined element is used more than once.
4014 // In other cases we're better off using a single VLVGx.
4015 if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD))
4016 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single);
4017
4018 // The best way of building a v2i64 from two i64s is to use VLVGP.
4019 if (VT == MVT::v2i64)
4020 return joinDwords(DAG, DL, Elems[0], Elems[1]);
4021
Ulrich Weigandcd808232015-05-05 19:26:48 +00004022 // Use a 64-bit merge high to combine two doubles.
4023 if (VT == MVT::v2f64)
4024 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4025
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004026 // Build v4f32 values directly from the FPRs:
4027 //
4028 // <Axxx> <Bxxx> <Cxxxx> <Dxxx>
4029 // V V VMRHF
4030 // <ABxx> <CDxx>
4031 // V VMRHG
4032 // <ABCD>
4033 if (VT == MVT::v4f32) {
4034 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4035 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]);
4036 // Avoid unnecessary undefs by reusing the other operand.
Sanjay Patel57195842016-03-14 17:28:46 +00004037 if (Op01.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004038 Op01 = Op23;
Sanjay Patel57195842016-03-14 17:28:46 +00004039 else if (Op23.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004040 Op23 = Op01;
4041 // Merging identical replications is a no-op.
4042 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23)
4043 return Op01;
4044 Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01);
4045 Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23);
4046 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH,
4047 DL, MVT::v2i64, Op01, Op23);
4048 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4049 }
4050
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004051 // Collect the constant terms.
4052 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue());
4053 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false);
4054
4055 unsigned NumConstants = 0;
4056 for (unsigned I = 0; I < NumElements; ++I) {
4057 SDValue Elem = Elems[I];
4058 if (Elem.getOpcode() == ISD::Constant ||
4059 Elem.getOpcode() == ISD::ConstantFP) {
4060 NumConstants += 1;
4061 Constants[I] = Elem;
4062 Done[I] = true;
4063 }
4064 }
4065 // If there was at least one constant, fill in the other elements of
4066 // Constants with undefs to get a full vector constant and use that
4067 // as the starting point.
4068 SDValue Result;
4069 if (NumConstants > 0) {
4070 for (unsigned I = 0; I < NumElements; ++I)
4071 if (!Constants[I].getNode())
4072 Constants[I] = DAG.getUNDEF(Elems[I].getValueType());
4073 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Constants);
4074 } else {
4075 // Otherwise try to use VLVGP to start the sequence in order to
4076 // avoid a false dependency on any previous contents of the vector
4077 // register. This only makes sense if one of the associated elements
4078 // is defined.
4079 unsigned I1 = NumElements / 2 - 1;
4080 unsigned I2 = NumElements - 1;
Sanjay Patel75068522016-03-14 18:09:43 +00004081 bool Def1 = !Elems[I1].isUndef();
4082 bool Def2 = !Elems[I2].isUndef();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004083 if (Def1 || Def2) {
4084 SDValue Elem1 = Elems[Def1 ? I1 : I2];
4085 SDValue Elem2 = Elems[Def2 ? I2 : I1];
4086 Result = DAG.getNode(ISD::BITCAST, DL, VT,
4087 joinDwords(DAG, DL, Elem1, Elem2));
4088 Done[I1] = true;
4089 Done[I2] = true;
4090 } else
4091 Result = DAG.getUNDEF(VT);
4092 }
4093
4094 // Use VLVGx to insert the other elements.
4095 for (unsigned I = 0; I < NumElements; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00004096 if (!Done[I] && !Elems[I].isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004097 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I],
4098 DAG.getConstant(I, DL, MVT::i32));
4099 return Result;
4100}
4101
4102SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op,
4103 SelectionDAG &DAG) const {
4104 const SystemZInstrInfo *TII =
4105 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
4106 auto *BVN = cast<BuildVectorSDNode>(Op.getNode());
4107 SDLoc DL(Op);
4108 EVT VT = Op.getValueType();
4109
4110 if (BVN->isConstant()) {
4111 // Try using VECTOR GENERATE BYTE MASK. This is the architecturally-
4112 // preferred way of creating all-zero and all-one vectors so give it
4113 // priority over other methods below.
4114 uint64_t Mask = 0;
4115 if (tryBuildVectorByteMask(BVN, Mask)) {
4116 SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
4117 DAG.getConstant(Mask, DL, MVT::i32));
4118 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4119 }
4120
4121 // Try using some form of replication.
4122 APInt SplatBits, SplatUndef;
4123 unsigned SplatBitSize;
4124 bool HasAnyUndefs;
4125 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4126 8, true) &&
4127 SplatBitSize <= 64) {
4128 // First try assuming that any undefined bits above the highest set bit
4129 // and below the lowest set bit are 1s. This increases the likelihood of
4130 // being able to use a sign-extended element value in VECTOR REPLICATE
4131 // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK.
4132 uint64_t SplatBitsZ = SplatBits.getZExtValue();
4133 uint64_t SplatUndefZ = SplatUndef.getZExtValue();
4134 uint64_t Lower = (SplatUndefZ
4135 & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1));
4136 uint64_t Upper = (SplatUndefZ
4137 & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1));
4138 uint64_t Value = SplatBitsZ | Upper | Lower;
4139 SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value,
4140 SplatBitSize);
4141 if (Op.getNode())
4142 return Op;
4143
4144 // Now try assuming that any undefined bits between the first and
4145 // last defined set bits are set. This increases the chances of
4146 // using a non-wraparound mask.
4147 uint64_t Middle = SplatUndefZ & ~Upper & ~Lower;
4148 Value = SplatBitsZ | Middle;
4149 Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize);
4150 if (Op.getNode())
4151 return Op;
4152 }
4153
4154 // Fall back to loading it from memory.
4155 return SDValue();
4156 }
4157
4158 // See if we should use shuffles to construct the vector from other vectors.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004159 if (SDValue Res = tryBuildVectorShuffle(DAG, BVN))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004160 return Res;
4161
Ulrich Weigandcd808232015-05-05 19:26:48 +00004162 // Detect SCALAR_TO_VECTOR conversions.
4163 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op))
4164 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0));
4165
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004166 // Otherwise use buildVector to build the vector up from GPRs.
4167 unsigned NumElements = Op.getNumOperands();
4168 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements);
4169 for (unsigned I = 0; I < NumElements; ++I)
4170 Ops[I] = Op.getOperand(I);
4171 return buildVector(DAG, DL, VT, Ops);
4172}
4173
4174SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
4175 SelectionDAG &DAG) const {
4176 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode());
4177 SDLoc DL(Op);
4178 EVT VT = Op.getValueType();
4179 unsigned NumElements = VT.getVectorNumElements();
4180
4181 if (VSN->isSplat()) {
4182 SDValue Op0 = Op.getOperand(0);
4183 unsigned Index = VSN->getSplatIndex();
4184 assert(Index < VT.getVectorNumElements() &&
4185 "Splat index should be defined and in first operand");
4186 // See whether the value we're splatting is directly available as a scalar.
4187 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4188 Op0.getOpcode() == ISD::BUILD_VECTOR)
4189 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index));
4190 // Otherwise keep it as a vector-to-vector operation.
4191 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0),
4192 DAG.getConstant(Index, DL, MVT::i32));
4193 }
4194
4195 GeneralShuffle GS(VT);
4196 for (unsigned I = 0; I < NumElements; ++I) {
4197 int Elt = VSN->getMaskElt(I);
4198 if (Elt < 0)
4199 GS.addUndef();
4200 else
4201 GS.add(Op.getOperand(unsigned(Elt) / NumElements),
4202 unsigned(Elt) % NumElements);
4203 }
4204 return GS.getNode(DAG, SDLoc(VSN));
4205}
4206
4207SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
4208 SelectionDAG &DAG) const {
4209 SDLoc DL(Op);
4210 // Just insert the scalar into element 0 of an undefined vector.
4211 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL,
4212 Op.getValueType(), DAG.getUNDEF(Op.getValueType()),
4213 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32));
4214}
4215
Ulrich Weigandcd808232015-05-05 19:26:48 +00004216SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
4217 SelectionDAG &DAG) const {
4218 // Handle insertions of floating-point values.
4219 SDLoc DL(Op);
4220 SDValue Op0 = Op.getOperand(0);
4221 SDValue Op1 = Op.getOperand(1);
4222 SDValue Op2 = Op.getOperand(2);
4223 EVT VT = Op.getValueType();
4224
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004225 // Insertions into constant indices of a v2f64 can be done using VPDI.
4226 // However, if the inserted value is a bitcast or a constant then it's
4227 // better to use GPRs, as below.
4228 if (VT == MVT::v2f64 &&
4229 Op1.getOpcode() != ISD::BITCAST &&
Ulrich Weigandcd808232015-05-05 19:26:48 +00004230 Op1.getOpcode() != ISD::ConstantFP &&
4231 Op2.getOpcode() == ISD::Constant) {
4232 uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue();
4233 unsigned Mask = VT.getVectorNumElements() - 1;
4234 if (Index <= Mask)
4235 return Op;
4236 }
4237
4238 // Otherwise bitcast to the equivalent integer form and insert via a GPR.
4239 MVT IntVT = MVT::getIntegerVT(VT.getVectorElementType().getSizeInBits());
4240 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements());
4241 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT,
4242 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0),
4243 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2);
4244 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4245}
4246
4247SDValue
4248SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
4249 SelectionDAG &DAG) const {
4250 // Handle extractions of floating-point values.
4251 SDLoc DL(Op);
4252 SDValue Op0 = Op.getOperand(0);
4253 SDValue Op1 = Op.getOperand(1);
4254 EVT VT = Op.getValueType();
4255 EVT VecVT = Op0.getValueType();
4256
4257 // Extractions of constant indices can be done directly.
4258 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) {
4259 uint64_t Index = CIndexN->getZExtValue();
4260 unsigned Mask = VecVT.getVectorNumElements() - 1;
4261 if (Index <= Mask)
4262 return Op;
4263 }
4264
4265 // Otherwise bitcast to the equivalent integer form and extract via a GPR.
4266 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
4267 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements());
4268 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT,
4269 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1);
4270 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4271}
4272
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004273SDValue
4274SystemZTargetLowering::lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004275 unsigned UnpackHigh) const {
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004276 SDValue PackedOp = Op.getOperand(0);
4277 EVT OutVT = Op.getValueType();
4278 EVT InVT = PackedOp.getValueType();
4279 unsigned ToBits = OutVT.getVectorElementType().getSizeInBits();
4280 unsigned FromBits = InVT.getVectorElementType().getSizeInBits();
4281 do {
4282 FromBits *= 2;
4283 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits),
4284 SystemZ::VectorBits / FromBits);
4285 PackedOp = DAG.getNode(UnpackHigh, SDLoc(PackedOp), OutVT, PackedOp);
4286 } while (FromBits != ToBits);
4287 return PackedOp;
4288}
4289
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004290SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG,
4291 unsigned ByScalar) const {
4292 // Look for cases where a vector shift can use the *_BY_SCALAR form.
4293 SDValue Op0 = Op.getOperand(0);
4294 SDValue Op1 = Op.getOperand(1);
4295 SDLoc DL(Op);
4296 EVT VT = Op.getValueType();
4297 unsigned ElemBitSize = VT.getVectorElementType().getSizeInBits();
4298
4299 // See whether the shift vector is a splat represented as BUILD_VECTOR.
4300 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) {
4301 APInt SplatBits, SplatUndef;
4302 unsigned SplatBitSize;
4303 bool HasAnyUndefs;
4304 // Check for constant splats. Use ElemBitSize as the minimum element
4305 // width and reject splats that need wider elements.
4306 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4307 ElemBitSize, true) &&
4308 SplatBitSize == ElemBitSize) {
4309 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff,
4310 DL, MVT::i32);
4311 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4312 }
4313 // Check for variable splats.
4314 BitVector UndefElements;
4315 SDValue Splat = BVN->getSplatValue(&UndefElements);
4316 if (Splat) {
4317 // Since i32 is the smallest legal type, we either need a no-op
4318 // or a truncation.
4319 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat);
4320 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4321 }
4322 }
4323
4324 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR,
4325 // and the shift amount is directly available in a GPR.
4326 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) {
4327 if (VSN->isSplat()) {
4328 SDValue VSNOp0 = VSN->getOperand(0);
4329 unsigned Index = VSN->getSplatIndex();
4330 assert(Index < VT.getVectorNumElements() &&
4331 "Splat index should be defined and in first operand");
4332 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4333 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) {
4334 // Since i32 is the smallest legal type, we either need a no-op
4335 // or a truncation.
4336 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32,
4337 VSNOp0.getOperand(Index));
4338 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4339 }
4340 }
4341 }
4342
4343 // Otherwise just treat the current form as legal.
4344 return Op;
4345}
4346
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004347SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
4348 SelectionDAG &DAG) const {
4349 switch (Op.getOpcode()) {
4350 case ISD::BR_CC:
4351 return lowerBR_CC(Op, DAG);
4352 case ISD::SELECT_CC:
4353 return lowerSELECT_CC(Op, DAG);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00004354 case ISD::SETCC:
4355 return lowerSETCC(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004356 case ISD::GlobalAddress:
4357 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
4358 case ISD::GlobalTLSAddress:
4359 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
4360 case ISD::BlockAddress:
4361 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
4362 case ISD::JumpTable:
4363 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
4364 case ISD::ConstantPool:
4365 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
4366 case ISD::BITCAST:
4367 return lowerBITCAST(Op, DAG);
4368 case ISD::VASTART:
4369 return lowerVASTART(Op, DAG);
4370 case ISD::VACOPY:
4371 return lowerVACOPY(Op, DAG);
4372 case ISD::DYNAMIC_STACKALLOC:
4373 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00004374 case ISD::SMUL_LOHI:
4375 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004376 case ISD::UMUL_LOHI:
4377 return lowerUMUL_LOHI(Op, DAG);
4378 case ISD::SDIVREM:
4379 return lowerSDIVREM(Op, DAG);
4380 case ISD::UDIVREM:
4381 return lowerUDIVREM(Op, DAG);
4382 case ISD::OR:
4383 return lowerOR(Op, DAG);
Ulrich Weigandb4012182015-03-31 12:56:33 +00004384 case ISD::CTPOP:
4385 return lowerCTPOP(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004386 case ISD::CTLZ_ZERO_UNDEF:
4387 return DAG.getNode(ISD::CTLZ, SDLoc(Op),
4388 Op.getValueType(), Op.getOperand(0));
4389 case ISD::CTTZ_ZERO_UNDEF:
4390 return DAG.getNode(ISD::CTTZ, SDLoc(Op),
4391 Op.getValueType(), Op.getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004392 case ISD::ATOMIC_SWAP:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004393 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
4394 case ISD::ATOMIC_STORE:
4395 return lowerATOMIC_STORE(Op, DAG);
4396 case ISD::ATOMIC_LOAD:
4397 return lowerATOMIC_LOAD(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004398 case ISD::ATOMIC_LOAD_ADD:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004399 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004400 case ISD::ATOMIC_LOAD_SUB:
Richard Sandiford41350a52013-12-24 15:18:04 +00004401 return lowerATOMIC_LOAD_SUB(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004402 case ISD::ATOMIC_LOAD_AND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004403 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004404 case ISD::ATOMIC_LOAD_OR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004405 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004406 case ISD::ATOMIC_LOAD_XOR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004407 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004408 case ISD::ATOMIC_LOAD_NAND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004409 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004410 case ISD::ATOMIC_LOAD_MIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004411 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004412 case ISD::ATOMIC_LOAD_MAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004413 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004414 case ISD::ATOMIC_LOAD_UMIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004415 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004416 case ISD::ATOMIC_LOAD_UMAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004417 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004418 case ISD::ATOMIC_CMP_SWAP:
4419 return lowerATOMIC_CMP_SWAP(Op, DAG);
4420 case ISD::STACKSAVE:
4421 return lowerSTACKSAVE(Op, DAG);
4422 case ISD::STACKRESTORE:
4423 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00004424 case ISD::PREFETCH:
4425 return lowerPREFETCH(Op, DAG);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004426 case ISD::INTRINSIC_W_CHAIN:
4427 return lowerINTRINSIC_W_CHAIN(Op, DAG);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004428 case ISD::INTRINSIC_WO_CHAIN:
4429 return lowerINTRINSIC_WO_CHAIN(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004430 case ISD::BUILD_VECTOR:
4431 return lowerBUILD_VECTOR(Op, DAG);
4432 case ISD::VECTOR_SHUFFLE:
4433 return lowerVECTOR_SHUFFLE(Op, DAG);
4434 case ISD::SCALAR_TO_VECTOR:
4435 return lowerSCALAR_TO_VECTOR(Op, DAG);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004436 case ISD::INSERT_VECTOR_ELT:
4437 return lowerINSERT_VECTOR_ELT(Op, DAG);
4438 case ISD::EXTRACT_VECTOR_ELT:
4439 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004440 case ISD::SIGN_EXTEND_VECTOR_INREG:
4441 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACK_HIGH);
4442 case ISD::ZERO_EXTEND_VECTOR_INREG:
4443 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACKL_HIGH);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004444 case ISD::SHL:
4445 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR);
4446 case ISD::SRL:
4447 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR);
4448 case ISD::SRA:
4449 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004450 default:
4451 llvm_unreachable("Unexpected node to lower");
4452 }
4453}
4454
4455const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
4456#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
Matthias Braund04893f2015-05-07 21:33:59 +00004457 switch ((SystemZISD::NodeType)Opcode) {
4458 case SystemZISD::FIRST_NUMBER: break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004459 OPCODE(RET_FLAG);
4460 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00004461 OPCODE(SIBCALL);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004462 OPCODE(TLS_GDCALL);
4463 OPCODE(TLS_LDCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004464 OPCODE(PCREL_WRAPPER);
Richard Sandiford54b36912013-09-27 15:14:04 +00004465 OPCODE(PCREL_OFFSET);
Richard Sandiford57485472013-12-13 15:35:00 +00004466 OPCODE(IABS);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00004467 OPCODE(ICMP);
4468 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00004469 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004470 OPCODE(BR_CCMASK);
4471 OPCODE(SELECT_CCMASK);
4472 OPCODE(ADJDYNALLOC);
4473 OPCODE(EXTRACT_ACCESS);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004474 OPCODE(POPCNT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004475 OPCODE(UMUL_LOHI64);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004476 OPCODE(SDIVREM32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004477 OPCODE(SDIVREM64);
4478 OPCODE(UDIVREM32);
4479 OPCODE(UDIVREM64);
Richard Sandifordd131ff82013-07-08 09:35:23 +00004480 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004481 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00004482 OPCODE(NC);
4483 OPCODE(NC_LOOP);
4484 OPCODE(OC);
4485 OPCODE(OC_LOOP);
4486 OPCODE(XC);
4487 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00004488 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004489 OPCODE(CLC_LOOP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00004490 OPCODE(STPCPY);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004491 OPCODE(STRCMP);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00004492 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00004493 OPCODE(IPM);
Richard Sandiford9afe6132013-12-10 10:36:34 +00004494 OPCODE(SERIALIZE);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004495 OPCODE(TBEGIN);
4496 OPCODE(TBEGIN_NOFLOAT);
4497 OPCODE(TEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004498 OPCODE(BYTE_MASK);
4499 OPCODE(ROTATE_MASK);
4500 OPCODE(REPLICATE);
4501 OPCODE(JOIN_DWORDS);
4502 OPCODE(SPLAT);
4503 OPCODE(MERGE_HIGH);
4504 OPCODE(MERGE_LOW);
4505 OPCODE(SHL_DOUBLE);
4506 OPCODE(PERMUTE_DWORDS);
4507 OPCODE(PERMUTE);
4508 OPCODE(PACK);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004509 OPCODE(PACKS_CC);
4510 OPCODE(PACKLS_CC);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004511 OPCODE(UNPACK_HIGH);
4512 OPCODE(UNPACKL_HIGH);
4513 OPCODE(UNPACK_LOW);
4514 OPCODE(UNPACKL_LOW);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004515 OPCODE(VSHL_BY_SCALAR);
4516 OPCODE(VSRL_BY_SCALAR);
4517 OPCODE(VSRA_BY_SCALAR);
4518 OPCODE(VSUM);
4519 OPCODE(VICMPE);
4520 OPCODE(VICMPH);
4521 OPCODE(VICMPHL);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004522 OPCODE(VICMPES);
4523 OPCODE(VICMPHS);
4524 OPCODE(VICMPHLS);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004525 OPCODE(VFCMPE);
4526 OPCODE(VFCMPH);
4527 OPCODE(VFCMPHE);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004528 OPCODE(VFCMPES);
4529 OPCODE(VFCMPHS);
4530 OPCODE(VFCMPHES);
4531 OPCODE(VFTCI);
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004532 OPCODE(VEXTEND);
4533 OPCODE(VROUND);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004534 OPCODE(VTM);
4535 OPCODE(VFAE_CC);
4536 OPCODE(VFAEZ_CC);
4537 OPCODE(VFEE_CC);
4538 OPCODE(VFEEZ_CC);
4539 OPCODE(VFENE_CC);
4540 OPCODE(VFENEZ_CC);
4541 OPCODE(VISTR_CC);
4542 OPCODE(VSTRC_CC);
4543 OPCODE(VSTRCZ_CC);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004544 OPCODE(ATOMIC_SWAPW);
4545 OPCODE(ATOMIC_LOADW_ADD);
4546 OPCODE(ATOMIC_LOADW_SUB);
4547 OPCODE(ATOMIC_LOADW_AND);
4548 OPCODE(ATOMIC_LOADW_OR);
4549 OPCODE(ATOMIC_LOADW_XOR);
4550 OPCODE(ATOMIC_LOADW_NAND);
4551 OPCODE(ATOMIC_LOADW_MIN);
4552 OPCODE(ATOMIC_LOADW_MAX);
4553 OPCODE(ATOMIC_LOADW_UMIN);
4554 OPCODE(ATOMIC_LOADW_UMAX);
4555 OPCODE(ATOMIC_CMP_SWAPW);
Richard Sandiford03481332013-08-23 11:36:42 +00004556 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004557 }
Craig Topper062a2ba2014-04-25 05:30:21 +00004558 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004559#undef OPCODE
4560}
4561
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004562// Return true if VT is a vector whose elements are a whole number of bytes
4563// in width.
4564static bool canTreatAsByteVector(EVT VT) {
4565 return VT.isVector() && VT.getVectorElementType().getSizeInBits() % 8 == 0;
4566}
4567
4568// Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT
4569// producing a result of type ResVT. Op is a possibly bitcast version
4570// of the input vector and Index is the index (based on type VecVT) that
4571// should be extracted. Return the new extraction if a simplification
4572// was possible or if Force is true.
4573SDValue SystemZTargetLowering::combineExtract(SDLoc DL, EVT ResVT, EVT VecVT,
4574 SDValue Op, unsigned Index,
4575 DAGCombinerInfo &DCI,
4576 bool Force) const {
4577 SelectionDAG &DAG = DCI.DAG;
4578
4579 // The number of bytes being extracted.
4580 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4581
4582 for (;;) {
4583 unsigned Opcode = Op.getOpcode();
4584 if (Opcode == ISD::BITCAST)
4585 // Look through bitcasts.
4586 Op = Op.getOperand(0);
4587 else if (Opcode == ISD::VECTOR_SHUFFLE &&
4588 canTreatAsByteVector(Op.getValueType())) {
4589 // Get a VPERM-like permute mask and see whether the bytes covered
4590 // by the extracted element are a contiguous sequence from one
4591 // source operand.
4592 SmallVector<int, SystemZ::VectorBytes> Bytes;
4593 getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes);
4594 int First;
4595 if (!getShuffleInput(Bytes, Index * BytesPerElement,
4596 BytesPerElement, First))
4597 break;
4598 if (First < 0)
4599 return DAG.getUNDEF(ResVT);
4600 // Make sure the contiguous sequence starts at a multiple of the
4601 // original element size.
4602 unsigned Byte = unsigned(First) % Bytes.size();
4603 if (Byte % BytesPerElement != 0)
4604 break;
4605 // We can get the extracted value directly from an input.
4606 Index = Byte / BytesPerElement;
4607 Op = Op.getOperand(unsigned(First) / Bytes.size());
4608 Force = true;
4609 } else if (Opcode == ISD::BUILD_VECTOR &&
4610 canTreatAsByteVector(Op.getValueType())) {
4611 // We can only optimize this case if the BUILD_VECTOR elements are
4612 // at least as wide as the extracted value.
4613 EVT OpVT = Op.getValueType();
4614 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4615 if (OpBytesPerElement < BytesPerElement)
4616 break;
4617 // Make sure that the least-significant bit of the extracted value
4618 // is the least significant bit of an input.
4619 unsigned End = (Index + 1) * BytesPerElement;
4620 if (End % OpBytesPerElement != 0)
4621 break;
4622 // We're extracting the low part of one operand of the BUILD_VECTOR.
4623 Op = Op.getOperand(End / OpBytesPerElement - 1);
4624 if (!Op.getValueType().isInteger()) {
4625 EVT VT = MVT::getIntegerVT(Op.getValueType().getSizeInBits());
4626 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
4627 DCI.AddToWorklist(Op.getNode());
4628 }
4629 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits());
4630 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
4631 if (VT != ResVT) {
4632 DCI.AddToWorklist(Op.getNode());
4633 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op);
4634 }
4635 return Op;
4636 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004637 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG ||
4638 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) &&
4639 canTreatAsByteVector(Op.getValueType()) &&
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004640 canTreatAsByteVector(Op.getOperand(0).getValueType())) {
4641 // Make sure that only the unextended bits are significant.
4642 EVT ExtVT = Op.getValueType();
4643 EVT OpVT = Op.getOperand(0).getValueType();
4644 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize();
4645 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4646 unsigned Byte = Index * BytesPerElement;
4647 unsigned SubByte = Byte % ExtBytesPerElement;
4648 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement;
4649 if (SubByte < MinSubByte ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004650 SubByte + BytesPerElement > ExtBytesPerElement)
4651 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004652 // Get the byte offset of the unextended element
4653 Byte = Byte / ExtBytesPerElement * OpBytesPerElement;
4654 // ...then add the byte offset relative to that element.
4655 Byte += SubByte - MinSubByte;
4656 if (Byte % BytesPerElement != 0)
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004657 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004658 Op = Op.getOperand(0);
4659 Index = Byte / BytesPerElement;
4660 Force = true;
4661 } else
4662 break;
4663 }
4664 if (Force) {
4665 if (Op.getValueType() != VecVT) {
4666 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op);
4667 DCI.AddToWorklist(Op.getNode());
4668 }
4669 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op,
4670 DAG.getConstant(Index, DL, MVT::i32));
4671 }
4672 return SDValue();
4673}
4674
4675// Optimize vector operations in scalar value Op on the basis that Op
4676// is truncated to TruncVT.
4677SDValue
4678SystemZTargetLowering::combineTruncateExtract(SDLoc DL, EVT TruncVT, SDValue Op,
4679 DAGCombinerInfo &DCI) const {
4680 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into
4681 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements
4682 // of type TruncVT.
4683 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4684 TruncVT.getSizeInBits() % 8 == 0) {
4685 SDValue Vec = Op.getOperand(0);
4686 EVT VecVT = Vec.getValueType();
4687 if (canTreatAsByteVector(VecVT)) {
4688 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
4689 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4690 unsigned TruncBytes = TruncVT.getStoreSize();
4691 if (BytesPerElement % TruncBytes == 0) {
4692 // Calculate the value of Y' in the above description. We are
4693 // splitting the original elements into Scale equal-sized pieces
4694 // and for truncation purposes want the last (least-significant)
4695 // of these pieces for IndexN. This is easiest to do by calculating
4696 // the start index of the following element and then subtracting 1.
4697 unsigned Scale = BytesPerElement / TruncBytes;
4698 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1;
4699
4700 // Defer the creation of the bitcast from X to combineExtract,
4701 // which might be able to optimize the extraction.
4702 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8),
4703 VecVT.getStoreSize() / TruncBytes);
4704 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT);
4705 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true);
4706 }
4707 }
4708 }
4709 }
4710 return SDValue();
4711}
4712
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004713SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
4714 DAGCombinerInfo &DCI) const {
4715 SelectionDAG &DAG = DCI.DAG;
4716 unsigned Opcode = N->getOpcode();
4717 if (Opcode == ISD::SIGN_EXTEND) {
4718 // Convert (sext (ashr (shl X, C1), C2)) to
4719 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
4720 // cheap as narrower ones.
4721 SDValue N0 = N->getOperand(0);
4722 EVT VT = N->getValueType(0);
4723 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
4724 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
4725 SDValue Inner = N0.getOperand(0);
4726 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
4727 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
4728 unsigned Extra = (VT.getSizeInBits() -
4729 N0.getValueType().getSizeInBits());
4730 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
4731 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
4732 EVT ShiftVT = N0.getOperand(1).getValueType();
4733 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
4734 Inner.getOperand(0));
4735 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004736 DAG.getConstant(NewShlAmt, SDLoc(Inner),
4737 ShiftVT));
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004738 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004739 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT));
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004740 }
4741 }
4742 }
4743 }
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004744 if (Opcode == SystemZISD::MERGE_HIGH ||
4745 Opcode == SystemZISD::MERGE_LOW) {
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004746 SDValue Op0 = N->getOperand(0);
4747 SDValue Op1 = N->getOperand(1);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004748 if (Op0.getOpcode() == ISD::BITCAST)
4749 Op0 = Op0.getOperand(0);
4750 if (Op0.getOpcode() == SystemZISD::BYTE_MASK &&
4751 cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0) {
4752 // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF
4753 // for v4f32.
4754 if (Op1 == N->getOperand(0))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004755 return Op1;
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004756 // (z_merge_? 0, X) -> (z_unpackl_? 0, X).
4757 EVT VT = Op1.getValueType();
4758 unsigned ElemBytes = VT.getVectorElementType().getStoreSize();
4759 if (ElemBytes <= 4) {
4760 Opcode = (Opcode == SystemZISD::MERGE_HIGH ?
4761 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW);
4762 EVT InVT = VT.changeVectorElementTypeToInteger();
4763 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16),
4764 SystemZ::VectorBytes / ElemBytes / 2);
4765 if (VT != InVT) {
4766 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1);
4767 DCI.AddToWorklist(Op1.getNode());
4768 }
4769 SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1);
4770 DCI.AddToWorklist(Op.getNode());
4771 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
4772 }
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004773 }
4774 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004775 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better
4776 // for the extraction to be done on a vMiN value, so that we can use VSTE.
4777 // If X has wider elements then convert it to:
4778 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z).
4779 if (Opcode == ISD::STORE) {
4780 auto *SN = cast<StoreSDNode>(N);
4781 EVT MemVT = SN->getMemoryVT();
4782 if (MemVT.isInteger()) {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004783 if (SDValue Value =
4784 combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004785 DCI.AddToWorklist(Value.getNode());
4786
4787 // Rewrite the store with the new form of stored value.
4788 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value,
4789 SN->getBasePtr(), SN->getMemoryVT(),
4790 SN->getMemOperand());
4791 }
4792 }
4793 }
4794 // Try to simplify a vector extraction.
4795 if (Opcode == ISD::EXTRACT_VECTOR_ELT) {
4796 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
4797 SDValue Op0 = N->getOperand(0);
4798 EVT VecVT = Op0.getValueType();
4799 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0,
4800 IndexN->getZExtValue(), DCI, false);
4801 }
4802 }
4803 // (join_dwords X, X) == (replicate X)
4804 if (Opcode == SystemZISD::JOIN_DWORDS &&
4805 N->getOperand(0) == N->getOperand(1))
4806 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0),
4807 N->getOperand(0));
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004808 // (fround (extract_vector_elt X 0))
4809 // (fround (extract_vector_elt X 1)) ->
4810 // (extract_vector_elt (VROUND X) 0)
4811 // (extract_vector_elt (VROUND X) 1)
4812 //
4813 // This is a special case since the target doesn't really support v2f32s.
4814 if (Opcode == ISD::FP_ROUND) {
4815 SDValue Op0 = N->getOperand(0);
4816 if (N->getValueType(0) == MVT::f32 &&
4817 Op0.hasOneUse() &&
4818 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4819 Op0.getOperand(0).getValueType() == MVT::v2f64 &&
4820 Op0.getOperand(1).getOpcode() == ISD::Constant &&
4821 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) {
4822 SDValue Vec = Op0.getOperand(0);
4823 for (auto *U : Vec->uses()) {
4824 if (U != Op0.getNode() &&
4825 U->hasOneUse() &&
4826 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4827 U->getOperand(0) == Vec &&
4828 U->getOperand(1).getOpcode() == ISD::Constant &&
4829 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) {
4830 SDValue OtherRound = SDValue(*U->use_begin(), 0);
4831 if (OtherRound.getOpcode() == ISD::FP_ROUND &&
4832 OtherRound.getOperand(0) == SDValue(U, 0) &&
4833 OtherRound.getValueType() == MVT::f32) {
4834 SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N),
4835 MVT::v4f32, Vec);
4836 DCI.AddToWorklist(VRound.getNode());
4837 SDValue Extract1 =
4838 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32,
4839 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32));
4840 DCI.AddToWorklist(Extract1.getNode());
4841 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1);
4842 SDValue Extract0 =
4843 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32,
4844 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
4845 return Extract0;
4846 }
4847 }
4848 }
4849 }
4850 }
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004851 return SDValue();
4852}
4853
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004854//===----------------------------------------------------------------------===//
4855// Custom insertion
4856//===----------------------------------------------------------------------===//
4857
4858// Create a new basic block after MBB.
4859static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
4860 MachineFunction &MF = *MBB->getParent();
4861 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00004862 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004863 return NewMBB;
4864}
4865
Richard Sandifordbe133a82013-08-28 09:01:51 +00004866// Split MBB after MI and return the new block (the one that contains
4867// instructions after MI).
4868static MachineBasicBlock *splitBlockAfter(MachineInstr *MI,
4869 MachineBasicBlock *MBB) {
4870 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
4871 NewMBB->splice(NewMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00004872 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Richard Sandifordbe133a82013-08-28 09:01:51 +00004873 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
4874 return NewMBB;
4875}
4876
Richard Sandiford5e318f02013-08-27 09:54:29 +00004877// Split MBB before MI and return the new block (the one that contains MI).
4878static MachineBasicBlock *splitBlockBefore(MachineInstr *MI,
4879 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004880 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004881 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004882 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
4883 return NewMBB;
4884}
4885
Richard Sandiford5e318f02013-08-27 09:54:29 +00004886// Force base value Base into a register before MI. Return the register.
4887static unsigned forceReg(MachineInstr *MI, MachineOperand &Base,
4888 const SystemZInstrInfo *TII) {
4889 if (Base.isReg())
4890 return Base.getReg();
4891
4892 MachineBasicBlock *MBB = MI->getParent();
4893 MachineFunction &MF = *MBB->getParent();
4894 MachineRegisterInfo &MRI = MF.getRegInfo();
4895
4896 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
4897 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg)
4898 .addOperand(Base).addImm(0).addReg(0);
4899 return Reg;
4900}
4901
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004902// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
4903MachineBasicBlock *
4904SystemZTargetLowering::emitSelect(MachineInstr *MI,
4905 MachineBasicBlock *MBB) const {
Eric Christophera6734172015-01-31 00:06:45 +00004906 const SystemZInstrInfo *TII =
4907 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004908
4909 unsigned DestReg = MI->getOperand(0).getReg();
4910 unsigned TrueReg = MI->getOperand(1).getReg();
4911 unsigned FalseReg = MI->getOperand(2).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00004912 unsigned CCValid = MI->getOperand(3).getImm();
4913 unsigned CCMask = MI->getOperand(4).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004914 DebugLoc DL = MI->getDebugLoc();
4915
4916 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004917 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004918 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
4919
4920 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00004921 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004922 // # fallthrough to FalseMBB
4923 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00004924 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4925 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004926 MBB->addSuccessor(JoinMBB);
4927 MBB->addSuccessor(FalseMBB);
4928
4929 // FalseMBB:
4930 // # fallthrough to JoinMBB
4931 MBB = FalseMBB;
4932 MBB->addSuccessor(JoinMBB);
4933
4934 // JoinMBB:
4935 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
4936 // ...
4937 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004938 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004939 .addReg(TrueReg).addMBB(StartMBB)
4940 .addReg(FalseReg).addMBB(FalseMBB);
4941
4942 MI->eraseFromParent();
4943 return JoinMBB;
4944}
4945
Richard Sandifordb86a8342013-06-27 09:27:40 +00004946// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
4947// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004948// happen when the condition is false rather than true. If a STORE ON
4949// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Richard Sandifordb86a8342013-06-27 09:27:40 +00004950MachineBasicBlock *
4951SystemZTargetLowering::emitCondStore(MachineInstr *MI,
4952 MachineBasicBlock *MBB,
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004953 unsigned StoreOpcode, unsigned STOCOpcode,
4954 bool Invert) const {
Eric Christophera6734172015-01-31 00:06:45 +00004955 const SystemZInstrInfo *TII =
4956 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordb86a8342013-06-27 09:27:40 +00004957
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004958 unsigned SrcReg = MI->getOperand(0).getReg();
4959 MachineOperand Base = MI->getOperand(1);
4960 int64_t Disp = MI->getOperand(2).getImm();
4961 unsigned IndexReg = MI->getOperand(3).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00004962 unsigned CCValid = MI->getOperand(4).getImm();
4963 unsigned CCMask = MI->getOperand(5).getImm();
Richard Sandifordb86a8342013-06-27 09:27:40 +00004964 DebugLoc DL = MI->getDebugLoc();
4965
4966 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
4967
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004968 // Use STOCOpcode if possible. We could use different store patterns in
4969 // order to avoid matching the index register, but the performance trade-offs
4970 // might be more complicated in that case.
Eric Christopher93bf97c2014-06-27 07:38:01 +00004971 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004972 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00004973 CCMask ^= CCValid;
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004974 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +00004975 .addReg(SrcReg).addOperand(Base).addImm(Disp)
4976 .addImm(CCValid).addImm(CCMask);
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004977 MI->eraseFromParent();
4978 return MBB;
4979 }
4980
Richard Sandifordb86a8342013-06-27 09:27:40 +00004981 // Get the condition needed to branch around the store.
4982 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00004983 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00004984
4985 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004986 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00004987 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
4988
4989 // StartMBB:
4990 // BRC CCMask, JoinMBB
4991 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00004992 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00004993 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4994 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00004995 MBB->addSuccessor(JoinMBB);
4996 MBB->addSuccessor(FalseMBB);
4997
4998 // FalseMBB:
4999 // store %SrcReg, %Disp(%Index,%Base)
5000 // # fallthrough to JoinMBB
5001 MBB = FalseMBB;
5002 BuildMI(MBB, DL, TII->get(StoreOpcode))
5003 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
5004 MBB->addSuccessor(JoinMBB);
5005
5006 MI->eraseFromParent();
5007 return JoinMBB;
5008}
5009
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005010// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
5011// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
5012// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
5013// BitSize is the width of the field in bits, or 0 if this is a partword
5014// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
5015// is one of the operands. Invert says whether the field should be
5016// inverted after performing BinOpcode (e.g. for NAND).
5017MachineBasicBlock *
5018SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI,
5019 MachineBasicBlock *MBB,
5020 unsigned BinOpcode,
5021 unsigned BitSize,
5022 bool Invert) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005023 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005024 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005025 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005026 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005027 bool IsSubWord = (BitSize < 32);
5028
5029 // Extract the operands. Base can be a register or a frame index.
5030 // Src2 can be a register or immediate.
5031 unsigned Dest = MI->getOperand(0).getReg();
5032 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
5033 int64_t Disp = MI->getOperand(2).getImm();
5034 MachineOperand Src2 = earlyUseOperand(MI->getOperand(3));
5035 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
5036 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
5037 DebugLoc DL = MI->getDebugLoc();
5038 if (IsSubWord)
5039 BitSize = MI->getOperand(6).getImm();
5040
5041 // Subword operations use 32-bit registers.
5042 const TargetRegisterClass *RC = (BitSize <= 32 ?
5043 &SystemZ::GR32BitRegClass :
5044 &SystemZ::GR64BitRegClass);
5045 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5046 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5047
5048 // Get the right opcodes for the displacement.
5049 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5050 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5051 assert(LOpcode && CSOpcode && "Displacement out of range");
5052
5053 // Create virtual registers for temporary results.
5054 unsigned OrigVal = MRI.createVirtualRegister(RC);
5055 unsigned OldVal = MRI.createVirtualRegister(RC);
5056 unsigned NewVal = (BinOpcode || IsSubWord ?
5057 MRI.createVirtualRegister(RC) : Src2.getReg());
5058 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5059 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5060
5061 // Insert a basic block for the main loop.
5062 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005063 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005064 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5065
5066 // StartMBB:
5067 // ...
5068 // %OrigVal = L Disp(%Base)
5069 // # fall through to LoopMMB
5070 MBB = StartMBB;
5071 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
5072 .addOperand(Base).addImm(Disp).addReg(0);
5073 MBB->addSuccessor(LoopMBB);
5074
5075 // LoopMBB:
5076 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
5077 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5078 // %RotatedNewVal = OP %RotatedOldVal, %Src2
5079 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5080 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5081 // JNE LoopMBB
5082 // # fall through to DoneMMB
5083 MBB = LoopMBB;
5084 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5085 .addReg(OrigVal).addMBB(StartMBB)
5086 .addReg(Dest).addMBB(LoopMBB);
5087 if (IsSubWord)
5088 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5089 .addReg(OldVal).addReg(BitShift).addImm(0);
5090 if (Invert) {
5091 // Perform the operation normally and then invert every bit of the field.
5092 unsigned Tmp = MRI.createVirtualRegister(RC);
5093 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
5094 .addReg(RotatedOldVal).addOperand(Src2);
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005095 if (BitSize <= 32)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005096 // XILF with the upper BitSize bits set.
Richard Sandiford652784e2013-09-25 11:11:53 +00005097 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005098 .addReg(Tmp).addImm(-1U << (32 - BitSize));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005099 else {
5100 // Use LCGR and add -1 to the result, which is more compact than
5101 // an XILF, XILH pair.
5102 unsigned Tmp2 = MRI.createVirtualRegister(RC);
5103 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
5104 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
5105 .addReg(Tmp2).addImm(-1);
5106 }
5107 } else if (BinOpcode)
5108 // A simply binary operation.
5109 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
5110 .addReg(RotatedOldVal).addOperand(Src2);
5111 else if (IsSubWord)
5112 // Use RISBG to rotate Src2 into position and use it to replace the
5113 // field in RotatedOldVal.
5114 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
5115 .addReg(RotatedOldVal).addReg(Src2.getReg())
5116 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
5117 if (IsSubWord)
5118 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5119 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5120 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
5121 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005122 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5123 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005124 MBB->addSuccessor(LoopMBB);
5125 MBB->addSuccessor(DoneMBB);
5126
5127 MI->eraseFromParent();
5128 return DoneMBB;
5129}
5130
5131// Implement EmitInstrWithCustomInserter for pseudo
5132// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
5133// instruction that should be used to compare the current field with the
5134// minimum or maximum value. KeepOldMask is the BRC condition-code mask
5135// for when the current field should be kept. BitSize is the width of
5136// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
5137MachineBasicBlock *
5138SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI,
5139 MachineBasicBlock *MBB,
5140 unsigned CompareOpcode,
5141 unsigned KeepOldMask,
5142 unsigned BitSize) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005143 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005144 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005145 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005146 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005147 bool IsSubWord = (BitSize < 32);
5148
5149 // Extract the operands. Base can be a register or a frame index.
5150 unsigned Dest = MI->getOperand(0).getReg();
5151 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
5152 int64_t Disp = MI->getOperand(2).getImm();
5153 unsigned Src2 = MI->getOperand(3).getReg();
5154 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
5155 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
5156 DebugLoc DL = MI->getDebugLoc();
5157 if (IsSubWord)
5158 BitSize = MI->getOperand(6).getImm();
5159
5160 // Subword operations use 32-bit registers.
5161 const TargetRegisterClass *RC = (BitSize <= 32 ?
5162 &SystemZ::GR32BitRegClass :
5163 &SystemZ::GR64BitRegClass);
5164 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5165 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5166
5167 // Get the right opcodes for the displacement.
5168 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5169 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5170 assert(LOpcode && CSOpcode && "Displacement out of range");
5171
5172 // Create virtual registers for temporary results.
5173 unsigned OrigVal = MRI.createVirtualRegister(RC);
5174 unsigned OldVal = MRI.createVirtualRegister(RC);
5175 unsigned NewVal = MRI.createVirtualRegister(RC);
5176 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5177 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
5178 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5179
5180 // Insert 3 basic blocks for the loop.
5181 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005182 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005183 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5184 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
5185 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
5186
5187 // StartMBB:
5188 // ...
5189 // %OrigVal = L Disp(%Base)
5190 // # fall through to LoopMMB
5191 MBB = StartMBB;
5192 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
5193 .addOperand(Base).addImm(Disp).addReg(0);
5194 MBB->addSuccessor(LoopMBB);
5195
5196 // LoopMBB:
5197 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
5198 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5199 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00005200 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005201 MBB = LoopMBB;
5202 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5203 .addReg(OrigVal).addMBB(StartMBB)
5204 .addReg(Dest).addMBB(UpdateMBB);
5205 if (IsSubWord)
5206 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5207 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005208 BuildMI(MBB, DL, TII->get(CompareOpcode))
5209 .addReg(RotatedOldVal).addReg(Src2);
5210 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005211 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005212 MBB->addSuccessor(UpdateMBB);
5213 MBB->addSuccessor(UseAltMBB);
5214
5215 // UseAltMBB:
5216 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
5217 // # fall through to UpdateMMB
5218 MBB = UseAltMBB;
5219 if (IsSubWord)
5220 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
5221 .addReg(RotatedOldVal).addReg(Src2)
5222 .addImm(32).addImm(31 + BitSize).addImm(0);
5223 MBB->addSuccessor(UpdateMBB);
5224
5225 // UpdateMBB:
5226 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
5227 // [ %RotatedAltVal, UseAltMBB ]
5228 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5229 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5230 // JNE LoopMBB
5231 // # fall through to DoneMMB
5232 MBB = UpdateMBB;
5233 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
5234 .addReg(RotatedOldVal).addMBB(LoopMBB)
5235 .addReg(RotatedAltVal).addMBB(UseAltMBB);
5236 if (IsSubWord)
5237 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5238 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5239 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
5240 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005241 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5242 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005243 MBB->addSuccessor(LoopMBB);
5244 MBB->addSuccessor(DoneMBB);
5245
5246 MI->eraseFromParent();
5247 return DoneMBB;
5248}
5249
5250// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
5251// instruction MI.
5252MachineBasicBlock *
5253SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI,
5254 MachineBasicBlock *MBB) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005255 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005256 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005257 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005258 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005259
5260 // Extract the operands. Base can be a register or a frame index.
5261 unsigned Dest = MI->getOperand(0).getReg();
5262 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
5263 int64_t Disp = MI->getOperand(2).getImm();
5264 unsigned OrigCmpVal = MI->getOperand(3).getReg();
5265 unsigned OrigSwapVal = MI->getOperand(4).getReg();
5266 unsigned BitShift = MI->getOperand(5).getReg();
5267 unsigned NegBitShift = MI->getOperand(6).getReg();
5268 int64_t BitSize = MI->getOperand(7).getImm();
5269 DebugLoc DL = MI->getDebugLoc();
5270
5271 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
5272
5273 // Get the right opcodes for the displacement.
5274 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
5275 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
5276 assert(LOpcode && CSOpcode && "Displacement out of range");
5277
5278 // Create virtual registers for temporary results.
5279 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
5280 unsigned OldVal = MRI.createVirtualRegister(RC);
5281 unsigned CmpVal = MRI.createVirtualRegister(RC);
5282 unsigned SwapVal = MRI.createVirtualRegister(RC);
5283 unsigned StoreVal = MRI.createVirtualRegister(RC);
5284 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
5285 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
5286 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
5287
5288 // Insert 2 basic blocks for the loop.
5289 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005290 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005291 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5292 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
5293
5294 // StartMBB:
5295 // ...
5296 // %OrigOldVal = L Disp(%Base)
5297 // # fall through to LoopMMB
5298 MBB = StartMBB;
5299 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
5300 .addOperand(Base).addImm(Disp).addReg(0);
5301 MBB->addSuccessor(LoopMBB);
5302
5303 // LoopMBB:
5304 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
5305 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
5306 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
5307 // %Dest = RLL %OldVal, BitSize(%BitShift)
5308 // ^^ The low BitSize bits contain the field
5309 // of interest.
5310 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
5311 // ^^ Replace the upper 32-BitSize bits of the
5312 // comparison value with those that we loaded,
5313 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005314 // CR %Dest, %RetryCmpVal
5315 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005316 // # Fall through to SetMBB
5317 MBB = LoopMBB;
5318 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5319 .addReg(OrigOldVal).addMBB(StartMBB)
5320 .addReg(RetryOldVal).addMBB(SetMBB);
5321 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
5322 .addReg(OrigCmpVal).addMBB(StartMBB)
5323 .addReg(RetryCmpVal).addMBB(SetMBB);
5324 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
5325 .addReg(OrigSwapVal).addMBB(StartMBB)
5326 .addReg(RetrySwapVal).addMBB(SetMBB);
5327 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
5328 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
5329 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
5330 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005331 BuildMI(MBB, DL, TII->get(SystemZ::CR))
5332 .addReg(Dest).addReg(RetryCmpVal);
5333 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005334 .addImm(SystemZ::CCMASK_ICMP)
5335 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005336 MBB->addSuccessor(DoneMBB);
5337 MBB->addSuccessor(SetMBB);
5338
5339 // SetMBB:
5340 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
5341 // ^^ Replace the upper 32-BitSize bits of the new
5342 // value with those that we loaded.
5343 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
5344 // ^^ Rotate the new field to its proper position.
5345 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
5346 // JNE LoopMBB
5347 // # fall through to ExitMMB
5348 MBB = SetMBB;
5349 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
5350 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
5351 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
5352 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
5353 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
5354 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005355 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5356 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005357 MBB->addSuccessor(LoopMBB);
5358 MBB->addSuccessor(DoneMBB);
5359
5360 MI->eraseFromParent();
5361 return DoneMBB;
5362}
5363
5364// Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true
5365// if the high register of the GR128 value must be cleared or false if
Richard Sandiford87a44362013-09-30 10:28:35 +00005366// it's "don't care". SubReg is subreg_l32 when extending a GR32
5367// and subreg_l64 when extending a GR64.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005368MachineBasicBlock *
5369SystemZTargetLowering::emitExt128(MachineInstr *MI,
5370 MachineBasicBlock *MBB,
5371 bool ClearEven, unsigned SubReg) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005372 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005373 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005374 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005375 MachineRegisterInfo &MRI = MF.getRegInfo();
5376 DebugLoc DL = MI->getDebugLoc();
5377
5378 unsigned Dest = MI->getOperand(0).getReg();
5379 unsigned Src = MI->getOperand(1).getReg();
5380 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
5381
5382 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
5383 if (ClearEven) {
5384 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
5385 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
5386
5387 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
5388 .addImm(0);
5389 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
Richard Sandiford87a44362013-09-30 10:28:35 +00005390 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005391 In128 = NewIn128;
5392 }
5393 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
5394 .addReg(In128).addReg(Src).addImm(SubReg);
5395
5396 MI->eraseFromParent();
5397 return MBB;
5398}
5399
Richard Sandifordd131ff82013-07-08 09:35:23 +00005400MachineBasicBlock *
Richard Sandiford564681c2013-08-12 10:28:10 +00005401SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI,
5402 MachineBasicBlock *MBB,
5403 unsigned Opcode) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +00005404 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005405 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005406 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandiford5e318f02013-08-27 09:54:29 +00005407 MachineRegisterInfo &MRI = MF.getRegInfo();
Richard Sandifordd131ff82013-07-08 09:35:23 +00005408 DebugLoc DL = MI->getDebugLoc();
5409
Richard Sandiford5e318f02013-08-27 09:54:29 +00005410 MachineOperand DestBase = earlyUseOperand(MI->getOperand(0));
Richard Sandifordd131ff82013-07-08 09:35:23 +00005411 uint64_t DestDisp = MI->getOperand(1).getImm();
Richard Sandiford5e318f02013-08-27 09:54:29 +00005412 MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2));
Richard Sandifordd131ff82013-07-08 09:35:23 +00005413 uint64_t SrcDisp = MI->getOperand(3).getImm();
5414 uint64_t Length = MI->getOperand(4).getImm();
5415
Richard Sandifordbe133a82013-08-28 09:01:51 +00005416 // When generating more than one CLC, all but the last will need to
5417 // branch to the end when a difference is found.
5418 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
Craig Topper062a2ba2014-04-25 05:30:21 +00005419 splitBlockAfter(MI, MBB) : nullptr);
Richard Sandifordbe133a82013-08-28 09:01:51 +00005420
Richard Sandiford5e318f02013-08-27 09:54:29 +00005421 // Check for the loop form, in which operand 5 is the trip count.
5422 if (MI->getNumExplicitOperands() > 5) {
5423 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
5424
5425 uint64_t StartCountReg = MI->getOperand(5).getReg();
5426 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
5427 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
5428 forceReg(MI, DestBase, TII));
5429
5430 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
5431 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
5432 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
5433 MRI.createVirtualRegister(RC));
5434 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
5435 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
5436 MRI.createVirtualRegister(RC));
5437
5438 RC = &SystemZ::GR64BitRegClass;
5439 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
5440 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
5441
5442 MachineBasicBlock *StartMBB = MBB;
5443 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
5444 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00005445 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005446
5447 // StartMBB:
5448 // # fall through to LoopMMB
5449 MBB->addSuccessor(LoopMBB);
5450
5451 // LoopMBB:
5452 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005453 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00005454 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005455 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00005456 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005457 // [ %NextCountReg, NextMBB ]
5458 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00005459 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00005460 // ( JLH EndMBB )
5461 //
5462 // The prefetch is used only for MVC. The JLH is used only for CLC.
5463 MBB = LoopMBB;
5464
5465 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
5466 .addReg(StartDestReg).addMBB(StartMBB)
5467 .addReg(NextDestReg).addMBB(NextMBB);
5468 if (!HaveSingleBase)
5469 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
5470 .addReg(StartSrcReg).addMBB(StartMBB)
5471 .addReg(NextSrcReg).addMBB(NextMBB);
5472 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
5473 .addReg(StartCountReg).addMBB(StartMBB)
5474 .addReg(NextCountReg).addMBB(NextMBB);
5475 if (Opcode == SystemZ::MVC)
5476 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
5477 .addImm(SystemZ::PFD_WRITE)
5478 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
5479 BuildMI(MBB, DL, TII->get(Opcode))
5480 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
5481 .addReg(ThisSrcReg).addImm(SrcDisp);
5482 if (EndMBB) {
5483 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5484 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5485 .addMBB(EndMBB);
5486 MBB->addSuccessor(EndMBB);
5487 MBB->addSuccessor(NextMBB);
5488 }
5489
5490 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00005491 // %NextDestReg = LA 256(%ThisDestReg)
5492 // %NextSrcReg = LA 256(%ThisSrcReg)
5493 // %NextCountReg = AGHI %ThisCountReg, -1
5494 // CGHI %NextCountReg, 0
5495 // JLH LoopMBB
5496 // # fall through to DoneMMB
5497 //
5498 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00005499 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005500
Richard Sandiford5e318f02013-08-27 09:54:29 +00005501 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
5502 .addReg(ThisDestReg).addImm(256).addReg(0);
5503 if (!HaveSingleBase)
5504 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
5505 .addReg(ThisSrcReg).addImm(256).addReg(0);
5506 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
5507 .addReg(ThisCountReg).addImm(-1);
5508 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
5509 .addReg(NextCountReg).addImm(0);
5510 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5511 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5512 .addMBB(LoopMBB);
5513 MBB->addSuccessor(LoopMBB);
5514 MBB->addSuccessor(DoneMBB);
5515
5516 DestBase = MachineOperand::CreateReg(NextDestReg, false);
5517 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
5518 Length &= 255;
5519 MBB = DoneMBB;
5520 }
5521 // Handle any remaining bytes with straight-line code.
5522 while (Length > 0) {
5523 uint64_t ThisLength = std::min(Length, uint64_t(256));
5524 // The previous iteration might have created out-of-range displacements.
5525 // Apply them using LAY if so.
5526 if (!isUInt<12>(DestDisp)) {
5527 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
5528 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
5529 .addOperand(DestBase).addImm(DestDisp).addReg(0);
5530 DestBase = MachineOperand::CreateReg(Reg, false);
5531 DestDisp = 0;
5532 }
5533 if (!isUInt<12>(SrcDisp)) {
5534 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
5535 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
5536 .addOperand(SrcBase).addImm(SrcDisp).addReg(0);
5537 SrcBase = MachineOperand::CreateReg(Reg, false);
5538 SrcDisp = 0;
5539 }
5540 BuildMI(*MBB, MI, DL, TII->get(Opcode))
5541 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength)
5542 .addOperand(SrcBase).addImm(SrcDisp);
5543 DestDisp += ThisLength;
5544 SrcDisp += ThisLength;
5545 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00005546 // If there's another CLC to go, branch to the end if a difference
5547 // was found.
5548 if (EndMBB && Length > 0) {
5549 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
5550 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5551 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5552 .addMBB(EndMBB);
5553 MBB->addSuccessor(EndMBB);
5554 MBB->addSuccessor(NextMBB);
5555 MBB = NextMBB;
5556 }
5557 }
5558 if (EndMBB) {
5559 MBB->addSuccessor(EndMBB);
5560 MBB = EndMBB;
5561 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005562 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00005563
5564 MI->eraseFromParent();
5565 return MBB;
5566}
5567
Richard Sandifordca232712013-08-16 11:21:54 +00005568// Decompose string pseudo-instruction MI into a loop that continually performs
5569// Opcode until CC != 3.
5570MachineBasicBlock *
5571SystemZTargetLowering::emitStringWrapper(MachineInstr *MI,
5572 MachineBasicBlock *MBB,
5573 unsigned Opcode) const {
Richard Sandifordca232712013-08-16 11:21:54 +00005574 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005575 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005576 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordca232712013-08-16 11:21:54 +00005577 MachineRegisterInfo &MRI = MF.getRegInfo();
5578 DebugLoc DL = MI->getDebugLoc();
5579
5580 uint64_t End1Reg = MI->getOperand(0).getReg();
5581 uint64_t Start1Reg = MI->getOperand(1).getReg();
5582 uint64_t Start2Reg = MI->getOperand(2).getReg();
5583 uint64_t CharReg = MI->getOperand(3).getReg();
5584
5585 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
5586 uint64_t This1Reg = MRI.createVirtualRegister(RC);
5587 uint64_t This2Reg = MRI.createVirtualRegister(RC);
5588 uint64_t End2Reg = MRI.createVirtualRegister(RC);
5589
5590 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005591 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00005592 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5593
5594 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00005595 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00005596 MBB->addSuccessor(LoopMBB);
5597
5598 // LoopMBB:
5599 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
5600 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford7789b082013-09-30 08:48:38 +00005601 // R0L = %CharReg
5602 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
Richard Sandifordca232712013-08-16 11:21:54 +00005603 // JO LoopMBB
5604 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00005605 //
Richard Sandiford7789b082013-09-30 08:48:38 +00005606 // The load of R0L can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00005607 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00005608
5609 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
5610 .addReg(Start1Reg).addMBB(StartMBB)
5611 .addReg(End1Reg).addMBB(LoopMBB);
5612 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
5613 .addReg(Start2Reg).addMBB(StartMBB)
5614 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford7789b082013-09-30 08:48:38 +00005615 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00005616 BuildMI(MBB, DL, TII->get(Opcode))
5617 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
5618 .addReg(This1Reg).addReg(This2Reg);
5619 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5620 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
5621 MBB->addSuccessor(LoopMBB);
5622 MBB->addSuccessor(DoneMBB);
5623
5624 DoneMBB->addLiveIn(SystemZ::CC);
5625
5626 MI->eraseFromParent();
5627 return DoneMBB;
5628}
5629
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005630// Update TBEGIN instruction with final opcode and register clobbers.
5631MachineBasicBlock *
5632SystemZTargetLowering::emitTransactionBegin(MachineInstr *MI,
5633 MachineBasicBlock *MBB,
5634 unsigned Opcode,
5635 bool NoFloat) const {
5636 MachineFunction &MF = *MBB->getParent();
5637 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
5638 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
5639
5640 // Update opcode.
5641 MI->setDesc(TII->get(Opcode));
5642
5643 // We cannot handle a TBEGIN that clobbers the stack or frame pointer.
5644 // Make sure to add the corresponding GRSM bits if they are missing.
5645 uint64_t Control = MI->getOperand(2).getImm();
5646 static const unsigned GPRControlBit[16] = {
5647 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000,
5648 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100
5649 };
5650 Control |= GPRControlBit[15];
5651 if (TFI->hasFP(MF))
5652 Control |= GPRControlBit[11];
5653 MI->getOperand(2).setImm(Control);
5654
5655 // Add GPR clobbers.
5656 for (int I = 0; I < 16; I++) {
5657 if ((Control & GPRControlBit[I]) == 0) {
5658 unsigned Reg = SystemZMC::GR64Regs[I];
5659 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5660 }
5661 }
5662
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005663 // Add FPR/VR clobbers.
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005664 if (!NoFloat && (Control & 4) != 0) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005665 if (Subtarget.hasVector()) {
5666 for (int I = 0; I < 32; I++) {
5667 unsigned Reg = SystemZMC::VR128Regs[I];
5668 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5669 }
5670 } else {
5671 for (int I = 0; I < 16; I++) {
5672 unsigned Reg = SystemZMC::FP64Regs[I];
5673 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5674 }
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005675 }
5676 }
5677
5678 return MBB;
5679}
5680
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00005681MachineBasicBlock *
5682SystemZTargetLowering::emitLoadAndTestCmp0(MachineInstr *MI,
NAKAMURA Takumi50df0c22015-11-02 01:38:12 +00005683 MachineBasicBlock *MBB,
5684 unsigned Opcode) const {
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00005685 MachineFunction &MF = *MBB->getParent();
5686 MachineRegisterInfo *MRI = &MF.getRegInfo();
5687 const SystemZInstrInfo *TII =
5688 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
5689 DebugLoc DL = MI->getDebugLoc();
5690
5691 unsigned SrcReg = MI->getOperand(0).getReg();
5692
5693 // Create new virtual register of the same class as source.
5694 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
5695 unsigned DstReg = MRI->createVirtualRegister(RC);
5696
5697 // Replace pseudo with a normal load-and-test that models the def as
5698 // well.
5699 BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg)
5700 .addReg(SrcReg);
5701 MI->eraseFromParent();
5702
5703 return MBB;
5704}
5705
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005706MachineBasicBlock *SystemZTargetLowering::
5707EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const {
5708 switch (MI->getOpcode()) {
Richard Sandiford7c5c0ea2013-10-01 13:10:16 +00005709 case SystemZ::Select32Mux:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005710 case SystemZ::Select32:
5711 case SystemZ::SelectF32:
5712 case SystemZ::Select64:
5713 case SystemZ::SelectF64:
5714 case SystemZ::SelectF128:
5715 return emitSelect(MI, MBB);
5716
Richard Sandiford2896d042013-10-01 14:33:55 +00005717 case SystemZ::CondStore8Mux:
5718 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
5719 case SystemZ::CondStore8MuxInv:
5720 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
5721 case SystemZ::CondStore16Mux:
5722 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
5723 case SystemZ::CondStore16MuxInv:
5724 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005725 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005726 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005727 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005728 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005729 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005730 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005731 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005732 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005733 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005734 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005735 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005736 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005737 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005738 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005739 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005740 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005741 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005742 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005743 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005744 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005745 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005746 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005747 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005748 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005749
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005750 case SystemZ::AEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00005751 return emitExt128(MI, MBB, false, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005752 case SystemZ::ZEXT128_32:
Richard Sandiford87a44362013-09-30 10:28:35 +00005753 return emitExt128(MI, MBB, true, SystemZ::subreg_l32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005754 case SystemZ::ZEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00005755 return emitExt128(MI, MBB, true, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005756
5757 case SystemZ::ATOMIC_SWAPW:
5758 return emitAtomicLoadBinary(MI, MBB, 0, 0);
5759 case SystemZ::ATOMIC_SWAP_32:
5760 return emitAtomicLoadBinary(MI, MBB, 0, 32);
5761 case SystemZ::ATOMIC_SWAP_64:
5762 return emitAtomicLoadBinary(MI, MBB, 0, 64);
5763
5764 case SystemZ::ATOMIC_LOADW_AR:
5765 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
5766 case SystemZ::ATOMIC_LOADW_AFI:
5767 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
5768 case SystemZ::ATOMIC_LOAD_AR:
5769 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
5770 case SystemZ::ATOMIC_LOAD_AHI:
5771 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
5772 case SystemZ::ATOMIC_LOAD_AFI:
5773 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
5774 case SystemZ::ATOMIC_LOAD_AGR:
5775 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
5776 case SystemZ::ATOMIC_LOAD_AGHI:
5777 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
5778 case SystemZ::ATOMIC_LOAD_AGFI:
5779 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
5780
5781 case SystemZ::ATOMIC_LOADW_SR:
5782 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
5783 case SystemZ::ATOMIC_LOAD_SR:
5784 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
5785 case SystemZ::ATOMIC_LOAD_SGR:
5786 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
5787
5788 case SystemZ::ATOMIC_LOADW_NR:
5789 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
5790 case SystemZ::ATOMIC_LOADW_NILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00005791 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005792 case SystemZ::ATOMIC_LOAD_NR:
5793 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005794 case SystemZ::ATOMIC_LOAD_NILL:
5795 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
5796 case SystemZ::ATOMIC_LOAD_NILH:
5797 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
5798 case SystemZ::ATOMIC_LOAD_NILF:
5799 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005800 case SystemZ::ATOMIC_LOAD_NGR:
5801 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005802 case SystemZ::ATOMIC_LOAD_NILL64:
5803 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
5804 case SystemZ::ATOMIC_LOAD_NILH64:
5805 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00005806 case SystemZ::ATOMIC_LOAD_NIHL64:
5807 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
5808 case SystemZ::ATOMIC_LOAD_NIHH64:
5809 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005810 case SystemZ::ATOMIC_LOAD_NILF64:
5811 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00005812 case SystemZ::ATOMIC_LOAD_NIHF64:
5813 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005814
5815 case SystemZ::ATOMIC_LOADW_OR:
5816 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
5817 case SystemZ::ATOMIC_LOADW_OILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00005818 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005819 case SystemZ::ATOMIC_LOAD_OR:
5820 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005821 case SystemZ::ATOMIC_LOAD_OILL:
5822 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
5823 case SystemZ::ATOMIC_LOAD_OILH:
5824 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
5825 case SystemZ::ATOMIC_LOAD_OILF:
5826 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005827 case SystemZ::ATOMIC_LOAD_OGR:
5828 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005829 case SystemZ::ATOMIC_LOAD_OILL64:
5830 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
5831 case SystemZ::ATOMIC_LOAD_OILH64:
5832 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00005833 case SystemZ::ATOMIC_LOAD_OIHL64:
5834 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
5835 case SystemZ::ATOMIC_LOAD_OIHH64:
5836 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005837 case SystemZ::ATOMIC_LOAD_OILF64:
5838 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00005839 case SystemZ::ATOMIC_LOAD_OIHF64:
5840 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005841
5842 case SystemZ::ATOMIC_LOADW_XR:
5843 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
5844 case SystemZ::ATOMIC_LOADW_XILF:
Richard Sandiford652784e2013-09-25 11:11:53 +00005845 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005846 case SystemZ::ATOMIC_LOAD_XR:
5847 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005848 case SystemZ::ATOMIC_LOAD_XILF:
5849 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005850 case SystemZ::ATOMIC_LOAD_XGR:
5851 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005852 case SystemZ::ATOMIC_LOAD_XILF64:
5853 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
Richard Sandiford5718dac2013-10-01 14:08:44 +00005854 case SystemZ::ATOMIC_LOAD_XIHF64:
5855 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005856
5857 case SystemZ::ATOMIC_LOADW_NRi:
5858 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
5859 case SystemZ::ATOMIC_LOADW_NILHi:
Richard Sandiford652784e2013-09-25 11:11:53 +00005860 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005861 case SystemZ::ATOMIC_LOAD_NRi:
5862 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005863 case SystemZ::ATOMIC_LOAD_NILLi:
5864 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
5865 case SystemZ::ATOMIC_LOAD_NILHi:
5866 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
5867 case SystemZ::ATOMIC_LOAD_NILFi:
5868 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005869 case SystemZ::ATOMIC_LOAD_NGRi:
5870 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005871 case SystemZ::ATOMIC_LOAD_NILL64i:
5872 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
5873 case SystemZ::ATOMIC_LOAD_NILH64i:
5874 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00005875 case SystemZ::ATOMIC_LOAD_NIHL64i:
5876 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
5877 case SystemZ::ATOMIC_LOAD_NIHH64i:
5878 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005879 case SystemZ::ATOMIC_LOAD_NILF64i:
5880 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00005881 case SystemZ::ATOMIC_LOAD_NIHF64i:
5882 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005883
5884 case SystemZ::ATOMIC_LOADW_MIN:
5885 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5886 SystemZ::CCMASK_CMP_LE, 0);
5887 case SystemZ::ATOMIC_LOAD_MIN_32:
5888 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5889 SystemZ::CCMASK_CMP_LE, 32);
5890 case SystemZ::ATOMIC_LOAD_MIN_64:
5891 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
5892 SystemZ::CCMASK_CMP_LE, 64);
5893
5894 case SystemZ::ATOMIC_LOADW_MAX:
5895 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5896 SystemZ::CCMASK_CMP_GE, 0);
5897 case SystemZ::ATOMIC_LOAD_MAX_32:
5898 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5899 SystemZ::CCMASK_CMP_GE, 32);
5900 case SystemZ::ATOMIC_LOAD_MAX_64:
5901 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
5902 SystemZ::CCMASK_CMP_GE, 64);
5903
5904 case SystemZ::ATOMIC_LOADW_UMIN:
5905 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5906 SystemZ::CCMASK_CMP_LE, 0);
5907 case SystemZ::ATOMIC_LOAD_UMIN_32:
5908 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5909 SystemZ::CCMASK_CMP_LE, 32);
5910 case SystemZ::ATOMIC_LOAD_UMIN_64:
5911 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
5912 SystemZ::CCMASK_CMP_LE, 64);
5913
5914 case SystemZ::ATOMIC_LOADW_UMAX:
5915 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5916 SystemZ::CCMASK_CMP_GE, 0);
5917 case SystemZ::ATOMIC_LOAD_UMAX_32:
5918 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5919 SystemZ::CCMASK_CMP_GE, 32);
5920 case SystemZ::ATOMIC_LOAD_UMAX_64:
5921 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
5922 SystemZ::CCMASK_CMP_GE, 64);
5923
5924 case SystemZ::ATOMIC_CMP_SWAPW:
5925 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005926 case SystemZ::MVCSequence:
5927 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00005928 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00005929 case SystemZ::NCSequence:
5930 case SystemZ::NCLoop:
5931 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
5932 case SystemZ::OCSequence:
5933 case SystemZ::OCLoop:
5934 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
5935 case SystemZ::XCSequence:
5936 case SystemZ::XCLoop:
5937 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005938 case SystemZ::CLCSequence:
5939 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00005940 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00005941 case SystemZ::CLSTLoop:
5942 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00005943 case SystemZ::MVSTLoop:
5944 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00005945 case SystemZ::SRSTLoop:
5946 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005947 case SystemZ::TBEGIN:
5948 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false);
5949 case SystemZ::TBEGIN_nofloat:
5950 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true);
5951 case SystemZ::TBEGINC:
5952 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true);
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00005953 case SystemZ::LTEBRCompare_VecPseudo:
5954 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR);
5955 case SystemZ::LTDBRCompare_VecPseudo:
5956 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR);
5957 case SystemZ::LTXBRCompare_VecPseudo:
5958 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR);
5959
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005960 default:
5961 llvm_unreachable("Unexpected instr type to insert");
5962 }
5963}