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"
31 // Represents a sequence for extracting a 0/1 value from an IPM result:
32 // (((X ^ XORValue) + AddValue) >> Bit)
33 struct IPMConversion {
34 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
35 : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
42 // Represents information about a comparison.
44 Comparison(SDValue Op0In, SDValue Op1In)
45 : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {}
47 // The operands to the comparison.
50 // The opcode that should be used to compare Op0 and Op1.
53 // A SystemZICMP value. Only used for integer comparisons.
56 // The mask of CC values that Opcode can produce.
59 // The mask of CC values for which the original condition is true.
64 // Classify VT as either 32 or 64 bit.
65 static bool is32Bit(EVT VT) {
66 switch (VT.getSimpleVT().SimpleTy) {
72 llvm_unreachable("Unsupported type");
76 // Return a version of MachineOperand that can be safely used before the
78 static MachineOperand earlyUseOperand(MachineOperand Op) {
84 SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm)
85 : TargetLowering(tm, new TargetLoweringObjectFileELF()),
86 Subtarget(*tm.getSubtargetImpl()), TM(tm) {
87 MVT PtrVT = getPointerTy();
89 // Set up the register classes.
90 if (Subtarget.hasHighWord())
91 addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass);
93 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
94 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
95 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
96 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
97 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
99 // Compute derived properties from the register classes
100 computeRegisterProperties();
102 // Set up special registers.
103 setExceptionPointerRegister(SystemZ::R6D);
104 setExceptionSelectorRegister(SystemZ::R7D);
105 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
107 // TODO: It may be better to default to latency-oriented scheduling, however
108 // LLVM's current latency-oriented scheduler can't handle physreg definitions
109 // such as SystemZ has with CC, so set this to the register-pressure
110 // scheduler, because it can.
111 setSchedulingPreference(Sched::RegPressure);
113 setBooleanContents(ZeroOrOneBooleanContent);
114 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
116 // Instructions are strings of 2-byte aligned 2-byte values.
117 setMinFunctionAlignment(2);
119 // Handle operations that are handled in a similar way for all types.
120 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
121 I <= MVT::LAST_FP_VALUETYPE;
123 MVT VT = MVT::SimpleValueType(I);
124 if (isTypeLegal(VT)) {
125 // Lower SET_CC into an IPM-based sequence.
126 setOperationAction(ISD::SETCC, VT, Custom);
128 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
129 setOperationAction(ISD::SELECT, VT, Expand);
131 // Lower SELECT_CC and BR_CC into separate comparisons and branches.
132 setOperationAction(ISD::SELECT_CC, VT, Custom);
133 setOperationAction(ISD::BR_CC, VT, Custom);
137 // Expand jump table branches as address arithmetic followed by an
139 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
141 // Expand BRCOND into a BR_CC (see above).
142 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
144 // Handle integer types.
145 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
146 I <= MVT::LAST_INTEGER_VALUETYPE;
148 MVT VT = MVT::SimpleValueType(I);
149 if (isTypeLegal(VT)) {
150 // Expand individual DIV and REMs into DIVREMs.
151 setOperationAction(ISD::SDIV, VT, Expand);
152 setOperationAction(ISD::UDIV, VT, Expand);
153 setOperationAction(ISD::SREM, VT, Expand);
154 setOperationAction(ISD::UREM, VT, Expand);
155 setOperationAction(ISD::SDIVREM, VT, Custom);
156 setOperationAction(ISD::UDIVREM, VT, Custom);
158 // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and
159 // stores, putting a serialization instruction after the stores.
160 setOperationAction(ISD::ATOMIC_LOAD, VT, Custom);
161 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
163 // No special instructions for these.
164 setOperationAction(ISD::CTPOP, VT, Expand);
165 setOperationAction(ISD::CTTZ, VT, Expand);
166 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
167 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
168 setOperationAction(ISD::ROTR, VT, Expand);
170 // Use *MUL_LOHI where possible instead of MULH*.
171 setOperationAction(ISD::MULHS, VT, Expand);
172 setOperationAction(ISD::MULHU, VT, Expand);
173 setOperationAction(ISD::SMUL_LOHI, VT, Custom);
174 setOperationAction(ISD::UMUL_LOHI, VT, Custom);
176 // We have instructions for signed but not unsigned FP conversion.
177 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
181 // Type legalization will convert 8- and 16-bit atomic operations into
182 // forms that operate on i32s (but still keeping the original memory VT).
183 // Lower them into full i32 operations.
184 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom);
185 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom);
186 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
187 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom);
188 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom);
189 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom);
190 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
191 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom);
192 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom);
193 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
194 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
195 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
197 // We have instructions for signed but not unsigned FP conversion.
198 // Handle unsigned 32-bit types as signed 64-bit types.
199 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
200 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
202 // We have native support for a 64-bit CTLZ, via FLOGR.
203 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
204 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
206 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
207 setOperationAction(ISD::OR, MVT::i64, Custom);
209 // FIXME: Can we support these natively?
210 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
211 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
212 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
214 // We have native instructions for i8, i16 and i32 extensions, but not i1.
215 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
216 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
217 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
218 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
220 // Handle the various types of symbolic address.
221 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
222 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
223 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
224 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
225 setOperationAction(ISD::JumpTable, PtrVT, Custom);
227 // We need to handle dynamic allocations specially because of the
228 // 160-byte area at the bottom of the stack.
229 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
231 // Use custom expanders so that we can force the function to use
233 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
234 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
236 // Handle prefetches with PFD or PFDRL.
237 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
239 // Handle floating-point types.
240 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
241 I <= MVT::LAST_FP_VALUETYPE;
243 MVT VT = MVT::SimpleValueType(I);
244 if (isTypeLegal(VT)) {
245 // We can use FI for FRINT.
246 setOperationAction(ISD::FRINT, VT, Legal);
248 // We can use the extended form of FI for other rounding operations.
249 if (Subtarget.hasFPExtension()) {
250 setOperationAction(ISD::FNEARBYINT, VT, Legal);
251 setOperationAction(ISD::FFLOOR, VT, Legal);
252 setOperationAction(ISD::FCEIL, VT, Legal);
253 setOperationAction(ISD::FTRUNC, VT, Legal);
254 setOperationAction(ISD::FROUND, VT, Legal);
257 // No special instructions for these.
258 setOperationAction(ISD::FSIN, VT, Expand);
259 setOperationAction(ISD::FCOS, VT, Expand);
260 setOperationAction(ISD::FREM, VT, Expand);
264 // We have fused multiply-addition for f32 and f64 but not f128.
265 setOperationAction(ISD::FMA, MVT::f32, Legal);
266 setOperationAction(ISD::FMA, MVT::f64, Legal);
267 setOperationAction(ISD::FMA, MVT::f128, Expand);
269 // Needed so that we don't try to implement f128 constant loads using
270 // a load-and-extend of a f80 constant (in cases where the constant
271 // would fit in an f80).
272 setLoadExtAction(ISD::EXTLOAD, MVT::f80, Expand);
274 // Floating-point truncation and stores need to be done separately.
275 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
276 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
277 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
279 // We have 64-bit FPR<->GPR moves, but need special handling for
281 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
282 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
284 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
285 // structure, but VAEND is a no-op.
286 setOperationAction(ISD::VASTART, MVT::Other, Custom);
287 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
288 setOperationAction(ISD::VAEND, MVT::Other, Expand);
290 // We want to use MVC in preference to even a single load/store pair.
291 MaxStoresPerMemcpy = 0;
292 MaxStoresPerMemcpyOptSize = 0;
294 // The main memset sequence is a byte store followed by an MVC.
295 // Two STC or MV..I stores win over that, but the kind of fused stores
296 // generated by target-independent code don't when the byte value is
297 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
298 // than "STC;MVC". Handle the choice in target-specific code instead.
299 MaxStoresPerMemset = 0;
300 MaxStoresPerMemsetOptSize = 0;
303 EVT SystemZTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
306 return VT.changeVectorElementTypeToInteger();
309 bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
310 VT = VT.getScalarType();
315 switch (VT.getSimpleVT().SimpleTy) {
328 bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
329 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
330 return Imm.isZero() || Imm.isNegZero();
333 bool SystemZTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
335 // Unaligned accesses should never be slower than the expanded version.
336 // We check specifically for aligned accesses in the few cases where
337 // they are required.
343 bool SystemZTargetLowering::isLegalAddressingMode(const AddrMode &AM,
345 // Punt on globals for now, although they can be used in limited
346 // RELATIVE LONG cases.
350 // Require a 20-bit signed offset.
351 if (!isInt<20>(AM.BaseOffs))
354 // Indexing is OK but no scale factor can be applied.
355 return AM.Scale == 0 || AM.Scale == 1;
358 bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
359 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
361 unsigned FromBits = FromType->getPrimitiveSizeInBits();
362 unsigned ToBits = ToType->getPrimitiveSizeInBits();
363 return FromBits > ToBits;
366 bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
367 if (!FromVT.isInteger() || !ToVT.isInteger())
369 unsigned FromBits = FromVT.getSizeInBits();
370 unsigned ToBits = ToVT.getSizeInBits();
371 return FromBits > ToBits;
374 //===----------------------------------------------------------------------===//
375 // Inline asm support
376 //===----------------------------------------------------------------------===//
378 TargetLowering::ConstraintType
379 SystemZTargetLowering::getConstraintType(const std::string &Constraint) const {
380 if (Constraint.size() == 1) {
381 switch (Constraint[0]) {
382 case 'a': // Address register
383 case 'd': // Data register (equivalent to 'r')
384 case 'f': // Floating-point register
385 case 'h': // High-part register
386 case 'r': // General-purpose register
387 return C_RegisterClass;
389 case 'Q': // Memory with base and unsigned 12-bit displacement
390 case 'R': // Likewise, plus an index
391 case 'S': // Memory with base and signed 20-bit displacement
392 case 'T': // Likewise, plus an index
393 case 'm': // Equivalent to 'T'.
396 case 'I': // Unsigned 8-bit constant
397 case 'J': // Unsigned 12-bit constant
398 case 'K': // Signed 16-bit constant
399 case 'L': // Signed 20-bit displacement (on all targets we support)
400 case 'M': // 0x7fffffff
407 return TargetLowering::getConstraintType(Constraint);
410 TargetLowering::ConstraintWeight SystemZTargetLowering::
411 getSingleConstraintMatchWeight(AsmOperandInfo &info,
412 const char *constraint) const {
413 ConstraintWeight weight = CW_Invalid;
414 Value *CallOperandVal = info.CallOperandVal;
415 // If we don't have a value, we can't do a match,
416 // but allow it at the lowest weight.
417 if (CallOperandVal == NULL)
419 Type *type = CallOperandVal->getType();
420 // Look at the constraint type.
421 switch (*constraint) {
423 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
426 case 'a': // Address register
427 case 'd': // Data register (equivalent to 'r')
428 case 'h': // High-part register
429 case 'r': // General-purpose register
430 if (CallOperandVal->getType()->isIntegerTy())
431 weight = CW_Register;
434 case 'f': // Floating-point register
435 if (type->isFloatingPointTy())
436 weight = CW_Register;
439 case 'I': // Unsigned 8-bit constant
440 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
441 if (isUInt<8>(C->getZExtValue()))
442 weight = CW_Constant;
445 case 'J': // Unsigned 12-bit constant
446 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
447 if (isUInt<12>(C->getZExtValue()))
448 weight = CW_Constant;
451 case 'K': // Signed 16-bit constant
452 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
453 if (isInt<16>(C->getSExtValue()))
454 weight = CW_Constant;
457 case 'L': // Signed 20-bit displacement (on all targets we support)
458 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
459 if (isInt<20>(C->getSExtValue()))
460 weight = CW_Constant;
463 case 'M': // 0x7fffffff
464 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
465 if (C->getZExtValue() == 0x7fffffff)
466 weight = CW_Constant;
472 // Parse a "{tNNN}" register constraint for which the register type "t"
473 // has already been verified. MC is the class associated with "t" and
474 // Map maps 0-based register numbers to LLVM register numbers.
475 static std::pair<unsigned, const TargetRegisterClass *>
476 parseRegisterNumber(const std::string &Constraint,
477 const TargetRegisterClass *RC, const unsigned *Map) {
478 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
479 if (isdigit(Constraint[2])) {
480 std::string Suffix(Constraint.data() + 2, Constraint.size() - 2);
481 unsigned Index = atoi(Suffix.c_str());
482 if (Index < 16 && Map[Index])
483 return std::make_pair(Map[Index], RC);
485 return std::make_pair(0u, static_cast<TargetRegisterClass*>(0));
488 std::pair<unsigned, const TargetRegisterClass *> SystemZTargetLowering::
489 getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const {
490 if (Constraint.size() == 1) {
491 // GCC Constraint Letters
492 switch (Constraint[0]) {
494 case 'd': // Data register (equivalent to 'r')
495 case 'r': // General-purpose register
497 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
498 else if (VT == MVT::i128)
499 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
500 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
502 case 'a': // Address register
504 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
505 else if (VT == MVT::i128)
506 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
507 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
509 case 'h': // High-part register (an LLVM extension)
510 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
512 case 'f': // Floating-point register
514 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
515 else if (VT == MVT::f128)
516 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
517 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
520 if (Constraint[0] == '{') {
521 // We need to override the default register parsing for GPRs and FPRs
522 // because the interpretation depends on VT. The internal names of
523 // the registers are also different from the external names
524 // (F0D and F0S instead of F0, etc.).
525 if (Constraint[1] == 'r') {
527 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
528 SystemZMC::GR32Regs);
530 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
531 SystemZMC::GR128Regs);
532 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
533 SystemZMC::GR64Regs);
535 if (Constraint[1] == 'f') {
537 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
538 SystemZMC::FP32Regs);
540 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
541 SystemZMC::FP128Regs);
542 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
543 SystemZMC::FP64Regs);
546 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
549 void SystemZTargetLowering::
550 LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
551 std::vector<SDValue> &Ops,
552 SelectionDAG &DAG) const {
553 // Only support length 1 constraints for now.
554 if (Constraint.length() == 1) {
555 switch (Constraint[0]) {
556 case 'I': // Unsigned 8-bit constant
557 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
558 if (isUInt<8>(C->getZExtValue()))
559 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
563 case 'J': // Unsigned 12-bit constant
564 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
565 if (isUInt<12>(C->getZExtValue()))
566 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
570 case 'K': // Signed 16-bit constant
571 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
572 if (isInt<16>(C->getSExtValue()))
573 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
577 case 'L': // Signed 20-bit displacement (on all targets we support)
578 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
579 if (isInt<20>(C->getSExtValue()))
580 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
584 case 'M': // 0x7fffffff
585 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
586 if (C->getZExtValue() == 0x7fffffff)
587 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
592 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
595 //===----------------------------------------------------------------------===//
596 // Calling conventions
597 //===----------------------------------------------------------------------===//
599 #include "SystemZGenCallingConv.inc"
601 bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
602 Type *ToType) const {
603 return isTruncateFree(FromType, ToType);
606 bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
607 if (!CI->isTailCall())
612 // Value is a value that has been passed to us in the location described by VA
613 // (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
614 // any loads onto Chain.
615 static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL,
616 CCValAssign &VA, SDValue Chain,
618 // If the argument has been promoted from a smaller type, insert an
619 // assertion to capture this.
620 if (VA.getLocInfo() == CCValAssign::SExt)
621 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
622 DAG.getValueType(VA.getValVT()));
623 else if (VA.getLocInfo() == CCValAssign::ZExt)
624 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
625 DAG.getValueType(VA.getValVT()));
628 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
629 else if (VA.getLocInfo() == CCValAssign::Indirect)
630 Value = DAG.getLoad(VA.getValVT(), DL, Chain, Value,
631 MachinePointerInfo(), false, false, false, 0);
633 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
637 // Value is a value of type VA.getValVT() that we need to copy into
638 // the location described by VA. Return a copy of Value converted to
639 // VA.getValVT(). The caller is responsible for handling indirect values.
640 static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL,
641 CCValAssign &VA, SDValue Value) {
642 switch (VA.getLocInfo()) {
643 case CCValAssign::SExt:
644 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
645 case CCValAssign::ZExt:
646 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
647 case CCValAssign::AExt:
648 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
649 case CCValAssign::Full:
652 llvm_unreachable("Unhandled getLocInfo()");
656 SDValue SystemZTargetLowering::
657 LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
658 const SmallVectorImpl<ISD::InputArg> &Ins,
659 SDLoc DL, SelectionDAG &DAG,
660 SmallVectorImpl<SDValue> &InVals) const {
661 MachineFunction &MF = DAG.getMachineFunction();
662 MachineFrameInfo *MFI = MF.getFrameInfo();
663 MachineRegisterInfo &MRI = MF.getRegInfo();
664 SystemZMachineFunctionInfo *FuncInfo =
665 MF.getInfo<SystemZMachineFunctionInfo>();
666 const SystemZFrameLowering *TFL =
667 static_cast<const SystemZFrameLowering *>(TM.getFrameLowering());
669 // Assign locations to all of the incoming arguments.
670 SmallVector<CCValAssign, 16> ArgLocs;
671 CCState CCInfo(CallConv, IsVarArg, MF, TM, ArgLocs, *DAG.getContext());
672 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
674 unsigned NumFixedGPRs = 0;
675 unsigned NumFixedFPRs = 0;
676 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
678 CCValAssign &VA = ArgLocs[I];
679 EVT LocVT = VA.getLocVT();
681 // Arguments passed in registers
682 const TargetRegisterClass *RC;
683 switch (LocVT.getSimpleVT().SimpleTy) {
685 // Integers smaller than i64 should be promoted to i64.
686 llvm_unreachable("Unexpected argument type");
689 RC = &SystemZ::GR32BitRegClass;
693 RC = &SystemZ::GR64BitRegClass;
697 RC = &SystemZ::FP32BitRegClass;
701 RC = &SystemZ::FP64BitRegClass;
705 unsigned VReg = MRI.createVirtualRegister(RC);
706 MRI.addLiveIn(VA.getLocReg(), VReg);
707 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
709 assert(VA.isMemLoc() && "Argument not register or memory");
711 // Create the frame index object for this incoming parameter.
712 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
713 VA.getLocMemOffset(), true);
715 // Create the SelectionDAG nodes corresponding to a load
716 // from this parameter. Unpromoted ints and floats are
717 // passed as right-justified 8-byte values.
718 EVT PtrVT = getPointerTy();
719 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
720 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
721 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, DAG.getIntPtrConstant(4));
722 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
723 MachinePointerInfo::getFixedStack(FI),
724 false, false, false, 0);
727 // Convert the value of the argument register into the value that's
729 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
733 // Save the number of non-varargs registers for later use by va_start, etc.
734 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
735 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
737 // Likewise the address (in the form of a frame index) of where the
738 // first stack vararg would be. The 1-byte size here is arbitrary.
739 int64_t StackSize = CCInfo.getNextStackOffset();
740 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true));
742 // ...and a similar frame index for the caller-allocated save area
743 // that will be used to store the incoming registers.
744 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
745 unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true);
746 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
748 // Store the FPR varargs in the reserved frame slots. (We store the
749 // GPRs as part of the prologue.)
750 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
751 SDValue MemOps[SystemZ::NumArgFPRs];
752 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
753 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
754 int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true);
755 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
756 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
757 &SystemZ::FP64BitRegClass);
758 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
759 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
760 MachinePointerInfo::getFixedStack(FI),
764 // Join the stores, which are independent of one another.
765 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
766 &MemOps[NumFixedFPRs],
767 SystemZ::NumArgFPRs - NumFixedFPRs);
774 static bool canUseSiblingCall(CCState ArgCCInfo,
775 SmallVectorImpl<CCValAssign> &ArgLocs) {
776 // Punt if there are any indirect or stack arguments, or if the call
777 // needs the call-saved argument register R6.
778 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
779 CCValAssign &VA = ArgLocs[I];
780 if (VA.getLocInfo() == CCValAssign::Indirect)
784 unsigned Reg = VA.getLocReg();
785 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
792 SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
793 SmallVectorImpl<SDValue> &InVals) const {
794 SelectionDAG &DAG = CLI.DAG;
796 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
797 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
798 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
799 SDValue Chain = CLI.Chain;
800 SDValue Callee = CLI.Callee;
801 bool &IsTailCall = CLI.IsTailCall;
802 CallingConv::ID CallConv = CLI.CallConv;
803 bool IsVarArg = CLI.IsVarArg;
804 MachineFunction &MF = DAG.getMachineFunction();
805 EVT PtrVT = getPointerTy();
807 // Analyze the operands of the call, assigning locations to each operand.
808 SmallVector<CCValAssign, 16> ArgLocs;
809 CCState ArgCCInfo(CallConv, IsVarArg, MF, TM, ArgLocs, *DAG.getContext());
810 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
812 // We don't support GuaranteedTailCallOpt, only automatically-detected
814 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs))
817 // Get a count of how many bytes are to be pushed on the stack.
818 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
820 // Mark the start of the call.
822 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumBytes, PtrVT, true),
825 // Copy argument values to their designated locations.
826 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
827 SmallVector<SDValue, 8> MemOpChains;
829 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
830 CCValAssign &VA = ArgLocs[I];
831 SDValue ArgValue = OutVals[I];
833 if (VA.getLocInfo() == CCValAssign::Indirect) {
834 // Store the argument in a stack slot and pass its address.
835 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
836 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
837 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, SpillSlot,
838 MachinePointerInfo::getFixedStack(FI),
840 ArgValue = SpillSlot;
842 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
845 // Queue up the argument copies and emit them at the end.
846 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
848 assert(VA.isMemLoc() && "Argument not register or memory");
850 // Work out the address of the stack slot. Unpromoted ints and
851 // floats are passed as right-justified 8-byte values.
852 if (!StackPtr.getNode())
853 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
854 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
855 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
857 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
858 DAG.getIntPtrConstant(Offset));
861 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address,
862 MachinePointerInfo(),
867 // Join the stores, which are independent of one another.
868 if (!MemOpChains.empty())
869 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
870 &MemOpChains[0], MemOpChains.size());
872 // Accept direct calls by converting symbolic call addresses to the
873 // associated Target* opcodes. Force %r1 to be used for indirect
876 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
877 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
878 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
879 } else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
880 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
881 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
882 } else if (IsTailCall) {
883 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
884 Glue = Chain.getValue(1);
885 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
888 // Build a sequence of copy-to-reg nodes, chained and glued together.
889 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
890 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
891 RegsToPass[I].second, Glue);
892 Glue = Chain.getValue(1);
895 // The first call operand is the chain and the second is the target address.
896 SmallVector<SDValue, 8> Ops;
897 Ops.push_back(Chain);
898 Ops.push_back(Callee);
900 // Add argument registers to the end of the list so that they are
901 // known live into the call.
902 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
903 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
904 RegsToPass[I].second.getValueType()));
906 // Glue the call to the argument copies, if any.
911 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
913 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, &Ops[0], Ops.size());
914 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, &Ops[0], Ops.size());
915 Glue = Chain.getValue(1);
917 // Mark the end of the call, which is glued to the call itself.
918 Chain = DAG.getCALLSEQ_END(Chain,
919 DAG.getConstant(NumBytes, PtrVT, true),
920 DAG.getConstant(0, PtrVT, true),
922 Glue = Chain.getValue(1);
924 // Assign locations to each value returned by this call.
925 SmallVector<CCValAssign, 16> RetLocs;
926 CCState RetCCInfo(CallConv, IsVarArg, MF, TM, RetLocs, *DAG.getContext());
927 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
929 // Copy all of the result registers out of their specified physreg.
930 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
931 CCValAssign &VA = RetLocs[I];
933 // Copy the value out, gluing the copy to the end of the call sequence.
934 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
935 VA.getLocVT(), Glue);
936 Chain = RetValue.getValue(1);
937 Glue = RetValue.getValue(2);
939 // Convert the value of the return register into the value that's
941 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
948 SystemZTargetLowering::LowerReturn(SDValue Chain,
949 CallingConv::ID CallConv, bool IsVarArg,
950 const SmallVectorImpl<ISD::OutputArg> &Outs,
951 const SmallVectorImpl<SDValue> &OutVals,
952 SDLoc DL, SelectionDAG &DAG) const {
953 MachineFunction &MF = DAG.getMachineFunction();
955 // Assign locations to each returned value.
956 SmallVector<CCValAssign, 16> RetLocs;
957 CCState RetCCInfo(CallConv, IsVarArg, MF, TM, RetLocs, *DAG.getContext());
958 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
960 // Quick exit for void returns
962 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
964 // Copy the result values into the output registers.
966 SmallVector<SDValue, 4> RetOps;
967 RetOps.push_back(Chain);
968 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
969 CCValAssign &VA = RetLocs[I];
970 SDValue RetValue = OutVals[I];
972 // Make the return register live on exit.
973 assert(VA.isRegLoc() && "Can only return in registers!");
975 // Promote the value as required.
976 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
978 // Chain and glue the copies together.
979 unsigned Reg = VA.getLocReg();
980 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
981 Glue = Chain.getValue(1);
982 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
985 // Update chain and glue.
988 RetOps.push_back(Glue);
990 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other,
991 RetOps.data(), RetOps.size());
994 SDValue SystemZTargetLowering::
995 prepareVolatileOrAtomicLoad(SDValue Chain, SDLoc DL, SelectionDAG &DAG) const {
996 return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain);
999 // CC is a comparison that will be implemented using an integer or
1000 // floating-point comparison. Return the condition code mask for
1001 // a branch on true. In the integer case, CCMASK_CMP_UO is set for
1002 // unsigned comparisons and clear for signed ones. In the floating-point
1003 // case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1004 static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1006 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1007 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1008 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1012 llvm_unreachable("Invalid integer condition!");
1021 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1022 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1027 // Return a sequence for getting a 1 from an IPM result when CC has a
1028 // value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1029 // The handling of CC values outside CCValid doesn't matter.
1030 static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1031 // Deal with cases where the result can be taken directly from a bit
1032 // of the IPM result.
1033 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1034 return IPMConversion(0, 0, SystemZ::IPM_CC);
1035 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1036 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1038 // Deal with cases where we can add a value to force the sign bit
1039 // to contain the right value. Putting the bit in 31 means we can
1040 // use SRL rather than RISBG(L), and also makes it easier to get a
1041 // 0/-1 value, so it has priority over the other tests below.
1043 // These sequences rely on the fact that the upper two bits of the
1044 // IPM result are zero.
1045 uint64_t TopBit = uint64_t(1) << 31;
1046 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1047 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1048 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1049 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1050 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1052 | SystemZ::CCMASK_2)))
1053 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1054 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1055 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1056 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1058 | SystemZ::CCMASK_3)))
1059 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1061 // Next try inverting the value and testing a bit. 0/1 could be
1062 // handled this way too, but we dealt with that case above.
1063 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1064 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1066 // Handle cases where adding a value forces a non-sign bit to contain
1068 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1069 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1070 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1071 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1073 // The remaing cases are 1, 2, 0/1/3 and 0/2/3. All these are
1074 // can be done by inverting the low CC bit and applying one of the
1075 // sign-based extractions above.
1076 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1077 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1078 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1079 return IPMConversion(1 << SystemZ::IPM_CC,
1080 TopBit - (3 << SystemZ::IPM_CC), 31);
1081 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1083 | SystemZ::CCMASK_3)))
1084 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1085 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1087 | SystemZ::CCMASK_3)))
1088 return IPMConversion(1 << SystemZ::IPM_CC,
1089 TopBit - (1 << SystemZ::IPM_CC), 31);
1091 llvm_unreachable("Unexpected CC combination");
1094 // If C can be converted to a comparison against zero, adjust the operands
1096 static void adjustZeroCmp(SelectionDAG &DAG, Comparison &C) {
1097 if (C.ICmpType == SystemZICMP::UnsignedOnly)
1100 ConstantSDNode *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
1104 int64_t Value = ConstOp1->getSExtValue();
1105 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1106 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1107 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1108 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1109 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1110 C.Op1 = DAG.getConstant(0, C.Op1.getValueType());
1114 // If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1115 // adjust the operands as necessary.
1116 static void adjustSubwordCmp(SelectionDAG &DAG, Comparison &C) {
1117 // For us to make any changes, it must a comparison between a single-use
1118 // load and a constant.
1119 if (!C.Op0.hasOneUse() ||
1120 C.Op0.getOpcode() != ISD::LOAD ||
1121 C.Op1.getOpcode() != ISD::Constant)
1124 // We must have an 8- or 16-bit load.
1125 LoadSDNode *Load = cast<LoadSDNode>(C.Op0);
1126 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1127 if (NumBits != 8 && NumBits != 16)
1130 // The load must be an extending one and the constant must be within the
1131 // range of the unextended value.
1132 ConstantSDNode *ConstOp1 = cast<ConstantSDNode>(C.Op1);
1133 uint64_t Value = ConstOp1->getZExtValue();
1134 uint64_t Mask = (1 << NumBits) - 1;
1135 if (Load->getExtensionType() == ISD::SEXTLOAD) {
1136 // Make sure that ConstOp1 is in range of C.Op0.
1137 int64_t SignedValue = ConstOp1->getSExtValue();
1138 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
1140 if (C.ICmpType != SystemZICMP::SignedOnly) {
1141 // Unsigned comparison between two sign-extended values is equivalent
1142 // to unsigned comparison between two zero-extended values.
1144 } else if (NumBits == 8) {
1145 // Try to treat the comparison as unsigned, so that we can use CLI.
1146 // Adjust CCMask and Value as necessary.
1147 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
1148 // Test whether the high bit of the byte is set.
1149 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1150 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
1151 // Test whether the high bit of the byte is clear.
1152 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
1154 // No instruction exists for this combination.
1156 C.ICmpType = SystemZICMP::UnsignedOnly;
1158 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1161 assert(C.ICmpType == SystemZICMP::Any &&
1162 "Signedness shouldn't matter here.");
1166 // Make sure that the first operand is an i32 of the right extension type.
1167 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1170 if (C.Op0.getValueType() != MVT::i32 ||
1171 Load->getExtensionType() != ExtType)
1172 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32,
1173 Load->getChain(), Load->getBasePtr(),
1174 Load->getPointerInfo(), Load->getMemoryVT(),
1175 Load->isVolatile(), Load->isNonTemporal(),
1176 Load->getAlignment());
1178 // Make sure that the second operand is an i32 with the right value.
1179 if (C.Op1.getValueType() != MVT::i32 ||
1180 Value != ConstOp1->getZExtValue())
1181 C.Op1 = DAG.getConstant(Value, MVT::i32);
1184 // Return true if Op is either an unextended load, or a load suitable
1185 // for integer register-memory comparisons of type ICmpType.
1186 static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
1187 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op.getNode());
1189 // There are no instructions to compare a register with a memory byte.
1190 if (Load->getMemoryVT() == MVT::i8)
1192 // Otherwise decide on extension type.
1193 switch (Load->getExtensionType()) {
1194 case ISD::NON_EXTLOAD:
1197 return ICmpType != SystemZICMP::UnsignedOnly;
1199 return ICmpType != SystemZICMP::SignedOnly;
1207 // Return true if it is better to swap the operands of C.
1208 static bool shouldSwapCmpOperands(const Comparison &C) {
1209 // Leave f128 comparisons alone, since they have no memory forms.
1210 if (C.Op0.getValueType() == MVT::f128)
1213 // Always keep a floating-point constant second, since comparisons with
1214 // zero can use LOAD TEST and comparisons with other constants make a
1215 // natural memory operand.
1216 if (isa<ConstantFPSDNode>(C.Op1))
1219 // Never swap comparisons with zero since there are many ways to optimize
1221 ConstantSDNode *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
1222 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
1225 // Also keep natural memory operands second if the loaded value is
1226 // only used here. Several comparisons have memory forms.
1227 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
1230 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1231 // In that case we generally prefer the memory to be second.
1232 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
1233 // The only exceptions are when the second operand is a constant and
1234 // we can use things like CHHSI.
1237 // The unsigned memory-immediate instructions can handle 16-bit
1238 // unsigned integers.
1239 if (C.ICmpType != SystemZICMP::SignedOnly &&
1240 isUInt<16>(ConstOp1->getZExtValue()))
1242 // The signed memory-immediate instructions can handle 16-bit
1244 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1245 isInt<16>(ConstOp1->getSExtValue()))
1250 // Try to promote the use of CGFR and CLGFR.
1251 unsigned Opcode0 = C.Op0.getOpcode();
1252 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
1254 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
1256 if (C.ICmpType != SystemZICMP::SignedOnly &&
1257 Opcode0 == ISD::AND &&
1258 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1259 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
1265 // Return a version of comparison CC mask CCMask in which the LT and GT
1266 // actions are swapped.
1267 static unsigned reverseCCMask(unsigned CCMask) {
1268 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1269 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1270 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1271 (CCMask & SystemZ::CCMASK_CMP_UO));
1274 // Check whether C compares a floating-point value with zero and if that
1275 // floating-point value is also negated. In this case we can use the
1276 // negation to set CC, so avoiding separate LOAD AND TEST and
1277 // LOAD (NEGATIVE/COMPLEMENT) instructions.
1278 static void adjustForFNeg(Comparison &C) {
1279 ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
1280 if (C1 && C1->isZero()) {
1281 for (SDNode::use_iterator I = C.Op0->use_begin(), E = C.Op0->use_end();
1284 if (N->getOpcode() == ISD::FNEG) {
1285 C.Op0 = SDValue(N, 0);
1286 C.CCMask = reverseCCMask(C.CCMask);
1293 // Check whether C compares (shl X, 32) with 0 and whether X is
1294 // also sign-extended. In that case it is better to test the result
1295 // of the sign extension using LTGFR.
1297 // This case is important because InstCombine transforms a comparison
1298 // with (sext (trunc X)) into a comparison with (shl X, 32).
1299 static void adjustForLTGFR(Comparison &C) {
1300 // Check for a comparison between (shl X, 32) and 0.
1301 if (C.Op0.getOpcode() == ISD::SHL &&
1302 C.Op0.getValueType() == MVT::i64 &&
1303 C.Op1.getOpcode() == ISD::Constant &&
1304 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
1305 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
1306 if (C1 && C1->getZExtValue() == 32) {
1307 SDValue ShlOp0 = C.Op0.getOperand(0);
1308 // See whether X has any SIGN_EXTEND_INREG uses.
1309 for (SDNode::use_iterator I = ShlOp0->use_begin(), E = ShlOp0->use_end();
1312 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
1313 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
1314 C.Op0 = SDValue(N, 0);
1322 // Return true if shift operation N has an in-range constant shift value.
1323 // Store it in ShiftVal if so.
1324 static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
1325 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
1329 uint64_t Amount = Shift->getZExtValue();
1330 if (Amount >= N.getValueType().getSizeInBits())
1337 // Check whether an AND with Mask is suitable for a TEST UNDER MASK
1338 // instruction and whether the CC value is descriptive enough to handle
1339 // a comparison of type Opcode between the AND result and CmpVal.
1340 // CCMask says which comparison result is being tested and BitSize is
1341 // the number of bits in the operands. If TEST UNDER MASK can be used,
1342 // return the corresponding CC mask, otherwise return 0.
1343 static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
1344 uint64_t Mask, uint64_t CmpVal,
1345 unsigned ICmpType) {
1346 assert(Mask != 0 && "ANDs with zero should have been removed by now");
1348 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
1349 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
1350 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
1353 // Work out the masks for the lowest and highest bits.
1354 unsigned HighShift = 63 - countLeadingZeros(Mask);
1355 uint64_t High = uint64_t(1) << HighShift;
1356 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
1358 // Signed ordered comparisons are effectively unsigned if the sign
1360 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
1362 // Check for equality comparisons with 0, or the equivalent.
1364 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1365 return SystemZ::CCMASK_TM_ALL_0;
1366 if (CCMask == SystemZ::CCMASK_CMP_NE)
1367 return SystemZ::CCMASK_TM_SOME_1;
1369 if (EffectivelyUnsigned && CmpVal <= Low) {
1370 if (CCMask == SystemZ::CCMASK_CMP_LT)
1371 return SystemZ::CCMASK_TM_ALL_0;
1372 if (CCMask == SystemZ::CCMASK_CMP_GE)
1373 return SystemZ::CCMASK_TM_SOME_1;
1375 if (EffectivelyUnsigned && CmpVal < Low) {
1376 if (CCMask == SystemZ::CCMASK_CMP_LE)
1377 return SystemZ::CCMASK_TM_ALL_0;
1378 if (CCMask == SystemZ::CCMASK_CMP_GT)
1379 return SystemZ::CCMASK_TM_SOME_1;
1382 // Check for equality comparisons with the mask, or the equivalent.
1383 if (CmpVal == Mask) {
1384 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1385 return SystemZ::CCMASK_TM_ALL_1;
1386 if (CCMask == SystemZ::CCMASK_CMP_NE)
1387 return SystemZ::CCMASK_TM_SOME_0;
1389 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
1390 if (CCMask == SystemZ::CCMASK_CMP_GT)
1391 return SystemZ::CCMASK_TM_ALL_1;
1392 if (CCMask == SystemZ::CCMASK_CMP_LE)
1393 return SystemZ::CCMASK_TM_SOME_0;
1395 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
1396 if (CCMask == SystemZ::CCMASK_CMP_GE)
1397 return SystemZ::CCMASK_TM_ALL_1;
1398 if (CCMask == SystemZ::CCMASK_CMP_LT)
1399 return SystemZ::CCMASK_TM_SOME_0;
1402 // Check for ordered comparisons with the top bit.
1403 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
1404 if (CCMask == SystemZ::CCMASK_CMP_LE)
1405 return SystemZ::CCMASK_TM_MSB_0;
1406 if (CCMask == SystemZ::CCMASK_CMP_GT)
1407 return SystemZ::CCMASK_TM_MSB_1;
1409 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
1410 if (CCMask == SystemZ::CCMASK_CMP_LT)
1411 return SystemZ::CCMASK_TM_MSB_0;
1412 if (CCMask == SystemZ::CCMASK_CMP_GE)
1413 return SystemZ::CCMASK_TM_MSB_1;
1416 // If there are just two bits, we can do equality checks for Low and High
1418 if (Mask == Low + High) {
1419 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
1420 return SystemZ::CCMASK_TM_MIXED_MSB_0;
1421 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
1422 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
1423 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
1424 return SystemZ::CCMASK_TM_MIXED_MSB_1;
1425 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
1426 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
1429 // Looks like we've exhausted our options.
1433 // See whether C can be implemented as a TEST UNDER MASK instruction.
1434 // Update the arguments with the TM version if so.
1435 static void adjustForTestUnderMask(SelectionDAG &DAG, Comparison &C) {
1436 // Check that we have a comparison with a constant.
1437 ConstantSDNode *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
1440 uint64_t CmpVal = ConstOp1->getZExtValue();
1442 // Check whether the nonconstant input is an AND with a constant mask.
1443 if (C.Op0.getOpcode() != ISD::AND)
1445 SDValue AndOp0 = C.Op0.getOperand(0);
1446 SDValue AndOp1 = C.Op0.getOperand(1);
1447 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(AndOp1.getNode());
1450 uint64_t MaskVal = Mask->getZExtValue();
1452 // Check whether the combination of mask, comparison value and comparison
1453 // type are suitable.
1454 unsigned BitSize = C.Op0.getValueType().getSizeInBits();
1455 unsigned NewCCMask, ShiftVal;
1456 if (C.ICmpType != SystemZICMP::SignedOnly &&
1457 AndOp0.getOpcode() == ISD::SHL &&
1458 isSimpleShift(AndOp0, ShiftVal) &&
1459 (NewCCMask = getTestUnderMaskCond(BitSize, C.CCMask, MaskVal >> ShiftVal,
1461 SystemZICMP::Any))) {
1462 AndOp0 = AndOp0.getOperand(0);
1463 AndOp1 = DAG.getConstant(MaskVal >> ShiftVal, AndOp0.getValueType());
1464 } else if (C.ICmpType != SystemZICMP::SignedOnly &&
1465 AndOp0.getOpcode() == ISD::SRL &&
1466 isSimpleShift(AndOp0, ShiftVal) &&
1467 (NewCCMask = getTestUnderMaskCond(BitSize, C.CCMask,
1468 MaskVal << ShiftVal,
1470 SystemZICMP::UnsignedOnly))) {
1471 AndOp0 = AndOp0.getOperand(0);
1472 AndOp1 = DAG.getConstant(MaskVal << ShiftVal, AndOp0.getValueType());
1474 NewCCMask = getTestUnderMaskCond(BitSize, C.CCMask, MaskVal, CmpVal,
1480 // Go ahead and make the change.
1481 C.Opcode = SystemZISD::TM;
1484 C.CCValid = SystemZ::CCMASK_TM;
1485 C.CCMask = NewCCMask;
1488 // Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
1489 static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
1490 ISD::CondCode Cond) {
1491 Comparison C(CmpOp0, CmpOp1);
1492 C.CCMask = CCMaskForCondCode(Cond);
1493 if (C.Op0.getValueType().isFloatingPoint()) {
1494 C.CCValid = SystemZ::CCMASK_FCMP;
1495 C.Opcode = SystemZISD::FCMP;
1497 C.CCValid = SystemZ::CCMASK_ICMP;
1498 C.Opcode = SystemZISD::ICMP;
1499 // Choose the type of comparison. Equality and inequality tests can
1500 // use either signed or unsigned comparisons. The choice also doesn't
1501 // matter if both sign bits are known to be clear. In those cases we
1502 // want to give the main isel code the freedom to choose whichever
1504 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1505 C.CCMask == SystemZ::CCMASK_CMP_NE ||
1506 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
1507 C.ICmpType = SystemZICMP::Any;
1508 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
1509 C.ICmpType = SystemZICMP::UnsignedOnly;
1511 C.ICmpType = SystemZICMP::SignedOnly;
1512 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
1513 adjustZeroCmp(DAG, C);
1514 adjustSubwordCmp(DAG, C);
1517 if (shouldSwapCmpOperands(C)) {
1518 std::swap(C.Op0, C.Op1);
1519 C.CCMask = reverseCCMask(C.CCMask);
1522 adjustForTestUnderMask(DAG, C);
1528 // Emit the comparison instruction described by C.
1529 static SDValue emitCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
1530 if (C.Opcode == SystemZISD::ICMP)
1531 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
1532 DAG.getConstant(C.ICmpType, MVT::i32));
1533 if (C.Opcode == SystemZISD::TM) {
1534 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
1535 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
1536 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
1537 DAG.getConstant(RegisterOnly, MVT::i32));
1539 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
1542 // Implement a 32-bit *MUL_LOHI operation by extending both operands to
1543 // 64 bits. Extend is the extension type to use. Store the high part
1544 // in Hi and the low part in Lo.
1545 static void lowerMUL_LOHI32(SelectionDAG &DAG, SDLoc DL,
1546 unsigned Extend, SDValue Op0, SDValue Op1,
1547 SDValue &Hi, SDValue &Lo) {
1548 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
1549 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
1550 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
1551 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul, DAG.getConstant(32, MVT::i64));
1552 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
1553 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
1556 // Lower a binary operation that produces two VT results, one in each
1557 // half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
1558 // Extend extends Op0 to a GR128, and Opcode performs the GR128 operation
1559 // on the extended Op0 and (unextended) Op1. Store the even register result
1560 // in Even and the odd register result in Odd.
1561 static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT,
1562 unsigned Extend, unsigned Opcode,
1563 SDValue Op0, SDValue Op1,
1564 SDValue &Even, SDValue &Odd) {
1565 SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0);
1566 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped,
1567 SDValue(In128, 0), Op1);
1568 bool Is32Bit = is32Bit(VT);
1569 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
1570 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
1573 // Return an i32 value that is 1 if the CC value produced by Glue is
1574 // in the mask CCMask and 0 otherwise. CC is known to have a value
1575 // in CCValid, so other values can be ignored.
1576 static SDValue emitSETCC(SelectionDAG &DAG, SDLoc DL, SDValue Glue,
1577 unsigned CCValid, unsigned CCMask) {
1578 IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
1579 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
1581 if (Conversion.XORValue)
1582 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
1583 DAG.getConstant(Conversion.XORValue, MVT::i32));
1585 if (Conversion.AddValue)
1586 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
1587 DAG.getConstant(Conversion.AddValue, MVT::i32));
1589 // The SHR/AND sequence should get optimized to an RISBG.
1590 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
1591 DAG.getConstant(Conversion.Bit, MVT::i32));
1592 if (Conversion.Bit != 31)
1593 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
1594 DAG.getConstant(1, MVT::i32));
1598 SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
1599 SelectionDAG &DAG) const {
1600 SDValue CmpOp0 = Op.getOperand(0);
1601 SDValue CmpOp1 = Op.getOperand(1);
1602 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
1605 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC));
1606 SDValue Glue = emitCmp(DAG, DL, C);
1607 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
1610 SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
1611 SDValue Chain = Op.getOperand(0);
1612 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1613 SDValue CmpOp0 = Op.getOperand(2);
1614 SDValue CmpOp1 = Op.getOperand(3);
1615 SDValue Dest = Op.getOperand(4);
1618 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC));
1619 SDValue Glue = emitCmp(DAG, DL, C);
1620 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
1621 Chain, DAG.getConstant(C.CCValid, MVT::i32),
1622 DAG.getConstant(C.CCMask, MVT::i32), Dest, Glue);
1625 // Return true if Pos is CmpOp and Neg is the negative of CmpOp,
1626 // allowing Pos and Neg to be wider than CmpOp.
1627 static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
1628 return (Neg.getOpcode() == ISD::SUB &&
1629 Neg.getOperand(0).getOpcode() == ISD::Constant &&
1630 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
1631 Neg.getOperand(1) == Pos &&
1633 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
1634 Pos.getOperand(0) == CmpOp)));
1637 // Return the absolute or negative absolute of Op; IsNegative decides which.
1638 static SDValue getAbsolute(SelectionDAG &DAG, SDLoc DL, SDValue Op,
1640 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
1642 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
1643 DAG.getConstant(0, Op.getValueType()), Op);
1647 SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
1648 SelectionDAG &DAG) const {
1649 SDValue CmpOp0 = Op.getOperand(0);
1650 SDValue CmpOp1 = Op.getOperand(1);
1651 SDValue TrueOp = Op.getOperand(2);
1652 SDValue FalseOp = Op.getOperand(3);
1653 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1656 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC));
1658 // Check for absolute and negative-absolute selections, including those
1659 // where the comparison value is sign-extended (for LPGFR and LNGFR).
1660 // This check supplements the one in DAGCombiner.
1661 if (C.Opcode == SystemZISD::ICMP &&
1662 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
1663 C.CCMask != SystemZ::CCMASK_CMP_NE &&
1664 C.Op1.getOpcode() == ISD::Constant &&
1665 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
1666 if (isAbsolute(C.Op0, TrueOp, FalseOp))
1667 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
1668 if (isAbsolute(C.Op0, FalseOp, TrueOp))
1669 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
1672 SDValue Glue = emitCmp(DAG, DL, C);
1674 // Special case for handling -1/0 results. The shifts we use here
1675 // should get optimized with the IPM conversion sequence.
1676 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
1677 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
1678 if (TrueC && FalseC) {
1679 int64_t TrueVal = TrueC->getSExtValue();
1680 int64_t FalseVal = FalseC->getSExtValue();
1681 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
1682 // Invert the condition if we want -1 on false.
1684 C.CCMask ^= C.CCValid;
1685 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
1686 EVT VT = Op.getValueType();
1687 // Extend the result to VT. Upper bits are ignored.
1689 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
1690 // Sign-extend from the low bit.
1691 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, MVT::i32);
1692 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
1693 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
1697 SmallVector<SDValue, 5> Ops;
1698 Ops.push_back(TrueOp);
1699 Ops.push_back(FalseOp);
1700 Ops.push_back(DAG.getConstant(C.CCValid, MVT::i32));
1701 Ops.push_back(DAG.getConstant(C.CCMask, MVT::i32));
1702 Ops.push_back(Glue);
1704 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
1705 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, &Ops[0], Ops.size());
1708 SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
1709 SelectionDAG &DAG) const {
1711 const GlobalValue *GV = Node->getGlobal();
1712 int64_t Offset = Node->getOffset();
1713 EVT PtrVT = getPointerTy();
1714 Reloc::Model RM = TM.getRelocationModel();
1715 CodeModel::Model CM = TM.getCodeModel();
1718 if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) {
1719 // Assign anchors at 1<<12 byte boundaries.
1720 uint64_t Anchor = Offset & ~uint64_t(0xfff);
1721 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
1722 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1724 // The offset can be folded into the address if it is aligned to a halfword.
1726 if (Offset != 0 && (Offset & 1) == 0) {
1727 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
1728 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
1732 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
1733 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1734 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
1735 MachinePointerInfo::getGOT(), false, false, false, 0);
1738 // If there was a non-zero offset that we didn't fold, create an explicit
1741 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
1742 DAG.getConstant(Offset, PtrVT));
1747 SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
1748 SelectionDAG &DAG) const {
1750 const GlobalValue *GV = Node->getGlobal();
1751 EVT PtrVT = getPointerTy();
1752 TLSModel::Model model = TM.getTLSModel(GV);
1754 if (model != TLSModel::LocalExec)
1755 llvm_unreachable("only local-exec TLS mode supported");
1757 // The high part of the thread pointer is in access register 0.
1758 SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
1759 DAG.getConstant(0, MVT::i32));
1760 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
1762 // The low part of the thread pointer is in access register 1.
1763 SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
1764 DAG.getConstant(1, MVT::i32));
1765 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
1767 // Merge them into a single 64-bit address.
1768 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
1769 DAG.getConstant(32, PtrVT));
1770 SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
1772 // Get the offset of GA from the thread pointer.
1773 SystemZConstantPoolValue *CPV =
1774 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
1776 // Force the offset into the constant pool and load it from there.
1777 SDValue CPAddr = DAG.getConstantPool(CPV, PtrVT, 8);
1778 SDValue Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
1779 CPAddr, MachinePointerInfo::getConstantPool(),
1780 false, false, false, 0);
1782 // Add the base and offset together.
1783 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
1786 SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
1787 SelectionDAG &DAG) const {
1789 const BlockAddress *BA = Node->getBlockAddress();
1790 int64_t Offset = Node->getOffset();
1791 EVT PtrVT = getPointerTy();
1793 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
1794 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1798 SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
1799 SelectionDAG &DAG) const {
1801 EVT PtrVT = getPointerTy();
1802 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1804 // Use LARL to load the address of the table.
1805 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1808 SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
1809 SelectionDAG &DAG) const {
1811 EVT PtrVT = getPointerTy();
1814 if (CP->isMachineConstantPoolEntry())
1815 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1816 CP->getAlignment());
1818 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1819 CP->getAlignment(), CP->getOffset());
1821 // Use LARL to load the address of the constant pool entry.
1822 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1825 SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
1826 SelectionDAG &DAG) const {
1828 SDValue In = Op.getOperand(0);
1829 EVT InVT = In.getValueType();
1830 EVT ResVT = Op.getValueType();
1832 if (InVT == MVT::i32 && ResVT == MVT::f32) {
1834 if (Subtarget.hasHighWord()) {
1835 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
1837 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
1838 MVT::i64, SDValue(U64, 0), In);
1840 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
1841 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
1842 DAG.getConstant(32, MVT::i64));
1844 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
1845 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32,
1846 DL, MVT::f32, Out64);
1848 if (InVT == MVT::f32 && ResVT == MVT::i32) {
1849 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
1850 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
1851 MVT::f64, SDValue(U64, 0), In);
1852 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
1853 if (Subtarget.hasHighWord())
1854 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
1856 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
1857 DAG.getConstant(32, MVT::i64));
1858 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
1860 llvm_unreachable("Unexpected bitcast combination");
1863 SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
1864 SelectionDAG &DAG) const {
1865 MachineFunction &MF = DAG.getMachineFunction();
1866 SystemZMachineFunctionInfo *FuncInfo =
1867 MF.getInfo<SystemZMachineFunctionInfo>();
1868 EVT PtrVT = getPointerTy();
1870 SDValue Chain = Op.getOperand(0);
1871 SDValue Addr = Op.getOperand(1);
1872 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1875 // The initial values of each field.
1876 const unsigned NumFields = 4;
1877 SDValue Fields[NumFields] = {
1878 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), PtrVT),
1879 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), PtrVT),
1880 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
1881 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
1884 // Store each field into its respective slot.
1885 SDValue MemOps[NumFields];
1886 unsigned Offset = 0;
1887 for (unsigned I = 0; I < NumFields; ++I) {
1888 SDValue FieldAddr = Addr;
1890 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
1891 DAG.getIntPtrConstant(Offset));
1892 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
1893 MachinePointerInfo(SV, Offset),
1897 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps, NumFields);
1900 SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
1901 SelectionDAG &DAG) const {
1902 SDValue Chain = Op.getOperand(0);
1903 SDValue DstPtr = Op.getOperand(1);
1904 SDValue SrcPtr = Op.getOperand(2);
1905 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
1906 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
1909 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32),
1910 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
1911 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
1914 SDValue SystemZTargetLowering::
1915 lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
1916 SDValue Chain = Op.getOperand(0);
1917 SDValue Size = Op.getOperand(1);
1920 unsigned SPReg = getStackPointerRegisterToSaveRestore();
1922 // Get a reference to the stack pointer.
1923 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
1925 // Get the new stack pointer value.
1926 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, Size);
1928 // Copy the new stack pointer back.
1929 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
1931 // The allocated data lives above the 160 bytes allocated for the standard
1932 // frame, plus any outgoing stack arguments. We don't know how much that
1933 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
1934 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
1935 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
1937 SDValue Ops[2] = { Result, Chain };
1938 return DAG.getMergeValues(Ops, 2, DL);
1941 SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
1942 SelectionDAG &DAG) const {
1943 EVT VT = Op.getValueType();
1947 // Just do a normal 64-bit multiplication and extract the results.
1948 // We define this so that it can be used for constant division.
1949 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
1950 Op.getOperand(1), Ops[1], Ops[0]);
1952 // Do a full 128-bit multiplication based on UMUL_LOHI64:
1954 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
1956 // but using the fact that the upper halves are either all zeros
1959 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
1961 // and grouping the right terms together since they are quicker than the
1964 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
1965 SDValue C63 = DAG.getConstant(63, MVT::i64);
1966 SDValue LL = Op.getOperand(0);
1967 SDValue RL = Op.getOperand(1);
1968 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
1969 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
1970 // UMUL_LOHI64 returns the low result in the odd register and the high
1971 // result in the even register. SMUL_LOHI is defined to return the
1972 // low half first, so the results are in reverse order.
1973 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
1974 LL, RL, Ops[1], Ops[0]);
1975 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
1976 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
1977 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
1978 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
1980 return DAG.getMergeValues(Ops, 2, DL);
1983 SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
1984 SelectionDAG &DAG) const {
1985 EVT VT = Op.getValueType();
1989 // Just do a normal 64-bit multiplication and extract the results.
1990 // We define this so that it can be used for constant division.
1991 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
1992 Op.getOperand(1), Ops[1], Ops[0]);
1994 // UMUL_LOHI64 returns the low result in the odd register and the high
1995 // result in the even register. UMUL_LOHI is defined to return the
1996 // low half first, so the results are in reverse order.
1997 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
1998 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
1999 return DAG.getMergeValues(Ops, 2, DL);
2002 SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
2003 SelectionDAG &DAG) const {
2004 SDValue Op0 = Op.getOperand(0);
2005 SDValue Op1 = Op.getOperand(1);
2006 EVT VT = Op.getValueType();
2010 // We use DSGF for 32-bit division.
2012 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
2013 Opcode = SystemZISD::SDIVREM32;
2014 } else if (DAG.ComputeNumSignBits(Op1) > 32) {
2015 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
2016 Opcode = SystemZISD::SDIVREM32;
2018 Opcode = SystemZISD::SDIVREM64;
2020 // DSG(F) takes a 64-bit dividend, so the even register in the GR128
2021 // input is "don't care". The instruction returns the remainder in
2022 // the even register and the quotient in the odd register.
2024 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
2025 Op0, Op1, Ops[1], Ops[0]);
2026 return DAG.getMergeValues(Ops, 2, DL);
2029 SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
2030 SelectionDAG &DAG) const {
2031 EVT VT = Op.getValueType();
2034 // DL(G) uses a double-width dividend, so we need to clear the even
2035 // register in the GR128 input. The instruction returns the remainder
2036 // in the even register and the quotient in the odd register.
2039 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
2040 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2042 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
2043 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2044 return DAG.getMergeValues(Ops, 2, DL);
2047 SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
2048 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
2050 // Get the known-zero masks for each operand.
2051 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
2052 APInt KnownZero[2], KnownOne[2];
2053 DAG.ComputeMaskedBits(Ops[0], KnownZero[0], KnownOne[0]);
2054 DAG.ComputeMaskedBits(Ops[1], KnownZero[1], KnownOne[1]);
2056 // See if the upper 32 bits of one operand and the lower 32 bits of the
2057 // other are known zero. They are the low and high operands respectively.
2058 uint64_t Masks[] = { KnownZero[0].getZExtValue(),
2059 KnownZero[1].getZExtValue() };
2061 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
2063 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
2068 SDValue LowOp = Ops[Low];
2069 SDValue HighOp = Ops[High];
2071 // If the high part is a constant, we're better off using IILH.
2072 if (HighOp.getOpcode() == ISD::Constant)
2075 // If the low part is a constant that is outside the range of LHI,
2076 // then we're better off using IILF.
2077 if (LowOp.getOpcode() == ISD::Constant) {
2078 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
2079 if (!isInt<16>(Value))
2083 // Check whether the high part is an AND that doesn't change the
2084 // high 32 bits and just masks out low bits. We can skip it if so.
2085 if (HighOp.getOpcode() == ISD::AND &&
2086 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
2087 SDValue HighOp0 = HighOp.getOperand(0);
2088 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
2089 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
2093 // Take advantage of the fact that all GR32 operations only change the
2094 // low 32 bits by truncating Low to an i32 and inserting it directly
2095 // using a subreg. The interesting cases are those where the truncation
2098 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
2099 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
2100 MVT::i64, HighOp, Low32);
2103 // Op is an atomic load. Lower it into a normal volatile load.
2104 SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
2105 SelectionDAG &DAG) const {
2106 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode());
2107 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
2108 Node->getChain(), Node->getBasePtr(),
2109 Node->getMemoryVT(), Node->getMemOperand());
2112 // Op is an atomic store. Lower it into a normal volatile store followed
2113 // by a serialization.
2114 SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
2115 SelectionDAG &DAG) const {
2116 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode());
2117 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
2118 Node->getBasePtr(), Node->getMemoryVT(),
2119 Node->getMemOperand());
2120 return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other,
2124 // Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
2125 // two into the fullword ATOMIC_LOADW_* operation given by Opcode.
2126 SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
2128 unsigned Opcode) const {
2129 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode());
2131 // 32-bit operations need no code outside the main loop.
2132 EVT NarrowVT = Node->getMemoryVT();
2133 EVT WideVT = MVT::i32;
2134 if (NarrowVT == WideVT)
2137 int64_t BitSize = NarrowVT.getSizeInBits();
2138 SDValue ChainIn = Node->getChain();
2139 SDValue Addr = Node->getBasePtr();
2140 SDValue Src2 = Node->getVal();
2141 MachineMemOperand *MMO = Node->getMemOperand();
2143 EVT PtrVT = Addr.getValueType();
2145 // Convert atomic subtracts of constants into additions.
2146 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
2147 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(Src2)) {
2148 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
2149 Src2 = DAG.getConstant(-Const->getSExtValue(), Src2.getValueType());
2152 // Get the address of the containing word.
2153 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
2154 DAG.getConstant(-4, PtrVT));
2156 // Get the number of bits that the word must be rotated left in order
2157 // to bring the field to the top bits of a GR32.
2158 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
2159 DAG.getConstant(3, PtrVT));
2160 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
2162 // Get the complementing shift amount, for rotating a field in the top
2163 // bits back to its proper position.
2164 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
2165 DAG.getConstant(0, WideVT), BitShift);
2167 // Extend the source operand to 32 bits and prepare it for the inner loop.
2168 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
2169 // operations require the source to be shifted in advance. (This shift
2170 // can be folded if the source is constant.) For AND and NAND, the lower
2171 // bits must be set, while for other opcodes they should be left clear.
2172 if (Opcode != SystemZISD::ATOMIC_SWAPW)
2173 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
2174 DAG.getConstant(32 - BitSize, WideVT));
2175 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
2176 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
2177 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
2178 DAG.getConstant(uint32_t(-1) >> BitSize, WideVT));
2180 // Construct the ATOMIC_LOADW_* node.
2181 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
2182 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
2183 DAG.getConstant(BitSize, WideVT) };
2184 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
2185 array_lengthof(Ops),
2188 // Rotate the result of the final CS so that the field is in the lower
2189 // bits of a GR32, then truncate it.
2190 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
2191 DAG.getConstant(BitSize, WideVT));
2192 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
2194 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
2195 return DAG.getMergeValues(RetOps, 2, DL);
2198 // Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
2199 // into a fullword ATOMIC_CMP_SWAPW operation.
2200 SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
2201 SelectionDAG &DAG) const {
2202 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode());
2204 // We have native support for 32-bit compare and swap.
2205 EVT NarrowVT = Node->getMemoryVT();
2206 EVT WideVT = MVT::i32;
2207 if (NarrowVT == WideVT)
2210 int64_t BitSize = NarrowVT.getSizeInBits();
2211 SDValue ChainIn = Node->getOperand(0);
2212 SDValue Addr = Node->getOperand(1);
2213 SDValue CmpVal = Node->getOperand(2);
2214 SDValue SwapVal = Node->getOperand(3);
2215 MachineMemOperand *MMO = Node->getMemOperand();
2217 EVT PtrVT = Addr.getValueType();
2219 // Get the address of the containing word.
2220 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
2221 DAG.getConstant(-4, PtrVT));
2223 // Get the number of bits that the word must be rotated left in order
2224 // to bring the field to the top bits of a GR32.
2225 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
2226 DAG.getConstant(3, PtrVT));
2227 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
2229 // Get the complementing shift amount, for rotating a field in the top
2230 // bits back to its proper position.
2231 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
2232 DAG.getConstant(0, WideVT), BitShift);
2234 // Construct the ATOMIC_CMP_SWAPW node.
2235 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
2236 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
2237 NegBitShift, DAG.getConstant(BitSize, WideVT) };
2238 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
2239 VTList, Ops, array_lengthof(Ops),
2244 SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
2245 SelectionDAG &DAG) const {
2246 MachineFunction &MF = DAG.getMachineFunction();
2247 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
2248 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
2249 SystemZ::R15D, Op.getValueType());
2252 SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
2253 SelectionDAG &DAG) const {
2254 MachineFunction &MF = DAG.getMachineFunction();
2255 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
2256 return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op),
2257 SystemZ::R15D, Op.getOperand(1));
2260 SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
2261 SelectionDAG &DAG) const {
2262 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2264 // Just preserve the chain.
2265 return Op.getOperand(0);
2267 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2268 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
2269 MemIntrinsicSDNode *Node = cast<MemIntrinsicSDNode>(Op.getNode());
2272 DAG.getConstant(Code, MVT::i32),
2275 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, SDLoc(Op),
2276 Node->getVTList(), Ops, array_lengthof(Ops),
2277 Node->getMemoryVT(), Node->getMemOperand());
2280 SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
2281 SelectionDAG &DAG) const {
2282 switch (Op.getOpcode()) {
2284 return lowerBR_CC(Op, DAG);
2285 case ISD::SELECT_CC:
2286 return lowerSELECT_CC(Op, DAG);
2288 return lowerSETCC(Op, DAG);
2289 case ISD::GlobalAddress:
2290 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
2291 case ISD::GlobalTLSAddress:
2292 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
2293 case ISD::BlockAddress:
2294 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
2295 case ISD::JumpTable:
2296 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
2297 case ISD::ConstantPool:
2298 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
2300 return lowerBITCAST(Op, DAG);
2302 return lowerVASTART(Op, DAG);
2304 return lowerVACOPY(Op, DAG);
2305 case ISD::DYNAMIC_STACKALLOC:
2306 return lowerDYNAMIC_STACKALLOC(Op, DAG);
2307 case ISD::SMUL_LOHI:
2308 return lowerSMUL_LOHI(Op, DAG);
2309 case ISD::UMUL_LOHI:
2310 return lowerUMUL_LOHI(Op, DAG);
2312 return lowerSDIVREM(Op, DAG);
2314 return lowerUDIVREM(Op, DAG);
2316 return lowerOR(Op, DAG);
2317 case ISD::ATOMIC_SWAP:
2318 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
2319 case ISD::ATOMIC_STORE:
2320 return lowerATOMIC_STORE(Op, DAG);
2321 case ISD::ATOMIC_LOAD:
2322 return lowerATOMIC_LOAD(Op, DAG);
2323 case ISD::ATOMIC_LOAD_ADD:
2324 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
2325 case ISD::ATOMIC_LOAD_SUB:
2326 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
2327 case ISD::ATOMIC_LOAD_AND:
2328 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
2329 case ISD::ATOMIC_LOAD_OR:
2330 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
2331 case ISD::ATOMIC_LOAD_XOR:
2332 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
2333 case ISD::ATOMIC_LOAD_NAND:
2334 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
2335 case ISD::ATOMIC_LOAD_MIN:
2336 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
2337 case ISD::ATOMIC_LOAD_MAX:
2338 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
2339 case ISD::ATOMIC_LOAD_UMIN:
2340 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
2341 case ISD::ATOMIC_LOAD_UMAX:
2342 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
2343 case ISD::ATOMIC_CMP_SWAP:
2344 return lowerATOMIC_CMP_SWAP(Op, DAG);
2345 case ISD::STACKSAVE:
2346 return lowerSTACKSAVE(Op, DAG);
2347 case ISD::STACKRESTORE:
2348 return lowerSTACKRESTORE(Op, DAG);
2350 return lowerPREFETCH(Op, DAG);
2352 llvm_unreachable("Unexpected node to lower");
2356 const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
2357 #define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
2362 OPCODE(PCREL_WRAPPER);
2363 OPCODE(PCREL_OFFSET);
2369 OPCODE(SELECT_CCMASK);
2370 OPCODE(ADJDYNALLOC);
2371 OPCODE(EXTRACT_ACCESS);
2372 OPCODE(UMUL_LOHI64);
2388 OPCODE(SEARCH_STRING);
2391 OPCODE(ATOMIC_SWAPW);
2392 OPCODE(ATOMIC_LOADW_ADD);
2393 OPCODE(ATOMIC_LOADW_SUB);
2394 OPCODE(ATOMIC_LOADW_AND);
2395 OPCODE(ATOMIC_LOADW_OR);
2396 OPCODE(ATOMIC_LOADW_XOR);
2397 OPCODE(ATOMIC_LOADW_NAND);
2398 OPCODE(ATOMIC_LOADW_MIN);
2399 OPCODE(ATOMIC_LOADW_MAX);
2400 OPCODE(ATOMIC_LOADW_UMIN);
2401 OPCODE(ATOMIC_LOADW_UMAX);
2402 OPCODE(ATOMIC_CMP_SWAPW);
2409 //===----------------------------------------------------------------------===//
2411 //===----------------------------------------------------------------------===//
2413 // Create a new basic block after MBB.
2414 static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
2415 MachineFunction &MF = *MBB->getParent();
2416 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
2417 MF.insert(llvm::next(MachineFunction::iterator(MBB)), NewMBB);
2421 // Split MBB after MI and return the new block (the one that contains
2422 // instructions after MI).
2423 static MachineBasicBlock *splitBlockAfter(MachineInstr *MI,
2424 MachineBasicBlock *MBB) {
2425 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
2426 NewMBB->splice(NewMBB->begin(), MBB,
2427 llvm::next(MachineBasicBlock::iterator(MI)),
2429 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
2433 // Split MBB before MI and return the new block (the one that contains MI).
2434 static MachineBasicBlock *splitBlockBefore(MachineInstr *MI,
2435 MachineBasicBlock *MBB) {
2436 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
2437 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
2438 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
2442 // Force base value Base into a register before MI. Return the register.
2443 static unsigned forceReg(MachineInstr *MI, MachineOperand &Base,
2444 const SystemZInstrInfo *TII) {
2446 return Base.getReg();
2448 MachineBasicBlock *MBB = MI->getParent();
2449 MachineFunction &MF = *MBB->getParent();
2450 MachineRegisterInfo &MRI = MF.getRegInfo();
2452 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
2453 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg)
2454 .addOperand(Base).addImm(0).addReg(0);
2458 // Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
2460 SystemZTargetLowering::emitSelect(MachineInstr *MI,
2461 MachineBasicBlock *MBB) const {
2462 const SystemZInstrInfo *TII = TM.getInstrInfo();
2464 unsigned DestReg = MI->getOperand(0).getReg();
2465 unsigned TrueReg = MI->getOperand(1).getReg();
2466 unsigned FalseReg = MI->getOperand(2).getReg();
2467 unsigned CCValid = MI->getOperand(3).getImm();
2468 unsigned CCMask = MI->getOperand(4).getImm();
2469 DebugLoc DL = MI->getDebugLoc();
2471 MachineBasicBlock *StartMBB = MBB;
2472 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
2473 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
2476 // BRC CCMask, JoinMBB
2477 // # fallthrough to FalseMBB
2479 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2480 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
2481 MBB->addSuccessor(JoinMBB);
2482 MBB->addSuccessor(FalseMBB);
2485 // # fallthrough to JoinMBB
2487 MBB->addSuccessor(JoinMBB);
2490 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
2493 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
2494 .addReg(TrueReg).addMBB(StartMBB)
2495 .addReg(FalseReg).addMBB(FalseMBB);
2497 MI->eraseFromParent();
2501 // Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
2502 // StoreOpcode is the store to use and Invert says whether the store should
2503 // happen when the condition is false rather than true. If a STORE ON
2504 // CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
2506 SystemZTargetLowering::emitCondStore(MachineInstr *MI,
2507 MachineBasicBlock *MBB,
2508 unsigned StoreOpcode, unsigned STOCOpcode,
2509 bool Invert) const {
2510 const SystemZInstrInfo *TII = TM.getInstrInfo();
2512 unsigned SrcReg = MI->getOperand(0).getReg();
2513 MachineOperand Base = MI->getOperand(1);
2514 int64_t Disp = MI->getOperand(2).getImm();
2515 unsigned IndexReg = MI->getOperand(3).getReg();
2516 unsigned CCValid = MI->getOperand(4).getImm();
2517 unsigned CCMask = MI->getOperand(5).getImm();
2518 DebugLoc DL = MI->getDebugLoc();
2520 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
2522 // Use STOCOpcode if possible. We could use different store patterns in
2523 // order to avoid matching the index register, but the performance trade-offs
2524 // might be more complicated in that case.
2525 if (STOCOpcode && !IndexReg && TM.getSubtargetImpl()->hasLoadStoreOnCond()) {
2528 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
2529 .addReg(SrcReg).addOperand(Base).addImm(Disp)
2530 .addImm(CCValid).addImm(CCMask);
2531 MI->eraseFromParent();
2535 // Get the condition needed to branch around the store.
2539 MachineBasicBlock *StartMBB = MBB;
2540 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
2541 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
2544 // BRC CCMask, JoinMBB
2545 // # fallthrough to FalseMBB
2547 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2548 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
2549 MBB->addSuccessor(JoinMBB);
2550 MBB->addSuccessor(FalseMBB);
2553 // store %SrcReg, %Disp(%Index,%Base)
2554 // # fallthrough to JoinMBB
2556 BuildMI(MBB, DL, TII->get(StoreOpcode))
2557 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
2558 MBB->addSuccessor(JoinMBB);
2560 MI->eraseFromParent();
2564 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
2565 // or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
2566 // performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
2567 // BitSize is the width of the field in bits, or 0 if this is a partword
2568 // ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
2569 // is one of the operands. Invert says whether the field should be
2570 // inverted after performing BinOpcode (e.g. for NAND).
2572 SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI,
2573 MachineBasicBlock *MBB,
2576 bool Invert) const {
2577 const SystemZInstrInfo *TII = TM.getInstrInfo();
2578 MachineFunction &MF = *MBB->getParent();
2579 MachineRegisterInfo &MRI = MF.getRegInfo();
2580 bool IsSubWord = (BitSize < 32);
2582 // Extract the operands. Base can be a register or a frame index.
2583 // Src2 can be a register or immediate.
2584 unsigned Dest = MI->getOperand(0).getReg();
2585 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2586 int64_t Disp = MI->getOperand(2).getImm();
2587 MachineOperand Src2 = earlyUseOperand(MI->getOperand(3));
2588 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
2589 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
2590 DebugLoc DL = MI->getDebugLoc();
2592 BitSize = MI->getOperand(6).getImm();
2594 // Subword operations use 32-bit registers.
2595 const TargetRegisterClass *RC = (BitSize <= 32 ?
2596 &SystemZ::GR32BitRegClass :
2597 &SystemZ::GR64BitRegClass);
2598 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
2599 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
2601 // Get the right opcodes for the displacement.
2602 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
2603 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
2604 assert(LOpcode && CSOpcode && "Displacement out of range");
2606 // Create virtual registers for temporary results.
2607 unsigned OrigVal = MRI.createVirtualRegister(RC);
2608 unsigned OldVal = MRI.createVirtualRegister(RC);
2609 unsigned NewVal = (BinOpcode || IsSubWord ?
2610 MRI.createVirtualRegister(RC) : Src2.getReg());
2611 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
2612 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
2614 // Insert a basic block for the main loop.
2615 MachineBasicBlock *StartMBB = MBB;
2616 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
2617 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2621 // %OrigVal = L Disp(%Base)
2622 // # fall through to LoopMMB
2624 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
2625 .addOperand(Base).addImm(Disp).addReg(0);
2626 MBB->addSuccessor(LoopMBB);
2629 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
2630 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
2631 // %RotatedNewVal = OP %RotatedOldVal, %Src2
2632 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
2633 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
2635 // # fall through to DoneMMB
2637 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2638 .addReg(OrigVal).addMBB(StartMBB)
2639 .addReg(Dest).addMBB(LoopMBB);
2641 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
2642 .addReg(OldVal).addReg(BitShift).addImm(0);
2644 // Perform the operation normally and then invert every bit of the field.
2645 unsigned Tmp = MRI.createVirtualRegister(RC);
2646 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
2647 .addReg(RotatedOldVal).addOperand(Src2);
2649 // XILF with the upper BitSize bits set.
2650 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
2651 .addReg(Tmp).addImm(uint32_t(~0 << (32 - BitSize)));
2652 else if (BitSize == 32)
2653 // XILF with every bit set.
2654 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
2655 .addReg(Tmp).addImm(~uint32_t(0));
2657 // Use LCGR and add -1 to the result, which is more compact than
2658 // an XILF, XILH pair.
2659 unsigned Tmp2 = MRI.createVirtualRegister(RC);
2660 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
2661 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
2662 .addReg(Tmp2).addImm(-1);
2664 } else if (BinOpcode)
2665 // A simply binary operation.
2666 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
2667 .addReg(RotatedOldVal).addOperand(Src2);
2669 // Use RISBG to rotate Src2 into position and use it to replace the
2670 // field in RotatedOldVal.
2671 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
2672 .addReg(RotatedOldVal).addReg(Src2.getReg())
2673 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
2675 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
2676 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
2677 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
2678 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
2679 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2680 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
2681 MBB->addSuccessor(LoopMBB);
2682 MBB->addSuccessor(DoneMBB);
2684 MI->eraseFromParent();
2688 // Implement EmitInstrWithCustomInserter for pseudo
2689 // ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
2690 // instruction that should be used to compare the current field with the
2691 // minimum or maximum value. KeepOldMask is the BRC condition-code mask
2692 // for when the current field should be kept. BitSize is the width of
2693 // the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
2695 SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI,
2696 MachineBasicBlock *MBB,
2697 unsigned CompareOpcode,
2698 unsigned KeepOldMask,
2699 unsigned BitSize) const {
2700 const SystemZInstrInfo *TII = TM.getInstrInfo();
2701 MachineFunction &MF = *MBB->getParent();
2702 MachineRegisterInfo &MRI = MF.getRegInfo();
2703 bool IsSubWord = (BitSize < 32);
2705 // Extract the operands. Base can be a register or a frame index.
2706 unsigned Dest = MI->getOperand(0).getReg();
2707 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2708 int64_t Disp = MI->getOperand(2).getImm();
2709 unsigned Src2 = MI->getOperand(3).getReg();
2710 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
2711 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
2712 DebugLoc DL = MI->getDebugLoc();
2714 BitSize = MI->getOperand(6).getImm();
2716 // Subword operations use 32-bit registers.
2717 const TargetRegisterClass *RC = (BitSize <= 32 ?
2718 &SystemZ::GR32BitRegClass :
2719 &SystemZ::GR64BitRegClass);
2720 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
2721 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
2723 // Get the right opcodes for the displacement.
2724 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
2725 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
2726 assert(LOpcode && CSOpcode && "Displacement out of range");
2728 // Create virtual registers for temporary results.
2729 unsigned OrigVal = MRI.createVirtualRegister(RC);
2730 unsigned OldVal = MRI.createVirtualRegister(RC);
2731 unsigned NewVal = MRI.createVirtualRegister(RC);
2732 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
2733 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
2734 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
2736 // Insert 3 basic blocks for the loop.
2737 MachineBasicBlock *StartMBB = MBB;
2738 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
2739 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2740 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
2741 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
2745 // %OrigVal = L Disp(%Base)
2746 // # fall through to LoopMMB
2748 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
2749 .addOperand(Base).addImm(Disp).addReg(0);
2750 MBB->addSuccessor(LoopMBB);
2753 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
2754 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
2755 // CompareOpcode %RotatedOldVal, %Src2
2756 // BRC KeepOldMask, UpdateMBB
2758 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2759 .addReg(OrigVal).addMBB(StartMBB)
2760 .addReg(Dest).addMBB(UpdateMBB);
2762 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
2763 .addReg(OldVal).addReg(BitShift).addImm(0);
2764 BuildMI(MBB, DL, TII->get(CompareOpcode))
2765 .addReg(RotatedOldVal).addReg(Src2);
2766 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2767 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
2768 MBB->addSuccessor(UpdateMBB);
2769 MBB->addSuccessor(UseAltMBB);
2772 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
2773 // # fall through to UpdateMMB
2776 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
2777 .addReg(RotatedOldVal).addReg(Src2)
2778 .addImm(32).addImm(31 + BitSize).addImm(0);
2779 MBB->addSuccessor(UpdateMBB);
2782 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
2783 // [ %RotatedAltVal, UseAltMBB ]
2784 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
2785 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
2787 // # fall through to DoneMMB
2789 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
2790 .addReg(RotatedOldVal).addMBB(LoopMBB)
2791 .addReg(RotatedAltVal).addMBB(UseAltMBB);
2793 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
2794 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
2795 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
2796 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
2797 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2798 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
2799 MBB->addSuccessor(LoopMBB);
2800 MBB->addSuccessor(DoneMBB);
2802 MI->eraseFromParent();
2806 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
2809 SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI,
2810 MachineBasicBlock *MBB) const {
2811 const SystemZInstrInfo *TII = TM.getInstrInfo();
2812 MachineFunction &MF = *MBB->getParent();
2813 MachineRegisterInfo &MRI = MF.getRegInfo();
2815 // Extract the operands. Base can be a register or a frame index.
2816 unsigned Dest = MI->getOperand(0).getReg();
2817 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2818 int64_t Disp = MI->getOperand(2).getImm();
2819 unsigned OrigCmpVal = MI->getOperand(3).getReg();
2820 unsigned OrigSwapVal = MI->getOperand(4).getReg();
2821 unsigned BitShift = MI->getOperand(5).getReg();
2822 unsigned NegBitShift = MI->getOperand(6).getReg();
2823 int64_t BitSize = MI->getOperand(7).getImm();
2824 DebugLoc DL = MI->getDebugLoc();
2826 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
2828 // Get the right opcodes for the displacement.
2829 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
2830 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
2831 assert(LOpcode && CSOpcode && "Displacement out of range");
2833 // Create virtual registers for temporary results.
2834 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
2835 unsigned OldVal = MRI.createVirtualRegister(RC);
2836 unsigned CmpVal = MRI.createVirtualRegister(RC);
2837 unsigned SwapVal = MRI.createVirtualRegister(RC);
2838 unsigned StoreVal = MRI.createVirtualRegister(RC);
2839 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
2840 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
2841 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
2843 // Insert 2 basic blocks for the loop.
2844 MachineBasicBlock *StartMBB = MBB;
2845 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
2846 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2847 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
2851 // %OrigOldVal = L Disp(%Base)
2852 // # fall through to LoopMMB
2854 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
2855 .addOperand(Base).addImm(Disp).addReg(0);
2856 MBB->addSuccessor(LoopMBB);
2859 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
2860 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
2861 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
2862 // %Dest = RLL %OldVal, BitSize(%BitShift)
2863 // ^^ The low BitSize bits contain the field
2865 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
2866 // ^^ Replace the upper 32-BitSize bits of the
2867 // comparison value with those that we loaded,
2868 // so that we can use a full word comparison.
2869 // CR %Dest, %RetryCmpVal
2871 // # Fall through to SetMBB
2873 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2874 .addReg(OrigOldVal).addMBB(StartMBB)
2875 .addReg(RetryOldVal).addMBB(SetMBB);
2876 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
2877 .addReg(OrigCmpVal).addMBB(StartMBB)
2878 .addReg(RetryCmpVal).addMBB(SetMBB);
2879 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
2880 .addReg(OrigSwapVal).addMBB(StartMBB)
2881 .addReg(RetrySwapVal).addMBB(SetMBB);
2882 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
2883 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
2884 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
2885 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
2886 BuildMI(MBB, DL, TII->get(SystemZ::CR))
2887 .addReg(Dest).addReg(RetryCmpVal);
2888 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2889 .addImm(SystemZ::CCMASK_ICMP)
2890 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
2891 MBB->addSuccessor(DoneMBB);
2892 MBB->addSuccessor(SetMBB);
2895 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
2896 // ^^ Replace the upper 32-BitSize bits of the new
2897 // value with those that we loaded.
2898 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
2899 // ^^ Rotate the new field to its proper position.
2900 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
2902 // # fall through to ExitMMB
2904 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
2905 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
2906 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
2907 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
2908 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
2909 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
2910 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2911 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
2912 MBB->addSuccessor(LoopMBB);
2913 MBB->addSuccessor(DoneMBB);
2915 MI->eraseFromParent();
2919 // Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true
2920 // if the high register of the GR128 value must be cleared or false if
2921 // it's "don't care". SubReg is subreg_l32 when extending a GR32
2922 // and subreg_l64 when extending a GR64.
2924 SystemZTargetLowering::emitExt128(MachineInstr *MI,
2925 MachineBasicBlock *MBB,
2926 bool ClearEven, unsigned SubReg) const {
2927 const SystemZInstrInfo *TII = TM.getInstrInfo();
2928 MachineFunction &MF = *MBB->getParent();
2929 MachineRegisterInfo &MRI = MF.getRegInfo();
2930 DebugLoc DL = MI->getDebugLoc();
2932 unsigned Dest = MI->getOperand(0).getReg();
2933 unsigned Src = MI->getOperand(1).getReg();
2934 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
2936 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
2938 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
2939 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
2941 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
2943 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
2944 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
2947 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
2948 .addReg(In128).addReg(Src).addImm(SubReg);
2950 MI->eraseFromParent();
2955 SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI,
2956 MachineBasicBlock *MBB,
2957 unsigned Opcode) const {
2958 const SystemZInstrInfo *TII = TM.getInstrInfo();
2959 MachineFunction &MF = *MBB->getParent();
2960 MachineRegisterInfo &MRI = MF.getRegInfo();
2961 DebugLoc DL = MI->getDebugLoc();
2963 MachineOperand DestBase = earlyUseOperand(MI->getOperand(0));
2964 uint64_t DestDisp = MI->getOperand(1).getImm();
2965 MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2));
2966 uint64_t SrcDisp = MI->getOperand(3).getImm();
2967 uint64_t Length = MI->getOperand(4).getImm();
2969 // When generating more than one CLC, all but the last will need to
2970 // branch to the end when a difference is found.
2971 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
2972 splitBlockAfter(MI, MBB) : 0);
2974 // Check for the loop form, in which operand 5 is the trip count.
2975 if (MI->getNumExplicitOperands() > 5) {
2976 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
2978 uint64_t StartCountReg = MI->getOperand(5).getReg();
2979 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
2980 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
2981 forceReg(MI, DestBase, TII));
2983 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
2984 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
2985 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
2986 MRI.createVirtualRegister(RC));
2987 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
2988 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
2989 MRI.createVirtualRegister(RC));
2991 RC = &SystemZ::GR64BitRegClass;
2992 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
2993 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
2995 MachineBasicBlock *StartMBB = MBB;
2996 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
2997 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2998 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
3001 // # fall through to LoopMMB
3002 MBB->addSuccessor(LoopMBB);
3005 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
3006 // [ %NextDestReg, NextMBB ]
3007 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
3008 // [ %NextSrcReg, NextMBB ]
3009 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
3010 // [ %NextCountReg, NextMBB ]
3011 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
3012 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
3015 // The prefetch is used only for MVC. The JLH is used only for CLC.
3018 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
3019 .addReg(StartDestReg).addMBB(StartMBB)
3020 .addReg(NextDestReg).addMBB(NextMBB);
3021 if (!HaveSingleBase)
3022 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
3023 .addReg(StartSrcReg).addMBB(StartMBB)
3024 .addReg(NextSrcReg).addMBB(NextMBB);
3025 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
3026 .addReg(StartCountReg).addMBB(StartMBB)
3027 .addReg(NextCountReg).addMBB(NextMBB);
3028 if (Opcode == SystemZ::MVC)
3029 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
3030 .addImm(SystemZ::PFD_WRITE)
3031 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
3032 BuildMI(MBB, DL, TII->get(Opcode))
3033 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
3034 .addReg(ThisSrcReg).addImm(SrcDisp);
3036 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3037 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
3039 MBB->addSuccessor(EndMBB);
3040 MBB->addSuccessor(NextMBB);
3044 // %NextDestReg = LA 256(%ThisDestReg)
3045 // %NextSrcReg = LA 256(%ThisSrcReg)
3046 // %NextCountReg = AGHI %ThisCountReg, -1
3047 // CGHI %NextCountReg, 0
3049 // # fall through to DoneMMB
3051 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
3054 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
3055 .addReg(ThisDestReg).addImm(256).addReg(0);
3056 if (!HaveSingleBase)
3057 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
3058 .addReg(ThisSrcReg).addImm(256).addReg(0);
3059 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
3060 .addReg(ThisCountReg).addImm(-1);
3061 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
3062 .addReg(NextCountReg).addImm(0);
3063 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3064 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
3066 MBB->addSuccessor(LoopMBB);
3067 MBB->addSuccessor(DoneMBB);
3069 DestBase = MachineOperand::CreateReg(NextDestReg, false);
3070 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
3074 // Handle any remaining bytes with straight-line code.
3075 while (Length > 0) {
3076 uint64_t ThisLength = std::min(Length, uint64_t(256));
3077 // The previous iteration might have created out-of-range displacements.
3078 // Apply them using LAY if so.
3079 if (!isUInt<12>(DestDisp)) {
3080 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
3081 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
3082 .addOperand(DestBase).addImm(DestDisp).addReg(0);
3083 DestBase = MachineOperand::CreateReg(Reg, false);
3086 if (!isUInt<12>(SrcDisp)) {
3087 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
3088 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
3089 .addOperand(SrcBase).addImm(SrcDisp).addReg(0);
3090 SrcBase = MachineOperand::CreateReg(Reg, false);
3093 BuildMI(*MBB, MI, DL, TII->get(Opcode))
3094 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength)
3095 .addOperand(SrcBase).addImm(SrcDisp);
3096 DestDisp += ThisLength;
3097 SrcDisp += ThisLength;
3098 Length -= ThisLength;
3099 // If there's another CLC to go, branch to the end if a difference
3101 if (EndMBB && Length > 0) {
3102 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
3103 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3104 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
3106 MBB->addSuccessor(EndMBB);
3107 MBB->addSuccessor(NextMBB);
3112 MBB->addSuccessor(EndMBB);
3114 MBB->addLiveIn(SystemZ::CC);
3117 MI->eraseFromParent();
3121 // Decompose string pseudo-instruction MI into a loop that continually performs
3122 // Opcode until CC != 3.
3124 SystemZTargetLowering::emitStringWrapper(MachineInstr *MI,
3125 MachineBasicBlock *MBB,
3126 unsigned Opcode) const {
3127 const SystemZInstrInfo *TII = TM.getInstrInfo();
3128 MachineFunction &MF = *MBB->getParent();
3129 MachineRegisterInfo &MRI = MF.getRegInfo();
3130 DebugLoc DL = MI->getDebugLoc();
3132 uint64_t End1Reg = MI->getOperand(0).getReg();
3133 uint64_t Start1Reg = MI->getOperand(1).getReg();
3134 uint64_t Start2Reg = MI->getOperand(2).getReg();
3135 uint64_t CharReg = MI->getOperand(3).getReg();
3137 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
3138 uint64_t This1Reg = MRI.createVirtualRegister(RC);
3139 uint64_t This2Reg = MRI.createVirtualRegister(RC);
3140 uint64_t End2Reg = MRI.createVirtualRegister(RC);
3142 MachineBasicBlock *StartMBB = MBB;
3143 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
3144 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
3147 // # fall through to LoopMMB
3148 MBB->addSuccessor(LoopMBB);
3151 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
3152 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
3154 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
3156 // # fall through to DoneMMB
3158 // The load of R0L can be hoisted by post-RA LICM.
3161 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
3162 .addReg(Start1Reg).addMBB(StartMBB)
3163 .addReg(End1Reg).addMBB(LoopMBB);
3164 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
3165 .addReg(Start2Reg).addMBB(StartMBB)
3166 .addReg(End2Reg).addMBB(LoopMBB);
3167 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
3168 BuildMI(MBB, DL, TII->get(Opcode))
3169 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
3170 .addReg(This1Reg).addReg(This2Reg);
3171 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3172 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
3173 MBB->addSuccessor(LoopMBB);
3174 MBB->addSuccessor(DoneMBB);
3176 DoneMBB->addLiveIn(SystemZ::CC);
3178 MI->eraseFromParent();
3182 MachineBasicBlock *SystemZTargetLowering::
3183 EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const {
3184 switch (MI->getOpcode()) {
3185 case SystemZ::Select32Mux:
3186 case SystemZ::Select32:
3187 case SystemZ::SelectF32:
3188 case SystemZ::Select64:
3189 case SystemZ::SelectF64:
3190 case SystemZ::SelectF128:
3191 return emitSelect(MI, MBB);
3193 case SystemZ::CondStore8Mux:
3194 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
3195 case SystemZ::CondStore8MuxInv:
3196 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
3197 case SystemZ::CondStore16Mux:
3198 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
3199 case SystemZ::CondStore16MuxInv:
3200 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
3201 case SystemZ::CondStore8:
3202 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
3203 case SystemZ::CondStore8Inv:
3204 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
3205 case SystemZ::CondStore16:
3206 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
3207 case SystemZ::CondStore16Inv:
3208 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
3209 case SystemZ::CondStore32:
3210 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
3211 case SystemZ::CondStore32Inv:
3212 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
3213 case SystemZ::CondStore64:
3214 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
3215 case SystemZ::CondStore64Inv:
3216 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
3217 case SystemZ::CondStoreF32:
3218 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
3219 case SystemZ::CondStoreF32Inv:
3220 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
3221 case SystemZ::CondStoreF64:
3222 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
3223 case SystemZ::CondStoreF64Inv:
3224 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
3226 case SystemZ::AEXT128_64:
3227 return emitExt128(MI, MBB, false, SystemZ::subreg_l64);
3228 case SystemZ::ZEXT128_32:
3229 return emitExt128(MI, MBB, true, SystemZ::subreg_l32);
3230 case SystemZ::ZEXT128_64:
3231 return emitExt128(MI, MBB, true, SystemZ::subreg_l64);
3233 case SystemZ::ATOMIC_SWAPW:
3234 return emitAtomicLoadBinary(MI, MBB, 0, 0);
3235 case SystemZ::ATOMIC_SWAP_32:
3236 return emitAtomicLoadBinary(MI, MBB, 0, 32);
3237 case SystemZ::ATOMIC_SWAP_64:
3238 return emitAtomicLoadBinary(MI, MBB, 0, 64);
3240 case SystemZ::ATOMIC_LOADW_AR:
3241 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
3242 case SystemZ::ATOMIC_LOADW_AFI:
3243 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
3244 case SystemZ::ATOMIC_LOAD_AR:
3245 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
3246 case SystemZ::ATOMIC_LOAD_AHI:
3247 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
3248 case SystemZ::ATOMIC_LOAD_AFI:
3249 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
3250 case SystemZ::ATOMIC_LOAD_AGR:
3251 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
3252 case SystemZ::ATOMIC_LOAD_AGHI:
3253 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
3254 case SystemZ::ATOMIC_LOAD_AGFI:
3255 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
3257 case SystemZ::ATOMIC_LOADW_SR:
3258 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
3259 case SystemZ::ATOMIC_LOAD_SR:
3260 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
3261 case SystemZ::ATOMIC_LOAD_SGR:
3262 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
3264 case SystemZ::ATOMIC_LOADW_NR:
3265 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
3266 case SystemZ::ATOMIC_LOADW_NILH:
3267 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
3268 case SystemZ::ATOMIC_LOAD_NR:
3269 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
3270 case SystemZ::ATOMIC_LOAD_NILL:
3271 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
3272 case SystemZ::ATOMIC_LOAD_NILH:
3273 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
3274 case SystemZ::ATOMIC_LOAD_NILF:
3275 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
3276 case SystemZ::ATOMIC_LOAD_NGR:
3277 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
3278 case SystemZ::ATOMIC_LOAD_NILL64:
3279 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
3280 case SystemZ::ATOMIC_LOAD_NILH64:
3281 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
3282 case SystemZ::ATOMIC_LOAD_NIHL64:
3283 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
3284 case SystemZ::ATOMIC_LOAD_NIHH64:
3285 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
3286 case SystemZ::ATOMIC_LOAD_NILF64:
3287 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
3288 case SystemZ::ATOMIC_LOAD_NIHF64:
3289 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
3291 case SystemZ::ATOMIC_LOADW_OR:
3292 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
3293 case SystemZ::ATOMIC_LOADW_OILH:
3294 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
3295 case SystemZ::ATOMIC_LOAD_OR:
3296 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
3297 case SystemZ::ATOMIC_LOAD_OILL:
3298 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
3299 case SystemZ::ATOMIC_LOAD_OILH:
3300 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
3301 case SystemZ::ATOMIC_LOAD_OILF:
3302 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
3303 case SystemZ::ATOMIC_LOAD_OGR:
3304 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
3305 case SystemZ::ATOMIC_LOAD_OILL64:
3306 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
3307 case SystemZ::ATOMIC_LOAD_OILH64:
3308 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
3309 case SystemZ::ATOMIC_LOAD_OIHL64:
3310 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
3311 case SystemZ::ATOMIC_LOAD_OIHH64:
3312 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
3313 case SystemZ::ATOMIC_LOAD_OILF64:
3314 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
3315 case SystemZ::ATOMIC_LOAD_OIHF64:
3316 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
3318 case SystemZ::ATOMIC_LOADW_XR:
3319 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
3320 case SystemZ::ATOMIC_LOADW_XILF:
3321 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
3322 case SystemZ::ATOMIC_LOAD_XR:
3323 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
3324 case SystemZ::ATOMIC_LOAD_XILF:
3325 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
3326 case SystemZ::ATOMIC_LOAD_XGR:
3327 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
3328 case SystemZ::ATOMIC_LOAD_XILF64:
3329 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
3330 case SystemZ::ATOMIC_LOAD_XIHF64:
3331 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
3333 case SystemZ::ATOMIC_LOADW_NRi:
3334 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
3335 case SystemZ::ATOMIC_LOADW_NILHi:
3336 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
3337 case SystemZ::ATOMIC_LOAD_NRi:
3338 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
3339 case SystemZ::ATOMIC_LOAD_NILLi:
3340 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
3341 case SystemZ::ATOMIC_LOAD_NILHi:
3342 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
3343 case SystemZ::ATOMIC_LOAD_NILFi:
3344 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
3345 case SystemZ::ATOMIC_LOAD_NGRi:
3346 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
3347 case SystemZ::ATOMIC_LOAD_NILL64i:
3348 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
3349 case SystemZ::ATOMIC_LOAD_NILH64i:
3350 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
3351 case SystemZ::ATOMIC_LOAD_NIHL64i:
3352 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
3353 case SystemZ::ATOMIC_LOAD_NIHH64i:
3354 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
3355 case SystemZ::ATOMIC_LOAD_NILF64i:
3356 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
3357 case SystemZ::ATOMIC_LOAD_NIHF64i:
3358 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
3360 case SystemZ::ATOMIC_LOADW_MIN:
3361 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3362 SystemZ::CCMASK_CMP_LE, 0);
3363 case SystemZ::ATOMIC_LOAD_MIN_32:
3364 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3365 SystemZ::CCMASK_CMP_LE, 32);
3366 case SystemZ::ATOMIC_LOAD_MIN_64:
3367 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
3368 SystemZ::CCMASK_CMP_LE, 64);
3370 case SystemZ::ATOMIC_LOADW_MAX:
3371 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3372 SystemZ::CCMASK_CMP_GE, 0);
3373 case SystemZ::ATOMIC_LOAD_MAX_32:
3374 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3375 SystemZ::CCMASK_CMP_GE, 32);
3376 case SystemZ::ATOMIC_LOAD_MAX_64:
3377 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
3378 SystemZ::CCMASK_CMP_GE, 64);
3380 case SystemZ::ATOMIC_LOADW_UMIN:
3381 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3382 SystemZ::CCMASK_CMP_LE, 0);
3383 case SystemZ::ATOMIC_LOAD_UMIN_32:
3384 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3385 SystemZ::CCMASK_CMP_LE, 32);
3386 case SystemZ::ATOMIC_LOAD_UMIN_64:
3387 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
3388 SystemZ::CCMASK_CMP_LE, 64);
3390 case SystemZ::ATOMIC_LOADW_UMAX:
3391 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3392 SystemZ::CCMASK_CMP_GE, 0);
3393 case SystemZ::ATOMIC_LOAD_UMAX_32:
3394 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3395 SystemZ::CCMASK_CMP_GE, 32);
3396 case SystemZ::ATOMIC_LOAD_UMAX_64:
3397 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
3398 SystemZ::CCMASK_CMP_GE, 64);
3400 case SystemZ::ATOMIC_CMP_SWAPW:
3401 return emitAtomicCmpSwapW(MI, MBB);
3402 case SystemZ::MVCSequence:
3403 case SystemZ::MVCLoop:
3404 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
3405 case SystemZ::NCSequence:
3406 case SystemZ::NCLoop:
3407 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
3408 case SystemZ::OCSequence:
3409 case SystemZ::OCLoop:
3410 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
3411 case SystemZ::XCSequence:
3412 case SystemZ::XCLoop:
3413 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
3414 case SystemZ::CLCSequence:
3415 case SystemZ::CLCLoop:
3416 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
3417 case SystemZ::CLSTLoop:
3418 return emitStringWrapper(MI, MBB, SystemZ::CLST);
3419 case SystemZ::MVSTLoop:
3420 return emitStringWrapper(MI, MBB, SystemZ::MVST);
3421 case SystemZ::SRSTLoop:
3422 return emitStringWrapper(MI, MBB, SystemZ::SRST);
3424 llvm_unreachable("Unexpected instr type to insert");