blob: 2d0a06af18ae5f9cdff838feb30a3451ec230f76 [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- SystemZISelLowering.cpp - SystemZ DAG lowering implementation -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SystemZTargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
Ulrich Weigand5f613df2013-05-06 16:15:19 +000014#include "SystemZISelLowering.h"
15#include "SystemZCallingConv.h"
16#include "SystemZConstantPoolValue.h"
17#include "SystemZMachineFunctionInfo.h"
18#include "SystemZTargetMachine.h"
19#include "llvm/CodeGen/CallingConvLower.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
22#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Jonas Paulsson7a794222016-08-17 13:24:19 +000023#include "llvm/Support/CommandLine.h"
Ulrich Weigand57c85f52015-04-01 12:51:43 +000024#include "llvm/IR/Intrinsics.h"
Will Dietz981af002013-10-12 00:55:57 +000025#include <cctype>
26
Ulrich Weigand5f613df2013-05-06 16:15:19 +000027using namespace llvm;
28
Chandler Carruth84e68b22014-04-22 02:41:26 +000029#define DEBUG_TYPE "systemz-lower"
30
Richard Sandifordf722a8e302013-10-16 11:10:55 +000031namespace {
32// Represents a sequence for extracting a 0/1 value from an IPM result:
33// (((X ^ XORValue) + AddValue) >> Bit)
34struct IPMConversion {
35 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
36 : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
37
38 int64_t XORValue;
39 int64_t AddValue;
40 unsigned Bit;
41};
Richard Sandifordd420f732013-12-13 15:28:45 +000042
43// Represents information about a comparison.
44struct Comparison {
45 Comparison(SDValue Op0In, SDValue Op1In)
46 : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {}
47
48 // The operands to the comparison.
49 SDValue Op0, Op1;
50
51 // The opcode that should be used to compare Op0 and Op1.
52 unsigned Opcode;
53
54 // A SystemZICMP value. Only used for integer comparisons.
55 unsigned ICmpType;
56
57 // The mask of CC values that Opcode can produce.
58 unsigned CCValid;
59
60 // The mask of CC values for which the original condition is true.
61 unsigned CCMask;
62};
Richard Sandifordc2312692014-03-06 10:38:30 +000063} // end anonymous namespace
Richard Sandifordf722a8e302013-10-16 11:10:55 +000064
Ulrich Weigand5f613df2013-05-06 16:15:19 +000065// Classify VT as either 32 or 64 bit.
66static bool is32Bit(EVT VT) {
67 switch (VT.getSimpleVT().SimpleTy) {
68 case MVT::i32:
69 return true;
70 case MVT::i64:
71 return false;
72 default:
73 llvm_unreachable("Unsupported type");
74 }
75}
76
77// Return a version of MachineOperand that can be safely used before the
78// final use.
79static MachineOperand earlyUseOperand(MachineOperand Op) {
80 if (Op.isReg())
81 Op.setIsKill(false);
82 return Op;
83}
84
Mehdi Amini44ede332015-07-09 02:09:04 +000085SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM,
Eric Christophera6734172015-01-31 00:06:45 +000086 const SystemZSubtarget &STI)
Mehdi Amini44ede332015-07-09 02:09:04 +000087 : TargetLowering(TM), Subtarget(STI) {
Mehdi Amini26d48132015-07-24 16:04:22 +000088 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
Ulrich Weigand5f613df2013-05-06 16:15:19 +000089
90 // Set up the register classes.
Richard Sandiford0755c932013-10-01 11:26:28 +000091 if (Subtarget.hasHighWord())
92 addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass);
93 else
94 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
Ulrich Weigand49506d72015-05-05 19:28:34 +000095 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
96 if (Subtarget.hasVector()) {
97 addRegisterClass(MVT::f32, &SystemZ::VR32BitRegClass);
98 addRegisterClass(MVT::f64, &SystemZ::VR64BitRegClass);
99 } else {
100 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
101 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
102 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000103 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
104
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000105 if (Subtarget.hasVector()) {
106 addRegisterClass(MVT::v16i8, &SystemZ::VR128BitRegClass);
107 addRegisterClass(MVT::v8i16, &SystemZ::VR128BitRegClass);
108 addRegisterClass(MVT::v4i32, &SystemZ::VR128BitRegClass);
109 addRegisterClass(MVT::v2i64, &SystemZ::VR128BitRegClass);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000110 addRegisterClass(MVT::v4f32, &SystemZ::VR128BitRegClass);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000111 addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000112 }
113
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000114 // Compute derived properties from the register classes
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000115 computeRegisterProperties(Subtarget.getRegisterInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000116
117 // Set up special registers.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000118 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
119
120 // TODO: It may be better to default to latency-oriented scheduling, however
121 // LLVM's current latency-oriented scheduler can't handle physreg definitions
Richard Sandiford14a44492013-05-22 13:38:45 +0000122 // such as SystemZ has with CC, so set this to the register-pressure
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000123 // scheduler, because it can.
124 setSchedulingPreference(Sched::RegPressure);
125
126 setBooleanContents(ZeroOrOneBooleanContent);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000127 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000128
129 // Instructions are strings of 2-byte aligned 2-byte values.
130 setMinFunctionAlignment(2);
131
132 // Handle operations that are handled in a similar way for all types.
133 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
134 I <= MVT::LAST_FP_VALUETYPE;
135 ++I) {
136 MVT VT = MVT::SimpleValueType(I);
137 if (isTypeLegal(VT)) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000138 // Lower SET_CC into an IPM-based sequence.
139 setOperationAction(ISD::SETCC, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000140
141 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
142 setOperationAction(ISD::SELECT, VT, Expand);
143
144 // Lower SELECT_CC and BR_CC into separate comparisons and branches.
145 setOperationAction(ISD::SELECT_CC, VT, Custom);
146 setOperationAction(ISD::BR_CC, VT, Custom);
147 }
148 }
149
150 // Expand jump table branches as address arithmetic followed by an
151 // indirect jump.
152 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
153
154 // Expand BRCOND into a BR_CC (see above).
155 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
156
157 // Handle integer types.
158 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
159 I <= MVT::LAST_INTEGER_VALUETYPE;
160 ++I) {
161 MVT VT = MVT::SimpleValueType(I);
162 if (isTypeLegal(VT)) {
163 // Expand individual DIV and REMs into DIVREMs.
164 setOperationAction(ISD::SDIV, VT, Expand);
165 setOperationAction(ISD::UDIV, VT, Expand);
166 setOperationAction(ISD::SREM, VT, Expand);
167 setOperationAction(ISD::UREM, VT, Expand);
168 setOperationAction(ISD::SDIVREM, VT, Custom);
169 setOperationAction(ISD::UDIVREM, VT, Custom);
170
Richard Sandifordbef3d7a2013-12-10 10:49:34 +0000171 // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and
172 // stores, putting a serialization instruction after the stores.
173 setOperationAction(ISD::ATOMIC_LOAD, VT, Custom);
174 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000175
Richard Sandiford41350a52013-12-24 15:18:04 +0000176 // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are
177 // available, or if the operand is constant.
178 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
179
Ulrich Weigandb4012182015-03-31 12:56:33 +0000180 // Use POPCNT on z196 and above.
181 if (Subtarget.hasPopulationCount())
182 setOperationAction(ISD::CTPOP, VT, Custom);
183 else
184 setOperationAction(ISD::CTPOP, VT, Expand);
185
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000186 // No special instructions for these.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000187 setOperationAction(ISD::CTTZ, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000188 setOperationAction(ISD::ROTR, VT, Expand);
189
Richard Sandiford7d86e472013-08-21 09:34:56 +0000190 // Use *MUL_LOHI where possible instead of MULH*.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000191 setOperationAction(ISD::MULHS, VT, Expand);
192 setOperationAction(ISD::MULHU, VT, Expand);
Richard Sandiford7d86e472013-08-21 09:34:56 +0000193 setOperationAction(ISD::SMUL_LOHI, VT, Custom);
194 setOperationAction(ISD::UMUL_LOHI, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000195
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000196 // Only z196 and above have native support for conversions to unsigned.
197 if (!Subtarget.hasFPExtension())
198 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000199 }
200 }
201
202 // Type legalization will convert 8- and 16-bit atomic operations into
203 // forms that operate on i32s (but still keeping the original memory VT).
204 // Lower them into full i32 operations.
205 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom);
206 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom);
207 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
208 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom);
209 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom);
210 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom);
211 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
212 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom);
213 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom);
214 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
215 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
216 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
217
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +0000218 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
219
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000220 // Traps are legal, as we will convert them to "j .+2".
221 setOperationAction(ISD::TRAP, MVT::Other, Legal);
222
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000223 // z10 has instructions for signed but not unsigned FP conversion.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000224 // Handle unsigned 32-bit types as signed 64-bit types.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000225 if (!Subtarget.hasFPExtension()) {
226 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
227 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
228 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000229
230 // We have native support for a 64-bit CTLZ, via FLOGR.
231 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
232 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
233
234 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
235 setOperationAction(ISD::OR, MVT::i64, Custom);
236
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000237 // FIXME: Can we support these natively?
238 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
239 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
240 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
241
242 // We have native instructions for i8, i16 and i32 extensions, but not i1.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000243 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000244 for (MVT VT : MVT::integer_valuetypes()) {
245 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
246 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
247 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
248 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000249
250 // Handle the various types of symbolic address.
251 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
252 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
253 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
254 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
255 setOperationAction(ISD::JumpTable, PtrVT, Custom);
256
257 // We need to handle dynamic allocations specially because of the
258 // 160-byte area at the bottom of the stack.
259 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +0000260 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, PtrVT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000261
262 // Use custom expanders so that we can force the function to use
263 // a frame pointer.
264 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
265 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
266
Richard Sandiford03481332013-08-23 11:36:42 +0000267 // Handle prefetches with PFD or PFDRL.
268 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
269
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000270 for (MVT VT : MVT::vector_valuetypes()) {
271 // Assume by default that all vector operations need to be expanded.
272 for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode)
273 if (getOperationAction(Opcode, VT) == Legal)
274 setOperationAction(Opcode, VT, Expand);
275
276 // Likewise all truncating stores and extending loads.
277 for (MVT InnerVT : MVT::vector_valuetypes()) {
278 setTruncStoreAction(VT, InnerVT, Expand);
279 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
280 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
281 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
282 }
283
284 if (isTypeLegal(VT)) {
285 // These operations are legal for anything that can be stored in a
286 // vector register, even if there is no native support for the format
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000287 // as such. In particular, we can do these for v4f32 even though there
288 // are no specific instructions for that format.
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000289 setOperationAction(ISD::LOAD, VT, Legal);
290 setOperationAction(ISD::STORE, VT, Legal);
291 setOperationAction(ISD::VSELECT, VT, Legal);
292 setOperationAction(ISD::BITCAST, VT, Legal);
293 setOperationAction(ISD::UNDEF, VT, Legal);
294
295 // Likewise, except that we need to replace the nodes with something
296 // more specific.
297 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
298 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
299 }
300 }
301
302 // Handle integer vector types.
303 for (MVT VT : MVT::integer_vector_valuetypes()) {
304 if (isTypeLegal(VT)) {
305 // These operations have direct equivalents.
306 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal);
307 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal);
308 setOperationAction(ISD::ADD, VT, Legal);
309 setOperationAction(ISD::SUB, VT, Legal);
310 if (VT != MVT::v2i64)
311 setOperationAction(ISD::MUL, VT, Legal);
312 setOperationAction(ISD::AND, VT, Legal);
313 setOperationAction(ISD::OR, VT, Legal);
314 setOperationAction(ISD::XOR, VT, Legal);
315 setOperationAction(ISD::CTPOP, VT, Custom);
316 setOperationAction(ISD::CTTZ, VT, Legal);
317 setOperationAction(ISD::CTLZ, VT, Legal);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000318
319 // Convert a GPR scalar to a vector by inserting it into element 0.
320 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
321
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000322 // Use a series of unpacks for extensions.
323 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Custom);
324 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Custom);
325
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000326 // Detect shifts by a scalar amount and convert them into
327 // V*_BY_SCALAR.
328 setOperationAction(ISD::SHL, VT, Custom);
329 setOperationAction(ISD::SRA, VT, Custom);
330 setOperationAction(ISD::SRL, VT, Custom);
331
332 // At present ROTL isn't matched by DAGCombiner. ROTR should be
333 // converted into ROTL.
334 setOperationAction(ISD::ROTL, VT, Expand);
335 setOperationAction(ISD::ROTR, VT, Expand);
336
337 // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands
338 // and inverting the result as necessary.
339 setOperationAction(ISD::SETCC, VT, Custom);
340 }
341 }
342
Ulrich Weigandcd808232015-05-05 19:26:48 +0000343 if (Subtarget.hasVector()) {
344 // There should be no need to check for float types other than v2f64
345 // since <2 x f32> isn't a legal type.
346 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
347 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
348 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
349 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
350 }
351
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000352 // Handle floating-point types.
353 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
354 I <= MVT::LAST_FP_VALUETYPE;
355 ++I) {
356 MVT VT = MVT::SimpleValueType(I);
357 if (isTypeLegal(VT)) {
358 // We can use FI for FRINT.
359 setOperationAction(ISD::FRINT, VT, Legal);
360
Richard Sandifordaf5f66a2013-08-21 09:04:20 +0000361 // We can use the extended form of FI for other rounding operations.
362 if (Subtarget.hasFPExtension()) {
363 setOperationAction(ISD::FNEARBYINT, VT, Legal);
364 setOperationAction(ISD::FFLOOR, VT, Legal);
365 setOperationAction(ISD::FCEIL, VT, Legal);
366 setOperationAction(ISD::FTRUNC, VT, Legal);
367 setOperationAction(ISD::FROUND, VT, Legal);
368 }
369
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000370 // No special instructions for these.
371 setOperationAction(ISD::FSIN, VT, Expand);
372 setOperationAction(ISD::FCOS, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000373 setOperationAction(ISD::FSINCOS, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000374 setOperationAction(ISD::FREM, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000375 setOperationAction(ISD::FPOW, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000376 }
377 }
378
Ulrich Weigandcd808232015-05-05 19:26:48 +0000379 // Handle floating-point vector types.
380 if (Subtarget.hasVector()) {
381 // Scalar-to-vector conversion is just a subreg.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000382 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000383 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
384
385 // Some insertions and extractions can be done directly but others
386 // need to go via integers.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000387 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000388 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000389 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000390 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
391
392 // These operations have direct equivalents.
393 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
394 setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
395 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
396 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
397 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
398 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
399 setOperationAction(ISD::FABS, MVT::v2f64, Legal);
400 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
401 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
402 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
403 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
404 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
405 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
406 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
407 }
408
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000409 // We have fused multiply-addition for f32 and f64 but not f128.
410 setOperationAction(ISD::FMA, MVT::f32, Legal);
411 setOperationAction(ISD::FMA, MVT::f64, Legal);
412 setOperationAction(ISD::FMA, MVT::f128, Expand);
413
414 // Needed so that we don't try to implement f128 constant loads using
415 // a load-and-extend of a f80 constant (in cases where the constant
416 // would fit in an f80).
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000417 for (MVT VT : MVT::fp_valuetypes())
418 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000419
420 // Floating-point truncation and stores need to be done separately.
421 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
422 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
423 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
424
425 // We have 64-bit FPR<->GPR moves, but need special handling for
426 // 32-bit forms.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000427 if (!Subtarget.hasVector()) {
428 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
429 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
430 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000431
432 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
433 // structure, but VAEND is a no-op.
434 setOperationAction(ISD::VASTART, MVT::Other, Custom);
435 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
436 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Richard Sandifordd131ff82013-07-08 09:35:23 +0000437
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000438 // Codes for which we want to perform some z-specific combinations.
439 setTargetDAGCombine(ISD::SIGN_EXTEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000440 setTargetDAGCombine(ISD::STORE);
441 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000442 setTargetDAGCombine(ISD::FP_ROUND);
Bryan Chan28b759c2016-05-16 20:32:22 +0000443 setTargetDAGCombine(ISD::BSWAP);
Elliot Colpbc2cfc22016-07-06 18:13:11 +0000444 setTargetDAGCombine(ISD::SHL);
445 setTargetDAGCombine(ISD::SRA);
446 setTargetDAGCombine(ISD::SRL);
447 setTargetDAGCombine(ISD::ROTL);
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000448
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000449 // Handle intrinsics.
450 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Ulrich Weigandc1708b22015-05-05 19:31:09 +0000451 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000452
Richard Sandifordd131ff82013-07-08 09:35:23 +0000453 // We want to use MVC in preference to even a single load/store pair.
454 MaxStoresPerMemcpy = 0;
455 MaxStoresPerMemcpyOptSize = 0;
Richard Sandiford47660c12013-07-09 09:32:42 +0000456
457 // The main memset sequence is a byte store followed by an MVC.
458 // Two STC or MV..I stores win over that, but the kind of fused stores
459 // generated by target-independent code don't when the byte value is
460 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
461 // than "STC;MVC". Handle the choice in target-specific code instead.
462 MaxStoresPerMemset = 0;
463 MaxStoresPerMemsetOptSize = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000464}
465
Mehdi Amini44ede332015-07-09 02:09:04 +0000466EVT SystemZTargetLowering::getSetCCResultType(const DataLayout &DL,
467 LLVMContext &, EVT VT) const {
Richard Sandifordabc010b2013-11-06 12:16:02 +0000468 if (!VT.isVector())
469 return MVT::i32;
470 return VT.changeVectorElementTypeToInteger();
471}
472
473bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
Stephen Lin73de7bf2013-07-09 18:16:56 +0000474 VT = VT.getScalarType();
475
476 if (!VT.isSimple())
477 return false;
478
479 switch (VT.getSimpleVT().SimpleTy) {
480 case MVT::f32:
481 case MVT::f64:
482 return true;
483 case MVT::f128:
484 return false;
485 default:
486 break;
487 }
488
489 return false;
490}
491
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000492bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
493 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
494 return Imm.isZero() || Imm.isNegZero();
495}
496
Ulrich Weigand1f6666a2015-03-31 12:52:27 +0000497bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
498 // We can use CGFI or CLGFI.
499 return isInt<32>(Imm) || isUInt<32>(Imm);
500}
501
502bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const {
503 // We can use ALGFI or SLGFI.
504 return isUInt<32>(Imm) || isUInt<32>(-Imm);
505}
506
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000507bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
508 unsigned,
509 unsigned,
510 bool *Fast) const {
Richard Sandiford46af5a22013-05-30 09:45:42 +0000511 // Unaligned accesses should never be slower than the expanded version.
512 // We check specifically for aligned accesses in the few cases where
513 // they are required.
514 if (Fast)
515 *Fast = true;
516 return true;
517}
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +0000518
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000519bool SystemZTargetLowering::isLegalAddressingMode(const DataLayout &DL,
520 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +0000521 unsigned AS) const {
Richard Sandiford791bea42013-07-31 12:58:26 +0000522 // Punt on globals for now, although they can be used in limited
523 // RELATIVE LONG cases.
524 if (AM.BaseGV)
525 return false;
526
527 // Require a 20-bit signed offset.
528 if (!isInt<20>(AM.BaseOffs))
529 return false;
530
531 // Indexing is OK but no scale factor can be applied.
532 return AM.Scale == 0 || AM.Scale == 1;
533}
534
Jonas Paulsson7a794222016-08-17 13:24:19 +0000535bool SystemZTargetLowering::isFoldableMemAccessOffset(Instruction *I,
536 int64_t Offset) const {
537 // This only applies to z13.
538 if (!Subtarget.hasVector())
539 return true;
540
541 // * Use LDE instead of LE/LEY to avoid partial register
542 // dependencies (LDE only supports small offsets).
543 // * Utilize the vector registers to hold floating point
544 // values (vector load / store instructions only support small
545 // offsets).
546
547 assert (isa<LoadInst>(I) || isa<StoreInst>(I));
548 Type *MemAccessTy = (isa<LoadInst>(I) ? I->getType() :
549 I->getOperand(0)->getType());
Jonas Paulssonc2829752017-01-11 14:40:39 +0000550 bool IsFPAccess = MemAccessTy->isFloatingPointTy();
551 bool IsVectorAccess = MemAccessTy->isVectorTy();
552
553 // A store of an extracted vector element will be combined into a VSTE type
554 // instruction.
555 if (!IsVectorAccess && isa<StoreInst>(I)) {
556 Value *DataOp = I->getOperand(0);
557 if (isa<ExtractElementInst>(DataOp))
558 IsVectorAccess = true;
559 }
560
561 // A load which gets inserted into a vector element will be combined into a
562 // VLE type instruction.
563 if (!IsVectorAccess && isa<LoadInst>(I) && I->hasOneUse()) {
564 User *LoadUser = *I->user_begin();
565 if (isa<InsertElementInst>(LoadUser))
566 IsVectorAccess = true;
567 }
568
569 if (!isUInt<12>(Offset) && (IsFPAccess || IsVectorAccess))
Jonas Paulsson7a794222016-08-17 13:24:19 +0000570 return false;
571
572 return true;
573}
574
Richard Sandiford709bda62013-08-19 12:42:31 +0000575bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
576 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
577 return false;
578 unsigned FromBits = FromType->getPrimitiveSizeInBits();
579 unsigned ToBits = ToType->getPrimitiveSizeInBits();
580 return FromBits > ToBits;
581}
582
583bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
584 if (!FromVT.isInteger() || !ToVT.isInteger())
585 return false;
586 unsigned FromBits = FromVT.getSizeInBits();
587 unsigned ToBits = ToVT.getSizeInBits();
588 return FromBits > ToBits;
589}
590
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000591//===----------------------------------------------------------------------===//
592// Inline asm support
593//===----------------------------------------------------------------------===//
594
595TargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000596SystemZTargetLowering::getConstraintType(StringRef Constraint) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000597 if (Constraint.size() == 1) {
598 switch (Constraint[0]) {
599 case 'a': // Address register
600 case 'd': // Data register (equivalent to 'r')
601 case 'f': // Floating-point register
Richard Sandiford0755c932013-10-01 11:26:28 +0000602 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000603 case 'r': // General-purpose register
604 return C_RegisterClass;
605
606 case 'Q': // Memory with base and unsigned 12-bit displacement
607 case 'R': // Likewise, plus an index
608 case 'S': // Memory with base and signed 20-bit displacement
609 case 'T': // Likewise, plus an index
610 case 'm': // Equivalent to 'T'.
611 return C_Memory;
612
613 case 'I': // Unsigned 8-bit constant
614 case 'J': // Unsigned 12-bit constant
615 case 'K': // Signed 16-bit constant
616 case 'L': // Signed 20-bit displacement (on all targets we support)
617 case 'M': // 0x7fffffff
618 return C_Other;
619
620 default:
621 break;
622 }
623 }
624 return TargetLowering::getConstraintType(Constraint);
625}
626
627TargetLowering::ConstraintWeight SystemZTargetLowering::
628getSingleConstraintMatchWeight(AsmOperandInfo &info,
629 const char *constraint) const {
630 ConstraintWeight weight = CW_Invalid;
631 Value *CallOperandVal = info.CallOperandVal;
632 // If we don't have a value, we can't do a match,
633 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +0000634 if (!CallOperandVal)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000635 return CW_Default;
636 Type *type = CallOperandVal->getType();
637 // Look at the constraint type.
638 switch (*constraint) {
639 default:
640 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
641 break;
642
643 case 'a': // Address register
644 case 'd': // Data register (equivalent to 'r')
Richard Sandiford0755c932013-10-01 11:26:28 +0000645 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000646 case 'r': // General-purpose register
647 if (CallOperandVal->getType()->isIntegerTy())
648 weight = CW_Register;
649 break;
650
651 case 'f': // Floating-point register
652 if (type->isFloatingPointTy())
653 weight = CW_Register;
654 break;
655
656 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000657 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000658 if (isUInt<8>(C->getZExtValue()))
659 weight = CW_Constant;
660 break;
661
662 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000663 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000664 if (isUInt<12>(C->getZExtValue()))
665 weight = CW_Constant;
666 break;
667
668 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000669 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000670 if (isInt<16>(C->getSExtValue()))
671 weight = CW_Constant;
672 break;
673
674 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000675 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000676 if (isInt<20>(C->getSExtValue()))
677 weight = CW_Constant;
678 break;
679
680 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000681 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000682 if (C->getZExtValue() == 0x7fffffff)
683 weight = CW_Constant;
684 break;
685 }
686 return weight;
687}
688
Richard Sandifordb8204052013-07-12 09:08:12 +0000689// Parse a "{tNNN}" register constraint for which the register type "t"
690// has already been verified. MC is the class associated with "t" and
691// Map maps 0-based register numbers to LLVM register numbers.
692static std::pair<unsigned, const TargetRegisterClass *>
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000693parseRegisterNumber(StringRef Constraint, const TargetRegisterClass *RC,
694 const unsigned *Map) {
Richard Sandifordb8204052013-07-12 09:08:12 +0000695 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
696 if (isdigit(Constraint[2])) {
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000697 unsigned Index;
698 bool Failed =
699 Constraint.slice(2, Constraint.size() - 1).getAsInteger(10, Index);
700 if (!Failed && Index < 16 && Map[Index])
Richard Sandifordb8204052013-07-12 09:08:12 +0000701 return std::make_pair(Map[Index], RC);
702 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000703 return std::make_pair(0U, nullptr);
Richard Sandifordb8204052013-07-12 09:08:12 +0000704}
705
Eric Christopher11e4df72015-02-26 22:38:43 +0000706std::pair<unsigned, const TargetRegisterClass *>
707SystemZTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000708 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000709 if (Constraint.size() == 1) {
710 // GCC Constraint Letters
711 switch (Constraint[0]) {
712 default: break;
713 case 'd': // Data register (equivalent to 'r')
714 case 'r': // General-purpose register
715 if (VT == MVT::i64)
716 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
717 else if (VT == MVT::i128)
718 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
719 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
720
721 case 'a': // Address register
722 if (VT == MVT::i64)
723 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
724 else if (VT == MVT::i128)
725 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
726 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
727
Richard Sandiford0755c932013-10-01 11:26:28 +0000728 case 'h': // High-part register (an LLVM extension)
729 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
730
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000731 case 'f': // Floating-point register
732 if (VT == MVT::f64)
733 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
734 else if (VT == MVT::f128)
735 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
736 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
737 }
738 }
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000739 if (Constraint.size() > 0 && Constraint[0] == '{') {
Richard Sandifordb8204052013-07-12 09:08:12 +0000740 // We need to override the default register parsing for GPRs and FPRs
741 // because the interpretation depends on VT. The internal names of
742 // the registers are also different from the external names
743 // (F0D and F0S instead of F0, etc.).
744 if (Constraint[1] == 'r') {
745 if (VT == MVT::i32)
746 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
747 SystemZMC::GR32Regs);
748 if (VT == MVT::i128)
749 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
750 SystemZMC::GR128Regs);
751 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
752 SystemZMC::GR64Regs);
753 }
754 if (Constraint[1] == 'f') {
755 if (VT == MVT::f32)
756 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
757 SystemZMC::FP32Regs);
758 if (VT == MVT::f128)
759 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
760 SystemZMC::FP128Regs);
761 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
762 SystemZMC::FP64Regs);
763 }
764 }
Eric Christopher11e4df72015-02-26 22:38:43 +0000765 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000766}
767
768void SystemZTargetLowering::
769LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
770 std::vector<SDValue> &Ops,
771 SelectionDAG &DAG) const {
772 // Only support length 1 constraints for now.
773 if (Constraint.length() == 1) {
774 switch (Constraint[0]) {
775 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000776 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000777 if (isUInt<8>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000778 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000779 Op.getValueType()));
780 return;
781
782 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000783 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000784 if (isUInt<12>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000785 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000786 Op.getValueType()));
787 return;
788
789 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000790 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000791 if (isInt<16>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000792 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000793 Op.getValueType()));
794 return;
795
796 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000797 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000798 if (isInt<20>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000799 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000800 Op.getValueType()));
801 return;
802
803 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000804 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000805 if (C->getZExtValue() == 0x7fffffff)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000806 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000807 Op.getValueType()));
808 return;
809 }
810 }
811 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
812}
813
814//===----------------------------------------------------------------------===//
815// Calling conventions
816//===----------------------------------------------------------------------===//
817
818#include "SystemZGenCallingConv.inc"
819
Richard Sandiford709bda62013-08-19 12:42:31 +0000820bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
821 Type *ToType) const {
822 return isTruncateFree(FromType, ToType);
823}
824
825bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Ulrich Weigand19d24d22015-11-13 13:00:27 +0000826 return CI->isTailCall();
Richard Sandiford709bda62013-08-19 12:42:31 +0000827}
828
Ulrich Weigand5211f9f2015-05-05 19:30:05 +0000829// We do not yet support 128-bit single-element vector types. If the user
830// attempts to use such types as function argument or return type, prefer
831// to error out instead of emitting code violating the ABI.
832static void VerifyVectorType(MVT VT, EVT ArgVT) {
833 if (ArgVT.isVector() && !VT.isVector())
834 report_fatal_error("Unsupported vector argument or return type");
835}
836
837static void VerifyVectorTypes(const SmallVectorImpl<ISD::InputArg> &Ins) {
838 for (unsigned i = 0; i < Ins.size(); ++i)
839 VerifyVectorType(Ins[i].VT, Ins[i].ArgVT);
840}
841
842static void VerifyVectorTypes(const SmallVectorImpl<ISD::OutputArg> &Outs) {
843 for (unsigned i = 0; i < Outs.size(); ++i)
844 VerifyVectorType(Outs[i].VT, Outs[i].ArgVT);
845}
846
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000847// Value is a value that has been passed to us in the location described by VA
848// (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
849// any loads onto Chain.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000850static SDValue convertLocVTToValVT(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000851 CCValAssign &VA, SDValue Chain,
852 SDValue Value) {
853 // If the argument has been promoted from a smaller type, insert an
854 // assertion to capture this.
855 if (VA.getLocInfo() == CCValAssign::SExt)
856 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
857 DAG.getValueType(VA.getValVT()));
858 else if (VA.getLocInfo() == CCValAssign::ZExt)
859 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
860 DAG.getValueType(VA.getValVT()));
861
862 if (VA.isExtInLoc())
863 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000864 else if (VA.getLocInfo() == CCValAssign::BCvt) {
865 // If this is a short vector argument loaded from the stack,
866 // extend from i64 to full vector size and then bitcast.
867 assert(VA.getLocVT() == MVT::i64);
868 assert(VA.getValVT().isVector());
Ahmed Bougacha128f8732016-04-26 21:15:30 +0000869 Value = DAG.getBuildVector(MVT::v2i64, DL, {Value, DAG.getUNDEF(MVT::i64)});
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000870 Value = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Value);
871 } else
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000872 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
873 return Value;
874}
875
876// Value is a value of type VA.getValVT() that we need to copy into
877// the location described by VA. Return a copy of Value converted to
878// VA.getValVT(). The caller is responsible for handling indirect values.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000879static SDValue convertValVTToLocVT(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000880 CCValAssign &VA, SDValue Value) {
881 switch (VA.getLocInfo()) {
882 case CCValAssign::SExt:
883 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
884 case CCValAssign::ZExt:
885 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
886 case CCValAssign::AExt:
887 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000888 case CCValAssign::BCvt:
889 // If this is a short vector argument to be stored to the stack,
890 // bitcast to v2i64 and then extract first element.
891 assert(VA.getLocVT() == MVT::i64);
892 assert(VA.getValVT().isVector());
893 Value = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Value);
894 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VA.getLocVT(), Value,
895 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000896 case CCValAssign::Full:
897 return Value;
898 default:
899 llvm_unreachable("Unhandled getLocInfo()");
900 }
901}
902
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000903SDValue SystemZTargetLowering::LowerFormalArguments(
904 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
905 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
906 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000907 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000908 MachineFrameInfo &MFI = MF.getFrameInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000909 MachineRegisterInfo &MRI = MF.getRegInfo();
910 SystemZMachineFunctionInfo *FuncInfo =
Eric Christophera6734172015-01-31 00:06:45 +0000911 MF.getInfo<SystemZMachineFunctionInfo>();
912 auto *TFL =
913 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering());
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +0000914 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000915
Ulrich Weigand5211f9f2015-05-05 19:30:05 +0000916 // Detect unsupported vector argument types.
917 if (Subtarget.hasVector())
918 VerifyVectorTypes(Ins);
919
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000920 // Assign locations to all of the incoming arguments.
921 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000922 SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000923 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
924
925 unsigned NumFixedGPRs = 0;
926 unsigned NumFixedFPRs = 0;
927 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
928 SDValue ArgValue;
929 CCValAssign &VA = ArgLocs[I];
930 EVT LocVT = VA.getLocVT();
931 if (VA.isRegLoc()) {
932 // Arguments passed in registers
933 const TargetRegisterClass *RC;
934 switch (LocVT.getSimpleVT().SimpleTy) {
935 default:
936 // Integers smaller than i64 should be promoted to i64.
937 llvm_unreachable("Unexpected argument type");
938 case MVT::i32:
939 NumFixedGPRs += 1;
940 RC = &SystemZ::GR32BitRegClass;
941 break;
942 case MVT::i64:
943 NumFixedGPRs += 1;
944 RC = &SystemZ::GR64BitRegClass;
945 break;
946 case MVT::f32:
947 NumFixedFPRs += 1;
948 RC = &SystemZ::FP32BitRegClass;
949 break;
950 case MVT::f64:
951 NumFixedFPRs += 1;
952 RC = &SystemZ::FP64BitRegClass;
953 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000954 case MVT::v16i8:
955 case MVT::v8i16:
956 case MVT::v4i32:
957 case MVT::v2i64:
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000958 case MVT::v4f32:
Ulrich Weigandcd808232015-05-05 19:26:48 +0000959 case MVT::v2f64:
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000960 RC = &SystemZ::VR128BitRegClass;
961 break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000962 }
963
964 unsigned VReg = MRI.createVirtualRegister(RC);
965 MRI.addLiveIn(VA.getLocReg(), VReg);
966 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
967 } else {
968 assert(VA.isMemLoc() && "Argument not register or memory");
969
970 // Create the frame index object for this incoming parameter.
Matthias Braun941a7052016-07-28 18:40:00 +0000971 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
972 VA.getLocMemOffset(), true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000973
974 // Create the SelectionDAG nodes corresponding to a load
975 // from this parameter. Unpromoted ints and floats are
976 // passed as right-justified 8-byte values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000977 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
978 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000979 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
980 DAG.getIntPtrConstant(4, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000981 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +0000982 MachinePointerInfo::getFixedStack(MF, FI));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000983 }
984
985 // Convert the value of the argument register into the value that's
986 // being passed.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +0000987 if (VA.getLocInfo() == CCValAssign::Indirect) {
Justin Lebar9c375812016-07-15 18:27:10 +0000988 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
989 MachinePointerInfo()));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +0000990 // If the original argument was split (e.g. i128), we need
991 // to load all parts of it here (using the same address).
992 unsigned ArgIndex = Ins[I].OrigArgIndex;
993 assert (Ins[I].PartOffset == 0);
994 while (I + 1 != E && Ins[I + 1].OrigArgIndex == ArgIndex) {
995 CCValAssign &PartVA = ArgLocs[I + 1];
996 unsigned PartOffset = Ins[I + 1].PartOffset;
997 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
998 DAG.getIntPtrConstant(PartOffset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +0000999 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
1000 MachinePointerInfo()));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001001 ++I;
1002 }
1003 } else
1004 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001005 }
1006
1007 if (IsVarArg) {
1008 // Save the number of non-varargs registers for later use by va_start, etc.
1009 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
1010 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
1011
1012 // Likewise the address (in the form of a frame index) of where the
1013 // first stack vararg would be. The 1-byte size here is arbitrary.
1014 int64_t StackSize = CCInfo.getNextStackOffset();
Matthias Braun941a7052016-07-28 18:40:00 +00001015 FuncInfo->setVarArgsFrameIndex(MFI.CreateFixedObject(1, StackSize, true));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001016
1017 // ...and a similar frame index for the caller-allocated save area
1018 // that will be used to store the incoming registers.
1019 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
Matthias Braun941a7052016-07-28 18:40:00 +00001020 unsigned RegSaveIndex = MFI.CreateFixedObject(1, RegSaveOffset, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001021 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
1022
1023 // Store the FPR varargs in the reserved frame slots. (We store the
1024 // GPRs as part of the prologue.)
1025 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
1026 SDValue MemOps[SystemZ::NumArgFPRs];
1027 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
1028 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
Matthias Braun941a7052016-07-28 18:40:00 +00001029 int FI = MFI.CreateFixedObject(8, RegSaveOffset + Offset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +00001030 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001031 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
1032 &SystemZ::FP64BitRegClass);
1033 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
1034 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00001035 MachinePointerInfo::getFixedStack(MF, FI));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001036 }
1037 // Join the stores, which are independent of one another.
1038 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00001039 makeArrayRef(&MemOps[NumFixedFPRs],
1040 SystemZ::NumArgFPRs-NumFixedFPRs));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001041 }
1042 }
1043
1044 return Chain;
1045}
1046
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +00001047static bool canUseSiblingCall(const CCState &ArgCCInfo,
Bryan Chan893110e2016-04-28 00:17:23 +00001048 SmallVectorImpl<CCValAssign> &ArgLocs,
1049 SmallVectorImpl<ISD::OutputArg> &Outs) {
Richard Sandiford709bda62013-08-19 12:42:31 +00001050 // Punt if there are any indirect or stack arguments, or if the call
Bryan Chan893110e2016-04-28 00:17:23 +00001051 // needs the callee-saved argument register R6, or if the call uses
1052 // the callee-saved register arguments SwiftSelf and SwiftError.
Richard Sandiford709bda62013-08-19 12:42:31 +00001053 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1054 CCValAssign &VA = ArgLocs[I];
1055 if (VA.getLocInfo() == CCValAssign::Indirect)
1056 return false;
1057 if (!VA.isRegLoc())
1058 return false;
1059 unsigned Reg = VA.getLocReg();
Richard Sandiford0755c932013-10-01 11:26:28 +00001060 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
Richard Sandiford709bda62013-08-19 12:42:31 +00001061 return false;
Bryan Chan893110e2016-04-28 00:17:23 +00001062 if (Outs[I].Flags.isSwiftSelf() || Outs[I].Flags.isSwiftError())
1063 return false;
Richard Sandiford709bda62013-08-19 12:42:31 +00001064 }
1065 return true;
1066}
1067
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001068SDValue
1069SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
1070 SmallVectorImpl<SDValue> &InVals) const {
1071 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001072 SDLoc &DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001073 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1074 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1075 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001076 SDValue Chain = CLI.Chain;
1077 SDValue Callee = CLI.Callee;
Richard Sandiford709bda62013-08-19 12:42:31 +00001078 bool &IsTailCall = CLI.IsTailCall;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001079 CallingConv::ID CallConv = CLI.CallConv;
1080 bool IsVarArg = CLI.IsVarArg;
1081 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +00001082 EVT PtrVT = getPointerTy(MF.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001083
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001084 // Detect unsupported vector argument and return types.
1085 if (Subtarget.hasVector()) {
1086 VerifyVectorTypes(Outs);
1087 VerifyVectorTypes(Ins);
1088 }
1089
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001090 // Analyze the operands of the call, assigning locations to each operand.
1091 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001092 SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001093 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
1094
Richard Sandiford709bda62013-08-19 12:42:31 +00001095 // We don't support GuaranteedTailCallOpt, only automatically-detected
1096 // sibling calls.
Bryan Chan893110e2016-04-28 00:17:23 +00001097 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs, Outs))
Richard Sandiford709bda62013-08-19 12:42:31 +00001098 IsTailCall = false;
1099
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001100 // Get a count of how many bytes are to be pushed on the stack.
1101 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1102
1103 // Mark the start of the call.
Richard Sandiford709bda62013-08-19 12:42:31 +00001104 if (!IsTailCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001105 Chain = DAG.getCALLSEQ_START(Chain,
1106 DAG.getConstant(NumBytes, DL, PtrVT, true),
Richard Sandiford709bda62013-08-19 12:42:31 +00001107 DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001108
1109 // Copy argument values to their designated locations.
1110 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
1111 SmallVector<SDValue, 8> MemOpChains;
1112 SDValue StackPtr;
1113 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1114 CCValAssign &VA = ArgLocs[I];
1115 SDValue ArgValue = OutVals[I];
1116
1117 if (VA.getLocInfo() == CCValAssign::Indirect) {
1118 // Store the argument in a stack slot and pass its address.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001119 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[I].ArgVT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001120 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Justin Lebar9c375812016-07-15 18:27:10 +00001121 MemOpChains.push_back(
1122 DAG.getStore(Chain, DL, ArgValue, SpillSlot,
1123 MachinePointerInfo::getFixedStack(MF, FI)));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001124 // If the original argument was split (e.g. i128), we need
1125 // to store all parts of it here (and pass just one address).
1126 unsigned ArgIndex = Outs[I].OrigArgIndex;
1127 assert (Outs[I].PartOffset == 0);
1128 while (I + 1 != E && Outs[I + 1].OrigArgIndex == ArgIndex) {
1129 SDValue PartValue = OutVals[I + 1];
1130 unsigned PartOffset = Outs[I + 1].PartOffset;
1131 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
1132 DAG.getIntPtrConstant(PartOffset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00001133 MemOpChains.push_back(
1134 DAG.getStore(Chain, DL, PartValue, Address,
1135 MachinePointerInfo::getFixedStack(MF, FI)));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001136 ++I;
1137 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001138 ArgValue = SpillSlot;
1139 } else
1140 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
1141
1142 if (VA.isRegLoc())
1143 // Queue up the argument copies and emit them at the end.
1144 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1145 else {
1146 assert(VA.isMemLoc() && "Argument not register or memory");
1147
1148 // Work out the address of the stack slot. Unpromoted ints and
1149 // floats are passed as right-justified 8-byte values.
1150 if (!StackPtr.getNode())
1151 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
1152 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
1153 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
1154 Offset += 4;
1155 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001156 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001157
1158 // Emit the store.
Justin Lebar9c375812016-07-15 18:27:10 +00001159 MemOpChains.push_back(
1160 DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001161 }
1162 }
1163
1164 // Join the stores, which are independent of one another.
1165 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001166 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001167
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001168 // Accept direct calls by converting symbolic call addresses to the
Richard Sandiford709bda62013-08-19 12:42:31 +00001169 // associated Target* opcodes. Force %r1 to be used for indirect
1170 // tail calls.
1171 SDValue Glue;
Richard Sandiford21f5d682014-03-06 11:22:58 +00001172 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001173 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
1174 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford21f5d682014-03-06 11:22:58 +00001175 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001176 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
1177 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford709bda62013-08-19 12:42:31 +00001178 } else if (IsTailCall) {
1179 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
1180 Glue = Chain.getValue(1);
1181 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
1182 }
1183
1184 // Build a sequence of copy-to-reg nodes, chained and glued together.
1185 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
1186 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
1187 RegsToPass[I].second, Glue);
1188 Glue = Chain.getValue(1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001189 }
1190
1191 // The first call operand is the chain and the second is the target address.
1192 SmallVector<SDValue, 8> Ops;
1193 Ops.push_back(Chain);
1194 Ops.push_back(Callee);
1195
1196 // Add argument registers to the end of the list so that they are
1197 // known live into the call.
1198 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
1199 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
1200 RegsToPass[I].second.getValueType()));
1201
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001202 // Add a register mask operand representing the call-preserved registers.
Eric Christophera6734172015-01-31 00:06:45 +00001203 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00001204 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001205 assert(Mask && "Missing call preserved mask for calling convention");
1206 Ops.push_back(DAG.getRegisterMask(Mask));
1207
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001208 // Glue the call to the argument copies, if any.
1209 if (Glue.getNode())
1210 Ops.push_back(Glue);
1211
1212 // Emit the call.
1213 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Richard Sandiford709bda62013-08-19 12:42:31 +00001214 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001215 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
1216 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001217 Glue = Chain.getValue(1);
1218
1219 // Mark the end of the call, which is glued to the call itself.
1220 Chain = DAG.getCALLSEQ_END(Chain,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001221 DAG.getConstant(NumBytes, DL, PtrVT, true),
1222 DAG.getConstant(0, DL, PtrVT, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001223 Glue, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001224 Glue = Chain.getValue(1);
1225
1226 // Assign locations to each value returned by this call.
1227 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001228 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001229 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
1230
1231 // Copy all of the result registers out of their specified physreg.
1232 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1233 CCValAssign &VA = RetLocs[I];
1234
1235 // Copy the value out, gluing the copy to the end of the call sequence.
1236 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
1237 VA.getLocVT(), Glue);
1238 Chain = RetValue.getValue(1);
1239 Glue = RetValue.getValue(2);
1240
1241 // Convert the value of the return register into the value that's
1242 // being returned.
1243 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
1244 }
1245
1246 return Chain;
1247}
1248
Ulrich Weiganda887f062015-08-13 13:37:06 +00001249bool SystemZTargetLowering::
1250CanLowerReturn(CallingConv::ID CallConv,
1251 MachineFunction &MF, bool isVarArg,
1252 const SmallVectorImpl<ISD::OutputArg> &Outs,
1253 LLVMContext &Context) const {
1254 // Detect unsupported vector return types.
1255 if (Subtarget.hasVector())
1256 VerifyVectorTypes(Outs);
1257
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001258 // Special case that we cannot easily detect in RetCC_SystemZ since
1259 // i128 is not a legal type.
1260 for (auto &Out : Outs)
1261 if (Out.ArgVT == MVT::i128)
1262 return false;
1263
Ulrich Weiganda887f062015-08-13 13:37:06 +00001264 SmallVector<CCValAssign, 16> RetLocs;
1265 CCState RetCCInfo(CallConv, isVarArg, MF, RetLocs, Context);
1266 return RetCCInfo.CheckReturn(Outs, RetCC_SystemZ);
1267}
1268
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001269SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001270SystemZTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1271 bool IsVarArg,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001272 const SmallVectorImpl<ISD::OutputArg> &Outs,
1273 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001274 const SDLoc &DL, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001275 MachineFunction &MF = DAG.getMachineFunction();
1276
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001277 // Detect unsupported vector return types.
1278 if (Subtarget.hasVector())
1279 VerifyVectorTypes(Outs);
1280
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001281 // Assign locations to each returned value.
1282 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001283 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001284 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
1285
1286 // Quick exit for void returns
1287 if (RetLocs.empty())
1288 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
1289
1290 // Copy the result values into the output registers.
1291 SDValue Glue;
1292 SmallVector<SDValue, 4> RetOps;
1293 RetOps.push_back(Chain);
1294 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1295 CCValAssign &VA = RetLocs[I];
1296 SDValue RetValue = OutVals[I];
1297
1298 // Make the return register live on exit.
1299 assert(VA.isRegLoc() && "Can only return in registers!");
1300
1301 // Promote the value as required.
1302 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
1303
1304 // Chain and glue the copies together.
1305 unsigned Reg = VA.getLocReg();
1306 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
1307 Glue = Chain.getValue(1);
1308 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
1309 }
1310
1311 // Update chain and glue.
1312 RetOps[0] = Chain;
1313 if (Glue.getNode())
1314 RetOps.push_back(Glue);
1315
Craig Topper48d114b2014-04-26 18:35:24 +00001316 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001317}
1318
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001319SDValue SystemZTargetLowering::prepareVolatileOrAtomicLoad(
1320 SDValue Chain, const SDLoc &DL, SelectionDAG &DAG) const {
Richard Sandiford9afe6132013-12-10 10:36:34 +00001321 return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain);
1322}
1323
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001324// Return true if Op is an intrinsic node with chain that returns the CC value
1325// as its only (other) argument. Provide the associated SystemZISD opcode and
1326// the mask of valid CC values if so.
1327static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode,
1328 unsigned &CCValid) {
1329 unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1330 switch (Id) {
1331 case Intrinsic::s390_tbegin:
1332 Opcode = SystemZISD::TBEGIN;
1333 CCValid = SystemZ::CCMASK_TBEGIN;
1334 return true;
1335
1336 case Intrinsic::s390_tbegin_nofloat:
1337 Opcode = SystemZISD::TBEGIN_NOFLOAT;
1338 CCValid = SystemZ::CCMASK_TBEGIN;
1339 return true;
1340
1341 case Intrinsic::s390_tend:
1342 Opcode = SystemZISD::TEND;
1343 CCValid = SystemZ::CCMASK_TEND;
1344 return true;
1345
1346 default:
1347 return false;
1348 }
1349}
1350
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001351// Return true if Op is an intrinsic node without chain that returns the
1352// CC value as its final argument. Provide the associated SystemZISD
1353// opcode and the mask of valid CC values if so.
1354static bool isIntrinsicWithCC(SDValue Op, unsigned &Opcode, unsigned &CCValid) {
1355 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1356 switch (Id) {
1357 case Intrinsic::s390_vpkshs:
1358 case Intrinsic::s390_vpksfs:
1359 case Intrinsic::s390_vpksgs:
1360 Opcode = SystemZISD::PACKS_CC;
1361 CCValid = SystemZ::CCMASK_VCMP;
1362 return true;
1363
1364 case Intrinsic::s390_vpklshs:
1365 case Intrinsic::s390_vpklsfs:
1366 case Intrinsic::s390_vpklsgs:
1367 Opcode = SystemZISD::PACKLS_CC;
1368 CCValid = SystemZ::CCMASK_VCMP;
1369 return true;
1370
1371 case Intrinsic::s390_vceqbs:
1372 case Intrinsic::s390_vceqhs:
1373 case Intrinsic::s390_vceqfs:
1374 case Intrinsic::s390_vceqgs:
1375 Opcode = SystemZISD::VICMPES;
1376 CCValid = SystemZ::CCMASK_VCMP;
1377 return true;
1378
1379 case Intrinsic::s390_vchbs:
1380 case Intrinsic::s390_vchhs:
1381 case Intrinsic::s390_vchfs:
1382 case Intrinsic::s390_vchgs:
1383 Opcode = SystemZISD::VICMPHS;
1384 CCValid = SystemZ::CCMASK_VCMP;
1385 return true;
1386
1387 case Intrinsic::s390_vchlbs:
1388 case Intrinsic::s390_vchlhs:
1389 case Intrinsic::s390_vchlfs:
1390 case Intrinsic::s390_vchlgs:
1391 Opcode = SystemZISD::VICMPHLS;
1392 CCValid = SystemZ::CCMASK_VCMP;
1393 return true;
1394
1395 case Intrinsic::s390_vtm:
1396 Opcode = SystemZISD::VTM;
1397 CCValid = SystemZ::CCMASK_VCMP;
1398 return true;
1399
1400 case Intrinsic::s390_vfaebs:
1401 case Intrinsic::s390_vfaehs:
1402 case Intrinsic::s390_vfaefs:
1403 Opcode = SystemZISD::VFAE_CC;
1404 CCValid = SystemZ::CCMASK_ANY;
1405 return true;
1406
1407 case Intrinsic::s390_vfaezbs:
1408 case Intrinsic::s390_vfaezhs:
1409 case Intrinsic::s390_vfaezfs:
1410 Opcode = SystemZISD::VFAEZ_CC;
1411 CCValid = SystemZ::CCMASK_ANY;
1412 return true;
1413
1414 case Intrinsic::s390_vfeebs:
1415 case Intrinsic::s390_vfeehs:
1416 case Intrinsic::s390_vfeefs:
1417 Opcode = SystemZISD::VFEE_CC;
1418 CCValid = SystemZ::CCMASK_ANY;
1419 return true;
1420
1421 case Intrinsic::s390_vfeezbs:
1422 case Intrinsic::s390_vfeezhs:
1423 case Intrinsic::s390_vfeezfs:
1424 Opcode = SystemZISD::VFEEZ_CC;
1425 CCValid = SystemZ::CCMASK_ANY;
1426 return true;
1427
1428 case Intrinsic::s390_vfenebs:
1429 case Intrinsic::s390_vfenehs:
1430 case Intrinsic::s390_vfenefs:
1431 Opcode = SystemZISD::VFENE_CC;
1432 CCValid = SystemZ::CCMASK_ANY;
1433 return true;
1434
1435 case Intrinsic::s390_vfenezbs:
1436 case Intrinsic::s390_vfenezhs:
1437 case Intrinsic::s390_vfenezfs:
1438 Opcode = SystemZISD::VFENEZ_CC;
1439 CCValid = SystemZ::CCMASK_ANY;
1440 return true;
1441
1442 case Intrinsic::s390_vistrbs:
1443 case Intrinsic::s390_vistrhs:
1444 case Intrinsic::s390_vistrfs:
1445 Opcode = SystemZISD::VISTR_CC;
1446 CCValid = SystemZ::CCMASK_0 | SystemZ::CCMASK_3;
1447 return true;
1448
1449 case Intrinsic::s390_vstrcbs:
1450 case Intrinsic::s390_vstrchs:
1451 case Intrinsic::s390_vstrcfs:
1452 Opcode = SystemZISD::VSTRC_CC;
1453 CCValid = SystemZ::CCMASK_ANY;
1454 return true;
1455
1456 case Intrinsic::s390_vstrczbs:
1457 case Intrinsic::s390_vstrczhs:
1458 case Intrinsic::s390_vstrczfs:
1459 Opcode = SystemZISD::VSTRCZ_CC;
1460 CCValid = SystemZ::CCMASK_ANY;
1461 return true;
1462
1463 case Intrinsic::s390_vfcedbs:
1464 Opcode = SystemZISD::VFCMPES;
1465 CCValid = SystemZ::CCMASK_VCMP;
1466 return true;
1467
1468 case Intrinsic::s390_vfchdbs:
1469 Opcode = SystemZISD::VFCMPHS;
1470 CCValid = SystemZ::CCMASK_VCMP;
1471 return true;
1472
1473 case Intrinsic::s390_vfchedbs:
1474 Opcode = SystemZISD::VFCMPHES;
1475 CCValid = SystemZ::CCMASK_VCMP;
1476 return true;
1477
1478 case Intrinsic::s390_vftcidb:
1479 Opcode = SystemZISD::VFTCI;
1480 CCValid = SystemZ::CCMASK_VCMP;
1481 return true;
1482
Marcin Koscielnickicf7cc722016-07-10 14:41:22 +00001483 case Intrinsic::s390_tdc:
1484 Opcode = SystemZISD::TDC;
1485 CCValid = SystemZ::CCMASK_TDC;
1486 return true;
1487
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001488 default:
1489 return false;
1490 }
1491}
1492
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001493// Emit an intrinsic with chain with a glued value instead of its CC result.
1494static SDValue emitIntrinsicWithChainAndGlue(SelectionDAG &DAG, SDValue Op,
1495 unsigned Opcode) {
1496 // Copy all operands except the intrinsic ID.
1497 unsigned NumOps = Op.getNumOperands();
1498 SmallVector<SDValue, 6> Ops;
1499 Ops.reserve(NumOps - 1);
1500 Ops.push_back(Op.getOperand(0));
1501 for (unsigned I = 2; I < NumOps; ++I)
1502 Ops.push_back(Op.getOperand(I));
1503
1504 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
1505 SDVTList RawVTs = DAG.getVTList(MVT::Other, MVT::Glue);
1506 SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1507 SDValue OldChain = SDValue(Op.getNode(), 1);
1508 SDValue NewChain = SDValue(Intr.getNode(), 0);
1509 DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain);
1510 return Intr;
1511}
1512
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001513// Emit an intrinsic with a glued value instead of its CC result.
1514static SDValue emitIntrinsicWithGlue(SelectionDAG &DAG, SDValue Op,
1515 unsigned Opcode) {
1516 // Copy all operands except the intrinsic ID.
1517 unsigned NumOps = Op.getNumOperands();
1518 SmallVector<SDValue, 6> Ops;
1519 Ops.reserve(NumOps - 1);
1520 for (unsigned I = 1; I < NumOps; ++I)
1521 Ops.push_back(Op.getOperand(I));
1522
1523 if (Op->getNumValues() == 1)
1524 return DAG.getNode(Opcode, SDLoc(Op), MVT::Glue, Ops);
1525 assert(Op->getNumValues() == 2 && "Expected exactly one non-CC result");
1526 SDVTList RawVTs = DAG.getVTList(Op->getValueType(0), MVT::Glue);
1527 return DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1528}
1529
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001530// CC is a comparison that will be implemented using an integer or
1531// floating-point comparison. Return the condition code mask for
1532// a branch on true. In the integer case, CCMASK_CMP_UO is set for
1533// unsigned comparisons and clear for signed ones. In the floating-point
1534// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1535static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1536#define CONV(X) \
1537 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1538 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1539 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1540
1541 switch (CC) {
1542 default:
1543 llvm_unreachable("Invalid integer condition!");
1544
1545 CONV(EQ);
1546 CONV(NE);
1547 CONV(GT);
1548 CONV(GE);
1549 CONV(LT);
1550 CONV(LE);
1551
1552 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1553 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1554 }
1555#undef CONV
1556}
1557
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001558// Return a sequence for getting a 1 from an IPM result when CC has a
1559// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1560// The handling of CC values outside CCValid doesn't matter.
1561static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1562 // Deal with cases where the result can be taken directly from a bit
1563 // of the IPM result.
1564 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1565 return IPMConversion(0, 0, SystemZ::IPM_CC);
1566 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1567 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1568
1569 // Deal with cases where we can add a value to force the sign bit
1570 // to contain the right value. Putting the bit in 31 means we can
1571 // use SRL rather than RISBG(L), and also makes it easier to get a
1572 // 0/-1 value, so it has priority over the other tests below.
1573 //
1574 // These sequences rely on the fact that the upper two bits of the
1575 // IPM result are zero.
1576 uint64_t TopBit = uint64_t(1) << 31;
1577 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1578 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1579 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1580 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1581 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1582 | SystemZ::CCMASK_1
1583 | SystemZ::CCMASK_2)))
1584 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1585 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1586 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1587 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1588 | SystemZ::CCMASK_2
1589 | SystemZ::CCMASK_3)))
1590 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1591
1592 // Next try inverting the value and testing a bit. 0/1 could be
1593 // handled this way too, but we dealt with that case above.
1594 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1595 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1596
1597 // Handle cases where adding a value forces a non-sign bit to contain
1598 // the right value.
1599 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1600 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1601 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1602 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1603
Alp Tokercb402912014-01-24 17:20:08 +00001604 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001605 // can be done by inverting the low CC bit and applying one of the
1606 // sign-based extractions above.
1607 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1608 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1609 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1610 return IPMConversion(1 << SystemZ::IPM_CC,
1611 TopBit - (3 << SystemZ::IPM_CC), 31);
1612 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1613 | SystemZ::CCMASK_1
1614 | SystemZ::CCMASK_3)))
1615 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1616 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1617 | SystemZ::CCMASK_2
1618 | SystemZ::CCMASK_3)))
1619 return IPMConversion(1 << SystemZ::IPM_CC,
1620 TopBit - (1 << SystemZ::IPM_CC), 31);
1621
1622 llvm_unreachable("Unexpected CC combination");
1623}
1624
Richard Sandifordd420f732013-12-13 15:28:45 +00001625// If C can be converted to a comparison against zero, adjust the operands
Richard Sandiforda0757082013-08-01 10:29:45 +00001626// as necessary.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001627static void adjustZeroCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001628 if (C.ICmpType == SystemZICMP::UnsignedOnly)
Richard Sandiforda0757082013-08-01 10:29:45 +00001629 return;
1630
Richard Sandiford21f5d682014-03-06 11:22:58 +00001631 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
Richard Sandiforda0757082013-08-01 10:29:45 +00001632 if (!ConstOp1)
1633 return;
1634
1635 int64_t Value = ConstOp1->getSExtValue();
Richard Sandifordd420f732013-12-13 15:28:45 +00001636 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1637 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1638 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1639 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1640 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001641 C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType());
Richard Sandiforda0757082013-08-01 10:29:45 +00001642 }
1643}
1644
Richard Sandifordd420f732013-12-13 15:28:45 +00001645// If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1646// adjust the operands as necessary.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001647static void adjustSubwordCmp(SelectionDAG &DAG, const SDLoc &DL,
1648 Comparison &C) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001649 // For us to make any changes, it must a comparison between a single-use
1650 // load and a constant.
Richard Sandifordd420f732013-12-13 15:28:45 +00001651 if (!C.Op0.hasOneUse() ||
1652 C.Op0.getOpcode() != ISD::LOAD ||
1653 C.Op1.getOpcode() != ISD::Constant)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001654 return;
1655
1656 // We must have an 8- or 16-bit load.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001657 auto *Load = cast<LoadSDNode>(C.Op0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001658 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1659 if (NumBits != 8 && NumBits != 16)
1660 return;
1661
1662 // The load must be an extending one and the constant must be within the
1663 // range of the unextended value.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001664 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001665 uint64_t Value = ConstOp1->getZExtValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001666 uint64_t Mask = (1 << NumBits) - 1;
1667 if (Load->getExtensionType() == ISD::SEXTLOAD) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001668 // Make sure that ConstOp1 is in range of C.Op0.
1669 int64_t SignedValue = ConstOp1->getSExtValue();
1670 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001671 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001672 if (C.ICmpType != SystemZICMP::SignedOnly) {
1673 // Unsigned comparison between two sign-extended values is equivalent
1674 // to unsigned comparison between two zero-extended values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001675 Value &= Mask;
Richard Sandifordd420f732013-12-13 15:28:45 +00001676 } else if (NumBits == 8) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001677 // Try to treat the comparison as unsigned, so that we can use CLI.
1678 // Adjust CCMask and Value as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001679 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001680 // Test whether the high bit of the byte is set.
Richard Sandifordd420f732013-12-13 15:28:45 +00001681 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1682 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001683 // Test whether the high bit of the byte is clear.
Richard Sandifordd420f732013-12-13 15:28:45 +00001684 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001685 else
1686 // No instruction exists for this combination.
1687 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001688 C.ICmpType = SystemZICMP::UnsignedOnly;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001689 }
1690 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1691 if (Value > Mask)
1692 return;
Ulrich Weigand47f36492015-12-16 18:04:06 +00001693 // If the constant is in range, we can use any comparison.
1694 C.ICmpType = SystemZICMP::Any;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001695 } else
1696 return;
1697
1698 // Make sure that the first operand is an i32 of the right extension type.
Richard Sandifordd420f732013-12-13 15:28:45 +00001699 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1700 ISD::SEXTLOAD :
1701 ISD::ZEXTLOAD);
1702 if (C.Op0.getValueType() != MVT::i32 ||
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001703 Load->getExtensionType() != ExtType)
Justin Lebar9c375812016-07-15 18:27:10 +00001704 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32, Load->getChain(),
1705 Load->getBasePtr(), Load->getPointerInfo(),
1706 Load->getMemoryVT(), Load->getAlignment(),
1707 Load->getMemOperand()->getFlags());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001708
1709 // Make sure that the second operand is an i32 with the right value.
Richard Sandifordd420f732013-12-13 15:28:45 +00001710 if (C.Op1.getValueType() != MVT::i32 ||
1711 Value != ConstOp1->getZExtValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001712 C.Op1 = DAG.getConstant(Value, DL, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001713}
1714
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001715// Return true if Op is either an unextended load, or a load suitable
1716// for integer register-memory comparisons of type ICmpType.
1717static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001718 auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001719 if (Load) {
1720 // There are no instructions to compare a register with a memory byte.
1721 if (Load->getMemoryVT() == MVT::i8)
1722 return false;
1723 // Otherwise decide on extension type.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001724 switch (Load->getExtensionType()) {
1725 case ISD::NON_EXTLOAD:
Richard Sandiford24e597b2013-08-23 11:27:19 +00001726 return true;
1727 case ISD::SEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001728 return ICmpType != SystemZICMP::UnsignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001729 case ISD::ZEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001730 return ICmpType != SystemZICMP::SignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001731 default:
1732 break;
1733 }
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001734 }
Richard Sandiford24e597b2013-08-23 11:27:19 +00001735 return false;
1736}
1737
Richard Sandifordd420f732013-12-13 15:28:45 +00001738// Return true if it is better to swap the operands of C.
1739static bool shouldSwapCmpOperands(const Comparison &C) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001740 // Leave f128 comparisons alone, since they have no memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001741 if (C.Op0.getValueType() == MVT::f128)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001742 return false;
1743
1744 // Always keep a floating-point constant second, since comparisons with
1745 // zero can use LOAD TEST and comparisons with other constants make a
1746 // natural memory operand.
Richard Sandifordd420f732013-12-13 15:28:45 +00001747 if (isa<ConstantFPSDNode>(C.Op1))
Richard Sandiford24e597b2013-08-23 11:27:19 +00001748 return false;
1749
1750 // Never swap comparisons with zero since there are many ways to optimize
1751 // those later.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001752 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001753 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001754 return false;
1755
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001756 // Also keep natural memory operands second if the loaded value is
1757 // only used here. Several comparisons have memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001758 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001759 return false;
1760
Richard Sandiford24e597b2013-08-23 11:27:19 +00001761 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1762 // In that case we generally prefer the memory to be second.
Richard Sandifordd420f732013-12-13 15:28:45 +00001763 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001764 // The only exceptions are when the second operand is a constant and
1765 // we can use things like CHHSI.
Richard Sandifordd420f732013-12-13 15:28:45 +00001766 if (!ConstOp1)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001767 return true;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001768 // The unsigned memory-immediate instructions can handle 16-bit
1769 // unsigned integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001770 if (C.ICmpType != SystemZICMP::SignedOnly &&
1771 isUInt<16>(ConstOp1->getZExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001772 return false;
1773 // The signed memory-immediate instructions can handle 16-bit
1774 // signed integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001775 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1776 isInt<16>(ConstOp1->getSExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001777 return false;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001778 return true;
1779 }
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001780
1781 // Try to promote the use of CGFR and CLGFR.
Richard Sandifordd420f732013-12-13 15:28:45 +00001782 unsigned Opcode0 = C.Op0.getOpcode();
1783 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001784 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001785 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001786 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001787 if (C.ICmpType != SystemZICMP::SignedOnly &&
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001788 Opcode0 == ISD::AND &&
Richard Sandifordd420f732013-12-13 15:28:45 +00001789 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1790 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001791 return true;
1792
Richard Sandiford24e597b2013-08-23 11:27:19 +00001793 return false;
1794}
1795
Richard Sandiford73170f82013-12-11 11:45:08 +00001796// Return a version of comparison CC mask CCMask in which the LT and GT
1797// actions are swapped.
1798static unsigned reverseCCMask(unsigned CCMask) {
1799 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1800 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1801 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1802 (CCMask & SystemZ::CCMASK_CMP_UO));
1803}
1804
Richard Sandiford0847c452013-12-13 15:50:30 +00001805// Check whether C tests for equality between X and Y and whether X - Y
1806// or Y - X is also computed. In that case it's better to compare the
1807// result of the subtraction against zero.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001808static void adjustForSubtraction(SelectionDAG &DAG, const SDLoc &DL,
1809 Comparison &C) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001810 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1811 C.CCMask == SystemZ::CCMASK_CMP_NE) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001812 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001813 SDNode *N = *I;
1814 if (N->getOpcode() == ISD::SUB &&
1815 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1816 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1817 C.Op0 = SDValue(N, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001818 C.Op1 = DAG.getConstant(0, DL, N->getValueType(0));
Richard Sandiford0847c452013-12-13 15:50:30 +00001819 return;
1820 }
1821 }
1822 }
1823}
1824
Richard Sandifordd420f732013-12-13 15:28:45 +00001825// Check whether C compares a floating-point value with zero and if that
1826// floating-point value is also negated. In this case we can use the
1827// negation to set CC, so avoiding separate LOAD AND TEST and
1828// LOAD (NEGATIVE/COMPLEMENT) instructions.
1829static void adjustForFNeg(Comparison &C) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001830 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
Richard Sandiford73170f82013-12-11 11:45:08 +00001831 if (C1 && C1->isZero()) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001832 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford73170f82013-12-11 11:45:08 +00001833 SDNode *N = *I;
1834 if (N->getOpcode() == ISD::FNEG) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001835 C.Op0 = SDValue(N, 0);
1836 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford73170f82013-12-11 11:45:08 +00001837 return;
1838 }
1839 }
1840 }
1841}
1842
Richard Sandifordd420f732013-12-13 15:28:45 +00001843// Check whether C compares (shl X, 32) with 0 and whether X is
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001844// also sign-extended. In that case it is better to test the result
1845// of the sign extension using LTGFR.
1846//
1847// This case is important because InstCombine transforms a comparison
1848// with (sext (trunc X)) into a comparison with (shl X, 32).
Richard Sandifordd420f732013-12-13 15:28:45 +00001849static void adjustForLTGFR(Comparison &C) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001850 // Check for a comparison between (shl X, 32) and 0.
Richard Sandifordd420f732013-12-13 15:28:45 +00001851 if (C.Op0.getOpcode() == ISD::SHL &&
1852 C.Op0.getValueType() == MVT::i64 &&
1853 C.Op1.getOpcode() == ISD::Constant &&
1854 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001855 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001856 if (C1 && C1->getZExtValue() == 32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001857 SDValue ShlOp0 = C.Op0.getOperand(0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001858 // See whether X has any SIGN_EXTEND_INREG uses.
Richard Sandiford28c111e2014-03-06 11:00:15 +00001859 for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001860 SDNode *N = *I;
1861 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
1862 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001863 C.Op0 = SDValue(N, 0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001864 return;
1865 }
1866 }
1867 }
1868 }
1869}
1870
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001871// If C compares the truncation of an extending load, try to compare
1872// the untruncated value instead. This exposes more opportunities to
1873// reuse CC.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001874static void adjustICmpTruncate(SelectionDAG &DAG, const SDLoc &DL,
1875 Comparison &C) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001876 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
1877 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
1878 C.Op1.getOpcode() == ISD::Constant &&
1879 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001880 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00001881 if (L->getMemoryVT().getStoreSizeInBits() <= C.Op0.getValueSizeInBits()) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001882 unsigned Type = L->getExtensionType();
1883 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
1884 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
1885 C.Op0 = C.Op0.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001886 C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType());
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001887 }
1888 }
1889 }
1890}
1891
Richard Sandiford030c1652013-09-13 09:09:50 +00001892// Return true if shift operation N has an in-range constant shift value.
1893// Store it in ShiftVal if so.
1894static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001895 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
Richard Sandiford030c1652013-09-13 09:09:50 +00001896 if (!Shift)
1897 return false;
1898
1899 uint64_t Amount = Shift->getZExtValue();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00001900 if (Amount >= N.getValueSizeInBits())
Richard Sandiford030c1652013-09-13 09:09:50 +00001901 return false;
1902
1903 ShiftVal = Amount;
1904 return true;
1905}
1906
1907// Check whether an AND with Mask is suitable for a TEST UNDER MASK
1908// instruction and whether the CC value is descriptive enough to handle
1909// a comparison of type Opcode between the AND result and CmpVal.
1910// CCMask says which comparison result is being tested and BitSize is
1911// the number of bits in the operands. If TEST UNDER MASK can be used,
1912// return the corresponding CC mask, otherwise return 0.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001913static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
1914 uint64_t Mask, uint64_t CmpVal,
1915 unsigned ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001916 assert(Mask != 0 && "ANDs with zero should have been removed by now");
1917
Richard Sandiford030c1652013-09-13 09:09:50 +00001918 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
1919 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
1920 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
1921 return 0;
1922
Richard Sandiford113c8702013-09-03 15:38:35 +00001923 // Work out the masks for the lowest and highest bits.
1924 unsigned HighShift = 63 - countLeadingZeros(Mask);
1925 uint64_t High = uint64_t(1) << HighShift;
1926 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
1927
1928 // Signed ordered comparisons are effectively unsigned if the sign
1929 // bit is dropped.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001930 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
Richard Sandiford113c8702013-09-03 15:38:35 +00001931
1932 // Check for equality comparisons with 0, or the equivalent.
1933 if (CmpVal == 0) {
1934 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1935 return SystemZ::CCMASK_TM_ALL_0;
1936 if (CCMask == SystemZ::CCMASK_CMP_NE)
1937 return SystemZ::CCMASK_TM_SOME_1;
1938 }
Ulrich Weigand4a4d4ab2016-02-01 18:31:19 +00001939 if (EffectivelyUnsigned && CmpVal > 0 && CmpVal <= Low) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001940 if (CCMask == SystemZ::CCMASK_CMP_LT)
1941 return SystemZ::CCMASK_TM_ALL_0;
1942 if (CCMask == SystemZ::CCMASK_CMP_GE)
1943 return SystemZ::CCMASK_TM_SOME_1;
1944 }
1945 if (EffectivelyUnsigned && CmpVal < Low) {
1946 if (CCMask == SystemZ::CCMASK_CMP_LE)
1947 return SystemZ::CCMASK_TM_ALL_0;
1948 if (CCMask == SystemZ::CCMASK_CMP_GT)
1949 return SystemZ::CCMASK_TM_SOME_1;
1950 }
1951
1952 // Check for equality comparisons with the mask, or the equivalent.
1953 if (CmpVal == Mask) {
1954 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1955 return SystemZ::CCMASK_TM_ALL_1;
1956 if (CCMask == SystemZ::CCMASK_CMP_NE)
1957 return SystemZ::CCMASK_TM_SOME_0;
1958 }
1959 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
1960 if (CCMask == SystemZ::CCMASK_CMP_GT)
1961 return SystemZ::CCMASK_TM_ALL_1;
1962 if (CCMask == SystemZ::CCMASK_CMP_LE)
1963 return SystemZ::CCMASK_TM_SOME_0;
1964 }
1965 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
1966 if (CCMask == SystemZ::CCMASK_CMP_GE)
1967 return SystemZ::CCMASK_TM_ALL_1;
1968 if (CCMask == SystemZ::CCMASK_CMP_LT)
1969 return SystemZ::CCMASK_TM_SOME_0;
1970 }
1971
1972 // Check for ordered comparisons with the top bit.
1973 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
1974 if (CCMask == SystemZ::CCMASK_CMP_LE)
1975 return SystemZ::CCMASK_TM_MSB_0;
1976 if (CCMask == SystemZ::CCMASK_CMP_GT)
1977 return SystemZ::CCMASK_TM_MSB_1;
1978 }
1979 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
1980 if (CCMask == SystemZ::CCMASK_CMP_LT)
1981 return SystemZ::CCMASK_TM_MSB_0;
1982 if (CCMask == SystemZ::CCMASK_CMP_GE)
1983 return SystemZ::CCMASK_TM_MSB_1;
1984 }
1985
1986 // If there are just two bits, we can do equality checks for Low and High
1987 // as well.
1988 if (Mask == Low + High) {
1989 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
1990 return SystemZ::CCMASK_TM_MIXED_MSB_0;
1991 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
1992 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
1993 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
1994 return SystemZ::CCMASK_TM_MIXED_MSB_1;
1995 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
1996 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
1997 }
1998
1999 // Looks like we've exhausted our options.
2000 return 0;
2001}
2002
Richard Sandifordd420f732013-12-13 15:28:45 +00002003// See whether C can be implemented as a TEST UNDER MASK instruction.
2004// Update the arguments with the TM version if so.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002005static void adjustForTestUnderMask(SelectionDAG &DAG, const SDLoc &DL,
2006 Comparison &C) {
Richard Sandiford113c8702013-09-03 15:38:35 +00002007 // Check that we have a comparison with a constant.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002008 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00002009 if (!ConstOp1)
Richard Sandiford35b9be22013-08-28 10:31:43 +00002010 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00002011 uint64_t CmpVal = ConstOp1->getZExtValue();
Richard Sandiford35b9be22013-08-28 10:31:43 +00002012
2013 // Check whether the nonconstant input is an AND with a constant mask.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002014 Comparison NewC(C);
2015 uint64_t MaskVal;
Craig Topper062a2ba2014-04-25 05:30:21 +00002016 ConstantSDNode *Mask = nullptr;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002017 if (C.Op0.getOpcode() == ISD::AND) {
2018 NewC.Op0 = C.Op0.getOperand(0);
2019 NewC.Op1 = C.Op0.getOperand(1);
2020 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
2021 if (!Mask)
2022 return;
2023 MaskVal = Mask->getZExtValue();
2024 } else {
2025 // There is no instruction to compare with a 64-bit immediate
2026 // so use TMHH instead if possible. We need an unsigned ordered
2027 // comparison with an i64 immediate.
2028 if (NewC.Op0.getValueType() != MVT::i64 ||
2029 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
2030 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
2031 NewC.ICmpType == SystemZICMP::SignedOnly)
2032 return;
2033 // Convert LE and GT comparisons into LT and GE.
2034 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
2035 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
2036 if (CmpVal == uint64_t(-1))
2037 return;
2038 CmpVal += 1;
2039 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
2040 }
2041 // If the low N bits of Op1 are zero than the low N bits of Op0 can
2042 // be masked off without changing the result.
2043 MaskVal = -(CmpVal & -CmpVal);
2044 NewC.ICmpType = SystemZICMP::UnsignedOnly;
2045 }
Ulrich Weigandb8d76fb2015-03-30 13:46:59 +00002046 if (!MaskVal)
2047 return;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002048
Richard Sandiford113c8702013-09-03 15:38:35 +00002049 // Check whether the combination of mask, comparison value and comparison
2050 // type are suitable.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002051 unsigned BitSize = NewC.Op0.getValueSizeInBits();
Richard Sandiford030c1652013-09-13 09:09:50 +00002052 unsigned NewCCMask, ShiftVal;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002053 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2054 NewC.Op0.getOpcode() == ISD::SHL &&
2055 isSimpleShift(NewC.Op0, ShiftVal) &&
2056 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
2057 MaskVal >> ShiftVal,
Richard Sandiford030c1652013-09-13 09:09:50 +00002058 CmpVal >> ShiftVal,
2059 SystemZICMP::Any))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002060 NewC.Op0 = NewC.Op0.getOperand(0);
2061 MaskVal >>= ShiftVal;
2062 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2063 NewC.Op0.getOpcode() == ISD::SRL &&
2064 isSimpleShift(NewC.Op0, ShiftVal) &&
2065 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
Richard Sandiford030c1652013-09-13 09:09:50 +00002066 MaskVal << ShiftVal,
2067 CmpVal << ShiftVal,
2068 SystemZICMP::UnsignedOnly))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002069 NewC.Op0 = NewC.Op0.getOperand(0);
2070 MaskVal <<= ShiftVal;
Richard Sandiford030c1652013-09-13 09:09:50 +00002071 } else {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002072 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
2073 NewC.ICmpType);
Richard Sandiford030c1652013-09-13 09:09:50 +00002074 if (!NewCCMask)
2075 return;
2076 }
Richard Sandiford113c8702013-09-03 15:38:35 +00002077
Richard Sandiford35b9be22013-08-28 10:31:43 +00002078 // Go ahead and make the change.
Richard Sandifordd420f732013-12-13 15:28:45 +00002079 C.Opcode = SystemZISD::TM;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002080 C.Op0 = NewC.Op0;
2081 if (Mask && Mask->getZExtValue() == MaskVal)
2082 C.Op1 = SDValue(Mask, 0);
2083 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002084 C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType());
Richard Sandifordd420f732013-12-13 15:28:45 +00002085 C.CCValid = SystemZ::CCMASK_TM;
2086 C.CCMask = NewCCMask;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002087}
2088
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002089// Return a Comparison that tests the condition-code result of intrinsic
2090// node Call against constant integer CC using comparison code Cond.
2091// Opcode is the opcode of the SystemZISD operation for the intrinsic
2092// and CCValid is the set of possible condition-code results.
2093static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode,
2094 SDValue Call, unsigned CCValid, uint64_t CC,
2095 ISD::CondCode Cond) {
2096 Comparison C(Call, SDValue());
2097 C.Opcode = Opcode;
2098 C.CCValid = CCValid;
2099 if (Cond == ISD::SETEQ)
2100 // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3.
2101 C.CCMask = CC < 4 ? 1 << (3 - CC) : 0;
2102 else if (Cond == ISD::SETNE)
2103 // ...and the inverse of that.
2104 C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1;
2105 else if (Cond == ISD::SETLT || Cond == ISD::SETULT)
2106 // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3,
2107 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002108 C.CCMask = CC < 4 ? ~0U << (4 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002109 else if (Cond == ISD::SETGE || Cond == ISD::SETUGE)
2110 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002111 C.CCMask = CC < 4 ? ~(~0U << (4 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002112 else if (Cond == ISD::SETLE || Cond == ISD::SETULE)
2113 // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true),
2114 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002115 C.CCMask = CC < 4 ? ~0U << (3 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002116 else if (Cond == ISD::SETGT || Cond == ISD::SETUGT)
2117 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002118 C.CCMask = CC < 4 ? ~(~0U << (3 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002119 else
2120 llvm_unreachable("Unexpected integer comparison type");
2121 C.CCMask &= CCValid;
2122 return C;
2123}
2124
Richard Sandifordd420f732013-12-13 15:28:45 +00002125// Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
2126static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002127 ISD::CondCode Cond, const SDLoc &DL) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002128 if (CmpOp1.getOpcode() == ISD::Constant) {
2129 uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue();
2130 unsigned Opcode, CCValid;
2131 if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
2132 CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) &&
2133 isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid))
2134 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002135 if (CmpOp0.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
2136 CmpOp0.getResNo() == CmpOp0->getNumValues() - 1 &&
2137 isIntrinsicWithCC(CmpOp0, Opcode, CCValid))
2138 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002139 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002140 Comparison C(CmpOp0, CmpOp1);
2141 C.CCMask = CCMaskForCondCode(Cond);
2142 if (C.Op0.getValueType().isFloatingPoint()) {
2143 C.CCValid = SystemZ::CCMASK_FCMP;
2144 C.Opcode = SystemZISD::FCMP;
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002145 adjustForFNeg(C);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002146 } else {
Richard Sandifordd420f732013-12-13 15:28:45 +00002147 C.CCValid = SystemZ::CCMASK_ICMP;
2148 C.Opcode = SystemZISD::ICMP;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002149 // Choose the type of comparison. Equality and inequality tests can
2150 // use either signed or unsigned comparisons. The choice also doesn't
2151 // matter if both sign bits are known to be clear. In those cases we
2152 // want to give the main isel code the freedom to choose whichever
2153 // form fits best.
Richard Sandifordd420f732013-12-13 15:28:45 +00002154 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
2155 C.CCMask == SystemZ::CCMASK_CMP_NE ||
2156 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
2157 C.ICmpType = SystemZICMP::Any;
2158 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
2159 C.ICmpType = SystemZICMP::UnsignedOnly;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002160 else
Richard Sandifordd420f732013-12-13 15:28:45 +00002161 C.ICmpType = SystemZICMP::SignedOnly;
2162 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002163 adjustZeroCmp(DAG, DL, C);
2164 adjustSubwordCmp(DAG, DL, C);
2165 adjustForSubtraction(DAG, DL, C);
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002166 adjustForLTGFR(C);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002167 adjustICmpTruncate(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002168 }
2169
Richard Sandifordd420f732013-12-13 15:28:45 +00002170 if (shouldSwapCmpOperands(C)) {
2171 std::swap(C.Op0, C.Op1);
2172 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford24e597b2013-08-23 11:27:19 +00002173 }
2174
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002175 adjustForTestUnderMask(DAG, DL, C);
Richard Sandifordd420f732013-12-13 15:28:45 +00002176 return C;
2177}
2178
2179// Emit the comparison instruction described by C.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002180static SDValue emitCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002181 if (!C.Op1.getNode()) {
2182 SDValue Op;
2183 switch (C.Op0.getOpcode()) {
2184 case ISD::INTRINSIC_W_CHAIN:
2185 Op = emitIntrinsicWithChainAndGlue(DAG, C.Op0, C.Opcode);
2186 break;
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002187 case ISD::INTRINSIC_WO_CHAIN:
2188 Op = emitIntrinsicWithGlue(DAG, C.Op0, C.Opcode);
2189 break;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002190 default:
2191 llvm_unreachable("Invalid comparison operands");
2192 }
2193 return SDValue(Op.getNode(), Op->getNumValues() - 1);
2194 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002195 if (C.Opcode == SystemZISD::ICMP)
2196 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002197 DAG.getConstant(C.ICmpType, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002198 if (C.Opcode == SystemZISD::TM) {
2199 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
2200 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
2201 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002202 DAG.getConstant(RegisterOnly, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002203 }
2204 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002205}
2206
Richard Sandiford7d86e472013-08-21 09:34:56 +00002207// Implement a 32-bit *MUL_LOHI operation by extending both operands to
2208// 64 bits. Extend is the extension type to use. Store the high part
2209// in Hi and the low part in Lo.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002210static void lowerMUL_LOHI32(SelectionDAG &DAG, const SDLoc &DL, unsigned Extend,
2211 SDValue Op0, SDValue Op1, SDValue &Hi,
2212 SDValue &Lo) {
Richard Sandiford7d86e472013-08-21 09:34:56 +00002213 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
2214 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
2215 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002216 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
2217 DAG.getConstant(32, DL, MVT::i64));
Richard Sandiford7d86e472013-08-21 09:34:56 +00002218 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
2219 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
2220}
2221
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002222// Lower a binary operation that produces two VT results, one in each
2223// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
2224// Extend extends Op0 to a GR128, and Opcode performs the GR128 operation
2225// on the extended Op0 and (unextended) Op1. Store the even register result
2226// in Even and the odd register result in Odd.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002227static void lowerGR128Binary(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
2228 unsigned Extend, unsigned Opcode, SDValue Op0,
2229 SDValue Op1, SDValue &Even, SDValue &Odd) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002230 SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0);
2231 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped,
2232 SDValue(In128, 0), Op1);
2233 bool Is32Bit = is32Bit(VT);
Richard Sandifordd8163202013-09-13 09:12:44 +00002234 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
2235 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002236}
2237
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002238// Return an i32 value that is 1 if the CC value produced by Glue is
2239// in the mask CCMask and 0 otherwise. CC is known to have a value
2240// in CCValid, so other values can be ignored.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002241static SDValue emitSETCC(SelectionDAG &DAG, const SDLoc &DL, SDValue Glue,
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002242 unsigned CCValid, unsigned CCMask) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002243 IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
2244 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
2245
2246 if (Conversion.XORValue)
2247 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002248 DAG.getConstant(Conversion.XORValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002249
2250 if (Conversion.AddValue)
2251 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002252 DAG.getConstant(Conversion.AddValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002253
2254 // The SHR/AND sequence should get optimized to an RISBG.
2255 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002256 DAG.getConstant(Conversion.Bit, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002257 if (Conversion.Bit != 31)
2258 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002259 DAG.getConstant(1, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002260 return Result;
2261}
2262
Ulrich Weigandcd808232015-05-05 19:26:48 +00002263// Return the SystemISD vector comparison operation for CC, or 0 if it cannot
2264// be done directly. IsFP is true if CC is for a floating-point rather than
2265// integer comparison.
2266static unsigned getVectorComparison(ISD::CondCode CC, bool IsFP) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002267 switch (CC) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002268 case ISD::SETOEQ:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002269 case ISD::SETEQ:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002270 return IsFP ? SystemZISD::VFCMPE : SystemZISD::VICMPE;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002271
Ulrich Weigandcd808232015-05-05 19:26:48 +00002272 case ISD::SETOGE:
2273 case ISD::SETGE:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002274 return IsFP ? SystemZISD::VFCMPHE : static_cast<SystemZISD::NodeType>(0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002275
2276 case ISD::SETOGT:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002277 case ISD::SETGT:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002278 return IsFP ? SystemZISD::VFCMPH : SystemZISD::VICMPH;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002279
2280 case ISD::SETUGT:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002281 return IsFP ? static_cast<SystemZISD::NodeType>(0) : SystemZISD::VICMPHL;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002282
2283 default:
2284 return 0;
2285 }
2286}
2287
2288// Return the SystemZISD vector comparison operation for CC or its inverse,
2289// or 0 if neither can be done directly. Indicate in Invert whether the
Ulrich Weigandcd808232015-05-05 19:26:48 +00002290// result is for the inverse of CC. IsFP is true if CC is for a
2291// floating-point rather than integer comparison.
2292static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, bool IsFP,
2293 bool &Invert) {
2294 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002295 Invert = false;
2296 return Opcode;
2297 }
2298
Ulrich Weigandcd808232015-05-05 19:26:48 +00002299 CC = ISD::getSetCCInverse(CC, !IsFP);
2300 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002301 Invert = true;
2302 return Opcode;
2303 }
2304
2305 return 0;
2306}
2307
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002308// Return a v2f64 that contains the extended form of elements Start and Start+1
2309// of v4f32 value Op.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002310static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, const SDLoc &DL,
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002311 SDValue Op) {
2312 int Mask[] = { Start, -1, Start + 1, -1 };
2313 Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask);
2314 return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op);
2315}
2316
2317// Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode,
2318// producing a result of type VT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002319static SDValue getVectorCmp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &DL,
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002320 EVT VT, SDValue CmpOp0, SDValue CmpOp1) {
2321 // There is no hardware support for v4f32, so extend the vector into
2322 // two v2f64s and compare those.
2323 if (CmpOp0.getValueType() == MVT::v4f32) {
2324 SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0);
2325 SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0);
2326 SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1);
2327 SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1);
2328 SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1);
2329 SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1);
2330 return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes);
2331 }
2332 return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1);
2333}
2334
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002335// Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing
2336// an integer mask of type VT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002337static SDValue lowerVectorSETCC(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002338 ISD::CondCode CC, SDValue CmpOp0,
2339 SDValue CmpOp1) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002340 bool IsFP = CmpOp0.getValueType().isFloatingPoint();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002341 bool Invert = false;
2342 SDValue Cmp;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002343 switch (CC) {
2344 // Handle tests for order using (or (ogt y x) (oge x y)).
2345 case ISD::SETUO:
2346 Invert = true;
2347 case ISD::SETO: {
2348 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002349 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2350 SDValue GE = getVectorCmp(DAG, SystemZISD::VFCMPHE, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002351 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE);
2352 break;
2353 }
2354
2355 // Handle <> tests using (or (ogt y x) (ogt x y)).
2356 case ISD::SETUEQ:
2357 Invert = true;
2358 case ISD::SETONE: {
2359 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002360 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2361 SDValue GT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002362 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT);
2363 break;
2364 }
2365
2366 // Otherwise a single comparison is enough. It doesn't really
2367 // matter whether we try the inversion or the swap first, since
2368 // there are no cases where both work.
2369 default:
2370 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002371 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002372 else {
2373 CC = ISD::getSetCCSwappedOperands(CC);
2374 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002375 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002376 else
2377 llvm_unreachable("Unhandled comparison");
2378 }
2379 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002380 }
2381 if (Invert) {
2382 SDValue Mask = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2383 DAG.getConstant(65535, DL, MVT::i32));
2384 Mask = DAG.getNode(ISD::BITCAST, DL, VT, Mask);
2385 Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask);
2386 }
2387 return Cmp;
2388}
2389
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002390SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
2391 SelectionDAG &DAG) const {
2392 SDValue CmpOp0 = Op.getOperand(0);
2393 SDValue CmpOp1 = Op.getOperand(1);
2394 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
2395 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002396 EVT VT = Op.getValueType();
2397 if (VT.isVector())
2398 return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002399
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002400 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002401 SDValue Glue = emitCmp(DAG, DL, C);
2402 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002403}
2404
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002405SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002406 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2407 SDValue CmpOp0 = Op.getOperand(2);
2408 SDValue CmpOp1 = Op.getOperand(3);
2409 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002410 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002411
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002412 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002413 SDValue Glue = emitCmp(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002414 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002415 Op.getOperand(0), DAG.getConstant(C.CCValid, DL, MVT::i32),
2416 DAG.getConstant(C.CCMask, DL, MVT::i32), Dest, Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002417}
2418
Richard Sandiford57485472013-12-13 15:35:00 +00002419// Return true if Pos is CmpOp and Neg is the negative of CmpOp,
2420// allowing Pos and Neg to be wider than CmpOp.
2421static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
2422 return (Neg.getOpcode() == ISD::SUB &&
2423 Neg.getOperand(0).getOpcode() == ISD::Constant &&
2424 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
2425 Neg.getOperand(1) == Pos &&
2426 (Pos == CmpOp ||
2427 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
2428 Pos.getOperand(0) == CmpOp)));
2429}
2430
2431// Return the absolute or negative absolute of Op; IsNegative decides which.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002432static SDValue getAbsolute(SelectionDAG &DAG, const SDLoc &DL, SDValue Op,
Richard Sandiford57485472013-12-13 15:35:00 +00002433 bool IsNegative) {
2434 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
2435 if (IsNegative)
2436 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002437 DAG.getConstant(0, DL, Op.getValueType()), Op);
Richard Sandiford57485472013-12-13 15:35:00 +00002438 return Op;
2439}
2440
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002441SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
2442 SelectionDAG &DAG) const {
2443 SDValue CmpOp0 = Op.getOperand(0);
2444 SDValue CmpOp1 = Op.getOperand(1);
2445 SDValue TrueOp = Op.getOperand(2);
2446 SDValue FalseOp = Op.getOperand(3);
2447 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002448 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002449
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002450 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandiford57485472013-12-13 15:35:00 +00002451
2452 // Check for absolute and negative-absolute selections, including those
2453 // where the comparison value is sign-extended (for LPGFR and LNGFR).
2454 // This check supplements the one in DAGCombiner.
2455 if (C.Opcode == SystemZISD::ICMP &&
2456 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
2457 C.CCMask != SystemZ::CCMASK_CMP_NE &&
2458 C.Op1.getOpcode() == ISD::Constant &&
2459 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
2460 if (isAbsolute(C.Op0, TrueOp, FalseOp))
2461 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
2462 if (isAbsolute(C.Op0, FalseOp, TrueOp))
2463 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
2464 }
2465
Richard Sandifordd420f732013-12-13 15:28:45 +00002466 SDValue Glue = emitCmp(DAG, DL, C);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002467
2468 // Special case for handling -1/0 results. The shifts we use here
2469 // should get optimized with the IPM conversion sequence.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002470 auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
2471 auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002472 if (TrueC && FalseC) {
2473 int64_t TrueVal = TrueC->getSExtValue();
2474 int64_t FalseVal = FalseC->getSExtValue();
2475 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
2476 // Invert the condition if we want -1 on false.
2477 if (TrueVal == 0)
Richard Sandifordd420f732013-12-13 15:28:45 +00002478 C.CCMask ^= C.CCValid;
2479 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002480 EVT VT = Op.getValueType();
2481 // Extend the result to VT. Upper bits are ignored.
2482 if (!is32Bit(VT))
2483 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
2484 // Sign-extend from the low bit.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002485 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002486 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
2487 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
2488 }
2489 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002490
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002491 SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, DL, MVT::i32),
2492 DAG.getConstant(C.CCMask, DL, MVT::i32), Glue};
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002493
2494 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00002495 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002496}
2497
2498SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
2499 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002500 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002501 const GlobalValue *GV = Node->getGlobal();
2502 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002503 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopher93bf97c2014-06-27 07:38:01 +00002504 CodeModel::Model CM = DAG.getTarget().getCodeModel();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002505
2506 SDValue Result;
Rafael Espindola3beef8d2016-06-27 23:15:57 +00002507 if (Subtarget.isPC32DBLSymbol(GV, CM)) {
Richard Sandiford54b36912013-09-27 15:14:04 +00002508 // Assign anchors at 1<<12 byte boundaries.
2509 uint64_t Anchor = Offset & ~uint64_t(0xfff);
2510 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
2511 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2512
2513 // The offset can be folded into the address if it is aligned to a halfword.
2514 Offset -= Anchor;
2515 if (Offset != 0 && (Offset & 1) == 0) {
2516 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
2517 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002518 Offset = 0;
2519 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002520 } else {
2521 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
2522 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2523 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Justin Lebar9c375812016-07-15 18:27:10 +00002524 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002525 }
2526
2527 // If there was a non-zero offset that we didn't fold, create an explicit
2528 // addition for it.
2529 if (Offset != 0)
2530 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002531 DAG.getConstant(Offset, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002532
2533 return Result;
2534}
2535
Ulrich Weigand7db69182015-02-18 09:13:27 +00002536SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
2537 SelectionDAG &DAG,
2538 unsigned Opcode,
2539 SDValue GOTOffset) const {
2540 SDLoc DL(Node);
Mehdi Amini44ede332015-07-09 02:09:04 +00002541 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand7db69182015-02-18 09:13:27 +00002542 SDValue Chain = DAG.getEntryNode();
2543 SDValue Glue;
2544
2545 // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
2546 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
2547 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
2548 Glue = Chain.getValue(1);
2549 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
2550 Glue = Chain.getValue(1);
2551
2552 // The first call operand is the chain and the second is the TLS symbol.
2553 SmallVector<SDValue, 8> Ops;
2554 Ops.push_back(Chain);
2555 Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
2556 Node->getValueType(0),
2557 0, 0));
2558
2559 // Add argument registers to the end of the list so that they are
2560 // known live into the call.
2561 Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
2562 Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
2563
2564 // Add a register mask operand representing the call-preserved registers.
2565 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002566 const uint32_t *Mask =
2567 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002568 assert(Mask && "Missing call preserved mask for calling convention");
2569 Ops.push_back(DAG.getRegisterMask(Mask));
2570
2571 // Glue the call to the argument copies.
2572 Ops.push_back(Glue);
2573
2574 // Emit the call.
2575 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2576 Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
2577 Glue = Chain.getValue(1);
2578
2579 // Copy the return value from %r2.
2580 return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
2581}
2582
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002583SDValue SystemZTargetLowering::lowerThreadPointer(const SDLoc &DL,
2584 SelectionDAG &DAG) const {
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002585 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002586 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002587
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002588 // The high part of the thread pointer is in access register 0.
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002589 SDValue TPHi = DAG.getCopyFromReg(Chain, DL, SystemZ::A0, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002590 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
2591
2592 // The low part of the thread pointer is in access register 1.
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002593 SDValue TPLo = DAG.getCopyFromReg(Chain, DL, SystemZ::A1, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002594 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
2595
2596 // Merge them into a single 64-bit address.
2597 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002598 DAG.getConstant(32, DL, PtrVT));
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002599 return DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
2600}
2601
2602SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
2603 SelectionDAG &DAG) const {
2604 if (DAG.getTarget().Options.EmulatedTLS)
2605 return LowerToTLSEmulatedModel(Node, DAG);
2606 SDLoc DL(Node);
2607 const GlobalValue *GV = Node->getGlobal();
2608 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2609 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
2610
2611 SDValue TP = lowerThreadPointer(DL, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002612
Ulrich Weigand7db69182015-02-18 09:13:27 +00002613 // Get the offset of GA from the thread pointer, based on the TLS model.
2614 SDValue Offset;
2615 switch (model) {
2616 case TLSModel::GeneralDynamic: {
2617 // Load the GOT offset of the tls_index (module ID / per-symbol offset).
2618 SystemZConstantPoolValue *CPV =
2619 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002620
Ulrich Weigand7db69182015-02-18 09:13:27 +00002621 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002622 Offset = DAG.getLoad(
2623 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002624 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002625
2626 // Call __tls_get_offset to retrieve the offset.
2627 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
2628 break;
2629 }
2630
2631 case TLSModel::LocalDynamic: {
2632 // Load the GOT offset of the module ID.
2633 SystemZConstantPoolValue *CPV =
2634 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
2635
2636 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002637 Offset = DAG.getLoad(
2638 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002639 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002640
2641 // Call __tls_get_offset to retrieve the module base offset.
2642 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
2643
2644 // Note: The SystemZLDCleanupPass will remove redundant computations
2645 // of the module base offset. Count total number of local-dynamic
2646 // accesses to trigger execution of that pass.
2647 SystemZMachineFunctionInfo* MFI =
2648 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
2649 MFI->incNumLocalDynamicTLSAccesses();
2650
2651 // Add the per-symbol offset.
2652 CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
2653
2654 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002655 DTPOffset = DAG.getLoad(
2656 PtrVT, DL, DAG.getEntryNode(), DTPOffset,
Justin Lebar9c375812016-07-15 18:27:10 +00002657 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002658
2659 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
2660 break;
2661 }
2662
2663 case TLSModel::InitialExec: {
2664 // Load the offset from the GOT.
2665 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2666 SystemZII::MO_INDNTPOFF);
2667 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +00002668 Offset =
2669 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Offset,
2670 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002671 break;
2672 }
2673
2674 case TLSModel::LocalExec: {
2675 // Force the offset into the constant pool and load it from there.
2676 SystemZConstantPoolValue *CPV =
2677 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
2678
2679 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002680 Offset = DAG.getLoad(
2681 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002682 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002683 break;
Ulrich Weigandb7e59092015-02-18 09:42:23 +00002684 }
Ulrich Weigand7db69182015-02-18 09:13:27 +00002685 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002686
2687 // Add the base and offset together.
2688 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
2689}
2690
2691SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
2692 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002693 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002694 const BlockAddress *BA = Node->getBlockAddress();
2695 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002696 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002697
2698 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
2699 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2700 return Result;
2701}
2702
2703SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
2704 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002705 SDLoc DL(JT);
Mehdi Amini44ede332015-07-09 02:09:04 +00002706 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002707 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
2708
2709 // Use LARL to load the address of the table.
2710 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2711}
2712
2713SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
2714 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002715 SDLoc DL(CP);
Mehdi Amini44ede332015-07-09 02:09:04 +00002716 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002717
2718 SDValue Result;
2719 if (CP->isMachineConstantPoolEntry())
2720 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002721 CP->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002722 else
2723 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002724 CP->getAlignment(), CP->getOffset());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002725
2726 // Use LARL to load the address of the constant pool entry.
2727 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2728}
2729
Ulrich Weigandf557d082016-04-04 12:44:55 +00002730SDValue SystemZTargetLowering::lowerFRAMEADDR(SDValue Op,
2731 SelectionDAG &DAG) const {
2732 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002733 MachineFrameInfo &MFI = MF.getFrameInfo();
2734 MFI.setFrameAddressIsTaken(true);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002735
2736 SDLoc DL(Op);
2737 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2738 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2739
2740 // If the back chain frame index has not been allocated yet, do so.
2741 SystemZMachineFunctionInfo *FI = MF.getInfo<SystemZMachineFunctionInfo>();
2742 int BackChainIdx = FI->getFramePointerSaveIndex();
2743 if (!BackChainIdx) {
2744 // By definition, the frame address is the address of the back chain.
Matthias Braun941a7052016-07-28 18:40:00 +00002745 BackChainIdx = MFI.CreateFixedObject(8, -SystemZMC::CallFrameSize, false);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002746 FI->setFramePointerSaveIndex(BackChainIdx);
2747 }
2748 SDValue BackChain = DAG.getFrameIndex(BackChainIdx, PtrVT);
2749
2750 // FIXME The frontend should detect this case.
2751 if (Depth > 0) {
2752 report_fatal_error("Unsupported stack frame traversal count");
2753 }
2754
2755 return BackChain;
2756}
2757
2758SDValue SystemZTargetLowering::lowerRETURNADDR(SDValue Op,
2759 SelectionDAG &DAG) const {
2760 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002761 MachineFrameInfo &MFI = MF.getFrameInfo();
2762 MFI.setReturnAddressIsTaken(true);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002763
2764 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
2765 return SDValue();
2766
2767 SDLoc DL(Op);
2768 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2769 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2770
2771 // FIXME The frontend should detect this case.
2772 if (Depth > 0) {
2773 report_fatal_error("Unsupported stack frame traversal count");
2774 }
2775
2776 // Return R14D, which has the return address. Mark it an implicit live-in.
2777 unsigned LinkReg = MF.addLiveIn(SystemZ::R14D, &SystemZ::GR64BitRegClass);
2778 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, LinkReg, PtrVT);
2779}
2780
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002781SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
2782 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002783 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002784 SDValue In = Op.getOperand(0);
2785 EVT InVT = In.getValueType();
2786 EVT ResVT = Op.getValueType();
2787
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002788 // Convert loads directly. This is normally done by DAGCombiner,
2789 // but we need this case for bitcasts that are created during lowering
2790 // and which are then lowered themselves.
2791 if (auto *LoadN = dyn_cast<LoadSDNode>(In))
2792 return DAG.getLoad(ResVT, DL, LoadN->getChain(), LoadN->getBasePtr(),
2793 LoadN->getMemOperand());
2794
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002795 if (InVT == MVT::i32 && ResVT == MVT::f32) {
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002796 SDValue In64;
2797 if (Subtarget.hasHighWord()) {
2798 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
2799 MVT::i64);
2800 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
2801 MVT::i64, SDValue(U64, 0), In);
2802 } else {
2803 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
2804 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002805 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002806 }
2807 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002808 return DAG.getTargetExtractSubreg(SystemZ::subreg_r32,
Richard Sandifordd8163202013-09-13 09:12:44 +00002809 DL, MVT::f32, Out64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002810 }
2811 if (InVT == MVT::f32 && ResVT == MVT::i32) {
2812 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002813 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002814 MVT::f64, SDValue(U64, 0), In);
2815 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002816 if (Subtarget.hasHighWord())
2817 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
2818 MVT::i32, Out64);
2819 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002820 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002821 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002822 }
2823 llvm_unreachable("Unexpected bitcast combination");
2824}
2825
2826SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
2827 SelectionDAG &DAG) const {
2828 MachineFunction &MF = DAG.getMachineFunction();
2829 SystemZMachineFunctionInfo *FuncInfo =
2830 MF.getInfo<SystemZMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002831 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002832
2833 SDValue Chain = Op.getOperand(0);
2834 SDValue Addr = Op.getOperand(1);
2835 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002836 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002837
2838 // The initial values of each field.
2839 const unsigned NumFields = 4;
2840 SDValue Fields[NumFields] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002841 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT),
2842 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002843 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
2844 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
2845 };
2846
2847 // Store each field into its respective slot.
2848 SDValue MemOps[NumFields];
2849 unsigned Offset = 0;
2850 for (unsigned I = 0; I < NumFields; ++I) {
2851 SDValue FieldAddr = Addr;
2852 if (Offset != 0)
2853 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002854 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002855 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00002856 MachinePointerInfo(SV, Offset));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002857 Offset += 8;
2858 }
Craig Topper48d114b2014-04-26 18:35:24 +00002859 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002860}
2861
2862SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
2863 SelectionDAG &DAG) const {
2864 SDValue Chain = Op.getOperand(0);
2865 SDValue DstPtr = Op.getOperand(1);
2866 SDValue SrcPtr = Op.getOperand(2);
2867 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
2868 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002869 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002870
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002871 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002872 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00002873 /*isTailCall*/false,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002874 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
2875}
2876
2877SDValue SystemZTargetLowering::
2878lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002879 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00002880 MachineFunction &MF = DAG.getMachineFunction();
2881 bool RealignOpt = !MF.getFunction()-> hasFnAttribute("no-realign-stack");
2882 bool StoreBackchain = MF.getFunction()->hasFnAttribute("backchain");
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002883
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002884 SDValue Chain = Op.getOperand(0);
2885 SDValue Size = Op.getOperand(1);
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002886 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002887 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002888
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002889 // If user has set the no alignment function attribute, ignore
2890 // alloca alignments.
2891 uint64_t AlignVal = (RealignOpt ?
2892 dyn_cast<ConstantSDNode>(Align)->getZExtValue() : 0);
2893
2894 uint64_t StackAlign = TFI->getStackAlignment();
2895 uint64_t RequiredAlign = std::max(AlignVal, StackAlign);
2896 uint64_t ExtraAlignSpace = RequiredAlign - StackAlign;
2897
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002898 unsigned SPReg = getStackPointerRegisterToSaveRestore();
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002899 SDValue NeededSpace = Size;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002900
2901 // Get a reference to the stack pointer.
2902 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
2903
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00002904 // If we need a backchain, save it now.
2905 SDValue Backchain;
2906 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00002907 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00002908
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002909 // Add extra space for alignment if needed.
2910 if (ExtraAlignSpace)
2911 NeededSpace = DAG.getNode(ISD::ADD, DL, MVT::i64, NeededSpace,
Elliot Colpbc2cfc22016-07-06 18:13:11 +00002912 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002913
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002914 // Get the new stack pointer value.
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002915 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, NeededSpace);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002916
2917 // Copy the new stack pointer back.
2918 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
2919
2920 // The allocated data lives above the 160 bytes allocated for the standard
2921 // frame, plus any outgoing stack arguments. We don't know how much that
2922 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
2923 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
2924 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
2925
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002926 // Dynamically realign if needed.
2927 if (RequiredAlign > StackAlign) {
2928 Result =
2929 DAG.getNode(ISD::ADD, DL, MVT::i64, Result,
2930 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
2931 Result =
2932 DAG.getNode(ISD::AND, DL, MVT::i64, Result,
2933 DAG.getConstant(~(RequiredAlign - 1), DL, MVT::i64));
2934 }
2935
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00002936 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00002937 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00002938
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002939 SDValue Ops[2] = { Result, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002940 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002941}
2942
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +00002943SDValue SystemZTargetLowering::lowerGET_DYNAMIC_AREA_OFFSET(
2944 SDValue Op, SelectionDAG &DAG) const {
2945 SDLoc DL(Op);
2946
2947 return DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
2948}
2949
Richard Sandiford7d86e472013-08-21 09:34:56 +00002950SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
2951 SelectionDAG &DAG) const {
2952 EVT VT = Op.getValueType();
2953 SDLoc DL(Op);
2954 SDValue Ops[2];
2955 if (is32Bit(VT))
2956 // Just do a normal 64-bit multiplication and extract the results.
2957 // We define this so that it can be used for constant division.
2958 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
2959 Op.getOperand(1), Ops[1], Ops[0]);
2960 else {
2961 // Do a full 128-bit multiplication based on UMUL_LOHI64:
2962 //
2963 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
2964 //
2965 // but using the fact that the upper halves are either all zeros
2966 // or all ones:
2967 //
2968 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
2969 //
2970 // and grouping the right terms together since they are quicker than the
2971 // multiplication:
2972 //
2973 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002974 SDValue C63 = DAG.getConstant(63, DL, MVT::i64);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002975 SDValue LL = Op.getOperand(0);
2976 SDValue RL = Op.getOperand(1);
2977 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
2978 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
2979 // UMUL_LOHI64 returns the low result in the odd register and the high
2980 // result in the even register. SMUL_LOHI is defined to return the
2981 // low half first, so the results are in reverse order.
2982 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2983 LL, RL, Ops[1], Ops[0]);
2984 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
2985 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
2986 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
2987 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
2988 }
Craig Topper64941d92014-04-27 19:20:57 +00002989 return DAG.getMergeValues(Ops, DL);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002990}
2991
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002992SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
2993 SelectionDAG &DAG) const {
2994 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002995 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002996 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00002997 if (is32Bit(VT))
2998 // Just do a normal 64-bit multiplication and extract the results.
2999 // We define this so that it can be used for constant division.
3000 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
3001 Op.getOperand(1), Ops[1], Ops[0]);
3002 else
3003 // UMUL_LOHI64 returns the low result in the odd register and the high
3004 // result in the even register. UMUL_LOHI is defined to return the
3005 // low half first, so the results are in reverse order.
3006 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
3007 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003008 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003009}
3010
3011SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
3012 SelectionDAG &DAG) const {
3013 SDValue Op0 = Op.getOperand(0);
3014 SDValue Op1 = Op.getOperand(1);
3015 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003016 SDLoc DL(Op);
Richard Sandiforde6e78852013-07-02 15:40:22 +00003017 unsigned Opcode;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003018
3019 // We use DSGF for 32-bit division.
3020 if (is32Bit(VT)) {
3021 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Richard Sandiforde6e78852013-07-02 15:40:22 +00003022 Opcode = SystemZISD::SDIVREM32;
3023 } else if (DAG.ComputeNumSignBits(Op1) > 32) {
3024 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
3025 Opcode = SystemZISD::SDIVREM32;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00003026 } else
Richard Sandiforde6e78852013-07-02 15:40:22 +00003027 Opcode = SystemZISD::SDIVREM64;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003028
3029 // DSG(F) takes a 64-bit dividend, so the even register in the GR128
3030 // input is "don't care". The instruction returns the remainder in
3031 // the even register and the quotient in the odd register.
3032 SDValue Ops[2];
Richard Sandiforde6e78852013-07-02 15:40:22 +00003033 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003034 Op0, Op1, Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003035 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003036}
3037
3038SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
3039 SelectionDAG &DAG) const {
3040 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003041 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003042
3043 // DL(G) uses a double-width dividend, so we need to clear the even
3044 // register in the GR128 input. The instruction returns the remainder
3045 // in the even register and the quotient in the odd register.
3046 SDValue Ops[2];
3047 if (is32Bit(VT))
3048 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
3049 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
3050 else
3051 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
3052 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003053 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003054}
3055
3056SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
3057 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
3058
3059 // Get the known-zero masks for each operand.
3060 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
3061 APInt KnownZero[2], KnownOne[2];
Jay Foada0653a32014-05-14 21:14:37 +00003062 DAG.computeKnownBits(Ops[0], KnownZero[0], KnownOne[0]);
3063 DAG.computeKnownBits(Ops[1], KnownZero[1], KnownOne[1]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003064
3065 // See if the upper 32 bits of one operand and the lower 32 bits of the
3066 // other are known zero. They are the low and high operands respectively.
3067 uint64_t Masks[] = { KnownZero[0].getZExtValue(),
3068 KnownZero[1].getZExtValue() };
3069 unsigned High, Low;
3070 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
3071 High = 1, Low = 0;
3072 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
3073 High = 0, Low = 1;
3074 else
3075 return Op;
3076
3077 SDValue LowOp = Ops[Low];
3078 SDValue HighOp = Ops[High];
3079
3080 // If the high part is a constant, we're better off using IILH.
3081 if (HighOp.getOpcode() == ISD::Constant)
3082 return Op;
3083
3084 // If the low part is a constant that is outside the range of LHI,
3085 // then we're better off using IILF.
3086 if (LowOp.getOpcode() == ISD::Constant) {
3087 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
3088 if (!isInt<16>(Value))
3089 return Op;
3090 }
3091
3092 // Check whether the high part is an AND that doesn't change the
3093 // high 32 bits and just masks out low bits. We can skip it if so.
3094 if (HighOp.getOpcode() == ISD::AND &&
3095 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
Richard Sandifordccc2a7c2013-12-03 11:01:54 +00003096 SDValue HighOp0 = HighOp.getOperand(0);
3097 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
3098 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
3099 HighOp = HighOp0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003100 }
3101
3102 // Take advantage of the fact that all GR32 operations only change the
3103 // low 32 bits by truncating Low to an i32 and inserting it directly
3104 // using a subreg. The interesting cases are those where the truncation
3105 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003106 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003107 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
Richard Sandiford87a44362013-09-30 10:28:35 +00003108 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00003109 MVT::i64, HighOp, Low32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003110}
3111
Ulrich Weigandb4012182015-03-31 12:56:33 +00003112SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
3113 SelectionDAG &DAG) const {
3114 EVT VT = Op.getValueType();
Ulrich Weigandb4012182015-03-31 12:56:33 +00003115 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003116 Op = Op.getOperand(0);
3117
3118 // Handle vector types via VPOPCT.
3119 if (VT.isVector()) {
3120 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op);
3121 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00003122 switch (VT.getScalarSizeInBits()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003123 case 8:
3124 break;
3125 case 16: {
3126 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
3127 SDValue Shift = DAG.getConstant(8, DL, MVT::i32);
3128 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift);
3129 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3130 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift);
3131 break;
3132 }
3133 case 32: {
3134 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3135 DAG.getConstant(0, DL, MVT::i32));
3136 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3137 break;
3138 }
3139 case 64: {
3140 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3141 DAG.getConstant(0, DL, MVT::i32));
3142 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp);
3143 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3144 break;
3145 }
3146 default:
3147 llvm_unreachable("Unexpected type");
3148 }
3149 return Op;
3150 }
Ulrich Weigandb4012182015-03-31 12:56:33 +00003151
3152 // Get the known-zero mask for the operand.
Ulrich Weigandb4012182015-03-31 12:56:33 +00003153 APInt KnownZero, KnownOne;
3154 DAG.computeKnownBits(Op, KnownZero, KnownOne);
Ulrich Weigand050527b2015-03-31 19:28:50 +00003155 unsigned NumSignificantBits = (~KnownZero).getActiveBits();
3156 if (NumSignificantBits == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003157 return DAG.getConstant(0, DL, VT);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003158
3159 // Skip known-zero high parts of the operand.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003160 int64_t OrigBitSize = VT.getSizeInBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003161 int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits);
3162 BitSize = std::min(BitSize, OrigBitSize);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003163
3164 // The POPCNT instruction counts the number of bits in each byte.
3165 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op);
3166 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op);
3167 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
3168
3169 // Add up per-byte counts in a binary tree. All bits of Op at
3170 // position larger than BitSize remain zero throughout.
3171 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003172 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003173 if (BitSize != OrigBitSize)
3174 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003175 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003176 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3177 }
3178
3179 // Extract overall result from high byte.
3180 if (BitSize > 8)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003181 Op = DAG.getNode(ISD::SRL, DL, VT, Op,
3182 DAG.getConstant(BitSize - 8, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003183
3184 return Op;
3185}
3186
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003187SDValue SystemZTargetLowering::lowerATOMIC_FENCE(SDValue Op,
3188 SelectionDAG &DAG) const {
3189 SDLoc DL(Op);
3190 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
3191 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
3192 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
3193 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
3194
3195 // The only fence that needs an instruction is a sequentially-consistent
3196 // cross-thread fence.
JF Bastien800f87a2016-04-06 21:19:33 +00003197 if (FenceOrdering == AtomicOrdering::SequentiallyConsistent &&
3198 FenceScope == CrossThread) {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003199 return SDValue(DAG.getMachineNode(SystemZ::Serialize, DL, MVT::Other,
JF Bastien800f87a2016-04-06 21:19:33 +00003200 Op.getOperand(0)),
3201 0);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003202 }
3203
3204 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
3205 return DAG.getNode(SystemZISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0));
3206}
3207
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003208// Op is an atomic load. Lower it into a normal volatile load.
3209SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
3210 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003211 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003212 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
3213 Node->getChain(), Node->getBasePtr(),
3214 Node->getMemoryVT(), Node->getMemOperand());
3215}
3216
3217// Op is an atomic store. Lower it into a normal volatile store followed
3218// by a serialization.
3219SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
3220 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003221 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003222 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
3223 Node->getBasePtr(), Node->getMemoryVT(),
3224 Node->getMemOperand());
3225 return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other,
3226 Chain), 0);
3227}
3228
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003229// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
3230// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003231SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
3232 SelectionDAG &DAG,
3233 unsigned Opcode) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003234 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003235
3236 // 32-bit operations need no code outside the main loop.
3237 EVT NarrowVT = Node->getMemoryVT();
3238 EVT WideVT = MVT::i32;
3239 if (NarrowVT == WideVT)
3240 return Op;
3241
3242 int64_t BitSize = NarrowVT.getSizeInBits();
3243 SDValue ChainIn = Node->getChain();
3244 SDValue Addr = Node->getBasePtr();
3245 SDValue Src2 = Node->getVal();
3246 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003247 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003248 EVT PtrVT = Addr.getValueType();
3249
3250 // Convert atomic subtracts of constants into additions.
3251 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
Richard Sandiford21f5d682014-03-06 11:22:58 +00003252 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003253 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003254 Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003255 }
3256
3257 // Get the address of the containing word.
3258 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003259 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003260
3261 // Get the number of bits that the word must be rotated left in order
3262 // to bring the field to the top bits of a GR32.
3263 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003264 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003265 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3266
3267 // Get the complementing shift amount, for rotating a field in the top
3268 // bits back to its proper position.
3269 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003270 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003271
3272 // Extend the source operand to 32 bits and prepare it for the inner loop.
3273 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
3274 // operations require the source to be shifted in advance. (This shift
3275 // can be folded if the source is constant.) For AND and NAND, the lower
3276 // bits must be set, while for other opcodes they should be left clear.
3277 if (Opcode != SystemZISD::ATOMIC_SWAPW)
3278 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003279 DAG.getConstant(32 - BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003280 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
3281 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
3282 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003283 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003284
3285 // Construct the ATOMIC_LOADW_* node.
3286 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3287 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003288 DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003289 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003290 NarrowVT, MMO);
3291
3292 // Rotate the result of the final CS so that the field is in the lower
3293 // bits of a GR32, then truncate it.
3294 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003295 DAG.getConstant(BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003296 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
3297
3298 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00003299 return DAG.getMergeValues(RetOps, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003300}
3301
Richard Sandiford41350a52013-12-24 15:18:04 +00003302// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
Richard Sandiford002019a2013-12-24 15:22:39 +00003303// into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
Richard Sandiford41350a52013-12-24 15:18:04 +00003304// operations into additions.
3305SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
3306 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003307 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandiford41350a52013-12-24 15:18:04 +00003308 EVT MemVT = Node->getMemoryVT();
3309 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
3310 // A full-width operation.
3311 assert(Op.getValueType() == MemVT && "Mismatched VTs");
3312 SDValue Src2 = Node->getVal();
3313 SDValue NegSrc2;
3314 SDLoc DL(Src2);
3315
Richard Sandiford21f5d682014-03-06 11:22:58 +00003316 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
Richard Sandiford41350a52013-12-24 15:18:04 +00003317 // Use an addition if the operand is constant and either LAA(G) is
3318 // available or the negative value is in the range of A(G)FHI.
3319 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
Eric Christopher93bf97c2014-06-27 07:38:01 +00003320 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003321 NegSrc2 = DAG.getConstant(Value, DL, MemVT);
Eric Christopher93bf97c2014-06-27 07:38:01 +00003322 } else if (Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00003323 // Use LAA(G) if available.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003324 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT),
Richard Sandiford41350a52013-12-24 15:18:04 +00003325 Src2);
3326
3327 if (NegSrc2.getNode())
3328 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
3329 Node->getChain(), Node->getBasePtr(), NegSrc2,
Konstantin Zhuravlyov8ea02462016-10-15 22:01:18 +00003330 Node->getMemOperand());
Richard Sandiford41350a52013-12-24 15:18:04 +00003331
3332 // Use the node as-is.
3333 return Op;
3334 }
3335
3336 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
3337}
3338
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003339// Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
3340// into a fullword ATOMIC_CMP_SWAPW operation.
3341SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
3342 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003343 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003344
3345 // We have native support for 32-bit compare and swap.
3346 EVT NarrowVT = Node->getMemoryVT();
3347 EVT WideVT = MVT::i32;
3348 if (NarrowVT == WideVT)
3349 return Op;
3350
3351 int64_t BitSize = NarrowVT.getSizeInBits();
3352 SDValue ChainIn = Node->getOperand(0);
3353 SDValue Addr = Node->getOperand(1);
3354 SDValue CmpVal = Node->getOperand(2);
3355 SDValue SwapVal = Node->getOperand(3);
3356 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003357 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003358 EVT PtrVT = Addr.getValueType();
3359
3360 // Get the address of the containing word.
3361 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003362 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003363
3364 // Get the number of bits that the word must be rotated left in order
3365 // to bring the field to the top bits of a GR32.
3366 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003367 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003368 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3369
3370 // Get the complementing shift amount, for rotating a field in the top
3371 // bits back to its proper position.
3372 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003373 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003374
3375 // Construct the ATOMIC_CMP_SWAPW node.
3376 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3377 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003378 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003379 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003380 VTList, Ops, NarrowVT, MMO);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003381 return AtomicOp;
3382}
3383
3384SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
3385 SelectionDAG &DAG) const {
3386 MachineFunction &MF = DAG.getMachineFunction();
3387 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003388 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003389 SystemZ::R15D, Op.getValueType());
3390}
3391
3392SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
3393 SelectionDAG &DAG) const {
3394 MachineFunction &MF = DAG.getMachineFunction();
3395 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003396 bool StoreBackchain = MF.getFunction()->hasFnAttribute("backchain");
3397
3398 SDValue Chain = Op.getOperand(0);
3399 SDValue NewSP = Op.getOperand(1);
3400 SDValue Backchain;
3401 SDLoc DL(Op);
3402
3403 if (StoreBackchain) {
3404 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, MVT::i64);
Justin Lebar9c375812016-07-15 18:27:10 +00003405 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003406 }
3407
3408 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R15D, NewSP);
3409
3410 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003411 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003412
3413 return Chain;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003414}
3415
Richard Sandiford03481332013-08-23 11:36:42 +00003416SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
3417 SelectionDAG &DAG) const {
3418 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3419 if (!IsData)
3420 // Just preserve the chain.
3421 return Op.getOperand(0);
3422
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003423 SDLoc DL(Op);
Richard Sandiford03481332013-08-23 11:36:42 +00003424 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
3425 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
Richard Sandiford21f5d682014-03-06 11:22:58 +00003426 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
Richard Sandiford03481332013-08-23 11:36:42 +00003427 SDValue Ops[] = {
3428 Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003429 DAG.getConstant(Code, DL, MVT::i32),
Richard Sandiford03481332013-08-23 11:36:42 +00003430 Op.getOperand(1)
3431 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003432 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003433 Node->getVTList(), Ops,
Richard Sandiford03481332013-08-23 11:36:42 +00003434 Node->getMemoryVT(), Node->getMemOperand());
3435}
3436
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003437// Return an i32 that contains the value of CC immediately after After,
3438// whose final operand must be MVT::Glue.
3439static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003440 SDLoc DL(After);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003441 SDValue Glue = SDValue(After, After->getNumValues() - 1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003442 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
3443 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
3444 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003445}
3446
3447SDValue
3448SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
3449 SelectionDAG &DAG) const {
3450 unsigned Opcode, CCValid;
3451 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) {
3452 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
3453 SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode);
3454 SDValue CC = getCCResult(DAG, Glued.getNode());
3455 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC);
3456 return SDValue();
3457 }
3458
3459 return SDValue();
3460}
3461
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003462SDValue
3463SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
3464 SelectionDAG &DAG) const {
3465 unsigned Opcode, CCValid;
3466 if (isIntrinsicWithCC(Op, Opcode, CCValid)) {
3467 SDValue Glued = emitIntrinsicWithGlue(DAG, Op, Opcode);
3468 SDValue CC = getCCResult(DAG, Glued.getNode());
3469 if (Op->getNumValues() == 1)
3470 return CC;
3471 assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result");
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00003472 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), Glued,
3473 CC);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003474 }
3475
3476 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3477 switch (Id) {
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00003478 case Intrinsic::thread_pointer:
3479 return lowerThreadPointer(SDLoc(Op), DAG);
3480
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003481 case Intrinsic::s390_vpdi:
3482 return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(),
3483 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3484
3485 case Intrinsic::s390_vperm:
3486 return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(),
3487 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3488
3489 case Intrinsic::s390_vuphb:
3490 case Intrinsic::s390_vuphh:
3491 case Intrinsic::s390_vuphf:
3492 return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(),
3493 Op.getOperand(1));
3494
3495 case Intrinsic::s390_vuplhb:
3496 case Intrinsic::s390_vuplhh:
3497 case Intrinsic::s390_vuplhf:
3498 return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(),
3499 Op.getOperand(1));
3500
3501 case Intrinsic::s390_vuplb:
3502 case Intrinsic::s390_vuplhw:
3503 case Intrinsic::s390_vuplf:
3504 return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(),
3505 Op.getOperand(1));
3506
3507 case Intrinsic::s390_vupllb:
3508 case Intrinsic::s390_vupllh:
3509 case Intrinsic::s390_vupllf:
3510 return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(),
3511 Op.getOperand(1));
3512
3513 case Intrinsic::s390_vsumb:
3514 case Intrinsic::s390_vsumh:
3515 case Intrinsic::s390_vsumgh:
3516 case Intrinsic::s390_vsumgf:
3517 case Intrinsic::s390_vsumqf:
3518 case Intrinsic::s390_vsumqg:
3519 return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(),
3520 Op.getOperand(1), Op.getOperand(2));
3521 }
3522
3523 return SDValue();
3524}
3525
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003526namespace {
3527// Says that SystemZISD operation Opcode can be used to perform the equivalent
3528// of a VPERM with permute vector Bytes. If Opcode takes three operands,
3529// Operand is the constant third operand, otherwise it is the number of
3530// bytes in each element of the result.
3531struct Permute {
3532 unsigned Opcode;
3533 unsigned Operand;
3534 unsigned char Bytes[SystemZ::VectorBytes];
3535};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003536}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003537
3538static const Permute PermuteForms[] = {
3539 // VMRHG
3540 { SystemZISD::MERGE_HIGH, 8,
3541 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } },
3542 // VMRHF
3543 { SystemZISD::MERGE_HIGH, 4,
3544 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
3545 // VMRHH
3546 { SystemZISD::MERGE_HIGH, 2,
3547 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
3548 // VMRHB
3549 { SystemZISD::MERGE_HIGH, 1,
3550 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
3551 // VMRLG
3552 { SystemZISD::MERGE_LOW, 8,
3553 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } },
3554 // VMRLF
3555 { SystemZISD::MERGE_LOW, 4,
3556 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
3557 // VMRLH
3558 { SystemZISD::MERGE_LOW, 2,
3559 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
3560 // VMRLB
3561 { SystemZISD::MERGE_LOW, 1,
3562 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
3563 // VPKG
3564 { SystemZISD::PACK, 4,
3565 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } },
3566 // VPKF
3567 { SystemZISD::PACK, 2,
3568 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
3569 // VPKH
3570 { SystemZISD::PACK, 1,
3571 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
3572 // VPDI V1, V2, 4 (low half of V1, high half of V2)
3573 { SystemZISD::PERMUTE_DWORDS, 4,
3574 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } },
3575 // VPDI V1, V2, 1 (high half of V1, low half of V2)
3576 { SystemZISD::PERMUTE_DWORDS, 1,
3577 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } }
3578};
3579
3580// Called after matching a vector shuffle against a particular pattern.
3581// Both the original shuffle and the pattern have two vector operands.
3582// OpNos[0] is the operand of the original shuffle that should be used for
3583// operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything.
3584// OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and
3585// set OpNo0 and OpNo1 to the shuffle operands that should actually be used
3586// for operands 0 and 1 of the pattern.
3587static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) {
3588 if (OpNos[0] < 0) {
3589 if (OpNos[1] < 0)
3590 return false;
3591 OpNo0 = OpNo1 = OpNos[1];
3592 } else if (OpNos[1] < 0) {
3593 OpNo0 = OpNo1 = OpNos[0];
3594 } else {
3595 OpNo0 = OpNos[0];
3596 OpNo1 = OpNos[1];
3597 }
3598 return true;
3599}
3600
3601// Bytes is a VPERM-like permute vector, except that -1 is used for
3602// undefined bytes. Return true if the VPERM can be implemented using P.
3603// When returning true set OpNo0 to the VPERM operand that should be
3604// used for operand 0 of P and likewise OpNo1 for operand 1 of P.
3605//
3606// For example, if swapping the VPERM operands allows P to match, OpNo0
3607// will be 1 and OpNo1 will be 0. If instead Bytes only refers to one
3608// operand, but rewriting it to use two duplicated operands allows it to
3609// match P, then OpNo0 and OpNo1 will be the same.
3610static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P,
3611 unsigned &OpNo0, unsigned &OpNo1) {
3612 int OpNos[] = { -1, -1 };
3613 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
3614 int Elt = Bytes[I];
3615 if (Elt >= 0) {
3616 // Make sure that the two permute vectors use the same suboperand
3617 // byte number. Only the operand numbers (the high bits) are
3618 // allowed to differ.
3619 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1))
3620 return false;
3621 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes;
3622 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes;
3623 // Make sure that the operand mappings are consistent with previous
3624 // elements.
3625 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3626 return false;
3627 OpNos[ModelOpNo] = RealOpNo;
3628 }
3629 }
3630 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3631}
3632
3633// As above, but search for a matching permute.
3634static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes,
3635 unsigned &OpNo0, unsigned &OpNo1) {
3636 for (auto &P : PermuteForms)
3637 if (matchPermute(Bytes, P, OpNo0, OpNo1))
3638 return &P;
3639 return nullptr;
3640}
3641
3642// Bytes is a VPERM-like permute vector, except that -1 is used for
3643// undefined bytes. This permute is an operand of an outer permute.
3644// See whether redistributing the -1 bytes gives a shuffle that can be
3645// implemented using P. If so, set Transform to a VPERM-like permute vector
3646// that, when applied to the result of P, gives the original permute in Bytes.
3647static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3648 const Permute &P,
3649 SmallVectorImpl<int> &Transform) {
3650 unsigned To = 0;
3651 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) {
3652 int Elt = Bytes[From];
3653 if (Elt < 0)
3654 // Byte number From of the result is undefined.
3655 Transform[From] = -1;
3656 else {
3657 while (P.Bytes[To] != Elt) {
3658 To += 1;
3659 if (To == SystemZ::VectorBytes)
3660 return false;
3661 }
3662 Transform[From] = To;
3663 }
3664 }
3665 return true;
3666}
3667
3668// As above, but search for a matching permute.
3669static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3670 SmallVectorImpl<int> &Transform) {
3671 for (auto &P : PermuteForms)
3672 if (matchDoublePermute(Bytes, P, Transform))
3673 return &P;
3674 return nullptr;
3675}
3676
3677// Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask,
3678// as if it had type vNi8.
3679static void getVPermMask(ShuffleVectorSDNode *VSN,
3680 SmallVectorImpl<int> &Bytes) {
3681 EVT VT = VSN->getValueType(0);
3682 unsigned NumElements = VT.getVectorNumElements();
3683 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3684 Bytes.resize(NumElements * BytesPerElement, -1);
3685 for (unsigned I = 0; I < NumElements; ++I) {
3686 int Index = VSN->getMaskElt(I);
3687 if (Index >= 0)
3688 for (unsigned J = 0; J < BytesPerElement; ++J)
3689 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
3690 }
3691}
3692
3693// Bytes is a VPERM-like permute vector, except that -1 is used for
3694// undefined bytes. See whether bytes [Start, Start + BytesPerElement) of
3695// the result come from a contiguous sequence of bytes from one input.
3696// Set Base to the selector for the first byte if so.
3697static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start,
3698 unsigned BytesPerElement, int &Base) {
3699 Base = -1;
3700 for (unsigned I = 0; I < BytesPerElement; ++I) {
3701 if (Bytes[Start + I] >= 0) {
3702 unsigned Elem = Bytes[Start + I];
3703 if (Base < 0) {
3704 Base = Elem - I;
3705 // Make sure the bytes would come from one input operand.
3706 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size())
3707 return false;
3708 } else if (unsigned(Base) != Elem - I)
3709 return false;
3710 }
3711 }
3712 return true;
3713}
3714
3715// Bytes is a VPERM-like permute vector, except that -1 is used for
3716// undefined bytes. Return true if it can be performed using VSLDI.
3717// When returning true, set StartIndex to the shift amount and OpNo0
3718// and OpNo1 to the VPERM operands that should be used as the first
3719// and second shift operand respectively.
3720static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes,
3721 unsigned &StartIndex, unsigned &OpNo0,
3722 unsigned &OpNo1) {
3723 int OpNos[] = { -1, -1 };
3724 int Shift = -1;
3725 for (unsigned I = 0; I < 16; ++I) {
3726 int Index = Bytes[I];
3727 if (Index >= 0) {
3728 int ExpectedShift = (Index - I) % SystemZ::VectorBytes;
3729 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes;
3730 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes;
3731 if (Shift < 0)
3732 Shift = ExpectedShift;
3733 else if (Shift != ExpectedShift)
3734 return false;
3735 // Make sure that the operand mappings are consistent with previous
3736 // elements.
3737 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3738 return false;
3739 OpNos[ModelOpNo] = RealOpNo;
3740 }
3741 }
3742 StartIndex = Shift;
3743 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3744}
3745
3746// Create a node that performs P on operands Op0 and Op1, casting the
3747// operands to the appropriate type. The type of the result is determined by P.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003748static SDValue getPermuteNode(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003749 const Permute &P, SDValue Op0, SDValue Op1) {
3750 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input
3751 // elements of a PACK are twice as wide as the outputs.
3752 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 :
3753 P.Opcode == SystemZISD::PACK ? P.Operand * 2 :
3754 P.Operand);
3755 // Cast both operands to the appropriate type.
3756 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8),
3757 SystemZ::VectorBytes / InBytes);
3758 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0);
3759 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1);
3760 SDValue Op;
3761 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) {
3762 SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32);
3763 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2);
3764 } else if (P.Opcode == SystemZISD::PACK) {
3765 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8),
3766 SystemZ::VectorBytes / P.Operand);
3767 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1);
3768 } else {
3769 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1);
3770 }
3771 return Op;
3772}
3773
3774// Bytes is a VPERM-like permute vector, except that -1 is used for
3775// undefined bytes. Implement it on operands Ops[0] and Ops[1] using
3776// VSLDI or VPERM.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003777static SDValue getGeneralPermuteNode(SelectionDAG &DAG, const SDLoc &DL,
3778 SDValue *Ops,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003779 const SmallVectorImpl<int> &Bytes) {
3780 for (unsigned I = 0; I < 2; ++I)
3781 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]);
3782
3783 // First see whether VSLDI can be used.
3784 unsigned StartIndex, OpNo0, OpNo1;
3785 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1))
3786 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0],
3787 Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32));
3788
3789 // Fall back on VPERM. Construct an SDNode for the permute vector.
3790 SDValue IndexNodes[SystemZ::VectorBytes];
3791 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3792 if (Bytes[I] >= 0)
3793 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32);
3794 else
3795 IndexNodes[I] = DAG.getUNDEF(MVT::i32);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00003796 SDValue Op2 = DAG.getBuildVector(MVT::v16i8, DL, IndexNodes);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003797 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2);
3798}
3799
3800namespace {
3801// Describes a general N-operand vector shuffle.
3802struct GeneralShuffle {
3803 GeneralShuffle(EVT vt) : VT(vt) {}
3804 void addUndef();
3805 void add(SDValue, unsigned);
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003806 SDValue getNode(SelectionDAG &, const SDLoc &);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003807
3808 // The operands of the shuffle.
3809 SmallVector<SDValue, SystemZ::VectorBytes> Ops;
3810
3811 // Index I is -1 if byte I of the result is undefined. Otherwise the
3812 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand
3813 // Bytes[I] / SystemZ::VectorBytes.
3814 SmallVector<int, SystemZ::VectorBytes> Bytes;
3815
3816 // The type of the shuffle result.
3817 EVT VT;
3818};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003819}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003820
3821// Add an extra undefined element to the shuffle.
3822void GeneralShuffle::addUndef() {
3823 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3824 for (unsigned I = 0; I < BytesPerElement; ++I)
3825 Bytes.push_back(-1);
3826}
3827
3828// Add an extra element to the shuffle, taking it from element Elem of Op.
3829// A null Op indicates a vector input whose value will be calculated later;
3830// there is at most one such input per shuffle and it always has the same
3831// type as the result.
3832void GeneralShuffle::add(SDValue Op, unsigned Elem) {
3833 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3834
3835 // The source vector can have wider elements than the result,
3836 // either through an explicit TRUNCATE or because of type legalization.
3837 // We want the least significant part.
3838 EVT FromVT = Op.getNode() ? Op.getValueType() : VT;
3839 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize();
3840 assert(FromBytesPerElement >= BytesPerElement &&
3841 "Invalid EXTRACT_VECTOR_ELT");
3842 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes +
3843 (FromBytesPerElement - BytesPerElement));
3844
3845 // Look through things like shuffles and bitcasts.
3846 while (Op.getNode()) {
3847 if (Op.getOpcode() == ISD::BITCAST)
3848 Op = Op.getOperand(0);
3849 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) {
3850 // See whether the bytes we need come from a contiguous part of one
3851 // operand.
3852 SmallVector<int, SystemZ::VectorBytes> OpBytes;
3853 getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes);
3854 int NewByte;
3855 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte))
3856 break;
3857 if (NewByte < 0) {
3858 addUndef();
3859 return;
3860 }
3861 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes);
3862 Byte = unsigned(NewByte) % SystemZ::VectorBytes;
Sanjay Patel57195842016-03-14 17:28:46 +00003863 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003864 addUndef();
3865 return;
3866 } else
3867 break;
3868 }
3869
3870 // Make sure that the source of the extraction is in Ops.
3871 unsigned OpNo = 0;
3872 for (; OpNo < Ops.size(); ++OpNo)
3873 if (Ops[OpNo] == Op)
3874 break;
3875 if (OpNo == Ops.size())
3876 Ops.push_back(Op);
3877
3878 // Add the element to Bytes.
3879 unsigned Base = OpNo * SystemZ::VectorBytes + Byte;
3880 for (unsigned I = 0; I < BytesPerElement; ++I)
3881 Bytes.push_back(Base + I);
3882}
3883
3884// Return SDNodes for the completed shuffle.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003885SDValue GeneralShuffle::getNode(SelectionDAG &DAG, const SDLoc &DL) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003886 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector");
3887
3888 if (Ops.size() == 0)
3889 return DAG.getUNDEF(VT);
3890
3891 // Make sure that there are at least two shuffle operands.
3892 if (Ops.size() == 1)
3893 Ops.push_back(DAG.getUNDEF(MVT::v16i8));
3894
3895 // Create a tree of shuffles, deferring root node until after the loop.
3896 // Try to redistribute the undefined elements of non-root nodes so that
3897 // the non-root shuffles match something like a pack or merge, then adjust
3898 // the parent node's permute vector to compensate for the new order.
3899 // Among other things, this copes with vectors like <2 x i16> that were
3900 // padded with undefined elements during type legalization.
3901 //
3902 // In the best case this redistribution will lead to the whole tree
3903 // using packs and merges. It should rarely be a loss in other cases.
3904 unsigned Stride = 1;
3905 for (; Stride * 2 < Ops.size(); Stride *= 2) {
3906 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) {
3907 SDValue SubOps[] = { Ops[I], Ops[I + Stride] };
3908
3909 // Create a mask for just these two operands.
3910 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes);
3911 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3912 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes;
3913 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes;
3914 if (OpNo == I)
3915 NewBytes[J] = Byte;
3916 else if (OpNo == I + Stride)
3917 NewBytes[J] = SystemZ::VectorBytes + Byte;
3918 else
3919 NewBytes[J] = -1;
3920 }
3921 // See if it would be better to reorganize NewMask to avoid using VPERM.
3922 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes);
3923 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) {
3924 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]);
3925 // Applying NewBytesMap to Ops[I] gets back to NewBytes.
3926 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3927 if (NewBytes[J] >= 0) {
3928 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes &&
3929 "Invalid double permute");
3930 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J];
3931 } else
3932 assert(NewBytesMap[J] < 0 && "Invalid double permute");
3933 }
3934 } else {
3935 // Just use NewBytes on the operands.
3936 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes);
3937 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J)
3938 if (NewBytes[J] >= 0)
3939 Bytes[J] = I * SystemZ::VectorBytes + J;
3940 }
3941 }
3942 }
3943
3944 // Now we just have 2 inputs. Put the second operand in Ops[1].
3945 if (Stride > 1) {
3946 Ops[1] = Ops[Stride];
3947 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3948 if (Bytes[I] >= int(SystemZ::VectorBytes))
3949 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes;
3950 }
3951
3952 // Look for an instruction that can do the permute without resorting
3953 // to VPERM.
3954 unsigned OpNo0, OpNo1;
3955 SDValue Op;
3956 if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1))
3957 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]);
3958 else
3959 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes);
3960 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3961}
3962
Ulrich Weigandcd808232015-05-05 19:26:48 +00003963// Return true if the given BUILD_VECTOR is a scalar-to-vector conversion.
3964static bool isScalarToVector(SDValue Op) {
3965 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00003966 if (!Op.getOperand(I).isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003967 return false;
3968 return true;
3969}
3970
3971// Return a vector of type VT that contains Value in the first element.
3972// The other elements don't matter.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003973static SDValue buildScalarToVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandcd808232015-05-05 19:26:48 +00003974 SDValue Value) {
3975 // If we have a constant, replicate it to all elements and let the
3976 // BUILD_VECTOR lowering take care of it.
3977 if (Value.getOpcode() == ISD::Constant ||
3978 Value.getOpcode() == ISD::ConstantFP) {
3979 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00003980 return DAG.getBuildVector(VT, DL, Ops);
Ulrich Weigandcd808232015-05-05 19:26:48 +00003981 }
Sanjay Patel57195842016-03-14 17:28:46 +00003982 if (Value.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003983 return DAG.getUNDEF(VT);
3984 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
3985}
3986
3987// Return a vector of type VT in which Op0 is in element 0 and Op1 is in
3988// element 1. Used for cases in which replication is cheap.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003989static SDValue buildMergeScalars(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandcd808232015-05-05 19:26:48 +00003990 SDValue Op0, SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00003991 if (Op0.isUndef()) {
3992 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003993 return DAG.getUNDEF(VT);
3994 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1);
3995 }
Sanjay Patel57195842016-03-14 17:28:46 +00003996 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003997 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0);
3998 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT,
3999 buildScalarToVector(DAG, DL, VT, Op0),
4000 buildScalarToVector(DAG, DL, VT, Op1));
4001}
4002
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004003// Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64
4004// vector for them.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004005static SDValue joinDwords(SelectionDAG &DAG, const SDLoc &DL, SDValue Op0,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004006 SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00004007 if (Op0.isUndef() && Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004008 return DAG.getUNDEF(MVT::v2i64);
4009 // If one of the two inputs is undefined then replicate the other one,
4010 // in order to avoid using another register unnecessarily.
Sanjay Patel57195842016-03-14 17:28:46 +00004011 if (Op0.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004012 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
Sanjay Patel57195842016-03-14 17:28:46 +00004013 else if (Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004014 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
4015 else {
4016 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
4017 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
4018 }
4019 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1);
4020}
4021
4022// Try to represent constant BUILD_VECTOR node BVN using a
4023// SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask
4024// on success.
4025static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) {
4026 EVT ElemVT = BVN->getValueType(0).getVectorElementType();
4027 unsigned BytesPerElement = ElemVT.getStoreSize();
4028 for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) {
4029 SDValue Op = BVN->getOperand(I);
Sanjay Patel75068522016-03-14 18:09:43 +00004030 if (!Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004031 uint64_t Value;
4032 if (Op.getOpcode() == ISD::Constant)
4033 Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue();
4034 else if (Op.getOpcode() == ISD::ConstantFP)
4035 Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt()
4036 .getZExtValue());
4037 else
4038 return false;
4039 for (unsigned J = 0; J < BytesPerElement; ++J) {
4040 uint64_t Byte = (Value >> (J * 8)) & 0xff;
4041 if (Byte == 0xff)
Aaron Ballman2a3aa1f242015-05-11 12:45:53 +00004042 Mask |= 1ULL << ((E - I - 1) * BytesPerElement + J);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004043 else if (Byte != 0)
4044 return false;
4045 }
4046 }
4047 }
4048 return true;
4049}
4050
4051// Try to load a vector constant in which BitsPerElement-bit value Value
4052// is replicated to fill the vector. VT is the type of the resulting
4053// constant, which may have elements of a different size from BitsPerElement.
4054// Return the SDValue of the constant on success, otherwise return
4055// an empty value.
4056static SDValue tryBuildVectorReplicate(SelectionDAG &DAG,
4057 const SystemZInstrInfo *TII,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004058 const SDLoc &DL, EVT VT, uint64_t Value,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004059 unsigned BitsPerElement) {
4060 // Signed 16-bit values can be replicated using VREPI.
4061 int64_t SignedValue = SignExtend64(Value, BitsPerElement);
4062 if (isInt<16>(SignedValue)) {
4063 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
4064 SystemZ::VectorBits / BitsPerElement);
4065 SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT,
4066 DAG.getConstant(SignedValue, DL, MVT::i32));
4067 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4068 }
4069 // See whether rotating the constant left some N places gives a value that
4070 // is one less than a power of 2 (i.e. all zeros followed by all ones).
4071 // If so we can use VGM.
4072 unsigned Start, End;
4073 if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) {
4074 // isRxSBGMask returns the bit numbers for a full 64-bit value,
4075 // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to
4076 // bit numbers for an BitsPerElement value, so that 0 denotes
4077 // 1 << (BitsPerElement-1).
4078 Start -= 64 - BitsPerElement;
4079 End -= 64 - BitsPerElement;
4080 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
4081 SystemZ::VectorBits / BitsPerElement);
4082 SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT,
4083 DAG.getConstant(Start, DL, MVT::i32),
4084 DAG.getConstant(End, DL, MVT::i32));
4085 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4086 }
4087 return SDValue();
4088}
4089
4090// If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually
4091// better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for
4092// the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR
4093// would benefit from this representation and return it if so.
4094static SDValue tryBuildVectorShuffle(SelectionDAG &DAG,
4095 BuildVectorSDNode *BVN) {
4096 EVT VT = BVN->getValueType(0);
4097 unsigned NumElements = VT.getVectorNumElements();
4098
4099 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation
4100 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still
4101 // need a BUILD_VECTOR, add an additional placeholder operand for that
4102 // BUILD_VECTOR and store its operands in ResidueOps.
4103 GeneralShuffle GS(VT);
4104 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps;
4105 bool FoundOne = false;
4106 for (unsigned I = 0; I < NumElements; ++I) {
4107 SDValue Op = BVN->getOperand(I);
4108 if (Op.getOpcode() == ISD::TRUNCATE)
4109 Op = Op.getOperand(0);
4110 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4111 Op.getOperand(1).getOpcode() == ISD::Constant) {
4112 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4113 GS.add(Op.getOperand(0), Elem);
4114 FoundOne = true;
Sanjay Patel57195842016-03-14 17:28:46 +00004115 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004116 GS.addUndef();
4117 } else {
4118 GS.add(SDValue(), ResidueOps.size());
Ulrich Weigande861e642015-09-15 14:27:46 +00004119 ResidueOps.push_back(BVN->getOperand(I));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004120 }
4121 }
4122
4123 // Nothing to do if there are no EXTRACT_VECTOR_ELTs.
4124 if (!FoundOne)
4125 return SDValue();
4126
4127 // Create the BUILD_VECTOR for the remaining elements, if any.
4128 if (!ResidueOps.empty()) {
4129 while (ResidueOps.size() < NumElements)
Ulrich Weigandf4d14f72015-10-08 17:46:59 +00004130 ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType()));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004131 for (auto &Op : GS.Ops) {
4132 if (!Op.getNode()) {
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004133 Op = DAG.getBuildVector(VT, SDLoc(BVN), ResidueOps);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004134 break;
4135 }
4136 }
4137 }
4138 return GS.getNode(DAG, SDLoc(BVN));
4139}
4140
4141// Combine GPR scalar values Elems into a vector of type VT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004142static SDValue buildVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004143 SmallVectorImpl<SDValue> &Elems) {
4144 // See whether there is a single replicated value.
4145 SDValue Single;
4146 unsigned int NumElements = Elems.size();
4147 unsigned int Count = 0;
4148 for (auto Elem : Elems) {
Sanjay Patel75068522016-03-14 18:09:43 +00004149 if (!Elem.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004150 if (!Single.getNode())
4151 Single = Elem;
4152 else if (Elem != Single) {
4153 Single = SDValue();
4154 break;
4155 }
4156 Count += 1;
4157 }
4158 }
4159 // There are three cases here:
4160 //
4161 // - if the only defined element is a loaded one, the best sequence
4162 // is a replicating load.
4163 //
4164 // - otherwise, if the only defined element is an i64 value, we will
4165 // end up with the same VLVGP sequence regardless of whether we short-cut
4166 // for replication or fall through to the later code.
4167 //
4168 // - otherwise, if the only defined element is an i32 or smaller value,
4169 // we would need 2 instructions to replicate it: VLVGP followed by VREPx.
4170 // This is only a win if the single defined element is used more than once.
4171 // In other cases we're better off using a single VLVGx.
4172 if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD))
4173 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single);
4174
4175 // The best way of building a v2i64 from two i64s is to use VLVGP.
4176 if (VT == MVT::v2i64)
4177 return joinDwords(DAG, DL, Elems[0], Elems[1]);
4178
Ulrich Weigandcd808232015-05-05 19:26:48 +00004179 // Use a 64-bit merge high to combine two doubles.
4180 if (VT == MVT::v2f64)
4181 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4182
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004183 // Build v4f32 values directly from the FPRs:
4184 //
4185 // <Axxx> <Bxxx> <Cxxxx> <Dxxx>
4186 // V V VMRHF
4187 // <ABxx> <CDxx>
4188 // V VMRHG
4189 // <ABCD>
4190 if (VT == MVT::v4f32) {
4191 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4192 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]);
4193 // Avoid unnecessary undefs by reusing the other operand.
Sanjay Patel57195842016-03-14 17:28:46 +00004194 if (Op01.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004195 Op01 = Op23;
Sanjay Patel57195842016-03-14 17:28:46 +00004196 else if (Op23.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004197 Op23 = Op01;
4198 // Merging identical replications is a no-op.
4199 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23)
4200 return Op01;
4201 Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01);
4202 Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23);
4203 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH,
4204 DL, MVT::v2i64, Op01, Op23);
4205 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4206 }
4207
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004208 // Collect the constant terms.
4209 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue());
4210 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false);
4211
4212 unsigned NumConstants = 0;
4213 for (unsigned I = 0; I < NumElements; ++I) {
4214 SDValue Elem = Elems[I];
4215 if (Elem.getOpcode() == ISD::Constant ||
4216 Elem.getOpcode() == ISD::ConstantFP) {
4217 NumConstants += 1;
4218 Constants[I] = Elem;
4219 Done[I] = true;
4220 }
4221 }
4222 // If there was at least one constant, fill in the other elements of
4223 // Constants with undefs to get a full vector constant and use that
4224 // as the starting point.
4225 SDValue Result;
4226 if (NumConstants > 0) {
4227 for (unsigned I = 0; I < NumElements; ++I)
4228 if (!Constants[I].getNode())
4229 Constants[I] = DAG.getUNDEF(Elems[I].getValueType());
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004230 Result = DAG.getBuildVector(VT, DL, Constants);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004231 } else {
4232 // Otherwise try to use VLVGP to start the sequence in order to
4233 // avoid a false dependency on any previous contents of the vector
4234 // register. This only makes sense if one of the associated elements
4235 // is defined.
4236 unsigned I1 = NumElements / 2 - 1;
4237 unsigned I2 = NumElements - 1;
Sanjay Patel75068522016-03-14 18:09:43 +00004238 bool Def1 = !Elems[I1].isUndef();
4239 bool Def2 = !Elems[I2].isUndef();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004240 if (Def1 || Def2) {
4241 SDValue Elem1 = Elems[Def1 ? I1 : I2];
4242 SDValue Elem2 = Elems[Def2 ? I2 : I1];
4243 Result = DAG.getNode(ISD::BITCAST, DL, VT,
4244 joinDwords(DAG, DL, Elem1, Elem2));
4245 Done[I1] = true;
4246 Done[I2] = true;
4247 } else
4248 Result = DAG.getUNDEF(VT);
4249 }
4250
4251 // Use VLVGx to insert the other elements.
4252 for (unsigned I = 0; I < NumElements; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00004253 if (!Done[I] && !Elems[I].isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004254 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I],
4255 DAG.getConstant(I, DL, MVT::i32));
4256 return Result;
4257}
4258
4259SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op,
4260 SelectionDAG &DAG) const {
4261 const SystemZInstrInfo *TII =
4262 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
4263 auto *BVN = cast<BuildVectorSDNode>(Op.getNode());
4264 SDLoc DL(Op);
4265 EVT VT = Op.getValueType();
4266
4267 if (BVN->isConstant()) {
4268 // Try using VECTOR GENERATE BYTE MASK. This is the architecturally-
4269 // preferred way of creating all-zero and all-one vectors so give it
4270 // priority over other methods below.
4271 uint64_t Mask = 0;
4272 if (tryBuildVectorByteMask(BVN, Mask)) {
4273 SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
4274 DAG.getConstant(Mask, DL, MVT::i32));
4275 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4276 }
4277
4278 // Try using some form of replication.
4279 APInt SplatBits, SplatUndef;
4280 unsigned SplatBitSize;
4281 bool HasAnyUndefs;
4282 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4283 8, true) &&
4284 SplatBitSize <= 64) {
4285 // First try assuming that any undefined bits above the highest set bit
4286 // and below the lowest set bit are 1s. This increases the likelihood of
4287 // being able to use a sign-extended element value in VECTOR REPLICATE
4288 // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK.
4289 uint64_t SplatBitsZ = SplatBits.getZExtValue();
4290 uint64_t SplatUndefZ = SplatUndef.getZExtValue();
4291 uint64_t Lower = (SplatUndefZ
4292 & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1));
4293 uint64_t Upper = (SplatUndefZ
4294 & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1));
4295 uint64_t Value = SplatBitsZ | Upper | Lower;
4296 SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value,
4297 SplatBitSize);
4298 if (Op.getNode())
4299 return Op;
4300
4301 // Now try assuming that any undefined bits between the first and
4302 // last defined set bits are set. This increases the chances of
4303 // using a non-wraparound mask.
4304 uint64_t Middle = SplatUndefZ & ~Upper & ~Lower;
4305 Value = SplatBitsZ | Middle;
4306 Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize);
4307 if (Op.getNode())
4308 return Op;
4309 }
4310
4311 // Fall back to loading it from memory.
4312 return SDValue();
4313 }
4314
4315 // See if we should use shuffles to construct the vector from other vectors.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004316 if (SDValue Res = tryBuildVectorShuffle(DAG, BVN))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004317 return Res;
4318
Ulrich Weigandcd808232015-05-05 19:26:48 +00004319 // Detect SCALAR_TO_VECTOR conversions.
4320 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op))
4321 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0));
4322
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004323 // Otherwise use buildVector to build the vector up from GPRs.
4324 unsigned NumElements = Op.getNumOperands();
4325 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements);
4326 for (unsigned I = 0; I < NumElements; ++I)
4327 Ops[I] = Op.getOperand(I);
4328 return buildVector(DAG, DL, VT, Ops);
4329}
4330
4331SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
4332 SelectionDAG &DAG) const {
4333 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode());
4334 SDLoc DL(Op);
4335 EVT VT = Op.getValueType();
4336 unsigned NumElements = VT.getVectorNumElements();
4337
4338 if (VSN->isSplat()) {
4339 SDValue Op0 = Op.getOperand(0);
4340 unsigned Index = VSN->getSplatIndex();
4341 assert(Index < VT.getVectorNumElements() &&
4342 "Splat index should be defined and in first operand");
4343 // See whether the value we're splatting is directly available as a scalar.
4344 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4345 Op0.getOpcode() == ISD::BUILD_VECTOR)
4346 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index));
4347 // Otherwise keep it as a vector-to-vector operation.
4348 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0),
4349 DAG.getConstant(Index, DL, MVT::i32));
4350 }
4351
4352 GeneralShuffle GS(VT);
4353 for (unsigned I = 0; I < NumElements; ++I) {
4354 int Elt = VSN->getMaskElt(I);
4355 if (Elt < 0)
4356 GS.addUndef();
4357 else
4358 GS.add(Op.getOperand(unsigned(Elt) / NumElements),
4359 unsigned(Elt) % NumElements);
4360 }
4361 return GS.getNode(DAG, SDLoc(VSN));
4362}
4363
4364SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
4365 SelectionDAG &DAG) const {
4366 SDLoc DL(Op);
4367 // Just insert the scalar into element 0 of an undefined vector.
4368 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL,
4369 Op.getValueType(), DAG.getUNDEF(Op.getValueType()),
4370 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32));
4371}
4372
Ulrich Weigandcd808232015-05-05 19:26:48 +00004373SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
4374 SelectionDAG &DAG) const {
4375 // Handle insertions of floating-point values.
4376 SDLoc DL(Op);
4377 SDValue Op0 = Op.getOperand(0);
4378 SDValue Op1 = Op.getOperand(1);
4379 SDValue Op2 = Op.getOperand(2);
4380 EVT VT = Op.getValueType();
4381
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004382 // Insertions into constant indices of a v2f64 can be done using VPDI.
4383 // However, if the inserted value is a bitcast or a constant then it's
4384 // better to use GPRs, as below.
4385 if (VT == MVT::v2f64 &&
4386 Op1.getOpcode() != ISD::BITCAST &&
Ulrich Weigandcd808232015-05-05 19:26:48 +00004387 Op1.getOpcode() != ISD::ConstantFP &&
4388 Op2.getOpcode() == ISD::Constant) {
4389 uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue();
4390 unsigned Mask = VT.getVectorNumElements() - 1;
4391 if (Index <= Mask)
4392 return Op;
4393 }
4394
4395 // Otherwise bitcast to the equivalent integer form and insert via a GPR.
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004396 MVT IntVT = MVT::getIntegerVT(VT.getScalarSizeInBits());
Ulrich Weigandcd808232015-05-05 19:26:48 +00004397 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements());
4398 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT,
4399 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0),
4400 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2);
4401 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4402}
4403
4404SDValue
4405SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
4406 SelectionDAG &DAG) const {
4407 // Handle extractions of floating-point values.
4408 SDLoc DL(Op);
4409 SDValue Op0 = Op.getOperand(0);
4410 SDValue Op1 = Op.getOperand(1);
4411 EVT VT = Op.getValueType();
4412 EVT VecVT = Op0.getValueType();
4413
4414 // Extractions of constant indices can be done directly.
4415 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) {
4416 uint64_t Index = CIndexN->getZExtValue();
4417 unsigned Mask = VecVT.getVectorNumElements() - 1;
4418 if (Index <= Mask)
4419 return Op;
4420 }
4421
4422 // Otherwise bitcast to the equivalent integer form and extract via a GPR.
4423 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
4424 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements());
4425 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT,
4426 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1);
4427 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4428}
4429
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004430SDValue
4431SystemZTargetLowering::lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004432 unsigned UnpackHigh) const {
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004433 SDValue PackedOp = Op.getOperand(0);
4434 EVT OutVT = Op.getValueType();
4435 EVT InVT = PackedOp.getValueType();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004436 unsigned ToBits = OutVT.getScalarSizeInBits();
4437 unsigned FromBits = InVT.getScalarSizeInBits();
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004438 do {
4439 FromBits *= 2;
4440 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits),
4441 SystemZ::VectorBits / FromBits);
4442 PackedOp = DAG.getNode(UnpackHigh, SDLoc(PackedOp), OutVT, PackedOp);
4443 } while (FromBits != ToBits);
4444 return PackedOp;
4445}
4446
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004447SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG,
4448 unsigned ByScalar) const {
4449 // Look for cases where a vector shift can use the *_BY_SCALAR form.
4450 SDValue Op0 = Op.getOperand(0);
4451 SDValue Op1 = Op.getOperand(1);
4452 SDLoc DL(Op);
4453 EVT VT = Op.getValueType();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004454 unsigned ElemBitSize = VT.getScalarSizeInBits();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004455
4456 // See whether the shift vector is a splat represented as BUILD_VECTOR.
4457 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) {
4458 APInt SplatBits, SplatUndef;
4459 unsigned SplatBitSize;
4460 bool HasAnyUndefs;
4461 // Check for constant splats. Use ElemBitSize as the minimum element
4462 // width and reject splats that need wider elements.
4463 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4464 ElemBitSize, true) &&
4465 SplatBitSize == ElemBitSize) {
4466 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff,
4467 DL, MVT::i32);
4468 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4469 }
4470 // Check for variable splats.
4471 BitVector UndefElements;
4472 SDValue Splat = BVN->getSplatValue(&UndefElements);
4473 if (Splat) {
4474 // Since i32 is the smallest legal type, we either need a no-op
4475 // or a truncation.
4476 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat);
4477 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4478 }
4479 }
4480
4481 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR,
4482 // and the shift amount is directly available in a GPR.
4483 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) {
4484 if (VSN->isSplat()) {
4485 SDValue VSNOp0 = VSN->getOperand(0);
4486 unsigned Index = VSN->getSplatIndex();
4487 assert(Index < VT.getVectorNumElements() &&
4488 "Splat index should be defined and in first operand");
4489 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4490 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) {
4491 // Since i32 is the smallest legal type, we either need a no-op
4492 // or a truncation.
4493 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32,
4494 VSNOp0.getOperand(Index));
4495 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4496 }
4497 }
4498 }
4499
4500 // Otherwise just treat the current form as legal.
4501 return Op;
4502}
4503
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004504SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
4505 SelectionDAG &DAG) const {
4506 switch (Op.getOpcode()) {
Ulrich Weigandf557d082016-04-04 12:44:55 +00004507 case ISD::FRAMEADDR:
4508 return lowerFRAMEADDR(Op, DAG);
4509 case ISD::RETURNADDR:
4510 return lowerRETURNADDR(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004511 case ISD::BR_CC:
4512 return lowerBR_CC(Op, DAG);
4513 case ISD::SELECT_CC:
4514 return lowerSELECT_CC(Op, DAG);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00004515 case ISD::SETCC:
4516 return lowerSETCC(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004517 case ISD::GlobalAddress:
4518 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
4519 case ISD::GlobalTLSAddress:
4520 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
4521 case ISD::BlockAddress:
4522 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
4523 case ISD::JumpTable:
4524 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
4525 case ISD::ConstantPool:
4526 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
4527 case ISD::BITCAST:
4528 return lowerBITCAST(Op, DAG);
4529 case ISD::VASTART:
4530 return lowerVASTART(Op, DAG);
4531 case ISD::VACOPY:
4532 return lowerVACOPY(Op, DAG);
4533 case ISD::DYNAMIC_STACKALLOC:
4534 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +00004535 case ISD::GET_DYNAMIC_AREA_OFFSET:
4536 return lowerGET_DYNAMIC_AREA_OFFSET(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00004537 case ISD::SMUL_LOHI:
4538 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004539 case ISD::UMUL_LOHI:
4540 return lowerUMUL_LOHI(Op, DAG);
4541 case ISD::SDIVREM:
4542 return lowerSDIVREM(Op, DAG);
4543 case ISD::UDIVREM:
4544 return lowerUDIVREM(Op, DAG);
4545 case ISD::OR:
4546 return lowerOR(Op, DAG);
Ulrich Weigandb4012182015-03-31 12:56:33 +00004547 case ISD::CTPOP:
4548 return lowerCTPOP(Op, DAG);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004549 case ISD::ATOMIC_FENCE:
4550 return lowerATOMIC_FENCE(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004551 case ISD::ATOMIC_SWAP:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004552 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
4553 case ISD::ATOMIC_STORE:
4554 return lowerATOMIC_STORE(Op, DAG);
4555 case ISD::ATOMIC_LOAD:
4556 return lowerATOMIC_LOAD(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004557 case ISD::ATOMIC_LOAD_ADD:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004558 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004559 case ISD::ATOMIC_LOAD_SUB:
Richard Sandiford41350a52013-12-24 15:18:04 +00004560 return lowerATOMIC_LOAD_SUB(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004561 case ISD::ATOMIC_LOAD_AND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004562 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004563 case ISD::ATOMIC_LOAD_OR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004564 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004565 case ISD::ATOMIC_LOAD_XOR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004566 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004567 case ISD::ATOMIC_LOAD_NAND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004568 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004569 case ISD::ATOMIC_LOAD_MIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004570 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004571 case ISD::ATOMIC_LOAD_MAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004572 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004573 case ISD::ATOMIC_LOAD_UMIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004574 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004575 case ISD::ATOMIC_LOAD_UMAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004576 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004577 case ISD::ATOMIC_CMP_SWAP:
4578 return lowerATOMIC_CMP_SWAP(Op, DAG);
4579 case ISD::STACKSAVE:
4580 return lowerSTACKSAVE(Op, DAG);
4581 case ISD::STACKRESTORE:
4582 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00004583 case ISD::PREFETCH:
4584 return lowerPREFETCH(Op, DAG);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004585 case ISD::INTRINSIC_W_CHAIN:
4586 return lowerINTRINSIC_W_CHAIN(Op, DAG);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004587 case ISD::INTRINSIC_WO_CHAIN:
4588 return lowerINTRINSIC_WO_CHAIN(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004589 case ISD::BUILD_VECTOR:
4590 return lowerBUILD_VECTOR(Op, DAG);
4591 case ISD::VECTOR_SHUFFLE:
4592 return lowerVECTOR_SHUFFLE(Op, DAG);
4593 case ISD::SCALAR_TO_VECTOR:
4594 return lowerSCALAR_TO_VECTOR(Op, DAG);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004595 case ISD::INSERT_VECTOR_ELT:
4596 return lowerINSERT_VECTOR_ELT(Op, DAG);
4597 case ISD::EXTRACT_VECTOR_ELT:
4598 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004599 case ISD::SIGN_EXTEND_VECTOR_INREG:
4600 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACK_HIGH);
4601 case ISD::ZERO_EXTEND_VECTOR_INREG:
4602 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACKL_HIGH);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004603 case ISD::SHL:
4604 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR);
4605 case ISD::SRL:
4606 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR);
4607 case ISD::SRA:
4608 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004609 default:
4610 llvm_unreachable("Unexpected node to lower");
4611 }
4612}
4613
4614const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
4615#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
Matthias Braund04893f2015-05-07 21:33:59 +00004616 switch ((SystemZISD::NodeType)Opcode) {
4617 case SystemZISD::FIRST_NUMBER: break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004618 OPCODE(RET_FLAG);
4619 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00004620 OPCODE(SIBCALL);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004621 OPCODE(TLS_GDCALL);
4622 OPCODE(TLS_LDCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004623 OPCODE(PCREL_WRAPPER);
Richard Sandiford54b36912013-09-27 15:14:04 +00004624 OPCODE(PCREL_OFFSET);
Richard Sandiford57485472013-12-13 15:35:00 +00004625 OPCODE(IABS);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00004626 OPCODE(ICMP);
4627 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00004628 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004629 OPCODE(BR_CCMASK);
4630 OPCODE(SELECT_CCMASK);
4631 OPCODE(ADJDYNALLOC);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004632 OPCODE(POPCNT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004633 OPCODE(UMUL_LOHI64);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004634 OPCODE(SDIVREM32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004635 OPCODE(SDIVREM64);
4636 OPCODE(UDIVREM32);
4637 OPCODE(UDIVREM64);
Richard Sandifordd131ff82013-07-08 09:35:23 +00004638 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004639 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00004640 OPCODE(NC);
4641 OPCODE(NC_LOOP);
4642 OPCODE(OC);
4643 OPCODE(OC_LOOP);
4644 OPCODE(XC);
4645 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00004646 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004647 OPCODE(CLC_LOOP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00004648 OPCODE(STPCPY);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004649 OPCODE(STRCMP);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00004650 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00004651 OPCODE(IPM);
Richard Sandiford9afe6132013-12-10 10:36:34 +00004652 OPCODE(SERIALIZE);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004653 OPCODE(MEMBARRIER);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004654 OPCODE(TBEGIN);
4655 OPCODE(TBEGIN_NOFLOAT);
4656 OPCODE(TEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004657 OPCODE(BYTE_MASK);
4658 OPCODE(ROTATE_MASK);
4659 OPCODE(REPLICATE);
4660 OPCODE(JOIN_DWORDS);
4661 OPCODE(SPLAT);
4662 OPCODE(MERGE_HIGH);
4663 OPCODE(MERGE_LOW);
4664 OPCODE(SHL_DOUBLE);
4665 OPCODE(PERMUTE_DWORDS);
4666 OPCODE(PERMUTE);
4667 OPCODE(PACK);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004668 OPCODE(PACKS_CC);
4669 OPCODE(PACKLS_CC);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004670 OPCODE(UNPACK_HIGH);
4671 OPCODE(UNPACKL_HIGH);
4672 OPCODE(UNPACK_LOW);
4673 OPCODE(UNPACKL_LOW);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004674 OPCODE(VSHL_BY_SCALAR);
4675 OPCODE(VSRL_BY_SCALAR);
4676 OPCODE(VSRA_BY_SCALAR);
4677 OPCODE(VSUM);
4678 OPCODE(VICMPE);
4679 OPCODE(VICMPH);
4680 OPCODE(VICMPHL);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004681 OPCODE(VICMPES);
4682 OPCODE(VICMPHS);
4683 OPCODE(VICMPHLS);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004684 OPCODE(VFCMPE);
4685 OPCODE(VFCMPH);
4686 OPCODE(VFCMPHE);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004687 OPCODE(VFCMPES);
4688 OPCODE(VFCMPHS);
4689 OPCODE(VFCMPHES);
4690 OPCODE(VFTCI);
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004691 OPCODE(VEXTEND);
4692 OPCODE(VROUND);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004693 OPCODE(VTM);
4694 OPCODE(VFAE_CC);
4695 OPCODE(VFAEZ_CC);
4696 OPCODE(VFEE_CC);
4697 OPCODE(VFEEZ_CC);
4698 OPCODE(VFENE_CC);
4699 OPCODE(VFENEZ_CC);
4700 OPCODE(VISTR_CC);
4701 OPCODE(VSTRC_CC);
4702 OPCODE(VSTRCZ_CC);
Marcin Koscielnicki32e87342016-07-02 02:20:40 +00004703 OPCODE(TDC);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004704 OPCODE(ATOMIC_SWAPW);
4705 OPCODE(ATOMIC_LOADW_ADD);
4706 OPCODE(ATOMIC_LOADW_SUB);
4707 OPCODE(ATOMIC_LOADW_AND);
4708 OPCODE(ATOMIC_LOADW_OR);
4709 OPCODE(ATOMIC_LOADW_XOR);
4710 OPCODE(ATOMIC_LOADW_NAND);
4711 OPCODE(ATOMIC_LOADW_MIN);
4712 OPCODE(ATOMIC_LOADW_MAX);
4713 OPCODE(ATOMIC_LOADW_UMIN);
4714 OPCODE(ATOMIC_LOADW_UMAX);
4715 OPCODE(ATOMIC_CMP_SWAPW);
Bryan Chan28b759c2016-05-16 20:32:22 +00004716 OPCODE(LRV);
4717 OPCODE(STRV);
Richard Sandiford03481332013-08-23 11:36:42 +00004718 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004719 }
Craig Topper062a2ba2014-04-25 05:30:21 +00004720 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004721#undef OPCODE
4722}
4723
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004724// Return true if VT is a vector whose elements are a whole number of bytes
4725// in width.
4726static bool canTreatAsByteVector(EVT VT) {
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004727 return VT.isVector() && VT.getScalarSizeInBits() % 8 == 0;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004728}
4729
4730// Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT
4731// producing a result of type ResVT. Op is a possibly bitcast version
4732// of the input vector and Index is the index (based on type VecVT) that
4733// should be extracted. Return the new extraction if a simplification
4734// was possible or if Force is true.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004735SDValue SystemZTargetLowering::combineExtract(const SDLoc &DL, EVT ResVT,
4736 EVT VecVT, SDValue Op,
4737 unsigned Index,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004738 DAGCombinerInfo &DCI,
4739 bool Force) const {
4740 SelectionDAG &DAG = DCI.DAG;
4741
4742 // The number of bytes being extracted.
4743 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4744
4745 for (;;) {
4746 unsigned Opcode = Op.getOpcode();
4747 if (Opcode == ISD::BITCAST)
4748 // Look through bitcasts.
4749 Op = Op.getOperand(0);
4750 else if (Opcode == ISD::VECTOR_SHUFFLE &&
4751 canTreatAsByteVector(Op.getValueType())) {
4752 // Get a VPERM-like permute mask and see whether the bytes covered
4753 // by the extracted element are a contiguous sequence from one
4754 // source operand.
4755 SmallVector<int, SystemZ::VectorBytes> Bytes;
4756 getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes);
4757 int First;
4758 if (!getShuffleInput(Bytes, Index * BytesPerElement,
4759 BytesPerElement, First))
4760 break;
4761 if (First < 0)
4762 return DAG.getUNDEF(ResVT);
4763 // Make sure the contiguous sequence starts at a multiple of the
4764 // original element size.
4765 unsigned Byte = unsigned(First) % Bytes.size();
4766 if (Byte % BytesPerElement != 0)
4767 break;
4768 // We can get the extracted value directly from an input.
4769 Index = Byte / BytesPerElement;
4770 Op = Op.getOperand(unsigned(First) / Bytes.size());
4771 Force = true;
4772 } else if (Opcode == ISD::BUILD_VECTOR &&
4773 canTreatAsByteVector(Op.getValueType())) {
4774 // We can only optimize this case if the BUILD_VECTOR elements are
4775 // at least as wide as the extracted value.
4776 EVT OpVT = Op.getValueType();
4777 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4778 if (OpBytesPerElement < BytesPerElement)
4779 break;
4780 // Make sure that the least-significant bit of the extracted value
4781 // is the least significant bit of an input.
4782 unsigned End = (Index + 1) * BytesPerElement;
4783 if (End % OpBytesPerElement != 0)
4784 break;
4785 // We're extracting the low part of one operand of the BUILD_VECTOR.
4786 Op = Op.getOperand(End / OpBytesPerElement - 1);
4787 if (!Op.getValueType().isInteger()) {
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00004788 EVT VT = MVT::getIntegerVT(Op.getValueSizeInBits());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004789 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
4790 DCI.AddToWorklist(Op.getNode());
4791 }
4792 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits());
4793 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
4794 if (VT != ResVT) {
4795 DCI.AddToWorklist(Op.getNode());
4796 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op);
4797 }
4798 return Op;
4799 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004800 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG ||
4801 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) &&
4802 canTreatAsByteVector(Op.getValueType()) &&
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004803 canTreatAsByteVector(Op.getOperand(0).getValueType())) {
4804 // Make sure that only the unextended bits are significant.
4805 EVT ExtVT = Op.getValueType();
4806 EVT OpVT = Op.getOperand(0).getValueType();
4807 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize();
4808 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4809 unsigned Byte = Index * BytesPerElement;
4810 unsigned SubByte = Byte % ExtBytesPerElement;
4811 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement;
4812 if (SubByte < MinSubByte ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004813 SubByte + BytesPerElement > ExtBytesPerElement)
4814 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004815 // Get the byte offset of the unextended element
4816 Byte = Byte / ExtBytesPerElement * OpBytesPerElement;
4817 // ...then add the byte offset relative to that element.
4818 Byte += SubByte - MinSubByte;
4819 if (Byte % BytesPerElement != 0)
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004820 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004821 Op = Op.getOperand(0);
4822 Index = Byte / BytesPerElement;
4823 Force = true;
4824 } else
4825 break;
4826 }
4827 if (Force) {
4828 if (Op.getValueType() != VecVT) {
4829 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op);
4830 DCI.AddToWorklist(Op.getNode());
4831 }
4832 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op,
4833 DAG.getConstant(Index, DL, MVT::i32));
4834 }
4835 return SDValue();
4836}
4837
4838// Optimize vector operations in scalar value Op on the basis that Op
4839// is truncated to TruncVT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004840SDValue SystemZTargetLowering::combineTruncateExtract(
4841 const SDLoc &DL, EVT TruncVT, SDValue Op, DAGCombinerInfo &DCI) const {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004842 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into
4843 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements
4844 // of type TruncVT.
4845 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4846 TruncVT.getSizeInBits() % 8 == 0) {
4847 SDValue Vec = Op.getOperand(0);
4848 EVT VecVT = Vec.getValueType();
4849 if (canTreatAsByteVector(VecVT)) {
4850 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
4851 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4852 unsigned TruncBytes = TruncVT.getStoreSize();
4853 if (BytesPerElement % TruncBytes == 0) {
4854 // Calculate the value of Y' in the above description. We are
4855 // splitting the original elements into Scale equal-sized pieces
4856 // and for truncation purposes want the last (least-significant)
4857 // of these pieces for IndexN. This is easiest to do by calculating
4858 // the start index of the following element and then subtracting 1.
4859 unsigned Scale = BytesPerElement / TruncBytes;
4860 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1;
4861
4862 // Defer the creation of the bitcast from X to combineExtract,
4863 // which might be able to optimize the extraction.
4864 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8),
4865 VecVT.getStoreSize() / TruncBytes);
4866 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT);
4867 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true);
4868 }
4869 }
4870 }
4871 }
4872 return SDValue();
4873}
4874
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004875SDValue SystemZTargetLowering::combineSIGN_EXTEND(
4876 SDNode *N, DAGCombinerInfo &DCI) const {
4877 // Convert (sext (ashr (shl X, C1), C2)) to
4878 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
4879 // cheap as narrower ones.
4880 SelectionDAG &DAG = DCI.DAG;
4881 SDValue N0 = N->getOperand(0);
4882 EVT VT = N->getValueType(0);
4883 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
4884 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
4885 SDValue Inner = N0.getOperand(0);
4886 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
4887 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00004888 unsigned Extra = (VT.getSizeInBits() - N0.getValueSizeInBits());
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004889 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
4890 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
4891 EVT ShiftVT = N0.getOperand(1).getValueType();
4892 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
4893 Inner.getOperand(0));
4894 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
4895 DAG.getConstant(NewShlAmt, SDLoc(Inner),
4896 ShiftVT));
4897 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
4898 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT));
4899 }
4900 }
4901 }
4902 return SDValue();
4903}
4904
4905SDValue SystemZTargetLowering::combineMERGE(
4906 SDNode *N, DAGCombinerInfo &DCI) const {
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004907 SelectionDAG &DAG = DCI.DAG;
4908 unsigned Opcode = N->getOpcode();
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004909 SDValue Op0 = N->getOperand(0);
4910 SDValue Op1 = N->getOperand(1);
4911 if (Op0.getOpcode() == ISD::BITCAST)
4912 Op0 = Op0.getOperand(0);
4913 if (Op0.getOpcode() == SystemZISD::BYTE_MASK &&
4914 cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0) {
4915 // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF
4916 // for v4f32.
4917 if (Op1 == N->getOperand(0))
4918 return Op1;
4919 // (z_merge_? 0, X) -> (z_unpackl_? 0, X).
4920 EVT VT = Op1.getValueType();
4921 unsigned ElemBytes = VT.getVectorElementType().getStoreSize();
4922 if (ElemBytes <= 4) {
4923 Opcode = (Opcode == SystemZISD::MERGE_HIGH ?
4924 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW);
4925 EVT InVT = VT.changeVectorElementTypeToInteger();
4926 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16),
4927 SystemZ::VectorBytes / ElemBytes / 2);
4928 if (VT != InVT) {
4929 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1);
4930 DCI.AddToWorklist(Op1.getNode());
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004931 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004932 SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1);
4933 DCI.AddToWorklist(Op.getNode());
4934 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004935 }
4936 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004937 return SDValue();
4938}
4939
4940SDValue SystemZTargetLowering::combineSTORE(
4941 SDNode *N, DAGCombinerInfo &DCI) const {
4942 SelectionDAG &DAG = DCI.DAG;
4943 auto *SN = cast<StoreSDNode>(N);
4944 auto &Op1 = N->getOperand(1);
4945 EVT MemVT = SN->getMemoryVT();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004946 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better
4947 // for the extraction to be done on a vMiN value, so that we can use VSTE.
4948 // If X has wider elements then convert it to:
4949 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z).
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004950 if (MemVT.isInteger()) {
4951 if (SDValue Value =
4952 combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) {
4953 DCI.AddToWorklist(Value.getNode());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004954
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004955 // Rewrite the store with the new form of stored value.
4956 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value,
4957 SN->getBasePtr(), SN->getMemoryVT(),
4958 SN->getMemOperand());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004959 }
4960 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004961 // Combine STORE (BSWAP) into STRVH/STRV/STRVG
4962 // See comment in combineBSWAP about volatile accesses.
4963 if (!SN->isVolatile() &&
4964 Op1.getOpcode() == ISD::BSWAP &&
4965 Op1.getNode()->hasOneUse() &&
4966 (Op1.getValueType() == MVT::i16 ||
4967 Op1.getValueType() == MVT::i32 ||
4968 Op1.getValueType() == MVT::i64)) {
4969
4970 SDValue BSwapOp = Op1.getOperand(0);
4971
4972 if (BSwapOp.getValueType() == MVT::i16)
4973 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), MVT::i32, BSwapOp);
4974
4975 SDValue Ops[] = {
4976 N->getOperand(0), BSwapOp, N->getOperand(2),
4977 DAG.getValueType(Op1.getValueType())
4978 };
4979
4980 return
4981 DAG.getMemIntrinsicNode(SystemZISD::STRV, SDLoc(N), DAG.getVTList(MVT::Other),
4982 Ops, MemVT, SN->getMemOperand());
4983 }
4984 return SDValue();
4985}
4986
4987SDValue SystemZTargetLowering::combineEXTRACT_VECTOR_ELT(
4988 SDNode *N, DAGCombinerInfo &DCI) const {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004989 // Try to simplify a vector extraction.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004990 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
4991 SDValue Op0 = N->getOperand(0);
4992 EVT VecVT = Op0.getValueType();
4993 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0,
4994 IndexN->getZExtValue(), DCI, false);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004995 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00004996 return SDValue();
4997}
4998
4999SDValue SystemZTargetLowering::combineJOIN_DWORDS(
5000 SDNode *N, DAGCombinerInfo &DCI) const {
5001 SelectionDAG &DAG = DCI.DAG;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005002 // (join_dwords X, X) == (replicate X)
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005003 if (N->getOperand(0) == N->getOperand(1))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005004 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0),
5005 N->getOperand(0));
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005006 return SDValue();
5007}
5008
5009SDValue SystemZTargetLowering::combineFP_ROUND(
5010 SDNode *N, DAGCombinerInfo &DCI) const {
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00005011 // (fpround (extract_vector_elt X 0))
5012 // (fpround (extract_vector_elt X 1)) ->
Ulrich Weigand80b3af72015-05-05 19:27:45 +00005013 // (extract_vector_elt (VROUND X) 0)
5014 // (extract_vector_elt (VROUND X) 1)
5015 //
5016 // This is a special case since the target doesn't really support v2f32s.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005017 SelectionDAG &DAG = DCI.DAG;
5018 SDValue Op0 = N->getOperand(0);
5019 if (N->getValueType(0) == MVT::f32 &&
5020 Op0.hasOneUse() &&
5021 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5022 Op0.getOperand(0).getValueType() == MVT::v2f64 &&
5023 Op0.getOperand(1).getOpcode() == ISD::Constant &&
5024 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) {
5025 SDValue Vec = Op0.getOperand(0);
5026 for (auto *U : Vec->uses()) {
5027 if (U != Op0.getNode() &&
5028 U->hasOneUse() &&
5029 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5030 U->getOperand(0) == Vec &&
5031 U->getOperand(1).getOpcode() == ISD::Constant &&
5032 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) {
5033 SDValue OtherRound = SDValue(*U->use_begin(), 0);
5034 if (OtherRound.getOpcode() == ISD::FP_ROUND &&
5035 OtherRound.getOperand(0) == SDValue(U, 0) &&
5036 OtherRound.getValueType() == MVT::f32) {
5037 SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N),
5038 MVT::v4f32, Vec);
5039 DCI.AddToWorklist(VRound.getNode());
5040 SDValue Extract1 =
5041 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32,
5042 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32));
5043 DCI.AddToWorklist(Extract1.getNode());
5044 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1);
5045 SDValue Extract0 =
5046 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32,
5047 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
5048 return Extract0;
Ulrich Weigand80b3af72015-05-05 19:27:45 +00005049 }
5050 }
5051 }
5052 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005053 return SDValue();
5054}
Bryan Chan28b759c2016-05-16 20:32:22 +00005055
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005056SDValue SystemZTargetLowering::combineBSWAP(
5057 SDNode *N, DAGCombinerInfo &DCI) const {
5058 SelectionDAG &DAG = DCI.DAG;
Bryan Chan28b759c2016-05-16 20:32:22 +00005059 // Combine BSWAP (LOAD) into LRVH/LRV/LRVG
5060 // These loads are allowed to access memory multiple times, and so we must check
5061 // that the loads are not volatile before performing the combine.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005062 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
5063 N->getOperand(0).hasOneUse() &&
5064 (N->getValueType(0) == MVT::i16 || N->getValueType(0) == MVT::i32 ||
5065 N->getValueType(0) == MVT::i64) &&
5066 !cast<LoadSDNode>(N->getOperand(0))->isVolatile()) {
Bryan Chan28b759c2016-05-16 20:32:22 +00005067 SDValue Load = N->getOperand(0);
5068 LoadSDNode *LD = cast<LoadSDNode>(Load);
5069
5070 // Create the byte-swapping load.
5071 SDValue Ops[] = {
5072 LD->getChain(), // Chain
5073 LD->getBasePtr(), // Ptr
5074 DAG.getValueType(N->getValueType(0)) // VT
5075 };
5076 SDValue BSLoad =
5077 DAG.getMemIntrinsicNode(SystemZISD::LRV, SDLoc(N),
5078 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
5079 MVT::i64 : MVT::i32, MVT::Other),
5080 Ops, LD->getMemoryVT(), LD->getMemOperand());
5081
5082 // If this is an i16 load, insert the truncate.
5083 SDValue ResVal = BSLoad;
5084 if (N->getValueType(0) == MVT::i16)
5085 ResVal = DAG.getNode(ISD::TRUNCATE, SDLoc(N), MVT::i16, BSLoad);
5086
5087 // First, combine the bswap away. This makes the value produced by the
5088 // load dead.
5089 DCI.CombineTo(N, ResVal);
5090
5091 // Next, combine the load away, we give it a bogus result value but a real
5092 // chain result. The result value is dead because the bswap is dead.
5093 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
5094
5095 // Return N so it doesn't get rechecked!
5096 return SDValue(N, 0);
5097 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005098 return SDValue();
5099}
Bryan Chan28b759c2016-05-16 20:32:22 +00005100
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005101SDValue SystemZTargetLowering::combineSHIFTROT(
5102 SDNode *N, DAGCombinerInfo &DCI) const {
5103
5104 SelectionDAG &DAG = DCI.DAG;
5105
5106 // Shift/rotate instructions only use the last 6 bits of the second operand
5107 // register. If the second operand is the result of an AND with an immediate
5108 // value that has its last 6 bits set, we can safely remove the AND operation.
Elliot Colp687691a2016-08-18 18:04:26 +00005109 //
5110 // If the AND operation doesn't have the last 6 bits set, we can't remove it
Elliot Colpa4092102016-08-23 14:03:02 +00005111 // entirely, but we can still truncate it to a 16-bit value. This prevents
5112 // us from ending up with a NILL with a signed operand, which will cause the
5113 // instruction printer to abort.
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005114 SDValue N1 = N->getOperand(1);
5115 if (N1.getOpcode() == ISD::AND) {
Elliot Colp687691a2016-08-18 18:04:26 +00005116 SDValue AndMaskOp = N1->getOperand(1);
5117 auto *AndMask = dyn_cast<ConstantSDNode>(AndMaskOp);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005118
5119 // The AND mask is constant
5120 if (AndMask) {
Elliot Colpa4092102016-08-23 14:03:02 +00005121 auto AmtVal = AndMask->getZExtValue();
5122
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005123 // Bottom 6 bits are set
5124 if ((AmtVal & 0x3f) == 0x3f) {
Elliot Colpa4092102016-08-23 14:03:02 +00005125 SDValue AndOp = N1->getOperand(0);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005126
5127 // This is the only use, so remove the node
5128 if (N1.hasOneUse()) {
5129 // Combine the AND away
5130 DCI.CombineTo(N1.getNode(), AndOp);
5131
5132 // Return N so it isn't rechecked
5133 return SDValue(N, 0);
5134
5135 // The node will be reused, so create a new node for this one use
5136 } else {
5137 SDValue Replace = DAG.getNode(N->getOpcode(), SDLoc(N),
5138 N->getValueType(0), N->getOperand(0),
5139 AndOp);
5140 DCI.AddToWorklist(Replace.getNode());
5141
5142 return Replace;
5143 }
Elliot Colp687691a2016-08-18 18:04:26 +00005144
Elliot Colpa4092102016-08-23 14:03:02 +00005145 // We can't remove the AND, but we can use NILL here (normally we would
5146 // use NILF). Only keep the last 16 bits of the mask. The actual
5147 // transformation will be handled by .td definitions.
5148 } else if (AmtVal >> 16 != 0) {
5149 SDValue AndOp = N1->getOperand(0);
Elliot Colp687691a2016-08-18 18:04:26 +00005150
Elliot Colpa4092102016-08-23 14:03:02 +00005151 auto NewMask = DAG.getConstant(AndMask->getZExtValue() & 0x0000ffff,
5152 SDLoc(AndMaskOp),
5153 AndMaskOp.getValueType());
Elliot Colp687691a2016-08-18 18:04:26 +00005154
Elliot Colpa4092102016-08-23 14:03:02 +00005155 auto NewAnd = DAG.getNode(N1.getOpcode(), SDLoc(N1), N1.getValueType(),
5156 AndOp, NewMask);
Elliot Colp687691a2016-08-18 18:04:26 +00005157
Elliot Colpa4092102016-08-23 14:03:02 +00005158 SDValue Replace = DAG.getNode(N->getOpcode(), SDLoc(N),
5159 N->getValueType(0), N->getOperand(0),
5160 NewAnd);
5161 DCI.AddToWorklist(Replace.getNode());
Elliot Colp687691a2016-08-18 18:04:26 +00005162
Elliot Colpa4092102016-08-23 14:03:02 +00005163 return Replace;
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005164 }
5165 }
5166 }
5167
5168 return SDValue();
5169}
5170
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005171SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
5172 DAGCombinerInfo &DCI) const {
5173 switch(N->getOpcode()) {
5174 default: break;
5175 case ISD::SIGN_EXTEND: return combineSIGN_EXTEND(N, DCI);
5176 case SystemZISD::MERGE_HIGH:
5177 case SystemZISD::MERGE_LOW: return combineMERGE(N, DCI);
5178 case ISD::STORE: return combineSTORE(N, DCI);
5179 case ISD::EXTRACT_VECTOR_ELT: return combineEXTRACT_VECTOR_ELT(N, DCI);
5180 case SystemZISD::JOIN_DWORDS: return combineJOIN_DWORDS(N, DCI);
5181 case ISD::FP_ROUND: return combineFP_ROUND(N, DCI);
5182 case ISD::BSWAP: return combineBSWAP(N, DCI);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005183 case ISD::SHL:
5184 case ISD::SRA:
5185 case ISD::SRL:
5186 case ISD::ROTL: return combineSHIFTROT(N, DCI);
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005187 }
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005188
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005189 return SDValue();
5190}
5191
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005192//===----------------------------------------------------------------------===//
5193// Custom insertion
5194//===----------------------------------------------------------------------===//
5195
5196// Create a new basic block after MBB.
5197static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
5198 MachineFunction &MF = *MBB->getParent();
5199 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00005200 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005201 return NewMBB;
5202}
5203
Richard Sandifordbe133a82013-08-28 09:01:51 +00005204// Split MBB after MI and return the new block (the one that contains
5205// instructions after MI).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005206static MachineBasicBlock *splitBlockAfter(MachineBasicBlock::iterator MI,
Richard Sandifordbe133a82013-08-28 09:01:51 +00005207 MachineBasicBlock *MBB) {
5208 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
5209 NewMBB->splice(NewMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00005210 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Richard Sandifordbe133a82013-08-28 09:01:51 +00005211 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
5212 return NewMBB;
5213}
5214
Richard Sandiford5e318f02013-08-27 09:54:29 +00005215// Split MBB before MI and return the new block (the one that contains MI).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005216static MachineBasicBlock *splitBlockBefore(MachineBasicBlock::iterator MI,
Richard Sandiford5e318f02013-08-27 09:54:29 +00005217 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005218 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005219 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005220 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
5221 return NewMBB;
5222}
5223
Richard Sandiford5e318f02013-08-27 09:54:29 +00005224// Force base value Base into a register before MI. Return the register.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005225static unsigned forceReg(MachineInstr &MI, MachineOperand &Base,
Richard Sandiford5e318f02013-08-27 09:54:29 +00005226 const SystemZInstrInfo *TII) {
5227 if (Base.isReg())
5228 return Base.getReg();
5229
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005230 MachineBasicBlock *MBB = MI.getParent();
Richard Sandiford5e318f02013-08-27 09:54:29 +00005231 MachineFunction &MF = *MBB->getParent();
5232 MachineRegisterInfo &MRI = MF.getRegInfo();
5233
5234 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005235 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LA), Reg)
5236 .addOperand(Base)
5237 .addImm(0)
5238 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005239 return Reg;
5240}
5241
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005242// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
5243MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005244SystemZTargetLowering::emitSelect(MachineInstr &MI,
Ulrich Weigand524f2762016-11-28 13:34:08 +00005245 MachineBasicBlock *MBB,
5246 unsigned LOCROpcode) const {
Eric Christophera6734172015-01-31 00:06:45 +00005247 const SystemZInstrInfo *TII =
5248 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005249
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005250 unsigned DestReg = MI.getOperand(0).getReg();
5251 unsigned TrueReg = MI.getOperand(1).getReg();
5252 unsigned FalseReg = MI.getOperand(2).getReg();
5253 unsigned CCValid = MI.getOperand(3).getImm();
5254 unsigned CCMask = MI.getOperand(4).getImm();
5255 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005256
Ulrich Weigand524f2762016-11-28 13:34:08 +00005257 // Use LOCROpcode if possible.
5258 if (LOCROpcode && Subtarget.hasLoadStoreOnCond()) {
5259 BuildMI(*MBB, MI, DL, TII->get(LOCROpcode), DestReg)
5260 .addReg(FalseReg).addReg(TrueReg)
5261 .addImm(CCValid).addImm(CCMask);
5262 MI.eraseFromParent();
5263 return MBB;
5264 }
5265
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005266 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005267 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005268 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5269
5270 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00005271 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005272 // # fallthrough to FalseMBB
5273 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005274 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5275 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005276 MBB->addSuccessor(JoinMBB);
5277 MBB->addSuccessor(FalseMBB);
5278
5279 // FalseMBB:
5280 // # fallthrough to JoinMBB
5281 MBB = FalseMBB;
5282 MBB->addSuccessor(JoinMBB);
5283
5284 // JoinMBB:
5285 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
5286 // ...
5287 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005288 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005289 .addReg(TrueReg).addMBB(StartMBB)
5290 .addReg(FalseReg).addMBB(FalseMBB);
5291
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005292 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005293 return JoinMBB;
5294}
5295
Richard Sandifordb86a8342013-06-27 09:27:40 +00005296// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
5297// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005298// happen when the condition is false rather than true. If a STORE ON
5299// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005300MachineBasicBlock *SystemZTargetLowering::emitCondStore(MachineInstr &MI,
5301 MachineBasicBlock *MBB,
5302 unsigned StoreOpcode,
5303 unsigned STOCOpcode,
5304 bool Invert) const {
Eric Christophera6734172015-01-31 00:06:45 +00005305 const SystemZInstrInfo *TII =
5306 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordb86a8342013-06-27 09:27:40 +00005307
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005308 unsigned SrcReg = MI.getOperand(0).getReg();
5309 MachineOperand Base = MI.getOperand(1);
5310 int64_t Disp = MI.getOperand(2).getImm();
5311 unsigned IndexReg = MI.getOperand(3).getReg();
5312 unsigned CCValid = MI.getOperand(4).getImm();
5313 unsigned CCMask = MI.getOperand(5).getImm();
5314 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005315
5316 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
5317
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005318 // Use STOCOpcode if possible. We could use different store patterns in
5319 // order to avoid matching the index register, but the performance trade-offs
5320 // might be more complicated in that case.
Eric Christopher93bf97c2014-06-27 07:38:01 +00005321 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005322 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005323 CCMask ^= CCValid;
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005324 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +00005325 .addReg(SrcReg).addOperand(Base).addImm(Disp)
5326 .addImm(CCValid).addImm(CCMask);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005327 MI.eraseFromParent();
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005328 return MBB;
5329 }
5330
Richard Sandifordb86a8342013-06-27 09:27:40 +00005331 // Get the condition needed to branch around the store.
5332 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005333 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00005334
5335 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005336 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005337 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5338
5339 // StartMBB:
5340 // BRC CCMask, JoinMBB
5341 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00005342 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005343 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5344 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005345 MBB->addSuccessor(JoinMBB);
5346 MBB->addSuccessor(FalseMBB);
5347
5348 // FalseMBB:
5349 // store %SrcReg, %Disp(%Index,%Base)
5350 // # fallthrough to JoinMBB
5351 MBB = FalseMBB;
5352 BuildMI(MBB, DL, TII->get(StoreOpcode))
5353 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
5354 MBB->addSuccessor(JoinMBB);
5355
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005356 MI.eraseFromParent();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005357 return JoinMBB;
5358}
5359
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005360// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
5361// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
5362// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
5363// BitSize is the width of the field in bits, or 0 if this is a partword
5364// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
5365// is one of the operands. Invert says whether the field should be
5366// inverted after performing BinOpcode (e.g. for NAND).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005367MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadBinary(
5368 MachineInstr &MI, MachineBasicBlock *MBB, unsigned BinOpcode,
5369 unsigned BitSize, bool Invert) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005370 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005371 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005372 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005373 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005374 bool IsSubWord = (BitSize < 32);
5375
5376 // Extract the operands. Base can be a register or a frame index.
5377 // Src2 can be a register or immediate.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005378 unsigned Dest = MI.getOperand(0).getReg();
5379 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5380 int64_t Disp = MI.getOperand(2).getImm();
5381 MachineOperand Src2 = earlyUseOperand(MI.getOperand(3));
5382 unsigned BitShift = (IsSubWord ? MI.getOperand(4).getReg() : 0);
5383 unsigned NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : 0);
5384 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005385 if (IsSubWord)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005386 BitSize = MI.getOperand(6).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005387
5388 // Subword operations use 32-bit registers.
5389 const TargetRegisterClass *RC = (BitSize <= 32 ?
5390 &SystemZ::GR32BitRegClass :
5391 &SystemZ::GR64BitRegClass);
5392 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5393 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5394
5395 // Get the right opcodes for the displacement.
5396 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5397 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5398 assert(LOpcode && CSOpcode && "Displacement out of range");
5399
5400 // Create virtual registers for temporary results.
5401 unsigned OrigVal = MRI.createVirtualRegister(RC);
5402 unsigned OldVal = MRI.createVirtualRegister(RC);
5403 unsigned NewVal = (BinOpcode || IsSubWord ?
5404 MRI.createVirtualRegister(RC) : Src2.getReg());
5405 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5406 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5407
5408 // Insert a basic block for the main loop.
5409 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005410 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005411 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5412
5413 // StartMBB:
5414 // ...
5415 // %OrigVal = L Disp(%Base)
5416 // # fall through to LoopMMB
5417 MBB = StartMBB;
5418 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
5419 .addOperand(Base).addImm(Disp).addReg(0);
5420 MBB->addSuccessor(LoopMBB);
5421
5422 // LoopMBB:
5423 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
5424 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5425 // %RotatedNewVal = OP %RotatedOldVal, %Src2
5426 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5427 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5428 // JNE LoopMBB
5429 // # fall through to DoneMMB
5430 MBB = LoopMBB;
5431 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5432 .addReg(OrigVal).addMBB(StartMBB)
5433 .addReg(Dest).addMBB(LoopMBB);
5434 if (IsSubWord)
5435 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5436 .addReg(OldVal).addReg(BitShift).addImm(0);
5437 if (Invert) {
5438 // Perform the operation normally and then invert every bit of the field.
5439 unsigned Tmp = MRI.createVirtualRegister(RC);
5440 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
5441 .addReg(RotatedOldVal).addOperand(Src2);
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005442 if (BitSize <= 32)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005443 // XILF with the upper BitSize bits set.
Richard Sandiford652784e2013-09-25 11:11:53 +00005444 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005445 .addReg(Tmp).addImm(-1U << (32 - BitSize));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005446 else {
5447 // Use LCGR and add -1 to the result, which is more compact than
5448 // an XILF, XILH pair.
5449 unsigned Tmp2 = MRI.createVirtualRegister(RC);
5450 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
5451 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
5452 .addReg(Tmp2).addImm(-1);
5453 }
5454 } else if (BinOpcode)
5455 // A simply binary operation.
5456 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
5457 .addReg(RotatedOldVal).addOperand(Src2);
5458 else if (IsSubWord)
5459 // Use RISBG to rotate Src2 into position and use it to replace the
5460 // field in RotatedOldVal.
5461 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
5462 .addReg(RotatedOldVal).addReg(Src2.getReg())
5463 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
5464 if (IsSubWord)
5465 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5466 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5467 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
5468 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005469 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5470 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005471 MBB->addSuccessor(LoopMBB);
5472 MBB->addSuccessor(DoneMBB);
5473
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005474 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005475 return DoneMBB;
5476}
5477
5478// Implement EmitInstrWithCustomInserter for pseudo
5479// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
5480// instruction that should be used to compare the current field with the
5481// minimum or maximum value. KeepOldMask is the BRC condition-code mask
5482// for when the current field should be kept. BitSize is the width of
5483// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005484MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadMinMax(
5485 MachineInstr &MI, MachineBasicBlock *MBB, unsigned CompareOpcode,
5486 unsigned KeepOldMask, unsigned BitSize) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005487 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005488 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005489 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005490 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005491 bool IsSubWord = (BitSize < 32);
5492
5493 // Extract the operands. Base can be a register or a frame index.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005494 unsigned Dest = MI.getOperand(0).getReg();
5495 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5496 int64_t Disp = MI.getOperand(2).getImm();
5497 unsigned Src2 = MI.getOperand(3).getReg();
5498 unsigned BitShift = (IsSubWord ? MI.getOperand(4).getReg() : 0);
5499 unsigned NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : 0);
5500 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005501 if (IsSubWord)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005502 BitSize = MI.getOperand(6).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005503
5504 // Subword operations use 32-bit registers.
5505 const TargetRegisterClass *RC = (BitSize <= 32 ?
5506 &SystemZ::GR32BitRegClass :
5507 &SystemZ::GR64BitRegClass);
5508 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5509 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5510
5511 // Get the right opcodes for the displacement.
5512 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5513 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5514 assert(LOpcode && CSOpcode && "Displacement out of range");
5515
5516 // Create virtual registers for temporary results.
5517 unsigned OrigVal = MRI.createVirtualRegister(RC);
5518 unsigned OldVal = MRI.createVirtualRegister(RC);
5519 unsigned NewVal = MRI.createVirtualRegister(RC);
5520 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5521 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
5522 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5523
5524 // Insert 3 basic blocks for the loop.
5525 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005526 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005527 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5528 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
5529 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
5530
5531 // StartMBB:
5532 // ...
5533 // %OrigVal = L Disp(%Base)
5534 // # fall through to LoopMMB
5535 MBB = StartMBB;
5536 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
5537 .addOperand(Base).addImm(Disp).addReg(0);
5538 MBB->addSuccessor(LoopMBB);
5539
5540 // LoopMBB:
5541 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
5542 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5543 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00005544 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005545 MBB = LoopMBB;
5546 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5547 .addReg(OrigVal).addMBB(StartMBB)
5548 .addReg(Dest).addMBB(UpdateMBB);
5549 if (IsSubWord)
5550 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5551 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005552 BuildMI(MBB, DL, TII->get(CompareOpcode))
5553 .addReg(RotatedOldVal).addReg(Src2);
5554 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005555 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005556 MBB->addSuccessor(UpdateMBB);
5557 MBB->addSuccessor(UseAltMBB);
5558
5559 // UseAltMBB:
5560 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
5561 // # fall through to UpdateMMB
5562 MBB = UseAltMBB;
5563 if (IsSubWord)
5564 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
5565 .addReg(RotatedOldVal).addReg(Src2)
5566 .addImm(32).addImm(31 + BitSize).addImm(0);
5567 MBB->addSuccessor(UpdateMBB);
5568
5569 // UpdateMBB:
5570 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
5571 // [ %RotatedAltVal, UseAltMBB ]
5572 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5573 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5574 // JNE LoopMBB
5575 // # fall through to DoneMMB
5576 MBB = UpdateMBB;
5577 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
5578 .addReg(RotatedOldVal).addMBB(LoopMBB)
5579 .addReg(RotatedAltVal).addMBB(UseAltMBB);
5580 if (IsSubWord)
5581 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5582 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5583 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
5584 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005585 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5586 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005587 MBB->addSuccessor(LoopMBB);
5588 MBB->addSuccessor(DoneMBB);
5589
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005590 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005591 return DoneMBB;
5592}
5593
5594// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
5595// instruction MI.
5596MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005597SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005598 MachineBasicBlock *MBB) const {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00005599
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005600 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005601 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005602 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005603 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005604
5605 // Extract the operands. Base can be a register or a frame index.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005606 unsigned Dest = MI.getOperand(0).getReg();
5607 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5608 int64_t Disp = MI.getOperand(2).getImm();
5609 unsigned OrigCmpVal = MI.getOperand(3).getReg();
5610 unsigned OrigSwapVal = MI.getOperand(4).getReg();
5611 unsigned BitShift = MI.getOperand(5).getReg();
5612 unsigned NegBitShift = MI.getOperand(6).getReg();
5613 int64_t BitSize = MI.getOperand(7).getImm();
5614 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005615
5616 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
5617
5618 // Get the right opcodes for the displacement.
5619 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
5620 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
5621 assert(LOpcode && CSOpcode && "Displacement out of range");
5622
5623 // Create virtual registers for temporary results.
5624 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
5625 unsigned OldVal = MRI.createVirtualRegister(RC);
5626 unsigned CmpVal = MRI.createVirtualRegister(RC);
5627 unsigned SwapVal = MRI.createVirtualRegister(RC);
5628 unsigned StoreVal = MRI.createVirtualRegister(RC);
5629 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
5630 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
5631 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
5632
5633 // Insert 2 basic blocks for the loop.
5634 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005635 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005636 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5637 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
5638
5639 // StartMBB:
5640 // ...
5641 // %OrigOldVal = L Disp(%Base)
5642 // # fall through to LoopMMB
5643 MBB = StartMBB;
5644 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
5645 .addOperand(Base).addImm(Disp).addReg(0);
5646 MBB->addSuccessor(LoopMBB);
5647
5648 // LoopMBB:
5649 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
5650 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
5651 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
5652 // %Dest = RLL %OldVal, BitSize(%BitShift)
5653 // ^^ The low BitSize bits contain the field
5654 // of interest.
5655 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
5656 // ^^ Replace the upper 32-BitSize bits of the
5657 // comparison value with those that we loaded,
5658 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005659 // CR %Dest, %RetryCmpVal
5660 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005661 // # Fall through to SetMBB
5662 MBB = LoopMBB;
5663 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5664 .addReg(OrigOldVal).addMBB(StartMBB)
5665 .addReg(RetryOldVal).addMBB(SetMBB);
5666 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
5667 .addReg(OrigCmpVal).addMBB(StartMBB)
5668 .addReg(RetryCmpVal).addMBB(SetMBB);
5669 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
5670 .addReg(OrigSwapVal).addMBB(StartMBB)
5671 .addReg(RetrySwapVal).addMBB(SetMBB);
5672 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
5673 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
5674 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
5675 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005676 BuildMI(MBB, DL, TII->get(SystemZ::CR))
5677 .addReg(Dest).addReg(RetryCmpVal);
5678 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005679 .addImm(SystemZ::CCMASK_ICMP)
5680 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005681 MBB->addSuccessor(DoneMBB);
5682 MBB->addSuccessor(SetMBB);
5683
5684 // SetMBB:
5685 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
5686 // ^^ Replace the upper 32-BitSize bits of the new
5687 // value with those that we loaded.
5688 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
5689 // ^^ Rotate the new field to its proper position.
5690 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
5691 // JNE LoopMBB
5692 // # fall through to ExitMMB
5693 MBB = SetMBB;
5694 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
5695 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
5696 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
5697 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
5698 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
5699 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005700 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5701 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005702 MBB->addSuccessor(LoopMBB);
5703 MBB->addSuccessor(DoneMBB);
5704
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005705 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005706 return DoneMBB;
5707}
5708
5709// Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true
5710// if the high register of the GR128 value must be cleared or false if
Richard Sandiford87a44362013-09-30 10:28:35 +00005711// it's "don't care". SubReg is subreg_l32 when extending a GR32
5712// and subreg_l64 when extending a GR64.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005713MachineBasicBlock *SystemZTargetLowering::emitExt128(MachineInstr &MI,
5714 MachineBasicBlock *MBB,
5715 bool ClearEven,
5716 unsigned SubReg) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005717 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005718 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005719 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005720 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005721 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005722
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005723 unsigned Dest = MI.getOperand(0).getReg();
5724 unsigned Src = MI.getOperand(1).getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005725 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
5726
5727 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
5728 if (ClearEven) {
5729 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
5730 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
5731
5732 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
5733 .addImm(0);
5734 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
Richard Sandiford87a44362013-09-30 10:28:35 +00005735 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005736 In128 = NewIn128;
5737 }
5738 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
5739 .addReg(In128).addReg(Src).addImm(SubReg);
5740
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005741 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005742 return MBB;
5743}
5744
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005745MachineBasicBlock *SystemZTargetLowering::emitMemMemWrapper(
5746 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +00005747 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005748 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005749 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandiford5e318f02013-08-27 09:54:29 +00005750 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005751 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordd131ff82013-07-08 09:35:23 +00005752
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005753 MachineOperand DestBase = earlyUseOperand(MI.getOperand(0));
5754 uint64_t DestDisp = MI.getOperand(1).getImm();
5755 MachineOperand SrcBase = earlyUseOperand(MI.getOperand(2));
5756 uint64_t SrcDisp = MI.getOperand(3).getImm();
5757 uint64_t Length = MI.getOperand(4).getImm();
Richard Sandifordd131ff82013-07-08 09:35:23 +00005758
Richard Sandifordbe133a82013-08-28 09:01:51 +00005759 // When generating more than one CLC, all but the last will need to
5760 // branch to the end when a difference is found.
5761 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
Craig Topper062a2ba2014-04-25 05:30:21 +00005762 splitBlockAfter(MI, MBB) : nullptr);
Richard Sandifordbe133a82013-08-28 09:01:51 +00005763
Richard Sandiford5e318f02013-08-27 09:54:29 +00005764 // Check for the loop form, in which operand 5 is the trip count.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005765 if (MI.getNumExplicitOperands() > 5) {
Richard Sandiford5e318f02013-08-27 09:54:29 +00005766 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
5767
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005768 uint64_t StartCountReg = MI.getOperand(5).getReg();
Richard Sandiford5e318f02013-08-27 09:54:29 +00005769 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
5770 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
5771 forceReg(MI, DestBase, TII));
5772
5773 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
5774 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
5775 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
5776 MRI.createVirtualRegister(RC));
5777 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
5778 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
5779 MRI.createVirtualRegister(RC));
5780
5781 RC = &SystemZ::GR64BitRegClass;
5782 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
5783 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
5784
5785 MachineBasicBlock *StartMBB = MBB;
5786 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
5787 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00005788 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005789
5790 // StartMBB:
5791 // # fall through to LoopMMB
5792 MBB->addSuccessor(LoopMBB);
5793
5794 // LoopMBB:
5795 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005796 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00005797 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005798 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00005799 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005800 // [ %NextCountReg, NextMBB ]
5801 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00005802 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00005803 // ( JLH EndMBB )
5804 //
5805 // The prefetch is used only for MVC. The JLH is used only for CLC.
5806 MBB = LoopMBB;
5807
5808 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
5809 .addReg(StartDestReg).addMBB(StartMBB)
5810 .addReg(NextDestReg).addMBB(NextMBB);
5811 if (!HaveSingleBase)
5812 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
5813 .addReg(StartSrcReg).addMBB(StartMBB)
5814 .addReg(NextSrcReg).addMBB(NextMBB);
5815 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
5816 .addReg(StartCountReg).addMBB(StartMBB)
5817 .addReg(NextCountReg).addMBB(NextMBB);
5818 if (Opcode == SystemZ::MVC)
5819 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
5820 .addImm(SystemZ::PFD_WRITE)
5821 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
5822 BuildMI(MBB, DL, TII->get(Opcode))
5823 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
5824 .addReg(ThisSrcReg).addImm(SrcDisp);
5825 if (EndMBB) {
5826 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5827 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5828 .addMBB(EndMBB);
5829 MBB->addSuccessor(EndMBB);
5830 MBB->addSuccessor(NextMBB);
5831 }
5832
5833 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00005834 // %NextDestReg = LA 256(%ThisDestReg)
5835 // %NextSrcReg = LA 256(%ThisSrcReg)
5836 // %NextCountReg = AGHI %ThisCountReg, -1
5837 // CGHI %NextCountReg, 0
5838 // JLH LoopMBB
5839 // # fall through to DoneMMB
5840 //
5841 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00005842 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005843
Richard Sandiford5e318f02013-08-27 09:54:29 +00005844 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
5845 .addReg(ThisDestReg).addImm(256).addReg(0);
5846 if (!HaveSingleBase)
5847 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
5848 .addReg(ThisSrcReg).addImm(256).addReg(0);
5849 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
5850 .addReg(ThisCountReg).addImm(-1);
5851 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
5852 .addReg(NextCountReg).addImm(0);
5853 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5854 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5855 .addMBB(LoopMBB);
5856 MBB->addSuccessor(LoopMBB);
5857 MBB->addSuccessor(DoneMBB);
5858
5859 DestBase = MachineOperand::CreateReg(NextDestReg, false);
5860 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
5861 Length &= 255;
5862 MBB = DoneMBB;
5863 }
5864 // Handle any remaining bytes with straight-line code.
5865 while (Length > 0) {
5866 uint64_t ThisLength = std::min(Length, uint64_t(256));
5867 // The previous iteration might have created out-of-range displacements.
5868 // Apply them using LAY if so.
5869 if (!isUInt<12>(DestDisp)) {
5870 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005871 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg)
5872 .addOperand(DestBase)
5873 .addImm(DestDisp)
5874 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005875 DestBase = MachineOperand::CreateReg(Reg, false);
5876 DestDisp = 0;
5877 }
5878 if (!isUInt<12>(SrcDisp)) {
5879 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005880 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg)
5881 .addOperand(SrcBase)
5882 .addImm(SrcDisp)
5883 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005884 SrcBase = MachineOperand::CreateReg(Reg, false);
5885 SrcDisp = 0;
5886 }
5887 BuildMI(*MBB, MI, DL, TII->get(Opcode))
5888 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength)
5889 .addOperand(SrcBase).addImm(SrcDisp);
5890 DestDisp += ThisLength;
5891 SrcDisp += ThisLength;
5892 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00005893 // If there's another CLC to go, branch to the end if a difference
5894 // was found.
5895 if (EndMBB && Length > 0) {
5896 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
5897 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5898 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5899 .addMBB(EndMBB);
5900 MBB->addSuccessor(EndMBB);
5901 MBB->addSuccessor(NextMBB);
5902 MBB = NextMBB;
5903 }
5904 }
5905 if (EndMBB) {
5906 MBB->addSuccessor(EndMBB);
5907 MBB = EndMBB;
5908 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005909 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00005910
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005911 MI.eraseFromParent();
Richard Sandifordd131ff82013-07-08 09:35:23 +00005912 return MBB;
5913}
5914
Richard Sandifordca232712013-08-16 11:21:54 +00005915// Decompose string pseudo-instruction MI into a loop that continually performs
5916// Opcode until CC != 3.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005917MachineBasicBlock *SystemZTargetLowering::emitStringWrapper(
5918 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Richard Sandifordca232712013-08-16 11:21:54 +00005919 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005920 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005921 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordca232712013-08-16 11:21:54 +00005922 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005923 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordca232712013-08-16 11:21:54 +00005924
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005925 uint64_t End1Reg = MI.getOperand(0).getReg();
5926 uint64_t Start1Reg = MI.getOperand(1).getReg();
5927 uint64_t Start2Reg = MI.getOperand(2).getReg();
5928 uint64_t CharReg = MI.getOperand(3).getReg();
Richard Sandifordca232712013-08-16 11:21:54 +00005929
5930 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
5931 uint64_t This1Reg = MRI.createVirtualRegister(RC);
5932 uint64_t This2Reg = MRI.createVirtualRegister(RC);
5933 uint64_t End2Reg = MRI.createVirtualRegister(RC);
5934
5935 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005936 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00005937 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5938
5939 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00005940 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00005941 MBB->addSuccessor(LoopMBB);
5942
5943 // LoopMBB:
5944 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
5945 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford7789b082013-09-30 08:48:38 +00005946 // R0L = %CharReg
5947 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
Richard Sandifordca232712013-08-16 11:21:54 +00005948 // JO LoopMBB
5949 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00005950 //
Richard Sandiford7789b082013-09-30 08:48:38 +00005951 // The load of R0L can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00005952 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00005953
5954 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
5955 .addReg(Start1Reg).addMBB(StartMBB)
5956 .addReg(End1Reg).addMBB(LoopMBB);
5957 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
5958 .addReg(Start2Reg).addMBB(StartMBB)
5959 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford7789b082013-09-30 08:48:38 +00005960 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00005961 BuildMI(MBB, DL, TII->get(Opcode))
5962 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
5963 .addReg(This1Reg).addReg(This2Reg);
5964 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5965 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
5966 MBB->addSuccessor(LoopMBB);
5967 MBB->addSuccessor(DoneMBB);
5968
5969 DoneMBB->addLiveIn(SystemZ::CC);
5970
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005971 MI.eraseFromParent();
Richard Sandifordca232712013-08-16 11:21:54 +00005972 return DoneMBB;
5973}
5974
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005975// Update TBEGIN instruction with final opcode and register clobbers.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005976MachineBasicBlock *SystemZTargetLowering::emitTransactionBegin(
5977 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode,
5978 bool NoFloat) const {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005979 MachineFunction &MF = *MBB->getParent();
5980 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
5981 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
5982
5983 // Update opcode.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005984 MI.setDesc(TII->get(Opcode));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005985
5986 // We cannot handle a TBEGIN that clobbers the stack or frame pointer.
5987 // Make sure to add the corresponding GRSM bits if they are missing.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005988 uint64_t Control = MI.getOperand(2).getImm();
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005989 static const unsigned GPRControlBit[16] = {
5990 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000,
5991 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100
5992 };
5993 Control |= GPRControlBit[15];
5994 if (TFI->hasFP(MF))
5995 Control |= GPRControlBit[11];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005996 MI.getOperand(2).setImm(Control);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005997
5998 // Add GPR clobbers.
5999 for (int I = 0; I < 16; I++) {
6000 if ((Control & GPRControlBit[I]) == 0) {
6001 unsigned Reg = SystemZMC::GR64Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006002 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006003 }
6004 }
6005
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006006 // Add FPR/VR clobbers.
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006007 if (!NoFloat && (Control & 4) != 0) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006008 if (Subtarget.hasVector()) {
6009 for (int I = 0; I < 32; I++) {
6010 unsigned Reg = SystemZMC::VR128Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006011 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006012 }
6013 } else {
6014 for (int I = 0; I < 16; I++) {
6015 unsigned Reg = SystemZMC::FP64Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006016 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006017 }
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006018 }
6019 }
6020
6021 return MBB;
6022}
6023
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006024MachineBasicBlock *SystemZTargetLowering::emitLoadAndTestCmp0(
6025 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006026 MachineFunction &MF = *MBB->getParent();
6027 MachineRegisterInfo *MRI = &MF.getRegInfo();
6028 const SystemZInstrInfo *TII =
6029 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006030 DebugLoc DL = MI.getDebugLoc();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006031
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006032 unsigned SrcReg = MI.getOperand(0).getReg();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006033
6034 // Create new virtual register of the same class as source.
6035 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
6036 unsigned DstReg = MRI->createVirtualRegister(RC);
6037
6038 // Replace pseudo with a normal load-and-test that models the def as
6039 // well.
6040 BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg)
6041 .addReg(SrcReg);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006042 MI.eraseFromParent();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006043
6044 return MBB;
6045}
6046
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006047MachineBasicBlock *SystemZTargetLowering::EmitInstrWithCustomInserter(
6048 MachineInstr &MI, MachineBasicBlock *MBB) const {
6049 switch (MI.getOpcode()) {
Richard Sandiford7c5c0ea2013-10-01 13:10:16 +00006050 case SystemZ::Select32Mux:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006051 return emitSelect(MI, MBB,
6052 Subtarget.hasLoadStoreOnCond2()? SystemZ::LOCRMux : 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006053 case SystemZ::Select32:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006054 return emitSelect(MI, MBB, SystemZ::LOCR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006055 case SystemZ::Select64:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006056 return emitSelect(MI, MBB, SystemZ::LOCGR);
6057 case SystemZ::SelectF32:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006058 case SystemZ::SelectF64:
6059 case SystemZ::SelectF128:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006060 return emitSelect(MI, MBB, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006061
Richard Sandiford2896d042013-10-01 14:33:55 +00006062 case SystemZ::CondStore8Mux:
6063 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
6064 case SystemZ::CondStore8MuxInv:
6065 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
6066 case SystemZ::CondStore16Mux:
6067 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
6068 case SystemZ::CondStore16MuxInv:
6069 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
Ulrich Weigand524f2762016-11-28 13:34:08 +00006070 case SystemZ::CondStore32Mux:
6071 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, false);
6072 case SystemZ::CondStore32MuxInv:
6073 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006074 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006075 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006076 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006077 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006078 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006079 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006080 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006081 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006082 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006083 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006084 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006085 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006086 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006087 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006088 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006089 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006090 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006091 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006092 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006093 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006094 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006095 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006096 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006097 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006098
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006099 case SystemZ::AEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00006100 return emitExt128(MI, MBB, false, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006101 case SystemZ::ZEXT128_32:
Richard Sandiford87a44362013-09-30 10:28:35 +00006102 return emitExt128(MI, MBB, true, SystemZ::subreg_l32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006103 case SystemZ::ZEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00006104 return emitExt128(MI, MBB, true, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006105
6106 case SystemZ::ATOMIC_SWAPW:
6107 return emitAtomicLoadBinary(MI, MBB, 0, 0);
6108 case SystemZ::ATOMIC_SWAP_32:
6109 return emitAtomicLoadBinary(MI, MBB, 0, 32);
6110 case SystemZ::ATOMIC_SWAP_64:
6111 return emitAtomicLoadBinary(MI, MBB, 0, 64);
6112
6113 case SystemZ::ATOMIC_LOADW_AR:
6114 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
6115 case SystemZ::ATOMIC_LOADW_AFI:
6116 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
6117 case SystemZ::ATOMIC_LOAD_AR:
6118 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
6119 case SystemZ::ATOMIC_LOAD_AHI:
6120 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
6121 case SystemZ::ATOMIC_LOAD_AFI:
6122 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
6123 case SystemZ::ATOMIC_LOAD_AGR:
6124 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
6125 case SystemZ::ATOMIC_LOAD_AGHI:
6126 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
6127 case SystemZ::ATOMIC_LOAD_AGFI:
6128 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
6129
6130 case SystemZ::ATOMIC_LOADW_SR:
6131 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
6132 case SystemZ::ATOMIC_LOAD_SR:
6133 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
6134 case SystemZ::ATOMIC_LOAD_SGR:
6135 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
6136
6137 case SystemZ::ATOMIC_LOADW_NR:
6138 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
6139 case SystemZ::ATOMIC_LOADW_NILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00006140 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006141 case SystemZ::ATOMIC_LOAD_NR:
6142 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006143 case SystemZ::ATOMIC_LOAD_NILL:
6144 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
6145 case SystemZ::ATOMIC_LOAD_NILH:
6146 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
6147 case SystemZ::ATOMIC_LOAD_NILF:
6148 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006149 case SystemZ::ATOMIC_LOAD_NGR:
6150 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006151 case SystemZ::ATOMIC_LOAD_NILL64:
6152 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
6153 case SystemZ::ATOMIC_LOAD_NILH64:
6154 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00006155 case SystemZ::ATOMIC_LOAD_NIHL64:
6156 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
6157 case SystemZ::ATOMIC_LOAD_NIHH64:
6158 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006159 case SystemZ::ATOMIC_LOAD_NILF64:
6160 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00006161 case SystemZ::ATOMIC_LOAD_NIHF64:
6162 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006163
6164 case SystemZ::ATOMIC_LOADW_OR:
6165 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
6166 case SystemZ::ATOMIC_LOADW_OILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00006167 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006168 case SystemZ::ATOMIC_LOAD_OR:
6169 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006170 case SystemZ::ATOMIC_LOAD_OILL:
6171 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
6172 case SystemZ::ATOMIC_LOAD_OILH:
6173 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
6174 case SystemZ::ATOMIC_LOAD_OILF:
6175 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006176 case SystemZ::ATOMIC_LOAD_OGR:
6177 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006178 case SystemZ::ATOMIC_LOAD_OILL64:
6179 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
6180 case SystemZ::ATOMIC_LOAD_OILH64:
6181 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00006182 case SystemZ::ATOMIC_LOAD_OIHL64:
6183 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
6184 case SystemZ::ATOMIC_LOAD_OIHH64:
6185 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006186 case SystemZ::ATOMIC_LOAD_OILF64:
6187 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00006188 case SystemZ::ATOMIC_LOAD_OIHF64:
6189 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006190
6191 case SystemZ::ATOMIC_LOADW_XR:
6192 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
6193 case SystemZ::ATOMIC_LOADW_XILF:
Richard Sandiford652784e2013-09-25 11:11:53 +00006194 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006195 case SystemZ::ATOMIC_LOAD_XR:
6196 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006197 case SystemZ::ATOMIC_LOAD_XILF:
6198 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006199 case SystemZ::ATOMIC_LOAD_XGR:
6200 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006201 case SystemZ::ATOMIC_LOAD_XILF64:
6202 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
Richard Sandiford5718dac2013-10-01 14:08:44 +00006203 case SystemZ::ATOMIC_LOAD_XIHF64:
6204 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006205
6206 case SystemZ::ATOMIC_LOADW_NRi:
6207 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
6208 case SystemZ::ATOMIC_LOADW_NILHi:
Richard Sandiford652784e2013-09-25 11:11:53 +00006209 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006210 case SystemZ::ATOMIC_LOAD_NRi:
6211 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006212 case SystemZ::ATOMIC_LOAD_NILLi:
6213 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
6214 case SystemZ::ATOMIC_LOAD_NILHi:
6215 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
6216 case SystemZ::ATOMIC_LOAD_NILFi:
6217 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006218 case SystemZ::ATOMIC_LOAD_NGRi:
6219 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006220 case SystemZ::ATOMIC_LOAD_NILL64i:
6221 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
6222 case SystemZ::ATOMIC_LOAD_NILH64i:
6223 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00006224 case SystemZ::ATOMIC_LOAD_NIHL64i:
6225 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
6226 case SystemZ::ATOMIC_LOAD_NIHH64i:
6227 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006228 case SystemZ::ATOMIC_LOAD_NILF64i:
6229 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00006230 case SystemZ::ATOMIC_LOAD_NIHF64i:
6231 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006232
6233 case SystemZ::ATOMIC_LOADW_MIN:
6234 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6235 SystemZ::CCMASK_CMP_LE, 0);
6236 case SystemZ::ATOMIC_LOAD_MIN_32:
6237 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6238 SystemZ::CCMASK_CMP_LE, 32);
6239 case SystemZ::ATOMIC_LOAD_MIN_64:
6240 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
6241 SystemZ::CCMASK_CMP_LE, 64);
6242
6243 case SystemZ::ATOMIC_LOADW_MAX:
6244 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6245 SystemZ::CCMASK_CMP_GE, 0);
6246 case SystemZ::ATOMIC_LOAD_MAX_32:
6247 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6248 SystemZ::CCMASK_CMP_GE, 32);
6249 case SystemZ::ATOMIC_LOAD_MAX_64:
6250 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
6251 SystemZ::CCMASK_CMP_GE, 64);
6252
6253 case SystemZ::ATOMIC_LOADW_UMIN:
6254 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6255 SystemZ::CCMASK_CMP_LE, 0);
6256 case SystemZ::ATOMIC_LOAD_UMIN_32:
6257 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6258 SystemZ::CCMASK_CMP_LE, 32);
6259 case SystemZ::ATOMIC_LOAD_UMIN_64:
6260 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6261 SystemZ::CCMASK_CMP_LE, 64);
6262
6263 case SystemZ::ATOMIC_LOADW_UMAX:
6264 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6265 SystemZ::CCMASK_CMP_GE, 0);
6266 case SystemZ::ATOMIC_LOAD_UMAX_32:
6267 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6268 SystemZ::CCMASK_CMP_GE, 32);
6269 case SystemZ::ATOMIC_LOAD_UMAX_64:
6270 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6271 SystemZ::CCMASK_CMP_GE, 64);
6272
6273 case SystemZ::ATOMIC_CMP_SWAPW:
6274 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006275 case SystemZ::MVCSequence:
6276 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006277 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00006278 case SystemZ::NCSequence:
6279 case SystemZ::NCLoop:
6280 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
6281 case SystemZ::OCSequence:
6282 case SystemZ::OCLoop:
6283 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
6284 case SystemZ::XCSequence:
6285 case SystemZ::XCLoop:
6286 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006287 case SystemZ::CLCSequence:
6288 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006289 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00006290 case SystemZ::CLSTLoop:
6291 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00006292 case SystemZ::MVSTLoop:
6293 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00006294 case SystemZ::SRSTLoop:
6295 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006296 case SystemZ::TBEGIN:
6297 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false);
6298 case SystemZ::TBEGIN_nofloat:
6299 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true);
6300 case SystemZ::TBEGINC:
6301 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true);
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006302 case SystemZ::LTEBRCompare_VecPseudo:
6303 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR);
6304 case SystemZ::LTDBRCompare_VecPseudo:
6305 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR);
6306 case SystemZ::LTXBRCompare_VecPseudo:
6307 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR);
6308
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006309 default:
6310 llvm_unreachable("Unexpected instr type to insert");
6311 }
6312}