blob: 944284d0fb8af1bddc5efc1431839e4bb3204f4d [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- SystemZISelLowering.cpp - SystemZ DAG lowering implementation -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SystemZTargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
Ulrich Weigand5f613df2013-05-06 16:15:19 +000014#include "SystemZISelLowering.h"
15#include "SystemZCallingConv.h"
16#include "SystemZConstantPoolValue.h"
17#include "SystemZMachineFunctionInfo.h"
18#include "SystemZTargetMachine.h"
19#include "llvm/CodeGen/CallingConvLower.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
22#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Ulrich Weigand57c85f52015-04-01 12:51:43 +000023#include "llvm/IR/Intrinsics.h"
Reid Kleckner0e8c4bb2017-09-07 23:27:44 +000024#include "llvm/IR/IntrinsicInst.h"
Craig Topperd0af7e82017-04-28 05:31:46 +000025#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/KnownBits.h"
Will Dietz981af002013-10-12 00:55:57 +000027#include <cctype>
28
Ulrich Weigand5f613df2013-05-06 16:15:19 +000029using namespace llvm;
30
Chandler Carruth84e68b22014-04-22 02:41:26 +000031#define DEBUG_TYPE "systemz-lower"
32
Richard Sandifordf722a8e302013-10-16 11:10:55 +000033namespace {
34// Represents a sequence for extracting a 0/1 value from an IPM result:
35// (((X ^ XORValue) + AddValue) >> Bit)
36struct IPMConversion {
37 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
38 : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
39
40 int64_t XORValue;
41 int64_t AddValue;
42 unsigned Bit;
43};
Richard Sandifordd420f732013-12-13 15:28:45 +000044
45// Represents information about a comparison.
46struct Comparison {
47 Comparison(SDValue Op0In, SDValue Op1In)
48 : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {}
49
50 // The operands to the comparison.
51 SDValue Op0, Op1;
52
53 // The opcode that should be used to compare Op0 and Op1.
54 unsigned Opcode;
55
56 // A SystemZICMP value. Only used for integer comparisons.
57 unsigned ICmpType;
58
59 // The mask of CC values that Opcode can produce.
60 unsigned CCValid;
61
62 // The mask of CC values for which the original condition is true.
63 unsigned CCMask;
64};
Richard Sandifordc2312692014-03-06 10:38:30 +000065} // end anonymous namespace
Richard Sandifordf722a8e302013-10-16 11:10:55 +000066
Ulrich Weigand5f613df2013-05-06 16:15:19 +000067// Classify VT as either 32 or 64 bit.
68static bool is32Bit(EVT VT) {
69 switch (VT.getSimpleVT().SimpleTy) {
70 case MVT::i32:
71 return true;
72 case MVT::i64:
73 return false;
74 default:
75 llvm_unreachable("Unsupported type");
76 }
77}
78
79// Return a version of MachineOperand that can be safely used before the
80// final use.
81static MachineOperand earlyUseOperand(MachineOperand Op) {
82 if (Op.isReg())
83 Op.setIsKill(false);
84 return Op;
85}
86
Mehdi Amini44ede332015-07-09 02:09:04 +000087SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM,
Eric Christophera6734172015-01-31 00:06:45 +000088 const SystemZSubtarget &STI)
Mehdi Amini44ede332015-07-09 02:09:04 +000089 : TargetLowering(TM), Subtarget(STI) {
Mehdi Amini26d48132015-07-24 16:04:22 +000090 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
Ulrich Weigand5f613df2013-05-06 16:15:19 +000091
92 // Set up the register classes.
Richard Sandiford0755c932013-10-01 11:26:28 +000093 if (Subtarget.hasHighWord())
94 addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass);
95 else
96 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
Ulrich Weigand49506d72015-05-05 19:28:34 +000097 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
98 if (Subtarget.hasVector()) {
99 addRegisterClass(MVT::f32, &SystemZ::VR32BitRegClass);
100 addRegisterClass(MVT::f64, &SystemZ::VR64BitRegClass);
101 } else {
102 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
103 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
104 }
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000105 if (Subtarget.hasVectorEnhancements1())
106 addRegisterClass(MVT::f128, &SystemZ::VR128BitRegClass);
107 else
108 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000109
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000110 if (Subtarget.hasVector()) {
111 addRegisterClass(MVT::v16i8, &SystemZ::VR128BitRegClass);
112 addRegisterClass(MVT::v8i16, &SystemZ::VR128BitRegClass);
113 addRegisterClass(MVT::v4i32, &SystemZ::VR128BitRegClass);
114 addRegisterClass(MVT::v2i64, &SystemZ::VR128BitRegClass);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000115 addRegisterClass(MVT::v4f32, &SystemZ::VR128BitRegClass);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000116 addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000117 }
118
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000119 // Compute derived properties from the register classes
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000120 computeRegisterProperties(Subtarget.getRegisterInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000121
122 // Set up special registers.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000123 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
124
125 // TODO: It may be better to default to latency-oriented scheduling, however
126 // LLVM's current latency-oriented scheduler can't handle physreg definitions
Richard Sandiford14a44492013-05-22 13:38:45 +0000127 // such as SystemZ has with CC, so set this to the register-pressure
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000128 // scheduler, because it can.
129 setSchedulingPreference(Sched::RegPressure);
130
131 setBooleanContents(ZeroOrOneBooleanContent);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000132 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000133
134 // Instructions are strings of 2-byte aligned 2-byte values.
135 setMinFunctionAlignment(2);
136
137 // Handle operations that are handled in a similar way for all types.
138 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
139 I <= MVT::LAST_FP_VALUETYPE;
140 ++I) {
141 MVT VT = MVT::SimpleValueType(I);
142 if (isTypeLegal(VT)) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000143 // Lower SET_CC into an IPM-based sequence.
144 setOperationAction(ISD::SETCC, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000145
146 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
147 setOperationAction(ISD::SELECT, VT, Expand);
148
149 // Lower SELECT_CC and BR_CC into separate comparisons and branches.
150 setOperationAction(ISD::SELECT_CC, VT, Custom);
151 setOperationAction(ISD::BR_CC, VT, Custom);
152 }
153 }
154
155 // Expand jump table branches as address arithmetic followed by an
156 // indirect jump.
157 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
158
159 // Expand BRCOND into a BR_CC (see above).
160 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
161
162 // Handle integer types.
163 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
164 I <= MVT::LAST_INTEGER_VALUETYPE;
165 ++I) {
166 MVT VT = MVT::SimpleValueType(I);
167 if (isTypeLegal(VT)) {
168 // Expand individual DIV and REMs into DIVREMs.
169 setOperationAction(ISD::SDIV, VT, Expand);
170 setOperationAction(ISD::UDIV, VT, Expand);
171 setOperationAction(ISD::SREM, VT, Expand);
172 setOperationAction(ISD::UREM, VT, Expand);
173 setOperationAction(ISD::SDIVREM, VT, Custom);
174 setOperationAction(ISD::UDIVREM, VT, Custom);
175
Richard Sandifordbef3d7a2013-12-10 10:49:34 +0000176 // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and
177 // stores, putting a serialization instruction after the stores.
178 setOperationAction(ISD::ATOMIC_LOAD, VT, Custom);
179 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000180
Richard Sandiford41350a52013-12-24 15:18:04 +0000181 // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are
182 // available, or if the operand is constant.
183 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
184
Ulrich Weigandb4012182015-03-31 12:56:33 +0000185 // Use POPCNT on z196 and above.
186 if (Subtarget.hasPopulationCount())
187 setOperationAction(ISD::CTPOP, VT, Custom);
188 else
189 setOperationAction(ISD::CTPOP, VT, Expand);
190
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000191 // No special instructions for these.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000192 setOperationAction(ISD::CTTZ, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000193 setOperationAction(ISD::ROTR, VT, Expand);
194
Richard Sandiford7d86e472013-08-21 09:34:56 +0000195 // Use *MUL_LOHI where possible instead of MULH*.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000196 setOperationAction(ISD::MULHS, VT, Expand);
197 setOperationAction(ISD::MULHU, VT, Expand);
Richard Sandiford7d86e472013-08-21 09:34:56 +0000198 setOperationAction(ISD::SMUL_LOHI, VT, Custom);
199 setOperationAction(ISD::UMUL_LOHI, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000200
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000201 // Only z196 and above have native support for conversions to unsigned.
Jonas Paulssonb7a2ef82017-02-02 15:42:14 +0000202 // On z10, promoting to i64 doesn't generate an inexact condition for
203 // values that are outside the i32 range but in the i64 range, so use
204 // the default expansion.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000205 if (!Subtarget.hasFPExtension())
206 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000207 }
208 }
209
210 // Type legalization will convert 8- and 16-bit atomic operations into
211 // forms that operate on i32s (but still keeping the original memory VT).
212 // Lower them into full i32 operations.
213 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom);
214 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom);
215 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
216 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom);
217 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom);
218 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom);
219 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
220 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom);
221 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom);
222 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
223 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000224
Ulrich Weiganda11f63a2017-08-04 18:57:58 +0000225 // Even though i128 is not a legal type, we still need to custom lower
226 // the atomic operations in order to exploit SystemZ instructions.
227 setOperationAction(ISD::ATOMIC_LOAD, MVT::i128, Custom);
228 setOperationAction(ISD::ATOMIC_STORE, MVT::i128, Custom);
Ulrich Weigand0f1de042017-09-28 16:22:54 +0000229
230 // We can use the CC result of compare-and-swap to implement
231 // the "success" result of ATOMIC_CMP_SWAP_WITH_SUCCESS.
232 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Custom);
233 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Custom);
234 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i128, Custom);
Ulrich Weiganda11f63a2017-08-04 18:57:58 +0000235
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +0000236 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
237
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000238 // Traps are legal, as we will convert them to "j .+2".
239 setOperationAction(ISD::TRAP, MVT::Other, Legal);
240
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000241 // z10 has instructions for signed but not unsigned FP conversion.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000242 // Handle unsigned 32-bit types as signed 64-bit types.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000243 if (!Subtarget.hasFPExtension()) {
244 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
245 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
246 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000247
248 // We have native support for a 64-bit CTLZ, via FLOGR.
249 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
250 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
251
252 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
253 setOperationAction(ISD::OR, MVT::i64, Custom);
254
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000255 // FIXME: Can we support these natively?
256 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
257 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
258 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
259
260 // We have native instructions for i8, i16 and i32 extensions, but not i1.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000261 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000262 for (MVT VT : MVT::integer_valuetypes()) {
263 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
264 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
265 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
266 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000267
268 // Handle the various types of symbolic address.
269 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
270 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
271 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
272 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
273 setOperationAction(ISD::JumpTable, PtrVT, Custom);
274
275 // We need to handle dynamic allocations specially because of the
276 // 160-byte area at the bottom of the stack.
277 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +0000278 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, PtrVT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000279
280 // Use custom expanders so that we can force the function to use
281 // a frame pointer.
282 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
283 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
284
Richard Sandiford03481332013-08-23 11:36:42 +0000285 // Handle prefetches with PFD or PFDRL.
286 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
287
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000288 for (MVT VT : MVT::vector_valuetypes()) {
289 // Assume by default that all vector operations need to be expanded.
290 for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode)
291 if (getOperationAction(Opcode, VT) == Legal)
292 setOperationAction(Opcode, VT, Expand);
293
294 // Likewise all truncating stores and extending loads.
295 for (MVT InnerVT : MVT::vector_valuetypes()) {
296 setTruncStoreAction(VT, InnerVT, Expand);
297 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
298 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
299 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
300 }
301
302 if (isTypeLegal(VT)) {
303 // These operations are legal for anything that can be stored in a
304 // vector register, even if there is no native support for the format
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000305 // as such. In particular, we can do these for v4f32 even though there
306 // are no specific instructions for that format.
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000307 setOperationAction(ISD::LOAD, VT, Legal);
308 setOperationAction(ISD::STORE, VT, Legal);
309 setOperationAction(ISD::VSELECT, VT, Legal);
310 setOperationAction(ISD::BITCAST, VT, Legal);
311 setOperationAction(ISD::UNDEF, VT, Legal);
312
313 // Likewise, except that we need to replace the nodes with something
314 // more specific.
315 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
316 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
317 }
318 }
319
320 // Handle integer vector types.
321 for (MVT VT : MVT::integer_vector_valuetypes()) {
322 if (isTypeLegal(VT)) {
323 // These operations have direct equivalents.
324 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal);
325 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal);
326 setOperationAction(ISD::ADD, VT, Legal);
327 setOperationAction(ISD::SUB, VT, Legal);
328 if (VT != MVT::v2i64)
329 setOperationAction(ISD::MUL, VT, Legal);
330 setOperationAction(ISD::AND, VT, Legal);
331 setOperationAction(ISD::OR, VT, Legal);
332 setOperationAction(ISD::XOR, VT, Legal);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000333 if (Subtarget.hasVectorEnhancements1())
334 setOperationAction(ISD::CTPOP, VT, Legal);
335 else
336 setOperationAction(ISD::CTPOP, VT, Custom);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000337 setOperationAction(ISD::CTTZ, VT, Legal);
338 setOperationAction(ISD::CTLZ, VT, Legal);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000339
340 // Convert a GPR scalar to a vector by inserting it into element 0.
341 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
342
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000343 // Use a series of unpacks for extensions.
344 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Custom);
345 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Custom);
346
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000347 // Detect shifts by a scalar amount and convert them into
348 // V*_BY_SCALAR.
349 setOperationAction(ISD::SHL, VT, Custom);
350 setOperationAction(ISD::SRA, VT, Custom);
351 setOperationAction(ISD::SRL, VT, Custom);
352
353 // At present ROTL isn't matched by DAGCombiner. ROTR should be
354 // converted into ROTL.
355 setOperationAction(ISD::ROTL, VT, Expand);
356 setOperationAction(ISD::ROTR, VT, Expand);
357
358 // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands
359 // and inverting the result as necessary.
360 setOperationAction(ISD::SETCC, VT, Custom);
361 }
362 }
363
Ulrich Weigandcd808232015-05-05 19:26:48 +0000364 if (Subtarget.hasVector()) {
365 // There should be no need to check for float types other than v2f64
366 // since <2 x f32> isn't a legal type.
367 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000368 setOperationAction(ISD::FP_TO_SINT, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000369 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000370 setOperationAction(ISD::FP_TO_UINT, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000371 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000372 setOperationAction(ISD::SINT_TO_FP, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000373 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000374 setOperationAction(ISD::UINT_TO_FP, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000375 }
376
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000377 // Handle floating-point types.
378 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
379 I <= MVT::LAST_FP_VALUETYPE;
380 ++I) {
381 MVT VT = MVT::SimpleValueType(I);
382 if (isTypeLegal(VT)) {
383 // We can use FI for FRINT.
384 setOperationAction(ISD::FRINT, VT, Legal);
385
Richard Sandifordaf5f66a2013-08-21 09:04:20 +0000386 // We can use the extended form of FI for other rounding operations.
387 if (Subtarget.hasFPExtension()) {
388 setOperationAction(ISD::FNEARBYINT, VT, Legal);
389 setOperationAction(ISD::FFLOOR, VT, Legal);
390 setOperationAction(ISD::FCEIL, VT, Legal);
391 setOperationAction(ISD::FTRUNC, VT, Legal);
392 setOperationAction(ISD::FROUND, VT, Legal);
393 }
394
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000395 // No special instructions for these.
396 setOperationAction(ISD::FSIN, VT, Expand);
397 setOperationAction(ISD::FCOS, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000398 setOperationAction(ISD::FSINCOS, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000399 setOperationAction(ISD::FREM, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000400 setOperationAction(ISD::FPOW, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000401 }
402 }
403
Ulrich Weigandcd808232015-05-05 19:26:48 +0000404 // Handle floating-point vector types.
405 if (Subtarget.hasVector()) {
406 // Scalar-to-vector conversion is just a subreg.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000407 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000408 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
409
410 // Some insertions and extractions can be done directly but others
411 // need to go via integers.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000412 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000413 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000414 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000415 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
416
417 // These operations have direct equivalents.
418 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
419 setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
420 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
421 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
422 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
423 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
424 setOperationAction(ISD::FABS, MVT::v2f64, Legal);
425 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
426 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
427 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
428 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
429 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
430 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
431 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
432 }
433
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000434 // The vector enhancements facility 1 has instructions for these.
435 if (Subtarget.hasVectorEnhancements1()) {
Ulrich Weigand33435c42017-07-17 17:42:48 +0000436 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
437 setOperationAction(ISD::FNEG, MVT::v4f32, Legal);
438 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
439 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
440 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
441 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
442 setOperationAction(ISD::FABS, MVT::v4f32, Legal);
443 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
444 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
445 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
446 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
447 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
448 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
449 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
450
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000451 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
452 setOperationAction(ISD::FMAXNAN, MVT::f64, Legal);
453 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
454 setOperationAction(ISD::FMINNAN, MVT::f64, Legal);
455
456 setOperationAction(ISD::FMAXNUM, MVT::v2f64, Legal);
457 setOperationAction(ISD::FMAXNAN, MVT::v2f64, Legal);
458 setOperationAction(ISD::FMINNUM, MVT::v2f64, Legal);
459 setOperationAction(ISD::FMINNAN, MVT::v2f64, Legal);
Ulrich Weigand33435c42017-07-17 17:42:48 +0000460
461 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
462 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal);
463 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
464 setOperationAction(ISD::FMINNAN, MVT::f32, Legal);
465
466 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
467 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal);
468 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
469 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal);
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000470
471 setOperationAction(ISD::FMAXNUM, MVT::f128, Legal);
472 setOperationAction(ISD::FMAXNAN, MVT::f128, Legal);
473 setOperationAction(ISD::FMINNUM, MVT::f128, Legal);
474 setOperationAction(ISD::FMINNAN, MVT::f128, Legal);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000475 }
476
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000477 // We have fused multiply-addition for f32 and f64 but not f128.
478 setOperationAction(ISD::FMA, MVT::f32, Legal);
479 setOperationAction(ISD::FMA, MVT::f64, Legal);
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000480 if (Subtarget.hasVectorEnhancements1())
481 setOperationAction(ISD::FMA, MVT::f128, Legal);
482 else
483 setOperationAction(ISD::FMA, MVT::f128, Expand);
484
485 // We don't have a copysign instruction on vector registers.
486 if (Subtarget.hasVectorEnhancements1())
487 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000488
489 // Needed so that we don't try to implement f128 constant loads using
490 // a load-and-extend of a f80 constant (in cases where the constant
491 // would fit in an f80).
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000492 for (MVT VT : MVT::fp_valuetypes())
493 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000494
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000495 // We don't have extending load instruction on vector registers.
496 if (Subtarget.hasVectorEnhancements1()) {
497 setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f32, Expand);
498 setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f64, Expand);
499 }
500
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000501 // Floating-point truncation and stores need to be done separately.
502 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
503 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
504 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
505
506 // We have 64-bit FPR<->GPR moves, but need special handling for
507 // 32-bit forms.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000508 if (!Subtarget.hasVector()) {
509 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
510 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
511 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000512
513 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
514 // structure, but VAEND is a no-op.
515 setOperationAction(ISD::VASTART, MVT::Other, Custom);
516 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
517 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Richard Sandifordd131ff82013-07-08 09:35:23 +0000518
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000519 // Codes for which we want to perform some z-specific combinations.
520 setTargetDAGCombine(ISD::SIGN_EXTEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000521 setTargetDAGCombine(ISD::STORE);
522 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000523 setTargetDAGCombine(ISD::FP_ROUND);
Bryan Chan28b759c2016-05-16 20:32:22 +0000524 setTargetDAGCombine(ISD::BSWAP);
Elliot Colpbc2cfc22016-07-06 18:13:11 +0000525 setTargetDAGCombine(ISD::SHL);
526 setTargetDAGCombine(ISD::SRA);
527 setTargetDAGCombine(ISD::SRL);
528 setTargetDAGCombine(ISD::ROTL);
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000529
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000530 // Handle intrinsics.
531 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Ulrich Weigandc1708b22015-05-05 19:31:09 +0000532 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000533
Richard Sandifordd131ff82013-07-08 09:35:23 +0000534 // We want to use MVC in preference to even a single load/store pair.
535 MaxStoresPerMemcpy = 0;
536 MaxStoresPerMemcpyOptSize = 0;
Richard Sandiford47660c12013-07-09 09:32:42 +0000537
538 // The main memset sequence is a byte store followed by an MVC.
539 // Two STC or MV..I stores win over that, but the kind of fused stores
540 // generated by target-independent code don't when the byte value is
541 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
542 // than "STC;MVC". Handle the choice in target-specific code instead.
543 MaxStoresPerMemset = 0;
544 MaxStoresPerMemsetOptSize = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000545}
546
Mehdi Amini44ede332015-07-09 02:09:04 +0000547EVT SystemZTargetLowering::getSetCCResultType(const DataLayout &DL,
548 LLVMContext &, EVT VT) const {
Richard Sandifordabc010b2013-11-06 12:16:02 +0000549 if (!VT.isVector())
550 return MVT::i32;
551 return VT.changeVectorElementTypeToInteger();
552}
553
554bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
Stephen Lin73de7bf2013-07-09 18:16:56 +0000555 VT = VT.getScalarType();
556
557 if (!VT.isSimple())
558 return false;
559
560 switch (VT.getSimpleVT().SimpleTy) {
561 case MVT::f32:
562 case MVT::f64:
563 return true;
564 case MVT::f128:
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000565 return Subtarget.hasVectorEnhancements1();
Stephen Lin73de7bf2013-07-09 18:16:56 +0000566 default:
567 break;
568 }
569
570 return false;
571}
572
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000573bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
574 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
575 return Imm.isZero() || Imm.isNegZero();
576}
577
Ulrich Weigand1f6666a2015-03-31 12:52:27 +0000578bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
579 // We can use CGFI or CLGFI.
580 return isInt<32>(Imm) || isUInt<32>(Imm);
581}
582
583bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const {
584 // We can use ALGFI or SLGFI.
585 return isUInt<32>(Imm) || isUInt<32>(-Imm);
586}
587
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000588bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
589 unsigned,
590 unsigned,
591 bool *Fast) const {
Richard Sandiford46af5a22013-05-30 09:45:42 +0000592 // Unaligned accesses should never be slower than the expanded version.
593 // We check specifically for aligned accesses in the few cases where
594 // they are required.
595 if (Fast)
596 *Fast = true;
597 return true;
598}
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +0000599
Jonas Paulsson024e3192017-07-21 11:59:37 +0000600// Information about the addressing mode for a memory access.
601struct AddressingMode {
602 // True if a long displacement is supported.
603 bool LongDisplacement;
604
605 // True if use of index register is supported.
606 bool IndexReg;
607
608 AddressingMode(bool LongDispl, bool IdxReg) :
609 LongDisplacement(LongDispl), IndexReg(IdxReg) {}
610};
611
612// Return the desired addressing mode for a Load which has only one use (in
613// the same block) which is a Store.
614static AddressingMode getLoadStoreAddrMode(bool HasVector,
615 Type *Ty) {
616 // With vector support a Load->Store combination may be combined to either
617 // an MVC or vector operations and it seems to work best to allow the
618 // vector addressing mode.
619 if (HasVector)
620 return AddressingMode(false/*LongDispl*/, true/*IdxReg*/);
621
622 // Otherwise only the MVC case is special.
623 bool MVC = Ty->isIntegerTy(8);
624 return AddressingMode(!MVC/*LongDispl*/, !MVC/*IdxReg*/);
625}
626
627// Return the addressing mode which seems most desirable given an LLVM
628// Instruction pointer.
629static AddressingMode
630supportedAddressingMode(Instruction *I, bool HasVector) {
631 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
632 switch (II->getIntrinsicID()) {
633 default: break;
634 case Intrinsic::memset:
635 case Intrinsic::memmove:
636 case Intrinsic::memcpy:
637 return AddressingMode(false/*LongDispl*/, false/*IdxReg*/);
638 }
639 }
640
641 if (isa<LoadInst>(I) && I->hasOneUse()) {
642 auto *SingleUser = dyn_cast<Instruction>(*I->user_begin());
643 if (SingleUser->getParent() == I->getParent()) {
644 if (isa<ICmpInst>(SingleUser)) {
645 if (auto *C = dyn_cast<ConstantInt>(SingleUser->getOperand(1)))
646 if (isInt<16>(C->getSExtValue()) || isUInt<16>(C->getZExtValue()))
647 // Comparison of memory with 16 bit signed / unsigned immediate
648 return AddressingMode(false/*LongDispl*/, false/*IdxReg*/);
649 } else if (isa<StoreInst>(SingleUser))
650 // Load->Store
651 return getLoadStoreAddrMode(HasVector, I->getType());
652 }
653 } else if (auto *StoreI = dyn_cast<StoreInst>(I)) {
654 if (auto *LoadI = dyn_cast<LoadInst>(StoreI->getValueOperand()))
655 if (LoadI->hasOneUse() && LoadI->getParent() == I->getParent())
656 // Load->Store
657 return getLoadStoreAddrMode(HasVector, LoadI->getType());
658 }
659
660 if (HasVector && (isa<LoadInst>(I) || isa<StoreInst>(I))) {
661
662 // * Use LDE instead of LE/LEY for z13 to avoid partial register
663 // dependencies (LDE only supports small offsets).
664 // * Utilize the vector registers to hold floating point
665 // values (vector load / store instructions only support small
666 // offsets).
667
668 Type *MemAccessTy = (isa<LoadInst>(I) ? I->getType() :
669 I->getOperand(0)->getType());
670 bool IsFPAccess = MemAccessTy->isFloatingPointTy();
671 bool IsVectorAccess = MemAccessTy->isVectorTy();
672
673 // A store of an extracted vector element will be combined into a VSTE type
674 // instruction.
675 if (!IsVectorAccess && isa<StoreInst>(I)) {
676 Value *DataOp = I->getOperand(0);
677 if (isa<ExtractElementInst>(DataOp))
678 IsVectorAccess = true;
679 }
680
681 // A load which gets inserted into a vector element will be combined into a
682 // VLE type instruction.
683 if (!IsVectorAccess && isa<LoadInst>(I) && I->hasOneUse()) {
684 User *LoadUser = *I->user_begin();
685 if (isa<InsertElementInst>(LoadUser))
686 IsVectorAccess = true;
687 }
688
689 if (IsFPAccess || IsVectorAccess)
690 return AddressingMode(false/*LongDispl*/, true/*IdxReg*/);
691 }
692
693 return AddressingMode(true/*LongDispl*/, true/*IdxReg*/);
694}
695
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000696bool SystemZTargetLowering::isLegalAddressingMode(const DataLayout &DL,
Jonas Paulsson6228aed2017-08-09 11:28:01 +0000697 const AddrMode &AM, Type *Ty, unsigned AS, Instruction *I) const {
Richard Sandiford791bea42013-07-31 12:58:26 +0000698 // Punt on globals for now, although they can be used in limited
699 // RELATIVE LONG cases.
700 if (AM.BaseGV)
701 return false;
702
703 // Require a 20-bit signed offset.
704 if (!isInt<20>(AM.BaseOffs))
705 return false;
706
Jonas Paulsson6228aed2017-08-09 11:28:01 +0000707 AddressingMode SupportedAM(true, true);
708 if (I != nullptr)
709 SupportedAM = supportedAddressingMode(I, Subtarget.hasVector());
710
711 if (!SupportedAM.LongDisplacement && !isUInt<12>(AM.BaseOffs))
712 return false;
713
714 if (!SupportedAM.IndexReg)
Jonas Paulsson024e3192017-07-21 11:59:37 +0000715 // No indexing allowed.
716 return AM.Scale == 0;
717 else
718 // Indexing is OK but no scale factor can be applied.
719 return AM.Scale == 0 || AM.Scale == 1;
Richard Sandiford791bea42013-07-31 12:58:26 +0000720}
721
Richard Sandiford709bda62013-08-19 12:42:31 +0000722bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
723 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
724 return false;
725 unsigned FromBits = FromType->getPrimitiveSizeInBits();
726 unsigned ToBits = ToType->getPrimitiveSizeInBits();
727 return FromBits > ToBits;
728}
729
730bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
731 if (!FromVT.isInteger() || !ToVT.isInteger())
732 return false;
733 unsigned FromBits = FromVT.getSizeInBits();
734 unsigned ToBits = ToVT.getSizeInBits();
735 return FromBits > ToBits;
736}
737
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000738//===----------------------------------------------------------------------===//
739// Inline asm support
740//===----------------------------------------------------------------------===//
741
742TargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000743SystemZTargetLowering::getConstraintType(StringRef Constraint) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000744 if (Constraint.size() == 1) {
745 switch (Constraint[0]) {
746 case 'a': // Address register
747 case 'd': // Data register (equivalent to 'r')
748 case 'f': // Floating-point register
Richard Sandiford0755c932013-10-01 11:26:28 +0000749 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000750 case 'r': // General-purpose register
751 return C_RegisterClass;
752
753 case 'Q': // Memory with base and unsigned 12-bit displacement
754 case 'R': // Likewise, plus an index
755 case 'S': // Memory with base and signed 20-bit displacement
756 case 'T': // Likewise, plus an index
757 case 'm': // Equivalent to 'T'.
758 return C_Memory;
759
760 case 'I': // Unsigned 8-bit constant
761 case 'J': // Unsigned 12-bit constant
762 case 'K': // Signed 16-bit constant
763 case 'L': // Signed 20-bit displacement (on all targets we support)
764 case 'M': // 0x7fffffff
765 return C_Other;
766
767 default:
768 break;
769 }
770 }
771 return TargetLowering::getConstraintType(Constraint);
772}
773
774TargetLowering::ConstraintWeight SystemZTargetLowering::
775getSingleConstraintMatchWeight(AsmOperandInfo &info,
776 const char *constraint) const {
777 ConstraintWeight weight = CW_Invalid;
778 Value *CallOperandVal = info.CallOperandVal;
779 // If we don't have a value, we can't do a match,
780 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +0000781 if (!CallOperandVal)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000782 return CW_Default;
783 Type *type = CallOperandVal->getType();
784 // Look at the constraint type.
785 switch (*constraint) {
786 default:
787 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
788 break;
789
790 case 'a': // Address register
791 case 'd': // Data register (equivalent to 'r')
Richard Sandiford0755c932013-10-01 11:26:28 +0000792 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000793 case 'r': // General-purpose register
794 if (CallOperandVal->getType()->isIntegerTy())
795 weight = CW_Register;
796 break;
797
798 case 'f': // Floating-point register
799 if (type->isFloatingPointTy())
800 weight = CW_Register;
801 break;
802
803 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000804 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000805 if (isUInt<8>(C->getZExtValue()))
806 weight = CW_Constant;
807 break;
808
809 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000810 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000811 if (isUInt<12>(C->getZExtValue()))
812 weight = CW_Constant;
813 break;
814
815 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000816 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000817 if (isInt<16>(C->getSExtValue()))
818 weight = CW_Constant;
819 break;
820
821 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000822 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000823 if (isInt<20>(C->getSExtValue()))
824 weight = CW_Constant;
825 break;
826
827 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000828 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000829 if (C->getZExtValue() == 0x7fffffff)
830 weight = CW_Constant;
831 break;
832 }
833 return weight;
834}
835
Richard Sandifordb8204052013-07-12 09:08:12 +0000836// Parse a "{tNNN}" register constraint for which the register type "t"
837// has already been verified. MC is the class associated with "t" and
838// Map maps 0-based register numbers to LLVM register numbers.
839static std::pair<unsigned, const TargetRegisterClass *>
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000840parseRegisterNumber(StringRef Constraint, const TargetRegisterClass *RC,
841 const unsigned *Map) {
Richard Sandifordb8204052013-07-12 09:08:12 +0000842 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
843 if (isdigit(Constraint[2])) {
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000844 unsigned Index;
845 bool Failed =
846 Constraint.slice(2, Constraint.size() - 1).getAsInteger(10, Index);
847 if (!Failed && Index < 16 && Map[Index])
Richard Sandifordb8204052013-07-12 09:08:12 +0000848 return std::make_pair(Map[Index], RC);
849 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000850 return std::make_pair(0U, nullptr);
Richard Sandifordb8204052013-07-12 09:08:12 +0000851}
852
Eric Christopher11e4df72015-02-26 22:38:43 +0000853std::pair<unsigned, const TargetRegisterClass *>
854SystemZTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000855 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000856 if (Constraint.size() == 1) {
857 // GCC Constraint Letters
858 switch (Constraint[0]) {
859 default: break;
860 case 'd': // Data register (equivalent to 'r')
861 case 'r': // General-purpose register
862 if (VT == MVT::i64)
863 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
864 else if (VT == MVT::i128)
865 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
866 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
867
868 case 'a': // Address register
869 if (VT == MVT::i64)
870 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
871 else if (VT == MVT::i128)
872 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
873 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
874
Richard Sandiford0755c932013-10-01 11:26:28 +0000875 case 'h': // High-part register (an LLVM extension)
876 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
877
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000878 case 'f': // Floating-point register
879 if (VT == MVT::f64)
880 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
881 else if (VT == MVT::f128)
882 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
883 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
884 }
885 }
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000886 if (Constraint.size() > 0 && Constraint[0] == '{') {
Richard Sandifordb8204052013-07-12 09:08:12 +0000887 // We need to override the default register parsing for GPRs and FPRs
888 // because the interpretation depends on VT. The internal names of
889 // the registers are also different from the external names
890 // (F0D and F0S instead of F0, etc.).
891 if (Constraint[1] == 'r') {
892 if (VT == MVT::i32)
893 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
894 SystemZMC::GR32Regs);
895 if (VT == MVT::i128)
896 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
897 SystemZMC::GR128Regs);
898 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
899 SystemZMC::GR64Regs);
900 }
901 if (Constraint[1] == 'f') {
902 if (VT == MVT::f32)
903 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
904 SystemZMC::FP32Regs);
905 if (VT == MVT::f128)
906 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
907 SystemZMC::FP128Regs);
908 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
909 SystemZMC::FP64Regs);
910 }
911 }
Eric Christopher11e4df72015-02-26 22:38:43 +0000912 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000913}
914
915void SystemZTargetLowering::
916LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
917 std::vector<SDValue> &Ops,
918 SelectionDAG &DAG) const {
919 // Only support length 1 constraints for now.
920 if (Constraint.length() == 1) {
921 switch (Constraint[0]) {
922 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000923 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000924 if (isUInt<8>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000925 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000926 Op.getValueType()));
927 return;
928
929 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000930 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000931 if (isUInt<12>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000932 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000933 Op.getValueType()));
934 return;
935
936 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000937 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000938 if (isInt<16>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000939 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000940 Op.getValueType()));
941 return;
942
943 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000944 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000945 if (isInt<20>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000946 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000947 Op.getValueType()));
948 return;
949
950 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000951 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000952 if (C->getZExtValue() == 0x7fffffff)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000953 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000954 Op.getValueType()));
955 return;
956 }
957 }
958 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
959}
960
961//===----------------------------------------------------------------------===//
962// Calling conventions
963//===----------------------------------------------------------------------===//
964
965#include "SystemZGenCallingConv.inc"
966
Richard Sandiford709bda62013-08-19 12:42:31 +0000967bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
968 Type *ToType) const {
969 return isTruncateFree(FromType, ToType);
970}
971
Matt Arsenault31380752017-04-18 21:16:46 +0000972bool SystemZTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
Ulrich Weigand19d24d22015-11-13 13:00:27 +0000973 return CI->isTailCall();
Richard Sandiford709bda62013-08-19 12:42:31 +0000974}
975
Ulrich Weigand5211f9f2015-05-05 19:30:05 +0000976// We do not yet support 128-bit single-element vector types. If the user
977// attempts to use such types as function argument or return type, prefer
978// to error out instead of emitting code violating the ABI.
979static void VerifyVectorType(MVT VT, EVT ArgVT) {
980 if (ArgVT.isVector() && !VT.isVector())
981 report_fatal_error("Unsupported vector argument or return type");
982}
983
984static void VerifyVectorTypes(const SmallVectorImpl<ISD::InputArg> &Ins) {
985 for (unsigned i = 0; i < Ins.size(); ++i)
986 VerifyVectorType(Ins[i].VT, Ins[i].ArgVT);
987}
988
989static void VerifyVectorTypes(const SmallVectorImpl<ISD::OutputArg> &Outs) {
990 for (unsigned i = 0; i < Outs.size(); ++i)
991 VerifyVectorType(Outs[i].VT, Outs[i].ArgVT);
992}
993
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000994// Value is a value that has been passed to us in the location described by VA
995// (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
996// any loads onto Chain.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000997static SDValue convertLocVTToValVT(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000998 CCValAssign &VA, SDValue Chain,
999 SDValue Value) {
1000 // If the argument has been promoted from a smaller type, insert an
1001 // assertion to capture this.
1002 if (VA.getLocInfo() == CCValAssign::SExt)
1003 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
1004 DAG.getValueType(VA.getValVT()));
1005 else if (VA.getLocInfo() == CCValAssign::ZExt)
1006 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
1007 DAG.getValueType(VA.getValVT()));
1008
1009 if (VA.isExtInLoc())
1010 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00001011 else if (VA.getLocInfo() == CCValAssign::BCvt) {
1012 // If this is a short vector argument loaded from the stack,
1013 // extend from i64 to full vector size and then bitcast.
1014 assert(VA.getLocVT() == MVT::i64);
1015 assert(VA.getValVT().isVector());
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001016 Value = DAG.getBuildVector(MVT::v2i64, DL, {Value, DAG.getUNDEF(MVT::i64)});
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00001017 Value = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Value);
1018 } else
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001019 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
1020 return Value;
1021}
1022
1023// Value is a value of type VA.getValVT() that we need to copy into
1024// the location described by VA. Return a copy of Value converted to
1025// VA.getValVT(). The caller is responsible for handling indirect values.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001026static SDValue convertValVTToLocVT(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001027 CCValAssign &VA, SDValue Value) {
1028 switch (VA.getLocInfo()) {
1029 case CCValAssign::SExt:
1030 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
1031 case CCValAssign::ZExt:
1032 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
1033 case CCValAssign::AExt:
1034 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00001035 case CCValAssign::BCvt:
1036 // If this is a short vector argument to be stored to the stack,
1037 // bitcast to v2i64 and then extract first element.
1038 assert(VA.getLocVT() == MVT::i64);
1039 assert(VA.getValVT().isVector());
1040 Value = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Value);
1041 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VA.getLocVT(), Value,
1042 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001043 case CCValAssign::Full:
1044 return Value;
1045 default:
1046 llvm_unreachable("Unhandled getLocInfo()");
1047 }
1048}
1049
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001050SDValue SystemZTargetLowering::LowerFormalArguments(
1051 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
1052 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1053 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001054 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001055 MachineFrameInfo &MFI = MF.getFrameInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001056 MachineRegisterInfo &MRI = MF.getRegInfo();
1057 SystemZMachineFunctionInfo *FuncInfo =
Eric Christophera6734172015-01-31 00:06:45 +00001058 MF.getInfo<SystemZMachineFunctionInfo>();
1059 auto *TFL =
1060 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering());
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001061 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001062
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001063 // Detect unsupported vector argument types.
1064 if (Subtarget.hasVector())
1065 VerifyVectorTypes(Ins);
1066
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001067 // Assign locations to all of the incoming arguments.
1068 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001069 SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001070 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
1071
1072 unsigned NumFixedGPRs = 0;
1073 unsigned NumFixedFPRs = 0;
1074 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1075 SDValue ArgValue;
1076 CCValAssign &VA = ArgLocs[I];
1077 EVT LocVT = VA.getLocVT();
1078 if (VA.isRegLoc()) {
1079 // Arguments passed in registers
1080 const TargetRegisterClass *RC;
1081 switch (LocVT.getSimpleVT().SimpleTy) {
1082 default:
1083 // Integers smaller than i64 should be promoted to i64.
1084 llvm_unreachable("Unexpected argument type");
1085 case MVT::i32:
1086 NumFixedGPRs += 1;
1087 RC = &SystemZ::GR32BitRegClass;
1088 break;
1089 case MVT::i64:
1090 NumFixedGPRs += 1;
1091 RC = &SystemZ::GR64BitRegClass;
1092 break;
1093 case MVT::f32:
1094 NumFixedFPRs += 1;
1095 RC = &SystemZ::FP32BitRegClass;
1096 break;
1097 case MVT::f64:
1098 NumFixedFPRs += 1;
1099 RC = &SystemZ::FP64BitRegClass;
1100 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001101 case MVT::v16i8:
1102 case MVT::v8i16:
1103 case MVT::v4i32:
1104 case MVT::v2i64:
Ulrich Weigand80b3af72015-05-05 19:27:45 +00001105 case MVT::v4f32:
Ulrich Weigandcd808232015-05-05 19:26:48 +00001106 case MVT::v2f64:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001107 RC = &SystemZ::VR128BitRegClass;
1108 break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001109 }
1110
1111 unsigned VReg = MRI.createVirtualRegister(RC);
1112 MRI.addLiveIn(VA.getLocReg(), VReg);
1113 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
1114 } else {
1115 assert(VA.isMemLoc() && "Argument not register or memory");
1116
1117 // Create the frame index object for this incoming parameter.
Matthias Braun941a7052016-07-28 18:40:00 +00001118 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
1119 VA.getLocMemOffset(), true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001120
1121 // Create the SelectionDAG nodes corresponding to a load
1122 // from this parameter. Unpromoted ints and floats are
1123 // passed as right-justified 8-byte values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001124 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
1125 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001126 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
1127 DAG.getIntPtrConstant(4, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001128 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00001129 MachinePointerInfo::getFixedStack(MF, FI));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001130 }
1131
1132 // Convert the value of the argument register into the value that's
1133 // being passed.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001134 if (VA.getLocInfo() == CCValAssign::Indirect) {
Justin Lebar9c375812016-07-15 18:27:10 +00001135 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
1136 MachinePointerInfo()));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001137 // If the original argument was split (e.g. i128), we need
1138 // to load all parts of it here (using the same address).
1139 unsigned ArgIndex = Ins[I].OrigArgIndex;
1140 assert (Ins[I].PartOffset == 0);
1141 while (I + 1 != E && Ins[I + 1].OrigArgIndex == ArgIndex) {
1142 CCValAssign &PartVA = ArgLocs[I + 1];
1143 unsigned PartOffset = Ins[I + 1].PartOffset;
1144 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
1145 DAG.getIntPtrConstant(PartOffset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00001146 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
1147 MachinePointerInfo()));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001148 ++I;
1149 }
1150 } else
1151 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001152 }
1153
1154 if (IsVarArg) {
1155 // Save the number of non-varargs registers for later use by va_start, etc.
1156 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
1157 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
1158
1159 // Likewise the address (in the form of a frame index) of where the
1160 // first stack vararg would be. The 1-byte size here is arbitrary.
1161 int64_t StackSize = CCInfo.getNextStackOffset();
Matthias Braun941a7052016-07-28 18:40:00 +00001162 FuncInfo->setVarArgsFrameIndex(MFI.CreateFixedObject(1, StackSize, true));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001163
1164 // ...and a similar frame index for the caller-allocated save area
1165 // that will be used to store the incoming registers.
1166 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
Matthias Braun941a7052016-07-28 18:40:00 +00001167 unsigned RegSaveIndex = MFI.CreateFixedObject(1, RegSaveOffset, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001168 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
1169
1170 // Store the FPR varargs in the reserved frame slots. (We store the
1171 // GPRs as part of the prologue.)
1172 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
1173 SDValue MemOps[SystemZ::NumArgFPRs];
1174 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
1175 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
Matthias Braun941a7052016-07-28 18:40:00 +00001176 int FI = MFI.CreateFixedObject(8, RegSaveOffset + Offset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +00001177 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001178 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
1179 &SystemZ::FP64BitRegClass);
1180 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
1181 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00001182 MachinePointerInfo::getFixedStack(MF, FI));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001183 }
1184 // Join the stores, which are independent of one another.
1185 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00001186 makeArrayRef(&MemOps[NumFixedFPRs],
1187 SystemZ::NumArgFPRs-NumFixedFPRs));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001188 }
1189 }
1190
1191 return Chain;
1192}
1193
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +00001194static bool canUseSiblingCall(const CCState &ArgCCInfo,
Bryan Chan893110e2016-04-28 00:17:23 +00001195 SmallVectorImpl<CCValAssign> &ArgLocs,
1196 SmallVectorImpl<ISD::OutputArg> &Outs) {
Richard Sandiford709bda62013-08-19 12:42:31 +00001197 // Punt if there are any indirect or stack arguments, or if the call
Bryan Chan893110e2016-04-28 00:17:23 +00001198 // needs the callee-saved argument register R6, or if the call uses
1199 // the callee-saved register arguments SwiftSelf and SwiftError.
Richard Sandiford709bda62013-08-19 12:42:31 +00001200 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1201 CCValAssign &VA = ArgLocs[I];
1202 if (VA.getLocInfo() == CCValAssign::Indirect)
1203 return false;
1204 if (!VA.isRegLoc())
1205 return false;
1206 unsigned Reg = VA.getLocReg();
Richard Sandiford0755c932013-10-01 11:26:28 +00001207 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
Richard Sandiford709bda62013-08-19 12:42:31 +00001208 return false;
Bryan Chan893110e2016-04-28 00:17:23 +00001209 if (Outs[I].Flags.isSwiftSelf() || Outs[I].Flags.isSwiftError())
1210 return false;
Richard Sandiford709bda62013-08-19 12:42:31 +00001211 }
1212 return true;
1213}
1214
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001215SDValue
1216SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
1217 SmallVectorImpl<SDValue> &InVals) const {
1218 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001219 SDLoc &DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001220 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1221 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1222 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001223 SDValue Chain = CLI.Chain;
1224 SDValue Callee = CLI.Callee;
Richard Sandiford709bda62013-08-19 12:42:31 +00001225 bool &IsTailCall = CLI.IsTailCall;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001226 CallingConv::ID CallConv = CLI.CallConv;
1227 bool IsVarArg = CLI.IsVarArg;
1228 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +00001229 EVT PtrVT = getPointerTy(MF.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001230
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001231 // Detect unsupported vector argument and return types.
1232 if (Subtarget.hasVector()) {
1233 VerifyVectorTypes(Outs);
1234 VerifyVectorTypes(Ins);
1235 }
1236
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001237 // Analyze the operands of the call, assigning locations to each operand.
1238 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001239 SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001240 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
1241
Richard Sandiford709bda62013-08-19 12:42:31 +00001242 // We don't support GuaranteedTailCallOpt, only automatically-detected
1243 // sibling calls.
Bryan Chan893110e2016-04-28 00:17:23 +00001244 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs, Outs))
Richard Sandiford709bda62013-08-19 12:42:31 +00001245 IsTailCall = false;
1246
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001247 // Get a count of how many bytes are to be pushed on the stack.
1248 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1249
1250 // Mark the start of the call.
Richard Sandiford709bda62013-08-19 12:42:31 +00001251 if (!IsTailCall)
Serge Pavlovd526b132017-05-09 13:35:13 +00001252 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001253
1254 // Copy argument values to their designated locations.
1255 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
1256 SmallVector<SDValue, 8> MemOpChains;
1257 SDValue StackPtr;
1258 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1259 CCValAssign &VA = ArgLocs[I];
1260 SDValue ArgValue = OutVals[I];
1261
1262 if (VA.getLocInfo() == CCValAssign::Indirect) {
1263 // Store the argument in a stack slot and pass its address.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001264 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[I].ArgVT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001265 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Justin Lebar9c375812016-07-15 18:27:10 +00001266 MemOpChains.push_back(
1267 DAG.getStore(Chain, DL, ArgValue, SpillSlot,
1268 MachinePointerInfo::getFixedStack(MF, FI)));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001269 // If the original argument was split (e.g. i128), we need
1270 // to store all parts of it here (and pass just one address).
1271 unsigned ArgIndex = Outs[I].OrigArgIndex;
1272 assert (Outs[I].PartOffset == 0);
1273 while (I + 1 != E && Outs[I + 1].OrigArgIndex == ArgIndex) {
1274 SDValue PartValue = OutVals[I + 1];
1275 unsigned PartOffset = Outs[I + 1].PartOffset;
1276 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
1277 DAG.getIntPtrConstant(PartOffset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00001278 MemOpChains.push_back(
1279 DAG.getStore(Chain, DL, PartValue, Address,
1280 MachinePointerInfo::getFixedStack(MF, FI)));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001281 ++I;
1282 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001283 ArgValue = SpillSlot;
1284 } else
1285 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
1286
1287 if (VA.isRegLoc())
1288 // Queue up the argument copies and emit them at the end.
1289 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1290 else {
1291 assert(VA.isMemLoc() && "Argument not register or memory");
1292
1293 // Work out the address of the stack slot. Unpromoted ints and
1294 // floats are passed as right-justified 8-byte values.
1295 if (!StackPtr.getNode())
1296 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
1297 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
1298 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
1299 Offset += 4;
1300 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001301 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001302
1303 // Emit the store.
Justin Lebar9c375812016-07-15 18:27:10 +00001304 MemOpChains.push_back(
1305 DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001306 }
1307 }
1308
1309 // Join the stores, which are independent of one another.
1310 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001311 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001312
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001313 // Accept direct calls by converting symbolic call addresses to the
Richard Sandiford709bda62013-08-19 12:42:31 +00001314 // associated Target* opcodes. Force %r1 to be used for indirect
1315 // tail calls.
1316 SDValue Glue;
Richard Sandiford21f5d682014-03-06 11:22:58 +00001317 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001318 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
1319 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford21f5d682014-03-06 11:22:58 +00001320 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001321 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
1322 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford709bda62013-08-19 12:42:31 +00001323 } else if (IsTailCall) {
1324 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
1325 Glue = Chain.getValue(1);
1326 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
1327 }
1328
1329 // Build a sequence of copy-to-reg nodes, chained and glued together.
1330 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
1331 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
1332 RegsToPass[I].second, Glue);
1333 Glue = Chain.getValue(1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001334 }
1335
1336 // The first call operand is the chain and the second is the target address.
1337 SmallVector<SDValue, 8> Ops;
1338 Ops.push_back(Chain);
1339 Ops.push_back(Callee);
1340
1341 // Add argument registers to the end of the list so that they are
1342 // known live into the call.
1343 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
1344 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
1345 RegsToPass[I].second.getValueType()));
1346
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001347 // Add a register mask operand representing the call-preserved registers.
Eric Christophera6734172015-01-31 00:06:45 +00001348 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00001349 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001350 assert(Mask && "Missing call preserved mask for calling convention");
1351 Ops.push_back(DAG.getRegisterMask(Mask));
1352
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001353 // Glue the call to the argument copies, if any.
1354 if (Glue.getNode())
1355 Ops.push_back(Glue);
1356
1357 // Emit the call.
1358 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Richard Sandiford709bda62013-08-19 12:42:31 +00001359 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001360 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
1361 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001362 Glue = Chain.getValue(1);
1363
1364 // Mark the end of the call, which is glued to the call itself.
1365 Chain = DAG.getCALLSEQ_END(Chain,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001366 DAG.getConstant(NumBytes, DL, PtrVT, true),
1367 DAG.getConstant(0, DL, PtrVT, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001368 Glue, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001369 Glue = Chain.getValue(1);
1370
1371 // Assign locations to each value returned by this call.
1372 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001373 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001374 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
1375
1376 // Copy all of the result registers out of their specified physreg.
1377 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1378 CCValAssign &VA = RetLocs[I];
1379
1380 // Copy the value out, gluing the copy to the end of the call sequence.
1381 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
1382 VA.getLocVT(), Glue);
1383 Chain = RetValue.getValue(1);
1384 Glue = RetValue.getValue(2);
1385
1386 // Convert the value of the return register into the value that's
1387 // being returned.
1388 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
1389 }
1390
1391 return Chain;
1392}
1393
Ulrich Weiganda887f062015-08-13 13:37:06 +00001394bool SystemZTargetLowering::
1395CanLowerReturn(CallingConv::ID CallConv,
1396 MachineFunction &MF, bool isVarArg,
1397 const SmallVectorImpl<ISD::OutputArg> &Outs,
1398 LLVMContext &Context) const {
1399 // Detect unsupported vector return types.
1400 if (Subtarget.hasVector())
1401 VerifyVectorTypes(Outs);
1402
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001403 // Special case that we cannot easily detect in RetCC_SystemZ since
1404 // i128 is not a legal type.
1405 for (auto &Out : Outs)
1406 if (Out.ArgVT == MVT::i128)
1407 return false;
1408
Ulrich Weiganda887f062015-08-13 13:37:06 +00001409 SmallVector<CCValAssign, 16> RetLocs;
1410 CCState RetCCInfo(CallConv, isVarArg, MF, RetLocs, Context);
1411 return RetCCInfo.CheckReturn(Outs, RetCC_SystemZ);
1412}
1413
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001414SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001415SystemZTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1416 bool IsVarArg,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001417 const SmallVectorImpl<ISD::OutputArg> &Outs,
1418 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001419 const SDLoc &DL, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001420 MachineFunction &MF = DAG.getMachineFunction();
1421
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001422 // Detect unsupported vector return types.
1423 if (Subtarget.hasVector())
1424 VerifyVectorTypes(Outs);
1425
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001426 // Assign locations to each returned value.
1427 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001428 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001429 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
1430
1431 // Quick exit for void returns
1432 if (RetLocs.empty())
1433 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
1434
1435 // Copy the result values into the output registers.
1436 SDValue Glue;
1437 SmallVector<SDValue, 4> RetOps;
1438 RetOps.push_back(Chain);
1439 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1440 CCValAssign &VA = RetLocs[I];
1441 SDValue RetValue = OutVals[I];
1442
1443 // Make the return register live on exit.
1444 assert(VA.isRegLoc() && "Can only return in registers!");
1445
1446 // Promote the value as required.
1447 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
1448
1449 // Chain and glue the copies together.
1450 unsigned Reg = VA.getLocReg();
1451 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
1452 Glue = Chain.getValue(1);
1453 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
1454 }
1455
1456 // Update chain and glue.
1457 RetOps[0] = Chain;
1458 if (Glue.getNode())
1459 RetOps.push_back(Glue);
1460
Craig Topper48d114b2014-04-26 18:35:24 +00001461 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001462}
1463
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001464// Return true if Op is an intrinsic node with chain that returns the CC value
1465// as its only (other) argument. Provide the associated SystemZISD opcode and
1466// the mask of valid CC values if so.
1467static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode,
1468 unsigned &CCValid) {
1469 unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1470 switch (Id) {
1471 case Intrinsic::s390_tbegin:
1472 Opcode = SystemZISD::TBEGIN;
1473 CCValid = SystemZ::CCMASK_TBEGIN;
1474 return true;
1475
1476 case Intrinsic::s390_tbegin_nofloat:
1477 Opcode = SystemZISD::TBEGIN_NOFLOAT;
1478 CCValid = SystemZ::CCMASK_TBEGIN;
1479 return true;
1480
1481 case Intrinsic::s390_tend:
1482 Opcode = SystemZISD::TEND;
1483 CCValid = SystemZ::CCMASK_TEND;
1484 return true;
1485
1486 default:
1487 return false;
1488 }
1489}
1490
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001491// Return true if Op is an intrinsic node without chain that returns the
1492// CC value as its final argument. Provide the associated SystemZISD
1493// opcode and the mask of valid CC values if so.
1494static bool isIntrinsicWithCC(SDValue Op, unsigned &Opcode, unsigned &CCValid) {
1495 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1496 switch (Id) {
1497 case Intrinsic::s390_vpkshs:
1498 case Intrinsic::s390_vpksfs:
1499 case Intrinsic::s390_vpksgs:
1500 Opcode = SystemZISD::PACKS_CC;
1501 CCValid = SystemZ::CCMASK_VCMP;
1502 return true;
1503
1504 case Intrinsic::s390_vpklshs:
1505 case Intrinsic::s390_vpklsfs:
1506 case Intrinsic::s390_vpklsgs:
1507 Opcode = SystemZISD::PACKLS_CC;
1508 CCValid = SystemZ::CCMASK_VCMP;
1509 return true;
1510
1511 case Intrinsic::s390_vceqbs:
1512 case Intrinsic::s390_vceqhs:
1513 case Intrinsic::s390_vceqfs:
1514 case Intrinsic::s390_vceqgs:
1515 Opcode = SystemZISD::VICMPES;
1516 CCValid = SystemZ::CCMASK_VCMP;
1517 return true;
1518
1519 case Intrinsic::s390_vchbs:
1520 case Intrinsic::s390_vchhs:
1521 case Intrinsic::s390_vchfs:
1522 case Intrinsic::s390_vchgs:
1523 Opcode = SystemZISD::VICMPHS;
1524 CCValid = SystemZ::CCMASK_VCMP;
1525 return true;
1526
1527 case Intrinsic::s390_vchlbs:
1528 case Intrinsic::s390_vchlhs:
1529 case Intrinsic::s390_vchlfs:
1530 case Intrinsic::s390_vchlgs:
1531 Opcode = SystemZISD::VICMPHLS;
1532 CCValid = SystemZ::CCMASK_VCMP;
1533 return true;
1534
1535 case Intrinsic::s390_vtm:
1536 Opcode = SystemZISD::VTM;
1537 CCValid = SystemZ::CCMASK_VCMP;
1538 return true;
1539
1540 case Intrinsic::s390_vfaebs:
1541 case Intrinsic::s390_vfaehs:
1542 case Intrinsic::s390_vfaefs:
1543 Opcode = SystemZISD::VFAE_CC;
1544 CCValid = SystemZ::CCMASK_ANY;
1545 return true;
1546
1547 case Intrinsic::s390_vfaezbs:
1548 case Intrinsic::s390_vfaezhs:
1549 case Intrinsic::s390_vfaezfs:
1550 Opcode = SystemZISD::VFAEZ_CC;
1551 CCValid = SystemZ::CCMASK_ANY;
1552 return true;
1553
1554 case Intrinsic::s390_vfeebs:
1555 case Intrinsic::s390_vfeehs:
1556 case Intrinsic::s390_vfeefs:
1557 Opcode = SystemZISD::VFEE_CC;
1558 CCValid = SystemZ::CCMASK_ANY;
1559 return true;
1560
1561 case Intrinsic::s390_vfeezbs:
1562 case Intrinsic::s390_vfeezhs:
1563 case Intrinsic::s390_vfeezfs:
1564 Opcode = SystemZISD::VFEEZ_CC;
1565 CCValid = SystemZ::CCMASK_ANY;
1566 return true;
1567
1568 case Intrinsic::s390_vfenebs:
1569 case Intrinsic::s390_vfenehs:
1570 case Intrinsic::s390_vfenefs:
1571 Opcode = SystemZISD::VFENE_CC;
1572 CCValid = SystemZ::CCMASK_ANY;
1573 return true;
1574
1575 case Intrinsic::s390_vfenezbs:
1576 case Intrinsic::s390_vfenezhs:
1577 case Intrinsic::s390_vfenezfs:
1578 Opcode = SystemZISD::VFENEZ_CC;
1579 CCValid = SystemZ::CCMASK_ANY;
1580 return true;
1581
1582 case Intrinsic::s390_vistrbs:
1583 case Intrinsic::s390_vistrhs:
1584 case Intrinsic::s390_vistrfs:
1585 Opcode = SystemZISD::VISTR_CC;
1586 CCValid = SystemZ::CCMASK_0 | SystemZ::CCMASK_3;
1587 return true;
1588
1589 case Intrinsic::s390_vstrcbs:
1590 case Intrinsic::s390_vstrchs:
1591 case Intrinsic::s390_vstrcfs:
1592 Opcode = SystemZISD::VSTRC_CC;
1593 CCValid = SystemZ::CCMASK_ANY;
1594 return true;
1595
1596 case Intrinsic::s390_vstrczbs:
1597 case Intrinsic::s390_vstrczhs:
1598 case Intrinsic::s390_vstrczfs:
1599 Opcode = SystemZISD::VSTRCZ_CC;
1600 CCValid = SystemZ::CCMASK_ANY;
1601 return true;
1602
1603 case Intrinsic::s390_vfcedbs:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001604 case Intrinsic::s390_vfcesbs:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001605 Opcode = SystemZISD::VFCMPES;
1606 CCValid = SystemZ::CCMASK_VCMP;
1607 return true;
1608
1609 case Intrinsic::s390_vfchdbs:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001610 case Intrinsic::s390_vfchsbs:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001611 Opcode = SystemZISD::VFCMPHS;
1612 CCValid = SystemZ::CCMASK_VCMP;
1613 return true;
1614
1615 case Intrinsic::s390_vfchedbs:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001616 case Intrinsic::s390_vfchesbs:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001617 Opcode = SystemZISD::VFCMPHES;
1618 CCValid = SystemZ::CCMASK_VCMP;
1619 return true;
1620
1621 case Intrinsic::s390_vftcidb:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001622 case Intrinsic::s390_vftcisb:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001623 Opcode = SystemZISD::VFTCI;
1624 CCValid = SystemZ::CCMASK_VCMP;
1625 return true;
1626
Marcin Koscielnickicf7cc722016-07-10 14:41:22 +00001627 case Intrinsic::s390_tdc:
1628 Opcode = SystemZISD::TDC;
1629 CCValid = SystemZ::CCMASK_TDC;
1630 return true;
1631
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001632 default:
1633 return false;
1634 }
1635}
1636
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001637// Emit an intrinsic with chain with a glued value instead of its CC result.
1638static SDValue emitIntrinsicWithChainAndGlue(SelectionDAG &DAG, SDValue Op,
1639 unsigned Opcode) {
1640 // Copy all operands except the intrinsic ID.
1641 unsigned NumOps = Op.getNumOperands();
1642 SmallVector<SDValue, 6> Ops;
1643 Ops.reserve(NumOps - 1);
1644 Ops.push_back(Op.getOperand(0));
1645 for (unsigned I = 2; I < NumOps; ++I)
1646 Ops.push_back(Op.getOperand(I));
1647
1648 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
1649 SDVTList RawVTs = DAG.getVTList(MVT::Other, MVT::Glue);
1650 SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1651 SDValue OldChain = SDValue(Op.getNode(), 1);
1652 SDValue NewChain = SDValue(Intr.getNode(), 0);
1653 DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain);
1654 return Intr;
1655}
1656
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001657// Emit an intrinsic with a glued value instead of its CC result.
1658static SDValue emitIntrinsicWithGlue(SelectionDAG &DAG, SDValue Op,
1659 unsigned Opcode) {
1660 // Copy all operands except the intrinsic ID.
1661 unsigned NumOps = Op.getNumOperands();
1662 SmallVector<SDValue, 6> Ops;
1663 Ops.reserve(NumOps - 1);
1664 for (unsigned I = 1; I < NumOps; ++I)
1665 Ops.push_back(Op.getOperand(I));
1666
1667 if (Op->getNumValues() == 1)
1668 return DAG.getNode(Opcode, SDLoc(Op), MVT::Glue, Ops);
1669 assert(Op->getNumValues() == 2 && "Expected exactly one non-CC result");
1670 SDVTList RawVTs = DAG.getVTList(Op->getValueType(0), MVT::Glue);
1671 return DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1672}
1673
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001674// CC is a comparison that will be implemented using an integer or
1675// floating-point comparison. Return the condition code mask for
1676// a branch on true. In the integer case, CCMASK_CMP_UO is set for
1677// unsigned comparisons and clear for signed ones. In the floating-point
1678// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1679static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1680#define CONV(X) \
1681 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1682 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1683 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1684
1685 switch (CC) {
1686 default:
1687 llvm_unreachable("Invalid integer condition!");
1688
1689 CONV(EQ);
1690 CONV(NE);
1691 CONV(GT);
1692 CONV(GE);
1693 CONV(LT);
1694 CONV(LE);
1695
1696 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1697 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1698 }
1699#undef CONV
1700}
1701
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001702// Return a sequence for getting a 1 from an IPM result when CC has a
1703// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1704// The handling of CC values outside CCValid doesn't matter.
1705static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1706 // Deal with cases where the result can be taken directly from a bit
1707 // of the IPM result.
1708 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1709 return IPMConversion(0, 0, SystemZ::IPM_CC);
1710 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1711 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1712
1713 // Deal with cases where we can add a value to force the sign bit
1714 // to contain the right value. Putting the bit in 31 means we can
1715 // use SRL rather than RISBG(L), and also makes it easier to get a
1716 // 0/-1 value, so it has priority over the other tests below.
1717 //
1718 // These sequences rely on the fact that the upper two bits of the
1719 // IPM result are zero.
1720 uint64_t TopBit = uint64_t(1) << 31;
1721 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1722 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1723 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1724 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1725 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1726 | SystemZ::CCMASK_1
1727 | SystemZ::CCMASK_2)))
1728 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1729 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1730 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1731 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1732 | SystemZ::CCMASK_2
1733 | SystemZ::CCMASK_3)))
1734 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1735
1736 // Next try inverting the value and testing a bit. 0/1 could be
1737 // handled this way too, but we dealt with that case above.
1738 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1739 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1740
1741 // Handle cases where adding a value forces a non-sign bit to contain
1742 // the right value.
1743 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1744 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1745 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1746 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1747
Alp Tokercb402912014-01-24 17:20:08 +00001748 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001749 // can be done by inverting the low CC bit and applying one of the
1750 // sign-based extractions above.
1751 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1752 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1753 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1754 return IPMConversion(1 << SystemZ::IPM_CC,
1755 TopBit - (3 << SystemZ::IPM_CC), 31);
1756 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1757 | SystemZ::CCMASK_1
1758 | SystemZ::CCMASK_3)))
1759 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1760 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1761 | SystemZ::CCMASK_2
1762 | SystemZ::CCMASK_3)))
1763 return IPMConversion(1 << SystemZ::IPM_CC,
1764 TopBit - (1 << SystemZ::IPM_CC), 31);
1765
1766 llvm_unreachable("Unexpected CC combination");
1767}
1768
Richard Sandifordd420f732013-12-13 15:28:45 +00001769// If C can be converted to a comparison against zero, adjust the operands
Richard Sandiforda0757082013-08-01 10:29:45 +00001770// as necessary.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001771static void adjustZeroCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001772 if (C.ICmpType == SystemZICMP::UnsignedOnly)
Richard Sandiforda0757082013-08-01 10:29:45 +00001773 return;
1774
Richard Sandiford21f5d682014-03-06 11:22:58 +00001775 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
Richard Sandiforda0757082013-08-01 10:29:45 +00001776 if (!ConstOp1)
1777 return;
1778
1779 int64_t Value = ConstOp1->getSExtValue();
Richard Sandifordd420f732013-12-13 15:28:45 +00001780 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1781 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1782 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1783 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1784 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001785 C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType());
Richard Sandiforda0757082013-08-01 10:29:45 +00001786 }
1787}
1788
Richard Sandifordd420f732013-12-13 15:28:45 +00001789// If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1790// adjust the operands as necessary.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001791static void adjustSubwordCmp(SelectionDAG &DAG, const SDLoc &DL,
1792 Comparison &C) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001793 // For us to make any changes, it must a comparison between a single-use
1794 // load and a constant.
Richard Sandifordd420f732013-12-13 15:28:45 +00001795 if (!C.Op0.hasOneUse() ||
1796 C.Op0.getOpcode() != ISD::LOAD ||
1797 C.Op1.getOpcode() != ISD::Constant)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001798 return;
1799
1800 // We must have an 8- or 16-bit load.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001801 auto *Load = cast<LoadSDNode>(C.Op0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001802 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1803 if (NumBits != 8 && NumBits != 16)
1804 return;
1805
1806 // The load must be an extending one and the constant must be within the
1807 // range of the unextended value.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001808 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001809 uint64_t Value = ConstOp1->getZExtValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001810 uint64_t Mask = (1 << NumBits) - 1;
1811 if (Load->getExtensionType() == ISD::SEXTLOAD) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001812 // Make sure that ConstOp1 is in range of C.Op0.
1813 int64_t SignedValue = ConstOp1->getSExtValue();
1814 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001815 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001816 if (C.ICmpType != SystemZICMP::SignedOnly) {
1817 // Unsigned comparison between two sign-extended values is equivalent
1818 // to unsigned comparison between two zero-extended values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001819 Value &= Mask;
Richard Sandifordd420f732013-12-13 15:28:45 +00001820 } else if (NumBits == 8) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001821 // Try to treat the comparison as unsigned, so that we can use CLI.
1822 // Adjust CCMask and Value as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001823 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001824 // Test whether the high bit of the byte is set.
Richard Sandifordd420f732013-12-13 15:28:45 +00001825 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1826 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001827 // Test whether the high bit of the byte is clear.
Richard Sandifordd420f732013-12-13 15:28:45 +00001828 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001829 else
1830 // No instruction exists for this combination.
1831 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001832 C.ICmpType = SystemZICMP::UnsignedOnly;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001833 }
1834 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1835 if (Value > Mask)
1836 return;
Ulrich Weigand47f36492015-12-16 18:04:06 +00001837 // If the constant is in range, we can use any comparison.
1838 C.ICmpType = SystemZICMP::Any;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001839 } else
1840 return;
1841
1842 // Make sure that the first operand is an i32 of the right extension type.
Richard Sandifordd420f732013-12-13 15:28:45 +00001843 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1844 ISD::SEXTLOAD :
1845 ISD::ZEXTLOAD);
1846 if (C.Op0.getValueType() != MVT::i32 ||
Jonas Paulssonb9a24672017-11-30 08:18:50 +00001847 Load->getExtensionType() != ExtType) {
Justin Lebar9c375812016-07-15 18:27:10 +00001848 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32, Load->getChain(),
1849 Load->getBasePtr(), Load->getPointerInfo(),
1850 Load->getMemoryVT(), Load->getAlignment(),
1851 Load->getMemOperand()->getFlags());
Jonas Paulssonb9a24672017-11-30 08:18:50 +00001852 // Update the chain uses.
1853 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), C.Op0.getValue(1));
1854 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001855
1856 // Make sure that the second operand is an i32 with the right value.
Richard Sandifordd420f732013-12-13 15:28:45 +00001857 if (C.Op1.getValueType() != MVT::i32 ||
1858 Value != ConstOp1->getZExtValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001859 C.Op1 = DAG.getConstant(Value, DL, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001860}
1861
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001862// Return true if Op is either an unextended load, or a load suitable
1863// for integer register-memory comparisons of type ICmpType.
1864static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001865 auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001866 if (Load) {
1867 // There are no instructions to compare a register with a memory byte.
1868 if (Load->getMemoryVT() == MVT::i8)
1869 return false;
1870 // Otherwise decide on extension type.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001871 switch (Load->getExtensionType()) {
1872 case ISD::NON_EXTLOAD:
Richard Sandiford24e597b2013-08-23 11:27:19 +00001873 return true;
1874 case ISD::SEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001875 return ICmpType != SystemZICMP::UnsignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001876 case ISD::ZEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001877 return ICmpType != SystemZICMP::SignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001878 default:
1879 break;
1880 }
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001881 }
Richard Sandiford24e597b2013-08-23 11:27:19 +00001882 return false;
1883}
1884
Richard Sandifordd420f732013-12-13 15:28:45 +00001885// Return true if it is better to swap the operands of C.
1886static bool shouldSwapCmpOperands(const Comparison &C) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001887 // Leave f128 comparisons alone, since they have no memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001888 if (C.Op0.getValueType() == MVT::f128)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001889 return false;
1890
1891 // Always keep a floating-point constant second, since comparisons with
1892 // zero can use LOAD TEST and comparisons with other constants make a
1893 // natural memory operand.
Richard Sandifordd420f732013-12-13 15:28:45 +00001894 if (isa<ConstantFPSDNode>(C.Op1))
Richard Sandiford24e597b2013-08-23 11:27:19 +00001895 return false;
1896
1897 // Never swap comparisons with zero since there are many ways to optimize
1898 // those later.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001899 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001900 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001901 return false;
1902
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001903 // Also keep natural memory operands second if the loaded value is
1904 // only used here. Several comparisons have memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001905 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001906 return false;
1907
Richard Sandiford24e597b2013-08-23 11:27:19 +00001908 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1909 // In that case we generally prefer the memory to be second.
Richard Sandifordd420f732013-12-13 15:28:45 +00001910 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001911 // The only exceptions are when the second operand is a constant and
1912 // we can use things like CHHSI.
Richard Sandifordd420f732013-12-13 15:28:45 +00001913 if (!ConstOp1)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001914 return true;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001915 // The unsigned memory-immediate instructions can handle 16-bit
1916 // unsigned integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001917 if (C.ICmpType != SystemZICMP::SignedOnly &&
1918 isUInt<16>(ConstOp1->getZExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001919 return false;
1920 // The signed memory-immediate instructions can handle 16-bit
1921 // signed integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001922 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1923 isInt<16>(ConstOp1->getSExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001924 return false;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001925 return true;
1926 }
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001927
1928 // Try to promote the use of CGFR and CLGFR.
Richard Sandifordd420f732013-12-13 15:28:45 +00001929 unsigned Opcode0 = C.Op0.getOpcode();
1930 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001931 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001932 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001933 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001934 if (C.ICmpType != SystemZICMP::SignedOnly &&
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001935 Opcode0 == ISD::AND &&
Richard Sandifordd420f732013-12-13 15:28:45 +00001936 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1937 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001938 return true;
1939
Richard Sandiford24e597b2013-08-23 11:27:19 +00001940 return false;
1941}
1942
Richard Sandiford73170f82013-12-11 11:45:08 +00001943// Return a version of comparison CC mask CCMask in which the LT and GT
1944// actions are swapped.
1945static unsigned reverseCCMask(unsigned CCMask) {
1946 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1947 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1948 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1949 (CCMask & SystemZ::CCMASK_CMP_UO));
1950}
1951
Richard Sandiford0847c452013-12-13 15:50:30 +00001952// Check whether C tests for equality between X and Y and whether X - Y
1953// or Y - X is also computed. In that case it's better to compare the
1954// result of the subtraction against zero.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001955static void adjustForSubtraction(SelectionDAG &DAG, const SDLoc &DL,
1956 Comparison &C) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001957 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1958 C.CCMask == SystemZ::CCMASK_CMP_NE) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001959 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001960 SDNode *N = *I;
1961 if (N->getOpcode() == ISD::SUB &&
1962 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1963 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1964 C.Op0 = SDValue(N, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001965 C.Op1 = DAG.getConstant(0, DL, N->getValueType(0));
Richard Sandiford0847c452013-12-13 15:50:30 +00001966 return;
1967 }
1968 }
1969 }
1970}
1971
Richard Sandifordd420f732013-12-13 15:28:45 +00001972// Check whether C compares a floating-point value with zero and if that
1973// floating-point value is also negated. In this case we can use the
1974// negation to set CC, so avoiding separate LOAD AND TEST and
1975// LOAD (NEGATIVE/COMPLEMENT) instructions.
1976static void adjustForFNeg(Comparison &C) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001977 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
Richard Sandiford73170f82013-12-11 11:45:08 +00001978 if (C1 && C1->isZero()) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001979 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford73170f82013-12-11 11:45:08 +00001980 SDNode *N = *I;
1981 if (N->getOpcode() == ISD::FNEG) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001982 C.Op0 = SDValue(N, 0);
1983 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford73170f82013-12-11 11:45:08 +00001984 return;
1985 }
1986 }
1987 }
1988}
1989
Richard Sandifordd420f732013-12-13 15:28:45 +00001990// Check whether C compares (shl X, 32) with 0 and whether X is
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001991// also sign-extended. In that case it is better to test the result
1992// of the sign extension using LTGFR.
1993//
1994// This case is important because InstCombine transforms a comparison
1995// with (sext (trunc X)) into a comparison with (shl X, 32).
Richard Sandifordd420f732013-12-13 15:28:45 +00001996static void adjustForLTGFR(Comparison &C) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001997 // Check for a comparison between (shl X, 32) and 0.
Richard Sandifordd420f732013-12-13 15:28:45 +00001998 if (C.Op0.getOpcode() == ISD::SHL &&
1999 C.Op0.getValueType() == MVT::i64 &&
2000 C.Op1.getOpcode() == ISD::Constant &&
2001 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002002 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00002003 if (C1 && C1->getZExtValue() == 32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00002004 SDValue ShlOp0 = C.Op0.getOperand(0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00002005 // See whether X has any SIGN_EXTEND_INREG uses.
Richard Sandiford28c111e2014-03-06 11:00:15 +00002006 for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00002007 SDNode *N = *I;
2008 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
2009 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00002010 C.Op0 = SDValue(N, 0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00002011 return;
2012 }
2013 }
2014 }
2015 }
2016}
2017
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002018// If C compares the truncation of an extending load, try to compare
2019// the untruncated value instead. This exposes more opportunities to
2020// reuse CC.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002021static void adjustICmpTruncate(SelectionDAG &DAG, const SDLoc &DL,
2022 Comparison &C) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002023 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
2024 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
2025 C.Op1.getOpcode() == ISD::Constant &&
2026 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002027 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002028 if (L->getMemoryVT().getStoreSizeInBits() <= C.Op0.getValueSizeInBits()) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002029 unsigned Type = L->getExtensionType();
2030 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
2031 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
2032 C.Op0 = C.Op0.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002033 C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType());
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002034 }
2035 }
2036 }
2037}
2038
Richard Sandiford030c1652013-09-13 09:09:50 +00002039// Return true if shift operation N has an in-range constant shift value.
2040// Store it in ShiftVal if so.
2041static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002042 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
Richard Sandiford030c1652013-09-13 09:09:50 +00002043 if (!Shift)
2044 return false;
2045
2046 uint64_t Amount = Shift->getZExtValue();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002047 if (Amount >= N.getValueSizeInBits())
Richard Sandiford030c1652013-09-13 09:09:50 +00002048 return false;
2049
2050 ShiftVal = Amount;
2051 return true;
2052}
2053
2054// Check whether an AND with Mask is suitable for a TEST UNDER MASK
2055// instruction and whether the CC value is descriptive enough to handle
2056// a comparison of type Opcode between the AND result and CmpVal.
2057// CCMask says which comparison result is being tested and BitSize is
2058// the number of bits in the operands. If TEST UNDER MASK can be used,
2059// return the corresponding CC mask, otherwise return 0.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002060static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
2061 uint64_t Mask, uint64_t CmpVal,
2062 unsigned ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00002063 assert(Mask != 0 && "ANDs with zero should have been removed by now");
2064
Richard Sandiford030c1652013-09-13 09:09:50 +00002065 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
2066 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
2067 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
2068 return 0;
2069
Richard Sandiford113c8702013-09-03 15:38:35 +00002070 // Work out the masks for the lowest and highest bits.
2071 unsigned HighShift = 63 - countLeadingZeros(Mask);
2072 uint64_t High = uint64_t(1) << HighShift;
2073 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
2074
2075 // Signed ordered comparisons are effectively unsigned if the sign
2076 // bit is dropped.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002077 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
Richard Sandiford113c8702013-09-03 15:38:35 +00002078
2079 // Check for equality comparisons with 0, or the equivalent.
2080 if (CmpVal == 0) {
2081 if (CCMask == SystemZ::CCMASK_CMP_EQ)
2082 return SystemZ::CCMASK_TM_ALL_0;
2083 if (CCMask == SystemZ::CCMASK_CMP_NE)
2084 return SystemZ::CCMASK_TM_SOME_1;
2085 }
Ulrich Weigand4a4d4ab2016-02-01 18:31:19 +00002086 if (EffectivelyUnsigned && CmpVal > 0 && CmpVal <= Low) {
Richard Sandiford113c8702013-09-03 15:38:35 +00002087 if (CCMask == SystemZ::CCMASK_CMP_LT)
2088 return SystemZ::CCMASK_TM_ALL_0;
2089 if (CCMask == SystemZ::CCMASK_CMP_GE)
2090 return SystemZ::CCMASK_TM_SOME_1;
2091 }
2092 if (EffectivelyUnsigned && CmpVal < Low) {
2093 if (CCMask == SystemZ::CCMASK_CMP_LE)
2094 return SystemZ::CCMASK_TM_ALL_0;
2095 if (CCMask == SystemZ::CCMASK_CMP_GT)
2096 return SystemZ::CCMASK_TM_SOME_1;
2097 }
2098
2099 // Check for equality comparisons with the mask, or the equivalent.
2100 if (CmpVal == Mask) {
2101 if (CCMask == SystemZ::CCMASK_CMP_EQ)
2102 return SystemZ::CCMASK_TM_ALL_1;
2103 if (CCMask == SystemZ::CCMASK_CMP_NE)
2104 return SystemZ::CCMASK_TM_SOME_0;
2105 }
2106 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
2107 if (CCMask == SystemZ::CCMASK_CMP_GT)
2108 return SystemZ::CCMASK_TM_ALL_1;
2109 if (CCMask == SystemZ::CCMASK_CMP_LE)
2110 return SystemZ::CCMASK_TM_SOME_0;
2111 }
2112 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
2113 if (CCMask == SystemZ::CCMASK_CMP_GE)
2114 return SystemZ::CCMASK_TM_ALL_1;
2115 if (CCMask == SystemZ::CCMASK_CMP_LT)
2116 return SystemZ::CCMASK_TM_SOME_0;
2117 }
2118
2119 // Check for ordered comparisons with the top bit.
2120 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
2121 if (CCMask == SystemZ::CCMASK_CMP_LE)
2122 return SystemZ::CCMASK_TM_MSB_0;
2123 if (CCMask == SystemZ::CCMASK_CMP_GT)
2124 return SystemZ::CCMASK_TM_MSB_1;
2125 }
2126 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
2127 if (CCMask == SystemZ::CCMASK_CMP_LT)
2128 return SystemZ::CCMASK_TM_MSB_0;
2129 if (CCMask == SystemZ::CCMASK_CMP_GE)
2130 return SystemZ::CCMASK_TM_MSB_1;
2131 }
2132
2133 // If there are just two bits, we can do equality checks for Low and High
2134 // as well.
2135 if (Mask == Low + High) {
2136 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
2137 return SystemZ::CCMASK_TM_MIXED_MSB_0;
2138 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
2139 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
2140 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
2141 return SystemZ::CCMASK_TM_MIXED_MSB_1;
2142 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
2143 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
2144 }
2145
2146 // Looks like we've exhausted our options.
2147 return 0;
2148}
2149
Richard Sandifordd420f732013-12-13 15:28:45 +00002150// See whether C can be implemented as a TEST UNDER MASK instruction.
2151// Update the arguments with the TM version if so.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002152static void adjustForTestUnderMask(SelectionDAG &DAG, const SDLoc &DL,
2153 Comparison &C) {
Richard Sandiford113c8702013-09-03 15:38:35 +00002154 // Check that we have a comparison with a constant.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002155 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00002156 if (!ConstOp1)
Richard Sandiford35b9be22013-08-28 10:31:43 +00002157 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00002158 uint64_t CmpVal = ConstOp1->getZExtValue();
Richard Sandiford35b9be22013-08-28 10:31:43 +00002159
2160 // Check whether the nonconstant input is an AND with a constant mask.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002161 Comparison NewC(C);
2162 uint64_t MaskVal;
Craig Topper062a2ba2014-04-25 05:30:21 +00002163 ConstantSDNode *Mask = nullptr;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002164 if (C.Op0.getOpcode() == ISD::AND) {
2165 NewC.Op0 = C.Op0.getOperand(0);
2166 NewC.Op1 = C.Op0.getOperand(1);
2167 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
2168 if (!Mask)
2169 return;
2170 MaskVal = Mask->getZExtValue();
2171 } else {
2172 // There is no instruction to compare with a 64-bit immediate
2173 // so use TMHH instead if possible. We need an unsigned ordered
2174 // comparison with an i64 immediate.
2175 if (NewC.Op0.getValueType() != MVT::i64 ||
2176 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
2177 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
2178 NewC.ICmpType == SystemZICMP::SignedOnly)
2179 return;
2180 // Convert LE and GT comparisons into LT and GE.
2181 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
2182 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
2183 if (CmpVal == uint64_t(-1))
2184 return;
2185 CmpVal += 1;
2186 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
2187 }
2188 // If the low N bits of Op1 are zero than the low N bits of Op0 can
2189 // be masked off without changing the result.
2190 MaskVal = -(CmpVal & -CmpVal);
2191 NewC.ICmpType = SystemZICMP::UnsignedOnly;
2192 }
Ulrich Weigandb8d76fb2015-03-30 13:46:59 +00002193 if (!MaskVal)
2194 return;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002195
Richard Sandiford113c8702013-09-03 15:38:35 +00002196 // Check whether the combination of mask, comparison value and comparison
2197 // type are suitable.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002198 unsigned BitSize = NewC.Op0.getValueSizeInBits();
Richard Sandiford030c1652013-09-13 09:09:50 +00002199 unsigned NewCCMask, ShiftVal;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002200 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2201 NewC.Op0.getOpcode() == ISD::SHL &&
2202 isSimpleShift(NewC.Op0, ShiftVal) &&
Jonas Paulsson8c336472017-06-26 13:38:27 +00002203 (MaskVal >> ShiftVal != 0) &&
Ulrich Weigand5bfed6c2017-12-05 19:42:07 +00002204 ((CmpVal >> ShiftVal) << ShiftVal) == CmpVal &&
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002205 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
2206 MaskVal >> ShiftVal,
Richard Sandiford030c1652013-09-13 09:09:50 +00002207 CmpVal >> ShiftVal,
2208 SystemZICMP::Any))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002209 NewC.Op0 = NewC.Op0.getOperand(0);
2210 MaskVal >>= ShiftVal;
2211 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2212 NewC.Op0.getOpcode() == ISD::SRL &&
2213 isSimpleShift(NewC.Op0, ShiftVal) &&
Jonas Paulsson8c336472017-06-26 13:38:27 +00002214 (MaskVal << ShiftVal != 0) &&
Ulrich Weigand5bfed6c2017-12-05 19:42:07 +00002215 ((CmpVal << ShiftVal) >> ShiftVal) == CmpVal &&
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002216 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
Richard Sandiford030c1652013-09-13 09:09:50 +00002217 MaskVal << ShiftVal,
2218 CmpVal << ShiftVal,
2219 SystemZICMP::UnsignedOnly))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002220 NewC.Op0 = NewC.Op0.getOperand(0);
2221 MaskVal <<= ShiftVal;
Richard Sandiford030c1652013-09-13 09:09:50 +00002222 } else {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002223 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
2224 NewC.ICmpType);
Richard Sandiford030c1652013-09-13 09:09:50 +00002225 if (!NewCCMask)
2226 return;
2227 }
Richard Sandiford113c8702013-09-03 15:38:35 +00002228
Richard Sandiford35b9be22013-08-28 10:31:43 +00002229 // Go ahead and make the change.
Richard Sandifordd420f732013-12-13 15:28:45 +00002230 C.Opcode = SystemZISD::TM;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002231 C.Op0 = NewC.Op0;
2232 if (Mask && Mask->getZExtValue() == MaskVal)
2233 C.Op1 = SDValue(Mask, 0);
2234 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002235 C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType());
Richard Sandifordd420f732013-12-13 15:28:45 +00002236 C.CCValid = SystemZ::CCMASK_TM;
2237 C.CCMask = NewCCMask;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002238}
2239
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002240// Return a Comparison that tests the condition-code result of intrinsic
2241// node Call against constant integer CC using comparison code Cond.
2242// Opcode is the opcode of the SystemZISD operation for the intrinsic
2243// and CCValid is the set of possible condition-code results.
2244static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode,
2245 SDValue Call, unsigned CCValid, uint64_t CC,
2246 ISD::CondCode Cond) {
2247 Comparison C(Call, SDValue());
2248 C.Opcode = Opcode;
2249 C.CCValid = CCValid;
2250 if (Cond == ISD::SETEQ)
2251 // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3.
2252 C.CCMask = CC < 4 ? 1 << (3 - CC) : 0;
2253 else if (Cond == ISD::SETNE)
2254 // ...and the inverse of that.
2255 C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1;
2256 else if (Cond == ISD::SETLT || Cond == ISD::SETULT)
2257 // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3,
2258 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002259 C.CCMask = CC < 4 ? ~0U << (4 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002260 else if (Cond == ISD::SETGE || Cond == ISD::SETUGE)
2261 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002262 C.CCMask = CC < 4 ? ~(~0U << (4 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002263 else if (Cond == ISD::SETLE || Cond == ISD::SETULE)
2264 // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true),
2265 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002266 C.CCMask = CC < 4 ? ~0U << (3 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002267 else if (Cond == ISD::SETGT || Cond == ISD::SETUGT)
2268 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002269 C.CCMask = CC < 4 ? ~(~0U << (3 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002270 else
2271 llvm_unreachable("Unexpected integer comparison type");
2272 C.CCMask &= CCValid;
2273 return C;
2274}
2275
Richard Sandifordd420f732013-12-13 15:28:45 +00002276// Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
2277static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002278 ISD::CondCode Cond, const SDLoc &DL) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002279 if (CmpOp1.getOpcode() == ISD::Constant) {
2280 uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue();
2281 unsigned Opcode, CCValid;
2282 if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
2283 CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) &&
2284 isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid))
2285 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002286 if (CmpOp0.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
2287 CmpOp0.getResNo() == CmpOp0->getNumValues() - 1 &&
2288 isIntrinsicWithCC(CmpOp0, Opcode, CCValid))
2289 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002290 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002291 Comparison C(CmpOp0, CmpOp1);
2292 C.CCMask = CCMaskForCondCode(Cond);
2293 if (C.Op0.getValueType().isFloatingPoint()) {
2294 C.CCValid = SystemZ::CCMASK_FCMP;
2295 C.Opcode = SystemZISD::FCMP;
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002296 adjustForFNeg(C);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002297 } else {
Richard Sandifordd420f732013-12-13 15:28:45 +00002298 C.CCValid = SystemZ::CCMASK_ICMP;
2299 C.Opcode = SystemZISD::ICMP;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002300 // Choose the type of comparison. Equality and inequality tests can
2301 // use either signed or unsigned comparisons. The choice also doesn't
2302 // matter if both sign bits are known to be clear. In those cases we
2303 // want to give the main isel code the freedom to choose whichever
2304 // form fits best.
Richard Sandifordd420f732013-12-13 15:28:45 +00002305 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
2306 C.CCMask == SystemZ::CCMASK_CMP_NE ||
2307 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
2308 C.ICmpType = SystemZICMP::Any;
2309 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
2310 C.ICmpType = SystemZICMP::UnsignedOnly;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002311 else
Richard Sandifordd420f732013-12-13 15:28:45 +00002312 C.ICmpType = SystemZICMP::SignedOnly;
2313 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002314 adjustZeroCmp(DAG, DL, C);
2315 adjustSubwordCmp(DAG, DL, C);
2316 adjustForSubtraction(DAG, DL, C);
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002317 adjustForLTGFR(C);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002318 adjustICmpTruncate(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002319 }
2320
Richard Sandifordd420f732013-12-13 15:28:45 +00002321 if (shouldSwapCmpOperands(C)) {
2322 std::swap(C.Op0, C.Op1);
2323 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford24e597b2013-08-23 11:27:19 +00002324 }
2325
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002326 adjustForTestUnderMask(DAG, DL, C);
Richard Sandifordd420f732013-12-13 15:28:45 +00002327 return C;
2328}
2329
2330// Emit the comparison instruction described by C.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002331static SDValue emitCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002332 if (!C.Op1.getNode()) {
2333 SDValue Op;
2334 switch (C.Op0.getOpcode()) {
2335 case ISD::INTRINSIC_W_CHAIN:
2336 Op = emitIntrinsicWithChainAndGlue(DAG, C.Op0, C.Opcode);
2337 break;
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002338 case ISD::INTRINSIC_WO_CHAIN:
2339 Op = emitIntrinsicWithGlue(DAG, C.Op0, C.Opcode);
2340 break;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002341 default:
2342 llvm_unreachable("Invalid comparison operands");
2343 }
2344 return SDValue(Op.getNode(), Op->getNumValues() - 1);
2345 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002346 if (C.Opcode == SystemZISD::ICMP)
2347 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002348 DAG.getConstant(C.ICmpType, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002349 if (C.Opcode == SystemZISD::TM) {
2350 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
2351 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
2352 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002353 DAG.getConstant(RegisterOnly, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002354 }
2355 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002356}
2357
Richard Sandiford7d86e472013-08-21 09:34:56 +00002358// Implement a 32-bit *MUL_LOHI operation by extending both operands to
2359// 64 bits. Extend is the extension type to use. Store the high part
2360// in Hi and the low part in Lo.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002361static void lowerMUL_LOHI32(SelectionDAG &DAG, const SDLoc &DL, unsigned Extend,
2362 SDValue Op0, SDValue Op1, SDValue &Hi,
2363 SDValue &Lo) {
Richard Sandiford7d86e472013-08-21 09:34:56 +00002364 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
2365 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
2366 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002367 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
2368 DAG.getConstant(32, DL, MVT::i64));
Richard Sandiford7d86e472013-08-21 09:34:56 +00002369 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
2370 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
2371}
2372
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002373// Lower a binary operation that produces two VT results, one in each
2374// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
Ulrich Weigand43579cf2017-07-05 13:17:31 +00002375// and Opcode performs the GR128 operation. Store the even register result
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002376// in Even and the odd register result in Odd.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002377static void lowerGR128Binary(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigand43579cf2017-07-05 13:17:31 +00002378 unsigned Opcode, SDValue Op0, SDValue Op1,
2379 SDValue &Even, SDValue &Odd) {
2380 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped, Op0, Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002381 bool Is32Bit = is32Bit(VT);
Richard Sandifordd8163202013-09-13 09:12:44 +00002382 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
2383 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002384}
2385
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002386// Return an i32 value that is 1 if the CC value produced by Glue is
2387// in the mask CCMask and 0 otherwise. CC is known to have a value
2388// in CCValid, so other values can be ignored.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002389static SDValue emitSETCC(SelectionDAG &DAG, const SDLoc &DL, SDValue Glue,
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002390 unsigned CCValid, unsigned CCMask) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002391 IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
2392 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
2393
2394 if (Conversion.XORValue)
2395 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002396 DAG.getConstant(Conversion.XORValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002397
2398 if (Conversion.AddValue)
2399 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002400 DAG.getConstant(Conversion.AddValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002401
2402 // The SHR/AND sequence should get optimized to an RISBG.
2403 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002404 DAG.getConstant(Conversion.Bit, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002405 if (Conversion.Bit != 31)
2406 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002407 DAG.getConstant(1, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002408 return Result;
2409}
2410
Ulrich Weigandcd808232015-05-05 19:26:48 +00002411// Return the SystemISD vector comparison operation for CC, or 0 if it cannot
2412// be done directly. IsFP is true if CC is for a floating-point rather than
2413// integer comparison.
2414static unsigned getVectorComparison(ISD::CondCode CC, bool IsFP) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002415 switch (CC) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002416 case ISD::SETOEQ:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002417 case ISD::SETEQ:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002418 return IsFP ? SystemZISD::VFCMPE : SystemZISD::VICMPE;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002419
Ulrich Weigandcd808232015-05-05 19:26:48 +00002420 case ISD::SETOGE:
2421 case ISD::SETGE:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002422 return IsFP ? SystemZISD::VFCMPHE : static_cast<SystemZISD::NodeType>(0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002423
2424 case ISD::SETOGT:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002425 case ISD::SETGT:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002426 return IsFP ? SystemZISD::VFCMPH : SystemZISD::VICMPH;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002427
2428 case ISD::SETUGT:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002429 return IsFP ? static_cast<SystemZISD::NodeType>(0) : SystemZISD::VICMPHL;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002430
2431 default:
2432 return 0;
2433 }
2434}
2435
2436// Return the SystemZISD vector comparison operation for CC or its inverse,
2437// or 0 if neither can be done directly. Indicate in Invert whether the
Ulrich Weigandcd808232015-05-05 19:26:48 +00002438// result is for the inverse of CC. IsFP is true if CC is for a
2439// floating-point rather than integer comparison.
2440static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, bool IsFP,
2441 bool &Invert) {
2442 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002443 Invert = false;
2444 return Opcode;
2445 }
2446
Ulrich Weigandcd808232015-05-05 19:26:48 +00002447 CC = ISD::getSetCCInverse(CC, !IsFP);
2448 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002449 Invert = true;
2450 return Opcode;
2451 }
2452
2453 return 0;
2454}
2455
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002456// Return a v2f64 that contains the extended form of elements Start and Start+1
2457// of v4f32 value Op.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002458static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, const SDLoc &DL,
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002459 SDValue Op) {
2460 int Mask[] = { Start, -1, Start + 1, -1 };
2461 Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask);
2462 return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op);
2463}
2464
2465// Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode,
2466// producing a result of type VT.
Ulrich Weigand33435c42017-07-17 17:42:48 +00002467SDValue SystemZTargetLowering::getVectorCmp(SelectionDAG &DAG, unsigned Opcode,
2468 const SDLoc &DL, EVT VT,
2469 SDValue CmpOp0,
2470 SDValue CmpOp1) const {
2471 // There is no hardware support for v4f32 (unless we have the vector
2472 // enhancements facility 1), so extend the vector into two v2f64s
2473 // and compare those.
2474 if (CmpOp0.getValueType() == MVT::v4f32 &&
2475 !Subtarget.hasVectorEnhancements1()) {
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002476 SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0);
2477 SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0);
2478 SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1);
2479 SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1);
2480 SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1);
2481 SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1);
2482 return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes);
2483 }
2484 return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1);
2485}
2486
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002487// Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing
2488// an integer mask of type VT.
Ulrich Weigand33435c42017-07-17 17:42:48 +00002489SDValue SystemZTargetLowering::lowerVectorSETCC(SelectionDAG &DAG,
2490 const SDLoc &DL, EVT VT,
2491 ISD::CondCode CC,
2492 SDValue CmpOp0,
2493 SDValue CmpOp1) const {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002494 bool IsFP = CmpOp0.getValueType().isFloatingPoint();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002495 bool Invert = false;
2496 SDValue Cmp;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002497 switch (CC) {
2498 // Handle tests for order using (or (ogt y x) (oge x y)).
2499 case ISD::SETUO:
2500 Invert = true;
Simon Pilgrim8c4069e2017-07-07 10:07:09 +00002501 LLVM_FALLTHROUGH;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002502 case ISD::SETO: {
2503 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002504 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2505 SDValue GE = getVectorCmp(DAG, SystemZISD::VFCMPHE, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002506 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE);
2507 break;
2508 }
2509
2510 // Handle <> tests using (or (ogt y x) (ogt x y)).
2511 case ISD::SETUEQ:
2512 Invert = true;
Simon Pilgrim8c4069e2017-07-07 10:07:09 +00002513 LLVM_FALLTHROUGH;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002514 case ISD::SETONE: {
2515 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002516 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2517 SDValue GT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002518 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT);
2519 break;
2520 }
2521
2522 // Otherwise a single comparison is enough. It doesn't really
2523 // matter whether we try the inversion or the swap first, since
2524 // there are no cases where both work.
2525 default:
2526 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002527 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002528 else {
2529 CC = ISD::getSetCCSwappedOperands(CC);
2530 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002531 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002532 else
2533 llvm_unreachable("Unhandled comparison");
2534 }
2535 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002536 }
2537 if (Invert) {
2538 SDValue Mask = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2539 DAG.getConstant(65535, DL, MVT::i32));
2540 Mask = DAG.getNode(ISD::BITCAST, DL, VT, Mask);
2541 Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask);
2542 }
2543 return Cmp;
2544}
2545
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002546SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
2547 SelectionDAG &DAG) const {
2548 SDValue CmpOp0 = Op.getOperand(0);
2549 SDValue CmpOp1 = Op.getOperand(1);
2550 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
2551 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002552 EVT VT = Op.getValueType();
2553 if (VT.isVector())
2554 return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002555
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002556 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002557 SDValue Glue = emitCmp(DAG, DL, C);
2558 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002559}
2560
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002561SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002562 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2563 SDValue CmpOp0 = Op.getOperand(2);
2564 SDValue CmpOp1 = Op.getOperand(3);
2565 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002566 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002567
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002568 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002569 SDValue Glue = emitCmp(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002570 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002571 Op.getOperand(0), DAG.getConstant(C.CCValid, DL, MVT::i32),
2572 DAG.getConstant(C.CCMask, DL, MVT::i32), Dest, Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002573}
2574
Richard Sandiford57485472013-12-13 15:35:00 +00002575// Return true if Pos is CmpOp and Neg is the negative of CmpOp,
2576// allowing Pos and Neg to be wider than CmpOp.
2577static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
2578 return (Neg.getOpcode() == ISD::SUB &&
2579 Neg.getOperand(0).getOpcode() == ISD::Constant &&
2580 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
2581 Neg.getOperand(1) == Pos &&
2582 (Pos == CmpOp ||
2583 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
2584 Pos.getOperand(0) == CmpOp)));
2585}
2586
2587// Return the absolute or negative absolute of Op; IsNegative decides which.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002588static SDValue getAbsolute(SelectionDAG &DAG, const SDLoc &DL, SDValue Op,
Richard Sandiford57485472013-12-13 15:35:00 +00002589 bool IsNegative) {
2590 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
2591 if (IsNegative)
2592 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002593 DAG.getConstant(0, DL, Op.getValueType()), Op);
Richard Sandiford57485472013-12-13 15:35:00 +00002594 return Op;
2595}
2596
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002597SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
2598 SelectionDAG &DAG) const {
2599 SDValue CmpOp0 = Op.getOperand(0);
2600 SDValue CmpOp1 = Op.getOperand(1);
2601 SDValue TrueOp = Op.getOperand(2);
2602 SDValue FalseOp = Op.getOperand(3);
2603 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002604 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002605
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002606 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandiford57485472013-12-13 15:35:00 +00002607
2608 // Check for absolute and negative-absolute selections, including those
2609 // where the comparison value is sign-extended (for LPGFR and LNGFR).
2610 // This check supplements the one in DAGCombiner.
2611 if (C.Opcode == SystemZISD::ICMP &&
2612 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
2613 C.CCMask != SystemZ::CCMASK_CMP_NE &&
2614 C.Op1.getOpcode() == ISD::Constant &&
2615 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
2616 if (isAbsolute(C.Op0, TrueOp, FalseOp))
2617 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
2618 if (isAbsolute(C.Op0, FalseOp, TrueOp))
2619 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
2620 }
2621
Richard Sandifordd420f732013-12-13 15:28:45 +00002622 SDValue Glue = emitCmp(DAG, DL, C);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002623
2624 // Special case for handling -1/0 results. The shifts we use here
2625 // should get optimized with the IPM conversion sequence.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002626 auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
2627 auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002628 if (TrueC && FalseC) {
2629 int64_t TrueVal = TrueC->getSExtValue();
2630 int64_t FalseVal = FalseC->getSExtValue();
2631 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
2632 // Invert the condition if we want -1 on false.
2633 if (TrueVal == 0)
Richard Sandifordd420f732013-12-13 15:28:45 +00002634 C.CCMask ^= C.CCValid;
2635 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002636 EVT VT = Op.getValueType();
2637 // Extend the result to VT. Upper bits are ignored.
2638 if (!is32Bit(VT))
2639 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
2640 // Sign-extend from the low bit.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002641 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002642 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
2643 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
2644 }
2645 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002646
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002647 SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, DL, MVT::i32),
2648 DAG.getConstant(C.CCMask, DL, MVT::i32), Glue};
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002649
2650 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00002651 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002652}
2653
2654SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
2655 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002656 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002657 const GlobalValue *GV = Node->getGlobal();
2658 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002659 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopher93bf97c2014-06-27 07:38:01 +00002660 CodeModel::Model CM = DAG.getTarget().getCodeModel();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002661
2662 SDValue Result;
Rafael Espindola3beef8d2016-06-27 23:15:57 +00002663 if (Subtarget.isPC32DBLSymbol(GV, CM)) {
Richard Sandiford54b36912013-09-27 15:14:04 +00002664 // Assign anchors at 1<<12 byte boundaries.
2665 uint64_t Anchor = Offset & ~uint64_t(0xfff);
2666 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
2667 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2668
2669 // The offset can be folded into the address if it is aligned to a halfword.
2670 Offset -= Anchor;
2671 if (Offset != 0 && (Offset & 1) == 0) {
2672 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
2673 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002674 Offset = 0;
2675 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002676 } else {
2677 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
2678 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2679 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Justin Lebar9c375812016-07-15 18:27:10 +00002680 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002681 }
2682
2683 // If there was a non-zero offset that we didn't fold, create an explicit
2684 // addition for it.
2685 if (Offset != 0)
2686 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002687 DAG.getConstant(Offset, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002688
2689 return Result;
2690}
2691
Ulrich Weigand7db69182015-02-18 09:13:27 +00002692SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
2693 SelectionDAG &DAG,
2694 unsigned Opcode,
2695 SDValue GOTOffset) const {
2696 SDLoc DL(Node);
Mehdi Amini44ede332015-07-09 02:09:04 +00002697 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand7db69182015-02-18 09:13:27 +00002698 SDValue Chain = DAG.getEntryNode();
2699 SDValue Glue;
2700
2701 // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
2702 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
2703 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
2704 Glue = Chain.getValue(1);
2705 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
2706 Glue = Chain.getValue(1);
2707
2708 // The first call operand is the chain and the second is the TLS symbol.
2709 SmallVector<SDValue, 8> Ops;
2710 Ops.push_back(Chain);
2711 Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
2712 Node->getValueType(0),
2713 0, 0));
2714
2715 // Add argument registers to the end of the list so that they are
2716 // known live into the call.
2717 Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
2718 Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
2719
2720 // Add a register mask operand representing the call-preserved registers.
2721 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002722 const uint32_t *Mask =
2723 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002724 assert(Mask && "Missing call preserved mask for calling convention");
2725 Ops.push_back(DAG.getRegisterMask(Mask));
2726
2727 // Glue the call to the argument copies.
2728 Ops.push_back(Glue);
2729
2730 // Emit the call.
2731 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2732 Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
2733 Glue = Chain.getValue(1);
2734
2735 // Copy the return value from %r2.
2736 return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
2737}
2738
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002739SDValue SystemZTargetLowering::lowerThreadPointer(const SDLoc &DL,
2740 SelectionDAG &DAG) const {
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002741 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002742 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002743
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002744 // The high part of the thread pointer is in access register 0.
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002745 SDValue TPHi = DAG.getCopyFromReg(Chain, DL, SystemZ::A0, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002746 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
2747
2748 // The low part of the thread pointer is in access register 1.
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002749 SDValue TPLo = DAG.getCopyFromReg(Chain, DL, SystemZ::A1, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002750 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
2751
2752 // Merge them into a single 64-bit address.
2753 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002754 DAG.getConstant(32, DL, PtrVT));
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002755 return DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
2756}
2757
2758SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
2759 SelectionDAG &DAG) const {
2760 if (DAG.getTarget().Options.EmulatedTLS)
2761 return LowerToTLSEmulatedModel(Node, DAG);
2762 SDLoc DL(Node);
2763 const GlobalValue *GV = Node->getGlobal();
2764 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2765 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
2766
2767 SDValue TP = lowerThreadPointer(DL, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002768
Ulrich Weigand7db69182015-02-18 09:13:27 +00002769 // Get the offset of GA from the thread pointer, based on the TLS model.
2770 SDValue Offset;
2771 switch (model) {
2772 case TLSModel::GeneralDynamic: {
2773 // Load the GOT offset of the tls_index (module ID / per-symbol offset).
2774 SystemZConstantPoolValue *CPV =
2775 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002776
Ulrich Weigand7db69182015-02-18 09:13:27 +00002777 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002778 Offset = DAG.getLoad(
2779 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002780 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002781
2782 // Call __tls_get_offset to retrieve the offset.
2783 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
2784 break;
2785 }
2786
2787 case TLSModel::LocalDynamic: {
2788 // Load the GOT offset of the module ID.
2789 SystemZConstantPoolValue *CPV =
2790 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
2791
2792 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002793 Offset = DAG.getLoad(
2794 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002795 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002796
2797 // Call __tls_get_offset to retrieve the module base offset.
2798 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
2799
2800 // Note: The SystemZLDCleanupPass will remove redundant computations
2801 // of the module base offset. Count total number of local-dynamic
2802 // accesses to trigger execution of that pass.
2803 SystemZMachineFunctionInfo* MFI =
2804 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
2805 MFI->incNumLocalDynamicTLSAccesses();
2806
2807 // Add the per-symbol offset.
2808 CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
2809
2810 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002811 DTPOffset = DAG.getLoad(
2812 PtrVT, DL, DAG.getEntryNode(), DTPOffset,
Justin Lebar9c375812016-07-15 18:27:10 +00002813 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002814
2815 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
2816 break;
2817 }
2818
2819 case TLSModel::InitialExec: {
2820 // Load the offset from the GOT.
2821 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2822 SystemZII::MO_INDNTPOFF);
2823 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +00002824 Offset =
2825 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Offset,
2826 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002827 break;
2828 }
2829
2830 case TLSModel::LocalExec: {
2831 // Force the offset into the constant pool and load it from there.
2832 SystemZConstantPoolValue *CPV =
2833 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
2834
2835 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002836 Offset = DAG.getLoad(
2837 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002838 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002839 break;
Ulrich Weigandb7e59092015-02-18 09:42:23 +00002840 }
Ulrich Weigand7db69182015-02-18 09:13:27 +00002841 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002842
2843 // Add the base and offset together.
2844 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
2845}
2846
2847SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
2848 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002849 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002850 const BlockAddress *BA = Node->getBlockAddress();
2851 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002852 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002853
2854 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
2855 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2856 return Result;
2857}
2858
2859SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
2860 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002861 SDLoc DL(JT);
Mehdi Amini44ede332015-07-09 02:09:04 +00002862 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002863 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
2864
2865 // Use LARL to load the address of the table.
2866 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2867}
2868
2869SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
2870 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002871 SDLoc DL(CP);
Mehdi Amini44ede332015-07-09 02:09:04 +00002872 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002873
2874 SDValue Result;
2875 if (CP->isMachineConstantPoolEntry())
2876 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002877 CP->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002878 else
2879 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002880 CP->getAlignment(), CP->getOffset());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002881
2882 // Use LARL to load the address of the constant pool entry.
2883 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2884}
2885
Ulrich Weigandf557d082016-04-04 12:44:55 +00002886SDValue SystemZTargetLowering::lowerFRAMEADDR(SDValue Op,
2887 SelectionDAG &DAG) const {
2888 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002889 MachineFrameInfo &MFI = MF.getFrameInfo();
2890 MFI.setFrameAddressIsTaken(true);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002891
2892 SDLoc DL(Op);
2893 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2894 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2895
2896 // If the back chain frame index has not been allocated yet, do so.
2897 SystemZMachineFunctionInfo *FI = MF.getInfo<SystemZMachineFunctionInfo>();
2898 int BackChainIdx = FI->getFramePointerSaveIndex();
2899 if (!BackChainIdx) {
2900 // By definition, the frame address is the address of the back chain.
Matthias Braun941a7052016-07-28 18:40:00 +00002901 BackChainIdx = MFI.CreateFixedObject(8, -SystemZMC::CallFrameSize, false);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002902 FI->setFramePointerSaveIndex(BackChainIdx);
2903 }
2904 SDValue BackChain = DAG.getFrameIndex(BackChainIdx, PtrVT);
2905
2906 // FIXME The frontend should detect this case.
2907 if (Depth > 0) {
2908 report_fatal_error("Unsupported stack frame traversal count");
2909 }
2910
2911 return BackChain;
2912}
2913
2914SDValue SystemZTargetLowering::lowerRETURNADDR(SDValue Op,
2915 SelectionDAG &DAG) const {
2916 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002917 MachineFrameInfo &MFI = MF.getFrameInfo();
2918 MFI.setReturnAddressIsTaken(true);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002919
2920 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
2921 return SDValue();
2922
2923 SDLoc DL(Op);
2924 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2925 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2926
2927 // FIXME The frontend should detect this case.
2928 if (Depth > 0) {
2929 report_fatal_error("Unsupported stack frame traversal count");
2930 }
2931
2932 // Return R14D, which has the return address. Mark it an implicit live-in.
2933 unsigned LinkReg = MF.addLiveIn(SystemZ::R14D, &SystemZ::GR64BitRegClass);
2934 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, LinkReg, PtrVT);
2935}
2936
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002937SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
2938 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002939 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002940 SDValue In = Op.getOperand(0);
2941 EVT InVT = In.getValueType();
2942 EVT ResVT = Op.getValueType();
2943
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002944 // Convert loads directly. This is normally done by DAGCombiner,
2945 // but we need this case for bitcasts that are created during lowering
2946 // and which are then lowered themselves.
2947 if (auto *LoadN = dyn_cast<LoadSDNode>(In))
Jonas Paulssonb9a24672017-11-30 08:18:50 +00002948 if (ISD::isNormalLoad(LoadN)) {
2949 SDValue NewLoad = DAG.getLoad(ResVT, DL, LoadN->getChain(),
2950 LoadN->getBasePtr(), LoadN->getMemOperand());
2951 // Update the chain uses.
2952 DAG.ReplaceAllUsesOfValueWith(SDValue(LoadN, 1), NewLoad.getValue(1));
2953 return NewLoad;
2954 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002955
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002956 if (InVT == MVT::i32 && ResVT == MVT::f32) {
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002957 SDValue In64;
2958 if (Subtarget.hasHighWord()) {
2959 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
2960 MVT::i64);
2961 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
2962 MVT::i64, SDValue(U64, 0), In);
2963 } else {
2964 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
2965 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002966 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002967 }
2968 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002969 return DAG.getTargetExtractSubreg(SystemZ::subreg_r32,
Richard Sandifordd8163202013-09-13 09:12:44 +00002970 DL, MVT::f32, Out64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002971 }
2972 if (InVT == MVT::f32 && ResVT == MVT::i32) {
2973 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002974 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002975 MVT::f64, SDValue(U64, 0), In);
2976 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002977 if (Subtarget.hasHighWord())
2978 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
2979 MVT::i32, Out64);
2980 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002981 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002982 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002983 }
2984 llvm_unreachable("Unexpected bitcast combination");
2985}
2986
2987SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
2988 SelectionDAG &DAG) const {
2989 MachineFunction &MF = DAG.getMachineFunction();
2990 SystemZMachineFunctionInfo *FuncInfo =
2991 MF.getInfo<SystemZMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002992 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002993
2994 SDValue Chain = Op.getOperand(0);
2995 SDValue Addr = Op.getOperand(1);
2996 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002997 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002998
2999 // The initial values of each field.
3000 const unsigned NumFields = 4;
3001 SDValue Fields[NumFields] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003002 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT),
3003 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003004 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
3005 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
3006 };
3007
3008 // Store each field into its respective slot.
3009 SDValue MemOps[NumFields];
3010 unsigned Offset = 0;
3011 for (unsigned I = 0; I < NumFields; ++I) {
3012 SDValue FieldAddr = Addr;
3013 if (Offset != 0)
3014 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003015 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003016 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00003017 MachinePointerInfo(SV, Offset));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003018 Offset += 8;
3019 }
Craig Topper48d114b2014-04-26 18:35:24 +00003020 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003021}
3022
3023SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
3024 SelectionDAG &DAG) const {
3025 SDValue Chain = Op.getOperand(0);
3026 SDValue DstPtr = Op.getOperand(1);
3027 SDValue SrcPtr = Op.getOperand(2);
3028 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
3029 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003030 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003031
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003032 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003033 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00003034 /*isTailCall*/false,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003035 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
3036}
3037
3038SDValue SystemZTargetLowering::
3039lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003040 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003041 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braunf1caa282017-12-15 22:22:58 +00003042 bool RealignOpt = !MF.getFunction().hasFnAttribute("no-realign-stack");
3043 bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003044
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003045 SDValue Chain = Op.getOperand(0);
3046 SDValue Size = Op.getOperand(1);
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003047 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003048 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003049
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003050 // If user has set the no alignment function attribute, ignore
3051 // alloca alignments.
3052 uint64_t AlignVal = (RealignOpt ?
3053 dyn_cast<ConstantSDNode>(Align)->getZExtValue() : 0);
3054
3055 uint64_t StackAlign = TFI->getStackAlignment();
3056 uint64_t RequiredAlign = std::max(AlignVal, StackAlign);
3057 uint64_t ExtraAlignSpace = RequiredAlign - StackAlign;
3058
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003059 unsigned SPReg = getStackPointerRegisterToSaveRestore();
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003060 SDValue NeededSpace = Size;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003061
3062 // Get a reference to the stack pointer.
3063 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
3064
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003065 // If we need a backchain, save it now.
3066 SDValue Backchain;
3067 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003068 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003069
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003070 // Add extra space for alignment if needed.
3071 if (ExtraAlignSpace)
3072 NeededSpace = DAG.getNode(ISD::ADD, DL, MVT::i64, NeededSpace,
Elliot Colpbc2cfc22016-07-06 18:13:11 +00003073 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003074
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003075 // Get the new stack pointer value.
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003076 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, NeededSpace);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003077
3078 // Copy the new stack pointer back.
3079 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
3080
3081 // The allocated data lives above the 160 bytes allocated for the standard
3082 // frame, plus any outgoing stack arguments. We don't know how much that
3083 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
3084 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
3085 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
3086
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003087 // Dynamically realign if needed.
3088 if (RequiredAlign > StackAlign) {
3089 Result =
3090 DAG.getNode(ISD::ADD, DL, MVT::i64, Result,
3091 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
3092 Result =
3093 DAG.getNode(ISD::AND, DL, MVT::i64, Result,
3094 DAG.getConstant(~(RequiredAlign - 1), DL, MVT::i64));
3095 }
3096
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003097 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003098 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003099
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003100 SDValue Ops[2] = { Result, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00003101 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003102}
3103
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +00003104SDValue SystemZTargetLowering::lowerGET_DYNAMIC_AREA_OFFSET(
3105 SDValue Op, SelectionDAG &DAG) const {
3106 SDLoc DL(Op);
3107
3108 return DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
3109}
3110
Richard Sandiford7d86e472013-08-21 09:34:56 +00003111SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
3112 SelectionDAG &DAG) const {
3113 EVT VT = Op.getValueType();
3114 SDLoc DL(Op);
3115 SDValue Ops[2];
3116 if (is32Bit(VT))
3117 // Just do a normal 64-bit multiplication and extract the results.
3118 // We define this so that it can be used for constant division.
3119 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
3120 Op.getOperand(1), Ops[1], Ops[0]);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +00003121 else if (Subtarget.hasMiscellaneousExtensions2())
3122 // SystemZISD::SMUL_LOHI returns the low result in the odd register and
3123 // the high result in the even register. ISD::SMUL_LOHI is defined to
3124 // return the low half first, so the results are in reverse order.
3125 lowerGR128Binary(DAG, DL, VT, SystemZISD::SMUL_LOHI,
3126 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003127 else {
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003128 // Do a full 128-bit multiplication based on SystemZISD::UMUL_LOHI:
Richard Sandiford7d86e472013-08-21 09:34:56 +00003129 //
3130 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
3131 //
3132 // but using the fact that the upper halves are either all zeros
3133 // or all ones:
3134 //
3135 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
3136 //
3137 // and grouping the right terms together since they are quicker than the
3138 // multiplication:
3139 //
3140 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003141 SDValue C63 = DAG.getConstant(63, DL, MVT::i64);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003142 SDValue LL = Op.getOperand(0);
3143 SDValue RL = Op.getOperand(1);
3144 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
3145 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003146 // SystemZISD::UMUL_LOHI returns the low result in the odd register and
3147 // the high result in the even register. ISD::SMUL_LOHI is defined to
3148 // return the low half first, so the results are in reverse order.
3149 lowerGR128Binary(DAG, DL, VT, SystemZISD::UMUL_LOHI,
Richard Sandiford7d86e472013-08-21 09:34:56 +00003150 LL, RL, Ops[1], Ops[0]);
3151 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
3152 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
3153 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
3154 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
3155 }
Craig Topper64941d92014-04-27 19:20:57 +00003156 return DAG.getMergeValues(Ops, DL);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003157}
3158
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003159SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
3160 SelectionDAG &DAG) const {
3161 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003162 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003163 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00003164 if (is32Bit(VT))
3165 // Just do a normal 64-bit multiplication and extract the results.
3166 // We define this so that it can be used for constant division.
3167 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
3168 Op.getOperand(1), Ops[1], Ops[0]);
3169 else
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003170 // SystemZISD::UMUL_LOHI returns the low result in the odd register and
3171 // the high result in the even register. ISD::UMUL_LOHI is defined to
3172 // return the low half first, so the results are in reverse order.
3173 lowerGR128Binary(DAG, DL, VT, SystemZISD::UMUL_LOHI,
Richard Sandiford7d86e472013-08-21 09:34:56 +00003174 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003175 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003176}
3177
3178SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
3179 SelectionDAG &DAG) const {
3180 SDValue Op0 = Op.getOperand(0);
3181 SDValue Op1 = Op.getOperand(1);
3182 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003183 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003184
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003185 // We use DSGF for 32-bit division. This means the first operand must
3186 // always be 64-bit, and the second operand should be 32-bit whenever
3187 // that is possible, to improve performance.
3188 if (is32Bit(VT))
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003189 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003190 else if (DAG.ComputeNumSignBits(Op1) > 32)
Richard Sandiforde6e78852013-07-02 15:40:22 +00003191 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003192
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003193 // DSG(F) returns the remainder in the even register and the
3194 // quotient in the odd register.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003195 SDValue Ops[2];
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003196 lowerGR128Binary(DAG, DL, VT, SystemZISD::SDIVREM, Op0, Op1, Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003197 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003198}
3199
3200SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
3201 SelectionDAG &DAG) const {
3202 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003203 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003204
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003205 // DL(G) returns the remainder in the even register and the
3206 // quotient in the odd register.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003207 SDValue Ops[2];
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003208 lowerGR128Binary(DAG, DL, VT, SystemZISD::UDIVREM,
3209 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003210 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003211}
3212
3213SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
3214 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
3215
3216 // Get the known-zero masks for each operand.
3217 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
Craig Topperd0af7e82017-04-28 05:31:46 +00003218 KnownBits Known[2];
3219 DAG.computeKnownBits(Ops[0], Known[0]);
3220 DAG.computeKnownBits(Ops[1], Known[1]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003221
3222 // See if the upper 32 bits of one operand and the lower 32 bits of the
3223 // other are known zero. They are the low and high operands respectively.
Craig Topperd0af7e82017-04-28 05:31:46 +00003224 uint64_t Masks[] = { Known[0].Zero.getZExtValue(),
3225 Known[1].Zero.getZExtValue() };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003226 unsigned High, Low;
3227 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
3228 High = 1, Low = 0;
3229 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
3230 High = 0, Low = 1;
3231 else
3232 return Op;
3233
3234 SDValue LowOp = Ops[Low];
3235 SDValue HighOp = Ops[High];
3236
3237 // If the high part is a constant, we're better off using IILH.
3238 if (HighOp.getOpcode() == ISD::Constant)
3239 return Op;
3240
3241 // If the low part is a constant that is outside the range of LHI,
3242 // then we're better off using IILF.
3243 if (LowOp.getOpcode() == ISD::Constant) {
3244 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
3245 if (!isInt<16>(Value))
3246 return Op;
3247 }
3248
3249 // Check whether the high part is an AND that doesn't change the
3250 // high 32 bits and just masks out low bits. We can skip it if so.
3251 if (HighOp.getOpcode() == ISD::AND &&
3252 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
Richard Sandifordccc2a7c2013-12-03 11:01:54 +00003253 SDValue HighOp0 = HighOp.getOperand(0);
3254 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
3255 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
3256 HighOp = HighOp0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003257 }
3258
3259 // Take advantage of the fact that all GR32 operations only change the
3260 // low 32 bits by truncating Low to an i32 and inserting it directly
3261 // using a subreg. The interesting cases are those where the truncation
3262 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003263 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003264 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
Richard Sandiford87a44362013-09-30 10:28:35 +00003265 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00003266 MVT::i64, HighOp, Low32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003267}
3268
Ulrich Weigandb4012182015-03-31 12:56:33 +00003269SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
3270 SelectionDAG &DAG) const {
3271 EVT VT = Op.getValueType();
Ulrich Weigandb4012182015-03-31 12:56:33 +00003272 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003273 Op = Op.getOperand(0);
3274
3275 // Handle vector types via VPOPCT.
3276 if (VT.isVector()) {
3277 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op);
3278 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00003279 switch (VT.getScalarSizeInBits()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003280 case 8:
3281 break;
3282 case 16: {
3283 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
3284 SDValue Shift = DAG.getConstant(8, DL, MVT::i32);
3285 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift);
3286 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3287 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift);
3288 break;
3289 }
3290 case 32: {
3291 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3292 DAG.getConstant(0, DL, MVT::i32));
3293 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3294 break;
3295 }
3296 case 64: {
3297 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3298 DAG.getConstant(0, DL, MVT::i32));
3299 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp);
3300 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3301 break;
3302 }
3303 default:
3304 llvm_unreachable("Unexpected type");
3305 }
3306 return Op;
3307 }
Ulrich Weigandb4012182015-03-31 12:56:33 +00003308
3309 // Get the known-zero mask for the operand.
Craig Topperd0af7e82017-04-28 05:31:46 +00003310 KnownBits Known;
3311 DAG.computeKnownBits(Op, Known);
3312 unsigned NumSignificantBits = (~Known.Zero).getActiveBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003313 if (NumSignificantBits == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003314 return DAG.getConstant(0, DL, VT);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003315
3316 // Skip known-zero high parts of the operand.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003317 int64_t OrigBitSize = VT.getSizeInBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003318 int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits);
3319 BitSize = std::min(BitSize, OrigBitSize);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003320
3321 // The POPCNT instruction counts the number of bits in each byte.
3322 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op);
3323 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op);
3324 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
3325
3326 // Add up per-byte counts in a binary tree. All bits of Op at
3327 // position larger than BitSize remain zero throughout.
3328 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003329 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003330 if (BitSize != OrigBitSize)
3331 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003332 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003333 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3334 }
3335
3336 // Extract overall result from high byte.
3337 if (BitSize > 8)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003338 Op = DAG.getNode(ISD::SRL, DL, VT, Op,
3339 DAG.getConstant(BitSize - 8, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003340
3341 return Op;
3342}
3343
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003344SDValue SystemZTargetLowering::lowerATOMIC_FENCE(SDValue Op,
3345 SelectionDAG &DAG) const {
3346 SDLoc DL(Op);
3347 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
3348 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003349 SyncScope::ID FenceSSID = static_cast<SyncScope::ID>(
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003350 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
3351
3352 // The only fence that needs an instruction is a sequentially-consistent
3353 // cross-thread fence.
JF Bastien800f87a2016-04-06 21:19:33 +00003354 if (FenceOrdering == AtomicOrdering::SequentiallyConsistent &&
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003355 FenceSSID == SyncScope::System) {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003356 return SDValue(DAG.getMachineNode(SystemZ::Serialize, DL, MVT::Other,
JF Bastien800f87a2016-04-06 21:19:33 +00003357 Op.getOperand(0)),
3358 0);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003359 }
3360
3361 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
3362 return DAG.getNode(SystemZISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0));
3363}
3364
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003365// Op is an atomic load. Lower it into a normal volatile load.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003366SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
3367 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003368 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003369 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003370 Node->getChain(), Node->getBasePtr(),
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003371 Node->getMemoryVT(), Node->getMemOperand());
3372}
3373
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003374// Op is an atomic store. Lower it into a normal volatile store.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003375SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
3376 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003377 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003378 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
3379 Node->getBasePtr(), Node->getMemoryVT(),
3380 Node->getMemOperand());
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003381 // We have to enforce sequential consistency by performing a
3382 // serialization operation after the store.
3383 if (Node->getOrdering() == AtomicOrdering::SequentiallyConsistent)
3384 Chain = SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op),
3385 MVT::Other, Chain), 0);
3386 return Chain;
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003387}
3388
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003389// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
3390// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003391SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
3392 SelectionDAG &DAG,
3393 unsigned Opcode) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003394 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003395
3396 // 32-bit operations need no code outside the main loop.
3397 EVT NarrowVT = Node->getMemoryVT();
3398 EVT WideVT = MVT::i32;
3399 if (NarrowVT == WideVT)
3400 return Op;
3401
3402 int64_t BitSize = NarrowVT.getSizeInBits();
3403 SDValue ChainIn = Node->getChain();
3404 SDValue Addr = Node->getBasePtr();
3405 SDValue Src2 = Node->getVal();
3406 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003407 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003408 EVT PtrVT = Addr.getValueType();
3409
3410 // Convert atomic subtracts of constants into additions.
3411 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
Richard Sandiford21f5d682014-03-06 11:22:58 +00003412 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003413 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003414 Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003415 }
3416
3417 // Get the address of the containing word.
3418 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003419 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003420
3421 // Get the number of bits that the word must be rotated left in order
3422 // to bring the field to the top bits of a GR32.
3423 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003424 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003425 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3426
3427 // Get the complementing shift amount, for rotating a field in the top
3428 // bits back to its proper position.
3429 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003430 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003431
3432 // Extend the source operand to 32 bits and prepare it for the inner loop.
3433 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
3434 // operations require the source to be shifted in advance. (This shift
3435 // can be folded if the source is constant.) For AND and NAND, the lower
3436 // bits must be set, while for other opcodes they should be left clear.
3437 if (Opcode != SystemZISD::ATOMIC_SWAPW)
3438 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003439 DAG.getConstant(32 - BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003440 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
3441 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
3442 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003443 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003444
3445 // Construct the ATOMIC_LOADW_* node.
3446 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3447 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003448 DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003449 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003450 NarrowVT, MMO);
3451
3452 // Rotate the result of the final CS so that the field is in the lower
3453 // bits of a GR32, then truncate it.
3454 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003455 DAG.getConstant(BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003456 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
3457
3458 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00003459 return DAG.getMergeValues(RetOps, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003460}
3461
Richard Sandiford41350a52013-12-24 15:18:04 +00003462// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
Richard Sandiford002019a2013-12-24 15:22:39 +00003463// into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
Richard Sandiford41350a52013-12-24 15:18:04 +00003464// operations into additions.
3465SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
3466 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003467 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandiford41350a52013-12-24 15:18:04 +00003468 EVT MemVT = Node->getMemoryVT();
3469 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
3470 // A full-width operation.
3471 assert(Op.getValueType() == MemVT && "Mismatched VTs");
3472 SDValue Src2 = Node->getVal();
3473 SDValue NegSrc2;
3474 SDLoc DL(Src2);
3475
Richard Sandiford21f5d682014-03-06 11:22:58 +00003476 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
Richard Sandiford41350a52013-12-24 15:18:04 +00003477 // Use an addition if the operand is constant and either LAA(G) is
3478 // available or the negative value is in the range of A(G)FHI.
3479 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
Eric Christopher93bf97c2014-06-27 07:38:01 +00003480 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003481 NegSrc2 = DAG.getConstant(Value, DL, MemVT);
Eric Christopher93bf97c2014-06-27 07:38:01 +00003482 } else if (Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00003483 // Use LAA(G) if available.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003484 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT),
Richard Sandiford41350a52013-12-24 15:18:04 +00003485 Src2);
3486
3487 if (NegSrc2.getNode())
3488 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
3489 Node->getChain(), Node->getBasePtr(), NegSrc2,
Konstantin Zhuravlyov8ea02462016-10-15 22:01:18 +00003490 Node->getMemOperand());
Richard Sandiford41350a52013-12-24 15:18:04 +00003491
3492 // Use the node as-is.
3493 return Op;
3494 }
3495
3496 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
3497}
3498
Ulrich Weigand0f1de042017-09-28 16:22:54 +00003499// Lower 8/16/32/64-bit ATOMIC_CMP_SWAP_WITH_SUCCESS node.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003500SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
3501 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003502 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003503 SDValue ChainIn = Node->getOperand(0);
3504 SDValue Addr = Node->getOperand(1);
3505 SDValue CmpVal = Node->getOperand(2);
3506 SDValue SwapVal = Node->getOperand(3);
3507 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003508 SDLoc DL(Node);
Ulrich Weigand0f1de042017-09-28 16:22:54 +00003509
3510 // We have native support for 32-bit and 64-bit compare and swap, but we
3511 // still need to expand extracting the "success" result from the CC.
3512 EVT NarrowVT = Node->getMemoryVT();
3513 EVT WideVT = NarrowVT == MVT::i64 ? MVT::i64 : MVT::i32;
3514 if (NarrowVT == WideVT) {
3515 SDVTList Tys = DAG.getVTList(WideVT, MVT::Other, MVT::Glue);
3516 SDValue Ops[] = { ChainIn, Addr, CmpVal, SwapVal };
3517 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAP,
3518 DL, Tys, Ops, NarrowVT, MMO);
3519 SDValue Success = emitSETCC(DAG, DL, AtomicOp.getValue(2),
3520 SystemZ::CCMASK_CS, SystemZ::CCMASK_CS_EQ);
3521
3522 DAG.ReplaceAllUsesOfValueWith(Op.getValue(0), AtomicOp.getValue(0));
3523 DAG.ReplaceAllUsesOfValueWith(Op.getValue(1), Success);
3524 DAG.ReplaceAllUsesOfValueWith(Op.getValue(2), AtomicOp.getValue(1));
3525 return SDValue();
3526 }
3527
3528 // Convert 8-bit and 16-bit compare and swap to a loop, implemented
3529 // via a fullword ATOMIC_CMP_SWAPW operation.
3530 int64_t BitSize = NarrowVT.getSizeInBits();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003531 EVT PtrVT = Addr.getValueType();
3532
3533 // Get the address of the containing word.
3534 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003535 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003536
3537 // Get the number of bits that the word must be rotated left in order
3538 // to bring the field to the top bits of a GR32.
3539 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003540 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003541 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3542
3543 // Get the complementing shift amount, for rotating a field in the top
3544 // bits back to its proper position.
3545 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003546 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003547
3548 // Construct the ATOMIC_CMP_SWAPW node.
Ulrich Weigand0f1de042017-09-28 16:22:54 +00003549 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other, MVT::Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003550 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003551 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003552 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003553 VTList, Ops, NarrowVT, MMO);
Ulrich Weigand0f1de042017-09-28 16:22:54 +00003554 SDValue Success = emitSETCC(DAG, DL, AtomicOp.getValue(2),
3555 SystemZ::CCMASK_ICMP, SystemZ::CCMASK_CMP_EQ);
3556
3557 DAG.ReplaceAllUsesOfValueWith(Op.getValue(0), AtomicOp.getValue(0));
3558 DAG.ReplaceAllUsesOfValueWith(Op.getValue(1), Success);
3559 DAG.ReplaceAllUsesOfValueWith(Op.getValue(2), AtomicOp.getValue(1));
3560 return SDValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003561}
3562
3563SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
3564 SelectionDAG &DAG) const {
3565 MachineFunction &MF = DAG.getMachineFunction();
3566 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003567 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003568 SystemZ::R15D, Op.getValueType());
3569}
3570
3571SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
3572 SelectionDAG &DAG) const {
3573 MachineFunction &MF = DAG.getMachineFunction();
3574 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Matthias Braunf1caa282017-12-15 22:22:58 +00003575 bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003576
3577 SDValue Chain = Op.getOperand(0);
3578 SDValue NewSP = Op.getOperand(1);
3579 SDValue Backchain;
3580 SDLoc DL(Op);
3581
3582 if (StoreBackchain) {
3583 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, MVT::i64);
Justin Lebar9c375812016-07-15 18:27:10 +00003584 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003585 }
3586
3587 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R15D, NewSP);
3588
3589 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003590 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003591
3592 return Chain;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003593}
3594
Richard Sandiford03481332013-08-23 11:36:42 +00003595SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
3596 SelectionDAG &DAG) const {
3597 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3598 if (!IsData)
3599 // Just preserve the chain.
3600 return Op.getOperand(0);
3601
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003602 SDLoc DL(Op);
Richard Sandiford03481332013-08-23 11:36:42 +00003603 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
3604 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
Richard Sandiford21f5d682014-03-06 11:22:58 +00003605 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
Richard Sandiford03481332013-08-23 11:36:42 +00003606 SDValue Ops[] = {
3607 Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003608 DAG.getConstant(Code, DL, MVT::i32),
Richard Sandiford03481332013-08-23 11:36:42 +00003609 Op.getOperand(1)
3610 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003611 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003612 Node->getVTList(), Ops,
Richard Sandiford03481332013-08-23 11:36:42 +00003613 Node->getMemoryVT(), Node->getMemOperand());
3614}
3615
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003616// Return an i32 that contains the value of CC immediately after After,
3617// whose final operand must be MVT::Glue.
3618static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003619 SDLoc DL(After);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003620 SDValue Glue = SDValue(After, After->getNumValues() - 1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003621 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
3622 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
3623 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003624}
3625
3626SDValue
3627SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
3628 SelectionDAG &DAG) const {
3629 unsigned Opcode, CCValid;
3630 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) {
3631 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
3632 SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode);
3633 SDValue CC = getCCResult(DAG, Glued.getNode());
3634 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC);
3635 return SDValue();
3636 }
3637
3638 return SDValue();
3639}
3640
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003641SDValue
3642SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
3643 SelectionDAG &DAG) const {
3644 unsigned Opcode, CCValid;
3645 if (isIntrinsicWithCC(Op, Opcode, CCValid)) {
3646 SDValue Glued = emitIntrinsicWithGlue(DAG, Op, Opcode);
3647 SDValue CC = getCCResult(DAG, Glued.getNode());
3648 if (Op->getNumValues() == 1)
3649 return CC;
3650 assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result");
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00003651 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), Glued,
3652 CC);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003653 }
3654
3655 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3656 switch (Id) {
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00003657 case Intrinsic::thread_pointer:
3658 return lowerThreadPointer(SDLoc(Op), DAG);
3659
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003660 case Intrinsic::s390_vpdi:
3661 return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(),
3662 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3663
3664 case Intrinsic::s390_vperm:
3665 return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(),
3666 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3667
3668 case Intrinsic::s390_vuphb:
3669 case Intrinsic::s390_vuphh:
3670 case Intrinsic::s390_vuphf:
3671 return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(),
3672 Op.getOperand(1));
3673
3674 case Intrinsic::s390_vuplhb:
3675 case Intrinsic::s390_vuplhh:
3676 case Intrinsic::s390_vuplhf:
3677 return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(),
3678 Op.getOperand(1));
3679
3680 case Intrinsic::s390_vuplb:
3681 case Intrinsic::s390_vuplhw:
3682 case Intrinsic::s390_vuplf:
3683 return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(),
3684 Op.getOperand(1));
3685
3686 case Intrinsic::s390_vupllb:
3687 case Intrinsic::s390_vupllh:
3688 case Intrinsic::s390_vupllf:
3689 return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(),
3690 Op.getOperand(1));
3691
3692 case Intrinsic::s390_vsumb:
3693 case Intrinsic::s390_vsumh:
3694 case Intrinsic::s390_vsumgh:
3695 case Intrinsic::s390_vsumgf:
3696 case Intrinsic::s390_vsumqf:
3697 case Intrinsic::s390_vsumqg:
3698 return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(),
3699 Op.getOperand(1), Op.getOperand(2));
3700 }
3701
3702 return SDValue();
3703}
3704
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003705namespace {
3706// Says that SystemZISD operation Opcode can be used to perform the equivalent
3707// of a VPERM with permute vector Bytes. If Opcode takes three operands,
3708// Operand is the constant third operand, otherwise it is the number of
3709// bytes in each element of the result.
3710struct Permute {
3711 unsigned Opcode;
3712 unsigned Operand;
3713 unsigned char Bytes[SystemZ::VectorBytes];
3714};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003715}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003716
3717static const Permute PermuteForms[] = {
3718 // VMRHG
3719 { SystemZISD::MERGE_HIGH, 8,
3720 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } },
3721 // VMRHF
3722 { SystemZISD::MERGE_HIGH, 4,
3723 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
3724 // VMRHH
3725 { SystemZISD::MERGE_HIGH, 2,
3726 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
3727 // VMRHB
3728 { SystemZISD::MERGE_HIGH, 1,
3729 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
3730 // VMRLG
3731 { SystemZISD::MERGE_LOW, 8,
3732 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } },
3733 // VMRLF
3734 { SystemZISD::MERGE_LOW, 4,
3735 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
3736 // VMRLH
3737 { SystemZISD::MERGE_LOW, 2,
3738 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
3739 // VMRLB
3740 { SystemZISD::MERGE_LOW, 1,
3741 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
3742 // VPKG
3743 { SystemZISD::PACK, 4,
3744 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } },
3745 // VPKF
3746 { SystemZISD::PACK, 2,
3747 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
3748 // VPKH
3749 { SystemZISD::PACK, 1,
3750 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
3751 // VPDI V1, V2, 4 (low half of V1, high half of V2)
3752 { SystemZISD::PERMUTE_DWORDS, 4,
3753 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } },
3754 // VPDI V1, V2, 1 (high half of V1, low half of V2)
3755 { SystemZISD::PERMUTE_DWORDS, 1,
3756 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } }
3757};
3758
3759// Called after matching a vector shuffle against a particular pattern.
3760// Both the original shuffle and the pattern have two vector operands.
3761// OpNos[0] is the operand of the original shuffle that should be used for
3762// operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything.
3763// OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and
3764// set OpNo0 and OpNo1 to the shuffle operands that should actually be used
3765// for operands 0 and 1 of the pattern.
3766static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) {
3767 if (OpNos[0] < 0) {
3768 if (OpNos[1] < 0)
3769 return false;
3770 OpNo0 = OpNo1 = OpNos[1];
3771 } else if (OpNos[1] < 0) {
3772 OpNo0 = OpNo1 = OpNos[0];
3773 } else {
3774 OpNo0 = OpNos[0];
3775 OpNo1 = OpNos[1];
3776 }
3777 return true;
3778}
3779
3780// Bytes is a VPERM-like permute vector, except that -1 is used for
3781// undefined bytes. Return true if the VPERM can be implemented using P.
3782// When returning true set OpNo0 to the VPERM operand that should be
3783// used for operand 0 of P and likewise OpNo1 for operand 1 of P.
3784//
3785// For example, if swapping the VPERM operands allows P to match, OpNo0
3786// will be 1 and OpNo1 will be 0. If instead Bytes only refers to one
3787// operand, but rewriting it to use two duplicated operands allows it to
3788// match P, then OpNo0 and OpNo1 will be the same.
3789static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P,
3790 unsigned &OpNo0, unsigned &OpNo1) {
3791 int OpNos[] = { -1, -1 };
3792 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
3793 int Elt = Bytes[I];
3794 if (Elt >= 0) {
3795 // Make sure that the two permute vectors use the same suboperand
3796 // byte number. Only the operand numbers (the high bits) are
3797 // allowed to differ.
3798 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1))
3799 return false;
3800 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes;
3801 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes;
3802 // Make sure that the operand mappings are consistent with previous
3803 // elements.
3804 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3805 return false;
3806 OpNos[ModelOpNo] = RealOpNo;
3807 }
3808 }
3809 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3810}
3811
3812// As above, but search for a matching permute.
3813static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes,
3814 unsigned &OpNo0, unsigned &OpNo1) {
3815 for (auto &P : PermuteForms)
3816 if (matchPermute(Bytes, P, OpNo0, OpNo1))
3817 return &P;
3818 return nullptr;
3819}
3820
3821// Bytes is a VPERM-like permute vector, except that -1 is used for
3822// undefined bytes. This permute is an operand of an outer permute.
3823// See whether redistributing the -1 bytes gives a shuffle that can be
3824// implemented using P. If so, set Transform to a VPERM-like permute vector
3825// that, when applied to the result of P, gives the original permute in Bytes.
3826static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3827 const Permute &P,
3828 SmallVectorImpl<int> &Transform) {
3829 unsigned To = 0;
3830 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) {
3831 int Elt = Bytes[From];
3832 if (Elt < 0)
3833 // Byte number From of the result is undefined.
3834 Transform[From] = -1;
3835 else {
3836 while (P.Bytes[To] != Elt) {
3837 To += 1;
3838 if (To == SystemZ::VectorBytes)
3839 return false;
3840 }
3841 Transform[From] = To;
3842 }
3843 }
3844 return true;
3845}
3846
3847// As above, but search for a matching permute.
3848static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3849 SmallVectorImpl<int> &Transform) {
3850 for (auto &P : PermuteForms)
3851 if (matchDoublePermute(Bytes, P, Transform))
3852 return &P;
3853 return nullptr;
3854}
3855
3856// Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask,
3857// as if it had type vNi8.
3858static void getVPermMask(ShuffleVectorSDNode *VSN,
3859 SmallVectorImpl<int> &Bytes) {
3860 EVT VT = VSN->getValueType(0);
3861 unsigned NumElements = VT.getVectorNumElements();
3862 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3863 Bytes.resize(NumElements * BytesPerElement, -1);
3864 for (unsigned I = 0; I < NumElements; ++I) {
3865 int Index = VSN->getMaskElt(I);
3866 if (Index >= 0)
3867 for (unsigned J = 0; J < BytesPerElement; ++J)
3868 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
3869 }
3870}
3871
3872// Bytes is a VPERM-like permute vector, except that -1 is used for
3873// undefined bytes. See whether bytes [Start, Start + BytesPerElement) of
3874// the result come from a contiguous sequence of bytes from one input.
3875// Set Base to the selector for the first byte if so.
3876static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start,
3877 unsigned BytesPerElement, int &Base) {
3878 Base = -1;
3879 for (unsigned I = 0; I < BytesPerElement; ++I) {
3880 if (Bytes[Start + I] >= 0) {
3881 unsigned Elem = Bytes[Start + I];
3882 if (Base < 0) {
3883 Base = Elem - I;
3884 // Make sure the bytes would come from one input operand.
3885 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size())
3886 return false;
3887 } else if (unsigned(Base) != Elem - I)
3888 return false;
3889 }
3890 }
3891 return true;
3892}
3893
3894// Bytes is a VPERM-like permute vector, except that -1 is used for
3895// undefined bytes. Return true if it can be performed using VSLDI.
3896// When returning true, set StartIndex to the shift amount and OpNo0
3897// and OpNo1 to the VPERM operands that should be used as the first
3898// and second shift operand respectively.
3899static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes,
3900 unsigned &StartIndex, unsigned &OpNo0,
3901 unsigned &OpNo1) {
3902 int OpNos[] = { -1, -1 };
3903 int Shift = -1;
3904 for (unsigned I = 0; I < 16; ++I) {
3905 int Index = Bytes[I];
3906 if (Index >= 0) {
3907 int ExpectedShift = (Index - I) % SystemZ::VectorBytes;
3908 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes;
3909 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes;
3910 if (Shift < 0)
3911 Shift = ExpectedShift;
3912 else if (Shift != ExpectedShift)
3913 return false;
3914 // Make sure that the operand mappings are consistent with previous
3915 // elements.
3916 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3917 return false;
3918 OpNos[ModelOpNo] = RealOpNo;
3919 }
3920 }
3921 StartIndex = Shift;
3922 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3923}
3924
3925// Create a node that performs P on operands Op0 and Op1, casting the
3926// operands to the appropriate type. The type of the result is determined by P.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003927static SDValue getPermuteNode(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003928 const Permute &P, SDValue Op0, SDValue Op1) {
3929 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input
3930 // elements of a PACK are twice as wide as the outputs.
3931 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 :
3932 P.Opcode == SystemZISD::PACK ? P.Operand * 2 :
3933 P.Operand);
3934 // Cast both operands to the appropriate type.
3935 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8),
3936 SystemZ::VectorBytes / InBytes);
3937 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0);
3938 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1);
3939 SDValue Op;
3940 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) {
3941 SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32);
3942 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2);
3943 } else if (P.Opcode == SystemZISD::PACK) {
3944 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8),
3945 SystemZ::VectorBytes / P.Operand);
3946 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1);
3947 } else {
3948 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1);
3949 }
3950 return Op;
3951}
3952
3953// Bytes is a VPERM-like permute vector, except that -1 is used for
3954// undefined bytes. Implement it on operands Ops[0] and Ops[1] using
3955// VSLDI or VPERM.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003956static SDValue getGeneralPermuteNode(SelectionDAG &DAG, const SDLoc &DL,
3957 SDValue *Ops,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003958 const SmallVectorImpl<int> &Bytes) {
3959 for (unsigned I = 0; I < 2; ++I)
3960 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]);
3961
3962 // First see whether VSLDI can be used.
3963 unsigned StartIndex, OpNo0, OpNo1;
3964 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1))
3965 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0],
3966 Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32));
3967
3968 // Fall back on VPERM. Construct an SDNode for the permute vector.
3969 SDValue IndexNodes[SystemZ::VectorBytes];
3970 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3971 if (Bytes[I] >= 0)
3972 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32);
3973 else
3974 IndexNodes[I] = DAG.getUNDEF(MVT::i32);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00003975 SDValue Op2 = DAG.getBuildVector(MVT::v16i8, DL, IndexNodes);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003976 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2);
3977}
3978
3979namespace {
3980// Describes a general N-operand vector shuffle.
3981struct GeneralShuffle {
3982 GeneralShuffle(EVT vt) : VT(vt) {}
3983 void addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003984 bool add(SDValue, unsigned);
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003985 SDValue getNode(SelectionDAG &, const SDLoc &);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003986
3987 // The operands of the shuffle.
3988 SmallVector<SDValue, SystemZ::VectorBytes> Ops;
3989
3990 // Index I is -1 if byte I of the result is undefined. Otherwise the
3991 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand
3992 // Bytes[I] / SystemZ::VectorBytes.
3993 SmallVector<int, SystemZ::VectorBytes> Bytes;
3994
3995 // The type of the shuffle result.
3996 EVT VT;
3997};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003998}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003999
4000// Add an extra undefined element to the shuffle.
4001void GeneralShuffle::addUndef() {
4002 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
4003 for (unsigned I = 0; I < BytesPerElement; ++I)
4004 Bytes.push_back(-1);
4005}
4006
4007// Add an extra element to the shuffle, taking it from element Elem of Op.
4008// A null Op indicates a vector input whose value will be calculated later;
4009// there is at most one such input per shuffle and it always has the same
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004010// type as the result. Aborts and returns false if the source vector elements
4011// of an EXTRACT_VECTOR_ELT are smaller than the destination elements. Per
4012// LLVM they become implicitly extended, but this is rare and not optimized.
4013bool GeneralShuffle::add(SDValue Op, unsigned Elem) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004014 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
4015
4016 // The source vector can have wider elements than the result,
4017 // either through an explicit TRUNCATE or because of type legalization.
4018 // We want the least significant part.
4019 EVT FromVT = Op.getNode() ? Op.getValueType() : VT;
4020 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004021
4022 // Return false if the source elements are smaller than their destination
4023 // elements.
4024 if (FromBytesPerElement < BytesPerElement)
4025 return false;
4026
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004027 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes +
4028 (FromBytesPerElement - BytesPerElement));
4029
4030 // Look through things like shuffles and bitcasts.
4031 while (Op.getNode()) {
4032 if (Op.getOpcode() == ISD::BITCAST)
4033 Op = Op.getOperand(0);
4034 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) {
4035 // See whether the bytes we need come from a contiguous part of one
4036 // operand.
4037 SmallVector<int, SystemZ::VectorBytes> OpBytes;
4038 getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes);
4039 int NewByte;
4040 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte))
4041 break;
4042 if (NewByte < 0) {
4043 addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004044 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004045 }
4046 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes);
4047 Byte = unsigned(NewByte) % SystemZ::VectorBytes;
Sanjay Patel57195842016-03-14 17:28:46 +00004048 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004049 addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004050 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004051 } else
4052 break;
4053 }
4054
4055 // Make sure that the source of the extraction is in Ops.
4056 unsigned OpNo = 0;
4057 for (; OpNo < Ops.size(); ++OpNo)
4058 if (Ops[OpNo] == Op)
4059 break;
4060 if (OpNo == Ops.size())
4061 Ops.push_back(Op);
4062
4063 // Add the element to Bytes.
4064 unsigned Base = OpNo * SystemZ::VectorBytes + Byte;
4065 for (unsigned I = 0; I < BytesPerElement; ++I)
4066 Bytes.push_back(Base + I);
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004067
4068 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004069}
4070
4071// Return SDNodes for the completed shuffle.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004072SDValue GeneralShuffle::getNode(SelectionDAG &DAG, const SDLoc &DL) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004073 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector");
4074
4075 if (Ops.size() == 0)
4076 return DAG.getUNDEF(VT);
4077
4078 // Make sure that there are at least two shuffle operands.
4079 if (Ops.size() == 1)
4080 Ops.push_back(DAG.getUNDEF(MVT::v16i8));
4081
4082 // Create a tree of shuffles, deferring root node until after the loop.
4083 // Try to redistribute the undefined elements of non-root nodes so that
4084 // the non-root shuffles match something like a pack or merge, then adjust
4085 // the parent node's permute vector to compensate for the new order.
4086 // Among other things, this copes with vectors like <2 x i16> that were
4087 // padded with undefined elements during type legalization.
4088 //
4089 // In the best case this redistribution will lead to the whole tree
4090 // using packs and merges. It should rarely be a loss in other cases.
4091 unsigned Stride = 1;
4092 for (; Stride * 2 < Ops.size(); Stride *= 2) {
4093 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) {
4094 SDValue SubOps[] = { Ops[I], Ops[I + Stride] };
4095
4096 // Create a mask for just these two operands.
4097 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes);
4098 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
4099 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes;
4100 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes;
4101 if (OpNo == I)
4102 NewBytes[J] = Byte;
4103 else if (OpNo == I + Stride)
4104 NewBytes[J] = SystemZ::VectorBytes + Byte;
4105 else
4106 NewBytes[J] = -1;
4107 }
4108 // See if it would be better to reorganize NewMask to avoid using VPERM.
4109 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes);
4110 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) {
4111 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]);
4112 // Applying NewBytesMap to Ops[I] gets back to NewBytes.
4113 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
4114 if (NewBytes[J] >= 0) {
4115 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes &&
4116 "Invalid double permute");
4117 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J];
4118 } else
4119 assert(NewBytesMap[J] < 0 && "Invalid double permute");
4120 }
4121 } else {
4122 // Just use NewBytes on the operands.
4123 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes);
4124 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J)
4125 if (NewBytes[J] >= 0)
4126 Bytes[J] = I * SystemZ::VectorBytes + J;
4127 }
4128 }
4129 }
4130
4131 // Now we just have 2 inputs. Put the second operand in Ops[1].
4132 if (Stride > 1) {
4133 Ops[1] = Ops[Stride];
4134 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
4135 if (Bytes[I] >= int(SystemZ::VectorBytes))
4136 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes;
4137 }
4138
4139 // Look for an instruction that can do the permute without resorting
4140 // to VPERM.
4141 unsigned OpNo0, OpNo1;
4142 SDValue Op;
4143 if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1))
4144 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]);
4145 else
4146 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes);
4147 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4148}
4149
Ulrich Weigandcd808232015-05-05 19:26:48 +00004150// Return true if the given BUILD_VECTOR is a scalar-to-vector conversion.
4151static bool isScalarToVector(SDValue Op) {
4152 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00004153 if (!Op.getOperand(I).isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004154 return false;
4155 return true;
4156}
4157
4158// Return a vector of type VT that contains Value in the first element.
4159// The other elements don't matter.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004160static SDValue buildScalarToVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandcd808232015-05-05 19:26:48 +00004161 SDValue Value) {
4162 // If we have a constant, replicate it to all elements and let the
4163 // BUILD_VECTOR lowering take care of it.
4164 if (Value.getOpcode() == ISD::Constant ||
4165 Value.getOpcode() == ISD::ConstantFP) {
4166 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004167 return DAG.getBuildVector(VT, DL, Ops);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004168 }
Sanjay Patel57195842016-03-14 17:28:46 +00004169 if (Value.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004170 return DAG.getUNDEF(VT);
4171 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
4172}
4173
4174// Return a vector of type VT in which Op0 is in element 0 and Op1 is in
4175// element 1. Used for cases in which replication is cheap.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004176static SDValue buildMergeScalars(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandcd808232015-05-05 19:26:48 +00004177 SDValue Op0, SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00004178 if (Op0.isUndef()) {
4179 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004180 return DAG.getUNDEF(VT);
4181 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1);
4182 }
Sanjay Patel57195842016-03-14 17:28:46 +00004183 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004184 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0);
4185 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT,
4186 buildScalarToVector(DAG, DL, VT, Op0),
4187 buildScalarToVector(DAG, DL, VT, Op1));
4188}
4189
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004190// Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64
4191// vector for them.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004192static SDValue joinDwords(SelectionDAG &DAG, const SDLoc &DL, SDValue Op0,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004193 SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00004194 if (Op0.isUndef() && Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004195 return DAG.getUNDEF(MVT::v2i64);
4196 // If one of the two inputs is undefined then replicate the other one,
4197 // in order to avoid using another register unnecessarily.
Sanjay Patel57195842016-03-14 17:28:46 +00004198 if (Op0.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004199 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
Sanjay Patel57195842016-03-14 17:28:46 +00004200 else if (Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004201 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
4202 else {
4203 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
4204 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
4205 }
4206 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1);
4207}
4208
4209// Try to represent constant BUILD_VECTOR node BVN using a
4210// SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask
4211// on success.
4212static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) {
4213 EVT ElemVT = BVN->getValueType(0).getVectorElementType();
4214 unsigned BytesPerElement = ElemVT.getStoreSize();
4215 for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) {
4216 SDValue Op = BVN->getOperand(I);
Sanjay Patel75068522016-03-14 18:09:43 +00004217 if (!Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004218 uint64_t Value;
4219 if (Op.getOpcode() == ISD::Constant)
4220 Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue();
4221 else if (Op.getOpcode() == ISD::ConstantFP)
4222 Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt()
4223 .getZExtValue());
4224 else
4225 return false;
4226 for (unsigned J = 0; J < BytesPerElement; ++J) {
4227 uint64_t Byte = (Value >> (J * 8)) & 0xff;
4228 if (Byte == 0xff)
Aaron Ballman2a3aa1f242015-05-11 12:45:53 +00004229 Mask |= 1ULL << ((E - I - 1) * BytesPerElement + J);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004230 else if (Byte != 0)
4231 return false;
4232 }
4233 }
4234 }
4235 return true;
4236}
4237
4238// Try to load a vector constant in which BitsPerElement-bit value Value
4239// is replicated to fill the vector. VT is the type of the resulting
4240// constant, which may have elements of a different size from BitsPerElement.
4241// Return the SDValue of the constant on success, otherwise return
4242// an empty value.
4243static SDValue tryBuildVectorReplicate(SelectionDAG &DAG,
4244 const SystemZInstrInfo *TII,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004245 const SDLoc &DL, EVT VT, uint64_t Value,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004246 unsigned BitsPerElement) {
4247 // Signed 16-bit values can be replicated using VREPI.
4248 int64_t SignedValue = SignExtend64(Value, BitsPerElement);
4249 if (isInt<16>(SignedValue)) {
4250 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
4251 SystemZ::VectorBits / BitsPerElement);
4252 SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT,
4253 DAG.getConstant(SignedValue, DL, MVT::i32));
4254 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4255 }
4256 // See whether rotating the constant left some N places gives a value that
4257 // is one less than a power of 2 (i.e. all zeros followed by all ones).
4258 // If so we can use VGM.
4259 unsigned Start, End;
4260 if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) {
4261 // isRxSBGMask returns the bit numbers for a full 64-bit value,
4262 // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to
4263 // bit numbers for an BitsPerElement value, so that 0 denotes
4264 // 1 << (BitsPerElement-1).
4265 Start -= 64 - BitsPerElement;
4266 End -= 64 - BitsPerElement;
4267 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
4268 SystemZ::VectorBits / BitsPerElement);
4269 SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT,
4270 DAG.getConstant(Start, DL, MVT::i32),
4271 DAG.getConstant(End, DL, MVT::i32));
4272 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4273 }
4274 return SDValue();
4275}
4276
4277// If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually
4278// better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for
4279// the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR
4280// would benefit from this representation and return it if so.
4281static SDValue tryBuildVectorShuffle(SelectionDAG &DAG,
4282 BuildVectorSDNode *BVN) {
4283 EVT VT = BVN->getValueType(0);
4284 unsigned NumElements = VT.getVectorNumElements();
4285
4286 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation
4287 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still
4288 // need a BUILD_VECTOR, add an additional placeholder operand for that
4289 // BUILD_VECTOR and store its operands in ResidueOps.
4290 GeneralShuffle GS(VT);
4291 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps;
4292 bool FoundOne = false;
4293 for (unsigned I = 0; I < NumElements; ++I) {
4294 SDValue Op = BVN->getOperand(I);
4295 if (Op.getOpcode() == ISD::TRUNCATE)
4296 Op = Op.getOperand(0);
4297 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4298 Op.getOperand(1).getOpcode() == ISD::Constant) {
4299 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004300 if (!GS.add(Op.getOperand(0), Elem))
4301 return SDValue();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004302 FoundOne = true;
Sanjay Patel57195842016-03-14 17:28:46 +00004303 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004304 GS.addUndef();
4305 } else {
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004306 if (!GS.add(SDValue(), ResidueOps.size()))
4307 return SDValue();
Ulrich Weigande861e642015-09-15 14:27:46 +00004308 ResidueOps.push_back(BVN->getOperand(I));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004309 }
4310 }
4311
4312 // Nothing to do if there are no EXTRACT_VECTOR_ELTs.
4313 if (!FoundOne)
4314 return SDValue();
4315
4316 // Create the BUILD_VECTOR for the remaining elements, if any.
4317 if (!ResidueOps.empty()) {
4318 while (ResidueOps.size() < NumElements)
Ulrich Weigandf4d14f72015-10-08 17:46:59 +00004319 ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType()));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004320 for (auto &Op : GS.Ops) {
4321 if (!Op.getNode()) {
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004322 Op = DAG.getBuildVector(VT, SDLoc(BVN), ResidueOps);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004323 break;
4324 }
4325 }
4326 }
4327 return GS.getNode(DAG, SDLoc(BVN));
4328}
4329
4330// Combine GPR scalar values Elems into a vector of type VT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004331static SDValue buildVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004332 SmallVectorImpl<SDValue> &Elems) {
4333 // See whether there is a single replicated value.
4334 SDValue Single;
4335 unsigned int NumElements = Elems.size();
4336 unsigned int Count = 0;
4337 for (auto Elem : Elems) {
Sanjay Patel75068522016-03-14 18:09:43 +00004338 if (!Elem.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004339 if (!Single.getNode())
4340 Single = Elem;
4341 else if (Elem != Single) {
4342 Single = SDValue();
4343 break;
4344 }
4345 Count += 1;
4346 }
4347 }
4348 // There are three cases here:
4349 //
4350 // - if the only defined element is a loaded one, the best sequence
4351 // is a replicating load.
4352 //
4353 // - otherwise, if the only defined element is an i64 value, we will
4354 // end up with the same VLVGP sequence regardless of whether we short-cut
4355 // for replication or fall through to the later code.
4356 //
4357 // - otherwise, if the only defined element is an i32 or smaller value,
4358 // we would need 2 instructions to replicate it: VLVGP followed by VREPx.
4359 // This is only a win if the single defined element is used more than once.
4360 // In other cases we're better off using a single VLVGx.
4361 if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD))
4362 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single);
4363
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004364 // If all elements are loads, use VLREP/VLEs (below).
4365 bool AllLoads = true;
4366 for (auto Elem : Elems)
4367 if (Elem.getOpcode() != ISD::LOAD || cast<LoadSDNode>(Elem)->isIndexed()) {
4368 AllLoads = false;
4369 break;
4370 }
4371
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004372 // The best way of building a v2i64 from two i64s is to use VLVGP.
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004373 if (VT == MVT::v2i64 && !AllLoads)
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004374 return joinDwords(DAG, DL, Elems[0], Elems[1]);
4375
Ulrich Weigandcd808232015-05-05 19:26:48 +00004376 // Use a 64-bit merge high to combine two doubles.
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004377 if (VT == MVT::v2f64 && !AllLoads)
Ulrich Weigandcd808232015-05-05 19:26:48 +00004378 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4379
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004380 // Build v4f32 values directly from the FPRs:
4381 //
4382 // <Axxx> <Bxxx> <Cxxxx> <Dxxx>
4383 // V V VMRHF
4384 // <ABxx> <CDxx>
4385 // V VMRHG
4386 // <ABCD>
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004387 if (VT == MVT::v4f32 && !AllLoads) {
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004388 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4389 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]);
4390 // Avoid unnecessary undefs by reusing the other operand.
Sanjay Patel57195842016-03-14 17:28:46 +00004391 if (Op01.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004392 Op01 = Op23;
Sanjay Patel57195842016-03-14 17:28:46 +00004393 else if (Op23.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004394 Op23 = Op01;
4395 // Merging identical replications is a no-op.
4396 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23)
4397 return Op01;
4398 Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01);
4399 Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23);
4400 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH,
4401 DL, MVT::v2i64, Op01, Op23);
4402 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4403 }
4404
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004405 // Collect the constant terms.
4406 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue());
4407 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false);
4408
4409 unsigned NumConstants = 0;
4410 for (unsigned I = 0; I < NumElements; ++I) {
4411 SDValue Elem = Elems[I];
4412 if (Elem.getOpcode() == ISD::Constant ||
4413 Elem.getOpcode() == ISD::ConstantFP) {
4414 NumConstants += 1;
4415 Constants[I] = Elem;
4416 Done[I] = true;
4417 }
4418 }
4419 // If there was at least one constant, fill in the other elements of
4420 // Constants with undefs to get a full vector constant and use that
4421 // as the starting point.
4422 SDValue Result;
4423 if (NumConstants > 0) {
4424 for (unsigned I = 0; I < NumElements; ++I)
4425 if (!Constants[I].getNode())
4426 Constants[I] = DAG.getUNDEF(Elems[I].getValueType());
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004427 Result = DAG.getBuildVector(VT, DL, Constants);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004428 } else {
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004429 // Otherwise try to use VLREP or VLVGP to start the sequence in order to
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004430 // avoid a false dependency on any previous contents of the vector
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004431 // register.
4432
4433 // Use a VLREP if at least one element is a load.
4434 unsigned LoadElIdx = UINT_MAX;
4435 for (unsigned I = 0; I < NumElements; ++I)
4436 if (Elems[I].getOpcode() == ISD::LOAD &&
4437 cast<LoadSDNode>(Elems[I])->isUnindexed()) {
4438 LoadElIdx = I;
4439 break;
4440 }
4441 if (LoadElIdx != UINT_MAX) {
4442 Result = DAG.getNode(SystemZISD::REPLICATE, DL, VT, Elems[LoadElIdx]);
4443 Done[LoadElIdx] = true;
4444 } else {
4445 // Try to use VLVGP.
4446 unsigned I1 = NumElements / 2 - 1;
4447 unsigned I2 = NumElements - 1;
4448 bool Def1 = !Elems[I1].isUndef();
4449 bool Def2 = !Elems[I2].isUndef();
4450 if (Def1 || Def2) {
4451 SDValue Elem1 = Elems[Def1 ? I1 : I2];
4452 SDValue Elem2 = Elems[Def2 ? I2 : I1];
4453 Result = DAG.getNode(ISD::BITCAST, DL, VT,
4454 joinDwords(DAG, DL, Elem1, Elem2));
4455 Done[I1] = true;
4456 Done[I2] = true;
4457 } else
4458 Result = DAG.getUNDEF(VT);
4459 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004460 }
4461
4462 // Use VLVGx to insert the other elements.
4463 for (unsigned I = 0; I < NumElements; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00004464 if (!Done[I] && !Elems[I].isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004465 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I],
4466 DAG.getConstant(I, DL, MVT::i32));
4467 return Result;
4468}
4469
4470SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op,
4471 SelectionDAG &DAG) const {
4472 const SystemZInstrInfo *TII =
4473 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
4474 auto *BVN = cast<BuildVectorSDNode>(Op.getNode());
4475 SDLoc DL(Op);
4476 EVT VT = Op.getValueType();
4477
4478 if (BVN->isConstant()) {
4479 // Try using VECTOR GENERATE BYTE MASK. This is the architecturally-
4480 // preferred way of creating all-zero and all-one vectors so give it
4481 // priority over other methods below.
4482 uint64_t Mask = 0;
4483 if (tryBuildVectorByteMask(BVN, Mask)) {
4484 SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
4485 DAG.getConstant(Mask, DL, MVT::i32));
4486 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4487 }
4488
4489 // Try using some form of replication.
4490 APInt SplatBits, SplatUndef;
4491 unsigned SplatBitSize;
4492 bool HasAnyUndefs;
4493 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4494 8, true) &&
4495 SplatBitSize <= 64) {
4496 // First try assuming that any undefined bits above the highest set bit
4497 // and below the lowest set bit are 1s. This increases the likelihood of
4498 // being able to use a sign-extended element value in VECTOR REPLICATE
4499 // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK.
4500 uint64_t SplatBitsZ = SplatBits.getZExtValue();
4501 uint64_t SplatUndefZ = SplatUndef.getZExtValue();
4502 uint64_t Lower = (SplatUndefZ
4503 & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1));
4504 uint64_t Upper = (SplatUndefZ
4505 & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1));
4506 uint64_t Value = SplatBitsZ | Upper | Lower;
4507 SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value,
4508 SplatBitSize);
4509 if (Op.getNode())
4510 return Op;
4511
4512 // Now try assuming that any undefined bits between the first and
4513 // last defined set bits are set. This increases the chances of
4514 // using a non-wraparound mask.
4515 uint64_t Middle = SplatUndefZ & ~Upper & ~Lower;
4516 Value = SplatBitsZ | Middle;
4517 Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize);
4518 if (Op.getNode())
4519 return Op;
4520 }
4521
4522 // Fall back to loading it from memory.
4523 return SDValue();
4524 }
4525
4526 // See if we should use shuffles to construct the vector from other vectors.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004527 if (SDValue Res = tryBuildVectorShuffle(DAG, BVN))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004528 return Res;
4529
Ulrich Weigandcd808232015-05-05 19:26:48 +00004530 // Detect SCALAR_TO_VECTOR conversions.
4531 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op))
4532 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0));
4533
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004534 // Otherwise use buildVector to build the vector up from GPRs.
4535 unsigned NumElements = Op.getNumOperands();
4536 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements);
4537 for (unsigned I = 0; I < NumElements; ++I)
4538 Ops[I] = Op.getOperand(I);
4539 return buildVector(DAG, DL, VT, Ops);
4540}
4541
4542SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
4543 SelectionDAG &DAG) const {
4544 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode());
4545 SDLoc DL(Op);
4546 EVT VT = Op.getValueType();
4547 unsigned NumElements = VT.getVectorNumElements();
4548
4549 if (VSN->isSplat()) {
4550 SDValue Op0 = Op.getOperand(0);
4551 unsigned Index = VSN->getSplatIndex();
4552 assert(Index < VT.getVectorNumElements() &&
4553 "Splat index should be defined and in first operand");
4554 // See whether the value we're splatting is directly available as a scalar.
4555 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4556 Op0.getOpcode() == ISD::BUILD_VECTOR)
4557 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index));
4558 // Otherwise keep it as a vector-to-vector operation.
4559 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0),
4560 DAG.getConstant(Index, DL, MVT::i32));
4561 }
4562
4563 GeneralShuffle GS(VT);
4564 for (unsigned I = 0; I < NumElements; ++I) {
4565 int Elt = VSN->getMaskElt(I);
4566 if (Elt < 0)
4567 GS.addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004568 else if (!GS.add(Op.getOperand(unsigned(Elt) / NumElements),
4569 unsigned(Elt) % NumElements))
4570 return SDValue();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004571 }
4572 return GS.getNode(DAG, SDLoc(VSN));
4573}
4574
4575SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
4576 SelectionDAG &DAG) const {
4577 SDLoc DL(Op);
4578 // Just insert the scalar into element 0 of an undefined vector.
4579 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL,
4580 Op.getValueType(), DAG.getUNDEF(Op.getValueType()),
4581 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32));
4582}
4583
Ulrich Weigandcd808232015-05-05 19:26:48 +00004584SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
4585 SelectionDAG &DAG) const {
4586 // Handle insertions of floating-point values.
4587 SDLoc DL(Op);
4588 SDValue Op0 = Op.getOperand(0);
4589 SDValue Op1 = Op.getOperand(1);
4590 SDValue Op2 = Op.getOperand(2);
4591 EVT VT = Op.getValueType();
4592
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004593 // Insertions into constant indices of a v2f64 can be done using VPDI.
4594 // However, if the inserted value is a bitcast or a constant then it's
4595 // better to use GPRs, as below.
4596 if (VT == MVT::v2f64 &&
4597 Op1.getOpcode() != ISD::BITCAST &&
Ulrich Weigandcd808232015-05-05 19:26:48 +00004598 Op1.getOpcode() != ISD::ConstantFP &&
4599 Op2.getOpcode() == ISD::Constant) {
4600 uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue();
4601 unsigned Mask = VT.getVectorNumElements() - 1;
4602 if (Index <= Mask)
4603 return Op;
4604 }
4605
4606 // Otherwise bitcast to the equivalent integer form and insert via a GPR.
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004607 MVT IntVT = MVT::getIntegerVT(VT.getScalarSizeInBits());
Ulrich Weigandcd808232015-05-05 19:26:48 +00004608 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements());
4609 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT,
4610 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0),
4611 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2);
4612 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4613}
4614
4615SDValue
4616SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
4617 SelectionDAG &DAG) const {
4618 // Handle extractions of floating-point values.
4619 SDLoc DL(Op);
4620 SDValue Op0 = Op.getOperand(0);
4621 SDValue Op1 = Op.getOperand(1);
4622 EVT VT = Op.getValueType();
4623 EVT VecVT = Op0.getValueType();
4624
4625 // Extractions of constant indices can be done directly.
4626 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) {
4627 uint64_t Index = CIndexN->getZExtValue();
4628 unsigned Mask = VecVT.getVectorNumElements() - 1;
4629 if (Index <= Mask)
4630 return Op;
4631 }
4632
4633 // Otherwise bitcast to the equivalent integer form and extract via a GPR.
4634 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
4635 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements());
4636 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT,
4637 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1);
4638 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4639}
4640
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004641SDValue
4642SystemZTargetLowering::lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004643 unsigned UnpackHigh) const {
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004644 SDValue PackedOp = Op.getOperand(0);
4645 EVT OutVT = Op.getValueType();
4646 EVT InVT = PackedOp.getValueType();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004647 unsigned ToBits = OutVT.getScalarSizeInBits();
4648 unsigned FromBits = InVT.getScalarSizeInBits();
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004649 do {
4650 FromBits *= 2;
4651 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits),
4652 SystemZ::VectorBits / FromBits);
4653 PackedOp = DAG.getNode(UnpackHigh, SDLoc(PackedOp), OutVT, PackedOp);
4654 } while (FromBits != ToBits);
4655 return PackedOp;
4656}
4657
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004658SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG,
4659 unsigned ByScalar) const {
4660 // Look for cases where a vector shift can use the *_BY_SCALAR form.
4661 SDValue Op0 = Op.getOperand(0);
4662 SDValue Op1 = Op.getOperand(1);
4663 SDLoc DL(Op);
4664 EVT VT = Op.getValueType();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004665 unsigned ElemBitSize = VT.getScalarSizeInBits();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004666
4667 // See whether the shift vector is a splat represented as BUILD_VECTOR.
4668 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) {
4669 APInt SplatBits, SplatUndef;
4670 unsigned SplatBitSize;
4671 bool HasAnyUndefs;
4672 // Check for constant splats. Use ElemBitSize as the minimum element
4673 // width and reject splats that need wider elements.
4674 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4675 ElemBitSize, true) &&
4676 SplatBitSize == ElemBitSize) {
4677 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff,
4678 DL, MVT::i32);
4679 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4680 }
4681 // Check for variable splats.
4682 BitVector UndefElements;
4683 SDValue Splat = BVN->getSplatValue(&UndefElements);
4684 if (Splat) {
4685 // Since i32 is the smallest legal type, we either need a no-op
4686 // or a truncation.
4687 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat);
4688 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4689 }
4690 }
4691
4692 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR,
4693 // and the shift amount is directly available in a GPR.
4694 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) {
4695 if (VSN->isSplat()) {
4696 SDValue VSNOp0 = VSN->getOperand(0);
4697 unsigned Index = VSN->getSplatIndex();
4698 assert(Index < VT.getVectorNumElements() &&
4699 "Splat index should be defined and in first operand");
4700 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4701 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) {
4702 // Since i32 is the smallest legal type, we either need a no-op
4703 // or a truncation.
4704 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32,
4705 VSNOp0.getOperand(Index));
4706 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4707 }
4708 }
4709 }
4710
4711 // Otherwise just treat the current form as legal.
4712 return Op;
4713}
4714
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004715SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
4716 SelectionDAG &DAG) const {
4717 switch (Op.getOpcode()) {
Ulrich Weigandf557d082016-04-04 12:44:55 +00004718 case ISD::FRAMEADDR:
4719 return lowerFRAMEADDR(Op, DAG);
4720 case ISD::RETURNADDR:
4721 return lowerRETURNADDR(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004722 case ISD::BR_CC:
4723 return lowerBR_CC(Op, DAG);
4724 case ISD::SELECT_CC:
4725 return lowerSELECT_CC(Op, DAG);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00004726 case ISD::SETCC:
4727 return lowerSETCC(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004728 case ISD::GlobalAddress:
4729 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
4730 case ISD::GlobalTLSAddress:
4731 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
4732 case ISD::BlockAddress:
4733 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
4734 case ISD::JumpTable:
4735 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
4736 case ISD::ConstantPool:
4737 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
4738 case ISD::BITCAST:
4739 return lowerBITCAST(Op, DAG);
4740 case ISD::VASTART:
4741 return lowerVASTART(Op, DAG);
4742 case ISD::VACOPY:
4743 return lowerVACOPY(Op, DAG);
4744 case ISD::DYNAMIC_STACKALLOC:
4745 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +00004746 case ISD::GET_DYNAMIC_AREA_OFFSET:
4747 return lowerGET_DYNAMIC_AREA_OFFSET(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00004748 case ISD::SMUL_LOHI:
4749 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004750 case ISD::UMUL_LOHI:
4751 return lowerUMUL_LOHI(Op, DAG);
4752 case ISD::SDIVREM:
4753 return lowerSDIVREM(Op, DAG);
4754 case ISD::UDIVREM:
4755 return lowerUDIVREM(Op, DAG);
4756 case ISD::OR:
4757 return lowerOR(Op, DAG);
Ulrich Weigandb4012182015-03-31 12:56:33 +00004758 case ISD::CTPOP:
4759 return lowerCTPOP(Op, DAG);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004760 case ISD::ATOMIC_FENCE:
4761 return lowerATOMIC_FENCE(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004762 case ISD::ATOMIC_SWAP:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004763 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
4764 case ISD::ATOMIC_STORE:
4765 return lowerATOMIC_STORE(Op, DAG);
4766 case ISD::ATOMIC_LOAD:
4767 return lowerATOMIC_LOAD(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004768 case ISD::ATOMIC_LOAD_ADD:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004769 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004770 case ISD::ATOMIC_LOAD_SUB:
Richard Sandiford41350a52013-12-24 15:18:04 +00004771 return lowerATOMIC_LOAD_SUB(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004772 case ISD::ATOMIC_LOAD_AND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004773 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004774 case ISD::ATOMIC_LOAD_OR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004775 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004776 case ISD::ATOMIC_LOAD_XOR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004777 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004778 case ISD::ATOMIC_LOAD_NAND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004779 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004780 case ISD::ATOMIC_LOAD_MIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004781 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004782 case ISD::ATOMIC_LOAD_MAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004783 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004784 case ISD::ATOMIC_LOAD_UMIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004785 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004786 case ISD::ATOMIC_LOAD_UMAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004787 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
Ulrich Weigand0f1de042017-09-28 16:22:54 +00004788 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004789 return lowerATOMIC_CMP_SWAP(Op, DAG);
4790 case ISD::STACKSAVE:
4791 return lowerSTACKSAVE(Op, DAG);
4792 case ISD::STACKRESTORE:
4793 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00004794 case ISD::PREFETCH:
4795 return lowerPREFETCH(Op, DAG);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004796 case ISD::INTRINSIC_W_CHAIN:
4797 return lowerINTRINSIC_W_CHAIN(Op, DAG);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004798 case ISD::INTRINSIC_WO_CHAIN:
4799 return lowerINTRINSIC_WO_CHAIN(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004800 case ISD::BUILD_VECTOR:
4801 return lowerBUILD_VECTOR(Op, DAG);
4802 case ISD::VECTOR_SHUFFLE:
4803 return lowerVECTOR_SHUFFLE(Op, DAG);
4804 case ISD::SCALAR_TO_VECTOR:
4805 return lowerSCALAR_TO_VECTOR(Op, DAG);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004806 case ISD::INSERT_VECTOR_ELT:
4807 return lowerINSERT_VECTOR_ELT(Op, DAG);
4808 case ISD::EXTRACT_VECTOR_ELT:
4809 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004810 case ISD::SIGN_EXTEND_VECTOR_INREG:
4811 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACK_HIGH);
4812 case ISD::ZERO_EXTEND_VECTOR_INREG:
4813 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACKL_HIGH);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004814 case ISD::SHL:
4815 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR);
4816 case ISD::SRL:
4817 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR);
4818 case ISD::SRA:
4819 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004820 default:
4821 llvm_unreachable("Unexpected node to lower");
4822 }
4823}
4824
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004825// Lower operations with invalid operand or result types (currently used
4826// only for 128-bit integer types).
4827
4828static SDValue lowerI128ToGR128(SelectionDAG &DAG, SDValue In) {
4829 SDLoc DL(In);
4830 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, In,
4831 DAG.getIntPtrConstant(0, DL));
4832 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, In,
4833 DAG.getIntPtrConstant(1, DL));
4834 SDNode *Pair = DAG.getMachineNode(SystemZ::PAIR128, DL,
4835 MVT::Untyped, Hi, Lo);
4836 return SDValue(Pair, 0);
4837}
4838
4839static SDValue lowerGR128ToI128(SelectionDAG &DAG, SDValue In) {
4840 SDLoc DL(In);
4841 SDValue Hi = DAG.getTargetExtractSubreg(SystemZ::subreg_h64,
4842 DL, MVT::i64, In);
4843 SDValue Lo = DAG.getTargetExtractSubreg(SystemZ::subreg_l64,
4844 DL, MVT::i64, In);
4845 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i128, Lo, Hi);
4846}
4847
4848void
4849SystemZTargetLowering::LowerOperationWrapper(SDNode *N,
4850 SmallVectorImpl<SDValue> &Results,
4851 SelectionDAG &DAG) const {
4852 switch (N->getOpcode()) {
4853 case ISD::ATOMIC_LOAD: {
4854 SDLoc DL(N);
4855 SDVTList Tys = DAG.getVTList(MVT::Untyped, MVT::Other);
4856 SDValue Ops[] = { N->getOperand(0), N->getOperand(1) };
4857 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
4858 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_LOAD_128,
4859 DL, Tys, Ops, MVT::i128, MMO);
4860 Results.push_back(lowerGR128ToI128(DAG, Res));
4861 Results.push_back(Res.getValue(1));
4862 break;
4863 }
4864 case ISD::ATOMIC_STORE: {
4865 SDLoc DL(N);
4866 SDVTList Tys = DAG.getVTList(MVT::Other);
4867 SDValue Ops[] = { N->getOperand(0),
4868 lowerI128ToGR128(DAG, N->getOperand(2)),
4869 N->getOperand(1) };
4870 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
4871 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_STORE_128,
4872 DL, Tys, Ops, MVT::i128, MMO);
4873 // We have to enforce sequential consistency by performing a
4874 // serialization operation after the store.
4875 if (cast<AtomicSDNode>(N)->getOrdering() ==
4876 AtomicOrdering::SequentiallyConsistent)
4877 Res = SDValue(DAG.getMachineNode(SystemZ::Serialize, DL,
4878 MVT::Other, Res), 0);
4879 Results.push_back(Res);
4880 break;
4881 }
Ulrich Weigand0f1de042017-09-28 16:22:54 +00004882 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004883 SDLoc DL(N);
Ulrich Weigand0f1de042017-09-28 16:22:54 +00004884 SDVTList Tys = DAG.getVTList(MVT::Untyped, MVT::Other, MVT::Glue);
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004885 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
4886 lowerI128ToGR128(DAG, N->getOperand(2)),
4887 lowerI128ToGR128(DAG, N->getOperand(3)) };
4888 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
4889 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAP_128,
4890 DL, Tys, Ops, MVT::i128, MMO);
Ulrich Weigand0f1de042017-09-28 16:22:54 +00004891 SDValue Success = emitSETCC(DAG, DL, Res.getValue(2),
4892 SystemZ::CCMASK_CS, SystemZ::CCMASK_CS_EQ);
4893 Success = DAG.getZExtOrTrunc(Success, DL, N->getValueType(1));
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004894 Results.push_back(lowerGR128ToI128(DAG, Res));
Ulrich Weigand0f1de042017-09-28 16:22:54 +00004895 Results.push_back(Success);
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004896 Results.push_back(Res.getValue(1));
4897 break;
4898 }
4899 default:
4900 llvm_unreachable("Unexpected node to lower");
4901 }
4902}
4903
4904void
4905SystemZTargetLowering::ReplaceNodeResults(SDNode *N,
4906 SmallVectorImpl<SDValue> &Results,
4907 SelectionDAG &DAG) const {
4908 return LowerOperationWrapper(N, Results, DAG);
4909}
4910
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004911const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
4912#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
Matthias Braund04893f2015-05-07 21:33:59 +00004913 switch ((SystemZISD::NodeType)Opcode) {
4914 case SystemZISD::FIRST_NUMBER: break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004915 OPCODE(RET_FLAG);
4916 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00004917 OPCODE(SIBCALL);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004918 OPCODE(TLS_GDCALL);
4919 OPCODE(TLS_LDCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004920 OPCODE(PCREL_WRAPPER);
Richard Sandiford54b36912013-09-27 15:14:04 +00004921 OPCODE(PCREL_OFFSET);
Richard Sandiford57485472013-12-13 15:35:00 +00004922 OPCODE(IABS);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00004923 OPCODE(ICMP);
4924 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00004925 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004926 OPCODE(BR_CCMASK);
4927 OPCODE(SELECT_CCMASK);
4928 OPCODE(ADJDYNALLOC);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004929 OPCODE(POPCNT);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +00004930 OPCODE(SMUL_LOHI);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00004931 OPCODE(UMUL_LOHI);
4932 OPCODE(SDIVREM);
4933 OPCODE(UDIVREM);
Richard Sandifordd131ff82013-07-08 09:35:23 +00004934 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004935 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00004936 OPCODE(NC);
4937 OPCODE(NC_LOOP);
4938 OPCODE(OC);
4939 OPCODE(OC_LOOP);
4940 OPCODE(XC);
4941 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00004942 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004943 OPCODE(CLC_LOOP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00004944 OPCODE(STPCPY);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004945 OPCODE(STRCMP);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00004946 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00004947 OPCODE(IPM);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004948 OPCODE(MEMBARRIER);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004949 OPCODE(TBEGIN);
4950 OPCODE(TBEGIN_NOFLOAT);
4951 OPCODE(TEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004952 OPCODE(BYTE_MASK);
4953 OPCODE(ROTATE_MASK);
4954 OPCODE(REPLICATE);
4955 OPCODE(JOIN_DWORDS);
4956 OPCODE(SPLAT);
4957 OPCODE(MERGE_HIGH);
4958 OPCODE(MERGE_LOW);
4959 OPCODE(SHL_DOUBLE);
4960 OPCODE(PERMUTE_DWORDS);
4961 OPCODE(PERMUTE);
4962 OPCODE(PACK);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004963 OPCODE(PACKS_CC);
4964 OPCODE(PACKLS_CC);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004965 OPCODE(UNPACK_HIGH);
4966 OPCODE(UNPACKL_HIGH);
4967 OPCODE(UNPACK_LOW);
4968 OPCODE(UNPACKL_LOW);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004969 OPCODE(VSHL_BY_SCALAR);
4970 OPCODE(VSRL_BY_SCALAR);
4971 OPCODE(VSRA_BY_SCALAR);
4972 OPCODE(VSUM);
4973 OPCODE(VICMPE);
4974 OPCODE(VICMPH);
4975 OPCODE(VICMPHL);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004976 OPCODE(VICMPES);
4977 OPCODE(VICMPHS);
4978 OPCODE(VICMPHLS);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004979 OPCODE(VFCMPE);
4980 OPCODE(VFCMPH);
4981 OPCODE(VFCMPHE);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004982 OPCODE(VFCMPES);
4983 OPCODE(VFCMPHS);
4984 OPCODE(VFCMPHES);
4985 OPCODE(VFTCI);
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004986 OPCODE(VEXTEND);
4987 OPCODE(VROUND);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004988 OPCODE(VTM);
4989 OPCODE(VFAE_CC);
4990 OPCODE(VFAEZ_CC);
4991 OPCODE(VFEE_CC);
4992 OPCODE(VFEEZ_CC);
4993 OPCODE(VFENE_CC);
4994 OPCODE(VFENEZ_CC);
4995 OPCODE(VISTR_CC);
4996 OPCODE(VSTRC_CC);
4997 OPCODE(VSTRCZ_CC);
Marcin Koscielnicki32e87342016-07-02 02:20:40 +00004998 OPCODE(TDC);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004999 OPCODE(ATOMIC_SWAPW);
5000 OPCODE(ATOMIC_LOADW_ADD);
5001 OPCODE(ATOMIC_LOADW_SUB);
5002 OPCODE(ATOMIC_LOADW_AND);
5003 OPCODE(ATOMIC_LOADW_OR);
5004 OPCODE(ATOMIC_LOADW_XOR);
5005 OPCODE(ATOMIC_LOADW_NAND);
5006 OPCODE(ATOMIC_LOADW_MIN);
5007 OPCODE(ATOMIC_LOADW_MAX);
5008 OPCODE(ATOMIC_LOADW_UMIN);
5009 OPCODE(ATOMIC_LOADW_UMAX);
5010 OPCODE(ATOMIC_CMP_SWAPW);
Ulrich Weigand0f1de042017-09-28 16:22:54 +00005011 OPCODE(ATOMIC_CMP_SWAP);
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00005012 OPCODE(ATOMIC_LOAD_128);
5013 OPCODE(ATOMIC_STORE_128);
5014 OPCODE(ATOMIC_CMP_SWAP_128);
Bryan Chan28b759c2016-05-16 20:32:22 +00005015 OPCODE(LRV);
5016 OPCODE(STRV);
Richard Sandiford03481332013-08-23 11:36:42 +00005017 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005018 }
Craig Topper062a2ba2014-04-25 05:30:21 +00005019 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005020#undef OPCODE
5021}
5022
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005023// Return true if VT is a vector whose elements are a whole number of bytes
Jonas Paulssoncad72ef2017-04-07 12:35:11 +00005024// in width. Also check for presence of vector support.
5025bool SystemZTargetLowering::canTreatAsByteVector(EVT VT) const {
5026 if (!Subtarget.hasVector())
5027 return false;
5028
Jonas Paulsson1d33cd32017-03-07 09:49:31 +00005029 return VT.isVector() && VT.getScalarSizeInBits() % 8 == 0 && VT.isSimple();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005030}
5031
5032// Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT
5033// producing a result of type ResVT. Op is a possibly bitcast version
5034// of the input vector and Index is the index (based on type VecVT) that
5035// should be extracted. Return the new extraction if a simplification
5036// was possible or if Force is true.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005037SDValue SystemZTargetLowering::combineExtract(const SDLoc &DL, EVT ResVT,
5038 EVT VecVT, SDValue Op,
5039 unsigned Index,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005040 DAGCombinerInfo &DCI,
5041 bool Force) const {
5042 SelectionDAG &DAG = DCI.DAG;
5043
5044 // The number of bytes being extracted.
5045 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
5046
5047 for (;;) {
5048 unsigned Opcode = Op.getOpcode();
5049 if (Opcode == ISD::BITCAST)
5050 // Look through bitcasts.
5051 Op = Op.getOperand(0);
5052 else if (Opcode == ISD::VECTOR_SHUFFLE &&
5053 canTreatAsByteVector(Op.getValueType())) {
5054 // Get a VPERM-like permute mask and see whether the bytes covered
5055 // by the extracted element are a contiguous sequence from one
5056 // source operand.
5057 SmallVector<int, SystemZ::VectorBytes> Bytes;
5058 getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes);
5059 int First;
5060 if (!getShuffleInput(Bytes, Index * BytesPerElement,
5061 BytesPerElement, First))
5062 break;
5063 if (First < 0)
5064 return DAG.getUNDEF(ResVT);
5065 // Make sure the contiguous sequence starts at a multiple of the
5066 // original element size.
5067 unsigned Byte = unsigned(First) % Bytes.size();
5068 if (Byte % BytesPerElement != 0)
5069 break;
5070 // We can get the extracted value directly from an input.
5071 Index = Byte / BytesPerElement;
5072 Op = Op.getOperand(unsigned(First) / Bytes.size());
5073 Force = true;
5074 } else if (Opcode == ISD::BUILD_VECTOR &&
5075 canTreatAsByteVector(Op.getValueType())) {
5076 // We can only optimize this case if the BUILD_VECTOR elements are
5077 // at least as wide as the extracted value.
5078 EVT OpVT = Op.getValueType();
5079 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
5080 if (OpBytesPerElement < BytesPerElement)
5081 break;
5082 // Make sure that the least-significant bit of the extracted value
5083 // is the least significant bit of an input.
5084 unsigned End = (Index + 1) * BytesPerElement;
5085 if (End % OpBytesPerElement != 0)
5086 break;
5087 // We're extracting the low part of one operand of the BUILD_VECTOR.
5088 Op = Op.getOperand(End / OpBytesPerElement - 1);
5089 if (!Op.getValueType().isInteger()) {
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00005090 EVT VT = MVT::getIntegerVT(Op.getValueSizeInBits());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005091 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
5092 DCI.AddToWorklist(Op.getNode());
5093 }
5094 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits());
5095 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
5096 if (VT != ResVT) {
5097 DCI.AddToWorklist(Op.getNode());
5098 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op);
5099 }
5100 return Op;
5101 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00005102 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG ||
5103 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) &&
5104 canTreatAsByteVector(Op.getValueType()) &&
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005105 canTreatAsByteVector(Op.getOperand(0).getValueType())) {
5106 // Make sure that only the unextended bits are significant.
5107 EVT ExtVT = Op.getValueType();
5108 EVT OpVT = Op.getOperand(0).getValueType();
5109 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize();
5110 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
5111 unsigned Byte = Index * BytesPerElement;
5112 unsigned SubByte = Byte % ExtBytesPerElement;
5113 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement;
5114 if (SubByte < MinSubByte ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00005115 SubByte + BytesPerElement > ExtBytesPerElement)
5116 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005117 // Get the byte offset of the unextended element
5118 Byte = Byte / ExtBytesPerElement * OpBytesPerElement;
5119 // ...then add the byte offset relative to that element.
5120 Byte += SubByte - MinSubByte;
5121 if (Byte % BytesPerElement != 0)
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00005122 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005123 Op = Op.getOperand(0);
5124 Index = Byte / BytesPerElement;
5125 Force = true;
5126 } else
5127 break;
5128 }
5129 if (Force) {
5130 if (Op.getValueType() != VecVT) {
5131 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op);
5132 DCI.AddToWorklist(Op.getNode());
5133 }
5134 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op,
5135 DAG.getConstant(Index, DL, MVT::i32));
5136 }
5137 return SDValue();
5138}
5139
5140// Optimize vector operations in scalar value Op on the basis that Op
5141// is truncated to TruncVT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005142SDValue SystemZTargetLowering::combineTruncateExtract(
5143 const SDLoc &DL, EVT TruncVT, SDValue Op, DAGCombinerInfo &DCI) const {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005144 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into
5145 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements
5146 // of type TruncVT.
5147 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5148 TruncVT.getSizeInBits() % 8 == 0) {
5149 SDValue Vec = Op.getOperand(0);
5150 EVT VecVT = Vec.getValueType();
5151 if (canTreatAsByteVector(VecVT)) {
5152 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
5153 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
5154 unsigned TruncBytes = TruncVT.getStoreSize();
5155 if (BytesPerElement % TruncBytes == 0) {
5156 // Calculate the value of Y' in the above description. We are
5157 // splitting the original elements into Scale equal-sized pieces
5158 // and for truncation purposes want the last (least-significant)
5159 // of these pieces for IndexN. This is easiest to do by calculating
5160 // the start index of the following element and then subtracting 1.
5161 unsigned Scale = BytesPerElement / TruncBytes;
5162 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1;
5163
5164 // Defer the creation of the bitcast from X to combineExtract,
5165 // which might be able to optimize the extraction.
5166 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8),
5167 VecVT.getStoreSize() / TruncBytes);
5168 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT);
5169 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true);
5170 }
5171 }
5172 }
5173 }
5174 return SDValue();
5175}
5176
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005177SDValue SystemZTargetLowering::combineSIGN_EXTEND(
5178 SDNode *N, DAGCombinerInfo &DCI) const {
5179 // Convert (sext (ashr (shl X, C1), C2)) to
5180 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
5181 // cheap as narrower ones.
5182 SelectionDAG &DAG = DCI.DAG;
5183 SDValue N0 = N->getOperand(0);
5184 EVT VT = N->getValueType(0);
5185 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
5186 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5187 SDValue Inner = N0.getOperand(0);
5188 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
5189 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00005190 unsigned Extra = (VT.getSizeInBits() - N0.getValueSizeInBits());
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005191 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
5192 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
5193 EVT ShiftVT = N0.getOperand(1).getValueType();
5194 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
5195 Inner.getOperand(0));
5196 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
5197 DAG.getConstant(NewShlAmt, SDLoc(Inner),
5198 ShiftVT));
5199 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
5200 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT));
5201 }
5202 }
5203 }
5204 return SDValue();
5205}
5206
5207SDValue SystemZTargetLowering::combineMERGE(
5208 SDNode *N, DAGCombinerInfo &DCI) const {
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005209 SelectionDAG &DAG = DCI.DAG;
5210 unsigned Opcode = N->getOpcode();
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005211 SDValue Op0 = N->getOperand(0);
5212 SDValue Op1 = N->getOperand(1);
5213 if (Op0.getOpcode() == ISD::BITCAST)
5214 Op0 = Op0.getOperand(0);
5215 if (Op0.getOpcode() == SystemZISD::BYTE_MASK &&
5216 cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0) {
5217 // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF
5218 // for v4f32.
5219 if (Op1 == N->getOperand(0))
5220 return Op1;
5221 // (z_merge_? 0, X) -> (z_unpackl_? 0, X).
5222 EVT VT = Op1.getValueType();
5223 unsigned ElemBytes = VT.getVectorElementType().getStoreSize();
5224 if (ElemBytes <= 4) {
5225 Opcode = (Opcode == SystemZISD::MERGE_HIGH ?
5226 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW);
5227 EVT InVT = VT.changeVectorElementTypeToInteger();
5228 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16),
5229 SystemZ::VectorBytes / ElemBytes / 2);
5230 if (VT != InVT) {
5231 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1);
5232 DCI.AddToWorklist(Op1.getNode());
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005233 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005234 SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1);
5235 DCI.AddToWorklist(Op.getNode());
5236 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005237 }
5238 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005239 return SDValue();
5240}
5241
5242SDValue SystemZTargetLowering::combineSTORE(
5243 SDNode *N, DAGCombinerInfo &DCI) const {
5244 SelectionDAG &DAG = DCI.DAG;
5245 auto *SN = cast<StoreSDNode>(N);
5246 auto &Op1 = N->getOperand(1);
5247 EVT MemVT = SN->getMemoryVT();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005248 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better
5249 // for the extraction to be done on a vMiN value, so that we can use VSTE.
5250 // If X has wider elements then convert it to:
5251 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z).
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005252 if (MemVT.isInteger()) {
5253 if (SDValue Value =
5254 combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) {
5255 DCI.AddToWorklist(Value.getNode());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005256
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005257 // Rewrite the store with the new form of stored value.
5258 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value,
5259 SN->getBasePtr(), SN->getMemoryVT(),
5260 SN->getMemOperand());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005261 }
5262 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005263 // Combine STORE (BSWAP) into STRVH/STRV/STRVG
5264 // See comment in combineBSWAP about volatile accesses.
Ulrich Weigand59a01a92017-09-19 20:50:05 +00005265 if (!SN->isTruncatingStore() &&
5266 !SN->isVolatile() &&
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005267 Op1.getOpcode() == ISD::BSWAP &&
5268 Op1.getNode()->hasOneUse() &&
5269 (Op1.getValueType() == MVT::i16 ||
5270 Op1.getValueType() == MVT::i32 ||
5271 Op1.getValueType() == MVT::i64)) {
5272
5273 SDValue BSwapOp = Op1.getOperand(0);
5274
5275 if (BSwapOp.getValueType() == MVT::i16)
5276 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), MVT::i32, BSwapOp);
5277
5278 SDValue Ops[] = {
5279 N->getOperand(0), BSwapOp, N->getOperand(2),
5280 DAG.getValueType(Op1.getValueType())
5281 };
5282
5283 return
5284 DAG.getMemIntrinsicNode(SystemZISD::STRV, SDLoc(N), DAG.getVTList(MVT::Other),
5285 Ops, MemVT, SN->getMemOperand());
5286 }
5287 return SDValue();
5288}
5289
5290SDValue SystemZTargetLowering::combineEXTRACT_VECTOR_ELT(
5291 SDNode *N, DAGCombinerInfo &DCI) const {
Jonas Paulsson56bb0852017-03-31 13:22:59 +00005292
Jonas Paulsson56bb0852017-03-31 13:22:59 +00005293 if (!Subtarget.hasVector())
5294 return SDValue();
5295
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005296 // Try to simplify a vector extraction.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005297 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
5298 SDValue Op0 = N->getOperand(0);
5299 EVT VecVT = Op0.getValueType();
5300 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0,
5301 IndexN->getZExtValue(), DCI, false);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005302 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005303 return SDValue();
5304}
5305
5306SDValue SystemZTargetLowering::combineJOIN_DWORDS(
5307 SDNode *N, DAGCombinerInfo &DCI) const {
5308 SelectionDAG &DAG = DCI.DAG;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005309 // (join_dwords X, X) == (replicate X)
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005310 if (N->getOperand(0) == N->getOperand(1))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005311 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0),
5312 N->getOperand(0));
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005313 return SDValue();
5314}
5315
5316SDValue SystemZTargetLowering::combineFP_ROUND(
5317 SDNode *N, DAGCombinerInfo &DCI) const {
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00005318 // (fpround (extract_vector_elt X 0))
5319 // (fpround (extract_vector_elt X 1)) ->
Ulrich Weigand80b3af72015-05-05 19:27:45 +00005320 // (extract_vector_elt (VROUND X) 0)
5321 // (extract_vector_elt (VROUND X) 1)
5322 //
5323 // This is a special case since the target doesn't really support v2f32s.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005324 SelectionDAG &DAG = DCI.DAG;
5325 SDValue Op0 = N->getOperand(0);
5326 if (N->getValueType(0) == MVT::f32 &&
5327 Op0.hasOneUse() &&
5328 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5329 Op0.getOperand(0).getValueType() == MVT::v2f64 &&
5330 Op0.getOperand(1).getOpcode() == ISD::Constant &&
5331 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) {
5332 SDValue Vec = Op0.getOperand(0);
5333 for (auto *U : Vec->uses()) {
5334 if (U != Op0.getNode() &&
5335 U->hasOneUse() &&
5336 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5337 U->getOperand(0) == Vec &&
5338 U->getOperand(1).getOpcode() == ISD::Constant &&
5339 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) {
5340 SDValue OtherRound = SDValue(*U->use_begin(), 0);
5341 if (OtherRound.getOpcode() == ISD::FP_ROUND &&
5342 OtherRound.getOperand(0) == SDValue(U, 0) &&
5343 OtherRound.getValueType() == MVT::f32) {
5344 SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N),
5345 MVT::v4f32, Vec);
5346 DCI.AddToWorklist(VRound.getNode());
5347 SDValue Extract1 =
5348 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32,
5349 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32));
5350 DCI.AddToWorklist(Extract1.getNode());
5351 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1);
5352 SDValue Extract0 =
5353 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32,
5354 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
5355 return Extract0;
Ulrich Weigand80b3af72015-05-05 19:27:45 +00005356 }
5357 }
5358 }
5359 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005360 return SDValue();
5361}
Bryan Chan28b759c2016-05-16 20:32:22 +00005362
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005363SDValue SystemZTargetLowering::combineBSWAP(
5364 SDNode *N, DAGCombinerInfo &DCI) const {
5365 SelectionDAG &DAG = DCI.DAG;
Bryan Chan28b759c2016-05-16 20:32:22 +00005366 // Combine BSWAP (LOAD) into LRVH/LRV/LRVG
5367 // These loads are allowed to access memory multiple times, and so we must check
5368 // that the loads are not volatile before performing the combine.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005369 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
5370 N->getOperand(0).hasOneUse() &&
5371 (N->getValueType(0) == MVT::i16 || N->getValueType(0) == MVT::i32 ||
5372 N->getValueType(0) == MVT::i64) &&
5373 !cast<LoadSDNode>(N->getOperand(0))->isVolatile()) {
Bryan Chan28b759c2016-05-16 20:32:22 +00005374 SDValue Load = N->getOperand(0);
5375 LoadSDNode *LD = cast<LoadSDNode>(Load);
5376
5377 // Create the byte-swapping load.
5378 SDValue Ops[] = {
5379 LD->getChain(), // Chain
5380 LD->getBasePtr(), // Ptr
5381 DAG.getValueType(N->getValueType(0)) // VT
5382 };
5383 SDValue BSLoad =
5384 DAG.getMemIntrinsicNode(SystemZISD::LRV, SDLoc(N),
5385 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
5386 MVT::i64 : MVT::i32, MVT::Other),
5387 Ops, LD->getMemoryVT(), LD->getMemOperand());
5388
5389 // If this is an i16 load, insert the truncate.
5390 SDValue ResVal = BSLoad;
5391 if (N->getValueType(0) == MVT::i16)
5392 ResVal = DAG.getNode(ISD::TRUNCATE, SDLoc(N), MVT::i16, BSLoad);
5393
5394 // First, combine the bswap away. This makes the value produced by the
5395 // load dead.
5396 DCI.CombineTo(N, ResVal);
5397
5398 // Next, combine the load away, we give it a bogus result value but a real
5399 // chain result. The result value is dead because the bswap is dead.
5400 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
5401
5402 // Return N so it doesn't get rechecked!
5403 return SDValue(N, 0);
5404 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005405 return SDValue();
5406}
Bryan Chan28b759c2016-05-16 20:32:22 +00005407
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005408SDValue SystemZTargetLowering::combineSHIFTROT(
5409 SDNode *N, DAGCombinerInfo &DCI) const {
5410
5411 SelectionDAG &DAG = DCI.DAG;
5412
5413 // Shift/rotate instructions only use the last 6 bits of the second operand
5414 // register. If the second operand is the result of an AND with an immediate
5415 // value that has its last 6 bits set, we can safely remove the AND operation.
Elliot Colp687691a2016-08-18 18:04:26 +00005416 //
5417 // If the AND operation doesn't have the last 6 bits set, we can't remove it
Elliot Colpa4092102016-08-23 14:03:02 +00005418 // entirely, but we can still truncate it to a 16-bit value. This prevents
5419 // us from ending up with a NILL with a signed operand, which will cause the
5420 // instruction printer to abort.
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005421 SDValue N1 = N->getOperand(1);
5422 if (N1.getOpcode() == ISD::AND) {
Elliot Colp687691a2016-08-18 18:04:26 +00005423 SDValue AndMaskOp = N1->getOperand(1);
5424 auto *AndMask = dyn_cast<ConstantSDNode>(AndMaskOp);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005425
5426 // The AND mask is constant
5427 if (AndMask) {
Elliot Colpa4092102016-08-23 14:03:02 +00005428 auto AmtVal = AndMask->getZExtValue();
5429
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005430 // Bottom 6 bits are set
5431 if ((AmtVal & 0x3f) == 0x3f) {
Elliot Colpa4092102016-08-23 14:03:02 +00005432 SDValue AndOp = N1->getOperand(0);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005433
5434 // This is the only use, so remove the node
5435 if (N1.hasOneUse()) {
5436 // Combine the AND away
5437 DCI.CombineTo(N1.getNode(), AndOp);
5438
5439 // Return N so it isn't rechecked
5440 return SDValue(N, 0);
5441
5442 // The node will be reused, so create a new node for this one use
5443 } else {
5444 SDValue Replace = DAG.getNode(N->getOpcode(), SDLoc(N),
5445 N->getValueType(0), N->getOperand(0),
5446 AndOp);
5447 DCI.AddToWorklist(Replace.getNode());
5448
5449 return Replace;
5450 }
Elliot Colp687691a2016-08-18 18:04:26 +00005451
Elliot Colpa4092102016-08-23 14:03:02 +00005452 // We can't remove the AND, but we can use NILL here (normally we would
5453 // use NILF). Only keep the last 16 bits of the mask. The actual
5454 // transformation will be handled by .td definitions.
5455 } else if (AmtVal >> 16 != 0) {
5456 SDValue AndOp = N1->getOperand(0);
Elliot Colp687691a2016-08-18 18:04:26 +00005457
Elliot Colpa4092102016-08-23 14:03:02 +00005458 auto NewMask = DAG.getConstant(AndMask->getZExtValue() & 0x0000ffff,
5459 SDLoc(AndMaskOp),
5460 AndMaskOp.getValueType());
Elliot Colp687691a2016-08-18 18:04:26 +00005461
Elliot Colpa4092102016-08-23 14:03:02 +00005462 auto NewAnd = DAG.getNode(N1.getOpcode(), SDLoc(N1), N1.getValueType(),
5463 AndOp, NewMask);
Elliot Colp687691a2016-08-18 18:04:26 +00005464
Elliot Colpa4092102016-08-23 14:03:02 +00005465 SDValue Replace = DAG.getNode(N->getOpcode(), SDLoc(N),
5466 N->getValueType(0), N->getOperand(0),
5467 NewAnd);
5468 DCI.AddToWorklist(Replace.getNode());
Elliot Colp687691a2016-08-18 18:04:26 +00005469
Elliot Colpa4092102016-08-23 14:03:02 +00005470 return Replace;
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005471 }
5472 }
5473 }
5474
5475 return SDValue();
5476}
5477
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005478SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
5479 DAGCombinerInfo &DCI) const {
5480 switch(N->getOpcode()) {
5481 default: break;
5482 case ISD::SIGN_EXTEND: return combineSIGN_EXTEND(N, DCI);
5483 case SystemZISD::MERGE_HIGH:
5484 case SystemZISD::MERGE_LOW: return combineMERGE(N, DCI);
5485 case ISD::STORE: return combineSTORE(N, DCI);
5486 case ISD::EXTRACT_VECTOR_ELT: return combineEXTRACT_VECTOR_ELT(N, DCI);
5487 case SystemZISD::JOIN_DWORDS: return combineJOIN_DWORDS(N, DCI);
5488 case ISD::FP_ROUND: return combineFP_ROUND(N, DCI);
5489 case ISD::BSWAP: return combineBSWAP(N, DCI);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005490 case ISD::SHL:
5491 case ISD::SRA:
5492 case ISD::SRL:
5493 case ISD::ROTL: return combineSHIFTROT(N, DCI);
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005494 }
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005495
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005496 return SDValue();
5497}
5498
Ulrich Weigand9eb858c2018-01-19 20:49:05 +00005499void
5500SystemZTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
5501 KnownBits &Known,
5502 const APInt &DemandedElts,
5503 const SelectionDAG &DAG,
5504 unsigned Depth) const {
5505 unsigned BitWidth = Known.getBitWidth();
5506
5507 Known.resetAll();
5508 switch (Op.getOpcode()) {
5509 case SystemZISD::SELECT_CCMASK: {
5510 KnownBits TrueKnown(BitWidth), FalseKnown(BitWidth);
5511 DAG.computeKnownBits(Op.getOperand(0), TrueKnown, Depth + 1);
5512 DAG.computeKnownBits(Op.getOperand(1), FalseKnown, Depth + 1);
5513 Known.Zero = TrueKnown.Zero & FalseKnown.Zero;
5514 Known.One = TrueKnown.One & FalseKnown.One;
5515 break;
5516 }
5517
5518 default:
5519 break;
5520 }
5521}
5522
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005523//===----------------------------------------------------------------------===//
5524// Custom insertion
5525//===----------------------------------------------------------------------===//
5526
5527// Create a new basic block after MBB.
5528static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
5529 MachineFunction &MF = *MBB->getParent();
5530 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00005531 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005532 return NewMBB;
5533}
5534
Richard Sandifordbe133a82013-08-28 09:01:51 +00005535// Split MBB after MI and return the new block (the one that contains
5536// instructions after MI).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005537static MachineBasicBlock *splitBlockAfter(MachineBasicBlock::iterator MI,
Richard Sandifordbe133a82013-08-28 09:01:51 +00005538 MachineBasicBlock *MBB) {
5539 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
5540 NewMBB->splice(NewMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00005541 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Richard Sandifordbe133a82013-08-28 09:01:51 +00005542 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
5543 return NewMBB;
5544}
5545
Richard Sandiford5e318f02013-08-27 09:54:29 +00005546// Split MBB before MI and return the new block (the one that contains MI).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005547static MachineBasicBlock *splitBlockBefore(MachineBasicBlock::iterator MI,
Richard Sandiford5e318f02013-08-27 09:54:29 +00005548 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005549 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005550 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005551 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
5552 return NewMBB;
5553}
5554
Richard Sandiford5e318f02013-08-27 09:54:29 +00005555// Force base value Base into a register before MI. Return the register.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005556static unsigned forceReg(MachineInstr &MI, MachineOperand &Base,
Richard Sandiford5e318f02013-08-27 09:54:29 +00005557 const SystemZInstrInfo *TII) {
5558 if (Base.isReg())
5559 return Base.getReg();
5560
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005561 MachineBasicBlock *MBB = MI.getParent();
Richard Sandiford5e318f02013-08-27 09:54:29 +00005562 MachineFunction &MF = *MBB->getParent();
5563 MachineRegisterInfo &MRI = MF.getRegInfo();
5564
5565 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005566 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LA), Reg)
Diana Picus116bbab2017-01-13 09:58:52 +00005567 .add(Base)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005568 .addImm(0)
5569 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005570 return Reg;
5571}
5572
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005573// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
5574MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005575SystemZTargetLowering::emitSelect(MachineInstr &MI,
Ulrich Weigand524f2762016-11-28 13:34:08 +00005576 MachineBasicBlock *MBB,
5577 unsigned LOCROpcode) const {
Eric Christophera6734172015-01-31 00:06:45 +00005578 const SystemZInstrInfo *TII =
5579 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005580
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005581 unsigned DestReg = MI.getOperand(0).getReg();
5582 unsigned TrueReg = MI.getOperand(1).getReg();
5583 unsigned FalseReg = MI.getOperand(2).getReg();
5584 unsigned CCValid = MI.getOperand(3).getImm();
5585 unsigned CCMask = MI.getOperand(4).getImm();
5586 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005587
Ulrich Weigand524f2762016-11-28 13:34:08 +00005588 // Use LOCROpcode if possible.
5589 if (LOCROpcode && Subtarget.hasLoadStoreOnCond()) {
5590 BuildMI(*MBB, MI, DL, TII->get(LOCROpcode), DestReg)
5591 .addReg(FalseReg).addReg(TrueReg)
5592 .addImm(CCValid).addImm(CCMask);
5593 MI.eraseFromParent();
5594 return MBB;
5595 }
5596
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005597 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005598 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005599 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5600
5601 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00005602 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005603 // # fallthrough to FalseMBB
5604 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005605 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5606 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005607 MBB->addSuccessor(JoinMBB);
5608 MBB->addSuccessor(FalseMBB);
5609
5610 // FalseMBB:
5611 // # fallthrough to JoinMBB
5612 MBB = FalseMBB;
5613 MBB->addSuccessor(JoinMBB);
5614
5615 // JoinMBB:
5616 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
5617 // ...
5618 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005619 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005620 .addReg(TrueReg).addMBB(StartMBB)
5621 .addReg(FalseReg).addMBB(FalseMBB);
5622
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005623 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005624 return JoinMBB;
5625}
5626
Richard Sandifordb86a8342013-06-27 09:27:40 +00005627// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
5628// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005629// happen when the condition is false rather than true. If a STORE ON
5630// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005631MachineBasicBlock *SystemZTargetLowering::emitCondStore(MachineInstr &MI,
5632 MachineBasicBlock *MBB,
5633 unsigned StoreOpcode,
5634 unsigned STOCOpcode,
5635 bool Invert) const {
Eric Christophera6734172015-01-31 00:06:45 +00005636 const SystemZInstrInfo *TII =
5637 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordb86a8342013-06-27 09:27:40 +00005638
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005639 unsigned SrcReg = MI.getOperand(0).getReg();
5640 MachineOperand Base = MI.getOperand(1);
5641 int64_t Disp = MI.getOperand(2).getImm();
5642 unsigned IndexReg = MI.getOperand(3).getReg();
5643 unsigned CCValid = MI.getOperand(4).getImm();
5644 unsigned CCMask = MI.getOperand(5).getImm();
5645 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005646
5647 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
5648
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005649 // Use STOCOpcode if possible. We could use different store patterns in
5650 // order to avoid matching the index register, but the performance trade-offs
5651 // might be more complicated in that case.
Eric Christopher93bf97c2014-06-27 07:38:01 +00005652 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005653 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005654 CCMask ^= CCValid;
Jonas Paulssonae8d22c2017-06-07 14:08:34 +00005655
5656 // ISel pattern matching also adds a load memory operand of the same
5657 // address, so take special care to find the storing memory operand.
5658 MachineMemOperand *MMO = nullptr;
5659 for (auto *I : MI.memoperands())
5660 if (I->isStore()) {
5661 MMO = I;
5662 break;
5663 }
5664
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005665 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Jonas Paulssonae8d22c2017-06-07 14:08:34 +00005666 .addReg(SrcReg)
5667 .add(Base)
5668 .addImm(Disp)
5669 .addImm(CCValid)
5670 .addImm(CCMask)
5671 .addMemOperand(MMO);
5672
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005673 MI.eraseFromParent();
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005674 return MBB;
5675 }
5676
Richard Sandifordb86a8342013-06-27 09:27:40 +00005677 // Get the condition needed to branch around the store.
5678 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005679 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00005680
5681 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005682 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005683 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5684
5685 // StartMBB:
5686 // BRC CCMask, JoinMBB
5687 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00005688 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005689 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5690 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005691 MBB->addSuccessor(JoinMBB);
5692 MBB->addSuccessor(FalseMBB);
5693
5694 // FalseMBB:
5695 // store %SrcReg, %Disp(%Index,%Base)
5696 // # fallthrough to JoinMBB
5697 MBB = FalseMBB;
5698 BuildMI(MBB, DL, TII->get(StoreOpcode))
Diana Picus116bbab2017-01-13 09:58:52 +00005699 .addReg(SrcReg)
5700 .add(Base)
5701 .addImm(Disp)
5702 .addReg(IndexReg);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005703 MBB->addSuccessor(JoinMBB);
5704
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005705 MI.eraseFromParent();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005706 return JoinMBB;
5707}
5708
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005709// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
5710// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
5711// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
5712// BitSize is the width of the field in bits, or 0 if this is a partword
5713// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
5714// is one of the operands. Invert says whether the field should be
5715// inverted after performing BinOpcode (e.g. for NAND).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005716MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadBinary(
5717 MachineInstr &MI, MachineBasicBlock *MBB, unsigned BinOpcode,
5718 unsigned BitSize, bool Invert) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005719 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005720 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005721 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005722 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005723 bool IsSubWord = (BitSize < 32);
5724
5725 // Extract the operands. Base can be a register or a frame index.
5726 // Src2 can be a register or immediate.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005727 unsigned Dest = MI.getOperand(0).getReg();
5728 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5729 int64_t Disp = MI.getOperand(2).getImm();
5730 MachineOperand Src2 = earlyUseOperand(MI.getOperand(3));
5731 unsigned BitShift = (IsSubWord ? MI.getOperand(4).getReg() : 0);
5732 unsigned NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : 0);
5733 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005734 if (IsSubWord)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005735 BitSize = MI.getOperand(6).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005736
5737 // Subword operations use 32-bit registers.
5738 const TargetRegisterClass *RC = (BitSize <= 32 ?
5739 &SystemZ::GR32BitRegClass :
5740 &SystemZ::GR64BitRegClass);
5741 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5742 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5743
5744 // Get the right opcodes for the displacement.
5745 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5746 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5747 assert(LOpcode && CSOpcode && "Displacement out of range");
5748
5749 // Create virtual registers for temporary results.
5750 unsigned OrigVal = MRI.createVirtualRegister(RC);
5751 unsigned OldVal = MRI.createVirtualRegister(RC);
5752 unsigned NewVal = (BinOpcode || IsSubWord ?
5753 MRI.createVirtualRegister(RC) : Src2.getReg());
5754 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5755 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5756
5757 // Insert a basic block for the main loop.
5758 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005759 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005760 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5761
5762 // StartMBB:
5763 // ...
5764 // %OrigVal = L Disp(%Base)
5765 // # fall through to LoopMMB
5766 MBB = StartMBB;
Diana Picus116bbab2017-01-13 09:58:52 +00005767 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005768 MBB->addSuccessor(LoopMBB);
5769
5770 // LoopMBB:
5771 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
5772 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5773 // %RotatedNewVal = OP %RotatedOldVal, %Src2
5774 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5775 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5776 // JNE LoopMBB
5777 // # fall through to DoneMMB
5778 MBB = LoopMBB;
5779 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5780 .addReg(OrigVal).addMBB(StartMBB)
5781 .addReg(Dest).addMBB(LoopMBB);
5782 if (IsSubWord)
5783 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5784 .addReg(OldVal).addReg(BitShift).addImm(0);
5785 if (Invert) {
5786 // Perform the operation normally and then invert every bit of the field.
5787 unsigned Tmp = MRI.createVirtualRegister(RC);
Diana Picus116bbab2017-01-13 09:58:52 +00005788 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp).addReg(RotatedOldVal).add(Src2);
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005789 if (BitSize <= 32)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005790 // XILF with the upper BitSize bits set.
Richard Sandiford652784e2013-09-25 11:11:53 +00005791 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005792 .addReg(Tmp).addImm(-1U << (32 - BitSize));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005793 else {
5794 // Use LCGR and add -1 to the result, which is more compact than
5795 // an XILF, XILH pair.
5796 unsigned Tmp2 = MRI.createVirtualRegister(RC);
5797 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
5798 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
5799 .addReg(Tmp2).addImm(-1);
5800 }
5801 } else if (BinOpcode)
5802 // A simply binary operation.
5803 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
Diana Picus116bbab2017-01-13 09:58:52 +00005804 .addReg(RotatedOldVal)
5805 .add(Src2);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005806 else if (IsSubWord)
5807 // Use RISBG to rotate Src2 into position and use it to replace the
5808 // field in RotatedOldVal.
5809 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
5810 .addReg(RotatedOldVal).addReg(Src2.getReg())
5811 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
5812 if (IsSubWord)
5813 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5814 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5815 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
Diana Picus116bbab2017-01-13 09:58:52 +00005816 .addReg(OldVal)
5817 .addReg(NewVal)
5818 .add(Base)
5819 .addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005820 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5821 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005822 MBB->addSuccessor(LoopMBB);
5823 MBB->addSuccessor(DoneMBB);
5824
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005825 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005826 return DoneMBB;
5827}
5828
5829// Implement EmitInstrWithCustomInserter for pseudo
5830// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
5831// instruction that should be used to compare the current field with the
5832// minimum or maximum value. KeepOldMask is the BRC condition-code mask
5833// for when the current field should be kept. BitSize is the width of
5834// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005835MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadMinMax(
5836 MachineInstr &MI, MachineBasicBlock *MBB, unsigned CompareOpcode,
5837 unsigned KeepOldMask, unsigned BitSize) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005838 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005839 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005840 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005841 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005842 bool IsSubWord = (BitSize < 32);
5843
5844 // Extract the operands. Base can be a register or a frame index.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005845 unsigned Dest = MI.getOperand(0).getReg();
5846 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5847 int64_t Disp = MI.getOperand(2).getImm();
5848 unsigned Src2 = MI.getOperand(3).getReg();
5849 unsigned BitShift = (IsSubWord ? MI.getOperand(4).getReg() : 0);
5850 unsigned NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : 0);
5851 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005852 if (IsSubWord)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005853 BitSize = MI.getOperand(6).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005854
5855 // Subword operations use 32-bit registers.
5856 const TargetRegisterClass *RC = (BitSize <= 32 ?
5857 &SystemZ::GR32BitRegClass :
5858 &SystemZ::GR64BitRegClass);
5859 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5860 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5861
5862 // Get the right opcodes for the displacement.
5863 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5864 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5865 assert(LOpcode && CSOpcode && "Displacement out of range");
5866
5867 // Create virtual registers for temporary results.
5868 unsigned OrigVal = MRI.createVirtualRegister(RC);
5869 unsigned OldVal = MRI.createVirtualRegister(RC);
5870 unsigned NewVal = MRI.createVirtualRegister(RC);
5871 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5872 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
5873 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5874
5875 // Insert 3 basic blocks for the loop.
5876 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005877 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005878 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5879 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
5880 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
5881
5882 // StartMBB:
5883 // ...
5884 // %OrigVal = L Disp(%Base)
5885 // # fall through to LoopMMB
5886 MBB = StartMBB;
Diana Picus116bbab2017-01-13 09:58:52 +00005887 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005888 MBB->addSuccessor(LoopMBB);
5889
5890 // LoopMBB:
5891 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
5892 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5893 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00005894 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005895 MBB = LoopMBB;
5896 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5897 .addReg(OrigVal).addMBB(StartMBB)
5898 .addReg(Dest).addMBB(UpdateMBB);
5899 if (IsSubWord)
5900 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5901 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005902 BuildMI(MBB, DL, TII->get(CompareOpcode))
5903 .addReg(RotatedOldVal).addReg(Src2);
5904 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005905 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005906 MBB->addSuccessor(UpdateMBB);
5907 MBB->addSuccessor(UseAltMBB);
5908
5909 // UseAltMBB:
5910 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
5911 // # fall through to UpdateMMB
5912 MBB = UseAltMBB;
5913 if (IsSubWord)
5914 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
5915 .addReg(RotatedOldVal).addReg(Src2)
5916 .addImm(32).addImm(31 + BitSize).addImm(0);
5917 MBB->addSuccessor(UpdateMBB);
5918
5919 // UpdateMBB:
5920 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
5921 // [ %RotatedAltVal, UseAltMBB ]
5922 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5923 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5924 // JNE LoopMBB
5925 // # fall through to DoneMMB
5926 MBB = UpdateMBB;
5927 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
5928 .addReg(RotatedOldVal).addMBB(LoopMBB)
5929 .addReg(RotatedAltVal).addMBB(UseAltMBB);
5930 if (IsSubWord)
5931 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5932 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5933 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
Diana Picus116bbab2017-01-13 09:58:52 +00005934 .addReg(OldVal)
5935 .addReg(NewVal)
5936 .add(Base)
5937 .addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005938 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5939 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005940 MBB->addSuccessor(LoopMBB);
5941 MBB->addSuccessor(DoneMBB);
5942
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005943 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005944 return DoneMBB;
5945}
5946
5947// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
5948// instruction MI.
5949MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005950SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005951 MachineBasicBlock *MBB) const {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00005952
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005953 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005954 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005955 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005956 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005957
5958 // Extract the operands. Base can be a register or a frame index.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005959 unsigned Dest = MI.getOperand(0).getReg();
5960 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5961 int64_t Disp = MI.getOperand(2).getImm();
5962 unsigned OrigCmpVal = MI.getOperand(3).getReg();
5963 unsigned OrigSwapVal = MI.getOperand(4).getReg();
5964 unsigned BitShift = MI.getOperand(5).getReg();
5965 unsigned NegBitShift = MI.getOperand(6).getReg();
5966 int64_t BitSize = MI.getOperand(7).getImm();
5967 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005968
5969 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
5970
5971 // Get the right opcodes for the displacement.
5972 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
5973 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
5974 assert(LOpcode && CSOpcode && "Displacement out of range");
5975
5976 // Create virtual registers for temporary results.
5977 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
5978 unsigned OldVal = MRI.createVirtualRegister(RC);
5979 unsigned CmpVal = MRI.createVirtualRegister(RC);
5980 unsigned SwapVal = MRI.createVirtualRegister(RC);
5981 unsigned StoreVal = MRI.createVirtualRegister(RC);
5982 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
5983 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
5984 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
5985
5986 // Insert 2 basic blocks for the loop.
5987 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005988 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005989 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5990 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
5991
5992 // StartMBB:
5993 // ...
5994 // %OrigOldVal = L Disp(%Base)
5995 // # fall through to LoopMMB
5996 MBB = StartMBB;
5997 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
Diana Picus116bbab2017-01-13 09:58:52 +00005998 .add(Base)
5999 .addImm(Disp)
6000 .addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006001 MBB->addSuccessor(LoopMBB);
6002
6003 // LoopMBB:
6004 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
6005 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
6006 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
6007 // %Dest = RLL %OldVal, BitSize(%BitShift)
6008 // ^^ The low BitSize bits contain the field
6009 // of interest.
6010 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
6011 // ^^ Replace the upper 32-BitSize bits of the
6012 // comparison value with those that we loaded,
6013 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00006014 // CR %Dest, %RetryCmpVal
6015 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006016 // # Fall through to SetMBB
6017 MBB = LoopMBB;
6018 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
6019 .addReg(OrigOldVal).addMBB(StartMBB)
6020 .addReg(RetryOldVal).addMBB(SetMBB);
6021 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
6022 .addReg(OrigCmpVal).addMBB(StartMBB)
6023 .addReg(RetryCmpVal).addMBB(SetMBB);
6024 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
6025 .addReg(OrigSwapVal).addMBB(StartMBB)
6026 .addReg(RetrySwapVal).addMBB(SetMBB);
6027 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
6028 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
6029 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
6030 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00006031 BuildMI(MBB, DL, TII->get(SystemZ::CR))
6032 .addReg(Dest).addReg(RetryCmpVal);
6033 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00006034 .addImm(SystemZ::CCMASK_ICMP)
6035 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006036 MBB->addSuccessor(DoneMBB);
6037 MBB->addSuccessor(SetMBB);
6038
6039 // SetMBB:
6040 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
6041 // ^^ Replace the upper 32-BitSize bits of the new
6042 // value with those that we loaded.
6043 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
6044 // ^^ Rotate the new field to its proper position.
6045 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
6046 // JNE LoopMBB
6047 // # fall through to ExitMMB
6048 MBB = SetMBB;
6049 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
6050 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
6051 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
6052 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
6053 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
Diana Picus116bbab2017-01-13 09:58:52 +00006054 .addReg(OldVal)
6055 .addReg(StoreVal)
6056 .add(Base)
6057 .addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00006058 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6059 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006060 MBB->addSuccessor(LoopMBB);
6061 MBB->addSuccessor(DoneMBB);
6062
Ulrich Weiganddf868552017-09-28 22:08:25 +00006063 // If the CC def wasn't dead in the ATOMIC_CMP_SWAPW, mark CC as live-in
6064 // to the block after the loop. At this point, CC may have been defined
6065 // either by the CR in LoopMBB or by the CS in SetMBB.
6066 if (!MI.registerDefIsDead(SystemZ::CC))
6067 DoneMBB->addLiveIn(SystemZ::CC);
6068
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006069 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006070 return DoneMBB;
6071}
6072
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00006073// Emit a move from two GR64s to a GR128.
6074MachineBasicBlock *
6075SystemZTargetLowering::emitPair128(MachineInstr &MI,
6076 MachineBasicBlock *MBB) const {
6077 MachineFunction &MF = *MBB->getParent();
6078 const SystemZInstrInfo *TII =
6079 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
6080 MachineRegisterInfo &MRI = MF.getRegInfo();
6081 DebugLoc DL = MI.getDebugLoc();
6082
6083 unsigned Dest = MI.getOperand(0).getReg();
6084 unsigned Hi = MI.getOperand(1).getReg();
6085 unsigned Lo = MI.getOperand(2).getReg();
6086 unsigned Tmp1 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6087 unsigned Tmp2 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6088
6089 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Tmp1);
6090 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Tmp2)
6091 .addReg(Tmp1).addReg(Hi).addImm(SystemZ::subreg_h64);
6092 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
6093 .addReg(Tmp2).addReg(Lo).addImm(SystemZ::subreg_l64);
6094
6095 MI.eraseFromParent();
6096 return MBB;
6097}
6098
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006099// Emit an extension from a GR64 to a GR128. ClearEven is true
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006100// if the high register of the GR128 value must be cleared or false if
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006101// it's "don't care".
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006102MachineBasicBlock *SystemZTargetLowering::emitExt128(MachineInstr &MI,
6103 MachineBasicBlock *MBB,
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006104 bool ClearEven) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006105 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00006106 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00006107 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006108 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006109 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006110
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006111 unsigned Dest = MI.getOperand(0).getReg();
6112 unsigned Src = MI.getOperand(1).getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006113 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6114
6115 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
6116 if (ClearEven) {
6117 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6118 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
6119
6120 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
6121 .addImm(0);
6122 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
Richard Sandiford87a44362013-09-30 10:28:35 +00006123 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006124 In128 = NewIn128;
6125 }
6126 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006127 .addReg(In128).addReg(Src).addImm(SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006128
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006129 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006130 return MBB;
6131}
6132
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006133MachineBasicBlock *SystemZTargetLowering::emitMemMemWrapper(
6134 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +00006135 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00006136 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00006137 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandiford5e318f02013-08-27 09:54:29 +00006138 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006139 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordd131ff82013-07-08 09:35:23 +00006140
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006141 MachineOperand DestBase = earlyUseOperand(MI.getOperand(0));
6142 uint64_t DestDisp = MI.getOperand(1).getImm();
6143 MachineOperand SrcBase = earlyUseOperand(MI.getOperand(2));
6144 uint64_t SrcDisp = MI.getOperand(3).getImm();
6145 uint64_t Length = MI.getOperand(4).getImm();
Richard Sandifordd131ff82013-07-08 09:35:23 +00006146
Richard Sandifordbe133a82013-08-28 09:01:51 +00006147 // When generating more than one CLC, all but the last will need to
6148 // branch to the end when a difference is found.
6149 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
Craig Topper062a2ba2014-04-25 05:30:21 +00006150 splitBlockAfter(MI, MBB) : nullptr);
Richard Sandifordbe133a82013-08-28 09:01:51 +00006151
Richard Sandiford5e318f02013-08-27 09:54:29 +00006152 // Check for the loop form, in which operand 5 is the trip count.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006153 if (MI.getNumExplicitOperands() > 5) {
Richard Sandiford5e318f02013-08-27 09:54:29 +00006154 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
6155
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006156 uint64_t StartCountReg = MI.getOperand(5).getReg();
Richard Sandiford5e318f02013-08-27 09:54:29 +00006157 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
6158 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
6159 forceReg(MI, DestBase, TII));
6160
6161 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
6162 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
6163 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
6164 MRI.createVirtualRegister(RC));
6165 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
6166 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
6167 MRI.createVirtualRegister(RC));
6168
6169 RC = &SystemZ::GR64BitRegClass;
6170 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
6171 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
6172
6173 MachineBasicBlock *StartMBB = MBB;
6174 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
6175 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00006176 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006177
6178 // StartMBB:
6179 // # fall through to LoopMMB
6180 MBB->addSuccessor(LoopMBB);
6181
6182 // LoopMBB:
6183 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00006184 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00006185 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00006186 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00006187 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00006188 // [ %NextCountReg, NextMBB ]
6189 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00006190 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00006191 // ( JLH EndMBB )
6192 //
6193 // The prefetch is used only for MVC. The JLH is used only for CLC.
6194 MBB = LoopMBB;
6195
6196 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
6197 .addReg(StartDestReg).addMBB(StartMBB)
6198 .addReg(NextDestReg).addMBB(NextMBB);
6199 if (!HaveSingleBase)
6200 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
6201 .addReg(StartSrcReg).addMBB(StartMBB)
6202 .addReg(NextSrcReg).addMBB(NextMBB);
6203 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
6204 .addReg(StartCountReg).addMBB(StartMBB)
6205 .addReg(NextCountReg).addMBB(NextMBB);
6206 if (Opcode == SystemZ::MVC)
6207 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
6208 .addImm(SystemZ::PFD_WRITE)
6209 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
6210 BuildMI(MBB, DL, TII->get(Opcode))
6211 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
6212 .addReg(ThisSrcReg).addImm(SrcDisp);
6213 if (EndMBB) {
6214 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6215 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
6216 .addMBB(EndMBB);
6217 MBB->addSuccessor(EndMBB);
6218 MBB->addSuccessor(NextMBB);
6219 }
6220
6221 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00006222 // %NextDestReg = LA 256(%ThisDestReg)
6223 // %NextSrcReg = LA 256(%ThisSrcReg)
6224 // %NextCountReg = AGHI %ThisCountReg, -1
6225 // CGHI %NextCountReg, 0
6226 // JLH LoopMBB
6227 // # fall through to DoneMMB
6228 //
6229 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00006230 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00006231
Richard Sandiford5e318f02013-08-27 09:54:29 +00006232 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
6233 .addReg(ThisDestReg).addImm(256).addReg(0);
6234 if (!HaveSingleBase)
6235 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
6236 .addReg(ThisSrcReg).addImm(256).addReg(0);
6237 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
6238 .addReg(ThisCountReg).addImm(-1);
6239 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
6240 .addReg(NextCountReg).addImm(0);
6241 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6242 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
6243 .addMBB(LoopMBB);
6244 MBB->addSuccessor(LoopMBB);
6245 MBB->addSuccessor(DoneMBB);
6246
6247 DestBase = MachineOperand::CreateReg(NextDestReg, false);
6248 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
6249 Length &= 255;
6250 MBB = DoneMBB;
6251 }
6252 // Handle any remaining bytes with straight-line code.
6253 while (Length > 0) {
6254 uint64_t ThisLength = std::min(Length, uint64_t(256));
6255 // The previous iteration might have created out-of-range displacements.
6256 // Apply them using LAY if so.
6257 if (!isUInt<12>(DestDisp)) {
6258 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006259 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg)
Diana Picus116bbab2017-01-13 09:58:52 +00006260 .add(DestBase)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006261 .addImm(DestDisp)
6262 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006263 DestBase = MachineOperand::CreateReg(Reg, false);
6264 DestDisp = 0;
6265 }
6266 if (!isUInt<12>(SrcDisp)) {
6267 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006268 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg)
Diana Picus116bbab2017-01-13 09:58:52 +00006269 .add(SrcBase)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006270 .addImm(SrcDisp)
6271 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006272 SrcBase = MachineOperand::CreateReg(Reg, false);
6273 SrcDisp = 0;
6274 }
6275 BuildMI(*MBB, MI, DL, TII->get(Opcode))
Diana Picus116bbab2017-01-13 09:58:52 +00006276 .add(DestBase)
6277 .addImm(DestDisp)
6278 .addImm(ThisLength)
6279 .add(SrcBase)
Jonas Paulssonae8d22c2017-06-07 14:08:34 +00006280 .addImm(SrcDisp)
6281 ->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Richard Sandiford5e318f02013-08-27 09:54:29 +00006282 DestDisp += ThisLength;
6283 SrcDisp += ThisLength;
6284 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00006285 // If there's another CLC to go, branch to the end if a difference
6286 // was found.
6287 if (EndMBB && Length > 0) {
6288 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
6289 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6290 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
6291 .addMBB(EndMBB);
6292 MBB->addSuccessor(EndMBB);
6293 MBB->addSuccessor(NextMBB);
6294 MBB = NextMBB;
6295 }
6296 }
6297 if (EndMBB) {
6298 MBB->addSuccessor(EndMBB);
6299 MBB = EndMBB;
6300 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006301 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00006302
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006303 MI.eraseFromParent();
Richard Sandifordd131ff82013-07-08 09:35:23 +00006304 return MBB;
6305}
6306
Richard Sandifordca232712013-08-16 11:21:54 +00006307// Decompose string pseudo-instruction MI into a loop that continually performs
6308// Opcode until CC != 3.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006309MachineBasicBlock *SystemZTargetLowering::emitStringWrapper(
6310 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Richard Sandifordca232712013-08-16 11:21:54 +00006311 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00006312 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00006313 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordca232712013-08-16 11:21:54 +00006314 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006315 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordca232712013-08-16 11:21:54 +00006316
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006317 uint64_t End1Reg = MI.getOperand(0).getReg();
6318 uint64_t Start1Reg = MI.getOperand(1).getReg();
6319 uint64_t Start2Reg = MI.getOperand(2).getReg();
6320 uint64_t CharReg = MI.getOperand(3).getReg();
Richard Sandifordca232712013-08-16 11:21:54 +00006321
6322 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
6323 uint64_t This1Reg = MRI.createVirtualRegister(RC);
6324 uint64_t This2Reg = MRI.createVirtualRegister(RC);
6325 uint64_t End2Reg = MRI.createVirtualRegister(RC);
6326
6327 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00006328 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00006329 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
6330
6331 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00006332 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00006333 MBB->addSuccessor(LoopMBB);
6334
6335 // LoopMBB:
6336 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
6337 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford7789b082013-09-30 08:48:38 +00006338 // R0L = %CharReg
6339 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
Richard Sandifordca232712013-08-16 11:21:54 +00006340 // JO LoopMBB
6341 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00006342 //
Richard Sandiford7789b082013-09-30 08:48:38 +00006343 // The load of R0L can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00006344 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00006345
6346 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
6347 .addReg(Start1Reg).addMBB(StartMBB)
6348 .addReg(End1Reg).addMBB(LoopMBB);
6349 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
6350 .addReg(Start2Reg).addMBB(StartMBB)
6351 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford7789b082013-09-30 08:48:38 +00006352 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00006353 BuildMI(MBB, DL, TII->get(Opcode))
6354 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
6355 .addReg(This1Reg).addReg(This2Reg);
6356 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6357 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
6358 MBB->addSuccessor(LoopMBB);
6359 MBB->addSuccessor(DoneMBB);
6360
6361 DoneMBB->addLiveIn(SystemZ::CC);
6362
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006363 MI.eraseFromParent();
Richard Sandifordca232712013-08-16 11:21:54 +00006364 return DoneMBB;
6365}
6366
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006367// Update TBEGIN instruction with final opcode and register clobbers.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006368MachineBasicBlock *SystemZTargetLowering::emitTransactionBegin(
6369 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode,
6370 bool NoFloat) const {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006371 MachineFunction &MF = *MBB->getParent();
6372 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
6373 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
6374
6375 // Update opcode.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006376 MI.setDesc(TII->get(Opcode));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006377
6378 // We cannot handle a TBEGIN that clobbers the stack or frame pointer.
6379 // Make sure to add the corresponding GRSM bits if they are missing.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006380 uint64_t Control = MI.getOperand(2).getImm();
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006381 static const unsigned GPRControlBit[16] = {
6382 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000,
6383 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100
6384 };
6385 Control |= GPRControlBit[15];
6386 if (TFI->hasFP(MF))
6387 Control |= GPRControlBit[11];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006388 MI.getOperand(2).setImm(Control);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006389
6390 // Add GPR clobbers.
6391 for (int I = 0; I < 16; I++) {
6392 if ((Control & GPRControlBit[I]) == 0) {
6393 unsigned Reg = SystemZMC::GR64Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006394 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006395 }
6396 }
6397
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006398 // Add FPR/VR clobbers.
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006399 if (!NoFloat && (Control & 4) != 0) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006400 if (Subtarget.hasVector()) {
6401 for (int I = 0; I < 32; I++) {
6402 unsigned Reg = SystemZMC::VR128Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006403 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006404 }
6405 } else {
6406 for (int I = 0; I < 16; I++) {
6407 unsigned Reg = SystemZMC::FP64Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006408 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006409 }
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006410 }
6411 }
6412
6413 return MBB;
6414}
6415
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006416MachineBasicBlock *SystemZTargetLowering::emitLoadAndTestCmp0(
6417 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006418 MachineFunction &MF = *MBB->getParent();
6419 MachineRegisterInfo *MRI = &MF.getRegInfo();
6420 const SystemZInstrInfo *TII =
6421 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006422 DebugLoc DL = MI.getDebugLoc();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006423
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006424 unsigned SrcReg = MI.getOperand(0).getReg();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006425
6426 // Create new virtual register of the same class as source.
6427 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
6428 unsigned DstReg = MRI->createVirtualRegister(RC);
6429
6430 // Replace pseudo with a normal load-and-test that models the def as
6431 // well.
6432 BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg)
6433 .addReg(SrcReg);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006434 MI.eraseFromParent();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006435
6436 return MBB;
6437}
6438
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006439MachineBasicBlock *SystemZTargetLowering::EmitInstrWithCustomInserter(
6440 MachineInstr &MI, MachineBasicBlock *MBB) const {
6441 switch (MI.getOpcode()) {
Richard Sandiford7c5c0ea2013-10-01 13:10:16 +00006442 case SystemZ::Select32Mux:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006443 return emitSelect(MI, MBB,
6444 Subtarget.hasLoadStoreOnCond2()? SystemZ::LOCRMux : 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006445 case SystemZ::Select32:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006446 return emitSelect(MI, MBB, SystemZ::LOCR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006447 case SystemZ::Select64:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006448 return emitSelect(MI, MBB, SystemZ::LOCGR);
6449 case SystemZ::SelectF32:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006450 case SystemZ::SelectF64:
6451 case SystemZ::SelectF128:
Ulrich Weigandf2968d52017-07-17 17:44:20 +00006452 case SystemZ::SelectVR128:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006453 return emitSelect(MI, MBB, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006454
Richard Sandiford2896d042013-10-01 14:33:55 +00006455 case SystemZ::CondStore8Mux:
6456 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
6457 case SystemZ::CondStore8MuxInv:
6458 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
6459 case SystemZ::CondStore16Mux:
6460 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
6461 case SystemZ::CondStore16MuxInv:
6462 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
Ulrich Weigand524f2762016-11-28 13:34:08 +00006463 case SystemZ::CondStore32Mux:
6464 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, false);
6465 case SystemZ::CondStore32MuxInv:
6466 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006467 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006468 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006469 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006470 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006471 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006472 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006473 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006474 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006475 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006476 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006477 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006478 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006479 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006480 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006481 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006482 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006483 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006484 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006485 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006486 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006487 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006488 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006489 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006490 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006491
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00006492 case SystemZ::PAIR128:
6493 return emitPair128(MI, MBB);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006494 case SystemZ::AEXT128:
6495 return emitExt128(MI, MBB, false);
6496 case SystemZ::ZEXT128:
6497 return emitExt128(MI, MBB, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006498
6499 case SystemZ::ATOMIC_SWAPW:
6500 return emitAtomicLoadBinary(MI, MBB, 0, 0);
6501 case SystemZ::ATOMIC_SWAP_32:
6502 return emitAtomicLoadBinary(MI, MBB, 0, 32);
6503 case SystemZ::ATOMIC_SWAP_64:
6504 return emitAtomicLoadBinary(MI, MBB, 0, 64);
6505
6506 case SystemZ::ATOMIC_LOADW_AR:
6507 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
6508 case SystemZ::ATOMIC_LOADW_AFI:
6509 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
6510 case SystemZ::ATOMIC_LOAD_AR:
6511 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
6512 case SystemZ::ATOMIC_LOAD_AHI:
6513 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
6514 case SystemZ::ATOMIC_LOAD_AFI:
6515 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
6516 case SystemZ::ATOMIC_LOAD_AGR:
6517 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
6518 case SystemZ::ATOMIC_LOAD_AGHI:
6519 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
6520 case SystemZ::ATOMIC_LOAD_AGFI:
6521 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
6522
6523 case SystemZ::ATOMIC_LOADW_SR:
6524 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
6525 case SystemZ::ATOMIC_LOAD_SR:
6526 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
6527 case SystemZ::ATOMIC_LOAD_SGR:
6528 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
6529
6530 case SystemZ::ATOMIC_LOADW_NR:
6531 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
6532 case SystemZ::ATOMIC_LOADW_NILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00006533 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006534 case SystemZ::ATOMIC_LOAD_NR:
6535 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006536 case SystemZ::ATOMIC_LOAD_NILL:
6537 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
6538 case SystemZ::ATOMIC_LOAD_NILH:
6539 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
6540 case SystemZ::ATOMIC_LOAD_NILF:
6541 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006542 case SystemZ::ATOMIC_LOAD_NGR:
6543 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006544 case SystemZ::ATOMIC_LOAD_NILL64:
6545 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
6546 case SystemZ::ATOMIC_LOAD_NILH64:
6547 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00006548 case SystemZ::ATOMIC_LOAD_NIHL64:
6549 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
6550 case SystemZ::ATOMIC_LOAD_NIHH64:
6551 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006552 case SystemZ::ATOMIC_LOAD_NILF64:
6553 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00006554 case SystemZ::ATOMIC_LOAD_NIHF64:
6555 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006556
6557 case SystemZ::ATOMIC_LOADW_OR:
6558 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
6559 case SystemZ::ATOMIC_LOADW_OILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00006560 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006561 case SystemZ::ATOMIC_LOAD_OR:
6562 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006563 case SystemZ::ATOMIC_LOAD_OILL:
6564 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
6565 case SystemZ::ATOMIC_LOAD_OILH:
6566 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
6567 case SystemZ::ATOMIC_LOAD_OILF:
6568 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006569 case SystemZ::ATOMIC_LOAD_OGR:
6570 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006571 case SystemZ::ATOMIC_LOAD_OILL64:
6572 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
6573 case SystemZ::ATOMIC_LOAD_OILH64:
6574 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00006575 case SystemZ::ATOMIC_LOAD_OIHL64:
6576 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
6577 case SystemZ::ATOMIC_LOAD_OIHH64:
6578 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006579 case SystemZ::ATOMIC_LOAD_OILF64:
6580 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00006581 case SystemZ::ATOMIC_LOAD_OIHF64:
6582 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006583
6584 case SystemZ::ATOMIC_LOADW_XR:
6585 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
6586 case SystemZ::ATOMIC_LOADW_XILF:
Richard Sandiford652784e2013-09-25 11:11:53 +00006587 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006588 case SystemZ::ATOMIC_LOAD_XR:
6589 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006590 case SystemZ::ATOMIC_LOAD_XILF:
6591 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006592 case SystemZ::ATOMIC_LOAD_XGR:
6593 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006594 case SystemZ::ATOMIC_LOAD_XILF64:
6595 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
Richard Sandiford5718dac2013-10-01 14:08:44 +00006596 case SystemZ::ATOMIC_LOAD_XIHF64:
6597 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006598
6599 case SystemZ::ATOMIC_LOADW_NRi:
6600 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
6601 case SystemZ::ATOMIC_LOADW_NILHi:
Richard Sandiford652784e2013-09-25 11:11:53 +00006602 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006603 case SystemZ::ATOMIC_LOAD_NRi:
6604 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006605 case SystemZ::ATOMIC_LOAD_NILLi:
6606 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
6607 case SystemZ::ATOMIC_LOAD_NILHi:
6608 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
6609 case SystemZ::ATOMIC_LOAD_NILFi:
6610 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006611 case SystemZ::ATOMIC_LOAD_NGRi:
6612 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006613 case SystemZ::ATOMIC_LOAD_NILL64i:
6614 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
6615 case SystemZ::ATOMIC_LOAD_NILH64i:
6616 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00006617 case SystemZ::ATOMIC_LOAD_NIHL64i:
6618 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
6619 case SystemZ::ATOMIC_LOAD_NIHH64i:
6620 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006621 case SystemZ::ATOMIC_LOAD_NILF64i:
6622 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00006623 case SystemZ::ATOMIC_LOAD_NIHF64i:
6624 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006625
6626 case SystemZ::ATOMIC_LOADW_MIN:
6627 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6628 SystemZ::CCMASK_CMP_LE, 0);
6629 case SystemZ::ATOMIC_LOAD_MIN_32:
6630 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6631 SystemZ::CCMASK_CMP_LE, 32);
6632 case SystemZ::ATOMIC_LOAD_MIN_64:
6633 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
6634 SystemZ::CCMASK_CMP_LE, 64);
6635
6636 case SystemZ::ATOMIC_LOADW_MAX:
6637 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6638 SystemZ::CCMASK_CMP_GE, 0);
6639 case SystemZ::ATOMIC_LOAD_MAX_32:
6640 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6641 SystemZ::CCMASK_CMP_GE, 32);
6642 case SystemZ::ATOMIC_LOAD_MAX_64:
6643 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
6644 SystemZ::CCMASK_CMP_GE, 64);
6645
6646 case SystemZ::ATOMIC_LOADW_UMIN:
6647 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6648 SystemZ::CCMASK_CMP_LE, 0);
6649 case SystemZ::ATOMIC_LOAD_UMIN_32:
6650 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6651 SystemZ::CCMASK_CMP_LE, 32);
6652 case SystemZ::ATOMIC_LOAD_UMIN_64:
6653 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6654 SystemZ::CCMASK_CMP_LE, 64);
6655
6656 case SystemZ::ATOMIC_LOADW_UMAX:
6657 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6658 SystemZ::CCMASK_CMP_GE, 0);
6659 case SystemZ::ATOMIC_LOAD_UMAX_32:
6660 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6661 SystemZ::CCMASK_CMP_GE, 32);
6662 case SystemZ::ATOMIC_LOAD_UMAX_64:
6663 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6664 SystemZ::CCMASK_CMP_GE, 64);
6665
6666 case SystemZ::ATOMIC_CMP_SWAPW:
6667 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006668 case SystemZ::MVCSequence:
6669 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006670 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00006671 case SystemZ::NCSequence:
6672 case SystemZ::NCLoop:
6673 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
6674 case SystemZ::OCSequence:
6675 case SystemZ::OCLoop:
6676 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
6677 case SystemZ::XCSequence:
6678 case SystemZ::XCLoop:
6679 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006680 case SystemZ::CLCSequence:
6681 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006682 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00006683 case SystemZ::CLSTLoop:
6684 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00006685 case SystemZ::MVSTLoop:
6686 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00006687 case SystemZ::SRSTLoop:
6688 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006689 case SystemZ::TBEGIN:
6690 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false);
6691 case SystemZ::TBEGIN_nofloat:
6692 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true);
6693 case SystemZ::TBEGINC:
6694 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true);
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006695 case SystemZ::LTEBRCompare_VecPseudo:
6696 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR);
6697 case SystemZ::LTDBRCompare_VecPseudo:
6698 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR);
6699 case SystemZ::LTXBRCompare_VecPseudo:
6700 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR);
6701
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006702 default:
6703 llvm_unreachable("Unexpected instr type to insert");
6704 }
6705}
Jonas Paulsson11d251c2017-05-10 13:03:25 +00006706
6707// This is only used by the isel schedulers, and is needed only to prevent
6708// compiler from crashing when list-ilp is used.
6709const TargetRegisterClass *
6710SystemZTargetLowering::getRepRegClassFor(MVT VT) const {
6711 if (VT == MVT::Untyped)
6712 return &SystemZ::ADDR128BitRegClass;
6713 return TargetLowering::getRepRegClassFor(VT);
6714}