blob: 64bd4767df64c8e60c68a9010bc3027ff200a99e [file] [log] [blame]
Anton Korobeynikov4403b932009-07-16 13:27:25 +00001//===-- SystemZISelLowering.cpp - SystemZ DAG Lowering Implementation -----==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SystemZTargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "systemz-lower"
15
16#include "SystemZISelLowering.h"
17#include "SystemZ.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"
36using namespace llvm;
37
38SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm) :
39 TargetLowering(tm), Subtarget(*tm.getSubtargetImpl()), TM(tm) {
40
41 // Set up the register classes.
Anton Korobeynikova51752c2009-07-16 13:42:31 +000042 addRegisterClass(MVT::i32, SystemZ::GR32RegisterClass);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000043 addRegisterClass(MVT::i64, SystemZ::GR64RegisterClass);
44
45 // Compute derived properties from the register classes
46 computeRegisterProperties();
47
48 // Provide all sorts of operation actions
49
Anton Korobeynikove0167c12009-07-16 13:35:30 +000050 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000051 setSchedulingPreference(SchedulingForLatency);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000052
53 setOperationAction(ISD::RET, MVT::Other, Custom);
54
Anton Korobeynikov4403b932009-07-16 13:27:25 +000055}
56
57SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
58 switch (Op.getOpcode()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000059 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
60 case ISD::RET: return LowerRET(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000061 default:
62 assert(0 && "unimplemented operand");
63 return SDValue();
64 }
65}
66
67//===----------------------------------------------------------------------===//
68// Calling Convention Implementation
69//===----------------------------------------------------------------------===//
70
71#include "SystemZGenCallingConv.inc"
72
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000073SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
74 SelectionDAG &DAG) {
75 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
76 switch (CC) {
77 default:
78 assert(0 && "Unsupported calling convention");
79 case CallingConv::C:
80 case CallingConv::Fast:
81 return LowerCCCArguments(Op, DAG);
82 }
83}
84
85/// LowerCCCArguments - transform physical registers into virtual registers and
86/// generate load operations for arguments places on the stack.
87// FIXME: struct return stuff
88// FIXME: varargs
89SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
90 SelectionDAG &DAG) {
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();
98
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);
103
104 assert(!isVarArg && "Varargs not supported yet");
105
106 SmallVector<SDValue, 16> ArgValues;
107 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
108 CCValAssign &VA = ArgLocs[i];
109 if (VA.isRegLoc()) {
110 // Arguments passed in registers
111 MVT RegVT = VA.getLocVT();
112 switch (RegVT.getSimpleVT()) {
113 default:
114 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
115 << RegVT.getSimpleVT()
116 << "\n";
117 abort();
118 case MVT::i64:
119 unsigned VReg =
120 RegInfo.createVirtualRegister(SystemZ::GR64RegisterClass);
121 RegInfo.addLiveIn(VA.getLocReg(), VReg);
122 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
123
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
126 // right size.
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()));
133
134 if (VA.getLocInfo() != CCValAssign::Full)
135 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
136
137 ArgValues.push_back(ArgValue);
138 }
139 } else {
140 // Sanity check
141 assert(VA.isMemLoc());
142 // Load the argument to a virtual register
143 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
144 if (ObjSize > 8) {
145 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
146 << VA.getLocVT().getSimpleVT()
147 << "\n";
148 }
149 // Create the frame index object for this incoming parameter...
150 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset());
151
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));
157 }
158 }
159
160 ArgValues.push_back(Root);
161
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());
165}
166
167SDValue 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();
173
174 // CCState - Info about the registers and stack slot.
175 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
176
177 // Analize return values of ISD::RET
178 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
179
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());
186 }
187
188 // The chain is always operand #0
189 SDValue Chain = Op.getOperand(0);
190 SDValue Flag;
191
192 // Copy the result values into the output registers.
193 for (unsigned i = 0; i != RVLocs.size(); ++i) {
194 CCValAssign &VA = RVLocs[i];
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000195 SDValue ResValue = Op.getOperand(i*2+1);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000196 assert(VA.isRegLoc() && "Can only return in registers!");
197
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000198 // If this is an 8/16/32-bit value, it is really should be passed promoted
199 // to 64 bits.
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);
206
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000207 // ISD::RET => ret chain, (regnum1,val1), ...
208 // So i*2+1 index only the regnums
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000209 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000210
211 // Guarantee that all emitted copies are stuck together,
212 // avoiding something bad.
213 Flag = Chain.getValue(1);
214 }
215
216 if (Flag.getNode())
217 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
218
219 // Return Void
220 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
221}
222
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000223const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
224 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000225 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000226 default: return NULL;
227 }
228}
229