blob: 0ca8bcd2df47e76d9e83a3eb8faab247ae6e0047 [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"
Will Dietz981af002013-10-12 00:55:57 +000023#include <cctype>
24
Ulrich Weigand5f613df2013-05-06 16:15:19 +000025using namespace llvm;
26
Chandler Carruth84e68b22014-04-22 02:41:26 +000027#define DEBUG_TYPE "systemz-lower"
28
Richard Sandifordf722a8e302013-10-16 11:10:55 +000029namespace {
30// Represents a sequence for extracting a 0/1 value from an IPM result:
31// (((X ^ XORValue) + AddValue) >> Bit)
32struct IPMConversion {
33 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
34 : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
35
36 int64_t XORValue;
37 int64_t AddValue;
38 unsigned Bit;
39};
Richard Sandifordd420f732013-12-13 15:28:45 +000040
41// Represents information about a comparison.
42struct Comparison {
43 Comparison(SDValue Op0In, SDValue Op1In)
44 : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {}
45
46 // The operands to the comparison.
47 SDValue Op0, Op1;
48
49 // The opcode that should be used to compare Op0 and Op1.
50 unsigned Opcode;
51
52 // A SystemZICMP value. Only used for integer comparisons.
53 unsigned ICmpType;
54
55 // The mask of CC values that Opcode can produce.
56 unsigned CCValid;
57
58 // The mask of CC values for which the original condition is true.
59 unsigned CCMask;
60};
Richard Sandifordc2312692014-03-06 10:38:30 +000061} // end anonymous namespace
Richard Sandifordf722a8e302013-10-16 11:10:55 +000062
Ulrich Weigand5f613df2013-05-06 16:15:19 +000063// Classify VT as either 32 or 64 bit.
64static bool is32Bit(EVT VT) {
65 switch (VT.getSimpleVT().SimpleTy) {
66 case MVT::i32:
67 return true;
68 case MVT::i64:
69 return false;
70 default:
71 llvm_unreachable("Unsupported type");
72 }
73}
74
75// Return a version of MachineOperand that can be safely used before the
76// final use.
77static MachineOperand earlyUseOperand(MachineOperand Op) {
78 if (Op.isReg())
79 Op.setIsKill(false);
80 return Op;
81}
82
Eric Christophera6734172015-01-31 00:06:45 +000083SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &tm,
84 const SystemZSubtarget &STI)
85 : TargetLowering(tm), Subtarget(STI) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000086 MVT PtrVT = getPointerTy();
87
88 // Set up the register classes.
Richard Sandiford0755c932013-10-01 11:26:28 +000089 if (Subtarget.hasHighWord())
90 addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass);
91 else
92 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
Ulrich Weigand5f613df2013-05-06 16:15:19 +000093 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
94 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
95 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
96 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
97
98 // Compute derived properties from the register classes
Eric Christopher23a3a7c2015-02-26 00:00:24 +000099 computeRegisterProperties(Subtarget.getRegisterInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000100
101 // Set up special registers.
102 setExceptionPointerRegister(SystemZ::R6D);
103 setExceptionSelectorRegister(SystemZ::R7D);
104 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
105
106 // TODO: It may be better to default to latency-oriented scheduling, however
107 // LLVM's current latency-oriented scheduler can't handle physreg definitions
Richard Sandiford14a44492013-05-22 13:38:45 +0000108 // such as SystemZ has with CC, so set this to the register-pressure
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000109 // scheduler, because it can.
110 setSchedulingPreference(Sched::RegPressure);
111
112 setBooleanContents(ZeroOrOneBooleanContent);
113 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
114
115 // Instructions are strings of 2-byte aligned 2-byte values.
116 setMinFunctionAlignment(2);
117
118 // Handle operations that are handled in a similar way for all types.
119 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
120 I <= MVT::LAST_FP_VALUETYPE;
121 ++I) {
122 MVT VT = MVT::SimpleValueType(I);
123 if (isTypeLegal(VT)) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000124 // Lower SET_CC into an IPM-based sequence.
125 setOperationAction(ISD::SETCC, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000126
127 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
128 setOperationAction(ISD::SELECT, VT, Expand);
129
130 // Lower SELECT_CC and BR_CC into separate comparisons and branches.
131 setOperationAction(ISD::SELECT_CC, VT, Custom);
132 setOperationAction(ISD::BR_CC, VT, Custom);
133 }
134 }
135
136 // Expand jump table branches as address arithmetic followed by an
137 // indirect jump.
138 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
139
140 // Expand BRCOND into a BR_CC (see above).
141 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
142
143 // Handle integer types.
144 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
145 I <= MVT::LAST_INTEGER_VALUETYPE;
146 ++I) {
147 MVT VT = MVT::SimpleValueType(I);
148 if (isTypeLegal(VT)) {
149 // Expand individual DIV and REMs into DIVREMs.
150 setOperationAction(ISD::SDIV, VT, Expand);
151 setOperationAction(ISD::UDIV, VT, Expand);
152 setOperationAction(ISD::SREM, VT, Expand);
153 setOperationAction(ISD::UREM, VT, Expand);
154 setOperationAction(ISD::SDIVREM, VT, Custom);
155 setOperationAction(ISD::UDIVREM, VT, Custom);
156
Richard Sandifordbef3d7a2013-12-10 10:49:34 +0000157 // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and
158 // stores, putting a serialization instruction after the stores.
159 setOperationAction(ISD::ATOMIC_LOAD, VT, Custom);
160 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000161
Richard Sandiford41350a52013-12-24 15:18:04 +0000162 // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are
163 // available, or if the operand is constant.
164 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
165
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000166 // No special instructions for these.
167 setOperationAction(ISD::CTPOP, VT, Expand);
168 setOperationAction(ISD::CTTZ, VT, Expand);
169 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
170 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
171 setOperationAction(ISD::ROTR, VT, Expand);
172
Richard Sandiford7d86e472013-08-21 09:34:56 +0000173 // Use *MUL_LOHI where possible instead of MULH*.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000174 setOperationAction(ISD::MULHS, VT, Expand);
175 setOperationAction(ISD::MULHU, VT, Expand);
Richard Sandiford7d86e472013-08-21 09:34:56 +0000176 setOperationAction(ISD::SMUL_LOHI, VT, Custom);
177 setOperationAction(ISD::UMUL_LOHI, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000178
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000179 // Only z196 and above have native support for conversions to unsigned.
180 if (!Subtarget.hasFPExtension())
181 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000182 }
183 }
184
185 // Type legalization will convert 8- and 16-bit atomic operations into
186 // forms that operate on i32s (but still keeping the original memory VT).
187 // Lower them into full i32 operations.
188 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom);
189 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom);
190 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
191 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom);
192 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom);
193 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom);
194 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
195 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom);
196 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom);
197 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
198 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
199 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
200
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000201 // z10 has instructions for signed but not unsigned FP conversion.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000202 // Handle unsigned 32-bit types as signed 64-bit types.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000203 if (!Subtarget.hasFPExtension()) {
204 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
205 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
206 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000207
208 // We have native support for a 64-bit CTLZ, via FLOGR.
209 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
210 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
211
212 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
213 setOperationAction(ISD::OR, MVT::i64, Custom);
214
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000215 // FIXME: Can we support these natively?
216 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
217 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
218 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
219
220 // We have native instructions for i8, i16 and i32 extensions, but not i1.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000221 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000222 for (MVT VT : MVT::integer_valuetypes()) {
223 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
224 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
225 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
226 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000227
228 // Handle the various types of symbolic address.
229 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
230 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
231 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
232 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
233 setOperationAction(ISD::JumpTable, PtrVT, Custom);
234
235 // We need to handle dynamic allocations specially because of the
236 // 160-byte area at the bottom of the stack.
237 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
238
239 // Use custom expanders so that we can force the function to use
240 // a frame pointer.
241 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
242 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
243
Richard Sandiford03481332013-08-23 11:36:42 +0000244 // Handle prefetches with PFD or PFDRL.
245 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
246
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000247 // Handle floating-point types.
248 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
249 I <= MVT::LAST_FP_VALUETYPE;
250 ++I) {
251 MVT VT = MVT::SimpleValueType(I);
252 if (isTypeLegal(VT)) {
253 // We can use FI for FRINT.
254 setOperationAction(ISD::FRINT, VT, Legal);
255
Richard Sandifordaf5f66a2013-08-21 09:04:20 +0000256 // We can use the extended form of FI for other rounding operations.
257 if (Subtarget.hasFPExtension()) {
258 setOperationAction(ISD::FNEARBYINT, VT, Legal);
259 setOperationAction(ISD::FFLOOR, VT, Legal);
260 setOperationAction(ISD::FCEIL, VT, Legal);
261 setOperationAction(ISD::FTRUNC, VT, Legal);
262 setOperationAction(ISD::FROUND, VT, Legal);
263 }
264
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000265 // No special instructions for these.
266 setOperationAction(ISD::FSIN, VT, Expand);
267 setOperationAction(ISD::FCOS, VT, Expand);
268 setOperationAction(ISD::FREM, VT, Expand);
269 }
270 }
271
272 // We have fused multiply-addition for f32 and f64 but not f128.
273 setOperationAction(ISD::FMA, MVT::f32, Legal);
274 setOperationAction(ISD::FMA, MVT::f64, Legal);
275 setOperationAction(ISD::FMA, MVT::f128, Expand);
276
277 // Needed so that we don't try to implement f128 constant loads using
278 // a load-and-extend of a f80 constant (in cases where the constant
279 // would fit in an f80).
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000280 for (MVT VT : MVT::fp_valuetypes())
281 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000282
283 // Floating-point truncation and stores need to be done separately.
284 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
285 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
286 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
287
288 // We have 64-bit FPR<->GPR moves, but need special handling for
289 // 32-bit forms.
290 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
291 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
292
293 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
294 // structure, but VAEND is a no-op.
295 setOperationAction(ISD::VASTART, MVT::Other, Custom);
296 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
297 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Richard Sandifordd131ff82013-07-08 09:35:23 +0000298
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000299 // Codes for which we want to perform some z-specific combinations.
300 setTargetDAGCombine(ISD::SIGN_EXTEND);
301
Richard Sandifordd131ff82013-07-08 09:35:23 +0000302 // We want to use MVC in preference to even a single load/store pair.
303 MaxStoresPerMemcpy = 0;
304 MaxStoresPerMemcpyOptSize = 0;
Richard Sandiford47660c12013-07-09 09:32:42 +0000305
306 // The main memset sequence is a byte store followed by an MVC.
307 // Two STC or MV..I stores win over that, but the kind of fused stores
308 // generated by target-independent code don't when the byte value is
309 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
310 // than "STC;MVC". Handle the choice in target-specific code instead.
311 MaxStoresPerMemset = 0;
312 MaxStoresPerMemsetOptSize = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000313}
314
Richard Sandifordabc010b2013-11-06 12:16:02 +0000315EVT SystemZTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
316 if (!VT.isVector())
317 return MVT::i32;
318 return VT.changeVectorElementTypeToInteger();
319}
320
321bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
Stephen Lin73de7bf2013-07-09 18:16:56 +0000322 VT = VT.getScalarType();
323
324 if (!VT.isSimple())
325 return false;
326
327 switch (VT.getSimpleVT().SimpleTy) {
328 case MVT::f32:
329 case MVT::f64:
330 return true;
331 case MVT::f128:
332 return false;
333 default:
334 break;
335 }
336
337 return false;
338}
339
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000340bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
341 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
342 return Imm.isZero() || Imm.isNegZero();
343}
344
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000345bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
346 unsigned,
347 unsigned,
348 bool *Fast) const {
Richard Sandiford46af5a22013-05-30 09:45:42 +0000349 // Unaligned accesses should never be slower than the expanded version.
350 // We check specifically for aligned accesses in the few cases where
351 // they are required.
352 if (Fast)
353 *Fast = true;
354 return true;
355}
356
Richard Sandiford791bea42013-07-31 12:58:26 +0000357bool SystemZTargetLowering::isLegalAddressingMode(const AddrMode &AM,
358 Type *Ty) const {
359 // Punt on globals for now, although they can be used in limited
360 // RELATIVE LONG cases.
361 if (AM.BaseGV)
362 return false;
363
364 // Require a 20-bit signed offset.
365 if (!isInt<20>(AM.BaseOffs))
366 return false;
367
368 // Indexing is OK but no scale factor can be applied.
369 return AM.Scale == 0 || AM.Scale == 1;
370}
371
Richard Sandiford709bda62013-08-19 12:42:31 +0000372bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
373 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
374 return false;
375 unsigned FromBits = FromType->getPrimitiveSizeInBits();
376 unsigned ToBits = ToType->getPrimitiveSizeInBits();
377 return FromBits > ToBits;
378}
379
380bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
381 if (!FromVT.isInteger() || !ToVT.isInteger())
382 return false;
383 unsigned FromBits = FromVT.getSizeInBits();
384 unsigned ToBits = ToVT.getSizeInBits();
385 return FromBits > ToBits;
386}
387
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000388//===----------------------------------------------------------------------===//
389// Inline asm support
390//===----------------------------------------------------------------------===//
391
392TargetLowering::ConstraintType
393SystemZTargetLowering::getConstraintType(const std::string &Constraint) const {
394 if (Constraint.size() == 1) {
395 switch (Constraint[0]) {
396 case 'a': // Address register
397 case 'd': // Data register (equivalent to 'r')
398 case 'f': // Floating-point register
Richard Sandiford0755c932013-10-01 11:26:28 +0000399 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000400 case 'r': // General-purpose register
401 return C_RegisterClass;
402
403 case 'Q': // Memory with base and unsigned 12-bit displacement
404 case 'R': // Likewise, plus an index
405 case 'S': // Memory with base and signed 20-bit displacement
406 case 'T': // Likewise, plus an index
407 case 'm': // Equivalent to 'T'.
408 return C_Memory;
409
410 case 'I': // Unsigned 8-bit constant
411 case 'J': // Unsigned 12-bit constant
412 case 'K': // Signed 16-bit constant
413 case 'L': // Signed 20-bit displacement (on all targets we support)
414 case 'M': // 0x7fffffff
415 return C_Other;
416
417 default:
418 break;
419 }
420 }
421 return TargetLowering::getConstraintType(Constraint);
422}
423
424TargetLowering::ConstraintWeight SystemZTargetLowering::
425getSingleConstraintMatchWeight(AsmOperandInfo &info,
426 const char *constraint) const {
427 ConstraintWeight weight = CW_Invalid;
428 Value *CallOperandVal = info.CallOperandVal;
429 // If we don't have a value, we can't do a match,
430 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +0000431 if (!CallOperandVal)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000432 return CW_Default;
433 Type *type = CallOperandVal->getType();
434 // Look at the constraint type.
435 switch (*constraint) {
436 default:
437 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
438 break;
439
440 case 'a': // Address register
441 case 'd': // Data register (equivalent to 'r')
Richard Sandiford0755c932013-10-01 11:26:28 +0000442 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000443 case 'r': // General-purpose register
444 if (CallOperandVal->getType()->isIntegerTy())
445 weight = CW_Register;
446 break;
447
448 case 'f': // Floating-point register
449 if (type->isFloatingPointTy())
450 weight = CW_Register;
451 break;
452
453 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000454 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000455 if (isUInt<8>(C->getZExtValue()))
456 weight = CW_Constant;
457 break;
458
459 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000460 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000461 if (isUInt<12>(C->getZExtValue()))
462 weight = CW_Constant;
463 break;
464
465 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000466 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000467 if (isInt<16>(C->getSExtValue()))
468 weight = CW_Constant;
469 break;
470
471 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000472 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000473 if (isInt<20>(C->getSExtValue()))
474 weight = CW_Constant;
475 break;
476
477 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000478 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000479 if (C->getZExtValue() == 0x7fffffff)
480 weight = CW_Constant;
481 break;
482 }
483 return weight;
484}
485
Richard Sandifordb8204052013-07-12 09:08:12 +0000486// Parse a "{tNNN}" register constraint for which the register type "t"
487// has already been verified. MC is the class associated with "t" and
488// Map maps 0-based register numbers to LLVM register numbers.
489static std::pair<unsigned, const TargetRegisterClass *>
490parseRegisterNumber(const std::string &Constraint,
491 const TargetRegisterClass *RC, const unsigned *Map) {
492 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
493 if (isdigit(Constraint[2])) {
494 std::string Suffix(Constraint.data() + 2, Constraint.size() - 2);
495 unsigned Index = atoi(Suffix.c_str());
496 if (Index < 16 && Map[Index])
497 return std::make_pair(Map[Index], RC);
498 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000499 return std::make_pair(0U, nullptr);
Richard Sandifordb8204052013-07-12 09:08:12 +0000500}
501
Eric Christopher11e4df72015-02-26 22:38:43 +0000502std::pair<unsigned, const TargetRegisterClass *>
503SystemZTargetLowering::getRegForInlineAsmConstraint(
504 const TargetRegisterInfo *TRI, const std::string &Constraint,
505 MVT VT) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000506 if (Constraint.size() == 1) {
507 // GCC Constraint Letters
508 switch (Constraint[0]) {
509 default: break;
510 case 'd': // Data register (equivalent to 'r')
511 case 'r': // General-purpose register
512 if (VT == MVT::i64)
513 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
514 else if (VT == MVT::i128)
515 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
516 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
517
518 case 'a': // Address register
519 if (VT == MVT::i64)
520 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
521 else if (VT == MVT::i128)
522 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
523 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
524
Richard Sandiford0755c932013-10-01 11:26:28 +0000525 case 'h': // High-part register (an LLVM extension)
526 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
527
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000528 case 'f': // Floating-point register
529 if (VT == MVT::f64)
530 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
531 else if (VT == MVT::f128)
532 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
533 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
534 }
535 }
Richard Sandifordb8204052013-07-12 09:08:12 +0000536 if (Constraint[0] == '{') {
537 // We need to override the default register parsing for GPRs and FPRs
538 // because the interpretation depends on VT. The internal names of
539 // the registers are also different from the external names
540 // (F0D and F0S instead of F0, etc.).
541 if (Constraint[1] == 'r') {
542 if (VT == MVT::i32)
543 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
544 SystemZMC::GR32Regs);
545 if (VT == MVT::i128)
546 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
547 SystemZMC::GR128Regs);
548 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
549 SystemZMC::GR64Regs);
550 }
551 if (Constraint[1] == 'f') {
552 if (VT == MVT::f32)
553 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
554 SystemZMC::FP32Regs);
555 if (VT == MVT::f128)
556 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
557 SystemZMC::FP128Regs);
558 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
559 SystemZMC::FP64Regs);
560 }
561 }
Eric Christopher11e4df72015-02-26 22:38:43 +0000562 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000563}
564
565void SystemZTargetLowering::
566LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
567 std::vector<SDValue> &Ops,
568 SelectionDAG &DAG) const {
569 // Only support length 1 constraints for now.
570 if (Constraint.length() == 1) {
571 switch (Constraint[0]) {
572 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000573 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000574 if (isUInt<8>(C->getZExtValue()))
575 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
576 Op.getValueType()));
577 return;
578
579 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000580 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000581 if (isUInt<12>(C->getZExtValue()))
582 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
583 Op.getValueType()));
584 return;
585
586 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000587 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000588 if (isInt<16>(C->getSExtValue()))
589 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
590 Op.getValueType()));
591 return;
592
593 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000594 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000595 if (isInt<20>(C->getSExtValue()))
596 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
597 Op.getValueType()));
598 return;
599
600 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000601 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000602 if (C->getZExtValue() == 0x7fffffff)
603 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
604 Op.getValueType()));
605 return;
606 }
607 }
608 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
609}
610
611//===----------------------------------------------------------------------===//
612// Calling conventions
613//===----------------------------------------------------------------------===//
614
615#include "SystemZGenCallingConv.inc"
616
Richard Sandiford709bda62013-08-19 12:42:31 +0000617bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
618 Type *ToType) const {
619 return isTruncateFree(FromType, ToType);
620}
621
622bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
623 if (!CI->isTailCall())
624 return false;
625 return true;
626}
627
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000628// Value is a value that has been passed to us in the location described by VA
629// (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
630// any loads onto Chain.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000631static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000632 CCValAssign &VA, SDValue Chain,
633 SDValue Value) {
634 // If the argument has been promoted from a smaller type, insert an
635 // assertion to capture this.
636 if (VA.getLocInfo() == CCValAssign::SExt)
637 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
638 DAG.getValueType(VA.getValVT()));
639 else if (VA.getLocInfo() == CCValAssign::ZExt)
640 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
641 DAG.getValueType(VA.getValVT()));
642
643 if (VA.isExtInLoc())
644 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
645 else if (VA.getLocInfo() == CCValAssign::Indirect)
646 Value = DAG.getLoad(VA.getValVT(), DL, Chain, Value,
647 MachinePointerInfo(), false, false, false, 0);
648 else
649 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
650 return Value;
651}
652
653// Value is a value of type VA.getValVT() that we need to copy into
654// the location described by VA. Return a copy of Value converted to
655// VA.getValVT(). The caller is responsible for handling indirect values.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000656static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000657 CCValAssign &VA, SDValue Value) {
658 switch (VA.getLocInfo()) {
659 case CCValAssign::SExt:
660 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
661 case CCValAssign::ZExt:
662 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
663 case CCValAssign::AExt:
664 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
665 case CCValAssign::Full:
666 return Value;
667 default:
668 llvm_unreachable("Unhandled getLocInfo()");
669 }
670}
671
672SDValue SystemZTargetLowering::
673LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
674 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000675 SDLoc DL, SelectionDAG &DAG,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000676 SmallVectorImpl<SDValue> &InVals) const {
677 MachineFunction &MF = DAG.getMachineFunction();
678 MachineFrameInfo *MFI = MF.getFrameInfo();
679 MachineRegisterInfo &MRI = MF.getRegInfo();
680 SystemZMachineFunctionInfo *FuncInfo =
Eric Christophera6734172015-01-31 00:06:45 +0000681 MF.getInfo<SystemZMachineFunctionInfo>();
682 auto *TFL =
683 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000684
685 // Assign locations to all of the incoming arguments.
686 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000687 CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000688 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
689
690 unsigned NumFixedGPRs = 0;
691 unsigned NumFixedFPRs = 0;
692 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
693 SDValue ArgValue;
694 CCValAssign &VA = ArgLocs[I];
695 EVT LocVT = VA.getLocVT();
696 if (VA.isRegLoc()) {
697 // Arguments passed in registers
698 const TargetRegisterClass *RC;
699 switch (LocVT.getSimpleVT().SimpleTy) {
700 default:
701 // Integers smaller than i64 should be promoted to i64.
702 llvm_unreachable("Unexpected argument type");
703 case MVT::i32:
704 NumFixedGPRs += 1;
705 RC = &SystemZ::GR32BitRegClass;
706 break;
707 case MVT::i64:
708 NumFixedGPRs += 1;
709 RC = &SystemZ::GR64BitRegClass;
710 break;
711 case MVT::f32:
712 NumFixedFPRs += 1;
713 RC = &SystemZ::FP32BitRegClass;
714 break;
715 case MVT::f64:
716 NumFixedFPRs += 1;
717 RC = &SystemZ::FP64BitRegClass;
718 break;
719 }
720
721 unsigned VReg = MRI.createVirtualRegister(RC);
722 MRI.addLiveIn(VA.getLocReg(), VReg);
723 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
724 } else {
725 assert(VA.isMemLoc() && "Argument not register or memory");
726
727 // Create the frame index object for this incoming parameter.
728 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
729 VA.getLocMemOffset(), true);
730
731 // Create the SelectionDAG nodes corresponding to a load
732 // from this parameter. Unpromoted ints and floats are
733 // passed as right-justified 8-byte values.
734 EVT PtrVT = getPointerTy();
735 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
736 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
737 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, DAG.getIntPtrConstant(4));
738 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
739 MachinePointerInfo::getFixedStack(FI),
740 false, false, false, 0);
741 }
742
743 // Convert the value of the argument register into the value that's
744 // being passed.
745 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
746 }
747
748 if (IsVarArg) {
749 // Save the number of non-varargs registers for later use by va_start, etc.
750 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
751 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
752
753 // Likewise the address (in the form of a frame index) of where the
754 // first stack vararg would be. The 1-byte size here is arbitrary.
755 int64_t StackSize = CCInfo.getNextStackOffset();
756 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true));
757
758 // ...and a similar frame index for the caller-allocated save area
759 // that will be used to store the incoming registers.
760 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
761 unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true);
762 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
763
764 // Store the FPR varargs in the reserved frame slots. (We store the
765 // GPRs as part of the prologue.)
766 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
767 SDValue MemOps[SystemZ::NumArgFPRs];
768 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
769 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
770 int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true);
771 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
772 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
773 &SystemZ::FP64BitRegClass);
774 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
775 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
776 MachinePointerInfo::getFixedStack(FI),
777 false, false, 0);
778
779 }
780 // Join the stores, which are independent of one another.
781 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Craig Topper2d2aa0c2014-04-30 07:17:30 +0000782 makeArrayRef(&MemOps[NumFixedFPRs],
783 SystemZ::NumArgFPRs-NumFixedFPRs));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000784 }
785 }
786
787 return Chain;
788}
789
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000790static bool canUseSiblingCall(const CCState &ArgCCInfo,
Richard Sandiford709bda62013-08-19 12:42:31 +0000791 SmallVectorImpl<CCValAssign> &ArgLocs) {
792 // Punt if there are any indirect or stack arguments, or if the call
793 // needs the call-saved argument register R6.
794 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
795 CCValAssign &VA = ArgLocs[I];
796 if (VA.getLocInfo() == CCValAssign::Indirect)
797 return false;
798 if (!VA.isRegLoc())
799 return false;
800 unsigned Reg = VA.getLocReg();
Richard Sandiford0755c932013-10-01 11:26:28 +0000801 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
Richard Sandiford709bda62013-08-19 12:42:31 +0000802 return false;
803 }
804 return true;
805}
806
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000807SDValue
808SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
809 SmallVectorImpl<SDValue> &InVals) const {
810 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000811 SDLoc &DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000812 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
813 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
814 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000815 SDValue Chain = CLI.Chain;
816 SDValue Callee = CLI.Callee;
Richard Sandiford709bda62013-08-19 12:42:31 +0000817 bool &IsTailCall = CLI.IsTailCall;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000818 CallingConv::ID CallConv = CLI.CallConv;
819 bool IsVarArg = CLI.IsVarArg;
820 MachineFunction &MF = DAG.getMachineFunction();
821 EVT PtrVT = getPointerTy();
822
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000823 // Analyze the operands of the call, assigning locations to each operand.
824 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000825 CCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000826 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
827
Richard Sandiford709bda62013-08-19 12:42:31 +0000828 // We don't support GuaranteedTailCallOpt, only automatically-detected
829 // sibling calls.
830 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs))
831 IsTailCall = false;
832
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000833 // Get a count of how many bytes are to be pushed on the stack.
834 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
835
836 // Mark the start of the call.
Richard Sandiford709bda62013-08-19 12:42:31 +0000837 if (!IsTailCall)
838 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumBytes, PtrVT, true),
839 DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000840
841 // Copy argument values to their designated locations.
842 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
843 SmallVector<SDValue, 8> MemOpChains;
844 SDValue StackPtr;
845 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
846 CCValAssign &VA = ArgLocs[I];
847 SDValue ArgValue = OutVals[I];
848
849 if (VA.getLocInfo() == CCValAssign::Indirect) {
850 // Store the argument in a stack slot and pass its address.
851 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
852 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
853 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, SpillSlot,
854 MachinePointerInfo::getFixedStack(FI),
855 false, false, 0));
856 ArgValue = SpillSlot;
857 } else
858 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
859
860 if (VA.isRegLoc())
861 // Queue up the argument copies and emit them at the end.
862 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
863 else {
864 assert(VA.isMemLoc() && "Argument not register or memory");
865
866 // Work out the address of the stack slot. Unpromoted ints and
867 // floats are passed as right-justified 8-byte values.
868 if (!StackPtr.getNode())
869 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
870 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
871 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
872 Offset += 4;
873 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
874 DAG.getIntPtrConstant(Offset));
875
876 // Emit the store.
877 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address,
878 MachinePointerInfo(),
879 false, false, 0));
880 }
881 }
882
883 // Join the stores, which are independent of one another.
884 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000885 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000886
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000887 // Accept direct calls by converting symbolic call addresses to the
Richard Sandiford709bda62013-08-19 12:42:31 +0000888 // associated Target* opcodes. Force %r1 to be used for indirect
889 // tail calls.
890 SDValue Glue;
Richard Sandiford21f5d682014-03-06 11:22:58 +0000891 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000892 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
893 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford21f5d682014-03-06 11:22:58 +0000894 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000895 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
896 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford709bda62013-08-19 12:42:31 +0000897 } else if (IsTailCall) {
898 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
899 Glue = Chain.getValue(1);
900 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
901 }
902
903 // Build a sequence of copy-to-reg nodes, chained and glued together.
904 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
905 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
906 RegsToPass[I].second, Glue);
907 Glue = Chain.getValue(1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000908 }
909
910 // The first call operand is the chain and the second is the target address.
911 SmallVector<SDValue, 8> Ops;
912 Ops.push_back(Chain);
913 Ops.push_back(Callee);
914
915 // Add argument registers to the end of the list so that they are
916 // known live into the call.
917 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
918 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
919 RegsToPass[I].second.getValueType()));
920
Richard Sandiford02bb0ec2014-07-10 11:44:37 +0000921 // Add a register mask operand representing the call-preserved registers.
Eric Christophera6734172015-01-31 00:06:45 +0000922 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +0000923 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
Richard Sandiford02bb0ec2014-07-10 11:44:37 +0000924 assert(Mask && "Missing call preserved mask for calling convention");
925 Ops.push_back(DAG.getRegisterMask(Mask));
926
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000927 // Glue the call to the argument copies, if any.
928 if (Glue.getNode())
929 Ops.push_back(Glue);
930
931 // Emit the call.
932 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Richard Sandiford709bda62013-08-19 12:42:31 +0000933 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +0000934 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
935 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000936 Glue = Chain.getValue(1);
937
938 // Mark the end of the call, which is glued to the call itself.
939 Chain = DAG.getCALLSEQ_END(Chain,
940 DAG.getConstant(NumBytes, PtrVT, true),
941 DAG.getConstant(0, PtrVT, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +0000942 Glue, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000943 Glue = Chain.getValue(1);
944
945 // Assign locations to each value returned by this call.
946 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000947 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000948 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
949
950 // Copy all of the result registers out of their specified physreg.
951 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
952 CCValAssign &VA = RetLocs[I];
953
954 // Copy the value out, gluing the copy to the end of the call sequence.
955 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
956 VA.getLocVT(), Glue);
957 Chain = RetValue.getValue(1);
958 Glue = RetValue.getValue(2);
959
960 // Convert the value of the return register into the value that's
961 // being returned.
962 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
963 }
964
965 return Chain;
966}
967
968SDValue
969SystemZTargetLowering::LowerReturn(SDValue Chain,
970 CallingConv::ID CallConv, bool IsVarArg,
971 const SmallVectorImpl<ISD::OutputArg> &Outs,
972 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000973 SDLoc DL, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000974 MachineFunction &MF = DAG.getMachineFunction();
975
976 // Assign locations to each returned value.
977 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000978 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000979 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
980
981 // Quick exit for void returns
982 if (RetLocs.empty())
983 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
984
985 // Copy the result values into the output registers.
986 SDValue Glue;
987 SmallVector<SDValue, 4> RetOps;
988 RetOps.push_back(Chain);
989 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
990 CCValAssign &VA = RetLocs[I];
991 SDValue RetValue = OutVals[I];
992
993 // Make the return register live on exit.
994 assert(VA.isRegLoc() && "Can only return in registers!");
995
996 // Promote the value as required.
997 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
998
999 // Chain and glue the copies together.
1000 unsigned Reg = VA.getLocReg();
1001 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
1002 Glue = Chain.getValue(1);
1003 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
1004 }
1005
1006 // Update chain and glue.
1007 RetOps[0] = Chain;
1008 if (Glue.getNode())
1009 RetOps.push_back(Glue);
1010
Craig Topper48d114b2014-04-26 18:35:24 +00001011 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001012}
1013
Richard Sandiford9afe6132013-12-10 10:36:34 +00001014SDValue SystemZTargetLowering::
1015prepareVolatileOrAtomicLoad(SDValue Chain, SDLoc DL, SelectionDAG &DAG) const {
1016 return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain);
1017}
1018
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001019// CC is a comparison that will be implemented using an integer or
1020// floating-point comparison. Return the condition code mask for
1021// a branch on true. In the integer case, CCMASK_CMP_UO is set for
1022// unsigned comparisons and clear for signed ones. In the floating-point
1023// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1024static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1025#define CONV(X) \
1026 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1027 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1028 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1029
1030 switch (CC) {
1031 default:
1032 llvm_unreachable("Invalid integer condition!");
1033
1034 CONV(EQ);
1035 CONV(NE);
1036 CONV(GT);
1037 CONV(GE);
1038 CONV(LT);
1039 CONV(LE);
1040
1041 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1042 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1043 }
1044#undef CONV
1045}
1046
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001047// Return a sequence for getting a 1 from an IPM result when CC has a
1048// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1049// The handling of CC values outside CCValid doesn't matter.
1050static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1051 // Deal with cases where the result can be taken directly from a bit
1052 // of the IPM result.
1053 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1054 return IPMConversion(0, 0, SystemZ::IPM_CC);
1055 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1056 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1057
1058 // Deal with cases where we can add a value to force the sign bit
1059 // to contain the right value. Putting the bit in 31 means we can
1060 // use SRL rather than RISBG(L), and also makes it easier to get a
1061 // 0/-1 value, so it has priority over the other tests below.
1062 //
1063 // These sequences rely on the fact that the upper two bits of the
1064 // IPM result are zero.
1065 uint64_t TopBit = uint64_t(1) << 31;
1066 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1067 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1068 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1069 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1070 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1071 | SystemZ::CCMASK_1
1072 | SystemZ::CCMASK_2)))
1073 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1074 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1075 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1076 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1077 | SystemZ::CCMASK_2
1078 | SystemZ::CCMASK_3)))
1079 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1080
1081 // Next try inverting the value and testing a bit. 0/1 could be
1082 // handled this way too, but we dealt with that case above.
1083 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1084 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1085
1086 // Handle cases where adding a value forces a non-sign bit to contain
1087 // the right value.
1088 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1089 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1090 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1091 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1092
Alp Tokercb402912014-01-24 17:20:08 +00001093 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001094 // can be done by inverting the low CC bit and applying one of the
1095 // sign-based extractions above.
1096 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1097 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1098 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1099 return IPMConversion(1 << SystemZ::IPM_CC,
1100 TopBit - (3 << SystemZ::IPM_CC), 31);
1101 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1102 | SystemZ::CCMASK_1
1103 | SystemZ::CCMASK_3)))
1104 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1105 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1106 | SystemZ::CCMASK_2
1107 | SystemZ::CCMASK_3)))
1108 return IPMConversion(1 << SystemZ::IPM_CC,
1109 TopBit - (1 << SystemZ::IPM_CC), 31);
1110
1111 llvm_unreachable("Unexpected CC combination");
1112}
1113
Richard Sandifordd420f732013-12-13 15:28:45 +00001114// If C can be converted to a comparison against zero, adjust the operands
Richard Sandiforda0757082013-08-01 10:29:45 +00001115// as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001116static void adjustZeroCmp(SelectionDAG &DAG, Comparison &C) {
1117 if (C.ICmpType == SystemZICMP::UnsignedOnly)
Richard Sandiforda0757082013-08-01 10:29:45 +00001118 return;
1119
Richard Sandiford21f5d682014-03-06 11:22:58 +00001120 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
Richard Sandiforda0757082013-08-01 10:29:45 +00001121 if (!ConstOp1)
1122 return;
1123
1124 int64_t Value = ConstOp1->getSExtValue();
Richard Sandifordd420f732013-12-13 15:28:45 +00001125 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1126 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1127 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1128 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1129 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1130 C.Op1 = DAG.getConstant(0, C.Op1.getValueType());
Richard Sandiforda0757082013-08-01 10:29:45 +00001131 }
1132}
1133
Richard Sandifordd420f732013-12-13 15:28:45 +00001134// If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1135// adjust the operands as necessary.
1136static void adjustSubwordCmp(SelectionDAG &DAG, Comparison &C) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001137 // For us to make any changes, it must a comparison between a single-use
1138 // load and a constant.
Richard Sandifordd420f732013-12-13 15:28:45 +00001139 if (!C.Op0.hasOneUse() ||
1140 C.Op0.getOpcode() != ISD::LOAD ||
1141 C.Op1.getOpcode() != ISD::Constant)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001142 return;
1143
1144 // We must have an 8- or 16-bit load.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001145 auto *Load = cast<LoadSDNode>(C.Op0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001146 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1147 if (NumBits != 8 && NumBits != 16)
1148 return;
1149
1150 // The load must be an extending one and the constant must be within the
1151 // range of the unextended value.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001152 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001153 uint64_t Value = ConstOp1->getZExtValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001154 uint64_t Mask = (1 << NumBits) - 1;
1155 if (Load->getExtensionType() == ISD::SEXTLOAD) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001156 // Make sure that ConstOp1 is in range of C.Op0.
1157 int64_t SignedValue = ConstOp1->getSExtValue();
1158 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001159 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001160 if (C.ICmpType != SystemZICMP::SignedOnly) {
1161 // Unsigned comparison between two sign-extended values is equivalent
1162 // to unsigned comparison between two zero-extended values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001163 Value &= Mask;
Richard Sandifordd420f732013-12-13 15:28:45 +00001164 } else if (NumBits == 8) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001165 // Try to treat the comparison as unsigned, so that we can use CLI.
1166 // Adjust CCMask and Value as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001167 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001168 // Test whether the high bit of the byte is set.
Richard Sandifordd420f732013-12-13 15:28:45 +00001169 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1170 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001171 // Test whether the high bit of the byte is clear.
Richard Sandifordd420f732013-12-13 15:28:45 +00001172 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001173 else
1174 // No instruction exists for this combination.
1175 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001176 C.ICmpType = SystemZICMP::UnsignedOnly;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001177 }
1178 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1179 if (Value > Mask)
1180 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001181 assert(C.ICmpType == SystemZICMP::Any &&
1182 "Signedness shouldn't matter here.");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001183 } else
1184 return;
1185
1186 // Make sure that the first operand is an i32 of the right extension type.
Richard Sandifordd420f732013-12-13 15:28:45 +00001187 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1188 ISD::SEXTLOAD :
1189 ISD::ZEXTLOAD);
1190 if (C.Op0.getValueType() != MVT::i32 ||
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001191 Load->getExtensionType() != ExtType)
Richard Sandifordd420f732013-12-13 15:28:45 +00001192 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32,
1193 Load->getChain(), Load->getBasePtr(),
1194 Load->getPointerInfo(), Load->getMemoryVT(),
1195 Load->isVolatile(), Load->isNonTemporal(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00001196 Load->isInvariant(), Load->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001197
1198 // Make sure that the second operand is an i32 with the right value.
Richard Sandifordd420f732013-12-13 15:28:45 +00001199 if (C.Op1.getValueType() != MVT::i32 ||
1200 Value != ConstOp1->getZExtValue())
1201 C.Op1 = DAG.getConstant(Value, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001202}
1203
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001204// Return true if Op is either an unextended load, or a load suitable
1205// for integer register-memory comparisons of type ICmpType.
1206static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001207 auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001208 if (Load) {
1209 // There are no instructions to compare a register with a memory byte.
1210 if (Load->getMemoryVT() == MVT::i8)
1211 return false;
1212 // Otherwise decide on extension type.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001213 switch (Load->getExtensionType()) {
1214 case ISD::NON_EXTLOAD:
Richard Sandiford24e597b2013-08-23 11:27:19 +00001215 return true;
1216 case ISD::SEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001217 return ICmpType != SystemZICMP::UnsignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001218 case ISD::ZEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001219 return ICmpType != SystemZICMP::SignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001220 default:
1221 break;
1222 }
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001223 }
Richard Sandiford24e597b2013-08-23 11:27:19 +00001224 return false;
1225}
1226
Richard Sandifordd420f732013-12-13 15:28:45 +00001227// Return true if it is better to swap the operands of C.
1228static bool shouldSwapCmpOperands(const Comparison &C) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001229 // Leave f128 comparisons alone, since they have no memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001230 if (C.Op0.getValueType() == MVT::f128)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001231 return false;
1232
1233 // Always keep a floating-point constant second, since comparisons with
1234 // zero can use LOAD TEST and comparisons with other constants make a
1235 // natural memory operand.
Richard Sandifordd420f732013-12-13 15:28:45 +00001236 if (isa<ConstantFPSDNode>(C.Op1))
Richard Sandiford24e597b2013-08-23 11:27:19 +00001237 return false;
1238
1239 // Never swap comparisons with zero since there are many ways to optimize
1240 // those later.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001241 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001242 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001243 return false;
1244
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001245 // Also keep natural memory operands second if the loaded value is
1246 // only used here. Several comparisons have memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001247 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001248 return false;
1249
Richard Sandiford24e597b2013-08-23 11:27:19 +00001250 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1251 // In that case we generally prefer the memory to be second.
Richard Sandifordd420f732013-12-13 15:28:45 +00001252 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001253 // The only exceptions are when the second operand is a constant and
1254 // we can use things like CHHSI.
Richard Sandifordd420f732013-12-13 15:28:45 +00001255 if (!ConstOp1)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001256 return true;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001257 // The unsigned memory-immediate instructions can handle 16-bit
1258 // unsigned integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001259 if (C.ICmpType != SystemZICMP::SignedOnly &&
1260 isUInt<16>(ConstOp1->getZExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001261 return false;
1262 // The signed memory-immediate instructions can handle 16-bit
1263 // signed integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001264 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1265 isInt<16>(ConstOp1->getSExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001266 return false;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001267 return true;
1268 }
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001269
1270 // Try to promote the use of CGFR and CLGFR.
Richard Sandifordd420f732013-12-13 15:28:45 +00001271 unsigned Opcode0 = C.Op0.getOpcode();
1272 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001273 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001274 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001275 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001276 if (C.ICmpType != SystemZICMP::SignedOnly &&
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001277 Opcode0 == ISD::AND &&
Richard Sandifordd420f732013-12-13 15:28:45 +00001278 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1279 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001280 return true;
1281
Richard Sandiford24e597b2013-08-23 11:27:19 +00001282 return false;
1283}
1284
Richard Sandiford73170f82013-12-11 11:45:08 +00001285// Return a version of comparison CC mask CCMask in which the LT and GT
1286// actions are swapped.
1287static unsigned reverseCCMask(unsigned CCMask) {
1288 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1289 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1290 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1291 (CCMask & SystemZ::CCMASK_CMP_UO));
1292}
1293
Richard Sandiford0847c452013-12-13 15:50:30 +00001294// Check whether C tests for equality between X and Y and whether X - Y
1295// or Y - X is also computed. In that case it's better to compare the
1296// result of the subtraction against zero.
1297static void adjustForSubtraction(SelectionDAG &DAG, Comparison &C) {
1298 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1299 C.CCMask == SystemZ::CCMASK_CMP_NE) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001300 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001301 SDNode *N = *I;
1302 if (N->getOpcode() == ISD::SUB &&
1303 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1304 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1305 C.Op0 = SDValue(N, 0);
1306 C.Op1 = DAG.getConstant(0, N->getValueType(0));
1307 return;
1308 }
1309 }
1310 }
1311}
1312
Richard Sandifordd420f732013-12-13 15:28:45 +00001313// Check whether C compares a floating-point value with zero and if that
1314// floating-point value is also negated. In this case we can use the
1315// negation to set CC, so avoiding separate LOAD AND TEST and
1316// LOAD (NEGATIVE/COMPLEMENT) instructions.
1317static void adjustForFNeg(Comparison &C) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001318 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
Richard Sandiford73170f82013-12-11 11:45:08 +00001319 if (C1 && C1->isZero()) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001320 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford73170f82013-12-11 11:45:08 +00001321 SDNode *N = *I;
1322 if (N->getOpcode() == ISD::FNEG) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001323 C.Op0 = SDValue(N, 0);
1324 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford73170f82013-12-11 11:45:08 +00001325 return;
1326 }
1327 }
1328 }
1329}
1330
Richard Sandifordd420f732013-12-13 15:28:45 +00001331// Check whether C compares (shl X, 32) with 0 and whether X is
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001332// also sign-extended. In that case it is better to test the result
1333// of the sign extension using LTGFR.
1334//
1335// This case is important because InstCombine transforms a comparison
1336// with (sext (trunc X)) into a comparison with (shl X, 32).
Richard Sandifordd420f732013-12-13 15:28:45 +00001337static void adjustForLTGFR(Comparison &C) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001338 // Check for a comparison between (shl X, 32) and 0.
Richard Sandifordd420f732013-12-13 15:28:45 +00001339 if (C.Op0.getOpcode() == ISD::SHL &&
1340 C.Op0.getValueType() == MVT::i64 &&
1341 C.Op1.getOpcode() == ISD::Constant &&
1342 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001343 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001344 if (C1 && C1->getZExtValue() == 32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001345 SDValue ShlOp0 = C.Op0.getOperand(0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001346 // See whether X has any SIGN_EXTEND_INREG uses.
Richard Sandiford28c111e2014-03-06 11:00:15 +00001347 for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001348 SDNode *N = *I;
1349 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
1350 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001351 C.Op0 = SDValue(N, 0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001352 return;
1353 }
1354 }
1355 }
1356 }
1357}
1358
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001359// If C compares the truncation of an extending load, try to compare
1360// the untruncated value instead. This exposes more opportunities to
1361// reuse CC.
1362static void adjustICmpTruncate(SelectionDAG &DAG, Comparison &C) {
1363 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
1364 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
1365 C.Op1.getOpcode() == ISD::Constant &&
1366 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001367 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001368 if (L->getMemoryVT().getStoreSizeInBits()
1369 <= C.Op0.getValueType().getSizeInBits()) {
1370 unsigned Type = L->getExtensionType();
1371 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
1372 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
1373 C.Op0 = C.Op0.getOperand(0);
1374 C.Op1 = DAG.getConstant(0, C.Op0.getValueType());
1375 }
1376 }
1377 }
1378}
1379
Richard Sandiford030c1652013-09-13 09:09:50 +00001380// Return true if shift operation N has an in-range constant shift value.
1381// Store it in ShiftVal if so.
1382static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001383 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
Richard Sandiford030c1652013-09-13 09:09:50 +00001384 if (!Shift)
1385 return false;
1386
1387 uint64_t Amount = Shift->getZExtValue();
1388 if (Amount >= N.getValueType().getSizeInBits())
1389 return false;
1390
1391 ShiftVal = Amount;
1392 return true;
1393}
1394
1395// Check whether an AND with Mask is suitable for a TEST UNDER MASK
1396// instruction and whether the CC value is descriptive enough to handle
1397// a comparison of type Opcode between the AND result and CmpVal.
1398// CCMask says which comparison result is being tested and BitSize is
1399// the number of bits in the operands. If TEST UNDER MASK can be used,
1400// return the corresponding CC mask, otherwise return 0.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001401static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
1402 uint64_t Mask, uint64_t CmpVal,
1403 unsigned ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001404 assert(Mask != 0 && "ANDs with zero should have been removed by now");
1405
Richard Sandiford030c1652013-09-13 09:09:50 +00001406 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
1407 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
1408 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
1409 return 0;
1410
Richard Sandiford113c8702013-09-03 15:38:35 +00001411 // Work out the masks for the lowest and highest bits.
1412 unsigned HighShift = 63 - countLeadingZeros(Mask);
1413 uint64_t High = uint64_t(1) << HighShift;
1414 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
1415
1416 // Signed ordered comparisons are effectively unsigned if the sign
1417 // bit is dropped.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001418 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
Richard Sandiford113c8702013-09-03 15:38:35 +00001419
1420 // Check for equality comparisons with 0, or the equivalent.
1421 if (CmpVal == 0) {
1422 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1423 return SystemZ::CCMASK_TM_ALL_0;
1424 if (CCMask == SystemZ::CCMASK_CMP_NE)
1425 return SystemZ::CCMASK_TM_SOME_1;
1426 }
1427 if (EffectivelyUnsigned && CmpVal <= Low) {
1428 if (CCMask == SystemZ::CCMASK_CMP_LT)
1429 return SystemZ::CCMASK_TM_ALL_0;
1430 if (CCMask == SystemZ::CCMASK_CMP_GE)
1431 return SystemZ::CCMASK_TM_SOME_1;
1432 }
1433 if (EffectivelyUnsigned && CmpVal < Low) {
1434 if (CCMask == SystemZ::CCMASK_CMP_LE)
1435 return SystemZ::CCMASK_TM_ALL_0;
1436 if (CCMask == SystemZ::CCMASK_CMP_GT)
1437 return SystemZ::CCMASK_TM_SOME_1;
1438 }
1439
1440 // Check for equality comparisons with the mask, or the equivalent.
1441 if (CmpVal == Mask) {
1442 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1443 return SystemZ::CCMASK_TM_ALL_1;
1444 if (CCMask == SystemZ::CCMASK_CMP_NE)
1445 return SystemZ::CCMASK_TM_SOME_0;
1446 }
1447 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
1448 if (CCMask == SystemZ::CCMASK_CMP_GT)
1449 return SystemZ::CCMASK_TM_ALL_1;
1450 if (CCMask == SystemZ::CCMASK_CMP_LE)
1451 return SystemZ::CCMASK_TM_SOME_0;
1452 }
1453 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
1454 if (CCMask == SystemZ::CCMASK_CMP_GE)
1455 return SystemZ::CCMASK_TM_ALL_1;
1456 if (CCMask == SystemZ::CCMASK_CMP_LT)
1457 return SystemZ::CCMASK_TM_SOME_0;
1458 }
1459
1460 // Check for ordered comparisons with the top bit.
1461 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
1462 if (CCMask == SystemZ::CCMASK_CMP_LE)
1463 return SystemZ::CCMASK_TM_MSB_0;
1464 if (CCMask == SystemZ::CCMASK_CMP_GT)
1465 return SystemZ::CCMASK_TM_MSB_1;
1466 }
1467 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
1468 if (CCMask == SystemZ::CCMASK_CMP_LT)
1469 return SystemZ::CCMASK_TM_MSB_0;
1470 if (CCMask == SystemZ::CCMASK_CMP_GE)
1471 return SystemZ::CCMASK_TM_MSB_1;
1472 }
1473
1474 // If there are just two bits, we can do equality checks for Low and High
1475 // as well.
1476 if (Mask == Low + High) {
1477 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
1478 return SystemZ::CCMASK_TM_MIXED_MSB_0;
1479 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
1480 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
1481 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
1482 return SystemZ::CCMASK_TM_MIXED_MSB_1;
1483 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
1484 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
1485 }
1486
1487 // Looks like we've exhausted our options.
1488 return 0;
1489}
1490
Richard Sandifordd420f732013-12-13 15:28:45 +00001491// See whether C can be implemented as a TEST UNDER MASK instruction.
1492// Update the arguments with the TM version if so.
1493static void adjustForTestUnderMask(SelectionDAG &DAG, Comparison &C) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001494 // Check that we have a comparison with a constant.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001495 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001496 if (!ConstOp1)
Richard Sandiford35b9be22013-08-28 10:31:43 +00001497 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001498 uint64_t CmpVal = ConstOp1->getZExtValue();
Richard Sandiford35b9be22013-08-28 10:31:43 +00001499
1500 // Check whether the nonconstant input is an AND with a constant mask.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001501 Comparison NewC(C);
1502 uint64_t MaskVal;
Craig Topper062a2ba2014-04-25 05:30:21 +00001503 ConstantSDNode *Mask = nullptr;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001504 if (C.Op0.getOpcode() == ISD::AND) {
1505 NewC.Op0 = C.Op0.getOperand(0);
1506 NewC.Op1 = C.Op0.getOperand(1);
1507 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
1508 if (!Mask)
1509 return;
1510 MaskVal = Mask->getZExtValue();
1511 } else {
1512 // There is no instruction to compare with a 64-bit immediate
1513 // so use TMHH instead if possible. We need an unsigned ordered
1514 // comparison with an i64 immediate.
1515 if (NewC.Op0.getValueType() != MVT::i64 ||
1516 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
1517 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
1518 NewC.ICmpType == SystemZICMP::SignedOnly)
1519 return;
1520 // Convert LE and GT comparisons into LT and GE.
1521 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
1522 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
1523 if (CmpVal == uint64_t(-1))
1524 return;
1525 CmpVal += 1;
1526 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1527 }
1528 // If the low N bits of Op1 are zero than the low N bits of Op0 can
1529 // be masked off without changing the result.
1530 MaskVal = -(CmpVal & -CmpVal);
1531 NewC.ICmpType = SystemZICMP::UnsignedOnly;
1532 }
Richard Sandiford35b9be22013-08-28 10:31:43 +00001533
Richard Sandiford113c8702013-09-03 15:38:35 +00001534 // Check whether the combination of mask, comparison value and comparison
1535 // type are suitable.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001536 unsigned BitSize = NewC.Op0.getValueType().getSizeInBits();
Richard Sandiford030c1652013-09-13 09:09:50 +00001537 unsigned NewCCMask, ShiftVal;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001538 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
1539 NewC.Op0.getOpcode() == ISD::SHL &&
1540 isSimpleShift(NewC.Op0, ShiftVal) &&
1541 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
1542 MaskVal >> ShiftVal,
Richard Sandiford030c1652013-09-13 09:09:50 +00001543 CmpVal >> ShiftVal,
1544 SystemZICMP::Any))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001545 NewC.Op0 = NewC.Op0.getOperand(0);
1546 MaskVal >>= ShiftVal;
1547 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
1548 NewC.Op0.getOpcode() == ISD::SRL &&
1549 isSimpleShift(NewC.Op0, ShiftVal) &&
1550 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
Richard Sandiford030c1652013-09-13 09:09:50 +00001551 MaskVal << ShiftVal,
1552 CmpVal << ShiftVal,
1553 SystemZICMP::UnsignedOnly))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001554 NewC.Op0 = NewC.Op0.getOperand(0);
1555 MaskVal <<= ShiftVal;
Richard Sandiford030c1652013-09-13 09:09:50 +00001556 } else {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001557 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
1558 NewC.ICmpType);
Richard Sandiford030c1652013-09-13 09:09:50 +00001559 if (!NewCCMask)
1560 return;
1561 }
Richard Sandiford113c8702013-09-03 15:38:35 +00001562
Richard Sandiford35b9be22013-08-28 10:31:43 +00001563 // Go ahead and make the change.
Richard Sandifordd420f732013-12-13 15:28:45 +00001564 C.Opcode = SystemZISD::TM;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001565 C.Op0 = NewC.Op0;
1566 if (Mask && Mask->getZExtValue() == MaskVal)
1567 C.Op1 = SDValue(Mask, 0);
1568 else
1569 C.Op1 = DAG.getConstant(MaskVal, C.Op0.getValueType());
Richard Sandifordd420f732013-12-13 15:28:45 +00001570 C.CCValid = SystemZ::CCMASK_TM;
1571 C.CCMask = NewCCMask;
Richard Sandiford35b9be22013-08-28 10:31:43 +00001572}
1573
Richard Sandifordd420f732013-12-13 15:28:45 +00001574// Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
1575static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
1576 ISD::CondCode Cond) {
1577 Comparison C(CmpOp0, CmpOp1);
1578 C.CCMask = CCMaskForCondCode(Cond);
1579 if (C.Op0.getValueType().isFloatingPoint()) {
1580 C.CCValid = SystemZ::CCMASK_FCMP;
1581 C.Opcode = SystemZISD::FCMP;
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001582 adjustForFNeg(C);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001583 } else {
Richard Sandifordd420f732013-12-13 15:28:45 +00001584 C.CCValid = SystemZ::CCMASK_ICMP;
1585 C.Opcode = SystemZISD::ICMP;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001586 // Choose the type of comparison. Equality and inequality tests can
1587 // use either signed or unsigned comparisons. The choice also doesn't
1588 // matter if both sign bits are known to be clear. In those cases we
1589 // want to give the main isel code the freedom to choose whichever
1590 // form fits best.
Richard Sandifordd420f732013-12-13 15:28:45 +00001591 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1592 C.CCMask == SystemZ::CCMASK_CMP_NE ||
1593 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
1594 C.ICmpType = SystemZICMP::Any;
1595 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
1596 C.ICmpType = SystemZICMP::UnsignedOnly;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001597 else
Richard Sandifordd420f732013-12-13 15:28:45 +00001598 C.ICmpType = SystemZICMP::SignedOnly;
1599 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
1600 adjustZeroCmp(DAG, C);
1601 adjustSubwordCmp(DAG, C);
Richard Sandiford0847c452013-12-13 15:50:30 +00001602 adjustForSubtraction(DAG, C);
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001603 adjustForLTGFR(C);
1604 adjustICmpTruncate(DAG, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001605 }
1606
Richard Sandifordd420f732013-12-13 15:28:45 +00001607 if (shouldSwapCmpOperands(C)) {
1608 std::swap(C.Op0, C.Op1);
1609 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford24e597b2013-08-23 11:27:19 +00001610 }
1611
Richard Sandifordd420f732013-12-13 15:28:45 +00001612 adjustForTestUnderMask(DAG, C);
Richard Sandifordd420f732013-12-13 15:28:45 +00001613 return C;
1614}
1615
1616// Emit the comparison instruction described by C.
1617static SDValue emitCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
1618 if (C.Opcode == SystemZISD::ICMP)
1619 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
1620 DAG.getConstant(C.ICmpType, MVT::i32));
1621 if (C.Opcode == SystemZISD::TM) {
1622 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
1623 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
1624 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
1625 DAG.getConstant(RegisterOnly, MVT::i32));
1626 }
1627 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001628}
1629
Richard Sandiford7d86e472013-08-21 09:34:56 +00001630// Implement a 32-bit *MUL_LOHI operation by extending both operands to
1631// 64 bits. Extend is the extension type to use. Store the high part
1632// in Hi and the low part in Lo.
1633static void lowerMUL_LOHI32(SelectionDAG &DAG, SDLoc DL,
1634 unsigned Extend, SDValue Op0, SDValue Op1,
1635 SDValue &Hi, SDValue &Lo) {
1636 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
1637 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
1638 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
1639 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul, DAG.getConstant(32, MVT::i64));
1640 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
1641 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
1642}
1643
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001644// Lower a binary operation that produces two VT results, one in each
1645// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
1646// Extend extends Op0 to a GR128, and Opcode performs the GR128 operation
1647// on the extended Op0 and (unextended) Op1. Store the even register result
1648// in Even and the odd register result in Odd.
Andrew Trickef9de2a2013-05-25 02:42:55 +00001649static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001650 unsigned Extend, unsigned Opcode,
1651 SDValue Op0, SDValue Op1,
1652 SDValue &Even, SDValue &Odd) {
1653 SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0);
1654 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped,
1655 SDValue(In128, 0), Op1);
1656 bool Is32Bit = is32Bit(VT);
Richard Sandifordd8163202013-09-13 09:12:44 +00001657 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
1658 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001659}
1660
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001661// Return an i32 value that is 1 if the CC value produced by Glue is
1662// in the mask CCMask and 0 otherwise. CC is known to have a value
1663// in CCValid, so other values can be ignored.
1664static SDValue emitSETCC(SelectionDAG &DAG, SDLoc DL, SDValue Glue,
1665 unsigned CCValid, unsigned CCMask) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001666 IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
1667 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
1668
1669 if (Conversion.XORValue)
1670 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
1671 DAG.getConstant(Conversion.XORValue, MVT::i32));
1672
1673 if (Conversion.AddValue)
1674 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
1675 DAG.getConstant(Conversion.AddValue, MVT::i32));
1676
1677 // The SHR/AND sequence should get optimized to an RISBG.
1678 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
1679 DAG.getConstant(Conversion.Bit, MVT::i32));
1680 if (Conversion.Bit != 31)
1681 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
1682 DAG.getConstant(1, MVT::i32));
1683 return Result;
1684}
1685
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001686SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
1687 SelectionDAG &DAG) const {
1688 SDValue CmpOp0 = Op.getOperand(0);
1689 SDValue CmpOp1 = Op.getOperand(1);
1690 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
1691 SDLoc DL(Op);
1692
Richard Sandifordd420f732013-12-13 15:28:45 +00001693 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC));
1694 SDValue Glue = emitCmp(DAG, DL, C);
1695 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001696}
1697
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001698SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
1699 SDValue Chain = Op.getOperand(0);
1700 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1701 SDValue CmpOp0 = Op.getOperand(2);
1702 SDValue CmpOp1 = Op.getOperand(3);
1703 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001704 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001705
Richard Sandifordd420f732013-12-13 15:28:45 +00001706 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC));
1707 SDValue Glue = emitCmp(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001708 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
Richard Sandifordd420f732013-12-13 15:28:45 +00001709 Chain, DAG.getConstant(C.CCValid, MVT::i32),
1710 DAG.getConstant(C.CCMask, MVT::i32), Dest, Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001711}
1712
Richard Sandiford57485472013-12-13 15:35:00 +00001713// Return true if Pos is CmpOp and Neg is the negative of CmpOp,
1714// allowing Pos and Neg to be wider than CmpOp.
1715static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
1716 return (Neg.getOpcode() == ISD::SUB &&
1717 Neg.getOperand(0).getOpcode() == ISD::Constant &&
1718 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
1719 Neg.getOperand(1) == Pos &&
1720 (Pos == CmpOp ||
1721 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
1722 Pos.getOperand(0) == CmpOp)));
1723}
1724
1725// Return the absolute or negative absolute of Op; IsNegative decides which.
1726static SDValue getAbsolute(SelectionDAG &DAG, SDLoc DL, SDValue Op,
1727 bool IsNegative) {
1728 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
1729 if (IsNegative)
1730 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
1731 DAG.getConstant(0, Op.getValueType()), Op);
1732 return Op;
1733}
1734
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001735SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
1736 SelectionDAG &DAG) const {
1737 SDValue CmpOp0 = Op.getOperand(0);
1738 SDValue CmpOp1 = Op.getOperand(1);
1739 SDValue TrueOp = Op.getOperand(2);
1740 SDValue FalseOp = Op.getOperand(3);
1741 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001742 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001743
Richard Sandifordd420f732013-12-13 15:28:45 +00001744 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC));
Richard Sandiford57485472013-12-13 15:35:00 +00001745
1746 // Check for absolute and negative-absolute selections, including those
1747 // where the comparison value is sign-extended (for LPGFR and LNGFR).
1748 // This check supplements the one in DAGCombiner.
1749 if (C.Opcode == SystemZISD::ICMP &&
1750 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
1751 C.CCMask != SystemZ::CCMASK_CMP_NE &&
1752 C.Op1.getOpcode() == ISD::Constant &&
1753 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
1754 if (isAbsolute(C.Op0, TrueOp, FalseOp))
1755 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
1756 if (isAbsolute(C.Op0, FalseOp, TrueOp))
1757 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
1758 }
1759
Richard Sandifordd420f732013-12-13 15:28:45 +00001760 SDValue Glue = emitCmp(DAG, DL, C);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001761
1762 // Special case for handling -1/0 results. The shifts we use here
1763 // should get optimized with the IPM conversion sequence.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001764 auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
1765 auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001766 if (TrueC && FalseC) {
1767 int64_t TrueVal = TrueC->getSExtValue();
1768 int64_t FalseVal = FalseC->getSExtValue();
1769 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
1770 // Invert the condition if we want -1 on false.
1771 if (TrueVal == 0)
Richard Sandifordd420f732013-12-13 15:28:45 +00001772 C.CCMask ^= C.CCValid;
1773 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001774 EVT VT = Op.getValueType();
1775 // Extend the result to VT. Upper bits are ignored.
1776 if (!is32Bit(VT))
1777 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
1778 // Sign-extend from the low bit.
1779 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, MVT::i32);
1780 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
1781 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
1782 }
1783 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001784
Benjamin Kramerea68a942015-02-19 15:26:17 +00001785 SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, MVT::i32),
1786 DAG.getConstant(C.CCMask, MVT::i32), Glue};
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001787
1788 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00001789 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001790}
1791
1792SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
1793 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001794 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001795 const GlobalValue *GV = Node->getGlobal();
1796 int64_t Offset = Node->getOffset();
1797 EVT PtrVT = getPointerTy();
Eric Christopher93bf97c2014-06-27 07:38:01 +00001798 Reloc::Model RM = DAG.getTarget().getRelocationModel();
1799 CodeModel::Model CM = DAG.getTarget().getCodeModel();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001800
1801 SDValue Result;
1802 if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) {
Richard Sandiford54b36912013-09-27 15:14:04 +00001803 // Assign anchors at 1<<12 byte boundaries.
1804 uint64_t Anchor = Offset & ~uint64_t(0xfff);
1805 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
1806 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1807
1808 // The offset can be folded into the address if it is aligned to a halfword.
1809 Offset -= Anchor;
1810 if (Offset != 0 && (Offset & 1) == 0) {
1811 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
1812 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001813 Offset = 0;
1814 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001815 } else {
1816 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
1817 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1818 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
1819 MachinePointerInfo::getGOT(), false, false, false, 0);
1820 }
1821
1822 // If there was a non-zero offset that we didn't fold, create an explicit
1823 // addition for it.
1824 if (Offset != 0)
1825 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
1826 DAG.getConstant(Offset, PtrVT));
1827
1828 return Result;
1829}
1830
Ulrich Weigand7db69182015-02-18 09:13:27 +00001831SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
1832 SelectionDAG &DAG,
1833 unsigned Opcode,
1834 SDValue GOTOffset) const {
1835 SDLoc DL(Node);
1836 EVT PtrVT = getPointerTy();
1837 SDValue Chain = DAG.getEntryNode();
1838 SDValue Glue;
1839
1840 // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
1841 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1842 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
1843 Glue = Chain.getValue(1);
1844 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
1845 Glue = Chain.getValue(1);
1846
1847 // The first call operand is the chain and the second is the TLS symbol.
1848 SmallVector<SDValue, 8> Ops;
1849 Ops.push_back(Chain);
1850 Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
1851 Node->getValueType(0),
1852 0, 0));
1853
1854 // Add argument registers to the end of the list so that they are
1855 // known live into the call.
1856 Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
1857 Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
1858
1859 // Add a register mask operand representing the call-preserved registers.
1860 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00001861 const uint32_t *Mask =
1862 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C);
Ulrich Weigand7db69182015-02-18 09:13:27 +00001863 assert(Mask && "Missing call preserved mask for calling convention");
1864 Ops.push_back(DAG.getRegisterMask(Mask));
1865
1866 // Glue the call to the argument copies.
1867 Ops.push_back(Glue);
1868
1869 // Emit the call.
1870 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1871 Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
1872 Glue = Chain.getValue(1);
1873
1874 // Copy the return value from %r2.
1875 return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
1876}
1877
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001878SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
1879 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001880 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001881 const GlobalValue *GV = Node->getGlobal();
1882 EVT PtrVT = getPointerTy();
Eric Christopher93bf97c2014-06-27 07:38:01 +00001883 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001884
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001885 // The high part of the thread pointer is in access register 0.
1886 SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
1887 DAG.getConstant(0, MVT::i32));
1888 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
1889
1890 // The low part of the thread pointer is in access register 1.
1891 SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
1892 DAG.getConstant(1, MVT::i32));
1893 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
1894
1895 // Merge them into a single 64-bit address.
1896 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
1897 DAG.getConstant(32, PtrVT));
1898 SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
1899
Ulrich Weigand7db69182015-02-18 09:13:27 +00001900 // Get the offset of GA from the thread pointer, based on the TLS model.
1901 SDValue Offset;
1902 switch (model) {
1903 case TLSModel::GeneralDynamic: {
1904 // Load the GOT offset of the tls_index (module ID / per-symbol offset).
1905 SystemZConstantPoolValue *CPV =
1906 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001907
Ulrich Weigand7db69182015-02-18 09:13:27 +00001908 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
1909 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
1910 Offset, MachinePointerInfo::getConstantPool(),
1911 false, false, false, 0);
1912
1913 // Call __tls_get_offset to retrieve the offset.
1914 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
1915 break;
1916 }
1917
1918 case TLSModel::LocalDynamic: {
1919 // Load the GOT offset of the module ID.
1920 SystemZConstantPoolValue *CPV =
1921 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
1922
1923 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
1924 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
1925 Offset, MachinePointerInfo::getConstantPool(),
1926 false, false, false, 0);
1927
1928 // Call __tls_get_offset to retrieve the module base offset.
1929 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
1930
1931 // Note: The SystemZLDCleanupPass will remove redundant computations
1932 // of the module base offset. Count total number of local-dynamic
1933 // accesses to trigger execution of that pass.
1934 SystemZMachineFunctionInfo* MFI =
1935 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
1936 MFI->incNumLocalDynamicTLSAccesses();
1937
1938 // Add the per-symbol offset.
1939 CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
1940
1941 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8);
1942 DTPOffset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
1943 DTPOffset, MachinePointerInfo::getConstantPool(),
1944 false, false, false, 0);
1945
1946 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
1947 break;
1948 }
1949
1950 case TLSModel::InitialExec: {
1951 // Load the offset from the GOT.
1952 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1953 SystemZII::MO_INDNTPOFF);
1954 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
1955 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
1956 Offset, MachinePointerInfo::getGOT(),
1957 false, false, false, 0);
1958 break;
1959 }
1960
1961 case TLSModel::LocalExec: {
1962 // Force the offset into the constant pool and load it from there.
1963 SystemZConstantPoolValue *CPV =
1964 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
1965
1966 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
1967 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
1968 Offset, MachinePointerInfo::getConstantPool(),
1969 false, false, false, 0);
1970 break;
Ulrich Weigandb7e59092015-02-18 09:42:23 +00001971 }
Ulrich Weigand7db69182015-02-18 09:13:27 +00001972 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001973
1974 // Add the base and offset together.
1975 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
1976}
1977
1978SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
1979 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001980 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001981 const BlockAddress *BA = Node->getBlockAddress();
1982 int64_t Offset = Node->getOffset();
1983 EVT PtrVT = getPointerTy();
1984
1985 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
1986 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1987 return Result;
1988}
1989
1990SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
1991 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001992 SDLoc DL(JT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001993 EVT PtrVT = getPointerTy();
1994 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1995
1996 // Use LARL to load the address of the table.
1997 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1998}
1999
2000SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
2001 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002002 SDLoc DL(CP);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002003 EVT PtrVT = getPointerTy();
2004
2005 SDValue Result;
2006 if (CP->isMachineConstantPoolEntry())
2007 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2008 CP->getAlignment());
2009 else
2010 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2011 CP->getAlignment(), CP->getOffset());
2012
2013 // Use LARL to load the address of the constant pool entry.
2014 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2015}
2016
2017SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
2018 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002019 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002020 SDValue In = Op.getOperand(0);
2021 EVT InVT = In.getValueType();
2022 EVT ResVT = Op.getValueType();
2023
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002024 if (InVT == MVT::i32 && ResVT == MVT::f32) {
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002025 SDValue In64;
2026 if (Subtarget.hasHighWord()) {
2027 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
2028 MVT::i64);
2029 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
2030 MVT::i64, SDValue(U64, 0), In);
2031 } else {
2032 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
2033 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
2034 DAG.getConstant(32, MVT::i64));
2035 }
2036 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
Richard Sandiford87a44362013-09-30 10:28:35 +00002037 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32,
Richard Sandifordd8163202013-09-13 09:12:44 +00002038 DL, MVT::f32, Out64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002039 }
2040 if (InVT == MVT::f32 && ResVT == MVT::i32) {
2041 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
Richard Sandiford87a44362013-09-30 10:28:35 +00002042 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002043 MVT::f64, SDValue(U64, 0), In);
2044 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002045 if (Subtarget.hasHighWord())
2046 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
2047 MVT::i32, Out64);
2048 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
2049 DAG.getConstant(32, MVT::i64));
2050 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002051 }
2052 llvm_unreachable("Unexpected bitcast combination");
2053}
2054
2055SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
2056 SelectionDAG &DAG) const {
2057 MachineFunction &MF = DAG.getMachineFunction();
2058 SystemZMachineFunctionInfo *FuncInfo =
2059 MF.getInfo<SystemZMachineFunctionInfo>();
2060 EVT PtrVT = getPointerTy();
2061
2062 SDValue Chain = Op.getOperand(0);
2063 SDValue Addr = Op.getOperand(1);
2064 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002065 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002066
2067 // The initial values of each field.
2068 const unsigned NumFields = 4;
2069 SDValue Fields[NumFields] = {
2070 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), PtrVT),
2071 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), PtrVT),
2072 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
2073 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
2074 };
2075
2076 // Store each field into its respective slot.
2077 SDValue MemOps[NumFields];
2078 unsigned Offset = 0;
2079 for (unsigned I = 0; I < NumFields; ++I) {
2080 SDValue FieldAddr = Addr;
2081 if (Offset != 0)
2082 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
2083 DAG.getIntPtrConstant(Offset));
2084 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
2085 MachinePointerInfo(SV, Offset),
2086 false, false, 0);
2087 Offset += 8;
2088 }
Craig Topper48d114b2014-04-26 18:35:24 +00002089 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002090}
2091
2092SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
2093 SelectionDAG &DAG) const {
2094 SDValue Chain = Op.getOperand(0);
2095 SDValue DstPtr = Op.getOperand(1);
2096 SDValue SrcPtr = Op.getOperand(2);
2097 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
2098 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002099 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002100
2101 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32),
2102 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
2103 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
2104}
2105
2106SDValue SystemZTargetLowering::
2107lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
2108 SDValue Chain = Op.getOperand(0);
2109 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002110 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002111
2112 unsigned SPReg = getStackPointerRegisterToSaveRestore();
2113
2114 // Get a reference to the stack pointer.
2115 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
2116
2117 // Get the new stack pointer value.
2118 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, Size);
2119
2120 // Copy the new stack pointer back.
2121 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
2122
2123 // The allocated data lives above the 160 bytes allocated for the standard
2124 // frame, plus any outgoing stack arguments. We don't know how much that
2125 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
2126 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
2127 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
2128
2129 SDValue Ops[2] = { Result, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002130 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002131}
2132
Richard Sandiford7d86e472013-08-21 09:34:56 +00002133SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
2134 SelectionDAG &DAG) const {
2135 EVT VT = Op.getValueType();
2136 SDLoc DL(Op);
2137 SDValue Ops[2];
2138 if (is32Bit(VT))
2139 // Just do a normal 64-bit multiplication and extract the results.
2140 // We define this so that it can be used for constant division.
2141 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
2142 Op.getOperand(1), Ops[1], Ops[0]);
2143 else {
2144 // Do a full 128-bit multiplication based on UMUL_LOHI64:
2145 //
2146 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
2147 //
2148 // but using the fact that the upper halves are either all zeros
2149 // or all ones:
2150 //
2151 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
2152 //
2153 // and grouping the right terms together since they are quicker than the
2154 // multiplication:
2155 //
2156 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
2157 SDValue C63 = DAG.getConstant(63, MVT::i64);
2158 SDValue LL = Op.getOperand(0);
2159 SDValue RL = Op.getOperand(1);
2160 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
2161 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
2162 // UMUL_LOHI64 returns the low result in the odd register and the high
2163 // result in the even register. SMUL_LOHI is defined to return the
2164 // low half first, so the results are in reverse order.
2165 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2166 LL, RL, Ops[1], Ops[0]);
2167 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
2168 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
2169 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
2170 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
2171 }
Craig Topper64941d92014-04-27 19:20:57 +00002172 return DAG.getMergeValues(Ops, DL);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002173}
2174
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002175SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
2176 SelectionDAG &DAG) const {
2177 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002178 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002179 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00002180 if (is32Bit(VT))
2181 // Just do a normal 64-bit multiplication and extract the results.
2182 // We define this so that it can be used for constant division.
2183 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
2184 Op.getOperand(1), Ops[1], Ops[0]);
2185 else
2186 // UMUL_LOHI64 returns the low result in the odd register and the high
2187 // result in the even register. UMUL_LOHI is defined to return the
2188 // low half first, so the results are in reverse order.
2189 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2190 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002191 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002192}
2193
2194SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
2195 SelectionDAG &DAG) const {
2196 SDValue Op0 = Op.getOperand(0);
2197 SDValue Op1 = Op.getOperand(1);
2198 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002199 SDLoc DL(Op);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002200 unsigned Opcode;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002201
2202 // We use DSGF for 32-bit division.
2203 if (is32Bit(VT)) {
2204 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002205 Opcode = SystemZISD::SDIVREM32;
2206 } else if (DAG.ComputeNumSignBits(Op1) > 32) {
2207 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
2208 Opcode = SystemZISD::SDIVREM32;
2209 } else
2210 Opcode = SystemZISD::SDIVREM64;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002211
2212 // DSG(F) takes a 64-bit dividend, so the even register in the GR128
2213 // input is "don't care". The instruction returns the remainder in
2214 // the even register and the quotient in the odd register.
2215 SDValue Ops[2];
Richard Sandiforde6e78852013-07-02 15:40:22 +00002216 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002217 Op0, Op1, Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002218 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002219}
2220
2221SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
2222 SelectionDAG &DAG) const {
2223 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002224 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002225
2226 // DL(G) uses a double-width dividend, so we need to clear the even
2227 // register in the GR128 input. The instruction returns the remainder
2228 // in the even register and the quotient in the odd register.
2229 SDValue Ops[2];
2230 if (is32Bit(VT))
2231 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
2232 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2233 else
2234 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
2235 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002236 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002237}
2238
2239SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
2240 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
2241
2242 // Get the known-zero masks for each operand.
2243 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
2244 APInt KnownZero[2], KnownOne[2];
Jay Foada0653a32014-05-14 21:14:37 +00002245 DAG.computeKnownBits(Ops[0], KnownZero[0], KnownOne[0]);
2246 DAG.computeKnownBits(Ops[1], KnownZero[1], KnownOne[1]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002247
2248 // See if the upper 32 bits of one operand and the lower 32 bits of the
2249 // other are known zero. They are the low and high operands respectively.
2250 uint64_t Masks[] = { KnownZero[0].getZExtValue(),
2251 KnownZero[1].getZExtValue() };
2252 unsigned High, Low;
2253 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
2254 High = 1, Low = 0;
2255 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
2256 High = 0, Low = 1;
2257 else
2258 return Op;
2259
2260 SDValue LowOp = Ops[Low];
2261 SDValue HighOp = Ops[High];
2262
2263 // If the high part is a constant, we're better off using IILH.
2264 if (HighOp.getOpcode() == ISD::Constant)
2265 return Op;
2266
2267 // If the low part is a constant that is outside the range of LHI,
2268 // then we're better off using IILF.
2269 if (LowOp.getOpcode() == ISD::Constant) {
2270 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
2271 if (!isInt<16>(Value))
2272 return Op;
2273 }
2274
2275 // Check whether the high part is an AND that doesn't change the
2276 // high 32 bits and just masks out low bits. We can skip it if so.
2277 if (HighOp.getOpcode() == ISD::AND &&
2278 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
Richard Sandifordccc2a7c2013-12-03 11:01:54 +00002279 SDValue HighOp0 = HighOp.getOperand(0);
2280 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
2281 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
2282 HighOp = HighOp0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002283 }
2284
2285 // Take advantage of the fact that all GR32 operations only change the
2286 // low 32 bits by truncating Low to an i32 and inserting it directly
2287 // using a subreg. The interesting cases are those where the truncation
2288 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002289 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002290 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
Richard Sandiford87a44362013-09-30 10:28:35 +00002291 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002292 MVT::i64, HighOp, Low32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002293}
2294
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002295// Op is an atomic load. Lower it into a normal volatile load.
2296SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
2297 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002298 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002299 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
2300 Node->getChain(), Node->getBasePtr(),
2301 Node->getMemoryVT(), Node->getMemOperand());
2302}
2303
2304// Op is an atomic store. Lower it into a normal volatile store followed
2305// by a serialization.
2306SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
2307 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002308 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002309 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
2310 Node->getBasePtr(), Node->getMemoryVT(),
2311 Node->getMemOperand());
2312 return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other,
2313 Chain), 0);
2314}
2315
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002316// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
2317// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002318SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
2319 SelectionDAG &DAG,
2320 unsigned Opcode) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002321 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002322
2323 // 32-bit operations need no code outside the main loop.
2324 EVT NarrowVT = Node->getMemoryVT();
2325 EVT WideVT = MVT::i32;
2326 if (NarrowVT == WideVT)
2327 return Op;
2328
2329 int64_t BitSize = NarrowVT.getSizeInBits();
2330 SDValue ChainIn = Node->getChain();
2331 SDValue Addr = Node->getBasePtr();
2332 SDValue Src2 = Node->getVal();
2333 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002334 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002335 EVT PtrVT = Addr.getValueType();
2336
2337 // Convert atomic subtracts of constants into additions.
2338 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
Richard Sandiford21f5d682014-03-06 11:22:58 +00002339 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002340 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
2341 Src2 = DAG.getConstant(-Const->getSExtValue(), Src2.getValueType());
2342 }
2343
2344 // Get the address of the containing word.
2345 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
2346 DAG.getConstant(-4, PtrVT));
2347
2348 // Get the number of bits that the word must be rotated left in order
2349 // to bring the field to the top bits of a GR32.
2350 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
2351 DAG.getConstant(3, PtrVT));
2352 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
2353
2354 // Get the complementing shift amount, for rotating a field in the top
2355 // bits back to its proper position.
2356 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
2357 DAG.getConstant(0, WideVT), BitShift);
2358
2359 // Extend the source operand to 32 bits and prepare it for the inner loop.
2360 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
2361 // operations require the source to be shifted in advance. (This shift
2362 // can be folded if the source is constant.) For AND and NAND, the lower
2363 // bits must be set, while for other opcodes they should be left clear.
2364 if (Opcode != SystemZISD::ATOMIC_SWAPW)
2365 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
2366 DAG.getConstant(32 - BitSize, WideVT));
2367 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
2368 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
2369 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
2370 DAG.getConstant(uint32_t(-1) >> BitSize, WideVT));
2371
2372 // Construct the ATOMIC_LOADW_* node.
2373 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
2374 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
2375 DAG.getConstant(BitSize, WideVT) };
2376 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002377 NarrowVT, MMO);
2378
2379 // Rotate the result of the final CS so that the field is in the lower
2380 // bits of a GR32, then truncate it.
2381 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
2382 DAG.getConstant(BitSize, WideVT));
2383 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
2384
2385 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002386 return DAG.getMergeValues(RetOps, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002387}
2388
Richard Sandiford41350a52013-12-24 15:18:04 +00002389// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
Richard Sandiford002019a2013-12-24 15:22:39 +00002390// into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
Richard Sandiford41350a52013-12-24 15:18:04 +00002391// operations into additions.
2392SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
2393 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002394 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandiford41350a52013-12-24 15:18:04 +00002395 EVT MemVT = Node->getMemoryVT();
2396 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
2397 // A full-width operation.
2398 assert(Op.getValueType() == MemVT && "Mismatched VTs");
2399 SDValue Src2 = Node->getVal();
2400 SDValue NegSrc2;
2401 SDLoc DL(Src2);
2402
Richard Sandiford21f5d682014-03-06 11:22:58 +00002403 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
Richard Sandiford41350a52013-12-24 15:18:04 +00002404 // Use an addition if the operand is constant and either LAA(G) is
2405 // available or the negative value is in the range of A(G)FHI.
2406 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
Eric Christopher93bf97c2014-06-27 07:38:01 +00002407 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00002408 NegSrc2 = DAG.getConstant(Value, MemVT);
Eric Christopher93bf97c2014-06-27 07:38:01 +00002409 } else if (Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00002410 // Use LAA(G) if available.
2411 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, MemVT),
2412 Src2);
2413
2414 if (NegSrc2.getNode())
2415 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
2416 Node->getChain(), Node->getBasePtr(), NegSrc2,
2417 Node->getMemOperand(), Node->getOrdering(),
2418 Node->getSynchScope());
2419
2420 // Use the node as-is.
2421 return Op;
2422 }
2423
2424 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
2425}
2426
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002427// Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
2428// into a fullword ATOMIC_CMP_SWAPW operation.
2429SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
2430 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002431 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002432
2433 // We have native support for 32-bit compare and swap.
2434 EVT NarrowVT = Node->getMemoryVT();
2435 EVT WideVT = MVT::i32;
2436 if (NarrowVT == WideVT)
2437 return Op;
2438
2439 int64_t BitSize = NarrowVT.getSizeInBits();
2440 SDValue ChainIn = Node->getOperand(0);
2441 SDValue Addr = Node->getOperand(1);
2442 SDValue CmpVal = Node->getOperand(2);
2443 SDValue SwapVal = Node->getOperand(3);
2444 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002445 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002446 EVT PtrVT = Addr.getValueType();
2447
2448 // Get the address of the containing word.
2449 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
2450 DAG.getConstant(-4, PtrVT));
2451
2452 // Get the number of bits that the word must be rotated left in order
2453 // to bring the field to the top bits of a GR32.
2454 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
2455 DAG.getConstant(3, PtrVT));
2456 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
2457
2458 // Get the complementing shift amount, for rotating a field in the top
2459 // bits back to its proper position.
2460 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
2461 DAG.getConstant(0, WideVT), BitShift);
2462
2463 // Construct the ATOMIC_CMP_SWAPW node.
2464 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
2465 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
2466 NegBitShift, DAG.getConstant(BitSize, WideVT) };
2467 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00002468 VTList, Ops, NarrowVT, MMO);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002469 return AtomicOp;
2470}
2471
2472SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
2473 SelectionDAG &DAG) const {
2474 MachineFunction &MF = DAG.getMachineFunction();
2475 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002476 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002477 SystemZ::R15D, Op.getValueType());
2478}
2479
2480SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
2481 SelectionDAG &DAG) const {
2482 MachineFunction &MF = DAG.getMachineFunction();
2483 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002484 return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002485 SystemZ::R15D, Op.getOperand(1));
2486}
2487
Richard Sandiford03481332013-08-23 11:36:42 +00002488SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
2489 SelectionDAG &DAG) const {
2490 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2491 if (!IsData)
2492 // Just preserve the chain.
2493 return Op.getOperand(0);
2494
2495 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2496 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
Richard Sandiford21f5d682014-03-06 11:22:58 +00002497 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
Richard Sandiford03481332013-08-23 11:36:42 +00002498 SDValue Ops[] = {
2499 Op.getOperand(0),
2500 DAG.getConstant(Code, MVT::i32),
2501 Op.getOperand(1)
2502 };
2503 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, SDLoc(Op),
Craig Topper206fcd42014-04-26 19:29:41 +00002504 Node->getVTList(), Ops,
Richard Sandiford03481332013-08-23 11:36:42 +00002505 Node->getMemoryVT(), Node->getMemOperand());
2506}
2507
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002508SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
2509 SelectionDAG &DAG) const {
2510 switch (Op.getOpcode()) {
2511 case ISD::BR_CC:
2512 return lowerBR_CC(Op, DAG);
2513 case ISD::SELECT_CC:
2514 return lowerSELECT_CC(Op, DAG);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002515 case ISD::SETCC:
2516 return lowerSETCC(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002517 case ISD::GlobalAddress:
2518 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
2519 case ISD::GlobalTLSAddress:
2520 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
2521 case ISD::BlockAddress:
2522 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
2523 case ISD::JumpTable:
2524 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
2525 case ISD::ConstantPool:
2526 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
2527 case ISD::BITCAST:
2528 return lowerBITCAST(Op, DAG);
2529 case ISD::VASTART:
2530 return lowerVASTART(Op, DAG);
2531 case ISD::VACOPY:
2532 return lowerVACOPY(Op, DAG);
2533 case ISD::DYNAMIC_STACKALLOC:
2534 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002535 case ISD::SMUL_LOHI:
2536 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002537 case ISD::UMUL_LOHI:
2538 return lowerUMUL_LOHI(Op, DAG);
2539 case ISD::SDIVREM:
2540 return lowerSDIVREM(Op, DAG);
2541 case ISD::UDIVREM:
2542 return lowerUDIVREM(Op, DAG);
2543 case ISD::OR:
2544 return lowerOR(Op, DAG);
2545 case ISD::ATOMIC_SWAP:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002546 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
2547 case ISD::ATOMIC_STORE:
2548 return lowerATOMIC_STORE(Op, DAG);
2549 case ISD::ATOMIC_LOAD:
2550 return lowerATOMIC_LOAD(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002551 case ISD::ATOMIC_LOAD_ADD:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002552 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002553 case ISD::ATOMIC_LOAD_SUB:
Richard Sandiford41350a52013-12-24 15:18:04 +00002554 return lowerATOMIC_LOAD_SUB(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002555 case ISD::ATOMIC_LOAD_AND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002556 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002557 case ISD::ATOMIC_LOAD_OR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002558 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002559 case ISD::ATOMIC_LOAD_XOR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002560 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002561 case ISD::ATOMIC_LOAD_NAND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002562 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002563 case ISD::ATOMIC_LOAD_MIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002564 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002565 case ISD::ATOMIC_LOAD_MAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002566 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002567 case ISD::ATOMIC_LOAD_UMIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002568 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002569 case ISD::ATOMIC_LOAD_UMAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002570 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002571 case ISD::ATOMIC_CMP_SWAP:
2572 return lowerATOMIC_CMP_SWAP(Op, DAG);
2573 case ISD::STACKSAVE:
2574 return lowerSTACKSAVE(Op, DAG);
2575 case ISD::STACKRESTORE:
2576 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00002577 case ISD::PREFETCH:
2578 return lowerPREFETCH(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002579 default:
2580 llvm_unreachable("Unexpected node to lower");
2581 }
2582}
2583
2584const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
2585#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
2586 switch (Opcode) {
2587 OPCODE(RET_FLAG);
2588 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00002589 OPCODE(SIBCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002590 OPCODE(PCREL_WRAPPER);
Richard Sandiford54b36912013-09-27 15:14:04 +00002591 OPCODE(PCREL_OFFSET);
Richard Sandiford57485472013-12-13 15:35:00 +00002592 OPCODE(IABS);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002593 OPCODE(ICMP);
2594 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00002595 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002596 OPCODE(BR_CCMASK);
2597 OPCODE(SELECT_CCMASK);
2598 OPCODE(ADJDYNALLOC);
2599 OPCODE(EXTRACT_ACCESS);
2600 OPCODE(UMUL_LOHI64);
2601 OPCODE(SDIVREM64);
2602 OPCODE(UDIVREM32);
2603 OPCODE(UDIVREM64);
Richard Sandifordd131ff82013-07-08 09:35:23 +00002604 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00002605 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00002606 OPCODE(NC);
2607 OPCODE(NC_LOOP);
2608 OPCODE(OC);
2609 OPCODE(OC_LOOP);
2610 OPCODE(XC);
2611 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00002612 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00002613 OPCODE(CLC_LOOP);
Richard Sandifordca232712013-08-16 11:21:54 +00002614 OPCODE(STRCMP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00002615 OPCODE(STPCPY);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00002616 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00002617 OPCODE(IPM);
Richard Sandiford9afe6132013-12-10 10:36:34 +00002618 OPCODE(SERIALIZE);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002619 OPCODE(ATOMIC_SWAPW);
2620 OPCODE(ATOMIC_LOADW_ADD);
2621 OPCODE(ATOMIC_LOADW_SUB);
2622 OPCODE(ATOMIC_LOADW_AND);
2623 OPCODE(ATOMIC_LOADW_OR);
2624 OPCODE(ATOMIC_LOADW_XOR);
2625 OPCODE(ATOMIC_LOADW_NAND);
2626 OPCODE(ATOMIC_LOADW_MIN);
2627 OPCODE(ATOMIC_LOADW_MAX);
2628 OPCODE(ATOMIC_LOADW_UMIN);
2629 OPCODE(ATOMIC_LOADW_UMAX);
2630 OPCODE(ATOMIC_CMP_SWAPW);
Richard Sandiford03481332013-08-23 11:36:42 +00002631 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002632 }
Craig Topper062a2ba2014-04-25 05:30:21 +00002633 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002634#undef OPCODE
2635}
2636
Richard Sandiford95bc5f92014-03-07 11:34:35 +00002637SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
2638 DAGCombinerInfo &DCI) const {
2639 SelectionDAG &DAG = DCI.DAG;
2640 unsigned Opcode = N->getOpcode();
2641 if (Opcode == ISD::SIGN_EXTEND) {
2642 // Convert (sext (ashr (shl X, C1), C2)) to
2643 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
2644 // cheap as narrower ones.
2645 SDValue N0 = N->getOperand(0);
2646 EVT VT = N->getValueType(0);
2647 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
2648 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2649 SDValue Inner = N0.getOperand(0);
2650 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
2651 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
2652 unsigned Extra = (VT.getSizeInBits() -
2653 N0.getValueType().getSizeInBits());
2654 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
2655 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
2656 EVT ShiftVT = N0.getOperand(1).getValueType();
2657 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
2658 Inner.getOperand(0));
2659 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
2660 DAG.getConstant(NewShlAmt, ShiftVT));
2661 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
2662 DAG.getConstant(NewSraAmt, ShiftVT));
2663 }
2664 }
2665 }
2666 }
2667 return SDValue();
2668}
2669
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002670//===----------------------------------------------------------------------===//
2671// Custom insertion
2672//===----------------------------------------------------------------------===//
2673
2674// Create a new basic block after MBB.
2675static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
2676 MachineFunction &MF = *MBB->getParent();
2677 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002678 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002679 return NewMBB;
2680}
2681
Richard Sandifordbe133a82013-08-28 09:01:51 +00002682// Split MBB after MI and return the new block (the one that contains
2683// instructions after MI).
2684static MachineBasicBlock *splitBlockAfter(MachineInstr *MI,
2685 MachineBasicBlock *MBB) {
2686 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
2687 NewMBB->splice(NewMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002688 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Richard Sandifordbe133a82013-08-28 09:01:51 +00002689 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
2690 return NewMBB;
2691}
2692
Richard Sandiford5e318f02013-08-27 09:54:29 +00002693// Split MBB before MI and return the new block (the one that contains MI).
2694static MachineBasicBlock *splitBlockBefore(MachineInstr *MI,
2695 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002696 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00002697 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002698 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
2699 return NewMBB;
2700}
2701
Richard Sandiford5e318f02013-08-27 09:54:29 +00002702// Force base value Base into a register before MI. Return the register.
2703static unsigned forceReg(MachineInstr *MI, MachineOperand &Base,
2704 const SystemZInstrInfo *TII) {
2705 if (Base.isReg())
2706 return Base.getReg();
2707
2708 MachineBasicBlock *MBB = MI->getParent();
2709 MachineFunction &MF = *MBB->getParent();
2710 MachineRegisterInfo &MRI = MF.getRegInfo();
2711
2712 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
2713 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg)
2714 .addOperand(Base).addImm(0).addReg(0);
2715 return Reg;
2716}
2717
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002718// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
2719MachineBasicBlock *
2720SystemZTargetLowering::emitSelect(MachineInstr *MI,
2721 MachineBasicBlock *MBB) const {
Eric Christophera6734172015-01-31 00:06:45 +00002722 const SystemZInstrInfo *TII =
2723 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002724
2725 unsigned DestReg = MI->getOperand(0).getReg();
2726 unsigned TrueReg = MI->getOperand(1).getReg();
2727 unsigned FalseReg = MI->getOperand(2).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00002728 unsigned CCValid = MI->getOperand(3).getImm();
2729 unsigned CCMask = MI->getOperand(4).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002730 DebugLoc DL = MI->getDebugLoc();
2731
2732 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002733 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002734 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
2735
2736 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00002737 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002738 // # fallthrough to FalseMBB
2739 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00002740 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2741 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002742 MBB->addSuccessor(JoinMBB);
2743 MBB->addSuccessor(FalseMBB);
2744
2745 // FalseMBB:
2746 // # fallthrough to JoinMBB
2747 MBB = FalseMBB;
2748 MBB->addSuccessor(JoinMBB);
2749
2750 // JoinMBB:
2751 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
2752 // ...
2753 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002754 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002755 .addReg(TrueReg).addMBB(StartMBB)
2756 .addReg(FalseReg).addMBB(FalseMBB);
2757
2758 MI->eraseFromParent();
2759 return JoinMBB;
2760}
2761
Richard Sandifordb86a8342013-06-27 09:27:40 +00002762// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
2763// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002764// happen when the condition is false rather than true. If a STORE ON
2765// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Richard Sandifordb86a8342013-06-27 09:27:40 +00002766MachineBasicBlock *
2767SystemZTargetLowering::emitCondStore(MachineInstr *MI,
2768 MachineBasicBlock *MBB,
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002769 unsigned StoreOpcode, unsigned STOCOpcode,
2770 bool Invert) const {
Eric Christophera6734172015-01-31 00:06:45 +00002771 const SystemZInstrInfo *TII =
2772 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordb86a8342013-06-27 09:27:40 +00002773
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002774 unsigned SrcReg = MI->getOperand(0).getReg();
2775 MachineOperand Base = MI->getOperand(1);
2776 int64_t Disp = MI->getOperand(2).getImm();
2777 unsigned IndexReg = MI->getOperand(3).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00002778 unsigned CCValid = MI->getOperand(4).getImm();
2779 unsigned CCMask = MI->getOperand(5).getImm();
Richard Sandifordb86a8342013-06-27 09:27:40 +00002780 DebugLoc DL = MI->getDebugLoc();
2781
2782 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
2783
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002784 // Use STOCOpcode if possible. We could use different store patterns in
2785 // order to avoid matching the index register, but the performance trade-offs
2786 // might be more complicated in that case.
Eric Christopher93bf97c2014-06-27 07:38:01 +00002787 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002788 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00002789 CCMask ^= CCValid;
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002790 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +00002791 .addReg(SrcReg).addOperand(Base).addImm(Disp)
2792 .addImm(CCValid).addImm(CCMask);
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002793 MI->eraseFromParent();
2794 return MBB;
2795 }
2796
Richard Sandifordb86a8342013-06-27 09:27:40 +00002797 // Get the condition needed to branch around the store.
2798 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00002799 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00002800
2801 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002802 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002803 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
2804
2805 // StartMBB:
2806 // BRC CCMask, JoinMBB
2807 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00002808 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00002809 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2810 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002811 MBB->addSuccessor(JoinMBB);
2812 MBB->addSuccessor(FalseMBB);
2813
2814 // FalseMBB:
2815 // store %SrcReg, %Disp(%Index,%Base)
2816 // # fallthrough to JoinMBB
2817 MBB = FalseMBB;
2818 BuildMI(MBB, DL, TII->get(StoreOpcode))
2819 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
2820 MBB->addSuccessor(JoinMBB);
2821
2822 MI->eraseFromParent();
2823 return JoinMBB;
2824}
2825
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002826// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
2827// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
2828// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
2829// BitSize is the width of the field in bits, or 0 if this is a partword
2830// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
2831// is one of the operands. Invert says whether the field should be
2832// inverted after performing BinOpcode (e.g. for NAND).
2833MachineBasicBlock *
2834SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI,
2835 MachineBasicBlock *MBB,
2836 unsigned BinOpcode,
2837 unsigned BitSize,
2838 bool Invert) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002839 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00002840 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00002841 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002842 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002843 bool IsSubWord = (BitSize < 32);
2844
2845 // Extract the operands. Base can be a register or a frame index.
2846 // Src2 can be a register or immediate.
2847 unsigned Dest = MI->getOperand(0).getReg();
2848 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2849 int64_t Disp = MI->getOperand(2).getImm();
2850 MachineOperand Src2 = earlyUseOperand(MI->getOperand(3));
2851 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
2852 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
2853 DebugLoc DL = MI->getDebugLoc();
2854 if (IsSubWord)
2855 BitSize = MI->getOperand(6).getImm();
2856
2857 // Subword operations use 32-bit registers.
2858 const TargetRegisterClass *RC = (BitSize <= 32 ?
2859 &SystemZ::GR32BitRegClass :
2860 &SystemZ::GR64BitRegClass);
2861 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
2862 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
2863
2864 // Get the right opcodes for the displacement.
2865 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
2866 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
2867 assert(LOpcode && CSOpcode && "Displacement out of range");
2868
2869 // Create virtual registers for temporary results.
2870 unsigned OrigVal = MRI.createVirtualRegister(RC);
2871 unsigned OldVal = MRI.createVirtualRegister(RC);
2872 unsigned NewVal = (BinOpcode || IsSubWord ?
2873 MRI.createVirtualRegister(RC) : Src2.getReg());
2874 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
2875 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
2876
2877 // Insert a basic block for the main loop.
2878 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002879 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002880 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2881
2882 // StartMBB:
2883 // ...
2884 // %OrigVal = L Disp(%Base)
2885 // # fall through to LoopMMB
2886 MBB = StartMBB;
2887 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
2888 .addOperand(Base).addImm(Disp).addReg(0);
2889 MBB->addSuccessor(LoopMBB);
2890
2891 // LoopMBB:
2892 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
2893 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
2894 // %RotatedNewVal = OP %RotatedOldVal, %Src2
2895 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
2896 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
2897 // JNE LoopMBB
2898 // # fall through to DoneMMB
2899 MBB = LoopMBB;
2900 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2901 .addReg(OrigVal).addMBB(StartMBB)
2902 .addReg(Dest).addMBB(LoopMBB);
2903 if (IsSubWord)
2904 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
2905 .addReg(OldVal).addReg(BitShift).addImm(0);
2906 if (Invert) {
2907 // Perform the operation normally and then invert every bit of the field.
2908 unsigned Tmp = MRI.createVirtualRegister(RC);
2909 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
2910 .addReg(RotatedOldVal).addOperand(Src2);
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00002911 if (BitSize <= 32)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002912 // XILF with the upper BitSize bits set.
Richard Sandiford652784e2013-09-25 11:11:53 +00002913 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00002914 .addReg(Tmp).addImm(-1U << (32 - BitSize));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002915 else {
2916 // Use LCGR and add -1 to the result, which is more compact than
2917 // an XILF, XILH pair.
2918 unsigned Tmp2 = MRI.createVirtualRegister(RC);
2919 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
2920 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
2921 .addReg(Tmp2).addImm(-1);
2922 }
2923 } else if (BinOpcode)
2924 // A simply binary operation.
2925 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
2926 .addReg(RotatedOldVal).addOperand(Src2);
2927 else if (IsSubWord)
2928 // Use RISBG to rotate Src2 into position and use it to replace the
2929 // field in RotatedOldVal.
2930 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
2931 .addReg(RotatedOldVal).addReg(Src2.getReg())
2932 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
2933 if (IsSubWord)
2934 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
2935 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
2936 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
2937 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00002938 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2939 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002940 MBB->addSuccessor(LoopMBB);
2941 MBB->addSuccessor(DoneMBB);
2942
2943 MI->eraseFromParent();
2944 return DoneMBB;
2945}
2946
2947// Implement EmitInstrWithCustomInserter for pseudo
2948// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
2949// instruction that should be used to compare the current field with the
2950// minimum or maximum value. KeepOldMask is the BRC condition-code mask
2951// for when the current field should be kept. BitSize is the width of
2952// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
2953MachineBasicBlock *
2954SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI,
2955 MachineBasicBlock *MBB,
2956 unsigned CompareOpcode,
2957 unsigned KeepOldMask,
2958 unsigned BitSize) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002959 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00002960 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00002961 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002962 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002963 bool IsSubWord = (BitSize < 32);
2964
2965 // Extract the operands. Base can be a register or a frame index.
2966 unsigned Dest = MI->getOperand(0).getReg();
2967 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2968 int64_t Disp = MI->getOperand(2).getImm();
2969 unsigned Src2 = MI->getOperand(3).getReg();
2970 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
2971 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
2972 DebugLoc DL = MI->getDebugLoc();
2973 if (IsSubWord)
2974 BitSize = MI->getOperand(6).getImm();
2975
2976 // Subword operations use 32-bit registers.
2977 const TargetRegisterClass *RC = (BitSize <= 32 ?
2978 &SystemZ::GR32BitRegClass :
2979 &SystemZ::GR64BitRegClass);
2980 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
2981 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
2982
2983 // Get the right opcodes for the displacement.
2984 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
2985 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
2986 assert(LOpcode && CSOpcode && "Displacement out of range");
2987
2988 // Create virtual registers for temporary results.
2989 unsigned OrigVal = MRI.createVirtualRegister(RC);
2990 unsigned OldVal = MRI.createVirtualRegister(RC);
2991 unsigned NewVal = MRI.createVirtualRegister(RC);
2992 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
2993 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
2994 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
2995
2996 // Insert 3 basic blocks for the loop.
2997 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002998 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002999 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
3000 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
3001 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
3002
3003 // StartMBB:
3004 // ...
3005 // %OrigVal = L Disp(%Base)
3006 // # fall through to LoopMMB
3007 MBB = StartMBB;
3008 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
3009 .addOperand(Base).addImm(Disp).addReg(0);
3010 MBB->addSuccessor(LoopMBB);
3011
3012 // LoopMBB:
3013 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
3014 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
3015 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00003016 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003017 MBB = LoopMBB;
3018 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
3019 .addReg(OrigVal).addMBB(StartMBB)
3020 .addReg(Dest).addMBB(UpdateMBB);
3021 if (IsSubWord)
3022 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
3023 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00003024 BuildMI(MBB, DL, TII->get(CompareOpcode))
3025 .addReg(RotatedOldVal).addReg(Src2);
3026 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00003027 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003028 MBB->addSuccessor(UpdateMBB);
3029 MBB->addSuccessor(UseAltMBB);
3030
3031 // UseAltMBB:
3032 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
3033 // # fall through to UpdateMMB
3034 MBB = UseAltMBB;
3035 if (IsSubWord)
3036 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
3037 .addReg(RotatedOldVal).addReg(Src2)
3038 .addImm(32).addImm(31 + BitSize).addImm(0);
3039 MBB->addSuccessor(UpdateMBB);
3040
3041 // UpdateMBB:
3042 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
3043 // [ %RotatedAltVal, UseAltMBB ]
3044 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
3045 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
3046 // JNE LoopMBB
3047 // # fall through to DoneMMB
3048 MBB = UpdateMBB;
3049 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
3050 .addReg(RotatedOldVal).addMBB(LoopMBB)
3051 .addReg(RotatedAltVal).addMBB(UseAltMBB);
3052 if (IsSubWord)
3053 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
3054 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
3055 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
3056 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00003057 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3058 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003059 MBB->addSuccessor(LoopMBB);
3060 MBB->addSuccessor(DoneMBB);
3061
3062 MI->eraseFromParent();
3063 return DoneMBB;
3064}
3065
3066// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
3067// instruction MI.
3068MachineBasicBlock *
3069SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI,
3070 MachineBasicBlock *MBB) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003071 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00003072 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00003073 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003074 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003075
3076 // Extract the operands. Base can be a register or a frame index.
3077 unsigned Dest = MI->getOperand(0).getReg();
3078 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
3079 int64_t Disp = MI->getOperand(2).getImm();
3080 unsigned OrigCmpVal = MI->getOperand(3).getReg();
3081 unsigned OrigSwapVal = MI->getOperand(4).getReg();
3082 unsigned BitShift = MI->getOperand(5).getReg();
3083 unsigned NegBitShift = MI->getOperand(6).getReg();
3084 int64_t BitSize = MI->getOperand(7).getImm();
3085 DebugLoc DL = MI->getDebugLoc();
3086
3087 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
3088
3089 // Get the right opcodes for the displacement.
3090 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
3091 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
3092 assert(LOpcode && CSOpcode && "Displacement out of range");
3093
3094 // Create virtual registers for temporary results.
3095 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
3096 unsigned OldVal = MRI.createVirtualRegister(RC);
3097 unsigned CmpVal = MRI.createVirtualRegister(RC);
3098 unsigned SwapVal = MRI.createVirtualRegister(RC);
3099 unsigned StoreVal = MRI.createVirtualRegister(RC);
3100 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
3101 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
3102 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
3103
3104 // Insert 2 basic blocks for the loop.
3105 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00003106 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003107 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
3108 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
3109
3110 // StartMBB:
3111 // ...
3112 // %OrigOldVal = L Disp(%Base)
3113 // # fall through to LoopMMB
3114 MBB = StartMBB;
3115 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
3116 .addOperand(Base).addImm(Disp).addReg(0);
3117 MBB->addSuccessor(LoopMBB);
3118
3119 // LoopMBB:
3120 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
3121 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
3122 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
3123 // %Dest = RLL %OldVal, BitSize(%BitShift)
3124 // ^^ The low BitSize bits contain the field
3125 // of interest.
3126 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
3127 // ^^ Replace the upper 32-BitSize bits of the
3128 // comparison value with those that we loaded,
3129 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00003130 // CR %Dest, %RetryCmpVal
3131 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003132 // # Fall through to SetMBB
3133 MBB = LoopMBB;
3134 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
3135 .addReg(OrigOldVal).addMBB(StartMBB)
3136 .addReg(RetryOldVal).addMBB(SetMBB);
3137 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
3138 .addReg(OrigCmpVal).addMBB(StartMBB)
3139 .addReg(RetryCmpVal).addMBB(SetMBB);
3140 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
3141 .addReg(OrigSwapVal).addMBB(StartMBB)
3142 .addReg(RetrySwapVal).addMBB(SetMBB);
3143 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
3144 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
3145 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
3146 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00003147 BuildMI(MBB, DL, TII->get(SystemZ::CR))
3148 .addReg(Dest).addReg(RetryCmpVal);
3149 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00003150 .addImm(SystemZ::CCMASK_ICMP)
3151 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003152 MBB->addSuccessor(DoneMBB);
3153 MBB->addSuccessor(SetMBB);
3154
3155 // SetMBB:
3156 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
3157 // ^^ Replace the upper 32-BitSize bits of the new
3158 // value with those that we loaded.
3159 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
3160 // ^^ Rotate the new field to its proper position.
3161 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
3162 // JNE LoopMBB
3163 // # fall through to ExitMMB
3164 MBB = SetMBB;
3165 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
3166 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
3167 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
3168 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
3169 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
3170 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00003171 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3172 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003173 MBB->addSuccessor(LoopMBB);
3174 MBB->addSuccessor(DoneMBB);
3175
3176 MI->eraseFromParent();
3177 return DoneMBB;
3178}
3179
3180// Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true
3181// if the high register of the GR128 value must be cleared or false if
Richard Sandiford87a44362013-09-30 10:28:35 +00003182// it's "don't care". SubReg is subreg_l32 when extending a GR32
3183// and subreg_l64 when extending a GR64.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003184MachineBasicBlock *
3185SystemZTargetLowering::emitExt128(MachineInstr *MI,
3186 MachineBasicBlock *MBB,
3187 bool ClearEven, unsigned SubReg) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003188 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00003189 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00003190 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003191 MachineRegisterInfo &MRI = MF.getRegInfo();
3192 DebugLoc DL = MI->getDebugLoc();
3193
3194 unsigned Dest = MI->getOperand(0).getReg();
3195 unsigned Src = MI->getOperand(1).getReg();
3196 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
3197
3198 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
3199 if (ClearEven) {
3200 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
3201 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
3202
3203 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
3204 .addImm(0);
3205 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
Richard Sandiford87a44362013-09-30 10:28:35 +00003206 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003207 In128 = NewIn128;
3208 }
3209 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
3210 .addReg(In128).addReg(Src).addImm(SubReg);
3211
3212 MI->eraseFromParent();
3213 return MBB;
3214}
3215
Richard Sandifordd131ff82013-07-08 09:35:23 +00003216MachineBasicBlock *
Richard Sandiford564681c2013-08-12 10:28:10 +00003217SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI,
3218 MachineBasicBlock *MBB,
3219 unsigned Opcode) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +00003220 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00003221 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00003222 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandiford5e318f02013-08-27 09:54:29 +00003223 MachineRegisterInfo &MRI = MF.getRegInfo();
Richard Sandifordd131ff82013-07-08 09:35:23 +00003224 DebugLoc DL = MI->getDebugLoc();
3225
Richard Sandiford5e318f02013-08-27 09:54:29 +00003226 MachineOperand DestBase = earlyUseOperand(MI->getOperand(0));
Richard Sandifordd131ff82013-07-08 09:35:23 +00003227 uint64_t DestDisp = MI->getOperand(1).getImm();
Richard Sandiford5e318f02013-08-27 09:54:29 +00003228 MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2));
Richard Sandifordd131ff82013-07-08 09:35:23 +00003229 uint64_t SrcDisp = MI->getOperand(3).getImm();
3230 uint64_t Length = MI->getOperand(4).getImm();
3231
Richard Sandifordbe133a82013-08-28 09:01:51 +00003232 // When generating more than one CLC, all but the last will need to
3233 // branch to the end when a difference is found.
3234 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
Craig Topper062a2ba2014-04-25 05:30:21 +00003235 splitBlockAfter(MI, MBB) : nullptr);
Richard Sandifordbe133a82013-08-28 09:01:51 +00003236
Richard Sandiford5e318f02013-08-27 09:54:29 +00003237 // Check for the loop form, in which operand 5 is the trip count.
3238 if (MI->getNumExplicitOperands() > 5) {
3239 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
3240
3241 uint64_t StartCountReg = MI->getOperand(5).getReg();
3242 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
3243 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
3244 forceReg(MI, DestBase, TII));
3245
3246 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
3247 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
3248 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
3249 MRI.createVirtualRegister(RC));
3250 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
3251 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
3252 MRI.createVirtualRegister(RC));
3253
3254 RC = &SystemZ::GR64BitRegClass;
3255 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
3256 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
3257
3258 MachineBasicBlock *StartMBB = MBB;
3259 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
3260 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00003261 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00003262
3263 // StartMBB:
3264 // # fall through to LoopMMB
3265 MBB->addSuccessor(LoopMBB);
3266
3267 // LoopMBB:
3268 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00003269 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00003270 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00003271 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00003272 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00003273 // [ %NextCountReg, NextMBB ]
3274 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00003275 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00003276 // ( JLH EndMBB )
3277 //
3278 // The prefetch is used only for MVC. The JLH is used only for CLC.
3279 MBB = LoopMBB;
3280
3281 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
3282 .addReg(StartDestReg).addMBB(StartMBB)
3283 .addReg(NextDestReg).addMBB(NextMBB);
3284 if (!HaveSingleBase)
3285 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
3286 .addReg(StartSrcReg).addMBB(StartMBB)
3287 .addReg(NextSrcReg).addMBB(NextMBB);
3288 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
3289 .addReg(StartCountReg).addMBB(StartMBB)
3290 .addReg(NextCountReg).addMBB(NextMBB);
3291 if (Opcode == SystemZ::MVC)
3292 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
3293 .addImm(SystemZ::PFD_WRITE)
3294 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
3295 BuildMI(MBB, DL, TII->get(Opcode))
3296 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
3297 .addReg(ThisSrcReg).addImm(SrcDisp);
3298 if (EndMBB) {
3299 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3300 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
3301 .addMBB(EndMBB);
3302 MBB->addSuccessor(EndMBB);
3303 MBB->addSuccessor(NextMBB);
3304 }
3305
3306 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00003307 // %NextDestReg = LA 256(%ThisDestReg)
3308 // %NextSrcReg = LA 256(%ThisSrcReg)
3309 // %NextCountReg = AGHI %ThisCountReg, -1
3310 // CGHI %NextCountReg, 0
3311 // JLH LoopMBB
3312 // # fall through to DoneMMB
3313 //
3314 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00003315 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00003316
Richard Sandiford5e318f02013-08-27 09:54:29 +00003317 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
3318 .addReg(ThisDestReg).addImm(256).addReg(0);
3319 if (!HaveSingleBase)
3320 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
3321 .addReg(ThisSrcReg).addImm(256).addReg(0);
3322 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
3323 .addReg(ThisCountReg).addImm(-1);
3324 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
3325 .addReg(NextCountReg).addImm(0);
3326 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3327 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
3328 .addMBB(LoopMBB);
3329 MBB->addSuccessor(LoopMBB);
3330 MBB->addSuccessor(DoneMBB);
3331
3332 DestBase = MachineOperand::CreateReg(NextDestReg, false);
3333 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
3334 Length &= 255;
3335 MBB = DoneMBB;
3336 }
3337 // Handle any remaining bytes with straight-line code.
3338 while (Length > 0) {
3339 uint64_t ThisLength = std::min(Length, uint64_t(256));
3340 // The previous iteration might have created out-of-range displacements.
3341 // Apply them using LAY if so.
3342 if (!isUInt<12>(DestDisp)) {
3343 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
3344 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
3345 .addOperand(DestBase).addImm(DestDisp).addReg(0);
3346 DestBase = MachineOperand::CreateReg(Reg, false);
3347 DestDisp = 0;
3348 }
3349 if (!isUInt<12>(SrcDisp)) {
3350 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
3351 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
3352 .addOperand(SrcBase).addImm(SrcDisp).addReg(0);
3353 SrcBase = MachineOperand::CreateReg(Reg, false);
3354 SrcDisp = 0;
3355 }
3356 BuildMI(*MBB, MI, DL, TII->get(Opcode))
3357 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength)
3358 .addOperand(SrcBase).addImm(SrcDisp);
3359 DestDisp += ThisLength;
3360 SrcDisp += ThisLength;
3361 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00003362 // If there's another CLC to go, branch to the end if a difference
3363 // was found.
3364 if (EndMBB && Length > 0) {
3365 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
3366 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3367 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
3368 .addMBB(EndMBB);
3369 MBB->addSuccessor(EndMBB);
3370 MBB->addSuccessor(NextMBB);
3371 MBB = NextMBB;
3372 }
3373 }
3374 if (EndMBB) {
3375 MBB->addSuccessor(EndMBB);
3376 MBB = EndMBB;
3377 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00003378 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00003379
3380 MI->eraseFromParent();
3381 return MBB;
3382}
3383
Richard Sandifordca232712013-08-16 11:21:54 +00003384// Decompose string pseudo-instruction MI into a loop that continually performs
3385// Opcode until CC != 3.
3386MachineBasicBlock *
3387SystemZTargetLowering::emitStringWrapper(MachineInstr *MI,
3388 MachineBasicBlock *MBB,
3389 unsigned Opcode) const {
Richard Sandifordca232712013-08-16 11:21:54 +00003390 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00003391 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00003392 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordca232712013-08-16 11:21:54 +00003393 MachineRegisterInfo &MRI = MF.getRegInfo();
3394 DebugLoc DL = MI->getDebugLoc();
3395
3396 uint64_t End1Reg = MI->getOperand(0).getReg();
3397 uint64_t Start1Reg = MI->getOperand(1).getReg();
3398 uint64_t Start2Reg = MI->getOperand(2).getReg();
3399 uint64_t CharReg = MI->getOperand(3).getReg();
3400
3401 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
3402 uint64_t This1Reg = MRI.createVirtualRegister(RC);
3403 uint64_t This2Reg = MRI.createVirtualRegister(RC);
3404 uint64_t End2Reg = MRI.createVirtualRegister(RC);
3405
3406 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00003407 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00003408 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
3409
3410 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00003411 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00003412 MBB->addSuccessor(LoopMBB);
3413
3414 // LoopMBB:
3415 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
3416 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford7789b082013-09-30 08:48:38 +00003417 // R0L = %CharReg
3418 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
Richard Sandifordca232712013-08-16 11:21:54 +00003419 // JO LoopMBB
3420 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00003421 //
Richard Sandiford7789b082013-09-30 08:48:38 +00003422 // The load of R0L can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00003423 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00003424
3425 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
3426 .addReg(Start1Reg).addMBB(StartMBB)
3427 .addReg(End1Reg).addMBB(LoopMBB);
3428 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
3429 .addReg(Start2Reg).addMBB(StartMBB)
3430 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford7789b082013-09-30 08:48:38 +00003431 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00003432 BuildMI(MBB, DL, TII->get(Opcode))
3433 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
3434 .addReg(This1Reg).addReg(This2Reg);
3435 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3436 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
3437 MBB->addSuccessor(LoopMBB);
3438 MBB->addSuccessor(DoneMBB);
3439
3440 DoneMBB->addLiveIn(SystemZ::CC);
3441
3442 MI->eraseFromParent();
3443 return DoneMBB;
3444}
3445
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003446MachineBasicBlock *SystemZTargetLowering::
3447EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const {
3448 switch (MI->getOpcode()) {
Richard Sandiford7c5c0ea2013-10-01 13:10:16 +00003449 case SystemZ::Select32Mux:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003450 case SystemZ::Select32:
3451 case SystemZ::SelectF32:
3452 case SystemZ::Select64:
3453 case SystemZ::SelectF64:
3454 case SystemZ::SelectF128:
3455 return emitSelect(MI, MBB);
3456
Richard Sandiford2896d042013-10-01 14:33:55 +00003457 case SystemZ::CondStore8Mux:
3458 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
3459 case SystemZ::CondStore8MuxInv:
3460 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
3461 case SystemZ::CondStore16Mux:
3462 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
3463 case SystemZ::CondStore16MuxInv:
3464 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003465 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003466 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003467 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003468 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003469 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003470 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003471 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003472 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003473 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003474 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003475 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003476 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003477 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003478 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003479 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003480 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003481 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003482 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003483 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003484 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003485 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003486 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003487 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003488 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003489
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003490 case SystemZ::AEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00003491 return emitExt128(MI, MBB, false, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003492 case SystemZ::ZEXT128_32:
Richard Sandiford87a44362013-09-30 10:28:35 +00003493 return emitExt128(MI, MBB, true, SystemZ::subreg_l32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003494 case SystemZ::ZEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00003495 return emitExt128(MI, MBB, true, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003496
3497 case SystemZ::ATOMIC_SWAPW:
3498 return emitAtomicLoadBinary(MI, MBB, 0, 0);
3499 case SystemZ::ATOMIC_SWAP_32:
3500 return emitAtomicLoadBinary(MI, MBB, 0, 32);
3501 case SystemZ::ATOMIC_SWAP_64:
3502 return emitAtomicLoadBinary(MI, MBB, 0, 64);
3503
3504 case SystemZ::ATOMIC_LOADW_AR:
3505 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
3506 case SystemZ::ATOMIC_LOADW_AFI:
3507 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
3508 case SystemZ::ATOMIC_LOAD_AR:
3509 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
3510 case SystemZ::ATOMIC_LOAD_AHI:
3511 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
3512 case SystemZ::ATOMIC_LOAD_AFI:
3513 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
3514 case SystemZ::ATOMIC_LOAD_AGR:
3515 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
3516 case SystemZ::ATOMIC_LOAD_AGHI:
3517 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
3518 case SystemZ::ATOMIC_LOAD_AGFI:
3519 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
3520
3521 case SystemZ::ATOMIC_LOADW_SR:
3522 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
3523 case SystemZ::ATOMIC_LOAD_SR:
3524 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
3525 case SystemZ::ATOMIC_LOAD_SGR:
3526 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
3527
3528 case SystemZ::ATOMIC_LOADW_NR:
3529 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
3530 case SystemZ::ATOMIC_LOADW_NILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00003531 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003532 case SystemZ::ATOMIC_LOAD_NR:
3533 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00003534 case SystemZ::ATOMIC_LOAD_NILL:
3535 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
3536 case SystemZ::ATOMIC_LOAD_NILH:
3537 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
3538 case SystemZ::ATOMIC_LOAD_NILF:
3539 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003540 case SystemZ::ATOMIC_LOAD_NGR:
3541 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00003542 case SystemZ::ATOMIC_LOAD_NILL64:
3543 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
3544 case SystemZ::ATOMIC_LOAD_NILH64:
3545 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00003546 case SystemZ::ATOMIC_LOAD_NIHL64:
3547 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
3548 case SystemZ::ATOMIC_LOAD_NIHH64:
3549 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00003550 case SystemZ::ATOMIC_LOAD_NILF64:
3551 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00003552 case SystemZ::ATOMIC_LOAD_NIHF64:
3553 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003554
3555 case SystemZ::ATOMIC_LOADW_OR:
3556 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
3557 case SystemZ::ATOMIC_LOADW_OILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00003558 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003559 case SystemZ::ATOMIC_LOAD_OR:
3560 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00003561 case SystemZ::ATOMIC_LOAD_OILL:
3562 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
3563 case SystemZ::ATOMIC_LOAD_OILH:
3564 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
3565 case SystemZ::ATOMIC_LOAD_OILF:
3566 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003567 case SystemZ::ATOMIC_LOAD_OGR:
3568 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00003569 case SystemZ::ATOMIC_LOAD_OILL64:
3570 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
3571 case SystemZ::ATOMIC_LOAD_OILH64:
3572 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00003573 case SystemZ::ATOMIC_LOAD_OIHL64:
3574 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
3575 case SystemZ::ATOMIC_LOAD_OIHH64:
3576 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00003577 case SystemZ::ATOMIC_LOAD_OILF64:
3578 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00003579 case SystemZ::ATOMIC_LOAD_OIHF64:
3580 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003581
3582 case SystemZ::ATOMIC_LOADW_XR:
3583 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
3584 case SystemZ::ATOMIC_LOADW_XILF:
Richard Sandiford652784e2013-09-25 11:11:53 +00003585 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003586 case SystemZ::ATOMIC_LOAD_XR:
3587 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00003588 case SystemZ::ATOMIC_LOAD_XILF:
3589 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003590 case SystemZ::ATOMIC_LOAD_XGR:
3591 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00003592 case SystemZ::ATOMIC_LOAD_XILF64:
3593 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
Richard Sandiford5718dac2013-10-01 14:08:44 +00003594 case SystemZ::ATOMIC_LOAD_XIHF64:
3595 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003596
3597 case SystemZ::ATOMIC_LOADW_NRi:
3598 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
3599 case SystemZ::ATOMIC_LOADW_NILHi:
Richard Sandiford652784e2013-09-25 11:11:53 +00003600 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003601 case SystemZ::ATOMIC_LOAD_NRi:
3602 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00003603 case SystemZ::ATOMIC_LOAD_NILLi:
3604 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
3605 case SystemZ::ATOMIC_LOAD_NILHi:
3606 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
3607 case SystemZ::ATOMIC_LOAD_NILFi:
3608 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003609 case SystemZ::ATOMIC_LOAD_NGRi:
3610 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00003611 case SystemZ::ATOMIC_LOAD_NILL64i:
3612 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
3613 case SystemZ::ATOMIC_LOAD_NILH64i:
3614 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00003615 case SystemZ::ATOMIC_LOAD_NIHL64i:
3616 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
3617 case SystemZ::ATOMIC_LOAD_NIHH64i:
3618 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00003619 case SystemZ::ATOMIC_LOAD_NILF64i:
3620 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00003621 case SystemZ::ATOMIC_LOAD_NIHF64i:
3622 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003623
3624 case SystemZ::ATOMIC_LOADW_MIN:
3625 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3626 SystemZ::CCMASK_CMP_LE, 0);
3627 case SystemZ::ATOMIC_LOAD_MIN_32:
3628 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3629 SystemZ::CCMASK_CMP_LE, 32);
3630 case SystemZ::ATOMIC_LOAD_MIN_64:
3631 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
3632 SystemZ::CCMASK_CMP_LE, 64);
3633
3634 case SystemZ::ATOMIC_LOADW_MAX:
3635 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3636 SystemZ::CCMASK_CMP_GE, 0);
3637 case SystemZ::ATOMIC_LOAD_MAX_32:
3638 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3639 SystemZ::CCMASK_CMP_GE, 32);
3640 case SystemZ::ATOMIC_LOAD_MAX_64:
3641 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
3642 SystemZ::CCMASK_CMP_GE, 64);
3643
3644 case SystemZ::ATOMIC_LOADW_UMIN:
3645 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3646 SystemZ::CCMASK_CMP_LE, 0);
3647 case SystemZ::ATOMIC_LOAD_UMIN_32:
3648 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3649 SystemZ::CCMASK_CMP_LE, 32);
3650 case SystemZ::ATOMIC_LOAD_UMIN_64:
3651 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
3652 SystemZ::CCMASK_CMP_LE, 64);
3653
3654 case SystemZ::ATOMIC_LOADW_UMAX:
3655 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3656 SystemZ::CCMASK_CMP_GE, 0);
3657 case SystemZ::ATOMIC_LOAD_UMAX_32:
3658 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3659 SystemZ::CCMASK_CMP_GE, 32);
3660 case SystemZ::ATOMIC_LOAD_UMAX_64:
3661 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
3662 SystemZ::CCMASK_CMP_GE, 64);
3663
3664 case SystemZ::ATOMIC_CMP_SWAPW:
3665 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00003666 case SystemZ::MVCSequence:
3667 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00003668 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00003669 case SystemZ::NCSequence:
3670 case SystemZ::NCLoop:
3671 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
3672 case SystemZ::OCSequence:
3673 case SystemZ::OCLoop:
3674 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
3675 case SystemZ::XCSequence:
3676 case SystemZ::XCLoop:
3677 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00003678 case SystemZ::CLCSequence:
3679 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00003680 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00003681 case SystemZ::CLSTLoop:
3682 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00003683 case SystemZ::MVSTLoop:
3684 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00003685 case SystemZ::SRSTLoop:
3686 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003687 default:
3688 llvm_unreachable("Unexpected instr type to insert");
3689 }
3690}