blob: c0e686e1370a487a3e0e6637d5430da64c8c5047 [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"
Craig Topperd0af7e82017-04-28 05:31:46 +000024#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/KnownBits.h"
Will Dietz981af002013-10-12 00:55:57 +000026#include <cctype>
27
Ulrich Weigand5f613df2013-05-06 16:15:19 +000028using namespace llvm;
29
Chandler Carruth84e68b22014-04-22 02:41:26 +000030#define DEBUG_TYPE "systemz-lower"
31
Richard Sandifordf722a8e302013-10-16 11:10:55 +000032namespace {
33// Represents a sequence for extracting a 0/1 value from an IPM result:
34// (((X ^ XORValue) + AddValue) >> Bit)
35struct IPMConversion {
36 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
37 : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
38
39 int64_t XORValue;
40 int64_t AddValue;
41 unsigned Bit;
42};
Richard Sandifordd420f732013-12-13 15:28:45 +000043
44// Represents information about a comparison.
45struct Comparison {
46 Comparison(SDValue Op0In, SDValue Op1In)
47 : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {}
48
49 // The operands to the comparison.
50 SDValue Op0, Op1;
51
52 // The opcode that should be used to compare Op0 and Op1.
53 unsigned Opcode;
54
55 // A SystemZICMP value. Only used for integer comparisons.
56 unsigned ICmpType;
57
58 // The mask of CC values that Opcode can produce.
59 unsigned CCValid;
60
61 // The mask of CC values for which the original condition is true.
62 unsigned CCMask;
63};
Richard Sandifordc2312692014-03-06 10:38:30 +000064} // end anonymous namespace
Richard Sandifordf722a8e302013-10-16 11:10:55 +000065
Ulrich Weigand5f613df2013-05-06 16:15:19 +000066// Classify VT as either 32 or 64 bit.
67static bool is32Bit(EVT VT) {
68 switch (VT.getSimpleVT().SimpleTy) {
69 case MVT::i32:
70 return true;
71 case MVT::i64:
72 return false;
73 default:
74 llvm_unreachable("Unsupported type");
75 }
76}
77
78// Return a version of MachineOperand that can be safely used before the
79// final use.
80static MachineOperand earlyUseOperand(MachineOperand Op) {
81 if (Op.isReg())
82 Op.setIsKill(false);
83 return Op;
84}
85
Mehdi Amini44ede332015-07-09 02:09:04 +000086SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM,
Eric Christophera6734172015-01-31 00:06:45 +000087 const SystemZSubtarget &STI)
Mehdi Amini44ede332015-07-09 02:09:04 +000088 : TargetLowering(TM), Subtarget(STI) {
Mehdi Amini26d48132015-07-24 16:04:22 +000089 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
Ulrich Weigand5f613df2013-05-06 16:15:19 +000090
91 // Set up the register classes.
Richard Sandiford0755c932013-10-01 11:26:28 +000092 if (Subtarget.hasHighWord())
93 addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass);
94 else
95 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
Ulrich Weigand49506d72015-05-05 19:28:34 +000096 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
97 if (Subtarget.hasVector()) {
98 addRegisterClass(MVT::f32, &SystemZ::VR32BitRegClass);
99 addRegisterClass(MVT::f64, &SystemZ::VR64BitRegClass);
100 } else {
101 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
102 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
103 }
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000104 if (Subtarget.hasVectorEnhancements1())
105 addRegisterClass(MVT::f128, &SystemZ::VR128BitRegClass);
106 else
107 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000108
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000109 if (Subtarget.hasVector()) {
110 addRegisterClass(MVT::v16i8, &SystemZ::VR128BitRegClass);
111 addRegisterClass(MVT::v8i16, &SystemZ::VR128BitRegClass);
112 addRegisterClass(MVT::v4i32, &SystemZ::VR128BitRegClass);
113 addRegisterClass(MVT::v2i64, &SystemZ::VR128BitRegClass);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000114 addRegisterClass(MVT::v4f32, &SystemZ::VR128BitRegClass);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000115 addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000116 }
117
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000118 // Compute derived properties from the register classes
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000119 computeRegisterProperties(Subtarget.getRegisterInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000120
121 // Set up special registers.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000122 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
123
124 // TODO: It may be better to default to latency-oriented scheduling, however
125 // LLVM's current latency-oriented scheduler can't handle physreg definitions
Richard Sandiford14a44492013-05-22 13:38:45 +0000126 // such as SystemZ has with CC, so set this to the register-pressure
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000127 // scheduler, because it can.
128 setSchedulingPreference(Sched::RegPressure);
129
130 setBooleanContents(ZeroOrOneBooleanContent);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000131 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000132
133 // Instructions are strings of 2-byte aligned 2-byte values.
134 setMinFunctionAlignment(2);
135
136 // Handle operations that are handled in a similar way for all types.
137 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
138 I <= MVT::LAST_FP_VALUETYPE;
139 ++I) {
140 MVT VT = MVT::SimpleValueType(I);
141 if (isTypeLegal(VT)) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000142 // Lower SET_CC into an IPM-based sequence.
143 setOperationAction(ISD::SETCC, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000144
145 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
146 setOperationAction(ISD::SELECT, VT, Expand);
147
148 // Lower SELECT_CC and BR_CC into separate comparisons and branches.
149 setOperationAction(ISD::SELECT_CC, VT, Custom);
150 setOperationAction(ISD::BR_CC, VT, Custom);
151 }
152 }
153
154 // Expand jump table branches as address arithmetic followed by an
155 // indirect jump.
156 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
157
158 // Expand BRCOND into a BR_CC (see above).
159 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
160
161 // Handle integer types.
162 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
163 I <= MVT::LAST_INTEGER_VALUETYPE;
164 ++I) {
165 MVT VT = MVT::SimpleValueType(I);
166 if (isTypeLegal(VT)) {
167 // Expand individual DIV and REMs into DIVREMs.
168 setOperationAction(ISD::SDIV, VT, Expand);
169 setOperationAction(ISD::UDIV, VT, Expand);
170 setOperationAction(ISD::SREM, VT, Expand);
171 setOperationAction(ISD::UREM, VT, Expand);
172 setOperationAction(ISD::SDIVREM, VT, Custom);
173 setOperationAction(ISD::UDIVREM, VT, Custom);
174
Richard Sandifordbef3d7a2013-12-10 10:49:34 +0000175 // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and
176 // stores, putting a serialization instruction after the stores.
177 setOperationAction(ISD::ATOMIC_LOAD, VT, Custom);
178 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000179
Richard Sandiford41350a52013-12-24 15:18:04 +0000180 // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are
181 // available, or if the operand is constant.
182 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
183
Ulrich Weigandb4012182015-03-31 12:56:33 +0000184 // Use POPCNT on z196 and above.
185 if (Subtarget.hasPopulationCount())
186 setOperationAction(ISD::CTPOP, VT, Custom);
187 else
188 setOperationAction(ISD::CTPOP, VT, Expand);
189
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000190 // No special instructions for these.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000191 setOperationAction(ISD::CTTZ, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000192 setOperationAction(ISD::ROTR, VT, Expand);
193
Richard Sandiford7d86e472013-08-21 09:34:56 +0000194 // Use *MUL_LOHI where possible instead of MULH*.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000195 setOperationAction(ISD::MULHS, VT, Expand);
196 setOperationAction(ISD::MULHU, VT, Expand);
Richard Sandiford7d86e472013-08-21 09:34:56 +0000197 setOperationAction(ISD::SMUL_LOHI, VT, Custom);
198 setOperationAction(ISD::UMUL_LOHI, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000199
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000200 // Only z196 and above have native support for conversions to unsigned.
Jonas Paulssonb7a2ef82017-02-02 15:42:14 +0000201 // On z10, promoting to i64 doesn't generate an inexact condition for
202 // values that are outside the i32 range but in the i64 range, so use
203 // the default expansion.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000204 if (!Subtarget.hasFPExtension())
205 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000206 }
207 }
208
209 // Type legalization will convert 8- and 16-bit atomic operations into
210 // forms that operate on i32s (but still keeping the original memory VT).
211 // Lower them into full i32 operations.
212 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom);
213 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom);
214 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
215 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom);
216 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom);
217 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom);
218 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
219 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom);
220 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom);
221 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
222 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
223 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
224
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +0000225 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
226
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000227 // Traps are legal, as we will convert them to "j .+2".
228 setOperationAction(ISD::TRAP, MVT::Other, Legal);
229
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000230 // z10 has instructions for signed but not unsigned FP conversion.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000231 // Handle unsigned 32-bit types as signed 64-bit types.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000232 if (!Subtarget.hasFPExtension()) {
233 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
234 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
235 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000236
237 // We have native support for a 64-bit CTLZ, via FLOGR.
238 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
239 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
240
241 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
242 setOperationAction(ISD::OR, MVT::i64, Custom);
243
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000244 // FIXME: Can we support these natively?
245 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
246 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
247 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
248
249 // We have native instructions for i8, i16 and i32 extensions, but not i1.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000250 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000251 for (MVT VT : MVT::integer_valuetypes()) {
252 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
253 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
254 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
255 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000256
257 // Handle the various types of symbolic address.
258 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
259 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
260 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
261 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
262 setOperationAction(ISD::JumpTable, PtrVT, Custom);
263
264 // We need to handle dynamic allocations specially because of the
265 // 160-byte area at the bottom of the stack.
266 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +0000267 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, PtrVT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000268
269 // Use custom expanders so that we can force the function to use
270 // a frame pointer.
271 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
272 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
273
Richard Sandiford03481332013-08-23 11:36:42 +0000274 // Handle prefetches with PFD or PFDRL.
275 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
276
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000277 for (MVT VT : MVT::vector_valuetypes()) {
278 // Assume by default that all vector operations need to be expanded.
279 for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode)
280 if (getOperationAction(Opcode, VT) == Legal)
281 setOperationAction(Opcode, VT, Expand);
282
283 // Likewise all truncating stores and extending loads.
284 for (MVT InnerVT : MVT::vector_valuetypes()) {
285 setTruncStoreAction(VT, InnerVT, Expand);
286 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
287 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
288 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
289 }
290
291 if (isTypeLegal(VT)) {
292 // These operations are legal for anything that can be stored in a
293 // vector register, even if there is no native support for the format
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000294 // as such. In particular, we can do these for v4f32 even though there
295 // are no specific instructions for that format.
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000296 setOperationAction(ISD::LOAD, VT, Legal);
297 setOperationAction(ISD::STORE, VT, Legal);
298 setOperationAction(ISD::VSELECT, VT, Legal);
299 setOperationAction(ISD::BITCAST, VT, Legal);
300 setOperationAction(ISD::UNDEF, VT, Legal);
301
302 // Likewise, except that we need to replace the nodes with something
303 // more specific.
304 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
305 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
306 }
307 }
308
309 // Handle integer vector types.
310 for (MVT VT : MVT::integer_vector_valuetypes()) {
311 if (isTypeLegal(VT)) {
312 // These operations have direct equivalents.
313 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal);
314 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal);
315 setOperationAction(ISD::ADD, VT, Legal);
316 setOperationAction(ISD::SUB, VT, Legal);
317 if (VT != MVT::v2i64)
318 setOperationAction(ISD::MUL, VT, Legal);
319 setOperationAction(ISD::AND, VT, Legal);
320 setOperationAction(ISD::OR, VT, Legal);
321 setOperationAction(ISD::XOR, VT, Legal);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000322 if (Subtarget.hasVectorEnhancements1())
323 setOperationAction(ISD::CTPOP, VT, Legal);
324 else
325 setOperationAction(ISD::CTPOP, VT, Custom);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000326 setOperationAction(ISD::CTTZ, VT, Legal);
327 setOperationAction(ISD::CTLZ, VT, Legal);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000328
329 // Convert a GPR scalar to a vector by inserting it into element 0.
330 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
331
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000332 // Use a series of unpacks for extensions.
333 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Custom);
334 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Custom);
335
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000336 // Detect shifts by a scalar amount and convert them into
337 // V*_BY_SCALAR.
338 setOperationAction(ISD::SHL, VT, Custom);
339 setOperationAction(ISD::SRA, VT, Custom);
340 setOperationAction(ISD::SRL, VT, Custom);
341
342 // At present ROTL isn't matched by DAGCombiner. ROTR should be
343 // converted into ROTL.
344 setOperationAction(ISD::ROTL, VT, Expand);
345 setOperationAction(ISD::ROTR, VT, Expand);
346
347 // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands
348 // and inverting the result as necessary.
349 setOperationAction(ISD::SETCC, VT, Custom);
350 }
351 }
352
Ulrich Weigandcd808232015-05-05 19:26:48 +0000353 if (Subtarget.hasVector()) {
354 // There should be no need to check for float types other than v2f64
355 // since <2 x f32> isn't a legal type.
356 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000357 setOperationAction(ISD::FP_TO_SINT, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000358 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000359 setOperationAction(ISD::FP_TO_UINT, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000360 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000361 setOperationAction(ISD::SINT_TO_FP, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000362 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000363 setOperationAction(ISD::UINT_TO_FP, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000364 }
365
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000366 // Handle floating-point types.
367 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
368 I <= MVT::LAST_FP_VALUETYPE;
369 ++I) {
370 MVT VT = MVT::SimpleValueType(I);
371 if (isTypeLegal(VT)) {
372 // We can use FI for FRINT.
373 setOperationAction(ISD::FRINT, VT, Legal);
374
Richard Sandifordaf5f66a2013-08-21 09:04:20 +0000375 // We can use the extended form of FI for other rounding operations.
376 if (Subtarget.hasFPExtension()) {
377 setOperationAction(ISD::FNEARBYINT, VT, Legal);
378 setOperationAction(ISD::FFLOOR, VT, Legal);
379 setOperationAction(ISD::FCEIL, VT, Legal);
380 setOperationAction(ISD::FTRUNC, VT, Legal);
381 setOperationAction(ISD::FROUND, VT, Legal);
382 }
383
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000384 // No special instructions for these.
385 setOperationAction(ISD::FSIN, VT, Expand);
386 setOperationAction(ISD::FCOS, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000387 setOperationAction(ISD::FSINCOS, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000388 setOperationAction(ISD::FREM, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000389 setOperationAction(ISD::FPOW, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000390 }
391 }
392
Ulrich Weigandcd808232015-05-05 19:26:48 +0000393 // Handle floating-point vector types.
394 if (Subtarget.hasVector()) {
395 // Scalar-to-vector conversion is just a subreg.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000396 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000397 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
398
399 // Some insertions and extractions can be done directly but others
400 // need to go via integers.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000401 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000402 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000403 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000404 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
405
406 // These operations have direct equivalents.
407 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
408 setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
409 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
410 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
411 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
412 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
413 setOperationAction(ISD::FABS, MVT::v2f64, Legal);
414 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
415 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
416 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
417 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
418 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
419 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
420 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
421 }
422
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000423 // The vector enhancements facility 1 has instructions for these.
424 if (Subtarget.hasVectorEnhancements1()) {
Ulrich Weigand33435c42017-07-17 17:42:48 +0000425 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
426 setOperationAction(ISD::FNEG, MVT::v4f32, Legal);
427 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
428 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
429 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
430 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
431 setOperationAction(ISD::FABS, MVT::v4f32, Legal);
432 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
433 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
434 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
435 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
436 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
437 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
438 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
439
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000440 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
441 setOperationAction(ISD::FMAXNAN, MVT::f64, Legal);
442 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
443 setOperationAction(ISD::FMINNAN, MVT::f64, Legal);
444
445 setOperationAction(ISD::FMAXNUM, MVT::v2f64, Legal);
446 setOperationAction(ISD::FMAXNAN, MVT::v2f64, Legal);
447 setOperationAction(ISD::FMINNUM, MVT::v2f64, Legal);
448 setOperationAction(ISD::FMINNAN, MVT::v2f64, Legal);
Ulrich Weigand33435c42017-07-17 17:42:48 +0000449
450 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
451 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal);
452 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
453 setOperationAction(ISD::FMINNAN, MVT::f32, Legal);
454
455 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
456 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal);
457 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
458 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal);
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000459
460 setOperationAction(ISD::FMAXNUM, MVT::f128, Legal);
461 setOperationAction(ISD::FMAXNAN, MVT::f128, Legal);
462 setOperationAction(ISD::FMINNUM, MVT::f128, Legal);
463 setOperationAction(ISD::FMINNAN, MVT::f128, Legal);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000464 }
465
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000466 // We have fused multiply-addition for f32 and f64 but not f128.
467 setOperationAction(ISD::FMA, MVT::f32, Legal);
468 setOperationAction(ISD::FMA, MVT::f64, Legal);
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000469 if (Subtarget.hasVectorEnhancements1())
470 setOperationAction(ISD::FMA, MVT::f128, Legal);
471 else
472 setOperationAction(ISD::FMA, MVT::f128, Expand);
473
474 // We don't have a copysign instruction on vector registers.
475 if (Subtarget.hasVectorEnhancements1())
476 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000477
478 // Needed so that we don't try to implement f128 constant loads using
479 // a load-and-extend of a f80 constant (in cases where the constant
480 // would fit in an f80).
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000481 for (MVT VT : MVT::fp_valuetypes())
482 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000483
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000484 // We don't have extending load instruction on vector registers.
485 if (Subtarget.hasVectorEnhancements1()) {
486 setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f32, Expand);
487 setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f64, Expand);
488 }
489
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000490 // Floating-point truncation and stores need to be done separately.
491 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
492 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
493 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
494
495 // We have 64-bit FPR<->GPR moves, but need special handling for
496 // 32-bit forms.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000497 if (!Subtarget.hasVector()) {
498 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
499 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
500 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000501
502 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
503 // structure, but VAEND is a no-op.
504 setOperationAction(ISD::VASTART, MVT::Other, Custom);
505 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
506 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Richard Sandifordd131ff82013-07-08 09:35:23 +0000507
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000508 // Codes for which we want to perform some z-specific combinations.
509 setTargetDAGCombine(ISD::SIGN_EXTEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000510 setTargetDAGCombine(ISD::STORE);
511 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000512 setTargetDAGCombine(ISD::FP_ROUND);
Bryan Chan28b759c2016-05-16 20:32:22 +0000513 setTargetDAGCombine(ISD::BSWAP);
Elliot Colpbc2cfc22016-07-06 18:13:11 +0000514 setTargetDAGCombine(ISD::SHL);
515 setTargetDAGCombine(ISD::SRA);
516 setTargetDAGCombine(ISD::SRL);
517 setTargetDAGCombine(ISD::ROTL);
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000518
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000519 // Handle intrinsics.
520 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Ulrich Weigandc1708b22015-05-05 19:31:09 +0000521 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000522
Richard Sandifordd131ff82013-07-08 09:35:23 +0000523 // We want to use MVC in preference to even a single load/store pair.
524 MaxStoresPerMemcpy = 0;
525 MaxStoresPerMemcpyOptSize = 0;
Richard Sandiford47660c12013-07-09 09:32:42 +0000526
527 // The main memset sequence is a byte store followed by an MVC.
528 // Two STC or MV..I stores win over that, but the kind of fused stores
529 // generated by target-independent code don't when the byte value is
530 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
531 // than "STC;MVC". Handle the choice in target-specific code instead.
532 MaxStoresPerMemset = 0;
533 MaxStoresPerMemsetOptSize = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000534}
535
Mehdi Amini44ede332015-07-09 02:09:04 +0000536EVT SystemZTargetLowering::getSetCCResultType(const DataLayout &DL,
537 LLVMContext &, EVT VT) const {
Richard Sandifordabc010b2013-11-06 12:16:02 +0000538 if (!VT.isVector())
539 return MVT::i32;
540 return VT.changeVectorElementTypeToInteger();
541}
542
543bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
Stephen Lin73de7bf2013-07-09 18:16:56 +0000544 VT = VT.getScalarType();
545
546 if (!VT.isSimple())
547 return false;
548
549 switch (VT.getSimpleVT().SimpleTy) {
550 case MVT::f32:
551 case MVT::f64:
552 return true;
553 case MVT::f128:
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000554 return Subtarget.hasVectorEnhancements1();
Stephen Lin73de7bf2013-07-09 18:16:56 +0000555 default:
556 break;
557 }
558
559 return false;
560}
561
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000562bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
563 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
564 return Imm.isZero() || Imm.isNegZero();
565}
566
Ulrich Weigand1f6666a2015-03-31 12:52:27 +0000567bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
568 // We can use CGFI or CLGFI.
569 return isInt<32>(Imm) || isUInt<32>(Imm);
570}
571
572bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const {
573 // We can use ALGFI or SLGFI.
574 return isUInt<32>(Imm) || isUInt<32>(-Imm);
575}
576
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000577bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
578 unsigned,
579 unsigned,
580 bool *Fast) const {
Richard Sandiford46af5a22013-05-30 09:45:42 +0000581 // Unaligned accesses should never be slower than the expanded version.
582 // We check specifically for aligned accesses in the few cases where
583 // they are required.
584 if (Fast)
585 *Fast = true;
586 return true;
587}
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +0000588
Jonas Paulsson024e3192017-07-21 11:59:37 +0000589// Information about the addressing mode for a memory access.
590struct AddressingMode {
591 // True if a long displacement is supported.
592 bool LongDisplacement;
593
594 // True if use of index register is supported.
595 bool IndexReg;
596
597 AddressingMode(bool LongDispl, bool IdxReg) :
598 LongDisplacement(LongDispl), IndexReg(IdxReg) {}
599};
600
601// Return the desired addressing mode for a Load which has only one use (in
602// the same block) which is a Store.
603static AddressingMode getLoadStoreAddrMode(bool HasVector,
604 Type *Ty) {
605 // With vector support a Load->Store combination may be combined to either
606 // an MVC or vector operations and it seems to work best to allow the
607 // vector addressing mode.
608 if (HasVector)
609 return AddressingMode(false/*LongDispl*/, true/*IdxReg*/);
610
611 // Otherwise only the MVC case is special.
612 bool MVC = Ty->isIntegerTy(8);
613 return AddressingMode(!MVC/*LongDispl*/, !MVC/*IdxReg*/);
614}
615
616// Return the addressing mode which seems most desirable given an LLVM
617// Instruction pointer.
618static AddressingMode
619supportedAddressingMode(Instruction *I, bool HasVector) {
620 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
621 switch (II->getIntrinsicID()) {
622 default: break;
623 case Intrinsic::memset:
624 case Intrinsic::memmove:
625 case Intrinsic::memcpy:
626 return AddressingMode(false/*LongDispl*/, false/*IdxReg*/);
627 }
628 }
629
630 if (isa<LoadInst>(I) && I->hasOneUse()) {
631 auto *SingleUser = dyn_cast<Instruction>(*I->user_begin());
632 if (SingleUser->getParent() == I->getParent()) {
633 if (isa<ICmpInst>(SingleUser)) {
634 if (auto *C = dyn_cast<ConstantInt>(SingleUser->getOperand(1)))
635 if (isInt<16>(C->getSExtValue()) || isUInt<16>(C->getZExtValue()))
636 // Comparison of memory with 16 bit signed / unsigned immediate
637 return AddressingMode(false/*LongDispl*/, false/*IdxReg*/);
638 } else if (isa<StoreInst>(SingleUser))
639 // Load->Store
640 return getLoadStoreAddrMode(HasVector, I->getType());
641 }
642 } else if (auto *StoreI = dyn_cast<StoreInst>(I)) {
643 if (auto *LoadI = dyn_cast<LoadInst>(StoreI->getValueOperand()))
644 if (LoadI->hasOneUse() && LoadI->getParent() == I->getParent())
645 // Load->Store
646 return getLoadStoreAddrMode(HasVector, LoadI->getType());
647 }
648
649 if (HasVector && (isa<LoadInst>(I) || isa<StoreInst>(I))) {
650
651 // * Use LDE instead of LE/LEY for z13 to avoid partial register
652 // dependencies (LDE only supports small offsets).
653 // * Utilize the vector registers to hold floating point
654 // values (vector load / store instructions only support small
655 // offsets).
656
657 Type *MemAccessTy = (isa<LoadInst>(I) ? I->getType() :
658 I->getOperand(0)->getType());
659 bool IsFPAccess = MemAccessTy->isFloatingPointTy();
660 bool IsVectorAccess = MemAccessTy->isVectorTy();
661
662 // A store of an extracted vector element will be combined into a VSTE type
663 // instruction.
664 if (!IsVectorAccess && isa<StoreInst>(I)) {
665 Value *DataOp = I->getOperand(0);
666 if (isa<ExtractElementInst>(DataOp))
667 IsVectorAccess = true;
668 }
669
670 // A load which gets inserted into a vector element will be combined into a
671 // VLE type instruction.
672 if (!IsVectorAccess && isa<LoadInst>(I) && I->hasOneUse()) {
673 User *LoadUser = *I->user_begin();
674 if (isa<InsertElementInst>(LoadUser))
675 IsVectorAccess = true;
676 }
677
678 if (IsFPAccess || IsVectorAccess)
679 return AddressingMode(false/*LongDispl*/, true/*IdxReg*/);
680 }
681
682 return AddressingMode(true/*LongDispl*/, true/*IdxReg*/);
683}
684
685// TODO: This method should also check for the displacement when *I is
686// passed. It may also be possible to merge with isFoldableMemAccessOffset()
687// now that both methods get the *I.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000688bool SystemZTargetLowering::isLegalAddressingMode(const DataLayout &DL,
Jonas Paulsson024e3192017-07-21 11:59:37 +0000689 const AddrMode &AM, Type *Ty, unsigned AS, Instruction *I) const {
Richard Sandiford791bea42013-07-31 12:58:26 +0000690 // Punt on globals for now, although they can be used in limited
691 // RELATIVE LONG cases.
692 if (AM.BaseGV)
693 return false;
694
695 // Require a 20-bit signed offset.
696 if (!isInt<20>(AM.BaseOffs))
697 return false;
698
Jonas Paulsson024e3192017-07-21 11:59:37 +0000699 if (I != nullptr &&
700 !supportedAddressingMode(I, Subtarget.hasVector()).IndexReg)
701 // No indexing allowed.
702 return AM.Scale == 0;
703 else
704 // Indexing is OK but no scale factor can be applied.
705 return AM.Scale == 0 || AM.Scale == 1;
Richard Sandiford791bea42013-07-31 12:58:26 +0000706}
707
Jonas Paulsson024e3192017-07-21 11:59:37 +0000708// TODO: Should we check for isInt<20> also?
Jonas Paulsson7a794222016-08-17 13:24:19 +0000709bool SystemZTargetLowering::isFoldableMemAccessOffset(Instruction *I,
710 int64_t Offset) const {
Jonas Paulsson024e3192017-07-21 11:59:37 +0000711 if (!supportedAddressingMode(I, Subtarget.hasVector()).LongDisplacement)
712 return (isUInt<12>(Offset));
Jonas Paulsson7a794222016-08-17 13:24:19 +0000713
714 return true;
715}
716
Richard Sandiford709bda62013-08-19 12:42:31 +0000717bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
718 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
719 return false;
720 unsigned FromBits = FromType->getPrimitiveSizeInBits();
721 unsigned ToBits = ToType->getPrimitiveSizeInBits();
722 return FromBits > ToBits;
723}
724
725bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
726 if (!FromVT.isInteger() || !ToVT.isInteger())
727 return false;
728 unsigned FromBits = FromVT.getSizeInBits();
729 unsigned ToBits = ToVT.getSizeInBits();
730 return FromBits > ToBits;
731}
732
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000733//===----------------------------------------------------------------------===//
734// Inline asm support
735//===----------------------------------------------------------------------===//
736
737TargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000738SystemZTargetLowering::getConstraintType(StringRef Constraint) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000739 if (Constraint.size() == 1) {
740 switch (Constraint[0]) {
741 case 'a': // Address register
742 case 'd': // Data register (equivalent to 'r')
743 case 'f': // Floating-point register
Richard Sandiford0755c932013-10-01 11:26:28 +0000744 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000745 case 'r': // General-purpose register
746 return C_RegisterClass;
747
748 case 'Q': // Memory with base and unsigned 12-bit displacement
749 case 'R': // Likewise, plus an index
750 case 'S': // Memory with base and signed 20-bit displacement
751 case 'T': // Likewise, plus an index
752 case 'm': // Equivalent to 'T'.
753 return C_Memory;
754
755 case 'I': // Unsigned 8-bit constant
756 case 'J': // Unsigned 12-bit constant
757 case 'K': // Signed 16-bit constant
758 case 'L': // Signed 20-bit displacement (on all targets we support)
759 case 'M': // 0x7fffffff
760 return C_Other;
761
762 default:
763 break;
764 }
765 }
766 return TargetLowering::getConstraintType(Constraint);
767}
768
769TargetLowering::ConstraintWeight SystemZTargetLowering::
770getSingleConstraintMatchWeight(AsmOperandInfo &info,
771 const char *constraint) const {
772 ConstraintWeight weight = CW_Invalid;
773 Value *CallOperandVal = info.CallOperandVal;
774 // If we don't have a value, we can't do a match,
775 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +0000776 if (!CallOperandVal)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000777 return CW_Default;
778 Type *type = CallOperandVal->getType();
779 // Look at the constraint type.
780 switch (*constraint) {
781 default:
782 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
783 break;
784
785 case 'a': // Address register
786 case 'd': // Data register (equivalent to 'r')
Richard Sandiford0755c932013-10-01 11:26:28 +0000787 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000788 case 'r': // General-purpose register
789 if (CallOperandVal->getType()->isIntegerTy())
790 weight = CW_Register;
791 break;
792
793 case 'f': // Floating-point register
794 if (type->isFloatingPointTy())
795 weight = CW_Register;
796 break;
797
798 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000799 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000800 if (isUInt<8>(C->getZExtValue()))
801 weight = CW_Constant;
802 break;
803
804 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000805 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000806 if (isUInt<12>(C->getZExtValue()))
807 weight = CW_Constant;
808 break;
809
810 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000811 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000812 if (isInt<16>(C->getSExtValue()))
813 weight = CW_Constant;
814 break;
815
816 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000817 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000818 if (isInt<20>(C->getSExtValue()))
819 weight = CW_Constant;
820 break;
821
822 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000823 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000824 if (C->getZExtValue() == 0x7fffffff)
825 weight = CW_Constant;
826 break;
827 }
828 return weight;
829}
830
Richard Sandifordb8204052013-07-12 09:08:12 +0000831// Parse a "{tNNN}" register constraint for which the register type "t"
832// has already been verified. MC is the class associated with "t" and
833// Map maps 0-based register numbers to LLVM register numbers.
834static std::pair<unsigned, const TargetRegisterClass *>
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000835parseRegisterNumber(StringRef Constraint, const TargetRegisterClass *RC,
836 const unsigned *Map) {
Richard Sandifordb8204052013-07-12 09:08:12 +0000837 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
838 if (isdigit(Constraint[2])) {
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000839 unsigned Index;
840 bool Failed =
841 Constraint.slice(2, Constraint.size() - 1).getAsInteger(10, Index);
842 if (!Failed && Index < 16 && Map[Index])
Richard Sandifordb8204052013-07-12 09:08:12 +0000843 return std::make_pair(Map[Index], RC);
844 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000845 return std::make_pair(0U, nullptr);
Richard Sandifordb8204052013-07-12 09:08:12 +0000846}
847
Eric Christopher11e4df72015-02-26 22:38:43 +0000848std::pair<unsigned, const TargetRegisterClass *>
849SystemZTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000850 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000851 if (Constraint.size() == 1) {
852 // GCC Constraint Letters
853 switch (Constraint[0]) {
854 default: break;
855 case 'd': // Data register (equivalent to 'r')
856 case 'r': // General-purpose register
857 if (VT == MVT::i64)
858 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
859 else if (VT == MVT::i128)
860 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
861 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
862
863 case 'a': // Address register
864 if (VT == MVT::i64)
865 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
866 else if (VT == MVT::i128)
867 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
868 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
869
Richard Sandiford0755c932013-10-01 11:26:28 +0000870 case 'h': // High-part register (an LLVM extension)
871 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
872
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000873 case 'f': // Floating-point register
874 if (VT == MVT::f64)
875 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
876 else if (VT == MVT::f128)
877 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
878 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
879 }
880 }
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000881 if (Constraint.size() > 0 && Constraint[0] == '{') {
Richard Sandifordb8204052013-07-12 09:08:12 +0000882 // We need to override the default register parsing for GPRs and FPRs
883 // because the interpretation depends on VT. The internal names of
884 // the registers are also different from the external names
885 // (F0D and F0S instead of F0, etc.).
886 if (Constraint[1] == 'r') {
887 if (VT == MVT::i32)
888 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
889 SystemZMC::GR32Regs);
890 if (VT == MVT::i128)
891 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
892 SystemZMC::GR128Regs);
893 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
894 SystemZMC::GR64Regs);
895 }
896 if (Constraint[1] == 'f') {
897 if (VT == MVT::f32)
898 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
899 SystemZMC::FP32Regs);
900 if (VT == MVT::f128)
901 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
902 SystemZMC::FP128Regs);
903 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
904 SystemZMC::FP64Regs);
905 }
906 }
Eric Christopher11e4df72015-02-26 22:38:43 +0000907 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000908}
909
910void SystemZTargetLowering::
911LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
912 std::vector<SDValue> &Ops,
913 SelectionDAG &DAG) const {
914 // Only support length 1 constraints for now.
915 if (Constraint.length() == 1) {
916 switch (Constraint[0]) {
917 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000918 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000919 if (isUInt<8>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000920 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000921 Op.getValueType()));
922 return;
923
924 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000925 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000926 if (isUInt<12>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000927 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000928 Op.getValueType()));
929 return;
930
931 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000932 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000933 if (isInt<16>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000934 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000935 Op.getValueType()));
936 return;
937
938 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000939 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000940 if (isInt<20>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000941 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000942 Op.getValueType()));
943 return;
944
945 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000946 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000947 if (C->getZExtValue() == 0x7fffffff)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000948 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000949 Op.getValueType()));
950 return;
951 }
952 }
953 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
954}
955
956//===----------------------------------------------------------------------===//
957// Calling conventions
958//===----------------------------------------------------------------------===//
959
960#include "SystemZGenCallingConv.inc"
961
Richard Sandiford709bda62013-08-19 12:42:31 +0000962bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
963 Type *ToType) const {
964 return isTruncateFree(FromType, ToType);
965}
966
Matt Arsenault31380752017-04-18 21:16:46 +0000967bool SystemZTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
Ulrich Weigand19d24d22015-11-13 13:00:27 +0000968 return CI->isTailCall();
Richard Sandiford709bda62013-08-19 12:42:31 +0000969}
970
Ulrich Weigand5211f9f2015-05-05 19:30:05 +0000971// We do not yet support 128-bit single-element vector types. If the user
972// attempts to use such types as function argument or return type, prefer
973// to error out instead of emitting code violating the ABI.
974static void VerifyVectorType(MVT VT, EVT ArgVT) {
975 if (ArgVT.isVector() && !VT.isVector())
976 report_fatal_error("Unsupported vector argument or return type");
977}
978
979static void VerifyVectorTypes(const SmallVectorImpl<ISD::InputArg> &Ins) {
980 for (unsigned i = 0; i < Ins.size(); ++i)
981 VerifyVectorType(Ins[i].VT, Ins[i].ArgVT);
982}
983
984static void VerifyVectorTypes(const SmallVectorImpl<ISD::OutputArg> &Outs) {
985 for (unsigned i = 0; i < Outs.size(); ++i)
986 VerifyVectorType(Outs[i].VT, Outs[i].ArgVT);
987}
988
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000989// Value is a value that has been passed to us in the location described by VA
990// (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
991// any loads onto Chain.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000992static SDValue convertLocVTToValVT(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000993 CCValAssign &VA, SDValue Chain,
994 SDValue Value) {
995 // If the argument has been promoted from a smaller type, insert an
996 // assertion to capture this.
997 if (VA.getLocInfo() == CCValAssign::SExt)
998 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
999 DAG.getValueType(VA.getValVT()));
1000 else if (VA.getLocInfo() == CCValAssign::ZExt)
1001 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
1002 DAG.getValueType(VA.getValVT()));
1003
1004 if (VA.isExtInLoc())
1005 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00001006 else if (VA.getLocInfo() == CCValAssign::BCvt) {
1007 // If this is a short vector argument loaded from the stack,
1008 // extend from i64 to full vector size and then bitcast.
1009 assert(VA.getLocVT() == MVT::i64);
1010 assert(VA.getValVT().isVector());
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001011 Value = DAG.getBuildVector(MVT::v2i64, DL, {Value, DAG.getUNDEF(MVT::i64)});
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00001012 Value = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Value);
1013 } else
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001014 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
1015 return Value;
1016}
1017
1018// Value is a value of type VA.getValVT() that we need to copy into
1019// the location described by VA. Return a copy of Value converted to
1020// VA.getValVT(). The caller is responsible for handling indirect values.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001021static SDValue convertValVTToLocVT(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001022 CCValAssign &VA, SDValue Value) {
1023 switch (VA.getLocInfo()) {
1024 case CCValAssign::SExt:
1025 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
1026 case CCValAssign::ZExt:
1027 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
1028 case CCValAssign::AExt:
1029 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00001030 case CCValAssign::BCvt:
1031 // If this is a short vector argument to be stored to the stack,
1032 // bitcast to v2i64 and then extract first element.
1033 assert(VA.getLocVT() == MVT::i64);
1034 assert(VA.getValVT().isVector());
1035 Value = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Value);
1036 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VA.getLocVT(), Value,
1037 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001038 case CCValAssign::Full:
1039 return Value;
1040 default:
1041 llvm_unreachable("Unhandled getLocInfo()");
1042 }
1043}
1044
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001045SDValue SystemZTargetLowering::LowerFormalArguments(
1046 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
1047 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1048 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001049 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001050 MachineFrameInfo &MFI = MF.getFrameInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001051 MachineRegisterInfo &MRI = MF.getRegInfo();
1052 SystemZMachineFunctionInfo *FuncInfo =
Eric Christophera6734172015-01-31 00:06:45 +00001053 MF.getInfo<SystemZMachineFunctionInfo>();
1054 auto *TFL =
1055 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering());
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001056 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001057
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001058 // Detect unsupported vector argument types.
1059 if (Subtarget.hasVector())
1060 VerifyVectorTypes(Ins);
1061
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001062 // Assign locations to all of the incoming arguments.
1063 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001064 SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001065 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
1066
1067 unsigned NumFixedGPRs = 0;
1068 unsigned NumFixedFPRs = 0;
1069 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1070 SDValue ArgValue;
1071 CCValAssign &VA = ArgLocs[I];
1072 EVT LocVT = VA.getLocVT();
1073 if (VA.isRegLoc()) {
1074 // Arguments passed in registers
1075 const TargetRegisterClass *RC;
1076 switch (LocVT.getSimpleVT().SimpleTy) {
1077 default:
1078 // Integers smaller than i64 should be promoted to i64.
1079 llvm_unreachable("Unexpected argument type");
1080 case MVT::i32:
1081 NumFixedGPRs += 1;
1082 RC = &SystemZ::GR32BitRegClass;
1083 break;
1084 case MVT::i64:
1085 NumFixedGPRs += 1;
1086 RC = &SystemZ::GR64BitRegClass;
1087 break;
1088 case MVT::f32:
1089 NumFixedFPRs += 1;
1090 RC = &SystemZ::FP32BitRegClass;
1091 break;
1092 case MVT::f64:
1093 NumFixedFPRs += 1;
1094 RC = &SystemZ::FP64BitRegClass;
1095 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001096 case MVT::v16i8:
1097 case MVT::v8i16:
1098 case MVT::v4i32:
1099 case MVT::v2i64:
Ulrich Weigand80b3af72015-05-05 19:27:45 +00001100 case MVT::v4f32:
Ulrich Weigandcd808232015-05-05 19:26:48 +00001101 case MVT::v2f64:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001102 RC = &SystemZ::VR128BitRegClass;
1103 break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001104 }
1105
1106 unsigned VReg = MRI.createVirtualRegister(RC);
1107 MRI.addLiveIn(VA.getLocReg(), VReg);
1108 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
1109 } else {
1110 assert(VA.isMemLoc() && "Argument not register or memory");
1111
1112 // Create the frame index object for this incoming parameter.
Matthias Braun941a7052016-07-28 18:40:00 +00001113 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
1114 VA.getLocMemOffset(), true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001115
1116 // Create the SelectionDAG nodes corresponding to a load
1117 // from this parameter. Unpromoted ints and floats are
1118 // passed as right-justified 8-byte values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001119 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
1120 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001121 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
1122 DAG.getIntPtrConstant(4, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001123 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00001124 MachinePointerInfo::getFixedStack(MF, FI));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001125 }
1126
1127 // Convert the value of the argument register into the value that's
1128 // being passed.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001129 if (VA.getLocInfo() == CCValAssign::Indirect) {
Justin Lebar9c375812016-07-15 18:27:10 +00001130 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
1131 MachinePointerInfo()));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001132 // If the original argument was split (e.g. i128), we need
1133 // to load all parts of it here (using the same address).
1134 unsigned ArgIndex = Ins[I].OrigArgIndex;
1135 assert (Ins[I].PartOffset == 0);
1136 while (I + 1 != E && Ins[I + 1].OrigArgIndex == ArgIndex) {
1137 CCValAssign &PartVA = ArgLocs[I + 1];
1138 unsigned PartOffset = Ins[I + 1].PartOffset;
1139 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
1140 DAG.getIntPtrConstant(PartOffset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00001141 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
1142 MachinePointerInfo()));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001143 ++I;
1144 }
1145 } else
1146 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001147 }
1148
1149 if (IsVarArg) {
1150 // Save the number of non-varargs registers for later use by va_start, etc.
1151 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
1152 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
1153
1154 // Likewise the address (in the form of a frame index) of where the
1155 // first stack vararg would be. The 1-byte size here is arbitrary.
1156 int64_t StackSize = CCInfo.getNextStackOffset();
Matthias Braun941a7052016-07-28 18:40:00 +00001157 FuncInfo->setVarArgsFrameIndex(MFI.CreateFixedObject(1, StackSize, true));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001158
1159 // ...and a similar frame index for the caller-allocated save area
1160 // that will be used to store the incoming registers.
1161 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
Matthias Braun941a7052016-07-28 18:40:00 +00001162 unsigned RegSaveIndex = MFI.CreateFixedObject(1, RegSaveOffset, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001163 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
1164
1165 // Store the FPR varargs in the reserved frame slots. (We store the
1166 // GPRs as part of the prologue.)
1167 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
1168 SDValue MemOps[SystemZ::NumArgFPRs];
1169 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
1170 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
Matthias Braun941a7052016-07-28 18:40:00 +00001171 int FI = MFI.CreateFixedObject(8, RegSaveOffset + Offset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +00001172 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001173 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
1174 &SystemZ::FP64BitRegClass);
1175 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
1176 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00001177 MachinePointerInfo::getFixedStack(MF, FI));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001178 }
1179 // Join the stores, which are independent of one another.
1180 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00001181 makeArrayRef(&MemOps[NumFixedFPRs],
1182 SystemZ::NumArgFPRs-NumFixedFPRs));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001183 }
1184 }
1185
1186 return Chain;
1187}
1188
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +00001189static bool canUseSiblingCall(const CCState &ArgCCInfo,
Bryan Chan893110e2016-04-28 00:17:23 +00001190 SmallVectorImpl<CCValAssign> &ArgLocs,
1191 SmallVectorImpl<ISD::OutputArg> &Outs) {
Richard Sandiford709bda62013-08-19 12:42:31 +00001192 // Punt if there are any indirect or stack arguments, or if the call
Bryan Chan893110e2016-04-28 00:17:23 +00001193 // needs the callee-saved argument register R6, or if the call uses
1194 // the callee-saved register arguments SwiftSelf and SwiftError.
Richard Sandiford709bda62013-08-19 12:42:31 +00001195 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1196 CCValAssign &VA = ArgLocs[I];
1197 if (VA.getLocInfo() == CCValAssign::Indirect)
1198 return false;
1199 if (!VA.isRegLoc())
1200 return false;
1201 unsigned Reg = VA.getLocReg();
Richard Sandiford0755c932013-10-01 11:26:28 +00001202 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
Richard Sandiford709bda62013-08-19 12:42:31 +00001203 return false;
Bryan Chan893110e2016-04-28 00:17:23 +00001204 if (Outs[I].Flags.isSwiftSelf() || Outs[I].Flags.isSwiftError())
1205 return false;
Richard Sandiford709bda62013-08-19 12:42:31 +00001206 }
1207 return true;
1208}
1209
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001210SDValue
1211SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
1212 SmallVectorImpl<SDValue> &InVals) const {
1213 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001214 SDLoc &DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001215 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1216 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1217 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001218 SDValue Chain = CLI.Chain;
1219 SDValue Callee = CLI.Callee;
Richard Sandiford709bda62013-08-19 12:42:31 +00001220 bool &IsTailCall = CLI.IsTailCall;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001221 CallingConv::ID CallConv = CLI.CallConv;
1222 bool IsVarArg = CLI.IsVarArg;
1223 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +00001224 EVT PtrVT = getPointerTy(MF.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001225
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001226 // Detect unsupported vector argument and return types.
1227 if (Subtarget.hasVector()) {
1228 VerifyVectorTypes(Outs);
1229 VerifyVectorTypes(Ins);
1230 }
1231
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001232 // Analyze the operands of the call, assigning locations to each operand.
1233 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001234 SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001235 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
1236
Richard Sandiford709bda62013-08-19 12:42:31 +00001237 // We don't support GuaranteedTailCallOpt, only automatically-detected
1238 // sibling calls.
Bryan Chan893110e2016-04-28 00:17:23 +00001239 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs, Outs))
Richard Sandiford709bda62013-08-19 12:42:31 +00001240 IsTailCall = false;
1241
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001242 // Get a count of how many bytes are to be pushed on the stack.
1243 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1244
1245 // Mark the start of the call.
Richard Sandiford709bda62013-08-19 12:42:31 +00001246 if (!IsTailCall)
Serge Pavlovd526b132017-05-09 13:35:13 +00001247 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001248
1249 // Copy argument values to their designated locations.
1250 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
1251 SmallVector<SDValue, 8> MemOpChains;
1252 SDValue StackPtr;
1253 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1254 CCValAssign &VA = ArgLocs[I];
1255 SDValue ArgValue = OutVals[I];
1256
1257 if (VA.getLocInfo() == CCValAssign::Indirect) {
1258 // Store the argument in a stack slot and pass its address.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001259 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[I].ArgVT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001260 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Justin Lebar9c375812016-07-15 18:27:10 +00001261 MemOpChains.push_back(
1262 DAG.getStore(Chain, DL, ArgValue, SpillSlot,
1263 MachinePointerInfo::getFixedStack(MF, FI)));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001264 // If the original argument was split (e.g. i128), we need
1265 // to store all parts of it here (and pass just one address).
1266 unsigned ArgIndex = Outs[I].OrigArgIndex;
1267 assert (Outs[I].PartOffset == 0);
1268 while (I + 1 != E && Outs[I + 1].OrigArgIndex == ArgIndex) {
1269 SDValue PartValue = OutVals[I + 1];
1270 unsigned PartOffset = Outs[I + 1].PartOffset;
1271 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
1272 DAG.getIntPtrConstant(PartOffset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00001273 MemOpChains.push_back(
1274 DAG.getStore(Chain, DL, PartValue, Address,
1275 MachinePointerInfo::getFixedStack(MF, FI)));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001276 ++I;
1277 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001278 ArgValue = SpillSlot;
1279 } else
1280 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
1281
1282 if (VA.isRegLoc())
1283 // Queue up the argument copies and emit them at the end.
1284 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1285 else {
1286 assert(VA.isMemLoc() && "Argument not register or memory");
1287
1288 // Work out the address of the stack slot. Unpromoted ints and
1289 // floats are passed as right-justified 8-byte values.
1290 if (!StackPtr.getNode())
1291 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
1292 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
1293 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
1294 Offset += 4;
1295 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001296 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001297
1298 // Emit the store.
Justin Lebar9c375812016-07-15 18:27:10 +00001299 MemOpChains.push_back(
1300 DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001301 }
1302 }
1303
1304 // Join the stores, which are independent of one another.
1305 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001306 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001307
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001308 // Accept direct calls by converting symbolic call addresses to the
Richard Sandiford709bda62013-08-19 12:42:31 +00001309 // associated Target* opcodes. Force %r1 to be used for indirect
1310 // tail calls.
1311 SDValue Glue;
Richard Sandiford21f5d682014-03-06 11:22:58 +00001312 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001313 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
1314 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford21f5d682014-03-06 11:22:58 +00001315 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001316 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
1317 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford709bda62013-08-19 12:42:31 +00001318 } else if (IsTailCall) {
1319 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
1320 Glue = Chain.getValue(1);
1321 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
1322 }
1323
1324 // Build a sequence of copy-to-reg nodes, chained and glued together.
1325 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
1326 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
1327 RegsToPass[I].second, Glue);
1328 Glue = Chain.getValue(1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001329 }
1330
1331 // The first call operand is the chain and the second is the target address.
1332 SmallVector<SDValue, 8> Ops;
1333 Ops.push_back(Chain);
1334 Ops.push_back(Callee);
1335
1336 // Add argument registers to the end of the list so that they are
1337 // known live into the call.
1338 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
1339 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
1340 RegsToPass[I].second.getValueType()));
1341
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001342 // Add a register mask operand representing the call-preserved registers.
Eric Christophera6734172015-01-31 00:06:45 +00001343 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00001344 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001345 assert(Mask && "Missing call preserved mask for calling convention");
1346 Ops.push_back(DAG.getRegisterMask(Mask));
1347
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001348 // Glue the call to the argument copies, if any.
1349 if (Glue.getNode())
1350 Ops.push_back(Glue);
1351
1352 // Emit the call.
1353 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Richard Sandiford709bda62013-08-19 12:42:31 +00001354 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001355 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
1356 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001357 Glue = Chain.getValue(1);
1358
1359 // Mark the end of the call, which is glued to the call itself.
1360 Chain = DAG.getCALLSEQ_END(Chain,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001361 DAG.getConstant(NumBytes, DL, PtrVT, true),
1362 DAG.getConstant(0, DL, PtrVT, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001363 Glue, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001364 Glue = Chain.getValue(1);
1365
1366 // Assign locations to each value returned by this call.
1367 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001368 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001369 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
1370
1371 // Copy all of the result registers out of their specified physreg.
1372 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1373 CCValAssign &VA = RetLocs[I];
1374
1375 // Copy the value out, gluing the copy to the end of the call sequence.
1376 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
1377 VA.getLocVT(), Glue);
1378 Chain = RetValue.getValue(1);
1379 Glue = RetValue.getValue(2);
1380
1381 // Convert the value of the return register into the value that's
1382 // being returned.
1383 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
1384 }
1385
1386 return Chain;
1387}
1388
Ulrich Weiganda887f062015-08-13 13:37:06 +00001389bool SystemZTargetLowering::
1390CanLowerReturn(CallingConv::ID CallConv,
1391 MachineFunction &MF, bool isVarArg,
1392 const SmallVectorImpl<ISD::OutputArg> &Outs,
1393 LLVMContext &Context) const {
1394 // Detect unsupported vector return types.
1395 if (Subtarget.hasVector())
1396 VerifyVectorTypes(Outs);
1397
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001398 // Special case that we cannot easily detect in RetCC_SystemZ since
1399 // i128 is not a legal type.
1400 for (auto &Out : Outs)
1401 if (Out.ArgVT == MVT::i128)
1402 return false;
1403
Ulrich Weiganda887f062015-08-13 13:37:06 +00001404 SmallVector<CCValAssign, 16> RetLocs;
1405 CCState RetCCInfo(CallConv, isVarArg, MF, RetLocs, Context);
1406 return RetCCInfo.CheckReturn(Outs, RetCC_SystemZ);
1407}
1408
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001409SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001410SystemZTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1411 bool IsVarArg,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001412 const SmallVectorImpl<ISD::OutputArg> &Outs,
1413 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001414 const SDLoc &DL, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001415 MachineFunction &MF = DAG.getMachineFunction();
1416
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001417 // Detect unsupported vector return types.
1418 if (Subtarget.hasVector())
1419 VerifyVectorTypes(Outs);
1420
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001421 // Assign locations to each returned value.
1422 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001423 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001424 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
1425
1426 // Quick exit for void returns
1427 if (RetLocs.empty())
1428 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
1429
1430 // Copy the result values into the output registers.
1431 SDValue Glue;
1432 SmallVector<SDValue, 4> RetOps;
1433 RetOps.push_back(Chain);
1434 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1435 CCValAssign &VA = RetLocs[I];
1436 SDValue RetValue = OutVals[I];
1437
1438 // Make the return register live on exit.
1439 assert(VA.isRegLoc() && "Can only return in registers!");
1440
1441 // Promote the value as required.
1442 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
1443
1444 // Chain and glue the copies together.
1445 unsigned Reg = VA.getLocReg();
1446 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
1447 Glue = Chain.getValue(1);
1448 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
1449 }
1450
1451 // Update chain and glue.
1452 RetOps[0] = Chain;
1453 if (Glue.getNode())
1454 RetOps.push_back(Glue);
1455
Craig Topper48d114b2014-04-26 18:35:24 +00001456 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001457}
1458
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001459// Return true if Op is an intrinsic node with chain that returns the CC value
1460// as its only (other) argument. Provide the associated SystemZISD opcode and
1461// the mask of valid CC values if so.
1462static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode,
1463 unsigned &CCValid) {
1464 unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1465 switch (Id) {
1466 case Intrinsic::s390_tbegin:
1467 Opcode = SystemZISD::TBEGIN;
1468 CCValid = SystemZ::CCMASK_TBEGIN;
1469 return true;
1470
1471 case Intrinsic::s390_tbegin_nofloat:
1472 Opcode = SystemZISD::TBEGIN_NOFLOAT;
1473 CCValid = SystemZ::CCMASK_TBEGIN;
1474 return true;
1475
1476 case Intrinsic::s390_tend:
1477 Opcode = SystemZISD::TEND;
1478 CCValid = SystemZ::CCMASK_TEND;
1479 return true;
1480
1481 default:
1482 return false;
1483 }
1484}
1485
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001486// Return true if Op is an intrinsic node without chain that returns the
1487// CC value as its final argument. Provide the associated SystemZISD
1488// opcode and the mask of valid CC values if so.
1489static bool isIntrinsicWithCC(SDValue Op, unsigned &Opcode, unsigned &CCValid) {
1490 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1491 switch (Id) {
1492 case Intrinsic::s390_vpkshs:
1493 case Intrinsic::s390_vpksfs:
1494 case Intrinsic::s390_vpksgs:
1495 Opcode = SystemZISD::PACKS_CC;
1496 CCValid = SystemZ::CCMASK_VCMP;
1497 return true;
1498
1499 case Intrinsic::s390_vpklshs:
1500 case Intrinsic::s390_vpklsfs:
1501 case Intrinsic::s390_vpklsgs:
1502 Opcode = SystemZISD::PACKLS_CC;
1503 CCValid = SystemZ::CCMASK_VCMP;
1504 return true;
1505
1506 case Intrinsic::s390_vceqbs:
1507 case Intrinsic::s390_vceqhs:
1508 case Intrinsic::s390_vceqfs:
1509 case Intrinsic::s390_vceqgs:
1510 Opcode = SystemZISD::VICMPES;
1511 CCValid = SystemZ::CCMASK_VCMP;
1512 return true;
1513
1514 case Intrinsic::s390_vchbs:
1515 case Intrinsic::s390_vchhs:
1516 case Intrinsic::s390_vchfs:
1517 case Intrinsic::s390_vchgs:
1518 Opcode = SystemZISD::VICMPHS;
1519 CCValid = SystemZ::CCMASK_VCMP;
1520 return true;
1521
1522 case Intrinsic::s390_vchlbs:
1523 case Intrinsic::s390_vchlhs:
1524 case Intrinsic::s390_vchlfs:
1525 case Intrinsic::s390_vchlgs:
1526 Opcode = SystemZISD::VICMPHLS;
1527 CCValid = SystemZ::CCMASK_VCMP;
1528 return true;
1529
1530 case Intrinsic::s390_vtm:
1531 Opcode = SystemZISD::VTM;
1532 CCValid = SystemZ::CCMASK_VCMP;
1533 return true;
1534
1535 case Intrinsic::s390_vfaebs:
1536 case Intrinsic::s390_vfaehs:
1537 case Intrinsic::s390_vfaefs:
1538 Opcode = SystemZISD::VFAE_CC;
1539 CCValid = SystemZ::CCMASK_ANY;
1540 return true;
1541
1542 case Intrinsic::s390_vfaezbs:
1543 case Intrinsic::s390_vfaezhs:
1544 case Intrinsic::s390_vfaezfs:
1545 Opcode = SystemZISD::VFAEZ_CC;
1546 CCValid = SystemZ::CCMASK_ANY;
1547 return true;
1548
1549 case Intrinsic::s390_vfeebs:
1550 case Intrinsic::s390_vfeehs:
1551 case Intrinsic::s390_vfeefs:
1552 Opcode = SystemZISD::VFEE_CC;
1553 CCValid = SystemZ::CCMASK_ANY;
1554 return true;
1555
1556 case Intrinsic::s390_vfeezbs:
1557 case Intrinsic::s390_vfeezhs:
1558 case Intrinsic::s390_vfeezfs:
1559 Opcode = SystemZISD::VFEEZ_CC;
1560 CCValid = SystemZ::CCMASK_ANY;
1561 return true;
1562
1563 case Intrinsic::s390_vfenebs:
1564 case Intrinsic::s390_vfenehs:
1565 case Intrinsic::s390_vfenefs:
1566 Opcode = SystemZISD::VFENE_CC;
1567 CCValid = SystemZ::CCMASK_ANY;
1568 return true;
1569
1570 case Intrinsic::s390_vfenezbs:
1571 case Intrinsic::s390_vfenezhs:
1572 case Intrinsic::s390_vfenezfs:
1573 Opcode = SystemZISD::VFENEZ_CC;
1574 CCValid = SystemZ::CCMASK_ANY;
1575 return true;
1576
1577 case Intrinsic::s390_vistrbs:
1578 case Intrinsic::s390_vistrhs:
1579 case Intrinsic::s390_vistrfs:
1580 Opcode = SystemZISD::VISTR_CC;
1581 CCValid = SystemZ::CCMASK_0 | SystemZ::CCMASK_3;
1582 return true;
1583
1584 case Intrinsic::s390_vstrcbs:
1585 case Intrinsic::s390_vstrchs:
1586 case Intrinsic::s390_vstrcfs:
1587 Opcode = SystemZISD::VSTRC_CC;
1588 CCValid = SystemZ::CCMASK_ANY;
1589 return true;
1590
1591 case Intrinsic::s390_vstrczbs:
1592 case Intrinsic::s390_vstrczhs:
1593 case Intrinsic::s390_vstrczfs:
1594 Opcode = SystemZISD::VSTRCZ_CC;
1595 CCValid = SystemZ::CCMASK_ANY;
1596 return true;
1597
1598 case Intrinsic::s390_vfcedbs:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001599 case Intrinsic::s390_vfcesbs:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001600 Opcode = SystemZISD::VFCMPES;
1601 CCValid = SystemZ::CCMASK_VCMP;
1602 return true;
1603
1604 case Intrinsic::s390_vfchdbs:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001605 case Intrinsic::s390_vfchsbs:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001606 Opcode = SystemZISD::VFCMPHS;
1607 CCValid = SystemZ::CCMASK_VCMP;
1608 return true;
1609
1610 case Intrinsic::s390_vfchedbs:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001611 case Intrinsic::s390_vfchesbs:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001612 Opcode = SystemZISD::VFCMPHES;
1613 CCValid = SystemZ::CCMASK_VCMP;
1614 return true;
1615
1616 case Intrinsic::s390_vftcidb:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001617 case Intrinsic::s390_vftcisb:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001618 Opcode = SystemZISD::VFTCI;
1619 CCValid = SystemZ::CCMASK_VCMP;
1620 return true;
1621
Marcin Koscielnickicf7cc722016-07-10 14:41:22 +00001622 case Intrinsic::s390_tdc:
1623 Opcode = SystemZISD::TDC;
1624 CCValid = SystemZ::CCMASK_TDC;
1625 return true;
1626
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001627 default:
1628 return false;
1629 }
1630}
1631
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001632// Emit an intrinsic with chain with a glued value instead of its CC result.
1633static SDValue emitIntrinsicWithChainAndGlue(SelectionDAG &DAG, SDValue Op,
1634 unsigned Opcode) {
1635 // Copy all operands except the intrinsic ID.
1636 unsigned NumOps = Op.getNumOperands();
1637 SmallVector<SDValue, 6> Ops;
1638 Ops.reserve(NumOps - 1);
1639 Ops.push_back(Op.getOperand(0));
1640 for (unsigned I = 2; I < NumOps; ++I)
1641 Ops.push_back(Op.getOperand(I));
1642
1643 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
1644 SDVTList RawVTs = DAG.getVTList(MVT::Other, MVT::Glue);
1645 SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1646 SDValue OldChain = SDValue(Op.getNode(), 1);
1647 SDValue NewChain = SDValue(Intr.getNode(), 0);
1648 DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain);
1649 return Intr;
1650}
1651
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001652// Emit an intrinsic with a glued value instead of its CC result.
1653static SDValue emitIntrinsicWithGlue(SelectionDAG &DAG, SDValue Op,
1654 unsigned Opcode) {
1655 // Copy all operands except the intrinsic ID.
1656 unsigned NumOps = Op.getNumOperands();
1657 SmallVector<SDValue, 6> Ops;
1658 Ops.reserve(NumOps - 1);
1659 for (unsigned I = 1; I < NumOps; ++I)
1660 Ops.push_back(Op.getOperand(I));
1661
1662 if (Op->getNumValues() == 1)
1663 return DAG.getNode(Opcode, SDLoc(Op), MVT::Glue, Ops);
1664 assert(Op->getNumValues() == 2 && "Expected exactly one non-CC result");
1665 SDVTList RawVTs = DAG.getVTList(Op->getValueType(0), MVT::Glue);
1666 return DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1667}
1668
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001669// CC is a comparison that will be implemented using an integer or
1670// floating-point comparison. Return the condition code mask for
1671// a branch on true. In the integer case, CCMASK_CMP_UO is set for
1672// unsigned comparisons and clear for signed ones. In the floating-point
1673// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1674static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1675#define CONV(X) \
1676 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1677 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1678 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1679
1680 switch (CC) {
1681 default:
1682 llvm_unreachable("Invalid integer condition!");
1683
1684 CONV(EQ);
1685 CONV(NE);
1686 CONV(GT);
1687 CONV(GE);
1688 CONV(LT);
1689 CONV(LE);
1690
1691 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1692 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1693 }
1694#undef CONV
1695}
1696
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001697// Return a sequence for getting a 1 from an IPM result when CC has a
1698// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1699// The handling of CC values outside CCValid doesn't matter.
1700static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1701 // Deal with cases where the result can be taken directly from a bit
1702 // of the IPM result.
1703 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1704 return IPMConversion(0, 0, SystemZ::IPM_CC);
1705 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1706 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1707
1708 // Deal with cases where we can add a value to force the sign bit
1709 // to contain the right value. Putting the bit in 31 means we can
1710 // use SRL rather than RISBG(L), and also makes it easier to get a
1711 // 0/-1 value, so it has priority over the other tests below.
1712 //
1713 // These sequences rely on the fact that the upper two bits of the
1714 // IPM result are zero.
1715 uint64_t TopBit = uint64_t(1) << 31;
1716 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1717 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1718 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1719 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1720 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1721 | SystemZ::CCMASK_1
1722 | SystemZ::CCMASK_2)))
1723 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1724 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1725 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1726 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1727 | SystemZ::CCMASK_2
1728 | SystemZ::CCMASK_3)))
1729 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1730
1731 // Next try inverting the value and testing a bit. 0/1 could be
1732 // handled this way too, but we dealt with that case above.
1733 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1734 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1735
1736 // Handle cases where adding a value forces a non-sign bit to contain
1737 // the right value.
1738 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1739 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1740 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1741 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1742
Alp Tokercb402912014-01-24 17:20:08 +00001743 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001744 // can be done by inverting the low CC bit and applying one of the
1745 // sign-based extractions above.
1746 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1747 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1748 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1749 return IPMConversion(1 << SystemZ::IPM_CC,
1750 TopBit - (3 << SystemZ::IPM_CC), 31);
1751 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1752 | SystemZ::CCMASK_1
1753 | SystemZ::CCMASK_3)))
1754 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1755 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1756 | SystemZ::CCMASK_2
1757 | SystemZ::CCMASK_3)))
1758 return IPMConversion(1 << SystemZ::IPM_CC,
1759 TopBit - (1 << SystemZ::IPM_CC), 31);
1760
1761 llvm_unreachable("Unexpected CC combination");
1762}
1763
Richard Sandifordd420f732013-12-13 15:28:45 +00001764// If C can be converted to a comparison against zero, adjust the operands
Richard Sandiforda0757082013-08-01 10:29:45 +00001765// as necessary.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001766static void adjustZeroCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001767 if (C.ICmpType == SystemZICMP::UnsignedOnly)
Richard Sandiforda0757082013-08-01 10:29:45 +00001768 return;
1769
Richard Sandiford21f5d682014-03-06 11:22:58 +00001770 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
Richard Sandiforda0757082013-08-01 10:29:45 +00001771 if (!ConstOp1)
1772 return;
1773
1774 int64_t Value = ConstOp1->getSExtValue();
Richard Sandifordd420f732013-12-13 15:28:45 +00001775 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1776 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1777 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1778 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1779 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001780 C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType());
Richard Sandiforda0757082013-08-01 10:29:45 +00001781 }
1782}
1783
Richard Sandifordd420f732013-12-13 15:28:45 +00001784// If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1785// adjust the operands as necessary.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001786static void adjustSubwordCmp(SelectionDAG &DAG, const SDLoc &DL,
1787 Comparison &C) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001788 // For us to make any changes, it must a comparison between a single-use
1789 // load and a constant.
Richard Sandifordd420f732013-12-13 15:28:45 +00001790 if (!C.Op0.hasOneUse() ||
1791 C.Op0.getOpcode() != ISD::LOAD ||
1792 C.Op1.getOpcode() != ISD::Constant)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001793 return;
1794
1795 // We must have an 8- or 16-bit load.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001796 auto *Load = cast<LoadSDNode>(C.Op0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001797 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1798 if (NumBits != 8 && NumBits != 16)
1799 return;
1800
1801 // The load must be an extending one and the constant must be within the
1802 // range of the unextended value.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001803 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001804 uint64_t Value = ConstOp1->getZExtValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001805 uint64_t Mask = (1 << NumBits) - 1;
1806 if (Load->getExtensionType() == ISD::SEXTLOAD) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001807 // Make sure that ConstOp1 is in range of C.Op0.
1808 int64_t SignedValue = ConstOp1->getSExtValue();
1809 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001810 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001811 if (C.ICmpType != SystemZICMP::SignedOnly) {
1812 // Unsigned comparison between two sign-extended values is equivalent
1813 // to unsigned comparison between two zero-extended values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001814 Value &= Mask;
Richard Sandifordd420f732013-12-13 15:28:45 +00001815 } else if (NumBits == 8) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001816 // Try to treat the comparison as unsigned, so that we can use CLI.
1817 // Adjust CCMask and Value as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001818 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001819 // Test whether the high bit of the byte is set.
Richard Sandifordd420f732013-12-13 15:28:45 +00001820 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1821 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001822 // Test whether the high bit of the byte is clear.
Richard Sandifordd420f732013-12-13 15:28:45 +00001823 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001824 else
1825 // No instruction exists for this combination.
1826 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001827 C.ICmpType = SystemZICMP::UnsignedOnly;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001828 }
1829 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1830 if (Value > Mask)
1831 return;
Ulrich Weigand47f36492015-12-16 18:04:06 +00001832 // If the constant is in range, we can use any comparison.
1833 C.ICmpType = SystemZICMP::Any;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001834 } else
1835 return;
1836
1837 // Make sure that the first operand is an i32 of the right extension type.
Richard Sandifordd420f732013-12-13 15:28:45 +00001838 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1839 ISD::SEXTLOAD :
1840 ISD::ZEXTLOAD);
1841 if (C.Op0.getValueType() != MVT::i32 ||
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001842 Load->getExtensionType() != ExtType)
Justin Lebar9c375812016-07-15 18:27:10 +00001843 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32, Load->getChain(),
1844 Load->getBasePtr(), Load->getPointerInfo(),
1845 Load->getMemoryVT(), Load->getAlignment(),
1846 Load->getMemOperand()->getFlags());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001847
1848 // Make sure that the second operand is an i32 with the right value.
Richard Sandifordd420f732013-12-13 15:28:45 +00001849 if (C.Op1.getValueType() != MVT::i32 ||
1850 Value != ConstOp1->getZExtValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001851 C.Op1 = DAG.getConstant(Value, DL, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001852}
1853
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001854// Return true if Op is either an unextended load, or a load suitable
1855// for integer register-memory comparisons of type ICmpType.
1856static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001857 auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001858 if (Load) {
1859 // There are no instructions to compare a register with a memory byte.
1860 if (Load->getMemoryVT() == MVT::i8)
1861 return false;
1862 // Otherwise decide on extension type.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001863 switch (Load->getExtensionType()) {
1864 case ISD::NON_EXTLOAD:
Richard Sandiford24e597b2013-08-23 11:27:19 +00001865 return true;
1866 case ISD::SEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001867 return ICmpType != SystemZICMP::UnsignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001868 case ISD::ZEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001869 return ICmpType != SystemZICMP::SignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001870 default:
1871 break;
1872 }
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001873 }
Richard Sandiford24e597b2013-08-23 11:27:19 +00001874 return false;
1875}
1876
Richard Sandifordd420f732013-12-13 15:28:45 +00001877// Return true if it is better to swap the operands of C.
1878static bool shouldSwapCmpOperands(const Comparison &C) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001879 // Leave f128 comparisons alone, since they have no memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001880 if (C.Op0.getValueType() == MVT::f128)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001881 return false;
1882
1883 // Always keep a floating-point constant second, since comparisons with
1884 // zero can use LOAD TEST and comparisons with other constants make a
1885 // natural memory operand.
Richard Sandifordd420f732013-12-13 15:28:45 +00001886 if (isa<ConstantFPSDNode>(C.Op1))
Richard Sandiford24e597b2013-08-23 11:27:19 +00001887 return false;
1888
1889 // Never swap comparisons with zero since there are many ways to optimize
1890 // those later.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001891 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001892 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001893 return false;
1894
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001895 // Also keep natural memory operands second if the loaded value is
1896 // only used here. Several comparisons have memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001897 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001898 return false;
1899
Richard Sandiford24e597b2013-08-23 11:27:19 +00001900 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1901 // In that case we generally prefer the memory to be second.
Richard Sandifordd420f732013-12-13 15:28:45 +00001902 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001903 // The only exceptions are when the second operand is a constant and
1904 // we can use things like CHHSI.
Richard Sandifordd420f732013-12-13 15:28:45 +00001905 if (!ConstOp1)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001906 return true;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001907 // The unsigned memory-immediate instructions can handle 16-bit
1908 // unsigned integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001909 if (C.ICmpType != SystemZICMP::SignedOnly &&
1910 isUInt<16>(ConstOp1->getZExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001911 return false;
1912 // The signed memory-immediate instructions can handle 16-bit
1913 // signed integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001914 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1915 isInt<16>(ConstOp1->getSExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001916 return false;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001917 return true;
1918 }
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001919
1920 // Try to promote the use of CGFR and CLGFR.
Richard Sandifordd420f732013-12-13 15:28:45 +00001921 unsigned Opcode0 = C.Op0.getOpcode();
1922 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001923 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001924 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001925 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001926 if (C.ICmpType != SystemZICMP::SignedOnly &&
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001927 Opcode0 == ISD::AND &&
Richard Sandifordd420f732013-12-13 15:28:45 +00001928 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1929 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001930 return true;
1931
Richard Sandiford24e597b2013-08-23 11:27:19 +00001932 return false;
1933}
1934
Richard Sandiford73170f82013-12-11 11:45:08 +00001935// Return a version of comparison CC mask CCMask in which the LT and GT
1936// actions are swapped.
1937static unsigned reverseCCMask(unsigned CCMask) {
1938 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1939 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1940 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1941 (CCMask & SystemZ::CCMASK_CMP_UO));
1942}
1943
Richard Sandiford0847c452013-12-13 15:50:30 +00001944// Check whether C tests for equality between X and Y and whether X - Y
1945// or Y - X is also computed. In that case it's better to compare the
1946// result of the subtraction against zero.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001947static void adjustForSubtraction(SelectionDAG &DAG, const SDLoc &DL,
1948 Comparison &C) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001949 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1950 C.CCMask == SystemZ::CCMASK_CMP_NE) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001951 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001952 SDNode *N = *I;
1953 if (N->getOpcode() == ISD::SUB &&
1954 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1955 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1956 C.Op0 = SDValue(N, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001957 C.Op1 = DAG.getConstant(0, DL, N->getValueType(0));
Richard Sandiford0847c452013-12-13 15:50:30 +00001958 return;
1959 }
1960 }
1961 }
1962}
1963
Richard Sandifordd420f732013-12-13 15:28:45 +00001964// Check whether C compares a floating-point value with zero and if that
1965// floating-point value is also negated. In this case we can use the
1966// negation to set CC, so avoiding separate LOAD AND TEST and
1967// LOAD (NEGATIVE/COMPLEMENT) instructions.
1968static void adjustForFNeg(Comparison &C) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001969 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
Richard Sandiford73170f82013-12-11 11:45:08 +00001970 if (C1 && C1->isZero()) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001971 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford73170f82013-12-11 11:45:08 +00001972 SDNode *N = *I;
1973 if (N->getOpcode() == ISD::FNEG) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001974 C.Op0 = SDValue(N, 0);
1975 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford73170f82013-12-11 11:45:08 +00001976 return;
1977 }
1978 }
1979 }
1980}
1981
Richard Sandifordd420f732013-12-13 15:28:45 +00001982// Check whether C compares (shl X, 32) with 0 and whether X is
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001983// also sign-extended. In that case it is better to test the result
1984// of the sign extension using LTGFR.
1985//
1986// This case is important because InstCombine transforms a comparison
1987// with (sext (trunc X)) into a comparison with (shl X, 32).
Richard Sandifordd420f732013-12-13 15:28:45 +00001988static void adjustForLTGFR(Comparison &C) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001989 // Check for a comparison between (shl X, 32) and 0.
Richard Sandifordd420f732013-12-13 15:28:45 +00001990 if (C.Op0.getOpcode() == ISD::SHL &&
1991 C.Op0.getValueType() == MVT::i64 &&
1992 C.Op1.getOpcode() == ISD::Constant &&
1993 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001994 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001995 if (C1 && C1->getZExtValue() == 32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001996 SDValue ShlOp0 = C.Op0.getOperand(0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001997 // See whether X has any SIGN_EXTEND_INREG uses.
Richard Sandiford28c111e2014-03-06 11:00:15 +00001998 for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001999 SDNode *N = *I;
2000 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
2001 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00002002 C.Op0 = SDValue(N, 0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00002003 return;
2004 }
2005 }
2006 }
2007 }
2008}
2009
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002010// If C compares the truncation of an extending load, try to compare
2011// the untruncated value instead. This exposes more opportunities to
2012// reuse CC.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002013static void adjustICmpTruncate(SelectionDAG &DAG, const SDLoc &DL,
2014 Comparison &C) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002015 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
2016 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
2017 C.Op1.getOpcode() == ISD::Constant &&
2018 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002019 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002020 if (L->getMemoryVT().getStoreSizeInBits() <= C.Op0.getValueSizeInBits()) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002021 unsigned Type = L->getExtensionType();
2022 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
2023 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
2024 C.Op0 = C.Op0.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002025 C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType());
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002026 }
2027 }
2028 }
2029}
2030
Richard Sandiford030c1652013-09-13 09:09:50 +00002031// Return true if shift operation N has an in-range constant shift value.
2032// Store it in ShiftVal if so.
2033static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002034 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
Richard Sandiford030c1652013-09-13 09:09:50 +00002035 if (!Shift)
2036 return false;
2037
2038 uint64_t Amount = Shift->getZExtValue();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002039 if (Amount >= N.getValueSizeInBits())
Richard Sandiford030c1652013-09-13 09:09:50 +00002040 return false;
2041
2042 ShiftVal = Amount;
2043 return true;
2044}
2045
2046// Check whether an AND with Mask is suitable for a TEST UNDER MASK
2047// instruction and whether the CC value is descriptive enough to handle
2048// a comparison of type Opcode between the AND result and CmpVal.
2049// CCMask says which comparison result is being tested and BitSize is
2050// the number of bits in the operands. If TEST UNDER MASK can be used,
2051// return the corresponding CC mask, otherwise return 0.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002052static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
2053 uint64_t Mask, uint64_t CmpVal,
2054 unsigned ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00002055 assert(Mask != 0 && "ANDs with zero should have been removed by now");
2056
Richard Sandiford030c1652013-09-13 09:09:50 +00002057 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
2058 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
2059 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
2060 return 0;
2061
Richard Sandiford113c8702013-09-03 15:38:35 +00002062 // Work out the masks for the lowest and highest bits.
2063 unsigned HighShift = 63 - countLeadingZeros(Mask);
2064 uint64_t High = uint64_t(1) << HighShift;
2065 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
2066
2067 // Signed ordered comparisons are effectively unsigned if the sign
2068 // bit is dropped.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002069 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
Richard Sandiford113c8702013-09-03 15:38:35 +00002070
2071 // Check for equality comparisons with 0, or the equivalent.
2072 if (CmpVal == 0) {
2073 if (CCMask == SystemZ::CCMASK_CMP_EQ)
2074 return SystemZ::CCMASK_TM_ALL_0;
2075 if (CCMask == SystemZ::CCMASK_CMP_NE)
2076 return SystemZ::CCMASK_TM_SOME_1;
2077 }
Ulrich Weigand4a4d4ab2016-02-01 18:31:19 +00002078 if (EffectivelyUnsigned && CmpVal > 0 && CmpVal <= Low) {
Richard Sandiford113c8702013-09-03 15:38:35 +00002079 if (CCMask == SystemZ::CCMASK_CMP_LT)
2080 return SystemZ::CCMASK_TM_ALL_0;
2081 if (CCMask == SystemZ::CCMASK_CMP_GE)
2082 return SystemZ::CCMASK_TM_SOME_1;
2083 }
2084 if (EffectivelyUnsigned && CmpVal < Low) {
2085 if (CCMask == SystemZ::CCMASK_CMP_LE)
2086 return SystemZ::CCMASK_TM_ALL_0;
2087 if (CCMask == SystemZ::CCMASK_CMP_GT)
2088 return SystemZ::CCMASK_TM_SOME_1;
2089 }
2090
2091 // Check for equality comparisons with the mask, or the equivalent.
2092 if (CmpVal == Mask) {
2093 if (CCMask == SystemZ::CCMASK_CMP_EQ)
2094 return SystemZ::CCMASK_TM_ALL_1;
2095 if (CCMask == SystemZ::CCMASK_CMP_NE)
2096 return SystemZ::CCMASK_TM_SOME_0;
2097 }
2098 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
2099 if (CCMask == SystemZ::CCMASK_CMP_GT)
2100 return SystemZ::CCMASK_TM_ALL_1;
2101 if (CCMask == SystemZ::CCMASK_CMP_LE)
2102 return SystemZ::CCMASK_TM_SOME_0;
2103 }
2104 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
2105 if (CCMask == SystemZ::CCMASK_CMP_GE)
2106 return SystemZ::CCMASK_TM_ALL_1;
2107 if (CCMask == SystemZ::CCMASK_CMP_LT)
2108 return SystemZ::CCMASK_TM_SOME_0;
2109 }
2110
2111 // Check for ordered comparisons with the top bit.
2112 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
2113 if (CCMask == SystemZ::CCMASK_CMP_LE)
2114 return SystemZ::CCMASK_TM_MSB_0;
2115 if (CCMask == SystemZ::CCMASK_CMP_GT)
2116 return SystemZ::CCMASK_TM_MSB_1;
2117 }
2118 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
2119 if (CCMask == SystemZ::CCMASK_CMP_LT)
2120 return SystemZ::CCMASK_TM_MSB_0;
2121 if (CCMask == SystemZ::CCMASK_CMP_GE)
2122 return SystemZ::CCMASK_TM_MSB_1;
2123 }
2124
2125 // If there are just two bits, we can do equality checks for Low and High
2126 // as well.
2127 if (Mask == Low + High) {
2128 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
2129 return SystemZ::CCMASK_TM_MIXED_MSB_0;
2130 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
2131 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
2132 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
2133 return SystemZ::CCMASK_TM_MIXED_MSB_1;
2134 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
2135 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
2136 }
2137
2138 // Looks like we've exhausted our options.
2139 return 0;
2140}
2141
Richard Sandifordd420f732013-12-13 15:28:45 +00002142// See whether C can be implemented as a TEST UNDER MASK instruction.
2143// Update the arguments with the TM version if so.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002144static void adjustForTestUnderMask(SelectionDAG &DAG, const SDLoc &DL,
2145 Comparison &C) {
Richard Sandiford113c8702013-09-03 15:38:35 +00002146 // Check that we have a comparison with a constant.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002147 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00002148 if (!ConstOp1)
Richard Sandiford35b9be22013-08-28 10:31:43 +00002149 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00002150 uint64_t CmpVal = ConstOp1->getZExtValue();
Richard Sandiford35b9be22013-08-28 10:31:43 +00002151
2152 // Check whether the nonconstant input is an AND with a constant mask.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002153 Comparison NewC(C);
2154 uint64_t MaskVal;
Craig Topper062a2ba2014-04-25 05:30:21 +00002155 ConstantSDNode *Mask = nullptr;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002156 if (C.Op0.getOpcode() == ISD::AND) {
2157 NewC.Op0 = C.Op0.getOperand(0);
2158 NewC.Op1 = C.Op0.getOperand(1);
2159 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
2160 if (!Mask)
2161 return;
2162 MaskVal = Mask->getZExtValue();
2163 } else {
2164 // There is no instruction to compare with a 64-bit immediate
2165 // so use TMHH instead if possible. We need an unsigned ordered
2166 // comparison with an i64 immediate.
2167 if (NewC.Op0.getValueType() != MVT::i64 ||
2168 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
2169 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
2170 NewC.ICmpType == SystemZICMP::SignedOnly)
2171 return;
2172 // Convert LE and GT comparisons into LT and GE.
2173 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
2174 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
2175 if (CmpVal == uint64_t(-1))
2176 return;
2177 CmpVal += 1;
2178 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
2179 }
2180 // If the low N bits of Op1 are zero than the low N bits of Op0 can
2181 // be masked off without changing the result.
2182 MaskVal = -(CmpVal & -CmpVal);
2183 NewC.ICmpType = SystemZICMP::UnsignedOnly;
2184 }
Ulrich Weigandb8d76fb2015-03-30 13:46:59 +00002185 if (!MaskVal)
2186 return;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002187
Richard Sandiford113c8702013-09-03 15:38:35 +00002188 // Check whether the combination of mask, comparison value and comparison
2189 // type are suitable.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002190 unsigned BitSize = NewC.Op0.getValueSizeInBits();
Richard Sandiford030c1652013-09-13 09:09:50 +00002191 unsigned NewCCMask, ShiftVal;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002192 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2193 NewC.Op0.getOpcode() == ISD::SHL &&
2194 isSimpleShift(NewC.Op0, ShiftVal) &&
Jonas Paulsson8c336472017-06-26 13:38:27 +00002195 (MaskVal >> ShiftVal != 0) &&
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002196 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
2197 MaskVal >> ShiftVal,
Richard Sandiford030c1652013-09-13 09:09:50 +00002198 CmpVal >> ShiftVal,
2199 SystemZICMP::Any))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002200 NewC.Op0 = NewC.Op0.getOperand(0);
2201 MaskVal >>= ShiftVal;
2202 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2203 NewC.Op0.getOpcode() == ISD::SRL &&
2204 isSimpleShift(NewC.Op0, ShiftVal) &&
Jonas Paulsson8c336472017-06-26 13:38:27 +00002205 (MaskVal << ShiftVal != 0) &&
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002206 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
Richard Sandiford030c1652013-09-13 09:09:50 +00002207 MaskVal << ShiftVal,
2208 CmpVal << ShiftVal,
2209 SystemZICMP::UnsignedOnly))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002210 NewC.Op0 = NewC.Op0.getOperand(0);
2211 MaskVal <<= ShiftVal;
Richard Sandiford030c1652013-09-13 09:09:50 +00002212 } else {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002213 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
2214 NewC.ICmpType);
Richard Sandiford030c1652013-09-13 09:09:50 +00002215 if (!NewCCMask)
2216 return;
2217 }
Richard Sandiford113c8702013-09-03 15:38:35 +00002218
Richard Sandiford35b9be22013-08-28 10:31:43 +00002219 // Go ahead and make the change.
Richard Sandifordd420f732013-12-13 15:28:45 +00002220 C.Opcode = SystemZISD::TM;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002221 C.Op0 = NewC.Op0;
2222 if (Mask && Mask->getZExtValue() == MaskVal)
2223 C.Op1 = SDValue(Mask, 0);
2224 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002225 C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType());
Richard Sandifordd420f732013-12-13 15:28:45 +00002226 C.CCValid = SystemZ::CCMASK_TM;
2227 C.CCMask = NewCCMask;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002228}
2229
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002230// Return a Comparison that tests the condition-code result of intrinsic
2231// node Call against constant integer CC using comparison code Cond.
2232// Opcode is the opcode of the SystemZISD operation for the intrinsic
2233// and CCValid is the set of possible condition-code results.
2234static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode,
2235 SDValue Call, unsigned CCValid, uint64_t CC,
2236 ISD::CondCode Cond) {
2237 Comparison C(Call, SDValue());
2238 C.Opcode = Opcode;
2239 C.CCValid = CCValid;
2240 if (Cond == ISD::SETEQ)
2241 // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3.
2242 C.CCMask = CC < 4 ? 1 << (3 - CC) : 0;
2243 else if (Cond == ISD::SETNE)
2244 // ...and the inverse of that.
2245 C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1;
2246 else if (Cond == ISD::SETLT || Cond == ISD::SETULT)
2247 // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3,
2248 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002249 C.CCMask = CC < 4 ? ~0U << (4 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002250 else if (Cond == ISD::SETGE || Cond == ISD::SETUGE)
2251 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002252 C.CCMask = CC < 4 ? ~(~0U << (4 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002253 else if (Cond == ISD::SETLE || Cond == ISD::SETULE)
2254 // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true),
2255 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002256 C.CCMask = CC < 4 ? ~0U << (3 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002257 else if (Cond == ISD::SETGT || Cond == ISD::SETUGT)
2258 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002259 C.CCMask = CC < 4 ? ~(~0U << (3 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002260 else
2261 llvm_unreachable("Unexpected integer comparison type");
2262 C.CCMask &= CCValid;
2263 return C;
2264}
2265
Richard Sandifordd420f732013-12-13 15:28:45 +00002266// Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
2267static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002268 ISD::CondCode Cond, const SDLoc &DL) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002269 if (CmpOp1.getOpcode() == ISD::Constant) {
2270 uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue();
2271 unsigned Opcode, CCValid;
2272 if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
2273 CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) &&
2274 isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid))
2275 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002276 if (CmpOp0.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
2277 CmpOp0.getResNo() == CmpOp0->getNumValues() - 1 &&
2278 isIntrinsicWithCC(CmpOp0, Opcode, CCValid))
2279 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002280 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002281 Comparison C(CmpOp0, CmpOp1);
2282 C.CCMask = CCMaskForCondCode(Cond);
2283 if (C.Op0.getValueType().isFloatingPoint()) {
2284 C.CCValid = SystemZ::CCMASK_FCMP;
2285 C.Opcode = SystemZISD::FCMP;
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002286 adjustForFNeg(C);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002287 } else {
Richard Sandifordd420f732013-12-13 15:28:45 +00002288 C.CCValid = SystemZ::CCMASK_ICMP;
2289 C.Opcode = SystemZISD::ICMP;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002290 // Choose the type of comparison. Equality and inequality tests can
2291 // use either signed or unsigned comparisons. The choice also doesn't
2292 // matter if both sign bits are known to be clear. In those cases we
2293 // want to give the main isel code the freedom to choose whichever
2294 // form fits best.
Richard Sandifordd420f732013-12-13 15:28:45 +00002295 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
2296 C.CCMask == SystemZ::CCMASK_CMP_NE ||
2297 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
2298 C.ICmpType = SystemZICMP::Any;
2299 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
2300 C.ICmpType = SystemZICMP::UnsignedOnly;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002301 else
Richard Sandifordd420f732013-12-13 15:28:45 +00002302 C.ICmpType = SystemZICMP::SignedOnly;
2303 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002304 adjustZeroCmp(DAG, DL, C);
2305 adjustSubwordCmp(DAG, DL, C);
2306 adjustForSubtraction(DAG, DL, C);
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002307 adjustForLTGFR(C);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002308 adjustICmpTruncate(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002309 }
2310
Richard Sandifordd420f732013-12-13 15:28:45 +00002311 if (shouldSwapCmpOperands(C)) {
2312 std::swap(C.Op0, C.Op1);
2313 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford24e597b2013-08-23 11:27:19 +00002314 }
2315
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002316 adjustForTestUnderMask(DAG, DL, C);
Richard Sandifordd420f732013-12-13 15:28:45 +00002317 return C;
2318}
2319
2320// Emit the comparison instruction described by C.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002321static SDValue emitCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002322 if (!C.Op1.getNode()) {
2323 SDValue Op;
2324 switch (C.Op0.getOpcode()) {
2325 case ISD::INTRINSIC_W_CHAIN:
2326 Op = emitIntrinsicWithChainAndGlue(DAG, C.Op0, C.Opcode);
2327 break;
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002328 case ISD::INTRINSIC_WO_CHAIN:
2329 Op = emitIntrinsicWithGlue(DAG, C.Op0, C.Opcode);
2330 break;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002331 default:
2332 llvm_unreachable("Invalid comparison operands");
2333 }
2334 return SDValue(Op.getNode(), Op->getNumValues() - 1);
2335 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002336 if (C.Opcode == SystemZISD::ICMP)
2337 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002338 DAG.getConstant(C.ICmpType, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002339 if (C.Opcode == SystemZISD::TM) {
2340 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
2341 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
2342 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002343 DAG.getConstant(RegisterOnly, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002344 }
2345 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002346}
2347
Richard Sandiford7d86e472013-08-21 09:34:56 +00002348// Implement a 32-bit *MUL_LOHI operation by extending both operands to
2349// 64 bits. Extend is the extension type to use. Store the high part
2350// in Hi and the low part in Lo.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002351static void lowerMUL_LOHI32(SelectionDAG &DAG, const SDLoc &DL, unsigned Extend,
2352 SDValue Op0, SDValue Op1, SDValue &Hi,
2353 SDValue &Lo) {
Richard Sandiford7d86e472013-08-21 09:34:56 +00002354 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
2355 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
2356 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002357 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
2358 DAG.getConstant(32, DL, MVT::i64));
Richard Sandiford7d86e472013-08-21 09:34:56 +00002359 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
2360 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
2361}
2362
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002363// Lower a binary operation that produces two VT results, one in each
2364// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
Ulrich Weigand43579cf2017-07-05 13:17:31 +00002365// and Opcode performs the GR128 operation. Store the even register result
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002366// in Even and the odd register result in Odd.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002367static void lowerGR128Binary(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigand43579cf2017-07-05 13:17:31 +00002368 unsigned Opcode, SDValue Op0, SDValue Op1,
2369 SDValue &Even, SDValue &Odd) {
2370 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped, Op0, Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002371 bool Is32Bit = is32Bit(VT);
Richard Sandifordd8163202013-09-13 09:12:44 +00002372 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
2373 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002374}
2375
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002376// Return an i32 value that is 1 if the CC value produced by Glue is
2377// in the mask CCMask and 0 otherwise. CC is known to have a value
2378// in CCValid, so other values can be ignored.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002379static SDValue emitSETCC(SelectionDAG &DAG, const SDLoc &DL, SDValue Glue,
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002380 unsigned CCValid, unsigned CCMask) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002381 IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
2382 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
2383
2384 if (Conversion.XORValue)
2385 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002386 DAG.getConstant(Conversion.XORValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002387
2388 if (Conversion.AddValue)
2389 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002390 DAG.getConstant(Conversion.AddValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002391
2392 // The SHR/AND sequence should get optimized to an RISBG.
2393 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002394 DAG.getConstant(Conversion.Bit, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002395 if (Conversion.Bit != 31)
2396 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002397 DAG.getConstant(1, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002398 return Result;
2399}
2400
Ulrich Weigandcd808232015-05-05 19:26:48 +00002401// Return the SystemISD vector comparison operation for CC, or 0 if it cannot
2402// be done directly. IsFP is true if CC is for a floating-point rather than
2403// integer comparison.
2404static unsigned getVectorComparison(ISD::CondCode CC, bool IsFP) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002405 switch (CC) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002406 case ISD::SETOEQ:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002407 case ISD::SETEQ:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002408 return IsFP ? SystemZISD::VFCMPE : SystemZISD::VICMPE;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002409
Ulrich Weigandcd808232015-05-05 19:26:48 +00002410 case ISD::SETOGE:
2411 case ISD::SETGE:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002412 return IsFP ? SystemZISD::VFCMPHE : static_cast<SystemZISD::NodeType>(0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002413
2414 case ISD::SETOGT:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002415 case ISD::SETGT:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002416 return IsFP ? SystemZISD::VFCMPH : SystemZISD::VICMPH;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002417
2418 case ISD::SETUGT:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002419 return IsFP ? static_cast<SystemZISD::NodeType>(0) : SystemZISD::VICMPHL;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002420
2421 default:
2422 return 0;
2423 }
2424}
2425
2426// Return the SystemZISD vector comparison operation for CC or its inverse,
2427// or 0 if neither can be done directly. Indicate in Invert whether the
Ulrich Weigandcd808232015-05-05 19:26:48 +00002428// result is for the inverse of CC. IsFP is true if CC is for a
2429// floating-point rather than integer comparison.
2430static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, bool IsFP,
2431 bool &Invert) {
2432 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002433 Invert = false;
2434 return Opcode;
2435 }
2436
Ulrich Weigandcd808232015-05-05 19:26:48 +00002437 CC = ISD::getSetCCInverse(CC, !IsFP);
2438 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002439 Invert = true;
2440 return Opcode;
2441 }
2442
2443 return 0;
2444}
2445
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002446// Return a v2f64 that contains the extended form of elements Start and Start+1
2447// of v4f32 value Op.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002448static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, const SDLoc &DL,
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002449 SDValue Op) {
2450 int Mask[] = { Start, -1, Start + 1, -1 };
2451 Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask);
2452 return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op);
2453}
2454
2455// Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode,
2456// producing a result of type VT.
Ulrich Weigand33435c42017-07-17 17:42:48 +00002457SDValue SystemZTargetLowering::getVectorCmp(SelectionDAG &DAG, unsigned Opcode,
2458 const SDLoc &DL, EVT VT,
2459 SDValue CmpOp0,
2460 SDValue CmpOp1) const {
2461 // There is no hardware support for v4f32 (unless we have the vector
2462 // enhancements facility 1), so extend the vector into two v2f64s
2463 // and compare those.
2464 if (CmpOp0.getValueType() == MVT::v4f32 &&
2465 !Subtarget.hasVectorEnhancements1()) {
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002466 SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0);
2467 SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0);
2468 SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1);
2469 SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1);
2470 SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1);
2471 SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1);
2472 return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes);
2473 }
2474 return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1);
2475}
2476
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002477// Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing
2478// an integer mask of type VT.
Ulrich Weigand33435c42017-07-17 17:42:48 +00002479SDValue SystemZTargetLowering::lowerVectorSETCC(SelectionDAG &DAG,
2480 const SDLoc &DL, EVT VT,
2481 ISD::CondCode CC,
2482 SDValue CmpOp0,
2483 SDValue CmpOp1) const {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002484 bool IsFP = CmpOp0.getValueType().isFloatingPoint();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002485 bool Invert = false;
2486 SDValue Cmp;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002487 switch (CC) {
2488 // Handle tests for order using (or (ogt y x) (oge x y)).
2489 case ISD::SETUO:
2490 Invert = true;
Simon Pilgrim8c4069e2017-07-07 10:07:09 +00002491 LLVM_FALLTHROUGH;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002492 case ISD::SETO: {
2493 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002494 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2495 SDValue GE = getVectorCmp(DAG, SystemZISD::VFCMPHE, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002496 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE);
2497 break;
2498 }
2499
2500 // Handle <> tests using (or (ogt y x) (ogt x y)).
2501 case ISD::SETUEQ:
2502 Invert = true;
Simon Pilgrim8c4069e2017-07-07 10:07:09 +00002503 LLVM_FALLTHROUGH;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002504 case ISD::SETONE: {
2505 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002506 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2507 SDValue GT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002508 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT);
2509 break;
2510 }
2511
2512 // Otherwise a single comparison is enough. It doesn't really
2513 // matter whether we try the inversion or the swap first, since
2514 // there are no cases where both work.
2515 default:
2516 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002517 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002518 else {
2519 CC = ISD::getSetCCSwappedOperands(CC);
2520 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002521 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002522 else
2523 llvm_unreachable("Unhandled comparison");
2524 }
2525 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002526 }
2527 if (Invert) {
2528 SDValue Mask = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2529 DAG.getConstant(65535, DL, MVT::i32));
2530 Mask = DAG.getNode(ISD::BITCAST, DL, VT, Mask);
2531 Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask);
2532 }
2533 return Cmp;
2534}
2535
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002536SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
2537 SelectionDAG &DAG) const {
2538 SDValue CmpOp0 = Op.getOperand(0);
2539 SDValue CmpOp1 = Op.getOperand(1);
2540 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
2541 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002542 EVT VT = Op.getValueType();
2543 if (VT.isVector())
2544 return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002545
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002546 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002547 SDValue Glue = emitCmp(DAG, DL, C);
2548 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002549}
2550
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002551SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002552 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2553 SDValue CmpOp0 = Op.getOperand(2);
2554 SDValue CmpOp1 = Op.getOperand(3);
2555 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002556 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002557
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002558 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002559 SDValue Glue = emitCmp(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002560 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002561 Op.getOperand(0), DAG.getConstant(C.CCValid, DL, MVT::i32),
2562 DAG.getConstant(C.CCMask, DL, MVT::i32), Dest, Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002563}
2564
Richard Sandiford57485472013-12-13 15:35:00 +00002565// Return true if Pos is CmpOp and Neg is the negative of CmpOp,
2566// allowing Pos and Neg to be wider than CmpOp.
2567static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
2568 return (Neg.getOpcode() == ISD::SUB &&
2569 Neg.getOperand(0).getOpcode() == ISD::Constant &&
2570 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
2571 Neg.getOperand(1) == Pos &&
2572 (Pos == CmpOp ||
2573 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
2574 Pos.getOperand(0) == CmpOp)));
2575}
2576
2577// Return the absolute or negative absolute of Op; IsNegative decides which.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002578static SDValue getAbsolute(SelectionDAG &DAG, const SDLoc &DL, SDValue Op,
Richard Sandiford57485472013-12-13 15:35:00 +00002579 bool IsNegative) {
2580 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
2581 if (IsNegative)
2582 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002583 DAG.getConstant(0, DL, Op.getValueType()), Op);
Richard Sandiford57485472013-12-13 15:35:00 +00002584 return Op;
2585}
2586
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002587SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
2588 SelectionDAG &DAG) const {
2589 SDValue CmpOp0 = Op.getOperand(0);
2590 SDValue CmpOp1 = Op.getOperand(1);
2591 SDValue TrueOp = Op.getOperand(2);
2592 SDValue FalseOp = Op.getOperand(3);
2593 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002594 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002595
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002596 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandiford57485472013-12-13 15:35:00 +00002597
2598 // Check for absolute and negative-absolute selections, including those
2599 // where the comparison value is sign-extended (for LPGFR and LNGFR).
2600 // This check supplements the one in DAGCombiner.
2601 if (C.Opcode == SystemZISD::ICMP &&
2602 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
2603 C.CCMask != SystemZ::CCMASK_CMP_NE &&
2604 C.Op1.getOpcode() == ISD::Constant &&
2605 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
2606 if (isAbsolute(C.Op0, TrueOp, FalseOp))
2607 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
2608 if (isAbsolute(C.Op0, FalseOp, TrueOp))
2609 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
2610 }
2611
Richard Sandifordd420f732013-12-13 15:28:45 +00002612 SDValue Glue = emitCmp(DAG, DL, C);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002613
2614 // Special case for handling -1/0 results. The shifts we use here
2615 // should get optimized with the IPM conversion sequence.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002616 auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
2617 auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002618 if (TrueC && FalseC) {
2619 int64_t TrueVal = TrueC->getSExtValue();
2620 int64_t FalseVal = FalseC->getSExtValue();
2621 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
2622 // Invert the condition if we want -1 on false.
2623 if (TrueVal == 0)
Richard Sandifordd420f732013-12-13 15:28:45 +00002624 C.CCMask ^= C.CCValid;
2625 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002626 EVT VT = Op.getValueType();
2627 // Extend the result to VT. Upper bits are ignored.
2628 if (!is32Bit(VT))
2629 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
2630 // Sign-extend from the low bit.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002631 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002632 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
2633 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
2634 }
2635 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002636
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002637 SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, DL, MVT::i32),
2638 DAG.getConstant(C.CCMask, DL, MVT::i32), Glue};
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002639
2640 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00002641 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002642}
2643
2644SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
2645 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002646 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002647 const GlobalValue *GV = Node->getGlobal();
2648 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002649 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopher93bf97c2014-06-27 07:38:01 +00002650 CodeModel::Model CM = DAG.getTarget().getCodeModel();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002651
2652 SDValue Result;
Rafael Espindola3beef8d2016-06-27 23:15:57 +00002653 if (Subtarget.isPC32DBLSymbol(GV, CM)) {
Richard Sandiford54b36912013-09-27 15:14:04 +00002654 // Assign anchors at 1<<12 byte boundaries.
2655 uint64_t Anchor = Offset & ~uint64_t(0xfff);
2656 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
2657 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2658
2659 // The offset can be folded into the address if it is aligned to a halfword.
2660 Offset -= Anchor;
2661 if (Offset != 0 && (Offset & 1) == 0) {
2662 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
2663 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002664 Offset = 0;
2665 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002666 } else {
2667 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
2668 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2669 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Justin Lebar9c375812016-07-15 18:27:10 +00002670 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002671 }
2672
2673 // If there was a non-zero offset that we didn't fold, create an explicit
2674 // addition for it.
2675 if (Offset != 0)
2676 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002677 DAG.getConstant(Offset, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002678
2679 return Result;
2680}
2681
Ulrich Weigand7db69182015-02-18 09:13:27 +00002682SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
2683 SelectionDAG &DAG,
2684 unsigned Opcode,
2685 SDValue GOTOffset) const {
2686 SDLoc DL(Node);
Mehdi Amini44ede332015-07-09 02:09:04 +00002687 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand7db69182015-02-18 09:13:27 +00002688 SDValue Chain = DAG.getEntryNode();
2689 SDValue Glue;
2690
2691 // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
2692 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
2693 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
2694 Glue = Chain.getValue(1);
2695 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
2696 Glue = Chain.getValue(1);
2697
2698 // The first call operand is the chain and the second is the TLS symbol.
2699 SmallVector<SDValue, 8> Ops;
2700 Ops.push_back(Chain);
2701 Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
2702 Node->getValueType(0),
2703 0, 0));
2704
2705 // Add argument registers to the end of the list so that they are
2706 // known live into the call.
2707 Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
2708 Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
2709
2710 // Add a register mask operand representing the call-preserved registers.
2711 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002712 const uint32_t *Mask =
2713 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002714 assert(Mask && "Missing call preserved mask for calling convention");
2715 Ops.push_back(DAG.getRegisterMask(Mask));
2716
2717 // Glue the call to the argument copies.
2718 Ops.push_back(Glue);
2719
2720 // Emit the call.
2721 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2722 Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
2723 Glue = Chain.getValue(1);
2724
2725 // Copy the return value from %r2.
2726 return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
2727}
2728
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002729SDValue SystemZTargetLowering::lowerThreadPointer(const SDLoc &DL,
2730 SelectionDAG &DAG) const {
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002731 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002732 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002733
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002734 // The high part of the thread pointer is in access register 0.
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002735 SDValue TPHi = DAG.getCopyFromReg(Chain, DL, SystemZ::A0, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002736 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
2737
2738 // The low part of the thread pointer is in access register 1.
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002739 SDValue TPLo = DAG.getCopyFromReg(Chain, DL, SystemZ::A1, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002740 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
2741
2742 // Merge them into a single 64-bit address.
2743 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002744 DAG.getConstant(32, DL, PtrVT));
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002745 return DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
2746}
2747
2748SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
2749 SelectionDAG &DAG) const {
2750 if (DAG.getTarget().Options.EmulatedTLS)
2751 return LowerToTLSEmulatedModel(Node, DAG);
2752 SDLoc DL(Node);
2753 const GlobalValue *GV = Node->getGlobal();
2754 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2755 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
2756
2757 SDValue TP = lowerThreadPointer(DL, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002758
Ulrich Weigand7db69182015-02-18 09:13:27 +00002759 // Get the offset of GA from the thread pointer, based on the TLS model.
2760 SDValue Offset;
2761 switch (model) {
2762 case TLSModel::GeneralDynamic: {
2763 // Load the GOT offset of the tls_index (module ID / per-symbol offset).
2764 SystemZConstantPoolValue *CPV =
2765 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002766
Ulrich Weigand7db69182015-02-18 09:13:27 +00002767 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002768 Offset = DAG.getLoad(
2769 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002770 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002771
2772 // Call __tls_get_offset to retrieve the offset.
2773 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
2774 break;
2775 }
2776
2777 case TLSModel::LocalDynamic: {
2778 // Load the GOT offset of the module ID.
2779 SystemZConstantPoolValue *CPV =
2780 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
2781
2782 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002783 Offset = DAG.getLoad(
2784 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002785 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002786
2787 // Call __tls_get_offset to retrieve the module base offset.
2788 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
2789
2790 // Note: The SystemZLDCleanupPass will remove redundant computations
2791 // of the module base offset. Count total number of local-dynamic
2792 // accesses to trigger execution of that pass.
2793 SystemZMachineFunctionInfo* MFI =
2794 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
2795 MFI->incNumLocalDynamicTLSAccesses();
2796
2797 // Add the per-symbol offset.
2798 CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
2799
2800 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002801 DTPOffset = DAG.getLoad(
2802 PtrVT, DL, DAG.getEntryNode(), DTPOffset,
Justin Lebar9c375812016-07-15 18:27:10 +00002803 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002804
2805 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
2806 break;
2807 }
2808
2809 case TLSModel::InitialExec: {
2810 // Load the offset from the GOT.
2811 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2812 SystemZII::MO_INDNTPOFF);
2813 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +00002814 Offset =
2815 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Offset,
2816 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002817 break;
2818 }
2819
2820 case TLSModel::LocalExec: {
2821 // Force the offset into the constant pool and load it from there.
2822 SystemZConstantPoolValue *CPV =
2823 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
2824
2825 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002826 Offset = DAG.getLoad(
2827 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002828 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002829 break;
Ulrich Weigandb7e59092015-02-18 09:42:23 +00002830 }
Ulrich Weigand7db69182015-02-18 09:13:27 +00002831 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002832
2833 // Add the base and offset together.
2834 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
2835}
2836
2837SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
2838 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002839 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002840 const BlockAddress *BA = Node->getBlockAddress();
2841 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002842 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002843
2844 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
2845 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2846 return Result;
2847}
2848
2849SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
2850 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002851 SDLoc DL(JT);
Mehdi Amini44ede332015-07-09 02:09:04 +00002852 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002853 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
2854
2855 // Use LARL to load the address of the table.
2856 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2857}
2858
2859SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
2860 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002861 SDLoc DL(CP);
Mehdi Amini44ede332015-07-09 02:09:04 +00002862 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002863
2864 SDValue Result;
2865 if (CP->isMachineConstantPoolEntry())
2866 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002867 CP->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002868 else
2869 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002870 CP->getAlignment(), CP->getOffset());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002871
2872 // Use LARL to load the address of the constant pool entry.
2873 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2874}
2875
Ulrich Weigandf557d082016-04-04 12:44:55 +00002876SDValue SystemZTargetLowering::lowerFRAMEADDR(SDValue Op,
2877 SelectionDAG &DAG) const {
2878 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002879 MachineFrameInfo &MFI = MF.getFrameInfo();
2880 MFI.setFrameAddressIsTaken(true);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002881
2882 SDLoc DL(Op);
2883 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2884 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2885
2886 // If the back chain frame index has not been allocated yet, do so.
2887 SystemZMachineFunctionInfo *FI = MF.getInfo<SystemZMachineFunctionInfo>();
2888 int BackChainIdx = FI->getFramePointerSaveIndex();
2889 if (!BackChainIdx) {
2890 // By definition, the frame address is the address of the back chain.
Matthias Braun941a7052016-07-28 18:40:00 +00002891 BackChainIdx = MFI.CreateFixedObject(8, -SystemZMC::CallFrameSize, false);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002892 FI->setFramePointerSaveIndex(BackChainIdx);
2893 }
2894 SDValue BackChain = DAG.getFrameIndex(BackChainIdx, PtrVT);
2895
2896 // FIXME The frontend should detect this case.
2897 if (Depth > 0) {
2898 report_fatal_error("Unsupported stack frame traversal count");
2899 }
2900
2901 return BackChain;
2902}
2903
2904SDValue SystemZTargetLowering::lowerRETURNADDR(SDValue Op,
2905 SelectionDAG &DAG) const {
2906 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002907 MachineFrameInfo &MFI = MF.getFrameInfo();
2908 MFI.setReturnAddressIsTaken(true);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002909
2910 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
2911 return SDValue();
2912
2913 SDLoc DL(Op);
2914 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2915 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2916
2917 // FIXME The frontend should detect this case.
2918 if (Depth > 0) {
2919 report_fatal_error("Unsupported stack frame traversal count");
2920 }
2921
2922 // Return R14D, which has the return address. Mark it an implicit live-in.
2923 unsigned LinkReg = MF.addLiveIn(SystemZ::R14D, &SystemZ::GR64BitRegClass);
2924 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, LinkReg, PtrVT);
2925}
2926
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002927SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
2928 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002929 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002930 SDValue In = Op.getOperand(0);
2931 EVT InVT = In.getValueType();
2932 EVT ResVT = Op.getValueType();
2933
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002934 // Convert loads directly. This is normally done by DAGCombiner,
2935 // but we need this case for bitcasts that are created during lowering
2936 // and which are then lowered themselves.
2937 if (auto *LoadN = dyn_cast<LoadSDNode>(In))
Nirav Daveaa65a2b2017-04-05 15:42:48 +00002938 if (ISD::isNormalLoad(LoadN))
2939 return DAG.getLoad(ResVT, DL, LoadN->getChain(), LoadN->getBasePtr(),
2940 LoadN->getMemOperand());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002941
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002942 if (InVT == MVT::i32 && ResVT == MVT::f32) {
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002943 SDValue In64;
2944 if (Subtarget.hasHighWord()) {
2945 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
2946 MVT::i64);
2947 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
2948 MVT::i64, SDValue(U64, 0), In);
2949 } else {
2950 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
2951 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002952 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002953 }
2954 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002955 return DAG.getTargetExtractSubreg(SystemZ::subreg_r32,
Richard Sandifordd8163202013-09-13 09:12:44 +00002956 DL, MVT::f32, Out64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002957 }
2958 if (InVT == MVT::f32 && ResVT == MVT::i32) {
2959 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002960 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002961 MVT::f64, SDValue(U64, 0), In);
2962 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002963 if (Subtarget.hasHighWord())
2964 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
2965 MVT::i32, Out64);
2966 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002967 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002968 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002969 }
2970 llvm_unreachable("Unexpected bitcast combination");
2971}
2972
2973SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
2974 SelectionDAG &DAG) const {
2975 MachineFunction &MF = DAG.getMachineFunction();
2976 SystemZMachineFunctionInfo *FuncInfo =
2977 MF.getInfo<SystemZMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002978 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002979
2980 SDValue Chain = Op.getOperand(0);
2981 SDValue Addr = Op.getOperand(1);
2982 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002983 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002984
2985 // The initial values of each field.
2986 const unsigned NumFields = 4;
2987 SDValue Fields[NumFields] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002988 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT),
2989 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002990 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
2991 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
2992 };
2993
2994 // Store each field into its respective slot.
2995 SDValue MemOps[NumFields];
2996 unsigned Offset = 0;
2997 for (unsigned I = 0; I < NumFields; ++I) {
2998 SDValue FieldAddr = Addr;
2999 if (Offset != 0)
3000 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003001 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003002 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00003003 MachinePointerInfo(SV, Offset));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003004 Offset += 8;
3005 }
Craig Topper48d114b2014-04-26 18:35:24 +00003006 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003007}
3008
3009SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
3010 SelectionDAG &DAG) const {
3011 SDValue Chain = Op.getOperand(0);
3012 SDValue DstPtr = Op.getOperand(1);
3013 SDValue SrcPtr = Op.getOperand(2);
3014 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
3015 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003016 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003017
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003018 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003019 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00003020 /*isTailCall*/false,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003021 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
3022}
3023
3024SDValue SystemZTargetLowering::
3025lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003026 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003027 MachineFunction &MF = DAG.getMachineFunction();
3028 bool RealignOpt = !MF.getFunction()-> hasFnAttribute("no-realign-stack");
3029 bool StoreBackchain = MF.getFunction()->hasFnAttribute("backchain");
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003030
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003031 SDValue Chain = Op.getOperand(0);
3032 SDValue Size = Op.getOperand(1);
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003033 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003034 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003035
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003036 // If user has set the no alignment function attribute, ignore
3037 // alloca alignments.
3038 uint64_t AlignVal = (RealignOpt ?
3039 dyn_cast<ConstantSDNode>(Align)->getZExtValue() : 0);
3040
3041 uint64_t StackAlign = TFI->getStackAlignment();
3042 uint64_t RequiredAlign = std::max(AlignVal, StackAlign);
3043 uint64_t ExtraAlignSpace = RequiredAlign - StackAlign;
3044
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003045 unsigned SPReg = getStackPointerRegisterToSaveRestore();
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003046 SDValue NeededSpace = Size;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003047
3048 // Get a reference to the stack pointer.
3049 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
3050
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003051 // If we need a backchain, save it now.
3052 SDValue Backchain;
3053 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003054 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003055
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003056 // Add extra space for alignment if needed.
3057 if (ExtraAlignSpace)
3058 NeededSpace = DAG.getNode(ISD::ADD, DL, MVT::i64, NeededSpace,
Elliot Colpbc2cfc22016-07-06 18:13:11 +00003059 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003060
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003061 // Get the new stack pointer value.
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003062 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, NeededSpace);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003063
3064 // Copy the new stack pointer back.
3065 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
3066
3067 // The allocated data lives above the 160 bytes allocated for the standard
3068 // frame, plus any outgoing stack arguments. We don't know how much that
3069 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
3070 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
3071 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
3072
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003073 // Dynamically realign if needed.
3074 if (RequiredAlign > StackAlign) {
3075 Result =
3076 DAG.getNode(ISD::ADD, DL, MVT::i64, Result,
3077 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
3078 Result =
3079 DAG.getNode(ISD::AND, DL, MVT::i64, Result,
3080 DAG.getConstant(~(RequiredAlign - 1), DL, MVT::i64));
3081 }
3082
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003083 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003084 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003085
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003086 SDValue Ops[2] = { Result, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00003087 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003088}
3089
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +00003090SDValue SystemZTargetLowering::lowerGET_DYNAMIC_AREA_OFFSET(
3091 SDValue Op, SelectionDAG &DAG) const {
3092 SDLoc DL(Op);
3093
3094 return DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
3095}
3096
Richard Sandiford7d86e472013-08-21 09:34:56 +00003097SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
3098 SelectionDAG &DAG) const {
3099 EVT VT = Op.getValueType();
3100 SDLoc DL(Op);
3101 SDValue Ops[2];
3102 if (is32Bit(VT))
3103 // Just do a normal 64-bit multiplication and extract the results.
3104 // We define this so that it can be used for constant division.
3105 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
3106 Op.getOperand(1), Ops[1], Ops[0]);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +00003107 else if (Subtarget.hasMiscellaneousExtensions2())
3108 // SystemZISD::SMUL_LOHI returns the low result in the odd register and
3109 // the high result in the even register. ISD::SMUL_LOHI is defined to
3110 // return the low half first, so the results are in reverse order.
3111 lowerGR128Binary(DAG, DL, VT, SystemZISD::SMUL_LOHI,
3112 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003113 else {
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003114 // Do a full 128-bit multiplication based on SystemZISD::UMUL_LOHI:
Richard Sandiford7d86e472013-08-21 09:34:56 +00003115 //
3116 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
3117 //
3118 // but using the fact that the upper halves are either all zeros
3119 // or all ones:
3120 //
3121 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
3122 //
3123 // and grouping the right terms together since they are quicker than the
3124 // multiplication:
3125 //
3126 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003127 SDValue C63 = DAG.getConstant(63, DL, MVT::i64);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003128 SDValue LL = Op.getOperand(0);
3129 SDValue RL = Op.getOperand(1);
3130 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
3131 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003132 // SystemZISD::UMUL_LOHI returns the low result in the odd register and
3133 // the high result in the even register. ISD::SMUL_LOHI is defined to
3134 // return the low half first, so the results are in reverse order.
3135 lowerGR128Binary(DAG, DL, VT, SystemZISD::UMUL_LOHI,
Richard Sandiford7d86e472013-08-21 09:34:56 +00003136 LL, RL, Ops[1], Ops[0]);
3137 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
3138 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
3139 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
3140 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
3141 }
Craig Topper64941d92014-04-27 19:20:57 +00003142 return DAG.getMergeValues(Ops, DL);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003143}
3144
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003145SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
3146 SelectionDAG &DAG) const {
3147 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003148 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003149 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00003150 if (is32Bit(VT))
3151 // Just do a normal 64-bit multiplication and extract the results.
3152 // We define this so that it can be used for constant division.
3153 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
3154 Op.getOperand(1), Ops[1], Ops[0]);
3155 else
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003156 // SystemZISD::UMUL_LOHI returns the low result in the odd register and
3157 // the high result in the even register. ISD::UMUL_LOHI is defined to
3158 // return the low half first, so the results are in reverse order.
3159 lowerGR128Binary(DAG, DL, VT, SystemZISD::UMUL_LOHI,
Richard Sandiford7d86e472013-08-21 09:34:56 +00003160 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003161 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003162}
3163
3164SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
3165 SelectionDAG &DAG) const {
3166 SDValue Op0 = Op.getOperand(0);
3167 SDValue Op1 = Op.getOperand(1);
3168 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003169 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003170
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003171 // We use DSGF for 32-bit division. This means the first operand must
3172 // always be 64-bit, and the second operand should be 32-bit whenever
3173 // that is possible, to improve performance.
3174 if (is32Bit(VT))
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003175 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003176 else if (DAG.ComputeNumSignBits(Op1) > 32)
Richard Sandiforde6e78852013-07-02 15:40:22 +00003177 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003178
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003179 // DSG(F) returns the remainder in the even register and the
3180 // quotient in the odd register.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003181 SDValue Ops[2];
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003182 lowerGR128Binary(DAG, DL, VT, SystemZISD::SDIVREM, Op0, Op1, Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003183 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003184}
3185
3186SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
3187 SelectionDAG &DAG) const {
3188 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003189 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003190
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003191 // DL(G) returns the remainder in the even register and the
3192 // quotient in the odd register.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003193 SDValue Ops[2];
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003194 lowerGR128Binary(DAG, DL, VT, SystemZISD::UDIVREM,
3195 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003196 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003197}
3198
3199SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
3200 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
3201
3202 // Get the known-zero masks for each operand.
3203 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
Craig Topperd0af7e82017-04-28 05:31:46 +00003204 KnownBits Known[2];
3205 DAG.computeKnownBits(Ops[0], Known[0]);
3206 DAG.computeKnownBits(Ops[1], Known[1]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003207
3208 // See if the upper 32 bits of one operand and the lower 32 bits of the
3209 // other are known zero. They are the low and high operands respectively.
Craig Topperd0af7e82017-04-28 05:31:46 +00003210 uint64_t Masks[] = { Known[0].Zero.getZExtValue(),
3211 Known[1].Zero.getZExtValue() };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003212 unsigned High, Low;
3213 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
3214 High = 1, Low = 0;
3215 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
3216 High = 0, Low = 1;
3217 else
3218 return Op;
3219
3220 SDValue LowOp = Ops[Low];
3221 SDValue HighOp = Ops[High];
3222
3223 // If the high part is a constant, we're better off using IILH.
3224 if (HighOp.getOpcode() == ISD::Constant)
3225 return Op;
3226
3227 // If the low part is a constant that is outside the range of LHI,
3228 // then we're better off using IILF.
3229 if (LowOp.getOpcode() == ISD::Constant) {
3230 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
3231 if (!isInt<16>(Value))
3232 return Op;
3233 }
3234
3235 // Check whether the high part is an AND that doesn't change the
3236 // high 32 bits and just masks out low bits. We can skip it if so.
3237 if (HighOp.getOpcode() == ISD::AND &&
3238 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
Richard Sandifordccc2a7c2013-12-03 11:01:54 +00003239 SDValue HighOp0 = HighOp.getOperand(0);
3240 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
3241 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
3242 HighOp = HighOp0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003243 }
3244
3245 // Take advantage of the fact that all GR32 operations only change the
3246 // low 32 bits by truncating Low to an i32 and inserting it directly
3247 // using a subreg. The interesting cases are those where the truncation
3248 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003249 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003250 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
Richard Sandiford87a44362013-09-30 10:28:35 +00003251 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00003252 MVT::i64, HighOp, Low32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003253}
3254
Ulrich Weigandb4012182015-03-31 12:56:33 +00003255SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
3256 SelectionDAG &DAG) const {
3257 EVT VT = Op.getValueType();
Ulrich Weigandb4012182015-03-31 12:56:33 +00003258 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003259 Op = Op.getOperand(0);
3260
3261 // Handle vector types via VPOPCT.
3262 if (VT.isVector()) {
3263 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op);
3264 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00003265 switch (VT.getScalarSizeInBits()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003266 case 8:
3267 break;
3268 case 16: {
3269 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
3270 SDValue Shift = DAG.getConstant(8, DL, MVT::i32);
3271 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift);
3272 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3273 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift);
3274 break;
3275 }
3276 case 32: {
3277 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3278 DAG.getConstant(0, DL, MVT::i32));
3279 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3280 break;
3281 }
3282 case 64: {
3283 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3284 DAG.getConstant(0, DL, MVT::i32));
3285 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp);
3286 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3287 break;
3288 }
3289 default:
3290 llvm_unreachable("Unexpected type");
3291 }
3292 return Op;
3293 }
Ulrich Weigandb4012182015-03-31 12:56:33 +00003294
3295 // Get the known-zero mask for the operand.
Craig Topperd0af7e82017-04-28 05:31:46 +00003296 KnownBits Known;
3297 DAG.computeKnownBits(Op, Known);
3298 unsigned NumSignificantBits = (~Known.Zero).getActiveBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003299 if (NumSignificantBits == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003300 return DAG.getConstant(0, DL, VT);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003301
3302 // Skip known-zero high parts of the operand.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003303 int64_t OrigBitSize = VT.getSizeInBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003304 int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits);
3305 BitSize = std::min(BitSize, OrigBitSize);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003306
3307 // The POPCNT instruction counts the number of bits in each byte.
3308 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op);
3309 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op);
3310 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
3311
3312 // Add up per-byte counts in a binary tree. All bits of Op at
3313 // position larger than BitSize remain zero throughout.
3314 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003315 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003316 if (BitSize != OrigBitSize)
3317 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003318 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003319 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3320 }
3321
3322 // Extract overall result from high byte.
3323 if (BitSize > 8)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003324 Op = DAG.getNode(ISD::SRL, DL, VT, Op,
3325 DAG.getConstant(BitSize - 8, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003326
3327 return Op;
3328}
3329
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003330SDValue SystemZTargetLowering::lowerATOMIC_FENCE(SDValue Op,
3331 SelectionDAG &DAG) const {
3332 SDLoc DL(Op);
3333 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
3334 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003335 SyncScope::ID FenceSSID = static_cast<SyncScope::ID>(
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003336 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
3337
3338 // The only fence that needs an instruction is a sequentially-consistent
3339 // cross-thread fence.
JF Bastien800f87a2016-04-06 21:19:33 +00003340 if (FenceOrdering == AtomicOrdering::SequentiallyConsistent &&
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003341 FenceSSID == SyncScope::System) {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003342 return SDValue(DAG.getMachineNode(SystemZ::Serialize, DL, MVT::Other,
JF Bastien800f87a2016-04-06 21:19:33 +00003343 Op.getOperand(0)),
3344 0);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003345 }
3346
3347 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
3348 return DAG.getNode(SystemZISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0));
3349}
3350
Ulrich Weigandeaf00512017-06-23 15:56:14 +00003351// Op is an atomic load. Lower it into a serialization followed
3352// by 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());
Ulrich Weigandeaf00512017-06-23 15:56:14 +00003356 SDValue Chain = SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op),
3357 MVT::Other, Node->getChain()), 0);
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003358 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
Ulrich Weigandeaf00512017-06-23 15:56:14 +00003359 Chain, Node->getBasePtr(),
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003360 Node->getMemoryVT(), Node->getMemOperand());
3361}
3362
3363// Op is an atomic store. Lower it into a normal volatile store followed
3364// by a serialization.
3365SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
3366 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003367 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003368 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
3369 Node->getBasePtr(), Node->getMemoryVT(),
3370 Node->getMemOperand());
3371 return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other,
3372 Chain), 0);
3373}
3374
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003375// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
3376// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003377SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
3378 SelectionDAG &DAG,
3379 unsigned Opcode) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003380 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003381
3382 // 32-bit operations need no code outside the main loop.
3383 EVT NarrowVT = Node->getMemoryVT();
3384 EVT WideVT = MVT::i32;
3385 if (NarrowVT == WideVT)
3386 return Op;
3387
3388 int64_t BitSize = NarrowVT.getSizeInBits();
3389 SDValue ChainIn = Node->getChain();
3390 SDValue Addr = Node->getBasePtr();
3391 SDValue Src2 = Node->getVal();
3392 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003393 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003394 EVT PtrVT = Addr.getValueType();
3395
3396 // Convert atomic subtracts of constants into additions.
3397 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
Richard Sandiford21f5d682014-03-06 11:22:58 +00003398 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003399 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003400 Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003401 }
3402
3403 // Get the address of the containing word.
3404 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003405 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003406
3407 // Get the number of bits that the word must be rotated left in order
3408 // to bring the field to the top bits of a GR32.
3409 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003410 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003411 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3412
3413 // Get the complementing shift amount, for rotating a field in the top
3414 // bits back to its proper position.
3415 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003416 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003417
3418 // Extend the source operand to 32 bits and prepare it for the inner loop.
3419 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
3420 // operations require the source to be shifted in advance. (This shift
3421 // can be folded if the source is constant.) For AND and NAND, the lower
3422 // bits must be set, while for other opcodes they should be left clear.
3423 if (Opcode != SystemZISD::ATOMIC_SWAPW)
3424 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003425 DAG.getConstant(32 - BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003426 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
3427 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
3428 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003429 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003430
3431 // Construct the ATOMIC_LOADW_* node.
3432 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3433 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003434 DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003435 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003436 NarrowVT, MMO);
3437
3438 // Rotate the result of the final CS so that the field is in the lower
3439 // bits of a GR32, then truncate it.
3440 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003441 DAG.getConstant(BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003442 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
3443
3444 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00003445 return DAG.getMergeValues(RetOps, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003446}
3447
Richard Sandiford41350a52013-12-24 15:18:04 +00003448// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
Richard Sandiford002019a2013-12-24 15:22:39 +00003449// into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
Richard Sandiford41350a52013-12-24 15:18:04 +00003450// operations into additions.
3451SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
3452 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003453 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandiford41350a52013-12-24 15:18:04 +00003454 EVT MemVT = Node->getMemoryVT();
3455 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
3456 // A full-width operation.
3457 assert(Op.getValueType() == MemVT && "Mismatched VTs");
3458 SDValue Src2 = Node->getVal();
3459 SDValue NegSrc2;
3460 SDLoc DL(Src2);
3461
Richard Sandiford21f5d682014-03-06 11:22:58 +00003462 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
Richard Sandiford41350a52013-12-24 15:18:04 +00003463 // Use an addition if the operand is constant and either LAA(G) is
3464 // available or the negative value is in the range of A(G)FHI.
3465 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
Eric Christopher93bf97c2014-06-27 07:38:01 +00003466 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003467 NegSrc2 = DAG.getConstant(Value, DL, MemVT);
Eric Christopher93bf97c2014-06-27 07:38:01 +00003468 } else if (Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00003469 // Use LAA(G) if available.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003470 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT),
Richard Sandiford41350a52013-12-24 15:18:04 +00003471 Src2);
3472
3473 if (NegSrc2.getNode())
3474 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
3475 Node->getChain(), Node->getBasePtr(), NegSrc2,
Konstantin Zhuravlyov8ea02462016-10-15 22:01:18 +00003476 Node->getMemOperand());
Richard Sandiford41350a52013-12-24 15:18:04 +00003477
3478 // Use the node as-is.
3479 return Op;
3480 }
3481
3482 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
3483}
3484
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003485// Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
3486// into a fullword ATOMIC_CMP_SWAPW operation.
3487SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
3488 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003489 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003490
3491 // We have native support for 32-bit compare and swap.
3492 EVT NarrowVT = Node->getMemoryVT();
3493 EVT WideVT = MVT::i32;
3494 if (NarrowVT == WideVT)
3495 return Op;
3496
3497 int64_t BitSize = NarrowVT.getSizeInBits();
3498 SDValue ChainIn = Node->getOperand(0);
3499 SDValue Addr = Node->getOperand(1);
3500 SDValue CmpVal = Node->getOperand(2);
3501 SDValue SwapVal = Node->getOperand(3);
3502 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003503 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003504 EVT PtrVT = Addr.getValueType();
3505
3506 // Get the address of the containing word.
3507 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003508 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003509
3510 // Get the number of bits that the word must be rotated left in order
3511 // to bring the field to the top bits of a GR32.
3512 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003513 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003514 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3515
3516 // Get the complementing shift amount, for rotating a field in the top
3517 // bits back to its proper position.
3518 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003519 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003520
3521 // Construct the ATOMIC_CMP_SWAPW node.
3522 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3523 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003524 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003525 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003526 VTList, Ops, NarrowVT, MMO);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003527 return AtomicOp;
3528}
3529
3530SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
3531 SelectionDAG &DAG) const {
3532 MachineFunction &MF = DAG.getMachineFunction();
3533 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003534 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003535 SystemZ::R15D, Op.getValueType());
3536}
3537
3538SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
3539 SelectionDAG &DAG) const {
3540 MachineFunction &MF = DAG.getMachineFunction();
3541 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003542 bool StoreBackchain = MF.getFunction()->hasFnAttribute("backchain");
3543
3544 SDValue Chain = Op.getOperand(0);
3545 SDValue NewSP = Op.getOperand(1);
3546 SDValue Backchain;
3547 SDLoc DL(Op);
3548
3549 if (StoreBackchain) {
3550 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, MVT::i64);
Justin Lebar9c375812016-07-15 18:27:10 +00003551 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003552 }
3553
3554 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R15D, NewSP);
3555
3556 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003557 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003558
3559 return Chain;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003560}
3561
Richard Sandiford03481332013-08-23 11:36:42 +00003562SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
3563 SelectionDAG &DAG) const {
3564 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3565 if (!IsData)
3566 // Just preserve the chain.
3567 return Op.getOperand(0);
3568
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003569 SDLoc DL(Op);
Richard Sandiford03481332013-08-23 11:36:42 +00003570 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
3571 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
Richard Sandiford21f5d682014-03-06 11:22:58 +00003572 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
Richard Sandiford03481332013-08-23 11:36:42 +00003573 SDValue Ops[] = {
3574 Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003575 DAG.getConstant(Code, DL, MVT::i32),
Richard Sandiford03481332013-08-23 11:36:42 +00003576 Op.getOperand(1)
3577 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003578 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003579 Node->getVTList(), Ops,
Richard Sandiford03481332013-08-23 11:36:42 +00003580 Node->getMemoryVT(), Node->getMemOperand());
3581}
3582
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003583// Return an i32 that contains the value of CC immediately after After,
3584// whose final operand must be MVT::Glue.
3585static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003586 SDLoc DL(After);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003587 SDValue Glue = SDValue(After, After->getNumValues() - 1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003588 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
3589 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
3590 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003591}
3592
3593SDValue
3594SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
3595 SelectionDAG &DAG) const {
3596 unsigned Opcode, CCValid;
3597 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) {
3598 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
3599 SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode);
3600 SDValue CC = getCCResult(DAG, Glued.getNode());
3601 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC);
3602 return SDValue();
3603 }
3604
3605 return SDValue();
3606}
3607
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003608SDValue
3609SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
3610 SelectionDAG &DAG) const {
3611 unsigned Opcode, CCValid;
3612 if (isIntrinsicWithCC(Op, Opcode, CCValid)) {
3613 SDValue Glued = emitIntrinsicWithGlue(DAG, Op, Opcode);
3614 SDValue CC = getCCResult(DAG, Glued.getNode());
3615 if (Op->getNumValues() == 1)
3616 return CC;
3617 assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result");
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00003618 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), Glued,
3619 CC);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003620 }
3621
3622 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3623 switch (Id) {
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00003624 case Intrinsic::thread_pointer:
3625 return lowerThreadPointer(SDLoc(Op), DAG);
3626
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003627 case Intrinsic::s390_vpdi:
3628 return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(),
3629 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3630
3631 case Intrinsic::s390_vperm:
3632 return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(),
3633 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3634
3635 case Intrinsic::s390_vuphb:
3636 case Intrinsic::s390_vuphh:
3637 case Intrinsic::s390_vuphf:
3638 return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(),
3639 Op.getOperand(1));
3640
3641 case Intrinsic::s390_vuplhb:
3642 case Intrinsic::s390_vuplhh:
3643 case Intrinsic::s390_vuplhf:
3644 return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(),
3645 Op.getOperand(1));
3646
3647 case Intrinsic::s390_vuplb:
3648 case Intrinsic::s390_vuplhw:
3649 case Intrinsic::s390_vuplf:
3650 return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(),
3651 Op.getOperand(1));
3652
3653 case Intrinsic::s390_vupllb:
3654 case Intrinsic::s390_vupllh:
3655 case Intrinsic::s390_vupllf:
3656 return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(),
3657 Op.getOperand(1));
3658
3659 case Intrinsic::s390_vsumb:
3660 case Intrinsic::s390_vsumh:
3661 case Intrinsic::s390_vsumgh:
3662 case Intrinsic::s390_vsumgf:
3663 case Intrinsic::s390_vsumqf:
3664 case Intrinsic::s390_vsumqg:
3665 return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(),
3666 Op.getOperand(1), Op.getOperand(2));
3667 }
3668
3669 return SDValue();
3670}
3671
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003672namespace {
3673// Says that SystemZISD operation Opcode can be used to perform the equivalent
3674// of a VPERM with permute vector Bytes. If Opcode takes three operands,
3675// Operand is the constant third operand, otherwise it is the number of
3676// bytes in each element of the result.
3677struct Permute {
3678 unsigned Opcode;
3679 unsigned Operand;
3680 unsigned char Bytes[SystemZ::VectorBytes];
3681};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003682}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003683
3684static const Permute PermuteForms[] = {
3685 // VMRHG
3686 { SystemZISD::MERGE_HIGH, 8,
3687 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } },
3688 // VMRHF
3689 { SystemZISD::MERGE_HIGH, 4,
3690 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
3691 // VMRHH
3692 { SystemZISD::MERGE_HIGH, 2,
3693 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
3694 // VMRHB
3695 { SystemZISD::MERGE_HIGH, 1,
3696 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
3697 // VMRLG
3698 { SystemZISD::MERGE_LOW, 8,
3699 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } },
3700 // VMRLF
3701 { SystemZISD::MERGE_LOW, 4,
3702 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
3703 // VMRLH
3704 { SystemZISD::MERGE_LOW, 2,
3705 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
3706 // VMRLB
3707 { SystemZISD::MERGE_LOW, 1,
3708 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
3709 // VPKG
3710 { SystemZISD::PACK, 4,
3711 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } },
3712 // VPKF
3713 { SystemZISD::PACK, 2,
3714 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
3715 // VPKH
3716 { SystemZISD::PACK, 1,
3717 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
3718 // VPDI V1, V2, 4 (low half of V1, high half of V2)
3719 { SystemZISD::PERMUTE_DWORDS, 4,
3720 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } },
3721 // VPDI V1, V2, 1 (high half of V1, low half of V2)
3722 { SystemZISD::PERMUTE_DWORDS, 1,
3723 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } }
3724};
3725
3726// Called after matching a vector shuffle against a particular pattern.
3727// Both the original shuffle and the pattern have two vector operands.
3728// OpNos[0] is the operand of the original shuffle that should be used for
3729// operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything.
3730// OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and
3731// set OpNo0 and OpNo1 to the shuffle operands that should actually be used
3732// for operands 0 and 1 of the pattern.
3733static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) {
3734 if (OpNos[0] < 0) {
3735 if (OpNos[1] < 0)
3736 return false;
3737 OpNo0 = OpNo1 = OpNos[1];
3738 } else if (OpNos[1] < 0) {
3739 OpNo0 = OpNo1 = OpNos[0];
3740 } else {
3741 OpNo0 = OpNos[0];
3742 OpNo1 = OpNos[1];
3743 }
3744 return true;
3745}
3746
3747// Bytes is a VPERM-like permute vector, except that -1 is used for
3748// undefined bytes. Return true if the VPERM can be implemented using P.
3749// When returning true set OpNo0 to the VPERM operand that should be
3750// used for operand 0 of P and likewise OpNo1 for operand 1 of P.
3751//
3752// For example, if swapping the VPERM operands allows P to match, OpNo0
3753// will be 1 and OpNo1 will be 0. If instead Bytes only refers to one
3754// operand, but rewriting it to use two duplicated operands allows it to
3755// match P, then OpNo0 and OpNo1 will be the same.
3756static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P,
3757 unsigned &OpNo0, unsigned &OpNo1) {
3758 int OpNos[] = { -1, -1 };
3759 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
3760 int Elt = Bytes[I];
3761 if (Elt >= 0) {
3762 // Make sure that the two permute vectors use the same suboperand
3763 // byte number. Only the operand numbers (the high bits) are
3764 // allowed to differ.
3765 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1))
3766 return false;
3767 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes;
3768 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes;
3769 // Make sure that the operand mappings are consistent with previous
3770 // elements.
3771 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3772 return false;
3773 OpNos[ModelOpNo] = RealOpNo;
3774 }
3775 }
3776 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3777}
3778
3779// As above, but search for a matching permute.
3780static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes,
3781 unsigned &OpNo0, unsigned &OpNo1) {
3782 for (auto &P : PermuteForms)
3783 if (matchPermute(Bytes, P, OpNo0, OpNo1))
3784 return &P;
3785 return nullptr;
3786}
3787
3788// Bytes is a VPERM-like permute vector, except that -1 is used for
3789// undefined bytes. This permute is an operand of an outer permute.
3790// See whether redistributing the -1 bytes gives a shuffle that can be
3791// implemented using P. If so, set Transform to a VPERM-like permute vector
3792// that, when applied to the result of P, gives the original permute in Bytes.
3793static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3794 const Permute &P,
3795 SmallVectorImpl<int> &Transform) {
3796 unsigned To = 0;
3797 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) {
3798 int Elt = Bytes[From];
3799 if (Elt < 0)
3800 // Byte number From of the result is undefined.
3801 Transform[From] = -1;
3802 else {
3803 while (P.Bytes[To] != Elt) {
3804 To += 1;
3805 if (To == SystemZ::VectorBytes)
3806 return false;
3807 }
3808 Transform[From] = To;
3809 }
3810 }
3811 return true;
3812}
3813
3814// As above, but search for a matching permute.
3815static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3816 SmallVectorImpl<int> &Transform) {
3817 for (auto &P : PermuteForms)
3818 if (matchDoublePermute(Bytes, P, Transform))
3819 return &P;
3820 return nullptr;
3821}
3822
3823// Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask,
3824// as if it had type vNi8.
3825static void getVPermMask(ShuffleVectorSDNode *VSN,
3826 SmallVectorImpl<int> &Bytes) {
3827 EVT VT = VSN->getValueType(0);
3828 unsigned NumElements = VT.getVectorNumElements();
3829 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3830 Bytes.resize(NumElements * BytesPerElement, -1);
3831 for (unsigned I = 0; I < NumElements; ++I) {
3832 int Index = VSN->getMaskElt(I);
3833 if (Index >= 0)
3834 for (unsigned J = 0; J < BytesPerElement; ++J)
3835 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
3836 }
3837}
3838
3839// Bytes is a VPERM-like permute vector, except that -1 is used for
3840// undefined bytes. See whether bytes [Start, Start + BytesPerElement) of
3841// the result come from a contiguous sequence of bytes from one input.
3842// Set Base to the selector for the first byte if so.
3843static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start,
3844 unsigned BytesPerElement, int &Base) {
3845 Base = -1;
3846 for (unsigned I = 0; I < BytesPerElement; ++I) {
3847 if (Bytes[Start + I] >= 0) {
3848 unsigned Elem = Bytes[Start + I];
3849 if (Base < 0) {
3850 Base = Elem - I;
3851 // Make sure the bytes would come from one input operand.
3852 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size())
3853 return false;
3854 } else if (unsigned(Base) != Elem - I)
3855 return false;
3856 }
3857 }
3858 return true;
3859}
3860
3861// Bytes is a VPERM-like permute vector, except that -1 is used for
3862// undefined bytes. Return true if it can be performed using VSLDI.
3863// When returning true, set StartIndex to the shift amount and OpNo0
3864// and OpNo1 to the VPERM operands that should be used as the first
3865// and second shift operand respectively.
3866static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes,
3867 unsigned &StartIndex, unsigned &OpNo0,
3868 unsigned &OpNo1) {
3869 int OpNos[] = { -1, -1 };
3870 int Shift = -1;
3871 for (unsigned I = 0; I < 16; ++I) {
3872 int Index = Bytes[I];
3873 if (Index >= 0) {
3874 int ExpectedShift = (Index - I) % SystemZ::VectorBytes;
3875 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes;
3876 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes;
3877 if (Shift < 0)
3878 Shift = ExpectedShift;
3879 else if (Shift != ExpectedShift)
3880 return false;
3881 // Make sure that the operand mappings are consistent with previous
3882 // elements.
3883 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3884 return false;
3885 OpNos[ModelOpNo] = RealOpNo;
3886 }
3887 }
3888 StartIndex = Shift;
3889 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3890}
3891
3892// Create a node that performs P on operands Op0 and Op1, casting the
3893// operands to the appropriate type. The type of the result is determined by P.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003894static SDValue getPermuteNode(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003895 const Permute &P, SDValue Op0, SDValue Op1) {
3896 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input
3897 // elements of a PACK are twice as wide as the outputs.
3898 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 :
3899 P.Opcode == SystemZISD::PACK ? P.Operand * 2 :
3900 P.Operand);
3901 // Cast both operands to the appropriate type.
3902 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8),
3903 SystemZ::VectorBytes / InBytes);
3904 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0);
3905 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1);
3906 SDValue Op;
3907 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) {
3908 SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32);
3909 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2);
3910 } else if (P.Opcode == SystemZISD::PACK) {
3911 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8),
3912 SystemZ::VectorBytes / P.Operand);
3913 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1);
3914 } else {
3915 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1);
3916 }
3917 return Op;
3918}
3919
3920// Bytes is a VPERM-like permute vector, except that -1 is used for
3921// undefined bytes. Implement it on operands Ops[0] and Ops[1] using
3922// VSLDI or VPERM.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003923static SDValue getGeneralPermuteNode(SelectionDAG &DAG, const SDLoc &DL,
3924 SDValue *Ops,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003925 const SmallVectorImpl<int> &Bytes) {
3926 for (unsigned I = 0; I < 2; ++I)
3927 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]);
3928
3929 // First see whether VSLDI can be used.
3930 unsigned StartIndex, OpNo0, OpNo1;
3931 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1))
3932 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0],
3933 Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32));
3934
3935 // Fall back on VPERM. Construct an SDNode for the permute vector.
3936 SDValue IndexNodes[SystemZ::VectorBytes];
3937 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3938 if (Bytes[I] >= 0)
3939 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32);
3940 else
3941 IndexNodes[I] = DAG.getUNDEF(MVT::i32);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00003942 SDValue Op2 = DAG.getBuildVector(MVT::v16i8, DL, IndexNodes);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003943 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2);
3944}
3945
3946namespace {
3947// Describes a general N-operand vector shuffle.
3948struct GeneralShuffle {
3949 GeneralShuffle(EVT vt) : VT(vt) {}
3950 void addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003951 bool add(SDValue, unsigned);
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003952 SDValue getNode(SelectionDAG &, const SDLoc &);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003953
3954 // The operands of the shuffle.
3955 SmallVector<SDValue, SystemZ::VectorBytes> Ops;
3956
3957 // Index I is -1 if byte I of the result is undefined. Otherwise the
3958 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand
3959 // Bytes[I] / SystemZ::VectorBytes.
3960 SmallVector<int, SystemZ::VectorBytes> Bytes;
3961
3962 // The type of the shuffle result.
3963 EVT VT;
3964};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003965}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003966
3967// Add an extra undefined element to the shuffle.
3968void GeneralShuffle::addUndef() {
3969 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3970 for (unsigned I = 0; I < BytesPerElement; ++I)
3971 Bytes.push_back(-1);
3972}
3973
3974// Add an extra element to the shuffle, taking it from element Elem of Op.
3975// A null Op indicates a vector input whose value will be calculated later;
3976// there is at most one such input per shuffle and it always has the same
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003977// type as the result. Aborts and returns false if the source vector elements
3978// of an EXTRACT_VECTOR_ELT are smaller than the destination elements. Per
3979// LLVM they become implicitly extended, but this is rare and not optimized.
3980bool GeneralShuffle::add(SDValue Op, unsigned Elem) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003981 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3982
3983 // The source vector can have wider elements than the result,
3984 // either through an explicit TRUNCATE or because of type legalization.
3985 // We want the least significant part.
3986 EVT FromVT = Op.getNode() ? Op.getValueType() : VT;
3987 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003988
3989 // Return false if the source elements are smaller than their destination
3990 // elements.
3991 if (FromBytesPerElement < BytesPerElement)
3992 return false;
3993
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003994 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes +
3995 (FromBytesPerElement - BytesPerElement));
3996
3997 // Look through things like shuffles and bitcasts.
3998 while (Op.getNode()) {
3999 if (Op.getOpcode() == ISD::BITCAST)
4000 Op = Op.getOperand(0);
4001 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) {
4002 // See whether the bytes we need come from a contiguous part of one
4003 // operand.
4004 SmallVector<int, SystemZ::VectorBytes> OpBytes;
4005 getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes);
4006 int NewByte;
4007 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte))
4008 break;
4009 if (NewByte < 0) {
4010 addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004011 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004012 }
4013 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes);
4014 Byte = unsigned(NewByte) % SystemZ::VectorBytes;
Sanjay Patel57195842016-03-14 17:28:46 +00004015 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004016 addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004017 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004018 } else
4019 break;
4020 }
4021
4022 // Make sure that the source of the extraction is in Ops.
4023 unsigned OpNo = 0;
4024 for (; OpNo < Ops.size(); ++OpNo)
4025 if (Ops[OpNo] == Op)
4026 break;
4027 if (OpNo == Ops.size())
4028 Ops.push_back(Op);
4029
4030 // Add the element to Bytes.
4031 unsigned Base = OpNo * SystemZ::VectorBytes + Byte;
4032 for (unsigned I = 0; I < BytesPerElement; ++I)
4033 Bytes.push_back(Base + I);
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004034
4035 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004036}
4037
4038// Return SDNodes for the completed shuffle.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004039SDValue GeneralShuffle::getNode(SelectionDAG &DAG, const SDLoc &DL) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004040 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector");
4041
4042 if (Ops.size() == 0)
4043 return DAG.getUNDEF(VT);
4044
4045 // Make sure that there are at least two shuffle operands.
4046 if (Ops.size() == 1)
4047 Ops.push_back(DAG.getUNDEF(MVT::v16i8));
4048
4049 // Create a tree of shuffles, deferring root node until after the loop.
4050 // Try to redistribute the undefined elements of non-root nodes so that
4051 // the non-root shuffles match something like a pack or merge, then adjust
4052 // the parent node's permute vector to compensate for the new order.
4053 // Among other things, this copes with vectors like <2 x i16> that were
4054 // padded with undefined elements during type legalization.
4055 //
4056 // In the best case this redistribution will lead to the whole tree
4057 // using packs and merges. It should rarely be a loss in other cases.
4058 unsigned Stride = 1;
4059 for (; Stride * 2 < Ops.size(); Stride *= 2) {
4060 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) {
4061 SDValue SubOps[] = { Ops[I], Ops[I + Stride] };
4062
4063 // Create a mask for just these two operands.
4064 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes);
4065 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
4066 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes;
4067 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes;
4068 if (OpNo == I)
4069 NewBytes[J] = Byte;
4070 else if (OpNo == I + Stride)
4071 NewBytes[J] = SystemZ::VectorBytes + Byte;
4072 else
4073 NewBytes[J] = -1;
4074 }
4075 // See if it would be better to reorganize NewMask to avoid using VPERM.
4076 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes);
4077 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) {
4078 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]);
4079 // Applying NewBytesMap to Ops[I] gets back to NewBytes.
4080 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
4081 if (NewBytes[J] >= 0) {
4082 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes &&
4083 "Invalid double permute");
4084 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J];
4085 } else
4086 assert(NewBytesMap[J] < 0 && "Invalid double permute");
4087 }
4088 } else {
4089 // Just use NewBytes on the operands.
4090 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes);
4091 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J)
4092 if (NewBytes[J] >= 0)
4093 Bytes[J] = I * SystemZ::VectorBytes + J;
4094 }
4095 }
4096 }
4097
4098 // Now we just have 2 inputs. Put the second operand in Ops[1].
4099 if (Stride > 1) {
4100 Ops[1] = Ops[Stride];
4101 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
4102 if (Bytes[I] >= int(SystemZ::VectorBytes))
4103 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes;
4104 }
4105
4106 // Look for an instruction that can do the permute without resorting
4107 // to VPERM.
4108 unsigned OpNo0, OpNo1;
4109 SDValue Op;
4110 if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1))
4111 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]);
4112 else
4113 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes);
4114 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4115}
4116
Ulrich Weigandcd808232015-05-05 19:26:48 +00004117// Return true if the given BUILD_VECTOR is a scalar-to-vector conversion.
4118static bool isScalarToVector(SDValue Op) {
4119 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00004120 if (!Op.getOperand(I).isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004121 return false;
4122 return true;
4123}
4124
4125// Return a vector of type VT that contains Value in the first element.
4126// The other elements don't matter.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004127static SDValue buildScalarToVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandcd808232015-05-05 19:26:48 +00004128 SDValue Value) {
4129 // If we have a constant, replicate it to all elements and let the
4130 // BUILD_VECTOR lowering take care of it.
4131 if (Value.getOpcode() == ISD::Constant ||
4132 Value.getOpcode() == ISD::ConstantFP) {
4133 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004134 return DAG.getBuildVector(VT, DL, Ops);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004135 }
Sanjay Patel57195842016-03-14 17:28:46 +00004136 if (Value.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004137 return DAG.getUNDEF(VT);
4138 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
4139}
4140
4141// Return a vector of type VT in which Op0 is in element 0 and Op1 is in
4142// element 1. Used for cases in which replication is cheap.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004143static SDValue buildMergeScalars(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandcd808232015-05-05 19:26:48 +00004144 SDValue Op0, SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00004145 if (Op0.isUndef()) {
4146 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004147 return DAG.getUNDEF(VT);
4148 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1);
4149 }
Sanjay Patel57195842016-03-14 17:28:46 +00004150 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004151 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0);
4152 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT,
4153 buildScalarToVector(DAG, DL, VT, Op0),
4154 buildScalarToVector(DAG, DL, VT, Op1));
4155}
4156
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004157// Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64
4158// vector for them.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004159static SDValue joinDwords(SelectionDAG &DAG, const SDLoc &DL, SDValue Op0,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004160 SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00004161 if (Op0.isUndef() && Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004162 return DAG.getUNDEF(MVT::v2i64);
4163 // If one of the two inputs is undefined then replicate the other one,
4164 // in order to avoid using another register unnecessarily.
Sanjay Patel57195842016-03-14 17:28:46 +00004165 if (Op0.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004166 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
Sanjay Patel57195842016-03-14 17:28:46 +00004167 else if (Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004168 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
4169 else {
4170 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
4171 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
4172 }
4173 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1);
4174}
4175
4176// Try to represent constant BUILD_VECTOR node BVN using a
4177// SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask
4178// on success.
4179static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) {
4180 EVT ElemVT = BVN->getValueType(0).getVectorElementType();
4181 unsigned BytesPerElement = ElemVT.getStoreSize();
4182 for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) {
4183 SDValue Op = BVN->getOperand(I);
Sanjay Patel75068522016-03-14 18:09:43 +00004184 if (!Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004185 uint64_t Value;
4186 if (Op.getOpcode() == ISD::Constant)
4187 Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue();
4188 else if (Op.getOpcode() == ISD::ConstantFP)
4189 Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt()
4190 .getZExtValue());
4191 else
4192 return false;
4193 for (unsigned J = 0; J < BytesPerElement; ++J) {
4194 uint64_t Byte = (Value >> (J * 8)) & 0xff;
4195 if (Byte == 0xff)
Aaron Ballman2a3aa1f242015-05-11 12:45:53 +00004196 Mask |= 1ULL << ((E - I - 1) * BytesPerElement + J);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004197 else if (Byte != 0)
4198 return false;
4199 }
4200 }
4201 }
4202 return true;
4203}
4204
4205// Try to load a vector constant in which BitsPerElement-bit value Value
4206// is replicated to fill the vector. VT is the type of the resulting
4207// constant, which may have elements of a different size from BitsPerElement.
4208// Return the SDValue of the constant on success, otherwise return
4209// an empty value.
4210static SDValue tryBuildVectorReplicate(SelectionDAG &DAG,
4211 const SystemZInstrInfo *TII,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004212 const SDLoc &DL, EVT VT, uint64_t Value,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004213 unsigned BitsPerElement) {
4214 // Signed 16-bit values can be replicated using VREPI.
4215 int64_t SignedValue = SignExtend64(Value, BitsPerElement);
4216 if (isInt<16>(SignedValue)) {
4217 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
4218 SystemZ::VectorBits / BitsPerElement);
4219 SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT,
4220 DAG.getConstant(SignedValue, DL, MVT::i32));
4221 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4222 }
4223 // See whether rotating the constant left some N places gives a value that
4224 // is one less than a power of 2 (i.e. all zeros followed by all ones).
4225 // If so we can use VGM.
4226 unsigned Start, End;
4227 if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) {
4228 // isRxSBGMask returns the bit numbers for a full 64-bit value,
4229 // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to
4230 // bit numbers for an BitsPerElement value, so that 0 denotes
4231 // 1 << (BitsPerElement-1).
4232 Start -= 64 - BitsPerElement;
4233 End -= 64 - BitsPerElement;
4234 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
4235 SystemZ::VectorBits / BitsPerElement);
4236 SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT,
4237 DAG.getConstant(Start, DL, MVT::i32),
4238 DAG.getConstant(End, DL, MVT::i32));
4239 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4240 }
4241 return SDValue();
4242}
4243
4244// If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually
4245// better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for
4246// the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR
4247// would benefit from this representation and return it if so.
4248static SDValue tryBuildVectorShuffle(SelectionDAG &DAG,
4249 BuildVectorSDNode *BVN) {
4250 EVT VT = BVN->getValueType(0);
4251 unsigned NumElements = VT.getVectorNumElements();
4252
4253 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation
4254 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still
4255 // need a BUILD_VECTOR, add an additional placeholder operand for that
4256 // BUILD_VECTOR and store its operands in ResidueOps.
4257 GeneralShuffle GS(VT);
4258 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps;
4259 bool FoundOne = false;
4260 for (unsigned I = 0; I < NumElements; ++I) {
4261 SDValue Op = BVN->getOperand(I);
4262 if (Op.getOpcode() == ISD::TRUNCATE)
4263 Op = Op.getOperand(0);
4264 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4265 Op.getOperand(1).getOpcode() == ISD::Constant) {
4266 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004267 if (!GS.add(Op.getOperand(0), Elem))
4268 return SDValue();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004269 FoundOne = true;
Sanjay Patel57195842016-03-14 17:28:46 +00004270 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004271 GS.addUndef();
4272 } else {
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004273 if (!GS.add(SDValue(), ResidueOps.size()))
4274 return SDValue();
Ulrich Weigande861e642015-09-15 14:27:46 +00004275 ResidueOps.push_back(BVN->getOperand(I));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004276 }
4277 }
4278
4279 // Nothing to do if there are no EXTRACT_VECTOR_ELTs.
4280 if (!FoundOne)
4281 return SDValue();
4282
4283 // Create the BUILD_VECTOR for the remaining elements, if any.
4284 if (!ResidueOps.empty()) {
4285 while (ResidueOps.size() < NumElements)
Ulrich Weigandf4d14f72015-10-08 17:46:59 +00004286 ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType()));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004287 for (auto &Op : GS.Ops) {
4288 if (!Op.getNode()) {
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004289 Op = DAG.getBuildVector(VT, SDLoc(BVN), ResidueOps);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004290 break;
4291 }
4292 }
4293 }
4294 return GS.getNode(DAG, SDLoc(BVN));
4295}
4296
4297// Combine GPR scalar values Elems into a vector of type VT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004298static SDValue buildVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004299 SmallVectorImpl<SDValue> &Elems) {
4300 // See whether there is a single replicated value.
4301 SDValue Single;
4302 unsigned int NumElements = Elems.size();
4303 unsigned int Count = 0;
4304 for (auto Elem : Elems) {
Sanjay Patel75068522016-03-14 18:09:43 +00004305 if (!Elem.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004306 if (!Single.getNode())
4307 Single = Elem;
4308 else if (Elem != Single) {
4309 Single = SDValue();
4310 break;
4311 }
4312 Count += 1;
4313 }
4314 }
4315 // There are three cases here:
4316 //
4317 // - if the only defined element is a loaded one, the best sequence
4318 // is a replicating load.
4319 //
4320 // - otherwise, if the only defined element is an i64 value, we will
4321 // end up with the same VLVGP sequence regardless of whether we short-cut
4322 // for replication or fall through to the later code.
4323 //
4324 // - otherwise, if the only defined element is an i32 or smaller value,
4325 // we would need 2 instructions to replicate it: VLVGP followed by VREPx.
4326 // This is only a win if the single defined element is used more than once.
4327 // In other cases we're better off using a single VLVGx.
4328 if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD))
4329 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single);
4330
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004331 // If all elements are loads, use VLREP/VLEs (below).
4332 bool AllLoads = true;
4333 for (auto Elem : Elems)
4334 if (Elem.getOpcode() != ISD::LOAD || cast<LoadSDNode>(Elem)->isIndexed()) {
4335 AllLoads = false;
4336 break;
4337 }
4338
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004339 // The best way of building a v2i64 from two i64s is to use VLVGP.
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004340 if (VT == MVT::v2i64 && !AllLoads)
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004341 return joinDwords(DAG, DL, Elems[0], Elems[1]);
4342
Ulrich Weigandcd808232015-05-05 19:26:48 +00004343 // Use a 64-bit merge high to combine two doubles.
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004344 if (VT == MVT::v2f64 && !AllLoads)
Ulrich Weigandcd808232015-05-05 19:26:48 +00004345 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4346
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004347 // Build v4f32 values directly from the FPRs:
4348 //
4349 // <Axxx> <Bxxx> <Cxxxx> <Dxxx>
4350 // V V VMRHF
4351 // <ABxx> <CDxx>
4352 // V VMRHG
4353 // <ABCD>
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004354 if (VT == MVT::v4f32 && !AllLoads) {
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004355 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4356 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]);
4357 // Avoid unnecessary undefs by reusing the other operand.
Sanjay Patel57195842016-03-14 17:28:46 +00004358 if (Op01.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004359 Op01 = Op23;
Sanjay Patel57195842016-03-14 17:28:46 +00004360 else if (Op23.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004361 Op23 = Op01;
4362 // Merging identical replications is a no-op.
4363 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23)
4364 return Op01;
4365 Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01);
4366 Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23);
4367 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH,
4368 DL, MVT::v2i64, Op01, Op23);
4369 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4370 }
4371
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004372 // Collect the constant terms.
4373 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue());
4374 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false);
4375
4376 unsigned NumConstants = 0;
4377 for (unsigned I = 0; I < NumElements; ++I) {
4378 SDValue Elem = Elems[I];
4379 if (Elem.getOpcode() == ISD::Constant ||
4380 Elem.getOpcode() == ISD::ConstantFP) {
4381 NumConstants += 1;
4382 Constants[I] = Elem;
4383 Done[I] = true;
4384 }
4385 }
4386 // If there was at least one constant, fill in the other elements of
4387 // Constants with undefs to get a full vector constant and use that
4388 // as the starting point.
4389 SDValue Result;
4390 if (NumConstants > 0) {
4391 for (unsigned I = 0; I < NumElements; ++I)
4392 if (!Constants[I].getNode())
4393 Constants[I] = DAG.getUNDEF(Elems[I].getValueType());
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004394 Result = DAG.getBuildVector(VT, DL, Constants);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004395 } else {
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004396 // Otherwise try to use VLREP or VLVGP to start the sequence in order to
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004397 // avoid a false dependency on any previous contents of the vector
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004398 // register.
4399
4400 // Use a VLREP if at least one element is a load.
4401 unsigned LoadElIdx = UINT_MAX;
4402 for (unsigned I = 0; I < NumElements; ++I)
4403 if (Elems[I].getOpcode() == ISD::LOAD &&
4404 cast<LoadSDNode>(Elems[I])->isUnindexed()) {
4405 LoadElIdx = I;
4406 break;
4407 }
4408 if (LoadElIdx != UINT_MAX) {
4409 Result = DAG.getNode(SystemZISD::REPLICATE, DL, VT, Elems[LoadElIdx]);
4410 Done[LoadElIdx] = true;
4411 } else {
4412 // Try to use VLVGP.
4413 unsigned I1 = NumElements / 2 - 1;
4414 unsigned I2 = NumElements - 1;
4415 bool Def1 = !Elems[I1].isUndef();
4416 bool Def2 = !Elems[I2].isUndef();
4417 if (Def1 || Def2) {
4418 SDValue Elem1 = Elems[Def1 ? I1 : I2];
4419 SDValue Elem2 = Elems[Def2 ? I2 : I1];
4420 Result = DAG.getNode(ISD::BITCAST, DL, VT,
4421 joinDwords(DAG, DL, Elem1, Elem2));
4422 Done[I1] = true;
4423 Done[I2] = true;
4424 } else
4425 Result = DAG.getUNDEF(VT);
4426 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004427 }
4428
4429 // Use VLVGx to insert the other elements.
4430 for (unsigned I = 0; I < NumElements; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00004431 if (!Done[I] && !Elems[I].isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004432 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I],
4433 DAG.getConstant(I, DL, MVT::i32));
4434 return Result;
4435}
4436
4437SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op,
4438 SelectionDAG &DAG) const {
4439 const SystemZInstrInfo *TII =
4440 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
4441 auto *BVN = cast<BuildVectorSDNode>(Op.getNode());
4442 SDLoc DL(Op);
4443 EVT VT = Op.getValueType();
4444
4445 if (BVN->isConstant()) {
4446 // Try using VECTOR GENERATE BYTE MASK. This is the architecturally-
4447 // preferred way of creating all-zero and all-one vectors so give it
4448 // priority over other methods below.
4449 uint64_t Mask = 0;
4450 if (tryBuildVectorByteMask(BVN, Mask)) {
4451 SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
4452 DAG.getConstant(Mask, DL, MVT::i32));
4453 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4454 }
4455
4456 // Try using some form of replication.
4457 APInt SplatBits, SplatUndef;
4458 unsigned SplatBitSize;
4459 bool HasAnyUndefs;
4460 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4461 8, true) &&
4462 SplatBitSize <= 64) {
4463 // First try assuming that any undefined bits above the highest set bit
4464 // and below the lowest set bit are 1s. This increases the likelihood of
4465 // being able to use a sign-extended element value in VECTOR REPLICATE
4466 // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK.
4467 uint64_t SplatBitsZ = SplatBits.getZExtValue();
4468 uint64_t SplatUndefZ = SplatUndef.getZExtValue();
4469 uint64_t Lower = (SplatUndefZ
4470 & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1));
4471 uint64_t Upper = (SplatUndefZ
4472 & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1));
4473 uint64_t Value = SplatBitsZ | Upper | Lower;
4474 SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value,
4475 SplatBitSize);
4476 if (Op.getNode())
4477 return Op;
4478
4479 // Now try assuming that any undefined bits between the first and
4480 // last defined set bits are set. This increases the chances of
4481 // using a non-wraparound mask.
4482 uint64_t Middle = SplatUndefZ & ~Upper & ~Lower;
4483 Value = SplatBitsZ | Middle;
4484 Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize);
4485 if (Op.getNode())
4486 return Op;
4487 }
4488
4489 // Fall back to loading it from memory.
4490 return SDValue();
4491 }
4492
4493 // See if we should use shuffles to construct the vector from other vectors.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004494 if (SDValue Res = tryBuildVectorShuffle(DAG, BVN))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004495 return Res;
4496
Ulrich Weigandcd808232015-05-05 19:26:48 +00004497 // Detect SCALAR_TO_VECTOR conversions.
4498 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op))
4499 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0));
4500
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004501 // Otherwise use buildVector to build the vector up from GPRs.
4502 unsigned NumElements = Op.getNumOperands();
4503 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements);
4504 for (unsigned I = 0; I < NumElements; ++I)
4505 Ops[I] = Op.getOperand(I);
4506 return buildVector(DAG, DL, VT, Ops);
4507}
4508
4509SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
4510 SelectionDAG &DAG) const {
4511 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode());
4512 SDLoc DL(Op);
4513 EVT VT = Op.getValueType();
4514 unsigned NumElements = VT.getVectorNumElements();
4515
4516 if (VSN->isSplat()) {
4517 SDValue Op0 = Op.getOperand(0);
4518 unsigned Index = VSN->getSplatIndex();
4519 assert(Index < VT.getVectorNumElements() &&
4520 "Splat index should be defined and in first operand");
4521 // See whether the value we're splatting is directly available as a scalar.
4522 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4523 Op0.getOpcode() == ISD::BUILD_VECTOR)
4524 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index));
4525 // Otherwise keep it as a vector-to-vector operation.
4526 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0),
4527 DAG.getConstant(Index, DL, MVT::i32));
4528 }
4529
4530 GeneralShuffle GS(VT);
4531 for (unsigned I = 0; I < NumElements; ++I) {
4532 int Elt = VSN->getMaskElt(I);
4533 if (Elt < 0)
4534 GS.addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004535 else if (!GS.add(Op.getOperand(unsigned(Elt) / NumElements),
4536 unsigned(Elt) % NumElements))
4537 return SDValue();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004538 }
4539 return GS.getNode(DAG, SDLoc(VSN));
4540}
4541
4542SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
4543 SelectionDAG &DAG) const {
4544 SDLoc DL(Op);
4545 // Just insert the scalar into element 0 of an undefined vector.
4546 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL,
4547 Op.getValueType(), DAG.getUNDEF(Op.getValueType()),
4548 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32));
4549}
4550
Ulrich Weigandcd808232015-05-05 19:26:48 +00004551SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
4552 SelectionDAG &DAG) const {
4553 // Handle insertions of floating-point values.
4554 SDLoc DL(Op);
4555 SDValue Op0 = Op.getOperand(0);
4556 SDValue Op1 = Op.getOperand(1);
4557 SDValue Op2 = Op.getOperand(2);
4558 EVT VT = Op.getValueType();
4559
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004560 // Insertions into constant indices of a v2f64 can be done using VPDI.
4561 // However, if the inserted value is a bitcast or a constant then it's
4562 // better to use GPRs, as below.
4563 if (VT == MVT::v2f64 &&
4564 Op1.getOpcode() != ISD::BITCAST &&
Ulrich Weigandcd808232015-05-05 19:26:48 +00004565 Op1.getOpcode() != ISD::ConstantFP &&
4566 Op2.getOpcode() == ISD::Constant) {
4567 uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue();
4568 unsigned Mask = VT.getVectorNumElements() - 1;
4569 if (Index <= Mask)
4570 return Op;
4571 }
4572
4573 // Otherwise bitcast to the equivalent integer form and insert via a GPR.
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004574 MVT IntVT = MVT::getIntegerVT(VT.getScalarSizeInBits());
Ulrich Weigandcd808232015-05-05 19:26:48 +00004575 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements());
4576 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT,
4577 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0),
4578 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2);
4579 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4580}
4581
4582SDValue
4583SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
4584 SelectionDAG &DAG) const {
4585 // Handle extractions of floating-point values.
4586 SDLoc DL(Op);
4587 SDValue Op0 = Op.getOperand(0);
4588 SDValue Op1 = Op.getOperand(1);
4589 EVT VT = Op.getValueType();
4590 EVT VecVT = Op0.getValueType();
4591
4592 // Extractions of constant indices can be done directly.
4593 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) {
4594 uint64_t Index = CIndexN->getZExtValue();
4595 unsigned Mask = VecVT.getVectorNumElements() - 1;
4596 if (Index <= Mask)
4597 return Op;
4598 }
4599
4600 // Otherwise bitcast to the equivalent integer form and extract via a GPR.
4601 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
4602 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements());
4603 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT,
4604 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1);
4605 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4606}
4607
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004608SDValue
4609SystemZTargetLowering::lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004610 unsigned UnpackHigh) const {
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004611 SDValue PackedOp = Op.getOperand(0);
4612 EVT OutVT = Op.getValueType();
4613 EVT InVT = PackedOp.getValueType();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004614 unsigned ToBits = OutVT.getScalarSizeInBits();
4615 unsigned FromBits = InVT.getScalarSizeInBits();
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004616 do {
4617 FromBits *= 2;
4618 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits),
4619 SystemZ::VectorBits / FromBits);
4620 PackedOp = DAG.getNode(UnpackHigh, SDLoc(PackedOp), OutVT, PackedOp);
4621 } while (FromBits != ToBits);
4622 return PackedOp;
4623}
4624
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004625SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG,
4626 unsigned ByScalar) const {
4627 // Look for cases where a vector shift can use the *_BY_SCALAR form.
4628 SDValue Op0 = Op.getOperand(0);
4629 SDValue Op1 = Op.getOperand(1);
4630 SDLoc DL(Op);
4631 EVT VT = Op.getValueType();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004632 unsigned ElemBitSize = VT.getScalarSizeInBits();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004633
4634 // See whether the shift vector is a splat represented as BUILD_VECTOR.
4635 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) {
4636 APInt SplatBits, SplatUndef;
4637 unsigned SplatBitSize;
4638 bool HasAnyUndefs;
4639 // Check for constant splats. Use ElemBitSize as the minimum element
4640 // width and reject splats that need wider elements.
4641 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4642 ElemBitSize, true) &&
4643 SplatBitSize == ElemBitSize) {
4644 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff,
4645 DL, MVT::i32);
4646 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4647 }
4648 // Check for variable splats.
4649 BitVector UndefElements;
4650 SDValue Splat = BVN->getSplatValue(&UndefElements);
4651 if (Splat) {
4652 // Since i32 is the smallest legal type, we either need a no-op
4653 // or a truncation.
4654 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat);
4655 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4656 }
4657 }
4658
4659 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR,
4660 // and the shift amount is directly available in a GPR.
4661 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) {
4662 if (VSN->isSplat()) {
4663 SDValue VSNOp0 = VSN->getOperand(0);
4664 unsigned Index = VSN->getSplatIndex();
4665 assert(Index < VT.getVectorNumElements() &&
4666 "Splat index should be defined and in first operand");
4667 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4668 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) {
4669 // Since i32 is the smallest legal type, we either need a no-op
4670 // or a truncation.
4671 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32,
4672 VSNOp0.getOperand(Index));
4673 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4674 }
4675 }
4676 }
4677
4678 // Otherwise just treat the current form as legal.
4679 return Op;
4680}
4681
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004682SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
4683 SelectionDAG &DAG) const {
4684 switch (Op.getOpcode()) {
Ulrich Weigandf557d082016-04-04 12:44:55 +00004685 case ISD::FRAMEADDR:
4686 return lowerFRAMEADDR(Op, DAG);
4687 case ISD::RETURNADDR:
4688 return lowerRETURNADDR(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004689 case ISD::BR_CC:
4690 return lowerBR_CC(Op, DAG);
4691 case ISD::SELECT_CC:
4692 return lowerSELECT_CC(Op, DAG);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00004693 case ISD::SETCC:
4694 return lowerSETCC(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004695 case ISD::GlobalAddress:
4696 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
4697 case ISD::GlobalTLSAddress:
4698 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
4699 case ISD::BlockAddress:
4700 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
4701 case ISD::JumpTable:
4702 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
4703 case ISD::ConstantPool:
4704 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
4705 case ISD::BITCAST:
4706 return lowerBITCAST(Op, DAG);
4707 case ISD::VASTART:
4708 return lowerVASTART(Op, DAG);
4709 case ISD::VACOPY:
4710 return lowerVACOPY(Op, DAG);
4711 case ISD::DYNAMIC_STACKALLOC:
4712 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +00004713 case ISD::GET_DYNAMIC_AREA_OFFSET:
4714 return lowerGET_DYNAMIC_AREA_OFFSET(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00004715 case ISD::SMUL_LOHI:
4716 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004717 case ISD::UMUL_LOHI:
4718 return lowerUMUL_LOHI(Op, DAG);
4719 case ISD::SDIVREM:
4720 return lowerSDIVREM(Op, DAG);
4721 case ISD::UDIVREM:
4722 return lowerUDIVREM(Op, DAG);
4723 case ISD::OR:
4724 return lowerOR(Op, DAG);
Ulrich Weigandb4012182015-03-31 12:56:33 +00004725 case ISD::CTPOP:
4726 return lowerCTPOP(Op, DAG);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004727 case ISD::ATOMIC_FENCE:
4728 return lowerATOMIC_FENCE(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004729 case ISD::ATOMIC_SWAP:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004730 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
4731 case ISD::ATOMIC_STORE:
4732 return lowerATOMIC_STORE(Op, DAG);
4733 case ISD::ATOMIC_LOAD:
4734 return lowerATOMIC_LOAD(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004735 case ISD::ATOMIC_LOAD_ADD:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004736 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004737 case ISD::ATOMIC_LOAD_SUB:
Richard Sandiford41350a52013-12-24 15:18:04 +00004738 return lowerATOMIC_LOAD_SUB(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004739 case ISD::ATOMIC_LOAD_AND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004740 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004741 case ISD::ATOMIC_LOAD_OR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004742 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004743 case ISD::ATOMIC_LOAD_XOR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004744 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004745 case ISD::ATOMIC_LOAD_NAND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004746 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004747 case ISD::ATOMIC_LOAD_MIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004748 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004749 case ISD::ATOMIC_LOAD_MAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004750 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004751 case ISD::ATOMIC_LOAD_UMIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004752 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004753 case ISD::ATOMIC_LOAD_UMAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004754 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004755 case ISD::ATOMIC_CMP_SWAP:
4756 return lowerATOMIC_CMP_SWAP(Op, DAG);
4757 case ISD::STACKSAVE:
4758 return lowerSTACKSAVE(Op, DAG);
4759 case ISD::STACKRESTORE:
4760 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00004761 case ISD::PREFETCH:
4762 return lowerPREFETCH(Op, DAG);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004763 case ISD::INTRINSIC_W_CHAIN:
4764 return lowerINTRINSIC_W_CHAIN(Op, DAG);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004765 case ISD::INTRINSIC_WO_CHAIN:
4766 return lowerINTRINSIC_WO_CHAIN(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004767 case ISD::BUILD_VECTOR:
4768 return lowerBUILD_VECTOR(Op, DAG);
4769 case ISD::VECTOR_SHUFFLE:
4770 return lowerVECTOR_SHUFFLE(Op, DAG);
4771 case ISD::SCALAR_TO_VECTOR:
4772 return lowerSCALAR_TO_VECTOR(Op, DAG);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004773 case ISD::INSERT_VECTOR_ELT:
4774 return lowerINSERT_VECTOR_ELT(Op, DAG);
4775 case ISD::EXTRACT_VECTOR_ELT:
4776 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004777 case ISD::SIGN_EXTEND_VECTOR_INREG:
4778 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACK_HIGH);
4779 case ISD::ZERO_EXTEND_VECTOR_INREG:
4780 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACKL_HIGH);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004781 case ISD::SHL:
4782 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR);
4783 case ISD::SRL:
4784 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR);
4785 case ISD::SRA:
4786 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004787 default:
4788 llvm_unreachable("Unexpected node to lower");
4789 }
4790}
4791
4792const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
4793#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
Matthias Braund04893f2015-05-07 21:33:59 +00004794 switch ((SystemZISD::NodeType)Opcode) {
4795 case SystemZISD::FIRST_NUMBER: break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004796 OPCODE(RET_FLAG);
4797 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00004798 OPCODE(SIBCALL);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004799 OPCODE(TLS_GDCALL);
4800 OPCODE(TLS_LDCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004801 OPCODE(PCREL_WRAPPER);
Richard Sandiford54b36912013-09-27 15:14:04 +00004802 OPCODE(PCREL_OFFSET);
Richard Sandiford57485472013-12-13 15:35:00 +00004803 OPCODE(IABS);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00004804 OPCODE(ICMP);
4805 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00004806 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004807 OPCODE(BR_CCMASK);
4808 OPCODE(SELECT_CCMASK);
4809 OPCODE(ADJDYNALLOC);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004810 OPCODE(POPCNT);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +00004811 OPCODE(SMUL_LOHI);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00004812 OPCODE(UMUL_LOHI);
4813 OPCODE(SDIVREM);
4814 OPCODE(UDIVREM);
Richard Sandifordd131ff82013-07-08 09:35:23 +00004815 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004816 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00004817 OPCODE(NC);
4818 OPCODE(NC_LOOP);
4819 OPCODE(OC);
4820 OPCODE(OC_LOOP);
4821 OPCODE(XC);
4822 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00004823 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004824 OPCODE(CLC_LOOP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00004825 OPCODE(STPCPY);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004826 OPCODE(STRCMP);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00004827 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00004828 OPCODE(IPM);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004829 OPCODE(MEMBARRIER);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004830 OPCODE(TBEGIN);
4831 OPCODE(TBEGIN_NOFLOAT);
4832 OPCODE(TEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004833 OPCODE(BYTE_MASK);
4834 OPCODE(ROTATE_MASK);
4835 OPCODE(REPLICATE);
4836 OPCODE(JOIN_DWORDS);
4837 OPCODE(SPLAT);
4838 OPCODE(MERGE_HIGH);
4839 OPCODE(MERGE_LOW);
4840 OPCODE(SHL_DOUBLE);
4841 OPCODE(PERMUTE_DWORDS);
4842 OPCODE(PERMUTE);
4843 OPCODE(PACK);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004844 OPCODE(PACKS_CC);
4845 OPCODE(PACKLS_CC);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004846 OPCODE(UNPACK_HIGH);
4847 OPCODE(UNPACKL_HIGH);
4848 OPCODE(UNPACK_LOW);
4849 OPCODE(UNPACKL_LOW);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004850 OPCODE(VSHL_BY_SCALAR);
4851 OPCODE(VSRL_BY_SCALAR);
4852 OPCODE(VSRA_BY_SCALAR);
4853 OPCODE(VSUM);
4854 OPCODE(VICMPE);
4855 OPCODE(VICMPH);
4856 OPCODE(VICMPHL);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004857 OPCODE(VICMPES);
4858 OPCODE(VICMPHS);
4859 OPCODE(VICMPHLS);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004860 OPCODE(VFCMPE);
4861 OPCODE(VFCMPH);
4862 OPCODE(VFCMPHE);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004863 OPCODE(VFCMPES);
4864 OPCODE(VFCMPHS);
4865 OPCODE(VFCMPHES);
4866 OPCODE(VFTCI);
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004867 OPCODE(VEXTEND);
4868 OPCODE(VROUND);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004869 OPCODE(VTM);
4870 OPCODE(VFAE_CC);
4871 OPCODE(VFAEZ_CC);
4872 OPCODE(VFEE_CC);
4873 OPCODE(VFEEZ_CC);
4874 OPCODE(VFENE_CC);
4875 OPCODE(VFENEZ_CC);
4876 OPCODE(VISTR_CC);
4877 OPCODE(VSTRC_CC);
4878 OPCODE(VSTRCZ_CC);
Marcin Koscielnicki32e87342016-07-02 02:20:40 +00004879 OPCODE(TDC);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004880 OPCODE(ATOMIC_SWAPW);
4881 OPCODE(ATOMIC_LOADW_ADD);
4882 OPCODE(ATOMIC_LOADW_SUB);
4883 OPCODE(ATOMIC_LOADW_AND);
4884 OPCODE(ATOMIC_LOADW_OR);
4885 OPCODE(ATOMIC_LOADW_XOR);
4886 OPCODE(ATOMIC_LOADW_NAND);
4887 OPCODE(ATOMIC_LOADW_MIN);
4888 OPCODE(ATOMIC_LOADW_MAX);
4889 OPCODE(ATOMIC_LOADW_UMIN);
4890 OPCODE(ATOMIC_LOADW_UMAX);
4891 OPCODE(ATOMIC_CMP_SWAPW);
Bryan Chan28b759c2016-05-16 20:32:22 +00004892 OPCODE(LRV);
4893 OPCODE(STRV);
Richard Sandiford03481332013-08-23 11:36:42 +00004894 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004895 }
Craig Topper062a2ba2014-04-25 05:30:21 +00004896 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004897#undef OPCODE
4898}
4899
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004900// Return true if VT is a vector whose elements are a whole number of bytes
Jonas Paulssoncad72ef2017-04-07 12:35:11 +00004901// in width. Also check for presence of vector support.
4902bool SystemZTargetLowering::canTreatAsByteVector(EVT VT) const {
4903 if (!Subtarget.hasVector())
4904 return false;
4905
Jonas Paulsson1d33cd32017-03-07 09:49:31 +00004906 return VT.isVector() && VT.getScalarSizeInBits() % 8 == 0 && VT.isSimple();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004907}
4908
4909// Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT
4910// producing a result of type ResVT. Op is a possibly bitcast version
4911// of the input vector and Index is the index (based on type VecVT) that
4912// should be extracted. Return the new extraction if a simplification
4913// was possible or if Force is true.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004914SDValue SystemZTargetLowering::combineExtract(const SDLoc &DL, EVT ResVT,
4915 EVT VecVT, SDValue Op,
4916 unsigned Index,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004917 DAGCombinerInfo &DCI,
4918 bool Force) const {
4919 SelectionDAG &DAG = DCI.DAG;
4920
4921 // The number of bytes being extracted.
4922 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4923
4924 for (;;) {
4925 unsigned Opcode = Op.getOpcode();
4926 if (Opcode == ISD::BITCAST)
4927 // Look through bitcasts.
4928 Op = Op.getOperand(0);
4929 else if (Opcode == ISD::VECTOR_SHUFFLE &&
4930 canTreatAsByteVector(Op.getValueType())) {
4931 // Get a VPERM-like permute mask and see whether the bytes covered
4932 // by the extracted element are a contiguous sequence from one
4933 // source operand.
4934 SmallVector<int, SystemZ::VectorBytes> Bytes;
4935 getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes);
4936 int First;
4937 if (!getShuffleInput(Bytes, Index * BytesPerElement,
4938 BytesPerElement, First))
4939 break;
4940 if (First < 0)
4941 return DAG.getUNDEF(ResVT);
4942 // Make sure the contiguous sequence starts at a multiple of the
4943 // original element size.
4944 unsigned Byte = unsigned(First) % Bytes.size();
4945 if (Byte % BytesPerElement != 0)
4946 break;
4947 // We can get the extracted value directly from an input.
4948 Index = Byte / BytesPerElement;
4949 Op = Op.getOperand(unsigned(First) / Bytes.size());
4950 Force = true;
4951 } else if (Opcode == ISD::BUILD_VECTOR &&
4952 canTreatAsByteVector(Op.getValueType())) {
4953 // We can only optimize this case if the BUILD_VECTOR elements are
4954 // at least as wide as the extracted value.
4955 EVT OpVT = Op.getValueType();
4956 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4957 if (OpBytesPerElement < BytesPerElement)
4958 break;
4959 // Make sure that the least-significant bit of the extracted value
4960 // is the least significant bit of an input.
4961 unsigned End = (Index + 1) * BytesPerElement;
4962 if (End % OpBytesPerElement != 0)
4963 break;
4964 // We're extracting the low part of one operand of the BUILD_VECTOR.
4965 Op = Op.getOperand(End / OpBytesPerElement - 1);
4966 if (!Op.getValueType().isInteger()) {
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00004967 EVT VT = MVT::getIntegerVT(Op.getValueSizeInBits());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004968 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
4969 DCI.AddToWorklist(Op.getNode());
4970 }
4971 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits());
4972 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
4973 if (VT != ResVT) {
4974 DCI.AddToWorklist(Op.getNode());
4975 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op);
4976 }
4977 return Op;
4978 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004979 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG ||
4980 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) &&
4981 canTreatAsByteVector(Op.getValueType()) &&
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004982 canTreatAsByteVector(Op.getOperand(0).getValueType())) {
4983 // Make sure that only the unextended bits are significant.
4984 EVT ExtVT = Op.getValueType();
4985 EVT OpVT = Op.getOperand(0).getValueType();
4986 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize();
4987 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4988 unsigned Byte = Index * BytesPerElement;
4989 unsigned SubByte = Byte % ExtBytesPerElement;
4990 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement;
4991 if (SubByte < MinSubByte ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004992 SubByte + BytesPerElement > ExtBytesPerElement)
4993 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004994 // Get the byte offset of the unextended element
4995 Byte = Byte / ExtBytesPerElement * OpBytesPerElement;
4996 // ...then add the byte offset relative to that element.
4997 Byte += SubByte - MinSubByte;
4998 if (Byte % BytesPerElement != 0)
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004999 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005000 Op = Op.getOperand(0);
5001 Index = Byte / BytesPerElement;
5002 Force = true;
5003 } else
5004 break;
5005 }
5006 if (Force) {
5007 if (Op.getValueType() != VecVT) {
5008 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op);
5009 DCI.AddToWorklist(Op.getNode());
5010 }
5011 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op,
5012 DAG.getConstant(Index, DL, MVT::i32));
5013 }
5014 return SDValue();
5015}
5016
5017// Optimize vector operations in scalar value Op on the basis that Op
5018// is truncated to TruncVT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005019SDValue SystemZTargetLowering::combineTruncateExtract(
5020 const SDLoc &DL, EVT TruncVT, SDValue Op, DAGCombinerInfo &DCI) const {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005021 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into
5022 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements
5023 // of type TruncVT.
5024 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5025 TruncVT.getSizeInBits() % 8 == 0) {
5026 SDValue Vec = Op.getOperand(0);
5027 EVT VecVT = Vec.getValueType();
5028 if (canTreatAsByteVector(VecVT)) {
5029 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
5030 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
5031 unsigned TruncBytes = TruncVT.getStoreSize();
5032 if (BytesPerElement % TruncBytes == 0) {
5033 // Calculate the value of Y' in the above description. We are
5034 // splitting the original elements into Scale equal-sized pieces
5035 // and for truncation purposes want the last (least-significant)
5036 // of these pieces for IndexN. This is easiest to do by calculating
5037 // the start index of the following element and then subtracting 1.
5038 unsigned Scale = BytesPerElement / TruncBytes;
5039 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1;
5040
5041 // Defer the creation of the bitcast from X to combineExtract,
5042 // which might be able to optimize the extraction.
5043 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8),
5044 VecVT.getStoreSize() / TruncBytes);
5045 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT);
5046 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true);
5047 }
5048 }
5049 }
5050 }
5051 return SDValue();
5052}
5053
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005054SDValue SystemZTargetLowering::combineSIGN_EXTEND(
5055 SDNode *N, DAGCombinerInfo &DCI) const {
5056 // Convert (sext (ashr (shl X, C1), C2)) to
5057 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
5058 // cheap as narrower ones.
5059 SelectionDAG &DAG = DCI.DAG;
5060 SDValue N0 = N->getOperand(0);
5061 EVT VT = N->getValueType(0);
5062 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
5063 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5064 SDValue Inner = N0.getOperand(0);
5065 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
5066 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00005067 unsigned Extra = (VT.getSizeInBits() - N0.getValueSizeInBits());
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005068 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
5069 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
5070 EVT ShiftVT = N0.getOperand(1).getValueType();
5071 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
5072 Inner.getOperand(0));
5073 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
5074 DAG.getConstant(NewShlAmt, SDLoc(Inner),
5075 ShiftVT));
5076 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
5077 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT));
5078 }
5079 }
5080 }
5081 return SDValue();
5082}
5083
5084SDValue SystemZTargetLowering::combineMERGE(
5085 SDNode *N, DAGCombinerInfo &DCI) const {
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005086 SelectionDAG &DAG = DCI.DAG;
5087 unsigned Opcode = N->getOpcode();
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005088 SDValue Op0 = N->getOperand(0);
5089 SDValue Op1 = N->getOperand(1);
5090 if (Op0.getOpcode() == ISD::BITCAST)
5091 Op0 = Op0.getOperand(0);
5092 if (Op0.getOpcode() == SystemZISD::BYTE_MASK &&
5093 cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0) {
5094 // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF
5095 // for v4f32.
5096 if (Op1 == N->getOperand(0))
5097 return Op1;
5098 // (z_merge_? 0, X) -> (z_unpackl_? 0, X).
5099 EVT VT = Op1.getValueType();
5100 unsigned ElemBytes = VT.getVectorElementType().getStoreSize();
5101 if (ElemBytes <= 4) {
5102 Opcode = (Opcode == SystemZISD::MERGE_HIGH ?
5103 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW);
5104 EVT InVT = VT.changeVectorElementTypeToInteger();
5105 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16),
5106 SystemZ::VectorBytes / ElemBytes / 2);
5107 if (VT != InVT) {
5108 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1);
5109 DCI.AddToWorklist(Op1.getNode());
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005110 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005111 SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1);
5112 DCI.AddToWorklist(Op.getNode());
5113 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005114 }
5115 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005116 return SDValue();
5117}
5118
5119SDValue SystemZTargetLowering::combineSTORE(
5120 SDNode *N, DAGCombinerInfo &DCI) const {
5121 SelectionDAG &DAG = DCI.DAG;
5122 auto *SN = cast<StoreSDNode>(N);
5123 auto &Op1 = N->getOperand(1);
5124 EVT MemVT = SN->getMemoryVT();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005125 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better
5126 // for the extraction to be done on a vMiN value, so that we can use VSTE.
5127 // If X has wider elements then convert it to:
5128 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z).
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005129 if (MemVT.isInteger()) {
5130 if (SDValue Value =
5131 combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) {
5132 DCI.AddToWorklist(Value.getNode());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005133
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005134 // Rewrite the store with the new form of stored value.
5135 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value,
5136 SN->getBasePtr(), SN->getMemoryVT(),
5137 SN->getMemOperand());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005138 }
5139 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005140 // Combine STORE (BSWAP) into STRVH/STRV/STRVG
5141 // See comment in combineBSWAP about volatile accesses.
5142 if (!SN->isVolatile() &&
5143 Op1.getOpcode() == ISD::BSWAP &&
5144 Op1.getNode()->hasOneUse() &&
5145 (Op1.getValueType() == MVT::i16 ||
5146 Op1.getValueType() == MVT::i32 ||
5147 Op1.getValueType() == MVT::i64)) {
5148
5149 SDValue BSwapOp = Op1.getOperand(0);
5150
5151 if (BSwapOp.getValueType() == MVT::i16)
5152 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), MVT::i32, BSwapOp);
5153
5154 SDValue Ops[] = {
5155 N->getOperand(0), BSwapOp, N->getOperand(2),
5156 DAG.getValueType(Op1.getValueType())
5157 };
5158
5159 return
5160 DAG.getMemIntrinsicNode(SystemZISD::STRV, SDLoc(N), DAG.getVTList(MVT::Other),
5161 Ops, MemVT, SN->getMemOperand());
5162 }
5163 return SDValue();
5164}
5165
5166SDValue SystemZTargetLowering::combineEXTRACT_VECTOR_ELT(
5167 SDNode *N, DAGCombinerInfo &DCI) const {
Jonas Paulsson56bb0852017-03-31 13:22:59 +00005168
Jonas Paulsson56bb0852017-03-31 13:22:59 +00005169 if (!Subtarget.hasVector())
5170 return SDValue();
5171
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005172 // Try to simplify a vector extraction.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005173 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
5174 SDValue Op0 = N->getOperand(0);
5175 EVT VecVT = Op0.getValueType();
5176 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0,
5177 IndexN->getZExtValue(), DCI, false);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005178 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005179 return SDValue();
5180}
5181
5182SDValue SystemZTargetLowering::combineJOIN_DWORDS(
5183 SDNode *N, DAGCombinerInfo &DCI) const {
5184 SelectionDAG &DAG = DCI.DAG;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005185 // (join_dwords X, X) == (replicate X)
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005186 if (N->getOperand(0) == N->getOperand(1))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005187 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0),
5188 N->getOperand(0));
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005189 return SDValue();
5190}
5191
5192SDValue SystemZTargetLowering::combineFP_ROUND(
5193 SDNode *N, DAGCombinerInfo &DCI) const {
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00005194 // (fpround (extract_vector_elt X 0))
5195 // (fpround (extract_vector_elt X 1)) ->
Ulrich Weigand80b3af72015-05-05 19:27:45 +00005196 // (extract_vector_elt (VROUND X) 0)
5197 // (extract_vector_elt (VROUND X) 1)
5198 //
5199 // This is a special case since the target doesn't really support v2f32s.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005200 SelectionDAG &DAG = DCI.DAG;
5201 SDValue Op0 = N->getOperand(0);
5202 if (N->getValueType(0) == MVT::f32 &&
5203 Op0.hasOneUse() &&
5204 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5205 Op0.getOperand(0).getValueType() == MVT::v2f64 &&
5206 Op0.getOperand(1).getOpcode() == ISD::Constant &&
5207 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) {
5208 SDValue Vec = Op0.getOperand(0);
5209 for (auto *U : Vec->uses()) {
5210 if (U != Op0.getNode() &&
5211 U->hasOneUse() &&
5212 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5213 U->getOperand(0) == Vec &&
5214 U->getOperand(1).getOpcode() == ISD::Constant &&
5215 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) {
5216 SDValue OtherRound = SDValue(*U->use_begin(), 0);
5217 if (OtherRound.getOpcode() == ISD::FP_ROUND &&
5218 OtherRound.getOperand(0) == SDValue(U, 0) &&
5219 OtherRound.getValueType() == MVT::f32) {
5220 SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N),
5221 MVT::v4f32, Vec);
5222 DCI.AddToWorklist(VRound.getNode());
5223 SDValue Extract1 =
5224 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32,
5225 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32));
5226 DCI.AddToWorklist(Extract1.getNode());
5227 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1);
5228 SDValue Extract0 =
5229 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32,
5230 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
5231 return Extract0;
Ulrich Weigand80b3af72015-05-05 19:27:45 +00005232 }
5233 }
5234 }
5235 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005236 return SDValue();
5237}
Bryan Chan28b759c2016-05-16 20:32:22 +00005238
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005239SDValue SystemZTargetLowering::combineBSWAP(
5240 SDNode *N, DAGCombinerInfo &DCI) const {
5241 SelectionDAG &DAG = DCI.DAG;
Bryan Chan28b759c2016-05-16 20:32:22 +00005242 // Combine BSWAP (LOAD) into LRVH/LRV/LRVG
5243 // These loads are allowed to access memory multiple times, and so we must check
5244 // that the loads are not volatile before performing the combine.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005245 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
5246 N->getOperand(0).hasOneUse() &&
5247 (N->getValueType(0) == MVT::i16 || N->getValueType(0) == MVT::i32 ||
5248 N->getValueType(0) == MVT::i64) &&
5249 !cast<LoadSDNode>(N->getOperand(0))->isVolatile()) {
Bryan Chan28b759c2016-05-16 20:32:22 +00005250 SDValue Load = N->getOperand(0);
5251 LoadSDNode *LD = cast<LoadSDNode>(Load);
5252
5253 // Create the byte-swapping load.
5254 SDValue Ops[] = {
5255 LD->getChain(), // Chain
5256 LD->getBasePtr(), // Ptr
5257 DAG.getValueType(N->getValueType(0)) // VT
5258 };
5259 SDValue BSLoad =
5260 DAG.getMemIntrinsicNode(SystemZISD::LRV, SDLoc(N),
5261 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
5262 MVT::i64 : MVT::i32, MVT::Other),
5263 Ops, LD->getMemoryVT(), LD->getMemOperand());
5264
5265 // If this is an i16 load, insert the truncate.
5266 SDValue ResVal = BSLoad;
5267 if (N->getValueType(0) == MVT::i16)
5268 ResVal = DAG.getNode(ISD::TRUNCATE, SDLoc(N), MVT::i16, BSLoad);
5269
5270 // First, combine the bswap away. This makes the value produced by the
5271 // load dead.
5272 DCI.CombineTo(N, ResVal);
5273
5274 // Next, combine the load away, we give it a bogus result value but a real
5275 // chain result. The result value is dead because the bswap is dead.
5276 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
5277
5278 // Return N so it doesn't get rechecked!
5279 return SDValue(N, 0);
5280 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005281 return SDValue();
5282}
Bryan Chan28b759c2016-05-16 20:32:22 +00005283
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005284SDValue SystemZTargetLowering::combineSHIFTROT(
5285 SDNode *N, DAGCombinerInfo &DCI) const {
5286
5287 SelectionDAG &DAG = DCI.DAG;
5288
5289 // Shift/rotate instructions only use the last 6 bits of the second operand
5290 // register. If the second operand is the result of an AND with an immediate
5291 // value that has its last 6 bits set, we can safely remove the AND operation.
Elliot Colp687691a2016-08-18 18:04:26 +00005292 //
5293 // If the AND operation doesn't have the last 6 bits set, we can't remove it
Elliot Colpa4092102016-08-23 14:03:02 +00005294 // entirely, but we can still truncate it to a 16-bit value. This prevents
5295 // us from ending up with a NILL with a signed operand, which will cause the
5296 // instruction printer to abort.
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005297 SDValue N1 = N->getOperand(1);
5298 if (N1.getOpcode() == ISD::AND) {
Elliot Colp687691a2016-08-18 18:04:26 +00005299 SDValue AndMaskOp = N1->getOperand(1);
5300 auto *AndMask = dyn_cast<ConstantSDNode>(AndMaskOp);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005301
5302 // The AND mask is constant
5303 if (AndMask) {
Elliot Colpa4092102016-08-23 14:03:02 +00005304 auto AmtVal = AndMask->getZExtValue();
5305
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005306 // Bottom 6 bits are set
5307 if ((AmtVal & 0x3f) == 0x3f) {
Elliot Colpa4092102016-08-23 14:03:02 +00005308 SDValue AndOp = N1->getOperand(0);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005309
5310 // This is the only use, so remove the node
5311 if (N1.hasOneUse()) {
5312 // Combine the AND away
5313 DCI.CombineTo(N1.getNode(), AndOp);
5314
5315 // Return N so it isn't rechecked
5316 return SDValue(N, 0);
5317
5318 // The node will be reused, so create a new node for this one use
5319 } else {
5320 SDValue Replace = DAG.getNode(N->getOpcode(), SDLoc(N),
5321 N->getValueType(0), N->getOperand(0),
5322 AndOp);
5323 DCI.AddToWorklist(Replace.getNode());
5324
5325 return Replace;
5326 }
Elliot Colp687691a2016-08-18 18:04:26 +00005327
Elliot Colpa4092102016-08-23 14:03:02 +00005328 // We can't remove the AND, but we can use NILL here (normally we would
5329 // use NILF). Only keep the last 16 bits of the mask. The actual
5330 // transformation will be handled by .td definitions.
5331 } else if (AmtVal >> 16 != 0) {
5332 SDValue AndOp = N1->getOperand(0);
Elliot Colp687691a2016-08-18 18:04:26 +00005333
Elliot Colpa4092102016-08-23 14:03:02 +00005334 auto NewMask = DAG.getConstant(AndMask->getZExtValue() & 0x0000ffff,
5335 SDLoc(AndMaskOp),
5336 AndMaskOp.getValueType());
Elliot Colp687691a2016-08-18 18:04:26 +00005337
Elliot Colpa4092102016-08-23 14:03:02 +00005338 auto NewAnd = DAG.getNode(N1.getOpcode(), SDLoc(N1), N1.getValueType(),
5339 AndOp, NewMask);
Elliot Colp687691a2016-08-18 18:04:26 +00005340
Elliot Colpa4092102016-08-23 14:03:02 +00005341 SDValue Replace = DAG.getNode(N->getOpcode(), SDLoc(N),
5342 N->getValueType(0), N->getOperand(0),
5343 NewAnd);
5344 DCI.AddToWorklist(Replace.getNode());
Elliot Colp687691a2016-08-18 18:04:26 +00005345
Elliot Colpa4092102016-08-23 14:03:02 +00005346 return Replace;
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005347 }
5348 }
5349 }
5350
5351 return SDValue();
5352}
5353
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005354SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
5355 DAGCombinerInfo &DCI) const {
5356 switch(N->getOpcode()) {
5357 default: break;
5358 case ISD::SIGN_EXTEND: return combineSIGN_EXTEND(N, DCI);
5359 case SystemZISD::MERGE_HIGH:
5360 case SystemZISD::MERGE_LOW: return combineMERGE(N, DCI);
5361 case ISD::STORE: return combineSTORE(N, DCI);
5362 case ISD::EXTRACT_VECTOR_ELT: return combineEXTRACT_VECTOR_ELT(N, DCI);
5363 case SystemZISD::JOIN_DWORDS: return combineJOIN_DWORDS(N, DCI);
5364 case ISD::FP_ROUND: return combineFP_ROUND(N, DCI);
5365 case ISD::BSWAP: return combineBSWAP(N, DCI);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005366 case ISD::SHL:
5367 case ISD::SRA:
5368 case ISD::SRL:
5369 case ISD::ROTL: return combineSHIFTROT(N, DCI);
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005370 }
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005371
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005372 return SDValue();
5373}
5374
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005375//===----------------------------------------------------------------------===//
5376// Custom insertion
5377//===----------------------------------------------------------------------===//
5378
5379// Create a new basic block after MBB.
5380static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
5381 MachineFunction &MF = *MBB->getParent();
5382 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00005383 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005384 return NewMBB;
5385}
5386
Richard Sandifordbe133a82013-08-28 09:01:51 +00005387// Split MBB after MI and return the new block (the one that contains
5388// instructions after MI).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005389static MachineBasicBlock *splitBlockAfter(MachineBasicBlock::iterator MI,
Richard Sandifordbe133a82013-08-28 09:01:51 +00005390 MachineBasicBlock *MBB) {
5391 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
5392 NewMBB->splice(NewMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00005393 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Richard Sandifordbe133a82013-08-28 09:01:51 +00005394 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
5395 return NewMBB;
5396}
5397
Richard Sandiford5e318f02013-08-27 09:54:29 +00005398// Split MBB before MI and return the new block (the one that contains MI).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005399static MachineBasicBlock *splitBlockBefore(MachineBasicBlock::iterator MI,
Richard Sandiford5e318f02013-08-27 09:54:29 +00005400 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005401 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005402 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005403 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
5404 return NewMBB;
5405}
5406
Richard Sandiford5e318f02013-08-27 09:54:29 +00005407// Force base value Base into a register before MI. Return the register.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005408static unsigned forceReg(MachineInstr &MI, MachineOperand &Base,
Richard Sandiford5e318f02013-08-27 09:54:29 +00005409 const SystemZInstrInfo *TII) {
5410 if (Base.isReg())
5411 return Base.getReg();
5412
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005413 MachineBasicBlock *MBB = MI.getParent();
Richard Sandiford5e318f02013-08-27 09:54:29 +00005414 MachineFunction &MF = *MBB->getParent();
5415 MachineRegisterInfo &MRI = MF.getRegInfo();
5416
5417 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005418 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LA), Reg)
Diana Picus116bbab2017-01-13 09:58:52 +00005419 .add(Base)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005420 .addImm(0)
5421 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005422 return Reg;
5423}
5424
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005425// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
5426MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005427SystemZTargetLowering::emitSelect(MachineInstr &MI,
Ulrich Weigand524f2762016-11-28 13:34:08 +00005428 MachineBasicBlock *MBB,
5429 unsigned LOCROpcode) const {
Eric Christophera6734172015-01-31 00:06:45 +00005430 const SystemZInstrInfo *TII =
5431 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005432
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005433 unsigned DestReg = MI.getOperand(0).getReg();
5434 unsigned TrueReg = MI.getOperand(1).getReg();
5435 unsigned FalseReg = MI.getOperand(2).getReg();
5436 unsigned CCValid = MI.getOperand(3).getImm();
5437 unsigned CCMask = MI.getOperand(4).getImm();
5438 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005439
Ulrich Weigand524f2762016-11-28 13:34:08 +00005440 // Use LOCROpcode if possible.
5441 if (LOCROpcode && Subtarget.hasLoadStoreOnCond()) {
5442 BuildMI(*MBB, MI, DL, TII->get(LOCROpcode), DestReg)
5443 .addReg(FalseReg).addReg(TrueReg)
5444 .addImm(CCValid).addImm(CCMask);
5445 MI.eraseFromParent();
5446 return MBB;
5447 }
5448
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005449 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005450 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005451 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5452
5453 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00005454 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005455 // # fallthrough to FalseMBB
5456 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005457 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5458 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005459 MBB->addSuccessor(JoinMBB);
5460 MBB->addSuccessor(FalseMBB);
5461
5462 // FalseMBB:
5463 // # fallthrough to JoinMBB
5464 MBB = FalseMBB;
5465 MBB->addSuccessor(JoinMBB);
5466
5467 // JoinMBB:
5468 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
5469 // ...
5470 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005471 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005472 .addReg(TrueReg).addMBB(StartMBB)
5473 .addReg(FalseReg).addMBB(FalseMBB);
5474
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005475 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005476 return JoinMBB;
5477}
5478
Richard Sandifordb86a8342013-06-27 09:27:40 +00005479// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
5480// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005481// happen when the condition is false rather than true. If a STORE ON
5482// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005483MachineBasicBlock *SystemZTargetLowering::emitCondStore(MachineInstr &MI,
5484 MachineBasicBlock *MBB,
5485 unsigned StoreOpcode,
5486 unsigned STOCOpcode,
5487 bool Invert) const {
Eric Christophera6734172015-01-31 00:06:45 +00005488 const SystemZInstrInfo *TII =
5489 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordb86a8342013-06-27 09:27:40 +00005490
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005491 unsigned SrcReg = MI.getOperand(0).getReg();
5492 MachineOperand Base = MI.getOperand(1);
5493 int64_t Disp = MI.getOperand(2).getImm();
5494 unsigned IndexReg = MI.getOperand(3).getReg();
5495 unsigned CCValid = MI.getOperand(4).getImm();
5496 unsigned CCMask = MI.getOperand(5).getImm();
5497 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005498
5499 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
5500
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005501 // Use STOCOpcode if possible. We could use different store patterns in
5502 // order to avoid matching the index register, but the performance trade-offs
5503 // might be more complicated in that case.
Eric Christopher93bf97c2014-06-27 07:38:01 +00005504 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005505 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005506 CCMask ^= CCValid;
Jonas Paulssonae8d22c2017-06-07 14:08:34 +00005507
5508 // ISel pattern matching also adds a load memory operand of the same
5509 // address, so take special care to find the storing memory operand.
5510 MachineMemOperand *MMO = nullptr;
5511 for (auto *I : MI.memoperands())
5512 if (I->isStore()) {
5513 MMO = I;
5514 break;
5515 }
5516
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005517 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Jonas Paulssonae8d22c2017-06-07 14:08:34 +00005518 .addReg(SrcReg)
5519 .add(Base)
5520 .addImm(Disp)
5521 .addImm(CCValid)
5522 .addImm(CCMask)
5523 .addMemOperand(MMO);
5524
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005525 MI.eraseFromParent();
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005526 return MBB;
5527 }
5528
Richard Sandifordb86a8342013-06-27 09:27:40 +00005529 // Get the condition needed to branch around the store.
5530 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005531 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00005532
5533 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005534 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005535 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5536
5537 // StartMBB:
5538 // BRC CCMask, JoinMBB
5539 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00005540 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005541 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5542 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005543 MBB->addSuccessor(JoinMBB);
5544 MBB->addSuccessor(FalseMBB);
5545
5546 // FalseMBB:
5547 // store %SrcReg, %Disp(%Index,%Base)
5548 // # fallthrough to JoinMBB
5549 MBB = FalseMBB;
5550 BuildMI(MBB, DL, TII->get(StoreOpcode))
Diana Picus116bbab2017-01-13 09:58:52 +00005551 .addReg(SrcReg)
5552 .add(Base)
5553 .addImm(Disp)
5554 .addReg(IndexReg);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005555 MBB->addSuccessor(JoinMBB);
5556
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005557 MI.eraseFromParent();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005558 return JoinMBB;
5559}
5560
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005561// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
5562// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
5563// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
5564// BitSize is the width of the field in bits, or 0 if this is a partword
5565// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
5566// is one of the operands. Invert says whether the field should be
5567// inverted after performing BinOpcode (e.g. for NAND).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005568MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadBinary(
5569 MachineInstr &MI, MachineBasicBlock *MBB, unsigned BinOpcode,
5570 unsigned BitSize, bool Invert) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005571 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005572 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005573 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005574 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005575 bool IsSubWord = (BitSize < 32);
5576
5577 // Extract the operands. Base can be a register or a frame index.
5578 // Src2 can be a register or immediate.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005579 unsigned Dest = MI.getOperand(0).getReg();
5580 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5581 int64_t Disp = MI.getOperand(2).getImm();
5582 MachineOperand Src2 = earlyUseOperand(MI.getOperand(3));
5583 unsigned BitShift = (IsSubWord ? MI.getOperand(4).getReg() : 0);
5584 unsigned NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : 0);
5585 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005586 if (IsSubWord)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005587 BitSize = MI.getOperand(6).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005588
5589 // Subword operations use 32-bit registers.
5590 const TargetRegisterClass *RC = (BitSize <= 32 ?
5591 &SystemZ::GR32BitRegClass :
5592 &SystemZ::GR64BitRegClass);
5593 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5594 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5595
5596 // Get the right opcodes for the displacement.
5597 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5598 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5599 assert(LOpcode && CSOpcode && "Displacement out of range");
5600
5601 // Create virtual registers for temporary results.
5602 unsigned OrigVal = MRI.createVirtualRegister(RC);
5603 unsigned OldVal = MRI.createVirtualRegister(RC);
5604 unsigned NewVal = (BinOpcode || IsSubWord ?
5605 MRI.createVirtualRegister(RC) : Src2.getReg());
5606 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5607 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5608
5609 // Insert a basic block for the main loop.
5610 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005611 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005612 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5613
5614 // StartMBB:
5615 // ...
5616 // %OrigVal = L Disp(%Base)
5617 // # fall through to LoopMMB
5618 MBB = StartMBB;
Diana Picus116bbab2017-01-13 09:58:52 +00005619 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005620 MBB->addSuccessor(LoopMBB);
5621
5622 // LoopMBB:
5623 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
5624 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5625 // %RotatedNewVal = OP %RotatedOldVal, %Src2
5626 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5627 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5628 // JNE LoopMBB
5629 // # fall through to DoneMMB
5630 MBB = LoopMBB;
5631 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5632 .addReg(OrigVal).addMBB(StartMBB)
5633 .addReg(Dest).addMBB(LoopMBB);
5634 if (IsSubWord)
5635 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5636 .addReg(OldVal).addReg(BitShift).addImm(0);
5637 if (Invert) {
5638 // Perform the operation normally and then invert every bit of the field.
5639 unsigned Tmp = MRI.createVirtualRegister(RC);
Diana Picus116bbab2017-01-13 09:58:52 +00005640 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp).addReg(RotatedOldVal).add(Src2);
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005641 if (BitSize <= 32)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005642 // XILF with the upper BitSize bits set.
Richard Sandiford652784e2013-09-25 11:11:53 +00005643 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005644 .addReg(Tmp).addImm(-1U << (32 - BitSize));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005645 else {
5646 // Use LCGR and add -1 to the result, which is more compact than
5647 // an XILF, XILH pair.
5648 unsigned Tmp2 = MRI.createVirtualRegister(RC);
5649 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
5650 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
5651 .addReg(Tmp2).addImm(-1);
5652 }
5653 } else if (BinOpcode)
5654 // A simply binary operation.
5655 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
Diana Picus116bbab2017-01-13 09:58:52 +00005656 .addReg(RotatedOldVal)
5657 .add(Src2);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005658 else if (IsSubWord)
5659 // Use RISBG to rotate Src2 into position and use it to replace the
5660 // field in RotatedOldVal.
5661 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
5662 .addReg(RotatedOldVal).addReg(Src2.getReg())
5663 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
5664 if (IsSubWord)
5665 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5666 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5667 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
Diana Picus116bbab2017-01-13 09:58:52 +00005668 .addReg(OldVal)
5669 .addReg(NewVal)
5670 .add(Base)
5671 .addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005672 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5673 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005674 MBB->addSuccessor(LoopMBB);
5675 MBB->addSuccessor(DoneMBB);
5676
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005677 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005678 return DoneMBB;
5679}
5680
5681// Implement EmitInstrWithCustomInserter for pseudo
5682// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
5683// instruction that should be used to compare the current field with the
5684// minimum or maximum value. KeepOldMask is the BRC condition-code mask
5685// for when the current field should be kept. BitSize is the width of
5686// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005687MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadMinMax(
5688 MachineInstr &MI, MachineBasicBlock *MBB, unsigned CompareOpcode,
5689 unsigned KeepOldMask, unsigned BitSize) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005690 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005691 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005692 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005693 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005694 bool IsSubWord = (BitSize < 32);
5695
5696 // Extract the operands. Base can be a register or a frame index.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005697 unsigned Dest = MI.getOperand(0).getReg();
5698 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5699 int64_t Disp = MI.getOperand(2).getImm();
5700 unsigned Src2 = MI.getOperand(3).getReg();
5701 unsigned BitShift = (IsSubWord ? MI.getOperand(4).getReg() : 0);
5702 unsigned NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : 0);
5703 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005704 if (IsSubWord)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005705 BitSize = MI.getOperand(6).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005706
5707 // Subword operations use 32-bit registers.
5708 const TargetRegisterClass *RC = (BitSize <= 32 ?
5709 &SystemZ::GR32BitRegClass :
5710 &SystemZ::GR64BitRegClass);
5711 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5712 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5713
5714 // Get the right opcodes for the displacement.
5715 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5716 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5717 assert(LOpcode && CSOpcode && "Displacement out of range");
5718
5719 // Create virtual registers for temporary results.
5720 unsigned OrigVal = MRI.createVirtualRegister(RC);
5721 unsigned OldVal = MRI.createVirtualRegister(RC);
5722 unsigned NewVal = MRI.createVirtualRegister(RC);
5723 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5724 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
5725 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5726
5727 // Insert 3 basic blocks for the loop.
5728 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005729 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005730 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5731 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
5732 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
5733
5734 // StartMBB:
5735 // ...
5736 // %OrigVal = L Disp(%Base)
5737 // # fall through to LoopMMB
5738 MBB = StartMBB;
Diana Picus116bbab2017-01-13 09:58:52 +00005739 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005740 MBB->addSuccessor(LoopMBB);
5741
5742 // LoopMBB:
5743 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
5744 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5745 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00005746 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005747 MBB = LoopMBB;
5748 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5749 .addReg(OrigVal).addMBB(StartMBB)
5750 .addReg(Dest).addMBB(UpdateMBB);
5751 if (IsSubWord)
5752 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5753 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005754 BuildMI(MBB, DL, TII->get(CompareOpcode))
5755 .addReg(RotatedOldVal).addReg(Src2);
5756 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005757 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005758 MBB->addSuccessor(UpdateMBB);
5759 MBB->addSuccessor(UseAltMBB);
5760
5761 // UseAltMBB:
5762 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
5763 // # fall through to UpdateMMB
5764 MBB = UseAltMBB;
5765 if (IsSubWord)
5766 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
5767 .addReg(RotatedOldVal).addReg(Src2)
5768 .addImm(32).addImm(31 + BitSize).addImm(0);
5769 MBB->addSuccessor(UpdateMBB);
5770
5771 // UpdateMBB:
5772 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
5773 // [ %RotatedAltVal, UseAltMBB ]
5774 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5775 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5776 // JNE LoopMBB
5777 // # fall through to DoneMMB
5778 MBB = UpdateMBB;
5779 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
5780 .addReg(RotatedOldVal).addMBB(LoopMBB)
5781 .addReg(RotatedAltVal).addMBB(UseAltMBB);
5782 if (IsSubWord)
5783 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5784 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5785 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
Diana Picus116bbab2017-01-13 09:58:52 +00005786 .addReg(OldVal)
5787 .addReg(NewVal)
5788 .add(Base)
5789 .addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005790 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5791 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005792 MBB->addSuccessor(LoopMBB);
5793 MBB->addSuccessor(DoneMBB);
5794
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005795 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005796 return DoneMBB;
5797}
5798
5799// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
5800// instruction MI.
5801MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005802SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005803 MachineBasicBlock *MBB) const {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00005804
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005805 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005806 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005807 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005808 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005809
5810 // Extract the operands. Base can be a register or a frame index.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005811 unsigned Dest = MI.getOperand(0).getReg();
5812 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5813 int64_t Disp = MI.getOperand(2).getImm();
5814 unsigned OrigCmpVal = MI.getOperand(3).getReg();
5815 unsigned OrigSwapVal = MI.getOperand(4).getReg();
5816 unsigned BitShift = MI.getOperand(5).getReg();
5817 unsigned NegBitShift = MI.getOperand(6).getReg();
5818 int64_t BitSize = MI.getOperand(7).getImm();
5819 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005820
5821 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
5822
5823 // Get the right opcodes for the displacement.
5824 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
5825 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
5826 assert(LOpcode && CSOpcode && "Displacement out of range");
5827
5828 // Create virtual registers for temporary results.
5829 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
5830 unsigned OldVal = MRI.createVirtualRegister(RC);
5831 unsigned CmpVal = MRI.createVirtualRegister(RC);
5832 unsigned SwapVal = MRI.createVirtualRegister(RC);
5833 unsigned StoreVal = MRI.createVirtualRegister(RC);
5834 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
5835 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
5836 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
5837
5838 // Insert 2 basic blocks for the loop.
5839 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005840 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005841 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5842 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
5843
5844 // StartMBB:
5845 // ...
5846 // %OrigOldVal = L Disp(%Base)
5847 // # fall through to LoopMMB
5848 MBB = StartMBB;
5849 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
Diana Picus116bbab2017-01-13 09:58:52 +00005850 .add(Base)
5851 .addImm(Disp)
5852 .addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005853 MBB->addSuccessor(LoopMBB);
5854
5855 // LoopMBB:
5856 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
5857 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
5858 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
5859 // %Dest = RLL %OldVal, BitSize(%BitShift)
5860 // ^^ The low BitSize bits contain the field
5861 // of interest.
5862 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
5863 // ^^ Replace the upper 32-BitSize bits of the
5864 // comparison value with those that we loaded,
5865 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005866 // CR %Dest, %RetryCmpVal
5867 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005868 // # Fall through to SetMBB
5869 MBB = LoopMBB;
5870 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5871 .addReg(OrigOldVal).addMBB(StartMBB)
5872 .addReg(RetryOldVal).addMBB(SetMBB);
5873 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
5874 .addReg(OrigCmpVal).addMBB(StartMBB)
5875 .addReg(RetryCmpVal).addMBB(SetMBB);
5876 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
5877 .addReg(OrigSwapVal).addMBB(StartMBB)
5878 .addReg(RetrySwapVal).addMBB(SetMBB);
5879 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
5880 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
5881 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
5882 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005883 BuildMI(MBB, DL, TII->get(SystemZ::CR))
5884 .addReg(Dest).addReg(RetryCmpVal);
5885 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005886 .addImm(SystemZ::CCMASK_ICMP)
5887 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005888 MBB->addSuccessor(DoneMBB);
5889 MBB->addSuccessor(SetMBB);
5890
5891 // SetMBB:
5892 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
5893 // ^^ Replace the upper 32-BitSize bits of the new
5894 // value with those that we loaded.
5895 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
5896 // ^^ Rotate the new field to its proper position.
5897 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
5898 // JNE LoopMBB
5899 // # fall through to ExitMMB
5900 MBB = SetMBB;
5901 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
5902 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
5903 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
5904 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
5905 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
Diana Picus116bbab2017-01-13 09:58:52 +00005906 .addReg(OldVal)
5907 .addReg(StoreVal)
5908 .add(Base)
5909 .addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005910 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5911 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005912 MBB->addSuccessor(LoopMBB);
5913 MBB->addSuccessor(DoneMBB);
5914
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005915 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005916 return DoneMBB;
5917}
5918
Ulrich Weigand43579cf2017-07-05 13:17:31 +00005919// Emit an extension from a GR64 to a GR128. ClearEven is true
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005920// if the high register of the GR128 value must be cleared or false if
Ulrich Weigand43579cf2017-07-05 13:17:31 +00005921// it's "don't care".
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005922MachineBasicBlock *SystemZTargetLowering::emitExt128(MachineInstr &MI,
5923 MachineBasicBlock *MBB,
Ulrich Weigand43579cf2017-07-05 13:17:31 +00005924 bool ClearEven) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005925 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005926 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005927 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005928 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005929 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005930
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005931 unsigned Dest = MI.getOperand(0).getReg();
5932 unsigned Src = MI.getOperand(1).getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005933 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
5934
5935 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
5936 if (ClearEven) {
5937 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
5938 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
5939
5940 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
5941 .addImm(0);
5942 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
Richard Sandiford87a44362013-09-30 10:28:35 +00005943 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005944 In128 = NewIn128;
5945 }
5946 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
Ulrich Weigand43579cf2017-07-05 13:17:31 +00005947 .addReg(In128).addReg(Src).addImm(SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005948
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005949 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005950 return MBB;
5951}
5952
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005953MachineBasicBlock *SystemZTargetLowering::emitMemMemWrapper(
5954 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +00005955 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005956 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005957 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandiford5e318f02013-08-27 09:54:29 +00005958 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005959 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordd131ff82013-07-08 09:35:23 +00005960
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005961 MachineOperand DestBase = earlyUseOperand(MI.getOperand(0));
5962 uint64_t DestDisp = MI.getOperand(1).getImm();
5963 MachineOperand SrcBase = earlyUseOperand(MI.getOperand(2));
5964 uint64_t SrcDisp = MI.getOperand(3).getImm();
5965 uint64_t Length = MI.getOperand(4).getImm();
Richard Sandifordd131ff82013-07-08 09:35:23 +00005966
Richard Sandifordbe133a82013-08-28 09:01:51 +00005967 // When generating more than one CLC, all but the last will need to
5968 // branch to the end when a difference is found.
5969 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
Craig Topper062a2ba2014-04-25 05:30:21 +00005970 splitBlockAfter(MI, MBB) : nullptr);
Richard Sandifordbe133a82013-08-28 09:01:51 +00005971
Richard Sandiford5e318f02013-08-27 09:54:29 +00005972 // Check for the loop form, in which operand 5 is the trip count.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005973 if (MI.getNumExplicitOperands() > 5) {
Richard Sandiford5e318f02013-08-27 09:54:29 +00005974 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
5975
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005976 uint64_t StartCountReg = MI.getOperand(5).getReg();
Richard Sandiford5e318f02013-08-27 09:54:29 +00005977 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
5978 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
5979 forceReg(MI, DestBase, TII));
5980
5981 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
5982 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
5983 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
5984 MRI.createVirtualRegister(RC));
5985 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
5986 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
5987 MRI.createVirtualRegister(RC));
5988
5989 RC = &SystemZ::GR64BitRegClass;
5990 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
5991 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
5992
5993 MachineBasicBlock *StartMBB = MBB;
5994 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
5995 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00005996 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005997
5998 // StartMBB:
5999 // # fall through to LoopMMB
6000 MBB->addSuccessor(LoopMBB);
6001
6002 // LoopMBB:
6003 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00006004 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00006005 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00006006 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00006007 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00006008 // [ %NextCountReg, NextMBB ]
6009 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00006010 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00006011 // ( JLH EndMBB )
6012 //
6013 // The prefetch is used only for MVC. The JLH is used only for CLC.
6014 MBB = LoopMBB;
6015
6016 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
6017 .addReg(StartDestReg).addMBB(StartMBB)
6018 .addReg(NextDestReg).addMBB(NextMBB);
6019 if (!HaveSingleBase)
6020 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
6021 .addReg(StartSrcReg).addMBB(StartMBB)
6022 .addReg(NextSrcReg).addMBB(NextMBB);
6023 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
6024 .addReg(StartCountReg).addMBB(StartMBB)
6025 .addReg(NextCountReg).addMBB(NextMBB);
6026 if (Opcode == SystemZ::MVC)
6027 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
6028 .addImm(SystemZ::PFD_WRITE)
6029 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
6030 BuildMI(MBB, DL, TII->get(Opcode))
6031 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
6032 .addReg(ThisSrcReg).addImm(SrcDisp);
6033 if (EndMBB) {
6034 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6035 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
6036 .addMBB(EndMBB);
6037 MBB->addSuccessor(EndMBB);
6038 MBB->addSuccessor(NextMBB);
6039 }
6040
6041 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00006042 // %NextDestReg = LA 256(%ThisDestReg)
6043 // %NextSrcReg = LA 256(%ThisSrcReg)
6044 // %NextCountReg = AGHI %ThisCountReg, -1
6045 // CGHI %NextCountReg, 0
6046 // JLH LoopMBB
6047 // # fall through to DoneMMB
6048 //
6049 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00006050 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00006051
Richard Sandiford5e318f02013-08-27 09:54:29 +00006052 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
6053 .addReg(ThisDestReg).addImm(256).addReg(0);
6054 if (!HaveSingleBase)
6055 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
6056 .addReg(ThisSrcReg).addImm(256).addReg(0);
6057 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
6058 .addReg(ThisCountReg).addImm(-1);
6059 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
6060 .addReg(NextCountReg).addImm(0);
6061 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6062 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
6063 .addMBB(LoopMBB);
6064 MBB->addSuccessor(LoopMBB);
6065 MBB->addSuccessor(DoneMBB);
6066
6067 DestBase = MachineOperand::CreateReg(NextDestReg, false);
6068 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
6069 Length &= 255;
6070 MBB = DoneMBB;
6071 }
6072 // Handle any remaining bytes with straight-line code.
6073 while (Length > 0) {
6074 uint64_t ThisLength = std::min(Length, uint64_t(256));
6075 // The previous iteration might have created out-of-range displacements.
6076 // Apply them using LAY if so.
6077 if (!isUInt<12>(DestDisp)) {
6078 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006079 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg)
Diana Picus116bbab2017-01-13 09:58:52 +00006080 .add(DestBase)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006081 .addImm(DestDisp)
6082 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006083 DestBase = MachineOperand::CreateReg(Reg, false);
6084 DestDisp = 0;
6085 }
6086 if (!isUInt<12>(SrcDisp)) {
6087 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006088 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg)
Diana Picus116bbab2017-01-13 09:58:52 +00006089 .add(SrcBase)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006090 .addImm(SrcDisp)
6091 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006092 SrcBase = MachineOperand::CreateReg(Reg, false);
6093 SrcDisp = 0;
6094 }
6095 BuildMI(*MBB, MI, DL, TII->get(Opcode))
Diana Picus116bbab2017-01-13 09:58:52 +00006096 .add(DestBase)
6097 .addImm(DestDisp)
6098 .addImm(ThisLength)
6099 .add(SrcBase)
Jonas Paulssonae8d22c2017-06-07 14:08:34 +00006100 .addImm(SrcDisp)
6101 ->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Richard Sandiford5e318f02013-08-27 09:54:29 +00006102 DestDisp += ThisLength;
6103 SrcDisp += ThisLength;
6104 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00006105 // If there's another CLC to go, branch to the end if a difference
6106 // was found.
6107 if (EndMBB && Length > 0) {
6108 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
6109 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6110 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
6111 .addMBB(EndMBB);
6112 MBB->addSuccessor(EndMBB);
6113 MBB->addSuccessor(NextMBB);
6114 MBB = NextMBB;
6115 }
6116 }
6117 if (EndMBB) {
6118 MBB->addSuccessor(EndMBB);
6119 MBB = EndMBB;
6120 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006121 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00006122
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006123 MI.eraseFromParent();
Richard Sandifordd131ff82013-07-08 09:35:23 +00006124 return MBB;
6125}
6126
Richard Sandifordca232712013-08-16 11:21:54 +00006127// Decompose string pseudo-instruction MI into a loop that continually performs
6128// Opcode until CC != 3.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006129MachineBasicBlock *SystemZTargetLowering::emitStringWrapper(
6130 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Richard Sandifordca232712013-08-16 11:21:54 +00006131 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00006132 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00006133 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordca232712013-08-16 11:21:54 +00006134 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006135 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordca232712013-08-16 11:21:54 +00006136
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006137 uint64_t End1Reg = MI.getOperand(0).getReg();
6138 uint64_t Start1Reg = MI.getOperand(1).getReg();
6139 uint64_t Start2Reg = MI.getOperand(2).getReg();
6140 uint64_t CharReg = MI.getOperand(3).getReg();
Richard Sandifordca232712013-08-16 11:21:54 +00006141
6142 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
6143 uint64_t This1Reg = MRI.createVirtualRegister(RC);
6144 uint64_t This2Reg = MRI.createVirtualRegister(RC);
6145 uint64_t End2Reg = MRI.createVirtualRegister(RC);
6146
6147 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00006148 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00006149 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
6150
6151 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00006152 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00006153 MBB->addSuccessor(LoopMBB);
6154
6155 // LoopMBB:
6156 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
6157 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford7789b082013-09-30 08:48:38 +00006158 // R0L = %CharReg
6159 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
Richard Sandifordca232712013-08-16 11:21:54 +00006160 // JO LoopMBB
6161 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00006162 //
Richard Sandiford7789b082013-09-30 08:48:38 +00006163 // The load of R0L can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00006164 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00006165
6166 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
6167 .addReg(Start1Reg).addMBB(StartMBB)
6168 .addReg(End1Reg).addMBB(LoopMBB);
6169 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
6170 .addReg(Start2Reg).addMBB(StartMBB)
6171 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford7789b082013-09-30 08:48:38 +00006172 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00006173 BuildMI(MBB, DL, TII->get(Opcode))
6174 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
6175 .addReg(This1Reg).addReg(This2Reg);
6176 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6177 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
6178 MBB->addSuccessor(LoopMBB);
6179 MBB->addSuccessor(DoneMBB);
6180
6181 DoneMBB->addLiveIn(SystemZ::CC);
6182
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006183 MI.eraseFromParent();
Richard Sandifordca232712013-08-16 11:21:54 +00006184 return DoneMBB;
6185}
6186
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006187// Update TBEGIN instruction with final opcode and register clobbers.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006188MachineBasicBlock *SystemZTargetLowering::emitTransactionBegin(
6189 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode,
6190 bool NoFloat) const {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006191 MachineFunction &MF = *MBB->getParent();
6192 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
6193 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
6194
6195 // Update opcode.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006196 MI.setDesc(TII->get(Opcode));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006197
6198 // We cannot handle a TBEGIN that clobbers the stack or frame pointer.
6199 // Make sure to add the corresponding GRSM bits if they are missing.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006200 uint64_t Control = MI.getOperand(2).getImm();
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006201 static const unsigned GPRControlBit[16] = {
6202 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000,
6203 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100
6204 };
6205 Control |= GPRControlBit[15];
6206 if (TFI->hasFP(MF))
6207 Control |= GPRControlBit[11];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006208 MI.getOperand(2).setImm(Control);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006209
6210 // Add GPR clobbers.
6211 for (int I = 0; I < 16; I++) {
6212 if ((Control & GPRControlBit[I]) == 0) {
6213 unsigned Reg = SystemZMC::GR64Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006214 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006215 }
6216 }
6217
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006218 // Add FPR/VR clobbers.
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006219 if (!NoFloat && (Control & 4) != 0) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006220 if (Subtarget.hasVector()) {
6221 for (int I = 0; I < 32; I++) {
6222 unsigned Reg = SystemZMC::VR128Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006223 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006224 }
6225 } else {
6226 for (int I = 0; I < 16; I++) {
6227 unsigned Reg = SystemZMC::FP64Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006228 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006229 }
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006230 }
6231 }
6232
6233 return MBB;
6234}
6235
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006236MachineBasicBlock *SystemZTargetLowering::emitLoadAndTestCmp0(
6237 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006238 MachineFunction &MF = *MBB->getParent();
6239 MachineRegisterInfo *MRI = &MF.getRegInfo();
6240 const SystemZInstrInfo *TII =
6241 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006242 DebugLoc DL = MI.getDebugLoc();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006243
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006244 unsigned SrcReg = MI.getOperand(0).getReg();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006245
6246 // Create new virtual register of the same class as source.
6247 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
6248 unsigned DstReg = MRI->createVirtualRegister(RC);
6249
6250 // Replace pseudo with a normal load-and-test that models the def as
6251 // well.
6252 BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg)
6253 .addReg(SrcReg);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006254 MI.eraseFromParent();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006255
6256 return MBB;
6257}
6258
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006259MachineBasicBlock *SystemZTargetLowering::EmitInstrWithCustomInserter(
6260 MachineInstr &MI, MachineBasicBlock *MBB) const {
6261 switch (MI.getOpcode()) {
Richard Sandiford7c5c0ea2013-10-01 13:10:16 +00006262 case SystemZ::Select32Mux:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006263 return emitSelect(MI, MBB,
6264 Subtarget.hasLoadStoreOnCond2()? SystemZ::LOCRMux : 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006265 case SystemZ::Select32:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006266 return emitSelect(MI, MBB, SystemZ::LOCR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006267 case SystemZ::Select64:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006268 return emitSelect(MI, MBB, SystemZ::LOCGR);
6269 case SystemZ::SelectF32:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006270 case SystemZ::SelectF64:
6271 case SystemZ::SelectF128:
Ulrich Weigandf2968d52017-07-17 17:44:20 +00006272 case SystemZ::SelectVR128:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006273 return emitSelect(MI, MBB, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006274
Richard Sandiford2896d042013-10-01 14:33:55 +00006275 case SystemZ::CondStore8Mux:
6276 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
6277 case SystemZ::CondStore8MuxInv:
6278 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
6279 case SystemZ::CondStore16Mux:
6280 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
6281 case SystemZ::CondStore16MuxInv:
6282 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
Ulrich Weigand524f2762016-11-28 13:34:08 +00006283 case SystemZ::CondStore32Mux:
6284 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, false);
6285 case SystemZ::CondStore32MuxInv:
6286 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006287 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006288 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006289 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006290 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006291 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006292 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006293 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006294 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006295 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006296 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006297 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006298 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006299 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006300 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006301 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006302 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006303 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006304 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006305 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006306 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006307 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006308 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006309 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006310 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006311
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006312 case SystemZ::AEXT128:
6313 return emitExt128(MI, MBB, false);
6314 case SystemZ::ZEXT128:
6315 return emitExt128(MI, MBB, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006316
6317 case SystemZ::ATOMIC_SWAPW:
6318 return emitAtomicLoadBinary(MI, MBB, 0, 0);
6319 case SystemZ::ATOMIC_SWAP_32:
6320 return emitAtomicLoadBinary(MI, MBB, 0, 32);
6321 case SystemZ::ATOMIC_SWAP_64:
6322 return emitAtomicLoadBinary(MI, MBB, 0, 64);
6323
6324 case SystemZ::ATOMIC_LOADW_AR:
6325 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
6326 case SystemZ::ATOMIC_LOADW_AFI:
6327 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
6328 case SystemZ::ATOMIC_LOAD_AR:
6329 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
6330 case SystemZ::ATOMIC_LOAD_AHI:
6331 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
6332 case SystemZ::ATOMIC_LOAD_AFI:
6333 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
6334 case SystemZ::ATOMIC_LOAD_AGR:
6335 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
6336 case SystemZ::ATOMIC_LOAD_AGHI:
6337 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
6338 case SystemZ::ATOMIC_LOAD_AGFI:
6339 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
6340
6341 case SystemZ::ATOMIC_LOADW_SR:
6342 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
6343 case SystemZ::ATOMIC_LOAD_SR:
6344 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
6345 case SystemZ::ATOMIC_LOAD_SGR:
6346 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
6347
6348 case SystemZ::ATOMIC_LOADW_NR:
6349 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
6350 case SystemZ::ATOMIC_LOADW_NILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00006351 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006352 case SystemZ::ATOMIC_LOAD_NR:
6353 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006354 case SystemZ::ATOMIC_LOAD_NILL:
6355 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
6356 case SystemZ::ATOMIC_LOAD_NILH:
6357 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
6358 case SystemZ::ATOMIC_LOAD_NILF:
6359 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006360 case SystemZ::ATOMIC_LOAD_NGR:
6361 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006362 case SystemZ::ATOMIC_LOAD_NILL64:
6363 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
6364 case SystemZ::ATOMIC_LOAD_NILH64:
6365 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00006366 case SystemZ::ATOMIC_LOAD_NIHL64:
6367 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
6368 case SystemZ::ATOMIC_LOAD_NIHH64:
6369 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006370 case SystemZ::ATOMIC_LOAD_NILF64:
6371 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00006372 case SystemZ::ATOMIC_LOAD_NIHF64:
6373 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006374
6375 case SystemZ::ATOMIC_LOADW_OR:
6376 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
6377 case SystemZ::ATOMIC_LOADW_OILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00006378 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006379 case SystemZ::ATOMIC_LOAD_OR:
6380 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006381 case SystemZ::ATOMIC_LOAD_OILL:
6382 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
6383 case SystemZ::ATOMIC_LOAD_OILH:
6384 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
6385 case SystemZ::ATOMIC_LOAD_OILF:
6386 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006387 case SystemZ::ATOMIC_LOAD_OGR:
6388 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006389 case SystemZ::ATOMIC_LOAD_OILL64:
6390 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
6391 case SystemZ::ATOMIC_LOAD_OILH64:
6392 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00006393 case SystemZ::ATOMIC_LOAD_OIHL64:
6394 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
6395 case SystemZ::ATOMIC_LOAD_OIHH64:
6396 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006397 case SystemZ::ATOMIC_LOAD_OILF64:
6398 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00006399 case SystemZ::ATOMIC_LOAD_OIHF64:
6400 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006401
6402 case SystemZ::ATOMIC_LOADW_XR:
6403 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
6404 case SystemZ::ATOMIC_LOADW_XILF:
Richard Sandiford652784e2013-09-25 11:11:53 +00006405 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006406 case SystemZ::ATOMIC_LOAD_XR:
6407 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006408 case SystemZ::ATOMIC_LOAD_XILF:
6409 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006410 case SystemZ::ATOMIC_LOAD_XGR:
6411 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006412 case SystemZ::ATOMIC_LOAD_XILF64:
6413 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
Richard Sandiford5718dac2013-10-01 14:08:44 +00006414 case SystemZ::ATOMIC_LOAD_XIHF64:
6415 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006416
6417 case SystemZ::ATOMIC_LOADW_NRi:
6418 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
6419 case SystemZ::ATOMIC_LOADW_NILHi:
Richard Sandiford652784e2013-09-25 11:11:53 +00006420 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006421 case SystemZ::ATOMIC_LOAD_NRi:
6422 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006423 case SystemZ::ATOMIC_LOAD_NILLi:
6424 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
6425 case SystemZ::ATOMIC_LOAD_NILHi:
6426 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
6427 case SystemZ::ATOMIC_LOAD_NILFi:
6428 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006429 case SystemZ::ATOMIC_LOAD_NGRi:
6430 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006431 case SystemZ::ATOMIC_LOAD_NILL64i:
6432 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
6433 case SystemZ::ATOMIC_LOAD_NILH64i:
6434 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00006435 case SystemZ::ATOMIC_LOAD_NIHL64i:
6436 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
6437 case SystemZ::ATOMIC_LOAD_NIHH64i:
6438 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006439 case SystemZ::ATOMIC_LOAD_NILF64i:
6440 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00006441 case SystemZ::ATOMIC_LOAD_NIHF64i:
6442 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006443
6444 case SystemZ::ATOMIC_LOADW_MIN:
6445 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6446 SystemZ::CCMASK_CMP_LE, 0);
6447 case SystemZ::ATOMIC_LOAD_MIN_32:
6448 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6449 SystemZ::CCMASK_CMP_LE, 32);
6450 case SystemZ::ATOMIC_LOAD_MIN_64:
6451 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
6452 SystemZ::CCMASK_CMP_LE, 64);
6453
6454 case SystemZ::ATOMIC_LOADW_MAX:
6455 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6456 SystemZ::CCMASK_CMP_GE, 0);
6457 case SystemZ::ATOMIC_LOAD_MAX_32:
6458 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6459 SystemZ::CCMASK_CMP_GE, 32);
6460 case SystemZ::ATOMIC_LOAD_MAX_64:
6461 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
6462 SystemZ::CCMASK_CMP_GE, 64);
6463
6464 case SystemZ::ATOMIC_LOADW_UMIN:
6465 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6466 SystemZ::CCMASK_CMP_LE, 0);
6467 case SystemZ::ATOMIC_LOAD_UMIN_32:
6468 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6469 SystemZ::CCMASK_CMP_LE, 32);
6470 case SystemZ::ATOMIC_LOAD_UMIN_64:
6471 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6472 SystemZ::CCMASK_CMP_LE, 64);
6473
6474 case SystemZ::ATOMIC_LOADW_UMAX:
6475 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6476 SystemZ::CCMASK_CMP_GE, 0);
6477 case SystemZ::ATOMIC_LOAD_UMAX_32:
6478 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6479 SystemZ::CCMASK_CMP_GE, 32);
6480 case SystemZ::ATOMIC_LOAD_UMAX_64:
6481 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6482 SystemZ::CCMASK_CMP_GE, 64);
6483
6484 case SystemZ::ATOMIC_CMP_SWAPW:
6485 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006486 case SystemZ::MVCSequence:
6487 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006488 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00006489 case SystemZ::NCSequence:
6490 case SystemZ::NCLoop:
6491 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
6492 case SystemZ::OCSequence:
6493 case SystemZ::OCLoop:
6494 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
6495 case SystemZ::XCSequence:
6496 case SystemZ::XCLoop:
6497 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006498 case SystemZ::CLCSequence:
6499 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006500 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00006501 case SystemZ::CLSTLoop:
6502 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00006503 case SystemZ::MVSTLoop:
6504 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00006505 case SystemZ::SRSTLoop:
6506 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006507 case SystemZ::TBEGIN:
6508 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false);
6509 case SystemZ::TBEGIN_nofloat:
6510 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true);
6511 case SystemZ::TBEGINC:
6512 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true);
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006513 case SystemZ::LTEBRCompare_VecPseudo:
6514 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR);
6515 case SystemZ::LTDBRCompare_VecPseudo:
6516 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR);
6517 case SystemZ::LTXBRCompare_VecPseudo:
6518 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR);
6519
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006520 default:
6521 llvm_unreachable("Unexpected instr type to insert");
6522 }
6523}
Jonas Paulsson11d251c2017-05-10 13:03:25 +00006524
6525// This is only used by the isel schedulers, and is needed only to prevent
6526// compiler from crashing when list-ilp is used.
6527const TargetRegisterClass *
6528SystemZTargetLowering::getRepRegClassFor(MVT VT) const {
6529 if (VT == MVT::Untyped)
6530 return &SystemZ::ADDR128BitRegClass;
6531 return TargetLowering::getRepRegClassFor(VT);
6532}