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/ADT/VectorExtras.h"
38 SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm) :
39 TargetLowering(tm), Subtarget(*tm.getSubtargetImpl()), TM(tm) {
41 RegInfo = TM.getRegisterInfo();
43 // Set up the register classes.
44 addRegisterClass(MVT::i32, SystemZ::GR32RegisterClass);
45 addRegisterClass(MVT::i64, SystemZ::GR64RegisterClass);
46 addRegisterClass(MVT::i128, SystemZ::GR128RegisterClass);
48 // Compute derived properties from the register classes
49 computeRegisterProperties();
51 // Set shifts properties
52 setShiftAmountFlavor(Extend);
53 setShiftAmountType(MVT::i32);
55 // Provide all sorts of operation actions
56 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
57 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
58 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
60 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
61 setSchedulingPreference(SchedulingForLatency);
63 setOperationAction(ISD::RET, MVT::Other, Custom);
65 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
66 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
67 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
69 // FIXME: Can we lower these 2 efficiently?
70 setOperationAction(ISD::SETCC, MVT::i32, Expand);
71 setOperationAction(ISD::SETCC, MVT::i64, Expand);
72 setOperationAction(ISD::SELECT, MVT::i32, Expand);
73 setOperationAction(ISD::SELECT, MVT::i64, Expand);
74 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
75 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
77 // Funny enough: we don't have 64-bit signed versions of these stuff, but have
79 setOperationAction(ISD::MULHS, MVT::i64, Expand);
80 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
83 SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
84 switch (Op.getOpcode()) {
85 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
86 case ISD::RET: return LowerRET(Op, DAG);
87 case ISD::CALL: return LowerCALL(Op, DAG);
88 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
89 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
91 assert(0 && "unimplemented operand");
96 //===----------------------------------------------------------------------===//
97 // Calling Convention Implementation
98 //===----------------------------------------------------------------------===//
100 #include "SystemZGenCallingConv.inc"
102 SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
104 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
107 assert(0 && "Unsupported calling convention");
109 case CallingConv::Fast:
110 return LowerCCCArguments(Op, DAG);
114 SDValue SystemZTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
115 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
116 unsigned CallingConv = TheCall->getCallingConv();
117 switch (CallingConv) {
119 assert(0 && "Unsupported calling convention");
120 case CallingConv::Fast:
122 return LowerCCCCallTo(Op, DAG, CallingConv);
126 /// LowerCCCArguments - transform physical registers into virtual registers and
127 /// generate load operations for arguments places on the stack.
128 // FIXME: struct return stuff
130 SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
132 MachineFunction &MF = DAG.getMachineFunction();
133 MachineFrameInfo *MFI = MF.getFrameInfo();
134 MachineRegisterInfo &RegInfo = MF.getRegInfo();
135 SDValue Root = Op.getOperand(0);
136 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
137 unsigned CC = MF.getFunction()->getCallingConv();
138 DebugLoc dl = Op.getDebugLoc();
140 // Assign locations to all of the incoming arguments.
141 SmallVector<CCValAssign, 16> ArgLocs;
142 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
143 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
145 assert(!isVarArg && "Varargs not supported yet");
147 SmallVector<SDValue, 16> ArgValues;
148 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
149 CCValAssign &VA = ArgLocs[i];
151 // Arguments passed in registers
152 MVT RegVT = VA.getLocVT();
153 switch (RegVT.getSimpleVT()) {
155 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
156 << RegVT.getSimpleVT()
161 RegInfo.createVirtualRegister(SystemZ::GR64RegisterClass);
162 RegInfo.addLiveIn(VA.getLocReg(), VReg);
163 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
165 // If this is an 8/16/32-bit value, it is really passed promoted to 64
166 // bits. Insert an assert[sz]ext to capture this, then truncate to the
168 if (VA.getLocInfo() == CCValAssign::SExt)
169 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
170 DAG.getValueType(VA.getValVT()));
171 else if (VA.getLocInfo() == CCValAssign::ZExt)
172 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
173 DAG.getValueType(VA.getValVT()));
175 if (VA.getLocInfo() != CCValAssign::Full)
176 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
178 ArgValues.push_back(ArgValue);
182 assert(VA.isMemLoc());
183 // Load the argument to a virtual register
184 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
186 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
187 << VA.getLocVT().getSimpleVT()
190 // Create the frame index object for this incoming parameter...
191 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset());
193 // Create the SelectionDAG nodes corresponding to a load
194 //from this parameter
195 SDValue FIN = DAG.getFrameIndex(FI, MVT::i64);
196 ArgValues.push_back(DAG.getLoad(VA.getLocVT(), dl, Root, FIN,
197 PseudoSourceValue::getFixedStack(FI), 0));
201 ArgValues.push_back(Root);
203 // Return the new list of results.
204 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
205 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
208 /// LowerCCCCallTo - functions arguments are copied from virtual regs to
209 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
211 SDValue SystemZTargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
213 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
214 SDValue Chain = TheCall->getChain();
215 SDValue Callee = TheCall->getCallee();
216 bool isVarArg = TheCall->isVarArg();
217 DebugLoc dl = Op.getDebugLoc();
218 MachineFunction &MF = DAG.getMachineFunction();
220 // Offset to first argument stack slot.
221 const unsigned FirstArgOffset = 160;
223 // Analyze operands of the call, assigning locations to each operand.
224 SmallVector<CCValAssign, 16> ArgLocs;
225 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
227 CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ);
229 // Get a count of how many bytes are to be pushed on the stack.
230 unsigned NumBytes = CCInfo.getNextStackOffset();
232 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
233 getPointerTy(), true));
235 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
236 SmallVector<SDValue, 12> MemOpChains;
239 // Walk the register/memloc assignments, inserting copies/loads.
240 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
241 CCValAssign &VA = ArgLocs[i];
243 // Arguments start after the 5 first operands of ISD::CALL
244 SDValue Arg = TheCall->getArg(i);
246 // Promote the value if needed.
247 switch (VA.getLocInfo()) {
248 default: assert(0 && "Unknown loc info!");
249 case CCValAssign::Full: break;
250 case CCValAssign::SExt:
251 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
253 case CCValAssign::ZExt:
254 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
256 case CCValAssign::AExt:
257 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
261 // Arguments that can be passed on register must be kept at RegsToPass
264 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
266 assert(VA.isMemLoc());
268 if (StackPtr.getNode() == 0)
270 DAG.getCopyFromReg(Chain, dl,
271 (RegInfo->hasFP(MF) ?
272 SystemZ::R11D : SystemZ::R15D),
275 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
276 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
278 DAG.getIntPtrConstant(Offset));
280 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
281 PseudoSourceValue::getStack(), Offset));
285 // Transform all store nodes into one single node because all store nodes are
286 // independent of each other.
287 if (!MemOpChains.empty())
288 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
289 &MemOpChains[0], MemOpChains.size());
291 // Build a sequence of copy-to-reg nodes chained together with token chain and
292 // flag operands which copy the outgoing args into registers. The InFlag in
293 // necessary since all emited instructions must be stuck together.
295 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
296 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
297 RegsToPass[i].second, InFlag);
298 InFlag = Chain.getValue(1);
301 // If the callee is a GlobalAddress node (quite common, every direct call is)
302 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
303 // Likewise ExternalSymbol -> TargetExternalSymbol.
304 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
305 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
306 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
307 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
309 // Returns a chain & a flag for retval copy to use.
310 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
311 SmallVector<SDValue, 8> Ops;
312 Ops.push_back(Chain);
313 Ops.push_back(Callee);
315 // Add argument registers to the end of the list so that they are
316 // known live into the call.
317 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
318 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
319 RegsToPass[i].second.getValueType()));
321 if (InFlag.getNode())
322 Ops.push_back(InFlag);
324 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
325 InFlag = Chain.getValue(1);
327 // Create the CALLSEQ_END node.
328 Chain = DAG.getCALLSEQ_END(Chain,
329 DAG.getConstant(NumBytes, getPointerTy(), true),
330 DAG.getConstant(0, getPointerTy(), true),
332 InFlag = Chain.getValue(1);
334 // Handle result values, copying them out of physregs into vregs that we
336 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
340 /// LowerCallResult - Lower the result values of an ISD::CALL into the
341 /// appropriate copies out of appropriate physical registers. This assumes that
342 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
343 /// being lowered. Returns a SDNode with the same number of values as the
346 SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
348 unsigned CallingConv,
350 bool isVarArg = TheCall->isVarArg();
351 DebugLoc dl = TheCall->getDebugLoc();
353 // Assign locations to each value returned by this call.
354 SmallVector<CCValAssign, 16> RVLocs;
355 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
357 CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ);
358 SmallVector<SDValue, 8> ResultVals;
360 // Copy all of the result registers out of their specified physreg.
361 for (unsigned i = 0; i != RVLocs.size(); ++i) {
362 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
363 RVLocs[i].getValVT(), InFlag).getValue(1);
364 InFlag = Chain.getValue(2);
365 ResultVals.push_back(Chain.getValue(0));
368 ResultVals.push_back(Chain);
370 // Merge everything together with a MERGE_VALUES node.
371 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
372 &ResultVals[0], ResultVals.size()).getNode();
376 SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
377 // CCValAssign - represent the assignment of the return value to a location
378 SmallVector<CCValAssign, 16> RVLocs;
379 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
380 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
381 DebugLoc dl = Op.getDebugLoc();
383 // CCState - Info about the registers and stack slot.
384 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
386 // Analize return values of ISD::RET
387 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
389 // If this is the first return lowered for this function, add the regs to the
390 // liveout set for the function.
391 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
392 for (unsigned i = 0; i != RVLocs.size(); ++i)
393 if (RVLocs[i].isRegLoc())
394 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
397 // The chain is always operand #0
398 SDValue Chain = Op.getOperand(0);
401 // Copy the result values into the output registers.
402 for (unsigned i = 0; i != RVLocs.size(); ++i) {
403 CCValAssign &VA = RVLocs[i];
404 SDValue ResValue = Op.getOperand(i*2+1);
405 assert(VA.isRegLoc() && "Can only return in registers!");
407 // If this is an 8/16/32-bit value, it is really should be passed promoted
409 if (VA.getLocInfo() == CCValAssign::SExt)
410 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
411 else if (VA.getLocInfo() == CCValAssign::ZExt)
412 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
413 else if (VA.getLocInfo() == CCValAssign::AExt)
414 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
416 // ISD::RET => ret chain, (regnum1,val1), ...
417 // So i*2+1 index only the regnums
418 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
420 // Guarantee that all emitted copies are stuck together,
421 // avoiding something bad.
422 Flag = Chain.getValue(1);
426 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
429 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
432 SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
433 ISD::CondCode CC, SDValue &SystemZCC,
435 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
437 // FIXME: Emit a test if RHS is zero
439 bool isUnsigned = false;
440 SystemZCC::CondCodes TCC;
442 default: assert(0 && "Invalid integer condition!");
450 isUnsigned = true; // FALLTHROUGH
455 isUnsigned = true; // FALLTHROUGH
462 TCC = SystemZCC::H; // FALLTHROUGH
466 case ISD::SETLT: // FALLTHROUGH
471 SystemZCC = DAG.getConstant(TCC, MVT::i32);
473 DebugLoc dl = LHS.getDebugLoc();
474 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
475 dl, MVT::Flag, LHS, RHS);
479 SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
480 SDValue Chain = Op.getOperand(0);
481 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
482 SDValue LHS = Op.getOperand(2);
483 SDValue RHS = Op.getOperand(3);
484 SDValue Dest = Op.getOperand(4);
485 DebugLoc dl = Op.getDebugLoc();
488 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
489 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
490 Chain, Dest, SystemZCC, Flag);
493 SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
494 SDValue LHS = Op.getOperand(0);
495 SDValue RHS = Op.getOperand(1);
496 SDValue TrueV = Op.getOperand(2);
497 SDValue FalseV = Op.getOperand(3);
498 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
499 DebugLoc dl = Op.getDebugLoc();
502 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
504 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
505 SmallVector<SDValue, 4> Ops;
506 Ops.push_back(TrueV);
507 Ops.push_back(FalseV);
508 Ops.push_back(SystemZCC);
511 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
515 const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
517 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
518 case SystemZISD::CALL: return "SystemZISD::CALL";
519 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
520 case SystemZISD::CMP: return "SystemZISD::CMP";
521 case SystemZISD::UCMP: return "SystemZISD::UCMP";
522 case SystemZISD::SELECT: return "SystemZISD::SELECT";
523 default: return NULL;
527 //===----------------------------------------------------------------------===//
528 // Other Lowering Code
529 //===----------------------------------------------------------------------===//
532 SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
533 MachineBasicBlock *BB) const {
534 const SystemZInstrInfo &TII = *TM.getInstrInfo();
535 DebugLoc dl = MI->getDebugLoc();
536 assert((MI->getOpcode() == SystemZ::Select32 ||
537 MI->getOpcode() == SystemZ::Select64) &&
538 "Unexpected instr type to insert");
540 // To "insert" a SELECT instruction, we actually have to insert the diamond
541 // control-flow pattern. The incoming instruction knows the destination vreg
542 // to set, the condition code register to branch on, the true/false values to
543 // select between, and a branch opcode to use.
544 const BasicBlock *LLVM_BB = BB->getBasicBlock();
545 MachineFunction::iterator I = BB;
553 // fallthrough --> copy0MBB
554 MachineBasicBlock *thisMBB = BB;
555 MachineFunction *F = BB->getParent();
556 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
557 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
558 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
559 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
560 F->insert(I, copy0MBB);
561 F->insert(I, copy1MBB);
562 // Update machine-CFG edges by transferring all successors of the current
563 // block to the new block which will contain the Phi node for the select.
564 copy1MBB->transferSuccessors(BB);
565 // Next, add the true and fallthrough blocks as its successors.
566 BB->addSuccessor(copy0MBB);
567 BB->addSuccessor(copy1MBB);
571 // # fallthrough to copy1MBB
574 // Update machine-CFG edges
575 BB->addSuccessor(copy1MBB);
578 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
581 BuildMI(BB, dl, TII.get(SystemZ::PHI),
582 MI->getOperand(0).getReg())
583 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
584 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
586 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.