blob: f2467affd292c07f4e9e26a6fe17f156a037e610 [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.
42 addRegisterClass(MVT::i64, SystemZ::GR64RegisterClass);
43
44 // Compute derived properties from the register classes
45 computeRegisterProperties();
46
47 // Provide all sorts of operation actions
48
Anton Korobeynikove0167c12009-07-16 13:35:30 +000049 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000050 setSchedulingPreference(SchedulingForLatency);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000051
52 setOperationAction(ISD::RET, MVT::Other, Custom);
53
Anton Korobeynikov4403b932009-07-16 13:27:25 +000054}
55
56SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
57 switch (Op.getOpcode()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000058 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
59 case ISD::RET: return LowerRET(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000060 default:
61 assert(0 && "unimplemented operand");
62 return SDValue();
63 }
64}
65
66//===----------------------------------------------------------------------===//
67// Calling Convention Implementation
68//===----------------------------------------------------------------------===//
69
70#include "SystemZGenCallingConv.inc"
71
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000072SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
73 SelectionDAG &DAG) {
74 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
75 switch (CC) {
76 default:
77 assert(0 && "Unsupported calling convention");
78 case CallingConv::C:
79 case CallingConv::Fast:
80 return LowerCCCArguments(Op, DAG);
81 }
82}
83
84/// LowerCCCArguments - transform physical registers into virtual registers and
85/// generate load operations for arguments places on the stack.
86// FIXME: struct return stuff
87// FIXME: varargs
88SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
89 SelectionDAG &DAG) {
90 MachineFunction &MF = DAG.getMachineFunction();
91 MachineFrameInfo *MFI = MF.getFrameInfo();
92 MachineRegisterInfo &RegInfo = MF.getRegInfo();
93 SDValue Root = Op.getOperand(0);
94 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
95 unsigned CC = MF.getFunction()->getCallingConv();
96 DebugLoc dl = Op.getDebugLoc();
97
98 // Assign locations to all of the incoming arguments.
99 SmallVector<CCValAssign, 16> ArgLocs;
100 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
101 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
102
103 assert(!isVarArg && "Varargs not supported yet");
104
105 SmallVector<SDValue, 16> ArgValues;
106 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
107 CCValAssign &VA = ArgLocs[i];
108 if (VA.isRegLoc()) {
109 // Arguments passed in registers
110 MVT RegVT = VA.getLocVT();
111 switch (RegVT.getSimpleVT()) {
112 default:
113 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
114 << RegVT.getSimpleVT()
115 << "\n";
116 abort();
117 case MVT::i64:
118 unsigned VReg =
119 RegInfo.createVirtualRegister(SystemZ::GR64RegisterClass);
120 RegInfo.addLiveIn(VA.getLocReg(), VReg);
121 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
122
123 // If this is an 8/16/32-bit value, it is really passed promoted to 64
124 // bits. Insert an assert[sz]ext to capture this, then truncate to the
125 // right size.
126 if (VA.getLocInfo() == CCValAssign::SExt)
127 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
128 DAG.getValueType(VA.getValVT()));
129 else if (VA.getLocInfo() == CCValAssign::ZExt)
130 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
131 DAG.getValueType(VA.getValVT()));
132
133 if (VA.getLocInfo() != CCValAssign::Full)
134 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
135
136 ArgValues.push_back(ArgValue);
137 }
138 } else {
139 // Sanity check
140 assert(VA.isMemLoc());
141 // Load the argument to a virtual register
142 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
143 if (ObjSize > 8) {
144 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
145 << VA.getLocVT().getSimpleVT()
146 << "\n";
147 }
148 // Create the frame index object for this incoming parameter...
149 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset());
150
151 // Create the SelectionDAG nodes corresponding to a load
152 //from this parameter
153 SDValue FIN = DAG.getFrameIndex(FI, MVT::i64);
154 ArgValues.push_back(DAG.getLoad(VA.getLocVT(), dl, Root, FIN,
155 PseudoSourceValue::getFixedStack(FI), 0));
156 }
157 }
158
159 ArgValues.push_back(Root);
160
161 // Return the new list of results.
162 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
163 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
164}
165
166SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
167 // CCValAssign - represent the assignment of the return value to a location
168 SmallVector<CCValAssign, 16> RVLocs;
169 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
170 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
171 DebugLoc dl = Op.getDebugLoc();
172
173 // CCState - Info about the registers and stack slot.
174 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
175
176 // Analize return values of ISD::RET
177 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
178
179 // If this is the first return lowered for this function, add the regs to the
180 // liveout set for the function.
181 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
182 for (unsigned i = 0; i != RVLocs.size(); ++i)
183 if (RVLocs[i].isRegLoc())
184 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
185 }
186
187 // The chain is always operand #0
188 SDValue Chain = Op.getOperand(0);
189 SDValue Flag;
190
191 // Copy the result values into the output registers.
192 for (unsigned i = 0; i != RVLocs.size(); ++i) {
193 CCValAssign &VA = RVLocs[i];
194 assert(VA.isRegLoc() && "Can only return in registers!");
195
196 // ISD::RET => ret chain, (regnum1,val1), ...
197 // So i*2+1 index only the regnums
198 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
199 Op.getOperand(i*2+1), Flag);
200
201 // Guarantee that all emitted copies are stuck together,
202 // avoiding something bad.
203 Flag = Chain.getValue(1);
204 }
205
206 if (Flag.getNode())
207 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
208
209 // Return Void
210 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
211}
212
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000213const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
214 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000215 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000216 default: return NULL;
217 }
218}
219