blob: 2ddee39754c0aafa34a6074e136a46a2ff0495dc [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"
Jonas Paulsson7a794222016-08-17 13:24:19 +000023#include "llvm/Support/CommandLine.h"
Ulrich Weigand57c85f52015-04-01 12:51:43 +000024#include "llvm/IR/Intrinsics.h"
Will Dietz981af002013-10-12 00:55:57 +000025#include <cctype>
26
Ulrich Weigand5f613df2013-05-06 16:15:19 +000027using namespace llvm;
28
Chandler Carruth84e68b22014-04-22 02:41:26 +000029#define DEBUG_TYPE "systemz-lower"
30
Richard Sandifordf722a8e302013-10-16 11:10:55 +000031namespace {
32// Represents a sequence for extracting a 0/1 value from an IPM result:
33// (((X ^ XORValue) + AddValue) >> Bit)
34struct IPMConversion {
35 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
36 : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
37
38 int64_t XORValue;
39 int64_t AddValue;
40 unsigned Bit;
41};
Richard Sandifordd420f732013-12-13 15:28:45 +000042
43// Represents information about a comparison.
44struct Comparison {
45 Comparison(SDValue Op0In, SDValue Op1In)
46 : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {}
47
48 // The operands to the comparison.
49 SDValue Op0, Op1;
50
51 // The opcode that should be used to compare Op0 and Op1.
52 unsigned Opcode;
53
54 // A SystemZICMP value. Only used for integer comparisons.
55 unsigned ICmpType;
56
57 // The mask of CC values that Opcode can produce.
58 unsigned CCValid;
59
60 // The mask of CC values for which the original condition is true.
61 unsigned CCMask;
62};
Richard Sandifordc2312692014-03-06 10:38:30 +000063} // end anonymous namespace
Richard Sandifordf722a8e302013-10-16 11:10:55 +000064
Ulrich Weigand5f613df2013-05-06 16:15:19 +000065// Classify VT as either 32 or 64 bit.
66static bool is32Bit(EVT VT) {
67 switch (VT.getSimpleVT().SimpleTy) {
68 case MVT::i32:
69 return true;
70 case MVT::i64:
71 return false;
72 default:
73 llvm_unreachable("Unsupported type");
74 }
75}
76
77// Return a version of MachineOperand that can be safely used before the
78// final use.
79static MachineOperand earlyUseOperand(MachineOperand Op) {
80 if (Op.isReg())
81 Op.setIsKill(false);
82 return Op;
83}
84
Mehdi Amini44ede332015-07-09 02:09:04 +000085SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM,
Eric Christophera6734172015-01-31 00:06:45 +000086 const SystemZSubtarget &STI)
Mehdi Amini44ede332015-07-09 02:09:04 +000087 : TargetLowering(TM), Subtarget(STI) {
Mehdi Amini26d48132015-07-24 16:04:22 +000088 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
Ulrich Weigand5f613df2013-05-06 16:15:19 +000089
90 // Set up the register classes.
Richard Sandiford0755c932013-10-01 11:26:28 +000091 if (Subtarget.hasHighWord())
92 addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass);
93 else
94 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
Ulrich Weigand49506d72015-05-05 19:28:34 +000095 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
96 if (Subtarget.hasVector()) {
97 addRegisterClass(MVT::f32, &SystemZ::VR32BitRegClass);
98 addRegisterClass(MVT::f64, &SystemZ::VR64BitRegClass);
99 } else {
100 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
101 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
102 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000103 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
104
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000105 if (Subtarget.hasVector()) {
106 addRegisterClass(MVT::v16i8, &SystemZ::VR128BitRegClass);
107 addRegisterClass(MVT::v8i16, &SystemZ::VR128BitRegClass);
108 addRegisterClass(MVT::v4i32, &SystemZ::VR128BitRegClass);
109 addRegisterClass(MVT::v2i64, &SystemZ::VR128BitRegClass);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000110 addRegisterClass(MVT::v4f32, &SystemZ::VR128BitRegClass);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000111 addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000112 }
113
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000114 // Compute derived properties from the register classes
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000115 computeRegisterProperties(Subtarget.getRegisterInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000116
117 // Set up special registers.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000118 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
119
120 // TODO: It may be better to default to latency-oriented scheduling, however
121 // LLVM's current latency-oriented scheduler can't handle physreg definitions
Richard Sandiford14a44492013-05-22 13:38:45 +0000122 // such as SystemZ has with CC, so set this to the register-pressure
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000123 // scheduler, because it can.
124 setSchedulingPreference(Sched::RegPressure);
125
126 setBooleanContents(ZeroOrOneBooleanContent);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000127 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000128
129 // Instructions are strings of 2-byte aligned 2-byte values.
130 setMinFunctionAlignment(2);
131
132 // Handle operations that are handled in a similar way for all types.
133 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
134 I <= MVT::LAST_FP_VALUETYPE;
135 ++I) {
136 MVT VT = MVT::SimpleValueType(I);
137 if (isTypeLegal(VT)) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000138 // Lower SET_CC into an IPM-based sequence.
139 setOperationAction(ISD::SETCC, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000140
141 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
142 setOperationAction(ISD::SELECT, VT, Expand);
143
144 // Lower SELECT_CC and BR_CC into separate comparisons and branches.
145 setOperationAction(ISD::SELECT_CC, VT, Custom);
146 setOperationAction(ISD::BR_CC, VT, Custom);
147 }
148 }
149
150 // Expand jump table branches as address arithmetic followed by an
151 // indirect jump.
152 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
153
154 // Expand BRCOND into a BR_CC (see above).
155 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
156
157 // Handle integer types.
158 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
159 I <= MVT::LAST_INTEGER_VALUETYPE;
160 ++I) {
161 MVT VT = MVT::SimpleValueType(I);
162 if (isTypeLegal(VT)) {
163 // Expand individual DIV and REMs into DIVREMs.
164 setOperationAction(ISD::SDIV, VT, Expand);
165 setOperationAction(ISD::UDIV, VT, Expand);
166 setOperationAction(ISD::SREM, VT, Expand);
167 setOperationAction(ISD::UREM, VT, Expand);
168 setOperationAction(ISD::SDIVREM, VT, Custom);
169 setOperationAction(ISD::UDIVREM, VT, Custom);
170
Richard Sandifordbef3d7a2013-12-10 10:49:34 +0000171 // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and
172 // stores, putting a serialization instruction after the stores.
173 setOperationAction(ISD::ATOMIC_LOAD, VT, Custom);
174 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000175
Richard Sandiford41350a52013-12-24 15:18:04 +0000176 // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are
177 // available, or if the operand is constant.
178 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
179
Ulrich Weigandb4012182015-03-31 12:56:33 +0000180 // Use POPCNT on z196 and above.
181 if (Subtarget.hasPopulationCount())
182 setOperationAction(ISD::CTPOP, VT, Custom);
183 else
184 setOperationAction(ISD::CTPOP, VT, Expand);
185
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000186 // No special instructions for these.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000187 setOperationAction(ISD::CTTZ, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000188 setOperationAction(ISD::ROTR, VT, Expand);
189
Richard Sandiford7d86e472013-08-21 09:34:56 +0000190 // Use *MUL_LOHI where possible instead of MULH*.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000191 setOperationAction(ISD::MULHS, VT, Expand);
192 setOperationAction(ISD::MULHU, VT, Expand);
Richard Sandiford7d86e472013-08-21 09:34:56 +0000193 setOperationAction(ISD::SMUL_LOHI, VT, Custom);
194 setOperationAction(ISD::UMUL_LOHI, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000195
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000196 // Only z196 and above have native support for conversions to unsigned.
197 if (!Subtarget.hasFPExtension())
198 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000199 }
200 }
201
202 // Type legalization will convert 8- and 16-bit atomic operations into
203 // forms that operate on i32s (but still keeping the original memory VT).
204 // Lower them into full i32 operations.
205 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom);
206 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom);
207 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
208 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom);
209 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom);
210 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom);
211 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
212 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom);
213 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom);
214 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
215 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
216 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
217
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +0000218 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
219
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000220 // Traps are legal, as we will convert them to "j .+2".
221 setOperationAction(ISD::TRAP, MVT::Other, Legal);
222
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000223 // z10 has instructions for signed but not unsigned FP conversion.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000224 // Handle unsigned 32-bit types as signed 64-bit types.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000225 if (!Subtarget.hasFPExtension()) {
226 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
227 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
228 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000229
230 // We have native support for a 64-bit CTLZ, via FLOGR.
231 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
232 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
233
234 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
235 setOperationAction(ISD::OR, MVT::i64, Custom);
236
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000237 // FIXME: Can we support these natively?
238 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
239 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
240 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
241
242 // We have native instructions for i8, i16 and i32 extensions, but not i1.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000243 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000244 for (MVT VT : MVT::integer_valuetypes()) {
245 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
246 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
247 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
248 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000249
250 // Handle the various types of symbolic address.
251 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
252 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
253 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
254 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
255 setOperationAction(ISD::JumpTable, PtrVT, Custom);
256
257 // We need to handle dynamic allocations specially because of the
258 // 160-byte area at the bottom of the stack.
259 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +0000260 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, PtrVT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000261
262 // Use custom expanders so that we can force the function to use
263 // a frame pointer.
264 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
265 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
266
Richard Sandiford03481332013-08-23 11:36:42 +0000267 // Handle prefetches with PFD or PFDRL.
268 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
269
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000270 for (MVT VT : MVT::vector_valuetypes()) {
271 // Assume by default that all vector operations need to be expanded.
272 for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode)
273 if (getOperationAction(Opcode, VT) == Legal)
274 setOperationAction(Opcode, VT, Expand);
275
276 // Likewise all truncating stores and extending loads.
277 for (MVT InnerVT : MVT::vector_valuetypes()) {
278 setTruncStoreAction(VT, InnerVT, Expand);
279 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
280 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
281 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
282 }
283
284 if (isTypeLegal(VT)) {
285 // These operations are legal for anything that can be stored in a
286 // vector register, even if there is no native support for the format
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000287 // as such. In particular, we can do these for v4f32 even though there
288 // are no specific instructions for that format.
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000289 setOperationAction(ISD::LOAD, VT, Legal);
290 setOperationAction(ISD::STORE, VT, Legal);
291 setOperationAction(ISD::VSELECT, VT, Legal);
292 setOperationAction(ISD::BITCAST, VT, Legal);
293 setOperationAction(ISD::UNDEF, VT, Legal);
294
295 // Likewise, except that we need to replace the nodes with something
296 // more specific.
297 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
298 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
299 }
300 }
301
302 // Handle integer vector types.
303 for (MVT VT : MVT::integer_vector_valuetypes()) {
304 if (isTypeLegal(VT)) {
305 // These operations have direct equivalents.
306 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal);
307 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal);
308 setOperationAction(ISD::ADD, VT, Legal);
309 setOperationAction(ISD::SUB, VT, Legal);
310 if (VT != MVT::v2i64)
311 setOperationAction(ISD::MUL, VT, Legal);
312 setOperationAction(ISD::AND, VT, Legal);
313 setOperationAction(ISD::OR, VT, Legal);
314 setOperationAction(ISD::XOR, VT, Legal);
315 setOperationAction(ISD::CTPOP, VT, Custom);
316 setOperationAction(ISD::CTTZ, VT, Legal);
317 setOperationAction(ISD::CTLZ, VT, Legal);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000318
319 // Convert a GPR scalar to a vector by inserting it into element 0.
320 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
321
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000322 // Use a series of unpacks for extensions.
323 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Custom);
324 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Custom);
325
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000326 // Detect shifts by a scalar amount and convert them into
327 // V*_BY_SCALAR.
328 setOperationAction(ISD::SHL, VT, Custom);
329 setOperationAction(ISD::SRA, VT, Custom);
330 setOperationAction(ISD::SRL, VT, Custom);
331
332 // At present ROTL isn't matched by DAGCombiner. ROTR should be
333 // converted into ROTL.
334 setOperationAction(ISD::ROTL, VT, Expand);
335 setOperationAction(ISD::ROTR, VT, Expand);
336
337 // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands
338 // and inverting the result as necessary.
339 setOperationAction(ISD::SETCC, VT, Custom);
340 }
341 }
342
Ulrich Weigandcd808232015-05-05 19:26:48 +0000343 if (Subtarget.hasVector()) {
344 // There should be no need to check for float types other than v2f64
345 // since <2 x f32> isn't a legal type.
346 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
347 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
348 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
349 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
350 }
351
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000352 // Handle floating-point types.
353 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
354 I <= MVT::LAST_FP_VALUETYPE;
355 ++I) {
356 MVT VT = MVT::SimpleValueType(I);
357 if (isTypeLegal(VT)) {
358 // We can use FI for FRINT.
359 setOperationAction(ISD::FRINT, VT, Legal);
360
Richard Sandifordaf5f66a2013-08-21 09:04:20 +0000361 // We can use the extended form of FI for other rounding operations.
362 if (Subtarget.hasFPExtension()) {
363 setOperationAction(ISD::FNEARBYINT, VT, Legal);
364 setOperationAction(ISD::FFLOOR, VT, Legal);
365 setOperationAction(ISD::FCEIL, VT, Legal);
366 setOperationAction(ISD::FTRUNC, VT, Legal);
367 setOperationAction(ISD::FROUND, VT, Legal);
368 }
369
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000370 // No special instructions for these.
371 setOperationAction(ISD::FSIN, VT, Expand);
372 setOperationAction(ISD::FCOS, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000373 setOperationAction(ISD::FSINCOS, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000374 setOperationAction(ISD::FREM, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000375 setOperationAction(ISD::FPOW, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000376 }
377 }
378
Ulrich Weigandcd808232015-05-05 19:26:48 +0000379 // Handle floating-point vector types.
380 if (Subtarget.hasVector()) {
381 // Scalar-to-vector conversion is just a subreg.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000382 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000383 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
384
385 // Some insertions and extractions can be done directly but others
386 // need to go via integers.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000387 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000388 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000389 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000390 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
391
392 // These operations have direct equivalents.
393 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
394 setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
395 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
396 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
397 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
398 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
399 setOperationAction(ISD::FABS, MVT::v2f64, Legal);
400 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
401 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
402 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
403 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
404 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
405 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
406 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
407 }
408
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000409 // We have fused multiply-addition for f32 and f64 but not f128.
410 setOperationAction(ISD::FMA, MVT::f32, Legal);
411 setOperationAction(ISD::FMA, MVT::f64, Legal);
412 setOperationAction(ISD::FMA, MVT::f128, Expand);
413
414 // Needed so that we don't try to implement f128 constant loads using
415 // a load-and-extend of a f80 constant (in cases where the constant
416 // would fit in an f80).
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000417 for (MVT VT : MVT::fp_valuetypes())
418 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000419
420 // Floating-point truncation and stores need to be done separately.
421 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
422 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
423 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
424
425 // We have 64-bit FPR<->GPR moves, but need special handling for
426 // 32-bit forms.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000427 if (!Subtarget.hasVector()) {
428 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
429 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
430 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000431
432 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
433 // structure, but VAEND is a no-op.
434 setOperationAction(ISD::VASTART, MVT::Other, Custom);
435 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
436 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Richard Sandifordd131ff82013-07-08 09:35:23 +0000437
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000438 // Codes for which we want to perform some z-specific combinations.
439 setTargetDAGCombine(ISD::SIGN_EXTEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000440 setTargetDAGCombine(ISD::STORE);
441 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000442 setTargetDAGCombine(ISD::FP_ROUND);
Bryan Chan28b759c2016-05-16 20:32:22 +0000443 setTargetDAGCombine(ISD::BSWAP);
Elliot Colpbc2cfc22016-07-06 18:13:11 +0000444 setTargetDAGCombine(ISD::SHL);
445 setTargetDAGCombine(ISD::SRA);
446 setTargetDAGCombine(ISD::SRL);
447 setTargetDAGCombine(ISD::ROTL);
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000448
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000449 // Handle intrinsics.
450 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Ulrich Weigandc1708b22015-05-05 19:31:09 +0000451 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000452
Richard Sandifordd131ff82013-07-08 09:35:23 +0000453 // We want to use MVC in preference to even a single load/store pair.
454 MaxStoresPerMemcpy = 0;
455 MaxStoresPerMemcpyOptSize = 0;
Richard Sandiford47660c12013-07-09 09:32:42 +0000456
457 // The main memset sequence is a byte store followed by an MVC.
458 // Two STC or MV..I stores win over that, but the kind of fused stores
459 // generated by target-independent code don't when the byte value is
460 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
461 // than "STC;MVC". Handle the choice in target-specific code instead.
462 MaxStoresPerMemset = 0;
463 MaxStoresPerMemsetOptSize = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000464}
465
Mehdi Amini44ede332015-07-09 02:09:04 +0000466EVT SystemZTargetLowering::getSetCCResultType(const DataLayout &DL,
467 LLVMContext &, EVT VT) const {
Richard Sandifordabc010b2013-11-06 12:16:02 +0000468 if (!VT.isVector())
469 return MVT::i32;
470 return VT.changeVectorElementTypeToInteger();
471}
472
473bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
Stephen Lin73de7bf2013-07-09 18:16:56 +0000474 VT = VT.getScalarType();
475
476 if (!VT.isSimple())
477 return false;
478
479 switch (VT.getSimpleVT().SimpleTy) {
480 case MVT::f32:
481 case MVT::f64:
482 return true;
483 case MVT::f128:
484 return false;
485 default:
486 break;
487 }
488
489 return false;
490}
491
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000492bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
493 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
494 return Imm.isZero() || Imm.isNegZero();
495}
496
Ulrich Weigand1f6666a2015-03-31 12:52:27 +0000497bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
498 // We can use CGFI or CLGFI.
499 return isInt<32>(Imm) || isUInt<32>(Imm);
500}
501
502bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const {
503 // We can use ALGFI or SLGFI.
504 return isUInt<32>(Imm) || isUInt<32>(-Imm);
505}
506
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000507bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
508 unsigned,
509 unsigned,
510 bool *Fast) const {
Richard Sandiford46af5a22013-05-30 09:45:42 +0000511 // Unaligned accesses should never be slower than the expanded version.
512 // We check specifically for aligned accesses in the few cases where
513 // they are required.
514 if (Fast)
515 *Fast = true;
516 return true;
517}
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +0000518
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000519bool SystemZTargetLowering::isLegalAddressingMode(const DataLayout &DL,
520 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +0000521 unsigned AS) const {
Richard Sandiford791bea42013-07-31 12:58:26 +0000522 // Punt on globals for now, although they can be used in limited
523 // RELATIVE LONG cases.
524 if (AM.BaseGV)
525 return false;
526
527 // Require a 20-bit signed offset.
528 if (!isInt<20>(AM.BaseOffs))
529 return false;
530
531 // Indexing is OK but no scale factor can be applied.
532 return AM.Scale == 0 || AM.Scale == 1;
533}
534
Jonas Paulsson7a794222016-08-17 13:24:19 +0000535bool SystemZTargetLowering::isFoldableMemAccessOffset(Instruction *I,
536 int64_t Offset) const {
537 // This only applies to z13.
538 if (!Subtarget.hasVector())
539 return true;
540
541 // * Use LDE instead of LE/LEY to avoid partial register
542 // dependencies (LDE only supports small offsets).
543 // * Utilize the vector registers to hold floating point
544 // values (vector load / store instructions only support small
545 // offsets).
546
547 assert (isa<LoadInst>(I) || isa<StoreInst>(I));
548 Type *MemAccessTy = (isa<LoadInst>(I) ? I->getType() :
549 I->getOperand(0)->getType());
550 if (!isUInt<12>(Offset) &&
551 (MemAccessTy->isFloatingPointTy() || MemAccessTy->isVectorTy()))
552 return false;
553
554 return true;
555}
556
Richard Sandiford709bda62013-08-19 12:42:31 +0000557bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
558 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
559 return false;
560 unsigned FromBits = FromType->getPrimitiveSizeInBits();
561 unsigned ToBits = ToType->getPrimitiveSizeInBits();
562 return FromBits > ToBits;
563}
564
565bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
566 if (!FromVT.isInteger() || !ToVT.isInteger())
567 return false;
568 unsigned FromBits = FromVT.getSizeInBits();
569 unsigned ToBits = ToVT.getSizeInBits();
570 return FromBits > ToBits;
571}
572
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000573//===----------------------------------------------------------------------===//
574// Inline asm support
575//===----------------------------------------------------------------------===//
576
577TargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000578SystemZTargetLowering::getConstraintType(StringRef Constraint) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000579 if (Constraint.size() == 1) {
580 switch (Constraint[0]) {
581 case 'a': // Address register
582 case 'd': // Data register (equivalent to 'r')
583 case 'f': // Floating-point register
Richard Sandiford0755c932013-10-01 11:26:28 +0000584 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000585 case 'r': // General-purpose register
586 return C_RegisterClass;
587
588 case 'Q': // Memory with base and unsigned 12-bit displacement
589 case 'R': // Likewise, plus an index
590 case 'S': // Memory with base and signed 20-bit displacement
591 case 'T': // Likewise, plus an index
592 case 'm': // Equivalent to 'T'.
593 return C_Memory;
594
595 case 'I': // Unsigned 8-bit constant
596 case 'J': // Unsigned 12-bit constant
597 case 'K': // Signed 16-bit constant
598 case 'L': // Signed 20-bit displacement (on all targets we support)
599 case 'M': // 0x7fffffff
600 return C_Other;
601
602 default:
603 break;
604 }
605 }
606 return TargetLowering::getConstraintType(Constraint);
607}
608
609TargetLowering::ConstraintWeight SystemZTargetLowering::
610getSingleConstraintMatchWeight(AsmOperandInfo &info,
611 const char *constraint) const {
612 ConstraintWeight weight = CW_Invalid;
613 Value *CallOperandVal = info.CallOperandVal;
614 // If we don't have a value, we can't do a match,
615 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +0000616 if (!CallOperandVal)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000617 return CW_Default;
618 Type *type = CallOperandVal->getType();
619 // Look at the constraint type.
620 switch (*constraint) {
621 default:
622 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
623 break;
624
625 case 'a': // Address register
626 case 'd': // Data register (equivalent to 'r')
Richard Sandiford0755c932013-10-01 11:26:28 +0000627 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000628 case 'r': // General-purpose register
629 if (CallOperandVal->getType()->isIntegerTy())
630 weight = CW_Register;
631 break;
632
633 case 'f': // Floating-point register
634 if (type->isFloatingPointTy())
635 weight = CW_Register;
636 break;
637
638 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000639 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000640 if (isUInt<8>(C->getZExtValue()))
641 weight = CW_Constant;
642 break;
643
644 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000645 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000646 if (isUInt<12>(C->getZExtValue()))
647 weight = CW_Constant;
648 break;
649
650 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000651 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000652 if (isInt<16>(C->getSExtValue()))
653 weight = CW_Constant;
654 break;
655
656 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000657 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000658 if (isInt<20>(C->getSExtValue()))
659 weight = CW_Constant;
660 break;
661
662 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000663 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000664 if (C->getZExtValue() == 0x7fffffff)
665 weight = CW_Constant;
666 break;
667 }
668 return weight;
669}
670
Richard Sandifordb8204052013-07-12 09:08:12 +0000671// Parse a "{tNNN}" register constraint for which the register type "t"
672// has already been verified. MC is the class associated with "t" and
673// Map maps 0-based register numbers to LLVM register numbers.
674static std::pair<unsigned, const TargetRegisterClass *>
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000675parseRegisterNumber(StringRef Constraint, const TargetRegisterClass *RC,
676 const unsigned *Map) {
Richard Sandifordb8204052013-07-12 09:08:12 +0000677 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
678 if (isdigit(Constraint[2])) {
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000679 unsigned Index;
680 bool Failed =
681 Constraint.slice(2, Constraint.size() - 1).getAsInteger(10, Index);
682 if (!Failed && Index < 16 && Map[Index])
Richard Sandifordb8204052013-07-12 09:08:12 +0000683 return std::make_pair(Map[Index], RC);
684 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000685 return std::make_pair(0U, nullptr);
Richard Sandifordb8204052013-07-12 09:08:12 +0000686}
687
Eric Christopher11e4df72015-02-26 22:38:43 +0000688std::pair<unsigned, const TargetRegisterClass *>
689SystemZTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000690 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000691 if (Constraint.size() == 1) {
692 // GCC Constraint Letters
693 switch (Constraint[0]) {
694 default: break;
695 case 'd': // Data register (equivalent to 'r')
696 case 'r': // General-purpose register
697 if (VT == MVT::i64)
698 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
699 else if (VT == MVT::i128)
700 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
701 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
702
703 case 'a': // Address register
704 if (VT == MVT::i64)
705 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
706 else if (VT == MVT::i128)
707 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
708 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
709
Richard Sandiford0755c932013-10-01 11:26:28 +0000710 case 'h': // High-part register (an LLVM extension)
711 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
712
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000713 case 'f': // Floating-point register
714 if (VT == MVT::f64)
715 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
716 else if (VT == MVT::f128)
717 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
718 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
719 }
720 }
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000721 if (Constraint.size() > 0 && Constraint[0] == '{') {
Richard Sandifordb8204052013-07-12 09:08:12 +0000722 // We need to override the default register parsing for GPRs and FPRs
723 // because the interpretation depends on VT. The internal names of
724 // the registers are also different from the external names
725 // (F0D and F0S instead of F0, etc.).
726 if (Constraint[1] == 'r') {
727 if (VT == MVT::i32)
728 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
729 SystemZMC::GR32Regs);
730 if (VT == MVT::i128)
731 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
732 SystemZMC::GR128Regs);
733 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
734 SystemZMC::GR64Regs);
735 }
736 if (Constraint[1] == 'f') {
737 if (VT == MVT::f32)
738 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
739 SystemZMC::FP32Regs);
740 if (VT == MVT::f128)
741 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
742 SystemZMC::FP128Regs);
743 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
744 SystemZMC::FP64Regs);
745 }
746 }
Eric Christopher11e4df72015-02-26 22:38:43 +0000747 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000748}
749
750void SystemZTargetLowering::
751LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
752 std::vector<SDValue> &Ops,
753 SelectionDAG &DAG) const {
754 // Only support length 1 constraints for now.
755 if (Constraint.length() == 1) {
756 switch (Constraint[0]) {
757 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000758 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000759 if (isUInt<8>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000760 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000761 Op.getValueType()));
762 return;
763
764 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000765 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000766 if (isUInt<12>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000767 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000768 Op.getValueType()));
769 return;
770
771 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000772 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000773 if (isInt<16>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000774 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000775 Op.getValueType()));
776 return;
777
778 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000779 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000780 if (isInt<20>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000781 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000782 Op.getValueType()));
783 return;
784
785 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000786 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000787 if (C->getZExtValue() == 0x7fffffff)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000788 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000789 Op.getValueType()));
790 return;
791 }
792 }
793 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
794}
795
796//===----------------------------------------------------------------------===//
797// Calling conventions
798//===----------------------------------------------------------------------===//
799
800#include "SystemZGenCallingConv.inc"
801
Richard Sandiford709bda62013-08-19 12:42:31 +0000802bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
803 Type *ToType) const {
804 return isTruncateFree(FromType, ToType);
805}
806
807bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Ulrich Weigand19d24d22015-11-13 13:00:27 +0000808 return CI->isTailCall();
Richard Sandiford709bda62013-08-19 12:42:31 +0000809}
810
Ulrich Weigand5211f9f2015-05-05 19:30:05 +0000811// We do not yet support 128-bit single-element vector types. If the user
812// attempts to use such types as function argument or return type, prefer
813// to error out instead of emitting code violating the ABI.
814static void VerifyVectorType(MVT VT, EVT ArgVT) {
815 if (ArgVT.isVector() && !VT.isVector())
816 report_fatal_error("Unsupported vector argument or return type");
817}
818
819static void VerifyVectorTypes(const SmallVectorImpl<ISD::InputArg> &Ins) {
820 for (unsigned i = 0; i < Ins.size(); ++i)
821 VerifyVectorType(Ins[i].VT, Ins[i].ArgVT);
822}
823
824static void VerifyVectorTypes(const SmallVectorImpl<ISD::OutputArg> &Outs) {
825 for (unsigned i = 0; i < Outs.size(); ++i)
826 VerifyVectorType(Outs[i].VT, Outs[i].ArgVT);
827}
828
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000829// Value is a value that has been passed to us in the location described by VA
830// (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
831// any loads onto Chain.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000832static SDValue convertLocVTToValVT(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000833 CCValAssign &VA, SDValue Chain,
834 SDValue Value) {
835 // If the argument has been promoted from a smaller type, insert an
836 // assertion to capture this.
837 if (VA.getLocInfo() == CCValAssign::SExt)
838 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
839 DAG.getValueType(VA.getValVT()));
840 else if (VA.getLocInfo() == CCValAssign::ZExt)
841 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
842 DAG.getValueType(VA.getValVT()));
843
844 if (VA.isExtInLoc())
845 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000846 else if (VA.getLocInfo() == CCValAssign::BCvt) {
847 // If this is a short vector argument loaded from the stack,
848 // extend from i64 to full vector size and then bitcast.
849 assert(VA.getLocVT() == MVT::i64);
850 assert(VA.getValVT().isVector());
Ahmed Bougacha128f8732016-04-26 21:15:30 +0000851 Value = DAG.getBuildVector(MVT::v2i64, DL, {Value, DAG.getUNDEF(MVT::i64)});
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000852 Value = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Value);
853 } else
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000854 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
855 return Value;
856}
857
858// Value is a value of type VA.getValVT() that we need to copy into
859// the location described by VA. Return a copy of Value converted to
860// VA.getValVT(). The caller is responsible for handling indirect values.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000861static SDValue convertValVTToLocVT(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000862 CCValAssign &VA, SDValue Value) {
863 switch (VA.getLocInfo()) {
864 case CCValAssign::SExt:
865 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
866 case CCValAssign::ZExt:
867 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
868 case CCValAssign::AExt:
869 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000870 case CCValAssign::BCvt:
871 // If this is a short vector argument to be stored to the stack,
872 // bitcast to v2i64 and then extract first element.
873 assert(VA.getLocVT() == MVT::i64);
874 assert(VA.getValVT().isVector());
875 Value = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Value);
876 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VA.getLocVT(), Value,
877 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000878 case CCValAssign::Full:
879 return Value;
880 default:
881 llvm_unreachable("Unhandled getLocInfo()");
882 }
883}
884
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000885SDValue SystemZTargetLowering::LowerFormalArguments(
886 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
887 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
888 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000889 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000890 MachineFrameInfo &MFI = MF.getFrameInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000891 MachineRegisterInfo &MRI = MF.getRegInfo();
892 SystemZMachineFunctionInfo *FuncInfo =
Eric Christophera6734172015-01-31 00:06:45 +0000893 MF.getInfo<SystemZMachineFunctionInfo>();
894 auto *TFL =
895 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering());
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +0000896 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000897
Ulrich Weigand5211f9f2015-05-05 19:30:05 +0000898 // Detect unsupported vector argument types.
899 if (Subtarget.hasVector())
900 VerifyVectorTypes(Ins);
901
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000902 // Assign locations to all of the incoming arguments.
903 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000904 SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000905 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
906
907 unsigned NumFixedGPRs = 0;
908 unsigned NumFixedFPRs = 0;
909 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
910 SDValue ArgValue;
911 CCValAssign &VA = ArgLocs[I];
912 EVT LocVT = VA.getLocVT();
913 if (VA.isRegLoc()) {
914 // Arguments passed in registers
915 const TargetRegisterClass *RC;
916 switch (LocVT.getSimpleVT().SimpleTy) {
917 default:
918 // Integers smaller than i64 should be promoted to i64.
919 llvm_unreachable("Unexpected argument type");
920 case MVT::i32:
921 NumFixedGPRs += 1;
922 RC = &SystemZ::GR32BitRegClass;
923 break;
924 case MVT::i64:
925 NumFixedGPRs += 1;
926 RC = &SystemZ::GR64BitRegClass;
927 break;
928 case MVT::f32:
929 NumFixedFPRs += 1;
930 RC = &SystemZ::FP32BitRegClass;
931 break;
932 case MVT::f64:
933 NumFixedFPRs += 1;
934 RC = &SystemZ::FP64BitRegClass;
935 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000936 case MVT::v16i8:
937 case MVT::v8i16:
938 case MVT::v4i32:
939 case MVT::v2i64:
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000940 case MVT::v4f32:
Ulrich Weigandcd808232015-05-05 19:26:48 +0000941 case MVT::v2f64:
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000942 RC = &SystemZ::VR128BitRegClass;
943 break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000944 }
945
946 unsigned VReg = MRI.createVirtualRegister(RC);
947 MRI.addLiveIn(VA.getLocReg(), VReg);
948 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
949 } else {
950 assert(VA.isMemLoc() && "Argument not register or memory");
951
952 // Create the frame index object for this incoming parameter.
Matthias Braun941a7052016-07-28 18:40:00 +0000953 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
954 VA.getLocMemOffset(), true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000955
956 // Create the SelectionDAG nodes corresponding to a load
957 // from this parameter. Unpromoted ints and floats are
958 // passed as right-justified 8-byte values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000959 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
960 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000961 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
962 DAG.getIntPtrConstant(4, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000963 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +0000964 MachinePointerInfo::getFixedStack(MF, FI));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000965 }
966
967 // Convert the value of the argument register into the value that's
968 // being passed.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +0000969 if (VA.getLocInfo() == CCValAssign::Indirect) {
Justin Lebar9c375812016-07-15 18:27:10 +0000970 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
971 MachinePointerInfo()));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +0000972 // If the original argument was split (e.g. i128), we need
973 // to load all parts of it here (using the same address).
974 unsigned ArgIndex = Ins[I].OrigArgIndex;
975 assert (Ins[I].PartOffset == 0);
976 while (I + 1 != E && Ins[I + 1].OrigArgIndex == ArgIndex) {
977 CCValAssign &PartVA = ArgLocs[I + 1];
978 unsigned PartOffset = Ins[I + 1].PartOffset;
979 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
980 DAG.getIntPtrConstant(PartOffset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +0000981 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
982 MachinePointerInfo()));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +0000983 ++I;
984 }
985 } else
986 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000987 }
988
989 if (IsVarArg) {
990 // Save the number of non-varargs registers for later use by va_start, etc.
991 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
992 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
993
994 // Likewise the address (in the form of a frame index) of where the
995 // first stack vararg would be. The 1-byte size here is arbitrary.
996 int64_t StackSize = CCInfo.getNextStackOffset();
Matthias Braun941a7052016-07-28 18:40:00 +0000997 FuncInfo->setVarArgsFrameIndex(MFI.CreateFixedObject(1, StackSize, true));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000998
999 // ...and a similar frame index for the caller-allocated save area
1000 // that will be used to store the incoming registers.
1001 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
Matthias Braun941a7052016-07-28 18:40:00 +00001002 unsigned RegSaveIndex = MFI.CreateFixedObject(1, RegSaveOffset, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001003 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
1004
1005 // Store the FPR varargs in the reserved frame slots. (We store the
1006 // GPRs as part of the prologue.)
1007 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
1008 SDValue MemOps[SystemZ::NumArgFPRs];
1009 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
1010 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
Matthias Braun941a7052016-07-28 18:40:00 +00001011 int FI = MFI.CreateFixedObject(8, RegSaveOffset + Offset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +00001012 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001013 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
1014 &SystemZ::FP64BitRegClass);
1015 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
1016 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00001017 MachinePointerInfo::getFixedStack(MF, FI));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001018 }
1019 // Join the stores, which are independent of one another.
1020 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00001021 makeArrayRef(&MemOps[NumFixedFPRs],
1022 SystemZ::NumArgFPRs-NumFixedFPRs));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001023 }
1024 }
1025
1026 return Chain;
1027}
1028
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +00001029static bool canUseSiblingCall(const CCState &ArgCCInfo,
Bryan Chan893110e2016-04-28 00:17:23 +00001030 SmallVectorImpl<CCValAssign> &ArgLocs,
1031 SmallVectorImpl<ISD::OutputArg> &Outs) {
Richard Sandiford709bda62013-08-19 12:42:31 +00001032 // Punt if there are any indirect or stack arguments, or if the call
Bryan Chan893110e2016-04-28 00:17:23 +00001033 // needs the callee-saved argument register R6, or if the call uses
1034 // the callee-saved register arguments SwiftSelf and SwiftError.
Richard Sandiford709bda62013-08-19 12:42:31 +00001035 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1036 CCValAssign &VA = ArgLocs[I];
1037 if (VA.getLocInfo() == CCValAssign::Indirect)
1038 return false;
1039 if (!VA.isRegLoc())
1040 return false;
1041 unsigned Reg = VA.getLocReg();
Richard Sandiford0755c932013-10-01 11:26:28 +00001042 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
Richard Sandiford709bda62013-08-19 12:42:31 +00001043 return false;
Bryan Chan893110e2016-04-28 00:17:23 +00001044 if (Outs[I].Flags.isSwiftSelf() || Outs[I].Flags.isSwiftError())
1045 return false;
Richard Sandiford709bda62013-08-19 12:42:31 +00001046 }
1047 return true;
1048}
1049
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001050SDValue
1051SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
1052 SmallVectorImpl<SDValue> &InVals) const {
1053 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001054 SDLoc &DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001055 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1056 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1057 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001058 SDValue Chain = CLI.Chain;
1059 SDValue Callee = CLI.Callee;
Richard Sandiford709bda62013-08-19 12:42:31 +00001060 bool &IsTailCall = CLI.IsTailCall;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001061 CallingConv::ID CallConv = CLI.CallConv;
1062 bool IsVarArg = CLI.IsVarArg;
1063 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +00001064 EVT PtrVT = getPointerTy(MF.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001065
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001066 // Detect unsupported vector argument and return types.
1067 if (Subtarget.hasVector()) {
1068 VerifyVectorTypes(Outs);
1069 VerifyVectorTypes(Ins);
1070 }
1071
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001072 // Analyze the operands of the call, assigning locations to each operand.
1073 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001074 SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001075 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
1076
Richard Sandiford709bda62013-08-19 12:42:31 +00001077 // We don't support GuaranteedTailCallOpt, only automatically-detected
1078 // sibling calls.
Bryan Chan893110e2016-04-28 00:17:23 +00001079 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs, Outs))
Richard Sandiford709bda62013-08-19 12:42:31 +00001080 IsTailCall = false;
1081
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001082 // Get a count of how many bytes are to be pushed on the stack.
1083 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1084
1085 // Mark the start of the call.
Richard Sandiford709bda62013-08-19 12:42:31 +00001086 if (!IsTailCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001087 Chain = DAG.getCALLSEQ_START(Chain,
1088 DAG.getConstant(NumBytes, DL, PtrVT, true),
Richard Sandiford709bda62013-08-19 12:42:31 +00001089 DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001090
1091 // Copy argument values to their designated locations.
1092 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
1093 SmallVector<SDValue, 8> MemOpChains;
1094 SDValue StackPtr;
1095 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1096 CCValAssign &VA = ArgLocs[I];
1097 SDValue ArgValue = OutVals[I];
1098
1099 if (VA.getLocInfo() == CCValAssign::Indirect) {
1100 // Store the argument in a stack slot and pass its address.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001101 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[I].ArgVT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001102 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Justin Lebar9c375812016-07-15 18:27:10 +00001103 MemOpChains.push_back(
1104 DAG.getStore(Chain, DL, ArgValue, SpillSlot,
1105 MachinePointerInfo::getFixedStack(MF, FI)));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001106 // If the original argument was split (e.g. i128), we need
1107 // to store all parts of it here (and pass just one address).
1108 unsigned ArgIndex = Outs[I].OrigArgIndex;
1109 assert (Outs[I].PartOffset == 0);
1110 while (I + 1 != E && Outs[I + 1].OrigArgIndex == ArgIndex) {
1111 SDValue PartValue = OutVals[I + 1];
1112 unsigned PartOffset = Outs[I + 1].PartOffset;
1113 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
1114 DAG.getIntPtrConstant(PartOffset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00001115 MemOpChains.push_back(
1116 DAG.getStore(Chain, DL, PartValue, Address,
1117 MachinePointerInfo::getFixedStack(MF, FI)));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001118 ++I;
1119 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001120 ArgValue = SpillSlot;
1121 } else
1122 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
1123
1124 if (VA.isRegLoc())
1125 // Queue up the argument copies and emit them at the end.
1126 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1127 else {
1128 assert(VA.isMemLoc() && "Argument not register or memory");
1129
1130 // Work out the address of the stack slot. Unpromoted ints and
1131 // floats are passed as right-justified 8-byte values.
1132 if (!StackPtr.getNode())
1133 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
1134 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
1135 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
1136 Offset += 4;
1137 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001138 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001139
1140 // Emit the store.
Justin Lebar9c375812016-07-15 18:27:10 +00001141 MemOpChains.push_back(
1142 DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001143 }
1144 }
1145
1146 // Join the stores, which are independent of one another.
1147 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001148 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001149
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001150 // Accept direct calls by converting symbolic call addresses to the
Richard Sandiford709bda62013-08-19 12:42:31 +00001151 // associated Target* opcodes. Force %r1 to be used for indirect
1152 // tail calls.
1153 SDValue Glue;
Richard Sandiford21f5d682014-03-06 11:22:58 +00001154 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001155 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
1156 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford21f5d682014-03-06 11:22:58 +00001157 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001158 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
1159 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford709bda62013-08-19 12:42:31 +00001160 } else if (IsTailCall) {
1161 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
1162 Glue = Chain.getValue(1);
1163 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
1164 }
1165
1166 // Build a sequence of copy-to-reg nodes, chained and glued together.
1167 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
1168 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
1169 RegsToPass[I].second, Glue);
1170 Glue = Chain.getValue(1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001171 }
1172
1173 // The first call operand is the chain and the second is the target address.
1174 SmallVector<SDValue, 8> Ops;
1175 Ops.push_back(Chain);
1176 Ops.push_back(Callee);
1177
1178 // Add argument registers to the end of the list so that they are
1179 // known live into the call.
1180 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
1181 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
1182 RegsToPass[I].second.getValueType()));
1183
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001184 // Add a register mask operand representing the call-preserved registers.
Eric Christophera6734172015-01-31 00:06:45 +00001185 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00001186 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001187 assert(Mask && "Missing call preserved mask for calling convention");
1188 Ops.push_back(DAG.getRegisterMask(Mask));
1189
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001190 // Glue the call to the argument copies, if any.
1191 if (Glue.getNode())
1192 Ops.push_back(Glue);
1193
1194 // Emit the call.
1195 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Richard Sandiford709bda62013-08-19 12:42:31 +00001196 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001197 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
1198 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001199 Glue = Chain.getValue(1);
1200
1201 // Mark the end of the call, which is glued to the call itself.
1202 Chain = DAG.getCALLSEQ_END(Chain,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001203 DAG.getConstant(NumBytes, DL, PtrVT, true),
1204 DAG.getConstant(0, DL, PtrVT, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001205 Glue, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001206 Glue = Chain.getValue(1);
1207
1208 // Assign locations to each value returned by this call.
1209 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001210 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001211 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
1212
1213 // Copy all of the result registers out of their specified physreg.
1214 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1215 CCValAssign &VA = RetLocs[I];
1216
1217 // Copy the value out, gluing the copy to the end of the call sequence.
1218 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
1219 VA.getLocVT(), Glue);
1220 Chain = RetValue.getValue(1);
1221 Glue = RetValue.getValue(2);
1222
1223 // Convert the value of the return register into the value that's
1224 // being returned.
1225 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
1226 }
1227
1228 return Chain;
1229}
1230
Ulrich Weiganda887f062015-08-13 13:37:06 +00001231bool SystemZTargetLowering::
1232CanLowerReturn(CallingConv::ID CallConv,
1233 MachineFunction &MF, bool isVarArg,
1234 const SmallVectorImpl<ISD::OutputArg> &Outs,
1235 LLVMContext &Context) const {
1236 // Detect unsupported vector return types.
1237 if (Subtarget.hasVector())
1238 VerifyVectorTypes(Outs);
1239
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001240 // Special case that we cannot easily detect in RetCC_SystemZ since
1241 // i128 is not a legal type.
1242 for (auto &Out : Outs)
1243 if (Out.ArgVT == MVT::i128)
1244 return false;
1245
Ulrich Weiganda887f062015-08-13 13:37:06 +00001246 SmallVector<CCValAssign, 16> RetLocs;
1247 CCState RetCCInfo(CallConv, isVarArg, MF, RetLocs, Context);
1248 return RetCCInfo.CheckReturn(Outs, RetCC_SystemZ);
1249}
1250
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001251SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001252SystemZTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1253 bool IsVarArg,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001254 const SmallVectorImpl<ISD::OutputArg> &Outs,
1255 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001256 const SDLoc &DL, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001257 MachineFunction &MF = DAG.getMachineFunction();
1258
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001259 // Detect unsupported vector return types.
1260 if (Subtarget.hasVector())
1261 VerifyVectorTypes(Outs);
1262
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001263 // Assign locations to each returned value.
1264 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001265 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001266 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
1267
1268 // Quick exit for void returns
1269 if (RetLocs.empty())
1270 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
1271
1272 // Copy the result values into the output registers.
1273 SDValue Glue;
1274 SmallVector<SDValue, 4> RetOps;
1275 RetOps.push_back(Chain);
1276 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1277 CCValAssign &VA = RetLocs[I];
1278 SDValue RetValue = OutVals[I];
1279
1280 // Make the return register live on exit.
1281 assert(VA.isRegLoc() && "Can only return in registers!");
1282
1283 // Promote the value as required.
1284 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
1285
1286 // Chain and glue the copies together.
1287 unsigned Reg = VA.getLocReg();
1288 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
1289 Glue = Chain.getValue(1);
1290 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
1291 }
1292
1293 // Update chain and glue.
1294 RetOps[0] = Chain;
1295 if (Glue.getNode())
1296 RetOps.push_back(Glue);
1297
Craig Topper48d114b2014-04-26 18:35:24 +00001298 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001299}
1300
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001301SDValue SystemZTargetLowering::prepareVolatileOrAtomicLoad(
1302 SDValue Chain, const SDLoc &DL, SelectionDAG &DAG) const {
Richard Sandiford9afe6132013-12-10 10:36:34 +00001303 return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain);
1304}
1305
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001306// Return true if Op is an intrinsic node with chain that returns the CC value
1307// as its only (other) argument. Provide the associated SystemZISD opcode and
1308// the mask of valid CC values if so.
1309static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode,
1310 unsigned &CCValid) {
1311 unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1312 switch (Id) {
1313 case Intrinsic::s390_tbegin:
1314 Opcode = SystemZISD::TBEGIN;
1315 CCValid = SystemZ::CCMASK_TBEGIN;
1316 return true;
1317
1318 case Intrinsic::s390_tbegin_nofloat:
1319 Opcode = SystemZISD::TBEGIN_NOFLOAT;
1320 CCValid = SystemZ::CCMASK_TBEGIN;
1321 return true;
1322
1323 case Intrinsic::s390_tend:
1324 Opcode = SystemZISD::TEND;
1325 CCValid = SystemZ::CCMASK_TEND;
1326 return true;
1327
1328 default:
1329 return false;
1330 }
1331}
1332
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001333// Return true if Op is an intrinsic node without chain that returns the
1334// CC value as its final argument. Provide the associated SystemZISD
1335// opcode and the mask of valid CC values if so.
1336static bool isIntrinsicWithCC(SDValue Op, unsigned &Opcode, unsigned &CCValid) {
1337 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1338 switch (Id) {
1339 case Intrinsic::s390_vpkshs:
1340 case Intrinsic::s390_vpksfs:
1341 case Intrinsic::s390_vpksgs:
1342 Opcode = SystemZISD::PACKS_CC;
1343 CCValid = SystemZ::CCMASK_VCMP;
1344 return true;
1345
1346 case Intrinsic::s390_vpklshs:
1347 case Intrinsic::s390_vpklsfs:
1348 case Intrinsic::s390_vpklsgs:
1349 Opcode = SystemZISD::PACKLS_CC;
1350 CCValid = SystemZ::CCMASK_VCMP;
1351 return true;
1352
1353 case Intrinsic::s390_vceqbs:
1354 case Intrinsic::s390_vceqhs:
1355 case Intrinsic::s390_vceqfs:
1356 case Intrinsic::s390_vceqgs:
1357 Opcode = SystemZISD::VICMPES;
1358 CCValid = SystemZ::CCMASK_VCMP;
1359 return true;
1360
1361 case Intrinsic::s390_vchbs:
1362 case Intrinsic::s390_vchhs:
1363 case Intrinsic::s390_vchfs:
1364 case Intrinsic::s390_vchgs:
1365 Opcode = SystemZISD::VICMPHS;
1366 CCValid = SystemZ::CCMASK_VCMP;
1367 return true;
1368
1369 case Intrinsic::s390_vchlbs:
1370 case Intrinsic::s390_vchlhs:
1371 case Intrinsic::s390_vchlfs:
1372 case Intrinsic::s390_vchlgs:
1373 Opcode = SystemZISD::VICMPHLS;
1374 CCValid = SystemZ::CCMASK_VCMP;
1375 return true;
1376
1377 case Intrinsic::s390_vtm:
1378 Opcode = SystemZISD::VTM;
1379 CCValid = SystemZ::CCMASK_VCMP;
1380 return true;
1381
1382 case Intrinsic::s390_vfaebs:
1383 case Intrinsic::s390_vfaehs:
1384 case Intrinsic::s390_vfaefs:
1385 Opcode = SystemZISD::VFAE_CC;
1386 CCValid = SystemZ::CCMASK_ANY;
1387 return true;
1388
1389 case Intrinsic::s390_vfaezbs:
1390 case Intrinsic::s390_vfaezhs:
1391 case Intrinsic::s390_vfaezfs:
1392 Opcode = SystemZISD::VFAEZ_CC;
1393 CCValid = SystemZ::CCMASK_ANY;
1394 return true;
1395
1396 case Intrinsic::s390_vfeebs:
1397 case Intrinsic::s390_vfeehs:
1398 case Intrinsic::s390_vfeefs:
1399 Opcode = SystemZISD::VFEE_CC;
1400 CCValid = SystemZ::CCMASK_ANY;
1401 return true;
1402
1403 case Intrinsic::s390_vfeezbs:
1404 case Intrinsic::s390_vfeezhs:
1405 case Intrinsic::s390_vfeezfs:
1406 Opcode = SystemZISD::VFEEZ_CC;
1407 CCValid = SystemZ::CCMASK_ANY;
1408 return true;
1409
1410 case Intrinsic::s390_vfenebs:
1411 case Intrinsic::s390_vfenehs:
1412 case Intrinsic::s390_vfenefs:
1413 Opcode = SystemZISD::VFENE_CC;
1414 CCValid = SystemZ::CCMASK_ANY;
1415 return true;
1416
1417 case Intrinsic::s390_vfenezbs:
1418 case Intrinsic::s390_vfenezhs:
1419 case Intrinsic::s390_vfenezfs:
1420 Opcode = SystemZISD::VFENEZ_CC;
1421 CCValid = SystemZ::CCMASK_ANY;
1422 return true;
1423
1424 case Intrinsic::s390_vistrbs:
1425 case Intrinsic::s390_vistrhs:
1426 case Intrinsic::s390_vistrfs:
1427 Opcode = SystemZISD::VISTR_CC;
1428 CCValid = SystemZ::CCMASK_0 | SystemZ::CCMASK_3;
1429 return true;
1430
1431 case Intrinsic::s390_vstrcbs:
1432 case Intrinsic::s390_vstrchs:
1433 case Intrinsic::s390_vstrcfs:
1434 Opcode = SystemZISD::VSTRC_CC;
1435 CCValid = SystemZ::CCMASK_ANY;
1436 return true;
1437
1438 case Intrinsic::s390_vstrczbs:
1439 case Intrinsic::s390_vstrczhs:
1440 case Intrinsic::s390_vstrczfs:
1441 Opcode = SystemZISD::VSTRCZ_CC;
1442 CCValid = SystemZ::CCMASK_ANY;
1443 return true;
1444
1445 case Intrinsic::s390_vfcedbs:
1446 Opcode = SystemZISD::VFCMPES;
1447 CCValid = SystemZ::CCMASK_VCMP;
1448 return true;
1449
1450 case Intrinsic::s390_vfchdbs:
1451 Opcode = SystemZISD::VFCMPHS;
1452 CCValid = SystemZ::CCMASK_VCMP;
1453 return true;
1454
1455 case Intrinsic::s390_vfchedbs:
1456 Opcode = SystemZISD::VFCMPHES;
1457 CCValid = SystemZ::CCMASK_VCMP;
1458 return true;
1459
1460 case Intrinsic::s390_vftcidb:
1461 Opcode = SystemZISD::VFTCI;
1462 CCValid = SystemZ::CCMASK_VCMP;
1463 return true;
1464
Marcin Koscielnickicf7cc722016-07-10 14:41:22 +00001465 case Intrinsic::s390_tdc:
1466 Opcode = SystemZISD::TDC;
1467 CCValid = SystemZ::CCMASK_TDC;
1468 return true;
1469
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001470 default:
1471 return false;
1472 }
1473}
1474
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001475// Emit an intrinsic with chain with a glued value instead of its CC result.
1476static SDValue emitIntrinsicWithChainAndGlue(SelectionDAG &DAG, SDValue Op,
1477 unsigned Opcode) {
1478 // Copy all operands except the intrinsic ID.
1479 unsigned NumOps = Op.getNumOperands();
1480 SmallVector<SDValue, 6> Ops;
1481 Ops.reserve(NumOps - 1);
1482 Ops.push_back(Op.getOperand(0));
1483 for (unsigned I = 2; I < NumOps; ++I)
1484 Ops.push_back(Op.getOperand(I));
1485
1486 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
1487 SDVTList RawVTs = DAG.getVTList(MVT::Other, MVT::Glue);
1488 SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1489 SDValue OldChain = SDValue(Op.getNode(), 1);
1490 SDValue NewChain = SDValue(Intr.getNode(), 0);
1491 DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain);
1492 return Intr;
1493}
1494
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001495// Emit an intrinsic with a glued value instead of its CC result.
1496static SDValue emitIntrinsicWithGlue(SelectionDAG &DAG, SDValue Op,
1497 unsigned Opcode) {
1498 // Copy all operands except the intrinsic ID.
1499 unsigned NumOps = Op.getNumOperands();
1500 SmallVector<SDValue, 6> Ops;
1501 Ops.reserve(NumOps - 1);
1502 for (unsigned I = 1; I < NumOps; ++I)
1503 Ops.push_back(Op.getOperand(I));
1504
1505 if (Op->getNumValues() == 1)
1506 return DAG.getNode(Opcode, SDLoc(Op), MVT::Glue, Ops);
1507 assert(Op->getNumValues() == 2 && "Expected exactly one non-CC result");
1508 SDVTList RawVTs = DAG.getVTList(Op->getValueType(0), MVT::Glue);
1509 return DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1510}
1511
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001512// CC is a comparison that will be implemented using an integer or
1513// floating-point comparison. Return the condition code mask for
1514// a branch on true. In the integer case, CCMASK_CMP_UO is set for
1515// unsigned comparisons and clear for signed ones. In the floating-point
1516// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1517static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1518#define CONV(X) \
1519 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1520 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1521 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1522
1523 switch (CC) {
1524 default:
1525 llvm_unreachable("Invalid integer condition!");
1526
1527 CONV(EQ);
1528 CONV(NE);
1529 CONV(GT);
1530 CONV(GE);
1531 CONV(LT);
1532 CONV(LE);
1533
1534 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1535 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1536 }
1537#undef CONV
1538}
1539
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001540// Return a sequence for getting a 1 from an IPM result when CC has a
1541// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1542// The handling of CC values outside CCValid doesn't matter.
1543static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1544 // Deal with cases where the result can be taken directly from a bit
1545 // of the IPM result.
1546 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1547 return IPMConversion(0, 0, SystemZ::IPM_CC);
1548 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1549 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1550
1551 // Deal with cases where we can add a value to force the sign bit
1552 // to contain the right value. Putting the bit in 31 means we can
1553 // use SRL rather than RISBG(L), and also makes it easier to get a
1554 // 0/-1 value, so it has priority over the other tests below.
1555 //
1556 // These sequences rely on the fact that the upper two bits of the
1557 // IPM result are zero.
1558 uint64_t TopBit = uint64_t(1) << 31;
1559 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1560 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1561 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1562 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1563 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1564 | SystemZ::CCMASK_1
1565 | SystemZ::CCMASK_2)))
1566 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1567 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1568 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1569 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1570 | SystemZ::CCMASK_2
1571 | SystemZ::CCMASK_3)))
1572 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1573
1574 // Next try inverting the value and testing a bit. 0/1 could be
1575 // handled this way too, but we dealt with that case above.
1576 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1577 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1578
1579 // Handle cases where adding a value forces a non-sign bit to contain
1580 // the right value.
1581 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1582 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1583 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1584 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1585
Alp Tokercb402912014-01-24 17:20:08 +00001586 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001587 // can be done by inverting the low CC bit and applying one of the
1588 // sign-based extractions above.
1589 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1590 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1591 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1592 return IPMConversion(1 << SystemZ::IPM_CC,
1593 TopBit - (3 << SystemZ::IPM_CC), 31);
1594 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1595 | SystemZ::CCMASK_1
1596 | SystemZ::CCMASK_3)))
1597 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1598 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1599 | SystemZ::CCMASK_2
1600 | SystemZ::CCMASK_3)))
1601 return IPMConversion(1 << SystemZ::IPM_CC,
1602 TopBit - (1 << SystemZ::IPM_CC), 31);
1603
1604 llvm_unreachable("Unexpected CC combination");
1605}
1606
Richard Sandifordd420f732013-12-13 15:28:45 +00001607// If C can be converted to a comparison against zero, adjust the operands
Richard Sandiforda0757082013-08-01 10:29:45 +00001608// as necessary.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001609static void adjustZeroCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001610 if (C.ICmpType == SystemZICMP::UnsignedOnly)
Richard Sandiforda0757082013-08-01 10:29:45 +00001611 return;
1612
Richard Sandiford21f5d682014-03-06 11:22:58 +00001613 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
Richard Sandiforda0757082013-08-01 10:29:45 +00001614 if (!ConstOp1)
1615 return;
1616
1617 int64_t Value = ConstOp1->getSExtValue();
Richard Sandifordd420f732013-12-13 15:28:45 +00001618 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1619 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1620 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1621 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1622 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001623 C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType());
Richard Sandiforda0757082013-08-01 10:29:45 +00001624 }
1625}
1626
Richard Sandifordd420f732013-12-13 15:28:45 +00001627// If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1628// adjust the operands as necessary.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001629static void adjustSubwordCmp(SelectionDAG &DAG, const SDLoc &DL,
1630 Comparison &C) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001631 // For us to make any changes, it must a comparison between a single-use
1632 // load and a constant.
Richard Sandifordd420f732013-12-13 15:28:45 +00001633 if (!C.Op0.hasOneUse() ||
1634 C.Op0.getOpcode() != ISD::LOAD ||
1635 C.Op1.getOpcode() != ISD::Constant)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001636 return;
1637
1638 // We must have an 8- or 16-bit load.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001639 auto *Load = cast<LoadSDNode>(C.Op0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001640 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1641 if (NumBits != 8 && NumBits != 16)
1642 return;
1643
1644 // The load must be an extending one and the constant must be within the
1645 // range of the unextended value.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001646 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001647 uint64_t Value = ConstOp1->getZExtValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001648 uint64_t Mask = (1 << NumBits) - 1;
1649 if (Load->getExtensionType() == ISD::SEXTLOAD) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001650 // Make sure that ConstOp1 is in range of C.Op0.
1651 int64_t SignedValue = ConstOp1->getSExtValue();
1652 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001653 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001654 if (C.ICmpType != SystemZICMP::SignedOnly) {
1655 // Unsigned comparison between two sign-extended values is equivalent
1656 // to unsigned comparison between two zero-extended values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001657 Value &= Mask;
Richard Sandifordd420f732013-12-13 15:28:45 +00001658 } else if (NumBits == 8) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001659 // Try to treat the comparison as unsigned, so that we can use CLI.
1660 // Adjust CCMask and Value as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001661 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001662 // Test whether the high bit of the byte is set.
Richard Sandifordd420f732013-12-13 15:28:45 +00001663 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1664 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001665 // Test whether the high bit of the byte is clear.
Richard Sandifordd420f732013-12-13 15:28:45 +00001666 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001667 else
1668 // No instruction exists for this combination.
1669 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001670 C.ICmpType = SystemZICMP::UnsignedOnly;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001671 }
1672 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1673 if (Value > Mask)
1674 return;
Ulrich Weigand47f36492015-12-16 18:04:06 +00001675 // If the constant is in range, we can use any comparison.
1676 C.ICmpType = SystemZICMP::Any;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001677 } else
1678 return;
1679
1680 // Make sure that the first operand is an i32 of the right extension type.
Richard Sandifordd420f732013-12-13 15:28:45 +00001681 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1682 ISD::SEXTLOAD :
1683 ISD::ZEXTLOAD);
1684 if (C.Op0.getValueType() != MVT::i32 ||
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001685 Load->getExtensionType() != ExtType)
Justin Lebar9c375812016-07-15 18:27:10 +00001686 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32, Load->getChain(),
1687 Load->getBasePtr(), Load->getPointerInfo(),
1688 Load->getMemoryVT(), Load->getAlignment(),
1689 Load->getMemOperand()->getFlags());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001690
1691 // Make sure that the second operand is an i32 with the right value.
Richard Sandifordd420f732013-12-13 15:28:45 +00001692 if (C.Op1.getValueType() != MVT::i32 ||
1693 Value != ConstOp1->getZExtValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001694 C.Op1 = DAG.getConstant(Value, DL, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001695}
1696
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001697// Return true if Op is either an unextended load, or a load suitable
1698// for integer register-memory comparisons of type ICmpType.
1699static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001700 auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001701 if (Load) {
1702 // There are no instructions to compare a register with a memory byte.
1703 if (Load->getMemoryVT() == MVT::i8)
1704 return false;
1705 // Otherwise decide on extension type.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001706 switch (Load->getExtensionType()) {
1707 case ISD::NON_EXTLOAD:
Richard Sandiford24e597b2013-08-23 11:27:19 +00001708 return true;
1709 case ISD::SEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001710 return ICmpType != SystemZICMP::UnsignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001711 case ISD::ZEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001712 return ICmpType != SystemZICMP::SignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001713 default:
1714 break;
1715 }
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001716 }
Richard Sandiford24e597b2013-08-23 11:27:19 +00001717 return false;
1718}
1719
Richard Sandifordd420f732013-12-13 15:28:45 +00001720// Return true if it is better to swap the operands of C.
1721static bool shouldSwapCmpOperands(const Comparison &C) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001722 // Leave f128 comparisons alone, since they have no memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001723 if (C.Op0.getValueType() == MVT::f128)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001724 return false;
1725
1726 // Always keep a floating-point constant second, since comparisons with
1727 // zero can use LOAD TEST and comparisons with other constants make a
1728 // natural memory operand.
Richard Sandifordd420f732013-12-13 15:28:45 +00001729 if (isa<ConstantFPSDNode>(C.Op1))
Richard Sandiford24e597b2013-08-23 11:27:19 +00001730 return false;
1731
1732 // Never swap comparisons with zero since there are many ways to optimize
1733 // those later.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001734 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001735 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001736 return false;
1737
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001738 // Also keep natural memory operands second if the loaded value is
1739 // only used here. Several comparisons have memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001740 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001741 return false;
1742
Richard Sandiford24e597b2013-08-23 11:27:19 +00001743 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1744 // In that case we generally prefer the memory to be second.
Richard Sandifordd420f732013-12-13 15:28:45 +00001745 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001746 // The only exceptions are when the second operand is a constant and
1747 // we can use things like CHHSI.
Richard Sandifordd420f732013-12-13 15:28:45 +00001748 if (!ConstOp1)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001749 return true;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001750 // The unsigned memory-immediate instructions can handle 16-bit
1751 // unsigned integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001752 if (C.ICmpType != SystemZICMP::SignedOnly &&
1753 isUInt<16>(ConstOp1->getZExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001754 return false;
1755 // The signed memory-immediate instructions can handle 16-bit
1756 // signed integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001757 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1758 isInt<16>(ConstOp1->getSExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001759 return false;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001760 return true;
1761 }
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001762
1763 // Try to promote the use of CGFR and CLGFR.
Richard Sandifordd420f732013-12-13 15:28:45 +00001764 unsigned Opcode0 = C.Op0.getOpcode();
1765 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001766 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001767 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001768 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001769 if (C.ICmpType != SystemZICMP::SignedOnly &&
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001770 Opcode0 == ISD::AND &&
Richard Sandifordd420f732013-12-13 15:28:45 +00001771 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1772 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001773 return true;
1774
Richard Sandiford24e597b2013-08-23 11:27:19 +00001775 return false;
1776}
1777
Richard Sandiford73170f82013-12-11 11:45:08 +00001778// Return a version of comparison CC mask CCMask in which the LT and GT
1779// actions are swapped.
1780static unsigned reverseCCMask(unsigned CCMask) {
1781 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1782 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1783 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1784 (CCMask & SystemZ::CCMASK_CMP_UO));
1785}
1786
Richard Sandiford0847c452013-12-13 15:50:30 +00001787// Check whether C tests for equality between X and Y and whether X - Y
1788// or Y - X is also computed. In that case it's better to compare the
1789// result of the subtraction against zero.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001790static void adjustForSubtraction(SelectionDAG &DAG, const SDLoc &DL,
1791 Comparison &C) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001792 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1793 C.CCMask == SystemZ::CCMASK_CMP_NE) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001794 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001795 SDNode *N = *I;
1796 if (N->getOpcode() == ISD::SUB &&
1797 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1798 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1799 C.Op0 = SDValue(N, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001800 C.Op1 = DAG.getConstant(0, DL, N->getValueType(0));
Richard Sandiford0847c452013-12-13 15:50:30 +00001801 return;
1802 }
1803 }
1804 }
1805}
1806
Richard Sandifordd420f732013-12-13 15:28:45 +00001807// Check whether C compares a floating-point value with zero and if that
1808// floating-point value is also negated. In this case we can use the
1809// negation to set CC, so avoiding separate LOAD AND TEST and
1810// LOAD (NEGATIVE/COMPLEMENT) instructions.
1811static void adjustForFNeg(Comparison &C) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001812 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
Richard Sandiford73170f82013-12-11 11:45:08 +00001813 if (C1 && C1->isZero()) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001814 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford73170f82013-12-11 11:45:08 +00001815 SDNode *N = *I;
1816 if (N->getOpcode() == ISD::FNEG) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001817 C.Op0 = SDValue(N, 0);
1818 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford73170f82013-12-11 11:45:08 +00001819 return;
1820 }
1821 }
1822 }
1823}
1824
Richard Sandifordd420f732013-12-13 15:28:45 +00001825// Check whether C compares (shl X, 32) with 0 and whether X is
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001826// also sign-extended. In that case it is better to test the result
1827// of the sign extension using LTGFR.
1828//
1829// This case is important because InstCombine transforms a comparison
1830// with (sext (trunc X)) into a comparison with (shl X, 32).
Richard Sandifordd420f732013-12-13 15:28:45 +00001831static void adjustForLTGFR(Comparison &C) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001832 // Check for a comparison between (shl X, 32) and 0.
Richard Sandifordd420f732013-12-13 15:28:45 +00001833 if (C.Op0.getOpcode() == ISD::SHL &&
1834 C.Op0.getValueType() == MVT::i64 &&
1835 C.Op1.getOpcode() == ISD::Constant &&
1836 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001837 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001838 if (C1 && C1->getZExtValue() == 32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001839 SDValue ShlOp0 = C.Op0.getOperand(0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001840 // See whether X has any SIGN_EXTEND_INREG uses.
Richard Sandiford28c111e2014-03-06 11:00:15 +00001841 for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001842 SDNode *N = *I;
1843 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
1844 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001845 C.Op0 = SDValue(N, 0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001846 return;
1847 }
1848 }
1849 }
1850 }
1851}
1852
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001853// If C compares the truncation of an extending load, try to compare
1854// the untruncated value instead. This exposes more opportunities to
1855// reuse CC.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001856static void adjustICmpTruncate(SelectionDAG &DAG, const SDLoc &DL,
1857 Comparison &C) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001858 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
1859 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
1860 C.Op1.getOpcode() == ISD::Constant &&
1861 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001862 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00001863 if (L->getMemoryVT().getStoreSizeInBits() <= C.Op0.getValueSizeInBits()) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001864 unsigned Type = L->getExtensionType();
1865 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
1866 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
1867 C.Op0 = C.Op0.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001868 C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType());
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001869 }
1870 }
1871 }
1872}
1873
Richard Sandiford030c1652013-09-13 09:09:50 +00001874// Return true if shift operation N has an in-range constant shift value.
1875// Store it in ShiftVal if so.
1876static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001877 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
Richard Sandiford030c1652013-09-13 09:09:50 +00001878 if (!Shift)
1879 return false;
1880
1881 uint64_t Amount = Shift->getZExtValue();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00001882 if (Amount >= N.getValueSizeInBits())
Richard Sandiford030c1652013-09-13 09:09:50 +00001883 return false;
1884
1885 ShiftVal = Amount;
1886 return true;
1887}
1888
1889// Check whether an AND with Mask is suitable for a TEST UNDER MASK
1890// instruction and whether the CC value is descriptive enough to handle
1891// a comparison of type Opcode between the AND result and CmpVal.
1892// CCMask says which comparison result is being tested and BitSize is
1893// the number of bits in the operands. If TEST UNDER MASK can be used,
1894// return the corresponding CC mask, otherwise return 0.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001895static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
1896 uint64_t Mask, uint64_t CmpVal,
1897 unsigned ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001898 assert(Mask != 0 && "ANDs with zero should have been removed by now");
1899
Richard Sandiford030c1652013-09-13 09:09:50 +00001900 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
1901 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
1902 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
1903 return 0;
1904
Richard Sandiford113c8702013-09-03 15:38:35 +00001905 // Work out the masks for the lowest and highest bits.
1906 unsigned HighShift = 63 - countLeadingZeros(Mask);
1907 uint64_t High = uint64_t(1) << HighShift;
1908 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
1909
1910 // Signed ordered comparisons are effectively unsigned if the sign
1911 // bit is dropped.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001912 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
Richard Sandiford113c8702013-09-03 15:38:35 +00001913
1914 // Check for equality comparisons with 0, or the equivalent.
1915 if (CmpVal == 0) {
1916 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1917 return SystemZ::CCMASK_TM_ALL_0;
1918 if (CCMask == SystemZ::CCMASK_CMP_NE)
1919 return SystemZ::CCMASK_TM_SOME_1;
1920 }
Ulrich Weigand4a4d4ab2016-02-01 18:31:19 +00001921 if (EffectivelyUnsigned && CmpVal > 0 && CmpVal <= Low) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001922 if (CCMask == SystemZ::CCMASK_CMP_LT)
1923 return SystemZ::CCMASK_TM_ALL_0;
1924 if (CCMask == SystemZ::CCMASK_CMP_GE)
1925 return SystemZ::CCMASK_TM_SOME_1;
1926 }
1927 if (EffectivelyUnsigned && CmpVal < Low) {
1928 if (CCMask == SystemZ::CCMASK_CMP_LE)
1929 return SystemZ::CCMASK_TM_ALL_0;
1930 if (CCMask == SystemZ::CCMASK_CMP_GT)
1931 return SystemZ::CCMASK_TM_SOME_1;
1932 }
1933
1934 // Check for equality comparisons with the mask, or the equivalent.
1935 if (CmpVal == Mask) {
1936 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1937 return SystemZ::CCMASK_TM_ALL_1;
1938 if (CCMask == SystemZ::CCMASK_CMP_NE)
1939 return SystemZ::CCMASK_TM_SOME_0;
1940 }
1941 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
1942 if (CCMask == SystemZ::CCMASK_CMP_GT)
1943 return SystemZ::CCMASK_TM_ALL_1;
1944 if (CCMask == SystemZ::CCMASK_CMP_LE)
1945 return SystemZ::CCMASK_TM_SOME_0;
1946 }
1947 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
1948 if (CCMask == SystemZ::CCMASK_CMP_GE)
1949 return SystemZ::CCMASK_TM_ALL_1;
1950 if (CCMask == SystemZ::CCMASK_CMP_LT)
1951 return SystemZ::CCMASK_TM_SOME_0;
1952 }
1953
1954 // Check for ordered comparisons with the top bit.
1955 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
1956 if (CCMask == SystemZ::CCMASK_CMP_LE)
1957 return SystemZ::CCMASK_TM_MSB_0;
1958 if (CCMask == SystemZ::CCMASK_CMP_GT)
1959 return SystemZ::CCMASK_TM_MSB_1;
1960 }
1961 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
1962 if (CCMask == SystemZ::CCMASK_CMP_LT)
1963 return SystemZ::CCMASK_TM_MSB_0;
1964 if (CCMask == SystemZ::CCMASK_CMP_GE)
1965 return SystemZ::CCMASK_TM_MSB_1;
1966 }
1967
1968 // If there are just two bits, we can do equality checks for Low and High
1969 // as well.
1970 if (Mask == Low + High) {
1971 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
1972 return SystemZ::CCMASK_TM_MIXED_MSB_0;
1973 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
1974 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
1975 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
1976 return SystemZ::CCMASK_TM_MIXED_MSB_1;
1977 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
1978 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
1979 }
1980
1981 // Looks like we've exhausted our options.
1982 return 0;
1983}
1984
Richard Sandifordd420f732013-12-13 15:28:45 +00001985// See whether C can be implemented as a TEST UNDER MASK instruction.
1986// Update the arguments with the TM version if so.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001987static void adjustForTestUnderMask(SelectionDAG &DAG, const SDLoc &DL,
1988 Comparison &C) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001989 // Check that we have a comparison with a constant.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001990 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001991 if (!ConstOp1)
Richard Sandiford35b9be22013-08-28 10:31:43 +00001992 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001993 uint64_t CmpVal = ConstOp1->getZExtValue();
Richard Sandiford35b9be22013-08-28 10:31:43 +00001994
1995 // Check whether the nonconstant input is an AND with a constant mask.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001996 Comparison NewC(C);
1997 uint64_t MaskVal;
Craig Topper062a2ba2014-04-25 05:30:21 +00001998 ConstantSDNode *Mask = nullptr;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001999 if (C.Op0.getOpcode() == ISD::AND) {
2000 NewC.Op0 = C.Op0.getOperand(0);
2001 NewC.Op1 = C.Op0.getOperand(1);
2002 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
2003 if (!Mask)
2004 return;
2005 MaskVal = Mask->getZExtValue();
2006 } else {
2007 // There is no instruction to compare with a 64-bit immediate
2008 // so use TMHH instead if possible. We need an unsigned ordered
2009 // comparison with an i64 immediate.
2010 if (NewC.Op0.getValueType() != MVT::i64 ||
2011 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
2012 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
2013 NewC.ICmpType == SystemZICMP::SignedOnly)
2014 return;
2015 // Convert LE and GT comparisons into LT and GE.
2016 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
2017 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
2018 if (CmpVal == uint64_t(-1))
2019 return;
2020 CmpVal += 1;
2021 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
2022 }
2023 // If the low N bits of Op1 are zero than the low N bits of Op0 can
2024 // be masked off without changing the result.
2025 MaskVal = -(CmpVal & -CmpVal);
2026 NewC.ICmpType = SystemZICMP::UnsignedOnly;
2027 }
Ulrich Weigandb8d76fb2015-03-30 13:46:59 +00002028 if (!MaskVal)
2029 return;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002030
Richard Sandiford113c8702013-09-03 15:38:35 +00002031 // Check whether the combination of mask, comparison value and comparison
2032 // type are suitable.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002033 unsigned BitSize = NewC.Op0.getValueSizeInBits();
Richard Sandiford030c1652013-09-13 09:09:50 +00002034 unsigned NewCCMask, ShiftVal;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002035 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2036 NewC.Op0.getOpcode() == ISD::SHL &&
2037 isSimpleShift(NewC.Op0, ShiftVal) &&
2038 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
2039 MaskVal >> ShiftVal,
Richard Sandiford030c1652013-09-13 09:09:50 +00002040 CmpVal >> ShiftVal,
2041 SystemZICMP::Any))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002042 NewC.Op0 = NewC.Op0.getOperand(0);
2043 MaskVal >>= ShiftVal;
2044 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2045 NewC.Op0.getOpcode() == ISD::SRL &&
2046 isSimpleShift(NewC.Op0, ShiftVal) &&
2047 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
Richard Sandiford030c1652013-09-13 09:09:50 +00002048 MaskVal << ShiftVal,
2049 CmpVal << ShiftVal,
2050 SystemZICMP::UnsignedOnly))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002051 NewC.Op0 = NewC.Op0.getOperand(0);
2052 MaskVal <<= ShiftVal;
Richard Sandiford030c1652013-09-13 09:09:50 +00002053 } else {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002054 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
2055 NewC.ICmpType);
Richard Sandiford030c1652013-09-13 09:09:50 +00002056 if (!NewCCMask)
2057 return;
2058 }
Richard Sandiford113c8702013-09-03 15:38:35 +00002059
Richard Sandiford35b9be22013-08-28 10:31:43 +00002060 // Go ahead and make the change.
Richard Sandifordd420f732013-12-13 15:28:45 +00002061 C.Opcode = SystemZISD::TM;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002062 C.Op0 = NewC.Op0;
2063 if (Mask && Mask->getZExtValue() == MaskVal)
2064 C.Op1 = SDValue(Mask, 0);
2065 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002066 C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType());
Richard Sandifordd420f732013-12-13 15:28:45 +00002067 C.CCValid = SystemZ::CCMASK_TM;
2068 C.CCMask = NewCCMask;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002069}
2070
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002071// Return a Comparison that tests the condition-code result of intrinsic
2072// node Call against constant integer CC using comparison code Cond.
2073// Opcode is the opcode of the SystemZISD operation for the intrinsic
2074// and CCValid is the set of possible condition-code results.
2075static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode,
2076 SDValue Call, unsigned CCValid, uint64_t CC,
2077 ISD::CondCode Cond) {
2078 Comparison C(Call, SDValue());
2079 C.Opcode = Opcode;
2080 C.CCValid = CCValid;
2081 if (Cond == ISD::SETEQ)
2082 // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3.
2083 C.CCMask = CC < 4 ? 1 << (3 - CC) : 0;
2084 else if (Cond == ISD::SETNE)
2085 // ...and the inverse of that.
2086 C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1;
2087 else if (Cond == ISD::SETLT || Cond == ISD::SETULT)
2088 // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3,
2089 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002090 C.CCMask = CC < 4 ? ~0U << (4 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002091 else if (Cond == ISD::SETGE || Cond == ISD::SETUGE)
2092 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002093 C.CCMask = CC < 4 ? ~(~0U << (4 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002094 else if (Cond == ISD::SETLE || Cond == ISD::SETULE)
2095 // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true),
2096 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002097 C.CCMask = CC < 4 ? ~0U << (3 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002098 else if (Cond == ISD::SETGT || Cond == ISD::SETUGT)
2099 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002100 C.CCMask = CC < 4 ? ~(~0U << (3 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002101 else
2102 llvm_unreachable("Unexpected integer comparison type");
2103 C.CCMask &= CCValid;
2104 return C;
2105}
2106
Richard Sandifordd420f732013-12-13 15:28:45 +00002107// Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
2108static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002109 ISD::CondCode Cond, const SDLoc &DL) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002110 if (CmpOp1.getOpcode() == ISD::Constant) {
2111 uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue();
2112 unsigned Opcode, CCValid;
2113 if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
2114 CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) &&
2115 isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid))
2116 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002117 if (CmpOp0.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
2118 CmpOp0.getResNo() == CmpOp0->getNumValues() - 1 &&
2119 isIntrinsicWithCC(CmpOp0, Opcode, CCValid))
2120 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002121 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002122 Comparison C(CmpOp0, CmpOp1);
2123 C.CCMask = CCMaskForCondCode(Cond);
2124 if (C.Op0.getValueType().isFloatingPoint()) {
2125 C.CCValid = SystemZ::CCMASK_FCMP;
2126 C.Opcode = SystemZISD::FCMP;
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002127 adjustForFNeg(C);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002128 } else {
Richard Sandifordd420f732013-12-13 15:28:45 +00002129 C.CCValid = SystemZ::CCMASK_ICMP;
2130 C.Opcode = SystemZISD::ICMP;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002131 // Choose the type of comparison. Equality and inequality tests can
2132 // use either signed or unsigned comparisons. The choice also doesn't
2133 // matter if both sign bits are known to be clear. In those cases we
2134 // want to give the main isel code the freedom to choose whichever
2135 // form fits best.
Richard Sandifordd420f732013-12-13 15:28:45 +00002136 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
2137 C.CCMask == SystemZ::CCMASK_CMP_NE ||
2138 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
2139 C.ICmpType = SystemZICMP::Any;
2140 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
2141 C.ICmpType = SystemZICMP::UnsignedOnly;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002142 else
Richard Sandifordd420f732013-12-13 15:28:45 +00002143 C.ICmpType = SystemZICMP::SignedOnly;
2144 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002145 adjustZeroCmp(DAG, DL, C);
2146 adjustSubwordCmp(DAG, DL, C);
2147 adjustForSubtraction(DAG, DL, C);
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002148 adjustForLTGFR(C);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002149 adjustICmpTruncate(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002150 }
2151
Richard Sandifordd420f732013-12-13 15:28:45 +00002152 if (shouldSwapCmpOperands(C)) {
2153 std::swap(C.Op0, C.Op1);
2154 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford24e597b2013-08-23 11:27:19 +00002155 }
2156
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002157 adjustForTestUnderMask(DAG, DL, C);
Richard Sandifordd420f732013-12-13 15:28:45 +00002158 return C;
2159}
2160
2161// Emit the comparison instruction described by C.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002162static SDValue emitCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002163 if (!C.Op1.getNode()) {
2164 SDValue Op;
2165 switch (C.Op0.getOpcode()) {
2166 case ISD::INTRINSIC_W_CHAIN:
2167 Op = emitIntrinsicWithChainAndGlue(DAG, C.Op0, C.Opcode);
2168 break;
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002169 case ISD::INTRINSIC_WO_CHAIN:
2170 Op = emitIntrinsicWithGlue(DAG, C.Op0, C.Opcode);
2171 break;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002172 default:
2173 llvm_unreachable("Invalid comparison operands");
2174 }
2175 return SDValue(Op.getNode(), Op->getNumValues() - 1);
2176 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002177 if (C.Opcode == SystemZISD::ICMP)
2178 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002179 DAG.getConstant(C.ICmpType, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002180 if (C.Opcode == SystemZISD::TM) {
2181 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
2182 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
2183 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002184 DAG.getConstant(RegisterOnly, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002185 }
2186 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002187}
2188
Richard Sandiford7d86e472013-08-21 09:34:56 +00002189// Implement a 32-bit *MUL_LOHI operation by extending both operands to
2190// 64 bits. Extend is the extension type to use. Store the high part
2191// in Hi and the low part in Lo.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002192static void lowerMUL_LOHI32(SelectionDAG &DAG, const SDLoc &DL, unsigned Extend,
2193 SDValue Op0, SDValue Op1, SDValue &Hi,
2194 SDValue &Lo) {
Richard Sandiford7d86e472013-08-21 09:34:56 +00002195 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
2196 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
2197 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002198 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
2199 DAG.getConstant(32, DL, MVT::i64));
Richard Sandiford7d86e472013-08-21 09:34:56 +00002200 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
2201 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
2202}
2203
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002204// Lower a binary operation that produces two VT results, one in each
2205// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
2206// Extend extends Op0 to a GR128, and Opcode performs the GR128 operation
2207// on the extended Op0 and (unextended) Op1. Store the even register result
2208// in Even and the odd register result in Odd.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002209static void lowerGR128Binary(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
2210 unsigned Extend, unsigned Opcode, SDValue Op0,
2211 SDValue Op1, SDValue &Even, SDValue &Odd) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002212 SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0);
2213 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped,
2214 SDValue(In128, 0), Op1);
2215 bool Is32Bit = is32Bit(VT);
Richard Sandifordd8163202013-09-13 09:12:44 +00002216 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
2217 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002218}
2219
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002220// Return an i32 value that is 1 if the CC value produced by Glue is
2221// in the mask CCMask and 0 otherwise. CC is known to have a value
2222// in CCValid, so other values can be ignored.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002223static SDValue emitSETCC(SelectionDAG &DAG, const SDLoc &DL, SDValue Glue,
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002224 unsigned CCValid, unsigned CCMask) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002225 IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
2226 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
2227
2228 if (Conversion.XORValue)
2229 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002230 DAG.getConstant(Conversion.XORValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002231
2232 if (Conversion.AddValue)
2233 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002234 DAG.getConstant(Conversion.AddValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002235
2236 // The SHR/AND sequence should get optimized to an RISBG.
2237 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002238 DAG.getConstant(Conversion.Bit, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002239 if (Conversion.Bit != 31)
2240 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002241 DAG.getConstant(1, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002242 return Result;
2243}
2244
Ulrich Weigandcd808232015-05-05 19:26:48 +00002245// Return the SystemISD vector comparison operation for CC, or 0 if it cannot
2246// be done directly. IsFP is true if CC is for a floating-point rather than
2247// integer comparison.
2248static unsigned getVectorComparison(ISD::CondCode CC, bool IsFP) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002249 switch (CC) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002250 case ISD::SETOEQ:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002251 case ISD::SETEQ:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002252 return IsFP ? SystemZISD::VFCMPE : SystemZISD::VICMPE;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002253
Ulrich Weigandcd808232015-05-05 19:26:48 +00002254 case ISD::SETOGE:
2255 case ISD::SETGE:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002256 return IsFP ? SystemZISD::VFCMPHE : static_cast<SystemZISD::NodeType>(0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002257
2258 case ISD::SETOGT:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002259 case ISD::SETGT:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002260 return IsFP ? SystemZISD::VFCMPH : SystemZISD::VICMPH;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002261
2262 case ISD::SETUGT:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002263 return IsFP ? static_cast<SystemZISD::NodeType>(0) : SystemZISD::VICMPHL;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002264
2265 default:
2266 return 0;
2267 }
2268}
2269
2270// Return the SystemZISD vector comparison operation for CC or its inverse,
2271// or 0 if neither can be done directly. Indicate in Invert whether the
Ulrich Weigandcd808232015-05-05 19:26:48 +00002272// result is for the inverse of CC. IsFP is true if CC is for a
2273// floating-point rather than integer comparison.
2274static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, bool IsFP,
2275 bool &Invert) {
2276 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002277 Invert = false;
2278 return Opcode;
2279 }
2280
Ulrich Weigandcd808232015-05-05 19:26:48 +00002281 CC = ISD::getSetCCInverse(CC, !IsFP);
2282 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002283 Invert = true;
2284 return Opcode;
2285 }
2286
2287 return 0;
2288}
2289
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002290// Return a v2f64 that contains the extended form of elements Start and Start+1
2291// of v4f32 value Op.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002292static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, const SDLoc &DL,
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002293 SDValue Op) {
2294 int Mask[] = { Start, -1, Start + 1, -1 };
2295 Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask);
2296 return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op);
2297}
2298
2299// Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode,
2300// producing a result of type VT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002301static SDValue getVectorCmp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &DL,
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002302 EVT VT, SDValue CmpOp0, SDValue CmpOp1) {
2303 // There is no hardware support for v4f32, so extend the vector into
2304 // two v2f64s and compare those.
2305 if (CmpOp0.getValueType() == MVT::v4f32) {
2306 SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0);
2307 SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0);
2308 SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1);
2309 SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1);
2310 SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1);
2311 SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1);
2312 return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes);
2313 }
2314 return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1);
2315}
2316
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002317// Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing
2318// an integer mask of type VT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002319static SDValue lowerVectorSETCC(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002320 ISD::CondCode CC, SDValue CmpOp0,
2321 SDValue CmpOp1) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002322 bool IsFP = CmpOp0.getValueType().isFloatingPoint();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002323 bool Invert = false;
2324 SDValue Cmp;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002325 switch (CC) {
2326 // Handle tests for order using (or (ogt y x) (oge x y)).
2327 case ISD::SETUO:
2328 Invert = true;
2329 case ISD::SETO: {
2330 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002331 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2332 SDValue GE = getVectorCmp(DAG, SystemZISD::VFCMPHE, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002333 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE);
2334 break;
2335 }
2336
2337 // Handle <> tests using (or (ogt y x) (ogt x y)).
2338 case ISD::SETUEQ:
2339 Invert = true;
2340 case ISD::SETONE: {
2341 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002342 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2343 SDValue GT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002344 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT);
2345 break;
2346 }
2347
2348 // Otherwise a single comparison is enough. It doesn't really
2349 // matter whether we try the inversion or the swap first, since
2350 // there are no cases where both work.
2351 default:
2352 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002353 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002354 else {
2355 CC = ISD::getSetCCSwappedOperands(CC);
2356 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002357 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002358 else
2359 llvm_unreachable("Unhandled comparison");
2360 }
2361 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002362 }
2363 if (Invert) {
2364 SDValue Mask = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2365 DAG.getConstant(65535, DL, MVT::i32));
2366 Mask = DAG.getNode(ISD::BITCAST, DL, VT, Mask);
2367 Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask);
2368 }
2369 return Cmp;
2370}
2371
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002372SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
2373 SelectionDAG &DAG) const {
2374 SDValue CmpOp0 = Op.getOperand(0);
2375 SDValue CmpOp1 = Op.getOperand(1);
2376 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
2377 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002378 EVT VT = Op.getValueType();
2379 if (VT.isVector())
2380 return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002381
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002382 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002383 SDValue Glue = emitCmp(DAG, DL, C);
2384 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002385}
2386
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002387SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002388 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2389 SDValue CmpOp0 = Op.getOperand(2);
2390 SDValue CmpOp1 = Op.getOperand(3);
2391 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002392 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002393
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002394 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002395 SDValue Glue = emitCmp(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002396 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002397 Op.getOperand(0), DAG.getConstant(C.CCValid, DL, MVT::i32),
2398 DAG.getConstant(C.CCMask, DL, MVT::i32), Dest, Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002399}
2400
Richard Sandiford57485472013-12-13 15:35:00 +00002401// Return true if Pos is CmpOp and Neg is the negative of CmpOp,
2402// allowing Pos and Neg to be wider than CmpOp.
2403static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
2404 return (Neg.getOpcode() == ISD::SUB &&
2405 Neg.getOperand(0).getOpcode() == ISD::Constant &&
2406 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
2407 Neg.getOperand(1) == Pos &&
2408 (Pos == CmpOp ||
2409 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
2410 Pos.getOperand(0) == CmpOp)));
2411}
2412
2413// Return the absolute or negative absolute of Op; IsNegative decides which.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002414static SDValue getAbsolute(SelectionDAG &DAG, const SDLoc &DL, SDValue Op,
Richard Sandiford57485472013-12-13 15:35:00 +00002415 bool IsNegative) {
2416 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
2417 if (IsNegative)
2418 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002419 DAG.getConstant(0, DL, Op.getValueType()), Op);
Richard Sandiford57485472013-12-13 15:35:00 +00002420 return Op;
2421}
2422
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002423SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
2424 SelectionDAG &DAG) const {
2425 SDValue CmpOp0 = Op.getOperand(0);
2426 SDValue CmpOp1 = Op.getOperand(1);
2427 SDValue TrueOp = Op.getOperand(2);
2428 SDValue FalseOp = Op.getOperand(3);
2429 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002430 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002431
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002432 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandiford57485472013-12-13 15:35:00 +00002433
2434 // Check for absolute and negative-absolute selections, including those
2435 // where the comparison value is sign-extended (for LPGFR and LNGFR).
2436 // This check supplements the one in DAGCombiner.
2437 if (C.Opcode == SystemZISD::ICMP &&
2438 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
2439 C.CCMask != SystemZ::CCMASK_CMP_NE &&
2440 C.Op1.getOpcode() == ISD::Constant &&
2441 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
2442 if (isAbsolute(C.Op0, TrueOp, FalseOp))
2443 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
2444 if (isAbsolute(C.Op0, FalseOp, TrueOp))
2445 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
2446 }
2447
Richard Sandifordd420f732013-12-13 15:28:45 +00002448 SDValue Glue = emitCmp(DAG, DL, C);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002449
2450 // Special case for handling -1/0 results. The shifts we use here
2451 // should get optimized with the IPM conversion sequence.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002452 auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
2453 auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002454 if (TrueC && FalseC) {
2455 int64_t TrueVal = TrueC->getSExtValue();
2456 int64_t FalseVal = FalseC->getSExtValue();
2457 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
2458 // Invert the condition if we want -1 on false.
2459 if (TrueVal == 0)
Richard Sandifordd420f732013-12-13 15:28:45 +00002460 C.CCMask ^= C.CCValid;
2461 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002462 EVT VT = Op.getValueType();
2463 // Extend the result to VT. Upper bits are ignored.
2464 if (!is32Bit(VT))
2465 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
2466 // Sign-extend from the low bit.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002467 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002468 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
2469 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
2470 }
2471 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002472
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002473 SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, DL, MVT::i32),
2474 DAG.getConstant(C.CCMask, DL, MVT::i32), Glue};
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002475
2476 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00002477 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002478}
2479
2480SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
2481 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002482 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002483 const GlobalValue *GV = Node->getGlobal();
2484 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002485 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopher93bf97c2014-06-27 07:38:01 +00002486 CodeModel::Model CM = DAG.getTarget().getCodeModel();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002487
2488 SDValue Result;
Rafael Espindola3beef8d2016-06-27 23:15:57 +00002489 if (Subtarget.isPC32DBLSymbol(GV, CM)) {
Richard Sandiford54b36912013-09-27 15:14:04 +00002490 // Assign anchors at 1<<12 byte boundaries.
2491 uint64_t Anchor = Offset & ~uint64_t(0xfff);
2492 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
2493 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2494
2495 // The offset can be folded into the address if it is aligned to a halfword.
2496 Offset -= Anchor;
2497 if (Offset != 0 && (Offset & 1) == 0) {
2498 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
2499 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002500 Offset = 0;
2501 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002502 } else {
2503 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
2504 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2505 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Justin Lebar9c375812016-07-15 18:27:10 +00002506 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002507 }
2508
2509 // If there was a non-zero offset that we didn't fold, create an explicit
2510 // addition for it.
2511 if (Offset != 0)
2512 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002513 DAG.getConstant(Offset, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002514
2515 return Result;
2516}
2517
Ulrich Weigand7db69182015-02-18 09:13:27 +00002518SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
2519 SelectionDAG &DAG,
2520 unsigned Opcode,
2521 SDValue GOTOffset) const {
2522 SDLoc DL(Node);
Mehdi Amini44ede332015-07-09 02:09:04 +00002523 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand7db69182015-02-18 09:13:27 +00002524 SDValue Chain = DAG.getEntryNode();
2525 SDValue Glue;
2526
2527 // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
2528 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
2529 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
2530 Glue = Chain.getValue(1);
2531 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
2532 Glue = Chain.getValue(1);
2533
2534 // The first call operand is the chain and the second is the TLS symbol.
2535 SmallVector<SDValue, 8> Ops;
2536 Ops.push_back(Chain);
2537 Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
2538 Node->getValueType(0),
2539 0, 0));
2540
2541 // Add argument registers to the end of the list so that they are
2542 // known live into the call.
2543 Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
2544 Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
2545
2546 // Add a register mask operand representing the call-preserved registers.
2547 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002548 const uint32_t *Mask =
2549 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002550 assert(Mask && "Missing call preserved mask for calling convention");
2551 Ops.push_back(DAG.getRegisterMask(Mask));
2552
2553 // Glue the call to the argument copies.
2554 Ops.push_back(Glue);
2555
2556 // Emit the call.
2557 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2558 Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
2559 Glue = Chain.getValue(1);
2560
2561 // Copy the return value from %r2.
2562 return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
2563}
2564
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002565SDValue SystemZTargetLowering::lowerThreadPointer(const SDLoc &DL,
2566 SelectionDAG &DAG) const {
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002567 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002568 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002569
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002570 // The high part of the thread pointer is in access register 0.
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002571 SDValue TPHi = DAG.getCopyFromReg(Chain, DL, SystemZ::A0, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002572 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
2573
2574 // The low part of the thread pointer is in access register 1.
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002575 SDValue TPLo = DAG.getCopyFromReg(Chain, DL, SystemZ::A1, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002576 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
2577
2578 // Merge them into a single 64-bit address.
2579 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002580 DAG.getConstant(32, DL, PtrVT));
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002581 return DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
2582}
2583
2584SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
2585 SelectionDAG &DAG) const {
2586 if (DAG.getTarget().Options.EmulatedTLS)
2587 return LowerToTLSEmulatedModel(Node, DAG);
2588 SDLoc DL(Node);
2589 const GlobalValue *GV = Node->getGlobal();
2590 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2591 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
2592
2593 SDValue TP = lowerThreadPointer(DL, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002594
Ulrich Weigand7db69182015-02-18 09:13:27 +00002595 // Get the offset of GA from the thread pointer, based on the TLS model.
2596 SDValue Offset;
2597 switch (model) {
2598 case TLSModel::GeneralDynamic: {
2599 // Load the GOT offset of the tls_index (module ID / per-symbol offset).
2600 SystemZConstantPoolValue *CPV =
2601 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002602
Ulrich Weigand7db69182015-02-18 09:13:27 +00002603 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002604 Offset = DAG.getLoad(
2605 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002606 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002607
2608 // Call __tls_get_offset to retrieve the offset.
2609 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
2610 break;
2611 }
2612
2613 case TLSModel::LocalDynamic: {
2614 // Load the GOT offset of the module ID.
2615 SystemZConstantPoolValue *CPV =
2616 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
2617
2618 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002619 Offset = DAG.getLoad(
2620 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002621 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002622
2623 // Call __tls_get_offset to retrieve the module base offset.
2624 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
2625
2626 // Note: The SystemZLDCleanupPass will remove redundant computations
2627 // of the module base offset. Count total number of local-dynamic
2628 // accesses to trigger execution of that pass.
2629 SystemZMachineFunctionInfo* MFI =
2630 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
2631 MFI->incNumLocalDynamicTLSAccesses();
2632
2633 // Add the per-symbol offset.
2634 CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
2635
2636 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002637 DTPOffset = DAG.getLoad(
2638 PtrVT, DL, DAG.getEntryNode(), DTPOffset,
Justin Lebar9c375812016-07-15 18:27:10 +00002639 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002640
2641 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
2642 break;
2643 }
2644
2645 case TLSModel::InitialExec: {
2646 // Load the offset from the GOT.
2647 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2648 SystemZII::MO_INDNTPOFF);
2649 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +00002650 Offset =
2651 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Offset,
2652 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002653 break;
2654 }
2655
2656 case TLSModel::LocalExec: {
2657 // Force the offset into the constant pool and load it from there.
2658 SystemZConstantPoolValue *CPV =
2659 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
2660
2661 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002662 Offset = DAG.getLoad(
2663 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002664 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002665 break;
Ulrich Weigandb7e59092015-02-18 09:42:23 +00002666 }
Ulrich Weigand7db69182015-02-18 09:13:27 +00002667 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002668
2669 // Add the base and offset together.
2670 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
2671}
2672
2673SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
2674 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002675 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002676 const BlockAddress *BA = Node->getBlockAddress();
2677 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002678 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002679
2680 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
2681 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2682 return Result;
2683}
2684
2685SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
2686 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002687 SDLoc DL(JT);
Mehdi Amini44ede332015-07-09 02:09:04 +00002688 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002689 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
2690
2691 // Use LARL to load the address of the table.
2692 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2693}
2694
2695SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
2696 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002697 SDLoc DL(CP);
Mehdi Amini44ede332015-07-09 02:09:04 +00002698 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002699
2700 SDValue Result;
2701 if (CP->isMachineConstantPoolEntry())
2702 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002703 CP->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002704 else
2705 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002706 CP->getAlignment(), CP->getOffset());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002707
2708 // Use LARL to load the address of the constant pool entry.
2709 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2710}
2711
Ulrich Weigandf557d082016-04-04 12:44:55 +00002712SDValue SystemZTargetLowering::lowerFRAMEADDR(SDValue Op,
2713 SelectionDAG &DAG) const {
2714 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002715 MachineFrameInfo &MFI = MF.getFrameInfo();
2716 MFI.setFrameAddressIsTaken(true);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002717
2718 SDLoc DL(Op);
2719 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2720 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2721
2722 // If the back chain frame index has not been allocated yet, do so.
2723 SystemZMachineFunctionInfo *FI = MF.getInfo<SystemZMachineFunctionInfo>();
2724 int BackChainIdx = FI->getFramePointerSaveIndex();
2725 if (!BackChainIdx) {
2726 // By definition, the frame address is the address of the back chain.
Matthias Braun941a7052016-07-28 18:40:00 +00002727 BackChainIdx = MFI.CreateFixedObject(8, -SystemZMC::CallFrameSize, false);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002728 FI->setFramePointerSaveIndex(BackChainIdx);
2729 }
2730 SDValue BackChain = DAG.getFrameIndex(BackChainIdx, PtrVT);
2731
2732 // FIXME The frontend should detect this case.
2733 if (Depth > 0) {
2734 report_fatal_error("Unsupported stack frame traversal count");
2735 }
2736
2737 return BackChain;
2738}
2739
2740SDValue SystemZTargetLowering::lowerRETURNADDR(SDValue Op,
2741 SelectionDAG &DAG) const {
2742 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002743 MachineFrameInfo &MFI = MF.getFrameInfo();
2744 MFI.setReturnAddressIsTaken(true);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002745
2746 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
2747 return SDValue();
2748
2749 SDLoc DL(Op);
2750 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2751 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2752
2753 // FIXME The frontend should detect this case.
2754 if (Depth > 0) {
2755 report_fatal_error("Unsupported stack frame traversal count");
2756 }
2757
2758 // Return R14D, which has the return address. Mark it an implicit live-in.
2759 unsigned LinkReg = MF.addLiveIn(SystemZ::R14D, &SystemZ::GR64BitRegClass);
2760 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, LinkReg, PtrVT);
2761}
2762
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002763SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
2764 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002765 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002766 SDValue In = Op.getOperand(0);
2767 EVT InVT = In.getValueType();
2768 EVT ResVT = Op.getValueType();
2769
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002770 // Convert loads directly. This is normally done by DAGCombiner,
2771 // but we need this case for bitcasts that are created during lowering
2772 // and which are then lowered themselves.
2773 if (auto *LoadN = dyn_cast<LoadSDNode>(In))
2774 return DAG.getLoad(ResVT, DL, LoadN->getChain(), LoadN->getBasePtr(),
2775 LoadN->getMemOperand());
2776
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002777 if (InVT == MVT::i32 && ResVT == MVT::f32) {
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002778 SDValue In64;
2779 if (Subtarget.hasHighWord()) {
2780 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
2781 MVT::i64);
2782 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
2783 MVT::i64, SDValue(U64, 0), In);
2784 } else {
2785 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
2786 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002787 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002788 }
2789 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002790 return DAG.getTargetExtractSubreg(SystemZ::subreg_r32,
Richard Sandifordd8163202013-09-13 09:12:44 +00002791 DL, MVT::f32, Out64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002792 }
2793 if (InVT == MVT::f32 && ResVT == MVT::i32) {
2794 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002795 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002796 MVT::f64, SDValue(U64, 0), In);
2797 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002798 if (Subtarget.hasHighWord())
2799 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
2800 MVT::i32, Out64);
2801 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002802 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002803 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002804 }
2805 llvm_unreachable("Unexpected bitcast combination");
2806}
2807
2808SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
2809 SelectionDAG &DAG) const {
2810 MachineFunction &MF = DAG.getMachineFunction();
2811 SystemZMachineFunctionInfo *FuncInfo =
2812 MF.getInfo<SystemZMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002813 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002814
2815 SDValue Chain = Op.getOperand(0);
2816 SDValue Addr = Op.getOperand(1);
2817 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002818 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002819
2820 // The initial values of each field.
2821 const unsigned NumFields = 4;
2822 SDValue Fields[NumFields] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002823 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT),
2824 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002825 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
2826 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
2827 };
2828
2829 // Store each field into its respective slot.
2830 SDValue MemOps[NumFields];
2831 unsigned Offset = 0;
2832 for (unsigned I = 0; I < NumFields; ++I) {
2833 SDValue FieldAddr = Addr;
2834 if (Offset != 0)
2835 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002836 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002837 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00002838 MachinePointerInfo(SV, Offset));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002839 Offset += 8;
2840 }
Craig Topper48d114b2014-04-26 18:35:24 +00002841 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002842}
2843
2844SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
2845 SelectionDAG &DAG) const {
2846 SDValue Chain = Op.getOperand(0);
2847 SDValue DstPtr = Op.getOperand(1);
2848 SDValue SrcPtr = Op.getOperand(2);
2849 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
2850 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002851 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002852
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002853 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002854 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00002855 /*isTailCall*/false,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002856 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
2857}
2858
2859SDValue SystemZTargetLowering::
2860lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002861 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00002862 MachineFunction &MF = DAG.getMachineFunction();
2863 bool RealignOpt = !MF.getFunction()-> hasFnAttribute("no-realign-stack");
2864 bool StoreBackchain = MF.getFunction()->hasFnAttribute("backchain");
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002865
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002866 SDValue Chain = Op.getOperand(0);
2867 SDValue Size = Op.getOperand(1);
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002868 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002869 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002870
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002871 // If user has set the no alignment function attribute, ignore
2872 // alloca alignments.
2873 uint64_t AlignVal = (RealignOpt ?
2874 dyn_cast<ConstantSDNode>(Align)->getZExtValue() : 0);
2875
2876 uint64_t StackAlign = TFI->getStackAlignment();
2877 uint64_t RequiredAlign = std::max(AlignVal, StackAlign);
2878 uint64_t ExtraAlignSpace = RequiredAlign - StackAlign;
2879
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002880 unsigned SPReg = getStackPointerRegisterToSaveRestore();
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002881 SDValue NeededSpace = Size;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002882
2883 // Get a reference to the stack pointer.
2884 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
2885
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00002886 // If we need a backchain, save it now.
2887 SDValue Backchain;
2888 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00002889 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00002890
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002891 // Add extra space for alignment if needed.
2892 if (ExtraAlignSpace)
2893 NeededSpace = DAG.getNode(ISD::ADD, DL, MVT::i64, NeededSpace,
Elliot Colpbc2cfc22016-07-06 18:13:11 +00002894 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002895
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002896 // Get the new stack pointer value.
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002897 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, NeededSpace);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002898
2899 // Copy the new stack pointer back.
2900 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
2901
2902 // The allocated data lives above the 160 bytes allocated for the standard
2903 // frame, plus any outgoing stack arguments. We don't know how much that
2904 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
2905 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
2906 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
2907
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002908 // Dynamically realign if needed.
2909 if (RequiredAlign > StackAlign) {
2910 Result =
2911 DAG.getNode(ISD::ADD, DL, MVT::i64, Result,
2912 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
2913 Result =
2914 DAG.getNode(ISD::AND, DL, MVT::i64, Result,
2915 DAG.getConstant(~(RequiredAlign - 1), DL, MVT::i64));
2916 }
2917
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00002918 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00002919 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00002920
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002921 SDValue Ops[2] = { Result, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002922 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002923}
2924
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +00002925SDValue SystemZTargetLowering::lowerGET_DYNAMIC_AREA_OFFSET(
2926 SDValue Op, SelectionDAG &DAG) const {
2927 SDLoc DL(Op);
2928
2929 return DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
2930}
2931
Richard Sandiford7d86e472013-08-21 09:34:56 +00002932SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
2933 SelectionDAG &DAG) const {
2934 EVT VT = Op.getValueType();
2935 SDLoc DL(Op);
2936 SDValue Ops[2];
2937 if (is32Bit(VT))
2938 // Just do a normal 64-bit multiplication and extract the results.
2939 // We define this so that it can be used for constant division.
2940 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
2941 Op.getOperand(1), Ops[1], Ops[0]);
2942 else {
2943 // Do a full 128-bit multiplication based on UMUL_LOHI64:
2944 //
2945 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
2946 //
2947 // but using the fact that the upper halves are either all zeros
2948 // or all ones:
2949 //
2950 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
2951 //
2952 // and grouping the right terms together since they are quicker than the
2953 // multiplication:
2954 //
2955 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002956 SDValue C63 = DAG.getConstant(63, DL, MVT::i64);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002957 SDValue LL = Op.getOperand(0);
2958 SDValue RL = Op.getOperand(1);
2959 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
2960 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
2961 // UMUL_LOHI64 returns the low result in the odd register and the high
2962 // result in the even register. SMUL_LOHI is defined to return the
2963 // low half first, so the results are in reverse order.
2964 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2965 LL, RL, Ops[1], Ops[0]);
2966 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
2967 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
2968 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
2969 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
2970 }
Craig Topper64941d92014-04-27 19:20:57 +00002971 return DAG.getMergeValues(Ops, DL);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002972}
2973
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002974SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
2975 SelectionDAG &DAG) const {
2976 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002977 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002978 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00002979 if (is32Bit(VT))
2980 // Just do a normal 64-bit multiplication and extract the results.
2981 // We define this so that it can be used for constant division.
2982 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
2983 Op.getOperand(1), Ops[1], Ops[0]);
2984 else
2985 // UMUL_LOHI64 returns the low result in the odd register and the high
2986 // result in the even register. UMUL_LOHI is defined to return the
2987 // low half first, so the results are in reverse order.
2988 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2989 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002990 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002991}
2992
2993SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
2994 SelectionDAG &DAG) const {
2995 SDValue Op0 = Op.getOperand(0);
2996 SDValue Op1 = Op.getOperand(1);
2997 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002998 SDLoc DL(Op);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002999 unsigned Opcode;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003000
3001 // We use DSGF for 32-bit division.
3002 if (is32Bit(VT)) {
3003 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Richard Sandiforde6e78852013-07-02 15:40:22 +00003004 Opcode = SystemZISD::SDIVREM32;
3005 } else if (DAG.ComputeNumSignBits(Op1) > 32) {
3006 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
3007 Opcode = SystemZISD::SDIVREM32;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00003008 } else
Richard Sandiforde6e78852013-07-02 15:40:22 +00003009 Opcode = SystemZISD::SDIVREM64;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003010
3011 // DSG(F) takes a 64-bit dividend, so the even register in the GR128
3012 // input is "don't care". The instruction returns the remainder in
3013 // the even register and the quotient in the odd register.
3014 SDValue Ops[2];
Richard Sandiforde6e78852013-07-02 15:40:22 +00003015 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003016 Op0, Op1, Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003017 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003018}
3019
3020SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
3021 SelectionDAG &DAG) const {
3022 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003023 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003024
3025 // DL(G) uses a double-width dividend, so we need to clear the even
3026 // register in the GR128 input. The instruction returns the remainder
3027 // in the even register and the quotient in the odd register.
3028 SDValue Ops[2];
3029 if (is32Bit(VT))
3030 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
3031 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
3032 else
3033 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
3034 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003035 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003036}
3037
3038SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
3039 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
3040
3041 // Get the known-zero masks for each operand.
3042 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
3043 APInt KnownZero[2], KnownOne[2];
Jay Foada0653a32014-05-14 21:14:37 +00003044 DAG.computeKnownBits(Ops[0], KnownZero[0], KnownOne[0]);
3045 DAG.computeKnownBits(Ops[1], KnownZero[1], KnownOne[1]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003046
3047 // See if the upper 32 bits of one operand and the lower 32 bits of the
3048 // other are known zero. They are the low and high operands respectively.
3049 uint64_t Masks[] = { KnownZero[0].getZExtValue(),
3050 KnownZero[1].getZExtValue() };
3051 unsigned High, Low;
3052 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
3053 High = 1, Low = 0;
3054 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
3055 High = 0, Low = 1;
3056 else
3057 return Op;
3058
3059 SDValue LowOp = Ops[Low];
3060 SDValue HighOp = Ops[High];
3061
3062 // If the high part is a constant, we're better off using IILH.
3063 if (HighOp.getOpcode() == ISD::Constant)
3064 return Op;
3065
3066 // If the low part is a constant that is outside the range of LHI,
3067 // then we're better off using IILF.
3068 if (LowOp.getOpcode() == ISD::Constant) {
3069 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
3070 if (!isInt<16>(Value))
3071 return Op;
3072 }
3073
3074 // Check whether the high part is an AND that doesn't change the
3075 // high 32 bits and just masks out low bits. We can skip it if so.
3076 if (HighOp.getOpcode() == ISD::AND &&
3077 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
Richard Sandifordccc2a7c2013-12-03 11:01:54 +00003078 SDValue HighOp0 = HighOp.getOperand(0);
3079 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
3080 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
3081 HighOp = HighOp0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003082 }
3083
3084 // Take advantage of the fact that all GR32 operations only change the
3085 // low 32 bits by truncating Low to an i32 and inserting it directly
3086 // using a subreg. The interesting cases are those where the truncation
3087 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003088 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003089 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
Richard Sandiford87a44362013-09-30 10:28:35 +00003090 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00003091 MVT::i64, HighOp, Low32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003092}
3093
Ulrich Weigandb4012182015-03-31 12:56:33 +00003094SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
3095 SelectionDAG &DAG) const {
3096 EVT VT = Op.getValueType();
Ulrich Weigandb4012182015-03-31 12:56:33 +00003097 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003098 Op = Op.getOperand(0);
3099
3100 // Handle vector types via VPOPCT.
3101 if (VT.isVector()) {
3102 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op);
3103 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00003104 switch (VT.getScalarSizeInBits()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003105 case 8:
3106 break;
3107 case 16: {
3108 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
3109 SDValue Shift = DAG.getConstant(8, DL, MVT::i32);
3110 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift);
3111 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3112 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift);
3113 break;
3114 }
3115 case 32: {
3116 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3117 DAG.getConstant(0, DL, MVT::i32));
3118 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3119 break;
3120 }
3121 case 64: {
3122 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3123 DAG.getConstant(0, DL, MVT::i32));
3124 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp);
3125 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3126 break;
3127 }
3128 default:
3129 llvm_unreachable("Unexpected type");
3130 }
3131 return Op;
3132 }
Ulrich Weigandb4012182015-03-31 12:56:33 +00003133
3134 // Get the known-zero mask for the operand.
Ulrich Weigandb4012182015-03-31 12:56:33 +00003135 APInt KnownZero, KnownOne;
3136 DAG.computeKnownBits(Op, KnownZero, KnownOne);
Ulrich Weigand050527b2015-03-31 19:28:50 +00003137 unsigned NumSignificantBits = (~KnownZero).getActiveBits();
3138 if (NumSignificantBits == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003139 return DAG.getConstant(0, DL, VT);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003140
3141 // Skip known-zero high parts of the operand.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003142 int64_t OrigBitSize = VT.getSizeInBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003143 int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits);
3144 BitSize = std::min(BitSize, OrigBitSize);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003145
3146 // The POPCNT instruction counts the number of bits in each byte.
3147 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op);
3148 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op);
3149 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
3150
3151 // Add up per-byte counts in a binary tree. All bits of Op at
3152 // position larger than BitSize remain zero throughout.
3153 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003154 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003155 if (BitSize != OrigBitSize)
3156 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003157 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003158 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3159 }
3160
3161 // Extract overall result from high byte.
3162 if (BitSize > 8)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003163 Op = DAG.getNode(ISD::SRL, DL, VT, Op,
3164 DAG.getConstant(BitSize - 8, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003165
3166 return Op;
3167}
3168
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003169SDValue SystemZTargetLowering::lowerATOMIC_FENCE(SDValue Op,
3170 SelectionDAG &DAG) const {
3171 SDLoc DL(Op);
3172 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
3173 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
3174 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
3175 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
3176
3177 // The only fence that needs an instruction is a sequentially-consistent
3178 // cross-thread fence.
JF Bastien800f87a2016-04-06 21:19:33 +00003179 if (FenceOrdering == AtomicOrdering::SequentiallyConsistent &&
3180 FenceScope == CrossThread) {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003181 return SDValue(DAG.getMachineNode(SystemZ::Serialize, DL, MVT::Other,
JF Bastien800f87a2016-04-06 21:19:33 +00003182 Op.getOperand(0)),
3183 0);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003184 }
3185
3186 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
3187 return DAG.getNode(SystemZISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0));
3188}
3189
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003190// Op is an atomic load. Lower it into a normal volatile load.
3191SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
3192 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003193 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003194 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
3195 Node->getChain(), Node->getBasePtr(),
3196 Node->getMemoryVT(), Node->getMemOperand());
3197}
3198
3199// Op is an atomic store. Lower it into a normal volatile store followed
3200// by a serialization.
3201SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
3202 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003203 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003204 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
3205 Node->getBasePtr(), Node->getMemoryVT(),
3206 Node->getMemOperand());
3207 return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other,
3208 Chain), 0);
3209}
3210
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003211// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
3212// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003213SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
3214 SelectionDAG &DAG,
3215 unsigned Opcode) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003216 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003217
3218 // 32-bit operations need no code outside the main loop.
3219 EVT NarrowVT = Node->getMemoryVT();
3220 EVT WideVT = MVT::i32;
3221 if (NarrowVT == WideVT)
3222 return Op;
3223
3224 int64_t BitSize = NarrowVT.getSizeInBits();
3225 SDValue ChainIn = Node->getChain();
3226 SDValue Addr = Node->getBasePtr();
3227 SDValue Src2 = Node->getVal();
3228 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003229 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003230 EVT PtrVT = Addr.getValueType();
3231
3232 // Convert atomic subtracts of constants into additions.
3233 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
Richard Sandiford21f5d682014-03-06 11:22:58 +00003234 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003235 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003236 Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003237 }
3238
3239 // Get the address of the containing word.
3240 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003241 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003242
3243 // Get the number of bits that the word must be rotated left in order
3244 // to bring the field to the top bits of a GR32.
3245 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003246 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003247 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3248
3249 // Get the complementing shift amount, for rotating a field in the top
3250 // bits back to its proper position.
3251 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003252 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003253
3254 // Extend the source operand to 32 bits and prepare it for the inner loop.
3255 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
3256 // operations require the source to be shifted in advance. (This shift
3257 // can be folded if the source is constant.) For AND and NAND, the lower
3258 // bits must be set, while for other opcodes they should be left clear.
3259 if (Opcode != SystemZISD::ATOMIC_SWAPW)
3260 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003261 DAG.getConstant(32 - BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003262 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
3263 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
3264 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003265 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003266
3267 // Construct the ATOMIC_LOADW_* node.
3268 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3269 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003270 DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003271 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003272 NarrowVT, MMO);
3273
3274 // Rotate the result of the final CS so that the field is in the lower
3275 // bits of a GR32, then truncate it.
3276 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003277 DAG.getConstant(BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003278 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
3279
3280 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00003281 return DAG.getMergeValues(RetOps, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003282}
3283
Richard Sandiford41350a52013-12-24 15:18:04 +00003284// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
Richard Sandiford002019a2013-12-24 15:22:39 +00003285// into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
Richard Sandiford41350a52013-12-24 15:18:04 +00003286// operations into additions.
3287SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
3288 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003289 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandiford41350a52013-12-24 15:18:04 +00003290 EVT MemVT = Node->getMemoryVT();
3291 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
3292 // A full-width operation.
3293 assert(Op.getValueType() == MemVT && "Mismatched VTs");
3294 SDValue Src2 = Node->getVal();
3295 SDValue NegSrc2;
3296 SDLoc DL(Src2);
3297
Richard Sandiford21f5d682014-03-06 11:22:58 +00003298 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
Richard Sandiford41350a52013-12-24 15:18:04 +00003299 // Use an addition if the operand is constant and either LAA(G) is
3300 // available or the negative value is in the range of A(G)FHI.
3301 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
Eric Christopher93bf97c2014-06-27 07:38:01 +00003302 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003303 NegSrc2 = DAG.getConstant(Value, DL, MemVT);
Eric Christopher93bf97c2014-06-27 07:38:01 +00003304 } else if (Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00003305 // Use LAA(G) if available.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003306 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT),
Richard Sandiford41350a52013-12-24 15:18:04 +00003307 Src2);
3308
3309 if (NegSrc2.getNode())
3310 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
3311 Node->getChain(), Node->getBasePtr(), NegSrc2,
Konstantin Zhuravlyov8ea02462016-10-15 22:01:18 +00003312 Node->getMemOperand());
Richard Sandiford41350a52013-12-24 15:18:04 +00003313
3314 // Use the node as-is.
3315 return Op;
3316 }
3317
3318 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
3319}
3320
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003321// Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
3322// into a fullword ATOMIC_CMP_SWAPW operation.
3323SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
3324 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003325 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003326
3327 // We have native support for 32-bit compare and swap.
3328 EVT NarrowVT = Node->getMemoryVT();
3329 EVT WideVT = MVT::i32;
3330 if (NarrowVT == WideVT)
3331 return Op;
3332
3333 int64_t BitSize = NarrowVT.getSizeInBits();
3334 SDValue ChainIn = Node->getOperand(0);
3335 SDValue Addr = Node->getOperand(1);
3336 SDValue CmpVal = Node->getOperand(2);
3337 SDValue SwapVal = Node->getOperand(3);
3338 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003339 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003340 EVT PtrVT = Addr.getValueType();
3341
3342 // Get the address of the containing word.
3343 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003344 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003345
3346 // Get the number of bits that the word must be rotated left in order
3347 // to bring the field to the top bits of a GR32.
3348 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003349 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003350 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3351
3352 // Get the complementing shift amount, for rotating a field in the top
3353 // bits back to its proper position.
3354 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003355 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003356
3357 // Construct the ATOMIC_CMP_SWAPW node.
3358 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3359 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003360 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003361 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003362 VTList, Ops, NarrowVT, MMO);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003363 return AtomicOp;
3364}
3365
3366SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
3367 SelectionDAG &DAG) const {
3368 MachineFunction &MF = DAG.getMachineFunction();
3369 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003370 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003371 SystemZ::R15D, Op.getValueType());
3372}
3373
3374SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
3375 SelectionDAG &DAG) const {
3376 MachineFunction &MF = DAG.getMachineFunction();
3377 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003378 bool StoreBackchain = MF.getFunction()->hasFnAttribute("backchain");
3379
3380 SDValue Chain = Op.getOperand(0);
3381 SDValue NewSP = Op.getOperand(1);
3382 SDValue Backchain;
3383 SDLoc DL(Op);
3384
3385 if (StoreBackchain) {
3386 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, MVT::i64);
Justin Lebar9c375812016-07-15 18:27:10 +00003387 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003388 }
3389
3390 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R15D, NewSP);
3391
3392 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003393 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003394
3395 return Chain;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003396}
3397
Richard Sandiford03481332013-08-23 11:36:42 +00003398SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
3399 SelectionDAG &DAG) const {
3400 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3401 if (!IsData)
3402 // Just preserve the chain.
3403 return Op.getOperand(0);
3404
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003405 SDLoc DL(Op);
Richard Sandiford03481332013-08-23 11:36:42 +00003406 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
3407 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
Richard Sandiford21f5d682014-03-06 11:22:58 +00003408 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
Richard Sandiford03481332013-08-23 11:36:42 +00003409 SDValue Ops[] = {
3410 Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003411 DAG.getConstant(Code, DL, MVT::i32),
Richard Sandiford03481332013-08-23 11:36:42 +00003412 Op.getOperand(1)
3413 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003414 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003415 Node->getVTList(), Ops,
Richard Sandiford03481332013-08-23 11:36:42 +00003416 Node->getMemoryVT(), Node->getMemOperand());
3417}
3418
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003419// Return an i32 that contains the value of CC immediately after After,
3420// whose final operand must be MVT::Glue.
3421static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003422 SDLoc DL(After);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003423 SDValue Glue = SDValue(After, After->getNumValues() - 1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003424 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
3425 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
3426 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003427}
3428
3429SDValue
3430SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
3431 SelectionDAG &DAG) const {
3432 unsigned Opcode, CCValid;
3433 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) {
3434 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
3435 SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode);
3436 SDValue CC = getCCResult(DAG, Glued.getNode());
3437 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC);
3438 return SDValue();
3439 }
3440
3441 return SDValue();
3442}
3443
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003444SDValue
3445SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
3446 SelectionDAG &DAG) const {
3447 unsigned Opcode, CCValid;
3448 if (isIntrinsicWithCC(Op, Opcode, CCValid)) {
3449 SDValue Glued = emitIntrinsicWithGlue(DAG, Op, Opcode);
3450 SDValue CC = getCCResult(DAG, Glued.getNode());
3451 if (Op->getNumValues() == 1)
3452 return CC;
3453 assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result");
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00003454 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), Glued,
3455 CC);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003456 }
3457
3458 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3459 switch (Id) {
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00003460 case Intrinsic::thread_pointer:
3461 return lowerThreadPointer(SDLoc(Op), DAG);
3462
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003463 case Intrinsic::s390_vpdi:
3464 return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(),
3465 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3466
3467 case Intrinsic::s390_vperm:
3468 return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(),
3469 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3470
3471 case Intrinsic::s390_vuphb:
3472 case Intrinsic::s390_vuphh:
3473 case Intrinsic::s390_vuphf:
3474 return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(),
3475 Op.getOperand(1));
3476
3477 case Intrinsic::s390_vuplhb:
3478 case Intrinsic::s390_vuplhh:
3479 case Intrinsic::s390_vuplhf:
3480 return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(),
3481 Op.getOperand(1));
3482
3483 case Intrinsic::s390_vuplb:
3484 case Intrinsic::s390_vuplhw:
3485 case Intrinsic::s390_vuplf:
3486 return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(),
3487 Op.getOperand(1));
3488
3489 case Intrinsic::s390_vupllb:
3490 case Intrinsic::s390_vupllh:
3491 case Intrinsic::s390_vupllf:
3492 return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(),
3493 Op.getOperand(1));
3494
3495 case Intrinsic::s390_vsumb:
3496 case Intrinsic::s390_vsumh:
3497 case Intrinsic::s390_vsumgh:
3498 case Intrinsic::s390_vsumgf:
3499 case Intrinsic::s390_vsumqf:
3500 case Intrinsic::s390_vsumqg:
3501 return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(),
3502 Op.getOperand(1), Op.getOperand(2));
3503 }
3504
3505 return SDValue();
3506}
3507
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003508namespace {
3509// Says that SystemZISD operation Opcode can be used to perform the equivalent
3510// of a VPERM with permute vector Bytes. If Opcode takes three operands,
3511// Operand is the constant third operand, otherwise it is the number of
3512// bytes in each element of the result.
3513struct Permute {
3514 unsigned Opcode;
3515 unsigned Operand;
3516 unsigned char Bytes[SystemZ::VectorBytes];
3517};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003518}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003519
3520static const Permute PermuteForms[] = {
3521 // VMRHG
3522 { SystemZISD::MERGE_HIGH, 8,
3523 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } },
3524 // VMRHF
3525 { SystemZISD::MERGE_HIGH, 4,
3526 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
3527 // VMRHH
3528 { SystemZISD::MERGE_HIGH, 2,
3529 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
3530 // VMRHB
3531 { SystemZISD::MERGE_HIGH, 1,
3532 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
3533 // VMRLG
3534 { SystemZISD::MERGE_LOW, 8,
3535 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } },
3536 // VMRLF
3537 { SystemZISD::MERGE_LOW, 4,
3538 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
3539 // VMRLH
3540 { SystemZISD::MERGE_LOW, 2,
3541 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
3542 // VMRLB
3543 { SystemZISD::MERGE_LOW, 1,
3544 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
3545 // VPKG
3546 { SystemZISD::PACK, 4,
3547 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } },
3548 // VPKF
3549 { SystemZISD::PACK, 2,
3550 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
3551 // VPKH
3552 { SystemZISD::PACK, 1,
3553 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
3554 // VPDI V1, V2, 4 (low half of V1, high half of V2)
3555 { SystemZISD::PERMUTE_DWORDS, 4,
3556 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } },
3557 // VPDI V1, V2, 1 (high half of V1, low half of V2)
3558 { SystemZISD::PERMUTE_DWORDS, 1,
3559 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } }
3560};
3561
3562// Called after matching a vector shuffle against a particular pattern.
3563// Both the original shuffle and the pattern have two vector operands.
3564// OpNos[0] is the operand of the original shuffle that should be used for
3565// operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything.
3566// OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and
3567// set OpNo0 and OpNo1 to the shuffle operands that should actually be used
3568// for operands 0 and 1 of the pattern.
3569static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) {
3570 if (OpNos[0] < 0) {
3571 if (OpNos[1] < 0)
3572 return false;
3573 OpNo0 = OpNo1 = OpNos[1];
3574 } else if (OpNos[1] < 0) {
3575 OpNo0 = OpNo1 = OpNos[0];
3576 } else {
3577 OpNo0 = OpNos[0];
3578 OpNo1 = OpNos[1];
3579 }
3580 return true;
3581}
3582
3583// Bytes is a VPERM-like permute vector, except that -1 is used for
3584// undefined bytes. Return true if the VPERM can be implemented using P.
3585// When returning true set OpNo0 to the VPERM operand that should be
3586// used for operand 0 of P and likewise OpNo1 for operand 1 of P.
3587//
3588// For example, if swapping the VPERM operands allows P to match, OpNo0
3589// will be 1 and OpNo1 will be 0. If instead Bytes only refers to one
3590// operand, but rewriting it to use two duplicated operands allows it to
3591// match P, then OpNo0 and OpNo1 will be the same.
3592static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P,
3593 unsigned &OpNo0, unsigned &OpNo1) {
3594 int OpNos[] = { -1, -1 };
3595 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
3596 int Elt = Bytes[I];
3597 if (Elt >= 0) {
3598 // Make sure that the two permute vectors use the same suboperand
3599 // byte number. Only the operand numbers (the high bits) are
3600 // allowed to differ.
3601 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1))
3602 return false;
3603 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes;
3604 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes;
3605 // Make sure that the operand mappings are consistent with previous
3606 // elements.
3607 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3608 return false;
3609 OpNos[ModelOpNo] = RealOpNo;
3610 }
3611 }
3612 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3613}
3614
3615// As above, but search for a matching permute.
3616static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes,
3617 unsigned &OpNo0, unsigned &OpNo1) {
3618 for (auto &P : PermuteForms)
3619 if (matchPermute(Bytes, P, OpNo0, OpNo1))
3620 return &P;
3621 return nullptr;
3622}
3623
3624// Bytes is a VPERM-like permute vector, except that -1 is used for
3625// undefined bytes. This permute is an operand of an outer permute.
3626// See whether redistributing the -1 bytes gives a shuffle that can be
3627// implemented using P. If so, set Transform to a VPERM-like permute vector
3628// that, when applied to the result of P, gives the original permute in Bytes.
3629static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3630 const Permute &P,
3631 SmallVectorImpl<int> &Transform) {
3632 unsigned To = 0;
3633 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) {
3634 int Elt = Bytes[From];
3635 if (Elt < 0)
3636 // Byte number From of the result is undefined.
3637 Transform[From] = -1;
3638 else {
3639 while (P.Bytes[To] != Elt) {
3640 To += 1;
3641 if (To == SystemZ::VectorBytes)
3642 return false;
3643 }
3644 Transform[From] = To;
3645 }
3646 }
3647 return true;
3648}
3649
3650// As above, but search for a matching permute.
3651static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3652 SmallVectorImpl<int> &Transform) {
3653 for (auto &P : PermuteForms)
3654 if (matchDoublePermute(Bytes, P, Transform))
3655 return &P;
3656 return nullptr;
3657}
3658
3659// Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask,
3660// as if it had type vNi8.
3661static void getVPermMask(ShuffleVectorSDNode *VSN,
3662 SmallVectorImpl<int> &Bytes) {
3663 EVT VT = VSN->getValueType(0);
3664 unsigned NumElements = VT.getVectorNumElements();
3665 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3666 Bytes.resize(NumElements * BytesPerElement, -1);
3667 for (unsigned I = 0; I < NumElements; ++I) {
3668 int Index = VSN->getMaskElt(I);
3669 if (Index >= 0)
3670 for (unsigned J = 0; J < BytesPerElement; ++J)
3671 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
3672 }
3673}
3674
3675// Bytes is a VPERM-like permute vector, except that -1 is used for
3676// undefined bytes. See whether bytes [Start, Start + BytesPerElement) of
3677// the result come from a contiguous sequence of bytes from one input.
3678// Set Base to the selector for the first byte if so.
3679static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start,
3680 unsigned BytesPerElement, int &Base) {
3681 Base = -1;
3682 for (unsigned I = 0; I < BytesPerElement; ++I) {
3683 if (Bytes[Start + I] >= 0) {
3684 unsigned Elem = Bytes[Start + I];
3685 if (Base < 0) {
3686 Base = Elem - I;
3687 // Make sure the bytes would come from one input operand.
3688 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size())
3689 return false;
3690 } else if (unsigned(Base) != Elem - I)
3691 return false;
3692 }
3693 }
3694 return true;
3695}
3696
3697// Bytes is a VPERM-like permute vector, except that -1 is used for
3698// undefined bytes. Return true if it can be performed using VSLDI.
3699// When returning true, set StartIndex to the shift amount and OpNo0
3700// and OpNo1 to the VPERM operands that should be used as the first
3701// and second shift operand respectively.
3702static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes,
3703 unsigned &StartIndex, unsigned &OpNo0,
3704 unsigned &OpNo1) {
3705 int OpNos[] = { -1, -1 };
3706 int Shift = -1;
3707 for (unsigned I = 0; I < 16; ++I) {
3708 int Index = Bytes[I];
3709 if (Index >= 0) {
3710 int ExpectedShift = (Index - I) % SystemZ::VectorBytes;
3711 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes;
3712 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes;
3713 if (Shift < 0)
3714 Shift = ExpectedShift;
3715 else if (Shift != ExpectedShift)
3716 return false;
3717 // Make sure that the operand mappings are consistent with previous
3718 // elements.
3719 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3720 return false;
3721 OpNos[ModelOpNo] = RealOpNo;
3722 }
3723 }
3724 StartIndex = Shift;
3725 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3726}
3727
3728// Create a node that performs P on operands Op0 and Op1, casting the
3729// operands to the appropriate type. The type of the result is determined by P.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003730static SDValue getPermuteNode(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003731 const Permute &P, SDValue Op0, SDValue Op1) {
3732 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input
3733 // elements of a PACK are twice as wide as the outputs.
3734 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 :
3735 P.Opcode == SystemZISD::PACK ? P.Operand * 2 :
3736 P.Operand);
3737 // Cast both operands to the appropriate type.
3738 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8),
3739 SystemZ::VectorBytes / InBytes);
3740 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0);
3741 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1);
3742 SDValue Op;
3743 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) {
3744 SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32);
3745 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2);
3746 } else if (P.Opcode == SystemZISD::PACK) {
3747 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8),
3748 SystemZ::VectorBytes / P.Operand);
3749 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1);
3750 } else {
3751 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1);
3752 }
3753 return Op;
3754}
3755
3756// Bytes is a VPERM-like permute vector, except that -1 is used for
3757// undefined bytes. Implement it on operands Ops[0] and Ops[1] using
3758// VSLDI or VPERM.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003759static SDValue getGeneralPermuteNode(SelectionDAG &DAG, const SDLoc &DL,
3760 SDValue *Ops,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003761 const SmallVectorImpl<int> &Bytes) {
3762 for (unsigned I = 0; I < 2; ++I)
3763 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]);
3764
3765 // First see whether VSLDI can be used.
3766 unsigned StartIndex, OpNo0, OpNo1;
3767 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1))
3768 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0],
3769 Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32));
3770
3771 // Fall back on VPERM. Construct an SDNode for the permute vector.
3772 SDValue IndexNodes[SystemZ::VectorBytes];
3773 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3774 if (Bytes[I] >= 0)
3775 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32);
3776 else
3777 IndexNodes[I] = DAG.getUNDEF(MVT::i32);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00003778 SDValue Op2 = DAG.getBuildVector(MVT::v16i8, DL, IndexNodes);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003779 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2);
3780}
3781
3782namespace {
3783// Describes a general N-operand vector shuffle.
3784struct GeneralShuffle {
3785 GeneralShuffle(EVT vt) : VT(vt) {}
3786 void addUndef();
3787 void add(SDValue, unsigned);
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003788 SDValue getNode(SelectionDAG &, const SDLoc &);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003789
3790 // The operands of the shuffle.
3791 SmallVector<SDValue, SystemZ::VectorBytes> Ops;
3792
3793 // Index I is -1 if byte I of the result is undefined. Otherwise the
3794 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand
3795 // Bytes[I] / SystemZ::VectorBytes.
3796 SmallVector<int, SystemZ::VectorBytes> Bytes;
3797
3798 // The type of the shuffle result.
3799 EVT VT;
3800};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003801}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003802
3803// Add an extra undefined element to the shuffle.
3804void GeneralShuffle::addUndef() {
3805 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3806 for (unsigned I = 0; I < BytesPerElement; ++I)
3807 Bytes.push_back(-1);
3808}
3809
3810// Add an extra element to the shuffle, taking it from element Elem of Op.
3811// A null Op indicates a vector input whose value will be calculated later;
3812// there is at most one such input per shuffle and it always has the same
3813// type as the result.
3814void GeneralShuffle::add(SDValue Op, unsigned Elem) {
3815 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3816
3817 // The source vector can have wider elements than the result,
3818 // either through an explicit TRUNCATE or because of type legalization.
3819 // We want the least significant part.
3820 EVT FromVT = Op.getNode() ? Op.getValueType() : VT;
3821 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize();
3822 assert(FromBytesPerElement >= BytesPerElement &&
3823 "Invalid EXTRACT_VECTOR_ELT");
3824 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes +
3825 (FromBytesPerElement - BytesPerElement));
3826
3827 // Look through things like shuffles and bitcasts.
3828 while (Op.getNode()) {
3829 if (Op.getOpcode() == ISD::BITCAST)
3830 Op = Op.getOperand(0);
3831 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) {
3832 // See whether the bytes we need come from a contiguous part of one
3833 // operand.
3834 SmallVector<int, SystemZ::VectorBytes> OpBytes;
3835 getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes);
3836 int NewByte;
3837 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte))
3838 break;
3839 if (NewByte < 0) {
3840 addUndef();
3841 return;
3842 }
3843 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes);
3844 Byte = unsigned(NewByte) % SystemZ::VectorBytes;
Sanjay Patel57195842016-03-14 17:28:46 +00003845 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003846 addUndef();
3847 return;
3848 } else
3849 break;
3850 }
3851
3852 // Make sure that the source of the extraction is in Ops.
3853 unsigned OpNo = 0;
3854 for (; OpNo < Ops.size(); ++OpNo)
3855 if (Ops[OpNo] == Op)
3856 break;
3857 if (OpNo == Ops.size())
3858 Ops.push_back(Op);
3859
3860 // Add the element to Bytes.
3861 unsigned Base = OpNo * SystemZ::VectorBytes + Byte;
3862 for (unsigned I = 0; I < BytesPerElement; ++I)
3863 Bytes.push_back(Base + I);
3864}
3865
3866// Return SDNodes for the completed shuffle.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003867SDValue GeneralShuffle::getNode(SelectionDAG &DAG, const SDLoc &DL) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003868 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector");
3869
3870 if (Ops.size() == 0)
3871 return DAG.getUNDEF(VT);
3872
3873 // Make sure that there are at least two shuffle operands.
3874 if (Ops.size() == 1)
3875 Ops.push_back(DAG.getUNDEF(MVT::v16i8));
3876
3877 // Create a tree of shuffles, deferring root node until after the loop.
3878 // Try to redistribute the undefined elements of non-root nodes so that
3879 // the non-root shuffles match something like a pack or merge, then adjust
3880 // the parent node's permute vector to compensate for the new order.
3881 // Among other things, this copes with vectors like <2 x i16> that were
3882 // padded with undefined elements during type legalization.
3883 //
3884 // In the best case this redistribution will lead to the whole tree
3885 // using packs and merges. It should rarely be a loss in other cases.
3886 unsigned Stride = 1;
3887 for (; Stride * 2 < Ops.size(); Stride *= 2) {
3888 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) {
3889 SDValue SubOps[] = { Ops[I], Ops[I + Stride] };
3890
3891 // Create a mask for just these two operands.
3892 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes);
3893 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3894 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes;
3895 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes;
3896 if (OpNo == I)
3897 NewBytes[J] = Byte;
3898 else if (OpNo == I + Stride)
3899 NewBytes[J] = SystemZ::VectorBytes + Byte;
3900 else
3901 NewBytes[J] = -1;
3902 }
3903 // See if it would be better to reorganize NewMask to avoid using VPERM.
3904 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes);
3905 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) {
3906 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]);
3907 // Applying NewBytesMap to Ops[I] gets back to NewBytes.
3908 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3909 if (NewBytes[J] >= 0) {
3910 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes &&
3911 "Invalid double permute");
3912 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J];
3913 } else
3914 assert(NewBytesMap[J] < 0 && "Invalid double permute");
3915 }
3916 } else {
3917 // Just use NewBytes on the operands.
3918 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes);
3919 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J)
3920 if (NewBytes[J] >= 0)
3921 Bytes[J] = I * SystemZ::VectorBytes + J;
3922 }
3923 }
3924 }
3925
3926 // Now we just have 2 inputs. Put the second operand in Ops[1].
3927 if (Stride > 1) {
3928 Ops[1] = Ops[Stride];
3929 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3930 if (Bytes[I] >= int(SystemZ::VectorBytes))
3931 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes;
3932 }
3933
3934 // Look for an instruction that can do the permute without resorting
3935 // to VPERM.
3936 unsigned OpNo0, OpNo1;
3937 SDValue Op;
3938 if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1))
3939 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]);
3940 else
3941 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes);
3942 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3943}
3944
Ulrich Weigandcd808232015-05-05 19:26:48 +00003945// Return true if the given BUILD_VECTOR is a scalar-to-vector conversion.
3946static bool isScalarToVector(SDValue Op) {
3947 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00003948 if (!Op.getOperand(I).isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003949 return false;
3950 return true;
3951}
3952
3953// Return a vector of type VT that contains Value in the first element.
3954// The other elements don't matter.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003955static SDValue buildScalarToVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandcd808232015-05-05 19:26:48 +00003956 SDValue Value) {
3957 // If we have a constant, replicate it to all elements and let the
3958 // BUILD_VECTOR lowering take care of it.
3959 if (Value.getOpcode() == ISD::Constant ||
3960 Value.getOpcode() == ISD::ConstantFP) {
3961 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00003962 return DAG.getBuildVector(VT, DL, Ops);
Ulrich Weigandcd808232015-05-05 19:26:48 +00003963 }
Sanjay Patel57195842016-03-14 17:28:46 +00003964 if (Value.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003965 return DAG.getUNDEF(VT);
3966 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
3967}
3968
3969// Return a vector of type VT in which Op0 is in element 0 and Op1 is in
3970// element 1. Used for cases in which replication is cheap.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003971static SDValue buildMergeScalars(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandcd808232015-05-05 19:26:48 +00003972 SDValue Op0, SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00003973 if (Op0.isUndef()) {
3974 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003975 return DAG.getUNDEF(VT);
3976 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1);
3977 }
Sanjay Patel57195842016-03-14 17:28:46 +00003978 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003979 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0);
3980 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT,
3981 buildScalarToVector(DAG, DL, VT, Op0),
3982 buildScalarToVector(DAG, DL, VT, Op1));
3983}
3984
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003985// Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64
3986// vector for them.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003987static SDValue joinDwords(SelectionDAG &DAG, const SDLoc &DL, SDValue Op0,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003988 SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00003989 if (Op0.isUndef() && Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003990 return DAG.getUNDEF(MVT::v2i64);
3991 // If one of the two inputs is undefined then replicate the other one,
3992 // in order to avoid using another register unnecessarily.
Sanjay Patel57195842016-03-14 17:28:46 +00003993 if (Op0.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003994 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
Sanjay Patel57195842016-03-14 17:28:46 +00003995 else if (Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003996 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
3997 else {
3998 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
3999 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
4000 }
4001 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1);
4002}
4003
4004// Try to represent constant BUILD_VECTOR node BVN using a
4005// SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask
4006// on success.
4007static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) {
4008 EVT ElemVT = BVN->getValueType(0).getVectorElementType();
4009 unsigned BytesPerElement = ElemVT.getStoreSize();
4010 for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) {
4011 SDValue Op = BVN->getOperand(I);
Sanjay Patel75068522016-03-14 18:09:43 +00004012 if (!Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004013 uint64_t Value;
4014 if (Op.getOpcode() == ISD::Constant)
4015 Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue();
4016 else if (Op.getOpcode() == ISD::ConstantFP)
4017 Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt()
4018 .getZExtValue());
4019 else
4020 return false;
4021 for (unsigned J = 0; J < BytesPerElement; ++J) {
4022 uint64_t Byte = (Value >> (J * 8)) & 0xff;
4023 if (Byte == 0xff)
Aaron Ballman2a3aa1f242015-05-11 12:45:53 +00004024 Mask |= 1ULL << ((E - I - 1) * BytesPerElement + J);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004025 else if (Byte != 0)
4026 return false;
4027 }
4028 }
4029 }
4030 return true;
4031}
4032
4033// Try to load a vector constant in which BitsPerElement-bit value Value
4034// is replicated to fill the vector. VT is the type of the resulting
4035// constant, which may have elements of a different size from BitsPerElement.
4036// Return the SDValue of the constant on success, otherwise return
4037// an empty value.
4038static SDValue tryBuildVectorReplicate(SelectionDAG &DAG,
4039 const SystemZInstrInfo *TII,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004040 const SDLoc &DL, EVT VT, uint64_t Value,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004041 unsigned BitsPerElement) {
4042 // Signed 16-bit values can be replicated using VREPI.
4043 int64_t SignedValue = SignExtend64(Value, BitsPerElement);
4044 if (isInt<16>(SignedValue)) {
4045 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
4046 SystemZ::VectorBits / BitsPerElement);
4047 SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT,
4048 DAG.getConstant(SignedValue, DL, MVT::i32));
4049 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4050 }
4051 // See whether rotating the constant left some N places gives a value that
4052 // is one less than a power of 2 (i.e. all zeros followed by all ones).
4053 // If so we can use VGM.
4054 unsigned Start, End;
4055 if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) {
4056 // isRxSBGMask returns the bit numbers for a full 64-bit value,
4057 // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to
4058 // bit numbers for an BitsPerElement value, so that 0 denotes
4059 // 1 << (BitsPerElement-1).
4060 Start -= 64 - BitsPerElement;
4061 End -= 64 - BitsPerElement;
4062 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
4063 SystemZ::VectorBits / BitsPerElement);
4064 SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT,
4065 DAG.getConstant(Start, DL, MVT::i32),
4066 DAG.getConstant(End, DL, MVT::i32));
4067 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4068 }
4069 return SDValue();
4070}
4071
4072// If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually
4073// better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for
4074// the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR
4075// would benefit from this representation and return it if so.
4076static SDValue tryBuildVectorShuffle(SelectionDAG &DAG,
4077 BuildVectorSDNode *BVN) {
4078 EVT VT = BVN->getValueType(0);
4079 unsigned NumElements = VT.getVectorNumElements();
4080
4081 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation
4082 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still
4083 // need a BUILD_VECTOR, add an additional placeholder operand for that
4084 // BUILD_VECTOR and store its operands in ResidueOps.
4085 GeneralShuffle GS(VT);
4086 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps;
4087 bool FoundOne = false;
4088 for (unsigned I = 0; I < NumElements; ++I) {
4089 SDValue Op = BVN->getOperand(I);
4090 if (Op.getOpcode() == ISD::TRUNCATE)
4091 Op = Op.getOperand(0);
4092 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4093 Op.getOperand(1).getOpcode() == ISD::Constant) {
4094 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4095 GS.add(Op.getOperand(0), Elem);
4096 FoundOne = true;
Sanjay Patel57195842016-03-14 17:28:46 +00004097 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004098 GS.addUndef();
4099 } else {
4100 GS.add(SDValue(), ResidueOps.size());
Ulrich Weigande861e642015-09-15 14:27:46 +00004101 ResidueOps.push_back(BVN->getOperand(I));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004102 }
4103 }
4104
4105 // Nothing to do if there are no EXTRACT_VECTOR_ELTs.
4106 if (!FoundOne)
4107 return SDValue();
4108
4109 // Create the BUILD_VECTOR for the remaining elements, if any.
4110 if (!ResidueOps.empty()) {
4111 while (ResidueOps.size() < NumElements)
Ulrich Weigandf4d14f72015-10-08 17:46:59 +00004112 ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType()));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004113 for (auto &Op : GS.Ops) {
4114 if (!Op.getNode()) {
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004115 Op = DAG.getBuildVector(VT, SDLoc(BVN), ResidueOps);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004116 break;
4117 }
4118 }
4119 }
4120 return GS.getNode(DAG, SDLoc(BVN));
4121}
4122
4123// Combine GPR scalar values Elems into a vector of type VT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004124static SDValue buildVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004125 SmallVectorImpl<SDValue> &Elems) {
4126 // See whether there is a single replicated value.
4127 SDValue Single;
4128 unsigned int NumElements = Elems.size();
4129 unsigned int Count = 0;
4130 for (auto Elem : Elems) {
Sanjay Patel75068522016-03-14 18:09:43 +00004131 if (!Elem.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004132 if (!Single.getNode())
4133 Single = Elem;
4134 else if (Elem != Single) {
4135 Single = SDValue();
4136 break;
4137 }
4138 Count += 1;
4139 }
4140 }
4141 // There are three cases here:
4142 //
4143 // - if the only defined element is a loaded one, the best sequence
4144 // is a replicating load.
4145 //
4146 // - otherwise, if the only defined element is an i64 value, we will
4147 // end up with the same VLVGP sequence regardless of whether we short-cut
4148 // for replication or fall through to the later code.
4149 //
4150 // - otherwise, if the only defined element is an i32 or smaller value,
4151 // we would need 2 instructions to replicate it: VLVGP followed by VREPx.
4152 // This is only a win if the single defined element is used more than once.
4153 // In other cases we're better off using a single VLVGx.
4154 if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD))
4155 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single);
4156
4157 // The best way of building a v2i64 from two i64s is to use VLVGP.
4158 if (VT == MVT::v2i64)
4159 return joinDwords(DAG, DL, Elems[0], Elems[1]);
4160
Ulrich Weigandcd808232015-05-05 19:26:48 +00004161 // Use a 64-bit merge high to combine two doubles.
4162 if (VT == MVT::v2f64)
4163 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4164
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004165 // Build v4f32 values directly from the FPRs:
4166 //
4167 // <Axxx> <Bxxx> <Cxxxx> <Dxxx>
4168 // V V VMRHF
4169 // <ABxx> <CDxx>
4170 // V VMRHG
4171 // <ABCD>
4172 if (VT == MVT::v4f32) {
4173 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4174 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]);
4175 // Avoid unnecessary undefs by reusing the other operand.
Sanjay Patel57195842016-03-14 17:28:46 +00004176 if (Op01.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004177 Op01 = Op23;
Sanjay Patel57195842016-03-14 17:28:46 +00004178 else if (Op23.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004179 Op23 = Op01;
4180 // Merging identical replications is a no-op.
4181 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23)
4182 return Op01;
4183 Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01);
4184 Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23);
4185 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH,
4186 DL, MVT::v2i64, Op01, Op23);
4187 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4188 }
4189
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004190 // Collect the constant terms.
4191 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue());
4192 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false);
4193
4194 unsigned NumConstants = 0;
4195 for (unsigned I = 0; I < NumElements; ++I) {
4196 SDValue Elem = Elems[I];
4197 if (Elem.getOpcode() == ISD::Constant ||
4198 Elem.getOpcode() == ISD::ConstantFP) {
4199 NumConstants += 1;
4200 Constants[I] = Elem;
4201 Done[I] = true;
4202 }
4203 }
4204 // If there was at least one constant, fill in the other elements of
4205 // Constants with undefs to get a full vector constant and use that
4206 // as the starting point.
4207 SDValue Result;
4208 if (NumConstants > 0) {
4209 for (unsigned I = 0; I < NumElements; ++I)
4210 if (!Constants[I].getNode())
4211 Constants[I] = DAG.getUNDEF(Elems[I].getValueType());
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004212 Result = DAG.getBuildVector(VT, DL, Constants);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004213 } else {
4214 // Otherwise try to use VLVGP to start the sequence in order to
4215 // avoid a false dependency on any previous contents of the vector
4216 // register. This only makes sense if one of the associated elements
4217 // is defined.
4218 unsigned I1 = NumElements / 2 - 1;
4219 unsigned I2 = NumElements - 1;
Sanjay Patel75068522016-03-14 18:09:43 +00004220 bool Def1 = !Elems[I1].isUndef();
4221 bool Def2 = !Elems[I2].isUndef();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004222 if (Def1 || Def2) {
4223 SDValue Elem1 = Elems[Def1 ? I1 : I2];
4224 SDValue Elem2 = Elems[Def2 ? I2 : I1];
4225 Result = DAG.getNode(ISD::BITCAST, DL, VT,
4226 joinDwords(DAG, DL, Elem1, Elem2));
4227 Done[I1] = true;
4228 Done[I2] = true;
4229 } else
4230 Result = DAG.getUNDEF(VT);
4231 }
4232
4233 // Use VLVGx to insert the other elements.
4234 for (unsigned I = 0; I < NumElements; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00004235 if (!Done[I] && !Elems[I].isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004236 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I],
4237 DAG.getConstant(I, DL, MVT::i32));
4238 return Result;
4239}
4240
4241SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op,
4242 SelectionDAG &DAG) const {
4243 const SystemZInstrInfo *TII =
4244 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
4245 auto *BVN = cast<BuildVectorSDNode>(Op.getNode());
4246 SDLoc DL(Op);
4247 EVT VT = Op.getValueType();
4248
4249 if (BVN->isConstant()) {
4250 // Try using VECTOR GENERATE BYTE MASK. This is the architecturally-
4251 // preferred way of creating all-zero and all-one vectors so give it
4252 // priority over other methods below.
4253 uint64_t Mask = 0;
4254 if (tryBuildVectorByteMask(BVN, Mask)) {
4255 SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
4256 DAG.getConstant(Mask, DL, MVT::i32));
4257 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4258 }
4259
4260 // Try using some form of replication.
4261 APInt SplatBits, SplatUndef;
4262 unsigned SplatBitSize;
4263 bool HasAnyUndefs;
4264 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4265 8, true) &&
4266 SplatBitSize <= 64) {
4267 // First try assuming that any undefined bits above the highest set bit
4268 // and below the lowest set bit are 1s. This increases the likelihood of
4269 // being able to use a sign-extended element value in VECTOR REPLICATE
4270 // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK.
4271 uint64_t SplatBitsZ = SplatBits.getZExtValue();
4272 uint64_t SplatUndefZ = SplatUndef.getZExtValue();
4273 uint64_t Lower = (SplatUndefZ
4274 & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1));
4275 uint64_t Upper = (SplatUndefZ
4276 & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1));
4277 uint64_t Value = SplatBitsZ | Upper | Lower;
4278 SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value,
4279 SplatBitSize);
4280 if (Op.getNode())
4281 return Op;
4282
4283 // Now try assuming that any undefined bits between the first and
4284 // last defined set bits are set. This increases the chances of
4285 // using a non-wraparound mask.
4286 uint64_t Middle = SplatUndefZ & ~Upper & ~Lower;
4287 Value = SplatBitsZ | Middle;
4288 Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize);
4289 if (Op.getNode())
4290 return Op;
4291 }
4292
4293 // Fall back to loading it from memory.
4294 return SDValue();
4295 }
4296
4297 // See if we should use shuffles to construct the vector from other vectors.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004298 if (SDValue Res = tryBuildVectorShuffle(DAG, BVN))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004299 return Res;
4300
Ulrich Weigandcd808232015-05-05 19:26:48 +00004301 // Detect SCALAR_TO_VECTOR conversions.
4302 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op))
4303 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0));
4304
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004305 // Otherwise use buildVector to build the vector up from GPRs.
4306 unsigned NumElements = Op.getNumOperands();
4307 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements);
4308 for (unsigned I = 0; I < NumElements; ++I)
4309 Ops[I] = Op.getOperand(I);
4310 return buildVector(DAG, DL, VT, Ops);
4311}
4312
4313SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
4314 SelectionDAG &DAG) const {
4315 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode());
4316 SDLoc DL(Op);
4317 EVT VT = Op.getValueType();
4318 unsigned NumElements = VT.getVectorNumElements();
4319
4320 if (VSN->isSplat()) {
4321 SDValue Op0 = Op.getOperand(0);
4322 unsigned Index = VSN->getSplatIndex();
4323 assert(Index < VT.getVectorNumElements() &&
4324 "Splat index should be defined and in first operand");
4325 // See whether the value we're splatting is directly available as a scalar.
4326 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4327 Op0.getOpcode() == ISD::BUILD_VECTOR)
4328 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index));
4329 // Otherwise keep it as a vector-to-vector operation.
4330 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0),
4331 DAG.getConstant(Index, DL, MVT::i32));
4332 }
4333
4334 GeneralShuffle GS(VT);
4335 for (unsigned I = 0; I < NumElements; ++I) {
4336 int Elt = VSN->getMaskElt(I);
4337 if (Elt < 0)
4338 GS.addUndef();
4339 else
4340 GS.add(Op.getOperand(unsigned(Elt) / NumElements),
4341 unsigned(Elt) % NumElements);
4342 }
4343 return GS.getNode(DAG, SDLoc(VSN));
4344}
4345
4346SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
4347 SelectionDAG &DAG) const {
4348 SDLoc DL(Op);
4349 // Just insert the scalar into element 0 of an undefined vector.
4350 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL,
4351 Op.getValueType(), DAG.getUNDEF(Op.getValueType()),
4352 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32));
4353}
4354
Ulrich Weigandcd808232015-05-05 19:26:48 +00004355SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
4356 SelectionDAG &DAG) const {
4357 // Handle insertions of floating-point values.
4358 SDLoc DL(Op);
4359 SDValue Op0 = Op.getOperand(0);
4360 SDValue Op1 = Op.getOperand(1);
4361 SDValue Op2 = Op.getOperand(2);
4362 EVT VT = Op.getValueType();
4363
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004364 // Insertions into constant indices of a v2f64 can be done using VPDI.
4365 // However, if the inserted value is a bitcast or a constant then it's
4366 // better to use GPRs, as below.
4367 if (VT == MVT::v2f64 &&
4368 Op1.getOpcode() != ISD::BITCAST &&
Ulrich Weigandcd808232015-05-05 19:26:48 +00004369 Op1.getOpcode() != ISD::ConstantFP &&
4370 Op2.getOpcode() == ISD::Constant) {
4371 uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue();
4372 unsigned Mask = VT.getVectorNumElements() - 1;
4373 if (Index <= Mask)
4374 return Op;
4375 }
4376
4377 // Otherwise bitcast to the equivalent integer form and insert via a GPR.
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004378 MVT IntVT = MVT::getIntegerVT(VT.getScalarSizeInBits());
Ulrich Weigandcd808232015-05-05 19:26:48 +00004379 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements());
4380 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT,
4381 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0),
4382 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2);
4383 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4384}
4385
4386SDValue
4387SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
4388 SelectionDAG &DAG) const {
4389 // Handle extractions of floating-point values.
4390 SDLoc DL(Op);
4391 SDValue Op0 = Op.getOperand(0);
4392 SDValue Op1 = Op.getOperand(1);
4393 EVT VT = Op.getValueType();
4394 EVT VecVT = Op0.getValueType();
4395
4396 // Extractions of constant indices can be done directly.
4397 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) {
4398 uint64_t Index = CIndexN->getZExtValue();
4399 unsigned Mask = VecVT.getVectorNumElements() - 1;
4400 if (Index <= Mask)
4401 return Op;
4402 }
4403
4404 // Otherwise bitcast to the equivalent integer form and extract via a GPR.
4405 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
4406 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements());
4407 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT,
4408 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1);
4409 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4410}
4411
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004412SDValue
4413SystemZTargetLowering::lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004414 unsigned UnpackHigh) const {
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004415 SDValue PackedOp = Op.getOperand(0);
4416 EVT OutVT = Op.getValueType();
4417 EVT InVT = PackedOp.getValueType();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004418 unsigned ToBits = OutVT.getScalarSizeInBits();
4419 unsigned FromBits = InVT.getScalarSizeInBits();
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004420 do {
4421 FromBits *= 2;
4422 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits),
4423 SystemZ::VectorBits / FromBits);
4424 PackedOp = DAG.getNode(UnpackHigh, SDLoc(PackedOp), OutVT, PackedOp);
4425 } while (FromBits != ToBits);
4426 return PackedOp;
4427}
4428
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004429SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG,
4430 unsigned ByScalar) const {
4431 // Look for cases where a vector shift can use the *_BY_SCALAR form.
4432 SDValue Op0 = Op.getOperand(0);
4433 SDValue Op1 = Op.getOperand(1);
4434 SDLoc DL(Op);
4435 EVT VT = Op.getValueType();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004436 unsigned ElemBitSize = VT.getScalarSizeInBits();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004437
4438 // See whether the shift vector is a splat represented as BUILD_VECTOR.
4439 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) {
4440 APInt SplatBits, SplatUndef;
4441 unsigned SplatBitSize;
4442 bool HasAnyUndefs;
4443 // Check for constant splats. Use ElemBitSize as the minimum element
4444 // width and reject splats that need wider elements.
4445 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4446 ElemBitSize, true) &&
4447 SplatBitSize == ElemBitSize) {
4448 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff,
4449 DL, MVT::i32);
4450 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4451 }
4452 // Check for variable splats.
4453 BitVector UndefElements;
4454 SDValue Splat = BVN->getSplatValue(&UndefElements);
4455 if (Splat) {
4456 // Since i32 is the smallest legal type, we either need a no-op
4457 // or a truncation.
4458 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat);
4459 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4460 }
4461 }
4462
4463 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR,
4464 // and the shift amount is directly available in a GPR.
4465 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) {
4466 if (VSN->isSplat()) {
4467 SDValue VSNOp0 = VSN->getOperand(0);
4468 unsigned Index = VSN->getSplatIndex();
4469 assert(Index < VT.getVectorNumElements() &&
4470 "Splat index should be defined and in first operand");
4471 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4472 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) {
4473 // Since i32 is the smallest legal type, we either need a no-op
4474 // or a truncation.
4475 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32,
4476 VSNOp0.getOperand(Index));
4477 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4478 }
4479 }
4480 }
4481
4482 // Otherwise just treat the current form as legal.
4483 return Op;
4484}
4485
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004486SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
4487 SelectionDAG &DAG) const {
4488 switch (Op.getOpcode()) {
Ulrich Weigandf557d082016-04-04 12:44:55 +00004489 case ISD::FRAMEADDR:
4490 return lowerFRAMEADDR(Op, DAG);
4491 case ISD::RETURNADDR:
4492 return lowerRETURNADDR(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004493 case ISD::BR_CC:
4494 return lowerBR_CC(Op, DAG);
4495 case ISD::SELECT_CC:
4496 return lowerSELECT_CC(Op, DAG);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00004497 case ISD::SETCC:
4498 return lowerSETCC(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004499 case ISD::GlobalAddress:
4500 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
4501 case ISD::GlobalTLSAddress:
4502 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
4503 case ISD::BlockAddress:
4504 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
4505 case ISD::JumpTable:
4506 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
4507 case ISD::ConstantPool:
4508 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
4509 case ISD::BITCAST:
4510 return lowerBITCAST(Op, DAG);
4511 case ISD::VASTART:
4512 return lowerVASTART(Op, DAG);
4513 case ISD::VACOPY:
4514 return lowerVACOPY(Op, DAG);
4515 case ISD::DYNAMIC_STACKALLOC:
4516 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +00004517 case ISD::GET_DYNAMIC_AREA_OFFSET:
4518 return lowerGET_DYNAMIC_AREA_OFFSET(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00004519 case ISD::SMUL_LOHI:
4520 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004521 case ISD::UMUL_LOHI:
4522 return lowerUMUL_LOHI(Op, DAG);
4523 case ISD::SDIVREM:
4524 return lowerSDIVREM(Op, DAG);
4525 case ISD::UDIVREM:
4526 return lowerUDIVREM(Op, DAG);
4527 case ISD::OR:
4528 return lowerOR(Op, DAG);
Ulrich Weigandb4012182015-03-31 12:56:33 +00004529 case ISD::CTPOP:
4530 return lowerCTPOP(Op, DAG);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004531 case ISD::ATOMIC_FENCE:
4532 return lowerATOMIC_FENCE(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004533 case ISD::ATOMIC_SWAP:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004534 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
4535 case ISD::ATOMIC_STORE:
4536 return lowerATOMIC_STORE(Op, DAG);
4537 case ISD::ATOMIC_LOAD:
4538 return lowerATOMIC_LOAD(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004539 case ISD::ATOMIC_LOAD_ADD:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004540 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004541 case ISD::ATOMIC_LOAD_SUB:
Richard Sandiford41350a52013-12-24 15:18:04 +00004542 return lowerATOMIC_LOAD_SUB(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004543 case ISD::ATOMIC_LOAD_AND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004544 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004545 case ISD::ATOMIC_LOAD_OR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004546 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004547 case ISD::ATOMIC_LOAD_XOR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004548 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004549 case ISD::ATOMIC_LOAD_NAND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004550 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004551 case ISD::ATOMIC_LOAD_MIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004552 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004553 case ISD::ATOMIC_LOAD_MAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004554 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004555 case ISD::ATOMIC_LOAD_UMIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004556 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004557 case ISD::ATOMIC_LOAD_UMAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004558 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004559 case ISD::ATOMIC_CMP_SWAP:
4560 return lowerATOMIC_CMP_SWAP(Op, DAG);
4561 case ISD::STACKSAVE:
4562 return lowerSTACKSAVE(Op, DAG);
4563 case ISD::STACKRESTORE:
4564 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00004565 case ISD::PREFETCH:
4566 return lowerPREFETCH(Op, DAG);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004567 case ISD::INTRINSIC_W_CHAIN:
4568 return lowerINTRINSIC_W_CHAIN(Op, DAG);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004569 case ISD::INTRINSIC_WO_CHAIN:
4570 return lowerINTRINSIC_WO_CHAIN(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004571 case ISD::BUILD_VECTOR:
4572 return lowerBUILD_VECTOR(Op, DAG);
4573 case ISD::VECTOR_SHUFFLE:
4574 return lowerVECTOR_SHUFFLE(Op, DAG);
4575 case ISD::SCALAR_TO_VECTOR:
4576 return lowerSCALAR_TO_VECTOR(Op, DAG);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004577 case ISD::INSERT_VECTOR_ELT:
4578 return lowerINSERT_VECTOR_ELT(Op, DAG);
4579 case ISD::EXTRACT_VECTOR_ELT:
4580 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004581 case ISD::SIGN_EXTEND_VECTOR_INREG:
4582 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACK_HIGH);
4583 case ISD::ZERO_EXTEND_VECTOR_INREG:
4584 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACKL_HIGH);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004585 case ISD::SHL:
4586 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR);
4587 case ISD::SRL:
4588 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR);
4589 case ISD::SRA:
4590 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004591 default:
4592 llvm_unreachable("Unexpected node to lower");
4593 }
4594}
4595
4596const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
4597#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
Matthias Braund04893f2015-05-07 21:33:59 +00004598 switch ((SystemZISD::NodeType)Opcode) {
4599 case SystemZISD::FIRST_NUMBER: break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004600 OPCODE(RET_FLAG);
4601 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00004602 OPCODE(SIBCALL);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004603 OPCODE(TLS_GDCALL);
4604 OPCODE(TLS_LDCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004605 OPCODE(PCREL_WRAPPER);
Richard Sandiford54b36912013-09-27 15:14:04 +00004606 OPCODE(PCREL_OFFSET);
Richard Sandiford57485472013-12-13 15:35:00 +00004607 OPCODE(IABS);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00004608 OPCODE(ICMP);
4609 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00004610 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004611 OPCODE(BR_CCMASK);
4612 OPCODE(SELECT_CCMASK);
4613 OPCODE(ADJDYNALLOC);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004614 OPCODE(POPCNT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004615 OPCODE(UMUL_LOHI64);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004616 OPCODE(SDIVREM32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004617 OPCODE(SDIVREM64);
4618 OPCODE(UDIVREM32);
4619 OPCODE(UDIVREM64);
Richard Sandifordd131ff82013-07-08 09:35:23 +00004620 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004621 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00004622 OPCODE(NC);
4623 OPCODE(NC_LOOP);
4624 OPCODE(OC);
4625 OPCODE(OC_LOOP);
4626 OPCODE(XC);
4627 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00004628 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004629 OPCODE(CLC_LOOP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00004630 OPCODE(STPCPY);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004631 OPCODE(STRCMP);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00004632 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00004633 OPCODE(IPM);
Richard Sandiford9afe6132013-12-10 10:36:34 +00004634 OPCODE(SERIALIZE);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004635 OPCODE(MEMBARRIER);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004636 OPCODE(TBEGIN);
4637 OPCODE(TBEGIN_NOFLOAT);
4638 OPCODE(TEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004639 OPCODE(BYTE_MASK);
4640 OPCODE(ROTATE_MASK);
4641 OPCODE(REPLICATE);
4642 OPCODE(JOIN_DWORDS);
4643 OPCODE(SPLAT);
4644 OPCODE(MERGE_HIGH);
4645 OPCODE(MERGE_LOW);
4646 OPCODE(SHL_DOUBLE);
4647 OPCODE(PERMUTE_DWORDS);
4648 OPCODE(PERMUTE);
4649 OPCODE(PACK);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004650 OPCODE(PACKS_CC);
4651 OPCODE(PACKLS_CC);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004652 OPCODE(UNPACK_HIGH);
4653 OPCODE(UNPACKL_HIGH);
4654 OPCODE(UNPACK_LOW);
4655 OPCODE(UNPACKL_LOW);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004656 OPCODE(VSHL_BY_SCALAR);
4657 OPCODE(VSRL_BY_SCALAR);
4658 OPCODE(VSRA_BY_SCALAR);
4659 OPCODE(VSUM);
4660 OPCODE(VICMPE);
4661 OPCODE(VICMPH);
4662 OPCODE(VICMPHL);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004663 OPCODE(VICMPES);
4664 OPCODE(VICMPHS);
4665 OPCODE(VICMPHLS);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004666 OPCODE(VFCMPE);
4667 OPCODE(VFCMPH);
4668 OPCODE(VFCMPHE);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004669 OPCODE(VFCMPES);
4670 OPCODE(VFCMPHS);
4671 OPCODE(VFCMPHES);
4672 OPCODE(VFTCI);
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004673 OPCODE(VEXTEND);
4674 OPCODE(VROUND);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004675 OPCODE(VTM);
4676 OPCODE(VFAE_CC);
4677 OPCODE(VFAEZ_CC);
4678 OPCODE(VFEE_CC);
4679 OPCODE(VFEEZ_CC);
4680 OPCODE(VFENE_CC);
4681 OPCODE(VFENEZ_CC);
4682 OPCODE(VISTR_CC);
4683 OPCODE(VSTRC_CC);
4684 OPCODE(VSTRCZ_CC);
Marcin Koscielnicki32e87342016-07-02 02:20:40 +00004685 OPCODE(TDC);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004686 OPCODE(ATOMIC_SWAPW);
4687 OPCODE(ATOMIC_LOADW_ADD);
4688 OPCODE(ATOMIC_LOADW_SUB);
4689 OPCODE(ATOMIC_LOADW_AND);
4690 OPCODE(ATOMIC_LOADW_OR);
4691 OPCODE(ATOMIC_LOADW_XOR);
4692 OPCODE(ATOMIC_LOADW_NAND);
4693 OPCODE(ATOMIC_LOADW_MIN);
4694 OPCODE(ATOMIC_LOADW_MAX);
4695 OPCODE(ATOMIC_LOADW_UMIN);
4696 OPCODE(ATOMIC_LOADW_UMAX);
4697 OPCODE(ATOMIC_CMP_SWAPW);
Bryan Chan28b759c2016-05-16 20:32:22 +00004698 OPCODE(LRV);
4699 OPCODE(STRV);
Richard Sandiford03481332013-08-23 11:36:42 +00004700 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004701 }
Craig Topper062a2ba2014-04-25 05:30:21 +00004702 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004703#undef OPCODE
4704}
4705
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004706// Return true if VT is a vector whose elements are a whole number of bytes
4707// in width.
4708static bool canTreatAsByteVector(EVT VT) {
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004709 return VT.isVector() && VT.getScalarSizeInBits() % 8 == 0;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004710}
4711
4712// Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT
4713// producing a result of type ResVT. Op is a possibly bitcast version
4714// of the input vector and Index is the index (based on type VecVT) that
4715// should be extracted. Return the new extraction if a simplification
4716// was possible or if Force is true.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004717SDValue SystemZTargetLowering::combineExtract(const SDLoc &DL, EVT ResVT,
4718 EVT VecVT, SDValue Op,
4719 unsigned Index,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004720 DAGCombinerInfo &DCI,
4721 bool Force) const {
4722 SelectionDAG &DAG = DCI.DAG;
4723
4724 // The number of bytes being extracted.
4725 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4726
4727 for (;;) {
4728 unsigned Opcode = Op.getOpcode();
4729 if (Opcode == ISD::BITCAST)
4730 // Look through bitcasts.
4731 Op = Op.getOperand(0);
4732 else if (Opcode == ISD::VECTOR_SHUFFLE &&
4733 canTreatAsByteVector(Op.getValueType())) {
4734 // Get a VPERM-like permute mask and see whether the bytes covered
4735 // by the extracted element are a contiguous sequence from one
4736 // source operand.
4737 SmallVector<int, SystemZ::VectorBytes> Bytes;
4738 getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes);
4739 int First;
4740 if (!getShuffleInput(Bytes, Index * BytesPerElement,
4741 BytesPerElement, First))
4742 break;
4743 if (First < 0)
4744 return DAG.getUNDEF(ResVT);
4745 // Make sure the contiguous sequence starts at a multiple of the
4746 // original element size.
4747 unsigned Byte = unsigned(First) % Bytes.size();
4748 if (Byte % BytesPerElement != 0)
4749 break;
4750 // We can get the extracted value directly from an input.
4751 Index = Byte / BytesPerElement;
4752 Op = Op.getOperand(unsigned(First) / Bytes.size());
4753 Force = true;
4754 } else if (Opcode == ISD::BUILD_VECTOR &&
4755 canTreatAsByteVector(Op.getValueType())) {
4756 // We can only optimize this case if the BUILD_VECTOR elements are
4757 // at least as wide as the extracted value.
4758 EVT OpVT = Op.getValueType();
4759 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4760 if (OpBytesPerElement < BytesPerElement)
4761 break;
4762 // Make sure that the least-significant bit of the extracted value
4763 // is the least significant bit of an input.
4764 unsigned End = (Index + 1) * BytesPerElement;
4765 if (End % OpBytesPerElement != 0)
4766 break;
4767 // We're extracting the low part of one operand of the BUILD_VECTOR.
4768 Op = Op.getOperand(End / OpBytesPerElement - 1);
4769 if (!Op.getValueType().isInteger()) {
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00004770 EVT VT = MVT::getIntegerVT(Op.getValueSizeInBits());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004771 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
4772 DCI.AddToWorklist(Op.getNode());
4773 }
4774 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits());
4775 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
4776 if (VT != ResVT) {
4777 DCI.AddToWorklist(Op.getNode());
4778 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op);
4779 }
4780 return Op;
4781 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004782 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG ||
4783 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) &&
4784 canTreatAsByteVector(Op.getValueType()) &&
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004785 canTreatAsByteVector(Op.getOperand(0).getValueType())) {
4786 // Make sure that only the unextended bits are significant.
4787 EVT ExtVT = Op.getValueType();
4788 EVT OpVT = Op.getOperand(0).getValueType();
4789 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize();
4790 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4791 unsigned Byte = Index * BytesPerElement;
4792 unsigned SubByte = Byte % ExtBytesPerElement;
4793 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement;
4794 if (SubByte < MinSubByte ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004795 SubByte + BytesPerElement > ExtBytesPerElement)
4796 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004797 // Get the byte offset of the unextended element
4798 Byte = Byte / ExtBytesPerElement * OpBytesPerElement;
4799 // ...then add the byte offset relative to that element.
4800 Byte += SubByte - MinSubByte;
4801 if (Byte % BytesPerElement != 0)
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004802 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004803 Op = Op.getOperand(0);
4804 Index = Byte / BytesPerElement;
4805 Force = true;
4806 } else
4807 break;
4808 }
4809 if (Force) {
4810 if (Op.getValueType() != VecVT) {
4811 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op);
4812 DCI.AddToWorklist(Op.getNode());
4813 }
4814 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op,
4815 DAG.getConstant(Index, DL, MVT::i32));
4816 }
4817 return SDValue();
4818}
4819
4820// Optimize vector operations in scalar value Op on the basis that Op
4821// is truncated to TruncVT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004822SDValue SystemZTargetLowering::combineTruncateExtract(
4823 const SDLoc &DL, EVT TruncVT, SDValue Op, DAGCombinerInfo &DCI) const {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004824 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into
4825 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements
4826 // of type TruncVT.
4827 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4828 TruncVT.getSizeInBits() % 8 == 0) {
4829 SDValue Vec = Op.getOperand(0);
4830 EVT VecVT = Vec.getValueType();
4831 if (canTreatAsByteVector(VecVT)) {
4832 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
4833 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4834 unsigned TruncBytes = TruncVT.getStoreSize();
4835 if (BytesPerElement % TruncBytes == 0) {
4836 // Calculate the value of Y' in the above description. We are
4837 // splitting the original elements into Scale equal-sized pieces
4838 // and for truncation purposes want the last (least-significant)
4839 // of these pieces for IndexN. This is easiest to do by calculating
4840 // the start index of the following element and then subtracting 1.
4841 unsigned Scale = BytesPerElement / TruncBytes;
4842 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1;
4843
4844 // Defer the creation of the bitcast from X to combineExtract,
4845 // which might be able to optimize the extraction.
4846 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8),
4847 VecVT.getStoreSize() / TruncBytes);
4848 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT);
4849 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true);
4850 }
4851 }
4852 }
4853 }
4854 return SDValue();
4855}
4856
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004857SDValue SystemZTargetLowering::combineSIGN_EXTEND(
4858 SDNode *N, DAGCombinerInfo &DCI) const {
4859 // Convert (sext (ashr (shl X, C1), C2)) to
4860 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
4861 // cheap as narrower ones.
4862 SelectionDAG &DAG = DCI.DAG;
4863 SDValue N0 = N->getOperand(0);
4864 EVT VT = N->getValueType(0);
4865 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
4866 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
4867 SDValue Inner = N0.getOperand(0);
4868 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
4869 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00004870 unsigned Extra = (VT.getSizeInBits() - N0.getValueSizeInBits());
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004871 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
4872 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
4873 EVT ShiftVT = N0.getOperand(1).getValueType();
4874 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
4875 Inner.getOperand(0));
4876 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
4877 DAG.getConstant(NewShlAmt, SDLoc(Inner),
4878 ShiftVT));
4879 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
4880 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT));
4881 }
4882 }
4883 }
4884 return SDValue();
4885}
4886
4887SDValue SystemZTargetLowering::combineMERGE(
4888 SDNode *N, DAGCombinerInfo &DCI) const {
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004889 SelectionDAG &DAG = DCI.DAG;
4890 unsigned Opcode = N->getOpcode();
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004891 SDValue Op0 = N->getOperand(0);
4892 SDValue Op1 = N->getOperand(1);
4893 if (Op0.getOpcode() == ISD::BITCAST)
4894 Op0 = Op0.getOperand(0);
4895 if (Op0.getOpcode() == SystemZISD::BYTE_MASK &&
4896 cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0) {
4897 // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF
4898 // for v4f32.
4899 if (Op1 == N->getOperand(0))
4900 return Op1;
4901 // (z_merge_? 0, X) -> (z_unpackl_? 0, X).
4902 EVT VT = Op1.getValueType();
4903 unsigned ElemBytes = VT.getVectorElementType().getStoreSize();
4904 if (ElemBytes <= 4) {
4905 Opcode = (Opcode == SystemZISD::MERGE_HIGH ?
4906 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW);
4907 EVT InVT = VT.changeVectorElementTypeToInteger();
4908 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16),
4909 SystemZ::VectorBytes / ElemBytes / 2);
4910 if (VT != InVT) {
4911 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1);
4912 DCI.AddToWorklist(Op1.getNode());
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004913 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004914 SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1);
4915 DCI.AddToWorklist(Op.getNode());
4916 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004917 }
4918 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004919 return SDValue();
4920}
4921
4922SDValue SystemZTargetLowering::combineSTORE(
4923 SDNode *N, DAGCombinerInfo &DCI) const {
4924 SelectionDAG &DAG = DCI.DAG;
4925 auto *SN = cast<StoreSDNode>(N);
4926 auto &Op1 = N->getOperand(1);
4927 EVT MemVT = SN->getMemoryVT();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004928 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better
4929 // for the extraction to be done on a vMiN value, so that we can use VSTE.
4930 // If X has wider elements then convert it to:
4931 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z).
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004932 if (MemVT.isInteger()) {
4933 if (SDValue Value =
4934 combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) {
4935 DCI.AddToWorklist(Value.getNode());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004936
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004937 // Rewrite the store with the new form of stored value.
4938 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value,
4939 SN->getBasePtr(), SN->getMemoryVT(),
4940 SN->getMemOperand());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004941 }
4942 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004943 // Combine STORE (BSWAP) into STRVH/STRV/STRVG
4944 // See comment in combineBSWAP about volatile accesses.
4945 if (!SN->isVolatile() &&
4946 Op1.getOpcode() == ISD::BSWAP &&
4947 Op1.getNode()->hasOneUse() &&
4948 (Op1.getValueType() == MVT::i16 ||
4949 Op1.getValueType() == MVT::i32 ||
4950 Op1.getValueType() == MVT::i64)) {
4951
4952 SDValue BSwapOp = Op1.getOperand(0);
4953
4954 if (BSwapOp.getValueType() == MVT::i16)
4955 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), MVT::i32, BSwapOp);
4956
4957 SDValue Ops[] = {
4958 N->getOperand(0), BSwapOp, N->getOperand(2),
4959 DAG.getValueType(Op1.getValueType())
4960 };
4961
4962 return
4963 DAG.getMemIntrinsicNode(SystemZISD::STRV, SDLoc(N), DAG.getVTList(MVT::Other),
4964 Ops, MemVT, SN->getMemOperand());
4965 }
4966 return SDValue();
4967}
4968
4969SDValue SystemZTargetLowering::combineEXTRACT_VECTOR_ELT(
4970 SDNode *N, DAGCombinerInfo &DCI) const {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004971 // Try to simplify a vector extraction.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004972 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
4973 SDValue Op0 = N->getOperand(0);
4974 EVT VecVT = Op0.getValueType();
4975 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0,
4976 IndexN->getZExtValue(), DCI, false);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004977 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004978 return SDValue();
4979}
4980
4981SDValue SystemZTargetLowering::combineJOIN_DWORDS(
4982 SDNode *N, DAGCombinerInfo &DCI) const {
4983 SelectionDAG &DAG = DCI.DAG;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004984 // (join_dwords X, X) == (replicate X)
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004985 if (N->getOperand(0) == N->getOperand(1))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004986 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0),
4987 N->getOperand(0));
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004988 return SDValue();
4989}
4990
4991SDValue SystemZTargetLowering::combineFP_ROUND(
4992 SDNode *N, DAGCombinerInfo &DCI) const {
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00004993 // (fpround (extract_vector_elt X 0))
4994 // (fpround (extract_vector_elt X 1)) ->
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004995 // (extract_vector_elt (VROUND X) 0)
4996 // (extract_vector_elt (VROUND X) 1)
4997 //
4998 // This is a special case since the target doesn't really support v2f32s.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004999 SelectionDAG &DAG = DCI.DAG;
5000 SDValue Op0 = N->getOperand(0);
5001 if (N->getValueType(0) == MVT::f32 &&
5002 Op0.hasOneUse() &&
5003 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5004 Op0.getOperand(0).getValueType() == MVT::v2f64 &&
5005 Op0.getOperand(1).getOpcode() == ISD::Constant &&
5006 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) {
5007 SDValue Vec = Op0.getOperand(0);
5008 for (auto *U : Vec->uses()) {
5009 if (U != Op0.getNode() &&
5010 U->hasOneUse() &&
5011 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5012 U->getOperand(0) == Vec &&
5013 U->getOperand(1).getOpcode() == ISD::Constant &&
5014 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) {
5015 SDValue OtherRound = SDValue(*U->use_begin(), 0);
5016 if (OtherRound.getOpcode() == ISD::FP_ROUND &&
5017 OtherRound.getOperand(0) == SDValue(U, 0) &&
5018 OtherRound.getValueType() == MVT::f32) {
5019 SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N),
5020 MVT::v4f32, Vec);
5021 DCI.AddToWorklist(VRound.getNode());
5022 SDValue Extract1 =
5023 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32,
5024 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32));
5025 DCI.AddToWorklist(Extract1.getNode());
5026 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1);
5027 SDValue Extract0 =
5028 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32,
5029 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
5030 return Extract0;
Ulrich Weigand80b3af72015-05-05 19:27:45 +00005031 }
5032 }
5033 }
5034 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005035 return SDValue();
5036}
Bryan Chan28b759c2016-05-16 20:32:22 +00005037
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005038SDValue SystemZTargetLowering::combineBSWAP(
5039 SDNode *N, DAGCombinerInfo &DCI) const {
5040 SelectionDAG &DAG = DCI.DAG;
Bryan Chan28b759c2016-05-16 20:32:22 +00005041 // Combine BSWAP (LOAD) into LRVH/LRV/LRVG
5042 // These loads are allowed to access memory multiple times, and so we must check
5043 // that the loads are not volatile before performing the combine.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005044 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
5045 N->getOperand(0).hasOneUse() &&
5046 (N->getValueType(0) == MVT::i16 || N->getValueType(0) == MVT::i32 ||
5047 N->getValueType(0) == MVT::i64) &&
5048 !cast<LoadSDNode>(N->getOperand(0))->isVolatile()) {
Bryan Chan28b759c2016-05-16 20:32:22 +00005049 SDValue Load = N->getOperand(0);
5050 LoadSDNode *LD = cast<LoadSDNode>(Load);
5051
5052 // Create the byte-swapping load.
5053 SDValue Ops[] = {
5054 LD->getChain(), // Chain
5055 LD->getBasePtr(), // Ptr
5056 DAG.getValueType(N->getValueType(0)) // VT
5057 };
5058 SDValue BSLoad =
5059 DAG.getMemIntrinsicNode(SystemZISD::LRV, SDLoc(N),
5060 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
5061 MVT::i64 : MVT::i32, MVT::Other),
5062 Ops, LD->getMemoryVT(), LD->getMemOperand());
5063
5064 // If this is an i16 load, insert the truncate.
5065 SDValue ResVal = BSLoad;
5066 if (N->getValueType(0) == MVT::i16)
5067 ResVal = DAG.getNode(ISD::TRUNCATE, SDLoc(N), MVT::i16, BSLoad);
5068
5069 // First, combine the bswap away. This makes the value produced by the
5070 // load dead.
5071 DCI.CombineTo(N, ResVal);
5072
5073 // Next, combine the load away, we give it a bogus result value but a real
5074 // chain result. The result value is dead because the bswap is dead.
5075 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
5076
5077 // Return N so it doesn't get rechecked!
5078 return SDValue(N, 0);
5079 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005080 return SDValue();
5081}
Bryan Chan28b759c2016-05-16 20:32:22 +00005082
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005083SDValue SystemZTargetLowering::combineSHIFTROT(
5084 SDNode *N, DAGCombinerInfo &DCI) const {
5085
5086 SelectionDAG &DAG = DCI.DAG;
5087
5088 // Shift/rotate instructions only use the last 6 bits of the second operand
5089 // register. If the second operand is the result of an AND with an immediate
5090 // value that has its last 6 bits set, we can safely remove the AND operation.
Elliot Colp687691a2016-08-18 18:04:26 +00005091 //
5092 // If the AND operation doesn't have the last 6 bits set, we can't remove it
Elliot Colpa4092102016-08-23 14:03:02 +00005093 // entirely, but we can still truncate it to a 16-bit value. This prevents
5094 // us from ending up with a NILL with a signed operand, which will cause the
5095 // instruction printer to abort.
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005096 SDValue N1 = N->getOperand(1);
5097 if (N1.getOpcode() == ISD::AND) {
Elliot Colp687691a2016-08-18 18:04:26 +00005098 SDValue AndMaskOp = N1->getOperand(1);
5099 auto *AndMask = dyn_cast<ConstantSDNode>(AndMaskOp);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005100
5101 // The AND mask is constant
5102 if (AndMask) {
Elliot Colpa4092102016-08-23 14:03:02 +00005103 auto AmtVal = AndMask->getZExtValue();
5104
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005105 // Bottom 6 bits are set
5106 if ((AmtVal & 0x3f) == 0x3f) {
Elliot Colpa4092102016-08-23 14:03:02 +00005107 SDValue AndOp = N1->getOperand(0);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005108
5109 // This is the only use, so remove the node
5110 if (N1.hasOneUse()) {
5111 // Combine the AND away
5112 DCI.CombineTo(N1.getNode(), AndOp);
5113
5114 // Return N so it isn't rechecked
5115 return SDValue(N, 0);
5116
5117 // The node will be reused, so create a new node for this one use
5118 } else {
5119 SDValue Replace = DAG.getNode(N->getOpcode(), SDLoc(N),
5120 N->getValueType(0), N->getOperand(0),
5121 AndOp);
5122 DCI.AddToWorklist(Replace.getNode());
5123
5124 return Replace;
5125 }
Elliot Colp687691a2016-08-18 18:04:26 +00005126
Elliot Colpa4092102016-08-23 14:03:02 +00005127 // We can't remove the AND, but we can use NILL here (normally we would
5128 // use NILF). Only keep the last 16 bits of the mask. The actual
5129 // transformation will be handled by .td definitions.
5130 } else if (AmtVal >> 16 != 0) {
5131 SDValue AndOp = N1->getOperand(0);
Elliot Colp687691a2016-08-18 18:04:26 +00005132
Elliot Colpa4092102016-08-23 14:03:02 +00005133 auto NewMask = DAG.getConstant(AndMask->getZExtValue() & 0x0000ffff,
5134 SDLoc(AndMaskOp),
5135 AndMaskOp.getValueType());
Elliot Colp687691a2016-08-18 18:04:26 +00005136
Elliot Colpa4092102016-08-23 14:03:02 +00005137 auto NewAnd = DAG.getNode(N1.getOpcode(), SDLoc(N1), N1.getValueType(),
5138 AndOp, NewMask);
Elliot Colp687691a2016-08-18 18:04:26 +00005139
Elliot Colpa4092102016-08-23 14:03:02 +00005140 SDValue Replace = DAG.getNode(N->getOpcode(), SDLoc(N),
5141 N->getValueType(0), N->getOperand(0),
5142 NewAnd);
5143 DCI.AddToWorklist(Replace.getNode());
Elliot Colp687691a2016-08-18 18:04:26 +00005144
Elliot Colpa4092102016-08-23 14:03:02 +00005145 return Replace;
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005146 }
5147 }
5148 }
5149
5150 return SDValue();
5151}
5152
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005153SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
5154 DAGCombinerInfo &DCI) const {
5155 switch(N->getOpcode()) {
5156 default: break;
5157 case ISD::SIGN_EXTEND: return combineSIGN_EXTEND(N, DCI);
5158 case SystemZISD::MERGE_HIGH:
5159 case SystemZISD::MERGE_LOW: return combineMERGE(N, DCI);
5160 case ISD::STORE: return combineSTORE(N, DCI);
5161 case ISD::EXTRACT_VECTOR_ELT: return combineEXTRACT_VECTOR_ELT(N, DCI);
5162 case SystemZISD::JOIN_DWORDS: return combineJOIN_DWORDS(N, DCI);
5163 case ISD::FP_ROUND: return combineFP_ROUND(N, DCI);
5164 case ISD::BSWAP: return combineBSWAP(N, DCI);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005165 case ISD::SHL:
5166 case ISD::SRA:
5167 case ISD::SRL:
5168 case ISD::ROTL: return combineSHIFTROT(N, DCI);
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005169 }
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005170
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005171 return SDValue();
5172}
5173
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005174//===----------------------------------------------------------------------===//
5175// Custom insertion
5176//===----------------------------------------------------------------------===//
5177
5178// Create a new basic block after MBB.
5179static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
5180 MachineFunction &MF = *MBB->getParent();
5181 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00005182 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005183 return NewMBB;
5184}
5185
Richard Sandifordbe133a82013-08-28 09:01:51 +00005186// Split MBB after MI and return the new block (the one that contains
5187// instructions after MI).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005188static MachineBasicBlock *splitBlockAfter(MachineBasicBlock::iterator MI,
Richard Sandifordbe133a82013-08-28 09:01:51 +00005189 MachineBasicBlock *MBB) {
5190 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
5191 NewMBB->splice(NewMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00005192 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Richard Sandifordbe133a82013-08-28 09:01:51 +00005193 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
5194 return NewMBB;
5195}
5196
Richard Sandiford5e318f02013-08-27 09:54:29 +00005197// Split MBB before MI and return the new block (the one that contains MI).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005198static MachineBasicBlock *splitBlockBefore(MachineBasicBlock::iterator MI,
Richard Sandiford5e318f02013-08-27 09:54:29 +00005199 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005200 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005201 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005202 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
5203 return NewMBB;
5204}
5205
Richard Sandiford5e318f02013-08-27 09:54:29 +00005206// Force base value Base into a register before MI. Return the register.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005207static unsigned forceReg(MachineInstr &MI, MachineOperand &Base,
Richard Sandiford5e318f02013-08-27 09:54:29 +00005208 const SystemZInstrInfo *TII) {
5209 if (Base.isReg())
5210 return Base.getReg();
5211
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005212 MachineBasicBlock *MBB = MI.getParent();
Richard Sandiford5e318f02013-08-27 09:54:29 +00005213 MachineFunction &MF = *MBB->getParent();
5214 MachineRegisterInfo &MRI = MF.getRegInfo();
5215
5216 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005217 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LA), Reg)
5218 .addOperand(Base)
5219 .addImm(0)
5220 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005221 return Reg;
5222}
5223
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005224// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
5225MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005226SystemZTargetLowering::emitSelect(MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005227 MachineBasicBlock *MBB) const {
Eric Christophera6734172015-01-31 00:06:45 +00005228 const SystemZInstrInfo *TII =
5229 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005230
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005231 unsigned DestReg = MI.getOperand(0).getReg();
5232 unsigned TrueReg = MI.getOperand(1).getReg();
5233 unsigned FalseReg = MI.getOperand(2).getReg();
5234 unsigned CCValid = MI.getOperand(3).getImm();
5235 unsigned CCMask = MI.getOperand(4).getImm();
5236 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005237
5238 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005239 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005240 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5241
5242 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00005243 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005244 // # fallthrough to FalseMBB
5245 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005246 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5247 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005248 MBB->addSuccessor(JoinMBB);
5249 MBB->addSuccessor(FalseMBB);
5250
5251 // FalseMBB:
5252 // # fallthrough to JoinMBB
5253 MBB = FalseMBB;
5254 MBB->addSuccessor(JoinMBB);
5255
5256 // JoinMBB:
5257 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
5258 // ...
5259 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005260 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005261 .addReg(TrueReg).addMBB(StartMBB)
5262 .addReg(FalseReg).addMBB(FalseMBB);
5263
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005264 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005265 return JoinMBB;
5266}
5267
Richard Sandifordb86a8342013-06-27 09:27:40 +00005268// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
5269// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005270// happen when the condition is false rather than true. If a STORE ON
5271// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005272MachineBasicBlock *SystemZTargetLowering::emitCondStore(MachineInstr &MI,
5273 MachineBasicBlock *MBB,
5274 unsigned StoreOpcode,
5275 unsigned STOCOpcode,
5276 bool Invert) const {
Eric Christophera6734172015-01-31 00:06:45 +00005277 const SystemZInstrInfo *TII =
5278 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordb86a8342013-06-27 09:27:40 +00005279
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005280 unsigned SrcReg = MI.getOperand(0).getReg();
5281 MachineOperand Base = MI.getOperand(1);
5282 int64_t Disp = MI.getOperand(2).getImm();
5283 unsigned IndexReg = MI.getOperand(3).getReg();
5284 unsigned CCValid = MI.getOperand(4).getImm();
5285 unsigned CCMask = MI.getOperand(5).getImm();
5286 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005287
5288 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
5289
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005290 // Use STOCOpcode if possible. We could use different store patterns in
5291 // order to avoid matching the index register, but the performance trade-offs
5292 // might be more complicated in that case.
Eric Christopher93bf97c2014-06-27 07:38:01 +00005293 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005294 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005295 CCMask ^= CCValid;
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005296 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +00005297 .addReg(SrcReg).addOperand(Base).addImm(Disp)
5298 .addImm(CCValid).addImm(CCMask);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005299 MI.eraseFromParent();
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005300 return MBB;
5301 }
5302
Richard Sandifordb86a8342013-06-27 09:27:40 +00005303 // Get the condition needed to branch around the store.
5304 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005305 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00005306
5307 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005308 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005309 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5310
5311 // StartMBB:
5312 // BRC CCMask, JoinMBB
5313 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00005314 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005315 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5316 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005317 MBB->addSuccessor(JoinMBB);
5318 MBB->addSuccessor(FalseMBB);
5319
5320 // FalseMBB:
5321 // store %SrcReg, %Disp(%Index,%Base)
5322 // # fallthrough to JoinMBB
5323 MBB = FalseMBB;
5324 BuildMI(MBB, DL, TII->get(StoreOpcode))
5325 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
5326 MBB->addSuccessor(JoinMBB);
5327
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005328 MI.eraseFromParent();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005329 return JoinMBB;
5330}
5331
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005332// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
5333// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
5334// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
5335// BitSize is the width of the field in bits, or 0 if this is a partword
5336// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
5337// is one of the operands. Invert says whether the field should be
5338// inverted after performing BinOpcode (e.g. for NAND).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005339MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadBinary(
5340 MachineInstr &MI, MachineBasicBlock *MBB, unsigned BinOpcode,
5341 unsigned BitSize, bool Invert) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005342 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005343 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005344 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005345 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005346 bool IsSubWord = (BitSize < 32);
5347
5348 // Extract the operands. Base can be a register or a frame index.
5349 // Src2 can be a register or immediate.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005350 unsigned Dest = MI.getOperand(0).getReg();
5351 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5352 int64_t Disp = MI.getOperand(2).getImm();
5353 MachineOperand Src2 = earlyUseOperand(MI.getOperand(3));
5354 unsigned BitShift = (IsSubWord ? MI.getOperand(4).getReg() : 0);
5355 unsigned NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : 0);
5356 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005357 if (IsSubWord)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005358 BitSize = MI.getOperand(6).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005359
5360 // Subword operations use 32-bit registers.
5361 const TargetRegisterClass *RC = (BitSize <= 32 ?
5362 &SystemZ::GR32BitRegClass :
5363 &SystemZ::GR64BitRegClass);
5364 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5365 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5366
5367 // Get the right opcodes for the displacement.
5368 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5369 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5370 assert(LOpcode && CSOpcode && "Displacement out of range");
5371
5372 // Create virtual registers for temporary results.
5373 unsigned OrigVal = MRI.createVirtualRegister(RC);
5374 unsigned OldVal = MRI.createVirtualRegister(RC);
5375 unsigned NewVal = (BinOpcode || IsSubWord ?
5376 MRI.createVirtualRegister(RC) : Src2.getReg());
5377 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5378 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5379
5380 // Insert a basic block for the main loop.
5381 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005382 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005383 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5384
5385 // StartMBB:
5386 // ...
5387 // %OrigVal = L Disp(%Base)
5388 // # fall through to LoopMMB
5389 MBB = StartMBB;
5390 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
5391 .addOperand(Base).addImm(Disp).addReg(0);
5392 MBB->addSuccessor(LoopMBB);
5393
5394 // LoopMBB:
5395 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
5396 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5397 // %RotatedNewVal = OP %RotatedOldVal, %Src2
5398 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5399 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5400 // JNE LoopMBB
5401 // # fall through to DoneMMB
5402 MBB = LoopMBB;
5403 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5404 .addReg(OrigVal).addMBB(StartMBB)
5405 .addReg(Dest).addMBB(LoopMBB);
5406 if (IsSubWord)
5407 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5408 .addReg(OldVal).addReg(BitShift).addImm(0);
5409 if (Invert) {
5410 // Perform the operation normally and then invert every bit of the field.
5411 unsigned Tmp = MRI.createVirtualRegister(RC);
5412 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
5413 .addReg(RotatedOldVal).addOperand(Src2);
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005414 if (BitSize <= 32)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005415 // XILF with the upper BitSize bits set.
Richard Sandiford652784e2013-09-25 11:11:53 +00005416 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005417 .addReg(Tmp).addImm(-1U << (32 - BitSize));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005418 else {
5419 // Use LCGR and add -1 to the result, which is more compact than
5420 // an XILF, XILH pair.
5421 unsigned Tmp2 = MRI.createVirtualRegister(RC);
5422 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
5423 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
5424 .addReg(Tmp2).addImm(-1);
5425 }
5426 } else if (BinOpcode)
5427 // A simply binary operation.
5428 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
5429 .addReg(RotatedOldVal).addOperand(Src2);
5430 else if (IsSubWord)
5431 // Use RISBG to rotate Src2 into position and use it to replace the
5432 // field in RotatedOldVal.
5433 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
5434 .addReg(RotatedOldVal).addReg(Src2.getReg())
5435 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
5436 if (IsSubWord)
5437 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5438 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5439 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
5440 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005441 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5442 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005443 MBB->addSuccessor(LoopMBB);
5444 MBB->addSuccessor(DoneMBB);
5445
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005446 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005447 return DoneMBB;
5448}
5449
5450// Implement EmitInstrWithCustomInserter for pseudo
5451// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
5452// instruction that should be used to compare the current field with the
5453// minimum or maximum value. KeepOldMask is the BRC condition-code mask
5454// for when the current field should be kept. BitSize is the width of
5455// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005456MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadMinMax(
5457 MachineInstr &MI, MachineBasicBlock *MBB, unsigned CompareOpcode,
5458 unsigned KeepOldMask, unsigned BitSize) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005459 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005460 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005461 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005462 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005463 bool IsSubWord = (BitSize < 32);
5464
5465 // Extract the operands. Base can be a register or a frame index.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005466 unsigned Dest = MI.getOperand(0).getReg();
5467 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5468 int64_t Disp = MI.getOperand(2).getImm();
5469 unsigned Src2 = MI.getOperand(3).getReg();
5470 unsigned BitShift = (IsSubWord ? MI.getOperand(4).getReg() : 0);
5471 unsigned NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : 0);
5472 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005473 if (IsSubWord)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005474 BitSize = MI.getOperand(6).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005475
5476 // Subword operations use 32-bit registers.
5477 const TargetRegisterClass *RC = (BitSize <= 32 ?
5478 &SystemZ::GR32BitRegClass :
5479 &SystemZ::GR64BitRegClass);
5480 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5481 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5482
5483 // Get the right opcodes for the displacement.
5484 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5485 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5486 assert(LOpcode && CSOpcode && "Displacement out of range");
5487
5488 // Create virtual registers for temporary results.
5489 unsigned OrigVal = MRI.createVirtualRegister(RC);
5490 unsigned OldVal = MRI.createVirtualRegister(RC);
5491 unsigned NewVal = MRI.createVirtualRegister(RC);
5492 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5493 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
5494 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5495
5496 // Insert 3 basic blocks for the loop.
5497 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005498 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005499 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5500 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
5501 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
5502
5503 // StartMBB:
5504 // ...
5505 // %OrigVal = L Disp(%Base)
5506 // # fall through to LoopMMB
5507 MBB = StartMBB;
5508 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
5509 .addOperand(Base).addImm(Disp).addReg(0);
5510 MBB->addSuccessor(LoopMBB);
5511
5512 // LoopMBB:
5513 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
5514 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5515 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00005516 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005517 MBB = LoopMBB;
5518 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5519 .addReg(OrigVal).addMBB(StartMBB)
5520 .addReg(Dest).addMBB(UpdateMBB);
5521 if (IsSubWord)
5522 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5523 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005524 BuildMI(MBB, DL, TII->get(CompareOpcode))
5525 .addReg(RotatedOldVal).addReg(Src2);
5526 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005527 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005528 MBB->addSuccessor(UpdateMBB);
5529 MBB->addSuccessor(UseAltMBB);
5530
5531 // UseAltMBB:
5532 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
5533 // # fall through to UpdateMMB
5534 MBB = UseAltMBB;
5535 if (IsSubWord)
5536 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
5537 .addReg(RotatedOldVal).addReg(Src2)
5538 .addImm(32).addImm(31 + BitSize).addImm(0);
5539 MBB->addSuccessor(UpdateMBB);
5540
5541 // UpdateMBB:
5542 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
5543 // [ %RotatedAltVal, UseAltMBB ]
5544 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5545 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5546 // JNE LoopMBB
5547 // # fall through to DoneMMB
5548 MBB = UpdateMBB;
5549 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
5550 .addReg(RotatedOldVal).addMBB(LoopMBB)
5551 .addReg(RotatedAltVal).addMBB(UseAltMBB);
5552 if (IsSubWord)
5553 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5554 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5555 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
5556 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005557 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5558 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005559 MBB->addSuccessor(LoopMBB);
5560 MBB->addSuccessor(DoneMBB);
5561
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005562 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005563 return DoneMBB;
5564}
5565
5566// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
5567// instruction MI.
5568MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005569SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005570 MachineBasicBlock *MBB) const {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00005571
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005572 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005573 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005574 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005575 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005576
5577 // Extract the operands. Base can be a register or a frame index.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005578 unsigned Dest = MI.getOperand(0).getReg();
5579 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5580 int64_t Disp = MI.getOperand(2).getImm();
5581 unsigned OrigCmpVal = MI.getOperand(3).getReg();
5582 unsigned OrigSwapVal = MI.getOperand(4).getReg();
5583 unsigned BitShift = MI.getOperand(5).getReg();
5584 unsigned NegBitShift = MI.getOperand(6).getReg();
5585 int64_t BitSize = MI.getOperand(7).getImm();
5586 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005587
5588 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
5589
5590 // Get the right opcodes for the displacement.
5591 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
5592 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
5593 assert(LOpcode && CSOpcode && "Displacement out of range");
5594
5595 // Create virtual registers for temporary results.
5596 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
5597 unsigned OldVal = MRI.createVirtualRegister(RC);
5598 unsigned CmpVal = MRI.createVirtualRegister(RC);
5599 unsigned SwapVal = MRI.createVirtualRegister(RC);
5600 unsigned StoreVal = MRI.createVirtualRegister(RC);
5601 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
5602 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
5603 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
5604
5605 // Insert 2 basic blocks for the loop.
5606 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005607 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005608 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5609 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
5610
5611 // StartMBB:
5612 // ...
5613 // %OrigOldVal = L Disp(%Base)
5614 // # fall through to LoopMMB
5615 MBB = StartMBB;
5616 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
5617 .addOperand(Base).addImm(Disp).addReg(0);
5618 MBB->addSuccessor(LoopMBB);
5619
5620 // LoopMBB:
5621 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
5622 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
5623 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
5624 // %Dest = RLL %OldVal, BitSize(%BitShift)
5625 // ^^ The low BitSize bits contain the field
5626 // of interest.
5627 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
5628 // ^^ Replace the upper 32-BitSize bits of the
5629 // comparison value with those that we loaded,
5630 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005631 // CR %Dest, %RetryCmpVal
5632 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005633 // # Fall through to SetMBB
5634 MBB = LoopMBB;
5635 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5636 .addReg(OrigOldVal).addMBB(StartMBB)
5637 .addReg(RetryOldVal).addMBB(SetMBB);
5638 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
5639 .addReg(OrigCmpVal).addMBB(StartMBB)
5640 .addReg(RetryCmpVal).addMBB(SetMBB);
5641 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
5642 .addReg(OrigSwapVal).addMBB(StartMBB)
5643 .addReg(RetrySwapVal).addMBB(SetMBB);
5644 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
5645 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
5646 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
5647 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005648 BuildMI(MBB, DL, TII->get(SystemZ::CR))
5649 .addReg(Dest).addReg(RetryCmpVal);
5650 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005651 .addImm(SystemZ::CCMASK_ICMP)
5652 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005653 MBB->addSuccessor(DoneMBB);
5654 MBB->addSuccessor(SetMBB);
5655
5656 // SetMBB:
5657 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
5658 // ^^ Replace the upper 32-BitSize bits of the new
5659 // value with those that we loaded.
5660 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
5661 // ^^ Rotate the new field to its proper position.
5662 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
5663 // JNE LoopMBB
5664 // # fall through to ExitMMB
5665 MBB = SetMBB;
5666 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
5667 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
5668 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
5669 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
5670 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
5671 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005672 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5673 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005674 MBB->addSuccessor(LoopMBB);
5675 MBB->addSuccessor(DoneMBB);
5676
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005677 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005678 return DoneMBB;
5679}
5680
5681// Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true
5682// if the high register of the GR128 value must be cleared or false if
Richard Sandiford87a44362013-09-30 10:28:35 +00005683// it's "don't care". SubReg is subreg_l32 when extending a GR32
5684// and subreg_l64 when extending a GR64.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005685MachineBasicBlock *SystemZTargetLowering::emitExt128(MachineInstr &MI,
5686 MachineBasicBlock *MBB,
5687 bool ClearEven,
5688 unsigned SubReg) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005689 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005690 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005691 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005692 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005693 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005694
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005695 unsigned Dest = MI.getOperand(0).getReg();
5696 unsigned Src = MI.getOperand(1).getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005697 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
5698
5699 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
5700 if (ClearEven) {
5701 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
5702 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
5703
5704 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
5705 .addImm(0);
5706 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
Richard Sandiford87a44362013-09-30 10:28:35 +00005707 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005708 In128 = NewIn128;
5709 }
5710 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
5711 .addReg(In128).addReg(Src).addImm(SubReg);
5712
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005713 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005714 return MBB;
5715}
5716
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005717MachineBasicBlock *SystemZTargetLowering::emitMemMemWrapper(
5718 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +00005719 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005720 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005721 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandiford5e318f02013-08-27 09:54:29 +00005722 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005723 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordd131ff82013-07-08 09:35:23 +00005724
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005725 MachineOperand DestBase = earlyUseOperand(MI.getOperand(0));
5726 uint64_t DestDisp = MI.getOperand(1).getImm();
5727 MachineOperand SrcBase = earlyUseOperand(MI.getOperand(2));
5728 uint64_t SrcDisp = MI.getOperand(3).getImm();
5729 uint64_t Length = MI.getOperand(4).getImm();
Richard Sandifordd131ff82013-07-08 09:35:23 +00005730
Richard Sandifordbe133a82013-08-28 09:01:51 +00005731 // When generating more than one CLC, all but the last will need to
5732 // branch to the end when a difference is found.
5733 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
Craig Topper062a2ba2014-04-25 05:30:21 +00005734 splitBlockAfter(MI, MBB) : nullptr);
Richard Sandifordbe133a82013-08-28 09:01:51 +00005735
Richard Sandiford5e318f02013-08-27 09:54:29 +00005736 // Check for the loop form, in which operand 5 is the trip count.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005737 if (MI.getNumExplicitOperands() > 5) {
Richard Sandiford5e318f02013-08-27 09:54:29 +00005738 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
5739
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005740 uint64_t StartCountReg = MI.getOperand(5).getReg();
Richard Sandiford5e318f02013-08-27 09:54:29 +00005741 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
5742 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
5743 forceReg(MI, DestBase, TII));
5744
5745 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
5746 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
5747 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
5748 MRI.createVirtualRegister(RC));
5749 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
5750 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
5751 MRI.createVirtualRegister(RC));
5752
5753 RC = &SystemZ::GR64BitRegClass;
5754 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
5755 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
5756
5757 MachineBasicBlock *StartMBB = MBB;
5758 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
5759 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00005760 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005761
5762 // StartMBB:
5763 // # fall through to LoopMMB
5764 MBB->addSuccessor(LoopMBB);
5765
5766 // LoopMBB:
5767 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005768 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00005769 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005770 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00005771 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005772 // [ %NextCountReg, NextMBB ]
5773 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00005774 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00005775 // ( JLH EndMBB )
5776 //
5777 // The prefetch is used only for MVC. The JLH is used only for CLC.
5778 MBB = LoopMBB;
5779
5780 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
5781 .addReg(StartDestReg).addMBB(StartMBB)
5782 .addReg(NextDestReg).addMBB(NextMBB);
5783 if (!HaveSingleBase)
5784 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
5785 .addReg(StartSrcReg).addMBB(StartMBB)
5786 .addReg(NextSrcReg).addMBB(NextMBB);
5787 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
5788 .addReg(StartCountReg).addMBB(StartMBB)
5789 .addReg(NextCountReg).addMBB(NextMBB);
5790 if (Opcode == SystemZ::MVC)
5791 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
5792 .addImm(SystemZ::PFD_WRITE)
5793 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
5794 BuildMI(MBB, DL, TII->get(Opcode))
5795 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
5796 .addReg(ThisSrcReg).addImm(SrcDisp);
5797 if (EndMBB) {
5798 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5799 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5800 .addMBB(EndMBB);
5801 MBB->addSuccessor(EndMBB);
5802 MBB->addSuccessor(NextMBB);
5803 }
5804
5805 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00005806 // %NextDestReg = LA 256(%ThisDestReg)
5807 // %NextSrcReg = LA 256(%ThisSrcReg)
5808 // %NextCountReg = AGHI %ThisCountReg, -1
5809 // CGHI %NextCountReg, 0
5810 // JLH LoopMBB
5811 // # fall through to DoneMMB
5812 //
5813 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00005814 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005815
Richard Sandiford5e318f02013-08-27 09:54:29 +00005816 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
5817 .addReg(ThisDestReg).addImm(256).addReg(0);
5818 if (!HaveSingleBase)
5819 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
5820 .addReg(ThisSrcReg).addImm(256).addReg(0);
5821 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
5822 .addReg(ThisCountReg).addImm(-1);
5823 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
5824 .addReg(NextCountReg).addImm(0);
5825 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5826 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5827 .addMBB(LoopMBB);
5828 MBB->addSuccessor(LoopMBB);
5829 MBB->addSuccessor(DoneMBB);
5830
5831 DestBase = MachineOperand::CreateReg(NextDestReg, false);
5832 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
5833 Length &= 255;
5834 MBB = DoneMBB;
5835 }
5836 // Handle any remaining bytes with straight-line code.
5837 while (Length > 0) {
5838 uint64_t ThisLength = std::min(Length, uint64_t(256));
5839 // The previous iteration might have created out-of-range displacements.
5840 // Apply them using LAY if so.
5841 if (!isUInt<12>(DestDisp)) {
5842 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005843 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg)
5844 .addOperand(DestBase)
5845 .addImm(DestDisp)
5846 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005847 DestBase = MachineOperand::CreateReg(Reg, false);
5848 DestDisp = 0;
5849 }
5850 if (!isUInt<12>(SrcDisp)) {
5851 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005852 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg)
5853 .addOperand(SrcBase)
5854 .addImm(SrcDisp)
5855 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005856 SrcBase = MachineOperand::CreateReg(Reg, false);
5857 SrcDisp = 0;
5858 }
5859 BuildMI(*MBB, MI, DL, TII->get(Opcode))
5860 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength)
5861 .addOperand(SrcBase).addImm(SrcDisp);
5862 DestDisp += ThisLength;
5863 SrcDisp += ThisLength;
5864 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00005865 // If there's another CLC to go, branch to the end if a difference
5866 // was found.
5867 if (EndMBB && Length > 0) {
5868 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
5869 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5870 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5871 .addMBB(EndMBB);
5872 MBB->addSuccessor(EndMBB);
5873 MBB->addSuccessor(NextMBB);
5874 MBB = NextMBB;
5875 }
5876 }
5877 if (EndMBB) {
5878 MBB->addSuccessor(EndMBB);
5879 MBB = EndMBB;
5880 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005881 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00005882
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005883 MI.eraseFromParent();
Richard Sandifordd131ff82013-07-08 09:35:23 +00005884 return MBB;
5885}
5886
Richard Sandifordca232712013-08-16 11:21:54 +00005887// Decompose string pseudo-instruction MI into a loop that continually performs
5888// Opcode until CC != 3.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005889MachineBasicBlock *SystemZTargetLowering::emitStringWrapper(
5890 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Richard Sandifordca232712013-08-16 11:21:54 +00005891 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005892 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005893 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordca232712013-08-16 11:21:54 +00005894 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005895 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordca232712013-08-16 11:21:54 +00005896
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005897 uint64_t End1Reg = MI.getOperand(0).getReg();
5898 uint64_t Start1Reg = MI.getOperand(1).getReg();
5899 uint64_t Start2Reg = MI.getOperand(2).getReg();
5900 uint64_t CharReg = MI.getOperand(3).getReg();
Richard Sandifordca232712013-08-16 11:21:54 +00005901
5902 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
5903 uint64_t This1Reg = MRI.createVirtualRegister(RC);
5904 uint64_t This2Reg = MRI.createVirtualRegister(RC);
5905 uint64_t End2Reg = MRI.createVirtualRegister(RC);
5906
5907 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005908 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00005909 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5910
5911 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00005912 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00005913 MBB->addSuccessor(LoopMBB);
5914
5915 // LoopMBB:
5916 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
5917 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford7789b082013-09-30 08:48:38 +00005918 // R0L = %CharReg
5919 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
Richard Sandifordca232712013-08-16 11:21:54 +00005920 // JO LoopMBB
5921 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00005922 //
Richard Sandiford7789b082013-09-30 08:48:38 +00005923 // The load of R0L can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00005924 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00005925
5926 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
5927 .addReg(Start1Reg).addMBB(StartMBB)
5928 .addReg(End1Reg).addMBB(LoopMBB);
5929 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
5930 .addReg(Start2Reg).addMBB(StartMBB)
5931 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford7789b082013-09-30 08:48:38 +00005932 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00005933 BuildMI(MBB, DL, TII->get(Opcode))
5934 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
5935 .addReg(This1Reg).addReg(This2Reg);
5936 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5937 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
5938 MBB->addSuccessor(LoopMBB);
5939 MBB->addSuccessor(DoneMBB);
5940
5941 DoneMBB->addLiveIn(SystemZ::CC);
5942
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005943 MI.eraseFromParent();
Richard Sandifordca232712013-08-16 11:21:54 +00005944 return DoneMBB;
5945}
5946
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005947// Update TBEGIN instruction with final opcode and register clobbers.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005948MachineBasicBlock *SystemZTargetLowering::emitTransactionBegin(
5949 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode,
5950 bool NoFloat) const {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005951 MachineFunction &MF = *MBB->getParent();
5952 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
5953 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
5954
5955 // Update opcode.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005956 MI.setDesc(TII->get(Opcode));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005957
5958 // We cannot handle a TBEGIN that clobbers the stack or frame pointer.
5959 // Make sure to add the corresponding GRSM bits if they are missing.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005960 uint64_t Control = MI.getOperand(2).getImm();
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005961 static const unsigned GPRControlBit[16] = {
5962 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000,
5963 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100
5964 };
5965 Control |= GPRControlBit[15];
5966 if (TFI->hasFP(MF))
5967 Control |= GPRControlBit[11];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005968 MI.getOperand(2).setImm(Control);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005969
5970 // Add GPR clobbers.
5971 for (int I = 0; I < 16; I++) {
5972 if ((Control & GPRControlBit[I]) == 0) {
5973 unsigned Reg = SystemZMC::GR64Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005974 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005975 }
5976 }
5977
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005978 // Add FPR/VR clobbers.
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005979 if (!NoFloat && (Control & 4) != 0) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005980 if (Subtarget.hasVector()) {
5981 for (int I = 0; I < 32; I++) {
5982 unsigned Reg = SystemZMC::VR128Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005983 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005984 }
5985 } else {
5986 for (int I = 0; I < 16; I++) {
5987 unsigned Reg = SystemZMC::FP64Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005988 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005989 }
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005990 }
5991 }
5992
5993 return MBB;
5994}
5995
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005996MachineBasicBlock *SystemZTargetLowering::emitLoadAndTestCmp0(
5997 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00005998 MachineFunction &MF = *MBB->getParent();
5999 MachineRegisterInfo *MRI = &MF.getRegInfo();
6000 const SystemZInstrInfo *TII =
6001 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006002 DebugLoc DL = MI.getDebugLoc();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006003
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006004 unsigned SrcReg = MI.getOperand(0).getReg();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006005
6006 // Create new virtual register of the same class as source.
6007 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
6008 unsigned DstReg = MRI->createVirtualRegister(RC);
6009
6010 // Replace pseudo with a normal load-and-test that models the def as
6011 // well.
6012 BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg)
6013 .addReg(SrcReg);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006014 MI.eraseFromParent();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006015
6016 return MBB;
6017}
6018
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006019MachineBasicBlock *SystemZTargetLowering::EmitInstrWithCustomInserter(
6020 MachineInstr &MI, MachineBasicBlock *MBB) const {
6021 switch (MI.getOpcode()) {
Richard Sandiford7c5c0ea2013-10-01 13:10:16 +00006022 case SystemZ::Select32Mux:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006023 case SystemZ::Select32:
6024 case SystemZ::SelectF32:
6025 case SystemZ::Select64:
6026 case SystemZ::SelectF64:
6027 case SystemZ::SelectF128:
6028 return emitSelect(MI, MBB);
6029
Richard Sandiford2896d042013-10-01 14:33:55 +00006030 case SystemZ::CondStore8Mux:
6031 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
6032 case SystemZ::CondStore8MuxInv:
6033 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
6034 case SystemZ::CondStore16Mux:
6035 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
6036 case SystemZ::CondStore16MuxInv:
6037 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006038 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006039 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006040 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006041 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006042 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006043 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006044 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006045 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006046 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006047 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006048 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006049 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006050 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006051 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006052 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006053 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006054 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006055 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006056 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006057 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006058 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006059 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006060 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006061 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006062
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006063 case SystemZ::AEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00006064 return emitExt128(MI, MBB, false, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006065 case SystemZ::ZEXT128_32:
Richard Sandiford87a44362013-09-30 10:28:35 +00006066 return emitExt128(MI, MBB, true, SystemZ::subreg_l32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006067 case SystemZ::ZEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00006068 return emitExt128(MI, MBB, true, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006069
6070 case SystemZ::ATOMIC_SWAPW:
6071 return emitAtomicLoadBinary(MI, MBB, 0, 0);
6072 case SystemZ::ATOMIC_SWAP_32:
6073 return emitAtomicLoadBinary(MI, MBB, 0, 32);
6074 case SystemZ::ATOMIC_SWAP_64:
6075 return emitAtomicLoadBinary(MI, MBB, 0, 64);
6076
6077 case SystemZ::ATOMIC_LOADW_AR:
6078 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
6079 case SystemZ::ATOMIC_LOADW_AFI:
6080 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
6081 case SystemZ::ATOMIC_LOAD_AR:
6082 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
6083 case SystemZ::ATOMIC_LOAD_AHI:
6084 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
6085 case SystemZ::ATOMIC_LOAD_AFI:
6086 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
6087 case SystemZ::ATOMIC_LOAD_AGR:
6088 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
6089 case SystemZ::ATOMIC_LOAD_AGHI:
6090 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
6091 case SystemZ::ATOMIC_LOAD_AGFI:
6092 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
6093
6094 case SystemZ::ATOMIC_LOADW_SR:
6095 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
6096 case SystemZ::ATOMIC_LOAD_SR:
6097 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
6098 case SystemZ::ATOMIC_LOAD_SGR:
6099 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
6100
6101 case SystemZ::ATOMIC_LOADW_NR:
6102 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
6103 case SystemZ::ATOMIC_LOADW_NILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00006104 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006105 case SystemZ::ATOMIC_LOAD_NR:
6106 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006107 case SystemZ::ATOMIC_LOAD_NILL:
6108 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
6109 case SystemZ::ATOMIC_LOAD_NILH:
6110 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
6111 case SystemZ::ATOMIC_LOAD_NILF:
6112 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006113 case SystemZ::ATOMIC_LOAD_NGR:
6114 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006115 case SystemZ::ATOMIC_LOAD_NILL64:
6116 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
6117 case SystemZ::ATOMIC_LOAD_NILH64:
6118 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00006119 case SystemZ::ATOMIC_LOAD_NIHL64:
6120 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
6121 case SystemZ::ATOMIC_LOAD_NIHH64:
6122 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006123 case SystemZ::ATOMIC_LOAD_NILF64:
6124 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00006125 case SystemZ::ATOMIC_LOAD_NIHF64:
6126 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006127
6128 case SystemZ::ATOMIC_LOADW_OR:
6129 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
6130 case SystemZ::ATOMIC_LOADW_OILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00006131 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006132 case SystemZ::ATOMIC_LOAD_OR:
6133 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006134 case SystemZ::ATOMIC_LOAD_OILL:
6135 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
6136 case SystemZ::ATOMIC_LOAD_OILH:
6137 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
6138 case SystemZ::ATOMIC_LOAD_OILF:
6139 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006140 case SystemZ::ATOMIC_LOAD_OGR:
6141 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006142 case SystemZ::ATOMIC_LOAD_OILL64:
6143 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
6144 case SystemZ::ATOMIC_LOAD_OILH64:
6145 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00006146 case SystemZ::ATOMIC_LOAD_OIHL64:
6147 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
6148 case SystemZ::ATOMIC_LOAD_OIHH64:
6149 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006150 case SystemZ::ATOMIC_LOAD_OILF64:
6151 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00006152 case SystemZ::ATOMIC_LOAD_OIHF64:
6153 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006154
6155 case SystemZ::ATOMIC_LOADW_XR:
6156 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
6157 case SystemZ::ATOMIC_LOADW_XILF:
Richard Sandiford652784e2013-09-25 11:11:53 +00006158 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006159 case SystemZ::ATOMIC_LOAD_XR:
6160 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006161 case SystemZ::ATOMIC_LOAD_XILF:
6162 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006163 case SystemZ::ATOMIC_LOAD_XGR:
6164 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006165 case SystemZ::ATOMIC_LOAD_XILF64:
6166 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
Richard Sandiford5718dac2013-10-01 14:08:44 +00006167 case SystemZ::ATOMIC_LOAD_XIHF64:
6168 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006169
6170 case SystemZ::ATOMIC_LOADW_NRi:
6171 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
6172 case SystemZ::ATOMIC_LOADW_NILHi:
Richard Sandiford652784e2013-09-25 11:11:53 +00006173 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006174 case SystemZ::ATOMIC_LOAD_NRi:
6175 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006176 case SystemZ::ATOMIC_LOAD_NILLi:
6177 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
6178 case SystemZ::ATOMIC_LOAD_NILHi:
6179 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
6180 case SystemZ::ATOMIC_LOAD_NILFi:
6181 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006182 case SystemZ::ATOMIC_LOAD_NGRi:
6183 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006184 case SystemZ::ATOMIC_LOAD_NILL64i:
6185 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
6186 case SystemZ::ATOMIC_LOAD_NILH64i:
6187 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00006188 case SystemZ::ATOMIC_LOAD_NIHL64i:
6189 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
6190 case SystemZ::ATOMIC_LOAD_NIHH64i:
6191 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006192 case SystemZ::ATOMIC_LOAD_NILF64i:
6193 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00006194 case SystemZ::ATOMIC_LOAD_NIHF64i:
6195 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006196
6197 case SystemZ::ATOMIC_LOADW_MIN:
6198 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6199 SystemZ::CCMASK_CMP_LE, 0);
6200 case SystemZ::ATOMIC_LOAD_MIN_32:
6201 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6202 SystemZ::CCMASK_CMP_LE, 32);
6203 case SystemZ::ATOMIC_LOAD_MIN_64:
6204 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
6205 SystemZ::CCMASK_CMP_LE, 64);
6206
6207 case SystemZ::ATOMIC_LOADW_MAX:
6208 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6209 SystemZ::CCMASK_CMP_GE, 0);
6210 case SystemZ::ATOMIC_LOAD_MAX_32:
6211 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6212 SystemZ::CCMASK_CMP_GE, 32);
6213 case SystemZ::ATOMIC_LOAD_MAX_64:
6214 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
6215 SystemZ::CCMASK_CMP_GE, 64);
6216
6217 case SystemZ::ATOMIC_LOADW_UMIN:
6218 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6219 SystemZ::CCMASK_CMP_LE, 0);
6220 case SystemZ::ATOMIC_LOAD_UMIN_32:
6221 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6222 SystemZ::CCMASK_CMP_LE, 32);
6223 case SystemZ::ATOMIC_LOAD_UMIN_64:
6224 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6225 SystemZ::CCMASK_CMP_LE, 64);
6226
6227 case SystemZ::ATOMIC_LOADW_UMAX:
6228 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6229 SystemZ::CCMASK_CMP_GE, 0);
6230 case SystemZ::ATOMIC_LOAD_UMAX_32:
6231 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6232 SystemZ::CCMASK_CMP_GE, 32);
6233 case SystemZ::ATOMIC_LOAD_UMAX_64:
6234 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6235 SystemZ::CCMASK_CMP_GE, 64);
6236
6237 case SystemZ::ATOMIC_CMP_SWAPW:
6238 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006239 case SystemZ::MVCSequence:
6240 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006241 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00006242 case SystemZ::NCSequence:
6243 case SystemZ::NCLoop:
6244 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
6245 case SystemZ::OCSequence:
6246 case SystemZ::OCLoop:
6247 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
6248 case SystemZ::XCSequence:
6249 case SystemZ::XCLoop:
6250 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006251 case SystemZ::CLCSequence:
6252 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006253 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00006254 case SystemZ::CLSTLoop:
6255 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00006256 case SystemZ::MVSTLoop:
6257 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00006258 case SystemZ::SRSTLoop:
6259 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006260 case SystemZ::TBEGIN:
6261 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false);
6262 case SystemZ::TBEGIN_nofloat:
6263 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true);
6264 case SystemZ::TBEGINC:
6265 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true);
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006266 case SystemZ::LTEBRCompare_VecPseudo:
6267 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR);
6268 case SystemZ::LTDBRCompare_VecPseudo:
6269 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR);
6270 case SystemZ::LTXBRCompare_VecPseudo:
6271 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR);
6272
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006273 default:
6274 llvm_unreachable("Unexpected instr type to insert");
6275 }
6276}