blob: d92ad6a0054508d3e7dd3e7aa0dc4c162ef3e42f [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
99 computeRegisterProperties();
100
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
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000502std::pair<unsigned, const TargetRegisterClass *> SystemZTargetLowering::
Chad Rosier295bd432013-06-22 18:37:38 +0000503getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000504 if (Constraint.size() == 1) {
505 // GCC Constraint Letters
506 switch (Constraint[0]) {
507 default: break;
508 case 'd': // Data register (equivalent to 'r')
509 case 'r': // General-purpose register
510 if (VT == MVT::i64)
511 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
512 else if (VT == MVT::i128)
513 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
514 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
515
516 case 'a': // Address register
517 if (VT == MVT::i64)
518 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
519 else if (VT == MVT::i128)
520 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
521 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
522
Richard Sandiford0755c932013-10-01 11:26:28 +0000523 case 'h': // High-part register (an LLVM extension)
524 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
525
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000526 case 'f': // Floating-point register
527 if (VT == MVT::f64)
528 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
529 else if (VT == MVT::f128)
530 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
531 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
532 }
533 }
Richard Sandifordb8204052013-07-12 09:08:12 +0000534 if (Constraint[0] == '{') {
535 // We need to override the default register parsing for GPRs and FPRs
536 // because the interpretation depends on VT. The internal names of
537 // the registers are also different from the external names
538 // (F0D and F0S instead of F0, etc.).
539 if (Constraint[1] == 'r') {
540 if (VT == MVT::i32)
541 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
542 SystemZMC::GR32Regs);
543 if (VT == MVT::i128)
544 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
545 SystemZMC::GR128Regs);
546 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
547 SystemZMC::GR64Regs);
548 }
549 if (Constraint[1] == 'f') {
550 if (VT == MVT::f32)
551 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
552 SystemZMC::FP32Regs);
553 if (VT == MVT::f128)
554 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
555 SystemZMC::FP128Regs);
556 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
557 SystemZMC::FP64Regs);
558 }
559 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000560 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
561}
562
563void SystemZTargetLowering::
564LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
565 std::vector<SDValue> &Ops,
566 SelectionDAG &DAG) const {
567 // Only support length 1 constraints for now.
568 if (Constraint.length() == 1) {
569 switch (Constraint[0]) {
570 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000571 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000572 if (isUInt<8>(C->getZExtValue()))
573 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
574 Op.getValueType()));
575 return;
576
577 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000578 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000579 if (isUInt<12>(C->getZExtValue()))
580 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
581 Op.getValueType()));
582 return;
583
584 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000585 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000586 if (isInt<16>(C->getSExtValue()))
587 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
588 Op.getValueType()));
589 return;
590
591 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000592 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000593 if (isInt<20>(C->getSExtValue()))
594 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
595 Op.getValueType()));
596 return;
597
598 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000599 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000600 if (C->getZExtValue() == 0x7fffffff)
601 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
602 Op.getValueType()));
603 return;
604 }
605 }
606 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
607}
608
609//===----------------------------------------------------------------------===//
610// Calling conventions
611//===----------------------------------------------------------------------===//
612
613#include "SystemZGenCallingConv.inc"
614
Richard Sandiford709bda62013-08-19 12:42:31 +0000615bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
616 Type *ToType) const {
617 return isTruncateFree(FromType, ToType);
618}
619
620bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
621 if (!CI->isTailCall())
622 return false;
623 return true;
624}
625
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000626// Value is a value that has been passed to us in the location described by VA
627// (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
628// any loads onto Chain.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000629static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000630 CCValAssign &VA, SDValue Chain,
631 SDValue Value) {
632 // If the argument has been promoted from a smaller type, insert an
633 // assertion to capture this.
634 if (VA.getLocInfo() == CCValAssign::SExt)
635 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
636 DAG.getValueType(VA.getValVT()));
637 else if (VA.getLocInfo() == CCValAssign::ZExt)
638 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
639 DAG.getValueType(VA.getValVT()));
640
641 if (VA.isExtInLoc())
642 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
643 else if (VA.getLocInfo() == CCValAssign::Indirect)
644 Value = DAG.getLoad(VA.getValVT(), DL, Chain, Value,
645 MachinePointerInfo(), false, false, false, 0);
646 else
647 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
648 return Value;
649}
650
651// Value is a value of type VA.getValVT() that we need to copy into
652// the location described by VA. Return a copy of Value converted to
653// VA.getValVT(). The caller is responsible for handling indirect values.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000654static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000655 CCValAssign &VA, SDValue Value) {
656 switch (VA.getLocInfo()) {
657 case CCValAssign::SExt:
658 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
659 case CCValAssign::ZExt:
660 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
661 case CCValAssign::AExt:
662 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
663 case CCValAssign::Full:
664 return Value;
665 default:
666 llvm_unreachable("Unhandled getLocInfo()");
667 }
668}
669
670SDValue SystemZTargetLowering::
671LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
672 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000673 SDLoc DL, SelectionDAG &DAG,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000674 SmallVectorImpl<SDValue> &InVals) const {
675 MachineFunction &MF = DAG.getMachineFunction();
676 MachineFrameInfo *MFI = MF.getFrameInfo();
677 MachineRegisterInfo &MRI = MF.getRegInfo();
678 SystemZMachineFunctionInfo *FuncInfo =
Eric Christophera6734172015-01-31 00:06:45 +0000679 MF.getInfo<SystemZMachineFunctionInfo>();
680 auto *TFL =
681 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000682
683 // Assign locations to all of the incoming arguments.
684 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000685 CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000686 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
687
688 unsigned NumFixedGPRs = 0;
689 unsigned NumFixedFPRs = 0;
690 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
691 SDValue ArgValue;
692 CCValAssign &VA = ArgLocs[I];
693 EVT LocVT = VA.getLocVT();
694 if (VA.isRegLoc()) {
695 // Arguments passed in registers
696 const TargetRegisterClass *RC;
697 switch (LocVT.getSimpleVT().SimpleTy) {
698 default:
699 // Integers smaller than i64 should be promoted to i64.
700 llvm_unreachable("Unexpected argument type");
701 case MVT::i32:
702 NumFixedGPRs += 1;
703 RC = &SystemZ::GR32BitRegClass;
704 break;
705 case MVT::i64:
706 NumFixedGPRs += 1;
707 RC = &SystemZ::GR64BitRegClass;
708 break;
709 case MVT::f32:
710 NumFixedFPRs += 1;
711 RC = &SystemZ::FP32BitRegClass;
712 break;
713 case MVT::f64:
714 NumFixedFPRs += 1;
715 RC = &SystemZ::FP64BitRegClass;
716 break;
717 }
718
719 unsigned VReg = MRI.createVirtualRegister(RC);
720 MRI.addLiveIn(VA.getLocReg(), VReg);
721 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
722 } else {
723 assert(VA.isMemLoc() && "Argument not register or memory");
724
725 // Create the frame index object for this incoming parameter.
726 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
727 VA.getLocMemOffset(), true);
728
729 // Create the SelectionDAG nodes corresponding to a load
730 // from this parameter. Unpromoted ints and floats are
731 // passed as right-justified 8-byte values.
732 EVT PtrVT = getPointerTy();
733 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
734 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
735 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, DAG.getIntPtrConstant(4));
736 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
737 MachinePointerInfo::getFixedStack(FI),
738 false, false, false, 0);
739 }
740
741 // Convert the value of the argument register into the value that's
742 // being passed.
743 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
744 }
745
746 if (IsVarArg) {
747 // Save the number of non-varargs registers for later use by va_start, etc.
748 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
749 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
750
751 // Likewise the address (in the form of a frame index) of where the
752 // first stack vararg would be. The 1-byte size here is arbitrary.
753 int64_t StackSize = CCInfo.getNextStackOffset();
754 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true));
755
756 // ...and a similar frame index for the caller-allocated save area
757 // that will be used to store the incoming registers.
758 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
759 unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true);
760 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
761
762 // Store the FPR varargs in the reserved frame slots. (We store the
763 // GPRs as part of the prologue.)
764 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
765 SDValue MemOps[SystemZ::NumArgFPRs];
766 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
767 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
768 int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true);
769 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
770 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
771 &SystemZ::FP64BitRegClass);
772 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
773 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
774 MachinePointerInfo::getFixedStack(FI),
775 false, false, 0);
776
777 }
778 // Join the stores, which are independent of one another.
779 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Craig Topper2d2aa0c2014-04-30 07:17:30 +0000780 makeArrayRef(&MemOps[NumFixedFPRs],
781 SystemZ::NumArgFPRs-NumFixedFPRs));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000782 }
783 }
784
785 return Chain;
786}
787
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000788static bool canUseSiblingCall(const CCState &ArgCCInfo,
Richard Sandiford709bda62013-08-19 12:42:31 +0000789 SmallVectorImpl<CCValAssign> &ArgLocs) {
790 // Punt if there are any indirect or stack arguments, or if the call
791 // needs the call-saved argument register R6.
792 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
793 CCValAssign &VA = ArgLocs[I];
794 if (VA.getLocInfo() == CCValAssign::Indirect)
795 return false;
796 if (!VA.isRegLoc())
797 return false;
798 unsigned Reg = VA.getLocReg();
Richard Sandiford0755c932013-10-01 11:26:28 +0000799 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
Richard Sandiford709bda62013-08-19 12:42:31 +0000800 return false;
801 }
802 return true;
803}
804
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000805SDValue
806SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
807 SmallVectorImpl<SDValue> &InVals) const {
808 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000809 SDLoc &DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000810 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
811 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
812 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000813 SDValue Chain = CLI.Chain;
814 SDValue Callee = CLI.Callee;
Richard Sandiford709bda62013-08-19 12:42:31 +0000815 bool &IsTailCall = CLI.IsTailCall;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000816 CallingConv::ID CallConv = CLI.CallConv;
817 bool IsVarArg = CLI.IsVarArg;
818 MachineFunction &MF = DAG.getMachineFunction();
819 EVT PtrVT = getPointerTy();
820
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000821 // Analyze the operands of the call, assigning locations to each operand.
822 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000823 CCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000824 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
825
Richard Sandiford709bda62013-08-19 12:42:31 +0000826 // We don't support GuaranteedTailCallOpt, only automatically-detected
827 // sibling calls.
828 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs))
829 IsTailCall = false;
830
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000831 // Get a count of how many bytes are to be pushed on the stack.
832 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
833
834 // Mark the start of the call.
Richard Sandiford709bda62013-08-19 12:42:31 +0000835 if (!IsTailCall)
836 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumBytes, PtrVT, true),
837 DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000838
839 // Copy argument values to their designated locations.
840 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
841 SmallVector<SDValue, 8> MemOpChains;
842 SDValue StackPtr;
843 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
844 CCValAssign &VA = ArgLocs[I];
845 SDValue ArgValue = OutVals[I];
846
847 if (VA.getLocInfo() == CCValAssign::Indirect) {
848 // Store the argument in a stack slot and pass its address.
849 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
850 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
851 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, SpillSlot,
852 MachinePointerInfo::getFixedStack(FI),
853 false, false, 0));
854 ArgValue = SpillSlot;
855 } else
856 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
857
858 if (VA.isRegLoc())
859 // Queue up the argument copies and emit them at the end.
860 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
861 else {
862 assert(VA.isMemLoc() && "Argument not register or memory");
863
864 // Work out the address of the stack slot. Unpromoted ints and
865 // floats are passed as right-justified 8-byte values.
866 if (!StackPtr.getNode())
867 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
868 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
869 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
870 Offset += 4;
871 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
872 DAG.getIntPtrConstant(Offset));
873
874 // Emit the store.
875 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address,
876 MachinePointerInfo(),
877 false, false, 0));
878 }
879 }
880
881 // Join the stores, which are independent of one another.
882 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000883 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000884
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000885 // Accept direct calls by converting symbolic call addresses to the
Richard Sandiford709bda62013-08-19 12:42:31 +0000886 // associated Target* opcodes. Force %r1 to be used for indirect
887 // tail calls.
888 SDValue Glue;
Richard Sandiford21f5d682014-03-06 11:22:58 +0000889 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000890 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
891 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford21f5d682014-03-06 11:22:58 +0000892 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000893 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
894 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford709bda62013-08-19 12:42:31 +0000895 } else if (IsTailCall) {
896 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
897 Glue = Chain.getValue(1);
898 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
899 }
900
901 // Build a sequence of copy-to-reg nodes, chained and glued together.
902 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
903 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
904 RegsToPass[I].second, Glue);
905 Glue = Chain.getValue(1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000906 }
907
908 // The first call operand is the chain and the second is the target address.
909 SmallVector<SDValue, 8> Ops;
910 Ops.push_back(Chain);
911 Ops.push_back(Callee);
912
913 // Add argument registers to the end of the list so that they are
914 // known live into the call.
915 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
916 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
917 RegsToPass[I].second.getValueType()));
918
Richard Sandiford02bb0ec2014-07-10 11:44:37 +0000919 // Add a register mask operand representing the call-preserved registers.
Eric Christophera6734172015-01-31 00:06:45 +0000920 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Richard Sandiford02bb0ec2014-07-10 11:44:37 +0000921 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
922 assert(Mask && "Missing call preserved mask for calling convention");
923 Ops.push_back(DAG.getRegisterMask(Mask));
924
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000925 // Glue the call to the argument copies, if any.
926 if (Glue.getNode())
927 Ops.push_back(Glue);
928
929 // Emit the call.
930 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Richard Sandiford709bda62013-08-19 12:42:31 +0000931 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +0000932 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
933 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000934 Glue = Chain.getValue(1);
935
936 // Mark the end of the call, which is glued to the call itself.
937 Chain = DAG.getCALLSEQ_END(Chain,
938 DAG.getConstant(NumBytes, PtrVT, true),
939 DAG.getConstant(0, PtrVT, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +0000940 Glue, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000941 Glue = Chain.getValue(1);
942
943 // Assign locations to each value returned by this call.
944 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000945 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000946 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
947
948 // Copy all of the result registers out of their specified physreg.
949 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
950 CCValAssign &VA = RetLocs[I];
951
952 // Copy the value out, gluing the copy to the end of the call sequence.
953 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
954 VA.getLocVT(), Glue);
955 Chain = RetValue.getValue(1);
956 Glue = RetValue.getValue(2);
957
958 // Convert the value of the return register into the value that's
959 // being returned.
960 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
961 }
962
963 return Chain;
964}
965
966SDValue
967SystemZTargetLowering::LowerReturn(SDValue Chain,
968 CallingConv::ID CallConv, bool IsVarArg,
969 const SmallVectorImpl<ISD::OutputArg> &Outs,
970 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000971 SDLoc DL, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000972 MachineFunction &MF = DAG.getMachineFunction();
973
974 // Assign locations to each returned value.
975 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000976 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000977 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
978
979 // Quick exit for void returns
980 if (RetLocs.empty())
981 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
982
983 // Copy the result values into the output registers.
984 SDValue Glue;
985 SmallVector<SDValue, 4> RetOps;
986 RetOps.push_back(Chain);
987 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
988 CCValAssign &VA = RetLocs[I];
989 SDValue RetValue = OutVals[I];
990
991 // Make the return register live on exit.
992 assert(VA.isRegLoc() && "Can only return in registers!");
993
994 // Promote the value as required.
995 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
996
997 // Chain and glue the copies together.
998 unsigned Reg = VA.getLocReg();
999 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
1000 Glue = Chain.getValue(1);
1001 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
1002 }
1003
1004 // Update chain and glue.
1005 RetOps[0] = Chain;
1006 if (Glue.getNode())
1007 RetOps.push_back(Glue);
1008
Craig Topper48d114b2014-04-26 18:35:24 +00001009 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001010}
1011
Richard Sandiford9afe6132013-12-10 10:36:34 +00001012SDValue SystemZTargetLowering::
1013prepareVolatileOrAtomicLoad(SDValue Chain, SDLoc DL, SelectionDAG &DAG) const {
1014 return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain);
1015}
1016
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001017// CC is a comparison that will be implemented using an integer or
1018// floating-point comparison. Return the condition code mask for
1019// a branch on true. In the integer case, CCMASK_CMP_UO is set for
1020// unsigned comparisons and clear for signed ones. In the floating-point
1021// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1022static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1023#define CONV(X) \
1024 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1025 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1026 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1027
1028 switch (CC) {
1029 default:
1030 llvm_unreachable("Invalid integer condition!");
1031
1032 CONV(EQ);
1033 CONV(NE);
1034 CONV(GT);
1035 CONV(GE);
1036 CONV(LT);
1037 CONV(LE);
1038
1039 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1040 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1041 }
1042#undef CONV
1043}
1044
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001045// Return a sequence for getting a 1 from an IPM result when CC has a
1046// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1047// The handling of CC values outside CCValid doesn't matter.
1048static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1049 // Deal with cases where the result can be taken directly from a bit
1050 // of the IPM result.
1051 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1052 return IPMConversion(0, 0, SystemZ::IPM_CC);
1053 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1054 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1055
1056 // Deal with cases where we can add a value to force the sign bit
1057 // to contain the right value. Putting the bit in 31 means we can
1058 // use SRL rather than RISBG(L), and also makes it easier to get a
1059 // 0/-1 value, so it has priority over the other tests below.
1060 //
1061 // These sequences rely on the fact that the upper two bits of the
1062 // IPM result are zero.
1063 uint64_t TopBit = uint64_t(1) << 31;
1064 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1065 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1066 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1067 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1068 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1069 | SystemZ::CCMASK_1
1070 | SystemZ::CCMASK_2)))
1071 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1072 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1073 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1074 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1075 | SystemZ::CCMASK_2
1076 | SystemZ::CCMASK_3)))
1077 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1078
1079 // Next try inverting the value and testing a bit. 0/1 could be
1080 // handled this way too, but we dealt with that case above.
1081 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1082 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1083
1084 // Handle cases where adding a value forces a non-sign bit to contain
1085 // the right value.
1086 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1087 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1088 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1089 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1090
Alp Tokercb402912014-01-24 17:20:08 +00001091 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001092 // can be done by inverting the low CC bit and applying one of the
1093 // sign-based extractions above.
1094 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1095 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1096 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1097 return IPMConversion(1 << SystemZ::IPM_CC,
1098 TopBit - (3 << SystemZ::IPM_CC), 31);
1099 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1100 | SystemZ::CCMASK_1
1101 | SystemZ::CCMASK_3)))
1102 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1103 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1104 | SystemZ::CCMASK_2
1105 | SystemZ::CCMASK_3)))
1106 return IPMConversion(1 << SystemZ::IPM_CC,
1107 TopBit - (1 << SystemZ::IPM_CC), 31);
1108
1109 llvm_unreachable("Unexpected CC combination");
1110}
1111
Richard Sandifordd420f732013-12-13 15:28:45 +00001112// If C can be converted to a comparison against zero, adjust the operands
Richard Sandiforda0757082013-08-01 10:29:45 +00001113// as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001114static void adjustZeroCmp(SelectionDAG &DAG, Comparison &C) {
1115 if (C.ICmpType == SystemZICMP::UnsignedOnly)
Richard Sandiforda0757082013-08-01 10:29:45 +00001116 return;
1117
Richard Sandiford21f5d682014-03-06 11:22:58 +00001118 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
Richard Sandiforda0757082013-08-01 10:29:45 +00001119 if (!ConstOp1)
1120 return;
1121
1122 int64_t Value = ConstOp1->getSExtValue();
Richard Sandifordd420f732013-12-13 15:28:45 +00001123 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1124 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1125 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1126 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1127 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1128 C.Op1 = DAG.getConstant(0, C.Op1.getValueType());
Richard Sandiforda0757082013-08-01 10:29:45 +00001129 }
1130}
1131
Richard Sandifordd420f732013-12-13 15:28:45 +00001132// If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1133// adjust the operands as necessary.
1134static void adjustSubwordCmp(SelectionDAG &DAG, Comparison &C) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001135 // For us to make any changes, it must a comparison between a single-use
1136 // load and a constant.
Richard Sandifordd420f732013-12-13 15:28:45 +00001137 if (!C.Op0.hasOneUse() ||
1138 C.Op0.getOpcode() != ISD::LOAD ||
1139 C.Op1.getOpcode() != ISD::Constant)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001140 return;
1141
1142 // We must have an 8- or 16-bit load.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001143 auto *Load = cast<LoadSDNode>(C.Op0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001144 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1145 if (NumBits != 8 && NumBits != 16)
1146 return;
1147
1148 // The load must be an extending one and the constant must be within the
1149 // range of the unextended value.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001150 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001151 uint64_t Value = ConstOp1->getZExtValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001152 uint64_t Mask = (1 << NumBits) - 1;
1153 if (Load->getExtensionType() == ISD::SEXTLOAD) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001154 // Make sure that ConstOp1 is in range of C.Op0.
1155 int64_t SignedValue = ConstOp1->getSExtValue();
1156 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001157 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001158 if (C.ICmpType != SystemZICMP::SignedOnly) {
1159 // Unsigned comparison between two sign-extended values is equivalent
1160 // to unsigned comparison between two zero-extended values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001161 Value &= Mask;
Richard Sandifordd420f732013-12-13 15:28:45 +00001162 } else if (NumBits == 8) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001163 // Try to treat the comparison as unsigned, so that we can use CLI.
1164 // Adjust CCMask and Value as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001165 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001166 // Test whether the high bit of the byte is set.
Richard Sandifordd420f732013-12-13 15:28:45 +00001167 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1168 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001169 // Test whether the high bit of the byte is clear.
Richard Sandifordd420f732013-12-13 15:28:45 +00001170 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001171 else
1172 // No instruction exists for this combination.
1173 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001174 C.ICmpType = SystemZICMP::UnsignedOnly;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001175 }
1176 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1177 if (Value > Mask)
1178 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001179 assert(C.ICmpType == SystemZICMP::Any &&
1180 "Signedness shouldn't matter here.");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001181 } else
1182 return;
1183
1184 // Make sure that the first operand is an i32 of the right extension type.
Richard Sandifordd420f732013-12-13 15:28:45 +00001185 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1186 ISD::SEXTLOAD :
1187 ISD::ZEXTLOAD);
1188 if (C.Op0.getValueType() != MVT::i32 ||
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001189 Load->getExtensionType() != ExtType)
Richard Sandifordd420f732013-12-13 15:28:45 +00001190 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32,
1191 Load->getChain(), Load->getBasePtr(),
1192 Load->getPointerInfo(), Load->getMemoryVT(),
1193 Load->isVolatile(), Load->isNonTemporal(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00001194 Load->isInvariant(), Load->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001195
1196 // Make sure that the second operand is an i32 with the right value.
Richard Sandifordd420f732013-12-13 15:28:45 +00001197 if (C.Op1.getValueType() != MVT::i32 ||
1198 Value != ConstOp1->getZExtValue())
1199 C.Op1 = DAG.getConstant(Value, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001200}
1201
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001202// Return true if Op is either an unextended load, or a load suitable
1203// for integer register-memory comparisons of type ICmpType.
1204static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001205 auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001206 if (Load) {
1207 // There are no instructions to compare a register with a memory byte.
1208 if (Load->getMemoryVT() == MVT::i8)
1209 return false;
1210 // Otherwise decide on extension type.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001211 switch (Load->getExtensionType()) {
1212 case ISD::NON_EXTLOAD:
Richard Sandiford24e597b2013-08-23 11:27:19 +00001213 return true;
1214 case ISD::SEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001215 return ICmpType != SystemZICMP::UnsignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001216 case ISD::ZEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001217 return ICmpType != SystemZICMP::SignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001218 default:
1219 break;
1220 }
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001221 }
Richard Sandiford24e597b2013-08-23 11:27:19 +00001222 return false;
1223}
1224
Richard Sandifordd420f732013-12-13 15:28:45 +00001225// Return true if it is better to swap the operands of C.
1226static bool shouldSwapCmpOperands(const Comparison &C) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001227 // Leave f128 comparisons alone, since they have no memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001228 if (C.Op0.getValueType() == MVT::f128)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001229 return false;
1230
1231 // Always keep a floating-point constant second, since comparisons with
1232 // zero can use LOAD TEST and comparisons with other constants make a
1233 // natural memory operand.
Richard Sandifordd420f732013-12-13 15:28:45 +00001234 if (isa<ConstantFPSDNode>(C.Op1))
Richard Sandiford24e597b2013-08-23 11:27:19 +00001235 return false;
1236
1237 // Never swap comparisons with zero since there are many ways to optimize
1238 // those later.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001239 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001240 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001241 return false;
1242
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001243 // Also keep natural memory operands second if the loaded value is
1244 // only used here. Several comparisons have memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001245 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001246 return false;
1247
Richard Sandiford24e597b2013-08-23 11:27:19 +00001248 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1249 // In that case we generally prefer the memory to be second.
Richard Sandifordd420f732013-12-13 15:28:45 +00001250 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001251 // The only exceptions are when the second operand is a constant and
1252 // we can use things like CHHSI.
Richard Sandifordd420f732013-12-13 15:28:45 +00001253 if (!ConstOp1)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001254 return true;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001255 // The unsigned memory-immediate instructions can handle 16-bit
1256 // unsigned integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001257 if (C.ICmpType != SystemZICMP::SignedOnly &&
1258 isUInt<16>(ConstOp1->getZExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001259 return false;
1260 // The signed memory-immediate instructions can handle 16-bit
1261 // signed integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001262 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1263 isInt<16>(ConstOp1->getSExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001264 return false;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001265 return true;
1266 }
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001267
1268 // Try to promote the use of CGFR and CLGFR.
Richard Sandifordd420f732013-12-13 15:28:45 +00001269 unsigned Opcode0 = C.Op0.getOpcode();
1270 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001271 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001272 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001273 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001274 if (C.ICmpType != SystemZICMP::SignedOnly &&
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001275 Opcode0 == ISD::AND &&
Richard Sandifordd420f732013-12-13 15:28:45 +00001276 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1277 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001278 return true;
1279
Richard Sandiford24e597b2013-08-23 11:27:19 +00001280 return false;
1281}
1282
Richard Sandiford73170f82013-12-11 11:45:08 +00001283// Return a version of comparison CC mask CCMask in which the LT and GT
1284// actions are swapped.
1285static unsigned reverseCCMask(unsigned CCMask) {
1286 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1287 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1288 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1289 (CCMask & SystemZ::CCMASK_CMP_UO));
1290}
1291
Richard Sandiford0847c452013-12-13 15:50:30 +00001292// Check whether C tests for equality between X and Y and whether X - Y
1293// or Y - X is also computed. In that case it's better to compare the
1294// result of the subtraction against zero.
1295static void adjustForSubtraction(SelectionDAG &DAG, Comparison &C) {
1296 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1297 C.CCMask == SystemZ::CCMASK_CMP_NE) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001298 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001299 SDNode *N = *I;
1300 if (N->getOpcode() == ISD::SUB &&
1301 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1302 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1303 C.Op0 = SDValue(N, 0);
1304 C.Op1 = DAG.getConstant(0, N->getValueType(0));
1305 return;
1306 }
1307 }
1308 }
1309}
1310
Richard Sandifordd420f732013-12-13 15:28:45 +00001311// Check whether C compares a floating-point value with zero and if that
1312// floating-point value is also negated. In this case we can use the
1313// negation to set CC, so avoiding separate LOAD AND TEST and
1314// LOAD (NEGATIVE/COMPLEMENT) instructions.
1315static void adjustForFNeg(Comparison &C) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001316 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
Richard Sandiford73170f82013-12-11 11:45:08 +00001317 if (C1 && C1->isZero()) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001318 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford73170f82013-12-11 11:45:08 +00001319 SDNode *N = *I;
1320 if (N->getOpcode() == ISD::FNEG) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001321 C.Op0 = SDValue(N, 0);
1322 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford73170f82013-12-11 11:45:08 +00001323 return;
1324 }
1325 }
1326 }
1327}
1328
Richard Sandifordd420f732013-12-13 15:28:45 +00001329// Check whether C compares (shl X, 32) with 0 and whether X is
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001330// also sign-extended. In that case it is better to test the result
1331// of the sign extension using LTGFR.
1332//
1333// This case is important because InstCombine transforms a comparison
1334// with (sext (trunc X)) into a comparison with (shl X, 32).
Richard Sandifordd420f732013-12-13 15:28:45 +00001335static void adjustForLTGFR(Comparison &C) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001336 // Check for a comparison between (shl X, 32) and 0.
Richard Sandifordd420f732013-12-13 15:28:45 +00001337 if (C.Op0.getOpcode() == ISD::SHL &&
1338 C.Op0.getValueType() == MVT::i64 &&
1339 C.Op1.getOpcode() == ISD::Constant &&
1340 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001341 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001342 if (C1 && C1->getZExtValue() == 32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001343 SDValue ShlOp0 = C.Op0.getOperand(0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001344 // See whether X has any SIGN_EXTEND_INREG uses.
Richard Sandiford28c111e2014-03-06 11:00:15 +00001345 for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001346 SDNode *N = *I;
1347 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
1348 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001349 C.Op0 = SDValue(N, 0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001350 return;
1351 }
1352 }
1353 }
1354 }
1355}
1356
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001357// If C compares the truncation of an extending load, try to compare
1358// the untruncated value instead. This exposes more opportunities to
1359// reuse CC.
1360static void adjustICmpTruncate(SelectionDAG &DAG, Comparison &C) {
1361 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
1362 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
1363 C.Op1.getOpcode() == ISD::Constant &&
1364 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001365 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001366 if (L->getMemoryVT().getStoreSizeInBits()
1367 <= C.Op0.getValueType().getSizeInBits()) {
1368 unsigned Type = L->getExtensionType();
1369 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
1370 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
1371 C.Op0 = C.Op0.getOperand(0);
1372 C.Op1 = DAG.getConstant(0, C.Op0.getValueType());
1373 }
1374 }
1375 }
1376}
1377
Richard Sandiford030c1652013-09-13 09:09:50 +00001378// Return true if shift operation N has an in-range constant shift value.
1379// Store it in ShiftVal if so.
1380static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001381 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
Richard Sandiford030c1652013-09-13 09:09:50 +00001382 if (!Shift)
1383 return false;
1384
1385 uint64_t Amount = Shift->getZExtValue();
1386 if (Amount >= N.getValueType().getSizeInBits())
1387 return false;
1388
1389 ShiftVal = Amount;
1390 return true;
1391}
1392
1393// Check whether an AND with Mask is suitable for a TEST UNDER MASK
1394// instruction and whether the CC value is descriptive enough to handle
1395// a comparison of type Opcode between the AND result and CmpVal.
1396// CCMask says which comparison result is being tested and BitSize is
1397// the number of bits in the operands. If TEST UNDER MASK can be used,
1398// return the corresponding CC mask, otherwise return 0.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001399static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
1400 uint64_t Mask, uint64_t CmpVal,
1401 unsigned ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001402 assert(Mask != 0 && "ANDs with zero should have been removed by now");
1403
Richard Sandiford030c1652013-09-13 09:09:50 +00001404 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
1405 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
1406 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
1407 return 0;
1408
Richard Sandiford113c8702013-09-03 15:38:35 +00001409 // Work out the masks for the lowest and highest bits.
1410 unsigned HighShift = 63 - countLeadingZeros(Mask);
1411 uint64_t High = uint64_t(1) << HighShift;
1412 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
1413
1414 // Signed ordered comparisons are effectively unsigned if the sign
1415 // bit is dropped.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001416 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
Richard Sandiford113c8702013-09-03 15:38:35 +00001417
1418 // Check for equality comparisons with 0, or the equivalent.
1419 if (CmpVal == 0) {
1420 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1421 return SystemZ::CCMASK_TM_ALL_0;
1422 if (CCMask == SystemZ::CCMASK_CMP_NE)
1423 return SystemZ::CCMASK_TM_SOME_1;
1424 }
1425 if (EffectivelyUnsigned && CmpVal <= Low) {
1426 if (CCMask == SystemZ::CCMASK_CMP_LT)
1427 return SystemZ::CCMASK_TM_ALL_0;
1428 if (CCMask == SystemZ::CCMASK_CMP_GE)
1429 return SystemZ::CCMASK_TM_SOME_1;
1430 }
1431 if (EffectivelyUnsigned && CmpVal < Low) {
1432 if (CCMask == SystemZ::CCMASK_CMP_LE)
1433 return SystemZ::CCMASK_TM_ALL_0;
1434 if (CCMask == SystemZ::CCMASK_CMP_GT)
1435 return SystemZ::CCMASK_TM_SOME_1;
1436 }
1437
1438 // Check for equality comparisons with the mask, or the equivalent.
1439 if (CmpVal == Mask) {
1440 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1441 return SystemZ::CCMASK_TM_ALL_1;
1442 if (CCMask == SystemZ::CCMASK_CMP_NE)
1443 return SystemZ::CCMASK_TM_SOME_0;
1444 }
1445 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
1446 if (CCMask == SystemZ::CCMASK_CMP_GT)
1447 return SystemZ::CCMASK_TM_ALL_1;
1448 if (CCMask == SystemZ::CCMASK_CMP_LE)
1449 return SystemZ::CCMASK_TM_SOME_0;
1450 }
1451 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
1452 if (CCMask == SystemZ::CCMASK_CMP_GE)
1453 return SystemZ::CCMASK_TM_ALL_1;
1454 if (CCMask == SystemZ::CCMASK_CMP_LT)
1455 return SystemZ::CCMASK_TM_SOME_0;
1456 }
1457
1458 // Check for ordered comparisons with the top bit.
1459 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
1460 if (CCMask == SystemZ::CCMASK_CMP_LE)
1461 return SystemZ::CCMASK_TM_MSB_0;
1462 if (CCMask == SystemZ::CCMASK_CMP_GT)
1463 return SystemZ::CCMASK_TM_MSB_1;
1464 }
1465 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
1466 if (CCMask == SystemZ::CCMASK_CMP_LT)
1467 return SystemZ::CCMASK_TM_MSB_0;
1468 if (CCMask == SystemZ::CCMASK_CMP_GE)
1469 return SystemZ::CCMASK_TM_MSB_1;
1470 }
1471
1472 // If there are just two bits, we can do equality checks for Low and High
1473 // as well.
1474 if (Mask == Low + High) {
1475 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
1476 return SystemZ::CCMASK_TM_MIXED_MSB_0;
1477 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
1478 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
1479 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
1480 return SystemZ::CCMASK_TM_MIXED_MSB_1;
1481 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
1482 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
1483 }
1484
1485 // Looks like we've exhausted our options.
1486 return 0;
1487}
1488
Richard Sandifordd420f732013-12-13 15:28:45 +00001489// See whether C can be implemented as a TEST UNDER MASK instruction.
1490// Update the arguments with the TM version if so.
1491static void adjustForTestUnderMask(SelectionDAG &DAG, Comparison &C) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001492 // Check that we have a comparison with a constant.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001493 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001494 if (!ConstOp1)
Richard Sandiford35b9be22013-08-28 10:31:43 +00001495 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001496 uint64_t CmpVal = ConstOp1->getZExtValue();
Richard Sandiford35b9be22013-08-28 10:31:43 +00001497
1498 // Check whether the nonconstant input is an AND with a constant mask.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001499 Comparison NewC(C);
1500 uint64_t MaskVal;
Craig Topper062a2ba2014-04-25 05:30:21 +00001501 ConstantSDNode *Mask = nullptr;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001502 if (C.Op0.getOpcode() == ISD::AND) {
1503 NewC.Op0 = C.Op0.getOperand(0);
1504 NewC.Op1 = C.Op0.getOperand(1);
1505 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
1506 if (!Mask)
1507 return;
1508 MaskVal = Mask->getZExtValue();
1509 } else {
1510 // There is no instruction to compare with a 64-bit immediate
1511 // so use TMHH instead if possible. We need an unsigned ordered
1512 // comparison with an i64 immediate.
1513 if (NewC.Op0.getValueType() != MVT::i64 ||
1514 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
1515 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
1516 NewC.ICmpType == SystemZICMP::SignedOnly)
1517 return;
1518 // Convert LE and GT comparisons into LT and GE.
1519 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
1520 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
1521 if (CmpVal == uint64_t(-1))
1522 return;
1523 CmpVal += 1;
1524 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1525 }
1526 // If the low N bits of Op1 are zero than the low N bits of Op0 can
1527 // be masked off without changing the result.
1528 MaskVal = -(CmpVal & -CmpVal);
1529 NewC.ICmpType = SystemZICMP::UnsignedOnly;
1530 }
Richard Sandiford35b9be22013-08-28 10:31:43 +00001531
Richard Sandiford113c8702013-09-03 15:38:35 +00001532 // Check whether the combination of mask, comparison value and comparison
1533 // type are suitable.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001534 unsigned BitSize = NewC.Op0.getValueType().getSizeInBits();
Richard Sandiford030c1652013-09-13 09:09:50 +00001535 unsigned NewCCMask, ShiftVal;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001536 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
1537 NewC.Op0.getOpcode() == ISD::SHL &&
1538 isSimpleShift(NewC.Op0, ShiftVal) &&
1539 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
1540 MaskVal >> ShiftVal,
Richard Sandiford030c1652013-09-13 09:09:50 +00001541 CmpVal >> ShiftVal,
1542 SystemZICMP::Any))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001543 NewC.Op0 = NewC.Op0.getOperand(0);
1544 MaskVal >>= ShiftVal;
1545 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
1546 NewC.Op0.getOpcode() == ISD::SRL &&
1547 isSimpleShift(NewC.Op0, ShiftVal) &&
1548 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
Richard Sandiford030c1652013-09-13 09:09:50 +00001549 MaskVal << ShiftVal,
1550 CmpVal << ShiftVal,
1551 SystemZICMP::UnsignedOnly))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001552 NewC.Op0 = NewC.Op0.getOperand(0);
1553 MaskVal <<= ShiftVal;
Richard Sandiford030c1652013-09-13 09:09:50 +00001554 } else {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001555 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
1556 NewC.ICmpType);
Richard Sandiford030c1652013-09-13 09:09:50 +00001557 if (!NewCCMask)
1558 return;
1559 }
Richard Sandiford113c8702013-09-03 15:38:35 +00001560
Richard Sandiford35b9be22013-08-28 10:31:43 +00001561 // Go ahead and make the change.
Richard Sandifordd420f732013-12-13 15:28:45 +00001562 C.Opcode = SystemZISD::TM;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001563 C.Op0 = NewC.Op0;
1564 if (Mask && Mask->getZExtValue() == MaskVal)
1565 C.Op1 = SDValue(Mask, 0);
1566 else
1567 C.Op1 = DAG.getConstant(MaskVal, C.Op0.getValueType());
Richard Sandifordd420f732013-12-13 15:28:45 +00001568 C.CCValid = SystemZ::CCMASK_TM;
1569 C.CCMask = NewCCMask;
Richard Sandiford35b9be22013-08-28 10:31:43 +00001570}
1571
Richard Sandifordd420f732013-12-13 15:28:45 +00001572// Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
1573static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
1574 ISD::CondCode Cond) {
1575 Comparison C(CmpOp0, CmpOp1);
1576 C.CCMask = CCMaskForCondCode(Cond);
1577 if (C.Op0.getValueType().isFloatingPoint()) {
1578 C.CCValid = SystemZ::CCMASK_FCMP;
1579 C.Opcode = SystemZISD::FCMP;
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001580 adjustForFNeg(C);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001581 } else {
Richard Sandifordd420f732013-12-13 15:28:45 +00001582 C.CCValid = SystemZ::CCMASK_ICMP;
1583 C.Opcode = SystemZISD::ICMP;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001584 // Choose the type of comparison. Equality and inequality tests can
1585 // use either signed or unsigned comparisons. The choice also doesn't
1586 // matter if both sign bits are known to be clear. In those cases we
1587 // want to give the main isel code the freedom to choose whichever
1588 // form fits best.
Richard Sandifordd420f732013-12-13 15:28:45 +00001589 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1590 C.CCMask == SystemZ::CCMASK_CMP_NE ||
1591 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
1592 C.ICmpType = SystemZICMP::Any;
1593 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
1594 C.ICmpType = SystemZICMP::UnsignedOnly;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001595 else
Richard Sandifordd420f732013-12-13 15:28:45 +00001596 C.ICmpType = SystemZICMP::SignedOnly;
1597 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
1598 adjustZeroCmp(DAG, C);
1599 adjustSubwordCmp(DAG, C);
Richard Sandiford0847c452013-12-13 15:50:30 +00001600 adjustForSubtraction(DAG, C);
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001601 adjustForLTGFR(C);
1602 adjustICmpTruncate(DAG, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001603 }
1604
Richard Sandifordd420f732013-12-13 15:28:45 +00001605 if (shouldSwapCmpOperands(C)) {
1606 std::swap(C.Op0, C.Op1);
1607 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford24e597b2013-08-23 11:27:19 +00001608 }
1609
Richard Sandifordd420f732013-12-13 15:28:45 +00001610 adjustForTestUnderMask(DAG, C);
Richard Sandifordd420f732013-12-13 15:28:45 +00001611 return C;
1612}
1613
1614// Emit the comparison instruction described by C.
1615static SDValue emitCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
1616 if (C.Opcode == SystemZISD::ICMP)
1617 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
1618 DAG.getConstant(C.ICmpType, MVT::i32));
1619 if (C.Opcode == SystemZISD::TM) {
1620 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
1621 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
1622 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
1623 DAG.getConstant(RegisterOnly, MVT::i32));
1624 }
1625 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001626}
1627
Richard Sandiford7d86e472013-08-21 09:34:56 +00001628// Implement a 32-bit *MUL_LOHI operation by extending both operands to
1629// 64 bits. Extend is the extension type to use. Store the high part
1630// in Hi and the low part in Lo.
1631static void lowerMUL_LOHI32(SelectionDAG &DAG, SDLoc DL,
1632 unsigned Extend, SDValue Op0, SDValue Op1,
1633 SDValue &Hi, SDValue &Lo) {
1634 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
1635 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
1636 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
1637 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul, DAG.getConstant(32, MVT::i64));
1638 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
1639 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
1640}
1641
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001642// Lower a binary operation that produces two VT results, one in each
1643// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
1644// Extend extends Op0 to a GR128, and Opcode performs the GR128 operation
1645// on the extended Op0 and (unextended) Op1. Store the even register result
1646// in Even and the odd register result in Odd.
Andrew Trickef9de2a2013-05-25 02:42:55 +00001647static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001648 unsigned Extend, unsigned Opcode,
1649 SDValue Op0, SDValue Op1,
1650 SDValue &Even, SDValue &Odd) {
1651 SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0);
1652 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped,
1653 SDValue(In128, 0), Op1);
1654 bool Is32Bit = is32Bit(VT);
Richard Sandifordd8163202013-09-13 09:12:44 +00001655 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
1656 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001657}
1658
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001659// Return an i32 value that is 1 if the CC value produced by Glue is
1660// in the mask CCMask and 0 otherwise. CC is known to have a value
1661// in CCValid, so other values can be ignored.
1662static SDValue emitSETCC(SelectionDAG &DAG, SDLoc DL, SDValue Glue,
1663 unsigned CCValid, unsigned CCMask) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001664 IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
1665 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
1666
1667 if (Conversion.XORValue)
1668 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
1669 DAG.getConstant(Conversion.XORValue, MVT::i32));
1670
1671 if (Conversion.AddValue)
1672 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
1673 DAG.getConstant(Conversion.AddValue, MVT::i32));
1674
1675 // The SHR/AND sequence should get optimized to an RISBG.
1676 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
1677 DAG.getConstant(Conversion.Bit, MVT::i32));
1678 if (Conversion.Bit != 31)
1679 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
1680 DAG.getConstant(1, MVT::i32));
1681 return Result;
1682}
1683
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001684SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
1685 SelectionDAG &DAG) const {
1686 SDValue CmpOp0 = Op.getOperand(0);
1687 SDValue CmpOp1 = Op.getOperand(1);
1688 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
1689 SDLoc DL(Op);
1690
Richard Sandifordd420f732013-12-13 15:28:45 +00001691 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC));
1692 SDValue Glue = emitCmp(DAG, DL, C);
1693 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001694}
1695
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001696SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
1697 SDValue Chain = Op.getOperand(0);
1698 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1699 SDValue CmpOp0 = Op.getOperand(2);
1700 SDValue CmpOp1 = Op.getOperand(3);
1701 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001702 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001703
Richard Sandifordd420f732013-12-13 15:28:45 +00001704 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC));
1705 SDValue Glue = emitCmp(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001706 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
Richard Sandifordd420f732013-12-13 15:28:45 +00001707 Chain, DAG.getConstant(C.CCValid, MVT::i32),
1708 DAG.getConstant(C.CCMask, MVT::i32), Dest, Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001709}
1710
Richard Sandiford57485472013-12-13 15:35:00 +00001711// Return true if Pos is CmpOp and Neg is the negative of CmpOp,
1712// allowing Pos and Neg to be wider than CmpOp.
1713static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
1714 return (Neg.getOpcode() == ISD::SUB &&
1715 Neg.getOperand(0).getOpcode() == ISD::Constant &&
1716 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
1717 Neg.getOperand(1) == Pos &&
1718 (Pos == CmpOp ||
1719 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
1720 Pos.getOperand(0) == CmpOp)));
1721}
1722
1723// Return the absolute or negative absolute of Op; IsNegative decides which.
1724static SDValue getAbsolute(SelectionDAG &DAG, SDLoc DL, SDValue Op,
1725 bool IsNegative) {
1726 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
1727 if (IsNegative)
1728 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
1729 DAG.getConstant(0, Op.getValueType()), Op);
1730 return Op;
1731}
1732
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001733SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
1734 SelectionDAG &DAG) const {
1735 SDValue CmpOp0 = Op.getOperand(0);
1736 SDValue CmpOp1 = Op.getOperand(1);
1737 SDValue TrueOp = Op.getOperand(2);
1738 SDValue FalseOp = Op.getOperand(3);
1739 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001740 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001741
Richard Sandifordd420f732013-12-13 15:28:45 +00001742 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC));
Richard Sandiford57485472013-12-13 15:35:00 +00001743
1744 // Check for absolute and negative-absolute selections, including those
1745 // where the comparison value is sign-extended (for LPGFR and LNGFR).
1746 // This check supplements the one in DAGCombiner.
1747 if (C.Opcode == SystemZISD::ICMP &&
1748 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
1749 C.CCMask != SystemZ::CCMASK_CMP_NE &&
1750 C.Op1.getOpcode() == ISD::Constant &&
1751 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
1752 if (isAbsolute(C.Op0, TrueOp, FalseOp))
1753 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
1754 if (isAbsolute(C.Op0, FalseOp, TrueOp))
1755 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
1756 }
1757
Richard Sandifordd420f732013-12-13 15:28:45 +00001758 SDValue Glue = emitCmp(DAG, DL, C);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001759
1760 // Special case for handling -1/0 results. The shifts we use here
1761 // should get optimized with the IPM conversion sequence.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001762 auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
1763 auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001764 if (TrueC && FalseC) {
1765 int64_t TrueVal = TrueC->getSExtValue();
1766 int64_t FalseVal = FalseC->getSExtValue();
1767 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
1768 // Invert the condition if we want -1 on false.
1769 if (TrueVal == 0)
Richard Sandifordd420f732013-12-13 15:28:45 +00001770 C.CCMask ^= C.CCValid;
1771 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001772 EVT VT = Op.getValueType();
1773 // Extend the result to VT. Upper bits are ignored.
1774 if (!is32Bit(VT))
1775 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
1776 // Sign-extend from the low bit.
1777 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, MVT::i32);
1778 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
1779 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
1780 }
1781 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001782
Richard Sandiford3d768e32013-07-31 12:30:20 +00001783 SmallVector<SDValue, 5> Ops;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001784 Ops.push_back(TrueOp);
1785 Ops.push_back(FalseOp);
Richard Sandifordd420f732013-12-13 15:28:45 +00001786 Ops.push_back(DAG.getConstant(C.CCValid, MVT::i32));
1787 Ops.push_back(DAG.getConstant(C.CCMask, MVT::i32));
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001788 Ops.push_back(Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001789
1790 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00001791 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001792}
1793
1794SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
1795 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001796 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001797 const GlobalValue *GV = Node->getGlobal();
1798 int64_t Offset = Node->getOffset();
1799 EVT PtrVT = getPointerTy();
Eric Christopher93bf97c2014-06-27 07:38:01 +00001800 Reloc::Model RM = DAG.getTarget().getRelocationModel();
1801 CodeModel::Model CM = DAG.getTarget().getCodeModel();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001802
1803 SDValue Result;
1804 if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) {
Richard Sandiford54b36912013-09-27 15:14:04 +00001805 // Assign anchors at 1<<12 byte boundaries.
1806 uint64_t Anchor = Offset & ~uint64_t(0xfff);
1807 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
1808 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1809
1810 // The offset can be folded into the address if it is aligned to a halfword.
1811 Offset -= Anchor;
1812 if (Offset != 0 && (Offset & 1) == 0) {
1813 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
1814 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001815 Offset = 0;
1816 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001817 } else {
1818 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
1819 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1820 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
1821 MachinePointerInfo::getGOT(), false, false, false, 0);
1822 }
1823
1824 // If there was a non-zero offset that we didn't fold, create an explicit
1825 // addition for it.
1826 if (Offset != 0)
1827 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
1828 DAG.getConstant(Offset, PtrVT));
1829
1830 return Result;
1831}
1832
Ulrich Weigand7db69182015-02-18 09:13:27 +00001833SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
1834 SelectionDAG &DAG,
1835 unsigned Opcode,
1836 SDValue GOTOffset) const {
1837 SDLoc DL(Node);
1838 EVT PtrVT = getPointerTy();
1839 SDValue Chain = DAG.getEntryNode();
1840 SDValue Glue;
1841
1842 // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
1843 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1844 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
1845 Glue = Chain.getValue(1);
1846 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
1847 Glue = Chain.getValue(1);
1848
1849 // The first call operand is the chain and the second is the TLS symbol.
1850 SmallVector<SDValue, 8> Ops;
1851 Ops.push_back(Chain);
1852 Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
1853 Node->getValueType(0),
1854 0, 0));
1855
1856 // Add argument registers to the end of the list so that they are
1857 // known live into the call.
1858 Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
1859 Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
1860
1861 // Add a register mask operand representing the call-preserved registers.
1862 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1863 const uint32_t *Mask = TRI->getCallPreservedMask(CallingConv::C);
1864 assert(Mask && "Missing call preserved mask for calling convention");
1865 Ops.push_back(DAG.getRegisterMask(Mask));
1866
1867 // Glue the call to the argument copies.
1868 Ops.push_back(Glue);
1869
1870 // Emit the call.
1871 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1872 Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
1873 Glue = Chain.getValue(1);
1874
1875 // Copy the return value from %r2.
1876 return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
1877}
1878
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001879SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
1880 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001881 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001882 const GlobalValue *GV = Node->getGlobal();
1883 EVT PtrVT = getPointerTy();
Eric Christopher93bf97c2014-06-27 07:38:01 +00001884 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001885
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001886 // The high part of the thread pointer is in access register 0.
1887 SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
1888 DAG.getConstant(0, MVT::i32));
1889 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
1890
1891 // The low part of the thread pointer is in access register 1.
1892 SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
1893 DAG.getConstant(1, MVT::i32));
1894 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
1895
1896 // Merge them into a single 64-bit address.
1897 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
1898 DAG.getConstant(32, PtrVT));
1899 SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
1900
Ulrich Weigand7db69182015-02-18 09:13:27 +00001901 // Get the offset of GA from the thread pointer, based on the TLS model.
1902 SDValue Offset;
1903 switch (model) {
1904 case TLSModel::GeneralDynamic: {
1905 // Load the GOT offset of the tls_index (module ID / per-symbol offset).
1906 SystemZConstantPoolValue *CPV =
1907 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001908
Ulrich Weigand7db69182015-02-18 09:13:27 +00001909 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
1910 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
1911 Offset, MachinePointerInfo::getConstantPool(),
1912 false, false, false, 0);
1913
1914 // Call __tls_get_offset to retrieve the offset.
1915 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
1916 break;
1917 }
1918
1919 case TLSModel::LocalDynamic: {
1920 // Load the GOT offset of the module ID.
1921 SystemZConstantPoolValue *CPV =
1922 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
1923
1924 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
1925 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
1926 Offset, MachinePointerInfo::getConstantPool(),
1927 false, false, false, 0);
1928
1929 // Call __tls_get_offset to retrieve the module base offset.
1930 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
1931
1932 // Note: The SystemZLDCleanupPass will remove redundant computations
1933 // of the module base offset. Count total number of local-dynamic
1934 // accesses to trigger execution of that pass.
1935 SystemZMachineFunctionInfo* MFI =
1936 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
1937 MFI->incNumLocalDynamicTLSAccesses();
1938
1939 // Add the per-symbol offset.
1940 CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
1941
1942 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8);
1943 DTPOffset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
1944 DTPOffset, MachinePointerInfo::getConstantPool(),
1945 false, false, false, 0);
1946
1947 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
1948 break;
1949 }
1950
1951 case TLSModel::InitialExec: {
1952 // Load the offset from the GOT.
1953 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1954 SystemZII::MO_INDNTPOFF);
1955 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
1956 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
1957 Offset, MachinePointerInfo::getGOT(),
1958 false, false, false, 0);
1959 break;
1960 }
1961
1962 case TLSModel::LocalExec: {
1963 // Force the offset into the constant pool and load it from there.
1964 SystemZConstantPoolValue *CPV =
1965 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
1966
1967 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
1968 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
1969 Offset, MachinePointerInfo::getConstantPool(),
1970 false, false, false, 0);
1971 break;
1972 }
1973
1974 default:
1975 llvm_unreachable("Unknown TLS model.");
1976 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001977
1978 // Add the base and offset together.
1979 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
1980}
1981
1982SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
1983 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001984 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001985 const BlockAddress *BA = Node->getBlockAddress();
1986 int64_t Offset = Node->getOffset();
1987 EVT PtrVT = getPointerTy();
1988
1989 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
1990 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1991 return Result;
1992}
1993
1994SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
1995 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001996 SDLoc DL(JT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001997 EVT PtrVT = getPointerTy();
1998 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1999
2000 // Use LARL to load the address of the table.
2001 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2002}
2003
2004SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
2005 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002006 SDLoc DL(CP);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002007 EVT PtrVT = getPointerTy();
2008
2009 SDValue Result;
2010 if (CP->isMachineConstantPoolEntry())
2011 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2012 CP->getAlignment());
2013 else
2014 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2015 CP->getAlignment(), CP->getOffset());
2016
2017 // Use LARL to load the address of the constant pool entry.
2018 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2019}
2020
2021SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
2022 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002023 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002024 SDValue In = Op.getOperand(0);
2025 EVT InVT = In.getValueType();
2026 EVT ResVT = Op.getValueType();
2027
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002028 if (InVT == MVT::i32 && ResVT == MVT::f32) {
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002029 SDValue In64;
2030 if (Subtarget.hasHighWord()) {
2031 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
2032 MVT::i64);
2033 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
2034 MVT::i64, SDValue(U64, 0), In);
2035 } else {
2036 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
2037 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
2038 DAG.getConstant(32, MVT::i64));
2039 }
2040 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
Richard Sandiford87a44362013-09-30 10:28:35 +00002041 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32,
Richard Sandifordd8163202013-09-13 09:12:44 +00002042 DL, MVT::f32, Out64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002043 }
2044 if (InVT == MVT::f32 && ResVT == MVT::i32) {
2045 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
Richard Sandiford87a44362013-09-30 10:28:35 +00002046 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002047 MVT::f64, SDValue(U64, 0), In);
2048 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002049 if (Subtarget.hasHighWord())
2050 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
2051 MVT::i32, Out64);
2052 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
2053 DAG.getConstant(32, MVT::i64));
2054 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002055 }
2056 llvm_unreachable("Unexpected bitcast combination");
2057}
2058
2059SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
2060 SelectionDAG &DAG) const {
2061 MachineFunction &MF = DAG.getMachineFunction();
2062 SystemZMachineFunctionInfo *FuncInfo =
2063 MF.getInfo<SystemZMachineFunctionInfo>();
2064 EVT PtrVT = getPointerTy();
2065
2066 SDValue Chain = Op.getOperand(0);
2067 SDValue Addr = Op.getOperand(1);
2068 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002069 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002070
2071 // The initial values of each field.
2072 const unsigned NumFields = 4;
2073 SDValue Fields[NumFields] = {
2074 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), PtrVT),
2075 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), PtrVT),
2076 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
2077 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
2078 };
2079
2080 // Store each field into its respective slot.
2081 SDValue MemOps[NumFields];
2082 unsigned Offset = 0;
2083 for (unsigned I = 0; I < NumFields; ++I) {
2084 SDValue FieldAddr = Addr;
2085 if (Offset != 0)
2086 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
2087 DAG.getIntPtrConstant(Offset));
2088 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
2089 MachinePointerInfo(SV, Offset),
2090 false, false, 0);
2091 Offset += 8;
2092 }
Craig Topper48d114b2014-04-26 18:35:24 +00002093 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002094}
2095
2096SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
2097 SelectionDAG &DAG) const {
2098 SDValue Chain = Op.getOperand(0);
2099 SDValue DstPtr = Op.getOperand(1);
2100 SDValue SrcPtr = Op.getOperand(2);
2101 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
2102 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002103 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002104
2105 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32),
2106 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
2107 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
2108}
2109
2110SDValue SystemZTargetLowering::
2111lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
2112 SDValue Chain = Op.getOperand(0);
2113 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002114 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002115
2116 unsigned SPReg = getStackPointerRegisterToSaveRestore();
2117
2118 // Get a reference to the stack pointer.
2119 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
2120
2121 // Get the new stack pointer value.
2122 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, Size);
2123
2124 // Copy the new stack pointer back.
2125 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
2126
2127 // The allocated data lives above the 160 bytes allocated for the standard
2128 // frame, plus any outgoing stack arguments. We don't know how much that
2129 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
2130 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
2131 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
2132
2133 SDValue Ops[2] = { Result, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002134 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002135}
2136
Richard Sandiford7d86e472013-08-21 09:34:56 +00002137SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
2138 SelectionDAG &DAG) const {
2139 EVT VT = Op.getValueType();
2140 SDLoc DL(Op);
2141 SDValue Ops[2];
2142 if (is32Bit(VT))
2143 // Just do a normal 64-bit multiplication and extract the results.
2144 // We define this so that it can be used for constant division.
2145 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
2146 Op.getOperand(1), Ops[1], Ops[0]);
2147 else {
2148 // Do a full 128-bit multiplication based on UMUL_LOHI64:
2149 //
2150 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
2151 //
2152 // but using the fact that the upper halves are either all zeros
2153 // or all ones:
2154 //
2155 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
2156 //
2157 // and grouping the right terms together since they are quicker than the
2158 // multiplication:
2159 //
2160 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
2161 SDValue C63 = DAG.getConstant(63, MVT::i64);
2162 SDValue LL = Op.getOperand(0);
2163 SDValue RL = Op.getOperand(1);
2164 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
2165 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
2166 // UMUL_LOHI64 returns the low result in the odd register and the high
2167 // result in the even register. SMUL_LOHI is defined to return the
2168 // low half first, so the results are in reverse order.
2169 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2170 LL, RL, Ops[1], Ops[0]);
2171 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
2172 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
2173 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
2174 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
2175 }
Craig Topper64941d92014-04-27 19:20:57 +00002176 return DAG.getMergeValues(Ops, DL);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002177}
2178
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002179SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
2180 SelectionDAG &DAG) const {
2181 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002182 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002183 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00002184 if (is32Bit(VT))
2185 // Just do a normal 64-bit multiplication and extract the results.
2186 // We define this so that it can be used for constant division.
2187 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
2188 Op.getOperand(1), Ops[1], Ops[0]);
2189 else
2190 // UMUL_LOHI64 returns the low result in the odd register and the high
2191 // result in the even register. UMUL_LOHI is defined to return the
2192 // low half first, so the results are in reverse order.
2193 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2194 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002195 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002196}
2197
2198SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
2199 SelectionDAG &DAG) const {
2200 SDValue Op0 = Op.getOperand(0);
2201 SDValue Op1 = Op.getOperand(1);
2202 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002203 SDLoc DL(Op);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002204 unsigned Opcode;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002205
2206 // We use DSGF for 32-bit division.
2207 if (is32Bit(VT)) {
2208 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002209 Opcode = SystemZISD::SDIVREM32;
2210 } else if (DAG.ComputeNumSignBits(Op1) > 32) {
2211 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
2212 Opcode = SystemZISD::SDIVREM32;
2213 } else
2214 Opcode = SystemZISD::SDIVREM64;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002215
2216 // DSG(F) takes a 64-bit dividend, so the even register in the GR128
2217 // input is "don't care". The instruction returns the remainder in
2218 // the even register and the quotient in the odd register.
2219 SDValue Ops[2];
Richard Sandiforde6e78852013-07-02 15:40:22 +00002220 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002221 Op0, Op1, Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002222 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002223}
2224
2225SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
2226 SelectionDAG &DAG) const {
2227 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002228 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002229
2230 // DL(G) uses a double-width dividend, so we need to clear the even
2231 // register in the GR128 input. The instruction returns the remainder
2232 // in the even register and the quotient in the odd register.
2233 SDValue Ops[2];
2234 if (is32Bit(VT))
2235 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
2236 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2237 else
2238 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
2239 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002240 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002241}
2242
2243SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
2244 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
2245
2246 // Get the known-zero masks for each operand.
2247 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
2248 APInt KnownZero[2], KnownOne[2];
Jay Foada0653a32014-05-14 21:14:37 +00002249 DAG.computeKnownBits(Ops[0], KnownZero[0], KnownOne[0]);
2250 DAG.computeKnownBits(Ops[1], KnownZero[1], KnownOne[1]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002251
2252 // See if the upper 32 bits of one operand and the lower 32 bits of the
2253 // other are known zero. They are the low and high operands respectively.
2254 uint64_t Masks[] = { KnownZero[0].getZExtValue(),
2255 KnownZero[1].getZExtValue() };
2256 unsigned High, Low;
2257 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
2258 High = 1, Low = 0;
2259 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
2260 High = 0, Low = 1;
2261 else
2262 return Op;
2263
2264 SDValue LowOp = Ops[Low];
2265 SDValue HighOp = Ops[High];
2266
2267 // If the high part is a constant, we're better off using IILH.
2268 if (HighOp.getOpcode() == ISD::Constant)
2269 return Op;
2270
2271 // If the low part is a constant that is outside the range of LHI,
2272 // then we're better off using IILF.
2273 if (LowOp.getOpcode() == ISD::Constant) {
2274 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
2275 if (!isInt<16>(Value))
2276 return Op;
2277 }
2278
2279 // Check whether the high part is an AND that doesn't change the
2280 // high 32 bits and just masks out low bits. We can skip it if so.
2281 if (HighOp.getOpcode() == ISD::AND &&
2282 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
Richard Sandifordccc2a7c2013-12-03 11:01:54 +00002283 SDValue HighOp0 = HighOp.getOperand(0);
2284 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
2285 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
2286 HighOp = HighOp0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002287 }
2288
2289 // Take advantage of the fact that all GR32 operations only change the
2290 // low 32 bits by truncating Low to an i32 and inserting it directly
2291 // using a subreg. The interesting cases are those where the truncation
2292 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002293 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002294 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
Richard Sandiford87a44362013-09-30 10:28:35 +00002295 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002296 MVT::i64, HighOp, Low32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002297}
2298
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002299// Op is an atomic load. Lower it into a normal volatile load.
2300SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
2301 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002302 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002303 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
2304 Node->getChain(), Node->getBasePtr(),
2305 Node->getMemoryVT(), Node->getMemOperand());
2306}
2307
2308// Op is an atomic store. Lower it into a normal volatile store followed
2309// by a serialization.
2310SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
2311 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002312 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002313 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
2314 Node->getBasePtr(), Node->getMemoryVT(),
2315 Node->getMemOperand());
2316 return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other,
2317 Chain), 0);
2318}
2319
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002320// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
2321// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002322SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
2323 SelectionDAG &DAG,
2324 unsigned Opcode) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002325 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002326
2327 // 32-bit operations need no code outside the main loop.
2328 EVT NarrowVT = Node->getMemoryVT();
2329 EVT WideVT = MVT::i32;
2330 if (NarrowVT == WideVT)
2331 return Op;
2332
2333 int64_t BitSize = NarrowVT.getSizeInBits();
2334 SDValue ChainIn = Node->getChain();
2335 SDValue Addr = Node->getBasePtr();
2336 SDValue Src2 = Node->getVal();
2337 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002338 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002339 EVT PtrVT = Addr.getValueType();
2340
2341 // Convert atomic subtracts of constants into additions.
2342 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
Richard Sandiford21f5d682014-03-06 11:22:58 +00002343 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002344 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
2345 Src2 = DAG.getConstant(-Const->getSExtValue(), Src2.getValueType());
2346 }
2347
2348 // Get the address of the containing word.
2349 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
2350 DAG.getConstant(-4, PtrVT));
2351
2352 // Get the number of bits that the word must be rotated left in order
2353 // to bring the field to the top bits of a GR32.
2354 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
2355 DAG.getConstant(3, PtrVT));
2356 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
2357
2358 // Get the complementing shift amount, for rotating a field in the top
2359 // bits back to its proper position.
2360 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
2361 DAG.getConstant(0, WideVT), BitShift);
2362
2363 // Extend the source operand to 32 bits and prepare it for the inner loop.
2364 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
2365 // operations require the source to be shifted in advance. (This shift
2366 // can be folded if the source is constant.) For AND and NAND, the lower
2367 // bits must be set, while for other opcodes they should be left clear.
2368 if (Opcode != SystemZISD::ATOMIC_SWAPW)
2369 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
2370 DAG.getConstant(32 - BitSize, WideVT));
2371 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
2372 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
2373 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
2374 DAG.getConstant(uint32_t(-1) >> BitSize, WideVT));
2375
2376 // Construct the ATOMIC_LOADW_* node.
2377 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
2378 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
2379 DAG.getConstant(BitSize, WideVT) };
2380 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002381 NarrowVT, MMO);
2382
2383 // Rotate the result of the final CS so that the field is in the lower
2384 // bits of a GR32, then truncate it.
2385 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
2386 DAG.getConstant(BitSize, WideVT));
2387 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
2388
2389 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002390 return DAG.getMergeValues(RetOps, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002391}
2392
Richard Sandiford41350a52013-12-24 15:18:04 +00002393// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
Richard Sandiford002019a2013-12-24 15:22:39 +00002394// into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
Richard Sandiford41350a52013-12-24 15:18:04 +00002395// operations into additions.
2396SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
2397 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002398 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandiford41350a52013-12-24 15:18:04 +00002399 EVT MemVT = Node->getMemoryVT();
2400 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
2401 // A full-width operation.
2402 assert(Op.getValueType() == MemVT && "Mismatched VTs");
2403 SDValue Src2 = Node->getVal();
2404 SDValue NegSrc2;
2405 SDLoc DL(Src2);
2406
Richard Sandiford21f5d682014-03-06 11:22:58 +00002407 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
Richard Sandiford41350a52013-12-24 15:18:04 +00002408 // Use an addition if the operand is constant and either LAA(G) is
2409 // available or the negative value is in the range of A(G)FHI.
2410 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
Eric Christopher93bf97c2014-06-27 07:38:01 +00002411 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00002412 NegSrc2 = DAG.getConstant(Value, MemVT);
Eric Christopher93bf97c2014-06-27 07:38:01 +00002413 } else if (Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00002414 // Use LAA(G) if available.
2415 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, MemVT),
2416 Src2);
2417
2418 if (NegSrc2.getNode())
2419 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
2420 Node->getChain(), Node->getBasePtr(), NegSrc2,
2421 Node->getMemOperand(), Node->getOrdering(),
2422 Node->getSynchScope());
2423
2424 // Use the node as-is.
2425 return Op;
2426 }
2427
2428 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
2429}
2430
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002431// Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
2432// into a fullword ATOMIC_CMP_SWAPW operation.
2433SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
2434 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002435 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002436
2437 // We have native support for 32-bit compare and swap.
2438 EVT NarrowVT = Node->getMemoryVT();
2439 EVT WideVT = MVT::i32;
2440 if (NarrowVT == WideVT)
2441 return Op;
2442
2443 int64_t BitSize = NarrowVT.getSizeInBits();
2444 SDValue ChainIn = Node->getOperand(0);
2445 SDValue Addr = Node->getOperand(1);
2446 SDValue CmpVal = Node->getOperand(2);
2447 SDValue SwapVal = Node->getOperand(3);
2448 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002449 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002450 EVT PtrVT = Addr.getValueType();
2451
2452 // Get the address of the containing word.
2453 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
2454 DAG.getConstant(-4, PtrVT));
2455
2456 // Get the number of bits that the word must be rotated left in order
2457 // to bring the field to the top bits of a GR32.
2458 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
2459 DAG.getConstant(3, PtrVT));
2460 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
2461
2462 // Get the complementing shift amount, for rotating a field in the top
2463 // bits back to its proper position.
2464 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
2465 DAG.getConstant(0, WideVT), BitShift);
2466
2467 // Construct the ATOMIC_CMP_SWAPW node.
2468 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
2469 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
2470 NegBitShift, DAG.getConstant(BitSize, WideVT) };
2471 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00002472 VTList, Ops, NarrowVT, MMO);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002473 return AtomicOp;
2474}
2475
2476SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
2477 SelectionDAG &DAG) const {
2478 MachineFunction &MF = DAG.getMachineFunction();
2479 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002480 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002481 SystemZ::R15D, Op.getValueType());
2482}
2483
2484SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
2485 SelectionDAG &DAG) const {
2486 MachineFunction &MF = DAG.getMachineFunction();
2487 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002488 return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002489 SystemZ::R15D, Op.getOperand(1));
2490}
2491
Richard Sandiford03481332013-08-23 11:36:42 +00002492SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
2493 SelectionDAG &DAG) const {
2494 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2495 if (!IsData)
2496 // Just preserve the chain.
2497 return Op.getOperand(0);
2498
2499 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2500 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
Richard Sandiford21f5d682014-03-06 11:22:58 +00002501 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
Richard Sandiford03481332013-08-23 11:36:42 +00002502 SDValue Ops[] = {
2503 Op.getOperand(0),
2504 DAG.getConstant(Code, MVT::i32),
2505 Op.getOperand(1)
2506 };
2507 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, SDLoc(Op),
Craig Topper206fcd42014-04-26 19:29:41 +00002508 Node->getVTList(), Ops,
Richard Sandiford03481332013-08-23 11:36:42 +00002509 Node->getMemoryVT(), Node->getMemOperand());
2510}
2511
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002512SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
2513 SelectionDAG &DAG) const {
2514 switch (Op.getOpcode()) {
2515 case ISD::BR_CC:
2516 return lowerBR_CC(Op, DAG);
2517 case ISD::SELECT_CC:
2518 return lowerSELECT_CC(Op, DAG);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002519 case ISD::SETCC:
2520 return lowerSETCC(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002521 case ISD::GlobalAddress:
2522 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
2523 case ISD::GlobalTLSAddress:
2524 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
2525 case ISD::BlockAddress:
2526 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
2527 case ISD::JumpTable:
2528 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
2529 case ISD::ConstantPool:
2530 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
2531 case ISD::BITCAST:
2532 return lowerBITCAST(Op, DAG);
2533 case ISD::VASTART:
2534 return lowerVASTART(Op, DAG);
2535 case ISD::VACOPY:
2536 return lowerVACOPY(Op, DAG);
2537 case ISD::DYNAMIC_STACKALLOC:
2538 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002539 case ISD::SMUL_LOHI:
2540 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002541 case ISD::UMUL_LOHI:
2542 return lowerUMUL_LOHI(Op, DAG);
2543 case ISD::SDIVREM:
2544 return lowerSDIVREM(Op, DAG);
2545 case ISD::UDIVREM:
2546 return lowerUDIVREM(Op, DAG);
2547 case ISD::OR:
2548 return lowerOR(Op, DAG);
2549 case ISD::ATOMIC_SWAP:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002550 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
2551 case ISD::ATOMIC_STORE:
2552 return lowerATOMIC_STORE(Op, DAG);
2553 case ISD::ATOMIC_LOAD:
2554 return lowerATOMIC_LOAD(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002555 case ISD::ATOMIC_LOAD_ADD:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002556 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002557 case ISD::ATOMIC_LOAD_SUB:
Richard Sandiford41350a52013-12-24 15:18:04 +00002558 return lowerATOMIC_LOAD_SUB(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002559 case ISD::ATOMIC_LOAD_AND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002560 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002561 case ISD::ATOMIC_LOAD_OR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002562 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002563 case ISD::ATOMIC_LOAD_XOR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002564 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002565 case ISD::ATOMIC_LOAD_NAND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002566 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002567 case ISD::ATOMIC_LOAD_MIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002568 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002569 case ISD::ATOMIC_LOAD_MAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002570 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002571 case ISD::ATOMIC_LOAD_UMIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002572 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002573 case ISD::ATOMIC_LOAD_UMAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002574 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002575 case ISD::ATOMIC_CMP_SWAP:
2576 return lowerATOMIC_CMP_SWAP(Op, DAG);
2577 case ISD::STACKSAVE:
2578 return lowerSTACKSAVE(Op, DAG);
2579 case ISD::STACKRESTORE:
2580 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00002581 case ISD::PREFETCH:
2582 return lowerPREFETCH(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002583 default:
2584 llvm_unreachable("Unexpected node to lower");
2585 }
2586}
2587
2588const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
2589#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
2590 switch (Opcode) {
2591 OPCODE(RET_FLAG);
2592 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00002593 OPCODE(SIBCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002594 OPCODE(PCREL_WRAPPER);
Richard Sandiford54b36912013-09-27 15:14:04 +00002595 OPCODE(PCREL_OFFSET);
Richard Sandiford57485472013-12-13 15:35:00 +00002596 OPCODE(IABS);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002597 OPCODE(ICMP);
2598 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00002599 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002600 OPCODE(BR_CCMASK);
2601 OPCODE(SELECT_CCMASK);
2602 OPCODE(ADJDYNALLOC);
2603 OPCODE(EXTRACT_ACCESS);
2604 OPCODE(UMUL_LOHI64);
2605 OPCODE(SDIVREM64);
2606 OPCODE(UDIVREM32);
2607 OPCODE(UDIVREM64);
Richard Sandifordd131ff82013-07-08 09:35:23 +00002608 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00002609 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00002610 OPCODE(NC);
2611 OPCODE(NC_LOOP);
2612 OPCODE(OC);
2613 OPCODE(OC_LOOP);
2614 OPCODE(XC);
2615 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00002616 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00002617 OPCODE(CLC_LOOP);
Richard Sandifordca232712013-08-16 11:21:54 +00002618 OPCODE(STRCMP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00002619 OPCODE(STPCPY);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00002620 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00002621 OPCODE(IPM);
Richard Sandiford9afe6132013-12-10 10:36:34 +00002622 OPCODE(SERIALIZE);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002623 OPCODE(ATOMIC_SWAPW);
2624 OPCODE(ATOMIC_LOADW_ADD);
2625 OPCODE(ATOMIC_LOADW_SUB);
2626 OPCODE(ATOMIC_LOADW_AND);
2627 OPCODE(ATOMIC_LOADW_OR);
2628 OPCODE(ATOMIC_LOADW_XOR);
2629 OPCODE(ATOMIC_LOADW_NAND);
2630 OPCODE(ATOMIC_LOADW_MIN);
2631 OPCODE(ATOMIC_LOADW_MAX);
2632 OPCODE(ATOMIC_LOADW_UMIN);
2633 OPCODE(ATOMIC_LOADW_UMAX);
2634 OPCODE(ATOMIC_CMP_SWAPW);
Richard Sandiford03481332013-08-23 11:36:42 +00002635 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002636 }
Craig Topper062a2ba2014-04-25 05:30:21 +00002637 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002638#undef OPCODE
2639}
2640
Richard Sandiford95bc5f92014-03-07 11:34:35 +00002641SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
2642 DAGCombinerInfo &DCI) const {
2643 SelectionDAG &DAG = DCI.DAG;
2644 unsigned Opcode = N->getOpcode();
2645 if (Opcode == ISD::SIGN_EXTEND) {
2646 // Convert (sext (ashr (shl X, C1), C2)) to
2647 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
2648 // cheap as narrower ones.
2649 SDValue N0 = N->getOperand(0);
2650 EVT VT = N->getValueType(0);
2651 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
2652 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2653 SDValue Inner = N0.getOperand(0);
2654 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
2655 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
2656 unsigned Extra = (VT.getSizeInBits() -
2657 N0.getValueType().getSizeInBits());
2658 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
2659 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
2660 EVT ShiftVT = N0.getOperand(1).getValueType();
2661 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
2662 Inner.getOperand(0));
2663 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
2664 DAG.getConstant(NewShlAmt, ShiftVT));
2665 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
2666 DAG.getConstant(NewSraAmt, ShiftVT));
2667 }
2668 }
2669 }
2670 }
2671 return SDValue();
2672}
2673
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002674//===----------------------------------------------------------------------===//
2675// Custom insertion
2676//===----------------------------------------------------------------------===//
2677
2678// Create a new basic block after MBB.
2679static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
2680 MachineFunction &MF = *MBB->getParent();
2681 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002682 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002683 return NewMBB;
2684}
2685
Richard Sandifordbe133a82013-08-28 09:01:51 +00002686// Split MBB after MI and return the new block (the one that contains
2687// instructions after MI).
2688static MachineBasicBlock *splitBlockAfter(MachineInstr *MI,
2689 MachineBasicBlock *MBB) {
2690 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
2691 NewMBB->splice(NewMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002692 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Richard Sandifordbe133a82013-08-28 09:01:51 +00002693 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
2694 return NewMBB;
2695}
2696
Richard Sandiford5e318f02013-08-27 09:54:29 +00002697// Split MBB before MI and return the new block (the one that contains MI).
2698static MachineBasicBlock *splitBlockBefore(MachineInstr *MI,
2699 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002700 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00002701 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002702 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
2703 return NewMBB;
2704}
2705
Richard Sandiford5e318f02013-08-27 09:54:29 +00002706// Force base value Base into a register before MI. Return the register.
2707static unsigned forceReg(MachineInstr *MI, MachineOperand &Base,
2708 const SystemZInstrInfo *TII) {
2709 if (Base.isReg())
2710 return Base.getReg();
2711
2712 MachineBasicBlock *MBB = MI->getParent();
2713 MachineFunction &MF = *MBB->getParent();
2714 MachineRegisterInfo &MRI = MF.getRegInfo();
2715
2716 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
2717 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg)
2718 .addOperand(Base).addImm(0).addReg(0);
2719 return Reg;
2720}
2721
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002722// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
2723MachineBasicBlock *
2724SystemZTargetLowering::emitSelect(MachineInstr *MI,
2725 MachineBasicBlock *MBB) const {
Eric Christophera6734172015-01-31 00:06:45 +00002726 const SystemZInstrInfo *TII =
2727 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002728
2729 unsigned DestReg = MI->getOperand(0).getReg();
2730 unsigned TrueReg = MI->getOperand(1).getReg();
2731 unsigned FalseReg = MI->getOperand(2).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00002732 unsigned CCValid = MI->getOperand(3).getImm();
2733 unsigned CCMask = MI->getOperand(4).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002734 DebugLoc DL = MI->getDebugLoc();
2735
2736 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002737 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002738 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
2739
2740 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00002741 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002742 // # fallthrough to FalseMBB
2743 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00002744 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2745 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002746 MBB->addSuccessor(JoinMBB);
2747 MBB->addSuccessor(FalseMBB);
2748
2749 // FalseMBB:
2750 // # fallthrough to JoinMBB
2751 MBB = FalseMBB;
2752 MBB->addSuccessor(JoinMBB);
2753
2754 // JoinMBB:
2755 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
2756 // ...
2757 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002758 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002759 .addReg(TrueReg).addMBB(StartMBB)
2760 .addReg(FalseReg).addMBB(FalseMBB);
2761
2762 MI->eraseFromParent();
2763 return JoinMBB;
2764}
2765
Richard Sandifordb86a8342013-06-27 09:27:40 +00002766// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
2767// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002768// happen when the condition is false rather than true. If a STORE ON
2769// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Richard Sandifordb86a8342013-06-27 09:27:40 +00002770MachineBasicBlock *
2771SystemZTargetLowering::emitCondStore(MachineInstr *MI,
2772 MachineBasicBlock *MBB,
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002773 unsigned StoreOpcode, unsigned STOCOpcode,
2774 bool Invert) const {
Eric Christophera6734172015-01-31 00:06:45 +00002775 const SystemZInstrInfo *TII =
2776 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordb86a8342013-06-27 09:27:40 +00002777
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002778 unsigned SrcReg = MI->getOperand(0).getReg();
2779 MachineOperand Base = MI->getOperand(1);
2780 int64_t Disp = MI->getOperand(2).getImm();
2781 unsigned IndexReg = MI->getOperand(3).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00002782 unsigned CCValid = MI->getOperand(4).getImm();
2783 unsigned CCMask = MI->getOperand(5).getImm();
Richard Sandifordb86a8342013-06-27 09:27:40 +00002784 DebugLoc DL = MI->getDebugLoc();
2785
2786 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
2787
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002788 // Use STOCOpcode if possible. We could use different store patterns in
2789 // order to avoid matching the index register, but the performance trade-offs
2790 // might be more complicated in that case.
Eric Christopher93bf97c2014-06-27 07:38:01 +00002791 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002792 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00002793 CCMask ^= CCValid;
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002794 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +00002795 .addReg(SrcReg).addOperand(Base).addImm(Disp)
2796 .addImm(CCValid).addImm(CCMask);
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002797 MI->eraseFromParent();
2798 return MBB;
2799 }
2800
Richard Sandifordb86a8342013-06-27 09:27:40 +00002801 // Get the condition needed to branch around the store.
2802 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00002803 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00002804
2805 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002806 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002807 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
2808
2809 // StartMBB:
2810 // BRC CCMask, JoinMBB
2811 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00002812 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00002813 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2814 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002815 MBB->addSuccessor(JoinMBB);
2816 MBB->addSuccessor(FalseMBB);
2817
2818 // FalseMBB:
2819 // store %SrcReg, %Disp(%Index,%Base)
2820 // # fallthrough to JoinMBB
2821 MBB = FalseMBB;
2822 BuildMI(MBB, DL, TII->get(StoreOpcode))
2823 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
2824 MBB->addSuccessor(JoinMBB);
2825
2826 MI->eraseFromParent();
2827 return JoinMBB;
2828}
2829
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002830// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
2831// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
2832// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
2833// BitSize is the width of the field in bits, or 0 if this is a partword
2834// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
2835// is one of the operands. Invert says whether the field should be
2836// inverted after performing BinOpcode (e.g. for NAND).
2837MachineBasicBlock *
2838SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI,
2839 MachineBasicBlock *MBB,
2840 unsigned BinOpcode,
2841 unsigned BitSize,
2842 bool Invert) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002843 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00002844 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00002845 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002846 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002847 bool IsSubWord = (BitSize < 32);
2848
2849 // Extract the operands. Base can be a register or a frame index.
2850 // Src2 can be a register or immediate.
2851 unsigned Dest = MI->getOperand(0).getReg();
2852 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2853 int64_t Disp = MI->getOperand(2).getImm();
2854 MachineOperand Src2 = earlyUseOperand(MI->getOperand(3));
2855 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
2856 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
2857 DebugLoc DL = MI->getDebugLoc();
2858 if (IsSubWord)
2859 BitSize = MI->getOperand(6).getImm();
2860
2861 // Subword operations use 32-bit registers.
2862 const TargetRegisterClass *RC = (BitSize <= 32 ?
2863 &SystemZ::GR32BitRegClass :
2864 &SystemZ::GR64BitRegClass);
2865 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
2866 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
2867
2868 // Get the right opcodes for the displacement.
2869 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
2870 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
2871 assert(LOpcode && CSOpcode && "Displacement out of range");
2872
2873 // Create virtual registers for temporary results.
2874 unsigned OrigVal = MRI.createVirtualRegister(RC);
2875 unsigned OldVal = MRI.createVirtualRegister(RC);
2876 unsigned NewVal = (BinOpcode || IsSubWord ?
2877 MRI.createVirtualRegister(RC) : Src2.getReg());
2878 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
2879 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
2880
2881 // Insert a basic block for the main loop.
2882 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002883 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002884 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2885
2886 // StartMBB:
2887 // ...
2888 // %OrigVal = L Disp(%Base)
2889 // # fall through to LoopMMB
2890 MBB = StartMBB;
2891 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
2892 .addOperand(Base).addImm(Disp).addReg(0);
2893 MBB->addSuccessor(LoopMBB);
2894
2895 // LoopMBB:
2896 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
2897 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
2898 // %RotatedNewVal = OP %RotatedOldVal, %Src2
2899 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
2900 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
2901 // JNE LoopMBB
2902 // # fall through to DoneMMB
2903 MBB = LoopMBB;
2904 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2905 .addReg(OrigVal).addMBB(StartMBB)
2906 .addReg(Dest).addMBB(LoopMBB);
2907 if (IsSubWord)
2908 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
2909 .addReg(OldVal).addReg(BitShift).addImm(0);
2910 if (Invert) {
2911 // Perform the operation normally and then invert every bit of the field.
2912 unsigned Tmp = MRI.createVirtualRegister(RC);
2913 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
2914 .addReg(RotatedOldVal).addOperand(Src2);
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00002915 if (BitSize <= 32)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002916 // XILF with the upper BitSize bits set.
Richard Sandiford652784e2013-09-25 11:11:53 +00002917 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00002918 .addReg(Tmp).addImm(-1U << (32 - BitSize));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002919 else {
2920 // Use LCGR and add -1 to the result, which is more compact than
2921 // an XILF, XILH pair.
2922 unsigned Tmp2 = MRI.createVirtualRegister(RC);
2923 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
2924 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
2925 .addReg(Tmp2).addImm(-1);
2926 }
2927 } else if (BinOpcode)
2928 // A simply binary operation.
2929 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
2930 .addReg(RotatedOldVal).addOperand(Src2);
2931 else if (IsSubWord)
2932 // Use RISBG to rotate Src2 into position and use it to replace the
2933 // field in RotatedOldVal.
2934 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
2935 .addReg(RotatedOldVal).addReg(Src2.getReg())
2936 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
2937 if (IsSubWord)
2938 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
2939 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
2940 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
2941 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00002942 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2943 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002944 MBB->addSuccessor(LoopMBB);
2945 MBB->addSuccessor(DoneMBB);
2946
2947 MI->eraseFromParent();
2948 return DoneMBB;
2949}
2950
2951// Implement EmitInstrWithCustomInserter for pseudo
2952// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
2953// instruction that should be used to compare the current field with the
2954// minimum or maximum value. KeepOldMask is the BRC condition-code mask
2955// for when the current field should be kept. BitSize is the width of
2956// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
2957MachineBasicBlock *
2958SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI,
2959 MachineBasicBlock *MBB,
2960 unsigned CompareOpcode,
2961 unsigned KeepOldMask,
2962 unsigned BitSize) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002963 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00002964 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00002965 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002966 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002967 bool IsSubWord = (BitSize < 32);
2968
2969 // Extract the operands. Base can be a register or a frame index.
2970 unsigned Dest = MI->getOperand(0).getReg();
2971 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2972 int64_t Disp = MI->getOperand(2).getImm();
2973 unsigned Src2 = MI->getOperand(3).getReg();
2974 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
2975 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
2976 DebugLoc DL = MI->getDebugLoc();
2977 if (IsSubWord)
2978 BitSize = MI->getOperand(6).getImm();
2979
2980 // Subword operations use 32-bit registers.
2981 const TargetRegisterClass *RC = (BitSize <= 32 ?
2982 &SystemZ::GR32BitRegClass :
2983 &SystemZ::GR64BitRegClass);
2984 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
2985 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
2986
2987 // Get the right opcodes for the displacement.
2988 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
2989 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
2990 assert(LOpcode && CSOpcode && "Displacement out of range");
2991
2992 // Create virtual registers for temporary results.
2993 unsigned OrigVal = MRI.createVirtualRegister(RC);
2994 unsigned OldVal = MRI.createVirtualRegister(RC);
2995 unsigned NewVal = MRI.createVirtualRegister(RC);
2996 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
2997 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
2998 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
2999
3000 // Insert 3 basic blocks for the loop.
3001 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00003002 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003003 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
3004 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
3005 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
3006
3007 // StartMBB:
3008 // ...
3009 // %OrigVal = L Disp(%Base)
3010 // # fall through to LoopMMB
3011 MBB = StartMBB;
3012 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
3013 .addOperand(Base).addImm(Disp).addReg(0);
3014 MBB->addSuccessor(LoopMBB);
3015
3016 // LoopMBB:
3017 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
3018 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
3019 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00003020 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003021 MBB = LoopMBB;
3022 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
3023 .addReg(OrigVal).addMBB(StartMBB)
3024 .addReg(Dest).addMBB(UpdateMBB);
3025 if (IsSubWord)
3026 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
3027 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00003028 BuildMI(MBB, DL, TII->get(CompareOpcode))
3029 .addReg(RotatedOldVal).addReg(Src2);
3030 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00003031 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003032 MBB->addSuccessor(UpdateMBB);
3033 MBB->addSuccessor(UseAltMBB);
3034
3035 // UseAltMBB:
3036 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
3037 // # fall through to UpdateMMB
3038 MBB = UseAltMBB;
3039 if (IsSubWord)
3040 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
3041 .addReg(RotatedOldVal).addReg(Src2)
3042 .addImm(32).addImm(31 + BitSize).addImm(0);
3043 MBB->addSuccessor(UpdateMBB);
3044
3045 // UpdateMBB:
3046 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
3047 // [ %RotatedAltVal, UseAltMBB ]
3048 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
3049 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
3050 // JNE LoopMBB
3051 // # fall through to DoneMMB
3052 MBB = UpdateMBB;
3053 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
3054 .addReg(RotatedOldVal).addMBB(LoopMBB)
3055 .addReg(RotatedAltVal).addMBB(UseAltMBB);
3056 if (IsSubWord)
3057 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
3058 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
3059 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
3060 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00003061 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3062 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003063 MBB->addSuccessor(LoopMBB);
3064 MBB->addSuccessor(DoneMBB);
3065
3066 MI->eraseFromParent();
3067 return DoneMBB;
3068}
3069
3070// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
3071// instruction MI.
3072MachineBasicBlock *
3073SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI,
3074 MachineBasicBlock *MBB) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003075 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00003076 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00003077 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003078 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003079
3080 // Extract the operands. Base can be a register or a frame index.
3081 unsigned Dest = MI->getOperand(0).getReg();
3082 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
3083 int64_t Disp = MI->getOperand(2).getImm();
3084 unsigned OrigCmpVal = MI->getOperand(3).getReg();
3085 unsigned OrigSwapVal = MI->getOperand(4).getReg();
3086 unsigned BitShift = MI->getOperand(5).getReg();
3087 unsigned NegBitShift = MI->getOperand(6).getReg();
3088 int64_t BitSize = MI->getOperand(7).getImm();
3089 DebugLoc DL = MI->getDebugLoc();
3090
3091 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
3092
3093 // Get the right opcodes for the displacement.
3094 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
3095 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
3096 assert(LOpcode && CSOpcode && "Displacement out of range");
3097
3098 // Create virtual registers for temporary results.
3099 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
3100 unsigned OldVal = MRI.createVirtualRegister(RC);
3101 unsigned CmpVal = MRI.createVirtualRegister(RC);
3102 unsigned SwapVal = MRI.createVirtualRegister(RC);
3103 unsigned StoreVal = MRI.createVirtualRegister(RC);
3104 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
3105 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
3106 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
3107
3108 // Insert 2 basic blocks for the loop.
3109 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00003110 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003111 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
3112 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
3113
3114 // StartMBB:
3115 // ...
3116 // %OrigOldVal = L Disp(%Base)
3117 // # fall through to LoopMMB
3118 MBB = StartMBB;
3119 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
3120 .addOperand(Base).addImm(Disp).addReg(0);
3121 MBB->addSuccessor(LoopMBB);
3122
3123 // LoopMBB:
3124 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
3125 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
3126 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
3127 // %Dest = RLL %OldVal, BitSize(%BitShift)
3128 // ^^ The low BitSize bits contain the field
3129 // of interest.
3130 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
3131 // ^^ Replace the upper 32-BitSize bits of the
3132 // comparison value with those that we loaded,
3133 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00003134 // CR %Dest, %RetryCmpVal
3135 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003136 // # Fall through to SetMBB
3137 MBB = LoopMBB;
3138 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
3139 .addReg(OrigOldVal).addMBB(StartMBB)
3140 .addReg(RetryOldVal).addMBB(SetMBB);
3141 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
3142 .addReg(OrigCmpVal).addMBB(StartMBB)
3143 .addReg(RetryCmpVal).addMBB(SetMBB);
3144 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
3145 .addReg(OrigSwapVal).addMBB(StartMBB)
3146 .addReg(RetrySwapVal).addMBB(SetMBB);
3147 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
3148 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
3149 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
3150 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00003151 BuildMI(MBB, DL, TII->get(SystemZ::CR))
3152 .addReg(Dest).addReg(RetryCmpVal);
3153 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00003154 .addImm(SystemZ::CCMASK_ICMP)
3155 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003156 MBB->addSuccessor(DoneMBB);
3157 MBB->addSuccessor(SetMBB);
3158
3159 // SetMBB:
3160 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
3161 // ^^ Replace the upper 32-BitSize bits of the new
3162 // value with those that we loaded.
3163 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
3164 // ^^ Rotate the new field to its proper position.
3165 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
3166 // JNE LoopMBB
3167 // # fall through to ExitMMB
3168 MBB = SetMBB;
3169 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
3170 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
3171 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
3172 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
3173 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
3174 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00003175 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3176 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003177 MBB->addSuccessor(LoopMBB);
3178 MBB->addSuccessor(DoneMBB);
3179
3180 MI->eraseFromParent();
3181 return DoneMBB;
3182}
3183
3184// Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true
3185// if the high register of the GR128 value must be cleared or false if
Richard Sandiford87a44362013-09-30 10:28:35 +00003186// it's "don't care". SubReg is subreg_l32 when extending a GR32
3187// and subreg_l64 when extending a GR64.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003188MachineBasicBlock *
3189SystemZTargetLowering::emitExt128(MachineInstr *MI,
3190 MachineBasicBlock *MBB,
3191 bool ClearEven, unsigned SubReg) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003192 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00003193 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00003194 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003195 MachineRegisterInfo &MRI = MF.getRegInfo();
3196 DebugLoc DL = MI->getDebugLoc();
3197
3198 unsigned Dest = MI->getOperand(0).getReg();
3199 unsigned Src = MI->getOperand(1).getReg();
3200 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
3201
3202 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
3203 if (ClearEven) {
3204 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
3205 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
3206
3207 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
3208 .addImm(0);
3209 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
Richard Sandiford87a44362013-09-30 10:28:35 +00003210 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003211 In128 = NewIn128;
3212 }
3213 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
3214 .addReg(In128).addReg(Src).addImm(SubReg);
3215
3216 MI->eraseFromParent();
3217 return MBB;
3218}
3219
Richard Sandifordd131ff82013-07-08 09:35:23 +00003220MachineBasicBlock *
Richard Sandiford564681c2013-08-12 10:28:10 +00003221SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI,
3222 MachineBasicBlock *MBB,
3223 unsigned Opcode) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +00003224 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00003225 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00003226 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandiford5e318f02013-08-27 09:54:29 +00003227 MachineRegisterInfo &MRI = MF.getRegInfo();
Richard Sandifordd131ff82013-07-08 09:35:23 +00003228 DebugLoc DL = MI->getDebugLoc();
3229
Richard Sandiford5e318f02013-08-27 09:54:29 +00003230 MachineOperand DestBase = earlyUseOperand(MI->getOperand(0));
Richard Sandifordd131ff82013-07-08 09:35:23 +00003231 uint64_t DestDisp = MI->getOperand(1).getImm();
Richard Sandiford5e318f02013-08-27 09:54:29 +00003232 MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2));
Richard Sandifordd131ff82013-07-08 09:35:23 +00003233 uint64_t SrcDisp = MI->getOperand(3).getImm();
3234 uint64_t Length = MI->getOperand(4).getImm();
3235
Richard Sandifordbe133a82013-08-28 09:01:51 +00003236 // When generating more than one CLC, all but the last will need to
3237 // branch to the end when a difference is found.
3238 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
Craig Topper062a2ba2014-04-25 05:30:21 +00003239 splitBlockAfter(MI, MBB) : nullptr);
Richard Sandifordbe133a82013-08-28 09:01:51 +00003240
Richard Sandiford5e318f02013-08-27 09:54:29 +00003241 // Check for the loop form, in which operand 5 is the trip count.
3242 if (MI->getNumExplicitOperands() > 5) {
3243 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
3244
3245 uint64_t StartCountReg = MI->getOperand(5).getReg();
3246 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
3247 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
3248 forceReg(MI, DestBase, TII));
3249
3250 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
3251 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
3252 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
3253 MRI.createVirtualRegister(RC));
3254 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
3255 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
3256 MRI.createVirtualRegister(RC));
3257
3258 RC = &SystemZ::GR64BitRegClass;
3259 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
3260 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
3261
3262 MachineBasicBlock *StartMBB = MBB;
3263 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
3264 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00003265 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00003266
3267 // StartMBB:
3268 // # fall through to LoopMMB
3269 MBB->addSuccessor(LoopMBB);
3270
3271 // LoopMBB:
3272 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00003273 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00003274 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00003275 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00003276 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00003277 // [ %NextCountReg, NextMBB ]
3278 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00003279 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00003280 // ( JLH EndMBB )
3281 //
3282 // The prefetch is used only for MVC. The JLH is used only for CLC.
3283 MBB = LoopMBB;
3284
3285 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
3286 .addReg(StartDestReg).addMBB(StartMBB)
3287 .addReg(NextDestReg).addMBB(NextMBB);
3288 if (!HaveSingleBase)
3289 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
3290 .addReg(StartSrcReg).addMBB(StartMBB)
3291 .addReg(NextSrcReg).addMBB(NextMBB);
3292 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
3293 .addReg(StartCountReg).addMBB(StartMBB)
3294 .addReg(NextCountReg).addMBB(NextMBB);
3295 if (Opcode == SystemZ::MVC)
3296 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
3297 .addImm(SystemZ::PFD_WRITE)
3298 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
3299 BuildMI(MBB, DL, TII->get(Opcode))
3300 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
3301 .addReg(ThisSrcReg).addImm(SrcDisp);
3302 if (EndMBB) {
3303 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3304 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
3305 .addMBB(EndMBB);
3306 MBB->addSuccessor(EndMBB);
3307 MBB->addSuccessor(NextMBB);
3308 }
3309
3310 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00003311 // %NextDestReg = LA 256(%ThisDestReg)
3312 // %NextSrcReg = LA 256(%ThisSrcReg)
3313 // %NextCountReg = AGHI %ThisCountReg, -1
3314 // CGHI %NextCountReg, 0
3315 // JLH LoopMBB
3316 // # fall through to DoneMMB
3317 //
3318 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00003319 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00003320
Richard Sandiford5e318f02013-08-27 09:54:29 +00003321 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
3322 .addReg(ThisDestReg).addImm(256).addReg(0);
3323 if (!HaveSingleBase)
3324 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
3325 .addReg(ThisSrcReg).addImm(256).addReg(0);
3326 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
3327 .addReg(ThisCountReg).addImm(-1);
3328 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
3329 .addReg(NextCountReg).addImm(0);
3330 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3331 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
3332 .addMBB(LoopMBB);
3333 MBB->addSuccessor(LoopMBB);
3334 MBB->addSuccessor(DoneMBB);
3335
3336 DestBase = MachineOperand::CreateReg(NextDestReg, false);
3337 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
3338 Length &= 255;
3339 MBB = DoneMBB;
3340 }
3341 // Handle any remaining bytes with straight-line code.
3342 while (Length > 0) {
3343 uint64_t ThisLength = std::min(Length, uint64_t(256));
3344 // The previous iteration might have created out-of-range displacements.
3345 // Apply them using LAY if so.
3346 if (!isUInt<12>(DestDisp)) {
3347 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
3348 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
3349 .addOperand(DestBase).addImm(DestDisp).addReg(0);
3350 DestBase = MachineOperand::CreateReg(Reg, false);
3351 DestDisp = 0;
3352 }
3353 if (!isUInt<12>(SrcDisp)) {
3354 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
3355 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
3356 .addOperand(SrcBase).addImm(SrcDisp).addReg(0);
3357 SrcBase = MachineOperand::CreateReg(Reg, false);
3358 SrcDisp = 0;
3359 }
3360 BuildMI(*MBB, MI, DL, TII->get(Opcode))
3361 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength)
3362 .addOperand(SrcBase).addImm(SrcDisp);
3363 DestDisp += ThisLength;
3364 SrcDisp += ThisLength;
3365 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00003366 // If there's another CLC to go, branch to the end if a difference
3367 // was found.
3368 if (EndMBB && Length > 0) {
3369 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
3370 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3371 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
3372 .addMBB(EndMBB);
3373 MBB->addSuccessor(EndMBB);
3374 MBB->addSuccessor(NextMBB);
3375 MBB = NextMBB;
3376 }
3377 }
3378 if (EndMBB) {
3379 MBB->addSuccessor(EndMBB);
3380 MBB = EndMBB;
3381 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00003382 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00003383
3384 MI->eraseFromParent();
3385 return MBB;
3386}
3387
Richard Sandifordca232712013-08-16 11:21:54 +00003388// Decompose string pseudo-instruction MI into a loop that continually performs
3389// Opcode until CC != 3.
3390MachineBasicBlock *
3391SystemZTargetLowering::emitStringWrapper(MachineInstr *MI,
3392 MachineBasicBlock *MBB,
3393 unsigned Opcode) const {
Richard Sandifordca232712013-08-16 11:21:54 +00003394 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00003395 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00003396 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordca232712013-08-16 11:21:54 +00003397 MachineRegisterInfo &MRI = MF.getRegInfo();
3398 DebugLoc DL = MI->getDebugLoc();
3399
3400 uint64_t End1Reg = MI->getOperand(0).getReg();
3401 uint64_t Start1Reg = MI->getOperand(1).getReg();
3402 uint64_t Start2Reg = MI->getOperand(2).getReg();
3403 uint64_t CharReg = MI->getOperand(3).getReg();
3404
3405 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
3406 uint64_t This1Reg = MRI.createVirtualRegister(RC);
3407 uint64_t This2Reg = MRI.createVirtualRegister(RC);
3408 uint64_t End2Reg = MRI.createVirtualRegister(RC);
3409
3410 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00003411 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00003412 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
3413
3414 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00003415 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00003416 MBB->addSuccessor(LoopMBB);
3417
3418 // LoopMBB:
3419 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
3420 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford7789b082013-09-30 08:48:38 +00003421 // R0L = %CharReg
3422 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
Richard Sandifordca232712013-08-16 11:21:54 +00003423 // JO LoopMBB
3424 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00003425 //
Richard Sandiford7789b082013-09-30 08:48:38 +00003426 // The load of R0L can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00003427 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00003428
3429 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
3430 .addReg(Start1Reg).addMBB(StartMBB)
3431 .addReg(End1Reg).addMBB(LoopMBB);
3432 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
3433 .addReg(Start2Reg).addMBB(StartMBB)
3434 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford7789b082013-09-30 08:48:38 +00003435 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00003436 BuildMI(MBB, DL, TII->get(Opcode))
3437 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
3438 .addReg(This1Reg).addReg(This2Reg);
3439 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3440 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
3441 MBB->addSuccessor(LoopMBB);
3442 MBB->addSuccessor(DoneMBB);
3443
3444 DoneMBB->addLiveIn(SystemZ::CC);
3445
3446 MI->eraseFromParent();
3447 return DoneMBB;
3448}
3449
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003450MachineBasicBlock *SystemZTargetLowering::
3451EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const {
3452 switch (MI->getOpcode()) {
Richard Sandiford7c5c0ea2013-10-01 13:10:16 +00003453 case SystemZ::Select32Mux:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003454 case SystemZ::Select32:
3455 case SystemZ::SelectF32:
3456 case SystemZ::Select64:
3457 case SystemZ::SelectF64:
3458 case SystemZ::SelectF128:
3459 return emitSelect(MI, MBB);
3460
Richard Sandiford2896d042013-10-01 14:33:55 +00003461 case SystemZ::CondStore8Mux:
3462 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
3463 case SystemZ::CondStore8MuxInv:
3464 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
3465 case SystemZ::CondStore16Mux:
3466 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
3467 case SystemZ::CondStore16MuxInv:
3468 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003469 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003470 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003471 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003472 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003473 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003474 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003475 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003476 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003477 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003478 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003479 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003480 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003481 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003482 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003483 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003484 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003485 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003486 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003487 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003488 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003489 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003490 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003491 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003492 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003493
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003494 case SystemZ::AEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00003495 return emitExt128(MI, MBB, false, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003496 case SystemZ::ZEXT128_32:
Richard Sandiford87a44362013-09-30 10:28:35 +00003497 return emitExt128(MI, MBB, true, SystemZ::subreg_l32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003498 case SystemZ::ZEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00003499 return emitExt128(MI, MBB, true, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003500
3501 case SystemZ::ATOMIC_SWAPW:
3502 return emitAtomicLoadBinary(MI, MBB, 0, 0);
3503 case SystemZ::ATOMIC_SWAP_32:
3504 return emitAtomicLoadBinary(MI, MBB, 0, 32);
3505 case SystemZ::ATOMIC_SWAP_64:
3506 return emitAtomicLoadBinary(MI, MBB, 0, 64);
3507
3508 case SystemZ::ATOMIC_LOADW_AR:
3509 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
3510 case SystemZ::ATOMIC_LOADW_AFI:
3511 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
3512 case SystemZ::ATOMIC_LOAD_AR:
3513 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
3514 case SystemZ::ATOMIC_LOAD_AHI:
3515 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
3516 case SystemZ::ATOMIC_LOAD_AFI:
3517 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
3518 case SystemZ::ATOMIC_LOAD_AGR:
3519 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
3520 case SystemZ::ATOMIC_LOAD_AGHI:
3521 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
3522 case SystemZ::ATOMIC_LOAD_AGFI:
3523 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
3524
3525 case SystemZ::ATOMIC_LOADW_SR:
3526 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
3527 case SystemZ::ATOMIC_LOAD_SR:
3528 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
3529 case SystemZ::ATOMIC_LOAD_SGR:
3530 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
3531
3532 case SystemZ::ATOMIC_LOADW_NR:
3533 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
3534 case SystemZ::ATOMIC_LOADW_NILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00003535 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003536 case SystemZ::ATOMIC_LOAD_NR:
3537 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00003538 case SystemZ::ATOMIC_LOAD_NILL:
3539 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
3540 case SystemZ::ATOMIC_LOAD_NILH:
3541 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
3542 case SystemZ::ATOMIC_LOAD_NILF:
3543 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003544 case SystemZ::ATOMIC_LOAD_NGR:
3545 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00003546 case SystemZ::ATOMIC_LOAD_NILL64:
3547 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
3548 case SystemZ::ATOMIC_LOAD_NILH64:
3549 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00003550 case SystemZ::ATOMIC_LOAD_NIHL64:
3551 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
3552 case SystemZ::ATOMIC_LOAD_NIHH64:
3553 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00003554 case SystemZ::ATOMIC_LOAD_NILF64:
3555 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00003556 case SystemZ::ATOMIC_LOAD_NIHF64:
3557 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003558
3559 case SystemZ::ATOMIC_LOADW_OR:
3560 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
3561 case SystemZ::ATOMIC_LOADW_OILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00003562 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003563 case SystemZ::ATOMIC_LOAD_OR:
3564 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00003565 case SystemZ::ATOMIC_LOAD_OILL:
3566 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
3567 case SystemZ::ATOMIC_LOAD_OILH:
3568 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
3569 case SystemZ::ATOMIC_LOAD_OILF:
3570 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003571 case SystemZ::ATOMIC_LOAD_OGR:
3572 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00003573 case SystemZ::ATOMIC_LOAD_OILL64:
3574 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
3575 case SystemZ::ATOMIC_LOAD_OILH64:
3576 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00003577 case SystemZ::ATOMIC_LOAD_OIHL64:
3578 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
3579 case SystemZ::ATOMIC_LOAD_OIHH64:
3580 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00003581 case SystemZ::ATOMIC_LOAD_OILF64:
3582 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00003583 case SystemZ::ATOMIC_LOAD_OIHF64:
3584 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003585
3586 case SystemZ::ATOMIC_LOADW_XR:
3587 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
3588 case SystemZ::ATOMIC_LOADW_XILF:
Richard Sandiford652784e2013-09-25 11:11:53 +00003589 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003590 case SystemZ::ATOMIC_LOAD_XR:
3591 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00003592 case SystemZ::ATOMIC_LOAD_XILF:
3593 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003594 case SystemZ::ATOMIC_LOAD_XGR:
3595 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00003596 case SystemZ::ATOMIC_LOAD_XILF64:
3597 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
Richard Sandiford5718dac2013-10-01 14:08:44 +00003598 case SystemZ::ATOMIC_LOAD_XIHF64:
3599 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003600
3601 case SystemZ::ATOMIC_LOADW_NRi:
3602 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
3603 case SystemZ::ATOMIC_LOADW_NILHi:
Richard Sandiford652784e2013-09-25 11:11:53 +00003604 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003605 case SystemZ::ATOMIC_LOAD_NRi:
3606 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00003607 case SystemZ::ATOMIC_LOAD_NILLi:
3608 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
3609 case SystemZ::ATOMIC_LOAD_NILHi:
3610 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
3611 case SystemZ::ATOMIC_LOAD_NILFi:
3612 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003613 case SystemZ::ATOMIC_LOAD_NGRi:
3614 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00003615 case SystemZ::ATOMIC_LOAD_NILL64i:
3616 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
3617 case SystemZ::ATOMIC_LOAD_NILH64i:
3618 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00003619 case SystemZ::ATOMIC_LOAD_NIHL64i:
3620 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
3621 case SystemZ::ATOMIC_LOAD_NIHH64i:
3622 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00003623 case SystemZ::ATOMIC_LOAD_NILF64i:
3624 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00003625 case SystemZ::ATOMIC_LOAD_NIHF64i:
3626 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003627
3628 case SystemZ::ATOMIC_LOADW_MIN:
3629 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3630 SystemZ::CCMASK_CMP_LE, 0);
3631 case SystemZ::ATOMIC_LOAD_MIN_32:
3632 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3633 SystemZ::CCMASK_CMP_LE, 32);
3634 case SystemZ::ATOMIC_LOAD_MIN_64:
3635 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
3636 SystemZ::CCMASK_CMP_LE, 64);
3637
3638 case SystemZ::ATOMIC_LOADW_MAX:
3639 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3640 SystemZ::CCMASK_CMP_GE, 0);
3641 case SystemZ::ATOMIC_LOAD_MAX_32:
3642 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3643 SystemZ::CCMASK_CMP_GE, 32);
3644 case SystemZ::ATOMIC_LOAD_MAX_64:
3645 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
3646 SystemZ::CCMASK_CMP_GE, 64);
3647
3648 case SystemZ::ATOMIC_LOADW_UMIN:
3649 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3650 SystemZ::CCMASK_CMP_LE, 0);
3651 case SystemZ::ATOMIC_LOAD_UMIN_32:
3652 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3653 SystemZ::CCMASK_CMP_LE, 32);
3654 case SystemZ::ATOMIC_LOAD_UMIN_64:
3655 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
3656 SystemZ::CCMASK_CMP_LE, 64);
3657
3658 case SystemZ::ATOMIC_LOADW_UMAX:
3659 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3660 SystemZ::CCMASK_CMP_GE, 0);
3661 case SystemZ::ATOMIC_LOAD_UMAX_32:
3662 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3663 SystemZ::CCMASK_CMP_GE, 32);
3664 case SystemZ::ATOMIC_LOAD_UMAX_64:
3665 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
3666 SystemZ::CCMASK_CMP_GE, 64);
3667
3668 case SystemZ::ATOMIC_CMP_SWAPW:
3669 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00003670 case SystemZ::MVCSequence:
3671 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00003672 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00003673 case SystemZ::NCSequence:
3674 case SystemZ::NCLoop:
3675 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
3676 case SystemZ::OCSequence:
3677 case SystemZ::OCLoop:
3678 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
3679 case SystemZ::XCSequence:
3680 case SystemZ::XCLoop:
3681 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00003682 case SystemZ::CLCSequence:
3683 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00003684 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00003685 case SystemZ::CLSTLoop:
3686 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00003687 case SystemZ::MVSTLoop:
3688 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00003689 case SystemZ::SRSTLoop:
3690 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003691 default:
3692 llvm_unreachable("Unexpected instr type to insert");
3693 }
3694}