blob: 937c4c858c657507197203148ced91dbd68cced1 [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
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000048 // Set shifts properties
49 setShiftAmountFlavor(Extend);
50 setShiftAmountType(MVT::i32);
51
Anton Korobeynikov4403b932009-07-16 13:27:25 +000052 // Provide all sorts of operation actions
53
Anton Korobeynikove0167c12009-07-16 13:35:30 +000054 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000055 setSchedulingPreference(SchedulingForLatency);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000056
57 setOperationAction(ISD::RET, MVT::Other, Custom);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000058}
59
60SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
61 switch (Op.getOpcode()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000062 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
63 case ISD::RET: return LowerRET(Op, DAG);
Anton Korobeynikovba249e42009-07-16 13:50:21 +000064 case ISD::CALL: return LowerCALL(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000065 default:
66 assert(0 && "unimplemented operand");
67 return SDValue();
68 }
69}
70
71//===----------------------------------------------------------------------===//
72// Calling Convention Implementation
73//===----------------------------------------------------------------------===//
74
75#include "SystemZGenCallingConv.inc"
76
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000077SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
78 SelectionDAG &DAG) {
79 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
80 switch (CC) {
81 default:
82 assert(0 && "Unsupported calling convention");
83 case CallingConv::C:
84 case CallingConv::Fast:
85 return LowerCCCArguments(Op, DAG);
86 }
87}
88
Anton Korobeynikovba249e42009-07-16 13:50:21 +000089SDValue SystemZTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
90 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
91 unsigned CallingConv = TheCall->getCallingConv();
92 switch (CallingConv) {
93 default:
94 assert(0 && "Unsupported calling convention");
95 case CallingConv::Fast:
96 case CallingConv::C:
97 return LowerCCCCallTo(Op, DAG, CallingConv);
98 }
99}
100
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000101/// LowerCCCArguments - transform physical registers into virtual registers and
102/// generate load operations for arguments places on the stack.
103// FIXME: struct return stuff
104// FIXME: varargs
105SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
106 SelectionDAG &DAG) {
107 MachineFunction &MF = DAG.getMachineFunction();
108 MachineFrameInfo *MFI = MF.getFrameInfo();
109 MachineRegisterInfo &RegInfo = MF.getRegInfo();
110 SDValue Root = Op.getOperand(0);
111 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
112 unsigned CC = MF.getFunction()->getCallingConv();
113 DebugLoc dl = Op.getDebugLoc();
114
115 // Assign locations to all of the incoming arguments.
116 SmallVector<CCValAssign, 16> ArgLocs;
117 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
118 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
119
120 assert(!isVarArg && "Varargs not supported yet");
121
122 SmallVector<SDValue, 16> ArgValues;
123 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
124 CCValAssign &VA = ArgLocs[i];
125 if (VA.isRegLoc()) {
126 // Arguments passed in registers
127 MVT RegVT = VA.getLocVT();
128 switch (RegVT.getSimpleVT()) {
129 default:
130 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
131 << RegVT.getSimpleVT()
132 << "\n";
133 abort();
134 case MVT::i64:
135 unsigned VReg =
136 RegInfo.createVirtualRegister(SystemZ::GR64RegisterClass);
137 RegInfo.addLiveIn(VA.getLocReg(), VReg);
138 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
139
140 // If this is an 8/16/32-bit value, it is really passed promoted to 64
141 // bits. Insert an assert[sz]ext to capture this, then truncate to the
142 // right size.
143 if (VA.getLocInfo() == CCValAssign::SExt)
144 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
145 DAG.getValueType(VA.getValVT()));
146 else if (VA.getLocInfo() == CCValAssign::ZExt)
147 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
148 DAG.getValueType(VA.getValVT()));
149
150 if (VA.getLocInfo() != CCValAssign::Full)
151 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
152
153 ArgValues.push_back(ArgValue);
154 }
155 } else {
156 // Sanity check
157 assert(VA.isMemLoc());
158 // Load the argument to a virtual register
159 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
160 if (ObjSize > 8) {
161 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
162 << VA.getLocVT().getSimpleVT()
163 << "\n";
164 }
165 // Create the frame index object for this incoming parameter...
166 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset());
167
168 // Create the SelectionDAG nodes corresponding to a load
169 //from this parameter
170 SDValue FIN = DAG.getFrameIndex(FI, MVT::i64);
171 ArgValues.push_back(DAG.getLoad(VA.getLocVT(), dl, Root, FIN,
172 PseudoSourceValue::getFixedStack(FI), 0));
173 }
174 }
175
176 ArgValues.push_back(Root);
177
178 // Return the new list of results.
179 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
180 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
181}
182
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000183/// LowerCCCCallTo - functions arguments are copied from virtual regs to
184/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
185/// TODO: sret.
186SDValue SystemZTargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
187 unsigned CC) {
188 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
189 SDValue Chain = TheCall->getChain();
190 SDValue Callee = TheCall->getCallee();
191 bool isVarArg = TheCall->isVarArg();
192 DebugLoc dl = Op.getDebugLoc();
193
194 // Analyze operands of the call, assigning locations to each operand.
195 SmallVector<CCValAssign, 16> ArgLocs;
196 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
197
198 CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ);
199
200 // Get a count of how many bytes are to be pushed on the stack.
201 unsigned NumBytes = CCInfo.getNextStackOffset();
202
203 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
204 getPointerTy(), true));
205
206 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
207 SmallVector<SDValue, 12> MemOpChains;
208 SDValue StackPtr;
209
210 // Walk the register/memloc assignments, inserting copies/loads.
211 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
212 CCValAssign &VA = ArgLocs[i];
213
214 // Arguments start after the 5 first operands of ISD::CALL
215 SDValue Arg = TheCall->getArg(i);
216
217 // Promote the value if needed.
218 switch (VA.getLocInfo()) {
219 default: assert(0 && "Unknown loc info!");
220 case CCValAssign::Full: break;
221 case CCValAssign::SExt:
222 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
223 break;
224 case CCValAssign::ZExt:
225 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
226 break;
227 case CCValAssign::AExt:
228 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
229 break;
230 }
231
232 // Arguments that can be passed on register must be kept at RegsToPass
233 // vector
234 if (VA.isRegLoc()) {
235 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
236 } else {
237 assert(VA.isMemLoc());
238
239 if (StackPtr.getNode() == 0)
240 StackPtr = DAG.getCopyFromReg(Chain, dl, SystemZ::R15D, getPointerTy());
241
242 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
243 StackPtr,
244 DAG.getIntPtrConstant(VA.getLocMemOffset()));
245
246
247 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
248 PseudoSourceValue::getStack(),
249 VA.getLocMemOffset()));
250 }
251 }
252
253 // Transform all store nodes into one single node because all store nodes are
254 // independent of each other.
255 if (!MemOpChains.empty())
256 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
257 &MemOpChains[0], MemOpChains.size());
258
259 // Build a sequence of copy-to-reg nodes chained together with token chain and
260 // flag operands which copy the outgoing args into registers. The InFlag in
261 // necessary since all emited instructions must be stuck together.
262 SDValue InFlag;
263 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
264 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
265 RegsToPass[i].second, InFlag);
266 InFlag = Chain.getValue(1);
267 }
268
269 // If the callee is a GlobalAddress node (quite common, every direct call is)
270 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
271 // Likewise ExternalSymbol -> TargetExternalSymbol.
272 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
273 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
274 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
275 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
276
277 // Returns a chain & a flag for retval copy to use.
278 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
279 SmallVector<SDValue, 8> Ops;
280 Ops.push_back(Chain);
281 Ops.push_back(Callee);
282
283 // Add argument registers to the end of the list so that they are
284 // known live into the call.
285 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
286 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
287 RegsToPass[i].second.getValueType()));
288
289 if (InFlag.getNode())
290 Ops.push_back(InFlag);
291
292 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
293 InFlag = Chain.getValue(1);
294
295 // Create the CALLSEQ_END node.
296 Chain = DAG.getCALLSEQ_END(Chain,
297 DAG.getConstant(NumBytes, getPointerTy(), true),
298 DAG.getConstant(0, getPointerTy(), true),
299 InFlag);
300 InFlag = Chain.getValue(1);
301
302 // Handle result values, copying them out of physregs into vregs that we
303 // return.
304 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
305 Op.getResNo());
306}
307
308/// LowerCallResult - Lower the result values of an ISD::CALL into the
309/// appropriate copies out of appropriate physical registers. This assumes that
310/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
311/// being lowered. Returns a SDNode with the same number of values as the
312/// ISD::CALL.
313SDNode*
314SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
315 CallSDNode *TheCall,
316 unsigned CallingConv,
317 SelectionDAG &DAG) {
318 bool isVarArg = TheCall->isVarArg();
319 DebugLoc dl = TheCall->getDebugLoc();
320
321 // Assign locations to each value returned by this call.
322 SmallVector<CCValAssign, 16> RVLocs;
323 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
324
325 CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ);
326 SmallVector<SDValue, 8> ResultVals;
327
328 // Copy all of the result registers out of their specified physreg.
329 for (unsigned i = 0; i != RVLocs.size(); ++i) {
330 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
331 RVLocs[i].getValVT(), InFlag).getValue(1);
332 InFlag = Chain.getValue(2);
333 ResultVals.push_back(Chain.getValue(0));
334 }
335
336 ResultVals.push_back(Chain);
337
338 // Merge everything together with a MERGE_VALUES node.
339 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
340 &ResultVals[0], ResultVals.size()).getNode();
341}
342
343
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000344SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
345 // CCValAssign - represent the assignment of the return value to a location
346 SmallVector<CCValAssign, 16> RVLocs;
347 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
348 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
349 DebugLoc dl = Op.getDebugLoc();
350
351 // CCState - Info about the registers and stack slot.
352 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
353
354 // Analize return values of ISD::RET
355 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
356
357 // If this is the first return lowered for this function, add the regs to the
358 // liveout set for the function.
359 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
360 for (unsigned i = 0; i != RVLocs.size(); ++i)
361 if (RVLocs[i].isRegLoc())
362 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
363 }
364
365 // The chain is always operand #0
366 SDValue Chain = Op.getOperand(0);
367 SDValue Flag;
368
369 // Copy the result values into the output registers.
370 for (unsigned i = 0; i != RVLocs.size(); ++i) {
371 CCValAssign &VA = RVLocs[i];
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000372 SDValue ResValue = Op.getOperand(i*2+1);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000373 assert(VA.isRegLoc() && "Can only return in registers!");
374
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000375 // If this is an 8/16/32-bit value, it is really should be passed promoted
376 // to 64 bits.
377 if (VA.getLocInfo() == CCValAssign::SExt)
378 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
379 else if (VA.getLocInfo() == CCValAssign::ZExt)
380 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
381 else if (VA.getLocInfo() == CCValAssign::AExt)
382 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
383
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000384 // ISD::RET => ret chain, (regnum1,val1), ...
385 // So i*2+1 index only the regnums
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000386 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000387
388 // Guarantee that all emitted copies are stuck together,
389 // avoiding something bad.
390 Flag = Chain.getValue(1);
391 }
392
393 if (Flag.getNode())
394 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
395
396 // Return Void
397 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
398}
399
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000400const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
401 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000402 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000403 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000404 default: return NULL;
405 }
406}
407