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