blob: 5528404a19579827f478e5b6812dee802311f0b6 [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
14#define DEBUG_TYPE "systemz-lower"
15
16#include "SystemZISelLowering.h"
17#include "SystemZCallingConv.h"
18#include "SystemZConstantPoolValue.h"
19#include "SystemZMachineFunctionInfo.h"
20#include "SystemZTargetMachine.h"
21#include "llvm/CodeGen/CallingConvLower.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
25
26using namespace llvm;
27
28// Classify VT as either 32 or 64 bit.
29static bool is32Bit(EVT VT) {
30 switch (VT.getSimpleVT().SimpleTy) {
31 case MVT::i32:
32 return true;
33 case MVT::i64:
34 return false;
35 default:
36 llvm_unreachable("Unsupported type");
37 }
38}
39
40// Return a version of MachineOperand that can be safely used before the
41// final use.
42static MachineOperand earlyUseOperand(MachineOperand Op) {
43 if (Op.isReg())
44 Op.setIsKill(false);
45 return Op;
46}
47
48SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm)
49 : TargetLowering(tm, new TargetLoweringObjectFileELF()),
50 Subtarget(*tm.getSubtargetImpl()), TM(tm) {
51 MVT PtrVT = getPointerTy();
52
53 // Set up the register classes.
54 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
55 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
56 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
57 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
58 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
59
60 // Compute derived properties from the register classes
61 computeRegisterProperties();
62
63 // Set up special registers.
64 setExceptionPointerRegister(SystemZ::R6D);
65 setExceptionSelectorRegister(SystemZ::R7D);
66 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
67
68 // TODO: It may be better to default to latency-oriented scheduling, however
69 // LLVM's current latency-oriented scheduler can't handle physreg definitions
Richard Sandiford14a44492013-05-22 13:38:45 +000070 // such as SystemZ has with CC, so set this to the register-pressure
Ulrich Weigand5f613df2013-05-06 16:15:19 +000071 // scheduler, because it can.
72 setSchedulingPreference(Sched::RegPressure);
73
74 setBooleanContents(ZeroOrOneBooleanContent);
75 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
76
77 // Instructions are strings of 2-byte aligned 2-byte values.
78 setMinFunctionAlignment(2);
79
80 // Handle operations that are handled in a similar way for all types.
81 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
82 I <= MVT::LAST_FP_VALUETYPE;
83 ++I) {
84 MVT VT = MVT::SimpleValueType(I);
85 if (isTypeLegal(VT)) {
86 // Expand SETCC(X, Y, COND) into SELECT_CC(X, Y, 1, 0, COND).
87 setOperationAction(ISD::SETCC, VT, Expand);
88
89 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
90 setOperationAction(ISD::SELECT, VT, Expand);
91
92 // Lower SELECT_CC and BR_CC into separate comparisons and branches.
93 setOperationAction(ISD::SELECT_CC, VT, Custom);
94 setOperationAction(ISD::BR_CC, VT, Custom);
95 }
96 }
97
98 // Expand jump table branches as address arithmetic followed by an
99 // indirect jump.
100 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
101
102 // Expand BRCOND into a BR_CC (see above).
103 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
104
105 // Handle integer types.
106 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
107 I <= MVT::LAST_INTEGER_VALUETYPE;
108 ++I) {
109 MVT VT = MVT::SimpleValueType(I);
110 if (isTypeLegal(VT)) {
111 // Expand individual DIV and REMs into DIVREMs.
112 setOperationAction(ISD::SDIV, VT, Expand);
113 setOperationAction(ISD::UDIV, VT, Expand);
114 setOperationAction(ISD::SREM, VT, Expand);
115 setOperationAction(ISD::UREM, VT, Expand);
116 setOperationAction(ISD::SDIVREM, VT, Custom);
117 setOperationAction(ISD::UDIVREM, VT, Custom);
118
119 // Expand ATOMIC_LOAD and ATOMIC_STORE using ATOMIC_CMP_SWAP.
120 // FIXME: probably much too conservative.
121 setOperationAction(ISD::ATOMIC_LOAD, VT, Expand);
122 setOperationAction(ISD::ATOMIC_STORE, VT, Expand);
123
124 // No special instructions for these.
125 setOperationAction(ISD::CTPOP, VT, Expand);
126 setOperationAction(ISD::CTTZ, VT, Expand);
127 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
128 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
129 setOperationAction(ISD::ROTR, VT, Expand);
130
Richard Sandiford7d86e472013-08-21 09:34:56 +0000131 // Use *MUL_LOHI where possible instead of MULH*.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000132 setOperationAction(ISD::MULHS, VT, Expand);
133 setOperationAction(ISD::MULHU, VT, Expand);
Richard Sandiford7d86e472013-08-21 09:34:56 +0000134 setOperationAction(ISD::SMUL_LOHI, VT, Custom);
135 setOperationAction(ISD::UMUL_LOHI, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000136
137 // We have instructions for signed but not unsigned FP conversion.
138 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
139 }
140 }
141
142 // Type legalization will convert 8- and 16-bit atomic operations into
143 // forms that operate on i32s (but still keeping the original memory VT).
144 // Lower them into full i32 operations.
145 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom);
146 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom);
147 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
148 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom);
149 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom);
150 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom);
151 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
152 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom);
153 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom);
154 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
155 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
156 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
157
158 // We have instructions for signed but not unsigned FP conversion.
159 // Handle unsigned 32-bit types as signed 64-bit types.
160 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
161 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
162
163 // We have native support for a 64-bit CTLZ, via FLOGR.
164 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
165 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
166
167 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
168 setOperationAction(ISD::OR, MVT::i64, Custom);
169
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000170 // FIXME: Can we support these natively?
171 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
172 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
173 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
174
175 // We have native instructions for i8, i16 and i32 extensions, but not i1.
176 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
177 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
178 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
179 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
180
181 // Handle the various types of symbolic address.
182 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
183 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
184 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
185 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
186 setOperationAction(ISD::JumpTable, PtrVT, Custom);
187
188 // We need to handle dynamic allocations specially because of the
189 // 160-byte area at the bottom of the stack.
190 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
191
192 // Use custom expanders so that we can force the function to use
193 // a frame pointer.
194 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
195 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
196
Richard Sandiford03481332013-08-23 11:36:42 +0000197 // Handle prefetches with PFD or PFDRL.
198 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
199
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000200 // Handle floating-point types.
201 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
202 I <= MVT::LAST_FP_VALUETYPE;
203 ++I) {
204 MVT VT = MVT::SimpleValueType(I);
205 if (isTypeLegal(VT)) {
206 // We can use FI for FRINT.
207 setOperationAction(ISD::FRINT, VT, Legal);
208
Richard Sandifordaf5f66a2013-08-21 09:04:20 +0000209 // We can use the extended form of FI for other rounding operations.
210 if (Subtarget.hasFPExtension()) {
211 setOperationAction(ISD::FNEARBYINT, VT, Legal);
212 setOperationAction(ISD::FFLOOR, VT, Legal);
213 setOperationAction(ISD::FCEIL, VT, Legal);
214 setOperationAction(ISD::FTRUNC, VT, Legal);
215 setOperationAction(ISD::FROUND, VT, Legal);
216 }
217
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000218 // No special instructions for these.
219 setOperationAction(ISD::FSIN, VT, Expand);
220 setOperationAction(ISD::FCOS, VT, Expand);
221 setOperationAction(ISD::FREM, VT, Expand);
222 }
223 }
224
225 // We have fused multiply-addition for f32 and f64 but not f128.
226 setOperationAction(ISD::FMA, MVT::f32, Legal);
227 setOperationAction(ISD::FMA, MVT::f64, Legal);
228 setOperationAction(ISD::FMA, MVT::f128, Expand);
229
230 // Needed so that we don't try to implement f128 constant loads using
231 // a load-and-extend of a f80 constant (in cases where the constant
232 // would fit in an f80).
233 setLoadExtAction(ISD::EXTLOAD, MVT::f80, Expand);
234
235 // Floating-point truncation and stores need to be done separately.
236 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
237 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
238 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
239
240 // We have 64-bit FPR<->GPR moves, but need special handling for
241 // 32-bit forms.
242 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
243 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
244
245 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
246 // structure, but VAEND is a no-op.
247 setOperationAction(ISD::VASTART, MVT::Other, Custom);
248 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
249 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Richard Sandifordd131ff82013-07-08 09:35:23 +0000250
251 // We want to use MVC in preference to even a single load/store pair.
252 MaxStoresPerMemcpy = 0;
253 MaxStoresPerMemcpyOptSize = 0;
Richard Sandiford47660c12013-07-09 09:32:42 +0000254
255 // The main memset sequence is a byte store followed by an MVC.
256 // Two STC or MV..I stores win over that, but the kind of fused stores
257 // generated by target-independent code don't when the byte value is
258 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
259 // than "STC;MVC". Handle the choice in target-specific code instead.
260 MaxStoresPerMemset = 0;
261 MaxStoresPerMemsetOptSize = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000262}
263
Stephen Lin73de7bf2013-07-09 18:16:56 +0000264bool
265SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
266 VT = VT.getScalarType();
267
268 if (!VT.isSimple())
269 return false;
270
271 switch (VT.getSimpleVT().SimpleTy) {
272 case MVT::f32:
273 case MVT::f64:
274 return true;
275 case MVT::f128:
276 return false;
277 default:
278 break;
279 }
280
281 return false;
282}
283
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000284bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
285 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
286 return Imm.isZero() || Imm.isNegZero();
287}
288
Richard Sandiford46af5a22013-05-30 09:45:42 +0000289bool SystemZTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
290 bool *Fast) const {
291 // Unaligned accesses should never be slower than the expanded version.
292 // We check specifically for aligned accesses in the few cases where
293 // they are required.
294 if (Fast)
295 *Fast = true;
296 return true;
297}
298
Richard Sandiford791bea42013-07-31 12:58:26 +0000299bool SystemZTargetLowering::isLegalAddressingMode(const AddrMode &AM,
300 Type *Ty) const {
301 // Punt on globals for now, although they can be used in limited
302 // RELATIVE LONG cases.
303 if (AM.BaseGV)
304 return false;
305
306 // Require a 20-bit signed offset.
307 if (!isInt<20>(AM.BaseOffs))
308 return false;
309
310 // Indexing is OK but no scale factor can be applied.
311 return AM.Scale == 0 || AM.Scale == 1;
312}
313
Richard Sandiford709bda62013-08-19 12:42:31 +0000314bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
315 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
316 return false;
317 unsigned FromBits = FromType->getPrimitiveSizeInBits();
318 unsigned ToBits = ToType->getPrimitiveSizeInBits();
319 return FromBits > ToBits;
320}
321
322bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
323 if (!FromVT.isInteger() || !ToVT.isInteger())
324 return false;
325 unsigned FromBits = FromVT.getSizeInBits();
326 unsigned ToBits = ToVT.getSizeInBits();
327 return FromBits > ToBits;
328}
329
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000330//===----------------------------------------------------------------------===//
331// Inline asm support
332//===----------------------------------------------------------------------===//
333
334TargetLowering::ConstraintType
335SystemZTargetLowering::getConstraintType(const std::string &Constraint) const {
336 if (Constraint.size() == 1) {
337 switch (Constraint[0]) {
338 case 'a': // Address register
339 case 'd': // Data register (equivalent to 'r')
340 case 'f': // Floating-point register
341 case 'r': // General-purpose register
342 return C_RegisterClass;
343
344 case 'Q': // Memory with base and unsigned 12-bit displacement
345 case 'R': // Likewise, plus an index
346 case 'S': // Memory with base and signed 20-bit displacement
347 case 'T': // Likewise, plus an index
348 case 'm': // Equivalent to 'T'.
349 return C_Memory;
350
351 case 'I': // Unsigned 8-bit constant
352 case 'J': // Unsigned 12-bit constant
353 case 'K': // Signed 16-bit constant
354 case 'L': // Signed 20-bit displacement (on all targets we support)
355 case 'M': // 0x7fffffff
356 return C_Other;
357
358 default:
359 break;
360 }
361 }
362 return TargetLowering::getConstraintType(Constraint);
363}
364
365TargetLowering::ConstraintWeight SystemZTargetLowering::
366getSingleConstraintMatchWeight(AsmOperandInfo &info,
367 const char *constraint) const {
368 ConstraintWeight weight = CW_Invalid;
369 Value *CallOperandVal = info.CallOperandVal;
370 // If we don't have a value, we can't do a match,
371 // but allow it at the lowest weight.
372 if (CallOperandVal == NULL)
373 return CW_Default;
374 Type *type = CallOperandVal->getType();
375 // Look at the constraint type.
376 switch (*constraint) {
377 default:
378 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
379 break;
380
381 case 'a': // Address register
382 case 'd': // Data register (equivalent to 'r')
383 case 'r': // General-purpose register
384 if (CallOperandVal->getType()->isIntegerTy())
385 weight = CW_Register;
386 break;
387
388 case 'f': // Floating-point register
389 if (type->isFloatingPointTy())
390 weight = CW_Register;
391 break;
392
393 case 'I': // Unsigned 8-bit constant
394 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
395 if (isUInt<8>(C->getZExtValue()))
396 weight = CW_Constant;
397 break;
398
399 case 'J': // Unsigned 12-bit constant
400 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
401 if (isUInt<12>(C->getZExtValue()))
402 weight = CW_Constant;
403 break;
404
405 case 'K': // Signed 16-bit constant
406 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
407 if (isInt<16>(C->getSExtValue()))
408 weight = CW_Constant;
409 break;
410
411 case 'L': // Signed 20-bit displacement (on all targets we support)
412 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
413 if (isInt<20>(C->getSExtValue()))
414 weight = CW_Constant;
415 break;
416
417 case 'M': // 0x7fffffff
418 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
419 if (C->getZExtValue() == 0x7fffffff)
420 weight = CW_Constant;
421 break;
422 }
423 return weight;
424}
425
Richard Sandifordb8204052013-07-12 09:08:12 +0000426// Parse a "{tNNN}" register constraint for which the register type "t"
427// has already been verified. MC is the class associated with "t" and
428// Map maps 0-based register numbers to LLVM register numbers.
429static std::pair<unsigned, const TargetRegisterClass *>
430parseRegisterNumber(const std::string &Constraint,
431 const TargetRegisterClass *RC, const unsigned *Map) {
432 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
433 if (isdigit(Constraint[2])) {
434 std::string Suffix(Constraint.data() + 2, Constraint.size() - 2);
435 unsigned Index = atoi(Suffix.c_str());
436 if (Index < 16 && Map[Index])
437 return std::make_pair(Map[Index], RC);
438 }
439 return std::make_pair(0u, static_cast<TargetRegisterClass*>(0));
440}
441
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000442std::pair<unsigned, const TargetRegisterClass *> SystemZTargetLowering::
Chad Rosier295bd432013-06-22 18:37:38 +0000443getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000444 if (Constraint.size() == 1) {
445 // GCC Constraint Letters
446 switch (Constraint[0]) {
447 default: break;
448 case 'd': // Data register (equivalent to 'r')
449 case 'r': // General-purpose register
450 if (VT == MVT::i64)
451 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
452 else if (VT == MVT::i128)
453 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
454 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
455
456 case 'a': // Address register
457 if (VT == MVT::i64)
458 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
459 else if (VT == MVT::i128)
460 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
461 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
462
463 case 'f': // Floating-point register
464 if (VT == MVT::f64)
465 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
466 else if (VT == MVT::f128)
467 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
468 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
469 }
470 }
Richard Sandifordb8204052013-07-12 09:08:12 +0000471 if (Constraint[0] == '{') {
472 // We need to override the default register parsing for GPRs and FPRs
473 // because the interpretation depends on VT. The internal names of
474 // the registers are also different from the external names
475 // (F0D and F0S instead of F0, etc.).
476 if (Constraint[1] == 'r') {
477 if (VT == MVT::i32)
478 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
479 SystemZMC::GR32Regs);
480 if (VT == MVT::i128)
481 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
482 SystemZMC::GR128Regs);
483 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
484 SystemZMC::GR64Regs);
485 }
486 if (Constraint[1] == 'f') {
487 if (VT == MVT::f32)
488 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
489 SystemZMC::FP32Regs);
490 if (VT == MVT::f128)
491 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
492 SystemZMC::FP128Regs);
493 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
494 SystemZMC::FP64Regs);
495 }
496 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000497 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
498}
499
500void SystemZTargetLowering::
501LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
502 std::vector<SDValue> &Ops,
503 SelectionDAG &DAG) const {
504 // Only support length 1 constraints for now.
505 if (Constraint.length() == 1) {
506 switch (Constraint[0]) {
507 case 'I': // Unsigned 8-bit constant
508 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
509 if (isUInt<8>(C->getZExtValue()))
510 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
511 Op.getValueType()));
512 return;
513
514 case 'J': // Unsigned 12-bit constant
515 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
516 if (isUInt<12>(C->getZExtValue()))
517 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
518 Op.getValueType()));
519 return;
520
521 case 'K': // Signed 16-bit constant
522 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
523 if (isInt<16>(C->getSExtValue()))
524 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
525 Op.getValueType()));
526 return;
527
528 case 'L': // Signed 20-bit displacement (on all targets we support)
529 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
530 if (isInt<20>(C->getSExtValue()))
531 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
532 Op.getValueType()));
533 return;
534
535 case 'M': // 0x7fffffff
536 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
537 if (C->getZExtValue() == 0x7fffffff)
538 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
539 Op.getValueType()));
540 return;
541 }
542 }
543 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
544}
545
546//===----------------------------------------------------------------------===//
547// Calling conventions
548//===----------------------------------------------------------------------===//
549
550#include "SystemZGenCallingConv.inc"
551
Richard Sandiford709bda62013-08-19 12:42:31 +0000552bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
553 Type *ToType) const {
554 return isTruncateFree(FromType, ToType);
555}
556
557bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
558 if (!CI->isTailCall())
559 return false;
560 return true;
561}
562
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000563// Value is a value that has been passed to us in the location described by VA
564// (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
565// any loads onto Chain.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000566static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000567 CCValAssign &VA, SDValue Chain,
568 SDValue Value) {
569 // If the argument has been promoted from a smaller type, insert an
570 // assertion to capture this.
571 if (VA.getLocInfo() == CCValAssign::SExt)
572 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
573 DAG.getValueType(VA.getValVT()));
574 else if (VA.getLocInfo() == CCValAssign::ZExt)
575 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
576 DAG.getValueType(VA.getValVT()));
577
578 if (VA.isExtInLoc())
579 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
580 else if (VA.getLocInfo() == CCValAssign::Indirect)
581 Value = DAG.getLoad(VA.getValVT(), DL, Chain, Value,
582 MachinePointerInfo(), false, false, false, 0);
583 else
584 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
585 return Value;
586}
587
588// Value is a value of type VA.getValVT() that we need to copy into
589// the location described by VA. Return a copy of Value converted to
590// VA.getValVT(). The caller is responsible for handling indirect values.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000591static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000592 CCValAssign &VA, SDValue Value) {
593 switch (VA.getLocInfo()) {
594 case CCValAssign::SExt:
595 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
596 case CCValAssign::ZExt:
597 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
598 case CCValAssign::AExt:
599 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
600 case CCValAssign::Full:
601 return Value;
602 default:
603 llvm_unreachable("Unhandled getLocInfo()");
604 }
605}
606
607SDValue SystemZTargetLowering::
608LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
609 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000610 SDLoc DL, SelectionDAG &DAG,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000611 SmallVectorImpl<SDValue> &InVals) const {
612 MachineFunction &MF = DAG.getMachineFunction();
613 MachineFrameInfo *MFI = MF.getFrameInfo();
614 MachineRegisterInfo &MRI = MF.getRegInfo();
615 SystemZMachineFunctionInfo *FuncInfo =
616 MF.getInfo<SystemZMachineFunctionInfo>();
617 const SystemZFrameLowering *TFL =
618 static_cast<const SystemZFrameLowering *>(TM.getFrameLowering());
619
620 // Assign locations to all of the incoming arguments.
621 SmallVector<CCValAssign, 16> ArgLocs;
622 CCState CCInfo(CallConv, IsVarArg, MF, TM, ArgLocs, *DAG.getContext());
623 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
624
625 unsigned NumFixedGPRs = 0;
626 unsigned NumFixedFPRs = 0;
627 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
628 SDValue ArgValue;
629 CCValAssign &VA = ArgLocs[I];
630 EVT LocVT = VA.getLocVT();
631 if (VA.isRegLoc()) {
632 // Arguments passed in registers
633 const TargetRegisterClass *RC;
634 switch (LocVT.getSimpleVT().SimpleTy) {
635 default:
636 // Integers smaller than i64 should be promoted to i64.
637 llvm_unreachable("Unexpected argument type");
638 case MVT::i32:
639 NumFixedGPRs += 1;
640 RC = &SystemZ::GR32BitRegClass;
641 break;
642 case MVT::i64:
643 NumFixedGPRs += 1;
644 RC = &SystemZ::GR64BitRegClass;
645 break;
646 case MVT::f32:
647 NumFixedFPRs += 1;
648 RC = &SystemZ::FP32BitRegClass;
649 break;
650 case MVT::f64:
651 NumFixedFPRs += 1;
652 RC = &SystemZ::FP64BitRegClass;
653 break;
654 }
655
656 unsigned VReg = MRI.createVirtualRegister(RC);
657 MRI.addLiveIn(VA.getLocReg(), VReg);
658 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
659 } else {
660 assert(VA.isMemLoc() && "Argument not register or memory");
661
662 // Create the frame index object for this incoming parameter.
663 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
664 VA.getLocMemOffset(), true);
665
666 // Create the SelectionDAG nodes corresponding to a load
667 // from this parameter. Unpromoted ints and floats are
668 // passed as right-justified 8-byte values.
669 EVT PtrVT = getPointerTy();
670 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
671 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
672 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, DAG.getIntPtrConstant(4));
673 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
674 MachinePointerInfo::getFixedStack(FI),
675 false, false, false, 0);
676 }
677
678 // Convert the value of the argument register into the value that's
679 // being passed.
680 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
681 }
682
683 if (IsVarArg) {
684 // Save the number of non-varargs registers for later use by va_start, etc.
685 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
686 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
687
688 // Likewise the address (in the form of a frame index) of where the
689 // first stack vararg would be. The 1-byte size here is arbitrary.
690 int64_t StackSize = CCInfo.getNextStackOffset();
691 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true));
692
693 // ...and a similar frame index for the caller-allocated save area
694 // that will be used to store the incoming registers.
695 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
696 unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true);
697 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
698
699 // Store the FPR varargs in the reserved frame slots. (We store the
700 // GPRs as part of the prologue.)
701 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
702 SDValue MemOps[SystemZ::NumArgFPRs];
703 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
704 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
705 int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true);
706 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
707 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
708 &SystemZ::FP64BitRegClass);
709 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
710 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
711 MachinePointerInfo::getFixedStack(FI),
712 false, false, 0);
713
714 }
715 // Join the stores, which are independent of one another.
716 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
717 &MemOps[NumFixedFPRs],
718 SystemZ::NumArgFPRs - NumFixedFPRs);
719 }
720 }
721
722 return Chain;
723}
724
Richard Sandiford709bda62013-08-19 12:42:31 +0000725static bool canUseSiblingCall(CCState ArgCCInfo,
726 SmallVectorImpl<CCValAssign> &ArgLocs) {
727 // Punt if there are any indirect or stack arguments, or if the call
728 // needs the call-saved argument register R6.
729 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
730 CCValAssign &VA = ArgLocs[I];
731 if (VA.getLocInfo() == CCValAssign::Indirect)
732 return false;
733 if (!VA.isRegLoc())
734 return false;
735 unsigned Reg = VA.getLocReg();
736 if (Reg == SystemZ::R6W || Reg == SystemZ::R6D)
737 return false;
738 }
739 return true;
740}
741
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000742SDValue
743SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
744 SmallVectorImpl<SDValue> &InVals) const {
745 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000746 SDLoc &DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000747 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
748 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
749 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000750 SDValue Chain = CLI.Chain;
751 SDValue Callee = CLI.Callee;
Richard Sandiford709bda62013-08-19 12:42:31 +0000752 bool &IsTailCall = CLI.IsTailCall;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000753 CallingConv::ID CallConv = CLI.CallConv;
754 bool IsVarArg = CLI.IsVarArg;
755 MachineFunction &MF = DAG.getMachineFunction();
756 EVT PtrVT = getPointerTy();
757
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000758 // Analyze the operands of the call, assigning locations to each operand.
759 SmallVector<CCValAssign, 16> ArgLocs;
760 CCState ArgCCInfo(CallConv, IsVarArg, MF, TM, ArgLocs, *DAG.getContext());
761 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
762
Richard Sandiford709bda62013-08-19 12:42:31 +0000763 // We don't support GuaranteedTailCallOpt, only automatically-detected
764 // sibling calls.
765 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs))
766 IsTailCall = false;
767
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000768 // Get a count of how many bytes are to be pushed on the stack.
769 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
770
771 // Mark the start of the call.
Richard Sandiford709bda62013-08-19 12:42:31 +0000772 if (!IsTailCall)
773 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumBytes, PtrVT, true),
774 DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000775
776 // Copy argument values to their designated locations.
777 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
778 SmallVector<SDValue, 8> MemOpChains;
779 SDValue StackPtr;
780 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
781 CCValAssign &VA = ArgLocs[I];
782 SDValue ArgValue = OutVals[I];
783
784 if (VA.getLocInfo() == CCValAssign::Indirect) {
785 // Store the argument in a stack slot and pass its address.
786 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
787 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
788 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, SpillSlot,
789 MachinePointerInfo::getFixedStack(FI),
790 false, false, 0));
791 ArgValue = SpillSlot;
792 } else
793 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
794
795 if (VA.isRegLoc())
796 // Queue up the argument copies and emit them at the end.
797 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
798 else {
799 assert(VA.isMemLoc() && "Argument not register or memory");
800
801 // Work out the address of the stack slot. Unpromoted ints and
802 // floats are passed as right-justified 8-byte values.
803 if (!StackPtr.getNode())
804 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
805 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
806 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
807 Offset += 4;
808 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
809 DAG.getIntPtrConstant(Offset));
810
811 // Emit the store.
812 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address,
813 MachinePointerInfo(),
814 false, false, 0));
815 }
816 }
817
818 // Join the stores, which are independent of one another.
819 if (!MemOpChains.empty())
820 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
821 &MemOpChains[0], MemOpChains.size());
822
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000823 // Accept direct calls by converting symbolic call addresses to the
Richard Sandiford709bda62013-08-19 12:42:31 +0000824 // associated Target* opcodes. Force %r1 to be used for indirect
825 // tail calls.
826 SDValue Glue;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000827 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
828 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
829 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
830 } else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
831 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
832 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford709bda62013-08-19 12:42:31 +0000833 } else if (IsTailCall) {
834 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
835 Glue = Chain.getValue(1);
836 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
837 }
838
839 // Build a sequence of copy-to-reg nodes, chained and glued together.
840 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
841 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
842 RegsToPass[I].second, Glue);
843 Glue = Chain.getValue(1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000844 }
845
846 // The first call operand is the chain and the second is the target address.
847 SmallVector<SDValue, 8> Ops;
848 Ops.push_back(Chain);
849 Ops.push_back(Callee);
850
851 // Add argument registers to the end of the list so that they are
852 // known live into the call.
853 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
854 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
855 RegsToPass[I].second.getValueType()));
856
857 // Glue the call to the argument copies, if any.
858 if (Glue.getNode())
859 Ops.push_back(Glue);
860
861 // Emit the call.
862 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Richard Sandiford709bda62013-08-19 12:42:31 +0000863 if (IsTailCall)
864 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, &Ops[0], Ops.size());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000865 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, &Ops[0], Ops.size());
866 Glue = Chain.getValue(1);
867
868 // Mark the end of the call, which is glued to the call itself.
869 Chain = DAG.getCALLSEQ_END(Chain,
870 DAG.getConstant(NumBytes, PtrVT, true),
871 DAG.getConstant(0, PtrVT, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +0000872 Glue, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000873 Glue = Chain.getValue(1);
874
875 // Assign locations to each value returned by this call.
876 SmallVector<CCValAssign, 16> RetLocs;
877 CCState RetCCInfo(CallConv, IsVarArg, MF, TM, RetLocs, *DAG.getContext());
878 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
879
880 // Copy all of the result registers out of their specified physreg.
881 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
882 CCValAssign &VA = RetLocs[I];
883
884 // Copy the value out, gluing the copy to the end of the call sequence.
885 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
886 VA.getLocVT(), Glue);
887 Chain = RetValue.getValue(1);
888 Glue = RetValue.getValue(2);
889
890 // Convert the value of the return register into the value that's
891 // being returned.
892 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
893 }
894
895 return Chain;
896}
897
898SDValue
899SystemZTargetLowering::LowerReturn(SDValue Chain,
900 CallingConv::ID CallConv, bool IsVarArg,
901 const SmallVectorImpl<ISD::OutputArg> &Outs,
902 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000903 SDLoc DL, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000904 MachineFunction &MF = DAG.getMachineFunction();
905
906 // Assign locations to each returned value.
907 SmallVector<CCValAssign, 16> RetLocs;
908 CCState RetCCInfo(CallConv, IsVarArg, MF, TM, RetLocs, *DAG.getContext());
909 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
910
911 // Quick exit for void returns
912 if (RetLocs.empty())
913 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
914
915 // Copy the result values into the output registers.
916 SDValue Glue;
917 SmallVector<SDValue, 4> RetOps;
918 RetOps.push_back(Chain);
919 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
920 CCValAssign &VA = RetLocs[I];
921 SDValue RetValue = OutVals[I];
922
923 // Make the return register live on exit.
924 assert(VA.isRegLoc() && "Can only return in registers!");
925
926 // Promote the value as required.
927 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
928
929 // Chain and glue the copies together.
930 unsigned Reg = VA.getLocReg();
931 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
932 Glue = Chain.getValue(1);
933 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
934 }
935
936 // Update chain and glue.
937 RetOps[0] = Chain;
938 if (Glue.getNode())
939 RetOps.push_back(Glue);
940
941 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other,
942 RetOps.data(), RetOps.size());
943}
944
945// CC is a comparison that will be implemented using an integer or
946// floating-point comparison. Return the condition code mask for
947// a branch on true. In the integer case, CCMASK_CMP_UO is set for
948// unsigned comparisons and clear for signed ones. In the floating-point
949// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
950static unsigned CCMaskForCondCode(ISD::CondCode CC) {
951#define CONV(X) \
952 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
953 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
954 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
955
956 switch (CC) {
957 default:
958 llvm_unreachable("Invalid integer condition!");
959
960 CONV(EQ);
961 CONV(NE);
962 CONV(GT);
963 CONV(GE);
964 CONV(LT);
965 CONV(LE);
966
967 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
968 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
969 }
970#undef CONV
971}
972
973// If a comparison described by IsUnsigned, CCMask, CmpOp0 and CmpOp1
Richard Sandiforda0757082013-08-01 10:29:45 +0000974// can be converted to a comparison against zero, adjust the operands
975// as necessary.
976static void adjustZeroCmp(SelectionDAG &DAG, bool &IsUnsigned,
977 SDValue &CmpOp0, SDValue &CmpOp1,
978 unsigned &CCMask) {
979 if (IsUnsigned)
980 return;
981
982 ConstantSDNode *ConstOp1 = dyn_cast<ConstantSDNode>(CmpOp1.getNode());
983 if (!ConstOp1)
984 return;
985
986 int64_t Value = ConstOp1->getSExtValue();
987 if ((Value == -1 && CCMask == SystemZ::CCMASK_CMP_GT) ||
988 (Value == -1 && CCMask == SystemZ::CCMASK_CMP_LE) ||
989 (Value == 1 && CCMask == SystemZ::CCMASK_CMP_LT) ||
990 (Value == 1 && CCMask == SystemZ::CCMASK_CMP_GE)) {
991 CCMask ^= SystemZ::CCMASK_CMP_EQ;
992 CmpOp1 = DAG.getConstant(0, CmpOp1.getValueType());
993 }
994}
995
996// If a comparison described by IsUnsigned, CCMask, CmpOp0 and CmpOp1
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000997// is suitable for CLI(Y), CHHSI or CLHHSI, adjust the operands as necessary.
998static void adjustSubwordCmp(SelectionDAG &DAG, bool &IsUnsigned,
999 SDValue &CmpOp0, SDValue &CmpOp1,
1000 unsigned &CCMask) {
1001 // For us to make any changes, it must a comparison between a single-use
1002 // load and a constant.
1003 if (!CmpOp0.hasOneUse() ||
1004 CmpOp0.getOpcode() != ISD::LOAD ||
1005 CmpOp1.getOpcode() != ISD::Constant)
1006 return;
1007
1008 // We must have an 8- or 16-bit load.
1009 LoadSDNode *Load = cast<LoadSDNode>(CmpOp0);
1010 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1011 if (NumBits != 8 && NumBits != 16)
1012 return;
1013
1014 // The load must be an extending one and the constant must be within the
1015 // range of the unextended value.
1016 ConstantSDNode *Constant = cast<ConstantSDNode>(CmpOp1);
1017 uint64_t Value = Constant->getZExtValue();
1018 uint64_t Mask = (1 << NumBits) - 1;
1019 if (Load->getExtensionType() == ISD::SEXTLOAD) {
1020 int64_t SignedValue = Constant->getSExtValue();
Aaron Ballmanb4284e62013-05-16 16:03:36 +00001021 if (uint64_t(SignedValue) + (1ULL << (NumBits - 1)) > Mask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001022 return;
1023 // Unsigned comparison between two sign-extended values is equivalent
1024 // to unsigned comparison between two zero-extended values.
1025 if (IsUnsigned)
1026 Value &= Mask;
1027 else if (CCMask == SystemZ::CCMASK_CMP_EQ ||
1028 CCMask == SystemZ::CCMASK_CMP_NE)
1029 // Any choice of IsUnsigned is OK for equality comparisons.
1030 // We could use either CHHSI or CLHHSI for 16-bit comparisons,
1031 // but since we use CLHHSI for zero extensions, it seems better
1032 // to be consistent and do the same here.
1033 Value &= Mask, IsUnsigned = true;
1034 else if (NumBits == 8) {
1035 // Try to treat the comparison as unsigned, so that we can use CLI.
1036 // Adjust CCMask and Value as necessary.
1037 if (Value == 0 && CCMask == SystemZ::CCMASK_CMP_LT)
1038 // Test whether the high bit of the byte is set.
1039 Value = 127, CCMask = SystemZ::CCMASK_CMP_GT, IsUnsigned = true;
Richard Sandiforda0757082013-08-01 10:29:45 +00001040 else if (Value == 0 && CCMask == SystemZ::CCMASK_CMP_GE)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001041 // Test whether the high bit of the byte is clear.
1042 Value = 128, CCMask = SystemZ::CCMASK_CMP_LT, IsUnsigned = true;
1043 else
1044 // No instruction exists for this combination.
1045 return;
1046 }
1047 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1048 if (Value > Mask)
1049 return;
1050 // Signed comparison between two zero-extended values is equivalent
1051 // to unsigned comparison.
1052 IsUnsigned = true;
1053 } else
1054 return;
1055
1056 // Make sure that the first operand is an i32 of the right extension type.
1057 ISD::LoadExtType ExtType = IsUnsigned ? ISD::ZEXTLOAD : ISD::SEXTLOAD;
1058 if (CmpOp0.getValueType() != MVT::i32 ||
1059 Load->getExtensionType() != ExtType)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001060 CmpOp0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001061 Load->getChain(), Load->getBasePtr(),
1062 Load->getPointerInfo(), Load->getMemoryVT(),
1063 Load->isVolatile(), Load->isNonTemporal(),
1064 Load->getAlignment());
1065
1066 // Make sure that the second operand is an i32 with the right value.
1067 if (CmpOp1.getValueType() != MVT::i32 ||
1068 Value != Constant->getZExtValue())
1069 CmpOp1 = DAG.getConstant(Value, MVT::i32);
1070}
1071
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001072// Return true if Op is either an unextended load, or a load suitable
1073// for integer register-memory comparisons of type ICmpType.
1074static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001075 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op.getNode());
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001076 if (Load) {
1077 // There are no instructions to compare a register with a memory byte.
1078 if (Load->getMemoryVT() == MVT::i8)
1079 return false;
1080 // Otherwise decide on extension type.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001081 switch (Load->getExtensionType()) {
1082 case ISD::NON_EXTLOAD:
Richard Sandiford24e597b2013-08-23 11:27:19 +00001083 return true;
1084 case ISD::SEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001085 return ICmpType != SystemZICMP::UnsignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001086 case ISD::ZEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001087 return ICmpType != SystemZICMP::SignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001088 default:
1089 break;
1090 }
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001091 }
Richard Sandiford24e597b2013-08-23 11:27:19 +00001092 return false;
1093}
1094
1095// Return true if it is better to swap comparison operands Op0 and Op1.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001096// ICmpType is the type of an integer comparison.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001097static bool shouldSwapCmpOperands(SDValue Op0, SDValue Op1,
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001098 unsigned ICmpType) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001099 // Leave f128 comparisons alone, since they have no memory forms.
1100 if (Op0.getValueType() == MVT::f128)
1101 return false;
1102
1103 // Always keep a floating-point constant second, since comparisons with
1104 // zero can use LOAD TEST and comparisons with other constants make a
1105 // natural memory operand.
1106 if (isa<ConstantFPSDNode>(Op1))
1107 return false;
1108
1109 // Never swap comparisons with zero since there are many ways to optimize
1110 // those later.
1111 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
1112 if (COp1 && COp1->getZExtValue() == 0)
1113 return false;
1114
1115 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1116 // In that case we generally prefer the memory to be second.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001117 if ((isNaturalMemoryOperand(Op0, ICmpType) && Op0.hasOneUse()) &&
1118 !(isNaturalMemoryOperand(Op1, ICmpType) && Op1.hasOneUse())) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001119 // The only exceptions are when the second operand is a constant and
1120 // we can use things like CHHSI.
1121 if (!COp1)
1122 return true;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001123 // The unsigned memory-immediate instructions can handle 16-bit
1124 // unsigned integers.
1125 if (ICmpType != SystemZICMP::SignedOnly &&
1126 isUInt<16>(COp1->getZExtValue()))
1127 return false;
1128 // The signed memory-immediate instructions can handle 16-bit
1129 // signed integers.
1130 if (ICmpType != SystemZICMP::UnsignedOnly &&
1131 isInt<16>(COp1->getSExtValue()))
1132 return false;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001133 return true;
1134 }
1135 return false;
1136}
1137
Richard Sandiford113c8702013-09-03 15:38:35 +00001138// Check whether the CC value produced by TEST UNDER MASK is descriptive
1139// enough to handle an AND with Mask followed by a comparison of type Opcode
1140// with CmpVal. CCMask says which comparison result is being tested and
1141// BitSize is the number of bits in the operands. Return the CC mask that
1142// should be used for the TEST UNDER MASK result, or 0 if the condition is
1143// too complex.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001144static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
1145 uint64_t Mask, uint64_t CmpVal,
1146 unsigned ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001147 assert(Mask != 0 && "ANDs with zero should have been removed by now");
1148
1149 // Work out the masks for the lowest and highest bits.
1150 unsigned HighShift = 63 - countLeadingZeros(Mask);
1151 uint64_t High = uint64_t(1) << HighShift;
1152 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
1153
1154 // Signed ordered comparisons are effectively unsigned if the sign
1155 // bit is dropped.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001156 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
Richard Sandiford113c8702013-09-03 15:38:35 +00001157
1158 // Check for equality comparisons with 0, or the equivalent.
1159 if (CmpVal == 0) {
1160 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1161 return SystemZ::CCMASK_TM_ALL_0;
1162 if (CCMask == SystemZ::CCMASK_CMP_NE)
1163 return SystemZ::CCMASK_TM_SOME_1;
1164 }
1165 if (EffectivelyUnsigned && CmpVal <= Low) {
1166 if (CCMask == SystemZ::CCMASK_CMP_LT)
1167 return SystemZ::CCMASK_TM_ALL_0;
1168 if (CCMask == SystemZ::CCMASK_CMP_GE)
1169 return SystemZ::CCMASK_TM_SOME_1;
1170 }
1171 if (EffectivelyUnsigned && CmpVal < Low) {
1172 if (CCMask == SystemZ::CCMASK_CMP_LE)
1173 return SystemZ::CCMASK_TM_ALL_0;
1174 if (CCMask == SystemZ::CCMASK_CMP_GT)
1175 return SystemZ::CCMASK_TM_SOME_1;
1176 }
1177
1178 // Check for equality comparisons with the mask, or the equivalent.
1179 if (CmpVal == Mask) {
1180 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1181 return SystemZ::CCMASK_TM_ALL_1;
1182 if (CCMask == SystemZ::CCMASK_CMP_NE)
1183 return SystemZ::CCMASK_TM_SOME_0;
1184 }
1185 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
1186 if (CCMask == SystemZ::CCMASK_CMP_GT)
1187 return SystemZ::CCMASK_TM_ALL_1;
1188 if (CCMask == SystemZ::CCMASK_CMP_LE)
1189 return SystemZ::CCMASK_TM_SOME_0;
1190 }
1191 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
1192 if (CCMask == SystemZ::CCMASK_CMP_GE)
1193 return SystemZ::CCMASK_TM_ALL_1;
1194 if (CCMask == SystemZ::CCMASK_CMP_LT)
1195 return SystemZ::CCMASK_TM_SOME_0;
1196 }
1197
1198 // Check for ordered comparisons with the top bit.
1199 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
1200 if (CCMask == SystemZ::CCMASK_CMP_LE)
1201 return SystemZ::CCMASK_TM_MSB_0;
1202 if (CCMask == SystemZ::CCMASK_CMP_GT)
1203 return SystemZ::CCMASK_TM_MSB_1;
1204 }
1205 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
1206 if (CCMask == SystemZ::CCMASK_CMP_LT)
1207 return SystemZ::CCMASK_TM_MSB_0;
1208 if (CCMask == SystemZ::CCMASK_CMP_GE)
1209 return SystemZ::CCMASK_TM_MSB_1;
1210 }
1211
1212 // If there are just two bits, we can do equality checks for Low and High
1213 // as well.
1214 if (Mask == Low + High) {
1215 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
1216 return SystemZ::CCMASK_TM_MIXED_MSB_0;
1217 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
1218 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
1219 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
1220 return SystemZ::CCMASK_TM_MIXED_MSB_1;
1221 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
1222 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
1223 }
1224
1225 // Looks like we've exhausted our options.
1226 return 0;
1227}
1228
Richard Sandiforda9eb9972013-09-10 10:20:32 +00001229// See whether the comparison (Opcode CmpOp0, CmpOp1, ICmpType) can be
1230// implemented as a TEST UNDER MASK instruction when the condition being
1231// tested is as described by CCValid and CCMask. Update the arguments
1232// with the TM version if so.
Richard Sandiford35b9be22013-08-28 10:31:43 +00001233static void adjustForTestUnderMask(unsigned &Opcode, SDValue &CmpOp0,
1234 SDValue &CmpOp1, unsigned &CCValid,
Richard Sandiforda9eb9972013-09-10 10:20:32 +00001235 unsigned &CCMask, unsigned &ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001236 // Check that we have a comparison with a constant.
Richard Sandiford35b9be22013-08-28 10:31:43 +00001237 ConstantSDNode *ConstCmpOp1 = dyn_cast<ConstantSDNode>(CmpOp1);
Richard Sandiford113c8702013-09-03 15:38:35 +00001238 if (!ConstCmpOp1)
Richard Sandiford35b9be22013-08-28 10:31:43 +00001239 return;
1240
1241 // Check whether the nonconstant input is an AND with a constant mask.
1242 if (CmpOp0.getOpcode() != ISD::AND)
1243 return;
1244 SDValue AndOp0 = CmpOp0.getOperand(0);
1245 SDValue AndOp1 = CmpOp0.getOperand(1);
1246 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(AndOp1.getNode());
1247 if (!Mask)
1248 return;
1249
1250 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
1251 uint64_t MaskVal = Mask->getZExtValue();
1252 if (!SystemZ::isImmLL(MaskVal) && !SystemZ::isImmLH(MaskVal) &&
1253 !SystemZ::isImmHL(MaskVal) && !SystemZ::isImmHH(MaskVal))
1254 return;
1255
Richard Sandiford113c8702013-09-03 15:38:35 +00001256 // Check whether the combination of mask, comparison value and comparison
1257 // type are suitable.
1258 unsigned BitSize = CmpOp0.getValueType().getSizeInBits();
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001259 unsigned NewCCMask = getTestUnderMaskCond(BitSize, CCMask, MaskVal,
1260 ConstCmpOp1->getZExtValue(),
1261 ICmpType);
Richard Sandiford113c8702013-09-03 15:38:35 +00001262 if (!NewCCMask)
1263 return;
1264
Richard Sandiford35b9be22013-08-28 10:31:43 +00001265 // Go ahead and make the change.
1266 Opcode = SystemZISD::TM;
1267 CmpOp0 = AndOp0;
1268 CmpOp1 = AndOp1;
Richard Sandiforda9eb9972013-09-10 10:20:32 +00001269 ICmpType = (bool(NewCCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
1270 bool(NewCCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
Richard Sandiford35b9be22013-08-28 10:31:43 +00001271 CCValid = SystemZ::CCMASK_TM;
Richard Sandiford113c8702013-09-03 15:38:35 +00001272 CCMask = NewCCMask;
Richard Sandiford35b9be22013-08-28 10:31:43 +00001273}
1274
Richard Sandiford3d768e32013-07-31 12:30:20 +00001275// Return a target node that compares CmpOp0 with CmpOp1 and stores a
1276// 2-bit result in CC. Set CCValid to the CCMASK_* of all possible
1277// 2-bit results and CCMask to the subset of those results that are
1278// associated with Cond.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001279static SDValue emitCmp(const SystemZTargetMachine &TM, SelectionDAG &DAG,
1280 SDLoc DL, SDValue CmpOp0, SDValue CmpOp1,
1281 ISD::CondCode Cond, unsigned &CCValid,
Richard Sandiford3d768e32013-07-31 12:30:20 +00001282 unsigned &CCMask) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001283 bool IsUnsigned = false;
Richard Sandiford3d768e32013-07-31 12:30:20 +00001284 CCMask = CCMaskForCondCode(Cond);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001285 unsigned Opcode, ICmpType = 0;
1286 if (CmpOp0.getValueType().isFloatingPoint()) {
Richard Sandiford3d768e32013-07-31 12:30:20 +00001287 CCValid = SystemZ::CCMASK_FCMP;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001288 Opcode = SystemZISD::FCMP;
1289 } else {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001290 IsUnsigned = CCMask & SystemZ::CCMASK_CMP_UO;
Richard Sandiford3d768e32013-07-31 12:30:20 +00001291 CCValid = SystemZ::CCMASK_ICMP;
1292 CCMask &= CCValid;
Richard Sandiforda0757082013-08-01 10:29:45 +00001293 adjustZeroCmp(DAG, IsUnsigned, CmpOp0, CmpOp1, CCMask);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001294 adjustSubwordCmp(DAG, IsUnsigned, CmpOp0, CmpOp1, CCMask);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001295 Opcode = SystemZISD::ICMP;
1296 // Choose the type of comparison. Equality and inequality tests can
1297 // use either signed or unsigned comparisons. The choice also doesn't
1298 // matter if both sign bits are known to be clear. In those cases we
1299 // want to give the main isel code the freedom to choose whichever
1300 // form fits best.
1301 if (CCMask == SystemZ::CCMASK_CMP_EQ ||
1302 CCMask == SystemZ::CCMASK_CMP_NE ||
1303 (DAG.SignBitIsZero(CmpOp0) && DAG.SignBitIsZero(CmpOp1)))
1304 ICmpType = SystemZICMP::Any;
1305 else if (IsUnsigned)
1306 ICmpType = SystemZICMP::UnsignedOnly;
1307 else
1308 ICmpType = SystemZICMP::SignedOnly;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001309 }
1310
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001311 if (shouldSwapCmpOperands(CmpOp0, CmpOp1, ICmpType)) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001312 std::swap(CmpOp0, CmpOp1);
1313 CCMask = ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1314 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1315 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1316 (CCMask & SystemZ::CCMASK_CMP_UO));
1317 }
1318
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001319 adjustForTestUnderMask(Opcode, CmpOp0, CmpOp1, CCValid, CCMask, ICmpType);
Richard Sandiforda9eb9972013-09-10 10:20:32 +00001320 if (Opcode == SystemZISD::ICMP || Opcode == SystemZISD::TM)
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001321 return DAG.getNode(Opcode, DL, MVT::Glue, CmpOp0, CmpOp1,
1322 DAG.getConstant(ICmpType, MVT::i32));
Richard Sandiford35b9be22013-08-28 10:31:43 +00001323 return DAG.getNode(Opcode, DL, MVT::Glue, CmpOp0, CmpOp1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001324}
1325
Richard Sandiford7d86e472013-08-21 09:34:56 +00001326// Implement a 32-bit *MUL_LOHI operation by extending both operands to
1327// 64 bits. Extend is the extension type to use. Store the high part
1328// in Hi and the low part in Lo.
1329static void lowerMUL_LOHI32(SelectionDAG &DAG, SDLoc DL,
1330 unsigned Extend, SDValue Op0, SDValue Op1,
1331 SDValue &Hi, SDValue &Lo) {
1332 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
1333 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
1334 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
1335 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul, DAG.getConstant(32, MVT::i64));
1336 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
1337 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
1338}
1339
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001340// Lower a binary operation that produces two VT results, one in each
1341// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
1342// Extend extends Op0 to a GR128, and Opcode performs the GR128 operation
1343// on the extended Op0 and (unextended) Op1. Store the even register result
1344// in Even and the odd register result in Odd.
Andrew Trickef9de2a2013-05-25 02:42:55 +00001345static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001346 unsigned Extend, unsigned Opcode,
1347 SDValue Op0, SDValue Op1,
1348 SDValue &Even, SDValue &Odd) {
1349 SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0);
1350 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped,
1351 SDValue(In128, 0), Op1);
1352 bool Is32Bit = is32Bit(VT);
1353 SDValue SubReg0 = DAG.getTargetConstant(SystemZ::even128(Is32Bit), VT);
1354 SDValue SubReg1 = DAG.getTargetConstant(SystemZ::odd128(Is32Bit), VT);
1355 SDNode *Reg0 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
1356 VT, Result, SubReg0);
1357 SDNode *Reg1 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
1358 VT, Result, SubReg1);
1359 Even = SDValue(Reg0, 0);
1360 Odd = SDValue(Reg1, 0);
1361}
1362
1363SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
1364 SDValue Chain = Op.getOperand(0);
1365 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1366 SDValue CmpOp0 = Op.getOperand(2);
1367 SDValue CmpOp1 = Op.getOperand(3);
1368 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001369 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001370
Richard Sandiford3d768e32013-07-31 12:30:20 +00001371 unsigned CCValid, CCMask;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001372 SDValue Flags = emitCmp(TM, DAG, DL, CmpOp0, CmpOp1, CC, CCValid, CCMask);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001373 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
Richard Sandiford3d768e32013-07-31 12:30:20 +00001374 Chain, DAG.getConstant(CCValid, MVT::i32),
1375 DAG.getConstant(CCMask, MVT::i32), Dest, Flags);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001376}
1377
1378SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
1379 SelectionDAG &DAG) const {
1380 SDValue CmpOp0 = Op.getOperand(0);
1381 SDValue CmpOp1 = Op.getOperand(1);
1382 SDValue TrueOp = Op.getOperand(2);
1383 SDValue FalseOp = Op.getOperand(3);
1384 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001385 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001386
Richard Sandiford3d768e32013-07-31 12:30:20 +00001387 unsigned CCValid, CCMask;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001388 SDValue Flags = emitCmp(TM, DAG, DL, CmpOp0, CmpOp1, CC, CCValid, CCMask);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001389
Richard Sandiford3d768e32013-07-31 12:30:20 +00001390 SmallVector<SDValue, 5> Ops;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001391 Ops.push_back(TrueOp);
1392 Ops.push_back(FalseOp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00001393 Ops.push_back(DAG.getConstant(CCValid, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001394 Ops.push_back(DAG.getConstant(CCMask, MVT::i32));
1395 Ops.push_back(Flags);
1396
1397 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
1398 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, &Ops[0], Ops.size());
1399}
1400
1401SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
1402 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001403 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001404 const GlobalValue *GV = Node->getGlobal();
1405 int64_t Offset = Node->getOffset();
1406 EVT PtrVT = getPointerTy();
1407 Reloc::Model RM = TM.getRelocationModel();
1408 CodeModel::Model CM = TM.getCodeModel();
1409
1410 SDValue Result;
1411 if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) {
1412 // Make sure that the offset is aligned to a halfword. If it isn't,
1413 // create an "anchor" at the previous 12-bit boundary.
1414 // FIXME check whether there is a better way of handling this.
1415 if (Offset & 1) {
1416 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT,
1417 Offset & ~uint64_t(0xfff));
1418 Offset &= 0xfff;
1419 } else {
1420 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Offset);
1421 Offset = 0;
1422 }
1423 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1424 } else {
1425 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
1426 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1427 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
1428 MachinePointerInfo::getGOT(), false, false, false, 0);
1429 }
1430
1431 // If there was a non-zero offset that we didn't fold, create an explicit
1432 // addition for it.
1433 if (Offset != 0)
1434 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
1435 DAG.getConstant(Offset, PtrVT));
1436
1437 return Result;
1438}
1439
1440SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
1441 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001442 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001443 const GlobalValue *GV = Node->getGlobal();
1444 EVT PtrVT = getPointerTy();
1445 TLSModel::Model model = TM.getTLSModel(GV);
1446
1447 if (model != TLSModel::LocalExec)
1448 llvm_unreachable("only local-exec TLS mode supported");
1449
1450 // The high part of the thread pointer is in access register 0.
1451 SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
1452 DAG.getConstant(0, MVT::i32));
1453 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
1454
1455 // The low part of the thread pointer is in access register 1.
1456 SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
1457 DAG.getConstant(1, MVT::i32));
1458 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
1459
1460 // Merge them into a single 64-bit address.
1461 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
1462 DAG.getConstant(32, PtrVT));
1463 SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
1464
1465 // Get the offset of GA from the thread pointer.
1466 SystemZConstantPoolValue *CPV =
1467 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
1468
1469 // Force the offset into the constant pool and load it from there.
1470 SDValue CPAddr = DAG.getConstantPool(CPV, PtrVT, 8);
1471 SDValue Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
1472 CPAddr, MachinePointerInfo::getConstantPool(),
1473 false, false, false, 0);
1474
1475 // Add the base and offset together.
1476 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
1477}
1478
1479SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
1480 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001481 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001482 const BlockAddress *BA = Node->getBlockAddress();
1483 int64_t Offset = Node->getOffset();
1484 EVT PtrVT = getPointerTy();
1485
1486 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
1487 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1488 return Result;
1489}
1490
1491SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
1492 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001493 SDLoc DL(JT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001494 EVT PtrVT = getPointerTy();
1495 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1496
1497 // Use LARL to load the address of the table.
1498 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1499}
1500
1501SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
1502 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001503 SDLoc DL(CP);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001504 EVT PtrVT = getPointerTy();
1505
1506 SDValue Result;
1507 if (CP->isMachineConstantPoolEntry())
1508 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1509 CP->getAlignment());
1510 else
1511 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1512 CP->getAlignment(), CP->getOffset());
1513
1514 // Use LARL to load the address of the constant pool entry.
1515 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1516}
1517
1518SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
1519 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001520 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001521 SDValue In = Op.getOperand(0);
1522 EVT InVT = In.getValueType();
1523 EVT ResVT = Op.getValueType();
1524
1525 SDValue SubReg32 = DAG.getTargetConstant(SystemZ::subreg_32bit, MVT::i64);
1526 SDValue Shift32 = DAG.getConstant(32, MVT::i64);
1527 if (InVT == MVT::i32 && ResVT == MVT::f32) {
1528 SDValue In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
1529 SDValue Shift = DAG.getNode(ISD::SHL, DL, MVT::i64, In64, Shift32);
1530 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, Shift);
1531 SDNode *Out = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
1532 MVT::f32, Out64, SubReg32);
1533 return SDValue(Out, 0);
1534 }
1535 if (InVT == MVT::f32 && ResVT == MVT::i32) {
1536 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
1537 SDNode *In64 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
1538 MVT::f64, SDValue(U64, 0), In, SubReg32);
1539 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, SDValue(In64, 0));
1540 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64, Shift32);
1541 SDValue Out = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
1542 return Out;
1543 }
1544 llvm_unreachable("Unexpected bitcast combination");
1545}
1546
1547SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
1548 SelectionDAG &DAG) const {
1549 MachineFunction &MF = DAG.getMachineFunction();
1550 SystemZMachineFunctionInfo *FuncInfo =
1551 MF.getInfo<SystemZMachineFunctionInfo>();
1552 EVT PtrVT = getPointerTy();
1553
1554 SDValue Chain = Op.getOperand(0);
1555 SDValue Addr = Op.getOperand(1);
1556 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001557 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001558
1559 // The initial values of each field.
1560 const unsigned NumFields = 4;
1561 SDValue Fields[NumFields] = {
1562 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), PtrVT),
1563 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), PtrVT),
1564 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
1565 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
1566 };
1567
1568 // Store each field into its respective slot.
1569 SDValue MemOps[NumFields];
1570 unsigned Offset = 0;
1571 for (unsigned I = 0; I < NumFields; ++I) {
1572 SDValue FieldAddr = Addr;
1573 if (Offset != 0)
1574 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
1575 DAG.getIntPtrConstant(Offset));
1576 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
1577 MachinePointerInfo(SV, Offset),
1578 false, false, 0);
1579 Offset += 8;
1580 }
1581 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps, NumFields);
1582}
1583
1584SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
1585 SelectionDAG &DAG) const {
1586 SDValue Chain = Op.getOperand(0);
1587 SDValue DstPtr = Op.getOperand(1);
1588 SDValue SrcPtr = Op.getOperand(2);
1589 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
1590 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001591 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001592
1593 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32),
1594 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
1595 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
1596}
1597
1598SDValue SystemZTargetLowering::
1599lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
1600 SDValue Chain = Op.getOperand(0);
1601 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001602 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001603
1604 unsigned SPReg = getStackPointerRegisterToSaveRestore();
1605
1606 // Get a reference to the stack pointer.
1607 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
1608
1609 // Get the new stack pointer value.
1610 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, Size);
1611
1612 // Copy the new stack pointer back.
1613 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
1614
1615 // The allocated data lives above the 160 bytes allocated for the standard
1616 // frame, plus any outgoing stack arguments. We don't know how much that
1617 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
1618 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
1619 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
1620
1621 SDValue Ops[2] = { Result, Chain };
1622 return DAG.getMergeValues(Ops, 2, DL);
1623}
1624
Richard Sandiford7d86e472013-08-21 09:34:56 +00001625SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
1626 SelectionDAG &DAG) const {
1627 EVT VT = Op.getValueType();
1628 SDLoc DL(Op);
1629 SDValue Ops[2];
1630 if (is32Bit(VT))
1631 // Just do a normal 64-bit multiplication and extract the results.
1632 // We define this so that it can be used for constant division.
1633 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
1634 Op.getOperand(1), Ops[1], Ops[0]);
1635 else {
1636 // Do a full 128-bit multiplication based on UMUL_LOHI64:
1637 //
1638 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
1639 //
1640 // but using the fact that the upper halves are either all zeros
1641 // or all ones:
1642 //
1643 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
1644 //
1645 // and grouping the right terms together since they are quicker than the
1646 // multiplication:
1647 //
1648 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
1649 SDValue C63 = DAG.getConstant(63, MVT::i64);
1650 SDValue LL = Op.getOperand(0);
1651 SDValue RL = Op.getOperand(1);
1652 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
1653 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
1654 // UMUL_LOHI64 returns the low result in the odd register and the high
1655 // result in the even register. SMUL_LOHI is defined to return the
1656 // low half first, so the results are in reverse order.
1657 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
1658 LL, RL, Ops[1], Ops[0]);
1659 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
1660 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
1661 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
1662 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
1663 }
1664 return DAG.getMergeValues(Ops, 2, DL);
1665}
1666
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001667SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
1668 SelectionDAG &DAG) const {
1669 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001670 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001671 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00001672 if (is32Bit(VT))
1673 // Just do a normal 64-bit multiplication and extract the results.
1674 // We define this so that it can be used for constant division.
1675 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
1676 Op.getOperand(1), Ops[1], Ops[0]);
1677 else
1678 // UMUL_LOHI64 returns the low result in the odd register and the high
1679 // result in the even register. UMUL_LOHI is defined to return the
1680 // low half first, so the results are in reverse order.
1681 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
1682 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001683 return DAG.getMergeValues(Ops, 2, DL);
1684}
1685
1686SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
1687 SelectionDAG &DAG) const {
1688 SDValue Op0 = Op.getOperand(0);
1689 SDValue Op1 = Op.getOperand(1);
1690 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001691 SDLoc DL(Op);
Richard Sandiforde6e78852013-07-02 15:40:22 +00001692 unsigned Opcode;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001693
1694 // We use DSGF for 32-bit division.
1695 if (is32Bit(VT)) {
1696 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Richard Sandiforde6e78852013-07-02 15:40:22 +00001697 Opcode = SystemZISD::SDIVREM32;
1698 } else if (DAG.ComputeNumSignBits(Op1) > 32) {
1699 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
1700 Opcode = SystemZISD::SDIVREM32;
1701 } else
1702 Opcode = SystemZISD::SDIVREM64;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001703
1704 // DSG(F) takes a 64-bit dividend, so the even register in the GR128
1705 // input is "don't care". The instruction returns the remainder in
1706 // the even register and the quotient in the odd register.
1707 SDValue Ops[2];
Richard Sandiforde6e78852013-07-02 15:40:22 +00001708 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001709 Op0, Op1, Ops[1], Ops[0]);
1710 return DAG.getMergeValues(Ops, 2, DL);
1711}
1712
1713SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
1714 SelectionDAG &DAG) const {
1715 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001716 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001717
1718 // DL(G) uses a double-width dividend, so we need to clear the even
1719 // register in the GR128 input. The instruction returns the remainder
1720 // in the even register and the quotient in the odd register.
1721 SDValue Ops[2];
1722 if (is32Bit(VT))
1723 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
1724 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
1725 else
1726 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
1727 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
1728 return DAG.getMergeValues(Ops, 2, DL);
1729}
1730
1731SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
1732 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
1733
1734 // Get the known-zero masks for each operand.
1735 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
1736 APInt KnownZero[2], KnownOne[2];
1737 DAG.ComputeMaskedBits(Ops[0], KnownZero[0], KnownOne[0]);
1738 DAG.ComputeMaskedBits(Ops[1], KnownZero[1], KnownOne[1]);
1739
1740 // See if the upper 32 bits of one operand and the lower 32 bits of the
1741 // other are known zero. They are the low and high operands respectively.
1742 uint64_t Masks[] = { KnownZero[0].getZExtValue(),
1743 KnownZero[1].getZExtValue() };
1744 unsigned High, Low;
1745 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
1746 High = 1, Low = 0;
1747 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
1748 High = 0, Low = 1;
1749 else
1750 return Op;
1751
1752 SDValue LowOp = Ops[Low];
1753 SDValue HighOp = Ops[High];
1754
1755 // If the high part is a constant, we're better off using IILH.
1756 if (HighOp.getOpcode() == ISD::Constant)
1757 return Op;
1758
1759 // If the low part is a constant that is outside the range of LHI,
1760 // then we're better off using IILF.
1761 if (LowOp.getOpcode() == ISD::Constant) {
1762 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
1763 if (!isInt<16>(Value))
1764 return Op;
1765 }
1766
1767 // Check whether the high part is an AND that doesn't change the
1768 // high 32 bits and just masks out low bits. We can skip it if so.
1769 if (HighOp.getOpcode() == ISD::AND &&
1770 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
1771 ConstantSDNode *MaskNode = cast<ConstantSDNode>(HighOp.getOperand(1));
1772 uint64_t Mask = MaskNode->getZExtValue() | Masks[High];
1773 if ((Mask >> 32) == 0xffffffff)
1774 HighOp = HighOp.getOperand(0);
1775 }
1776
1777 // Take advantage of the fact that all GR32 operations only change the
1778 // low 32 bits by truncating Low to an i32 and inserting it directly
1779 // using a subreg. The interesting cases are those where the truncation
1780 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00001781 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001782 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
1783 SDValue SubReg32 = DAG.getTargetConstant(SystemZ::subreg_32bit, MVT::i64);
1784 SDNode *Result = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
1785 MVT::i64, HighOp, Low32, SubReg32);
1786 return SDValue(Result, 0);
1787}
1788
1789// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
1790// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
1791SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
1792 SelectionDAG &DAG,
1793 unsigned Opcode) const {
1794 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode());
1795
1796 // 32-bit operations need no code outside the main loop.
1797 EVT NarrowVT = Node->getMemoryVT();
1798 EVT WideVT = MVT::i32;
1799 if (NarrowVT == WideVT)
1800 return Op;
1801
1802 int64_t BitSize = NarrowVT.getSizeInBits();
1803 SDValue ChainIn = Node->getChain();
1804 SDValue Addr = Node->getBasePtr();
1805 SDValue Src2 = Node->getVal();
1806 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001807 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001808 EVT PtrVT = Addr.getValueType();
1809
1810 // Convert atomic subtracts of constants into additions.
1811 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
1812 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(Src2)) {
1813 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
1814 Src2 = DAG.getConstant(-Const->getSExtValue(), Src2.getValueType());
1815 }
1816
1817 // Get the address of the containing word.
1818 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
1819 DAG.getConstant(-4, PtrVT));
1820
1821 // Get the number of bits that the word must be rotated left in order
1822 // to bring the field to the top bits of a GR32.
1823 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
1824 DAG.getConstant(3, PtrVT));
1825 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
1826
1827 // Get the complementing shift amount, for rotating a field in the top
1828 // bits back to its proper position.
1829 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
1830 DAG.getConstant(0, WideVT), BitShift);
1831
1832 // Extend the source operand to 32 bits and prepare it for the inner loop.
1833 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
1834 // operations require the source to be shifted in advance. (This shift
1835 // can be folded if the source is constant.) For AND and NAND, the lower
1836 // bits must be set, while for other opcodes they should be left clear.
1837 if (Opcode != SystemZISD::ATOMIC_SWAPW)
1838 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
1839 DAG.getConstant(32 - BitSize, WideVT));
1840 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
1841 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
1842 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
1843 DAG.getConstant(uint32_t(-1) >> BitSize, WideVT));
1844
1845 // Construct the ATOMIC_LOADW_* node.
1846 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
1847 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
1848 DAG.getConstant(BitSize, WideVT) };
1849 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
1850 array_lengthof(Ops),
1851 NarrowVT, MMO);
1852
1853 // Rotate the result of the final CS so that the field is in the lower
1854 // bits of a GR32, then truncate it.
1855 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
1856 DAG.getConstant(BitSize, WideVT));
1857 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
1858
1859 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
1860 return DAG.getMergeValues(RetOps, 2, DL);
1861}
1862
1863// Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
1864// into a fullword ATOMIC_CMP_SWAPW operation.
1865SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
1866 SelectionDAG &DAG) const {
1867 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode());
1868
1869 // We have native support for 32-bit compare and swap.
1870 EVT NarrowVT = Node->getMemoryVT();
1871 EVT WideVT = MVT::i32;
1872 if (NarrowVT == WideVT)
1873 return Op;
1874
1875 int64_t BitSize = NarrowVT.getSizeInBits();
1876 SDValue ChainIn = Node->getOperand(0);
1877 SDValue Addr = Node->getOperand(1);
1878 SDValue CmpVal = Node->getOperand(2);
1879 SDValue SwapVal = Node->getOperand(3);
1880 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001881 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001882 EVT PtrVT = Addr.getValueType();
1883
1884 // Get the address of the containing word.
1885 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
1886 DAG.getConstant(-4, PtrVT));
1887
1888 // Get the number of bits that the word must be rotated left in order
1889 // to bring the field to the top bits of a GR32.
1890 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
1891 DAG.getConstant(3, PtrVT));
1892 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
1893
1894 // Get the complementing shift amount, for rotating a field in the top
1895 // bits back to its proper position.
1896 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
1897 DAG.getConstant(0, WideVT), BitShift);
1898
1899 // Construct the ATOMIC_CMP_SWAPW node.
1900 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
1901 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
1902 NegBitShift, DAG.getConstant(BitSize, WideVT) };
1903 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
1904 VTList, Ops, array_lengthof(Ops),
1905 NarrowVT, MMO);
1906 return AtomicOp;
1907}
1908
1909SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
1910 SelectionDAG &DAG) const {
1911 MachineFunction &MF = DAG.getMachineFunction();
1912 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001913 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001914 SystemZ::R15D, Op.getValueType());
1915}
1916
1917SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
1918 SelectionDAG &DAG) const {
1919 MachineFunction &MF = DAG.getMachineFunction();
1920 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001921 return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001922 SystemZ::R15D, Op.getOperand(1));
1923}
1924
Richard Sandiford03481332013-08-23 11:36:42 +00001925SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
1926 SelectionDAG &DAG) const {
1927 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
1928 if (!IsData)
1929 // Just preserve the chain.
1930 return Op.getOperand(0);
1931
1932 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
1933 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
1934 MemIntrinsicSDNode *Node = cast<MemIntrinsicSDNode>(Op.getNode());
1935 SDValue Ops[] = {
1936 Op.getOperand(0),
1937 DAG.getConstant(Code, MVT::i32),
1938 Op.getOperand(1)
1939 };
1940 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, SDLoc(Op),
1941 Node->getVTList(), Ops, array_lengthof(Ops),
1942 Node->getMemoryVT(), Node->getMemOperand());
1943}
1944
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001945SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
1946 SelectionDAG &DAG) const {
1947 switch (Op.getOpcode()) {
1948 case ISD::BR_CC:
1949 return lowerBR_CC(Op, DAG);
1950 case ISD::SELECT_CC:
1951 return lowerSELECT_CC(Op, DAG);
1952 case ISD::GlobalAddress:
1953 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
1954 case ISD::GlobalTLSAddress:
1955 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
1956 case ISD::BlockAddress:
1957 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
1958 case ISD::JumpTable:
1959 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
1960 case ISD::ConstantPool:
1961 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
1962 case ISD::BITCAST:
1963 return lowerBITCAST(Op, DAG);
1964 case ISD::VASTART:
1965 return lowerVASTART(Op, DAG);
1966 case ISD::VACOPY:
1967 return lowerVACOPY(Op, DAG);
1968 case ISD::DYNAMIC_STACKALLOC:
1969 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00001970 case ISD::SMUL_LOHI:
1971 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001972 case ISD::UMUL_LOHI:
1973 return lowerUMUL_LOHI(Op, DAG);
1974 case ISD::SDIVREM:
1975 return lowerSDIVREM(Op, DAG);
1976 case ISD::UDIVREM:
1977 return lowerUDIVREM(Op, DAG);
1978 case ISD::OR:
1979 return lowerOR(Op, DAG);
1980 case ISD::ATOMIC_SWAP:
1981 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_SWAPW);
1982 case ISD::ATOMIC_LOAD_ADD:
1983 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
1984 case ISD::ATOMIC_LOAD_SUB:
1985 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
1986 case ISD::ATOMIC_LOAD_AND:
1987 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
1988 case ISD::ATOMIC_LOAD_OR:
1989 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
1990 case ISD::ATOMIC_LOAD_XOR:
1991 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
1992 case ISD::ATOMIC_LOAD_NAND:
1993 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
1994 case ISD::ATOMIC_LOAD_MIN:
1995 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
1996 case ISD::ATOMIC_LOAD_MAX:
1997 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
1998 case ISD::ATOMIC_LOAD_UMIN:
1999 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
2000 case ISD::ATOMIC_LOAD_UMAX:
2001 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
2002 case ISD::ATOMIC_CMP_SWAP:
2003 return lowerATOMIC_CMP_SWAP(Op, DAG);
2004 case ISD::STACKSAVE:
2005 return lowerSTACKSAVE(Op, DAG);
2006 case ISD::STACKRESTORE:
2007 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00002008 case ISD::PREFETCH:
2009 return lowerPREFETCH(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002010 default:
2011 llvm_unreachable("Unexpected node to lower");
2012 }
2013}
2014
2015const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
2016#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
2017 switch (Opcode) {
2018 OPCODE(RET_FLAG);
2019 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00002020 OPCODE(SIBCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002021 OPCODE(PCREL_WRAPPER);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002022 OPCODE(ICMP);
2023 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00002024 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002025 OPCODE(BR_CCMASK);
2026 OPCODE(SELECT_CCMASK);
2027 OPCODE(ADJDYNALLOC);
2028 OPCODE(EXTRACT_ACCESS);
2029 OPCODE(UMUL_LOHI64);
2030 OPCODE(SDIVREM64);
2031 OPCODE(UDIVREM32);
2032 OPCODE(UDIVREM64);
Richard Sandifordd131ff82013-07-08 09:35:23 +00002033 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00002034 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00002035 OPCODE(NC);
2036 OPCODE(NC_LOOP);
2037 OPCODE(OC);
2038 OPCODE(OC_LOOP);
2039 OPCODE(XC);
2040 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00002041 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00002042 OPCODE(CLC_LOOP);
Richard Sandifordca232712013-08-16 11:21:54 +00002043 OPCODE(STRCMP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00002044 OPCODE(STPCPY);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00002045 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00002046 OPCODE(IPM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002047 OPCODE(ATOMIC_SWAPW);
2048 OPCODE(ATOMIC_LOADW_ADD);
2049 OPCODE(ATOMIC_LOADW_SUB);
2050 OPCODE(ATOMIC_LOADW_AND);
2051 OPCODE(ATOMIC_LOADW_OR);
2052 OPCODE(ATOMIC_LOADW_XOR);
2053 OPCODE(ATOMIC_LOADW_NAND);
2054 OPCODE(ATOMIC_LOADW_MIN);
2055 OPCODE(ATOMIC_LOADW_MAX);
2056 OPCODE(ATOMIC_LOADW_UMIN);
2057 OPCODE(ATOMIC_LOADW_UMAX);
2058 OPCODE(ATOMIC_CMP_SWAPW);
Richard Sandiford03481332013-08-23 11:36:42 +00002059 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002060 }
2061 return NULL;
2062#undef OPCODE
2063}
2064
2065//===----------------------------------------------------------------------===//
2066// Custom insertion
2067//===----------------------------------------------------------------------===//
2068
2069// Create a new basic block after MBB.
2070static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
2071 MachineFunction &MF = *MBB->getParent();
2072 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
2073 MF.insert(llvm::next(MachineFunction::iterator(MBB)), NewMBB);
2074 return NewMBB;
2075}
2076
Richard Sandifordbe133a82013-08-28 09:01:51 +00002077// Split MBB after MI and return the new block (the one that contains
2078// instructions after MI).
2079static MachineBasicBlock *splitBlockAfter(MachineInstr *MI,
2080 MachineBasicBlock *MBB) {
2081 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
2082 NewMBB->splice(NewMBB->begin(), MBB,
2083 llvm::next(MachineBasicBlock::iterator(MI)),
2084 MBB->end());
2085 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
2086 return NewMBB;
2087}
2088
Richard Sandiford5e318f02013-08-27 09:54:29 +00002089// Split MBB before MI and return the new block (the one that contains MI).
2090static MachineBasicBlock *splitBlockBefore(MachineInstr *MI,
2091 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002092 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00002093 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002094 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
2095 return NewMBB;
2096}
2097
Richard Sandiford5e318f02013-08-27 09:54:29 +00002098// Force base value Base into a register before MI. Return the register.
2099static unsigned forceReg(MachineInstr *MI, MachineOperand &Base,
2100 const SystemZInstrInfo *TII) {
2101 if (Base.isReg())
2102 return Base.getReg();
2103
2104 MachineBasicBlock *MBB = MI->getParent();
2105 MachineFunction &MF = *MBB->getParent();
2106 MachineRegisterInfo &MRI = MF.getRegInfo();
2107
2108 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
2109 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg)
2110 .addOperand(Base).addImm(0).addReg(0);
2111 return Reg;
2112}
2113
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002114// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
2115MachineBasicBlock *
2116SystemZTargetLowering::emitSelect(MachineInstr *MI,
2117 MachineBasicBlock *MBB) const {
2118 const SystemZInstrInfo *TII = TM.getInstrInfo();
2119
2120 unsigned DestReg = MI->getOperand(0).getReg();
2121 unsigned TrueReg = MI->getOperand(1).getReg();
2122 unsigned FalseReg = MI->getOperand(2).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00002123 unsigned CCValid = MI->getOperand(3).getImm();
2124 unsigned CCMask = MI->getOperand(4).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002125 DebugLoc DL = MI->getDebugLoc();
2126
2127 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002128 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002129 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
2130
2131 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00002132 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002133 // # fallthrough to FalseMBB
2134 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00002135 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2136 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002137 MBB->addSuccessor(JoinMBB);
2138 MBB->addSuccessor(FalseMBB);
2139
2140 // FalseMBB:
2141 // # fallthrough to JoinMBB
2142 MBB = FalseMBB;
2143 MBB->addSuccessor(JoinMBB);
2144
2145 // JoinMBB:
2146 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
2147 // ...
2148 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002149 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002150 .addReg(TrueReg).addMBB(StartMBB)
2151 .addReg(FalseReg).addMBB(FalseMBB);
2152
2153 MI->eraseFromParent();
2154 return JoinMBB;
2155}
2156
Richard Sandifordb86a8342013-06-27 09:27:40 +00002157// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
2158// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002159// happen when the condition is false rather than true. If a STORE ON
2160// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Richard Sandifordb86a8342013-06-27 09:27:40 +00002161MachineBasicBlock *
2162SystemZTargetLowering::emitCondStore(MachineInstr *MI,
2163 MachineBasicBlock *MBB,
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002164 unsigned StoreOpcode, unsigned STOCOpcode,
2165 bool Invert) const {
Richard Sandifordb86a8342013-06-27 09:27:40 +00002166 const SystemZInstrInfo *TII = TM.getInstrInfo();
2167
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002168 unsigned SrcReg = MI->getOperand(0).getReg();
2169 MachineOperand Base = MI->getOperand(1);
2170 int64_t Disp = MI->getOperand(2).getImm();
2171 unsigned IndexReg = MI->getOperand(3).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00002172 unsigned CCValid = MI->getOperand(4).getImm();
2173 unsigned CCMask = MI->getOperand(5).getImm();
Richard Sandifordb86a8342013-06-27 09:27:40 +00002174 DebugLoc DL = MI->getDebugLoc();
2175
2176 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
2177
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002178 // Use STOCOpcode if possible. We could use different store patterns in
2179 // order to avoid matching the index register, but the performance trade-offs
2180 // might be more complicated in that case.
2181 if (STOCOpcode && !IndexReg && TM.getSubtargetImpl()->hasLoadStoreOnCond()) {
2182 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00002183 CCMask ^= CCValid;
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002184 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +00002185 .addReg(SrcReg).addOperand(Base).addImm(Disp)
2186 .addImm(CCValid).addImm(CCMask);
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002187 MI->eraseFromParent();
2188 return MBB;
2189 }
2190
Richard Sandifordb86a8342013-06-27 09:27:40 +00002191 // Get the condition needed to branch around the store.
2192 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00002193 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00002194
2195 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002196 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002197 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
2198
2199 // StartMBB:
2200 // BRC CCMask, JoinMBB
2201 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00002202 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00002203 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2204 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002205 MBB->addSuccessor(JoinMBB);
2206 MBB->addSuccessor(FalseMBB);
2207
2208 // FalseMBB:
2209 // store %SrcReg, %Disp(%Index,%Base)
2210 // # fallthrough to JoinMBB
2211 MBB = FalseMBB;
2212 BuildMI(MBB, DL, TII->get(StoreOpcode))
2213 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
2214 MBB->addSuccessor(JoinMBB);
2215
2216 MI->eraseFromParent();
2217 return JoinMBB;
2218}
2219
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002220// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
2221// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
2222// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
2223// BitSize is the width of the field in bits, or 0 if this is a partword
2224// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
2225// is one of the operands. Invert says whether the field should be
2226// inverted after performing BinOpcode (e.g. for NAND).
2227MachineBasicBlock *
2228SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI,
2229 MachineBasicBlock *MBB,
2230 unsigned BinOpcode,
2231 unsigned BitSize,
2232 bool Invert) const {
2233 const SystemZInstrInfo *TII = TM.getInstrInfo();
2234 MachineFunction &MF = *MBB->getParent();
2235 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002236 bool IsSubWord = (BitSize < 32);
2237
2238 // Extract the operands. Base can be a register or a frame index.
2239 // Src2 can be a register or immediate.
2240 unsigned Dest = MI->getOperand(0).getReg();
2241 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2242 int64_t Disp = MI->getOperand(2).getImm();
2243 MachineOperand Src2 = earlyUseOperand(MI->getOperand(3));
2244 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
2245 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
2246 DebugLoc DL = MI->getDebugLoc();
2247 if (IsSubWord)
2248 BitSize = MI->getOperand(6).getImm();
2249
2250 // Subword operations use 32-bit registers.
2251 const TargetRegisterClass *RC = (BitSize <= 32 ?
2252 &SystemZ::GR32BitRegClass :
2253 &SystemZ::GR64BitRegClass);
2254 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
2255 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
2256
2257 // Get the right opcodes for the displacement.
2258 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
2259 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
2260 assert(LOpcode && CSOpcode && "Displacement out of range");
2261
2262 // Create virtual registers for temporary results.
2263 unsigned OrigVal = MRI.createVirtualRegister(RC);
2264 unsigned OldVal = MRI.createVirtualRegister(RC);
2265 unsigned NewVal = (BinOpcode || IsSubWord ?
2266 MRI.createVirtualRegister(RC) : Src2.getReg());
2267 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
2268 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
2269
2270 // Insert a basic block for the main loop.
2271 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002272 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002273 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2274
2275 // StartMBB:
2276 // ...
2277 // %OrigVal = L Disp(%Base)
2278 // # fall through to LoopMMB
2279 MBB = StartMBB;
2280 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
2281 .addOperand(Base).addImm(Disp).addReg(0);
2282 MBB->addSuccessor(LoopMBB);
2283
2284 // LoopMBB:
2285 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
2286 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
2287 // %RotatedNewVal = OP %RotatedOldVal, %Src2
2288 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
2289 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
2290 // JNE LoopMBB
2291 // # fall through to DoneMMB
2292 MBB = LoopMBB;
2293 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2294 .addReg(OrigVal).addMBB(StartMBB)
2295 .addReg(Dest).addMBB(LoopMBB);
2296 if (IsSubWord)
2297 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
2298 .addReg(OldVal).addReg(BitShift).addImm(0);
2299 if (Invert) {
2300 // Perform the operation normally and then invert every bit of the field.
2301 unsigned Tmp = MRI.createVirtualRegister(RC);
2302 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
2303 .addReg(RotatedOldVal).addOperand(Src2);
2304 if (BitSize < 32)
2305 // XILF with the upper BitSize bits set.
2306 BuildMI(MBB, DL, TII->get(SystemZ::XILF32), RotatedNewVal)
2307 .addReg(Tmp).addImm(uint32_t(~0 << (32 - BitSize)));
2308 else if (BitSize == 32)
2309 // XILF with every bit set.
2310 BuildMI(MBB, DL, TII->get(SystemZ::XILF32), RotatedNewVal)
2311 .addReg(Tmp).addImm(~uint32_t(0));
2312 else {
2313 // Use LCGR and add -1 to the result, which is more compact than
2314 // an XILF, XILH pair.
2315 unsigned Tmp2 = MRI.createVirtualRegister(RC);
2316 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
2317 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
2318 .addReg(Tmp2).addImm(-1);
2319 }
2320 } else if (BinOpcode)
2321 // A simply binary operation.
2322 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
2323 .addReg(RotatedOldVal).addOperand(Src2);
2324 else if (IsSubWord)
2325 // Use RISBG to rotate Src2 into position and use it to replace the
2326 // field in RotatedOldVal.
2327 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
2328 .addReg(RotatedOldVal).addReg(Src2.getReg())
2329 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
2330 if (IsSubWord)
2331 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
2332 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
2333 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
2334 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00002335 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2336 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002337 MBB->addSuccessor(LoopMBB);
2338 MBB->addSuccessor(DoneMBB);
2339
2340 MI->eraseFromParent();
2341 return DoneMBB;
2342}
2343
2344// Implement EmitInstrWithCustomInserter for pseudo
2345// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
2346// instruction that should be used to compare the current field with the
2347// minimum or maximum value. KeepOldMask is the BRC condition-code mask
2348// for when the current field should be kept. BitSize is the width of
2349// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
2350MachineBasicBlock *
2351SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI,
2352 MachineBasicBlock *MBB,
2353 unsigned CompareOpcode,
2354 unsigned KeepOldMask,
2355 unsigned BitSize) const {
2356 const SystemZInstrInfo *TII = TM.getInstrInfo();
2357 MachineFunction &MF = *MBB->getParent();
2358 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002359 bool IsSubWord = (BitSize < 32);
2360
2361 // Extract the operands. Base can be a register or a frame index.
2362 unsigned Dest = MI->getOperand(0).getReg();
2363 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2364 int64_t Disp = MI->getOperand(2).getImm();
2365 unsigned Src2 = MI->getOperand(3).getReg();
2366 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
2367 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
2368 DebugLoc DL = MI->getDebugLoc();
2369 if (IsSubWord)
2370 BitSize = MI->getOperand(6).getImm();
2371
2372 // Subword operations use 32-bit registers.
2373 const TargetRegisterClass *RC = (BitSize <= 32 ?
2374 &SystemZ::GR32BitRegClass :
2375 &SystemZ::GR64BitRegClass);
2376 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
2377 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
2378
2379 // Get the right opcodes for the displacement.
2380 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
2381 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
2382 assert(LOpcode && CSOpcode && "Displacement out of range");
2383
2384 // Create virtual registers for temporary results.
2385 unsigned OrigVal = MRI.createVirtualRegister(RC);
2386 unsigned OldVal = MRI.createVirtualRegister(RC);
2387 unsigned NewVal = MRI.createVirtualRegister(RC);
2388 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
2389 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
2390 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
2391
2392 // Insert 3 basic blocks for the loop.
2393 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002394 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002395 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2396 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
2397 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
2398
2399 // StartMBB:
2400 // ...
2401 // %OrigVal = L Disp(%Base)
2402 // # fall through to LoopMMB
2403 MBB = StartMBB;
2404 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
2405 .addOperand(Base).addImm(Disp).addReg(0);
2406 MBB->addSuccessor(LoopMBB);
2407
2408 // LoopMBB:
2409 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
2410 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
2411 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00002412 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002413 MBB = LoopMBB;
2414 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2415 .addReg(OrigVal).addMBB(StartMBB)
2416 .addReg(Dest).addMBB(UpdateMBB);
2417 if (IsSubWord)
2418 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
2419 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00002420 BuildMI(MBB, DL, TII->get(CompareOpcode))
2421 .addReg(RotatedOldVal).addReg(Src2);
2422 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00002423 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002424 MBB->addSuccessor(UpdateMBB);
2425 MBB->addSuccessor(UseAltMBB);
2426
2427 // UseAltMBB:
2428 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
2429 // # fall through to UpdateMMB
2430 MBB = UseAltMBB;
2431 if (IsSubWord)
2432 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
2433 .addReg(RotatedOldVal).addReg(Src2)
2434 .addImm(32).addImm(31 + BitSize).addImm(0);
2435 MBB->addSuccessor(UpdateMBB);
2436
2437 // UpdateMBB:
2438 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
2439 // [ %RotatedAltVal, UseAltMBB ]
2440 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
2441 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
2442 // JNE LoopMBB
2443 // # fall through to DoneMMB
2444 MBB = UpdateMBB;
2445 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
2446 .addReg(RotatedOldVal).addMBB(LoopMBB)
2447 .addReg(RotatedAltVal).addMBB(UseAltMBB);
2448 if (IsSubWord)
2449 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
2450 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
2451 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
2452 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00002453 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2454 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002455 MBB->addSuccessor(LoopMBB);
2456 MBB->addSuccessor(DoneMBB);
2457
2458 MI->eraseFromParent();
2459 return DoneMBB;
2460}
2461
2462// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
2463// instruction MI.
2464MachineBasicBlock *
2465SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI,
2466 MachineBasicBlock *MBB) const {
2467 const SystemZInstrInfo *TII = TM.getInstrInfo();
2468 MachineFunction &MF = *MBB->getParent();
2469 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002470
2471 // Extract the operands. Base can be a register or a frame index.
2472 unsigned Dest = MI->getOperand(0).getReg();
2473 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2474 int64_t Disp = MI->getOperand(2).getImm();
2475 unsigned OrigCmpVal = MI->getOperand(3).getReg();
2476 unsigned OrigSwapVal = MI->getOperand(4).getReg();
2477 unsigned BitShift = MI->getOperand(5).getReg();
2478 unsigned NegBitShift = MI->getOperand(6).getReg();
2479 int64_t BitSize = MI->getOperand(7).getImm();
2480 DebugLoc DL = MI->getDebugLoc();
2481
2482 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
2483
2484 // Get the right opcodes for the displacement.
2485 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
2486 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
2487 assert(LOpcode && CSOpcode && "Displacement out of range");
2488
2489 // Create virtual registers for temporary results.
2490 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
2491 unsigned OldVal = MRI.createVirtualRegister(RC);
2492 unsigned CmpVal = MRI.createVirtualRegister(RC);
2493 unsigned SwapVal = MRI.createVirtualRegister(RC);
2494 unsigned StoreVal = MRI.createVirtualRegister(RC);
2495 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
2496 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
2497 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
2498
2499 // Insert 2 basic blocks for the loop.
2500 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002501 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002502 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2503 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
2504
2505 // StartMBB:
2506 // ...
2507 // %OrigOldVal = L Disp(%Base)
2508 // # fall through to LoopMMB
2509 MBB = StartMBB;
2510 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
2511 .addOperand(Base).addImm(Disp).addReg(0);
2512 MBB->addSuccessor(LoopMBB);
2513
2514 // LoopMBB:
2515 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
2516 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
2517 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
2518 // %Dest = RLL %OldVal, BitSize(%BitShift)
2519 // ^^ The low BitSize bits contain the field
2520 // of interest.
2521 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
2522 // ^^ Replace the upper 32-BitSize bits of the
2523 // comparison value with those that we loaded,
2524 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00002525 // CR %Dest, %RetryCmpVal
2526 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002527 // # Fall through to SetMBB
2528 MBB = LoopMBB;
2529 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2530 .addReg(OrigOldVal).addMBB(StartMBB)
2531 .addReg(RetryOldVal).addMBB(SetMBB);
2532 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
2533 .addReg(OrigCmpVal).addMBB(StartMBB)
2534 .addReg(RetryCmpVal).addMBB(SetMBB);
2535 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
2536 .addReg(OrigSwapVal).addMBB(StartMBB)
2537 .addReg(RetrySwapVal).addMBB(SetMBB);
2538 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
2539 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
2540 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
2541 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00002542 BuildMI(MBB, DL, TII->get(SystemZ::CR))
2543 .addReg(Dest).addReg(RetryCmpVal);
2544 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00002545 .addImm(SystemZ::CCMASK_ICMP)
2546 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002547 MBB->addSuccessor(DoneMBB);
2548 MBB->addSuccessor(SetMBB);
2549
2550 // SetMBB:
2551 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
2552 // ^^ Replace the upper 32-BitSize bits of the new
2553 // value with those that we loaded.
2554 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
2555 // ^^ Rotate the new field to its proper position.
2556 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
2557 // JNE LoopMBB
2558 // # fall through to ExitMMB
2559 MBB = SetMBB;
2560 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
2561 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
2562 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
2563 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
2564 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
2565 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00002566 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2567 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002568 MBB->addSuccessor(LoopMBB);
2569 MBB->addSuccessor(DoneMBB);
2570
2571 MI->eraseFromParent();
2572 return DoneMBB;
2573}
2574
2575// Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true
2576// if the high register of the GR128 value must be cleared or false if
2577// it's "don't care". SubReg is subreg_odd32 when extending a GR32
2578// and subreg_odd when extending a GR64.
2579MachineBasicBlock *
2580SystemZTargetLowering::emitExt128(MachineInstr *MI,
2581 MachineBasicBlock *MBB,
2582 bool ClearEven, unsigned SubReg) const {
2583 const SystemZInstrInfo *TII = TM.getInstrInfo();
2584 MachineFunction &MF = *MBB->getParent();
2585 MachineRegisterInfo &MRI = MF.getRegInfo();
2586 DebugLoc DL = MI->getDebugLoc();
2587
2588 unsigned Dest = MI->getOperand(0).getReg();
2589 unsigned Src = MI->getOperand(1).getReg();
2590 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
2591
2592 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
2593 if (ClearEven) {
2594 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
2595 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
2596
2597 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
2598 .addImm(0);
2599 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
2600 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_high);
2601 In128 = NewIn128;
2602 }
2603 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
2604 .addReg(In128).addReg(Src).addImm(SubReg);
2605
2606 MI->eraseFromParent();
2607 return MBB;
2608}
2609
Richard Sandifordd131ff82013-07-08 09:35:23 +00002610MachineBasicBlock *
Richard Sandiford564681c2013-08-12 10:28:10 +00002611SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI,
2612 MachineBasicBlock *MBB,
2613 unsigned Opcode) const {
Richard Sandifordd131ff82013-07-08 09:35:23 +00002614 const SystemZInstrInfo *TII = TM.getInstrInfo();
Richard Sandiford5e318f02013-08-27 09:54:29 +00002615 MachineFunction &MF = *MBB->getParent();
2616 MachineRegisterInfo &MRI = MF.getRegInfo();
Richard Sandifordd131ff82013-07-08 09:35:23 +00002617 DebugLoc DL = MI->getDebugLoc();
2618
Richard Sandiford5e318f02013-08-27 09:54:29 +00002619 MachineOperand DestBase = earlyUseOperand(MI->getOperand(0));
Richard Sandifordd131ff82013-07-08 09:35:23 +00002620 uint64_t DestDisp = MI->getOperand(1).getImm();
Richard Sandiford5e318f02013-08-27 09:54:29 +00002621 MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2));
Richard Sandifordd131ff82013-07-08 09:35:23 +00002622 uint64_t SrcDisp = MI->getOperand(3).getImm();
2623 uint64_t Length = MI->getOperand(4).getImm();
2624
Richard Sandifordbe133a82013-08-28 09:01:51 +00002625 // When generating more than one CLC, all but the last will need to
2626 // branch to the end when a difference is found.
2627 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
2628 splitBlockAfter(MI, MBB) : 0);
2629
Richard Sandiford5e318f02013-08-27 09:54:29 +00002630 // Check for the loop form, in which operand 5 is the trip count.
2631 if (MI->getNumExplicitOperands() > 5) {
2632 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
2633
2634 uint64_t StartCountReg = MI->getOperand(5).getReg();
2635 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
2636 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
2637 forceReg(MI, DestBase, TII));
2638
2639 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
2640 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
2641 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
2642 MRI.createVirtualRegister(RC));
2643 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
2644 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
2645 MRI.createVirtualRegister(RC));
2646
2647 RC = &SystemZ::GR64BitRegClass;
2648 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
2649 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
2650
2651 MachineBasicBlock *StartMBB = MBB;
2652 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
2653 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00002654 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00002655
2656 // StartMBB:
2657 // # fall through to LoopMMB
2658 MBB->addSuccessor(LoopMBB);
2659
2660 // LoopMBB:
2661 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00002662 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00002663 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00002664 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00002665 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00002666 // [ %NextCountReg, NextMBB ]
2667 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00002668 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00002669 // ( JLH EndMBB )
2670 //
2671 // The prefetch is used only for MVC. The JLH is used only for CLC.
2672 MBB = LoopMBB;
2673
2674 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
2675 .addReg(StartDestReg).addMBB(StartMBB)
2676 .addReg(NextDestReg).addMBB(NextMBB);
2677 if (!HaveSingleBase)
2678 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
2679 .addReg(StartSrcReg).addMBB(StartMBB)
2680 .addReg(NextSrcReg).addMBB(NextMBB);
2681 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
2682 .addReg(StartCountReg).addMBB(StartMBB)
2683 .addReg(NextCountReg).addMBB(NextMBB);
2684 if (Opcode == SystemZ::MVC)
2685 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
2686 .addImm(SystemZ::PFD_WRITE)
2687 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
2688 BuildMI(MBB, DL, TII->get(Opcode))
2689 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
2690 .addReg(ThisSrcReg).addImm(SrcDisp);
2691 if (EndMBB) {
2692 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2693 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
2694 .addMBB(EndMBB);
2695 MBB->addSuccessor(EndMBB);
2696 MBB->addSuccessor(NextMBB);
2697 }
2698
2699 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00002700 // %NextDestReg = LA 256(%ThisDestReg)
2701 // %NextSrcReg = LA 256(%ThisSrcReg)
2702 // %NextCountReg = AGHI %ThisCountReg, -1
2703 // CGHI %NextCountReg, 0
2704 // JLH LoopMBB
2705 // # fall through to DoneMMB
2706 //
2707 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00002708 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002709
Richard Sandiford5e318f02013-08-27 09:54:29 +00002710 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
2711 .addReg(ThisDestReg).addImm(256).addReg(0);
2712 if (!HaveSingleBase)
2713 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
2714 .addReg(ThisSrcReg).addImm(256).addReg(0);
2715 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
2716 .addReg(ThisCountReg).addImm(-1);
2717 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
2718 .addReg(NextCountReg).addImm(0);
2719 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2720 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
2721 .addMBB(LoopMBB);
2722 MBB->addSuccessor(LoopMBB);
2723 MBB->addSuccessor(DoneMBB);
2724
2725 DestBase = MachineOperand::CreateReg(NextDestReg, false);
2726 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
2727 Length &= 255;
2728 MBB = DoneMBB;
2729 }
2730 // Handle any remaining bytes with straight-line code.
2731 while (Length > 0) {
2732 uint64_t ThisLength = std::min(Length, uint64_t(256));
2733 // The previous iteration might have created out-of-range displacements.
2734 // Apply them using LAY if so.
2735 if (!isUInt<12>(DestDisp)) {
2736 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
2737 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
2738 .addOperand(DestBase).addImm(DestDisp).addReg(0);
2739 DestBase = MachineOperand::CreateReg(Reg, false);
2740 DestDisp = 0;
2741 }
2742 if (!isUInt<12>(SrcDisp)) {
2743 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
2744 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
2745 .addOperand(SrcBase).addImm(SrcDisp).addReg(0);
2746 SrcBase = MachineOperand::CreateReg(Reg, false);
2747 SrcDisp = 0;
2748 }
2749 BuildMI(*MBB, MI, DL, TII->get(Opcode))
2750 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength)
2751 .addOperand(SrcBase).addImm(SrcDisp);
2752 DestDisp += ThisLength;
2753 SrcDisp += ThisLength;
2754 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00002755 // If there's another CLC to go, branch to the end if a difference
2756 // was found.
2757 if (EndMBB && Length > 0) {
2758 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
2759 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2760 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
2761 .addMBB(EndMBB);
2762 MBB->addSuccessor(EndMBB);
2763 MBB->addSuccessor(NextMBB);
2764 MBB = NextMBB;
2765 }
2766 }
2767 if (EndMBB) {
2768 MBB->addSuccessor(EndMBB);
2769 MBB = EndMBB;
2770 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00002771 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00002772
2773 MI->eraseFromParent();
2774 return MBB;
2775}
2776
Richard Sandifordca232712013-08-16 11:21:54 +00002777// Decompose string pseudo-instruction MI into a loop that continually performs
2778// Opcode until CC != 3.
2779MachineBasicBlock *
2780SystemZTargetLowering::emitStringWrapper(MachineInstr *MI,
2781 MachineBasicBlock *MBB,
2782 unsigned Opcode) const {
2783 const SystemZInstrInfo *TII = TM.getInstrInfo();
2784 MachineFunction &MF = *MBB->getParent();
2785 MachineRegisterInfo &MRI = MF.getRegInfo();
2786 DebugLoc DL = MI->getDebugLoc();
2787
2788 uint64_t End1Reg = MI->getOperand(0).getReg();
2789 uint64_t Start1Reg = MI->getOperand(1).getReg();
2790 uint64_t Start2Reg = MI->getOperand(2).getReg();
2791 uint64_t CharReg = MI->getOperand(3).getReg();
2792
2793 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
2794 uint64_t This1Reg = MRI.createVirtualRegister(RC);
2795 uint64_t This2Reg = MRI.createVirtualRegister(RC);
2796 uint64_t End2Reg = MRI.createVirtualRegister(RC);
2797
2798 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00002799 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00002800 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2801
2802 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00002803 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00002804 MBB->addSuccessor(LoopMBB);
2805
2806 // LoopMBB:
2807 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
2808 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford6f6d5512013-08-20 09:38:48 +00002809 // R0W = %CharReg
Richard Sandifordca232712013-08-16 11:21:54 +00002810 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0W
2811 // JO LoopMBB
2812 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00002813 //
2814 // The load of R0W can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00002815 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00002816
2817 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
2818 .addReg(Start1Reg).addMBB(StartMBB)
2819 .addReg(End1Reg).addMBB(LoopMBB);
2820 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
2821 .addReg(Start2Reg).addMBB(StartMBB)
2822 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford6f6d5512013-08-20 09:38:48 +00002823 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0W).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00002824 BuildMI(MBB, DL, TII->get(Opcode))
2825 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
2826 .addReg(This1Reg).addReg(This2Reg);
2827 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2828 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
2829 MBB->addSuccessor(LoopMBB);
2830 MBB->addSuccessor(DoneMBB);
2831
2832 DoneMBB->addLiveIn(SystemZ::CC);
2833
2834 MI->eraseFromParent();
2835 return DoneMBB;
2836}
2837
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002838MachineBasicBlock *SystemZTargetLowering::
2839EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const {
2840 switch (MI->getOpcode()) {
2841 case SystemZ::Select32:
2842 case SystemZ::SelectF32:
2843 case SystemZ::Select64:
2844 case SystemZ::SelectF64:
2845 case SystemZ::SelectF128:
2846 return emitSelect(MI, MBB);
2847
Richard Sandifordb86a8342013-06-27 09:27:40 +00002848 case SystemZ::CondStore8_32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002849 return emitCondStore(MI, MBB, SystemZ::STC32, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002850 case SystemZ::CondStore8_32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002851 return emitCondStore(MI, MBB, SystemZ::STC32, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002852 case SystemZ::CondStore16_32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002853 return emitCondStore(MI, MBB, SystemZ::STH32, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002854 case SystemZ::CondStore16_32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002855 return emitCondStore(MI, MBB, SystemZ::STH32, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002856 case SystemZ::CondStore32_32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002857 return emitCondStore(MI, MBB, SystemZ::ST32, SystemZ::STOC32, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002858 case SystemZ::CondStore32_32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002859 return emitCondStore(MI, MBB, SystemZ::ST32, SystemZ::STOC32, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002860 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002861 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002862 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002863 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002864 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002865 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002866 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002867 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002868 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002869 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002870 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002871 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002872 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002873 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002874 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002875 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002876 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002877 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002878 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002879 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002880 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002881 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002882 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00002883 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00002884
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002885 case SystemZ::AEXT128_64:
2886 return emitExt128(MI, MBB, false, SystemZ::subreg_low);
2887 case SystemZ::ZEXT128_32:
2888 return emitExt128(MI, MBB, true, SystemZ::subreg_low32);
2889 case SystemZ::ZEXT128_64:
2890 return emitExt128(MI, MBB, true, SystemZ::subreg_low);
2891
2892 case SystemZ::ATOMIC_SWAPW:
2893 return emitAtomicLoadBinary(MI, MBB, 0, 0);
2894 case SystemZ::ATOMIC_SWAP_32:
2895 return emitAtomicLoadBinary(MI, MBB, 0, 32);
2896 case SystemZ::ATOMIC_SWAP_64:
2897 return emitAtomicLoadBinary(MI, MBB, 0, 64);
2898
2899 case SystemZ::ATOMIC_LOADW_AR:
2900 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
2901 case SystemZ::ATOMIC_LOADW_AFI:
2902 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
2903 case SystemZ::ATOMIC_LOAD_AR:
2904 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
2905 case SystemZ::ATOMIC_LOAD_AHI:
2906 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
2907 case SystemZ::ATOMIC_LOAD_AFI:
2908 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
2909 case SystemZ::ATOMIC_LOAD_AGR:
2910 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
2911 case SystemZ::ATOMIC_LOAD_AGHI:
2912 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
2913 case SystemZ::ATOMIC_LOAD_AGFI:
2914 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
2915
2916 case SystemZ::ATOMIC_LOADW_SR:
2917 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
2918 case SystemZ::ATOMIC_LOAD_SR:
2919 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
2920 case SystemZ::ATOMIC_LOAD_SGR:
2921 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
2922
2923 case SystemZ::ATOMIC_LOADW_NR:
2924 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
2925 case SystemZ::ATOMIC_LOADW_NILH:
2926 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH32, 0);
2927 case SystemZ::ATOMIC_LOAD_NR:
2928 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
2929 case SystemZ::ATOMIC_LOAD_NILL32:
2930 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL32, 32);
2931 case SystemZ::ATOMIC_LOAD_NILH32:
2932 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH32, 32);
2933 case SystemZ::ATOMIC_LOAD_NILF32:
2934 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF32, 32);
2935 case SystemZ::ATOMIC_LOAD_NGR:
2936 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
2937 case SystemZ::ATOMIC_LOAD_NILL:
2938 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 64);
2939 case SystemZ::ATOMIC_LOAD_NILH:
2940 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 64);
2941 case SystemZ::ATOMIC_LOAD_NIHL:
2942 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL, 64);
2943 case SystemZ::ATOMIC_LOAD_NIHH:
2944 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH, 64);
2945 case SystemZ::ATOMIC_LOAD_NILF:
2946 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 64);
2947 case SystemZ::ATOMIC_LOAD_NIHF:
2948 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF, 64);
2949
2950 case SystemZ::ATOMIC_LOADW_OR:
2951 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
2952 case SystemZ::ATOMIC_LOADW_OILH:
2953 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH32, 0);
2954 case SystemZ::ATOMIC_LOAD_OR:
2955 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
2956 case SystemZ::ATOMIC_LOAD_OILL32:
2957 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL32, 32);
2958 case SystemZ::ATOMIC_LOAD_OILH32:
2959 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH32, 32);
2960 case SystemZ::ATOMIC_LOAD_OILF32:
2961 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF32, 32);
2962 case SystemZ::ATOMIC_LOAD_OGR:
2963 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
2964 case SystemZ::ATOMIC_LOAD_OILL:
2965 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 64);
2966 case SystemZ::ATOMIC_LOAD_OILH:
2967 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 64);
2968 case SystemZ::ATOMIC_LOAD_OIHL:
2969 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL, 64);
2970 case SystemZ::ATOMIC_LOAD_OIHH:
2971 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH, 64);
2972 case SystemZ::ATOMIC_LOAD_OILF:
2973 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 64);
2974 case SystemZ::ATOMIC_LOAD_OIHF:
2975 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF, 64);
2976
2977 case SystemZ::ATOMIC_LOADW_XR:
2978 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
2979 case SystemZ::ATOMIC_LOADW_XILF:
2980 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF32, 0);
2981 case SystemZ::ATOMIC_LOAD_XR:
2982 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
2983 case SystemZ::ATOMIC_LOAD_XILF32:
2984 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF32, 32);
2985 case SystemZ::ATOMIC_LOAD_XGR:
2986 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
2987 case SystemZ::ATOMIC_LOAD_XILF:
2988 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 64);
2989 case SystemZ::ATOMIC_LOAD_XIHF:
2990 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF, 64);
2991
2992 case SystemZ::ATOMIC_LOADW_NRi:
2993 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
2994 case SystemZ::ATOMIC_LOADW_NILHi:
2995 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH32, 0, true);
2996 case SystemZ::ATOMIC_LOAD_NRi:
2997 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
2998 case SystemZ::ATOMIC_LOAD_NILL32i:
2999 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL32, 32, true);
3000 case SystemZ::ATOMIC_LOAD_NILH32i:
3001 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH32, 32, true);
3002 case SystemZ::ATOMIC_LOAD_NILF32i:
3003 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF32, 32, true);
3004 case SystemZ::ATOMIC_LOAD_NGRi:
3005 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
3006 case SystemZ::ATOMIC_LOAD_NILLi:
3007 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 64, true);
3008 case SystemZ::ATOMIC_LOAD_NILHi:
3009 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 64, true);
3010 case SystemZ::ATOMIC_LOAD_NIHLi:
3011 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL, 64, true);
3012 case SystemZ::ATOMIC_LOAD_NIHHi:
3013 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH, 64, true);
3014 case SystemZ::ATOMIC_LOAD_NILFi:
3015 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 64, true);
3016 case SystemZ::ATOMIC_LOAD_NIHFi:
3017 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF, 64, true);
3018
3019 case SystemZ::ATOMIC_LOADW_MIN:
3020 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3021 SystemZ::CCMASK_CMP_LE, 0);
3022 case SystemZ::ATOMIC_LOAD_MIN_32:
3023 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3024 SystemZ::CCMASK_CMP_LE, 32);
3025 case SystemZ::ATOMIC_LOAD_MIN_64:
3026 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
3027 SystemZ::CCMASK_CMP_LE, 64);
3028
3029 case SystemZ::ATOMIC_LOADW_MAX:
3030 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3031 SystemZ::CCMASK_CMP_GE, 0);
3032 case SystemZ::ATOMIC_LOAD_MAX_32:
3033 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3034 SystemZ::CCMASK_CMP_GE, 32);
3035 case SystemZ::ATOMIC_LOAD_MAX_64:
3036 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
3037 SystemZ::CCMASK_CMP_GE, 64);
3038
3039 case SystemZ::ATOMIC_LOADW_UMIN:
3040 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3041 SystemZ::CCMASK_CMP_LE, 0);
3042 case SystemZ::ATOMIC_LOAD_UMIN_32:
3043 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3044 SystemZ::CCMASK_CMP_LE, 32);
3045 case SystemZ::ATOMIC_LOAD_UMIN_64:
3046 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
3047 SystemZ::CCMASK_CMP_LE, 64);
3048
3049 case SystemZ::ATOMIC_LOADW_UMAX:
3050 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3051 SystemZ::CCMASK_CMP_GE, 0);
3052 case SystemZ::ATOMIC_LOAD_UMAX_32:
3053 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3054 SystemZ::CCMASK_CMP_GE, 32);
3055 case SystemZ::ATOMIC_LOAD_UMAX_64:
3056 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
3057 SystemZ::CCMASK_CMP_GE, 64);
3058
3059 case SystemZ::ATOMIC_CMP_SWAPW:
3060 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00003061 case SystemZ::MVCSequence:
3062 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00003063 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00003064 case SystemZ::NCSequence:
3065 case SystemZ::NCLoop:
3066 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
3067 case SystemZ::OCSequence:
3068 case SystemZ::OCLoop:
3069 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
3070 case SystemZ::XCSequence:
3071 case SystemZ::XCLoop:
3072 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00003073 case SystemZ::CLCSequence:
3074 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00003075 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00003076 case SystemZ::CLSTLoop:
3077 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00003078 case SystemZ::MVSTLoop:
3079 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00003080 case SystemZ::SRSTLoop:
3081 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003082 default:
3083 llvm_unreachable("Unexpected instr type to insert");
3084 }
3085}