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);
47 // Compute derived properties from the register classes
48 computeRegisterProperties();
50 // Set shifts properties
51 setShiftAmountFlavor(Extend);
52 setShiftAmountType(MVT::i32);
54 // Provide all sorts of operation actions
56 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
57 setSchedulingPreference(SchedulingForLatency);
59 setOperationAction(ISD::RET, MVT::Other, Custom);
61 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
62 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
63 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
65 // FIXME: Can we lower these 2 efficiently?
66 setOperationAction(ISD::SETCC, MVT::i32, Expand);
67 setOperationAction(ISD::SETCC, MVT::i64, Expand);
68 setOperationAction(ISD::SELECT, MVT::i32, Expand);
69 setOperationAction(ISD::SELECT, MVT::i64, Expand);
70 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
71 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
74 SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
75 switch (Op.getOpcode()) {
76 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
77 case ISD::RET: return LowerRET(Op, DAG);
78 case ISD::CALL: return LowerCALL(Op, DAG);
79 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
80 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
82 assert(0 && "unimplemented operand");
87 //===----------------------------------------------------------------------===//
88 // Calling Convention Implementation
89 //===----------------------------------------------------------------------===//
91 #include "SystemZGenCallingConv.inc"
93 SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
95 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
98 assert(0 && "Unsupported calling convention");
100 case CallingConv::Fast:
101 return LowerCCCArguments(Op, DAG);
105 SDValue SystemZTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
106 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
107 unsigned CallingConv = TheCall->getCallingConv();
108 switch (CallingConv) {
110 assert(0 && "Unsupported calling convention");
111 case CallingConv::Fast:
113 return LowerCCCCallTo(Op, DAG, CallingConv);
117 /// LowerCCCArguments - transform physical registers into virtual registers and
118 /// generate load operations for arguments places on the stack.
119 // FIXME: struct return stuff
121 SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
123 MachineFunction &MF = DAG.getMachineFunction();
124 MachineFrameInfo *MFI = MF.getFrameInfo();
125 MachineRegisterInfo &RegInfo = MF.getRegInfo();
126 SDValue Root = Op.getOperand(0);
127 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
128 unsigned CC = MF.getFunction()->getCallingConv();
129 DebugLoc dl = Op.getDebugLoc();
131 // Assign locations to all of the incoming arguments.
132 SmallVector<CCValAssign, 16> ArgLocs;
133 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
134 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
136 assert(!isVarArg && "Varargs not supported yet");
138 SmallVector<SDValue, 16> ArgValues;
139 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
140 CCValAssign &VA = ArgLocs[i];
142 // Arguments passed in registers
143 MVT RegVT = VA.getLocVT();
144 switch (RegVT.getSimpleVT()) {
146 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
147 << RegVT.getSimpleVT()
152 RegInfo.createVirtualRegister(SystemZ::GR64RegisterClass);
153 RegInfo.addLiveIn(VA.getLocReg(), VReg);
154 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
156 // If this is an 8/16/32-bit value, it is really passed promoted to 64
157 // bits. Insert an assert[sz]ext to capture this, then truncate to the
159 if (VA.getLocInfo() == CCValAssign::SExt)
160 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
161 DAG.getValueType(VA.getValVT()));
162 else if (VA.getLocInfo() == CCValAssign::ZExt)
163 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
164 DAG.getValueType(VA.getValVT()));
166 if (VA.getLocInfo() != CCValAssign::Full)
167 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
169 ArgValues.push_back(ArgValue);
173 assert(VA.isMemLoc());
174 // Load the argument to a virtual register
175 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
177 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
178 << VA.getLocVT().getSimpleVT()
181 // Create the frame index object for this incoming parameter...
182 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset());
184 // Create the SelectionDAG nodes corresponding to a load
185 //from this parameter
186 SDValue FIN = DAG.getFrameIndex(FI, MVT::i64);
187 ArgValues.push_back(DAG.getLoad(VA.getLocVT(), dl, Root, FIN,
188 PseudoSourceValue::getFixedStack(FI), 0));
192 ArgValues.push_back(Root);
194 // Return the new list of results.
195 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
196 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
199 /// LowerCCCCallTo - functions arguments are copied from virtual regs to
200 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
202 SDValue SystemZTargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
204 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
205 SDValue Chain = TheCall->getChain();
206 SDValue Callee = TheCall->getCallee();
207 bool isVarArg = TheCall->isVarArg();
208 DebugLoc dl = Op.getDebugLoc();
209 MachineFunction &MF = DAG.getMachineFunction();
211 // Offset to first argument stack slot.
212 const unsigned FirstArgOffset = 160;
214 // Analyze operands of the call, assigning locations to each operand.
215 SmallVector<CCValAssign, 16> ArgLocs;
216 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
218 CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ);
220 // Get a count of how many bytes are to be pushed on the stack.
221 unsigned NumBytes = CCInfo.getNextStackOffset();
223 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
224 getPointerTy(), true));
226 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
227 SmallVector<SDValue, 12> MemOpChains;
230 // Walk the register/memloc assignments, inserting copies/loads.
231 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
232 CCValAssign &VA = ArgLocs[i];
234 // Arguments start after the 5 first operands of ISD::CALL
235 SDValue Arg = TheCall->getArg(i);
237 // Promote the value if needed.
238 switch (VA.getLocInfo()) {
239 default: assert(0 && "Unknown loc info!");
240 case CCValAssign::Full: break;
241 case CCValAssign::SExt:
242 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
244 case CCValAssign::ZExt:
245 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
247 case CCValAssign::AExt:
248 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
252 // Arguments that can be passed on register must be kept at RegsToPass
255 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
257 assert(VA.isMemLoc());
259 if (StackPtr.getNode() == 0)
261 DAG.getCopyFromReg(Chain, dl,
262 (RegInfo->hasFP(MF) ?
263 SystemZ::R11D : SystemZ::R15D),
266 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
267 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
269 DAG.getIntPtrConstant(Offset));
271 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
272 PseudoSourceValue::getStack(), Offset));
276 // Transform all store nodes into one single node because all store nodes are
277 // independent of each other.
278 if (!MemOpChains.empty())
279 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
280 &MemOpChains[0], MemOpChains.size());
282 // Build a sequence of copy-to-reg nodes chained together with token chain and
283 // flag operands which copy the outgoing args into registers. The InFlag in
284 // necessary since all emited instructions must be stuck together.
286 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
287 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
288 RegsToPass[i].second, InFlag);
289 InFlag = Chain.getValue(1);
292 // If the callee is a GlobalAddress node (quite common, every direct call is)
293 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
294 // Likewise ExternalSymbol -> TargetExternalSymbol.
295 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
296 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
297 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
298 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
300 // Returns a chain & a flag for retval copy to use.
301 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
302 SmallVector<SDValue, 8> Ops;
303 Ops.push_back(Chain);
304 Ops.push_back(Callee);
306 // Add argument registers to the end of the list so that they are
307 // known live into the call.
308 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
309 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
310 RegsToPass[i].second.getValueType()));
312 if (InFlag.getNode())
313 Ops.push_back(InFlag);
315 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
316 InFlag = Chain.getValue(1);
318 // Create the CALLSEQ_END node.
319 Chain = DAG.getCALLSEQ_END(Chain,
320 DAG.getConstant(NumBytes, getPointerTy(), true),
321 DAG.getConstant(0, getPointerTy(), true),
323 InFlag = Chain.getValue(1);
325 // Handle result values, copying them out of physregs into vregs that we
327 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
331 /// LowerCallResult - Lower the result values of an ISD::CALL into the
332 /// appropriate copies out of appropriate physical registers. This assumes that
333 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
334 /// being lowered. Returns a SDNode with the same number of values as the
337 SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
339 unsigned CallingConv,
341 bool isVarArg = TheCall->isVarArg();
342 DebugLoc dl = TheCall->getDebugLoc();
344 // Assign locations to each value returned by this call.
345 SmallVector<CCValAssign, 16> RVLocs;
346 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
348 CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ);
349 SmallVector<SDValue, 8> ResultVals;
351 // Copy all of the result registers out of their specified physreg.
352 for (unsigned i = 0; i != RVLocs.size(); ++i) {
353 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
354 RVLocs[i].getValVT(), InFlag).getValue(1);
355 InFlag = Chain.getValue(2);
356 ResultVals.push_back(Chain.getValue(0));
359 ResultVals.push_back(Chain);
361 // Merge everything together with a MERGE_VALUES node.
362 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
363 &ResultVals[0], ResultVals.size()).getNode();
367 SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
368 // CCValAssign - represent the assignment of the return value to a location
369 SmallVector<CCValAssign, 16> RVLocs;
370 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
371 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
372 DebugLoc dl = Op.getDebugLoc();
374 // CCState - Info about the registers and stack slot.
375 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
377 // Analize return values of ISD::RET
378 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
380 // If this is the first return lowered for this function, add the regs to the
381 // liveout set for the function.
382 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
383 for (unsigned i = 0; i != RVLocs.size(); ++i)
384 if (RVLocs[i].isRegLoc())
385 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
388 // The chain is always operand #0
389 SDValue Chain = Op.getOperand(0);
392 // Copy the result values into the output registers.
393 for (unsigned i = 0; i != RVLocs.size(); ++i) {
394 CCValAssign &VA = RVLocs[i];
395 SDValue ResValue = Op.getOperand(i*2+1);
396 assert(VA.isRegLoc() && "Can only return in registers!");
398 // If this is an 8/16/32-bit value, it is really should be passed promoted
400 if (VA.getLocInfo() == CCValAssign::SExt)
401 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
402 else if (VA.getLocInfo() == CCValAssign::ZExt)
403 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
404 else if (VA.getLocInfo() == CCValAssign::AExt)
405 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
407 // ISD::RET => ret chain, (regnum1,val1), ...
408 // So i*2+1 index only the regnums
409 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
411 // Guarantee that all emitted copies are stuck together,
412 // avoiding something bad.
413 Flag = Chain.getValue(1);
417 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
420 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
423 SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
424 ISD::CondCode CC, SDValue &SystemZCC,
426 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
428 // FIXME: Emit a test if RHS is zero
430 bool isUnsigned = false;
431 SystemZCC::CondCodes TCC;
433 default: assert(0 && "Invalid integer condition!");
441 isUnsigned = true; // FALLTHROUGH
446 isUnsigned = true; // FALLTHROUGH
453 TCC = SystemZCC::H; // FALLTHROUGH
457 case ISD::SETLT: // FALLTHROUGH
462 SystemZCC = DAG.getConstant(TCC, MVT::i32);
464 DebugLoc dl = LHS.getDebugLoc();
465 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
466 dl, MVT::Flag, LHS, RHS);
470 SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
471 SDValue Chain = Op.getOperand(0);
472 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
473 SDValue LHS = Op.getOperand(2);
474 SDValue RHS = Op.getOperand(3);
475 SDValue Dest = Op.getOperand(4);
476 DebugLoc dl = Op.getDebugLoc();
479 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
480 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
481 Chain, Dest, SystemZCC, Flag);
484 SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
485 SDValue LHS = Op.getOperand(0);
486 SDValue RHS = Op.getOperand(1);
487 SDValue TrueV = Op.getOperand(2);
488 SDValue FalseV = Op.getOperand(3);
489 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
490 DebugLoc dl = Op.getDebugLoc();
493 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
495 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
496 SmallVector<SDValue, 4> Ops;
497 Ops.push_back(TrueV);
498 Ops.push_back(FalseV);
499 Ops.push_back(SystemZCC);
502 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
506 const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
508 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
509 case SystemZISD::CALL: return "SystemZISD::CALL";
510 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
511 case SystemZISD::CMP: return "SystemZISD::CMP";
512 case SystemZISD::UCMP: return "SystemZISD::UCMP";
513 case SystemZISD::SELECT: return "SystemZISD::SELECT";
514 default: return NULL;
518 //===----------------------------------------------------------------------===//
519 // Other Lowering Code
520 //===----------------------------------------------------------------------===//
523 SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
524 MachineBasicBlock *BB) const {
525 const SystemZInstrInfo &TII = *TM.getInstrInfo();
526 DebugLoc dl = MI->getDebugLoc();
527 assert((MI->getOpcode() == SystemZ::Select32 ||
528 MI->getOpcode() == SystemZ::Select64) &&
529 "Unexpected instr type to insert");
531 // To "insert" a SELECT instruction, we actually have to insert the diamond
532 // control-flow pattern. The incoming instruction knows the destination vreg
533 // to set, the condition code register to branch on, the true/false values to
534 // select between, and a branch opcode to use.
535 const BasicBlock *LLVM_BB = BB->getBasicBlock();
536 MachineFunction::iterator I = BB;
544 // fallthrough --> copy0MBB
545 MachineBasicBlock *thisMBB = BB;
546 MachineFunction *F = BB->getParent();
547 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
548 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
549 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
550 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
551 F->insert(I, copy0MBB);
552 F->insert(I, copy1MBB);
553 // Update machine-CFG edges by transferring all successors of the current
554 // block to the new block which will contain the Phi node for the select.
555 copy1MBB->transferSuccessors(BB);
556 // Next, add the true and fallthrough blocks as its successors.
557 BB->addSuccessor(copy0MBB);
558 BB->addSuccessor(copy1MBB);
562 // # fallthrough to copy1MBB
565 // Update machine-CFG edges
566 BB->addSuccessor(copy1MBB);
569 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
572 BuildMI(BB, dl, TII.get(SystemZ::PHI),
573 MI->getOperand(0).getReg())
574 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
575 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
577 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.