blob: 9e24a3b9548342743f109afcc3a75d65b64a5b39 [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- SystemZISelLowering.cpp - SystemZ DAG lowering implementation -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SystemZTargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
Ulrich Weigand5f613df2013-05-06 16:15:19 +000014#include "SystemZISelLowering.h"
15#include "SystemZCallingConv.h"
16#include "SystemZConstantPoolValue.h"
17#include "SystemZMachineFunctionInfo.h"
18#include "SystemZTargetMachine.h"
19#include "llvm/CodeGen/CallingConvLower.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
22#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Ulrich Weigand57c85f52015-04-01 12:51:43 +000023#include "llvm/IR/Intrinsics.h"
Reid Kleckner0e8c4bb2017-09-07 23:27:44 +000024#include "llvm/IR/IntrinsicInst.h"
Craig Topperd0af7e82017-04-28 05:31:46 +000025#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/KnownBits.h"
Will Dietz981af002013-10-12 00:55:57 +000027#include <cctype>
28
Ulrich Weigand5f613df2013-05-06 16:15:19 +000029using namespace llvm;
30
Chandler Carruth84e68b22014-04-22 02:41:26 +000031#define DEBUG_TYPE "systemz-lower"
32
Richard Sandifordf722a8e302013-10-16 11:10:55 +000033namespace {
34// Represents a sequence for extracting a 0/1 value from an IPM result:
35// (((X ^ XORValue) + AddValue) >> Bit)
36struct IPMConversion {
37 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
38 : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
39
40 int64_t XORValue;
41 int64_t AddValue;
42 unsigned Bit;
43};
Richard Sandifordd420f732013-12-13 15:28:45 +000044
45// Represents information about a comparison.
46struct Comparison {
47 Comparison(SDValue Op0In, SDValue Op1In)
48 : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {}
49
50 // The operands to the comparison.
51 SDValue Op0, Op1;
52
53 // The opcode that should be used to compare Op0 and Op1.
54 unsigned Opcode;
55
56 // A SystemZICMP value. Only used for integer comparisons.
57 unsigned ICmpType;
58
59 // The mask of CC values that Opcode can produce.
60 unsigned CCValid;
61
62 // The mask of CC values for which the original condition is true.
63 unsigned CCMask;
64};
Richard Sandifordc2312692014-03-06 10:38:30 +000065} // end anonymous namespace
Richard Sandifordf722a8e302013-10-16 11:10:55 +000066
Ulrich Weigand5f613df2013-05-06 16:15:19 +000067// Classify VT as either 32 or 64 bit.
68static bool is32Bit(EVT VT) {
69 switch (VT.getSimpleVT().SimpleTy) {
70 case MVT::i32:
71 return true;
72 case MVT::i64:
73 return false;
74 default:
75 llvm_unreachable("Unsupported type");
76 }
77}
78
79// Return a version of MachineOperand that can be safely used before the
80// final use.
81static MachineOperand earlyUseOperand(MachineOperand Op) {
82 if (Op.isReg())
83 Op.setIsKill(false);
84 return Op;
85}
86
Mehdi Amini44ede332015-07-09 02:09:04 +000087SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM,
Eric Christophera6734172015-01-31 00:06:45 +000088 const SystemZSubtarget &STI)
Mehdi Amini44ede332015-07-09 02:09:04 +000089 : TargetLowering(TM), Subtarget(STI) {
Mehdi Amini26d48132015-07-24 16:04:22 +000090 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
Ulrich Weigand5f613df2013-05-06 16:15:19 +000091
92 // Set up the register classes.
Richard Sandiford0755c932013-10-01 11:26:28 +000093 if (Subtarget.hasHighWord())
94 addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass);
95 else
96 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
Ulrich Weigand49506d72015-05-05 19:28:34 +000097 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
98 if (Subtarget.hasVector()) {
99 addRegisterClass(MVT::f32, &SystemZ::VR32BitRegClass);
100 addRegisterClass(MVT::f64, &SystemZ::VR64BitRegClass);
101 } else {
102 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
103 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
104 }
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000105 if (Subtarget.hasVectorEnhancements1())
106 addRegisterClass(MVT::f128, &SystemZ::VR128BitRegClass);
107 else
108 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000109
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000110 if (Subtarget.hasVector()) {
111 addRegisterClass(MVT::v16i8, &SystemZ::VR128BitRegClass);
112 addRegisterClass(MVT::v8i16, &SystemZ::VR128BitRegClass);
113 addRegisterClass(MVT::v4i32, &SystemZ::VR128BitRegClass);
114 addRegisterClass(MVT::v2i64, &SystemZ::VR128BitRegClass);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000115 addRegisterClass(MVT::v4f32, &SystemZ::VR128BitRegClass);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000116 addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000117 }
118
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000119 // Compute derived properties from the register classes
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000120 computeRegisterProperties(Subtarget.getRegisterInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000121
122 // Set up special registers.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000123 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
124
125 // TODO: It may be better to default to latency-oriented scheduling, however
126 // LLVM's current latency-oriented scheduler can't handle physreg definitions
Richard Sandiford14a44492013-05-22 13:38:45 +0000127 // such as SystemZ has with CC, so set this to the register-pressure
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000128 // scheduler, because it can.
129 setSchedulingPreference(Sched::RegPressure);
130
131 setBooleanContents(ZeroOrOneBooleanContent);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000132 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000133
134 // Instructions are strings of 2-byte aligned 2-byte values.
135 setMinFunctionAlignment(2);
136
137 // Handle operations that are handled in a similar way for all types.
138 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
139 I <= MVT::LAST_FP_VALUETYPE;
140 ++I) {
141 MVT VT = MVT::SimpleValueType(I);
142 if (isTypeLegal(VT)) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000143 // Lower SET_CC into an IPM-based sequence.
144 setOperationAction(ISD::SETCC, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000145
146 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
147 setOperationAction(ISD::SELECT, VT, Expand);
148
149 // Lower SELECT_CC and BR_CC into separate comparisons and branches.
150 setOperationAction(ISD::SELECT_CC, VT, Custom);
151 setOperationAction(ISD::BR_CC, VT, Custom);
152 }
153 }
154
155 // Expand jump table branches as address arithmetic followed by an
156 // indirect jump.
157 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
158
159 // Expand BRCOND into a BR_CC (see above).
160 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
161
162 // Handle integer types.
163 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
164 I <= MVT::LAST_INTEGER_VALUETYPE;
165 ++I) {
166 MVT VT = MVT::SimpleValueType(I);
167 if (isTypeLegal(VT)) {
168 // Expand individual DIV and REMs into DIVREMs.
169 setOperationAction(ISD::SDIV, VT, Expand);
170 setOperationAction(ISD::UDIV, VT, Expand);
171 setOperationAction(ISD::SREM, VT, Expand);
172 setOperationAction(ISD::UREM, VT, Expand);
173 setOperationAction(ISD::SDIVREM, VT, Custom);
174 setOperationAction(ISD::UDIVREM, VT, Custom);
175
Richard Sandifordbef3d7a2013-12-10 10:49:34 +0000176 // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and
177 // stores, putting a serialization instruction after the stores.
178 setOperationAction(ISD::ATOMIC_LOAD, VT, Custom);
179 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000180
Richard Sandiford41350a52013-12-24 15:18:04 +0000181 // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are
182 // available, or if the operand is constant.
183 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
184
Ulrich Weigandb4012182015-03-31 12:56:33 +0000185 // Use POPCNT on z196 and above.
186 if (Subtarget.hasPopulationCount())
187 setOperationAction(ISD::CTPOP, VT, Custom);
188 else
189 setOperationAction(ISD::CTPOP, VT, Expand);
190
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000191 // No special instructions for these.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000192 setOperationAction(ISD::CTTZ, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000193 setOperationAction(ISD::ROTR, VT, Expand);
194
Richard Sandiford7d86e472013-08-21 09:34:56 +0000195 // Use *MUL_LOHI where possible instead of MULH*.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000196 setOperationAction(ISD::MULHS, VT, Expand);
197 setOperationAction(ISD::MULHU, VT, Expand);
Richard Sandiford7d86e472013-08-21 09:34:56 +0000198 setOperationAction(ISD::SMUL_LOHI, VT, Custom);
199 setOperationAction(ISD::UMUL_LOHI, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000200
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000201 // Only z196 and above have native support for conversions to unsigned.
Jonas Paulssonb7a2ef82017-02-02 15:42:14 +0000202 // On z10, promoting to i64 doesn't generate an inexact condition for
203 // values that are outside the i32 range but in the i64 range, so use
204 // the default expansion.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000205 if (!Subtarget.hasFPExtension())
206 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000207 }
208 }
209
210 // Type legalization will convert 8- and 16-bit atomic operations into
211 // forms that operate on i32s (but still keeping the original memory VT).
212 // Lower them into full i32 operations.
213 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom);
214 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom);
215 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
216 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom);
217 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom);
218 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom);
219 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
220 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom);
221 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom);
222 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
223 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
224 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
225
Ulrich Weiganda11f63a2017-08-04 18:57:58 +0000226 // Even though i128 is not a legal type, we still need to custom lower
227 // the atomic operations in order to exploit SystemZ instructions.
228 setOperationAction(ISD::ATOMIC_LOAD, MVT::i128, Custom);
229 setOperationAction(ISD::ATOMIC_STORE, MVT::i128, Custom);
230 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
231
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +0000232 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
233
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000234 // Traps are legal, as we will convert them to "j .+2".
235 setOperationAction(ISD::TRAP, MVT::Other, Legal);
236
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000237 // z10 has instructions for signed but not unsigned FP conversion.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000238 // Handle unsigned 32-bit types as signed 64-bit types.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000239 if (!Subtarget.hasFPExtension()) {
240 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
241 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
242 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000243
244 // We have native support for a 64-bit CTLZ, via FLOGR.
245 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
246 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
247
248 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
249 setOperationAction(ISD::OR, MVT::i64, Custom);
250
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000251 // FIXME: Can we support these natively?
252 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
253 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
254 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
255
256 // We have native instructions for i8, i16 and i32 extensions, but not i1.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000257 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000258 for (MVT VT : MVT::integer_valuetypes()) {
259 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
260 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
261 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
262 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000263
264 // Handle the various types of symbolic address.
265 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
266 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
267 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
268 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
269 setOperationAction(ISD::JumpTable, PtrVT, Custom);
270
271 // We need to handle dynamic allocations specially because of the
272 // 160-byte area at the bottom of the stack.
273 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +0000274 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, PtrVT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000275
276 // Use custom expanders so that we can force the function to use
277 // a frame pointer.
278 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
279 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
280
Richard Sandiford03481332013-08-23 11:36:42 +0000281 // Handle prefetches with PFD or PFDRL.
282 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
283
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000284 for (MVT VT : MVT::vector_valuetypes()) {
285 // Assume by default that all vector operations need to be expanded.
286 for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode)
287 if (getOperationAction(Opcode, VT) == Legal)
288 setOperationAction(Opcode, VT, Expand);
289
290 // Likewise all truncating stores and extending loads.
291 for (MVT InnerVT : MVT::vector_valuetypes()) {
292 setTruncStoreAction(VT, InnerVT, Expand);
293 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
294 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
295 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
296 }
297
298 if (isTypeLegal(VT)) {
299 // These operations are legal for anything that can be stored in a
300 // vector register, even if there is no native support for the format
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000301 // as such. In particular, we can do these for v4f32 even though there
302 // are no specific instructions for that format.
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000303 setOperationAction(ISD::LOAD, VT, Legal);
304 setOperationAction(ISD::STORE, VT, Legal);
305 setOperationAction(ISD::VSELECT, VT, Legal);
306 setOperationAction(ISD::BITCAST, VT, Legal);
307 setOperationAction(ISD::UNDEF, VT, Legal);
308
309 // Likewise, except that we need to replace the nodes with something
310 // more specific.
311 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
312 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
313 }
314 }
315
316 // Handle integer vector types.
317 for (MVT VT : MVT::integer_vector_valuetypes()) {
318 if (isTypeLegal(VT)) {
319 // These operations have direct equivalents.
320 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal);
321 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal);
322 setOperationAction(ISD::ADD, VT, Legal);
323 setOperationAction(ISD::SUB, VT, Legal);
324 if (VT != MVT::v2i64)
325 setOperationAction(ISD::MUL, VT, Legal);
326 setOperationAction(ISD::AND, VT, Legal);
327 setOperationAction(ISD::OR, VT, Legal);
328 setOperationAction(ISD::XOR, VT, Legal);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000329 if (Subtarget.hasVectorEnhancements1())
330 setOperationAction(ISD::CTPOP, VT, Legal);
331 else
332 setOperationAction(ISD::CTPOP, VT, Custom);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000333 setOperationAction(ISD::CTTZ, VT, Legal);
334 setOperationAction(ISD::CTLZ, VT, Legal);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000335
336 // Convert a GPR scalar to a vector by inserting it into element 0.
337 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
338
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000339 // Use a series of unpacks for extensions.
340 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Custom);
341 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Custom);
342
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000343 // Detect shifts by a scalar amount and convert them into
344 // V*_BY_SCALAR.
345 setOperationAction(ISD::SHL, VT, Custom);
346 setOperationAction(ISD::SRA, VT, Custom);
347 setOperationAction(ISD::SRL, VT, Custom);
348
349 // At present ROTL isn't matched by DAGCombiner. ROTR should be
350 // converted into ROTL.
351 setOperationAction(ISD::ROTL, VT, Expand);
352 setOperationAction(ISD::ROTR, VT, Expand);
353
354 // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands
355 // and inverting the result as necessary.
356 setOperationAction(ISD::SETCC, VT, Custom);
357 }
358 }
359
Ulrich Weigandcd808232015-05-05 19:26:48 +0000360 if (Subtarget.hasVector()) {
361 // There should be no need to check for float types other than v2f64
362 // since <2 x f32> isn't a legal type.
363 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000364 setOperationAction(ISD::FP_TO_SINT, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000365 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000366 setOperationAction(ISD::FP_TO_UINT, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000367 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000368 setOperationAction(ISD::SINT_TO_FP, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000369 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000370 setOperationAction(ISD::UINT_TO_FP, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000371 }
372
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000373 // Handle floating-point types.
374 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
375 I <= MVT::LAST_FP_VALUETYPE;
376 ++I) {
377 MVT VT = MVT::SimpleValueType(I);
378 if (isTypeLegal(VT)) {
379 // We can use FI for FRINT.
380 setOperationAction(ISD::FRINT, VT, Legal);
381
Richard Sandifordaf5f66a2013-08-21 09:04:20 +0000382 // We can use the extended form of FI for other rounding operations.
383 if (Subtarget.hasFPExtension()) {
384 setOperationAction(ISD::FNEARBYINT, VT, Legal);
385 setOperationAction(ISD::FFLOOR, VT, Legal);
386 setOperationAction(ISD::FCEIL, VT, Legal);
387 setOperationAction(ISD::FTRUNC, VT, Legal);
388 setOperationAction(ISD::FROUND, VT, Legal);
389 }
390
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000391 // No special instructions for these.
392 setOperationAction(ISD::FSIN, VT, Expand);
393 setOperationAction(ISD::FCOS, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000394 setOperationAction(ISD::FSINCOS, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000395 setOperationAction(ISD::FREM, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000396 setOperationAction(ISD::FPOW, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000397 }
398 }
399
Ulrich Weigandcd808232015-05-05 19:26:48 +0000400 // Handle floating-point vector types.
401 if (Subtarget.hasVector()) {
402 // Scalar-to-vector conversion is just a subreg.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000403 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000404 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
405
406 // Some insertions and extractions can be done directly but others
407 // need to go via integers.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000408 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000409 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000410 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000411 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
412
413 // These operations have direct equivalents.
414 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
415 setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
416 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
417 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
418 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
419 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
420 setOperationAction(ISD::FABS, MVT::v2f64, Legal);
421 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
422 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
423 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
424 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
425 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
426 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
427 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
428 }
429
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000430 // The vector enhancements facility 1 has instructions for these.
431 if (Subtarget.hasVectorEnhancements1()) {
Ulrich Weigand33435c42017-07-17 17:42:48 +0000432 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
433 setOperationAction(ISD::FNEG, MVT::v4f32, Legal);
434 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
435 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
436 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
437 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
438 setOperationAction(ISD::FABS, MVT::v4f32, Legal);
439 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
440 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
441 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
442 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
443 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
444 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
445 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
446
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000447 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
448 setOperationAction(ISD::FMAXNAN, MVT::f64, Legal);
449 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
450 setOperationAction(ISD::FMINNAN, MVT::f64, Legal);
451
452 setOperationAction(ISD::FMAXNUM, MVT::v2f64, Legal);
453 setOperationAction(ISD::FMAXNAN, MVT::v2f64, Legal);
454 setOperationAction(ISD::FMINNUM, MVT::v2f64, Legal);
455 setOperationAction(ISD::FMINNAN, MVT::v2f64, Legal);
Ulrich Weigand33435c42017-07-17 17:42:48 +0000456
457 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
458 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal);
459 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
460 setOperationAction(ISD::FMINNAN, MVT::f32, Legal);
461
462 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
463 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal);
464 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
465 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal);
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000466
467 setOperationAction(ISD::FMAXNUM, MVT::f128, Legal);
468 setOperationAction(ISD::FMAXNAN, MVT::f128, Legal);
469 setOperationAction(ISD::FMINNUM, MVT::f128, Legal);
470 setOperationAction(ISD::FMINNAN, MVT::f128, Legal);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000471 }
472
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000473 // We have fused multiply-addition for f32 and f64 but not f128.
474 setOperationAction(ISD::FMA, MVT::f32, Legal);
475 setOperationAction(ISD::FMA, MVT::f64, Legal);
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000476 if (Subtarget.hasVectorEnhancements1())
477 setOperationAction(ISD::FMA, MVT::f128, Legal);
478 else
479 setOperationAction(ISD::FMA, MVT::f128, Expand);
480
481 // We don't have a copysign instruction on vector registers.
482 if (Subtarget.hasVectorEnhancements1())
483 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000484
485 // Needed so that we don't try to implement f128 constant loads using
486 // a load-and-extend of a f80 constant (in cases where the constant
487 // would fit in an f80).
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000488 for (MVT VT : MVT::fp_valuetypes())
489 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000490
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000491 // We don't have extending load instruction on vector registers.
492 if (Subtarget.hasVectorEnhancements1()) {
493 setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f32, Expand);
494 setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f64, Expand);
495 }
496
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000497 // Floating-point truncation and stores need to be done separately.
498 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
499 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
500 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
501
502 // We have 64-bit FPR<->GPR moves, but need special handling for
503 // 32-bit forms.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000504 if (!Subtarget.hasVector()) {
505 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
506 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
507 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000508
509 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
510 // structure, but VAEND is a no-op.
511 setOperationAction(ISD::VASTART, MVT::Other, Custom);
512 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
513 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Richard Sandifordd131ff82013-07-08 09:35:23 +0000514
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000515 // Codes for which we want to perform some z-specific combinations.
516 setTargetDAGCombine(ISD::SIGN_EXTEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000517 setTargetDAGCombine(ISD::STORE);
518 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000519 setTargetDAGCombine(ISD::FP_ROUND);
Bryan Chan28b759c2016-05-16 20:32:22 +0000520 setTargetDAGCombine(ISD::BSWAP);
Elliot Colpbc2cfc22016-07-06 18:13:11 +0000521 setTargetDAGCombine(ISD::SHL);
522 setTargetDAGCombine(ISD::SRA);
523 setTargetDAGCombine(ISD::SRL);
524 setTargetDAGCombine(ISD::ROTL);
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000525
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000526 // Handle intrinsics.
527 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Ulrich Weigandc1708b22015-05-05 19:31:09 +0000528 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000529
Richard Sandifordd131ff82013-07-08 09:35:23 +0000530 // We want to use MVC in preference to even a single load/store pair.
531 MaxStoresPerMemcpy = 0;
532 MaxStoresPerMemcpyOptSize = 0;
Richard Sandiford47660c12013-07-09 09:32:42 +0000533
534 // The main memset sequence is a byte store followed by an MVC.
535 // Two STC or MV..I stores win over that, but the kind of fused stores
536 // generated by target-independent code don't when the byte value is
537 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
538 // than "STC;MVC". Handle the choice in target-specific code instead.
539 MaxStoresPerMemset = 0;
540 MaxStoresPerMemsetOptSize = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000541}
542
Mehdi Amini44ede332015-07-09 02:09:04 +0000543EVT SystemZTargetLowering::getSetCCResultType(const DataLayout &DL,
544 LLVMContext &, EVT VT) const {
Richard Sandifordabc010b2013-11-06 12:16:02 +0000545 if (!VT.isVector())
546 return MVT::i32;
547 return VT.changeVectorElementTypeToInteger();
548}
549
550bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
Stephen Lin73de7bf2013-07-09 18:16:56 +0000551 VT = VT.getScalarType();
552
553 if (!VT.isSimple())
554 return false;
555
556 switch (VT.getSimpleVT().SimpleTy) {
557 case MVT::f32:
558 case MVT::f64:
559 return true;
560 case MVT::f128:
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000561 return Subtarget.hasVectorEnhancements1();
Stephen Lin73de7bf2013-07-09 18:16:56 +0000562 default:
563 break;
564 }
565
566 return false;
567}
568
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000569bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
570 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
571 return Imm.isZero() || Imm.isNegZero();
572}
573
Ulrich Weigand1f6666a2015-03-31 12:52:27 +0000574bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
575 // We can use CGFI or CLGFI.
576 return isInt<32>(Imm) || isUInt<32>(Imm);
577}
578
579bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const {
580 // We can use ALGFI or SLGFI.
581 return isUInt<32>(Imm) || isUInt<32>(-Imm);
582}
583
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000584bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
585 unsigned,
586 unsigned,
587 bool *Fast) const {
Richard Sandiford46af5a22013-05-30 09:45:42 +0000588 // Unaligned accesses should never be slower than the expanded version.
589 // We check specifically for aligned accesses in the few cases where
590 // they are required.
591 if (Fast)
592 *Fast = true;
593 return true;
594}
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +0000595
Jonas Paulsson024e3192017-07-21 11:59:37 +0000596// Information about the addressing mode for a memory access.
597struct AddressingMode {
598 // True if a long displacement is supported.
599 bool LongDisplacement;
600
601 // True if use of index register is supported.
602 bool IndexReg;
603
604 AddressingMode(bool LongDispl, bool IdxReg) :
605 LongDisplacement(LongDispl), IndexReg(IdxReg) {}
606};
607
608// Return the desired addressing mode for a Load which has only one use (in
609// the same block) which is a Store.
610static AddressingMode getLoadStoreAddrMode(bool HasVector,
611 Type *Ty) {
612 // With vector support a Load->Store combination may be combined to either
613 // an MVC or vector operations and it seems to work best to allow the
614 // vector addressing mode.
615 if (HasVector)
616 return AddressingMode(false/*LongDispl*/, true/*IdxReg*/);
617
618 // Otherwise only the MVC case is special.
619 bool MVC = Ty->isIntegerTy(8);
620 return AddressingMode(!MVC/*LongDispl*/, !MVC/*IdxReg*/);
621}
622
623// Return the addressing mode which seems most desirable given an LLVM
624// Instruction pointer.
625static AddressingMode
626supportedAddressingMode(Instruction *I, bool HasVector) {
627 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
628 switch (II->getIntrinsicID()) {
629 default: break;
630 case Intrinsic::memset:
631 case Intrinsic::memmove:
632 case Intrinsic::memcpy:
633 return AddressingMode(false/*LongDispl*/, false/*IdxReg*/);
634 }
635 }
636
637 if (isa<LoadInst>(I) && I->hasOneUse()) {
638 auto *SingleUser = dyn_cast<Instruction>(*I->user_begin());
639 if (SingleUser->getParent() == I->getParent()) {
640 if (isa<ICmpInst>(SingleUser)) {
641 if (auto *C = dyn_cast<ConstantInt>(SingleUser->getOperand(1)))
642 if (isInt<16>(C->getSExtValue()) || isUInt<16>(C->getZExtValue()))
643 // Comparison of memory with 16 bit signed / unsigned immediate
644 return AddressingMode(false/*LongDispl*/, false/*IdxReg*/);
645 } else if (isa<StoreInst>(SingleUser))
646 // Load->Store
647 return getLoadStoreAddrMode(HasVector, I->getType());
648 }
649 } else if (auto *StoreI = dyn_cast<StoreInst>(I)) {
650 if (auto *LoadI = dyn_cast<LoadInst>(StoreI->getValueOperand()))
651 if (LoadI->hasOneUse() && LoadI->getParent() == I->getParent())
652 // Load->Store
653 return getLoadStoreAddrMode(HasVector, LoadI->getType());
654 }
655
656 if (HasVector && (isa<LoadInst>(I) || isa<StoreInst>(I))) {
657
658 // * Use LDE instead of LE/LEY for z13 to avoid partial register
659 // dependencies (LDE only supports small offsets).
660 // * Utilize the vector registers to hold floating point
661 // values (vector load / store instructions only support small
662 // offsets).
663
664 Type *MemAccessTy = (isa<LoadInst>(I) ? I->getType() :
665 I->getOperand(0)->getType());
666 bool IsFPAccess = MemAccessTy->isFloatingPointTy();
667 bool IsVectorAccess = MemAccessTy->isVectorTy();
668
669 // A store of an extracted vector element will be combined into a VSTE type
670 // instruction.
671 if (!IsVectorAccess && isa<StoreInst>(I)) {
672 Value *DataOp = I->getOperand(0);
673 if (isa<ExtractElementInst>(DataOp))
674 IsVectorAccess = true;
675 }
676
677 // A load which gets inserted into a vector element will be combined into a
678 // VLE type instruction.
679 if (!IsVectorAccess && isa<LoadInst>(I) && I->hasOneUse()) {
680 User *LoadUser = *I->user_begin();
681 if (isa<InsertElementInst>(LoadUser))
682 IsVectorAccess = true;
683 }
684
685 if (IsFPAccess || IsVectorAccess)
686 return AddressingMode(false/*LongDispl*/, true/*IdxReg*/);
687 }
688
689 return AddressingMode(true/*LongDispl*/, true/*IdxReg*/);
690}
691
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000692bool SystemZTargetLowering::isLegalAddressingMode(const DataLayout &DL,
Jonas Paulsson6228aed2017-08-09 11:28:01 +0000693 const AddrMode &AM, Type *Ty, unsigned AS, Instruction *I) const {
Richard Sandiford791bea42013-07-31 12:58:26 +0000694 // Punt on globals for now, although they can be used in limited
695 // RELATIVE LONG cases.
696 if (AM.BaseGV)
697 return false;
698
699 // Require a 20-bit signed offset.
700 if (!isInt<20>(AM.BaseOffs))
701 return false;
702
Jonas Paulsson6228aed2017-08-09 11:28:01 +0000703 AddressingMode SupportedAM(true, true);
704 if (I != nullptr)
705 SupportedAM = supportedAddressingMode(I, Subtarget.hasVector());
706
707 if (!SupportedAM.LongDisplacement && !isUInt<12>(AM.BaseOffs))
708 return false;
709
710 if (!SupportedAM.IndexReg)
Jonas Paulsson024e3192017-07-21 11:59:37 +0000711 // No indexing allowed.
712 return AM.Scale == 0;
713 else
714 // Indexing is OK but no scale factor can be applied.
715 return AM.Scale == 0 || AM.Scale == 1;
Richard Sandiford791bea42013-07-31 12:58:26 +0000716}
717
Richard Sandiford709bda62013-08-19 12:42:31 +0000718bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
719 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
720 return false;
721 unsigned FromBits = FromType->getPrimitiveSizeInBits();
722 unsigned ToBits = ToType->getPrimitiveSizeInBits();
723 return FromBits > ToBits;
724}
725
726bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
727 if (!FromVT.isInteger() || !ToVT.isInteger())
728 return false;
729 unsigned FromBits = FromVT.getSizeInBits();
730 unsigned ToBits = ToVT.getSizeInBits();
731 return FromBits > ToBits;
732}
733
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000734//===----------------------------------------------------------------------===//
735// Inline asm support
736//===----------------------------------------------------------------------===//
737
738TargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000739SystemZTargetLowering::getConstraintType(StringRef Constraint) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000740 if (Constraint.size() == 1) {
741 switch (Constraint[0]) {
742 case 'a': // Address register
743 case 'd': // Data register (equivalent to 'r')
744 case 'f': // Floating-point register
Richard Sandiford0755c932013-10-01 11:26:28 +0000745 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000746 case 'r': // General-purpose register
747 return C_RegisterClass;
748
749 case 'Q': // Memory with base and unsigned 12-bit displacement
750 case 'R': // Likewise, plus an index
751 case 'S': // Memory with base and signed 20-bit displacement
752 case 'T': // Likewise, plus an index
753 case 'm': // Equivalent to 'T'.
754 return C_Memory;
755
756 case 'I': // Unsigned 8-bit constant
757 case 'J': // Unsigned 12-bit constant
758 case 'K': // Signed 16-bit constant
759 case 'L': // Signed 20-bit displacement (on all targets we support)
760 case 'M': // 0x7fffffff
761 return C_Other;
762
763 default:
764 break;
765 }
766 }
767 return TargetLowering::getConstraintType(Constraint);
768}
769
770TargetLowering::ConstraintWeight SystemZTargetLowering::
771getSingleConstraintMatchWeight(AsmOperandInfo &info,
772 const char *constraint) const {
773 ConstraintWeight weight = CW_Invalid;
774 Value *CallOperandVal = info.CallOperandVal;
775 // If we don't have a value, we can't do a match,
776 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +0000777 if (!CallOperandVal)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000778 return CW_Default;
779 Type *type = CallOperandVal->getType();
780 // Look at the constraint type.
781 switch (*constraint) {
782 default:
783 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
784 break;
785
786 case 'a': // Address register
787 case 'd': // Data register (equivalent to 'r')
Richard Sandiford0755c932013-10-01 11:26:28 +0000788 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000789 case 'r': // General-purpose register
790 if (CallOperandVal->getType()->isIntegerTy())
791 weight = CW_Register;
792 break;
793
794 case 'f': // Floating-point register
795 if (type->isFloatingPointTy())
796 weight = CW_Register;
797 break;
798
799 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000800 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000801 if (isUInt<8>(C->getZExtValue()))
802 weight = CW_Constant;
803 break;
804
805 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000806 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000807 if (isUInt<12>(C->getZExtValue()))
808 weight = CW_Constant;
809 break;
810
811 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000812 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000813 if (isInt<16>(C->getSExtValue()))
814 weight = CW_Constant;
815 break;
816
817 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000818 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000819 if (isInt<20>(C->getSExtValue()))
820 weight = CW_Constant;
821 break;
822
823 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000824 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000825 if (C->getZExtValue() == 0x7fffffff)
826 weight = CW_Constant;
827 break;
828 }
829 return weight;
830}
831
Richard Sandifordb8204052013-07-12 09:08:12 +0000832// Parse a "{tNNN}" register constraint for which the register type "t"
833// has already been verified. MC is the class associated with "t" and
834// Map maps 0-based register numbers to LLVM register numbers.
835static std::pair<unsigned, const TargetRegisterClass *>
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000836parseRegisterNumber(StringRef Constraint, const TargetRegisterClass *RC,
837 const unsigned *Map) {
Richard Sandifordb8204052013-07-12 09:08:12 +0000838 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
839 if (isdigit(Constraint[2])) {
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000840 unsigned Index;
841 bool Failed =
842 Constraint.slice(2, Constraint.size() - 1).getAsInteger(10, Index);
843 if (!Failed && Index < 16 && Map[Index])
Richard Sandifordb8204052013-07-12 09:08:12 +0000844 return std::make_pair(Map[Index], RC);
845 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000846 return std::make_pair(0U, nullptr);
Richard Sandifordb8204052013-07-12 09:08:12 +0000847}
848
Eric Christopher11e4df72015-02-26 22:38:43 +0000849std::pair<unsigned, const TargetRegisterClass *>
850SystemZTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000851 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000852 if (Constraint.size() == 1) {
853 // GCC Constraint Letters
854 switch (Constraint[0]) {
855 default: break;
856 case 'd': // Data register (equivalent to 'r')
857 case 'r': // General-purpose register
858 if (VT == MVT::i64)
859 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
860 else if (VT == MVT::i128)
861 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
862 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
863
864 case 'a': // Address register
865 if (VT == MVT::i64)
866 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
867 else if (VT == MVT::i128)
868 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
869 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
870
Richard Sandiford0755c932013-10-01 11:26:28 +0000871 case 'h': // High-part register (an LLVM extension)
872 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
873
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000874 case 'f': // Floating-point register
875 if (VT == MVT::f64)
876 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
877 else if (VT == MVT::f128)
878 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
879 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
880 }
881 }
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000882 if (Constraint.size() > 0 && Constraint[0] == '{') {
Richard Sandifordb8204052013-07-12 09:08:12 +0000883 // We need to override the default register parsing for GPRs and FPRs
884 // because the interpretation depends on VT. The internal names of
885 // the registers are also different from the external names
886 // (F0D and F0S instead of F0, etc.).
887 if (Constraint[1] == 'r') {
888 if (VT == MVT::i32)
889 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
890 SystemZMC::GR32Regs);
891 if (VT == MVT::i128)
892 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
893 SystemZMC::GR128Regs);
894 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
895 SystemZMC::GR64Regs);
896 }
897 if (Constraint[1] == 'f') {
898 if (VT == MVT::f32)
899 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
900 SystemZMC::FP32Regs);
901 if (VT == MVT::f128)
902 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
903 SystemZMC::FP128Regs);
904 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
905 SystemZMC::FP64Regs);
906 }
907 }
Eric Christopher11e4df72015-02-26 22:38:43 +0000908 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000909}
910
911void SystemZTargetLowering::
912LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
913 std::vector<SDValue> &Ops,
914 SelectionDAG &DAG) const {
915 // Only support length 1 constraints for now.
916 if (Constraint.length() == 1) {
917 switch (Constraint[0]) {
918 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000919 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000920 if (isUInt<8>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000921 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000922 Op.getValueType()));
923 return;
924
925 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000926 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000927 if (isUInt<12>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000928 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000929 Op.getValueType()));
930 return;
931
932 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000933 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000934 if (isInt<16>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000935 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000936 Op.getValueType()));
937 return;
938
939 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000940 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000941 if (isInt<20>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000942 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000943 Op.getValueType()));
944 return;
945
946 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000947 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000948 if (C->getZExtValue() == 0x7fffffff)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000949 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000950 Op.getValueType()));
951 return;
952 }
953 }
954 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
955}
956
957//===----------------------------------------------------------------------===//
958// Calling conventions
959//===----------------------------------------------------------------------===//
960
961#include "SystemZGenCallingConv.inc"
962
Richard Sandiford709bda62013-08-19 12:42:31 +0000963bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
964 Type *ToType) const {
965 return isTruncateFree(FromType, ToType);
966}
967
Matt Arsenault31380752017-04-18 21:16:46 +0000968bool SystemZTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
Ulrich Weigand19d24d22015-11-13 13:00:27 +0000969 return CI->isTailCall();
Richard Sandiford709bda62013-08-19 12:42:31 +0000970}
971
Ulrich Weigand5211f9f2015-05-05 19:30:05 +0000972// We do not yet support 128-bit single-element vector types. If the user
973// attempts to use such types as function argument or return type, prefer
974// to error out instead of emitting code violating the ABI.
975static void VerifyVectorType(MVT VT, EVT ArgVT) {
976 if (ArgVT.isVector() && !VT.isVector())
977 report_fatal_error("Unsupported vector argument or return type");
978}
979
980static void VerifyVectorTypes(const SmallVectorImpl<ISD::InputArg> &Ins) {
981 for (unsigned i = 0; i < Ins.size(); ++i)
982 VerifyVectorType(Ins[i].VT, Ins[i].ArgVT);
983}
984
985static void VerifyVectorTypes(const SmallVectorImpl<ISD::OutputArg> &Outs) {
986 for (unsigned i = 0; i < Outs.size(); ++i)
987 VerifyVectorType(Outs[i].VT, Outs[i].ArgVT);
988}
989
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000990// Value is a value that has been passed to us in the location described by VA
991// (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
992// any loads onto Chain.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000993static SDValue convertLocVTToValVT(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000994 CCValAssign &VA, SDValue Chain,
995 SDValue Value) {
996 // If the argument has been promoted from a smaller type, insert an
997 // assertion to capture this.
998 if (VA.getLocInfo() == CCValAssign::SExt)
999 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
1000 DAG.getValueType(VA.getValVT()));
1001 else if (VA.getLocInfo() == CCValAssign::ZExt)
1002 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
1003 DAG.getValueType(VA.getValVT()));
1004
1005 if (VA.isExtInLoc())
1006 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00001007 else if (VA.getLocInfo() == CCValAssign::BCvt) {
1008 // If this is a short vector argument loaded from the stack,
1009 // extend from i64 to full vector size and then bitcast.
1010 assert(VA.getLocVT() == MVT::i64);
1011 assert(VA.getValVT().isVector());
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001012 Value = DAG.getBuildVector(MVT::v2i64, DL, {Value, DAG.getUNDEF(MVT::i64)});
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00001013 Value = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Value);
1014 } else
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001015 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
1016 return Value;
1017}
1018
1019// Value is a value of type VA.getValVT() that we need to copy into
1020// the location described by VA. Return a copy of Value converted to
1021// VA.getValVT(). The caller is responsible for handling indirect values.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001022static SDValue convertValVTToLocVT(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001023 CCValAssign &VA, SDValue Value) {
1024 switch (VA.getLocInfo()) {
1025 case CCValAssign::SExt:
1026 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
1027 case CCValAssign::ZExt:
1028 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
1029 case CCValAssign::AExt:
1030 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00001031 case CCValAssign::BCvt:
1032 // If this is a short vector argument to be stored to the stack,
1033 // bitcast to v2i64 and then extract first element.
1034 assert(VA.getLocVT() == MVT::i64);
1035 assert(VA.getValVT().isVector());
1036 Value = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Value);
1037 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VA.getLocVT(), Value,
1038 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001039 case CCValAssign::Full:
1040 return Value;
1041 default:
1042 llvm_unreachable("Unhandled getLocInfo()");
1043 }
1044}
1045
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001046SDValue SystemZTargetLowering::LowerFormalArguments(
1047 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
1048 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1049 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001050 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001051 MachineFrameInfo &MFI = MF.getFrameInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001052 MachineRegisterInfo &MRI = MF.getRegInfo();
1053 SystemZMachineFunctionInfo *FuncInfo =
Eric Christophera6734172015-01-31 00:06:45 +00001054 MF.getInfo<SystemZMachineFunctionInfo>();
1055 auto *TFL =
1056 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering());
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001057 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001058
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001059 // Detect unsupported vector argument types.
1060 if (Subtarget.hasVector())
1061 VerifyVectorTypes(Ins);
1062
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001063 // Assign locations to all of the incoming arguments.
1064 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001065 SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001066 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
1067
1068 unsigned NumFixedGPRs = 0;
1069 unsigned NumFixedFPRs = 0;
1070 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1071 SDValue ArgValue;
1072 CCValAssign &VA = ArgLocs[I];
1073 EVT LocVT = VA.getLocVT();
1074 if (VA.isRegLoc()) {
1075 // Arguments passed in registers
1076 const TargetRegisterClass *RC;
1077 switch (LocVT.getSimpleVT().SimpleTy) {
1078 default:
1079 // Integers smaller than i64 should be promoted to i64.
1080 llvm_unreachable("Unexpected argument type");
1081 case MVT::i32:
1082 NumFixedGPRs += 1;
1083 RC = &SystemZ::GR32BitRegClass;
1084 break;
1085 case MVT::i64:
1086 NumFixedGPRs += 1;
1087 RC = &SystemZ::GR64BitRegClass;
1088 break;
1089 case MVT::f32:
1090 NumFixedFPRs += 1;
1091 RC = &SystemZ::FP32BitRegClass;
1092 break;
1093 case MVT::f64:
1094 NumFixedFPRs += 1;
1095 RC = &SystemZ::FP64BitRegClass;
1096 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001097 case MVT::v16i8:
1098 case MVT::v8i16:
1099 case MVT::v4i32:
1100 case MVT::v2i64:
Ulrich Weigand80b3af72015-05-05 19:27:45 +00001101 case MVT::v4f32:
Ulrich Weigandcd808232015-05-05 19:26:48 +00001102 case MVT::v2f64:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001103 RC = &SystemZ::VR128BitRegClass;
1104 break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001105 }
1106
1107 unsigned VReg = MRI.createVirtualRegister(RC);
1108 MRI.addLiveIn(VA.getLocReg(), VReg);
1109 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
1110 } else {
1111 assert(VA.isMemLoc() && "Argument not register or memory");
1112
1113 // Create the frame index object for this incoming parameter.
Matthias Braun941a7052016-07-28 18:40:00 +00001114 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
1115 VA.getLocMemOffset(), true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001116
1117 // Create the SelectionDAG nodes corresponding to a load
1118 // from this parameter. Unpromoted ints and floats are
1119 // passed as right-justified 8-byte values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001120 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
1121 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001122 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
1123 DAG.getIntPtrConstant(4, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001124 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00001125 MachinePointerInfo::getFixedStack(MF, FI));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001126 }
1127
1128 // Convert the value of the argument register into the value that's
1129 // being passed.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001130 if (VA.getLocInfo() == CCValAssign::Indirect) {
Justin Lebar9c375812016-07-15 18:27:10 +00001131 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
1132 MachinePointerInfo()));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001133 // If the original argument was split (e.g. i128), we need
1134 // to load all parts of it here (using the same address).
1135 unsigned ArgIndex = Ins[I].OrigArgIndex;
1136 assert (Ins[I].PartOffset == 0);
1137 while (I + 1 != E && Ins[I + 1].OrigArgIndex == ArgIndex) {
1138 CCValAssign &PartVA = ArgLocs[I + 1];
1139 unsigned PartOffset = Ins[I + 1].PartOffset;
1140 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
1141 DAG.getIntPtrConstant(PartOffset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00001142 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
1143 MachinePointerInfo()));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001144 ++I;
1145 }
1146 } else
1147 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001148 }
1149
1150 if (IsVarArg) {
1151 // Save the number of non-varargs registers for later use by va_start, etc.
1152 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
1153 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
1154
1155 // Likewise the address (in the form of a frame index) of where the
1156 // first stack vararg would be. The 1-byte size here is arbitrary.
1157 int64_t StackSize = CCInfo.getNextStackOffset();
Matthias Braun941a7052016-07-28 18:40:00 +00001158 FuncInfo->setVarArgsFrameIndex(MFI.CreateFixedObject(1, StackSize, true));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001159
1160 // ...and a similar frame index for the caller-allocated save area
1161 // that will be used to store the incoming registers.
1162 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
Matthias Braun941a7052016-07-28 18:40:00 +00001163 unsigned RegSaveIndex = MFI.CreateFixedObject(1, RegSaveOffset, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001164 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
1165
1166 // Store the FPR varargs in the reserved frame slots. (We store the
1167 // GPRs as part of the prologue.)
1168 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
1169 SDValue MemOps[SystemZ::NumArgFPRs];
1170 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
1171 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
Matthias Braun941a7052016-07-28 18:40:00 +00001172 int FI = MFI.CreateFixedObject(8, RegSaveOffset + Offset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +00001173 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001174 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
1175 &SystemZ::FP64BitRegClass);
1176 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
1177 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00001178 MachinePointerInfo::getFixedStack(MF, FI));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001179 }
1180 // Join the stores, which are independent of one another.
1181 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00001182 makeArrayRef(&MemOps[NumFixedFPRs],
1183 SystemZ::NumArgFPRs-NumFixedFPRs));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001184 }
1185 }
1186
1187 return Chain;
1188}
1189
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +00001190static bool canUseSiblingCall(const CCState &ArgCCInfo,
Bryan Chan893110e2016-04-28 00:17:23 +00001191 SmallVectorImpl<CCValAssign> &ArgLocs,
1192 SmallVectorImpl<ISD::OutputArg> &Outs) {
Richard Sandiford709bda62013-08-19 12:42:31 +00001193 // Punt if there are any indirect or stack arguments, or if the call
Bryan Chan893110e2016-04-28 00:17:23 +00001194 // needs the callee-saved argument register R6, or if the call uses
1195 // the callee-saved register arguments SwiftSelf and SwiftError.
Richard Sandiford709bda62013-08-19 12:42:31 +00001196 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1197 CCValAssign &VA = ArgLocs[I];
1198 if (VA.getLocInfo() == CCValAssign::Indirect)
1199 return false;
1200 if (!VA.isRegLoc())
1201 return false;
1202 unsigned Reg = VA.getLocReg();
Richard Sandiford0755c932013-10-01 11:26:28 +00001203 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
Richard Sandiford709bda62013-08-19 12:42:31 +00001204 return false;
Bryan Chan893110e2016-04-28 00:17:23 +00001205 if (Outs[I].Flags.isSwiftSelf() || Outs[I].Flags.isSwiftError())
1206 return false;
Richard Sandiford709bda62013-08-19 12:42:31 +00001207 }
1208 return true;
1209}
1210
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001211SDValue
1212SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
1213 SmallVectorImpl<SDValue> &InVals) const {
1214 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001215 SDLoc &DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001216 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1217 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1218 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001219 SDValue Chain = CLI.Chain;
1220 SDValue Callee = CLI.Callee;
Richard Sandiford709bda62013-08-19 12:42:31 +00001221 bool &IsTailCall = CLI.IsTailCall;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001222 CallingConv::ID CallConv = CLI.CallConv;
1223 bool IsVarArg = CLI.IsVarArg;
1224 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +00001225 EVT PtrVT = getPointerTy(MF.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001226
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001227 // Detect unsupported vector argument and return types.
1228 if (Subtarget.hasVector()) {
1229 VerifyVectorTypes(Outs);
1230 VerifyVectorTypes(Ins);
1231 }
1232
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001233 // Analyze the operands of the call, assigning locations to each operand.
1234 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001235 SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001236 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
1237
Richard Sandiford709bda62013-08-19 12:42:31 +00001238 // We don't support GuaranteedTailCallOpt, only automatically-detected
1239 // sibling calls.
Bryan Chan893110e2016-04-28 00:17:23 +00001240 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs, Outs))
Richard Sandiford709bda62013-08-19 12:42:31 +00001241 IsTailCall = false;
1242
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001243 // Get a count of how many bytes are to be pushed on the stack.
1244 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1245
1246 // Mark the start of the call.
Richard Sandiford709bda62013-08-19 12:42:31 +00001247 if (!IsTailCall)
Serge Pavlovd526b132017-05-09 13:35:13 +00001248 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001249
1250 // Copy argument values to their designated locations.
1251 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
1252 SmallVector<SDValue, 8> MemOpChains;
1253 SDValue StackPtr;
1254 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1255 CCValAssign &VA = ArgLocs[I];
1256 SDValue ArgValue = OutVals[I];
1257
1258 if (VA.getLocInfo() == CCValAssign::Indirect) {
1259 // Store the argument in a stack slot and pass its address.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001260 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[I].ArgVT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001261 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Justin Lebar9c375812016-07-15 18:27:10 +00001262 MemOpChains.push_back(
1263 DAG.getStore(Chain, DL, ArgValue, SpillSlot,
1264 MachinePointerInfo::getFixedStack(MF, FI)));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001265 // If the original argument was split (e.g. i128), we need
1266 // to store all parts of it here (and pass just one address).
1267 unsigned ArgIndex = Outs[I].OrigArgIndex;
1268 assert (Outs[I].PartOffset == 0);
1269 while (I + 1 != E && Outs[I + 1].OrigArgIndex == ArgIndex) {
1270 SDValue PartValue = OutVals[I + 1];
1271 unsigned PartOffset = Outs[I + 1].PartOffset;
1272 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
1273 DAG.getIntPtrConstant(PartOffset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00001274 MemOpChains.push_back(
1275 DAG.getStore(Chain, DL, PartValue, Address,
1276 MachinePointerInfo::getFixedStack(MF, FI)));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001277 ++I;
1278 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001279 ArgValue = SpillSlot;
1280 } else
1281 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
1282
1283 if (VA.isRegLoc())
1284 // Queue up the argument copies and emit them at the end.
1285 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1286 else {
1287 assert(VA.isMemLoc() && "Argument not register or memory");
1288
1289 // Work out the address of the stack slot. Unpromoted ints and
1290 // floats are passed as right-justified 8-byte values.
1291 if (!StackPtr.getNode())
1292 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
1293 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
1294 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
1295 Offset += 4;
1296 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001297 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001298
1299 // Emit the store.
Justin Lebar9c375812016-07-15 18:27:10 +00001300 MemOpChains.push_back(
1301 DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001302 }
1303 }
1304
1305 // Join the stores, which are independent of one another.
1306 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001307 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001308
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001309 // Accept direct calls by converting symbolic call addresses to the
Richard Sandiford709bda62013-08-19 12:42:31 +00001310 // associated Target* opcodes. Force %r1 to be used for indirect
1311 // tail calls.
1312 SDValue Glue;
Richard Sandiford21f5d682014-03-06 11:22:58 +00001313 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001314 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
1315 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford21f5d682014-03-06 11:22:58 +00001316 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001317 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
1318 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford709bda62013-08-19 12:42:31 +00001319 } else if (IsTailCall) {
1320 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
1321 Glue = Chain.getValue(1);
1322 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
1323 }
1324
1325 // Build a sequence of copy-to-reg nodes, chained and glued together.
1326 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
1327 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
1328 RegsToPass[I].second, Glue);
1329 Glue = Chain.getValue(1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001330 }
1331
1332 // The first call operand is the chain and the second is the target address.
1333 SmallVector<SDValue, 8> Ops;
1334 Ops.push_back(Chain);
1335 Ops.push_back(Callee);
1336
1337 // Add argument registers to the end of the list so that they are
1338 // known live into the call.
1339 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
1340 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
1341 RegsToPass[I].second.getValueType()));
1342
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001343 // Add a register mask operand representing the call-preserved registers.
Eric Christophera6734172015-01-31 00:06:45 +00001344 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00001345 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001346 assert(Mask && "Missing call preserved mask for calling convention");
1347 Ops.push_back(DAG.getRegisterMask(Mask));
1348
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001349 // Glue the call to the argument copies, if any.
1350 if (Glue.getNode())
1351 Ops.push_back(Glue);
1352
1353 // Emit the call.
1354 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Richard Sandiford709bda62013-08-19 12:42:31 +00001355 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001356 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
1357 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001358 Glue = Chain.getValue(1);
1359
1360 // Mark the end of the call, which is glued to the call itself.
1361 Chain = DAG.getCALLSEQ_END(Chain,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001362 DAG.getConstant(NumBytes, DL, PtrVT, true),
1363 DAG.getConstant(0, DL, PtrVT, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001364 Glue, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001365 Glue = Chain.getValue(1);
1366
1367 // Assign locations to each value returned by this call.
1368 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001369 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001370 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
1371
1372 // Copy all of the result registers out of their specified physreg.
1373 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1374 CCValAssign &VA = RetLocs[I];
1375
1376 // Copy the value out, gluing the copy to the end of the call sequence.
1377 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
1378 VA.getLocVT(), Glue);
1379 Chain = RetValue.getValue(1);
1380 Glue = RetValue.getValue(2);
1381
1382 // Convert the value of the return register into the value that's
1383 // being returned.
1384 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
1385 }
1386
1387 return Chain;
1388}
1389
Ulrich Weiganda887f062015-08-13 13:37:06 +00001390bool SystemZTargetLowering::
1391CanLowerReturn(CallingConv::ID CallConv,
1392 MachineFunction &MF, bool isVarArg,
1393 const SmallVectorImpl<ISD::OutputArg> &Outs,
1394 LLVMContext &Context) const {
1395 // Detect unsupported vector return types.
1396 if (Subtarget.hasVector())
1397 VerifyVectorTypes(Outs);
1398
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001399 // Special case that we cannot easily detect in RetCC_SystemZ since
1400 // i128 is not a legal type.
1401 for (auto &Out : Outs)
1402 if (Out.ArgVT == MVT::i128)
1403 return false;
1404
Ulrich Weiganda887f062015-08-13 13:37:06 +00001405 SmallVector<CCValAssign, 16> RetLocs;
1406 CCState RetCCInfo(CallConv, isVarArg, MF, RetLocs, Context);
1407 return RetCCInfo.CheckReturn(Outs, RetCC_SystemZ);
1408}
1409
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001410SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001411SystemZTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1412 bool IsVarArg,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001413 const SmallVectorImpl<ISD::OutputArg> &Outs,
1414 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001415 const SDLoc &DL, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001416 MachineFunction &MF = DAG.getMachineFunction();
1417
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001418 // Detect unsupported vector return types.
1419 if (Subtarget.hasVector())
1420 VerifyVectorTypes(Outs);
1421
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001422 // Assign locations to each returned value.
1423 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001424 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001425 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
1426
1427 // Quick exit for void returns
1428 if (RetLocs.empty())
1429 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
1430
1431 // Copy the result values into the output registers.
1432 SDValue Glue;
1433 SmallVector<SDValue, 4> RetOps;
1434 RetOps.push_back(Chain);
1435 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1436 CCValAssign &VA = RetLocs[I];
1437 SDValue RetValue = OutVals[I];
1438
1439 // Make the return register live on exit.
1440 assert(VA.isRegLoc() && "Can only return in registers!");
1441
1442 // Promote the value as required.
1443 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
1444
1445 // Chain and glue the copies together.
1446 unsigned Reg = VA.getLocReg();
1447 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
1448 Glue = Chain.getValue(1);
1449 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
1450 }
1451
1452 // Update chain and glue.
1453 RetOps[0] = Chain;
1454 if (Glue.getNode())
1455 RetOps.push_back(Glue);
1456
Craig Topper48d114b2014-04-26 18:35:24 +00001457 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001458}
1459
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001460// Return true if Op is an intrinsic node with chain that returns the CC value
1461// as its only (other) argument. Provide the associated SystemZISD opcode and
1462// the mask of valid CC values if so.
1463static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode,
1464 unsigned &CCValid) {
1465 unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1466 switch (Id) {
1467 case Intrinsic::s390_tbegin:
1468 Opcode = SystemZISD::TBEGIN;
1469 CCValid = SystemZ::CCMASK_TBEGIN;
1470 return true;
1471
1472 case Intrinsic::s390_tbegin_nofloat:
1473 Opcode = SystemZISD::TBEGIN_NOFLOAT;
1474 CCValid = SystemZ::CCMASK_TBEGIN;
1475 return true;
1476
1477 case Intrinsic::s390_tend:
1478 Opcode = SystemZISD::TEND;
1479 CCValid = SystemZ::CCMASK_TEND;
1480 return true;
1481
1482 default:
1483 return false;
1484 }
1485}
1486
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001487// Return true if Op is an intrinsic node without chain that returns the
1488// CC value as its final argument. Provide the associated SystemZISD
1489// opcode and the mask of valid CC values if so.
1490static bool isIntrinsicWithCC(SDValue Op, unsigned &Opcode, unsigned &CCValid) {
1491 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1492 switch (Id) {
1493 case Intrinsic::s390_vpkshs:
1494 case Intrinsic::s390_vpksfs:
1495 case Intrinsic::s390_vpksgs:
1496 Opcode = SystemZISD::PACKS_CC;
1497 CCValid = SystemZ::CCMASK_VCMP;
1498 return true;
1499
1500 case Intrinsic::s390_vpklshs:
1501 case Intrinsic::s390_vpklsfs:
1502 case Intrinsic::s390_vpklsgs:
1503 Opcode = SystemZISD::PACKLS_CC;
1504 CCValid = SystemZ::CCMASK_VCMP;
1505 return true;
1506
1507 case Intrinsic::s390_vceqbs:
1508 case Intrinsic::s390_vceqhs:
1509 case Intrinsic::s390_vceqfs:
1510 case Intrinsic::s390_vceqgs:
1511 Opcode = SystemZISD::VICMPES;
1512 CCValid = SystemZ::CCMASK_VCMP;
1513 return true;
1514
1515 case Intrinsic::s390_vchbs:
1516 case Intrinsic::s390_vchhs:
1517 case Intrinsic::s390_vchfs:
1518 case Intrinsic::s390_vchgs:
1519 Opcode = SystemZISD::VICMPHS;
1520 CCValid = SystemZ::CCMASK_VCMP;
1521 return true;
1522
1523 case Intrinsic::s390_vchlbs:
1524 case Intrinsic::s390_vchlhs:
1525 case Intrinsic::s390_vchlfs:
1526 case Intrinsic::s390_vchlgs:
1527 Opcode = SystemZISD::VICMPHLS;
1528 CCValid = SystemZ::CCMASK_VCMP;
1529 return true;
1530
1531 case Intrinsic::s390_vtm:
1532 Opcode = SystemZISD::VTM;
1533 CCValid = SystemZ::CCMASK_VCMP;
1534 return true;
1535
1536 case Intrinsic::s390_vfaebs:
1537 case Intrinsic::s390_vfaehs:
1538 case Intrinsic::s390_vfaefs:
1539 Opcode = SystemZISD::VFAE_CC;
1540 CCValid = SystemZ::CCMASK_ANY;
1541 return true;
1542
1543 case Intrinsic::s390_vfaezbs:
1544 case Intrinsic::s390_vfaezhs:
1545 case Intrinsic::s390_vfaezfs:
1546 Opcode = SystemZISD::VFAEZ_CC;
1547 CCValid = SystemZ::CCMASK_ANY;
1548 return true;
1549
1550 case Intrinsic::s390_vfeebs:
1551 case Intrinsic::s390_vfeehs:
1552 case Intrinsic::s390_vfeefs:
1553 Opcode = SystemZISD::VFEE_CC;
1554 CCValid = SystemZ::CCMASK_ANY;
1555 return true;
1556
1557 case Intrinsic::s390_vfeezbs:
1558 case Intrinsic::s390_vfeezhs:
1559 case Intrinsic::s390_vfeezfs:
1560 Opcode = SystemZISD::VFEEZ_CC;
1561 CCValid = SystemZ::CCMASK_ANY;
1562 return true;
1563
1564 case Intrinsic::s390_vfenebs:
1565 case Intrinsic::s390_vfenehs:
1566 case Intrinsic::s390_vfenefs:
1567 Opcode = SystemZISD::VFENE_CC;
1568 CCValid = SystemZ::CCMASK_ANY;
1569 return true;
1570
1571 case Intrinsic::s390_vfenezbs:
1572 case Intrinsic::s390_vfenezhs:
1573 case Intrinsic::s390_vfenezfs:
1574 Opcode = SystemZISD::VFENEZ_CC;
1575 CCValid = SystemZ::CCMASK_ANY;
1576 return true;
1577
1578 case Intrinsic::s390_vistrbs:
1579 case Intrinsic::s390_vistrhs:
1580 case Intrinsic::s390_vistrfs:
1581 Opcode = SystemZISD::VISTR_CC;
1582 CCValid = SystemZ::CCMASK_0 | SystemZ::CCMASK_3;
1583 return true;
1584
1585 case Intrinsic::s390_vstrcbs:
1586 case Intrinsic::s390_vstrchs:
1587 case Intrinsic::s390_vstrcfs:
1588 Opcode = SystemZISD::VSTRC_CC;
1589 CCValid = SystemZ::CCMASK_ANY;
1590 return true;
1591
1592 case Intrinsic::s390_vstrczbs:
1593 case Intrinsic::s390_vstrczhs:
1594 case Intrinsic::s390_vstrczfs:
1595 Opcode = SystemZISD::VSTRCZ_CC;
1596 CCValid = SystemZ::CCMASK_ANY;
1597 return true;
1598
1599 case Intrinsic::s390_vfcedbs:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001600 case Intrinsic::s390_vfcesbs:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001601 Opcode = SystemZISD::VFCMPES;
1602 CCValid = SystemZ::CCMASK_VCMP;
1603 return true;
1604
1605 case Intrinsic::s390_vfchdbs:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001606 case Intrinsic::s390_vfchsbs:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001607 Opcode = SystemZISD::VFCMPHS;
1608 CCValid = SystemZ::CCMASK_VCMP;
1609 return true;
1610
1611 case Intrinsic::s390_vfchedbs:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001612 case Intrinsic::s390_vfchesbs:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001613 Opcode = SystemZISD::VFCMPHES;
1614 CCValid = SystemZ::CCMASK_VCMP;
1615 return true;
1616
1617 case Intrinsic::s390_vftcidb:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001618 case Intrinsic::s390_vftcisb:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001619 Opcode = SystemZISD::VFTCI;
1620 CCValid = SystemZ::CCMASK_VCMP;
1621 return true;
1622
Marcin Koscielnickicf7cc722016-07-10 14:41:22 +00001623 case Intrinsic::s390_tdc:
1624 Opcode = SystemZISD::TDC;
1625 CCValid = SystemZ::CCMASK_TDC;
1626 return true;
1627
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001628 default:
1629 return false;
1630 }
1631}
1632
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001633// Emit an intrinsic with chain with a glued value instead of its CC result.
1634static SDValue emitIntrinsicWithChainAndGlue(SelectionDAG &DAG, SDValue Op,
1635 unsigned Opcode) {
1636 // Copy all operands except the intrinsic ID.
1637 unsigned NumOps = Op.getNumOperands();
1638 SmallVector<SDValue, 6> Ops;
1639 Ops.reserve(NumOps - 1);
1640 Ops.push_back(Op.getOperand(0));
1641 for (unsigned I = 2; I < NumOps; ++I)
1642 Ops.push_back(Op.getOperand(I));
1643
1644 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
1645 SDVTList RawVTs = DAG.getVTList(MVT::Other, MVT::Glue);
1646 SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1647 SDValue OldChain = SDValue(Op.getNode(), 1);
1648 SDValue NewChain = SDValue(Intr.getNode(), 0);
1649 DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain);
1650 return Intr;
1651}
1652
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001653// Emit an intrinsic with a glued value instead of its CC result.
1654static SDValue emitIntrinsicWithGlue(SelectionDAG &DAG, SDValue Op,
1655 unsigned Opcode) {
1656 // Copy all operands except the intrinsic ID.
1657 unsigned NumOps = Op.getNumOperands();
1658 SmallVector<SDValue, 6> Ops;
1659 Ops.reserve(NumOps - 1);
1660 for (unsigned I = 1; I < NumOps; ++I)
1661 Ops.push_back(Op.getOperand(I));
1662
1663 if (Op->getNumValues() == 1)
1664 return DAG.getNode(Opcode, SDLoc(Op), MVT::Glue, Ops);
1665 assert(Op->getNumValues() == 2 && "Expected exactly one non-CC result");
1666 SDVTList RawVTs = DAG.getVTList(Op->getValueType(0), MVT::Glue);
1667 return DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1668}
1669
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001670// CC is a comparison that will be implemented using an integer or
1671// floating-point comparison. Return the condition code mask for
1672// a branch on true. In the integer case, CCMASK_CMP_UO is set for
1673// unsigned comparisons and clear for signed ones. In the floating-point
1674// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1675static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1676#define CONV(X) \
1677 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1678 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1679 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1680
1681 switch (CC) {
1682 default:
1683 llvm_unreachable("Invalid integer condition!");
1684
1685 CONV(EQ);
1686 CONV(NE);
1687 CONV(GT);
1688 CONV(GE);
1689 CONV(LT);
1690 CONV(LE);
1691
1692 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1693 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1694 }
1695#undef CONV
1696}
1697
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001698// Return a sequence for getting a 1 from an IPM result when CC has a
1699// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1700// The handling of CC values outside CCValid doesn't matter.
1701static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1702 // Deal with cases where the result can be taken directly from a bit
1703 // of the IPM result.
1704 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1705 return IPMConversion(0, 0, SystemZ::IPM_CC);
1706 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1707 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1708
1709 // Deal with cases where we can add a value to force the sign bit
1710 // to contain the right value. Putting the bit in 31 means we can
1711 // use SRL rather than RISBG(L), and also makes it easier to get a
1712 // 0/-1 value, so it has priority over the other tests below.
1713 //
1714 // These sequences rely on the fact that the upper two bits of the
1715 // IPM result are zero.
1716 uint64_t TopBit = uint64_t(1) << 31;
1717 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1718 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1719 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1720 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1721 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1722 | SystemZ::CCMASK_1
1723 | SystemZ::CCMASK_2)))
1724 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1725 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1726 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1727 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1728 | SystemZ::CCMASK_2
1729 | SystemZ::CCMASK_3)))
1730 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1731
1732 // Next try inverting the value and testing a bit. 0/1 could be
1733 // handled this way too, but we dealt with that case above.
1734 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1735 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1736
1737 // Handle cases where adding a value forces a non-sign bit to contain
1738 // the right value.
1739 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1740 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1741 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1742 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1743
Alp Tokercb402912014-01-24 17:20:08 +00001744 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001745 // can be done by inverting the low CC bit and applying one of the
1746 // sign-based extractions above.
1747 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1748 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1749 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1750 return IPMConversion(1 << SystemZ::IPM_CC,
1751 TopBit - (3 << SystemZ::IPM_CC), 31);
1752 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1753 | SystemZ::CCMASK_1
1754 | SystemZ::CCMASK_3)))
1755 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1756 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1757 | SystemZ::CCMASK_2
1758 | SystemZ::CCMASK_3)))
1759 return IPMConversion(1 << SystemZ::IPM_CC,
1760 TopBit - (1 << SystemZ::IPM_CC), 31);
1761
1762 llvm_unreachable("Unexpected CC combination");
1763}
1764
Richard Sandifordd420f732013-12-13 15:28:45 +00001765// If C can be converted to a comparison against zero, adjust the operands
Richard Sandiforda0757082013-08-01 10:29:45 +00001766// as necessary.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001767static void adjustZeroCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001768 if (C.ICmpType == SystemZICMP::UnsignedOnly)
Richard Sandiforda0757082013-08-01 10:29:45 +00001769 return;
1770
Richard Sandiford21f5d682014-03-06 11:22:58 +00001771 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
Richard Sandiforda0757082013-08-01 10:29:45 +00001772 if (!ConstOp1)
1773 return;
1774
1775 int64_t Value = ConstOp1->getSExtValue();
Richard Sandifordd420f732013-12-13 15:28:45 +00001776 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1777 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1778 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1779 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1780 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001781 C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType());
Richard Sandiforda0757082013-08-01 10:29:45 +00001782 }
1783}
1784
Richard Sandifordd420f732013-12-13 15:28:45 +00001785// If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1786// adjust the operands as necessary.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001787static void adjustSubwordCmp(SelectionDAG &DAG, const SDLoc &DL,
1788 Comparison &C) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001789 // For us to make any changes, it must a comparison between a single-use
1790 // load and a constant.
Richard Sandifordd420f732013-12-13 15:28:45 +00001791 if (!C.Op0.hasOneUse() ||
1792 C.Op0.getOpcode() != ISD::LOAD ||
1793 C.Op1.getOpcode() != ISD::Constant)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001794 return;
1795
1796 // We must have an 8- or 16-bit load.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001797 auto *Load = cast<LoadSDNode>(C.Op0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001798 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1799 if (NumBits != 8 && NumBits != 16)
1800 return;
1801
1802 // The load must be an extending one and the constant must be within the
1803 // range of the unextended value.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001804 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001805 uint64_t Value = ConstOp1->getZExtValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001806 uint64_t Mask = (1 << NumBits) - 1;
1807 if (Load->getExtensionType() == ISD::SEXTLOAD) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001808 // Make sure that ConstOp1 is in range of C.Op0.
1809 int64_t SignedValue = ConstOp1->getSExtValue();
1810 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001811 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001812 if (C.ICmpType != SystemZICMP::SignedOnly) {
1813 // Unsigned comparison between two sign-extended values is equivalent
1814 // to unsigned comparison between two zero-extended values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001815 Value &= Mask;
Richard Sandifordd420f732013-12-13 15:28:45 +00001816 } else if (NumBits == 8) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001817 // Try to treat the comparison as unsigned, so that we can use CLI.
1818 // Adjust CCMask and Value as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001819 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001820 // Test whether the high bit of the byte is set.
Richard Sandifordd420f732013-12-13 15:28:45 +00001821 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1822 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001823 // Test whether the high bit of the byte is clear.
Richard Sandifordd420f732013-12-13 15:28:45 +00001824 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001825 else
1826 // No instruction exists for this combination.
1827 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001828 C.ICmpType = SystemZICMP::UnsignedOnly;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001829 }
1830 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1831 if (Value > Mask)
1832 return;
Ulrich Weigand47f36492015-12-16 18:04:06 +00001833 // If the constant is in range, we can use any comparison.
1834 C.ICmpType = SystemZICMP::Any;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001835 } else
1836 return;
1837
1838 // Make sure that the first operand is an i32 of the right extension type.
Richard Sandifordd420f732013-12-13 15:28:45 +00001839 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1840 ISD::SEXTLOAD :
1841 ISD::ZEXTLOAD);
1842 if (C.Op0.getValueType() != MVT::i32 ||
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001843 Load->getExtensionType() != ExtType)
Justin Lebar9c375812016-07-15 18:27:10 +00001844 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32, Load->getChain(),
1845 Load->getBasePtr(), Load->getPointerInfo(),
1846 Load->getMemoryVT(), Load->getAlignment(),
1847 Load->getMemOperand()->getFlags());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001848
1849 // Make sure that the second operand is an i32 with the right value.
Richard Sandifordd420f732013-12-13 15:28:45 +00001850 if (C.Op1.getValueType() != MVT::i32 ||
1851 Value != ConstOp1->getZExtValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001852 C.Op1 = DAG.getConstant(Value, DL, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001853}
1854
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001855// Return true if Op is either an unextended load, or a load suitable
1856// for integer register-memory comparisons of type ICmpType.
1857static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001858 auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001859 if (Load) {
1860 // There are no instructions to compare a register with a memory byte.
1861 if (Load->getMemoryVT() == MVT::i8)
1862 return false;
1863 // Otherwise decide on extension type.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001864 switch (Load->getExtensionType()) {
1865 case ISD::NON_EXTLOAD:
Richard Sandiford24e597b2013-08-23 11:27:19 +00001866 return true;
1867 case ISD::SEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001868 return ICmpType != SystemZICMP::UnsignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001869 case ISD::ZEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001870 return ICmpType != SystemZICMP::SignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001871 default:
1872 break;
1873 }
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001874 }
Richard Sandiford24e597b2013-08-23 11:27:19 +00001875 return false;
1876}
1877
Richard Sandifordd420f732013-12-13 15:28:45 +00001878// Return true if it is better to swap the operands of C.
1879static bool shouldSwapCmpOperands(const Comparison &C) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001880 // Leave f128 comparisons alone, since they have no memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001881 if (C.Op0.getValueType() == MVT::f128)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001882 return false;
1883
1884 // Always keep a floating-point constant second, since comparisons with
1885 // zero can use LOAD TEST and comparisons with other constants make a
1886 // natural memory operand.
Richard Sandifordd420f732013-12-13 15:28:45 +00001887 if (isa<ConstantFPSDNode>(C.Op1))
Richard Sandiford24e597b2013-08-23 11:27:19 +00001888 return false;
1889
1890 // Never swap comparisons with zero since there are many ways to optimize
1891 // those later.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001892 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001893 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001894 return false;
1895
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001896 // Also keep natural memory operands second if the loaded value is
1897 // only used here. Several comparisons have memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001898 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001899 return false;
1900
Richard Sandiford24e597b2013-08-23 11:27:19 +00001901 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1902 // In that case we generally prefer the memory to be second.
Richard Sandifordd420f732013-12-13 15:28:45 +00001903 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001904 // The only exceptions are when the second operand is a constant and
1905 // we can use things like CHHSI.
Richard Sandifordd420f732013-12-13 15:28:45 +00001906 if (!ConstOp1)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001907 return true;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001908 // The unsigned memory-immediate instructions can handle 16-bit
1909 // unsigned integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001910 if (C.ICmpType != SystemZICMP::SignedOnly &&
1911 isUInt<16>(ConstOp1->getZExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001912 return false;
1913 // The signed memory-immediate instructions can handle 16-bit
1914 // signed integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001915 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1916 isInt<16>(ConstOp1->getSExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001917 return false;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001918 return true;
1919 }
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001920
1921 // Try to promote the use of CGFR and CLGFR.
Richard Sandifordd420f732013-12-13 15:28:45 +00001922 unsigned Opcode0 = C.Op0.getOpcode();
1923 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001924 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001925 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001926 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001927 if (C.ICmpType != SystemZICMP::SignedOnly &&
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001928 Opcode0 == ISD::AND &&
Richard Sandifordd420f732013-12-13 15:28:45 +00001929 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1930 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001931 return true;
1932
Richard Sandiford24e597b2013-08-23 11:27:19 +00001933 return false;
1934}
1935
Richard Sandiford73170f82013-12-11 11:45:08 +00001936// Return a version of comparison CC mask CCMask in which the LT and GT
1937// actions are swapped.
1938static unsigned reverseCCMask(unsigned CCMask) {
1939 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1940 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1941 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1942 (CCMask & SystemZ::CCMASK_CMP_UO));
1943}
1944
Richard Sandiford0847c452013-12-13 15:50:30 +00001945// Check whether C tests for equality between X and Y and whether X - Y
1946// or Y - X is also computed. In that case it's better to compare the
1947// result of the subtraction against zero.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001948static void adjustForSubtraction(SelectionDAG &DAG, const SDLoc &DL,
1949 Comparison &C) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001950 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1951 C.CCMask == SystemZ::CCMASK_CMP_NE) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001952 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001953 SDNode *N = *I;
1954 if (N->getOpcode() == ISD::SUB &&
1955 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1956 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1957 C.Op0 = SDValue(N, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001958 C.Op1 = DAG.getConstant(0, DL, N->getValueType(0));
Richard Sandiford0847c452013-12-13 15:50:30 +00001959 return;
1960 }
1961 }
1962 }
1963}
1964
Richard Sandifordd420f732013-12-13 15:28:45 +00001965// Check whether C compares a floating-point value with zero and if that
1966// floating-point value is also negated. In this case we can use the
1967// negation to set CC, so avoiding separate LOAD AND TEST and
1968// LOAD (NEGATIVE/COMPLEMENT) instructions.
1969static void adjustForFNeg(Comparison &C) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001970 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
Richard Sandiford73170f82013-12-11 11:45:08 +00001971 if (C1 && C1->isZero()) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001972 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford73170f82013-12-11 11:45:08 +00001973 SDNode *N = *I;
1974 if (N->getOpcode() == ISD::FNEG) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001975 C.Op0 = SDValue(N, 0);
1976 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford73170f82013-12-11 11:45:08 +00001977 return;
1978 }
1979 }
1980 }
1981}
1982
Richard Sandifordd420f732013-12-13 15:28:45 +00001983// Check whether C compares (shl X, 32) with 0 and whether X is
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001984// also sign-extended. In that case it is better to test the result
1985// of the sign extension using LTGFR.
1986//
1987// This case is important because InstCombine transforms a comparison
1988// with (sext (trunc X)) into a comparison with (shl X, 32).
Richard Sandifordd420f732013-12-13 15:28:45 +00001989static void adjustForLTGFR(Comparison &C) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001990 // Check for a comparison between (shl X, 32) and 0.
Richard Sandifordd420f732013-12-13 15:28:45 +00001991 if (C.Op0.getOpcode() == ISD::SHL &&
1992 C.Op0.getValueType() == MVT::i64 &&
1993 C.Op1.getOpcode() == ISD::Constant &&
1994 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001995 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001996 if (C1 && C1->getZExtValue() == 32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001997 SDValue ShlOp0 = C.Op0.getOperand(0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001998 // See whether X has any SIGN_EXTEND_INREG uses.
Richard Sandiford28c111e2014-03-06 11:00:15 +00001999 for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00002000 SDNode *N = *I;
2001 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
2002 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00002003 C.Op0 = SDValue(N, 0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00002004 return;
2005 }
2006 }
2007 }
2008 }
2009}
2010
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002011// If C compares the truncation of an extending load, try to compare
2012// the untruncated value instead. This exposes more opportunities to
2013// reuse CC.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002014static void adjustICmpTruncate(SelectionDAG &DAG, const SDLoc &DL,
2015 Comparison &C) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002016 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
2017 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
2018 C.Op1.getOpcode() == ISD::Constant &&
2019 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002020 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002021 if (L->getMemoryVT().getStoreSizeInBits() <= C.Op0.getValueSizeInBits()) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002022 unsigned Type = L->getExtensionType();
2023 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
2024 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
2025 C.Op0 = C.Op0.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002026 C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType());
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002027 }
2028 }
2029 }
2030}
2031
Richard Sandiford030c1652013-09-13 09:09:50 +00002032// Return true if shift operation N has an in-range constant shift value.
2033// Store it in ShiftVal if so.
2034static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002035 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
Richard Sandiford030c1652013-09-13 09:09:50 +00002036 if (!Shift)
2037 return false;
2038
2039 uint64_t Amount = Shift->getZExtValue();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002040 if (Amount >= N.getValueSizeInBits())
Richard Sandiford030c1652013-09-13 09:09:50 +00002041 return false;
2042
2043 ShiftVal = Amount;
2044 return true;
2045}
2046
2047// Check whether an AND with Mask is suitable for a TEST UNDER MASK
2048// instruction and whether the CC value is descriptive enough to handle
2049// a comparison of type Opcode between the AND result and CmpVal.
2050// CCMask says which comparison result is being tested and BitSize is
2051// the number of bits in the operands. If TEST UNDER MASK can be used,
2052// return the corresponding CC mask, otherwise return 0.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002053static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
2054 uint64_t Mask, uint64_t CmpVal,
2055 unsigned ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00002056 assert(Mask != 0 && "ANDs with zero should have been removed by now");
2057
Richard Sandiford030c1652013-09-13 09:09:50 +00002058 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
2059 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
2060 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
2061 return 0;
2062
Richard Sandiford113c8702013-09-03 15:38:35 +00002063 // Work out the masks for the lowest and highest bits.
2064 unsigned HighShift = 63 - countLeadingZeros(Mask);
2065 uint64_t High = uint64_t(1) << HighShift;
2066 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
2067
2068 // Signed ordered comparisons are effectively unsigned if the sign
2069 // bit is dropped.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002070 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
Richard Sandiford113c8702013-09-03 15:38:35 +00002071
2072 // Check for equality comparisons with 0, or the equivalent.
2073 if (CmpVal == 0) {
2074 if (CCMask == SystemZ::CCMASK_CMP_EQ)
2075 return SystemZ::CCMASK_TM_ALL_0;
2076 if (CCMask == SystemZ::CCMASK_CMP_NE)
2077 return SystemZ::CCMASK_TM_SOME_1;
2078 }
Ulrich Weigand4a4d4ab2016-02-01 18:31:19 +00002079 if (EffectivelyUnsigned && CmpVal > 0 && CmpVal <= Low) {
Richard Sandiford113c8702013-09-03 15:38:35 +00002080 if (CCMask == SystemZ::CCMASK_CMP_LT)
2081 return SystemZ::CCMASK_TM_ALL_0;
2082 if (CCMask == SystemZ::CCMASK_CMP_GE)
2083 return SystemZ::CCMASK_TM_SOME_1;
2084 }
2085 if (EffectivelyUnsigned && CmpVal < Low) {
2086 if (CCMask == SystemZ::CCMASK_CMP_LE)
2087 return SystemZ::CCMASK_TM_ALL_0;
2088 if (CCMask == SystemZ::CCMASK_CMP_GT)
2089 return SystemZ::CCMASK_TM_SOME_1;
2090 }
2091
2092 // Check for equality comparisons with the mask, or the equivalent.
2093 if (CmpVal == Mask) {
2094 if (CCMask == SystemZ::CCMASK_CMP_EQ)
2095 return SystemZ::CCMASK_TM_ALL_1;
2096 if (CCMask == SystemZ::CCMASK_CMP_NE)
2097 return SystemZ::CCMASK_TM_SOME_0;
2098 }
2099 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
2100 if (CCMask == SystemZ::CCMASK_CMP_GT)
2101 return SystemZ::CCMASK_TM_ALL_1;
2102 if (CCMask == SystemZ::CCMASK_CMP_LE)
2103 return SystemZ::CCMASK_TM_SOME_0;
2104 }
2105 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
2106 if (CCMask == SystemZ::CCMASK_CMP_GE)
2107 return SystemZ::CCMASK_TM_ALL_1;
2108 if (CCMask == SystemZ::CCMASK_CMP_LT)
2109 return SystemZ::CCMASK_TM_SOME_0;
2110 }
2111
2112 // Check for ordered comparisons with the top bit.
2113 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
2114 if (CCMask == SystemZ::CCMASK_CMP_LE)
2115 return SystemZ::CCMASK_TM_MSB_0;
2116 if (CCMask == SystemZ::CCMASK_CMP_GT)
2117 return SystemZ::CCMASK_TM_MSB_1;
2118 }
2119 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
2120 if (CCMask == SystemZ::CCMASK_CMP_LT)
2121 return SystemZ::CCMASK_TM_MSB_0;
2122 if (CCMask == SystemZ::CCMASK_CMP_GE)
2123 return SystemZ::CCMASK_TM_MSB_1;
2124 }
2125
2126 // If there are just two bits, we can do equality checks for Low and High
2127 // as well.
2128 if (Mask == Low + High) {
2129 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
2130 return SystemZ::CCMASK_TM_MIXED_MSB_0;
2131 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
2132 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
2133 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
2134 return SystemZ::CCMASK_TM_MIXED_MSB_1;
2135 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
2136 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
2137 }
2138
2139 // Looks like we've exhausted our options.
2140 return 0;
2141}
2142
Richard Sandifordd420f732013-12-13 15:28:45 +00002143// See whether C can be implemented as a TEST UNDER MASK instruction.
2144// Update the arguments with the TM version if so.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002145static void adjustForTestUnderMask(SelectionDAG &DAG, const SDLoc &DL,
2146 Comparison &C) {
Richard Sandiford113c8702013-09-03 15:38:35 +00002147 // Check that we have a comparison with a constant.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002148 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00002149 if (!ConstOp1)
Richard Sandiford35b9be22013-08-28 10:31:43 +00002150 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00002151 uint64_t CmpVal = ConstOp1->getZExtValue();
Richard Sandiford35b9be22013-08-28 10:31:43 +00002152
2153 // Check whether the nonconstant input is an AND with a constant mask.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002154 Comparison NewC(C);
2155 uint64_t MaskVal;
Craig Topper062a2ba2014-04-25 05:30:21 +00002156 ConstantSDNode *Mask = nullptr;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002157 if (C.Op0.getOpcode() == ISD::AND) {
2158 NewC.Op0 = C.Op0.getOperand(0);
2159 NewC.Op1 = C.Op0.getOperand(1);
2160 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
2161 if (!Mask)
2162 return;
2163 MaskVal = Mask->getZExtValue();
2164 } else {
2165 // There is no instruction to compare with a 64-bit immediate
2166 // so use TMHH instead if possible. We need an unsigned ordered
2167 // comparison with an i64 immediate.
2168 if (NewC.Op0.getValueType() != MVT::i64 ||
2169 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
2170 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
2171 NewC.ICmpType == SystemZICMP::SignedOnly)
2172 return;
2173 // Convert LE and GT comparisons into LT and GE.
2174 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
2175 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
2176 if (CmpVal == uint64_t(-1))
2177 return;
2178 CmpVal += 1;
2179 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
2180 }
2181 // If the low N bits of Op1 are zero than the low N bits of Op0 can
2182 // be masked off without changing the result.
2183 MaskVal = -(CmpVal & -CmpVal);
2184 NewC.ICmpType = SystemZICMP::UnsignedOnly;
2185 }
Ulrich Weigandb8d76fb2015-03-30 13:46:59 +00002186 if (!MaskVal)
2187 return;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002188
Richard Sandiford113c8702013-09-03 15:38:35 +00002189 // Check whether the combination of mask, comparison value and comparison
2190 // type are suitable.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002191 unsigned BitSize = NewC.Op0.getValueSizeInBits();
Richard Sandiford030c1652013-09-13 09:09:50 +00002192 unsigned NewCCMask, ShiftVal;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002193 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2194 NewC.Op0.getOpcode() == ISD::SHL &&
2195 isSimpleShift(NewC.Op0, ShiftVal) &&
Jonas Paulsson8c336472017-06-26 13:38:27 +00002196 (MaskVal >> ShiftVal != 0) &&
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002197 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
2198 MaskVal >> ShiftVal,
Richard Sandiford030c1652013-09-13 09:09:50 +00002199 CmpVal >> ShiftVal,
2200 SystemZICMP::Any))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002201 NewC.Op0 = NewC.Op0.getOperand(0);
2202 MaskVal >>= ShiftVal;
2203 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2204 NewC.Op0.getOpcode() == ISD::SRL &&
2205 isSimpleShift(NewC.Op0, ShiftVal) &&
Jonas Paulsson8c336472017-06-26 13:38:27 +00002206 (MaskVal << ShiftVal != 0) &&
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002207 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
Richard Sandiford030c1652013-09-13 09:09:50 +00002208 MaskVal << ShiftVal,
2209 CmpVal << ShiftVal,
2210 SystemZICMP::UnsignedOnly))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002211 NewC.Op0 = NewC.Op0.getOperand(0);
2212 MaskVal <<= ShiftVal;
Richard Sandiford030c1652013-09-13 09:09:50 +00002213 } else {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002214 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
2215 NewC.ICmpType);
Richard Sandiford030c1652013-09-13 09:09:50 +00002216 if (!NewCCMask)
2217 return;
2218 }
Richard Sandiford113c8702013-09-03 15:38:35 +00002219
Richard Sandiford35b9be22013-08-28 10:31:43 +00002220 // Go ahead and make the change.
Richard Sandifordd420f732013-12-13 15:28:45 +00002221 C.Opcode = SystemZISD::TM;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002222 C.Op0 = NewC.Op0;
2223 if (Mask && Mask->getZExtValue() == MaskVal)
2224 C.Op1 = SDValue(Mask, 0);
2225 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002226 C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType());
Richard Sandifordd420f732013-12-13 15:28:45 +00002227 C.CCValid = SystemZ::CCMASK_TM;
2228 C.CCMask = NewCCMask;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002229}
2230
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002231// Return a Comparison that tests the condition-code result of intrinsic
2232// node Call against constant integer CC using comparison code Cond.
2233// Opcode is the opcode of the SystemZISD operation for the intrinsic
2234// and CCValid is the set of possible condition-code results.
2235static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode,
2236 SDValue Call, unsigned CCValid, uint64_t CC,
2237 ISD::CondCode Cond) {
2238 Comparison C(Call, SDValue());
2239 C.Opcode = Opcode;
2240 C.CCValid = CCValid;
2241 if (Cond == ISD::SETEQ)
2242 // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3.
2243 C.CCMask = CC < 4 ? 1 << (3 - CC) : 0;
2244 else if (Cond == ISD::SETNE)
2245 // ...and the inverse of that.
2246 C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1;
2247 else if (Cond == ISD::SETLT || Cond == ISD::SETULT)
2248 // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3,
2249 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002250 C.CCMask = CC < 4 ? ~0U << (4 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002251 else if (Cond == ISD::SETGE || Cond == ISD::SETUGE)
2252 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002253 C.CCMask = CC < 4 ? ~(~0U << (4 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002254 else if (Cond == ISD::SETLE || Cond == ISD::SETULE)
2255 // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true),
2256 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002257 C.CCMask = CC < 4 ? ~0U << (3 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002258 else if (Cond == ISD::SETGT || Cond == ISD::SETUGT)
2259 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002260 C.CCMask = CC < 4 ? ~(~0U << (3 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002261 else
2262 llvm_unreachable("Unexpected integer comparison type");
2263 C.CCMask &= CCValid;
2264 return C;
2265}
2266
Richard Sandifordd420f732013-12-13 15:28:45 +00002267// Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
2268static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002269 ISD::CondCode Cond, const SDLoc &DL) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002270 if (CmpOp1.getOpcode() == ISD::Constant) {
2271 uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue();
2272 unsigned Opcode, CCValid;
2273 if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
2274 CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) &&
2275 isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid))
2276 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002277 if (CmpOp0.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
2278 CmpOp0.getResNo() == CmpOp0->getNumValues() - 1 &&
2279 isIntrinsicWithCC(CmpOp0, Opcode, CCValid))
2280 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002281 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002282 Comparison C(CmpOp0, CmpOp1);
2283 C.CCMask = CCMaskForCondCode(Cond);
2284 if (C.Op0.getValueType().isFloatingPoint()) {
2285 C.CCValid = SystemZ::CCMASK_FCMP;
2286 C.Opcode = SystemZISD::FCMP;
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002287 adjustForFNeg(C);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002288 } else {
Richard Sandifordd420f732013-12-13 15:28:45 +00002289 C.CCValid = SystemZ::CCMASK_ICMP;
2290 C.Opcode = SystemZISD::ICMP;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002291 // Choose the type of comparison. Equality and inequality tests can
2292 // use either signed or unsigned comparisons. The choice also doesn't
2293 // matter if both sign bits are known to be clear. In those cases we
2294 // want to give the main isel code the freedom to choose whichever
2295 // form fits best.
Richard Sandifordd420f732013-12-13 15:28:45 +00002296 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
2297 C.CCMask == SystemZ::CCMASK_CMP_NE ||
2298 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
2299 C.ICmpType = SystemZICMP::Any;
2300 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
2301 C.ICmpType = SystemZICMP::UnsignedOnly;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002302 else
Richard Sandifordd420f732013-12-13 15:28:45 +00002303 C.ICmpType = SystemZICMP::SignedOnly;
2304 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002305 adjustZeroCmp(DAG, DL, C);
2306 adjustSubwordCmp(DAG, DL, C);
2307 adjustForSubtraction(DAG, DL, C);
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002308 adjustForLTGFR(C);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002309 adjustICmpTruncate(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002310 }
2311
Richard Sandifordd420f732013-12-13 15:28:45 +00002312 if (shouldSwapCmpOperands(C)) {
2313 std::swap(C.Op0, C.Op1);
2314 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford24e597b2013-08-23 11:27:19 +00002315 }
2316
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002317 adjustForTestUnderMask(DAG, DL, C);
Richard Sandifordd420f732013-12-13 15:28:45 +00002318 return C;
2319}
2320
2321// Emit the comparison instruction described by C.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002322static SDValue emitCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002323 if (!C.Op1.getNode()) {
2324 SDValue Op;
2325 switch (C.Op0.getOpcode()) {
2326 case ISD::INTRINSIC_W_CHAIN:
2327 Op = emitIntrinsicWithChainAndGlue(DAG, C.Op0, C.Opcode);
2328 break;
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002329 case ISD::INTRINSIC_WO_CHAIN:
2330 Op = emitIntrinsicWithGlue(DAG, C.Op0, C.Opcode);
2331 break;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002332 default:
2333 llvm_unreachable("Invalid comparison operands");
2334 }
2335 return SDValue(Op.getNode(), Op->getNumValues() - 1);
2336 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002337 if (C.Opcode == SystemZISD::ICMP)
2338 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002339 DAG.getConstant(C.ICmpType, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002340 if (C.Opcode == SystemZISD::TM) {
2341 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
2342 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
2343 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002344 DAG.getConstant(RegisterOnly, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002345 }
2346 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002347}
2348
Richard Sandiford7d86e472013-08-21 09:34:56 +00002349// Implement a 32-bit *MUL_LOHI operation by extending both operands to
2350// 64 bits. Extend is the extension type to use. Store the high part
2351// in Hi and the low part in Lo.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002352static void lowerMUL_LOHI32(SelectionDAG &DAG, const SDLoc &DL, unsigned Extend,
2353 SDValue Op0, SDValue Op1, SDValue &Hi,
2354 SDValue &Lo) {
Richard Sandiford7d86e472013-08-21 09:34:56 +00002355 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
2356 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
2357 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002358 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
2359 DAG.getConstant(32, DL, MVT::i64));
Richard Sandiford7d86e472013-08-21 09:34:56 +00002360 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
2361 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
2362}
2363
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002364// Lower a binary operation that produces two VT results, one in each
2365// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
Ulrich Weigand43579cf2017-07-05 13:17:31 +00002366// and Opcode performs the GR128 operation. Store the even register result
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002367// in Even and the odd register result in Odd.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002368static void lowerGR128Binary(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigand43579cf2017-07-05 13:17:31 +00002369 unsigned Opcode, SDValue Op0, SDValue Op1,
2370 SDValue &Even, SDValue &Odd) {
2371 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped, Op0, Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002372 bool Is32Bit = is32Bit(VT);
Richard Sandifordd8163202013-09-13 09:12:44 +00002373 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
2374 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002375}
2376
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002377// Return an i32 value that is 1 if the CC value produced by Glue is
2378// in the mask CCMask and 0 otherwise. CC is known to have a value
2379// in CCValid, so other values can be ignored.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002380static SDValue emitSETCC(SelectionDAG &DAG, const SDLoc &DL, SDValue Glue,
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002381 unsigned CCValid, unsigned CCMask) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002382 IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
2383 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
2384
2385 if (Conversion.XORValue)
2386 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002387 DAG.getConstant(Conversion.XORValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002388
2389 if (Conversion.AddValue)
2390 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002391 DAG.getConstant(Conversion.AddValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002392
2393 // The SHR/AND sequence should get optimized to an RISBG.
2394 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002395 DAG.getConstant(Conversion.Bit, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002396 if (Conversion.Bit != 31)
2397 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002398 DAG.getConstant(1, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002399 return Result;
2400}
2401
Ulrich Weigandcd808232015-05-05 19:26:48 +00002402// Return the SystemISD vector comparison operation for CC, or 0 if it cannot
2403// be done directly. IsFP is true if CC is for a floating-point rather than
2404// integer comparison.
2405static unsigned getVectorComparison(ISD::CondCode CC, bool IsFP) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002406 switch (CC) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002407 case ISD::SETOEQ:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002408 case ISD::SETEQ:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002409 return IsFP ? SystemZISD::VFCMPE : SystemZISD::VICMPE;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002410
Ulrich Weigandcd808232015-05-05 19:26:48 +00002411 case ISD::SETOGE:
2412 case ISD::SETGE:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002413 return IsFP ? SystemZISD::VFCMPHE : static_cast<SystemZISD::NodeType>(0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002414
2415 case ISD::SETOGT:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002416 case ISD::SETGT:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002417 return IsFP ? SystemZISD::VFCMPH : SystemZISD::VICMPH;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002418
2419 case ISD::SETUGT:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002420 return IsFP ? static_cast<SystemZISD::NodeType>(0) : SystemZISD::VICMPHL;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002421
2422 default:
2423 return 0;
2424 }
2425}
2426
2427// Return the SystemZISD vector comparison operation for CC or its inverse,
2428// or 0 if neither can be done directly. Indicate in Invert whether the
Ulrich Weigandcd808232015-05-05 19:26:48 +00002429// result is for the inverse of CC. IsFP is true if CC is for a
2430// floating-point rather than integer comparison.
2431static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, bool IsFP,
2432 bool &Invert) {
2433 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002434 Invert = false;
2435 return Opcode;
2436 }
2437
Ulrich Weigandcd808232015-05-05 19:26:48 +00002438 CC = ISD::getSetCCInverse(CC, !IsFP);
2439 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002440 Invert = true;
2441 return Opcode;
2442 }
2443
2444 return 0;
2445}
2446
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002447// Return a v2f64 that contains the extended form of elements Start and Start+1
2448// of v4f32 value Op.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002449static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, const SDLoc &DL,
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002450 SDValue Op) {
2451 int Mask[] = { Start, -1, Start + 1, -1 };
2452 Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask);
2453 return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op);
2454}
2455
2456// Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode,
2457// producing a result of type VT.
Ulrich Weigand33435c42017-07-17 17:42:48 +00002458SDValue SystemZTargetLowering::getVectorCmp(SelectionDAG &DAG, unsigned Opcode,
2459 const SDLoc &DL, EVT VT,
2460 SDValue CmpOp0,
2461 SDValue CmpOp1) const {
2462 // There is no hardware support for v4f32 (unless we have the vector
2463 // enhancements facility 1), so extend the vector into two v2f64s
2464 // and compare those.
2465 if (CmpOp0.getValueType() == MVT::v4f32 &&
2466 !Subtarget.hasVectorEnhancements1()) {
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002467 SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0);
2468 SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0);
2469 SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1);
2470 SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1);
2471 SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1);
2472 SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1);
2473 return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes);
2474 }
2475 return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1);
2476}
2477
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002478// Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing
2479// an integer mask of type VT.
Ulrich Weigand33435c42017-07-17 17:42:48 +00002480SDValue SystemZTargetLowering::lowerVectorSETCC(SelectionDAG &DAG,
2481 const SDLoc &DL, EVT VT,
2482 ISD::CondCode CC,
2483 SDValue CmpOp0,
2484 SDValue CmpOp1) const {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002485 bool IsFP = CmpOp0.getValueType().isFloatingPoint();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002486 bool Invert = false;
2487 SDValue Cmp;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002488 switch (CC) {
2489 // Handle tests for order using (or (ogt y x) (oge x y)).
2490 case ISD::SETUO:
2491 Invert = true;
Simon Pilgrim8c4069e2017-07-07 10:07:09 +00002492 LLVM_FALLTHROUGH;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002493 case ISD::SETO: {
2494 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002495 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2496 SDValue GE = getVectorCmp(DAG, SystemZISD::VFCMPHE, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002497 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE);
2498 break;
2499 }
2500
2501 // Handle <> tests using (or (ogt y x) (ogt x y)).
2502 case ISD::SETUEQ:
2503 Invert = true;
Simon Pilgrim8c4069e2017-07-07 10:07:09 +00002504 LLVM_FALLTHROUGH;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002505 case ISD::SETONE: {
2506 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002507 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2508 SDValue GT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002509 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT);
2510 break;
2511 }
2512
2513 // Otherwise a single comparison is enough. It doesn't really
2514 // matter whether we try the inversion or the swap first, since
2515 // there are no cases where both work.
2516 default:
2517 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002518 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002519 else {
2520 CC = ISD::getSetCCSwappedOperands(CC);
2521 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002522 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002523 else
2524 llvm_unreachable("Unhandled comparison");
2525 }
2526 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002527 }
2528 if (Invert) {
2529 SDValue Mask = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2530 DAG.getConstant(65535, DL, MVT::i32));
2531 Mask = DAG.getNode(ISD::BITCAST, DL, VT, Mask);
2532 Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask);
2533 }
2534 return Cmp;
2535}
2536
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002537SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
2538 SelectionDAG &DAG) const {
2539 SDValue CmpOp0 = Op.getOperand(0);
2540 SDValue CmpOp1 = Op.getOperand(1);
2541 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
2542 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002543 EVT VT = Op.getValueType();
2544 if (VT.isVector())
2545 return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002546
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002547 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002548 SDValue Glue = emitCmp(DAG, DL, C);
2549 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002550}
2551
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002552SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002553 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2554 SDValue CmpOp0 = Op.getOperand(2);
2555 SDValue CmpOp1 = Op.getOperand(3);
2556 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002557 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002558
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002559 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002560 SDValue Glue = emitCmp(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002561 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002562 Op.getOperand(0), DAG.getConstant(C.CCValid, DL, MVT::i32),
2563 DAG.getConstant(C.CCMask, DL, MVT::i32), Dest, Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002564}
2565
Richard Sandiford57485472013-12-13 15:35:00 +00002566// Return true if Pos is CmpOp and Neg is the negative of CmpOp,
2567// allowing Pos and Neg to be wider than CmpOp.
2568static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
2569 return (Neg.getOpcode() == ISD::SUB &&
2570 Neg.getOperand(0).getOpcode() == ISD::Constant &&
2571 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
2572 Neg.getOperand(1) == Pos &&
2573 (Pos == CmpOp ||
2574 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
2575 Pos.getOperand(0) == CmpOp)));
2576}
2577
2578// Return the absolute or negative absolute of Op; IsNegative decides which.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002579static SDValue getAbsolute(SelectionDAG &DAG, const SDLoc &DL, SDValue Op,
Richard Sandiford57485472013-12-13 15:35:00 +00002580 bool IsNegative) {
2581 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
2582 if (IsNegative)
2583 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002584 DAG.getConstant(0, DL, Op.getValueType()), Op);
Richard Sandiford57485472013-12-13 15:35:00 +00002585 return Op;
2586}
2587
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002588SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
2589 SelectionDAG &DAG) const {
2590 SDValue CmpOp0 = Op.getOperand(0);
2591 SDValue CmpOp1 = Op.getOperand(1);
2592 SDValue TrueOp = Op.getOperand(2);
2593 SDValue FalseOp = Op.getOperand(3);
2594 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002595 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002596
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002597 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandiford57485472013-12-13 15:35:00 +00002598
2599 // Check for absolute and negative-absolute selections, including those
2600 // where the comparison value is sign-extended (for LPGFR and LNGFR).
2601 // This check supplements the one in DAGCombiner.
2602 if (C.Opcode == SystemZISD::ICMP &&
2603 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
2604 C.CCMask != SystemZ::CCMASK_CMP_NE &&
2605 C.Op1.getOpcode() == ISD::Constant &&
2606 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
2607 if (isAbsolute(C.Op0, TrueOp, FalseOp))
2608 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
2609 if (isAbsolute(C.Op0, FalseOp, TrueOp))
2610 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
2611 }
2612
Richard Sandifordd420f732013-12-13 15:28:45 +00002613 SDValue Glue = emitCmp(DAG, DL, C);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002614
2615 // Special case for handling -1/0 results. The shifts we use here
2616 // should get optimized with the IPM conversion sequence.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002617 auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
2618 auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002619 if (TrueC && FalseC) {
2620 int64_t TrueVal = TrueC->getSExtValue();
2621 int64_t FalseVal = FalseC->getSExtValue();
2622 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
2623 // Invert the condition if we want -1 on false.
2624 if (TrueVal == 0)
Richard Sandifordd420f732013-12-13 15:28:45 +00002625 C.CCMask ^= C.CCValid;
2626 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002627 EVT VT = Op.getValueType();
2628 // Extend the result to VT. Upper bits are ignored.
2629 if (!is32Bit(VT))
2630 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
2631 // Sign-extend from the low bit.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002632 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002633 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
2634 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
2635 }
2636 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002637
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002638 SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, DL, MVT::i32),
2639 DAG.getConstant(C.CCMask, DL, MVT::i32), Glue};
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002640
2641 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00002642 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002643}
2644
2645SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
2646 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002647 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002648 const GlobalValue *GV = Node->getGlobal();
2649 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002650 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopher93bf97c2014-06-27 07:38:01 +00002651 CodeModel::Model CM = DAG.getTarget().getCodeModel();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002652
2653 SDValue Result;
Rafael Espindola3beef8d2016-06-27 23:15:57 +00002654 if (Subtarget.isPC32DBLSymbol(GV, CM)) {
Richard Sandiford54b36912013-09-27 15:14:04 +00002655 // Assign anchors at 1<<12 byte boundaries.
2656 uint64_t Anchor = Offset & ~uint64_t(0xfff);
2657 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
2658 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2659
2660 // The offset can be folded into the address if it is aligned to a halfword.
2661 Offset -= Anchor;
2662 if (Offset != 0 && (Offset & 1) == 0) {
2663 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
2664 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002665 Offset = 0;
2666 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002667 } else {
2668 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
2669 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2670 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Justin Lebar9c375812016-07-15 18:27:10 +00002671 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002672 }
2673
2674 // If there was a non-zero offset that we didn't fold, create an explicit
2675 // addition for it.
2676 if (Offset != 0)
2677 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002678 DAG.getConstant(Offset, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002679
2680 return Result;
2681}
2682
Ulrich Weigand7db69182015-02-18 09:13:27 +00002683SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
2684 SelectionDAG &DAG,
2685 unsigned Opcode,
2686 SDValue GOTOffset) const {
2687 SDLoc DL(Node);
Mehdi Amini44ede332015-07-09 02:09:04 +00002688 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand7db69182015-02-18 09:13:27 +00002689 SDValue Chain = DAG.getEntryNode();
2690 SDValue Glue;
2691
2692 // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
2693 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
2694 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
2695 Glue = Chain.getValue(1);
2696 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
2697 Glue = Chain.getValue(1);
2698
2699 // The first call operand is the chain and the second is the TLS symbol.
2700 SmallVector<SDValue, 8> Ops;
2701 Ops.push_back(Chain);
2702 Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
2703 Node->getValueType(0),
2704 0, 0));
2705
2706 // Add argument registers to the end of the list so that they are
2707 // known live into the call.
2708 Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
2709 Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
2710
2711 // Add a register mask operand representing the call-preserved registers.
2712 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002713 const uint32_t *Mask =
2714 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002715 assert(Mask && "Missing call preserved mask for calling convention");
2716 Ops.push_back(DAG.getRegisterMask(Mask));
2717
2718 // Glue the call to the argument copies.
2719 Ops.push_back(Glue);
2720
2721 // Emit the call.
2722 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2723 Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
2724 Glue = Chain.getValue(1);
2725
2726 // Copy the return value from %r2.
2727 return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
2728}
2729
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002730SDValue SystemZTargetLowering::lowerThreadPointer(const SDLoc &DL,
2731 SelectionDAG &DAG) const {
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002732 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002733 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002734
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002735 // The high part of the thread pointer is in access register 0.
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002736 SDValue TPHi = DAG.getCopyFromReg(Chain, DL, SystemZ::A0, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002737 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
2738
2739 // The low part of the thread pointer is in access register 1.
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002740 SDValue TPLo = DAG.getCopyFromReg(Chain, DL, SystemZ::A1, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002741 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
2742
2743 // Merge them into a single 64-bit address.
2744 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002745 DAG.getConstant(32, DL, PtrVT));
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002746 return DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
2747}
2748
2749SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
2750 SelectionDAG &DAG) const {
2751 if (DAG.getTarget().Options.EmulatedTLS)
2752 return LowerToTLSEmulatedModel(Node, DAG);
2753 SDLoc DL(Node);
2754 const GlobalValue *GV = Node->getGlobal();
2755 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2756 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
2757
2758 SDValue TP = lowerThreadPointer(DL, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002759
Ulrich Weigand7db69182015-02-18 09:13:27 +00002760 // Get the offset of GA from the thread pointer, based on the TLS model.
2761 SDValue Offset;
2762 switch (model) {
2763 case TLSModel::GeneralDynamic: {
2764 // Load the GOT offset of the tls_index (module ID / per-symbol offset).
2765 SystemZConstantPoolValue *CPV =
2766 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002767
Ulrich Weigand7db69182015-02-18 09:13:27 +00002768 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002769 Offset = DAG.getLoad(
2770 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002771 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002772
2773 // Call __tls_get_offset to retrieve the offset.
2774 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
2775 break;
2776 }
2777
2778 case TLSModel::LocalDynamic: {
2779 // Load the GOT offset of the module ID.
2780 SystemZConstantPoolValue *CPV =
2781 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
2782
2783 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002784 Offset = DAG.getLoad(
2785 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002786 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002787
2788 // Call __tls_get_offset to retrieve the module base offset.
2789 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
2790
2791 // Note: The SystemZLDCleanupPass will remove redundant computations
2792 // of the module base offset. Count total number of local-dynamic
2793 // accesses to trigger execution of that pass.
2794 SystemZMachineFunctionInfo* MFI =
2795 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
2796 MFI->incNumLocalDynamicTLSAccesses();
2797
2798 // Add the per-symbol offset.
2799 CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
2800
2801 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002802 DTPOffset = DAG.getLoad(
2803 PtrVT, DL, DAG.getEntryNode(), DTPOffset,
Justin Lebar9c375812016-07-15 18:27:10 +00002804 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002805
2806 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
2807 break;
2808 }
2809
2810 case TLSModel::InitialExec: {
2811 // Load the offset from the GOT.
2812 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2813 SystemZII::MO_INDNTPOFF);
2814 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +00002815 Offset =
2816 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Offset,
2817 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002818 break;
2819 }
2820
2821 case TLSModel::LocalExec: {
2822 // Force the offset into the constant pool and load it from there.
2823 SystemZConstantPoolValue *CPV =
2824 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
2825
2826 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002827 Offset = DAG.getLoad(
2828 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002829 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002830 break;
Ulrich Weigandb7e59092015-02-18 09:42:23 +00002831 }
Ulrich Weigand7db69182015-02-18 09:13:27 +00002832 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002833
2834 // Add the base and offset together.
2835 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
2836}
2837
2838SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
2839 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002840 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002841 const BlockAddress *BA = Node->getBlockAddress();
2842 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002843 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002844
2845 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
2846 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2847 return Result;
2848}
2849
2850SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
2851 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002852 SDLoc DL(JT);
Mehdi Amini44ede332015-07-09 02:09:04 +00002853 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002854 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
2855
2856 // Use LARL to load the address of the table.
2857 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2858}
2859
2860SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
2861 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002862 SDLoc DL(CP);
Mehdi Amini44ede332015-07-09 02:09:04 +00002863 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002864
2865 SDValue Result;
2866 if (CP->isMachineConstantPoolEntry())
2867 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002868 CP->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002869 else
2870 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002871 CP->getAlignment(), CP->getOffset());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002872
2873 // Use LARL to load the address of the constant pool entry.
2874 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2875}
2876
Ulrich Weigandf557d082016-04-04 12:44:55 +00002877SDValue SystemZTargetLowering::lowerFRAMEADDR(SDValue Op,
2878 SelectionDAG &DAG) const {
2879 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002880 MachineFrameInfo &MFI = MF.getFrameInfo();
2881 MFI.setFrameAddressIsTaken(true);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002882
2883 SDLoc DL(Op);
2884 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2885 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2886
2887 // If the back chain frame index has not been allocated yet, do so.
2888 SystemZMachineFunctionInfo *FI = MF.getInfo<SystemZMachineFunctionInfo>();
2889 int BackChainIdx = FI->getFramePointerSaveIndex();
2890 if (!BackChainIdx) {
2891 // By definition, the frame address is the address of the back chain.
Matthias Braun941a7052016-07-28 18:40:00 +00002892 BackChainIdx = MFI.CreateFixedObject(8, -SystemZMC::CallFrameSize, false);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002893 FI->setFramePointerSaveIndex(BackChainIdx);
2894 }
2895 SDValue BackChain = DAG.getFrameIndex(BackChainIdx, PtrVT);
2896
2897 // FIXME The frontend should detect this case.
2898 if (Depth > 0) {
2899 report_fatal_error("Unsupported stack frame traversal count");
2900 }
2901
2902 return BackChain;
2903}
2904
2905SDValue SystemZTargetLowering::lowerRETURNADDR(SDValue Op,
2906 SelectionDAG &DAG) const {
2907 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002908 MachineFrameInfo &MFI = MF.getFrameInfo();
2909 MFI.setReturnAddressIsTaken(true);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002910
2911 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
2912 return SDValue();
2913
2914 SDLoc DL(Op);
2915 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2916 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2917
2918 // FIXME The frontend should detect this case.
2919 if (Depth > 0) {
2920 report_fatal_error("Unsupported stack frame traversal count");
2921 }
2922
2923 // Return R14D, which has the return address. Mark it an implicit live-in.
2924 unsigned LinkReg = MF.addLiveIn(SystemZ::R14D, &SystemZ::GR64BitRegClass);
2925 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, LinkReg, PtrVT);
2926}
2927
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002928SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
2929 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002930 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002931 SDValue In = Op.getOperand(0);
2932 EVT InVT = In.getValueType();
2933 EVT ResVT = Op.getValueType();
2934
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002935 // Convert loads directly. This is normally done by DAGCombiner,
2936 // but we need this case for bitcasts that are created during lowering
2937 // and which are then lowered themselves.
2938 if (auto *LoadN = dyn_cast<LoadSDNode>(In))
Nirav Daveaa65a2b2017-04-05 15:42:48 +00002939 if (ISD::isNormalLoad(LoadN))
2940 return DAG.getLoad(ResVT, DL, LoadN->getChain(), LoadN->getBasePtr(),
2941 LoadN->getMemOperand());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002942
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002943 if (InVT == MVT::i32 && ResVT == MVT::f32) {
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002944 SDValue In64;
2945 if (Subtarget.hasHighWord()) {
2946 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
2947 MVT::i64);
2948 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
2949 MVT::i64, SDValue(U64, 0), In);
2950 } else {
2951 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
2952 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002953 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002954 }
2955 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002956 return DAG.getTargetExtractSubreg(SystemZ::subreg_r32,
Richard Sandifordd8163202013-09-13 09:12:44 +00002957 DL, MVT::f32, Out64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002958 }
2959 if (InVT == MVT::f32 && ResVT == MVT::i32) {
2960 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002961 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002962 MVT::f64, SDValue(U64, 0), In);
2963 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002964 if (Subtarget.hasHighWord())
2965 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
2966 MVT::i32, Out64);
2967 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002968 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002969 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002970 }
2971 llvm_unreachable("Unexpected bitcast combination");
2972}
2973
2974SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
2975 SelectionDAG &DAG) const {
2976 MachineFunction &MF = DAG.getMachineFunction();
2977 SystemZMachineFunctionInfo *FuncInfo =
2978 MF.getInfo<SystemZMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002979 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002980
2981 SDValue Chain = Op.getOperand(0);
2982 SDValue Addr = Op.getOperand(1);
2983 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002984 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002985
2986 // The initial values of each field.
2987 const unsigned NumFields = 4;
2988 SDValue Fields[NumFields] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002989 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT),
2990 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002991 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
2992 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
2993 };
2994
2995 // Store each field into its respective slot.
2996 SDValue MemOps[NumFields];
2997 unsigned Offset = 0;
2998 for (unsigned I = 0; I < NumFields; ++I) {
2999 SDValue FieldAddr = Addr;
3000 if (Offset != 0)
3001 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003002 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003003 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00003004 MachinePointerInfo(SV, Offset));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003005 Offset += 8;
3006 }
Craig Topper48d114b2014-04-26 18:35:24 +00003007 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003008}
3009
3010SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
3011 SelectionDAG &DAG) const {
3012 SDValue Chain = Op.getOperand(0);
3013 SDValue DstPtr = Op.getOperand(1);
3014 SDValue SrcPtr = Op.getOperand(2);
3015 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
3016 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003017 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003018
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003019 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003020 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00003021 /*isTailCall*/false,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003022 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
3023}
3024
3025SDValue SystemZTargetLowering::
3026lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003027 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003028 MachineFunction &MF = DAG.getMachineFunction();
3029 bool RealignOpt = !MF.getFunction()-> hasFnAttribute("no-realign-stack");
3030 bool StoreBackchain = MF.getFunction()->hasFnAttribute("backchain");
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003031
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003032 SDValue Chain = Op.getOperand(0);
3033 SDValue Size = Op.getOperand(1);
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003034 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003035 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003036
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003037 // If user has set the no alignment function attribute, ignore
3038 // alloca alignments.
3039 uint64_t AlignVal = (RealignOpt ?
3040 dyn_cast<ConstantSDNode>(Align)->getZExtValue() : 0);
3041
3042 uint64_t StackAlign = TFI->getStackAlignment();
3043 uint64_t RequiredAlign = std::max(AlignVal, StackAlign);
3044 uint64_t ExtraAlignSpace = RequiredAlign - StackAlign;
3045
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003046 unsigned SPReg = getStackPointerRegisterToSaveRestore();
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003047 SDValue NeededSpace = Size;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003048
3049 // Get a reference to the stack pointer.
3050 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
3051
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003052 // If we need a backchain, save it now.
3053 SDValue Backchain;
3054 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003055 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003056
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003057 // Add extra space for alignment if needed.
3058 if (ExtraAlignSpace)
3059 NeededSpace = DAG.getNode(ISD::ADD, DL, MVT::i64, NeededSpace,
Elliot Colpbc2cfc22016-07-06 18:13:11 +00003060 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003061
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003062 // Get the new stack pointer value.
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003063 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, NeededSpace);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003064
3065 // Copy the new stack pointer back.
3066 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
3067
3068 // The allocated data lives above the 160 bytes allocated for the standard
3069 // frame, plus any outgoing stack arguments. We don't know how much that
3070 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
3071 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
3072 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
3073
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003074 // Dynamically realign if needed.
3075 if (RequiredAlign > StackAlign) {
3076 Result =
3077 DAG.getNode(ISD::ADD, DL, MVT::i64, Result,
3078 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
3079 Result =
3080 DAG.getNode(ISD::AND, DL, MVT::i64, Result,
3081 DAG.getConstant(~(RequiredAlign - 1), DL, MVT::i64));
3082 }
3083
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003084 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003085 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003086
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003087 SDValue Ops[2] = { Result, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00003088 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003089}
3090
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +00003091SDValue SystemZTargetLowering::lowerGET_DYNAMIC_AREA_OFFSET(
3092 SDValue Op, SelectionDAG &DAG) const {
3093 SDLoc DL(Op);
3094
3095 return DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
3096}
3097
Richard Sandiford7d86e472013-08-21 09:34:56 +00003098SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
3099 SelectionDAG &DAG) const {
3100 EVT VT = Op.getValueType();
3101 SDLoc DL(Op);
3102 SDValue Ops[2];
3103 if (is32Bit(VT))
3104 // Just do a normal 64-bit multiplication and extract the results.
3105 // We define this so that it can be used for constant division.
3106 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
3107 Op.getOperand(1), Ops[1], Ops[0]);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +00003108 else if (Subtarget.hasMiscellaneousExtensions2())
3109 // SystemZISD::SMUL_LOHI returns the low result in the odd register and
3110 // the high result in the even register. ISD::SMUL_LOHI is defined to
3111 // return the low half first, so the results are in reverse order.
3112 lowerGR128Binary(DAG, DL, VT, SystemZISD::SMUL_LOHI,
3113 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003114 else {
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003115 // Do a full 128-bit multiplication based on SystemZISD::UMUL_LOHI:
Richard Sandiford7d86e472013-08-21 09:34:56 +00003116 //
3117 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
3118 //
3119 // but using the fact that the upper halves are either all zeros
3120 // or all ones:
3121 //
3122 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
3123 //
3124 // and grouping the right terms together since they are quicker than the
3125 // multiplication:
3126 //
3127 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003128 SDValue C63 = DAG.getConstant(63, DL, MVT::i64);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003129 SDValue LL = Op.getOperand(0);
3130 SDValue RL = Op.getOperand(1);
3131 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
3132 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003133 // SystemZISD::UMUL_LOHI returns the low result in the odd register and
3134 // the high result in the even register. ISD::SMUL_LOHI is defined to
3135 // return the low half first, so the results are in reverse order.
3136 lowerGR128Binary(DAG, DL, VT, SystemZISD::UMUL_LOHI,
Richard Sandiford7d86e472013-08-21 09:34:56 +00003137 LL, RL, Ops[1], Ops[0]);
3138 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
3139 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
3140 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
3141 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
3142 }
Craig Topper64941d92014-04-27 19:20:57 +00003143 return DAG.getMergeValues(Ops, DL);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003144}
3145
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003146SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
3147 SelectionDAG &DAG) const {
3148 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003149 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003150 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00003151 if (is32Bit(VT))
3152 // Just do a normal 64-bit multiplication and extract the results.
3153 // We define this so that it can be used for constant division.
3154 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
3155 Op.getOperand(1), Ops[1], Ops[0]);
3156 else
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003157 // SystemZISD::UMUL_LOHI returns the low result in the odd register and
3158 // the high result in the even register. ISD::UMUL_LOHI is defined to
3159 // return the low half first, so the results are in reverse order.
3160 lowerGR128Binary(DAG, DL, VT, SystemZISD::UMUL_LOHI,
Richard Sandiford7d86e472013-08-21 09:34:56 +00003161 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003162 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003163}
3164
3165SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
3166 SelectionDAG &DAG) const {
3167 SDValue Op0 = Op.getOperand(0);
3168 SDValue Op1 = Op.getOperand(1);
3169 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003170 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003171
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003172 // We use DSGF for 32-bit division. This means the first operand must
3173 // always be 64-bit, and the second operand should be 32-bit whenever
3174 // that is possible, to improve performance.
3175 if (is32Bit(VT))
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003176 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003177 else if (DAG.ComputeNumSignBits(Op1) > 32)
Richard Sandiforde6e78852013-07-02 15:40:22 +00003178 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003179
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003180 // DSG(F) returns the remainder in the even register and the
3181 // quotient in the odd register.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003182 SDValue Ops[2];
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003183 lowerGR128Binary(DAG, DL, VT, SystemZISD::SDIVREM, Op0, Op1, Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003184 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003185}
3186
3187SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
3188 SelectionDAG &DAG) const {
3189 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003190 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003191
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003192 // DL(G) returns the remainder in the even register and the
3193 // quotient in the odd register.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003194 SDValue Ops[2];
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003195 lowerGR128Binary(DAG, DL, VT, SystemZISD::UDIVREM,
3196 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003197 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003198}
3199
3200SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
3201 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
3202
3203 // Get the known-zero masks for each operand.
3204 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
Craig Topperd0af7e82017-04-28 05:31:46 +00003205 KnownBits Known[2];
3206 DAG.computeKnownBits(Ops[0], Known[0]);
3207 DAG.computeKnownBits(Ops[1], Known[1]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003208
3209 // See if the upper 32 bits of one operand and the lower 32 bits of the
3210 // other are known zero. They are the low and high operands respectively.
Craig Topperd0af7e82017-04-28 05:31:46 +00003211 uint64_t Masks[] = { Known[0].Zero.getZExtValue(),
3212 Known[1].Zero.getZExtValue() };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003213 unsigned High, Low;
3214 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
3215 High = 1, Low = 0;
3216 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
3217 High = 0, Low = 1;
3218 else
3219 return Op;
3220
3221 SDValue LowOp = Ops[Low];
3222 SDValue HighOp = Ops[High];
3223
3224 // If the high part is a constant, we're better off using IILH.
3225 if (HighOp.getOpcode() == ISD::Constant)
3226 return Op;
3227
3228 // If the low part is a constant that is outside the range of LHI,
3229 // then we're better off using IILF.
3230 if (LowOp.getOpcode() == ISD::Constant) {
3231 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
3232 if (!isInt<16>(Value))
3233 return Op;
3234 }
3235
3236 // Check whether the high part is an AND that doesn't change the
3237 // high 32 bits and just masks out low bits. We can skip it if so.
3238 if (HighOp.getOpcode() == ISD::AND &&
3239 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
Richard Sandifordccc2a7c2013-12-03 11:01:54 +00003240 SDValue HighOp0 = HighOp.getOperand(0);
3241 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
3242 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
3243 HighOp = HighOp0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003244 }
3245
3246 // Take advantage of the fact that all GR32 operations only change the
3247 // low 32 bits by truncating Low to an i32 and inserting it directly
3248 // using a subreg. The interesting cases are those where the truncation
3249 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003250 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003251 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
Richard Sandiford87a44362013-09-30 10:28:35 +00003252 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00003253 MVT::i64, HighOp, Low32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003254}
3255
Ulrich Weigandb4012182015-03-31 12:56:33 +00003256SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
3257 SelectionDAG &DAG) const {
3258 EVT VT = Op.getValueType();
Ulrich Weigandb4012182015-03-31 12:56:33 +00003259 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003260 Op = Op.getOperand(0);
3261
3262 // Handle vector types via VPOPCT.
3263 if (VT.isVector()) {
3264 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op);
3265 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00003266 switch (VT.getScalarSizeInBits()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003267 case 8:
3268 break;
3269 case 16: {
3270 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
3271 SDValue Shift = DAG.getConstant(8, DL, MVT::i32);
3272 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift);
3273 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3274 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift);
3275 break;
3276 }
3277 case 32: {
3278 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3279 DAG.getConstant(0, DL, MVT::i32));
3280 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3281 break;
3282 }
3283 case 64: {
3284 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3285 DAG.getConstant(0, DL, MVT::i32));
3286 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp);
3287 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3288 break;
3289 }
3290 default:
3291 llvm_unreachable("Unexpected type");
3292 }
3293 return Op;
3294 }
Ulrich Weigandb4012182015-03-31 12:56:33 +00003295
3296 // Get the known-zero mask for the operand.
Craig Topperd0af7e82017-04-28 05:31:46 +00003297 KnownBits Known;
3298 DAG.computeKnownBits(Op, Known);
3299 unsigned NumSignificantBits = (~Known.Zero).getActiveBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003300 if (NumSignificantBits == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003301 return DAG.getConstant(0, DL, VT);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003302
3303 // Skip known-zero high parts of the operand.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003304 int64_t OrigBitSize = VT.getSizeInBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003305 int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits);
3306 BitSize = std::min(BitSize, OrigBitSize);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003307
3308 // The POPCNT instruction counts the number of bits in each byte.
3309 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op);
3310 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op);
3311 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
3312
3313 // Add up per-byte counts in a binary tree. All bits of Op at
3314 // position larger than BitSize remain zero throughout.
3315 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003316 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003317 if (BitSize != OrigBitSize)
3318 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003319 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003320 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3321 }
3322
3323 // Extract overall result from high byte.
3324 if (BitSize > 8)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003325 Op = DAG.getNode(ISD::SRL, DL, VT, Op,
3326 DAG.getConstant(BitSize - 8, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003327
3328 return Op;
3329}
3330
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003331SDValue SystemZTargetLowering::lowerATOMIC_FENCE(SDValue Op,
3332 SelectionDAG &DAG) const {
3333 SDLoc DL(Op);
3334 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
3335 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003336 SyncScope::ID FenceSSID = static_cast<SyncScope::ID>(
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003337 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
3338
3339 // The only fence that needs an instruction is a sequentially-consistent
3340 // cross-thread fence.
JF Bastien800f87a2016-04-06 21:19:33 +00003341 if (FenceOrdering == AtomicOrdering::SequentiallyConsistent &&
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003342 FenceSSID == SyncScope::System) {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003343 return SDValue(DAG.getMachineNode(SystemZ::Serialize, DL, MVT::Other,
JF Bastien800f87a2016-04-06 21:19:33 +00003344 Op.getOperand(0)),
3345 0);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003346 }
3347
3348 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
3349 return DAG.getNode(SystemZISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0));
3350}
3351
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003352// Op is an atomic load. Lower it into a normal volatile load.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003353SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
3354 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003355 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003356 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003357 Node->getChain(), Node->getBasePtr(),
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003358 Node->getMemoryVT(), Node->getMemOperand());
3359}
3360
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003361// Op is an atomic store. Lower it into a normal volatile store.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003362SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
3363 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003364 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003365 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
3366 Node->getBasePtr(), Node->getMemoryVT(),
3367 Node->getMemOperand());
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003368 // We have to enforce sequential consistency by performing a
3369 // serialization operation after the store.
3370 if (Node->getOrdering() == AtomicOrdering::SequentiallyConsistent)
3371 Chain = SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op),
3372 MVT::Other, Chain), 0);
3373 return Chain;
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003374}
3375
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003376// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
3377// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003378SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
3379 SelectionDAG &DAG,
3380 unsigned Opcode) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003381 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003382
3383 // 32-bit operations need no code outside the main loop.
3384 EVT NarrowVT = Node->getMemoryVT();
3385 EVT WideVT = MVT::i32;
3386 if (NarrowVT == WideVT)
3387 return Op;
3388
3389 int64_t BitSize = NarrowVT.getSizeInBits();
3390 SDValue ChainIn = Node->getChain();
3391 SDValue Addr = Node->getBasePtr();
3392 SDValue Src2 = Node->getVal();
3393 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003394 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003395 EVT PtrVT = Addr.getValueType();
3396
3397 // Convert atomic subtracts of constants into additions.
3398 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
Richard Sandiford21f5d682014-03-06 11:22:58 +00003399 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003400 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003401 Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003402 }
3403
3404 // Get the address of the containing word.
3405 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003406 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003407
3408 // Get the number of bits that the word must be rotated left in order
3409 // to bring the field to the top bits of a GR32.
3410 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003411 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003412 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3413
3414 // Get the complementing shift amount, for rotating a field in the top
3415 // bits back to its proper position.
3416 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003417 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003418
3419 // Extend the source operand to 32 bits and prepare it for the inner loop.
3420 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
3421 // operations require the source to be shifted in advance. (This shift
3422 // can be folded if the source is constant.) For AND and NAND, the lower
3423 // bits must be set, while for other opcodes they should be left clear.
3424 if (Opcode != SystemZISD::ATOMIC_SWAPW)
3425 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003426 DAG.getConstant(32 - BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003427 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
3428 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
3429 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003430 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003431
3432 // Construct the ATOMIC_LOADW_* node.
3433 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3434 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003435 DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003436 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003437 NarrowVT, MMO);
3438
3439 // Rotate the result of the final CS so that the field is in the lower
3440 // bits of a GR32, then truncate it.
3441 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003442 DAG.getConstant(BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003443 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
3444
3445 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00003446 return DAG.getMergeValues(RetOps, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003447}
3448
Richard Sandiford41350a52013-12-24 15:18:04 +00003449// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
Richard Sandiford002019a2013-12-24 15:22:39 +00003450// into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
Richard Sandiford41350a52013-12-24 15:18:04 +00003451// operations into additions.
3452SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
3453 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003454 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandiford41350a52013-12-24 15:18:04 +00003455 EVT MemVT = Node->getMemoryVT();
3456 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
3457 // A full-width operation.
3458 assert(Op.getValueType() == MemVT && "Mismatched VTs");
3459 SDValue Src2 = Node->getVal();
3460 SDValue NegSrc2;
3461 SDLoc DL(Src2);
3462
Richard Sandiford21f5d682014-03-06 11:22:58 +00003463 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
Richard Sandiford41350a52013-12-24 15:18:04 +00003464 // Use an addition if the operand is constant and either LAA(G) is
3465 // available or the negative value is in the range of A(G)FHI.
3466 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
Eric Christopher93bf97c2014-06-27 07:38:01 +00003467 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003468 NegSrc2 = DAG.getConstant(Value, DL, MemVT);
Eric Christopher93bf97c2014-06-27 07:38:01 +00003469 } else if (Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00003470 // Use LAA(G) if available.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003471 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT),
Richard Sandiford41350a52013-12-24 15:18:04 +00003472 Src2);
3473
3474 if (NegSrc2.getNode())
3475 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
3476 Node->getChain(), Node->getBasePtr(), NegSrc2,
Konstantin Zhuravlyov8ea02462016-10-15 22:01:18 +00003477 Node->getMemOperand());
Richard Sandiford41350a52013-12-24 15:18:04 +00003478
3479 // Use the node as-is.
3480 return Op;
3481 }
3482
3483 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
3484}
3485
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003486// Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
3487// into a fullword ATOMIC_CMP_SWAPW operation.
3488SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
3489 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003490 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003491
3492 // We have native support for 32-bit compare and swap.
3493 EVT NarrowVT = Node->getMemoryVT();
3494 EVT WideVT = MVT::i32;
3495 if (NarrowVT == WideVT)
3496 return Op;
3497
3498 int64_t BitSize = NarrowVT.getSizeInBits();
3499 SDValue ChainIn = Node->getOperand(0);
3500 SDValue Addr = Node->getOperand(1);
3501 SDValue CmpVal = Node->getOperand(2);
3502 SDValue SwapVal = Node->getOperand(3);
3503 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003504 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003505 EVT PtrVT = Addr.getValueType();
3506
3507 // Get the address of the containing word.
3508 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003509 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003510
3511 // Get the number of bits that the word must be rotated left in order
3512 // to bring the field to the top bits of a GR32.
3513 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003514 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003515 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3516
3517 // Get the complementing shift amount, for rotating a field in the top
3518 // bits back to its proper position.
3519 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003520 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003521
3522 // Construct the ATOMIC_CMP_SWAPW node.
3523 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3524 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003525 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003526 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003527 VTList, Ops, NarrowVT, MMO);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003528 return AtomicOp;
3529}
3530
3531SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
3532 SelectionDAG &DAG) const {
3533 MachineFunction &MF = DAG.getMachineFunction();
3534 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003535 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003536 SystemZ::R15D, Op.getValueType());
3537}
3538
3539SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
3540 SelectionDAG &DAG) const {
3541 MachineFunction &MF = DAG.getMachineFunction();
3542 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003543 bool StoreBackchain = MF.getFunction()->hasFnAttribute("backchain");
3544
3545 SDValue Chain = Op.getOperand(0);
3546 SDValue NewSP = Op.getOperand(1);
3547 SDValue Backchain;
3548 SDLoc DL(Op);
3549
3550 if (StoreBackchain) {
3551 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, MVT::i64);
Justin Lebar9c375812016-07-15 18:27:10 +00003552 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003553 }
3554
3555 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R15D, NewSP);
3556
3557 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003558 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003559
3560 return Chain;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003561}
3562
Richard Sandiford03481332013-08-23 11:36:42 +00003563SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
3564 SelectionDAG &DAG) const {
3565 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3566 if (!IsData)
3567 // Just preserve the chain.
3568 return Op.getOperand(0);
3569
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003570 SDLoc DL(Op);
Richard Sandiford03481332013-08-23 11:36:42 +00003571 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
3572 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
Richard Sandiford21f5d682014-03-06 11:22:58 +00003573 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
Richard Sandiford03481332013-08-23 11:36:42 +00003574 SDValue Ops[] = {
3575 Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003576 DAG.getConstant(Code, DL, MVT::i32),
Richard Sandiford03481332013-08-23 11:36:42 +00003577 Op.getOperand(1)
3578 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003579 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003580 Node->getVTList(), Ops,
Richard Sandiford03481332013-08-23 11:36:42 +00003581 Node->getMemoryVT(), Node->getMemOperand());
3582}
3583
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003584// Return an i32 that contains the value of CC immediately after After,
3585// whose final operand must be MVT::Glue.
3586static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003587 SDLoc DL(After);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003588 SDValue Glue = SDValue(After, After->getNumValues() - 1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003589 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
3590 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
3591 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003592}
3593
3594SDValue
3595SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
3596 SelectionDAG &DAG) const {
3597 unsigned Opcode, CCValid;
3598 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) {
3599 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
3600 SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode);
3601 SDValue CC = getCCResult(DAG, Glued.getNode());
3602 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC);
3603 return SDValue();
3604 }
3605
3606 return SDValue();
3607}
3608
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003609SDValue
3610SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
3611 SelectionDAG &DAG) const {
3612 unsigned Opcode, CCValid;
3613 if (isIntrinsicWithCC(Op, Opcode, CCValid)) {
3614 SDValue Glued = emitIntrinsicWithGlue(DAG, Op, Opcode);
3615 SDValue CC = getCCResult(DAG, Glued.getNode());
3616 if (Op->getNumValues() == 1)
3617 return CC;
3618 assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result");
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00003619 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), Glued,
3620 CC);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003621 }
3622
3623 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3624 switch (Id) {
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00003625 case Intrinsic::thread_pointer:
3626 return lowerThreadPointer(SDLoc(Op), DAG);
3627
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003628 case Intrinsic::s390_vpdi:
3629 return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(),
3630 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3631
3632 case Intrinsic::s390_vperm:
3633 return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(),
3634 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3635
3636 case Intrinsic::s390_vuphb:
3637 case Intrinsic::s390_vuphh:
3638 case Intrinsic::s390_vuphf:
3639 return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(),
3640 Op.getOperand(1));
3641
3642 case Intrinsic::s390_vuplhb:
3643 case Intrinsic::s390_vuplhh:
3644 case Intrinsic::s390_vuplhf:
3645 return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(),
3646 Op.getOperand(1));
3647
3648 case Intrinsic::s390_vuplb:
3649 case Intrinsic::s390_vuplhw:
3650 case Intrinsic::s390_vuplf:
3651 return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(),
3652 Op.getOperand(1));
3653
3654 case Intrinsic::s390_vupllb:
3655 case Intrinsic::s390_vupllh:
3656 case Intrinsic::s390_vupllf:
3657 return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(),
3658 Op.getOperand(1));
3659
3660 case Intrinsic::s390_vsumb:
3661 case Intrinsic::s390_vsumh:
3662 case Intrinsic::s390_vsumgh:
3663 case Intrinsic::s390_vsumgf:
3664 case Intrinsic::s390_vsumqf:
3665 case Intrinsic::s390_vsumqg:
3666 return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(),
3667 Op.getOperand(1), Op.getOperand(2));
3668 }
3669
3670 return SDValue();
3671}
3672
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003673namespace {
3674// Says that SystemZISD operation Opcode can be used to perform the equivalent
3675// of a VPERM with permute vector Bytes. If Opcode takes three operands,
3676// Operand is the constant third operand, otherwise it is the number of
3677// bytes in each element of the result.
3678struct Permute {
3679 unsigned Opcode;
3680 unsigned Operand;
3681 unsigned char Bytes[SystemZ::VectorBytes];
3682};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003683}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003684
3685static const Permute PermuteForms[] = {
3686 // VMRHG
3687 { SystemZISD::MERGE_HIGH, 8,
3688 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } },
3689 // VMRHF
3690 { SystemZISD::MERGE_HIGH, 4,
3691 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
3692 // VMRHH
3693 { SystemZISD::MERGE_HIGH, 2,
3694 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
3695 // VMRHB
3696 { SystemZISD::MERGE_HIGH, 1,
3697 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
3698 // VMRLG
3699 { SystemZISD::MERGE_LOW, 8,
3700 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } },
3701 // VMRLF
3702 { SystemZISD::MERGE_LOW, 4,
3703 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
3704 // VMRLH
3705 { SystemZISD::MERGE_LOW, 2,
3706 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
3707 // VMRLB
3708 { SystemZISD::MERGE_LOW, 1,
3709 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
3710 // VPKG
3711 { SystemZISD::PACK, 4,
3712 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } },
3713 // VPKF
3714 { SystemZISD::PACK, 2,
3715 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
3716 // VPKH
3717 { SystemZISD::PACK, 1,
3718 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
3719 // VPDI V1, V2, 4 (low half of V1, high half of V2)
3720 { SystemZISD::PERMUTE_DWORDS, 4,
3721 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } },
3722 // VPDI V1, V2, 1 (high half of V1, low half of V2)
3723 { SystemZISD::PERMUTE_DWORDS, 1,
3724 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } }
3725};
3726
3727// Called after matching a vector shuffle against a particular pattern.
3728// Both the original shuffle and the pattern have two vector operands.
3729// OpNos[0] is the operand of the original shuffle that should be used for
3730// operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything.
3731// OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and
3732// set OpNo0 and OpNo1 to the shuffle operands that should actually be used
3733// for operands 0 and 1 of the pattern.
3734static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) {
3735 if (OpNos[0] < 0) {
3736 if (OpNos[1] < 0)
3737 return false;
3738 OpNo0 = OpNo1 = OpNos[1];
3739 } else if (OpNos[1] < 0) {
3740 OpNo0 = OpNo1 = OpNos[0];
3741 } else {
3742 OpNo0 = OpNos[0];
3743 OpNo1 = OpNos[1];
3744 }
3745 return true;
3746}
3747
3748// Bytes is a VPERM-like permute vector, except that -1 is used for
3749// undefined bytes. Return true if the VPERM can be implemented using P.
3750// When returning true set OpNo0 to the VPERM operand that should be
3751// used for operand 0 of P and likewise OpNo1 for operand 1 of P.
3752//
3753// For example, if swapping the VPERM operands allows P to match, OpNo0
3754// will be 1 and OpNo1 will be 0. If instead Bytes only refers to one
3755// operand, but rewriting it to use two duplicated operands allows it to
3756// match P, then OpNo0 and OpNo1 will be the same.
3757static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P,
3758 unsigned &OpNo0, unsigned &OpNo1) {
3759 int OpNos[] = { -1, -1 };
3760 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
3761 int Elt = Bytes[I];
3762 if (Elt >= 0) {
3763 // Make sure that the two permute vectors use the same suboperand
3764 // byte number. Only the operand numbers (the high bits) are
3765 // allowed to differ.
3766 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1))
3767 return false;
3768 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes;
3769 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes;
3770 // Make sure that the operand mappings are consistent with previous
3771 // elements.
3772 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3773 return false;
3774 OpNos[ModelOpNo] = RealOpNo;
3775 }
3776 }
3777 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3778}
3779
3780// As above, but search for a matching permute.
3781static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes,
3782 unsigned &OpNo0, unsigned &OpNo1) {
3783 for (auto &P : PermuteForms)
3784 if (matchPermute(Bytes, P, OpNo0, OpNo1))
3785 return &P;
3786 return nullptr;
3787}
3788
3789// Bytes is a VPERM-like permute vector, except that -1 is used for
3790// undefined bytes. This permute is an operand of an outer permute.
3791// See whether redistributing the -1 bytes gives a shuffle that can be
3792// implemented using P. If so, set Transform to a VPERM-like permute vector
3793// that, when applied to the result of P, gives the original permute in Bytes.
3794static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3795 const Permute &P,
3796 SmallVectorImpl<int> &Transform) {
3797 unsigned To = 0;
3798 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) {
3799 int Elt = Bytes[From];
3800 if (Elt < 0)
3801 // Byte number From of the result is undefined.
3802 Transform[From] = -1;
3803 else {
3804 while (P.Bytes[To] != Elt) {
3805 To += 1;
3806 if (To == SystemZ::VectorBytes)
3807 return false;
3808 }
3809 Transform[From] = To;
3810 }
3811 }
3812 return true;
3813}
3814
3815// As above, but search for a matching permute.
3816static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3817 SmallVectorImpl<int> &Transform) {
3818 for (auto &P : PermuteForms)
3819 if (matchDoublePermute(Bytes, P, Transform))
3820 return &P;
3821 return nullptr;
3822}
3823
3824// Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask,
3825// as if it had type vNi8.
3826static void getVPermMask(ShuffleVectorSDNode *VSN,
3827 SmallVectorImpl<int> &Bytes) {
3828 EVT VT = VSN->getValueType(0);
3829 unsigned NumElements = VT.getVectorNumElements();
3830 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3831 Bytes.resize(NumElements * BytesPerElement, -1);
3832 for (unsigned I = 0; I < NumElements; ++I) {
3833 int Index = VSN->getMaskElt(I);
3834 if (Index >= 0)
3835 for (unsigned J = 0; J < BytesPerElement; ++J)
3836 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
3837 }
3838}
3839
3840// Bytes is a VPERM-like permute vector, except that -1 is used for
3841// undefined bytes. See whether bytes [Start, Start + BytesPerElement) of
3842// the result come from a contiguous sequence of bytes from one input.
3843// Set Base to the selector for the first byte if so.
3844static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start,
3845 unsigned BytesPerElement, int &Base) {
3846 Base = -1;
3847 for (unsigned I = 0; I < BytesPerElement; ++I) {
3848 if (Bytes[Start + I] >= 0) {
3849 unsigned Elem = Bytes[Start + I];
3850 if (Base < 0) {
3851 Base = Elem - I;
3852 // Make sure the bytes would come from one input operand.
3853 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size())
3854 return false;
3855 } else if (unsigned(Base) != Elem - I)
3856 return false;
3857 }
3858 }
3859 return true;
3860}
3861
3862// Bytes is a VPERM-like permute vector, except that -1 is used for
3863// undefined bytes. Return true if it can be performed using VSLDI.
3864// When returning true, set StartIndex to the shift amount and OpNo0
3865// and OpNo1 to the VPERM operands that should be used as the first
3866// and second shift operand respectively.
3867static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes,
3868 unsigned &StartIndex, unsigned &OpNo0,
3869 unsigned &OpNo1) {
3870 int OpNos[] = { -1, -1 };
3871 int Shift = -1;
3872 for (unsigned I = 0; I < 16; ++I) {
3873 int Index = Bytes[I];
3874 if (Index >= 0) {
3875 int ExpectedShift = (Index - I) % SystemZ::VectorBytes;
3876 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes;
3877 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes;
3878 if (Shift < 0)
3879 Shift = ExpectedShift;
3880 else if (Shift != ExpectedShift)
3881 return false;
3882 // Make sure that the operand mappings are consistent with previous
3883 // elements.
3884 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3885 return false;
3886 OpNos[ModelOpNo] = RealOpNo;
3887 }
3888 }
3889 StartIndex = Shift;
3890 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3891}
3892
3893// Create a node that performs P on operands Op0 and Op1, casting the
3894// operands to the appropriate type. The type of the result is determined by P.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003895static SDValue getPermuteNode(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003896 const Permute &P, SDValue Op0, SDValue Op1) {
3897 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input
3898 // elements of a PACK are twice as wide as the outputs.
3899 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 :
3900 P.Opcode == SystemZISD::PACK ? P.Operand * 2 :
3901 P.Operand);
3902 // Cast both operands to the appropriate type.
3903 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8),
3904 SystemZ::VectorBytes / InBytes);
3905 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0);
3906 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1);
3907 SDValue Op;
3908 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) {
3909 SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32);
3910 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2);
3911 } else if (P.Opcode == SystemZISD::PACK) {
3912 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8),
3913 SystemZ::VectorBytes / P.Operand);
3914 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1);
3915 } else {
3916 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1);
3917 }
3918 return Op;
3919}
3920
3921// Bytes is a VPERM-like permute vector, except that -1 is used for
3922// undefined bytes. Implement it on operands Ops[0] and Ops[1] using
3923// VSLDI or VPERM.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003924static SDValue getGeneralPermuteNode(SelectionDAG &DAG, const SDLoc &DL,
3925 SDValue *Ops,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003926 const SmallVectorImpl<int> &Bytes) {
3927 for (unsigned I = 0; I < 2; ++I)
3928 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]);
3929
3930 // First see whether VSLDI can be used.
3931 unsigned StartIndex, OpNo0, OpNo1;
3932 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1))
3933 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0],
3934 Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32));
3935
3936 // Fall back on VPERM. Construct an SDNode for the permute vector.
3937 SDValue IndexNodes[SystemZ::VectorBytes];
3938 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3939 if (Bytes[I] >= 0)
3940 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32);
3941 else
3942 IndexNodes[I] = DAG.getUNDEF(MVT::i32);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00003943 SDValue Op2 = DAG.getBuildVector(MVT::v16i8, DL, IndexNodes);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003944 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2);
3945}
3946
3947namespace {
3948// Describes a general N-operand vector shuffle.
3949struct GeneralShuffle {
3950 GeneralShuffle(EVT vt) : VT(vt) {}
3951 void addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003952 bool add(SDValue, unsigned);
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003953 SDValue getNode(SelectionDAG &, const SDLoc &);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003954
3955 // The operands of the shuffle.
3956 SmallVector<SDValue, SystemZ::VectorBytes> Ops;
3957
3958 // Index I is -1 if byte I of the result is undefined. Otherwise the
3959 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand
3960 // Bytes[I] / SystemZ::VectorBytes.
3961 SmallVector<int, SystemZ::VectorBytes> Bytes;
3962
3963 // The type of the shuffle result.
3964 EVT VT;
3965};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003966}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003967
3968// Add an extra undefined element to the shuffle.
3969void GeneralShuffle::addUndef() {
3970 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3971 for (unsigned I = 0; I < BytesPerElement; ++I)
3972 Bytes.push_back(-1);
3973}
3974
3975// Add an extra element to the shuffle, taking it from element Elem of Op.
3976// A null Op indicates a vector input whose value will be calculated later;
3977// there is at most one such input per shuffle and it always has the same
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003978// type as the result. Aborts and returns false if the source vector elements
3979// of an EXTRACT_VECTOR_ELT are smaller than the destination elements. Per
3980// LLVM they become implicitly extended, but this is rare and not optimized.
3981bool GeneralShuffle::add(SDValue Op, unsigned Elem) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003982 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3983
3984 // The source vector can have wider elements than the result,
3985 // either through an explicit TRUNCATE or because of type legalization.
3986 // We want the least significant part.
3987 EVT FromVT = Op.getNode() ? Op.getValueType() : VT;
3988 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003989
3990 // Return false if the source elements are smaller than their destination
3991 // elements.
3992 if (FromBytesPerElement < BytesPerElement)
3993 return false;
3994
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003995 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes +
3996 (FromBytesPerElement - BytesPerElement));
3997
3998 // Look through things like shuffles and bitcasts.
3999 while (Op.getNode()) {
4000 if (Op.getOpcode() == ISD::BITCAST)
4001 Op = Op.getOperand(0);
4002 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) {
4003 // See whether the bytes we need come from a contiguous part of one
4004 // operand.
4005 SmallVector<int, SystemZ::VectorBytes> OpBytes;
4006 getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes);
4007 int NewByte;
4008 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte))
4009 break;
4010 if (NewByte < 0) {
4011 addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004012 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004013 }
4014 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes);
4015 Byte = unsigned(NewByte) % SystemZ::VectorBytes;
Sanjay Patel57195842016-03-14 17:28:46 +00004016 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004017 addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004018 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004019 } else
4020 break;
4021 }
4022
4023 // Make sure that the source of the extraction is in Ops.
4024 unsigned OpNo = 0;
4025 for (; OpNo < Ops.size(); ++OpNo)
4026 if (Ops[OpNo] == Op)
4027 break;
4028 if (OpNo == Ops.size())
4029 Ops.push_back(Op);
4030
4031 // Add the element to Bytes.
4032 unsigned Base = OpNo * SystemZ::VectorBytes + Byte;
4033 for (unsigned I = 0; I < BytesPerElement; ++I)
4034 Bytes.push_back(Base + I);
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004035
4036 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004037}
4038
4039// Return SDNodes for the completed shuffle.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004040SDValue GeneralShuffle::getNode(SelectionDAG &DAG, const SDLoc &DL) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004041 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector");
4042
4043 if (Ops.size() == 0)
4044 return DAG.getUNDEF(VT);
4045
4046 // Make sure that there are at least two shuffle operands.
4047 if (Ops.size() == 1)
4048 Ops.push_back(DAG.getUNDEF(MVT::v16i8));
4049
4050 // Create a tree of shuffles, deferring root node until after the loop.
4051 // Try to redistribute the undefined elements of non-root nodes so that
4052 // the non-root shuffles match something like a pack or merge, then adjust
4053 // the parent node's permute vector to compensate for the new order.
4054 // Among other things, this copes with vectors like <2 x i16> that were
4055 // padded with undefined elements during type legalization.
4056 //
4057 // In the best case this redistribution will lead to the whole tree
4058 // using packs and merges. It should rarely be a loss in other cases.
4059 unsigned Stride = 1;
4060 for (; Stride * 2 < Ops.size(); Stride *= 2) {
4061 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) {
4062 SDValue SubOps[] = { Ops[I], Ops[I + Stride] };
4063
4064 // Create a mask for just these two operands.
4065 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes);
4066 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
4067 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes;
4068 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes;
4069 if (OpNo == I)
4070 NewBytes[J] = Byte;
4071 else if (OpNo == I + Stride)
4072 NewBytes[J] = SystemZ::VectorBytes + Byte;
4073 else
4074 NewBytes[J] = -1;
4075 }
4076 // See if it would be better to reorganize NewMask to avoid using VPERM.
4077 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes);
4078 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) {
4079 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]);
4080 // Applying NewBytesMap to Ops[I] gets back to NewBytes.
4081 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
4082 if (NewBytes[J] >= 0) {
4083 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes &&
4084 "Invalid double permute");
4085 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J];
4086 } else
4087 assert(NewBytesMap[J] < 0 && "Invalid double permute");
4088 }
4089 } else {
4090 // Just use NewBytes on the operands.
4091 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes);
4092 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J)
4093 if (NewBytes[J] >= 0)
4094 Bytes[J] = I * SystemZ::VectorBytes + J;
4095 }
4096 }
4097 }
4098
4099 // Now we just have 2 inputs. Put the second operand in Ops[1].
4100 if (Stride > 1) {
4101 Ops[1] = Ops[Stride];
4102 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
4103 if (Bytes[I] >= int(SystemZ::VectorBytes))
4104 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes;
4105 }
4106
4107 // Look for an instruction that can do the permute without resorting
4108 // to VPERM.
4109 unsigned OpNo0, OpNo1;
4110 SDValue Op;
4111 if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1))
4112 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]);
4113 else
4114 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes);
4115 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4116}
4117
Ulrich Weigandcd808232015-05-05 19:26:48 +00004118// Return true if the given BUILD_VECTOR is a scalar-to-vector conversion.
4119static bool isScalarToVector(SDValue Op) {
4120 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00004121 if (!Op.getOperand(I).isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004122 return false;
4123 return true;
4124}
4125
4126// Return a vector of type VT that contains Value in the first element.
4127// The other elements don't matter.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004128static SDValue buildScalarToVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandcd808232015-05-05 19:26:48 +00004129 SDValue Value) {
4130 // If we have a constant, replicate it to all elements and let the
4131 // BUILD_VECTOR lowering take care of it.
4132 if (Value.getOpcode() == ISD::Constant ||
4133 Value.getOpcode() == ISD::ConstantFP) {
4134 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004135 return DAG.getBuildVector(VT, DL, Ops);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004136 }
Sanjay Patel57195842016-03-14 17:28:46 +00004137 if (Value.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004138 return DAG.getUNDEF(VT);
4139 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
4140}
4141
4142// Return a vector of type VT in which Op0 is in element 0 and Op1 is in
4143// element 1. Used for cases in which replication is cheap.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004144static SDValue buildMergeScalars(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandcd808232015-05-05 19:26:48 +00004145 SDValue Op0, SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00004146 if (Op0.isUndef()) {
4147 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004148 return DAG.getUNDEF(VT);
4149 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1);
4150 }
Sanjay Patel57195842016-03-14 17:28:46 +00004151 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004152 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0);
4153 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT,
4154 buildScalarToVector(DAG, DL, VT, Op0),
4155 buildScalarToVector(DAG, DL, VT, Op1));
4156}
4157
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004158// Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64
4159// vector for them.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004160static SDValue joinDwords(SelectionDAG &DAG, const SDLoc &DL, SDValue Op0,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004161 SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00004162 if (Op0.isUndef() && Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004163 return DAG.getUNDEF(MVT::v2i64);
4164 // If one of the two inputs is undefined then replicate the other one,
4165 // in order to avoid using another register unnecessarily.
Sanjay Patel57195842016-03-14 17:28:46 +00004166 if (Op0.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004167 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
Sanjay Patel57195842016-03-14 17:28:46 +00004168 else if (Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004169 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
4170 else {
4171 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
4172 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
4173 }
4174 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1);
4175}
4176
4177// Try to represent constant BUILD_VECTOR node BVN using a
4178// SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask
4179// on success.
4180static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) {
4181 EVT ElemVT = BVN->getValueType(0).getVectorElementType();
4182 unsigned BytesPerElement = ElemVT.getStoreSize();
4183 for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) {
4184 SDValue Op = BVN->getOperand(I);
Sanjay Patel75068522016-03-14 18:09:43 +00004185 if (!Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004186 uint64_t Value;
4187 if (Op.getOpcode() == ISD::Constant)
4188 Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue();
4189 else if (Op.getOpcode() == ISD::ConstantFP)
4190 Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt()
4191 .getZExtValue());
4192 else
4193 return false;
4194 for (unsigned J = 0; J < BytesPerElement; ++J) {
4195 uint64_t Byte = (Value >> (J * 8)) & 0xff;
4196 if (Byte == 0xff)
Aaron Ballman2a3aa1f242015-05-11 12:45:53 +00004197 Mask |= 1ULL << ((E - I - 1) * BytesPerElement + J);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004198 else if (Byte != 0)
4199 return false;
4200 }
4201 }
4202 }
4203 return true;
4204}
4205
4206// Try to load a vector constant in which BitsPerElement-bit value Value
4207// is replicated to fill the vector. VT is the type of the resulting
4208// constant, which may have elements of a different size from BitsPerElement.
4209// Return the SDValue of the constant on success, otherwise return
4210// an empty value.
4211static SDValue tryBuildVectorReplicate(SelectionDAG &DAG,
4212 const SystemZInstrInfo *TII,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004213 const SDLoc &DL, EVT VT, uint64_t Value,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004214 unsigned BitsPerElement) {
4215 // Signed 16-bit values can be replicated using VREPI.
4216 int64_t SignedValue = SignExtend64(Value, BitsPerElement);
4217 if (isInt<16>(SignedValue)) {
4218 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
4219 SystemZ::VectorBits / BitsPerElement);
4220 SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT,
4221 DAG.getConstant(SignedValue, DL, MVT::i32));
4222 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4223 }
4224 // See whether rotating the constant left some N places gives a value that
4225 // is one less than a power of 2 (i.e. all zeros followed by all ones).
4226 // If so we can use VGM.
4227 unsigned Start, End;
4228 if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) {
4229 // isRxSBGMask returns the bit numbers for a full 64-bit value,
4230 // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to
4231 // bit numbers for an BitsPerElement value, so that 0 denotes
4232 // 1 << (BitsPerElement-1).
4233 Start -= 64 - BitsPerElement;
4234 End -= 64 - BitsPerElement;
4235 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
4236 SystemZ::VectorBits / BitsPerElement);
4237 SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT,
4238 DAG.getConstant(Start, DL, MVT::i32),
4239 DAG.getConstant(End, DL, MVT::i32));
4240 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4241 }
4242 return SDValue();
4243}
4244
4245// If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually
4246// better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for
4247// the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR
4248// would benefit from this representation and return it if so.
4249static SDValue tryBuildVectorShuffle(SelectionDAG &DAG,
4250 BuildVectorSDNode *BVN) {
4251 EVT VT = BVN->getValueType(0);
4252 unsigned NumElements = VT.getVectorNumElements();
4253
4254 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation
4255 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still
4256 // need a BUILD_VECTOR, add an additional placeholder operand for that
4257 // BUILD_VECTOR and store its operands in ResidueOps.
4258 GeneralShuffle GS(VT);
4259 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps;
4260 bool FoundOne = false;
4261 for (unsigned I = 0; I < NumElements; ++I) {
4262 SDValue Op = BVN->getOperand(I);
4263 if (Op.getOpcode() == ISD::TRUNCATE)
4264 Op = Op.getOperand(0);
4265 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4266 Op.getOperand(1).getOpcode() == ISD::Constant) {
4267 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004268 if (!GS.add(Op.getOperand(0), Elem))
4269 return SDValue();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004270 FoundOne = true;
Sanjay Patel57195842016-03-14 17:28:46 +00004271 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004272 GS.addUndef();
4273 } else {
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004274 if (!GS.add(SDValue(), ResidueOps.size()))
4275 return SDValue();
Ulrich Weigande861e642015-09-15 14:27:46 +00004276 ResidueOps.push_back(BVN->getOperand(I));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004277 }
4278 }
4279
4280 // Nothing to do if there are no EXTRACT_VECTOR_ELTs.
4281 if (!FoundOne)
4282 return SDValue();
4283
4284 // Create the BUILD_VECTOR for the remaining elements, if any.
4285 if (!ResidueOps.empty()) {
4286 while (ResidueOps.size() < NumElements)
Ulrich Weigandf4d14f72015-10-08 17:46:59 +00004287 ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType()));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004288 for (auto &Op : GS.Ops) {
4289 if (!Op.getNode()) {
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004290 Op = DAG.getBuildVector(VT, SDLoc(BVN), ResidueOps);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004291 break;
4292 }
4293 }
4294 }
4295 return GS.getNode(DAG, SDLoc(BVN));
4296}
4297
4298// Combine GPR scalar values Elems into a vector of type VT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004299static SDValue buildVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004300 SmallVectorImpl<SDValue> &Elems) {
4301 // See whether there is a single replicated value.
4302 SDValue Single;
4303 unsigned int NumElements = Elems.size();
4304 unsigned int Count = 0;
4305 for (auto Elem : Elems) {
Sanjay Patel75068522016-03-14 18:09:43 +00004306 if (!Elem.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004307 if (!Single.getNode())
4308 Single = Elem;
4309 else if (Elem != Single) {
4310 Single = SDValue();
4311 break;
4312 }
4313 Count += 1;
4314 }
4315 }
4316 // There are three cases here:
4317 //
4318 // - if the only defined element is a loaded one, the best sequence
4319 // is a replicating load.
4320 //
4321 // - otherwise, if the only defined element is an i64 value, we will
4322 // end up with the same VLVGP sequence regardless of whether we short-cut
4323 // for replication or fall through to the later code.
4324 //
4325 // - otherwise, if the only defined element is an i32 or smaller value,
4326 // we would need 2 instructions to replicate it: VLVGP followed by VREPx.
4327 // This is only a win if the single defined element is used more than once.
4328 // In other cases we're better off using a single VLVGx.
4329 if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD))
4330 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single);
4331
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004332 // If all elements are loads, use VLREP/VLEs (below).
4333 bool AllLoads = true;
4334 for (auto Elem : Elems)
4335 if (Elem.getOpcode() != ISD::LOAD || cast<LoadSDNode>(Elem)->isIndexed()) {
4336 AllLoads = false;
4337 break;
4338 }
4339
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004340 // The best way of building a v2i64 from two i64s is to use VLVGP.
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004341 if (VT == MVT::v2i64 && !AllLoads)
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004342 return joinDwords(DAG, DL, Elems[0], Elems[1]);
4343
Ulrich Weigandcd808232015-05-05 19:26:48 +00004344 // Use a 64-bit merge high to combine two doubles.
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004345 if (VT == MVT::v2f64 && !AllLoads)
Ulrich Weigandcd808232015-05-05 19:26:48 +00004346 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4347
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004348 // Build v4f32 values directly from the FPRs:
4349 //
4350 // <Axxx> <Bxxx> <Cxxxx> <Dxxx>
4351 // V V VMRHF
4352 // <ABxx> <CDxx>
4353 // V VMRHG
4354 // <ABCD>
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004355 if (VT == MVT::v4f32 && !AllLoads) {
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004356 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4357 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]);
4358 // Avoid unnecessary undefs by reusing the other operand.
Sanjay Patel57195842016-03-14 17:28:46 +00004359 if (Op01.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004360 Op01 = Op23;
Sanjay Patel57195842016-03-14 17:28:46 +00004361 else if (Op23.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004362 Op23 = Op01;
4363 // Merging identical replications is a no-op.
4364 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23)
4365 return Op01;
4366 Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01);
4367 Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23);
4368 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH,
4369 DL, MVT::v2i64, Op01, Op23);
4370 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4371 }
4372
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004373 // Collect the constant terms.
4374 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue());
4375 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false);
4376
4377 unsigned NumConstants = 0;
4378 for (unsigned I = 0; I < NumElements; ++I) {
4379 SDValue Elem = Elems[I];
4380 if (Elem.getOpcode() == ISD::Constant ||
4381 Elem.getOpcode() == ISD::ConstantFP) {
4382 NumConstants += 1;
4383 Constants[I] = Elem;
4384 Done[I] = true;
4385 }
4386 }
4387 // If there was at least one constant, fill in the other elements of
4388 // Constants with undefs to get a full vector constant and use that
4389 // as the starting point.
4390 SDValue Result;
4391 if (NumConstants > 0) {
4392 for (unsigned I = 0; I < NumElements; ++I)
4393 if (!Constants[I].getNode())
4394 Constants[I] = DAG.getUNDEF(Elems[I].getValueType());
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004395 Result = DAG.getBuildVector(VT, DL, Constants);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004396 } else {
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004397 // Otherwise try to use VLREP or VLVGP to start the sequence in order to
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004398 // avoid a false dependency on any previous contents of the vector
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004399 // register.
4400
4401 // Use a VLREP if at least one element is a load.
4402 unsigned LoadElIdx = UINT_MAX;
4403 for (unsigned I = 0; I < NumElements; ++I)
4404 if (Elems[I].getOpcode() == ISD::LOAD &&
4405 cast<LoadSDNode>(Elems[I])->isUnindexed()) {
4406 LoadElIdx = I;
4407 break;
4408 }
4409 if (LoadElIdx != UINT_MAX) {
4410 Result = DAG.getNode(SystemZISD::REPLICATE, DL, VT, Elems[LoadElIdx]);
4411 Done[LoadElIdx] = true;
4412 } else {
4413 // Try to use VLVGP.
4414 unsigned I1 = NumElements / 2 - 1;
4415 unsigned I2 = NumElements - 1;
4416 bool Def1 = !Elems[I1].isUndef();
4417 bool Def2 = !Elems[I2].isUndef();
4418 if (Def1 || Def2) {
4419 SDValue Elem1 = Elems[Def1 ? I1 : I2];
4420 SDValue Elem2 = Elems[Def2 ? I2 : I1];
4421 Result = DAG.getNode(ISD::BITCAST, DL, VT,
4422 joinDwords(DAG, DL, Elem1, Elem2));
4423 Done[I1] = true;
4424 Done[I2] = true;
4425 } else
4426 Result = DAG.getUNDEF(VT);
4427 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004428 }
4429
4430 // Use VLVGx to insert the other elements.
4431 for (unsigned I = 0; I < NumElements; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00004432 if (!Done[I] && !Elems[I].isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004433 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I],
4434 DAG.getConstant(I, DL, MVT::i32));
4435 return Result;
4436}
4437
4438SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op,
4439 SelectionDAG &DAG) const {
4440 const SystemZInstrInfo *TII =
4441 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
4442 auto *BVN = cast<BuildVectorSDNode>(Op.getNode());
4443 SDLoc DL(Op);
4444 EVT VT = Op.getValueType();
4445
4446 if (BVN->isConstant()) {
4447 // Try using VECTOR GENERATE BYTE MASK. This is the architecturally-
4448 // preferred way of creating all-zero and all-one vectors so give it
4449 // priority over other methods below.
4450 uint64_t Mask = 0;
4451 if (tryBuildVectorByteMask(BVN, Mask)) {
4452 SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
4453 DAG.getConstant(Mask, DL, MVT::i32));
4454 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4455 }
4456
4457 // Try using some form of replication.
4458 APInt SplatBits, SplatUndef;
4459 unsigned SplatBitSize;
4460 bool HasAnyUndefs;
4461 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4462 8, true) &&
4463 SplatBitSize <= 64) {
4464 // First try assuming that any undefined bits above the highest set bit
4465 // and below the lowest set bit are 1s. This increases the likelihood of
4466 // being able to use a sign-extended element value in VECTOR REPLICATE
4467 // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK.
4468 uint64_t SplatBitsZ = SplatBits.getZExtValue();
4469 uint64_t SplatUndefZ = SplatUndef.getZExtValue();
4470 uint64_t Lower = (SplatUndefZ
4471 & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1));
4472 uint64_t Upper = (SplatUndefZ
4473 & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1));
4474 uint64_t Value = SplatBitsZ | Upper | Lower;
4475 SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value,
4476 SplatBitSize);
4477 if (Op.getNode())
4478 return Op;
4479
4480 // Now try assuming that any undefined bits between the first and
4481 // last defined set bits are set. This increases the chances of
4482 // using a non-wraparound mask.
4483 uint64_t Middle = SplatUndefZ & ~Upper & ~Lower;
4484 Value = SplatBitsZ | Middle;
4485 Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize);
4486 if (Op.getNode())
4487 return Op;
4488 }
4489
4490 // Fall back to loading it from memory.
4491 return SDValue();
4492 }
4493
4494 // See if we should use shuffles to construct the vector from other vectors.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004495 if (SDValue Res = tryBuildVectorShuffle(DAG, BVN))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004496 return Res;
4497
Ulrich Weigandcd808232015-05-05 19:26:48 +00004498 // Detect SCALAR_TO_VECTOR conversions.
4499 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op))
4500 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0));
4501
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004502 // Otherwise use buildVector to build the vector up from GPRs.
4503 unsigned NumElements = Op.getNumOperands();
4504 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements);
4505 for (unsigned I = 0; I < NumElements; ++I)
4506 Ops[I] = Op.getOperand(I);
4507 return buildVector(DAG, DL, VT, Ops);
4508}
4509
4510SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
4511 SelectionDAG &DAG) const {
4512 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode());
4513 SDLoc DL(Op);
4514 EVT VT = Op.getValueType();
4515 unsigned NumElements = VT.getVectorNumElements();
4516
4517 if (VSN->isSplat()) {
4518 SDValue Op0 = Op.getOperand(0);
4519 unsigned Index = VSN->getSplatIndex();
4520 assert(Index < VT.getVectorNumElements() &&
4521 "Splat index should be defined and in first operand");
4522 // See whether the value we're splatting is directly available as a scalar.
4523 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4524 Op0.getOpcode() == ISD::BUILD_VECTOR)
4525 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index));
4526 // Otherwise keep it as a vector-to-vector operation.
4527 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0),
4528 DAG.getConstant(Index, DL, MVT::i32));
4529 }
4530
4531 GeneralShuffle GS(VT);
4532 for (unsigned I = 0; I < NumElements; ++I) {
4533 int Elt = VSN->getMaskElt(I);
4534 if (Elt < 0)
4535 GS.addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004536 else if (!GS.add(Op.getOperand(unsigned(Elt) / NumElements),
4537 unsigned(Elt) % NumElements))
4538 return SDValue();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004539 }
4540 return GS.getNode(DAG, SDLoc(VSN));
4541}
4542
4543SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
4544 SelectionDAG &DAG) const {
4545 SDLoc DL(Op);
4546 // Just insert the scalar into element 0 of an undefined vector.
4547 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL,
4548 Op.getValueType(), DAG.getUNDEF(Op.getValueType()),
4549 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32));
4550}
4551
Ulrich Weigandcd808232015-05-05 19:26:48 +00004552SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
4553 SelectionDAG &DAG) const {
4554 // Handle insertions of floating-point values.
4555 SDLoc DL(Op);
4556 SDValue Op0 = Op.getOperand(0);
4557 SDValue Op1 = Op.getOperand(1);
4558 SDValue Op2 = Op.getOperand(2);
4559 EVT VT = Op.getValueType();
4560
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004561 // Insertions into constant indices of a v2f64 can be done using VPDI.
4562 // However, if the inserted value is a bitcast or a constant then it's
4563 // better to use GPRs, as below.
4564 if (VT == MVT::v2f64 &&
4565 Op1.getOpcode() != ISD::BITCAST &&
Ulrich Weigandcd808232015-05-05 19:26:48 +00004566 Op1.getOpcode() != ISD::ConstantFP &&
4567 Op2.getOpcode() == ISD::Constant) {
4568 uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue();
4569 unsigned Mask = VT.getVectorNumElements() - 1;
4570 if (Index <= Mask)
4571 return Op;
4572 }
4573
4574 // Otherwise bitcast to the equivalent integer form and insert via a GPR.
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004575 MVT IntVT = MVT::getIntegerVT(VT.getScalarSizeInBits());
Ulrich Weigandcd808232015-05-05 19:26:48 +00004576 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements());
4577 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT,
4578 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0),
4579 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2);
4580 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4581}
4582
4583SDValue
4584SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
4585 SelectionDAG &DAG) const {
4586 // Handle extractions of floating-point values.
4587 SDLoc DL(Op);
4588 SDValue Op0 = Op.getOperand(0);
4589 SDValue Op1 = Op.getOperand(1);
4590 EVT VT = Op.getValueType();
4591 EVT VecVT = Op0.getValueType();
4592
4593 // Extractions of constant indices can be done directly.
4594 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) {
4595 uint64_t Index = CIndexN->getZExtValue();
4596 unsigned Mask = VecVT.getVectorNumElements() - 1;
4597 if (Index <= Mask)
4598 return Op;
4599 }
4600
4601 // Otherwise bitcast to the equivalent integer form and extract via a GPR.
4602 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
4603 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements());
4604 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT,
4605 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1);
4606 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4607}
4608
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004609SDValue
4610SystemZTargetLowering::lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004611 unsigned UnpackHigh) const {
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004612 SDValue PackedOp = Op.getOperand(0);
4613 EVT OutVT = Op.getValueType();
4614 EVT InVT = PackedOp.getValueType();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004615 unsigned ToBits = OutVT.getScalarSizeInBits();
4616 unsigned FromBits = InVT.getScalarSizeInBits();
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004617 do {
4618 FromBits *= 2;
4619 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits),
4620 SystemZ::VectorBits / FromBits);
4621 PackedOp = DAG.getNode(UnpackHigh, SDLoc(PackedOp), OutVT, PackedOp);
4622 } while (FromBits != ToBits);
4623 return PackedOp;
4624}
4625
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004626SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG,
4627 unsigned ByScalar) const {
4628 // Look for cases where a vector shift can use the *_BY_SCALAR form.
4629 SDValue Op0 = Op.getOperand(0);
4630 SDValue Op1 = Op.getOperand(1);
4631 SDLoc DL(Op);
4632 EVT VT = Op.getValueType();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004633 unsigned ElemBitSize = VT.getScalarSizeInBits();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004634
4635 // See whether the shift vector is a splat represented as BUILD_VECTOR.
4636 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) {
4637 APInt SplatBits, SplatUndef;
4638 unsigned SplatBitSize;
4639 bool HasAnyUndefs;
4640 // Check for constant splats. Use ElemBitSize as the minimum element
4641 // width and reject splats that need wider elements.
4642 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4643 ElemBitSize, true) &&
4644 SplatBitSize == ElemBitSize) {
4645 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff,
4646 DL, MVT::i32);
4647 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4648 }
4649 // Check for variable splats.
4650 BitVector UndefElements;
4651 SDValue Splat = BVN->getSplatValue(&UndefElements);
4652 if (Splat) {
4653 // Since i32 is the smallest legal type, we either need a no-op
4654 // or a truncation.
4655 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat);
4656 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4657 }
4658 }
4659
4660 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR,
4661 // and the shift amount is directly available in a GPR.
4662 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) {
4663 if (VSN->isSplat()) {
4664 SDValue VSNOp0 = VSN->getOperand(0);
4665 unsigned Index = VSN->getSplatIndex();
4666 assert(Index < VT.getVectorNumElements() &&
4667 "Splat index should be defined and in first operand");
4668 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4669 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) {
4670 // Since i32 is the smallest legal type, we either need a no-op
4671 // or a truncation.
4672 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32,
4673 VSNOp0.getOperand(Index));
4674 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4675 }
4676 }
4677 }
4678
4679 // Otherwise just treat the current form as legal.
4680 return Op;
4681}
4682
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004683SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
4684 SelectionDAG &DAG) const {
4685 switch (Op.getOpcode()) {
Ulrich Weigandf557d082016-04-04 12:44:55 +00004686 case ISD::FRAMEADDR:
4687 return lowerFRAMEADDR(Op, DAG);
4688 case ISD::RETURNADDR:
4689 return lowerRETURNADDR(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004690 case ISD::BR_CC:
4691 return lowerBR_CC(Op, DAG);
4692 case ISD::SELECT_CC:
4693 return lowerSELECT_CC(Op, DAG);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00004694 case ISD::SETCC:
4695 return lowerSETCC(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004696 case ISD::GlobalAddress:
4697 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
4698 case ISD::GlobalTLSAddress:
4699 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
4700 case ISD::BlockAddress:
4701 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
4702 case ISD::JumpTable:
4703 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
4704 case ISD::ConstantPool:
4705 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
4706 case ISD::BITCAST:
4707 return lowerBITCAST(Op, DAG);
4708 case ISD::VASTART:
4709 return lowerVASTART(Op, DAG);
4710 case ISD::VACOPY:
4711 return lowerVACOPY(Op, DAG);
4712 case ISD::DYNAMIC_STACKALLOC:
4713 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +00004714 case ISD::GET_DYNAMIC_AREA_OFFSET:
4715 return lowerGET_DYNAMIC_AREA_OFFSET(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00004716 case ISD::SMUL_LOHI:
4717 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004718 case ISD::UMUL_LOHI:
4719 return lowerUMUL_LOHI(Op, DAG);
4720 case ISD::SDIVREM:
4721 return lowerSDIVREM(Op, DAG);
4722 case ISD::UDIVREM:
4723 return lowerUDIVREM(Op, DAG);
4724 case ISD::OR:
4725 return lowerOR(Op, DAG);
Ulrich Weigandb4012182015-03-31 12:56:33 +00004726 case ISD::CTPOP:
4727 return lowerCTPOP(Op, DAG);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004728 case ISD::ATOMIC_FENCE:
4729 return lowerATOMIC_FENCE(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004730 case ISD::ATOMIC_SWAP:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004731 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
4732 case ISD::ATOMIC_STORE:
4733 return lowerATOMIC_STORE(Op, DAG);
4734 case ISD::ATOMIC_LOAD:
4735 return lowerATOMIC_LOAD(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004736 case ISD::ATOMIC_LOAD_ADD:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004737 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004738 case ISD::ATOMIC_LOAD_SUB:
Richard Sandiford41350a52013-12-24 15:18:04 +00004739 return lowerATOMIC_LOAD_SUB(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004740 case ISD::ATOMIC_LOAD_AND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004741 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004742 case ISD::ATOMIC_LOAD_OR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004743 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004744 case ISD::ATOMIC_LOAD_XOR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004745 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004746 case ISD::ATOMIC_LOAD_NAND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004747 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004748 case ISD::ATOMIC_LOAD_MIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004749 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004750 case ISD::ATOMIC_LOAD_MAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004751 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004752 case ISD::ATOMIC_LOAD_UMIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004753 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004754 case ISD::ATOMIC_LOAD_UMAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004755 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004756 case ISD::ATOMIC_CMP_SWAP:
4757 return lowerATOMIC_CMP_SWAP(Op, DAG);
4758 case ISD::STACKSAVE:
4759 return lowerSTACKSAVE(Op, DAG);
4760 case ISD::STACKRESTORE:
4761 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00004762 case ISD::PREFETCH:
4763 return lowerPREFETCH(Op, DAG);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004764 case ISD::INTRINSIC_W_CHAIN:
4765 return lowerINTRINSIC_W_CHAIN(Op, DAG);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004766 case ISD::INTRINSIC_WO_CHAIN:
4767 return lowerINTRINSIC_WO_CHAIN(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004768 case ISD::BUILD_VECTOR:
4769 return lowerBUILD_VECTOR(Op, DAG);
4770 case ISD::VECTOR_SHUFFLE:
4771 return lowerVECTOR_SHUFFLE(Op, DAG);
4772 case ISD::SCALAR_TO_VECTOR:
4773 return lowerSCALAR_TO_VECTOR(Op, DAG);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004774 case ISD::INSERT_VECTOR_ELT:
4775 return lowerINSERT_VECTOR_ELT(Op, DAG);
4776 case ISD::EXTRACT_VECTOR_ELT:
4777 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004778 case ISD::SIGN_EXTEND_VECTOR_INREG:
4779 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACK_HIGH);
4780 case ISD::ZERO_EXTEND_VECTOR_INREG:
4781 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACKL_HIGH);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004782 case ISD::SHL:
4783 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR);
4784 case ISD::SRL:
4785 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR);
4786 case ISD::SRA:
4787 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004788 default:
4789 llvm_unreachable("Unexpected node to lower");
4790 }
4791}
4792
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004793// Lower operations with invalid operand or result types (currently used
4794// only for 128-bit integer types).
4795
4796static SDValue lowerI128ToGR128(SelectionDAG &DAG, SDValue In) {
4797 SDLoc DL(In);
4798 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, In,
4799 DAG.getIntPtrConstant(0, DL));
4800 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, In,
4801 DAG.getIntPtrConstant(1, DL));
4802 SDNode *Pair = DAG.getMachineNode(SystemZ::PAIR128, DL,
4803 MVT::Untyped, Hi, Lo);
4804 return SDValue(Pair, 0);
4805}
4806
4807static SDValue lowerGR128ToI128(SelectionDAG &DAG, SDValue In) {
4808 SDLoc DL(In);
4809 SDValue Hi = DAG.getTargetExtractSubreg(SystemZ::subreg_h64,
4810 DL, MVT::i64, In);
4811 SDValue Lo = DAG.getTargetExtractSubreg(SystemZ::subreg_l64,
4812 DL, MVT::i64, In);
4813 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i128, Lo, Hi);
4814}
4815
4816void
4817SystemZTargetLowering::LowerOperationWrapper(SDNode *N,
4818 SmallVectorImpl<SDValue> &Results,
4819 SelectionDAG &DAG) const {
4820 switch (N->getOpcode()) {
4821 case ISD::ATOMIC_LOAD: {
4822 SDLoc DL(N);
4823 SDVTList Tys = DAG.getVTList(MVT::Untyped, MVT::Other);
4824 SDValue Ops[] = { N->getOperand(0), N->getOperand(1) };
4825 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
4826 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_LOAD_128,
4827 DL, Tys, Ops, MVT::i128, MMO);
4828 Results.push_back(lowerGR128ToI128(DAG, Res));
4829 Results.push_back(Res.getValue(1));
4830 break;
4831 }
4832 case ISD::ATOMIC_STORE: {
4833 SDLoc DL(N);
4834 SDVTList Tys = DAG.getVTList(MVT::Other);
4835 SDValue Ops[] = { N->getOperand(0),
4836 lowerI128ToGR128(DAG, N->getOperand(2)),
4837 N->getOperand(1) };
4838 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
4839 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_STORE_128,
4840 DL, Tys, Ops, MVT::i128, MMO);
4841 // We have to enforce sequential consistency by performing a
4842 // serialization operation after the store.
4843 if (cast<AtomicSDNode>(N)->getOrdering() ==
4844 AtomicOrdering::SequentiallyConsistent)
4845 Res = SDValue(DAG.getMachineNode(SystemZ::Serialize, DL,
4846 MVT::Other, Res), 0);
4847 Results.push_back(Res);
4848 break;
4849 }
4850 case ISD::ATOMIC_CMP_SWAP: {
4851 SDLoc DL(N);
4852 SDVTList Tys = DAG.getVTList(MVT::Untyped, MVT::Other);
4853 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
4854 lowerI128ToGR128(DAG, N->getOperand(2)),
4855 lowerI128ToGR128(DAG, N->getOperand(3)) };
4856 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
4857 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAP_128,
4858 DL, Tys, Ops, MVT::i128, MMO);
4859 Results.push_back(lowerGR128ToI128(DAG, Res));
4860 Results.push_back(Res.getValue(1));
4861 break;
4862 }
4863 default:
4864 llvm_unreachable("Unexpected node to lower");
4865 }
4866}
4867
4868void
4869SystemZTargetLowering::ReplaceNodeResults(SDNode *N,
4870 SmallVectorImpl<SDValue> &Results,
4871 SelectionDAG &DAG) const {
4872 return LowerOperationWrapper(N, Results, DAG);
4873}
4874
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004875const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
4876#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
Matthias Braund04893f2015-05-07 21:33:59 +00004877 switch ((SystemZISD::NodeType)Opcode) {
4878 case SystemZISD::FIRST_NUMBER: break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004879 OPCODE(RET_FLAG);
4880 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00004881 OPCODE(SIBCALL);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004882 OPCODE(TLS_GDCALL);
4883 OPCODE(TLS_LDCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004884 OPCODE(PCREL_WRAPPER);
Richard Sandiford54b36912013-09-27 15:14:04 +00004885 OPCODE(PCREL_OFFSET);
Richard Sandiford57485472013-12-13 15:35:00 +00004886 OPCODE(IABS);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00004887 OPCODE(ICMP);
4888 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00004889 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004890 OPCODE(BR_CCMASK);
4891 OPCODE(SELECT_CCMASK);
4892 OPCODE(ADJDYNALLOC);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004893 OPCODE(POPCNT);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +00004894 OPCODE(SMUL_LOHI);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00004895 OPCODE(UMUL_LOHI);
4896 OPCODE(SDIVREM);
4897 OPCODE(UDIVREM);
Richard Sandifordd131ff82013-07-08 09:35:23 +00004898 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004899 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00004900 OPCODE(NC);
4901 OPCODE(NC_LOOP);
4902 OPCODE(OC);
4903 OPCODE(OC_LOOP);
4904 OPCODE(XC);
4905 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00004906 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004907 OPCODE(CLC_LOOP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00004908 OPCODE(STPCPY);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004909 OPCODE(STRCMP);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00004910 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00004911 OPCODE(IPM);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004912 OPCODE(MEMBARRIER);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004913 OPCODE(TBEGIN);
4914 OPCODE(TBEGIN_NOFLOAT);
4915 OPCODE(TEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004916 OPCODE(BYTE_MASK);
4917 OPCODE(ROTATE_MASK);
4918 OPCODE(REPLICATE);
4919 OPCODE(JOIN_DWORDS);
4920 OPCODE(SPLAT);
4921 OPCODE(MERGE_HIGH);
4922 OPCODE(MERGE_LOW);
4923 OPCODE(SHL_DOUBLE);
4924 OPCODE(PERMUTE_DWORDS);
4925 OPCODE(PERMUTE);
4926 OPCODE(PACK);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004927 OPCODE(PACKS_CC);
4928 OPCODE(PACKLS_CC);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004929 OPCODE(UNPACK_HIGH);
4930 OPCODE(UNPACKL_HIGH);
4931 OPCODE(UNPACK_LOW);
4932 OPCODE(UNPACKL_LOW);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004933 OPCODE(VSHL_BY_SCALAR);
4934 OPCODE(VSRL_BY_SCALAR);
4935 OPCODE(VSRA_BY_SCALAR);
4936 OPCODE(VSUM);
4937 OPCODE(VICMPE);
4938 OPCODE(VICMPH);
4939 OPCODE(VICMPHL);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004940 OPCODE(VICMPES);
4941 OPCODE(VICMPHS);
4942 OPCODE(VICMPHLS);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004943 OPCODE(VFCMPE);
4944 OPCODE(VFCMPH);
4945 OPCODE(VFCMPHE);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004946 OPCODE(VFCMPES);
4947 OPCODE(VFCMPHS);
4948 OPCODE(VFCMPHES);
4949 OPCODE(VFTCI);
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004950 OPCODE(VEXTEND);
4951 OPCODE(VROUND);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004952 OPCODE(VTM);
4953 OPCODE(VFAE_CC);
4954 OPCODE(VFAEZ_CC);
4955 OPCODE(VFEE_CC);
4956 OPCODE(VFEEZ_CC);
4957 OPCODE(VFENE_CC);
4958 OPCODE(VFENEZ_CC);
4959 OPCODE(VISTR_CC);
4960 OPCODE(VSTRC_CC);
4961 OPCODE(VSTRCZ_CC);
Marcin Koscielnicki32e87342016-07-02 02:20:40 +00004962 OPCODE(TDC);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004963 OPCODE(ATOMIC_SWAPW);
4964 OPCODE(ATOMIC_LOADW_ADD);
4965 OPCODE(ATOMIC_LOADW_SUB);
4966 OPCODE(ATOMIC_LOADW_AND);
4967 OPCODE(ATOMIC_LOADW_OR);
4968 OPCODE(ATOMIC_LOADW_XOR);
4969 OPCODE(ATOMIC_LOADW_NAND);
4970 OPCODE(ATOMIC_LOADW_MIN);
4971 OPCODE(ATOMIC_LOADW_MAX);
4972 OPCODE(ATOMIC_LOADW_UMIN);
4973 OPCODE(ATOMIC_LOADW_UMAX);
4974 OPCODE(ATOMIC_CMP_SWAPW);
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004975 OPCODE(ATOMIC_LOAD_128);
4976 OPCODE(ATOMIC_STORE_128);
4977 OPCODE(ATOMIC_CMP_SWAP_128);
Bryan Chan28b759c2016-05-16 20:32:22 +00004978 OPCODE(LRV);
4979 OPCODE(STRV);
Richard Sandiford03481332013-08-23 11:36:42 +00004980 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004981 }
Craig Topper062a2ba2014-04-25 05:30:21 +00004982 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004983#undef OPCODE
4984}
4985
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004986// Return true if VT is a vector whose elements are a whole number of bytes
Jonas Paulssoncad72ef2017-04-07 12:35:11 +00004987// in width. Also check for presence of vector support.
4988bool SystemZTargetLowering::canTreatAsByteVector(EVT VT) const {
4989 if (!Subtarget.hasVector())
4990 return false;
4991
Jonas Paulsson1d33cd32017-03-07 09:49:31 +00004992 return VT.isVector() && VT.getScalarSizeInBits() % 8 == 0 && VT.isSimple();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004993}
4994
4995// Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT
4996// producing a result of type ResVT. Op is a possibly bitcast version
4997// of the input vector and Index is the index (based on type VecVT) that
4998// should be extracted. Return the new extraction if a simplification
4999// was possible or if Force is true.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005000SDValue SystemZTargetLowering::combineExtract(const SDLoc &DL, EVT ResVT,
5001 EVT VecVT, SDValue Op,
5002 unsigned Index,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005003 DAGCombinerInfo &DCI,
5004 bool Force) const {
5005 SelectionDAG &DAG = DCI.DAG;
5006
5007 // The number of bytes being extracted.
5008 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
5009
5010 for (;;) {
5011 unsigned Opcode = Op.getOpcode();
5012 if (Opcode == ISD::BITCAST)
5013 // Look through bitcasts.
5014 Op = Op.getOperand(0);
5015 else if (Opcode == ISD::VECTOR_SHUFFLE &&
5016 canTreatAsByteVector(Op.getValueType())) {
5017 // Get a VPERM-like permute mask and see whether the bytes covered
5018 // by the extracted element are a contiguous sequence from one
5019 // source operand.
5020 SmallVector<int, SystemZ::VectorBytes> Bytes;
5021 getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes);
5022 int First;
5023 if (!getShuffleInput(Bytes, Index * BytesPerElement,
5024 BytesPerElement, First))
5025 break;
5026 if (First < 0)
5027 return DAG.getUNDEF(ResVT);
5028 // Make sure the contiguous sequence starts at a multiple of the
5029 // original element size.
5030 unsigned Byte = unsigned(First) % Bytes.size();
5031 if (Byte % BytesPerElement != 0)
5032 break;
5033 // We can get the extracted value directly from an input.
5034 Index = Byte / BytesPerElement;
5035 Op = Op.getOperand(unsigned(First) / Bytes.size());
5036 Force = true;
5037 } else if (Opcode == ISD::BUILD_VECTOR &&
5038 canTreatAsByteVector(Op.getValueType())) {
5039 // We can only optimize this case if the BUILD_VECTOR elements are
5040 // at least as wide as the extracted value.
5041 EVT OpVT = Op.getValueType();
5042 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
5043 if (OpBytesPerElement < BytesPerElement)
5044 break;
5045 // Make sure that the least-significant bit of the extracted value
5046 // is the least significant bit of an input.
5047 unsigned End = (Index + 1) * BytesPerElement;
5048 if (End % OpBytesPerElement != 0)
5049 break;
5050 // We're extracting the low part of one operand of the BUILD_VECTOR.
5051 Op = Op.getOperand(End / OpBytesPerElement - 1);
5052 if (!Op.getValueType().isInteger()) {
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00005053 EVT VT = MVT::getIntegerVT(Op.getValueSizeInBits());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005054 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
5055 DCI.AddToWorklist(Op.getNode());
5056 }
5057 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits());
5058 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
5059 if (VT != ResVT) {
5060 DCI.AddToWorklist(Op.getNode());
5061 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op);
5062 }
5063 return Op;
5064 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00005065 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG ||
5066 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) &&
5067 canTreatAsByteVector(Op.getValueType()) &&
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005068 canTreatAsByteVector(Op.getOperand(0).getValueType())) {
5069 // Make sure that only the unextended bits are significant.
5070 EVT ExtVT = Op.getValueType();
5071 EVT OpVT = Op.getOperand(0).getValueType();
5072 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize();
5073 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
5074 unsigned Byte = Index * BytesPerElement;
5075 unsigned SubByte = Byte % ExtBytesPerElement;
5076 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement;
5077 if (SubByte < MinSubByte ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00005078 SubByte + BytesPerElement > ExtBytesPerElement)
5079 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005080 // Get the byte offset of the unextended element
5081 Byte = Byte / ExtBytesPerElement * OpBytesPerElement;
5082 // ...then add the byte offset relative to that element.
5083 Byte += SubByte - MinSubByte;
5084 if (Byte % BytesPerElement != 0)
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00005085 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005086 Op = Op.getOperand(0);
5087 Index = Byte / BytesPerElement;
5088 Force = true;
5089 } else
5090 break;
5091 }
5092 if (Force) {
5093 if (Op.getValueType() != VecVT) {
5094 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op);
5095 DCI.AddToWorklist(Op.getNode());
5096 }
5097 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op,
5098 DAG.getConstant(Index, DL, MVT::i32));
5099 }
5100 return SDValue();
5101}
5102
5103// Optimize vector operations in scalar value Op on the basis that Op
5104// is truncated to TruncVT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005105SDValue SystemZTargetLowering::combineTruncateExtract(
5106 const SDLoc &DL, EVT TruncVT, SDValue Op, DAGCombinerInfo &DCI) const {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005107 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into
5108 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements
5109 // of type TruncVT.
5110 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5111 TruncVT.getSizeInBits() % 8 == 0) {
5112 SDValue Vec = Op.getOperand(0);
5113 EVT VecVT = Vec.getValueType();
5114 if (canTreatAsByteVector(VecVT)) {
5115 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
5116 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
5117 unsigned TruncBytes = TruncVT.getStoreSize();
5118 if (BytesPerElement % TruncBytes == 0) {
5119 // Calculate the value of Y' in the above description. We are
5120 // splitting the original elements into Scale equal-sized pieces
5121 // and for truncation purposes want the last (least-significant)
5122 // of these pieces for IndexN. This is easiest to do by calculating
5123 // the start index of the following element and then subtracting 1.
5124 unsigned Scale = BytesPerElement / TruncBytes;
5125 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1;
5126
5127 // Defer the creation of the bitcast from X to combineExtract,
5128 // which might be able to optimize the extraction.
5129 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8),
5130 VecVT.getStoreSize() / TruncBytes);
5131 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT);
5132 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true);
5133 }
5134 }
5135 }
5136 }
5137 return SDValue();
5138}
5139
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005140SDValue SystemZTargetLowering::combineSIGN_EXTEND(
5141 SDNode *N, DAGCombinerInfo &DCI) const {
5142 // Convert (sext (ashr (shl X, C1), C2)) to
5143 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
5144 // cheap as narrower ones.
5145 SelectionDAG &DAG = DCI.DAG;
5146 SDValue N0 = N->getOperand(0);
5147 EVT VT = N->getValueType(0);
5148 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
5149 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5150 SDValue Inner = N0.getOperand(0);
5151 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
5152 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00005153 unsigned Extra = (VT.getSizeInBits() - N0.getValueSizeInBits());
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005154 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
5155 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
5156 EVT ShiftVT = N0.getOperand(1).getValueType();
5157 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
5158 Inner.getOperand(0));
5159 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
5160 DAG.getConstant(NewShlAmt, SDLoc(Inner),
5161 ShiftVT));
5162 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
5163 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT));
5164 }
5165 }
5166 }
5167 return SDValue();
5168}
5169
5170SDValue SystemZTargetLowering::combineMERGE(
5171 SDNode *N, DAGCombinerInfo &DCI) const {
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005172 SelectionDAG &DAG = DCI.DAG;
5173 unsigned Opcode = N->getOpcode();
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005174 SDValue Op0 = N->getOperand(0);
5175 SDValue Op1 = N->getOperand(1);
5176 if (Op0.getOpcode() == ISD::BITCAST)
5177 Op0 = Op0.getOperand(0);
5178 if (Op0.getOpcode() == SystemZISD::BYTE_MASK &&
5179 cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0) {
5180 // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF
5181 // for v4f32.
5182 if (Op1 == N->getOperand(0))
5183 return Op1;
5184 // (z_merge_? 0, X) -> (z_unpackl_? 0, X).
5185 EVT VT = Op1.getValueType();
5186 unsigned ElemBytes = VT.getVectorElementType().getStoreSize();
5187 if (ElemBytes <= 4) {
5188 Opcode = (Opcode == SystemZISD::MERGE_HIGH ?
5189 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW);
5190 EVT InVT = VT.changeVectorElementTypeToInteger();
5191 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16),
5192 SystemZ::VectorBytes / ElemBytes / 2);
5193 if (VT != InVT) {
5194 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1);
5195 DCI.AddToWorklist(Op1.getNode());
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005196 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005197 SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1);
5198 DCI.AddToWorklist(Op.getNode());
5199 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005200 }
5201 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005202 return SDValue();
5203}
5204
5205SDValue SystemZTargetLowering::combineSTORE(
5206 SDNode *N, DAGCombinerInfo &DCI) const {
5207 SelectionDAG &DAG = DCI.DAG;
5208 auto *SN = cast<StoreSDNode>(N);
5209 auto &Op1 = N->getOperand(1);
5210 EVT MemVT = SN->getMemoryVT();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005211 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better
5212 // for the extraction to be done on a vMiN value, so that we can use VSTE.
5213 // If X has wider elements then convert it to:
5214 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z).
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005215 if (MemVT.isInteger()) {
5216 if (SDValue Value =
5217 combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) {
5218 DCI.AddToWorklist(Value.getNode());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005219
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005220 // Rewrite the store with the new form of stored value.
5221 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value,
5222 SN->getBasePtr(), SN->getMemoryVT(),
5223 SN->getMemOperand());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005224 }
5225 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005226 // Combine STORE (BSWAP) into STRVH/STRV/STRVG
5227 // See comment in combineBSWAP about volatile accesses.
Ulrich Weigand59a01a92017-09-19 20:50:05 +00005228 if (!SN->isTruncatingStore() &&
5229 !SN->isVolatile() &&
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005230 Op1.getOpcode() == ISD::BSWAP &&
5231 Op1.getNode()->hasOneUse() &&
5232 (Op1.getValueType() == MVT::i16 ||
5233 Op1.getValueType() == MVT::i32 ||
5234 Op1.getValueType() == MVT::i64)) {
5235
5236 SDValue BSwapOp = Op1.getOperand(0);
5237
5238 if (BSwapOp.getValueType() == MVT::i16)
5239 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), MVT::i32, BSwapOp);
5240
5241 SDValue Ops[] = {
5242 N->getOperand(0), BSwapOp, N->getOperand(2),
5243 DAG.getValueType(Op1.getValueType())
5244 };
5245
5246 return
5247 DAG.getMemIntrinsicNode(SystemZISD::STRV, SDLoc(N), DAG.getVTList(MVT::Other),
5248 Ops, MemVT, SN->getMemOperand());
5249 }
5250 return SDValue();
5251}
5252
5253SDValue SystemZTargetLowering::combineEXTRACT_VECTOR_ELT(
5254 SDNode *N, DAGCombinerInfo &DCI) const {
Jonas Paulsson56bb0852017-03-31 13:22:59 +00005255
Jonas Paulsson56bb0852017-03-31 13:22:59 +00005256 if (!Subtarget.hasVector())
5257 return SDValue();
5258
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005259 // Try to simplify a vector extraction.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005260 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
5261 SDValue Op0 = N->getOperand(0);
5262 EVT VecVT = Op0.getValueType();
5263 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0,
5264 IndexN->getZExtValue(), DCI, false);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005265 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005266 return SDValue();
5267}
5268
5269SDValue SystemZTargetLowering::combineJOIN_DWORDS(
5270 SDNode *N, DAGCombinerInfo &DCI) const {
5271 SelectionDAG &DAG = DCI.DAG;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005272 // (join_dwords X, X) == (replicate X)
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005273 if (N->getOperand(0) == N->getOperand(1))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005274 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0),
5275 N->getOperand(0));
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005276 return SDValue();
5277}
5278
5279SDValue SystemZTargetLowering::combineFP_ROUND(
5280 SDNode *N, DAGCombinerInfo &DCI) const {
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00005281 // (fpround (extract_vector_elt X 0))
5282 // (fpround (extract_vector_elt X 1)) ->
Ulrich Weigand80b3af72015-05-05 19:27:45 +00005283 // (extract_vector_elt (VROUND X) 0)
5284 // (extract_vector_elt (VROUND X) 1)
5285 //
5286 // This is a special case since the target doesn't really support v2f32s.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005287 SelectionDAG &DAG = DCI.DAG;
5288 SDValue Op0 = N->getOperand(0);
5289 if (N->getValueType(0) == MVT::f32 &&
5290 Op0.hasOneUse() &&
5291 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5292 Op0.getOperand(0).getValueType() == MVT::v2f64 &&
5293 Op0.getOperand(1).getOpcode() == ISD::Constant &&
5294 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) {
5295 SDValue Vec = Op0.getOperand(0);
5296 for (auto *U : Vec->uses()) {
5297 if (U != Op0.getNode() &&
5298 U->hasOneUse() &&
5299 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5300 U->getOperand(0) == Vec &&
5301 U->getOperand(1).getOpcode() == ISD::Constant &&
5302 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) {
5303 SDValue OtherRound = SDValue(*U->use_begin(), 0);
5304 if (OtherRound.getOpcode() == ISD::FP_ROUND &&
5305 OtherRound.getOperand(0) == SDValue(U, 0) &&
5306 OtherRound.getValueType() == MVT::f32) {
5307 SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N),
5308 MVT::v4f32, Vec);
5309 DCI.AddToWorklist(VRound.getNode());
5310 SDValue Extract1 =
5311 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32,
5312 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32));
5313 DCI.AddToWorklist(Extract1.getNode());
5314 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1);
5315 SDValue Extract0 =
5316 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32,
5317 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
5318 return Extract0;
Ulrich Weigand80b3af72015-05-05 19:27:45 +00005319 }
5320 }
5321 }
5322 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005323 return SDValue();
5324}
Bryan Chan28b759c2016-05-16 20:32:22 +00005325
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005326SDValue SystemZTargetLowering::combineBSWAP(
5327 SDNode *N, DAGCombinerInfo &DCI) const {
5328 SelectionDAG &DAG = DCI.DAG;
Bryan Chan28b759c2016-05-16 20:32:22 +00005329 // Combine BSWAP (LOAD) into LRVH/LRV/LRVG
5330 // These loads are allowed to access memory multiple times, and so we must check
5331 // that the loads are not volatile before performing the combine.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005332 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
5333 N->getOperand(0).hasOneUse() &&
5334 (N->getValueType(0) == MVT::i16 || N->getValueType(0) == MVT::i32 ||
5335 N->getValueType(0) == MVT::i64) &&
5336 !cast<LoadSDNode>(N->getOperand(0))->isVolatile()) {
Bryan Chan28b759c2016-05-16 20:32:22 +00005337 SDValue Load = N->getOperand(0);
5338 LoadSDNode *LD = cast<LoadSDNode>(Load);
5339
5340 // Create the byte-swapping load.
5341 SDValue Ops[] = {
5342 LD->getChain(), // Chain
5343 LD->getBasePtr(), // Ptr
5344 DAG.getValueType(N->getValueType(0)) // VT
5345 };
5346 SDValue BSLoad =
5347 DAG.getMemIntrinsicNode(SystemZISD::LRV, SDLoc(N),
5348 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
5349 MVT::i64 : MVT::i32, MVT::Other),
5350 Ops, LD->getMemoryVT(), LD->getMemOperand());
5351
5352 // If this is an i16 load, insert the truncate.
5353 SDValue ResVal = BSLoad;
5354 if (N->getValueType(0) == MVT::i16)
5355 ResVal = DAG.getNode(ISD::TRUNCATE, SDLoc(N), MVT::i16, BSLoad);
5356
5357 // First, combine the bswap away. This makes the value produced by the
5358 // load dead.
5359 DCI.CombineTo(N, ResVal);
5360
5361 // Next, combine the load away, we give it a bogus result value but a real
5362 // chain result. The result value is dead because the bswap is dead.
5363 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
5364
5365 // Return N so it doesn't get rechecked!
5366 return SDValue(N, 0);
5367 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005368 return SDValue();
5369}
Bryan Chan28b759c2016-05-16 20:32:22 +00005370
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005371SDValue SystemZTargetLowering::combineSHIFTROT(
5372 SDNode *N, DAGCombinerInfo &DCI) const {
5373
5374 SelectionDAG &DAG = DCI.DAG;
5375
5376 // Shift/rotate instructions only use the last 6 bits of the second operand
5377 // register. If the second operand is the result of an AND with an immediate
5378 // value that has its last 6 bits set, we can safely remove the AND operation.
Elliot Colp687691a2016-08-18 18:04:26 +00005379 //
5380 // If the AND operation doesn't have the last 6 bits set, we can't remove it
Elliot Colpa4092102016-08-23 14:03:02 +00005381 // entirely, but we can still truncate it to a 16-bit value. This prevents
5382 // us from ending up with a NILL with a signed operand, which will cause the
5383 // instruction printer to abort.
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005384 SDValue N1 = N->getOperand(1);
5385 if (N1.getOpcode() == ISD::AND) {
Elliot Colp687691a2016-08-18 18:04:26 +00005386 SDValue AndMaskOp = N1->getOperand(1);
5387 auto *AndMask = dyn_cast<ConstantSDNode>(AndMaskOp);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005388
5389 // The AND mask is constant
5390 if (AndMask) {
Elliot Colpa4092102016-08-23 14:03:02 +00005391 auto AmtVal = AndMask->getZExtValue();
5392
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005393 // Bottom 6 bits are set
5394 if ((AmtVal & 0x3f) == 0x3f) {
Elliot Colpa4092102016-08-23 14:03:02 +00005395 SDValue AndOp = N1->getOperand(0);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005396
5397 // This is the only use, so remove the node
5398 if (N1.hasOneUse()) {
5399 // Combine the AND away
5400 DCI.CombineTo(N1.getNode(), AndOp);
5401
5402 // Return N so it isn't rechecked
5403 return SDValue(N, 0);
5404
5405 // The node will be reused, so create a new node for this one use
5406 } else {
5407 SDValue Replace = DAG.getNode(N->getOpcode(), SDLoc(N),
5408 N->getValueType(0), N->getOperand(0),
5409 AndOp);
5410 DCI.AddToWorklist(Replace.getNode());
5411
5412 return Replace;
5413 }
Elliot Colp687691a2016-08-18 18:04:26 +00005414
Elliot Colpa4092102016-08-23 14:03:02 +00005415 // We can't remove the AND, but we can use NILL here (normally we would
5416 // use NILF). Only keep the last 16 bits of the mask. The actual
5417 // transformation will be handled by .td definitions.
5418 } else if (AmtVal >> 16 != 0) {
5419 SDValue AndOp = N1->getOperand(0);
Elliot Colp687691a2016-08-18 18:04:26 +00005420
Elliot Colpa4092102016-08-23 14:03:02 +00005421 auto NewMask = DAG.getConstant(AndMask->getZExtValue() & 0x0000ffff,
5422 SDLoc(AndMaskOp),
5423 AndMaskOp.getValueType());
Elliot Colp687691a2016-08-18 18:04:26 +00005424
Elliot Colpa4092102016-08-23 14:03:02 +00005425 auto NewAnd = DAG.getNode(N1.getOpcode(), SDLoc(N1), N1.getValueType(),
5426 AndOp, NewMask);
Elliot Colp687691a2016-08-18 18:04:26 +00005427
Elliot Colpa4092102016-08-23 14:03:02 +00005428 SDValue Replace = DAG.getNode(N->getOpcode(), SDLoc(N),
5429 N->getValueType(0), N->getOperand(0),
5430 NewAnd);
5431 DCI.AddToWorklist(Replace.getNode());
Elliot Colp687691a2016-08-18 18:04:26 +00005432
Elliot Colpa4092102016-08-23 14:03:02 +00005433 return Replace;
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005434 }
5435 }
5436 }
5437
5438 return SDValue();
5439}
5440
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005441SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
5442 DAGCombinerInfo &DCI) const {
5443 switch(N->getOpcode()) {
5444 default: break;
5445 case ISD::SIGN_EXTEND: return combineSIGN_EXTEND(N, DCI);
5446 case SystemZISD::MERGE_HIGH:
5447 case SystemZISD::MERGE_LOW: return combineMERGE(N, DCI);
5448 case ISD::STORE: return combineSTORE(N, DCI);
5449 case ISD::EXTRACT_VECTOR_ELT: return combineEXTRACT_VECTOR_ELT(N, DCI);
5450 case SystemZISD::JOIN_DWORDS: return combineJOIN_DWORDS(N, DCI);
5451 case ISD::FP_ROUND: return combineFP_ROUND(N, DCI);
5452 case ISD::BSWAP: return combineBSWAP(N, DCI);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005453 case ISD::SHL:
5454 case ISD::SRA:
5455 case ISD::SRL:
5456 case ISD::ROTL: return combineSHIFTROT(N, DCI);
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005457 }
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005458
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005459 return SDValue();
5460}
5461
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005462//===----------------------------------------------------------------------===//
5463// Custom insertion
5464//===----------------------------------------------------------------------===//
5465
5466// Create a new basic block after MBB.
5467static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
5468 MachineFunction &MF = *MBB->getParent();
5469 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00005470 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005471 return NewMBB;
5472}
5473
Richard Sandifordbe133a82013-08-28 09:01:51 +00005474// Split MBB after MI and return the new block (the one that contains
5475// instructions after MI).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005476static MachineBasicBlock *splitBlockAfter(MachineBasicBlock::iterator MI,
Richard Sandifordbe133a82013-08-28 09:01:51 +00005477 MachineBasicBlock *MBB) {
5478 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
5479 NewMBB->splice(NewMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00005480 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Richard Sandifordbe133a82013-08-28 09:01:51 +00005481 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
5482 return NewMBB;
5483}
5484
Richard Sandiford5e318f02013-08-27 09:54:29 +00005485// Split MBB before MI and return the new block (the one that contains MI).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005486static MachineBasicBlock *splitBlockBefore(MachineBasicBlock::iterator MI,
Richard Sandiford5e318f02013-08-27 09:54:29 +00005487 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005488 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005489 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005490 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
5491 return NewMBB;
5492}
5493
Richard Sandiford5e318f02013-08-27 09:54:29 +00005494// Force base value Base into a register before MI. Return the register.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005495static unsigned forceReg(MachineInstr &MI, MachineOperand &Base,
Richard Sandiford5e318f02013-08-27 09:54:29 +00005496 const SystemZInstrInfo *TII) {
5497 if (Base.isReg())
5498 return Base.getReg();
5499
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005500 MachineBasicBlock *MBB = MI.getParent();
Richard Sandiford5e318f02013-08-27 09:54:29 +00005501 MachineFunction &MF = *MBB->getParent();
5502 MachineRegisterInfo &MRI = MF.getRegInfo();
5503
5504 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005505 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LA), Reg)
Diana Picus116bbab2017-01-13 09:58:52 +00005506 .add(Base)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005507 .addImm(0)
5508 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005509 return Reg;
5510}
5511
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005512// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
5513MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005514SystemZTargetLowering::emitSelect(MachineInstr &MI,
Ulrich Weigand524f2762016-11-28 13:34:08 +00005515 MachineBasicBlock *MBB,
5516 unsigned LOCROpcode) const {
Eric Christophera6734172015-01-31 00:06:45 +00005517 const SystemZInstrInfo *TII =
5518 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005519
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005520 unsigned DestReg = MI.getOperand(0).getReg();
5521 unsigned TrueReg = MI.getOperand(1).getReg();
5522 unsigned FalseReg = MI.getOperand(2).getReg();
5523 unsigned CCValid = MI.getOperand(3).getImm();
5524 unsigned CCMask = MI.getOperand(4).getImm();
5525 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005526
Ulrich Weigand524f2762016-11-28 13:34:08 +00005527 // Use LOCROpcode if possible.
5528 if (LOCROpcode && Subtarget.hasLoadStoreOnCond()) {
5529 BuildMI(*MBB, MI, DL, TII->get(LOCROpcode), DestReg)
5530 .addReg(FalseReg).addReg(TrueReg)
5531 .addImm(CCValid).addImm(CCMask);
5532 MI.eraseFromParent();
5533 return MBB;
5534 }
5535
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005536 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005537 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005538 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5539
5540 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00005541 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005542 // # fallthrough to FalseMBB
5543 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005544 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5545 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005546 MBB->addSuccessor(JoinMBB);
5547 MBB->addSuccessor(FalseMBB);
5548
5549 // FalseMBB:
5550 // # fallthrough to JoinMBB
5551 MBB = FalseMBB;
5552 MBB->addSuccessor(JoinMBB);
5553
5554 // JoinMBB:
5555 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
5556 // ...
5557 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005558 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005559 .addReg(TrueReg).addMBB(StartMBB)
5560 .addReg(FalseReg).addMBB(FalseMBB);
5561
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005562 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005563 return JoinMBB;
5564}
5565
Richard Sandifordb86a8342013-06-27 09:27:40 +00005566// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
5567// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005568// happen when the condition is false rather than true. If a STORE ON
5569// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005570MachineBasicBlock *SystemZTargetLowering::emitCondStore(MachineInstr &MI,
5571 MachineBasicBlock *MBB,
5572 unsigned StoreOpcode,
5573 unsigned STOCOpcode,
5574 bool Invert) const {
Eric Christophera6734172015-01-31 00:06:45 +00005575 const SystemZInstrInfo *TII =
5576 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordb86a8342013-06-27 09:27:40 +00005577
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005578 unsigned SrcReg = MI.getOperand(0).getReg();
5579 MachineOperand Base = MI.getOperand(1);
5580 int64_t Disp = MI.getOperand(2).getImm();
5581 unsigned IndexReg = MI.getOperand(3).getReg();
5582 unsigned CCValid = MI.getOperand(4).getImm();
5583 unsigned CCMask = MI.getOperand(5).getImm();
5584 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005585
5586 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
5587
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005588 // Use STOCOpcode if possible. We could use different store patterns in
5589 // order to avoid matching the index register, but the performance trade-offs
5590 // might be more complicated in that case.
Eric Christopher93bf97c2014-06-27 07:38:01 +00005591 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005592 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005593 CCMask ^= CCValid;
Jonas Paulssonae8d22c2017-06-07 14:08:34 +00005594
5595 // ISel pattern matching also adds a load memory operand of the same
5596 // address, so take special care to find the storing memory operand.
5597 MachineMemOperand *MMO = nullptr;
5598 for (auto *I : MI.memoperands())
5599 if (I->isStore()) {
5600 MMO = I;
5601 break;
5602 }
5603
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005604 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Jonas Paulssonae8d22c2017-06-07 14:08:34 +00005605 .addReg(SrcReg)
5606 .add(Base)
5607 .addImm(Disp)
5608 .addImm(CCValid)
5609 .addImm(CCMask)
5610 .addMemOperand(MMO);
5611
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005612 MI.eraseFromParent();
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005613 return MBB;
5614 }
5615
Richard Sandifordb86a8342013-06-27 09:27:40 +00005616 // Get the condition needed to branch around the store.
5617 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005618 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00005619
5620 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005621 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005622 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5623
5624 // StartMBB:
5625 // BRC CCMask, JoinMBB
5626 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00005627 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005628 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5629 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005630 MBB->addSuccessor(JoinMBB);
5631 MBB->addSuccessor(FalseMBB);
5632
5633 // FalseMBB:
5634 // store %SrcReg, %Disp(%Index,%Base)
5635 // # fallthrough to JoinMBB
5636 MBB = FalseMBB;
5637 BuildMI(MBB, DL, TII->get(StoreOpcode))
Diana Picus116bbab2017-01-13 09:58:52 +00005638 .addReg(SrcReg)
5639 .add(Base)
5640 .addImm(Disp)
5641 .addReg(IndexReg);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005642 MBB->addSuccessor(JoinMBB);
5643
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005644 MI.eraseFromParent();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005645 return JoinMBB;
5646}
5647
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005648// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
5649// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
5650// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
5651// BitSize is the width of the field in bits, or 0 if this is a partword
5652// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
5653// is one of the operands. Invert says whether the field should be
5654// inverted after performing BinOpcode (e.g. for NAND).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005655MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadBinary(
5656 MachineInstr &MI, MachineBasicBlock *MBB, unsigned BinOpcode,
5657 unsigned BitSize, bool Invert) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005658 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005659 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005660 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005661 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005662 bool IsSubWord = (BitSize < 32);
5663
5664 // Extract the operands. Base can be a register or a frame index.
5665 // Src2 can be a register or immediate.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005666 unsigned Dest = MI.getOperand(0).getReg();
5667 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5668 int64_t Disp = MI.getOperand(2).getImm();
5669 MachineOperand Src2 = earlyUseOperand(MI.getOperand(3));
5670 unsigned BitShift = (IsSubWord ? MI.getOperand(4).getReg() : 0);
5671 unsigned NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : 0);
5672 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005673 if (IsSubWord)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005674 BitSize = MI.getOperand(6).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005675
5676 // Subword operations use 32-bit registers.
5677 const TargetRegisterClass *RC = (BitSize <= 32 ?
5678 &SystemZ::GR32BitRegClass :
5679 &SystemZ::GR64BitRegClass);
5680 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5681 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5682
5683 // Get the right opcodes for the displacement.
5684 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5685 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5686 assert(LOpcode && CSOpcode && "Displacement out of range");
5687
5688 // Create virtual registers for temporary results.
5689 unsigned OrigVal = MRI.createVirtualRegister(RC);
5690 unsigned OldVal = MRI.createVirtualRegister(RC);
5691 unsigned NewVal = (BinOpcode || IsSubWord ?
5692 MRI.createVirtualRegister(RC) : Src2.getReg());
5693 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5694 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5695
5696 // Insert a basic block for the main loop.
5697 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005698 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005699 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5700
5701 // StartMBB:
5702 // ...
5703 // %OrigVal = L Disp(%Base)
5704 // # fall through to LoopMMB
5705 MBB = StartMBB;
Diana Picus116bbab2017-01-13 09:58:52 +00005706 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005707 MBB->addSuccessor(LoopMBB);
5708
5709 // LoopMBB:
5710 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
5711 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5712 // %RotatedNewVal = OP %RotatedOldVal, %Src2
5713 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5714 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5715 // JNE LoopMBB
5716 // # fall through to DoneMMB
5717 MBB = LoopMBB;
5718 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5719 .addReg(OrigVal).addMBB(StartMBB)
5720 .addReg(Dest).addMBB(LoopMBB);
5721 if (IsSubWord)
5722 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5723 .addReg(OldVal).addReg(BitShift).addImm(0);
5724 if (Invert) {
5725 // Perform the operation normally and then invert every bit of the field.
5726 unsigned Tmp = MRI.createVirtualRegister(RC);
Diana Picus116bbab2017-01-13 09:58:52 +00005727 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp).addReg(RotatedOldVal).add(Src2);
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005728 if (BitSize <= 32)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005729 // XILF with the upper BitSize bits set.
Richard Sandiford652784e2013-09-25 11:11:53 +00005730 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005731 .addReg(Tmp).addImm(-1U << (32 - BitSize));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005732 else {
5733 // Use LCGR and add -1 to the result, which is more compact than
5734 // an XILF, XILH pair.
5735 unsigned Tmp2 = MRI.createVirtualRegister(RC);
5736 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
5737 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
5738 .addReg(Tmp2).addImm(-1);
5739 }
5740 } else if (BinOpcode)
5741 // A simply binary operation.
5742 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
Diana Picus116bbab2017-01-13 09:58:52 +00005743 .addReg(RotatedOldVal)
5744 .add(Src2);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005745 else if (IsSubWord)
5746 // Use RISBG to rotate Src2 into position and use it to replace the
5747 // field in RotatedOldVal.
5748 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
5749 .addReg(RotatedOldVal).addReg(Src2.getReg())
5750 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
5751 if (IsSubWord)
5752 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5753 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5754 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
Diana Picus116bbab2017-01-13 09:58:52 +00005755 .addReg(OldVal)
5756 .addReg(NewVal)
5757 .add(Base)
5758 .addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005759 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5760 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005761 MBB->addSuccessor(LoopMBB);
5762 MBB->addSuccessor(DoneMBB);
5763
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005764 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005765 return DoneMBB;
5766}
5767
5768// Implement EmitInstrWithCustomInserter for pseudo
5769// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
5770// instruction that should be used to compare the current field with the
5771// minimum or maximum value. KeepOldMask is the BRC condition-code mask
5772// for when the current field should be kept. BitSize is the width of
5773// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005774MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadMinMax(
5775 MachineInstr &MI, MachineBasicBlock *MBB, unsigned CompareOpcode,
5776 unsigned KeepOldMask, unsigned BitSize) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005777 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005778 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005779 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005780 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005781 bool IsSubWord = (BitSize < 32);
5782
5783 // Extract the operands. Base can be a register or a frame index.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005784 unsigned Dest = MI.getOperand(0).getReg();
5785 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5786 int64_t Disp = MI.getOperand(2).getImm();
5787 unsigned Src2 = MI.getOperand(3).getReg();
5788 unsigned BitShift = (IsSubWord ? MI.getOperand(4).getReg() : 0);
5789 unsigned NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : 0);
5790 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005791 if (IsSubWord)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005792 BitSize = MI.getOperand(6).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005793
5794 // Subword operations use 32-bit registers.
5795 const TargetRegisterClass *RC = (BitSize <= 32 ?
5796 &SystemZ::GR32BitRegClass :
5797 &SystemZ::GR64BitRegClass);
5798 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5799 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5800
5801 // Get the right opcodes for the displacement.
5802 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5803 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5804 assert(LOpcode && CSOpcode && "Displacement out of range");
5805
5806 // Create virtual registers for temporary results.
5807 unsigned OrigVal = MRI.createVirtualRegister(RC);
5808 unsigned OldVal = MRI.createVirtualRegister(RC);
5809 unsigned NewVal = MRI.createVirtualRegister(RC);
5810 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5811 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
5812 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5813
5814 // Insert 3 basic blocks for the loop.
5815 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005816 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005817 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5818 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
5819 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
5820
5821 // StartMBB:
5822 // ...
5823 // %OrigVal = L Disp(%Base)
5824 // # fall through to LoopMMB
5825 MBB = StartMBB;
Diana Picus116bbab2017-01-13 09:58:52 +00005826 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005827 MBB->addSuccessor(LoopMBB);
5828
5829 // LoopMBB:
5830 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
5831 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5832 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00005833 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005834 MBB = LoopMBB;
5835 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5836 .addReg(OrigVal).addMBB(StartMBB)
5837 .addReg(Dest).addMBB(UpdateMBB);
5838 if (IsSubWord)
5839 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5840 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005841 BuildMI(MBB, DL, TII->get(CompareOpcode))
5842 .addReg(RotatedOldVal).addReg(Src2);
5843 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005844 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005845 MBB->addSuccessor(UpdateMBB);
5846 MBB->addSuccessor(UseAltMBB);
5847
5848 // UseAltMBB:
5849 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
5850 // # fall through to UpdateMMB
5851 MBB = UseAltMBB;
5852 if (IsSubWord)
5853 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
5854 .addReg(RotatedOldVal).addReg(Src2)
5855 .addImm(32).addImm(31 + BitSize).addImm(0);
5856 MBB->addSuccessor(UpdateMBB);
5857
5858 // UpdateMBB:
5859 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
5860 // [ %RotatedAltVal, UseAltMBB ]
5861 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5862 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5863 // JNE LoopMBB
5864 // # fall through to DoneMMB
5865 MBB = UpdateMBB;
5866 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
5867 .addReg(RotatedOldVal).addMBB(LoopMBB)
5868 .addReg(RotatedAltVal).addMBB(UseAltMBB);
5869 if (IsSubWord)
5870 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5871 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5872 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
Diana Picus116bbab2017-01-13 09:58:52 +00005873 .addReg(OldVal)
5874 .addReg(NewVal)
5875 .add(Base)
5876 .addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005877 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5878 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005879 MBB->addSuccessor(LoopMBB);
5880 MBB->addSuccessor(DoneMBB);
5881
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005882 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005883 return DoneMBB;
5884}
5885
5886// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
5887// instruction MI.
5888MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005889SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005890 MachineBasicBlock *MBB) const {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00005891
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005892 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005893 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005894 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005895 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005896
5897 // Extract the operands. Base can be a register or a frame index.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005898 unsigned Dest = MI.getOperand(0).getReg();
5899 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5900 int64_t Disp = MI.getOperand(2).getImm();
5901 unsigned OrigCmpVal = MI.getOperand(3).getReg();
5902 unsigned OrigSwapVal = MI.getOperand(4).getReg();
5903 unsigned BitShift = MI.getOperand(5).getReg();
5904 unsigned NegBitShift = MI.getOperand(6).getReg();
5905 int64_t BitSize = MI.getOperand(7).getImm();
5906 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005907
5908 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
5909
5910 // Get the right opcodes for the displacement.
5911 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
5912 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
5913 assert(LOpcode && CSOpcode && "Displacement out of range");
5914
5915 // Create virtual registers for temporary results.
5916 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
5917 unsigned OldVal = MRI.createVirtualRegister(RC);
5918 unsigned CmpVal = MRI.createVirtualRegister(RC);
5919 unsigned SwapVal = MRI.createVirtualRegister(RC);
5920 unsigned StoreVal = MRI.createVirtualRegister(RC);
5921 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
5922 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
5923 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
5924
5925 // Insert 2 basic blocks for the loop.
5926 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005927 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005928 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5929 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
5930
5931 // StartMBB:
5932 // ...
5933 // %OrigOldVal = L Disp(%Base)
5934 // # fall through to LoopMMB
5935 MBB = StartMBB;
5936 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
Diana Picus116bbab2017-01-13 09:58:52 +00005937 .add(Base)
5938 .addImm(Disp)
5939 .addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005940 MBB->addSuccessor(LoopMBB);
5941
5942 // LoopMBB:
5943 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
5944 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
5945 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
5946 // %Dest = RLL %OldVal, BitSize(%BitShift)
5947 // ^^ The low BitSize bits contain the field
5948 // of interest.
5949 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
5950 // ^^ Replace the upper 32-BitSize bits of the
5951 // comparison value with those that we loaded,
5952 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005953 // CR %Dest, %RetryCmpVal
5954 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005955 // # Fall through to SetMBB
5956 MBB = LoopMBB;
5957 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5958 .addReg(OrigOldVal).addMBB(StartMBB)
5959 .addReg(RetryOldVal).addMBB(SetMBB);
5960 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
5961 .addReg(OrigCmpVal).addMBB(StartMBB)
5962 .addReg(RetryCmpVal).addMBB(SetMBB);
5963 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
5964 .addReg(OrigSwapVal).addMBB(StartMBB)
5965 .addReg(RetrySwapVal).addMBB(SetMBB);
5966 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
5967 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
5968 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
5969 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005970 BuildMI(MBB, DL, TII->get(SystemZ::CR))
5971 .addReg(Dest).addReg(RetryCmpVal);
5972 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005973 .addImm(SystemZ::CCMASK_ICMP)
5974 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005975 MBB->addSuccessor(DoneMBB);
5976 MBB->addSuccessor(SetMBB);
5977
5978 // SetMBB:
5979 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
5980 // ^^ Replace the upper 32-BitSize bits of the new
5981 // value with those that we loaded.
5982 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
5983 // ^^ Rotate the new field to its proper position.
5984 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
5985 // JNE LoopMBB
5986 // # fall through to ExitMMB
5987 MBB = SetMBB;
5988 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
5989 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
5990 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
5991 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
5992 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
Diana Picus116bbab2017-01-13 09:58:52 +00005993 .addReg(OldVal)
5994 .addReg(StoreVal)
5995 .add(Base)
5996 .addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005997 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5998 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005999 MBB->addSuccessor(LoopMBB);
6000 MBB->addSuccessor(DoneMBB);
6001
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006002 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006003 return DoneMBB;
6004}
6005
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00006006// Emit a move from two GR64s to a GR128.
6007MachineBasicBlock *
6008SystemZTargetLowering::emitPair128(MachineInstr &MI,
6009 MachineBasicBlock *MBB) const {
6010 MachineFunction &MF = *MBB->getParent();
6011 const SystemZInstrInfo *TII =
6012 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
6013 MachineRegisterInfo &MRI = MF.getRegInfo();
6014 DebugLoc DL = MI.getDebugLoc();
6015
6016 unsigned Dest = MI.getOperand(0).getReg();
6017 unsigned Hi = MI.getOperand(1).getReg();
6018 unsigned Lo = MI.getOperand(2).getReg();
6019 unsigned Tmp1 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6020 unsigned Tmp2 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6021
6022 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Tmp1);
6023 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Tmp2)
6024 .addReg(Tmp1).addReg(Hi).addImm(SystemZ::subreg_h64);
6025 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
6026 .addReg(Tmp2).addReg(Lo).addImm(SystemZ::subreg_l64);
6027
6028 MI.eraseFromParent();
6029 return MBB;
6030}
6031
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006032// Emit an extension from a GR64 to a GR128. ClearEven is true
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006033// if the high register of the GR128 value must be cleared or false if
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006034// it's "don't care".
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006035MachineBasicBlock *SystemZTargetLowering::emitExt128(MachineInstr &MI,
6036 MachineBasicBlock *MBB,
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006037 bool ClearEven) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006038 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00006039 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00006040 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006041 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006042 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006043
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006044 unsigned Dest = MI.getOperand(0).getReg();
6045 unsigned Src = MI.getOperand(1).getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006046 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6047
6048 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
6049 if (ClearEven) {
6050 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6051 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
6052
6053 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
6054 .addImm(0);
6055 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
Richard Sandiford87a44362013-09-30 10:28:35 +00006056 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006057 In128 = NewIn128;
6058 }
6059 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006060 .addReg(In128).addReg(Src).addImm(SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006061
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006062 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006063 return MBB;
6064}
6065
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006066MachineBasicBlock *SystemZTargetLowering::emitMemMemWrapper(
6067 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +00006068 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00006069 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00006070 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandiford5e318f02013-08-27 09:54:29 +00006071 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006072 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordd131ff82013-07-08 09:35:23 +00006073
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006074 MachineOperand DestBase = earlyUseOperand(MI.getOperand(0));
6075 uint64_t DestDisp = MI.getOperand(1).getImm();
6076 MachineOperand SrcBase = earlyUseOperand(MI.getOperand(2));
6077 uint64_t SrcDisp = MI.getOperand(3).getImm();
6078 uint64_t Length = MI.getOperand(4).getImm();
Richard Sandifordd131ff82013-07-08 09:35:23 +00006079
Richard Sandifordbe133a82013-08-28 09:01:51 +00006080 // When generating more than one CLC, all but the last will need to
6081 // branch to the end when a difference is found.
6082 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
Craig Topper062a2ba2014-04-25 05:30:21 +00006083 splitBlockAfter(MI, MBB) : nullptr);
Richard Sandifordbe133a82013-08-28 09:01:51 +00006084
Richard Sandiford5e318f02013-08-27 09:54:29 +00006085 // Check for the loop form, in which operand 5 is the trip count.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006086 if (MI.getNumExplicitOperands() > 5) {
Richard Sandiford5e318f02013-08-27 09:54:29 +00006087 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
6088
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006089 uint64_t StartCountReg = MI.getOperand(5).getReg();
Richard Sandiford5e318f02013-08-27 09:54:29 +00006090 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
6091 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
6092 forceReg(MI, DestBase, TII));
6093
6094 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
6095 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
6096 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
6097 MRI.createVirtualRegister(RC));
6098 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
6099 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
6100 MRI.createVirtualRegister(RC));
6101
6102 RC = &SystemZ::GR64BitRegClass;
6103 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
6104 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
6105
6106 MachineBasicBlock *StartMBB = MBB;
6107 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
6108 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00006109 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006110
6111 // StartMBB:
6112 // # fall through to LoopMMB
6113 MBB->addSuccessor(LoopMBB);
6114
6115 // LoopMBB:
6116 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00006117 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00006118 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00006119 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00006120 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00006121 // [ %NextCountReg, NextMBB ]
6122 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00006123 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00006124 // ( JLH EndMBB )
6125 //
6126 // The prefetch is used only for MVC. The JLH is used only for CLC.
6127 MBB = LoopMBB;
6128
6129 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
6130 .addReg(StartDestReg).addMBB(StartMBB)
6131 .addReg(NextDestReg).addMBB(NextMBB);
6132 if (!HaveSingleBase)
6133 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
6134 .addReg(StartSrcReg).addMBB(StartMBB)
6135 .addReg(NextSrcReg).addMBB(NextMBB);
6136 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
6137 .addReg(StartCountReg).addMBB(StartMBB)
6138 .addReg(NextCountReg).addMBB(NextMBB);
6139 if (Opcode == SystemZ::MVC)
6140 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
6141 .addImm(SystemZ::PFD_WRITE)
6142 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
6143 BuildMI(MBB, DL, TII->get(Opcode))
6144 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
6145 .addReg(ThisSrcReg).addImm(SrcDisp);
6146 if (EndMBB) {
6147 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6148 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
6149 .addMBB(EndMBB);
6150 MBB->addSuccessor(EndMBB);
6151 MBB->addSuccessor(NextMBB);
6152 }
6153
6154 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00006155 // %NextDestReg = LA 256(%ThisDestReg)
6156 // %NextSrcReg = LA 256(%ThisSrcReg)
6157 // %NextCountReg = AGHI %ThisCountReg, -1
6158 // CGHI %NextCountReg, 0
6159 // JLH LoopMBB
6160 // # fall through to DoneMMB
6161 //
6162 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00006163 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00006164
Richard Sandiford5e318f02013-08-27 09:54:29 +00006165 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
6166 .addReg(ThisDestReg).addImm(256).addReg(0);
6167 if (!HaveSingleBase)
6168 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
6169 .addReg(ThisSrcReg).addImm(256).addReg(0);
6170 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
6171 .addReg(ThisCountReg).addImm(-1);
6172 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
6173 .addReg(NextCountReg).addImm(0);
6174 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6175 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
6176 .addMBB(LoopMBB);
6177 MBB->addSuccessor(LoopMBB);
6178 MBB->addSuccessor(DoneMBB);
6179
6180 DestBase = MachineOperand::CreateReg(NextDestReg, false);
6181 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
6182 Length &= 255;
6183 MBB = DoneMBB;
6184 }
6185 // Handle any remaining bytes with straight-line code.
6186 while (Length > 0) {
6187 uint64_t ThisLength = std::min(Length, uint64_t(256));
6188 // The previous iteration might have created out-of-range displacements.
6189 // Apply them using LAY if so.
6190 if (!isUInt<12>(DestDisp)) {
6191 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006192 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg)
Diana Picus116bbab2017-01-13 09:58:52 +00006193 .add(DestBase)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006194 .addImm(DestDisp)
6195 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006196 DestBase = MachineOperand::CreateReg(Reg, false);
6197 DestDisp = 0;
6198 }
6199 if (!isUInt<12>(SrcDisp)) {
6200 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006201 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg)
Diana Picus116bbab2017-01-13 09:58:52 +00006202 .add(SrcBase)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006203 .addImm(SrcDisp)
6204 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006205 SrcBase = MachineOperand::CreateReg(Reg, false);
6206 SrcDisp = 0;
6207 }
6208 BuildMI(*MBB, MI, DL, TII->get(Opcode))
Diana Picus116bbab2017-01-13 09:58:52 +00006209 .add(DestBase)
6210 .addImm(DestDisp)
6211 .addImm(ThisLength)
6212 .add(SrcBase)
Jonas Paulssonae8d22c2017-06-07 14:08:34 +00006213 .addImm(SrcDisp)
6214 ->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Richard Sandiford5e318f02013-08-27 09:54:29 +00006215 DestDisp += ThisLength;
6216 SrcDisp += ThisLength;
6217 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00006218 // If there's another CLC to go, branch to the end if a difference
6219 // was found.
6220 if (EndMBB && Length > 0) {
6221 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
6222 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6223 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
6224 .addMBB(EndMBB);
6225 MBB->addSuccessor(EndMBB);
6226 MBB->addSuccessor(NextMBB);
6227 MBB = NextMBB;
6228 }
6229 }
6230 if (EndMBB) {
6231 MBB->addSuccessor(EndMBB);
6232 MBB = EndMBB;
6233 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006234 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00006235
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006236 MI.eraseFromParent();
Richard Sandifordd131ff82013-07-08 09:35:23 +00006237 return MBB;
6238}
6239
Richard Sandifordca232712013-08-16 11:21:54 +00006240// Decompose string pseudo-instruction MI into a loop that continually performs
6241// Opcode until CC != 3.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006242MachineBasicBlock *SystemZTargetLowering::emitStringWrapper(
6243 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Richard Sandifordca232712013-08-16 11:21:54 +00006244 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00006245 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00006246 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordca232712013-08-16 11:21:54 +00006247 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006248 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordca232712013-08-16 11:21:54 +00006249
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006250 uint64_t End1Reg = MI.getOperand(0).getReg();
6251 uint64_t Start1Reg = MI.getOperand(1).getReg();
6252 uint64_t Start2Reg = MI.getOperand(2).getReg();
6253 uint64_t CharReg = MI.getOperand(3).getReg();
Richard Sandifordca232712013-08-16 11:21:54 +00006254
6255 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
6256 uint64_t This1Reg = MRI.createVirtualRegister(RC);
6257 uint64_t This2Reg = MRI.createVirtualRegister(RC);
6258 uint64_t End2Reg = MRI.createVirtualRegister(RC);
6259
6260 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00006261 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00006262 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
6263
6264 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00006265 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00006266 MBB->addSuccessor(LoopMBB);
6267
6268 // LoopMBB:
6269 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
6270 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford7789b082013-09-30 08:48:38 +00006271 // R0L = %CharReg
6272 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
Richard Sandifordca232712013-08-16 11:21:54 +00006273 // JO LoopMBB
6274 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00006275 //
Richard Sandiford7789b082013-09-30 08:48:38 +00006276 // The load of R0L can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00006277 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00006278
6279 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
6280 .addReg(Start1Reg).addMBB(StartMBB)
6281 .addReg(End1Reg).addMBB(LoopMBB);
6282 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
6283 .addReg(Start2Reg).addMBB(StartMBB)
6284 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford7789b082013-09-30 08:48:38 +00006285 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00006286 BuildMI(MBB, DL, TII->get(Opcode))
6287 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
6288 .addReg(This1Reg).addReg(This2Reg);
6289 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6290 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
6291 MBB->addSuccessor(LoopMBB);
6292 MBB->addSuccessor(DoneMBB);
6293
6294 DoneMBB->addLiveIn(SystemZ::CC);
6295
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006296 MI.eraseFromParent();
Richard Sandifordca232712013-08-16 11:21:54 +00006297 return DoneMBB;
6298}
6299
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006300// Update TBEGIN instruction with final opcode and register clobbers.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006301MachineBasicBlock *SystemZTargetLowering::emitTransactionBegin(
6302 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode,
6303 bool NoFloat) const {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006304 MachineFunction &MF = *MBB->getParent();
6305 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
6306 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
6307
6308 // Update opcode.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006309 MI.setDesc(TII->get(Opcode));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006310
6311 // We cannot handle a TBEGIN that clobbers the stack or frame pointer.
6312 // Make sure to add the corresponding GRSM bits if they are missing.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006313 uint64_t Control = MI.getOperand(2).getImm();
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006314 static const unsigned GPRControlBit[16] = {
6315 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000,
6316 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100
6317 };
6318 Control |= GPRControlBit[15];
6319 if (TFI->hasFP(MF))
6320 Control |= GPRControlBit[11];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006321 MI.getOperand(2).setImm(Control);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006322
6323 // Add GPR clobbers.
6324 for (int I = 0; I < 16; I++) {
6325 if ((Control & GPRControlBit[I]) == 0) {
6326 unsigned Reg = SystemZMC::GR64Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006327 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006328 }
6329 }
6330
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006331 // Add FPR/VR clobbers.
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006332 if (!NoFloat && (Control & 4) != 0) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006333 if (Subtarget.hasVector()) {
6334 for (int I = 0; I < 32; I++) {
6335 unsigned Reg = SystemZMC::VR128Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006336 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006337 }
6338 } else {
6339 for (int I = 0; I < 16; I++) {
6340 unsigned Reg = SystemZMC::FP64Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006341 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006342 }
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006343 }
6344 }
6345
6346 return MBB;
6347}
6348
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006349MachineBasicBlock *SystemZTargetLowering::emitLoadAndTestCmp0(
6350 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006351 MachineFunction &MF = *MBB->getParent();
6352 MachineRegisterInfo *MRI = &MF.getRegInfo();
6353 const SystemZInstrInfo *TII =
6354 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006355 DebugLoc DL = MI.getDebugLoc();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006356
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006357 unsigned SrcReg = MI.getOperand(0).getReg();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006358
6359 // Create new virtual register of the same class as source.
6360 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
6361 unsigned DstReg = MRI->createVirtualRegister(RC);
6362
6363 // Replace pseudo with a normal load-and-test that models the def as
6364 // well.
6365 BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg)
6366 .addReg(SrcReg);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006367 MI.eraseFromParent();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006368
6369 return MBB;
6370}
6371
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006372MachineBasicBlock *SystemZTargetLowering::EmitInstrWithCustomInserter(
6373 MachineInstr &MI, MachineBasicBlock *MBB) const {
6374 switch (MI.getOpcode()) {
Richard Sandiford7c5c0ea2013-10-01 13:10:16 +00006375 case SystemZ::Select32Mux:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006376 return emitSelect(MI, MBB,
6377 Subtarget.hasLoadStoreOnCond2()? SystemZ::LOCRMux : 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006378 case SystemZ::Select32:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006379 return emitSelect(MI, MBB, SystemZ::LOCR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006380 case SystemZ::Select64:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006381 return emitSelect(MI, MBB, SystemZ::LOCGR);
6382 case SystemZ::SelectF32:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006383 case SystemZ::SelectF64:
6384 case SystemZ::SelectF128:
Ulrich Weigandf2968d52017-07-17 17:44:20 +00006385 case SystemZ::SelectVR128:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006386 return emitSelect(MI, MBB, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006387
Richard Sandiford2896d042013-10-01 14:33:55 +00006388 case SystemZ::CondStore8Mux:
6389 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
6390 case SystemZ::CondStore8MuxInv:
6391 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
6392 case SystemZ::CondStore16Mux:
6393 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
6394 case SystemZ::CondStore16MuxInv:
6395 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
Ulrich Weigand524f2762016-11-28 13:34:08 +00006396 case SystemZ::CondStore32Mux:
6397 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, false);
6398 case SystemZ::CondStore32MuxInv:
6399 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006400 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006401 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006402 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006403 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006404 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006405 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006406 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006407 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006408 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006409 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006410 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006411 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006412 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006413 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006414 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006415 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006416 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006417 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006418 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006419 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006420 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006421 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006422 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006423 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006424
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00006425 case SystemZ::PAIR128:
6426 return emitPair128(MI, MBB);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006427 case SystemZ::AEXT128:
6428 return emitExt128(MI, MBB, false);
6429 case SystemZ::ZEXT128:
6430 return emitExt128(MI, MBB, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006431
6432 case SystemZ::ATOMIC_SWAPW:
6433 return emitAtomicLoadBinary(MI, MBB, 0, 0);
6434 case SystemZ::ATOMIC_SWAP_32:
6435 return emitAtomicLoadBinary(MI, MBB, 0, 32);
6436 case SystemZ::ATOMIC_SWAP_64:
6437 return emitAtomicLoadBinary(MI, MBB, 0, 64);
6438
6439 case SystemZ::ATOMIC_LOADW_AR:
6440 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
6441 case SystemZ::ATOMIC_LOADW_AFI:
6442 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
6443 case SystemZ::ATOMIC_LOAD_AR:
6444 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
6445 case SystemZ::ATOMIC_LOAD_AHI:
6446 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
6447 case SystemZ::ATOMIC_LOAD_AFI:
6448 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
6449 case SystemZ::ATOMIC_LOAD_AGR:
6450 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
6451 case SystemZ::ATOMIC_LOAD_AGHI:
6452 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
6453 case SystemZ::ATOMIC_LOAD_AGFI:
6454 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
6455
6456 case SystemZ::ATOMIC_LOADW_SR:
6457 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
6458 case SystemZ::ATOMIC_LOAD_SR:
6459 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
6460 case SystemZ::ATOMIC_LOAD_SGR:
6461 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
6462
6463 case SystemZ::ATOMIC_LOADW_NR:
6464 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
6465 case SystemZ::ATOMIC_LOADW_NILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00006466 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006467 case SystemZ::ATOMIC_LOAD_NR:
6468 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006469 case SystemZ::ATOMIC_LOAD_NILL:
6470 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
6471 case SystemZ::ATOMIC_LOAD_NILH:
6472 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
6473 case SystemZ::ATOMIC_LOAD_NILF:
6474 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006475 case SystemZ::ATOMIC_LOAD_NGR:
6476 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006477 case SystemZ::ATOMIC_LOAD_NILL64:
6478 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
6479 case SystemZ::ATOMIC_LOAD_NILH64:
6480 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00006481 case SystemZ::ATOMIC_LOAD_NIHL64:
6482 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
6483 case SystemZ::ATOMIC_LOAD_NIHH64:
6484 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006485 case SystemZ::ATOMIC_LOAD_NILF64:
6486 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00006487 case SystemZ::ATOMIC_LOAD_NIHF64:
6488 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006489
6490 case SystemZ::ATOMIC_LOADW_OR:
6491 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
6492 case SystemZ::ATOMIC_LOADW_OILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00006493 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006494 case SystemZ::ATOMIC_LOAD_OR:
6495 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006496 case SystemZ::ATOMIC_LOAD_OILL:
6497 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
6498 case SystemZ::ATOMIC_LOAD_OILH:
6499 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
6500 case SystemZ::ATOMIC_LOAD_OILF:
6501 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006502 case SystemZ::ATOMIC_LOAD_OGR:
6503 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006504 case SystemZ::ATOMIC_LOAD_OILL64:
6505 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
6506 case SystemZ::ATOMIC_LOAD_OILH64:
6507 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00006508 case SystemZ::ATOMIC_LOAD_OIHL64:
6509 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
6510 case SystemZ::ATOMIC_LOAD_OIHH64:
6511 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006512 case SystemZ::ATOMIC_LOAD_OILF64:
6513 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00006514 case SystemZ::ATOMIC_LOAD_OIHF64:
6515 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006516
6517 case SystemZ::ATOMIC_LOADW_XR:
6518 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
6519 case SystemZ::ATOMIC_LOADW_XILF:
Richard Sandiford652784e2013-09-25 11:11:53 +00006520 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006521 case SystemZ::ATOMIC_LOAD_XR:
6522 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006523 case SystemZ::ATOMIC_LOAD_XILF:
6524 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006525 case SystemZ::ATOMIC_LOAD_XGR:
6526 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006527 case SystemZ::ATOMIC_LOAD_XILF64:
6528 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
Richard Sandiford5718dac2013-10-01 14:08:44 +00006529 case SystemZ::ATOMIC_LOAD_XIHF64:
6530 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006531
6532 case SystemZ::ATOMIC_LOADW_NRi:
6533 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
6534 case SystemZ::ATOMIC_LOADW_NILHi:
Richard Sandiford652784e2013-09-25 11:11:53 +00006535 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006536 case SystemZ::ATOMIC_LOAD_NRi:
6537 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006538 case SystemZ::ATOMIC_LOAD_NILLi:
6539 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
6540 case SystemZ::ATOMIC_LOAD_NILHi:
6541 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
6542 case SystemZ::ATOMIC_LOAD_NILFi:
6543 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006544 case SystemZ::ATOMIC_LOAD_NGRi:
6545 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006546 case SystemZ::ATOMIC_LOAD_NILL64i:
6547 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
6548 case SystemZ::ATOMIC_LOAD_NILH64i:
6549 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00006550 case SystemZ::ATOMIC_LOAD_NIHL64i:
6551 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
6552 case SystemZ::ATOMIC_LOAD_NIHH64i:
6553 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006554 case SystemZ::ATOMIC_LOAD_NILF64i:
6555 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00006556 case SystemZ::ATOMIC_LOAD_NIHF64i:
6557 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006558
6559 case SystemZ::ATOMIC_LOADW_MIN:
6560 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6561 SystemZ::CCMASK_CMP_LE, 0);
6562 case SystemZ::ATOMIC_LOAD_MIN_32:
6563 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6564 SystemZ::CCMASK_CMP_LE, 32);
6565 case SystemZ::ATOMIC_LOAD_MIN_64:
6566 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
6567 SystemZ::CCMASK_CMP_LE, 64);
6568
6569 case SystemZ::ATOMIC_LOADW_MAX:
6570 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6571 SystemZ::CCMASK_CMP_GE, 0);
6572 case SystemZ::ATOMIC_LOAD_MAX_32:
6573 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6574 SystemZ::CCMASK_CMP_GE, 32);
6575 case SystemZ::ATOMIC_LOAD_MAX_64:
6576 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
6577 SystemZ::CCMASK_CMP_GE, 64);
6578
6579 case SystemZ::ATOMIC_LOADW_UMIN:
6580 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6581 SystemZ::CCMASK_CMP_LE, 0);
6582 case SystemZ::ATOMIC_LOAD_UMIN_32:
6583 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6584 SystemZ::CCMASK_CMP_LE, 32);
6585 case SystemZ::ATOMIC_LOAD_UMIN_64:
6586 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6587 SystemZ::CCMASK_CMP_LE, 64);
6588
6589 case SystemZ::ATOMIC_LOADW_UMAX:
6590 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6591 SystemZ::CCMASK_CMP_GE, 0);
6592 case SystemZ::ATOMIC_LOAD_UMAX_32:
6593 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6594 SystemZ::CCMASK_CMP_GE, 32);
6595 case SystemZ::ATOMIC_LOAD_UMAX_64:
6596 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6597 SystemZ::CCMASK_CMP_GE, 64);
6598
6599 case SystemZ::ATOMIC_CMP_SWAPW:
6600 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006601 case SystemZ::MVCSequence:
6602 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006603 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00006604 case SystemZ::NCSequence:
6605 case SystemZ::NCLoop:
6606 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
6607 case SystemZ::OCSequence:
6608 case SystemZ::OCLoop:
6609 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
6610 case SystemZ::XCSequence:
6611 case SystemZ::XCLoop:
6612 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006613 case SystemZ::CLCSequence:
6614 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006615 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00006616 case SystemZ::CLSTLoop:
6617 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00006618 case SystemZ::MVSTLoop:
6619 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00006620 case SystemZ::SRSTLoop:
6621 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006622 case SystemZ::TBEGIN:
6623 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false);
6624 case SystemZ::TBEGIN_nofloat:
6625 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true);
6626 case SystemZ::TBEGINC:
6627 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true);
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006628 case SystemZ::LTEBRCompare_VecPseudo:
6629 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR);
6630 case SystemZ::LTDBRCompare_VecPseudo:
6631 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR);
6632 case SystemZ::LTXBRCompare_VecPseudo:
6633 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR);
6634
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006635 default:
6636 llvm_unreachable("Unexpected instr type to insert");
6637 }
6638}
Jonas Paulsson11d251c2017-05-10 13:03:25 +00006639
6640// This is only used by the isel schedulers, and is needed only to prevent
6641// compiler from crashing when list-ilp is used.
6642const TargetRegisterClass *
6643SystemZTargetLowering::getRepRegClassFor(MVT VT) const {
6644 if (VT == MVT::Untyped)
6645 return &SystemZ::ADDR128BitRegClass;
6646 return TargetLowering::getRepRegClassFor(VT);
6647}