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"
28 // Classify VT as either 32 or 64 bit.
29 static bool is32Bit(EVT VT) {
30 switch (VT.getSimpleVT().SimpleTy) {
36 llvm_unreachable("Unsupported type");
40 // Return a version of MachineOperand that can be safely used before the
42 static MachineOperand earlyUseOperand(MachineOperand Op) {
48 SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm)
49 : TargetLowering(tm, new TargetLoweringObjectFileELF()),
50 Subtarget(*tm.getSubtargetImpl()), TM(tm) {
51 MVT PtrVT = getPointerTy();
53 // Set up the register classes.
54 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
55 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
56 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
57 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
58 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
60 // Compute derived properties from the register classes
61 computeRegisterProperties();
63 // Set up special registers.
64 setExceptionPointerRegister(SystemZ::R6D);
65 setExceptionSelectorRegister(SystemZ::R7D);
66 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
68 // TODO: It may be better to default to latency-oriented scheduling, however
69 // LLVM's current latency-oriented scheduler can't handle physreg definitions
70 // such as SystemZ has with CC, so set this to the register-pressure
71 // scheduler, because it can.
72 setSchedulingPreference(Sched::RegPressure);
74 setBooleanContents(ZeroOrOneBooleanContent);
75 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
77 // Instructions are strings of 2-byte aligned 2-byte values.
78 setMinFunctionAlignment(2);
80 // Handle operations that are handled in a similar way for all types.
81 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
82 I <= MVT::LAST_FP_VALUETYPE;
84 MVT VT = MVT::SimpleValueType(I);
85 if (isTypeLegal(VT)) {
86 // Expand SETCC(X, Y, COND) into SELECT_CC(X, Y, 1, 0, COND).
87 setOperationAction(ISD::SETCC, VT, Expand);
89 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
90 setOperationAction(ISD::SELECT, VT, Expand);
92 // Lower SELECT_CC and BR_CC into separate comparisons and branches.
93 setOperationAction(ISD::SELECT_CC, VT, Custom);
94 setOperationAction(ISD::BR_CC, VT, Custom);
98 // Expand jump table branches as address arithmetic followed by an
100 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
102 // Expand BRCOND into a BR_CC (see above).
103 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
105 // Handle integer types.
106 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
107 I <= MVT::LAST_INTEGER_VALUETYPE;
109 MVT VT = MVT::SimpleValueType(I);
110 if (isTypeLegal(VT)) {
111 // Expand individual DIV and REMs into DIVREMs.
112 setOperationAction(ISD::SDIV, VT, Expand);
113 setOperationAction(ISD::UDIV, VT, Expand);
114 setOperationAction(ISD::SREM, VT, Expand);
115 setOperationAction(ISD::UREM, VT, Expand);
116 setOperationAction(ISD::SDIVREM, VT, Custom);
117 setOperationAction(ISD::UDIVREM, VT, Custom);
119 // Expand ATOMIC_LOAD and ATOMIC_STORE using ATOMIC_CMP_SWAP.
120 // FIXME: probably much too conservative.
121 setOperationAction(ISD::ATOMIC_LOAD, VT, Expand);
122 setOperationAction(ISD::ATOMIC_STORE, VT, Expand);
124 // No special instructions for these.
125 setOperationAction(ISD::CTPOP, VT, Expand);
126 setOperationAction(ISD::CTTZ, VT, Expand);
127 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
128 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
129 setOperationAction(ISD::ROTR, VT, Expand);
131 // Use *MUL_LOHI where possible instead of MULH*.
132 setOperationAction(ISD::MULHS, VT, Expand);
133 setOperationAction(ISD::MULHU, VT, Expand);
134 setOperationAction(ISD::SMUL_LOHI, VT, Custom);
135 setOperationAction(ISD::UMUL_LOHI, VT, Custom);
137 // We have instructions for signed but not unsigned FP conversion.
138 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
142 // Type legalization will convert 8- and 16-bit atomic operations into
143 // forms that operate on i32s (but still keeping the original memory VT).
144 // Lower them into full i32 operations.
145 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom);
146 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom);
147 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
148 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom);
149 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom);
150 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom);
151 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
152 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom);
153 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom);
154 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
155 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
156 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
158 // We have instructions for signed but not unsigned FP conversion.
159 // Handle unsigned 32-bit types as signed 64-bit types.
160 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
161 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
163 // We have native support for a 64-bit CTLZ, via FLOGR.
164 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
165 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
167 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
168 setOperationAction(ISD::OR, MVT::i64, Custom);
170 // FIXME: Can we support these natively?
171 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
172 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
173 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
175 // We have native instructions for i8, i16 and i32 extensions, but not i1.
176 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
177 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
178 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
179 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
181 // Handle the various types of symbolic address.
182 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
183 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
184 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
185 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
186 setOperationAction(ISD::JumpTable, PtrVT, Custom);
188 // We need to handle dynamic allocations specially because of the
189 // 160-byte area at the bottom of the stack.
190 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
192 // Use custom expanders so that we can force the function to use
194 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
195 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
197 // Handle prefetches with PFD or PFDRL.
198 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
200 // Handle floating-point types.
201 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
202 I <= MVT::LAST_FP_VALUETYPE;
204 MVT VT = MVT::SimpleValueType(I);
205 if (isTypeLegal(VT)) {
206 // We can use FI for FRINT.
207 setOperationAction(ISD::FRINT, VT, Legal);
209 // We can use the extended form of FI for other rounding operations.
210 if (Subtarget.hasFPExtension()) {
211 setOperationAction(ISD::FNEARBYINT, VT, Legal);
212 setOperationAction(ISD::FFLOOR, VT, Legal);
213 setOperationAction(ISD::FCEIL, VT, Legal);
214 setOperationAction(ISD::FTRUNC, VT, Legal);
215 setOperationAction(ISD::FROUND, VT, Legal);
218 // No special instructions for these.
219 setOperationAction(ISD::FSIN, VT, Expand);
220 setOperationAction(ISD::FCOS, VT, Expand);
221 setOperationAction(ISD::FREM, VT, Expand);
225 // We have fused multiply-addition for f32 and f64 but not f128.
226 setOperationAction(ISD::FMA, MVT::f32, Legal);
227 setOperationAction(ISD::FMA, MVT::f64, Legal);
228 setOperationAction(ISD::FMA, MVT::f128, Expand);
230 // Needed so that we don't try to implement f128 constant loads using
231 // a load-and-extend of a f80 constant (in cases where the constant
232 // would fit in an f80).
233 setLoadExtAction(ISD::EXTLOAD, MVT::f80, Expand);
235 // Floating-point truncation and stores need to be done separately.
236 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
237 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
238 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
240 // We have 64-bit FPR<->GPR moves, but need special handling for
242 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
243 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
245 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
246 // structure, but VAEND is a no-op.
247 setOperationAction(ISD::VASTART, MVT::Other, Custom);
248 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
249 setOperationAction(ISD::VAEND, MVT::Other, Expand);
251 // We want to use MVC in preference to even a single load/store pair.
252 MaxStoresPerMemcpy = 0;
253 MaxStoresPerMemcpyOptSize = 0;
255 // The main memset sequence is a byte store followed by an MVC.
256 // Two STC or MV..I stores win over that, but the kind of fused stores
257 // generated by target-independent code don't when the byte value is
258 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
259 // than "STC;MVC". Handle the choice in target-specific code instead.
260 MaxStoresPerMemset = 0;
261 MaxStoresPerMemsetOptSize = 0;
265 SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
266 VT = VT.getScalarType();
271 switch (VT.getSimpleVT().SimpleTy) {
284 bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
285 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
286 return Imm.isZero() || Imm.isNegZero();
289 bool SystemZTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
291 // Unaligned accesses should never be slower than the expanded version.
292 // We check specifically for aligned accesses in the few cases where
293 // they are required.
299 bool SystemZTargetLowering::isLegalAddressingMode(const AddrMode &AM,
301 // Punt on globals for now, although they can be used in limited
302 // RELATIVE LONG cases.
306 // Require a 20-bit signed offset.
307 if (!isInt<20>(AM.BaseOffs))
310 // Indexing is OK but no scale factor can be applied.
311 return AM.Scale == 0 || AM.Scale == 1;
314 bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
315 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
317 unsigned FromBits = FromType->getPrimitiveSizeInBits();
318 unsigned ToBits = ToType->getPrimitiveSizeInBits();
319 return FromBits > ToBits;
322 bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
323 if (!FromVT.isInteger() || !ToVT.isInteger())
325 unsigned FromBits = FromVT.getSizeInBits();
326 unsigned ToBits = ToVT.getSizeInBits();
327 return FromBits > ToBits;
330 //===----------------------------------------------------------------------===//
331 // Inline asm support
332 //===----------------------------------------------------------------------===//
334 TargetLowering::ConstraintType
335 SystemZTargetLowering::getConstraintType(const std::string &Constraint) const {
336 if (Constraint.size() == 1) {
337 switch (Constraint[0]) {
338 case 'a': // Address register
339 case 'd': // Data register (equivalent to 'r')
340 case 'f': // Floating-point register
341 case 'r': // General-purpose register
342 return C_RegisterClass;
344 case 'Q': // Memory with base and unsigned 12-bit displacement
345 case 'R': // Likewise, plus an index
346 case 'S': // Memory with base and signed 20-bit displacement
347 case 'T': // Likewise, plus an index
348 case 'm': // Equivalent to 'T'.
351 case 'I': // Unsigned 8-bit constant
352 case 'J': // Unsigned 12-bit constant
353 case 'K': // Signed 16-bit constant
354 case 'L': // Signed 20-bit displacement (on all targets we support)
355 case 'M': // 0x7fffffff
362 return TargetLowering::getConstraintType(Constraint);
365 TargetLowering::ConstraintWeight SystemZTargetLowering::
366 getSingleConstraintMatchWeight(AsmOperandInfo &info,
367 const char *constraint) const {
368 ConstraintWeight weight = CW_Invalid;
369 Value *CallOperandVal = info.CallOperandVal;
370 // If we don't have a value, we can't do a match,
371 // but allow it at the lowest weight.
372 if (CallOperandVal == NULL)
374 Type *type = CallOperandVal->getType();
375 // Look at the constraint type.
376 switch (*constraint) {
378 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
381 case 'a': // Address register
382 case 'd': // Data register (equivalent to 'r')
383 case 'r': // General-purpose register
384 if (CallOperandVal->getType()->isIntegerTy())
385 weight = CW_Register;
388 case 'f': // Floating-point register
389 if (type->isFloatingPointTy())
390 weight = CW_Register;
393 case 'I': // Unsigned 8-bit constant
394 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
395 if (isUInt<8>(C->getZExtValue()))
396 weight = CW_Constant;
399 case 'J': // Unsigned 12-bit constant
400 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
401 if (isUInt<12>(C->getZExtValue()))
402 weight = CW_Constant;
405 case 'K': // Signed 16-bit constant
406 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
407 if (isInt<16>(C->getSExtValue()))
408 weight = CW_Constant;
411 case 'L': // Signed 20-bit displacement (on all targets we support)
412 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
413 if (isInt<20>(C->getSExtValue()))
414 weight = CW_Constant;
417 case 'M': // 0x7fffffff
418 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
419 if (C->getZExtValue() == 0x7fffffff)
420 weight = CW_Constant;
426 // Parse a "{tNNN}" register constraint for which the register type "t"
427 // has already been verified. MC is the class associated with "t" and
428 // Map maps 0-based register numbers to LLVM register numbers.
429 static std::pair<unsigned, const TargetRegisterClass *>
430 parseRegisterNumber(const std::string &Constraint,
431 const TargetRegisterClass *RC, const unsigned *Map) {
432 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
433 if (isdigit(Constraint[2])) {
434 std::string Suffix(Constraint.data() + 2, Constraint.size() - 2);
435 unsigned Index = atoi(Suffix.c_str());
436 if (Index < 16 && Map[Index])
437 return std::make_pair(Map[Index], RC);
439 return std::make_pair(0u, static_cast<TargetRegisterClass*>(0));
442 std::pair<unsigned, const TargetRegisterClass *> SystemZTargetLowering::
443 getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const {
444 if (Constraint.size() == 1) {
445 // GCC Constraint Letters
446 switch (Constraint[0]) {
448 case 'd': // Data register (equivalent to 'r')
449 case 'r': // General-purpose register
451 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
452 else if (VT == MVT::i128)
453 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
454 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
456 case 'a': // Address register
458 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
459 else if (VT == MVT::i128)
460 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
461 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
463 case 'f': // Floating-point register
465 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
466 else if (VT == MVT::f128)
467 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
468 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
471 if (Constraint[0] == '{') {
472 // We need to override the default register parsing for GPRs and FPRs
473 // because the interpretation depends on VT. The internal names of
474 // the registers are also different from the external names
475 // (F0D and F0S instead of F0, etc.).
476 if (Constraint[1] == 'r') {
478 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
479 SystemZMC::GR32Regs);
481 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
482 SystemZMC::GR128Regs);
483 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
484 SystemZMC::GR64Regs);
486 if (Constraint[1] == 'f') {
488 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
489 SystemZMC::FP32Regs);
491 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
492 SystemZMC::FP128Regs);
493 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
494 SystemZMC::FP64Regs);
497 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
500 void SystemZTargetLowering::
501 LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
502 std::vector<SDValue> &Ops,
503 SelectionDAG &DAG) const {
504 // Only support length 1 constraints for now.
505 if (Constraint.length() == 1) {
506 switch (Constraint[0]) {
507 case 'I': // Unsigned 8-bit constant
508 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
509 if (isUInt<8>(C->getZExtValue()))
510 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
514 case 'J': // Unsigned 12-bit constant
515 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
516 if (isUInt<12>(C->getZExtValue()))
517 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
521 case 'K': // Signed 16-bit constant
522 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
523 if (isInt<16>(C->getSExtValue()))
524 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
528 case 'L': // Signed 20-bit displacement (on all targets we support)
529 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
530 if (isInt<20>(C->getSExtValue()))
531 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
535 case 'M': // 0x7fffffff
536 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
537 if (C->getZExtValue() == 0x7fffffff)
538 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
543 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
546 //===----------------------------------------------------------------------===//
547 // Calling conventions
548 //===----------------------------------------------------------------------===//
550 #include "SystemZGenCallingConv.inc"
552 bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
553 Type *ToType) const {
554 return isTruncateFree(FromType, ToType);
557 bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
558 if (!CI->isTailCall())
563 // Value is a value that has been passed to us in the location described by VA
564 // (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
565 // any loads onto Chain.
566 static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL,
567 CCValAssign &VA, SDValue Chain,
569 // If the argument has been promoted from a smaller type, insert an
570 // assertion to capture this.
571 if (VA.getLocInfo() == CCValAssign::SExt)
572 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
573 DAG.getValueType(VA.getValVT()));
574 else if (VA.getLocInfo() == CCValAssign::ZExt)
575 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
576 DAG.getValueType(VA.getValVT()));
579 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
580 else if (VA.getLocInfo() == CCValAssign::Indirect)
581 Value = DAG.getLoad(VA.getValVT(), DL, Chain, Value,
582 MachinePointerInfo(), false, false, false, 0);
584 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
588 // Value is a value of type VA.getValVT() that we need to copy into
589 // the location described by VA. Return a copy of Value converted to
590 // VA.getValVT(). The caller is responsible for handling indirect values.
591 static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL,
592 CCValAssign &VA, SDValue Value) {
593 switch (VA.getLocInfo()) {
594 case CCValAssign::SExt:
595 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
596 case CCValAssign::ZExt:
597 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
598 case CCValAssign::AExt:
599 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
600 case CCValAssign::Full:
603 llvm_unreachable("Unhandled getLocInfo()");
607 SDValue SystemZTargetLowering::
608 LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
609 const SmallVectorImpl<ISD::InputArg> &Ins,
610 SDLoc DL, SelectionDAG &DAG,
611 SmallVectorImpl<SDValue> &InVals) const {
612 MachineFunction &MF = DAG.getMachineFunction();
613 MachineFrameInfo *MFI = MF.getFrameInfo();
614 MachineRegisterInfo &MRI = MF.getRegInfo();
615 SystemZMachineFunctionInfo *FuncInfo =
616 MF.getInfo<SystemZMachineFunctionInfo>();
617 const SystemZFrameLowering *TFL =
618 static_cast<const SystemZFrameLowering *>(TM.getFrameLowering());
620 // Assign locations to all of the incoming arguments.
621 SmallVector<CCValAssign, 16> ArgLocs;
622 CCState CCInfo(CallConv, IsVarArg, MF, TM, ArgLocs, *DAG.getContext());
623 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
625 unsigned NumFixedGPRs = 0;
626 unsigned NumFixedFPRs = 0;
627 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
629 CCValAssign &VA = ArgLocs[I];
630 EVT LocVT = VA.getLocVT();
632 // Arguments passed in registers
633 const TargetRegisterClass *RC;
634 switch (LocVT.getSimpleVT().SimpleTy) {
636 // Integers smaller than i64 should be promoted to i64.
637 llvm_unreachable("Unexpected argument type");
640 RC = &SystemZ::GR32BitRegClass;
644 RC = &SystemZ::GR64BitRegClass;
648 RC = &SystemZ::FP32BitRegClass;
652 RC = &SystemZ::FP64BitRegClass;
656 unsigned VReg = MRI.createVirtualRegister(RC);
657 MRI.addLiveIn(VA.getLocReg(), VReg);
658 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
660 assert(VA.isMemLoc() && "Argument not register or memory");
662 // Create the frame index object for this incoming parameter.
663 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
664 VA.getLocMemOffset(), true);
666 // Create the SelectionDAG nodes corresponding to a load
667 // from this parameter. Unpromoted ints and floats are
668 // passed as right-justified 8-byte values.
669 EVT PtrVT = getPointerTy();
670 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
671 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
672 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, DAG.getIntPtrConstant(4));
673 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
674 MachinePointerInfo::getFixedStack(FI),
675 false, false, false, 0);
678 // Convert the value of the argument register into the value that's
680 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
684 // Save the number of non-varargs registers for later use by va_start, etc.
685 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
686 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
688 // Likewise the address (in the form of a frame index) of where the
689 // first stack vararg would be. The 1-byte size here is arbitrary.
690 int64_t StackSize = CCInfo.getNextStackOffset();
691 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true));
693 // ...and a similar frame index for the caller-allocated save area
694 // that will be used to store the incoming registers.
695 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
696 unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true);
697 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
699 // Store the FPR varargs in the reserved frame slots. (We store the
700 // GPRs as part of the prologue.)
701 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
702 SDValue MemOps[SystemZ::NumArgFPRs];
703 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
704 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
705 int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true);
706 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
707 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
708 &SystemZ::FP64BitRegClass);
709 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
710 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
711 MachinePointerInfo::getFixedStack(FI),
715 // Join the stores, which are independent of one another.
716 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
717 &MemOps[NumFixedFPRs],
718 SystemZ::NumArgFPRs - NumFixedFPRs);
725 static bool canUseSiblingCall(CCState ArgCCInfo,
726 SmallVectorImpl<CCValAssign> &ArgLocs) {
727 // Punt if there are any indirect or stack arguments, or if the call
728 // needs the call-saved argument register R6.
729 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
730 CCValAssign &VA = ArgLocs[I];
731 if (VA.getLocInfo() == CCValAssign::Indirect)
735 unsigned Reg = VA.getLocReg();
736 if (Reg == SystemZ::R6W || Reg == SystemZ::R6D)
743 SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
744 SmallVectorImpl<SDValue> &InVals) const {
745 SelectionDAG &DAG = CLI.DAG;
747 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
748 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
749 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
750 SDValue Chain = CLI.Chain;
751 SDValue Callee = CLI.Callee;
752 bool &IsTailCall = CLI.IsTailCall;
753 CallingConv::ID CallConv = CLI.CallConv;
754 bool IsVarArg = CLI.IsVarArg;
755 MachineFunction &MF = DAG.getMachineFunction();
756 EVT PtrVT = getPointerTy();
758 // Analyze the operands of the call, assigning locations to each operand.
759 SmallVector<CCValAssign, 16> ArgLocs;
760 CCState ArgCCInfo(CallConv, IsVarArg, MF, TM, ArgLocs, *DAG.getContext());
761 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
763 // We don't support GuaranteedTailCallOpt, only automatically-detected
765 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs))
768 // Get a count of how many bytes are to be pushed on the stack.
769 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
771 // Mark the start of the call.
773 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumBytes, PtrVT, true),
776 // Copy argument values to their designated locations.
777 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
778 SmallVector<SDValue, 8> MemOpChains;
780 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
781 CCValAssign &VA = ArgLocs[I];
782 SDValue ArgValue = OutVals[I];
784 if (VA.getLocInfo() == CCValAssign::Indirect) {
785 // Store the argument in a stack slot and pass its address.
786 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
787 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
788 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, SpillSlot,
789 MachinePointerInfo::getFixedStack(FI),
791 ArgValue = SpillSlot;
793 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
796 // Queue up the argument copies and emit them at the end.
797 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
799 assert(VA.isMemLoc() && "Argument not register or memory");
801 // Work out the address of the stack slot. Unpromoted ints and
802 // floats are passed as right-justified 8-byte values.
803 if (!StackPtr.getNode())
804 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
805 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
806 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
808 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
809 DAG.getIntPtrConstant(Offset));
812 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address,
813 MachinePointerInfo(),
818 // Join the stores, which are independent of one another.
819 if (!MemOpChains.empty())
820 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
821 &MemOpChains[0], MemOpChains.size());
823 // Accept direct calls by converting symbolic call addresses to the
824 // associated Target* opcodes. Force %r1 to be used for indirect
827 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
828 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
829 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
830 } else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
831 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
832 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
833 } else if (IsTailCall) {
834 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
835 Glue = Chain.getValue(1);
836 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
839 // Build a sequence of copy-to-reg nodes, chained and glued together.
840 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
841 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
842 RegsToPass[I].second, Glue);
843 Glue = Chain.getValue(1);
846 // The first call operand is the chain and the second is the target address.
847 SmallVector<SDValue, 8> Ops;
848 Ops.push_back(Chain);
849 Ops.push_back(Callee);
851 // Add argument registers to the end of the list so that they are
852 // known live into the call.
853 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
854 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
855 RegsToPass[I].second.getValueType()));
857 // Glue the call to the argument copies, if any.
862 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
864 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, &Ops[0], Ops.size());
865 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, &Ops[0], Ops.size());
866 Glue = Chain.getValue(1);
868 // Mark the end of the call, which is glued to the call itself.
869 Chain = DAG.getCALLSEQ_END(Chain,
870 DAG.getConstant(NumBytes, PtrVT, true),
871 DAG.getConstant(0, PtrVT, true),
873 Glue = Chain.getValue(1);
875 // Assign locations to each value returned by this call.
876 SmallVector<CCValAssign, 16> RetLocs;
877 CCState RetCCInfo(CallConv, IsVarArg, MF, TM, RetLocs, *DAG.getContext());
878 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
880 // Copy all of the result registers out of their specified physreg.
881 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
882 CCValAssign &VA = RetLocs[I];
884 // Copy the value out, gluing the copy to the end of the call sequence.
885 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
886 VA.getLocVT(), Glue);
887 Chain = RetValue.getValue(1);
888 Glue = RetValue.getValue(2);
890 // Convert the value of the return register into the value that's
892 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
899 SystemZTargetLowering::LowerReturn(SDValue Chain,
900 CallingConv::ID CallConv, bool IsVarArg,
901 const SmallVectorImpl<ISD::OutputArg> &Outs,
902 const SmallVectorImpl<SDValue> &OutVals,
903 SDLoc DL, SelectionDAG &DAG) const {
904 MachineFunction &MF = DAG.getMachineFunction();
906 // Assign locations to each returned value.
907 SmallVector<CCValAssign, 16> RetLocs;
908 CCState RetCCInfo(CallConv, IsVarArg, MF, TM, RetLocs, *DAG.getContext());
909 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
911 // Quick exit for void returns
913 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
915 // Copy the result values into the output registers.
917 SmallVector<SDValue, 4> RetOps;
918 RetOps.push_back(Chain);
919 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
920 CCValAssign &VA = RetLocs[I];
921 SDValue RetValue = OutVals[I];
923 // Make the return register live on exit.
924 assert(VA.isRegLoc() && "Can only return in registers!");
926 // Promote the value as required.
927 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
929 // Chain and glue the copies together.
930 unsigned Reg = VA.getLocReg();
931 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
932 Glue = Chain.getValue(1);
933 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
936 // Update chain and glue.
939 RetOps.push_back(Glue);
941 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other,
942 RetOps.data(), RetOps.size());
945 // CC is a comparison that will be implemented using an integer or
946 // floating-point comparison. Return the condition code mask for
947 // a branch on true. In the integer case, CCMASK_CMP_UO is set for
948 // unsigned comparisons and clear for signed ones. In the floating-point
949 // case, CCMASK_CMP_UO has its normal mask meaning (unordered).
950 static unsigned CCMaskForCondCode(ISD::CondCode CC) {
952 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
953 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
954 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
958 llvm_unreachable("Invalid integer condition!");
967 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
968 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
973 // If a comparison described by IsUnsigned, CCMask, CmpOp0 and CmpOp1
974 // can be converted to a comparison against zero, adjust the operands
976 static void adjustZeroCmp(SelectionDAG &DAG, bool &IsUnsigned,
977 SDValue &CmpOp0, SDValue &CmpOp1,
982 ConstantSDNode *ConstOp1 = dyn_cast<ConstantSDNode>(CmpOp1.getNode());
986 int64_t Value = ConstOp1->getSExtValue();
987 if ((Value == -1 && CCMask == SystemZ::CCMASK_CMP_GT) ||
988 (Value == -1 && CCMask == SystemZ::CCMASK_CMP_LE) ||
989 (Value == 1 && CCMask == SystemZ::CCMASK_CMP_LT) ||
990 (Value == 1 && CCMask == SystemZ::CCMASK_CMP_GE)) {
991 CCMask ^= SystemZ::CCMASK_CMP_EQ;
992 CmpOp1 = DAG.getConstant(0, CmpOp1.getValueType());
996 // If a comparison described by IsUnsigned, CCMask, CmpOp0 and CmpOp1
997 // is suitable for CLI(Y), CHHSI or CLHHSI, adjust the operands as necessary.
998 static void adjustSubwordCmp(SelectionDAG &DAG, bool &IsUnsigned,
999 SDValue &CmpOp0, SDValue &CmpOp1,
1001 // For us to make any changes, it must a comparison between a single-use
1002 // load and a constant.
1003 if (!CmpOp0.hasOneUse() ||
1004 CmpOp0.getOpcode() != ISD::LOAD ||
1005 CmpOp1.getOpcode() != ISD::Constant)
1008 // We must have an 8- or 16-bit load.
1009 LoadSDNode *Load = cast<LoadSDNode>(CmpOp0);
1010 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1011 if (NumBits != 8 && NumBits != 16)
1014 // The load must be an extending one and the constant must be within the
1015 // range of the unextended value.
1016 ConstantSDNode *Constant = cast<ConstantSDNode>(CmpOp1);
1017 uint64_t Value = Constant->getZExtValue();
1018 uint64_t Mask = (1 << NumBits) - 1;
1019 if (Load->getExtensionType() == ISD::SEXTLOAD) {
1020 int64_t SignedValue = Constant->getSExtValue();
1021 if (uint64_t(SignedValue) + (1ULL << (NumBits - 1)) > Mask)
1023 // Unsigned comparison between two sign-extended values is equivalent
1024 // to unsigned comparison between two zero-extended values.
1027 else if (CCMask == SystemZ::CCMASK_CMP_EQ ||
1028 CCMask == SystemZ::CCMASK_CMP_NE)
1029 // Any choice of IsUnsigned is OK for equality comparisons.
1030 // We could use either CHHSI or CLHHSI for 16-bit comparisons,
1031 // but since we use CLHHSI for zero extensions, it seems better
1032 // to be consistent and do the same here.
1033 Value &= Mask, IsUnsigned = true;
1034 else if (NumBits == 8) {
1035 // Try to treat the comparison as unsigned, so that we can use CLI.
1036 // Adjust CCMask and Value as necessary.
1037 if (Value == 0 && CCMask == SystemZ::CCMASK_CMP_LT)
1038 // Test whether the high bit of the byte is set.
1039 Value = 127, CCMask = SystemZ::CCMASK_CMP_GT, IsUnsigned = true;
1040 else if (Value == 0 && CCMask == SystemZ::CCMASK_CMP_GE)
1041 // Test whether the high bit of the byte is clear.
1042 Value = 128, CCMask = SystemZ::CCMASK_CMP_LT, IsUnsigned = true;
1044 // No instruction exists for this combination.
1047 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1050 // Signed comparison between two zero-extended values is equivalent
1051 // to unsigned comparison.
1056 // Make sure that the first operand is an i32 of the right extension type.
1057 ISD::LoadExtType ExtType = IsUnsigned ? ISD::ZEXTLOAD : ISD::SEXTLOAD;
1058 if (CmpOp0.getValueType() != MVT::i32 ||
1059 Load->getExtensionType() != ExtType)
1060 CmpOp0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32,
1061 Load->getChain(), Load->getBasePtr(),
1062 Load->getPointerInfo(), Load->getMemoryVT(),
1063 Load->isVolatile(), Load->isNonTemporal(),
1064 Load->getAlignment());
1066 // Make sure that the second operand is an i32 with the right value.
1067 if (CmpOp1.getValueType() != MVT::i32 ||
1068 Value != Constant->getZExtValue())
1069 CmpOp1 = DAG.getConstant(Value, MVT::i32);
1072 // Return true if a comparison described by CCMask, CmpOp0 and CmpOp1
1073 // is an equality comparison that is better implemented using unsigned
1074 // rather than signed comparison instructions.
1075 static bool preferUnsignedComparison(SDValue CmpOp0, SDValue CmpOp1,
1077 // The test must be for equality or inequality.
1078 if (CCMask != SystemZ::CCMASK_CMP_EQ && CCMask != SystemZ::CCMASK_CMP_NE)
1081 if (CmpOp1.getOpcode() == ISD::Constant) {
1082 uint64_t Value = cast<ConstantSDNode>(CmpOp1)->getSExtValue();
1084 // If we're comparing with memory, prefer unsigned comparisons for
1085 // values that are in the unsigned 16-bit range but not the signed
1086 // 16-bit range. We want to use CLFHSI and CLGHSI.
1087 if (CmpOp0.hasOneUse() &&
1088 ISD::isNormalLoad(CmpOp0.getNode()) &&
1089 (Value >= 32768 && Value < 65536))
1092 // Use unsigned comparisons for values that are in the CLGFI range
1093 // but not in the CGFI range.
1094 if (CmpOp0.getValueType() == MVT::i64 && (Value >> 31) == 1)
1100 // Prefer CL for zero-extended loads.
1101 if (CmpOp1.getOpcode() == ISD::ZERO_EXTEND ||
1102 ISD::isZEXTLoad(CmpOp1.getNode()))
1105 // ...and for "in-register" zero extensions.
1106 if (CmpOp1.getOpcode() == ISD::AND && CmpOp1.getValueType() == MVT::i64) {
1107 SDValue Mask = CmpOp1.getOperand(1);
1108 if (Mask.getOpcode() == ISD::Constant &&
1109 cast<ConstantSDNode>(Mask)->getZExtValue() == 0xffffffff)
1116 // Return true if Op is either an unextended load, or a load with the
1117 // extension type given by IsUnsigned.
1118 static bool isNaturalMemoryOperand(SDValue Op, bool IsUnsigned) {
1119 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op.getNode());
1121 switch (Load->getExtensionType()) {
1122 case ISD::NON_EXTLOAD:
1135 // Return true if it is better to swap comparison operands Op0 and Op1.
1136 // IsUnsigned says whether an integer comparison is signed or unsigned.
1137 static bool shouldSwapCmpOperands(SDValue Op0, SDValue Op1,
1139 // Leave f128 comparisons alone, since they have no memory forms.
1140 if (Op0.getValueType() == MVT::f128)
1143 // Always keep a floating-point constant second, since comparisons with
1144 // zero can use LOAD TEST and comparisons with other constants make a
1145 // natural memory operand.
1146 if (isa<ConstantFPSDNode>(Op1))
1149 // Never swap comparisons with zero since there are many ways to optimize
1151 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
1152 if (COp1 && COp1->getZExtValue() == 0)
1155 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1156 // In that case we generally prefer the memory to be second.
1157 if ((isNaturalMemoryOperand(Op0, IsUnsigned) && Op0.hasOneUse()) &&
1158 !(isNaturalMemoryOperand(Op1, IsUnsigned) && Op1.hasOneUse())) {
1159 // The only exceptions are when the second operand is a constant and
1160 // we can use things like CHHSI.
1164 // The memory-immediate instructions require 16-bit unsigned integers.
1165 if (isUInt<16>(COp1->getZExtValue()))
1168 // There are no comparisons between integers and signed memory bytes.
1169 // The others require 16-bit signed integers.
1170 if (cast<LoadSDNode>(Op0.getNode())->getMemoryVT() == MVT::i8 ||
1171 isInt<16>(COp1->getSExtValue()))
1179 // See whether the comparison (Opcode CmpOp0, CmpOp1) can be implemented
1180 // as a TEST UNDER MASK instruction when the condition being tested is
1181 // as described by CCValid and CCMask. Update the arguments with the
1182 // TM version if so.
1183 static void adjustForTestUnderMask(unsigned &Opcode, SDValue &CmpOp0,
1184 SDValue &CmpOp1, unsigned &CCValid,
1186 // For now we just handle equality and inequality with zero.
1187 if (CCMask != SystemZ::CCMASK_CMP_EQ &&
1188 (CCMask ^ CCValid) != SystemZ::CCMASK_CMP_EQ)
1190 ConstantSDNode *ConstCmpOp1 = dyn_cast<ConstantSDNode>(CmpOp1);
1191 if (!ConstCmpOp1 || ConstCmpOp1->getZExtValue() != 0)
1194 // Check whether the nonconstant input is an AND with a constant mask.
1195 if (CmpOp0.getOpcode() != ISD::AND)
1197 SDValue AndOp0 = CmpOp0.getOperand(0);
1198 SDValue AndOp1 = CmpOp0.getOperand(1);
1199 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(AndOp1.getNode());
1203 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
1204 uint64_t MaskVal = Mask->getZExtValue();
1205 if (!SystemZ::isImmLL(MaskVal) && !SystemZ::isImmLH(MaskVal) &&
1206 !SystemZ::isImmHL(MaskVal) && !SystemZ::isImmHH(MaskVal))
1209 // Go ahead and make the change.
1210 Opcode = SystemZISD::TM;
1213 CCValid = SystemZ::CCMASK_TM;
1214 CCMask = (CCMask == SystemZ::CCMASK_CMP_EQ ?
1215 SystemZ::CCMASK_TM_ALL_0 :
1216 SystemZ::CCMASK_TM_ALL_0 ^ CCValid);
1219 // Return a target node that compares CmpOp0 with CmpOp1 and stores a
1220 // 2-bit result in CC. Set CCValid to the CCMASK_* of all possible
1221 // 2-bit results and CCMask to the subset of those results that are
1222 // associated with Cond.
1223 static SDValue emitCmp(SelectionDAG &DAG, SDLoc DL, SDValue CmpOp0,
1224 SDValue CmpOp1, ISD::CondCode Cond, unsigned &CCValid,
1226 bool IsUnsigned = false;
1227 CCMask = CCMaskForCondCode(Cond);
1228 if (CmpOp0.getValueType().isFloatingPoint())
1229 CCValid = SystemZ::CCMASK_FCMP;
1231 IsUnsigned = CCMask & SystemZ::CCMASK_CMP_UO;
1232 CCValid = SystemZ::CCMASK_ICMP;
1234 adjustZeroCmp(DAG, IsUnsigned, CmpOp0, CmpOp1, CCMask);
1235 adjustSubwordCmp(DAG, IsUnsigned, CmpOp0, CmpOp1, CCMask);
1236 if (preferUnsignedComparison(CmpOp0, CmpOp1, CCMask))
1240 if (shouldSwapCmpOperands(CmpOp0, CmpOp1, IsUnsigned)) {
1241 std::swap(CmpOp0, CmpOp1);
1242 CCMask = ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1243 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1244 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1245 (CCMask & SystemZ::CCMASK_CMP_UO));
1248 unsigned Opcode = (IsUnsigned ? SystemZISD::UCMP : SystemZISD::CMP);
1249 adjustForTestUnderMask(Opcode, CmpOp0, CmpOp1, CCValid, CCMask);
1250 return DAG.getNode(Opcode, DL, MVT::Glue, CmpOp0, CmpOp1);
1253 // Implement a 32-bit *MUL_LOHI operation by extending both operands to
1254 // 64 bits. Extend is the extension type to use. Store the high part
1255 // in Hi and the low part in Lo.
1256 static void lowerMUL_LOHI32(SelectionDAG &DAG, SDLoc DL,
1257 unsigned Extend, SDValue Op0, SDValue Op1,
1258 SDValue &Hi, SDValue &Lo) {
1259 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
1260 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
1261 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
1262 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul, DAG.getConstant(32, MVT::i64));
1263 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
1264 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
1267 // Lower a binary operation that produces two VT results, one in each
1268 // half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
1269 // Extend extends Op0 to a GR128, and Opcode performs the GR128 operation
1270 // on the extended Op0 and (unextended) Op1. Store the even register result
1271 // in Even and the odd register result in Odd.
1272 static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT,
1273 unsigned Extend, unsigned Opcode,
1274 SDValue Op0, SDValue Op1,
1275 SDValue &Even, SDValue &Odd) {
1276 SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0);
1277 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped,
1278 SDValue(In128, 0), Op1);
1279 bool Is32Bit = is32Bit(VT);
1280 SDValue SubReg0 = DAG.getTargetConstant(SystemZ::even128(Is32Bit), VT);
1281 SDValue SubReg1 = DAG.getTargetConstant(SystemZ::odd128(Is32Bit), VT);
1282 SDNode *Reg0 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
1283 VT, Result, SubReg0);
1284 SDNode *Reg1 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
1285 VT, Result, SubReg1);
1286 Even = SDValue(Reg0, 0);
1287 Odd = SDValue(Reg1, 0);
1290 SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
1291 SDValue Chain = Op.getOperand(0);
1292 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1293 SDValue CmpOp0 = Op.getOperand(2);
1294 SDValue CmpOp1 = Op.getOperand(3);
1295 SDValue Dest = Op.getOperand(4);
1298 unsigned CCValid, CCMask;
1299 SDValue Flags = emitCmp(DAG, DL, CmpOp0, CmpOp1, CC, CCValid, CCMask);
1300 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
1301 Chain, DAG.getConstant(CCValid, MVT::i32),
1302 DAG.getConstant(CCMask, MVT::i32), Dest, Flags);
1305 SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
1306 SelectionDAG &DAG) const {
1307 SDValue CmpOp0 = Op.getOperand(0);
1308 SDValue CmpOp1 = Op.getOperand(1);
1309 SDValue TrueOp = Op.getOperand(2);
1310 SDValue FalseOp = Op.getOperand(3);
1311 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1314 unsigned CCValid, CCMask;
1315 SDValue Flags = emitCmp(DAG, DL, CmpOp0, CmpOp1, CC, CCValid, CCMask);
1317 SmallVector<SDValue, 5> Ops;
1318 Ops.push_back(TrueOp);
1319 Ops.push_back(FalseOp);
1320 Ops.push_back(DAG.getConstant(CCValid, MVT::i32));
1321 Ops.push_back(DAG.getConstant(CCMask, MVT::i32));
1322 Ops.push_back(Flags);
1324 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
1325 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, &Ops[0], Ops.size());
1328 SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
1329 SelectionDAG &DAG) const {
1331 const GlobalValue *GV = Node->getGlobal();
1332 int64_t Offset = Node->getOffset();
1333 EVT PtrVT = getPointerTy();
1334 Reloc::Model RM = TM.getRelocationModel();
1335 CodeModel::Model CM = TM.getCodeModel();
1338 if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) {
1339 // Make sure that the offset is aligned to a halfword. If it isn't,
1340 // create an "anchor" at the previous 12-bit boundary.
1341 // FIXME check whether there is a better way of handling this.
1343 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT,
1344 Offset & ~uint64_t(0xfff));
1347 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Offset);
1350 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1352 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
1353 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1354 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
1355 MachinePointerInfo::getGOT(), false, false, false, 0);
1358 // If there was a non-zero offset that we didn't fold, create an explicit
1361 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
1362 DAG.getConstant(Offset, PtrVT));
1367 SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
1368 SelectionDAG &DAG) const {
1370 const GlobalValue *GV = Node->getGlobal();
1371 EVT PtrVT = getPointerTy();
1372 TLSModel::Model model = TM.getTLSModel(GV);
1374 if (model != TLSModel::LocalExec)
1375 llvm_unreachable("only local-exec TLS mode supported");
1377 // The high part of the thread pointer is in access register 0.
1378 SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
1379 DAG.getConstant(0, MVT::i32));
1380 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
1382 // The low part of the thread pointer is in access register 1.
1383 SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
1384 DAG.getConstant(1, MVT::i32));
1385 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
1387 // Merge them into a single 64-bit address.
1388 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
1389 DAG.getConstant(32, PtrVT));
1390 SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
1392 // Get the offset of GA from the thread pointer.
1393 SystemZConstantPoolValue *CPV =
1394 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
1396 // Force the offset into the constant pool and load it from there.
1397 SDValue CPAddr = DAG.getConstantPool(CPV, PtrVT, 8);
1398 SDValue Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
1399 CPAddr, MachinePointerInfo::getConstantPool(),
1400 false, false, false, 0);
1402 // Add the base and offset together.
1403 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
1406 SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
1407 SelectionDAG &DAG) const {
1409 const BlockAddress *BA = Node->getBlockAddress();
1410 int64_t Offset = Node->getOffset();
1411 EVT PtrVT = getPointerTy();
1413 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
1414 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1418 SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
1419 SelectionDAG &DAG) const {
1421 EVT PtrVT = getPointerTy();
1422 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1424 // Use LARL to load the address of the table.
1425 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1428 SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
1429 SelectionDAG &DAG) const {
1431 EVT PtrVT = getPointerTy();
1434 if (CP->isMachineConstantPoolEntry())
1435 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1436 CP->getAlignment());
1438 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1439 CP->getAlignment(), CP->getOffset());
1441 // Use LARL to load the address of the constant pool entry.
1442 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1445 SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
1446 SelectionDAG &DAG) const {
1448 SDValue In = Op.getOperand(0);
1449 EVT InVT = In.getValueType();
1450 EVT ResVT = Op.getValueType();
1452 SDValue SubReg32 = DAG.getTargetConstant(SystemZ::subreg_32bit, MVT::i64);
1453 SDValue Shift32 = DAG.getConstant(32, MVT::i64);
1454 if (InVT == MVT::i32 && ResVT == MVT::f32) {
1455 SDValue In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
1456 SDValue Shift = DAG.getNode(ISD::SHL, DL, MVT::i64, In64, Shift32);
1457 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, Shift);
1458 SDNode *Out = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
1459 MVT::f32, Out64, SubReg32);
1460 return SDValue(Out, 0);
1462 if (InVT == MVT::f32 && ResVT == MVT::i32) {
1463 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
1464 SDNode *In64 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
1465 MVT::f64, SDValue(U64, 0), In, SubReg32);
1466 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, SDValue(In64, 0));
1467 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64, Shift32);
1468 SDValue Out = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
1471 llvm_unreachable("Unexpected bitcast combination");
1474 SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
1475 SelectionDAG &DAG) const {
1476 MachineFunction &MF = DAG.getMachineFunction();
1477 SystemZMachineFunctionInfo *FuncInfo =
1478 MF.getInfo<SystemZMachineFunctionInfo>();
1479 EVT PtrVT = getPointerTy();
1481 SDValue Chain = Op.getOperand(0);
1482 SDValue Addr = Op.getOperand(1);
1483 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1486 // The initial values of each field.
1487 const unsigned NumFields = 4;
1488 SDValue Fields[NumFields] = {
1489 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), PtrVT),
1490 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), PtrVT),
1491 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
1492 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
1495 // Store each field into its respective slot.
1496 SDValue MemOps[NumFields];
1497 unsigned Offset = 0;
1498 for (unsigned I = 0; I < NumFields; ++I) {
1499 SDValue FieldAddr = Addr;
1501 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
1502 DAG.getIntPtrConstant(Offset));
1503 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
1504 MachinePointerInfo(SV, Offset),
1508 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps, NumFields);
1511 SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
1512 SelectionDAG &DAG) const {
1513 SDValue Chain = Op.getOperand(0);
1514 SDValue DstPtr = Op.getOperand(1);
1515 SDValue SrcPtr = Op.getOperand(2);
1516 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
1517 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
1520 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32),
1521 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
1522 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
1525 SDValue SystemZTargetLowering::
1526 lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
1527 SDValue Chain = Op.getOperand(0);
1528 SDValue Size = Op.getOperand(1);
1531 unsigned SPReg = getStackPointerRegisterToSaveRestore();
1533 // Get a reference to the stack pointer.
1534 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
1536 // Get the new stack pointer value.
1537 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, Size);
1539 // Copy the new stack pointer back.
1540 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
1542 // The allocated data lives above the 160 bytes allocated for the standard
1543 // frame, plus any outgoing stack arguments. We don't know how much that
1544 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
1545 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
1546 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
1548 SDValue Ops[2] = { Result, Chain };
1549 return DAG.getMergeValues(Ops, 2, DL);
1552 SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
1553 SelectionDAG &DAG) const {
1554 EVT VT = Op.getValueType();
1558 // Just do a normal 64-bit multiplication and extract the results.
1559 // We define this so that it can be used for constant division.
1560 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
1561 Op.getOperand(1), Ops[1], Ops[0]);
1563 // Do a full 128-bit multiplication based on UMUL_LOHI64:
1565 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
1567 // but using the fact that the upper halves are either all zeros
1570 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
1572 // and grouping the right terms together since they are quicker than the
1575 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
1576 SDValue C63 = DAG.getConstant(63, MVT::i64);
1577 SDValue LL = Op.getOperand(0);
1578 SDValue RL = Op.getOperand(1);
1579 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
1580 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
1581 // UMUL_LOHI64 returns the low result in the odd register and the high
1582 // result in the even register. SMUL_LOHI is defined to return the
1583 // low half first, so the results are in reverse order.
1584 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
1585 LL, RL, Ops[1], Ops[0]);
1586 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
1587 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
1588 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
1589 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
1591 return DAG.getMergeValues(Ops, 2, DL);
1594 SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
1595 SelectionDAG &DAG) const {
1596 EVT VT = Op.getValueType();
1600 // Just do a normal 64-bit multiplication and extract the results.
1601 // We define this so that it can be used for constant division.
1602 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
1603 Op.getOperand(1), Ops[1], Ops[0]);
1605 // UMUL_LOHI64 returns the low result in the odd register and the high
1606 // result in the even register. UMUL_LOHI is defined to return the
1607 // low half first, so the results are in reverse order.
1608 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
1609 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
1610 return DAG.getMergeValues(Ops, 2, DL);
1613 SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
1614 SelectionDAG &DAG) const {
1615 SDValue Op0 = Op.getOperand(0);
1616 SDValue Op1 = Op.getOperand(1);
1617 EVT VT = Op.getValueType();
1621 // We use DSGF for 32-bit division.
1623 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
1624 Opcode = SystemZISD::SDIVREM32;
1625 } else if (DAG.ComputeNumSignBits(Op1) > 32) {
1626 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
1627 Opcode = SystemZISD::SDIVREM32;
1629 Opcode = SystemZISD::SDIVREM64;
1631 // DSG(F) takes a 64-bit dividend, so the even register in the GR128
1632 // input is "don't care". The instruction returns the remainder in
1633 // the even register and the quotient in the odd register.
1635 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
1636 Op0, Op1, Ops[1], Ops[0]);
1637 return DAG.getMergeValues(Ops, 2, DL);
1640 SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
1641 SelectionDAG &DAG) const {
1642 EVT VT = Op.getValueType();
1645 // DL(G) uses a double-width dividend, so we need to clear the even
1646 // register in the GR128 input. The instruction returns the remainder
1647 // in the even register and the quotient in the odd register.
1650 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
1651 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
1653 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
1654 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
1655 return DAG.getMergeValues(Ops, 2, DL);
1658 SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
1659 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
1661 // Get the known-zero masks for each operand.
1662 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
1663 APInt KnownZero[2], KnownOne[2];
1664 DAG.ComputeMaskedBits(Ops[0], KnownZero[0], KnownOne[0]);
1665 DAG.ComputeMaskedBits(Ops[1], KnownZero[1], KnownOne[1]);
1667 // See if the upper 32 bits of one operand and the lower 32 bits of the
1668 // other are known zero. They are the low and high operands respectively.
1669 uint64_t Masks[] = { KnownZero[0].getZExtValue(),
1670 KnownZero[1].getZExtValue() };
1672 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
1674 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
1679 SDValue LowOp = Ops[Low];
1680 SDValue HighOp = Ops[High];
1682 // If the high part is a constant, we're better off using IILH.
1683 if (HighOp.getOpcode() == ISD::Constant)
1686 // If the low part is a constant that is outside the range of LHI,
1687 // then we're better off using IILF.
1688 if (LowOp.getOpcode() == ISD::Constant) {
1689 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
1690 if (!isInt<16>(Value))
1694 // Check whether the high part is an AND that doesn't change the
1695 // high 32 bits and just masks out low bits. We can skip it if so.
1696 if (HighOp.getOpcode() == ISD::AND &&
1697 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
1698 ConstantSDNode *MaskNode = cast<ConstantSDNode>(HighOp.getOperand(1));
1699 uint64_t Mask = MaskNode->getZExtValue() | Masks[High];
1700 if ((Mask >> 32) == 0xffffffff)
1701 HighOp = HighOp.getOperand(0);
1704 // Take advantage of the fact that all GR32 operations only change the
1705 // low 32 bits by truncating Low to an i32 and inserting it directly
1706 // using a subreg. The interesting cases are those where the truncation
1709 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
1710 SDValue SubReg32 = DAG.getTargetConstant(SystemZ::subreg_32bit, MVT::i64);
1711 SDNode *Result = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
1712 MVT::i64, HighOp, Low32, SubReg32);
1713 return SDValue(Result, 0);
1716 // Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
1717 // two into the fullword ATOMIC_LOADW_* operation given by Opcode.
1718 SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
1720 unsigned Opcode) const {
1721 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode());
1723 // 32-bit operations need no code outside the main loop.
1724 EVT NarrowVT = Node->getMemoryVT();
1725 EVT WideVT = MVT::i32;
1726 if (NarrowVT == WideVT)
1729 int64_t BitSize = NarrowVT.getSizeInBits();
1730 SDValue ChainIn = Node->getChain();
1731 SDValue Addr = Node->getBasePtr();
1732 SDValue Src2 = Node->getVal();
1733 MachineMemOperand *MMO = Node->getMemOperand();
1735 EVT PtrVT = Addr.getValueType();
1737 // Convert atomic subtracts of constants into additions.
1738 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
1739 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(Src2)) {
1740 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
1741 Src2 = DAG.getConstant(-Const->getSExtValue(), Src2.getValueType());
1744 // Get the address of the containing word.
1745 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
1746 DAG.getConstant(-4, PtrVT));
1748 // Get the number of bits that the word must be rotated left in order
1749 // to bring the field to the top bits of a GR32.
1750 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
1751 DAG.getConstant(3, PtrVT));
1752 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
1754 // Get the complementing shift amount, for rotating a field in the top
1755 // bits back to its proper position.
1756 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
1757 DAG.getConstant(0, WideVT), BitShift);
1759 // Extend the source operand to 32 bits and prepare it for the inner loop.
1760 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
1761 // operations require the source to be shifted in advance. (This shift
1762 // can be folded if the source is constant.) For AND and NAND, the lower
1763 // bits must be set, while for other opcodes they should be left clear.
1764 if (Opcode != SystemZISD::ATOMIC_SWAPW)
1765 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
1766 DAG.getConstant(32 - BitSize, WideVT));
1767 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
1768 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
1769 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
1770 DAG.getConstant(uint32_t(-1) >> BitSize, WideVT));
1772 // Construct the ATOMIC_LOADW_* node.
1773 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
1774 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
1775 DAG.getConstant(BitSize, WideVT) };
1776 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
1777 array_lengthof(Ops),
1780 // Rotate the result of the final CS so that the field is in the lower
1781 // bits of a GR32, then truncate it.
1782 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
1783 DAG.getConstant(BitSize, WideVT));
1784 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
1786 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
1787 return DAG.getMergeValues(RetOps, 2, DL);
1790 // Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
1791 // into a fullword ATOMIC_CMP_SWAPW operation.
1792 SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
1793 SelectionDAG &DAG) const {
1794 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode());
1796 // We have native support for 32-bit compare and swap.
1797 EVT NarrowVT = Node->getMemoryVT();
1798 EVT WideVT = MVT::i32;
1799 if (NarrowVT == WideVT)
1802 int64_t BitSize = NarrowVT.getSizeInBits();
1803 SDValue ChainIn = Node->getOperand(0);
1804 SDValue Addr = Node->getOperand(1);
1805 SDValue CmpVal = Node->getOperand(2);
1806 SDValue SwapVal = Node->getOperand(3);
1807 MachineMemOperand *MMO = Node->getMemOperand();
1809 EVT PtrVT = Addr.getValueType();
1811 // Get the address of the containing word.
1812 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
1813 DAG.getConstant(-4, PtrVT));
1815 // Get the number of bits that the word must be rotated left in order
1816 // to bring the field to the top bits of a GR32.
1817 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
1818 DAG.getConstant(3, PtrVT));
1819 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
1821 // Get the complementing shift amount, for rotating a field in the top
1822 // bits back to its proper position.
1823 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
1824 DAG.getConstant(0, WideVT), BitShift);
1826 // Construct the ATOMIC_CMP_SWAPW node.
1827 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
1828 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
1829 NegBitShift, DAG.getConstant(BitSize, WideVT) };
1830 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
1831 VTList, Ops, array_lengthof(Ops),
1836 SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
1837 SelectionDAG &DAG) const {
1838 MachineFunction &MF = DAG.getMachineFunction();
1839 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
1840 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
1841 SystemZ::R15D, Op.getValueType());
1844 SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
1845 SelectionDAG &DAG) const {
1846 MachineFunction &MF = DAG.getMachineFunction();
1847 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
1848 return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op),
1849 SystemZ::R15D, Op.getOperand(1));
1852 SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
1853 SelectionDAG &DAG) const {
1854 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
1856 // Just preserve the chain.
1857 return Op.getOperand(0);
1859 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
1860 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
1861 MemIntrinsicSDNode *Node = cast<MemIntrinsicSDNode>(Op.getNode());
1864 DAG.getConstant(Code, MVT::i32),
1867 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, SDLoc(Op),
1868 Node->getVTList(), Ops, array_lengthof(Ops),
1869 Node->getMemoryVT(), Node->getMemOperand());
1872 SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
1873 SelectionDAG &DAG) const {
1874 switch (Op.getOpcode()) {
1876 return lowerBR_CC(Op, DAG);
1877 case ISD::SELECT_CC:
1878 return lowerSELECT_CC(Op, DAG);
1879 case ISD::GlobalAddress:
1880 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
1881 case ISD::GlobalTLSAddress:
1882 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
1883 case ISD::BlockAddress:
1884 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
1885 case ISD::JumpTable:
1886 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
1887 case ISD::ConstantPool:
1888 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
1890 return lowerBITCAST(Op, DAG);
1892 return lowerVASTART(Op, DAG);
1894 return lowerVACOPY(Op, DAG);
1895 case ISD::DYNAMIC_STACKALLOC:
1896 return lowerDYNAMIC_STACKALLOC(Op, DAG);
1897 case ISD::SMUL_LOHI:
1898 return lowerSMUL_LOHI(Op, DAG);
1899 case ISD::UMUL_LOHI:
1900 return lowerUMUL_LOHI(Op, DAG);
1902 return lowerSDIVREM(Op, DAG);
1904 return lowerUDIVREM(Op, DAG);
1906 return lowerOR(Op, DAG);
1907 case ISD::ATOMIC_SWAP:
1908 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_SWAPW);
1909 case ISD::ATOMIC_LOAD_ADD:
1910 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
1911 case ISD::ATOMIC_LOAD_SUB:
1912 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
1913 case ISD::ATOMIC_LOAD_AND:
1914 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
1915 case ISD::ATOMIC_LOAD_OR:
1916 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
1917 case ISD::ATOMIC_LOAD_XOR:
1918 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
1919 case ISD::ATOMIC_LOAD_NAND:
1920 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
1921 case ISD::ATOMIC_LOAD_MIN:
1922 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
1923 case ISD::ATOMIC_LOAD_MAX:
1924 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
1925 case ISD::ATOMIC_LOAD_UMIN:
1926 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
1927 case ISD::ATOMIC_LOAD_UMAX:
1928 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
1929 case ISD::ATOMIC_CMP_SWAP:
1930 return lowerATOMIC_CMP_SWAP(Op, DAG);
1931 case ISD::STACKSAVE:
1932 return lowerSTACKSAVE(Op, DAG);
1933 case ISD::STACKRESTORE:
1934 return lowerSTACKRESTORE(Op, DAG);
1936 return lowerPREFETCH(Op, DAG);
1938 llvm_unreachable("Unexpected node to lower");
1942 const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
1943 #define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
1948 OPCODE(PCREL_WRAPPER);
1953 OPCODE(SELECT_CCMASK);
1954 OPCODE(ADJDYNALLOC);
1955 OPCODE(EXTRACT_ACCESS);
1956 OPCODE(UMUL_LOHI64);
1966 OPCODE(SEARCH_STRING);
1968 OPCODE(ATOMIC_SWAPW);
1969 OPCODE(ATOMIC_LOADW_ADD);
1970 OPCODE(ATOMIC_LOADW_SUB);
1971 OPCODE(ATOMIC_LOADW_AND);
1972 OPCODE(ATOMIC_LOADW_OR);
1973 OPCODE(ATOMIC_LOADW_XOR);
1974 OPCODE(ATOMIC_LOADW_NAND);
1975 OPCODE(ATOMIC_LOADW_MIN);
1976 OPCODE(ATOMIC_LOADW_MAX);
1977 OPCODE(ATOMIC_LOADW_UMIN);
1978 OPCODE(ATOMIC_LOADW_UMAX);
1979 OPCODE(ATOMIC_CMP_SWAPW);
1986 //===----------------------------------------------------------------------===//
1988 //===----------------------------------------------------------------------===//
1990 // Create a new basic block after MBB.
1991 static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
1992 MachineFunction &MF = *MBB->getParent();
1993 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
1994 MF.insert(llvm::next(MachineFunction::iterator(MBB)), NewMBB);
1998 // Split MBB after MI and return the new block (the one that contains
1999 // instructions after MI).
2000 static MachineBasicBlock *splitBlockAfter(MachineInstr *MI,
2001 MachineBasicBlock *MBB) {
2002 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
2003 NewMBB->splice(NewMBB->begin(), MBB,
2004 llvm::next(MachineBasicBlock::iterator(MI)),
2006 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
2010 // Split MBB before MI and return the new block (the one that contains MI).
2011 static MachineBasicBlock *splitBlockBefore(MachineInstr *MI,
2012 MachineBasicBlock *MBB) {
2013 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
2014 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
2015 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
2019 // Force base value Base into a register before MI. Return the register.
2020 static unsigned forceReg(MachineInstr *MI, MachineOperand &Base,
2021 const SystemZInstrInfo *TII) {
2023 return Base.getReg();
2025 MachineBasicBlock *MBB = MI->getParent();
2026 MachineFunction &MF = *MBB->getParent();
2027 MachineRegisterInfo &MRI = MF.getRegInfo();
2029 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
2030 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg)
2031 .addOperand(Base).addImm(0).addReg(0);
2035 // Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
2037 SystemZTargetLowering::emitSelect(MachineInstr *MI,
2038 MachineBasicBlock *MBB) const {
2039 const SystemZInstrInfo *TII = TM.getInstrInfo();
2041 unsigned DestReg = MI->getOperand(0).getReg();
2042 unsigned TrueReg = MI->getOperand(1).getReg();
2043 unsigned FalseReg = MI->getOperand(2).getReg();
2044 unsigned CCValid = MI->getOperand(3).getImm();
2045 unsigned CCMask = MI->getOperand(4).getImm();
2046 DebugLoc DL = MI->getDebugLoc();
2048 MachineBasicBlock *StartMBB = MBB;
2049 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
2050 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
2053 // BRC CCMask, JoinMBB
2054 // # fallthrough to FalseMBB
2056 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2057 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
2058 MBB->addSuccessor(JoinMBB);
2059 MBB->addSuccessor(FalseMBB);
2062 // # fallthrough to JoinMBB
2064 MBB->addSuccessor(JoinMBB);
2067 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
2070 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
2071 .addReg(TrueReg).addMBB(StartMBB)
2072 .addReg(FalseReg).addMBB(FalseMBB);
2074 MI->eraseFromParent();
2078 // Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
2079 // StoreOpcode is the store to use and Invert says whether the store should
2080 // happen when the condition is false rather than true. If a STORE ON
2081 // CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
2083 SystemZTargetLowering::emitCondStore(MachineInstr *MI,
2084 MachineBasicBlock *MBB,
2085 unsigned StoreOpcode, unsigned STOCOpcode,
2086 bool Invert) const {
2087 const SystemZInstrInfo *TII = TM.getInstrInfo();
2089 unsigned SrcReg = MI->getOperand(0).getReg();
2090 MachineOperand Base = MI->getOperand(1);
2091 int64_t Disp = MI->getOperand(2).getImm();
2092 unsigned IndexReg = MI->getOperand(3).getReg();
2093 unsigned CCValid = MI->getOperand(4).getImm();
2094 unsigned CCMask = MI->getOperand(5).getImm();
2095 DebugLoc DL = MI->getDebugLoc();
2097 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
2099 // Use STOCOpcode if possible. We could use different store patterns in
2100 // order to avoid matching the index register, but the performance trade-offs
2101 // might be more complicated in that case.
2102 if (STOCOpcode && !IndexReg && TM.getSubtargetImpl()->hasLoadStoreOnCond()) {
2105 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
2106 .addReg(SrcReg).addOperand(Base).addImm(Disp)
2107 .addImm(CCValid).addImm(CCMask);
2108 MI->eraseFromParent();
2112 // Get the condition needed to branch around the store.
2116 MachineBasicBlock *StartMBB = MBB;
2117 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
2118 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
2121 // BRC CCMask, JoinMBB
2122 // # fallthrough to FalseMBB
2124 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2125 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
2126 MBB->addSuccessor(JoinMBB);
2127 MBB->addSuccessor(FalseMBB);
2130 // store %SrcReg, %Disp(%Index,%Base)
2131 // # fallthrough to JoinMBB
2133 BuildMI(MBB, DL, TII->get(StoreOpcode))
2134 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
2135 MBB->addSuccessor(JoinMBB);
2137 MI->eraseFromParent();
2141 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
2142 // or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
2143 // performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
2144 // BitSize is the width of the field in bits, or 0 if this is a partword
2145 // ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
2146 // is one of the operands. Invert says whether the field should be
2147 // inverted after performing BinOpcode (e.g. for NAND).
2149 SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI,
2150 MachineBasicBlock *MBB,
2153 bool Invert) const {
2154 const SystemZInstrInfo *TII = TM.getInstrInfo();
2155 MachineFunction &MF = *MBB->getParent();
2156 MachineRegisterInfo &MRI = MF.getRegInfo();
2157 bool IsSubWord = (BitSize < 32);
2159 // Extract the operands. Base can be a register or a frame index.
2160 // Src2 can be a register or immediate.
2161 unsigned Dest = MI->getOperand(0).getReg();
2162 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2163 int64_t Disp = MI->getOperand(2).getImm();
2164 MachineOperand Src2 = earlyUseOperand(MI->getOperand(3));
2165 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
2166 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
2167 DebugLoc DL = MI->getDebugLoc();
2169 BitSize = MI->getOperand(6).getImm();
2171 // Subword operations use 32-bit registers.
2172 const TargetRegisterClass *RC = (BitSize <= 32 ?
2173 &SystemZ::GR32BitRegClass :
2174 &SystemZ::GR64BitRegClass);
2175 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
2176 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
2178 // Get the right opcodes for the displacement.
2179 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
2180 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
2181 assert(LOpcode && CSOpcode && "Displacement out of range");
2183 // Create virtual registers for temporary results.
2184 unsigned OrigVal = MRI.createVirtualRegister(RC);
2185 unsigned OldVal = MRI.createVirtualRegister(RC);
2186 unsigned NewVal = (BinOpcode || IsSubWord ?
2187 MRI.createVirtualRegister(RC) : Src2.getReg());
2188 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
2189 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
2191 // Insert a basic block for the main loop.
2192 MachineBasicBlock *StartMBB = MBB;
2193 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
2194 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2198 // %OrigVal = L Disp(%Base)
2199 // # fall through to LoopMMB
2201 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
2202 .addOperand(Base).addImm(Disp).addReg(0);
2203 MBB->addSuccessor(LoopMBB);
2206 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
2207 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
2208 // %RotatedNewVal = OP %RotatedOldVal, %Src2
2209 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
2210 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
2212 // # fall through to DoneMMB
2214 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2215 .addReg(OrigVal).addMBB(StartMBB)
2216 .addReg(Dest).addMBB(LoopMBB);
2218 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
2219 .addReg(OldVal).addReg(BitShift).addImm(0);
2221 // Perform the operation normally and then invert every bit of the field.
2222 unsigned Tmp = MRI.createVirtualRegister(RC);
2223 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
2224 .addReg(RotatedOldVal).addOperand(Src2);
2226 // XILF with the upper BitSize bits set.
2227 BuildMI(MBB, DL, TII->get(SystemZ::XILF32), RotatedNewVal)
2228 .addReg(Tmp).addImm(uint32_t(~0 << (32 - BitSize)));
2229 else if (BitSize == 32)
2230 // XILF with every bit set.
2231 BuildMI(MBB, DL, TII->get(SystemZ::XILF32), RotatedNewVal)
2232 .addReg(Tmp).addImm(~uint32_t(0));
2234 // Use LCGR and add -1 to the result, which is more compact than
2235 // an XILF, XILH pair.
2236 unsigned Tmp2 = MRI.createVirtualRegister(RC);
2237 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
2238 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
2239 .addReg(Tmp2).addImm(-1);
2241 } else if (BinOpcode)
2242 // A simply binary operation.
2243 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
2244 .addReg(RotatedOldVal).addOperand(Src2);
2246 // Use RISBG to rotate Src2 into position and use it to replace the
2247 // field in RotatedOldVal.
2248 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
2249 .addReg(RotatedOldVal).addReg(Src2.getReg())
2250 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
2252 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
2253 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
2254 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
2255 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
2256 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2257 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
2258 MBB->addSuccessor(LoopMBB);
2259 MBB->addSuccessor(DoneMBB);
2261 MI->eraseFromParent();
2265 // Implement EmitInstrWithCustomInserter for pseudo
2266 // ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
2267 // instruction that should be used to compare the current field with the
2268 // minimum or maximum value. KeepOldMask is the BRC condition-code mask
2269 // for when the current field should be kept. BitSize is the width of
2270 // the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
2272 SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI,
2273 MachineBasicBlock *MBB,
2274 unsigned CompareOpcode,
2275 unsigned KeepOldMask,
2276 unsigned BitSize) const {
2277 const SystemZInstrInfo *TII = TM.getInstrInfo();
2278 MachineFunction &MF = *MBB->getParent();
2279 MachineRegisterInfo &MRI = MF.getRegInfo();
2280 bool IsSubWord = (BitSize < 32);
2282 // Extract the operands. Base can be a register or a frame index.
2283 unsigned Dest = MI->getOperand(0).getReg();
2284 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2285 int64_t Disp = MI->getOperand(2).getImm();
2286 unsigned Src2 = MI->getOperand(3).getReg();
2287 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
2288 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
2289 DebugLoc DL = MI->getDebugLoc();
2291 BitSize = MI->getOperand(6).getImm();
2293 // Subword operations use 32-bit registers.
2294 const TargetRegisterClass *RC = (BitSize <= 32 ?
2295 &SystemZ::GR32BitRegClass :
2296 &SystemZ::GR64BitRegClass);
2297 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
2298 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
2300 // Get the right opcodes for the displacement.
2301 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
2302 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
2303 assert(LOpcode && CSOpcode && "Displacement out of range");
2305 // Create virtual registers for temporary results.
2306 unsigned OrigVal = MRI.createVirtualRegister(RC);
2307 unsigned OldVal = MRI.createVirtualRegister(RC);
2308 unsigned NewVal = MRI.createVirtualRegister(RC);
2309 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
2310 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
2311 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
2313 // Insert 3 basic blocks for the loop.
2314 MachineBasicBlock *StartMBB = MBB;
2315 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
2316 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2317 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
2318 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
2322 // %OrigVal = L Disp(%Base)
2323 // # fall through to LoopMMB
2325 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
2326 .addOperand(Base).addImm(Disp).addReg(0);
2327 MBB->addSuccessor(LoopMBB);
2330 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
2331 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
2332 // CompareOpcode %RotatedOldVal, %Src2
2333 // BRC KeepOldMask, UpdateMBB
2335 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2336 .addReg(OrigVal).addMBB(StartMBB)
2337 .addReg(Dest).addMBB(UpdateMBB);
2339 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
2340 .addReg(OldVal).addReg(BitShift).addImm(0);
2341 BuildMI(MBB, DL, TII->get(CompareOpcode))
2342 .addReg(RotatedOldVal).addReg(Src2);
2343 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2344 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
2345 MBB->addSuccessor(UpdateMBB);
2346 MBB->addSuccessor(UseAltMBB);
2349 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
2350 // # fall through to UpdateMMB
2353 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
2354 .addReg(RotatedOldVal).addReg(Src2)
2355 .addImm(32).addImm(31 + BitSize).addImm(0);
2356 MBB->addSuccessor(UpdateMBB);
2359 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
2360 // [ %RotatedAltVal, UseAltMBB ]
2361 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
2362 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
2364 // # fall through to DoneMMB
2366 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
2367 .addReg(RotatedOldVal).addMBB(LoopMBB)
2368 .addReg(RotatedAltVal).addMBB(UseAltMBB);
2370 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
2371 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
2372 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
2373 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
2374 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2375 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
2376 MBB->addSuccessor(LoopMBB);
2377 MBB->addSuccessor(DoneMBB);
2379 MI->eraseFromParent();
2383 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
2386 SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI,
2387 MachineBasicBlock *MBB) const {
2388 const SystemZInstrInfo *TII = TM.getInstrInfo();
2389 MachineFunction &MF = *MBB->getParent();
2390 MachineRegisterInfo &MRI = MF.getRegInfo();
2392 // Extract the operands. Base can be a register or a frame index.
2393 unsigned Dest = MI->getOperand(0).getReg();
2394 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2395 int64_t Disp = MI->getOperand(2).getImm();
2396 unsigned OrigCmpVal = MI->getOperand(3).getReg();
2397 unsigned OrigSwapVal = MI->getOperand(4).getReg();
2398 unsigned BitShift = MI->getOperand(5).getReg();
2399 unsigned NegBitShift = MI->getOperand(6).getReg();
2400 int64_t BitSize = MI->getOperand(7).getImm();
2401 DebugLoc DL = MI->getDebugLoc();
2403 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
2405 // Get the right opcodes for the displacement.
2406 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
2407 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
2408 assert(LOpcode && CSOpcode && "Displacement out of range");
2410 // Create virtual registers for temporary results.
2411 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
2412 unsigned OldVal = MRI.createVirtualRegister(RC);
2413 unsigned CmpVal = MRI.createVirtualRegister(RC);
2414 unsigned SwapVal = MRI.createVirtualRegister(RC);
2415 unsigned StoreVal = MRI.createVirtualRegister(RC);
2416 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
2417 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
2418 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
2420 // Insert 2 basic blocks for the loop.
2421 MachineBasicBlock *StartMBB = MBB;
2422 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
2423 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2424 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
2428 // %OrigOldVal = L Disp(%Base)
2429 // # fall through to LoopMMB
2431 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
2432 .addOperand(Base).addImm(Disp).addReg(0);
2433 MBB->addSuccessor(LoopMBB);
2436 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
2437 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
2438 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
2439 // %Dest = RLL %OldVal, BitSize(%BitShift)
2440 // ^^ The low BitSize bits contain the field
2442 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
2443 // ^^ Replace the upper 32-BitSize bits of the
2444 // comparison value with those that we loaded,
2445 // so that we can use a full word comparison.
2446 // CR %Dest, %RetryCmpVal
2448 // # Fall through to SetMBB
2450 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2451 .addReg(OrigOldVal).addMBB(StartMBB)
2452 .addReg(RetryOldVal).addMBB(SetMBB);
2453 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
2454 .addReg(OrigCmpVal).addMBB(StartMBB)
2455 .addReg(RetryCmpVal).addMBB(SetMBB);
2456 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
2457 .addReg(OrigSwapVal).addMBB(StartMBB)
2458 .addReg(RetrySwapVal).addMBB(SetMBB);
2459 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
2460 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
2461 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
2462 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
2463 BuildMI(MBB, DL, TII->get(SystemZ::CR))
2464 .addReg(Dest).addReg(RetryCmpVal);
2465 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2466 .addImm(SystemZ::CCMASK_ICMP)
2467 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
2468 MBB->addSuccessor(DoneMBB);
2469 MBB->addSuccessor(SetMBB);
2472 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
2473 // ^^ Replace the upper 32-BitSize bits of the new
2474 // value with those that we loaded.
2475 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
2476 // ^^ Rotate the new field to its proper position.
2477 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
2479 // # fall through to ExitMMB
2481 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
2482 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
2483 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
2484 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
2485 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
2486 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
2487 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2488 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
2489 MBB->addSuccessor(LoopMBB);
2490 MBB->addSuccessor(DoneMBB);
2492 MI->eraseFromParent();
2496 // Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true
2497 // if the high register of the GR128 value must be cleared or false if
2498 // it's "don't care". SubReg is subreg_odd32 when extending a GR32
2499 // and subreg_odd when extending a GR64.
2501 SystemZTargetLowering::emitExt128(MachineInstr *MI,
2502 MachineBasicBlock *MBB,
2503 bool ClearEven, unsigned SubReg) const {
2504 const SystemZInstrInfo *TII = TM.getInstrInfo();
2505 MachineFunction &MF = *MBB->getParent();
2506 MachineRegisterInfo &MRI = MF.getRegInfo();
2507 DebugLoc DL = MI->getDebugLoc();
2509 unsigned Dest = MI->getOperand(0).getReg();
2510 unsigned Src = MI->getOperand(1).getReg();
2511 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
2513 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
2515 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
2516 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
2518 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
2520 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
2521 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_high);
2524 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
2525 .addReg(In128).addReg(Src).addImm(SubReg);
2527 MI->eraseFromParent();
2532 SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI,
2533 MachineBasicBlock *MBB,
2534 unsigned Opcode) const {
2535 const SystemZInstrInfo *TII = TM.getInstrInfo();
2536 MachineFunction &MF = *MBB->getParent();
2537 MachineRegisterInfo &MRI = MF.getRegInfo();
2538 DebugLoc DL = MI->getDebugLoc();
2540 MachineOperand DestBase = earlyUseOperand(MI->getOperand(0));
2541 uint64_t DestDisp = MI->getOperand(1).getImm();
2542 MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2));
2543 uint64_t SrcDisp = MI->getOperand(3).getImm();
2544 uint64_t Length = MI->getOperand(4).getImm();
2546 // When generating more than one CLC, all but the last will need to
2547 // branch to the end when a difference is found.
2548 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
2549 splitBlockAfter(MI, MBB) : 0);
2551 // Check for the loop form, in which operand 5 is the trip count.
2552 if (MI->getNumExplicitOperands() > 5) {
2553 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
2555 uint64_t StartCountReg = MI->getOperand(5).getReg();
2556 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
2557 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
2558 forceReg(MI, DestBase, TII));
2560 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
2561 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
2562 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
2563 MRI.createVirtualRegister(RC));
2564 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
2565 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
2566 MRI.createVirtualRegister(RC));
2568 RC = &SystemZ::GR64BitRegClass;
2569 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
2570 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
2572 MachineBasicBlock *StartMBB = MBB;
2573 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
2574 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2575 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
2578 // # fall through to LoopMMB
2579 MBB->addSuccessor(LoopMBB);
2582 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
2583 // [ %NextDestReg, NextMBB ]
2584 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
2585 // [ %NextSrcReg, NextMBB ]
2586 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
2587 // [ %NextCountReg, NextMBB ]
2588 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
2589 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
2592 // The prefetch is used only for MVC. The JLH is used only for CLC.
2595 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
2596 .addReg(StartDestReg).addMBB(StartMBB)
2597 .addReg(NextDestReg).addMBB(NextMBB);
2598 if (!HaveSingleBase)
2599 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
2600 .addReg(StartSrcReg).addMBB(StartMBB)
2601 .addReg(NextSrcReg).addMBB(NextMBB);
2602 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
2603 .addReg(StartCountReg).addMBB(StartMBB)
2604 .addReg(NextCountReg).addMBB(NextMBB);
2605 if (Opcode == SystemZ::MVC)
2606 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
2607 .addImm(SystemZ::PFD_WRITE)
2608 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
2609 BuildMI(MBB, DL, TII->get(Opcode))
2610 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
2611 .addReg(ThisSrcReg).addImm(SrcDisp);
2613 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2614 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
2616 MBB->addSuccessor(EndMBB);
2617 MBB->addSuccessor(NextMBB);
2621 // %NextDestReg = LA 256(%ThisDestReg)
2622 // %NextSrcReg = LA 256(%ThisSrcReg)
2623 // %NextCountReg = AGHI %ThisCountReg, -1
2624 // CGHI %NextCountReg, 0
2626 // # fall through to DoneMMB
2628 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
2631 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
2632 .addReg(ThisDestReg).addImm(256).addReg(0);
2633 if (!HaveSingleBase)
2634 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
2635 .addReg(ThisSrcReg).addImm(256).addReg(0);
2636 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
2637 .addReg(ThisCountReg).addImm(-1);
2638 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
2639 .addReg(NextCountReg).addImm(0);
2640 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2641 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
2643 MBB->addSuccessor(LoopMBB);
2644 MBB->addSuccessor(DoneMBB);
2646 DestBase = MachineOperand::CreateReg(NextDestReg, false);
2647 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
2651 // Handle any remaining bytes with straight-line code.
2652 while (Length > 0) {
2653 uint64_t ThisLength = std::min(Length, uint64_t(256));
2654 // The previous iteration might have created out-of-range displacements.
2655 // Apply them using LAY if so.
2656 if (!isUInt<12>(DestDisp)) {
2657 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
2658 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
2659 .addOperand(DestBase).addImm(DestDisp).addReg(0);
2660 DestBase = MachineOperand::CreateReg(Reg, false);
2663 if (!isUInt<12>(SrcDisp)) {
2664 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
2665 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
2666 .addOperand(SrcBase).addImm(SrcDisp).addReg(0);
2667 SrcBase = MachineOperand::CreateReg(Reg, false);
2670 BuildMI(*MBB, MI, DL, TII->get(Opcode))
2671 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength)
2672 .addOperand(SrcBase).addImm(SrcDisp);
2673 DestDisp += ThisLength;
2674 SrcDisp += ThisLength;
2675 Length -= ThisLength;
2676 // If there's another CLC to go, branch to the end if a difference
2678 if (EndMBB && Length > 0) {
2679 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
2680 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2681 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
2683 MBB->addSuccessor(EndMBB);
2684 MBB->addSuccessor(NextMBB);
2689 MBB->addSuccessor(EndMBB);
2691 MBB->addLiveIn(SystemZ::CC);
2694 MI->eraseFromParent();
2698 // Decompose string pseudo-instruction MI into a loop that continually performs
2699 // Opcode until CC != 3.
2701 SystemZTargetLowering::emitStringWrapper(MachineInstr *MI,
2702 MachineBasicBlock *MBB,
2703 unsigned Opcode) const {
2704 const SystemZInstrInfo *TII = TM.getInstrInfo();
2705 MachineFunction &MF = *MBB->getParent();
2706 MachineRegisterInfo &MRI = MF.getRegInfo();
2707 DebugLoc DL = MI->getDebugLoc();
2709 uint64_t End1Reg = MI->getOperand(0).getReg();
2710 uint64_t Start1Reg = MI->getOperand(1).getReg();
2711 uint64_t Start2Reg = MI->getOperand(2).getReg();
2712 uint64_t CharReg = MI->getOperand(3).getReg();
2714 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
2715 uint64_t This1Reg = MRI.createVirtualRegister(RC);
2716 uint64_t This2Reg = MRI.createVirtualRegister(RC);
2717 uint64_t End2Reg = MRI.createVirtualRegister(RC);
2719 MachineBasicBlock *StartMBB = MBB;
2720 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
2721 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2724 // # fall through to LoopMMB
2725 MBB->addSuccessor(LoopMBB);
2728 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
2729 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
2731 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0W
2733 // # fall through to DoneMMB
2735 // The load of R0W can be hoisted by post-RA LICM.
2738 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
2739 .addReg(Start1Reg).addMBB(StartMBB)
2740 .addReg(End1Reg).addMBB(LoopMBB);
2741 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
2742 .addReg(Start2Reg).addMBB(StartMBB)
2743 .addReg(End2Reg).addMBB(LoopMBB);
2744 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0W).addReg(CharReg);
2745 BuildMI(MBB, DL, TII->get(Opcode))
2746 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
2747 .addReg(This1Reg).addReg(This2Reg);
2748 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2749 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
2750 MBB->addSuccessor(LoopMBB);
2751 MBB->addSuccessor(DoneMBB);
2753 DoneMBB->addLiveIn(SystemZ::CC);
2755 MI->eraseFromParent();
2759 MachineBasicBlock *SystemZTargetLowering::
2760 EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const {
2761 switch (MI->getOpcode()) {
2762 case SystemZ::Select32:
2763 case SystemZ::SelectF32:
2764 case SystemZ::Select64:
2765 case SystemZ::SelectF64:
2766 case SystemZ::SelectF128:
2767 return emitSelect(MI, MBB);
2769 case SystemZ::CondStore8_32:
2770 return emitCondStore(MI, MBB, SystemZ::STC32, 0, false);
2771 case SystemZ::CondStore8_32Inv:
2772 return emitCondStore(MI, MBB, SystemZ::STC32, 0, true);
2773 case SystemZ::CondStore16_32:
2774 return emitCondStore(MI, MBB, SystemZ::STH32, 0, false);
2775 case SystemZ::CondStore16_32Inv:
2776 return emitCondStore(MI, MBB, SystemZ::STH32, 0, true);
2777 case SystemZ::CondStore32_32:
2778 return emitCondStore(MI, MBB, SystemZ::ST32, SystemZ::STOC32, false);
2779 case SystemZ::CondStore32_32Inv:
2780 return emitCondStore(MI, MBB, SystemZ::ST32, SystemZ::STOC32, true);
2781 case SystemZ::CondStore8:
2782 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
2783 case SystemZ::CondStore8Inv:
2784 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
2785 case SystemZ::CondStore16:
2786 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
2787 case SystemZ::CondStore16Inv:
2788 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
2789 case SystemZ::CondStore32:
2790 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
2791 case SystemZ::CondStore32Inv:
2792 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
2793 case SystemZ::CondStore64:
2794 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
2795 case SystemZ::CondStore64Inv:
2796 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
2797 case SystemZ::CondStoreF32:
2798 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
2799 case SystemZ::CondStoreF32Inv:
2800 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
2801 case SystemZ::CondStoreF64:
2802 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
2803 case SystemZ::CondStoreF64Inv:
2804 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
2806 case SystemZ::AEXT128_64:
2807 return emitExt128(MI, MBB, false, SystemZ::subreg_low);
2808 case SystemZ::ZEXT128_32:
2809 return emitExt128(MI, MBB, true, SystemZ::subreg_low32);
2810 case SystemZ::ZEXT128_64:
2811 return emitExt128(MI, MBB, true, SystemZ::subreg_low);
2813 case SystemZ::ATOMIC_SWAPW:
2814 return emitAtomicLoadBinary(MI, MBB, 0, 0);
2815 case SystemZ::ATOMIC_SWAP_32:
2816 return emitAtomicLoadBinary(MI, MBB, 0, 32);
2817 case SystemZ::ATOMIC_SWAP_64:
2818 return emitAtomicLoadBinary(MI, MBB, 0, 64);
2820 case SystemZ::ATOMIC_LOADW_AR:
2821 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
2822 case SystemZ::ATOMIC_LOADW_AFI:
2823 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
2824 case SystemZ::ATOMIC_LOAD_AR:
2825 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
2826 case SystemZ::ATOMIC_LOAD_AHI:
2827 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
2828 case SystemZ::ATOMIC_LOAD_AFI:
2829 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
2830 case SystemZ::ATOMIC_LOAD_AGR:
2831 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
2832 case SystemZ::ATOMIC_LOAD_AGHI:
2833 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
2834 case SystemZ::ATOMIC_LOAD_AGFI:
2835 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
2837 case SystemZ::ATOMIC_LOADW_SR:
2838 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
2839 case SystemZ::ATOMIC_LOAD_SR:
2840 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
2841 case SystemZ::ATOMIC_LOAD_SGR:
2842 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
2844 case SystemZ::ATOMIC_LOADW_NR:
2845 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
2846 case SystemZ::ATOMIC_LOADW_NILH:
2847 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH32, 0);
2848 case SystemZ::ATOMIC_LOAD_NR:
2849 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
2850 case SystemZ::ATOMIC_LOAD_NILL32:
2851 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL32, 32);
2852 case SystemZ::ATOMIC_LOAD_NILH32:
2853 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH32, 32);
2854 case SystemZ::ATOMIC_LOAD_NILF32:
2855 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF32, 32);
2856 case SystemZ::ATOMIC_LOAD_NGR:
2857 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
2858 case SystemZ::ATOMIC_LOAD_NILL:
2859 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 64);
2860 case SystemZ::ATOMIC_LOAD_NILH:
2861 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 64);
2862 case SystemZ::ATOMIC_LOAD_NIHL:
2863 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL, 64);
2864 case SystemZ::ATOMIC_LOAD_NIHH:
2865 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH, 64);
2866 case SystemZ::ATOMIC_LOAD_NILF:
2867 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 64);
2868 case SystemZ::ATOMIC_LOAD_NIHF:
2869 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF, 64);
2871 case SystemZ::ATOMIC_LOADW_OR:
2872 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
2873 case SystemZ::ATOMIC_LOADW_OILH:
2874 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH32, 0);
2875 case SystemZ::ATOMIC_LOAD_OR:
2876 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
2877 case SystemZ::ATOMIC_LOAD_OILL32:
2878 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL32, 32);
2879 case SystemZ::ATOMIC_LOAD_OILH32:
2880 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH32, 32);
2881 case SystemZ::ATOMIC_LOAD_OILF32:
2882 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF32, 32);
2883 case SystemZ::ATOMIC_LOAD_OGR:
2884 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
2885 case SystemZ::ATOMIC_LOAD_OILL:
2886 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 64);
2887 case SystemZ::ATOMIC_LOAD_OILH:
2888 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 64);
2889 case SystemZ::ATOMIC_LOAD_OIHL:
2890 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL, 64);
2891 case SystemZ::ATOMIC_LOAD_OIHH:
2892 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH, 64);
2893 case SystemZ::ATOMIC_LOAD_OILF:
2894 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 64);
2895 case SystemZ::ATOMIC_LOAD_OIHF:
2896 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF, 64);
2898 case SystemZ::ATOMIC_LOADW_XR:
2899 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
2900 case SystemZ::ATOMIC_LOADW_XILF:
2901 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF32, 0);
2902 case SystemZ::ATOMIC_LOAD_XR:
2903 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
2904 case SystemZ::ATOMIC_LOAD_XILF32:
2905 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF32, 32);
2906 case SystemZ::ATOMIC_LOAD_XGR:
2907 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
2908 case SystemZ::ATOMIC_LOAD_XILF:
2909 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 64);
2910 case SystemZ::ATOMIC_LOAD_XIHF:
2911 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF, 64);
2913 case SystemZ::ATOMIC_LOADW_NRi:
2914 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
2915 case SystemZ::ATOMIC_LOADW_NILHi:
2916 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH32, 0, true);
2917 case SystemZ::ATOMIC_LOAD_NRi:
2918 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
2919 case SystemZ::ATOMIC_LOAD_NILL32i:
2920 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL32, 32, true);
2921 case SystemZ::ATOMIC_LOAD_NILH32i:
2922 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH32, 32, true);
2923 case SystemZ::ATOMIC_LOAD_NILF32i:
2924 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF32, 32, true);
2925 case SystemZ::ATOMIC_LOAD_NGRi:
2926 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
2927 case SystemZ::ATOMIC_LOAD_NILLi:
2928 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 64, true);
2929 case SystemZ::ATOMIC_LOAD_NILHi:
2930 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 64, true);
2931 case SystemZ::ATOMIC_LOAD_NIHLi:
2932 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL, 64, true);
2933 case SystemZ::ATOMIC_LOAD_NIHHi:
2934 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH, 64, true);
2935 case SystemZ::ATOMIC_LOAD_NILFi:
2936 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 64, true);
2937 case SystemZ::ATOMIC_LOAD_NIHFi:
2938 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF, 64, true);
2940 case SystemZ::ATOMIC_LOADW_MIN:
2941 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
2942 SystemZ::CCMASK_CMP_LE, 0);
2943 case SystemZ::ATOMIC_LOAD_MIN_32:
2944 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
2945 SystemZ::CCMASK_CMP_LE, 32);
2946 case SystemZ::ATOMIC_LOAD_MIN_64:
2947 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
2948 SystemZ::CCMASK_CMP_LE, 64);
2950 case SystemZ::ATOMIC_LOADW_MAX:
2951 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
2952 SystemZ::CCMASK_CMP_GE, 0);
2953 case SystemZ::ATOMIC_LOAD_MAX_32:
2954 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
2955 SystemZ::CCMASK_CMP_GE, 32);
2956 case SystemZ::ATOMIC_LOAD_MAX_64:
2957 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
2958 SystemZ::CCMASK_CMP_GE, 64);
2960 case SystemZ::ATOMIC_LOADW_UMIN:
2961 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
2962 SystemZ::CCMASK_CMP_LE, 0);
2963 case SystemZ::ATOMIC_LOAD_UMIN_32:
2964 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
2965 SystemZ::CCMASK_CMP_LE, 32);
2966 case SystemZ::ATOMIC_LOAD_UMIN_64:
2967 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
2968 SystemZ::CCMASK_CMP_LE, 64);
2970 case SystemZ::ATOMIC_LOADW_UMAX:
2971 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
2972 SystemZ::CCMASK_CMP_GE, 0);
2973 case SystemZ::ATOMIC_LOAD_UMAX_32:
2974 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
2975 SystemZ::CCMASK_CMP_GE, 32);
2976 case SystemZ::ATOMIC_LOAD_UMAX_64:
2977 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
2978 SystemZ::CCMASK_CMP_GE, 64);
2980 case SystemZ::ATOMIC_CMP_SWAPW:
2981 return emitAtomicCmpSwapW(MI, MBB);
2982 case SystemZ::MVCSequence:
2983 case SystemZ::MVCLoop:
2984 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
2985 case SystemZ::CLCSequence:
2986 case SystemZ::CLCLoop:
2987 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
2988 case SystemZ::CLSTLoop:
2989 return emitStringWrapper(MI, MBB, SystemZ::CLST);
2990 case SystemZ::MVSTLoop:
2991 return emitStringWrapper(MI, MBB, SystemZ::MVST);
2992 case SystemZ::SRSTLoop:
2993 return emitStringWrapper(MI, MBB, SystemZ::SRST);
2995 llvm_unreachable("Unexpected instr type to insert");