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 // Set up the register classes.
42 addRegisterClass(MVT::i32, SystemZ::GR32RegisterClass);
43 addRegisterClass(MVT::i64, SystemZ::GR64RegisterClass);
45 // Compute derived properties from the register classes
46 computeRegisterProperties();
48 // Provide all sorts of operation actions
50 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
51 setSchedulingPreference(SchedulingForLatency);
53 setOperationAction(ISD::RET, MVT::Other, Custom);
57 SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
58 switch (Op.getOpcode()) {
59 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
60 case ISD::RET: return LowerRET(Op, DAG);
62 assert(0 && "unimplemented operand");
67 //===----------------------------------------------------------------------===//
68 // Calling Convention Implementation
69 //===----------------------------------------------------------------------===//
71 #include "SystemZGenCallingConv.inc"
73 SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
75 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
78 assert(0 && "Unsupported calling convention");
80 case CallingConv::Fast:
81 return LowerCCCArguments(Op, DAG);
85 /// LowerCCCArguments - transform physical registers into virtual registers and
86 /// generate load operations for arguments places on the stack.
87 // FIXME: struct return stuff
89 SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
91 MachineFunction &MF = DAG.getMachineFunction();
92 MachineFrameInfo *MFI = MF.getFrameInfo();
93 MachineRegisterInfo &RegInfo = MF.getRegInfo();
94 SDValue Root = Op.getOperand(0);
95 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
96 unsigned CC = MF.getFunction()->getCallingConv();
97 DebugLoc dl = Op.getDebugLoc();
99 // Assign locations to all of the incoming arguments.
100 SmallVector<CCValAssign, 16> ArgLocs;
101 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
102 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
104 assert(!isVarArg && "Varargs not supported yet");
106 SmallVector<SDValue, 16> ArgValues;
107 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
108 CCValAssign &VA = ArgLocs[i];
110 // Arguments passed in registers
111 MVT RegVT = VA.getLocVT();
112 switch (RegVT.getSimpleVT()) {
114 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
115 << RegVT.getSimpleVT()
120 RegInfo.createVirtualRegister(SystemZ::GR64RegisterClass);
121 RegInfo.addLiveIn(VA.getLocReg(), VReg);
122 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
124 // If this is an 8/16/32-bit value, it is really passed promoted to 64
125 // bits. Insert an assert[sz]ext to capture this, then truncate to the
127 if (VA.getLocInfo() == CCValAssign::SExt)
128 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
129 DAG.getValueType(VA.getValVT()));
130 else if (VA.getLocInfo() == CCValAssign::ZExt)
131 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
132 DAG.getValueType(VA.getValVT()));
134 if (VA.getLocInfo() != CCValAssign::Full)
135 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
137 ArgValues.push_back(ArgValue);
141 assert(VA.isMemLoc());
142 // Load the argument to a virtual register
143 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
145 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
146 << VA.getLocVT().getSimpleVT()
149 // Create the frame index object for this incoming parameter...
150 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset());
152 // Create the SelectionDAG nodes corresponding to a load
153 //from this parameter
154 SDValue FIN = DAG.getFrameIndex(FI, MVT::i64);
155 ArgValues.push_back(DAG.getLoad(VA.getLocVT(), dl, Root, FIN,
156 PseudoSourceValue::getFixedStack(FI), 0));
160 ArgValues.push_back(Root);
162 // Return the new list of results.
163 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
164 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
167 SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
168 // CCValAssign - represent the assignment of the return value to a location
169 SmallVector<CCValAssign, 16> RVLocs;
170 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
171 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
172 DebugLoc dl = Op.getDebugLoc();
174 // CCState - Info about the registers and stack slot.
175 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
177 // Analize return values of ISD::RET
178 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
180 // If this is the first return lowered for this function, add the regs to the
181 // liveout set for the function.
182 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
183 for (unsigned i = 0; i != RVLocs.size(); ++i)
184 if (RVLocs[i].isRegLoc())
185 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
188 // The chain is always operand #0
189 SDValue Chain = Op.getOperand(0);
192 // Copy the result values into the output registers.
193 for (unsigned i = 0; i != RVLocs.size(); ++i) {
194 CCValAssign &VA = RVLocs[i];
195 SDValue ResValue = Op.getOperand(i*2+1);
196 assert(VA.isRegLoc() && "Can only return in registers!");
198 // If this is an 8/16/32-bit value, it is really should be passed promoted
200 if (VA.getLocInfo() == CCValAssign::SExt)
201 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
202 else if (VA.getLocInfo() == CCValAssign::ZExt)
203 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
204 else if (VA.getLocInfo() == CCValAssign::AExt)
205 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
207 // ISD::RET => ret chain, (regnum1,val1), ...
208 // So i*2+1 index only the regnums
209 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
211 // Guarantee that all emitted copies are stuck together,
212 // avoiding something bad.
213 Flag = Chain.getValue(1);
217 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
220 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
223 const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
225 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
226 default: return NULL;