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/Support/Debug.h"
35 #include "llvm/Target/TargetOptions.h"
36 #include "llvm/ADT/VectorExtras.h"
39 SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm) :
40 TargetLowering(tm), Subtarget(*tm.getSubtargetImpl()), TM(tm) {
42 RegInfo = TM.getRegisterInfo();
44 // Set up the register classes.
45 addRegisterClass(MVT::i32, SystemZ::GR32RegisterClass);
46 addRegisterClass(MVT::i64, SystemZ::GR64RegisterClass);
47 addRegisterClass(MVT::v2i32,SystemZ::GR64PRegisterClass);
48 addRegisterClass(MVT::i128, SystemZ::GR128RegisterClass);
49 addRegisterClass(MVT::v2i64,SystemZ::GR128RegisterClass);
52 addRegisterClass(MVT::f32, SystemZ::FP32RegisterClass);
53 addRegisterClass(MVT::f64, SystemZ::FP64RegisterClass);
56 // Compute derived properties from the register classes
57 computeRegisterProperties();
59 // Set shifts properties
60 setShiftAmountFlavor(Extend);
61 setShiftAmountType(MVT::i64);
63 // Provide all sorts of operation actions
64 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
65 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
66 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
68 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
69 setSchedulingPreference(SchedulingForLatency);
71 setOperationAction(ISD::RET, MVT::Other, Custom);
73 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
74 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
75 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
76 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
77 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
78 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
79 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
81 setOperationAction(ISD::SDIV, MVT::i32, Expand);
82 setOperationAction(ISD::UDIV, MVT::i32, Expand);
83 setOperationAction(ISD::SDIV, MVT::i64, Expand);
84 setOperationAction(ISD::UDIV, MVT::i64, Expand);
85 setOperationAction(ISD::SREM, MVT::i32, Expand);
86 setOperationAction(ISD::UREM, MVT::i32, Expand);
87 setOperationAction(ISD::SREM, MVT::i64, Expand);
88 setOperationAction(ISD::UREM, MVT::i64, Expand);
90 // FIXME: Can we lower these 2 efficiently?
91 setOperationAction(ISD::SETCC, MVT::i32, Expand);
92 setOperationAction(ISD::SETCC, MVT::i64, Expand);
93 setOperationAction(ISD::SELECT, MVT::i32, Expand);
94 setOperationAction(ISD::SELECT, MVT::i64, Expand);
95 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
96 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
98 // Funny enough: we don't have 64-bit signed versions of these stuff, but have
100 setOperationAction(ISD::MULHS, MVT::i64, Expand);
101 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
104 SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
105 switch (Op.getOpcode()) {
106 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
107 case ISD::RET: return LowerRET(Op, DAG);
108 case ISD::CALL: return LowerCALL(Op, DAG);
109 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
110 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
111 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
112 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
114 assert(0 && "unimplemented operand");
119 //===----------------------------------------------------------------------===//
120 // Calling Convention Implementation
121 //===----------------------------------------------------------------------===//
123 #include "SystemZGenCallingConv.inc"
125 SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
127 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
130 assert(0 && "Unsupported calling convention");
132 case CallingConv::Fast:
133 return LowerCCCArguments(Op, DAG);
137 SDValue SystemZTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
138 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
139 unsigned CallingConv = TheCall->getCallingConv();
140 switch (CallingConv) {
142 assert(0 && "Unsupported calling convention");
143 case CallingConv::Fast:
145 return LowerCCCCallTo(Op, DAG, CallingConv);
149 /// LowerCCCArguments - transform physical registers into virtual registers and
150 /// generate load operations for arguments places on the stack.
151 // FIXME: struct return stuff
153 SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
155 MachineFunction &MF = DAG.getMachineFunction();
156 MachineFrameInfo *MFI = MF.getFrameInfo();
157 MachineRegisterInfo &RegInfo = MF.getRegInfo();
158 SDValue Root = Op.getOperand(0);
159 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
160 unsigned CC = MF.getFunction()->getCallingConv();
161 DebugLoc dl = Op.getDebugLoc();
163 // Assign locations to all of the incoming arguments.
164 SmallVector<CCValAssign, 16> ArgLocs;
165 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
166 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
168 assert(!isVarArg && "Varargs not supported yet");
170 SmallVector<SDValue, 16> ArgValues;
171 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
172 CCValAssign &VA = ArgLocs[i];
174 // Arguments passed in registers
175 MVT RegVT = VA.getLocVT();
176 TargetRegisterClass *RC;
177 switch (RegVT.getSimpleVT()) {
179 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
180 << RegVT.getSimpleVT()
184 RC = SystemZ::GR64RegisterClass;
187 RC = SystemZ::FP32RegisterClass;
190 RC = SystemZ::FP64RegisterClass;
194 unsigned VReg = RegInfo.createVirtualRegister(RC);
195 RegInfo.addLiveIn(VA.getLocReg(), VReg);
196 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
198 // If this is an 8/16/32-bit value, it is really passed promoted to 64
199 // bits. Insert an assert[sz]ext to capture this, then truncate to the
201 if (VA.getLocInfo() == CCValAssign::SExt)
202 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
203 DAG.getValueType(VA.getValVT()));
204 else if (VA.getLocInfo() == CCValAssign::ZExt)
205 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
206 DAG.getValueType(VA.getValVT()));
208 if (VA.getLocInfo() != CCValAssign::Full)
209 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
211 ArgValues.push_back(ArgValue);
214 assert(VA.isMemLoc());
216 // Create the nodes corresponding to a load from this parameter slot.
217 // Create the frame index object for this incoming parameter...
218 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
219 VA.getLocMemOffset());
221 // Create the SelectionDAG nodes corresponding to a load
222 //from this parameter
223 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
224 ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN,
225 PseudoSourceValue::getFixedStack(FI), 0));
229 ArgValues.push_back(Root);
231 // Return the new list of results.
232 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
233 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
236 /// LowerCCCCallTo - functions arguments are copied from virtual regs to
237 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
239 SDValue SystemZTargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
241 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
242 SDValue Chain = TheCall->getChain();
243 SDValue Callee = TheCall->getCallee();
244 bool isVarArg = TheCall->isVarArg();
245 DebugLoc dl = Op.getDebugLoc();
246 MachineFunction &MF = DAG.getMachineFunction();
248 // Offset to first argument stack slot.
249 const unsigned FirstArgOffset = 160;
251 // Analyze operands of the call, assigning locations to each operand.
252 SmallVector<CCValAssign, 16> ArgLocs;
253 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
255 CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ);
257 // Get a count of how many bytes are to be pushed on the stack.
258 unsigned NumBytes = CCInfo.getNextStackOffset();
260 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
261 getPointerTy(), true));
263 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
264 SmallVector<SDValue, 12> MemOpChains;
267 // Walk the register/memloc assignments, inserting copies/loads.
268 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
269 CCValAssign &VA = ArgLocs[i];
271 // Arguments start after the 5 first operands of ISD::CALL
272 SDValue Arg = TheCall->getArg(i);
274 // Promote the value if needed.
275 switch (VA.getLocInfo()) {
276 default: assert(0 && "Unknown loc info!");
277 case CCValAssign::Full: break;
278 case CCValAssign::SExt:
279 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
281 case CCValAssign::ZExt:
282 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
284 case CCValAssign::AExt:
285 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
289 // Arguments that can be passed on register must be kept at RegsToPass
292 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
294 assert(VA.isMemLoc());
296 if (StackPtr.getNode() == 0)
298 DAG.getCopyFromReg(Chain, dl,
299 (RegInfo->hasFP(MF) ?
300 SystemZ::R11D : SystemZ::R15D),
303 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
304 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
306 DAG.getIntPtrConstant(Offset));
308 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
309 PseudoSourceValue::getStack(), Offset));
313 // Transform all store nodes into one single node because all store nodes are
314 // independent of each other.
315 if (!MemOpChains.empty())
316 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
317 &MemOpChains[0], MemOpChains.size());
319 // Build a sequence of copy-to-reg nodes chained together with token chain and
320 // flag operands which copy the outgoing args into registers. The InFlag in
321 // necessary since all emited instructions must be stuck together.
323 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
324 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
325 RegsToPass[i].second, InFlag);
326 InFlag = Chain.getValue(1);
329 // If the callee is a GlobalAddress node (quite common, every direct call is)
330 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
331 // Likewise ExternalSymbol -> TargetExternalSymbol.
332 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
333 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
334 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
335 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
337 // Returns a chain & a flag for retval copy to use.
338 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
339 SmallVector<SDValue, 8> Ops;
340 Ops.push_back(Chain);
341 Ops.push_back(Callee);
343 // Add argument registers to the end of the list so that they are
344 // known live into the call.
345 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
346 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
347 RegsToPass[i].second.getValueType()));
349 if (InFlag.getNode())
350 Ops.push_back(InFlag);
352 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
353 InFlag = Chain.getValue(1);
355 // Create the CALLSEQ_END node.
356 Chain = DAG.getCALLSEQ_END(Chain,
357 DAG.getConstant(NumBytes, getPointerTy(), true),
358 DAG.getConstant(0, getPointerTy(), true),
360 InFlag = Chain.getValue(1);
362 // Handle result values, copying them out of physregs into vregs that we
364 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
368 /// LowerCallResult - Lower the result values of an ISD::CALL into the
369 /// appropriate copies out of appropriate physical registers. This assumes that
370 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
371 /// being lowered. Returns a SDNode with the same number of values as the
374 SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
376 unsigned CallingConv,
378 bool isVarArg = TheCall->isVarArg();
379 DebugLoc dl = TheCall->getDebugLoc();
381 // Assign locations to each value returned by this call.
382 SmallVector<CCValAssign, 16> RVLocs;
383 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
385 CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ);
386 SmallVector<SDValue, 8> ResultVals;
388 // Copy all of the result registers out of their specified physreg.
389 for (unsigned i = 0; i != RVLocs.size(); ++i) {
390 CCValAssign &VA = RVLocs[i];
392 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
393 VA.getLocVT(), InFlag).getValue(1);
394 SDValue RetValue = Chain.getValue(0);
395 InFlag = Chain.getValue(2);
397 // If this is an 8/16/32-bit value, it is really passed promoted to 64
398 // bits. Insert an assert[sz]ext to capture this, then truncate to the
400 if (VA.getLocInfo() == CCValAssign::SExt)
401 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
402 DAG.getValueType(VA.getValVT()));
403 else if (VA.getLocInfo() == CCValAssign::ZExt)
404 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
405 DAG.getValueType(VA.getValVT()));
407 if (VA.getLocInfo() != CCValAssign::Full)
408 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
410 ResultVals.push_back(RetValue);
413 ResultVals.push_back(Chain);
415 // Merge everything together with a MERGE_VALUES node.
416 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
417 &ResultVals[0], ResultVals.size()).getNode();
421 SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
422 // CCValAssign - represent the assignment of the return value to a location
423 SmallVector<CCValAssign, 16> RVLocs;
424 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
425 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
426 DebugLoc dl = Op.getDebugLoc();
428 // CCState - Info about the registers and stack slot.
429 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
431 // Analize return values of ISD::RET
432 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
434 // If this is the first return lowered for this function, add the regs to the
435 // liveout set for the function.
436 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
437 for (unsigned i = 0; i != RVLocs.size(); ++i)
438 if (RVLocs[i].isRegLoc())
439 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
442 // The chain is always operand #0
443 SDValue Chain = Op.getOperand(0);
446 // Copy the result values into the output registers.
447 for (unsigned i = 0; i != RVLocs.size(); ++i) {
448 CCValAssign &VA = RVLocs[i];
449 SDValue ResValue = Op.getOperand(i*2+1);
450 assert(VA.isRegLoc() && "Can only return in registers!");
452 // If this is an 8/16/32-bit value, it is really should be passed promoted
454 if (VA.getLocInfo() == CCValAssign::SExt)
455 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
456 else if (VA.getLocInfo() == CCValAssign::ZExt)
457 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
458 else if (VA.getLocInfo() == CCValAssign::AExt)
459 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
461 // ISD::RET => ret chain, (regnum1,val1), ...
462 // So i*2+1 index only the regnums
463 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
465 // Guarantee that all emitted copies are stuck together,
466 // avoiding something bad.
467 Flag = Chain.getValue(1);
471 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
474 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
477 SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
478 ISD::CondCode CC, SDValue &SystemZCC,
480 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
482 // FIXME: Emit a test if RHS is zero
484 bool isUnsigned = false;
485 SystemZCC::CondCodes TCC;
487 default: assert(0 && "Invalid integer condition!");
495 isUnsigned = true; // FALLTHROUGH
500 isUnsigned = true; // FALLTHROUGH
507 TCC = SystemZCC::H; // FALLTHROUGH
511 case ISD::SETLT: // FALLTHROUGH
516 SystemZCC = DAG.getConstant(TCC, MVT::i32);
518 DebugLoc dl = LHS.getDebugLoc();
519 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
520 dl, MVT::Flag, LHS, RHS);
524 SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
525 SDValue Chain = Op.getOperand(0);
526 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
527 SDValue LHS = Op.getOperand(2);
528 SDValue RHS = Op.getOperand(3);
529 SDValue Dest = Op.getOperand(4);
530 DebugLoc dl = Op.getDebugLoc();
533 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
534 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
535 Chain, Dest, SystemZCC, Flag);
538 SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
539 SDValue LHS = Op.getOperand(0);
540 SDValue RHS = Op.getOperand(1);
541 SDValue TrueV = Op.getOperand(2);
542 SDValue FalseV = Op.getOperand(3);
543 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
544 DebugLoc dl = Op.getDebugLoc();
547 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
549 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
550 SmallVector<SDValue, 4> Ops;
551 Ops.push_back(TrueV);
552 Ops.push_back(FalseV);
553 Ops.push_back(SystemZCC);
556 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
559 SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
561 DebugLoc dl = Op.getDebugLoc();
562 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
563 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
565 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
566 bool ExtraLoadRequired =
567 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
570 if (!IsPic && !ExtraLoadRequired) {
571 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
574 unsigned char OpFlags = 0;
575 if (ExtraLoadRequired)
576 OpFlags = SystemZII::MO_GOTENT;
578 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
581 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
582 getPointerTy(), Result);
584 if (ExtraLoadRequired)
585 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
586 PseudoSourceValue::getGOT(), 0);
588 // If there was a non-zero offset that we didn't fold, create an explicit
591 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
592 DAG.getConstant(Offset, getPointerTy()));
598 SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
600 DebugLoc dl = Op.getDebugLoc();
601 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
602 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
604 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
607 const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
609 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
610 case SystemZISD::CALL: return "SystemZISD::CALL";
611 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
612 case SystemZISD::CMP: return "SystemZISD::CMP";
613 case SystemZISD::UCMP: return "SystemZISD::UCMP";
614 case SystemZISD::SELECT: return "SystemZISD::SELECT";
615 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
616 default: return NULL;
620 //===----------------------------------------------------------------------===//
621 // Other Lowering Code
622 //===----------------------------------------------------------------------===//
625 SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
626 MachineBasicBlock *BB) const {
627 const SystemZInstrInfo &TII = *TM.getInstrInfo();
628 DebugLoc dl = MI->getDebugLoc();
629 assert((MI->getOpcode() == SystemZ::Select32 ||
630 MI->getOpcode() == SystemZ::Select64) &&
631 "Unexpected instr type to insert");
633 // To "insert" a SELECT instruction, we actually have to insert the diamond
634 // control-flow pattern. The incoming instruction knows the destination vreg
635 // to set, the condition code register to branch on, the true/false values to
636 // select between, and a branch opcode to use.
637 const BasicBlock *LLVM_BB = BB->getBasicBlock();
638 MachineFunction::iterator I = BB;
646 // fallthrough --> copy0MBB
647 MachineBasicBlock *thisMBB = BB;
648 MachineFunction *F = BB->getParent();
649 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
650 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
651 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
652 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
653 F->insert(I, copy0MBB);
654 F->insert(I, copy1MBB);
655 // Update machine-CFG edges by transferring all successors of the current
656 // block to the new block which will contain the Phi node for the select.
657 copy1MBB->transferSuccessors(BB);
658 // Next, add the true and fallthrough blocks as its successors.
659 BB->addSuccessor(copy0MBB);
660 BB->addSuccessor(copy1MBB);
664 // # fallthrough to copy1MBB
667 // Update machine-CFG edges
668 BB->addSuccessor(copy1MBB);
671 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
674 BuildMI(BB, dl, TII.get(SystemZ::PHI),
675 MI->getOperand(0).getReg())
676 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
677 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
679 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.