blob: 0d29676f5007a3427391631a56e146d017048ac9 [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 {
Richard Sandifordd420f732013-12-13 15:28:45 +000034// Represents information about a comparison.
35struct Comparison {
36 Comparison(SDValue Op0In, SDValue Op1In)
37 : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {}
38
39 // The operands to the comparison.
40 SDValue Op0, Op1;
41
42 // The opcode that should be used to compare Op0 and Op1.
43 unsigned Opcode;
44
45 // A SystemZICMP value. Only used for integer comparisons.
46 unsigned ICmpType;
47
48 // The mask of CC values that Opcode can produce.
49 unsigned CCValid;
50
51 // The mask of CC values for which the original condition is true.
52 unsigned CCMask;
53};
Richard Sandifordc2312692014-03-06 10:38:30 +000054} // end anonymous namespace
Richard Sandifordf722a8e302013-10-16 11:10:55 +000055
Ulrich Weigand5f613df2013-05-06 16:15:19 +000056// Classify VT as either 32 or 64 bit.
57static bool is32Bit(EVT VT) {
58 switch (VT.getSimpleVT().SimpleTy) {
59 case MVT::i32:
60 return true;
61 case MVT::i64:
62 return false;
63 default:
64 llvm_unreachable("Unsupported type");
65 }
66}
67
68// Return a version of MachineOperand that can be safely used before the
69// final use.
70static MachineOperand earlyUseOperand(MachineOperand Op) {
71 if (Op.isReg())
72 Op.setIsKill(false);
73 return Op;
74}
75
Mehdi Amini44ede332015-07-09 02:09:04 +000076SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM,
Eric Christophera6734172015-01-31 00:06:45 +000077 const SystemZSubtarget &STI)
Mehdi Amini44ede332015-07-09 02:09:04 +000078 : TargetLowering(TM), Subtarget(STI) {
Mehdi Amini26d48132015-07-24 16:04:22 +000079 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
Ulrich Weigand5f613df2013-05-06 16:15:19 +000080
81 // Set up the register classes.
Richard Sandiford0755c932013-10-01 11:26:28 +000082 if (Subtarget.hasHighWord())
83 addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass);
84 else
85 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
Ulrich Weigand49506d72015-05-05 19:28:34 +000086 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
87 if (Subtarget.hasVector()) {
88 addRegisterClass(MVT::f32, &SystemZ::VR32BitRegClass);
89 addRegisterClass(MVT::f64, &SystemZ::VR64BitRegClass);
90 } else {
91 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
92 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
93 }
Ulrich Weigandf2968d52017-07-17 17:44:20 +000094 if (Subtarget.hasVectorEnhancements1())
95 addRegisterClass(MVT::f128, &SystemZ::VR128BitRegClass);
96 else
97 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
Ulrich Weigand5f613df2013-05-06 16:15:19 +000098
Ulrich Weigandce4c1092015-05-05 19:25:42 +000099 if (Subtarget.hasVector()) {
100 addRegisterClass(MVT::v16i8, &SystemZ::VR128BitRegClass);
101 addRegisterClass(MVT::v8i16, &SystemZ::VR128BitRegClass);
102 addRegisterClass(MVT::v4i32, &SystemZ::VR128BitRegClass);
103 addRegisterClass(MVT::v2i64, &SystemZ::VR128BitRegClass);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000104 addRegisterClass(MVT::v4f32, &SystemZ::VR128BitRegClass);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000105 addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000106 }
107
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000108 // Compute derived properties from the register classes
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000109 computeRegisterProperties(Subtarget.getRegisterInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000110
111 // Set up special registers.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000112 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
113
114 // TODO: It may be better to default to latency-oriented scheduling, however
115 // LLVM's current latency-oriented scheduler can't handle physreg definitions
Richard Sandiford14a44492013-05-22 13:38:45 +0000116 // such as SystemZ has with CC, so set this to the register-pressure
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000117 // scheduler, because it can.
118 setSchedulingPreference(Sched::RegPressure);
119
120 setBooleanContents(ZeroOrOneBooleanContent);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000121 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000122
123 // Instructions are strings of 2-byte aligned 2-byte values.
124 setMinFunctionAlignment(2);
125
126 // Handle operations that are handled in a similar way for all types.
127 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
128 I <= MVT::LAST_FP_VALUETYPE;
129 ++I) {
130 MVT VT = MVT::SimpleValueType(I);
131 if (isTypeLegal(VT)) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000132 // Lower SET_CC into an IPM-based sequence.
133 setOperationAction(ISD::SETCC, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000134
135 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
136 setOperationAction(ISD::SELECT, VT, Expand);
137
138 // Lower SELECT_CC and BR_CC into separate comparisons and branches.
139 setOperationAction(ISD::SELECT_CC, VT, Custom);
140 setOperationAction(ISD::BR_CC, VT, Custom);
141 }
142 }
143
144 // Expand jump table branches as address arithmetic followed by an
145 // indirect jump.
146 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
147
148 // Expand BRCOND into a BR_CC (see above).
149 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
150
151 // Handle integer types.
152 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
153 I <= MVT::LAST_INTEGER_VALUETYPE;
154 ++I) {
155 MVT VT = MVT::SimpleValueType(I);
156 if (isTypeLegal(VT)) {
157 // Expand individual DIV and REMs into DIVREMs.
158 setOperationAction(ISD::SDIV, VT, Expand);
159 setOperationAction(ISD::UDIV, VT, Expand);
160 setOperationAction(ISD::SREM, VT, Expand);
161 setOperationAction(ISD::UREM, VT, Expand);
162 setOperationAction(ISD::SDIVREM, VT, Custom);
163 setOperationAction(ISD::UDIVREM, VT, Custom);
164
Richard Sandifordbef3d7a2013-12-10 10:49:34 +0000165 // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and
166 // stores, putting a serialization instruction after the stores.
167 setOperationAction(ISD::ATOMIC_LOAD, VT, Custom);
168 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000169
Richard Sandiford41350a52013-12-24 15:18:04 +0000170 // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are
171 // available, or if the operand is constant.
172 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
173
Ulrich Weigandb4012182015-03-31 12:56:33 +0000174 // Use POPCNT on z196 and above.
175 if (Subtarget.hasPopulationCount())
176 setOperationAction(ISD::CTPOP, VT, Custom);
177 else
178 setOperationAction(ISD::CTPOP, VT, Expand);
179
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000180 // No special instructions for these.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000181 setOperationAction(ISD::CTTZ, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000182 setOperationAction(ISD::ROTR, VT, Expand);
183
Richard Sandiford7d86e472013-08-21 09:34:56 +0000184 // Use *MUL_LOHI where possible instead of MULH*.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000185 setOperationAction(ISD::MULHS, VT, Expand);
186 setOperationAction(ISD::MULHU, VT, Expand);
Richard Sandiford7d86e472013-08-21 09:34:56 +0000187 setOperationAction(ISD::SMUL_LOHI, VT, Custom);
188 setOperationAction(ISD::UMUL_LOHI, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000189
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000190 // Only z196 and above have native support for conversions to unsigned.
Jonas Paulssonb7a2ef82017-02-02 15:42:14 +0000191 // On z10, promoting to i64 doesn't generate an inexact condition for
192 // values that are outside the i32 range but in the i64 range, so use
193 // the default expansion.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000194 if (!Subtarget.hasFPExtension())
195 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000196 }
197 }
198
199 // Type legalization will convert 8- and 16-bit atomic operations into
200 // forms that operate on i32s (but still keeping the original memory VT).
201 // Lower them into full i32 operations.
202 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom);
203 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom);
204 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
205 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom);
206 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom);
207 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom);
208 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
209 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom);
210 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom);
211 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
212 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000213
Ulrich Weiganda11f63a2017-08-04 18:57:58 +0000214 // Even though i128 is not a legal type, we still need to custom lower
215 // the atomic operations in order to exploit SystemZ instructions.
216 setOperationAction(ISD::ATOMIC_LOAD, MVT::i128, Custom);
217 setOperationAction(ISD::ATOMIC_STORE, MVT::i128, Custom);
Ulrich Weigand0f1de042017-09-28 16:22:54 +0000218
219 // We can use the CC result of compare-and-swap to implement
220 // the "success" result of ATOMIC_CMP_SWAP_WITH_SUCCESS.
221 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Custom);
222 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Custom);
223 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i128, Custom);
Ulrich Weiganda11f63a2017-08-04 18:57:58 +0000224
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +0000225 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
226
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000227 // Traps are legal, as we will convert them to "j .+2".
228 setOperationAction(ISD::TRAP, MVT::Other, Legal);
229
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000230 // z10 has instructions for signed but not unsigned FP conversion.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000231 // Handle unsigned 32-bit types as signed 64-bit types.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000232 if (!Subtarget.hasFPExtension()) {
233 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
234 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
235 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000236
237 // We have native support for a 64-bit CTLZ, via FLOGR.
238 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
239 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
240
241 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
242 setOperationAction(ISD::OR, MVT::i64, Custom);
243
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000244 // FIXME: Can we support these natively?
245 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
246 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
247 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
248
249 // We have native instructions for i8, i16 and i32 extensions, but not i1.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000250 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000251 for (MVT VT : MVT::integer_valuetypes()) {
252 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
253 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
254 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
255 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000256
257 // Handle the various types of symbolic address.
258 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
259 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
260 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
261 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
262 setOperationAction(ISD::JumpTable, PtrVT, Custom);
263
264 // We need to handle dynamic allocations specially because of the
265 // 160-byte area at the bottom of the stack.
266 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +0000267 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, PtrVT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000268
269 // Use custom expanders so that we can force the function to use
270 // a frame pointer.
271 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
272 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
273
Richard Sandiford03481332013-08-23 11:36:42 +0000274 // Handle prefetches with PFD or PFDRL.
275 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
276
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000277 for (MVT VT : MVT::vector_valuetypes()) {
278 // Assume by default that all vector operations need to be expanded.
279 for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode)
280 if (getOperationAction(Opcode, VT) == Legal)
281 setOperationAction(Opcode, VT, Expand);
282
283 // Likewise all truncating stores and extending loads.
284 for (MVT InnerVT : MVT::vector_valuetypes()) {
285 setTruncStoreAction(VT, InnerVT, Expand);
286 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
287 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
288 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
289 }
290
291 if (isTypeLegal(VT)) {
292 // These operations are legal for anything that can be stored in a
293 // vector register, even if there is no native support for the format
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000294 // as such. In particular, we can do these for v4f32 even though there
295 // are no specific instructions for that format.
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000296 setOperationAction(ISD::LOAD, VT, Legal);
297 setOperationAction(ISD::STORE, VT, Legal);
298 setOperationAction(ISD::VSELECT, VT, Legal);
299 setOperationAction(ISD::BITCAST, VT, Legal);
300 setOperationAction(ISD::UNDEF, VT, Legal);
301
302 // Likewise, except that we need to replace the nodes with something
303 // more specific.
304 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
305 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
306 }
307 }
308
309 // Handle integer vector types.
310 for (MVT VT : MVT::integer_vector_valuetypes()) {
311 if (isTypeLegal(VT)) {
312 // These operations have direct equivalents.
313 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal);
314 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal);
315 setOperationAction(ISD::ADD, VT, Legal);
316 setOperationAction(ISD::SUB, VT, Legal);
317 if (VT != MVT::v2i64)
318 setOperationAction(ISD::MUL, VT, Legal);
319 setOperationAction(ISD::AND, VT, Legal);
320 setOperationAction(ISD::OR, VT, Legal);
321 setOperationAction(ISD::XOR, VT, Legal);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000322 if (Subtarget.hasVectorEnhancements1())
323 setOperationAction(ISD::CTPOP, VT, Legal);
324 else
325 setOperationAction(ISD::CTPOP, VT, Custom);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000326 setOperationAction(ISD::CTTZ, VT, Legal);
327 setOperationAction(ISD::CTLZ, VT, Legal);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000328
329 // Convert a GPR scalar to a vector by inserting it into element 0.
330 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
331
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000332 // Use a series of unpacks for extensions.
333 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Custom);
334 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Custom);
335
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000336 // Detect shifts by a scalar amount and convert them into
337 // V*_BY_SCALAR.
338 setOperationAction(ISD::SHL, VT, Custom);
339 setOperationAction(ISD::SRA, VT, Custom);
340 setOperationAction(ISD::SRL, VT, Custom);
341
342 // At present ROTL isn't matched by DAGCombiner. ROTR should be
343 // converted into ROTL.
344 setOperationAction(ISD::ROTL, VT, Expand);
345 setOperationAction(ISD::ROTR, VT, Expand);
346
347 // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands
348 // and inverting the result as necessary.
349 setOperationAction(ISD::SETCC, VT, Custom);
350 }
351 }
352
Ulrich Weigandcd808232015-05-05 19:26:48 +0000353 if (Subtarget.hasVector()) {
354 // There should be no need to check for float types other than v2f64
355 // since <2 x f32> isn't a legal type.
356 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000357 setOperationAction(ISD::FP_TO_SINT, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000358 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000359 setOperationAction(ISD::FP_TO_UINT, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000360 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000361 setOperationAction(ISD::SINT_TO_FP, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000362 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000363 setOperationAction(ISD::UINT_TO_FP, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000364 }
365
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000366 // Handle floating-point types.
367 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
368 I <= MVT::LAST_FP_VALUETYPE;
369 ++I) {
370 MVT VT = MVT::SimpleValueType(I);
371 if (isTypeLegal(VT)) {
372 // We can use FI for FRINT.
373 setOperationAction(ISD::FRINT, VT, Legal);
374
Richard Sandifordaf5f66a2013-08-21 09:04:20 +0000375 // We can use the extended form of FI for other rounding operations.
376 if (Subtarget.hasFPExtension()) {
377 setOperationAction(ISD::FNEARBYINT, VT, Legal);
378 setOperationAction(ISD::FFLOOR, VT, Legal);
379 setOperationAction(ISD::FCEIL, VT, Legal);
380 setOperationAction(ISD::FTRUNC, VT, Legal);
381 setOperationAction(ISD::FROUND, VT, Legal);
382 }
383
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000384 // No special instructions for these.
385 setOperationAction(ISD::FSIN, VT, Expand);
386 setOperationAction(ISD::FCOS, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000387 setOperationAction(ISD::FSINCOS, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000388 setOperationAction(ISD::FREM, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000389 setOperationAction(ISD::FPOW, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000390 }
391 }
392
Ulrich Weigandcd808232015-05-05 19:26:48 +0000393 // Handle floating-point vector types.
394 if (Subtarget.hasVector()) {
395 // Scalar-to-vector conversion is just a subreg.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000396 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000397 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
398
399 // Some insertions and extractions can be done directly but others
400 // need to go via integers.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000401 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000402 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000403 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000404 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
405
406 // These operations have direct equivalents.
407 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
408 setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
409 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
410 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
411 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
412 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
413 setOperationAction(ISD::FABS, MVT::v2f64, Legal);
414 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
415 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
416 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
417 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
418 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
419 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
420 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
421 }
422
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000423 // The vector enhancements facility 1 has instructions for these.
424 if (Subtarget.hasVectorEnhancements1()) {
Ulrich Weigand33435c42017-07-17 17:42:48 +0000425 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
426 setOperationAction(ISD::FNEG, MVT::v4f32, Legal);
427 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
428 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
429 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
430 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
431 setOperationAction(ISD::FABS, MVT::v4f32, Legal);
432 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
433 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
434 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
435 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
436 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
437 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
438 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
439
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000440 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
441 setOperationAction(ISD::FMAXNAN, MVT::f64, Legal);
442 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
443 setOperationAction(ISD::FMINNAN, MVT::f64, Legal);
444
445 setOperationAction(ISD::FMAXNUM, MVT::v2f64, Legal);
446 setOperationAction(ISD::FMAXNAN, MVT::v2f64, Legal);
447 setOperationAction(ISD::FMINNUM, MVT::v2f64, Legal);
448 setOperationAction(ISD::FMINNAN, MVT::v2f64, Legal);
Ulrich Weigand33435c42017-07-17 17:42:48 +0000449
450 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
451 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal);
452 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
453 setOperationAction(ISD::FMINNAN, MVT::f32, Legal);
454
455 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
456 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal);
457 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
458 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal);
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000459
460 setOperationAction(ISD::FMAXNUM, MVT::f128, Legal);
461 setOperationAction(ISD::FMAXNAN, MVT::f128, Legal);
462 setOperationAction(ISD::FMINNUM, MVT::f128, Legal);
463 setOperationAction(ISD::FMINNAN, MVT::f128, Legal);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000464 }
465
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000466 // We have fused multiply-addition for f32 and f64 but not f128.
467 setOperationAction(ISD::FMA, MVT::f32, Legal);
468 setOperationAction(ISD::FMA, MVT::f64, Legal);
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000469 if (Subtarget.hasVectorEnhancements1())
470 setOperationAction(ISD::FMA, MVT::f128, Legal);
471 else
472 setOperationAction(ISD::FMA, MVT::f128, Expand);
473
474 // We don't have a copysign instruction on vector registers.
475 if (Subtarget.hasVectorEnhancements1())
476 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000477
478 // Needed so that we don't try to implement f128 constant loads using
479 // a load-and-extend of a f80 constant (in cases where the constant
480 // would fit in an f80).
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000481 for (MVT VT : MVT::fp_valuetypes())
482 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000483
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000484 // We don't have extending load instruction on vector registers.
485 if (Subtarget.hasVectorEnhancements1()) {
486 setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f32, Expand);
487 setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f64, Expand);
488 }
489
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000490 // Floating-point truncation and stores need to be done separately.
491 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
492 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
493 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
494
495 // We have 64-bit FPR<->GPR moves, but need special handling for
496 // 32-bit forms.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000497 if (!Subtarget.hasVector()) {
498 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
499 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
500 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000501
502 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
503 // structure, but VAEND is a no-op.
504 setOperationAction(ISD::VASTART, MVT::Other, Custom);
505 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
506 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Richard Sandifordd131ff82013-07-08 09:35:23 +0000507
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000508 // Codes for which we want to perform some z-specific combinations.
Ulrich Weigand849a59f2018-01-19 20:52:04 +0000509 setTargetDAGCombine(ISD::ZERO_EXTEND);
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000510 setTargetDAGCombine(ISD::SIGN_EXTEND);
Ulrich Weigand849a59f2018-01-19 20:52:04 +0000511 setTargetDAGCombine(ISD::SIGN_EXTEND_INREG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000512 setTargetDAGCombine(ISD::STORE);
513 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000514 setTargetDAGCombine(ISD::FP_ROUND);
Bryan Chan28b759c2016-05-16 20:32:22 +0000515 setTargetDAGCombine(ISD::BSWAP);
Elliot Colpbc2cfc22016-07-06 18:13:11 +0000516 setTargetDAGCombine(ISD::SHL);
517 setTargetDAGCombine(ISD::SRA);
518 setTargetDAGCombine(ISD::SRL);
519 setTargetDAGCombine(ISD::ROTL);
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000520
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000521 // Handle intrinsics.
522 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Ulrich Weigandc1708b22015-05-05 19:31:09 +0000523 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000524
Richard Sandifordd131ff82013-07-08 09:35:23 +0000525 // We want to use MVC in preference to even a single load/store pair.
526 MaxStoresPerMemcpy = 0;
527 MaxStoresPerMemcpyOptSize = 0;
Richard Sandiford47660c12013-07-09 09:32:42 +0000528
529 // The main memset sequence is a byte store followed by an MVC.
530 // Two STC or MV..I stores win over that, but the kind of fused stores
531 // generated by target-independent code don't when the byte value is
532 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
533 // than "STC;MVC". Handle the choice in target-specific code instead.
534 MaxStoresPerMemset = 0;
535 MaxStoresPerMemsetOptSize = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000536}
537
Mehdi Amini44ede332015-07-09 02:09:04 +0000538EVT SystemZTargetLowering::getSetCCResultType(const DataLayout &DL,
539 LLVMContext &, EVT VT) const {
Richard Sandifordabc010b2013-11-06 12:16:02 +0000540 if (!VT.isVector())
541 return MVT::i32;
542 return VT.changeVectorElementTypeToInteger();
543}
544
545bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
Stephen Lin73de7bf2013-07-09 18:16:56 +0000546 VT = VT.getScalarType();
547
548 if (!VT.isSimple())
549 return false;
550
551 switch (VT.getSimpleVT().SimpleTy) {
552 case MVT::f32:
553 case MVT::f64:
554 return true;
555 case MVT::f128:
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000556 return Subtarget.hasVectorEnhancements1();
Stephen Lin73de7bf2013-07-09 18:16:56 +0000557 default:
558 break;
559 }
560
561 return false;
562}
563
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000564bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
565 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
566 return Imm.isZero() || Imm.isNegZero();
567}
568
Ulrich Weigand1f6666a2015-03-31 12:52:27 +0000569bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
570 // We can use CGFI or CLGFI.
571 return isInt<32>(Imm) || isUInt<32>(Imm);
572}
573
574bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const {
575 // We can use ALGFI or SLGFI.
576 return isUInt<32>(Imm) || isUInt<32>(-Imm);
577}
578
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000579bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
580 unsigned,
581 unsigned,
582 bool *Fast) const {
Richard Sandiford46af5a22013-05-30 09:45:42 +0000583 // Unaligned accesses should never be slower than the expanded version.
584 // We check specifically for aligned accesses in the few cases where
585 // they are required.
586 if (Fast)
587 *Fast = true;
588 return true;
589}
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +0000590
Jonas Paulsson024e3192017-07-21 11:59:37 +0000591// Information about the addressing mode for a memory access.
592struct AddressingMode {
593 // True if a long displacement is supported.
594 bool LongDisplacement;
595
596 // True if use of index register is supported.
597 bool IndexReg;
598
599 AddressingMode(bool LongDispl, bool IdxReg) :
600 LongDisplacement(LongDispl), IndexReg(IdxReg) {}
601};
602
603// Return the desired addressing mode for a Load which has only one use (in
604// the same block) which is a Store.
605static AddressingMode getLoadStoreAddrMode(bool HasVector,
606 Type *Ty) {
607 // With vector support a Load->Store combination may be combined to either
608 // an MVC or vector operations and it seems to work best to allow the
609 // vector addressing mode.
610 if (HasVector)
611 return AddressingMode(false/*LongDispl*/, true/*IdxReg*/);
612
613 // Otherwise only the MVC case is special.
614 bool MVC = Ty->isIntegerTy(8);
615 return AddressingMode(!MVC/*LongDispl*/, !MVC/*IdxReg*/);
616}
617
618// Return the addressing mode which seems most desirable given an LLVM
619// Instruction pointer.
620static AddressingMode
621supportedAddressingMode(Instruction *I, bool HasVector) {
622 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
623 switch (II->getIntrinsicID()) {
624 default: break;
625 case Intrinsic::memset:
626 case Intrinsic::memmove:
627 case Intrinsic::memcpy:
628 return AddressingMode(false/*LongDispl*/, false/*IdxReg*/);
629 }
630 }
631
632 if (isa<LoadInst>(I) && I->hasOneUse()) {
633 auto *SingleUser = dyn_cast<Instruction>(*I->user_begin());
634 if (SingleUser->getParent() == I->getParent()) {
635 if (isa<ICmpInst>(SingleUser)) {
636 if (auto *C = dyn_cast<ConstantInt>(SingleUser->getOperand(1)))
637 if (isInt<16>(C->getSExtValue()) || isUInt<16>(C->getZExtValue()))
638 // Comparison of memory with 16 bit signed / unsigned immediate
639 return AddressingMode(false/*LongDispl*/, false/*IdxReg*/);
640 } else if (isa<StoreInst>(SingleUser))
641 // Load->Store
642 return getLoadStoreAddrMode(HasVector, I->getType());
643 }
644 } else if (auto *StoreI = dyn_cast<StoreInst>(I)) {
645 if (auto *LoadI = dyn_cast<LoadInst>(StoreI->getValueOperand()))
646 if (LoadI->hasOneUse() && LoadI->getParent() == I->getParent())
647 // Load->Store
648 return getLoadStoreAddrMode(HasVector, LoadI->getType());
649 }
650
651 if (HasVector && (isa<LoadInst>(I) || isa<StoreInst>(I))) {
652
653 // * Use LDE instead of LE/LEY for z13 to avoid partial register
654 // dependencies (LDE only supports small offsets).
655 // * Utilize the vector registers to hold floating point
656 // values (vector load / store instructions only support small
657 // offsets).
658
659 Type *MemAccessTy = (isa<LoadInst>(I) ? I->getType() :
660 I->getOperand(0)->getType());
661 bool IsFPAccess = MemAccessTy->isFloatingPointTy();
662 bool IsVectorAccess = MemAccessTy->isVectorTy();
663
664 // A store of an extracted vector element will be combined into a VSTE type
665 // instruction.
666 if (!IsVectorAccess && isa<StoreInst>(I)) {
667 Value *DataOp = I->getOperand(0);
668 if (isa<ExtractElementInst>(DataOp))
669 IsVectorAccess = true;
670 }
671
672 // A load which gets inserted into a vector element will be combined into a
673 // VLE type instruction.
674 if (!IsVectorAccess && isa<LoadInst>(I) && I->hasOneUse()) {
675 User *LoadUser = *I->user_begin();
676 if (isa<InsertElementInst>(LoadUser))
677 IsVectorAccess = true;
678 }
679
680 if (IsFPAccess || IsVectorAccess)
681 return AddressingMode(false/*LongDispl*/, true/*IdxReg*/);
682 }
683
684 return AddressingMode(true/*LongDispl*/, true/*IdxReg*/);
685}
686
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000687bool SystemZTargetLowering::isLegalAddressingMode(const DataLayout &DL,
Jonas Paulsson6228aed2017-08-09 11:28:01 +0000688 const AddrMode &AM, Type *Ty, unsigned AS, Instruction *I) const {
Richard Sandiford791bea42013-07-31 12:58:26 +0000689 // Punt on globals for now, although they can be used in limited
690 // RELATIVE LONG cases.
691 if (AM.BaseGV)
692 return false;
693
694 // Require a 20-bit signed offset.
695 if (!isInt<20>(AM.BaseOffs))
696 return false;
697
Jonas Paulsson6228aed2017-08-09 11:28:01 +0000698 AddressingMode SupportedAM(true, true);
699 if (I != nullptr)
700 SupportedAM = supportedAddressingMode(I, Subtarget.hasVector());
701
702 if (!SupportedAM.LongDisplacement && !isUInt<12>(AM.BaseOffs))
703 return false;
704
705 if (!SupportedAM.IndexReg)
Jonas Paulsson024e3192017-07-21 11:59:37 +0000706 // No indexing allowed.
707 return AM.Scale == 0;
708 else
709 // Indexing is OK but no scale factor can be applied.
710 return AM.Scale == 0 || AM.Scale == 1;
Richard Sandiford791bea42013-07-31 12:58:26 +0000711}
712
Richard Sandiford709bda62013-08-19 12:42:31 +0000713bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
714 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
715 return false;
716 unsigned FromBits = FromType->getPrimitiveSizeInBits();
717 unsigned ToBits = ToType->getPrimitiveSizeInBits();
718 return FromBits > ToBits;
719}
720
721bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
722 if (!FromVT.isInteger() || !ToVT.isInteger())
723 return false;
724 unsigned FromBits = FromVT.getSizeInBits();
725 unsigned ToBits = ToVT.getSizeInBits();
726 return FromBits > ToBits;
727}
728
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000729//===----------------------------------------------------------------------===//
730// Inline asm support
731//===----------------------------------------------------------------------===//
732
733TargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000734SystemZTargetLowering::getConstraintType(StringRef Constraint) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000735 if (Constraint.size() == 1) {
736 switch (Constraint[0]) {
737 case 'a': // Address register
738 case 'd': // Data register (equivalent to 'r')
739 case 'f': // Floating-point register
Richard Sandiford0755c932013-10-01 11:26:28 +0000740 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000741 case 'r': // General-purpose register
742 return C_RegisterClass;
743
744 case 'Q': // Memory with base and unsigned 12-bit displacement
745 case 'R': // Likewise, plus an index
746 case 'S': // Memory with base and signed 20-bit displacement
747 case 'T': // Likewise, plus an index
748 case 'm': // Equivalent to 'T'.
749 return C_Memory;
750
751 case 'I': // Unsigned 8-bit constant
752 case 'J': // Unsigned 12-bit constant
753 case 'K': // Signed 16-bit constant
754 case 'L': // Signed 20-bit displacement (on all targets we support)
755 case 'M': // 0x7fffffff
756 return C_Other;
757
758 default:
759 break;
760 }
761 }
762 return TargetLowering::getConstraintType(Constraint);
763}
764
765TargetLowering::ConstraintWeight SystemZTargetLowering::
766getSingleConstraintMatchWeight(AsmOperandInfo &info,
767 const char *constraint) const {
768 ConstraintWeight weight = CW_Invalid;
769 Value *CallOperandVal = info.CallOperandVal;
770 // If we don't have a value, we can't do a match,
771 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +0000772 if (!CallOperandVal)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000773 return CW_Default;
774 Type *type = CallOperandVal->getType();
775 // Look at the constraint type.
776 switch (*constraint) {
777 default:
778 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
779 break;
780
781 case 'a': // Address register
782 case 'd': // Data register (equivalent to 'r')
Richard Sandiford0755c932013-10-01 11:26:28 +0000783 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000784 case 'r': // General-purpose register
785 if (CallOperandVal->getType()->isIntegerTy())
786 weight = CW_Register;
787 break;
788
789 case 'f': // Floating-point register
790 if (type->isFloatingPointTy())
791 weight = CW_Register;
792 break;
793
794 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000795 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000796 if (isUInt<8>(C->getZExtValue()))
797 weight = CW_Constant;
798 break;
799
800 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000801 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000802 if (isUInt<12>(C->getZExtValue()))
803 weight = CW_Constant;
804 break;
805
806 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000807 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000808 if (isInt<16>(C->getSExtValue()))
809 weight = CW_Constant;
810 break;
811
812 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000813 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000814 if (isInt<20>(C->getSExtValue()))
815 weight = CW_Constant;
816 break;
817
818 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000819 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000820 if (C->getZExtValue() == 0x7fffffff)
821 weight = CW_Constant;
822 break;
823 }
824 return weight;
825}
826
Richard Sandifordb8204052013-07-12 09:08:12 +0000827// Parse a "{tNNN}" register constraint for which the register type "t"
828// has already been verified. MC is the class associated with "t" and
829// Map maps 0-based register numbers to LLVM register numbers.
830static std::pair<unsigned, const TargetRegisterClass *>
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000831parseRegisterNumber(StringRef Constraint, const TargetRegisterClass *RC,
832 const unsigned *Map) {
Richard Sandifordb8204052013-07-12 09:08:12 +0000833 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
834 if (isdigit(Constraint[2])) {
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000835 unsigned Index;
836 bool Failed =
837 Constraint.slice(2, Constraint.size() - 1).getAsInteger(10, Index);
838 if (!Failed && Index < 16 && Map[Index])
Richard Sandifordb8204052013-07-12 09:08:12 +0000839 return std::make_pair(Map[Index], RC);
840 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000841 return std::make_pair(0U, nullptr);
Richard Sandifordb8204052013-07-12 09:08:12 +0000842}
843
Eric Christopher11e4df72015-02-26 22:38:43 +0000844std::pair<unsigned, const TargetRegisterClass *>
845SystemZTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000846 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000847 if (Constraint.size() == 1) {
848 // GCC Constraint Letters
849 switch (Constraint[0]) {
850 default: break;
851 case 'd': // Data register (equivalent to 'r')
852 case 'r': // General-purpose register
853 if (VT == MVT::i64)
854 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
855 else if (VT == MVT::i128)
856 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
857 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
858
859 case 'a': // Address register
860 if (VT == MVT::i64)
861 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
862 else if (VT == MVT::i128)
863 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
864 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
865
Richard Sandiford0755c932013-10-01 11:26:28 +0000866 case 'h': // High-part register (an LLVM extension)
867 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
868
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000869 case 'f': // Floating-point register
870 if (VT == MVT::f64)
871 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
872 else if (VT == MVT::f128)
873 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
874 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
875 }
876 }
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000877 if (Constraint.size() > 0 && Constraint[0] == '{') {
Richard Sandifordb8204052013-07-12 09:08:12 +0000878 // We need to override the default register parsing for GPRs and FPRs
879 // because the interpretation depends on VT. The internal names of
880 // the registers are also different from the external names
881 // (F0D and F0S instead of F0, etc.).
882 if (Constraint[1] == 'r') {
883 if (VT == MVT::i32)
884 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
885 SystemZMC::GR32Regs);
886 if (VT == MVT::i128)
887 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
888 SystemZMC::GR128Regs);
889 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
890 SystemZMC::GR64Regs);
891 }
892 if (Constraint[1] == 'f') {
893 if (VT == MVT::f32)
894 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
895 SystemZMC::FP32Regs);
896 if (VT == MVT::f128)
897 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
898 SystemZMC::FP128Regs);
899 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
900 SystemZMC::FP64Regs);
901 }
902 }
Eric Christopher11e4df72015-02-26 22:38:43 +0000903 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000904}
905
906void SystemZTargetLowering::
907LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
908 std::vector<SDValue> &Ops,
909 SelectionDAG &DAG) const {
910 // Only support length 1 constraints for now.
911 if (Constraint.length() == 1) {
912 switch (Constraint[0]) {
913 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000914 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000915 if (isUInt<8>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000916 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000917 Op.getValueType()));
918 return;
919
920 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000921 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000922 if (isUInt<12>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000923 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000924 Op.getValueType()));
925 return;
926
927 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000928 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000929 if (isInt<16>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000930 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000931 Op.getValueType()));
932 return;
933
934 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000935 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000936 if (isInt<20>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000937 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000938 Op.getValueType()));
939 return;
940
941 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000942 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000943 if (C->getZExtValue() == 0x7fffffff)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000944 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000945 Op.getValueType()));
946 return;
947 }
948 }
949 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
950}
951
952//===----------------------------------------------------------------------===//
953// Calling conventions
954//===----------------------------------------------------------------------===//
955
956#include "SystemZGenCallingConv.inc"
957
Richard Sandiford709bda62013-08-19 12:42:31 +0000958bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
959 Type *ToType) const {
960 return isTruncateFree(FromType, ToType);
961}
962
Matt Arsenault31380752017-04-18 21:16:46 +0000963bool SystemZTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
Ulrich Weigand19d24d22015-11-13 13:00:27 +0000964 return CI->isTailCall();
Richard Sandiford709bda62013-08-19 12:42:31 +0000965}
966
Ulrich Weigand5211f9f2015-05-05 19:30:05 +0000967// We do not yet support 128-bit single-element vector types. If the user
968// attempts to use such types as function argument or return type, prefer
969// to error out instead of emitting code violating the ABI.
970static void VerifyVectorType(MVT VT, EVT ArgVT) {
971 if (ArgVT.isVector() && !VT.isVector())
972 report_fatal_error("Unsupported vector argument or return type");
973}
974
975static void VerifyVectorTypes(const SmallVectorImpl<ISD::InputArg> &Ins) {
976 for (unsigned i = 0; i < Ins.size(); ++i)
977 VerifyVectorType(Ins[i].VT, Ins[i].ArgVT);
978}
979
980static void VerifyVectorTypes(const SmallVectorImpl<ISD::OutputArg> &Outs) {
981 for (unsigned i = 0; i < Outs.size(); ++i)
982 VerifyVectorType(Outs[i].VT, Outs[i].ArgVT);
983}
984
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000985// Value is a value that has been passed to us in the location described by VA
986// (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
987// any loads onto Chain.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000988static SDValue convertLocVTToValVT(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000989 CCValAssign &VA, SDValue Chain,
990 SDValue Value) {
991 // If the argument has been promoted from a smaller type, insert an
992 // assertion to capture this.
993 if (VA.getLocInfo() == CCValAssign::SExt)
994 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
995 DAG.getValueType(VA.getValVT()));
996 else if (VA.getLocInfo() == CCValAssign::ZExt)
997 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
998 DAG.getValueType(VA.getValVT()));
999
1000 if (VA.isExtInLoc())
1001 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00001002 else if (VA.getLocInfo() == CCValAssign::BCvt) {
1003 // If this is a short vector argument loaded from the stack,
1004 // extend from i64 to full vector size and then bitcast.
1005 assert(VA.getLocVT() == MVT::i64);
1006 assert(VA.getValVT().isVector());
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001007 Value = DAG.getBuildVector(MVT::v2i64, DL, {Value, DAG.getUNDEF(MVT::i64)});
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00001008 Value = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Value);
1009 } else
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001010 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
1011 return Value;
1012}
1013
1014// Value is a value of type VA.getValVT() that we need to copy into
1015// the location described by VA. Return a copy of Value converted to
1016// VA.getValVT(). The caller is responsible for handling indirect values.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001017static SDValue convertValVTToLocVT(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001018 CCValAssign &VA, SDValue Value) {
1019 switch (VA.getLocInfo()) {
1020 case CCValAssign::SExt:
1021 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
1022 case CCValAssign::ZExt:
1023 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
1024 case CCValAssign::AExt:
1025 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00001026 case CCValAssign::BCvt:
1027 // If this is a short vector argument to be stored to the stack,
1028 // bitcast to v2i64 and then extract first element.
1029 assert(VA.getLocVT() == MVT::i64);
1030 assert(VA.getValVT().isVector());
1031 Value = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Value);
1032 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VA.getLocVT(), Value,
1033 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001034 case CCValAssign::Full:
1035 return Value;
1036 default:
1037 llvm_unreachable("Unhandled getLocInfo()");
1038 }
1039}
1040
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001041SDValue SystemZTargetLowering::LowerFormalArguments(
1042 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
1043 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1044 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001045 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001046 MachineFrameInfo &MFI = MF.getFrameInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001047 MachineRegisterInfo &MRI = MF.getRegInfo();
1048 SystemZMachineFunctionInfo *FuncInfo =
Eric Christophera6734172015-01-31 00:06:45 +00001049 MF.getInfo<SystemZMachineFunctionInfo>();
1050 auto *TFL =
1051 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering());
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001052 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001053
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001054 // Detect unsupported vector argument types.
1055 if (Subtarget.hasVector())
1056 VerifyVectorTypes(Ins);
1057
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001058 // Assign locations to all of the incoming arguments.
1059 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001060 SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001061 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
1062
1063 unsigned NumFixedGPRs = 0;
1064 unsigned NumFixedFPRs = 0;
1065 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1066 SDValue ArgValue;
1067 CCValAssign &VA = ArgLocs[I];
1068 EVT LocVT = VA.getLocVT();
1069 if (VA.isRegLoc()) {
1070 // Arguments passed in registers
1071 const TargetRegisterClass *RC;
1072 switch (LocVT.getSimpleVT().SimpleTy) {
1073 default:
1074 // Integers smaller than i64 should be promoted to i64.
1075 llvm_unreachable("Unexpected argument type");
1076 case MVT::i32:
1077 NumFixedGPRs += 1;
1078 RC = &SystemZ::GR32BitRegClass;
1079 break;
1080 case MVT::i64:
1081 NumFixedGPRs += 1;
1082 RC = &SystemZ::GR64BitRegClass;
1083 break;
1084 case MVT::f32:
1085 NumFixedFPRs += 1;
1086 RC = &SystemZ::FP32BitRegClass;
1087 break;
1088 case MVT::f64:
1089 NumFixedFPRs += 1;
1090 RC = &SystemZ::FP64BitRegClass;
1091 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001092 case MVT::v16i8:
1093 case MVT::v8i16:
1094 case MVT::v4i32:
1095 case MVT::v2i64:
Ulrich Weigand80b3af72015-05-05 19:27:45 +00001096 case MVT::v4f32:
Ulrich Weigandcd808232015-05-05 19:26:48 +00001097 case MVT::v2f64:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001098 RC = &SystemZ::VR128BitRegClass;
1099 break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001100 }
1101
1102 unsigned VReg = MRI.createVirtualRegister(RC);
1103 MRI.addLiveIn(VA.getLocReg(), VReg);
1104 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
1105 } else {
1106 assert(VA.isMemLoc() && "Argument not register or memory");
1107
1108 // Create the frame index object for this incoming parameter.
Matthias Braun941a7052016-07-28 18:40:00 +00001109 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
1110 VA.getLocMemOffset(), true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001111
1112 // Create the SelectionDAG nodes corresponding to a load
1113 // from this parameter. Unpromoted ints and floats are
1114 // passed as right-justified 8-byte values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001115 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
1116 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001117 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
1118 DAG.getIntPtrConstant(4, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001119 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00001120 MachinePointerInfo::getFixedStack(MF, FI));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001121 }
1122
1123 // Convert the value of the argument register into the value that's
1124 // being passed.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001125 if (VA.getLocInfo() == CCValAssign::Indirect) {
Justin Lebar9c375812016-07-15 18:27:10 +00001126 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
1127 MachinePointerInfo()));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001128 // If the original argument was split (e.g. i128), we need
1129 // to load all parts of it here (using the same address).
1130 unsigned ArgIndex = Ins[I].OrigArgIndex;
1131 assert (Ins[I].PartOffset == 0);
1132 while (I + 1 != E && Ins[I + 1].OrigArgIndex == ArgIndex) {
1133 CCValAssign &PartVA = ArgLocs[I + 1];
1134 unsigned PartOffset = Ins[I + 1].PartOffset;
1135 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
1136 DAG.getIntPtrConstant(PartOffset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00001137 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
1138 MachinePointerInfo()));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001139 ++I;
1140 }
1141 } else
1142 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001143 }
1144
1145 if (IsVarArg) {
1146 // Save the number of non-varargs registers for later use by va_start, etc.
1147 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
1148 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
1149
1150 // Likewise the address (in the form of a frame index) of where the
1151 // first stack vararg would be. The 1-byte size here is arbitrary.
1152 int64_t StackSize = CCInfo.getNextStackOffset();
Matthias Braun941a7052016-07-28 18:40:00 +00001153 FuncInfo->setVarArgsFrameIndex(MFI.CreateFixedObject(1, StackSize, true));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001154
1155 // ...and a similar frame index for the caller-allocated save area
1156 // that will be used to store the incoming registers.
1157 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
Matthias Braun941a7052016-07-28 18:40:00 +00001158 unsigned RegSaveIndex = MFI.CreateFixedObject(1, RegSaveOffset, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001159 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
1160
1161 // Store the FPR varargs in the reserved frame slots. (We store the
1162 // GPRs as part of the prologue.)
1163 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
1164 SDValue MemOps[SystemZ::NumArgFPRs];
1165 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
1166 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
Matthias Braun941a7052016-07-28 18:40:00 +00001167 int FI = MFI.CreateFixedObject(8, RegSaveOffset + Offset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +00001168 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001169 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
1170 &SystemZ::FP64BitRegClass);
1171 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
1172 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00001173 MachinePointerInfo::getFixedStack(MF, FI));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001174 }
1175 // Join the stores, which are independent of one another.
1176 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00001177 makeArrayRef(&MemOps[NumFixedFPRs],
1178 SystemZ::NumArgFPRs-NumFixedFPRs));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001179 }
1180 }
1181
1182 return Chain;
1183}
1184
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +00001185static bool canUseSiblingCall(const CCState &ArgCCInfo,
Bryan Chan893110e2016-04-28 00:17:23 +00001186 SmallVectorImpl<CCValAssign> &ArgLocs,
1187 SmallVectorImpl<ISD::OutputArg> &Outs) {
Richard Sandiford709bda62013-08-19 12:42:31 +00001188 // Punt if there are any indirect or stack arguments, or if the call
Bryan Chan893110e2016-04-28 00:17:23 +00001189 // needs the callee-saved argument register R6, or if the call uses
1190 // the callee-saved register arguments SwiftSelf and SwiftError.
Richard Sandiford709bda62013-08-19 12:42:31 +00001191 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1192 CCValAssign &VA = ArgLocs[I];
1193 if (VA.getLocInfo() == CCValAssign::Indirect)
1194 return false;
1195 if (!VA.isRegLoc())
1196 return false;
1197 unsigned Reg = VA.getLocReg();
Richard Sandiford0755c932013-10-01 11:26:28 +00001198 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
Richard Sandiford709bda62013-08-19 12:42:31 +00001199 return false;
Bryan Chan893110e2016-04-28 00:17:23 +00001200 if (Outs[I].Flags.isSwiftSelf() || Outs[I].Flags.isSwiftError())
1201 return false;
Richard Sandiford709bda62013-08-19 12:42:31 +00001202 }
1203 return true;
1204}
1205
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001206SDValue
1207SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
1208 SmallVectorImpl<SDValue> &InVals) const {
1209 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001210 SDLoc &DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001211 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1212 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1213 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001214 SDValue Chain = CLI.Chain;
1215 SDValue Callee = CLI.Callee;
Richard Sandiford709bda62013-08-19 12:42:31 +00001216 bool &IsTailCall = CLI.IsTailCall;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001217 CallingConv::ID CallConv = CLI.CallConv;
1218 bool IsVarArg = CLI.IsVarArg;
1219 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +00001220 EVT PtrVT = getPointerTy(MF.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001221
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001222 // Detect unsupported vector argument and return types.
1223 if (Subtarget.hasVector()) {
1224 VerifyVectorTypes(Outs);
1225 VerifyVectorTypes(Ins);
1226 }
1227
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001228 // Analyze the operands of the call, assigning locations to each operand.
1229 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001230 SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001231 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
1232
Richard Sandiford709bda62013-08-19 12:42:31 +00001233 // We don't support GuaranteedTailCallOpt, only automatically-detected
1234 // sibling calls.
Bryan Chan893110e2016-04-28 00:17:23 +00001235 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs, Outs))
Richard Sandiford709bda62013-08-19 12:42:31 +00001236 IsTailCall = false;
1237
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001238 // Get a count of how many bytes are to be pushed on the stack.
1239 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1240
1241 // Mark the start of the call.
Richard Sandiford709bda62013-08-19 12:42:31 +00001242 if (!IsTailCall)
Serge Pavlovd526b132017-05-09 13:35:13 +00001243 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001244
1245 // Copy argument values to their designated locations.
1246 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
1247 SmallVector<SDValue, 8> MemOpChains;
1248 SDValue StackPtr;
1249 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1250 CCValAssign &VA = ArgLocs[I];
1251 SDValue ArgValue = OutVals[I];
1252
1253 if (VA.getLocInfo() == CCValAssign::Indirect) {
1254 // Store the argument in a stack slot and pass its address.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001255 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[I].ArgVT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001256 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Justin Lebar9c375812016-07-15 18:27:10 +00001257 MemOpChains.push_back(
1258 DAG.getStore(Chain, DL, ArgValue, SpillSlot,
1259 MachinePointerInfo::getFixedStack(MF, FI)));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001260 // If the original argument was split (e.g. i128), we need
1261 // to store all parts of it here (and pass just one address).
1262 unsigned ArgIndex = Outs[I].OrigArgIndex;
1263 assert (Outs[I].PartOffset == 0);
1264 while (I + 1 != E && Outs[I + 1].OrigArgIndex == ArgIndex) {
1265 SDValue PartValue = OutVals[I + 1];
1266 unsigned PartOffset = Outs[I + 1].PartOffset;
1267 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
1268 DAG.getIntPtrConstant(PartOffset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00001269 MemOpChains.push_back(
1270 DAG.getStore(Chain, DL, PartValue, Address,
1271 MachinePointerInfo::getFixedStack(MF, FI)));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001272 ++I;
1273 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001274 ArgValue = SpillSlot;
1275 } else
1276 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
1277
1278 if (VA.isRegLoc())
1279 // Queue up the argument copies and emit them at the end.
1280 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1281 else {
1282 assert(VA.isMemLoc() && "Argument not register or memory");
1283
1284 // Work out the address of the stack slot. Unpromoted ints and
1285 // floats are passed as right-justified 8-byte values.
1286 if (!StackPtr.getNode())
1287 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
1288 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
1289 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
1290 Offset += 4;
1291 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001292 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001293
1294 // Emit the store.
Justin Lebar9c375812016-07-15 18:27:10 +00001295 MemOpChains.push_back(
1296 DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001297 }
1298 }
1299
1300 // Join the stores, which are independent of one another.
1301 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001302 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001303
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001304 // Accept direct calls by converting symbolic call addresses to the
Richard Sandiford709bda62013-08-19 12:42:31 +00001305 // associated Target* opcodes. Force %r1 to be used for indirect
1306 // tail calls.
1307 SDValue Glue;
Richard Sandiford21f5d682014-03-06 11:22:58 +00001308 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001309 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
1310 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford21f5d682014-03-06 11:22:58 +00001311 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001312 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
1313 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford709bda62013-08-19 12:42:31 +00001314 } else if (IsTailCall) {
1315 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
1316 Glue = Chain.getValue(1);
1317 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
1318 }
1319
1320 // Build a sequence of copy-to-reg nodes, chained and glued together.
1321 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
1322 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
1323 RegsToPass[I].second, Glue);
1324 Glue = Chain.getValue(1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001325 }
1326
1327 // The first call operand is the chain and the second is the target address.
1328 SmallVector<SDValue, 8> Ops;
1329 Ops.push_back(Chain);
1330 Ops.push_back(Callee);
1331
1332 // Add argument registers to the end of the list so that they are
1333 // known live into the call.
1334 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
1335 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
1336 RegsToPass[I].second.getValueType()));
1337
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001338 // Add a register mask operand representing the call-preserved registers.
Eric Christophera6734172015-01-31 00:06:45 +00001339 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00001340 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001341 assert(Mask && "Missing call preserved mask for calling convention");
1342 Ops.push_back(DAG.getRegisterMask(Mask));
1343
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001344 // Glue the call to the argument copies, if any.
1345 if (Glue.getNode())
1346 Ops.push_back(Glue);
1347
1348 // Emit the call.
1349 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Richard Sandiford709bda62013-08-19 12:42:31 +00001350 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001351 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
1352 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001353 Glue = Chain.getValue(1);
1354
1355 // Mark the end of the call, which is glued to the call itself.
1356 Chain = DAG.getCALLSEQ_END(Chain,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001357 DAG.getConstant(NumBytes, DL, PtrVT, true),
1358 DAG.getConstant(0, DL, PtrVT, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001359 Glue, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001360 Glue = Chain.getValue(1);
1361
1362 // Assign locations to each value returned by this call.
1363 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001364 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001365 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
1366
1367 // Copy all of the result registers out of their specified physreg.
1368 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1369 CCValAssign &VA = RetLocs[I];
1370
1371 // Copy the value out, gluing the copy to the end of the call sequence.
1372 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
1373 VA.getLocVT(), Glue);
1374 Chain = RetValue.getValue(1);
1375 Glue = RetValue.getValue(2);
1376
1377 // Convert the value of the return register into the value that's
1378 // being returned.
1379 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
1380 }
1381
1382 return Chain;
1383}
1384
Ulrich Weiganda887f062015-08-13 13:37:06 +00001385bool SystemZTargetLowering::
1386CanLowerReturn(CallingConv::ID CallConv,
1387 MachineFunction &MF, bool isVarArg,
1388 const SmallVectorImpl<ISD::OutputArg> &Outs,
1389 LLVMContext &Context) const {
1390 // Detect unsupported vector return types.
1391 if (Subtarget.hasVector())
1392 VerifyVectorTypes(Outs);
1393
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001394 // Special case that we cannot easily detect in RetCC_SystemZ since
1395 // i128 is not a legal type.
1396 for (auto &Out : Outs)
1397 if (Out.ArgVT == MVT::i128)
1398 return false;
1399
Ulrich Weiganda887f062015-08-13 13:37:06 +00001400 SmallVector<CCValAssign, 16> RetLocs;
1401 CCState RetCCInfo(CallConv, isVarArg, MF, RetLocs, Context);
1402 return RetCCInfo.CheckReturn(Outs, RetCC_SystemZ);
1403}
1404
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001405SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001406SystemZTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1407 bool IsVarArg,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001408 const SmallVectorImpl<ISD::OutputArg> &Outs,
1409 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001410 const SDLoc &DL, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001411 MachineFunction &MF = DAG.getMachineFunction();
1412
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001413 // Detect unsupported vector return types.
1414 if (Subtarget.hasVector())
1415 VerifyVectorTypes(Outs);
1416
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001417 // Assign locations to each returned value.
1418 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001419 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001420 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
1421
1422 // Quick exit for void returns
1423 if (RetLocs.empty())
1424 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
1425
1426 // Copy the result values into the output registers.
1427 SDValue Glue;
1428 SmallVector<SDValue, 4> RetOps;
1429 RetOps.push_back(Chain);
1430 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1431 CCValAssign &VA = RetLocs[I];
1432 SDValue RetValue = OutVals[I];
1433
1434 // Make the return register live on exit.
1435 assert(VA.isRegLoc() && "Can only return in registers!");
1436
1437 // Promote the value as required.
1438 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
1439
1440 // Chain and glue the copies together.
1441 unsigned Reg = VA.getLocReg();
1442 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
1443 Glue = Chain.getValue(1);
1444 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
1445 }
1446
1447 // Update chain and glue.
1448 RetOps[0] = Chain;
1449 if (Glue.getNode())
1450 RetOps.push_back(Glue);
1451
Craig Topper48d114b2014-04-26 18:35:24 +00001452 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001453}
1454
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001455// Return true if Op is an intrinsic node with chain that returns the CC value
1456// as its only (other) argument. Provide the associated SystemZISD opcode and
1457// the mask of valid CC values if so.
1458static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode,
1459 unsigned &CCValid) {
1460 unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1461 switch (Id) {
1462 case Intrinsic::s390_tbegin:
1463 Opcode = SystemZISD::TBEGIN;
1464 CCValid = SystemZ::CCMASK_TBEGIN;
1465 return true;
1466
1467 case Intrinsic::s390_tbegin_nofloat:
1468 Opcode = SystemZISD::TBEGIN_NOFLOAT;
1469 CCValid = SystemZ::CCMASK_TBEGIN;
1470 return true;
1471
1472 case Intrinsic::s390_tend:
1473 Opcode = SystemZISD::TEND;
1474 CCValid = SystemZ::CCMASK_TEND;
1475 return true;
1476
1477 default:
1478 return false;
1479 }
1480}
1481
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001482// Return true if Op is an intrinsic node without chain that returns the
1483// CC value as its final argument. Provide the associated SystemZISD
1484// opcode and the mask of valid CC values if so.
1485static bool isIntrinsicWithCC(SDValue Op, unsigned &Opcode, unsigned &CCValid) {
1486 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1487 switch (Id) {
1488 case Intrinsic::s390_vpkshs:
1489 case Intrinsic::s390_vpksfs:
1490 case Intrinsic::s390_vpksgs:
1491 Opcode = SystemZISD::PACKS_CC;
1492 CCValid = SystemZ::CCMASK_VCMP;
1493 return true;
1494
1495 case Intrinsic::s390_vpklshs:
1496 case Intrinsic::s390_vpklsfs:
1497 case Intrinsic::s390_vpklsgs:
1498 Opcode = SystemZISD::PACKLS_CC;
1499 CCValid = SystemZ::CCMASK_VCMP;
1500 return true;
1501
1502 case Intrinsic::s390_vceqbs:
1503 case Intrinsic::s390_vceqhs:
1504 case Intrinsic::s390_vceqfs:
1505 case Intrinsic::s390_vceqgs:
1506 Opcode = SystemZISD::VICMPES;
1507 CCValid = SystemZ::CCMASK_VCMP;
1508 return true;
1509
1510 case Intrinsic::s390_vchbs:
1511 case Intrinsic::s390_vchhs:
1512 case Intrinsic::s390_vchfs:
1513 case Intrinsic::s390_vchgs:
1514 Opcode = SystemZISD::VICMPHS;
1515 CCValid = SystemZ::CCMASK_VCMP;
1516 return true;
1517
1518 case Intrinsic::s390_vchlbs:
1519 case Intrinsic::s390_vchlhs:
1520 case Intrinsic::s390_vchlfs:
1521 case Intrinsic::s390_vchlgs:
1522 Opcode = SystemZISD::VICMPHLS;
1523 CCValid = SystemZ::CCMASK_VCMP;
1524 return true;
1525
1526 case Intrinsic::s390_vtm:
1527 Opcode = SystemZISD::VTM;
1528 CCValid = SystemZ::CCMASK_VCMP;
1529 return true;
1530
1531 case Intrinsic::s390_vfaebs:
1532 case Intrinsic::s390_vfaehs:
1533 case Intrinsic::s390_vfaefs:
1534 Opcode = SystemZISD::VFAE_CC;
1535 CCValid = SystemZ::CCMASK_ANY;
1536 return true;
1537
1538 case Intrinsic::s390_vfaezbs:
1539 case Intrinsic::s390_vfaezhs:
1540 case Intrinsic::s390_vfaezfs:
1541 Opcode = SystemZISD::VFAEZ_CC;
1542 CCValid = SystemZ::CCMASK_ANY;
1543 return true;
1544
1545 case Intrinsic::s390_vfeebs:
1546 case Intrinsic::s390_vfeehs:
1547 case Intrinsic::s390_vfeefs:
1548 Opcode = SystemZISD::VFEE_CC;
1549 CCValid = SystemZ::CCMASK_ANY;
1550 return true;
1551
1552 case Intrinsic::s390_vfeezbs:
1553 case Intrinsic::s390_vfeezhs:
1554 case Intrinsic::s390_vfeezfs:
1555 Opcode = SystemZISD::VFEEZ_CC;
1556 CCValid = SystemZ::CCMASK_ANY;
1557 return true;
1558
1559 case Intrinsic::s390_vfenebs:
1560 case Intrinsic::s390_vfenehs:
1561 case Intrinsic::s390_vfenefs:
1562 Opcode = SystemZISD::VFENE_CC;
1563 CCValid = SystemZ::CCMASK_ANY;
1564 return true;
1565
1566 case Intrinsic::s390_vfenezbs:
1567 case Intrinsic::s390_vfenezhs:
1568 case Intrinsic::s390_vfenezfs:
1569 Opcode = SystemZISD::VFENEZ_CC;
1570 CCValid = SystemZ::CCMASK_ANY;
1571 return true;
1572
1573 case Intrinsic::s390_vistrbs:
1574 case Intrinsic::s390_vistrhs:
1575 case Intrinsic::s390_vistrfs:
1576 Opcode = SystemZISD::VISTR_CC;
1577 CCValid = SystemZ::CCMASK_0 | SystemZ::CCMASK_3;
1578 return true;
1579
1580 case Intrinsic::s390_vstrcbs:
1581 case Intrinsic::s390_vstrchs:
1582 case Intrinsic::s390_vstrcfs:
1583 Opcode = SystemZISD::VSTRC_CC;
1584 CCValid = SystemZ::CCMASK_ANY;
1585 return true;
1586
1587 case Intrinsic::s390_vstrczbs:
1588 case Intrinsic::s390_vstrczhs:
1589 case Intrinsic::s390_vstrczfs:
1590 Opcode = SystemZISD::VSTRCZ_CC;
1591 CCValid = SystemZ::CCMASK_ANY;
1592 return true;
1593
1594 case Intrinsic::s390_vfcedbs:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001595 case Intrinsic::s390_vfcesbs:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001596 Opcode = SystemZISD::VFCMPES;
1597 CCValid = SystemZ::CCMASK_VCMP;
1598 return true;
1599
1600 case Intrinsic::s390_vfchdbs:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001601 case Intrinsic::s390_vfchsbs:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001602 Opcode = SystemZISD::VFCMPHS;
1603 CCValid = SystemZ::CCMASK_VCMP;
1604 return true;
1605
1606 case Intrinsic::s390_vfchedbs:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001607 case Intrinsic::s390_vfchesbs:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001608 Opcode = SystemZISD::VFCMPHES;
1609 CCValid = SystemZ::CCMASK_VCMP;
1610 return true;
1611
1612 case Intrinsic::s390_vftcidb:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001613 case Intrinsic::s390_vftcisb:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001614 Opcode = SystemZISD::VFTCI;
1615 CCValid = SystemZ::CCMASK_VCMP;
1616 return true;
1617
Marcin Koscielnickicf7cc722016-07-10 14:41:22 +00001618 case Intrinsic::s390_tdc:
1619 Opcode = SystemZISD::TDC;
1620 CCValid = SystemZ::CCMASK_TDC;
1621 return true;
1622
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001623 default:
1624 return false;
1625 }
1626}
1627
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001628// Emit an intrinsic with chain with a glued value instead of its CC result.
1629static SDValue emitIntrinsicWithChainAndGlue(SelectionDAG &DAG, SDValue Op,
1630 unsigned Opcode) {
1631 // Copy all operands except the intrinsic ID.
1632 unsigned NumOps = Op.getNumOperands();
1633 SmallVector<SDValue, 6> Ops;
1634 Ops.reserve(NumOps - 1);
1635 Ops.push_back(Op.getOperand(0));
1636 for (unsigned I = 2; I < NumOps; ++I)
1637 Ops.push_back(Op.getOperand(I));
1638
1639 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
1640 SDVTList RawVTs = DAG.getVTList(MVT::Other, MVT::Glue);
1641 SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1642 SDValue OldChain = SDValue(Op.getNode(), 1);
1643 SDValue NewChain = SDValue(Intr.getNode(), 0);
1644 DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain);
1645 return Intr;
1646}
1647
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001648// Emit an intrinsic with a glued value instead of its CC result.
1649static SDValue emitIntrinsicWithGlue(SelectionDAG &DAG, SDValue Op,
1650 unsigned Opcode) {
1651 // Copy all operands except the intrinsic ID.
1652 unsigned NumOps = Op.getNumOperands();
1653 SmallVector<SDValue, 6> Ops;
1654 Ops.reserve(NumOps - 1);
1655 for (unsigned I = 1; I < NumOps; ++I)
1656 Ops.push_back(Op.getOperand(I));
1657
1658 if (Op->getNumValues() == 1)
1659 return DAG.getNode(Opcode, SDLoc(Op), MVT::Glue, Ops);
1660 assert(Op->getNumValues() == 2 && "Expected exactly one non-CC result");
1661 SDVTList RawVTs = DAG.getVTList(Op->getValueType(0), MVT::Glue);
1662 return DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1663}
1664
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001665// CC is a comparison that will be implemented using an integer or
1666// floating-point comparison. Return the condition code mask for
1667// a branch on true. In the integer case, CCMASK_CMP_UO is set for
1668// unsigned comparisons and clear for signed ones. In the floating-point
1669// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1670static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1671#define CONV(X) \
1672 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1673 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1674 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1675
1676 switch (CC) {
1677 default:
1678 llvm_unreachable("Invalid integer condition!");
1679
1680 CONV(EQ);
1681 CONV(NE);
1682 CONV(GT);
1683 CONV(GE);
1684 CONV(LT);
1685 CONV(LE);
1686
1687 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1688 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1689 }
1690#undef CONV
1691}
1692
Richard Sandifordd420f732013-12-13 15:28:45 +00001693// If C can be converted to a comparison against zero, adjust the operands
Richard Sandiforda0757082013-08-01 10:29:45 +00001694// as necessary.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001695static void adjustZeroCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001696 if (C.ICmpType == SystemZICMP::UnsignedOnly)
Richard Sandiforda0757082013-08-01 10:29:45 +00001697 return;
1698
Richard Sandiford21f5d682014-03-06 11:22:58 +00001699 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
Richard Sandiforda0757082013-08-01 10:29:45 +00001700 if (!ConstOp1)
1701 return;
1702
1703 int64_t Value = ConstOp1->getSExtValue();
Richard Sandifordd420f732013-12-13 15:28:45 +00001704 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1705 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1706 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1707 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1708 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001709 C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType());
Richard Sandiforda0757082013-08-01 10:29:45 +00001710 }
1711}
1712
Richard Sandifordd420f732013-12-13 15:28:45 +00001713// If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1714// adjust the operands as necessary.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001715static void adjustSubwordCmp(SelectionDAG &DAG, const SDLoc &DL,
1716 Comparison &C) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001717 // For us to make any changes, it must a comparison between a single-use
1718 // load and a constant.
Richard Sandifordd420f732013-12-13 15:28:45 +00001719 if (!C.Op0.hasOneUse() ||
1720 C.Op0.getOpcode() != ISD::LOAD ||
1721 C.Op1.getOpcode() != ISD::Constant)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001722 return;
1723
1724 // We must have an 8- or 16-bit load.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001725 auto *Load = cast<LoadSDNode>(C.Op0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001726 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1727 if (NumBits != 8 && NumBits != 16)
1728 return;
1729
1730 // The load must be an extending one and the constant must be within the
1731 // range of the unextended value.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001732 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001733 uint64_t Value = ConstOp1->getZExtValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001734 uint64_t Mask = (1 << NumBits) - 1;
1735 if (Load->getExtensionType() == ISD::SEXTLOAD) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001736 // Make sure that ConstOp1 is in range of C.Op0.
1737 int64_t SignedValue = ConstOp1->getSExtValue();
1738 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001739 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001740 if (C.ICmpType != SystemZICMP::SignedOnly) {
1741 // Unsigned comparison between two sign-extended values is equivalent
1742 // to unsigned comparison between two zero-extended values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001743 Value &= Mask;
Richard Sandifordd420f732013-12-13 15:28:45 +00001744 } else if (NumBits == 8) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001745 // Try to treat the comparison as unsigned, so that we can use CLI.
1746 // Adjust CCMask and Value as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001747 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001748 // Test whether the high bit of the byte is set.
Richard Sandifordd420f732013-12-13 15:28:45 +00001749 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1750 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001751 // Test whether the high bit of the byte is clear.
Richard Sandifordd420f732013-12-13 15:28:45 +00001752 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001753 else
1754 // No instruction exists for this combination.
1755 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001756 C.ICmpType = SystemZICMP::UnsignedOnly;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001757 }
1758 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1759 if (Value > Mask)
1760 return;
Ulrich Weigand47f36492015-12-16 18:04:06 +00001761 // If the constant is in range, we can use any comparison.
1762 C.ICmpType = SystemZICMP::Any;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001763 } else
1764 return;
1765
1766 // Make sure that the first operand is an i32 of the right extension type.
Richard Sandifordd420f732013-12-13 15:28:45 +00001767 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1768 ISD::SEXTLOAD :
1769 ISD::ZEXTLOAD);
1770 if (C.Op0.getValueType() != MVT::i32 ||
Jonas Paulssonb9a24672017-11-30 08:18:50 +00001771 Load->getExtensionType() != ExtType) {
Justin Lebar9c375812016-07-15 18:27:10 +00001772 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32, Load->getChain(),
1773 Load->getBasePtr(), Load->getPointerInfo(),
1774 Load->getMemoryVT(), Load->getAlignment(),
1775 Load->getMemOperand()->getFlags());
Jonas Paulssonb9a24672017-11-30 08:18:50 +00001776 // Update the chain uses.
1777 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), C.Op0.getValue(1));
1778 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001779
1780 // Make sure that the second operand is an i32 with the right value.
Richard Sandifordd420f732013-12-13 15:28:45 +00001781 if (C.Op1.getValueType() != MVT::i32 ||
1782 Value != ConstOp1->getZExtValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001783 C.Op1 = DAG.getConstant(Value, DL, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001784}
1785
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001786// Return true if Op is either an unextended load, or a load suitable
1787// for integer register-memory comparisons of type ICmpType.
1788static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001789 auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001790 if (Load) {
1791 // There are no instructions to compare a register with a memory byte.
1792 if (Load->getMemoryVT() == MVT::i8)
1793 return false;
1794 // Otherwise decide on extension type.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001795 switch (Load->getExtensionType()) {
1796 case ISD::NON_EXTLOAD:
Richard Sandiford24e597b2013-08-23 11:27:19 +00001797 return true;
1798 case ISD::SEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001799 return ICmpType != SystemZICMP::UnsignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001800 case ISD::ZEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001801 return ICmpType != SystemZICMP::SignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001802 default:
1803 break;
1804 }
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001805 }
Richard Sandiford24e597b2013-08-23 11:27:19 +00001806 return false;
1807}
1808
Richard Sandifordd420f732013-12-13 15:28:45 +00001809// Return true if it is better to swap the operands of C.
1810static bool shouldSwapCmpOperands(const Comparison &C) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001811 // Leave f128 comparisons alone, since they have no memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001812 if (C.Op0.getValueType() == MVT::f128)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001813 return false;
1814
1815 // Always keep a floating-point constant second, since comparisons with
1816 // zero can use LOAD TEST and comparisons with other constants make a
1817 // natural memory operand.
Richard Sandifordd420f732013-12-13 15:28:45 +00001818 if (isa<ConstantFPSDNode>(C.Op1))
Richard Sandiford24e597b2013-08-23 11:27:19 +00001819 return false;
1820
1821 // Never swap comparisons with zero since there are many ways to optimize
1822 // those later.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001823 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001824 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001825 return false;
1826
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001827 // Also keep natural memory operands second if the loaded value is
1828 // only used here. Several comparisons have memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001829 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001830 return false;
1831
Richard Sandiford24e597b2013-08-23 11:27:19 +00001832 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1833 // In that case we generally prefer the memory to be second.
Richard Sandifordd420f732013-12-13 15:28:45 +00001834 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001835 // The only exceptions are when the second operand is a constant and
1836 // we can use things like CHHSI.
Richard Sandifordd420f732013-12-13 15:28:45 +00001837 if (!ConstOp1)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001838 return true;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001839 // The unsigned memory-immediate instructions can handle 16-bit
1840 // unsigned integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001841 if (C.ICmpType != SystemZICMP::SignedOnly &&
1842 isUInt<16>(ConstOp1->getZExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001843 return false;
1844 // The signed memory-immediate instructions can handle 16-bit
1845 // signed integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001846 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1847 isInt<16>(ConstOp1->getSExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001848 return false;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001849 return true;
1850 }
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001851
1852 // Try to promote the use of CGFR and CLGFR.
Richard Sandifordd420f732013-12-13 15:28:45 +00001853 unsigned Opcode0 = C.Op0.getOpcode();
1854 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001855 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001856 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001857 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001858 if (C.ICmpType != SystemZICMP::SignedOnly &&
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001859 Opcode0 == ISD::AND &&
Richard Sandifordd420f732013-12-13 15:28:45 +00001860 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1861 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001862 return true;
1863
Richard Sandiford24e597b2013-08-23 11:27:19 +00001864 return false;
1865}
1866
Richard Sandiford73170f82013-12-11 11:45:08 +00001867// Return a version of comparison CC mask CCMask in which the LT and GT
1868// actions are swapped.
1869static unsigned reverseCCMask(unsigned CCMask) {
1870 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1871 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1872 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1873 (CCMask & SystemZ::CCMASK_CMP_UO));
1874}
1875
Richard Sandiford0847c452013-12-13 15:50:30 +00001876// Check whether C tests for equality between X and Y and whether X - Y
1877// or Y - X is also computed. In that case it's better to compare the
1878// result of the subtraction against zero.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001879static void adjustForSubtraction(SelectionDAG &DAG, const SDLoc &DL,
1880 Comparison &C) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001881 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1882 C.CCMask == SystemZ::CCMASK_CMP_NE) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001883 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001884 SDNode *N = *I;
1885 if (N->getOpcode() == ISD::SUB &&
1886 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1887 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1888 C.Op0 = SDValue(N, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001889 C.Op1 = DAG.getConstant(0, DL, N->getValueType(0));
Richard Sandiford0847c452013-12-13 15:50:30 +00001890 return;
1891 }
1892 }
1893 }
1894}
1895
Richard Sandifordd420f732013-12-13 15:28:45 +00001896// Check whether C compares a floating-point value with zero and if that
1897// floating-point value is also negated. In this case we can use the
1898// negation to set CC, so avoiding separate LOAD AND TEST and
1899// LOAD (NEGATIVE/COMPLEMENT) instructions.
1900static void adjustForFNeg(Comparison &C) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001901 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
Richard Sandiford73170f82013-12-11 11:45:08 +00001902 if (C1 && C1->isZero()) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001903 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford73170f82013-12-11 11:45:08 +00001904 SDNode *N = *I;
1905 if (N->getOpcode() == ISD::FNEG) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001906 C.Op0 = SDValue(N, 0);
1907 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford73170f82013-12-11 11:45:08 +00001908 return;
1909 }
1910 }
1911 }
1912}
1913
Richard Sandifordd420f732013-12-13 15:28:45 +00001914// Check whether C compares (shl X, 32) with 0 and whether X is
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001915// also sign-extended. In that case it is better to test the result
1916// of the sign extension using LTGFR.
1917//
1918// This case is important because InstCombine transforms a comparison
1919// with (sext (trunc X)) into a comparison with (shl X, 32).
Richard Sandifordd420f732013-12-13 15:28:45 +00001920static void adjustForLTGFR(Comparison &C) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001921 // Check for a comparison between (shl X, 32) and 0.
Richard Sandifordd420f732013-12-13 15:28:45 +00001922 if (C.Op0.getOpcode() == ISD::SHL &&
1923 C.Op0.getValueType() == MVT::i64 &&
1924 C.Op1.getOpcode() == ISD::Constant &&
1925 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001926 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001927 if (C1 && C1->getZExtValue() == 32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001928 SDValue ShlOp0 = C.Op0.getOperand(0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001929 // See whether X has any SIGN_EXTEND_INREG uses.
Richard Sandiford28c111e2014-03-06 11:00:15 +00001930 for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001931 SDNode *N = *I;
1932 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
1933 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001934 C.Op0 = SDValue(N, 0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001935 return;
1936 }
1937 }
1938 }
1939 }
1940}
1941
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001942// If C compares the truncation of an extending load, try to compare
1943// the untruncated value instead. This exposes more opportunities to
1944// reuse CC.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001945static void adjustICmpTruncate(SelectionDAG &DAG, const SDLoc &DL,
1946 Comparison &C) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001947 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
1948 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
1949 C.Op1.getOpcode() == ISD::Constant &&
1950 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001951 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00001952 if (L->getMemoryVT().getStoreSizeInBits() <= C.Op0.getValueSizeInBits()) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001953 unsigned Type = L->getExtensionType();
1954 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
1955 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
1956 C.Op0 = C.Op0.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001957 C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType());
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001958 }
1959 }
1960 }
1961}
1962
Richard Sandiford030c1652013-09-13 09:09:50 +00001963// Return true if shift operation N has an in-range constant shift value.
1964// Store it in ShiftVal if so.
1965static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001966 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
Richard Sandiford030c1652013-09-13 09:09:50 +00001967 if (!Shift)
1968 return false;
1969
1970 uint64_t Amount = Shift->getZExtValue();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00001971 if (Amount >= N.getValueSizeInBits())
Richard Sandiford030c1652013-09-13 09:09:50 +00001972 return false;
1973
1974 ShiftVal = Amount;
1975 return true;
1976}
1977
1978// Check whether an AND with Mask is suitable for a TEST UNDER MASK
1979// instruction and whether the CC value is descriptive enough to handle
1980// a comparison of type Opcode between the AND result and CmpVal.
1981// CCMask says which comparison result is being tested and BitSize is
1982// the number of bits in the operands. If TEST UNDER MASK can be used,
1983// return the corresponding CC mask, otherwise return 0.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001984static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
1985 uint64_t Mask, uint64_t CmpVal,
1986 unsigned ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001987 assert(Mask != 0 && "ANDs with zero should have been removed by now");
1988
Richard Sandiford030c1652013-09-13 09:09:50 +00001989 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
1990 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
1991 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
1992 return 0;
1993
Richard Sandiford113c8702013-09-03 15:38:35 +00001994 // Work out the masks for the lowest and highest bits.
1995 unsigned HighShift = 63 - countLeadingZeros(Mask);
1996 uint64_t High = uint64_t(1) << HighShift;
1997 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
1998
1999 // Signed ordered comparisons are effectively unsigned if the sign
2000 // bit is dropped.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002001 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
Richard Sandiford113c8702013-09-03 15:38:35 +00002002
2003 // Check for equality comparisons with 0, or the equivalent.
2004 if (CmpVal == 0) {
2005 if (CCMask == SystemZ::CCMASK_CMP_EQ)
2006 return SystemZ::CCMASK_TM_ALL_0;
2007 if (CCMask == SystemZ::CCMASK_CMP_NE)
2008 return SystemZ::CCMASK_TM_SOME_1;
2009 }
Ulrich Weigand4a4d4ab2016-02-01 18:31:19 +00002010 if (EffectivelyUnsigned && CmpVal > 0 && CmpVal <= Low) {
Richard Sandiford113c8702013-09-03 15:38:35 +00002011 if (CCMask == SystemZ::CCMASK_CMP_LT)
2012 return SystemZ::CCMASK_TM_ALL_0;
2013 if (CCMask == SystemZ::CCMASK_CMP_GE)
2014 return SystemZ::CCMASK_TM_SOME_1;
2015 }
2016 if (EffectivelyUnsigned && CmpVal < Low) {
2017 if (CCMask == SystemZ::CCMASK_CMP_LE)
2018 return SystemZ::CCMASK_TM_ALL_0;
2019 if (CCMask == SystemZ::CCMASK_CMP_GT)
2020 return SystemZ::CCMASK_TM_SOME_1;
2021 }
2022
2023 // Check for equality comparisons with the mask, or the equivalent.
2024 if (CmpVal == Mask) {
2025 if (CCMask == SystemZ::CCMASK_CMP_EQ)
2026 return SystemZ::CCMASK_TM_ALL_1;
2027 if (CCMask == SystemZ::CCMASK_CMP_NE)
2028 return SystemZ::CCMASK_TM_SOME_0;
2029 }
2030 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
2031 if (CCMask == SystemZ::CCMASK_CMP_GT)
2032 return SystemZ::CCMASK_TM_ALL_1;
2033 if (CCMask == SystemZ::CCMASK_CMP_LE)
2034 return SystemZ::CCMASK_TM_SOME_0;
2035 }
2036 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
2037 if (CCMask == SystemZ::CCMASK_CMP_GE)
2038 return SystemZ::CCMASK_TM_ALL_1;
2039 if (CCMask == SystemZ::CCMASK_CMP_LT)
2040 return SystemZ::CCMASK_TM_SOME_0;
2041 }
2042
2043 // Check for ordered comparisons with the top bit.
2044 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
2045 if (CCMask == SystemZ::CCMASK_CMP_LE)
2046 return SystemZ::CCMASK_TM_MSB_0;
2047 if (CCMask == SystemZ::CCMASK_CMP_GT)
2048 return SystemZ::CCMASK_TM_MSB_1;
2049 }
2050 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
2051 if (CCMask == SystemZ::CCMASK_CMP_LT)
2052 return SystemZ::CCMASK_TM_MSB_0;
2053 if (CCMask == SystemZ::CCMASK_CMP_GE)
2054 return SystemZ::CCMASK_TM_MSB_1;
2055 }
2056
2057 // If there are just two bits, we can do equality checks for Low and High
2058 // as well.
2059 if (Mask == Low + High) {
2060 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
2061 return SystemZ::CCMASK_TM_MIXED_MSB_0;
2062 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
2063 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
2064 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
2065 return SystemZ::CCMASK_TM_MIXED_MSB_1;
2066 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
2067 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
2068 }
2069
2070 // Looks like we've exhausted our options.
2071 return 0;
2072}
2073
Richard Sandifordd420f732013-12-13 15:28:45 +00002074// See whether C can be implemented as a TEST UNDER MASK instruction.
2075// Update the arguments with the TM version if so.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002076static void adjustForTestUnderMask(SelectionDAG &DAG, const SDLoc &DL,
2077 Comparison &C) {
Richard Sandiford113c8702013-09-03 15:38:35 +00002078 // Check that we have a comparison with a constant.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002079 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00002080 if (!ConstOp1)
Richard Sandiford35b9be22013-08-28 10:31:43 +00002081 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00002082 uint64_t CmpVal = ConstOp1->getZExtValue();
Richard Sandiford35b9be22013-08-28 10:31:43 +00002083
2084 // Check whether the nonconstant input is an AND with a constant mask.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002085 Comparison NewC(C);
2086 uint64_t MaskVal;
Craig Topper062a2ba2014-04-25 05:30:21 +00002087 ConstantSDNode *Mask = nullptr;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002088 if (C.Op0.getOpcode() == ISD::AND) {
2089 NewC.Op0 = C.Op0.getOperand(0);
2090 NewC.Op1 = C.Op0.getOperand(1);
2091 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
2092 if (!Mask)
2093 return;
2094 MaskVal = Mask->getZExtValue();
2095 } else {
2096 // There is no instruction to compare with a 64-bit immediate
2097 // so use TMHH instead if possible. We need an unsigned ordered
2098 // comparison with an i64 immediate.
2099 if (NewC.Op0.getValueType() != MVT::i64 ||
2100 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
2101 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
2102 NewC.ICmpType == SystemZICMP::SignedOnly)
2103 return;
2104 // Convert LE and GT comparisons into LT and GE.
2105 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
2106 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
2107 if (CmpVal == uint64_t(-1))
2108 return;
2109 CmpVal += 1;
2110 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
2111 }
2112 // If the low N bits of Op1 are zero than the low N bits of Op0 can
2113 // be masked off without changing the result.
2114 MaskVal = -(CmpVal & -CmpVal);
2115 NewC.ICmpType = SystemZICMP::UnsignedOnly;
2116 }
Ulrich Weigandb8d76fb2015-03-30 13:46:59 +00002117 if (!MaskVal)
2118 return;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002119
Richard Sandiford113c8702013-09-03 15:38:35 +00002120 // Check whether the combination of mask, comparison value and comparison
2121 // type are suitable.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002122 unsigned BitSize = NewC.Op0.getValueSizeInBits();
Richard Sandiford030c1652013-09-13 09:09:50 +00002123 unsigned NewCCMask, ShiftVal;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002124 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2125 NewC.Op0.getOpcode() == ISD::SHL &&
2126 isSimpleShift(NewC.Op0, ShiftVal) &&
Jonas Paulsson8c336472017-06-26 13:38:27 +00002127 (MaskVal >> ShiftVal != 0) &&
Ulrich Weigand5bfed6c2017-12-05 19:42:07 +00002128 ((CmpVal >> ShiftVal) << ShiftVal) == CmpVal &&
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002129 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
2130 MaskVal >> ShiftVal,
Richard Sandiford030c1652013-09-13 09:09:50 +00002131 CmpVal >> ShiftVal,
2132 SystemZICMP::Any))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002133 NewC.Op0 = NewC.Op0.getOperand(0);
2134 MaskVal >>= ShiftVal;
2135 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2136 NewC.Op0.getOpcode() == ISD::SRL &&
2137 isSimpleShift(NewC.Op0, ShiftVal) &&
Jonas Paulsson8c336472017-06-26 13:38:27 +00002138 (MaskVal << ShiftVal != 0) &&
Ulrich Weigand5bfed6c2017-12-05 19:42:07 +00002139 ((CmpVal << ShiftVal) >> ShiftVal) == CmpVal &&
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002140 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
Richard Sandiford030c1652013-09-13 09:09:50 +00002141 MaskVal << ShiftVal,
2142 CmpVal << ShiftVal,
2143 SystemZICMP::UnsignedOnly))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002144 NewC.Op0 = NewC.Op0.getOperand(0);
2145 MaskVal <<= ShiftVal;
Richard Sandiford030c1652013-09-13 09:09:50 +00002146 } else {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002147 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
2148 NewC.ICmpType);
Richard Sandiford030c1652013-09-13 09:09:50 +00002149 if (!NewCCMask)
2150 return;
2151 }
Richard Sandiford113c8702013-09-03 15:38:35 +00002152
Richard Sandiford35b9be22013-08-28 10:31:43 +00002153 // Go ahead and make the change.
Richard Sandifordd420f732013-12-13 15:28:45 +00002154 C.Opcode = SystemZISD::TM;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002155 C.Op0 = NewC.Op0;
2156 if (Mask && Mask->getZExtValue() == MaskVal)
2157 C.Op1 = SDValue(Mask, 0);
2158 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002159 C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType());
Richard Sandifordd420f732013-12-13 15:28:45 +00002160 C.CCValid = SystemZ::CCMASK_TM;
2161 C.CCMask = NewCCMask;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002162}
2163
Ulrich Weigand31112892018-01-19 20:54:18 +00002164// See whether the comparison argument contains a redundant AND
2165// and remove it if so. This sometimes happens due to the generic
2166// BRCOND expansion.
2167static void adjustForRedundantAnd(SelectionDAG &DAG, const SDLoc &DL,
2168 Comparison &C) {
2169 if (C.Op0.getOpcode() != ISD::AND)
2170 return;
2171 auto *Mask = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
2172 if (!Mask)
2173 return;
2174 KnownBits Known;
2175 DAG.computeKnownBits(C.Op0.getOperand(0), Known);
2176 if ((~Known.Zero).getZExtValue() & ~Mask->getZExtValue())
2177 return;
2178
2179 C.Op0 = C.Op0.getOperand(0);
2180}
2181
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002182// Return a Comparison that tests the condition-code result of intrinsic
2183// node Call against constant integer CC using comparison code Cond.
2184// Opcode is the opcode of the SystemZISD operation for the intrinsic
2185// and CCValid is the set of possible condition-code results.
2186static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode,
2187 SDValue Call, unsigned CCValid, uint64_t CC,
2188 ISD::CondCode Cond) {
2189 Comparison C(Call, SDValue());
2190 C.Opcode = Opcode;
2191 C.CCValid = CCValid;
2192 if (Cond == ISD::SETEQ)
2193 // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3.
2194 C.CCMask = CC < 4 ? 1 << (3 - CC) : 0;
2195 else if (Cond == ISD::SETNE)
2196 // ...and the inverse of that.
2197 C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1;
2198 else if (Cond == ISD::SETLT || Cond == ISD::SETULT)
2199 // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3,
2200 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002201 C.CCMask = CC < 4 ? ~0U << (4 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002202 else if (Cond == ISD::SETGE || Cond == ISD::SETUGE)
2203 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002204 C.CCMask = CC < 4 ? ~(~0U << (4 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002205 else if (Cond == ISD::SETLE || Cond == ISD::SETULE)
2206 // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true),
2207 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002208 C.CCMask = CC < 4 ? ~0U << (3 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002209 else if (Cond == ISD::SETGT || Cond == ISD::SETUGT)
2210 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002211 C.CCMask = CC < 4 ? ~(~0U << (3 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002212 else
2213 llvm_unreachable("Unexpected integer comparison type");
2214 C.CCMask &= CCValid;
2215 return C;
2216}
2217
Richard Sandifordd420f732013-12-13 15:28:45 +00002218// Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
2219static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002220 ISD::CondCode Cond, const SDLoc &DL) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002221 if (CmpOp1.getOpcode() == ISD::Constant) {
2222 uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue();
2223 unsigned Opcode, CCValid;
2224 if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
2225 CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) &&
2226 isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid))
2227 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002228 if (CmpOp0.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
2229 CmpOp0.getResNo() == CmpOp0->getNumValues() - 1 &&
2230 isIntrinsicWithCC(CmpOp0, Opcode, CCValid))
2231 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002232 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002233 Comparison C(CmpOp0, CmpOp1);
2234 C.CCMask = CCMaskForCondCode(Cond);
2235 if (C.Op0.getValueType().isFloatingPoint()) {
2236 C.CCValid = SystemZ::CCMASK_FCMP;
2237 C.Opcode = SystemZISD::FCMP;
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002238 adjustForFNeg(C);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002239 } else {
Richard Sandifordd420f732013-12-13 15:28:45 +00002240 C.CCValid = SystemZ::CCMASK_ICMP;
2241 C.Opcode = SystemZISD::ICMP;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002242 // Choose the type of comparison. Equality and inequality tests can
2243 // use either signed or unsigned comparisons. The choice also doesn't
2244 // matter if both sign bits are known to be clear. In those cases we
2245 // want to give the main isel code the freedom to choose whichever
2246 // form fits best.
Richard Sandifordd420f732013-12-13 15:28:45 +00002247 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
2248 C.CCMask == SystemZ::CCMASK_CMP_NE ||
2249 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
2250 C.ICmpType = SystemZICMP::Any;
2251 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
2252 C.ICmpType = SystemZICMP::UnsignedOnly;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002253 else
Richard Sandifordd420f732013-12-13 15:28:45 +00002254 C.ICmpType = SystemZICMP::SignedOnly;
2255 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
Ulrich Weigand31112892018-01-19 20:54:18 +00002256 adjustForRedundantAnd(DAG, DL, C);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002257 adjustZeroCmp(DAG, DL, C);
2258 adjustSubwordCmp(DAG, DL, C);
2259 adjustForSubtraction(DAG, DL, C);
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002260 adjustForLTGFR(C);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002261 adjustICmpTruncate(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002262 }
2263
Richard Sandifordd420f732013-12-13 15:28:45 +00002264 if (shouldSwapCmpOperands(C)) {
2265 std::swap(C.Op0, C.Op1);
2266 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford24e597b2013-08-23 11:27:19 +00002267 }
2268
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002269 adjustForTestUnderMask(DAG, DL, C);
Richard Sandifordd420f732013-12-13 15:28:45 +00002270 return C;
2271}
2272
2273// Emit the comparison instruction described by C.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002274static SDValue emitCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002275 if (!C.Op1.getNode()) {
2276 SDValue Op;
2277 switch (C.Op0.getOpcode()) {
2278 case ISD::INTRINSIC_W_CHAIN:
2279 Op = emitIntrinsicWithChainAndGlue(DAG, C.Op0, C.Opcode);
2280 break;
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002281 case ISD::INTRINSIC_WO_CHAIN:
2282 Op = emitIntrinsicWithGlue(DAG, C.Op0, C.Opcode);
2283 break;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002284 default:
2285 llvm_unreachable("Invalid comparison operands");
2286 }
2287 return SDValue(Op.getNode(), Op->getNumValues() - 1);
2288 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002289 if (C.Opcode == SystemZISD::ICMP)
2290 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002291 DAG.getConstant(C.ICmpType, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002292 if (C.Opcode == SystemZISD::TM) {
2293 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
2294 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
2295 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002296 DAG.getConstant(RegisterOnly, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002297 }
2298 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002299}
2300
Richard Sandiford7d86e472013-08-21 09:34:56 +00002301// Implement a 32-bit *MUL_LOHI operation by extending both operands to
2302// 64 bits. Extend is the extension type to use. Store the high part
2303// in Hi and the low part in Lo.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002304static void lowerMUL_LOHI32(SelectionDAG &DAG, const SDLoc &DL, unsigned Extend,
2305 SDValue Op0, SDValue Op1, SDValue &Hi,
2306 SDValue &Lo) {
Richard Sandiford7d86e472013-08-21 09:34:56 +00002307 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
2308 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
2309 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002310 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
2311 DAG.getConstant(32, DL, MVT::i64));
Richard Sandiford7d86e472013-08-21 09:34:56 +00002312 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
2313 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
2314}
2315
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002316// Lower a binary operation that produces two VT results, one in each
2317// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
Ulrich Weigand43579cf2017-07-05 13:17:31 +00002318// and Opcode performs the GR128 operation. Store the even register result
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002319// in Even and the odd register result in Odd.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002320static void lowerGR128Binary(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigand43579cf2017-07-05 13:17:31 +00002321 unsigned Opcode, SDValue Op0, SDValue Op1,
2322 SDValue &Even, SDValue &Odd) {
2323 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped, Op0, Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002324 bool Is32Bit = is32Bit(VT);
Richard Sandifordd8163202013-09-13 09:12:44 +00002325 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
2326 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002327}
2328
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002329// Return an i32 value that is 1 if the CC value produced by Glue is
2330// in the mask CCMask and 0 otherwise. CC is known to have a value
2331// in CCValid, so other values can be ignored.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002332static SDValue emitSETCC(SelectionDAG &DAG, const SDLoc &DL, SDValue Glue,
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002333 unsigned CCValid, unsigned CCMask) {
Ulrich Weigand849a59f2018-01-19 20:52:04 +00002334 SDValue Ops[] = { DAG.getConstant(1, DL, MVT::i32),
2335 DAG.getConstant(0, DL, MVT::i32),
2336 DAG.getConstant(CCValid, DL, MVT::i32),
2337 DAG.getConstant(CCMask, DL, MVT::i32), Glue };
2338 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, MVT::i32, Ops);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002339}
2340
Ulrich Weigandcd808232015-05-05 19:26:48 +00002341// Return the SystemISD vector comparison operation for CC, or 0 if it cannot
2342// be done directly. IsFP is true if CC is for a floating-point rather than
2343// integer comparison.
2344static unsigned getVectorComparison(ISD::CondCode CC, bool IsFP) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002345 switch (CC) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002346 case ISD::SETOEQ:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002347 case ISD::SETEQ:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002348 return IsFP ? SystemZISD::VFCMPE : SystemZISD::VICMPE;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002349
Ulrich Weigandcd808232015-05-05 19:26:48 +00002350 case ISD::SETOGE:
2351 case ISD::SETGE:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002352 return IsFP ? SystemZISD::VFCMPHE : static_cast<SystemZISD::NodeType>(0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002353
2354 case ISD::SETOGT:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002355 case ISD::SETGT:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002356 return IsFP ? SystemZISD::VFCMPH : SystemZISD::VICMPH;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002357
2358 case ISD::SETUGT:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002359 return IsFP ? static_cast<SystemZISD::NodeType>(0) : SystemZISD::VICMPHL;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002360
2361 default:
2362 return 0;
2363 }
2364}
2365
2366// Return the SystemZISD vector comparison operation for CC or its inverse,
2367// or 0 if neither can be done directly. Indicate in Invert whether the
Ulrich Weigandcd808232015-05-05 19:26:48 +00002368// result is for the inverse of CC. IsFP is true if CC is for a
2369// floating-point rather than integer comparison.
2370static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, bool IsFP,
2371 bool &Invert) {
2372 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002373 Invert = false;
2374 return Opcode;
2375 }
2376
Ulrich Weigandcd808232015-05-05 19:26:48 +00002377 CC = ISD::getSetCCInverse(CC, !IsFP);
2378 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002379 Invert = true;
2380 return Opcode;
2381 }
2382
2383 return 0;
2384}
2385
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002386// Return a v2f64 that contains the extended form of elements Start and Start+1
2387// of v4f32 value Op.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002388static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, const SDLoc &DL,
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002389 SDValue Op) {
2390 int Mask[] = { Start, -1, Start + 1, -1 };
2391 Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask);
2392 return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op);
2393}
2394
2395// Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode,
2396// producing a result of type VT.
Ulrich Weigand33435c42017-07-17 17:42:48 +00002397SDValue SystemZTargetLowering::getVectorCmp(SelectionDAG &DAG, unsigned Opcode,
2398 const SDLoc &DL, EVT VT,
2399 SDValue CmpOp0,
2400 SDValue CmpOp1) const {
2401 // There is no hardware support for v4f32 (unless we have the vector
2402 // enhancements facility 1), so extend the vector into two v2f64s
2403 // and compare those.
2404 if (CmpOp0.getValueType() == MVT::v4f32 &&
2405 !Subtarget.hasVectorEnhancements1()) {
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002406 SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0);
2407 SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0);
2408 SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1);
2409 SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1);
2410 SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1);
2411 SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1);
2412 return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes);
2413 }
2414 return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1);
2415}
2416
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002417// Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing
2418// an integer mask of type VT.
Ulrich Weigand33435c42017-07-17 17:42:48 +00002419SDValue SystemZTargetLowering::lowerVectorSETCC(SelectionDAG &DAG,
2420 const SDLoc &DL, EVT VT,
2421 ISD::CondCode CC,
2422 SDValue CmpOp0,
2423 SDValue CmpOp1) const {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002424 bool IsFP = CmpOp0.getValueType().isFloatingPoint();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002425 bool Invert = false;
2426 SDValue Cmp;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002427 switch (CC) {
2428 // Handle tests for order using (or (ogt y x) (oge x y)).
2429 case ISD::SETUO:
2430 Invert = true;
Simon Pilgrim8c4069e2017-07-07 10:07:09 +00002431 LLVM_FALLTHROUGH;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002432 case ISD::SETO: {
2433 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002434 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2435 SDValue GE = getVectorCmp(DAG, SystemZISD::VFCMPHE, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002436 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE);
2437 break;
2438 }
2439
2440 // Handle <> tests using (or (ogt y x) (ogt x y)).
2441 case ISD::SETUEQ:
2442 Invert = true;
Simon Pilgrim8c4069e2017-07-07 10:07:09 +00002443 LLVM_FALLTHROUGH;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002444 case ISD::SETONE: {
2445 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002446 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2447 SDValue GT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002448 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT);
2449 break;
2450 }
2451
2452 // Otherwise a single comparison is enough. It doesn't really
2453 // matter whether we try the inversion or the swap first, since
2454 // there are no cases where both work.
2455 default:
2456 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002457 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002458 else {
2459 CC = ISD::getSetCCSwappedOperands(CC);
2460 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002461 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002462 else
2463 llvm_unreachable("Unhandled comparison");
2464 }
2465 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002466 }
2467 if (Invert) {
2468 SDValue Mask = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2469 DAG.getConstant(65535, DL, MVT::i32));
2470 Mask = DAG.getNode(ISD::BITCAST, DL, VT, Mask);
2471 Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask);
2472 }
2473 return Cmp;
2474}
2475
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002476SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
2477 SelectionDAG &DAG) const {
2478 SDValue CmpOp0 = Op.getOperand(0);
2479 SDValue CmpOp1 = Op.getOperand(1);
2480 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
2481 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002482 EVT VT = Op.getValueType();
2483 if (VT.isVector())
2484 return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002485
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002486 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002487 SDValue Glue = emitCmp(DAG, DL, C);
2488 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002489}
2490
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002491SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002492 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2493 SDValue CmpOp0 = Op.getOperand(2);
2494 SDValue CmpOp1 = Op.getOperand(3);
2495 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002496 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002497
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002498 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002499 SDValue Glue = emitCmp(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002500 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002501 Op.getOperand(0), DAG.getConstant(C.CCValid, DL, MVT::i32),
2502 DAG.getConstant(C.CCMask, DL, MVT::i32), Dest, Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002503}
2504
Richard Sandiford57485472013-12-13 15:35:00 +00002505// Return true if Pos is CmpOp and Neg is the negative of CmpOp,
2506// allowing Pos and Neg to be wider than CmpOp.
2507static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
2508 return (Neg.getOpcode() == ISD::SUB &&
2509 Neg.getOperand(0).getOpcode() == ISD::Constant &&
2510 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
2511 Neg.getOperand(1) == Pos &&
2512 (Pos == CmpOp ||
2513 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
2514 Pos.getOperand(0) == CmpOp)));
2515}
2516
2517// Return the absolute or negative absolute of Op; IsNegative decides which.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002518static SDValue getAbsolute(SelectionDAG &DAG, const SDLoc &DL, SDValue Op,
Richard Sandiford57485472013-12-13 15:35:00 +00002519 bool IsNegative) {
2520 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
2521 if (IsNegative)
2522 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002523 DAG.getConstant(0, DL, Op.getValueType()), Op);
Richard Sandiford57485472013-12-13 15:35:00 +00002524 return Op;
2525}
2526
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002527SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
2528 SelectionDAG &DAG) const {
2529 SDValue CmpOp0 = Op.getOperand(0);
2530 SDValue CmpOp1 = Op.getOperand(1);
2531 SDValue TrueOp = Op.getOperand(2);
2532 SDValue FalseOp = Op.getOperand(3);
2533 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002534 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002535
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002536 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandiford57485472013-12-13 15:35:00 +00002537
2538 // Check for absolute and negative-absolute selections, including those
2539 // where the comparison value is sign-extended (for LPGFR and LNGFR).
2540 // This check supplements the one in DAGCombiner.
2541 if (C.Opcode == SystemZISD::ICMP &&
2542 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
2543 C.CCMask != SystemZ::CCMASK_CMP_NE &&
2544 C.Op1.getOpcode() == ISD::Constant &&
2545 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
2546 if (isAbsolute(C.Op0, TrueOp, FalseOp))
2547 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
2548 if (isAbsolute(C.Op0, FalseOp, TrueOp))
2549 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
2550 }
2551
Richard Sandifordd420f732013-12-13 15:28:45 +00002552 SDValue Glue = emitCmp(DAG, DL, C);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002553 SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, DL, MVT::i32),
2554 DAG.getConstant(C.CCMask, DL, MVT::i32), Glue};
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002555
Ulrich Weigand849a59f2018-01-19 20:52:04 +00002556 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, Op.getValueType(), Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002557}
2558
2559SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
2560 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002561 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002562 const GlobalValue *GV = Node->getGlobal();
2563 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002564 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopher93bf97c2014-06-27 07:38:01 +00002565 CodeModel::Model CM = DAG.getTarget().getCodeModel();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002566
2567 SDValue Result;
Rafael Espindola3beef8d2016-06-27 23:15:57 +00002568 if (Subtarget.isPC32DBLSymbol(GV, CM)) {
Richard Sandiford54b36912013-09-27 15:14:04 +00002569 // Assign anchors at 1<<12 byte boundaries.
2570 uint64_t Anchor = Offset & ~uint64_t(0xfff);
2571 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
2572 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2573
2574 // The offset can be folded into the address if it is aligned to a halfword.
2575 Offset -= Anchor;
2576 if (Offset != 0 && (Offset & 1) == 0) {
2577 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
2578 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002579 Offset = 0;
2580 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002581 } else {
2582 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
2583 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2584 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Justin Lebar9c375812016-07-15 18:27:10 +00002585 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002586 }
2587
2588 // If there was a non-zero offset that we didn't fold, create an explicit
2589 // addition for it.
2590 if (Offset != 0)
2591 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002592 DAG.getConstant(Offset, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002593
2594 return Result;
2595}
2596
Ulrich Weigand7db69182015-02-18 09:13:27 +00002597SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
2598 SelectionDAG &DAG,
2599 unsigned Opcode,
2600 SDValue GOTOffset) const {
2601 SDLoc DL(Node);
Mehdi Amini44ede332015-07-09 02:09:04 +00002602 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand7db69182015-02-18 09:13:27 +00002603 SDValue Chain = DAG.getEntryNode();
2604 SDValue Glue;
2605
2606 // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
2607 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
2608 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
2609 Glue = Chain.getValue(1);
2610 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
2611 Glue = Chain.getValue(1);
2612
2613 // The first call operand is the chain and the second is the TLS symbol.
2614 SmallVector<SDValue, 8> Ops;
2615 Ops.push_back(Chain);
2616 Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
2617 Node->getValueType(0),
2618 0, 0));
2619
2620 // Add argument registers to the end of the list so that they are
2621 // known live into the call.
2622 Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
2623 Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
2624
2625 // Add a register mask operand representing the call-preserved registers.
2626 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002627 const uint32_t *Mask =
2628 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002629 assert(Mask && "Missing call preserved mask for calling convention");
2630 Ops.push_back(DAG.getRegisterMask(Mask));
2631
2632 // Glue the call to the argument copies.
2633 Ops.push_back(Glue);
2634
2635 // Emit the call.
2636 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2637 Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
2638 Glue = Chain.getValue(1);
2639
2640 // Copy the return value from %r2.
2641 return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
2642}
2643
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002644SDValue SystemZTargetLowering::lowerThreadPointer(const SDLoc &DL,
2645 SelectionDAG &DAG) const {
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002646 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002647 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002648
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002649 // The high part of the thread pointer is in access register 0.
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002650 SDValue TPHi = DAG.getCopyFromReg(Chain, DL, SystemZ::A0, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002651 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
2652
2653 // The low part of the thread pointer is in access register 1.
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002654 SDValue TPLo = DAG.getCopyFromReg(Chain, DL, SystemZ::A1, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002655 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
2656
2657 // Merge them into a single 64-bit address.
2658 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002659 DAG.getConstant(32, DL, PtrVT));
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002660 return DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
2661}
2662
2663SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
2664 SelectionDAG &DAG) const {
2665 if (DAG.getTarget().Options.EmulatedTLS)
2666 return LowerToTLSEmulatedModel(Node, DAG);
2667 SDLoc DL(Node);
2668 const GlobalValue *GV = Node->getGlobal();
2669 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2670 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
2671
2672 SDValue TP = lowerThreadPointer(DL, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002673
Ulrich Weigand7db69182015-02-18 09:13:27 +00002674 // Get the offset of GA from the thread pointer, based on the TLS model.
2675 SDValue Offset;
2676 switch (model) {
2677 case TLSModel::GeneralDynamic: {
2678 // Load the GOT offset of the tls_index (module ID / per-symbol offset).
2679 SystemZConstantPoolValue *CPV =
2680 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002681
Ulrich Weigand7db69182015-02-18 09:13:27 +00002682 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002683 Offset = DAG.getLoad(
2684 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002685 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002686
2687 // Call __tls_get_offset to retrieve the offset.
2688 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
2689 break;
2690 }
2691
2692 case TLSModel::LocalDynamic: {
2693 // Load the GOT offset of the module ID.
2694 SystemZConstantPoolValue *CPV =
2695 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
2696
2697 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002698 Offset = DAG.getLoad(
2699 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002700 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002701
2702 // Call __tls_get_offset to retrieve the module base offset.
2703 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
2704
2705 // Note: The SystemZLDCleanupPass will remove redundant computations
2706 // of the module base offset. Count total number of local-dynamic
2707 // accesses to trigger execution of that pass.
2708 SystemZMachineFunctionInfo* MFI =
2709 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
2710 MFI->incNumLocalDynamicTLSAccesses();
2711
2712 // Add the per-symbol offset.
2713 CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
2714
2715 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002716 DTPOffset = DAG.getLoad(
2717 PtrVT, DL, DAG.getEntryNode(), DTPOffset,
Justin Lebar9c375812016-07-15 18:27:10 +00002718 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002719
2720 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
2721 break;
2722 }
2723
2724 case TLSModel::InitialExec: {
2725 // Load the offset from the GOT.
2726 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2727 SystemZII::MO_INDNTPOFF);
2728 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +00002729 Offset =
2730 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Offset,
2731 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002732 break;
2733 }
2734
2735 case TLSModel::LocalExec: {
2736 // Force the offset into the constant pool and load it from there.
2737 SystemZConstantPoolValue *CPV =
2738 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
2739
2740 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002741 Offset = DAG.getLoad(
2742 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002743 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002744 break;
Ulrich Weigandb7e59092015-02-18 09:42:23 +00002745 }
Ulrich Weigand7db69182015-02-18 09:13:27 +00002746 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002747
2748 // Add the base and offset together.
2749 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
2750}
2751
2752SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
2753 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002754 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002755 const BlockAddress *BA = Node->getBlockAddress();
2756 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002757 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002758
2759 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
2760 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2761 return Result;
2762}
2763
2764SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
2765 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002766 SDLoc DL(JT);
Mehdi Amini44ede332015-07-09 02:09:04 +00002767 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002768 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
2769
2770 // Use LARL to load the address of the table.
2771 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2772}
2773
2774SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
2775 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002776 SDLoc DL(CP);
Mehdi Amini44ede332015-07-09 02:09:04 +00002777 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002778
2779 SDValue Result;
2780 if (CP->isMachineConstantPoolEntry())
2781 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002782 CP->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002783 else
2784 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002785 CP->getAlignment(), CP->getOffset());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002786
2787 // Use LARL to load the address of the constant pool entry.
2788 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2789}
2790
Ulrich Weigandf557d082016-04-04 12:44:55 +00002791SDValue SystemZTargetLowering::lowerFRAMEADDR(SDValue Op,
2792 SelectionDAG &DAG) const {
2793 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002794 MachineFrameInfo &MFI = MF.getFrameInfo();
2795 MFI.setFrameAddressIsTaken(true);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002796
2797 SDLoc DL(Op);
2798 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2799 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2800
2801 // If the back chain frame index has not been allocated yet, do so.
2802 SystemZMachineFunctionInfo *FI = MF.getInfo<SystemZMachineFunctionInfo>();
2803 int BackChainIdx = FI->getFramePointerSaveIndex();
2804 if (!BackChainIdx) {
2805 // By definition, the frame address is the address of the back chain.
Matthias Braun941a7052016-07-28 18:40:00 +00002806 BackChainIdx = MFI.CreateFixedObject(8, -SystemZMC::CallFrameSize, false);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002807 FI->setFramePointerSaveIndex(BackChainIdx);
2808 }
2809 SDValue BackChain = DAG.getFrameIndex(BackChainIdx, PtrVT);
2810
2811 // FIXME The frontend should detect this case.
2812 if (Depth > 0) {
2813 report_fatal_error("Unsupported stack frame traversal count");
2814 }
2815
2816 return BackChain;
2817}
2818
2819SDValue SystemZTargetLowering::lowerRETURNADDR(SDValue Op,
2820 SelectionDAG &DAG) const {
2821 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002822 MachineFrameInfo &MFI = MF.getFrameInfo();
2823 MFI.setReturnAddressIsTaken(true);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002824
2825 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
2826 return SDValue();
2827
2828 SDLoc DL(Op);
2829 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2830 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2831
2832 // FIXME The frontend should detect this case.
2833 if (Depth > 0) {
2834 report_fatal_error("Unsupported stack frame traversal count");
2835 }
2836
2837 // Return R14D, which has the return address. Mark it an implicit live-in.
2838 unsigned LinkReg = MF.addLiveIn(SystemZ::R14D, &SystemZ::GR64BitRegClass);
2839 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, LinkReg, PtrVT);
2840}
2841
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002842SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
2843 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002844 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002845 SDValue In = Op.getOperand(0);
2846 EVT InVT = In.getValueType();
2847 EVT ResVT = Op.getValueType();
2848
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002849 // Convert loads directly. This is normally done by DAGCombiner,
2850 // but we need this case for bitcasts that are created during lowering
2851 // and which are then lowered themselves.
2852 if (auto *LoadN = dyn_cast<LoadSDNode>(In))
Jonas Paulssonb9a24672017-11-30 08:18:50 +00002853 if (ISD::isNormalLoad(LoadN)) {
2854 SDValue NewLoad = DAG.getLoad(ResVT, DL, LoadN->getChain(),
2855 LoadN->getBasePtr(), LoadN->getMemOperand());
2856 // Update the chain uses.
2857 DAG.ReplaceAllUsesOfValueWith(SDValue(LoadN, 1), NewLoad.getValue(1));
2858 return NewLoad;
2859 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002860
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002861 if (InVT == MVT::i32 && ResVT == MVT::f32) {
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002862 SDValue In64;
2863 if (Subtarget.hasHighWord()) {
2864 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
2865 MVT::i64);
2866 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
2867 MVT::i64, SDValue(U64, 0), In);
2868 } else {
2869 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
2870 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002871 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002872 }
2873 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002874 return DAG.getTargetExtractSubreg(SystemZ::subreg_r32,
Richard Sandifordd8163202013-09-13 09:12:44 +00002875 DL, MVT::f32, Out64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002876 }
2877 if (InVT == MVT::f32 && ResVT == MVT::i32) {
2878 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002879 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002880 MVT::f64, SDValue(U64, 0), In);
2881 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002882 if (Subtarget.hasHighWord())
2883 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
2884 MVT::i32, Out64);
2885 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002886 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002887 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002888 }
2889 llvm_unreachable("Unexpected bitcast combination");
2890}
2891
2892SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
2893 SelectionDAG &DAG) const {
2894 MachineFunction &MF = DAG.getMachineFunction();
2895 SystemZMachineFunctionInfo *FuncInfo =
2896 MF.getInfo<SystemZMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002897 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002898
2899 SDValue Chain = Op.getOperand(0);
2900 SDValue Addr = Op.getOperand(1);
2901 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002902 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002903
2904 // The initial values of each field.
2905 const unsigned NumFields = 4;
2906 SDValue Fields[NumFields] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002907 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT),
2908 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002909 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
2910 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
2911 };
2912
2913 // Store each field into its respective slot.
2914 SDValue MemOps[NumFields];
2915 unsigned Offset = 0;
2916 for (unsigned I = 0; I < NumFields; ++I) {
2917 SDValue FieldAddr = Addr;
2918 if (Offset != 0)
2919 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002920 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002921 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00002922 MachinePointerInfo(SV, Offset));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002923 Offset += 8;
2924 }
Craig Topper48d114b2014-04-26 18:35:24 +00002925 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002926}
2927
2928SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
2929 SelectionDAG &DAG) const {
2930 SDValue Chain = Op.getOperand(0);
2931 SDValue DstPtr = Op.getOperand(1);
2932 SDValue SrcPtr = Op.getOperand(2);
2933 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
2934 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002935 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002936
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002937 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002938 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00002939 /*isTailCall*/false,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002940 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
2941}
2942
2943SDValue SystemZTargetLowering::
2944lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002945 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00002946 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braunf1caa282017-12-15 22:22:58 +00002947 bool RealignOpt = !MF.getFunction().hasFnAttribute("no-realign-stack");
2948 bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002949
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002950 SDValue Chain = Op.getOperand(0);
2951 SDValue Size = Op.getOperand(1);
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002952 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002953 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002954
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002955 // If user has set the no alignment function attribute, ignore
2956 // alloca alignments.
2957 uint64_t AlignVal = (RealignOpt ?
2958 dyn_cast<ConstantSDNode>(Align)->getZExtValue() : 0);
2959
2960 uint64_t StackAlign = TFI->getStackAlignment();
2961 uint64_t RequiredAlign = std::max(AlignVal, StackAlign);
2962 uint64_t ExtraAlignSpace = RequiredAlign - StackAlign;
2963
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002964 unsigned SPReg = getStackPointerRegisterToSaveRestore();
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002965 SDValue NeededSpace = Size;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002966
2967 // Get a reference to the stack pointer.
2968 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
2969
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00002970 // If we need a backchain, save it now.
2971 SDValue Backchain;
2972 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00002973 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00002974
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002975 // Add extra space for alignment if needed.
2976 if (ExtraAlignSpace)
2977 NeededSpace = DAG.getNode(ISD::ADD, DL, MVT::i64, NeededSpace,
Elliot Colpbc2cfc22016-07-06 18:13:11 +00002978 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002979
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002980 // Get the new stack pointer value.
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002981 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, NeededSpace);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002982
2983 // Copy the new stack pointer back.
2984 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
2985
2986 // The allocated data lives above the 160 bytes allocated for the standard
2987 // frame, plus any outgoing stack arguments. We don't know how much that
2988 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
2989 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
2990 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
2991
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002992 // Dynamically realign if needed.
2993 if (RequiredAlign > StackAlign) {
2994 Result =
2995 DAG.getNode(ISD::ADD, DL, MVT::i64, Result,
2996 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
2997 Result =
2998 DAG.getNode(ISD::AND, DL, MVT::i64, Result,
2999 DAG.getConstant(~(RequiredAlign - 1), DL, MVT::i64));
3000 }
3001
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003002 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003003 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003004
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003005 SDValue Ops[2] = { Result, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00003006 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003007}
3008
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +00003009SDValue SystemZTargetLowering::lowerGET_DYNAMIC_AREA_OFFSET(
3010 SDValue Op, SelectionDAG &DAG) const {
3011 SDLoc DL(Op);
3012
3013 return DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
3014}
3015
Richard Sandiford7d86e472013-08-21 09:34:56 +00003016SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
3017 SelectionDAG &DAG) const {
3018 EVT VT = Op.getValueType();
3019 SDLoc DL(Op);
3020 SDValue Ops[2];
3021 if (is32Bit(VT))
3022 // Just do a normal 64-bit multiplication and extract the results.
3023 // We define this so that it can be used for constant division.
3024 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
3025 Op.getOperand(1), Ops[1], Ops[0]);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +00003026 else if (Subtarget.hasMiscellaneousExtensions2())
3027 // SystemZISD::SMUL_LOHI returns the low result in the odd register and
3028 // the high result in the even register. ISD::SMUL_LOHI is defined to
3029 // return the low half first, so the results are in reverse order.
3030 lowerGR128Binary(DAG, DL, VT, SystemZISD::SMUL_LOHI,
3031 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003032 else {
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003033 // Do a full 128-bit multiplication based on SystemZISD::UMUL_LOHI:
Richard Sandiford7d86e472013-08-21 09:34:56 +00003034 //
3035 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
3036 //
3037 // but using the fact that the upper halves are either all zeros
3038 // or all ones:
3039 //
3040 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
3041 //
3042 // and grouping the right terms together since they are quicker than the
3043 // multiplication:
3044 //
3045 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003046 SDValue C63 = DAG.getConstant(63, DL, MVT::i64);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003047 SDValue LL = Op.getOperand(0);
3048 SDValue RL = Op.getOperand(1);
3049 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
3050 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003051 // SystemZISD::UMUL_LOHI returns the low result in the odd register and
3052 // the high result in the even register. ISD::SMUL_LOHI is defined to
3053 // return the low half first, so the results are in reverse order.
3054 lowerGR128Binary(DAG, DL, VT, SystemZISD::UMUL_LOHI,
Richard Sandiford7d86e472013-08-21 09:34:56 +00003055 LL, RL, Ops[1], Ops[0]);
3056 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
3057 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
3058 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
3059 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
3060 }
Craig Topper64941d92014-04-27 19:20:57 +00003061 return DAG.getMergeValues(Ops, DL);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003062}
3063
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003064SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
3065 SelectionDAG &DAG) const {
3066 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003067 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003068 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00003069 if (is32Bit(VT))
3070 // Just do a normal 64-bit multiplication and extract the results.
3071 // We define this so that it can be used for constant division.
3072 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
3073 Op.getOperand(1), Ops[1], Ops[0]);
3074 else
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003075 // SystemZISD::UMUL_LOHI returns the low result in the odd register and
3076 // the high result in the even register. ISD::UMUL_LOHI is defined to
3077 // return the low half first, so the results are in reverse order.
3078 lowerGR128Binary(DAG, DL, VT, SystemZISD::UMUL_LOHI,
Richard Sandiford7d86e472013-08-21 09:34:56 +00003079 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003080 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003081}
3082
3083SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
3084 SelectionDAG &DAG) const {
3085 SDValue Op0 = Op.getOperand(0);
3086 SDValue Op1 = Op.getOperand(1);
3087 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003088 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003089
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003090 // We use DSGF for 32-bit division. This means the first operand must
3091 // always be 64-bit, and the second operand should be 32-bit whenever
3092 // that is possible, to improve performance.
3093 if (is32Bit(VT))
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003094 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003095 else if (DAG.ComputeNumSignBits(Op1) > 32)
Richard Sandiforde6e78852013-07-02 15:40:22 +00003096 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003097
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003098 // DSG(F) returns the remainder in the even register and the
3099 // quotient in the odd register.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003100 SDValue Ops[2];
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003101 lowerGR128Binary(DAG, DL, VT, SystemZISD::SDIVREM, Op0, Op1, Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003102 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003103}
3104
3105SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
3106 SelectionDAG &DAG) const {
3107 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003108 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003109
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003110 // DL(G) returns the remainder in the even register and the
3111 // quotient in the odd register.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003112 SDValue Ops[2];
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003113 lowerGR128Binary(DAG, DL, VT, SystemZISD::UDIVREM,
3114 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003115 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003116}
3117
3118SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
3119 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
3120
3121 // Get the known-zero masks for each operand.
3122 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
Craig Topperd0af7e82017-04-28 05:31:46 +00003123 KnownBits Known[2];
3124 DAG.computeKnownBits(Ops[0], Known[0]);
3125 DAG.computeKnownBits(Ops[1], Known[1]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003126
3127 // See if the upper 32 bits of one operand and the lower 32 bits of the
3128 // other are known zero. They are the low and high operands respectively.
Craig Topperd0af7e82017-04-28 05:31:46 +00003129 uint64_t Masks[] = { Known[0].Zero.getZExtValue(),
3130 Known[1].Zero.getZExtValue() };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003131 unsigned High, Low;
3132 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
3133 High = 1, Low = 0;
3134 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
3135 High = 0, Low = 1;
3136 else
3137 return Op;
3138
3139 SDValue LowOp = Ops[Low];
3140 SDValue HighOp = Ops[High];
3141
3142 // If the high part is a constant, we're better off using IILH.
3143 if (HighOp.getOpcode() == ISD::Constant)
3144 return Op;
3145
3146 // If the low part is a constant that is outside the range of LHI,
3147 // then we're better off using IILF.
3148 if (LowOp.getOpcode() == ISD::Constant) {
3149 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
3150 if (!isInt<16>(Value))
3151 return Op;
3152 }
3153
3154 // Check whether the high part is an AND that doesn't change the
3155 // high 32 bits and just masks out low bits. We can skip it if so.
3156 if (HighOp.getOpcode() == ISD::AND &&
3157 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
Richard Sandifordccc2a7c2013-12-03 11:01:54 +00003158 SDValue HighOp0 = HighOp.getOperand(0);
3159 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
3160 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
3161 HighOp = HighOp0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003162 }
3163
3164 // Take advantage of the fact that all GR32 operations only change the
3165 // low 32 bits by truncating Low to an i32 and inserting it directly
3166 // using a subreg. The interesting cases are those where the truncation
3167 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003168 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003169 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
Richard Sandiford87a44362013-09-30 10:28:35 +00003170 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00003171 MVT::i64, HighOp, Low32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003172}
3173
Ulrich Weigandb4012182015-03-31 12:56:33 +00003174SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
3175 SelectionDAG &DAG) const {
3176 EVT VT = Op.getValueType();
Ulrich Weigandb4012182015-03-31 12:56:33 +00003177 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003178 Op = Op.getOperand(0);
3179
3180 // Handle vector types via VPOPCT.
3181 if (VT.isVector()) {
3182 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op);
3183 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00003184 switch (VT.getScalarSizeInBits()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003185 case 8:
3186 break;
3187 case 16: {
3188 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
3189 SDValue Shift = DAG.getConstant(8, DL, MVT::i32);
3190 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift);
3191 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3192 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift);
3193 break;
3194 }
3195 case 32: {
3196 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3197 DAG.getConstant(0, DL, MVT::i32));
3198 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3199 break;
3200 }
3201 case 64: {
3202 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3203 DAG.getConstant(0, DL, MVT::i32));
3204 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp);
3205 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3206 break;
3207 }
3208 default:
3209 llvm_unreachable("Unexpected type");
3210 }
3211 return Op;
3212 }
Ulrich Weigandb4012182015-03-31 12:56:33 +00003213
3214 // Get the known-zero mask for the operand.
Craig Topperd0af7e82017-04-28 05:31:46 +00003215 KnownBits Known;
3216 DAG.computeKnownBits(Op, Known);
3217 unsigned NumSignificantBits = (~Known.Zero).getActiveBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003218 if (NumSignificantBits == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003219 return DAG.getConstant(0, DL, VT);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003220
3221 // Skip known-zero high parts of the operand.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003222 int64_t OrigBitSize = VT.getSizeInBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003223 int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits);
3224 BitSize = std::min(BitSize, OrigBitSize);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003225
3226 // The POPCNT instruction counts the number of bits in each byte.
3227 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op);
3228 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op);
3229 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
3230
3231 // Add up per-byte counts in a binary tree. All bits of Op at
3232 // position larger than BitSize remain zero throughout.
3233 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003234 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003235 if (BitSize != OrigBitSize)
3236 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003237 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003238 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3239 }
3240
3241 // Extract overall result from high byte.
3242 if (BitSize > 8)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003243 Op = DAG.getNode(ISD::SRL, DL, VT, Op,
3244 DAG.getConstant(BitSize - 8, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003245
3246 return Op;
3247}
3248
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003249SDValue SystemZTargetLowering::lowerATOMIC_FENCE(SDValue Op,
3250 SelectionDAG &DAG) const {
3251 SDLoc DL(Op);
3252 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
3253 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003254 SyncScope::ID FenceSSID = static_cast<SyncScope::ID>(
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003255 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
3256
3257 // The only fence that needs an instruction is a sequentially-consistent
3258 // cross-thread fence.
JF Bastien800f87a2016-04-06 21:19:33 +00003259 if (FenceOrdering == AtomicOrdering::SequentiallyConsistent &&
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003260 FenceSSID == SyncScope::System) {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003261 return SDValue(DAG.getMachineNode(SystemZ::Serialize, DL, MVT::Other,
JF Bastien800f87a2016-04-06 21:19:33 +00003262 Op.getOperand(0)),
3263 0);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003264 }
3265
3266 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
3267 return DAG.getNode(SystemZISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0));
3268}
3269
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003270// Op is an atomic load. Lower it into a normal volatile load.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003271SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
3272 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003273 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003274 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003275 Node->getChain(), Node->getBasePtr(),
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003276 Node->getMemoryVT(), Node->getMemOperand());
3277}
3278
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003279// Op is an atomic store. Lower it into a normal volatile store.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003280SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
3281 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003282 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003283 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
3284 Node->getBasePtr(), Node->getMemoryVT(),
3285 Node->getMemOperand());
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003286 // We have to enforce sequential consistency by performing a
3287 // serialization operation after the store.
3288 if (Node->getOrdering() == AtomicOrdering::SequentiallyConsistent)
3289 Chain = SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op),
3290 MVT::Other, Chain), 0);
3291 return Chain;
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003292}
3293
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003294// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
3295// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003296SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
3297 SelectionDAG &DAG,
3298 unsigned Opcode) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003299 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003300
3301 // 32-bit operations need no code outside the main loop.
3302 EVT NarrowVT = Node->getMemoryVT();
3303 EVT WideVT = MVT::i32;
3304 if (NarrowVT == WideVT)
3305 return Op;
3306
3307 int64_t BitSize = NarrowVT.getSizeInBits();
3308 SDValue ChainIn = Node->getChain();
3309 SDValue Addr = Node->getBasePtr();
3310 SDValue Src2 = Node->getVal();
3311 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003312 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003313 EVT PtrVT = Addr.getValueType();
3314
3315 // Convert atomic subtracts of constants into additions.
3316 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
Richard Sandiford21f5d682014-03-06 11:22:58 +00003317 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003318 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003319 Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003320 }
3321
3322 // Get the address of the containing word.
3323 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003324 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003325
3326 // Get the number of bits that the word must be rotated left in order
3327 // to bring the field to the top bits of a GR32.
3328 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003329 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003330 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3331
3332 // Get the complementing shift amount, for rotating a field in the top
3333 // bits back to its proper position.
3334 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003335 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003336
3337 // Extend the source operand to 32 bits and prepare it for the inner loop.
3338 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
3339 // operations require the source to be shifted in advance. (This shift
3340 // can be folded if the source is constant.) For AND and NAND, the lower
3341 // bits must be set, while for other opcodes they should be left clear.
3342 if (Opcode != SystemZISD::ATOMIC_SWAPW)
3343 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003344 DAG.getConstant(32 - BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003345 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
3346 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
3347 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003348 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003349
3350 // Construct the ATOMIC_LOADW_* node.
3351 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3352 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003353 DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003354 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003355 NarrowVT, MMO);
3356
3357 // Rotate the result of the final CS so that the field is in the lower
3358 // bits of a GR32, then truncate it.
3359 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003360 DAG.getConstant(BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003361 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
3362
3363 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00003364 return DAG.getMergeValues(RetOps, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003365}
3366
Richard Sandiford41350a52013-12-24 15:18:04 +00003367// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
Richard Sandiford002019a2013-12-24 15:22:39 +00003368// into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
Richard Sandiford41350a52013-12-24 15:18:04 +00003369// operations into additions.
3370SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
3371 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003372 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandiford41350a52013-12-24 15:18:04 +00003373 EVT MemVT = Node->getMemoryVT();
3374 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
3375 // A full-width operation.
3376 assert(Op.getValueType() == MemVT && "Mismatched VTs");
3377 SDValue Src2 = Node->getVal();
3378 SDValue NegSrc2;
3379 SDLoc DL(Src2);
3380
Richard Sandiford21f5d682014-03-06 11:22:58 +00003381 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
Richard Sandiford41350a52013-12-24 15:18:04 +00003382 // Use an addition if the operand is constant and either LAA(G) is
3383 // available or the negative value is in the range of A(G)FHI.
3384 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
Eric Christopher93bf97c2014-06-27 07:38:01 +00003385 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003386 NegSrc2 = DAG.getConstant(Value, DL, MemVT);
Eric Christopher93bf97c2014-06-27 07:38:01 +00003387 } else if (Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00003388 // Use LAA(G) if available.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003389 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT),
Richard Sandiford41350a52013-12-24 15:18:04 +00003390 Src2);
3391
3392 if (NegSrc2.getNode())
3393 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
3394 Node->getChain(), Node->getBasePtr(), NegSrc2,
Konstantin Zhuravlyov8ea02462016-10-15 22:01:18 +00003395 Node->getMemOperand());
Richard Sandiford41350a52013-12-24 15:18:04 +00003396
3397 // Use the node as-is.
3398 return Op;
3399 }
3400
3401 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
3402}
3403
Ulrich Weigand0f1de042017-09-28 16:22:54 +00003404// Lower 8/16/32/64-bit ATOMIC_CMP_SWAP_WITH_SUCCESS node.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003405SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
3406 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003407 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003408 SDValue ChainIn = Node->getOperand(0);
3409 SDValue Addr = Node->getOperand(1);
3410 SDValue CmpVal = Node->getOperand(2);
3411 SDValue SwapVal = Node->getOperand(3);
3412 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003413 SDLoc DL(Node);
Ulrich Weigand0f1de042017-09-28 16:22:54 +00003414
3415 // We have native support for 32-bit and 64-bit compare and swap, but we
3416 // still need to expand extracting the "success" result from the CC.
3417 EVT NarrowVT = Node->getMemoryVT();
3418 EVT WideVT = NarrowVT == MVT::i64 ? MVT::i64 : MVT::i32;
3419 if (NarrowVT == WideVT) {
3420 SDVTList Tys = DAG.getVTList(WideVT, MVT::Other, MVT::Glue);
3421 SDValue Ops[] = { ChainIn, Addr, CmpVal, SwapVal };
3422 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAP,
3423 DL, Tys, Ops, NarrowVT, MMO);
3424 SDValue Success = emitSETCC(DAG, DL, AtomicOp.getValue(2),
3425 SystemZ::CCMASK_CS, SystemZ::CCMASK_CS_EQ);
3426
3427 DAG.ReplaceAllUsesOfValueWith(Op.getValue(0), AtomicOp.getValue(0));
3428 DAG.ReplaceAllUsesOfValueWith(Op.getValue(1), Success);
3429 DAG.ReplaceAllUsesOfValueWith(Op.getValue(2), AtomicOp.getValue(1));
3430 return SDValue();
3431 }
3432
3433 // Convert 8-bit and 16-bit compare and swap to a loop, implemented
3434 // via a fullword ATOMIC_CMP_SWAPW operation.
3435 int64_t BitSize = NarrowVT.getSizeInBits();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003436 EVT PtrVT = Addr.getValueType();
3437
3438 // Get the address of the containing word.
3439 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003440 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003441
3442 // Get the number of bits that the word must be rotated left in order
3443 // to bring the field to the top bits of a GR32.
3444 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003445 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003446 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3447
3448 // Get the complementing shift amount, for rotating a field in the top
3449 // bits back to its proper position.
3450 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003451 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003452
3453 // Construct the ATOMIC_CMP_SWAPW node.
Ulrich Weigand0f1de042017-09-28 16:22:54 +00003454 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other, MVT::Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003455 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003456 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003457 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003458 VTList, Ops, NarrowVT, MMO);
Ulrich Weigand0f1de042017-09-28 16:22:54 +00003459 SDValue Success = emitSETCC(DAG, DL, AtomicOp.getValue(2),
3460 SystemZ::CCMASK_ICMP, SystemZ::CCMASK_CMP_EQ);
3461
3462 DAG.ReplaceAllUsesOfValueWith(Op.getValue(0), AtomicOp.getValue(0));
3463 DAG.ReplaceAllUsesOfValueWith(Op.getValue(1), Success);
3464 DAG.ReplaceAllUsesOfValueWith(Op.getValue(2), AtomicOp.getValue(1));
3465 return SDValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003466}
3467
3468SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
3469 SelectionDAG &DAG) const {
3470 MachineFunction &MF = DAG.getMachineFunction();
3471 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003472 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003473 SystemZ::R15D, Op.getValueType());
3474}
3475
3476SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
3477 SelectionDAG &DAG) const {
3478 MachineFunction &MF = DAG.getMachineFunction();
3479 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Matthias Braunf1caa282017-12-15 22:22:58 +00003480 bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003481
3482 SDValue Chain = Op.getOperand(0);
3483 SDValue NewSP = Op.getOperand(1);
3484 SDValue Backchain;
3485 SDLoc DL(Op);
3486
3487 if (StoreBackchain) {
3488 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, MVT::i64);
Justin Lebar9c375812016-07-15 18:27:10 +00003489 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003490 }
3491
3492 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R15D, NewSP);
3493
3494 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003495 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003496
3497 return Chain;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003498}
3499
Richard Sandiford03481332013-08-23 11:36:42 +00003500SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
3501 SelectionDAG &DAG) const {
3502 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3503 if (!IsData)
3504 // Just preserve the chain.
3505 return Op.getOperand(0);
3506
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003507 SDLoc DL(Op);
Richard Sandiford03481332013-08-23 11:36:42 +00003508 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
3509 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
Richard Sandiford21f5d682014-03-06 11:22:58 +00003510 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
Richard Sandiford03481332013-08-23 11:36:42 +00003511 SDValue Ops[] = {
3512 Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003513 DAG.getConstant(Code, DL, MVT::i32),
Richard Sandiford03481332013-08-23 11:36:42 +00003514 Op.getOperand(1)
3515 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003516 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003517 Node->getVTList(), Ops,
Richard Sandiford03481332013-08-23 11:36:42 +00003518 Node->getMemoryVT(), Node->getMemOperand());
3519}
3520
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003521// Return an i32 that contains the value of CC immediately after After,
3522// whose final operand must be MVT::Glue.
3523static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003524 SDLoc DL(After);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003525 SDValue Glue = SDValue(After, After->getNumValues() - 1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003526 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
3527 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
3528 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003529}
3530
3531SDValue
3532SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
3533 SelectionDAG &DAG) const {
3534 unsigned Opcode, CCValid;
3535 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) {
3536 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
3537 SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode);
3538 SDValue CC = getCCResult(DAG, Glued.getNode());
3539 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC);
3540 return SDValue();
3541 }
3542
3543 return SDValue();
3544}
3545
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003546SDValue
3547SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
3548 SelectionDAG &DAG) const {
3549 unsigned Opcode, CCValid;
3550 if (isIntrinsicWithCC(Op, Opcode, CCValid)) {
3551 SDValue Glued = emitIntrinsicWithGlue(DAG, Op, Opcode);
3552 SDValue CC = getCCResult(DAG, Glued.getNode());
3553 if (Op->getNumValues() == 1)
3554 return CC;
3555 assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result");
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00003556 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), Glued,
3557 CC);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003558 }
3559
3560 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3561 switch (Id) {
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00003562 case Intrinsic::thread_pointer:
3563 return lowerThreadPointer(SDLoc(Op), DAG);
3564
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003565 case Intrinsic::s390_vpdi:
3566 return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(),
3567 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3568
3569 case Intrinsic::s390_vperm:
3570 return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(),
3571 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3572
3573 case Intrinsic::s390_vuphb:
3574 case Intrinsic::s390_vuphh:
3575 case Intrinsic::s390_vuphf:
3576 return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(),
3577 Op.getOperand(1));
3578
3579 case Intrinsic::s390_vuplhb:
3580 case Intrinsic::s390_vuplhh:
3581 case Intrinsic::s390_vuplhf:
3582 return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(),
3583 Op.getOperand(1));
3584
3585 case Intrinsic::s390_vuplb:
3586 case Intrinsic::s390_vuplhw:
3587 case Intrinsic::s390_vuplf:
3588 return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(),
3589 Op.getOperand(1));
3590
3591 case Intrinsic::s390_vupllb:
3592 case Intrinsic::s390_vupllh:
3593 case Intrinsic::s390_vupllf:
3594 return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(),
3595 Op.getOperand(1));
3596
3597 case Intrinsic::s390_vsumb:
3598 case Intrinsic::s390_vsumh:
3599 case Intrinsic::s390_vsumgh:
3600 case Intrinsic::s390_vsumgf:
3601 case Intrinsic::s390_vsumqf:
3602 case Intrinsic::s390_vsumqg:
3603 return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(),
3604 Op.getOperand(1), Op.getOperand(2));
3605 }
3606
3607 return SDValue();
3608}
3609
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003610namespace {
3611// Says that SystemZISD operation Opcode can be used to perform the equivalent
3612// of a VPERM with permute vector Bytes. If Opcode takes three operands,
3613// Operand is the constant third operand, otherwise it is the number of
3614// bytes in each element of the result.
3615struct Permute {
3616 unsigned Opcode;
3617 unsigned Operand;
3618 unsigned char Bytes[SystemZ::VectorBytes];
3619};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003620}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003621
3622static const Permute PermuteForms[] = {
3623 // VMRHG
3624 { SystemZISD::MERGE_HIGH, 8,
3625 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } },
3626 // VMRHF
3627 { SystemZISD::MERGE_HIGH, 4,
3628 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
3629 // VMRHH
3630 { SystemZISD::MERGE_HIGH, 2,
3631 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
3632 // VMRHB
3633 { SystemZISD::MERGE_HIGH, 1,
3634 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
3635 // VMRLG
3636 { SystemZISD::MERGE_LOW, 8,
3637 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } },
3638 // VMRLF
3639 { SystemZISD::MERGE_LOW, 4,
3640 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
3641 // VMRLH
3642 { SystemZISD::MERGE_LOW, 2,
3643 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
3644 // VMRLB
3645 { SystemZISD::MERGE_LOW, 1,
3646 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
3647 // VPKG
3648 { SystemZISD::PACK, 4,
3649 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } },
3650 // VPKF
3651 { SystemZISD::PACK, 2,
3652 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
3653 // VPKH
3654 { SystemZISD::PACK, 1,
3655 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
3656 // VPDI V1, V2, 4 (low half of V1, high half of V2)
3657 { SystemZISD::PERMUTE_DWORDS, 4,
3658 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } },
3659 // VPDI V1, V2, 1 (high half of V1, low half of V2)
3660 { SystemZISD::PERMUTE_DWORDS, 1,
3661 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } }
3662};
3663
3664// Called after matching a vector shuffle against a particular pattern.
3665// Both the original shuffle and the pattern have two vector operands.
3666// OpNos[0] is the operand of the original shuffle that should be used for
3667// operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything.
3668// OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and
3669// set OpNo0 and OpNo1 to the shuffle operands that should actually be used
3670// for operands 0 and 1 of the pattern.
3671static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) {
3672 if (OpNos[0] < 0) {
3673 if (OpNos[1] < 0)
3674 return false;
3675 OpNo0 = OpNo1 = OpNos[1];
3676 } else if (OpNos[1] < 0) {
3677 OpNo0 = OpNo1 = OpNos[0];
3678 } else {
3679 OpNo0 = OpNos[0];
3680 OpNo1 = OpNos[1];
3681 }
3682 return true;
3683}
3684
3685// Bytes is a VPERM-like permute vector, except that -1 is used for
3686// undefined bytes. Return true if the VPERM can be implemented using P.
3687// When returning true set OpNo0 to the VPERM operand that should be
3688// used for operand 0 of P and likewise OpNo1 for operand 1 of P.
3689//
3690// For example, if swapping the VPERM operands allows P to match, OpNo0
3691// will be 1 and OpNo1 will be 0. If instead Bytes only refers to one
3692// operand, but rewriting it to use two duplicated operands allows it to
3693// match P, then OpNo0 and OpNo1 will be the same.
3694static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P,
3695 unsigned &OpNo0, unsigned &OpNo1) {
3696 int OpNos[] = { -1, -1 };
3697 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
3698 int Elt = Bytes[I];
3699 if (Elt >= 0) {
3700 // Make sure that the two permute vectors use the same suboperand
3701 // byte number. Only the operand numbers (the high bits) are
3702 // allowed to differ.
3703 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1))
3704 return false;
3705 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes;
3706 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes;
3707 // Make sure that the operand mappings are consistent with previous
3708 // elements.
3709 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3710 return false;
3711 OpNos[ModelOpNo] = RealOpNo;
3712 }
3713 }
3714 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3715}
3716
3717// As above, but search for a matching permute.
3718static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes,
3719 unsigned &OpNo0, unsigned &OpNo1) {
3720 for (auto &P : PermuteForms)
3721 if (matchPermute(Bytes, P, OpNo0, OpNo1))
3722 return &P;
3723 return nullptr;
3724}
3725
3726// Bytes is a VPERM-like permute vector, except that -1 is used for
3727// undefined bytes. This permute is an operand of an outer permute.
3728// See whether redistributing the -1 bytes gives a shuffle that can be
3729// implemented using P. If so, set Transform to a VPERM-like permute vector
3730// that, when applied to the result of P, gives the original permute in Bytes.
3731static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3732 const Permute &P,
3733 SmallVectorImpl<int> &Transform) {
3734 unsigned To = 0;
3735 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) {
3736 int Elt = Bytes[From];
3737 if (Elt < 0)
3738 // Byte number From of the result is undefined.
3739 Transform[From] = -1;
3740 else {
3741 while (P.Bytes[To] != Elt) {
3742 To += 1;
3743 if (To == SystemZ::VectorBytes)
3744 return false;
3745 }
3746 Transform[From] = To;
3747 }
3748 }
3749 return true;
3750}
3751
3752// As above, but search for a matching permute.
3753static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3754 SmallVectorImpl<int> &Transform) {
3755 for (auto &P : PermuteForms)
3756 if (matchDoublePermute(Bytes, P, Transform))
3757 return &P;
3758 return nullptr;
3759}
3760
3761// Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask,
3762// as if it had type vNi8.
3763static void getVPermMask(ShuffleVectorSDNode *VSN,
3764 SmallVectorImpl<int> &Bytes) {
3765 EVT VT = VSN->getValueType(0);
3766 unsigned NumElements = VT.getVectorNumElements();
3767 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3768 Bytes.resize(NumElements * BytesPerElement, -1);
3769 for (unsigned I = 0; I < NumElements; ++I) {
3770 int Index = VSN->getMaskElt(I);
3771 if (Index >= 0)
3772 for (unsigned J = 0; J < BytesPerElement; ++J)
3773 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
3774 }
3775}
3776
3777// Bytes is a VPERM-like permute vector, except that -1 is used for
3778// undefined bytes. See whether bytes [Start, Start + BytesPerElement) of
3779// the result come from a contiguous sequence of bytes from one input.
3780// Set Base to the selector for the first byte if so.
3781static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start,
3782 unsigned BytesPerElement, int &Base) {
3783 Base = -1;
3784 for (unsigned I = 0; I < BytesPerElement; ++I) {
3785 if (Bytes[Start + I] >= 0) {
3786 unsigned Elem = Bytes[Start + I];
3787 if (Base < 0) {
3788 Base = Elem - I;
3789 // Make sure the bytes would come from one input operand.
3790 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size())
3791 return false;
3792 } else if (unsigned(Base) != Elem - I)
3793 return false;
3794 }
3795 }
3796 return true;
3797}
3798
3799// Bytes is a VPERM-like permute vector, except that -1 is used for
3800// undefined bytes. Return true if it can be performed using VSLDI.
3801// When returning true, set StartIndex to the shift amount and OpNo0
3802// and OpNo1 to the VPERM operands that should be used as the first
3803// and second shift operand respectively.
3804static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes,
3805 unsigned &StartIndex, unsigned &OpNo0,
3806 unsigned &OpNo1) {
3807 int OpNos[] = { -1, -1 };
3808 int Shift = -1;
3809 for (unsigned I = 0; I < 16; ++I) {
3810 int Index = Bytes[I];
3811 if (Index >= 0) {
3812 int ExpectedShift = (Index - I) % SystemZ::VectorBytes;
3813 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes;
3814 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes;
3815 if (Shift < 0)
3816 Shift = ExpectedShift;
3817 else if (Shift != ExpectedShift)
3818 return false;
3819 // Make sure that the operand mappings are consistent with previous
3820 // elements.
3821 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3822 return false;
3823 OpNos[ModelOpNo] = RealOpNo;
3824 }
3825 }
3826 StartIndex = Shift;
3827 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3828}
3829
3830// Create a node that performs P on operands Op0 and Op1, casting the
3831// operands to the appropriate type. The type of the result is determined by P.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003832static SDValue getPermuteNode(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003833 const Permute &P, SDValue Op0, SDValue Op1) {
3834 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input
3835 // elements of a PACK are twice as wide as the outputs.
3836 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 :
3837 P.Opcode == SystemZISD::PACK ? P.Operand * 2 :
3838 P.Operand);
3839 // Cast both operands to the appropriate type.
3840 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8),
3841 SystemZ::VectorBytes / InBytes);
3842 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0);
3843 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1);
3844 SDValue Op;
3845 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) {
3846 SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32);
3847 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2);
3848 } else if (P.Opcode == SystemZISD::PACK) {
3849 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8),
3850 SystemZ::VectorBytes / P.Operand);
3851 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1);
3852 } else {
3853 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1);
3854 }
3855 return Op;
3856}
3857
3858// Bytes is a VPERM-like permute vector, except that -1 is used for
3859// undefined bytes. Implement it on operands Ops[0] and Ops[1] using
3860// VSLDI or VPERM.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003861static SDValue getGeneralPermuteNode(SelectionDAG &DAG, const SDLoc &DL,
3862 SDValue *Ops,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003863 const SmallVectorImpl<int> &Bytes) {
3864 for (unsigned I = 0; I < 2; ++I)
3865 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]);
3866
3867 // First see whether VSLDI can be used.
3868 unsigned StartIndex, OpNo0, OpNo1;
3869 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1))
3870 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0],
3871 Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32));
3872
3873 // Fall back on VPERM. Construct an SDNode for the permute vector.
3874 SDValue IndexNodes[SystemZ::VectorBytes];
3875 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3876 if (Bytes[I] >= 0)
3877 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32);
3878 else
3879 IndexNodes[I] = DAG.getUNDEF(MVT::i32);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00003880 SDValue Op2 = DAG.getBuildVector(MVT::v16i8, DL, IndexNodes);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003881 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2);
3882}
3883
3884namespace {
3885// Describes a general N-operand vector shuffle.
3886struct GeneralShuffle {
3887 GeneralShuffle(EVT vt) : VT(vt) {}
3888 void addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003889 bool add(SDValue, unsigned);
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003890 SDValue getNode(SelectionDAG &, const SDLoc &);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003891
3892 // The operands of the shuffle.
3893 SmallVector<SDValue, SystemZ::VectorBytes> Ops;
3894
3895 // Index I is -1 if byte I of the result is undefined. Otherwise the
3896 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand
3897 // Bytes[I] / SystemZ::VectorBytes.
3898 SmallVector<int, SystemZ::VectorBytes> Bytes;
3899
3900 // The type of the shuffle result.
3901 EVT VT;
3902};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003903}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003904
3905// Add an extra undefined element to the shuffle.
3906void GeneralShuffle::addUndef() {
3907 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3908 for (unsigned I = 0; I < BytesPerElement; ++I)
3909 Bytes.push_back(-1);
3910}
3911
3912// Add an extra element to the shuffle, taking it from element Elem of Op.
3913// A null Op indicates a vector input whose value will be calculated later;
3914// there is at most one such input per shuffle and it always has the same
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003915// type as the result. Aborts and returns false if the source vector elements
3916// of an EXTRACT_VECTOR_ELT are smaller than the destination elements. Per
3917// LLVM they become implicitly extended, but this is rare and not optimized.
3918bool GeneralShuffle::add(SDValue Op, unsigned Elem) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003919 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3920
3921 // The source vector can have wider elements than the result,
3922 // either through an explicit TRUNCATE or because of type legalization.
3923 // We want the least significant part.
3924 EVT FromVT = Op.getNode() ? Op.getValueType() : VT;
3925 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003926
3927 // Return false if the source elements are smaller than their destination
3928 // elements.
3929 if (FromBytesPerElement < BytesPerElement)
3930 return false;
3931
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003932 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes +
3933 (FromBytesPerElement - BytesPerElement));
3934
3935 // Look through things like shuffles and bitcasts.
3936 while (Op.getNode()) {
3937 if (Op.getOpcode() == ISD::BITCAST)
3938 Op = Op.getOperand(0);
3939 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) {
3940 // See whether the bytes we need come from a contiguous part of one
3941 // operand.
3942 SmallVector<int, SystemZ::VectorBytes> OpBytes;
3943 getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes);
3944 int NewByte;
3945 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte))
3946 break;
3947 if (NewByte < 0) {
3948 addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003949 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003950 }
3951 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes);
3952 Byte = unsigned(NewByte) % SystemZ::VectorBytes;
Sanjay Patel57195842016-03-14 17:28:46 +00003953 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003954 addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003955 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003956 } else
3957 break;
3958 }
3959
3960 // Make sure that the source of the extraction is in Ops.
3961 unsigned OpNo = 0;
3962 for (; OpNo < Ops.size(); ++OpNo)
3963 if (Ops[OpNo] == Op)
3964 break;
3965 if (OpNo == Ops.size())
3966 Ops.push_back(Op);
3967
3968 // Add the element to Bytes.
3969 unsigned Base = OpNo * SystemZ::VectorBytes + Byte;
3970 for (unsigned I = 0; I < BytesPerElement; ++I)
3971 Bytes.push_back(Base + I);
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003972
3973 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003974}
3975
3976// Return SDNodes for the completed shuffle.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003977SDValue GeneralShuffle::getNode(SelectionDAG &DAG, const SDLoc &DL) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003978 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector");
3979
3980 if (Ops.size() == 0)
3981 return DAG.getUNDEF(VT);
3982
3983 // Make sure that there are at least two shuffle operands.
3984 if (Ops.size() == 1)
3985 Ops.push_back(DAG.getUNDEF(MVT::v16i8));
3986
3987 // Create a tree of shuffles, deferring root node until after the loop.
3988 // Try to redistribute the undefined elements of non-root nodes so that
3989 // the non-root shuffles match something like a pack or merge, then adjust
3990 // the parent node's permute vector to compensate for the new order.
3991 // Among other things, this copes with vectors like <2 x i16> that were
3992 // padded with undefined elements during type legalization.
3993 //
3994 // In the best case this redistribution will lead to the whole tree
3995 // using packs and merges. It should rarely be a loss in other cases.
3996 unsigned Stride = 1;
3997 for (; Stride * 2 < Ops.size(); Stride *= 2) {
3998 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) {
3999 SDValue SubOps[] = { Ops[I], Ops[I + Stride] };
4000
4001 // Create a mask for just these two operands.
4002 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes);
4003 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
4004 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes;
4005 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes;
4006 if (OpNo == I)
4007 NewBytes[J] = Byte;
4008 else if (OpNo == I + Stride)
4009 NewBytes[J] = SystemZ::VectorBytes + Byte;
4010 else
4011 NewBytes[J] = -1;
4012 }
4013 // See if it would be better to reorganize NewMask to avoid using VPERM.
4014 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes);
4015 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) {
4016 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]);
4017 // Applying NewBytesMap to Ops[I] gets back to NewBytes.
4018 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
4019 if (NewBytes[J] >= 0) {
4020 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes &&
4021 "Invalid double permute");
4022 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J];
4023 } else
4024 assert(NewBytesMap[J] < 0 && "Invalid double permute");
4025 }
4026 } else {
4027 // Just use NewBytes on the operands.
4028 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes);
4029 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J)
4030 if (NewBytes[J] >= 0)
4031 Bytes[J] = I * SystemZ::VectorBytes + J;
4032 }
4033 }
4034 }
4035
4036 // Now we just have 2 inputs. Put the second operand in Ops[1].
4037 if (Stride > 1) {
4038 Ops[1] = Ops[Stride];
4039 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
4040 if (Bytes[I] >= int(SystemZ::VectorBytes))
4041 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes;
4042 }
4043
4044 // Look for an instruction that can do the permute without resorting
4045 // to VPERM.
4046 unsigned OpNo0, OpNo1;
4047 SDValue Op;
4048 if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1))
4049 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]);
4050 else
4051 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes);
4052 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4053}
4054
Ulrich Weigandcd808232015-05-05 19:26:48 +00004055// Return true if the given BUILD_VECTOR is a scalar-to-vector conversion.
4056static bool isScalarToVector(SDValue Op) {
4057 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00004058 if (!Op.getOperand(I).isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004059 return false;
4060 return true;
4061}
4062
4063// Return a vector of type VT that contains Value in the first element.
4064// The other elements don't matter.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004065static SDValue buildScalarToVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandcd808232015-05-05 19:26:48 +00004066 SDValue Value) {
4067 // If we have a constant, replicate it to all elements and let the
4068 // BUILD_VECTOR lowering take care of it.
4069 if (Value.getOpcode() == ISD::Constant ||
4070 Value.getOpcode() == ISD::ConstantFP) {
4071 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004072 return DAG.getBuildVector(VT, DL, Ops);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004073 }
Sanjay Patel57195842016-03-14 17:28:46 +00004074 if (Value.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004075 return DAG.getUNDEF(VT);
4076 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
4077}
4078
4079// Return a vector of type VT in which Op0 is in element 0 and Op1 is in
4080// element 1. Used for cases in which replication is cheap.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004081static SDValue buildMergeScalars(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandcd808232015-05-05 19:26:48 +00004082 SDValue Op0, SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00004083 if (Op0.isUndef()) {
4084 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004085 return DAG.getUNDEF(VT);
4086 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1);
4087 }
Sanjay Patel57195842016-03-14 17:28:46 +00004088 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004089 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0);
4090 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT,
4091 buildScalarToVector(DAG, DL, VT, Op0),
4092 buildScalarToVector(DAG, DL, VT, Op1));
4093}
4094
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004095// Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64
4096// vector for them.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004097static SDValue joinDwords(SelectionDAG &DAG, const SDLoc &DL, SDValue Op0,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004098 SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00004099 if (Op0.isUndef() && Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004100 return DAG.getUNDEF(MVT::v2i64);
4101 // If one of the two inputs is undefined then replicate the other one,
4102 // in order to avoid using another register unnecessarily.
Sanjay Patel57195842016-03-14 17:28:46 +00004103 if (Op0.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004104 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
Sanjay Patel57195842016-03-14 17:28:46 +00004105 else if (Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004106 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
4107 else {
4108 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
4109 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
4110 }
4111 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1);
4112}
4113
4114// Try to represent constant BUILD_VECTOR node BVN using a
4115// SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask
4116// on success.
4117static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) {
4118 EVT ElemVT = BVN->getValueType(0).getVectorElementType();
4119 unsigned BytesPerElement = ElemVT.getStoreSize();
4120 for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) {
4121 SDValue Op = BVN->getOperand(I);
Sanjay Patel75068522016-03-14 18:09:43 +00004122 if (!Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004123 uint64_t Value;
4124 if (Op.getOpcode() == ISD::Constant)
4125 Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue();
4126 else if (Op.getOpcode() == ISD::ConstantFP)
4127 Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt()
4128 .getZExtValue());
4129 else
4130 return false;
4131 for (unsigned J = 0; J < BytesPerElement; ++J) {
4132 uint64_t Byte = (Value >> (J * 8)) & 0xff;
4133 if (Byte == 0xff)
Aaron Ballman2a3aa1f242015-05-11 12:45:53 +00004134 Mask |= 1ULL << ((E - I - 1) * BytesPerElement + J);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004135 else if (Byte != 0)
4136 return false;
4137 }
4138 }
4139 }
4140 return true;
4141}
4142
4143// Try to load a vector constant in which BitsPerElement-bit value Value
4144// is replicated to fill the vector. VT is the type of the resulting
4145// constant, which may have elements of a different size from BitsPerElement.
4146// Return the SDValue of the constant on success, otherwise return
4147// an empty value.
4148static SDValue tryBuildVectorReplicate(SelectionDAG &DAG,
4149 const SystemZInstrInfo *TII,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004150 const SDLoc &DL, EVT VT, uint64_t Value,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004151 unsigned BitsPerElement) {
4152 // Signed 16-bit values can be replicated using VREPI.
4153 int64_t SignedValue = SignExtend64(Value, BitsPerElement);
4154 if (isInt<16>(SignedValue)) {
4155 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
4156 SystemZ::VectorBits / BitsPerElement);
4157 SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT,
4158 DAG.getConstant(SignedValue, DL, MVT::i32));
4159 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4160 }
4161 // See whether rotating the constant left some N places gives a value that
4162 // is one less than a power of 2 (i.e. all zeros followed by all ones).
4163 // If so we can use VGM.
4164 unsigned Start, End;
4165 if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) {
4166 // isRxSBGMask returns the bit numbers for a full 64-bit value,
4167 // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to
4168 // bit numbers for an BitsPerElement value, so that 0 denotes
4169 // 1 << (BitsPerElement-1).
4170 Start -= 64 - BitsPerElement;
4171 End -= 64 - BitsPerElement;
4172 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
4173 SystemZ::VectorBits / BitsPerElement);
4174 SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT,
4175 DAG.getConstant(Start, DL, MVT::i32),
4176 DAG.getConstant(End, DL, MVT::i32));
4177 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4178 }
4179 return SDValue();
4180}
4181
4182// If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually
4183// better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for
4184// the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR
4185// would benefit from this representation and return it if so.
4186static SDValue tryBuildVectorShuffle(SelectionDAG &DAG,
4187 BuildVectorSDNode *BVN) {
4188 EVT VT = BVN->getValueType(0);
4189 unsigned NumElements = VT.getVectorNumElements();
4190
4191 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation
4192 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still
4193 // need a BUILD_VECTOR, add an additional placeholder operand for that
4194 // BUILD_VECTOR and store its operands in ResidueOps.
4195 GeneralShuffle GS(VT);
4196 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps;
4197 bool FoundOne = false;
4198 for (unsigned I = 0; I < NumElements; ++I) {
4199 SDValue Op = BVN->getOperand(I);
4200 if (Op.getOpcode() == ISD::TRUNCATE)
4201 Op = Op.getOperand(0);
4202 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4203 Op.getOperand(1).getOpcode() == ISD::Constant) {
4204 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004205 if (!GS.add(Op.getOperand(0), Elem))
4206 return SDValue();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004207 FoundOne = true;
Sanjay Patel57195842016-03-14 17:28:46 +00004208 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004209 GS.addUndef();
4210 } else {
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004211 if (!GS.add(SDValue(), ResidueOps.size()))
4212 return SDValue();
Ulrich Weigande861e642015-09-15 14:27:46 +00004213 ResidueOps.push_back(BVN->getOperand(I));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004214 }
4215 }
4216
4217 // Nothing to do if there are no EXTRACT_VECTOR_ELTs.
4218 if (!FoundOne)
4219 return SDValue();
4220
4221 // Create the BUILD_VECTOR for the remaining elements, if any.
4222 if (!ResidueOps.empty()) {
4223 while (ResidueOps.size() < NumElements)
Ulrich Weigandf4d14f72015-10-08 17:46:59 +00004224 ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType()));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004225 for (auto &Op : GS.Ops) {
4226 if (!Op.getNode()) {
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004227 Op = DAG.getBuildVector(VT, SDLoc(BVN), ResidueOps);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004228 break;
4229 }
4230 }
4231 }
4232 return GS.getNode(DAG, SDLoc(BVN));
4233}
4234
4235// Combine GPR scalar values Elems into a vector of type VT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004236static SDValue buildVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004237 SmallVectorImpl<SDValue> &Elems) {
4238 // See whether there is a single replicated value.
4239 SDValue Single;
4240 unsigned int NumElements = Elems.size();
4241 unsigned int Count = 0;
4242 for (auto Elem : Elems) {
Sanjay Patel75068522016-03-14 18:09:43 +00004243 if (!Elem.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004244 if (!Single.getNode())
4245 Single = Elem;
4246 else if (Elem != Single) {
4247 Single = SDValue();
4248 break;
4249 }
4250 Count += 1;
4251 }
4252 }
4253 // There are three cases here:
4254 //
4255 // - if the only defined element is a loaded one, the best sequence
4256 // is a replicating load.
4257 //
4258 // - otherwise, if the only defined element is an i64 value, we will
4259 // end up with the same VLVGP sequence regardless of whether we short-cut
4260 // for replication or fall through to the later code.
4261 //
4262 // - otherwise, if the only defined element is an i32 or smaller value,
4263 // we would need 2 instructions to replicate it: VLVGP followed by VREPx.
4264 // This is only a win if the single defined element is used more than once.
4265 // In other cases we're better off using a single VLVGx.
4266 if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD))
4267 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single);
4268
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004269 // If all elements are loads, use VLREP/VLEs (below).
4270 bool AllLoads = true;
4271 for (auto Elem : Elems)
4272 if (Elem.getOpcode() != ISD::LOAD || cast<LoadSDNode>(Elem)->isIndexed()) {
4273 AllLoads = false;
4274 break;
4275 }
4276
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004277 // The best way of building a v2i64 from two i64s is to use VLVGP.
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004278 if (VT == MVT::v2i64 && !AllLoads)
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004279 return joinDwords(DAG, DL, Elems[0], Elems[1]);
4280
Ulrich Weigandcd808232015-05-05 19:26:48 +00004281 // Use a 64-bit merge high to combine two doubles.
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004282 if (VT == MVT::v2f64 && !AllLoads)
Ulrich Weigandcd808232015-05-05 19:26:48 +00004283 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4284
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004285 // Build v4f32 values directly from the FPRs:
4286 //
4287 // <Axxx> <Bxxx> <Cxxxx> <Dxxx>
4288 // V V VMRHF
4289 // <ABxx> <CDxx>
4290 // V VMRHG
4291 // <ABCD>
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004292 if (VT == MVT::v4f32 && !AllLoads) {
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004293 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4294 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]);
4295 // Avoid unnecessary undefs by reusing the other operand.
Sanjay Patel57195842016-03-14 17:28:46 +00004296 if (Op01.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004297 Op01 = Op23;
Sanjay Patel57195842016-03-14 17:28:46 +00004298 else if (Op23.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004299 Op23 = Op01;
4300 // Merging identical replications is a no-op.
4301 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23)
4302 return Op01;
4303 Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01);
4304 Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23);
4305 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH,
4306 DL, MVT::v2i64, Op01, Op23);
4307 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4308 }
4309
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004310 // Collect the constant terms.
4311 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue());
4312 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false);
4313
4314 unsigned NumConstants = 0;
4315 for (unsigned I = 0; I < NumElements; ++I) {
4316 SDValue Elem = Elems[I];
4317 if (Elem.getOpcode() == ISD::Constant ||
4318 Elem.getOpcode() == ISD::ConstantFP) {
4319 NumConstants += 1;
4320 Constants[I] = Elem;
4321 Done[I] = true;
4322 }
4323 }
4324 // If there was at least one constant, fill in the other elements of
4325 // Constants with undefs to get a full vector constant and use that
4326 // as the starting point.
4327 SDValue Result;
4328 if (NumConstants > 0) {
4329 for (unsigned I = 0; I < NumElements; ++I)
4330 if (!Constants[I].getNode())
4331 Constants[I] = DAG.getUNDEF(Elems[I].getValueType());
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004332 Result = DAG.getBuildVector(VT, DL, Constants);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004333 } else {
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004334 // Otherwise try to use VLREP or VLVGP to start the sequence in order to
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004335 // avoid a false dependency on any previous contents of the vector
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004336 // register.
4337
4338 // Use a VLREP if at least one element is a load.
4339 unsigned LoadElIdx = UINT_MAX;
4340 for (unsigned I = 0; I < NumElements; ++I)
4341 if (Elems[I].getOpcode() == ISD::LOAD &&
4342 cast<LoadSDNode>(Elems[I])->isUnindexed()) {
4343 LoadElIdx = I;
4344 break;
4345 }
4346 if (LoadElIdx != UINT_MAX) {
4347 Result = DAG.getNode(SystemZISD::REPLICATE, DL, VT, Elems[LoadElIdx]);
4348 Done[LoadElIdx] = true;
4349 } else {
4350 // Try to use VLVGP.
4351 unsigned I1 = NumElements / 2 - 1;
4352 unsigned I2 = NumElements - 1;
4353 bool Def1 = !Elems[I1].isUndef();
4354 bool Def2 = !Elems[I2].isUndef();
4355 if (Def1 || Def2) {
4356 SDValue Elem1 = Elems[Def1 ? I1 : I2];
4357 SDValue Elem2 = Elems[Def2 ? I2 : I1];
4358 Result = DAG.getNode(ISD::BITCAST, DL, VT,
4359 joinDwords(DAG, DL, Elem1, Elem2));
4360 Done[I1] = true;
4361 Done[I2] = true;
4362 } else
4363 Result = DAG.getUNDEF(VT);
4364 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004365 }
4366
4367 // Use VLVGx to insert the other elements.
4368 for (unsigned I = 0; I < NumElements; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00004369 if (!Done[I] && !Elems[I].isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004370 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I],
4371 DAG.getConstant(I, DL, MVT::i32));
4372 return Result;
4373}
4374
4375SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op,
4376 SelectionDAG &DAG) const {
4377 const SystemZInstrInfo *TII =
4378 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
4379 auto *BVN = cast<BuildVectorSDNode>(Op.getNode());
4380 SDLoc DL(Op);
4381 EVT VT = Op.getValueType();
4382
4383 if (BVN->isConstant()) {
4384 // Try using VECTOR GENERATE BYTE MASK. This is the architecturally-
4385 // preferred way of creating all-zero and all-one vectors so give it
4386 // priority over other methods below.
4387 uint64_t Mask = 0;
4388 if (tryBuildVectorByteMask(BVN, Mask)) {
4389 SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
4390 DAG.getConstant(Mask, DL, MVT::i32));
4391 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4392 }
4393
4394 // Try using some form of replication.
4395 APInt SplatBits, SplatUndef;
4396 unsigned SplatBitSize;
4397 bool HasAnyUndefs;
4398 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4399 8, true) &&
4400 SplatBitSize <= 64) {
4401 // First try assuming that any undefined bits above the highest set bit
4402 // and below the lowest set bit are 1s. This increases the likelihood of
4403 // being able to use a sign-extended element value in VECTOR REPLICATE
4404 // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK.
4405 uint64_t SplatBitsZ = SplatBits.getZExtValue();
4406 uint64_t SplatUndefZ = SplatUndef.getZExtValue();
4407 uint64_t Lower = (SplatUndefZ
4408 & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1));
4409 uint64_t Upper = (SplatUndefZ
4410 & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1));
4411 uint64_t Value = SplatBitsZ | Upper | Lower;
4412 SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value,
4413 SplatBitSize);
4414 if (Op.getNode())
4415 return Op;
4416
4417 // Now try assuming that any undefined bits between the first and
4418 // last defined set bits are set. This increases the chances of
4419 // using a non-wraparound mask.
4420 uint64_t Middle = SplatUndefZ & ~Upper & ~Lower;
4421 Value = SplatBitsZ | Middle;
4422 Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize);
4423 if (Op.getNode())
4424 return Op;
4425 }
4426
4427 // Fall back to loading it from memory.
4428 return SDValue();
4429 }
4430
4431 // See if we should use shuffles to construct the vector from other vectors.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004432 if (SDValue Res = tryBuildVectorShuffle(DAG, BVN))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004433 return Res;
4434
Ulrich Weigandcd808232015-05-05 19:26:48 +00004435 // Detect SCALAR_TO_VECTOR conversions.
4436 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op))
4437 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0));
4438
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004439 // Otherwise use buildVector to build the vector up from GPRs.
4440 unsigned NumElements = Op.getNumOperands();
4441 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements);
4442 for (unsigned I = 0; I < NumElements; ++I)
4443 Ops[I] = Op.getOperand(I);
4444 return buildVector(DAG, DL, VT, Ops);
4445}
4446
4447SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
4448 SelectionDAG &DAG) const {
4449 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode());
4450 SDLoc DL(Op);
4451 EVT VT = Op.getValueType();
4452 unsigned NumElements = VT.getVectorNumElements();
4453
4454 if (VSN->isSplat()) {
4455 SDValue Op0 = Op.getOperand(0);
4456 unsigned Index = VSN->getSplatIndex();
4457 assert(Index < VT.getVectorNumElements() &&
4458 "Splat index should be defined and in first operand");
4459 // See whether the value we're splatting is directly available as a scalar.
4460 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4461 Op0.getOpcode() == ISD::BUILD_VECTOR)
4462 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index));
4463 // Otherwise keep it as a vector-to-vector operation.
4464 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0),
4465 DAG.getConstant(Index, DL, MVT::i32));
4466 }
4467
4468 GeneralShuffle GS(VT);
4469 for (unsigned I = 0; I < NumElements; ++I) {
4470 int Elt = VSN->getMaskElt(I);
4471 if (Elt < 0)
4472 GS.addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004473 else if (!GS.add(Op.getOperand(unsigned(Elt) / NumElements),
4474 unsigned(Elt) % NumElements))
4475 return SDValue();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004476 }
4477 return GS.getNode(DAG, SDLoc(VSN));
4478}
4479
4480SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
4481 SelectionDAG &DAG) const {
4482 SDLoc DL(Op);
4483 // Just insert the scalar into element 0 of an undefined vector.
4484 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL,
4485 Op.getValueType(), DAG.getUNDEF(Op.getValueType()),
4486 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32));
4487}
4488
Ulrich Weigandcd808232015-05-05 19:26:48 +00004489SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
4490 SelectionDAG &DAG) const {
4491 // Handle insertions of floating-point values.
4492 SDLoc DL(Op);
4493 SDValue Op0 = Op.getOperand(0);
4494 SDValue Op1 = Op.getOperand(1);
4495 SDValue Op2 = Op.getOperand(2);
4496 EVT VT = Op.getValueType();
4497
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004498 // Insertions into constant indices of a v2f64 can be done using VPDI.
4499 // However, if the inserted value is a bitcast or a constant then it's
4500 // better to use GPRs, as below.
4501 if (VT == MVT::v2f64 &&
4502 Op1.getOpcode() != ISD::BITCAST &&
Ulrich Weigandcd808232015-05-05 19:26:48 +00004503 Op1.getOpcode() != ISD::ConstantFP &&
4504 Op2.getOpcode() == ISD::Constant) {
4505 uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue();
4506 unsigned Mask = VT.getVectorNumElements() - 1;
4507 if (Index <= Mask)
4508 return Op;
4509 }
4510
4511 // Otherwise bitcast to the equivalent integer form and insert via a GPR.
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004512 MVT IntVT = MVT::getIntegerVT(VT.getScalarSizeInBits());
Ulrich Weigandcd808232015-05-05 19:26:48 +00004513 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements());
4514 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT,
4515 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0),
4516 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2);
4517 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4518}
4519
4520SDValue
4521SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
4522 SelectionDAG &DAG) const {
4523 // Handle extractions of floating-point values.
4524 SDLoc DL(Op);
4525 SDValue Op0 = Op.getOperand(0);
4526 SDValue Op1 = Op.getOperand(1);
4527 EVT VT = Op.getValueType();
4528 EVT VecVT = Op0.getValueType();
4529
4530 // Extractions of constant indices can be done directly.
4531 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) {
4532 uint64_t Index = CIndexN->getZExtValue();
4533 unsigned Mask = VecVT.getVectorNumElements() - 1;
4534 if (Index <= Mask)
4535 return Op;
4536 }
4537
4538 // Otherwise bitcast to the equivalent integer form and extract via a GPR.
4539 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
4540 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements());
4541 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT,
4542 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1);
4543 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4544}
4545
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004546SDValue
4547SystemZTargetLowering::lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004548 unsigned UnpackHigh) const {
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004549 SDValue PackedOp = Op.getOperand(0);
4550 EVT OutVT = Op.getValueType();
4551 EVT InVT = PackedOp.getValueType();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004552 unsigned ToBits = OutVT.getScalarSizeInBits();
4553 unsigned FromBits = InVT.getScalarSizeInBits();
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004554 do {
4555 FromBits *= 2;
4556 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits),
4557 SystemZ::VectorBits / FromBits);
4558 PackedOp = DAG.getNode(UnpackHigh, SDLoc(PackedOp), OutVT, PackedOp);
4559 } while (FromBits != ToBits);
4560 return PackedOp;
4561}
4562
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004563SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG,
4564 unsigned ByScalar) const {
4565 // Look for cases where a vector shift can use the *_BY_SCALAR form.
4566 SDValue Op0 = Op.getOperand(0);
4567 SDValue Op1 = Op.getOperand(1);
4568 SDLoc DL(Op);
4569 EVT VT = Op.getValueType();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004570 unsigned ElemBitSize = VT.getScalarSizeInBits();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004571
4572 // See whether the shift vector is a splat represented as BUILD_VECTOR.
4573 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) {
4574 APInt SplatBits, SplatUndef;
4575 unsigned SplatBitSize;
4576 bool HasAnyUndefs;
4577 // Check for constant splats. Use ElemBitSize as the minimum element
4578 // width and reject splats that need wider elements.
4579 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4580 ElemBitSize, true) &&
4581 SplatBitSize == ElemBitSize) {
4582 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff,
4583 DL, MVT::i32);
4584 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4585 }
4586 // Check for variable splats.
4587 BitVector UndefElements;
4588 SDValue Splat = BVN->getSplatValue(&UndefElements);
4589 if (Splat) {
4590 // Since i32 is the smallest legal type, we either need a no-op
4591 // or a truncation.
4592 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat);
4593 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4594 }
4595 }
4596
4597 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR,
4598 // and the shift amount is directly available in a GPR.
4599 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) {
4600 if (VSN->isSplat()) {
4601 SDValue VSNOp0 = VSN->getOperand(0);
4602 unsigned Index = VSN->getSplatIndex();
4603 assert(Index < VT.getVectorNumElements() &&
4604 "Splat index should be defined and in first operand");
4605 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4606 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) {
4607 // Since i32 is the smallest legal type, we either need a no-op
4608 // or a truncation.
4609 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32,
4610 VSNOp0.getOperand(Index));
4611 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4612 }
4613 }
4614 }
4615
4616 // Otherwise just treat the current form as legal.
4617 return Op;
4618}
4619
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004620SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
4621 SelectionDAG &DAG) const {
4622 switch (Op.getOpcode()) {
Ulrich Weigandf557d082016-04-04 12:44:55 +00004623 case ISD::FRAMEADDR:
4624 return lowerFRAMEADDR(Op, DAG);
4625 case ISD::RETURNADDR:
4626 return lowerRETURNADDR(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004627 case ISD::BR_CC:
4628 return lowerBR_CC(Op, DAG);
4629 case ISD::SELECT_CC:
4630 return lowerSELECT_CC(Op, DAG);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00004631 case ISD::SETCC:
4632 return lowerSETCC(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004633 case ISD::GlobalAddress:
4634 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
4635 case ISD::GlobalTLSAddress:
4636 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
4637 case ISD::BlockAddress:
4638 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
4639 case ISD::JumpTable:
4640 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
4641 case ISD::ConstantPool:
4642 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
4643 case ISD::BITCAST:
4644 return lowerBITCAST(Op, DAG);
4645 case ISD::VASTART:
4646 return lowerVASTART(Op, DAG);
4647 case ISD::VACOPY:
4648 return lowerVACOPY(Op, DAG);
4649 case ISD::DYNAMIC_STACKALLOC:
4650 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +00004651 case ISD::GET_DYNAMIC_AREA_OFFSET:
4652 return lowerGET_DYNAMIC_AREA_OFFSET(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00004653 case ISD::SMUL_LOHI:
4654 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004655 case ISD::UMUL_LOHI:
4656 return lowerUMUL_LOHI(Op, DAG);
4657 case ISD::SDIVREM:
4658 return lowerSDIVREM(Op, DAG);
4659 case ISD::UDIVREM:
4660 return lowerUDIVREM(Op, DAG);
4661 case ISD::OR:
4662 return lowerOR(Op, DAG);
Ulrich Weigandb4012182015-03-31 12:56:33 +00004663 case ISD::CTPOP:
4664 return lowerCTPOP(Op, DAG);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004665 case ISD::ATOMIC_FENCE:
4666 return lowerATOMIC_FENCE(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004667 case ISD::ATOMIC_SWAP:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004668 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
4669 case ISD::ATOMIC_STORE:
4670 return lowerATOMIC_STORE(Op, DAG);
4671 case ISD::ATOMIC_LOAD:
4672 return lowerATOMIC_LOAD(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004673 case ISD::ATOMIC_LOAD_ADD:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004674 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004675 case ISD::ATOMIC_LOAD_SUB:
Richard Sandiford41350a52013-12-24 15:18:04 +00004676 return lowerATOMIC_LOAD_SUB(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004677 case ISD::ATOMIC_LOAD_AND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004678 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004679 case ISD::ATOMIC_LOAD_OR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004680 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004681 case ISD::ATOMIC_LOAD_XOR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004682 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004683 case ISD::ATOMIC_LOAD_NAND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004684 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004685 case ISD::ATOMIC_LOAD_MIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004686 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004687 case ISD::ATOMIC_LOAD_MAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004688 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004689 case ISD::ATOMIC_LOAD_UMIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004690 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004691 case ISD::ATOMIC_LOAD_UMAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004692 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
Ulrich Weigand0f1de042017-09-28 16:22:54 +00004693 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004694 return lowerATOMIC_CMP_SWAP(Op, DAG);
4695 case ISD::STACKSAVE:
4696 return lowerSTACKSAVE(Op, DAG);
4697 case ISD::STACKRESTORE:
4698 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00004699 case ISD::PREFETCH:
4700 return lowerPREFETCH(Op, DAG);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004701 case ISD::INTRINSIC_W_CHAIN:
4702 return lowerINTRINSIC_W_CHAIN(Op, DAG);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004703 case ISD::INTRINSIC_WO_CHAIN:
4704 return lowerINTRINSIC_WO_CHAIN(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004705 case ISD::BUILD_VECTOR:
4706 return lowerBUILD_VECTOR(Op, DAG);
4707 case ISD::VECTOR_SHUFFLE:
4708 return lowerVECTOR_SHUFFLE(Op, DAG);
4709 case ISD::SCALAR_TO_VECTOR:
4710 return lowerSCALAR_TO_VECTOR(Op, DAG);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004711 case ISD::INSERT_VECTOR_ELT:
4712 return lowerINSERT_VECTOR_ELT(Op, DAG);
4713 case ISD::EXTRACT_VECTOR_ELT:
4714 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004715 case ISD::SIGN_EXTEND_VECTOR_INREG:
4716 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACK_HIGH);
4717 case ISD::ZERO_EXTEND_VECTOR_INREG:
4718 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACKL_HIGH);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004719 case ISD::SHL:
4720 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR);
4721 case ISD::SRL:
4722 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR);
4723 case ISD::SRA:
4724 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004725 default:
4726 llvm_unreachable("Unexpected node to lower");
4727 }
4728}
4729
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004730// Lower operations with invalid operand or result types (currently used
4731// only for 128-bit integer types).
4732
4733static SDValue lowerI128ToGR128(SelectionDAG &DAG, SDValue In) {
4734 SDLoc DL(In);
4735 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, In,
4736 DAG.getIntPtrConstant(0, DL));
4737 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, In,
4738 DAG.getIntPtrConstant(1, DL));
4739 SDNode *Pair = DAG.getMachineNode(SystemZ::PAIR128, DL,
4740 MVT::Untyped, Hi, Lo);
4741 return SDValue(Pair, 0);
4742}
4743
4744static SDValue lowerGR128ToI128(SelectionDAG &DAG, SDValue In) {
4745 SDLoc DL(In);
4746 SDValue Hi = DAG.getTargetExtractSubreg(SystemZ::subreg_h64,
4747 DL, MVT::i64, In);
4748 SDValue Lo = DAG.getTargetExtractSubreg(SystemZ::subreg_l64,
4749 DL, MVT::i64, In);
4750 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i128, Lo, Hi);
4751}
4752
4753void
4754SystemZTargetLowering::LowerOperationWrapper(SDNode *N,
4755 SmallVectorImpl<SDValue> &Results,
4756 SelectionDAG &DAG) const {
4757 switch (N->getOpcode()) {
4758 case ISD::ATOMIC_LOAD: {
4759 SDLoc DL(N);
4760 SDVTList Tys = DAG.getVTList(MVT::Untyped, MVT::Other);
4761 SDValue Ops[] = { N->getOperand(0), N->getOperand(1) };
4762 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
4763 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_LOAD_128,
4764 DL, Tys, Ops, MVT::i128, MMO);
4765 Results.push_back(lowerGR128ToI128(DAG, Res));
4766 Results.push_back(Res.getValue(1));
4767 break;
4768 }
4769 case ISD::ATOMIC_STORE: {
4770 SDLoc DL(N);
4771 SDVTList Tys = DAG.getVTList(MVT::Other);
4772 SDValue Ops[] = { N->getOperand(0),
4773 lowerI128ToGR128(DAG, N->getOperand(2)),
4774 N->getOperand(1) };
4775 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
4776 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_STORE_128,
4777 DL, Tys, Ops, MVT::i128, MMO);
4778 // We have to enforce sequential consistency by performing a
4779 // serialization operation after the store.
4780 if (cast<AtomicSDNode>(N)->getOrdering() ==
4781 AtomicOrdering::SequentiallyConsistent)
4782 Res = SDValue(DAG.getMachineNode(SystemZ::Serialize, DL,
4783 MVT::Other, Res), 0);
4784 Results.push_back(Res);
4785 break;
4786 }
Ulrich Weigand0f1de042017-09-28 16:22:54 +00004787 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004788 SDLoc DL(N);
Ulrich Weigand0f1de042017-09-28 16:22:54 +00004789 SDVTList Tys = DAG.getVTList(MVT::Untyped, MVT::Other, MVT::Glue);
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004790 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
4791 lowerI128ToGR128(DAG, N->getOperand(2)),
4792 lowerI128ToGR128(DAG, N->getOperand(3)) };
4793 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
4794 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAP_128,
4795 DL, Tys, Ops, MVT::i128, MMO);
Ulrich Weigand0f1de042017-09-28 16:22:54 +00004796 SDValue Success = emitSETCC(DAG, DL, Res.getValue(2),
4797 SystemZ::CCMASK_CS, SystemZ::CCMASK_CS_EQ);
4798 Success = DAG.getZExtOrTrunc(Success, DL, N->getValueType(1));
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004799 Results.push_back(lowerGR128ToI128(DAG, Res));
Ulrich Weigand0f1de042017-09-28 16:22:54 +00004800 Results.push_back(Success);
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004801 Results.push_back(Res.getValue(1));
4802 break;
4803 }
4804 default:
4805 llvm_unreachable("Unexpected node to lower");
4806 }
4807}
4808
4809void
4810SystemZTargetLowering::ReplaceNodeResults(SDNode *N,
4811 SmallVectorImpl<SDValue> &Results,
4812 SelectionDAG &DAG) const {
4813 return LowerOperationWrapper(N, Results, DAG);
4814}
4815
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004816const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
4817#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
Matthias Braund04893f2015-05-07 21:33:59 +00004818 switch ((SystemZISD::NodeType)Opcode) {
4819 case SystemZISD::FIRST_NUMBER: break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004820 OPCODE(RET_FLAG);
4821 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00004822 OPCODE(SIBCALL);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004823 OPCODE(TLS_GDCALL);
4824 OPCODE(TLS_LDCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004825 OPCODE(PCREL_WRAPPER);
Richard Sandiford54b36912013-09-27 15:14:04 +00004826 OPCODE(PCREL_OFFSET);
Richard Sandiford57485472013-12-13 15:35:00 +00004827 OPCODE(IABS);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00004828 OPCODE(ICMP);
4829 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00004830 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004831 OPCODE(BR_CCMASK);
4832 OPCODE(SELECT_CCMASK);
4833 OPCODE(ADJDYNALLOC);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004834 OPCODE(POPCNT);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +00004835 OPCODE(SMUL_LOHI);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00004836 OPCODE(UMUL_LOHI);
4837 OPCODE(SDIVREM);
4838 OPCODE(UDIVREM);
Richard Sandifordd131ff82013-07-08 09:35:23 +00004839 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004840 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00004841 OPCODE(NC);
4842 OPCODE(NC_LOOP);
4843 OPCODE(OC);
4844 OPCODE(OC_LOOP);
4845 OPCODE(XC);
4846 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00004847 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004848 OPCODE(CLC_LOOP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00004849 OPCODE(STPCPY);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004850 OPCODE(STRCMP);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00004851 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00004852 OPCODE(IPM);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004853 OPCODE(MEMBARRIER);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004854 OPCODE(TBEGIN);
4855 OPCODE(TBEGIN_NOFLOAT);
4856 OPCODE(TEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004857 OPCODE(BYTE_MASK);
4858 OPCODE(ROTATE_MASK);
4859 OPCODE(REPLICATE);
4860 OPCODE(JOIN_DWORDS);
4861 OPCODE(SPLAT);
4862 OPCODE(MERGE_HIGH);
4863 OPCODE(MERGE_LOW);
4864 OPCODE(SHL_DOUBLE);
4865 OPCODE(PERMUTE_DWORDS);
4866 OPCODE(PERMUTE);
4867 OPCODE(PACK);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004868 OPCODE(PACKS_CC);
4869 OPCODE(PACKLS_CC);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004870 OPCODE(UNPACK_HIGH);
4871 OPCODE(UNPACKL_HIGH);
4872 OPCODE(UNPACK_LOW);
4873 OPCODE(UNPACKL_LOW);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004874 OPCODE(VSHL_BY_SCALAR);
4875 OPCODE(VSRL_BY_SCALAR);
4876 OPCODE(VSRA_BY_SCALAR);
4877 OPCODE(VSUM);
4878 OPCODE(VICMPE);
4879 OPCODE(VICMPH);
4880 OPCODE(VICMPHL);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004881 OPCODE(VICMPES);
4882 OPCODE(VICMPHS);
4883 OPCODE(VICMPHLS);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004884 OPCODE(VFCMPE);
4885 OPCODE(VFCMPH);
4886 OPCODE(VFCMPHE);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004887 OPCODE(VFCMPES);
4888 OPCODE(VFCMPHS);
4889 OPCODE(VFCMPHES);
4890 OPCODE(VFTCI);
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004891 OPCODE(VEXTEND);
4892 OPCODE(VROUND);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004893 OPCODE(VTM);
4894 OPCODE(VFAE_CC);
4895 OPCODE(VFAEZ_CC);
4896 OPCODE(VFEE_CC);
4897 OPCODE(VFEEZ_CC);
4898 OPCODE(VFENE_CC);
4899 OPCODE(VFENEZ_CC);
4900 OPCODE(VISTR_CC);
4901 OPCODE(VSTRC_CC);
4902 OPCODE(VSTRCZ_CC);
Marcin Koscielnicki32e87342016-07-02 02:20:40 +00004903 OPCODE(TDC);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004904 OPCODE(ATOMIC_SWAPW);
4905 OPCODE(ATOMIC_LOADW_ADD);
4906 OPCODE(ATOMIC_LOADW_SUB);
4907 OPCODE(ATOMIC_LOADW_AND);
4908 OPCODE(ATOMIC_LOADW_OR);
4909 OPCODE(ATOMIC_LOADW_XOR);
4910 OPCODE(ATOMIC_LOADW_NAND);
4911 OPCODE(ATOMIC_LOADW_MIN);
4912 OPCODE(ATOMIC_LOADW_MAX);
4913 OPCODE(ATOMIC_LOADW_UMIN);
4914 OPCODE(ATOMIC_LOADW_UMAX);
4915 OPCODE(ATOMIC_CMP_SWAPW);
Ulrich Weigand0f1de042017-09-28 16:22:54 +00004916 OPCODE(ATOMIC_CMP_SWAP);
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004917 OPCODE(ATOMIC_LOAD_128);
4918 OPCODE(ATOMIC_STORE_128);
4919 OPCODE(ATOMIC_CMP_SWAP_128);
Bryan Chan28b759c2016-05-16 20:32:22 +00004920 OPCODE(LRV);
4921 OPCODE(STRV);
Richard Sandiford03481332013-08-23 11:36:42 +00004922 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004923 }
Craig Topper062a2ba2014-04-25 05:30:21 +00004924 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004925#undef OPCODE
4926}
4927
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004928// Return true if VT is a vector whose elements are a whole number of bytes
Jonas Paulssoncad72ef2017-04-07 12:35:11 +00004929// in width. Also check for presence of vector support.
4930bool SystemZTargetLowering::canTreatAsByteVector(EVT VT) const {
4931 if (!Subtarget.hasVector())
4932 return false;
4933
Jonas Paulsson1d33cd32017-03-07 09:49:31 +00004934 return VT.isVector() && VT.getScalarSizeInBits() % 8 == 0 && VT.isSimple();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004935}
4936
4937// Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT
4938// producing a result of type ResVT. Op is a possibly bitcast version
4939// of the input vector and Index is the index (based on type VecVT) that
4940// should be extracted. Return the new extraction if a simplification
4941// was possible or if Force is true.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004942SDValue SystemZTargetLowering::combineExtract(const SDLoc &DL, EVT ResVT,
4943 EVT VecVT, SDValue Op,
4944 unsigned Index,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004945 DAGCombinerInfo &DCI,
4946 bool Force) const {
4947 SelectionDAG &DAG = DCI.DAG;
4948
4949 // The number of bytes being extracted.
4950 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4951
4952 for (;;) {
4953 unsigned Opcode = Op.getOpcode();
4954 if (Opcode == ISD::BITCAST)
4955 // Look through bitcasts.
4956 Op = Op.getOperand(0);
4957 else if (Opcode == ISD::VECTOR_SHUFFLE &&
4958 canTreatAsByteVector(Op.getValueType())) {
4959 // Get a VPERM-like permute mask and see whether the bytes covered
4960 // by the extracted element are a contiguous sequence from one
4961 // source operand.
4962 SmallVector<int, SystemZ::VectorBytes> Bytes;
4963 getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes);
4964 int First;
4965 if (!getShuffleInput(Bytes, Index * BytesPerElement,
4966 BytesPerElement, First))
4967 break;
4968 if (First < 0)
4969 return DAG.getUNDEF(ResVT);
4970 // Make sure the contiguous sequence starts at a multiple of the
4971 // original element size.
4972 unsigned Byte = unsigned(First) % Bytes.size();
4973 if (Byte % BytesPerElement != 0)
4974 break;
4975 // We can get the extracted value directly from an input.
4976 Index = Byte / BytesPerElement;
4977 Op = Op.getOperand(unsigned(First) / Bytes.size());
4978 Force = true;
4979 } else if (Opcode == ISD::BUILD_VECTOR &&
4980 canTreatAsByteVector(Op.getValueType())) {
4981 // We can only optimize this case if the BUILD_VECTOR elements are
4982 // at least as wide as the extracted value.
4983 EVT OpVT = Op.getValueType();
4984 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4985 if (OpBytesPerElement < BytesPerElement)
4986 break;
4987 // Make sure that the least-significant bit of the extracted value
4988 // is the least significant bit of an input.
4989 unsigned End = (Index + 1) * BytesPerElement;
4990 if (End % OpBytesPerElement != 0)
4991 break;
4992 // We're extracting the low part of one operand of the BUILD_VECTOR.
4993 Op = Op.getOperand(End / OpBytesPerElement - 1);
4994 if (!Op.getValueType().isInteger()) {
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00004995 EVT VT = MVT::getIntegerVT(Op.getValueSizeInBits());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004996 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
4997 DCI.AddToWorklist(Op.getNode());
4998 }
4999 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits());
5000 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
5001 if (VT != ResVT) {
5002 DCI.AddToWorklist(Op.getNode());
5003 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op);
5004 }
5005 return Op;
5006 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00005007 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG ||
5008 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) &&
5009 canTreatAsByteVector(Op.getValueType()) &&
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005010 canTreatAsByteVector(Op.getOperand(0).getValueType())) {
5011 // Make sure that only the unextended bits are significant.
5012 EVT ExtVT = Op.getValueType();
5013 EVT OpVT = Op.getOperand(0).getValueType();
5014 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize();
5015 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
5016 unsigned Byte = Index * BytesPerElement;
5017 unsigned SubByte = Byte % ExtBytesPerElement;
5018 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement;
5019 if (SubByte < MinSubByte ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00005020 SubByte + BytesPerElement > ExtBytesPerElement)
5021 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005022 // Get the byte offset of the unextended element
5023 Byte = Byte / ExtBytesPerElement * OpBytesPerElement;
5024 // ...then add the byte offset relative to that element.
5025 Byte += SubByte - MinSubByte;
5026 if (Byte % BytesPerElement != 0)
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00005027 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005028 Op = Op.getOperand(0);
5029 Index = Byte / BytesPerElement;
5030 Force = true;
5031 } else
5032 break;
5033 }
5034 if (Force) {
5035 if (Op.getValueType() != VecVT) {
5036 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op);
5037 DCI.AddToWorklist(Op.getNode());
5038 }
5039 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op,
5040 DAG.getConstant(Index, DL, MVT::i32));
5041 }
5042 return SDValue();
5043}
5044
5045// Optimize vector operations in scalar value Op on the basis that Op
5046// is truncated to TruncVT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005047SDValue SystemZTargetLowering::combineTruncateExtract(
5048 const SDLoc &DL, EVT TruncVT, SDValue Op, DAGCombinerInfo &DCI) const {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005049 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into
5050 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements
5051 // of type TruncVT.
5052 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5053 TruncVT.getSizeInBits() % 8 == 0) {
5054 SDValue Vec = Op.getOperand(0);
5055 EVT VecVT = Vec.getValueType();
5056 if (canTreatAsByteVector(VecVT)) {
5057 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
5058 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
5059 unsigned TruncBytes = TruncVT.getStoreSize();
5060 if (BytesPerElement % TruncBytes == 0) {
5061 // Calculate the value of Y' in the above description. We are
5062 // splitting the original elements into Scale equal-sized pieces
5063 // and for truncation purposes want the last (least-significant)
5064 // of these pieces for IndexN. This is easiest to do by calculating
5065 // the start index of the following element and then subtracting 1.
5066 unsigned Scale = BytesPerElement / TruncBytes;
5067 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1;
5068
5069 // Defer the creation of the bitcast from X to combineExtract,
5070 // which might be able to optimize the extraction.
5071 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8),
5072 VecVT.getStoreSize() / TruncBytes);
5073 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT);
5074 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true);
5075 }
5076 }
5077 }
5078 }
5079 return SDValue();
5080}
5081
Ulrich Weigand849a59f2018-01-19 20:52:04 +00005082SDValue SystemZTargetLowering::combineZERO_EXTEND(
5083 SDNode *N, DAGCombinerInfo &DCI) const {
5084 // Convert (zext (select_ccmask C1, C2)) into (select_ccmask C1', C2')
5085 SelectionDAG &DAG = DCI.DAG;
5086 SDValue N0 = N->getOperand(0);
5087 EVT VT = N->getValueType(0);
5088 if (N0.getOpcode() == SystemZISD::SELECT_CCMASK) {
5089 auto *TrueOp = dyn_cast<ConstantSDNode>(N0.getOperand(0));
5090 auto *FalseOp = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5091 if (TrueOp && FalseOp) {
5092 SDLoc DL(N0);
5093 SDValue Ops[] = { DAG.getConstant(TrueOp->getZExtValue(), DL, VT),
5094 DAG.getConstant(FalseOp->getZExtValue(), DL, VT),
5095 N0.getOperand(2), N0.getOperand(3), N0.getOperand(4) };
5096 SDValue NewSelect = DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VT, Ops);
5097 // If N0 has multiple uses, change other uses as well.
5098 if (!N0.hasOneUse()) {
5099 SDValue TruncSelect =
5100 DAG.getNode(ISD::TRUNCATE, DL, N0.getValueType(), NewSelect);
5101 DCI.CombineTo(N0.getNode(), TruncSelect);
5102 }
5103 return NewSelect;
5104 }
5105 }
5106 return SDValue();
5107}
5108
5109SDValue SystemZTargetLowering::combineSIGN_EXTEND_INREG(
5110 SDNode *N, DAGCombinerInfo &DCI) const {
5111 // Convert (sext_in_reg (setcc LHS, RHS, COND), i1)
5112 // and (sext_in_reg (any_extend (setcc LHS, RHS, COND)), i1)
5113 // into (select_cc LHS, RHS, -1, 0, COND)
5114 SelectionDAG &DAG = DCI.DAG;
5115 SDValue N0 = N->getOperand(0);
5116 EVT VT = N->getValueType(0);
5117 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
5118 if (N0.hasOneUse() && N0.getOpcode() == ISD::ANY_EXTEND)
5119 N0 = N0.getOperand(0);
5120 if (EVT == MVT::i1 && N0.hasOneUse() && N0.getOpcode() == ISD::SETCC) {
5121 SDLoc DL(N0);
5122 SDValue Ops[] = { N0.getOperand(0), N0.getOperand(1),
5123 DAG.getConstant(-1, DL, VT), DAG.getConstant(0, DL, VT),
5124 N0.getOperand(2) };
5125 return DAG.getNode(ISD::SELECT_CC, DL, VT, Ops);
5126 }
5127 return SDValue();
5128}
5129
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005130SDValue SystemZTargetLowering::combineSIGN_EXTEND(
5131 SDNode *N, DAGCombinerInfo &DCI) const {
5132 // Convert (sext (ashr (shl X, C1), C2)) to
5133 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
5134 // cheap as narrower ones.
5135 SelectionDAG &DAG = DCI.DAG;
5136 SDValue N0 = N->getOperand(0);
5137 EVT VT = N->getValueType(0);
5138 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
5139 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5140 SDValue Inner = N0.getOperand(0);
5141 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
5142 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00005143 unsigned Extra = (VT.getSizeInBits() - N0.getValueSizeInBits());
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005144 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
5145 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
5146 EVT ShiftVT = N0.getOperand(1).getValueType();
5147 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
5148 Inner.getOperand(0));
5149 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
5150 DAG.getConstant(NewShlAmt, SDLoc(Inner),
5151 ShiftVT));
5152 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
5153 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT));
5154 }
5155 }
5156 }
5157 return SDValue();
5158}
5159
5160SDValue SystemZTargetLowering::combineMERGE(
5161 SDNode *N, DAGCombinerInfo &DCI) const {
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005162 SelectionDAG &DAG = DCI.DAG;
5163 unsigned Opcode = N->getOpcode();
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005164 SDValue Op0 = N->getOperand(0);
5165 SDValue Op1 = N->getOperand(1);
5166 if (Op0.getOpcode() == ISD::BITCAST)
5167 Op0 = Op0.getOperand(0);
5168 if (Op0.getOpcode() == SystemZISD::BYTE_MASK &&
5169 cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0) {
5170 // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF
5171 // for v4f32.
5172 if (Op1 == N->getOperand(0))
5173 return Op1;
5174 // (z_merge_? 0, X) -> (z_unpackl_? 0, X).
5175 EVT VT = Op1.getValueType();
5176 unsigned ElemBytes = VT.getVectorElementType().getStoreSize();
5177 if (ElemBytes <= 4) {
5178 Opcode = (Opcode == SystemZISD::MERGE_HIGH ?
5179 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW);
5180 EVT InVT = VT.changeVectorElementTypeToInteger();
5181 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16),
5182 SystemZ::VectorBytes / ElemBytes / 2);
5183 if (VT != InVT) {
5184 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1);
5185 DCI.AddToWorklist(Op1.getNode());
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005186 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005187 SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1);
5188 DCI.AddToWorklist(Op.getNode());
5189 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005190 }
5191 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005192 return SDValue();
5193}
5194
5195SDValue SystemZTargetLowering::combineSTORE(
5196 SDNode *N, DAGCombinerInfo &DCI) const {
5197 SelectionDAG &DAG = DCI.DAG;
5198 auto *SN = cast<StoreSDNode>(N);
5199 auto &Op1 = N->getOperand(1);
5200 EVT MemVT = SN->getMemoryVT();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005201 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better
5202 // for the extraction to be done on a vMiN value, so that we can use VSTE.
5203 // If X has wider elements then convert it to:
5204 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z).
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005205 if (MemVT.isInteger()) {
5206 if (SDValue Value =
5207 combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) {
5208 DCI.AddToWorklist(Value.getNode());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005209
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005210 // Rewrite the store with the new form of stored value.
5211 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value,
5212 SN->getBasePtr(), SN->getMemoryVT(),
5213 SN->getMemOperand());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005214 }
5215 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005216 // Combine STORE (BSWAP) into STRVH/STRV/STRVG
5217 // See comment in combineBSWAP about volatile accesses.
Ulrich Weigand59a01a92017-09-19 20:50:05 +00005218 if (!SN->isTruncatingStore() &&
5219 !SN->isVolatile() &&
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005220 Op1.getOpcode() == ISD::BSWAP &&
5221 Op1.getNode()->hasOneUse() &&
5222 (Op1.getValueType() == MVT::i16 ||
5223 Op1.getValueType() == MVT::i32 ||
5224 Op1.getValueType() == MVT::i64)) {
5225
5226 SDValue BSwapOp = Op1.getOperand(0);
5227
5228 if (BSwapOp.getValueType() == MVT::i16)
5229 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), MVT::i32, BSwapOp);
5230
5231 SDValue Ops[] = {
5232 N->getOperand(0), BSwapOp, N->getOperand(2),
5233 DAG.getValueType(Op1.getValueType())
5234 };
5235
5236 return
5237 DAG.getMemIntrinsicNode(SystemZISD::STRV, SDLoc(N), DAG.getVTList(MVT::Other),
5238 Ops, MemVT, SN->getMemOperand());
5239 }
5240 return SDValue();
5241}
5242
5243SDValue SystemZTargetLowering::combineEXTRACT_VECTOR_ELT(
5244 SDNode *N, DAGCombinerInfo &DCI) const {
Jonas Paulsson56bb0852017-03-31 13:22:59 +00005245
Jonas Paulsson56bb0852017-03-31 13:22:59 +00005246 if (!Subtarget.hasVector())
5247 return SDValue();
5248
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005249 // Try to simplify a vector extraction.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005250 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
5251 SDValue Op0 = N->getOperand(0);
5252 EVT VecVT = Op0.getValueType();
5253 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0,
5254 IndexN->getZExtValue(), DCI, false);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005255 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005256 return SDValue();
5257}
5258
5259SDValue SystemZTargetLowering::combineJOIN_DWORDS(
5260 SDNode *N, DAGCombinerInfo &DCI) const {
5261 SelectionDAG &DAG = DCI.DAG;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005262 // (join_dwords X, X) == (replicate X)
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005263 if (N->getOperand(0) == N->getOperand(1))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005264 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0),
5265 N->getOperand(0));
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005266 return SDValue();
5267}
5268
5269SDValue SystemZTargetLowering::combineFP_ROUND(
5270 SDNode *N, DAGCombinerInfo &DCI) const {
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00005271 // (fpround (extract_vector_elt X 0))
5272 // (fpround (extract_vector_elt X 1)) ->
Ulrich Weigand80b3af72015-05-05 19:27:45 +00005273 // (extract_vector_elt (VROUND X) 0)
5274 // (extract_vector_elt (VROUND X) 1)
5275 //
5276 // This is a special case since the target doesn't really support v2f32s.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005277 SelectionDAG &DAG = DCI.DAG;
5278 SDValue Op0 = N->getOperand(0);
5279 if (N->getValueType(0) == MVT::f32 &&
5280 Op0.hasOneUse() &&
5281 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5282 Op0.getOperand(0).getValueType() == MVT::v2f64 &&
5283 Op0.getOperand(1).getOpcode() == ISD::Constant &&
5284 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) {
5285 SDValue Vec = Op0.getOperand(0);
5286 for (auto *U : Vec->uses()) {
5287 if (U != Op0.getNode() &&
5288 U->hasOneUse() &&
5289 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5290 U->getOperand(0) == Vec &&
5291 U->getOperand(1).getOpcode() == ISD::Constant &&
5292 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) {
5293 SDValue OtherRound = SDValue(*U->use_begin(), 0);
5294 if (OtherRound.getOpcode() == ISD::FP_ROUND &&
5295 OtherRound.getOperand(0) == SDValue(U, 0) &&
5296 OtherRound.getValueType() == MVT::f32) {
5297 SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N),
5298 MVT::v4f32, Vec);
5299 DCI.AddToWorklist(VRound.getNode());
5300 SDValue Extract1 =
5301 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32,
5302 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32));
5303 DCI.AddToWorklist(Extract1.getNode());
5304 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1);
5305 SDValue Extract0 =
5306 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32,
5307 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
5308 return Extract0;
Ulrich Weigand80b3af72015-05-05 19:27:45 +00005309 }
5310 }
5311 }
5312 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005313 return SDValue();
5314}
Bryan Chan28b759c2016-05-16 20:32:22 +00005315
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005316SDValue SystemZTargetLowering::combineBSWAP(
5317 SDNode *N, DAGCombinerInfo &DCI) const {
5318 SelectionDAG &DAG = DCI.DAG;
Bryan Chan28b759c2016-05-16 20:32:22 +00005319 // Combine BSWAP (LOAD) into LRVH/LRV/LRVG
5320 // These loads are allowed to access memory multiple times, and so we must check
5321 // that the loads are not volatile before performing the combine.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005322 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
5323 N->getOperand(0).hasOneUse() &&
5324 (N->getValueType(0) == MVT::i16 || N->getValueType(0) == MVT::i32 ||
5325 N->getValueType(0) == MVT::i64) &&
5326 !cast<LoadSDNode>(N->getOperand(0))->isVolatile()) {
Bryan Chan28b759c2016-05-16 20:32:22 +00005327 SDValue Load = N->getOperand(0);
5328 LoadSDNode *LD = cast<LoadSDNode>(Load);
5329
5330 // Create the byte-swapping load.
5331 SDValue Ops[] = {
5332 LD->getChain(), // Chain
5333 LD->getBasePtr(), // Ptr
5334 DAG.getValueType(N->getValueType(0)) // VT
5335 };
5336 SDValue BSLoad =
5337 DAG.getMemIntrinsicNode(SystemZISD::LRV, SDLoc(N),
5338 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
5339 MVT::i64 : MVT::i32, MVT::Other),
5340 Ops, LD->getMemoryVT(), LD->getMemOperand());
5341
5342 // If this is an i16 load, insert the truncate.
5343 SDValue ResVal = BSLoad;
5344 if (N->getValueType(0) == MVT::i16)
5345 ResVal = DAG.getNode(ISD::TRUNCATE, SDLoc(N), MVT::i16, BSLoad);
5346
5347 // First, combine the bswap away. This makes the value produced by the
5348 // load dead.
5349 DCI.CombineTo(N, ResVal);
5350
5351 // Next, combine the load away, we give it a bogus result value but a real
5352 // chain result. The result value is dead because the bswap is dead.
5353 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
5354
5355 // Return N so it doesn't get rechecked!
5356 return SDValue(N, 0);
5357 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005358 return SDValue();
5359}
Bryan Chan28b759c2016-05-16 20:32:22 +00005360
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005361SDValue SystemZTargetLowering::combineSHIFTROT(
5362 SDNode *N, DAGCombinerInfo &DCI) const {
5363
5364 SelectionDAG &DAG = DCI.DAG;
5365
5366 // Shift/rotate instructions only use the last 6 bits of the second operand
5367 // register. If the second operand is the result of an AND with an immediate
5368 // value that has its last 6 bits set, we can safely remove the AND operation.
Elliot Colp687691a2016-08-18 18:04:26 +00005369 //
5370 // If the AND operation doesn't have the last 6 bits set, we can't remove it
Elliot Colpa4092102016-08-23 14:03:02 +00005371 // entirely, but we can still truncate it to a 16-bit value. This prevents
5372 // us from ending up with a NILL with a signed operand, which will cause the
5373 // instruction printer to abort.
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005374 SDValue N1 = N->getOperand(1);
5375 if (N1.getOpcode() == ISD::AND) {
Elliot Colp687691a2016-08-18 18:04:26 +00005376 SDValue AndMaskOp = N1->getOperand(1);
5377 auto *AndMask = dyn_cast<ConstantSDNode>(AndMaskOp);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005378
5379 // The AND mask is constant
5380 if (AndMask) {
Elliot Colpa4092102016-08-23 14:03:02 +00005381 auto AmtVal = AndMask->getZExtValue();
5382
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005383 // Bottom 6 bits are set
5384 if ((AmtVal & 0x3f) == 0x3f) {
Elliot Colpa4092102016-08-23 14:03:02 +00005385 SDValue AndOp = N1->getOperand(0);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005386
5387 // This is the only use, so remove the node
5388 if (N1.hasOneUse()) {
5389 // Combine the AND away
5390 DCI.CombineTo(N1.getNode(), AndOp);
5391
5392 // Return N so it isn't rechecked
5393 return SDValue(N, 0);
5394
5395 // The node will be reused, so create a new node for this one use
5396 } else {
5397 SDValue Replace = DAG.getNode(N->getOpcode(), SDLoc(N),
5398 N->getValueType(0), N->getOperand(0),
5399 AndOp);
5400 DCI.AddToWorklist(Replace.getNode());
5401
5402 return Replace;
5403 }
Elliot Colp687691a2016-08-18 18:04:26 +00005404
Elliot Colpa4092102016-08-23 14:03:02 +00005405 // We can't remove the AND, but we can use NILL here (normally we would
5406 // use NILF). Only keep the last 16 bits of the mask. The actual
5407 // transformation will be handled by .td definitions.
5408 } else if (AmtVal >> 16 != 0) {
5409 SDValue AndOp = N1->getOperand(0);
Elliot Colp687691a2016-08-18 18:04:26 +00005410
Elliot Colpa4092102016-08-23 14:03:02 +00005411 auto NewMask = DAG.getConstant(AndMask->getZExtValue() & 0x0000ffff,
5412 SDLoc(AndMaskOp),
5413 AndMaskOp.getValueType());
Elliot Colp687691a2016-08-18 18:04:26 +00005414
Elliot Colpa4092102016-08-23 14:03:02 +00005415 auto NewAnd = DAG.getNode(N1.getOpcode(), SDLoc(N1), N1.getValueType(),
5416 AndOp, NewMask);
Elliot Colp687691a2016-08-18 18:04:26 +00005417
Elliot Colpa4092102016-08-23 14:03:02 +00005418 SDValue Replace = DAG.getNode(N->getOpcode(), SDLoc(N),
5419 N->getValueType(0), N->getOperand(0),
5420 NewAnd);
5421 DCI.AddToWorklist(Replace.getNode());
Elliot Colp687691a2016-08-18 18:04:26 +00005422
Elliot Colpa4092102016-08-23 14:03:02 +00005423 return Replace;
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005424 }
5425 }
5426 }
5427
5428 return SDValue();
5429}
5430
Ulrich Weigand31112892018-01-19 20:54:18 +00005431static bool combineCCMask(SDValue &Glue, int &CCValid, int &CCMask) {
5432 // We have a SELECT_CCMASK or BR_CCMASK comparing the condition code
5433 // set by the glued instruction using the CCValid / CCMask masks,
5434 // If the glued instruction is itself a (ICMP (SELECT_CCMASK)) testing
5435 // the condition code set by some other instruction, see whether we
5436 // can directly use that condition code.
5437 bool Invert = false;
5438
5439 // Verify that we have an appropriate mask for a EQ or NE comparison.
5440 if (CCValid != SystemZ::CCMASK_ICMP)
5441 return false;
5442 if (CCMask == SystemZ::CCMASK_CMP_NE)
5443 Invert = !Invert;
5444 else if (CCMask != SystemZ::CCMASK_CMP_EQ)
5445 return false;
5446
5447 // Verify that we have an ICMP that is the single user of a SELECT_CCMASK.
5448 SDNode *ICmp = Glue.getNode();
5449 if (ICmp->getOpcode() != SystemZISD::ICMP)
5450 return false;
5451 SDNode *Select = ICmp->getOperand(0).getNode();
5452 if (Select->getOpcode() != SystemZISD::SELECT_CCMASK)
5453 return false;
5454 if (!Select->hasOneUse())
5455 return false;
5456
5457 // Verify that the ICMP compares against one of select values.
5458 auto *CompareVal = dyn_cast<ConstantSDNode>(ICmp->getOperand(1));
5459 if (!CompareVal)
5460 return false;
5461 auto *TrueVal = dyn_cast<ConstantSDNode>(Select->getOperand(0));
5462 if (!TrueVal)
5463 return false;
5464 auto *FalseVal = dyn_cast<ConstantSDNode>(Select->getOperand(1));
5465 if (!FalseVal)
5466 return false;
5467 if (CompareVal->getZExtValue() == FalseVal->getZExtValue())
5468 Invert = !Invert;
5469 else if (CompareVal->getZExtValue() != TrueVal->getZExtValue())
5470 return false;
5471
5472 // Compute the effective CC mask for the new branch or select.
5473 auto *NewCCValid = dyn_cast<ConstantSDNode>(Select->getOperand(2));
5474 auto *NewCCMask = dyn_cast<ConstantSDNode>(Select->getOperand(3));
5475 if (!NewCCValid || !NewCCMask)
5476 return false;
5477 CCValid = NewCCValid->getZExtValue();
5478 CCMask = NewCCMask->getZExtValue();
5479 if (Invert)
5480 CCMask ^= CCValid;
5481
5482 // Return the updated Glue link.
5483 Glue = Select->getOperand(4);
5484 return true;
5485}
5486
Ulrich Weigand145d63f2018-01-22 15:41:49 +00005487static bool combineMergeChains(SDValue &Chain, SDValue Glue) {
5488 // We are about to glue an instruction with input chain Chain to the
5489 // instruction Glue. Verify that this would not create an invalid
5490 // topological sort due to intervening chain nodes.
5491
5492 SDNode *Node = Glue.getNode();
5493 for (int ResNo = Node->getNumValues() - 1; ResNo >= 0; --ResNo)
5494 if (Node->getValueType(ResNo) == MVT::Other) {
5495 SDValue OutChain = SDValue(Node, ResNo);
5496 // FIXME: We should be able to at least handle an intervening
5497 // TokenFactor node by swapping chains around a bit ...
5498 return Chain == OutChain;
5499 }
5500
5501 return true;
5502}
5503
Ulrich Weigand31112892018-01-19 20:54:18 +00005504SDValue SystemZTargetLowering::combineBR_CCMASK(
5505 SDNode *N, DAGCombinerInfo &DCI) const {
5506 SelectionDAG &DAG = DCI.DAG;
5507
5508 // Combine BR_CCMASK (ICMP (SELECT_CCMASK)) into a single BR_CCMASK.
5509 auto *CCValid = dyn_cast<ConstantSDNode>(N->getOperand(1));
5510 auto *CCMask = dyn_cast<ConstantSDNode>(N->getOperand(2));
5511 if (!CCValid || !CCMask)
5512 return SDValue();
5513
5514 int CCValidVal = CCValid->getZExtValue();
5515 int CCMaskVal = CCMask->getZExtValue();
Ulrich Weigand145d63f2018-01-22 15:41:49 +00005516 SDValue Chain = N->getOperand(0);
Ulrich Weigand31112892018-01-19 20:54:18 +00005517 SDValue Glue = N->getOperand(4);
5518
Ulrich Weigand145d63f2018-01-22 15:41:49 +00005519 if (combineCCMask(Glue, CCValidVal, CCMaskVal)
5520 && combineMergeChains(Chain, Glue))
Ulrich Weigand31112892018-01-19 20:54:18 +00005521 return DAG.getNode(SystemZISD::BR_CCMASK, SDLoc(N), N->getValueType(0),
Ulrich Weigand145d63f2018-01-22 15:41:49 +00005522 Chain,
Ulrich Weigand31112892018-01-19 20:54:18 +00005523 DAG.getConstant(CCValidVal, SDLoc(N), MVT::i32),
5524 DAG.getConstant(CCMaskVal, SDLoc(N), MVT::i32),
5525 N->getOperand(3), Glue);
5526 return SDValue();
5527}
5528
5529SDValue SystemZTargetLowering::combineSELECT_CCMASK(
5530 SDNode *N, DAGCombinerInfo &DCI) const {
5531 SelectionDAG &DAG = DCI.DAG;
5532
5533 // Combine SELECT_CCMASK (ICMP (SELECT_CCMASK)) into a single SELECT_CCMASK.
5534 auto *CCValid = dyn_cast<ConstantSDNode>(N->getOperand(2));
5535 auto *CCMask = dyn_cast<ConstantSDNode>(N->getOperand(3));
5536 if (!CCValid || !CCMask)
5537 return SDValue();
5538
5539 int CCValidVal = CCValid->getZExtValue();
5540 int CCMaskVal = CCMask->getZExtValue();
5541 SDValue Glue = N->getOperand(4);
5542
5543 if (combineCCMask(Glue, CCValidVal, CCMaskVal))
5544 return DAG.getNode(SystemZISD::SELECT_CCMASK, SDLoc(N), N->getValueType(0),
5545 N->getOperand(0),
5546 N->getOperand(1),
5547 DAG.getConstant(CCValidVal, SDLoc(N), MVT::i32),
5548 DAG.getConstant(CCMaskVal, SDLoc(N), MVT::i32),
5549 Glue);
5550 return SDValue();
5551}
5552
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005553SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
5554 DAGCombinerInfo &DCI) const {
5555 switch(N->getOpcode()) {
5556 default: break;
Ulrich Weigand849a59f2018-01-19 20:52:04 +00005557 case ISD::ZERO_EXTEND: return combineZERO_EXTEND(N, DCI);
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005558 case ISD::SIGN_EXTEND: return combineSIGN_EXTEND(N, DCI);
Ulrich Weigand849a59f2018-01-19 20:52:04 +00005559 case ISD::SIGN_EXTEND_INREG: return combineSIGN_EXTEND_INREG(N, DCI);
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005560 case SystemZISD::MERGE_HIGH:
5561 case SystemZISD::MERGE_LOW: return combineMERGE(N, DCI);
5562 case ISD::STORE: return combineSTORE(N, DCI);
5563 case ISD::EXTRACT_VECTOR_ELT: return combineEXTRACT_VECTOR_ELT(N, DCI);
5564 case SystemZISD::JOIN_DWORDS: return combineJOIN_DWORDS(N, DCI);
5565 case ISD::FP_ROUND: return combineFP_ROUND(N, DCI);
5566 case ISD::BSWAP: return combineBSWAP(N, DCI);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005567 case ISD::SHL:
5568 case ISD::SRA:
5569 case ISD::SRL:
5570 case ISD::ROTL: return combineSHIFTROT(N, DCI);
Ulrich Weigand31112892018-01-19 20:54:18 +00005571 case SystemZISD::BR_CCMASK: return combineBR_CCMASK(N, DCI);
5572 case SystemZISD::SELECT_CCMASK: return combineSELECT_CCMASK(N, DCI);
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005573 }
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005574
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005575 return SDValue();
5576}
5577
Ulrich Weigand9eb858c2018-01-19 20:49:05 +00005578void
5579SystemZTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
5580 KnownBits &Known,
5581 const APInt &DemandedElts,
5582 const SelectionDAG &DAG,
5583 unsigned Depth) const {
5584 unsigned BitWidth = Known.getBitWidth();
5585
5586 Known.resetAll();
5587 switch (Op.getOpcode()) {
5588 case SystemZISD::SELECT_CCMASK: {
5589 KnownBits TrueKnown(BitWidth), FalseKnown(BitWidth);
5590 DAG.computeKnownBits(Op.getOperand(0), TrueKnown, Depth + 1);
5591 DAG.computeKnownBits(Op.getOperand(1), FalseKnown, Depth + 1);
5592 Known.Zero = TrueKnown.Zero & FalseKnown.Zero;
5593 Known.One = TrueKnown.One & FalseKnown.One;
5594 break;
5595 }
5596
5597 default:
5598 break;
5599 }
5600}
5601
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005602//===----------------------------------------------------------------------===//
5603// Custom insertion
5604//===----------------------------------------------------------------------===//
5605
5606// Create a new basic block after MBB.
5607static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
5608 MachineFunction &MF = *MBB->getParent();
5609 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00005610 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005611 return NewMBB;
5612}
5613
Richard Sandifordbe133a82013-08-28 09:01:51 +00005614// Split MBB after MI and return the new block (the one that contains
5615// instructions after MI).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005616static MachineBasicBlock *splitBlockAfter(MachineBasicBlock::iterator MI,
Richard Sandifordbe133a82013-08-28 09:01:51 +00005617 MachineBasicBlock *MBB) {
5618 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
5619 NewMBB->splice(NewMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00005620 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Richard Sandifordbe133a82013-08-28 09:01:51 +00005621 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
5622 return NewMBB;
5623}
5624
Richard Sandiford5e318f02013-08-27 09:54:29 +00005625// Split MBB before MI and return the new block (the one that contains MI).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005626static MachineBasicBlock *splitBlockBefore(MachineBasicBlock::iterator MI,
Richard Sandiford5e318f02013-08-27 09:54:29 +00005627 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005628 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005629 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005630 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
5631 return NewMBB;
5632}
5633
Richard Sandiford5e318f02013-08-27 09:54:29 +00005634// Force base value Base into a register before MI. Return the register.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005635static unsigned forceReg(MachineInstr &MI, MachineOperand &Base,
Richard Sandiford5e318f02013-08-27 09:54:29 +00005636 const SystemZInstrInfo *TII) {
5637 if (Base.isReg())
5638 return Base.getReg();
5639
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005640 MachineBasicBlock *MBB = MI.getParent();
Richard Sandiford5e318f02013-08-27 09:54:29 +00005641 MachineFunction &MF = *MBB->getParent();
5642 MachineRegisterInfo &MRI = MF.getRegInfo();
5643
5644 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005645 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LA), Reg)
Diana Picus116bbab2017-01-13 09:58:52 +00005646 .add(Base)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005647 .addImm(0)
5648 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005649 return Reg;
5650}
5651
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005652// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
5653MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005654SystemZTargetLowering::emitSelect(MachineInstr &MI,
Ulrich Weigand524f2762016-11-28 13:34:08 +00005655 MachineBasicBlock *MBB,
5656 unsigned LOCROpcode) const {
Eric Christophera6734172015-01-31 00:06:45 +00005657 const SystemZInstrInfo *TII =
5658 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005659
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005660 unsigned DestReg = MI.getOperand(0).getReg();
5661 unsigned TrueReg = MI.getOperand(1).getReg();
5662 unsigned FalseReg = MI.getOperand(2).getReg();
5663 unsigned CCValid = MI.getOperand(3).getImm();
5664 unsigned CCMask = MI.getOperand(4).getImm();
5665 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005666
Ulrich Weigand524f2762016-11-28 13:34:08 +00005667 // Use LOCROpcode if possible.
5668 if (LOCROpcode && Subtarget.hasLoadStoreOnCond()) {
5669 BuildMI(*MBB, MI, DL, TII->get(LOCROpcode), DestReg)
5670 .addReg(FalseReg).addReg(TrueReg)
5671 .addImm(CCValid).addImm(CCMask);
5672 MI.eraseFromParent();
5673 return MBB;
5674 }
5675
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005676 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005677 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005678 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5679
5680 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00005681 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005682 // # fallthrough to FalseMBB
5683 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005684 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5685 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005686 MBB->addSuccessor(JoinMBB);
5687 MBB->addSuccessor(FalseMBB);
5688
5689 // FalseMBB:
5690 // # fallthrough to JoinMBB
5691 MBB = FalseMBB;
5692 MBB->addSuccessor(JoinMBB);
5693
5694 // JoinMBB:
5695 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
5696 // ...
5697 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005698 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005699 .addReg(TrueReg).addMBB(StartMBB)
5700 .addReg(FalseReg).addMBB(FalseMBB);
5701
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005702 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005703 return JoinMBB;
5704}
5705
Richard Sandifordb86a8342013-06-27 09:27:40 +00005706// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
5707// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005708// happen when the condition is false rather than true. If a STORE ON
5709// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005710MachineBasicBlock *SystemZTargetLowering::emitCondStore(MachineInstr &MI,
5711 MachineBasicBlock *MBB,
5712 unsigned StoreOpcode,
5713 unsigned STOCOpcode,
5714 bool Invert) const {
Eric Christophera6734172015-01-31 00:06:45 +00005715 const SystemZInstrInfo *TII =
5716 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordb86a8342013-06-27 09:27:40 +00005717
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005718 unsigned SrcReg = MI.getOperand(0).getReg();
5719 MachineOperand Base = MI.getOperand(1);
5720 int64_t Disp = MI.getOperand(2).getImm();
5721 unsigned IndexReg = MI.getOperand(3).getReg();
5722 unsigned CCValid = MI.getOperand(4).getImm();
5723 unsigned CCMask = MI.getOperand(5).getImm();
5724 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005725
5726 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
5727
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005728 // Use STOCOpcode if possible. We could use different store patterns in
5729 // order to avoid matching the index register, but the performance trade-offs
5730 // might be more complicated in that case.
Eric Christopher93bf97c2014-06-27 07:38:01 +00005731 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005732 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005733 CCMask ^= CCValid;
Jonas Paulssonae8d22c2017-06-07 14:08:34 +00005734
5735 // ISel pattern matching also adds a load memory operand of the same
5736 // address, so take special care to find the storing memory operand.
5737 MachineMemOperand *MMO = nullptr;
5738 for (auto *I : MI.memoperands())
5739 if (I->isStore()) {
5740 MMO = I;
5741 break;
5742 }
5743
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005744 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Jonas Paulssonae8d22c2017-06-07 14:08:34 +00005745 .addReg(SrcReg)
5746 .add(Base)
5747 .addImm(Disp)
5748 .addImm(CCValid)
5749 .addImm(CCMask)
5750 .addMemOperand(MMO);
5751
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005752 MI.eraseFromParent();
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005753 return MBB;
5754 }
5755
Richard Sandifordb86a8342013-06-27 09:27:40 +00005756 // Get the condition needed to branch around the store.
5757 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005758 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00005759
5760 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005761 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005762 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5763
5764 // StartMBB:
5765 // BRC CCMask, JoinMBB
5766 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00005767 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005768 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5769 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005770 MBB->addSuccessor(JoinMBB);
5771 MBB->addSuccessor(FalseMBB);
5772
5773 // FalseMBB:
5774 // store %SrcReg, %Disp(%Index,%Base)
5775 // # fallthrough to JoinMBB
5776 MBB = FalseMBB;
5777 BuildMI(MBB, DL, TII->get(StoreOpcode))
Diana Picus116bbab2017-01-13 09:58:52 +00005778 .addReg(SrcReg)
5779 .add(Base)
5780 .addImm(Disp)
5781 .addReg(IndexReg);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005782 MBB->addSuccessor(JoinMBB);
5783
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005784 MI.eraseFromParent();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005785 return JoinMBB;
5786}
5787
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005788// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
5789// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
5790// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
5791// BitSize is the width of the field in bits, or 0 if this is a partword
5792// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
5793// is one of the operands. Invert says whether the field should be
5794// inverted after performing BinOpcode (e.g. for NAND).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005795MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadBinary(
5796 MachineInstr &MI, MachineBasicBlock *MBB, unsigned BinOpcode,
5797 unsigned BitSize, bool Invert) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005798 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005799 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005800 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005801 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005802 bool IsSubWord = (BitSize < 32);
5803
5804 // Extract the operands. Base can be a register or a frame index.
5805 // Src2 can be a register or immediate.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005806 unsigned Dest = MI.getOperand(0).getReg();
5807 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5808 int64_t Disp = MI.getOperand(2).getImm();
5809 MachineOperand Src2 = earlyUseOperand(MI.getOperand(3));
5810 unsigned BitShift = (IsSubWord ? MI.getOperand(4).getReg() : 0);
5811 unsigned NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : 0);
5812 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005813 if (IsSubWord)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005814 BitSize = MI.getOperand(6).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005815
5816 // Subword operations use 32-bit registers.
5817 const TargetRegisterClass *RC = (BitSize <= 32 ?
5818 &SystemZ::GR32BitRegClass :
5819 &SystemZ::GR64BitRegClass);
5820 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5821 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5822
5823 // Get the right opcodes for the displacement.
5824 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5825 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5826 assert(LOpcode && CSOpcode && "Displacement out of range");
5827
5828 // Create virtual registers for temporary results.
5829 unsigned OrigVal = MRI.createVirtualRegister(RC);
5830 unsigned OldVal = MRI.createVirtualRegister(RC);
5831 unsigned NewVal = (BinOpcode || IsSubWord ?
5832 MRI.createVirtualRegister(RC) : Src2.getReg());
5833 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5834 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5835
5836 // Insert a basic block for the main loop.
5837 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005838 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005839 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5840
5841 // StartMBB:
5842 // ...
5843 // %OrigVal = L Disp(%Base)
5844 // # fall through to LoopMMB
5845 MBB = StartMBB;
Diana Picus116bbab2017-01-13 09:58:52 +00005846 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005847 MBB->addSuccessor(LoopMBB);
5848
5849 // LoopMBB:
5850 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
5851 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5852 // %RotatedNewVal = OP %RotatedOldVal, %Src2
5853 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5854 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5855 // JNE LoopMBB
5856 // # fall through to DoneMMB
5857 MBB = LoopMBB;
5858 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5859 .addReg(OrigVal).addMBB(StartMBB)
5860 .addReg(Dest).addMBB(LoopMBB);
5861 if (IsSubWord)
5862 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5863 .addReg(OldVal).addReg(BitShift).addImm(0);
5864 if (Invert) {
5865 // Perform the operation normally and then invert every bit of the field.
5866 unsigned Tmp = MRI.createVirtualRegister(RC);
Diana Picus116bbab2017-01-13 09:58:52 +00005867 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp).addReg(RotatedOldVal).add(Src2);
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005868 if (BitSize <= 32)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005869 // XILF with the upper BitSize bits set.
Richard Sandiford652784e2013-09-25 11:11:53 +00005870 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005871 .addReg(Tmp).addImm(-1U << (32 - BitSize));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005872 else {
5873 // Use LCGR and add -1 to the result, which is more compact than
5874 // an XILF, XILH pair.
5875 unsigned Tmp2 = MRI.createVirtualRegister(RC);
5876 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
5877 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
5878 .addReg(Tmp2).addImm(-1);
5879 }
5880 } else if (BinOpcode)
5881 // A simply binary operation.
5882 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
Diana Picus116bbab2017-01-13 09:58:52 +00005883 .addReg(RotatedOldVal)
5884 .add(Src2);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005885 else if (IsSubWord)
5886 // Use RISBG to rotate Src2 into position and use it to replace the
5887 // field in RotatedOldVal.
5888 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
5889 .addReg(RotatedOldVal).addReg(Src2.getReg())
5890 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
5891 if (IsSubWord)
5892 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5893 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5894 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
Diana Picus116bbab2017-01-13 09:58:52 +00005895 .addReg(OldVal)
5896 .addReg(NewVal)
5897 .add(Base)
5898 .addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005899 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5900 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005901 MBB->addSuccessor(LoopMBB);
5902 MBB->addSuccessor(DoneMBB);
5903
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005904 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005905 return DoneMBB;
5906}
5907
5908// Implement EmitInstrWithCustomInserter for pseudo
5909// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
5910// instruction that should be used to compare the current field with the
5911// minimum or maximum value. KeepOldMask is the BRC condition-code mask
5912// for when the current field should be kept. BitSize is the width of
5913// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005914MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadMinMax(
5915 MachineInstr &MI, MachineBasicBlock *MBB, unsigned CompareOpcode,
5916 unsigned KeepOldMask, unsigned BitSize) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005917 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005918 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005919 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005920 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005921 bool IsSubWord = (BitSize < 32);
5922
5923 // Extract the operands. Base can be a register or a frame index.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005924 unsigned Dest = MI.getOperand(0).getReg();
5925 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5926 int64_t Disp = MI.getOperand(2).getImm();
5927 unsigned Src2 = MI.getOperand(3).getReg();
5928 unsigned BitShift = (IsSubWord ? MI.getOperand(4).getReg() : 0);
5929 unsigned NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : 0);
5930 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005931 if (IsSubWord)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005932 BitSize = MI.getOperand(6).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005933
5934 // Subword operations use 32-bit registers.
5935 const TargetRegisterClass *RC = (BitSize <= 32 ?
5936 &SystemZ::GR32BitRegClass :
5937 &SystemZ::GR64BitRegClass);
5938 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5939 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5940
5941 // Get the right opcodes for the displacement.
5942 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5943 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5944 assert(LOpcode && CSOpcode && "Displacement out of range");
5945
5946 // Create virtual registers for temporary results.
5947 unsigned OrigVal = MRI.createVirtualRegister(RC);
5948 unsigned OldVal = MRI.createVirtualRegister(RC);
5949 unsigned NewVal = MRI.createVirtualRegister(RC);
5950 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5951 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
5952 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5953
5954 // Insert 3 basic blocks for the loop.
5955 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005956 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005957 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5958 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
5959 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
5960
5961 // StartMBB:
5962 // ...
5963 // %OrigVal = L Disp(%Base)
5964 // # fall through to LoopMMB
5965 MBB = StartMBB;
Diana Picus116bbab2017-01-13 09:58:52 +00005966 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005967 MBB->addSuccessor(LoopMBB);
5968
5969 // LoopMBB:
5970 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
5971 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5972 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00005973 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005974 MBB = LoopMBB;
5975 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5976 .addReg(OrigVal).addMBB(StartMBB)
5977 .addReg(Dest).addMBB(UpdateMBB);
5978 if (IsSubWord)
5979 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5980 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005981 BuildMI(MBB, DL, TII->get(CompareOpcode))
5982 .addReg(RotatedOldVal).addReg(Src2);
5983 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005984 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005985 MBB->addSuccessor(UpdateMBB);
5986 MBB->addSuccessor(UseAltMBB);
5987
5988 // UseAltMBB:
5989 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
5990 // # fall through to UpdateMMB
5991 MBB = UseAltMBB;
5992 if (IsSubWord)
5993 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
5994 .addReg(RotatedOldVal).addReg(Src2)
5995 .addImm(32).addImm(31 + BitSize).addImm(0);
5996 MBB->addSuccessor(UpdateMBB);
5997
5998 // UpdateMBB:
5999 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
6000 // [ %RotatedAltVal, UseAltMBB ]
6001 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
6002 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
6003 // JNE LoopMBB
6004 // # fall through to DoneMMB
6005 MBB = UpdateMBB;
6006 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
6007 .addReg(RotatedOldVal).addMBB(LoopMBB)
6008 .addReg(RotatedAltVal).addMBB(UseAltMBB);
6009 if (IsSubWord)
6010 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
6011 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
6012 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
Diana Picus116bbab2017-01-13 09:58:52 +00006013 .addReg(OldVal)
6014 .addReg(NewVal)
6015 .add(Base)
6016 .addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00006017 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6018 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006019 MBB->addSuccessor(LoopMBB);
6020 MBB->addSuccessor(DoneMBB);
6021
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006022 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006023 return DoneMBB;
6024}
6025
6026// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
6027// instruction MI.
6028MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006029SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006030 MachineBasicBlock *MBB) const {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00006031
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006032 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00006033 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00006034 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006035 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006036
6037 // Extract the operands. Base can be a register or a frame index.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006038 unsigned Dest = MI.getOperand(0).getReg();
6039 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
6040 int64_t Disp = MI.getOperand(2).getImm();
6041 unsigned OrigCmpVal = MI.getOperand(3).getReg();
6042 unsigned OrigSwapVal = MI.getOperand(4).getReg();
6043 unsigned BitShift = MI.getOperand(5).getReg();
6044 unsigned NegBitShift = MI.getOperand(6).getReg();
6045 int64_t BitSize = MI.getOperand(7).getImm();
6046 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006047
6048 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
6049
6050 // Get the right opcodes for the displacement.
6051 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
6052 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
6053 assert(LOpcode && CSOpcode && "Displacement out of range");
6054
6055 // Create virtual registers for temporary results.
6056 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
6057 unsigned OldVal = MRI.createVirtualRegister(RC);
6058 unsigned CmpVal = MRI.createVirtualRegister(RC);
6059 unsigned SwapVal = MRI.createVirtualRegister(RC);
6060 unsigned StoreVal = MRI.createVirtualRegister(RC);
6061 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
6062 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
6063 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
6064
6065 // Insert 2 basic blocks for the loop.
6066 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00006067 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006068 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
6069 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
6070
6071 // StartMBB:
6072 // ...
6073 // %OrigOldVal = L Disp(%Base)
6074 // # fall through to LoopMMB
6075 MBB = StartMBB;
6076 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
Diana Picus116bbab2017-01-13 09:58:52 +00006077 .add(Base)
6078 .addImm(Disp)
6079 .addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006080 MBB->addSuccessor(LoopMBB);
6081
6082 // LoopMBB:
6083 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
6084 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
6085 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
6086 // %Dest = RLL %OldVal, BitSize(%BitShift)
6087 // ^^ The low BitSize bits contain the field
6088 // of interest.
6089 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
6090 // ^^ Replace the upper 32-BitSize bits of the
6091 // comparison value with those that we loaded,
6092 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00006093 // CR %Dest, %RetryCmpVal
6094 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006095 // # Fall through to SetMBB
6096 MBB = LoopMBB;
6097 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
6098 .addReg(OrigOldVal).addMBB(StartMBB)
6099 .addReg(RetryOldVal).addMBB(SetMBB);
6100 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
6101 .addReg(OrigCmpVal).addMBB(StartMBB)
6102 .addReg(RetryCmpVal).addMBB(SetMBB);
6103 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
6104 .addReg(OrigSwapVal).addMBB(StartMBB)
6105 .addReg(RetrySwapVal).addMBB(SetMBB);
6106 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
6107 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
6108 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
6109 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00006110 BuildMI(MBB, DL, TII->get(SystemZ::CR))
6111 .addReg(Dest).addReg(RetryCmpVal);
6112 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00006113 .addImm(SystemZ::CCMASK_ICMP)
6114 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006115 MBB->addSuccessor(DoneMBB);
6116 MBB->addSuccessor(SetMBB);
6117
6118 // SetMBB:
6119 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
6120 // ^^ Replace the upper 32-BitSize bits of the new
6121 // value with those that we loaded.
6122 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
6123 // ^^ Rotate the new field to its proper position.
6124 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
6125 // JNE LoopMBB
6126 // # fall through to ExitMMB
6127 MBB = SetMBB;
6128 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
6129 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
6130 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
6131 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
6132 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
Diana Picus116bbab2017-01-13 09:58:52 +00006133 .addReg(OldVal)
6134 .addReg(StoreVal)
6135 .add(Base)
6136 .addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00006137 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6138 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006139 MBB->addSuccessor(LoopMBB);
6140 MBB->addSuccessor(DoneMBB);
6141
Ulrich Weiganddf868552017-09-28 22:08:25 +00006142 // If the CC def wasn't dead in the ATOMIC_CMP_SWAPW, mark CC as live-in
6143 // to the block after the loop. At this point, CC may have been defined
6144 // either by the CR in LoopMBB or by the CS in SetMBB.
6145 if (!MI.registerDefIsDead(SystemZ::CC))
6146 DoneMBB->addLiveIn(SystemZ::CC);
6147
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006148 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006149 return DoneMBB;
6150}
6151
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00006152// Emit a move from two GR64s to a GR128.
6153MachineBasicBlock *
6154SystemZTargetLowering::emitPair128(MachineInstr &MI,
6155 MachineBasicBlock *MBB) const {
6156 MachineFunction &MF = *MBB->getParent();
6157 const SystemZInstrInfo *TII =
6158 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
6159 MachineRegisterInfo &MRI = MF.getRegInfo();
6160 DebugLoc DL = MI.getDebugLoc();
6161
6162 unsigned Dest = MI.getOperand(0).getReg();
6163 unsigned Hi = MI.getOperand(1).getReg();
6164 unsigned Lo = MI.getOperand(2).getReg();
6165 unsigned Tmp1 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6166 unsigned Tmp2 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6167
6168 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Tmp1);
6169 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Tmp2)
6170 .addReg(Tmp1).addReg(Hi).addImm(SystemZ::subreg_h64);
6171 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
6172 .addReg(Tmp2).addReg(Lo).addImm(SystemZ::subreg_l64);
6173
6174 MI.eraseFromParent();
6175 return MBB;
6176}
6177
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006178// Emit an extension from a GR64 to a GR128. ClearEven is true
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006179// if the high register of the GR128 value must be cleared or false if
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006180// it's "don't care".
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006181MachineBasicBlock *SystemZTargetLowering::emitExt128(MachineInstr &MI,
6182 MachineBasicBlock *MBB,
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006183 bool ClearEven) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006184 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00006185 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00006186 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006187 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006188 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006189
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006190 unsigned Dest = MI.getOperand(0).getReg();
6191 unsigned Src = MI.getOperand(1).getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006192 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6193
6194 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
6195 if (ClearEven) {
6196 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6197 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
6198
6199 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
6200 .addImm(0);
6201 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
Richard Sandiford87a44362013-09-30 10:28:35 +00006202 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006203 In128 = NewIn128;
6204 }
6205 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006206 .addReg(In128).addReg(Src).addImm(SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006207
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006208 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006209 return MBB;
6210}
6211
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006212MachineBasicBlock *SystemZTargetLowering::emitMemMemWrapper(
6213 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +00006214 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00006215 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00006216 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandiford5e318f02013-08-27 09:54:29 +00006217 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006218 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordd131ff82013-07-08 09:35:23 +00006219
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006220 MachineOperand DestBase = earlyUseOperand(MI.getOperand(0));
6221 uint64_t DestDisp = MI.getOperand(1).getImm();
6222 MachineOperand SrcBase = earlyUseOperand(MI.getOperand(2));
6223 uint64_t SrcDisp = MI.getOperand(3).getImm();
6224 uint64_t Length = MI.getOperand(4).getImm();
Richard Sandifordd131ff82013-07-08 09:35:23 +00006225
Richard Sandifordbe133a82013-08-28 09:01:51 +00006226 // When generating more than one CLC, all but the last will need to
6227 // branch to the end when a difference is found.
6228 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
Craig Topper062a2ba2014-04-25 05:30:21 +00006229 splitBlockAfter(MI, MBB) : nullptr);
Richard Sandifordbe133a82013-08-28 09:01:51 +00006230
Richard Sandiford5e318f02013-08-27 09:54:29 +00006231 // Check for the loop form, in which operand 5 is the trip count.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006232 if (MI.getNumExplicitOperands() > 5) {
Richard Sandiford5e318f02013-08-27 09:54:29 +00006233 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
6234
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006235 uint64_t StartCountReg = MI.getOperand(5).getReg();
Richard Sandiford5e318f02013-08-27 09:54:29 +00006236 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
6237 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
6238 forceReg(MI, DestBase, TII));
6239
6240 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
6241 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
6242 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
6243 MRI.createVirtualRegister(RC));
6244 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
6245 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
6246 MRI.createVirtualRegister(RC));
6247
6248 RC = &SystemZ::GR64BitRegClass;
6249 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
6250 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
6251
6252 MachineBasicBlock *StartMBB = MBB;
6253 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
6254 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00006255 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006256
6257 // StartMBB:
6258 // # fall through to LoopMMB
6259 MBB->addSuccessor(LoopMBB);
6260
6261 // LoopMBB:
6262 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00006263 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00006264 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00006265 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00006266 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00006267 // [ %NextCountReg, NextMBB ]
6268 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00006269 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00006270 // ( JLH EndMBB )
6271 //
6272 // The prefetch is used only for MVC. The JLH is used only for CLC.
6273 MBB = LoopMBB;
6274
6275 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
6276 .addReg(StartDestReg).addMBB(StartMBB)
6277 .addReg(NextDestReg).addMBB(NextMBB);
6278 if (!HaveSingleBase)
6279 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
6280 .addReg(StartSrcReg).addMBB(StartMBB)
6281 .addReg(NextSrcReg).addMBB(NextMBB);
6282 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
6283 .addReg(StartCountReg).addMBB(StartMBB)
6284 .addReg(NextCountReg).addMBB(NextMBB);
6285 if (Opcode == SystemZ::MVC)
6286 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
6287 .addImm(SystemZ::PFD_WRITE)
6288 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
6289 BuildMI(MBB, DL, TII->get(Opcode))
6290 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
6291 .addReg(ThisSrcReg).addImm(SrcDisp);
6292 if (EndMBB) {
6293 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6294 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
6295 .addMBB(EndMBB);
6296 MBB->addSuccessor(EndMBB);
6297 MBB->addSuccessor(NextMBB);
6298 }
6299
6300 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00006301 // %NextDestReg = LA 256(%ThisDestReg)
6302 // %NextSrcReg = LA 256(%ThisSrcReg)
6303 // %NextCountReg = AGHI %ThisCountReg, -1
6304 // CGHI %NextCountReg, 0
6305 // JLH LoopMBB
6306 // # fall through to DoneMMB
6307 //
6308 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00006309 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00006310
Richard Sandiford5e318f02013-08-27 09:54:29 +00006311 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
6312 .addReg(ThisDestReg).addImm(256).addReg(0);
6313 if (!HaveSingleBase)
6314 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
6315 .addReg(ThisSrcReg).addImm(256).addReg(0);
6316 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
6317 .addReg(ThisCountReg).addImm(-1);
6318 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
6319 .addReg(NextCountReg).addImm(0);
6320 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6321 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
6322 .addMBB(LoopMBB);
6323 MBB->addSuccessor(LoopMBB);
6324 MBB->addSuccessor(DoneMBB);
6325
6326 DestBase = MachineOperand::CreateReg(NextDestReg, false);
6327 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
6328 Length &= 255;
6329 MBB = DoneMBB;
6330 }
6331 // Handle any remaining bytes with straight-line code.
6332 while (Length > 0) {
6333 uint64_t ThisLength = std::min(Length, uint64_t(256));
6334 // The previous iteration might have created out-of-range displacements.
6335 // Apply them using LAY if so.
6336 if (!isUInt<12>(DestDisp)) {
6337 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006338 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg)
Diana Picus116bbab2017-01-13 09:58:52 +00006339 .add(DestBase)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006340 .addImm(DestDisp)
6341 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006342 DestBase = MachineOperand::CreateReg(Reg, false);
6343 DestDisp = 0;
6344 }
6345 if (!isUInt<12>(SrcDisp)) {
6346 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006347 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg)
Diana Picus116bbab2017-01-13 09:58:52 +00006348 .add(SrcBase)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006349 .addImm(SrcDisp)
6350 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006351 SrcBase = MachineOperand::CreateReg(Reg, false);
6352 SrcDisp = 0;
6353 }
6354 BuildMI(*MBB, MI, DL, TII->get(Opcode))
Diana Picus116bbab2017-01-13 09:58:52 +00006355 .add(DestBase)
6356 .addImm(DestDisp)
6357 .addImm(ThisLength)
6358 .add(SrcBase)
Jonas Paulssonae8d22c2017-06-07 14:08:34 +00006359 .addImm(SrcDisp)
6360 ->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Richard Sandiford5e318f02013-08-27 09:54:29 +00006361 DestDisp += ThisLength;
6362 SrcDisp += ThisLength;
6363 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00006364 // If there's another CLC to go, branch to the end if a difference
6365 // was found.
6366 if (EndMBB && Length > 0) {
6367 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
6368 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6369 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
6370 .addMBB(EndMBB);
6371 MBB->addSuccessor(EndMBB);
6372 MBB->addSuccessor(NextMBB);
6373 MBB = NextMBB;
6374 }
6375 }
6376 if (EndMBB) {
6377 MBB->addSuccessor(EndMBB);
6378 MBB = EndMBB;
6379 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006380 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00006381
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006382 MI.eraseFromParent();
Richard Sandifordd131ff82013-07-08 09:35:23 +00006383 return MBB;
6384}
6385
Richard Sandifordca232712013-08-16 11:21:54 +00006386// Decompose string pseudo-instruction MI into a loop that continually performs
6387// Opcode until CC != 3.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006388MachineBasicBlock *SystemZTargetLowering::emitStringWrapper(
6389 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Richard Sandifordca232712013-08-16 11:21:54 +00006390 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00006391 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00006392 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordca232712013-08-16 11:21:54 +00006393 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006394 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordca232712013-08-16 11:21:54 +00006395
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006396 uint64_t End1Reg = MI.getOperand(0).getReg();
6397 uint64_t Start1Reg = MI.getOperand(1).getReg();
6398 uint64_t Start2Reg = MI.getOperand(2).getReg();
6399 uint64_t CharReg = MI.getOperand(3).getReg();
Richard Sandifordca232712013-08-16 11:21:54 +00006400
6401 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
6402 uint64_t This1Reg = MRI.createVirtualRegister(RC);
6403 uint64_t This2Reg = MRI.createVirtualRegister(RC);
6404 uint64_t End2Reg = MRI.createVirtualRegister(RC);
6405
6406 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00006407 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00006408 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
6409
6410 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00006411 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00006412 MBB->addSuccessor(LoopMBB);
6413
6414 // LoopMBB:
6415 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
6416 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford7789b082013-09-30 08:48:38 +00006417 // R0L = %CharReg
6418 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
Richard Sandifordca232712013-08-16 11:21:54 +00006419 // JO LoopMBB
6420 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00006421 //
Richard Sandiford7789b082013-09-30 08:48:38 +00006422 // The load of R0L can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00006423 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00006424
6425 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
6426 .addReg(Start1Reg).addMBB(StartMBB)
6427 .addReg(End1Reg).addMBB(LoopMBB);
6428 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
6429 .addReg(Start2Reg).addMBB(StartMBB)
6430 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford7789b082013-09-30 08:48:38 +00006431 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00006432 BuildMI(MBB, DL, TII->get(Opcode))
6433 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
6434 .addReg(This1Reg).addReg(This2Reg);
6435 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6436 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
6437 MBB->addSuccessor(LoopMBB);
6438 MBB->addSuccessor(DoneMBB);
6439
6440 DoneMBB->addLiveIn(SystemZ::CC);
6441
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006442 MI.eraseFromParent();
Richard Sandifordca232712013-08-16 11:21:54 +00006443 return DoneMBB;
6444}
6445
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006446// Update TBEGIN instruction with final opcode and register clobbers.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006447MachineBasicBlock *SystemZTargetLowering::emitTransactionBegin(
6448 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode,
6449 bool NoFloat) const {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006450 MachineFunction &MF = *MBB->getParent();
6451 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
6452 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
6453
6454 // Update opcode.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006455 MI.setDesc(TII->get(Opcode));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006456
6457 // We cannot handle a TBEGIN that clobbers the stack or frame pointer.
6458 // Make sure to add the corresponding GRSM bits if they are missing.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006459 uint64_t Control = MI.getOperand(2).getImm();
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006460 static const unsigned GPRControlBit[16] = {
6461 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000,
6462 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100
6463 };
6464 Control |= GPRControlBit[15];
6465 if (TFI->hasFP(MF))
6466 Control |= GPRControlBit[11];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006467 MI.getOperand(2).setImm(Control);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006468
6469 // Add GPR clobbers.
6470 for (int I = 0; I < 16; I++) {
6471 if ((Control & GPRControlBit[I]) == 0) {
6472 unsigned Reg = SystemZMC::GR64Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006473 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006474 }
6475 }
6476
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006477 // Add FPR/VR clobbers.
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006478 if (!NoFloat && (Control & 4) != 0) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006479 if (Subtarget.hasVector()) {
6480 for (int I = 0; I < 32; I++) {
6481 unsigned Reg = SystemZMC::VR128Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006482 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006483 }
6484 } else {
6485 for (int I = 0; I < 16; I++) {
6486 unsigned Reg = SystemZMC::FP64Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006487 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006488 }
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006489 }
6490 }
6491
6492 return MBB;
6493}
6494
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006495MachineBasicBlock *SystemZTargetLowering::emitLoadAndTestCmp0(
6496 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006497 MachineFunction &MF = *MBB->getParent();
6498 MachineRegisterInfo *MRI = &MF.getRegInfo();
6499 const SystemZInstrInfo *TII =
6500 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006501 DebugLoc DL = MI.getDebugLoc();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006502
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006503 unsigned SrcReg = MI.getOperand(0).getReg();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006504
6505 // Create new virtual register of the same class as source.
6506 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
6507 unsigned DstReg = MRI->createVirtualRegister(RC);
6508
6509 // Replace pseudo with a normal load-and-test that models the def as
6510 // well.
6511 BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg)
6512 .addReg(SrcReg);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006513 MI.eraseFromParent();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006514
6515 return MBB;
6516}
6517
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006518MachineBasicBlock *SystemZTargetLowering::EmitInstrWithCustomInserter(
6519 MachineInstr &MI, MachineBasicBlock *MBB) const {
6520 switch (MI.getOpcode()) {
Richard Sandiford7c5c0ea2013-10-01 13:10:16 +00006521 case SystemZ::Select32Mux:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006522 return emitSelect(MI, MBB,
6523 Subtarget.hasLoadStoreOnCond2()? SystemZ::LOCRMux : 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006524 case SystemZ::Select32:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006525 return emitSelect(MI, MBB, SystemZ::LOCR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006526 case SystemZ::Select64:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006527 return emitSelect(MI, MBB, SystemZ::LOCGR);
6528 case SystemZ::SelectF32:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006529 case SystemZ::SelectF64:
6530 case SystemZ::SelectF128:
Ulrich Weigandf2968d52017-07-17 17:44:20 +00006531 case SystemZ::SelectVR128:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006532 return emitSelect(MI, MBB, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006533
Richard Sandiford2896d042013-10-01 14:33:55 +00006534 case SystemZ::CondStore8Mux:
6535 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
6536 case SystemZ::CondStore8MuxInv:
6537 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
6538 case SystemZ::CondStore16Mux:
6539 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
6540 case SystemZ::CondStore16MuxInv:
6541 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
Ulrich Weigand524f2762016-11-28 13:34:08 +00006542 case SystemZ::CondStore32Mux:
6543 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, false);
6544 case SystemZ::CondStore32MuxInv:
6545 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006546 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006547 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006548 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006549 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006550 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006551 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006552 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006553 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006554 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006555 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006556 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006557 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006558 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006559 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006560 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006561 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006562 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006563 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006564 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006565 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006566 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006567 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006568 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006569 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006570
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00006571 case SystemZ::PAIR128:
6572 return emitPair128(MI, MBB);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006573 case SystemZ::AEXT128:
6574 return emitExt128(MI, MBB, false);
6575 case SystemZ::ZEXT128:
6576 return emitExt128(MI, MBB, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006577
6578 case SystemZ::ATOMIC_SWAPW:
6579 return emitAtomicLoadBinary(MI, MBB, 0, 0);
6580 case SystemZ::ATOMIC_SWAP_32:
6581 return emitAtomicLoadBinary(MI, MBB, 0, 32);
6582 case SystemZ::ATOMIC_SWAP_64:
6583 return emitAtomicLoadBinary(MI, MBB, 0, 64);
6584
6585 case SystemZ::ATOMIC_LOADW_AR:
6586 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
6587 case SystemZ::ATOMIC_LOADW_AFI:
6588 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
6589 case SystemZ::ATOMIC_LOAD_AR:
6590 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
6591 case SystemZ::ATOMIC_LOAD_AHI:
6592 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
6593 case SystemZ::ATOMIC_LOAD_AFI:
6594 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
6595 case SystemZ::ATOMIC_LOAD_AGR:
6596 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
6597 case SystemZ::ATOMIC_LOAD_AGHI:
6598 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
6599 case SystemZ::ATOMIC_LOAD_AGFI:
6600 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
6601
6602 case SystemZ::ATOMIC_LOADW_SR:
6603 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
6604 case SystemZ::ATOMIC_LOAD_SR:
6605 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
6606 case SystemZ::ATOMIC_LOAD_SGR:
6607 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
6608
6609 case SystemZ::ATOMIC_LOADW_NR:
6610 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
6611 case SystemZ::ATOMIC_LOADW_NILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00006612 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006613 case SystemZ::ATOMIC_LOAD_NR:
6614 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006615 case SystemZ::ATOMIC_LOAD_NILL:
6616 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
6617 case SystemZ::ATOMIC_LOAD_NILH:
6618 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
6619 case SystemZ::ATOMIC_LOAD_NILF:
6620 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006621 case SystemZ::ATOMIC_LOAD_NGR:
6622 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006623 case SystemZ::ATOMIC_LOAD_NILL64:
6624 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
6625 case SystemZ::ATOMIC_LOAD_NILH64:
6626 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00006627 case SystemZ::ATOMIC_LOAD_NIHL64:
6628 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
6629 case SystemZ::ATOMIC_LOAD_NIHH64:
6630 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006631 case SystemZ::ATOMIC_LOAD_NILF64:
6632 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00006633 case SystemZ::ATOMIC_LOAD_NIHF64:
6634 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006635
6636 case SystemZ::ATOMIC_LOADW_OR:
6637 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
6638 case SystemZ::ATOMIC_LOADW_OILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00006639 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006640 case SystemZ::ATOMIC_LOAD_OR:
6641 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006642 case SystemZ::ATOMIC_LOAD_OILL:
6643 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
6644 case SystemZ::ATOMIC_LOAD_OILH:
6645 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
6646 case SystemZ::ATOMIC_LOAD_OILF:
6647 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006648 case SystemZ::ATOMIC_LOAD_OGR:
6649 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006650 case SystemZ::ATOMIC_LOAD_OILL64:
6651 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
6652 case SystemZ::ATOMIC_LOAD_OILH64:
6653 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00006654 case SystemZ::ATOMIC_LOAD_OIHL64:
6655 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
6656 case SystemZ::ATOMIC_LOAD_OIHH64:
6657 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006658 case SystemZ::ATOMIC_LOAD_OILF64:
6659 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00006660 case SystemZ::ATOMIC_LOAD_OIHF64:
6661 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006662
6663 case SystemZ::ATOMIC_LOADW_XR:
6664 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
6665 case SystemZ::ATOMIC_LOADW_XILF:
Richard Sandiford652784e2013-09-25 11:11:53 +00006666 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006667 case SystemZ::ATOMIC_LOAD_XR:
6668 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006669 case SystemZ::ATOMIC_LOAD_XILF:
6670 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006671 case SystemZ::ATOMIC_LOAD_XGR:
6672 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006673 case SystemZ::ATOMIC_LOAD_XILF64:
6674 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
Richard Sandiford5718dac2013-10-01 14:08:44 +00006675 case SystemZ::ATOMIC_LOAD_XIHF64:
6676 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006677
6678 case SystemZ::ATOMIC_LOADW_NRi:
6679 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
6680 case SystemZ::ATOMIC_LOADW_NILHi:
Richard Sandiford652784e2013-09-25 11:11:53 +00006681 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006682 case SystemZ::ATOMIC_LOAD_NRi:
6683 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006684 case SystemZ::ATOMIC_LOAD_NILLi:
6685 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
6686 case SystemZ::ATOMIC_LOAD_NILHi:
6687 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
6688 case SystemZ::ATOMIC_LOAD_NILFi:
6689 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006690 case SystemZ::ATOMIC_LOAD_NGRi:
6691 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006692 case SystemZ::ATOMIC_LOAD_NILL64i:
6693 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
6694 case SystemZ::ATOMIC_LOAD_NILH64i:
6695 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00006696 case SystemZ::ATOMIC_LOAD_NIHL64i:
6697 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
6698 case SystemZ::ATOMIC_LOAD_NIHH64i:
6699 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006700 case SystemZ::ATOMIC_LOAD_NILF64i:
6701 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00006702 case SystemZ::ATOMIC_LOAD_NIHF64i:
6703 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006704
6705 case SystemZ::ATOMIC_LOADW_MIN:
6706 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6707 SystemZ::CCMASK_CMP_LE, 0);
6708 case SystemZ::ATOMIC_LOAD_MIN_32:
6709 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6710 SystemZ::CCMASK_CMP_LE, 32);
6711 case SystemZ::ATOMIC_LOAD_MIN_64:
6712 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
6713 SystemZ::CCMASK_CMP_LE, 64);
6714
6715 case SystemZ::ATOMIC_LOADW_MAX:
6716 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6717 SystemZ::CCMASK_CMP_GE, 0);
6718 case SystemZ::ATOMIC_LOAD_MAX_32:
6719 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6720 SystemZ::CCMASK_CMP_GE, 32);
6721 case SystemZ::ATOMIC_LOAD_MAX_64:
6722 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
6723 SystemZ::CCMASK_CMP_GE, 64);
6724
6725 case SystemZ::ATOMIC_LOADW_UMIN:
6726 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6727 SystemZ::CCMASK_CMP_LE, 0);
6728 case SystemZ::ATOMIC_LOAD_UMIN_32:
6729 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6730 SystemZ::CCMASK_CMP_LE, 32);
6731 case SystemZ::ATOMIC_LOAD_UMIN_64:
6732 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6733 SystemZ::CCMASK_CMP_LE, 64);
6734
6735 case SystemZ::ATOMIC_LOADW_UMAX:
6736 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6737 SystemZ::CCMASK_CMP_GE, 0);
6738 case SystemZ::ATOMIC_LOAD_UMAX_32:
6739 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6740 SystemZ::CCMASK_CMP_GE, 32);
6741 case SystemZ::ATOMIC_LOAD_UMAX_64:
6742 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6743 SystemZ::CCMASK_CMP_GE, 64);
6744
6745 case SystemZ::ATOMIC_CMP_SWAPW:
6746 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006747 case SystemZ::MVCSequence:
6748 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006749 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00006750 case SystemZ::NCSequence:
6751 case SystemZ::NCLoop:
6752 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
6753 case SystemZ::OCSequence:
6754 case SystemZ::OCLoop:
6755 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
6756 case SystemZ::XCSequence:
6757 case SystemZ::XCLoop:
6758 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006759 case SystemZ::CLCSequence:
6760 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006761 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00006762 case SystemZ::CLSTLoop:
6763 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00006764 case SystemZ::MVSTLoop:
6765 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00006766 case SystemZ::SRSTLoop:
6767 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006768 case SystemZ::TBEGIN:
6769 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false);
6770 case SystemZ::TBEGIN_nofloat:
6771 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true);
6772 case SystemZ::TBEGINC:
6773 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true);
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006774 case SystemZ::LTEBRCompare_VecPseudo:
6775 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR);
6776 case SystemZ::LTDBRCompare_VecPseudo:
6777 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR);
6778 case SystemZ::LTXBRCompare_VecPseudo:
6779 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR);
6780
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006781 default:
6782 llvm_unreachable("Unexpected instr type to insert");
6783 }
6784}
Jonas Paulsson11d251c2017-05-10 13:03:25 +00006785
6786// This is only used by the isel schedulers, and is needed only to prevent
6787// compiler from crashing when list-ilp is used.
6788const TargetRegisterClass *
6789SystemZTargetLowering::getRepRegClassFor(MVT VT) const {
6790 if (VT == MVT::Untyped)
6791 return &SystemZ::ADDR128BitRegClass;
6792 return TargetLowering::getRepRegClassFor(VT);
6793}