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 and a wider multiplication otherwise.
132 setOperationAction(ISD::MULHS, VT, Expand);
133 setOperationAction(ISD::MULHU, VT, Expand);
135 // We have instructions for signed but not unsigned FP conversion.
136 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
140 // Type legalization will convert 8- and 16-bit atomic operations into
141 // forms that operate on i32s (but still keeping the original memory VT).
142 // Lower them into full i32 operations.
143 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom);
144 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom);
145 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
146 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom);
147 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom);
148 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom);
149 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
150 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom);
151 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom);
152 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
153 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
154 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
156 // We have instructions for signed but not unsigned FP conversion.
157 // Handle unsigned 32-bit types as signed 64-bit types.
158 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
159 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
161 // We have native support for a 64-bit CTLZ, via FLOGR.
162 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
163 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
165 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
166 setOperationAction(ISD::OR, MVT::i64, Custom);
168 // The architecture has 32-bit SMUL_LOHI and UMUL_LOHI (MR and MLR),
169 // but they aren't really worth using. There is no 64-bit SMUL_LOHI,
170 // but there is a 64-bit UMUL_LOHI: MLGR.
171 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
172 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
173 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
174 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Custom);
176 // FIXME: Can we support these natively?
177 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
178 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
179 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
181 // We have native instructions for i8, i16 and i32 extensions, but not i1.
182 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
183 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
184 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
185 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
187 // Handle the various types of symbolic address.
188 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
189 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
190 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
191 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
192 setOperationAction(ISD::JumpTable, PtrVT, Custom);
194 // We need to handle dynamic allocations specially because of the
195 // 160-byte area at the bottom of the stack.
196 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
198 // Use custom expanders so that we can force the function to use
200 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
201 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
203 // Handle floating-point types.
204 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
205 I <= MVT::LAST_FP_VALUETYPE;
207 MVT VT = MVT::SimpleValueType(I);
208 if (isTypeLegal(VT)) {
209 // We can use FI for FRINT.
210 setOperationAction(ISD::FRINT, VT, Legal);
212 // No special instructions for these.
213 setOperationAction(ISD::FSIN, VT, Expand);
214 setOperationAction(ISD::FCOS, VT, Expand);
215 setOperationAction(ISD::FREM, VT, Expand);
219 // We have fused multiply-addition for f32 and f64 but not f128.
220 setOperationAction(ISD::FMA, MVT::f32, Legal);
221 setOperationAction(ISD::FMA, MVT::f64, Legal);
222 setOperationAction(ISD::FMA, MVT::f128, Expand);
224 // Needed so that we don't try to implement f128 constant loads using
225 // a load-and-extend of a f80 constant (in cases where the constant
226 // would fit in an f80).
227 setLoadExtAction(ISD::EXTLOAD, MVT::f80, Expand);
229 // Floating-point truncation and stores need to be done separately.
230 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
231 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
232 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
234 // We have 64-bit FPR<->GPR moves, but need special handling for
236 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
237 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
239 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
240 // structure, but VAEND is a no-op.
241 setOperationAction(ISD::VASTART, MVT::Other, Custom);
242 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
243 setOperationAction(ISD::VAEND, MVT::Other, Expand);
245 // We want to use MVC in preference to even a single load/store pair.
246 MaxStoresPerMemcpy = 0;
247 MaxStoresPerMemcpyOptSize = 0;
248 MaxStoresPerMemmove = 0;
249 MaxStoresPerMemmoveOptSize = 0;
252 bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
253 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
254 return Imm.isZero() || Imm.isNegZero();
257 bool SystemZTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
259 // Unaligned accesses should never be slower than the expanded version.
260 // We check specifically for aligned accesses in the few cases where
261 // they are required.
267 //===----------------------------------------------------------------------===//
268 // Inline asm support
269 //===----------------------------------------------------------------------===//
271 TargetLowering::ConstraintType
272 SystemZTargetLowering::getConstraintType(const std::string &Constraint) const {
273 if (Constraint.size() == 1) {
274 switch (Constraint[0]) {
275 case 'a': // Address register
276 case 'd': // Data register (equivalent to 'r')
277 case 'f': // Floating-point register
278 case 'r': // General-purpose register
279 return C_RegisterClass;
281 case 'Q': // Memory with base and unsigned 12-bit displacement
282 case 'R': // Likewise, plus an index
283 case 'S': // Memory with base and signed 20-bit displacement
284 case 'T': // Likewise, plus an index
285 case 'm': // Equivalent to 'T'.
288 case 'I': // Unsigned 8-bit constant
289 case 'J': // Unsigned 12-bit constant
290 case 'K': // Signed 16-bit constant
291 case 'L': // Signed 20-bit displacement (on all targets we support)
292 case 'M': // 0x7fffffff
299 return TargetLowering::getConstraintType(Constraint);
302 TargetLowering::ConstraintWeight SystemZTargetLowering::
303 getSingleConstraintMatchWeight(AsmOperandInfo &info,
304 const char *constraint) const {
305 ConstraintWeight weight = CW_Invalid;
306 Value *CallOperandVal = info.CallOperandVal;
307 // If we don't have a value, we can't do a match,
308 // but allow it at the lowest weight.
309 if (CallOperandVal == NULL)
311 Type *type = CallOperandVal->getType();
312 // Look at the constraint type.
313 switch (*constraint) {
315 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
318 case 'a': // Address register
319 case 'd': // Data register (equivalent to 'r')
320 case 'r': // General-purpose register
321 if (CallOperandVal->getType()->isIntegerTy())
322 weight = CW_Register;
325 case 'f': // Floating-point register
326 if (type->isFloatingPointTy())
327 weight = CW_Register;
330 case 'I': // Unsigned 8-bit constant
331 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
332 if (isUInt<8>(C->getZExtValue()))
333 weight = CW_Constant;
336 case 'J': // Unsigned 12-bit constant
337 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
338 if (isUInt<12>(C->getZExtValue()))
339 weight = CW_Constant;
342 case 'K': // Signed 16-bit constant
343 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
344 if (isInt<16>(C->getSExtValue()))
345 weight = CW_Constant;
348 case 'L': // Signed 20-bit displacement (on all targets we support)
349 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
350 if (isInt<20>(C->getSExtValue()))
351 weight = CW_Constant;
354 case 'M': // 0x7fffffff
355 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal))
356 if (C->getZExtValue() == 0x7fffffff)
357 weight = CW_Constant;
363 std::pair<unsigned, const TargetRegisterClass *> SystemZTargetLowering::
364 getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const {
365 if (Constraint.size() == 1) {
366 // GCC Constraint Letters
367 switch (Constraint[0]) {
369 case 'd': // Data register (equivalent to 'r')
370 case 'r': // General-purpose register
372 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
373 else if (VT == MVT::i128)
374 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
375 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
377 case 'a': // Address register
379 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
380 else if (VT == MVT::i128)
381 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
382 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
384 case 'f': // Floating-point register
386 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
387 else if (VT == MVT::f128)
388 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
389 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
392 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
395 void SystemZTargetLowering::
396 LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
397 std::vector<SDValue> &Ops,
398 SelectionDAG &DAG) const {
399 // Only support length 1 constraints for now.
400 if (Constraint.length() == 1) {
401 switch (Constraint[0]) {
402 case 'I': // Unsigned 8-bit constant
403 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
404 if (isUInt<8>(C->getZExtValue()))
405 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
409 case 'J': // Unsigned 12-bit constant
410 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
411 if (isUInt<12>(C->getZExtValue()))
412 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
416 case 'K': // Signed 16-bit constant
417 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
418 if (isInt<16>(C->getSExtValue()))
419 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
423 case 'L': // Signed 20-bit displacement (on all targets we support)
424 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
425 if (isInt<20>(C->getSExtValue()))
426 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
430 case 'M': // 0x7fffffff
431 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
432 if (C->getZExtValue() == 0x7fffffff)
433 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
438 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
441 //===----------------------------------------------------------------------===//
442 // Calling conventions
443 //===----------------------------------------------------------------------===//
445 #include "SystemZGenCallingConv.inc"
447 // Value is a value that has been passed to us in the location described by VA
448 // (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
449 // any loads onto Chain.
450 static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL,
451 CCValAssign &VA, SDValue Chain,
453 // If the argument has been promoted from a smaller type, insert an
454 // assertion to capture this.
455 if (VA.getLocInfo() == CCValAssign::SExt)
456 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
457 DAG.getValueType(VA.getValVT()));
458 else if (VA.getLocInfo() == CCValAssign::ZExt)
459 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
460 DAG.getValueType(VA.getValVT()));
463 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
464 else if (VA.getLocInfo() == CCValAssign::Indirect)
465 Value = DAG.getLoad(VA.getValVT(), DL, Chain, Value,
466 MachinePointerInfo(), false, false, false, 0);
468 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
472 // Value is a value of type VA.getValVT() that we need to copy into
473 // the location described by VA. Return a copy of Value converted to
474 // VA.getValVT(). The caller is responsible for handling indirect values.
475 static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL,
476 CCValAssign &VA, SDValue Value) {
477 switch (VA.getLocInfo()) {
478 case CCValAssign::SExt:
479 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
480 case CCValAssign::ZExt:
481 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
482 case CCValAssign::AExt:
483 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
484 case CCValAssign::Full:
487 llvm_unreachable("Unhandled getLocInfo()");
491 SDValue SystemZTargetLowering::
492 LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
493 const SmallVectorImpl<ISD::InputArg> &Ins,
494 SDLoc DL, SelectionDAG &DAG,
495 SmallVectorImpl<SDValue> &InVals) const {
496 MachineFunction &MF = DAG.getMachineFunction();
497 MachineFrameInfo *MFI = MF.getFrameInfo();
498 MachineRegisterInfo &MRI = MF.getRegInfo();
499 SystemZMachineFunctionInfo *FuncInfo =
500 MF.getInfo<SystemZMachineFunctionInfo>();
501 const SystemZFrameLowering *TFL =
502 static_cast<const SystemZFrameLowering *>(TM.getFrameLowering());
504 // Assign locations to all of the incoming arguments.
505 SmallVector<CCValAssign, 16> ArgLocs;
506 CCState CCInfo(CallConv, IsVarArg, MF, TM, ArgLocs, *DAG.getContext());
507 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
509 unsigned NumFixedGPRs = 0;
510 unsigned NumFixedFPRs = 0;
511 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
513 CCValAssign &VA = ArgLocs[I];
514 EVT LocVT = VA.getLocVT();
516 // Arguments passed in registers
517 const TargetRegisterClass *RC;
518 switch (LocVT.getSimpleVT().SimpleTy) {
520 // Integers smaller than i64 should be promoted to i64.
521 llvm_unreachable("Unexpected argument type");
524 RC = &SystemZ::GR32BitRegClass;
528 RC = &SystemZ::GR64BitRegClass;
532 RC = &SystemZ::FP32BitRegClass;
536 RC = &SystemZ::FP64BitRegClass;
540 unsigned VReg = MRI.createVirtualRegister(RC);
541 MRI.addLiveIn(VA.getLocReg(), VReg);
542 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
544 assert(VA.isMemLoc() && "Argument not register or memory");
546 // Create the frame index object for this incoming parameter.
547 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
548 VA.getLocMemOffset(), true);
550 // Create the SelectionDAG nodes corresponding to a load
551 // from this parameter. Unpromoted ints and floats are
552 // passed as right-justified 8-byte values.
553 EVT PtrVT = getPointerTy();
554 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
555 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
556 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, DAG.getIntPtrConstant(4));
557 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
558 MachinePointerInfo::getFixedStack(FI),
559 false, false, false, 0);
562 // Convert the value of the argument register into the value that's
564 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
568 // Save the number of non-varargs registers for later use by va_start, etc.
569 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
570 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
572 // Likewise the address (in the form of a frame index) of where the
573 // first stack vararg would be. The 1-byte size here is arbitrary.
574 int64_t StackSize = CCInfo.getNextStackOffset();
575 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true));
577 // ...and a similar frame index for the caller-allocated save area
578 // that will be used to store the incoming registers.
579 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
580 unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true);
581 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
583 // Store the FPR varargs in the reserved frame slots. (We store the
584 // GPRs as part of the prologue.)
585 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
586 SDValue MemOps[SystemZ::NumArgFPRs];
587 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
588 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
589 int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true);
590 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
591 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
592 &SystemZ::FP64BitRegClass);
593 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
594 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
595 MachinePointerInfo::getFixedStack(FI),
599 // Join the stores, which are independent of one another.
600 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
601 &MemOps[NumFixedFPRs],
602 SystemZ::NumArgFPRs - NumFixedFPRs);
610 SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
611 SmallVectorImpl<SDValue> &InVals) const {
612 SelectionDAG &DAG = CLI.DAG;
614 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
615 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
616 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
617 SDValue Chain = CLI.Chain;
618 SDValue Callee = CLI.Callee;
619 bool &isTailCall = CLI.IsTailCall;
620 CallingConv::ID CallConv = CLI.CallConv;
621 bool IsVarArg = CLI.IsVarArg;
622 MachineFunction &MF = DAG.getMachineFunction();
623 EVT PtrVT = getPointerTy();
625 // SystemZ target does not yet support tail call optimization.
628 // Analyze the operands of the call, assigning locations to each operand.
629 SmallVector<CCValAssign, 16> ArgLocs;
630 CCState ArgCCInfo(CallConv, IsVarArg, MF, TM, ArgLocs, *DAG.getContext());
631 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
633 // Get a count of how many bytes are to be pushed on the stack.
634 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
636 // Mark the start of the call.
637 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumBytes, PtrVT, true),
640 // Copy argument values to their designated locations.
641 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
642 SmallVector<SDValue, 8> MemOpChains;
644 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
645 CCValAssign &VA = ArgLocs[I];
646 SDValue ArgValue = OutVals[I];
648 if (VA.getLocInfo() == CCValAssign::Indirect) {
649 // Store the argument in a stack slot and pass its address.
650 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
651 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
652 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, SpillSlot,
653 MachinePointerInfo::getFixedStack(FI),
655 ArgValue = SpillSlot;
657 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
660 // Queue up the argument copies and emit them at the end.
661 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
663 assert(VA.isMemLoc() && "Argument not register or memory");
665 // Work out the address of the stack slot. Unpromoted ints and
666 // floats are passed as right-justified 8-byte values.
667 if (!StackPtr.getNode())
668 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
669 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
670 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
672 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
673 DAG.getIntPtrConstant(Offset));
676 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address,
677 MachinePointerInfo(),
682 // Join the stores, which are independent of one another.
683 if (!MemOpChains.empty())
684 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
685 &MemOpChains[0], MemOpChains.size());
687 // Build a sequence of copy-to-reg nodes, chained and glued together.
689 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
690 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
691 RegsToPass[I].second, Glue);
692 Glue = Chain.getValue(1);
695 // Accept direct calls by converting symbolic call addresses to the
696 // associated Target* opcodes.
697 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
698 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
699 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
700 } else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
701 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
702 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
705 // The first call operand is the chain and the second is the target address.
706 SmallVector<SDValue, 8> Ops;
707 Ops.push_back(Chain);
708 Ops.push_back(Callee);
710 // Add argument registers to the end of the list so that they are
711 // known live into the call.
712 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
713 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
714 RegsToPass[I].second.getValueType()));
716 // Glue the call to the argument copies, if any.
721 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
722 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, &Ops[0], Ops.size());
723 Glue = Chain.getValue(1);
725 // Mark the end of the call, which is glued to the call itself.
726 Chain = DAG.getCALLSEQ_END(Chain,
727 DAG.getConstant(NumBytes, PtrVT, true),
728 DAG.getConstant(0, PtrVT, true),
730 Glue = Chain.getValue(1);
732 // Assign locations to each value returned by this call.
733 SmallVector<CCValAssign, 16> RetLocs;
734 CCState RetCCInfo(CallConv, IsVarArg, MF, TM, RetLocs, *DAG.getContext());
735 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
737 // Copy all of the result registers out of their specified physreg.
738 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
739 CCValAssign &VA = RetLocs[I];
741 // Copy the value out, gluing the copy to the end of the call sequence.
742 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
743 VA.getLocVT(), Glue);
744 Chain = RetValue.getValue(1);
745 Glue = RetValue.getValue(2);
747 // Convert the value of the return register into the value that's
749 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
756 SystemZTargetLowering::LowerReturn(SDValue Chain,
757 CallingConv::ID CallConv, bool IsVarArg,
758 const SmallVectorImpl<ISD::OutputArg> &Outs,
759 const SmallVectorImpl<SDValue> &OutVals,
760 SDLoc DL, SelectionDAG &DAG) const {
761 MachineFunction &MF = DAG.getMachineFunction();
763 // Assign locations to each returned value.
764 SmallVector<CCValAssign, 16> RetLocs;
765 CCState RetCCInfo(CallConv, IsVarArg, MF, TM, RetLocs, *DAG.getContext());
766 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
768 // Quick exit for void returns
770 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
772 // Copy the result values into the output registers.
774 SmallVector<SDValue, 4> RetOps;
775 RetOps.push_back(Chain);
776 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
777 CCValAssign &VA = RetLocs[I];
778 SDValue RetValue = OutVals[I];
780 // Make the return register live on exit.
781 assert(VA.isRegLoc() && "Can only return in registers!");
783 // Promote the value as required.
784 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
786 // Chain and glue the copies together.
787 unsigned Reg = VA.getLocReg();
788 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
789 Glue = Chain.getValue(1);
790 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
793 // Update chain and glue.
796 RetOps.push_back(Glue);
798 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other,
799 RetOps.data(), RetOps.size());
802 // CC is a comparison that will be implemented using an integer or
803 // floating-point comparison. Return the condition code mask for
804 // a branch on true. In the integer case, CCMASK_CMP_UO is set for
805 // unsigned comparisons and clear for signed ones. In the floating-point
806 // case, CCMASK_CMP_UO has its normal mask meaning (unordered).
807 static unsigned CCMaskForCondCode(ISD::CondCode CC) {
809 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
810 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
811 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
815 llvm_unreachable("Invalid integer condition!");
824 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
825 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
830 // If a comparison described by IsUnsigned, CCMask, CmpOp0 and CmpOp1
831 // is suitable for CLI(Y), CHHSI or CLHHSI, adjust the operands as necessary.
832 static void adjustSubwordCmp(SelectionDAG &DAG, bool &IsUnsigned,
833 SDValue &CmpOp0, SDValue &CmpOp1,
835 // For us to make any changes, it must a comparison between a single-use
836 // load and a constant.
837 if (!CmpOp0.hasOneUse() ||
838 CmpOp0.getOpcode() != ISD::LOAD ||
839 CmpOp1.getOpcode() != ISD::Constant)
842 // We must have an 8- or 16-bit load.
843 LoadSDNode *Load = cast<LoadSDNode>(CmpOp0);
844 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
845 if (NumBits != 8 && NumBits != 16)
848 // The load must be an extending one and the constant must be within the
849 // range of the unextended value.
850 ConstantSDNode *Constant = cast<ConstantSDNode>(CmpOp1);
851 uint64_t Value = Constant->getZExtValue();
852 uint64_t Mask = (1 << NumBits) - 1;
853 if (Load->getExtensionType() == ISD::SEXTLOAD) {
854 int64_t SignedValue = Constant->getSExtValue();
855 if (uint64_t(SignedValue) + (1ULL << (NumBits - 1)) > Mask)
857 // Unsigned comparison between two sign-extended values is equivalent
858 // to unsigned comparison between two zero-extended values.
861 else if (CCMask == SystemZ::CCMASK_CMP_EQ ||
862 CCMask == SystemZ::CCMASK_CMP_NE)
863 // Any choice of IsUnsigned is OK for equality comparisons.
864 // We could use either CHHSI or CLHHSI for 16-bit comparisons,
865 // but since we use CLHHSI for zero extensions, it seems better
866 // to be consistent and do the same here.
867 Value &= Mask, IsUnsigned = true;
868 else if (NumBits == 8) {
869 // Try to treat the comparison as unsigned, so that we can use CLI.
870 // Adjust CCMask and Value as necessary.
871 if (Value == 0 && CCMask == SystemZ::CCMASK_CMP_LT)
872 // Test whether the high bit of the byte is set.
873 Value = 127, CCMask = SystemZ::CCMASK_CMP_GT, IsUnsigned = true;
874 else if (SignedValue == -1 && CCMask == SystemZ::CCMASK_CMP_GT)
875 // Test whether the high bit of the byte is clear.
876 Value = 128, CCMask = SystemZ::CCMASK_CMP_LT, IsUnsigned = true;
878 // No instruction exists for this combination.
881 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
884 // Signed comparison between two zero-extended values is equivalent
885 // to unsigned comparison.
890 // Make sure that the first operand is an i32 of the right extension type.
891 ISD::LoadExtType ExtType = IsUnsigned ? ISD::ZEXTLOAD : ISD::SEXTLOAD;
892 if (CmpOp0.getValueType() != MVT::i32 ||
893 Load->getExtensionType() != ExtType)
894 CmpOp0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32,
895 Load->getChain(), Load->getBasePtr(),
896 Load->getPointerInfo(), Load->getMemoryVT(),
897 Load->isVolatile(), Load->isNonTemporal(),
898 Load->getAlignment());
900 // Make sure that the second operand is an i32 with the right value.
901 if (CmpOp1.getValueType() != MVT::i32 ||
902 Value != Constant->getZExtValue())
903 CmpOp1 = DAG.getConstant(Value, MVT::i32);
906 // Return true if a comparison described by CCMask, CmpOp0 and CmpOp1
907 // is an equality comparison that is better implemented using unsigned
908 // rather than signed comparison instructions.
909 static bool preferUnsignedComparison(SelectionDAG &DAG, SDValue CmpOp0,
910 SDValue CmpOp1, unsigned CCMask) {
911 // The test must be for equality or inequality.
912 if (CCMask != SystemZ::CCMASK_CMP_EQ && CCMask != SystemZ::CCMASK_CMP_NE)
915 if (CmpOp1.getOpcode() == ISD::Constant) {
916 uint64_t Value = cast<ConstantSDNode>(CmpOp1)->getSExtValue();
918 // If we're comparing with memory, prefer unsigned comparisons for
919 // values that are in the unsigned 16-bit range but not the signed
920 // 16-bit range. We want to use CLFHSI and CLGHSI.
921 if (CmpOp0.hasOneUse() &&
922 ISD::isNormalLoad(CmpOp0.getNode()) &&
923 (Value >= 32768 && Value < 65536))
926 // Use unsigned comparisons for values that are in the CLGFI range
927 // but not in the CGFI range.
928 if (CmpOp0.getValueType() == MVT::i64 && (Value >> 31) == 1)
934 // Prefer CL for zero-extended loads.
935 if (CmpOp1.getOpcode() == ISD::ZERO_EXTEND ||
936 ISD::isZEXTLoad(CmpOp1.getNode()))
939 // ...and for "in-register" zero extensions.
940 if (CmpOp1.getOpcode() == ISD::AND && CmpOp1.getValueType() == MVT::i64) {
941 SDValue Mask = CmpOp1.getOperand(1);
942 if (Mask.getOpcode() == ISD::Constant &&
943 cast<ConstantSDNode>(Mask)->getZExtValue() == 0xffffffff)
950 // Return a target node that compares CmpOp0 and CmpOp1. Set CCMask to the
951 // 4-bit condition-code mask for CC.
952 static SDValue emitCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
953 ISD::CondCode CC, unsigned &CCMask) {
954 bool IsUnsigned = false;
955 CCMask = CCMaskForCondCode(CC);
956 if (!CmpOp0.getValueType().isFloatingPoint()) {
957 IsUnsigned = CCMask & SystemZ::CCMASK_CMP_UO;
958 CCMask &= ~SystemZ::CCMASK_CMP_UO;
959 adjustSubwordCmp(DAG, IsUnsigned, CmpOp0, CmpOp1, CCMask);
960 if (preferUnsignedComparison(DAG, CmpOp0, CmpOp1, CCMask))
965 return DAG.getNode((IsUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
966 DL, MVT::Glue, CmpOp0, CmpOp1);
969 // Lower a binary operation that produces two VT results, one in each
970 // half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
971 // Extend extends Op0 to a GR128, and Opcode performs the GR128 operation
972 // on the extended Op0 and (unextended) Op1. Store the even register result
973 // in Even and the odd register result in Odd.
974 static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT,
975 unsigned Extend, unsigned Opcode,
976 SDValue Op0, SDValue Op1,
977 SDValue &Even, SDValue &Odd) {
978 SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0);
979 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped,
980 SDValue(In128, 0), Op1);
981 bool Is32Bit = is32Bit(VT);
982 SDValue SubReg0 = DAG.getTargetConstant(SystemZ::even128(Is32Bit), VT);
983 SDValue SubReg1 = DAG.getTargetConstant(SystemZ::odd128(Is32Bit), VT);
984 SDNode *Reg0 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
985 VT, Result, SubReg0);
986 SDNode *Reg1 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
987 VT, Result, SubReg1);
988 Even = SDValue(Reg0, 0);
989 Odd = SDValue(Reg1, 0);
992 SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
993 SDValue Chain = Op.getOperand(0);
994 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
995 SDValue CmpOp0 = Op.getOperand(2);
996 SDValue CmpOp1 = Op.getOperand(3);
997 SDValue Dest = Op.getOperand(4);
1001 SDValue Flags = emitCmp(DAG, CmpOp0, CmpOp1, CC, CCMask);
1002 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
1003 Chain, DAG.getConstant(CCMask, MVT::i32), Dest, Flags);
1006 SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
1007 SelectionDAG &DAG) const {
1008 SDValue CmpOp0 = Op.getOperand(0);
1009 SDValue CmpOp1 = Op.getOperand(1);
1010 SDValue TrueOp = Op.getOperand(2);
1011 SDValue FalseOp = Op.getOperand(3);
1012 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1016 SDValue Flags = emitCmp(DAG, CmpOp0, CmpOp1, CC, CCMask);
1018 SmallVector<SDValue, 4> Ops;
1019 Ops.push_back(TrueOp);
1020 Ops.push_back(FalseOp);
1021 Ops.push_back(DAG.getConstant(CCMask, MVT::i32));
1022 Ops.push_back(Flags);
1024 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
1025 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, &Ops[0], Ops.size());
1028 SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
1029 SelectionDAG &DAG) const {
1031 const GlobalValue *GV = Node->getGlobal();
1032 int64_t Offset = Node->getOffset();
1033 EVT PtrVT = getPointerTy();
1034 Reloc::Model RM = TM.getRelocationModel();
1035 CodeModel::Model CM = TM.getCodeModel();
1038 if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) {
1039 // Make sure that the offset is aligned to a halfword. If it isn't,
1040 // create an "anchor" at the previous 12-bit boundary.
1041 // FIXME check whether there is a better way of handling this.
1043 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT,
1044 Offset & ~uint64_t(0xfff));
1047 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Offset);
1050 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1052 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
1053 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1054 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
1055 MachinePointerInfo::getGOT(), false, false, false, 0);
1058 // If there was a non-zero offset that we didn't fold, create an explicit
1061 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
1062 DAG.getConstant(Offset, PtrVT));
1067 SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
1068 SelectionDAG &DAG) const {
1070 const GlobalValue *GV = Node->getGlobal();
1071 EVT PtrVT = getPointerTy();
1072 TLSModel::Model model = TM.getTLSModel(GV);
1074 if (model != TLSModel::LocalExec)
1075 llvm_unreachable("only local-exec TLS mode supported");
1077 // The high part of the thread pointer is in access register 0.
1078 SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
1079 DAG.getConstant(0, MVT::i32));
1080 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
1082 // The low part of the thread pointer is in access register 1.
1083 SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
1084 DAG.getConstant(1, MVT::i32));
1085 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
1087 // Merge them into a single 64-bit address.
1088 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
1089 DAG.getConstant(32, PtrVT));
1090 SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
1092 // Get the offset of GA from the thread pointer.
1093 SystemZConstantPoolValue *CPV =
1094 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
1096 // Force the offset into the constant pool and load it from there.
1097 SDValue CPAddr = DAG.getConstantPool(CPV, PtrVT, 8);
1098 SDValue Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
1099 CPAddr, MachinePointerInfo::getConstantPool(),
1100 false, false, false, 0);
1102 // Add the base and offset together.
1103 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
1106 SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
1107 SelectionDAG &DAG) const {
1109 const BlockAddress *BA = Node->getBlockAddress();
1110 int64_t Offset = Node->getOffset();
1111 EVT PtrVT = getPointerTy();
1113 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
1114 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1118 SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
1119 SelectionDAG &DAG) const {
1121 EVT PtrVT = getPointerTy();
1122 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1124 // Use LARL to load the address of the table.
1125 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1128 SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
1129 SelectionDAG &DAG) const {
1131 EVT PtrVT = getPointerTy();
1134 if (CP->isMachineConstantPoolEntry())
1135 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1136 CP->getAlignment());
1138 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1139 CP->getAlignment(), CP->getOffset());
1141 // Use LARL to load the address of the constant pool entry.
1142 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1145 SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
1146 SelectionDAG &DAG) const {
1148 SDValue In = Op.getOperand(0);
1149 EVT InVT = In.getValueType();
1150 EVT ResVT = Op.getValueType();
1152 SDValue SubReg32 = DAG.getTargetConstant(SystemZ::subreg_32bit, MVT::i64);
1153 SDValue Shift32 = DAG.getConstant(32, MVT::i64);
1154 if (InVT == MVT::i32 && ResVT == MVT::f32) {
1155 SDValue In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
1156 SDValue Shift = DAG.getNode(ISD::SHL, DL, MVT::i64, In64, Shift32);
1157 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, Shift);
1158 SDNode *Out = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
1159 MVT::f32, Out64, SubReg32);
1160 return SDValue(Out, 0);
1162 if (InVT == MVT::f32 && ResVT == MVT::i32) {
1163 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
1164 SDNode *In64 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
1165 MVT::f64, SDValue(U64, 0), In, SubReg32);
1166 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, SDValue(In64, 0));
1167 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64, Shift32);
1168 SDValue Out = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
1171 llvm_unreachable("Unexpected bitcast combination");
1174 SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
1175 SelectionDAG &DAG) const {
1176 MachineFunction &MF = DAG.getMachineFunction();
1177 SystemZMachineFunctionInfo *FuncInfo =
1178 MF.getInfo<SystemZMachineFunctionInfo>();
1179 EVT PtrVT = getPointerTy();
1181 SDValue Chain = Op.getOperand(0);
1182 SDValue Addr = Op.getOperand(1);
1183 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1186 // The initial values of each field.
1187 const unsigned NumFields = 4;
1188 SDValue Fields[NumFields] = {
1189 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), PtrVT),
1190 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), PtrVT),
1191 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
1192 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
1195 // Store each field into its respective slot.
1196 SDValue MemOps[NumFields];
1197 unsigned Offset = 0;
1198 for (unsigned I = 0; I < NumFields; ++I) {
1199 SDValue FieldAddr = Addr;
1201 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
1202 DAG.getIntPtrConstant(Offset));
1203 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
1204 MachinePointerInfo(SV, Offset),
1208 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps, NumFields);
1211 SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
1212 SelectionDAG &DAG) const {
1213 SDValue Chain = Op.getOperand(0);
1214 SDValue DstPtr = Op.getOperand(1);
1215 SDValue SrcPtr = Op.getOperand(2);
1216 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
1217 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
1220 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32),
1221 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
1222 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
1225 SDValue SystemZTargetLowering::
1226 lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
1227 SDValue Chain = Op.getOperand(0);
1228 SDValue Size = Op.getOperand(1);
1231 unsigned SPReg = getStackPointerRegisterToSaveRestore();
1233 // Get a reference to the stack pointer.
1234 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
1236 // Get the new stack pointer value.
1237 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, Size);
1239 // Copy the new stack pointer back.
1240 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
1242 // The allocated data lives above the 160 bytes allocated for the standard
1243 // frame, plus any outgoing stack arguments. We don't know how much that
1244 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
1245 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
1246 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
1248 SDValue Ops[2] = { Result, Chain };
1249 return DAG.getMergeValues(Ops, 2, DL);
1252 SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
1253 SelectionDAG &DAG) const {
1254 EVT VT = Op.getValueType();
1256 assert(!is32Bit(VT) && "Only support 64-bit UMUL_LOHI");
1258 // UMUL_LOHI64 returns the low result in the odd register and the high
1259 // result in the even register. UMUL_LOHI is defined to return the
1260 // low half first, so the results are in reverse order.
1262 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
1263 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
1264 return DAG.getMergeValues(Ops, 2, DL);
1267 SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
1268 SelectionDAG &DAG) const {
1269 SDValue Op0 = Op.getOperand(0);
1270 SDValue Op1 = Op.getOperand(1);
1271 EVT VT = Op.getValueType();
1275 // We use DSGF for 32-bit division.
1277 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
1278 Opcode = SystemZISD::SDIVREM32;
1279 } else if (DAG.ComputeNumSignBits(Op1) > 32) {
1280 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
1281 Opcode = SystemZISD::SDIVREM32;
1283 Opcode = SystemZISD::SDIVREM64;
1285 // DSG(F) takes a 64-bit dividend, so the even register in the GR128
1286 // input is "don't care". The instruction returns the remainder in
1287 // the even register and the quotient in the odd register.
1289 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
1290 Op0, Op1, Ops[1], Ops[0]);
1291 return DAG.getMergeValues(Ops, 2, DL);
1294 SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
1295 SelectionDAG &DAG) const {
1296 EVT VT = Op.getValueType();
1299 // DL(G) uses a double-width dividend, so we need to clear the even
1300 // register in the GR128 input. The instruction returns the remainder
1301 // in the even register and the quotient in the odd register.
1304 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
1305 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
1307 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
1308 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
1309 return DAG.getMergeValues(Ops, 2, DL);
1312 SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
1313 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
1315 // Get the known-zero masks for each operand.
1316 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
1317 APInt KnownZero[2], KnownOne[2];
1318 DAG.ComputeMaskedBits(Ops[0], KnownZero[0], KnownOne[0]);
1319 DAG.ComputeMaskedBits(Ops[1], KnownZero[1], KnownOne[1]);
1321 // See if the upper 32 bits of one operand and the lower 32 bits of the
1322 // other are known zero. They are the low and high operands respectively.
1323 uint64_t Masks[] = { KnownZero[0].getZExtValue(),
1324 KnownZero[1].getZExtValue() };
1326 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
1328 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
1333 SDValue LowOp = Ops[Low];
1334 SDValue HighOp = Ops[High];
1336 // If the high part is a constant, we're better off using IILH.
1337 if (HighOp.getOpcode() == ISD::Constant)
1340 // If the low part is a constant that is outside the range of LHI,
1341 // then we're better off using IILF.
1342 if (LowOp.getOpcode() == ISD::Constant) {
1343 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
1344 if (!isInt<16>(Value))
1348 // Check whether the high part is an AND that doesn't change the
1349 // high 32 bits and just masks out low bits. We can skip it if so.
1350 if (HighOp.getOpcode() == ISD::AND &&
1351 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
1352 ConstantSDNode *MaskNode = cast<ConstantSDNode>(HighOp.getOperand(1));
1353 uint64_t Mask = MaskNode->getZExtValue() | Masks[High];
1354 if ((Mask >> 32) == 0xffffffff)
1355 HighOp = HighOp.getOperand(0);
1358 // Take advantage of the fact that all GR32 operations only change the
1359 // low 32 bits by truncating Low to an i32 and inserting it directly
1360 // using a subreg. The interesting cases are those where the truncation
1363 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
1364 SDValue SubReg32 = DAG.getTargetConstant(SystemZ::subreg_32bit, MVT::i64);
1365 SDNode *Result = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
1366 MVT::i64, HighOp, Low32, SubReg32);
1367 return SDValue(Result, 0);
1370 // Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
1371 // two into the fullword ATOMIC_LOADW_* operation given by Opcode.
1372 SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
1374 unsigned Opcode) const {
1375 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode());
1377 // 32-bit operations need no code outside the main loop.
1378 EVT NarrowVT = Node->getMemoryVT();
1379 EVT WideVT = MVT::i32;
1380 if (NarrowVT == WideVT)
1383 int64_t BitSize = NarrowVT.getSizeInBits();
1384 SDValue ChainIn = Node->getChain();
1385 SDValue Addr = Node->getBasePtr();
1386 SDValue Src2 = Node->getVal();
1387 MachineMemOperand *MMO = Node->getMemOperand();
1389 EVT PtrVT = Addr.getValueType();
1391 // Convert atomic subtracts of constants into additions.
1392 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
1393 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(Src2)) {
1394 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
1395 Src2 = DAG.getConstant(-Const->getSExtValue(), Src2.getValueType());
1398 // Get the address of the containing word.
1399 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
1400 DAG.getConstant(-4, PtrVT));
1402 // Get the number of bits that the word must be rotated left in order
1403 // to bring the field to the top bits of a GR32.
1404 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
1405 DAG.getConstant(3, PtrVT));
1406 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
1408 // Get the complementing shift amount, for rotating a field in the top
1409 // bits back to its proper position.
1410 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
1411 DAG.getConstant(0, WideVT), BitShift);
1413 // Extend the source operand to 32 bits and prepare it for the inner loop.
1414 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
1415 // operations require the source to be shifted in advance. (This shift
1416 // can be folded if the source is constant.) For AND and NAND, the lower
1417 // bits must be set, while for other opcodes they should be left clear.
1418 if (Opcode != SystemZISD::ATOMIC_SWAPW)
1419 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
1420 DAG.getConstant(32 - BitSize, WideVT));
1421 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
1422 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
1423 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
1424 DAG.getConstant(uint32_t(-1) >> BitSize, WideVT));
1426 // Construct the ATOMIC_LOADW_* node.
1427 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
1428 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
1429 DAG.getConstant(BitSize, WideVT) };
1430 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
1431 array_lengthof(Ops),
1434 // Rotate the result of the final CS so that the field is in the lower
1435 // bits of a GR32, then truncate it.
1436 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
1437 DAG.getConstant(BitSize, WideVT));
1438 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
1440 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
1441 return DAG.getMergeValues(RetOps, 2, DL);
1444 // Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
1445 // into a fullword ATOMIC_CMP_SWAPW operation.
1446 SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
1447 SelectionDAG &DAG) const {
1448 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode());
1450 // We have native support for 32-bit compare and swap.
1451 EVT NarrowVT = Node->getMemoryVT();
1452 EVT WideVT = MVT::i32;
1453 if (NarrowVT == WideVT)
1456 int64_t BitSize = NarrowVT.getSizeInBits();
1457 SDValue ChainIn = Node->getOperand(0);
1458 SDValue Addr = Node->getOperand(1);
1459 SDValue CmpVal = Node->getOperand(2);
1460 SDValue SwapVal = Node->getOperand(3);
1461 MachineMemOperand *MMO = Node->getMemOperand();
1463 EVT PtrVT = Addr.getValueType();
1465 // Get the address of the containing word.
1466 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
1467 DAG.getConstant(-4, PtrVT));
1469 // Get the number of bits that the word must be rotated left in order
1470 // to bring the field to the top bits of a GR32.
1471 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
1472 DAG.getConstant(3, PtrVT));
1473 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
1475 // Get the complementing shift amount, for rotating a field in the top
1476 // bits back to its proper position.
1477 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
1478 DAG.getConstant(0, WideVT), BitShift);
1480 // Construct the ATOMIC_CMP_SWAPW node.
1481 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
1482 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
1483 NegBitShift, DAG.getConstant(BitSize, WideVT) };
1484 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
1485 VTList, Ops, array_lengthof(Ops),
1490 SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
1491 SelectionDAG &DAG) const {
1492 MachineFunction &MF = DAG.getMachineFunction();
1493 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
1494 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
1495 SystemZ::R15D, Op.getValueType());
1498 SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
1499 SelectionDAG &DAG) const {
1500 MachineFunction &MF = DAG.getMachineFunction();
1501 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
1502 return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op),
1503 SystemZ::R15D, Op.getOperand(1));
1506 SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
1507 SelectionDAG &DAG) const {
1508 switch (Op.getOpcode()) {
1510 return lowerBR_CC(Op, DAG);
1511 case ISD::SELECT_CC:
1512 return lowerSELECT_CC(Op, DAG);
1513 case ISD::GlobalAddress:
1514 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
1515 case ISD::GlobalTLSAddress:
1516 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
1517 case ISD::BlockAddress:
1518 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
1519 case ISD::JumpTable:
1520 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
1521 case ISD::ConstantPool:
1522 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
1524 return lowerBITCAST(Op, DAG);
1526 return lowerVASTART(Op, DAG);
1528 return lowerVACOPY(Op, DAG);
1529 case ISD::DYNAMIC_STACKALLOC:
1530 return lowerDYNAMIC_STACKALLOC(Op, DAG);
1531 case ISD::UMUL_LOHI:
1532 return lowerUMUL_LOHI(Op, DAG);
1534 return lowerSDIVREM(Op, DAG);
1536 return lowerUDIVREM(Op, DAG);
1538 return lowerOR(Op, DAG);
1539 case ISD::ATOMIC_SWAP:
1540 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_SWAPW);
1541 case ISD::ATOMIC_LOAD_ADD:
1542 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
1543 case ISD::ATOMIC_LOAD_SUB:
1544 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
1545 case ISD::ATOMIC_LOAD_AND:
1546 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
1547 case ISD::ATOMIC_LOAD_OR:
1548 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
1549 case ISD::ATOMIC_LOAD_XOR:
1550 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
1551 case ISD::ATOMIC_LOAD_NAND:
1552 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
1553 case ISD::ATOMIC_LOAD_MIN:
1554 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
1555 case ISD::ATOMIC_LOAD_MAX:
1556 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
1557 case ISD::ATOMIC_LOAD_UMIN:
1558 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
1559 case ISD::ATOMIC_LOAD_UMAX:
1560 return lowerATOMIC_LOAD(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
1561 case ISD::ATOMIC_CMP_SWAP:
1562 return lowerATOMIC_CMP_SWAP(Op, DAG);
1563 case ISD::STACKSAVE:
1564 return lowerSTACKSAVE(Op, DAG);
1565 case ISD::STACKRESTORE:
1566 return lowerSTACKRESTORE(Op, DAG);
1568 llvm_unreachable("Unexpected node to lower");
1572 const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
1573 #define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
1577 OPCODE(PCREL_WRAPPER);
1581 OPCODE(SELECT_CCMASK);
1582 OPCODE(ADJDYNALLOC);
1583 OPCODE(EXTRACT_ACCESS);
1584 OPCODE(UMUL_LOHI64);
1589 OPCODE(ATOMIC_SWAPW);
1590 OPCODE(ATOMIC_LOADW_ADD);
1591 OPCODE(ATOMIC_LOADW_SUB);
1592 OPCODE(ATOMIC_LOADW_AND);
1593 OPCODE(ATOMIC_LOADW_OR);
1594 OPCODE(ATOMIC_LOADW_XOR);
1595 OPCODE(ATOMIC_LOADW_NAND);
1596 OPCODE(ATOMIC_LOADW_MIN);
1597 OPCODE(ATOMIC_LOADW_MAX);
1598 OPCODE(ATOMIC_LOADW_UMIN);
1599 OPCODE(ATOMIC_LOADW_UMAX);
1600 OPCODE(ATOMIC_CMP_SWAPW);
1606 //===----------------------------------------------------------------------===//
1608 //===----------------------------------------------------------------------===//
1610 // Create a new basic block after MBB.
1611 static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
1612 MachineFunction &MF = *MBB->getParent();
1613 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
1614 MF.insert(llvm::next(MachineFunction::iterator(MBB)), NewMBB);
1618 // Split MBB after MI and return the new block (the one that contains
1619 // instructions after MI).
1620 static MachineBasicBlock *splitBlockAfter(MachineInstr *MI,
1621 MachineBasicBlock *MBB) {
1622 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
1623 NewMBB->splice(NewMBB->begin(), MBB,
1624 llvm::next(MachineBasicBlock::iterator(MI)),
1626 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
1630 bool SystemZTargetLowering::
1631 convertPrevCompareToBranch(MachineBasicBlock *MBB,
1632 MachineBasicBlock::iterator MBBI,
1633 unsigned CCMask, MachineBasicBlock *Target) const {
1634 MachineBasicBlock::iterator Compare = MBBI;
1635 MachineBasicBlock::iterator Begin = MBB->begin();
1638 if (Compare == Begin)
1642 while (Compare->isDebugValue());
1644 const SystemZInstrInfo *TII = TM.getInstrInfo();
1645 unsigned FusedOpcode = TII->getCompareAndBranch(Compare->getOpcode(),
1650 DebugLoc DL = Compare->getDebugLoc();
1651 BuildMI(*MBB, MBBI, DL, TII->get(FusedOpcode))
1652 .addOperand(Compare->getOperand(0)).addOperand(Compare->getOperand(1))
1653 .addImm(CCMask).addMBB(Target);
1654 Compare->removeFromParent();
1658 // Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
1660 SystemZTargetLowering::emitSelect(MachineInstr *MI,
1661 MachineBasicBlock *MBB) const {
1662 const SystemZInstrInfo *TII = TM.getInstrInfo();
1664 unsigned DestReg = MI->getOperand(0).getReg();
1665 unsigned TrueReg = MI->getOperand(1).getReg();
1666 unsigned FalseReg = MI->getOperand(2).getReg();
1667 unsigned CCMask = MI->getOperand(3).getImm();
1668 DebugLoc DL = MI->getDebugLoc();
1670 MachineBasicBlock *StartMBB = MBB;
1671 MachineBasicBlock *JoinMBB = splitBlockAfter(MI, MBB);
1672 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
1675 // BRC CCMask, JoinMBB
1676 // # fallthrough to FalseMBB
1678 // The original DAG glues comparisons to their uses, both to ensure
1679 // that no CC-clobbering instructions are inserted between them, and
1680 // to ensure that comparison results are not reused. This means that
1681 // this Select is the sole user of any preceding comparison instruction
1682 // and that we can try to use a fused compare and branch instead.
1684 if (!convertPrevCompareToBranch(MBB, MI, CCMask, JoinMBB))
1685 BuildMI(MBB, DL, TII->get(SystemZ::BRC)).addImm(CCMask).addMBB(JoinMBB);
1686 MBB->addSuccessor(JoinMBB);
1687 MBB->addSuccessor(FalseMBB);
1690 // # fallthrough to JoinMBB
1692 MBB->addSuccessor(JoinMBB);
1695 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
1698 BuildMI(*MBB, MBB->begin(), DL, TII->get(SystemZ::PHI), DestReg)
1699 .addReg(TrueReg).addMBB(StartMBB)
1700 .addReg(FalseReg).addMBB(FalseMBB);
1702 MI->eraseFromParent();
1706 // Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
1707 // StoreOpcode is the store to use and Invert says whether the store should
1708 // happen when the condition is false rather than true.
1710 SystemZTargetLowering::emitCondStore(MachineInstr *MI,
1711 MachineBasicBlock *MBB,
1712 unsigned StoreOpcode, bool Invert) const {
1713 const SystemZInstrInfo *TII = TM.getInstrInfo();
1715 MachineOperand Base = MI->getOperand(0);
1716 int64_t Disp = MI->getOperand(1).getImm();
1717 unsigned IndexReg = MI->getOperand(2).getReg();
1718 unsigned SrcReg = MI->getOperand(3).getReg();
1719 unsigned CCMask = MI->getOperand(4).getImm();
1720 DebugLoc DL = MI->getDebugLoc();
1722 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
1724 // Get the condition needed to branch around the store.
1726 CCMask = CCMask ^ SystemZ::CCMASK_ANY;
1728 MachineBasicBlock *StartMBB = MBB;
1729 MachineBasicBlock *JoinMBB = splitBlockAfter(MI, MBB);
1730 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
1733 // BRC CCMask, JoinMBB
1734 // # fallthrough to FalseMBB
1736 // The original DAG glues comparisons to their uses, both to ensure
1737 // that no CC-clobbering instructions are inserted between them, and
1738 // to ensure that comparison results are not reused. This means that
1739 // this CondStore is the sole user of any preceding comparison instruction
1740 // and that we can try to use a fused compare and branch instead.
1742 if (!convertPrevCompareToBranch(MBB, MI, CCMask, JoinMBB))
1743 BuildMI(MBB, DL, TII->get(SystemZ::BRC)).addImm(CCMask).addMBB(JoinMBB);
1744 MBB->addSuccessor(JoinMBB);
1745 MBB->addSuccessor(FalseMBB);
1748 // store %SrcReg, %Disp(%Index,%Base)
1749 // # fallthrough to JoinMBB
1751 BuildMI(MBB, DL, TII->get(StoreOpcode))
1752 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
1753 MBB->addSuccessor(JoinMBB);
1755 MI->eraseFromParent();
1759 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
1760 // or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
1761 // performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
1762 // BitSize is the width of the field in bits, or 0 if this is a partword
1763 // ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
1764 // is one of the operands. Invert says whether the field should be
1765 // inverted after performing BinOpcode (e.g. for NAND).
1767 SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI,
1768 MachineBasicBlock *MBB,
1771 bool Invert) const {
1772 const SystemZInstrInfo *TII = TM.getInstrInfo();
1773 MachineFunction &MF = *MBB->getParent();
1774 MachineRegisterInfo &MRI = MF.getRegInfo();
1775 unsigned MaskNE = CCMaskForCondCode(ISD::SETNE);
1776 bool IsSubWord = (BitSize < 32);
1778 // Extract the operands. Base can be a register or a frame index.
1779 // Src2 can be a register or immediate.
1780 unsigned Dest = MI->getOperand(0).getReg();
1781 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
1782 int64_t Disp = MI->getOperand(2).getImm();
1783 MachineOperand Src2 = earlyUseOperand(MI->getOperand(3));
1784 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
1785 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
1786 DebugLoc DL = MI->getDebugLoc();
1788 BitSize = MI->getOperand(6).getImm();
1790 // Subword operations use 32-bit registers.
1791 const TargetRegisterClass *RC = (BitSize <= 32 ?
1792 &SystemZ::GR32BitRegClass :
1793 &SystemZ::GR64BitRegClass);
1794 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
1795 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
1797 // Get the right opcodes for the displacement.
1798 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
1799 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
1800 assert(LOpcode && CSOpcode && "Displacement out of range");
1802 // Create virtual registers for temporary results.
1803 unsigned OrigVal = MRI.createVirtualRegister(RC);
1804 unsigned OldVal = MRI.createVirtualRegister(RC);
1805 unsigned NewVal = (BinOpcode || IsSubWord ?
1806 MRI.createVirtualRegister(RC) : Src2.getReg());
1807 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
1808 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
1810 // Insert a basic block for the main loop.
1811 MachineBasicBlock *StartMBB = MBB;
1812 MachineBasicBlock *DoneMBB = splitBlockAfter(MI, MBB);
1813 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
1817 // %OrigVal = L Disp(%Base)
1818 // # fall through to LoopMMB
1820 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
1821 .addOperand(Base).addImm(Disp).addReg(0);
1822 MBB->addSuccessor(LoopMBB);
1825 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
1826 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
1827 // %RotatedNewVal = OP %RotatedOldVal, %Src2
1828 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
1829 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
1831 // # fall through to DoneMMB
1833 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
1834 .addReg(OrigVal).addMBB(StartMBB)
1835 .addReg(Dest).addMBB(LoopMBB);
1837 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
1838 .addReg(OldVal).addReg(BitShift).addImm(0);
1840 // Perform the operation normally and then invert every bit of the field.
1841 unsigned Tmp = MRI.createVirtualRegister(RC);
1842 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
1843 .addReg(RotatedOldVal).addOperand(Src2);
1845 // XILF with the upper BitSize bits set.
1846 BuildMI(MBB, DL, TII->get(SystemZ::XILF32), RotatedNewVal)
1847 .addReg(Tmp).addImm(uint32_t(~0 << (32 - BitSize)));
1848 else if (BitSize == 32)
1849 // XILF with every bit set.
1850 BuildMI(MBB, DL, TII->get(SystemZ::XILF32), RotatedNewVal)
1851 .addReg(Tmp).addImm(~uint32_t(0));
1853 // Use LCGR and add -1 to the result, which is more compact than
1854 // an XILF, XILH pair.
1855 unsigned Tmp2 = MRI.createVirtualRegister(RC);
1856 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
1857 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
1858 .addReg(Tmp2).addImm(-1);
1860 } else if (BinOpcode)
1861 // A simply binary operation.
1862 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
1863 .addReg(RotatedOldVal).addOperand(Src2);
1865 // Use RISBG to rotate Src2 into position and use it to replace the
1866 // field in RotatedOldVal.
1867 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
1868 .addReg(RotatedOldVal).addReg(Src2.getReg())
1869 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
1871 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
1872 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
1873 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
1874 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
1875 BuildMI(MBB, DL, TII->get(SystemZ::BRC)).addImm(MaskNE).addMBB(LoopMBB);
1876 MBB->addSuccessor(LoopMBB);
1877 MBB->addSuccessor(DoneMBB);
1879 MI->eraseFromParent();
1883 // Implement EmitInstrWithCustomInserter for pseudo
1884 // ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
1885 // instruction that should be used to compare the current field with the
1886 // minimum or maximum value. KeepOldMask is the BRC condition-code mask
1887 // for when the current field should be kept. BitSize is the width of
1888 // the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
1890 SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI,
1891 MachineBasicBlock *MBB,
1892 unsigned CompareOpcode,
1893 unsigned KeepOldMask,
1894 unsigned BitSize) const {
1895 const SystemZInstrInfo *TII = TM.getInstrInfo();
1896 MachineFunction &MF = *MBB->getParent();
1897 MachineRegisterInfo &MRI = MF.getRegInfo();
1898 unsigned MaskNE = CCMaskForCondCode(ISD::SETNE);
1899 bool IsSubWord = (BitSize < 32);
1901 // Extract the operands. Base can be a register or a frame index.
1902 unsigned Dest = MI->getOperand(0).getReg();
1903 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
1904 int64_t Disp = MI->getOperand(2).getImm();
1905 unsigned Src2 = MI->getOperand(3).getReg();
1906 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
1907 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
1908 DebugLoc DL = MI->getDebugLoc();
1910 BitSize = MI->getOperand(6).getImm();
1912 // Subword operations use 32-bit registers.
1913 const TargetRegisterClass *RC = (BitSize <= 32 ?
1914 &SystemZ::GR32BitRegClass :
1915 &SystemZ::GR64BitRegClass);
1916 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
1917 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
1919 // Get the right opcodes for the displacement.
1920 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
1921 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
1922 assert(LOpcode && CSOpcode && "Displacement out of range");
1924 // Create virtual registers for temporary results.
1925 unsigned OrigVal = MRI.createVirtualRegister(RC);
1926 unsigned OldVal = MRI.createVirtualRegister(RC);
1927 unsigned NewVal = MRI.createVirtualRegister(RC);
1928 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
1929 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
1930 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
1932 // Insert 3 basic blocks for the loop.
1933 MachineBasicBlock *StartMBB = MBB;
1934 MachineBasicBlock *DoneMBB = splitBlockAfter(MI, MBB);
1935 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
1936 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
1937 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
1941 // %OrigVal = L Disp(%Base)
1942 // # fall through to LoopMMB
1944 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
1945 .addOperand(Base).addImm(Disp).addReg(0);
1946 MBB->addSuccessor(LoopMBB);
1949 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
1950 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
1951 // CompareOpcode %RotatedOldVal, %Src2
1952 // BRC KeepOldMask, UpdateMBB
1954 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
1955 .addReg(OrigVal).addMBB(StartMBB)
1956 .addReg(Dest).addMBB(UpdateMBB);
1958 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
1959 .addReg(OldVal).addReg(BitShift).addImm(0);
1960 unsigned FusedOpcode = TII->getCompareAndBranch(CompareOpcode);
1962 BuildMI(MBB, DL, TII->get(FusedOpcode))
1963 .addReg(RotatedOldVal).addReg(Src2)
1964 .addImm(KeepOldMask).addMBB(UpdateMBB);
1966 BuildMI(MBB, DL, TII->get(CompareOpcode))
1967 .addReg(RotatedOldVal).addReg(Src2);
1968 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
1969 .addImm(KeepOldMask).addMBB(UpdateMBB);
1971 MBB->addSuccessor(UpdateMBB);
1972 MBB->addSuccessor(UseAltMBB);
1975 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
1976 // # fall through to UpdateMMB
1979 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
1980 .addReg(RotatedOldVal).addReg(Src2)
1981 .addImm(32).addImm(31 + BitSize).addImm(0);
1982 MBB->addSuccessor(UpdateMBB);
1985 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
1986 // [ %RotatedAltVal, UseAltMBB ]
1987 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
1988 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
1990 // # fall through to DoneMMB
1992 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
1993 .addReg(RotatedOldVal).addMBB(LoopMBB)
1994 .addReg(RotatedAltVal).addMBB(UseAltMBB);
1996 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
1997 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
1998 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
1999 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
2000 BuildMI(MBB, DL, TII->get(SystemZ::BRC)).addImm(MaskNE).addMBB(LoopMBB);
2001 MBB->addSuccessor(LoopMBB);
2002 MBB->addSuccessor(DoneMBB);
2004 MI->eraseFromParent();
2008 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
2011 SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI,
2012 MachineBasicBlock *MBB) const {
2013 const SystemZInstrInfo *TII = TM.getInstrInfo();
2014 MachineFunction &MF = *MBB->getParent();
2015 MachineRegisterInfo &MRI = MF.getRegInfo();
2016 unsigned MaskNE = CCMaskForCondCode(ISD::SETNE);
2018 // Extract the operands. Base can be a register or a frame index.
2019 unsigned Dest = MI->getOperand(0).getReg();
2020 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
2021 int64_t Disp = MI->getOperand(2).getImm();
2022 unsigned OrigCmpVal = MI->getOperand(3).getReg();
2023 unsigned OrigSwapVal = MI->getOperand(4).getReg();
2024 unsigned BitShift = MI->getOperand(5).getReg();
2025 unsigned NegBitShift = MI->getOperand(6).getReg();
2026 int64_t BitSize = MI->getOperand(7).getImm();
2027 DebugLoc DL = MI->getDebugLoc();
2029 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
2031 // Get the right opcodes for the displacement.
2032 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
2033 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
2034 assert(LOpcode && CSOpcode && "Displacement out of range");
2036 // Create virtual registers for temporary results.
2037 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
2038 unsigned OldVal = MRI.createVirtualRegister(RC);
2039 unsigned CmpVal = MRI.createVirtualRegister(RC);
2040 unsigned SwapVal = MRI.createVirtualRegister(RC);
2041 unsigned StoreVal = MRI.createVirtualRegister(RC);
2042 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
2043 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
2044 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
2046 // Insert 2 basic blocks for the loop.
2047 MachineBasicBlock *StartMBB = MBB;
2048 MachineBasicBlock *DoneMBB = splitBlockAfter(MI, MBB);
2049 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
2050 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
2054 // %OrigOldVal = L Disp(%Base)
2055 // # fall through to LoopMMB
2057 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
2058 .addOperand(Base).addImm(Disp).addReg(0);
2059 MBB->addSuccessor(LoopMBB);
2062 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
2063 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
2064 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
2065 // %Dest = RLL %OldVal, BitSize(%BitShift)
2066 // ^^ The low BitSize bits contain the field
2068 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
2069 // ^^ Replace the upper 32-BitSize bits of the
2070 // comparison value with those that we loaded,
2071 // so that we can use a full word comparison.
2072 // CRJNE %Dest, %RetryCmpVal, DoneMBB
2073 // # Fall through to SetMBB
2075 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2076 .addReg(OrigOldVal).addMBB(StartMBB)
2077 .addReg(RetryOldVal).addMBB(SetMBB);
2078 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
2079 .addReg(OrigCmpVal).addMBB(StartMBB)
2080 .addReg(RetryCmpVal).addMBB(SetMBB);
2081 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
2082 .addReg(OrigSwapVal).addMBB(StartMBB)
2083 .addReg(RetrySwapVal).addMBB(SetMBB);
2084 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
2085 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
2086 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
2087 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
2088 BuildMI(MBB, DL, TII->get(SystemZ::CRJ))
2089 .addReg(Dest).addReg(RetryCmpVal)
2090 .addImm(MaskNE).addMBB(DoneMBB);
2091 MBB->addSuccessor(DoneMBB);
2092 MBB->addSuccessor(SetMBB);
2095 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
2096 // ^^ Replace the upper 32-BitSize bits of the new
2097 // value with those that we loaded.
2098 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
2099 // ^^ Rotate the new field to its proper position.
2100 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
2102 // # fall through to ExitMMB
2104 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
2105 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
2106 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
2107 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
2108 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
2109 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
2110 BuildMI(MBB, DL, TII->get(SystemZ::BRC)).addImm(MaskNE).addMBB(LoopMBB);
2111 MBB->addSuccessor(LoopMBB);
2112 MBB->addSuccessor(DoneMBB);
2114 MI->eraseFromParent();
2118 // Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true
2119 // if the high register of the GR128 value must be cleared or false if
2120 // it's "don't care". SubReg is subreg_odd32 when extending a GR32
2121 // and subreg_odd when extending a GR64.
2123 SystemZTargetLowering::emitExt128(MachineInstr *MI,
2124 MachineBasicBlock *MBB,
2125 bool ClearEven, unsigned SubReg) const {
2126 const SystemZInstrInfo *TII = TM.getInstrInfo();
2127 MachineFunction &MF = *MBB->getParent();
2128 MachineRegisterInfo &MRI = MF.getRegInfo();
2129 DebugLoc DL = MI->getDebugLoc();
2131 unsigned Dest = MI->getOperand(0).getReg();
2132 unsigned Src = MI->getOperand(1).getReg();
2133 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
2135 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
2137 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
2138 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
2140 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
2142 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
2143 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_high);
2146 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
2147 .addReg(In128).addReg(Src).addImm(SubReg);
2149 MI->eraseFromParent();
2154 SystemZTargetLowering::emitMVCWrapper(MachineInstr *MI,
2155 MachineBasicBlock *MBB) const {
2156 const SystemZInstrInfo *TII = TM.getInstrInfo();
2157 DebugLoc DL = MI->getDebugLoc();
2159 MachineOperand DestBase = MI->getOperand(0);
2160 uint64_t DestDisp = MI->getOperand(1).getImm();
2161 MachineOperand SrcBase = MI->getOperand(2);
2162 uint64_t SrcDisp = MI->getOperand(3).getImm();
2163 uint64_t Length = MI->getOperand(4).getImm();
2165 BuildMI(*MBB, MI, DL, TII->get(SystemZ::MVC))
2166 .addOperand(DestBase).addImm(DestDisp).addImm(Length)
2167 .addOperand(SrcBase).addImm(SrcDisp);
2169 MI->eraseFromParent();
2173 MachineBasicBlock *SystemZTargetLowering::
2174 EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const {
2175 switch (MI->getOpcode()) {
2176 case SystemZ::Select32:
2177 case SystemZ::SelectF32:
2178 case SystemZ::Select64:
2179 case SystemZ::SelectF64:
2180 case SystemZ::SelectF128:
2181 return emitSelect(MI, MBB);
2183 case SystemZ::CondStore8_32:
2184 return emitCondStore(MI, MBB, SystemZ::STC32, false);
2185 case SystemZ::CondStore8_32Inv:
2186 return emitCondStore(MI, MBB, SystemZ::STC32, true);
2187 case SystemZ::CondStore16_32:
2188 return emitCondStore(MI, MBB, SystemZ::STH32, false);
2189 case SystemZ::CondStore16_32Inv:
2190 return emitCondStore(MI, MBB, SystemZ::STH32, true);
2191 case SystemZ::CondStore32_32:
2192 return emitCondStore(MI, MBB, SystemZ::ST32, false);
2193 case SystemZ::CondStore32_32Inv:
2194 return emitCondStore(MI, MBB, SystemZ::ST32, true);
2195 case SystemZ::CondStore8:
2196 return emitCondStore(MI, MBB, SystemZ::STC, false);
2197 case SystemZ::CondStore8Inv:
2198 return emitCondStore(MI, MBB, SystemZ::STC, true);
2199 case SystemZ::CondStore16:
2200 return emitCondStore(MI, MBB, SystemZ::STH, false);
2201 case SystemZ::CondStore16Inv:
2202 return emitCondStore(MI, MBB, SystemZ::STH, true);
2203 case SystemZ::CondStore32:
2204 return emitCondStore(MI, MBB, SystemZ::ST, false);
2205 case SystemZ::CondStore32Inv:
2206 return emitCondStore(MI, MBB, SystemZ::ST, true);
2207 case SystemZ::CondStore64:
2208 return emitCondStore(MI, MBB, SystemZ::STG, false);
2209 case SystemZ::CondStore64Inv:
2210 return emitCondStore(MI, MBB, SystemZ::STG, true);
2211 case SystemZ::CondStoreF32:
2212 return emitCondStore(MI, MBB, SystemZ::STE, false);
2213 case SystemZ::CondStoreF32Inv:
2214 return emitCondStore(MI, MBB, SystemZ::STE, true);
2215 case SystemZ::CondStoreF64:
2216 return emitCondStore(MI, MBB, SystemZ::STD, false);
2217 case SystemZ::CondStoreF64Inv:
2218 return emitCondStore(MI, MBB, SystemZ::STD, true);
2220 case SystemZ::AEXT128_64:
2221 return emitExt128(MI, MBB, false, SystemZ::subreg_low);
2222 case SystemZ::ZEXT128_32:
2223 return emitExt128(MI, MBB, true, SystemZ::subreg_low32);
2224 case SystemZ::ZEXT128_64:
2225 return emitExt128(MI, MBB, true, SystemZ::subreg_low);
2227 case SystemZ::ATOMIC_SWAPW:
2228 return emitAtomicLoadBinary(MI, MBB, 0, 0);
2229 case SystemZ::ATOMIC_SWAP_32:
2230 return emitAtomicLoadBinary(MI, MBB, 0, 32);
2231 case SystemZ::ATOMIC_SWAP_64:
2232 return emitAtomicLoadBinary(MI, MBB, 0, 64);
2234 case SystemZ::ATOMIC_LOADW_AR:
2235 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
2236 case SystemZ::ATOMIC_LOADW_AFI:
2237 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
2238 case SystemZ::ATOMIC_LOAD_AR:
2239 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
2240 case SystemZ::ATOMIC_LOAD_AHI:
2241 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
2242 case SystemZ::ATOMIC_LOAD_AFI:
2243 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
2244 case SystemZ::ATOMIC_LOAD_AGR:
2245 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
2246 case SystemZ::ATOMIC_LOAD_AGHI:
2247 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
2248 case SystemZ::ATOMIC_LOAD_AGFI:
2249 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
2251 case SystemZ::ATOMIC_LOADW_SR:
2252 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
2253 case SystemZ::ATOMIC_LOAD_SR:
2254 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
2255 case SystemZ::ATOMIC_LOAD_SGR:
2256 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
2258 case SystemZ::ATOMIC_LOADW_NR:
2259 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
2260 case SystemZ::ATOMIC_LOADW_NILH:
2261 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH32, 0);
2262 case SystemZ::ATOMIC_LOAD_NR:
2263 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
2264 case SystemZ::ATOMIC_LOAD_NILL32:
2265 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL32, 32);
2266 case SystemZ::ATOMIC_LOAD_NILH32:
2267 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH32, 32);
2268 case SystemZ::ATOMIC_LOAD_NILF32:
2269 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF32, 32);
2270 case SystemZ::ATOMIC_LOAD_NGR:
2271 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
2272 case SystemZ::ATOMIC_LOAD_NILL:
2273 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 64);
2274 case SystemZ::ATOMIC_LOAD_NILH:
2275 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 64);
2276 case SystemZ::ATOMIC_LOAD_NIHL:
2277 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL, 64);
2278 case SystemZ::ATOMIC_LOAD_NIHH:
2279 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH, 64);
2280 case SystemZ::ATOMIC_LOAD_NILF:
2281 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 64);
2282 case SystemZ::ATOMIC_LOAD_NIHF:
2283 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF, 64);
2285 case SystemZ::ATOMIC_LOADW_OR:
2286 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
2287 case SystemZ::ATOMIC_LOADW_OILH:
2288 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH32, 0);
2289 case SystemZ::ATOMIC_LOAD_OR:
2290 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
2291 case SystemZ::ATOMIC_LOAD_OILL32:
2292 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL32, 32);
2293 case SystemZ::ATOMIC_LOAD_OILH32:
2294 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH32, 32);
2295 case SystemZ::ATOMIC_LOAD_OILF32:
2296 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF32, 32);
2297 case SystemZ::ATOMIC_LOAD_OGR:
2298 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
2299 case SystemZ::ATOMIC_LOAD_OILL:
2300 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 64);
2301 case SystemZ::ATOMIC_LOAD_OILH:
2302 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 64);
2303 case SystemZ::ATOMIC_LOAD_OIHL:
2304 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL, 64);
2305 case SystemZ::ATOMIC_LOAD_OIHH:
2306 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH, 64);
2307 case SystemZ::ATOMIC_LOAD_OILF:
2308 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 64);
2309 case SystemZ::ATOMIC_LOAD_OIHF:
2310 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF, 64);
2312 case SystemZ::ATOMIC_LOADW_XR:
2313 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
2314 case SystemZ::ATOMIC_LOADW_XILF:
2315 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF32, 0);
2316 case SystemZ::ATOMIC_LOAD_XR:
2317 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
2318 case SystemZ::ATOMIC_LOAD_XILF32:
2319 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF32, 32);
2320 case SystemZ::ATOMIC_LOAD_XGR:
2321 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
2322 case SystemZ::ATOMIC_LOAD_XILF:
2323 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 64);
2324 case SystemZ::ATOMIC_LOAD_XIHF:
2325 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF, 64);
2327 case SystemZ::ATOMIC_LOADW_NRi:
2328 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
2329 case SystemZ::ATOMIC_LOADW_NILHi:
2330 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH32, 0, true);
2331 case SystemZ::ATOMIC_LOAD_NRi:
2332 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
2333 case SystemZ::ATOMIC_LOAD_NILL32i:
2334 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL32, 32, true);
2335 case SystemZ::ATOMIC_LOAD_NILH32i:
2336 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH32, 32, true);
2337 case SystemZ::ATOMIC_LOAD_NILF32i:
2338 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF32, 32, true);
2339 case SystemZ::ATOMIC_LOAD_NGRi:
2340 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
2341 case SystemZ::ATOMIC_LOAD_NILLi:
2342 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 64, true);
2343 case SystemZ::ATOMIC_LOAD_NILHi:
2344 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 64, true);
2345 case SystemZ::ATOMIC_LOAD_NIHLi:
2346 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL, 64, true);
2347 case SystemZ::ATOMIC_LOAD_NIHHi:
2348 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH, 64, true);
2349 case SystemZ::ATOMIC_LOAD_NILFi:
2350 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 64, true);
2351 case SystemZ::ATOMIC_LOAD_NIHFi:
2352 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF, 64, true);
2354 case SystemZ::ATOMIC_LOADW_MIN:
2355 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
2356 SystemZ::CCMASK_CMP_LE, 0);
2357 case SystemZ::ATOMIC_LOAD_MIN_32:
2358 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
2359 SystemZ::CCMASK_CMP_LE, 32);
2360 case SystemZ::ATOMIC_LOAD_MIN_64:
2361 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
2362 SystemZ::CCMASK_CMP_LE, 64);
2364 case SystemZ::ATOMIC_LOADW_MAX:
2365 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
2366 SystemZ::CCMASK_CMP_GE, 0);
2367 case SystemZ::ATOMIC_LOAD_MAX_32:
2368 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
2369 SystemZ::CCMASK_CMP_GE, 32);
2370 case SystemZ::ATOMIC_LOAD_MAX_64:
2371 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
2372 SystemZ::CCMASK_CMP_GE, 64);
2374 case SystemZ::ATOMIC_LOADW_UMIN:
2375 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
2376 SystemZ::CCMASK_CMP_LE, 0);
2377 case SystemZ::ATOMIC_LOAD_UMIN_32:
2378 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
2379 SystemZ::CCMASK_CMP_LE, 32);
2380 case SystemZ::ATOMIC_LOAD_UMIN_64:
2381 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
2382 SystemZ::CCMASK_CMP_LE, 64);
2384 case SystemZ::ATOMIC_LOADW_UMAX:
2385 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
2386 SystemZ::CCMASK_CMP_GE, 0);
2387 case SystemZ::ATOMIC_LOAD_UMAX_32:
2388 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
2389 SystemZ::CCMASK_CMP_GE, 32);
2390 case SystemZ::ATOMIC_LOAD_UMAX_64:
2391 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
2392 SystemZ::CCMASK_CMP_GE, 64);
2394 case SystemZ::ATOMIC_CMP_SWAPW:
2395 return emitAtomicCmpSwapW(MI, MBB);
2397 // The original DAG glues comparisons to their uses, both to ensure
2398 // that no CC-clobbering instructions are inserted between them, and
2399 // to ensure that comparison results are not reused. This means that
2400 // a BRC is the sole user of a preceding comparison and that we can
2401 // try to use a fused compare and branch instead.
2402 if (convertPrevCompareToBranch(MBB, MI, MI->getOperand(0).getImm(),
2403 MI->getOperand(1).getMBB()))
2404 MI->eraseFromParent();
2406 case SystemZ::MVCWrapper:
2407 return emitMVCWrapper(MI, MBB);
2409 llvm_unreachable("Unexpected instr type to insert");