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"
18 #include "SystemZTargetMachine.h"
19 #include "SystemZSubtarget.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Function.h"
22 #include "llvm/Intrinsics.h"
23 #include "llvm/CallingConv.h"
24 #include "llvm/GlobalVariable.h"
25 #include "llvm/GlobalAlias.h"
26 #include "llvm/CodeGen/CallingConvLower.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/PseudoSourceValue.h"
32 #include "llvm/CodeGen/SelectionDAGISel.h"
33 #include "llvm/CodeGen/ValueTypes.h"
34 #include "llvm/Target/TargetOptions.h"
35 #include "llvm/Target/TargetLoweringObjectFile.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/ADT/VectorExtras.h"
41 SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm) :
42 TargetLowering(tm, new TargetLoweringObjectFileELF()),
43 Subtarget(*tm.getSubtargetImpl()), TM(tm) {
45 RegInfo = TM.getRegisterInfo();
47 // Set up the register classes.
48 addRegisterClass(MVT::i32, SystemZ::GR32RegisterClass);
49 addRegisterClass(MVT::i64, SystemZ::GR64RegisterClass);
50 addRegisterClass(MVT::v2i32,SystemZ::GR64PRegisterClass);
51 addRegisterClass(MVT::v2i64,SystemZ::GR128RegisterClass);
54 addRegisterClass(MVT::f32, SystemZ::FP32RegisterClass);
55 addRegisterClass(MVT::f64, SystemZ::FP64RegisterClass);
58 // Compute derived properties from the register classes
59 computeRegisterProperties();
61 // Set shifts properties
62 setShiftAmountType(MVT::i64);
64 // Provide all sorts of operation actions
65 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
66 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
67 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
69 setLoadExtAction(ISD::SEXTLOAD, MVT::f32, Expand);
70 setLoadExtAction(ISD::ZEXTLOAD, MVT::f32, Expand);
71 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
73 setLoadExtAction(ISD::SEXTLOAD, MVT::f64, Expand);
74 setLoadExtAction(ISD::ZEXTLOAD, MVT::f64, Expand);
75 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
77 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
79 // TODO: It may be better to default to latency-oriented scheduling, however
80 // LLVM's current latency-oriented scheduler can't handle physreg definitions
81 // such as SystemZ has with PSW, so set this to the register-pressure
82 // scheduler, because it can.
83 setSchedulingPreference(SchedulingForRegPressure);
85 setBooleanContents(ZeroOrOneBooleanContent);
87 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
88 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
89 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
90 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
91 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
92 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
93 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
94 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
95 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
96 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
97 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
99 setOperationAction(ISD::SDIV, MVT::i32, Expand);
100 setOperationAction(ISD::UDIV, MVT::i32, Expand);
101 setOperationAction(ISD::SDIV, MVT::i64, Expand);
102 setOperationAction(ISD::UDIV, MVT::i64, Expand);
103 setOperationAction(ISD::SREM, MVT::i32, Expand);
104 setOperationAction(ISD::UREM, MVT::i32, Expand);
105 setOperationAction(ISD::SREM, MVT::i64, Expand);
106 setOperationAction(ISD::UREM, MVT::i64, Expand);
108 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
110 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
111 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
112 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
113 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
114 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
115 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
117 // FIXME: Can we lower these 2 efficiently?
118 setOperationAction(ISD::SETCC, MVT::i32, Expand);
119 setOperationAction(ISD::SETCC, MVT::i64, Expand);
120 setOperationAction(ISD::SETCC, MVT::f32, Expand);
121 setOperationAction(ISD::SETCC, MVT::f64, Expand);
122 setOperationAction(ISD::SELECT, MVT::i32, Expand);
123 setOperationAction(ISD::SELECT, MVT::i64, Expand);
124 setOperationAction(ISD::SELECT, MVT::f32, Expand);
125 setOperationAction(ISD::SELECT, MVT::f64, Expand);
126 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
127 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
128 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
129 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
131 setOperationAction(ISD::MULHS, MVT::i64, Expand);
132 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
134 // FIXME: Can we support these natively?
135 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
136 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
137 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
138 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
140 // Lower some FP stuff
141 setOperationAction(ISD::FSIN, MVT::f32, Expand);
142 setOperationAction(ISD::FSIN, MVT::f64, Expand);
143 setOperationAction(ISD::FCOS, MVT::f32, Expand);
144 setOperationAction(ISD::FCOS, MVT::f64, Expand);
145 setOperationAction(ISD::FREM, MVT::f32, Expand);
146 setOperationAction(ISD::FREM, MVT::f64, Expand);
148 // We have only 64-bit bitconverts
149 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
150 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
152 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
153 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
154 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
155 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
157 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
160 SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
161 switch (Op.getOpcode()) {
162 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
163 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
164 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
165 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
166 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
168 llvm_unreachable("Should not custom lower this!");
173 bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm) const {
181 return Imm.isZero() || Imm.isNegZero();
184 //===----------------------------------------------------------------------===//
185 // SystemZ Inline Assembly Support
186 //===----------------------------------------------------------------------===//
188 /// getConstraintType - Given a constraint letter, return the type of
189 /// constraint it is for this target.
190 TargetLowering::ConstraintType
191 SystemZTargetLowering::getConstraintType(const std::string &Constraint) const {
192 if (Constraint.size() == 1) {
193 switch (Constraint[0]) {
195 return C_RegisterClass;
200 return TargetLowering::getConstraintType(Constraint);
203 std::pair<unsigned, const TargetRegisterClass*>
204 SystemZTargetLowering::
205 getRegForInlineAsmConstraint(const std::string &Constraint,
207 if (Constraint.size() == 1) {
208 // GCC Constraint Letters
209 switch (Constraint[0]) {
211 case 'r': // GENERAL_REGS
213 return std::make_pair(0U, SystemZ::GR32RegisterClass);
214 else if (VT == MVT::i128)
215 return std::make_pair(0U, SystemZ::GR128RegisterClass);
217 return std::make_pair(0U, SystemZ::GR64RegisterClass);
221 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
224 //===----------------------------------------------------------------------===//
225 // Calling Convention Implementation
226 //===----------------------------------------------------------------------===//
228 #include "SystemZGenCallingConv.inc"
231 SystemZTargetLowering::LowerFormalArguments(SDValue Chain,
232 CallingConv::ID CallConv,
234 const SmallVectorImpl<ISD::InputArg>
238 SmallVectorImpl<SDValue> &InVals) {
242 llvm_unreachable("Unsupported calling convention");
244 case CallingConv::Fast:
245 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
250 SystemZTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
251 CallingConv::ID CallConv, bool isVarArg,
253 const SmallVectorImpl<ISD::OutputArg> &Outs,
254 const SmallVectorImpl<ISD::InputArg> &Ins,
255 DebugLoc dl, SelectionDAG &DAG,
256 SmallVectorImpl<SDValue> &InVals) {
260 llvm_unreachable("Unsupported calling convention");
261 case CallingConv::Fast:
263 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
264 Outs, Ins, dl, DAG, InVals);
268 /// LowerCCCArguments - transform physical registers into virtual registers and
269 /// generate load operations for arguments places on the stack.
270 // FIXME: struct return stuff
273 SystemZTargetLowering::LowerCCCArguments(SDValue Chain,
274 CallingConv::ID CallConv,
276 const SmallVectorImpl<ISD::InputArg>
280 SmallVectorImpl<SDValue> &InVals) {
282 MachineFunction &MF = DAG.getMachineFunction();
283 MachineFrameInfo *MFI = MF.getFrameInfo();
284 MachineRegisterInfo &RegInfo = MF.getRegInfo();
286 // Assign locations to all of the incoming arguments.
287 SmallVector<CCValAssign, 16> ArgLocs;
288 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
289 ArgLocs, *DAG.getContext());
290 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
293 llvm_report_error("Varargs not supported yet");
295 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
297 CCValAssign &VA = ArgLocs[i];
298 EVT LocVT = VA.getLocVT();
300 // Arguments passed in registers
301 TargetRegisterClass *RC;
302 switch (LocVT.getSimpleVT().SimpleTy) {
305 errs() << "LowerFormalArguments Unhandled argument type: "
306 << LocVT.getSimpleVT().SimpleTy
311 RC = SystemZ::GR64RegisterClass;
314 RC = SystemZ::FP32RegisterClass;
317 RC = SystemZ::FP64RegisterClass;
321 unsigned VReg = RegInfo.createVirtualRegister(RC);
322 RegInfo.addLiveIn(VA.getLocReg(), VReg);
323 ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, LocVT);
326 assert(VA.isMemLoc());
328 // Create the nodes corresponding to a load from this parameter slot.
329 // Create the frame index object for this incoming parameter...
330 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits()/8,
331 VA.getLocMemOffset());
333 // Create the SelectionDAG nodes corresponding to a load
334 // from this parameter
335 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
336 ArgValue = DAG.getLoad(LocVT, dl, Chain, FIN,
337 PseudoSourceValue::getFixedStack(FI), 0);
340 // If this is an 8/16/32-bit value, it is really passed promoted to 64
341 // bits. Insert an assert[sz]ext to capture this, then truncate to the
343 if (VA.getLocInfo() == CCValAssign::SExt)
344 ArgValue = DAG.getNode(ISD::AssertSext, dl, LocVT, ArgValue,
345 DAG.getValueType(VA.getValVT()));
346 else if (VA.getLocInfo() == CCValAssign::ZExt)
347 ArgValue = DAG.getNode(ISD::AssertZext, dl, LocVT, ArgValue,
348 DAG.getValueType(VA.getValVT()));
350 if (VA.getLocInfo() != CCValAssign::Full)
351 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
353 InVals.push_back(ArgValue);
359 /// LowerCCCCallTo - functions arguments are copied from virtual regs to
360 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
363 SystemZTargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
364 CallingConv::ID CallConv, bool isVarArg,
366 const SmallVectorImpl<ISD::OutputArg>
368 const SmallVectorImpl<ISD::InputArg> &Ins,
369 DebugLoc dl, SelectionDAG &DAG,
370 SmallVectorImpl<SDValue> &InVals) {
372 MachineFunction &MF = DAG.getMachineFunction();
374 // Offset to first argument stack slot.
375 const unsigned FirstArgOffset = 160;
377 // Analyze operands of the call, assigning locations to each operand.
378 SmallVector<CCValAssign, 16> ArgLocs;
379 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
380 ArgLocs, *DAG.getContext());
382 CCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
384 // Get a count of how many bytes are to be pushed on the stack.
385 unsigned NumBytes = CCInfo.getNextStackOffset();
387 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
388 getPointerTy(), true));
390 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
391 SmallVector<SDValue, 12> MemOpChains;
394 // Walk the register/memloc assignments, inserting copies/loads.
395 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
396 CCValAssign &VA = ArgLocs[i];
398 SDValue Arg = Outs[i].Val;
400 // Promote the value if needed.
401 switch (VA.getLocInfo()) {
402 default: assert(0 && "Unknown loc info!");
403 case CCValAssign::Full: break;
404 case CCValAssign::SExt:
405 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
407 case CCValAssign::ZExt:
408 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
410 case CCValAssign::AExt:
411 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
415 // Arguments that can be passed on register must be kept at RegsToPass
418 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
420 assert(VA.isMemLoc());
422 if (StackPtr.getNode() == 0)
424 DAG.getCopyFromReg(Chain, dl,
425 (RegInfo->hasFP(MF) ?
426 SystemZ::R11D : SystemZ::R15D),
429 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
430 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
432 DAG.getIntPtrConstant(Offset));
434 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
435 PseudoSourceValue::getStack(), Offset));
439 // Transform all store nodes into one single node because all store nodes are
440 // independent of each other.
441 if (!MemOpChains.empty())
442 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
443 &MemOpChains[0], MemOpChains.size());
445 // Build a sequence of copy-to-reg nodes chained together with token chain and
446 // flag operands which copy the outgoing args into registers. The InFlag in
447 // necessary since all emited instructions must be stuck together.
449 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
450 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
451 RegsToPass[i].second, InFlag);
452 InFlag = Chain.getValue(1);
455 // If the callee is a GlobalAddress node (quite common, every direct call is)
456 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
457 // Likewise ExternalSymbol -> TargetExternalSymbol.
458 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
459 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
460 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
461 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
463 // Returns a chain & a flag for retval copy to use.
464 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
465 SmallVector<SDValue, 8> Ops;
466 Ops.push_back(Chain);
467 Ops.push_back(Callee);
469 // Add argument registers to the end of the list so that they are
470 // known live into the call.
471 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
472 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
473 RegsToPass[i].second.getValueType()));
475 if (InFlag.getNode())
476 Ops.push_back(InFlag);
478 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
479 InFlag = Chain.getValue(1);
481 // Create the CALLSEQ_END node.
482 Chain = DAG.getCALLSEQ_END(Chain,
483 DAG.getConstant(NumBytes, getPointerTy(), true),
484 DAG.getConstant(0, getPointerTy(), true),
486 InFlag = Chain.getValue(1);
488 // Handle result values, copying them out of physregs into vregs that we
490 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
494 /// LowerCallResult - Lower the result values of a call into the
495 /// appropriate copies out of appropriate physical registers.
498 SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
499 CallingConv::ID CallConv, bool isVarArg,
500 const SmallVectorImpl<ISD::InputArg>
502 DebugLoc dl, SelectionDAG &DAG,
503 SmallVectorImpl<SDValue> &InVals) {
505 // Assign locations to each value returned by this call.
506 SmallVector<CCValAssign, 16> RVLocs;
507 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
510 CCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
512 // Copy all of the result registers out of their specified physreg.
513 for (unsigned i = 0; i != RVLocs.size(); ++i) {
514 CCValAssign &VA = RVLocs[i];
516 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
517 VA.getLocVT(), InFlag).getValue(1);
518 SDValue RetValue = Chain.getValue(0);
519 InFlag = Chain.getValue(2);
521 // If this is an 8/16/32-bit value, it is really passed promoted to 64
522 // bits. Insert an assert[sz]ext to capture this, then truncate to the
524 if (VA.getLocInfo() == CCValAssign::SExt)
525 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
526 DAG.getValueType(VA.getValVT()));
527 else if (VA.getLocInfo() == CCValAssign::ZExt)
528 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
529 DAG.getValueType(VA.getValVT()));
531 if (VA.getLocInfo() != CCValAssign::Full)
532 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
534 InVals.push_back(RetValue);
542 SystemZTargetLowering::LowerReturn(SDValue Chain,
543 CallingConv::ID CallConv, bool isVarArg,
544 const SmallVectorImpl<ISD::OutputArg> &Outs,
545 DebugLoc dl, SelectionDAG &DAG) {
547 // CCValAssign - represent the assignment of the return value to a location
548 SmallVector<CCValAssign, 16> RVLocs;
550 // CCState - Info about the registers and stack slot.
551 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
552 RVLocs, *DAG.getContext());
554 // Analize return values.
555 CCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
557 // If this is the first return lowered for this function, add the regs to the
558 // liveout set for the function.
559 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
560 for (unsigned i = 0; i != RVLocs.size(); ++i)
561 if (RVLocs[i].isRegLoc())
562 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
567 // Copy the result values into the output registers.
568 for (unsigned i = 0; i != RVLocs.size(); ++i) {
569 CCValAssign &VA = RVLocs[i];
570 SDValue ResValue = Outs[i].Val;
571 assert(VA.isRegLoc() && "Can only return in registers!");
573 // If this is an 8/16/32-bit value, it is really should be passed promoted
575 if (VA.getLocInfo() == CCValAssign::SExt)
576 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
577 else if (VA.getLocInfo() == CCValAssign::ZExt)
578 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
579 else if (VA.getLocInfo() == CCValAssign::AExt)
580 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
582 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
584 // Guarantee that all emitted copies are stuck together,
585 // avoiding something bad.
586 Flag = Chain.getValue(1);
590 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
593 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
596 SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
597 ISD::CondCode CC, SDValue &SystemZCC,
599 // FIXME: Emit a test if RHS is zero
601 bool isUnsigned = false;
602 SystemZCC::CondCodes TCC;
605 llvm_unreachable("Invalid integer condition!");
611 TCC = SystemZCC::NLH;
627 if (LHS.getValueType().isFloatingPoint()) {
631 isUnsigned = true; // FALLTHROUGH
637 if (LHS.getValueType().isFloatingPoint()) {
641 isUnsigned = true; // FALLTHROUGH
647 if (LHS.getValueType().isFloatingPoint()) {
648 TCC = SystemZCC::NLE;
651 isUnsigned = true; // FALLTHROUGH
657 if (LHS.getValueType().isFloatingPoint()) {
658 TCC = SystemZCC::NHE;
661 isUnsigned = true; // FALLTHROUGH
668 SystemZCC = DAG.getConstant(TCC, MVT::i32);
670 DebugLoc dl = LHS.getDebugLoc();
671 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
672 dl, MVT::i64, LHS, RHS);
676 SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
677 SDValue Chain = Op.getOperand(0);
678 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
679 SDValue LHS = Op.getOperand(2);
680 SDValue RHS = Op.getOperand(3);
681 SDValue Dest = Op.getOperand(4);
682 DebugLoc dl = Op.getDebugLoc();
685 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
686 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
687 Chain, Dest, SystemZCC, Flag);
690 SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
691 SDValue LHS = Op.getOperand(0);
692 SDValue RHS = Op.getOperand(1);
693 SDValue TrueV = Op.getOperand(2);
694 SDValue FalseV = Op.getOperand(3);
695 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
696 DebugLoc dl = Op.getDebugLoc();
699 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
701 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
702 SmallVector<SDValue, 4> Ops;
703 Ops.push_back(TrueV);
704 Ops.push_back(FalseV);
705 Ops.push_back(SystemZCC);
708 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
711 SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
713 DebugLoc dl = Op.getDebugLoc();
714 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
715 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
717 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
718 bool ExtraLoadRequired =
719 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
722 if (!IsPic && !ExtraLoadRequired) {
723 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
726 unsigned char OpFlags = 0;
727 if (ExtraLoadRequired)
728 OpFlags = SystemZII::MO_GOTENT;
730 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
733 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
734 getPointerTy(), Result);
736 if (ExtraLoadRequired)
737 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
738 PseudoSourceValue::getGOT(), 0);
740 // If there was a non-zero offset that we didn't fold, create an explicit
743 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
744 DAG.getConstant(Offset, getPointerTy()));
750 SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
752 DebugLoc dl = Op.getDebugLoc();
753 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
754 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
756 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
761 // FIXME: This is just dirty hack. We need to lower cpool properly
762 SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
764 DebugLoc dl = Op.getDebugLoc();
765 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
767 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
771 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
774 const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
776 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
777 case SystemZISD::CALL: return "SystemZISD::CALL";
778 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
779 case SystemZISD::CMP: return "SystemZISD::CMP";
780 case SystemZISD::UCMP: return "SystemZISD::UCMP";
781 case SystemZISD::SELECT: return "SystemZISD::SELECT";
782 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
783 default: return NULL;
787 //===----------------------------------------------------------------------===//
788 // Other Lowering Code
789 //===----------------------------------------------------------------------===//
792 SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
793 MachineBasicBlock *BB,
794 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
795 const SystemZInstrInfo &TII = *TM.getInstrInfo();
796 DebugLoc dl = MI->getDebugLoc();
797 assert((MI->getOpcode() == SystemZ::Select32 ||
798 MI->getOpcode() == SystemZ::SelectF32 ||
799 MI->getOpcode() == SystemZ::Select64 ||
800 MI->getOpcode() == SystemZ::SelectF64) &&
801 "Unexpected instr type to insert");
803 // To "insert" a SELECT instruction, we actually have to insert the diamond
804 // control-flow pattern. The incoming instruction knows the destination vreg
805 // to set, the condition code register to branch on, the true/false values to
806 // select between, and a branch opcode to use.
807 const BasicBlock *LLVM_BB = BB->getBasicBlock();
808 MachineFunction::iterator I = BB;
816 // fallthrough --> copy0MBB
817 MachineBasicBlock *thisMBB = BB;
818 MachineFunction *F = BB->getParent();
819 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
820 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
821 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
822 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
823 F->insert(I, copy0MBB);
824 F->insert(I, copy1MBB);
825 // Inform sdisel of the edge changes.
826 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
827 SE = BB->succ_end(); SI != SE; ++SI)
828 EM->insert(std::make_pair(*SI, copy1MBB));
829 // Update machine-CFG edges by transferring all successors of the current
830 // block to the new block which will contain the Phi node for the select.
831 copy1MBB->transferSuccessors(BB);
832 // Next, add the true and fallthrough blocks as its successors.
833 BB->addSuccessor(copy0MBB);
834 BB->addSuccessor(copy1MBB);
838 // # fallthrough to copy1MBB
841 // Update machine-CFG edges
842 BB->addSuccessor(copy1MBB);
845 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
848 BuildMI(BB, dl, TII.get(SystemZ::PHI),
849 MI->getOperand(0).getReg())
850 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
851 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
853 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.