blob: 51a3e641d5d59cccf29bfc4fdf6e99ac3388e66f [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"
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000035#include "llvm/Target/TargetOptions.h"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000036#include "llvm/ADT/VectorExtras.h"
37using namespace llvm;
38
39SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm) :
40 TargetLowering(tm), Subtarget(*tm.getSubtargetImpl()), TM(tm) {
41
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +000042 RegInfo = TM.getRegisterInfo();
43
Anton Korobeynikov4403b932009-07-16 13:27:25 +000044 // Set up the register classes.
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +000045 addRegisterClass(MVT::i32, SystemZ::GR32RegisterClass);
46 addRegisterClass(MVT::i64, SystemZ::GR64RegisterClass);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +000047 addRegisterClass(MVT::v2i32,SystemZ::GR64PRegisterClass);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +000048 addRegisterClass(MVT::v2i64,SystemZ::GR128RegisterClass);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000049
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000050 if (!UseSoftFloat) {
51 addRegisterClass(MVT::f32, SystemZ::FP32RegisterClass);
52 addRegisterClass(MVT::f64, SystemZ::FP64RegisterClass);
Anton Korobeynikov1ada84d2009-07-16 14:24:16 +000053
54 addLegalFPImmediate(APFloat(+0.0)); // lzer
55 addLegalFPImmediate(APFloat(+0.0f)); // lzdr
56 addLegalFPImmediate(APFloat(-0.0)); // lzer + lner
57 addLegalFPImmediate(APFloat(-0.0f)); // lzdr + lndr
Anton Korobeynikov2c97ae82009-07-16 14:19:02 +000058 }
59
Anton Korobeynikov4403b932009-07-16 13:27:25 +000060 // Compute derived properties from the register classes
61 computeRegisterProperties();
62
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000063 // Set shifts properties
64 setShiftAmountFlavor(Extend);
Anton Korobeynikov48e8b3c2009-07-16 14:15:24 +000065 setShiftAmountType(MVT::i64);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000066
Anton Korobeynikov4403b932009-07-16 13:27:25 +000067 // Provide all sorts of operation actions
Anton Korobeynikovbf022172009-07-16 13:53:35 +000068 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
69 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
70 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000071
Anton Korobeynikov85c5c3f2009-07-16 14:22:46 +000072 setLoadExtAction(ISD::SEXTLOAD, MVT::f32, Expand);
73 setLoadExtAction(ISD::ZEXTLOAD, MVT::f32, Expand);
74 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Anton Korobeynikov299dc782009-07-16 14:22:30 +000075
Anton Korobeynikov85c5c3f2009-07-16 14:22:46 +000076 setLoadExtAction(ISD::SEXTLOAD, MVT::f64, Expand);
77 setLoadExtAction(ISD::ZEXTLOAD, MVT::f64, Expand);
78 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
Anton Korobeynikov23eff5c2009-07-16 14:20:08 +000079
Anton Korobeynikove0167c12009-07-16 13:35:30 +000080 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000081 setSchedulingPreference(SchedulingForLatency);
Anton Korobeynikov159ac632009-07-16 14:28:46 +000082 setBooleanContents(ZeroOrOneBooleanContent);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +000083
84 setOperationAction(ISD::RET, MVT::Other, Custom);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000085
Anton Korobeynikov983d3a12009-07-16 14:07:24 +000086 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +000087 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
88 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
89 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Anton Korobeynikovae535672009-07-16 14:19:35 +000090 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
91 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
92 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
93 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +000094 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +000095 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Anton Korobeynikovc772c442009-07-16 14:08:15 +000096 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +000097
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +000098 setOperationAction(ISD::SDIV, MVT::i32, Expand);
99 setOperationAction(ISD::UDIV, MVT::i32, Expand);
100 setOperationAction(ISD::SDIV, MVT::i64, Expand);
101 setOperationAction(ISD::UDIV, MVT::i64, Expand);
102 setOperationAction(ISD::SREM, MVT::i32, Expand);
103 setOperationAction(ISD::UREM, MVT::i32, Expand);
104 setOperationAction(ISD::SREM, MVT::i64, Expand);
105 setOperationAction(ISD::UREM, MVT::i64, Expand);
106
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000107 // FIXME: Can we lower these 2 efficiently?
108 setOperationAction(ISD::SETCC, MVT::i32, Expand);
109 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000110 setOperationAction(ISD::SETCC, MVT::f32, Expand);
111 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000112 setOperationAction(ISD::SELECT, MVT::i32, Expand);
113 setOperationAction(ISD::SELECT, MVT::i64, Expand);
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000114 setOperationAction(ISD::SELECT, MVT::f32, Expand);
115 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000116 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
117 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000118 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
119 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000120
Anton Korobeynikov8d1837d2009-07-16 13:56:42 +0000121 // Funny enough: we don't have 64-bit signed versions of these stuff, but have
122 // unsigned.
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000123 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Anton Korobeynikovdd0239b2009-07-16 13:53:55 +0000124 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
Anton Korobeynikov9b4ae572009-07-16 14:20:56 +0000125
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000126 // Lower some FP stuff
Anton Korobeynikov9b4ae572009-07-16 14:20:56 +0000127 setOperationAction(ISD::FSIN, MVT::f32, Expand);
128 setOperationAction(ISD::FSIN, MVT::f64, Expand);
129 setOperationAction(ISD::FCOS, MVT::f32, Expand);
130 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Anton Korobeynikov98db78a2009-07-16 14:26:06 +0000131
Anton Korobeynikov05a0b8b2009-07-16 14:27:01 +0000132 // We have only 64-bit bitconverts
133 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Promote);
134 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Promote);
135
Anton Korobeynikova89430e2009-07-16 14:25:30 +0000136 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
137 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Anton Korobeynikov98db78a2009-07-16 14:26:06 +0000138 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
139 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Anton Korobeynikov20d062f2009-07-16 14:25:46 +0000140
141 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000142}
143
144SDValue SystemZTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
145 switch (Op.getOpcode()) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000146 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
147 case ISD::RET: return LowerRET(Op, DAG);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000148 case ISD::CALL: return LowerCALL(Op, DAG);
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000149 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000150 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000151 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000152 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Anton Korobeynikovae535672009-07-16 14:19:35 +0000153 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000154 default:
155 assert(0 && "unimplemented operand");
156 return SDValue();
157 }
158}
159
160//===----------------------------------------------------------------------===//
161// Calling Convention Implementation
162//===----------------------------------------------------------------------===//
163
164#include "SystemZGenCallingConv.inc"
165
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000166SDValue SystemZTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
167 SelectionDAG &DAG) {
168 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
169 switch (CC) {
170 default:
171 assert(0 && "Unsupported calling convention");
172 case CallingConv::C:
173 case CallingConv::Fast:
174 return LowerCCCArguments(Op, DAG);
175 }
176}
177
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000178SDValue SystemZTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
179 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
180 unsigned CallingConv = TheCall->getCallingConv();
181 switch (CallingConv) {
182 default:
183 assert(0 && "Unsupported calling convention");
184 case CallingConv::Fast:
185 case CallingConv::C:
186 return LowerCCCCallTo(Op, DAG, CallingConv);
187 }
188}
189
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000190/// LowerCCCArguments - transform physical registers into virtual registers and
191/// generate load operations for arguments places on the stack.
192// FIXME: struct return stuff
193// FIXME: varargs
194SDValue SystemZTargetLowering::LowerCCCArguments(SDValue Op,
195 SelectionDAG &DAG) {
196 MachineFunction &MF = DAG.getMachineFunction();
197 MachineFrameInfo *MFI = MF.getFrameInfo();
198 MachineRegisterInfo &RegInfo = MF.getRegInfo();
199 SDValue Root = Op.getOperand(0);
200 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
201 unsigned CC = MF.getFunction()->getCallingConv();
202 DebugLoc dl = Op.getDebugLoc();
203
204 // Assign locations to all of the incoming arguments.
205 SmallVector<CCValAssign, 16> ArgLocs;
206 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
207 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_SystemZ);
208
209 assert(!isVarArg && "Varargs not supported yet");
210
211 SmallVector<SDValue, 16> ArgValues;
212 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
213 CCValAssign &VA = ArgLocs[i];
214 if (VA.isRegLoc()) {
215 // Arguments passed in registers
216 MVT RegVT = VA.getLocVT();
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000217 TargetRegisterClass *RC;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000218 switch (RegVT.getSimpleVT()) {
219 default:
220 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
221 << RegVT.getSimpleVT()
222 << "\n";
223 abort();
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000224 case MVT::i64:
225 RC = SystemZ::GR64RegisterClass;
226 break;
227 case MVT::f32:
228 RC = SystemZ::FP32RegisterClass;
229 break;
230 case MVT::f64:
231 RC = SystemZ::FP64RegisterClass;
232 break;
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000233 }
Anton Korobeynikov0e31d5c2009-07-16 14:19:16 +0000234
235 unsigned VReg = RegInfo.createVirtualRegister(RC);
236 RegInfo.addLiveIn(VA.getLocReg(), VReg);
237 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
238
239 // If this is an 8/16/32-bit value, it is really passed promoted to 64
240 // bits. Insert an assert[sz]ext to capture this, then truncate to the
241 // right size.
242 if (VA.getLocInfo() == CCValAssign::SExt)
243 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
244 DAG.getValueType(VA.getValVT()));
245 else if (VA.getLocInfo() == CCValAssign::ZExt)
246 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
247 DAG.getValueType(VA.getValVT()));
248
249 if (VA.getLocInfo() != CCValAssign::Full)
250 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
251
252 ArgValues.push_back(ArgValue);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000253 } else {
254 // Sanity check
255 assert(VA.isMemLoc());
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000256
257 // Create the nodes corresponding to a load from this parameter slot.
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000258 // Create the frame index object for this incoming parameter...
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000259 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
260 VA.getLocMemOffset());
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000261
262 // Create the SelectionDAG nodes corresponding to a load
263 //from this parameter
Anton Korobeynikov980d5502009-07-16 14:08:42 +0000264 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
265 ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN,
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000266 PseudoSourceValue::getFixedStack(FI), 0));
267 }
268 }
269
270 ArgValues.push_back(Root);
271
272 // Return the new list of results.
273 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
274 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
275}
276
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000277/// LowerCCCCallTo - functions arguments are copied from virtual regs to
278/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
279/// TODO: sret.
280SDValue SystemZTargetLowering::LowerCCCCallTo(SDValue Op, SelectionDAG &DAG,
281 unsigned CC) {
282 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
283 SDValue Chain = TheCall->getChain();
284 SDValue Callee = TheCall->getCallee();
285 bool isVarArg = TheCall->isVarArg();
286 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000287 MachineFunction &MF = DAG.getMachineFunction();
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000288
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000289 // Offset to first argument stack slot.
290 const unsigned FirstArgOffset = 160;
291
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000292 // Analyze operands of the call, assigning locations to each operand.
293 SmallVector<CCValAssign, 16> ArgLocs;
294 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
295
296 CCInfo.AnalyzeCallOperands(TheCall, CC_SystemZ);
297
298 // Get a count of how many bytes are to be pushed on the stack.
299 unsigned NumBytes = CCInfo.getNextStackOffset();
300
301 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
302 getPointerTy(), true));
303
304 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
305 SmallVector<SDValue, 12> MemOpChains;
306 SDValue StackPtr;
307
308 // Walk the register/memloc assignments, inserting copies/loads.
309 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
310 CCValAssign &VA = ArgLocs[i];
311
312 // Arguments start after the 5 first operands of ISD::CALL
313 SDValue Arg = TheCall->getArg(i);
314
315 // Promote the value if needed.
316 switch (VA.getLocInfo()) {
317 default: assert(0 && "Unknown loc info!");
318 case CCValAssign::Full: break;
319 case CCValAssign::SExt:
320 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
321 break;
322 case CCValAssign::ZExt:
323 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
324 break;
325 case CCValAssign::AExt:
326 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
327 break;
328 }
329
330 // Arguments that can be passed on register must be kept at RegsToPass
331 // vector
332 if (VA.isRegLoc()) {
333 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
334 } else {
335 assert(VA.isMemLoc());
336
337 if (StackPtr.getNode() == 0)
Anton Korobeynikov656ac6f2009-07-16 13:51:53 +0000338 StackPtr =
339 DAG.getCopyFromReg(Chain, dl,
340 (RegInfo->hasFP(MF) ?
341 SystemZ::R11D : SystemZ::R15D),
342 getPointerTy());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000343
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000344 unsigned Offset = FirstArgOffset + VA.getLocMemOffset();
345 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
346 StackPtr,
347 DAG.getIntPtrConstant(Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000348
349 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Anton Korobeynikovc7b71be2009-07-16 13:52:10 +0000350 PseudoSourceValue::getStack(), Offset));
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000351 }
352 }
353
354 // Transform all store nodes into one single node because all store nodes are
355 // independent of each other.
356 if (!MemOpChains.empty())
357 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
358 &MemOpChains[0], MemOpChains.size());
359
360 // Build a sequence of copy-to-reg nodes chained together with token chain and
361 // flag operands which copy the outgoing args into registers. The InFlag in
362 // necessary since all emited instructions must be stuck together.
363 SDValue InFlag;
364 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
365 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
366 RegsToPass[i].second, InFlag);
367 InFlag = Chain.getValue(1);
368 }
369
370 // If the callee is a GlobalAddress node (quite common, every direct call is)
371 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
372 // Likewise ExternalSymbol -> TargetExternalSymbol.
373 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
374 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
375 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
376 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
377
378 // Returns a chain & a flag for retval copy to use.
379 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
380 SmallVector<SDValue, 8> Ops;
381 Ops.push_back(Chain);
382 Ops.push_back(Callee);
383
384 // Add argument registers to the end of the list so that they are
385 // known live into the call.
386 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
387 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
388 RegsToPass[i].second.getValueType()));
389
390 if (InFlag.getNode())
391 Ops.push_back(InFlag);
392
393 Chain = DAG.getNode(SystemZISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
394 InFlag = Chain.getValue(1);
395
396 // Create the CALLSEQ_END node.
397 Chain = DAG.getCALLSEQ_END(Chain,
398 DAG.getConstant(NumBytes, getPointerTy(), true),
399 DAG.getConstant(0, getPointerTy(), true),
400 InFlag);
401 InFlag = Chain.getValue(1);
402
403 // Handle result values, copying them out of physregs into vregs that we
404 // return.
405 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
406 Op.getResNo());
407}
408
409/// LowerCallResult - Lower the result values of an ISD::CALL into the
410/// appropriate copies out of appropriate physical registers. This assumes that
411/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
412/// being lowered. Returns a SDNode with the same number of values as the
413/// ISD::CALL.
414SDNode*
415SystemZTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
416 CallSDNode *TheCall,
417 unsigned CallingConv,
418 SelectionDAG &DAG) {
419 bool isVarArg = TheCall->isVarArg();
420 DebugLoc dl = TheCall->getDebugLoc();
421
422 // Assign locations to each value returned by this call.
423 SmallVector<CCValAssign, 16> RVLocs;
424 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
425
426 CCInfo.AnalyzeCallResult(TheCall, RetCC_SystemZ);
427 SmallVector<SDValue, 8> ResultVals;
428
429 // Copy all of the result registers out of their specified physreg.
430 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000431 CCValAssign &VA = RVLocs[i];
432
433 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
434 VA.getLocVT(), InFlag).getValue(1);
435 SDValue RetValue = Chain.getValue(0);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000436 InFlag = Chain.getValue(2);
Anton Korobeynikov22836d12009-07-16 13:58:24 +0000437
438 // If this is an 8/16/32-bit value, it is really passed promoted to 64
439 // bits. Insert an assert[sz]ext to capture this, then truncate to the
440 // right size.
441 if (VA.getLocInfo() == CCValAssign::SExt)
442 RetValue = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), RetValue,
443 DAG.getValueType(VA.getValVT()));
444 else if (VA.getLocInfo() == CCValAssign::ZExt)
445 RetValue = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), RetValue,
446 DAG.getValueType(VA.getValVT()));
447
448 if (VA.getLocInfo() != CCValAssign::Full)
449 RetValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), RetValue);
450
451 ResultVals.push_back(RetValue);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000452 }
453
454 ResultVals.push_back(Chain);
455
456 // Merge everything together with a MERGE_VALUES node.
457 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
458 &ResultVals[0], ResultVals.size()).getNode();
459}
460
461
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000462SDValue SystemZTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
463 // CCValAssign - represent the assignment of the return value to a location
464 SmallVector<CCValAssign, 16> RVLocs;
465 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
466 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
467 DebugLoc dl = Op.getDebugLoc();
468
469 // CCState - Info about the registers and stack slot.
470 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
471
472 // Analize return values of ISD::RET
473 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_SystemZ);
474
475 // If this is the first return lowered for this function, add the regs to the
476 // liveout set for the function.
477 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
478 for (unsigned i = 0; i != RVLocs.size(); ++i)
479 if (RVLocs[i].isRegLoc())
480 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
481 }
482
483 // The chain is always operand #0
484 SDValue Chain = Op.getOperand(0);
485 SDValue Flag;
486
487 // Copy the result values into the output registers.
488 for (unsigned i = 0; i != RVLocs.size(); ++i) {
489 CCValAssign &VA = RVLocs[i];
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000490 SDValue ResValue = Op.getOperand(i*2+1);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000491 assert(VA.isRegLoc() && "Can only return in registers!");
492
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000493 // If this is an 8/16/32-bit value, it is really should be passed promoted
494 // to 64 bits.
495 if (VA.getLocInfo() == CCValAssign::SExt)
496 ResValue = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ResValue);
497 else if (VA.getLocInfo() == CCValAssign::ZExt)
498 ResValue = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ResValue);
499 else if (VA.getLocInfo() == CCValAssign::AExt)
500 ResValue = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ResValue);
501
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000502 // ISD::RET => ret chain, (regnum1,val1), ...
503 // So i*2+1 index only the regnums
Anton Korobeynikova51752c2009-07-16 13:42:31 +0000504 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ResValue, Flag);
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000505
506 // Guarantee that all emitted copies are stuck together,
507 // avoiding something bad.
508 Flag = Chain.getValue(1);
509 }
510
511 if (Flag.getNode())
512 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
513
514 // Return Void
515 return DAG.getNode(SystemZISD::RET_FLAG, dl, MVT::Other, Chain);
516}
517
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000518SDValue SystemZTargetLowering::EmitCmp(SDValue LHS, SDValue RHS,
519 ISD::CondCode CC, SDValue &SystemZCC,
520 SelectionDAG &DAG) {
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000521 // FIXME: Emit a test if RHS is zero
522
523 bool isUnsigned = false;
524 SystemZCC::CondCodes TCC;
525 switch (CC) {
526 default: assert(0 && "Invalid integer condition!");
527 case ISD::SETEQ:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000528 case ISD::SETOEQ:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000529 TCC = SystemZCC::E;
530 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000531 case ISD::SETUEQ:
532 TCC = SystemZCC::NLH;
533 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000534 case ISD::SETNE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000535 case ISD::SETONE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000536 TCC = SystemZCC::NE;
537 break;
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000538 case ISD::SETUNE:
539 TCC = SystemZCC::LH;
540 break;
541 case ISD::SETO:
542 TCC = SystemZCC::O;
543 break;
544 case ISD::SETUO:
545 TCC = SystemZCC::NO;
546 break;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000547 case ISD::SETULE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000548 if (LHS.getValueType().isFloatingPoint()) {
549 TCC = SystemZCC::NH;
550 break;
551 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000552 isUnsigned = true; // FALLTHROUGH
553 case ISD::SETLE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000554 case ISD::SETOLE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000555 TCC = SystemZCC::LE;
556 break;
557 case ISD::SETUGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000558 if (LHS.getValueType().isFloatingPoint()) {
559 TCC = SystemZCC::NL;
560 break;
561 }
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000562 isUnsigned = true; // FALLTHROUGH
563 case ISD::SETGE:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000564 case ISD::SETOGE:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000565 TCC = SystemZCC::HE;
566 break;
567 case ISD::SETUGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000568 if (LHS.getValueType().isFloatingPoint()) {
569 TCC = SystemZCC::NLE;
570 break;
571 }
572 isUnsigned = true; // FALLTHROUGH
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000573 case ISD::SETGT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000574 case ISD::SETOGT:
575 TCC = SystemZCC::H;
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000576 break;
577 case ISD::SETULT:
Anton Korobeynikov10c086c2009-07-16 14:19:54 +0000578 if (LHS.getValueType().isFloatingPoint()) {
579 TCC = SystemZCC::NHE;
580 break;
581 }
582 isUnsigned = true; // FALLTHROUGH
583 case ISD::SETLT:
584 case ISD::SETOLT:
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000585 TCC = SystemZCC::L;
586 break;
587 }
588
589 SystemZCC = DAG.getConstant(TCC, MVT::i32);
590
591 DebugLoc dl = LHS.getDebugLoc();
592 return DAG.getNode((isUnsigned ? SystemZISD::UCMP : SystemZISD::CMP),
593 dl, MVT::Flag, LHS, RHS);
594}
595
596
597SDValue SystemZTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
598 SDValue Chain = Op.getOperand(0);
599 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
600 SDValue LHS = Op.getOperand(2);
601 SDValue RHS = Op.getOperand(3);
602 SDValue Dest = Op.getOperand(4);
603 DebugLoc dl = Op.getDebugLoc();
604
605 SDValue SystemZCC;
606 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
607 return DAG.getNode(SystemZISD::BRCOND, dl, Op.getValueType(),
608 Chain, Dest, SystemZCC, Flag);
609}
610
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000611SDValue SystemZTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
612 SDValue LHS = Op.getOperand(0);
613 SDValue RHS = Op.getOperand(1);
614 SDValue TrueV = Op.getOperand(2);
615 SDValue FalseV = Op.getOperand(3);
616 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
617 DebugLoc dl = Op.getDebugLoc();
618
619 SDValue SystemZCC;
620 SDValue Flag = EmitCmp(LHS, RHS, CC, SystemZCC, DAG);
621
622 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
623 SmallVector<SDValue, 4> Ops;
624 Ops.push_back(TrueV);
625 Ops.push_back(FalseV);
626 Ops.push_back(SystemZCC);
627 Ops.push_back(Flag);
628
629 return DAG.getNode(SystemZISD::SELECT, dl, VTs, &Ops[0], Ops.size());
630}
631
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000632SDValue SystemZTargetLowering::LowerGlobalAddress(SDValue Op,
633 SelectionDAG &DAG) {
634 DebugLoc dl = Op.getDebugLoc();
635 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000636 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000637
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000638 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
639 bool ExtraLoadRequired =
640 Subtarget.GVRequiresExtraLoad(GV, getTargetMachine(), false);
641
642 SDValue Result;
643 if (!IsPic && !ExtraLoadRequired) {
644 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
645 Offset = 0;
646 } else {
647 unsigned char OpFlags = 0;
648 if (ExtraLoadRequired)
649 OpFlags = SystemZII::MO_GOTENT;
650
651 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
652 }
653
654 Result = DAG.getNode(SystemZISD::PCRelativeWrapper, dl,
655 getPointerTy(), Result);
656
657 if (ExtraLoadRequired)
658 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
659 PseudoSourceValue::getGOT(), 0);
660
661 // If there was a non-zero offset that we didn't fold, create an explicit
662 // addition for it.
663 if (Offset != 0)
664 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
665 DAG.getConstant(Offset, getPointerTy()));
666
667 return Result;
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000668}
669
Anton Korobeynikovae535672009-07-16 14:19:35 +0000670// FIXME: PIC here
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000671SDValue SystemZTargetLowering::LowerJumpTable(SDValue Op,
672 SelectionDAG &DAG) {
673 DebugLoc dl = Op.getDebugLoc();
674 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
675 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
676
677 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
678}
679
Anton Korobeynikovae535672009-07-16 14:19:35 +0000680
681// FIXME: PIC here
682// FIXME: This is just dirty hack. We need to lower cpool properly
683SDValue SystemZTargetLowering::LowerConstantPool(SDValue Op,
684 SelectionDAG &DAG) {
685 DebugLoc dl = Op.getDebugLoc();
686 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
687
688 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
689 CP->getAlignment(),
690 CP->getOffset());
691
692 return DAG.getNode(SystemZISD::PCRelativeWrapper, dl, getPointerTy(), Result);
693}
694
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000695const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
696 switch (Opcode) {
Anton Korobeynikov87a24e32009-07-16 13:28:59 +0000697 case SystemZISD::RET_FLAG: return "SystemZISD::RET_FLAG";
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000698 case SystemZISD::CALL: return "SystemZISD::CALL";
Anton Korobeynikov4ec3e5f2009-07-16 13:52:31 +0000699 case SystemZISD::BRCOND: return "SystemZISD::BRCOND";
700 case SystemZISD::CMP: return "SystemZISD::CMP";
701 case SystemZISD::UCMP: return "SystemZISD::UCMP";
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000702 case SystemZISD::SELECT: return "SystemZISD::SELECT";
Anton Korobeynikovbad769f2009-07-16 13:57:27 +0000703 case SystemZISD::PCRelativeWrapper: return "SystemZISD::PCRelativeWrapper";
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000704 default: return NULL;
705 }
706}
707
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000708//===----------------------------------------------------------------------===//
709// Other Lowering Code
710//===----------------------------------------------------------------------===//
711
712MachineBasicBlock*
713SystemZTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
714 MachineBasicBlock *BB) const {
715 const SystemZInstrInfo &TII = *TM.getInstrInfo();
716 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovda723d72009-07-16 14:22:15 +0000717 assert((MI->getOpcode() == SystemZ::Select32 ||
718 MI->getOpcode() == SystemZ::SelectF32 ||
719 MI->getOpcode() == SystemZ::Select64 ||
720 MI->getOpcode() == SystemZ::SelectF64) &&
Anton Korobeynikov7d1e39b2009-07-16 13:52:51 +0000721 "Unexpected instr type to insert");
722
723 // To "insert" a SELECT instruction, we actually have to insert the diamond
724 // control-flow pattern. The incoming instruction knows the destination vreg
725 // to set, the condition code register to branch on, the true/false values to
726 // select between, and a branch opcode to use.
727 const BasicBlock *LLVM_BB = BB->getBasicBlock();
728 MachineFunction::iterator I = BB;
729 ++I;
730
731 // thisMBB:
732 // ...
733 // TrueVal = ...
734 // cmpTY ccX, r1, r2
735 // jCC copy1MBB
736 // fallthrough --> copy0MBB
737 MachineBasicBlock *thisMBB = BB;
738 MachineFunction *F = BB->getParent();
739 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
740 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
741 SystemZCC::CondCodes CC = (SystemZCC::CondCodes)MI->getOperand(3).getImm();
742 BuildMI(BB, dl, TII.getBrCond(CC)).addMBB(copy1MBB);
743 F->insert(I, copy0MBB);
744 F->insert(I, copy1MBB);
745 // Update machine-CFG edges by transferring all successors of the current
746 // block to the new block which will contain the Phi node for the select.
747 copy1MBB->transferSuccessors(BB);
748 // Next, add the true and fallthrough blocks as its successors.
749 BB->addSuccessor(copy0MBB);
750 BB->addSuccessor(copy1MBB);
751
752 // copy0MBB:
753 // %FalseValue = ...
754 // # fallthrough to copy1MBB
755 BB = copy0MBB;
756
757 // Update machine-CFG edges
758 BB->addSuccessor(copy1MBB);
759
760 // copy1MBB:
761 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
762 // ...
763 BB = copy1MBB;
764 BuildMI(BB, dl, TII.get(SystemZ::PHI),
765 MI->getOperand(0).getReg())
766 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
767 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
768
769 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
770 return BB;
771}