1 //===-- SystemZISelLowering.cpp - SystemZ DAG lowering implementation -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the SystemZTargetLowering class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "systemz-lower"
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"
30 // Represents a sequence for extracting a 0/1 value from an IPM result:
31 // (((X ^ XORValue) + AddValue) >> Bit)
32 struct IPMConversion {
33 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
34 : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
41 // Represents information about a comparison.
43 Comparison(SDValue Op0In, SDValue Op1In)
44 : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {}
46 // The operands to the comparison.
49 // The opcode that should be used to compare Op0 and Op1.
52 // A SystemZICMP value. Only used for integer comparisons.
55 // The mask of CC values that Opcode can produce.
58 // The mask of CC values for which the original condition is true.
63 // Classify VT as either 32 or 64 bit.
64 static bool is32Bit(EVT VT) {
65 switch (VT.getSimpleVT().SimpleTy) {
71 llvm_unreachable("Unsupported type");
75 // Return a version of MachineOperand that can be safely used before the
77 static MachineOperand earlyUseOperand(MachineOperand Op) {
83 SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm)
84 : TargetLowering(tm, new TargetLoweringObjectFileELF()),
85 Subtarget(*tm.getSubtargetImpl()), TM(tm) {
86 MVT PtrVT = getPointerTy();
88 // Set up the register classes.
89 if (Subtarget.hasHighWord())
90 addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass);
92 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
93 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
94 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
95 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
96 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
98 // Compute derived properties from the register classes
99 computeRegisterProperties();
101 // Set up special registers.
102 setExceptionPointerRegister(SystemZ::R6D);
103 setExceptionSelectorRegister(SystemZ::R7D);
104 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
106 // TODO: It may be better to default to latency-oriented scheduling, however
107 // LLVM's current latency-oriented scheduler can't handle physreg definitions
108 // such as SystemZ has with CC, so set this to the register-pressure
109 // scheduler, because it can.
110 setSchedulingPreference(Sched::RegPressure);
112 setBooleanContents(ZeroOrOneBooleanContent);
113 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
115 // Instructions are strings of 2-byte aligned 2-byte values.
116 setMinFunctionAlignment(2);
118 // Handle operations that are handled in a similar way for all types.
119 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
120 I <= MVT::LAST_FP_VALUETYPE;
122 MVT VT = MVT::SimpleValueType(I);
123 if (isTypeLegal(VT)) {
124 // Lower SET_CC into an IPM-based sequence.
125 setOperationAction(ISD::SETCC, VT, Custom);
127 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
128 setOperationAction(ISD::SELECT, VT, Expand);
130 // Lower SELECT_CC and BR_CC into separate comparisons and branches.
131 setOperationAction(ISD::SELECT_CC, VT, Custom);
132 setOperationAction(ISD::BR_CC, VT, Custom);
136 // Expand jump table branches as address arithmetic followed by an
138 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
140 // Expand BRCOND into a BR_CC (see above).
141 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
143 // Handle integer types.
144 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
145 I <= MVT::LAST_INTEGER_VALUETYPE;
147 MVT VT = MVT::SimpleValueType(I);
148 if (isTypeLegal(VT)) {
149 // Expand individual DIV and REMs into DIVREMs.
150 setOperationAction(ISD::SDIV, VT, Expand);
151 setOperationAction(ISD::UDIV, VT, Expand);
152 setOperationAction(ISD::SREM, VT, Expand);
153 setOperationAction(ISD::UREM, VT, Expand);
154 setOperationAction(ISD::SDIVREM, VT, Custom);
155 setOperationAction(ISD::UDIVREM, VT, Custom);
157 // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and
158 // stores, putting a serialization instruction after the stores.
159 setOperationAction(ISD::ATOMIC_LOAD, VT, Custom);
160 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
162 // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are
163 // available, or if the operand is constant.
164 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
166 // No special instructions for these.
167 setOperationAction(ISD::CTPOP, VT, Expand);
168 setOperationAction(ISD::CTTZ, VT, Expand);
169 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
170 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
171 setOperationAction(ISD::ROTR, VT, Expand);
173 // Use *MUL_LOHI where possible instead of MULH*.
174 setOperationAction(ISD::MULHS, VT, Expand);
175 setOperationAction(ISD::MULHU, VT, Expand);
176 setOperationAction(ISD::SMUL_LOHI, VT, Custom);
177 setOperationAction(ISD::UMUL_LOHI, VT, Custom);
179 // We have instructions for signed but not unsigned FP conversion.
180 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
184 // Type legalization will convert 8- and 16-bit atomic operations into
185 // forms that operate on i32s (but still keeping the original memory VT).
186 // Lower them into full i32 operations.
187 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom);
188 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom);
189 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
190 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom);
191 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom);
192 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom);
193 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
194 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom);
195 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom);
196 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
197 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
198 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
200 // We have instructions for signed but not unsigned FP conversion.
201 // Handle unsigned 32-bit types as signed 64-bit types.
202 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
203 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
205 // We have native support for a 64-bit CTLZ, via FLOGR.
206 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
207 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
209 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
210 setOperationAction(ISD::OR, MVT::i64, Custom);
212 // FIXME: Can we support these natively?
213 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
214 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
215 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
217 // We have native instructions for i8, i16 and i32 extensions, but not i1.
218 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
219 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
220 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
221 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
223 // Handle the various types of symbolic address.
224 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
225 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
226 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
227 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
228 setOperationAction(ISD::JumpTable, PtrVT, Custom);
230 // We need to handle dynamic allocations specially because of the
231 // 160-byte area at the bottom of the stack.
232 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
234 // Use custom expanders so that we can force the function to use
236 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
237 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
239 // Handle prefetches with PFD or PFDRL.
240 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
242 // Handle floating-point types.
243 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
244 I <= MVT::LAST_FP_VALUETYPE;
246 MVT VT = MVT::SimpleValueType(I);
247 if (isTypeLegal(VT)) {
248 // We can use FI for FRINT.
249 setOperationAction(ISD::FRINT, VT, Legal);
251 // We can use the extended form of FI for other rounding operations.
252 if (Subtarget.hasFPExtension()) {
253 setOperationAction(ISD::FNEARBYINT, VT, Legal);
254 setOperationAction(ISD::FFLOOR, VT, Legal);
255 setOperationAction(ISD::FCEIL, VT, Legal);
256 setOperationAction(ISD::FTRUNC, VT, Legal);
257 setOperationAction(ISD::FROUND, VT, Legal);
260 // No special instructions for these.
261 setOperationAction(ISD::FSIN, VT, Expand);
262 setOperationAction(ISD::FCOS, VT, Expand);
263 setOperationAction(ISD::FREM, VT, Expand);
267 // We have fused multiply-addition for f32 and f64 but not f128.
268 setOperationAction(ISD::FMA, MVT::f32, Legal);
269 setOperationAction(ISD::FMA, MVT::f64, Legal);
270 setOperationAction(ISD::FMA, MVT::f128, Expand);
272 // Needed so that we don't try to implement f128 constant loads using
273 // a load-and-extend of a f80 constant (in cases where the constant
274 // would fit in an f80).
275 setLoadExtAction(ISD::EXTLOAD, MVT::f80, Expand);
277 // Floating-point truncation and stores need to be done separately.
278 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
279 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
280 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
282 // We have 64-bit FPR<->GPR moves, but need special handling for
284 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
285 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
287 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
288 // structure, but VAEND is a no-op.
289 setOperationAction(ISD::VASTART, MVT::Other, Custom);
290 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
291 setOperationAction(ISD::VAEND, MVT::Other, Expand);
293 // We want to use MVC in preference to even a single load/store pair.
294 MaxStoresPerMemcpy = 0;
295 MaxStoresPerMemcpyOptSize = 0;
297 // The main memset sequence is a byte store followed by an MVC.
298 // Two STC or MV..I stores win over that, but the kind of fused stores
299 // generated by target-independent code don't when the byte value is
300 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
301 // than "STC;MVC". Handle the choice in target-specific code instead.
302 MaxStoresPerMemset = 0;
303 MaxStoresPerMemsetOptSize = 0;
306 EVT SystemZTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
309 return VT.changeVectorElementTypeToInteger();
312 bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
313 VT = VT.getScalarType();
318 switch (VT.getSimpleVT().SimpleTy) {
331 bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
332 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
333 return Imm.isZero() || Imm.isNegZero();
336 bool SystemZTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
338 // Unaligned accesses should never be slower than the expanded version.
339 // We check specifically for aligned accesses in the few cases where
340 // they are required.
346 bool SystemZTargetLowering::isLegalAddressingMode(const AddrMode &AM,
348 // Punt on globals for now, although they can be used in limited
349 // RELATIVE LONG cases.
353 // Require a 20-bit signed offset.
354 if (!isInt<20>(AM.BaseOffs))
357 // Indexing is OK but no scale factor can be applied.
358 return AM.Scale == 0 || AM.Scale == 1;
361 bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
362 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
364 unsigned FromBits = FromType->getPrimitiveSizeInBits();
365 unsigned ToBits = ToType->getPrimitiveSizeInBits();
366 return FromBits > ToBits;
369 bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
370 if (!FromVT.isInteger() || !ToVT.isInteger())
372 unsigned FromBits = FromVT.getSizeInBits();
373 unsigned ToBits = ToVT.getSizeInBits();
374 return FromBits > ToBits;
377 //===----------------------------------------------------------------------===//
378 // Inline asm support
379 //===----------------------------------------------------------------------===//
381 TargetLowering::ConstraintType
382 SystemZTargetLowering::getConstraintType(const std::string &Constraint) const {
383 if (Constraint.size() == 1) {
384 switch (Constraint[0]) {
385 case 'a': // Address register
386 case 'd': // Data register (equivalent to 'r')
387 case 'f': // Floating-point register
388 case 'h': // High-part register
389 case 'r': // General-purpose register
390 return C_RegisterClass;
392 case 'Q': // Memory with base and unsigned 12-bit displacement
393 case 'R': // Likewise, plus an index
394 case 'S': // Memory with base and signed 20-bit displacement
395 case 'T': // Likewise, plus an index
396 case 'm': // Equivalent to 'T'.
399 case 'I': // Unsigned 8-bit constant
400 case 'J': // Unsigned 12-bit constant
401 case 'K': // Signed 16-bit constant
402 case 'L': // Signed 20-bit displacement (on all targets we support)
403 case 'M': // 0x7fffffff
410 return TargetLowering::getConstraintType(Constraint);
413 TargetLowering::ConstraintWeight SystemZTargetLowering::
414 getSingleConstraintMatchWeight(AsmOperandInfo &info,
415 const char *constraint) const {
416 ConstraintWeight weight = CW_Invalid;
417 Value *CallOperandVal = info.CallOperandVal;
418 // If we don't have a value, we can't do a match,
419 // but allow it at the lowest weight.
420 if (CallOperandVal == NULL)
422 Type *type = CallOperandVal->getType();
423 // Look at the constraint type.
424 switch (*constraint) {
426 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
429 case 'a': // Address register
430 case 'd': // Data register (equivalent to 'r')
431 case 'h': // High-part register
432 case 'r': // General-purpose register
433 if (CallOperandVal->getType()->isIntegerTy())
434 weight = CW_Register;
437 case 'f': // Floating-point register
438 if (type->isFloatingPointTy())
439 weight = CW_Register;
442 case 'I': // Unsigned 8-bit constant
443 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
444 if (isUInt<8>(C->getZExtValue()))
445 weight = CW_Constant;
448 case 'J': // Unsigned 12-bit constant
449 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
450 if (isUInt<12>(C->getZExtValue()))
451 weight = CW_Constant;
454 case 'K': // Signed 16-bit constant
455 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
456 if (isInt<16>(C->getSExtValue()))
457 weight = CW_Constant;
460 case 'L': // Signed 20-bit displacement (on all targets we support)
461 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
462 if (isInt<20>(C->getSExtValue()))
463 weight = CW_Constant;
466 case 'M': // 0x7fffffff
467 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
468 if (C->getZExtValue() == 0x7fffffff)
469 weight = CW_Constant;
475 // Parse a "{tNNN}" register constraint for which the register type "t"
476 // has already been verified. MC is the class associated with "t" and
477 // Map maps 0-based register numbers to LLVM register numbers.
478 static std::pair<unsigned, const TargetRegisterClass *>
479 parseRegisterNumber(const std::string &Constraint,
480 const TargetRegisterClass *RC, const unsigned *Map) {
481 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
482 if (isdigit(Constraint[2])) {
483 std::string Suffix(Constraint.data() + 2, Constraint.size() - 2);
484 unsigned Index = atoi(Suffix.c_str());
485 if (Index < 16 && Map[Index])
486 return std::make_pair(Map[Index], RC);
488 return std::make_pair(0u, static_cast<TargetRegisterClass*>(0));
491 std::pair<unsigned, const TargetRegisterClass *> SystemZTargetLowering::
492 getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const {
493 if (Constraint.size() == 1) {
494 // GCC Constraint Letters
495 switch (Constraint[0]) {
497 case 'd': // Data register (equivalent to 'r')
498 case 'r': // General-purpose register
500 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
501 else if (VT == MVT::i128)
502 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
503 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
505 case 'a': // Address register
507 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
508 else if (VT == MVT::i128)
509 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
510 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
512 case 'h': // High-part register (an LLVM extension)
513 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
515 case 'f': // Floating-point register
517 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
518 else if (VT == MVT::f128)
519 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
520 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
523 if (Constraint[0] == '{') {
524 // We need to override the default register parsing for GPRs and FPRs
525 // because the interpretation depends on VT. The internal names of
526 // the registers are also different from the external names
527 // (F0D and F0S instead of F0, etc.).
528 if (Constraint[1] == 'r') {
530 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
531 SystemZMC::GR32Regs);
533 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
534 SystemZMC::GR128Regs);
535 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
536 SystemZMC::GR64Regs);
538 if (Constraint[1] == 'f') {
540 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
541 SystemZMC::FP32Regs);
543 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
544 SystemZMC::FP128Regs);
545 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
546 SystemZMC::FP64Regs);
549 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
552 void SystemZTargetLowering::
553 LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
554 std::vector<SDValue> &Ops,
555 SelectionDAG &DAG) const {
556 // Only support length 1 constraints for now.
557 if (Constraint.length() == 1) {
558 switch (Constraint[0]) {
559 case 'I': // Unsigned 8-bit constant
560 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
561 if (isUInt<8>(C->getZExtValue()))
562 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
566 case 'J': // Unsigned 12-bit constant
567 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
568 if (isUInt<12>(C->getZExtValue()))
569 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
573 case 'K': // Signed 16-bit constant
574 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
575 if (isInt<16>(C->getSExtValue()))
576 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
580 case 'L': // Signed 20-bit displacement (on all targets we support)
581 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
582 if (isInt<20>(C->getSExtValue()))
583 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
587 case 'M': // 0x7fffffff
588 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
589 if (C->getZExtValue() == 0x7fffffff)
590 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
595 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
598 //===----------------------------------------------------------------------===//
599 // Calling conventions
600 //===----------------------------------------------------------------------===//
602 #include "SystemZGenCallingConv.inc"
604 bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
605 Type *ToType) const {
606 return isTruncateFree(FromType, ToType);
609 bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
610 if (!CI->isTailCall())
615 // Value is a value that has been passed to us in the location described by VA
616 // (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
617 // any loads onto Chain.
618 static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL,
619 CCValAssign &VA, SDValue Chain,
621 // If the argument has been promoted from a smaller type, insert an
622 // assertion to capture this.
623 if (VA.getLocInfo() == CCValAssign::SExt)
624 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
625 DAG.getValueType(VA.getValVT()));
626 else if (VA.getLocInfo() == CCValAssign::ZExt)
627 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
628 DAG.getValueType(VA.getValVT()));
631 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
632 else if (VA.getLocInfo() == CCValAssign::Indirect)
633 Value = DAG.getLoad(VA.getValVT(), DL, Chain, Value,
634 MachinePointerInfo(), false, false, false, 0);
636 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
640 // Value is a value of type VA.getValVT() that we need to copy into
641 // the location described by VA. Return a copy of Value converted to
642 // VA.getValVT(). The caller is responsible for handling indirect values.
643 static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL,
644 CCValAssign &VA, SDValue Value) {
645 switch (VA.getLocInfo()) {
646 case CCValAssign::SExt:
647 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
648 case CCValAssign::ZExt:
649 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
650 case CCValAssign::AExt:
651 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
652 case CCValAssign::Full:
655 llvm_unreachable("Unhandled getLocInfo()");
659 SDValue SystemZTargetLowering::
660 LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
661 const SmallVectorImpl<ISD::InputArg> &Ins,
662 SDLoc DL, SelectionDAG &DAG,
663 SmallVectorImpl<SDValue> &InVals) const {
664 MachineFunction &MF = DAG.getMachineFunction();
665 MachineFrameInfo *MFI = MF.getFrameInfo();
666 MachineRegisterInfo &MRI = MF.getRegInfo();
667 SystemZMachineFunctionInfo *FuncInfo =
668 MF.getInfo<SystemZMachineFunctionInfo>();
669 const SystemZFrameLowering *TFL =
670 static_cast<const SystemZFrameLowering *>(TM.getFrameLowering());
672 // Assign locations to all of the incoming arguments.
673 SmallVector<CCValAssign, 16> ArgLocs;
674 CCState CCInfo(CallConv, IsVarArg, MF, TM, ArgLocs, *DAG.getContext());
675 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
677 unsigned NumFixedGPRs = 0;
678 unsigned NumFixedFPRs = 0;
679 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
681 CCValAssign &VA = ArgLocs[I];
682 EVT LocVT = VA.getLocVT();
684 // Arguments passed in registers
685 const TargetRegisterClass *RC;
686 switch (LocVT.getSimpleVT().SimpleTy) {
688 // Integers smaller than i64 should be promoted to i64.
689 llvm_unreachable("Unexpected argument type");
692 RC = &SystemZ::GR32BitRegClass;
696 RC = &SystemZ::GR64BitRegClass;
700 RC = &SystemZ::FP32BitRegClass;
704 RC = &SystemZ::FP64BitRegClass;
708 unsigned VReg = MRI.createVirtualRegister(RC);
709 MRI.addLiveIn(VA.getLocReg(), VReg);
710 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
712 assert(VA.isMemLoc() && "Argument not register or memory");
714 // Create the frame index object for this incoming parameter.
715 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
716 VA.getLocMemOffset(), true);
718 // Create the SelectionDAG nodes corresponding to a load
719 // from this parameter. Unpromoted ints and floats are
720 // passed as right-justified 8-byte values.
721 EVT PtrVT = getPointerTy();
722 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
723 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
724 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, DAG.getIntPtrConstant(4));
725 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
726 MachinePointerInfo::getFixedStack(FI),
727 false, false, false, 0);
730 // Convert the value of the argument register into the value that's
732 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
736 // Save the number of non-varargs registers for later use by va_start, etc.
737 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
738 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
740 // Likewise the address (in the form of a frame index) of where the
741 // first stack vararg would be. The 1-byte size here is arbitrary.
742 int64_t StackSize = CCInfo.getNextStackOffset();
743 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true));
745 // ...and a similar frame index for the caller-allocated save area
746 // that will be used to store the incoming registers.
747 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
748 unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true);
749 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
751 // Store the FPR varargs in the reserved frame slots. (We store the
752 // GPRs as part of the prologue.)
753 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
754 SDValue MemOps[SystemZ::NumArgFPRs];
755 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
756 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
757 int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true);
758 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
759 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
760 &SystemZ::FP64BitRegClass);
761 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
762 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
763 MachinePointerInfo::getFixedStack(FI),
767 // Join the stores, which are independent of one another.
768 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
769 &MemOps[NumFixedFPRs],
770 SystemZ::NumArgFPRs - NumFixedFPRs);
777 static bool canUseSiblingCall(CCState ArgCCInfo,
778 SmallVectorImpl<CCValAssign> &ArgLocs) {
779 // Punt if there are any indirect or stack arguments, or if the call
780 // needs the call-saved argument register R6.
781 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
782 CCValAssign &VA = ArgLocs[I];
783 if (VA.getLocInfo() == CCValAssign::Indirect)
787 unsigned Reg = VA.getLocReg();
788 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
795 SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
796 SmallVectorImpl<SDValue> &InVals) const {
797 SelectionDAG &DAG = CLI.DAG;
799 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
800 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
801 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
802 SDValue Chain = CLI.Chain;
803 SDValue Callee = CLI.Callee;
804 bool &IsTailCall = CLI.IsTailCall;
805 CallingConv::ID CallConv = CLI.CallConv;
806 bool IsVarArg = CLI.IsVarArg;
807 MachineFunction &MF = DAG.getMachineFunction();
808 EVT PtrVT = getPointerTy();
810 // Analyze the operands of the call, assigning locations to each operand.
811 SmallVector<CCValAssign, 16> ArgLocs;
812 CCState ArgCCInfo(CallConv, IsVarArg, MF, TM, ArgLocs, *DAG.getContext());
813 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
815 // We don't support GuaranteedTailCallOpt, only automatically-detected
817 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs))
820 // Get a count of how many bytes are to be pushed on the stack.
821 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
823 // Mark the start of the call.
825 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumBytes, PtrVT, true),
828 // Copy argument values to their designated locations.
829 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
830 SmallVector<SDValue, 8> MemOpChains;
832 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
833 CCValAssign &VA = ArgLocs[I];
834 SDValue ArgValue = OutVals[I];
836 if (VA.getLocInfo() == CCValAssign::Indirect) {
837 // Store the argument in a stack slot and pass its address.
838 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
839 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
840 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, SpillSlot,
841 MachinePointerInfo::getFixedStack(FI),
843 ArgValue = SpillSlot;
845 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
848 // Queue up the argument copies and emit them at the end.
849 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
851 assert(VA.isMemLoc() && "Argument not register or memory");
853 // Work out the address of the stack slot. Unpromoted ints and
854 // floats are passed as right-justified 8-byte values.
855 if (!StackPtr.getNode())
856 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
857 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
858 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
860 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
861 DAG.getIntPtrConstant(Offset));
864 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address,
865 MachinePointerInfo(),
870 // Join the stores, which are independent of one another.
871 if (!MemOpChains.empty())
872 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
873 &MemOpChains[0], MemOpChains.size());
875 // Accept direct calls by converting symbolic call addresses to the
876 // associated Target* opcodes. Force %r1 to be used for indirect
879 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
880 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
881 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
882 } else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
883 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
884 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
885 } else if (IsTailCall) {
886 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
887 Glue = Chain.getValue(1);
888 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
891 // Build a sequence of copy-to-reg nodes, chained and glued together.
892 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
893 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
894 RegsToPass[I].second, Glue);
895 Glue = Chain.getValue(1);
898 // The first call operand is the chain and the second is the target address.
899 SmallVector<SDValue, 8> Ops;
900 Ops.push_back(Chain);
901 Ops.push_back(Callee);
903 // Add argument registers to the end of the list so that they are
904 // known live into the call.
905 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
906 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
907 RegsToPass[I].second.getValueType()));
909 // Glue the call to the argument copies, if any.
914 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
916 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, &Ops[0], Ops.size());
917 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, &Ops[0], Ops.size());
918 Glue = Chain.getValue(1);
920 // Mark the end of the call, which is glued to the call itself.
921 Chain = DAG.getCALLSEQ_END(Chain,
922 DAG.getConstant(NumBytes, PtrVT, true),
923 DAG.getConstant(0, PtrVT, true),
925 Glue = Chain.getValue(1);
927 // Assign locations to each value returned by this call.
928 SmallVector<CCValAssign, 16> RetLocs;
929 CCState RetCCInfo(CallConv, IsVarArg, MF, TM, RetLocs, *DAG.getContext());
930 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
932 // Copy all of the result registers out of their specified physreg.
933 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
934 CCValAssign &VA = RetLocs[I];
936 // Copy the value out, gluing the copy to the end of the call sequence.
937 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
938 VA.getLocVT(), Glue);
939 Chain = RetValue.getValue(1);
940 Glue = RetValue.getValue(2);
942 // Convert the value of the return register into the value that's
944 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
951 SystemZTargetLowering::LowerReturn(SDValue Chain,
952 CallingConv::ID CallConv, bool IsVarArg,
953 const SmallVectorImpl<ISD::OutputArg> &Outs,
954 const SmallVectorImpl<SDValue> &OutVals,
955 SDLoc DL, SelectionDAG &DAG) const {
956 MachineFunction &MF = DAG.getMachineFunction();
958 // Assign locations to each returned value.
959 SmallVector<CCValAssign, 16> RetLocs;
960 CCState RetCCInfo(CallConv, IsVarArg, MF, TM, RetLocs, *DAG.getContext());
961 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
963 // Quick exit for void returns
965 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
967 // Copy the result values into the output registers.
969 SmallVector<SDValue, 4> RetOps;
970 RetOps.push_back(Chain);
971 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
972 CCValAssign &VA = RetLocs[I];
973 SDValue RetValue = OutVals[I];
975 // Make the return register live on exit.
976 assert(VA.isRegLoc() && "Can only return in registers!");
978 // Promote the value as required.
979 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
981 // Chain and glue the copies together.
982 unsigned Reg = VA.getLocReg();
983 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
984 Glue = Chain.getValue(1);
985 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
988 // Update chain and glue.
991 RetOps.push_back(Glue);
993 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other,
994 RetOps.data(), RetOps.size());
997 SDValue SystemZTargetLowering::
998 prepareVolatileOrAtomicLoad(SDValue Chain, SDLoc DL, SelectionDAG &DAG) const {
999 return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain);
1002 // CC is a comparison that will be implemented using an integer or
1003 // floating-point comparison. Return the condition code mask for
1004 // a branch on true. In the integer case, CCMASK_CMP_UO is set for
1005 // unsigned comparisons and clear for signed ones. In the floating-point
1006 // case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1007 static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1009 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1010 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1011 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1015 llvm_unreachable("Invalid integer condition!");
1024 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1025 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1030 // Return a sequence for getting a 1 from an IPM result when CC has a
1031 // value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1032 // The handling of CC values outside CCValid doesn't matter.
1033 static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1034 // Deal with cases where the result can be taken directly from a bit
1035 // of the IPM result.
1036 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1037 return IPMConversion(0, 0, SystemZ::IPM_CC);
1038 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1039 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1041 // Deal with cases where we can add a value to force the sign bit
1042 // to contain the right value. Putting the bit in 31 means we can
1043 // use SRL rather than RISBG(L), and also makes it easier to get a
1044 // 0/-1 value, so it has priority over the other tests below.
1046 // These sequences rely on the fact that the upper two bits of the
1047 // IPM result are zero.
1048 uint64_t TopBit = uint64_t(1) << 31;
1049 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1050 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1051 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1052 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1053 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1055 | SystemZ::CCMASK_2)))
1056 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1057 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1058 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1059 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1061 | SystemZ::CCMASK_3)))
1062 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1064 // Next try inverting the value and testing a bit. 0/1 could be
1065 // handled this way too, but we dealt with that case above.
1066 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1067 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1069 // Handle cases where adding a value forces a non-sign bit to contain
1071 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1072 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1073 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1074 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1076 // The remaing cases are 1, 2, 0/1/3 and 0/2/3. All these are
1077 // can be done by inverting the low CC bit and applying one of the
1078 // sign-based extractions above.
1079 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1080 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1081 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1082 return IPMConversion(1 << SystemZ::IPM_CC,
1083 TopBit - (3 << SystemZ::IPM_CC), 31);
1084 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1086 | SystemZ::CCMASK_3)))
1087 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1088 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1090 | SystemZ::CCMASK_3)))
1091 return IPMConversion(1 << SystemZ::IPM_CC,
1092 TopBit - (1 << SystemZ::IPM_CC), 31);
1094 llvm_unreachable("Unexpected CC combination");
1097 // If C can be converted to a comparison against zero, adjust the operands
1099 static void adjustZeroCmp(SelectionDAG &DAG, Comparison &C) {
1100 if (C.ICmpType == SystemZICMP::UnsignedOnly)
1103 ConstantSDNode *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
1107 int64_t Value = ConstOp1->getSExtValue();
1108 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1109 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1110 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1111 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1112 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1113 C.Op1 = DAG.getConstant(0, C.Op1.getValueType());
1117 // If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1118 // adjust the operands as necessary.
1119 static void adjustSubwordCmp(SelectionDAG &DAG, Comparison &C) {
1120 // For us to make any changes, it must a comparison between a single-use
1121 // load and a constant.
1122 if (!C.Op0.hasOneUse() ||
1123 C.Op0.getOpcode() != ISD::LOAD ||
1124 C.Op1.getOpcode() != ISD::Constant)
1127 // We must have an 8- or 16-bit load.
1128 LoadSDNode *Load = cast<LoadSDNode>(C.Op0);
1129 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1130 if (NumBits != 8 && NumBits != 16)
1133 // The load must be an extending one and the constant must be within the
1134 // range of the unextended value.
1135 ConstantSDNode *ConstOp1 = cast<ConstantSDNode>(C.Op1);
1136 uint64_t Value = ConstOp1->getZExtValue();
1137 uint64_t Mask = (1 << NumBits) - 1;
1138 if (Load->getExtensionType() == ISD::SEXTLOAD) {
1139 // Make sure that ConstOp1 is in range of C.Op0.
1140 int64_t SignedValue = ConstOp1->getSExtValue();
1141 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
1143 if (C.ICmpType != SystemZICMP::SignedOnly) {
1144 // Unsigned comparison between two sign-extended values is equivalent
1145 // to unsigned comparison between two zero-extended values.
1147 } else if (NumBits == 8) {
1148 // Try to treat the comparison as unsigned, so that we can use CLI.
1149 // Adjust CCMask and Value as necessary.
1150 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
1151 // Test whether the high bit of the byte is set.
1152 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1153 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
1154 // Test whether the high bit of the byte is clear.
1155 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
1157 // No instruction exists for this combination.
1159 C.ICmpType = SystemZICMP::UnsignedOnly;
1161 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1164 assert(C.ICmpType == SystemZICMP::Any &&
1165 "Signedness shouldn't matter here.");
1169 // Make sure that the first operand is an i32 of the right extension type.
1170 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1173 if (C.Op0.getValueType() != MVT::i32 ||
1174 Load->getExtensionType() != ExtType)
1175 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32,
1176 Load->getChain(), Load->getBasePtr(),
1177 Load->getPointerInfo(), Load->getMemoryVT(),
1178 Load->isVolatile(), Load->isNonTemporal(),
1179 Load->getAlignment());
1181 // Make sure that the second operand is an i32 with the right value.
1182 if (C.Op1.getValueType() != MVT::i32 ||
1183 Value != ConstOp1->getZExtValue())
1184 C.Op1 = DAG.getConstant(Value, MVT::i32);
1187 // Return true if Op is either an unextended load, or a load suitable
1188 // for integer register-memory comparisons of type ICmpType.
1189 static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
1190 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op.getNode());
1192 // There are no instructions to compare a register with a memory byte.
1193 if (Load->getMemoryVT() == MVT::i8)
1195 // Otherwise decide on extension type.
1196 switch (Load->getExtensionType()) {
1197 case ISD::NON_EXTLOAD:
1200 return ICmpType != SystemZICMP::UnsignedOnly;
1202 return ICmpType != SystemZICMP::SignedOnly;
1210 // Return true if it is better to swap the operands of C.
1211 static bool shouldSwapCmpOperands(const Comparison &C) {
1212 // Leave f128 comparisons alone, since they have no memory forms.
1213 if (C.Op0.getValueType() == MVT::f128)
1216 // Always keep a floating-point constant second, since comparisons with
1217 // zero can use LOAD TEST and comparisons with other constants make a
1218 // natural memory operand.
1219 if (isa<ConstantFPSDNode>(C.Op1))
1222 // Never swap comparisons with zero since there are many ways to optimize
1224 ConstantSDNode *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
1225 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
1228 // Also keep natural memory operands second if the loaded value is
1229 // only used here. Several comparisons have memory forms.
1230 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
1233 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1234 // In that case we generally prefer the memory to be second.
1235 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
1236 // The only exceptions are when the second operand is a constant and
1237 // we can use things like CHHSI.
1240 // The unsigned memory-immediate instructions can handle 16-bit
1241 // unsigned integers.
1242 if (C.ICmpType != SystemZICMP::SignedOnly &&
1243 isUInt<16>(ConstOp1->getZExtValue()))
1245 // The signed memory-immediate instructions can handle 16-bit
1247 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1248 isInt<16>(ConstOp1->getSExtValue()))
1253 // Try to promote the use of CGFR and CLGFR.
1254 unsigned Opcode0 = C.Op0.getOpcode();
1255 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
1257 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
1259 if (C.ICmpType != SystemZICMP::SignedOnly &&
1260 Opcode0 == ISD::AND &&
1261 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1262 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
1268 // Return a version of comparison CC mask CCMask in which the LT and GT
1269 // actions are swapped.
1270 static unsigned reverseCCMask(unsigned CCMask) {
1271 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1272 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1273 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1274 (CCMask & SystemZ::CCMASK_CMP_UO));
1277 // Check whether C tests for equality between X and Y and whether X - Y
1278 // or Y - X is also computed. In that case it's better to compare the
1279 // result of the subtraction against zero.
1280 static void adjustForSubtraction(SelectionDAG &DAG, Comparison &C) {
1281 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1282 C.CCMask == SystemZ::CCMASK_CMP_NE) {
1283 for (SDNode::use_iterator I = C.Op0->use_begin(), E = C.Op0->use_end();
1286 if (N->getOpcode() == ISD::SUB &&
1287 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1288 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1289 C.Op0 = SDValue(N, 0);
1290 C.Op1 = DAG.getConstant(0, N->getValueType(0));
1297 // Check whether C compares a floating-point value with zero and if that
1298 // floating-point value is also negated. In this case we can use the
1299 // negation to set CC, so avoiding separate LOAD AND TEST and
1300 // LOAD (NEGATIVE/COMPLEMENT) instructions.
1301 static void adjustForFNeg(Comparison &C) {
1302 ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
1303 if (C1 && C1->isZero()) {
1304 for (SDNode::use_iterator I = C.Op0->use_begin(), E = C.Op0->use_end();
1307 if (N->getOpcode() == ISD::FNEG) {
1308 C.Op0 = SDValue(N, 0);
1309 C.CCMask = reverseCCMask(C.CCMask);
1316 // Check whether C compares (shl X, 32) with 0 and whether X is
1317 // also sign-extended. In that case it is better to test the result
1318 // of the sign extension using LTGFR.
1320 // This case is important because InstCombine transforms a comparison
1321 // with (sext (trunc X)) into a comparison with (shl X, 32).
1322 static void adjustForLTGFR(Comparison &C) {
1323 // Check for a comparison between (shl X, 32) and 0.
1324 if (C.Op0.getOpcode() == ISD::SHL &&
1325 C.Op0.getValueType() == MVT::i64 &&
1326 C.Op1.getOpcode() == ISD::Constant &&
1327 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
1328 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
1329 if (C1 && C1->getZExtValue() == 32) {
1330 SDValue ShlOp0 = C.Op0.getOperand(0);
1331 // See whether X has any SIGN_EXTEND_INREG uses.
1332 for (SDNode::use_iterator I = ShlOp0->use_begin(), E = ShlOp0->use_end();
1335 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
1336 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
1337 C.Op0 = SDValue(N, 0);
1345 // If C compares the truncation of an extending load, try to compare
1346 // the untruncated value instead. This exposes more opportunities to
1348 static void adjustICmpTruncate(SelectionDAG &DAG, Comparison &C) {
1349 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
1350 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
1351 C.Op1.getOpcode() == ISD::Constant &&
1352 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
1353 LoadSDNode *L = cast<LoadSDNode>(C.Op0.getOperand(0));
1354 if (L->getMemoryVT().getStoreSizeInBits()
1355 <= C.Op0.getValueType().getSizeInBits()) {
1356 unsigned Type = L->getExtensionType();
1357 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
1358 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
1359 C.Op0 = C.Op0.getOperand(0);
1360 C.Op1 = DAG.getConstant(0, C.Op0.getValueType());
1366 // Return true if shift operation N has an in-range constant shift value.
1367 // Store it in ShiftVal if so.
1368 static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
1369 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
1373 uint64_t Amount = Shift->getZExtValue();
1374 if (Amount >= N.getValueType().getSizeInBits())
1381 // Check whether an AND with Mask is suitable for a TEST UNDER MASK
1382 // instruction and whether the CC value is descriptive enough to handle
1383 // a comparison of type Opcode between the AND result and CmpVal.
1384 // CCMask says which comparison result is being tested and BitSize is
1385 // the number of bits in the operands. If TEST UNDER MASK can be used,
1386 // return the corresponding CC mask, otherwise return 0.
1387 static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
1388 uint64_t Mask, uint64_t CmpVal,
1389 unsigned ICmpType) {
1390 assert(Mask != 0 && "ANDs with zero should have been removed by now");
1392 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
1393 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
1394 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
1397 // Work out the masks for the lowest and highest bits.
1398 unsigned HighShift = 63 - countLeadingZeros(Mask);
1399 uint64_t High = uint64_t(1) << HighShift;
1400 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
1402 // Signed ordered comparisons are effectively unsigned if the sign
1404 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
1406 // Check for equality comparisons with 0, or the equivalent.
1408 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1409 return SystemZ::CCMASK_TM_ALL_0;
1410 if (CCMask == SystemZ::CCMASK_CMP_NE)
1411 return SystemZ::CCMASK_TM_SOME_1;
1413 if (EffectivelyUnsigned && CmpVal <= Low) {
1414 if (CCMask == SystemZ::CCMASK_CMP_LT)
1415 return SystemZ::CCMASK_TM_ALL_0;
1416 if (CCMask == SystemZ::CCMASK_CMP_GE)
1417 return SystemZ::CCMASK_TM_SOME_1;
1419 if (EffectivelyUnsigned && CmpVal < Low) {
1420 if (CCMask == SystemZ::CCMASK_CMP_LE)
1421 return SystemZ::CCMASK_TM_ALL_0;
1422 if (CCMask == SystemZ::CCMASK_CMP_GT)
1423 return SystemZ::CCMASK_TM_SOME_1;
1426 // Check for equality comparisons with the mask, or the equivalent.
1427 if (CmpVal == Mask) {
1428 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1429 return SystemZ::CCMASK_TM_ALL_1;
1430 if (CCMask == SystemZ::CCMASK_CMP_NE)
1431 return SystemZ::CCMASK_TM_SOME_0;
1433 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
1434 if (CCMask == SystemZ::CCMASK_CMP_GT)
1435 return SystemZ::CCMASK_TM_ALL_1;
1436 if (CCMask == SystemZ::CCMASK_CMP_LE)
1437 return SystemZ::CCMASK_TM_SOME_0;
1439 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
1440 if (CCMask == SystemZ::CCMASK_CMP_GE)
1441 return SystemZ::CCMASK_TM_ALL_1;
1442 if (CCMask == SystemZ::CCMASK_CMP_LT)
1443 return SystemZ::CCMASK_TM_SOME_0;
1446 // Check for ordered comparisons with the top bit.
1447 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
1448 if (CCMask == SystemZ::CCMASK_CMP_LE)
1449 return SystemZ::CCMASK_TM_MSB_0;
1450 if (CCMask == SystemZ::CCMASK_CMP_GT)
1451 return SystemZ::CCMASK_TM_MSB_1;
1453 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
1454 if (CCMask == SystemZ::CCMASK_CMP_LT)
1455 return SystemZ::CCMASK_TM_MSB_0;
1456 if (CCMask == SystemZ::CCMASK_CMP_GE)
1457 return SystemZ::CCMASK_TM_MSB_1;
1460 // If there are just two bits, we can do equality checks for Low and High
1462 if (Mask == Low + High) {
1463 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
1464 return SystemZ::CCMASK_TM_MIXED_MSB_0;
1465 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
1466 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
1467 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
1468 return SystemZ::CCMASK_TM_MIXED_MSB_1;
1469 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
1470 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
1473 // Looks like we've exhausted our options.
1477 // See whether C can be implemented as a TEST UNDER MASK instruction.
1478 // Update the arguments with the TM version if so.
1479 static void adjustForTestUnderMask(SelectionDAG &DAG, Comparison &C) {
1480 // Check that we have a comparison with a constant.
1481 ConstantSDNode *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
1484 uint64_t CmpVal = ConstOp1->getZExtValue();
1486 // Check whether the nonconstant input is an AND with a constant mask.
1489 ConstantSDNode *Mask = 0;
1490 if (C.Op0.getOpcode() == ISD::AND) {
1491 NewC.Op0 = C.Op0.getOperand(0);
1492 NewC.Op1 = C.Op0.getOperand(1);
1493 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
1496 MaskVal = Mask->getZExtValue();
1498 // There is no instruction to compare with a 64-bit immediate
1499 // so use TMHH instead if possible. We need an unsigned ordered
1500 // comparison with an i64 immediate.
1501 if (NewC.Op0.getValueType() != MVT::i64 ||
1502 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
1503 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
1504 NewC.ICmpType == SystemZICMP::SignedOnly)
1506 // Convert LE and GT comparisons into LT and GE.
1507 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
1508 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
1509 if (CmpVal == uint64_t(-1))
1512 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1514 // If the low N bits of Op1 are zero than the low N bits of Op0 can
1515 // be masked off without changing the result.
1516 MaskVal = -(CmpVal & -CmpVal);
1517 NewC.ICmpType = SystemZICMP::UnsignedOnly;
1520 // Check whether the combination of mask, comparison value and comparison
1521 // type are suitable.
1522 unsigned BitSize = NewC.Op0.getValueType().getSizeInBits();
1523 unsigned NewCCMask, ShiftVal;
1524 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
1525 NewC.Op0.getOpcode() == ISD::SHL &&
1526 isSimpleShift(NewC.Op0, ShiftVal) &&
1527 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
1528 MaskVal >> ShiftVal,
1530 SystemZICMP::Any))) {
1531 NewC.Op0 = NewC.Op0.getOperand(0);
1532 MaskVal >>= ShiftVal;
1533 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
1534 NewC.Op0.getOpcode() == ISD::SRL &&
1535 isSimpleShift(NewC.Op0, ShiftVal) &&
1536 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
1537 MaskVal << ShiftVal,
1539 SystemZICMP::UnsignedOnly))) {
1540 NewC.Op0 = NewC.Op0.getOperand(0);
1541 MaskVal <<= ShiftVal;
1543 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
1549 // Go ahead and make the change.
1550 C.Opcode = SystemZISD::TM;
1552 if (Mask && Mask->getZExtValue() == MaskVal)
1553 C.Op1 = SDValue(Mask, 0);
1555 C.Op1 = DAG.getConstant(MaskVal, C.Op0.getValueType());
1556 C.CCValid = SystemZ::CCMASK_TM;
1557 C.CCMask = NewCCMask;
1560 // Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
1561 static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
1562 ISD::CondCode Cond) {
1563 Comparison C(CmpOp0, CmpOp1);
1564 C.CCMask = CCMaskForCondCode(Cond);
1565 if (C.Op0.getValueType().isFloatingPoint()) {
1566 C.CCValid = SystemZ::CCMASK_FCMP;
1567 C.Opcode = SystemZISD::FCMP;
1570 C.CCValid = SystemZ::CCMASK_ICMP;
1571 C.Opcode = SystemZISD::ICMP;
1572 // Choose the type of comparison. Equality and inequality tests can
1573 // use either signed or unsigned comparisons. The choice also doesn't
1574 // matter if both sign bits are known to be clear. In those cases we
1575 // want to give the main isel code the freedom to choose whichever
1577 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1578 C.CCMask == SystemZ::CCMASK_CMP_NE ||
1579 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
1580 C.ICmpType = SystemZICMP::Any;
1581 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
1582 C.ICmpType = SystemZICMP::UnsignedOnly;
1584 C.ICmpType = SystemZICMP::SignedOnly;
1585 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
1586 adjustZeroCmp(DAG, C);
1587 adjustSubwordCmp(DAG, C);
1588 adjustForSubtraction(DAG, C);
1590 adjustICmpTruncate(DAG, C);
1593 if (shouldSwapCmpOperands(C)) {
1594 std::swap(C.Op0, C.Op1);
1595 C.CCMask = reverseCCMask(C.CCMask);
1598 adjustForTestUnderMask(DAG, C);
1602 // Emit the comparison instruction described by C.
1603 static SDValue emitCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
1604 if (C.Opcode == SystemZISD::ICMP)
1605 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
1606 DAG.getConstant(C.ICmpType, MVT::i32));
1607 if (C.Opcode == SystemZISD::TM) {
1608 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
1609 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
1610 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
1611 DAG.getConstant(RegisterOnly, MVT::i32));
1613 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
1616 // Implement a 32-bit *MUL_LOHI operation by extending both operands to
1617 // 64 bits. Extend is the extension type to use. Store the high part
1618 // in Hi and the low part in Lo.
1619 static void lowerMUL_LOHI32(SelectionDAG &DAG, SDLoc DL,
1620 unsigned Extend, SDValue Op0, SDValue Op1,
1621 SDValue &Hi, SDValue &Lo) {
1622 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
1623 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
1624 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
1625 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul, DAG.getConstant(32, MVT::i64));
1626 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
1627 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
1630 // Lower a binary operation that produces two VT results, one in each
1631 // half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
1632 // Extend extends Op0 to a GR128, and Opcode performs the GR128 operation
1633 // on the extended Op0 and (unextended) Op1. Store the even register result
1634 // in Even and the odd register result in Odd.
1635 static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT,
1636 unsigned Extend, unsigned Opcode,
1637 SDValue Op0, SDValue Op1,
1638 SDValue &Even, SDValue &Odd) {
1639 SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0);
1640 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped,
1641 SDValue(In128, 0), Op1);
1642 bool Is32Bit = is32Bit(VT);
1643 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
1644 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
1647 // Return an i32 value that is 1 if the CC value produced by Glue is
1648 // in the mask CCMask and 0 otherwise. CC is known to have a value
1649 // in CCValid, so other values can be ignored.
1650 static SDValue emitSETCC(SelectionDAG &DAG, SDLoc DL, SDValue Glue,
1651 unsigned CCValid, unsigned CCMask) {
1652 IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
1653 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
1655 if (Conversion.XORValue)
1656 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
1657 DAG.getConstant(Conversion.XORValue, MVT::i32));
1659 if (Conversion.AddValue)
1660 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
1661 DAG.getConstant(Conversion.AddValue, MVT::i32));
1663 // The SHR/AND sequence should get optimized to an RISBG.
1664 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
1665 DAG.getConstant(Conversion.Bit, MVT::i32));
1666 if (Conversion.Bit != 31)
1667 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
1668 DAG.getConstant(1, MVT::i32));
1672 SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
1673 SelectionDAG &DAG) const {
1674 SDValue CmpOp0 = Op.getOperand(0);
1675 SDValue CmpOp1 = Op.getOperand(1);
1676 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
1679 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC));
1680 SDValue Glue = emitCmp(DAG, DL, C);
1681 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
1684 SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
1685 SDValue Chain = Op.getOperand(0);
1686 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1687 SDValue CmpOp0 = Op.getOperand(2);
1688 SDValue CmpOp1 = Op.getOperand(3);
1689 SDValue Dest = Op.getOperand(4);
1692 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC));
1693 SDValue Glue = emitCmp(DAG, DL, C);
1694 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
1695 Chain, DAG.getConstant(C.CCValid, MVT::i32),
1696 DAG.getConstant(C.CCMask, MVT::i32), Dest, Glue);
1699 // Return true if Pos is CmpOp and Neg is the negative of CmpOp,
1700 // allowing Pos and Neg to be wider than CmpOp.
1701 static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
1702 return (Neg.getOpcode() == ISD::SUB &&
1703 Neg.getOperand(0).getOpcode() == ISD::Constant &&
1704 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
1705 Neg.getOperand(1) == Pos &&
1707 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
1708 Pos.getOperand(0) == CmpOp)));
1711 // Return the absolute or negative absolute of Op; IsNegative decides which.
1712 static SDValue getAbsolute(SelectionDAG &DAG, SDLoc DL, SDValue Op,
1714 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
1716 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
1717 DAG.getConstant(0, Op.getValueType()), Op);
1721 SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
1722 SelectionDAG &DAG) const {
1723 SDValue CmpOp0 = Op.getOperand(0);
1724 SDValue CmpOp1 = Op.getOperand(1);
1725 SDValue TrueOp = Op.getOperand(2);
1726 SDValue FalseOp = Op.getOperand(3);
1727 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1730 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC));
1732 // Check for absolute and negative-absolute selections, including those
1733 // where the comparison value is sign-extended (for LPGFR and LNGFR).
1734 // This check supplements the one in DAGCombiner.
1735 if (C.Opcode == SystemZISD::ICMP &&
1736 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
1737 C.CCMask != SystemZ::CCMASK_CMP_NE &&
1738 C.Op1.getOpcode() == ISD::Constant &&
1739 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
1740 if (isAbsolute(C.Op0, TrueOp, FalseOp))
1741 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
1742 if (isAbsolute(C.Op0, FalseOp, TrueOp))
1743 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
1746 SDValue Glue = emitCmp(DAG, DL, C);
1748 // Special case for handling -1/0 results. The shifts we use here
1749 // should get optimized with the IPM conversion sequence.
1750 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
1751 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
1752 if (TrueC && FalseC) {
1753 int64_t TrueVal = TrueC->getSExtValue();
1754 int64_t FalseVal = FalseC->getSExtValue();
1755 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
1756 // Invert the condition if we want -1 on false.
1758 C.CCMask ^= C.CCValid;
1759 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
1760 EVT VT = Op.getValueType();
1761 // Extend the result to VT. Upper bits are ignored.
1763 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
1764 // Sign-extend from the low bit.
1765 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, MVT::i32);
1766 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
1767 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
1771 SmallVector<SDValue, 5> Ops;
1772 Ops.push_back(TrueOp);
1773 Ops.push_back(FalseOp);
1774 Ops.push_back(DAG.getConstant(C.CCValid, MVT::i32));
1775 Ops.push_back(DAG.getConstant(C.CCMask, MVT::i32));
1776 Ops.push_back(Glue);
1778 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
1779 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, &Ops[0], Ops.size());
1782 SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
1783 SelectionDAG &DAG) const {
1785 const GlobalValue *GV = Node->getGlobal();
1786 int64_t Offset = Node->getOffset();
1787 EVT PtrVT = getPointerTy();
1788 Reloc::Model RM = TM.getRelocationModel();
1789 CodeModel::Model CM = TM.getCodeModel();
1792 if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) {
1793 // Assign anchors at 1<<12 byte boundaries.
1794 uint64_t Anchor = Offset & ~uint64_t(0xfff);
1795 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
1796 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1798 // The offset can be folded into the address if it is aligned to a halfword.
1800 if (Offset != 0 && (Offset & 1) == 0) {
1801 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
1802 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
1806 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
1807 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1808 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
1809 MachinePointerInfo::getGOT(), false, false, false, 0);
1812 // If there was a non-zero offset that we didn't fold, create an explicit
1815 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
1816 DAG.getConstant(Offset, PtrVT));
1821 SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
1822 SelectionDAG &DAG) const {
1824 const GlobalValue *GV = Node->getGlobal();
1825 EVT PtrVT = getPointerTy();
1826 TLSModel::Model model = TM.getTLSModel(GV);
1828 if (model != TLSModel::LocalExec)
1829 llvm_unreachable("only local-exec TLS mode supported");
1831 // The high part of the thread pointer is in access register 0.
1832 SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
1833 DAG.getConstant(0, MVT::i32));
1834 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
1836 // The low part of the thread pointer is in access register 1.
1837 SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
1838 DAG.getConstant(1, MVT::i32));
1839 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
1841 // Merge them into a single 64-bit address.
1842 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
1843 DAG.getConstant(32, PtrVT));
1844 SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
1846 // Get the offset of GA from the thread pointer.
1847 SystemZConstantPoolValue *CPV =
1848 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
1850 // Force the offset into the constant pool and load it from there.
1851 SDValue CPAddr = DAG.getConstantPool(CPV, PtrVT, 8);
1852 SDValue Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
1853 CPAddr, MachinePointerInfo::getConstantPool(),
1854 false, false, false, 0);
1856 // Add the base and offset together.
1857 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
1860 SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
1861 SelectionDAG &DAG) const {
1863 const BlockAddress *BA = Node->getBlockAddress();
1864 int64_t Offset = Node->getOffset();
1865 EVT PtrVT = getPointerTy();
1867 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
1868 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1872 SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
1873 SelectionDAG &DAG) const {
1875 EVT PtrVT = getPointerTy();
1876 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1878 // Use LARL to load the address of the table.
1879 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1882 SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
1883 SelectionDAG &DAG) const {
1885 EVT PtrVT = getPointerTy();
1888 if (CP->isMachineConstantPoolEntry())
1889 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1890 CP->getAlignment());
1892 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1893 CP->getAlignment(), CP->getOffset());
1895 // Use LARL to load the address of the constant pool entry.
1896 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1899 SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
1900 SelectionDAG &DAG) const {
1902 SDValue In = Op.getOperand(0);
1903 EVT InVT = In.getValueType();
1904 EVT ResVT = Op.getValueType();
1906 if (InVT == MVT::i32 && ResVT == MVT::f32) {
1908 if (Subtarget.hasHighWord()) {
1909 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
1911 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
1912 MVT::i64, SDValue(U64, 0), In);
1914 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
1915 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
1916 DAG.getConstant(32, MVT::i64));
1918 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
1919 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32,
1920 DL, MVT::f32, Out64);
1922 if (InVT == MVT::f32 && ResVT == MVT::i32) {
1923 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
1924 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
1925 MVT::f64, SDValue(U64, 0), In);
1926 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
1927 if (Subtarget.hasHighWord())
1928 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
1930 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
1931 DAG.getConstant(32, MVT::i64));
1932 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
1934 llvm_unreachable("Unexpected bitcast combination");
1937 SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
1938 SelectionDAG &DAG) const {
1939 MachineFunction &MF = DAG.getMachineFunction();
1940 SystemZMachineFunctionInfo *FuncInfo =
1941 MF.getInfo<SystemZMachineFunctionInfo>();
1942 EVT PtrVT = getPointerTy();
1944 SDValue Chain = Op.getOperand(0);
1945 SDValue Addr = Op.getOperand(1);
1946 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1949 // The initial values of each field.
1950 const unsigned NumFields = 4;
1951 SDValue Fields[NumFields] = {
1952 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), PtrVT),
1953 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), PtrVT),
1954 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
1955 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
1958 // Store each field into its respective slot.
1959 SDValue MemOps[NumFields];
1960 unsigned Offset = 0;
1961 for (unsigned I = 0; I < NumFields; ++I) {
1962 SDValue FieldAddr = Addr;
1964 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
1965 DAG.getIntPtrConstant(Offset));
1966 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
1967 MachinePointerInfo(SV, Offset),
1971 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps, NumFields);
1974 SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
1975 SelectionDAG &DAG) const {
1976 SDValue Chain = Op.getOperand(0);
1977 SDValue DstPtr = Op.getOperand(1);
1978 SDValue SrcPtr = Op.getOperand(2);
1979 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
1980 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
1983 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32),
1984 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
1985 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
1988 SDValue SystemZTargetLowering::
1989 lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
1990 SDValue Chain = Op.getOperand(0);
1991 SDValue Size = Op.getOperand(1);
1994 unsigned SPReg = getStackPointerRegisterToSaveRestore();
1996 // Get a reference to the stack pointer.
1997 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
1999 // Get the new stack pointer value.
2000 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, Size);
2002 // Copy the new stack pointer back.
2003 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
2005 // The allocated data lives above the 160 bytes allocated for the standard
2006 // frame, plus any outgoing stack arguments. We don't know how much that
2007 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
2008 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
2009 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
2011 SDValue Ops[2] = { Result, Chain };
2012 return DAG.getMergeValues(Ops, 2, DL);
2015 SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
2016 SelectionDAG &DAG) const {
2017 EVT VT = Op.getValueType();
2021 // Just do a normal 64-bit multiplication and extract the results.
2022 // We define this so that it can be used for constant division.
2023 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
2024 Op.getOperand(1), Ops[1], Ops[0]);
2026 // Do a full 128-bit multiplication based on UMUL_LOHI64:
2028 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
2030 // but using the fact that the upper halves are either all zeros
2033 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
2035 // and grouping the right terms together since they are quicker than the
2038 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
2039 SDValue C63 = DAG.getConstant(63, MVT::i64);
2040 SDValue LL = Op.getOperand(0);
2041 SDValue RL = Op.getOperand(1);
2042 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
2043 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
2044 // UMUL_LOHI64 returns the low result in the odd register and the high
2045 // result in the even register. SMUL_LOHI is defined to return the
2046 // low half first, so the results are in reverse order.
2047 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2048 LL, RL, Ops[1], Ops[0]);
2049 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
2050 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
2051 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
2052 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
2054 return DAG.getMergeValues(Ops, 2, DL);
2057 SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
2058 SelectionDAG &DAG) const {
2059 EVT VT = Op.getValueType();
2063 // Just do a normal 64-bit multiplication and extract the results.
2064 // We define this so that it can be used for constant division.
2065 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
2066 Op.getOperand(1), Ops[1], Ops[0]);
2068 // UMUL_LOHI64 returns the low result in the odd register and the high
2069 // result in the even register. UMUL_LOHI is defined to return the
2070 // low half first, so the results are in reverse order.
2071 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2072 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2073 return DAG.getMergeValues(Ops, 2, DL);
2076 SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
2077 SelectionDAG &DAG) const {
2078 SDValue Op0 = Op.getOperand(0);
2079 SDValue Op1 = Op.getOperand(1);
2080 EVT VT = Op.getValueType();
2084 // We use DSGF for 32-bit division.
2086 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
2087 Opcode = SystemZISD::SDIVREM32;
2088 } else if (DAG.ComputeNumSignBits(Op1) > 32) {
2089 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
2090 Opcode = SystemZISD::SDIVREM32;
2092 Opcode = SystemZISD::SDIVREM64;
2094 // DSG(F) takes a 64-bit dividend, so the even register in the GR128
2095 // input is "don't care". The instruction returns the remainder in
2096 // the even register and the quotient in the odd register.
2098 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
2099 Op0, Op1, Ops[1], Ops[0]);
2100 return DAG.getMergeValues(Ops, 2, DL);
2103 SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
2104 SelectionDAG &DAG) const {
2105 EVT VT = Op.getValueType();
2108 // DL(G) uses a double-width dividend, so we need to clear the even
2109 // register in the GR128 input. The instruction returns the remainder
2110 // in the even register and the quotient in the odd register.
2113 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
2114 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2116 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
2117 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2118 return DAG.getMergeValues(Ops, 2, DL);
2121 SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
2122 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
2124 // Get the known-zero masks for each operand.
2125 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
2126 APInt KnownZero[2], KnownOne[2];
2127 DAG.ComputeMaskedBits(Ops[0], KnownZero[0], KnownOne[0]);
2128 DAG.ComputeMaskedBits(Ops[1], KnownZero[1], KnownOne[1]);
2130 // See if the upper 32 bits of one operand and the lower 32 bits of the
2131 // other are known zero. They are the low and high operands respectively.
2132 uint64_t Masks[] = { KnownZero[0].getZExtValue(),
2133 KnownZero[1].getZExtValue() };
2135 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
2137 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
2142 SDValue LowOp = Ops[Low];
2143 SDValue HighOp = Ops[High];
2145 // If the high part is a constant, we're better off using IILH.
2146 if (HighOp.getOpcode() == ISD::Constant)
2149 // If the low part is a constant that is outside the range of LHI,
2150 // then we're better off using IILF.
2151 if (LowOp.getOpcode() == ISD::Constant) {
2152 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
2153 if (!isInt<16>(Value))
2157 // Check whether the high part is an AND that doesn't change the
2158 // high 32 bits and just masks out low bits. We can skip it if so.
2159 if (HighOp.getOpcode() == ISD::AND &&
2160 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
2161 SDValue HighOp0 = HighOp.getOperand(0);
2162 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
2163 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
2167 // Take advantage of the fact that all GR32 operations only change the
2168 // low 32 bits by truncating Low to an i32 and inserting it directly
2169 // using a subreg. The interesting cases are those where the truncation
2172 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
2173 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
2174 MVT::i64, HighOp, Low32);
2177 // Op is an atomic load. Lower it into a normal volatile load.
2178 SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
2179 SelectionDAG &DAG) const {
2180 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode());
2181 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
2182 Node->getChain(), Node->getBasePtr(),
2183 Node->getMemoryVT(), Node->getMemOperand());
2186 // Op is an atomic store. Lower it into a normal volatile store followed
2187 // by a serialization.
2188 SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
2189 SelectionDAG &DAG) const {
2190 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode());
2191 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
2192 Node->getBasePtr(), Node->getMemoryVT(),
2193 Node->getMemOperand());
2194 return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other,
2198 // Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
2199 // two into the fullword ATOMIC_LOADW_* operation given by Opcode.
2200 SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
2202 unsigned Opcode) const {
2203 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode());
2205 // 32-bit operations need no code outside the main loop.
2206 EVT NarrowVT = Node->getMemoryVT();
2207 EVT WideVT = MVT::i32;
2208 if (NarrowVT == WideVT)
2211 int64_t BitSize = NarrowVT.getSizeInBits();
2212 SDValue ChainIn = Node->getChain();
2213 SDValue Addr = Node->getBasePtr();
2214 SDValue Src2 = Node->getVal();
2215 MachineMemOperand *MMO = Node->getMemOperand();
2217 EVT PtrVT = Addr.getValueType();
2219 // Convert atomic subtracts of constants into additions.
2220 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
2221 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(Src2)) {
2222 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
2223 Src2 = DAG.getConstant(-Const->getSExtValue(), Src2.getValueType());
2226 // Get the address of the containing word.
2227 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
2228 DAG.getConstant(-4, PtrVT));
2230 // Get the number of bits that the word must be rotated left in order
2231 // to bring the field to the top bits of a GR32.
2232 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
2233 DAG.getConstant(3, PtrVT));
2234 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
2236 // Get the complementing shift amount, for rotating a field in the top
2237 // bits back to its proper position.
2238 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
2239 DAG.getConstant(0, WideVT), BitShift);
2241 // Extend the source operand to 32 bits and prepare it for the inner loop.
2242 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
2243 // operations require the source to be shifted in advance. (This shift
2244 // can be folded if the source is constant.) For AND and NAND, the lower
2245 // bits must be set, while for other opcodes they should be left clear.
2246 if (Opcode != SystemZISD::ATOMIC_SWAPW)
2247 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
2248 DAG.getConstant(32 - BitSize, WideVT));
2249 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
2250 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
2251 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
2252 DAG.getConstant(uint32_t(-1) >> BitSize, WideVT));
2254 // Construct the ATOMIC_LOADW_* node.
2255 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
2256 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
2257 DAG.getConstant(BitSize, WideVT) };
2258 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
2259 array_lengthof(Ops),
2262 // Rotate the result of the final CS so that the field is in the lower
2263 // bits of a GR32, then truncate it.
2264 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
2265 DAG.getConstant(BitSize, WideVT));
2266 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
2268 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
2269 return DAG.getMergeValues(RetOps, 2, DL);
2272 // Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
2273 // into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
2274 // operations into additions.
2275 SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
2276 SelectionDAG &DAG) const {
2277 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode());
2278 EVT MemVT = Node->getMemoryVT();
2279 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
2280 // A full-width operation.
2281 assert(Op.getValueType() == MemVT && "Mismatched VTs");
2282 SDValue Src2 = Node->getVal();
2286 if (ConstantSDNode *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
2287 // Use an addition if the operand is constant and either LAA(G) is
2288 // available or the negative value is in the range of A(G)FHI.
2289 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
2290 if (isInt<32>(Value) || TM.getSubtargetImpl()->hasInterlockedAccess1())
2291 NegSrc2 = DAG.getConstant(Value, MemVT);
2292 } else if (TM.getSubtargetImpl()->hasInterlockedAccess1())
2293 // Use LAA(G) if available.
2294 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, MemVT),
2297 if (NegSrc2.getNode())
2298 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
2299 Node->getChain(), Node->getBasePtr(), NegSrc2,
2300 Node->getMemOperand(), Node->getOrdering(),
2301 Node->getSynchScope());
2303 // Use the node as-is.
2307 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
2310 // Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
2311 // into a fullword ATOMIC_CMP_SWAPW operation.
2312 SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
2313 SelectionDAG &DAG) const {
2314 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode());
2316 // We have native support for 32-bit compare and swap.
2317 EVT NarrowVT = Node->getMemoryVT();
2318 EVT WideVT = MVT::i32;
2319 if (NarrowVT == WideVT)
2322 int64_t BitSize = NarrowVT.getSizeInBits();
2323 SDValue ChainIn = Node->getOperand(0);
2324 SDValue Addr = Node->getOperand(1);
2325 SDValue CmpVal = Node->getOperand(2);
2326 SDValue SwapVal = Node->getOperand(3);
2327 MachineMemOperand *MMO = Node->getMemOperand();
2329 EVT PtrVT = Addr.getValueType();
2331 // Get the address of the containing word.
2332 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
2333 DAG.getConstant(-4, PtrVT));
2335 // Get the number of bits that the word must be rotated left in order
2336 // to bring the field to the top bits of a GR32.
2337 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
2338 DAG.getConstant(3, PtrVT));
2339 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
2341 // Get the complementing shift amount, for rotating a field in the top
2342 // bits back to its proper position.
2343 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
2344 DAG.getConstant(0, WideVT), BitShift);
2346 // Construct the ATOMIC_CMP_SWAPW node.
2347 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
2348 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
2349 NegBitShift, DAG.getConstant(BitSize, WideVT) };
2350 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
2351 VTList, Ops, array_lengthof(Ops),
2356 SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
2357 SelectionDAG &DAG) const {
2358 MachineFunction &MF = DAG.getMachineFunction();
2359 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
2360 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
2361 SystemZ::R15D, Op.getValueType());
2364 SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
2365 SelectionDAG &DAG) const {
2366 MachineFunction &MF = DAG.getMachineFunction();
2367 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
2368 return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op),
2369 SystemZ::R15D, Op.getOperand(1));
2372 SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
2373 SelectionDAG &DAG) const {
2374 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2376 // Just preserve the chain.
2377 return Op.getOperand(0);
2379 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2380 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
2381 MemIntrinsicSDNode *Node = cast<MemIntrinsicSDNode>(Op.getNode());
2384 DAG.getConstant(Code, MVT::i32),
2387 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, SDLoc(Op),
2388 Node->getVTList(), Ops, array_lengthof(Ops),
2389 Node->getMemoryVT(), Node->getMemOperand());
2392 SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
2393 SelectionDAG &DAG) const {
2394 switch (Op.getOpcode()) {
2396 return lowerBR_CC(Op, DAG);
2397 case ISD::SELECT_CC:
2398 return lowerSELECT_CC(Op, DAG);
2400 return lowerSETCC(Op, DAG);
2401 case ISD::GlobalAddress:
2402 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
2403 case ISD::GlobalTLSAddress:
2404 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
2405 case ISD::BlockAddress:
2406 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
2407 case ISD::JumpTable:
2408 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
2409 case ISD::ConstantPool:
2410 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
2412 return lowerBITCAST(Op, DAG);
2414 return lowerVASTART(Op, DAG);
2416 return lowerVACOPY(Op, DAG);
2417 case ISD::DYNAMIC_STACKALLOC:
2418 return lowerDYNAMIC_STACKALLOC(Op, DAG);
2419 case ISD::SMUL_LOHI:
2420 return lowerSMUL_LOHI(Op, DAG);
2421 case ISD::UMUL_LOHI:
2422 return lowerUMUL_LOHI(Op, DAG);
2424 return lowerSDIVREM(Op, DAG);
2426 return lowerUDIVREM(Op, DAG);
2428 return lowerOR(Op, DAG);
2429 case ISD::ATOMIC_SWAP:
2430 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
2431 case ISD::ATOMIC_STORE:
2432 return lowerATOMIC_STORE(Op, DAG);
2433 case ISD::ATOMIC_LOAD:
2434 return lowerATOMIC_LOAD(Op, DAG);
2435 case ISD::ATOMIC_LOAD_ADD:
2436 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
2437 case ISD::ATOMIC_LOAD_SUB:
2438 return lowerATOMIC_LOAD_SUB(Op, DAG);
2439 case ISD::ATOMIC_LOAD_AND:
2440 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
2441 case ISD::ATOMIC_LOAD_OR:
2442 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
2443 case ISD::ATOMIC_LOAD_XOR:
2444 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
2445 case ISD::ATOMIC_LOAD_NAND:
2446 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
2447 case ISD::ATOMIC_LOAD_MIN:
2448 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
2449 case ISD::ATOMIC_LOAD_MAX:
2450 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
2451 case ISD::ATOMIC_LOAD_UMIN:
2452 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
2453 case ISD::ATOMIC_LOAD_UMAX:
2454 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
2455 case ISD::ATOMIC_CMP_SWAP:
2456 return lowerATOMIC_CMP_SWAP(Op, DAG);
2457 case ISD::STACKSAVE:
2458 return lowerSTACKSAVE(Op, DAG);
2459 case ISD::STACKRESTORE:
2460 return lowerSTACKRESTORE(Op, DAG);
2462 return lowerPREFETCH(Op, DAG);
2464 llvm_unreachable("Unexpected node to lower");
2468 const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
2469 #define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
2474 OPCODE(PCREL_WRAPPER);
2475 OPCODE(PCREL_OFFSET);
2481 OPCODE(SELECT_CCMASK);
2482 OPCODE(ADJDYNALLOC);
2483 OPCODE(EXTRACT_ACCESS);
2484 OPCODE(UMUL_LOHI64);
2500 OPCODE(SEARCH_STRING);
2503 OPCODE(ATOMIC_SWAPW);
2504 OPCODE(ATOMIC_LOADW_ADD);
2505 OPCODE(ATOMIC_LOADW_SUB);
2506 OPCODE(ATOMIC_LOADW_AND);
2507 OPCODE(ATOMIC_LOADW_OR);
2508 OPCODE(ATOMIC_LOADW_XOR);
2509 OPCODE(ATOMIC_LOADW_NAND);
2510 OPCODE(ATOMIC_LOADW_MIN);
2511 OPCODE(ATOMIC_LOADW_MAX);
2512 OPCODE(ATOMIC_LOADW_UMIN);
2513 OPCODE(ATOMIC_LOADW_UMAX);
2514 OPCODE(ATOMIC_CMP_SWAPW);
2521 //===----------------------------------------------------------------------===//
2523 //===----------------------------------------------------------------------===//
2525 // Create a new basic block after MBB.
2526 static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
2527 MachineFunction &MF = *MBB->getParent();
2528 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
2529 MF.insert(llvm::next(MachineFunction::iterator(MBB)), NewMBB);
2533 // Split MBB after MI and return the new block (the one that contains
2534 // instructions after MI).
2535 static MachineBasicBlock *splitBlockAfter(MachineInstr *MI,
2536 MachineBasicBlock *MBB) {
2537 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
2538 NewMBB->splice(NewMBB->begin(), MBB,
2539 llvm::next(MachineBasicBlock::iterator(MI)),
2541 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
2545 // Split MBB before MI and return the new block (the one that contains MI).
2546 static MachineBasicBlock *splitBlockBefore(MachineInstr *MI,
2547 MachineBasicBlock *MBB) {
2548 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
2549 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
2550 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
2554 // Force base value Base into a register before MI. Return the register.
2555 static unsigned forceReg(MachineInstr *MI, MachineOperand &Base,
2556 const SystemZInstrInfo *TII) {
2558 return Base.getReg();
2560 MachineBasicBlock *MBB = MI->getParent();
2561 MachineFunction &MF = *MBB->getParent();
2562 MachineRegisterInfo &MRI = MF.getRegInfo();
2564 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
2565 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg)
2566 .addOperand(Base).addImm(0).addReg(0);
2570 // Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
2572 SystemZTargetLowering::emitSelect(MachineInstr *MI,
2573 MachineBasicBlock *MBB) const {
2574 const SystemZInstrInfo *TII = TM.getInstrInfo();
2576 unsigned DestReg = MI->getOperand(0).getReg();
2577 unsigned TrueReg = MI->getOperand(1).getReg();
2578 unsigned FalseReg = MI->getOperand(2).getReg();
2579 unsigned CCValid = MI->getOperand(3).getImm();
2580 unsigned CCMask = MI->getOperand(4).getImm();
2581 DebugLoc DL = MI->getDebugLoc();
2583 MachineBasicBlock *StartMBB = MBB;
2584 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
2585 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
2588 // BRC CCMask, JoinMBB
2589 // # fallthrough to FalseMBB
2591 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2592 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
2593 MBB->addSuccessor(JoinMBB);
2594 MBB->addSuccessor(FalseMBB);
2597 // # fallthrough to JoinMBB
2599 MBB->addSuccessor(JoinMBB);
2602 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
2605 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
2606 .addReg(TrueReg).addMBB(StartMBB)
2607 .addReg(FalseReg).addMBB(FalseMBB);
2609 MI->eraseFromParent();
2613 // Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
2614 // StoreOpcode is the store to use and Invert says whether the store should
2615 // happen when the condition is false rather than true. If a STORE ON
2616 // CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
2618 SystemZTargetLowering::emitCondStore(MachineInstr *MI,
2619 MachineBasicBlock *MBB,
2620 unsigned StoreOpcode, unsigned STOCOpcode,
2621 bool Invert) const {
2622 const SystemZInstrInfo *TII = TM.getInstrInfo();
2624 unsigned SrcReg = MI->getOperand(0).getReg();
2625 MachineOperand Base = MI->getOperand(1);
2626 int64_t Disp = MI->getOperand(2).getImm();
2627 unsigned IndexReg = MI->getOperand(3).getReg();
2628 unsigned CCValid = MI->getOperand(4).getImm();
2629 unsigned CCMask = MI->getOperand(5).getImm();
2630 DebugLoc DL = MI->getDebugLoc();
2632 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
2634 // Use STOCOpcode if possible. We could use different store patterns in
2635 // order to avoid matching the index register, but the performance trade-offs
2636 // might be more complicated in that case.
2637 if (STOCOpcode && !IndexReg && TM.getSubtargetImpl()->hasLoadStoreOnCond()) {
2640 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
2641 .addReg(SrcReg).addOperand(Base).addImm(Disp)
2642 .addImm(CCValid).addImm(CCMask);
2643 MI->eraseFromParent();
2647 // Get the condition needed to branch around the store.
2651 MachineBasicBlock *StartMBB = MBB;
2652 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
2653 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
2656 // BRC CCMask, JoinMBB
2657 // # fallthrough to FalseMBB
2659 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2660 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
2661 MBB->addSuccessor(JoinMBB);
2662 MBB->addSuccessor(FalseMBB);
2665 // store %SrcReg, %Disp(%Index,%Base)
2666 // # fallthrough to JoinMBB
2668 BuildMI(MBB, DL, TII->get(StoreOpcode))
2669 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
2670 MBB->addSuccessor(JoinMBB);
2672 MI->eraseFromParent();
2676 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
2677 // or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
2678 // performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
2679 // BitSize is the width of the field in bits, or 0 if this is a partword
2680 // ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
2681 // is one of the operands. Invert says whether the field should be
2682 // inverted after performing BinOpcode (e.g. for NAND).
2684 SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI,
2685 MachineBasicBlock *MBB,
2688 bool Invert) const {
2689 const SystemZInstrInfo *TII = TM.getInstrInfo();
2690 MachineFunction &MF = *MBB->getParent();
2691 MachineRegisterInfo &MRI = MF.getRegInfo();
2692 bool IsSubWord = (BitSize < 32);
2694 // Extract the operands. Base can be a register or a frame index.
2695 // Src2 can be a register or immediate.
2696 unsigned Dest = MI->getOperand(0).getReg();
2697 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2698 int64_t Disp = MI->getOperand(2).getImm();
2699 MachineOperand Src2 = earlyUseOperand(MI->getOperand(3));
2700 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
2701 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
2702 DebugLoc DL = MI->getDebugLoc();
2704 BitSize = MI->getOperand(6).getImm();
2706 // Subword operations use 32-bit registers.
2707 const TargetRegisterClass *RC = (BitSize <= 32 ?
2708 &SystemZ::GR32BitRegClass :
2709 &SystemZ::GR64BitRegClass);
2710 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
2711 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
2713 // Get the right opcodes for the displacement.
2714 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
2715 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
2716 assert(LOpcode && CSOpcode && "Displacement out of range");
2718 // Create virtual registers for temporary results.
2719 unsigned OrigVal = MRI.createVirtualRegister(RC);
2720 unsigned OldVal = MRI.createVirtualRegister(RC);
2721 unsigned NewVal = (BinOpcode || IsSubWord ?
2722 MRI.createVirtualRegister(RC) : Src2.getReg());
2723 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
2724 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
2726 // Insert a basic block for the main loop.
2727 MachineBasicBlock *StartMBB = MBB;
2728 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
2729 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2733 // %OrigVal = L Disp(%Base)
2734 // # fall through to LoopMMB
2736 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
2737 .addOperand(Base).addImm(Disp).addReg(0);
2738 MBB->addSuccessor(LoopMBB);
2741 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
2742 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
2743 // %RotatedNewVal = OP %RotatedOldVal, %Src2
2744 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
2745 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
2747 // # fall through to DoneMMB
2749 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2750 .addReg(OrigVal).addMBB(StartMBB)
2751 .addReg(Dest).addMBB(LoopMBB);
2753 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
2754 .addReg(OldVal).addReg(BitShift).addImm(0);
2756 // Perform the operation normally and then invert every bit of the field.
2757 unsigned Tmp = MRI.createVirtualRegister(RC);
2758 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
2759 .addReg(RotatedOldVal).addOperand(Src2);
2761 // XILF with the upper BitSize bits set.
2762 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
2763 .addReg(Tmp).addImm(uint32_t(~0 << (32 - BitSize)));
2764 else if (BitSize == 32)
2765 // XILF with every bit set.
2766 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
2767 .addReg(Tmp).addImm(~uint32_t(0));
2769 // Use LCGR and add -1 to the result, which is more compact than
2770 // an XILF, XILH pair.
2771 unsigned Tmp2 = MRI.createVirtualRegister(RC);
2772 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
2773 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
2774 .addReg(Tmp2).addImm(-1);
2776 } else if (BinOpcode)
2777 // A simply binary operation.
2778 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
2779 .addReg(RotatedOldVal).addOperand(Src2);
2781 // Use RISBG to rotate Src2 into position and use it to replace the
2782 // field in RotatedOldVal.
2783 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
2784 .addReg(RotatedOldVal).addReg(Src2.getReg())
2785 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
2787 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
2788 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
2789 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
2790 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
2791 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2792 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
2793 MBB->addSuccessor(LoopMBB);
2794 MBB->addSuccessor(DoneMBB);
2796 MI->eraseFromParent();
2800 // Implement EmitInstrWithCustomInserter for pseudo
2801 // ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
2802 // instruction that should be used to compare the current field with the
2803 // minimum or maximum value. KeepOldMask is the BRC condition-code mask
2804 // for when the current field should be kept. BitSize is the width of
2805 // the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
2807 SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI,
2808 MachineBasicBlock *MBB,
2809 unsigned CompareOpcode,
2810 unsigned KeepOldMask,
2811 unsigned BitSize) const {
2812 const SystemZInstrInfo *TII = TM.getInstrInfo();
2813 MachineFunction &MF = *MBB->getParent();
2814 MachineRegisterInfo &MRI = MF.getRegInfo();
2815 bool IsSubWord = (BitSize < 32);
2817 // Extract the operands. Base can be a register or a frame index.
2818 unsigned Dest = MI->getOperand(0).getReg();
2819 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2820 int64_t Disp = MI->getOperand(2).getImm();
2821 unsigned Src2 = MI->getOperand(3).getReg();
2822 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
2823 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
2824 DebugLoc DL = MI->getDebugLoc();
2826 BitSize = MI->getOperand(6).getImm();
2828 // Subword operations use 32-bit registers.
2829 const TargetRegisterClass *RC = (BitSize <= 32 ?
2830 &SystemZ::GR32BitRegClass :
2831 &SystemZ::GR64BitRegClass);
2832 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
2833 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
2835 // Get the right opcodes for the displacement.
2836 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
2837 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
2838 assert(LOpcode && CSOpcode && "Displacement out of range");
2840 // Create virtual registers for temporary results.
2841 unsigned OrigVal = MRI.createVirtualRegister(RC);
2842 unsigned OldVal = MRI.createVirtualRegister(RC);
2843 unsigned NewVal = MRI.createVirtualRegister(RC);
2844 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
2845 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
2846 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
2848 // Insert 3 basic blocks for the loop.
2849 MachineBasicBlock *StartMBB = MBB;
2850 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
2851 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2852 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
2853 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
2857 // %OrigVal = L Disp(%Base)
2858 // # fall through to LoopMMB
2860 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
2861 .addOperand(Base).addImm(Disp).addReg(0);
2862 MBB->addSuccessor(LoopMBB);
2865 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
2866 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
2867 // CompareOpcode %RotatedOldVal, %Src2
2868 // BRC KeepOldMask, UpdateMBB
2870 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2871 .addReg(OrigVal).addMBB(StartMBB)
2872 .addReg(Dest).addMBB(UpdateMBB);
2874 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
2875 .addReg(OldVal).addReg(BitShift).addImm(0);
2876 BuildMI(MBB, DL, TII->get(CompareOpcode))
2877 .addReg(RotatedOldVal).addReg(Src2);
2878 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2879 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
2880 MBB->addSuccessor(UpdateMBB);
2881 MBB->addSuccessor(UseAltMBB);
2884 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
2885 // # fall through to UpdateMMB
2888 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
2889 .addReg(RotatedOldVal).addReg(Src2)
2890 .addImm(32).addImm(31 + BitSize).addImm(0);
2891 MBB->addSuccessor(UpdateMBB);
2894 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
2895 // [ %RotatedAltVal, UseAltMBB ]
2896 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
2897 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
2899 // # fall through to DoneMMB
2901 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
2902 .addReg(RotatedOldVal).addMBB(LoopMBB)
2903 .addReg(RotatedAltVal).addMBB(UseAltMBB);
2905 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
2906 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
2907 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
2908 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
2909 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2910 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
2911 MBB->addSuccessor(LoopMBB);
2912 MBB->addSuccessor(DoneMBB);
2914 MI->eraseFromParent();
2918 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
2921 SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI,
2922 MachineBasicBlock *MBB) const {
2923 const SystemZInstrInfo *TII = TM.getInstrInfo();
2924 MachineFunction &MF = *MBB->getParent();
2925 MachineRegisterInfo &MRI = MF.getRegInfo();
2927 // Extract the operands. Base can be a register or a frame index.
2928 unsigned Dest = MI->getOperand(0).getReg();
2929 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2930 int64_t Disp = MI->getOperand(2).getImm();
2931 unsigned OrigCmpVal = MI->getOperand(3).getReg();
2932 unsigned OrigSwapVal = MI->getOperand(4).getReg();
2933 unsigned BitShift = MI->getOperand(5).getReg();
2934 unsigned NegBitShift = MI->getOperand(6).getReg();
2935 int64_t BitSize = MI->getOperand(7).getImm();
2936 DebugLoc DL = MI->getDebugLoc();
2938 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
2940 // Get the right opcodes for the displacement.
2941 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
2942 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
2943 assert(LOpcode && CSOpcode && "Displacement out of range");
2945 // Create virtual registers for temporary results.
2946 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
2947 unsigned OldVal = MRI.createVirtualRegister(RC);
2948 unsigned CmpVal = MRI.createVirtualRegister(RC);
2949 unsigned SwapVal = MRI.createVirtualRegister(RC);
2950 unsigned StoreVal = MRI.createVirtualRegister(RC);
2951 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
2952 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
2953 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
2955 // Insert 2 basic blocks for the loop.
2956 MachineBasicBlock *StartMBB = MBB;
2957 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
2958 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2959 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
2963 // %OrigOldVal = L Disp(%Base)
2964 // # fall through to LoopMMB
2966 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
2967 .addOperand(Base).addImm(Disp).addReg(0);
2968 MBB->addSuccessor(LoopMBB);
2971 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
2972 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
2973 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
2974 // %Dest = RLL %OldVal, BitSize(%BitShift)
2975 // ^^ The low BitSize bits contain the field
2977 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
2978 // ^^ Replace the upper 32-BitSize bits of the
2979 // comparison value with those that we loaded,
2980 // so that we can use a full word comparison.
2981 // CR %Dest, %RetryCmpVal
2983 // # Fall through to SetMBB
2985 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2986 .addReg(OrigOldVal).addMBB(StartMBB)
2987 .addReg(RetryOldVal).addMBB(SetMBB);
2988 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
2989 .addReg(OrigCmpVal).addMBB(StartMBB)
2990 .addReg(RetryCmpVal).addMBB(SetMBB);
2991 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
2992 .addReg(OrigSwapVal).addMBB(StartMBB)
2993 .addReg(RetrySwapVal).addMBB(SetMBB);
2994 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
2995 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
2996 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
2997 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
2998 BuildMI(MBB, DL, TII->get(SystemZ::CR))
2999 .addReg(Dest).addReg(RetryCmpVal);
3000 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3001 .addImm(SystemZ::CCMASK_ICMP)
3002 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
3003 MBB->addSuccessor(DoneMBB);
3004 MBB->addSuccessor(SetMBB);
3007 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
3008 // ^^ Replace the upper 32-BitSize bits of the new
3009 // value with those that we loaded.
3010 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
3011 // ^^ Rotate the new field to its proper position.
3012 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
3014 // # fall through to ExitMMB
3016 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
3017 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
3018 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
3019 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
3020 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
3021 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
3022 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3023 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
3024 MBB->addSuccessor(LoopMBB);
3025 MBB->addSuccessor(DoneMBB);
3027 MI->eraseFromParent();
3031 // Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true
3032 // if the high register of the GR128 value must be cleared or false if
3033 // it's "don't care". SubReg is subreg_l32 when extending a GR32
3034 // and subreg_l64 when extending a GR64.
3036 SystemZTargetLowering::emitExt128(MachineInstr *MI,
3037 MachineBasicBlock *MBB,
3038 bool ClearEven, unsigned SubReg) const {
3039 const SystemZInstrInfo *TII = TM.getInstrInfo();
3040 MachineFunction &MF = *MBB->getParent();
3041 MachineRegisterInfo &MRI = MF.getRegInfo();
3042 DebugLoc DL = MI->getDebugLoc();
3044 unsigned Dest = MI->getOperand(0).getReg();
3045 unsigned Src = MI->getOperand(1).getReg();
3046 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
3048 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
3050 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
3051 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
3053 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
3055 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
3056 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
3059 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
3060 .addReg(In128).addReg(Src).addImm(SubReg);
3062 MI->eraseFromParent();
3067 SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI,
3068 MachineBasicBlock *MBB,
3069 unsigned Opcode) const {
3070 const SystemZInstrInfo *TII = TM.getInstrInfo();
3071 MachineFunction &MF = *MBB->getParent();
3072 MachineRegisterInfo &MRI = MF.getRegInfo();
3073 DebugLoc DL = MI->getDebugLoc();
3075 MachineOperand DestBase = earlyUseOperand(MI->getOperand(0));
3076 uint64_t DestDisp = MI->getOperand(1).getImm();
3077 MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2));
3078 uint64_t SrcDisp = MI->getOperand(3).getImm();
3079 uint64_t Length = MI->getOperand(4).getImm();
3081 // When generating more than one CLC, all but the last will need to
3082 // branch to the end when a difference is found.
3083 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
3084 splitBlockAfter(MI, MBB) : 0);
3086 // Check for the loop form, in which operand 5 is the trip count.
3087 if (MI->getNumExplicitOperands() > 5) {
3088 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
3090 uint64_t StartCountReg = MI->getOperand(5).getReg();
3091 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
3092 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
3093 forceReg(MI, DestBase, TII));
3095 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
3096 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
3097 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
3098 MRI.createVirtualRegister(RC));
3099 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
3100 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
3101 MRI.createVirtualRegister(RC));
3103 RC = &SystemZ::GR64BitRegClass;
3104 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
3105 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
3107 MachineBasicBlock *StartMBB = MBB;
3108 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
3109 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
3110 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
3113 // # fall through to LoopMMB
3114 MBB->addSuccessor(LoopMBB);
3117 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
3118 // [ %NextDestReg, NextMBB ]
3119 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
3120 // [ %NextSrcReg, NextMBB ]
3121 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
3122 // [ %NextCountReg, NextMBB ]
3123 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
3124 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
3127 // The prefetch is used only for MVC. The JLH is used only for CLC.
3130 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
3131 .addReg(StartDestReg).addMBB(StartMBB)
3132 .addReg(NextDestReg).addMBB(NextMBB);
3133 if (!HaveSingleBase)
3134 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
3135 .addReg(StartSrcReg).addMBB(StartMBB)
3136 .addReg(NextSrcReg).addMBB(NextMBB);
3137 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
3138 .addReg(StartCountReg).addMBB(StartMBB)
3139 .addReg(NextCountReg).addMBB(NextMBB);
3140 if (Opcode == SystemZ::MVC)
3141 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
3142 .addImm(SystemZ::PFD_WRITE)
3143 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
3144 BuildMI(MBB, DL, TII->get(Opcode))
3145 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
3146 .addReg(ThisSrcReg).addImm(SrcDisp);
3148 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3149 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
3151 MBB->addSuccessor(EndMBB);
3152 MBB->addSuccessor(NextMBB);
3156 // %NextDestReg = LA 256(%ThisDestReg)
3157 // %NextSrcReg = LA 256(%ThisSrcReg)
3158 // %NextCountReg = AGHI %ThisCountReg, -1
3159 // CGHI %NextCountReg, 0
3161 // # fall through to DoneMMB
3163 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
3166 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
3167 .addReg(ThisDestReg).addImm(256).addReg(0);
3168 if (!HaveSingleBase)
3169 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
3170 .addReg(ThisSrcReg).addImm(256).addReg(0);
3171 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
3172 .addReg(ThisCountReg).addImm(-1);
3173 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
3174 .addReg(NextCountReg).addImm(0);
3175 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3176 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
3178 MBB->addSuccessor(LoopMBB);
3179 MBB->addSuccessor(DoneMBB);
3181 DestBase = MachineOperand::CreateReg(NextDestReg, false);
3182 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
3186 // Handle any remaining bytes with straight-line code.
3187 while (Length > 0) {
3188 uint64_t ThisLength = std::min(Length, uint64_t(256));
3189 // The previous iteration might have created out-of-range displacements.
3190 // Apply them using LAY if so.
3191 if (!isUInt<12>(DestDisp)) {
3192 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
3193 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
3194 .addOperand(DestBase).addImm(DestDisp).addReg(0);
3195 DestBase = MachineOperand::CreateReg(Reg, false);
3198 if (!isUInt<12>(SrcDisp)) {
3199 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
3200 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
3201 .addOperand(SrcBase).addImm(SrcDisp).addReg(0);
3202 SrcBase = MachineOperand::CreateReg(Reg, false);
3205 BuildMI(*MBB, MI, DL, TII->get(Opcode))
3206 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength)
3207 .addOperand(SrcBase).addImm(SrcDisp);
3208 DestDisp += ThisLength;
3209 SrcDisp += ThisLength;
3210 Length -= ThisLength;
3211 // If there's another CLC to go, branch to the end if a difference
3213 if (EndMBB && Length > 0) {
3214 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
3215 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3216 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
3218 MBB->addSuccessor(EndMBB);
3219 MBB->addSuccessor(NextMBB);
3224 MBB->addSuccessor(EndMBB);
3226 MBB->addLiveIn(SystemZ::CC);
3229 MI->eraseFromParent();
3233 // Decompose string pseudo-instruction MI into a loop that continually performs
3234 // Opcode until CC != 3.
3236 SystemZTargetLowering::emitStringWrapper(MachineInstr *MI,
3237 MachineBasicBlock *MBB,
3238 unsigned Opcode) const {
3239 const SystemZInstrInfo *TII = TM.getInstrInfo();
3240 MachineFunction &MF = *MBB->getParent();
3241 MachineRegisterInfo &MRI = MF.getRegInfo();
3242 DebugLoc DL = MI->getDebugLoc();
3244 uint64_t End1Reg = MI->getOperand(0).getReg();
3245 uint64_t Start1Reg = MI->getOperand(1).getReg();
3246 uint64_t Start2Reg = MI->getOperand(2).getReg();
3247 uint64_t CharReg = MI->getOperand(3).getReg();
3249 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
3250 uint64_t This1Reg = MRI.createVirtualRegister(RC);
3251 uint64_t This2Reg = MRI.createVirtualRegister(RC);
3252 uint64_t End2Reg = MRI.createVirtualRegister(RC);
3254 MachineBasicBlock *StartMBB = MBB;
3255 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
3256 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
3259 // # fall through to LoopMMB
3260 MBB->addSuccessor(LoopMBB);
3263 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
3264 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
3266 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
3268 // # fall through to DoneMMB
3270 // The load of R0L can be hoisted by post-RA LICM.
3273 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
3274 .addReg(Start1Reg).addMBB(StartMBB)
3275 .addReg(End1Reg).addMBB(LoopMBB);
3276 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
3277 .addReg(Start2Reg).addMBB(StartMBB)
3278 .addReg(End2Reg).addMBB(LoopMBB);
3279 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
3280 BuildMI(MBB, DL, TII->get(Opcode))
3281 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
3282 .addReg(This1Reg).addReg(This2Reg);
3283 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3284 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
3285 MBB->addSuccessor(LoopMBB);
3286 MBB->addSuccessor(DoneMBB);
3288 DoneMBB->addLiveIn(SystemZ::CC);
3290 MI->eraseFromParent();
3294 MachineBasicBlock *SystemZTargetLowering::
3295 EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const {
3296 switch (MI->getOpcode()) {
3297 case SystemZ::Select32Mux:
3298 case SystemZ::Select32:
3299 case SystemZ::SelectF32:
3300 case SystemZ::Select64:
3301 case SystemZ::SelectF64:
3302 case SystemZ::SelectF128:
3303 return emitSelect(MI, MBB);
3305 case SystemZ::CondStore8Mux:
3306 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
3307 case SystemZ::CondStore8MuxInv:
3308 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
3309 case SystemZ::CondStore16Mux:
3310 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
3311 case SystemZ::CondStore16MuxInv:
3312 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
3313 case SystemZ::CondStore8:
3314 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
3315 case SystemZ::CondStore8Inv:
3316 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
3317 case SystemZ::CondStore16:
3318 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
3319 case SystemZ::CondStore16Inv:
3320 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
3321 case SystemZ::CondStore32:
3322 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
3323 case SystemZ::CondStore32Inv:
3324 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
3325 case SystemZ::CondStore64:
3326 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
3327 case SystemZ::CondStore64Inv:
3328 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
3329 case SystemZ::CondStoreF32:
3330 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
3331 case SystemZ::CondStoreF32Inv:
3332 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
3333 case SystemZ::CondStoreF64:
3334 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
3335 case SystemZ::CondStoreF64Inv:
3336 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
3338 case SystemZ::AEXT128_64:
3339 return emitExt128(MI, MBB, false, SystemZ::subreg_l64);
3340 case SystemZ::ZEXT128_32:
3341 return emitExt128(MI, MBB, true, SystemZ::subreg_l32);
3342 case SystemZ::ZEXT128_64:
3343 return emitExt128(MI, MBB, true, SystemZ::subreg_l64);
3345 case SystemZ::ATOMIC_SWAPW:
3346 return emitAtomicLoadBinary(MI, MBB, 0, 0);
3347 case SystemZ::ATOMIC_SWAP_32:
3348 return emitAtomicLoadBinary(MI, MBB, 0, 32);
3349 case SystemZ::ATOMIC_SWAP_64:
3350 return emitAtomicLoadBinary(MI, MBB, 0, 64);
3352 case SystemZ::ATOMIC_LOADW_AR:
3353 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
3354 case SystemZ::ATOMIC_LOADW_AFI:
3355 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
3356 case SystemZ::ATOMIC_LOAD_AR:
3357 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
3358 case SystemZ::ATOMIC_LOAD_AHI:
3359 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
3360 case SystemZ::ATOMIC_LOAD_AFI:
3361 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
3362 case SystemZ::ATOMIC_LOAD_AGR:
3363 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
3364 case SystemZ::ATOMIC_LOAD_AGHI:
3365 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
3366 case SystemZ::ATOMIC_LOAD_AGFI:
3367 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
3369 case SystemZ::ATOMIC_LOADW_SR:
3370 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
3371 case SystemZ::ATOMIC_LOAD_SR:
3372 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
3373 case SystemZ::ATOMIC_LOAD_SGR:
3374 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
3376 case SystemZ::ATOMIC_LOADW_NR:
3377 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
3378 case SystemZ::ATOMIC_LOADW_NILH:
3379 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
3380 case SystemZ::ATOMIC_LOAD_NR:
3381 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
3382 case SystemZ::ATOMIC_LOAD_NILL:
3383 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
3384 case SystemZ::ATOMIC_LOAD_NILH:
3385 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
3386 case SystemZ::ATOMIC_LOAD_NILF:
3387 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
3388 case SystemZ::ATOMIC_LOAD_NGR:
3389 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
3390 case SystemZ::ATOMIC_LOAD_NILL64:
3391 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
3392 case SystemZ::ATOMIC_LOAD_NILH64:
3393 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
3394 case SystemZ::ATOMIC_LOAD_NIHL64:
3395 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
3396 case SystemZ::ATOMIC_LOAD_NIHH64:
3397 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
3398 case SystemZ::ATOMIC_LOAD_NILF64:
3399 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
3400 case SystemZ::ATOMIC_LOAD_NIHF64:
3401 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
3403 case SystemZ::ATOMIC_LOADW_OR:
3404 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
3405 case SystemZ::ATOMIC_LOADW_OILH:
3406 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
3407 case SystemZ::ATOMIC_LOAD_OR:
3408 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
3409 case SystemZ::ATOMIC_LOAD_OILL:
3410 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
3411 case SystemZ::ATOMIC_LOAD_OILH:
3412 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
3413 case SystemZ::ATOMIC_LOAD_OILF:
3414 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
3415 case SystemZ::ATOMIC_LOAD_OGR:
3416 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
3417 case SystemZ::ATOMIC_LOAD_OILL64:
3418 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
3419 case SystemZ::ATOMIC_LOAD_OILH64:
3420 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
3421 case SystemZ::ATOMIC_LOAD_OIHL64:
3422 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
3423 case SystemZ::ATOMIC_LOAD_OIHH64:
3424 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
3425 case SystemZ::ATOMIC_LOAD_OILF64:
3426 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
3427 case SystemZ::ATOMIC_LOAD_OIHF64:
3428 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
3430 case SystemZ::ATOMIC_LOADW_XR:
3431 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
3432 case SystemZ::ATOMIC_LOADW_XILF:
3433 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
3434 case SystemZ::ATOMIC_LOAD_XR:
3435 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
3436 case SystemZ::ATOMIC_LOAD_XILF:
3437 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
3438 case SystemZ::ATOMIC_LOAD_XGR:
3439 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
3440 case SystemZ::ATOMIC_LOAD_XILF64:
3441 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
3442 case SystemZ::ATOMIC_LOAD_XIHF64:
3443 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
3445 case SystemZ::ATOMIC_LOADW_NRi:
3446 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
3447 case SystemZ::ATOMIC_LOADW_NILHi:
3448 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
3449 case SystemZ::ATOMIC_LOAD_NRi:
3450 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
3451 case SystemZ::ATOMIC_LOAD_NILLi:
3452 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
3453 case SystemZ::ATOMIC_LOAD_NILHi:
3454 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
3455 case SystemZ::ATOMIC_LOAD_NILFi:
3456 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
3457 case SystemZ::ATOMIC_LOAD_NGRi:
3458 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
3459 case SystemZ::ATOMIC_LOAD_NILL64i:
3460 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
3461 case SystemZ::ATOMIC_LOAD_NILH64i:
3462 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
3463 case SystemZ::ATOMIC_LOAD_NIHL64i:
3464 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
3465 case SystemZ::ATOMIC_LOAD_NIHH64i:
3466 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
3467 case SystemZ::ATOMIC_LOAD_NILF64i:
3468 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
3469 case SystemZ::ATOMIC_LOAD_NIHF64i:
3470 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
3472 case SystemZ::ATOMIC_LOADW_MIN:
3473 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3474 SystemZ::CCMASK_CMP_LE, 0);
3475 case SystemZ::ATOMIC_LOAD_MIN_32:
3476 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3477 SystemZ::CCMASK_CMP_LE, 32);
3478 case SystemZ::ATOMIC_LOAD_MIN_64:
3479 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
3480 SystemZ::CCMASK_CMP_LE, 64);
3482 case SystemZ::ATOMIC_LOADW_MAX:
3483 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3484 SystemZ::CCMASK_CMP_GE, 0);
3485 case SystemZ::ATOMIC_LOAD_MAX_32:
3486 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3487 SystemZ::CCMASK_CMP_GE, 32);
3488 case SystemZ::ATOMIC_LOAD_MAX_64:
3489 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
3490 SystemZ::CCMASK_CMP_GE, 64);
3492 case SystemZ::ATOMIC_LOADW_UMIN:
3493 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3494 SystemZ::CCMASK_CMP_LE, 0);
3495 case SystemZ::ATOMIC_LOAD_UMIN_32:
3496 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3497 SystemZ::CCMASK_CMP_LE, 32);
3498 case SystemZ::ATOMIC_LOAD_UMIN_64:
3499 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
3500 SystemZ::CCMASK_CMP_LE, 64);
3502 case SystemZ::ATOMIC_LOADW_UMAX:
3503 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3504 SystemZ::CCMASK_CMP_GE, 0);
3505 case SystemZ::ATOMIC_LOAD_UMAX_32:
3506 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3507 SystemZ::CCMASK_CMP_GE, 32);
3508 case SystemZ::ATOMIC_LOAD_UMAX_64:
3509 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
3510 SystemZ::CCMASK_CMP_GE, 64);
3512 case SystemZ::ATOMIC_CMP_SWAPW:
3513 return emitAtomicCmpSwapW(MI, MBB);
3514 case SystemZ::MVCSequence:
3515 case SystemZ::MVCLoop:
3516 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
3517 case SystemZ::NCSequence:
3518 case SystemZ::NCLoop:
3519 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
3520 case SystemZ::OCSequence:
3521 case SystemZ::OCLoop:
3522 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
3523 case SystemZ::XCSequence:
3524 case SystemZ::XCLoop:
3525 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
3526 case SystemZ::CLCSequence:
3527 case SystemZ::CLCLoop:
3528 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
3529 case SystemZ::CLSTLoop:
3530 return emitStringWrapper(MI, MBB, SystemZ::CLST);
3531 case SystemZ::MVSTLoop:
3532 return emitStringWrapper(MI, MBB, SystemZ::MVST);
3533 case SystemZ::SRSTLoop:
3534 return emitStringWrapper(MI, MBB, SystemZ::SRST);
3536 llvm_unreachable("Unexpected instr type to insert");