blob: f6c73c3df1ca8e4f23793dedb0addc8964f8d5cd [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 Christopher52349952014-07-01 20:19:02 +000083SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &tm)
84 : TargetLowering(tm, new TargetLoweringObjectFileELF()),
85 Subtarget(tm.getSubtarget<SystemZSubtarget>()) {
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.
221 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
222 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
223 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
224 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
225
226 // Handle the various types of symbolic address.
227 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
228 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
229 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
230 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
231 setOperationAction(ISD::JumpTable, PtrVT, Custom);
232
233 // We need to handle dynamic allocations specially because of the
234 // 160-byte area at the bottom of the stack.
235 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
236
237 // Use custom expanders so that we can force the function to use
238 // a frame pointer.
239 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
240 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
241
Richard Sandiford03481332013-08-23 11:36:42 +0000242 // Handle prefetches with PFD or PFDRL.
243 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
244
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000245 // Handle floating-point types.
246 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
247 I <= MVT::LAST_FP_VALUETYPE;
248 ++I) {
249 MVT VT = MVT::SimpleValueType(I);
250 if (isTypeLegal(VT)) {
251 // We can use FI for FRINT.
252 setOperationAction(ISD::FRINT, VT, Legal);
253
Richard Sandifordaf5f66a2013-08-21 09:04:20 +0000254 // We can use the extended form of FI for other rounding operations.
255 if (Subtarget.hasFPExtension()) {
256 setOperationAction(ISD::FNEARBYINT, VT, Legal);
257 setOperationAction(ISD::FFLOOR, VT, Legal);
258 setOperationAction(ISD::FCEIL, VT, Legal);
259 setOperationAction(ISD::FTRUNC, VT, Legal);
260 setOperationAction(ISD::FROUND, VT, Legal);
261 }
262
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000263 // No special instructions for these.
264 setOperationAction(ISD::FSIN, VT, Expand);
265 setOperationAction(ISD::FCOS, VT, Expand);
266 setOperationAction(ISD::FREM, VT, Expand);
267 }
268 }
269
270 // We have fused multiply-addition for f32 and f64 but not f128.
271 setOperationAction(ISD::FMA, MVT::f32, Legal);
272 setOperationAction(ISD::FMA, MVT::f64, Legal);
273 setOperationAction(ISD::FMA, MVT::f128, Expand);
274
275 // Needed so that we don't try to implement f128 constant loads using
276 // a load-and-extend of a f80 constant (in cases where the constant
277 // would fit in an f80).
278 setLoadExtAction(ISD::EXTLOAD, MVT::f80, Expand);
279
280 // Floating-point truncation and stores need to be done separately.
281 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
282 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
283 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
284
285 // We have 64-bit FPR<->GPR moves, but need special handling for
286 // 32-bit forms.
287 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
288 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
289
290 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
291 // structure, but VAEND is a no-op.
292 setOperationAction(ISD::VASTART, MVT::Other, Custom);
293 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
294 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Richard Sandifordd131ff82013-07-08 09:35:23 +0000295
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000296 // Codes for which we want to perform some z-specific combinations.
297 setTargetDAGCombine(ISD::SIGN_EXTEND);
298
Richard Sandifordd131ff82013-07-08 09:35:23 +0000299 // We want to use MVC in preference to even a single load/store pair.
300 MaxStoresPerMemcpy = 0;
301 MaxStoresPerMemcpyOptSize = 0;
Richard Sandiford47660c12013-07-09 09:32:42 +0000302
303 // The main memset sequence is a byte store followed by an MVC.
304 // Two STC or MV..I stores win over that, but the kind of fused stores
305 // generated by target-independent code don't when the byte value is
306 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
307 // than "STC;MVC". Handle the choice in target-specific code instead.
308 MaxStoresPerMemset = 0;
309 MaxStoresPerMemsetOptSize = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000310}
311
Richard Sandifordabc010b2013-11-06 12:16:02 +0000312EVT SystemZTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
313 if (!VT.isVector())
314 return MVT::i32;
315 return VT.changeVectorElementTypeToInteger();
316}
317
318bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
Stephen Lin73de7bf2013-07-09 18:16:56 +0000319 VT = VT.getScalarType();
320
321 if (!VT.isSimple())
322 return false;
323
324 switch (VT.getSimpleVT().SimpleTy) {
325 case MVT::f32:
326 case MVT::f64:
327 return true;
328 case MVT::f128:
329 return false;
330 default:
331 break;
332 }
333
334 return false;
335}
336
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000337bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
338 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
339 return Imm.isZero() || Imm.isNegZero();
340}
341
Richard Sandiford46af5a22013-05-30 09:45:42 +0000342bool SystemZTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
Matt Arsenault25793a32014-02-05 23:15:53 +0000343 unsigned,
Richard Sandiford46af5a22013-05-30 09:45:42 +0000344 bool *Fast) const {
345 // Unaligned accesses should never be slower than the expanded version.
346 // We check specifically for aligned accesses in the few cases where
347 // they are required.
348 if (Fast)
349 *Fast = true;
350 return true;
351}
352
Richard Sandiford791bea42013-07-31 12:58:26 +0000353bool SystemZTargetLowering::isLegalAddressingMode(const AddrMode &AM,
354 Type *Ty) const {
355 // Punt on globals for now, although they can be used in limited
356 // RELATIVE LONG cases.
357 if (AM.BaseGV)
358 return false;
359
360 // Require a 20-bit signed offset.
361 if (!isInt<20>(AM.BaseOffs))
362 return false;
363
364 // Indexing is OK but no scale factor can be applied.
365 return AM.Scale == 0 || AM.Scale == 1;
366}
367
Richard Sandiford709bda62013-08-19 12:42:31 +0000368bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
369 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
370 return false;
371 unsigned FromBits = FromType->getPrimitiveSizeInBits();
372 unsigned ToBits = ToType->getPrimitiveSizeInBits();
373 return FromBits > ToBits;
374}
375
376bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
377 if (!FromVT.isInteger() || !ToVT.isInteger())
378 return false;
379 unsigned FromBits = FromVT.getSizeInBits();
380 unsigned ToBits = ToVT.getSizeInBits();
381 return FromBits > ToBits;
382}
383
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000384//===----------------------------------------------------------------------===//
385// Inline asm support
386//===----------------------------------------------------------------------===//
387
388TargetLowering::ConstraintType
389SystemZTargetLowering::getConstraintType(const std::string &Constraint) const {
390 if (Constraint.size() == 1) {
391 switch (Constraint[0]) {
392 case 'a': // Address register
393 case 'd': // Data register (equivalent to 'r')
394 case 'f': // Floating-point register
Richard Sandiford0755c932013-10-01 11:26:28 +0000395 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000396 case 'r': // General-purpose register
397 return C_RegisterClass;
398
399 case 'Q': // Memory with base and unsigned 12-bit displacement
400 case 'R': // Likewise, plus an index
401 case 'S': // Memory with base and signed 20-bit displacement
402 case 'T': // Likewise, plus an index
403 case 'm': // Equivalent to 'T'.
404 return C_Memory;
405
406 case 'I': // Unsigned 8-bit constant
407 case 'J': // Unsigned 12-bit constant
408 case 'K': // Signed 16-bit constant
409 case 'L': // Signed 20-bit displacement (on all targets we support)
410 case 'M': // 0x7fffffff
411 return C_Other;
412
413 default:
414 break;
415 }
416 }
417 return TargetLowering::getConstraintType(Constraint);
418}
419
420TargetLowering::ConstraintWeight SystemZTargetLowering::
421getSingleConstraintMatchWeight(AsmOperandInfo &info,
422 const char *constraint) const {
423 ConstraintWeight weight = CW_Invalid;
424 Value *CallOperandVal = info.CallOperandVal;
425 // If we don't have a value, we can't do a match,
426 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +0000427 if (!CallOperandVal)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000428 return CW_Default;
429 Type *type = CallOperandVal->getType();
430 // Look at the constraint type.
431 switch (*constraint) {
432 default:
433 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
434 break;
435
436 case 'a': // Address register
437 case 'd': // Data register (equivalent to 'r')
Richard Sandiford0755c932013-10-01 11:26:28 +0000438 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000439 case 'r': // General-purpose register
440 if (CallOperandVal->getType()->isIntegerTy())
441 weight = CW_Register;
442 break;
443
444 case 'f': // Floating-point register
445 if (type->isFloatingPointTy())
446 weight = CW_Register;
447 break;
448
449 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000450 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000451 if (isUInt<8>(C->getZExtValue()))
452 weight = CW_Constant;
453 break;
454
455 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000456 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000457 if (isUInt<12>(C->getZExtValue()))
458 weight = CW_Constant;
459 break;
460
461 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000462 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000463 if (isInt<16>(C->getSExtValue()))
464 weight = CW_Constant;
465 break;
466
467 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000468 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000469 if (isInt<20>(C->getSExtValue()))
470 weight = CW_Constant;
471 break;
472
473 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000474 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000475 if (C->getZExtValue() == 0x7fffffff)
476 weight = CW_Constant;
477 break;
478 }
479 return weight;
480}
481
Richard Sandifordb8204052013-07-12 09:08:12 +0000482// Parse a "{tNNN}" register constraint for which the register type "t"
483// has already been verified. MC is the class associated with "t" and
484// Map maps 0-based register numbers to LLVM register numbers.
485static std::pair<unsigned, const TargetRegisterClass *>
486parseRegisterNumber(const std::string &Constraint,
487 const TargetRegisterClass *RC, const unsigned *Map) {
488 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
489 if (isdigit(Constraint[2])) {
490 std::string Suffix(Constraint.data() + 2, Constraint.size() - 2);
491 unsigned Index = atoi(Suffix.c_str());
492 if (Index < 16 && Map[Index])
493 return std::make_pair(Map[Index], RC);
494 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000495 return std::make_pair(0U, nullptr);
Richard Sandifordb8204052013-07-12 09:08:12 +0000496}
497
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000498std::pair<unsigned, const TargetRegisterClass *> SystemZTargetLowering::
Chad Rosier295bd432013-06-22 18:37:38 +0000499getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000500 if (Constraint.size() == 1) {
501 // GCC Constraint Letters
502 switch (Constraint[0]) {
503 default: break;
504 case 'd': // Data register (equivalent to 'r')
505 case 'r': // General-purpose register
506 if (VT == MVT::i64)
507 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
508 else if (VT == MVT::i128)
509 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
510 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
511
512 case 'a': // Address register
513 if (VT == MVT::i64)
514 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
515 else if (VT == MVT::i128)
516 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
517 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
518
Richard Sandiford0755c932013-10-01 11:26:28 +0000519 case 'h': // High-part register (an LLVM extension)
520 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
521
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000522 case 'f': // Floating-point register
523 if (VT == MVT::f64)
524 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
525 else if (VT == MVT::f128)
526 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
527 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
528 }
529 }
Richard Sandifordb8204052013-07-12 09:08:12 +0000530 if (Constraint[0] == '{') {
531 // We need to override the default register parsing for GPRs and FPRs
532 // because the interpretation depends on VT. The internal names of
533 // the registers are also different from the external names
534 // (F0D and F0S instead of F0, etc.).
535 if (Constraint[1] == 'r') {
536 if (VT == MVT::i32)
537 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
538 SystemZMC::GR32Regs);
539 if (VT == MVT::i128)
540 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
541 SystemZMC::GR128Regs);
542 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
543 SystemZMC::GR64Regs);
544 }
545 if (Constraint[1] == 'f') {
546 if (VT == MVT::f32)
547 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
548 SystemZMC::FP32Regs);
549 if (VT == MVT::f128)
550 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
551 SystemZMC::FP128Regs);
552 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
553 SystemZMC::FP64Regs);
554 }
555 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000556 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
557}
558
559void SystemZTargetLowering::
560LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
561 std::vector<SDValue> &Ops,
562 SelectionDAG &DAG) const {
563 // Only support length 1 constraints for now.
564 if (Constraint.length() == 1) {
565 switch (Constraint[0]) {
566 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000567 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000568 if (isUInt<8>(C->getZExtValue()))
569 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
570 Op.getValueType()));
571 return;
572
573 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000574 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000575 if (isUInt<12>(C->getZExtValue()))
576 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
577 Op.getValueType()));
578 return;
579
580 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000581 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000582 if (isInt<16>(C->getSExtValue()))
583 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
584 Op.getValueType()));
585 return;
586
587 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000588 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000589 if (isInt<20>(C->getSExtValue()))
590 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
591 Op.getValueType()));
592 return;
593
594 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000595 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000596 if (C->getZExtValue() == 0x7fffffff)
597 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
598 Op.getValueType()));
599 return;
600 }
601 }
602 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
603}
604
605//===----------------------------------------------------------------------===//
606// Calling conventions
607//===----------------------------------------------------------------------===//
608
609#include "SystemZGenCallingConv.inc"
610
Richard Sandiford709bda62013-08-19 12:42:31 +0000611bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
612 Type *ToType) const {
613 return isTruncateFree(FromType, ToType);
614}
615
616bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
617 if (!CI->isTailCall())
618 return false;
619 return true;
620}
621
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000622// Value is a value that has been passed to us in the location described by VA
623// (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
624// any loads onto Chain.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000625static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000626 CCValAssign &VA, SDValue Chain,
627 SDValue Value) {
628 // If the argument has been promoted from a smaller type, insert an
629 // assertion to capture this.
630 if (VA.getLocInfo() == CCValAssign::SExt)
631 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
632 DAG.getValueType(VA.getValVT()));
633 else if (VA.getLocInfo() == CCValAssign::ZExt)
634 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
635 DAG.getValueType(VA.getValVT()));
636
637 if (VA.isExtInLoc())
638 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
639 else if (VA.getLocInfo() == CCValAssign::Indirect)
640 Value = DAG.getLoad(VA.getValVT(), DL, Chain, Value,
641 MachinePointerInfo(), false, false, false, 0);
642 else
643 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
644 return Value;
645}
646
647// Value is a value of type VA.getValVT() that we need to copy into
648// the location described by VA. Return a copy of Value converted to
649// VA.getValVT(). The caller is responsible for handling indirect values.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000650static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000651 CCValAssign &VA, SDValue Value) {
652 switch (VA.getLocInfo()) {
653 case CCValAssign::SExt:
654 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
655 case CCValAssign::ZExt:
656 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
657 case CCValAssign::AExt:
658 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
659 case CCValAssign::Full:
660 return Value;
661 default:
662 llvm_unreachable("Unhandled getLocInfo()");
663 }
664}
665
666SDValue SystemZTargetLowering::
667LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
668 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000669 SDLoc DL, SelectionDAG &DAG,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000670 SmallVectorImpl<SDValue> &InVals) const {
671 MachineFunction &MF = DAG.getMachineFunction();
672 MachineFrameInfo *MFI = MF.getFrameInfo();
673 MachineRegisterInfo &MRI = MF.getRegInfo();
674 SystemZMachineFunctionInfo *FuncInfo =
675 MF.getInfo<SystemZMachineFunctionInfo>();
Eric Christopher93bf97c2014-06-27 07:38:01 +0000676 auto *TFL = static_cast<const SystemZFrameLowering *>(
677 DAG.getTarget().getFrameLowering());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000678
679 // Assign locations to all of the incoming arguments.
680 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher93bf97c2014-06-27 07:38:01 +0000681 CCState CCInfo(CallConv, IsVarArg, MF, DAG.getTarget(), ArgLocs,
682 *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000683 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
684
685 unsigned NumFixedGPRs = 0;
686 unsigned NumFixedFPRs = 0;
687 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
688 SDValue ArgValue;
689 CCValAssign &VA = ArgLocs[I];
690 EVT LocVT = VA.getLocVT();
691 if (VA.isRegLoc()) {
692 // Arguments passed in registers
693 const TargetRegisterClass *RC;
694 switch (LocVT.getSimpleVT().SimpleTy) {
695 default:
696 // Integers smaller than i64 should be promoted to i64.
697 llvm_unreachable("Unexpected argument type");
698 case MVT::i32:
699 NumFixedGPRs += 1;
700 RC = &SystemZ::GR32BitRegClass;
701 break;
702 case MVT::i64:
703 NumFixedGPRs += 1;
704 RC = &SystemZ::GR64BitRegClass;
705 break;
706 case MVT::f32:
707 NumFixedFPRs += 1;
708 RC = &SystemZ::FP32BitRegClass;
709 break;
710 case MVT::f64:
711 NumFixedFPRs += 1;
712 RC = &SystemZ::FP64BitRegClass;
713 break;
714 }
715
716 unsigned VReg = MRI.createVirtualRegister(RC);
717 MRI.addLiveIn(VA.getLocReg(), VReg);
718 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
719 } else {
720 assert(VA.isMemLoc() && "Argument not register or memory");
721
722 // Create the frame index object for this incoming parameter.
723 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
724 VA.getLocMemOffset(), true);
725
726 // Create the SelectionDAG nodes corresponding to a load
727 // from this parameter. Unpromoted ints and floats are
728 // passed as right-justified 8-byte values.
729 EVT PtrVT = getPointerTy();
730 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
731 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
732 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, DAG.getIntPtrConstant(4));
733 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
734 MachinePointerInfo::getFixedStack(FI),
735 false, false, false, 0);
736 }
737
738 // Convert the value of the argument register into the value that's
739 // being passed.
740 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
741 }
742
743 if (IsVarArg) {
744 // Save the number of non-varargs registers for later use by va_start, etc.
745 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
746 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
747
748 // Likewise the address (in the form of a frame index) of where the
749 // first stack vararg would be. The 1-byte size here is arbitrary.
750 int64_t StackSize = CCInfo.getNextStackOffset();
751 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true));
752
753 // ...and a similar frame index for the caller-allocated save area
754 // that will be used to store the incoming registers.
755 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
756 unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true);
757 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
758
759 // Store the FPR varargs in the reserved frame slots. (We store the
760 // GPRs as part of the prologue.)
761 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
762 SDValue MemOps[SystemZ::NumArgFPRs];
763 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
764 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
765 int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true);
766 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
767 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
768 &SystemZ::FP64BitRegClass);
769 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
770 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
771 MachinePointerInfo::getFixedStack(FI),
772 false, false, 0);
773
774 }
775 // Join the stores, which are independent of one another.
776 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Craig Topper2d2aa0c2014-04-30 07:17:30 +0000777 makeArrayRef(&MemOps[NumFixedFPRs],
778 SystemZ::NumArgFPRs-NumFixedFPRs));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000779 }
780 }
781
782 return Chain;
783}
784
Richard Sandiford709bda62013-08-19 12:42:31 +0000785static bool canUseSiblingCall(CCState ArgCCInfo,
786 SmallVectorImpl<CCValAssign> &ArgLocs) {
787 // Punt if there are any indirect or stack arguments, or if the call
788 // needs the call-saved argument register R6.
789 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
790 CCValAssign &VA = ArgLocs[I];
791 if (VA.getLocInfo() == CCValAssign::Indirect)
792 return false;
793 if (!VA.isRegLoc())
794 return false;
795 unsigned Reg = VA.getLocReg();
Richard Sandiford0755c932013-10-01 11:26:28 +0000796 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
Richard Sandiford709bda62013-08-19 12:42:31 +0000797 return false;
798 }
799 return true;
800}
801
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000802SDValue
803SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
804 SmallVectorImpl<SDValue> &InVals) const {
805 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000806 SDLoc &DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000807 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
808 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
809 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000810 SDValue Chain = CLI.Chain;
811 SDValue Callee = CLI.Callee;
Richard Sandiford709bda62013-08-19 12:42:31 +0000812 bool &IsTailCall = CLI.IsTailCall;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000813 CallingConv::ID CallConv = CLI.CallConv;
814 bool IsVarArg = CLI.IsVarArg;
815 MachineFunction &MF = DAG.getMachineFunction();
816 EVT PtrVT = getPointerTy();
817
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000818 // Analyze the operands of the call, assigning locations to each operand.
819 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher93bf97c2014-06-27 07:38:01 +0000820 CCState ArgCCInfo(CallConv, IsVarArg, MF, DAG.getTarget(), ArgLocs,
821 *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000822 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
823
Richard Sandiford709bda62013-08-19 12:42:31 +0000824 // We don't support GuaranteedTailCallOpt, only automatically-detected
825 // sibling calls.
826 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs))
827 IsTailCall = false;
828
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000829 // Get a count of how many bytes are to be pushed on the stack.
830 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
831
832 // Mark the start of the call.
Richard Sandiford709bda62013-08-19 12:42:31 +0000833 if (!IsTailCall)
834 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumBytes, PtrVT, true),
835 DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000836
837 // Copy argument values to their designated locations.
838 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
839 SmallVector<SDValue, 8> MemOpChains;
840 SDValue StackPtr;
841 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
842 CCValAssign &VA = ArgLocs[I];
843 SDValue ArgValue = OutVals[I];
844
845 if (VA.getLocInfo() == CCValAssign::Indirect) {
846 // Store the argument in a stack slot and pass its address.
847 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
848 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
849 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, SpillSlot,
850 MachinePointerInfo::getFixedStack(FI),
851 false, false, 0));
852 ArgValue = SpillSlot;
853 } else
854 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
855
856 if (VA.isRegLoc())
857 // Queue up the argument copies and emit them at the end.
858 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
859 else {
860 assert(VA.isMemLoc() && "Argument not register or memory");
861
862 // Work out the address of the stack slot. Unpromoted ints and
863 // floats are passed as right-justified 8-byte values.
864 if (!StackPtr.getNode())
865 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
866 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
867 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
868 Offset += 4;
869 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
870 DAG.getIntPtrConstant(Offset));
871
872 // Emit the store.
873 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address,
874 MachinePointerInfo(),
875 false, false, 0));
876 }
877 }
878
879 // Join the stores, which are independent of one another.
880 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000881 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000882
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000883 // Accept direct calls by converting symbolic call addresses to the
Richard Sandiford709bda62013-08-19 12:42:31 +0000884 // associated Target* opcodes. Force %r1 to be used for indirect
885 // tail calls.
886 SDValue Glue;
Richard Sandiford21f5d682014-03-06 11:22:58 +0000887 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000888 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
889 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford21f5d682014-03-06 11:22:58 +0000890 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000891 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
892 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford709bda62013-08-19 12:42:31 +0000893 } else if (IsTailCall) {
894 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
895 Glue = Chain.getValue(1);
896 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
897 }
898
899 // Build a sequence of copy-to-reg nodes, chained and glued together.
900 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
901 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
902 RegsToPass[I].second, Glue);
903 Glue = Chain.getValue(1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000904 }
905
906 // The first call operand is the chain and the second is the target address.
907 SmallVector<SDValue, 8> Ops;
908 Ops.push_back(Chain);
909 Ops.push_back(Callee);
910
911 // Add argument registers to the end of the list so that they are
912 // known live into the call.
913 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
914 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
915 RegsToPass[I].second.getValueType()));
916
917 // Glue the call to the argument copies, if any.
918 if (Glue.getNode())
919 Ops.push_back(Glue);
920
921 // Emit the call.
922 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Richard Sandiford709bda62013-08-19 12:42:31 +0000923 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +0000924 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
925 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000926 Glue = Chain.getValue(1);
927
928 // Mark the end of the call, which is glued to the call itself.
929 Chain = DAG.getCALLSEQ_END(Chain,
930 DAG.getConstant(NumBytes, PtrVT, true),
931 DAG.getConstant(0, PtrVT, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +0000932 Glue, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000933 Glue = Chain.getValue(1);
934
935 // Assign locations to each value returned by this call.
936 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopher93bf97c2014-06-27 07:38:01 +0000937 CCState RetCCInfo(CallConv, IsVarArg, MF, DAG.getTarget(), RetLocs,
938 *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000939 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
940
941 // Copy all of the result registers out of their specified physreg.
942 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
943 CCValAssign &VA = RetLocs[I];
944
945 // Copy the value out, gluing the copy to the end of the call sequence.
946 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
947 VA.getLocVT(), Glue);
948 Chain = RetValue.getValue(1);
949 Glue = RetValue.getValue(2);
950
951 // Convert the value of the return register into the value that's
952 // being returned.
953 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
954 }
955
956 return Chain;
957}
958
959SDValue
960SystemZTargetLowering::LowerReturn(SDValue Chain,
961 CallingConv::ID CallConv, bool IsVarArg,
962 const SmallVectorImpl<ISD::OutputArg> &Outs,
963 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000964 SDLoc DL, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000965 MachineFunction &MF = DAG.getMachineFunction();
966
967 // Assign locations to each returned value.
968 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopher93bf97c2014-06-27 07:38:01 +0000969 CCState RetCCInfo(CallConv, IsVarArg, MF, DAG.getTarget(), RetLocs,
970 *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000971 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
972
973 // Quick exit for void returns
974 if (RetLocs.empty())
975 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
976
977 // Copy the result values into the output registers.
978 SDValue Glue;
979 SmallVector<SDValue, 4> RetOps;
980 RetOps.push_back(Chain);
981 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
982 CCValAssign &VA = RetLocs[I];
983 SDValue RetValue = OutVals[I];
984
985 // Make the return register live on exit.
986 assert(VA.isRegLoc() && "Can only return in registers!");
987
988 // Promote the value as required.
989 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
990
991 // Chain and glue the copies together.
992 unsigned Reg = VA.getLocReg();
993 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
994 Glue = Chain.getValue(1);
995 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
996 }
997
998 // Update chain and glue.
999 RetOps[0] = Chain;
1000 if (Glue.getNode())
1001 RetOps.push_back(Glue);
1002
Craig Topper48d114b2014-04-26 18:35:24 +00001003 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001004}
1005
Richard Sandiford9afe6132013-12-10 10:36:34 +00001006SDValue SystemZTargetLowering::
1007prepareVolatileOrAtomicLoad(SDValue Chain, SDLoc DL, SelectionDAG &DAG) const {
1008 return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain);
1009}
1010
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001011// CC is a comparison that will be implemented using an integer or
1012// floating-point comparison. Return the condition code mask for
1013// a branch on true. In the integer case, CCMASK_CMP_UO is set for
1014// unsigned comparisons and clear for signed ones. In the floating-point
1015// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1016static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1017#define CONV(X) \
1018 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1019 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1020 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1021
1022 switch (CC) {
1023 default:
1024 llvm_unreachable("Invalid integer condition!");
1025
1026 CONV(EQ);
1027 CONV(NE);
1028 CONV(GT);
1029 CONV(GE);
1030 CONV(LT);
1031 CONV(LE);
1032
1033 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1034 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1035 }
1036#undef CONV
1037}
1038
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001039// Return a sequence for getting a 1 from an IPM result when CC has a
1040// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1041// The handling of CC values outside CCValid doesn't matter.
1042static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1043 // Deal with cases where the result can be taken directly from a bit
1044 // of the IPM result.
1045 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1046 return IPMConversion(0, 0, SystemZ::IPM_CC);
1047 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1048 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1049
1050 // Deal with cases where we can add a value to force the sign bit
1051 // to contain the right value. Putting the bit in 31 means we can
1052 // use SRL rather than RISBG(L), and also makes it easier to get a
1053 // 0/-1 value, so it has priority over the other tests below.
1054 //
1055 // These sequences rely on the fact that the upper two bits of the
1056 // IPM result are zero.
1057 uint64_t TopBit = uint64_t(1) << 31;
1058 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1059 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1060 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1061 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1062 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1063 | SystemZ::CCMASK_1
1064 | SystemZ::CCMASK_2)))
1065 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1066 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1067 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1068 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1069 | SystemZ::CCMASK_2
1070 | SystemZ::CCMASK_3)))
1071 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1072
1073 // Next try inverting the value and testing a bit. 0/1 could be
1074 // handled this way too, but we dealt with that case above.
1075 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1076 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1077
1078 // Handle cases where adding a value forces a non-sign bit to contain
1079 // the right value.
1080 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1081 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1082 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1083 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1084
Alp Tokercb402912014-01-24 17:20:08 +00001085 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001086 // can be done by inverting the low CC bit and applying one of the
1087 // sign-based extractions above.
1088 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1089 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1090 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1091 return IPMConversion(1 << SystemZ::IPM_CC,
1092 TopBit - (3 << SystemZ::IPM_CC), 31);
1093 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1094 | SystemZ::CCMASK_1
1095 | SystemZ::CCMASK_3)))
1096 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1097 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1098 | SystemZ::CCMASK_2
1099 | SystemZ::CCMASK_3)))
1100 return IPMConversion(1 << SystemZ::IPM_CC,
1101 TopBit - (1 << SystemZ::IPM_CC), 31);
1102
1103 llvm_unreachable("Unexpected CC combination");
1104}
1105
Richard Sandifordd420f732013-12-13 15:28:45 +00001106// If C can be converted to a comparison against zero, adjust the operands
Richard Sandiforda0757082013-08-01 10:29:45 +00001107// as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001108static void adjustZeroCmp(SelectionDAG &DAG, Comparison &C) {
1109 if (C.ICmpType == SystemZICMP::UnsignedOnly)
Richard Sandiforda0757082013-08-01 10:29:45 +00001110 return;
1111
Richard Sandiford21f5d682014-03-06 11:22:58 +00001112 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
Richard Sandiforda0757082013-08-01 10:29:45 +00001113 if (!ConstOp1)
1114 return;
1115
1116 int64_t Value = ConstOp1->getSExtValue();
Richard Sandifordd420f732013-12-13 15:28:45 +00001117 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1118 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1119 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1120 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1121 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1122 C.Op1 = DAG.getConstant(0, C.Op1.getValueType());
Richard Sandiforda0757082013-08-01 10:29:45 +00001123 }
1124}
1125
Richard Sandifordd420f732013-12-13 15:28:45 +00001126// If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1127// adjust the operands as necessary.
1128static void adjustSubwordCmp(SelectionDAG &DAG, Comparison &C) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001129 // For us to make any changes, it must a comparison between a single-use
1130 // load and a constant.
Richard Sandifordd420f732013-12-13 15:28:45 +00001131 if (!C.Op0.hasOneUse() ||
1132 C.Op0.getOpcode() != ISD::LOAD ||
1133 C.Op1.getOpcode() != ISD::Constant)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001134 return;
1135
1136 // We must have an 8- or 16-bit load.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001137 auto *Load = cast<LoadSDNode>(C.Op0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001138 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1139 if (NumBits != 8 && NumBits != 16)
1140 return;
1141
1142 // The load must be an extending one and the constant must be within the
1143 // range of the unextended value.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001144 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001145 uint64_t Value = ConstOp1->getZExtValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001146 uint64_t Mask = (1 << NumBits) - 1;
1147 if (Load->getExtensionType() == ISD::SEXTLOAD) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001148 // Make sure that ConstOp1 is in range of C.Op0.
1149 int64_t SignedValue = ConstOp1->getSExtValue();
1150 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001151 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001152 if (C.ICmpType != SystemZICMP::SignedOnly) {
1153 // Unsigned comparison between two sign-extended values is equivalent
1154 // to unsigned comparison between two zero-extended values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001155 Value &= Mask;
Richard Sandifordd420f732013-12-13 15:28:45 +00001156 } else if (NumBits == 8) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001157 // Try to treat the comparison as unsigned, so that we can use CLI.
1158 // Adjust CCMask and Value as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001159 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001160 // Test whether the high bit of the byte is set.
Richard Sandifordd420f732013-12-13 15:28:45 +00001161 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1162 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001163 // Test whether the high bit of the byte is clear.
Richard Sandifordd420f732013-12-13 15:28:45 +00001164 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001165 else
1166 // No instruction exists for this combination.
1167 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001168 C.ICmpType = SystemZICMP::UnsignedOnly;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001169 }
1170 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1171 if (Value > Mask)
1172 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001173 assert(C.ICmpType == SystemZICMP::Any &&
1174 "Signedness shouldn't matter here.");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001175 } else
1176 return;
1177
1178 // Make sure that the first operand is an i32 of the right extension type.
Richard Sandifordd420f732013-12-13 15:28:45 +00001179 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1180 ISD::SEXTLOAD :
1181 ISD::ZEXTLOAD);
1182 if (C.Op0.getValueType() != MVT::i32 ||
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001183 Load->getExtensionType() != ExtType)
Richard Sandifordd420f732013-12-13 15:28:45 +00001184 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32,
1185 Load->getChain(), Load->getBasePtr(),
1186 Load->getPointerInfo(), Load->getMemoryVT(),
1187 Load->isVolatile(), Load->isNonTemporal(),
1188 Load->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001189
1190 // Make sure that the second operand is an i32 with the right value.
Richard Sandifordd420f732013-12-13 15:28:45 +00001191 if (C.Op1.getValueType() != MVT::i32 ||
1192 Value != ConstOp1->getZExtValue())
1193 C.Op1 = DAG.getConstant(Value, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001194}
1195
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001196// Return true if Op is either an unextended load, or a load suitable
1197// for integer register-memory comparisons of type ICmpType.
1198static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001199 auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001200 if (Load) {
1201 // There are no instructions to compare a register with a memory byte.
1202 if (Load->getMemoryVT() == MVT::i8)
1203 return false;
1204 // Otherwise decide on extension type.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001205 switch (Load->getExtensionType()) {
1206 case ISD::NON_EXTLOAD:
Richard Sandiford24e597b2013-08-23 11:27:19 +00001207 return true;
1208 case ISD::SEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001209 return ICmpType != SystemZICMP::UnsignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001210 case ISD::ZEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001211 return ICmpType != SystemZICMP::SignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001212 default:
1213 break;
1214 }
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001215 }
Richard Sandiford24e597b2013-08-23 11:27:19 +00001216 return false;
1217}
1218
Richard Sandifordd420f732013-12-13 15:28:45 +00001219// Return true if it is better to swap the operands of C.
1220static bool shouldSwapCmpOperands(const Comparison &C) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001221 // Leave f128 comparisons alone, since they have no memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001222 if (C.Op0.getValueType() == MVT::f128)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001223 return false;
1224
1225 // Always keep a floating-point constant second, since comparisons with
1226 // zero can use LOAD TEST and comparisons with other constants make a
1227 // natural memory operand.
Richard Sandifordd420f732013-12-13 15:28:45 +00001228 if (isa<ConstantFPSDNode>(C.Op1))
Richard Sandiford24e597b2013-08-23 11:27:19 +00001229 return false;
1230
1231 // Never swap comparisons with zero since there are many ways to optimize
1232 // those later.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001233 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001234 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001235 return false;
1236
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001237 // Also keep natural memory operands second if the loaded value is
1238 // only used here. Several comparisons have memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001239 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001240 return false;
1241
Richard Sandiford24e597b2013-08-23 11:27:19 +00001242 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1243 // In that case we generally prefer the memory to be second.
Richard Sandifordd420f732013-12-13 15:28:45 +00001244 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001245 // The only exceptions are when the second operand is a constant and
1246 // we can use things like CHHSI.
Richard Sandifordd420f732013-12-13 15:28:45 +00001247 if (!ConstOp1)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001248 return true;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001249 // The unsigned memory-immediate instructions can handle 16-bit
1250 // unsigned integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001251 if (C.ICmpType != SystemZICMP::SignedOnly &&
1252 isUInt<16>(ConstOp1->getZExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001253 return false;
1254 // The signed memory-immediate instructions can handle 16-bit
1255 // signed integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001256 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1257 isInt<16>(ConstOp1->getSExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001258 return false;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001259 return true;
1260 }
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001261
1262 // Try to promote the use of CGFR and CLGFR.
Richard Sandifordd420f732013-12-13 15:28:45 +00001263 unsigned Opcode0 = C.Op0.getOpcode();
1264 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001265 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001266 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001267 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001268 if (C.ICmpType != SystemZICMP::SignedOnly &&
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001269 Opcode0 == ISD::AND &&
Richard Sandifordd420f732013-12-13 15:28:45 +00001270 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1271 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001272 return true;
1273
Richard Sandiford24e597b2013-08-23 11:27:19 +00001274 return false;
1275}
1276
Richard Sandiford73170f82013-12-11 11:45:08 +00001277// Return a version of comparison CC mask CCMask in which the LT and GT
1278// actions are swapped.
1279static unsigned reverseCCMask(unsigned CCMask) {
1280 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1281 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1282 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1283 (CCMask & SystemZ::CCMASK_CMP_UO));
1284}
1285
Richard Sandiford0847c452013-12-13 15:50:30 +00001286// Check whether C tests for equality between X and Y and whether X - Y
1287// or Y - X is also computed. In that case it's better to compare the
1288// result of the subtraction against zero.
1289static void adjustForSubtraction(SelectionDAG &DAG, Comparison &C) {
1290 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1291 C.CCMask == SystemZ::CCMASK_CMP_NE) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001292 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001293 SDNode *N = *I;
1294 if (N->getOpcode() == ISD::SUB &&
1295 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1296 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1297 C.Op0 = SDValue(N, 0);
1298 C.Op1 = DAG.getConstant(0, N->getValueType(0));
1299 return;
1300 }
1301 }
1302 }
1303}
1304
Richard Sandifordd420f732013-12-13 15:28:45 +00001305// Check whether C compares a floating-point value with zero and if that
1306// floating-point value is also negated. In this case we can use the
1307// negation to set CC, so avoiding separate LOAD AND TEST and
1308// LOAD (NEGATIVE/COMPLEMENT) instructions.
1309static void adjustForFNeg(Comparison &C) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001310 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
Richard Sandiford73170f82013-12-11 11:45:08 +00001311 if (C1 && C1->isZero()) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001312 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford73170f82013-12-11 11:45:08 +00001313 SDNode *N = *I;
1314 if (N->getOpcode() == ISD::FNEG) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001315 C.Op0 = SDValue(N, 0);
1316 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford73170f82013-12-11 11:45:08 +00001317 return;
1318 }
1319 }
1320 }
1321}
1322
Richard Sandifordd420f732013-12-13 15:28:45 +00001323// Check whether C compares (shl X, 32) with 0 and whether X is
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001324// also sign-extended. In that case it is better to test the result
1325// of the sign extension using LTGFR.
1326//
1327// This case is important because InstCombine transforms a comparison
1328// with (sext (trunc X)) into a comparison with (shl X, 32).
Richard Sandifordd420f732013-12-13 15:28:45 +00001329static void adjustForLTGFR(Comparison &C) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001330 // Check for a comparison between (shl X, 32) and 0.
Richard Sandifordd420f732013-12-13 15:28:45 +00001331 if (C.Op0.getOpcode() == ISD::SHL &&
1332 C.Op0.getValueType() == MVT::i64 &&
1333 C.Op1.getOpcode() == ISD::Constant &&
1334 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001335 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001336 if (C1 && C1->getZExtValue() == 32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001337 SDValue ShlOp0 = C.Op0.getOperand(0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001338 // See whether X has any SIGN_EXTEND_INREG uses.
Richard Sandiford28c111e2014-03-06 11:00:15 +00001339 for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001340 SDNode *N = *I;
1341 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
1342 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001343 C.Op0 = SDValue(N, 0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001344 return;
1345 }
1346 }
1347 }
1348 }
1349}
1350
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001351// If C compares the truncation of an extending load, try to compare
1352// the untruncated value instead. This exposes more opportunities to
1353// reuse CC.
1354static void adjustICmpTruncate(SelectionDAG &DAG, Comparison &C) {
1355 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
1356 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
1357 C.Op1.getOpcode() == ISD::Constant &&
1358 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001359 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001360 if (L->getMemoryVT().getStoreSizeInBits()
1361 <= C.Op0.getValueType().getSizeInBits()) {
1362 unsigned Type = L->getExtensionType();
1363 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
1364 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
1365 C.Op0 = C.Op0.getOperand(0);
1366 C.Op1 = DAG.getConstant(0, C.Op0.getValueType());
1367 }
1368 }
1369 }
1370}
1371
Richard Sandiford030c1652013-09-13 09:09:50 +00001372// Return true if shift operation N has an in-range constant shift value.
1373// Store it in ShiftVal if so.
1374static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001375 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
Richard Sandiford030c1652013-09-13 09:09:50 +00001376 if (!Shift)
1377 return false;
1378
1379 uint64_t Amount = Shift->getZExtValue();
1380 if (Amount >= N.getValueType().getSizeInBits())
1381 return false;
1382
1383 ShiftVal = Amount;
1384 return true;
1385}
1386
1387// Check whether an AND with Mask is suitable for a TEST UNDER MASK
1388// instruction and whether the CC value is descriptive enough to handle
1389// a comparison of type Opcode between the AND result and CmpVal.
1390// CCMask says which comparison result is being tested and BitSize is
1391// the number of bits in the operands. If TEST UNDER MASK can be used,
1392// return the corresponding CC mask, otherwise return 0.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001393static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
1394 uint64_t Mask, uint64_t CmpVal,
1395 unsigned ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001396 assert(Mask != 0 && "ANDs with zero should have been removed by now");
1397
Richard Sandiford030c1652013-09-13 09:09:50 +00001398 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
1399 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
1400 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
1401 return 0;
1402
Richard Sandiford113c8702013-09-03 15:38:35 +00001403 // Work out the masks for the lowest and highest bits.
1404 unsigned HighShift = 63 - countLeadingZeros(Mask);
1405 uint64_t High = uint64_t(1) << HighShift;
1406 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
1407
1408 // Signed ordered comparisons are effectively unsigned if the sign
1409 // bit is dropped.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001410 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
Richard Sandiford113c8702013-09-03 15:38:35 +00001411
1412 // Check for equality comparisons with 0, or the equivalent.
1413 if (CmpVal == 0) {
1414 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1415 return SystemZ::CCMASK_TM_ALL_0;
1416 if (CCMask == SystemZ::CCMASK_CMP_NE)
1417 return SystemZ::CCMASK_TM_SOME_1;
1418 }
1419 if (EffectivelyUnsigned && CmpVal <= Low) {
1420 if (CCMask == SystemZ::CCMASK_CMP_LT)
1421 return SystemZ::CCMASK_TM_ALL_0;
1422 if (CCMask == SystemZ::CCMASK_CMP_GE)
1423 return SystemZ::CCMASK_TM_SOME_1;
1424 }
1425 if (EffectivelyUnsigned && CmpVal < Low) {
1426 if (CCMask == SystemZ::CCMASK_CMP_LE)
1427 return SystemZ::CCMASK_TM_ALL_0;
1428 if (CCMask == SystemZ::CCMASK_CMP_GT)
1429 return SystemZ::CCMASK_TM_SOME_1;
1430 }
1431
1432 // Check for equality comparisons with the mask, or the equivalent.
1433 if (CmpVal == Mask) {
1434 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1435 return SystemZ::CCMASK_TM_ALL_1;
1436 if (CCMask == SystemZ::CCMASK_CMP_NE)
1437 return SystemZ::CCMASK_TM_SOME_0;
1438 }
1439 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
1440 if (CCMask == SystemZ::CCMASK_CMP_GT)
1441 return SystemZ::CCMASK_TM_ALL_1;
1442 if (CCMask == SystemZ::CCMASK_CMP_LE)
1443 return SystemZ::CCMASK_TM_SOME_0;
1444 }
1445 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
1446 if (CCMask == SystemZ::CCMASK_CMP_GE)
1447 return SystemZ::CCMASK_TM_ALL_1;
1448 if (CCMask == SystemZ::CCMASK_CMP_LT)
1449 return SystemZ::CCMASK_TM_SOME_0;
1450 }
1451
1452 // Check for ordered comparisons with the top bit.
1453 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
1454 if (CCMask == SystemZ::CCMASK_CMP_LE)
1455 return SystemZ::CCMASK_TM_MSB_0;
1456 if (CCMask == SystemZ::CCMASK_CMP_GT)
1457 return SystemZ::CCMASK_TM_MSB_1;
1458 }
1459 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
1460 if (CCMask == SystemZ::CCMASK_CMP_LT)
1461 return SystemZ::CCMASK_TM_MSB_0;
1462 if (CCMask == SystemZ::CCMASK_CMP_GE)
1463 return SystemZ::CCMASK_TM_MSB_1;
1464 }
1465
1466 // If there are just two bits, we can do equality checks for Low and High
1467 // as well.
1468 if (Mask == Low + High) {
1469 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
1470 return SystemZ::CCMASK_TM_MIXED_MSB_0;
1471 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
1472 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
1473 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
1474 return SystemZ::CCMASK_TM_MIXED_MSB_1;
1475 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
1476 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
1477 }
1478
1479 // Looks like we've exhausted our options.
1480 return 0;
1481}
1482
Richard Sandifordd420f732013-12-13 15:28:45 +00001483// See whether C can be implemented as a TEST UNDER MASK instruction.
1484// Update the arguments with the TM version if so.
1485static void adjustForTestUnderMask(SelectionDAG &DAG, Comparison &C) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001486 // Check that we have a comparison with a constant.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001487 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001488 if (!ConstOp1)
Richard Sandiford35b9be22013-08-28 10:31:43 +00001489 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001490 uint64_t CmpVal = ConstOp1->getZExtValue();
Richard Sandiford35b9be22013-08-28 10:31:43 +00001491
1492 // Check whether the nonconstant input is an AND with a constant mask.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001493 Comparison NewC(C);
1494 uint64_t MaskVal;
Craig Topper062a2ba2014-04-25 05:30:21 +00001495 ConstantSDNode *Mask = nullptr;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001496 if (C.Op0.getOpcode() == ISD::AND) {
1497 NewC.Op0 = C.Op0.getOperand(0);
1498 NewC.Op1 = C.Op0.getOperand(1);
1499 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
1500 if (!Mask)
1501 return;
1502 MaskVal = Mask->getZExtValue();
1503 } else {
1504 // There is no instruction to compare with a 64-bit immediate
1505 // so use TMHH instead if possible. We need an unsigned ordered
1506 // comparison with an i64 immediate.
1507 if (NewC.Op0.getValueType() != MVT::i64 ||
1508 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
1509 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
1510 NewC.ICmpType == SystemZICMP::SignedOnly)
1511 return;
1512 // Convert LE and GT comparisons into LT and GE.
1513 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
1514 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
1515 if (CmpVal == uint64_t(-1))
1516 return;
1517 CmpVal += 1;
1518 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1519 }
1520 // If the low N bits of Op1 are zero than the low N bits of Op0 can
1521 // be masked off without changing the result.
1522 MaskVal = -(CmpVal & -CmpVal);
1523 NewC.ICmpType = SystemZICMP::UnsignedOnly;
1524 }
Richard Sandiford35b9be22013-08-28 10:31:43 +00001525
Richard Sandiford113c8702013-09-03 15:38:35 +00001526 // Check whether the combination of mask, comparison value and comparison
1527 // type are suitable.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001528 unsigned BitSize = NewC.Op0.getValueType().getSizeInBits();
Richard Sandiford030c1652013-09-13 09:09:50 +00001529 unsigned NewCCMask, ShiftVal;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001530 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
1531 NewC.Op0.getOpcode() == ISD::SHL &&
1532 isSimpleShift(NewC.Op0, ShiftVal) &&
1533 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
1534 MaskVal >> ShiftVal,
Richard Sandiford030c1652013-09-13 09:09:50 +00001535 CmpVal >> ShiftVal,
1536 SystemZICMP::Any))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001537 NewC.Op0 = NewC.Op0.getOperand(0);
1538 MaskVal >>= ShiftVal;
1539 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
1540 NewC.Op0.getOpcode() == ISD::SRL &&
1541 isSimpleShift(NewC.Op0, ShiftVal) &&
1542 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
Richard Sandiford030c1652013-09-13 09:09:50 +00001543 MaskVal << ShiftVal,
1544 CmpVal << ShiftVal,
1545 SystemZICMP::UnsignedOnly))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001546 NewC.Op0 = NewC.Op0.getOperand(0);
1547 MaskVal <<= ShiftVal;
Richard Sandiford030c1652013-09-13 09:09:50 +00001548 } else {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001549 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
1550 NewC.ICmpType);
Richard Sandiford030c1652013-09-13 09:09:50 +00001551 if (!NewCCMask)
1552 return;
1553 }
Richard Sandiford113c8702013-09-03 15:38:35 +00001554
Richard Sandiford35b9be22013-08-28 10:31:43 +00001555 // Go ahead and make the change.
Richard Sandifordd420f732013-12-13 15:28:45 +00001556 C.Opcode = SystemZISD::TM;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001557 C.Op0 = NewC.Op0;
1558 if (Mask && Mask->getZExtValue() == MaskVal)
1559 C.Op1 = SDValue(Mask, 0);
1560 else
1561 C.Op1 = DAG.getConstant(MaskVal, C.Op0.getValueType());
Richard Sandifordd420f732013-12-13 15:28:45 +00001562 C.CCValid = SystemZ::CCMASK_TM;
1563 C.CCMask = NewCCMask;
Richard Sandiford35b9be22013-08-28 10:31:43 +00001564}
1565
Richard Sandifordd420f732013-12-13 15:28:45 +00001566// Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
1567static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
1568 ISD::CondCode Cond) {
1569 Comparison C(CmpOp0, CmpOp1);
1570 C.CCMask = CCMaskForCondCode(Cond);
1571 if (C.Op0.getValueType().isFloatingPoint()) {
1572 C.CCValid = SystemZ::CCMASK_FCMP;
1573 C.Opcode = SystemZISD::FCMP;
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001574 adjustForFNeg(C);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001575 } else {
Richard Sandifordd420f732013-12-13 15:28:45 +00001576 C.CCValid = SystemZ::CCMASK_ICMP;
1577 C.Opcode = SystemZISD::ICMP;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001578 // Choose the type of comparison. Equality and inequality tests can
1579 // use either signed or unsigned comparisons. The choice also doesn't
1580 // matter if both sign bits are known to be clear. In those cases we
1581 // want to give the main isel code the freedom to choose whichever
1582 // form fits best.
Richard Sandifordd420f732013-12-13 15:28:45 +00001583 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1584 C.CCMask == SystemZ::CCMASK_CMP_NE ||
1585 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
1586 C.ICmpType = SystemZICMP::Any;
1587 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
1588 C.ICmpType = SystemZICMP::UnsignedOnly;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001589 else
Richard Sandifordd420f732013-12-13 15:28:45 +00001590 C.ICmpType = SystemZICMP::SignedOnly;
1591 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
1592 adjustZeroCmp(DAG, C);
1593 adjustSubwordCmp(DAG, C);
Richard Sandiford0847c452013-12-13 15:50:30 +00001594 adjustForSubtraction(DAG, C);
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001595 adjustForLTGFR(C);
1596 adjustICmpTruncate(DAG, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001597 }
1598
Richard Sandifordd420f732013-12-13 15:28:45 +00001599 if (shouldSwapCmpOperands(C)) {
1600 std::swap(C.Op0, C.Op1);
1601 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford24e597b2013-08-23 11:27:19 +00001602 }
1603
Richard Sandifordd420f732013-12-13 15:28:45 +00001604 adjustForTestUnderMask(DAG, C);
Richard Sandifordd420f732013-12-13 15:28:45 +00001605 return C;
1606}
1607
1608// Emit the comparison instruction described by C.
1609static SDValue emitCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
1610 if (C.Opcode == SystemZISD::ICMP)
1611 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
1612 DAG.getConstant(C.ICmpType, MVT::i32));
1613 if (C.Opcode == SystemZISD::TM) {
1614 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
1615 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
1616 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
1617 DAG.getConstant(RegisterOnly, MVT::i32));
1618 }
1619 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001620}
1621
Richard Sandiford7d86e472013-08-21 09:34:56 +00001622// Implement a 32-bit *MUL_LOHI operation by extending both operands to
1623// 64 bits. Extend is the extension type to use. Store the high part
1624// in Hi and the low part in Lo.
1625static void lowerMUL_LOHI32(SelectionDAG &DAG, SDLoc DL,
1626 unsigned Extend, SDValue Op0, SDValue Op1,
1627 SDValue &Hi, SDValue &Lo) {
1628 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
1629 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
1630 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
1631 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul, DAG.getConstant(32, MVT::i64));
1632 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
1633 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
1634}
1635
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001636// Lower a binary operation that produces two VT results, one in each
1637// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
1638// Extend extends Op0 to a GR128, and Opcode performs the GR128 operation
1639// on the extended Op0 and (unextended) Op1. Store the even register result
1640// in Even and the odd register result in Odd.
Andrew Trickef9de2a2013-05-25 02:42:55 +00001641static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001642 unsigned Extend, unsigned Opcode,
1643 SDValue Op0, SDValue Op1,
1644 SDValue &Even, SDValue &Odd) {
1645 SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0);
1646 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped,
1647 SDValue(In128, 0), Op1);
1648 bool Is32Bit = is32Bit(VT);
Richard Sandifordd8163202013-09-13 09:12:44 +00001649 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
1650 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001651}
1652
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001653// Return an i32 value that is 1 if the CC value produced by Glue is
1654// in the mask CCMask and 0 otherwise. CC is known to have a value
1655// in CCValid, so other values can be ignored.
1656static SDValue emitSETCC(SelectionDAG &DAG, SDLoc DL, SDValue Glue,
1657 unsigned CCValid, unsigned CCMask) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001658 IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
1659 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
1660
1661 if (Conversion.XORValue)
1662 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
1663 DAG.getConstant(Conversion.XORValue, MVT::i32));
1664
1665 if (Conversion.AddValue)
1666 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
1667 DAG.getConstant(Conversion.AddValue, MVT::i32));
1668
1669 // The SHR/AND sequence should get optimized to an RISBG.
1670 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
1671 DAG.getConstant(Conversion.Bit, MVT::i32));
1672 if (Conversion.Bit != 31)
1673 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
1674 DAG.getConstant(1, MVT::i32));
1675 return Result;
1676}
1677
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001678SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
1679 SelectionDAG &DAG) const {
1680 SDValue CmpOp0 = Op.getOperand(0);
1681 SDValue CmpOp1 = Op.getOperand(1);
1682 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
1683 SDLoc DL(Op);
1684
Richard Sandifordd420f732013-12-13 15:28:45 +00001685 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC));
1686 SDValue Glue = emitCmp(DAG, DL, C);
1687 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001688}
1689
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001690SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
1691 SDValue Chain = Op.getOperand(0);
1692 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1693 SDValue CmpOp0 = Op.getOperand(2);
1694 SDValue CmpOp1 = Op.getOperand(3);
1695 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001696 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001697
Richard Sandifordd420f732013-12-13 15:28:45 +00001698 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC));
1699 SDValue Glue = emitCmp(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001700 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
Richard Sandifordd420f732013-12-13 15:28:45 +00001701 Chain, DAG.getConstant(C.CCValid, MVT::i32),
1702 DAG.getConstant(C.CCMask, MVT::i32), Dest, Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001703}
1704
Richard Sandiford57485472013-12-13 15:35:00 +00001705// Return true if Pos is CmpOp and Neg is the negative of CmpOp,
1706// allowing Pos and Neg to be wider than CmpOp.
1707static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
1708 return (Neg.getOpcode() == ISD::SUB &&
1709 Neg.getOperand(0).getOpcode() == ISD::Constant &&
1710 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
1711 Neg.getOperand(1) == Pos &&
1712 (Pos == CmpOp ||
1713 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
1714 Pos.getOperand(0) == CmpOp)));
1715}
1716
1717// Return the absolute or negative absolute of Op; IsNegative decides which.
1718static SDValue getAbsolute(SelectionDAG &DAG, SDLoc DL, SDValue Op,
1719 bool IsNegative) {
1720 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
1721 if (IsNegative)
1722 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
1723 DAG.getConstant(0, Op.getValueType()), Op);
1724 return Op;
1725}
1726
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001727SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
1728 SelectionDAG &DAG) const {
1729 SDValue CmpOp0 = Op.getOperand(0);
1730 SDValue CmpOp1 = Op.getOperand(1);
1731 SDValue TrueOp = Op.getOperand(2);
1732 SDValue FalseOp = Op.getOperand(3);
1733 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001734 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001735
Richard Sandifordd420f732013-12-13 15:28:45 +00001736 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC));
Richard Sandiford57485472013-12-13 15:35:00 +00001737
1738 // Check for absolute and negative-absolute selections, including those
1739 // where the comparison value is sign-extended (for LPGFR and LNGFR).
1740 // This check supplements the one in DAGCombiner.
1741 if (C.Opcode == SystemZISD::ICMP &&
1742 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
1743 C.CCMask != SystemZ::CCMASK_CMP_NE &&
1744 C.Op1.getOpcode() == ISD::Constant &&
1745 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
1746 if (isAbsolute(C.Op0, TrueOp, FalseOp))
1747 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
1748 if (isAbsolute(C.Op0, FalseOp, TrueOp))
1749 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
1750 }
1751
Richard Sandifordd420f732013-12-13 15:28:45 +00001752 SDValue Glue = emitCmp(DAG, DL, C);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001753
1754 // Special case for handling -1/0 results. The shifts we use here
1755 // should get optimized with the IPM conversion sequence.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001756 auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
1757 auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001758 if (TrueC && FalseC) {
1759 int64_t TrueVal = TrueC->getSExtValue();
1760 int64_t FalseVal = FalseC->getSExtValue();
1761 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
1762 // Invert the condition if we want -1 on false.
1763 if (TrueVal == 0)
Richard Sandifordd420f732013-12-13 15:28:45 +00001764 C.CCMask ^= C.CCValid;
1765 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001766 EVT VT = Op.getValueType();
1767 // Extend the result to VT. Upper bits are ignored.
1768 if (!is32Bit(VT))
1769 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
1770 // Sign-extend from the low bit.
1771 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, MVT::i32);
1772 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
1773 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
1774 }
1775 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001776
Richard Sandiford3d768e32013-07-31 12:30:20 +00001777 SmallVector<SDValue, 5> Ops;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001778 Ops.push_back(TrueOp);
1779 Ops.push_back(FalseOp);
Richard Sandifordd420f732013-12-13 15:28:45 +00001780 Ops.push_back(DAG.getConstant(C.CCValid, MVT::i32));
1781 Ops.push_back(DAG.getConstant(C.CCMask, MVT::i32));
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00001782 Ops.push_back(Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001783
1784 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00001785 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001786}
1787
1788SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
1789 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001790 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001791 const GlobalValue *GV = Node->getGlobal();
1792 int64_t Offset = Node->getOffset();
1793 EVT PtrVT = getPointerTy();
Eric Christopher93bf97c2014-06-27 07:38:01 +00001794 Reloc::Model RM = DAG.getTarget().getRelocationModel();
1795 CodeModel::Model CM = DAG.getTarget().getCodeModel();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001796
1797 SDValue Result;
1798 if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) {
Richard Sandiford54b36912013-09-27 15:14:04 +00001799 // Assign anchors at 1<<12 byte boundaries.
1800 uint64_t Anchor = Offset & ~uint64_t(0xfff);
1801 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
1802 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1803
1804 // The offset can be folded into the address if it is aligned to a halfword.
1805 Offset -= Anchor;
1806 if (Offset != 0 && (Offset & 1) == 0) {
1807 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
1808 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001809 Offset = 0;
1810 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001811 } else {
1812 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
1813 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1814 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
1815 MachinePointerInfo::getGOT(), false, false, false, 0);
1816 }
1817
1818 // If there was a non-zero offset that we didn't fold, create an explicit
1819 // addition for it.
1820 if (Offset != 0)
1821 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
1822 DAG.getConstant(Offset, PtrVT));
1823
1824 return Result;
1825}
1826
1827SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
1828 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001829 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001830 const GlobalValue *GV = Node->getGlobal();
1831 EVT PtrVT = getPointerTy();
Eric Christopher93bf97c2014-06-27 07:38:01 +00001832 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001833
1834 if (model != TLSModel::LocalExec)
1835 llvm_unreachable("only local-exec TLS mode supported");
1836
1837 // The high part of the thread pointer is in access register 0.
1838 SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
1839 DAG.getConstant(0, MVT::i32));
1840 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
1841
1842 // The low part of the thread pointer is in access register 1.
1843 SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
1844 DAG.getConstant(1, MVT::i32));
1845 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
1846
1847 // Merge them into a single 64-bit address.
1848 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
1849 DAG.getConstant(32, PtrVT));
1850 SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
1851
1852 // Get the offset of GA from the thread pointer.
1853 SystemZConstantPoolValue *CPV =
1854 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
1855
1856 // Force the offset into the constant pool and load it from there.
1857 SDValue CPAddr = DAG.getConstantPool(CPV, PtrVT, 8);
1858 SDValue Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
1859 CPAddr, MachinePointerInfo::getConstantPool(),
1860 false, false, false, 0);
1861
1862 // Add the base and offset together.
1863 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
1864}
1865
1866SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
1867 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001868 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001869 const BlockAddress *BA = Node->getBlockAddress();
1870 int64_t Offset = Node->getOffset();
1871 EVT PtrVT = getPointerTy();
1872
1873 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
1874 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1875 return Result;
1876}
1877
1878SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
1879 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001880 SDLoc DL(JT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001881 EVT PtrVT = getPointerTy();
1882 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1883
1884 // Use LARL to load the address of the table.
1885 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1886}
1887
1888SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
1889 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001890 SDLoc DL(CP);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001891 EVT PtrVT = getPointerTy();
1892
1893 SDValue Result;
1894 if (CP->isMachineConstantPoolEntry())
1895 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1896 CP->getAlignment());
1897 else
1898 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1899 CP->getAlignment(), CP->getOffset());
1900
1901 // Use LARL to load the address of the constant pool entry.
1902 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1903}
1904
1905SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
1906 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001907 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001908 SDValue In = Op.getOperand(0);
1909 EVT InVT = In.getValueType();
1910 EVT ResVT = Op.getValueType();
1911
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001912 if (InVT == MVT::i32 && ResVT == MVT::f32) {
Richard Sandifordf6377fb2013-10-01 14:31:11 +00001913 SDValue In64;
1914 if (Subtarget.hasHighWord()) {
1915 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
1916 MVT::i64);
1917 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
1918 MVT::i64, SDValue(U64, 0), In);
1919 } else {
1920 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
1921 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
1922 DAG.getConstant(32, MVT::i64));
1923 }
1924 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
Richard Sandiford87a44362013-09-30 10:28:35 +00001925 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32,
Richard Sandifordd8163202013-09-13 09:12:44 +00001926 DL, MVT::f32, Out64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001927 }
1928 if (InVT == MVT::f32 && ResVT == MVT::i32) {
1929 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
Richard Sandiford87a44362013-09-30 10:28:35 +00001930 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00001931 MVT::f64, SDValue(U64, 0), In);
1932 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
Richard Sandifordf6377fb2013-10-01 14:31:11 +00001933 if (Subtarget.hasHighWord())
1934 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
1935 MVT::i32, Out64);
1936 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
1937 DAG.getConstant(32, MVT::i64));
1938 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001939 }
1940 llvm_unreachable("Unexpected bitcast combination");
1941}
1942
1943SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
1944 SelectionDAG &DAG) const {
1945 MachineFunction &MF = DAG.getMachineFunction();
1946 SystemZMachineFunctionInfo *FuncInfo =
1947 MF.getInfo<SystemZMachineFunctionInfo>();
1948 EVT PtrVT = getPointerTy();
1949
1950 SDValue Chain = Op.getOperand(0);
1951 SDValue Addr = Op.getOperand(1);
1952 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001953 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001954
1955 // The initial values of each field.
1956 const unsigned NumFields = 4;
1957 SDValue Fields[NumFields] = {
1958 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), PtrVT),
1959 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), PtrVT),
1960 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
1961 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
1962 };
1963
1964 // Store each field into its respective slot.
1965 SDValue MemOps[NumFields];
1966 unsigned Offset = 0;
1967 for (unsigned I = 0; I < NumFields; ++I) {
1968 SDValue FieldAddr = Addr;
1969 if (Offset != 0)
1970 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
1971 DAG.getIntPtrConstant(Offset));
1972 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
1973 MachinePointerInfo(SV, Offset),
1974 false, false, 0);
1975 Offset += 8;
1976 }
Craig Topper48d114b2014-04-26 18:35:24 +00001977 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001978}
1979
1980SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
1981 SelectionDAG &DAG) const {
1982 SDValue Chain = Op.getOperand(0);
1983 SDValue DstPtr = Op.getOperand(1);
1984 SDValue SrcPtr = Op.getOperand(2);
1985 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
1986 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001987 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001988
1989 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32),
1990 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
1991 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
1992}
1993
1994SDValue SystemZTargetLowering::
1995lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
1996 SDValue Chain = Op.getOperand(0);
1997 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001998 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001999
2000 unsigned SPReg = getStackPointerRegisterToSaveRestore();
2001
2002 // Get a reference to the stack pointer.
2003 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
2004
2005 // Get the new stack pointer value.
2006 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, Size);
2007
2008 // Copy the new stack pointer back.
2009 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
2010
2011 // The allocated data lives above the 160 bytes allocated for the standard
2012 // frame, plus any outgoing stack arguments. We don't know how much that
2013 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
2014 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
2015 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
2016
2017 SDValue Ops[2] = { Result, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002018 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002019}
2020
Richard Sandiford7d86e472013-08-21 09:34:56 +00002021SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
2022 SelectionDAG &DAG) const {
2023 EVT VT = Op.getValueType();
2024 SDLoc DL(Op);
2025 SDValue Ops[2];
2026 if (is32Bit(VT))
2027 // Just do a normal 64-bit multiplication and extract the results.
2028 // We define this so that it can be used for constant division.
2029 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
2030 Op.getOperand(1), Ops[1], Ops[0]);
2031 else {
2032 // Do a full 128-bit multiplication based on UMUL_LOHI64:
2033 //
2034 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
2035 //
2036 // but using the fact that the upper halves are either all zeros
2037 // or all ones:
2038 //
2039 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
2040 //
2041 // and grouping the right terms together since they are quicker than the
2042 // multiplication:
2043 //
2044 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
2045 SDValue C63 = DAG.getConstant(63, MVT::i64);
2046 SDValue LL = Op.getOperand(0);
2047 SDValue RL = Op.getOperand(1);
2048 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
2049 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
2050 // UMUL_LOHI64 returns the low result in the odd register and the high
2051 // result in the even register. SMUL_LOHI is defined to return the
2052 // low half first, so the results are in reverse order.
2053 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2054 LL, RL, Ops[1], Ops[0]);
2055 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
2056 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
2057 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
2058 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
2059 }
Craig Topper64941d92014-04-27 19:20:57 +00002060 return DAG.getMergeValues(Ops, DL);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002061}
2062
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002063SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
2064 SelectionDAG &DAG) const {
2065 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002066 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002067 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00002068 if (is32Bit(VT))
2069 // Just do a normal 64-bit multiplication and extract the results.
2070 // We define this so that it can be used for constant division.
2071 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
2072 Op.getOperand(1), Ops[1], Ops[0]);
2073 else
2074 // UMUL_LOHI64 returns the low result in the odd register and the high
2075 // result in the even register. UMUL_LOHI is defined to return the
2076 // low half first, so the results are in reverse order.
2077 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2078 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002079 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002080}
2081
2082SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
2083 SelectionDAG &DAG) const {
2084 SDValue Op0 = Op.getOperand(0);
2085 SDValue Op1 = Op.getOperand(1);
2086 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002087 SDLoc DL(Op);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002088 unsigned Opcode;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002089
2090 // We use DSGF for 32-bit division.
2091 if (is32Bit(VT)) {
2092 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002093 Opcode = SystemZISD::SDIVREM32;
2094 } else if (DAG.ComputeNumSignBits(Op1) > 32) {
2095 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
2096 Opcode = SystemZISD::SDIVREM32;
2097 } else
2098 Opcode = SystemZISD::SDIVREM64;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002099
2100 // DSG(F) takes a 64-bit dividend, so the even register in the GR128
2101 // input is "don't care". The instruction returns the remainder in
2102 // the even register and the quotient in the odd register.
2103 SDValue Ops[2];
Richard Sandiforde6e78852013-07-02 15:40:22 +00002104 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002105 Op0, Op1, Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002106 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002107}
2108
2109SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
2110 SelectionDAG &DAG) const {
2111 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002112 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002113
2114 // DL(G) uses a double-width dividend, so we need to clear the even
2115 // register in the GR128 input. The instruction returns the remainder
2116 // in the even register and the quotient in the odd register.
2117 SDValue Ops[2];
2118 if (is32Bit(VT))
2119 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
2120 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2121 else
2122 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
2123 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002124 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002125}
2126
2127SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
2128 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
2129
2130 // Get the known-zero masks for each operand.
2131 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
2132 APInt KnownZero[2], KnownOne[2];
Jay Foada0653a32014-05-14 21:14:37 +00002133 DAG.computeKnownBits(Ops[0], KnownZero[0], KnownOne[0]);
2134 DAG.computeKnownBits(Ops[1], KnownZero[1], KnownOne[1]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002135
2136 // See if the upper 32 bits of one operand and the lower 32 bits of the
2137 // other are known zero. They are the low and high operands respectively.
2138 uint64_t Masks[] = { KnownZero[0].getZExtValue(),
2139 KnownZero[1].getZExtValue() };
2140 unsigned High, Low;
2141 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
2142 High = 1, Low = 0;
2143 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
2144 High = 0, Low = 1;
2145 else
2146 return Op;
2147
2148 SDValue LowOp = Ops[Low];
2149 SDValue HighOp = Ops[High];
2150
2151 // If the high part is a constant, we're better off using IILH.
2152 if (HighOp.getOpcode() == ISD::Constant)
2153 return Op;
2154
2155 // If the low part is a constant that is outside the range of LHI,
2156 // then we're better off using IILF.
2157 if (LowOp.getOpcode() == ISD::Constant) {
2158 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
2159 if (!isInt<16>(Value))
2160 return Op;
2161 }
2162
2163 // Check whether the high part is an AND that doesn't change the
2164 // high 32 bits and just masks out low bits. We can skip it if so.
2165 if (HighOp.getOpcode() == ISD::AND &&
2166 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
Richard Sandifordccc2a7c2013-12-03 11:01:54 +00002167 SDValue HighOp0 = HighOp.getOperand(0);
2168 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
2169 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
2170 HighOp = HighOp0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002171 }
2172
2173 // Take advantage of the fact that all GR32 operations only change the
2174 // low 32 bits by truncating Low to an i32 and inserting it directly
2175 // using a subreg. The interesting cases are those where the truncation
2176 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002177 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002178 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
Richard Sandiford87a44362013-09-30 10:28:35 +00002179 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002180 MVT::i64, HighOp, Low32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002181}
2182
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002183// Op is an atomic load. Lower it into a normal volatile load.
2184SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
2185 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002186 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002187 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
2188 Node->getChain(), Node->getBasePtr(),
2189 Node->getMemoryVT(), Node->getMemOperand());
2190}
2191
2192// Op is an atomic store. Lower it into a normal volatile store followed
2193// by a serialization.
2194SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
2195 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002196 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002197 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
2198 Node->getBasePtr(), Node->getMemoryVT(),
2199 Node->getMemOperand());
2200 return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other,
2201 Chain), 0);
2202}
2203
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002204// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
2205// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002206SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
2207 SelectionDAG &DAG,
2208 unsigned Opcode) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002209 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002210
2211 // 32-bit operations need no code outside the main loop.
2212 EVT NarrowVT = Node->getMemoryVT();
2213 EVT WideVT = MVT::i32;
2214 if (NarrowVT == WideVT)
2215 return Op;
2216
2217 int64_t BitSize = NarrowVT.getSizeInBits();
2218 SDValue ChainIn = Node->getChain();
2219 SDValue Addr = Node->getBasePtr();
2220 SDValue Src2 = Node->getVal();
2221 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002222 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002223 EVT PtrVT = Addr.getValueType();
2224
2225 // Convert atomic subtracts of constants into additions.
2226 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
Richard Sandiford21f5d682014-03-06 11:22:58 +00002227 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002228 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
2229 Src2 = DAG.getConstant(-Const->getSExtValue(), Src2.getValueType());
2230 }
2231
2232 // Get the address of the containing word.
2233 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
2234 DAG.getConstant(-4, PtrVT));
2235
2236 // Get the number of bits that the word must be rotated left in order
2237 // to bring the field to the top bits of a GR32.
2238 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
2239 DAG.getConstant(3, PtrVT));
2240 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
2241
2242 // Get the complementing shift amount, for rotating a field in the top
2243 // bits back to its proper position.
2244 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
2245 DAG.getConstant(0, WideVT), BitShift);
2246
2247 // Extend the source operand to 32 bits and prepare it for the inner loop.
2248 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
2249 // operations require the source to be shifted in advance. (This shift
2250 // can be folded if the source is constant.) For AND and NAND, the lower
2251 // bits must be set, while for other opcodes they should be left clear.
2252 if (Opcode != SystemZISD::ATOMIC_SWAPW)
2253 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
2254 DAG.getConstant(32 - BitSize, WideVT));
2255 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
2256 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
2257 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
2258 DAG.getConstant(uint32_t(-1) >> BitSize, WideVT));
2259
2260 // Construct the ATOMIC_LOADW_* node.
2261 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
2262 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
2263 DAG.getConstant(BitSize, WideVT) };
2264 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002265 NarrowVT, MMO);
2266
2267 // Rotate the result of the final CS so that the field is in the lower
2268 // bits of a GR32, then truncate it.
2269 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
2270 DAG.getConstant(BitSize, WideVT));
2271 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
2272
2273 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002274 return DAG.getMergeValues(RetOps, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002275}
2276
Richard Sandiford41350a52013-12-24 15:18:04 +00002277// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
Richard Sandiford002019a2013-12-24 15:22:39 +00002278// into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
Richard Sandiford41350a52013-12-24 15:18:04 +00002279// operations into additions.
2280SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
2281 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002282 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandiford41350a52013-12-24 15:18:04 +00002283 EVT MemVT = Node->getMemoryVT();
2284 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
2285 // A full-width operation.
2286 assert(Op.getValueType() == MemVT && "Mismatched VTs");
2287 SDValue Src2 = Node->getVal();
2288 SDValue NegSrc2;
2289 SDLoc DL(Src2);
2290
Richard Sandiford21f5d682014-03-06 11:22:58 +00002291 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
Richard Sandiford41350a52013-12-24 15:18:04 +00002292 // Use an addition if the operand is constant and either LAA(G) is
2293 // available or the negative value is in the range of A(G)FHI.
2294 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
Eric Christopher93bf97c2014-06-27 07:38:01 +00002295 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00002296 NegSrc2 = DAG.getConstant(Value, MemVT);
Eric Christopher93bf97c2014-06-27 07:38:01 +00002297 } else if (Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00002298 // Use LAA(G) if available.
2299 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, MemVT),
2300 Src2);
2301
2302 if (NegSrc2.getNode())
2303 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
2304 Node->getChain(), Node->getBasePtr(), NegSrc2,
2305 Node->getMemOperand(), Node->getOrdering(),
2306 Node->getSynchScope());
2307
2308 // Use the node as-is.
2309 return Op;
2310 }
2311
2312 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
2313}
2314
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002315// Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
2316// into a fullword ATOMIC_CMP_SWAPW operation.
2317SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
2318 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002319 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002320
2321 // We have native support for 32-bit compare and swap.
2322 EVT NarrowVT = Node->getMemoryVT();
2323 EVT WideVT = MVT::i32;
2324 if (NarrowVT == WideVT)
2325 return Op;
2326
2327 int64_t BitSize = NarrowVT.getSizeInBits();
2328 SDValue ChainIn = Node->getOperand(0);
2329 SDValue Addr = Node->getOperand(1);
2330 SDValue CmpVal = Node->getOperand(2);
2331 SDValue SwapVal = Node->getOperand(3);
2332 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002333 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002334 EVT PtrVT = Addr.getValueType();
2335
2336 // Get the address of the containing word.
2337 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
2338 DAG.getConstant(-4, PtrVT));
2339
2340 // Get the number of bits that the word must be rotated left in order
2341 // to bring the field to the top bits of a GR32.
2342 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
2343 DAG.getConstant(3, PtrVT));
2344 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
2345
2346 // Get the complementing shift amount, for rotating a field in the top
2347 // bits back to its proper position.
2348 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
2349 DAG.getConstant(0, WideVT), BitShift);
2350
2351 // Construct the ATOMIC_CMP_SWAPW node.
2352 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
2353 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
2354 NegBitShift, DAG.getConstant(BitSize, WideVT) };
2355 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00002356 VTList, Ops, NarrowVT, MMO);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002357 return AtomicOp;
2358}
2359
2360SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
2361 SelectionDAG &DAG) const {
2362 MachineFunction &MF = DAG.getMachineFunction();
2363 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002364 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002365 SystemZ::R15D, Op.getValueType());
2366}
2367
2368SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
2369 SelectionDAG &DAG) const {
2370 MachineFunction &MF = DAG.getMachineFunction();
2371 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002372 return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002373 SystemZ::R15D, Op.getOperand(1));
2374}
2375
Richard Sandiford03481332013-08-23 11:36:42 +00002376SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
2377 SelectionDAG &DAG) const {
2378 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2379 if (!IsData)
2380 // Just preserve the chain.
2381 return Op.getOperand(0);
2382
2383 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2384 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
Richard Sandiford21f5d682014-03-06 11:22:58 +00002385 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
Richard Sandiford03481332013-08-23 11:36:42 +00002386 SDValue Ops[] = {
2387 Op.getOperand(0),
2388 DAG.getConstant(Code, MVT::i32),
2389 Op.getOperand(1)
2390 };
2391 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, SDLoc(Op),
Craig Topper206fcd42014-04-26 19:29:41 +00002392 Node->getVTList(), Ops,
Richard Sandiford03481332013-08-23 11:36:42 +00002393 Node->getMemoryVT(), Node->getMemOperand());
2394}
2395
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002396SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
2397 SelectionDAG &DAG) const {
2398 switch (Op.getOpcode()) {
2399 case ISD::BR_CC:
2400 return lowerBR_CC(Op, DAG);
2401 case ISD::SELECT_CC:
2402 return lowerSELECT_CC(Op, DAG);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002403 case ISD::SETCC:
2404 return lowerSETCC(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002405 case ISD::GlobalAddress:
2406 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
2407 case ISD::GlobalTLSAddress:
2408 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
2409 case ISD::BlockAddress:
2410 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
2411 case ISD::JumpTable:
2412 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
2413 case ISD::ConstantPool:
2414 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
2415 case ISD::BITCAST:
2416 return lowerBITCAST(Op, DAG);
2417 case ISD::VASTART:
2418 return lowerVASTART(Op, DAG);
2419 case ISD::VACOPY:
2420 return lowerVACOPY(Op, DAG);
2421 case ISD::DYNAMIC_STACKALLOC:
2422 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002423 case ISD::SMUL_LOHI:
2424 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002425 case ISD::UMUL_LOHI:
2426 return lowerUMUL_LOHI(Op, DAG);
2427 case ISD::SDIVREM:
2428 return lowerSDIVREM(Op, DAG);
2429 case ISD::UDIVREM:
2430 return lowerUDIVREM(Op, DAG);
2431 case ISD::OR:
2432 return lowerOR(Op, DAG);
2433 case ISD::ATOMIC_SWAP:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002434 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
2435 case ISD::ATOMIC_STORE:
2436 return lowerATOMIC_STORE(Op, DAG);
2437 case ISD::ATOMIC_LOAD:
2438 return lowerATOMIC_LOAD(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002439 case ISD::ATOMIC_LOAD_ADD:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002440 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002441 case ISD::ATOMIC_LOAD_SUB:
Richard Sandiford41350a52013-12-24 15:18:04 +00002442 return lowerATOMIC_LOAD_SUB(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002443 case ISD::ATOMIC_LOAD_AND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002444 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002445 case ISD::ATOMIC_LOAD_OR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002446 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002447 case ISD::ATOMIC_LOAD_XOR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002448 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002449 case ISD::ATOMIC_LOAD_NAND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002450 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002451 case ISD::ATOMIC_LOAD_MIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002452 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002453 case ISD::ATOMIC_LOAD_MAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002454 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002455 case ISD::ATOMIC_LOAD_UMIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002456 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002457 case ISD::ATOMIC_LOAD_UMAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00002458 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002459 case ISD::ATOMIC_CMP_SWAP:
2460 return lowerATOMIC_CMP_SWAP(Op, DAG);
2461 case ISD::STACKSAVE:
2462 return lowerSTACKSAVE(Op, DAG);
2463 case ISD::STACKRESTORE:
2464 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00002465 case ISD::PREFETCH:
2466 return lowerPREFETCH(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002467 default:
2468 llvm_unreachable("Unexpected node to lower");
2469 }
2470}
2471
2472const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
2473#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
2474 switch (Opcode) {
2475 OPCODE(RET_FLAG);
2476 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00002477 OPCODE(SIBCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002478 OPCODE(PCREL_WRAPPER);
Richard Sandiford54b36912013-09-27 15:14:04 +00002479 OPCODE(PCREL_OFFSET);
Richard Sandiford57485472013-12-13 15:35:00 +00002480 OPCODE(IABS);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002481 OPCODE(ICMP);
2482 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00002483 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002484 OPCODE(BR_CCMASK);
2485 OPCODE(SELECT_CCMASK);
2486 OPCODE(ADJDYNALLOC);
2487 OPCODE(EXTRACT_ACCESS);
2488 OPCODE(UMUL_LOHI64);
2489 OPCODE(SDIVREM64);
2490 OPCODE(UDIVREM32);
2491 OPCODE(UDIVREM64);
Richard Sandifordd131ff82013-07-08 09:35:23 +00002492 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00002493 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00002494 OPCODE(NC);
2495 OPCODE(NC_LOOP);
2496 OPCODE(OC);
2497 OPCODE(OC_LOOP);
2498 OPCODE(XC);
2499 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00002500 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00002501 OPCODE(CLC_LOOP);
Richard Sandifordca232712013-08-16 11:21:54 +00002502 OPCODE(STRCMP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00002503 OPCODE(STPCPY);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00002504 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00002505 OPCODE(IPM);
Richard Sandiford9afe6132013-12-10 10:36:34 +00002506 OPCODE(SERIALIZE);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002507 OPCODE(ATOMIC_SWAPW);
2508 OPCODE(ATOMIC_LOADW_ADD);
2509 OPCODE(ATOMIC_LOADW_SUB);
2510 OPCODE(ATOMIC_LOADW_AND);
2511 OPCODE(ATOMIC_LOADW_OR);
2512 OPCODE(ATOMIC_LOADW_XOR);
2513 OPCODE(ATOMIC_LOADW_NAND);
2514 OPCODE(ATOMIC_LOADW_MIN);
2515 OPCODE(ATOMIC_LOADW_MAX);
2516 OPCODE(ATOMIC_LOADW_UMIN);
2517 OPCODE(ATOMIC_LOADW_UMAX);
2518 OPCODE(ATOMIC_CMP_SWAPW);
Richard Sandiford03481332013-08-23 11:36:42 +00002519 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002520 }
Craig Topper062a2ba2014-04-25 05:30:21 +00002521 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002522#undef OPCODE
2523}
2524
Richard Sandiford95bc5f92014-03-07 11:34:35 +00002525SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
2526 DAGCombinerInfo &DCI) const {
2527 SelectionDAG &DAG = DCI.DAG;
2528 unsigned Opcode = N->getOpcode();
2529 if (Opcode == ISD::SIGN_EXTEND) {
2530 // Convert (sext (ashr (shl X, C1), C2)) to
2531 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
2532 // cheap as narrower ones.
2533 SDValue N0 = N->getOperand(0);
2534 EVT VT = N->getValueType(0);
2535 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
2536 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2537 SDValue Inner = N0.getOperand(0);
2538 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
2539 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
2540 unsigned Extra = (VT.getSizeInBits() -
2541 N0.getValueType().getSizeInBits());
2542 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
2543 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
2544 EVT ShiftVT = N0.getOperand(1).getValueType();
2545 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
2546 Inner.getOperand(0));
2547 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
2548 DAG.getConstant(NewShlAmt, ShiftVT));
2549 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
2550 DAG.getConstant(NewSraAmt, ShiftVT));
2551 }
2552 }
2553 }
2554 }
2555 return SDValue();
2556}
2557
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002558//===----------------------------------------------------------------------===//
2559// Custom insertion
2560//===----------------------------------------------------------------------===//
2561
2562// Create a new basic block after MBB.
2563static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
2564 MachineFunction &MF = *MBB->getParent();
2565 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002566 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002567 return NewMBB;
2568}
2569
Richard Sandifordbe133a82013-08-28 09:01:51 +00002570// Split MBB after MI and return the new block (the one that contains
2571// instructions after MI).
2572static MachineBasicBlock *splitBlockAfter(MachineInstr *MI,
2573 MachineBasicBlock *MBB) {
2574 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
2575 NewMBB->splice(NewMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002576 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Richard Sandifordbe133a82013-08-28 09:01:51 +00002577 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
2578 return NewMBB;
2579}
2580
Richard Sandiford5e318f02013-08-27 09:54:29 +00002581// Split MBB before MI and return the new block (the one that contains MI).
2582static MachineBasicBlock *splitBlockBefore(MachineInstr *MI,
2583 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002584 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00002585 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002586 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
2587 return NewMBB;
2588}
2589
Richard Sandiford5e318f02013-08-27 09:54:29 +00002590// Force base value Base into a register before MI. Return the register.
2591static unsigned forceReg(MachineInstr *MI, MachineOperand &Base,
2592 const SystemZInstrInfo *TII) {
2593 if (Base.isReg())
2594 return Base.getReg();
2595
2596 MachineBasicBlock *MBB = MI->getParent();
2597 MachineFunction &MF = *MBB->getParent();
2598 MachineRegisterInfo &MRI = MF.getRegInfo();
2599
2600 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
2601 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg)
2602 .addOperand(Base).addImm(0).addReg(0);
2603 return Reg;
2604}
2605
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002606// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
2607MachineBasicBlock *
2608SystemZTargetLowering::emitSelect(MachineInstr *MI,
2609 MachineBasicBlock *MBB) const {
Eric Christopher93bf97c2014-06-27 07:38:01 +00002610 const SystemZInstrInfo *TII = static_cast<const SystemZInstrInfo *>(
2611 MBB->getParent()->getTarget().getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002612
2613 unsigned DestReg = MI->getOperand(0).getReg();
2614 unsigned TrueReg = MI->getOperand(1).getReg();
2615 unsigned FalseReg = MI->getOperand(2).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00002616 unsigned CCValid = MI->getOperand(3).getImm();
2617 unsigned CCMask = MI->getOperand(4).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002618 DebugLoc DL = MI->getDebugLoc();
2619
2620 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002621 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002622 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
2623
2624 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00002625 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002626 // # fallthrough to FalseMBB
2627 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00002628 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2629 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002630 MBB->addSuccessor(JoinMBB);
2631 MBB->addSuccessor(FalseMBB);
2632
2633 // FalseMBB:
2634 // # fallthrough to JoinMBB
2635 MBB = FalseMBB;
2636 MBB->addSuccessor(JoinMBB);
2637
2638 // JoinMBB:
2639 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
2640 // ...
2641 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002642 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002643 .addReg(TrueReg).addMBB(StartMBB)
2644 .addReg(FalseReg).addMBB(FalseMBB);
2645
2646 MI->eraseFromParent();
2647 return JoinMBB;
2648}
2649
Richard Sandifordb86a8342013-06-27 09:27:40 +00002650// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
2651// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002652// happen when the condition is false rather than true. If a STORE ON
2653// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Richard Sandifordb86a8342013-06-27 09:27:40 +00002654MachineBasicBlock *
2655SystemZTargetLowering::emitCondStore(MachineInstr *MI,
2656 MachineBasicBlock *MBB,
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002657 unsigned StoreOpcode, unsigned STOCOpcode,
2658 bool Invert) const {
Eric Christopher93bf97c2014-06-27 07:38:01 +00002659 const SystemZInstrInfo *TII = static_cast<const SystemZInstrInfo *>(
2660 MBB->getParent()->getTarget().getInstrInfo());
Richard Sandifordb86a8342013-06-27 09:27:40 +00002661
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002662 unsigned SrcReg = MI->getOperand(0).getReg();
2663 MachineOperand Base = MI->getOperand(1);
2664 int64_t Disp = MI->getOperand(2).getImm();
2665 unsigned IndexReg = MI->getOperand(3).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00002666 unsigned CCValid = MI->getOperand(4).getImm();
2667 unsigned CCMask = MI->getOperand(5).getImm();
Richard Sandifordb86a8342013-06-27 09:27:40 +00002668 DebugLoc DL = MI->getDebugLoc();
2669
2670 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
2671
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002672 // Use STOCOpcode if possible. We could use different store patterns in
2673 // order to avoid matching the index register, but the performance trade-offs
2674 // might be more complicated in that case.
Eric Christopher93bf97c2014-06-27 07:38:01 +00002675 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002676 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00002677 CCMask ^= CCValid;
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002678 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +00002679 .addReg(SrcReg).addOperand(Base).addImm(Disp)
2680 .addImm(CCValid).addImm(CCMask);
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002681 MI->eraseFromParent();
2682 return MBB;
2683 }
2684
Richard Sandifordb86a8342013-06-27 09:27:40 +00002685 // Get the condition needed to branch around the store.
2686 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00002687 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00002688
2689 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002690 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002691 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
2692
2693 // StartMBB:
2694 // BRC CCMask, JoinMBB
2695 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00002696 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00002697 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2698 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002699 MBB->addSuccessor(JoinMBB);
2700 MBB->addSuccessor(FalseMBB);
2701
2702 // FalseMBB:
2703 // store %SrcReg, %Disp(%Index,%Base)
2704 // # fallthrough to JoinMBB
2705 MBB = FalseMBB;
2706 BuildMI(MBB, DL, TII->get(StoreOpcode))
2707 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
2708 MBB->addSuccessor(JoinMBB);
2709
2710 MI->eraseFromParent();
2711 return JoinMBB;
2712}
2713
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002714// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
2715// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
2716// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
2717// BitSize is the width of the field in bits, or 0 if this is a partword
2718// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
2719// is one of the operands. Invert says whether the field should be
2720// inverted after performing BinOpcode (e.g. for NAND).
2721MachineBasicBlock *
2722SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI,
2723 MachineBasicBlock *MBB,
2724 unsigned BinOpcode,
2725 unsigned BitSize,
2726 bool Invert) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002727 MachineFunction &MF = *MBB->getParent();
Eric Christopher93bf97c2014-06-27 07:38:01 +00002728 const SystemZInstrInfo *TII =
2729 static_cast<const SystemZInstrInfo *>(MF.getTarget().getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002730 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002731 bool IsSubWord = (BitSize < 32);
2732
2733 // Extract the operands. Base can be a register or a frame index.
2734 // Src2 can be a register or immediate.
2735 unsigned Dest = MI->getOperand(0).getReg();
2736 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2737 int64_t Disp = MI->getOperand(2).getImm();
2738 MachineOperand Src2 = earlyUseOperand(MI->getOperand(3));
2739 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
2740 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
2741 DebugLoc DL = MI->getDebugLoc();
2742 if (IsSubWord)
2743 BitSize = MI->getOperand(6).getImm();
2744
2745 // Subword operations use 32-bit registers.
2746 const TargetRegisterClass *RC = (BitSize <= 32 ?
2747 &SystemZ::GR32BitRegClass :
2748 &SystemZ::GR64BitRegClass);
2749 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
2750 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
2751
2752 // Get the right opcodes for the displacement.
2753 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
2754 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
2755 assert(LOpcode && CSOpcode && "Displacement out of range");
2756
2757 // Create virtual registers for temporary results.
2758 unsigned OrigVal = MRI.createVirtualRegister(RC);
2759 unsigned OldVal = MRI.createVirtualRegister(RC);
2760 unsigned NewVal = (BinOpcode || IsSubWord ?
2761 MRI.createVirtualRegister(RC) : Src2.getReg());
2762 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
2763 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
2764
2765 // Insert a basic block for the main loop.
2766 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002767 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002768 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2769
2770 // StartMBB:
2771 // ...
2772 // %OrigVal = L Disp(%Base)
2773 // # fall through to LoopMMB
2774 MBB = StartMBB;
2775 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
2776 .addOperand(Base).addImm(Disp).addReg(0);
2777 MBB->addSuccessor(LoopMBB);
2778
2779 // LoopMBB:
2780 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
2781 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
2782 // %RotatedNewVal = OP %RotatedOldVal, %Src2
2783 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
2784 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
2785 // JNE LoopMBB
2786 // # fall through to DoneMMB
2787 MBB = LoopMBB;
2788 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2789 .addReg(OrigVal).addMBB(StartMBB)
2790 .addReg(Dest).addMBB(LoopMBB);
2791 if (IsSubWord)
2792 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
2793 .addReg(OldVal).addReg(BitShift).addImm(0);
2794 if (Invert) {
2795 // Perform the operation normally and then invert every bit of the field.
2796 unsigned Tmp = MRI.createVirtualRegister(RC);
2797 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
2798 .addReg(RotatedOldVal).addOperand(Src2);
2799 if (BitSize < 32)
2800 // XILF with the upper BitSize bits set.
Richard Sandiford652784e2013-09-25 11:11:53 +00002801 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002802 .addReg(Tmp).addImm(uint32_t(~0 << (32 - BitSize)));
2803 else if (BitSize == 32)
2804 // XILF with every bit set.
Richard Sandiford652784e2013-09-25 11:11:53 +00002805 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002806 .addReg(Tmp).addImm(~uint32_t(0));
2807 else {
2808 // Use LCGR and add -1 to the result, which is more compact than
2809 // an XILF, XILH pair.
2810 unsigned Tmp2 = MRI.createVirtualRegister(RC);
2811 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
2812 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
2813 .addReg(Tmp2).addImm(-1);
2814 }
2815 } else if (BinOpcode)
2816 // A simply binary operation.
2817 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
2818 .addReg(RotatedOldVal).addOperand(Src2);
2819 else if (IsSubWord)
2820 // Use RISBG to rotate Src2 into position and use it to replace the
2821 // field in RotatedOldVal.
2822 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
2823 .addReg(RotatedOldVal).addReg(Src2.getReg())
2824 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
2825 if (IsSubWord)
2826 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
2827 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
2828 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
2829 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00002830 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2831 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002832 MBB->addSuccessor(LoopMBB);
2833 MBB->addSuccessor(DoneMBB);
2834
2835 MI->eraseFromParent();
2836 return DoneMBB;
2837}
2838
2839// Implement EmitInstrWithCustomInserter for pseudo
2840// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
2841// instruction that should be used to compare the current field with the
2842// minimum or maximum value. KeepOldMask is the BRC condition-code mask
2843// for when the current field should be kept. BitSize is the width of
2844// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
2845MachineBasicBlock *
2846SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI,
2847 MachineBasicBlock *MBB,
2848 unsigned CompareOpcode,
2849 unsigned KeepOldMask,
2850 unsigned BitSize) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002851 MachineFunction &MF = *MBB->getParent();
Eric Christopher93bf97c2014-06-27 07:38:01 +00002852 const SystemZInstrInfo *TII =
2853 static_cast<const SystemZInstrInfo *>(MF.getTarget().getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002854 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002855 bool IsSubWord = (BitSize < 32);
2856
2857 // Extract the operands. Base can be a register or a frame index.
2858 unsigned Dest = MI->getOperand(0).getReg();
2859 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2860 int64_t Disp = MI->getOperand(2).getImm();
2861 unsigned Src2 = MI->getOperand(3).getReg();
2862 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
2863 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
2864 DebugLoc DL = MI->getDebugLoc();
2865 if (IsSubWord)
2866 BitSize = MI->getOperand(6).getImm();
2867
2868 // Subword operations use 32-bit registers.
2869 const TargetRegisterClass *RC = (BitSize <= 32 ?
2870 &SystemZ::GR32BitRegClass :
2871 &SystemZ::GR64BitRegClass);
2872 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
2873 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
2874
2875 // Get the right opcodes for the displacement.
2876 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
2877 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
2878 assert(LOpcode && CSOpcode && "Displacement out of range");
2879
2880 // Create virtual registers for temporary results.
2881 unsigned OrigVal = MRI.createVirtualRegister(RC);
2882 unsigned OldVal = MRI.createVirtualRegister(RC);
2883 unsigned NewVal = MRI.createVirtualRegister(RC);
2884 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
2885 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
2886 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
2887
2888 // Insert 3 basic blocks for the loop.
2889 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002890 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002891 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2892 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
2893 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
2894
2895 // StartMBB:
2896 // ...
2897 // %OrigVal = L Disp(%Base)
2898 // # fall through to LoopMMB
2899 MBB = StartMBB;
2900 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
2901 .addOperand(Base).addImm(Disp).addReg(0);
2902 MBB->addSuccessor(LoopMBB);
2903
2904 // LoopMBB:
2905 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
2906 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
2907 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00002908 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002909 MBB = LoopMBB;
2910 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2911 .addReg(OrigVal).addMBB(StartMBB)
2912 .addReg(Dest).addMBB(UpdateMBB);
2913 if (IsSubWord)
2914 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
2915 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00002916 BuildMI(MBB, DL, TII->get(CompareOpcode))
2917 .addReg(RotatedOldVal).addReg(Src2);
2918 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00002919 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002920 MBB->addSuccessor(UpdateMBB);
2921 MBB->addSuccessor(UseAltMBB);
2922
2923 // UseAltMBB:
2924 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
2925 // # fall through to UpdateMMB
2926 MBB = UseAltMBB;
2927 if (IsSubWord)
2928 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
2929 .addReg(RotatedOldVal).addReg(Src2)
2930 .addImm(32).addImm(31 + BitSize).addImm(0);
2931 MBB->addSuccessor(UpdateMBB);
2932
2933 // UpdateMBB:
2934 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
2935 // [ %RotatedAltVal, UseAltMBB ]
2936 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
2937 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
2938 // JNE LoopMBB
2939 // # fall through to DoneMMB
2940 MBB = UpdateMBB;
2941 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
2942 .addReg(RotatedOldVal).addMBB(LoopMBB)
2943 .addReg(RotatedAltVal).addMBB(UseAltMBB);
2944 if (IsSubWord)
2945 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
2946 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
2947 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
2948 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00002949 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2950 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002951 MBB->addSuccessor(LoopMBB);
2952 MBB->addSuccessor(DoneMBB);
2953
2954 MI->eraseFromParent();
2955 return DoneMBB;
2956}
2957
2958// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
2959// instruction MI.
2960MachineBasicBlock *
2961SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI,
2962 MachineBasicBlock *MBB) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002963 MachineFunction &MF = *MBB->getParent();
Eric Christopher93bf97c2014-06-27 07:38:01 +00002964 const SystemZInstrInfo *TII =
2965 static_cast<const SystemZInstrInfo *>(MF.getTarget().getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002966 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002967
2968 // Extract the operands. Base can be a register or a frame index.
2969 unsigned Dest = MI->getOperand(0).getReg();
2970 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2971 int64_t Disp = MI->getOperand(2).getImm();
2972 unsigned OrigCmpVal = MI->getOperand(3).getReg();
2973 unsigned OrigSwapVal = MI->getOperand(4).getReg();
2974 unsigned BitShift = MI->getOperand(5).getReg();
2975 unsigned NegBitShift = MI->getOperand(6).getReg();
2976 int64_t BitSize = MI->getOperand(7).getImm();
2977 DebugLoc DL = MI->getDebugLoc();
2978
2979 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
2980
2981 // Get the right opcodes for the displacement.
2982 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
2983 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
2984 assert(LOpcode && CSOpcode && "Displacement out of range");
2985
2986 // Create virtual registers for temporary results.
2987 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
2988 unsigned OldVal = MRI.createVirtualRegister(RC);
2989 unsigned CmpVal = MRI.createVirtualRegister(RC);
2990 unsigned SwapVal = MRI.createVirtualRegister(RC);
2991 unsigned StoreVal = MRI.createVirtualRegister(RC);
2992 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
2993 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
2994 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
2995
2996 // Insert 2 basic blocks for the loop.
2997 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002998 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002999 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
3000 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
3001
3002 // StartMBB:
3003 // ...
3004 // %OrigOldVal = L Disp(%Base)
3005 // # fall through to LoopMMB
3006 MBB = StartMBB;
3007 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
3008 .addOperand(Base).addImm(Disp).addReg(0);
3009 MBB->addSuccessor(LoopMBB);
3010
3011 // LoopMBB:
3012 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
3013 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
3014 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
3015 // %Dest = RLL %OldVal, BitSize(%BitShift)
3016 // ^^ The low BitSize bits contain the field
3017 // of interest.
3018 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
3019 // ^^ Replace the upper 32-BitSize bits of the
3020 // comparison value with those that we loaded,
3021 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00003022 // CR %Dest, %RetryCmpVal
3023 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003024 // # Fall through to SetMBB
3025 MBB = LoopMBB;
3026 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
3027 .addReg(OrigOldVal).addMBB(StartMBB)
3028 .addReg(RetryOldVal).addMBB(SetMBB);
3029 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
3030 .addReg(OrigCmpVal).addMBB(StartMBB)
3031 .addReg(RetryCmpVal).addMBB(SetMBB);
3032 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
3033 .addReg(OrigSwapVal).addMBB(StartMBB)
3034 .addReg(RetrySwapVal).addMBB(SetMBB);
3035 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
3036 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
3037 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
3038 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00003039 BuildMI(MBB, DL, TII->get(SystemZ::CR))
3040 .addReg(Dest).addReg(RetryCmpVal);
3041 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00003042 .addImm(SystemZ::CCMASK_ICMP)
3043 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003044 MBB->addSuccessor(DoneMBB);
3045 MBB->addSuccessor(SetMBB);
3046
3047 // SetMBB:
3048 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
3049 // ^^ Replace the upper 32-BitSize bits of the new
3050 // value with those that we loaded.
3051 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
3052 // ^^ Rotate the new field to its proper position.
3053 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
3054 // JNE LoopMBB
3055 // # fall through to ExitMMB
3056 MBB = SetMBB;
3057 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
3058 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
3059 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
3060 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
3061 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
3062 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00003063 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3064 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003065 MBB->addSuccessor(LoopMBB);
3066 MBB->addSuccessor(DoneMBB);
3067
3068 MI->eraseFromParent();
3069 return DoneMBB;
3070}
3071
3072// Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true
3073// if the high register of the GR128 value must be cleared or false if
Richard Sandiford87a44362013-09-30 10:28:35 +00003074// it's "don't care". SubReg is subreg_l32 when extending a GR32
3075// and subreg_l64 when extending a GR64.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003076MachineBasicBlock *
3077SystemZTargetLowering::emitExt128(MachineInstr *MI,
3078 MachineBasicBlock *MBB,
3079 bool ClearEven, unsigned SubReg) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003080 MachineFunction &MF = *MBB->getParent();
Eric Christopher93bf97c2014-06-27 07:38:01 +00003081 const SystemZInstrInfo *TII =
3082 static_cast<const SystemZInstrInfo *>(MF.getTarget().getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003083 MachineRegisterInfo &MRI = MF.getRegInfo();
3084 DebugLoc DL = MI->getDebugLoc();
3085
3086 unsigned Dest = MI->getOperand(0).getReg();
3087 unsigned Src = MI->getOperand(1).getReg();
3088 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
3089
3090 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
3091 if (ClearEven) {
3092 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
3093 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
3094
3095 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
3096 .addImm(0);
3097 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
Richard Sandiford87a44362013-09-30 10:28:35 +00003098 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003099 In128 = NewIn128;
3100 }
3101 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
3102 .addReg(In128).addReg(Src).addImm(SubReg);
3103
3104 MI->eraseFromParent();
3105 return MBB;
3106}
3107
Richard Sandifordd131ff82013-07-08 09:35:23 +00003108MachineBasicBlock *
Richard Sandiford564681c2013-08-12 10:28:10 +00003109SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI,
3110 MachineBasicBlock *MBB,
3111 unsigned Opcode) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +00003112 MachineFunction &MF = *MBB->getParent();
Eric Christopher93bf97c2014-06-27 07:38:01 +00003113 const SystemZInstrInfo *TII =
3114 static_cast<const SystemZInstrInfo *>(MF.getTarget().getInstrInfo());
Richard Sandiford5e318f02013-08-27 09:54:29 +00003115 MachineRegisterInfo &MRI = MF.getRegInfo();
Richard Sandifordd131ff82013-07-08 09:35:23 +00003116 DebugLoc DL = MI->getDebugLoc();
3117
Richard Sandiford5e318f02013-08-27 09:54:29 +00003118 MachineOperand DestBase = earlyUseOperand(MI->getOperand(0));
Richard Sandifordd131ff82013-07-08 09:35:23 +00003119 uint64_t DestDisp = MI->getOperand(1).getImm();
Richard Sandiford5e318f02013-08-27 09:54:29 +00003120 MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2));
Richard Sandifordd131ff82013-07-08 09:35:23 +00003121 uint64_t SrcDisp = MI->getOperand(3).getImm();
3122 uint64_t Length = MI->getOperand(4).getImm();
3123
Richard Sandifordbe133a82013-08-28 09:01:51 +00003124 // When generating more than one CLC, all but the last will need to
3125 // branch to the end when a difference is found.
3126 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
Craig Topper062a2ba2014-04-25 05:30:21 +00003127 splitBlockAfter(MI, MBB) : nullptr);
Richard Sandifordbe133a82013-08-28 09:01:51 +00003128
Richard Sandiford5e318f02013-08-27 09:54:29 +00003129 // Check for the loop form, in which operand 5 is the trip count.
3130 if (MI->getNumExplicitOperands() > 5) {
3131 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
3132
3133 uint64_t StartCountReg = MI->getOperand(5).getReg();
3134 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
3135 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
3136 forceReg(MI, DestBase, TII));
3137
3138 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
3139 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
3140 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
3141 MRI.createVirtualRegister(RC));
3142 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
3143 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
3144 MRI.createVirtualRegister(RC));
3145
3146 RC = &SystemZ::GR64BitRegClass;
3147 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
3148 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
3149
3150 MachineBasicBlock *StartMBB = MBB;
3151 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
3152 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00003153 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00003154
3155 // StartMBB:
3156 // # fall through to LoopMMB
3157 MBB->addSuccessor(LoopMBB);
3158
3159 // LoopMBB:
3160 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00003161 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00003162 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00003163 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00003164 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00003165 // [ %NextCountReg, NextMBB ]
3166 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00003167 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00003168 // ( JLH EndMBB )
3169 //
3170 // The prefetch is used only for MVC. The JLH is used only for CLC.
3171 MBB = LoopMBB;
3172
3173 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
3174 .addReg(StartDestReg).addMBB(StartMBB)
3175 .addReg(NextDestReg).addMBB(NextMBB);
3176 if (!HaveSingleBase)
3177 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
3178 .addReg(StartSrcReg).addMBB(StartMBB)
3179 .addReg(NextSrcReg).addMBB(NextMBB);
3180 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
3181 .addReg(StartCountReg).addMBB(StartMBB)
3182 .addReg(NextCountReg).addMBB(NextMBB);
3183 if (Opcode == SystemZ::MVC)
3184 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
3185 .addImm(SystemZ::PFD_WRITE)
3186 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
3187 BuildMI(MBB, DL, TII->get(Opcode))
3188 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
3189 .addReg(ThisSrcReg).addImm(SrcDisp);
3190 if (EndMBB) {
3191 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3192 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
3193 .addMBB(EndMBB);
3194 MBB->addSuccessor(EndMBB);
3195 MBB->addSuccessor(NextMBB);
3196 }
3197
3198 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00003199 // %NextDestReg = LA 256(%ThisDestReg)
3200 // %NextSrcReg = LA 256(%ThisSrcReg)
3201 // %NextCountReg = AGHI %ThisCountReg, -1
3202 // CGHI %NextCountReg, 0
3203 // JLH LoopMBB
3204 // # fall through to DoneMMB
3205 //
3206 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00003207 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00003208
Richard Sandiford5e318f02013-08-27 09:54:29 +00003209 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
3210 .addReg(ThisDestReg).addImm(256).addReg(0);
3211 if (!HaveSingleBase)
3212 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
3213 .addReg(ThisSrcReg).addImm(256).addReg(0);
3214 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
3215 .addReg(ThisCountReg).addImm(-1);
3216 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
3217 .addReg(NextCountReg).addImm(0);
3218 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3219 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
3220 .addMBB(LoopMBB);
3221 MBB->addSuccessor(LoopMBB);
3222 MBB->addSuccessor(DoneMBB);
3223
3224 DestBase = MachineOperand::CreateReg(NextDestReg, false);
3225 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
3226 Length &= 255;
3227 MBB = DoneMBB;
3228 }
3229 // Handle any remaining bytes with straight-line code.
3230 while (Length > 0) {
3231 uint64_t ThisLength = std::min(Length, uint64_t(256));
3232 // The previous iteration might have created out-of-range displacements.
3233 // Apply them using LAY if so.
3234 if (!isUInt<12>(DestDisp)) {
3235 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
3236 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
3237 .addOperand(DestBase).addImm(DestDisp).addReg(0);
3238 DestBase = MachineOperand::CreateReg(Reg, false);
3239 DestDisp = 0;
3240 }
3241 if (!isUInt<12>(SrcDisp)) {
3242 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
3243 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
3244 .addOperand(SrcBase).addImm(SrcDisp).addReg(0);
3245 SrcBase = MachineOperand::CreateReg(Reg, false);
3246 SrcDisp = 0;
3247 }
3248 BuildMI(*MBB, MI, DL, TII->get(Opcode))
3249 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength)
3250 .addOperand(SrcBase).addImm(SrcDisp);
3251 DestDisp += ThisLength;
3252 SrcDisp += ThisLength;
3253 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00003254 // If there's another CLC to go, branch to the end if a difference
3255 // was found.
3256 if (EndMBB && Length > 0) {
3257 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
3258 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3259 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
3260 .addMBB(EndMBB);
3261 MBB->addSuccessor(EndMBB);
3262 MBB->addSuccessor(NextMBB);
3263 MBB = NextMBB;
3264 }
3265 }
3266 if (EndMBB) {
3267 MBB->addSuccessor(EndMBB);
3268 MBB = EndMBB;
3269 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00003270 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00003271
3272 MI->eraseFromParent();
3273 return MBB;
3274}
3275
Richard Sandifordca232712013-08-16 11:21:54 +00003276// Decompose string pseudo-instruction MI into a loop that continually performs
3277// Opcode until CC != 3.
3278MachineBasicBlock *
3279SystemZTargetLowering::emitStringWrapper(MachineInstr *MI,
3280 MachineBasicBlock *MBB,
3281 unsigned Opcode) const {
Richard Sandifordca232712013-08-16 11:21:54 +00003282 MachineFunction &MF = *MBB->getParent();
Eric Christopher93bf97c2014-06-27 07:38:01 +00003283 const SystemZInstrInfo *TII =
3284 static_cast<const SystemZInstrInfo *>(MF.getTarget().getInstrInfo());
Richard Sandifordca232712013-08-16 11:21:54 +00003285 MachineRegisterInfo &MRI = MF.getRegInfo();
3286 DebugLoc DL = MI->getDebugLoc();
3287
3288 uint64_t End1Reg = MI->getOperand(0).getReg();
3289 uint64_t Start1Reg = MI->getOperand(1).getReg();
3290 uint64_t Start2Reg = MI->getOperand(2).getReg();
3291 uint64_t CharReg = MI->getOperand(3).getReg();
3292
3293 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
3294 uint64_t This1Reg = MRI.createVirtualRegister(RC);
3295 uint64_t This2Reg = MRI.createVirtualRegister(RC);
3296 uint64_t End2Reg = MRI.createVirtualRegister(RC);
3297
3298 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00003299 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00003300 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
3301
3302 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00003303 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00003304 MBB->addSuccessor(LoopMBB);
3305
3306 // LoopMBB:
3307 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
3308 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford7789b082013-09-30 08:48:38 +00003309 // R0L = %CharReg
3310 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
Richard Sandifordca232712013-08-16 11:21:54 +00003311 // JO LoopMBB
3312 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00003313 //
Richard Sandiford7789b082013-09-30 08:48:38 +00003314 // The load of R0L can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00003315 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00003316
3317 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
3318 .addReg(Start1Reg).addMBB(StartMBB)
3319 .addReg(End1Reg).addMBB(LoopMBB);
3320 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
3321 .addReg(Start2Reg).addMBB(StartMBB)
3322 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford7789b082013-09-30 08:48:38 +00003323 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00003324 BuildMI(MBB, DL, TII->get(Opcode))
3325 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
3326 .addReg(This1Reg).addReg(This2Reg);
3327 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3328 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
3329 MBB->addSuccessor(LoopMBB);
3330 MBB->addSuccessor(DoneMBB);
3331
3332 DoneMBB->addLiveIn(SystemZ::CC);
3333
3334 MI->eraseFromParent();
3335 return DoneMBB;
3336}
3337
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003338MachineBasicBlock *SystemZTargetLowering::
3339EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const {
3340 switch (MI->getOpcode()) {
Richard Sandiford7c5c0ea2013-10-01 13:10:16 +00003341 case SystemZ::Select32Mux:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003342 case SystemZ::Select32:
3343 case SystemZ::SelectF32:
3344 case SystemZ::Select64:
3345 case SystemZ::SelectF64:
3346 case SystemZ::SelectF128:
3347 return emitSelect(MI, MBB);
3348
Richard Sandiford2896d042013-10-01 14:33:55 +00003349 case SystemZ::CondStore8Mux:
3350 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
3351 case SystemZ::CondStore8MuxInv:
3352 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
3353 case SystemZ::CondStore16Mux:
3354 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
3355 case SystemZ::CondStore16MuxInv:
3356 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003357 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003358 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003359 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003360 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003361 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003362 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003363 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003364 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003365 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003366 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003367 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003368 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003369 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003370 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003371 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003372 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003373 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003374 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003375 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003376 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003377 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003378 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003379 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00003380 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00003381
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003382 case SystemZ::AEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00003383 return emitExt128(MI, MBB, false, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003384 case SystemZ::ZEXT128_32:
Richard Sandiford87a44362013-09-30 10:28:35 +00003385 return emitExt128(MI, MBB, true, SystemZ::subreg_l32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003386 case SystemZ::ZEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00003387 return emitExt128(MI, MBB, true, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003388
3389 case SystemZ::ATOMIC_SWAPW:
3390 return emitAtomicLoadBinary(MI, MBB, 0, 0);
3391 case SystemZ::ATOMIC_SWAP_32:
3392 return emitAtomicLoadBinary(MI, MBB, 0, 32);
3393 case SystemZ::ATOMIC_SWAP_64:
3394 return emitAtomicLoadBinary(MI, MBB, 0, 64);
3395
3396 case SystemZ::ATOMIC_LOADW_AR:
3397 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
3398 case SystemZ::ATOMIC_LOADW_AFI:
3399 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
3400 case SystemZ::ATOMIC_LOAD_AR:
3401 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
3402 case SystemZ::ATOMIC_LOAD_AHI:
3403 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
3404 case SystemZ::ATOMIC_LOAD_AFI:
3405 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
3406 case SystemZ::ATOMIC_LOAD_AGR:
3407 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
3408 case SystemZ::ATOMIC_LOAD_AGHI:
3409 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
3410 case SystemZ::ATOMIC_LOAD_AGFI:
3411 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
3412
3413 case SystemZ::ATOMIC_LOADW_SR:
3414 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
3415 case SystemZ::ATOMIC_LOAD_SR:
3416 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
3417 case SystemZ::ATOMIC_LOAD_SGR:
3418 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
3419
3420 case SystemZ::ATOMIC_LOADW_NR:
3421 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
3422 case SystemZ::ATOMIC_LOADW_NILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00003423 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003424 case SystemZ::ATOMIC_LOAD_NR:
3425 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00003426 case SystemZ::ATOMIC_LOAD_NILL:
3427 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
3428 case SystemZ::ATOMIC_LOAD_NILH:
3429 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
3430 case SystemZ::ATOMIC_LOAD_NILF:
3431 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003432 case SystemZ::ATOMIC_LOAD_NGR:
3433 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00003434 case SystemZ::ATOMIC_LOAD_NILL64:
3435 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
3436 case SystemZ::ATOMIC_LOAD_NILH64:
3437 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00003438 case SystemZ::ATOMIC_LOAD_NIHL64:
3439 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
3440 case SystemZ::ATOMIC_LOAD_NIHH64:
3441 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00003442 case SystemZ::ATOMIC_LOAD_NILF64:
3443 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00003444 case SystemZ::ATOMIC_LOAD_NIHF64:
3445 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003446
3447 case SystemZ::ATOMIC_LOADW_OR:
3448 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
3449 case SystemZ::ATOMIC_LOADW_OILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00003450 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003451 case SystemZ::ATOMIC_LOAD_OR:
3452 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00003453 case SystemZ::ATOMIC_LOAD_OILL:
3454 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
3455 case SystemZ::ATOMIC_LOAD_OILH:
3456 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
3457 case SystemZ::ATOMIC_LOAD_OILF:
3458 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003459 case SystemZ::ATOMIC_LOAD_OGR:
3460 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00003461 case SystemZ::ATOMIC_LOAD_OILL64:
3462 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
3463 case SystemZ::ATOMIC_LOAD_OILH64:
3464 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00003465 case SystemZ::ATOMIC_LOAD_OIHL64:
3466 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
3467 case SystemZ::ATOMIC_LOAD_OIHH64:
3468 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00003469 case SystemZ::ATOMIC_LOAD_OILF64:
3470 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00003471 case SystemZ::ATOMIC_LOAD_OIHF64:
3472 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003473
3474 case SystemZ::ATOMIC_LOADW_XR:
3475 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
3476 case SystemZ::ATOMIC_LOADW_XILF:
Richard Sandiford652784e2013-09-25 11:11:53 +00003477 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003478 case SystemZ::ATOMIC_LOAD_XR:
3479 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00003480 case SystemZ::ATOMIC_LOAD_XILF:
3481 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003482 case SystemZ::ATOMIC_LOAD_XGR:
3483 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00003484 case SystemZ::ATOMIC_LOAD_XILF64:
3485 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
Richard Sandiford5718dac2013-10-01 14:08:44 +00003486 case SystemZ::ATOMIC_LOAD_XIHF64:
3487 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003488
3489 case SystemZ::ATOMIC_LOADW_NRi:
3490 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
3491 case SystemZ::ATOMIC_LOADW_NILHi:
Richard Sandiford652784e2013-09-25 11:11:53 +00003492 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003493 case SystemZ::ATOMIC_LOAD_NRi:
3494 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00003495 case SystemZ::ATOMIC_LOAD_NILLi:
3496 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
3497 case SystemZ::ATOMIC_LOAD_NILHi:
3498 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
3499 case SystemZ::ATOMIC_LOAD_NILFi:
3500 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003501 case SystemZ::ATOMIC_LOAD_NGRi:
3502 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00003503 case SystemZ::ATOMIC_LOAD_NILL64i:
3504 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
3505 case SystemZ::ATOMIC_LOAD_NILH64i:
3506 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00003507 case SystemZ::ATOMIC_LOAD_NIHL64i:
3508 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
3509 case SystemZ::ATOMIC_LOAD_NIHH64i:
3510 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00003511 case SystemZ::ATOMIC_LOAD_NILF64i:
3512 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00003513 case SystemZ::ATOMIC_LOAD_NIHF64i:
3514 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003515
3516 case SystemZ::ATOMIC_LOADW_MIN:
3517 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3518 SystemZ::CCMASK_CMP_LE, 0);
3519 case SystemZ::ATOMIC_LOAD_MIN_32:
3520 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3521 SystemZ::CCMASK_CMP_LE, 32);
3522 case SystemZ::ATOMIC_LOAD_MIN_64:
3523 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
3524 SystemZ::CCMASK_CMP_LE, 64);
3525
3526 case SystemZ::ATOMIC_LOADW_MAX:
3527 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3528 SystemZ::CCMASK_CMP_GE, 0);
3529 case SystemZ::ATOMIC_LOAD_MAX_32:
3530 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3531 SystemZ::CCMASK_CMP_GE, 32);
3532 case SystemZ::ATOMIC_LOAD_MAX_64:
3533 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
3534 SystemZ::CCMASK_CMP_GE, 64);
3535
3536 case SystemZ::ATOMIC_LOADW_UMIN:
3537 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3538 SystemZ::CCMASK_CMP_LE, 0);
3539 case SystemZ::ATOMIC_LOAD_UMIN_32:
3540 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3541 SystemZ::CCMASK_CMP_LE, 32);
3542 case SystemZ::ATOMIC_LOAD_UMIN_64:
3543 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
3544 SystemZ::CCMASK_CMP_LE, 64);
3545
3546 case SystemZ::ATOMIC_LOADW_UMAX:
3547 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3548 SystemZ::CCMASK_CMP_GE, 0);
3549 case SystemZ::ATOMIC_LOAD_UMAX_32:
3550 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3551 SystemZ::CCMASK_CMP_GE, 32);
3552 case SystemZ::ATOMIC_LOAD_UMAX_64:
3553 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
3554 SystemZ::CCMASK_CMP_GE, 64);
3555
3556 case SystemZ::ATOMIC_CMP_SWAPW:
3557 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00003558 case SystemZ::MVCSequence:
3559 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00003560 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00003561 case SystemZ::NCSequence:
3562 case SystemZ::NCLoop:
3563 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
3564 case SystemZ::OCSequence:
3565 case SystemZ::OCLoop:
3566 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
3567 case SystemZ::XCSequence:
3568 case SystemZ::XCLoop:
3569 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00003570 case SystemZ::CLCSequence:
3571 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00003572 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00003573 case SystemZ::CLSTLoop:
3574 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00003575 case SystemZ::MVSTLoop:
3576 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00003577 case SystemZ::SRSTLoop:
3578 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003579 default:
3580 llvm_unreachable("Unexpected instr type to insert");
3581 }
3582}